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 "bgpd/bgpd.h"
27 #include "bgpd/bgp_pbr.h"
28 #include "bgpd/bgp_debug.h"
29 #include "bgpd/bgp_flowspec_util.h"
30 #include "bgpd/bgp_ecommunity.h"
31 #include "bgpd/bgp_route.h"
32 #include "bgpd/bgp_attr.h"
33 #include "bgpd/bgp_zebra.h"
34 #include "bgpd/bgp_mplsvpn.h"
35 #include "bgpd/bgp_flowspec_private.h"
36 #include "bgpd/bgp_errors.h"
38 DEFINE_MTYPE_STATIC(BGPD
, PBR_MATCH_ENTRY
, "PBR match entry")
39 DEFINE_MTYPE_STATIC(BGPD
, PBR_MATCH
, "PBR match")
40 DEFINE_MTYPE_STATIC(BGPD
, PBR_ACTION
, "PBR action")
41 DEFINE_MTYPE_STATIC(BGPD
, PBR
, "BGP PBR Context")
42 DEFINE_MTYPE_STATIC(BGPD
, PBR_VALMASK
, "BGP PBR Val Mask Value")
44 RB_GENERATE(bgp_pbr_interface_head
, bgp_pbr_interface
,
45 id_entry
, bgp_pbr_interface_compare
);
46 struct bgp_pbr_interface_head ifaces_by_name_ipv4
=
47 RB_INITIALIZER(&ifaces_by_name_ipv4
);
49 static int bgp_pbr_match_counter_unique
;
50 static int bgp_pbr_match_entry_counter_unique
;
51 static int bgp_pbr_action_counter_unique
;
52 static int bgp_pbr_match_iptable_counter_unique
;
54 struct bgp_pbr_match_iptable_unique
{
56 struct bgp_pbr_match
*bpm_found
;
59 struct bgp_pbr_match_entry_unique
{
61 struct bgp_pbr_match_entry
*bpme_found
;
64 struct bgp_pbr_action_unique
{
66 struct bgp_pbr_action
*bpa_found
;
69 static int bgp_pbr_action_walkcb(struct hash_backet
*backet
, void *arg
)
71 struct bgp_pbr_action
*bpa
= (struct bgp_pbr_action
*)backet
->data
;
72 struct bgp_pbr_action_unique
*bpau
= (struct bgp_pbr_action_unique
*)
74 uint32_t unique
= bpau
->unique
;
76 if (bpa
->unique
== unique
) {
77 bpau
->bpa_found
= bpa
;
78 return HASHWALK_ABORT
;
80 return HASHWALK_CONTINUE
;
83 static int bgp_pbr_match_entry_walkcb(struct hash_backet
*backet
, void *arg
)
85 struct bgp_pbr_match_entry
*bpme
=
86 (struct bgp_pbr_match_entry
*)backet
->data
;
87 struct bgp_pbr_match_entry_unique
*bpmeu
=
88 (struct bgp_pbr_match_entry_unique
*)arg
;
89 uint32_t unique
= bpmeu
->unique
;
91 if (bpme
->unique
== unique
) {
92 bpmeu
->bpme_found
= bpme
;
93 return HASHWALK_ABORT
;
95 return HASHWALK_CONTINUE
;
98 struct bgp_pbr_match_ipsetname
{
100 struct bgp_pbr_match
*bpm_found
;
103 static int bgp_pbr_match_pername_walkcb(struct hash_backet
*backet
, void *arg
)
105 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)backet
->data
;
106 struct bgp_pbr_match_ipsetname
*bpmi
=
107 (struct bgp_pbr_match_ipsetname
*)arg
;
108 char *ipset_name
= bpmi
->ipsetname
;
110 if (!strncmp(ipset_name
, bpm
->ipset_name
,
111 ZEBRA_IPSET_NAME_SIZE
)) {
112 bpmi
->bpm_found
= bpm
;
113 return HASHWALK_ABORT
;
115 return HASHWALK_CONTINUE
;
118 static int bgp_pbr_match_iptable_walkcb(struct hash_backet
*backet
, void *arg
)
120 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)backet
->data
;
121 struct bgp_pbr_match_iptable_unique
*bpmiu
=
122 (struct bgp_pbr_match_iptable_unique
*)arg
;
123 uint32_t unique
= bpmiu
->unique
;
125 if (bpm
->unique2
== unique
) {
126 bpmiu
->bpm_found
= bpm
;
127 return HASHWALK_ABORT
;
129 return HASHWALK_CONTINUE
;
132 struct bgp_pbr_match_unique
{
134 struct bgp_pbr_match
*bpm_found
;
137 static int bgp_pbr_match_walkcb(struct hash_backet
*backet
, void *arg
)
139 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)backet
->data
;
140 struct bgp_pbr_match_unique
*bpmu
= (struct bgp_pbr_match_unique
*)
142 uint32_t unique
= bpmu
->unique
;
144 if (bpm
->unique
== unique
) {
145 bpmu
->bpm_found
= bpm
;
146 return HASHWALK_ABORT
;
148 return HASHWALK_CONTINUE
;
151 static int sprintf_bgp_pbr_match_val(char *str
, struct bgp_pbr_match_val
*mval
,
157 ptr
+= sprintf(ptr
, "%s", prepend
);
159 if (mval
->unary_operator
& OPERATOR_UNARY_OR
)
160 ptr
+= sprintf(ptr
, ", or ");
161 if (mval
->unary_operator
& OPERATOR_UNARY_AND
)
162 ptr
+= sprintf(ptr
, ", and ");
164 if (mval
->compare_operator
& OPERATOR_COMPARE_LESS_THAN
)
165 ptr
+= sprintf(ptr
, "<");
166 if (mval
->compare_operator
& OPERATOR_COMPARE_GREATER_THAN
)
167 ptr
+= sprintf(ptr
, ">");
168 if (mval
->compare_operator
& OPERATOR_COMPARE_EQUAL_TO
)
169 ptr
+= sprintf(ptr
, "=");
170 if (mval
->compare_operator
& OPERATOR_COMPARE_EXACT_MATCH
)
171 ptr
+= sprintf(ptr
, "match");
172 ptr
+= sprintf(ptr
, " %u", mval
->value
);
173 return (int)(ptr
- str
);
176 #define INCREMENT_DISPLAY(_ptr, _cnt) do { \
178 (_ptr) += sprintf((_ptr), "; "); \
182 /* this structure can be used for port range,
183 * but also for other values range like packet length range
185 struct bgp_pbr_range_port
{
190 /* this structure can be used to filter with a mask
191 * for instance it supports not instructions like for
194 struct bgp_pbr_val_mask
{
199 /* this structure is used to pass instructs
200 * so that BGP can create pbr instructions to ZEBRA
202 struct bgp_pbr_filter
{
207 struct bgp_pbr_range_port
*pkt_len
;
208 struct bgp_pbr_range_port
*src_port
;
209 struct bgp_pbr_range_port
*dst_port
;
210 struct bgp_pbr_val_mask
*tcp_flags
;
211 struct bgp_pbr_val_mask
*dscp
;
212 struct bgp_pbr_val_mask
*pkt_len_val
;
213 struct bgp_pbr_val_mask
*fragment
;
216 /* this structure is used to contain OR instructions
217 * so that BGP can create multiple pbr instructions
220 struct bgp_pbr_or_filter
{
221 struct list
*tcpflags
;
223 struct list
*pkt_len
;
224 struct list
*fragment
;
225 struct list
*icmp_type
;
226 struct list
*icmp_code
;
229 static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp
*bgp
,
230 struct bgp_path_info
*path
,
231 struct bgp_pbr_filter
*bpf
,
235 static void bgp_pbr_dump_entry(struct bgp_pbr_filter
*bpf
, bool add
);
237 static bool bgp_pbr_extract_enumerate_unary_opposite(
238 uint8_t unary_operator
,
239 struct bgp_pbr_val_mask
*and_valmask
,
240 struct list
*or_valmask
, uint32_t value
,
243 if (unary_operator
== OPERATOR_UNARY_AND
&& and_valmask
) {
244 if (type_entry
== FLOWSPEC_TCP_FLAGS
) {
246 TCP_HEADER_ALL_FLAGS
&
248 } else if (type_entry
== FLOWSPEC_DSCP
||
249 type_entry
== FLOWSPEC_PKT_LEN
||
250 type_entry
== FLOWSPEC_FRAGMENT
) {
251 and_valmask
->val
= value
;
252 and_valmask
->mask
= 1; /* inverse */
254 } else if (unary_operator
== OPERATOR_UNARY_OR
&& or_valmask
) {
255 and_valmask
= XCALLOC(MTYPE_PBR_VALMASK
,
256 sizeof(struct bgp_pbr_val_mask
));
257 if (type_entry
== FLOWSPEC_TCP_FLAGS
) {
258 and_valmask
->val
= TCP_HEADER_ALL_FLAGS
;
260 TCP_HEADER_ALL_FLAGS
&
262 } else if (type_entry
== FLOWSPEC_DSCP
||
263 type_entry
== FLOWSPEC_FRAGMENT
||
264 type_entry
== FLOWSPEC_PKT_LEN
) {
265 and_valmask
->val
= value
;
266 and_valmask
->mask
= 1; /* inverse */
268 listnode_add(or_valmask
, and_valmask
);
269 } else if (type_entry
== FLOWSPEC_ICMP_CODE
||
270 type_entry
== FLOWSPEC_ICMP_TYPE
)
275 /* TCP : FIN and SYN -> val = ALL; mask = 3
276 * TCP : not (FIN and SYN) -> val = ALL; mask = ALL & ~(FIN|RST)
277 * other variables type: dscp, pkt len, fragment
278 * - value is copied in bgp_pbr_val_mask->val value
279 * - if negate form is identifierd, bgp_pbr_val_mask->mask set to 1
281 static bool bgp_pbr_extract_enumerate_unary(struct bgp_pbr_match_val list
[],
282 int num
, uint8_t unary_operator
,
283 void *valmask
, uint8_t type_entry
)
286 struct bgp_pbr_val_mask
*and_valmask
= NULL
;
287 struct list
*or_valmask
= NULL
;
291 if (unary_operator
== OPERATOR_UNARY_AND
) {
292 and_valmask
= (struct bgp_pbr_val_mask
*)valmask
;
293 memset(and_valmask
, 0, sizeof(struct bgp_pbr_val_mask
));
294 } else if (unary_operator
== OPERATOR_UNARY_OR
) {
295 or_valmask
= (struct list
*)valmask
;
298 for (i
= 0; i
< num
; i
++) {
299 if (i
!= 0 && list
[i
].unary_operator
!=
302 if (!(list
[i
].compare_operator
&
303 OPERATOR_COMPARE_EQUAL_TO
) &&
304 !(list
[i
].compare_operator
&
305 OPERATOR_COMPARE_EXACT_MATCH
)) {
306 if ((list
[i
].compare_operator
&
307 OPERATOR_COMPARE_LESS_THAN
) &&
308 (list
[i
].compare_operator
&
309 OPERATOR_COMPARE_GREATER_THAN
)) {
310 ret
= bgp_pbr_extract_enumerate_unary_opposite(
311 unary_operator
, and_valmask
,
312 or_valmask
, list
[i
].value
,
320 if (unary_operator
== OPERATOR_UNARY_AND
&& and_valmask
) {
321 if (type_entry
== FLOWSPEC_TCP_FLAGS
)
323 TCP_HEADER_ALL_FLAGS
& list
[i
].value
;
324 } else if (unary_operator
== OPERATOR_UNARY_OR
&& or_valmask
) {
325 and_valmask
= XCALLOC(MTYPE_PBR_VALMASK
,
326 sizeof(struct bgp_pbr_val_mask
));
327 if (type_entry
== FLOWSPEC_TCP_FLAGS
) {
328 and_valmask
->val
= TCP_HEADER_ALL_FLAGS
;
330 TCP_HEADER_ALL_FLAGS
& list
[i
].value
;
331 } else if (type_entry
== FLOWSPEC_DSCP
||
332 type_entry
== FLOWSPEC_ICMP_TYPE
||
333 type_entry
== FLOWSPEC_ICMP_CODE
||
334 type_entry
== FLOWSPEC_FRAGMENT
||
335 type_entry
== FLOWSPEC_PKT_LEN
)
336 and_valmask
->val
= list
[i
].value
;
337 listnode_add(or_valmask
, and_valmask
);
340 if (unary_operator
== OPERATOR_UNARY_AND
&& and_valmask
341 && type_entry
== FLOWSPEC_TCP_FLAGS
)
342 and_valmask
->val
= TCP_HEADER_ALL_FLAGS
;
346 /* if unary operator can either be UNARY_OR/AND/OR-AND.
347 * in the latter case, combinationf of both is not handled
349 static bool bgp_pbr_extract_enumerate(struct bgp_pbr_match_val list
[],
350 int num
, uint8_t unary_operator
,
351 void *valmask
, uint8_t type_entry
)
354 uint8_t unary_operator_val
;
355 bool double_check
= false;
357 if ((unary_operator
& OPERATOR_UNARY_OR
) &&
358 (unary_operator
& OPERATOR_UNARY_AND
)) {
359 unary_operator_val
= OPERATOR_UNARY_AND
;
362 unary_operator_val
= unary_operator
;
363 ret
= bgp_pbr_extract_enumerate_unary(list
, num
, unary_operator_val
,
364 valmask
, type_entry
);
365 if (!ret
&& double_check
)
366 ret
= bgp_pbr_extract_enumerate_unary(list
, num
,
373 /* returns the unary operator that is in the list
374 * return 0 if both operators are used
376 static uint8_t bgp_pbr_match_val_get_operator(struct bgp_pbr_match_val list
[],
381 uint8_t unary_operator
= OPERATOR_UNARY_AND
;
383 for (i
= 0; i
< num
; i
++) {
386 if (list
[i
].unary_operator
& OPERATOR_UNARY_OR
)
387 unary_operator
= OPERATOR_UNARY_OR
;
388 if ((list
[i
].unary_operator
& OPERATOR_UNARY_AND
389 && unary_operator
== OPERATOR_UNARY_OR
) ||
390 (list
[i
].unary_operator
& OPERATOR_UNARY_OR
391 && unary_operator
== OPERATOR_UNARY_AND
))
394 return unary_operator
;
398 /* return true if extraction ok
400 static bool bgp_pbr_extract(struct bgp_pbr_match_val list
[],
402 struct bgp_pbr_range_port
*range
)
405 bool exact_match
= false;
408 memset(range
, 0, sizeof(struct bgp_pbr_range_port
));
412 for (i
= 0; i
< num
; i
++) {
413 if (i
!= 0 && (list
[i
].compare_operator
==
414 OPERATOR_COMPARE_EQUAL_TO
))
416 if (i
== 0 && (list
[i
].compare_operator
==
417 OPERATOR_COMPARE_EQUAL_TO
)) {
419 range
->min_port
= list
[i
].value
;
422 if (exact_match
== true && i
> 0)
424 if (list
[i
].compare_operator
==
425 (OPERATOR_COMPARE_GREATER_THAN
+
426 OPERATOR_COMPARE_EQUAL_TO
)) {
428 range
->min_port
= list
[i
].value
;
429 } else if (list
[i
].compare_operator
==
430 (OPERATOR_COMPARE_LESS_THAN
+
431 OPERATOR_COMPARE_EQUAL_TO
)) {
433 range
->max_port
= list
[i
].value
;
434 } else if (list
[i
].compare_operator
==
435 OPERATOR_COMPARE_LESS_THAN
) {
437 range
->max_port
= list
[i
].value
- 1;
438 } else if (list
[i
].compare_operator
==
439 OPERATOR_COMPARE_GREATER_THAN
) {
441 range
->min_port
= list
[i
].value
+ 1;
447 static int bgp_pbr_validate_policy_route(struct bgp_pbr_entry_main
*api
)
449 bool enumerate_icmp
= false;
451 /* because bgp pbr entry may contain unsupported
452 * combinations, a message will be displayed here if
454 * for now, only match/set supported is
455 * - combination src/dst => redirect nexthop [ + rate]
456 * - combination src/dst => redirect VRF [ + rate]
457 * - combination src/dst => drop
458 * - combination srcport + @IP
460 if (api
->match_protocol_num
> 1) {
461 if (BGP_DEBUG(pbr
, PBR
))
462 zlog_debug("BGP: match protocol operations:"
463 "multiple protocols ( %d). ignoring.",
464 api
->match_protocol_num
);
467 if (api
->match_protocol_num
== 1 &&
468 api
->protocol
[0].value
!= PROTOCOL_UDP
&&
469 api
->protocol
[0].value
!= PROTOCOL_ICMP
&&
470 api
->protocol
[0].value
!= PROTOCOL_TCP
) {
471 if (BGP_DEBUG(pbr
, PBR
))
472 zlog_debug("BGP: match protocol operations:"
473 "protocol (%d) not supported. ignoring",
474 api
->match_protocol_num
);
477 if (!bgp_pbr_extract(api
->src_port
, api
->match_src_port_num
, NULL
)) {
478 if (BGP_DEBUG(pbr
, PBR
))
479 zlog_debug("BGP: match src port operations:"
480 "too complex. ignoring.");
483 if (!bgp_pbr_extract(api
->dst_port
, api
->match_dst_port_num
, NULL
)) {
484 if (BGP_DEBUG(pbr
, PBR
))
485 zlog_debug("BGP: match dst port operations:"
486 "too complex. ignoring.");
489 if (!bgp_pbr_extract_enumerate(api
->tcpflags
,
490 api
->match_tcpflags_num
,
492 OPERATOR_UNARY_OR
, NULL
,
493 FLOWSPEC_TCP_FLAGS
)) {
494 if (BGP_DEBUG(pbr
, PBR
))
495 zlog_debug("BGP: match tcp flags:"
496 "too complex. ignoring.");
499 if (!bgp_pbr_extract(api
->icmp_type
, api
->match_icmp_type_num
, NULL
)) {
500 if (!bgp_pbr_extract_enumerate(api
->icmp_type
,
501 api
->match_icmp_type_num
,
502 OPERATOR_UNARY_OR
, NULL
,
503 FLOWSPEC_ICMP_TYPE
)) {
504 if (BGP_DEBUG(pbr
, PBR
))
505 zlog_debug("BGP: match icmp type operations:"
506 "too complex. ignoring.");
509 enumerate_icmp
= true;
511 if (!bgp_pbr_extract(api
->icmp_code
, api
->match_icmp_code_num
, NULL
)) {
512 if (!bgp_pbr_extract_enumerate(api
->icmp_code
,
513 api
->match_icmp_code_num
,
514 OPERATOR_UNARY_OR
, NULL
,
515 FLOWSPEC_ICMP_CODE
)) {
516 if (BGP_DEBUG(pbr
, PBR
))
517 zlog_debug("BGP: match icmp code operations:"
518 "too complex. ignoring.");
520 } else if (api
->match_icmp_type_num
> 1 &&
521 enumerate_icmp
== false) {
522 if (BGP_DEBUG(pbr
, PBR
))
523 zlog_debug("BGP: match icmp code is enumerate"
524 ", and icmp type is not."
525 " too complex. ignoring.");
529 if (!bgp_pbr_extract(api
->port
, api
->match_port_num
, NULL
)) {
530 if (BGP_DEBUG(pbr
, PBR
))
531 zlog_debug("BGP: match port operations:"
532 "too complex. ignoring.");
535 if (api
->match_packet_length_num
) {
538 ret
= bgp_pbr_extract(api
->packet_length
,
539 api
->match_packet_length_num
, NULL
);
541 ret
= bgp_pbr_extract_enumerate(api
->packet_length
,
542 api
->match_packet_length_num
,
544 | OPERATOR_UNARY_AND
,
545 NULL
, FLOWSPEC_PKT_LEN
);
547 if (BGP_DEBUG(pbr
, PBR
))
548 zlog_debug("BGP: match packet length operations:"
549 "too complex. ignoring.");
553 if (api
->match_dscp_num
) {
554 if (!bgp_pbr_extract_enumerate(api
->dscp
, api
->match_dscp_num
,
555 OPERATOR_UNARY_OR
| OPERATOR_UNARY_AND
,
556 NULL
, FLOWSPEC_DSCP
)) {
557 if (BGP_DEBUG(pbr
, PBR
))
558 zlog_debug("BGP: match DSCP operations:"
559 "too complex. ignoring.");
563 if (api
->match_fragment_num
) {
567 success
= bgp_pbr_extract_enumerate(api
->fragment
,
568 api
->match_fragment_num
,
570 | OPERATOR_UNARY_AND
,
571 NULL
, FLOWSPEC_FRAGMENT
);
575 for (i
= 0; i
< api
->match_fragment_num
; i
++) {
576 if (api
->fragment
[i
].value
!= 1 &&
577 api
->fragment
[i
].value
!= 2 &&
578 api
->fragment
[i
].value
!= 4 &&
579 api
->fragment
[i
].value
!= 8) {
582 "Value not valid (%d) for this implementation",
583 api
->fragment
[i
].value
);
587 sprintf(fail_str
, "too complex. ignoring");
589 if (BGP_DEBUG(pbr
, PBR
))
590 zlog_debug("BGP: match fragment operation (%d) %s",
591 api
->match_fragment_num
,
597 /* no combinations with both src_port and dst_port
598 * or port with src_port and dst_port
600 if (api
->match_src_port_num
+ api
->match_dst_port_num
+
601 api
->match_port_num
> 3) {
602 if (BGP_DEBUG(pbr
, PBR
))
603 zlog_debug("BGP: match multiple port operations:"
604 " too complex. ignoring.");
607 if ((api
->match_src_port_num
|| api
->match_dst_port_num
608 || api
->match_port_num
) && (api
->match_icmp_type_num
609 || api
->match_icmp_code_num
)) {
610 if (BGP_DEBUG(pbr
, PBR
))
611 zlog_debug("BGP: match multiple port/imcp operations:"
612 " too complex. ignoring.");
615 if (!(api
->match_bitmask
& PREFIX_SRC_PRESENT
) &&
616 !(api
->match_bitmask
& PREFIX_DST_PRESENT
)) {
617 if (BGP_DEBUG(pbr
, PBR
)) {
618 bgp_pbr_print_policy_route(api
);
619 zlog_debug("BGP: match actions without src"
620 " or dst address can not operate."
628 /* return -1 if build or validation failed */
629 static int bgp_pbr_build_and_validate_entry(struct prefix
*p
,
630 struct bgp_path_info
*info
,
631 struct bgp_pbr_entry_main
*api
)
634 int i
, action_count
= 0;
635 struct ecommunity
*ecom
;
636 struct ecommunity_val
*ecom_eval
;
637 struct bgp_pbr_entry_action
*api_action
;
638 struct prefix
*src
= NULL
, *dst
= NULL
;
639 int valid_prefix
= 0;
642 /* extract match from flowspec entries */
643 ret
= bgp_flowspec_match_rules_fill((uint8_t *)p
->u
.prefix_flowspec
.ptr
,
644 p
->u
.prefix_flowspec
.prefixlen
, api
);
647 /* extract actiosn from flowspec ecom list */
648 if (info
&& info
->attr
&& info
->attr
->ecommunity
) {
649 ecom
= info
->attr
->ecommunity
;
650 for (i
= 0; i
< ecom
->size
; i
++) {
651 ecom_eval
= (struct ecommunity_val
*)
652 (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
654 if (action_count
> ACTIONS_MAX_NUM
) {
655 if (BGP_DEBUG(pbr
, PBR_ERROR
))
657 EC_BGP_FLOWSPEC_PACKET
,
658 "%s: flowspec actions exceeds limit (max %u)",
659 __func__
, action_count
);
662 api_action
= &api
->actions
[action_count
- 1];
664 if ((ecom_eval
->val
[1] ==
665 (char)ECOMMUNITY_REDIRECT_VRF
) &&
666 (ecom_eval
->val
[0] ==
667 (char)ECOMMUNITY_ENCODE_TRANS_EXP
||
669 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_2
||
671 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_3
)) {
672 struct ecommunity
*eckey
= ecommunity_new();
673 struct ecommunity_val ecom_copy
;
675 memcpy(&ecom_copy
, ecom_eval
,
676 sizeof(struct ecommunity_val
));
678 ~ECOMMUNITY_ENCODE_TRANS_EXP
;
679 ecom_copy
.val
[1] = ECOMMUNITY_ROUTE_TARGET
;
680 ecommunity_add_val(eckey
, &ecom_copy
);
682 api_action
->action
= ACTION_REDIRECT
;
683 api_action
->u
.redirect_vrf
=
684 get_first_vrf_for_redirect_with_rt(
686 ecommunity_free(&eckey
);
687 } else if ((ecom_eval
->val
[0] ==
688 (char)ECOMMUNITY_ENCODE_REDIRECT_IP_NH
) &&
689 (ecom_eval
->val
[1] ==
690 (char)ECOMMUNITY_REDIRECT_IP_NH
)) {
691 api_action
->action
= ACTION_REDIRECT_IP
;
692 api_action
->u
.zr
.redirect_ip_v4
.s_addr
=
693 info
->attr
->nexthop
.s_addr
;
694 api_action
->u
.zr
.duplicate
= ecom_eval
->val
[7];
696 if (ecom_eval
->val
[0] !=
697 (char)ECOMMUNITY_ENCODE_TRANS_EXP
)
699 ret
= ecommunity_fill_pbr_action(ecom_eval
,
708 /* validate if incoming matc/action is compatible
709 * with our policy routing engine
711 if (!bgp_pbr_validate_policy_route(api
))
714 /* check inconsistency in the match rule */
715 if (api
->match_bitmask
& PREFIX_SRC_PRESENT
) {
716 src
= &api
->src_prefix
;
717 afi
= family2afi(src
->family
);
720 if (api
->match_bitmask
& PREFIX_DST_PRESENT
) {
721 dst
= &api
->dst_prefix
;
722 if (valid_prefix
&& afi
!= family2afi(dst
->family
)) {
723 if (BGP_DEBUG(pbr
, PBR
)) {
724 bgp_pbr_print_policy_route(api
);
725 zlog_debug("%s: inconsistency:"
726 " no match for afi src and dst (%u/%u)",
727 __func__
, afi
, family2afi(dst
->family
));
735 static void bgp_pbr_match_entry_free(void *arg
)
737 struct bgp_pbr_match_entry
*bpme
;
739 bpme
= (struct bgp_pbr_match_entry
*)arg
;
741 if (bpme
->installed
) {
742 bgp_send_pbr_ipset_entry_match(bpme
, false);
743 bpme
->installed
= false;
744 bpme
->backpointer
= NULL
;
746 XFREE(MTYPE_PBR_MATCH_ENTRY
, bpme
);
749 static void bgp_pbr_match_free(void *arg
)
751 struct bgp_pbr_match
*bpm
;
753 bpm
= (struct bgp_pbr_match
*)arg
;
755 hash_clean(bpm
->entry_hash
, bgp_pbr_match_entry_free
);
757 if (hashcount(bpm
->entry_hash
) == 0) {
758 /* delete iptable entry first */
759 /* then delete ipset match */
760 if (bpm
->installed
) {
761 if (bpm
->installed_in_iptable
) {
762 bgp_send_pbr_iptable(bpm
->action
,
764 bpm
->installed_in_iptable
= false;
765 bpm
->action
->refcnt
--;
767 bgp_send_pbr_ipset_match(bpm
, false);
768 bpm
->installed
= false;
772 hash_free(bpm
->entry_hash
);
774 XFREE(MTYPE_PBR_MATCH
, bpm
);
777 static void *bgp_pbr_match_alloc_intern(void *arg
)
779 struct bgp_pbr_match
*bpm
, *new;
781 bpm
= (struct bgp_pbr_match
*)arg
;
783 new = XCALLOC(MTYPE_PBR_MATCH
, sizeof(*new));
784 memcpy(new, bpm
, sizeof(*bpm
));
789 static void bgp_pbr_action_free(void *arg
)
791 struct bgp_pbr_action
*bpa
;
793 bpa
= (struct bgp_pbr_action
*)arg
;
795 if (bpa
->refcnt
== 0) {
796 if (bpa
->installed
&& bpa
->table_id
!= 0) {
797 bgp_send_pbr_rule_action(bpa
, false);
798 bgp_zebra_announce_default(bpa
->bgp
, &(bpa
->nh
),
802 bpa
->installed
= false;
805 XFREE(MTYPE_PBR_ACTION
, bpa
);
808 static void *bgp_pbr_action_alloc_intern(void *arg
)
810 struct bgp_pbr_action
*bpa
, *new;
812 bpa
= (struct bgp_pbr_action
*)arg
;
814 new = XCALLOC(MTYPE_PBR_ACTION
, sizeof(*new));
816 memcpy(new, bpa
, sizeof(*bpa
));
821 static void *bgp_pbr_match_entry_alloc_intern(void *arg
)
823 struct bgp_pbr_match_entry
*bpme
, *new;
825 bpme
= (struct bgp_pbr_match_entry
*)arg
;
827 new = XCALLOC(MTYPE_PBR_MATCH_ENTRY
, sizeof(*new));
829 memcpy(new, bpme
, sizeof(*bpme
));
834 uint32_t bgp_pbr_match_hash_key(void *arg
)
836 struct bgp_pbr_match
*pbm
= (struct bgp_pbr_match
*)arg
;
839 key
= jhash_1word(pbm
->vrf_id
, 0x4312abde);
840 key
= jhash_1word(pbm
->flags
, key
);
841 key
= jhash(&pbm
->pkt_len_min
, 2, key
);
842 key
= jhash(&pbm
->pkt_len_max
, 2, key
);
843 key
= jhash(&pbm
->tcp_flags
, 2, key
);
844 key
= jhash(&pbm
->tcp_mask_flags
, 2, key
);
845 key
= jhash(&pbm
->dscp_value
, 1, key
);
846 key
= jhash(&pbm
->fragment
, 1, key
);
847 return jhash_1word(pbm
->type
, key
);
850 int bgp_pbr_match_hash_equal(const void *arg1
, const void *arg2
)
852 const struct bgp_pbr_match
*r1
, *r2
;
854 r1
= (const struct bgp_pbr_match
*)arg1
;
855 r2
= (const struct bgp_pbr_match
*)arg2
;
857 if (r1
->vrf_id
!= r2
->vrf_id
)
860 if (r1
->type
!= r2
->type
)
863 if (r1
->flags
!= r2
->flags
)
866 if (r1
->action
!= r2
->action
)
869 if (r1
->pkt_len_min
!= r2
->pkt_len_min
)
872 if (r1
->pkt_len_max
!= r2
->pkt_len_max
)
875 if (r1
->tcp_flags
!= r2
->tcp_flags
)
878 if (r1
->tcp_mask_flags
!= r2
->tcp_mask_flags
)
881 if (r1
->dscp_value
!= r2
->dscp_value
)
884 if (r1
->fragment
!= r2
->fragment
)
889 uint32_t bgp_pbr_match_entry_hash_key(void *arg
)
891 struct bgp_pbr_match_entry
*pbme
;
894 pbme
= (struct bgp_pbr_match_entry
*)arg
;
895 key
= prefix_hash_key(&pbme
->src
);
896 key
= jhash_1word(prefix_hash_key(&pbme
->dst
), key
);
897 key
= jhash(&pbme
->dst_port_min
, 2, key
);
898 key
= jhash(&pbme
->src_port_min
, 2, key
);
899 key
= jhash(&pbme
->dst_port_max
, 2, key
);
900 key
= jhash(&pbme
->src_port_max
, 2, key
);
901 key
= jhash(&pbme
->proto
, 1, key
);
906 int bgp_pbr_match_entry_hash_equal(const void *arg1
, const void *arg2
)
908 const struct bgp_pbr_match_entry
*r1
, *r2
;
910 r1
= (const struct bgp_pbr_match_entry
*)arg1
;
911 r2
= (const struct bgp_pbr_match_entry
*)arg2
;
913 /* on updates, comparing
914 * backpointer is not necessary
917 /* unique value is self calculated
920 /* rate is ignored for now
923 if (!prefix_same(&r1
->src
, &r2
->src
))
926 if (!prefix_same(&r1
->dst
, &r2
->dst
))
929 if (r1
->src_port_min
!= r2
->src_port_min
)
932 if (r1
->dst_port_min
!= r2
->dst_port_min
)
935 if (r1
->src_port_max
!= r2
->src_port_max
)
938 if (r1
->dst_port_max
!= r2
->dst_port_max
)
941 if (r1
->proto
!= r2
->proto
)
947 uint32_t bgp_pbr_action_hash_key(void *arg
)
949 struct bgp_pbr_action
*pbra
;
952 pbra
= (struct bgp_pbr_action
*)arg
;
953 key
= jhash_1word(pbra
->table_id
, 0x4312abde);
954 key
= jhash_1word(pbra
->fwmark
, key
);
958 int bgp_pbr_action_hash_equal(const void *arg1
, const void *arg2
)
960 const struct bgp_pbr_action
*r1
, *r2
;
962 r1
= (const struct bgp_pbr_action
*)arg1
;
963 r2
= (const struct bgp_pbr_action
*)arg2
;
965 /* unique value is self calculated
966 * table and fwmark is self calculated
969 if (r1
->vrf_id
!= r2
->vrf_id
)
972 if (memcmp(&r1
->nh
, &r2
->nh
, sizeof(struct nexthop
)))
977 struct bgp_pbr_action
*bgp_pbr_action_rule_lookup(vrf_id_t vrf_id
,
980 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
981 struct bgp_pbr_action_unique bpau
;
983 if (!bgp
|| unique
== 0)
985 bpau
.unique
= unique
;
986 bpau
.bpa_found
= NULL
;
987 hash_walk(bgp
->pbr_action_hash
, bgp_pbr_action_walkcb
, &bpau
);
988 return bpau
.bpa_found
;
991 struct bgp_pbr_match
*bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id
,
994 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
995 struct bgp_pbr_match_unique bpmu
;
997 if (!bgp
|| unique
== 0)
999 bpmu
.unique
= unique
;
1000 bpmu
.bpm_found
= NULL
;
1001 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_match_walkcb
, &bpmu
);
1002 return bpmu
.bpm_found
;
1005 struct bgp_pbr_match_entry
*bgp_pbr_match_ipset_entry_lookup(vrf_id_t vrf_id
,
1009 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1010 struct bgp_pbr_match_entry_unique bpmeu
;
1011 struct bgp_pbr_match_ipsetname bpmi
;
1013 if (!bgp
|| unique
== 0)
1015 bpmi
.ipsetname
= XCALLOC(MTYPE_TMP
, ZEBRA_IPSET_NAME_SIZE
);
1016 snprintf(bpmi
.ipsetname
, ZEBRA_IPSET_NAME_SIZE
, "%s", ipset_name
);
1017 bpmi
.bpm_found
= NULL
;
1018 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_match_pername_walkcb
, &bpmi
);
1019 XFREE(MTYPE_TMP
, bpmi
.ipsetname
);
1020 if (!bpmi
.bpm_found
)
1022 bpmeu
.bpme_found
= NULL
;
1023 bpmeu
.unique
= unique
;
1024 hash_walk(bpmi
.bpm_found
->entry_hash
,
1025 bgp_pbr_match_entry_walkcb
, &bpmeu
);
1026 return bpmeu
.bpme_found
;
1029 struct bgp_pbr_match
*bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id
,
1032 struct bgp
*bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1033 struct bgp_pbr_match_iptable_unique bpmiu
;
1035 if (!bgp
|| unique
== 0)
1037 bpmiu
.unique
= unique
;
1038 bpmiu
.bpm_found
= NULL
;
1039 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_match_iptable_walkcb
, &bpmiu
);
1040 return bpmiu
.bpm_found
;
1043 void bgp_pbr_cleanup(struct bgp
*bgp
)
1045 if (bgp
->pbr_match_hash
) {
1046 hash_clean(bgp
->pbr_match_hash
, bgp_pbr_match_free
);
1047 hash_free(bgp
->pbr_match_hash
);
1048 bgp
->pbr_match_hash
= NULL
;
1050 if (bgp
->pbr_action_hash
) {
1051 hash_clean(bgp
->pbr_action_hash
, bgp_pbr_action_free
);
1052 hash_free(bgp
->pbr_action_hash
);
1053 bgp
->pbr_action_hash
= NULL
;
1055 if (bgp
->bgp_pbr_cfg
== NULL
)
1057 bgp_pbr_reset(bgp
, AFI_IP
);
1058 XFREE(MTYPE_PBR
, bgp
->bgp_pbr_cfg
);
1059 bgp
->bgp_pbr_cfg
= NULL
;
1062 void bgp_pbr_init(struct bgp
*bgp
)
1064 bgp
->pbr_match_hash
=
1065 hash_create_size(8, bgp_pbr_match_hash_key
,
1066 bgp_pbr_match_hash_equal
,
1068 bgp
->pbr_action_hash
=
1069 hash_create_size(8, bgp_pbr_action_hash_key
,
1070 bgp_pbr_action_hash_equal
,
1071 "Match Hash Entry");
1073 bgp
->bgp_pbr_cfg
= XCALLOC(MTYPE_PBR
, sizeof(struct bgp_pbr_config
));
1074 bgp
->bgp_pbr_cfg
->pbr_interface_any_ipv4
= true;
1077 void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main
*api
)
1080 char return_string
[512];
1081 char *ptr
= return_string
;
1085 ptr
+= sprintf(ptr
, "MATCH : ");
1086 if (api
->match_bitmask
& PREFIX_SRC_PRESENT
) {
1087 struct prefix
*p
= &(api
->src_prefix
);
1089 ptr
+= sprintf(ptr
, "@src %s", prefix2str(p
, buff
, 64));
1090 INCREMENT_DISPLAY(ptr
, nb_items
);
1092 if (api
->match_bitmask
& PREFIX_DST_PRESENT
) {
1093 struct prefix
*p
= &(api
->dst_prefix
);
1095 INCREMENT_DISPLAY(ptr
, nb_items
);
1096 ptr
+= sprintf(ptr
, "@dst %s", prefix2str(p
, buff
, 64));
1099 if (api
->match_protocol_num
)
1100 INCREMENT_DISPLAY(ptr
, nb_items
);
1101 for (i
= 0; i
< api
->match_protocol_num
; i
++)
1102 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->protocol
[i
],
1103 i
> 0 ? NULL
: "@proto ");
1105 if (api
->match_src_port_num
)
1106 INCREMENT_DISPLAY(ptr
, nb_items
);
1107 for (i
= 0; i
< api
->match_src_port_num
; i
++)
1108 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->src_port
[i
],
1109 i
> 0 ? NULL
: "@srcport ");
1111 if (api
->match_dst_port_num
)
1112 INCREMENT_DISPLAY(ptr
, nb_items
);
1113 for (i
= 0; i
< api
->match_dst_port_num
; i
++)
1114 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->dst_port
[i
],
1115 i
> 0 ? NULL
: "@dstport ");
1117 if (api
->match_port_num
)
1118 INCREMENT_DISPLAY(ptr
, nb_items
);
1119 for (i
= 0; i
< api
->match_port_num
; i
++)
1120 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->port
[i
],
1121 i
> 0 ? NULL
: "@port ");
1123 if (api
->match_icmp_type_num
)
1124 INCREMENT_DISPLAY(ptr
, nb_items
);
1125 for (i
= 0; i
< api
->match_icmp_type_num
; i
++)
1126 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->icmp_type
[i
],
1127 i
> 0 ? NULL
: "@icmptype ");
1129 if (api
->match_icmp_code_num
)
1130 INCREMENT_DISPLAY(ptr
, nb_items
);
1131 for (i
= 0; i
< api
->match_icmp_code_num
; i
++)
1132 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->icmp_code
[i
],
1133 i
> 0 ? NULL
: "@icmpcode ");
1135 if (api
->match_packet_length_num
)
1136 INCREMENT_DISPLAY(ptr
, nb_items
);
1137 for (i
= 0; i
< api
->match_packet_length_num
; i
++)
1138 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->packet_length
[i
],
1139 i
> 0 ? NULL
: "@plen ");
1141 if (api
->match_dscp_num
)
1142 INCREMENT_DISPLAY(ptr
, nb_items
);
1143 for (i
= 0; i
< api
->match_dscp_num
; i
++)
1144 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->dscp
[i
],
1145 i
> 0 ? NULL
: "@dscp ");
1147 if (api
->match_tcpflags_num
)
1148 INCREMENT_DISPLAY(ptr
, nb_items
);
1149 for (i
= 0; i
< api
->match_tcpflags_num
; i
++)
1150 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->tcpflags
[i
],
1151 i
> 0 ? NULL
: "@tcpflags ");
1153 if (api
->match_fragment_num
)
1154 INCREMENT_DISPLAY(ptr
, nb_items
);
1155 for (i
= 0; i
< api
->match_fragment_num
; i
++)
1156 ptr
+= sprintf_bgp_pbr_match_val(ptr
, &api
->fragment
[i
],
1157 i
> 0 ? NULL
: "@fragment ");
1159 ptr
= return_string
;
1161 ptr
+= sprintf(ptr
, "; ");
1162 if (api
->action_num
)
1163 ptr
+= sprintf(ptr
, "SET : ");
1165 for (i
= 0; i
< api
->action_num
; i
++) {
1166 switch (api
->actions
[i
].action
) {
1167 case ACTION_TRAFFICRATE
:
1168 INCREMENT_DISPLAY(ptr
, nb_items
);
1169 ptr
+= sprintf(ptr
, "@set rate %f",
1170 api
->actions
[i
].u
.r
.rate
);
1172 case ACTION_TRAFFIC_ACTION
:
1173 INCREMENT_DISPLAY(ptr
, nb_items
);
1174 ptr
+= sprintf(ptr
, "@action ");
1175 if (api
->actions
[i
].u
.za
.filter
1176 & TRAFFIC_ACTION_TERMINATE
)
1178 " terminate (apply filter(s))");
1179 if (api
->actions
[i
].u
.za
.filter
1180 & TRAFFIC_ACTION_DISTRIBUTE
)
1181 ptr
+= sprintf(ptr
, " distribute");
1182 if (api
->actions
[i
].u
.za
.filter
1183 & TRAFFIC_ACTION_SAMPLE
)
1184 ptr
+= sprintf(ptr
, " sample");
1186 case ACTION_REDIRECT_IP
:
1187 INCREMENT_DISPLAY(ptr
, nb_items
);
1188 char local_buff
[INET_ADDRSTRLEN
];
1190 if (inet_ntop(AF_INET
,
1191 &api
->actions
[i
].u
.zr
.redirect_ip_v4
,
1192 local_buff
, INET_ADDRSTRLEN
) != NULL
)
1194 "@redirect ip nh %s", local_buff
);
1196 case ACTION_REDIRECT
:
1197 INCREMENT_DISPLAY(ptr
, nb_items
);
1198 ptr
+= sprintf(ptr
, "@redirect vrf %u",
1199 api
->actions
[i
].u
.redirect_vrf
);
1201 case ACTION_MARKING
:
1202 INCREMENT_DISPLAY(ptr
, nb_items
);
1203 ptr
+= sprintf(ptr
, "@set dscp %u",
1204 api
->actions
[i
].u
.marking_dscp
);
1210 zlog_info("%s", return_string
);
1213 static void bgp_pbr_flush_entry(struct bgp
*bgp
, struct bgp_pbr_action
*bpa
,
1214 struct bgp_pbr_match
*bpm
,
1215 struct bgp_pbr_match_entry
*bpme
)
1217 /* if bpme is null, bpm is also null
1221 /* ipset del entry */
1222 if (bpme
->installed
) {
1223 bgp_send_pbr_ipset_entry_match(bpme
, false);
1224 bpme
->installed
= false;
1225 bpme
->backpointer
= NULL
;
1227 struct bgp_path_info
*path
;
1228 struct bgp_path_info_extra
*extra
;
1230 /* unlink bgp_path_info to bpme */
1231 path
= (struct bgp_path_info
*)bpme
->path
;
1232 extra
= bgp_path_info_extra_get(path
);
1233 if (extra
->bgp_fs_pbr
)
1234 listnode_delete(extra
->bgp_fs_pbr
, bpme
);
1238 hash_release(bpm
->entry_hash
, bpme
);
1239 if (hashcount(bpm
->entry_hash
) == 0) {
1240 /* delete iptable entry first */
1241 /* then delete ipset match */
1242 if (bpm
->installed
) {
1243 if (bpm
->installed_in_iptable
) {
1244 bgp_send_pbr_iptable(bpm
->action
,
1246 bpm
->installed_in_iptable
= false;
1247 bpm
->action
->refcnt
--;
1249 bgp_send_pbr_ipset_match(bpm
, false);
1250 bpm
->installed
= false;
1253 hash_release(bgp
->pbr_match_hash
, bpm
);
1254 /* XXX release pbr_match_action if not used
1255 * note that drop does not need to call send_pbr_action
1258 if (bpa
->refcnt
== 0) {
1259 if (bpa
->installed
&& bpa
->table_id
!= 0) {
1260 bgp_send_pbr_rule_action(bpa
, false);
1261 bgp_zebra_announce_default(bpa
->bgp
, &(bpa
->nh
),
1265 bpa
->installed
= false;
1270 struct bgp_pbr_match_entry_remain
{
1271 struct bgp_pbr_match_entry
*bpme_to_match
;
1272 struct bgp_pbr_match_entry
*bpme_found
;
1275 static int bgp_pbr_get_remaining_entry(struct hash_backet
*backet
, void *arg
)
1277 struct bgp_pbr_match
*bpm
= (struct bgp_pbr_match
*)backet
->data
;
1278 struct bgp_pbr_match_entry_remain
*bpmer
=
1279 (struct bgp_pbr_match_entry_remain
*)arg
;
1280 struct bgp_pbr_match
*bpm_temp
;
1281 struct bgp_pbr_match_entry
*bpme
= bpmer
->bpme_to_match
;
1283 if (!bpme
->backpointer
||
1284 bpm
== bpme
->backpointer
||
1285 bpme
->backpointer
->action
== bpm
->action
)
1286 return HASHWALK_CONTINUE
;
1287 /* ensure bpm other characteristics are equal */
1288 bpm_temp
= bpme
->backpointer
;
1289 if (bpm_temp
->vrf_id
!= bpm
->vrf_id
||
1290 bpm_temp
->type
!= bpm
->type
||
1291 bpm_temp
->flags
!= bpm
->flags
||
1292 bpm_temp
->tcp_flags
!= bpm
->tcp_flags
||
1293 bpm_temp
->tcp_mask_flags
!= bpm
->tcp_mask_flags
||
1294 bpm_temp
->pkt_len_min
!= bpm
->pkt_len_min
||
1295 bpm_temp
->pkt_len_max
!= bpm
->pkt_len_max
||
1296 bpm_temp
->dscp_value
!= bpm
->dscp_value
||
1297 bpm_temp
->fragment
!= bpm
->fragment
)
1298 return HASHWALK_CONTINUE
;
1300 /* look for remaining bpme */
1301 bpmer
->bpme_found
= hash_lookup(bpm
->entry_hash
, bpme
);
1302 if (!bpmer
->bpme_found
)
1303 return HASHWALK_CONTINUE
;
1304 return HASHWALK_ABORT
;
1307 static void bgp_pbr_policyroute_remove_from_zebra_unit(
1308 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
)
1310 struct bgp_pbr_match temp
;
1311 struct bgp_pbr_match_entry temp2
;
1312 struct bgp_pbr_match
*bpm
;
1313 struct bgp_pbr_match_entry
*bpme
;
1314 struct bgp_pbr_match_entry_remain bpmer
;
1315 struct bgp_pbr_range_port
*src_port
;
1316 struct bgp_pbr_range_port
*dst_port
;
1317 struct bgp_pbr_range_port
*pkt_len
;
1321 src_port
= bpf
->src_port
;
1322 dst_port
= bpf
->dst_port
;
1323 pkt_len
= bpf
->pkt_len
;
1325 if (BGP_DEBUG(zebra
, ZEBRA
))
1326 bgp_pbr_dump_entry(bpf
, false);
1328 /* as we don't know information from EC
1329 * look for bpm that have the bpm
1330 * with vrf_id characteristics
1332 memset(&temp2
, 0, sizeof(temp2
));
1333 memset(&temp
, 0, sizeof(temp
));
1335 temp
.flags
|= MATCH_IP_SRC_SET
;
1336 prefix_copy(&temp2
.src
, bpf
->src
);
1338 temp2
.src
.family
= AF_INET
;
1340 temp
.flags
|= MATCH_IP_DST_SET
;
1341 prefix_copy(&temp2
.dst
, bpf
->dst
);
1343 temp2
.dst
.family
= AF_INET
;
1344 if (src_port
&& (src_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
1345 if (bpf
->protocol
== IPPROTO_ICMP
)
1346 temp
.flags
|= MATCH_ICMP_SET
;
1347 temp
.flags
|= MATCH_PORT_SRC_SET
;
1348 temp2
.src_port_min
= src_port
->min_port
;
1349 if (src_port
->max_port
) {
1350 temp
.flags
|= MATCH_PORT_SRC_RANGE_SET
;
1351 temp2
.src_port_max
= src_port
->max_port
;
1354 if (dst_port
&& (dst_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
1355 if (bpf
->protocol
== IPPROTO_ICMP
)
1356 temp
.flags
|= MATCH_ICMP_SET
;
1357 temp
.flags
|= MATCH_PORT_DST_SET
;
1358 temp2
.dst_port_min
= dst_port
->min_port
;
1359 if (dst_port
->max_port
) {
1360 temp
.flags
|= MATCH_PORT_DST_RANGE_SET
;
1361 temp2
.dst_port_max
= dst_port
->max_port
;
1364 temp2
.proto
= bpf
->protocol
;
1367 temp
.pkt_len_min
= pkt_len
->min_port
;
1368 if (pkt_len
->max_port
)
1369 temp
.pkt_len_max
= pkt_len
->max_port
;
1370 } else if (bpf
->pkt_len_val
) {
1371 if (bpf
->pkt_len_val
->mask
)
1372 temp
.flags
|= MATCH_PKT_LEN_INVERSE_SET
;
1373 temp
.pkt_len_min
= bpf
->pkt_len_val
->val
;
1375 if (bpf
->tcp_flags
) {
1376 temp
.tcp_flags
= bpf
->tcp_flags
->val
;
1377 temp
.tcp_mask_flags
= bpf
->tcp_flags
->mask
;
1380 if (bpf
->dscp
->mask
)
1381 temp
.flags
|= MATCH_DSCP_INVERSE_SET
;
1383 temp
.flags
|= MATCH_DSCP_SET
;
1384 temp
.dscp_value
= bpf
->dscp
->val
;
1386 if (bpf
->fragment
) {
1387 if (bpf
->fragment
->mask
)
1388 temp
.flags
|= MATCH_FRAGMENT_INVERSE_SET
;
1389 temp
.fragment
= bpf
->fragment
->val
;
1392 if (bpf
->src
== NULL
|| bpf
->dst
== NULL
) {
1393 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
1394 temp
.type
= IPSET_NET_PORT
;
1396 temp
.type
= IPSET_NET
;
1398 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
1399 temp
.type
= IPSET_NET_PORT_NET
;
1401 temp
.type
= IPSET_NET_NET
;
1403 if (bpf
->vrf_id
== VRF_UNKNOWN
) /* XXX case BGP destroy */
1406 temp
.vrf_id
= bpf
->vrf_id
;
1409 bpme
->backpointer
= bpm
;
1410 /* right now, a previous entry may already exist
1411 * flush previous entry if necessary
1413 bpmer
.bpme_to_match
= bpme
;
1414 bpmer
.bpme_found
= NULL
;
1415 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_get_remaining_entry
, &bpmer
);
1416 if (bpmer
.bpme_found
) {
1417 static struct bgp_pbr_match
*local_bpm
;
1418 static struct bgp_pbr_action
*local_bpa
;
1420 local_bpm
= bpmer
.bpme_found
->backpointer
;
1421 local_bpa
= local_bpm
->action
;
1422 bgp_pbr_flush_entry(bgp
, local_bpa
,
1423 local_bpm
, bpmer
.bpme_found
);
1427 static uint8_t bgp_pbr_next_type_entry(uint8_t type_entry
)
1429 if (type_entry
== FLOWSPEC_TCP_FLAGS
)
1430 return FLOWSPEC_DSCP
;
1431 if (type_entry
== FLOWSPEC_DSCP
)
1432 return FLOWSPEC_PKT_LEN
;
1433 if (type_entry
== FLOWSPEC_PKT_LEN
)
1434 return FLOWSPEC_FRAGMENT
;
1435 if (type_entry
== FLOWSPEC_FRAGMENT
)
1436 return FLOWSPEC_ICMP_TYPE
;
1440 static void bgp_pbr_icmp_action(struct bgp
*bgp
, struct bgp_path_info
*path
,
1441 struct bgp_pbr_filter
*bpf
,
1442 struct bgp_pbr_or_filter
*bpof
, bool add
,
1443 struct nexthop
*nh
, float *rate
)
1445 struct bgp_pbr_range_port srcp
, dstp
;
1446 struct bgp_pbr_val_mask
*icmp_type
, *icmp_code
;
1447 struct listnode
*tnode
, *cnode
;
1451 if (bpf
->protocol
!= IPPROTO_ICMP
)
1453 bpf
->src_port
= &srcp
;
1454 bpf
->dst_port
= &dstp
;
1455 /* parse icmp type and lookup appropriate icmp code
1456 * if no icmp code found, create as many entryes as
1457 * there are listed icmp codes for that icmp type
1459 if (!bpof
->icmp_type
) {
1461 srcp
.max_port
= 255;
1462 for (ALL_LIST_ELEMENTS_RO(bpof
->icmp_code
, cnode
, icmp_code
)) {
1463 dstp
.min_port
= icmp_code
->val
;
1465 bgp_pbr_policyroute_add_to_zebra_unit(
1466 bgp
, path
, bpf
, nh
, rate
);
1468 bgp_pbr_policyroute_remove_from_zebra_unit(
1473 for (ALL_LIST_ELEMENTS_RO(bpof
->icmp_type
, tnode
, icmp_type
)) {
1474 srcp
.min_port
= icmp_type
->val
;
1477 /* only icmp type. create an entry only with icmp type */
1478 if (!bpof
->icmp_code
) {
1479 /* icmp type is not one of the above
1480 * forge an entry only based on the icmp type
1483 dstp
.max_port
= 255;
1485 bgp_pbr_policyroute_add_to_zebra_unit(
1486 bgp
, path
, bpf
, nh
, rate
);
1488 bgp_pbr_policyroute_remove_from_zebra_unit(
1492 for (ALL_LIST_ELEMENTS_RO(bpof
->icmp_code
, cnode
, icmp_code
)) {
1493 dstp
.min_port
= icmp_code
->val
;
1495 bgp_pbr_policyroute_add_to_zebra_unit(
1496 bgp
, path
, bpf
, nh
, rate
);
1498 bgp_pbr_policyroute_remove_from_zebra_unit(
1504 static void bgp_pbr_policyroute_remove_from_zebra_recursive(
1505 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
,
1506 struct bgp_pbr_or_filter
*bpof
, uint8_t type_entry
)
1508 struct listnode
*node
, *nnode
;
1509 struct bgp_pbr_val_mask
*valmask
;
1510 uint8_t next_type_entry
;
1511 struct list
*orig_list
;
1512 struct bgp_pbr_val_mask
**target_val
;
1514 if (type_entry
== 0)
1515 return bgp_pbr_policyroute_remove_from_zebra_unit(bgp
, path
,
1517 next_type_entry
= bgp_pbr_next_type_entry(type_entry
);
1518 if (type_entry
== FLOWSPEC_TCP_FLAGS
&& bpof
->tcpflags
) {
1519 orig_list
= bpof
->tcpflags
;
1520 target_val
= &bpf
->tcp_flags
;
1521 } else if (type_entry
== FLOWSPEC_DSCP
&& bpof
->dscp
) {
1522 orig_list
= bpof
->dscp
;
1523 target_val
= &bpf
->dscp
;
1524 } else if (type_entry
== FLOWSPEC_PKT_LEN
&& bpof
->pkt_len
) {
1525 orig_list
= bpof
->pkt_len
;
1526 target_val
= &bpf
->pkt_len_val
;
1527 } else if (type_entry
== FLOWSPEC_FRAGMENT
&& bpof
->fragment
) {
1528 orig_list
= bpof
->fragment
;
1529 target_val
= &bpf
->fragment
;
1530 } else if (type_entry
== FLOWSPEC_ICMP_TYPE
&&
1531 (bpof
->icmp_type
|| bpof
->icmp_code
)) {
1532 /* enumerate list for icmp - must be last one */
1533 bgp_pbr_icmp_action(bgp
, path
, bpf
, bpof
, false, NULL
, NULL
);
1536 return bgp_pbr_policyroute_remove_from_zebra_recursive(
1537 bgp
, path
, bpf
, bpof
, next_type_entry
);
1539 for (ALL_LIST_ELEMENTS(orig_list
, node
, nnode
, valmask
)) {
1540 *target_val
= valmask
;
1541 bgp_pbr_policyroute_remove_from_zebra_recursive(
1542 bgp
, path
, bpf
, bpof
, next_type_entry
);
1546 static void bgp_pbr_policyroute_remove_from_zebra(
1547 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
,
1548 struct bgp_pbr_or_filter
*bpof
)
1551 return bgp_pbr_policyroute_remove_from_zebra_unit(bgp
, path
,
1554 bgp_pbr_policyroute_remove_from_zebra_recursive(
1555 bgp
, path
, bpf
, bpof
, FLOWSPEC_TCP_FLAGS
);
1556 else if (bpof
->dscp
)
1557 bgp_pbr_policyroute_remove_from_zebra_recursive(
1558 bgp
, path
, bpf
, bpof
, FLOWSPEC_DSCP
);
1559 else if (bpof
->pkt_len
)
1560 bgp_pbr_policyroute_remove_from_zebra_recursive(
1561 bgp
, path
, bpf
, bpof
, FLOWSPEC_PKT_LEN
);
1562 else if (bpof
->fragment
)
1563 bgp_pbr_policyroute_remove_from_zebra_recursive(
1564 bgp
, path
, bpf
, bpof
, FLOWSPEC_FRAGMENT
);
1565 else if (bpof
->icmp_type
|| bpof
->icmp_code
)
1566 bgp_pbr_policyroute_remove_from_zebra_recursive(
1567 bgp
, path
, bpf
, bpof
, FLOWSPEC_ICMP_TYPE
);
1569 bgp_pbr_policyroute_remove_from_zebra_unit(bgp
, path
, bpf
);
1572 list_delete_all_node(bpof
->tcpflags
);
1574 list_delete_all_node(bpof
->dscp
);
1576 list_delete_all_node(bpof
->pkt_len
);
1578 list_delete_all_node(bpof
->fragment
);
1581 static void bgp_pbr_dump_entry(struct bgp_pbr_filter
*bpf
, bool add
)
1583 struct bgp_pbr_range_port
*src_port
;
1584 struct bgp_pbr_range_port
*dst_port
;
1585 struct bgp_pbr_range_port
*pkt_len
;
1586 char bufsrc
[64], bufdst
[64];
1588 int remaining_len
= 0;
1589 char protocol_str
[16];
1593 src_port
= bpf
->src_port
;
1594 dst_port
= bpf
->dst_port
;
1595 pkt_len
= bpf
->pkt_len
;
1597 protocol_str
[0] = '\0';
1598 if (bpf
->tcp_flags
&& bpf
->tcp_flags
->mask
)
1599 bpf
->protocol
= IPPROTO_TCP
;
1601 snprintf(protocol_str
, sizeof(protocol_str
),
1602 "proto %d", bpf
->protocol
);
1604 if (bpf
->protocol
== IPPROTO_ICMP
&& src_port
&& dst_port
)
1605 remaining_len
+= snprintf(buffer
, sizeof(buffer
),
1608 dst_port
->min_port
);
1609 else if (bpf
->protocol
== IPPROTO_UDP
||
1610 bpf
->protocol
== IPPROTO_TCP
) {
1612 if (src_port
&& src_port
->min_port
)
1613 remaining_len
+= snprintf(buffer
,
1617 src_port
->max_port
?
1618 src_port
->max_port
:
1619 src_port
->min_port
);
1620 if (dst_port
&& dst_port
->min_port
)
1621 remaining_len
+= snprintf(buffer
+
1627 dst_port
->max_port
?
1628 dst_port
->max_port
:
1629 dst_port
->min_port
);
1631 if (pkt_len
&& (pkt_len
->min_port
|| pkt_len
->max_port
)) {
1632 remaining_len
+= snprintf(buffer
+ remaining_len
,
1640 } else if (bpf
->pkt_len_val
) {
1641 remaining_len
+= snprintf(buffer
+ remaining_len
,
1645 bpf
->pkt_len_val
->mask
1647 bpf
->pkt_len_val
->val
);
1649 if (bpf
->tcp_flags
) {
1650 remaining_len
+= snprintf(buffer
+ remaining_len
,
1654 bpf
->tcp_flags
->val
,
1655 bpf
->tcp_flags
->mask
);
1658 snprintf(buffer
+ remaining_len
,
1666 zlog_debug("BGP: %s FS PBR from %s to %s, %s %s",
1667 add
? "adding" : "removing",
1668 bpf
->src
== NULL
? "<all>" :
1669 prefix2str(bpf
->src
, bufsrc
, sizeof(bufsrc
)),
1670 bpf
->dst
== NULL
? "<all>" :
1671 prefix2str(bpf
->dst
, bufdst
, sizeof(bufdst
)),
1672 protocol_str
, buffer
);
1676 static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp
*bgp
,
1677 struct bgp_path_info
*path
,
1678 struct bgp_pbr_filter
*bpf
,
1682 struct bgp_pbr_match temp
;
1683 struct bgp_pbr_match_entry temp2
;
1684 struct bgp_pbr_match
*bpm
;
1685 struct bgp_pbr_match_entry
*bpme
= NULL
;
1686 struct bgp_pbr_action temp3
;
1687 struct bgp_pbr_action
*bpa
= NULL
;
1688 struct bgp_pbr_match_entry_remain bpmer
;
1689 struct bgp_pbr_range_port
*src_port
;
1690 struct bgp_pbr_range_port
*dst_port
;
1691 struct bgp_pbr_range_port
*pkt_len
;
1692 bool bpme_found
= false;
1696 src_port
= bpf
->src_port
;
1697 dst_port
= bpf
->dst_port
;
1698 pkt_len
= bpf
->pkt_len
;
1700 if (BGP_DEBUG(zebra
, ZEBRA
))
1701 bgp_pbr_dump_entry(bpf
, true);
1703 /* look for bpa first */
1704 memset(&temp3
, 0, sizeof(temp3
));
1708 memcpy(&temp3
.nh
, nh
, sizeof(struct nexthop
));
1709 temp3
.vrf_id
= bpf
->vrf_id
;
1710 bpa
= hash_get(bgp
->pbr_action_hash
, &temp3
,
1711 bgp_pbr_action_alloc_intern
);
1713 if (bpa
->fwmark
== 0) {
1714 /* drop is handled by iptable */
1715 if (nh
&& nh
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1717 bpa
->installed
= true;
1719 bpa
->fwmark
= bgp_zebra_tm_get_id();
1720 bpa
->table_id
= bpa
->fwmark
;
1721 bpa
->installed
= false;
1724 bpa
->unique
= ++bgp_pbr_action_counter_unique
;
1725 /* 0 value is forbidden */
1726 bpa
->install_in_progress
= false;
1729 /* then look for bpm */
1730 memset(&temp
, 0, sizeof(temp
));
1731 temp
.vrf_id
= bpf
->vrf_id
;
1733 temp
.flags
|= MATCH_IP_SRC_SET
;
1735 temp
.flags
|= MATCH_IP_DST_SET
;
1737 if (src_port
&& (src_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
1738 if (bpf
->protocol
== IPPROTO_ICMP
)
1739 temp
.flags
|= MATCH_ICMP_SET
;
1740 temp
.flags
|= MATCH_PORT_SRC_SET
;
1742 if (dst_port
&& (dst_port
->min_port
|| bpf
->protocol
== IPPROTO_ICMP
)) {
1743 if (bpf
->protocol
== IPPROTO_ICMP
)
1744 temp
.flags
|= MATCH_ICMP_SET
;
1745 temp
.flags
|= MATCH_PORT_DST_SET
;
1747 if (src_port
&& src_port
->max_port
)
1748 temp
.flags
|= MATCH_PORT_SRC_RANGE_SET
;
1749 if (dst_port
&& dst_port
->max_port
)
1750 temp
.flags
|= MATCH_PORT_DST_RANGE_SET
;
1752 if (bpf
->src
== NULL
|| bpf
->dst
== NULL
) {
1753 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
1754 temp
.type
= IPSET_NET_PORT
;
1756 temp
.type
= IPSET_NET
;
1758 if (temp
.flags
& (MATCH_PORT_DST_SET
| MATCH_PORT_SRC_SET
))
1759 temp
.type
= IPSET_NET_PORT_NET
;
1761 temp
.type
= IPSET_NET_NET
;
1764 temp
.pkt_len_min
= pkt_len
->min_port
;
1765 if (pkt_len
->max_port
)
1766 temp
.pkt_len_max
= pkt_len
->max_port
;
1767 } else if (bpf
->pkt_len_val
) {
1768 if (bpf
->pkt_len_val
->mask
)
1769 temp
.flags
|= MATCH_PKT_LEN_INVERSE_SET
;
1770 temp
.pkt_len_min
= bpf
->pkt_len_val
->val
;
1772 if (bpf
->tcp_flags
) {
1773 temp
.tcp_flags
= bpf
->tcp_flags
->val
;
1774 temp
.tcp_mask_flags
= bpf
->tcp_flags
->mask
;
1777 if (bpf
->dscp
->mask
)
1778 temp
.flags
|= MATCH_DSCP_INVERSE_SET
;
1780 temp
.flags
|= MATCH_DSCP_SET
;
1781 temp
.dscp_value
= bpf
->dscp
->val
;
1783 if (bpf
->fragment
) {
1784 if (bpf
->fragment
->mask
)
1785 temp
.flags
|= MATCH_FRAGMENT_INVERSE_SET
;
1786 temp
.fragment
= bpf
->fragment
->val
;
1789 bpm
= hash_get(bgp
->pbr_match_hash
, &temp
,
1790 bgp_pbr_match_alloc_intern
);
1792 /* new, then self allocate ipset_name and unique */
1793 if (bpm
->unique
== 0) {
1794 bpm
->unique
= ++bgp_pbr_match_counter_unique
;
1795 /* 0 value is forbidden */
1796 sprintf(bpm
->ipset_name
, "match%p", bpm
);
1797 bpm
->entry_hash
= hash_create_size(8,
1798 bgp_pbr_match_entry_hash_key
,
1799 bgp_pbr_match_entry_hash_equal
,
1800 "Match Entry Hash");
1801 bpm
->installed
= false;
1803 /* unique2 should be updated too */
1804 bpm
->unique2
= ++bgp_pbr_match_iptable_counter_unique
;
1805 bpm
->installed_in_iptable
= false;
1806 bpm
->install_in_progress
= false;
1807 bpm
->install_iptable_in_progress
= false;
1810 memset(&temp2
, 0, sizeof(temp2
));
1812 prefix_copy(&temp2
.src
, bpf
->src
);
1814 temp2
.src
.family
= AF_INET
;
1816 prefix_copy(&temp2
.dst
, bpf
->dst
);
1818 temp2
.dst
.family
= AF_INET
;
1819 temp2
.src_port_min
= src_port
? src_port
->min_port
: 0;
1820 temp2
.dst_port_min
= dst_port
? dst_port
->min_port
: 0;
1821 temp2
.src_port_max
= src_port
? src_port
->max_port
: 0;
1822 temp2
.dst_port_max
= dst_port
? dst_port
->max_port
: 0;
1823 temp2
.proto
= bpf
->protocol
;
1824 bpme
= hash_get(bpm
->entry_hash
, &temp2
,
1825 bgp_pbr_match_entry_alloc_intern
);
1826 if (bpme
->unique
== 0) {
1827 bpme
->unique
= ++bgp_pbr_match_entry_counter_unique
;
1828 /* 0 value is forbidden */
1829 bpme
->backpointer
= bpm
;
1830 bpme
->installed
= false;
1831 bpme
->install_in_progress
= false;
1832 /* link bgp info to bpme */
1833 bpme
->path
= (void *)path
;
1837 /* already installed */
1839 struct bgp_path_info_extra
*extra
=
1840 bgp_path_info_extra_get(path
);
1842 if (extra
&& extra
->bgp_fs_pbr
&&
1843 listnode_lookup(extra
->bgp_fs_pbr
, bpme
)) {
1844 if (BGP_DEBUG(pbr
, PBR_ERROR
))
1846 "%s: entry %p/%p already installed in bgp pbr",
1847 __func__
, path
, bpme
);
1851 /* BGP FS: append entry to zebra
1852 * - policies are not routing entries and as such
1853 * route replace semantics don't necessarily follow
1854 * through to policy entries
1855 * - because of that, not all policing information will be stored
1856 * into zebra. and non selected policies will be suppressed from zebra
1857 * - as consequence, in order to bring consistency
1858 * a policy will be added, then ifan ecmp policy exists,
1859 * it will be suppressed subsequently
1862 if (!bpa
->installed
&& !bpa
->install_in_progress
) {
1863 bgp_send_pbr_rule_action(bpa
, true);
1864 bgp_zebra_announce_default(bgp
, nh
,
1865 AFI_IP
, bpa
->table_id
, true);
1869 if (!bpm
->installed
)
1870 bgp_send_pbr_ipset_match(bpm
, true);
1872 if (!bpme
->installed
)
1873 bgp_send_pbr_ipset_entry_match(bpme
, true);
1876 if (!bpm
->installed_in_iptable
)
1877 bgp_send_pbr_iptable(bpa
, bpm
, true);
1879 /* A previous entry may already exist
1880 * flush previous entry if necessary
1882 bpmer
.bpme_to_match
= bpme
;
1883 bpmer
.bpme_found
= NULL
;
1884 hash_walk(bgp
->pbr_match_hash
, bgp_pbr_get_remaining_entry
, &bpmer
);
1885 if (bpmer
.bpme_found
) {
1886 static struct bgp_pbr_match
*local_bpm
;
1887 static struct bgp_pbr_action
*local_bpa
;
1889 local_bpm
= bpmer
.bpme_found
->backpointer
;
1890 local_bpa
= local_bpm
->action
;
1891 bgp_pbr_flush_entry(bgp
, local_bpa
,
1892 local_bpm
, bpmer
.bpme_found
);
1898 static void bgp_pbr_policyroute_add_to_zebra_recursive(
1899 struct bgp
*bgp
, struct bgp_path_info
*path
, struct bgp_pbr_filter
*bpf
,
1900 struct bgp_pbr_or_filter
*bpof
, struct nexthop
*nh
, float *rate
,
1903 struct listnode
*node
, *nnode
;
1904 struct bgp_pbr_val_mask
*valmask
;
1905 uint8_t next_type_entry
;
1906 struct list
*orig_list
;
1907 struct bgp_pbr_val_mask
**target_val
;
1909 if (type_entry
== 0)
1910 return bgp_pbr_policyroute_add_to_zebra_unit(bgp
, path
, bpf
, nh
,
1912 next_type_entry
= bgp_pbr_next_type_entry(type_entry
);
1913 if (type_entry
== FLOWSPEC_TCP_FLAGS
&& bpof
->tcpflags
) {
1914 orig_list
= bpof
->tcpflags
;
1915 target_val
= &bpf
->tcp_flags
;
1916 } else if (type_entry
== FLOWSPEC_DSCP
&& bpof
->dscp
) {
1917 orig_list
= bpof
->dscp
;
1918 target_val
= &bpf
->dscp
;
1919 } else if (type_entry
== FLOWSPEC_PKT_LEN
&& bpof
->pkt_len
) {
1920 orig_list
= bpof
->pkt_len
;
1921 target_val
= &bpf
->pkt_len_val
;
1922 } else if (type_entry
== FLOWSPEC_FRAGMENT
&& bpof
->fragment
) {
1923 orig_list
= bpof
->fragment
;
1924 target_val
= &bpf
->fragment
;
1925 } else if (type_entry
== FLOWSPEC_ICMP_TYPE
&&
1926 (bpof
->icmp_type
|| bpof
->icmp_code
)) {
1927 /* enumerate list for icmp - must be last one */
1928 bgp_pbr_icmp_action(bgp
, path
, bpf
, bpof
, true, nh
, rate
);
1931 return bgp_pbr_policyroute_add_to_zebra_recursive(
1932 bgp
, path
, bpf
, bpof
, nh
, rate
, next_type_entry
);
1934 for (ALL_LIST_ELEMENTS(orig_list
, node
, nnode
, valmask
)) {
1935 *target_val
= valmask
;
1936 bgp_pbr_policyroute_add_to_zebra_recursive(
1937 bgp
, path
, bpf
, bpof
, nh
, rate
, next_type_entry
);
1941 static void bgp_pbr_policyroute_add_to_zebra(struct bgp
*bgp
,
1942 struct bgp_path_info
*path
,
1943 struct bgp_pbr_filter
*bpf
,
1944 struct bgp_pbr_or_filter
*bpof
,
1945 struct nexthop
*nh
, float *rate
)
1948 return bgp_pbr_policyroute_add_to_zebra_unit(bgp
, path
, bpf
, nh
,
1951 bgp_pbr_policyroute_add_to_zebra_recursive(
1952 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_TCP_FLAGS
);
1953 else if (bpof
->dscp
)
1954 bgp_pbr_policyroute_add_to_zebra_recursive(
1955 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_DSCP
);
1956 else if (bpof
->pkt_len
)
1957 bgp_pbr_policyroute_add_to_zebra_recursive(
1958 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_PKT_LEN
);
1959 else if (bpof
->fragment
)
1960 bgp_pbr_policyroute_add_to_zebra_recursive(
1961 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_FRAGMENT
);
1962 else if (bpof
->icmp_type
|| bpof
->icmp_code
)
1963 bgp_pbr_policyroute_add_to_zebra_recursive(
1964 bgp
, path
, bpf
, bpof
, nh
, rate
, FLOWSPEC_ICMP_TYPE
);
1966 bgp_pbr_policyroute_add_to_zebra_unit(bgp
, path
, bpf
, nh
, rate
);
1969 list_delete_all_node(bpof
->tcpflags
);
1971 list_delete_all_node(bpof
->dscp
);
1973 list_delete_all_node(bpof
->pkt_len
);
1975 list_delete_all_node(bpof
->fragment
);
1976 if (bpof
->icmp_type
)
1977 list_delete_all_node(bpof
->icmp_type
);
1978 if (bpof
->icmp_code
)
1979 list_delete_all_node(bpof
->icmp_code
);
1982 static void bgp_pbr_handle_entry(struct bgp
*bgp
, struct bgp_path_info
*path
,
1983 struct bgp_pbr_entry_main
*api
, bool add
)
1987 int continue_loop
= 1;
1989 struct prefix
*src
= NULL
, *dst
= NULL
;
1991 struct bgp_pbr_range_port
*srcp
= NULL
, *dstp
= NULL
;
1992 struct bgp_pbr_range_port range
, range_icmp_code
;
1993 struct bgp_pbr_range_port pkt_len
;
1994 struct bgp_pbr_filter bpf
;
1996 struct bgp_pbr_or_filter bpof
;
1997 struct bgp_pbr_val_mask bpvm
;
1999 memset(&nh
, 0, sizeof(struct nexthop
));
2000 memset(&bpf
, 0, sizeof(struct bgp_pbr_filter
));
2001 memset(&bpof
, 0, sizeof(struct bgp_pbr_or_filter
));
2002 if (api
->match_bitmask
& PREFIX_SRC_PRESENT
)
2003 src
= &api
->src_prefix
;
2004 if (api
->match_bitmask
& PREFIX_DST_PRESENT
)
2005 dst
= &api
->dst_prefix
;
2006 memset(&nh
, 0, sizeof(struct nexthop
));
2007 nh
.vrf_id
= VRF_UNKNOWN
;
2008 if (api
->match_protocol_num
)
2009 proto
= (uint8_t)api
->protocol
[0].value
;
2010 /* if match_port is selected, then either src or dst port will be parsed
2011 * but not both at the same time
2013 if (api
->match_port_num
>= 1) {
2014 bgp_pbr_extract(api
->port
,
2015 api
->match_port_num
,
2017 srcp
= dstp
= &range
;
2018 } else if (api
->match_src_port_num
>= 1) {
2019 bgp_pbr_extract(api
->src_port
,
2020 api
->match_src_port_num
,
2024 } else if (api
->match_dst_port_num
>= 1) {
2025 bgp_pbr_extract(api
->dst_port
,
2026 api
->match_dst_port_num
,
2031 if (api
->match_icmp_type_num
>= 1) {
2032 proto
= IPPROTO_ICMP
;
2033 if (bgp_pbr_extract(api
->icmp_type
,
2034 api
->match_icmp_type_num
,
2038 bpof
.icmp_type
= list_new();
2039 bgp_pbr_extract_enumerate(api
->icmp_type
,
2040 api
->match_icmp_type_num
,
2043 FLOWSPEC_ICMP_TYPE
);
2046 if (api
->match_icmp_code_num
>= 1) {
2047 proto
= IPPROTO_ICMP
;
2048 if (bgp_pbr_extract(api
->icmp_code
,
2049 api
->match_icmp_code_num
,
2051 dstp
= &range_icmp_code
;
2053 bpof
.icmp_code
= list_new();
2054 bgp_pbr_extract_enumerate(api
->icmp_code
,
2055 api
->match_icmp_code_num
,
2058 FLOWSPEC_ICMP_CODE
);
2062 if (api
->match_tcpflags_num
) {
2063 kind_enum
= bgp_pbr_match_val_get_operator(api
->tcpflags
,
2064 api
->match_tcpflags_num
);
2065 if (kind_enum
== OPERATOR_UNARY_AND
) {
2066 bpf
.tcp_flags
= &bpvm
;
2067 bgp_pbr_extract_enumerate(api
->tcpflags
,
2068 api
->match_tcpflags_num
,
2071 FLOWSPEC_TCP_FLAGS
);
2072 } else if (kind_enum
== OPERATOR_UNARY_OR
) {
2073 bpof
.tcpflags
= list_new();
2074 bgp_pbr_extract_enumerate(api
->tcpflags
,
2075 api
->match_tcpflags_num
,
2078 FLOWSPEC_TCP_FLAGS
);
2081 if (api
->match_packet_length_num
) {
2084 ret
= bgp_pbr_extract(api
->packet_length
,
2085 api
->match_packet_length_num
,
2088 bpf
.pkt_len
= &pkt_len
;
2090 bpof
.pkt_len
= list_new();
2091 bgp_pbr_extract_enumerate(api
->packet_length
,
2092 api
->match_packet_length_num
,
2098 if (api
->match_dscp_num
>= 1) {
2099 bpof
.dscp
= list_new();
2100 bgp_pbr_extract_enumerate(api
->dscp
, api
->match_dscp_num
,
2102 bpof
.dscp
, FLOWSPEC_DSCP
);
2104 if (api
->match_fragment_num
) {
2105 bpof
.fragment
= list_new();
2106 bgp_pbr_extract_enumerate(api
->fragment
,
2107 api
->match_fragment_num
,
2112 bpf
.vrf_id
= api
->vrf_id
;
2115 bpf
.protocol
= proto
;
2116 bpf
.src_port
= srcp
;
2117 bpf
.dst_port
= dstp
;
2119 return bgp_pbr_policyroute_remove_from_zebra(bgp
, path
, &bpf
,
2121 /* no action for add = true */
2122 for (i
= 0; i
< api
->action_num
; i
++) {
2123 switch (api
->actions
[i
].action
) {
2124 case ACTION_TRAFFICRATE
:
2126 if (api
->actions
[i
].u
.r
.rate
== 0) {
2127 nh
.vrf_id
= api
->vrf_id
;
2128 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2129 bgp_pbr_policyroute_add_to_zebra(
2130 bgp
, path
, &bpf
, &bpof
, &nh
, &rate
);
2132 /* update rate. can be reentrant */
2133 rate
= api
->actions
[i
].u
.r
.rate
;
2134 if (BGP_DEBUG(pbr
, PBR
)) {
2135 bgp_pbr_print_policy_route(api
);
2136 zlog_warn("PBR: ignoring Set action rate %f",
2137 api
->actions
[i
].u
.r
.rate
);
2141 case ACTION_TRAFFIC_ACTION
:
2142 if (api
->actions
[i
].u
.za
.filter
2143 & TRAFFIC_ACTION_SAMPLE
) {
2144 if (BGP_DEBUG(pbr
, PBR
)) {
2145 bgp_pbr_print_policy_route(api
);
2146 zlog_warn("PBR: Sample action Ignored");
2150 if (api
->actions
[i
].u
.za
.filter
2151 & TRAFFIC_ACTION_DISTRIBUTE
) {
2152 if (BGP_DEBUG(pbr
, PBR
)) {
2153 bgp_pbr_print_policy_route(api
);
2154 zlog_warn("PBR: Distribute action Applies");
2157 /* continue forwarding entry as before
2161 #endif /* XXX to confirm behaviour of traffic action. for now , ignore */
2162 /* terminate action: run other filters
2165 case ACTION_REDIRECT_IP
:
2166 nh
.type
= NEXTHOP_TYPE_IPV4
;
2167 nh
.gate
.ipv4
.s_addr
=
2168 api
->actions
[i
].u
.zr
.redirect_ip_v4
.s_addr
;
2169 nh
.vrf_id
= api
->vrf_id
;
2170 bgp_pbr_policyroute_add_to_zebra(bgp
, path
, &bpf
, &bpof
,
2172 /* XXX combination with REDIRECT_VRF
2173 * + REDIRECT_NH_IP not done
2177 case ACTION_REDIRECT
:
2178 nh
.vrf_id
= api
->actions
[i
].u
.redirect_vrf
;
2179 nh
.type
= NEXTHOP_TYPE_IPV4
;
2180 bgp_pbr_policyroute_add_to_zebra(bgp
, path
, &bpf
, &bpof
,
2184 case ACTION_MARKING
:
2185 if (BGP_DEBUG(pbr
, PBR
)) {
2186 bgp_pbr_print_policy_route(api
);
2187 zlog_warn("PBR: Set DSCP %u Ignored",
2188 api
->actions
[i
].u
.marking_dscp
);
2194 if (continue_loop
== 0)
2199 void bgp_pbr_update_entry(struct bgp
*bgp
, struct prefix
*p
,
2200 struct bgp_path_info
*info
, afi_t afi
, safi_t safi
,
2203 struct bgp_pbr_entry_main api
;
2206 return; /* IPv6 not supported */
2207 if (safi
!= SAFI_FLOWSPEC
)
2208 return; /* not supported */
2209 /* Make Zebra API structure. */
2210 memset(&api
, 0, sizeof(api
));
2211 api
.vrf_id
= bgp
->vrf_id
;
2214 if (!bgp_zebra_tm_chunk_obtained()) {
2215 if (BGP_DEBUG(pbr
, PBR_ERROR
))
2216 flog_err(EC_BGP_TABLE_CHUNK
,
2217 "%s: table chunk not obtained yet", __func__
);
2221 if (bgp_pbr_build_and_validate_entry(p
, info
, &api
) < 0) {
2222 if (BGP_DEBUG(pbr
, PBR_ERROR
))
2223 flog_err(EC_BGP_FLOWSPEC_INSTALLATION
,
2224 "%s: cancel updating entry %p in bgp pbr",
2228 bgp_pbr_handle_entry(bgp
, info
, &api
, nlri_update
);
2231 int bgp_pbr_interface_compare(const struct bgp_pbr_interface
*a
,
2232 const struct bgp_pbr_interface
*b
)
2234 return strcmp(a
->name
, b
->name
);
2237 struct bgp_pbr_interface
*bgp_pbr_interface_lookup(const char *name
,
2238 struct bgp_pbr_interface_head
*head
)
2240 struct bgp_pbr_interface pbr_if
;
2242 strlcpy(pbr_if
.name
, name
, sizeof(pbr_if
.name
));
2243 return (RB_FIND(bgp_pbr_interface_head
,
2247 /* this function resets to the default policy routing
2248 * go back to default status
2250 void bgp_pbr_reset(struct bgp
*bgp
, afi_t afi
)
2252 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2253 struct bgp_pbr_interface_head
*head
;
2254 struct bgp_pbr_interface
*pbr_if
;
2256 if (!bgp_pbr_cfg
|| afi
!= AFI_IP
)
2258 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2260 while (!RB_EMPTY(bgp_pbr_interface_head
, head
)) {
2261 pbr_if
= RB_ROOT(bgp_pbr_interface_head
, head
);
2262 RB_REMOVE(bgp_pbr_interface_head
, head
, pbr_if
);
2263 XFREE(MTYPE_TMP
, pbr_if
);