1 /* Zebra Policy Based Routing (PBR) main handling.
2 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * This file is part of FRR.
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
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.
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
29 #include "zebra/zebra_router.h"
30 #include "zebra/zebra_pbr.h"
32 #include "zebra/zapi_msg.h"
33 #include "zebra/zebra_memory.h"
34 #include "zebra/zserv.h"
37 DEFINE_MTYPE_STATIC(ZEBRA
, PBR_IPTABLE_IFNAME
, "PBR interface list")
40 static const struct message ipset_type_msg
[] = {
41 {IPSET_NET_PORT_NET
, "net,port,net"},
42 {IPSET_NET_PORT
, "net,port"},
43 {IPSET_NET_NET
, "net,net"},
48 const struct message icmp_typecode_str
[] = {
49 { 0 << 8, "echo-reply"},
51 { 3 << 8, "network-unreachable"},
52 { (3 << 8) + 1, "host-unreachable"},
53 { (3 << 8) + 2, "protocol-unreachable"},
54 { (3 << 8) + 3, "port-unreachable"},
55 { (3 << 8) + 4, "fragmentation-needed"},
56 { (3 << 8) + 5, "source-route-failed"},
57 { (3 << 8) + 6, "network-unknown"},
58 { (3 << 8) + 7, "host-unknown"},
59 { (3 << 8) + 9, "network-prohibited"},
60 { (3 << 8) + 10, "host-prohibited"},
61 { (3 << 8) + 11, "TOS-network-unreachable"},
62 { (3 << 8) + 12, "TOS-host-unreachable"},
63 { (3 << 8) + 13, "communication-prohibited"},
64 { (3 << 8) + 14, "host-precedence-violation"},
65 { (3 << 8) + 15, "precedence-cutoff"},
66 { 4 << 8, "source-quench"},
67 { 5 << 8, "network-redirect"},
68 { (5 << 8) + 1, "host-redirect"},
69 { (5 << 8) + 2, "TOS-network-redirect"},
70 { (5 << 8) + 3, "TOS-host-redirect"},
71 { 8 << 8, "echo-request"},
73 { 9 << 8, "router-advertisement"},
74 { 10 << 8, "router-solicitation"},
75 { 11 << 8, "ttl-zero-during-transit"},
76 { (11 << 8) + 1, "ttl-zero-during-reassembly"},
77 { 12 << 8, "ip-header-bad"},
78 { (12 << 8) + 1, "required-option-missing"},
79 { 13 << 8, "timestamp-request"},
80 { 14 << 8, "timestamp-reply"},
81 { 17 << 8, "address-mask-request"},
82 { 18 << 8, "address-mask-reply"},
87 static const struct message tcp_value_str
[] = {
88 {TCP_HEADER_FIN
, "FIN"},
89 {TCP_HEADER_SYN
, "SYN"},
90 {TCP_HEADER_RST
, "RST"},
91 {TCP_HEADER_PSH
, "PSH"},
92 {TCP_HEADER_ACK
, "ACK"},
93 {TCP_HEADER_URG
, "URG"},
97 static const struct message fragment_value_str
[] = {
100 {4, "first-fragment"},
101 {8, "last-fragment"},
105 /* static function declarations */
106 DEFINE_HOOK(zebra_pbr_ipset_entry_get_stat
,
107 (struct zebra_pbr_ipset_entry
*ipset
, uint64_t *pkts
,
109 (ipset
, pkts
, bytes
))
111 DEFINE_HOOK(zebra_pbr_iptable_get_stat
,
112 (struct zebra_pbr_iptable
*iptable
, uint64_t *pkts
,
114 (iptable
, pkts
, bytes
))
116 DEFINE_HOOK(zebra_pbr_iptable_update
,
117 (int cmd
, struct zebra_pbr_iptable
*iptable
), (cmd
, iptable
));
119 DEFINE_HOOK(zebra_pbr_ipset_entry_update
,
120 (int cmd
, struct zebra_pbr_ipset_entry
*ipset
), (cmd
, ipset
));
122 DEFINE_HOOK(zebra_pbr_ipset_update
,
123 (int cmd
, struct zebra_pbr_ipset
*ipset
), (cmd
, ipset
));
125 /* Private functions */
127 /* Public functions */
128 void zebra_pbr_rules_free(void *arg
)
130 struct zebra_pbr_rule
*rule
;
132 rule
= (struct zebra_pbr_rule
*)arg
;
134 (void)kernel_del_pbr_rule(rule
);
135 XFREE(MTYPE_TMP
, rule
);
138 uint32_t zebra_pbr_rules_hash_key(const void *arg
)
140 const struct zebra_pbr_rule
*rule
;
144 key
= jhash_3words(rule
->rule
.seq
, rule
->rule
.priority
,
145 rule
->rule
.action
.table
,
146 prefix_hash_key(&rule
->rule
.filter
.src_ip
));
148 if (rule
->rule
.filter
.fwmark
)
149 key
= jhash_3words(rule
->rule
.filter
.fwmark
, rule
->vrf_id
,
150 rule
->rule
.ifindex
, key
);
152 key
= jhash_2words(rule
->vrf_id
, rule
->rule
.ifindex
, key
);
154 return jhash_3words(rule
->rule
.filter
.src_port
,
155 rule
->rule
.filter
.dst_port
,
156 prefix_hash_key(&rule
->rule
.filter
.dst_ip
),
157 jhash_1word(rule
->rule
.unique
, key
));
160 bool zebra_pbr_rules_hash_equal(const void *arg1
, const void *arg2
)
162 const struct zebra_pbr_rule
*r1
, *r2
;
164 r1
= (const struct zebra_pbr_rule
*)arg1
;
165 r2
= (const struct zebra_pbr_rule
*)arg2
;
167 if (r1
->rule
.seq
!= r2
->rule
.seq
)
170 if (r1
->rule
.priority
!= r2
->rule
.priority
)
173 if (r1
->rule
.unique
!= r2
->rule
.unique
)
176 if (r1
->rule
.action
.table
!= r2
->rule
.action
.table
)
179 if (r1
->rule
.filter
.src_port
!= r2
->rule
.filter
.src_port
)
182 if (r1
->rule
.filter
.dst_port
!= r2
->rule
.filter
.dst_port
)
185 if (r1
->rule
.filter
.fwmark
!= r2
->rule
.filter
.fwmark
)
188 if (!prefix_same(&r1
->rule
.filter
.src_ip
, &r2
->rule
.filter
.src_ip
))
191 if (!prefix_same(&r1
->rule
.filter
.dst_ip
, &r2
->rule
.filter
.dst_ip
))
194 if (r1
->rule
.ifindex
!= r2
->rule
.ifindex
)
197 if (r1
->vrf_id
!= r2
->vrf_id
)
203 struct pbr_rule_unique_lookup
{
204 struct zebra_pbr_rule
*rule
;
210 static int pbr_rule_lookup_unique_walker(struct hash_bucket
*b
, void *data
)
212 struct pbr_rule_unique_lookup
*pul
= data
;
213 struct zebra_pbr_rule
*rule
= b
->data
;
215 if (pul
->unique
== rule
->rule
.unique
216 && pul
->ifindex
== rule
->rule
.ifindex
217 && pul
->vrf_id
== rule
->vrf_id
) {
219 return HASHWALK_ABORT
;
222 return HASHWALK_CONTINUE
;
225 static struct zebra_pbr_rule
*
226 pbr_rule_lookup_unique(struct zebra_pbr_rule
*zrule
)
228 struct pbr_rule_unique_lookup pul
;
230 pul
.unique
= zrule
->rule
.unique
;
231 pul
.ifindex
= zrule
->rule
.ifindex
;
233 pul
.vrf_id
= zrule
->vrf_id
;
234 hash_walk(zrouter
.rules_hash
, &pbr_rule_lookup_unique_walker
, &pul
);
239 void zebra_pbr_ipset_free(void *arg
)
241 struct zebra_pbr_ipset
*ipset
;
243 ipset
= (struct zebra_pbr_ipset
*)arg
;
244 hook_call(zebra_pbr_ipset_update
, 0, ipset
);
245 XFREE(MTYPE_TMP
, ipset
);
248 uint32_t zebra_pbr_ipset_hash_key(const void *arg
)
250 const struct zebra_pbr_ipset
*ipset
= arg
;
251 uint32_t *pnt
= (uint32_t *)&ipset
->ipset_name
;
252 uint32_t key
= jhash_1word(ipset
->vrf_id
, 0x63ab42de);
254 return jhash2(pnt
, ZEBRA_IPSET_NAME_HASH_SIZE
, key
);
257 bool zebra_pbr_ipset_hash_equal(const void *arg1
, const void *arg2
)
259 const struct zebra_pbr_ipset
*r1
, *r2
;
261 r1
= (const struct zebra_pbr_ipset
*)arg1
;
262 r2
= (const struct zebra_pbr_ipset
*)arg2
;
264 if (r1
->type
!= r2
->type
)
266 if (r1
->unique
!= r2
->unique
)
268 if (r1
->vrf_id
!= r2
->vrf_id
)
271 if (strncmp(r1
->ipset_name
, r2
->ipset_name
,
272 ZEBRA_IPSET_NAME_SIZE
))
277 void zebra_pbr_ipset_entry_free(void *arg
)
279 struct zebra_pbr_ipset_entry
*ipset
;
281 ipset
= (struct zebra_pbr_ipset_entry
*)arg
;
283 hook_call(zebra_pbr_ipset_entry_update
, 0, ipset
);
285 XFREE(MTYPE_TMP
, ipset
);
288 uint32_t zebra_pbr_ipset_entry_hash_key(const void *arg
)
290 const struct zebra_pbr_ipset_entry
*ipset
;
294 key
= prefix_hash_key(&ipset
->src
);
295 key
= jhash_1word(ipset
->unique
, key
);
296 key
= jhash_1word(prefix_hash_key(&ipset
->dst
), key
);
297 key
= jhash(&ipset
->dst_port_min
, 2, key
);
298 key
= jhash(&ipset
->dst_port_max
, 2, key
);
299 key
= jhash(&ipset
->src_port_min
, 2, key
);
300 key
= jhash(&ipset
->src_port_max
, 2, key
);
301 key
= jhash(&ipset
->proto
, 1, key
);
306 bool zebra_pbr_ipset_entry_hash_equal(const void *arg1
, const void *arg2
)
308 const struct zebra_pbr_ipset_entry
*r1
, *r2
;
310 r1
= (const struct zebra_pbr_ipset_entry
*)arg1
;
311 r2
= (const struct zebra_pbr_ipset_entry
*)arg2
;
313 if (r1
->unique
!= r2
->unique
)
316 if (!prefix_same(&r1
->src
, &r2
->src
))
319 if (!prefix_same(&r1
->dst
, &r2
->dst
))
322 if (r1
->src_port_min
!= r2
->src_port_min
)
325 if (r1
->src_port_max
!= r2
->src_port_max
)
328 if (r1
->dst_port_min
!= r2
->dst_port_min
)
331 if (r1
->dst_port_max
!= r2
->dst_port_max
)
334 if (r1
->proto
!= r2
->proto
)
339 void zebra_pbr_iptable_free(void *arg
)
341 struct zebra_pbr_iptable
*iptable
;
342 struct listnode
*node
, *nnode
;
345 iptable
= (struct zebra_pbr_iptable
*)arg
;
346 hook_call(zebra_pbr_iptable_update
, 0, iptable
);
348 if (iptable
->interface_name_list
) {
349 for (ALL_LIST_ELEMENTS(iptable
->interface_name_list
, node
,
351 XFREE(MTYPE_PBR_IPTABLE_IFNAME
, name
);
352 list_delete_node(iptable
->interface_name_list
, node
);
354 list_delete(&iptable
->interface_name_list
);
356 XFREE(MTYPE_TMP
, iptable
);
359 uint32_t zebra_pbr_iptable_hash_key(const void *arg
)
361 const struct zebra_pbr_iptable
*iptable
= arg
;
362 uint32_t *pnt
= (uint32_t *)&(iptable
->ipset_name
);
365 key
= jhash2(pnt
, ZEBRA_IPSET_NAME_HASH_SIZE
,
367 key
= jhash_1word(iptable
->fwmark
, key
);
368 key
= jhash_1word(iptable
->pkt_len_min
, key
);
369 key
= jhash_1word(iptable
->pkt_len_max
, key
);
370 key
= jhash_1word(iptable
->tcp_flags
, key
);
371 key
= jhash_1word(iptable
->tcp_mask_flags
, key
);
372 key
= jhash_1word(iptable
->dscp_value
, key
);
373 key
= jhash_1word(iptable
->protocol
, key
);
374 key
= jhash_1word(iptable
->fragment
, key
);
375 key
= jhash_1word(iptable
->vrf_id
, key
);
377 return jhash_3words(iptable
->filter_bm
, iptable
->type
,
378 iptable
->unique
, key
);
381 bool zebra_pbr_iptable_hash_equal(const void *arg1
, const void *arg2
)
383 const struct zebra_pbr_iptable
*r1
, *r2
;
385 r1
= (const struct zebra_pbr_iptable
*)arg1
;
386 r2
= (const struct zebra_pbr_iptable
*)arg2
;
388 if (r1
->vrf_id
!= r2
->vrf_id
)
390 if (r1
->type
!= r2
->type
)
392 if (r1
->unique
!= r2
->unique
)
394 if (r1
->filter_bm
!= r2
->filter_bm
)
396 if (r1
->fwmark
!= r2
->fwmark
)
398 if (r1
->action
!= r2
->action
)
400 if (strncmp(r1
->ipset_name
, r2
->ipset_name
,
401 ZEBRA_IPSET_NAME_SIZE
))
403 if (r1
->pkt_len_min
!= r2
->pkt_len_min
)
405 if (r1
->pkt_len_max
!= r2
->pkt_len_max
)
407 if (r1
->tcp_flags
!= r2
->tcp_flags
)
409 if (r1
->tcp_mask_flags
!= r2
->tcp_mask_flags
)
411 if (r1
->dscp_value
!= r2
->dscp_value
)
413 if (r1
->fragment
!= r2
->fragment
)
415 if (r1
->protocol
!= r2
->protocol
)
420 static void *pbr_rule_alloc_intern(void *arg
)
422 struct zebra_pbr_rule
*zpr
;
423 struct zebra_pbr_rule
*new;
425 zpr
= (struct zebra_pbr_rule
*)arg
;
427 new = XCALLOC(MTYPE_TMP
, sizeof(*new));
429 memcpy(new, zpr
, sizeof(*zpr
));
434 static int pbr_rule_release(struct zebra_pbr_rule
*rule
)
436 struct zebra_pbr_rule
*lookup
;
438 lookup
= hash_lookup(zrouter
.rules_hash
, rule
);
443 hash_release(zrouter
.rules_hash
, lookup
);
444 XFREE(MTYPE_TMP
, lookup
);
449 void zebra_pbr_add_rule(struct zebra_pbr_rule
*rule
)
451 struct zebra_pbr_rule
*found
;
454 * Check if we already have it (this checks via a unique ID, walking
455 * over the hash table, not via a hash operation).
457 found
= pbr_rule_lookup_unique(rule
);
459 (void)hash_get(zrouter
.rules_hash
, rule
, pbr_rule_alloc_intern
);
461 /* If found, this is an update */
463 (void)kernel_update_pbr_rule(found
, rule
);
465 if (pbr_rule_release(found
))
467 "%s: Rule being updated we know nothing about",
468 __PRETTY_FUNCTION__
);
471 (void)kernel_add_pbr_rule(rule
);
474 void zebra_pbr_del_rule(struct zebra_pbr_rule
*rule
)
476 (void)kernel_del_pbr_rule(rule
);
478 if (pbr_rule_release(rule
))
479 zlog_debug("%s: Rule being deleted we know nothing about",
483 static void zebra_pbr_cleanup_rules(struct hash_bucket
*b
, void *data
)
485 struct zebra_pbr_rule
*rule
= b
->data
;
488 if (rule
->sock
== *sock
) {
489 (void)kernel_del_pbr_rule(rule
);
490 if (hash_release(zrouter
.rules_hash
, rule
))
491 XFREE(MTYPE_TMP
, rule
);
494 "%s: Rule seq: %u is being cleaned but we can't find it in our tables",
495 __func__
, rule
->rule
.seq
);
499 static void zebra_pbr_cleanup_ipset(struct hash_bucket
*b
, void *data
)
501 struct zebra_pbr_ipset
*ipset
= b
->data
;
504 if (ipset
->sock
== *sock
) {
505 if (hash_release(zrouter
.ipset_hash
, ipset
))
506 zebra_pbr_ipset_free(ipset
);
508 hook_call(zebra_pbr_ipset_update
, 0, ipset
);
512 static void zebra_pbr_cleanup_ipset_entry(struct hash_bucket
*b
, void *data
)
514 struct zebra_pbr_ipset_entry
*ipset
= b
->data
;
517 if (ipset
->sock
== *sock
) {
518 if (hash_release(zrouter
.ipset_entry_hash
, ipset
))
519 zebra_pbr_ipset_entry_free(ipset
);
521 hook_call(zebra_pbr_ipset_entry_update
, 0, ipset
);
525 static void zebra_pbr_cleanup_iptable(struct hash_bucket
*b
, void *data
)
527 struct zebra_pbr_iptable
*iptable
= b
->data
;
530 if (iptable
->sock
== *sock
) {
531 if (hash_release(zrouter
.iptable_hash
, iptable
))
532 zebra_pbr_iptable_free(iptable
);
534 hook_call(zebra_pbr_iptable_update
, 0, iptable
);
538 static int zebra_pbr_client_close_cleanup(struct zserv
*client
)
540 int sock
= client
->sock
;
544 hash_iterate(zrouter
.rules_hash
, zebra_pbr_cleanup_rules
, &sock
);
545 hash_iterate(zrouter
.iptable_hash
, zebra_pbr_cleanup_iptable
, &sock
);
546 hash_iterate(zrouter
.ipset_entry_hash
, zebra_pbr_cleanup_ipset_entry
,
548 hash_iterate(zrouter
.ipset_hash
, zebra_pbr_cleanup_ipset
, &sock
);
552 void zebra_pbr_init(void)
554 hook_register(zserv_client_close
, zebra_pbr_client_close_cleanup
);
557 static void *pbr_ipset_alloc_intern(void *arg
)
559 struct zebra_pbr_ipset
*zpi
;
560 struct zebra_pbr_ipset
*new;
562 zpi
= (struct zebra_pbr_ipset
*)arg
;
564 new = XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_ipset
));
566 memcpy(new, zpi
, sizeof(*zpi
));
571 void zebra_pbr_create_ipset(struct zebra_pbr_ipset
*ipset
)
575 (void)hash_get(zrouter
.ipset_hash
, ipset
, pbr_ipset_alloc_intern
);
576 ret
= hook_call(zebra_pbr_ipset_update
, 1, ipset
);
577 kernel_pbr_ipset_add_del_status(ipset
,
578 ret
? ZEBRA_DPLANE_INSTALL_SUCCESS
579 : ZEBRA_DPLANE_INSTALL_FAILURE
);
582 void zebra_pbr_destroy_ipset(struct zebra_pbr_ipset
*ipset
)
584 struct zebra_pbr_ipset
*lookup
;
586 lookup
= hash_lookup(zrouter
.ipset_hash
, ipset
);
587 hook_call(zebra_pbr_ipset_update
, 0, ipset
);
589 hash_release(zrouter
.ipset_hash
, lookup
);
590 XFREE(MTYPE_TMP
, lookup
);
593 "%s: IPSet Entry being deleted we know nothing about",
597 struct pbr_ipset_name_lookup
{
598 struct zebra_pbr_ipset
*ipset
;
599 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
602 const char *zebra_pbr_ipset_type2str(uint32_t type
)
604 return lookup_msg(ipset_type_msg
, type
,
605 "Unrecognized IPset Type");
608 static int zebra_pbr_ipset_pername_walkcb(struct hash_bucket
*bucket
, void *arg
)
610 struct pbr_ipset_name_lookup
*pinl
=
611 (struct pbr_ipset_name_lookup
*)arg
;
612 struct zebra_pbr_ipset
*zpi
= (struct zebra_pbr_ipset
*)bucket
->data
;
614 if (!strncmp(pinl
->ipset_name
, zpi
->ipset_name
,
615 ZEBRA_IPSET_NAME_SIZE
)) {
617 return HASHWALK_ABORT
;
619 return HASHWALK_CONTINUE
;
622 struct zebra_pbr_ipset
*zebra_pbr_lookup_ipset_pername(char *ipsetname
)
624 struct pbr_ipset_name_lookup pinl
;
625 struct pbr_ipset_name_lookup
*ptr
= &pinl
;
629 memset(ptr
, 0, sizeof(struct pbr_ipset_name_lookup
));
630 snprintf((char *)ptr
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
, "%s",
632 hash_walk(zrouter
.ipset_hash
, zebra_pbr_ipset_pername_walkcb
, ptr
);
636 static void *pbr_ipset_entry_alloc_intern(void *arg
)
638 struct zebra_pbr_ipset_entry
*zpi
;
639 struct zebra_pbr_ipset_entry
*new;
641 zpi
= (struct zebra_pbr_ipset_entry
*)arg
;
643 new = XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_ipset_entry
));
645 memcpy(new, zpi
, sizeof(*zpi
));
650 void zebra_pbr_add_ipset_entry(struct zebra_pbr_ipset_entry
*ipset
)
654 (void)hash_get(zrouter
.ipset_entry_hash
, ipset
,
655 pbr_ipset_entry_alloc_intern
);
656 ret
= hook_call(zebra_pbr_ipset_entry_update
, 1, ipset
);
657 kernel_pbr_ipset_entry_add_del_status(ipset
,
658 ret
? ZEBRA_DPLANE_INSTALL_SUCCESS
659 : ZEBRA_DPLANE_INSTALL_FAILURE
);
662 void zebra_pbr_del_ipset_entry(struct zebra_pbr_ipset_entry
*ipset
)
664 struct zebra_pbr_ipset_entry
*lookup
;
666 lookup
= hash_lookup(zrouter
.ipset_entry_hash
, ipset
);
667 hook_call(zebra_pbr_ipset_entry_update
, 0, ipset
);
669 hash_release(zrouter
.ipset_entry_hash
, lookup
);
670 XFREE(MTYPE_TMP
, lookup
);
672 zlog_debug("%s: IPSet being deleted we know nothing about",
676 static void *pbr_iptable_alloc_intern(void *arg
)
678 struct zebra_pbr_iptable
*zpi
;
679 struct zebra_pbr_iptable
*new;
683 zpi
= (struct zebra_pbr_iptable
*)arg
;
685 new = XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
687 /* Deep structure copy */
688 memcpy(new, zpi
, sizeof(*zpi
));
689 new->interface_name_list
= list_new();
691 if (zpi
->interface_name_list
) {
692 for (ALL_LIST_ELEMENTS_RO(zpi
->interface_name_list
, ln
, ifname
))
693 listnode_add(new->interface_name_list
,
694 XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME
, ifname
));
700 void zebra_pbr_add_iptable(struct zebra_pbr_iptable
*iptable
)
704 (void)hash_get(zrouter
.iptable_hash
, iptable
, pbr_iptable_alloc_intern
);
705 ret
= hook_call(zebra_pbr_iptable_update
, 1, iptable
);
706 kernel_pbr_iptable_add_del_status(iptable
,
707 ret
? ZEBRA_DPLANE_INSTALL_SUCCESS
708 : ZEBRA_DPLANE_INSTALL_FAILURE
);
711 void zebra_pbr_del_iptable(struct zebra_pbr_iptable
*iptable
)
713 struct zebra_pbr_iptable
*lookup
;
715 lookup
= hash_lookup(zrouter
.iptable_hash
, iptable
);
716 hook_call(zebra_pbr_iptable_update
, 0, iptable
);
718 struct listnode
*node
, *nnode
;
721 hash_release(zrouter
.iptable_hash
, lookup
);
722 for (ALL_LIST_ELEMENTS(iptable
->interface_name_list
,
723 node
, nnode
, name
)) {
724 XFREE(MTYPE_PBR_IPTABLE_IFNAME
, name
);
725 list_delete_node(iptable
->interface_name_list
,
728 list_delete(&iptable
->interface_name_list
);
729 XFREE(MTYPE_TMP
, lookup
);
731 zlog_debug("%s: IPTable being deleted we know nothing about",
736 * Handle success or failure of rule (un)install in the kernel.
738 void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule
*rule
,
739 enum zebra_dplane_status res
)
742 case ZEBRA_DPLANE_INSTALL_SUCCESS
:
743 zsend_rule_notify_owner(rule
, ZAPI_RULE_INSTALLED
);
745 case ZEBRA_DPLANE_INSTALL_FAILURE
:
746 zsend_rule_notify_owner(rule
, ZAPI_RULE_FAIL_INSTALL
);
748 case ZEBRA_DPLANE_DELETE_SUCCESS
:
749 zsend_rule_notify_owner(rule
, ZAPI_RULE_REMOVED
);
751 case ZEBRA_DPLANE_DELETE_FAILURE
:
752 zsend_rule_notify_owner(rule
, ZAPI_RULE_FAIL_REMOVE
);
754 case ZEBRA_DPLANE_STATUS_NONE
:
760 * Handle success or failure of ipset (un)install in the kernel.
762 void kernel_pbr_ipset_add_del_status(struct zebra_pbr_ipset
*ipset
,
763 enum zebra_dplane_status res
)
766 case ZEBRA_DPLANE_INSTALL_SUCCESS
:
767 zsend_ipset_notify_owner(ipset
, ZAPI_IPSET_INSTALLED
);
769 case ZEBRA_DPLANE_INSTALL_FAILURE
:
770 zsend_ipset_notify_owner(ipset
, ZAPI_IPSET_FAIL_INSTALL
);
772 case ZEBRA_DPLANE_DELETE_SUCCESS
:
773 zsend_ipset_notify_owner(ipset
, ZAPI_IPSET_REMOVED
);
775 case ZEBRA_DPLANE_DELETE_FAILURE
:
776 zsend_ipset_notify_owner(ipset
, ZAPI_IPSET_FAIL_REMOVE
);
778 case ZEBRA_DPLANE_STATUS_NONE
:
784 * Handle success or failure of ipset (un)install in the kernel.
786 void kernel_pbr_ipset_entry_add_del_status(
787 struct zebra_pbr_ipset_entry
*ipset
,
788 enum zebra_dplane_status res
)
791 case ZEBRA_DPLANE_INSTALL_SUCCESS
:
792 zsend_ipset_entry_notify_owner(ipset
,
793 ZAPI_IPSET_ENTRY_INSTALLED
);
795 case ZEBRA_DPLANE_INSTALL_FAILURE
:
796 zsend_ipset_entry_notify_owner(ipset
,
797 ZAPI_IPSET_ENTRY_FAIL_INSTALL
);
799 case ZEBRA_DPLANE_DELETE_SUCCESS
:
800 zsend_ipset_entry_notify_owner(ipset
,
801 ZAPI_IPSET_ENTRY_REMOVED
);
803 case ZEBRA_DPLANE_DELETE_FAILURE
:
804 zsend_ipset_entry_notify_owner(ipset
,
805 ZAPI_IPSET_ENTRY_FAIL_REMOVE
);
807 case ZEBRA_DPLANE_STATUS_NONE
:
813 * Handle success or failure of ipset (un)install in the kernel.
815 void kernel_pbr_iptable_add_del_status(struct zebra_pbr_iptable
*iptable
,
816 enum zebra_dplane_status res
)
819 case ZEBRA_DPLANE_INSTALL_SUCCESS
:
820 zsend_iptable_notify_owner(iptable
, ZAPI_IPTABLE_INSTALLED
);
822 case ZEBRA_DPLANE_INSTALL_FAILURE
:
823 zsend_iptable_notify_owner(iptable
, ZAPI_IPTABLE_FAIL_INSTALL
);
825 case ZEBRA_DPLANE_DELETE_SUCCESS
:
826 zsend_iptable_notify_owner(iptable
,
827 ZAPI_IPTABLE_REMOVED
);
829 case ZEBRA_DPLANE_DELETE_FAILURE
:
830 zsend_iptable_notify_owner(iptable
,
831 ZAPI_IPTABLE_FAIL_REMOVE
);
833 case ZEBRA_DPLANE_STATUS_NONE
:
839 * Handle rule delete notification from kernel.
841 int kernel_pbr_rule_del(struct zebra_pbr_rule
*rule
)
846 struct zebra_pbr_ipset_entry_unique_display
{
847 struct zebra_pbr_ipset
*zpi
;
849 struct zebra_ns
*zns
;
852 struct zebra_pbr_env_display
{
853 struct zebra_ns
*zns
;
858 static const char *zebra_pbr_prefix2str(union prefixconstptr pu
,
861 const struct prefix
*p
= pu
.p
;
862 char buf
[PREFIX2STR_BUFFER
];
864 if (p
->family
== AF_INET
&& p
->prefixlen
== IPV4_MAX_PREFIXLEN
) {
865 snprintf(str
, size
, "%s", inet_ntop(p
->family
, &p
->u
.prefix
,
866 buf
, PREFIX2STR_BUFFER
));
869 return prefix2str(pu
, str
, size
);
872 static void zebra_pbr_display_icmp(struct vty
*vty
,
873 struct zebra_pbr_ipset_entry
*zpie
)
875 char decoded_str
[20];
878 /* range icmp type */
879 if (zpie
->src_port_max
|| zpie
->dst_port_max
) {
880 vty_out(vty
, ":icmp:[type <%d:%d>;code <%d:%d>",
881 zpie
->src_port_min
, zpie
->src_port_max
,
882 zpie
->dst_port_min
, zpie
->dst_port_max
);
884 port
= ((zpie
->src_port_min
<< 8) & 0xff00) +
885 (zpie
->dst_port_min
& 0xff);
886 memset(decoded_str
, 0, sizeof(decoded_str
));
887 sprintf(decoded_str
, "%d/%d",
890 vty_out(vty
, ":icmp:%s",
891 lookup_msg(icmp_typecode_str
,
896 static void zebra_pbr_display_port(struct vty
*vty
, uint32_t filter_bm
,
897 uint16_t port_min
, uint16_t port_max
,
900 if (!(filter_bm
& PBR_FILTER_PROTO
)) {
902 vty_out(vty
, ":udp/tcp:%d-%d",
905 vty_out(vty
, ":udp/tcp:%d",
909 vty_out(vty
, ":proto %d:%d-%d",
910 proto
, port_min
, port_max
);
912 vty_out(vty
, ":proto %d:%d",
917 static int zebra_pbr_show_ipset_entry_walkcb(struct hash_bucket
*bucket
,
920 struct zebra_pbr_ipset_entry_unique_display
*unique
=
921 (struct zebra_pbr_ipset_entry_unique_display
*)arg
;
922 struct zebra_pbr_ipset
*zpi
= unique
->zpi
;
923 struct vty
*vty
= unique
->vty
;
924 struct zebra_pbr_ipset_entry
*zpie
=
925 (struct zebra_pbr_ipset_entry
*)bucket
->data
;
926 uint64_t pkts
= 0, bytes
= 0;
929 if (zpie
->backpointer
!= zpi
)
930 return HASHWALK_CONTINUE
;
932 if ((zpi
->type
== IPSET_NET_NET
) ||
933 (zpi
->type
== IPSET_NET_PORT_NET
)) {
934 char buf
[PREFIX_STRLEN
];
936 zebra_pbr_prefix2str(&(zpie
->src
), buf
, sizeof(buf
));
937 vty_out(vty
, "\tfrom %s", buf
);
938 if (zpie
->filter_bm
& PBR_FILTER_SRC_PORT
&&
939 zpie
->proto
!= IPPROTO_ICMP
)
940 zebra_pbr_display_port(vty
, zpie
->filter_bm
,
944 vty_out(vty
, " to ");
945 zebra_pbr_prefix2str(&(zpie
->dst
), buf
, sizeof(buf
));
946 vty_out(vty
, "%s", buf
);
947 if (zpie
->filter_bm
& PBR_FILTER_DST_PORT
&&
948 zpie
->proto
!= IPPROTO_ICMP
)
949 zebra_pbr_display_port(vty
, zpie
->filter_bm
,
953 if (zpie
->proto
== IPPROTO_ICMP
)
954 zebra_pbr_display_icmp(vty
, zpie
);
955 } else if ((zpi
->type
== IPSET_NET
) ||
956 (zpi
->type
== IPSET_NET_PORT
)) {
957 char buf
[PREFIX_STRLEN
];
959 if (zpie
->filter_bm
& PBR_FILTER_SRC_IP
) {
960 zebra_pbr_prefix2str(&(zpie
->src
), buf
, sizeof(buf
));
961 vty_out(vty
, "\tfrom %s", buf
);
963 if (zpie
->filter_bm
& PBR_FILTER_SRC_PORT
&&
964 zpie
->proto
!= IPPROTO_ICMP
)
965 zebra_pbr_display_port(vty
, zpie
->filter_bm
,
969 if (zpie
->filter_bm
& PBR_FILTER_DST_IP
) {
970 zebra_pbr_prefix2str(&(zpie
->dst
), buf
, sizeof(buf
));
971 vty_out(vty
, "\tto %s", buf
);
973 if (zpie
->filter_bm
& PBR_FILTER_DST_PORT
&&
974 zpie
->proto
!= IPPROTO_ICMP
)
975 zebra_pbr_display_port(vty
, zpie
->filter_bm
,
979 if (zpie
->proto
== IPPROTO_ICMP
)
980 zebra_pbr_display_icmp(vty
, zpie
);
982 vty_out(vty
, " (%u)\n", zpie
->unique
);
984 ret
= hook_call(zebra_pbr_ipset_entry_get_stat
, zpie
, &pkts
,
987 vty_out(vty
, "\t pkts %" PRIu64
", bytes %" PRIu64
"\n",
989 return HASHWALK_CONTINUE
;
992 static int zebra_pbr_show_ipset_walkcb(struct hash_bucket
*bucket
, void *arg
)
994 struct zebra_pbr_env_display
*uniqueipset
=
995 (struct zebra_pbr_env_display
*)arg
;
996 struct zebra_pbr_ipset
*zpi
= (struct zebra_pbr_ipset
*)bucket
->data
;
997 struct zebra_pbr_ipset_entry_unique_display unique
;
998 struct vty
*vty
= uniqueipset
->vty
;
999 struct zebra_ns
*zns
= uniqueipset
->zns
;
1001 vty_out(vty
, "IPset %s type %s\n", zpi
->ipset_name
,
1002 zebra_pbr_ipset_type2str(zpi
->type
));
1006 hash_walk(zrouter
.ipset_entry_hash
, zebra_pbr_show_ipset_entry_walkcb
,
1009 return HASHWALK_CONTINUE
;
1012 size_t zebra_pbr_tcpflags_snprintf(char *buffer
, size_t len
,
1015 size_t len_written
= 0;
1016 static struct message nt
= {0};
1017 const struct message
*pnt
;
1020 for (pnt
= tcp_value_str
;
1021 memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
1022 if (pnt
->key
& tcp_val
) {
1023 len_written
+= snprintf(buffer
+ len_written
,
1034 void zebra_pbr_show_ipset_list(struct vty
*vty
, char *ipsetname
)
1036 struct zebra_pbr_ipset
*zpi
;
1037 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1038 struct zebra_pbr_ipset_entry_unique_display unique
;
1039 struct zebra_pbr_env_display uniqueipset
;
1042 zpi
= zebra_pbr_lookup_ipset_pername(ipsetname
);
1044 vty_out(vty
, "No IPset %s found\n", ipsetname
);
1047 vty_out(vty
, "IPset %s type %s\n", ipsetname
,
1048 zebra_pbr_ipset_type2str(zpi
->type
));
1053 hash_walk(zrouter
.ipset_entry_hash
,
1054 zebra_pbr_show_ipset_entry_walkcb
, &unique
);
1057 uniqueipset
.zns
= zns
;
1058 uniqueipset
.vty
= vty
;
1059 uniqueipset
.name
= NULL
;
1060 hash_walk(zrouter
.ipset_hash
, zebra_pbr_show_ipset_walkcb
,
1064 struct pbr_rule_fwmark_lookup
{
1065 struct zebra_pbr_rule
*ptr
;
1069 static int zebra_pbr_rule_lookup_fwmark_walkcb(struct hash_bucket
*bucket
,
1072 struct pbr_rule_fwmark_lookup
*iprule
=
1073 (struct pbr_rule_fwmark_lookup
*)arg
;
1074 struct zebra_pbr_rule
*zpr
= (struct zebra_pbr_rule
*)bucket
->data
;
1076 if (iprule
->fwmark
== zpr
->rule
.filter
.fwmark
) {
1078 return HASHWALK_ABORT
;
1080 return HASHWALK_CONTINUE
;
1083 static void zebra_pbr_show_iptable_unit(struct zebra_pbr_iptable
*iptable
,
1085 struct zebra_ns
*zns
)
1088 uint64_t pkts
= 0, bytes
= 0;
1090 vty_out(vty
, "IPtable %s action %s (%u)\n", iptable
->ipset_name
,
1091 iptable
->action
== ZEBRA_IPTABLES_DROP
? "drop" : "redirect",
1093 if (iptable
->type
== IPSET_NET_PORT
||
1094 iptable
->type
== IPSET_NET_PORT_NET
) {
1095 if (!(iptable
->filter_bm
& MATCH_ICMP_SET
)) {
1096 if (iptable
->filter_bm
& PBR_FILTER_DST_PORT
)
1097 vty_out(vty
, "\t lookup dst port\n");
1098 else if (iptable
->filter_bm
& PBR_FILTER_SRC_PORT
)
1099 vty_out(vty
, "\t lookup src port\n");
1102 if (iptable
->pkt_len_min
|| iptable
->pkt_len_max
) {
1103 if (!iptable
->pkt_len_max
)
1104 vty_out(vty
, "\t pkt len %u\n",
1105 iptable
->pkt_len_min
);
1107 vty_out(vty
, "\t pkt len [%u;%u]\n",
1108 iptable
->pkt_len_min
,
1109 iptable
->pkt_len_max
);
1111 if (iptable
->tcp_flags
|| iptable
->tcp_mask_flags
) {
1112 char tcp_flag_str
[64];
1113 char tcp_flag_mask_str
[64];
1115 zebra_pbr_tcpflags_snprintf(tcp_flag_str
,
1116 sizeof(tcp_flag_str
),
1117 iptable
->tcp_flags
);
1118 zebra_pbr_tcpflags_snprintf(tcp_flag_mask_str
,
1119 sizeof(tcp_flag_mask_str
),
1120 iptable
->tcp_mask_flags
);
1121 vty_out(vty
, "\t tcpflags [%s/%s]\n",
1122 tcp_flag_str
, tcp_flag_mask_str
);
1124 if (iptable
->filter_bm
& (MATCH_DSCP_SET
| MATCH_DSCP_INVERSE_SET
)) {
1125 vty_out(vty
, "\t dscp %s %d\n",
1126 iptable
->filter_bm
& MATCH_DSCP_INVERSE_SET
?
1127 "not" : "", iptable
->dscp_value
);
1129 if (iptable
->fragment
) {
1132 sprintf(val_str
, "%d", iptable
->fragment
);
1133 vty_out(vty
, "\t fragment%s %s\n",
1134 iptable
->filter_bm
& MATCH_FRAGMENT_INVERSE_SET
?
1135 " not" : "", lookup_msg(fragment_value_str
,
1136 iptable
->fragment
, val_str
));
1138 if (iptable
->protocol
) {
1139 vty_out(vty
, "\t protocol %d\n",
1142 ret
= hook_call(zebra_pbr_iptable_get_stat
, iptable
, &pkts
,
1144 if (ret
&& pkts
> 0)
1145 vty_out(vty
, "\t pkts %" PRIu64
", bytes %" PRIu64
"\n",
1147 if (iptable
->action
!= ZEBRA_IPTABLES_DROP
) {
1148 struct pbr_rule_fwmark_lookup prfl
;
1150 prfl
.fwmark
= iptable
->fwmark
;
1152 hash_walk(zrouter
.rules_hash
,
1153 &zebra_pbr_rule_lookup_fwmark_walkcb
, &prfl
);
1155 struct zebra_pbr_rule
*zpr
= prfl
.ptr
;
1157 vty_out(vty
, "\t table %u, fwmark %u\n",
1158 zpr
->rule
.action
.table
,
1164 static int zebra_pbr_show_iptable_walkcb(struct hash_bucket
*bucket
, void *arg
)
1166 struct zebra_pbr_iptable
*iptable
=
1167 (struct zebra_pbr_iptable
*)bucket
->data
;
1168 struct zebra_pbr_env_display
*env
= (struct zebra_pbr_env_display
*)arg
;
1169 struct vty
*vty
= env
->vty
;
1170 struct zebra_ns
*zns
= env
->zns
;
1171 char *iptable_name
= env
->name
;
1174 zebra_pbr_show_iptable_unit(iptable
, vty
, zns
);
1175 else if (!strncmp(iptable_name
,
1176 iptable
->ipset_name
,
1177 ZEBRA_IPSET_NAME_SIZE
))
1178 zebra_pbr_show_iptable_unit(iptable
, vty
, zns
);
1179 return HASHWALK_CONTINUE
;
1182 void zebra_pbr_show_iptable(struct vty
*vty
, char *iptable_name
)
1184 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1185 struct zebra_pbr_env_display env
;
1189 env
.name
= iptable_name
;
1190 hash_walk(zrouter
.iptable_hash
, zebra_pbr_show_iptable_walkcb
, &env
);
1193 void zebra_pbr_iptable_update_interfacelist(struct stream
*s
,
1194 struct zebra_pbr_iptable
*zpi
)
1196 uint32_t i
= 0, index
;
1197 struct interface
*ifp
;
1200 for (i
= 0; i
< zpi
->nb_interface
; i
++) {
1201 STREAM_GETL(s
, index
);
1202 ifp
= if_lookup_by_index(index
, zpi
->vrf_id
);
1205 name
= XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME
, ifp
->name
);
1206 listnode_add(zpi
->interface_name_list
, name
);