]>
Commit | Line | Data |
---|---|---|
942bf97b | 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 | ||
43fe6a2a DS |
24 | #include <jhash.h> |
25 | #include <hash.h> | |
f80ec7e3 | 26 | #include <memory.h> |
73a829f7 | 27 | #include <hook.h> |
43fe6a2a | 28 | |
7f0ea8a4 | 29 | #include "zebra/zebra_router.h" |
942bf97b | 30 | #include "zebra/zebra_pbr.h" |
31 | #include "zebra/rt.h" | |
bf094f69 | 32 | #include "zebra/zapi_msg.h" |
c0ea1ae7 | 33 | #include "zebra/zserv.h" |
8eeca5a2 | 34 | #include "zebra/debug.h" |
59f47eb0 | 35 | #include "zebra/zebra_neigh.h" |
f80ec7e3 PG |
36 | |
37 | /* definitions */ | |
bf8d3d6a | 38 | DEFINE_MTYPE_STATIC(ZEBRA, PBR_IPTABLE_IFNAME, "PBR interface list"); |
942bf97b | 39 | |
40 | /* definitions */ | |
586f4ccf PG |
41 | static const struct message ipset_type_msg[] = { |
42 | {IPSET_NET_PORT_NET, "net,port,net"}, | |
43 | {IPSET_NET_PORT, "net,port"}, | |
44 | {IPSET_NET_NET, "net,net"}, | |
45 | {IPSET_NET, "net"}, | |
46 | {0} | |
47 | }; | |
942bf97b | 48 | |
be729dd7 PG |
49 | const struct message icmp_typecode_str[] = { |
50 | { 0 << 8, "echo-reply"}, | |
51 | { 0 << 8, "pong"}, | |
52 | { 3 << 8, "network-unreachable"}, | |
53 | { (3 << 8) + 1, "host-unreachable"}, | |
54 | { (3 << 8) + 2, "protocol-unreachable"}, | |
55 | { (3 << 8) + 3, "port-unreachable"}, | |
56 | { (3 << 8) + 4, "fragmentation-needed"}, | |
57 | { (3 << 8) + 5, "source-route-failed"}, | |
58 | { (3 << 8) + 6, "network-unknown"}, | |
59 | { (3 << 8) + 7, "host-unknown"}, | |
60 | { (3 << 8) + 9, "network-prohibited"}, | |
61 | { (3 << 8) + 10, "host-prohibited"}, | |
62 | { (3 << 8) + 11, "TOS-network-unreachable"}, | |
63 | { (3 << 8) + 12, "TOS-host-unreachable"}, | |
64 | { (3 << 8) + 13, "communication-prohibited"}, | |
65 | { (3 << 8) + 14, "host-precedence-violation"}, | |
66 | { (3 << 8) + 15, "precedence-cutoff"}, | |
67 | { 4 << 8, "source-quench"}, | |
68 | { 5 << 8, "network-redirect"}, | |
69 | { (5 << 8) + 1, "host-redirect"}, | |
70 | { (5 << 8) + 2, "TOS-network-redirect"}, | |
71 | { (5 << 8) + 3, "TOS-host-redirect"}, | |
72 | { 8 << 8, "echo-request"}, | |
73 | { 8 << 8, "ping"}, | |
74 | { 9 << 8, "router-advertisement"}, | |
75 | { 10 << 8, "router-solicitation"}, | |
76 | { 11 << 8, "ttl-zero-during-transit"}, | |
77 | { (11 << 8) + 1, "ttl-zero-during-reassembly"}, | |
78 | { 12 << 8, "ip-header-bad"}, | |
79 | { (12 << 8) + 1, "required-option-missing"}, | |
80 | { 13 << 8, "timestamp-request"}, | |
81 | { 14 << 8, "timestamp-reply"}, | |
82 | { 17 << 8, "address-mask-request"}, | |
83 | { 18 << 8, "address-mask-reply"}, | |
84 | {0} | |
85 | }; | |
86 | ||
c9b1139a PG |
87 | const struct message icmpv6_typecode_str[] = { |
88 | { 128 << 8, "echo-request"}, | |
89 | { 129 << 8, "echo-reply"}, | |
90 | { 1 << 8, "no-route"}, | |
91 | { (1 << 8) + 1, "communication-prohibited"}, | |
92 | { (1 << 8) + 3, "address-unreachable"}, | |
93 | { (1 << 8) + 4, "port-unreachable"}, | |
94 | { (2 << 8), "packet-too-big"}, | |
95 | { 3 << 0, "ttl-zero-during-transit"}, | |
96 | { (3 << 8) + 1, "ttl-zero-during-reassembly"}, | |
97 | { 4 << 0, "bad-header"}, | |
98 | { (4 << 0) + 1, "unknown-header-type"}, | |
99 | { (4 << 0) + 2, "unknown-option"}, | |
100 | { 133 << 8, "router-solicitation"}, | |
101 | { 134 << 8, "router-advertisement"}, | |
102 | { 135 << 8, "neighbor-solicitation"}, | |
103 | { 136 << 8, "neighbor-advertisement"}, | |
104 | { 137 << 8, "redirect"}, | |
105 | {0} | |
106 | }; | |
107 | ||
dc993e76 PG |
108 | /* definitions */ |
109 | static const struct message tcp_value_str[] = { | |
110 | {TCP_HEADER_FIN, "FIN"}, | |
111 | {TCP_HEADER_SYN, "SYN"}, | |
112 | {TCP_HEADER_RST, "RST"}, | |
113 | {TCP_HEADER_PSH, "PSH"}, | |
114 | {TCP_HEADER_ACK, "ACK"}, | |
115 | {TCP_HEADER_URG, "URG"}, | |
116 | {0} | |
117 | }; | |
118 | ||
5ac5b7cc PG |
119 | static const struct message fragment_value_str[] = { |
120 | {1, "dont-fragment"}, | |
121 | {2, "is-fragment"}, | |
122 | {4, "first-fragment"}, | |
123 | {8, "last-fragment"}, | |
124 | {0} | |
125 | }; | |
126 | ||
59f47eb0 AK |
127 | struct zebra_pbr_env_display { |
128 | struct zebra_ns *zns; | |
129 | struct vty *vty; | |
130 | char *name; | |
131 | }; | |
132 | ||
942bf97b | 133 | /* static function declarations */ |
1c6fca1f | 134 | DEFINE_HOOK(zebra_pbr_ipset_entry_get_stat, |
62f20a52 DS |
135 | (struct zebra_pbr_ipset_entry *ipset, uint64_t *pkts, |
136 | uint64_t *bytes), | |
8451921b | 137 | (ipset, pkts, bytes)); |
62f20a52 | 138 | |
1c6fca1f | 139 | DEFINE_HOOK(zebra_pbr_iptable_get_stat, |
62f20a52 DS |
140 | (struct zebra_pbr_iptable *iptable, uint64_t *pkts, |
141 | uint64_t *bytes), | |
8451921b | 142 | (iptable, pkts, bytes)); |
62f20a52 | 143 | |
1c6fca1f | 144 | DEFINE_HOOK(zebra_pbr_iptable_update, |
62f20a52 DS |
145 | (int cmd, struct zebra_pbr_iptable *iptable), (cmd, iptable)); |
146 | ||
1c6fca1f | 147 | DEFINE_HOOK(zebra_pbr_ipset_entry_update, |
62f20a52 DS |
148 | (int cmd, struct zebra_pbr_ipset_entry *ipset), (cmd, ipset)); |
149 | ||
1c6fca1f | 150 | DEFINE_HOOK(zebra_pbr_ipset_update, |
62f20a52 | 151 | (int cmd, struct zebra_pbr_ipset *ipset), (cmd, ipset)); |
942bf97b | 152 | |
59f47eb0 AK |
153 | /* resolve nexthop for dataplane (dpdk) programming */ |
154 | static bool zebra_pbr_expand_action; | |
155 | ||
942bf97b | 156 | /* Private functions */ |
157 | ||
158 | /* Public functions */ | |
43fe6a2a | 159 | void zebra_pbr_rules_free(void *arg) |
1fbfe5a5 | 160 | { |
43fe6a2a DS |
161 | struct zebra_pbr_rule *rule; |
162 | ||
163 | rule = (struct zebra_pbr_rule *)arg; | |
164 | ||
f62e5480 | 165 | (void)dplane_pbr_rule_delete(rule); |
43fe6a2a DS |
166 | XFREE(MTYPE_TMP, rule); |
167 | } | |
168 | ||
d8b87afe | 169 | uint32_t zebra_pbr_rules_hash_key(const void *arg) |
43fe6a2a | 170 | { |
d8b87afe | 171 | const struct zebra_pbr_rule *rule; |
43fe6a2a DS |
172 | uint32_t key; |
173 | ||
d8b87afe | 174 | rule = arg; |
5dd0722d PG |
175 | key = jhash_3words(rule->rule.seq, rule->rule.priority, |
176 | rule->rule.action.table, | |
177 | prefix_hash_key(&rule->rule.filter.src_ip)); | |
a0321978 | 178 | |
b94683f0 DS |
179 | key = jhash_3words(rule->rule.filter.fwmark, rule->vrf_id, |
180 | rule->rule.filter.ip_proto, key); | |
58a1d249 DS |
181 | |
182 | key = jhash(rule->ifname, strlen(rule->ifname), key); | |
7f0ea8a4 | 183 | |
5dd0722d PG |
184 | return jhash_3words(rule->rule.filter.src_port, |
185 | rule->rule.filter.dst_port, | |
186 | prefix_hash_key(&rule->rule.filter.dst_ip), | |
187 | jhash_1word(rule->rule.unique, key)); | |
43fe6a2a DS |
188 | } |
189 | ||
74df8d6d | 190 | bool zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2) |
43fe6a2a DS |
191 | { |
192 | const struct zebra_pbr_rule *r1, *r2; | |
193 | ||
194 | r1 = (const struct zebra_pbr_rule *)arg1; | |
195 | r2 = (const struct zebra_pbr_rule *)arg2; | |
196 | ||
5dd0722d | 197 | if (r1->rule.seq != r2->rule.seq) |
74df8d6d | 198 | return false; |
43fe6a2a | 199 | |
5dd0722d | 200 | if (r1->rule.priority != r2->rule.priority) |
74df8d6d | 201 | return false; |
43fe6a2a | 202 | |
5dd0722d | 203 | if (r1->rule.unique != r2->rule.unique) |
74df8d6d | 204 | return false; |
b6c5d343 | 205 | |
5dd0722d | 206 | if (r1->rule.action.table != r2->rule.action.table) |
74df8d6d | 207 | return false; |
43fe6a2a | 208 | |
5dd0722d | 209 | if (r1->rule.filter.src_port != r2->rule.filter.src_port) |
74df8d6d | 210 | return false; |
43fe6a2a | 211 | |
5dd0722d | 212 | if (r1->rule.filter.dst_port != r2->rule.filter.dst_port) |
74df8d6d | 213 | return false; |
43fe6a2a | 214 | |
5dd0722d | 215 | if (r1->rule.filter.fwmark != r2->rule.filter.fwmark) |
74df8d6d | 216 | return false; |
b94683f0 DS |
217 | |
218 | if (r1->rule.filter.ip_proto != r2->rule.filter.ip_proto) | |
219 | return false; | |
1907e4b8 | 220 | |
5dd0722d | 221 | if (!prefix_same(&r1->rule.filter.src_ip, &r2->rule.filter.src_ip)) |
74df8d6d | 222 | return false; |
43fe6a2a | 223 | |
5dd0722d | 224 | if (!prefix_same(&r1->rule.filter.dst_ip, &r2->rule.filter.dst_ip)) |
74df8d6d | 225 | return false; |
43fe6a2a | 226 | |
58a1d249 | 227 | if (strcmp(r1->rule.ifname, r2->rule.ifname) != 0) |
74df8d6d | 228 | return false; |
a0321978 | 229 | |
7f0ea8a4 DS |
230 | if (r1->vrf_id != r2->vrf_id) |
231 | return false; | |
232 | ||
74df8d6d | 233 | return true; |
43fe6a2a DS |
234 | } |
235 | ||
f46bbab4 | 236 | struct pbr_rule_unique_lookup { |
8c3cd6c6 DS |
237 | struct zebra_pbr_rule *rule; |
238 | uint32_t unique; | |
58a1d249 | 239 | char ifname[INTERFACE_NAMSIZ + 1]; |
7f0ea8a4 | 240 | vrf_id_t vrf_id; |
8c3cd6c6 DS |
241 | }; |
242 | ||
e3b78da8 | 243 | static int pbr_rule_lookup_unique_walker(struct hash_bucket *b, void *data) |
8c3cd6c6 | 244 | { |
f46bbab4 | 245 | struct pbr_rule_unique_lookup *pul = data; |
8c3cd6c6 DS |
246 | struct zebra_pbr_rule *rule = b->data; |
247 | ||
7f0ea8a4 | 248 | if (pul->unique == rule->rule.unique |
58a1d249 | 249 | && strncmp(pul->ifname, rule->rule.ifname, INTERFACE_NAMSIZ) == 0 |
7f0ea8a4 | 250 | && pul->vrf_id == rule->vrf_id) { |
8c3cd6c6 DS |
251 | pul->rule = rule; |
252 | return HASHWALK_ABORT; | |
253 | } | |
254 | ||
255 | return HASHWALK_CONTINUE; | |
256 | } | |
257 | ||
62f20a52 DS |
258 | static struct zebra_pbr_rule * |
259 | pbr_rule_lookup_unique(struct zebra_pbr_rule *zrule) | |
8c3cd6c6 | 260 | { |
f46bbab4 | 261 | struct pbr_rule_unique_lookup pul; |
8c3cd6c6 | 262 | |
7f0ea8a4 | 263 | pul.unique = zrule->rule.unique; |
58a1d249 | 264 | strlcpy(pul.ifname, zrule->rule.ifname, INTERFACE_NAMSIZ); |
8c3cd6c6 | 265 | pul.rule = NULL; |
7f0ea8a4 DS |
266 | pul.vrf_id = zrule->vrf_id; |
267 | hash_walk(zrouter.rules_hash, &pbr_rule_lookup_unique_walker, &pul); | |
8c3cd6c6 DS |
268 | |
269 | return pul.rule; | |
270 | } | |
271 | ||
7661461a PG |
272 | void zebra_pbr_ipset_free(void *arg) |
273 | { | |
274 | struct zebra_pbr_ipset *ipset; | |
275 | ||
276 | ipset = (struct zebra_pbr_ipset *)arg; | |
1c6fca1f | 277 | hook_call(zebra_pbr_ipset_update, 0, ipset); |
7661461a PG |
278 | XFREE(MTYPE_TMP, ipset); |
279 | } | |
280 | ||
d8b87afe | 281 | uint32_t zebra_pbr_ipset_hash_key(const void *arg) |
7661461a | 282 | { |
d8b87afe | 283 | const struct zebra_pbr_ipset *ipset = arg; |
425bdd6b | 284 | uint32_t *pnt = (uint32_t *)&ipset->ipset_name; |
62f20a52 | 285 | uint32_t key = jhash_1word(ipset->vrf_id, 0x63ab42de); |
7661461a | 286 | |
a60b7031 PG |
287 | key = jhash_1word(ipset->family, key); |
288 | ||
62f20a52 | 289 | return jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE, key); |
7661461a PG |
290 | } |
291 | ||
74df8d6d | 292 | bool zebra_pbr_ipset_hash_equal(const void *arg1, const void *arg2) |
7661461a PG |
293 | { |
294 | const struct zebra_pbr_ipset *r1, *r2; | |
295 | ||
296 | r1 = (const struct zebra_pbr_ipset *)arg1; | |
297 | r2 = (const struct zebra_pbr_ipset *)arg2; | |
298 | ||
299 | if (r1->type != r2->type) | |
74df8d6d | 300 | return false; |
7661461a | 301 | if (r1->unique != r2->unique) |
74df8d6d | 302 | return false; |
62f20a52 DS |
303 | if (r1->vrf_id != r2->vrf_id) |
304 | return false; | |
a60b7031 PG |
305 | if (r1->family != r2->family) |
306 | return false; | |
62f20a52 | 307 | |
7661461a PG |
308 | if (strncmp(r1->ipset_name, r2->ipset_name, |
309 | ZEBRA_IPSET_NAME_SIZE)) | |
74df8d6d DS |
310 | return false; |
311 | return true; | |
7661461a PG |
312 | } |
313 | ||
314 | void zebra_pbr_ipset_entry_free(void *arg) | |
315 | { | |
316 | struct zebra_pbr_ipset_entry *ipset; | |
317 | ||
318 | ipset = (struct zebra_pbr_ipset_entry *)arg; | |
73a829f7 | 319 | |
1c6fca1f | 320 | hook_call(zebra_pbr_ipset_entry_update, 0, ipset); |
7661461a PG |
321 | |
322 | XFREE(MTYPE_TMP, ipset); | |
323 | } | |
324 | ||
d8b87afe | 325 | uint32_t zebra_pbr_ipset_entry_hash_key(const void *arg) |
7661461a | 326 | { |
d8b87afe | 327 | const struct zebra_pbr_ipset_entry *ipset; |
7661461a PG |
328 | uint32_t key; |
329 | ||
d8b87afe | 330 | ipset = arg; |
7661461a PG |
331 | key = prefix_hash_key(&ipset->src); |
332 | key = jhash_1word(ipset->unique, key); | |
333 | key = jhash_1word(prefix_hash_key(&ipset->dst), key); | |
25d760c5 PG |
334 | key = jhash(&ipset->dst_port_min, 2, key); |
335 | key = jhash(&ipset->dst_port_max, 2, key); | |
336 | key = jhash(&ipset->src_port_min, 2, key); | |
337 | key = jhash(&ipset->src_port_max, 2, key); | |
338 | key = jhash(&ipset->proto, 1, key); | |
7661461a PG |
339 | |
340 | return key; | |
341 | } | |
342 | ||
74df8d6d | 343 | bool zebra_pbr_ipset_entry_hash_equal(const void *arg1, const void *arg2) |
7661461a PG |
344 | { |
345 | const struct zebra_pbr_ipset_entry *r1, *r2; | |
346 | ||
347 | r1 = (const struct zebra_pbr_ipset_entry *)arg1; | |
348 | r2 = (const struct zebra_pbr_ipset_entry *)arg2; | |
349 | ||
350 | if (r1->unique != r2->unique) | |
74df8d6d | 351 | return false; |
7661461a PG |
352 | |
353 | if (!prefix_same(&r1->src, &r2->src)) | |
74df8d6d | 354 | return false; |
7661461a PG |
355 | |
356 | if (!prefix_same(&r1->dst, &r2->dst)) | |
74df8d6d | 357 | return false; |
7661461a | 358 | |
25d760c5 | 359 | if (r1->src_port_min != r2->src_port_min) |
74df8d6d | 360 | return false; |
25d760c5 PG |
361 | |
362 | if (r1->src_port_max != r2->src_port_max) | |
74df8d6d | 363 | return false; |
25d760c5 PG |
364 | |
365 | if (r1->dst_port_min != r2->dst_port_min) | |
74df8d6d | 366 | return false; |
25d760c5 PG |
367 | |
368 | if (r1->dst_port_max != r2->dst_port_max) | |
74df8d6d | 369 | return false; |
25d760c5 PG |
370 | |
371 | if (r1->proto != r2->proto) | |
74df8d6d DS |
372 | return false; |
373 | return true; | |
7661461a PG |
374 | } |
375 | ||
3b1de7b8 PG |
376 | /* this function gives option to flush plugin memory contexts |
377 | * with all parameter. set it to true to flush all | |
378 | * set it to false to flush only passed arg argument | |
379 | */ | |
380 | static void _zebra_pbr_iptable_free_all(void *arg, bool all) | |
7abd6c4f PG |
381 | { |
382 | struct zebra_pbr_iptable *iptable; | |
f80ec7e3 PG |
383 | struct listnode *node, *nnode; |
384 | char *name; | |
7abd6c4f PG |
385 | |
386 | iptable = (struct zebra_pbr_iptable *)arg; | |
3b1de7b8 PG |
387 | |
388 | if (all) | |
389 | hook_call(zebra_pbr_iptable_update, 0, iptable); | |
7abd6c4f | 390 | |
8b5c4dce QY |
391 | if (iptable->interface_name_list) { |
392 | for (ALL_LIST_ELEMENTS(iptable->interface_name_list, node, | |
393 | nnode, name)) { | |
394 | XFREE(MTYPE_PBR_IPTABLE_IFNAME, name); | |
395 | list_delete_node(iptable->interface_name_list, node); | |
396 | } | |
397 | list_delete(&iptable->interface_name_list); | |
f80ec7e3 | 398 | } |
7abd6c4f PG |
399 | XFREE(MTYPE_TMP, iptable); |
400 | } | |
401 | ||
3b1de7b8 PG |
402 | void zebra_pbr_iptable_free(void *arg) |
403 | { | |
404 | _zebra_pbr_iptable_free_all(arg, false); | |
405 | } | |
406 | ||
d8b87afe | 407 | uint32_t zebra_pbr_iptable_hash_key(const void *arg) |
7abd6c4f | 408 | { |
d8b87afe | 409 | const struct zebra_pbr_iptable *iptable = arg; |
7abd6c4f PG |
410 | uint32_t *pnt = (uint32_t *)&(iptable->ipset_name); |
411 | uint32_t key; | |
412 | ||
413 | key = jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE, | |
414 | 0x63ab42de); | |
415 | key = jhash_1word(iptable->fwmark, key); | |
a60b7031 PG |
416 | key = jhash_1word(iptable->family, key); |
417 | key = jhash_1word(iptable->flow_label, key); | |
e7f7dad4 PG |
418 | key = jhash_1word(iptable->pkt_len_min, key); |
419 | key = jhash_1word(iptable->pkt_len_max, key); | |
dc993e76 PG |
420 | key = jhash_1word(iptable->tcp_flags, key); |
421 | key = jhash_1word(iptable->tcp_mask_flags, key); | |
4977bd6c | 422 | key = jhash_1word(iptable->dscp_value, key); |
f449d223 | 423 | key = jhash_1word(iptable->protocol, key); |
5ac5b7cc | 424 | key = jhash_1word(iptable->fragment, key); |
62f20a52 DS |
425 | key = jhash_1word(iptable->vrf_id, key); |
426 | ||
7abd6c4f PG |
427 | return jhash_3words(iptable->filter_bm, iptable->type, |
428 | iptable->unique, key); | |
429 | } | |
430 | ||
74df8d6d | 431 | bool zebra_pbr_iptable_hash_equal(const void *arg1, const void *arg2) |
7abd6c4f PG |
432 | { |
433 | const struct zebra_pbr_iptable *r1, *r2; | |
434 | ||
435 | r1 = (const struct zebra_pbr_iptable *)arg1; | |
436 | r2 = (const struct zebra_pbr_iptable *)arg2; | |
437 | ||
62f20a52 | 438 | if (r1->vrf_id != r2->vrf_id) |
b08047f8 | 439 | return false; |
7abd6c4f | 440 | if (r1->type != r2->type) |
74df8d6d | 441 | return false; |
7abd6c4f | 442 | if (r1->unique != r2->unique) |
74df8d6d | 443 | return false; |
7abd6c4f | 444 | if (r1->filter_bm != r2->filter_bm) |
74df8d6d | 445 | return false; |
7abd6c4f | 446 | if (r1->fwmark != r2->fwmark) |
74df8d6d | 447 | return false; |
7abd6c4f | 448 | if (r1->action != r2->action) |
74df8d6d | 449 | return false; |
7abd6c4f PG |
450 | if (strncmp(r1->ipset_name, r2->ipset_name, |
451 | ZEBRA_IPSET_NAME_SIZE)) | |
74df8d6d | 452 | return false; |
a60b7031 PG |
453 | if (r1->family != r2->family) |
454 | return false; | |
455 | if (r1->flow_label != r2->flow_label) | |
456 | return false; | |
e7f7dad4 | 457 | if (r1->pkt_len_min != r2->pkt_len_min) |
74df8d6d | 458 | return false; |
e7f7dad4 | 459 | if (r1->pkt_len_max != r2->pkt_len_max) |
74df8d6d | 460 | return false; |
dc993e76 | 461 | if (r1->tcp_flags != r2->tcp_flags) |
74df8d6d | 462 | return false; |
dc993e76 | 463 | if (r1->tcp_mask_flags != r2->tcp_mask_flags) |
74df8d6d | 464 | return false; |
4977bd6c | 465 | if (r1->dscp_value != r2->dscp_value) |
74df8d6d | 466 | return false; |
5ac5b7cc | 467 | if (r1->fragment != r2->fragment) |
74df8d6d | 468 | return false; |
f449d223 PG |
469 | if (r1->protocol != r2->protocol) |
470 | return false; | |
74df8d6d | 471 | return true; |
7abd6c4f PG |
472 | } |
473 | ||
43fe6a2a DS |
474 | static void *pbr_rule_alloc_intern(void *arg) |
475 | { | |
476 | struct zebra_pbr_rule *zpr; | |
477 | struct zebra_pbr_rule *new; | |
478 | ||
479 | zpr = (struct zebra_pbr_rule *)arg; | |
480 | ||
481 | new = XCALLOC(MTYPE_TMP, sizeof(*new)); | |
482 | ||
483 | memcpy(new, zpr, sizeof(*zpr)); | |
484 | ||
485 | return new; | |
486 | } | |
487 | ||
59f47eb0 AK |
488 | static struct zebra_pbr_rule *pbr_rule_free(struct zebra_pbr_rule *hash_data, |
489 | bool free_data) | |
490 | { | |
491 | if (hash_data->action.neigh) | |
492 | zebra_neigh_deref(hash_data); | |
493 | hash_release(zrouter.rules_hash, hash_data); | |
494 | if (free_data) { | |
495 | XFREE(MTYPE_TMP, hash_data); | |
496 | return NULL; | |
497 | } | |
498 | ||
499 | return hash_data; | |
500 | } | |
501 | ||
502 | static struct zebra_pbr_rule *pbr_rule_release(struct zebra_pbr_rule *rule, | |
503 | bool free_data) | |
3ae327cb SW |
504 | { |
505 | struct zebra_pbr_rule *lookup; | |
506 | ||
507 | lookup = hash_lookup(zrouter.rules_hash, rule); | |
508 | ||
509 | if (!lookup) | |
59f47eb0 | 510 | return NULL; |
3ae327cb | 511 | |
59f47eb0 AK |
512 | return pbr_rule_free(lookup, free_data); |
513 | } | |
3ae327cb | 514 | |
59f47eb0 AK |
515 | void zebra_pbr_show_rule_unit(struct zebra_pbr_rule *rule, struct vty *vty) |
516 | { | |
517 | struct pbr_rule *prule = &rule->rule; | |
518 | struct zebra_pbr_action *zaction = &rule->action; | |
519 | ||
520 | vty_out(vty, "Rules if %s\n", rule->ifname); | |
521 | vty_out(vty, " Seq %u pri %u\n", prule->seq, prule->priority); | |
522 | if (prule->filter.filter_bm & PBR_FILTER_SRC_IP) | |
523 | vty_out(vty, " SRC IP Match: %pFX\n", &prule->filter.src_ip); | |
524 | if (prule->filter.filter_bm & PBR_FILTER_DST_IP) | |
525 | vty_out(vty, " DST IP Match: %pFX\n", &prule->filter.dst_ip); | |
526 | if (prule->filter.filter_bm & PBR_FILTER_IP_PROTOCOL) | |
527 | vty_out(vty, " IP protocol Match: %u\n", | |
528 | prule->filter.ip_proto); | |
529 | if (prule->filter.filter_bm & PBR_FILTER_SRC_PORT) | |
530 | vty_out(vty, " SRC Port Match: %u\n", prule->filter.src_port); | |
531 | if (prule->filter.filter_bm & PBR_FILTER_DST_PORT) | |
532 | vty_out(vty, " DST Port Match: %u\n", prule->filter.dst_port); | |
533 | ||
534 | if (prule->filter.filter_bm & PBR_FILTER_DSFIELD) { | |
535 | vty_out(vty, " DSCP Match: %u\n", | |
536 | (prule->filter.dsfield & PBR_DSFIELD_DSCP) >> 2); | |
537 | vty_out(vty, " ECN Match: %u\n", | |
538 | prule->filter.dsfield & PBR_DSFIELD_ECN); | |
539 | } | |
540 | ||
541 | if (prule->filter.filter_bm & PBR_FILTER_FWMARK) | |
542 | vty_out(vty, " MARK Match: %u\n", prule->filter.fwmark); | |
543 | ||
544 | vty_out(vty, " Tableid: %u\n", prule->action.table); | |
545 | if (zaction->afi == AFI_IP) | |
546 | vty_out(vty, " Action: nh: %pI4 intf: %s\n", | |
547 | &zaction->gate.ipv4, | |
548 | ifindex2ifname(zaction->ifindex, rule->vrf_id)); | |
549 | if (zaction->afi == AFI_IP6) | |
550 | vty_out(vty, " Action: nh: %pI6 intf: %s\n", | |
551 | &zaction->gate.ipv6, | |
552 | ifindex2ifname(zaction->ifindex, rule->vrf_id)); | |
553 | if (zaction->neigh && (zaction->neigh->flags & ZEBRA_NEIGH_ENT_ACTIVE)) | |
554 | vty_out(vty, " Action: mac: %pEA\n", &zaction->neigh->mac); | |
555 | } | |
556 | ||
557 | static int zebra_pbr_show_rules_walkcb(struct hash_bucket *bucket, void *arg) | |
558 | { | |
559 | struct zebra_pbr_rule *rule = (struct zebra_pbr_rule *)bucket->data; | |
560 | struct zebra_pbr_env_display *env = (struct zebra_pbr_env_display *)arg; | |
561 | struct vty *vty = env->vty; | |
562 | ||
563 | zebra_pbr_show_rule_unit(rule, vty); | |
564 | ||
565 | return HASHWALK_CONTINUE; | |
566 | } | |
567 | ||
568 | void zebra_pbr_show_rule(struct vty *vty) | |
569 | { | |
570 | struct zebra_pbr_env_display env; | |
571 | ||
572 | env.vty = vty; | |
573 | hash_walk(zrouter.rules_hash, zebra_pbr_show_rules_walkcb, &env); | |
574 | } | |
575 | ||
576 | void zebra_pbr_config_write(struct vty *vty) | |
577 | { | |
578 | if (zebra_pbr_expand_action) | |
579 | vty_out(vty, "pbr nexthop-resolve\n"); | |
580 | } | |
581 | ||
582 | void zebra_pbr_expand_action_update(bool enable) | |
583 | { | |
584 | zebra_pbr_expand_action = enable; | |
585 | } | |
586 | ||
587 | static void zebra_pbr_expand_rule(struct zebra_pbr_rule *rule) | |
588 | { | |
589 | struct prefix p; | |
590 | struct route_table *table; | |
591 | struct route_node *rn; | |
592 | rib_dest_t *dest; | |
593 | struct route_entry *re; | |
594 | const struct nexthop_group *nhg; | |
595 | const struct nexthop *nexthop; | |
596 | struct zebra_pbr_action *action = &rule->action; | |
597 | struct ipaddr ip; | |
598 | ||
599 | if (!zebra_pbr_expand_action) | |
600 | return; | |
601 | ||
602 | table = zebra_vrf_get_table_with_table_id( | |
603 | AFI_IP, SAFI_UNICAST, VRF_DEFAULT, rule->rule.action.table); | |
604 | if (!table) | |
605 | return; | |
606 | ||
607 | memset(&p, 0, sizeof(p)); | |
608 | p.family = AF_INET; | |
609 | ||
610 | rn = route_node_lookup(table, &p); | |
611 | if (!rn) | |
612 | return; | |
613 | ||
614 | dest = rib_dest_from_rnode(rn); | |
615 | re = dest->selected_fib; | |
616 | if (!re) { | |
617 | route_unlock_node(rn); | |
618 | return; | |
619 | } | |
620 | ||
621 | nhg = rib_get_fib_nhg(re); | |
622 | if (!nhg) { | |
623 | route_unlock_node(rn); | |
624 | return; | |
625 | } | |
626 | ||
627 | nexthop = nhg->nexthop; | |
628 | if (nexthop) { | |
629 | switch (nexthop->type) { | |
630 | case NEXTHOP_TYPE_IPV4: | |
631 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
632 | action->afi = AFI_IP; | |
633 | action->gate.ipv4 = nexthop->gate.ipv4; | |
634 | action->ifindex = nexthop->ifindex; | |
635 | ip.ipa_type = AF_INET; | |
636 | ip.ipaddr_v4 = action->gate.ipv4; | |
637 | zebra_neigh_ref(action->ifindex, &ip, rule); | |
638 | break; | |
639 | ||
640 | case NEXTHOP_TYPE_IPV6: | |
641 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
642 | action->afi = AFI_IP6; | |
643 | action->gate.ipv6 = nexthop->gate.ipv6; | |
644 | action->ifindex = nexthop->ifindex; | |
645 | ip.ipa_type = AF_INET6; | |
646 | ip.ipaddr_v6 = action->gate.ipv6; | |
647 | zebra_neigh_ref(action->ifindex, &ip, rule); | |
648 | break; | |
649 | ||
650 | default: | |
651 | action->afi = AFI_UNSPEC; | |
652 | } | |
653 | } | |
654 | ||
655 | route_unlock_node(rn); | |
3ae327cb SW |
656 | } |
657 | ||
7f0ea8a4 | 658 | void zebra_pbr_add_rule(struct zebra_pbr_rule *rule) |
43fe6a2a | 659 | { |
3ae327cb | 660 | struct zebra_pbr_rule *found; |
59f47eb0 AK |
661 | struct zebra_pbr_rule *old; |
662 | struct zebra_pbr_rule *new; | |
8c3cd6c6 | 663 | |
3ae327cb SW |
664 | /** |
665 | * Check if we already have it (this checks via a unique ID, walking | |
666 | * over the hash table, not via a hash operation). | |
8c3cd6c6 | 667 | */ |
3ae327cb SW |
668 | found = pbr_rule_lookup_unique(rule); |
669 | ||
3ae327cb SW |
670 | /* If found, this is an update */ |
671 | if (found) { | |
8eeca5a2 SW |
672 | if (IS_ZEBRA_DEBUG_PBR) |
673 | zlog_debug( | |
674 | "%s: seq: %d, prior: %d, unique: %d, ifname: %s -- update", | |
675 | __func__, rule->rule.seq, rule->rule.priority, | |
676 | rule->rule.unique, rule->rule.ifname); | |
677 | ||
59f47eb0 AK |
678 | /* remove the old entry from the hash but don't free the hash |
679 | * data yet as we need it for the dplane update | |
680 | */ | |
681 | old = pbr_rule_release(found, false); | |
682 | ||
683 | /* insert new entry into hash */ | |
684 | new = hash_get(zrouter.rules_hash, rule, pbr_rule_alloc_intern); | |
685 | /* expand the action if needed */ | |
686 | zebra_pbr_expand_rule(new); | |
687 | /* update dataplane */ | |
688 | (void)dplane_pbr_rule_update(found, new); | |
689 | /* release the old hash data */ | |
690 | if (old) | |
691 | XFREE(MTYPE_TMP, old); | |
8eeca5a2 SW |
692 | } else { |
693 | if (IS_ZEBRA_DEBUG_PBR) | |
694 | zlog_debug( | |
695 | "%s: seq: %d, prior: %d, unique: %d, ifname: %s -- new", | |
696 | __func__, rule->rule.seq, rule->rule.priority, | |
697 | rule->rule.unique, rule->rule.ifname); | |
698 | ||
59f47eb0 AK |
699 | /* insert new entry into hash */ |
700 | new = hash_get(zrouter.rules_hash, rule, pbr_rule_alloc_intern); | |
701 | /* expand the action if needed */ | |
702 | zebra_pbr_expand_rule(new); | |
703 | (void)dplane_pbr_rule_add(new); | |
8eeca5a2 | 704 | } |
f7692085 | 705 | |
1fbfe5a5 DS |
706 | } |
707 | ||
7f0ea8a4 | 708 | void zebra_pbr_del_rule(struct zebra_pbr_rule *rule) |
1fbfe5a5 | 709 | { |
8eeca5a2 SW |
710 | if (IS_ZEBRA_DEBUG_PBR) |
711 | zlog_debug("%s: seq: %d, prior: %d, unique: %d, ifname: %s", | |
712 | __func__, rule->rule.seq, rule->rule.priority, | |
713 | rule->rule.unique, rule->rule.ifname); | |
714 | ||
f62e5480 | 715 | (void)dplane_pbr_rule_delete(rule); |
43fe6a2a | 716 | |
59f47eb0 | 717 | if (pbr_rule_release(rule, true)) |
9df414fe | 718 | zlog_debug("%s: Rule being deleted we know nothing about", |
15569c58 | 719 | __func__); |
1fbfe5a5 DS |
720 | } |
721 | ||
5162e000 PG |
722 | void zebra_pbr_process_iptable(struct zebra_dplane_ctx *ctx) |
723 | { | |
724 | int mode, ret = 0; | |
725 | struct zebra_pbr_iptable ipt; | |
726 | ||
727 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_IPTABLE_ADD) | |
728 | mode = 1; | |
729 | else | |
730 | mode = 0; | |
731 | ||
8d78e148 DS |
732 | dplane_ctx_get_pbr_iptable(ctx, &ipt); |
733 | ||
734 | ret = hook_call(zebra_pbr_iptable_update, mode, &ipt); | |
735 | if (ret) | |
736 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS); | |
737 | else | |
5162e000 PG |
738 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_FAILURE); |
739 | } | |
740 | ||
ef524230 PG |
741 | void zebra_pbr_process_ipset(struct zebra_dplane_ctx *ctx) |
742 | { | |
743 | int mode, ret = 0; | |
744 | struct zebra_pbr_ipset ipset; | |
745 | ||
746 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_IPSET_ADD) | |
747 | mode = 1; | |
748 | else | |
749 | mode = 0; | |
8249f96a DS |
750 | |
751 | dplane_ctx_get_pbr_ipset(ctx, &ipset); | |
752 | ||
753 | ret = hook_call(zebra_pbr_ipset_update, mode, &ipset); | |
754 | if (ret) | |
755 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS); | |
756 | else | |
ef524230 PG |
757 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_FAILURE); |
758 | } | |
759 | ||
760 | void zebra_pbr_process_ipset_entry(struct zebra_dplane_ctx *ctx) | |
761 | { | |
762 | int mode, ret = 0; | |
763 | struct zebra_pbr_ipset_entry ipset_entry; | |
764 | struct zebra_pbr_ipset ipset; | |
765 | ||
766 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_IPSET_ENTRY_ADD) | |
767 | mode = 1; | |
768 | else | |
769 | mode = 0; | |
770 | ||
f284c132 | 771 | dplane_ctx_get_pbr_ipset_entry(ctx, &ipset_entry); |
8249f96a DS |
772 | dplane_ctx_get_pbr_ipset(ctx, &ipset); |
773 | ||
ef524230 PG |
774 | ipset_entry.backpointer = &ipset; |
775 | ||
776 | ret = hook_call(zebra_pbr_ipset_entry_update, mode, &ipset_entry); | |
777 | if (ret) | |
778 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS); | |
779 | else | |
780 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_FAILURE); | |
781 | } | |
782 | ||
e3b78da8 | 783 | static void zebra_pbr_cleanup_rules(struct hash_bucket *b, void *data) |
e69aa084 | 784 | { |
e69aa084 DS |
785 | struct zebra_pbr_rule *rule = b->data; |
786 | int *sock = data; | |
787 | ||
788 | if (rule->sock == *sock) { | |
f62e5480 | 789 | (void)dplane_pbr_rule_delete(rule); |
59f47eb0 | 790 | pbr_rule_free(rule, true); |
e69aa084 DS |
791 | } |
792 | } | |
793 | ||
e3b78da8 | 794 | static void zebra_pbr_cleanup_ipset(struct hash_bucket *b, void *data) |
c2ef5232 | 795 | { |
c2ef5232 PG |
796 | struct zebra_pbr_ipset *ipset = b->data; |
797 | int *sock = data; | |
798 | ||
73a829f7 | 799 | if (ipset->sock == *sock) { |
b147e204 QY |
800 | if (hash_release(zrouter.ipset_hash, ipset)) |
801 | zebra_pbr_ipset_free(ipset); | |
802 | else | |
803 | hook_call(zebra_pbr_ipset_update, 0, ipset); | |
73a829f7 | 804 | } |
c2ef5232 PG |
805 | } |
806 | ||
e3b78da8 | 807 | static void zebra_pbr_cleanup_ipset_entry(struct hash_bucket *b, void *data) |
c2ef5232 | 808 | { |
c2ef5232 PG |
809 | struct zebra_pbr_ipset_entry *ipset = b->data; |
810 | int *sock = data; | |
811 | ||
73a829f7 | 812 | if (ipset->sock == *sock) { |
b147e204 QY |
813 | if (hash_release(zrouter.ipset_entry_hash, ipset)) |
814 | zebra_pbr_ipset_entry_free(ipset); | |
815 | else | |
816 | hook_call(zebra_pbr_ipset_entry_update, 0, ipset); | |
73a829f7 | 817 | } |
c2ef5232 PG |
818 | } |
819 | ||
e3b78da8 | 820 | static void zebra_pbr_cleanup_iptable(struct hash_bucket *b, void *data) |
c2ef5232 | 821 | { |
c2ef5232 PG |
822 | struct zebra_pbr_iptable *iptable = b->data; |
823 | int *sock = data; | |
824 | ||
73a829f7 | 825 | if (iptable->sock == *sock) { |
b147e204 | 826 | if (hash_release(zrouter.iptable_hash, iptable)) |
3b1de7b8 | 827 | _zebra_pbr_iptable_free_all(iptable, true); |
b147e204 QY |
828 | else |
829 | hook_call(zebra_pbr_iptable_update, 0, iptable); | |
73a829f7 | 830 | } |
c2ef5232 PG |
831 | } |
832 | ||
4c0ec639 | 833 | static int zebra_pbr_client_close_cleanup(struct zserv *client) |
e69aa084 | 834 | { |
4c0ec639 | 835 | int sock = client->sock; |
e69aa084 | 836 | |
4c0ec639 PG |
837 | if (!sock) |
838 | return 0; | |
7f0ea8a4 | 839 | hash_iterate(zrouter.rules_hash, zebra_pbr_cleanup_rules, &sock); |
62f20a52 DS |
840 | hash_iterate(zrouter.iptable_hash, zebra_pbr_cleanup_iptable, &sock); |
841 | hash_iterate(zrouter.ipset_entry_hash, zebra_pbr_cleanup_ipset_entry, | |
842 | &sock); | |
843 | hash_iterate(zrouter.ipset_hash, zebra_pbr_cleanup_ipset, &sock); | |
4c0ec639 PG |
844 | return 1; |
845 | } | |
846 | ||
847 | void zebra_pbr_init(void) | |
848 | { | |
c0ea1ae7 | 849 | hook_register(zserv_client_close, zebra_pbr_client_close_cleanup); |
e69aa084 DS |
850 | } |
851 | ||
7661461a PG |
852 | static void *pbr_ipset_alloc_intern(void *arg) |
853 | { | |
854 | struct zebra_pbr_ipset *zpi; | |
855 | struct zebra_pbr_ipset *new; | |
856 | ||
857 | zpi = (struct zebra_pbr_ipset *)arg; | |
858 | ||
859 | new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset)); | |
860 | ||
861 | memcpy(new, zpi, sizeof(*zpi)); | |
862 | ||
863 | return new; | |
864 | } | |
865 | ||
62f20a52 | 866 | void zebra_pbr_create_ipset(struct zebra_pbr_ipset *ipset) |
7661461a | 867 | { |
62f20a52 | 868 | (void)hash_get(zrouter.ipset_hash, ipset, pbr_ipset_alloc_intern); |
ef524230 | 869 | (void)dplane_pbr_ipset_add(ipset); |
7661461a PG |
870 | } |
871 | ||
62f20a52 | 872 | void zebra_pbr_destroy_ipset(struct zebra_pbr_ipset *ipset) |
7661461a PG |
873 | { |
874 | struct zebra_pbr_ipset *lookup; | |
875 | ||
62f20a52 | 876 | lookup = hash_lookup(zrouter.ipset_hash, ipset); |
ef524230 | 877 | (void)dplane_pbr_ipset_delete(ipset); |
de67547d | 878 | if (lookup) { |
62f20a52 | 879 | hash_release(zrouter.ipset_hash, lookup); |
7661461a | 880 | XFREE(MTYPE_TMP, lookup); |
de67547d | 881 | } else |
9df414fe QY |
882 | zlog_debug( |
883 | "%s: IPSet Entry being deleted we know nothing about", | |
15569c58 | 884 | __func__); |
7661461a PG |
885 | } |
886 | ||
ed78b7c8 PG |
887 | struct pbr_ipset_name_lookup { |
888 | struct zebra_pbr_ipset *ipset; | |
889 | char ipset_name[ZEBRA_IPSET_NAME_SIZE]; | |
890 | }; | |
891 | ||
5b0d92b8 | 892 | const char *zebra_pbr_ipset_type2str(uint32_t type) |
586f4ccf PG |
893 | { |
894 | return lookup_msg(ipset_type_msg, type, | |
895 | "Unrecognized IPset Type"); | |
896 | } | |
897 | ||
e3b78da8 | 898 | static int zebra_pbr_ipset_pername_walkcb(struct hash_bucket *bucket, void *arg) |
ed78b7c8 PG |
899 | { |
900 | struct pbr_ipset_name_lookup *pinl = | |
901 | (struct pbr_ipset_name_lookup *)arg; | |
e3b78da8 | 902 | struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data; |
ed78b7c8 PG |
903 | |
904 | if (!strncmp(pinl->ipset_name, zpi->ipset_name, | |
905 | ZEBRA_IPSET_NAME_SIZE)) { | |
906 | pinl->ipset = zpi; | |
907 | return HASHWALK_ABORT; | |
908 | } | |
909 | return HASHWALK_CONTINUE; | |
910 | } | |
911 | ||
62f20a52 | 912 | struct zebra_pbr_ipset *zebra_pbr_lookup_ipset_pername(char *ipsetname) |
d59c13af | 913 | { |
ed78b7c8 PG |
914 | struct pbr_ipset_name_lookup pinl; |
915 | struct pbr_ipset_name_lookup *ptr = &pinl; | |
916 | ||
d59c13af PG |
917 | if (!ipsetname) |
918 | return NULL; | |
ed78b7c8 PG |
919 | memset(ptr, 0, sizeof(struct pbr_ipset_name_lookup)); |
920 | snprintf((char *)ptr->ipset_name, ZEBRA_IPSET_NAME_SIZE, "%s", | |
921 | ipsetname); | |
62f20a52 | 922 | hash_walk(zrouter.ipset_hash, zebra_pbr_ipset_pername_walkcb, ptr); |
ed78b7c8 | 923 | return ptr->ipset; |
d59c13af PG |
924 | } |
925 | ||
7661461a PG |
926 | static void *pbr_ipset_entry_alloc_intern(void *arg) |
927 | { | |
928 | struct zebra_pbr_ipset_entry *zpi; | |
929 | struct zebra_pbr_ipset_entry *new; | |
930 | ||
931 | zpi = (struct zebra_pbr_ipset_entry *)arg; | |
932 | ||
933 | new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset_entry)); | |
934 | ||
935 | memcpy(new, zpi, sizeof(*zpi)); | |
936 | ||
937 | return new; | |
938 | } | |
939 | ||
62f20a52 | 940 | void zebra_pbr_add_ipset_entry(struct zebra_pbr_ipset_entry *ipset) |
7661461a | 941 | { |
62f20a52 | 942 | (void)hash_get(zrouter.ipset_entry_hash, ipset, |
7661461a | 943 | pbr_ipset_entry_alloc_intern); |
ef524230 | 944 | (void)dplane_pbr_ipset_entry_add(ipset); |
7661461a PG |
945 | } |
946 | ||
62f20a52 | 947 | void zebra_pbr_del_ipset_entry(struct zebra_pbr_ipset_entry *ipset) |
7661461a PG |
948 | { |
949 | struct zebra_pbr_ipset_entry *lookup; | |
950 | ||
62f20a52 | 951 | lookup = hash_lookup(zrouter.ipset_entry_hash, ipset); |
ef524230 | 952 | (void)dplane_pbr_ipset_entry_delete(ipset); |
de67547d | 953 | if (lookup) { |
62f20a52 | 954 | hash_release(zrouter.ipset_entry_hash, lookup); |
7661461a | 955 | XFREE(MTYPE_TMP, lookup); |
de67547d | 956 | } else |
9df414fe | 957 | zlog_debug("%s: IPSet being deleted we know nothing about", |
15569c58 | 958 | __func__); |
7661461a PG |
959 | } |
960 | ||
7abd6c4f PG |
961 | static void *pbr_iptable_alloc_intern(void *arg) |
962 | { | |
963 | struct zebra_pbr_iptable *zpi; | |
964 | struct zebra_pbr_iptable *new; | |
592af4cc QY |
965 | struct listnode *ln; |
966 | char *ifname; | |
7abd6c4f PG |
967 | |
968 | zpi = (struct zebra_pbr_iptable *)arg; | |
969 | ||
970 | new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable)); | |
971 | ||
592af4cc | 972 | /* Deep structure copy */ |
7abd6c4f | 973 | memcpy(new, zpi, sizeof(*zpi)); |
592af4cc QY |
974 | new->interface_name_list = list_new(); |
975 | ||
976 | if (zpi->interface_name_list) { | |
977 | for (ALL_LIST_ELEMENTS_RO(zpi->interface_name_list, ln, ifname)) | |
978 | listnode_add(new->interface_name_list, | |
979 | XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifname)); | |
980 | } | |
7abd6c4f PG |
981 | |
982 | return new; | |
983 | } | |
984 | ||
62f20a52 | 985 | void zebra_pbr_add_iptable(struct zebra_pbr_iptable *iptable) |
7abd6c4f | 986 | { |
c9250e28 PG |
987 | struct zebra_pbr_iptable *ipt_hash; |
988 | ||
989 | ipt_hash = hash_get(zrouter.iptable_hash, iptable, | |
990 | pbr_iptable_alloc_intern); | |
991 | (void)dplane_pbr_iptable_add(ipt_hash); | |
7abd6c4f PG |
992 | } |
993 | ||
62f20a52 | 994 | void zebra_pbr_del_iptable(struct zebra_pbr_iptable *iptable) |
7abd6c4f | 995 | { |
f80ec7e3 | 996 | struct zebra_pbr_iptable *lookup; |
7abd6c4f | 997 | |
62f20a52 | 998 | lookup = hash_lookup(zrouter.iptable_hash, iptable); |
5162e000 | 999 | (void)dplane_pbr_iptable_delete(iptable); |
f80ec7e3 PG |
1000 | if (lookup) { |
1001 | struct listnode *node, *nnode; | |
1002 | char *name; | |
1003 | ||
62f20a52 | 1004 | hash_release(zrouter.iptable_hash, lookup); |
f80ec7e3 PG |
1005 | for (ALL_LIST_ELEMENTS(iptable->interface_name_list, |
1006 | node, nnode, name)) { | |
1007 | XFREE(MTYPE_PBR_IPTABLE_IFNAME, name); | |
1008 | list_delete_node(iptable->interface_name_list, | |
1009 | node); | |
1010 | } | |
8b5c4dce | 1011 | list_delete(&iptable->interface_name_list); |
7abd6c4f | 1012 | XFREE(MTYPE_TMP, lookup); |
f80ec7e3 | 1013 | } else |
9df414fe | 1014 | zlog_debug("%s: IPTable being deleted we know nothing about", |
5e81f5dd | 1015 | __func__); |
7abd6c4f PG |
1016 | } |
1017 | ||
942bf97b | 1018 | /* |
1019 | * Handle success or failure of rule (un)install in the kernel. | |
1020 | */ | |
f62e5480 JU |
1021 | void zebra_pbr_dplane_result(struct zebra_dplane_ctx *ctx) |
1022 | { | |
1023 | enum zebra_dplane_result res; | |
1024 | enum dplane_op_e op; | |
1025 | ||
1026 | res = dplane_ctx_get_status(ctx); | |
1027 | op = dplane_ctx_get_op(ctx); | |
1028 | if (op == DPLANE_OP_RULE_ADD || op == DPLANE_OP_RULE_UPDATE) | |
1029 | zsend_rule_notify_owner(ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1030 | ? ZAPI_RULE_INSTALLED | |
1031 | : ZAPI_RULE_FAIL_INSTALL); | |
1032 | else if (op == DPLANE_OP_RULE_DELETE) | |
1033 | zsend_rule_notify_owner(ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1034 | ? ZAPI_RULE_REMOVED | |
1035 | : ZAPI_RULE_FAIL_REMOVE); | |
5162e000 PG |
1036 | else if (op == DPLANE_OP_IPTABLE_ADD) |
1037 | zsend_iptable_notify_owner(ctx, | |
1038 | res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1039 | ? ZAPI_IPTABLE_INSTALLED | |
1040 | : ZAPI_IPTABLE_FAIL_INSTALL); | |
1041 | else if (op == DPLANE_OP_IPTABLE_DELETE) | |
1042 | zsend_iptable_notify_owner(ctx, | |
1043 | res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1044 | ? ZAPI_IPTABLE_REMOVED | |
1045 | : ZAPI_IPTABLE_FAIL_REMOVE); | |
ef524230 PG |
1046 | else if (op == DPLANE_OP_IPSET_ADD) |
1047 | zsend_ipset_notify_owner(ctx, | |
1048 | res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1049 | ? ZAPI_IPSET_INSTALLED | |
1050 | : ZAPI_IPSET_FAIL_INSTALL); | |
1051 | else if (op == DPLANE_OP_IPSET_DELETE) | |
1052 | zsend_ipset_notify_owner(ctx, | |
1053 | res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1054 | ? ZAPI_IPSET_REMOVED | |
1055 | : ZAPI_IPSET_FAIL_REMOVE); | |
1056 | else if (op == DPLANE_OP_IPSET_ENTRY_ADD) | |
1057 | zsend_ipset_entry_notify_owner( | |
1058 | ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1059 | ? ZAPI_IPSET_ENTRY_INSTALLED | |
1060 | : ZAPI_IPSET_ENTRY_FAIL_INSTALL); | |
1061 | else if (op == DPLANE_OP_IPSET_ENTRY_DELETE) | |
1062 | zsend_ipset_entry_notify_owner( | |
1063 | ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS | |
1064 | ? ZAPI_IPSET_ENTRY_REMOVED | |
1065 | : ZAPI_IPSET_ENTRY_FAIL_REMOVE); | |
f62e5480 JU |
1066 | else |
1067 | flog_err( | |
1068 | EC_ZEBRA_PBR_RULE_UPDATE, | |
1069 | "Context received in pbr rule dplane result handler with incorrect OP code (%u)", | |
1070 | op); | |
942bf97b | 1071 | } |
1072 | ||
1073 | /* | |
1074 | * Handle rule delete notification from kernel. | |
1075 | */ | |
a0321978 | 1076 | int kernel_pbr_rule_del(struct zebra_pbr_rule *rule) |
942bf97b | 1077 | { |
1078 | return 0; | |
1079 | } | |
586f4ccf PG |
1080 | |
1081 | struct zebra_pbr_ipset_entry_unique_display { | |
1082 | struct zebra_pbr_ipset *zpi; | |
1083 | struct vty *vty; | |
73a829f7 | 1084 | struct zebra_ns *zns; |
586f4ccf PG |
1085 | }; |
1086 | ||
586f4ccf PG |
1087 | |
1088 | static const char *zebra_pbr_prefix2str(union prefixconstptr pu, | |
1089 | char *str, int size) | |
1090 | { | |
1091 | const struct prefix *p = pu.p; | |
1092 | char buf[PREFIX2STR_BUFFER]; | |
1093 | ||
936fbaef | 1094 | if ((p->family == AF_INET && p->prefixlen == IPV4_MAX_BITLEN) |
f4d81e55 | 1095 | || (p->family == AF_INET6 && p->prefixlen == IPV6_MAX_BITLEN)) { |
586f4ccf PG |
1096 | snprintf(str, size, "%s", inet_ntop(p->family, &p->u.prefix, |
1097 | buf, PREFIX2STR_BUFFER)); | |
1098 | return str; | |
1099 | } | |
1100 | return prefix2str(pu, str, size); | |
1101 | } | |
1102 | ||
be729dd7 PG |
1103 | static void zebra_pbr_display_icmp(struct vty *vty, |
1104 | struct zebra_pbr_ipset_entry *zpie) | |
1105 | { | |
1106 | char decoded_str[20]; | |
1107 | uint16_t port; | |
c9b1139a PG |
1108 | struct zebra_pbr_ipset *zpi; |
1109 | ||
1110 | zpi = zpie->backpointer; | |
be729dd7 PG |
1111 | |
1112 | /* range icmp type */ | |
1113 | if (zpie->src_port_max || zpie->dst_port_max) { | |
2b7165e7 | 1114 | vty_out(vty, ":icmp:[type <%u:%u>;code <%u:%u>", |
be729dd7 PG |
1115 | zpie->src_port_min, zpie->src_port_max, |
1116 | zpie->dst_port_min, zpie->dst_port_max); | |
1117 | } else { | |
1118 | port = ((zpie->src_port_min << 8) & 0xff00) + | |
1119 | (zpie->dst_port_min & 0xff); | |
1120 | memset(decoded_str, 0, sizeof(decoded_str)); | |
2b7165e7 | 1121 | snprintf(decoded_str, sizeof(decoded_str), "%u/%u", |
772270f3 | 1122 | zpie->src_port_min, zpie->dst_port_min); |
c9b1139a PG |
1123 | vty_out(vty, ":%s:%s", |
1124 | zpi->family == AF_INET6 ? "ipv6-icmp" : "icmp", | |
1125 | lookup_msg(zpi->family == AF_INET6 ? | |
1126 | icmpv6_typecode_str : icmp_typecode_str, | |
be729dd7 PG |
1127 | port, decoded_str)); |
1128 | } | |
1129 | } | |
1130 | ||
25d760c5 PG |
1131 | static void zebra_pbr_display_port(struct vty *vty, uint32_t filter_bm, |
1132 | uint16_t port_min, uint16_t port_max, | |
1133 | uint8_t proto) | |
1134 | { | |
1135 | if (!(filter_bm & PBR_FILTER_PROTO)) { | |
1136 | if (port_max) | |
1137 | vty_out(vty, ":udp/tcp:%d-%d", | |
1138 | port_min, port_max); | |
1139 | else | |
1140 | vty_out(vty, ":udp/tcp:%d", | |
1141 | port_min); | |
1142 | } else { | |
1143 | if (port_max) | |
1144 | vty_out(vty, ":proto %d:%d-%d", | |
1145 | proto, port_min, port_max); | |
1146 | else | |
1147 | vty_out(vty, ":proto %d:%d", | |
1148 | proto, port_min); | |
1149 | } | |
1150 | } | |
1151 | ||
e3b78da8 | 1152 | static int zebra_pbr_show_ipset_entry_walkcb(struct hash_bucket *bucket, |
586f4ccf PG |
1153 | void *arg) |
1154 | { | |
1155 | struct zebra_pbr_ipset_entry_unique_display *unique = | |
1156 | (struct zebra_pbr_ipset_entry_unique_display *)arg; | |
1157 | struct zebra_pbr_ipset *zpi = unique->zpi; | |
1158 | struct vty *vty = unique->vty; | |
1159 | struct zebra_pbr_ipset_entry *zpie = | |
e3b78da8 | 1160 | (struct zebra_pbr_ipset_entry *)bucket->data; |
73a829f7 | 1161 | uint64_t pkts = 0, bytes = 0; |
73a829f7 | 1162 | int ret = 0; |
586f4ccf PG |
1163 | |
1164 | if (zpie->backpointer != zpi) | |
1165 | return HASHWALK_CONTINUE; | |
1166 | ||
25d760c5 PG |
1167 | if ((zpi->type == IPSET_NET_NET) || |
1168 | (zpi->type == IPSET_NET_PORT_NET)) { | |
586f4ccf PG |
1169 | char buf[PREFIX_STRLEN]; |
1170 | ||
1171 | zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf)); | |
1172 | vty_out(vty, "\tfrom %s", buf); | |
be729dd7 PG |
1173 | if (zpie->filter_bm & PBR_FILTER_SRC_PORT && |
1174 | zpie->proto != IPPROTO_ICMP) | |
25d760c5 PG |
1175 | zebra_pbr_display_port(vty, zpie->filter_bm, |
1176 | zpie->src_port_min, | |
1177 | zpie->src_port_max, | |
1178 | zpie->proto); | |
586f4ccf PG |
1179 | vty_out(vty, " to "); |
1180 | zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf)); | |
1181 | vty_out(vty, "%s", buf); | |
be729dd7 PG |
1182 | if (zpie->filter_bm & PBR_FILTER_DST_PORT && |
1183 | zpie->proto != IPPROTO_ICMP) | |
25d760c5 PG |
1184 | zebra_pbr_display_port(vty, zpie->filter_bm, |
1185 | zpie->dst_port_min, | |
1186 | zpie->dst_port_max, | |
1187 | zpie->proto); | |
be729dd7 PG |
1188 | if (zpie->proto == IPPROTO_ICMP) |
1189 | zebra_pbr_display_icmp(vty, zpie); | |
25d760c5 PG |
1190 | } else if ((zpi->type == IPSET_NET) || |
1191 | (zpi->type == IPSET_NET_PORT)) { | |
586f4ccf PG |
1192 | char buf[PREFIX_STRLEN]; |
1193 | ||
1194 | if (zpie->filter_bm & PBR_FILTER_SRC_IP) { | |
1195 | zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf)); | |
1196 | vty_out(vty, "\tfrom %s", buf); | |
1197 | } | |
be729dd7 PG |
1198 | if (zpie->filter_bm & PBR_FILTER_SRC_PORT && |
1199 | zpie->proto != IPPROTO_ICMP) | |
25d760c5 PG |
1200 | zebra_pbr_display_port(vty, zpie->filter_bm, |
1201 | zpie->src_port_min, | |
1202 | zpie->src_port_max, | |
1203 | zpie->proto); | |
586f4ccf PG |
1204 | if (zpie->filter_bm & PBR_FILTER_DST_IP) { |
1205 | zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf)); | |
1206 | vty_out(vty, "\tto %s", buf); | |
1207 | } | |
be729dd7 PG |
1208 | if (zpie->filter_bm & PBR_FILTER_DST_PORT && |
1209 | zpie->proto != IPPROTO_ICMP) | |
25d760c5 PG |
1210 | zebra_pbr_display_port(vty, zpie->filter_bm, |
1211 | zpie->dst_port_min, | |
1212 | zpie->dst_port_max, | |
1213 | zpie->proto); | |
be729dd7 PG |
1214 | if (zpie->proto == IPPROTO_ICMP) |
1215 | zebra_pbr_display_icmp(vty, zpie); | |
586f4ccf PG |
1216 | } |
1217 | vty_out(vty, " (%u)\n", zpie->unique); | |
1218 | ||
1c6fca1f | 1219 | ret = hook_call(zebra_pbr_ipset_entry_get_stat, zpie, &pkts, |
62f20a52 | 1220 | &bytes); |
73a829f7 PG |
1221 | if (ret && pkts > 0) |
1222 | vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n", | |
1223 | pkts, bytes); | |
586f4ccf PG |
1224 | return HASHWALK_CONTINUE; |
1225 | } | |
1226 | ||
e3b78da8 | 1227 | static int zebra_pbr_show_ipset_walkcb(struct hash_bucket *bucket, void *arg) |
586f4ccf PG |
1228 | { |
1229 | struct zebra_pbr_env_display *uniqueipset = | |
1230 | (struct zebra_pbr_env_display *)arg; | |
e3b78da8 | 1231 | struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data; |
586f4ccf PG |
1232 | struct zebra_pbr_ipset_entry_unique_display unique; |
1233 | struct vty *vty = uniqueipset->vty; | |
1234 | struct zebra_ns *zns = uniqueipset->zns; | |
1235 | ||
a60b7031 PG |
1236 | vty_out(vty, "IPset %s type %s family %s\n", zpi->ipset_name, |
1237 | zebra_pbr_ipset_type2str(zpi->type), | |
c6423c31 | 1238 | family2str(zpi->family)); |
586f4ccf PG |
1239 | unique.vty = vty; |
1240 | unique.zpi = zpi; | |
73a829f7 | 1241 | unique.zns = zns; |
62f20a52 | 1242 | hash_walk(zrouter.ipset_entry_hash, zebra_pbr_show_ipset_entry_walkcb, |
586f4ccf PG |
1243 | &unique); |
1244 | vty_out(vty, "\n"); | |
1245 | return HASHWALK_CONTINUE; | |
1246 | } | |
1247 | ||
dc993e76 PG |
1248 | size_t zebra_pbr_tcpflags_snprintf(char *buffer, size_t len, |
1249 | uint16_t tcp_val) | |
1250 | { | |
1251 | size_t len_written = 0; | |
1252 | static struct message nt = {0}; | |
1253 | const struct message *pnt; | |
1254 | int incr = 0; | |
1255 | ||
1256 | for (pnt = tcp_value_str; | |
1257 | memcmp(pnt, &nt, sizeof(struct message)); pnt++) | |
1258 | if (pnt->key & tcp_val) { | |
1259 | len_written += snprintf(buffer + len_written, | |
1260 | len - len_written, | |
1261 | "%s%s", incr ? | |
1262 | ",":"", pnt->str); | |
1263 | incr++; | |
1264 | } | |
1265 | return len_written; | |
1266 | } | |
1267 | ||
586f4ccf PG |
1268 | /* |
1269 | */ | |
1270 | void zebra_pbr_show_ipset_list(struct vty *vty, char *ipsetname) | |
1271 | { | |
1272 | struct zebra_pbr_ipset *zpi; | |
1273 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
1274 | struct zebra_pbr_ipset_entry_unique_display unique; | |
1275 | struct zebra_pbr_env_display uniqueipset; | |
1276 | ||
1277 | if (ipsetname) { | |
62f20a52 | 1278 | zpi = zebra_pbr_lookup_ipset_pername(ipsetname); |
586f4ccf PG |
1279 | if (!zpi) { |
1280 | vty_out(vty, "No IPset %s found\n", ipsetname); | |
1281 | return; | |
1282 | } | |
a60b7031 PG |
1283 | vty_out(vty, "IPset %s type %s family %s\n", ipsetname, |
1284 | zebra_pbr_ipset_type2str(zpi->type), | |
c6423c31 | 1285 | family2str(zpi->family)); |
586f4ccf PG |
1286 | unique.vty = vty; |
1287 | unique.zpi = zpi; | |
73a829f7 | 1288 | unique.zns = zns; |
62f20a52 DS |
1289 | hash_walk(zrouter.ipset_entry_hash, |
1290 | zebra_pbr_show_ipset_entry_walkcb, &unique); | |
586f4ccf PG |
1291 | return; |
1292 | } | |
1293 | uniqueipset.zns = zns; | |
1294 | uniqueipset.vty = vty; | |
7929821a | 1295 | uniqueipset.name = NULL; |
62f20a52 | 1296 | hash_walk(zrouter.ipset_hash, zebra_pbr_show_ipset_walkcb, |
586f4ccf PG |
1297 | &uniqueipset); |
1298 | } | |
1299 | ||
1300 | struct pbr_rule_fwmark_lookup { | |
1301 | struct zebra_pbr_rule *ptr; | |
1302 | uint32_t fwmark; | |
1303 | }; | |
1304 | ||
e3b78da8 | 1305 | static int zebra_pbr_rule_lookup_fwmark_walkcb(struct hash_bucket *bucket, |
586f4ccf PG |
1306 | void *arg) |
1307 | { | |
1308 | struct pbr_rule_fwmark_lookup *iprule = | |
1309 | (struct pbr_rule_fwmark_lookup *)arg; | |
e3b78da8 | 1310 | struct zebra_pbr_rule *zpr = (struct zebra_pbr_rule *)bucket->data; |
586f4ccf PG |
1311 | |
1312 | if (iprule->fwmark == zpr->rule.filter.fwmark) { | |
1313 | iprule->ptr = zpr; | |
1314 | return HASHWALK_ABORT; | |
1315 | } | |
1316 | return HASHWALK_CONTINUE; | |
1317 | } | |
1318 | ||
7929821a PG |
1319 | static void zebra_pbr_show_iptable_unit(struct zebra_pbr_iptable *iptable, |
1320 | struct vty *vty, | |
1321 | struct zebra_ns *zns) | |
586f4ccf | 1322 | { |
73a829f7 PG |
1323 | int ret; |
1324 | uint64_t pkts = 0, bytes = 0; | |
586f4ccf | 1325 | |
a60b7031 PG |
1326 | vty_out(vty, "IPtable %s family %s action %s (%u)\n", |
1327 | iptable->ipset_name, | |
c9b1139a | 1328 | family2str(iptable->family), |
586f4ccf PG |
1329 | iptable->action == ZEBRA_IPTABLES_DROP ? "drop" : "redirect", |
1330 | iptable->unique); | |
0b328d3f PG |
1331 | if (iptable->type == IPSET_NET_PORT || |
1332 | iptable->type == IPSET_NET_PORT_NET) { | |
1333 | if (!(iptable->filter_bm & MATCH_ICMP_SET)) { | |
1334 | if (iptable->filter_bm & PBR_FILTER_DST_PORT) | |
1335 | vty_out(vty, "\t lookup dst port\n"); | |
1336 | else if (iptable->filter_bm & PBR_FILTER_SRC_PORT) | |
1337 | vty_out(vty, "\t lookup src port\n"); | |
1338 | } | |
1339 | } | |
e7f7dad4 PG |
1340 | if (iptable->pkt_len_min || iptable->pkt_len_max) { |
1341 | if (!iptable->pkt_len_max) | |
1342 | vty_out(vty, "\t pkt len %u\n", | |
1343 | iptable->pkt_len_min); | |
1344 | else | |
1345 | vty_out(vty, "\t pkt len [%u;%u]\n", | |
1346 | iptable->pkt_len_min, | |
1347 | iptable->pkt_len_max); | |
1348 | } | |
dc993e76 PG |
1349 | if (iptable->tcp_flags || iptable->tcp_mask_flags) { |
1350 | char tcp_flag_str[64]; | |
1351 | char tcp_flag_mask_str[64]; | |
1352 | ||
1353 | zebra_pbr_tcpflags_snprintf(tcp_flag_str, | |
1354 | sizeof(tcp_flag_str), | |
1355 | iptable->tcp_flags); | |
1356 | zebra_pbr_tcpflags_snprintf(tcp_flag_mask_str, | |
1357 | sizeof(tcp_flag_mask_str), | |
1358 | iptable->tcp_mask_flags); | |
1359 | vty_out(vty, "\t tcpflags [%s/%s]\n", | |
1360 | tcp_flag_str, tcp_flag_mask_str); | |
1361 | } | |
69214c57 PG |
1362 | if (iptable->filter_bm & (MATCH_DSCP_SET | MATCH_DSCP_INVERSE_SET)) { |
1363 | vty_out(vty, "\t dscp %s %d\n", | |
1364 | iptable->filter_bm & MATCH_DSCP_INVERSE_SET ? | |
1365 | "not" : "", iptable->dscp_value); | |
1366 | } | |
a60b7031 PG |
1367 | if (iptable->filter_bm & (MATCH_FLOW_LABEL_SET | |
1368 | MATCH_FLOW_LABEL_INVERSE_SET)) { | |
1369 | vty_out(vty, "\t flowlabel %s %d\n", | |
1370 | iptable->filter_bm & MATCH_FLOW_LABEL_INVERSE_SET ? | |
1371 | "not" : "", iptable->flow_label); | |
1372 | } | |
5ac5b7cc PG |
1373 | if (iptable->fragment) { |
1374 | char val_str[10]; | |
1375 | ||
772270f3 | 1376 | snprintf(val_str, sizeof(val_str), "%d", iptable->fragment); |
5ac5b7cc PG |
1377 | vty_out(vty, "\t fragment%s %s\n", |
1378 | iptable->filter_bm & MATCH_FRAGMENT_INVERSE_SET ? | |
1379 | " not" : "", lookup_msg(fragment_value_str, | |
1380 | iptable->fragment, val_str)); | |
1381 | } | |
f449d223 PG |
1382 | if (iptable->protocol) { |
1383 | vty_out(vty, "\t protocol %d\n", | |
1384 | iptable->protocol); | |
1385 | } | |
1c6fca1f | 1386 | ret = hook_call(zebra_pbr_iptable_get_stat, iptable, &pkts, |
62f20a52 | 1387 | &bytes); |
73a829f7 PG |
1388 | if (ret && pkts > 0) |
1389 | vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n", | |
1390 | pkts, bytes); | |
586f4ccf PG |
1391 | if (iptable->action != ZEBRA_IPTABLES_DROP) { |
1392 | struct pbr_rule_fwmark_lookup prfl; | |
1393 | ||
1394 | prfl.fwmark = iptable->fwmark; | |
1395 | prfl.ptr = NULL; | |
7f0ea8a4 | 1396 | hash_walk(zrouter.rules_hash, |
586f4ccf PG |
1397 | &zebra_pbr_rule_lookup_fwmark_walkcb, &prfl); |
1398 | if (prfl.ptr) { | |
1399 | struct zebra_pbr_rule *zpr = prfl.ptr; | |
1400 | ||
1401 | vty_out(vty, "\t table %u, fwmark %u\n", | |
1402 | zpr->rule.action.table, | |
1403 | prfl.fwmark); | |
1404 | } | |
1405 | } | |
7929821a PG |
1406 | } |
1407 | ||
e3b78da8 | 1408 | static int zebra_pbr_show_iptable_walkcb(struct hash_bucket *bucket, void *arg) |
7929821a PG |
1409 | { |
1410 | struct zebra_pbr_iptable *iptable = | |
e3b78da8 | 1411 | (struct zebra_pbr_iptable *)bucket->data; |
7929821a PG |
1412 | struct zebra_pbr_env_display *env = (struct zebra_pbr_env_display *)arg; |
1413 | struct vty *vty = env->vty; | |
1414 | struct zebra_ns *zns = env->zns; | |
1415 | char *iptable_name = env->name; | |
1416 | ||
1417 | if (!iptable_name) | |
1418 | zebra_pbr_show_iptable_unit(iptable, vty, zns); | |
1419 | else if (!strncmp(iptable_name, | |
1420 | iptable->ipset_name, | |
1421 | ZEBRA_IPSET_NAME_SIZE)) | |
1422 | zebra_pbr_show_iptable_unit(iptable, vty, zns); | |
586f4ccf PG |
1423 | return HASHWALK_CONTINUE; |
1424 | } | |
1425 | ||
7929821a | 1426 | void zebra_pbr_show_iptable(struct vty *vty, char *iptable_name) |
586f4ccf PG |
1427 | { |
1428 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
1429 | struct zebra_pbr_env_display env; | |
1430 | ||
1431 | env.vty = vty; | |
1432 | env.zns = zns; | |
7929821a | 1433 | env.name = iptable_name; |
62f20a52 | 1434 | hash_walk(zrouter.iptable_hash, zebra_pbr_show_iptable_walkcb, &env); |
586f4ccf | 1435 | } |
f80ec7e3 PG |
1436 | |
1437 | void zebra_pbr_iptable_update_interfacelist(struct stream *s, | |
1438 | struct zebra_pbr_iptable *zpi) | |
1439 | { | |
1440 | uint32_t i = 0, index; | |
1441 | struct interface *ifp; | |
1442 | char *name; | |
1443 | ||
1444 | for (i = 0; i < zpi->nb_interface; i++) { | |
1445 | STREAM_GETL(s, index); | |
1446 | ifp = if_lookup_by_index(index, zpi->vrf_id); | |
1447 | if (!ifp) | |
1448 | continue; | |
1449 | name = XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifp->name); | |
1450 | listnode_add(zpi->interface_name_list, name); | |
1451 | } | |
1452 | stream_failure: | |
1453 | return; | |
1454 | } |