]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pbr.c
zebra: Keep track of rules written
[mirror_frr.git] / zebra / zebra_pbr.c
1 /* Zebra Policy Based Routing (PBR) main handling.
2 * Copyright (C) 2018 Cumulus Networks, Inc.
3 *
4 * This file is part of FRR.
5 *
6 * FRR is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FRR; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include <jhash.h>
25 #include <hash.h>
26
27 #include "zebra/zebra_pbr.h"
28 #include "zebra/rt.h"
29
30 /* definitions */
31
32 /* static function declarations */
33
34 /* Private functions */
35
36 /* Public functions */
37 void zebra_pbr_rules_free(void *arg)
38 {
39 struct zebra_pbr_rule *rule;
40
41 rule = (struct zebra_pbr_rule *)arg;
42
43 kernel_del_pbr_rule(rule, NULL);
44 XFREE(MTYPE_TMP, rule);
45 }
46
47 uint32_t zebra_pbr_rules_hash_key(void *arg)
48 {
49 struct zebra_pbr_rule *rule;
50 uint32_t key;
51
52 rule = (struct zebra_pbr_rule *)arg;
53 key = jhash_3words(rule->seq, rule->priority, rule->action.table,
54 prefix_hash_key(&rule->filter.src_ip));
55 return jhash_3words(rule->filter.src_port, rule->filter.dst_port,
56 prefix_hash_key(&rule->filter.dst_ip), key);
57 }
58
59 int zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2)
60 {
61 const struct zebra_pbr_rule *r1, *r2;
62
63 r1 = (const struct zebra_pbr_rule *)arg1;
64 r2 = (const struct zebra_pbr_rule *)arg2;
65
66 if (r1->seq != r2->seq)
67 return 0;
68
69 if (r1->priority != r2->priority)
70 return 0;
71
72 if (r1->action.table != r2->action.table)
73 return 0;
74
75 if (r1->filter.src_port != r2->filter.src_port)
76 return 0;
77
78 if (r1->filter.dst_port != r2->filter.dst_port)
79 return 0;
80
81 if (!prefix_same(&r1->filter.src_ip, &r2->filter.src_ip))
82 return 0;
83
84 if (!prefix_same(&r1->filter.dst_ip, &r2->filter.dst_ip))
85 return 0;
86
87 return 1;
88 }
89
90 static void *pbr_rule_alloc_intern(void *arg)
91 {
92 struct zebra_pbr_rule *zpr;
93 struct zebra_pbr_rule *new;
94
95 zpr = (struct zebra_pbr_rule *)arg;
96
97 new = XCALLOC(MTYPE_TMP, sizeof(*new));
98
99 memcpy(new, zpr, sizeof(*zpr));
100
101 return new;
102 }
103
104 void zebra_pbr_add_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule,
105 struct interface *ifp)
106 {
107 (void)hash_get(zns->rules_hash, rule, pbr_rule_alloc_intern);
108 kernel_add_pbr_rule(rule, ifp);
109 }
110
111 void zebra_pbr_del_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule,
112 struct interface *ifp)
113 {
114 struct zebra_pbr_rule *lookup;
115
116 lookup = hash_lookup(zns->rules_hash, rule);
117 kernel_del_pbr_rule(rule, ifp);
118
119 if (lookup)
120 XFREE(MTYPE_TMP, lookup);
121 else
122 zlog_warn("%s: Rule being deleted we know nothing about",
123 __PRETTY_FUNCTION__);
124 }
125
126 /*
127 * Handle success or failure of rule (un)install in the kernel.
128 */
129 void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule *rule,
130 struct interface *ifp,
131 enum southbound_results res)
132 {
133 }
134
135 /*
136 * Handle rule delete notification from kernel.
137 */
138 int kernel_pbr_rule_del(struct zebra_pbr_rule *rule, struct interface *ifp)
139 {
140 return 0;
141 }