]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_pbr.c
edd8317c3161058c5bba53db19f1d19fa87c8fa0
[mirror_frr.git] / bgpd / bgp_pbr.c
1 /*
2 * BGP pbr
3 * Copyright (C) 6WIND
4 *
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
8 * later version.
9 *
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.
14 *
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
18 */
19
20 #include "zebra.h"
21 #include "prefix.h"
22 #include "zclient.h"
23 #include "jhash.h"
24 #include "pbr.h"
25
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"
37
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")
43
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);
48
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;
53
54 struct bgp_pbr_match_iptable_unique {
55 uint32_t unique;
56 struct bgp_pbr_match *bpm_found;
57 };
58
59 struct bgp_pbr_match_entry_unique {
60 uint32_t unique;
61 struct bgp_pbr_match_entry *bpme_found;
62 };
63
64 struct bgp_pbr_action_unique {
65 uint32_t unique;
66 struct bgp_pbr_action *bpa_found;
67 };
68
69 static int bgp_pbr_action_walkcb(struct hash_backet *backet, void *arg)
70 {
71 struct bgp_pbr_action *bpa = (struct bgp_pbr_action *)backet->data;
72 struct bgp_pbr_action_unique *bpau = (struct bgp_pbr_action_unique *)
73 arg;
74 uint32_t unique = bpau->unique;
75
76 if (bpa->unique == unique) {
77 bpau->bpa_found = bpa;
78 return HASHWALK_ABORT;
79 }
80 return HASHWALK_CONTINUE;
81 }
82
83 static int bgp_pbr_match_entry_walkcb(struct hash_backet *backet, void *arg)
84 {
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;
90
91 if (bpme->unique == unique) {
92 bpmeu->bpme_found = bpme;
93 return HASHWALK_ABORT;
94 }
95 return HASHWALK_CONTINUE;
96 }
97
98 struct bgp_pbr_match_ipsetname {
99 char *ipsetname;
100 struct bgp_pbr_match *bpm_found;
101 };
102
103 static int bgp_pbr_match_pername_walkcb(struct hash_backet *backet, void *arg)
104 {
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;
109
110 if (!strncmp(ipset_name, bpm->ipset_name,
111 ZEBRA_IPSET_NAME_SIZE)) {
112 bpmi->bpm_found = bpm;
113 return HASHWALK_ABORT;
114 }
115 return HASHWALK_CONTINUE;
116 }
117
118 static int bgp_pbr_match_iptable_walkcb(struct hash_backet *backet, void *arg)
119 {
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;
124
125 if (bpm->unique2 == unique) {
126 bpmiu->bpm_found = bpm;
127 return HASHWALK_ABORT;
128 }
129 return HASHWALK_CONTINUE;
130 }
131
132 struct bgp_pbr_match_unique {
133 uint32_t unique;
134 struct bgp_pbr_match *bpm_found;
135 };
136
137 static int bgp_pbr_match_walkcb(struct hash_backet *backet, void *arg)
138 {
139 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data;
140 struct bgp_pbr_match_unique *bpmu = (struct bgp_pbr_match_unique *)
141 arg;
142 uint32_t unique = bpmu->unique;
143
144 if (bpm->unique == unique) {
145 bpmu->bpm_found = bpm;
146 return HASHWALK_ABORT;
147 }
148 return HASHWALK_CONTINUE;
149 }
150
151 static int sprintf_bgp_pbr_match_val(char *str, struct bgp_pbr_match_val *mval,
152 const char *prepend)
153 {
154 char *ptr = str;
155
156 if (prepend)
157 ptr += sprintf(ptr, "%s", prepend);
158 else {
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 ");
163 }
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);
174 }
175
176 #define INCREMENT_DISPLAY(_ptr, _cnt) do { \
177 if (_cnt) \
178 (_ptr) += sprintf((_ptr), "; "); \
179 _cnt++; \
180 } while (0)
181
182 /* this structure can be used for port range,
183 * but also for other values range like packet length range
184 */
185 struct bgp_pbr_range_port {
186 uint16_t min_port;
187 uint16_t max_port;
188 };
189
190 /* this structure can be used to filter with a mask
191 * for instance it supports not instructions like for
192 * tcpflags
193 */
194 struct bgp_pbr_val_mask {
195 uint16_t val;
196 uint16_t mask;
197 };
198
199 /* this structure is used to pass instructs
200 * so that BGP can create pbr instructions to ZEBRA
201 */
202 struct bgp_pbr_filter {
203 vrf_id_t vrf_id;
204 struct prefix *src;
205 struct prefix *dst;
206 uint8_t protocol;
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;
214 };
215
216 /* this structure is used to contain OR instructions
217 * so that BGP can create multiple pbr instructions
218 * to ZEBRA
219 */
220 struct bgp_pbr_or_filter {
221 struct list *tcpflags;
222 struct list *dscp;
223 struct list *pkt_len;
224 struct list *fragment;
225 struct list *icmp_type;
226 struct list *icmp_code;
227 };
228
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,
232 struct nexthop *nh,
233 float *rate);
234
235 static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add);
236
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,
241 uint8_t type_entry)
242 {
243 if (unary_operator == OPERATOR_UNARY_AND && and_valmask) {
244 if (type_entry == FLOWSPEC_TCP_FLAGS) {
245 and_valmask->mask |=
246 TCP_HEADER_ALL_FLAGS &
247 ~(value);
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 */
253 }
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;
259 and_valmask->mask |=
260 TCP_HEADER_ALL_FLAGS &
261 ~(value);
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 */
267 }
268 listnode_add(or_valmask, and_valmask);
269 } else if (type_entry == FLOWSPEC_ICMP_CODE ||
270 type_entry == FLOWSPEC_ICMP_TYPE)
271 return false;
272 return true;
273 }
274
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
280 */
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)
284 {
285 int i = 0;
286 struct bgp_pbr_val_mask *and_valmask = NULL;
287 struct list *or_valmask = NULL;
288 bool ret;
289
290 if (valmask) {
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;
296 }
297 }
298 for (i = 0; i < num; i++) {
299 if (i != 0 && list[i].unary_operator !=
300 unary_operator)
301 return false;
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,
313 type_entry);
314 if (ret == false)
315 return ret;
316 continue;
317 }
318 return false;
319 }
320 if (unary_operator == OPERATOR_UNARY_AND && and_valmask) {
321 if (type_entry == FLOWSPEC_TCP_FLAGS)
322 and_valmask->mask |=
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;
329 and_valmask->mask |=
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);
338 }
339 }
340 if (unary_operator == OPERATOR_UNARY_AND && and_valmask
341 && type_entry == FLOWSPEC_TCP_FLAGS)
342 and_valmask->val = TCP_HEADER_ALL_FLAGS;
343 return true;
344 }
345
346 /* if unary operator can either be UNARY_OR/AND/OR-AND.
347 * in the latter case, combinationf of both is not handled
348 */
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)
352 {
353 bool ret;
354 uint8_t unary_operator_val;
355 bool double_check = false;
356
357 if ((unary_operator & OPERATOR_UNARY_OR) &&
358 (unary_operator & OPERATOR_UNARY_AND)) {
359 unary_operator_val = OPERATOR_UNARY_AND;
360 double_check = true;
361 } else
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,
367 OPERATOR_UNARY_OR,
368 valmask,
369 type_entry);
370 return ret;
371 }
372
373 /* returns the unary operator that is in the list
374 * return 0 if both operators are used
375 */
376 static uint8_t bgp_pbr_match_val_get_operator(struct bgp_pbr_match_val list[],
377 int num)
378
379 {
380 int i;
381 uint8_t unary_operator = OPERATOR_UNARY_AND;
382
383 for (i = 0; i < num; i++) {
384 if (i == 0)
385 continue;
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))
392 return 0;
393 }
394 return unary_operator;
395 }
396
397
398 /* return true if extraction ok
399 */
400 static bool bgp_pbr_extract(struct bgp_pbr_match_val list[],
401 int num,
402 struct bgp_pbr_range_port *range)
403 {
404 int i = 0;
405 bool exact_match = false;
406
407 if (range)
408 memset(range, 0, sizeof(struct bgp_pbr_range_port));
409
410 if (num > 2)
411 return false;
412 for (i = 0; i < num; i++) {
413 if (i != 0 && (list[i].compare_operator ==
414 OPERATOR_COMPARE_EQUAL_TO))
415 return false;
416 if (i == 0 && (list[i].compare_operator ==
417 OPERATOR_COMPARE_EQUAL_TO)) {
418 if (range)
419 range->min_port = list[i].value;
420 exact_match = true;
421 }
422 if (exact_match == true && i > 0)
423 return false;
424 if (list[i].compare_operator ==
425 (OPERATOR_COMPARE_GREATER_THAN +
426 OPERATOR_COMPARE_EQUAL_TO)) {
427 if (range)
428 range->min_port = list[i].value;
429 } else if (list[i].compare_operator ==
430 (OPERATOR_COMPARE_LESS_THAN +
431 OPERATOR_COMPARE_EQUAL_TO)) {
432 if (range)
433 range->max_port = list[i].value;
434 } else if (list[i].compare_operator ==
435 OPERATOR_COMPARE_LESS_THAN) {
436 if (range)
437 range->max_port = list[i].value - 1;
438 } else if (list[i].compare_operator ==
439 OPERATOR_COMPARE_GREATER_THAN) {
440 if (range)
441 range->min_port = list[i].value + 1;
442 }
443 }
444 return true;
445 }
446
447 static int bgp_pbr_validate_policy_route(struct bgp_pbr_entry_main *api)
448 {
449 bool enumerate_icmp = false;
450
451 /* because bgp pbr entry may contain unsupported
452 * combinations, a message will be displayed here if
453 * not supported.
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
459 */
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);
465 return 0;
466 }
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);
475 return 0;
476 }
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.");
481 return 0;
482 }
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.");
487 return 0;
488 }
489 if (!bgp_pbr_extract_enumerate(api->tcpflags,
490 api->match_tcpflags_num,
491 OPERATOR_UNARY_AND |
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.");
497 return 0;
498 }
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.");
507 return 0;
508 }
509 enumerate_icmp = true;
510 }
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.");
519 return 0;
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.");
526 return 0;
527 }
528 }
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.");
533 return 0;
534 }
535 if (api->match_packet_length_num) {
536 bool ret;
537
538 ret = bgp_pbr_extract(api->packet_length,
539 api->match_packet_length_num, NULL);
540 if (!ret)
541 ret = bgp_pbr_extract_enumerate(api->packet_length,
542 api->match_packet_length_num,
543 OPERATOR_UNARY_OR
544 | OPERATOR_UNARY_AND,
545 NULL, FLOWSPEC_PKT_LEN);
546 if (!ret) {
547 if (BGP_DEBUG(pbr, PBR))
548 zlog_debug("BGP: match packet length operations:"
549 "too complex. ignoring.");
550 return 0;
551 }
552 }
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.");
560 return 0;
561 }
562 }
563 if (api->match_fragment_num) {
564 char fail_str[64];
565 bool success;
566
567 success = bgp_pbr_extract_enumerate(api->fragment,
568 api->match_fragment_num,
569 OPERATOR_UNARY_OR
570 | OPERATOR_UNARY_AND,
571 NULL, FLOWSPEC_FRAGMENT);
572 if (success) {
573 int i;
574
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) {
580 success = false;
581 sprintf(fail_str,
582 "Value not valid (%d) for this implementation",
583 api->fragment[i].value);
584 }
585 }
586 } else
587 sprintf(fail_str, "too complex. ignoring");
588 if (!success) {
589 if (BGP_DEBUG(pbr, PBR))
590 zlog_debug("BGP: match fragment operation (%d) %s",
591 api->match_fragment_num,
592 fail_str);
593 return 0;
594 }
595 }
596
597 /* no combinations with both src_port and dst_port
598 * or port with src_port and dst_port
599 */
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.");
605 return 0;
606 }
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.");
613 return 0;
614 }
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."
621 " ignoring.");
622 }
623 return 0;
624 }
625 return 1;
626 }
627
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 *path,
631 struct bgp_pbr_entry_main *api)
632 {
633 int ret;
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;
640 afi_t afi = AFI_IP;
641
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);
645 if (ret < 0)
646 return -1;
647 /* extract actiosn from flowspec ecom list */
648 if (path && path->attr && path->attr->ecommunity) {
649 ecom = path->attr->ecommunity;
650 for (i = 0; i < ecom->size; i++) {
651 ecom_eval = (struct ecommunity_val *)
652 (ecom->val + (i * ECOMMUNITY_SIZE));
653 action_count++;
654 if (action_count > ACTIONS_MAX_NUM) {
655 if (BGP_DEBUG(pbr, PBR_ERROR))
656 flog_err(
657 EC_BGP_FLOWSPEC_PACKET,
658 "%s: flowspec actions exceeds limit (max %u)",
659 __func__, action_count);
660 break;
661 }
662 api_action = &api->actions[action_count - 1];
663
664 if ((ecom_eval->val[1] ==
665 (char)ECOMMUNITY_REDIRECT_VRF) &&
666 (ecom_eval->val[0] ==
667 (char)ECOMMUNITY_ENCODE_TRANS_EXP ||
668 ecom_eval->val[0] ==
669 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 ||
670 ecom_eval->val[0] ==
671 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_3)) {
672 struct ecommunity *eckey = ecommunity_new();
673 struct ecommunity_val ecom_copy;
674
675 memcpy(&ecom_copy, ecom_eval,
676 sizeof(struct ecommunity_val));
677 ecom_copy.val[0] &=
678 ~ECOMMUNITY_ENCODE_TRANS_EXP;
679 ecom_copy.val[1] = ECOMMUNITY_ROUTE_TARGET;
680 ecommunity_add_val(eckey, &ecom_copy);
681
682 api_action->action = ACTION_REDIRECT;
683 api_action->u.redirect_vrf =
684 get_first_vrf_for_redirect_with_rt(
685 eckey);
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 path->attr->nexthop.s_addr;
694 api_action->u.zr.duplicate = ecom_eval->val[7];
695 } else {
696 if (ecom_eval->val[0] !=
697 (char)ECOMMUNITY_ENCODE_TRANS_EXP)
698 continue;
699 ret = ecommunity_fill_pbr_action(ecom_eval,
700 api_action);
701 if (ret != 0)
702 continue;
703 }
704 api->action_num++;
705 }
706 }
707
708 /* validate if incoming matc/action is compatible
709 * with our policy routing engine
710 */
711 if (!bgp_pbr_validate_policy_route(api))
712 return -1;
713
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);
718 valid_prefix = 1;
719 }
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));
728 }
729 return -1;
730 }
731 }
732 return 0;
733 }
734
735 static void bgp_pbr_match_entry_free(void *arg)
736 {
737 struct bgp_pbr_match_entry *bpme;
738
739 bpme = (struct bgp_pbr_match_entry *)arg;
740
741 if (bpme->installed) {
742 bgp_send_pbr_ipset_entry_match(bpme, false);
743 bpme->installed = false;
744 bpme->backpointer = NULL;
745 }
746 XFREE(MTYPE_PBR_MATCH_ENTRY, bpme);
747 }
748
749 static void bgp_pbr_match_free(void *arg)
750 {
751 struct bgp_pbr_match *bpm;
752
753 bpm = (struct bgp_pbr_match *)arg;
754
755 hash_clean(bpm->entry_hash, bgp_pbr_match_entry_free);
756
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,
763 bpm, false);
764 bpm->installed_in_iptable = false;
765 bpm->action->refcnt--;
766 }
767 bgp_send_pbr_ipset_match(bpm, false);
768 bpm->installed = false;
769 bpm->action = NULL;
770 }
771 }
772 hash_free(bpm->entry_hash);
773
774 XFREE(MTYPE_PBR_MATCH, bpm);
775 }
776
777 static void *bgp_pbr_match_alloc_intern(void *arg)
778 {
779 struct bgp_pbr_match *bpm, *new;
780
781 bpm = (struct bgp_pbr_match *)arg;
782
783 new = XCALLOC(MTYPE_PBR_MATCH, sizeof(*new));
784 memcpy(new, bpm, sizeof(*bpm));
785
786 return new;
787 }
788
789 static void bgp_pbr_action_free(void *arg)
790 {
791 struct bgp_pbr_action *bpa;
792
793 bpa = (struct bgp_pbr_action *)arg;
794
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),
799 AFI_IP,
800 bpa->table_id,
801 false);
802 bpa->installed = false;
803 }
804 }
805 XFREE(MTYPE_PBR_ACTION, bpa);
806 }
807
808 static void *bgp_pbr_action_alloc_intern(void *arg)
809 {
810 struct bgp_pbr_action *bpa, *new;
811
812 bpa = (struct bgp_pbr_action *)arg;
813
814 new = XCALLOC(MTYPE_PBR_ACTION, sizeof(*new));
815
816 memcpy(new, bpa, sizeof(*bpa));
817
818 return new;
819 }
820
821 static void *bgp_pbr_match_entry_alloc_intern(void *arg)
822 {
823 struct bgp_pbr_match_entry *bpme, *new;
824
825 bpme = (struct bgp_pbr_match_entry *)arg;
826
827 new = XCALLOC(MTYPE_PBR_MATCH_ENTRY, sizeof(*new));
828
829 memcpy(new, bpme, sizeof(*bpme));
830
831 return new;
832 }
833
834 uint32_t bgp_pbr_match_hash_key(void *arg)
835 {
836 struct bgp_pbr_match *pbm = (struct bgp_pbr_match *)arg;
837 uint32_t key;
838
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);
848 }
849
850 bool bgp_pbr_match_hash_equal(const void *arg1, const void *arg2)
851 {
852 const struct bgp_pbr_match *r1, *r2;
853
854 r1 = (const struct bgp_pbr_match *)arg1;
855 r2 = (const struct bgp_pbr_match *)arg2;
856
857 if (r1->vrf_id != r2->vrf_id)
858 return false;
859
860 if (r1->type != r2->type)
861 return false;
862
863 if (r1->flags != r2->flags)
864 return false;
865
866 if (r1->action != r2->action)
867 return false;
868
869 if (r1->pkt_len_min != r2->pkt_len_min)
870 return false;
871
872 if (r1->pkt_len_max != r2->pkt_len_max)
873 return false;
874
875 if (r1->tcp_flags != r2->tcp_flags)
876 return false;
877
878 if (r1->tcp_mask_flags != r2->tcp_mask_flags)
879 return false;
880
881 if (r1->dscp_value != r2->dscp_value)
882 return false;
883
884 if (r1->fragment != r2->fragment)
885 return false;
886 return true;
887 }
888
889 uint32_t bgp_pbr_match_entry_hash_key(void *arg)
890 {
891 struct bgp_pbr_match_entry *pbme;
892 uint32_t key;
893
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);
902
903 return key;
904 }
905
906 bool bgp_pbr_match_entry_hash_equal(const void *arg1, const void *arg2)
907 {
908 const struct bgp_pbr_match_entry *r1, *r2;
909
910 r1 = (const struct bgp_pbr_match_entry *)arg1;
911 r2 = (const struct bgp_pbr_match_entry *)arg2;
912
913 /*
914 * on updates, comparing backpointer is not necessary
915 * unique value is self calculated
916 * rate is ignored for now
917 */
918
919 if (!prefix_same(&r1->src, &r2->src))
920 return false;
921
922 if (!prefix_same(&r1->dst, &r2->dst))
923 return false;
924
925 if (r1->src_port_min != r2->src_port_min)
926 return false;
927
928 if (r1->dst_port_min != r2->dst_port_min)
929 return false;
930
931 if (r1->src_port_max != r2->src_port_max)
932 return false;
933
934 if (r1->dst_port_max != r2->dst_port_max)
935 return false;
936
937 if (r1->proto != r2->proto)
938 return false;
939
940 return true;
941 }
942
943 uint32_t bgp_pbr_action_hash_key(void *arg)
944 {
945 struct bgp_pbr_action *pbra;
946 uint32_t key;
947
948 pbra = (struct bgp_pbr_action *)arg;
949 key = jhash_1word(pbra->table_id, 0x4312abde);
950 key = jhash_1word(pbra->fwmark, key);
951 return key;
952 }
953
954 bool bgp_pbr_action_hash_equal(const void *arg1, const void *arg2)
955 {
956 const struct bgp_pbr_action *r1, *r2;
957
958 r1 = (const struct bgp_pbr_action *)arg1;
959 r2 = (const struct bgp_pbr_action *)arg2;
960
961 /* unique value is self calculated
962 * table and fwmark is self calculated
963 * rate is ignored
964 */
965 if (r1->vrf_id != r2->vrf_id)
966 return false;
967
968 if (memcmp(&r1->nh, &r2->nh, sizeof(struct nexthop)))
969 return false;
970
971 return true;
972 }
973
974 struct bgp_pbr_action *bgp_pbr_action_rule_lookup(vrf_id_t vrf_id,
975 uint32_t unique)
976 {
977 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
978 struct bgp_pbr_action_unique bpau;
979
980 if (!bgp || unique == 0)
981 return NULL;
982 bpau.unique = unique;
983 bpau.bpa_found = NULL;
984 hash_walk(bgp->pbr_action_hash, bgp_pbr_action_walkcb, &bpau);
985 return bpau.bpa_found;
986 }
987
988 struct bgp_pbr_match *bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id,
989 uint32_t unique)
990 {
991 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
992 struct bgp_pbr_match_unique bpmu;
993
994 if (!bgp || unique == 0)
995 return NULL;
996 bpmu.unique = unique;
997 bpmu.bpm_found = NULL;
998 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_walkcb, &bpmu);
999 return bpmu.bpm_found;
1000 }
1001
1002 struct bgp_pbr_match_entry *bgp_pbr_match_ipset_entry_lookup(vrf_id_t vrf_id,
1003 char *ipset_name,
1004 uint32_t unique)
1005 {
1006 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1007 struct bgp_pbr_match_entry_unique bpmeu;
1008 struct bgp_pbr_match_ipsetname bpmi;
1009
1010 if (!bgp || unique == 0)
1011 return NULL;
1012 bpmi.ipsetname = XCALLOC(MTYPE_TMP, ZEBRA_IPSET_NAME_SIZE);
1013 snprintf(bpmi.ipsetname, ZEBRA_IPSET_NAME_SIZE, "%s", ipset_name);
1014 bpmi.bpm_found = NULL;
1015 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_pername_walkcb, &bpmi);
1016 XFREE(MTYPE_TMP, bpmi.ipsetname);
1017 if (!bpmi.bpm_found)
1018 return NULL;
1019 bpmeu.bpme_found = NULL;
1020 bpmeu.unique = unique;
1021 hash_walk(bpmi.bpm_found->entry_hash,
1022 bgp_pbr_match_entry_walkcb, &bpmeu);
1023 return bpmeu.bpme_found;
1024 }
1025
1026 struct bgp_pbr_match *bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id,
1027 uint32_t unique)
1028 {
1029 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1030 struct bgp_pbr_match_iptable_unique bpmiu;
1031
1032 if (!bgp || unique == 0)
1033 return NULL;
1034 bpmiu.unique = unique;
1035 bpmiu.bpm_found = NULL;
1036 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_iptable_walkcb, &bpmiu);
1037 return bpmiu.bpm_found;
1038 }
1039
1040 void bgp_pbr_cleanup(struct bgp *bgp)
1041 {
1042 if (bgp->pbr_match_hash) {
1043 hash_clean(bgp->pbr_match_hash, bgp_pbr_match_free);
1044 hash_free(bgp->pbr_match_hash);
1045 bgp->pbr_match_hash = NULL;
1046 }
1047 if (bgp->pbr_action_hash) {
1048 hash_clean(bgp->pbr_action_hash, bgp_pbr_action_free);
1049 hash_free(bgp->pbr_action_hash);
1050 bgp->pbr_action_hash = NULL;
1051 }
1052 if (bgp->bgp_pbr_cfg == NULL)
1053 return;
1054 bgp_pbr_reset(bgp, AFI_IP);
1055 XFREE(MTYPE_PBR, bgp->bgp_pbr_cfg);
1056 bgp->bgp_pbr_cfg = NULL;
1057 }
1058
1059 void bgp_pbr_init(struct bgp *bgp)
1060 {
1061 bgp->pbr_match_hash =
1062 hash_create_size(8, bgp_pbr_match_hash_key,
1063 bgp_pbr_match_hash_equal,
1064 "Match Hash");
1065 bgp->pbr_action_hash =
1066 hash_create_size(8, bgp_pbr_action_hash_key,
1067 bgp_pbr_action_hash_equal,
1068 "Match Hash Entry");
1069
1070 bgp->bgp_pbr_cfg = XCALLOC(MTYPE_PBR, sizeof(struct bgp_pbr_config));
1071 bgp->bgp_pbr_cfg->pbr_interface_any_ipv4 = true;
1072 }
1073
1074 void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main *api)
1075 {
1076 int i = 0;
1077 char return_string[512];
1078 char *ptr = return_string;
1079 char buff[64];
1080 int nb_items = 0;
1081
1082 ptr += sprintf(ptr, "MATCH : ");
1083 if (api->match_bitmask & PREFIX_SRC_PRESENT) {
1084 struct prefix *p = &(api->src_prefix);
1085
1086 ptr += sprintf(ptr, "@src %s", prefix2str(p, buff, 64));
1087 INCREMENT_DISPLAY(ptr, nb_items);
1088 }
1089 if (api->match_bitmask & PREFIX_DST_PRESENT) {
1090 struct prefix *p = &(api->dst_prefix);
1091
1092 INCREMENT_DISPLAY(ptr, nb_items);
1093 ptr += sprintf(ptr, "@dst %s", prefix2str(p, buff, 64));
1094 }
1095
1096 if (api->match_protocol_num)
1097 INCREMENT_DISPLAY(ptr, nb_items);
1098 for (i = 0; i < api->match_protocol_num; i++)
1099 ptr += sprintf_bgp_pbr_match_val(ptr, &api->protocol[i],
1100 i > 0 ? NULL : "@proto ");
1101
1102 if (api->match_src_port_num)
1103 INCREMENT_DISPLAY(ptr, nb_items);
1104 for (i = 0; i < api->match_src_port_num; i++)
1105 ptr += sprintf_bgp_pbr_match_val(ptr, &api->src_port[i],
1106 i > 0 ? NULL : "@srcport ");
1107
1108 if (api->match_dst_port_num)
1109 INCREMENT_DISPLAY(ptr, nb_items);
1110 for (i = 0; i < api->match_dst_port_num; i++)
1111 ptr += sprintf_bgp_pbr_match_val(ptr, &api->dst_port[i],
1112 i > 0 ? NULL : "@dstport ");
1113
1114 if (api->match_port_num)
1115 INCREMENT_DISPLAY(ptr, nb_items);
1116 for (i = 0; i < api->match_port_num; i++)
1117 ptr += sprintf_bgp_pbr_match_val(ptr, &api->port[i],
1118 i > 0 ? NULL : "@port ");
1119
1120 if (api->match_icmp_type_num)
1121 INCREMENT_DISPLAY(ptr, nb_items);
1122 for (i = 0; i < api->match_icmp_type_num; i++)
1123 ptr += sprintf_bgp_pbr_match_val(ptr, &api->icmp_type[i],
1124 i > 0 ? NULL : "@icmptype ");
1125
1126 if (api->match_icmp_code_num)
1127 INCREMENT_DISPLAY(ptr, nb_items);
1128 for (i = 0; i < api->match_icmp_code_num; i++)
1129 ptr += sprintf_bgp_pbr_match_val(ptr, &api->icmp_code[i],
1130 i > 0 ? NULL : "@icmpcode ");
1131
1132 if (api->match_packet_length_num)
1133 INCREMENT_DISPLAY(ptr, nb_items);
1134 for (i = 0; i < api->match_packet_length_num; i++)
1135 ptr += sprintf_bgp_pbr_match_val(ptr, &api->packet_length[i],
1136 i > 0 ? NULL : "@plen ");
1137
1138 if (api->match_dscp_num)
1139 INCREMENT_DISPLAY(ptr, nb_items);
1140 for (i = 0; i < api->match_dscp_num; i++)
1141 ptr += sprintf_bgp_pbr_match_val(ptr, &api->dscp[i],
1142 i > 0 ? NULL : "@dscp ");
1143
1144 if (api->match_tcpflags_num)
1145 INCREMENT_DISPLAY(ptr, nb_items);
1146 for (i = 0; i < api->match_tcpflags_num; i++)
1147 ptr += sprintf_bgp_pbr_match_val(ptr, &api->tcpflags[i],
1148 i > 0 ? NULL : "@tcpflags ");
1149
1150 if (api->match_fragment_num)
1151 INCREMENT_DISPLAY(ptr, nb_items);
1152 for (i = 0; i < api->match_fragment_num; i++)
1153 ptr += sprintf_bgp_pbr_match_val(ptr, &api->fragment[i],
1154 i > 0 ? NULL : "@fragment ");
1155 if (!nb_items)
1156 ptr = return_string;
1157 else
1158 ptr += sprintf(ptr, "; ");
1159 if (api->action_num)
1160 ptr += sprintf(ptr, "SET : ");
1161 nb_items = 0;
1162 for (i = 0; i < api->action_num; i++) {
1163 switch (api->actions[i].action) {
1164 case ACTION_TRAFFICRATE:
1165 INCREMENT_DISPLAY(ptr, nb_items);
1166 ptr += sprintf(ptr, "@set rate %f",
1167 api->actions[i].u.r.rate);
1168 break;
1169 case ACTION_TRAFFIC_ACTION:
1170 INCREMENT_DISPLAY(ptr, nb_items);
1171 ptr += sprintf(ptr, "@action ");
1172 if (api->actions[i].u.za.filter
1173 & TRAFFIC_ACTION_TERMINATE)
1174 ptr += sprintf(ptr,
1175 " terminate (apply filter(s))");
1176 if (api->actions[i].u.za.filter
1177 & TRAFFIC_ACTION_DISTRIBUTE)
1178 ptr += sprintf(ptr, " distribute");
1179 if (api->actions[i].u.za.filter
1180 & TRAFFIC_ACTION_SAMPLE)
1181 ptr += sprintf(ptr, " sample");
1182 break;
1183 case ACTION_REDIRECT_IP:
1184 INCREMENT_DISPLAY(ptr, nb_items);
1185 char local_buff[INET_ADDRSTRLEN];
1186
1187 if (inet_ntop(AF_INET,
1188 &api->actions[i].u.zr.redirect_ip_v4,
1189 local_buff, INET_ADDRSTRLEN) != NULL)
1190 ptr += sprintf(ptr,
1191 "@redirect ip nh %s", local_buff);
1192 break;
1193 case ACTION_REDIRECT:
1194 INCREMENT_DISPLAY(ptr, nb_items);
1195 ptr += sprintf(ptr, "@redirect vrf %u",
1196 api->actions[i].u.redirect_vrf);
1197 break;
1198 case ACTION_MARKING:
1199 INCREMENT_DISPLAY(ptr, nb_items);
1200 ptr += sprintf(ptr, "@set dscp %u",
1201 api->actions[i].u.marking_dscp);
1202 break;
1203 default:
1204 break;
1205 }
1206 }
1207 zlog_info("%s", return_string);
1208 }
1209
1210 static void bgp_pbr_flush_entry(struct bgp *bgp, struct bgp_pbr_action *bpa,
1211 struct bgp_pbr_match *bpm,
1212 struct bgp_pbr_match_entry *bpme)
1213 {
1214 /* if bpme is null, bpm is also null
1215 */
1216 if (bpme == NULL)
1217 return;
1218 /* ipset del entry */
1219 if (bpme->installed) {
1220 bgp_send_pbr_ipset_entry_match(bpme, false);
1221 bpme->installed = false;
1222 bpme->backpointer = NULL;
1223 if (bpme->path) {
1224 struct bgp_path_info *path;
1225 struct bgp_path_info_extra *extra;
1226
1227 /* unlink bgp_path_info to bpme */
1228 path = (struct bgp_path_info *)bpme->path;
1229 extra = bgp_path_info_extra_get(path);
1230 if (extra->bgp_fs_pbr)
1231 listnode_delete(extra->bgp_fs_pbr, bpme);
1232 bpme->path = NULL;
1233 }
1234 }
1235 hash_release(bpm->entry_hash, bpme);
1236 if (hashcount(bpm->entry_hash) == 0) {
1237 /* delete iptable entry first */
1238 /* then delete ipset match */
1239 if (bpm->installed) {
1240 if (bpm->installed_in_iptable) {
1241 bgp_send_pbr_iptable(bpm->action,
1242 bpm, false);
1243 bpm->installed_in_iptable = false;
1244 bpm->action->refcnt--;
1245 }
1246 bgp_send_pbr_ipset_match(bpm, false);
1247 bpm->installed = false;
1248 bpm->action = NULL;
1249 }
1250 hash_release(bgp->pbr_match_hash, bpm);
1251 /* XXX release pbr_match_action if not used
1252 * note that drop does not need to call send_pbr_action
1253 */
1254 }
1255 if (bpa->refcnt == 0) {
1256 if (bpa->installed && bpa->table_id != 0) {
1257 bgp_send_pbr_rule_action(bpa, false);
1258 bgp_zebra_announce_default(bpa->bgp, &(bpa->nh),
1259 AFI_IP,
1260 bpa->table_id,
1261 false);
1262 bpa->installed = false;
1263 }
1264 }
1265 }
1266
1267 struct bgp_pbr_match_entry_remain {
1268 struct bgp_pbr_match_entry *bpme_to_match;
1269 struct bgp_pbr_match_entry *bpme_found;
1270 };
1271
1272 static int bgp_pbr_get_remaining_entry(struct hash_backet *backet, void *arg)
1273 {
1274 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data;
1275 struct bgp_pbr_match_entry_remain *bpmer =
1276 (struct bgp_pbr_match_entry_remain *)arg;
1277 struct bgp_pbr_match *bpm_temp;
1278 struct bgp_pbr_match_entry *bpme = bpmer->bpme_to_match;
1279
1280 if (!bpme->backpointer ||
1281 bpm == bpme->backpointer ||
1282 bpme->backpointer->action == bpm->action)
1283 return HASHWALK_CONTINUE;
1284 /* ensure bpm other characteristics are equal */
1285 bpm_temp = bpme->backpointer;
1286 if (bpm_temp->vrf_id != bpm->vrf_id ||
1287 bpm_temp->type != bpm->type ||
1288 bpm_temp->flags != bpm->flags ||
1289 bpm_temp->tcp_flags != bpm->tcp_flags ||
1290 bpm_temp->tcp_mask_flags != bpm->tcp_mask_flags ||
1291 bpm_temp->pkt_len_min != bpm->pkt_len_min ||
1292 bpm_temp->pkt_len_max != bpm->pkt_len_max ||
1293 bpm_temp->dscp_value != bpm->dscp_value ||
1294 bpm_temp->fragment != bpm->fragment)
1295 return HASHWALK_CONTINUE;
1296
1297 /* look for remaining bpme */
1298 bpmer->bpme_found = hash_lookup(bpm->entry_hash, bpme);
1299 if (!bpmer->bpme_found)
1300 return HASHWALK_CONTINUE;
1301 return HASHWALK_ABORT;
1302 }
1303
1304 static void bgp_pbr_policyroute_remove_from_zebra_unit(
1305 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf)
1306 {
1307 struct bgp_pbr_match temp;
1308 struct bgp_pbr_match_entry temp2;
1309 struct bgp_pbr_match *bpm;
1310 struct bgp_pbr_match_entry *bpme;
1311 struct bgp_pbr_match_entry_remain bpmer;
1312 struct bgp_pbr_range_port *src_port;
1313 struct bgp_pbr_range_port *dst_port;
1314 struct bgp_pbr_range_port *pkt_len;
1315
1316 if (!bpf)
1317 return;
1318 src_port = bpf->src_port;
1319 dst_port = bpf->dst_port;
1320 pkt_len = bpf->pkt_len;
1321
1322 if (BGP_DEBUG(zebra, ZEBRA))
1323 bgp_pbr_dump_entry(bpf, false);
1324
1325 /* as we don't know information from EC
1326 * look for bpm that have the bpm
1327 * with vrf_id characteristics
1328 */
1329 memset(&temp2, 0, sizeof(temp2));
1330 memset(&temp, 0, sizeof(temp));
1331 if (bpf->src) {
1332 temp.flags |= MATCH_IP_SRC_SET;
1333 prefix_copy(&temp2.src, bpf->src);
1334 } else
1335 temp2.src.family = AF_INET;
1336 if (bpf->dst) {
1337 temp.flags |= MATCH_IP_DST_SET;
1338 prefix_copy(&temp2.dst, bpf->dst);
1339 } else
1340 temp2.dst.family = AF_INET;
1341 if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1342 if (bpf->protocol == IPPROTO_ICMP)
1343 temp.flags |= MATCH_ICMP_SET;
1344 temp.flags |= MATCH_PORT_SRC_SET;
1345 temp2.src_port_min = src_port->min_port;
1346 if (src_port->max_port) {
1347 temp.flags |= MATCH_PORT_SRC_RANGE_SET;
1348 temp2.src_port_max = src_port->max_port;
1349 }
1350 }
1351 if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1352 if (bpf->protocol == IPPROTO_ICMP)
1353 temp.flags |= MATCH_ICMP_SET;
1354 temp.flags |= MATCH_PORT_DST_SET;
1355 temp2.dst_port_min = dst_port->min_port;
1356 if (dst_port->max_port) {
1357 temp.flags |= MATCH_PORT_DST_RANGE_SET;
1358 temp2.dst_port_max = dst_port->max_port;
1359 }
1360 }
1361 temp2.proto = bpf->protocol;
1362
1363 if (pkt_len) {
1364 temp.pkt_len_min = pkt_len->min_port;
1365 if (pkt_len->max_port)
1366 temp.pkt_len_max = pkt_len->max_port;
1367 } else if (bpf->pkt_len_val) {
1368 if (bpf->pkt_len_val->mask)
1369 temp.flags |= MATCH_PKT_LEN_INVERSE_SET;
1370 temp.pkt_len_min = bpf->pkt_len_val->val;
1371 }
1372 if (bpf->tcp_flags) {
1373 temp.tcp_flags = bpf->tcp_flags->val;
1374 temp.tcp_mask_flags = bpf->tcp_flags->mask;
1375 }
1376 if (bpf->dscp) {
1377 if (bpf->dscp->mask)
1378 temp.flags |= MATCH_DSCP_INVERSE_SET;
1379 else
1380 temp.flags |= MATCH_DSCP_SET;
1381 temp.dscp_value = bpf->dscp->val;
1382 }
1383 if (bpf->fragment) {
1384 if (bpf->fragment->mask)
1385 temp.flags |= MATCH_FRAGMENT_INVERSE_SET;
1386 temp.fragment = bpf->fragment->val;
1387 }
1388
1389 if (bpf->src == NULL || bpf->dst == NULL) {
1390 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1391 temp.type = IPSET_NET_PORT;
1392 else
1393 temp.type = IPSET_NET;
1394 } else {
1395 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1396 temp.type = IPSET_NET_PORT_NET;
1397 else
1398 temp.type = IPSET_NET_NET;
1399 }
1400 if (bpf->vrf_id == VRF_UNKNOWN) /* XXX case BGP destroy */
1401 temp.vrf_id = 0;
1402 else
1403 temp.vrf_id = bpf->vrf_id;
1404 bpme = &temp2;
1405 bpm = &temp;
1406 bpme->backpointer = bpm;
1407 /* right now, a previous entry may already exist
1408 * flush previous entry if necessary
1409 */
1410 bpmer.bpme_to_match = bpme;
1411 bpmer.bpme_found = NULL;
1412 hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer);
1413 if (bpmer.bpme_found) {
1414 static struct bgp_pbr_match *local_bpm;
1415 static struct bgp_pbr_action *local_bpa;
1416
1417 local_bpm = bpmer.bpme_found->backpointer;
1418 local_bpa = local_bpm->action;
1419 bgp_pbr_flush_entry(bgp, local_bpa,
1420 local_bpm, bpmer.bpme_found);
1421 }
1422 }
1423
1424 static uint8_t bgp_pbr_next_type_entry(uint8_t type_entry)
1425 {
1426 if (type_entry == FLOWSPEC_TCP_FLAGS)
1427 return FLOWSPEC_DSCP;
1428 if (type_entry == FLOWSPEC_DSCP)
1429 return FLOWSPEC_PKT_LEN;
1430 if (type_entry == FLOWSPEC_PKT_LEN)
1431 return FLOWSPEC_FRAGMENT;
1432 if (type_entry == FLOWSPEC_FRAGMENT)
1433 return FLOWSPEC_ICMP_TYPE;
1434 return 0;
1435 }
1436
1437 static void bgp_pbr_icmp_action(struct bgp *bgp, struct bgp_path_info *path,
1438 struct bgp_pbr_filter *bpf,
1439 struct bgp_pbr_or_filter *bpof, bool add,
1440 struct nexthop *nh, float *rate)
1441 {
1442 struct bgp_pbr_range_port srcp, dstp;
1443 struct bgp_pbr_val_mask *icmp_type, *icmp_code;
1444 struct listnode *tnode, *cnode;
1445
1446 if (!bpf)
1447 return;
1448 if (bpf->protocol != IPPROTO_ICMP)
1449 return;
1450 bpf->src_port = &srcp;
1451 bpf->dst_port = &dstp;
1452 /* parse icmp type and lookup appropriate icmp code
1453 * if no icmp code found, create as many entryes as
1454 * there are listed icmp codes for that icmp type
1455 */
1456 if (!bpof->icmp_type) {
1457 srcp.min_port = 0;
1458 srcp.max_port = 255;
1459 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) {
1460 dstp.min_port = icmp_code->val;
1461 if (add)
1462 bgp_pbr_policyroute_add_to_zebra_unit(
1463 bgp, path, bpf, nh, rate);
1464 else
1465 bgp_pbr_policyroute_remove_from_zebra_unit(
1466 bgp, path, bpf);
1467 }
1468 return;
1469 }
1470 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_type, tnode, icmp_type)) {
1471 srcp.min_port = icmp_type->val;
1472 srcp.max_port = 0;
1473 dstp.max_port = 0;
1474 /* only icmp type. create an entry only with icmp type */
1475 if (!bpof->icmp_code) {
1476 /* icmp type is not one of the above
1477 * forge an entry only based on the icmp type
1478 */
1479 dstp.min_port = 0;
1480 dstp.max_port = 255;
1481 if (add)
1482 bgp_pbr_policyroute_add_to_zebra_unit(
1483 bgp, path, bpf, nh, rate);
1484 else
1485 bgp_pbr_policyroute_remove_from_zebra_unit(
1486 bgp, path, bpf);
1487 continue;
1488 }
1489 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) {
1490 dstp.min_port = icmp_code->val;
1491 if (add)
1492 bgp_pbr_policyroute_add_to_zebra_unit(
1493 bgp, path, bpf, nh, rate);
1494 else
1495 bgp_pbr_policyroute_remove_from_zebra_unit(
1496 bgp, path, bpf);
1497 }
1498 }
1499 }
1500
1501 static void bgp_pbr_policyroute_remove_from_zebra_recursive(
1502 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
1503 struct bgp_pbr_or_filter *bpof, uint8_t type_entry)
1504 {
1505 struct listnode *node, *nnode;
1506 struct bgp_pbr_val_mask *valmask;
1507 uint8_t next_type_entry;
1508 struct list *orig_list;
1509 struct bgp_pbr_val_mask **target_val;
1510
1511 if (type_entry == 0)
1512 return bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path,
1513 bpf);
1514 next_type_entry = bgp_pbr_next_type_entry(type_entry);
1515 if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) {
1516 orig_list = bpof->tcpflags;
1517 target_val = &bpf->tcp_flags;
1518 } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) {
1519 orig_list = bpof->dscp;
1520 target_val = &bpf->dscp;
1521 } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) {
1522 orig_list = bpof->pkt_len;
1523 target_val = &bpf->pkt_len_val;
1524 } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) {
1525 orig_list = bpof->fragment;
1526 target_val = &bpf->fragment;
1527 } else if (type_entry == FLOWSPEC_ICMP_TYPE &&
1528 (bpof->icmp_type || bpof->icmp_code)) {
1529 /* enumerate list for icmp - must be last one */
1530 bgp_pbr_icmp_action(bgp, path, bpf, bpof, false, NULL, NULL);
1531 return;
1532 } else {
1533 return bgp_pbr_policyroute_remove_from_zebra_recursive(
1534 bgp, path, bpf, bpof, next_type_entry);
1535 }
1536 for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) {
1537 *target_val = valmask;
1538 bgp_pbr_policyroute_remove_from_zebra_recursive(
1539 bgp, path, bpf, bpof, next_type_entry);
1540 }
1541 }
1542
1543 static void bgp_pbr_policyroute_remove_from_zebra(
1544 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
1545 struct bgp_pbr_or_filter *bpof)
1546 {
1547 if (!bpof)
1548 return bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path,
1549 bpf);
1550 if (bpof->tcpflags)
1551 bgp_pbr_policyroute_remove_from_zebra_recursive(
1552 bgp, path, bpf, bpof, FLOWSPEC_TCP_FLAGS);
1553 else if (bpof->dscp)
1554 bgp_pbr_policyroute_remove_from_zebra_recursive(
1555 bgp, path, bpf, bpof, FLOWSPEC_DSCP);
1556 else if (bpof->pkt_len)
1557 bgp_pbr_policyroute_remove_from_zebra_recursive(
1558 bgp, path, bpf, bpof, FLOWSPEC_PKT_LEN);
1559 else if (bpof->fragment)
1560 bgp_pbr_policyroute_remove_from_zebra_recursive(
1561 bgp, path, bpf, bpof, FLOWSPEC_FRAGMENT);
1562 else if (bpof->icmp_type || bpof->icmp_code)
1563 bgp_pbr_policyroute_remove_from_zebra_recursive(
1564 bgp, path, bpf, bpof, FLOWSPEC_ICMP_TYPE);
1565 else
1566 bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path, bpf);
1567 /* flush bpof */
1568 if (bpof->tcpflags)
1569 list_delete_all_node(bpof->tcpflags);
1570 if (bpof->dscp)
1571 list_delete_all_node(bpof->dscp);
1572 if (bpof->pkt_len)
1573 list_delete_all_node(bpof->pkt_len);
1574 if (bpof->fragment)
1575 list_delete_all_node(bpof->fragment);
1576 }
1577
1578 static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add)
1579 {
1580 struct bgp_pbr_range_port *src_port;
1581 struct bgp_pbr_range_port *dst_port;
1582 struct bgp_pbr_range_port *pkt_len;
1583 char bufsrc[64], bufdst[64];
1584 char buffer[64];
1585 int remaining_len = 0;
1586 char protocol_str[16];
1587
1588 if (!bpf)
1589 return;
1590 src_port = bpf->src_port;
1591 dst_port = bpf->dst_port;
1592 pkt_len = bpf->pkt_len;
1593
1594 protocol_str[0] = '\0';
1595 if (bpf->tcp_flags && bpf->tcp_flags->mask)
1596 bpf->protocol = IPPROTO_TCP;
1597 if (bpf->protocol)
1598 snprintf(protocol_str, sizeof(protocol_str),
1599 "proto %d", bpf->protocol);
1600 buffer[0] = '\0';
1601 if (bpf->protocol == IPPROTO_ICMP && src_port && dst_port)
1602 remaining_len += snprintf(buffer, sizeof(buffer),
1603 "type %d, code %d",
1604 src_port->min_port,
1605 dst_port->min_port);
1606 else if (bpf->protocol == IPPROTO_UDP ||
1607 bpf->protocol == IPPROTO_TCP) {
1608
1609 if (src_port && src_port->min_port)
1610 remaining_len += snprintf(buffer,
1611 sizeof(buffer),
1612 "from [%u:%u]",
1613 src_port->min_port,
1614 src_port->max_port ?
1615 src_port->max_port :
1616 src_port->min_port);
1617 if (dst_port && dst_port->min_port)
1618 remaining_len += snprintf(buffer +
1619 remaining_len,
1620 sizeof(buffer)
1621 - remaining_len,
1622 "to [%u:%u]",
1623 dst_port->min_port,
1624 dst_port->max_port ?
1625 dst_port->max_port :
1626 dst_port->min_port);
1627 }
1628 if (pkt_len && (pkt_len->min_port || pkt_len->max_port)) {
1629 remaining_len += snprintf(buffer + remaining_len,
1630 sizeof(buffer)
1631 - remaining_len,
1632 " len [%u:%u]",
1633 pkt_len->min_port,
1634 pkt_len->max_port ?
1635 pkt_len->max_port :
1636 pkt_len->min_port);
1637 } else if (bpf->pkt_len_val) {
1638 remaining_len += snprintf(buffer + remaining_len,
1639 sizeof(buffer)
1640 - remaining_len,
1641 " %s len %u",
1642 bpf->pkt_len_val->mask
1643 ? "!" : "",
1644 bpf->pkt_len_val->val);
1645 }
1646 if (bpf->tcp_flags) {
1647 remaining_len += snprintf(buffer + remaining_len,
1648 sizeof(buffer)
1649 - remaining_len,
1650 "tcpflags %x/%x",
1651 bpf->tcp_flags->val,
1652 bpf->tcp_flags->mask);
1653 }
1654 if (bpf->dscp) {
1655 snprintf(buffer + remaining_len,
1656 sizeof(buffer)
1657 - remaining_len,
1658 "%s dscp %d",
1659 bpf->dscp->mask
1660 ? "!" : "",
1661 bpf->dscp->val);
1662 }
1663 zlog_debug("BGP: %s FS PBR from %s to %s, %s %s",
1664 add ? "adding" : "removing",
1665 bpf->src == NULL ? "<all>" :
1666 prefix2str(bpf->src, bufsrc, sizeof(bufsrc)),
1667 bpf->dst == NULL ? "<all>" :
1668 prefix2str(bpf->dst, bufdst, sizeof(bufdst)),
1669 protocol_str, buffer);
1670
1671 }
1672
1673 static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp *bgp,
1674 struct bgp_path_info *path,
1675 struct bgp_pbr_filter *bpf,
1676 struct nexthop *nh,
1677 float *rate)
1678 {
1679 struct bgp_pbr_match temp;
1680 struct bgp_pbr_match_entry temp2;
1681 struct bgp_pbr_match *bpm;
1682 struct bgp_pbr_match_entry *bpme = NULL;
1683 struct bgp_pbr_action temp3;
1684 struct bgp_pbr_action *bpa = NULL;
1685 struct bgp_pbr_match_entry_remain bpmer;
1686 struct bgp_pbr_range_port *src_port;
1687 struct bgp_pbr_range_port *dst_port;
1688 struct bgp_pbr_range_port *pkt_len;
1689 bool bpme_found = false;
1690
1691 if (!bpf)
1692 return;
1693 src_port = bpf->src_port;
1694 dst_port = bpf->dst_port;
1695 pkt_len = bpf->pkt_len;
1696
1697 if (BGP_DEBUG(zebra, ZEBRA))
1698 bgp_pbr_dump_entry(bpf, true);
1699
1700 /* look for bpa first */
1701 memset(&temp3, 0, sizeof(temp3));
1702 if (rate)
1703 temp3.rate = *rate;
1704 if (nh)
1705 memcpy(&temp3.nh, nh, sizeof(struct nexthop));
1706 temp3.vrf_id = bpf->vrf_id;
1707 bpa = hash_get(bgp->pbr_action_hash, &temp3,
1708 bgp_pbr_action_alloc_intern);
1709
1710 if (bpa->fwmark == 0) {
1711 /* drop is handled by iptable */
1712 if (nh && nh->type == NEXTHOP_TYPE_BLACKHOLE) {
1713 bpa->table_id = 0;
1714 bpa->installed = true;
1715 } else {
1716 bpa->fwmark = bgp_zebra_tm_get_id();
1717 bpa->table_id = bpa->fwmark;
1718 bpa->installed = false;
1719 }
1720 bpa->bgp = bgp;
1721 bpa->unique = ++bgp_pbr_action_counter_unique;
1722 /* 0 value is forbidden */
1723 bpa->install_in_progress = false;
1724 }
1725
1726 /* then look for bpm */
1727 memset(&temp, 0, sizeof(temp));
1728 temp.vrf_id = bpf->vrf_id;
1729 if (bpf->src)
1730 temp.flags |= MATCH_IP_SRC_SET;
1731 if (bpf->dst)
1732 temp.flags |= MATCH_IP_DST_SET;
1733
1734 if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1735 if (bpf->protocol == IPPROTO_ICMP)
1736 temp.flags |= MATCH_ICMP_SET;
1737 temp.flags |= MATCH_PORT_SRC_SET;
1738 }
1739 if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1740 if (bpf->protocol == IPPROTO_ICMP)
1741 temp.flags |= MATCH_ICMP_SET;
1742 temp.flags |= MATCH_PORT_DST_SET;
1743 }
1744 if (src_port && src_port->max_port)
1745 temp.flags |= MATCH_PORT_SRC_RANGE_SET;
1746 if (dst_port && dst_port->max_port)
1747 temp.flags |= MATCH_PORT_DST_RANGE_SET;
1748
1749 if (bpf->src == NULL || bpf->dst == NULL) {
1750 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1751 temp.type = IPSET_NET_PORT;
1752 else
1753 temp.type = IPSET_NET;
1754 } else {
1755 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1756 temp.type = IPSET_NET_PORT_NET;
1757 else
1758 temp.type = IPSET_NET_NET;
1759 }
1760 if (pkt_len) {
1761 temp.pkt_len_min = pkt_len->min_port;
1762 if (pkt_len->max_port)
1763 temp.pkt_len_max = pkt_len->max_port;
1764 } else if (bpf->pkt_len_val) {
1765 if (bpf->pkt_len_val->mask)
1766 temp.flags |= MATCH_PKT_LEN_INVERSE_SET;
1767 temp.pkt_len_min = bpf->pkt_len_val->val;
1768 }
1769 if (bpf->tcp_flags) {
1770 temp.tcp_flags = bpf->tcp_flags->val;
1771 temp.tcp_mask_flags = bpf->tcp_flags->mask;
1772 }
1773 if (bpf->dscp) {
1774 if (bpf->dscp->mask)
1775 temp.flags |= MATCH_DSCP_INVERSE_SET;
1776 else
1777 temp.flags |= MATCH_DSCP_SET;
1778 temp.dscp_value = bpf->dscp->val;
1779 }
1780 if (bpf->fragment) {
1781 if (bpf->fragment->mask)
1782 temp.flags |= MATCH_FRAGMENT_INVERSE_SET;
1783 temp.fragment = bpf->fragment->val;
1784 }
1785 temp.action = bpa;
1786 bpm = hash_get(bgp->pbr_match_hash, &temp,
1787 bgp_pbr_match_alloc_intern);
1788
1789 /* new, then self allocate ipset_name and unique */
1790 if (bpm->unique == 0) {
1791 bpm->unique = ++bgp_pbr_match_counter_unique;
1792 /* 0 value is forbidden */
1793 sprintf(bpm->ipset_name, "match%p", bpm);
1794 bpm->entry_hash = hash_create_size(8,
1795 bgp_pbr_match_entry_hash_key,
1796 bgp_pbr_match_entry_hash_equal,
1797 "Match Entry Hash");
1798 bpm->installed = false;
1799
1800 /* unique2 should be updated too */
1801 bpm->unique2 = ++bgp_pbr_match_iptable_counter_unique;
1802 bpm->installed_in_iptable = false;
1803 bpm->install_in_progress = false;
1804 bpm->install_iptable_in_progress = false;
1805 }
1806
1807 memset(&temp2, 0, sizeof(temp2));
1808 if (bpf->src)
1809 prefix_copy(&temp2.src, bpf->src);
1810 else
1811 temp2.src.family = AF_INET;
1812 if (bpf->dst)
1813 prefix_copy(&temp2.dst, bpf->dst);
1814 else
1815 temp2.dst.family = AF_INET;
1816 temp2.src_port_min = src_port ? src_port->min_port : 0;
1817 temp2.dst_port_min = dst_port ? dst_port->min_port : 0;
1818 temp2.src_port_max = src_port ? src_port->max_port : 0;
1819 temp2.dst_port_max = dst_port ? dst_port->max_port : 0;
1820 temp2.proto = bpf->protocol;
1821 bpme = hash_get(bpm->entry_hash, &temp2,
1822 bgp_pbr_match_entry_alloc_intern);
1823 if (bpme->unique == 0) {
1824 bpme->unique = ++bgp_pbr_match_entry_counter_unique;
1825 /* 0 value is forbidden */
1826 bpme->backpointer = bpm;
1827 bpme->installed = false;
1828 bpme->install_in_progress = false;
1829 /* link bgp info to bpme */
1830 bpme->path = (void *)path;
1831 } else
1832 bpme_found = true;
1833
1834 /* already installed */
1835 if (bpme_found) {
1836 struct bgp_path_info_extra *extra =
1837 bgp_path_info_extra_get(path);
1838
1839 if (extra && extra->bgp_fs_pbr &&
1840 listnode_lookup(extra->bgp_fs_pbr, bpme)) {
1841 if (BGP_DEBUG(pbr, PBR_ERROR))
1842 zlog_err(
1843 "%s: entry %p/%p already installed in bgp pbr",
1844 __func__, path, bpme);
1845 return;
1846 }
1847 }
1848 /* BGP FS: append entry to zebra
1849 * - policies are not routing entries and as such
1850 * route replace semantics don't necessarily follow
1851 * through to policy entries
1852 * - because of that, not all policing information will be stored
1853 * into zebra. and non selected policies will be suppressed from zebra
1854 * - as consequence, in order to bring consistency
1855 * a policy will be added, then ifan ecmp policy exists,
1856 * it will be suppressed subsequently
1857 */
1858 /* ip rule add */
1859 if (!bpa->installed && !bpa->install_in_progress) {
1860 bgp_send_pbr_rule_action(bpa, true);
1861 bgp_zebra_announce_default(bgp, nh,
1862 AFI_IP, bpa->table_id, true);
1863 }
1864
1865 /* ipset create */
1866 if (!bpm->installed)
1867 bgp_send_pbr_ipset_match(bpm, true);
1868 /* ipset add */
1869 if (!bpme->installed)
1870 bgp_send_pbr_ipset_entry_match(bpme, true);
1871
1872 /* iptables */
1873 if (!bpm->installed_in_iptable)
1874 bgp_send_pbr_iptable(bpa, bpm, true);
1875
1876 /* A previous entry may already exist
1877 * flush previous entry if necessary
1878 */
1879 bpmer.bpme_to_match = bpme;
1880 bpmer.bpme_found = NULL;
1881 hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer);
1882 if (bpmer.bpme_found) {
1883 static struct bgp_pbr_match *local_bpm;
1884 static struct bgp_pbr_action *local_bpa;
1885
1886 local_bpm = bpmer.bpme_found->backpointer;
1887 local_bpa = local_bpm->action;
1888 bgp_pbr_flush_entry(bgp, local_bpa,
1889 local_bpm, bpmer.bpme_found);
1890 }
1891
1892
1893 }
1894
1895 static void bgp_pbr_policyroute_add_to_zebra_recursive(
1896 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
1897 struct bgp_pbr_or_filter *bpof, struct nexthop *nh, float *rate,
1898 uint8_t type_entry)
1899 {
1900 struct listnode *node, *nnode;
1901 struct bgp_pbr_val_mask *valmask;
1902 uint8_t next_type_entry;
1903 struct list *orig_list;
1904 struct bgp_pbr_val_mask **target_val;
1905
1906 if (type_entry == 0)
1907 return bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh,
1908 rate);
1909 next_type_entry = bgp_pbr_next_type_entry(type_entry);
1910 if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) {
1911 orig_list = bpof->tcpflags;
1912 target_val = &bpf->tcp_flags;
1913 } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) {
1914 orig_list = bpof->dscp;
1915 target_val = &bpf->dscp;
1916 } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) {
1917 orig_list = bpof->pkt_len;
1918 target_val = &bpf->pkt_len_val;
1919 } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) {
1920 orig_list = bpof->fragment;
1921 target_val = &bpf->fragment;
1922 } else if (type_entry == FLOWSPEC_ICMP_TYPE &&
1923 (bpof->icmp_type || bpof->icmp_code)) {
1924 /* enumerate list for icmp - must be last one */
1925 bgp_pbr_icmp_action(bgp, path, bpf, bpof, true, nh, rate);
1926 return;
1927 } else {
1928 return bgp_pbr_policyroute_add_to_zebra_recursive(
1929 bgp, path, bpf, bpof, nh, rate, next_type_entry);
1930 }
1931 for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) {
1932 *target_val = valmask;
1933 bgp_pbr_policyroute_add_to_zebra_recursive(
1934 bgp, path, bpf, bpof, nh, rate, next_type_entry);
1935 }
1936 }
1937
1938 static void bgp_pbr_policyroute_add_to_zebra(struct bgp *bgp,
1939 struct bgp_path_info *path,
1940 struct bgp_pbr_filter *bpf,
1941 struct bgp_pbr_or_filter *bpof,
1942 struct nexthop *nh, float *rate)
1943 {
1944 if (!bpof)
1945 return bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh,
1946 rate);
1947 if (bpof->tcpflags)
1948 bgp_pbr_policyroute_add_to_zebra_recursive(
1949 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_TCP_FLAGS);
1950 else if (bpof->dscp)
1951 bgp_pbr_policyroute_add_to_zebra_recursive(
1952 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_DSCP);
1953 else if (bpof->pkt_len)
1954 bgp_pbr_policyroute_add_to_zebra_recursive(
1955 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_PKT_LEN);
1956 else if (bpof->fragment)
1957 bgp_pbr_policyroute_add_to_zebra_recursive(
1958 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_FRAGMENT);
1959 else if (bpof->icmp_type || bpof->icmp_code)
1960 bgp_pbr_policyroute_add_to_zebra_recursive(
1961 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_ICMP_TYPE);
1962 else
1963 bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh, rate);
1964 /* flush bpof */
1965 if (bpof->tcpflags)
1966 list_delete_all_node(bpof->tcpflags);
1967 if (bpof->dscp)
1968 list_delete_all_node(bpof->dscp);
1969 if (bpof->pkt_len)
1970 list_delete_all_node(bpof->pkt_len);
1971 if (bpof->fragment)
1972 list_delete_all_node(bpof->fragment);
1973 if (bpof->icmp_type)
1974 list_delete_all_node(bpof->icmp_type);
1975 if (bpof->icmp_code)
1976 list_delete_all_node(bpof->icmp_code);
1977 }
1978
1979 static void bgp_pbr_handle_entry(struct bgp *bgp, struct bgp_path_info *path,
1980 struct bgp_pbr_entry_main *api, bool add)
1981 {
1982 struct nexthop nh;
1983 int i = 0;
1984 int continue_loop = 1;
1985 float rate = 0;
1986 struct prefix *src = NULL, *dst = NULL;
1987 uint8_t proto = 0;
1988 struct bgp_pbr_range_port *srcp = NULL, *dstp = NULL;
1989 struct bgp_pbr_range_port range, range_icmp_code;
1990 struct bgp_pbr_range_port pkt_len;
1991 struct bgp_pbr_filter bpf;
1992 uint8_t kind_enum;
1993 struct bgp_pbr_or_filter bpof;
1994 struct bgp_pbr_val_mask bpvm;
1995
1996 memset(&nh, 0, sizeof(struct nexthop));
1997 memset(&bpf, 0, sizeof(struct bgp_pbr_filter));
1998 memset(&bpof, 0, sizeof(struct bgp_pbr_or_filter));
1999 if (api->match_bitmask & PREFIX_SRC_PRESENT)
2000 src = &api->src_prefix;
2001 if (api->match_bitmask & PREFIX_DST_PRESENT)
2002 dst = &api->dst_prefix;
2003 memset(&nh, 0, sizeof(struct nexthop));
2004 nh.vrf_id = VRF_UNKNOWN;
2005 if (api->match_protocol_num)
2006 proto = (uint8_t)api->protocol[0].value;
2007 /* if match_port is selected, then either src or dst port will be parsed
2008 * but not both at the same time
2009 */
2010 if (api->match_port_num >= 1) {
2011 bgp_pbr_extract(api->port,
2012 api->match_port_num,
2013 &range);
2014 srcp = dstp = &range;
2015 } else if (api->match_src_port_num >= 1) {
2016 bgp_pbr_extract(api->src_port,
2017 api->match_src_port_num,
2018 &range);
2019 srcp = &range;
2020 dstp = NULL;
2021 } else if (api->match_dst_port_num >= 1) {
2022 bgp_pbr_extract(api->dst_port,
2023 api->match_dst_port_num,
2024 &range);
2025 dstp = &range;
2026 srcp = NULL;
2027 }
2028 if (api->match_icmp_type_num >= 1) {
2029 proto = IPPROTO_ICMP;
2030 if (bgp_pbr_extract(api->icmp_type,
2031 api->match_icmp_type_num,
2032 &range))
2033 srcp = &range;
2034 else {
2035 bpof.icmp_type = list_new();
2036 bgp_pbr_extract_enumerate(api->icmp_type,
2037 api->match_icmp_type_num,
2038 OPERATOR_UNARY_OR,
2039 bpof.icmp_type,
2040 FLOWSPEC_ICMP_TYPE);
2041 }
2042 }
2043 if (api->match_icmp_code_num >= 1) {
2044 proto = IPPROTO_ICMP;
2045 if (bgp_pbr_extract(api->icmp_code,
2046 api->match_icmp_code_num,
2047 &range_icmp_code))
2048 dstp = &range_icmp_code;
2049 else {
2050 bpof.icmp_code = list_new();
2051 bgp_pbr_extract_enumerate(api->icmp_code,
2052 api->match_icmp_code_num,
2053 OPERATOR_UNARY_OR,
2054 bpof.icmp_code,
2055 FLOWSPEC_ICMP_CODE);
2056 }
2057 }
2058
2059 if (api->match_tcpflags_num) {
2060 kind_enum = bgp_pbr_match_val_get_operator(api->tcpflags,
2061 api->match_tcpflags_num);
2062 if (kind_enum == OPERATOR_UNARY_AND) {
2063 bpf.tcp_flags = &bpvm;
2064 bgp_pbr_extract_enumerate(api->tcpflags,
2065 api->match_tcpflags_num,
2066 OPERATOR_UNARY_AND,
2067 bpf.tcp_flags,
2068 FLOWSPEC_TCP_FLAGS);
2069 } else if (kind_enum == OPERATOR_UNARY_OR) {
2070 bpof.tcpflags = list_new();
2071 bgp_pbr_extract_enumerate(api->tcpflags,
2072 api->match_tcpflags_num,
2073 OPERATOR_UNARY_OR,
2074 bpof.tcpflags,
2075 FLOWSPEC_TCP_FLAGS);
2076 }
2077 }
2078 if (api->match_packet_length_num) {
2079 bool ret;
2080
2081 ret = bgp_pbr_extract(api->packet_length,
2082 api->match_packet_length_num,
2083 &pkt_len);
2084 if (ret)
2085 bpf.pkt_len = &pkt_len;
2086 else {
2087 bpof.pkt_len = list_new();
2088 bgp_pbr_extract_enumerate(api->packet_length,
2089 api->match_packet_length_num,
2090 OPERATOR_UNARY_OR,
2091 bpof.pkt_len,
2092 FLOWSPEC_PKT_LEN);
2093 }
2094 }
2095 if (api->match_dscp_num >= 1) {
2096 bpof.dscp = list_new();
2097 bgp_pbr_extract_enumerate(api->dscp, api->match_dscp_num,
2098 OPERATOR_UNARY_OR,
2099 bpof.dscp, FLOWSPEC_DSCP);
2100 }
2101 if (api->match_fragment_num) {
2102 bpof.fragment = list_new();
2103 bgp_pbr_extract_enumerate(api->fragment,
2104 api->match_fragment_num,
2105 OPERATOR_UNARY_OR,
2106 bpof.fragment,
2107 FLOWSPEC_FRAGMENT);
2108 }
2109 bpf.vrf_id = api->vrf_id;
2110 bpf.src = src;
2111 bpf.dst = dst;
2112 bpf.protocol = proto;
2113 bpf.src_port = srcp;
2114 bpf.dst_port = dstp;
2115 if (!add)
2116 return bgp_pbr_policyroute_remove_from_zebra(bgp, path, &bpf,
2117 &bpof);
2118 /* no action for add = true */
2119 for (i = 0; i < api->action_num; i++) {
2120 switch (api->actions[i].action) {
2121 case ACTION_TRAFFICRATE:
2122 /* drop packet */
2123 if (api->actions[i].u.r.rate == 0) {
2124 nh.vrf_id = api->vrf_id;
2125 nh.type = NEXTHOP_TYPE_BLACKHOLE;
2126 bgp_pbr_policyroute_add_to_zebra(
2127 bgp, path, &bpf, &bpof, &nh, &rate);
2128 } else {
2129 /* update rate. can be reentrant */
2130 rate = api->actions[i].u.r.rate;
2131 if (BGP_DEBUG(pbr, PBR)) {
2132 bgp_pbr_print_policy_route(api);
2133 zlog_warn("PBR: ignoring Set action rate %f",
2134 api->actions[i].u.r.rate);
2135 }
2136 }
2137 break;
2138 case ACTION_TRAFFIC_ACTION:
2139 if (api->actions[i].u.za.filter
2140 & TRAFFIC_ACTION_SAMPLE) {
2141 if (BGP_DEBUG(pbr, PBR)) {
2142 bgp_pbr_print_policy_route(api);
2143 zlog_warn("PBR: Sample action Ignored");
2144 }
2145 }
2146 #if 0
2147 if (api->actions[i].u.za.filter
2148 & TRAFFIC_ACTION_DISTRIBUTE) {
2149 if (BGP_DEBUG(pbr, PBR)) {
2150 bgp_pbr_print_policy_route(api);
2151 zlog_warn("PBR: Distribute action Applies");
2152 }
2153 continue_loop = 0;
2154 /* continue forwarding entry as before
2155 * no action
2156 */
2157 }
2158 #endif /* XXX to confirm behaviour of traffic action. for now , ignore */
2159 /* terminate action: run other filters
2160 */
2161 break;
2162 case ACTION_REDIRECT_IP:
2163 nh.type = NEXTHOP_TYPE_IPV4;
2164 nh.gate.ipv4.s_addr =
2165 api->actions[i].u.zr.redirect_ip_v4.s_addr;
2166 nh.vrf_id = api->vrf_id;
2167 bgp_pbr_policyroute_add_to_zebra(bgp, path, &bpf, &bpof,
2168 &nh, &rate);
2169 /* XXX combination with REDIRECT_VRF
2170 * + REDIRECT_NH_IP not done
2171 */
2172 continue_loop = 0;
2173 break;
2174 case ACTION_REDIRECT:
2175 nh.vrf_id = api->actions[i].u.redirect_vrf;
2176 nh.type = NEXTHOP_TYPE_IPV4;
2177 bgp_pbr_policyroute_add_to_zebra(bgp, path, &bpf, &bpof,
2178 &nh, &rate);
2179 continue_loop = 0;
2180 break;
2181 case ACTION_MARKING:
2182 if (BGP_DEBUG(pbr, PBR)) {
2183 bgp_pbr_print_policy_route(api);
2184 zlog_warn("PBR: Set DSCP %u Ignored",
2185 api->actions[i].u.marking_dscp);
2186 }
2187 break;
2188 default:
2189 break;
2190 }
2191 if (continue_loop == 0)
2192 break;
2193 }
2194 }
2195
2196 void bgp_pbr_update_entry(struct bgp *bgp, struct prefix *p,
2197 struct bgp_path_info *info, afi_t afi, safi_t safi,
2198 bool nlri_update)
2199 {
2200 struct bgp_pbr_entry_main api;
2201
2202 if (afi == AFI_IP6)
2203 return; /* IPv6 not supported */
2204 if (safi != SAFI_FLOWSPEC)
2205 return; /* not supported */
2206 /* Make Zebra API structure. */
2207 memset(&api, 0, sizeof(api));
2208 api.vrf_id = bgp->vrf_id;
2209 api.afi = afi;
2210
2211 if (!bgp_zebra_tm_chunk_obtained()) {
2212 if (BGP_DEBUG(pbr, PBR_ERROR))
2213 flog_err(EC_BGP_TABLE_CHUNK,
2214 "%s: table chunk not obtained yet", __func__);
2215 return;
2216 }
2217
2218 if (bgp_pbr_build_and_validate_entry(p, info, &api) < 0) {
2219 if (BGP_DEBUG(pbr, PBR_ERROR))
2220 flog_err(EC_BGP_FLOWSPEC_INSTALLATION,
2221 "%s: cancel updating entry %p in bgp pbr",
2222 __func__, info);
2223 return;
2224 }
2225 bgp_pbr_handle_entry(bgp, info, &api, nlri_update);
2226 }
2227
2228 int bgp_pbr_interface_compare(const struct bgp_pbr_interface *a,
2229 const struct bgp_pbr_interface *b)
2230 {
2231 return strcmp(a->name, b->name);
2232 }
2233
2234 struct bgp_pbr_interface *bgp_pbr_interface_lookup(const char *name,
2235 struct bgp_pbr_interface_head *head)
2236 {
2237 struct bgp_pbr_interface pbr_if;
2238
2239 strlcpy(pbr_if.name, name, sizeof(pbr_if.name));
2240 return (RB_FIND(bgp_pbr_interface_head,
2241 head, &pbr_if));
2242 }
2243
2244 /* this function resets to the default policy routing
2245 * go back to default status
2246 */
2247 void bgp_pbr_reset(struct bgp *bgp, afi_t afi)
2248 {
2249 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
2250 struct bgp_pbr_interface_head *head;
2251 struct bgp_pbr_interface *pbr_if;
2252
2253 if (!bgp_pbr_cfg || afi != AFI_IP)
2254 return;
2255 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
2256
2257 while (!RB_EMPTY(bgp_pbr_interface_head, head)) {
2258 pbr_if = RB_ROOT(bgp_pbr_interface_head, head);
2259 RB_REMOVE(bgp_pbr_interface_head, head, pbr_if);
2260 XFREE(MTYPE_TMP, pbr_if);
2261 }
2262 }