5 * FRR is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * FRR is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/printfrr.h"
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_pbr.h"
30 #include "bgpd/bgp_debug.h"
31 #include "bgpd/bgp_flowspec_util.h"
32 #include "bgpd/bgp_ecommunity.h"
33 #include "bgpd/bgp_route.h"
34 #include "bgpd/bgp_attr.h"
35 #include "bgpd/bgp_zebra.h"
36 #include "bgpd/bgp_mplsvpn.h"
37 #include "bgpd/bgp_flowspec_private.h"
38 #include "bgpd/bgp_errors.h"
40 DEFINE_MTYPE_STATIC(BGPD
, PBR_MATCH_ENTRY
, "PBR match entry");
41 DEFINE_MTYPE_STATIC(BGPD
, PBR_MATCH
, "PBR match");
42 DEFINE_MTYPE_STATIC(BGPD
, PBR_ACTION
, "PBR action");
43 DEFINE_MTYPE_STATIC(BGPD
, PBR_RULE
, "PBR rule");
44 DEFINE_MTYPE_STATIC(BGPD
, PBR
, "BGP PBR Context");
45 DEFINE_MTYPE_STATIC(BGPD
, PBR_VALMASK
, "BGP PBR Val Mask Value");
47 /* chain strings too long to fit in one line */
48 #define FSPEC_ACTION_EXCEED_LIMIT "flowspec actions exceeds limit"
49 #define IPV6_FRAGMENT_INVALID "fragment not valid for IPv6 for this implementation"
51 RB_GENERATE(bgp_pbr_interface_head
, bgp_pbr_interface
,
52 id_entry
, bgp_pbr_interface_compare
);
53 struct bgp_pbr_interface_head ifaces_by_name_ipv4
=
54 RB_INITIALIZER(&ifaces_by_name_ipv4
);
56 static int bgp_pbr_match_counter_unique
;
57 static int bgp_pbr_match_entry_counter_unique
;
58 static int bgp_pbr_action_counter_unique
;
59 static int bgp_pbr_match_iptable_counter_unique
;
61 struct bgp_pbr_match_iptable_unique
{
63 struct bgp_pbr_match
*bpm_found
;
66 struct bgp_pbr_match_entry_unique
{
68 struct bgp_pbr_match_entry
*bpme_found
;
71 struct bgp_pbr_action_unique
{
73 struct bgp_pbr_action
*bpa_found
;
76 struct bgp_pbr_rule_unique
{
78 struct bgp_pbr_rule
*bpr_found
;
81 static int bgp_pbr_rule_walkcb(struct hash_bucket
*bucket
, void *arg
)
83 struct bgp_pbr_rule
*bpr
= (struct bgp_pbr_rule
*)bucket
->data
;
84 struct bgp_pbr_rule_unique
*bpru
= (struct bgp_pbr_rule_unique
*)
86 uint32_t unique
= bpru
->unique
;
88 if (bpr
->unique
== unique
) {
89 bpru
->bpr_found
= bpr
;
90 return HASHWALK_ABORT
;
92 return HASHWALK_CONTINUE
;
95 static int bgp_pbr_action_walkcb(struct hash_bucket
*bucket
, void *arg
)
97 struct bgp_pbr_action
*bpa
= (struct bgp_pbr_action
*)bucket
->data
;
98 struct bgp_pbr_action_unique
*bpau
= (struct bgp_pbr_action_unique
*)
100 uint32_t unique
= bpau
->unique
;
102 if (bpa
->unique
== unique
) {
103 bpau
->bpa_found
= bpa
;
104 return HASHWALK_ABORT
;
106 return HASHWALK_CONTINUE
;
109 static int bgp_pbr_match_entry_walkcb(struct hash_bucket
*bucket
, void *arg
)
111 struct bgp_pbr_match_entry
*bpme
=
112 (struct bgp_pbr_match_entry
*)bucket
->data
;
113 struct bgp_pbr_match_entry_unique
*bpmeu
=
114 (struct bgp_pbr_match_entry_unique
*)arg
;
115 uint32_t unique
= bpmeu
->unique
;
117 if (bpme
->unique
== unique
) {
118 bpmeu
->bpme_found
= bpme
;
119 return HASHWALK_ABORT
;
121 return HASHWALK_CONTINUE
;
124 struct bgp_pbr_match_ipsetname
{
126 struct bgp_pbr_match
*bpm_found
;
129 static int bgp_pbr_match_pername_walkcb(struct hash_bucket
*bucket
, void *arg
)
131 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)bucket
->data
;
132 struct bgp_pbr_match_ipsetname
*bpmi
=
133 (struct bgp_pbr_match_ipsetname
*)arg
;
134 char *ipset_name
= bpmi
->ipsetname
;
136 if (!strncmp(ipset_name
, bpm
->ipset_name
,
137 ZEBRA_IPSET_NAME_SIZE
)) {
138 bpmi
->bpm_found
= bpm
;
139 return HASHWALK_ABORT
;
141 return HASHWALK_CONTINUE
;
144 static int bgp_pbr_match_iptable_walkcb(struct hash_bucket
*bucket
, void *arg
)
146 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)bucket
->data
;
147 struct bgp_pbr_match_iptable_unique
*bpmiu
=
148 (struct bgp_pbr_match_iptable_unique
*)arg
;
149 uint32_t unique
= bpmiu
->unique
;
151 if (bpm
->unique2
== unique
) {
152 bpmiu
->bpm_found
= bpm
;
153 return HASHWALK_ABORT
;
155 return HASHWALK_CONTINUE
;
158 struct bgp_pbr_match_unique
{
160 struct bgp_pbr_match
*bpm_found
;
163 static int bgp_pbr_match_walkcb(struct hash_bucket
*bucket
, void *arg
)
165 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)bucket
->data
;
166 struct bgp_pbr_match_unique
*bpmu
= (struct bgp_pbr_match_unique
*)
168 uint32_t unique
= bpmu
->unique
;
170 if (bpm
->unique
== unique
) {
171 bpmu
->bpm_found
= bpm
;
172 return HASHWALK_ABORT
;
174 return HASHWALK_CONTINUE
;
177 static int snprintf_bgp_pbr_match_val(char *str
, int len
,
178 struct bgp_pbr_match_val
*mval
,
185 delta
= snprintf(ptr
, len
, "%s", prepend
);
189 if (mval
->unary_operator
& OPERATOR_UNARY_OR
) {
190 delta
= snprintf(ptr
, len
, ", or ");
194 if (mval
->unary_operator
& OPERATOR_UNARY_AND
) {
195 delta
= snprintf(ptr
, len
, ", and ");
200 if (mval
->compare_operator
& OPERATOR_COMPARE_LESS_THAN
) {
201 delta
= snprintf(ptr
, len
, "<");
205 if (mval
->compare_operator
& OPERATOR_COMPARE_GREATER_THAN
) {
206 delta
= snprintf(ptr
, len
, ">");
210 if (mval
->compare_operator
& OPERATOR_COMPARE_EQUAL_TO
) {
211 delta
= snprintf(ptr
, len
, "=");
215 if (mval
->compare_operator
& OPERATOR_COMPARE_EXACT_MATCH
) {
216 delta
= snprintf(ptr
, len
, "match");
220 ptr
+= snprintf(ptr
, len
, " %u", mval
->value
);
221 return (int)(ptr
- str
);
224 #define INCREMENT_DISPLAY(_ptr, _cnt, _len) do { \
228 sn_delta = snprintf((_ptr), (_len), "; ");\
229 (_len) -= sn_delta; \
230 (_ptr) += sn_delta; \
235 /* this structure can be used for port range,
236 * but also for other values range like packet length range
238 struct bgp_pbr_range_port
{
243 /* this structure can be used to filter with a mask
244 * for instance it supports not instructions like for
247 struct bgp_pbr_val_mask
{
252 /* this structure is used to pass instructs
253 * so that BGP can create pbr instructions to ZEBRA
255 struct bgp_pbr_filter
{
261 uint8_t bitmask_iprule
;
263 struct bgp_pbr_range_port
*pkt_len
;
264 struct bgp_pbr_range_port
*src_port
;
265 struct bgp_pbr_range_port
*dst_port
;
266 struct bgp_pbr_val_mask
*tcp_flags
;
267 struct bgp_pbr_val_mask
*dscp
;
268 struct bgp_pbr_val_mask
*flow_label
;
269 struct bgp_pbr_val_mask
*pkt_len_val
;
270 struct bgp_pbr_val_mask
*fragment
;
273 /* this structure is used to contain OR instructions
274 * so that BGP can create multiple pbr instructions
277 struct bgp_pbr_or_filter
{
278 struct list
*tcpflags
;
280 struct list
*flowlabel
;
281 struct list
*pkt_len
;
282 struct list
*fragment
;
283 struct list
*icmp_type
;
284 struct list
*icmp_code
;
287 static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp
*bgp
,
288 struct bgp_path_info
*path
,
289 struct bgp_pbr_filter
*bpf
,
293 static void bgp_pbr_dump_entry(struct bgp_pbr_filter
*bpf
, bool add
);
295 static bool bgp_pbr_extract_enumerate_unary_opposite(
296 uint8_t unary_operator
,
297 struct bgp_pbr_val_mask
*and_valmask
,
298 struct list
*or_valmask
, uint32_t value
,
301 if (unary_operator
== OPERATOR_UNARY_AND
&& and_valmask
) {
302 if (type_entry
== FLOWSPEC_TCP_FLAGS
) {
304 TCP_HEADER_ALL_FLAGS
&
306 } else if (type_entry
== FLOWSPEC_DSCP
||
307 type_entry
== FLOWSPEC_FLOW_LABEL
||
308 type_entry
== FLOWSPEC_PKT_LEN
||
309 type_entry
== FLOWSPEC_FRAGMENT
) {
310 and_valmask
->val
= value
;
311 and_valmask
->mask
= 1; /* inverse */
313 } else if (unary_operator
== OPERATOR_UNARY_OR
&& or_valmask
) {
314 and_valmask
= XCALLOC(MTYPE_PBR_VALMASK
,
315 sizeof(struct bgp_pbr_val_mask
));
316 if (type_entry
== FLOWSPEC_TCP_FLAGS
) {
317 and_valmask
->val
= TCP_HEADER_ALL_FLAGS
;
319 TCP_HEADER_ALL_FLAGS
&
321 } else if (type_entry
== FLOWSPEC_DSCP
||
322 type_entry
== FLOWSPEC_FLOW_LABEL
||
323 type_entry
== FLOWSPEC_FRAGMENT
||
324 type_entry
== FLOWSPEC_PKT_LEN
) {
325 and_valmask
->val
= value
;
326 and_valmask
->mask
= 1; /* inverse */
328 listnode_add(or_valmask
, and_valmask
);
329 } else if (type_entry
== FLOWSPEC_ICMP_CODE
||
330 type_entry
== FLOWSPEC_ICMP_TYPE
)
335 /* TCP : FIN and SYN -> val = ALL; mask = 3
336 * TCP : not (FIN and SYN) -> val = ALL; mask = ALL & ~(FIN|RST)
337 * other variables type: dscp, pkt len, fragment, flow label
338 * - value is copied in bgp_pbr_val_mask->val value
339 * - if negate form is identifierd, bgp_pbr_val_mask->mask set to 1
341 static bool bgp_pbr_extract_enumerate_unary(struct bgp_pbr_match_val list
[],
342 int num
, uint8_t unary_operator
,
343 void *valmask
, uint8_t type_entry
)
346 struct bgp_pbr_val_mask
*and_valmask
= NULL
;
347 struct list
*or_valmask
= NULL
;
351 if (unary_operator
== OPERATOR_UNARY_AND
) {
352 and_valmask
= (struct bgp_pbr_val_mask
*)valmask
;
353 memset(and_valmask
, 0, sizeof(struct bgp_pbr_val_mask
));
354 } else if (unary_operator
== OPERATOR_UNARY_OR
) {
355 or_valmask
= (struct list
*)valmask
;
358 for (i
= 0; i
< num
; i
++) {
359 if (i
!= 0 && list
[i
].unary_operator
!=
362 if (!(list
[i
].compare_operator
&
363 OPERATOR_COMPARE_EQUAL_TO
) &&
364 !(list
[i
].compare_operator
&
365 OPERATOR_COMPARE_EXACT_MATCH
)) {
366 if ((list
[i
].compare_operator
&
367 OPERATOR_COMPARE_LESS_THAN
) &&
368 (list
[i
].compare_operator
&
369 OPERATOR_COMPARE_GREATER_THAN
)) {
370 ret
= bgp_pbr_extract_enumerate_unary_opposite(
371 unary_operator
, and_valmask
,
372 or_valmask
, list
[i
].value
,
380 if (unary_operator
== OPERATOR_UNARY_AND
&& and_valmask
) {
381 if (type_entry
== FLOWSPEC_TCP_FLAGS
)
383 TCP_HEADER_ALL_FLAGS
& list
[i
].value
;
384 } else if (unary_operator
== OPERATOR_UNARY_OR
&& or_valmask
) {
385 and_valmask
= XCALLOC(MTYPE_PBR_VALMASK
,
386 sizeof(struct bgp_pbr_val_mask
));
387 if (type_entry
== FLOWSPEC_TCP_FLAGS
) {
388 and_valmask
->val
= TCP_HEADER_ALL_FLAGS
;
390 TCP_HEADER_ALL_FLAGS
& list
[i
].value
;
391 } else if (type_entry
== FLOWSPEC_DSCP
||
392 type_entry
== FLOWSPEC_FLOW_LABEL
||
393 type_entry
== FLOWSPEC_ICMP_TYPE
||
394 type_entry
== FLOWSPEC_ICMP_CODE
||
395 type_entry
== FLOWSPEC_FRAGMENT
||
396 type_entry
== FLOWSPEC_PKT_LEN
)
397 and_valmask
->val
= list
[i
].value
;
398 listnode_add(or_valmask
, and_valmask
);
401 if (unary_operator
== OPERATOR_UNARY_AND
&& and_valmask
402 && type_entry
== FLOWSPEC_TCP_FLAGS
)
403 and_valmask
->val
= TCP_HEADER_ALL_FLAGS
;
407 /* if unary operator can either be UNARY_OR/AND/OR-AND.
408 * in the latter case, combinationf of both is not handled
410 static bool bgp_pbr_extract_enumerate(struct bgp_pbr_match_val list
[],
411 int num
, uint8_t unary_operator
,
412 void *valmask
, uint8_t type_entry
)
415 uint8_t unary_operator_val
;
416 bool double_check
= false;
418 if ((unary_operator
& OPERATOR_UNARY_OR
) &&
419 (unary_operator
& OPERATOR_UNARY_AND
)) {
420 unary_operator_val
= OPERATOR_UNARY_AND
;
423 unary_operator_val
= unary_operator
;
424 ret
= bgp_pbr_extract_enumerate_unary(list
, num
, unary_operator_val
,
425 valmask
, type_entry
);
426 if (!ret
&& double_check
)
427 ret
= bgp_pbr_extract_enumerate_unary(list
, num
,
434 /* returns the unary operator that is in the list
435 * return 0 if both operators are used
437 static uint8_t bgp_pbr_match_val_get_operator(struct bgp_pbr_match_val list
[],
442 uint8_t unary_operator
= OPERATOR_UNARY_AND
;
444 for (i
= 0; i
< num
; i
++) {
447 if (list
[i
].unary_operator
& OPERATOR_UNARY_OR
)
448 unary_operator
= OPERATOR_UNARY_OR
;
449 if ((list
[i
].unary_operator
& OPERATOR_UNARY_AND
450 && unary_operator
== OPERATOR_UNARY_OR
) ||
451 (list
[i
].unary_operator
& OPERATOR_UNARY_OR
452 && unary_operator
== OPERATOR_UNARY_AND
))
455 return unary_operator
;
459 /* return true if extraction ok
461 static bool bgp_pbr_extract(struct bgp_pbr_match_val list
[],
463 struct bgp_pbr_range_port
*range
)
466 bool exact_match
= false;
469 memset(range
, 0, sizeof(struct bgp_pbr_range_port
));
473 for (i
= 0; i
< num
; i
++) {
474 if (i
!= 0 && (list
[i
].compare_operator
==
475 OPERATOR_COMPARE_EQUAL_TO
))
477 if (i
== 0 && (list
[i
].compare_operator
==
478 OPERATOR_COMPARE_EQUAL_TO
)) {
480 range
->min_port
= list
[i
].value
;
483 if (exact_match
&& i
> 0)
485 if (list
[i
].compare_operator
==
486 (OPERATOR_COMPARE_GREATER_THAN
+
487 OPERATOR_COMPARE_EQUAL_TO
)) {
489 range
->min_port
= list
[i
].value
;
490 } else if (list
[i
].compare_operator
==
491 (OPERATOR_COMPARE_LESS_THAN
+
492 OPERATOR_COMPARE_EQUAL_TO
)) {
494 range
->max_port
= list
[i
].value
;
495 } else if (list
[i
].compare_operator
==
496 OPERATOR_COMPARE_LESS_THAN
) {
498 range
->max_port
= list
[i
].value
- 1;
499 } else if (list
[i
].compare_operator
==
500 OPERATOR_COMPARE_GREATER_THAN
) {
502 range
->min_port
= list
[i
].value
+ 1;
508 static int bgp_pbr_validate_policy_route(struct bgp_pbr_entry_main
*api
)
510 bool enumerate_icmp
= false;
512 if (api
->type
== BGP_PBR_UNDEFINED
) {
513 if (BGP_DEBUG(pbr
, PBR
))
514 zlog_debug("BGP: pbr entry undefined. cancel.");
517 /* because bgp pbr entry may contain unsupported
518 * combinations, a message will be displayed here if
520 * for now, only match/set supported is
521 * - combination src/dst => redirect nexthop [ + rate]
522 * - combination src/dst => redirect VRF [ + rate]
523 * - combination src/dst => drop
524 * - combination srcport + @IP
526 if (api
->match_protocol_num
> 1) {
527 if (BGP_DEBUG(pbr
, PBR
))
528 zlog_debug("BGP: match protocol operations:multiple protocols ( %d). ignoring.",
529 api
->match_protocol_num
);
532 if (api
->src_prefix_offset
> 0 ||
533 api
->dst_prefix_offset
> 0) {
534 if (BGP_DEBUG(pbr
, PBR
))
535 zlog_debug("BGP: match prefix offset:"
536 "implementation does not support it.");
539 if (api
->match_protocol_num
== 1 &&
540 api
->protocol
[0].value
!= PROTOCOL_UDP
&&
541 api
->protocol
[0].value
!= PROTOCOL_ICMP
&&
542 api
->protocol
[0].value
!= PROTOCOL_ICMPV6
&&
543 api
->protocol
[0].value
!= PROTOCOL_TCP
) {
544 if (BGP_DEBUG(pbr
, PBR
))
545 zlog_debug("BGP: match protocol operations:protocol (%d) not supported. ignoring",
546 api
->match_protocol_num
);
549 if (!bgp_pbr_extract(api
->src_port
, api
->match_src_port_num
, NULL
)) {
550 if (BGP_DEBUG(pbr
, PBR
))
551 zlog_debug("BGP: match src port operations:too complex. ignoring.");
554 if (!bgp_pbr_extract(api
->dst_port
, api
->match_dst_port_num
, NULL
)) {
555 if (BGP_DEBUG(pbr
, PBR
))
556 zlog_debug("BGP: match dst port operations:too complex. ignoring.");
559 if (!bgp_pbr_extract_enumerate(api
->tcpflags
,
560 api
->match_tcpflags_num
,
562 OPERATOR_UNARY_OR
, NULL
,
563 FLOWSPEC_TCP_FLAGS
)) {
564 if (BGP_DEBUG(pbr
, PBR
))
565 zlog_debug("BGP: match tcp flags:too complex. ignoring.");
568 if (!bgp_pbr_extract(api
->icmp_type
, api
->match_icmp_type_num
, NULL
)) {
569 if (!bgp_pbr_extract_enumerate(api
->icmp_type
,
570 api
->match_icmp_type_num
,
571 OPERATOR_UNARY_OR
, NULL
,
572 FLOWSPEC_ICMP_TYPE
)) {
573 if (BGP_DEBUG(pbr
, PBR
))
574 zlog_debug("BGP: match icmp type operations:too complex. ignoring.");
577 enumerate_icmp
= true;
579 if (!bgp_pbr_extract(api
->icmp_code
, api
->match_icmp_code_num
, NULL
)) {
580 if (!bgp_pbr_extract_enumerate(api
->icmp_code
,
581 api
->match_icmp_code_num
,
582 OPERATOR_UNARY_OR
, NULL
,
583 FLOWSPEC_ICMP_CODE
)) {
584 if (BGP_DEBUG(pbr
, PBR
))
585 zlog_debug("BGP: match icmp code operations:too complex. ignoring.");
587 } else if (api
->match_icmp_type_num
> 1 &&
589 if (BGP_DEBUG(pbr
, PBR
))
590 zlog_debug("BGP: match icmp code is enumerate, and icmp type is not. too complex. ignoring.");
594 if (!bgp_pbr_extract(api
->port
, api
->match_port_num
, NULL
)) {
595 if (BGP_DEBUG(pbr
, PBR
))
596 zlog_debug("BGP: match port operations:too complex. ignoring.");
599 if (api
->match_packet_length_num
) {
602 ret
= bgp_pbr_extract(api
->packet_length
,
603 api
->match_packet_length_num
, NULL
);
605 ret
= bgp_pbr_extract_enumerate(api
->packet_length
,
606 api
->match_packet_length_num
,
608 | OPERATOR_UNARY_AND
,
609 NULL
, FLOWSPEC_PKT_LEN
);
611 if (BGP_DEBUG(pbr
, PBR
))
612 zlog_debug("BGP: match packet length operations:too complex. ignoring.");
616 if (api
->match_dscp_num
) {
617 if (!bgp_pbr_extract_enumerate(api
->dscp
, api
->match_dscp_num
,
618 OPERATOR_UNARY_OR
| OPERATOR_UNARY_AND
,
619 NULL
, FLOWSPEC_DSCP
)) {
620 if (BGP_DEBUG(pbr
, PBR
))
621 zlog_debug("BGP: match DSCP operations:too complex. ignoring.");
625 if (api
->match_flowlabel_num
) {
626 if (api
->afi
== AFI_IP
) {
627 if (BGP_DEBUG(pbr
, PBR
))
628 zlog_debug("BGP: match Flow Label operations:"
632 if (!bgp_pbr_extract_enumerate(api
->flow_label
,
633 api
->match_flowlabel_num
,
634 OPERATOR_UNARY_OR
| OPERATOR_UNARY_AND
,
635 NULL
, FLOWSPEC_FLOW_LABEL
)) {
636 if (BGP_DEBUG(pbr
, PBR
))
637 zlog_debug("BGP: match FlowLabel operations:"
638 "too complex. ignoring.");
641 if (BGP_DEBUG(pbr
, PBR
))
642 zlog_debug("BGP: match FlowLabel operations "
643 "not supported. ignoring.");
646 if (api
->match_fragment_num
) {
650 success
= bgp_pbr_extract_enumerate(api
->fragment
,
651 api
->match_fragment_num
,
653 | OPERATOR_UNARY_AND
,
654 NULL
, FLOWSPEC_FRAGMENT
);
658 for (i
= 0; i
< api
->match_fragment_num
; i
++) {
659 if (api
->fragment
[i
].value
!= 1 &&
660 api
->fragment
[i
].value
!= 2 &&
661 api
->fragment
[i
].value
!= 4 &&
662 api
->fragment
[i
].value
!= 8) {
665 fail_str
, sizeof(fail_str
),
666 "Value not valid (%d) for this implementation",
667 api
->fragment
[i
].value
);
669 if (api
->afi
== AFI_IP6
&&
670 api
->fragment
[i
].value
== 1) {
672 snprintf(fail_str
, sizeof(fail_str
),
673 "IPv6 dont fragment match invalid (%d)",
674 api
->fragment
[i
].value
);
677 if (api
->afi
== AFI_IP6
) {
679 snprintf(fail_str
, sizeof(fail_str
),
680 "%s", IPV6_FRAGMENT_INVALID
);
683 snprintf(fail_str
, sizeof(fail_str
),
684 "too complex. ignoring");
686 if (BGP_DEBUG(pbr
, PBR
))
687 zlog_debug("BGP: match fragment operation (%d) %s",
688 api
->match_fragment_num
,
694 /* no combinations with both src_port and dst_port
695 * or port with src_port and dst_port
697 if (api
->match_src_port_num
+ api
->match_dst_port_num
+
698 api
->match_port_num
> 3) {
699 if (BGP_DEBUG(pbr
, PBR
))
700 zlog_debug("BGP: match multiple port operations: too complex. ignoring.");
703 if ((api
->match_src_port_num
|| api
->match_dst_port_num
704 || api
->match_port_num
) && (api
->match_icmp_type_num
705 || api
->match_icmp_code_num
)) {
706 if (BGP_DEBUG(pbr
, PBR
))
707 zlog_debug("BGP: match multiple port/imcp operations: too complex. ignoring.");
710 /* iprule only supports redirect IP */
711 if (api
->type
== BGP_PBR_IPRULE
) {
714 for (i
= 0; i
< api
->action_num
; i
++) {
715 if (api
->actions
[i
].action
== ACTION_TRAFFICRATE
&&
716 api
->actions
[i
].u
.r
.rate
== 0) {
717 if (BGP_DEBUG(pbr
, PBR
)) {
718 bgp_pbr_print_policy_route(api
);
719 zlog_debug("BGP: iprule match actions drop not supported");
723 if (api
->actions
[i
].action
== ACTION_MARKING
) {
724 if (BGP_DEBUG(pbr
, PBR
)) {
725 bgp_pbr_print_policy_route(api
);
726 zlog_warn("PBR: iprule set DSCP/Flow Label %u not supported",
727 api
->actions
[i
].u
.marking_dscp
);
730 if (api
->actions
[i
].action
== ACTION_REDIRECT
) {
731 if (BGP_DEBUG(pbr
, PBR
)) {
732 bgp_pbr_print_policy_route(api
);
733 zlog_warn("PBR: iprule redirect VRF %u not supported",
734 api
->actions
[i
].u
.redirect_vrf
);
739 } else if (!(api
->match_bitmask
& PREFIX_SRC_PRESENT
) &&
740 !(api
->match_bitmask
& PREFIX_DST_PRESENT
)) {
741 if (BGP_DEBUG(pbr
, PBR
)) {
742 bgp_pbr_print_policy_route(api
);
743 zlog_debug("BGP: match actions without src or dst address can not operate. ignoring.");
750 /* return -1 if build or validation failed */
752 int bgp_pbr_build_and_validate_entry(const struct prefix
*p
,
753 struct bgp_path_info
*path
,
754 struct bgp_pbr_entry_main
*api
)
757 uint32_t i
, action_count
= 0;
758 struct ecommunity
*ecom
;
759 struct ecommunity_val
*ecom_eval
;
760 struct bgp_pbr_entry_action
*api_action
;
761 struct prefix
*src
= NULL
, *dst
= NULL
;
762 int valid_prefix
= 0;
763 struct bgp_pbr_entry_action
*api_action_redirect_ip
= NULL
;
764 bool discard_action_found
= false;
765 afi_t afi
= family2afi(p
->u
.prefix_flowspec
.family
);
767 /* extract match from flowspec entries */
768 ret
= bgp_flowspec_match_rules_fill((uint8_t *)p
->u
.prefix_flowspec
.ptr
,
769 p
->u
.prefix_flowspec
.prefixlen
, api
, afi
);
772 /* extract actiosn from flowspec ecom list */
773 if (path
&& bgp_attr_get_ecommunity(path
->attr
)) {
774 ecom
= bgp_attr_get_ecommunity(path
->attr
);
775 for (i
= 0; i
< ecom
->size
; i
++) {
776 ecom_eval
= (struct ecommunity_val
*)
777 (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
779 if (action_count
> ACTIONS_MAX_NUM
) {
780 if (BGP_DEBUG(pbr
, PBR_ERROR
))
782 EC_BGP_FLOWSPEC_PACKET
,
785 FSPEC_ACTION_EXCEED_LIMIT
,
789 api_action
= &api
->actions
[action_count
- 1];
791 if ((ecom_eval
->val
[1] ==
792 (char)ECOMMUNITY_REDIRECT_VRF
) &&
793 (ecom_eval
->val
[0] ==
794 (char)ECOMMUNITY_ENCODE_TRANS_EXP
||
796 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_2
||
798 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_3
)) {
799 struct ecommunity
*eckey
= ecommunity_new();
800 struct ecommunity_val ecom_copy
;
802 memcpy(&ecom_copy
, ecom_eval
,
803 sizeof(struct ecommunity_val
));
805 ~ECOMMUNITY_ENCODE_TRANS_EXP
;
806 ecom_copy
.val
[1] = ECOMMUNITY_ROUTE_TARGET
;
807 ecommunity_add_val(eckey
, &ecom_copy
,
810 api_action
->action
= ACTION_REDIRECT
;
811 api_action
->u
.redirect_vrf
=
812 get_first_vrf_for_redirect_with_rt(
814 ecommunity_free(&eckey
);
815 } else if ((ecom_eval
->val
[0] ==
816 (char)ECOMMUNITY_ENCODE_REDIRECT_IP_NH
) &&
817 (ecom_eval
->val
[1] ==
818 (char)ECOMMUNITY_REDIRECT_IP_NH
)) {
819 /* in case the 2 ecom present,
821 * draft-ietf-idr-flowspec-redirect
823 if (api_action_redirect_ip
&&
824 p
->u
.prefix_flowspec
.family
== AF_INET
) {
825 if (api_action_redirect_ip
->u
826 .zr
.redirect_ip_v4
.s_addr
829 if (path
->attr
->nexthop
.s_addr
832 api_action_redirect_ip
->u
.zr
833 .redirect_ip_v4
.s_addr
=
834 path
->attr
->nexthop
.s_addr
;
835 api_action_redirect_ip
->u
.zr
.duplicate
838 } else if (api_action_redirect_ip
&&
839 p
->u
.prefix_flowspec
.family
== AF_INET6
) {
840 if (memcmp(&api_action_redirect_ip
->u
843 sizeof(struct in6_addr
)))
845 if (path
->attr
->mp_nexthop_len
== 0 ||
846 path
->attr
->mp_nexthop_len
==
847 BGP_ATTR_NHLEN_IPV4
||
848 path
->attr
->mp_nexthop_len
==
849 BGP_ATTR_NHLEN_VPNV4
)
851 memcpy(&api_action_redirect_ip
->u
853 &path
->attr
->mp_nexthop_global
,
854 sizeof(struct in6_addr
));
855 api_action_redirect_ip
->u
.zr
.duplicate
858 } else if (p
->u
.prefix_flowspec
.family
==
860 api_action
->action
= ACTION_REDIRECT_IP
;
861 api_action
->u
.zr
.redirect_ip_v4
.s_addr
=
862 path
->attr
->nexthop
.s_addr
;
863 api_action
->u
.zr
.duplicate
=
865 api_action_redirect_ip
= api_action
;
866 } else if (p
->u
.prefix_flowspec
.family
==
868 api_action
->action
= ACTION_REDIRECT_IP
;
869 memcpy(&api_action
->u
871 &path
->attr
->mp_nexthop_global
,
872 sizeof(struct in6_addr
));
873 api_action
->u
.zr
.duplicate
875 api_action_redirect_ip
= api_action
;
877 } else if ((ecom_eval
->val
[0] ==
878 (char)ECOMMUNITY_ENCODE_IP
) &&
879 (ecom_eval
->val
[1] ==
880 (char)ECOMMUNITY_FLOWSPEC_REDIRECT_IPV4
)) {
881 /* in case the 2 ecom present,
882 * overwrite simpson draft
883 * update redirect ip fields
885 if (api_action_redirect_ip
) {
886 memcpy(&(api_action_redirect_ip
->u
887 .zr
.redirect_ip_v4
.s_addr
),
888 (ecom_eval
->val
+2), 4);
889 api_action_redirect_ip
->u
894 api_action
->action
= ACTION_REDIRECT_IP
;
895 memcpy(&(api_action
->u
896 .zr
.redirect_ip_v4
.s_addr
),
897 (ecom_eval
->val
+2), 4);
898 api_action
->u
.zr
.duplicate
=
900 api_action_redirect_ip
= api_action
;
903 if (ecom_eval
->val
[0] !=
904 (char)ECOMMUNITY_ENCODE_TRANS_EXP
)
906 ret
= ecommunity_fill_pbr_action(ecom_eval
,
911 if ((api_action
->action
== ACTION_TRAFFICRATE
)
912 && api
->actions
[i
].u
.r
.rate
== 0)
913 discard_action_found
= true;
918 if (path
&& path
->attr
&& bgp_attr_get_ipv6_ecommunity(path
->attr
)) {
919 struct ecommunity_val_ipv6
*ipv6_ecom_eval
;
921 ecom
= bgp_attr_get_ipv6_ecommunity(path
->attr
);
922 for (i
= 0; i
< ecom
->size
; i
++) {
923 ipv6_ecom_eval
= (struct ecommunity_val_ipv6
*)
924 (ecom
->val
+ (i
* ecom
->unit_size
));
926 if (action_count
> ACTIONS_MAX_NUM
) {
927 if (BGP_DEBUG(pbr
, PBR_ERROR
))
929 EC_BGP_FLOWSPEC_PACKET
,
930 "%s: flowspec actions exceeds limit (max %u)",
931 __func__
, action_count
);
934 api_action
= &api
->actions
[action_count
- 1];
935 if ((ipv6_ecom_eval
->val
[1] ==
936 (char)ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6
) &&
937 (ipv6_ecom_eval
->val
[0] ==
938 (char)ECOMMUNITY_ENCODE_TRANS_EXP
)) {
939 struct ecommunity
*eckey
= ecommunity_new();
940 struct ecommunity_val_ipv6 ecom_copy
;
942 eckey
->unit_size
= IPV6_ECOMMUNITY_SIZE
;
943 memcpy(&ecom_copy
, ipv6_ecom_eval
,
944 sizeof(struct ecommunity_val_ipv6
));
945 ecom_copy
.val
[1] = ECOMMUNITY_ROUTE_TARGET
;
946 ecommunity_add_val_ipv6(eckey
, &ecom_copy
,
948 api_action
->action
= ACTION_REDIRECT
;
949 api_action
->u
.redirect_vrf
=
950 get_first_vrf_for_redirect_with_rt(
952 ecommunity_free(&eckey
);
957 /* if ECOMMUNITY_TRAFFIC_RATE = 0 as action
958 * then reduce the API action list to that action
960 if (api
->action_num
> 1 && discard_action_found
) {
962 memset(&api
->actions
[0], 0,
963 sizeof(struct bgp_pbr_entry_action
));
964 api
->actions
[0].action
= ACTION_TRAFFICRATE
;
967 /* validate if incoming matc/action is compatible
968 * with our policy routing engine
970 if (!bgp_pbr_validate_policy_route(api
))
973 /* check inconsistency in the match rule */
974 if (api
->match_bitmask
& PREFIX_SRC_PRESENT
) {
975 src
= &api
->src_prefix
;
976 afi
= family2afi(src
->family
);
979 if (api
->match_bitmask
& PREFIX_DST_PRESENT
) {
980 dst
= &api
->dst_prefix
;
981 if (valid_prefix
&& afi
!= family2afi(dst
->family
)) {
982 if (BGP_DEBUG(pbr
, PBR
)) {
983 bgp_pbr_print_policy_route(api
);
984 zlog_debug("%s: inconsistency: no match for afi src and dst (%u/%u)",
985 __func__
, afi
, family2afi(dst
->family
));
993 static void bgp_pbr_match_entry_free(void *arg
)
995 struct bgp_pbr_match_entry
*bpme
;
997 bpme
= (struct bgp_pbr_match_entry
*)arg
;
999 if (bpme
->installed
) {
1000 bgp_send_pbr_ipset_entry_match(bpme
, false);
1001 bpme
->installed
= false;
1002 bpme
->backpointer
= NULL
;
1004 XFREE(MTYPE_PBR_MATCH_ENTRY
, bpme
);
1007 static void bgp_pbr_match_free(void *arg
)
1009 struct bgp_pbr_match
*bpm
;
1011 bpm
= (struct bgp_pbr_match
*)arg
;
1013 hash_clean(bpm
->entry_hash
, bgp_pbr_match_entry_free
);
1015 if (hashcount(bpm
->entry_hash
) == 0) {
1016 /* delete iptable entry first */
1017 /* then delete ipset match */
1018 if (bpm
->installed
) {
1019 if (bpm
->installed_in_iptable
) {
1020 bgp_send_pbr_iptable(bpm
->action
,
1022 bpm
->installed_in_iptable
= false;
1023 bpm
->action
->refcnt
--;
1025 bgp_send_pbr_ipset_match(bpm
, false);
1026 bpm
->installed
= false;
1030 hash_free(bpm
->entry_hash
);
1032 XFREE(MTYPE_PBR_MATCH
, bpm
);
1035 static void *bgp_pbr_match_alloc_intern(void *arg
)
1037 struct bgp_pbr_match
*bpm
, *new;
1039 bpm
= (struct bgp_pbr_match
*)arg
;
1041 new = XCALLOC(MTYPE_PBR_MATCH
, sizeof(*new));
1042 memcpy(new, bpm
, sizeof(*bpm
));
1047 static void bgp_pbr_rule_free(void *arg
)
1049 struct bgp_pbr_rule
*bpr
;
1051 bpr
= (struct bgp_pbr_rule
*)arg
;
1054 if (bpr
->installed
) {
1055 bgp_send_pbr_rule_action(bpr
->action
, bpr
, false);
1056 bpr
->installed
= false;
1057 bpr
->action
->refcnt
--;
1060 XFREE(MTYPE_PBR_RULE
, bpr
);
1063 static void *bgp_pbr_rule_alloc_intern(void *arg
)
1065 struct bgp_pbr_rule
*bpr
, *new;
1067 bpr
= (struct bgp_pbr_rule
*)arg
;
1069 new = XCALLOC(MTYPE_PBR_RULE
, sizeof(*new));
1070 memcpy(new, bpr
, sizeof(*bpr
));
1075 static void bgp_pbr_bpa_remove(struct bgp_pbr_action
*bpa
)
1077 if ((bpa
->refcnt
== 0) && bpa
->installed
&& bpa
->table_id
!= 0) {
1078 bgp_send_pbr_rule_action(bpa
, NULL
, false);
1079 bgp_zebra_announce_default(bpa
->bgp
, &bpa
->nh
, bpa
->afi
,
1080 bpa
->table_id
, false);
1081 bpa
->installed
= false;
1085 static void bgp_pbr_bpa_add(struct bgp_pbr_action
*bpa
)
1087 if (!bpa
->installed
&& !bpa
->install_in_progress
) {
1088 bgp_send_pbr_rule_action(bpa
, NULL
, true);
1089 bgp_zebra_announce_default(bpa
->bgp
, &bpa
->nh
, bpa
->afi
,
1090 bpa
->table_id
, true);
1094 static void bgp_pbr_action_free(void *arg
)
1096 struct bgp_pbr_action
*bpa
= arg
;
1098 bgp_pbr_bpa_remove(bpa
);
1100 XFREE(MTYPE_PBR_ACTION
, bpa
);
1103 static void *bgp_pbr_action_alloc_intern(void *arg
)
1105 struct bgp_pbr_action
*bpa
, *new;
1107 bpa
= (struct bgp_pbr_action
*)arg
;
1109 new = XCALLOC(MTYPE_PBR_ACTION
, sizeof(*new));
1111 memcpy(new, bpa
, sizeof(*bpa
));
1116 static void *bgp_pbr_match_entry_alloc_intern(void *arg
)
1118 struct bgp_pbr_match_entry
*bpme
, *new;
1120 bpme
= (struct bgp_pbr_match_entry
*)arg
;
1122 new = XCALLOC(MTYPE_PBR_MATCH_ENTRY
, sizeof(*new));
1124 memcpy(new, bpme
, sizeof(*bpme
));
1129 uint32_t bgp_pbr_match_hash_key(const void *arg
)
1131 const struct bgp_pbr_match
*pbm
= arg
;
1134 key
= jhash_1word(pbm
->vrf_id
, 0x4312abde);
1135 key
= jhash_1word(pbm
->flags
, key
);
1136 key
= jhash_1word(pbm
->family
, key
);
1137 key
= jhash(&pbm
->pkt_len_min
, 2, key
);
1138 key
= jhash(&pbm
->pkt_len_max
, 2, key
);
1139 key
= jhash(&pbm
->tcp_flags
, 2, key
);
1140 key
= jhash(&pbm
->tcp_mask_flags
, 2, key
);
1141 key
= jhash(&pbm
->dscp_value
, 1, key
);
1142 key
= jhash(&pbm
->flow_label
, 2, key
);
1143 key
= jhash(&pbm
->fragment
, 1, key
);
1144 key
= jhash(&pbm
->protocol
, 1, key
);
1145 return jhash_1word(pbm
->type
, key
);
1148 bool bgp_pbr_match_hash_equal(const void *arg1
, const void *arg2
)
1150 const struct bgp_pbr_match
*r1
, *r2
;
1152 r1
= (const struct bgp_pbr_match
*)arg1
;
1153 r2
= (const struct bgp_pbr_match
*)arg2
;
1155 if (r1
->vrf_id
!= r2
->vrf_id
)
1158 if (r1
->family
!= r2
->family
)
1161 if (r1
->type
!= r2
->type
)
1164 if (r1
->flags
!= r2
->flags
)
1167 if (r1
->action
!= r2
->action
)
1170 if (r1
->pkt_len_min
!= r2
->pkt_len_min
)
1173 if (r1
->pkt_len_max
!= r2
->pkt_len_max
)
1176 if (r1
->tcp_flags
!= r2
->tcp_flags
)
1179 if (r1
->tcp_mask_flags
!= r2
->tcp_mask_flags
)
1182 if (r1
->dscp_value
!= r2
->dscp_value
)
1185 if (r1
->flow_label
!= r2
->flow_label
)
1188 if (r1
->fragment
!= r2
->fragment
)
1191 if (r1
->protocol
!= r2
->protocol
)
1196 uint32_t bgp_pbr_rule_hash_key(const void *arg
)
1198 const struct bgp_pbr_rule
*pbr
= arg
;
1201 key
= prefix_hash_key(&pbr
->src
);
1202 key
= jhash_1word(pbr
->vrf_id
, key
);
1203 key
= jhash_1word(pbr
->flags
, key
);
1204 return jhash_1word(prefix_hash_key(&pbr
->dst
), key
);
1207 bool bgp_pbr_rule_hash_equal(const void *arg1
, const void *arg2
)
1209 const struct bgp_pbr_rule
*r1
, *r2
;
1211 r1
= (const struct bgp_pbr_rule
*)arg1
;
1212 r2
= (const struct bgp_pbr_rule
*)arg2
;
1214 if (r1
->vrf_id
!= r2
->vrf_id
)
1217 if (r1
->flags
!= r2
->flags
)
1220 if (r1
->action
!= r2
->action
)
1223 if ((r1
->flags
& MATCH_IP_SRC_SET
) &&
1224 !prefix_same(&r1
->src
, &r2
->src
))
1227 if ((r1
->flags
& MATCH_IP_DST_SET
) &&
1228 !prefix_same(&r1
->dst
, &r2
->dst
))
1234 uint32_t bgp_pbr_match_entry_hash_key(const void *arg
)
1236 const struct bgp_pbr_match_entry
*pbme
;
1240 key
= prefix_hash_key(&pbme
->src
);
1241 key
= jhash_1word(prefix_hash_key(&pbme
->dst
), key
);
1242 key
= jhash(&pbme
->dst_port_min
, 2, key
);
1243 key
= jhash(&pbme
->src_port_min
, 2, key
);
1244 key
= jhash(&pbme
->dst_port_max
, 2, key
);
1245 key
= jhash(&pbme
->src_port_max
, 2, key
);
1246 key
= jhash(&pbme
->proto
, 1, key
);
1251 bool bgp_pbr_match_entry_hash_equal(const void *arg1
, const void *arg2
)
1253 const struct bgp_pbr_match_entry
*r1
, *r2
;
1255 r1
= (const struct bgp_pbr_match_entry
*)arg1
;
1256 r2
= (const struct bgp_pbr_match_entry
*)arg2
;
1259 * on updates, comparing backpointer is not necessary
1260 * unique value is self calculated
1261 * rate is ignored for now
1264 if (!prefix_same(&r1
->src
, &r2
->src
))
1267 if (!prefix_same(&r1
->dst
, &r2
->dst
))
1270 if (r1
->src_port_min
!= r2
->src_port_min
)
1273 if (r1
->dst_port_min
!= r2
->dst_port_min
)
1276 if (r1
->src_port_max
!= r2
->src_port_max
)
1279 if (r1
->dst_port_max
!= r2
->dst_port_max
)
1282 if (r1
->proto
!= r2
->proto
)
1288 uint32_t bgp_pbr_action_hash_key(const void *arg
)
1290 const struct bgp_pbr_action
*pbra
;
1294 key
= jhash_1word(pbra
->table_id
, 0x4312abde);
1295 key
= jhash_1word(pbra
->fwmark
, key
);
1296 key
= jhash_1word(pbra
->afi
, key
);
1300 bool bgp_pbr_action_hash_equal(const void *arg1
, const void *arg2
)
1302 const struct bgp_pbr_action
*r1
, *r2
;
1304 r1
= (const struct bgp_pbr_action
*)arg1
;
1305 r2
= (const struct bgp_pbr_action
*)arg2
;
1307 /* unique value is self calculated
1308 * table and fwmark is self calculated
1311 if (r1
->vrf_id
!= r2
->vrf_id
)
1314 if (r1
->afi
!= r2
->afi
)
1317 return nexthop_same(&r1
->nh
, &r2
->nh
);
1320 struct bgp_pbr_rule
*bgp_pbr_rule_lookup(vrf_id_t vrf_id
,
1323 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1324 struct bgp_pbr_rule_unique bpru
;
1326 if (!bgp
|| unique
== 0)
1328 bpru
.unique
= unique
;
1329 bpru
.bpr_found
= NULL
;
1330 hash_walk(bgp
->pbr_rule_hash
, bgp_pbr_rule_walkcb
, &bpru
);
1331 return bpru
.bpr_found
;
1334 struct bgp_pbr_action
*bgp_pbr_action_rule_lookup(vrf_id_t vrf_id
,
1337 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1338 struct bgp_pbr_action_unique bpau
;
1340 if (!bgp
|| unique
== 0)
1342 bpau
.unique
= unique
;
1343 bpau
.bpa_found
= NULL
;
1344 hash_walk(bgp
->pbr_action_hash
, bgp_pbr_action_walkcb
, &bpau
);
1345 return bpau
.bpa_found
;
1348 struct bgp_pbr_match
*bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id
,
1351 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1352 struct bgp_pbr_match_unique bpmu
;
1354 if (!bgp
|| unique
== 0)
1356 bpmu
.unique
= unique
;
1357 bpmu
.bpm_found
= NULL
;
1358 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_match_walkcb
, &bpmu
);
1359 return bpmu
.bpm_found
;
1362 struct bgp_pbr_match_entry
*bgp_pbr_match_ipset_entry_lookup(vrf_id_t vrf_id
,
1366 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1367 struct bgp_pbr_match_entry_unique bpmeu
;
1368 struct bgp_pbr_match_ipsetname bpmi
;
1370 if (!bgp
|| unique
== 0)
1372 bpmi
.ipsetname
= XCALLOC(MTYPE_TMP
, ZEBRA_IPSET_NAME_SIZE
);
1373 snprintf(bpmi
.ipsetname
, ZEBRA_IPSET_NAME_SIZE
, "%s", ipset_name
);
1374 bpmi
.bpm_found
= NULL
;
1375 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_match_pername_walkcb
, &bpmi
);
1376 XFREE(MTYPE_TMP
, bpmi
.ipsetname
);
1377 if (!bpmi
.bpm_found
)
1379 bpmeu
.bpme_found
= NULL
;
1380 bpmeu
.unique
= unique
;
1381 hash_walk(bpmi
.bpm_found
->entry_hash
,
1382 bgp_pbr_match_entry_walkcb
, &bpmeu
);
1383 return bpmeu
.bpme_found
;
1386 struct bgp_pbr_match
*bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id
,
1389 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1390 struct bgp_pbr_match_iptable_unique bpmiu
;
1392 if (!bgp
|| unique
== 0)
1394 bpmiu
.unique
= unique
;
1395 bpmiu
.bpm_found
= NULL
;
1396 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_match_iptable_walkcb
, &bpmiu
);
1397 return bpmiu
.bpm_found
;
1400 void bgp_pbr_cleanup(struct bgp
*bgp
)
1402 if (bgp
->pbr_match_hash
) {
1403 hash_clean(bgp
->pbr_match_hash
, bgp_pbr_match_free
);
1404 hash_free(bgp
->pbr_match_hash
);
1405 bgp
->pbr_match_hash
= NULL
;
1407 if (bgp
->pbr_rule_hash
) {
1408 hash_clean(bgp
->pbr_rule_hash
, bgp_pbr_rule_free
);
1409 hash_free(bgp
->pbr_rule_hash
);
1410 bgp
->pbr_rule_hash
= NULL
;
1412 if (bgp
->pbr_action_hash
) {
1413 hash_clean(bgp
->pbr_action_hash
, bgp_pbr_action_free
);
1414 hash_free(bgp
->pbr_action_hash
);
1415 bgp
->pbr_action_hash
= NULL
;
1417 if (bgp
->bgp_pbr_cfg
== NULL
)
1419 bgp_pbr_reset(bgp
, AFI_IP
);
1420 bgp_pbr_reset(bgp
, AFI_IP6
);
1421 XFREE(MTYPE_PBR
, bgp
->bgp_pbr_cfg
);
1424 void bgp_pbr_init(struct bgp
*bgp
)
1426 bgp
->pbr_match_hash
=
1427 hash_create_size(8, bgp_pbr_match_hash_key
,
1428 bgp_pbr_match_hash_equal
,
1430 bgp
->pbr_action_hash
=
1431 hash_create_size(8, bgp_pbr_action_hash_key
,
1432 bgp_pbr_action_hash_equal
,
1433 "Match Hash Entry");
1435 bgp
->pbr_rule_hash
=
1436 hash_create_size(8, bgp_pbr_rule_hash_key
,
1437 bgp_pbr_rule_hash_equal
,
1440 bgp
->bgp_pbr_cfg
= XCALLOC(MTYPE_PBR
, sizeof(struct bgp_pbr_config
));
1441 bgp
->bgp_pbr_cfg
->pbr_interface_any_ipv4
= true;
1444 void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main
*api
)
1447 char return_string
[512];
1448 char *ptr
= return_string
;
1450 int delta
, len
= sizeof(return_string
);
1452 delta
= snprintf(ptr
, sizeof(return_string
), "MATCH : ");
1455 if (api
->match_bitmask
& PREFIX_SRC_PRESENT
) {
1456 struct prefix
*p
= &(api
->src_prefix
);
1458 if (api
->src_prefix_offset
)
1459 delta
= snprintfrr(ptr
, len
, "@src %pFX/off%u", p
,
1460 api
->src_prefix_offset
);
1462 delta
= snprintfrr(ptr
, len
, "@src %pFX", p
);
1465 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1467 if (api
->match_bitmask
& PREFIX_DST_PRESENT
) {
1468 struct prefix
*p
= &(api
->dst_prefix
);
1470 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1471 if (api
->dst_prefix_offset
)
1472 delta
= snprintfrr(ptr
, len
, "@dst %pFX/off%u", p
,
1473 api
->dst_prefix_offset
);
1475 delta
= snprintfrr(ptr
, len
, "@dst %pFX", p
);
1480 if (api
->match_protocol_num
)
1481 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1482 for (i
= 0; i
< api
->match_protocol_num
; i
++) {
1483 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->protocol
[i
],
1484 i
> 0 ? NULL
: "@proto ");
1489 if (api
->match_src_port_num
)
1490 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1491 for (i
= 0; i
< api
->match_src_port_num
; i
++) {
1492 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->src_port
[i
],
1493 i
> 0 ? NULL
: "@srcport ");
1498 if (api
->match_dst_port_num
)
1499 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1500 for (i
= 0; i
< api
->match_dst_port_num
; i
++) {
1501 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->dst_port
[i
],
1502 i
> 0 ? NULL
: "@dstport ");
1507 if (api
->match_port_num
)
1508 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1509 for (i
= 0; i
< api
->match_port_num
; i
++) {
1510 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->port
[i
],
1511 i
> 0 ? NULL
: "@port ");
1516 if (api
->match_icmp_type_num
)
1517 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1518 for (i
= 0; i
< api
->match_icmp_type_num
; i
++) {
1519 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->icmp_type
[i
],
1520 i
> 0 ? NULL
: "@icmptype ");
1525 if (api
->match_icmp_code_num
)
1526 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1527 for (i
= 0; i
< api
->match_icmp_code_num
; i
++) {
1528 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->icmp_code
[i
],
1529 i
> 0 ? NULL
: "@icmpcode ");
1534 if (api
->match_packet_length_num
)
1535 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1536 for (i
= 0; i
< api
->match_packet_length_num
; i
++) {
1537 delta
= snprintf_bgp_pbr_match_val(ptr
, len
,
1538 &api
->packet_length
[i
],
1539 i
> 0 ? NULL
: "@plen ");
1544 if (api
->match_dscp_num
)
1545 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1546 for (i
= 0; i
< api
->match_dscp_num
; i
++) {
1547 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->dscp
[i
],
1548 i
> 0 ? NULL
: "@dscp ");
1553 if (api
->match_flowlabel_num
)
1554 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1555 for (i
= 0; i
< api
->match_flowlabel_num
; i
++) {
1556 delta
= snprintf_bgp_pbr_match_val(ptr
, len
,
1557 &api
->flow_label
[i
],
1558 i
> 0 ? NULL
: "@flowlabel ");
1563 if (api
->match_tcpflags_num
)
1564 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1565 for (i
= 0; i
< api
->match_tcpflags_num
; i
++) {
1566 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->tcpflags
[i
],
1567 i
> 0 ? NULL
: "@tcpflags ");
1572 if (api
->match_fragment_num
)
1573 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1574 for (i
= 0; i
< api
->match_fragment_num
; i
++) {
1575 delta
= snprintf_bgp_pbr_match_val(ptr
, len
, &api
->fragment
[i
],
1576 i
> 0 ? NULL
: "@fragment ");
1581 len
= sizeof(return_string
);
1583 ptr
= return_string
;
1585 len
-= (ptr
- return_string
);
1586 delta
= snprintf(ptr
, len
, "; ");
1590 if (api
->action_num
) {
1591 delta
= snprintf(ptr
, len
, "SET : ");
1596 for (i
= 0; i
< api
->action_num
; i
++) {
1597 switch (api
->actions
[i
].action
) {
1598 case ACTION_TRAFFICRATE
:
1599 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1600 delta
= snprintf(ptr
, len
, "@set rate %f",
1601 api
->actions
[i
].u
.r
.rate
);
1605 case ACTION_TRAFFIC_ACTION
:
1606 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1607 delta
= snprintf(ptr
, len
, "@action ");
1610 if (api
->actions
[i
].u
.za
.filter
1611 & TRAFFIC_ACTION_TERMINATE
) {
1612 delta
= snprintf(ptr
, len
,
1613 " terminate (apply filter(s))");
1617 if (api
->actions
[i
].u
.za
.filter
1618 & TRAFFIC_ACTION_DISTRIBUTE
) {
1619 delta
= snprintf(ptr
, len
, " distribute");
1623 if (api
->actions
[i
].u
.za
.filter
1624 & TRAFFIC_ACTION_SAMPLE
) {
1625 delta
= snprintf(ptr
, len
, " sample");
1630 case ACTION_REDIRECT_IP
: {
1631 char local_buff
[INET6_ADDRSTRLEN
];
1634 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1635 if (api
->afi
== AF_INET
)
1636 ptr_ip
= &api
->actions
[i
].u
.zr
.redirect_ip_v4
;
1638 ptr_ip
= &api
->actions
[i
].u
.zr
.redirect_ip_v6
;
1639 if (inet_ntop(afi2family(api
->afi
),
1641 INET6_ADDRSTRLEN
) != NULL
) {
1642 delta
= snprintf(ptr
, len
,
1643 "@redirect ip nh %s", local_buff
);
1649 case ACTION_REDIRECT
: {
1652 vrf
= vrf_lookup_by_id(api
->actions
[i
].u
.redirect_vrf
);
1653 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1654 delta
= snprintf(ptr
, len
, "@redirect vrf %s(%u)",
1656 api
->actions
[i
].u
.redirect_vrf
);
1661 case ACTION_MARKING
:
1662 INCREMENT_DISPLAY(ptr
, nb_items
, len
);
1663 delta
= snprintf(ptr
, len
, "@set dscp/flowlabel %u",
1664 api
->actions
[i
].u
.marking_dscp
);
1672 zlog_info("%s", return_string
);
1675 static void bgp_pbr_flush_iprule(struct bgp
*bgp
, struct bgp_pbr_action
*bpa
,
1676 struct bgp_pbr_rule
*bpr
)
1678 /* if bpr is null, do nothing
1682 if (bpr
->installed
) {
1683 bgp_send_pbr_rule_action(bpa
, bpr
, false);
1684 bpr
->installed
= false;
1685 bpr
->action
->refcnt
--;
1688 struct bgp_path_info
*path
;
1689 struct bgp_path_info_extra
*extra
;
1691 /* unlink path to bpme */
1692 path
= (struct bgp_path_info
*)bpr
->path
;
1693 extra
= bgp_path_info_extra_get(path
);
1694 if (extra
->bgp_fs_iprule
)
1695 listnode_delete(extra
->bgp_fs_iprule
, bpr
);
1699 hash_release(bgp
->pbr_rule_hash
, bpr
);
1700 bgp_pbr_bpa_remove(bpa
);
1703 static void bgp_pbr_flush_entry(struct bgp
*bgp
, struct bgp_pbr_action
*bpa
,
1704 struct bgp_pbr_match
*bpm
,
1705 struct bgp_pbr_match_entry
*bpme
)
1707 /* if bpme is null, bpm is also null
1711 /* ipset del entry */
1712 if (bpme
->installed
) {
1713 bgp_send_pbr_ipset_entry_match(bpme
, false);
1714 bpme
->installed
= false;
1715 bpme
->backpointer
= NULL
;
1717 struct bgp_path_info
*path
;
1718 struct bgp_path_info_extra
*extra
;
1720 /* unlink path to bpme */
1721 path
= (struct bgp_path_info
*)bpme
->path
;
1722 extra
= bgp_path_info_extra_get(path
);
1723 if (extra
->bgp_fs_pbr
)
1724 listnode_delete(extra
->bgp_fs_pbr
, bpme
);
1728 hash_release(bpm
->entry_hash
, bpme
);
1729 if (hashcount(bpm
->entry_hash
) == 0) {
1730 /* delete iptable entry first */
1731 /* then delete ipset match */
1732 if (bpm
->installed
) {
1733 if (bpm
->installed_in_iptable
) {
1734 bgp_send_pbr_iptable(bpm
->action
,
1736 bpm
->installed_in_iptable
= false;
1737 bpm
->action
->refcnt
--;
1739 bgp_send_pbr_ipset_match(bpm
, false);
1740 bpm
->installed
= false;
1743 hash_release(bgp
->pbr_match_hash
, bpm
);
1744 /* XXX release pbr_match_action if not used
1745 * note that drop does not need to call send_pbr_action
1748 bgp_pbr_bpa_remove(bpa
);
1751 struct bgp_pbr_match_entry_remain
{
1752 struct bgp_pbr_match_entry
*bpme_to_match
;
1753 struct bgp_pbr_match_entry
*bpme_found
;
1756 struct bgp_pbr_rule_remain
{
1757 struct bgp_pbr_rule
*bpr_to_match
;
1758 struct bgp_pbr_rule
*bpr_found
;
1761 static int bgp_pbr_get_same_rule(struct hash_bucket
*bucket
, void *arg
)
1763 struct bgp_pbr_rule
*r1
= (struct bgp_pbr_rule
*)bucket
->data
;
1764 struct bgp_pbr_rule_remain
*ctxt
=
1765 (struct bgp_pbr_rule_remain
*)arg
;
1766 struct bgp_pbr_rule
*r2
;
1768 r2
= ctxt
->bpr_to_match
;
1770 if (r1
->vrf_id
!= r2
->vrf_id
)
1771 return HASHWALK_CONTINUE
;
1773 if (r1
->flags
!= r2
->flags
)
1774 return HASHWALK_CONTINUE
;
1776 if ((r1
->flags
& MATCH_IP_SRC_SET
) &&
1777 !prefix_same(&r1
->src
, &r2
->src
))
1778 return HASHWALK_CONTINUE
;
1780 if ((r1
->flags
& MATCH_IP_DST_SET
) &&
1781 !prefix_same(&r1
->dst
, &r2
->dst
))
1782 return HASHWALK_CONTINUE
;
1784 /* this function is used for two cases:
1785 * - remove an entry upon withdraw request
1786 * (case r2->action is null)
1787 * - replace an old iprule with different action
1788 * (case r2->action is != null)
1789 * the old one is removed after the new one
1790 * this is to avoid disruption in traffic
1792 if (r2
->action
== NULL
||
1793 r1
->action
!= r2
->action
) {
1794 ctxt
->bpr_found
= r1
;
1795 return HASHWALK_ABORT
;
1797 return HASHWALK_CONTINUE
;
1800 static int bgp_pbr_get_remaining_entry(struct hash_bucket
*bucket
, void *arg
)
1802 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)bucket
->data
;
1803 struct bgp_pbr_match_entry_remain
*bpmer
=
1804 (struct bgp_pbr_match_entry_remain
*)arg
;
1805 struct bgp_pbr_match
*bpm_temp
;
1806 struct bgp_pbr_match_entry
*bpme
= bpmer
->bpme_to_match
;
1808 if (!bpme
->backpointer
||
1809 bpm
== bpme
->backpointer
||
1810 bpme
->backpointer
->action
== bpm
->action
)
1811 return HASHWALK_CONTINUE
;
1812 /* ensure bpm other characteristics are equal */
1813 bpm_temp
= bpme
->backpointer
;
1814 if (bpm_temp
->vrf_id
!= bpm
->vrf_id
||
1815 bpm_temp
->type
!= bpm
->type
||
1816 bpm_temp
->flags
!= bpm
->flags
||
1817 bpm_temp
->tcp_flags
!= bpm
->tcp_flags
||
1818 bpm_temp
->tcp_mask_flags
!= bpm
->tcp_mask_flags
||
1819 bpm_temp
->pkt_len_min
!= bpm
->pkt_len_min
||
1820 bpm_temp
->pkt_len_max
!= bpm
->pkt_len_max
||
1821 bpm_temp
->dscp_value
!= bpm
->dscp_value
||
1822 bpm_temp
->flow_label
!= bpm
->flow_label
||
1823 bpm_temp
->family
!= bpm
->family
||
1824 bpm_temp
->fragment
!= bpm
->fragment
)
1825 return HASHWALK_CONTINUE
;
1827 /* look for remaining bpme */
1828 bpmer
->bpme_found
= hash_lookup(bpm
->entry_hash
, bpme
);
1829 if (!bpmer
->bpme_found
)
1830 return HASHWALK_CONTINUE
;
1831 return HASHWALK_ABORT
;
1834 static void bgp_pbr_policyroute_remove_from_zebra_unit(
1835 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
)
1837 struct bgp_pbr_match temp
;
1838 struct bgp_pbr_match_entry temp2
;
1839 struct bgp_pbr_rule pbr_rule
;
1840 struct bgp_pbr_rule
*bpr
;
1841 struct bgp_pbr_match
*bpm
;
1842 struct bgp_pbr_match_entry
*bpme
;
1843 struct bgp_pbr_match_entry_remain bpmer
;
1844 struct bgp_pbr_range_port
*src_port
;
1845 struct bgp_pbr_range_port
*dst_port
;
1846 struct bgp_pbr_range_port
*pkt_len
;
1847 struct bgp_pbr_rule_remain bprr
;
1851 src_port
= bpf
->src_port
;
1852 dst_port
= bpf
->dst_port
;
1853 pkt_len
= bpf
->pkt_len
;
1855 if (BGP_DEBUG(zebra
, ZEBRA
))
1856 bgp_pbr_dump_entry(bpf
, false);
1858 /* as we don't know information from EC
1859 * look for bpm that have the bpm
1860 * with vrf_id characteristics
1862 memset(&temp2
, 0, sizeof(temp2
));
1863 memset(&temp
, 0, sizeof(temp
));
1865 if (bpf
->type
== BGP_PBR_IPRULE
) {
1866 memset(&pbr_rule
, 0, sizeof(pbr_rule
));
1867 pbr_rule
.vrf_id
= bpf
->vrf_id
;
1869 prefix_copy(&pbr_rule
.src
, bpf
->src
);
1870 pbr_rule
.flags
|= MATCH_IP_SRC_SET
;
1873 prefix_copy(&pbr_rule
.dst
, bpf
->dst
);
1874 pbr_rule
.flags
|= MATCH_IP_DST_SET
;
1877 /* A previous entry may already exist
1878 * flush previous entry if necessary
1880 bprr
.bpr_to_match
= bpr
;
1881 bprr
.bpr_found
= NULL
;
1882 hash_walk(bgp
->pbr_rule_hash
, bgp_pbr_get_same_rule
, &bprr
);
1883 if (bprr
.bpr_found
) {
1884 static struct bgp_pbr_rule
*local_bpr
;
1885 static struct bgp_pbr_action
*local_bpa
;
1887 local_bpr
= bprr
.bpr_found
;
1888 local_bpa
= local_bpr
->action
;
1889 bgp_pbr_flush_iprule(bgp
, local_bpa
,
1895 temp
.family
= bpf
->family
;
1897 temp
.flags
|= MATCH_IP_SRC_SET
;
1898 prefix_copy(&temp2
.src
, bpf
->src
);
1900 temp2
.src
.family
= bpf
->family
;
1902 temp
.flags
|= MATCH_IP_DST_SET
;
1903 prefix_copy(&temp2
.dst
, bpf
->dst
);
1905 temp2
.dst
.family
= bpf
->family
;
1906 if (src_port
&& (src_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
1907 if (bpf
->protocol
== IPPROTO_ICMP
)
1908 temp
.flags
|= MATCH_ICMP_SET
;
1909 temp
.flags
|= MATCH_PORT_SRC_SET
;
1910 temp2
.src_port_min
= src_port
->min_port
;
1911 if (src_port
->max_port
) {
1912 temp
.flags
|= MATCH_PORT_SRC_RANGE_SET
;
1913 temp2
.src_port_max
= src_port
->max_port
;
1916 if (dst_port
&& (dst_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
1917 if (bpf
->protocol
== IPPROTO_ICMP
)
1918 temp
.flags
|= MATCH_ICMP_SET
;
1919 temp
.flags
|= MATCH_PORT_DST_SET
;
1920 temp2
.dst_port_min
= dst_port
->min_port
;
1921 if (dst_port
->max_port
) {
1922 temp
.flags
|= MATCH_PORT_DST_RANGE_SET
;
1923 temp2
.dst_port_max
= dst_port
->max_port
;
1926 temp2
.proto
= bpf
->protocol
;
1929 temp
.pkt_len_min
= pkt_len
->min_port
;
1930 if (pkt_len
->max_port
)
1931 temp
.pkt_len_max
= pkt_len
->max_port
;
1932 } else if (bpf
->pkt_len_val
) {
1933 if (bpf
->pkt_len_val
->mask
)
1934 temp
.flags
|= MATCH_PKT_LEN_INVERSE_SET
;
1935 temp
.pkt_len_min
= bpf
->pkt_len_val
->val
;
1937 if (bpf
->tcp_flags
) {
1938 temp
.tcp_flags
= bpf
->tcp_flags
->val
;
1939 temp
.tcp_mask_flags
= bpf
->tcp_flags
->mask
;
1942 if (bpf
->dscp
->mask
)
1943 temp
.flags
|= MATCH_DSCP_INVERSE_SET
;
1945 temp
.flags
|= MATCH_DSCP_SET
;
1946 temp
.dscp_value
= bpf
->dscp
->val
;
1948 if (bpf
->flow_label
) {
1949 if (bpf
->flow_label
->mask
)
1950 temp
.flags
|= MATCH_FLOW_LABEL_INVERSE_SET
;
1952 temp
.flags
|= MATCH_FLOW_LABEL_SET
;
1953 temp
.flow_label
= bpf
->flow_label
->val
;
1956 if (bpf
->fragment
) {
1957 if (bpf
->fragment
->mask
)
1958 temp
.flags
|= MATCH_FRAGMENT_INVERSE_SET
;
1959 temp
.fragment
= bpf
->fragment
->val
;
1962 if (bpf
->src
== NULL
|| bpf
->dst
== NULL
) {
1963 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
1964 temp
.type
= IPSET_NET_PORT
;
1966 temp
.type
= IPSET_NET
;
1968 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
1969 temp
.type
= IPSET_NET_PORT_NET
;
1971 temp
.type
= IPSET_NET_NET
;
1973 if (bpf
->vrf_id
== VRF_UNKNOWN
) /* XXX case BGP destroy */
1974 temp
.vrf_id
= VRF_DEFAULT
;
1976 temp
.vrf_id
= bpf
->vrf_id
;
1979 bpme
->backpointer
= bpm
;
1980 /* right now, a previous entry may already exist
1981 * flush previous entry if necessary
1983 bpmer
.bpme_to_match
= bpme
;
1984 bpmer
.bpme_found
= NULL
;
1985 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_get_remaining_entry
, &bpmer
);
1986 if (bpmer
.bpme_found
) {
1987 static struct bgp_pbr_match
*local_bpm
;
1988 static struct bgp_pbr_action
*local_bpa
;
1990 local_bpm
= bpmer
.bpme_found
->backpointer
;
1991 local_bpa
= local_bpm
->action
;
1992 bgp_pbr_flush_entry(bgp
, local_bpa
,
1993 local_bpm
, bpmer
.bpme_found
);
1997 static uint8_t bgp_pbr_next_type_entry(uint8_t type_entry
)
1999 if (type_entry
== FLOWSPEC_TCP_FLAGS
)
2000 return FLOWSPEC_DSCP
;
2001 if (type_entry
== FLOWSPEC_DSCP
)
2002 return FLOWSPEC_FLOW_LABEL
;
2003 if (type_entry
== FLOWSPEC_FLOW_LABEL
)
2004 return FLOWSPEC_PKT_LEN
;
2005 if (type_entry
== FLOWSPEC_PKT_LEN
)
2006 return FLOWSPEC_FRAGMENT
;
2007 if (type_entry
== FLOWSPEC_FRAGMENT
)
2008 return FLOWSPEC_ICMP_TYPE
;
2012 static void bgp_pbr_icmp_action(struct bgp
*bgp
, struct bgp_path_info
*path
,
2013 struct bgp_pbr_filter
*bpf
,
2014 struct bgp_pbr_or_filter
*bpof
, bool add
,
2015 struct nexthop
*nh
, float *rate
)
2017 struct bgp_pbr_range_port srcp
, dstp
;
2018 struct bgp_pbr_val_mask
*icmp_type
, *icmp_code
;
2019 struct listnode
*tnode
, *cnode
;
2023 if (bpf
->protocol
!= IPPROTO_ICMP
)
2026 memset(&srcp
, 0, sizeof(srcp
));
2027 memset(&dstp
, 0, sizeof(dstp
));
2028 bpf
->src_port
= &srcp
;
2029 bpf
->dst_port
= &dstp
;
2030 /* parse icmp type and lookup appropriate icmp code
2031 * if no icmp code found, create as many entryes as
2032 * there are listed icmp codes for that icmp type
2034 if (!bpof
->icmp_type
) {
2036 srcp
.max_port
= 255;
2037 for (ALL_LIST_ELEMENTS_RO(bpof
->icmp_code
, cnode
, icmp_code
)) {
2038 dstp
.min_port
= icmp_code
->val
;
2040 bgp_pbr_policyroute_add_to_zebra_unit(
2041 bgp
, path
, bpf
, nh
, rate
);
2043 bgp_pbr_policyroute_remove_from_zebra_unit(
2048 for (ALL_LIST_ELEMENTS_RO(bpof
->icmp_type
, tnode
, icmp_type
)) {
2049 srcp
.min_port
= icmp_type
->val
;
2052 /* only icmp type. create an entry only with icmp type */
2053 if (!bpof
->icmp_code
) {
2054 /* icmp type is not one of the above
2055 * forge an entry only based on the icmp type
2058 dstp
.max_port
= 255;
2060 bgp_pbr_policyroute_add_to_zebra_unit(
2061 bgp
, path
, bpf
, nh
, rate
);
2063 bgp_pbr_policyroute_remove_from_zebra_unit(
2067 for (ALL_LIST_ELEMENTS_RO(bpof
->icmp_code
, cnode
, icmp_code
)) {
2068 dstp
.min_port
= icmp_code
->val
;
2070 bgp_pbr_policyroute_add_to_zebra_unit(
2071 bgp
, path
, bpf
, nh
, rate
);
2073 bgp_pbr_policyroute_remove_from_zebra_unit(
2079 static void bgp_pbr_policyroute_remove_from_zebra_recursive(
2080 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
,
2081 struct bgp_pbr_or_filter
*bpof
, uint8_t type_entry
)
2083 struct listnode
*node
, *nnode
;
2084 struct bgp_pbr_val_mask
*valmask
;
2085 uint8_t next_type_entry
;
2086 struct list
*orig_list
;
2087 struct bgp_pbr_val_mask
**target_val
;
2089 if (type_entry
== 0) {
2090 bgp_pbr_policyroute_remove_from_zebra_unit(bgp
, path
, bpf
);
2093 next_type_entry
= bgp_pbr_next_type_entry(type_entry
);
2094 if (type_entry
== FLOWSPEC_TCP_FLAGS
&& bpof
->tcpflags
) {
2095 orig_list
= bpof
->tcpflags
;
2096 target_val
= &bpf
->tcp_flags
;
2097 } else if (type_entry
== FLOWSPEC_DSCP
&& bpof
->dscp
) {
2098 orig_list
= bpof
->dscp
;
2099 target_val
= &bpf
->dscp
;
2100 } else if (type_entry
== FLOWSPEC_FLOW_LABEL
&& bpof
->flowlabel
) {
2101 orig_list
= bpof
->flowlabel
;
2102 target_val
= &bpf
->flow_label
;
2103 } else if (type_entry
== FLOWSPEC_PKT_LEN
&& bpof
->pkt_len
) {
2104 orig_list
= bpof
->pkt_len
;
2105 target_val
= &bpf
->pkt_len_val
;
2106 } else if (type_entry
== FLOWSPEC_FRAGMENT
&& bpof
->fragment
) {
2107 orig_list
= bpof
->fragment
;
2108 target_val
= &bpf
->fragment
;
2109 } else if (type_entry
== FLOWSPEC_ICMP_TYPE
&&
2110 (bpof
->icmp_type
|| bpof
->icmp_code
)) {
2111 /* enumerate list for icmp - must be last one */
2112 bgp_pbr_icmp_action(bgp
, path
, bpf
, bpof
, false, NULL
, NULL
);
2115 bgp_pbr_policyroute_remove_from_zebra_recursive(
2116 bgp
, path
, bpf
, bpof
, next_type_entry
);
2119 for (ALL_LIST_ELEMENTS(orig_list
, node
, nnode
, valmask
)) {
2120 *target_val
= valmask
;
2121 bgp_pbr_policyroute_remove_from_zebra_recursive(
2122 bgp
, path
, bpf
, bpof
, next_type_entry
);
2126 static void bgp_pbr_policyroute_remove_from_zebra(
2127 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
,
2128 struct bgp_pbr_or_filter
*bpof
)
2131 bgp_pbr_policyroute_remove_from_zebra_unit(bgp
, path
, bpf
);
2135 bgp_pbr_policyroute_remove_from_zebra_recursive(
2136 bgp
, path
, bpf
, bpof
, FLOWSPEC_TCP_FLAGS
);
2137 else if (bpof
->dscp
)
2138 bgp_pbr_policyroute_remove_from_zebra_recursive(
2139 bgp
, path
, bpf
, bpof
, FLOWSPEC_DSCP
);
2140 else if (bpof
->flowlabel
)
2141 bgp_pbr_policyroute_remove_from_zebra_recursive(
2142 bgp
, path
, bpf
, bpof
, FLOWSPEC_FLOW_LABEL
);
2143 else if (bpof
->pkt_len
)
2144 bgp_pbr_policyroute_remove_from_zebra_recursive(
2145 bgp
, path
, bpf
, bpof
, FLOWSPEC_PKT_LEN
);
2146 else if (bpof
->fragment
)
2147 bgp_pbr_policyroute_remove_from_zebra_recursive(
2148 bgp
, path
, bpf
, bpof
, FLOWSPEC_FRAGMENT
);
2149 else if (bpof
->icmp_type
|| bpof
->icmp_code
)
2150 bgp_pbr_policyroute_remove_from_zebra_recursive(
2151 bgp
, path
, bpf
, bpof
, FLOWSPEC_ICMP_TYPE
);
2153 bgp_pbr_policyroute_remove_from_zebra_unit(bgp
, path
, bpf
);
2156 list_delete_all_node(bpof
->tcpflags
);
2158 list_delete_all_node(bpof
->dscp
);
2159 if (bpof
->flowlabel
)
2160 list_delete_all_node(bpof
->flowlabel
);
2162 list_delete_all_node(bpof
->pkt_len
);
2164 list_delete_all_node(bpof
->fragment
);
2167 static void bgp_pbr_dump_entry(struct bgp_pbr_filter
*bpf
, bool add
)
2169 struct bgp_pbr_range_port
*src_port
;
2170 struct bgp_pbr_range_port
*dst_port
;
2171 struct bgp_pbr_range_port
*pkt_len
;
2172 char bufsrc
[64], bufdst
[64];
2174 int remaining_len
= 0;
2175 char protocol_str
[16];
2179 src_port
= bpf
->src_port
;
2180 dst_port
= bpf
->dst_port
;
2181 pkt_len
= bpf
->pkt_len
;
2183 protocol_str
[0] = '\0';
2184 if (bpf
->tcp_flags
&& bpf
->tcp_flags
->mask
)
2185 bpf
->protocol
= IPPROTO_TCP
;
2187 snprintf(protocol_str
, sizeof(protocol_str
),
2188 "proto %d", bpf
->protocol
);
2190 if (bpf
->protocol
== IPPROTO_ICMP
&& src_port
&& dst_port
)
2191 remaining_len
+= snprintf(buffer
, sizeof(buffer
),
2194 dst_port
->min_port
);
2195 else if (bpf
->protocol
== IPPROTO_UDP
||
2196 bpf
->protocol
== IPPROTO_TCP
) {
2198 if (src_port
&& src_port
->min_port
)
2199 remaining_len
+= snprintf(buffer
,
2203 src_port
->max_port
?
2204 src_port
->max_port
:
2205 src_port
->min_port
);
2206 if (dst_port
&& dst_port
->min_port
)
2207 remaining_len
+= snprintf(buffer
+
2213 dst_port
->max_port
?
2214 dst_port
->max_port
:
2215 dst_port
->min_port
);
2217 if (pkt_len
&& (pkt_len
->min_port
|| pkt_len
->max_port
)) {
2218 remaining_len
+= snprintf(buffer
+ remaining_len
,
2226 } else if (bpf
->pkt_len_val
) {
2227 remaining_len
+= snprintf(buffer
+ remaining_len
,
2231 bpf
->pkt_len_val
->mask
2233 bpf
->pkt_len_val
->val
);
2235 if (bpf
->tcp_flags
) {
2236 remaining_len
+= snprintf(buffer
+ remaining_len
,
2240 bpf
->tcp_flags
->val
,
2241 bpf
->tcp_flags
->mask
);
2244 snprintf(buffer
+ remaining_len
,
2252 if (bpf
->flow_label
) {
2253 snprintf(buffer
+ remaining_len
,
2257 bpf
->flow_label
->mask
2259 bpf
->flow_label
->val
);
2261 zlog_debug("BGP: %s FS PBR from %s to %s, %s %s",
2262 add
? "adding" : "removing",
2263 bpf
->src
== NULL
? "<all>" :
2264 prefix2str(bpf
->src
, bufsrc
, sizeof(bufsrc
)),
2265 bpf
->dst
== NULL
? "<all>" :
2266 prefix2str(bpf
->dst
, bufdst
, sizeof(bufdst
)),
2267 protocol_str
, buffer
);
2271 static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp
*bgp
,
2272 struct bgp_path_info
*path
,
2273 struct bgp_pbr_filter
*bpf
,
2277 struct bgp_pbr_match temp
;
2278 struct bgp_pbr_match_entry temp2
;
2279 struct bgp_pbr_match
*bpm
;
2280 struct bgp_pbr_match_entry
*bpme
= NULL
;
2281 struct bgp_pbr_action temp3
;
2282 struct bgp_pbr_action
*bpa
= NULL
;
2283 struct bgp_pbr_match_entry_remain bpmer
;
2284 struct bgp_pbr_rule_remain bprr
;
2285 struct bgp_pbr_range_port
*src_port
;
2286 struct bgp_pbr_range_port
*dst_port
;
2287 struct bgp_pbr_range_port
*pkt_len
;
2288 struct bgp_pbr_rule pbr_rule
;
2289 struct bgp_pbr_rule
*bpr
;
2290 bool bpr_found
= false;
2291 bool bpme_found
= false;
2292 struct vrf
*vrf
= NULL
;
2296 src_port
= bpf
->src_port
;
2297 dst_port
= bpf
->dst_port
;
2298 pkt_len
= bpf
->pkt_len
;
2300 if (BGP_DEBUG(zebra
, ZEBRA
))
2301 bgp_pbr_dump_entry(bpf
, true);
2303 /* look for bpa first */
2304 memset(&temp3
, 0, sizeof(temp3
));
2308 memcpy(&temp3
.nh
, nh
, sizeof(struct nexthop
));
2309 temp3
.vrf_id
= bpf
->vrf_id
;
2310 temp3
.afi
= family2afi(bpf
->family
);
2311 bpa
= hash_get(bgp
->pbr_action_hash
, &temp3
,
2312 bgp_pbr_action_alloc_intern
);
2315 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
2316 if (bpa
->fwmark
== 0) {
2317 /* drop is handled by iptable */
2318 if (nh
&& nh
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2320 bpa
->installed
= true;
2322 bpa
->fwmark
= bgp_zebra_tm_get_id();
2323 /* if action is redirect-vrf, then
2324 * use directly table_id of vrf
2326 if (nh
&& vrf
&& !vrf_is_backend_netns()
2327 && bpf
->vrf_id
!= vrf
->vrf_id
)
2328 bpa
->table_id
= vrf
->data
.l
.table_id
;
2330 bpa
->table_id
= bpa
->fwmark
;
2331 bpa
->installed
= false;
2334 bpa
->unique
= ++bgp_pbr_action_counter_unique
;
2335 /* 0 value is forbidden */
2336 bpa
->install_in_progress
= false;
2338 if (bpf
->type
== BGP_PBR_IPRULE
) {
2339 memset(&pbr_rule
, 0, sizeof(pbr_rule
));
2340 pbr_rule
.vrf_id
= bpf
->vrf_id
;
2341 pbr_rule
.priority
= 20;
2343 pbr_rule
.flags
|= MATCH_IP_SRC_SET
;
2344 prefix_copy(&pbr_rule
.src
, bpf
->src
);
2347 pbr_rule
.flags
|= MATCH_IP_DST_SET
;
2348 prefix_copy(&pbr_rule
.dst
, bpf
->dst
);
2350 pbr_rule
.action
= bpa
;
2351 bpr
= hash_get(bgp
->pbr_rule_hash
, &pbr_rule
,
2352 bgp_pbr_rule_alloc_intern
);
2353 if (bpr
&& bpr
->unique
== 0) {
2354 bpr
->unique
= ++bgp_pbr_action_counter_unique
;
2355 bpr
->installed
= false;
2356 bpr
->install_in_progress
= false;
2357 /* link bgp info to bpr */
2358 bpr
->path
= (void *)path
;
2361 /* already installed */
2362 if (bpr_found
&& bpr
) {
2363 struct bgp_path_info_extra
*extra
=
2364 bgp_path_info_extra_get(path
);
2367 listnode_lookup_nocheck(extra
->bgp_fs_iprule
,
2369 if (BGP_DEBUG(pbr
, PBR_ERROR
))
2370 zlog_err("%s: entry %p/%p already installed in bgp pbr iprule",
2371 __func__
, path
, bpr
);
2376 bgp_pbr_bpa_add(bpa
);
2379 if (bpr
&& !bpr
->installed
)
2380 bgp_send_pbr_rule_action(bpa
, bpr
, true);
2382 /* A previous entry may already exist
2383 * flush previous entry if necessary
2385 bprr
.bpr_to_match
= bpr
;
2386 bprr
.bpr_found
= NULL
;
2387 hash_walk(bgp
->pbr_rule_hash
, bgp_pbr_get_same_rule
, &bprr
);
2388 if (bprr
.bpr_found
) {
2389 static struct bgp_pbr_rule
*local_bpr
;
2390 static struct bgp_pbr_action
*local_bpa
;
2392 local_bpr
= bprr
.bpr_found
;
2393 local_bpa
= local_bpr
->action
;
2394 bgp_pbr_flush_iprule(bgp
, local_bpa
,
2399 /* then look for bpm */
2400 memset(&temp
, 0, sizeof(temp
));
2401 temp
.vrf_id
= bpf
->vrf_id
;
2402 temp
.family
= bpf
->family
;
2404 temp
.flags
|= MATCH_IP_SRC_SET
;
2406 temp
.flags
|= MATCH_IP_DST_SET
;
2408 if (src_port
&& (src_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
2409 if (bpf
->protocol
== IPPROTO_ICMP
)
2410 temp
.flags
|= MATCH_ICMP_SET
;
2411 temp
.flags
|= MATCH_PORT_SRC_SET
;
2413 if (dst_port
&& (dst_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
2414 if (bpf
->protocol
== IPPROTO_ICMP
)
2415 temp
.flags
|= MATCH_ICMP_SET
;
2416 temp
.flags
|= MATCH_PORT_DST_SET
;
2418 if (src_port
&& src_port
->max_port
)
2419 temp
.flags
|= MATCH_PORT_SRC_RANGE_SET
;
2420 if (dst_port
&& dst_port
->max_port
)
2421 temp
.flags
|= MATCH_PORT_DST_RANGE_SET
;
2423 if (bpf
->src
== NULL
|| bpf
->dst
== NULL
) {
2424 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
2425 temp
.type
= IPSET_NET_PORT
;
2427 temp
.type
= IPSET_NET
;
2429 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
2430 temp
.type
= IPSET_NET_PORT_NET
;
2432 temp
.type
= IPSET_NET_NET
;
2435 temp
.pkt_len_min
= pkt_len
->min_port
;
2436 if (pkt_len
->max_port
)
2437 temp
.pkt_len_max
= pkt_len
->max_port
;
2438 } else if (bpf
->pkt_len_val
) {
2439 if (bpf
->pkt_len_val
->mask
)
2440 temp
.flags
|= MATCH_PKT_LEN_INVERSE_SET
;
2441 temp
.pkt_len_min
= bpf
->pkt_len_val
->val
;
2443 if (bpf
->tcp_flags
) {
2444 temp
.tcp_flags
= bpf
->tcp_flags
->val
;
2445 temp
.tcp_mask_flags
= bpf
->tcp_flags
->mask
;
2448 if (bpf
->dscp
->mask
)
2449 temp
.flags
|= MATCH_DSCP_INVERSE_SET
;
2451 temp
.flags
|= MATCH_DSCP_SET
;
2452 temp
.dscp_value
= bpf
->dscp
->val
;
2454 if (bpf
->flow_label
) {
2455 if (bpf
->flow_label
->mask
)
2456 temp
.flags
|= MATCH_FLOW_LABEL_INVERSE_SET
;
2458 temp
.flags
|= MATCH_FLOW_LABEL_SET
;
2459 temp
.flow_label
= bpf
->flow_label
->val
;
2461 if (bpf
->fragment
) {
2462 if (bpf
->fragment
->mask
)
2463 temp
.flags
|= MATCH_FRAGMENT_INVERSE_SET
;
2464 temp
.fragment
= bpf
->fragment
->val
;
2466 if (bpf
->protocol
) {
2467 temp
.protocol
= bpf
->protocol
;
2468 temp
.flags
|= MATCH_PROTOCOL_SET
;
2471 bpm
= hash_get(bgp
->pbr_match_hash
, &temp
,
2472 bgp_pbr_match_alloc_intern
);
2474 /* new, then self allocate ipset_name and unique */
2475 if (bpm
->unique
== 0) {
2476 bpm
->unique
= ++bgp_pbr_match_counter_unique
;
2477 /* 0 value is forbidden */
2478 snprintf(bpm
->ipset_name
, sizeof(bpm
->ipset_name
),
2480 bpm
->entry_hash
= hash_create_size(8,
2481 bgp_pbr_match_entry_hash_key
,
2482 bgp_pbr_match_entry_hash_equal
,
2483 "Match Entry Hash");
2484 bpm
->installed
= false;
2486 /* unique2 should be updated too */
2487 bpm
->unique2
= ++bgp_pbr_match_iptable_counter_unique
;
2488 bpm
->installed_in_iptable
= false;
2489 bpm
->install_in_progress
= false;
2490 bpm
->install_iptable_in_progress
= false;
2493 memset(&temp2
, 0, sizeof(temp2
));
2495 prefix_copy(&temp2
.src
, bpf
->src
);
2497 temp2
.src
.family
= bpf
->family
;
2499 prefix_copy(&temp2
.dst
, bpf
->dst
);
2501 temp2
.dst
.family
= bpf
->family
;
2502 temp2
.src_port_min
= src_port
? src_port
->min_port
: 0;
2503 temp2
.dst_port_min
= dst_port
? dst_port
->min_port
: 0;
2504 temp2
.src_port_max
= src_port
? src_port
->max_port
: 0;
2505 temp2
.dst_port_max
= dst_port
? dst_port
->max_port
: 0;
2506 temp2
.proto
= bpf
->protocol
;
2507 bpme
= hash_get(bpm
->entry_hash
, &temp2
,
2508 bgp_pbr_match_entry_alloc_intern
);
2509 if (bpme
->unique
== 0) {
2510 bpme
->unique
= ++bgp_pbr_match_entry_counter_unique
;
2511 /* 0 value is forbidden */
2512 bpme
->backpointer
= bpm
;
2513 bpme
->installed
= false;
2514 bpme
->install_in_progress
= false;
2515 /* link bgp info to bpme */
2516 bpme
->path
= (void *)path
;
2520 /* already installed */
2522 struct bgp_path_info_extra
*extra
=
2523 bgp_path_info_extra_get(path
);
2526 listnode_lookup_nocheck(extra
->bgp_fs_pbr
, bpme
)) {
2527 if (BGP_DEBUG(pbr
, PBR_ERROR
))
2529 "%s: entry %p/%p already installed in bgp pbr",
2530 __func__
, path
, bpme
);
2534 /* BGP FS: append entry to zebra
2535 * - policies are not routing entries and as such
2536 * route replace semantics don't necessarily follow
2537 * through to policy entries
2538 * - because of that, not all policing information will be stored
2539 * into zebra. and non selected policies will be suppressed from zebra
2540 * - as consequence, in order to bring consistency
2541 * a policy will be added, then ifan ecmp policy exists,
2542 * it will be suppressed subsequently
2545 bgp_pbr_bpa_add(bpa
);
2548 if (!bpm
->installed
)
2549 bgp_send_pbr_ipset_match(bpm
, true);
2551 if (!bpme
->installed
)
2552 bgp_send_pbr_ipset_entry_match(bpme
, true);
2555 if (!bpm
->installed_in_iptable
)
2556 bgp_send_pbr_iptable(bpa
, bpm
, true);
2558 /* A previous entry may already exist
2559 * flush previous entry if necessary
2561 bpmer
.bpme_to_match
= bpme
;
2562 bpmer
.bpme_found
= NULL
;
2563 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_get_remaining_entry
, &bpmer
);
2564 if (bpmer
.bpme_found
) {
2565 static struct bgp_pbr_match
*local_bpm
;
2566 static struct bgp_pbr_action
*local_bpa
;
2568 local_bpm
= bpmer
.bpme_found
->backpointer
;
2569 local_bpa
= local_bpm
->action
;
2570 bgp_pbr_flush_entry(bgp
, local_bpa
,
2571 local_bpm
, bpmer
.bpme_found
);
2577 static void bgp_pbr_policyroute_add_to_zebra_recursive(
2578 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
,
2579 struct bgp_pbr_or_filter
*bpof
, struct nexthop
*nh
, float *rate
,
2582 struct listnode
*node
, *nnode
;
2583 struct bgp_pbr_val_mask
*valmask
;
2584 uint8_t next_type_entry
;
2585 struct list
*orig_list
;
2586 struct bgp_pbr_val_mask
**target_val
;
2588 if (type_entry
== 0) {
2589 bgp_pbr_policyroute_add_to_zebra_unit(bgp
, path
, bpf
, nh
, rate
);
2592 next_type_entry
= bgp_pbr_next_type_entry(type_entry
);
2593 if (type_entry
== FLOWSPEC_TCP_FLAGS
&& bpof
->tcpflags
) {
2594 orig_list
= bpof
->tcpflags
;
2595 target_val
= &bpf
->tcp_flags
;
2596 } else if (type_entry
== FLOWSPEC_DSCP
&& bpof
->dscp
) {
2597 orig_list
= bpof
->dscp
;
2598 target_val
= &bpf
->dscp
;
2599 } else if (type_entry
== FLOWSPEC_PKT_LEN
&& bpof
->pkt_len
) {
2600 orig_list
= bpof
->pkt_len
;
2601 target_val
= &bpf
->pkt_len_val
;
2602 } else if (type_entry
== FLOWSPEC_FRAGMENT
&& bpof
->fragment
) {
2603 orig_list
= bpof
->fragment
;
2604 target_val
= &bpf
->fragment
;
2605 } else if (type_entry
== FLOWSPEC_ICMP_TYPE
&&
2606 (bpof
->icmp_type
|| bpof
->icmp_code
)) {
2607 /* enumerate list for icmp - must be last one */
2608 bgp_pbr_icmp_action(bgp
, path
, bpf
, bpof
, true, nh
, rate
);
2611 bgp_pbr_policyroute_add_to_zebra_recursive(
2612 bgp
, path
, bpf
, bpof
, nh
, rate
, next_type_entry
);
2615 for (ALL_LIST_ELEMENTS(orig_list
, node
, nnode
, valmask
)) {
2616 *target_val
= valmask
;
2617 bgp_pbr_policyroute_add_to_zebra_recursive(
2618 bgp
, path
, bpf
, bpof
, nh
, rate
, next_type_entry
);
2622 static void bgp_pbr_policyroute_add_to_zebra(struct bgp
*bgp
,
2623 struct bgp_path_info
*path
,
2624 struct bgp_pbr_filter
*bpf
,
2625 struct bgp_pbr_or_filter
*bpof
,
2626 struct nexthop
*nh
, float *rate
)
2629 bgp_pbr_policyroute_add_to_zebra_unit(bgp
, path
, bpf
, nh
, rate
);
2633 bgp_pbr_policyroute_add_to_zebra_recursive(
2634 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_TCP_FLAGS
);
2635 else if (bpof
->dscp
)
2636 bgp_pbr_policyroute_add_to_zebra_recursive(
2637 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_DSCP
);
2638 else if (bpof
->pkt_len
)
2639 bgp_pbr_policyroute_add_to_zebra_recursive(
2640 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_PKT_LEN
);
2641 else if (bpof
->fragment
)
2642 bgp_pbr_policyroute_add_to_zebra_recursive(
2643 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_FRAGMENT
);
2644 else if (bpof
->icmp_type
|| bpof
->icmp_code
)
2645 bgp_pbr_policyroute_add_to_zebra_recursive(
2646 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_ICMP_TYPE
);
2648 bgp_pbr_policyroute_add_to_zebra_unit(bgp
, path
, bpf
, nh
, rate
);
2651 list_delete_all_node(bpof
->tcpflags
);
2653 list_delete_all_node(bpof
->dscp
);
2655 list_delete_all_node(bpof
->pkt_len
);
2657 list_delete_all_node(bpof
->fragment
);
2658 if (bpof
->icmp_type
)
2659 list_delete_all_node(bpof
->icmp_type
);
2660 if (bpof
->icmp_code
)
2661 list_delete_all_node(bpof
->icmp_code
);
2664 static void bgp_pbr_handle_entry(struct bgp
*bgp
, struct bgp_path_info
*path
,
2665 struct bgp_pbr_entry_main
*api
, bool add
)
2669 int continue_loop
= 1;
2671 struct prefix
*src
= NULL
, *dst
= NULL
;
2673 struct bgp_pbr_range_port
*srcp
= NULL
, *dstp
= NULL
;
2674 struct bgp_pbr_range_port range
, range_icmp_code
;
2675 struct bgp_pbr_range_port pkt_len
;
2676 struct bgp_pbr_filter bpf
;
2678 struct bgp_pbr_or_filter bpof
;
2679 struct bgp_pbr_val_mask bpvm
;
2681 memset(&range
, 0, sizeof(range
));
2682 memset(&nh
, 0, sizeof(struct nexthop
));
2683 memset(&bpf
, 0, sizeof(struct bgp_pbr_filter
));
2684 memset(&bpof
, 0, sizeof(struct bgp_pbr_or_filter
));
2685 if (api
->match_bitmask
& PREFIX_SRC_PRESENT
||
2686 (api
->type
== BGP_PBR_IPRULE
&&
2687 api
->match_bitmask_iprule
& PREFIX_SRC_PRESENT
))
2688 src
= &api
->src_prefix
;
2689 if (api
->match_bitmask
& PREFIX_DST_PRESENT
||
2690 (api
->type
== BGP_PBR_IPRULE
&&
2691 api
->match_bitmask_iprule
& PREFIX_DST_PRESENT
))
2692 dst
= &api
->dst_prefix
;
2693 if (api
->type
== BGP_PBR_IPRULE
)
2694 bpf
.type
= api
->type
;
2695 memset(&nh
, 0, sizeof(struct nexthop
));
2696 nh
.vrf_id
= VRF_UNKNOWN
;
2697 if (api
->match_protocol_num
) {
2698 proto
= (uint8_t)api
->protocol
[0].value
;
2699 if (api
->afi
== AF_INET6
&& proto
== IPPROTO_ICMPV6
)
2700 proto
= IPPROTO_ICMP
;
2702 /* if match_port is selected, then either src or dst port will be parsed
2703 * but not both at the same time
2705 if (api
->match_port_num
>= 1) {
2706 bgp_pbr_extract(api
->port
,
2707 api
->match_port_num
,
2709 srcp
= dstp
= &range
;
2710 } else if (api
->match_src_port_num
>= 1) {
2711 bgp_pbr_extract(api
->src_port
,
2712 api
->match_src_port_num
,
2716 } else if (api
->match_dst_port_num
>= 1) {
2717 bgp_pbr_extract(api
->dst_port
,
2718 api
->match_dst_port_num
,
2723 if (api
->match_icmp_type_num
>= 1) {
2724 proto
= IPPROTO_ICMP
;
2725 if (bgp_pbr_extract(api
->icmp_type
,
2726 api
->match_icmp_type_num
,
2730 bpof
.icmp_type
= list_new();
2731 bgp_pbr_extract_enumerate(api
->icmp_type
,
2732 api
->match_icmp_type_num
,
2735 FLOWSPEC_ICMP_TYPE
);
2738 if (api
->match_icmp_code_num
>= 1) {
2739 proto
= IPPROTO_ICMP
;
2740 if (bgp_pbr_extract(api
->icmp_code
,
2741 api
->match_icmp_code_num
,
2743 dstp
= &range_icmp_code
;
2745 bpof
.icmp_code
= list_new();
2746 bgp_pbr_extract_enumerate(api
->icmp_code
,
2747 api
->match_icmp_code_num
,
2750 FLOWSPEC_ICMP_CODE
);
2754 if (api
->match_tcpflags_num
) {
2755 kind_enum
= bgp_pbr_match_val_get_operator(api
->tcpflags
,
2756 api
->match_tcpflags_num
);
2757 if (kind_enum
== OPERATOR_UNARY_AND
) {
2758 bpf
.tcp_flags
= &bpvm
;
2759 bgp_pbr_extract_enumerate(api
->tcpflags
,
2760 api
->match_tcpflags_num
,
2763 FLOWSPEC_TCP_FLAGS
);
2764 } else if (kind_enum
== OPERATOR_UNARY_OR
) {
2765 bpof
.tcpflags
= list_new();
2766 bgp_pbr_extract_enumerate(api
->tcpflags
,
2767 api
->match_tcpflags_num
,
2770 FLOWSPEC_TCP_FLAGS
);
2773 if (api
->match_packet_length_num
) {
2776 ret
= bgp_pbr_extract(api
->packet_length
,
2777 api
->match_packet_length_num
,
2780 bpf
.pkt_len
= &pkt_len
;
2782 bpof
.pkt_len
= list_new();
2783 bgp_pbr_extract_enumerate(api
->packet_length
,
2784 api
->match_packet_length_num
,
2790 if (api
->match_dscp_num
>= 1) {
2791 bpof
.dscp
= list_new();
2792 bgp_pbr_extract_enumerate(api
->dscp
, api
->match_dscp_num
,
2794 bpof
.dscp
, FLOWSPEC_DSCP
);
2796 if (api
->match_fragment_num
) {
2797 bpof
.fragment
= list_new();
2798 bgp_pbr_extract_enumerate(api
->fragment
,
2799 api
->match_fragment_num
,
2804 bpf
.vrf_id
= api
->vrf_id
;
2807 bpf
.protocol
= proto
;
2808 bpf
.src_port
= srcp
;
2809 bpf
.dst_port
= dstp
;
2810 bpf
.family
= afi2family(api
->afi
);
2812 bgp_pbr_policyroute_remove_from_zebra(bgp
, path
, &bpf
, &bpof
);
2815 /* no action for add = true */
2816 for (i
= 0; i
< api
->action_num
; i
++) {
2817 switch (api
->actions
[i
].action
) {
2818 case ACTION_TRAFFICRATE
:
2820 if (api
->actions
[i
].u
.r
.rate
== 0) {
2821 nh
.vrf_id
= api
->vrf_id
;
2822 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2823 bgp_pbr_policyroute_add_to_zebra(
2824 bgp
, path
, &bpf
, &bpof
, &nh
, &rate
);
2826 /* update rate. can be reentrant */
2827 rate
= api
->actions
[i
].u
.r
.rate
;
2828 if (BGP_DEBUG(pbr
, PBR
)) {
2829 bgp_pbr_print_policy_route(api
);
2830 zlog_warn("PBR: ignoring Set action rate %f",
2831 api
->actions
[i
].u
.r
.rate
);
2835 case ACTION_TRAFFIC_ACTION
:
2836 if (api
->actions
[i
].u
.za
.filter
2837 & TRAFFIC_ACTION_SAMPLE
) {
2838 if (BGP_DEBUG(pbr
, PBR
)) {
2839 bgp_pbr_print_policy_route(api
);
2840 zlog_warn("PBR: Sample action Ignored");
2843 /* terminate action: run other filters
2846 case ACTION_REDIRECT_IP
:
2847 nh
.vrf_id
= api
->vrf_id
;
2848 if (api
->afi
== AFI_IP
) {
2849 nh
.type
= NEXTHOP_TYPE_IPV4
;
2850 nh
.gate
.ipv4
.s_addr
=
2851 api
->actions
[i
].u
.zr
.
2852 redirect_ip_v4
.s_addr
;
2854 nh
.type
= NEXTHOP_TYPE_IPV6
;
2855 memcpy(&nh
.gate
.ipv6
,
2856 &api
->actions
[i
].u
.zr
.redirect_ip_v6
,
2857 sizeof(struct in6_addr
));
2859 bgp_pbr_policyroute_add_to_zebra(bgp
, path
, &bpf
, &bpof
,
2861 /* XXX combination with REDIRECT_VRF
2862 * + REDIRECT_NH_IP not done
2866 case ACTION_REDIRECT
:
2867 if (api
->afi
== AFI_IP
)
2868 nh
.type
= NEXTHOP_TYPE_IPV4
;
2870 nh
.type
= NEXTHOP_TYPE_IPV6
;
2871 nh
.vrf_id
= api
->actions
[i
].u
.redirect_vrf
;
2872 bgp_pbr_policyroute_add_to_zebra(bgp
, path
, &bpf
, &bpof
,
2876 case ACTION_MARKING
:
2877 if (BGP_DEBUG(pbr
, PBR
)) {
2878 bgp_pbr_print_policy_route(api
);
2879 zlog_warn("PBR: Set DSCP/FlowLabel %u Ignored",
2880 api
->actions
[i
].u
.marking_dscp
);
2886 if (continue_loop
== 0)
2891 void bgp_pbr_update_entry(struct bgp
*bgp
, const struct prefix
*p
,
2892 struct bgp_path_info
*info
, afi_t afi
, safi_t safi
,
2895 struct bgp_pbr_entry_main api
;
2897 if (safi
!= SAFI_FLOWSPEC
)
2898 return; /* not supported */
2899 /* Make Zebra API structure. */
2900 memset(&api
, 0, sizeof(api
));
2901 api
.vrf_id
= bgp
->vrf_id
;
2904 if (!bgp_zebra_tm_chunk_obtained()) {
2905 if (BGP_DEBUG(pbr
, PBR_ERROR
))
2906 flog_err(EC_BGP_TABLE_CHUNK
,
2907 "%s: table chunk not obtained yet", __func__
);
2911 if (bgp_pbr_build_and_validate_entry(p
, info
, &api
) < 0) {
2912 if (BGP_DEBUG(pbr
, PBR_ERROR
))
2913 flog_err(EC_BGP_FLOWSPEC_INSTALLATION
,
2914 "%s: cancel updating entry %p in bgp pbr",
2918 bgp_pbr_handle_entry(bgp
, info
, &api
, nlri_update
);
2921 int bgp_pbr_interface_compare(const struct bgp_pbr_interface
*a
,
2922 const struct bgp_pbr_interface
*b
)
2924 return strcmp(a
->name
, b
->name
);
2927 struct bgp_pbr_interface
*bgp_pbr_interface_lookup(const char *name
,
2928 struct bgp_pbr_interface_head
*head
)
2930 struct bgp_pbr_interface pbr_if
;
2932 strlcpy(pbr_if
.name
, name
, sizeof(pbr_if
.name
));
2933 return (RB_FIND(bgp_pbr_interface_head
,
2937 /* this function resets to the default policy routing
2938 * go back to default status
2940 void bgp_pbr_reset(struct bgp
*bgp
, afi_t afi
)
2942 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2943 struct bgp_pbr_interface_head
*head
;
2944 struct bgp_pbr_interface
*pbr_if
;
2949 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2951 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
2952 while (!RB_EMPTY(bgp_pbr_interface_head
, head
)) {
2953 pbr_if
= RB_ROOT(bgp_pbr_interface_head
, head
);
2954 RB_REMOVE(bgp_pbr_interface_head
, head
, pbr_if
);
2955 XFREE(MTYPE_TMP
, pbr_if
);