]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_pbr.c
bgpd: display the list of iprules attached to a fs entry
[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
75static int bgp_pbr_rule_walkcb(struct hash_backet *backet, void *arg)
76{
77 struct bgp_pbr_rule *bpr = (struct bgp_pbr_rule *)backet->data;
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
70eabd12
PG
89static int bgp_pbr_action_walkcb(struct hash_backet *backet, void *arg)
90{
91 struct bgp_pbr_action *bpa = (struct bgp_pbr_action *)backet->data;
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
c5d429e1
PG
103static int bgp_pbr_match_entry_walkcb(struct hash_backet *backet, void *arg)
104{
105 struct bgp_pbr_match_entry *bpme =
106 (struct bgp_pbr_match_entry *)backet->data;
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
123static int bgp_pbr_match_pername_walkcb(struct hash_backet *backet, void *arg)
124{
125 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data;
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
1815c6fc
PG
138static int bgp_pbr_match_iptable_walkcb(struct hash_backet *backet, void *arg)
139{
140 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data;
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
157static int bgp_pbr_match_walkcb(struct hash_backet *backet, void *arg)
158{
159 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data;
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);
336 if (ret == false)
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 }
444 if (exact_match == true && i > 0)
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 &&
548 enumerate_icmp == false) {
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;
45918cfb
PG
701
702 /* extract match from flowspec entries */
703 ret = bgp_flowspec_match_rules_fill((uint8_t *)p->u.prefix_flowspec.ptr,
704 p->u.prefix_flowspec.prefixlen, api);
705 if (ret < 0)
706 return -1;
707 /* extract actiosn from flowspec ecom list */
40381db7
DS
708 if (path && path->attr && path->attr->ecommunity) {
709 ecom = path->attr->ecommunity;
45918cfb
PG
710 for (i = 0; i < ecom->size; i++) {
711 ecom_eval = (struct ecommunity_val *)
149d272b
PG
712 (ecom->val + (i * ECOMMUNITY_SIZE));
713 action_count++;
45918cfb 714 if (action_count > ACTIONS_MAX_NUM) {
f146bb54 715 if (BGP_DEBUG(pbr, PBR_ERROR))
1c50c1c0
QY
716 flog_err(
717 EC_BGP_FLOWSPEC_PACKET,
718 "%s: flowspec actions exceeds limit (max %u)",
719 __func__, action_count);
45918cfb
PG
720 break;
721 }
149d272b 722 api_action = &api->actions[action_count - 1];
45918cfb
PG
723
724 if ((ecom_eval->val[1] ==
725 (char)ECOMMUNITY_REDIRECT_VRF) &&
726 (ecom_eval->val[0] ==
727 (char)ECOMMUNITY_ENCODE_TRANS_EXP ||
728 ecom_eval->val[0] ==
729 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 ||
730 ecom_eval->val[0] ==
731 (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_3)) {
732 struct ecommunity *eckey = ecommunity_new();
733 struct ecommunity_val ecom_copy;
734
735 memcpy(&ecom_copy, ecom_eval,
736 sizeof(struct ecommunity_val));
737 ecom_copy.val[0] &=
738 ~ECOMMUNITY_ENCODE_TRANS_EXP;
739 ecom_copy.val[1] = ECOMMUNITY_ROUTE_TARGET;
740 ecommunity_add_val(eckey, &ecom_copy);
741
742 api_action->action = ACTION_REDIRECT;
743 api_action->u.redirect_vrf =
744 get_first_vrf_for_redirect_with_rt(
745 eckey);
746 ecommunity_free(&eckey);
747 } else if ((ecom_eval->val[0] ==
748 (char)ECOMMUNITY_ENCODE_REDIRECT_IP_NH) &&
749 (ecom_eval->val[1] ==
750 (char)ECOMMUNITY_REDIRECT_IP_NH)) {
2551b26e
PG
751 /* in case the 2 ecom present,
752 * do not overwrite
753 * draft-ietf-idr-flowspec-redirect
754 */
755 if (api_action_redirect_ip) {
756 if (api_action_redirect_ip->u
757 .zr.redirect_ip_v4.s_addr)
758 continue;
759 if (!path->attr->nexthop.s_addr)
760 continue;
761 api_action_redirect_ip->u
762 .zr.redirect_ip_v4.s_addr =
763 path->attr->nexthop.s_addr;
764 api_action_redirect_ip->u.zr.duplicate
765 = ecom_eval->val[7];
766 continue;
767 } else {
768 api_action->action = ACTION_REDIRECT_IP;
769 api_action->u.zr.redirect_ip_v4.s_addr =
770 path->attr->nexthop.s_addr;
771 api_action->u.zr.duplicate =
772 ecom_eval->val[7];
773 api_action_redirect_ip = api_action;
774 }
775 } else if ((ecom_eval->val[0] ==
776 (char)ECOMMUNITY_ENCODE_IP) &&
777 (ecom_eval->val[1] ==
778 (char)ECOMMUNITY_FLOWSPEC_REDIRECT_IPV4)) {
779 /* in case the 2 ecom present,
780 * overwrite simpson draft
781 * update redirect ip fields
782 */
783 if (api_action_redirect_ip) {
784 memcpy(&(api_action_redirect_ip->u
785 .zr.redirect_ip_v4.s_addr),
786 (ecom_eval->val+2), 4);
787 api_action_redirect_ip->u
788 .zr.duplicate =
789 ecom_eval->val[7];
790 continue;
791 } else {
792 api_action->action = ACTION_REDIRECT_IP;
793 memcpy(&(api_action->u
794 .zr.redirect_ip_v4.s_addr),
795 (ecom_eval->val+2), 4);
796 api_action->u.zr.duplicate =
797 ecom_eval->val[7];
798 api_action_redirect_ip = api_action;
799 }
45918cfb
PG
800 } else {
801 if (ecom_eval->val[0] !=
802 (char)ECOMMUNITY_ENCODE_TRANS_EXP)
803 continue;
804 ret = ecommunity_fill_pbr_action(ecom_eval,
805 api_action);
806 if (ret != 0)
807 continue;
808 }
809 api->action_num++;
810 }
811 }
812
813 /* validate if incoming matc/action is compatible
814 * with our policy routing engine
815 */
816 if (!bgp_pbr_validate_policy_route(api))
817 return -1;
818
819 /* check inconsistency in the match rule */
820 if (api->match_bitmask & PREFIX_SRC_PRESENT) {
821 src = &api->src_prefix;
822 afi = family2afi(src->family);
823 valid_prefix = 1;
824 }
825 if (api->match_bitmask & PREFIX_DST_PRESENT) {
826 dst = &api->dst_prefix;
827 if (valid_prefix && afi != family2afi(dst->family)) {
ac7c35f8 828 if (BGP_DEBUG(pbr, PBR)) {
45918cfb 829 bgp_pbr_print_policy_route(api);
ac7c35f8
PG
830 zlog_debug("%s: inconsistency:"
831 " no match for afi src and dst (%u/%u)",
832 __func__, afi, family2afi(dst->family));
833 }
45918cfb
PG
834 return -1;
835 }
836 }
837 return 0;
838}
839
a6b07429
PG
840static void bgp_pbr_match_entry_free(void *arg)
841{
842 struct bgp_pbr_match_entry *bpme;
843
844 bpme = (struct bgp_pbr_match_entry *)arg;
845
846 if (bpme->installed) {
847 bgp_send_pbr_ipset_entry_match(bpme, false);
848 bpme->installed = false;
849 bpme->backpointer = NULL;
850 }
851 XFREE(MTYPE_PBR_MATCH_ENTRY, bpme);
852}
853
854static void bgp_pbr_match_free(void *arg)
855{
856 struct bgp_pbr_match *bpm;
857
858 bpm = (struct bgp_pbr_match *)arg;
859
860 hash_clean(bpm->entry_hash, bgp_pbr_match_entry_free);
861
862 if (hashcount(bpm->entry_hash) == 0) {
863 /* delete iptable entry first */
864 /* then delete ipset match */
865 if (bpm->installed) {
866 if (bpm->installed_in_iptable) {
867 bgp_send_pbr_iptable(bpm->action,
868 bpm, false);
869 bpm->installed_in_iptable = false;
870 bpm->action->refcnt--;
871 }
872 bgp_send_pbr_ipset_match(bpm, false);
873 bpm->installed = false;
874 bpm->action = NULL;
875 }
876 }
877 hash_free(bpm->entry_hash);
878
879 XFREE(MTYPE_PBR_MATCH, bpm);
880}
881
d114b0d7
PG
882static void *bgp_pbr_match_alloc_intern(void *arg)
883{
884 struct bgp_pbr_match *bpm, *new;
885
886 bpm = (struct bgp_pbr_match *)arg;
887
888 new = XCALLOC(MTYPE_PBR_MATCH, sizeof(*new));
889 memcpy(new, bpm, sizeof(*bpm));
890
891 return new;
892}
893
27e376d4
PG
894static void bgp_pbr_rule_free(void *arg)
895{
896 struct bgp_pbr_rule *bpr;
897
898 bpr = (struct bgp_pbr_rule *)arg;
899
900 /* delete iprule */
901 if (bpr->installed) {
902 bgp_send_pbr_rule_action(bpr->action, bpr, false);
903 bpr->installed = false;
904 bpr->action->refcnt--;
905 bpr->action = NULL;
906 }
907 XFREE(MTYPE_PBR_RULE, bpr);
908}
909
910static void *bgp_pbr_rule_alloc_intern(void *arg)
911{
912 struct bgp_pbr_rule *bpr, *new;
913
914 bpr = (struct bgp_pbr_rule *)arg;
915
916 new = XCALLOC(MTYPE_PBR_RULE, sizeof(*new));
917 memcpy(new, bpr, sizeof(*bpr));
918
919 return new;
920}
921
a6b07429
PG
922static void bgp_pbr_action_free(void *arg)
923{
924 struct bgp_pbr_action *bpa;
925
926 bpa = (struct bgp_pbr_action *)arg;
927
928 if (bpa->refcnt == 0) {
929 if (bpa->installed && bpa->table_id != 0) {
6cfe5d15 930 bgp_send_pbr_rule_action(bpa, NULL, false);
a6b07429
PG
931 bgp_zebra_announce_default(bpa->bgp, &(bpa->nh),
932 AFI_IP,
933 bpa->table_id,
934 false);
6ee20355 935 bpa->installed = false;
a6b07429
PG
936 }
937 }
938 XFREE(MTYPE_PBR_ACTION, bpa);
939}
940
d114b0d7
PG
941static void *bgp_pbr_action_alloc_intern(void *arg)
942{
943 struct bgp_pbr_action *bpa, *new;
944
945 bpa = (struct bgp_pbr_action *)arg;
946
947 new = XCALLOC(MTYPE_PBR_ACTION, sizeof(*new));
948
949 memcpy(new, bpa, sizeof(*bpa));
950
951 return new;
952}
953
954static void *bgp_pbr_match_entry_alloc_intern(void *arg)
955{
956 struct bgp_pbr_match_entry *bpme, *new;
957
958 bpme = (struct bgp_pbr_match_entry *)arg;
959
960 new = XCALLOC(MTYPE_PBR_MATCH_ENTRY, sizeof(*new));
961
962 memcpy(new, bpme, sizeof(*bpme));
963
964 return new;
965}
966
f3d32faa
PG
967uint32_t bgp_pbr_match_hash_key(void *arg)
968{
969 struct bgp_pbr_match *pbm = (struct bgp_pbr_match *)arg;
970 uint32_t key;
971
972 key = jhash_1word(pbm->vrf_id, 0x4312abde);
973 key = jhash_1word(pbm->flags, key);
9717d3e5
PG
974 key = jhash(&pbm->pkt_len_min, 2, key);
975 key = jhash(&pbm->pkt_len_max, 2, key);
976 key = jhash(&pbm->tcp_flags, 2, key);
977 key = jhash(&pbm->tcp_mask_flags, 2, key);
978 key = jhash(&pbm->dscp_value, 1, key);
979 key = jhash(&pbm->fragment, 1, key);
f3d32faa
PG
980 return jhash_1word(pbm->type, key);
981}
982
74df8d6d 983bool bgp_pbr_match_hash_equal(const void *arg1, const void *arg2)
f3d32faa
PG
984{
985 const struct bgp_pbr_match *r1, *r2;
986
987 r1 = (const struct bgp_pbr_match *)arg1;
988 r2 = (const struct bgp_pbr_match *)arg2;
989
990 if (r1->vrf_id != r2->vrf_id)
74df8d6d 991 return false;
f3d32faa
PG
992
993 if (r1->type != r2->type)
74df8d6d 994 return false;
f3d32faa
PG
995
996 if (r1->flags != r2->flags)
74df8d6d 997 return false;
f3d32faa
PG
998
999 if (r1->action != r2->action)
74df8d6d 1000 return false;
f3d32faa 1001
83360720 1002 if (r1->pkt_len_min != r2->pkt_len_min)
74df8d6d 1003 return false;
83360720
PG
1004
1005 if (r1->pkt_len_max != r2->pkt_len_max)
74df8d6d 1006 return false;
83360720 1007
2da7d62e 1008 if (r1->tcp_flags != r2->tcp_flags)
74df8d6d 1009 return false;
2da7d62e
PG
1010
1011 if (r1->tcp_mask_flags != r2->tcp_mask_flags)
74df8d6d 1012 return false;
2da7d62e 1013
4977bd6c 1014 if (r1->dscp_value != r2->dscp_value)
74df8d6d 1015 return false;
6f5617d8
PG
1016
1017 if (r1->fragment != r2->fragment)
74df8d6d
DS
1018 return false;
1019 return true;
f3d32faa
PG
1020}
1021
27e376d4
PG
1022uint32_t bgp_pbr_rule_hash_key(void *arg)
1023{
1024 struct bgp_pbr_rule *pbr = (struct bgp_pbr_rule *)arg;
1025 uint32_t key;
1026
1027 key = prefix_hash_key(&pbr->src);
1028 key = jhash_1word(pbr->vrf_id, key);
1029 key = jhash_1word(pbr->flags, key);
1030 return jhash_1word(prefix_hash_key(&pbr->dst), key);
1031}
1032
1033bool bgp_pbr_rule_hash_equal(const void *arg1, const void *arg2)
1034{
1035 const struct bgp_pbr_rule *r1, *r2;
1036
1037 r1 = (const struct bgp_pbr_rule *)arg1;
1038 r2 = (const struct bgp_pbr_rule *)arg2;
1039
1040 if (r1->vrf_id != r2->vrf_id)
1041 return false;
1042
1043 if (r1->flags != r2->flags)
1044 return false;
1045
1046 if (r1->action != r2->action)
1047 return false;
1048
1049 if ((r1->flags & MATCH_IP_SRC_SET) &&
1050 !prefix_same(&r1->src, &r2->src))
1051 return false;
1052
1053 if ((r1->flags & MATCH_IP_DST_SET) &&
1054 !prefix_same(&r1->dst, &r2->dst))
1055 return false;
1056
1057 return true;
1058}
1059
f3d32faa
PG
1060uint32_t bgp_pbr_match_entry_hash_key(void *arg)
1061{
1062 struct bgp_pbr_match_entry *pbme;
1063 uint32_t key;
1064
1065 pbme = (struct bgp_pbr_match_entry *)arg;
1066 key = prefix_hash_key(&pbme->src);
1067 key = jhash_1word(prefix_hash_key(&pbme->dst), key);
1de7dfff
PG
1068 key = jhash(&pbme->dst_port_min, 2, key);
1069 key = jhash(&pbme->src_port_min, 2, key);
1070 key = jhash(&pbme->dst_port_max, 2, key);
1071 key = jhash(&pbme->src_port_max, 2, key);
1072 key = jhash(&pbme->proto, 1, key);
f3d32faa
PG
1073
1074 return key;
1075}
1076
74df8d6d 1077bool bgp_pbr_match_entry_hash_equal(const void *arg1, const void *arg2)
f3d32faa
PG
1078{
1079 const struct bgp_pbr_match_entry *r1, *r2;
1080
1081 r1 = (const struct bgp_pbr_match_entry *)arg1;
1082 r2 = (const struct bgp_pbr_match_entry *)arg2;
1083
74df8d6d
DS
1084 /*
1085 * on updates, comparing backpointer is not necessary
1086 * unique value is self calculated
1087 * rate is ignored for now
f3d32faa
PG
1088 */
1089
1090 if (!prefix_same(&r1->src, &r2->src))
74df8d6d 1091 return false;
f3d32faa
PG
1092
1093 if (!prefix_same(&r1->dst, &r2->dst))
74df8d6d 1094 return false;
f3d32faa 1095
1de7dfff 1096 if (r1->src_port_min != r2->src_port_min)
74df8d6d 1097 return false;
1de7dfff
PG
1098
1099 if (r1->dst_port_min != r2->dst_port_min)
74df8d6d 1100 return false;
1de7dfff
PG
1101
1102 if (r1->src_port_max != r2->src_port_max)
74df8d6d 1103 return false;
1de7dfff
PG
1104
1105 if (r1->dst_port_max != r2->dst_port_max)
74df8d6d 1106 return false;
1de7dfff
PG
1107
1108 if (r1->proto != r2->proto)
74df8d6d 1109 return false;
1de7dfff 1110
74df8d6d 1111 return true;
f3d32faa
PG
1112}
1113
1114uint32_t bgp_pbr_action_hash_key(void *arg)
1115{
1116 struct bgp_pbr_action *pbra;
1117 uint32_t key;
1118
1119 pbra = (struct bgp_pbr_action *)arg;
1120 key = jhash_1word(pbra->table_id, 0x4312abde);
1121 key = jhash_1word(pbra->fwmark, key);
1122 return key;
1123}
1124
74df8d6d 1125bool bgp_pbr_action_hash_equal(const void *arg1, const void *arg2)
f3d32faa
PG
1126{
1127 const struct bgp_pbr_action *r1, *r2;
1128
1129 r1 = (const struct bgp_pbr_action *)arg1;
1130 r2 = (const struct bgp_pbr_action *)arg2;
1131
1132 /* unique value is self calculated
1133 * table and fwmark is self calculated
e414819e 1134 * rate is ignored
f3d32faa 1135 */
f3d32faa 1136 if (r1->vrf_id != r2->vrf_id)
74df8d6d 1137 return false;
f3d32faa
PG
1138
1139 if (memcmp(&r1->nh, &r2->nh, sizeof(struct nexthop)))
74df8d6d
DS
1140 return false;
1141
1142 return true;
f3d32faa 1143}
bbe6ffd6 1144
ffee150e
PG
1145struct bgp_pbr_rule *bgp_pbr_rule_lookup(vrf_id_t vrf_id,
1146 uint32_t unique)
1147{
1148 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1149 struct bgp_pbr_rule_unique bpru;
1150
1151 if (!bgp || unique == 0)
1152 return NULL;
1153 bpru.unique = unique;
1154 bpru.bpr_found = NULL;
1155 hash_walk(bgp->pbr_rule_hash, bgp_pbr_rule_walkcb, &bpru);
1156 return bpru.bpr_found;
1157}
1158
70eabd12
PG
1159struct bgp_pbr_action *bgp_pbr_action_rule_lookup(vrf_id_t vrf_id,
1160 uint32_t unique)
bbe6ffd6 1161{
70eabd12
PG
1162 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1163 struct bgp_pbr_action_unique bpau;
1164
1165 if (!bgp || unique == 0)
1166 return NULL;
1167 bpau.unique = unique;
1168 bpau.bpa_found = NULL;
1169 hash_walk(bgp->pbr_action_hash, bgp_pbr_action_walkcb, &bpau);
1170 return bpau.bpa_found;
bbe6ffd6
PG
1171}
1172
1173struct bgp_pbr_match *bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id,
1174 uint32_t unique)
1175{
c5d429e1
PG
1176 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1177 struct bgp_pbr_match_unique bpmu;
1178
1179 if (!bgp || unique == 0)
1180 return NULL;
1181 bpmu.unique = unique;
1182 bpmu.bpm_found = NULL;
1183 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_walkcb, &bpmu);
1184 return bpmu.bpm_found;
bbe6ffd6
PG
1185}
1186
1187struct bgp_pbr_match_entry *bgp_pbr_match_ipset_entry_lookup(vrf_id_t vrf_id,
1188 char *ipset_name,
1189 uint32_t unique)
1190{
c5d429e1
PG
1191 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1192 struct bgp_pbr_match_entry_unique bpmeu;
1193 struct bgp_pbr_match_ipsetname bpmi;
1194
1195 if (!bgp || unique == 0)
1196 return NULL;
1197 bpmi.ipsetname = XCALLOC(MTYPE_TMP, ZEBRA_IPSET_NAME_SIZE);
1198 snprintf(bpmi.ipsetname, ZEBRA_IPSET_NAME_SIZE, "%s", ipset_name);
1199 bpmi.bpm_found = NULL;
1200 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_pername_walkcb, &bpmi);
1201 XFREE(MTYPE_TMP, bpmi.ipsetname);
1202 if (!bpmi.bpm_found)
1203 return NULL;
1204 bpmeu.bpme_found = NULL;
1205 bpmeu.unique = unique;
1206 hash_walk(bpmi.bpm_found->entry_hash,
1207 bgp_pbr_match_entry_walkcb, &bpmeu);
1208 return bpmeu.bpme_found;
bbe6ffd6
PG
1209}
1210
1815c6fc
PG
1211struct bgp_pbr_match *bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id,
1212 uint32_t unique)
1213{
1214 struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id);
1215 struct bgp_pbr_match_iptable_unique bpmiu;
1216
1217 if (!bgp || unique == 0)
1218 return NULL;
1219 bpmiu.unique = unique;
1220 bpmiu.bpm_found = NULL;
1221 hash_walk(bgp->pbr_match_hash, bgp_pbr_match_iptable_walkcb, &bpmiu);
1222 return bpmiu.bpm_found;
1223}
1224
a6b07429
PG
1225void bgp_pbr_cleanup(struct bgp *bgp)
1226{
1227 if (bgp->pbr_match_hash) {
1228 hash_clean(bgp->pbr_match_hash, bgp_pbr_match_free);
1229 hash_free(bgp->pbr_match_hash);
1230 bgp->pbr_match_hash = NULL;
1231 }
27e376d4
PG
1232 if (bgp->pbr_rule_hash) {
1233 hash_clean(bgp->pbr_rule_hash, bgp_pbr_rule_free);
1234 hash_free(bgp->pbr_rule_hash);
1235 bgp->pbr_rule_hash = NULL;
1236 }
a6b07429
PG
1237 if (bgp->pbr_action_hash) {
1238 hash_clean(bgp->pbr_action_hash, bgp_pbr_action_free);
1239 hash_free(bgp->pbr_action_hash);
1240 bgp->pbr_action_hash = NULL;
1241 }
4762c213
PG
1242 if (bgp->bgp_pbr_cfg == NULL)
1243 return;
1244 bgp_pbr_reset(bgp, AFI_IP);
1245 XFREE(MTYPE_PBR, bgp->bgp_pbr_cfg);
1246 bgp->bgp_pbr_cfg = NULL;
a6b07429
PG
1247}
1248
f3d32faa
PG
1249void bgp_pbr_init(struct bgp *bgp)
1250{
1251 bgp->pbr_match_hash =
1252 hash_create_size(8, bgp_pbr_match_hash_key,
1253 bgp_pbr_match_hash_equal,
1254 "Match Hash");
1255 bgp->pbr_action_hash =
1256 hash_create_size(8, bgp_pbr_action_hash_key,
1257 bgp_pbr_action_hash_equal,
1258 "Match Hash Entry");
4762c213 1259
27e376d4
PG
1260 bgp->pbr_rule_hash =
1261 hash_create_size(8, bgp_pbr_rule_hash_key,
1262 bgp_pbr_rule_hash_equal,
1263 "Match Rule");
1264
4762c213
PG
1265 bgp->bgp_pbr_cfg = XCALLOC(MTYPE_PBR, sizeof(struct bgp_pbr_config));
1266 bgp->bgp_pbr_cfg->pbr_interface_any_ipv4 = true;
f3d32faa 1267}
b46b6f1a
PG
1268
1269void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main *api)
1270{
1271 int i = 0;
1272 char return_string[512];
1273 char *ptr = return_string;
1274 char buff[64];
1275 int nb_items = 0;
1276
1277 ptr += sprintf(ptr, "MATCH : ");
1278 if (api->match_bitmask & PREFIX_SRC_PRESENT) {
1279 struct prefix *p = &(api->src_prefix);
1280
1281 ptr += sprintf(ptr, "@src %s", prefix2str(p, buff, 64));
1282 INCREMENT_DISPLAY(ptr, nb_items);
1283 }
1284 if (api->match_bitmask & PREFIX_DST_PRESENT) {
1285 struct prefix *p = &(api->dst_prefix);
1286
1287 INCREMENT_DISPLAY(ptr, nb_items);
1288 ptr += sprintf(ptr, "@dst %s", prefix2str(p, buff, 64));
1289 }
1290
1291 if (api->match_protocol_num)
1292 INCREMENT_DISPLAY(ptr, nb_items);
1293 for (i = 0; i < api->match_protocol_num; i++)
1294 ptr += sprintf_bgp_pbr_match_val(ptr, &api->protocol[i],
1295 i > 0 ? NULL : "@proto ");
1296
1297 if (api->match_src_port_num)
1298 INCREMENT_DISPLAY(ptr, nb_items);
1299 for (i = 0; i < api->match_src_port_num; i++)
1300 ptr += sprintf_bgp_pbr_match_val(ptr, &api->src_port[i],
1301 i > 0 ? NULL : "@srcport ");
1302
1303 if (api->match_dst_port_num)
1304 INCREMENT_DISPLAY(ptr, nb_items);
1305 for (i = 0; i < api->match_dst_port_num; i++)
1306 ptr += sprintf_bgp_pbr_match_val(ptr, &api->dst_port[i],
1307 i > 0 ? NULL : "@dstport ");
1308
1309 if (api->match_port_num)
1310 INCREMENT_DISPLAY(ptr, nb_items);
1311 for (i = 0; i < api->match_port_num; i++)
1312 ptr += sprintf_bgp_pbr_match_val(ptr, &api->port[i],
1313 i > 0 ? NULL : "@port ");
1314
1315 if (api->match_icmp_type_num)
1316 INCREMENT_DISPLAY(ptr, nb_items);
1317 for (i = 0; i < api->match_icmp_type_num; i++)
1318 ptr += sprintf_bgp_pbr_match_val(ptr, &api->icmp_type[i],
1319 i > 0 ? NULL : "@icmptype ");
1320
1321 if (api->match_icmp_code_num)
1322 INCREMENT_DISPLAY(ptr, nb_items);
1323 for (i = 0; i < api->match_icmp_code_num; i++)
1324 ptr += sprintf_bgp_pbr_match_val(ptr, &api->icmp_code[i],
1325 i > 0 ? NULL : "@icmpcode ");
1326
1327 if (api->match_packet_length_num)
1328 INCREMENT_DISPLAY(ptr, nb_items);
1329 for (i = 0; i < api->match_packet_length_num; i++)
1330 ptr += sprintf_bgp_pbr_match_val(ptr, &api->packet_length[i],
1331 i > 0 ? NULL : "@plen ");
1332
1333 if (api->match_dscp_num)
1334 INCREMENT_DISPLAY(ptr, nb_items);
1335 for (i = 0; i < api->match_dscp_num; i++)
1336 ptr += sprintf_bgp_pbr_match_val(ptr, &api->dscp[i],
1337 i > 0 ? NULL : "@dscp ");
1338
1339 if (api->match_tcpflags_num)
1340 INCREMENT_DISPLAY(ptr, nb_items);
1341 for (i = 0; i < api->match_tcpflags_num; i++)
1342 ptr += sprintf_bgp_pbr_match_val(ptr, &api->tcpflags[i],
1343 i > 0 ? NULL : "@tcpflags ");
1344
588ec356 1345 if (api->match_fragment_num)
b46b6f1a 1346 INCREMENT_DISPLAY(ptr, nb_items);
588ec356
PG
1347 for (i = 0; i < api->match_fragment_num; i++)
1348 ptr += sprintf_bgp_pbr_match_val(ptr, &api->fragment[i],
1349 i > 0 ? NULL : "@fragment ");
b46b6f1a
PG
1350 if (!nb_items)
1351 ptr = return_string;
1352 else
1353 ptr += sprintf(ptr, "; ");
1354 if (api->action_num)
1355 ptr += sprintf(ptr, "SET : ");
1356 nb_items = 0;
1357 for (i = 0; i < api->action_num; i++) {
1358 switch (api->actions[i].action) {
1359 case ACTION_TRAFFICRATE:
1360 INCREMENT_DISPLAY(ptr, nb_items);
1361 ptr += sprintf(ptr, "@set rate %f",
1362 api->actions[i].u.r.rate);
1363 break;
1364 case ACTION_TRAFFIC_ACTION:
1365 INCREMENT_DISPLAY(ptr, nb_items);
1366 ptr += sprintf(ptr, "@action ");
1367 if (api->actions[i].u.za.filter
1368 & TRAFFIC_ACTION_TERMINATE)
1369 ptr += sprintf(ptr,
1370 " terminate (apply filter(s))");
1371 if (api->actions[i].u.za.filter
1372 & TRAFFIC_ACTION_DISTRIBUTE)
1373 ptr += sprintf(ptr, " distribute");
1374 if (api->actions[i].u.za.filter
1375 & TRAFFIC_ACTION_SAMPLE)
1376 ptr += sprintf(ptr, " sample");
1377 break;
1378 case ACTION_REDIRECT_IP:
1379 INCREMENT_DISPLAY(ptr, nb_items);
1380 char local_buff[INET_ADDRSTRLEN];
1381
1382 if (inet_ntop(AF_INET,
1383 &api->actions[i].u.zr.redirect_ip_v4,
1384 local_buff, INET_ADDRSTRLEN) != NULL)
1385 ptr += sprintf(ptr,
1386 "@redirect ip nh %s", local_buff);
1387 break;
1388 case ACTION_REDIRECT:
1389 INCREMENT_DISPLAY(ptr, nb_items);
1390 ptr += sprintf(ptr, "@redirect vrf %u",
1391 api->actions[i].u.redirect_vrf);
1392 break;
1393 case ACTION_MARKING:
1394 INCREMENT_DISPLAY(ptr, nb_items);
1395 ptr += sprintf(ptr, "@set dscp %u",
1396 api->actions[i].u.marking_dscp);
1397 break;
1398 default:
1399 break;
1400 }
1401 }
1402 zlog_info("%s", return_string);
1403}
45918cfb 1404
9350f1df
PG
1405static void bgp_pbr_flush_iprule(struct bgp *bgp, struct bgp_pbr_action *bpa,
1406 struct bgp_pbr_rule *bpr)
1407{
1408 /* if bpr is null, do nothing
1409 */
1410 if (bpr == NULL)
1411 return;
1412 if (bpr->installed) {
1413 bgp_send_pbr_rule_action(bpa, bpr, false);
1414 bpr->installed = false;
1415 bpr->action->refcnt--;
1416 bpr->action = NULL;
ce3c0614
PG
1417 if (bpr->path) {
1418 struct bgp_path_info *path;
1419 struct bgp_path_info_extra *extra;
1420
1421 /* unlink path to bpme */
1422 path = (struct bgp_path_info *)bpr->path;
1423 extra = bgp_path_info_extra_get(path);
1424 listnode_delete(extra->bgp_fs_iprule, bpr);
1425 bpr->path = NULL;
1426 }
9350f1df
PG
1427 }
1428 hash_release(bgp->pbr_rule_hash, bpr);
1429 if (bpa->refcnt == 0) {
1430 if (bpa->installed && bpa->table_id != 0) {
1431 bgp_send_pbr_rule_action(bpa, NULL, false);
1432 bgp_zebra_announce_default(bpa->bgp, &(bpa->nh),
1433 AFI_IP,
1434 bpa->table_id,
1435 false);
1436 bpa->installed = false;
1437 }
1438 }
1439}
1440
d114b0d7
PG
1441static void bgp_pbr_flush_entry(struct bgp *bgp, struct bgp_pbr_action *bpa,
1442 struct bgp_pbr_match *bpm,
1443 struct bgp_pbr_match_entry *bpme)
1444{
1445 /* if bpme is null, bpm is also null
1446 */
1447 if (bpme == NULL)
1448 return;
1449 /* ipset del entry */
1450 if (bpme->installed) {
1451 bgp_send_pbr_ipset_entry_match(bpme, false);
1452 bpme->installed = false;
1453 bpme->backpointer = NULL;
9b6d8fcf
DS
1454 if (bpme->path) {
1455 struct bgp_path_info *path;
4b7e6066 1456 struct bgp_path_info_extra *extra;
b588b642 1457
ce3c0614 1458 /* unlink path to bpme */
9b6d8fcf
DS
1459 path = (struct bgp_path_info *)bpme->path;
1460 extra = bgp_path_info_extra_get(path);
ce3c0614 1461 listnode_delete(extra->bgp_fs_pbr, bpme);
9b6d8fcf 1462 bpme->path = NULL;
b588b642 1463 }
d114b0d7
PG
1464 }
1465 hash_release(bpm->entry_hash, bpme);
1466 if (hashcount(bpm->entry_hash) == 0) {
1467 /* delete iptable entry first */
1468 /* then delete ipset match */
1469 if (bpm->installed) {
1470 if (bpm->installed_in_iptable) {
1471 bgp_send_pbr_iptable(bpm->action,
1472 bpm, false);
1473 bpm->installed_in_iptable = false;
a6b07429 1474 bpm->action->refcnt--;
d114b0d7
PG
1475 }
1476 bgp_send_pbr_ipset_match(bpm, false);
1477 bpm->installed = false;
1478 bpm->action = NULL;
1479 }
1480 hash_release(bgp->pbr_match_hash, bpm);
1481 /* XXX release pbr_match_action if not used
1482 * note that drop does not need to call send_pbr_action
1483 */
1484 }
a6b07429
PG
1485 if (bpa->refcnt == 0) {
1486 if (bpa->installed && bpa->table_id != 0) {
6cfe5d15 1487 bgp_send_pbr_rule_action(bpa, NULL, false);
a6b07429
PG
1488 bgp_zebra_announce_default(bpa->bgp, &(bpa->nh),
1489 AFI_IP,
1490 bpa->table_id,
1491 false);
6ee20355 1492 bpa->installed = false;
a6b07429
PG
1493 }
1494 }
d114b0d7
PG
1495}
1496
1497struct bgp_pbr_match_entry_remain {
1498 struct bgp_pbr_match_entry *bpme_to_match;
1499 struct bgp_pbr_match_entry *bpme_found;
1500};
1501
9350f1df
PG
1502struct bgp_pbr_rule_remain {
1503 struct bgp_pbr_rule *bpr_to_match;
1504 struct bgp_pbr_rule *bpr_found;
1505};
1506
1507static int bgp_pbr_get_same_rule(struct hash_backet *backet, void *arg)
1508{
1509 struct bgp_pbr_rule *r1 = (struct bgp_pbr_rule *)backet->data;
1510 struct bgp_pbr_rule_remain *ctxt =
1511 (struct bgp_pbr_rule_remain *)arg;
1512 struct bgp_pbr_rule *r2;
1513
1514 r2 = ctxt->bpr_to_match;
1515
1516 if (r1->vrf_id != r2->vrf_id)
1517 return HASHWALK_CONTINUE;
1518
1519 if (r1->flags != r2->flags)
1520 return HASHWALK_CONTINUE;
1521
1522 if ((r1->flags & MATCH_IP_SRC_SET) &&
1523 !prefix_same(&r1->src, &r2->src))
1524 return HASHWALK_CONTINUE;
1525
1526 if ((r1->flags & MATCH_IP_DST_SET) &&
1527 !prefix_same(&r1->dst, &r2->dst))
1528 return HASHWALK_CONTINUE;
1529
1530 /* this function is used for two cases:
1531 * - remove an entry upon withdraw request
1532 * (case r2->action is null)
1533 * - replace an old iprule with different action
1534 * (case r2->action is != null)
1535 * the old one is removed after the new one
1536 * this is to avoid disruption in traffic
1537 */
1538 if (r2->action == NULL ||
1539 r1->action != r2->action) {
1540 ctxt->bpr_found = r1;
1541 return HASHWALK_ABORT;
1542 }
1543 return HASHWALK_CONTINUE;
1544}
1545
d114b0d7
PG
1546static int bgp_pbr_get_remaining_entry(struct hash_backet *backet, void *arg)
1547{
1548 struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data;
1549 struct bgp_pbr_match_entry_remain *bpmer =
1550 (struct bgp_pbr_match_entry_remain *)arg;
1551 struct bgp_pbr_match *bpm_temp;
1552 struct bgp_pbr_match_entry *bpme = bpmer->bpme_to_match;
1553
1554 if (!bpme->backpointer ||
1555 bpm == bpme->backpointer ||
1556 bpme->backpointer->action == bpm->action)
1557 return HASHWALK_CONTINUE;
1558 /* ensure bpm other characteristics are equal */
1559 bpm_temp = bpme->backpointer;
1560 if (bpm_temp->vrf_id != bpm->vrf_id ||
1561 bpm_temp->type != bpm->type ||
836b6953
PG
1562 bpm_temp->flags != bpm->flags ||
1563 bpm_temp->tcp_flags != bpm->tcp_flags ||
1564 bpm_temp->tcp_mask_flags != bpm->tcp_mask_flags ||
1565 bpm_temp->pkt_len_min != bpm->pkt_len_min ||
1566 bpm_temp->pkt_len_max != bpm->pkt_len_max ||
1567 bpm_temp->dscp_value != bpm->dscp_value ||
1568 bpm_temp->fragment != bpm->fragment)
d114b0d7
PG
1569 return HASHWALK_CONTINUE;
1570
1571 /* look for remaining bpme */
1572 bpmer->bpme_found = hash_lookup(bpm->entry_hash, bpme);
1573 if (!bpmer->bpme_found)
1574 return HASHWALK_CONTINUE;
1575 return HASHWALK_ABORT;
1576}
1577
9b6d8fcf
DS
1578static void bgp_pbr_policyroute_remove_from_zebra_unit(
1579 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf)
d114b0d7
PG
1580{
1581 struct bgp_pbr_match temp;
1582 struct bgp_pbr_match_entry temp2;
9350f1df
PG
1583 struct bgp_pbr_rule pbr_rule;
1584 struct bgp_pbr_rule *bpr;
d114b0d7
PG
1585 struct bgp_pbr_match *bpm;
1586 struct bgp_pbr_match_entry *bpme;
1587 struct bgp_pbr_match_entry_remain bpmer;
0e867886
PG
1588 struct bgp_pbr_range_port *src_port;
1589 struct bgp_pbr_range_port *dst_port;
1590 struct bgp_pbr_range_port *pkt_len;
9350f1df 1591 struct bgp_pbr_rule_remain bprr;
0e867886
PG
1592
1593 if (!bpf)
1594 return;
1595 src_port = bpf->src_port;
1596 dst_port = bpf->dst_port;
1597 pkt_len = bpf->pkt_len;
d114b0d7 1598
064a9d52
PG
1599 if (BGP_DEBUG(zebra, ZEBRA))
1600 bgp_pbr_dump_entry(bpf, false);
1601
d114b0d7
PG
1602 /* as we don't know information from EC
1603 * look for bpm that have the bpm
1604 * with vrf_id characteristics
1605 */
1606 memset(&temp2, 0, sizeof(temp2));
1607 memset(&temp, 0, sizeof(temp));
9350f1df
PG
1608
1609 if (bpf->type == BGP_PBR_IPRULE) {
1610 memset(&pbr_rule, 0, sizeof(pbr_rule));
1611 pbr_rule.vrf_id = bpf->vrf_id;
1612 if (bpf->src) {
1613 prefix_copy(&pbr_rule.src, bpf->src);
1614 pbr_rule.flags |= MATCH_IP_SRC_SET;
1615 }
1616 if (bpf->dst) {
1617 prefix_copy(&pbr_rule.dst, bpf->dst);
1618 pbr_rule.flags |= MATCH_IP_DST_SET;
1619 }
1620 bpr = &pbr_rule;
1621 /* A previous entry may already exist
1622 * flush previous entry if necessary
1623 */
1624 bprr.bpr_to_match = bpr;
1625 bprr.bpr_found = NULL;
1626 hash_walk(bgp->pbr_rule_hash, bgp_pbr_get_same_rule, &bprr);
1627 if (bprr.bpr_found) {
1628 static struct bgp_pbr_rule *local_bpr;
1629 static struct bgp_pbr_action *local_bpa;
1630
1631 local_bpr = bprr.bpr_found;
1632 local_bpa = local_bpr->action;
1633 bgp_pbr_flush_iprule(bgp, local_bpa,
1634 local_bpr);
1635 }
1636 return;
1637 }
1638
0e867886 1639 if (bpf->src) {
d114b0d7 1640 temp.flags |= MATCH_IP_SRC_SET;
0e867886 1641 prefix_copy(&temp2.src, bpf->src);
d114b0d7
PG
1642 } else
1643 temp2.src.family = AF_INET;
0e867886 1644 if (bpf->dst) {
d114b0d7 1645 temp.flags |= MATCH_IP_DST_SET;
0e867886 1646 prefix_copy(&temp2.dst, bpf->dst);
d114b0d7
PG
1647 } else
1648 temp2.dst.family = AF_INET;
3bed2363
PG
1649 if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1650 if (bpf->protocol == IPPROTO_ICMP)
1651 temp.flags |= MATCH_ICMP_SET;
1de7dfff
PG
1652 temp.flags |= MATCH_PORT_SRC_SET;
1653 temp2.src_port_min = src_port->min_port;
1654 if (src_port->max_port) {
1655 temp.flags |= MATCH_PORT_SRC_RANGE_SET;
1656 temp2.src_port_max = src_port->max_port;
1657 }
1658 }
3bed2363
PG
1659 if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
1660 if (bpf->protocol == IPPROTO_ICMP)
1661 temp.flags |= MATCH_ICMP_SET;
1de7dfff
PG
1662 temp.flags |= MATCH_PORT_DST_SET;
1663 temp2.dst_port_min = dst_port->min_port;
1664 if (dst_port->max_port) {
1665 temp.flags |= MATCH_PORT_DST_RANGE_SET;
1666 temp2.dst_port_max = dst_port->max_port;
1667 }
1668 }
0e867886 1669 temp2.proto = bpf->protocol;
1de7dfff 1670
cfaf18ce 1671 if (pkt_len) {
83360720 1672 temp.pkt_len_min = pkt_len->min_port;
cfaf18ce
PG
1673 if (pkt_len->max_port)
1674 temp.pkt_len_max = pkt_len->max_port;
1675 } else if (bpf->pkt_len_val) {
1676 if (bpf->pkt_len_val->mask)
1677 temp.flags |= MATCH_PKT_LEN_INVERSE_SET;
1678 temp.pkt_len_min = bpf->pkt_len_val->val;
1679 }
2da7d62e
PG
1680 if (bpf->tcp_flags) {
1681 temp.tcp_flags = bpf->tcp_flags->val;
1682 temp.tcp_mask_flags = bpf->tcp_flags->mask;
1683 }
4977bd6c
PG
1684 if (bpf->dscp) {
1685 if (bpf->dscp->mask)
1686 temp.flags |= MATCH_DSCP_INVERSE_SET;
1687 else
1688 temp.flags |= MATCH_DSCP_SET;
1689 temp.dscp_value = bpf->dscp->val;
1690 }
6f5617d8
PG
1691 if (bpf->fragment) {
1692 if (bpf->fragment->mask)
1693 temp.flags |= MATCH_FRAGMENT_INVERSE_SET;
1694 temp.fragment = bpf->fragment->val;
1695 }
83360720 1696
0e867886 1697 if (bpf->src == NULL || bpf->dst == NULL) {
1de7dfff
PG
1698 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1699 temp.type = IPSET_NET_PORT;
1700 else
1701 temp.type = IPSET_NET;
1702 } else {
1703 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
1704 temp.type = IPSET_NET_PORT_NET;
1705 else
1706 temp.type = IPSET_NET_NET;
1707 }
0e867886 1708 if (bpf->vrf_id == VRF_UNKNOWN) /* XXX case BGP destroy */
d114b0d7
PG
1709 temp.vrf_id = 0;
1710 else
0e867886 1711 temp.vrf_id = bpf->vrf_id;
d114b0d7
PG
1712 bpme = &temp2;
1713 bpm = &temp;
1714 bpme->backpointer = bpm;
1715 /* right now, a previous entry may already exist
1716 * flush previous entry if necessary
1717 */
1718 bpmer.bpme_to_match = bpme;
1719 bpmer.bpme_found = NULL;
1720 hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer);
1721 if (bpmer.bpme_found) {
1722 static struct bgp_pbr_match *local_bpm;
1723 static struct bgp_pbr_action *local_bpa;
1724
1725 local_bpm = bpmer.bpme_found->backpointer;
1726 local_bpa = local_bpm->action;
1727 bgp_pbr_flush_entry(bgp, local_bpa,
1728 local_bpm, bpmer.bpme_found);
1729 }
1730}
1731
e45aea59
PG
1732static uint8_t bgp_pbr_next_type_entry(uint8_t type_entry)
1733{
1734 if (type_entry == FLOWSPEC_TCP_FLAGS)
1735 return FLOWSPEC_DSCP;
1736 if (type_entry == FLOWSPEC_DSCP)
1737 return FLOWSPEC_PKT_LEN;
1738 if (type_entry == FLOWSPEC_PKT_LEN)
1739 return FLOWSPEC_FRAGMENT;
da3fa383
PG
1740 if (type_entry == FLOWSPEC_FRAGMENT)
1741 return FLOWSPEC_ICMP_TYPE;
e45aea59
PG
1742 return 0;
1743}
1744
9b6d8fcf 1745static void bgp_pbr_icmp_action(struct bgp *bgp, struct bgp_path_info *path,
4b7e6066
DS
1746 struct bgp_pbr_filter *bpf,
1747 struct bgp_pbr_or_filter *bpof, bool add,
1748 struct nexthop *nh, float *rate)
da3fa383
PG
1749{
1750 struct bgp_pbr_range_port srcp, dstp;
1751 struct bgp_pbr_val_mask *icmp_type, *icmp_code;
1752 struct listnode *tnode, *cnode;
1753
1754 if (!bpf)
1755 return;
1756 if (bpf->protocol != IPPROTO_ICMP)
1757 return;
1758 bpf->src_port = &srcp;
1759 bpf->dst_port = &dstp;
1760 /* parse icmp type and lookup appropriate icmp code
1761 * if no icmp code found, create as many entryes as
1762 * there are listed icmp codes for that icmp type
1763 */
1764 if (!bpof->icmp_type) {
1765 srcp.min_port = 0;
1766 srcp.max_port = 255;
1767 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) {
1768 dstp.min_port = icmp_code->val;
1769 if (add)
9b6d8fcf
DS
1770 bgp_pbr_policyroute_add_to_zebra_unit(
1771 bgp, path, bpf, nh, rate);
da3fa383
PG
1772 else
1773 bgp_pbr_policyroute_remove_from_zebra_unit(
9b6d8fcf 1774 bgp, path, bpf);
da3fa383
PG
1775 }
1776 return;
1777 }
1778 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_type, tnode, icmp_type)) {
1779 srcp.min_port = icmp_type->val;
1780 srcp.max_port = 0;
1781 dstp.max_port = 0;
1782 /* only icmp type. create an entry only with icmp type */
1783 if (!bpof->icmp_code) {
1784 /* icmp type is not one of the above
1785 * forge an entry only based on the icmp type
1786 */
1787 dstp.min_port = 0;
1788 dstp.max_port = 255;
1789 if (add)
1790 bgp_pbr_policyroute_add_to_zebra_unit(
9b6d8fcf 1791 bgp, path, bpf, nh, rate);
da3fa383 1792 else
9b6d8fcf
DS
1793 bgp_pbr_policyroute_remove_from_zebra_unit(
1794 bgp, path, bpf);
da3fa383
PG
1795 continue;
1796 }
1797 for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) {
1798 dstp.min_port = icmp_code->val;
1799 if (add)
1800 bgp_pbr_policyroute_add_to_zebra_unit(
9b6d8fcf 1801 bgp, path, bpf, nh, rate);
da3fa383
PG
1802 else
1803 bgp_pbr_policyroute_remove_from_zebra_unit(
9b6d8fcf 1804 bgp, path, bpf);
da3fa383
PG
1805 }
1806 }
1807}
1808
4b7e6066 1809static void bgp_pbr_policyroute_remove_from_zebra_recursive(
9b6d8fcf
DS
1810 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
1811 struct bgp_pbr_or_filter *bpof, uint8_t type_entry)
56707a36
PG
1812{
1813 struct listnode *node, *nnode;
1814 struct bgp_pbr_val_mask *valmask;
1815 uint8_t next_type_entry;
1816 struct list *orig_list;
1817 struct bgp_pbr_val_mask **target_val;
1818
d90b788e
A
1819 if (type_entry == 0) {
1820 bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path, bpf);
1821 return;
1822 }
e45aea59 1823 next_type_entry = bgp_pbr_next_type_entry(type_entry);
56707a36 1824 if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) {
56707a36
PG
1825 orig_list = bpof->tcpflags;
1826 target_val = &bpf->tcp_flags;
1827 } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) {
56707a36
PG
1828 orig_list = bpof->dscp;
1829 target_val = &bpf->dscp;
cfaf18ce 1830 } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) {
cfaf18ce
PG
1831 orig_list = bpof->pkt_len;
1832 target_val = &bpf->pkt_len_val;
6f5617d8 1833 } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) {
6f5617d8
PG
1834 orig_list = bpof->fragment;
1835 target_val = &bpf->fragment;
da3fa383
PG
1836 } else if (type_entry == FLOWSPEC_ICMP_TYPE &&
1837 (bpof->icmp_type || bpof->icmp_code)) {
1838 /* enumerate list for icmp - must be last one */
9b6d8fcf 1839 bgp_pbr_icmp_action(bgp, path, bpf, bpof, false, NULL, NULL);
da3fa383 1840 return;
56707a36 1841 } else {
d90b788e 1842 bgp_pbr_policyroute_remove_from_zebra_recursive(
9b6d8fcf 1843 bgp, path, bpf, bpof, next_type_entry);
d90b788e 1844 return;
56707a36
PG
1845 }
1846 for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) {
1847 *target_val = valmask;
9b6d8fcf
DS
1848 bgp_pbr_policyroute_remove_from_zebra_recursive(
1849 bgp, path, bpf, bpof, next_type_entry);
56707a36
PG
1850 }
1851}
1852
4b7e6066 1853static void bgp_pbr_policyroute_remove_from_zebra(
9b6d8fcf
DS
1854 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
1855 struct bgp_pbr_or_filter *bpof)
9bba1455 1856{
d90b788e
A
1857 if (!bpof) {
1858 bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path, bpf);
1859 return;
1860 }
56707a36 1861 if (bpof->tcpflags)
9b6d8fcf
DS
1862 bgp_pbr_policyroute_remove_from_zebra_recursive(
1863 bgp, path, bpf, bpof, FLOWSPEC_TCP_FLAGS);
56707a36 1864 else if (bpof->dscp)
9b6d8fcf
DS
1865 bgp_pbr_policyroute_remove_from_zebra_recursive(
1866 bgp, path, bpf, bpof, FLOWSPEC_DSCP);
cfaf18ce 1867 else if (bpof->pkt_len)
9b6d8fcf
DS
1868 bgp_pbr_policyroute_remove_from_zebra_recursive(
1869 bgp, path, bpf, bpof, FLOWSPEC_PKT_LEN);
6f5617d8 1870 else if (bpof->fragment)
9b6d8fcf
DS
1871 bgp_pbr_policyroute_remove_from_zebra_recursive(
1872 bgp, path, bpf, bpof, FLOWSPEC_FRAGMENT);
da3fa383 1873 else if (bpof->icmp_type || bpof->icmp_code)
9b6d8fcf
DS
1874 bgp_pbr_policyroute_remove_from_zebra_recursive(
1875 bgp, path, bpf, bpof, FLOWSPEC_ICMP_TYPE);
56707a36 1876 else
9b6d8fcf 1877 bgp_pbr_policyroute_remove_from_zebra_unit(bgp, path, bpf);
56707a36
PG
1878 /* flush bpof */
1879 if (bpof->tcpflags)
1880 list_delete_all_node(bpof->tcpflags);
1881 if (bpof->dscp)
1882 list_delete_all_node(bpof->dscp);
cfaf18ce
PG
1883 if (bpof->pkt_len)
1884 list_delete_all_node(bpof->pkt_len);
6f5617d8
PG
1885 if (bpof->fragment)
1886 list_delete_all_node(bpof->fragment);
9bba1455
PG
1887}
1888
064a9d52
PG
1889static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add)
1890{
1891 struct bgp_pbr_range_port *src_port;
1892 struct bgp_pbr_range_port *dst_port;
1893 struct bgp_pbr_range_port *pkt_len;
1894 char bufsrc[64], bufdst[64];
1895 char buffer[64];
1896 int remaining_len = 0;
1897 char protocol_str[16];
1898
1899 if (!bpf)
1900 return;
1901 src_port = bpf->src_port;
1902 dst_port = bpf->dst_port;
1903 pkt_len = bpf->pkt_len;
1904
1905 protocol_str[0] = '\0';
1906 if (bpf->tcp_flags && bpf->tcp_flags->mask)
1907 bpf->protocol = IPPROTO_TCP;
1908 if (bpf->protocol)
1909 snprintf(protocol_str, sizeof(protocol_str),
1910 "proto %d", bpf->protocol);
1911 buffer[0] = '\0';
1912 if (bpf->protocol == IPPROTO_ICMP && src_port && dst_port)
1913 remaining_len += snprintf(buffer, sizeof(buffer),
1914 "type %d, code %d",
1915 src_port->min_port,
1916 dst_port->min_port);
1917 else if (bpf->protocol == IPPROTO_UDP ||
1918 bpf->protocol == IPPROTO_TCP) {
1919
1920 if (src_port && src_port->min_port)
1921 remaining_len += snprintf(buffer,
1922 sizeof(buffer),
1923 "from [%u:%u]",
1924 src_port->min_port,
1925 src_port->max_port ?
1926 src_port->max_port :
1927 src_port->min_port);
1928 if (dst_port && dst_port->min_port)
1929 remaining_len += snprintf(buffer +
1930 remaining_len,
1931 sizeof(buffer)
1932 - remaining_len,
1933 "to [%u:%u]",
1934 dst_port->min_port,
1935 dst_port->max_port ?
1936 dst_port->max_port :
1937 dst_port->min_port);
1938 }
1939 if (pkt_len && (pkt_len->min_port || pkt_len->max_port)) {
1940 remaining_len += snprintf(buffer + remaining_len,
1941 sizeof(buffer)
1942 - remaining_len,
1943 " len [%u:%u]",
1944 pkt_len->min_port,
1945 pkt_len->max_port ?
1946 pkt_len->max_port :
1947 pkt_len->min_port);
1948 } else if (bpf->pkt_len_val) {
1949 remaining_len += snprintf(buffer + remaining_len,
1950 sizeof(buffer)
1951 - remaining_len,
1952 " %s len %u",
1953 bpf->pkt_len_val->mask
1954 ? "!" : "",
1955 bpf->pkt_len_val->val);
1956 }
1957 if (bpf->tcp_flags) {
1958 remaining_len += snprintf(buffer + remaining_len,
1959 sizeof(buffer)
1960 - remaining_len,
1961 "tcpflags %x/%x",
1962 bpf->tcp_flags->val,
1963 bpf->tcp_flags->mask);
1964 }
1965 if (bpf->dscp) {
1966 snprintf(buffer + remaining_len,
1967 sizeof(buffer)
1968 - remaining_len,
1969 "%s dscp %d",
1970 bpf->dscp->mask
1971 ? "!" : "",
1972 bpf->dscp->val);
1973 }
45837bc4 1974 zlog_debug("BGP: %s FS PBR from %s to %s, %s %s",
064a9d52
PG
1975 add ? "adding" : "removing",
1976 bpf->src == NULL ? "<all>" :
1977 prefix2str(bpf->src, bufsrc, sizeof(bufsrc)),
1978 bpf->dst == NULL ? "<all>" :
1979 prefix2str(bpf->dst, bufdst, sizeof(bufdst)),
1980 protocol_str, buffer);
1981
1982}
1983
9bba1455 1984static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp *bgp,
9b6d8fcf 1985 struct bgp_path_info *path,
4b7e6066
DS
1986 struct bgp_pbr_filter *bpf,
1987 struct nexthop *nh,
1988 float *rate)
d114b0d7
PG
1989{
1990 struct bgp_pbr_match temp;
1991 struct bgp_pbr_match_entry temp2;
1992 struct bgp_pbr_match *bpm;
1993 struct bgp_pbr_match_entry *bpme = NULL;
1994 struct bgp_pbr_action temp3;
1995 struct bgp_pbr_action *bpa = NULL;
1996 struct bgp_pbr_match_entry_remain bpmer;
9350f1df 1997 struct bgp_pbr_rule_remain bprr;
0e867886
PG
1998 struct bgp_pbr_range_port *src_port;
1999 struct bgp_pbr_range_port *dst_port;
2000 struct bgp_pbr_range_port *pkt_len;
9350f1df
PG
2001 struct bgp_pbr_rule pbr_rule;
2002 struct bgp_pbr_rule *bpr;
ce3c0614 2003 bool bpr_found = false;
c26edcda 2004 bool bpme_found = false;
0e867886
PG
2005
2006 if (!bpf)
2007 return;
2008 src_port = bpf->src_port;
2009 dst_port = bpf->dst_port;
2010 pkt_len = bpf->pkt_len;
d114b0d7 2011
064a9d52
PG
2012 if (BGP_DEBUG(zebra, ZEBRA))
2013 bgp_pbr_dump_entry(bpf, true);
2014
d114b0d7
PG
2015 /* look for bpa first */
2016 memset(&temp3, 0, sizeof(temp3));
2017 if (rate)
2018 temp3.rate = *rate;
2019 if (nh)
2020 memcpy(&temp3.nh, nh, sizeof(struct nexthop));
0e867886 2021 temp3.vrf_id = bpf->vrf_id;
d114b0d7
PG
2022 bpa = hash_get(bgp->pbr_action_hash, &temp3,
2023 bgp_pbr_action_alloc_intern);
2024
2025 if (bpa->fwmark == 0) {
d114b0d7
PG
2026 /* drop is handled by iptable */
2027 if (nh && nh->type == NEXTHOP_TYPE_BLACKHOLE) {
2028 bpa->table_id = 0;
2029 bpa->installed = true;
2030 } else {
31c28cd7
PG
2031 bpa->fwmark = bgp_zebra_tm_get_id();
2032 bpa->table_id = bpa->fwmark;
d114b0d7
PG
2033 bpa->installed = false;
2034 }
a6b07429 2035 bpa->bgp = bgp;
d114b0d7
PG
2036 bpa->unique = ++bgp_pbr_action_counter_unique;
2037 /* 0 value is forbidden */
2038 bpa->install_in_progress = false;
2039 }
9350f1df
PG
2040 if (bpf->type == BGP_PBR_IPRULE) {
2041 memset(&pbr_rule, 0, sizeof(pbr_rule));
2042 pbr_rule.vrf_id = bpf->vrf_id;
8112a7a0 2043 pbr_rule.priority = 20;
9350f1df
PG
2044 if (bpf->src) {
2045 pbr_rule.flags |= MATCH_IP_SRC_SET;
2046 prefix_copy(&pbr_rule.src, bpf->src);
2047 }
2048 if (bpf->dst) {
2049 pbr_rule.flags |= MATCH_IP_DST_SET;
2050 prefix_copy(&pbr_rule.dst, bpf->dst);
2051 }
2052 pbr_rule.action = bpa;
2053 bpr = hash_get(bgp->pbr_rule_hash, &pbr_rule,
2054 bgp_pbr_rule_alloc_intern);
2055 if (bpr && bpr->unique == 0) {
2056 bpr->unique = ++bgp_pbr_action_counter_unique;
2057 bpr->installed = false;
2058 bpr->install_in_progress = false;
ce3c0614
PG
2059 /* link bgp info to bpr */
2060 bpr->path = (void *)path;
2061 } else
2062 bpr_found = true;
2063 /* already installed */
2064 if (bpr_found && bpr) {
2065 struct bgp_path_info_extra *extra =
2066 bgp_path_info_extra_get(path);
2067
2068 if (extra && listnode_lookup(extra->bgp_fs_iprule,
2069 bpr)) {
2070 if (BGP_DEBUG(pbr, PBR_ERROR))
2071 zlog_err("%s: entry %p/%p already "
2072 "installed in bgp pbr iprule",
2073 __func__, path, bpr);
2074 return;
2075 }
9350f1df
PG
2076 }
2077 if (!bpa->installed && !bpa->install_in_progress) {
2078 bgp_send_pbr_rule_action(bpa, NULL, true);
2079 bgp_zebra_announce_default(bgp, nh,
2080 AFI_IP, bpa->table_id, true);
2081 }
2082 /* ip rule add */
2083 if (bpr && !bpr->installed)
2084 bgp_send_pbr_rule_action(bpa, bpr, true);
d114b0d7 2085
9350f1df
PG
2086 /* A previous entry may already exist
2087 * flush previous entry if necessary
2088 */
2089 bprr.bpr_to_match = bpr;
2090 bprr.bpr_found = NULL;
2091 hash_walk(bgp->pbr_rule_hash, bgp_pbr_get_same_rule, &bprr);
2092 if (bprr.bpr_found) {
2093 static struct bgp_pbr_rule *local_bpr;
2094 static struct bgp_pbr_action *local_bpa;
2095
2096 local_bpr = bprr.bpr_found;
2097 local_bpa = local_bpr->action;
2098 bgp_pbr_flush_iprule(bgp, local_bpa,
2099 local_bpr);
2100 }
2101 return;
2102 }
d114b0d7
PG
2103 /* then look for bpm */
2104 memset(&temp, 0, sizeof(temp));
0e867886
PG
2105 temp.vrf_id = bpf->vrf_id;
2106 if (bpf->src)
d114b0d7 2107 temp.flags |= MATCH_IP_SRC_SET;
0e867886 2108 if (bpf->dst)
d114b0d7 2109 temp.flags |= MATCH_IP_DST_SET;
1de7dfff 2110
3bed2363
PG
2111 if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
2112 if (bpf->protocol == IPPROTO_ICMP)
2113 temp.flags |= MATCH_ICMP_SET;
1de7dfff 2114 temp.flags |= MATCH_PORT_SRC_SET;
3bed2363
PG
2115 }
2116 if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) {
2117 if (bpf->protocol == IPPROTO_ICMP)
2118 temp.flags |= MATCH_ICMP_SET;
1de7dfff 2119 temp.flags |= MATCH_PORT_DST_SET;
3bed2363 2120 }
1de7dfff
PG
2121 if (src_port && src_port->max_port)
2122 temp.flags |= MATCH_PORT_SRC_RANGE_SET;
2123 if (dst_port && dst_port->max_port)
2124 temp.flags |= MATCH_PORT_DST_RANGE_SET;
3bed2363
PG
2125
2126 if (bpf->src == NULL || bpf->dst == NULL) {
2127 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
2128 temp.type = IPSET_NET_PORT;
2129 else
2130 temp.type = IPSET_NET;
2131 } else {
2132 if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET))
2133 temp.type = IPSET_NET_PORT_NET;
2134 else
2135 temp.type = IPSET_NET_NET;
2136 }
cfaf18ce 2137 if (pkt_len) {
83360720 2138 temp.pkt_len_min = pkt_len->min_port;
cfaf18ce
PG
2139 if (pkt_len->max_port)
2140 temp.pkt_len_max = pkt_len->max_port;
2141 } else if (bpf->pkt_len_val) {
2142 if (bpf->pkt_len_val->mask)
2143 temp.flags |= MATCH_PKT_LEN_INVERSE_SET;
2144 temp.pkt_len_min = bpf->pkt_len_val->val;
2145 }
2da7d62e
PG
2146 if (bpf->tcp_flags) {
2147 temp.tcp_flags = bpf->tcp_flags->val;
2148 temp.tcp_mask_flags = bpf->tcp_flags->mask;
2149 }
4977bd6c
PG
2150 if (bpf->dscp) {
2151 if (bpf->dscp->mask)
2152 temp.flags |= MATCH_DSCP_INVERSE_SET;
2153 else
2154 temp.flags |= MATCH_DSCP_SET;
2155 temp.dscp_value = bpf->dscp->val;
2156 }
6f5617d8
PG
2157 if (bpf->fragment) {
2158 if (bpf->fragment->mask)
2159 temp.flags |= MATCH_FRAGMENT_INVERSE_SET;
2160 temp.fragment = bpf->fragment->val;
2161 }
d114b0d7
PG
2162 temp.action = bpa;
2163 bpm = hash_get(bgp->pbr_match_hash, &temp,
2164 bgp_pbr_match_alloc_intern);
2165
2166 /* new, then self allocate ipset_name and unique */
5a430eee 2167 if (bpm->unique == 0) {
d114b0d7
PG
2168 bpm->unique = ++bgp_pbr_match_counter_unique;
2169 /* 0 value is forbidden */
2170 sprintf(bpm->ipset_name, "match%p", bpm);
2171 bpm->entry_hash = hash_create_size(8,
2172 bgp_pbr_match_entry_hash_key,
2173 bgp_pbr_match_entry_hash_equal,
2174 "Match Entry Hash");
2175 bpm->installed = false;
2176
2177 /* unique2 should be updated too */
2178 bpm->unique2 = ++bgp_pbr_match_iptable_counter_unique;
2179 bpm->installed_in_iptable = false;
2180 bpm->install_in_progress = false;
2181 bpm->install_iptable_in_progress = false;
2182 }
2183
2184 memset(&temp2, 0, sizeof(temp2));
0e867886
PG
2185 if (bpf->src)
2186 prefix_copy(&temp2.src, bpf->src);
d114b0d7
PG
2187 else
2188 temp2.src.family = AF_INET;
0e867886
PG
2189 if (bpf->dst)
2190 prefix_copy(&temp2.dst, bpf->dst);
d114b0d7
PG
2191 else
2192 temp2.dst.family = AF_INET;
1de7dfff
PG
2193 temp2.src_port_min = src_port ? src_port->min_port : 0;
2194 temp2.dst_port_min = dst_port ? dst_port->min_port : 0;
2195 temp2.src_port_max = src_port ? src_port->max_port : 0;
2196 temp2.dst_port_max = dst_port ? dst_port->max_port : 0;
0e867886 2197 temp2.proto = bpf->protocol;
5a430eee
PG
2198 bpme = hash_get(bpm->entry_hash, &temp2,
2199 bgp_pbr_match_entry_alloc_intern);
2200 if (bpme->unique == 0) {
d114b0d7
PG
2201 bpme->unique = ++bgp_pbr_match_entry_counter_unique;
2202 /* 0 value is forbidden */
2203 bpme->backpointer = bpm;
2204 bpme->installed = false;
2205 bpme->install_in_progress = false;
b588b642 2206 /* link bgp info to bpme */
9b6d8fcf 2207 bpme->path = (void *)path;
c26edcda
PG
2208 } else
2209 bpme_found = true;
d114b0d7 2210
c26edcda 2211 /* already installed */
5a430eee 2212 if (bpme_found) {
18ee8310 2213 struct bgp_path_info_extra *extra =
9b6d8fcf 2214 bgp_path_info_extra_get(path);
c26edcda 2215
ce3c0614 2216 if (extra && listnode_lookup(extra->bgp_fs_pbr, bpme)) {
c26edcda 2217 if (BGP_DEBUG(pbr, PBR_ERROR))
9b6d8fcf
DS
2218 zlog_err(
2219 "%s: entry %p/%p already installed in bgp pbr",
2220 __func__, path, bpme);
c26edcda
PG
2221 return;
2222 }
2223 }
d114b0d7
PG
2224 /* BGP FS: append entry to zebra
2225 * - policies are not routing entries and as such
2226 * route replace semantics don't necessarily follow
2227 * through to policy entries
2228 * - because of that, not all policing information will be stored
2229 * into zebra. and non selected policies will be suppressed from zebra
2230 * - as consequence, in order to bring consistency
2231 * a policy will be added, then ifan ecmp policy exists,
2232 * it will be suppressed subsequently
2233 */
2234 /* ip rule add */
1a1f4a4c 2235 if (!bpa->installed && !bpa->install_in_progress) {
6cfe5d15 2236 bgp_send_pbr_rule_action(bpa, NULL, true);
f7df1907
PG
2237 bgp_zebra_announce_default(bgp, nh,
2238 AFI_IP, bpa->table_id, true);
2239 }
d114b0d7
PG
2240
2241 /* ipset create */
6ea591c7 2242 if (!bpm->installed)
d114b0d7
PG
2243 bgp_send_pbr_ipset_match(bpm, true);
2244 /* ipset add */
6ea591c7 2245 if (!bpme->installed)
d114b0d7
PG
2246 bgp_send_pbr_ipset_entry_match(bpme, true);
2247
2248 /* iptables */
6ea591c7 2249 if (!bpm->installed_in_iptable)
d114b0d7
PG
2250 bgp_send_pbr_iptable(bpa, bpm, true);
2251
2252 /* A previous entry may already exist
2253 * flush previous entry if necessary
2254 */
2255 bpmer.bpme_to_match = bpme;
2256 bpmer.bpme_found = NULL;
2257 hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer);
2258 if (bpmer.bpme_found) {
2259 static struct bgp_pbr_match *local_bpm;
2260 static struct bgp_pbr_action *local_bpa;
2261
2262 local_bpm = bpmer.bpme_found->backpointer;
2263 local_bpa = local_bpm->action;
2264 bgp_pbr_flush_entry(bgp, local_bpa,
2265 local_bpm, bpmer.bpme_found);
2266 }
2267
2268
2269}
2270
4b7e6066 2271static void bgp_pbr_policyroute_add_to_zebra_recursive(
9b6d8fcf
DS
2272 struct bgp *bgp, struct bgp_path_info *path, struct bgp_pbr_filter *bpf,
2273 struct bgp_pbr_or_filter *bpof, struct nexthop *nh, float *rate,
2274 uint8_t type_entry)
56707a36
PG
2275{
2276 struct listnode *node, *nnode;
2277 struct bgp_pbr_val_mask *valmask;
2278 uint8_t next_type_entry;
2279 struct list *orig_list;
2280 struct bgp_pbr_val_mask **target_val;
2281
d90b788e
A
2282 if (type_entry == 0) {
2283 bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh, rate);
2284 return;
2285 }
e45aea59 2286 next_type_entry = bgp_pbr_next_type_entry(type_entry);
56707a36 2287 if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) {
56707a36
PG
2288 orig_list = bpof->tcpflags;
2289 target_val = &bpf->tcp_flags;
2290 } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) {
56707a36
PG
2291 orig_list = bpof->dscp;
2292 target_val = &bpf->dscp;
cfaf18ce 2293 } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) {
cfaf18ce
PG
2294 orig_list = bpof->pkt_len;
2295 target_val = &bpf->pkt_len_val;
6f5617d8 2296 } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) {
6f5617d8
PG
2297 orig_list = bpof->fragment;
2298 target_val = &bpf->fragment;
da3fa383
PG
2299 } else if (type_entry == FLOWSPEC_ICMP_TYPE &&
2300 (bpof->icmp_type || bpof->icmp_code)) {
2301 /* enumerate list for icmp - must be last one */
9b6d8fcf 2302 bgp_pbr_icmp_action(bgp, path, bpf, bpof, true, nh, rate);
da3fa383 2303 return;
56707a36 2304 } else {
d90b788e 2305 bgp_pbr_policyroute_add_to_zebra_recursive(
9b6d8fcf 2306 bgp, path, bpf, bpof, nh, rate, next_type_entry);
d90b788e 2307 return;
56707a36
PG
2308 }
2309 for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) {
2310 *target_val = valmask;
9b6d8fcf
DS
2311 bgp_pbr_policyroute_add_to_zebra_recursive(
2312 bgp, path, bpf, bpof, nh, rate, next_type_entry);
56707a36
PG
2313 }
2314}
2315
9bba1455 2316static void bgp_pbr_policyroute_add_to_zebra(struct bgp *bgp,
9b6d8fcf 2317 struct bgp_path_info *path,
4b7e6066
DS
2318 struct bgp_pbr_filter *bpf,
2319 struct bgp_pbr_or_filter *bpof,
2320 struct nexthop *nh, float *rate)
9bba1455 2321{
d90b788e
A
2322 if (!bpof) {
2323 bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh, rate);
2324 return;
2325 }
56707a36 2326 if (bpof->tcpflags)
9b6d8fcf
DS
2327 bgp_pbr_policyroute_add_to_zebra_recursive(
2328 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_TCP_FLAGS);
56707a36 2329 else if (bpof->dscp)
9b6d8fcf
DS
2330 bgp_pbr_policyroute_add_to_zebra_recursive(
2331 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_DSCP);
cfaf18ce 2332 else if (bpof->pkt_len)
9b6d8fcf
DS
2333 bgp_pbr_policyroute_add_to_zebra_recursive(
2334 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_PKT_LEN);
6f5617d8 2335 else if (bpof->fragment)
9b6d8fcf
DS
2336 bgp_pbr_policyroute_add_to_zebra_recursive(
2337 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_FRAGMENT);
da3fa383 2338 else if (bpof->icmp_type || bpof->icmp_code)
9b6d8fcf
DS
2339 bgp_pbr_policyroute_add_to_zebra_recursive(
2340 bgp, path, bpf, bpof, nh, rate, FLOWSPEC_ICMP_TYPE);
56707a36 2341 else
9b6d8fcf 2342 bgp_pbr_policyroute_add_to_zebra_unit(bgp, path, bpf, nh, rate);
56707a36
PG
2343 /* flush bpof */
2344 if (bpof->tcpflags)
2345 list_delete_all_node(bpof->tcpflags);
2346 if (bpof->dscp)
2347 list_delete_all_node(bpof->dscp);
cfaf18ce
PG
2348 if (bpof->pkt_len)
2349 list_delete_all_node(bpof->pkt_len);
6f5617d8
PG
2350 if (bpof->fragment)
2351 list_delete_all_node(bpof->fragment);
da3fa383
PG
2352 if (bpof->icmp_type)
2353 list_delete_all_node(bpof->icmp_type);
2354 if (bpof->icmp_code)
2355 list_delete_all_node(bpof->icmp_code);
932404b7
PG
2356}
2357
9b6d8fcf 2358static void bgp_pbr_handle_entry(struct bgp *bgp, struct bgp_path_info *path,
4b7e6066 2359 struct bgp_pbr_entry_main *api, bool add)
d114b0d7
PG
2360{
2361 struct nexthop nh;
2362 int i = 0;
2363 int continue_loop = 1;
2364 float rate = 0;
2365 struct prefix *src = NULL, *dst = NULL;
1de7dfff
PG
2366 uint8_t proto = 0;
2367 struct bgp_pbr_range_port *srcp = NULL, *dstp = NULL;
932404b7 2368 struct bgp_pbr_range_port range, range_icmp_code;
83360720 2369 struct bgp_pbr_range_port pkt_len;
0e867886 2370 struct bgp_pbr_filter bpf;
c5ee26cc
PG
2371 uint8_t kind_enum;
2372 struct bgp_pbr_or_filter bpof;
9bba1455 2373 struct bgp_pbr_val_mask bpvm;
d114b0d7 2374
8cda9106 2375 memset(&nh, 0, sizeof(struct nexthop));
0e867886 2376 memset(&bpf, 0, sizeof(struct bgp_pbr_filter));
c5ee26cc 2377 memset(&bpof, 0, sizeof(struct bgp_pbr_or_filter));
9350f1df
PG
2378 if (api->match_bitmask & PREFIX_SRC_PRESENT ||
2379 (api->type == BGP_PBR_IPRULE &&
2380 api->match_bitmask_iprule & PREFIX_SRC_PRESENT))
d114b0d7 2381 src = &api->src_prefix;
9350f1df
PG
2382 if (api->match_bitmask & PREFIX_DST_PRESENT ||
2383 (api->type == BGP_PBR_IPRULE &&
2384 api->match_bitmask_iprule & PREFIX_DST_PRESENT))
d114b0d7 2385 dst = &api->dst_prefix;
9350f1df
PG
2386 if (api->type == BGP_PBR_IPRULE)
2387 bpf.type = api->type;
d114b0d7
PG
2388 memset(&nh, 0, sizeof(struct nexthop));
2389 nh.vrf_id = VRF_UNKNOWN;
1de7dfff
PG
2390 if (api->match_protocol_num)
2391 proto = (uint8_t)api->protocol[0].value;
2392 /* if match_port is selected, then either src or dst port will be parsed
2393 * but not both at the same time
2394 */
2395 if (api->match_port_num >= 1) {
2396 bgp_pbr_extract(api->port,
2397 api->match_port_num,
2398 &range);
2399 srcp = dstp = &range;
2400 } else if (api->match_src_port_num >= 1) {
2401 bgp_pbr_extract(api->src_port,
2402 api->match_src_port_num,
2403 &range);
2404 srcp = &range;
2405 dstp = NULL;
2406 } else if (api->match_dst_port_num >= 1) {
2407 bgp_pbr_extract(api->dst_port,
2408 api->match_dst_port_num,
2409 &range);
2410 dstp = &range;
2411 srcp = NULL;
2412 }
932404b7
PG
2413 if (api->match_icmp_type_num >= 1) {
2414 proto = IPPROTO_ICMP;
da3fa383
PG
2415 if (bgp_pbr_extract(api->icmp_type,
2416 api->match_icmp_type_num,
2417 &range))
932404b7 2418 srcp = &range;
da3fa383
PG
2419 else {
2420 bpof.icmp_type = list_new();
2421 bgp_pbr_extract_enumerate(api->icmp_type,
2422 api->match_icmp_type_num,
2423 OPERATOR_UNARY_OR,
2424 bpof.icmp_type,
2425 FLOWSPEC_ICMP_TYPE);
932404b7
PG
2426 }
2427 }
2428 if (api->match_icmp_code_num >= 1) {
2429 proto = IPPROTO_ICMP;
da3fa383
PG
2430 if (bgp_pbr_extract(api->icmp_code,
2431 api->match_icmp_code_num,
2432 &range_icmp_code))
932404b7 2433 dstp = &range_icmp_code;
da3fa383
PG
2434 else {
2435 bpof.icmp_code = list_new();
2436 bgp_pbr_extract_enumerate(api->icmp_code,
2437 api->match_icmp_code_num,
2438 OPERATOR_UNARY_OR,
2439 bpof.icmp_code,
2440 FLOWSPEC_ICMP_CODE);
932404b7
PG
2441 }
2442 }
2da7d62e 2443
c5ee26cc
PG
2444 if (api->match_tcpflags_num) {
2445 kind_enum = bgp_pbr_match_val_get_operator(api->tcpflags,
2446 api->match_tcpflags_num);
2447 if (kind_enum == OPERATOR_UNARY_AND) {
9bba1455 2448 bpf.tcp_flags = &bpvm;
c5ee26cc
PG
2449 bgp_pbr_extract_enumerate(api->tcpflags,
2450 api->match_tcpflags_num,
35703998
PG
2451 OPERATOR_UNARY_AND,
2452 bpf.tcp_flags,
2453 FLOWSPEC_TCP_FLAGS);
c5ee26cc
PG
2454 } else if (kind_enum == OPERATOR_UNARY_OR) {
2455 bpof.tcpflags = list_new();
2456 bgp_pbr_extract_enumerate(api->tcpflags,
2457 api->match_tcpflags_num,
35703998
PG
2458 OPERATOR_UNARY_OR,
2459 bpof.tcpflags,
2460 FLOWSPEC_TCP_FLAGS);
c5ee26cc
PG
2461 }
2462 }
cfaf18ce
PG
2463 if (api->match_packet_length_num) {
2464 bool ret;
2465
2466 ret = bgp_pbr_extract(api->packet_length,
2467 api->match_packet_length_num,
2468 &pkt_len);
2469 if (ret)
2470 bpf.pkt_len = &pkt_len;
2471 else {
2472 bpof.pkt_len = list_new();
2473 bgp_pbr_extract_enumerate(api->packet_length,
2474 api->match_packet_length_num,
2475 OPERATOR_UNARY_OR,
2476 bpof.pkt_len,
2477 FLOWSPEC_PKT_LEN);
2478 }
2da7d62e 2479 }
4977bd6c 2480 if (api->match_dscp_num >= 1) {
35703998
PG
2481 bpof.dscp = list_new();
2482 bgp_pbr_extract_enumerate(api->dscp, api->match_dscp_num,
2483 OPERATOR_UNARY_OR,
2484 bpof.dscp, FLOWSPEC_DSCP);
4977bd6c 2485 }
6f5617d8
PG
2486 if (api->match_fragment_num) {
2487 bpof.fragment = list_new();
2488 bgp_pbr_extract_enumerate(api->fragment,
2489 api->match_fragment_num,
2490 OPERATOR_UNARY_OR,
2491 bpof.fragment,
2492 FLOWSPEC_FRAGMENT);
2493 }
0e867886
PG
2494 bpf.vrf_id = api->vrf_id;
2495 bpf.src = src;
2496 bpf.dst = dst;
2497 bpf.protocol = proto;
0e867886
PG
2498 bpf.src_port = srcp;
2499 bpf.dst_port = dstp;
d90b788e
A
2500 if (!add) {
2501 bgp_pbr_policyroute_remove_from_zebra(bgp, path, &bpf, &bpof);
2502 return;
2503 }
d114b0d7
PG
2504 /* no action for add = true */
2505 for (i = 0; i < api->action_num; i++) {
2506 switch (api->actions[i].action) {
2507 case ACTION_TRAFFICRATE:
2508 /* drop packet */
2509 if (api->actions[i].u.r.rate == 0) {
2510 nh.vrf_id = api->vrf_id;
2511 nh.type = NEXTHOP_TYPE_BLACKHOLE;
9b6d8fcf
DS
2512 bgp_pbr_policyroute_add_to_zebra(
2513 bgp, path, &bpf, &bpof, &nh, &rate);
d114b0d7
PG
2514 } else {
2515 /* update rate. can be reentrant */
2516 rate = api->actions[i].u.r.rate;
ac7c35f8 2517 if (BGP_DEBUG(pbr, PBR)) {
d114b0d7 2518 bgp_pbr_print_policy_route(api);
ac7c35f8
PG
2519 zlog_warn("PBR: ignoring Set action rate %f",
2520 api->actions[i].u.r.rate);
2521 }
d114b0d7
PG
2522 }
2523 break;
2524 case ACTION_TRAFFIC_ACTION:
2525 if (api->actions[i].u.za.filter
2526 & TRAFFIC_ACTION_SAMPLE) {
ac7c35f8 2527 if (BGP_DEBUG(pbr, PBR)) {
d114b0d7 2528 bgp_pbr_print_policy_route(api);
ac7c35f8
PG
2529 zlog_warn("PBR: Sample action Ignored");
2530 }
d114b0d7
PG
2531 }
2532#if 0
2533 if (api->actions[i].u.za.filter
2534 & TRAFFIC_ACTION_DISTRIBUTE) {
ac7c35f8 2535 if (BGP_DEBUG(pbr, PBR)) {
d114b0d7 2536 bgp_pbr_print_policy_route(api);
ac7c35f8
PG
2537 zlog_warn("PBR: Distribute action Applies");
2538 }
d114b0d7
PG
2539 continue_loop = 0;
2540 /* continue forwarding entry as before
2541 * no action
2542 */
2543 }
2544#endif /* XXX to confirm behaviour of traffic action. for now , ignore */
2545 /* terminate action: run other filters
2546 */
2547 break;
2548 case ACTION_REDIRECT_IP:
2549 nh.type = NEXTHOP_TYPE_IPV4;
2550 nh.gate.ipv4.s_addr =
2551 api->actions[i].u.zr.redirect_ip_v4.s_addr;
2552 nh.vrf_id = api->vrf_id;
9b6d8fcf 2553 bgp_pbr_policyroute_add_to_zebra(bgp, path, &bpf, &bpof,
da3fa383 2554 &nh, &rate);
d114b0d7
PG
2555 /* XXX combination with REDIRECT_VRF
2556 * + REDIRECT_NH_IP not done
2557 */
2558 continue_loop = 0;
2559 break;
2560 case ACTION_REDIRECT:
2561 nh.vrf_id = api->actions[i].u.redirect_vrf;
2562 nh.type = NEXTHOP_TYPE_IPV4;
9b6d8fcf 2563 bgp_pbr_policyroute_add_to_zebra(bgp, path, &bpf, &bpof,
da3fa383 2564 &nh, &rate);
d114b0d7
PG
2565 continue_loop = 0;
2566 break;
2567 case ACTION_MARKING:
ac7c35f8 2568 if (BGP_DEBUG(pbr, PBR)) {
d114b0d7 2569 bgp_pbr_print_policy_route(api);
ac7c35f8
PG
2570 zlog_warn("PBR: Set DSCP %u Ignored",
2571 api->actions[i].u.marking_dscp);
2572 }
d114b0d7
PG
2573 break;
2574 default:
2575 break;
2576 }
2577 if (continue_loop == 0)
2578 break;
2579 }
2580}
2581
45918cfb 2582void bgp_pbr_update_entry(struct bgp *bgp, struct prefix *p,
4b7e6066
DS
2583 struct bgp_path_info *info, afi_t afi, safi_t safi,
2584 bool nlri_update)
45918cfb
PG
2585{
2586 struct bgp_pbr_entry_main api;
2587
2588 if (afi == AFI_IP6)
2589 return; /* IPv6 not supported */
2590 if (safi != SAFI_FLOWSPEC)
2591 return; /* not supported */
2592 /* Make Zebra API structure. */
2593 memset(&api, 0, sizeof(api));
2594 api.vrf_id = bgp->vrf_id;
2595 api.afi = afi;
2596
6818e7e5 2597 if (!bgp_zebra_tm_chunk_obtained()) {
f146bb54 2598 if (BGP_DEBUG(pbr, PBR_ERROR))
e50f7cfd 2599 flog_err(EC_BGP_TABLE_CHUNK,
1c50c1c0 2600 "%s: table chunk not obtained yet", __func__);
6818e7e5
PG
2601 return;
2602 }
6818e7e5
PG
2603
2604 if (bgp_pbr_build_and_validate_entry(p, info, &api) < 0) {
2605 if (BGP_DEBUG(pbr, PBR_ERROR))
e50f7cfd 2606 flog_err(EC_BGP_FLOWSPEC_INSTALLATION,
1c50c1c0
QY
2607 "%s: cancel updating entry %p in bgp pbr",
2608 __func__, info);
45918cfb
PG
2609 return;
2610 }
d114b0d7 2611 bgp_pbr_handle_entry(bgp, info, &api, nlri_update);
45918cfb 2612}
4762c213
PG
2613
2614int bgp_pbr_interface_compare(const struct bgp_pbr_interface *a,
2615 const struct bgp_pbr_interface *b)
2616{
2617 return strcmp(a->name, b->name);
2618}
2619
2620struct bgp_pbr_interface *bgp_pbr_interface_lookup(const char *name,
2621 struct bgp_pbr_interface_head *head)
2622{
2623 struct bgp_pbr_interface pbr_if;
2624
2625 strlcpy(pbr_if.name, name, sizeof(pbr_if.name));
2626 return (RB_FIND(bgp_pbr_interface_head,
2627 head, &pbr_if));
2628}
2629
2630/* this function resets to the default policy routing
2631 * go back to default status
2632 */
2633void bgp_pbr_reset(struct bgp *bgp, afi_t afi)
2634{
2635 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
2636 struct bgp_pbr_interface_head *head;
2637 struct bgp_pbr_interface *pbr_if;
2638
2639 if (!bgp_pbr_cfg || afi != AFI_IP)
2640 return;
2641 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
2642
2643 while (!RB_EMPTY(bgp_pbr_interface_head, head)) {
2644 pbr_if = RB_ROOT(bgp_pbr_interface_head, head);
2645 RB_REMOVE(bgp_pbr_interface_head, head, pbr_if);
2646 XFREE(MTYPE_TMP, pbr_if);
2647 }
2648}