]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_pbr.c
Merge pull request #8887 from LabNConsulting/chopps/fix-test-retries
[mirror_frr.git] / bgpd / bgp_pbr.c
CommitLineData
bbe6ffd6
PG
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"
f3d32faa 23#include "jhash.h"
2e1f721e 24#include "pbr.h"
bbe6ffd6 25
2dbe669b
DA
26#include "lib/printfrr.h"
27
f3d32faa 28#include "bgpd/bgpd.h"
bbe6ffd6 29#include "bgpd/bgp_pbr.h"
b46b6f1a 30#include "bgpd/bgp_debug.h"
45918cfb
PG
31#include "bgpd/bgp_flowspec_util.h"
32#include "bgpd/bgp_ecommunity.h"
33#include "bgpd/bgp_route.h"
34#include "bgpd/bgp_attr.h"
d114b0d7 35#include "bgpd/bgp_zebra.h"
529efa23 36#include "bgpd/bgp_mplsvpn.h"
35703998 37#include "bgpd/bgp_flowspec_private.h"
4f3be667 38#include "bgpd/bgp_errors.h"
d114b0d7 39
bf8d3d6a
DL
40DEFINE_MTYPE_STATIC(BGPD, PBR_MATCH_ENTRY, "PBR match entry");
41DEFINE_MTYPE_STATIC(BGPD, PBR_MATCH, "PBR match");
42DEFINE_MTYPE_STATIC(BGPD, PBR_ACTION, "PBR action");
43DEFINE_MTYPE_STATIC(BGPD, PBR_RULE, "PBR rule");
44DEFINE_MTYPE_STATIC(BGPD, PBR, "BGP PBR Context");
45DEFINE_MTYPE_STATIC(BGPD, PBR_VALMASK, "BGP PBR Val Mask Value");
4762c213 46
9a659715
PG
47/* chain strings too long to fit in one line */
48#define FSPEC_ACTION_EXCEED_LIMIT "flowspec actions exceeds limit"
173ebf47 49#define IPV6_FRAGMENT_INVALID "fragment not valid for IPv6 for this implementation"
9a659715 50
4762c213
PG
51RB_GENERATE(bgp_pbr_interface_head, bgp_pbr_interface,
52 id_entry, bgp_pbr_interface_compare);
53struct bgp_pbr_interface_head ifaces_by_name_ipv4 =
54 RB_INITIALIZER(&ifaces_by_name_ipv4);
d114b0d7
PG
55
56static int bgp_pbr_match_counter_unique;
57static int bgp_pbr_match_entry_counter_unique;
58static int bgp_pbr_action_counter_unique;
59static int bgp_pbr_match_iptable_counter_unique;
b46b6f1a 60
1815c6fc
PG
61struct bgp_pbr_match_iptable_unique {
62 uint32_t unique;
63 struct bgp_pbr_match *bpm_found;
64};
65
c5d429e1
PG
66struct bgp_pbr_match_entry_unique {
67 uint32_t unique;
68 struct bgp_pbr_match_entry *bpme_found;
69};
70
70eabd12
PG
71struct bgp_pbr_action_unique {
72 uint32_t unique;
73 struct bgp_pbr_action *bpa_found;
74};
75
ffee150e
PG
76struct bgp_pbr_rule_unique {
77 uint32_t unique;
78 struct bgp_pbr_rule *bpr_found;
79};
80
e3b78da8 81static int bgp_pbr_rule_walkcb(struct hash_bucket *bucket, void *arg)
ffee150e 82{
e3b78da8 83 struct bgp_pbr_rule *bpr = (struct bgp_pbr_rule *)bucket->data;
ffee150e
PG
84 struct bgp_pbr_rule_unique *bpru = (struct bgp_pbr_rule_unique *)
85 arg;
86 uint32_t unique = bpru->unique;
87
88 if (bpr->unique == unique) {
89 bpru->bpr_found = bpr;
90 return HASHWALK_ABORT;
91 }
92 return HASHWALK_CONTINUE;
93}
94
e3b78da8 95static int bgp_pbr_action_walkcb(struct hash_bucket *bucket, void *arg)
70eabd12 96{
e3b78da8 97 struct bgp_pbr_action *bpa = (struct bgp_pbr_action *)bucket->data;
70eabd12
PG
98 struct bgp_pbr_action_unique *bpau = (struct bgp_pbr_action_unique *)
99 arg;
100 uint32_t unique = bpau->unique;
101
102 if (bpa->unique == unique) {
103 bpau->bpa_found = bpa;
104 return HASHWALK_ABORT;
105 }
106 return HASHWALK_CONTINUE;
107}
108
e3b78da8 109static int bgp_pbr_match_entry_walkcb(struct hash_bucket *bucket, void *arg)
c5d429e1
PG
110{
111 struct bgp_pbr_match_entry *bpme =
e3b78da8 112 (struct bgp_pbr_match_entry *)bucket->data;
c5d429e1
PG
113 struct bgp_pbr_match_entry_unique *bpmeu =
114 (struct bgp_pbr_match_entry_unique *)arg;
115 uint32_t unique = bpmeu->unique;
116
117 if (bpme->unique == unique) {
118 bpmeu->bpme_found = bpme;
119 return HASHWALK_ABORT;
120 }
121 return HASHWALK_CONTINUE;
122}
123
124struct bgp_pbr_match_ipsetname {
125 char *ipsetname;
126 struct bgp_pbr_match *bpm_found;
127};
128
e3b78da8 129static int bgp_pbr_match_pername_walkcb(struct hash_bucket *bucket, void *arg)
c5d429e1 130{
e3b78da8 131 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)bucket->data;
c5d429e1
PG
132 struct bgp_pbr_match_ipsetname *bpmi =
133 (struct bgp_pbr_match_ipsetname *)arg;
134 char *ipset_name = bpmi->ipsetname;
135
136 if (!strncmp(ipset_name, bpm->ipset_name,
137 ZEBRA_IPSET_NAME_SIZE)) {
138 bpmi->bpm_found = bpm;
139 return HASHWALK_ABORT;
140 }
141 return HASHWALK_CONTINUE;
142}
143
e3b78da8 144static int bgp_pbr_match_iptable_walkcb(struct hash_bucket *bucket, void *arg)
1815c6fc 145{
e3b78da8 146 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)bucket->data;
1815c6fc
PG
147 struct bgp_pbr_match_iptable_unique *bpmiu =
148 (struct bgp_pbr_match_iptable_unique *)arg;
149 uint32_t unique = bpmiu->unique;
150
151 if (bpm->unique2 == unique) {
152 bpmiu->bpm_found = bpm;
153 return HASHWALK_ABORT;
154 }
155 return HASHWALK_CONTINUE;
156}
157
c5d429e1
PG
158struct bgp_pbr_match_unique {
159 uint32_t unique;
160 struct bgp_pbr_match *bpm_found;
161};
162
e3b78da8 163static int bgp_pbr_match_walkcb(struct hash_bucket *bucket, void *arg)
c5d429e1 164{
e3b78da8 165 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)bucket->data;
c5d429e1
PG
166 struct bgp_pbr_match_unique *bpmu = (struct bgp_pbr_match_unique *)
167 arg;
168 uint32_t unique = bpmu->unique;
169
170 if (bpm->unique == unique) {
171 bpmu->bpm_found = bpm;
172 return HASHWALK_ABORT;
173 }
174 return HASHWALK_CONTINUE;
175}
176
4371bf91
PG
177static int snprintf_bgp_pbr_match_val(char *str, int len,
178 struct bgp_pbr_match_val *mval,
9cec4121 179 const char *prepend)
b46b6f1a
PG
180{
181 char *ptr = str;
9cec4121
PG
182 int delta;
183
184 if (prepend) {
185 delta = snprintf(ptr, len, "%s", prepend);
186 ptr += delta;
187 len -= delta;
188 } else {
189 if (mval->unary_operator & OPERATOR_UNARY_OR) {
190 delta = snprintf(ptr, len, ", or ");
191 ptr += delta;
192 len -= delta;
193 }
194 if (mval->unary_operator & OPERATOR_UNARY_AND) {
195 delta = snprintf(ptr, len, ", and ");
196 ptr += delta;
197 len -= delta;
198 }
199 }
200 if (mval->compare_operator & OPERATOR_COMPARE_LESS_THAN) {
201 delta = snprintf(ptr, len, "<");
202 ptr += delta;
203 len -= delta;
204 }
205 if (mval->compare_operator & OPERATOR_COMPARE_GREATER_THAN) {
206 delta = snprintf(ptr, len, ">");
207 ptr += delta;
208 len -= delta;
209 }
210 if (mval->compare_operator & OPERATOR_COMPARE_EQUAL_TO) {
211 delta = snprintf(ptr, len, "=");
212 ptr += delta;
213 len -= delta;
b46b6f1a 214 }
9cec4121
PG
215 if (mval->compare_operator & OPERATOR_COMPARE_EXACT_MATCH) {
216 delta = snprintf(ptr, len, "match");
217 ptr += delta;
218 len -= delta;
219 }
220 ptr += snprintf(ptr, len, " %u", mval->value);
b46b6f1a
PG
221 return (int)(ptr - str);
222}
223
9cec4121
PG
224#define INCREMENT_DISPLAY(_ptr, _cnt, _len) do { \
225 int sn_delta; \
226 \
227 if (_cnt) { \
228 sn_delta = snprintf((_ptr), (_len), "; ");\
229 (_len) -= sn_delta; \
230 (_ptr) += sn_delta; \
231 } \
232 (_cnt)++; \
b46b6f1a
PG
233 } while (0)
234
0e867886
PG
235/* this structure can be used for port range,
236 * but also for other values range like packet length range
237 */
1de7dfff
PG
238struct bgp_pbr_range_port {
239 uint16_t min_port;
240 uint16_t max_port;
241};
242
2da7d62e
PG
243/* this structure can be used to filter with a mask
244 * for instance it supports not instructions like for
245 * tcpflags
246 */
247struct bgp_pbr_val_mask {
248 uint16_t val;
249 uint16_t mask;
250};
251
0e867886
PG
252/* this structure is used to pass instructs
253 * so that BGP can create pbr instructions to ZEBRA
254 */
255struct bgp_pbr_filter {
9350f1df 256 uint8_t type;
0e867886 257 vrf_id_t vrf_id;
f01e580f 258 uint8_t family;
0e867886
PG
259 struct prefix *src;
260 struct prefix *dst;
9350f1df 261 uint8_t bitmask_iprule;
0e867886
PG
262 uint8_t protocol;
263 struct bgp_pbr_range_port *pkt_len;
264 struct bgp_pbr_range_port *src_port;
265 struct bgp_pbr_range_port *dst_port;
2da7d62e 266 struct bgp_pbr_val_mask *tcp_flags;
4977bd6c 267 struct bgp_pbr_val_mask *dscp;
40881800 268 struct bgp_pbr_val_mask *flow_label;
cfaf18ce 269 struct bgp_pbr_val_mask *pkt_len_val;
6f5617d8 270 struct bgp_pbr_val_mask *fragment;
0e867886
PG
271};
272
c5ee26cc
PG
273/* this structure is used to contain OR instructions
274 * so that BGP can create multiple pbr instructions
275 * to ZEBRA
276 */
277struct bgp_pbr_or_filter {
278 struct list *tcpflags;
56707a36 279 struct list *dscp;
40881800 280 struct list *flowlabel;
cfaf18ce 281 struct list *pkt_len;
6f5617d8 282 struct list *fragment;
da3fa383
PG
283 struct list *icmp_type;
284 struct list *icmp_code;
c5ee26cc
PG
285};
286
da3fa383 287static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp *bgp,
9b6d8fcf 288 struct bgp_path_info *path,
da3fa383
PG
289 struct bgp_pbr_filter *bpf,
290 struct nexthop *nh,
291 float *rate);
6e288819 292
064a9d52
PG
293static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add);
294
6e288819
PG
295static bool bgp_pbr_extract_enumerate_unary_opposite(
296 uint8_t unary_operator,
297 struct bgp_pbr_val_mask *and_valmask,
298 struct list *or_valmask, uint32_t value,
299 uint8_t type_entry)
300{
301 if (unary_operator == OPERATOR_UNARY_AND && and_valmask) {
302 if (type_entry == FLOWSPEC_TCP_FLAGS) {
303 and_valmask->mask |=
304 TCP_HEADER_ALL_FLAGS &
305 ~(value);
306 } else if (type_entry == FLOWSPEC_DSCP ||
40881800 307 type_entry == FLOWSPEC_FLOW_LABEL ||
6e288819
PG
308 type_entry == FLOWSPEC_PKT_LEN ||
309 type_entry == FLOWSPEC_FRAGMENT) {
310 and_valmask->val = value;
311 and_valmask->mask = 1; /* inverse */
312 }
313 } else if (unary_operator == OPERATOR_UNARY_OR && or_valmask) {
314 and_valmask = XCALLOC(MTYPE_PBR_VALMASK,
315 sizeof(struct bgp_pbr_val_mask));
316 if (type_entry == FLOWSPEC_TCP_FLAGS) {
317 and_valmask->val = TCP_HEADER_ALL_FLAGS;
318 and_valmask->mask |=
319 TCP_HEADER_ALL_FLAGS &
320 ~(value);
321 } else if (type_entry == FLOWSPEC_DSCP ||
40881800 322 type_entry == FLOWSPEC_FLOW_LABEL ||
6e288819
PG
323 type_entry == FLOWSPEC_FRAGMENT ||
324 type_entry == FLOWSPEC_PKT_LEN) {
325 and_valmask->val = value;
326 and_valmask->mask = 1; /* inverse */
327 }
328 listnode_add(or_valmask, and_valmask);
329 } else if (type_entry == FLOWSPEC_ICMP_CODE ||
330 type_entry == FLOWSPEC_ICMP_TYPE)
331 return false;
332 return true;
333}
334
2da7d62e
PG
335/* TCP : FIN and SYN -> val = ALL; mask = 3
336 * TCP : not (FIN and SYN) -> val = ALL; mask = ALL & ~(FIN|RST)
40881800 337 * other variables type: dscp, pkt len, fragment, flow label
6f5617d8
PG
338 * - value is copied in bgp_pbr_val_mask->val value
339 * - if negate form is identifierd, bgp_pbr_val_mask->mask set to 1
2da7d62e 340 */
f7b2e630
PG
341static bool bgp_pbr_extract_enumerate_unary(struct bgp_pbr_match_val list[],
342 int num, uint8_t unary_operator,
35703998 343 void *valmask, uint8_t type_entry)
932404b7
PG
344{
345 int i = 0;
f7b2e630
PG
346 struct bgp_pbr_val_mask *and_valmask = NULL;
347 struct list *or_valmask = NULL;
6e288819 348 bool ret;
932404b7 349
f7b2e630
PG
350 if (valmask) {
351 if (unary_operator == OPERATOR_UNARY_AND) {
352 and_valmask = (struct bgp_pbr_val_mask *)valmask;
353 memset(and_valmask, 0, sizeof(struct bgp_pbr_val_mask));
354 } else if (unary_operator == OPERATOR_UNARY_OR) {
355 or_valmask = (struct list *)valmask;
356 }
357 }
932404b7 358 for (i = 0; i < num; i++) {
2da7d62e
PG
359 if (i != 0 && list[i].unary_operator !=
360 unary_operator)
932404b7 361 return false;
2da7d62e
PG
362 if (!(list[i].compare_operator &
363 OPERATOR_COMPARE_EQUAL_TO) &&
364 !(list[i].compare_operator &
365 OPERATOR_COMPARE_EXACT_MATCH)) {
366 if ((list[i].compare_operator &
367 OPERATOR_COMPARE_LESS_THAN) &&
368 (list[i].compare_operator &
369 OPERATOR_COMPARE_GREATER_THAN)) {
6e288819
PG
370 ret = bgp_pbr_extract_enumerate_unary_opposite(
371 unary_operator, and_valmask,
372 or_valmask, list[i].value,
373 type_entry);
d8729f8c 374 if (!ret)
6e288819 375 return ret;
2da7d62e
PG
376 continue;
377 }
378 return false;
379 }
35703998
PG
380 if (unary_operator == OPERATOR_UNARY_AND && and_valmask) {
381 if (type_entry == FLOWSPEC_TCP_FLAGS)
382 and_valmask->mask |=
383 TCP_HEADER_ALL_FLAGS & list[i].value;
384 } else if (unary_operator == OPERATOR_UNARY_OR && or_valmask) {
f7b2e630
PG
385 and_valmask = XCALLOC(MTYPE_PBR_VALMASK,
386 sizeof(struct bgp_pbr_val_mask));
35703998
PG
387 if (type_entry == FLOWSPEC_TCP_FLAGS) {
388 and_valmask->val = TCP_HEADER_ALL_FLAGS;
389 and_valmask->mask |=
390 TCP_HEADER_ALL_FLAGS & list[i].value;
cfaf18ce 391 } else if (type_entry == FLOWSPEC_DSCP ||
40881800 392 type_entry == FLOWSPEC_FLOW_LABEL ||
da3fa383
PG
393 type_entry == FLOWSPEC_ICMP_TYPE ||
394 type_entry == FLOWSPEC_ICMP_CODE ||
6f5617d8 395 type_entry == FLOWSPEC_FRAGMENT ||
cfaf18ce 396 type_entry == FLOWSPEC_PKT_LEN)
35703998 397 and_valmask->val = list[i].value;
f7b2e630
PG
398 listnode_add(or_valmask, and_valmask);
399 }
932404b7 400 }
35703998
PG
401 if (unary_operator == OPERATOR_UNARY_AND && and_valmask
402 && type_entry == FLOWSPEC_TCP_FLAGS)
403 and_valmask->val = TCP_HEADER_ALL_FLAGS;
932404b7
PG
404 return true;
405}
406
f7b2e630
PG
407/* if unary operator can either be UNARY_OR/AND/OR-AND.
408 * in the latter case, combinationf of both is not handled
409 */
410static bool bgp_pbr_extract_enumerate(struct bgp_pbr_match_val list[],
411 int num, uint8_t unary_operator,
35703998 412 void *valmask, uint8_t type_entry)
f7b2e630
PG
413{
414 bool ret;
3f54c705 415 uint8_t unary_operator_val;
f7b2e630
PG
416 bool double_check = false;
417
418 if ((unary_operator & OPERATOR_UNARY_OR) &&
419 (unary_operator & OPERATOR_UNARY_AND)) {
420 unary_operator_val = OPERATOR_UNARY_AND;
421 double_check = true;
422 } else
423 unary_operator_val = unary_operator;
424 ret = bgp_pbr_extract_enumerate_unary(list, num, unary_operator_val,
35703998 425 valmask, type_entry);
f7b2e630
PG
426 if (!ret && double_check)
427 ret = bgp_pbr_extract_enumerate_unary(list, num,
428 OPERATOR_UNARY_OR,
35703998
PG
429 valmask,
430 type_entry);
f7b2e630
PG
431 return ret;
432}
433
434/* returns the unary operator that is in the list
435 * return 0 if both operators are used
436 */
437static uint8_t bgp_pbr_match_val_get_operator(struct bgp_pbr_match_val list[],
438 int num)
439
440{
441 int i;
442 uint8_t unary_operator = OPERATOR_UNARY_AND;
443
444 for (i = 0; i < num; i++) {
445 if (i == 0)
446 continue;
447 if (list[i].unary_operator & OPERATOR_UNARY_OR)
448 unary_operator = OPERATOR_UNARY_OR;
449 if ((list[i].unary_operator & OPERATOR_UNARY_AND
450 && unary_operator == OPERATOR_UNARY_OR) ||
451 (list[i].unary_operator & OPERATOR_UNARY_OR
452 && unary_operator == OPERATOR_UNARY_AND))
453 return 0;
454 }
455 return unary_operator;
456}
457
458
1de7dfff
PG
459/* return true if extraction ok
460 */
461static bool bgp_pbr_extract(struct bgp_pbr_match_val list[],
462 int num,
463 struct bgp_pbr_range_port *range)
464{
465 int i = 0;
466 bool exact_match = false;
467
468 if (range)
469 memset(range, 0, sizeof(struct bgp_pbr_range_port));
470
471 if (num > 2)
472 return false;
473 for (i = 0; i < num; i++) {
474 if (i != 0 && (list[i].compare_operator ==
475 OPERATOR_COMPARE_EQUAL_TO))
476 return false;
477 if (i == 0 && (list[i].compare_operator ==
478 OPERATOR_COMPARE_EQUAL_TO)) {
479 if (range)
480 range->min_port = list[i].value;
481 exact_match = true;
482 }
d8729f8c 483 if (exact_match && i > 0)
1de7dfff
PG
484 return false;
485 if (list[i].compare_operator ==
486 (OPERATOR_COMPARE_GREATER_THAN +
487 OPERATOR_COMPARE_EQUAL_TO)) {
488 if (range)
489 range->min_port = list[i].value;
490 } else if (list[i].compare_operator ==
491 (OPERATOR_COMPARE_LESS_THAN +
492 OPERATOR_COMPARE_EQUAL_TO)) {
493 if (range)
494 range->max_port = list[i].value;
495 } else if (list[i].compare_operator ==
496 OPERATOR_COMPARE_LESS_THAN) {
497 if (range)
498 range->max_port = list[i].value - 1;
499 } else if (list[i].compare_operator ==
500 OPERATOR_COMPARE_GREATER_THAN) {
501 if (range)
502 range->min_port = list[i].value + 1;
503 }
504 }
505 return true;
506}
507
b46b6f1a
PG
508static int bgp_pbr_validate_policy_route(struct bgp_pbr_entry_main *api)
509{
932404b7
PG
510 bool enumerate_icmp = false;
511
5fa779c9
PG
512 if (api->type == BGP_PBR_UNDEFINED) {
513 if (BGP_DEBUG(pbr, PBR))
514 zlog_debug("BGP: pbr entry undefined. cancel.");
515 return 0;
516 }
b46b6f1a
PG
517 /* because bgp pbr entry may contain unsupported
518 * combinations, a message will be displayed here if
519 * not supported.
520 * for now, only match/set supported is
521 * - combination src/dst => redirect nexthop [ + rate]
522 * - combination src/dst => redirect VRF [ + rate]
523 * - combination src/dst => drop
1de7dfff 524 * - combination srcport + @IP
b46b6f1a 525 */
1de7dfff
PG
526 if (api->match_protocol_num > 1) {
527 if (BGP_DEBUG(pbr, PBR))
3efd0893 528 zlog_debug("BGP: match protocol operations:multiple protocols ( %d). ignoring.",
1de7dfff
PG
529 api->match_protocol_num);
530 return 0;
531 }
173ebf47
PG
532 if (api->src_prefix_offset > 0 ||
533 api->dst_prefix_offset > 0) {
534 if (BGP_DEBUG(pbr, PBR))
535 zlog_debug("BGP: match prefix offset:"
536 "implementation does not support it.");
537 return 0;
538 }
1de7dfff
PG
539 if (api->match_protocol_num == 1 &&
540 api->protocol[0].value != PROTOCOL_UDP &&
932404b7 541 api->protocol[0].value != PROTOCOL_ICMP &&
f2ead0a5 542 api->protocol[0].value != PROTOCOL_ICMPV6 &&
1de7dfff
PG
543 api->protocol[0].value != PROTOCOL_TCP) {
544 if (BGP_DEBUG(pbr, PBR))
3efd0893 545 zlog_debug("BGP: match protocol operations:protocol (%d) not supported. ignoring",
1de7dfff
PG
546 api->match_protocol_num);
547 return 0;
548 }
549 if (!bgp_pbr_extract(api->src_port, api->match_src_port_num, NULL)) {
550 if (BGP_DEBUG(pbr, PBR))
3efd0893 551 zlog_debug("BGP: match src port operations:too complex. ignoring.");
1de7dfff
PG
552 return 0;
553 }
554 if (!bgp_pbr_extract(api->dst_port, api->match_dst_port_num, NULL)) {
555 if (BGP_DEBUG(pbr, PBR))
3efd0893 556 zlog_debug("BGP: match dst port operations:too complex. ignoring.");
1de7dfff
PG
557 return 0;
558 }
2da7d62e
PG
559 if (!bgp_pbr_extract_enumerate(api->tcpflags,
560 api->match_tcpflags_num,
c5ee26cc 561 OPERATOR_UNARY_AND |
35703998
PG
562 OPERATOR_UNARY_OR, NULL,
563 FLOWSPEC_TCP_FLAGS)) {
2da7d62e 564 if (BGP_DEBUG(pbr, PBR))
3efd0893 565 zlog_debug("BGP: match tcp flags:too complex. ignoring.");
2da7d62e
PG
566 return 0;
567 }
932404b7
PG
568 if (!bgp_pbr_extract(api->icmp_type, api->match_icmp_type_num, NULL)) {
569 if (!bgp_pbr_extract_enumerate(api->icmp_type,
2da7d62e 570 api->match_icmp_type_num,
35703998
PG
571 OPERATOR_UNARY_OR, NULL,
572 FLOWSPEC_ICMP_TYPE)) {
932404b7 573 if (BGP_DEBUG(pbr, PBR))
3efd0893 574 zlog_debug("BGP: match icmp type operations:too complex. ignoring.");
932404b7
PG
575 return 0;
576 }
577 enumerate_icmp = true;
578 }
579 if (!bgp_pbr_extract(api->icmp_code, api->match_icmp_code_num, NULL)) {
580 if (!bgp_pbr_extract_enumerate(api->icmp_code,
2da7d62e 581 api->match_icmp_code_num,
35703998
PG
582 OPERATOR_UNARY_OR, NULL,
583 FLOWSPEC_ICMP_CODE)) {
932404b7 584 if (BGP_DEBUG(pbr, PBR))
3efd0893 585 zlog_debug("BGP: match icmp code operations:too complex. ignoring.");
932404b7
PG
586 return 0;
587 } else if (api->match_icmp_type_num > 1 &&
d8729f8c 588 !enumerate_icmp) {
932404b7 589 if (BGP_DEBUG(pbr, PBR))
3efd0893 590 zlog_debug("BGP: match icmp code is enumerate, and icmp type is not. too complex. ignoring.");
932404b7
PG
591 return 0;
592 }
593 }
1de7dfff
PG
594 if (!bgp_pbr_extract(api->port, api->match_port_num, NULL)) {
595 if (BGP_DEBUG(pbr, PBR))
3efd0893 596 zlog_debug("BGP: match port operations:too complex. ignoring.");
1de7dfff
PG
597 return 0;
598 }
cfaf18ce
PG
599 if (api->match_packet_length_num) {
600 bool ret;
601
602 ret = bgp_pbr_extract(api->packet_length,
603 api->match_packet_length_num, NULL);
604 if (!ret)
605 ret = bgp_pbr_extract_enumerate(api->packet_length,
606 api->match_packet_length_num,
607 OPERATOR_UNARY_OR
608 | OPERATOR_UNARY_AND,
609 NULL, FLOWSPEC_PKT_LEN);
610 if (!ret) {
611 if (BGP_DEBUG(pbr, PBR))
3efd0893 612 zlog_debug("BGP: match packet length operations:too complex. ignoring.");
cfaf18ce
PG
613 return 0;
614 }
83360720 615 }
35703998
PG
616 if (api->match_dscp_num) {
617 if (!bgp_pbr_extract_enumerate(api->dscp, api->match_dscp_num,
618 OPERATOR_UNARY_OR | OPERATOR_UNARY_AND,
619 NULL, FLOWSPEC_DSCP)) {
620 if (BGP_DEBUG(pbr, PBR))
3efd0893 621 zlog_debug("BGP: match DSCP operations:too complex. ignoring.");
35703998
PG
622 return 0;
623 }
4977bd6c 624 }
40881800
PG
625 if (api->match_flowlabel_num) {
626 if (api->afi == AFI_IP) {
627 if (BGP_DEBUG(pbr, PBR))
628 zlog_debug("BGP: match Flow Label operations:"
629 "Not for IPv4.");
630 return 0;
631 }
632 if (!bgp_pbr_extract_enumerate(api->flow_label,
633 api->match_flowlabel_num,
634 OPERATOR_UNARY_OR | OPERATOR_UNARY_AND,
635 NULL, FLOWSPEC_FLOW_LABEL)) {
636 if (BGP_DEBUG(pbr, PBR))
637 zlog_debug("BGP: match FlowLabel operations:"
638 "too complex. ignoring.");
639 return 0;
640 }
173ebf47
PG
641 if (BGP_DEBUG(pbr, PBR))
642 zlog_debug("BGP: match FlowLabel operations "
643 "not supported. ignoring.");
644 return 0;
40881800 645 }
6f5617d8
PG
646 if (api->match_fragment_num) {
647 char fail_str[64];
648 bool success;
649
650 success = bgp_pbr_extract_enumerate(api->fragment,
651 api->match_fragment_num,
652 OPERATOR_UNARY_OR
653 | OPERATOR_UNARY_AND,
654 NULL, FLOWSPEC_FRAGMENT);
655 if (success) {
656 int i;
657
658 for (i = 0; i < api->match_fragment_num; i++) {
659 if (api->fragment[i].value != 1 &&
660 api->fragment[i].value != 2 &&
661 api->fragment[i].value != 4 &&
662 api->fragment[i].value != 8) {
663 success = false;
772270f3
QY
664 snprintf(
665 fail_str, sizeof(fail_str),
6f5617d8
PG
666 "Value not valid (%d) for this implementation",
667 api->fragment[i].value);
668 }
40881800
PG
669 if (api->afi == AFI_IP6 &&
670 api->fragment[i].value == 1) {
671 success = false;
672 snprintf(fail_str, sizeof(fail_str),
673 "IPv6 dont fragment match invalid (%d)",
674 api->fragment[i].value);
675 }
6f5617d8 676 }
173ebf47
PG
677 if (api->afi == AFI_IP6) {
678 success = false;
679 snprintf(fail_str, sizeof(fail_str),
680 "%s", IPV6_FRAGMENT_INVALID);
681 }
6f5617d8 682 } else
772270f3
QY
683 snprintf(fail_str, sizeof(fail_str),
684 "too complex. ignoring");
6f5617d8
PG
685 if (!success) {
686 if (BGP_DEBUG(pbr, PBR))
687 zlog_debug("BGP: match fragment operation (%d) %s",
688 api->match_fragment_num,
689 fail_str);
690 return 0;
691 }
692 }
693
1de7dfff
PG
694 /* no combinations with both src_port and dst_port
695 * or port with src_port and dst_port
696 */
697 if (api->match_src_port_num + api->match_dst_port_num +
698 api->match_port_num > 3) {
699 if (BGP_DEBUG(pbr, PBR))
3efd0893 700 zlog_debug("BGP: match multiple port operations: too complex. ignoring.");
1de7dfff
PG
701 return 0;
702 }
932404b7
PG
703 if ((api->match_src_port_num || api->match_dst_port_num
704 || api->match_port_num) && (api->match_icmp_type_num
705 || api->match_icmp_code_num)) {
706 if (BGP_DEBUG(pbr, PBR))
3efd0893 707 zlog_debug("BGP: match multiple port/imcp operations: too complex. ignoring.");
932404b7
PG
708 return 0;
709 }
a35a794a
PG
710 /* iprule only supports redirect IP */
711 if (api->type == BGP_PBR_IPRULE) {
712 int i;
713
714 for (i = 0; i < api->action_num; i++) {
715 if (api->actions[i].action == ACTION_TRAFFICRATE &&
716 api->actions[i].u.r.rate == 0) {
717 if (BGP_DEBUG(pbr, PBR)) {
718 bgp_pbr_print_policy_route(api);
3efd0893 719 zlog_debug("BGP: iprule match actions drop not supported");
a35a794a
PG
720 }
721 return 0;
722 }
723 if (api->actions[i].action == ACTION_MARKING) {
724 if (BGP_DEBUG(pbr, PBR)) {
725 bgp_pbr_print_policy_route(api);
40881800 726 zlog_warn("PBR: iprule set DSCP/Flow Label %u not supported",
a35a794a
PG
727 api->actions[i].u.marking_dscp);
728 }
729 }
730 if (api->actions[i].action == ACTION_REDIRECT) {
731 if (BGP_DEBUG(pbr, PBR)) {
732 bgp_pbr_print_policy_route(api);
3efd0893 733 zlog_warn("PBR: iprule redirect VRF %u not supported",
a35a794a
PG
734 api->actions[i].u.redirect_vrf);
735 }
736 }
737 }
738
739 } else if (!(api->match_bitmask & PREFIX_SRC_PRESENT) &&
740 !(api->match_bitmask & PREFIX_DST_PRESENT)) {
ac7c35f8 741 if (BGP_DEBUG(pbr, PBR)) {
b46b6f1a 742 bgp_pbr_print_policy_route(api);
3efd0893 743 zlog_debug("BGP: match actions without src or dst address can not operate. ignoring.");
ac7c35f8 744 }
b46b6f1a
PG
745 return 0;
746 }
747 return 1;
748}
bbe6ffd6 749
45918cfb 750/* return -1 if build or validation failed */
1840384b 751
5a1ae2c2
DS
752int bgp_pbr_build_and_validate_entry(const struct prefix *p,
753 struct bgp_path_info *path,
754 struct bgp_pbr_entry_main *api)
45918cfb
PG
755{
756 int ret;
f6e07e1b 757 uint32_t i, action_count = 0;
45918cfb
PG
758 struct ecommunity *ecom;
759 struct ecommunity_val *ecom_eval;
760 struct bgp_pbr_entry_action *api_action;
761 struct prefix *src = NULL, *dst = NULL;
762 int valid_prefix = 0;
2551b26e 763 struct bgp_pbr_entry_action *api_action_redirect_ip = NULL;
46b89000 764 bool discard_action_found = false;
1840384b 765 afi_t afi = family2afi(p->u.prefix_flowspec.family);
45918cfb
PG
766
767 /* extract match from flowspec entries */
768 ret = bgp_flowspec_match_rules_fill((uint8_t *)p->u.prefix_flowspec.ptr,
1840384b 769 p->u.prefix_flowspec.prefixlen, api, afi);
45918cfb
PG
770 if (ret < 0)
771 return -1;
772 /* extract actiosn from flowspec ecom list */
05864da7 773 if (path && path->attr->ecommunity) {
40381db7 774 ecom = path->attr->ecommunity;
45918cfb
PG
775 for (i = 0; i < ecom->size; i++) {
776 ecom_eval = (struct ecommunity_val *)
149d272b
PG
777 (ecom->val + (i * ECOMMUNITY_SIZE));
778 action_count++;
45918cfb 779 if (action_count > ACTIONS_MAX_NUM) {
f146bb54 780 if (BGP_DEBUG(pbr, PBR_ERROR))
1c50c1c0
QY
781 flog_err(
782 EC_BGP_FLOWSPEC_PACKET,
9a659715
PG
783 "%s: %s (max %u)",
784 __func__,
785 FSPEC_ACTION_EXCEED_LIMIT,
786 action_count);
45918cfb
PG
787 break;
788 }
149d272b 789 api_action = &api->actions[action_count - 1];
45918cfb
PG
790
791 if ((ecom_eval->val[1] ==
792 (char)ECOMMUNITY_REDIRECT_VRF) &&
793 (ecom_eval->val[0] ==
794 (char)ECOMMUNITY_ENCODE_TRANS_EXP ||
795 ecom_eval->val[0] ==
796 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 ||
797 ecom_eval->val[0] ==
798 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_3)) {
799 struct ecommunity *eckey = ecommunity_new();
800 struct ecommunity_val ecom_copy;
801
802 memcpy(&ecom_copy, ecom_eval,
803 sizeof(struct ecommunity_val));
804 ecom_copy.val[0] &=
805 ~ECOMMUNITY_ENCODE_TRANS_EXP;
806 ecom_copy.val[1] = ECOMMUNITY_ROUTE_TARGET;
1207a5bc 807 ecommunity_add_val(eckey, &ecom_copy,
808 false, false);
45918cfb
PG
809
810 api_action->action = ACTION_REDIRECT;
811 api_action->u.redirect_vrf =
812 get_first_vrf_for_redirect_with_rt(
813 eckey);
814 ecommunity_free(&eckey);
815 } else if ((ecom_eval->val[0] ==
816 (char)ECOMMUNITY_ENCODE_REDIRECT_IP_NH) &&
817 (ecom_eval->val[1] ==
818 (char)ECOMMUNITY_REDIRECT_IP_NH)) {
2551b26e
PG
819 /* in case the 2 ecom present,
820 * do not overwrite
821 * draft-ietf-idr-flowspec-redirect
822 */
f01e580f
PG
823 if (api_action_redirect_ip &&
824 p->u.prefix_flowspec.family == AF_INET) {
825 if (api_action_redirect_ip->u
826 .zr.redirect_ip_v4.s_addr
975a328e 827 != INADDR_ANY)
2551b26e 828 continue;
975a328e
DA
829 if (path->attr->nexthop.s_addr
830 == INADDR_ANY)
2551b26e 831 continue;
975a328e
DA
832 api_action_redirect_ip->u.zr
833 .redirect_ip_v4.s_addr =
2551b26e
PG
834 path->attr->nexthop.s_addr;
835 api_action_redirect_ip->u.zr.duplicate
836 = ecom_eval->val[7];
837 continue;
f01e580f
PG
838 } else if (api_action_redirect_ip &&
839 p->u.prefix_flowspec.family == AF_INET6) {
840 if (memcmp(&api_action_redirect_ip->u
841 .zr.redirect_ip_v6,
842 &in6addr_any,
843 sizeof(struct in6_addr)))
844 continue;
845 if (path->attr->mp_nexthop_len == 0 ||
846 path->attr->mp_nexthop_len ==
847 BGP_ATTR_NHLEN_IPV4 ||
848 path->attr->mp_nexthop_len ==
849 BGP_ATTR_NHLEN_VPNV4)
850 continue;
851 memcpy(&api_action_redirect_ip->u
852 .zr.redirect_ip_v6,
853 &path->attr->mp_nexthop_global,
854 sizeof(struct in6_addr));
855 api_action_redirect_ip->u.zr.duplicate
856 = ecom_eval->val[7];
857 continue;
4371bf91
PG
858 } else if (p->u.prefix_flowspec.family ==
859 AF_INET) {
2551b26e
PG
860 api_action->action = ACTION_REDIRECT_IP;
861 api_action->u.zr.redirect_ip_v4.s_addr =
862 path->attr->nexthop.s_addr;
863 api_action->u.zr.duplicate =
864 ecom_eval->val[7];
865 api_action_redirect_ip = api_action;
4371bf91
PG
866 } else if (p->u.prefix_flowspec.family ==
867 AF_INET6) {
f01e580f
PG
868 api_action->action = ACTION_REDIRECT_IP;
869 memcpy(&api_action->u
870 .zr.redirect_ip_v6,
871 &path->attr->mp_nexthop_global,
872 sizeof(struct in6_addr));
873 api_action->u.zr.duplicate
874 = ecom_eval->val[7];
875 api_action_redirect_ip = api_action;
2551b26e
PG
876 }
877 } else if ((ecom_eval->val[0] ==
878 (char)ECOMMUNITY_ENCODE_IP) &&
879 (ecom_eval->val[1] ==
880 (char)ECOMMUNITY_FLOWSPEC_REDIRECT_IPV4)) {
881 /* in case the 2 ecom present,
882 * overwrite simpson draft
883 * update redirect ip fields
884 */
885 if (api_action_redirect_ip) {
886 memcpy(&(api_action_redirect_ip->u
887 .zr.redirect_ip_v4.s_addr),
888 (ecom_eval->val+2), 4);
889 api_action_redirect_ip->u
890 .zr.duplicate =
891 ecom_eval->val[7];
892 continue;
893 } else {
894 api_action->action = ACTION_REDIRECT_IP;
895 memcpy(&(api_action->u
896 .zr.redirect_ip_v4.s_addr),
897 (ecom_eval->val+2), 4);
898 api_action->u.zr.duplicate =
899 ecom_eval->val[7];
900 api_action_redirect_ip = api_action;
901 }
45918cfb
PG
902 } else {
903 if (ecom_eval->val[0] !=
904 (char)ECOMMUNITY_ENCODE_TRANS_EXP)
905 continue;
906 ret = ecommunity_fill_pbr_action(ecom_eval,
f01e580f
PG
907 api_action,
908 afi);
45918cfb
PG
909 if (ret != 0)
910 continue;
4371bf91
PG
911 if ((api_action->action == ACTION_TRAFFICRATE)
912 && api->actions[i].u.r.rate == 0)
46b89000 913 discard_action_found = true;
45918cfb
PG
914 }
915 api->action_num++;
916 }
917 }
d04ac434 918 if (path && path->attr && bgp_attr_get_ipv6_ecommunity(path->attr)) {
9a659715
PG
919 struct ecommunity_val_ipv6 *ipv6_ecom_eval;
920
d04ac434 921 ecom = bgp_attr_get_ipv6_ecommunity(path->attr);
9a659715
PG
922 for (i = 0; i < ecom->size; i++) {
923 ipv6_ecom_eval = (struct ecommunity_val_ipv6 *)
924 (ecom->val + (i * ecom->unit_size));
925 action_count++;
926 if (action_count > ACTIONS_MAX_NUM) {
927 if (BGP_DEBUG(pbr, PBR_ERROR))
928 flog_err(
929 EC_BGP_FLOWSPEC_PACKET,
930 "%s: flowspec actions exceeds limit (max %u)",
931 __func__, action_count);
932 break;
933 }
934 api_action = &api->actions[action_count - 1];
935 if ((ipv6_ecom_eval->val[1] ==
936 (char)ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6) &&
937 (ipv6_ecom_eval->val[0] ==
938 (char)ECOMMUNITY_ENCODE_TRANS_EXP)) {
939 struct ecommunity *eckey = ecommunity_new();
940 struct ecommunity_val_ipv6 ecom_copy;
941
942 eckey->unit_size = IPV6_ECOMMUNITY_SIZE;
943 memcpy(&ecom_copy, ipv6_ecom_eval,
944 sizeof(struct ecommunity_val_ipv6));
945 ecom_copy.val[1] = ECOMMUNITY_ROUTE_TARGET;
946 ecommunity_add_val_ipv6(eckey, &ecom_copy,
947 false, false);
948 api_action->action = ACTION_REDIRECT;
949 api_action->u.redirect_vrf =
950 get_first_vrf_for_redirect_with_rt(
951 eckey);
952 ecommunity_free(&eckey);
953 api->action_num++;
954 }
955 }
956 }
46b89000
PG
957 /* if ECOMMUNITY_TRAFFIC_RATE = 0 as action
958 * then reduce the API action list to that action
959 */
960 if (api->action_num > 1 && discard_action_found) {
961 api->action_num = 1;
962 memset(&api->actions[0], 0,
963 sizeof(struct bgp_pbr_entry_action));
964 api->actions[0].action = ACTION_TRAFFICRATE;
965 }
45918cfb
PG
966
967 /* validate if incoming matc/action is compatible
968 * with our policy routing engine
969 */
970 if (!bgp_pbr_validate_policy_route(api))
971 return -1;
972
973 /* check inconsistency in the match rule */
974 if (api->match_bitmask & PREFIX_SRC_PRESENT) {
975 src = &api->src_prefix;
976 afi = family2afi(src->family);
977 valid_prefix = 1;
978 }
979 if (api->match_bitmask & PREFIX_DST_PRESENT) {
980 dst = &api->dst_prefix;
981 if (valid_prefix && afi != family2afi(dst->family)) {
ac7c35f8 982 if (BGP_DEBUG(pbr, PBR)) {
45918cfb 983 bgp_pbr_print_policy_route(api);
3efd0893 984 zlog_debug("%s: inconsistency: no match for afi src and dst (%u/%u)",
ac7c35f8
PG
985 __func__, afi, family2afi(dst->family));
986 }
45918cfb
PG
987 return -1;
988 }
989 }
990 return 0;
991}
992
a6b07429
PG
993static void bgp_pbr_match_entry_free(void *arg)
994{
995 struct bgp_pbr_match_entry *bpme;
996
997 bpme = (struct bgp_pbr_match_entry *)arg;
998
999 if (bpme->installed) {
1000 bgp_send_pbr_ipset_entry_match(bpme, false);
1001 bpme->installed = false;
1002 bpme->backpointer = NULL;
1003 }
1004 XFREE(MTYPE_PBR_MATCH_ENTRY, bpme);
1005}
1006
1007static void bgp_pbr_match_free(void *arg)
1008{
1009 struct bgp_pbr_match *bpm;
1010
1011 bpm = (struct bgp_pbr_match *)arg;
1012
1013 hash_clean(bpm->entry_hash, bgp_pbr_match_entry_free);
1014
1015 if (hashcount(bpm->entry_hash) == 0) {
1016 /* delete iptable entry first */
1017 /* then delete ipset match */
1018 if (bpm->installed) {
1019 if (bpm->installed_in_iptable) {
1020 bgp_send_pbr_iptable(bpm->action,
1021 bpm, false);
1022 bpm->installed_in_iptable = false;
1023 bpm->action->refcnt--;
1024 }
1025 bgp_send_pbr_ipset_match(bpm, false);
1026 bpm->installed = false;
1027 bpm->action = NULL;
1028 }
1029 }
1030 hash_free(bpm->entry_hash);
1031
1032 XFREE(MTYPE_PBR_MATCH, bpm);
1033}
1034
d114b0d7
PG
1035static void *bgp_pbr_match_alloc_intern(void *arg)
1036{
1037 struct bgp_pbr_match *bpm, *new;
1038
1039 bpm = (struct bgp_pbr_match *)arg;
1040
1041 new = XCALLOC(MTYPE_PBR_MATCH, sizeof(*new));
1042 memcpy(new, bpm, sizeof(*bpm));
1043
1044 return new;
1045}
1046
27e376d4
PG
1047static void bgp_pbr_rule_free(void *arg)
1048{
1049 struct bgp_pbr_rule *bpr;
1050
1051 bpr = (struct bgp_pbr_rule *)arg;
1052
1053 /* delete iprule */
1054 if (bpr->installed) {
1055 bgp_send_pbr_rule_action(bpr->action, bpr, false);
1056 bpr->installed = false;
1057 bpr->action->refcnt--;
1058 bpr->action = NULL;
1059 }
1060 XFREE(MTYPE_PBR_RULE, bpr);
1061}
1062
1063static void *bgp_pbr_rule_alloc_intern(void *arg)
1064{
1065 struct bgp_pbr_rule *bpr, *new;
1066
1067 bpr = (struct bgp_pbr_rule *)arg;
1068
1069 new = XCALLOC(MTYPE_PBR_RULE, sizeof(*new));
1070 memcpy(new, bpr, sizeof(*bpr));
1071
1072 return new;
1073}
1074
ebd1a47c
DS
1075static void bgp_pbr_bpa_remove(struct bgp_pbr_action *bpa)
1076{
1077 if ((bpa->refcnt == 0) && bpa->installed && bpa->table_id != 0) {
1078 bgp_send_pbr_rule_action(bpa, NULL, false);
1079 bgp_zebra_announce_default(bpa->bgp, &bpa->nh, bpa->afi,
1080 bpa->table_id, false);
1081 bpa->installed = false;
1082 }
1083}
1084
db1fcc98
DS
1085static void bgp_pbr_bpa_add(struct bgp_pbr_action *bpa)
1086{
1087 if (!bpa->installed && !bpa->install_in_progress) {
1088 bgp_send_pbr_rule_action(bpa, NULL, true);
1089 bgp_zebra_announce_default(bpa->bgp, &bpa->nh, bpa->afi,
1090 bpa->table_id, true);
1091 }
1092}
1093
a6b07429
PG
1094static void bgp_pbr_action_free(void *arg)
1095{
ebd1a47c 1096 struct bgp_pbr_action *bpa = arg;
a6b07429 1097
ebd1a47c 1098 bgp_pbr_bpa_remove(bpa);
a6b07429 1099
a6b07429
PG
1100 XFREE(MTYPE_PBR_ACTION, bpa);
1101}
1102
d114b0d7
PG
1103static void *bgp_pbr_action_alloc_intern(void *arg)
1104{
1105 struct bgp_pbr_action *bpa, *new;
1106
1107 bpa = (struct bgp_pbr_action *)arg;
1108
1109 new = XCALLOC(MTYPE_PBR_ACTION, sizeof(*new));
1110
1111 memcpy(new, bpa, sizeof(*bpa));
1112
1113 return new;
1114}
1115
1116static void *bgp_pbr_match_entry_alloc_intern(void *arg)
1117{
1118 struct bgp_pbr_match_entry *bpme, *new;
1119
1120 bpme = (struct bgp_pbr_match_entry *)arg;
1121
1122 new = XCALLOC(MTYPE_PBR_MATCH_ENTRY, sizeof(*new));
1123
1124 memcpy(new, bpme, sizeof(*bpme));
1125
1126 return new;
1127}
1128
d8b87afe 1129uint32_t bgp_pbr_match_hash_key(const void *arg)
f3d32faa 1130{
d8b87afe 1131 const struct bgp_pbr_match *pbm = arg;
f3d32faa
PG
1132 uint32_t key;
1133
1134 key = jhash_1word(pbm->vrf_id, 0x4312abde);
1135 key = jhash_1word(pbm->flags, key);
a60b7031 1136 key = jhash_1word(pbm->family, key);
9717d3e5
PG
1137 key = jhash(&pbm->pkt_len_min, 2, key);
1138 key = jhash(&pbm->pkt_len_max, 2, key);
1139 key = jhash(&pbm->tcp_flags, 2, key);
1140 key = jhash(&pbm->tcp_mask_flags, 2, key);
1141 key = jhash(&pbm->dscp_value, 1, key);
40881800 1142 key = jhash(&pbm->flow_label, 2, key);
9717d3e5 1143 key = jhash(&pbm->fragment, 1, key);
f449d223 1144 key = jhash(&pbm->protocol, 1, key);
f3d32faa
PG
1145 return jhash_1word(pbm->type, key);
1146}
1147
74df8d6d 1148bool bgp_pbr_match_hash_equal(const void *arg1, const void *arg2)
f3d32faa
PG
1149{
1150 const struct bgp_pbr_match *r1, *r2;
1151
1152 r1 = (const struct bgp_pbr_match *)arg1;
1153 r2 = (const struct bgp_pbr_match *)arg2;
1154
1155 if (r1->vrf_id != r2->vrf_id)
74df8d6d 1156 return false;
f3d32faa 1157
a60b7031
PG
1158 if (r1->family != r2->family)
1159 return false;
1160
f3d32faa 1161 if (r1->type != r2->type)
74df8d6d 1162 return false;
f3d32faa
PG
1163
1164 if (r1->flags != r2->flags)
74df8d6d 1165 return false;
f3d32faa
PG
1166
1167 if (r1->action != r2->action)
74df8d6d 1168 return false;
f3d32faa 1169
83360720 1170 if (r1->pkt_len_min != r2->pkt_len_min)
74df8d6d 1171 return false;
83360720
PG
1172
1173 if (r1->pkt_len_max != r2->pkt_len_max)
74df8d6d 1174 return false;
83360720 1175
2da7d62e 1176 if (r1->tcp_flags != r2->tcp_flags)
74df8d6d 1177 return false;
2da7d62e
PG
1178
1179 if (r1->tcp_mask_flags != r2->tcp_mask_flags)
74df8d6d 1180 return false;
2da7d62e 1181
4977bd6c 1182 if (r1->dscp_value != r2->dscp_value)
74df8d6d 1183 return false;
6f5617d8 1184
40881800
PG
1185 if (r1->flow_label != r2->flow_label)
1186 return false;
1187
6f5617d8 1188 if (r1->fragment != r2->fragment)
74df8d6d 1189 return false;
f449d223
PG
1190
1191 if (r1->protocol != r2->protocol)
1192 return false;
74df8d6d 1193 return true;
f3d32faa
PG
1194}
1195
d8b87afe 1196uint32_t bgp_pbr_rule_hash_key(const void *arg)
27e376d4 1197{
d8b87afe 1198 const struct bgp_pbr_rule *pbr = arg;
27e376d4
PG
1199 uint32_t key;
1200
1201 key = prefix_hash_key(&pbr->src);
1202 key = jhash_1word(pbr->vrf_id, key);
1203 key = jhash_1word(pbr->flags, key);
1204 return jhash_1word(prefix_hash_key(&pbr->dst), key);
1205}
1206
1207bool bgp_pbr_rule_hash_equal(const void *arg1, const void *arg2)
1208{
1209 const struct bgp_pbr_rule *r1, *r2;
1210
1211 r1 = (const struct bgp_pbr_rule *)arg1;
1212 r2 = (const struct bgp_pbr_rule *)arg2;
1213
1214 if (r1->vrf_id != r2->vrf_id)
1215 return false;
1216
1217 if (r1->flags != r2->flags)
1218 return false;
1219
1220 if (r1->action != r2->action)
1221 return false;
1222
1223 if ((r1->flags & MATCH_IP_SRC_SET) &&
1224 !prefix_same(&r1->src, &r2->src))
1225 return false;
1226
1227 if ((r1->flags & MATCH_IP_DST_SET) &&
1228 !prefix_same(&r1->dst, &r2->dst))
1229 return false;
1230
1231 return true;
1232}
1233
d8b87afe 1234uint32_t bgp_pbr_match_entry_hash_key(const void *arg)
f3d32faa 1235{
d8b87afe 1236 const struct bgp_pbr_match_entry *pbme;
f3d32faa
PG
1237 uint32_t key;
1238
d8b87afe 1239 pbme = arg;
f3d32faa
PG
1240 key = prefix_hash_key(&pbme->src);
1241 key = jhash_1word(prefix_hash_key(&pbme->dst), key);
1de7dfff
PG
1242 key = jhash(&pbme->dst_port_min, 2, key);
1243 key = jhash(&pbme->src_port_min, 2, key);
1244 key = jhash(&pbme->dst_port_max, 2, key);
1245 key = jhash(&pbme->src_port_max, 2, key);
1246 key = jhash(&pbme->proto, 1, key);
f3d32faa
PG
1247
1248 return key;
1249}
1250
74df8d6d 1251bool bgp_pbr_match_entry_hash_equal(const void *arg1, const void *arg2)
f3d32faa
PG
1252{
1253 const struct bgp_pbr_match_entry *r1, *r2;
1254
1255 r1 = (const struct bgp_pbr_match_entry *)arg1;
1256 r2 = (const struct bgp_pbr_match_entry *)arg2;
1257
74df8d6d
DS
1258 /*
1259 * on updates, comparing backpointer is not necessary
1260 * unique value is self calculated
1261 * rate is ignored for now
f3d32faa
PG
1262 */
1263
1264 if (!prefix_same(&r1->src, &r2->src))
74df8d6d 1265 return false;
f3d32faa
PG
1266
1267 if (!prefix_same(&r1->dst, &r2->dst))
74df8d6d 1268 return false;
f3d32faa 1269
1de7dfff 1270 if (r1->src_port_min != r2->src_port_min)
74df8d6d 1271 return false;
1de7dfff
PG
1272
1273 if (r1->dst_port_min != r2->dst_port_min)
74df8d6d 1274 return false;
1de7dfff
PG
1275
1276 if (r1->src_port_max != r2->src_port_max)
74df8d6d 1277 return false;
1de7dfff
PG
1278
1279 if (r1->dst_port_max != r2->dst_port_max)
74df8d6d 1280 return false;
1de7dfff
PG
1281
1282 if (r1->proto != r2->proto)
74df8d6d 1283 return false;
1de7dfff 1284
74df8d6d 1285 return true;
f3d32faa
PG
1286}
1287
d8b87afe 1288uint32_t bgp_pbr_action_hash_key(const void *arg)
f3d32faa 1289{
d8b87afe 1290 const struct bgp_pbr_action *pbra;
f3d32faa
PG
1291 uint32_t key;
1292
d8b87afe 1293 pbra = arg;
f3d32faa
PG
1294 key = jhash_1word(pbra->table_id, 0x4312abde);
1295 key = jhash_1word(pbra->fwmark, key);
f01e580f 1296 key = jhash_1word(pbra->afi, key);
f3d32faa
PG
1297 return key;
1298}
1299
74df8d6d 1300bool bgp_pbr_action_hash_equal(const void *arg1, const void *arg2)
f3d32faa
PG
1301{
1302 const struct bgp_pbr_action *r1, *r2;
1303
1304 r1 = (const struct bgp_pbr_action *)arg1;
1305 r2 = (const struct bgp_pbr_action *)arg2;
1306
1307 /* unique value is self calculated
1308 * table and fwmark is self calculated
e414819e 1309 * rate is ignored
f3d32faa 1310 */
f3d32faa 1311 if (r1->vrf_id != r2->vrf_id)
74df8d6d 1312 return false;
f3d32faa 1313
f01e580f
PG
1314 if (r1->afi != r2->afi)
1315 return false;
1316
f3d32faa 1317 if (memcmp(&r1->nh, &r2->nh, sizeof(struct nexthop)))
74df8d6d
DS
1318 return false;
1319
1320 return true;
f3d32faa 1321}
bbe6ffd6 1322
ffee150e
PG
1323struct bgp_pbr_rule *bgp_pbr_rule_lookup(vrf_id_t vrf_id,
1324 uint32_t unique)
1325{
1326 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1327 struct bgp_pbr_rule_unique bpru;
1328
1329 if (!bgp || unique == 0)
1330 return NULL;
1331 bpru.unique = unique;
1332 bpru.bpr_found = NULL;
1333 hash_walk(bgp->pbr_rule_hash, bgp_pbr_rule_walkcb, &bpru);
1334 return bpru.bpr_found;
1335}
1336
70eabd12
PG
1337struct bgp_pbr_action *bgp_pbr_action_rule_lookup(vrf_id_t vrf_id,
1338 uint32_t unique)
bbe6ffd6 1339{
70eabd12
PG
1340 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1341 struct bgp_pbr_action_unique bpau;
1342
1343 if (!bgp || unique == 0)
1344 return NULL;
1345 bpau.unique = unique;
1346 bpau.bpa_found = NULL;
1347 hash_walk(bgp->pbr_action_hash, bgp_pbr_action_walkcb, &bpau);
1348 return bpau.bpa_found;
bbe6ffd6
PG
1349}
1350
1351struct bgp_pbr_match *bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id,
1352 uint32_t unique)
1353{
c5d429e1
PG
1354 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1355 struct bgp_pbr_match_unique bpmu;
1356
1357 if (!bgp || unique == 0)
1358 return NULL;
1359 bpmu.unique = unique;
1360 bpmu.bpm_found = NULL;
1361 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_walkcb, &bpmu);
1362 return bpmu.bpm_found;
bbe6ffd6
PG
1363}
1364
1365struct bgp_pbr_match_entry *bgp_pbr_match_ipset_entry_lookup(vrf_id_t vrf_id,
1366 char *ipset_name,
1367 uint32_t unique)
1368{
c5d429e1
PG
1369 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1370 struct bgp_pbr_match_entry_unique bpmeu;
1371 struct bgp_pbr_match_ipsetname bpmi;
1372
1373 if (!bgp || unique == 0)
1374 return NULL;
1375 bpmi.ipsetname = XCALLOC(MTYPE_TMP, ZEBRA_IPSET_NAME_SIZE);
1376 snprintf(bpmi.ipsetname, ZEBRA_IPSET_NAME_SIZE, "%s", ipset_name);
1377 bpmi.bpm_found = NULL;
1378 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_pername_walkcb, &bpmi);
1379 XFREE(MTYPE_TMP, bpmi.ipsetname);
1380 if (!bpmi.bpm_found)
1381 return NULL;
1382 bpmeu.bpme_found = NULL;
1383 bpmeu.unique = unique;
1384 hash_walk(bpmi.bpm_found->entry_hash,
1385 bgp_pbr_match_entry_walkcb, &bpmeu);
1386 return bpmeu.bpme_found;
bbe6ffd6
PG
1387}
1388
1815c6fc
PG
1389struct bgp_pbr_match *bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id,
1390 uint32_t unique)
1391{
1392 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1393 struct bgp_pbr_match_iptable_unique bpmiu;
1394
1395 if (!bgp || unique == 0)
1396 return NULL;
1397 bpmiu.unique = unique;
1398 bpmiu.bpm_found = NULL;
1399 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_iptable_walkcb, &bpmiu);
1400 return bpmiu.bpm_found;
1401}
1402
a6b07429
PG
1403void bgp_pbr_cleanup(struct bgp *bgp)
1404{
1405 if (bgp->pbr_match_hash) {
1406 hash_clean(bgp->pbr_match_hash, bgp_pbr_match_free);
1407 hash_free(bgp->pbr_match_hash);
1408 bgp->pbr_match_hash = NULL;
1409 }
27e376d4
PG
1410 if (bgp->pbr_rule_hash) {
1411 hash_clean(bgp->pbr_rule_hash, bgp_pbr_rule_free);
1412 hash_free(bgp->pbr_rule_hash);
1413 bgp->pbr_rule_hash = NULL;
1414 }
a6b07429
PG
1415 if (bgp->pbr_action_hash) {
1416 hash_clean(bgp->pbr_action_hash, bgp_pbr_action_free);
1417 hash_free(bgp->pbr_action_hash);
1418 bgp->pbr_action_hash = NULL;
1419 }
4762c213
PG
1420 if (bgp->bgp_pbr_cfg == NULL)
1421 return;
1422 bgp_pbr_reset(bgp, AFI_IP);
8f242187 1423 bgp_pbr_reset(bgp, AFI_IP6);
4762c213 1424 XFREE(MTYPE_PBR, bgp->bgp_pbr_cfg);
a6b07429
PG
1425}
1426
f3d32faa
PG
1427void bgp_pbr_init(struct bgp *bgp)
1428{
1429 bgp->pbr_match_hash =
1430 hash_create_size(8, bgp_pbr_match_hash_key,
1431 bgp_pbr_match_hash_equal,
1432 "Match Hash");
1433 bgp->pbr_action_hash =
1434 hash_create_size(8, bgp_pbr_action_hash_key,
1435 bgp_pbr_action_hash_equal,
1436 "Match Hash Entry");
4762c213 1437
27e376d4
PG
1438 bgp->pbr_rule_hash =
1439 hash_create_size(8, bgp_pbr_rule_hash_key,
1440 bgp_pbr_rule_hash_equal,
1441 "Match Rule");
1442
4762c213
PG
1443 bgp->bgp_pbr_cfg = XCALLOC(MTYPE_PBR, sizeof(struct bgp_pbr_config));
1444 bgp->bgp_pbr_cfg->pbr_interface_any_ipv4 = true;
f3d32faa 1445}
b46b6f1a
PG
1446
1447void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main *api)
1448{
1449 int i = 0;
1450 char return_string[512];
1451 char *ptr = return_string;
b46b6f1a 1452 int nb_items = 0;
9cec4121 1453 int delta, len = sizeof(return_string);
b46b6f1a 1454
9cec4121
PG
1455 delta = snprintf(ptr, sizeof(return_string), "MATCH : ");
1456 len -= delta;
1457 ptr += delta;
b46b6f1a
PG
1458 if (api->match_bitmask & PREFIX_SRC_PRESENT) {
1459 struct prefix *p = &(api->src_prefix);
1460
9cec4121 1461 if (api->src_prefix_offset)
2dbe669b
DA
1462 delta = snprintfrr(ptr, len, "@src %pFX/off%u", p,
1463 api->src_prefix_offset);
9cec4121 1464 else
2dbe669b 1465 delta = snprintfrr(ptr, len, "@src %pFX", p);
9cec4121
PG
1466 len -= delta;
1467 ptr += delta;
1468 INCREMENT_DISPLAY(ptr, nb_items, len);
b46b6f1a
PG
1469 }
1470 if (api->match_bitmask & PREFIX_DST_PRESENT) {
1471 struct prefix *p = &(api->dst_prefix);
1472
9cec4121
PG
1473 INCREMENT_DISPLAY(ptr, nb_items, len);
1474 if (api->dst_prefix_offset)
2dbe669b
DA
1475 delta = snprintfrr(ptr, len, "@dst %pFX/off%u", p,
1476 api->dst_prefix_offset);
9cec4121 1477 else
2dbe669b 1478 delta = snprintfrr(ptr, len, "@dst %pFX", p);
9cec4121
PG
1479 len -= delta;
1480 ptr += delta;
b46b6f1a
PG
1481 }
1482
1483 if (api->match_protocol_num)
9cec4121
PG
1484 INCREMENT_DISPLAY(ptr, nb_items, len);
1485 for (i = 0; i < api->match_protocol_num; i++) {
1486 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->protocol[i],
1487 i > 0 ? NULL : "@proto ");
1488 len -= delta;
1489 ptr += delta;
1490 }
b46b6f1a
PG
1491
1492 if (api->match_src_port_num)
9cec4121
PG
1493 INCREMENT_DISPLAY(ptr, nb_items, len);
1494 for (i = 0; i < api->match_src_port_num; i++) {
1495 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->src_port[i],
1496 i > 0 ? NULL : "@srcport ");
1497 len -= delta;
1498 ptr += delta;
1499 }
b46b6f1a
PG
1500
1501 if (api->match_dst_port_num)
9cec4121
PG
1502 INCREMENT_DISPLAY(ptr, nb_items, len);
1503 for (i = 0; i < api->match_dst_port_num; i++) {
1504 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->dst_port[i],
1505 i > 0 ? NULL : "@dstport ");
1506 len -= delta;
1507 ptr += delta;
1508 }
b46b6f1a
PG
1509
1510 if (api->match_port_num)
9cec4121
PG
1511 INCREMENT_DISPLAY(ptr, nb_items, len);
1512 for (i = 0; i < api->match_port_num; i++) {
1513 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->port[i],
1514 i > 0 ? NULL : "@port ");
1515 len -= delta;
1516 ptr += delta;
1517 }
b46b6f1a
PG
1518
1519 if (api->match_icmp_type_num)
9cec4121
PG
1520 INCREMENT_DISPLAY(ptr, nb_items, len);
1521 for (i = 0; i < api->match_icmp_type_num; i++) {
1522 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->icmp_type[i],
1523 i > 0 ? NULL : "@icmptype ");
1524 len -= delta;
1525 ptr += delta;
1526 }
b46b6f1a
PG
1527
1528 if (api->match_icmp_code_num)
9cec4121
PG
1529 INCREMENT_DISPLAY(ptr, nb_items, len);
1530 for (i = 0; i < api->match_icmp_code_num; i++) {
1531 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->icmp_code[i],
1532 i > 0 ? NULL : "@icmpcode ");
1533 len -= delta;
1534 ptr += delta;
1535 }
b46b6f1a
PG
1536
1537 if (api->match_packet_length_num)
9cec4121
PG
1538 INCREMENT_DISPLAY(ptr, nb_items, len);
1539 for (i = 0; i < api->match_packet_length_num; i++) {
4371bf91
PG
1540 delta = snprintf_bgp_pbr_match_val(ptr, len,
1541 &api->packet_length[i],
9cec4121
PG
1542 i > 0 ? NULL : "@plen ");
1543 len -= delta;
1544 ptr += delta;
1545 }
b46b6f1a
PG
1546
1547 if (api->match_dscp_num)
9cec4121
PG
1548 INCREMENT_DISPLAY(ptr, nb_items, len);
1549 for (i = 0; i < api->match_dscp_num; i++) {
1550 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->dscp[i],
1551 i > 0 ? NULL : "@dscp ");
1552 len -= delta;
1553 ptr += delta;
1554 }
b46b6f1a 1555
40881800
PG
1556 if (api->match_flowlabel_num)
1557 INCREMENT_DISPLAY(ptr, nb_items, len);
1558 for (i = 0; i < api->match_flowlabel_num; i++) {
4371bf91
PG
1559 delta = snprintf_bgp_pbr_match_val(ptr, len,
1560 &api->flow_label[i],
40881800
PG
1561 i > 0 ? NULL : "@flowlabel ");
1562 len -= delta;
1563 ptr += delta;
1564 }
1565
b46b6f1a 1566 if (api->match_tcpflags_num)
9cec4121
PG
1567 INCREMENT_DISPLAY(ptr, nb_items, len);
1568 for (i = 0; i < api->match_tcpflags_num; i++) {
1569 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->tcpflags[i],
1570 i > 0 ? NULL : "@tcpflags ");
1571 len -= delta;
1572 ptr += delta;
1573 }
b46b6f1a 1574
588ec356 1575 if (api->match_fragment_num)
9cec4121
PG
1576 INCREMENT_DISPLAY(ptr, nb_items, len);
1577 for (i = 0; i < api->match_fragment_num; i++) {
1578 delta = snprintf_bgp_pbr_match_val(ptr, len, &api->fragment[i],
1579 i > 0 ? NULL : "@fragment ");
1580 len -= delta;
1581 ptr += delta;
1582 }
1583
1584 len = sizeof(return_string);
1585 if (!nb_items) {
b46b6f1a 1586 ptr = return_string;
9cec4121
PG
1587 } else {
1588 len -= (ptr - return_string);
1589 delta = snprintf(ptr, len, "; ");
1590 len -= delta;
1591 ptr += delta;
1592 }
1593 if (api->action_num) {
1594 delta = snprintf(ptr, len, "SET : ");
1595 len -= delta;
1596 ptr += delta;
1597 }
b46b6f1a
PG
1598 nb_items = 0;
1599 for (i = 0; i < api->action_num; i++) {
1600 switch (api->actions[i].action) {
1601 case ACTION_TRAFFICRATE:
9cec4121
PG
1602 INCREMENT_DISPLAY(ptr, nb_items, len);
1603 delta = snprintf(ptr, len, "@set rate %f",
1604 api->actions[i].u.r.rate);
1605 len -= delta;
1606 ptr += delta;
b46b6f1a
PG
1607 break;
1608 case ACTION_TRAFFIC_ACTION:
9cec4121
PG
1609 INCREMENT_DISPLAY(ptr, nb_items, len);
1610 delta = snprintf(ptr, len, "@action ");
1611 len -= delta;
1612 ptr += delta;
b46b6f1a 1613 if (api->actions[i].u.za.filter
9cec4121
PG
1614 & TRAFFIC_ACTION_TERMINATE) {
1615 delta = snprintf(ptr, len,
1616 " terminate (apply filter(s))");
1617 len -= delta;
1618 ptr += delta;
1619 }
b46b6f1a 1620 if (api->actions[i].u.za.filter
9cec4121
PG
1621 & TRAFFIC_ACTION_DISTRIBUTE) {
1622 delta = snprintf(ptr, len, " distribute");
1623 len -= delta;
1624 ptr += delta;
1625 }
b46b6f1a 1626 if (api->actions[i].u.za.filter
9cec4121
PG
1627 & TRAFFIC_ACTION_SAMPLE) {
1628 delta = snprintf(ptr, len, " sample");
1629 len -= delta;
1630 ptr += delta;
1631 }
b46b6f1a 1632 break;
f01e580f
PG
1633 case ACTION_REDIRECT_IP: {
1634 char local_buff[INET6_ADDRSTRLEN];
1635 void *ptr_ip;
b46b6f1a 1636
9cec4121 1637 INCREMENT_DISPLAY(ptr, nb_items, len);
f01e580f
PG
1638 if (api->afi == AF_INET)
1639 ptr_ip = &api->actions[i].u.zr.redirect_ip_v4;
1640 else
1641 ptr_ip = &api->actions[i].u.zr.redirect_ip_v6;
1642 if (inet_ntop(afi2family(api->afi),
1643 ptr_ip, local_buff,
1644 INET6_ADDRSTRLEN) != NULL) {
9cec4121 1645 delta = snprintf(ptr, len,
b46b6f1a 1646 "@redirect ip nh %s", local_buff);
9cec4121
PG
1647 len -= delta;
1648 ptr += delta;
f01e580f 1649 }
b46b6f1a 1650 break;
f01e580f 1651 }
137147c6
DS
1652 case ACTION_REDIRECT: {
1653 struct vrf *vrf;
1654
1655 vrf = vrf_lookup_by_id(api->actions[i].u.redirect_vrf);
9cec4121
PG
1656 INCREMENT_DISPLAY(ptr, nb_items, len);
1657 delta = snprintf(ptr, len, "@redirect vrf %s(%u)",
1658 VRF_LOGNAME(vrf),
1659 api->actions[i].u.redirect_vrf);
1660 len -= delta;
1661 ptr += delta;
b46b6f1a 1662 break;
137147c6 1663 }
b46b6f1a 1664 case ACTION_MARKING:
9cec4121 1665 INCREMENT_DISPLAY(ptr, nb_items, len);
40881800 1666 delta = snprintf(ptr, len, "@set dscp/flowlabel %u",
9cec4121
PG
1667 api->actions[i].u.marking_dscp);
1668 len -= delta;
1669 ptr += delta;
b46b6f1a
PG
1670 break;
1671 default:
1672 break;
1673 }
1674 }
1675 zlog_info("%s", return_string);
1676}
45918cfb 1677
9350f1df
PG
1678static void bgp_pbr_flush_iprule(struct bgp *bgp, struct bgp_pbr_action *bpa,
1679 struct bgp_pbr_rule *bpr)
1680{
1681 /* if bpr is null, do nothing
1682 */
1683 if (bpr == NULL)
1684 return;
1685 if (bpr->installed) {
1686 bgp_send_pbr_rule_action(bpa, bpr, false);
1687 bpr->installed = false;
1688 bpr->action->refcnt--;
1689 bpr->action = NULL;
ce3c0614
PG
1690 if (bpr->path) {
1691 struct bgp_path_info *path;
1692 struct bgp_path_info_extra *extra;
1693
1694 /* unlink path to bpme */
1695 path = (struct bgp_path_info *)bpr->path;
1696 extra = bgp_path_info_extra_get(path);
3e3708cb
PG
1697 if (extra->bgp_fs_iprule)
1698 listnode_delete(extra->bgp_fs_iprule, bpr);
ce3c0614
PG
1699 bpr->path = NULL;
1700 }
9350f1df
PG
1701 }
1702 hash_release(bgp->pbr_rule_hash, bpr);
ebd1a47c 1703 bgp_pbr_bpa_remove(bpa);
9350f1df
PG
1704}
1705
d114b0d7
PG
1706static void bgp_pbr_flush_entry(struct bgp *bgp, struct bgp_pbr_action *bpa,
1707 struct bgp_pbr_match *bpm,
1708 struct bgp_pbr_match_entry *bpme)
1709{
1710 /* if bpme is null, bpm is also null
1711 */
1712 if (bpme == NULL)
1713 return;
1714 /* ipset del entry */
1715 if (bpme->installed) {
1716 bgp_send_pbr_ipset_entry_match(bpme, false);
1717 bpme->installed = false;
1718 bpme->backpointer = NULL;
9b6d8fcf
DS
1719 if (bpme->path) {
1720 struct bgp_path_info *path;
4b7e6066 1721 struct bgp_path_info_extra *extra;
b588b642 1722
ce3c0614 1723 /* unlink path to bpme */
9b6d8fcf
DS
1724 path = (struct bgp_path_info *)bpme->path;
1725 extra = bgp_path_info_extra_get(path);
3e3708cb
PG
1726 if (extra->bgp_fs_pbr)
1727 listnode_delete(extra->bgp_fs_pbr, bpme);
9b6d8fcf 1728 bpme->path = NULL;
b588b642 1729 }
d114b0d7
PG
1730 }
1731 hash_release(bpm->entry_hash, bpme);
1732 if (hashcount(bpm->entry_hash) == 0) {
1733 /* delete iptable entry first */
1734 /* then delete ipset match */
1735 if (bpm->installed) {
1736 if (bpm->installed_in_iptable) {
1737 bgp_send_pbr_iptable(bpm->action,
1738 bpm, false);
1739 bpm->installed_in_iptable = false;
a6b07429 1740 bpm->action->refcnt--;
d114b0d7
PG
1741 }
1742 bgp_send_pbr_ipset_match(bpm, false);
1743 bpm->installed = false;
1744 bpm->action = NULL;
1745 }
1746 hash_release(bgp->pbr_match_hash, bpm);
1747 /* XXX release pbr_match_action if not used
1748 * note that drop does not need to call send_pbr_action
1749 */
1750 }
ebd1a47c 1751 bgp_pbr_bpa_remove(bpa);
d114b0d7
PG
1752}
1753
1754struct bgp_pbr_match_entry_remain {
1755 struct bgp_pbr_match_entry *bpme_to_match;
1756 struct bgp_pbr_match_entry *bpme_found;
1757};
1758
9350f1df
PG
1759struct bgp_pbr_rule_remain {
1760 struct bgp_pbr_rule *bpr_to_match;
1761 struct bgp_pbr_rule *bpr_found;
1762};
1763
e3b78da8 1764static int bgp_pbr_get_same_rule(struct hash_bucket *bucket, void *arg)
9350f1df 1765{
e3b78da8 1766 struct bgp_pbr_rule *r1 = (struct bgp_pbr_rule *)bucket->data;
9350f1df
PG
1767 struct bgp_pbr_rule_remain *ctxt =
1768 (struct bgp_pbr_rule_remain *)arg;
1769 struct bgp_pbr_rule *r2;
1770
1771 r2 = ctxt->bpr_to_match;
1772
1773 if (r1->vrf_id != r2->vrf_id)
1774 return HASHWALK_CONTINUE;
1775
1776 if (r1->flags != r2->flags)
1777 return HASHWALK_CONTINUE;
1778
1779 if ((r1->flags & MATCH_IP_SRC_SET) &&
1780 !prefix_same(&r1->src, &r2->src))
1781 return HASHWALK_CONTINUE;
1782
1783 if ((r1->flags & MATCH_IP_DST_SET) &&
1784 !prefix_same(&r1->dst, &r2->dst))
1785 return HASHWALK_CONTINUE;
1786
1787 /* this function is used for two cases:
1788 * - remove an entry upon withdraw request
1789 * (case r2->action is null)
1790 * - replace an old iprule with different action
1791 * (case r2->action is != null)
1792 * the old one is removed after the new one
1793 * this is to avoid disruption in traffic
1794 */
1795 if (r2->action == NULL ||
1796 r1->action != r2->action) {
1797 ctxt->bpr_found = r1;
1798 return HASHWALK_ABORT;
1799 }
1800 return HASHWALK_CONTINUE;
1801}
1802
e3b78da8 1803static int bgp_pbr_get_remaining_entry(struct hash_bucket *bucket, void *arg)
d114b0d7 1804{
e3b78da8 1805 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)bucket->data;
d114b0d7
PG
1806 struct bgp_pbr_match_entry_remain *bpmer =
1807 (struct bgp_pbr_match_entry_remain *)arg;
1808 struct bgp_pbr_match *bpm_temp;
1809 struct bgp_pbr_match_entry *bpme = bpmer->bpme_to_match;
1810
1811 if (!bpme->backpointer ||
1812 bpm == bpme->backpointer ||
1813 bpme->backpointer->action == bpm->action)
1814 return HASHWALK_CONTINUE;
1815 /* ensure bpm other characteristics are equal */
1816 bpm_temp = bpme->backpointer;
1817 if (bpm_temp->vrf_id != bpm->vrf_id ||
1818 bpm_temp->type != bpm->type ||
836b6953
PG
1819 bpm_temp->flags != bpm->flags ||
1820 bpm_temp->tcp_flags != bpm->tcp_flags ||
1821 bpm_temp->tcp_mask_flags != bpm->tcp_mask_flags ||
1822 bpm_temp->pkt_len_min != bpm->pkt_len_min ||
1823 bpm_temp->pkt_len_max != bpm->pkt_len_max ||
1824 bpm_temp->dscp_value != bpm->dscp_value ||
40881800 1825 bpm_temp->flow_label != bpm->flow_label ||
a60b7031 1826 bpm_temp->family != bpm->family ||
836b6953 1827 bpm_temp->fragment != bpm->fragment)
d114b0d7
PG
1828 return HASHWALK_CONTINUE;
1829
1830 /* look for remaining bpme */
1831 bpmer->bpme_found = hash_lookup(bpm->entry_hash, bpme);
1832 if (!bpmer->bpme_found)
1833 return HASHWALK_CONTINUE;
1834 return HASHWALK_ABORT;
1835}
1836
9b6d8fcf
DS
1837static void bgp_pbr_policyroute_remove_from_zebra_unit(
1838 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf)
d114b0d7
PG
1839{
1840 struct bgp_pbr_match temp;
1841 struct bgp_pbr_match_entry temp2;
9350f1df
PG
1842 struct bgp_pbr_rule pbr_rule;
1843 struct bgp_pbr_rule *bpr;
d114b0d7
PG
1844 struct bgp_pbr_match *bpm;
1845 struct bgp_pbr_match_entry *bpme;
1846 struct bgp_pbr_match_entry_remain bpmer;
0e867886
PG
1847 struct bgp_pbr_range_port *src_port;
1848 struct bgp_pbr_range_port *dst_port;
1849 struct bgp_pbr_range_port *pkt_len;
9350f1df 1850 struct bgp_pbr_rule_remain bprr;
0e867886
PG
1851
1852 if (!bpf)
1853 return;
1854 src_port = bpf->src_port;
1855 dst_port = bpf->dst_port;
1856 pkt_len = bpf->pkt_len;
d114b0d7 1857
064a9d52
PG
1858 if (BGP_DEBUG(zebra, ZEBRA))
1859 bgp_pbr_dump_entry(bpf, false);
1860
d114b0d7
PG
1861 /* as we don't know information from EC
1862 * look for bpm that have the bpm
1863 * with vrf_id characteristics
1864 */
1865 memset(&temp2, 0, sizeof(temp2));
1866 memset(&temp, 0, sizeof(temp));
9350f1df
PG
1867
1868 if (bpf->type == BGP_PBR_IPRULE) {
1869 memset(&pbr_rule, 0, sizeof(pbr_rule));
1870 pbr_rule.vrf_id = bpf->vrf_id;
1871 if (bpf->src) {
1872 prefix_copy(&pbr_rule.src, bpf->src);
1873 pbr_rule.flags |= MATCH_IP_SRC_SET;
1874 }
1875 if (bpf->dst) {
1876 prefix_copy(&pbr_rule.dst, bpf->dst);
1877 pbr_rule.flags |= MATCH_IP_DST_SET;
1878 }
1879 bpr = &pbr_rule;
1880 /* A previous entry may already exist
1881 * flush previous entry if necessary
1882 */
1883 bprr.bpr_to_match = bpr;
1884 bprr.bpr_found = NULL;
1885 hash_walk(bgp->pbr_rule_hash, bgp_pbr_get_same_rule, &bprr);
1886 if (bprr.bpr_found) {
1887 static struct bgp_pbr_rule *local_bpr;
1888 static struct bgp_pbr_action *local_bpa;
1889
1890 local_bpr = bprr.bpr_found;
1891 local_bpa = local_bpr->action;
1892 bgp_pbr_flush_iprule(bgp, local_bpa,
1893 local_bpr);
1894 }
1895 return;
1896 }
1897
a60b7031 1898 temp.family = bpf->family;
0e867886 1899 if (bpf->src) {
d114b0d7 1900 temp.flags |= MATCH_IP_SRC_SET;
0e867886 1901 prefix_copy(&temp2.src, bpf->src);
d114b0d7 1902 } else
f01e580f 1903 temp2.src.family = bpf->family;
0e867886 1904 if (bpf->dst) {
d114b0d7 1905 temp.flags |= MATCH_IP_DST_SET;
0e867886 1906 prefix_copy(&temp2.dst, bpf->dst);
d114b0d7 1907 } else
f01e580f 1908 temp2.dst.family = bpf->family;
3bed2363
PG
1909 if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1910 if (bpf->protocol == IPPROTO_ICMP)
1911 temp.flags |= MATCH_ICMP_SET;
1de7dfff
PG
1912 temp.flags |= MATCH_PORT_SRC_SET;
1913 temp2.src_port_min = src_port->min_port;
1914 if (src_port->max_port) {
1915 temp.flags |= MATCH_PORT_SRC_RANGE_SET;
1916 temp2.src_port_max = src_port->max_port;
1917 }
1918 }
3bed2363
PG
1919 if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1920 if (bpf->protocol == IPPROTO_ICMP)
1921 temp.flags |= MATCH_ICMP_SET;
1de7dfff
PG
1922 temp.flags |= MATCH_PORT_DST_SET;
1923 temp2.dst_port_min = dst_port->min_port;
1924 if (dst_port->max_port) {
1925 temp.flags |= MATCH_PORT_DST_RANGE_SET;
1926 temp2.dst_port_max = dst_port->max_port;
1927 }
1928 }
0e867886 1929 temp2.proto = bpf->protocol;
1de7dfff 1930
cfaf18ce 1931 if (pkt_len) {
83360720 1932 temp.pkt_len_min = pkt_len->min_port;
cfaf18ce
PG
1933 if (pkt_len->max_port)
1934 temp.pkt_len_max = pkt_len->max_port;
1935 } else if (bpf->pkt_len_val) {
1936 if (bpf->pkt_len_val->mask)
1937 temp.flags |= MATCH_PKT_LEN_INVERSE_SET;
1938 temp.pkt_len_min = bpf->pkt_len_val->val;
1939 }
2da7d62e
PG
1940 if (bpf->tcp_flags) {
1941 temp.tcp_flags = bpf->tcp_flags->val;
1942 temp.tcp_mask_flags = bpf->tcp_flags->mask;
1943 }
4977bd6c
PG
1944 if (bpf->dscp) {
1945 if (bpf->dscp->mask)
1946 temp.flags |= MATCH_DSCP_INVERSE_SET;
1947 else
1948 temp.flags |= MATCH_DSCP_SET;
1949 temp.dscp_value = bpf->dscp->val;
1950 }
40881800
PG
1951 if (bpf->flow_label) {
1952 if (bpf->flow_label->mask)
1953 temp.flags |= MATCH_FLOW_LABEL_INVERSE_SET;
1954 else
1955 temp.flags |= MATCH_FLOW_LABEL_SET;
1956 temp.flow_label = bpf->flow_label->val;
1957 }
1958
6f5617d8
PG
1959 if (bpf->fragment) {
1960 if (bpf->fragment->mask)
1961 temp.flags |= MATCH_FRAGMENT_INVERSE_SET;
1962 temp.fragment = bpf->fragment->val;
1963 }
83360720 1964
0e867886 1965 if (bpf->src == NULL || bpf->dst == NULL) {
1de7dfff
PG
1966 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1967 temp.type = IPSET_NET_PORT;
1968 else
1969 temp.type = IPSET_NET;
1970 } else {
1971 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1972 temp.type = IPSET_NET_PORT_NET;
1973 else
1974 temp.type = IPSET_NET_NET;
1975 }
0e867886 1976 if (bpf->vrf_id == VRF_UNKNOWN) /* XXX case BGP destroy */
946de1b9 1977 temp.vrf_id = VRF_DEFAULT;
d114b0d7 1978 else
0e867886 1979 temp.vrf_id = bpf->vrf_id;
d114b0d7
PG
1980 bpme = &temp2;
1981 bpm = &temp;
1982 bpme->backpointer = bpm;
1983 /* right now, a previous entry may already exist
1984 * flush previous entry if necessary
1985 */
1986 bpmer.bpme_to_match = bpme;
1987 bpmer.bpme_found = NULL;
1988 hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer);
1989 if (bpmer.bpme_found) {
1990 static struct bgp_pbr_match *local_bpm;
1991 static struct bgp_pbr_action *local_bpa;
1992
1993 local_bpm = bpmer.bpme_found->backpointer;
1994 local_bpa = local_bpm->action;
1995 bgp_pbr_flush_entry(bgp, local_bpa,
1996 local_bpm, bpmer.bpme_found);
1997 }
1998}
1999
e45aea59
PG
2000static uint8_t bgp_pbr_next_type_entry(uint8_t type_entry)
2001{
2002 if (type_entry == FLOWSPEC_TCP_FLAGS)
2003 return FLOWSPEC_DSCP;
2004 if (type_entry == FLOWSPEC_DSCP)
40881800
PG
2005 return FLOWSPEC_FLOW_LABEL;
2006 if (type_entry == FLOWSPEC_FLOW_LABEL)
e45aea59
PG
2007 return FLOWSPEC_PKT_LEN;
2008 if (type_entry == FLOWSPEC_PKT_LEN)
2009 return FLOWSPEC_FRAGMENT;
da3fa383
PG
2010 if (type_entry == FLOWSPEC_FRAGMENT)
2011 return FLOWSPEC_ICMP_TYPE;
e45aea59
PG
2012 return 0;
2013}
2014
9b6d8fcf 2015static void bgp_pbr_icmp_action(struct bgp *bgp, struct bgp_path_info *path,
4b7e6066
DS
2016 struct bgp_pbr_filter *bpf,
2017 struct bgp_pbr_or_filter *bpof, bool add,
2018 struct nexthop *nh, float *rate)
da3fa383
PG
2019{
2020 struct bgp_pbr_range_port srcp, dstp;
2021 struct bgp_pbr_val_mask *icmp_type, *icmp_code;
2022 struct listnode *tnode, *cnode;
2023
2024 if (!bpf)
2025 return;
2026 if (bpf->protocol != IPPROTO_ICMP)
2027 return;
60031a55
DS
2028
2029 memset(&srcp, 0, sizeof(srcp));
2030 memset(&dstp, 0, sizeof(dstp));
da3fa383
PG
2031 bpf->src_port = &srcp;
2032 bpf->dst_port = &dstp;
2033 /* parse icmp type and lookup appropriate icmp code
2034 * if no icmp code found, create as many entryes as
2035 * there are listed icmp codes for that icmp type
2036 */
2037 if (!bpof->icmp_type) {
2038 srcp.min_port = 0;
2039 srcp.max_port = 255;
2040 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) {
2041 dstp.min_port = icmp_code->val;
2042 if (add)
9b6d8fcf
DS
2043 bgp_pbr_policyroute_add_to_zebra_unit(
2044 bgp, path, bpf, nh, rate);
da3fa383
PG
2045 else
2046 bgp_pbr_policyroute_remove_from_zebra_unit(
9b6d8fcf 2047 bgp, path, bpf);
da3fa383
PG
2048 }
2049 return;
2050 }
2051 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_type, tnode, icmp_type)) {
2052 srcp.min_port = icmp_type->val;
2053 srcp.max_port = 0;
2054 dstp.max_port = 0;
2055 /* only icmp type. create an entry only with icmp type */
2056 if (!bpof->icmp_code) {
2057 /* icmp type is not one of the above
2058 * forge an entry only based on the icmp type
2059 */
2060 dstp.min_port = 0;
2061 dstp.max_port = 255;
2062 if (add)
2063 bgp_pbr_policyroute_add_to_zebra_unit(
9b6d8fcf 2064 bgp, path, bpf, nh, rate);
da3fa383 2065 else
9b6d8fcf
DS
2066 bgp_pbr_policyroute_remove_from_zebra_unit(
2067 bgp, path, bpf);
da3fa383
PG
2068 continue;
2069 }
2070 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) {
2071 dstp.min_port = icmp_code->val;
2072 if (add)
2073 bgp_pbr_policyroute_add_to_zebra_unit(
9b6d8fcf 2074 bgp, path, bpf, nh, rate);
da3fa383
PG
2075 else
2076 bgp_pbr_policyroute_remove_from_zebra_unit(
9b6d8fcf 2077 bgp, path, bpf);
da3fa383
PG
2078 }
2079 }
2080}
2081
4b7e6066 2082static void bgp_pbr_policyroute_remove_from_zebra_recursive(
9b6d8fcf
DS
2083 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
2084 struct bgp_pbr_or_filter *bpof, uint8_t type_entry)
56707a36
PG
2085{
2086 struct listnode *node, *nnode;
2087 struct bgp_pbr_val_mask *valmask;
2088 uint8_t next_type_entry;
2089 struct list *orig_list;
2090 struct bgp_pbr_val_mask **target_val;
2091
d90b788e
A
2092 if (type_entry == 0) {
2093 bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path, bpf);
2094 return;
2095 }
e45aea59 2096 next_type_entry = bgp_pbr_next_type_entry(type_entry);
56707a36 2097 if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) {
56707a36
PG
2098 orig_list = bpof->tcpflags;
2099 target_val = &bpf->tcp_flags;
2100 } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) {
56707a36
PG
2101 orig_list = bpof->dscp;
2102 target_val = &bpf->dscp;
40881800
PG
2103 } else if (type_entry == FLOWSPEC_FLOW_LABEL && bpof->flowlabel) {
2104 orig_list = bpof->flowlabel;
2105 target_val = &bpf->flow_label;
cfaf18ce 2106 } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) {
cfaf18ce
PG
2107 orig_list = bpof->pkt_len;
2108 target_val = &bpf->pkt_len_val;
6f5617d8 2109 } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) {
6f5617d8
PG
2110 orig_list = bpof->fragment;
2111 target_val = &bpf->fragment;
da3fa383
PG
2112 } else if (type_entry == FLOWSPEC_ICMP_TYPE &&
2113 (bpof->icmp_type || bpof->icmp_code)) {
2114 /* enumerate list for icmp - must be last one */
9b6d8fcf 2115 bgp_pbr_icmp_action(bgp, path, bpf, bpof, false, NULL, NULL);
da3fa383 2116 return;
56707a36 2117 } else {
d90b788e 2118 bgp_pbr_policyroute_remove_from_zebra_recursive(
9b6d8fcf 2119 bgp, path, bpf, bpof, next_type_entry);
d90b788e 2120 return;
56707a36
PG
2121 }
2122 for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) {
2123 *target_val = valmask;
9b6d8fcf
DS
2124 bgp_pbr_policyroute_remove_from_zebra_recursive(
2125 bgp, path, bpf, bpof, next_type_entry);
56707a36
PG
2126 }
2127}
2128
4b7e6066 2129static void bgp_pbr_policyroute_remove_from_zebra(
9b6d8fcf
DS
2130 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
2131 struct bgp_pbr_or_filter *bpof)
9bba1455 2132{
d90b788e
A
2133 if (!bpof) {
2134 bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path, bpf);
2135 return;
2136 }
56707a36 2137 if (bpof->tcpflags)
9b6d8fcf
DS
2138 bgp_pbr_policyroute_remove_from_zebra_recursive(
2139 bgp, path, bpf, bpof, FLOWSPEC_TCP_FLAGS);
56707a36 2140 else if (bpof->dscp)
9b6d8fcf
DS
2141 bgp_pbr_policyroute_remove_from_zebra_recursive(
2142 bgp, path, bpf, bpof, FLOWSPEC_DSCP);
40881800
PG
2143 else if (bpof->flowlabel)
2144 bgp_pbr_policyroute_remove_from_zebra_recursive(
2145 bgp, path, bpf, bpof, FLOWSPEC_FLOW_LABEL);
cfaf18ce 2146 else if (bpof->pkt_len)
9b6d8fcf
DS
2147 bgp_pbr_policyroute_remove_from_zebra_recursive(
2148 bgp, path, bpf, bpof, FLOWSPEC_PKT_LEN);
6f5617d8 2149 else if (bpof->fragment)
9b6d8fcf
DS
2150 bgp_pbr_policyroute_remove_from_zebra_recursive(
2151 bgp, path, bpf, bpof, FLOWSPEC_FRAGMENT);
da3fa383 2152 else if (bpof->icmp_type || bpof->icmp_code)
9b6d8fcf
DS
2153 bgp_pbr_policyroute_remove_from_zebra_recursive(
2154 bgp, path, bpf, bpof, FLOWSPEC_ICMP_TYPE);
56707a36 2155 else
9b6d8fcf 2156 bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path, bpf);
56707a36
PG
2157 /* flush bpof */
2158 if (bpof->tcpflags)
2159 list_delete_all_node(bpof->tcpflags);
2160 if (bpof->dscp)
2161 list_delete_all_node(bpof->dscp);
40881800
PG
2162 if (bpof->flowlabel)
2163 list_delete_all_node(bpof->flowlabel);
cfaf18ce
PG
2164 if (bpof->pkt_len)
2165 list_delete_all_node(bpof->pkt_len);
6f5617d8
PG
2166 if (bpof->fragment)
2167 list_delete_all_node(bpof->fragment);
9bba1455
PG
2168}
2169
064a9d52
PG
2170static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add)
2171{
2172 struct bgp_pbr_range_port *src_port;
2173 struct bgp_pbr_range_port *dst_port;
2174 struct bgp_pbr_range_port *pkt_len;
2175 char bufsrc[64], bufdst[64];
2176 char buffer[64];
2177 int remaining_len = 0;
2178 char protocol_str[16];
2179
2180 if (!bpf)
2181 return;
2182 src_port = bpf->src_port;
2183 dst_port = bpf->dst_port;
2184 pkt_len = bpf->pkt_len;
2185
2186 protocol_str[0] = '\0';
2187 if (bpf->tcp_flags && bpf->tcp_flags->mask)
2188 bpf->protocol = IPPROTO_TCP;
2189 if (bpf->protocol)
2190 snprintf(protocol_str, sizeof(protocol_str),
2191 "proto %d", bpf->protocol);
2192 buffer[0] = '\0';
2193 if (bpf->protocol == IPPROTO_ICMP && src_port && dst_port)
2194 remaining_len += snprintf(buffer, sizeof(buffer),
2195 "type %d, code %d",
2196 src_port->min_port,
2197 dst_port->min_port);
2198 else if (bpf->protocol == IPPROTO_UDP ||
2199 bpf->protocol == IPPROTO_TCP) {
2200
2201 if (src_port && src_port->min_port)
2202 remaining_len += snprintf(buffer,
2203 sizeof(buffer),
2204 "from [%u:%u]",
2205 src_port->min_port,
2206 src_port->max_port ?
2207 src_port->max_port :
2208 src_port->min_port);
2209 if (dst_port && dst_port->min_port)
2210 remaining_len += snprintf(buffer +
2211 remaining_len,
2212 sizeof(buffer)
2213 - remaining_len,
2214 "to [%u:%u]",
2215 dst_port->min_port,
2216 dst_port->max_port ?
2217 dst_port->max_port :
2218 dst_port->min_port);
2219 }
2220 if (pkt_len && (pkt_len->min_port || pkt_len->max_port)) {
2221 remaining_len += snprintf(buffer + remaining_len,
2222 sizeof(buffer)
2223 - remaining_len,
2224 " len [%u:%u]",
2225 pkt_len->min_port,
2226 pkt_len->max_port ?
2227 pkt_len->max_port :
2228 pkt_len->min_port);
2229 } else if (bpf->pkt_len_val) {
2230 remaining_len += snprintf(buffer + remaining_len,
2231 sizeof(buffer)
2232 - remaining_len,
2233 " %s len %u",
2234 bpf->pkt_len_val->mask
2235 ? "!" : "",
2236 bpf->pkt_len_val->val);
2237 }
2238 if (bpf->tcp_flags) {
2239 remaining_len += snprintf(buffer + remaining_len,
2240 sizeof(buffer)
2241 - remaining_len,
2242 "tcpflags %x/%x",
2243 bpf->tcp_flags->val,
2244 bpf->tcp_flags->mask);
2245 }
2246 if (bpf->dscp) {
2247 snprintf(buffer + remaining_len,
2248 sizeof(buffer)
2249 - remaining_len,
2250 "%s dscp %d",
2251 bpf->dscp->mask
2252 ? "!" : "",
2253 bpf->dscp->val);
2254 }
40881800
PG
2255 if (bpf->flow_label) {
2256 snprintf(buffer + remaining_len,
2257 sizeof(buffer)
2258 - remaining_len,
2259 "%s flow_label %d",
2260 bpf->flow_label->mask
2261 ? "!" : "",
2262 bpf->flow_label->val);
2263 }
45837bc4 2264 zlog_debug("BGP: %s FS PBR from %s to %s, %s %s",
064a9d52
PG
2265 add ? "adding" : "removing",
2266 bpf->src == NULL ? "<all>" :
2267 prefix2str(bpf->src, bufsrc, sizeof(bufsrc)),
2268 bpf->dst == NULL ? "<all>" :
2269 prefix2str(bpf->dst, bufdst, sizeof(bufdst)),
2270 protocol_str, buffer);
2271
2272}
2273
9bba1455 2274static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp *bgp,
9b6d8fcf 2275 struct bgp_path_info *path,
4b7e6066
DS
2276 struct bgp_pbr_filter *bpf,
2277 struct nexthop *nh,
2278 float *rate)
d114b0d7
PG
2279{
2280 struct bgp_pbr_match temp;
2281 struct bgp_pbr_match_entry temp2;
2282 struct bgp_pbr_match *bpm;
2283 struct bgp_pbr_match_entry *bpme = NULL;
2284 struct bgp_pbr_action temp3;
2285 struct bgp_pbr_action *bpa = NULL;
2286 struct bgp_pbr_match_entry_remain bpmer;
9350f1df 2287 struct bgp_pbr_rule_remain bprr;
0e867886
PG
2288 struct bgp_pbr_range_port *src_port;
2289 struct bgp_pbr_range_port *dst_port;
2290 struct bgp_pbr_range_port *pkt_len;
9350f1df
PG
2291 struct bgp_pbr_rule pbr_rule;
2292 struct bgp_pbr_rule *bpr;
ce3c0614 2293 bool bpr_found = false;
c26edcda 2294 bool bpme_found = false;
0e867886
PG
2295
2296 if (!bpf)
2297 return;
2298 src_port = bpf->src_port;
2299 dst_port = bpf->dst_port;
2300 pkt_len = bpf->pkt_len;
d114b0d7 2301
064a9d52
PG
2302 if (BGP_DEBUG(zebra, ZEBRA))
2303 bgp_pbr_dump_entry(bpf, true);
2304
d114b0d7
PG
2305 /* look for bpa first */
2306 memset(&temp3, 0, sizeof(temp3));
2307 if (rate)
2308 temp3.rate = *rate;
2309 if (nh)
2310 memcpy(&temp3.nh, nh, sizeof(struct nexthop));
0e867886 2311 temp3.vrf_id = bpf->vrf_id;
f01e580f 2312 temp3.afi = family2afi(bpf->family);
d114b0d7
PG
2313 bpa = hash_get(bgp->pbr_action_hash, &temp3,
2314 bgp_pbr_action_alloc_intern);
2315
2316 if (bpa->fwmark == 0) {
d114b0d7
PG
2317 /* drop is handled by iptable */
2318 if (nh && nh->type == NEXTHOP_TYPE_BLACKHOLE) {
2319 bpa->table_id = 0;
2320 bpa->installed = true;
2321 } else {
31c28cd7
PG
2322 bpa->fwmark = bgp_zebra_tm_get_id();
2323 bpa->table_id = bpa->fwmark;
d114b0d7
PG
2324 bpa->installed = false;
2325 }
a6b07429 2326 bpa->bgp = bgp;
d114b0d7
PG
2327 bpa->unique = ++bgp_pbr_action_counter_unique;
2328 /* 0 value is forbidden */
2329 bpa->install_in_progress = false;
2330 }
9350f1df
PG
2331 if (bpf->type == BGP_PBR_IPRULE) {
2332 memset(&pbr_rule, 0, sizeof(pbr_rule));
2333 pbr_rule.vrf_id = bpf->vrf_id;
8112a7a0 2334 pbr_rule.priority = 20;
9350f1df
PG
2335 if (bpf->src) {
2336 pbr_rule.flags |= MATCH_IP_SRC_SET;
2337 prefix_copy(&pbr_rule.src, bpf->src);
2338 }
2339 if (bpf->dst) {
2340 pbr_rule.flags |= MATCH_IP_DST_SET;
2341 prefix_copy(&pbr_rule.dst, bpf->dst);
2342 }
2343 pbr_rule.action = bpa;
2344 bpr = hash_get(bgp->pbr_rule_hash, &pbr_rule,
2345 bgp_pbr_rule_alloc_intern);
2346 if (bpr && bpr->unique == 0) {
2347 bpr->unique = ++bgp_pbr_action_counter_unique;
2348 bpr->installed = false;
2349 bpr->install_in_progress = false;
ce3c0614
PG
2350 /* link bgp info to bpr */
2351 bpr->path = (void *)path;
2352 } else
2353 bpr_found = true;
2354 /* already installed */
2355 if (bpr_found && bpr) {
2356 struct bgp_path_info_extra *extra =
2357 bgp_path_info_extra_get(path);
2358
e0c7edb0
PG
2359 if (extra &&
2360 listnode_lookup_nocheck(extra->bgp_fs_iprule,
2361 bpr)) {
ce3c0614 2362 if (BGP_DEBUG(pbr, PBR_ERROR))
3efd0893 2363 zlog_err("%s: entry %p/%p already installed in bgp pbr iprule",
ce3c0614
PG
2364 __func__, path, bpr);
2365 return;
2366 }
9350f1df 2367 }
db1fcc98
DS
2368
2369 bgp_pbr_bpa_add(bpa);
2370
9350f1df
PG
2371 /* ip rule add */
2372 if (bpr && !bpr->installed)
2373 bgp_send_pbr_rule_action(bpa, bpr, true);
d114b0d7 2374
9350f1df
PG
2375 /* A previous entry may already exist
2376 * flush previous entry if necessary
2377 */
2378 bprr.bpr_to_match = bpr;
2379 bprr.bpr_found = NULL;
2380 hash_walk(bgp->pbr_rule_hash, bgp_pbr_get_same_rule, &bprr);
2381 if (bprr.bpr_found) {
2382 static struct bgp_pbr_rule *local_bpr;
2383 static struct bgp_pbr_action *local_bpa;
2384
2385 local_bpr = bprr.bpr_found;
2386 local_bpa = local_bpr->action;
2387 bgp_pbr_flush_iprule(bgp, local_bpa,
2388 local_bpr);
2389 }
2390 return;
2391 }
d114b0d7
PG
2392 /* then look for bpm */
2393 memset(&temp, 0, sizeof(temp));
0e867886 2394 temp.vrf_id = bpf->vrf_id;
a60b7031 2395 temp.family = bpf->family;
0e867886 2396 if (bpf->src)
d114b0d7 2397 temp.flags |= MATCH_IP_SRC_SET;
0e867886 2398 if (bpf->dst)
d114b0d7 2399 temp.flags |= MATCH_IP_DST_SET;
1de7dfff 2400
3bed2363
PG
2401 if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
2402 if (bpf->protocol == IPPROTO_ICMP)
2403 temp.flags |= MATCH_ICMP_SET;
1de7dfff 2404 temp.flags |= MATCH_PORT_SRC_SET;
3bed2363
PG
2405 }
2406 if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
2407 if (bpf->protocol == IPPROTO_ICMP)
2408 temp.flags |= MATCH_ICMP_SET;
1de7dfff 2409 temp.flags |= MATCH_PORT_DST_SET;
3bed2363 2410 }
1de7dfff
PG
2411 if (src_port && src_port->max_port)
2412 temp.flags |= MATCH_PORT_SRC_RANGE_SET;
2413 if (dst_port && dst_port->max_port)
2414 temp.flags |= MATCH_PORT_DST_RANGE_SET;
3bed2363
PG
2415
2416 if (bpf->src == NULL || bpf->dst == NULL) {
2417 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
2418 temp.type = IPSET_NET_PORT;
2419 else
2420 temp.type = IPSET_NET;
2421 } else {
2422 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
2423 temp.type = IPSET_NET_PORT_NET;
2424 else
2425 temp.type = IPSET_NET_NET;
2426 }
cfaf18ce 2427 if (pkt_len) {
83360720 2428 temp.pkt_len_min = pkt_len->min_port;
cfaf18ce
PG
2429 if (pkt_len->max_port)
2430 temp.pkt_len_max = pkt_len->max_port;
2431 } else if (bpf->pkt_len_val) {
2432 if (bpf->pkt_len_val->mask)
2433 temp.flags |= MATCH_PKT_LEN_INVERSE_SET;
2434 temp.pkt_len_min = bpf->pkt_len_val->val;
2435 }
2da7d62e
PG
2436 if (bpf->tcp_flags) {
2437 temp.tcp_flags = bpf->tcp_flags->val;
2438 temp.tcp_mask_flags = bpf->tcp_flags->mask;
2439 }
4977bd6c
PG
2440 if (bpf->dscp) {
2441 if (bpf->dscp->mask)
2442 temp.flags |= MATCH_DSCP_INVERSE_SET;
2443 else
2444 temp.flags |= MATCH_DSCP_SET;
2445 temp.dscp_value = bpf->dscp->val;
2446 }
40881800
PG
2447 if (bpf->flow_label) {
2448 if (bpf->flow_label->mask)
2449 temp.flags |= MATCH_FLOW_LABEL_INVERSE_SET;
2450 else
2451 temp.flags |= MATCH_FLOW_LABEL_SET;
2452 temp.flow_label = bpf->flow_label->val;
2453 }
6f5617d8
PG
2454 if (bpf->fragment) {
2455 if (bpf->fragment->mask)
2456 temp.flags |= MATCH_FRAGMENT_INVERSE_SET;
2457 temp.fragment = bpf->fragment->val;
2458 }
f449d223
PG
2459 if (bpf->protocol) {
2460 temp.protocol = bpf->protocol;
2461 temp.flags |= MATCH_PROTOCOL_SET;
2462 }
d114b0d7
PG
2463 temp.action = bpa;
2464 bpm = hash_get(bgp->pbr_match_hash, &temp,
2465 bgp_pbr_match_alloc_intern);
2466
2467 /* new, then self allocate ipset_name and unique */
5a430eee 2468 if (bpm->unique == 0) {
d114b0d7
PG
2469 bpm->unique = ++bgp_pbr_match_counter_unique;
2470 /* 0 value is forbidden */
4371bf91
PG
2471 snprintf(bpm->ipset_name, sizeof(bpm->ipset_name),
2472 "match%p", bpm);
d114b0d7
PG
2473 bpm->entry_hash = hash_create_size(8,
2474 bgp_pbr_match_entry_hash_key,
2475 bgp_pbr_match_entry_hash_equal,
2476 "Match Entry Hash");
2477 bpm->installed = false;
2478
2479 /* unique2 should be updated too */
2480 bpm->unique2 = ++bgp_pbr_match_iptable_counter_unique;
2481 bpm->installed_in_iptable = false;
2482 bpm->install_in_progress = false;
2483 bpm->install_iptable_in_progress = false;
2484 }
2485
2486 memset(&temp2, 0, sizeof(temp2));
0e867886
PG
2487 if (bpf->src)
2488 prefix_copy(&temp2.src, bpf->src);
d114b0d7 2489 else
f01e580f 2490 temp2.src.family = bpf->family;
0e867886
PG
2491 if (bpf->dst)
2492 prefix_copy(&temp2.dst, bpf->dst);
d114b0d7 2493 else
f01e580f 2494 temp2.dst.family = bpf->family;
1de7dfff
PG
2495 temp2.src_port_min = src_port ? src_port->min_port : 0;
2496 temp2.dst_port_min = dst_port ? dst_port->min_port : 0;
2497 temp2.src_port_max = src_port ? src_port->max_port : 0;
2498 temp2.dst_port_max = dst_port ? dst_port->max_port : 0;
0e867886 2499 temp2.proto = bpf->protocol;
5a430eee
PG
2500 bpme = hash_get(bpm->entry_hash, &temp2,
2501 bgp_pbr_match_entry_alloc_intern);
2502 if (bpme->unique == 0) {
d114b0d7
PG
2503 bpme->unique = ++bgp_pbr_match_entry_counter_unique;
2504 /* 0 value is forbidden */
2505 bpme->backpointer = bpm;
2506 bpme->installed = false;
2507 bpme->install_in_progress = false;
b588b642 2508 /* link bgp info to bpme */
9b6d8fcf 2509 bpme->path = (void *)path;
c26edcda
PG
2510 } else
2511 bpme_found = true;
d114b0d7 2512
c26edcda 2513 /* already installed */
5a430eee 2514 if (bpme_found) {
18ee8310 2515 struct bgp_path_info_extra *extra =
9b6d8fcf 2516 bgp_path_info_extra_get(path);
c26edcda 2517
e0c7edb0
PG
2518 if (extra &&
2519 listnode_lookup_nocheck(extra->bgp_fs_pbr, bpme)) {
c26edcda 2520 if (BGP_DEBUG(pbr, PBR_ERROR))
9b6d8fcf
DS
2521 zlog_err(
2522 "%s: entry %p/%p already installed in bgp pbr",
2523 __func__, path, bpme);
c26edcda
PG
2524 return;
2525 }
2526 }
d114b0d7
PG
2527 /* BGP FS: append entry to zebra
2528 * - policies are not routing entries and as such
2529 * route replace semantics don't necessarily follow
2530 * through to policy entries
2531 * - because of that, not all policing information will be stored
2532 * into zebra. and non selected policies will be suppressed from zebra
2533 * - as consequence, in order to bring consistency
2534 * a policy will be added, then ifan ecmp policy exists,
2535 * it will be suppressed subsequently
2536 */
2537 /* ip rule add */
db1fcc98 2538 bgp_pbr_bpa_add(bpa);
d114b0d7
PG
2539
2540 /* ipset create */
6ea591c7 2541 if (!bpm->installed)
d114b0d7
PG
2542 bgp_send_pbr_ipset_match(bpm, true);
2543 /* ipset add */
6ea591c7 2544 if (!bpme->installed)
d114b0d7
PG
2545 bgp_send_pbr_ipset_entry_match(bpme, true);
2546
2547 /* iptables */
6ea591c7 2548 if (!bpm->installed_in_iptable)
d114b0d7
PG
2549 bgp_send_pbr_iptable(bpa, bpm, true);
2550
2551 /* A previous entry may already exist
2552 * flush previous entry if necessary
2553 */
2554 bpmer.bpme_to_match = bpme;
2555 bpmer.bpme_found = NULL;
2556 hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer);
2557 if (bpmer.bpme_found) {
2558 static struct bgp_pbr_match *local_bpm;
2559 static struct bgp_pbr_action *local_bpa;
2560
2561 local_bpm = bpmer.bpme_found->backpointer;
2562 local_bpa = local_bpm->action;
2563 bgp_pbr_flush_entry(bgp, local_bpa,
2564 local_bpm, bpmer.bpme_found);
2565 }
2566
2567
2568}
2569
4b7e6066 2570static void bgp_pbr_policyroute_add_to_zebra_recursive(
9b6d8fcf
DS
2571 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
2572 struct bgp_pbr_or_filter *bpof, struct nexthop *nh, float *rate,
2573 uint8_t type_entry)
56707a36
PG
2574{
2575 struct listnode *node, *nnode;
2576 struct bgp_pbr_val_mask *valmask;
2577 uint8_t next_type_entry;
2578 struct list *orig_list;
2579 struct bgp_pbr_val_mask **target_val;
2580
d90b788e
A
2581 if (type_entry == 0) {
2582 bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh, rate);
2583 return;
2584 }
e45aea59 2585 next_type_entry = bgp_pbr_next_type_entry(type_entry);
56707a36 2586 if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) {
56707a36
PG
2587 orig_list = bpof->tcpflags;
2588 target_val = &bpf->tcp_flags;
2589 } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) {
56707a36
PG
2590 orig_list = bpof->dscp;
2591 target_val = &bpf->dscp;
cfaf18ce 2592 } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) {
cfaf18ce
PG
2593 orig_list = bpof->pkt_len;
2594 target_val = &bpf->pkt_len_val;
6f5617d8 2595 } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) {
6f5617d8
PG
2596 orig_list = bpof->fragment;
2597 target_val = &bpf->fragment;
da3fa383
PG
2598 } else if (type_entry == FLOWSPEC_ICMP_TYPE &&
2599 (bpof->icmp_type || bpof->icmp_code)) {
2600 /* enumerate list for icmp - must be last one */
9b6d8fcf 2601 bgp_pbr_icmp_action(bgp, path, bpf, bpof, true, nh, rate);
da3fa383 2602 return;
56707a36 2603 } else {
d90b788e 2604 bgp_pbr_policyroute_add_to_zebra_recursive(
9b6d8fcf 2605 bgp, path, bpf, bpof, nh, rate, next_type_entry);
d90b788e 2606 return;
56707a36
PG
2607 }
2608 for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) {
2609 *target_val = valmask;
9b6d8fcf
DS
2610 bgp_pbr_policyroute_add_to_zebra_recursive(
2611 bgp, path, bpf, bpof, nh, rate, next_type_entry);
56707a36
PG
2612 }
2613}
2614
9bba1455 2615static void bgp_pbr_policyroute_add_to_zebra(struct bgp *bgp,
9b6d8fcf 2616 struct bgp_path_info *path,
4b7e6066
DS
2617 struct bgp_pbr_filter *bpf,
2618 struct bgp_pbr_or_filter *bpof,
2619 struct nexthop *nh, float *rate)
9bba1455 2620{
d90b788e
A
2621 if (!bpof) {
2622 bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh, rate);
2623 return;
2624 }
56707a36 2625 if (bpof->tcpflags)
9b6d8fcf
DS
2626 bgp_pbr_policyroute_add_to_zebra_recursive(
2627 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_TCP_FLAGS);
56707a36 2628 else if (bpof->dscp)
9b6d8fcf
DS
2629 bgp_pbr_policyroute_add_to_zebra_recursive(
2630 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_DSCP);
cfaf18ce 2631 else if (bpof->pkt_len)
9b6d8fcf
DS
2632 bgp_pbr_policyroute_add_to_zebra_recursive(
2633 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_PKT_LEN);
6f5617d8 2634 else if (bpof->fragment)
9b6d8fcf
DS
2635 bgp_pbr_policyroute_add_to_zebra_recursive(
2636 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_FRAGMENT);
da3fa383 2637 else if (bpof->icmp_type || bpof->icmp_code)
9b6d8fcf
DS
2638 bgp_pbr_policyroute_add_to_zebra_recursive(
2639 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_ICMP_TYPE);
56707a36 2640 else
9b6d8fcf 2641 bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh, rate);
56707a36
PG
2642 /* flush bpof */
2643 if (bpof->tcpflags)
2644 list_delete_all_node(bpof->tcpflags);
2645 if (bpof->dscp)
2646 list_delete_all_node(bpof->dscp);
cfaf18ce
PG
2647 if (bpof->pkt_len)
2648 list_delete_all_node(bpof->pkt_len);
6f5617d8
PG
2649 if (bpof->fragment)
2650 list_delete_all_node(bpof->fragment);
da3fa383
PG
2651 if (bpof->icmp_type)
2652 list_delete_all_node(bpof->icmp_type);
2653 if (bpof->icmp_code)
2654 list_delete_all_node(bpof->icmp_code);
932404b7
PG
2655}
2656
9b6d8fcf 2657static void bgp_pbr_handle_entry(struct bgp *bgp, struct bgp_path_info *path,
4b7e6066 2658 struct bgp_pbr_entry_main *api, bool add)
d114b0d7
PG
2659{
2660 struct nexthop nh;
2661 int i = 0;
2662 int continue_loop = 1;
2663 float rate = 0;
2664 struct prefix *src = NULL, *dst = NULL;
1de7dfff
PG
2665 uint8_t proto = 0;
2666 struct bgp_pbr_range_port *srcp = NULL, *dstp = NULL;
932404b7 2667 struct bgp_pbr_range_port range, range_icmp_code;
83360720 2668 struct bgp_pbr_range_port pkt_len;
0e867886 2669 struct bgp_pbr_filter bpf;
c5ee26cc
PG
2670 uint8_t kind_enum;
2671 struct bgp_pbr_or_filter bpof;
9bba1455 2672 struct bgp_pbr_val_mask bpvm;
d114b0d7 2673
645a1f1d 2674 memset(&range, 0, sizeof(range));
8cda9106 2675 memset(&nh, 0, sizeof(struct nexthop));
0e867886 2676 memset(&bpf, 0, sizeof(struct bgp_pbr_filter));
c5ee26cc 2677 memset(&bpof, 0, sizeof(struct bgp_pbr_or_filter));
9350f1df
PG
2678 if (api->match_bitmask & PREFIX_SRC_PRESENT ||
2679 (api->type == BGP_PBR_IPRULE &&
2680 api->match_bitmask_iprule & PREFIX_SRC_PRESENT))
d114b0d7 2681 src = &api->src_prefix;
9350f1df
PG
2682 if (api->match_bitmask & PREFIX_DST_PRESENT ||
2683 (api->type == BGP_PBR_IPRULE &&
2684 api->match_bitmask_iprule & PREFIX_DST_PRESENT))
d114b0d7 2685 dst = &api->dst_prefix;
9350f1df
PG
2686 if (api->type == BGP_PBR_IPRULE)
2687 bpf.type = api->type;
d114b0d7
PG
2688 memset(&nh, 0, sizeof(struct nexthop));
2689 nh.vrf_id = VRF_UNKNOWN;
f2ead0a5 2690 if (api->match_protocol_num) {
1de7dfff 2691 proto = (uint8_t)api->protocol[0].value;
f2ead0a5
PG
2692 if (api->afi == AF_INET6 && proto == IPPROTO_ICMPV6)
2693 proto = IPPROTO_ICMP;
2694 }
1de7dfff
PG
2695 /* if match_port is selected, then either src or dst port will be parsed
2696 * but not both at the same time
2697 */
2698 if (api->match_port_num >= 1) {
2699 bgp_pbr_extract(api->port,
2700 api->match_port_num,
2701 &range);
2702 srcp = dstp = &range;
2703 } else if (api->match_src_port_num >= 1) {
2704 bgp_pbr_extract(api->src_port,
2705 api->match_src_port_num,
2706 &range);
2707 srcp = &range;
2708 dstp = NULL;
2709 } else if (api->match_dst_port_num >= 1) {
2710 bgp_pbr_extract(api->dst_port,
2711 api->match_dst_port_num,
2712 &range);
2713 dstp = &range;
2714 srcp = NULL;
2715 }
932404b7
PG
2716 if (api->match_icmp_type_num >= 1) {
2717 proto = IPPROTO_ICMP;
da3fa383
PG
2718 if (bgp_pbr_extract(api->icmp_type,
2719 api->match_icmp_type_num,
2720 &range))
932404b7 2721 srcp = &range;
da3fa383
PG
2722 else {
2723 bpof.icmp_type = list_new();
2724 bgp_pbr_extract_enumerate(api->icmp_type,
2725 api->match_icmp_type_num,
2726 OPERATOR_UNARY_OR,
2727 bpof.icmp_type,
2728 FLOWSPEC_ICMP_TYPE);
932404b7
PG
2729 }
2730 }
2731 if (api->match_icmp_code_num >= 1) {
2732 proto = IPPROTO_ICMP;
da3fa383
PG
2733 if (bgp_pbr_extract(api->icmp_code,
2734 api->match_icmp_code_num,
2735 &range_icmp_code))
932404b7 2736 dstp = &range_icmp_code;
da3fa383
PG
2737 else {
2738 bpof.icmp_code = list_new();
2739 bgp_pbr_extract_enumerate(api->icmp_code,
2740 api->match_icmp_code_num,
2741 OPERATOR_UNARY_OR,
2742 bpof.icmp_code,
2743 FLOWSPEC_ICMP_CODE);
932404b7
PG
2744 }
2745 }
2da7d62e 2746
c5ee26cc
PG
2747 if (api->match_tcpflags_num) {
2748 kind_enum = bgp_pbr_match_val_get_operator(api->tcpflags,
2749 api->match_tcpflags_num);
2750 if (kind_enum == OPERATOR_UNARY_AND) {
9bba1455 2751 bpf.tcp_flags = &bpvm;
c5ee26cc
PG
2752 bgp_pbr_extract_enumerate(api->tcpflags,
2753 api->match_tcpflags_num,
35703998
PG
2754 OPERATOR_UNARY_AND,
2755 bpf.tcp_flags,
2756 FLOWSPEC_TCP_FLAGS);
c5ee26cc
PG
2757 } else if (kind_enum == OPERATOR_UNARY_OR) {
2758 bpof.tcpflags = list_new();
2759 bgp_pbr_extract_enumerate(api->tcpflags,
2760 api->match_tcpflags_num,
35703998
PG
2761 OPERATOR_UNARY_OR,
2762 bpof.tcpflags,
2763 FLOWSPEC_TCP_FLAGS);
c5ee26cc
PG
2764 }
2765 }
cfaf18ce
PG
2766 if (api->match_packet_length_num) {
2767 bool ret;
2768
2769 ret = bgp_pbr_extract(api->packet_length,
2770 api->match_packet_length_num,
2771 &pkt_len);
2772 if (ret)
2773 bpf.pkt_len = &pkt_len;
2774 else {
2775 bpof.pkt_len = list_new();
2776 bgp_pbr_extract_enumerate(api->packet_length,
2777 api->match_packet_length_num,
2778 OPERATOR_UNARY_OR,
2779 bpof.pkt_len,
2780 FLOWSPEC_PKT_LEN);
2781 }
2da7d62e 2782 }
4977bd6c 2783 if (api->match_dscp_num >= 1) {
35703998
PG
2784 bpof.dscp = list_new();
2785 bgp_pbr_extract_enumerate(api->dscp, api->match_dscp_num,
2786 OPERATOR_UNARY_OR,
2787 bpof.dscp, FLOWSPEC_DSCP);
4977bd6c 2788 }
6f5617d8
PG
2789 if (api->match_fragment_num) {
2790 bpof.fragment = list_new();
2791 bgp_pbr_extract_enumerate(api->fragment,
2792 api->match_fragment_num,
2793 OPERATOR_UNARY_OR,
2794 bpof.fragment,
2795 FLOWSPEC_FRAGMENT);
2796 }
0e867886
PG
2797 bpf.vrf_id = api->vrf_id;
2798 bpf.src = src;
2799 bpf.dst = dst;
2800 bpf.protocol = proto;
0e867886
PG
2801 bpf.src_port = srcp;
2802 bpf.dst_port = dstp;
f01e580f 2803 bpf.family = afi2family(api->afi);
d90b788e
A
2804 if (!add) {
2805 bgp_pbr_policyroute_remove_from_zebra(bgp, path, &bpf, &bpof);
2806 return;
2807 }
d114b0d7
PG
2808 /* no action for add = true */
2809 for (i = 0; i < api->action_num; i++) {
2810 switch (api->actions[i].action) {
2811 case ACTION_TRAFFICRATE:
2812 /* drop packet */
2813 if (api->actions[i].u.r.rate == 0) {
2814 nh.vrf_id = api->vrf_id;
2815 nh.type = NEXTHOP_TYPE_BLACKHOLE;
9b6d8fcf
DS
2816 bgp_pbr_policyroute_add_to_zebra(
2817 bgp, path, &bpf, &bpof, &nh, &rate);
d114b0d7
PG
2818 } else {
2819 /* update rate. can be reentrant */
2820 rate = api->actions[i].u.r.rate;
ac7c35f8 2821 if (BGP_DEBUG(pbr, PBR)) {
d114b0d7 2822 bgp_pbr_print_policy_route(api);
ac7c35f8
PG
2823 zlog_warn("PBR: ignoring Set action rate %f",
2824 api->actions[i].u.r.rate);
2825 }
d114b0d7
PG
2826 }
2827 break;
2828 case ACTION_TRAFFIC_ACTION:
2829 if (api->actions[i].u.za.filter
2830 & TRAFFIC_ACTION_SAMPLE) {
ac7c35f8 2831 if (BGP_DEBUG(pbr, PBR)) {
d114b0d7 2832 bgp_pbr_print_policy_route(api);
ac7c35f8
PG
2833 zlog_warn("PBR: Sample action Ignored");
2834 }
d114b0d7 2835 }
d114b0d7
PG
2836 /* terminate action: run other filters
2837 */
2838 break;
2839 case ACTION_REDIRECT_IP:
d114b0d7 2840 nh.vrf_id = api->vrf_id;
f01e580f
PG
2841 if (api->afi == AFI_IP) {
2842 nh.type = NEXTHOP_TYPE_IPV4;
2843 nh.gate.ipv4.s_addr =
2844 api->actions[i].u.zr.
2845 redirect_ip_v4.s_addr;
2846 } else {
2847 nh.type = NEXTHOP_TYPE_IPV6;
2848 memcpy(&nh.gate.ipv6,
2849 &api->actions[i].u.zr.redirect_ip_v6,
2850 sizeof(struct in6_addr));
2851 }
9b6d8fcf 2852 bgp_pbr_policyroute_add_to_zebra(bgp, path, &bpf, &bpof,
da3fa383 2853 &nh, &rate);
d114b0d7
PG
2854 /* XXX combination with REDIRECT_VRF
2855 * + REDIRECT_NH_IP not done
2856 */
2857 continue_loop = 0;
2858 break;
2859 case ACTION_REDIRECT:
9a659715
PG
2860 if (api->afi == AFI_IP)
2861 nh.type = NEXTHOP_TYPE_IPV4;
2862 else
2863 nh.type = NEXTHOP_TYPE_IPV6;
d114b0d7 2864 nh.vrf_id = api->actions[i].u.redirect_vrf;
9b6d8fcf 2865 bgp_pbr_policyroute_add_to_zebra(bgp, path, &bpf, &bpof,
da3fa383 2866 &nh, &rate);
d114b0d7
PG
2867 continue_loop = 0;
2868 break;
2869 case ACTION_MARKING:
ac7c35f8 2870 if (BGP_DEBUG(pbr, PBR)) {
d114b0d7 2871 bgp_pbr_print_policy_route(api);
40881800 2872 zlog_warn("PBR: Set DSCP/FlowLabel %u Ignored",
ac7c35f8
PG
2873 api->actions[i].u.marking_dscp);
2874 }
d114b0d7
PG
2875 break;
2876 default:
2877 break;
2878 }
2879 if (continue_loop == 0)
2880 break;
2881 }
2882}
2883
5a1ae2c2 2884void bgp_pbr_update_entry(struct bgp *bgp, const struct prefix *p,
4b7e6066
DS
2885 struct bgp_path_info *info, afi_t afi, safi_t safi,
2886 bool nlri_update)
45918cfb
PG
2887{
2888 struct bgp_pbr_entry_main api;
2889
45918cfb
PG
2890 if (safi != SAFI_FLOWSPEC)
2891 return; /* not supported */
2892 /* Make Zebra API structure. */
2893 memset(&api, 0, sizeof(api));
2894 api.vrf_id = bgp->vrf_id;
2895 api.afi = afi;
2896
6818e7e5 2897 if (!bgp_zebra_tm_chunk_obtained()) {
f146bb54 2898 if (BGP_DEBUG(pbr, PBR_ERROR))
e50f7cfd 2899 flog_err(EC_BGP_TABLE_CHUNK,
1c50c1c0 2900 "%s: table chunk not obtained yet", __func__);
6818e7e5
PG
2901 return;
2902 }
6818e7e5
PG
2903
2904 if (bgp_pbr_build_and_validate_entry(p, info, &api) < 0) {
2905 if (BGP_DEBUG(pbr, PBR_ERROR))
e50f7cfd 2906 flog_err(EC_BGP_FLOWSPEC_INSTALLATION,
1c50c1c0
QY
2907 "%s: cancel updating entry %p in bgp pbr",
2908 __func__, info);
45918cfb
PG
2909 return;
2910 }
d114b0d7 2911 bgp_pbr_handle_entry(bgp, info, &api, nlri_update);
45918cfb 2912}
4762c213
PG
2913
2914int bgp_pbr_interface_compare(const struct bgp_pbr_interface *a,
2915 const struct bgp_pbr_interface *b)
2916{
2917 return strcmp(a->name, b->name);
2918}
2919
2920struct bgp_pbr_interface *bgp_pbr_interface_lookup(const char *name,
2921 struct bgp_pbr_interface_head *head)
2922{
2923 struct bgp_pbr_interface pbr_if;
2924
2925 strlcpy(pbr_if.name, name, sizeof(pbr_if.name));
2926 return (RB_FIND(bgp_pbr_interface_head,
2927 head, &pbr_if));
2928}
2929
2930/* this function resets to the default policy routing
2931 * go back to default status
2932 */
2933void bgp_pbr_reset(struct bgp *bgp, afi_t afi)
2934{
2935 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
2936 struct bgp_pbr_interface_head *head;
2937 struct bgp_pbr_interface *pbr_if;
2938
8f242187 2939 if (!bgp_pbr_cfg)
4762c213 2940 return;
8f242187
PG
2941 if (afi == AFI_IP)
2942 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
2943 else
2944 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
4762c213
PG
2945 while (!RB_EMPTY(bgp_pbr_interface_head, head)) {
2946 pbr_if = RB_ROOT(bgp_pbr_interface_head, head);
2947 RB_REMOVE(bgp_pbr_interface_head, head, pbr_if);
2948 XFREE(MTYPE_TMP, pbr_if);
2949 }
2950}