]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pbr.c
Merge pull request #7942 from donaldsharp/rpki_reload_fix
[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 #include <memory.h>
27 #include <hook.h>
28
29 #include "zebra/zebra_router.h"
30 #include "zebra/zebra_pbr.h"
31 #include "zebra/rt.h"
32 #include "zebra/zapi_msg.h"
33 #include "zebra/zebra_memory.h"
34 #include "zebra/zserv.h"
35 #include "zebra/debug.h"
36
37 /* definitions */
38 DEFINE_MTYPE_STATIC(ZEBRA, PBR_IPTABLE_IFNAME, "PBR interface list")
39
40 /* definitions */
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 };
48
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
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
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
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
127 /* static function declarations */
128 DEFINE_HOOK(zebra_pbr_ipset_entry_get_stat,
129 (struct zebra_pbr_ipset_entry *ipset, uint64_t *pkts,
130 uint64_t *bytes),
131 (ipset, pkts, bytes))
132
133 DEFINE_HOOK(zebra_pbr_iptable_get_stat,
134 (struct zebra_pbr_iptable *iptable, uint64_t *pkts,
135 uint64_t *bytes),
136 (iptable, pkts, bytes))
137
138 DEFINE_HOOK(zebra_pbr_iptable_update,
139 (int cmd, struct zebra_pbr_iptable *iptable), (cmd, iptable));
140
141 DEFINE_HOOK(zebra_pbr_ipset_entry_update,
142 (int cmd, struct zebra_pbr_ipset_entry *ipset), (cmd, ipset));
143
144 DEFINE_HOOK(zebra_pbr_ipset_update,
145 (int cmd, struct zebra_pbr_ipset *ipset), (cmd, ipset));
146
147 /* Private functions */
148
149 /* Public functions */
150 void zebra_pbr_rules_free(void *arg)
151 {
152 struct zebra_pbr_rule *rule;
153
154 rule = (struct zebra_pbr_rule *)arg;
155
156 (void)dplane_pbr_rule_delete(rule);
157 XFREE(MTYPE_TMP, rule);
158 }
159
160 uint32_t zebra_pbr_rules_hash_key(const void *arg)
161 {
162 const struct zebra_pbr_rule *rule;
163 uint32_t key;
164
165 rule = arg;
166 key = jhash_3words(rule->rule.seq, rule->rule.priority,
167 rule->rule.action.table,
168 prefix_hash_key(&rule->rule.filter.src_ip));
169
170 if (rule->rule.filter.fwmark)
171 key = jhash_2words(rule->rule.filter.fwmark, rule->vrf_id, key);
172 else
173 key = jhash_1word(rule->vrf_id, key);
174
175 key = jhash(rule->ifname, strlen(rule->ifname), key);
176
177 return jhash_3words(rule->rule.filter.src_port,
178 rule->rule.filter.dst_port,
179 prefix_hash_key(&rule->rule.filter.dst_ip),
180 jhash_1word(rule->rule.unique, key));
181 }
182
183 bool zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2)
184 {
185 const struct zebra_pbr_rule *r1, *r2;
186
187 r1 = (const struct zebra_pbr_rule *)arg1;
188 r2 = (const struct zebra_pbr_rule *)arg2;
189
190 if (r1->rule.seq != r2->rule.seq)
191 return false;
192
193 if (r1->rule.priority != r2->rule.priority)
194 return false;
195
196 if (r1->rule.unique != r2->rule.unique)
197 return false;
198
199 if (r1->rule.action.table != r2->rule.action.table)
200 return false;
201
202 if (r1->rule.filter.src_port != r2->rule.filter.src_port)
203 return false;
204
205 if (r1->rule.filter.dst_port != r2->rule.filter.dst_port)
206 return false;
207
208 if (r1->rule.filter.fwmark != r2->rule.filter.fwmark)
209 return false;
210
211 if (!prefix_same(&r1->rule.filter.src_ip, &r2->rule.filter.src_ip))
212 return false;
213
214 if (!prefix_same(&r1->rule.filter.dst_ip, &r2->rule.filter.dst_ip))
215 return false;
216
217 if (strcmp(r1->rule.ifname, r2->rule.ifname) != 0)
218 return false;
219
220 if (r1->vrf_id != r2->vrf_id)
221 return false;
222
223 return true;
224 }
225
226 struct pbr_rule_unique_lookup {
227 struct zebra_pbr_rule *rule;
228 uint32_t unique;
229 char ifname[INTERFACE_NAMSIZ + 1];
230 vrf_id_t vrf_id;
231 };
232
233 static int pbr_rule_lookup_unique_walker(struct hash_bucket *b, void *data)
234 {
235 struct pbr_rule_unique_lookup *pul = data;
236 struct zebra_pbr_rule *rule = b->data;
237
238 if (pul->unique == rule->rule.unique
239 && strncmp(pul->ifname, rule->rule.ifname, INTERFACE_NAMSIZ) == 0
240 && pul->vrf_id == rule->vrf_id) {
241 pul->rule = rule;
242 return HASHWALK_ABORT;
243 }
244
245 return HASHWALK_CONTINUE;
246 }
247
248 static struct zebra_pbr_rule *
249 pbr_rule_lookup_unique(struct zebra_pbr_rule *zrule)
250 {
251 struct pbr_rule_unique_lookup pul;
252
253 pul.unique = zrule->rule.unique;
254 strlcpy(pul.ifname, zrule->rule.ifname, INTERFACE_NAMSIZ);
255 pul.rule = NULL;
256 pul.vrf_id = zrule->vrf_id;
257 hash_walk(zrouter.rules_hash, &pbr_rule_lookup_unique_walker, &pul);
258
259 return pul.rule;
260 }
261
262 void zebra_pbr_ipset_free(void *arg)
263 {
264 struct zebra_pbr_ipset *ipset;
265
266 ipset = (struct zebra_pbr_ipset *)arg;
267 hook_call(zebra_pbr_ipset_update, 0, ipset);
268 XFREE(MTYPE_TMP, ipset);
269 }
270
271 uint32_t zebra_pbr_ipset_hash_key(const void *arg)
272 {
273 const struct zebra_pbr_ipset *ipset = arg;
274 uint32_t *pnt = (uint32_t *)&ipset->ipset_name;
275 uint32_t key = jhash_1word(ipset->vrf_id, 0x63ab42de);
276
277 key = jhash_1word(ipset->family, key);
278
279 return jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE, key);
280 }
281
282 bool zebra_pbr_ipset_hash_equal(const void *arg1, const void *arg2)
283 {
284 const struct zebra_pbr_ipset *r1, *r2;
285
286 r1 = (const struct zebra_pbr_ipset *)arg1;
287 r2 = (const struct zebra_pbr_ipset *)arg2;
288
289 if (r1->type != r2->type)
290 return false;
291 if (r1->unique != r2->unique)
292 return false;
293 if (r1->vrf_id != r2->vrf_id)
294 return false;
295 if (r1->family != r2->family)
296 return false;
297
298 if (strncmp(r1->ipset_name, r2->ipset_name,
299 ZEBRA_IPSET_NAME_SIZE))
300 return false;
301 return true;
302 }
303
304 void zebra_pbr_ipset_entry_free(void *arg)
305 {
306 struct zebra_pbr_ipset_entry *ipset;
307
308 ipset = (struct zebra_pbr_ipset_entry *)arg;
309
310 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
311
312 XFREE(MTYPE_TMP, ipset);
313 }
314
315 uint32_t zebra_pbr_ipset_entry_hash_key(const void *arg)
316 {
317 const struct zebra_pbr_ipset_entry *ipset;
318 uint32_t key;
319
320 ipset = arg;
321 key = prefix_hash_key(&ipset->src);
322 key = jhash_1word(ipset->unique, key);
323 key = jhash_1word(prefix_hash_key(&ipset->dst), key);
324 key = jhash(&ipset->dst_port_min, 2, key);
325 key = jhash(&ipset->dst_port_max, 2, key);
326 key = jhash(&ipset->src_port_min, 2, key);
327 key = jhash(&ipset->src_port_max, 2, key);
328 key = jhash(&ipset->proto, 1, key);
329
330 return key;
331 }
332
333 bool zebra_pbr_ipset_entry_hash_equal(const void *arg1, const void *arg2)
334 {
335 const struct zebra_pbr_ipset_entry *r1, *r2;
336
337 r1 = (const struct zebra_pbr_ipset_entry *)arg1;
338 r2 = (const struct zebra_pbr_ipset_entry *)arg2;
339
340 if (r1->unique != r2->unique)
341 return false;
342
343 if (!prefix_same(&r1->src, &r2->src))
344 return false;
345
346 if (!prefix_same(&r1->dst, &r2->dst))
347 return false;
348
349 if (r1->src_port_min != r2->src_port_min)
350 return false;
351
352 if (r1->src_port_max != r2->src_port_max)
353 return false;
354
355 if (r1->dst_port_min != r2->dst_port_min)
356 return false;
357
358 if (r1->dst_port_max != r2->dst_port_max)
359 return false;
360
361 if (r1->proto != r2->proto)
362 return false;
363 return true;
364 }
365
366 /* this function gives option to flush plugin memory contexts
367 * with all parameter. set it to true to flush all
368 * set it to false to flush only passed arg argument
369 */
370 static void _zebra_pbr_iptable_free_all(void *arg, bool all)
371 {
372 struct zebra_pbr_iptable *iptable;
373 struct listnode *node, *nnode;
374 char *name;
375
376 iptable = (struct zebra_pbr_iptable *)arg;
377
378 if (all)
379 hook_call(zebra_pbr_iptable_update, 0, iptable);
380
381 if (iptable->interface_name_list) {
382 for (ALL_LIST_ELEMENTS(iptable->interface_name_list, node,
383 nnode, name)) {
384 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
385 list_delete_node(iptable->interface_name_list, node);
386 }
387 list_delete(&iptable->interface_name_list);
388 }
389 XFREE(MTYPE_TMP, iptable);
390 }
391
392 void zebra_pbr_iptable_free(void *arg)
393 {
394 _zebra_pbr_iptable_free_all(arg, false);
395 }
396
397 uint32_t zebra_pbr_iptable_hash_key(const void *arg)
398 {
399 const struct zebra_pbr_iptable *iptable = arg;
400 uint32_t *pnt = (uint32_t *)&(iptable->ipset_name);
401 uint32_t key;
402
403 key = jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE,
404 0x63ab42de);
405 key = jhash_1word(iptable->fwmark, key);
406 key = jhash_1word(iptable->family, key);
407 key = jhash_1word(iptable->flow_label, key);
408 key = jhash_1word(iptable->pkt_len_min, key);
409 key = jhash_1word(iptable->pkt_len_max, key);
410 key = jhash_1word(iptable->tcp_flags, key);
411 key = jhash_1word(iptable->tcp_mask_flags, key);
412 key = jhash_1word(iptable->dscp_value, key);
413 key = jhash_1word(iptable->protocol, key);
414 key = jhash_1word(iptable->fragment, key);
415 key = jhash_1word(iptable->vrf_id, key);
416
417 return jhash_3words(iptable->filter_bm, iptable->type,
418 iptable->unique, key);
419 }
420
421 bool zebra_pbr_iptable_hash_equal(const void *arg1, const void *arg2)
422 {
423 const struct zebra_pbr_iptable *r1, *r2;
424
425 r1 = (const struct zebra_pbr_iptable *)arg1;
426 r2 = (const struct zebra_pbr_iptable *)arg2;
427
428 if (r1->vrf_id != r2->vrf_id)
429 return false;
430 if (r1->type != r2->type)
431 return false;
432 if (r1->unique != r2->unique)
433 return false;
434 if (r1->filter_bm != r2->filter_bm)
435 return false;
436 if (r1->fwmark != r2->fwmark)
437 return false;
438 if (r1->action != r2->action)
439 return false;
440 if (strncmp(r1->ipset_name, r2->ipset_name,
441 ZEBRA_IPSET_NAME_SIZE))
442 return false;
443 if (r1->family != r2->family)
444 return false;
445 if (r1->flow_label != r2->flow_label)
446 return false;
447 if (r1->pkt_len_min != r2->pkt_len_min)
448 return false;
449 if (r1->pkt_len_max != r2->pkt_len_max)
450 return false;
451 if (r1->tcp_flags != r2->tcp_flags)
452 return false;
453 if (r1->tcp_mask_flags != r2->tcp_mask_flags)
454 return false;
455 if (r1->dscp_value != r2->dscp_value)
456 return false;
457 if (r1->fragment != r2->fragment)
458 return false;
459 if (r1->protocol != r2->protocol)
460 return false;
461 return true;
462 }
463
464 static void *pbr_rule_alloc_intern(void *arg)
465 {
466 struct zebra_pbr_rule *zpr;
467 struct zebra_pbr_rule *new;
468
469 zpr = (struct zebra_pbr_rule *)arg;
470
471 new = XCALLOC(MTYPE_TMP, sizeof(*new));
472
473 memcpy(new, zpr, sizeof(*zpr));
474
475 return new;
476 }
477
478 static int pbr_rule_release(struct zebra_pbr_rule *rule)
479 {
480 struct zebra_pbr_rule *lookup;
481
482 lookup = hash_lookup(zrouter.rules_hash, rule);
483
484 if (!lookup)
485 return -ENOENT;
486
487 hash_release(zrouter.rules_hash, lookup);
488 XFREE(MTYPE_TMP, lookup);
489
490 return 0;
491 }
492
493 void zebra_pbr_add_rule(struct zebra_pbr_rule *rule)
494 {
495 struct zebra_pbr_rule *found;
496
497 /**
498 * Check if we already have it (this checks via a unique ID, walking
499 * over the hash table, not via a hash operation).
500 */
501 found = pbr_rule_lookup_unique(rule);
502
503 /* If found, this is an update */
504 if (found) {
505 if (IS_ZEBRA_DEBUG_PBR)
506 zlog_debug(
507 "%s: seq: %d, prior: %d, unique: %d, ifname: %s -- update",
508 __func__, rule->rule.seq, rule->rule.priority,
509 rule->rule.unique, rule->rule.ifname);
510
511 (void)dplane_pbr_rule_update(found, rule);
512
513 if (pbr_rule_release(found))
514 zlog_debug(
515 "%s: Rule being updated we know nothing about",
516 __PRETTY_FUNCTION__);
517
518 } else {
519 if (IS_ZEBRA_DEBUG_PBR)
520 zlog_debug(
521 "%s: seq: %d, prior: %d, unique: %d, ifname: %s -- new",
522 __func__, rule->rule.seq, rule->rule.priority,
523 rule->rule.unique, rule->rule.ifname);
524
525 (void)dplane_pbr_rule_add(rule);
526 }
527
528 (void)hash_get(zrouter.rules_hash, rule, pbr_rule_alloc_intern);
529 }
530
531 void zebra_pbr_del_rule(struct zebra_pbr_rule *rule)
532 {
533 if (IS_ZEBRA_DEBUG_PBR)
534 zlog_debug("%s: seq: %d, prior: %d, unique: %d, ifname: %s",
535 __func__, rule->rule.seq, rule->rule.priority,
536 rule->rule.unique, rule->rule.ifname);
537
538 (void)dplane_pbr_rule_delete(rule);
539
540 if (pbr_rule_release(rule))
541 zlog_debug("%s: Rule being deleted we know nothing about",
542 __func__);
543 }
544
545 static void zebra_pbr_cleanup_rules(struct hash_bucket *b, void *data)
546 {
547 struct zebra_pbr_rule *rule = b->data;
548 int *sock = data;
549
550 if (rule->sock == *sock) {
551 (void)dplane_pbr_rule_delete(rule);
552 if (hash_release(zrouter.rules_hash, rule))
553 XFREE(MTYPE_TMP, rule);
554 else
555 zlog_debug(
556 "%s: Rule seq: %u is being cleaned but we can't find it in our tables",
557 __func__, rule->rule.seq);
558 }
559 }
560
561 static void zebra_pbr_cleanup_ipset(struct hash_bucket *b, void *data)
562 {
563 struct zebra_pbr_ipset *ipset = b->data;
564 int *sock = data;
565
566 if (ipset->sock == *sock) {
567 if (hash_release(zrouter.ipset_hash, ipset))
568 zebra_pbr_ipset_free(ipset);
569 else
570 hook_call(zebra_pbr_ipset_update, 0, ipset);
571 }
572 }
573
574 static void zebra_pbr_cleanup_ipset_entry(struct hash_bucket *b, void *data)
575 {
576 struct zebra_pbr_ipset_entry *ipset = b->data;
577 int *sock = data;
578
579 if (ipset->sock == *sock) {
580 if (hash_release(zrouter.ipset_entry_hash, ipset))
581 zebra_pbr_ipset_entry_free(ipset);
582 else
583 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
584 }
585 }
586
587 static void zebra_pbr_cleanup_iptable(struct hash_bucket *b, void *data)
588 {
589 struct zebra_pbr_iptable *iptable = b->data;
590 int *sock = data;
591
592 if (iptable->sock == *sock) {
593 if (hash_release(zrouter.iptable_hash, iptable))
594 _zebra_pbr_iptable_free_all(iptable, true);
595 else
596 hook_call(zebra_pbr_iptable_update, 0, iptable);
597 }
598 }
599
600 static int zebra_pbr_client_close_cleanup(struct zserv *client)
601 {
602 int sock = client->sock;
603
604 if (!sock)
605 return 0;
606 hash_iterate(zrouter.rules_hash, zebra_pbr_cleanup_rules, &sock);
607 hash_iterate(zrouter.iptable_hash, zebra_pbr_cleanup_iptable, &sock);
608 hash_iterate(zrouter.ipset_entry_hash, zebra_pbr_cleanup_ipset_entry,
609 &sock);
610 hash_iterate(zrouter.ipset_hash, zebra_pbr_cleanup_ipset, &sock);
611 return 1;
612 }
613
614 void zebra_pbr_init(void)
615 {
616 hook_register(zserv_client_close, zebra_pbr_client_close_cleanup);
617 }
618
619 static void *pbr_ipset_alloc_intern(void *arg)
620 {
621 struct zebra_pbr_ipset *zpi;
622 struct zebra_pbr_ipset *new;
623
624 zpi = (struct zebra_pbr_ipset *)arg;
625
626 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset));
627
628 memcpy(new, zpi, sizeof(*zpi));
629
630 return new;
631 }
632
633 void zebra_pbr_create_ipset(struct zebra_pbr_ipset *ipset)
634 {
635 int ret;
636
637 (void)hash_get(zrouter.ipset_hash, ipset, pbr_ipset_alloc_intern);
638 ret = hook_call(zebra_pbr_ipset_update, 1, ipset);
639 kernel_pbr_ipset_add_del_status(ipset,
640 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
641 : ZEBRA_DPLANE_INSTALL_FAILURE);
642 }
643
644 void zebra_pbr_destroy_ipset(struct zebra_pbr_ipset *ipset)
645 {
646 struct zebra_pbr_ipset *lookup;
647
648 lookup = hash_lookup(zrouter.ipset_hash, ipset);
649 hook_call(zebra_pbr_ipset_update, 0, ipset);
650 if (lookup) {
651 hash_release(zrouter.ipset_hash, lookup);
652 XFREE(MTYPE_TMP, lookup);
653 } else
654 zlog_debug(
655 "%s: IPSet Entry being deleted we know nothing about",
656 __func__);
657 }
658
659 struct pbr_ipset_name_lookup {
660 struct zebra_pbr_ipset *ipset;
661 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
662 };
663
664 const char *zebra_pbr_ipset_type2str(uint32_t type)
665 {
666 return lookup_msg(ipset_type_msg, type,
667 "Unrecognized IPset Type");
668 }
669
670 static int zebra_pbr_ipset_pername_walkcb(struct hash_bucket *bucket, void *arg)
671 {
672 struct pbr_ipset_name_lookup *pinl =
673 (struct pbr_ipset_name_lookup *)arg;
674 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
675
676 if (!strncmp(pinl->ipset_name, zpi->ipset_name,
677 ZEBRA_IPSET_NAME_SIZE)) {
678 pinl->ipset = zpi;
679 return HASHWALK_ABORT;
680 }
681 return HASHWALK_CONTINUE;
682 }
683
684 struct zebra_pbr_ipset *zebra_pbr_lookup_ipset_pername(char *ipsetname)
685 {
686 struct pbr_ipset_name_lookup pinl;
687 struct pbr_ipset_name_lookup *ptr = &pinl;
688
689 if (!ipsetname)
690 return NULL;
691 memset(ptr, 0, sizeof(struct pbr_ipset_name_lookup));
692 snprintf((char *)ptr->ipset_name, ZEBRA_IPSET_NAME_SIZE, "%s",
693 ipsetname);
694 hash_walk(zrouter.ipset_hash, zebra_pbr_ipset_pername_walkcb, ptr);
695 return ptr->ipset;
696 }
697
698 static void *pbr_ipset_entry_alloc_intern(void *arg)
699 {
700 struct zebra_pbr_ipset_entry *zpi;
701 struct zebra_pbr_ipset_entry *new;
702
703 zpi = (struct zebra_pbr_ipset_entry *)arg;
704
705 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset_entry));
706
707 memcpy(new, zpi, sizeof(*zpi));
708
709 return new;
710 }
711
712 void zebra_pbr_add_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
713 {
714 int ret;
715
716 (void)hash_get(zrouter.ipset_entry_hash, ipset,
717 pbr_ipset_entry_alloc_intern);
718 ret = hook_call(zebra_pbr_ipset_entry_update, 1, ipset);
719 kernel_pbr_ipset_entry_add_del_status(ipset,
720 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
721 : ZEBRA_DPLANE_INSTALL_FAILURE);
722 }
723
724 void zebra_pbr_del_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
725 {
726 struct zebra_pbr_ipset_entry *lookup;
727
728 lookup = hash_lookup(zrouter.ipset_entry_hash, ipset);
729 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
730 if (lookup) {
731 hash_release(zrouter.ipset_entry_hash, lookup);
732 XFREE(MTYPE_TMP, lookup);
733 } else
734 zlog_debug("%s: IPSet being deleted we know nothing about",
735 __func__);
736 }
737
738 static void *pbr_iptable_alloc_intern(void *arg)
739 {
740 struct zebra_pbr_iptable *zpi;
741 struct zebra_pbr_iptable *new;
742 struct listnode *ln;
743 char *ifname;
744
745 zpi = (struct zebra_pbr_iptable *)arg;
746
747 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable));
748
749 /* Deep structure copy */
750 memcpy(new, zpi, sizeof(*zpi));
751 new->interface_name_list = list_new();
752
753 if (zpi->interface_name_list) {
754 for (ALL_LIST_ELEMENTS_RO(zpi->interface_name_list, ln, ifname))
755 listnode_add(new->interface_name_list,
756 XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifname));
757 }
758
759 return new;
760 }
761
762 void zebra_pbr_add_iptable(struct zebra_pbr_iptable *iptable)
763 {
764 int ret;
765
766 (void)hash_get(zrouter.iptable_hash, iptable, pbr_iptable_alloc_intern);
767 ret = hook_call(zebra_pbr_iptable_update, 1, iptable);
768 kernel_pbr_iptable_add_del_status(iptable,
769 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
770 : ZEBRA_DPLANE_INSTALL_FAILURE);
771 }
772
773 void zebra_pbr_del_iptable(struct zebra_pbr_iptable *iptable)
774 {
775 struct zebra_pbr_iptable *lookup;
776
777 lookup = hash_lookup(zrouter.iptable_hash, iptable);
778 hook_call(zebra_pbr_iptable_update, 0, iptable);
779 if (lookup) {
780 struct listnode *node, *nnode;
781 char *name;
782
783 hash_release(zrouter.iptable_hash, lookup);
784 for (ALL_LIST_ELEMENTS(iptable->interface_name_list,
785 node, nnode, name)) {
786 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
787 list_delete_node(iptable->interface_name_list,
788 node);
789 }
790 list_delete(&iptable->interface_name_list);
791 XFREE(MTYPE_TMP, lookup);
792 } else
793 zlog_debug("%s: IPTable being deleted we know nothing about",
794 __func__);
795 }
796
797 /*
798 * Handle success or failure of rule (un)install in the kernel.
799 */
800 void zebra_pbr_dplane_result(struct zebra_dplane_ctx *ctx)
801 {
802 enum zebra_dplane_result res;
803 enum dplane_op_e op;
804
805 res = dplane_ctx_get_status(ctx);
806 op = dplane_ctx_get_op(ctx);
807 if (op == DPLANE_OP_RULE_ADD || op == DPLANE_OP_RULE_UPDATE)
808 zsend_rule_notify_owner(ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS
809 ? ZAPI_RULE_INSTALLED
810 : ZAPI_RULE_FAIL_INSTALL);
811 else if (op == DPLANE_OP_RULE_DELETE)
812 zsend_rule_notify_owner(ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS
813 ? ZAPI_RULE_REMOVED
814 : ZAPI_RULE_FAIL_REMOVE);
815 else
816 flog_err(
817 EC_ZEBRA_PBR_RULE_UPDATE,
818 "Context received in pbr rule dplane result handler with incorrect OP code (%u)",
819 op);
820
821
822 dplane_ctx_fini(&ctx);
823 }
824
825 /*
826 * Handle success or failure of ipset (un)install in the kernel.
827 */
828 void kernel_pbr_ipset_add_del_status(struct zebra_pbr_ipset *ipset,
829 enum zebra_dplane_status res)
830 {
831 switch (res) {
832 case ZEBRA_DPLANE_INSTALL_SUCCESS:
833 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_INSTALLED);
834 break;
835 case ZEBRA_DPLANE_INSTALL_FAILURE:
836 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_FAIL_INSTALL);
837 break;
838 case ZEBRA_DPLANE_DELETE_SUCCESS:
839 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_REMOVED);
840 break;
841 case ZEBRA_DPLANE_DELETE_FAILURE:
842 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_FAIL_REMOVE);
843 break;
844 case ZEBRA_DPLANE_STATUS_NONE:
845 break;
846 }
847 }
848
849 /*
850 * Handle success or failure of ipset (un)install in the kernel.
851 */
852 void kernel_pbr_ipset_entry_add_del_status(
853 struct zebra_pbr_ipset_entry *ipset,
854 enum zebra_dplane_status res)
855 {
856 switch (res) {
857 case ZEBRA_DPLANE_INSTALL_SUCCESS:
858 zsend_ipset_entry_notify_owner(ipset,
859 ZAPI_IPSET_ENTRY_INSTALLED);
860 break;
861 case ZEBRA_DPLANE_INSTALL_FAILURE:
862 zsend_ipset_entry_notify_owner(ipset,
863 ZAPI_IPSET_ENTRY_FAIL_INSTALL);
864 break;
865 case ZEBRA_DPLANE_DELETE_SUCCESS:
866 zsend_ipset_entry_notify_owner(ipset,
867 ZAPI_IPSET_ENTRY_REMOVED);
868 break;
869 case ZEBRA_DPLANE_DELETE_FAILURE:
870 zsend_ipset_entry_notify_owner(ipset,
871 ZAPI_IPSET_ENTRY_FAIL_REMOVE);
872 break;
873 case ZEBRA_DPLANE_STATUS_NONE:
874 break;
875 }
876 }
877
878 /*
879 * Handle success or failure of ipset (un)install in the kernel.
880 */
881 void kernel_pbr_iptable_add_del_status(struct zebra_pbr_iptable *iptable,
882 enum zebra_dplane_status res)
883 {
884 switch (res) {
885 case ZEBRA_DPLANE_INSTALL_SUCCESS:
886 zsend_iptable_notify_owner(iptable, ZAPI_IPTABLE_INSTALLED);
887 break;
888 case ZEBRA_DPLANE_INSTALL_FAILURE:
889 zsend_iptable_notify_owner(iptable, ZAPI_IPTABLE_FAIL_INSTALL);
890 break;
891 case ZEBRA_DPLANE_DELETE_SUCCESS:
892 zsend_iptable_notify_owner(iptable,
893 ZAPI_IPTABLE_REMOVED);
894 break;
895 case ZEBRA_DPLANE_DELETE_FAILURE:
896 zsend_iptable_notify_owner(iptable,
897 ZAPI_IPTABLE_FAIL_REMOVE);
898 break;
899 case ZEBRA_DPLANE_STATUS_NONE:
900 break;
901 }
902 }
903
904 /*
905 * Handle rule delete notification from kernel.
906 */
907 int kernel_pbr_rule_del(struct zebra_pbr_rule *rule)
908 {
909 return 0;
910 }
911
912 struct zebra_pbr_ipset_entry_unique_display {
913 struct zebra_pbr_ipset *zpi;
914 struct vty *vty;
915 struct zebra_ns *zns;
916 };
917
918 struct zebra_pbr_env_display {
919 struct zebra_ns *zns;
920 struct vty *vty;
921 char *name;
922 };
923
924 static const char *zebra_pbr_prefix2str(union prefixconstptr pu,
925 char *str, int size)
926 {
927 const struct prefix *p = pu.p;
928 char buf[PREFIX2STR_BUFFER];
929
930 if ((p->family == AF_INET && p->prefixlen == IPV4_MAX_PREFIXLEN) ||
931 (p->family == AF_INET6 && p->prefixlen == IPV6_MAX_PREFIXLEN)) {
932 snprintf(str, size, "%s", inet_ntop(p->family, &p->u.prefix,
933 buf, PREFIX2STR_BUFFER));
934 return str;
935 }
936 return prefix2str(pu, str, size);
937 }
938
939 static void zebra_pbr_display_icmp(struct vty *vty,
940 struct zebra_pbr_ipset_entry *zpie)
941 {
942 char decoded_str[20];
943 uint16_t port;
944 struct zebra_pbr_ipset *zpi;
945
946 zpi = zpie->backpointer;
947
948 /* range icmp type */
949 if (zpie->src_port_max || zpie->dst_port_max) {
950 vty_out(vty, ":icmp:[type <%u:%u>;code <%u:%u>",
951 zpie->src_port_min, zpie->src_port_max,
952 zpie->dst_port_min, zpie->dst_port_max);
953 } else {
954 port = ((zpie->src_port_min << 8) & 0xff00) +
955 (zpie->dst_port_min & 0xff);
956 memset(decoded_str, 0, sizeof(decoded_str));
957 snprintf(decoded_str, sizeof(decoded_str), "%u/%u",
958 zpie->src_port_min, zpie->dst_port_min);
959 vty_out(vty, ":%s:%s",
960 zpi->family == AF_INET6 ? "ipv6-icmp" : "icmp",
961 lookup_msg(zpi->family == AF_INET6 ?
962 icmpv6_typecode_str : icmp_typecode_str,
963 port, decoded_str));
964 }
965 }
966
967 static void zebra_pbr_display_port(struct vty *vty, uint32_t filter_bm,
968 uint16_t port_min, uint16_t port_max,
969 uint8_t proto)
970 {
971 if (!(filter_bm & PBR_FILTER_PROTO)) {
972 if (port_max)
973 vty_out(vty, ":udp/tcp:%d-%d",
974 port_min, port_max);
975 else
976 vty_out(vty, ":udp/tcp:%d",
977 port_min);
978 } else {
979 if (port_max)
980 vty_out(vty, ":proto %d:%d-%d",
981 proto, port_min, port_max);
982 else
983 vty_out(vty, ":proto %d:%d",
984 proto, port_min);
985 }
986 }
987
988 static int zebra_pbr_show_ipset_entry_walkcb(struct hash_bucket *bucket,
989 void *arg)
990 {
991 struct zebra_pbr_ipset_entry_unique_display *unique =
992 (struct zebra_pbr_ipset_entry_unique_display *)arg;
993 struct zebra_pbr_ipset *zpi = unique->zpi;
994 struct vty *vty = unique->vty;
995 struct zebra_pbr_ipset_entry *zpie =
996 (struct zebra_pbr_ipset_entry *)bucket->data;
997 uint64_t pkts = 0, bytes = 0;
998 int ret = 0;
999
1000 if (zpie->backpointer != zpi)
1001 return HASHWALK_CONTINUE;
1002
1003 if ((zpi->type == IPSET_NET_NET) ||
1004 (zpi->type == IPSET_NET_PORT_NET)) {
1005 char buf[PREFIX_STRLEN];
1006
1007 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
1008 vty_out(vty, "\tfrom %s", buf);
1009 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
1010 zpie->proto != IPPROTO_ICMP)
1011 zebra_pbr_display_port(vty, zpie->filter_bm,
1012 zpie->src_port_min,
1013 zpie->src_port_max,
1014 zpie->proto);
1015 vty_out(vty, " to ");
1016 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
1017 vty_out(vty, "%s", buf);
1018 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
1019 zpie->proto != IPPROTO_ICMP)
1020 zebra_pbr_display_port(vty, zpie->filter_bm,
1021 zpie->dst_port_min,
1022 zpie->dst_port_max,
1023 zpie->proto);
1024 if (zpie->proto == IPPROTO_ICMP)
1025 zebra_pbr_display_icmp(vty, zpie);
1026 } else if ((zpi->type == IPSET_NET) ||
1027 (zpi->type == IPSET_NET_PORT)) {
1028 char buf[PREFIX_STRLEN];
1029
1030 if (zpie->filter_bm & PBR_FILTER_SRC_IP) {
1031 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
1032 vty_out(vty, "\tfrom %s", buf);
1033 }
1034 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
1035 zpie->proto != IPPROTO_ICMP)
1036 zebra_pbr_display_port(vty, zpie->filter_bm,
1037 zpie->src_port_min,
1038 zpie->src_port_max,
1039 zpie->proto);
1040 if (zpie->filter_bm & PBR_FILTER_DST_IP) {
1041 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
1042 vty_out(vty, "\tto %s", buf);
1043 }
1044 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
1045 zpie->proto != IPPROTO_ICMP)
1046 zebra_pbr_display_port(vty, zpie->filter_bm,
1047 zpie->dst_port_min,
1048 zpie->dst_port_max,
1049 zpie->proto);
1050 if (zpie->proto == IPPROTO_ICMP)
1051 zebra_pbr_display_icmp(vty, zpie);
1052 }
1053 vty_out(vty, " (%u)\n", zpie->unique);
1054
1055 ret = hook_call(zebra_pbr_ipset_entry_get_stat, zpie, &pkts,
1056 &bytes);
1057 if (ret && pkts > 0)
1058 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
1059 pkts, bytes);
1060 return HASHWALK_CONTINUE;
1061 }
1062
1063 static int zebra_pbr_show_ipset_walkcb(struct hash_bucket *bucket, void *arg)
1064 {
1065 struct zebra_pbr_env_display *uniqueipset =
1066 (struct zebra_pbr_env_display *)arg;
1067 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
1068 struct zebra_pbr_ipset_entry_unique_display unique;
1069 struct vty *vty = uniqueipset->vty;
1070 struct zebra_ns *zns = uniqueipset->zns;
1071
1072 vty_out(vty, "IPset %s type %s family %s\n", zpi->ipset_name,
1073 zebra_pbr_ipset_type2str(zpi->type),
1074 family2str(zpi->family));
1075 unique.vty = vty;
1076 unique.zpi = zpi;
1077 unique.zns = zns;
1078 hash_walk(zrouter.ipset_entry_hash, zebra_pbr_show_ipset_entry_walkcb,
1079 &unique);
1080 vty_out(vty, "\n");
1081 return HASHWALK_CONTINUE;
1082 }
1083
1084 size_t zebra_pbr_tcpflags_snprintf(char *buffer, size_t len,
1085 uint16_t tcp_val)
1086 {
1087 size_t len_written = 0;
1088 static struct message nt = {0};
1089 const struct message *pnt;
1090 int incr = 0;
1091
1092 for (pnt = tcp_value_str;
1093 memcmp(pnt, &nt, sizeof(struct message)); pnt++)
1094 if (pnt->key & tcp_val) {
1095 len_written += snprintf(buffer + len_written,
1096 len - len_written,
1097 "%s%s", incr ?
1098 ",":"", pnt->str);
1099 incr++;
1100 }
1101 return len_written;
1102 }
1103
1104 /*
1105 */
1106 void zebra_pbr_show_ipset_list(struct vty *vty, char *ipsetname)
1107 {
1108 struct zebra_pbr_ipset *zpi;
1109 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1110 struct zebra_pbr_ipset_entry_unique_display unique;
1111 struct zebra_pbr_env_display uniqueipset;
1112
1113 if (ipsetname) {
1114 zpi = zebra_pbr_lookup_ipset_pername(ipsetname);
1115 if (!zpi) {
1116 vty_out(vty, "No IPset %s found\n", ipsetname);
1117 return;
1118 }
1119 vty_out(vty, "IPset %s type %s family %s\n", ipsetname,
1120 zebra_pbr_ipset_type2str(zpi->type),
1121 family2str(zpi->family));
1122 unique.vty = vty;
1123 unique.zpi = zpi;
1124 unique.zns = zns;
1125 hash_walk(zrouter.ipset_entry_hash,
1126 zebra_pbr_show_ipset_entry_walkcb, &unique);
1127 return;
1128 }
1129 uniqueipset.zns = zns;
1130 uniqueipset.vty = vty;
1131 uniqueipset.name = NULL;
1132 hash_walk(zrouter.ipset_hash, zebra_pbr_show_ipset_walkcb,
1133 &uniqueipset);
1134 }
1135
1136 struct pbr_rule_fwmark_lookup {
1137 struct zebra_pbr_rule *ptr;
1138 uint32_t fwmark;
1139 };
1140
1141 static int zebra_pbr_rule_lookup_fwmark_walkcb(struct hash_bucket *bucket,
1142 void *arg)
1143 {
1144 struct pbr_rule_fwmark_lookup *iprule =
1145 (struct pbr_rule_fwmark_lookup *)arg;
1146 struct zebra_pbr_rule *zpr = (struct zebra_pbr_rule *)bucket->data;
1147
1148 if (iprule->fwmark == zpr->rule.filter.fwmark) {
1149 iprule->ptr = zpr;
1150 return HASHWALK_ABORT;
1151 }
1152 return HASHWALK_CONTINUE;
1153 }
1154
1155 static void zebra_pbr_show_iptable_unit(struct zebra_pbr_iptable *iptable,
1156 struct vty *vty,
1157 struct zebra_ns *zns)
1158 {
1159 int ret;
1160 uint64_t pkts = 0, bytes = 0;
1161
1162 vty_out(vty, "IPtable %s family %s action %s (%u)\n",
1163 iptable->ipset_name,
1164 family2str(iptable->family),
1165 iptable->action == ZEBRA_IPTABLES_DROP ? "drop" : "redirect",
1166 iptable->unique);
1167 if (iptable->type == IPSET_NET_PORT ||
1168 iptable->type == IPSET_NET_PORT_NET) {
1169 if (!(iptable->filter_bm & MATCH_ICMP_SET)) {
1170 if (iptable->filter_bm & PBR_FILTER_DST_PORT)
1171 vty_out(vty, "\t lookup dst port\n");
1172 else if (iptable->filter_bm & PBR_FILTER_SRC_PORT)
1173 vty_out(vty, "\t lookup src port\n");
1174 }
1175 }
1176 if (iptable->pkt_len_min || iptable->pkt_len_max) {
1177 if (!iptable->pkt_len_max)
1178 vty_out(vty, "\t pkt len %u\n",
1179 iptable->pkt_len_min);
1180 else
1181 vty_out(vty, "\t pkt len [%u;%u]\n",
1182 iptable->pkt_len_min,
1183 iptable->pkt_len_max);
1184 }
1185 if (iptable->tcp_flags || iptable->tcp_mask_flags) {
1186 char tcp_flag_str[64];
1187 char tcp_flag_mask_str[64];
1188
1189 zebra_pbr_tcpflags_snprintf(tcp_flag_str,
1190 sizeof(tcp_flag_str),
1191 iptable->tcp_flags);
1192 zebra_pbr_tcpflags_snprintf(tcp_flag_mask_str,
1193 sizeof(tcp_flag_mask_str),
1194 iptable->tcp_mask_flags);
1195 vty_out(vty, "\t tcpflags [%s/%s]\n",
1196 tcp_flag_str, tcp_flag_mask_str);
1197 }
1198 if (iptable->filter_bm & (MATCH_DSCP_SET | MATCH_DSCP_INVERSE_SET)) {
1199 vty_out(vty, "\t dscp %s %d\n",
1200 iptable->filter_bm & MATCH_DSCP_INVERSE_SET ?
1201 "not" : "", iptable->dscp_value);
1202 }
1203 if (iptable->filter_bm & (MATCH_FLOW_LABEL_SET |
1204 MATCH_FLOW_LABEL_INVERSE_SET)) {
1205 vty_out(vty, "\t flowlabel %s %d\n",
1206 iptable->filter_bm & MATCH_FLOW_LABEL_INVERSE_SET ?
1207 "not" : "", iptable->flow_label);
1208 }
1209 if (iptable->fragment) {
1210 char val_str[10];
1211
1212 snprintf(val_str, sizeof(val_str), "%d", iptable->fragment);
1213 vty_out(vty, "\t fragment%s %s\n",
1214 iptable->filter_bm & MATCH_FRAGMENT_INVERSE_SET ?
1215 " not" : "", lookup_msg(fragment_value_str,
1216 iptable->fragment, val_str));
1217 }
1218 if (iptable->protocol) {
1219 vty_out(vty, "\t protocol %d\n",
1220 iptable->protocol);
1221 }
1222 ret = hook_call(zebra_pbr_iptable_get_stat, iptable, &pkts,
1223 &bytes);
1224 if (ret && pkts > 0)
1225 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
1226 pkts, bytes);
1227 if (iptable->action != ZEBRA_IPTABLES_DROP) {
1228 struct pbr_rule_fwmark_lookup prfl;
1229
1230 prfl.fwmark = iptable->fwmark;
1231 prfl.ptr = NULL;
1232 hash_walk(zrouter.rules_hash,
1233 &zebra_pbr_rule_lookup_fwmark_walkcb, &prfl);
1234 if (prfl.ptr) {
1235 struct zebra_pbr_rule *zpr = prfl.ptr;
1236
1237 vty_out(vty, "\t table %u, fwmark %u\n",
1238 zpr->rule.action.table,
1239 prfl.fwmark);
1240 }
1241 }
1242 }
1243
1244 static int zebra_pbr_show_iptable_walkcb(struct hash_bucket *bucket, void *arg)
1245 {
1246 struct zebra_pbr_iptable *iptable =
1247 (struct zebra_pbr_iptable *)bucket->data;
1248 struct zebra_pbr_env_display *env = (struct zebra_pbr_env_display *)arg;
1249 struct vty *vty = env->vty;
1250 struct zebra_ns *zns = env->zns;
1251 char *iptable_name = env->name;
1252
1253 if (!iptable_name)
1254 zebra_pbr_show_iptable_unit(iptable, vty, zns);
1255 else if (!strncmp(iptable_name,
1256 iptable->ipset_name,
1257 ZEBRA_IPSET_NAME_SIZE))
1258 zebra_pbr_show_iptable_unit(iptable, vty, zns);
1259 return HASHWALK_CONTINUE;
1260 }
1261
1262 void zebra_pbr_show_iptable(struct vty *vty, char *iptable_name)
1263 {
1264 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1265 struct zebra_pbr_env_display env;
1266
1267 env.vty = vty;
1268 env.zns = zns;
1269 env.name = iptable_name;
1270 hash_walk(zrouter.iptable_hash, zebra_pbr_show_iptable_walkcb, &env);
1271 }
1272
1273 void zebra_pbr_iptable_update_interfacelist(struct stream *s,
1274 struct zebra_pbr_iptable *zpi)
1275 {
1276 uint32_t i = 0, index;
1277 struct interface *ifp;
1278 char *name;
1279
1280 for (i = 0; i < zpi->nb_interface; i++) {
1281 STREAM_GETL(s, index);
1282 ifp = if_lookup_by_index(index, zpi->vrf_id);
1283 if (!ifp)
1284 continue;
1285 name = XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifp->name);
1286 listnode_add(zpi->interface_name_list, name);
1287 }
1288 stream_failure:
1289 return;
1290 }