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