]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_routemap.c
Merge pull request #8003 from donaldsharp/timings
[mirror_frr.git] / bgpd / bgp_routemap.c
1 /* Route map function of bgpd.
2 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "filter.h"
25 #include "routemap.h"
26 #include "command.h"
27 #include "linklist.h"
28 #include "plist.h"
29 #include "memory.h"
30 #include "log.h"
31 #include "frrlua.h"
32 #ifdef HAVE_LIBPCREPOSIX
33 #include <pcreposix.h>
34 #else
35 #include <regex.h>
36 #endif /* HAVE_LIBPCREPOSIX */
37 #include "buffer.h"
38 #include "sockunion.h"
39 #include "hash.h"
40 #include "queue.h"
41 #include "frrstr.h"
42 #include "network.h"
43 #include "lib/northbound_cli.h"
44
45 #include "bgpd/bgpd.h"
46 #include "bgpd/bgp_table.h"
47 #include "bgpd/bgp_attr.h"
48 #include "bgpd/bgp_aspath.h"
49 #include "bgpd/bgp_packet.h"
50 #include "bgpd/bgp_route.h"
51 #include "bgpd/bgp_zebra.h"
52 #include "bgpd/bgp_regex.h"
53 #include "bgpd/bgp_community.h"
54 #include "bgpd/bgp_clist.h"
55 #include "bgpd/bgp_filter.h"
56 #include "bgpd/bgp_mplsvpn.h"
57 #include "bgpd/bgp_ecommunity.h"
58 #include "bgpd/bgp_lcommunity.h"
59 #include "bgpd/bgp_vty.h"
60 #include "bgpd/bgp_debug.h"
61 #include "bgpd/bgp_evpn.h"
62 #include "bgpd/bgp_evpn_private.h"
63 #include "bgpd/bgp_evpn_vty.h"
64 #include "bgpd/bgp_mplsvpn.h"
65 #include "bgpd/bgp_pbr.h"
66 #include "bgpd/bgp_flowspec_util.h"
67 #include "bgpd/bgp_encap_types.h"
68 #include "bgpd/bgp_mpath.h"
69 #include "bgpd/bgp_script.h"
70
71 #ifdef ENABLE_BGP_VNC
72 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
73 #endif
74
75 #ifndef VTYSH_EXTRACT_PL
76 #include "bgpd/bgp_routemap_clippy.c"
77 #endif
78
79 /* Memo of route-map commands.
80
81 o Cisco route-map
82
83 match as-path : Done
84 community : Done
85 interface : Done
86 ip address : Done
87 ip next-hop : Done
88 ip route-source : Done
89 ip prefix-list : Done
90 ipv6 address : Done
91 ipv6 next-hop : Done
92 ipv6 route-source: (This will not be implemented by bgpd)
93 ipv6 prefix-list : Done
94 length : (This will not be implemented by bgpd)
95 metric : Done
96 route-type : (This will not be implemented by bgpd)
97 tag : Done
98 local-preference : Done
99
100 set as-path prepend : Done
101 as-path tag : Not yet
102 automatic-tag : (This will not be implemented by bgpd)
103 community : Done
104 large-community : Done
105 large-comm-list : Done
106 comm-list : Not yet
107 dampning : Not yet
108 default : (This will not be implemented by bgpd)
109 interface : (This will not be implemented by bgpd)
110 ip default : (This will not be implemented by bgpd)
111 ip next-hop : Done
112 ip precedence : (This will not be implemented by bgpd)
113 ip tos : (This will not be implemented by bgpd)
114 level : (This will not be implemented by bgpd)
115 local-preference : Done
116 metric : Done
117 metric-type : Not yet
118 origin : Done
119 tag : Done
120 weight : Done
121 table : Done
122
123 o Local extensions
124
125 set ipv6 next-hop global: Done
126 set ipv6 next-hop prefer-global: Done
127 set ipv6 next-hop local : Done
128 set as-path exclude : Done
129
130 */
131
132 /* generic value manipulation to be shared in multiple rules */
133
134 #define RMAP_VALUE_SET 0
135 #define RMAP_VALUE_ADD 1
136 #define RMAP_VALUE_SUB 2
137
138 struct rmap_value {
139 uint8_t action;
140 uint8_t variable;
141 uint32_t value;
142 };
143
144 static int route_value_match(struct rmap_value *rv, uint32_t value)
145 {
146 if (rv->variable == 0 && value == rv->value)
147 return RMAP_MATCH;
148
149 return RMAP_NOMATCH;
150 }
151
152 static uint32_t route_value_adjust(struct rmap_value *rv, uint32_t current,
153 struct peer *peer)
154 {
155 uint32_t value;
156
157 switch (rv->variable) {
158 case 1:
159 value = peer->rtt;
160 break;
161 default:
162 value = rv->value;
163 break;
164 }
165
166 switch (rv->action) {
167 case RMAP_VALUE_ADD:
168 if (current > UINT32_MAX - value)
169 return UINT32_MAX;
170 return current + value;
171 case RMAP_VALUE_SUB:
172 if (current <= value)
173 return 0;
174 return current - value;
175 default:
176 return value;
177 }
178 }
179
180 static void *route_value_compile(const char *arg)
181 {
182 uint8_t action = RMAP_VALUE_SET, var = 0;
183 unsigned long larg = 0;
184 char *endptr = NULL;
185 struct rmap_value *rv;
186
187 if (arg[0] == '+') {
188 action = RMAP_VALUE_ADD;
189 arg++;
190 } else if (arg[0] == '-') {
191 action = RMAP_VALUE_SUB;
192 arg++;
193 }
194
195 if (all_digit(arg)) {
196 errno = 0;
197 larg = strtoul(arg, &endptr, 10);
198 if (*arg == 0 || *endptr != 0 || errno || larg > UINT32_MAX)
199 return NULL;
200 } else {
201 if (strcmp(arg, "rtt") == 0)
202 var = 1;
203 else
204 return NULL;
205 }
206
207 rv = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_value));
208
209 rv->action = action;
210 rv->variable = var;
211 rv->value = larg;
212 return rv;
213 }
214
215 static void route_value_free(void *rule)
216 {
217 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
218 }
219
220 /* generic as path object to be shared in multiple rules */
221
222 static void *route_aspath_compile(const char *arg)
223 {
224 struct aspath *aspath;
225
226 aspath = aspath_str2aspath(arg);
227 if (!aspath)
228 return NULL;
229 return aspath;
230 }
231
232 static void route_aspath_free(void *rule)
233 {
234 struct aspath *aspath = rule;
235 aspath_free(aspath);
236 }
237
238 struct bgp_match_peer_compiled {
239 char *interface;
240 union sockunion su;
241 };
242
243 /* 'match peer (A.B.C.D|X:X::X:X|WORD)' */
244
245 /* Compares the peer specified in the 'match peer' clause with the peer
246 received in bgp_path_info->peer. If it is the same, or if the peer structure
247 received is a peer_group containing it, returns RMAP_MATCH. */
248 static enum route_map_cmd_result_t
249 route_match_peer(void *rule, const struct prefix *prefix, void *object)
250 {
251 struct bgp_match_peer_compiled *pc;
252 union sockunion *su;
253 union sockunion su_def = {
254 .sin = {.sin_family = AF_INET, .sin_addr.s_addr = INADDR_ANY}};
255 struct peer_group *group;
256 struct peer *peer;
257 struct listnode *node, *nnode;
258
259 pc = rule;
260 su = &pc->su;
261 peer = ((struct bgp_path_info *)object)->peer;
262
263 if (pc->interface) {
264 if (!peer->conf_if)
265 return RMAP_NOMATCH;
266
267 if (strcmp(peer->conf_if, pc->interface) == 0)
268 return RMAP_MATCH;
269
270 return RMAP_NOMATCH;
271 }
272
273 /* If su='0.0.0.0' (command 'match peer local'), and it's a
274 NETWORK,
275 REDISTRIBUTE, AGGREGATE-ADDRESS or DEFAULT_GENERATED route
276 => return RMAP_MATCH
277 */
278 if (sockunion_same(su, &su_def)) {
279 int ret;
280 if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_NETWORK)
281 || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_REDISTRIBUTE)
282 || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_AGGREGATE)
283 || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_DEFAULT))
284 ret = RMAP_MATCH;
285 else
286 ret = RMAP_NOMATCH;
287 return ret;
288 }
289
290 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
291 if (sockunion_same(su, &peer->su))
292 return RMAP_MATCH;
293
294 return RMAP_NOMATCH;
295 } else {
296 group = peer->group;
297 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
298 if (sockunion_same(su, &peer->su))
299 return RMAP_MATCH;
300 }
301 return RMAP_NOMATCH;
302 }
303
304 return RMAP_NOMATCH;
305 }
306
307 static void *route_match_peer_compile(const char *arg)
308 {
309 struct bgp_match_peer_compiled *pc;
310 int ret;
311
312 pc = XCALLOC(MTYPE_ROUTE_MAP_COMPILED,
313 sizeof(struct bgp_match_peer_compiled));
314
315 ret = str2sockunion(strcmp(arg, "local") ? arg : "0.0.0.0", &pc->su);
316 if (ret < 0) {
317 pc->interface = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
318 return pc;
319 }
320
321 return pc;
322 }
323
324 /* Free route map's compiled `ip address' value. */
325 static void route_match_peer_free(void *rule)
326 {
327 struct bgp_match_peer_compiled *pc = rule;
328
329 XFREE(MTYPE_ROUTE_MAP_COMPILED, pc->interface);
330
331 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
332 }
333
334 /* Route map commands for ip address matching. */
335 static const struct route_map_rule_cmd route_match_peer_cmd = {
336 "peer",
337 route_match_peer,
338 route_match_peer_compile,
339 route_match_peer_free
340 };
341
342 #ifdef HAVE_SCRIPTING
343
344 enum frrlua_rm_status {
345 /*
346 * Script function run failure. This will translate into a deny
347 */
348 LUA_RM_FAILURE = 0,
349 /*
350 * No Match was found for the route map function
351 */
352 LUA_RM_NOMATCH,
353 /*
354 * Match was found but no changes were made to the incoming data.
355 */
356 LUA_RM_MATCH,
357 /*
358 * Match was found and data was modified, so figure out what changed
359 */
360 LUA_RM_MATCH_AND_CHANGE,
361 };
362
363 static enum route_map_cmd_result_t
364 route_match_script(void *rule, const struct prefix *prefix, void *object)
365 {
366 const char *scriptname = rule;
367 struct bgp_path_info *path = (struct bgp_path_info *)object;
368
369 struct frrscript *fs = frrscript_load(scriptname, NULL);
370
371 if (!fs) {
372 zlog_err("Issue loading script rule; defaulting to no match");
373 return RMAP_NOMATCH;
374 }
375
376 enum frrlua_rm_status status_failure = LUA_RM_FAILURE,
377 status_nomatch = LUA_RM_NOMATCH,
378 status_match = LUA_RM_MATCH,
379 status_match_and_change = LUA_RM_MATCH_AND_CHANGE;
380
381 /* Make result values available */
382 struct frrscript_env env[] = {
383 {"integer", "RM_FAILURE", &status_failure},
384 {"integer", "RM_NOMATCH", &status_nomatch},
385 {"integer", "RM_MATCH", &status_match},
386 {"integer", "RM_MATCH_AND_CHANGE", &status_match_and_change},
387 {"integer", "action", &status_failure},
388 {"prefix", "prefix", prefix},
389 {"attr", "attributes", path->attr},
390 {"peer", "peer", path->peer},
391 {}};
392
393 struct frrscript_env results[] = {
394 {"integer", "action"},
395 {"attr", "attributes"},
396 {},
397 };
398
399 int result = frrscript_call(fs, env);
400
401 if (result) {
402 zlog_err("Issue running script rule; defaulting to no match");
403 return RMAP_NOMATCH;
404 }
405
406 enum frrlua_rm_status *lrm_status =
407 frrscript_get_result(fs, &results[0]);
408
409 int status = RMAP_NOMATCH;
410
411 switch (*lrm_status) {
412 case LUA_RM_FAILURE:
413 zlog_err(
414 "Executing route-map match script '%s' failed; defaulting to no match",
415 scriptname);
416 status = RMAP_NOMATCH;
417 break;
418 case LUA_RM_NOMATCH:
419 status = RMAP_NOMATCH;
420 break;
421 case LUA_RM_MATCH_AND_CHANGE:
422 status = RMAP_MATCH;
423 zlog_debug("Updating attribute based on script's values");
424
425 uint32_t locpref = 0;
426 struct attr *newattr = frrscript_get_result(fs, &results[1]);
427
428 path->attr->med = newattr->med;
429
430 if (path->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
431 locpref = path->attr->local_pref;
432 if (locpref != newattr->local_pref) {
433 SET_FLAG(path->attr->flag,
434 ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF));
435 path->attr->local_pref = newattr->local_pref;
436 }
437
438 aspath_free(newattr->aspath);
439 XFREE(MTYPE_TMP, newattr);
440 break;
441 case LUA_RM_MATCH:
442 status = RMAP_MATCH;
443 break;
444 }
445
446 XFREE(MTYPE_TMP, lrm_status);
447 frrscript_unload(fs);
448
449 return status;
450 }
451
452 static void *route_match_script_compile(const char *arg)
453 {
454 char *scriptname;
455
456 scriptname = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
457
458 return scriptname;
459 }
460
461 static void route_match_script_free(void *rule)
462 {
463 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
464 }
465
466 static const struct route_map_rule_cmd route_match_script_cmd = {
467 "script",
468 route_match_script,
469 route_match_script_compile,
470 route_match_script_free
471 };
472
473 #endif /* HAVE_SCRIPTING */
474
475 /* `match ip address IP_ACCESS_LIST' */
476
477 /* Match function should return 1 if match is success else return
478 zero. */
479 static enum route_map_cmd_result_t
480 route_match_ip_address(void *rule, const struct prefix *prefix, void *object)
481 {
482 struct access_list *alist;
483
484 if (prefix->family == AF_INET) {
485 alist = access_list_lookup(AFI_IP, (char *)rule);
486 if (alist == NULL)
487 return RMAP_NOMATCH;
488
489 return (access_list_apply(alist, prefix) == FILTER_DENY
490 ? RMAP_NOMATCH
491 : RMAP_MATCH);
492 }
493 return RMAP_NOMATCH;
494 }
495
496 /* Route map `ip address' match statement. `arg' should be
497 access-list name. */
498 static void *route_match_ip_address_compile(const char *arg)
499 {
500 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
501 }
502
503 /* Free route map's compiled `ip address' value. */
504 static void route_match_ip_address_free(void *rule)
505 {
506 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
507 }
508
509 /* Route map commands for ip address matching. */
510 static const struct route_map_rule_cmd route_match_ip_address_cmd = {
511 "ip address",
512 route_match_ip_address,
513 route_match_ip_address_compile,
514 route_match_ip_address_free
515 };
516
517 /* `match ip next-hop IP_ADDRESS' */
518
519 /* Match function return 1 if match is success else return zero. */
520 static enum route_map_cmd_result_t
521 route_match_ip_next_hop(void *rule, const struct prefix *prefix, void *object)
522 {
523 struct access_list *alist;
524 struct bgp_path_info *path;
525 struct prefix_ipv4 p;
526
527 if (prefix->family == AF_INET) {
528 path = object;
529 p.family = AF_INET;
530 p.prefix = path->attr->nexthop;
531 p.prefixlen = IPV4_MAX_BITLEN;
532
533 alist = access_list_lookup(AFI_IP, (char *)rule);
534 if (alist == NULL)
535 return RMAP_NOMATCH;
536
537 return (access_list_apply(alist, &p) == FILTER_DENY
538 ? RMAP_NOMATCH
539 : RMAP_MATCH);
540 }
541 return RMAP_NOMATCH;
542 }
543
544 /* Route map `ip next-hop' match statement. `arg' is
545 access-list name. */
546 static void *route_match_ip_next_hop_compile(const char *arg)
547 {
548 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
549 }
550
551 /* Free route map's compiled `ip address' value. */
552 static void route_match_ip_next_hop_free(void *rule)
553 {
554 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
555 }
556
557 /* Route map commands for ip next-hop matching. */
558 static const struct route_map_rule_cmd route_match_ip_next_hop_cmd = {
559 "ip next-hop",
560 route_match_ip_next_hop,
561 route_match_ip_next_hop_compile,
562 route_match_ip_next_hop_free
563 };
564
565 /* `match ip route-source ACCESS-LIST' */
566
567 /* Match function return 1 if match is success else return zero. */
568 static enum route_map_cmd_result_t
569 route_match_ip_route_source(void *rule, const struct prefix *pfx, void *object)
570 {
571 struct access_list *alist;
572 struct bgp_path_info *path;
573 struct peer *peer;
574 struct prefix_ipv4 p;
575
576 if (pfx->family == AF_INET) {
577 path = object;
578 peer = path->peer;
579
580 if (!peer || sockunion_family(&peer->su) != AF_INET)
581 return RMAP_NOMATCH;
582
583 p.family = AF_INET;
584 p.prefix = peer->su.sin.sin_addr;
585 p.prefixlen = IPV4_MAX_BITLEN;
586
587 alist = access_list_lookup(AFI_IP, (char *)rule);
588 if (alist == NULL)
589 return RMAP_NOMATCH;
590
591 return (access_list_apply(alist, &p) == FILTER_DENY
592 ? RMAP_NOMATCH
593 : RMAP_MATCH);
594 }
595 return RMAP_NOMATCH;
596 }
597
598 /* Route map `ip route-source' match statement. `arg' is
599 access-list name. */
600 static void *route_match_ip_route_source_compile(const char *arg)
601 {
602 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
603 }
604
605 /* Free route map's compiled `ip address' value. */
606 static void route_match_ip_route_source_free(void *rule)
607 {
608 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
609 }
610
611 /* Route map commands for ip route-source matching. */
612 static const struct route_map_rule_cmd route_match_ip_route_source_cmd = {
613 "ip route-source",
614 route_match_ip_route_source,
615 route_match_ip_route_source_compile,
616 route_match_ip_route_source_free
617 };
618
619 static enum route_map_cmd_result_t
620 route_match_prefix_list_flowspec(afi_t afi, struct prefix_list *plist,
621 const struct prefix *p)
622 {
623 int ret;
624 struct bgp_pbr_entry_main api;
625
626 memset(&api, 0, sizeof(api));
627
628 if (family2afi(p->u.prefix_flowspec.family) != afi)
629 return RMAP_NOMATCH;
630
631 /* extract match from flowspec entries */
632 ret = bgp_flowspec_match_rules_fill(
633 (uint8_t *)p->u.prefix_flowspec.ptr,
634 p->u.prefix_flowspec.prefixlen, &api,
635 afi);
636 if (ret < 0)
637 return RMAP_NOMATCH;
638 if (api.match_bitmask & PREFIX_DST_PRESENT ||
639 api.match_bitmask_iprule & PREFIX_DST_PRESENT) {
640 if (family2afi((&api.dst_prefix)->family) != afi)
641 return RMAP_NOMATCH;
642 return prefix_list_apply(plist, &api.dst_prefix) == PREFIX_DENY
643 ? RMAP_NOMATCH
644 : RMAP_MATCH;
645 } else if (api.match_bitmask & PREFIX_SRC_PRESENT ||
646 api.match_bitmask_iprule & PREFIX_SRC_PRESENT) {
647 if (family2afi((&api.src_prefix)->family) != afi)
648 return RMAP_NOMATCH;
649 return (prefix_list_apply(plist, &api.src_prefix) == PREFIX_DENY
650 ? RMAP_NOMATCH
651 : RMAP_MATCH);
652 }
653 return RMAP_NOMATCH;
654 }
655
656 static enum route_map_cmd_result_t
657 route_match_address_prefix_list(void *rule, afi_t afi,
658 const struct prefix *prefix, void *object)
659 {
660 struct prefix_list *plist;
661
662 plist = prefix_list_lookup(afi, (char *)rule);
663 if (plist == NULL)
664 return RMAP_NOMATCH;
665
666 if (prefix->family == AF_FLOWSPEC)
667 return route_match_prefix_list_flowspec(afi, plist,
668 prefix);
669 return (prefix_list_apply(plist, prefix) == PREFIX_DENY ? RMAP_NOMATCH
670 : RMAP_MATCH);
671 }
672
673 static enum route_map_cmd_result_t
674 route_match_ip_address_prefix_list(void *rule, const struct prefix *prefix,
675 void *object)
676 {
677 return route_match_address_prefix_list(rule, AFI_IP, prefix, object);
678 }
679
680 static void *route_match_ip_address_prefix_list_compile(const char *arg)
681 {
682 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
683 }
684
685 static void route_match_ip_address_prefix_list_free(void *rule)
686 {
687 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
688 }
689
690 static const struct route_map_rule_cmd
691 route_match_ip_address_prefix_list_cmd = {
692 "ip address prefix-list",
693 route_match_ip_address_prefix_list,
694 route_match_ip_address_prefix_list_compile,
695 route_match_ip_address_prefix_list_free
696 };
697
698 /* `match ip next-hop prefix-list PREFIX_LIST' */
699
700 static enum route_map_cmd_result_t
701 route_match_ip_next_hop_prefix_list(void *rule, const struct prefix *prefix,
702 void *object)
703 {
704 struct prefix_list *plist;
705 struct bgp_path_info *path;
706 struct prefix_ipv4 p;
707
708 if (prefix->family == AF_INET) {
709 path = object;
710 p.family = AF_INET;
711 p.prefix = path->attr->nexthop;
712 p.prefixlen = IPV4_MAX_BITLEN;
713
714 plist = prefix_list_lookup(AFI_IP, (char *)rule);
715 if (plist == NULL)
716 return RMAP_NOMATCH;
717
718 return (prefix_list_apply(plist, &p) == PREFIX_DENY
719 ? RMAP_NOMATCH
720 : RMAP_MATCH);
721 }
722 return RMAP_NOMATCH;
723 }
724
725 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg)
726 {
727 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
728 }
729
730 static void route_match_ip_next_hop_prefix_list_free(void *rule)
731 {
732 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
733 }
734
735 static const struct route_map_rule_cmd
736 route_match_ip_next_hop_prefix_list_cmd = {
737 "ip next-hop prefix-list",
738 route_match_ip_next_hop_prefix_list,
739 route_match_ip_next_hop_prefix_list_compile,
740 route_match_ip_next_hop_prefix_list_free
741 };
742
743 /* `match ip next-hop type <blackhole>' */
744
745 static enum route_map_cmd_result_t
746 route_match_ip_next_hop_type(void *rule, const struct prefix *prefix,
747 void *object)
748 {
749 struct bgp_path_info *path;
750
751 if (prefix->family == AF_INET) {
752 path = (struct bgp_path_info *)object;
753 if (!path)
754 return RMAP_NOMATCH;
755
756 /* If nexthop interface's index can't be resolved and nexthop is
757 set to any address then mark it as type `blackhole`.
758 This logic works for matching kernel/static routes like:
759 `ip route add blackhole 10.0.0.1`. */
760 if (path->attr->nexthop.s_addr == INADDR_ANY
761 && !path->attr->nh_ifindex)
762 return RMAP_MATCH;
763 }
764 return RMAP_NOMATCH;
765 }
766
767 static void *route_match_ip_next_hop_type_compile(const char *arg)
768 {
769 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
770 }
771
772 static void route_match_ip_next_hop_type_free(void *rule)
773 {
774 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
775 }
776
777 static const struct route_map_rule_cmd
778 route_match_ip_next_hop_type_cmd = {
779 "ip next-hop type",
780 route_match_ip_next_hop_type,
781 route_match_ip_next_hop_type_compile,
782 route_match_ip_next_hop_type_free
783 };
784
785 /* `match ip route-source prefix-list PREFIX_LIST' */
786
787 static enum route_map_cmd_result_t
788 route_match_ip_route_source_prefix_list(void *rule, const struct prefix *prefix,
789 void *object)
790 {
791 struct prefix_list *plist;
792 struct bgp_path_info *path;
793 struct peer *peer;
794 struct prefix_ipv4 p;
795
796 if (prefix->family == AF_INET) {
797 path = object;
798 peer = path->peer;
799
800 if (!peer || sockunion_family(&peer->su) != AF_INET)
801 return RMAP_NOMATCH;
802
803 p.family = AF_INET;
804 p.prefix = peer->su.sin.sin_addr;
805 p.prefixlen = IPV4_MAX_BITLEN;
806
807 plist = prefix_list_lookup(AFI_IP, (char *)rule);
808 if (plist == NULL)
809 return RMAP_NOMATCH;
810
811 return (prefix_list_apply(plist, &p) == PREFIX_DENY
812 ? RMAP_NOMATCH
813 : RMAP_MATCH);
814 }
815 return RMAP_NOMATCH;
816 }
817
818 static void *route_match_ip_route_source_prefix_list_compile(const char *arg)
819 {
820 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
821 }
822
823 static void route_match_ip_route_source_prefix_list_free(void *rule)
824 {
825 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
826 }
827
828 static const struct route_map_rule_cmd
829 route_match_ip_route_source_prefix_list_cmd = {
830 "ip route-source prefix-list",
831 route_match_ip_route_source_prefix_list,
832 route_match_ip_route_source_prefix_list_compile,
833 route_match_ip_route_source_prefix_list_free
834 };
835
836 /* `match evpn default-route' */
837
838 /* Match function should return 1 if match is success else 0 */
839 static enum route_map_cmd_result_t
840 route_match_evpn_default_route(void *rule, const struct prefix *p, void *object)
841 {
842 if (is_evpn_prefix_default(p))
843 return RMAP_MATCH;
844
845 return RMAP_NOMATCH;
846 }
847
848 /* Route map commands for default-route matching. */
849 static const struct route_map_rule_cmd
850 route_match_evpn_default_route_cmd = {
851 "evpn default-route",
852 route_match_evpn_default_route,
853 NULL,
854 NULL
855 };
856
857 /* `match mac address MAC_ACCESS_LIST' */
858
859 /* Match function should return 1 if match is success else return
860 zero. */
861 static enum route_map_cmd_result_t
862 route_match_mac_address(void *rule, const struct prefix *prefix, void *object)
863 {
864 struct access_list *alist;
865 struct prefix p;
866
867 alist = access_list_lookup(AFI_L2VPN, (char *)rule);
868 if (alist == NULL)
869 return RMAP_NOMATCH;
870
871 if (prefix->u.prefix_evpn.route_type != BGP_EVPN_MAC_IP_ROUTE)
872 return RMAP_NOMATCH;
873
874 p.family = AF_ETHERNET;
875 p.prefixlen = ETH_ALEN * 8;
876 p.u.prefix_eth = prefix->u.prefix_evpn.macip_addr.mac;
877
878 return (access_list_apply(alist, &p) == FILTER_DENY ? RMAP_NOMATCH
879 : RMAP_MATCH);
880 }
881
882 /* Route map `mac address' match statement. `arg' should be
883 access-list name. */
884 static void *route_match_mac_address_compile(const char *arg)
885 {
886 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
887 }
888
889 /* Free route map's compiled `ip address' value. */
890 static void route_match_mac_address_free(void *rule)
891 {
892 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
893 }
894
895 /* Route map commands for mac address matching. */
896 static const struct route_map_rule_cmd route_match_mac_address_cmd = {
897 "mac address",
898 route_match_mac_address,
899 route_match_mac_address_compile,
900 route_match_mac_address_free
901 };
902
903 /*
904 * Match function returns:
905 * ...RMAP_MATCH if match is found.
906 * ...RMAP_NOMATCH if match is not found.
907 * ...RMAP_NOOP to ignore this match check.
908 */
909 static enum route_map_cmd_result_t
910 route_match_vni(void *rule, const struct prefix *prefix, void *object)
911 {
912 vni_t vni = 0;
913 unsigned int label_cnt = 0;
914 struct bgp_path_info *path = NULL;
915 struct prefix_evpn *evp = (struct prefix_evpn *) prefix;
916
917 vni = *((vni_t *)rule);
918 path = (struct bgp_path_info *)object;
919
920 /*
921 * This rmap filter is valid for vxlan tunnel type only.
922 * For any other tunnel type, return noop to ignore
923 * this check.
924 */
925 if (path->attr->encap_tunneltype != BGP_ENCAP_TYPE_VXLAN)
926 return RMAP_NOOP;
927
928 /*
929 * Apply filter to type 1, 2, 5 routes only.
930 * Other route types do not have vni label.
931 */
932 if (evp
933 && (evp->prefix.route_type != BGP_EVPN_AD_ROUTE
934 && evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
935 && evp->prefix.route_type != BGP_EVPN_IP_PREFIX_ROUTE))
936 return RMAP_NOOP;
937
938 if (path->extra == NULL)
939 return RMAP_NOMATCH;
940
941 for (;
942 label_cnt < BGP_MAX_LABELS && label_cnt < path->extra->num_labels;
943 label_cnt++) {
944 if (vni == label2vni(&path->extra->label[label_cnt]))
945 return RMAP_MATCH;
946 }
947
948 return RMAP_NOMATCH;
949 }
950
951 /* Route map `vni' match statement. */
952 static void *route_match_vni_compile(const char *arg)
953 {
954 vni_t *vni = NULL;
955 char *end = NULL;
956
957 vni = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(vni_t));
958
959 *vni = strtoul(arg, &end, 10);
960 if (*end != '\0') {
961 XFREE(MTYPE_ROUTE_MAP_COMPILED, vni);
962 return NULL;
963 }
964
965 return vni;
966 }
967
968 /* Free route map's compiled `vni' value. */
969 static void route_match_vni_free(void *rule)
970 {
971 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
972 }
973
974 /* Route map commands for vni matching. */
975 static const struct route_map_rule_cmd route_match_evpn_vni_cmd = {
976 "evpn vni",
977 route_match_vni,
978 route_match_vni_compile,
979 route_match_vni_free
980 };
981
982 /* `match evpn route-type' */
983
984 /* Match function should return 1 if match is success else return
985 zero. */
986 static enum route_map_cmd_result_t
987 route_match_evpn_route_type(void *rule, const struct prefix *pfx, void *object)
988 {
989 uint8_t route_type = 0;
990
991 route_type = *((uint8_t *)rule);
992
993 if (route_type == pfx->u.prefix_evpn.route_type)
994 return RMAP_MATCH;
995
996 return RMAP_NOMATCH;
997 }
998
999 /* Route map `route-type' match statement. */
1000 static void *route_match_evpn_route_type_compile(const char *arg)
1001 {
1002 uint8_t *route_type = NULL;
1003
1004 route_type = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint8_t));
1005
1006 if (strncmp(arg, "ma", 2) == 0)
1007 *route_type = BGP_EVPN_MAC_IP_ROUTE;
1008 else if (strncmp(arg, "mu", 2) == 0)
1009 *route_type = BGP_EVPN_IMET_ROUTE;
1010 else
1011 *route_type = BGP_EVPN_IP_PREFIX_ROUTE;
1012
1013 return route_type;
1014 }
1015
1016 /* Free route map's compiled `route-type' value. */
1017 static void route_match_evpn_route_type_free(void *rule)
1018 {
1019 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1020 }
1021
1022 /* Route map commands for evpn route-type matching. */
1023 static const struct route_map_rule_cmd route_match_evpn_route_type_cmd = {
1024 "evpn route-type",
1025 route_match_evpn_route_type,
1026 route_match_evpn_route_type_compile,
1027 route_match_evpn_route_type_free
1028 };
1029
1030 /* `match rd' */
1031
1032 /* Match function should return 1 if match is success else return zero. */
1033 static enum route_map_cmd_result_t
1034 route_match_rd(void *rule, const struct prefix *prefix, void *object)
1035 {
1036 struct prefix_rd *prd_rule = NULL;
1037 const struct prefix_rd *prd_route = NULL;
1038 struct bgp_path_info *path = NULL;
1039
1040 if (prefix->family != AF_EVPN)
1041 return RMAP_NOMATCH;
1042
1043 prd_rule = (struct prefix_rd *)rule;
1044 path = (struct bgp_path_info *)object;
1045
1046 if (path->net == NULL || path->net->pdest == NULL)
1047 return RMAP_NOMATCH;
1048
1049 prd_route = (struct prefix_rd *)bgp_dest_get_prefix(path->net->pdest);
1050 if (memcmp(prd_route->val, prd_rule->val, ECOMMUNITY_SIZE) == 0)
1051 return RMAP_MATCH;
1052
1053 return RMAP_NOMATCH;
1054 }
1055
1056 /* Route map `rd' match statement. */
1057 static void *route_match_rd_compile(const char *arg)
1058 {
1059 struct prefix_rd *prd;
1060 int ret;
1061
1062 prd = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct prefix_rd));
1063
1064 ret = str2prefix_rd(arg, prd);
1065 if (!ret) {
1066 XFREE(MTYPE_ROUTE_MAP_COMPILED, prd);
1067 return NULL;
1068 }
1069
1070 return prd;
1071 }
1072
1073 /* Free route map's compiled `rd' value. */
1074 static void route_match_rd_free(void *rule)
1075 {
1076 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1077 }
1078
1079 /* Route map commands for rd matching. */
1080 static const struct route_map_rule_cmd route_match_evpn_rd_cmd = {
1081 "evpn rd",
1082 route_match_rd,
1083 route_match_rd_compile,
1084 route_match_rd_free
1085 };
1086
1087 /* Route map commands for VRF route leak with source vrf matching */
1088 static enum route_map_cmd_result_t
1089 route_match_vrl_source_vrf(void *rule, const struct prefix *prefix,
1090 void *object)
1091 {
1092 struct bgp_path_info *path;
1093 char *vrf_name;
1094
1095 vrf_name = rule;
1096 path = (struct bgp_path_info *)object;
1097
1098 if (strncmp(vrf_name, "n/a", VRF_NAMSIZ) == 0)
1099 return RMAP_NOMATCH;
1100
1101 if (path->extra == NULL)
1102 return RMAP_NOMATCH;
1103
1104 if (strncmp(vrf_name, vrf_id_to_name(path->extra->bgp_orig->vrf_id),
1105 VRF_NAMSIZ)
1106 == 0)
1107 return RMAP_MATCH;
1108
1109 return RMAP_NOMATCH;
1110 }
1111
1112 static void *route_match_vrl_source_vrf_compile(const char *arg)
1113 {
1114 uint8_t *vrf_name = NULL;
1115
1116 vrf_name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1117
1118 return vrf_name;
1119 }
1120
1121 /* Free route map's compiled `route-type' value. */
1122 static void route_match_vrl_source_vrf_free(void *rule)
1123 {
1124 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1125 }
1126
1127 static const struct route_map_rule_cmd route_match_vrl_source_vrf_cmd = {
1128 "source-vrf",
1129 route_match_vrl_source_vrf,
1130 route_match_vrl_source_vrf_compile,
1131 route_match_vrl_source_vrf_free
1132 };
1133
1134 /* `match local-preference LOCAL-PREF' */
1135
1136 /* Match function return 1 if match is success else return zero. */
1137 static enum route_map_cmd_result_t
1138 route_match_local_pref(void *rule, const struct prefix *prefix, void *object)
1139 {
1140 uint32_t *local_pref;
1141 struct bgp_path_info *path;
1142
1143 local_pref = rule;
1144 path = object;
1145
1146 if (path->attr->local_pref == *local_pref)
1147 return RMAP_MATCH;
1148 else
1149 return RMAP_NOMATCH;
1150 }
1151
1152 /*
1153 * Route map `match local-preference' match statement.
1154 * `arg' is local-pref value
1155 */
1156 static void *route_match_local_pref_compile(const char *arg)
1157 {
1158 uint32_t *local_pref;
1159 char *endptr = NULL;
1160 unsigned long tmpval;
1161
1162 /* Locpref value shoud be integer. */
1163 if (!all_digit(arg))
1164 return NULL;
1165
1166 errno = 0;
1167 tmpval = strtoul(arg, &endptr, 10);
1168 if (*endptr != '\0' || errno || tmpval > UINT32_MAX)
1169 return NULL;
1170
1171 local_pref = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
1172
1173 *local_pref = tmpval;
1174 return local_pref;
1175 }
1176
1177 /* Free route map's compiled `match local-preference' value. */
1178 static void route_match_local_pref_free(void *rule)
1179 {
1180 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1181 }
1182
1183 /* Route map commands for metric matching. */
1184 static const struct route_map_rule_cmd route_match_local_pref_cmd = {
1185 "local-preference",
1186 route_match_local_pref,
1187 route_match_local_pref_compile,
1188 route_match_local_pref_free
1189 };
1190
1191 /* `match metric METRIC' */
1192
1193 /* Match function return 1 if match is success else return zero. */
1194 static enum route_map_cmd_result_t
1195 route_match_metric(void *rule, const struct prefix *prefix, void *object)
1196 {
1197 struct rmap_value *rv;
1198 struct bgp_path_info *path;
1199
1200 rv = rule;
1201 path = object;
1202 return route_value_match(rv, path->attr->med);
1203 }
1204
1205 /* Route map commands for metric matching. */
1206 static const struct route_map_rule_cmd route_match_metric_cmd = {
1207 "metric",
1208 route_match_metric,
1209 route_value_compile,
1210 route_value_free,
1211 };
1212
1213 /* `match as-path ASPATH' */
1214
1215 /* Match function for as-path match. I assume given object is */
1216 static enum route_map_cmd_result_t
1217 route_match_aspath(void *rule, const struct prefix *prefix, void *object)
1218 {
1219
1220 struct as_list *as_list;
1221 struct bgp_path_info *path;
1222
1223 as_list = as_list_lookup((char *)rule);
1224 if (as_list == NULL)
1225 return RMAP_NOMATCH;
1226
1227 path = object;
1228
1229 /* Perform match. */
1230 return ((as_list_apply(as_list, path->attr->aspath) == AS_FILTER_DENY)
1231 ? RMAP_NOMATCH
1232 : RMAP_MATCH);
1233 }
1234
1235 /* Compile function for as-path match. */
1236 static void *route_match_aspath_compile(const char *arg)
1237 {
1238 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1239 }
1240
1241 /* Compile function for as-path match. */
1242 static void route_match_aspath_free(void *rule)
1243 {
1244 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1245 }
1246
1247 /* Route map commands for aspath matching. */
1248 static const struct route_map_rule_cmd route_match_aspath_cmd = {
1249 "as-path",
1250 route_match_aspath,
1251 route_match_aspath_compile,
1252 route_match_aspath_free
1253 };
1254
1255 /* `match community COMMUNIY' */
1256 struct rmap_community {
1257 char *name;
1258 uint32_t name_hash;
1259 int exact;
1260 };
1261
1262 /* Match function for community match. */
1263 static enum route_map_cmd_result_t
1264 route_match_community(void *rule, const struct prefix *prefix, void *object)
1265 {
1266 struct community_list *list;
1267 struct bgp_path_info *path;
1268 struct rmap_community *rcom = rule;
1269
1270 path = object;
1271 rcom = rule;
1272
1273 list = community_list_lookup(bgp_clist, rcom->name, rcom->name_hash,
1274 COMMUNITY_LIST_MASTER);
1275 if (!list)
1276 return RMAP_NOMATCH;
1277
1278 if (rcom->exact) {
1279 if (community_list_exact_match(path->attr->community, list))
1280 return RMAP_MATCH;
1281 } else {
1282 if (community_list_match(path->attr->community, list))
1283 return RMAP_MATCH;
1284 }
1285
1286 return RMAP_NOMATCH;
1287 }
1288
1289 /* Compile function for community match. */
1290 static void *route_match_community_compile(const char *arg)
1291 {
1292 struct rmap_community *rcom;
1293 int len;
1294 char *p;
1295
1296 rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
1297
1298 p = strchr(arg, ' ');
1299 if (p) {
1300 len = p - arg;
1301 rcom->name = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, len + 1);
1302 memcpy(rcom->name, arg, len);
1303 rcom->exact = 1;
1304 } else {
1305 rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1306 rcom->exact = 0;
1307 }
1308
1309 rcom->name_hash = bgp_clist_hash_key(rcom->name);
1310 return rcom;
1311 }
1312
1313 /* Compile function for community match. */
1314 static void route_match_community_free(void *rule)
1315 {
1316 struct rmap_community *rcom = rule;
1317
1318 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
1319 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
1320 }
1321
1322 /*
1323 * In routemap processing there is a need to add the
1324 * name as a rule_key in the dependency table. Routemap
1325 * lib is unaware of rule_key when exact-match clause
1326 * is in use. routemap lib uses the compiled output to
1327 * get the rule_key value.
1328 */
1329 static void *route_match_get_community_key(void *rule)
1330 {
1331 struct rmap_community *rcom;
1332
1333 rcom = rule;
1334 return rcom->name;
1335 }
1336
1337
1338 /* Route map commands for community matching. */
1339 static const struct route_map_rule_cmd route_match_community_cmd = {
1340 "community",
1341 route_match_community,
1342 route_match_community_compile,
1343 route_match_community_free,
1344 route_match_get_community_key
1345 };
1346
1347 /* Match function for lcommunity match. */
1348 static enum route_map_cmd_result_t
1349 route_match_lcommunity(void *rule, const struct prefix *prefix, void *object)
1350 {
1351 struct community_list *list;
1352 struct bgp_path_info *path;
1353 struct rmap_community *rcom = rule;
1354
1355 path = object;
1356
1357 list = community_list_lookup(bgp_clist, rcom->name, rcom->name_hash,
1358 LARGE_COMMUNITY_LIST_MASTER);
1359 if (!list)
1360 return RMAP_NOMATCH;
1361
1362 if (rcom->exact) {
1363 if (lcommunity_list_exact_match(path->attr->lcommunity, list))
1364 return RMAP_MATCH;
1365 } else {
1366 if (lcommunity_list_match(path->attr->lcommunity, list))
1367 return RMAP_MATCH;
1368 }
1369
1370 return RMAP_NOMATCH;
1371 }
1372
1373 /* Compile function for community match. */
1374 static void *route_match_lcommunity_compile(const char *arg)
1375 {
1376 struct rmap_community *rcom;
1377 int len;
1378 char *p;
1379
1380 rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
1381
1382 p = strchr(arg, ' ');
1383 if (p) {
1384 len = p - arg;
1385 rcom->name = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, len + 1);
1386 memcpy(rcom->name, arg, len);
1387 rcom->exact = 1;
1388 } else {
1389 rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1390 rcom->exact = 0;
1391 }
1392
1393 rcom->name_hash = bgp_clist_hash_key(rcom->name);
1394 return rcom;
1395 }
1396
1397 /* Compile function for community match. */
1398 static void route_match_lcommunity_free(void *rule)
1399 {
1400 struct rmap_community *rcom = rule;
1401
1402 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
1403 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
1404 }
1405
1406 /* Route map commands for community matching. */
1407 static const struct route_map_rule_cmd route_match_lcommunity_cmd = {
1408 "large-community",
1409 route_match_lcommunity,
1410 route_match_lcommunity_compile,
1411 route_match_lcommunity_free,
1412 route_match_get_community_key
1413 };
1414
1415
1416 /* Match function for extcommunity match. */
1417 static enum route_map_cmd_result_t
1418 route_match_ecommunity(void *rule, const struct prefix *prefix, void *object)
1419 {
1420 struct community_list *list;
1421 struct bgp_path_info *path;
1422 struct rmap_community *rcom = rule;
1423
1424 path = object;
1425
1426 list = community_list_lookup(bgp_clist, rcom->name, rcom->name_hash,
1427 EXTCOMMUNITY_LIST_MASTER);
1428 if (!list)
1429 return RMAP_NOMATCH;
1430
1431 if (ecommunity_list_match(path->attr->ecommunity, list))
1432 return RMAP_MATCH;
1433
1434 return RMAP_NOMATCH;
1435 }
1436
1437 /* Compile function for extcommunity match. */
1438 static void *route_match_ecommunity_compile(const char *arg)
1439 {
1440 struct rmap_community *rcom;
1441
1442 rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
1443 rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1444 rcom->name_hash = bgp_clist_hash_key(rcom->name);
1445
1446 return rcom;
1447 }
1448
1449 /* Compile function for extcommunity match. */
1450 static void route_match_ecommunity_free(void *rule)
1451 {
1452 struct rmap_community *rcom = rule;
1453
1454 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
1455 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
1456 }
1457
1458 /* Route map commands for community matching. */
1459 static const struct route_map_rule_cmd route_match_ecommunity_cmd = {
1460 "extcommunity",
1461 route_match_ecommunity,
1462 route_match_ecommunity_compile,
1463 route_match_ecommunity_free
1464 };
1465
1466 /* `match nlri` and `set nlri` are replaced by `address-family ipv4`
1467 and `address-family vpnv4'. */
1468
1469 /* `match origin' */
1470 static enum route_map_cmd_result_t
1471 route_match_origin(void *rule, const struct prefix *prefix, void *object)
1472 {
1473 uint8_t *origin;
1474 struct bgp_path_info *path;
1475
1476 origin = rule;
1477 path = object;
1478
1479 if (path->attr->origin == *origin)
1480 return RMAP_MATCH;
1481
1482 return RMAP_NOMATCH;
1483 }
1484
1485 static void *route_match_origin_compile(const char *arg)
1486 {
1487 uint8_t *origin;
1488
1489 origin = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint8_t));
1490
1491 if (strcmp(arg, "igp") == 0)
1492 *origin = 0;
1493 else if (strcmp(arg, "egp") == 0)
1494 *origin = 1;
1495 else
1496 *origin = 2;
1497
1498 return origin;
1499 }
1500
1501 /* Free route map's compiled `ip address' value. */
1502 static void route_match_origin_free(void *rule)
1503 {
1504 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1505 }
1506
1507 /* Route map commands for origin matching. */
1508 static const struct route_map_rule_cmd route_match_origin_cmd = {
1509 "origin",
1510 route_match_origin,
1511 route_match_origin_compile,
1512 route_match_origin_free
1513 };
1514
1515 /* match probability { */
1516
1517 static enum route_map_cmd_result_t
1518 route_match_probability(void *rule, const struct prefix *prefix, void *object)
1519 {
1520 long r = frr_weak_random();
1521
1522 switch (*(long *)rule) {
1523 case 0:
1524 break;
1525 case RAND_MAX:
1526 return RMAP_MATCH;
1527 default:
1528 if (r < *(long *)rule) {
1529 return RMAP_MATCH;
1530 }
1531 }
1532
1533 return RMAP_NOMATCH;
1534 }
1535
1536 static void *route_match_probability_compile(const char *arg)
1537 {
1538 long *lobule;
1539 unsigned perc;
1540
1541 perc = atoi(arg);
1542 lobule = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(long));
1543
1544 switch (perc) {
1545 case 0:
1546 *lobule = 0;
1547 break;
1548 case 100:
1549 *lobule = RAND_MAX;
1550 break;
1551 default:
1552 *lobule = RAND_MAX / 100 * perc;
1553 }
1554
1555 return lobule;
1556 }
1557
1558 static void route_match_probability_free(void *rule)
1559 {
1560 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1561 }
1562
1563 static const struct route_map_rule_cmd route_match_probability_cmd = {
1564 "probability",
1565 route_match_probability,
1566 route_match_probability_compile,
1567 route_match_probability_free
1568 };
1569
1570 /* `match interface IFNAME' */
1571 /* Match function should return 1 if match is success else return
1572 zero. */
1573 static enum route_map_cmd_result_t
1574 route_match_interface(void *rule, const struct prefix *prefix, void *object)
1575 {
1576 struct interface *ifp;
1577 struct bgp_path_info *path;
1578
1579 path = object;
1580
1581 if (!path)
1582 return RMAP_NOMATCH;
1583
1584 ifp = if_lookup_by_name_all_vrf((char *)rule);
1585
1586 if (ifp == NULL || ifp->ifindex != path->attr->nh_ifindex)
1587 return RMAP_NOMATCH;
1588
1589 return RMAP_MATCH;
1590 }
1591
1592 /* Route map `interface' match statement. `arg' should be
1593 interface name. */
1594 static void *route_match_interface_compile(const char *arg)
1595 {
1596 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
1597 }
1598
1599 /* Free route map's compiled `interface' value. */
1600 static void route_match_interface_free(void *rule)
1601 {
1602 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1603 }
1604
1605 /* Route map commands for ip address matching. */
1606 static const struct route_map_rule_cmd route_match_interface_cmd = {
1607 "interface",
1608 route_match_interface,
1609 route_match_interface_compile,
1610 route_match_interface_free
1611 };
1612
1613 /* } */
1614
1615 /* `set ip next-hop IP_ADDRESS' */
1616
1617 /* Match function return 1 if match is success else return zero. */
1618 static enum route_map_cmd_result_t
1619 route_match_tag(void *rule, const struct prefix *prefix, void *object)
1620 {
1621 route_tag_t *tag;
1622 struct bgp_path_info *path;
1623
1624 tag = rule;
1625 path = object;
1626
1627 return ((path->attr->tag == *tag) ? RMAP_MATCH : RMAP_NOMATCH);
1628 }
1629
1630
1631 /* Route map commands for tag matching. */
1632 static const struct route_map_rule_cmd route_match_tag_cmd = {
1633 "tag",
1634 route_match_tag,
1635 route_map_rule_tag_compile,
1636 route_map_rule_tag_free,
1637 };
1638
1639 static enum route_map_cmd_result_t
1640 route_set_srte_color(void *rule, const struct prefix *prefix, void *object)
1641 {
1642 uint32_t *srte_color = rule;
1643 struct bgp_path_info *path;
1644
1645 path = object;
1646
1647 path->attr->srte_color = *srte_color;
1648 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR);
1649
1650 return RMAP_OKAY;
1651 }
1652
1653 /* Route map `sr-te color' compile function */
1654 static void *route_set_srte_color_compile(const char *arg)
1655 {
1656 uint32_t *color;
1657
1658 color = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
1659 *color = atoi(arg);
1660
1661 return color;
1662 }
1663
1664 /* Free route map's compiled `sr-te color' value. */
1665 static void route_set_srte_color_free(void *rule)
1666 {
1667 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
1668 }
1669
1670 /* Route map commands for sr-te color set. */
1671 struct route_map_rule_cmd route_set_srte_color_cmd = {
1672 "sr-te color", route_set_srte_color, route_set_srte_color_compile,
1673 route_set_srte_color_free};
1674
1675 /* Set nexthop to object. ojbect must be pointer to struct attr. */
1676 struct rmap_ip_nexthop_set {
1677 struct in_addr *address;
1678 int peer_address;
1679 int unchanged;
1680 };
1681
1682 static enum route_map_cmd_result_t
1683 route_set_ip_nexthop(void *rule, const struct prefix *prefix, void *object)
1684 {
1685 struct rmap_ip_nexthop_set *rins = rule;
1686 struct bgp_path_info *path;
1687 struct peer *peer;
1688
1689 if (prefix->family == AF_INET6)
1690 return RMAP_OKAY;
1691
1692 path = object;
1693 peer = path->peer;
1694
1695 if (rins->unchanged) {
1696 SET_FLAG(path->attr->rmap_change_flags,
1697 BATTR_RMAP_NEXTHOP_UNCHANGED);
1698 } else if (rins->peer_address) {
1699 if ((CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN)
1700 || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IMPORT))
1701 && peer->su_remote
1702 && sockunion_family(peer->su_remote) == AF_INET) {
1703 path->attr->nexthop.s_addr =
1704 sockunion2ip(peer->su_remote);
1705 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1706 } else if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT)) {
1707 /* The next hop value will be set as part of
1708 * packet rewrite. Set the flags here to indicate
1709 * that rewrite needs to be done.
1710 * Also, clear the value.
1711 */
1712 SET_FLAG(path->attr->rmap_change_flags,
1713 BATTR_RMAP_NEXTHOP_PEER_ADDRESS);
1714 path->attr->nexthop.s_addr = INADDR_ANY;
1715 }
1716 } else {
1717 /* Set next hop value. */
1718 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1719 path->attr->nexthop = *rins->address;
1720 SET_FLAG(path->attr->rmap_change_flags,
1721 BATTR_RMAP_IPV4_NHOP_CHANGED);
1722 /* case for MP-BGP : MPLS VPN */
1723 path->attr->mp_nexthop_global_in = *rins->address;
1724 path->attr->mp_nexthop_len = sizeof(*rins->address);
1725 }
1726
1727 return RMAP_OKAY;
1728 }
1729
1730 /* Route map `ip nexthop' compile function. Given string is converted
1731 to struct in_addr structure. */
1732 static void *route_set_ip_nexthop_compile(const char *arg)
1733 {
1734 struct rmap_ip_nexthop_set *rins;
1735 struct in_addr *address = NULL;
1736 int peer_address = 0;
1737 int unchanged = 0;
1738 int ret;
1739
1740 if (strcmp(arg, "peer-address") == 0)
1741 peer_address = 1;
1742 else if (strcmp(arg, "unchanged") == 0)
1743 unchanged = 1;
1744 else {
1745 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
1746 sizeof(struct in_addr));
1747 ret = inet_aton(arg, address);
1748
1749 if (ret == 0) {
1750 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
1751 return NULL;
1752 }
1753 }
1754
1755 rins = XCALLOC(MTYPE_ROUTE_MAP_COMPILED,
1756 sizeof(struct rmap_ip_nexthop_set));
1757
1758 rins->address = address;
1759 rins->peer_address = peer_address;
1760 rins->unchanged = unchanged;
1761
1762 return rins;
1763 }
1764
1765 /* Free route map's compiled `ip nexthop' value. */
1766 static void route_set_ip_nexthop_free(void *rule)
1767 {
1768 struct rmap_ip_nexthop_set *rins = rule;
1769
1770 XFREE(MTYPE_ROUTE_MAP_COMPILED, rins->address);
1771
1772 XFREE(MTYPE_ROUTE_MAP_COMPILED, rins);
1773 }
1774
1775 /* Route map commands for ip nexthop set. */
1776 static const struct route_map_rule_cmd route_set_ip_nexthop_cmd = {
1777 "ip next-hop",
1778 route_set_ip_nexthop,
1779 route_set_ip_nexthop_compile,
1780 route_set_ip_nexthop_free
1781 };
1782
1783 /* `set local-preference LOCAL_PREF' */
1784
1785 /* Set local preference. */
1786 static enum route_map_cmd_result_t
1787 route_set_local_pref(void *rule, const struct prefix *prefix, void *object)
1788 {
1789 struct rmap_value *rv;
1790 struct bgp_path_info *path;
1791 uint32_t locpref = 0;
1792
1793 /* Fetch routemap's rule information. */
1794 rv = rule;
1795 path = object;
1796
1797 /* Set local preference value. */
1798 if (path->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
1799 locpref = path->attr->local_pref;
1800
1801 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1802 path->attr->local_pref = route_value_adjust(rv, locpref, path->peer);
1803
1804 return RMAP_OKAY;
1805 }
1806
1807 /* Set local preference rule structure. */
1808 static const struct route_map_rule_cmd route_set_local_pref_cmd = {
1809 "local-preference",
1810 route_set_local_pref,
1811 route_value_compile,
1812 route_value_free,
1813 };
1814
1815 /* `set weight WEIGHT' */
1816
1817 /* Set weight. */
1818 static enum route_map_cmd_result_t
1819 route_set_weight(void *rule, const struct prefix *prefix, void *object)
1820 {
1821 struct rmap_value *rv;
1822 struct bgp_path_info *path;
1823
1824 /* Fetch routemap's rule information. */
1825 rv = rule;
1826 path = object;
1827
1828 /* Set weight value. */
1829 path->attr->weight = route_value_adjust(rv, 0, path->peer);
1830
1831 return RMAP_OKAY;
1832 }
1833
1834 /* Set local preference rule structure. */
1835 static const struct route_map_rule_cmd route_set_weight_cmd = {
1836 "weight",
1837 route_set_weight,
1838 route_value_compile,
1839 route_value_free,
1840 };
1841
1842 /* `set distance DISTANCE */
1843 static enum route_map_cmd_result_t
1844 route_set_distance(void *rule, const struct prefix *prefix, void *object)
1845 {
1846 struct bgp_path_info *path = object;
1847 struct rmap_value *rv = rule;
1848
1849 path->attr->distance = rv->value;
1850
1851 return RMAP_OKAY;
1852 }
1853
1854 /* set distance rule structure */
1855 static const struct route_map_rule_cmd route_set_distance_cmd = {
1856 "distance",
1857 route_set_distance,
1858 route_value_compile,
1859 route_value_free,
1860 };
1861
1862 /* `set metric METRIC' */
1863
1864 /* Set metric to attribute. */
1865 static enum route_map_cmd_result_t
1866 route_set_metric(void *rule, const struct prefix *prefix, void *object)
1867 {
1868 struct rmap_value *rv;
1869 struct bgp_path_info *path;
1870 uint32_t med = 0;
1871
1872 /* Fetch routemap's rule information. */
1873 rv = rule;
1874 path = object;
1875
1876 if (path->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
1877 med = path->attr->med;
1878
1879 path->attr->med = route_value_adjust(rv, med, path->peer);
1880 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
1881
1882 return RMAP_OKAY;
1883 }
1884
1885 /* Set metric rule structure. */
1886 static const struct route_map_rule_cmd route_set_metric_cmd = {
1887 "metric",
1888 route_set_metric,
1889 route_value_compile,
1890 route_value_free,
1891 };
1892
1893 /* `set table (1-4294967295)' */
1894
1895 static enum route_map_cmd_result_t
1896 route_set_table_id(void *rule, const struct prefix *prefix,
1897
1898 void *object)
1899 {
1900 struct rmap_value *rv;
1901 struct bgp_path_info *path;
1902
1903 /* Fetch routemap's rule information. */
1904 rv = rule;
1905 path = object;
1906
1907 path->attr->rmap_table_id = rv->value;
1908
1909 return RMAP_OKAY;
1910 }
1911
1912 /* Set table_id rule structure. */
1913 static const struct route_map_rule_cmd route_set_table_id_cmd = {
1914 "table",
1915 route_set_table_id,
1916 route_value_compile,
1917 route_value_free
1918 };
1919
1920 /* `set as-path prepend ASPATH' */
1921
1922 /* For AS path prepend mechanism. */
1923 static enum route_map_cmd_result_t
1924 route_set_aspath_prepend(void *rule, const struct prefix *prefix, void *object)
1925 {
1926 struct aspath *aspath;
1927 struct aspath *new;
1928 struct bgp_path_info *path;
1929
1930 path = object;
1931
1932 if (path->attr->aspath->refcnt)
1933 new = aspath_dup(path->attr->aspath);
1934 else
1935 new = path->attr->aspath;
1936
1937 if ((uintptr_t)rule > 10) {
1938 aspath = rule;
1939 aspath_prepend(aspath, new);
1940 } else {
1941 as_t as = aspath_leftmost(new);
1942 if (!as)
1943 as = path->peer->as;
1944 new = aspath_add_seq_n(new, as, (uintptr_t)rule);
1945 }
1946
1947 path->attr->aspath = new;
1948
1949 return RMAP_OKAY;
1950 }
1951
1952 static void *route_set_aspath_prepend_compile(const char *arg)
1953 {
1954 unsigned int num;
1955
1956 if (sscanf(arg, "last-as %u", &num) == 1 && num > 0 && num <= 10)
1957 return (void *)(uintptr_t)num;
1958
1959 return route_aspath_compile(arg);
1960 }
1961
1962 static void route_set_aspath_prepend_free(void *rule)
1963 {
1964 if ((uintptr_t)rule > 10)
1965 route_aspath_free(rule);
1966 }
1967
1968
1969 /* Set as-path prepend rule structure. */
1970 static const struct route_map_rule_cmd route_set_aspath_prepend_cmd = {
1971 "as-path prepend",
1972 route_set_aspath_prepend,
1973 route_set_aspath_prepend_compile,
1974 route_set_aspath_prepend_free,
1975 };
1976
1977 /* `set as-path exclude ASn' */
1978
1979 /* For ASN exclude mechanism.
1980 * Iterate over ASns requested and filter them from the given AS_PATH one by
1981 * one.
1982 * Make a deep copy of existing AS_PATH, but for the first ASn only.
1983 */
1984 static enum route_map_cmd_result_t
1985 route_set_aspath_exclude(void *rule, const struct prefix *dummy, void *object)
1986 {
1987 struct aspath *new_path, *exclude_path;
1988 struct bgp_path_info *path;
1989
1990 exclude_path = rule;
1991 path = object;
1992 if (path->attr->aspath->refcnt)
1993 new_path = aspath_dup(path->attr->aspath);
1994 else
1995 new_path = path->attr->aspath;
1996 path->attr->aspath = aspath_filter_exclude(new_path, exclude_path);
1997
1998 return RMAP_OKAY;
1999 }
2000
2001 /* Set ASn exlude rule structure. */
2002 static const struct route_map_rule_cmd route_set_aspath_exclude_cmd = {
2003 "as-path exclude",
2004 route_set_aspath_exclude,
2005 route_aspath_compile,
2006 route_aspath_free,
2007 };
2008
2009 /* `set community COMMUNITY' */
2010 struct rmap_com_set {
2011 struct community *com;
2012 int additive;
2013 int none;
2014 };
2015
2016 /* For community set mechanism. */
2017 static enum route_map_cmd_result_t
2018 route_set_community(void *rule, const struct prefix *prefix, void *object)
2019 {
2020 struct rmap_com_set *rcs;
2021 struct bgp_path_info *path;
2022 struct attr *attr;
2023 struct community *new = NULL;
2024 struct community *old;
2025 struct community *merge;
2026
2027 rcs = rule;
2028 path = object;
2029 attr = path->attr;
2030 old = attr->community;
2031
2032 /* "none" case. */
2033 if (rcs->none) {
2034 attr->flag &= ~(ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES));
2035 attr->community = NULL;
2036 /* See the longer comment down below. */
2037 if (old && old->refcnt == 0)
2038 community_free(&old);
2039 return RMAP_OKAY;
2040 }
2041
2042 /* "additive" case. */
2043 if (rcs->additive && old) {
2044 merge = community_merge(community_dup(old), rcs->com);
2045
2046 new = community_uniq_sort(merge);
2047 community_free(&merge);
2048 } else
2049 new = community_dup(rcs->com);
2050
2051 /* HACK: if the old community is not intern'd,
2052 * we should free it here, or all reference to it may be
2053 * lost.
2054 * Really need to cleanup attribute caching sometime.
2055 */
2056 if (old && old->refcnt == 0)
2057 community_free(&old);
2058
2059 /* will be interned by caller if required */
2060 attr->community = new;
2061
2062 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
2063
2064 return RMAP_OKAY;
2065 }
2066
2067 /* Compile function for set community. */
2068 static void *route_set_community_compile(const char *arg)
2069 {
2070 struct rmap_com_set *rcs;
2071 struct community *com = NULL;
2072 char *sp;
2073 int additive = 0;
2074 int none = 0;
2075
2076 if (strcmp(arg, "none") == 0)
2077 none = 1;
2078 else {
2079 sp = strstr(arg, "additive");
2080
2081 if (sp && sp > arg) {
2082 /* "additive" keyword is included. */
2083 additive = 1;
2084 *(sp - 1) = '\0';
2085 }
2086
2087 com = community_str2com(arg);
2088
2089 if (additive)
2090 *(sp - 1) = ' ';
2091
2092 if (!com)
2093 return NULL;
2094 }
2095
2096 rcs = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_com_set));
2097 rcs->com = com;
2098 rcs->additive = additive;
2099 rcs->none = none;
2100
2101 return rcs;
2102 }
2103
2104 /* Free function for set community. */
2105 static void route_set_community_free(void *rule)
2106 {
2107 struct rmap_com_set *rcs = rule;
2108
2109 if (rcs->com)
2110 community_free(&rcs->com);
2111 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcs);
2112 }
2113
2114 /* Set community rule structure. */
2115 static const struct route_map_rule_cmd route_set_community_cmd = {
2116 "community",
2117 route_set_community,
2118 route_set_community_compile,
2119 route_set_community_free,
2120 };
2121
2122 /* `set community COMMUNITY' */
2123 struct rmap_lcom_set {
2124 struct lcommunity *lcom;
2125 int additive;
2126 int none;
2127 };
2128
2129
2130 /* For lcommunity set mechanism. */
2131 static enum route_map_cmd_result_t
2132 route_set_lcommunity(void *rule, const struct prefix *prefix, void *object)
2133 {
2134 struct rmap_lcom_set *rcs;
2135 struct bgp_path_info *path;
2136 struct attr *attr;
2137 struct lcommunity *new = NULL;
2138 struct lcommunity *old;
2139 struct lcommunity *merge;
2140
2141 rcs = rule;
2142 path = object;
2143 attr = path->attr;
2144 old = attr->lcommunity;
2145
2146 /* "none" case. */
2147 if (rcs->none) {
2148 attr->flag &= ~(ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES));
2149 attr->lcommunity = NULL;
2150
2151 /* See the longer comment down below. */
2152 if (old && old->refcnt == 0)
2153 lcommunity_free(&old);
2154 return RMAP_OKAY;
2155 }
2156
2157 if (rcs->additive && old) {
2158 merge = lcommunity_merge(lcommunity_dup(old), rcs->lcom);
2159
2160 new = lcommunity_uniq_sort(merge);
2161 lcommunity_free(&merge);
2162 } else
2163 new = lcommunity_dup(rcs->lcom);
2164
2165 /* HACK: if the old large-community is not intern'd,
2166 * we should free it here, or all reference to it may be
2167 * lost.
2168 * Really need to cleanup attribute caching sometime.
2169 */
2170 if (old && old->refcnt == 0)
2171 lcommunity_free(&old);
2172
2173 /* will be intern()'d or attr_flush()'d by bgp_update_main() */
2174 attr->lcommunity = new;
2175
2176 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES);
2177
2178 return RMAP_OKAY;
2179 }
2180
2181 /* Compile function for set community. */
2182 static void *route_set_lcommunity_compile(const char *arg)
2183 {
2184 struct rmap_lcom_set *rcs;
2185 struct lcommunity *lcom = NULL;
2186 char *sp;
2187 int additive = 0;
2188 int none = 0;
2189
2190 if (strcmp(arg, "none") == 0)
2191 none = 1;
2192 else {
2193 sp = strstr(arg, "additive");
2194
2195 if (sp && sp > arg) {
2196 /* "additive" keyworkd is included. */
2197 additive = 1;
2198 *(sp - 1) = '\0';
2199 }
2200
2201 lcom = lcommunity_str2com(arg);
2202
2203 if (additive)
2204 *(sp - 1) = ' ';
2205
2206 if (!lcom)
2207 return NULL;
2208 }
2209
2210 rcs = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_com_set));
2211 rcs->lcom = lcom;
2212 rcs->additive = additive;
2213 rcs->none = none;
2214
2215 return rcs;
2216 }
2217
2218 /* Free function for set lcommunity. */
2219 static void route_set_lcommunity_free(void *rule)
2220 {
2221 struct rmap_lcom_set *rcs = rule;
2222
2223 if (rcs->lcom) {
2224 lcommunity_free(&rcs->lcom);
2225 }
2226 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcs);
2227 }
2228
2229 /* Set community rule structure. */
2230 static const struct route_map_rule_cmd route_set_lcommunity_cmd = {
2231 "large-community",
2232 route_set_lcommunity,
2233 route_set_lcommunity_compile,
2234 route_set_lcommunity_free,
2235 };
2236
2237 /* `set large-comm-list (<1-99>|<100-500>|WORD) delete' */
2238
2239 /* For large community set mechanism. */
2240 static enum route_map_cmd_result_t
2241 route_set_lcommunity_delete(void *rule, const struct prefix *pfx, void *object)
2242 {
2243 struct community_list *list;
2244 struct lcommunity *merge;
2245 struct lcommunity *new;
2246 struct lcommunity *old;
2247 struct bgp_path_info *path;
2248 struct rmap_community *rcom = rule;
2249
2250 if (!rcom)
2251 return RMAP_OKAY;
2252
2253 path = object;
2254 list = community_list_lookup(bgp_clist, rcom->name, rcom->name_hash,
2255 LARGE_COMMUNITY_LIST_MASTER);
2256 old = path->attr->lcommunity;
2257
2258 if (list && old) {
2259 merge = lcommunity_list_match_delete(lcommunity_dup(old), list);
2260 new = lcommunity_uniq_sort(merge);
2261 lcommunity_free(&merge);
2262
2263 /* HACK: if the old community is not intern'd,
2264 * we should free it here, or all reference to it may be
2265 * lost.
2266 * Really need to cleanup attribute caching sometime.
2267 */
2268 if (old->refcnt == 0)
2269 lcommunity_free(&old);
2270
2271 if (new->size == 0) {
2272 path->attr->lcommunity = NULL;
2273 path->attr->flag &=
2274 ~ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES);
2275 lcommunity_free(&new);
2276 } else {
2277 path->attr->lcommunity = new;
2278 path->attr->flag |=
2279 ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES);
2280 }
2281 }
2282
2283 return RMAP_OKAY;
2284 }
2285
2286 /* Compile function for set lcommunity. */
2287 static void *route_set_lcommunity_delete_compile(const char *arg)
2288 {
2289 struct rmap_community *rcom;
2290 char **splits;
2291 int num;
2292
2293 frrstr_split(arg, " ", &splits, &num);
2294
2295 rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
2296 rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, splits[0]);
2297 rcom->name_hash = bgp_clist_hash_key(rcom->name);
2298
2299 for (int i = 0; i < num; i++)
2300 XFREE(MTYPE_TMP, splits[i]);
2301 XFREE(MTYPE_TMP, splits);
2302
2303 return rcom;
2304 }
2305
2306 /* Free function for set lcommunity. */
2307 static void route_set_lcommunity_delete_free(void *rule)
2308 {
2309 struct rmap_community *rcom = rule;
2310
2311 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
2312 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
2313 }
2314
2315 /* Set lcommunity rule structure. */
2316 static const struct route_map_rule_cmd route_set_lcommunity_delete_cmd = {
2317 "large-comm-list",
2318 route_set_lcommunity_delete,
2319 route_set_lcommunity_delete_compile,
2320 route_set_lcommunity_delete_free,
2321 };
2322
2323
2324 /* `set comm-list (<1-99>|<100-500>|WORD) delete' */
2325
2326 /* For community set mechanism. */
2327 static enum route_map_cmd_result_t
2328 route_set_community_delete(void *rule, const struct prefix *prefix,
2329 void *object)
2330 {
2331 struct community_list *list;
2332 struct community *merge;
2333 struct community *new;
2334 struct community *old;
2335 struct bgp_path_info *path;
2336 struct rmap_community *rcom = rule;
2337
2338 if (!rcom)
2339 return RMAP_OKAY;
2340
2341 path = object;
2342 list = community_list_lookup(bgp_clist, rcom->name, rcom->name_hash,
2343 COMMUNITY_LIST_MASTER);
2344 old = path->attr->community;
2345
2346 if (list && old) {
2347 merge = community_list_match_delete(community_dup(old), list);
2348 new = community_uniq_sort(merge);
2349 community_free(&merge);
2350
2351 /* HACK: if the old community is not intern'd,
2352 * we should free it here, or all reference to it may be
2353 * lost.
2354 * Really need to cleanup attribute caching sometime.
2355 */
2356 if (old->refcnt == 0)
2357 community_free(&old);
2358
2359 if (new->size == 0) {
2360 path->attr->community = NULL;
2361 path->attr->flag &=
2362 ~ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
2363 community_free(&new);
2364 } else {
2365 path->attr->community = new;
2366 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES);
2367 }
2368 }
2369
2370 return RMAP_OKAY;
2371 }
2372
2373 /* Compile function for set community. */
2374 static void *route_set_community_delete_compile(const char *arg)
2375 {
2376 struct rmap_community *rcom;
2377 char **splits;
2378 int num;
2379
2380 frrstr_split(arg, " ", &splits, &num);
2381
2382 rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community));
2383 rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, splits[0]);
2384 rcom->name_hash = bgp_clist_hash_key(rcom->name);
2385
2386 for (int i = 0; i < num; i++)
2387 XFREE(MTYPE_TMP, splits[i]);
2388 XFREE(MTYPE_TMP, splits);
2389
2390 return rcom;
2391 }
2392
2393 /* Free function for set community. */
2394 static void route_set_community_delete_free(void *rule)
2395 {
2396 struct rmap_community *rcom = rule;
2397
2398 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name);
2399 XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom);
2400 }
2401
2402 /* Set community rule structure. */
2403 static const struct route_map_rule_cmd route_set_community_delete_cmd = {
2404 "comm-list",
2405 route_set_community_delete,
2406 route_set_community_delete_compile,
2407 route_set_community_delete_free,
2408 };
2409
2410 /* `set extcommunity rt COMMUNITY' */
2411
2412 /* For community set mechanism. Used by _rt and _soo. */
2413 static enum route_map_cmd_result_t
2414 route_set_ecommunity(void *rule, const struct prefix *prefix, void *object)
2415 {
2416 struct ecommunity *ecom;
2417 struct ecommunity *new_ecom;
2418 struct ecommunity *old_ecom;
2419 struct bgp_path_info *path;
2420
2421 ecom = rule;
2422 path = object;
2423
2424 if (!ecom)
2425 return RMAP_OKAY;
2426
2427 /* We assume additive for Extended Community. */
2428 old_ecom = path->attr->ecommunity;
2429
2430 if (old_ecom) {
2431 new_ecom = ecommunity_merge(ecommunity_dup(old_ecom), ecom);
2432
2433 /* old_ecom->refcnt = 1 => owned elsewhere, e.g.
2434 * bgp_update_receive()
2435 * ->refcnt = 0 => set by a previous route-map
2436 * statement */
2437 if (!old_ecom->refcnt)
2438 ecommunity_free(&old_ecom);
2439 } else
2440 new_ecom = ecommunity_dup(ecom);
2441
2442 /* will be intern()'d or attr_flush()'d by bgp_update_main() */
2443 path->attr->ecommunity = new_ecom;
2444
2445 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
2446
2447 return RMAP_OKAY;
2448 }
2449
2450 /* Compile function for set community. */
2451 static void *route_set_ecommunity_rt_compile(const char *arg)
2452 {
2453 struct ecommunity *ecom;
2454
2455 ecom = ecommunity_str2com(arg, ECOMMUNITY_ROUTE_TARGET, 0);
2456 if (!ecom)
2457 return NULL;
2458 return ecommunity_intern(ecom);
2459 }
2460
2461 /* Free function for set community. Used by _rt and _soo */
2462 static void route_set_ecommunity_free(void *rule)
2463 {
2464 struct ecommunity *ecom = rule;
2465 ecommunity_unintern(&ecom);
2466 }
2467
2468 /* Set community rule structure. */
2469 static const struct route_map_rule_cmd route_set_ecommunity_rt_cmd = {
2470 "extcommunity rt",
2471 route_set_ecommunity,
2472 route_set_ecommunity_rt_compile,
2473 route_set_ecommunity_free,
2474 };
2475
2476 /* `set extcommunity soo COMMUNITY' */
2477
2478 /* Compile function for set community. */
2479 static void *route_set_ecommunity_soo_compile(const char *arg)
2480 {
2481 struct ecommunity *ecom;
2482
2483 ecom = ecommunity_str2com(arg, ECOMMUNITY_SITE_ORIGIN, 0);
2484 if (!ecom)
2485 return NULL;
2486
2487 return ecommunity_intern(ecom);
2488 }
2489
2490 /* Set community rule structure. */
2491 static const struct route_map_rule_cmd route_set_ecommunity_soo_cmd = {
2492 "extcommunity soo",
2493 route_set_ecommunity,
2494 route_set_ecommunity_soo_compile,
2495 route_set_ecommunity_free,
2496 };
2497
2498 /* `set extcommunity bandwidth' */
2499
2500 struct rmap_ecomm_lb_set {
2501 uint8_t lb_type;
2502 #define RMAP_ECOMM_LB_SET_VALUE 1
2503 #define RMAP_ECOMM_LB_SET_CUMUL 2
2504 #define RMAP_ECOMM_LB_SET_NUM_MPATH 3
2505 bool non_trans;
2506 uint32_t bw;
2507 };
2508
2509 static enum route_map_cmd_result_t
2510 route_set_ecommunity_lb(void *rule, const struct prefix *prefix, void *object)
2511 {
2512 struct rmap_ecomm_lb_set *rels = rule;
2513 struct bgp_path_info *path;
2514 struct peer *peer;
2515 struct ecommunity ecom_lb = {0};
2516 struct ecommunity_val lb_eval;
2517 uint32_t bw_bytes = 0;
2518 uint16_t mpath_count = 0;
2519 struct ecommunity *new_ecom;
2520 struct ecommunity *old_ecom;
2521 as_t as;
2522
2523 path = object;
2524 peer = path->peer;
2525 if (!peer || !peer->bgp)
2526 return RMAP_ERROR;
2527
2528 /* Build link bandwidth extended community */
2529 as = (peer->bgp->as > BGP_AS_MAX) ? BGP_AS_TRANS : peer->bgp->as;
2530 if (rels->lb_type == RMAP_ECOMM_LB_SET_VALUE) {
2531 bw_bytes = ((uint64_t)rels->bw * 1000 * 1000) / 8;
2532 } else if (rels->lb_type == RMAP_ECOMM_LB_SET_CUMUL) {
2533 /* process this only for the best path. */
2534 if (!CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
2535 return RMAP_OKAY;
2536
2537 bw_bytes = (uint32_t)bgp_path_info_mpath_cumbw(path);
2538 if (!bw_bytes)
2539 return RMAP_OKAY;
2540
2541 } else if (rels->lb_type == RMAP_ECOMM_LB_SET_NUM_MPATH) {
2542
2543 /* process this only for the best path. */
2544 if (!CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
2545 return RMAP_OKAY;
2546
2547 bw_bytes = ((uint64_t)peer->bgp->lb_ref_bw * 1000 * 1000) / 8;
2548 mpath_count = bgp_path_info_mpath_count(path) + 1;
2549 bw_bytes *= mpath_count;
2550 }
2551
2552 encode_lb_extcomm(as, bw_bytes, rels->non_trans, &lb_eval);
2553
2554 /* add to route or merge with existing */
2555 old_ecom = path->attr->ecommunity;
2556 if (old_ecom) {
2557 new_ecom = ecommunity_dup(old_ecom);
2558 ecommunity_add_val(new_ecom, &lb_eval, true, true);
2559 if (!old_ecom->refcnt)
2560 ecommunity_free(&old_ecom);
2561 } else {
2562 ecom_lb.size = 1;
2563 ecom_lb.unit_size = ECOMMUNITY_SIZE;
2564 ecom_lb.val = (uint8_t *)lb_eval.val;
2565 new_ecom = ecommunity_dup(&ecom_lb);
2566 }
2567
2568 /* new_ecom will be intern()'d or attr_flush()'d in call stack */
2569 path->attr->ecommunity = new_ecom;
2570 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
2571
2572 /* Mark that route-map has set link bandwidth; used in attribute
2573 * setting decisions.
2574 */
2575 SET_FLAG(path->attr->rmap_change_flags, BATTR_RMAP_LINK_BW_SET);
2576
2577 return RMAP_OKAY;
2578 }
2579
2580 static void *route_set_ecommunity_lb_compile(const char *arg)
2581 {
2582 struct rmap_ecomm_lb_set *rels;
2583 uint8_t lb_type;
2584 uint32_t bw = 0;
2585 char bw_str[40] = {0};
2586 char *p, *str;
2587 bool non_trans = false;
2588
2589 str = (char *)arg;
2590 p = strchr(arg, ' ');
2591 if (p) {
2592 int len;
2593
2594 len = p - arg;
2595 memcpy(bw_str, arg, len);
2596 non_trans = true;
2597 str = bw_str;
2598 }
2599
2600 if (strcmp(str, "cumulative") == 0)
2601 lb_type = RMAP_ECOMM_LB_SET_CUMUL;
2602 else if (strcmp(str, "num-multipaths") == 0)
2603 lb_type = RMAP_ECOMM_LB_SET_NUM_MPATH;
2604 else {
2605 char *end = NULL;
2606
2607 bw = strtoul(str, &end, 10);
2608 if (*end != '\0')
2609 return NULL;
2610 lb_type = RMAP_ECOMM_LB_SET_VALUE;
2611 }
2612
2613 rels = XCALLOC(MTYPE_ROUTE_MAP_COMPILED,
2614 sizeof(struct rmap_ecomm_lb_set));
2615 rels->lb_type = lb_type;
2616 rels->bw = bw;
2617 rels->non_trans = non_trans;
2618
2619 return rels;
2620 }
2621
2622 static void route_set_ecommunity_lb_free(void *rule)
2623 {
2624 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2625 }
2626
2627 /* Set community rule structure. */
2628 struct route_map_rule_cmd route_set_ecommunity_lb_cmd = {
2629 "extcommunity bandwidth",
2630 route_set_ecommunity_lb,
2631 route_set_ecommunity_lb_compile,
2632 route_set_ecommunity_lb_free,
2633 };
2634
2635 /* `set origin ORIGIN' */
2636
2637 /* For origin set. */
2638 static enum route_map_cmd_result_t
2639 route_set_origin(void *rule, const struct prefix *prefix, void *object)
2640 {
2641 uint8_t *origin;
2642 struct bgp_path_info *path;
2643
2644 origin = rule;
2645 path = object;
2646
2647 path->attr->origin = *origin;
2648
2649 return RMAP_OKAY;
2650 }
2651
2652 /* Compile function for origin set. */
2653 static void *route_set_origin_compile(const char *arg)
2654 {
2655 uint8_t *origin;
2656
2657 origin = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint8_t));
2658
2659 if (strcmp(arg, "igp") == 0)
2660 *origin = 0;
2661 else if (strcmp(arg, "egp") == 0)
2662 *origin = 1;
2663 else
2664 *origin = 2;
2665
2666 return origin;
2667 }
2668
2669 /* Compile function for origin set. */
2670 static void route_set_origin_free(void *rule)
2671 {
2672 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2673 }
2674
2675 /* Set origin rule structure. */
2676 static const struct route_map_rule_cmd route_set_origin_cmd = {
2677 "origin",
2678 route_set_origin,
2679 route_set_origin_compile,
2680 route_set_origin_free,
2681 };
2682
2683 /* `set atomic-aggregate' */
2684
2685 /* For atomic aggregate set. */
2686 static enum route_map_cmd_result_t
2687 route_set_atomic_aggregate(void *rule, const struct prefix *pfx, void *object)
2688 {
2689 struct bgp_path_info *path;
2690
2691 path = object;
2692 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE);
2693
2694 return RMAP_OKAY;
2695 }
2696
2697 /* Compile function for atomic aggregate. */
2698 static void *route_set_atomic_aggregate_compile(const char *arg)
2699 {
2700 return (void *)1;
2701 }
2702
2703 /* Compile function for atomic aggregate. */
2704 static void route_set_atomic_aggregate_free(void *rule)
2705 {
2706 return;
2707 }
2708
2709 /* Set atomic aggregate rule structure. */
2710 static const struct route_map_rule_cmd route_set_atomic_aggregate_cmd = {
2711 "atomic-aggregate",
2712 route_set_atomic_aggregate,
2713 route_set_atomic_aggregate_compile,
2714 route_set_atomic_aggregate_free,
2715 };
2716
2717 /* `set aggregator as AS A.B.C.D' */
2718 struct aggregator {
2719 as_t as;
2720 struct in_addr address;
2721 };
2722
2723 static enum route_map_cmd_result_t
2724 route_set_aggregator_as(void *rule, const struct prefix *prefix, void *object)
2725 {
2726 struct bgp_path_info *path;
2727 struct aggregator *aggregator;
2728
2729 path = object;
2730 aggregator = rule;
2731
2732 path->attr->aggregator_as = aggregator->as;
2733 path->attr->aggregator_addr = aggregator->address;
2734 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR);
2735
2736 return RMAP_OKAY;
2737 }
2738
2739 static void *route_set_aggregator_as_compile(const char *arg)
2740 {
2741 struct aggregator *aggregator;
2742 char as[10];
2743 char address[20];
2744 int ret;
2745
2746 aggregator =
2747 XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct aggregator));
2748 if (sscanf(arg, "%s %s", as, address) != 2) {
2749 XFREE(MTYPE_ROUTE_MAP_COMPILED, aggregator);
2750 return NULL;
2751 }
2752
2753 aggregator->as = strtoul(as, NULL, 10);
2754 ret = inet_aton(address, &aggregator->address);
2755 if (ret == 0) {
2756 XFREE(MTYPE_ROUTE_MAP_COMPILED, aggregator);
2757 return NULL;
2758 }
2759 return aggregator;
2760 }
2761
2762 static void route_set_aggregator_as_free(void *rule)
2763 {
2764 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2765 }
2766
2767 static const struct route_map_rule_cmd route_set_aggregator_as_cmd = {
2768 "aggregator as",
2769 route_set_aggregator_as,
2770 route_set_aggregator_as_compile,
2771 route_set_aggregator_as_free,
2772 };
2773
2774 /* Set tag to object. object must be pointer to struct bgp_path_info */
2775 static enum route_map_cmd_result_t
2776 route_set_tag(void *rule, const struct prefix *prefix, void *object)
2777 {
2778 route_tag_t *tag;
2779 struct bgp_path_info *path;
2780
2781 tag = rule;
2782 path = object;
2783
2784 /* Set tag value */
2785 path->attr->tag = *tag;
2786
2787 return RMAP_OKAY;
2788 }
2789
2790 /* Route map commands for tag set. */
2791 static const struct route_map_rule_cmd route_set_tag_cmd = {
2792 "tag",
2793 route_set_tag,
2794 route_map_rule_tag_compile,
2795 route_map_rule_tag_free,
2796 };
2797
2798 /* Set label-index to object. object must be pointer to struct bgp_path_info */
2799 static enum route_map_cmd_result_t
2800 route_set_label_index(void *rule, const struct prefix *prefix, void *object)
2801 {
2802 struct rmap_value *rv;
2803 struct bgp_path_info *path;
2804 uint32_t label_index;
2805
2806 /* Fetch routemap's rule information. */
2807 rv = rule;
2808 path = object;
2809
2810 /* Set label-index value. */
2811 label_index = rv->value;
2812 if (label_index) {
2813 path->attr->label_index = label_index;
2814 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID);
2815 }
2816
2817 return RMAP_OKAY;
2818 }
2819
2820 /* Route map commands for label-index set. */
2821 static const struct route_map_rule_cmd route_set_label_index_cmd = {
2822 "label-index",
2823 route_set_label_index,
2824 route_value_compile,
2825 route_value_free,
2826 };
2827
2828 /* `match ipv6 address IP_ACCESS_LIST' */
2829
2830 static enum route_map_cmd_result_t
2831 route_match_ipv6_address(void *rule, const struct prefix *prefix, void *object)
2832 {
2833 struct access_list *alist;
2834
2835 if (prefix->family == AF_INET6) {
2836 alist = access_list_lookup(AFI_IP6, (char *)rule);
2837 if (alist == NULL)
2838 return RMAP_NOMATCH;
2839
2840 return (access_list_apply(alist, prefix) == FILTER_DENY
2841 ? RMAP_NOMATCH
2842 : RMAP_MATCH);
2843 }
2844 return RMAP_NOMATCH;
2845 }
2846
2847 static void *route_match_ipv6_address_compile(const char *arg)
2848 {
2849 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
2850 }
2851
2852 static void route_match_ipv6_address_free(void *rule)
2853 {
2854 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2855 }
2856
2857 /* Route map commands for ip address matching. */
2858 static const struct route_map_rule_cmd route_match_ipv6_address_cmd = {
2859 "ipv6 address",
2860 route_match_ipv6_address,
2861 route_match_ipv6_address_compile,
2862 route_match_ipv6_address_free
2863 };
2864
2865 /* `match ipv6 next-hop IP_ADDRESS' */
2866
2867 static enum route_map_cmd_result_t
2868 route_match_ipv6_next_hop(void *rule, const struct prefix *prefix, void *object)
2869 {
2870 struct in6_addr *addr = rule;
2871 struct bgp_path_info *path;
2872
2873 path = object;
2874
2875 if (IPV6_ADDR_SAME(&path->attr->mp_nexthop_global, addr))
2876 return RMAP_MATCH;
2877
2878 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
2879 && IPV6_ADDR_SAME(&path->attr->mp_nexthop_local, rule))
2880 return RMAP_MATCH;
2881
2882 return RMAP_NOMATCH;
2883 }
2884
2885 static void *route_match_ipv6_next_hop_compile(const char *arg)
2886 {
2887 struct in6_addr *address;
2888 int ret;
2889
2890 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
2891
2892 ret = inet_pton(AF_INET6, arg, address);
2893 if (!ret) {
2894 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
2895 return NULL;
2896 }
2897
2898 return address;
2899 }
2900
2901 static void route_match_ipv6_next_hop_free(void *rule)
2902 {
2903 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2904 }
2905
2906 static const struct route_map_rule_cmd route_match_ipv6_next_hop_cmd = {
2907 "ipv6 next-hop",
2908 route_match_ipv6_next_hop,
2909 route_match_ipv6_next_hop_compile,
2910 route_match_ipv6_next_hop_free
2911 };
2912
2913 /* `match ip next-hop IP_ADDRESS' */
2914
2915 static enum route_map_cmd_result_t
2916 route_match_ipv4_next_hop(void *rule, const struct prefix *prefix, void *object)
2917 {
2918 struct in_addr *addr = rule;
2919 struct bgp_path_info *path;
2920
2921 path = object;
2922
2923 if (path->attr->nexthop.s_addr == addr->s_addr
2924 || (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV4
2925 && IPV4_ADDR_SAME(&path->attr->mp_nexthop_global_in, addr)))
2926 return RMAP_MATCH;
2927
2928 return RMAP_NOMATCH;
2929 }
2930
2931 static void *route_match_ipv4_next_hop_compile(const char *arg)
2932 {
2933 struct in_addr *address;
2934 int ret;
2935
2936 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
2937
2938 ret = inet_pton(AF_INET, arg, address);
2939 if (!ret) {
2940 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
2941 return NULL;
2942 }
2943
2944 return address;
2945 }
2946
2947 static void route_match_ipv4_next_hop_free(void *rule)
2948 {
2949 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2950 }
2951
2952 static const struct route_map_rule_cmd route_match_ipv4_next_hop_cmd = {
2953 "ip next-hop address",
2954 route_match_ipv4_next_hop,
2955 route_match_ipv4_next_hop_compile,
2956 route_match_ipv4_next_hop_free
2957 };
2958
2959 /* `match ipv6 address prefix-list PREFIX_LIST' */
2960
2961 static enum route_map_cmd_result_t
2962 route_match_ipv6_address_prefix_list(void *rule, const struct prefix *prefix,
2963 void *object)
2964 {
2965 return route_match_address_prefix_list(rule, AFI_IP6, prefix, object);
2966 }
2967
2968 static void *route_match_ipv6_address_prefix_list_compile(const char *arg)
2969 {
2970 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
2971 }
2972
2973 static void route_match_ipv6_address_prefix_list_free(void *rule)
2974 {
2975 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2976 }
2977
2978 static const struct route_map_rule_cmd
2979 route_match_ipv6_address_prefix_list_cmd = {
2980 "ipv6 address prefix-list",
2981 route_match_ipv6_address_prefix_list,
2982 route_match_ipv6_address_prefix_list_compile,
2983 route_match_ipv6_address_prefix_list_free
2984 };
2985
2986 /* `match ipv6 next-hop type <TYPE>' */
2987
2988 static enum route_map_cmd_result_t
2989 route_match_ipv6_next_hop_type(void *rule, const struct prefix *prefix,
2990 void *object)
2991 {
2992 struct bgp_path_info *path;
2993 struct in6_addr *addr = rule;
2994
2995 if (prefix->family == AF_INET6) {
2996 path = (struct bgp_path_info *)object;
2997 if (!path)
2998 return RMAP_NOMATCH;
2999
3000 if (IPV6_ADDR_SAME(&path->attr->mp_nexthop_global, addr)
3001 && !path->attr->nh_ifindex)
3002 return RMAP_MATCH;
3003 }
3004
3005 return RMAP_NOMATCH;
3006 }
3007
3008 static void *route_match_ipv6_next_hop_type_compile(const char *arg)
3009 {
3010 struct in6_addr *address;
3011 int ret;
3012
3013 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3014
3015 ret = inet_pton(AF_INET6, "::0", address);
3016 if (!ret) {
3017 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3018 return NULL;
3019 }
3020
3021 return address;
3022 }
3023
3024 static void route_match_ipv6_next_hop_type_free(void *rule)
3025 {
3026 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3027 }
3028
3029 static const struct route_map_rule_cmd
3030 route_match_ipv6_next_hop_type_cmd = {
3031 "ipv6 next-hop type",
3032 route_match_ipv6_next_hop_type,
3033 route_match_ipv6_next_hop_type_compile,
3034 route_match_ipv6_next_hop_type_free
3035 };
3036
3037 /* `set ipv6 nexthop global IP_ADDRESS' */
3038
3039 /* Set nexthop to object. ojbect must be pointer to struct attr. */
3040 static enum route_map_cmd_result_t
3041 route_set_ipv6_nexthop_global(void *rule, const struct prefix *p, void *object)
3042 {
3043 struct in6_addr *address;
3044 struct bgp_path_info *path;
3045
3046 /* Fetch routemap's rule information. */
3047 address = rule;
3048 path = object;
3049
3050 /* Set next hop value. */
3051 path->attr->mp_nexthop_global = *address;
3052
3053 /* Set nexthop length. */
3054 if (path->attr->mp_nexthop_len == 0)
3055 path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
3056
3057 SET_FLAG(path->attr->rmap_change_flags,
3058 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED);
3059
3060 return RMAP_OKAY;
3061 }
3062
3063 /* Route map `ip next-hop' compile function. Given string is converted
3064 to struct in_addr structure. */
3065 static void *route_set_ipv6_nexthop_global_compile(const char *arg)
3066 {
3067 int ret;
3068 struct in6_addr *address;
3069
3070 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3071
3072 ret = inet_pton(AF_INET6, arg, address);
3073
3074 if (ret == 0) {
3075 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3076 return NULL;
3077 }
3078
3079 return address;
3080 }
3081
3082 /* Free route map's compiled `ip next-hop' value. */
3083 static void route_set_ipv6_nexthop_global_free(void *rule)
3084 {
3085 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3086 }
3087
3088 /* Route map commands for ip nexthop set. */
3089 static const struct route_map_rule_cmd
3090 route_set_ipv6_nexthop_global_cmd = {
3091 "ipv6 next-hop global",
3092 route_set_ipv6_nexthop_global,
3093 route_set_ipv6_nexthop_global_compile,
3094 route_set_ipv6_nexthop_global_free
3095 };
3096
3097 /* Set next-hop preference value. */
3098 static enum route_map_cmd_result_t
3099 route_set_ipv6_nexthop_prefer_global(void *rule, const struct prefix *prefix,
3100 void *object)
3101 {
3102 struct bgp_path_info *path;
3103 struct peer *peer;
3104
3105 /* Fetch routemap's rule information. */
3106 path = object;
3107 peer = path->peer;
3108
3109 if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN)
3110 || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IMPORT)) {
3111 /* Set next hop preference to global */
3112 path->attr->mp_nexthop_prefer_global = true;
3113 SET_FLAG(path->attr->rmap_change_flags,
3114 BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED);
3115 } else {
3116 path->attr->mp_nexthop_prefer_global = false;
3117 SET_FLAG(path->attr->rmap_change_flags,
3118 BATTR_RMAP_IPV6_PREFER_GLOBAL_CHANGED);
3119 }
3120
3121 return RMAP_OKAY;
3122 }
3123
3124 static void *route_set_ipv6_nexthop_prefer_global_compile(const char *arg)
3125 {
3126 int *rins = NULL;
3127
3128 rins = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(int));
3129 *rins = 1;
3130
3131 return rins;
3132 }
3133
3134 /* Free route map's compiled `ip next-hop' value. */
3135 static void route_set_ipv6_nexthop_prefer_global_free(void *rule)
3136 {
3137 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3138 }
3139
3140 /* Route map commands for ip nexthop set preferred. */
3141 static const struct route_map_rule_cmd
3142 route_set_ipv6_nexthop_prefer_global_cmd = {
3143 "ipv6 next-hop prefer-global",
3144 route_set_ipv6_nexthop_prefer_global,
3145 route_set_ipv6_nexthop_prefer_global_compile,
3146 route_set_ipv6_nexthop_prefer_global_free
3147 };
3148
3149 /* `set ipv6 nexthop local IP_ADDRESS' */
3150
3151 /* Set nexthop to object. ojbect must be pointer to struct attr. */
3152 static enum route_map_cmd_result_t
3153 route_set_ipv6_nexthop_local(void *rule, const struct prefix *p, void *object)
3154 {
3155 struct in6_addr *address;
3156 struct bgp_path_info *path;
3157
3158 /* Fetch routemap's rule information. */
3159 address = rule;
3160 path = object;
3161
3162 /* Set next hop value. */
3163 path->attr->mp_nexthop_local = *address;
3164
3165 /* Set nexthop length. */
3166 if (path->attr->mp_nexthop_len != BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
3167 path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
3168
3169 SET_FLAG(path->attr->rmap_change_flags,
3170 BATTR_RMAP_IPV6_LL_NHOP_CHANGED);
3171
3172 return RMAP_OKAY;
3173 }
3174
3175 /* Route map `ip nexthop' compile function. Given string is converted
3176 to struct in_addr structure. */
3177 static void *route_set_ipv6_nexthop_local_compile(const char *arg)
3178 {
3179 int ret;
3180 struct in6_addr *address;
3181
3182 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3183
3184 ret = inet_pton(AF_INET6, arg, address);
3185
3186 if (ret == 0) {
3187 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3188 return NULL;
3189 }
3190
3191 return address;
3192 }
3193
3194 /* Free route map's compiled `ip nexthop' value. */
3195 static void route_set_ipv6_nexthop_local_free(void *rule)
3196 {
3197 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3198 }
3199
3200 /* Route map commands for ip nexthop set. */
3201 static const struct route_map_rule_cmd
3202 route_set_ipv6_nexthop_local_cmd = {
3203 "ipv6 next-hop local",
3204 route_set_ipv6_nexthop_local,
3205 route_set_ipv6_nexthop_local_compile,
3206 route_set_ipv6_nexthop_local_free
3207 };
3208
3209 /* `set ipv6 nexthop peer-address' */
3210
3211 /* Set nexthop to object. ojbect must be pointer to struct attr. */
3212 static enum route_map_cmd_result_t
3213 route_set_ipv6_nexthop_peer(void *rule, const struct prefix *pfx, void *object)
3214 {
3215 struct in6_addr peer_address;
3216 struct bgp_path_info *path;
3217 struct peer *peer;
3218
3219 /* Fetch routemap's rule information. */
3220 path = object;
3221 peer = path->peer;
3222
3223 if ((CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN)
3224 || CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IMPORT))
3225 && peer->su_remote
3226 && sockunion_family(peer->su_remote) == AF_INET6) {
3227 peer_address = peer->su_remote->sin6.sin6_addr;
3228 /* Set next hop value and length in attribute. */
3229 if (IN6_IS_ADDR_LINKLOCAL(&peer_address)) {
3230 path->attr->mp_nexthop_local = peer_address;
3231 if (path->attr->mp_nexthop_len
3232 != BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
3233 path->attr->mp_nexthop_len =
3234 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
3235 } else {
3236 path->attr->mp_nexthop_global = peer_address;
3237 if (path->attr->mp_nexthop_len == 0)
3238 path->attr->mp_nexthop_len =
3239 BGP_ATTR_NHLEN_IPV6_GLOBAL;
3240 }
3241
3242 } else if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT)) {
3243 /* The next hop value will be set as part of packet
3244 * rewrite.
3245 * Set the flags here to indicate that rewrite needs to
3246 * be done.
3247 * Also, clear the value - we clear both global and
3248 * link-local
3249 * nexthops, whether we send one or both is determined
3250 * elsewhere.
3251 */
3252 SET_FLAG(path->attr->rmap_change_flags,
3253 BATTR_RMAP_NEXTHOP_PEER_ADDRESS);
3254 /* clear next hop value. */
3255 memset(&(path->attr->mp_nexthop_global), 0,
3256 sizeof(struct in6_addr));
3257 memset(&(path->attr->mp_nexthop_local), 0,
3258 sizeof(struct in6_addr));
3259 }
3260
3261 return RMAP_OKAY;
3262 }
3263
3264 /* Route map `ip next-hop' compile function. Given string is converted
3265 to struct in_addr structure. */
3266 static void *route_set_ipv6_nexthop_peer_compile(const char *arg)
3267 {
3268 int *rins = NULL;
3269
3270 rins = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(int));
3271 *rins = 1;
3272
3273 return rins;
3274 }
3275
3276 /* Free route map's compiled `ip next-hop' value. */
3277 static void route_set_ipv6_nexthop_peer_free(void *rule)
3278 {
3279 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3280 }
3281
3282 /* Route map commands for ip nexthop set. */
3283 static const struct route_map_rule_cmd route_set_ipv6_nexthop_peer_cmd = {
3284 "ipv6 next-hop peer-address",
3285 route_set_ipv6_nexthop_peer,
3286 route_set_ipv6_nexthop_peer_compile,
3287 route_set_ipv6_nexthop_peer_free
3288 };
3289
3290 /* `set ipv4 vpn next-hop A.B.C.D' */
3291
3292 static enum route_map_cmd_result_t
3293 route_set_vpnv4_nexthop(void *rule, const struct prefix *prefix, void *object)
3294 {
3295 struct in_addr *address;
3296 struct bgp_path_info *path;
3297
3298 /* Fetch routemap's rule information. */
3299 address = rule;
3300 path = object;
3301
3302 /* Set next hop value. */
3303 path->attr->mp_nexthop_global_in = *address;
3304 path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
3305
3306 return RMAP_OKAY;
3307 }
3308
3309 static void *route_set_vpnv4_nexthop_compile(const char *arg)
3310 {
3311 int ret;
3312 struct in_addr *address;
3313
3314 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
3315
3316 ret = inet_aton(arg, address);
3317
3318 if (ret == 0) {
3319 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3320 return NULL;
3321 }
3322
3323 return address;
3324 }
3325
3326 /* `set ipv6 vpn next-hop A.B.C.D' */
3327
3328 static enum route_map_cmd_result_t
3329 route_set_vpnv6_nexthop(void *rule, const struct prefix *prefix, void *object)
3330 {
3331 struct in6_addr *address;
3332 struct bgp_path_info *path;
3333
3334 /* Fetch routemap's rule information. */
3335 address = rule;
3336 path = object;
3337
3338 /* Set next hop value. */
3339 memcpy(&path->attr->mp_nexthop_global, address,
3340 sizeof(struct in6_addr));
3341 path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_VPNV6_GLOBAL;
3342
3343 return RMAP_OKAY;
3344 }
3345
3346 static void *route_set_vpnv6_nexthop_compile(const char *arg)
3347 {
3348 int ret;
3349 struct in6_addr *address;
3350
3351 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3352 ret = inet_pton(AF_INET6, arg, address);
3353
3354 if (ret == 0) {
3355 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3356 return NULL;
3357 }
3358
3359 return address;
3360 }
3361
3362 static void route_set_vpn_nexthop_free(void *rule)
3363 {
3364 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3365 }
3366
3367 /* Route map commands for ipv4 next-hop set. */
3368 static const struct route_map_rule_cmd route_set_vpnv4_nexthop_cmd = {
3369 "ipv4 vpn next-hop",
3370 route_set_vpnv4_nexthop,
3371 route_set_vpnv4_nexthop_compile,
3372 route_set_vpn_nexthop_free
3373 };
3374
3375 /* Route map commands for ipv6 next-hop set. */
3376 static const struct route_map_rule_cmd route_set_vpnv6_nexthop_cmd = {
3377 "ipv6 vpn next-hop",
3378 route_set_vpnv6_nexthop,
3379 route_set_vpnv6_nexthop_compile,
3380 route_set_vpn_nexthop_free
3381 };
3382
3383 /* `set originator-id' */
3384
3385 /* For origin set. */
3386 static enum route_map_cmd_result_t
3387 route_set_originator_id(void *rule, const struct prefix *prefix, void *object)
3388 {
3389 struct in_addr *address;
3390 struct bgp_path_info *path;
3391
3392 address = rule;
3393 path = object;
3394
3395 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID);
3396 path->attr->originator_id = *address;
3397
3398 return RMAP_OKAY;
3399 }
3400
3401 /* Compile function for originator-id set. */
3402 static void *route_set_originator_id_compile(const char *arg)
3403 {
3404 int ret;
3405 struct in_addr *address;
3406
3407 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
3408
3409 ret = inet_aton(arg, address);
3410
3411 if (ret == 0) {
3412 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3413 return NULL;
3414 }
3415
3416 return address;
3417 }
3418
3419 /* Compile function for originator_id set. */
3420 static void route_set_originator_id_free(void *rule)
3421 {
3422 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3423 }
3424
3425 /* Set originator-id rule structure. */
3426 static const struct route_map_rule_cmd route_set_originator_id_cmd = {
3427 "originator-id",
3428 route_set_originator_id,
3429 route_set_originator_id_compile,
3430 route_set_originator_id_free,
3431 };
3432
3433 /*
3434 * This is the workhorse routine for processing in/out routemap
3435 * modifications.
3436 */
3437 static void bgp_route_map_process_peer(const char *rmap_name,
3438 struct route_map *map, struct peer *peer,
3439 int afi, int safi, int route_update)
3440 {
3441 struct bgp_filter *filter;
3442
3443 if (!peer || !rmap_name)
3444 return;
3445
3446 filter = &peer->filter[afi][safi];
3447 /*
3448 * in is for non-route-server clients,
3449 * out is for all peers
3450 */
3451 if (filter->map[RMAP_IN].name
3452 && (strcmp(rmap_name, filter->map[RMAP_IN].name) == 0)) {
3453 filter->map[RMAP_IN].map = map;
3454
3455 if (route_update && peer->status == Established) {
3456 if (CHECK_FLAG(peer->af_flags[afi][safi],
3457 PEER_FLAG_SOFT_RECONFIG)) {
3458 if (bgp_debug_update(peer, NULL, NULL, 1))
3459 zlog_debug(
3460 "Processing route_map %s(%s:%s) update on peer %s (inbound, soft-reconfig)",
3461 rmap_name, afi2str(afi),
3462 safi2str(safi), peer->host);
3463
3464 bgp_soft_reconfig_in(peer, afi, safi);
3465 } else if (CHECK_FLAG(peer->cap,
3466 PEER_CAP_REFRESH_OLD_RCV)
3467 || CHECK_FLAG(peer->cap,
3468 PEER_CAP_REFRESH_NEW_RCV)) {
3469 if (bgp_debug_update(peer, NULL, NULL, 1))
3470 zlog_debug(
3471 "Processing route_map %s(%s:%s) update on peer %s (inbound, route-refresh)",
3472 rmap_name, afi2str(afi),
3473 safi2str(safi), peer->host);
3474 bgp_route_refresh_send(
3475 peer, afi, safi, 0, 0, 0,
3476 BGP_ROUTE_REFRESH_NORMAL);
3477 }
3478 }
3479 }
3480
3481 /*
3482 * For outbound, unsuppress and default-originate map change (content or
3483 * map created), merely update the "config" here, the actual route
3484 * announcement happens at the group level.
3485 */
3486 if (filter->map[RMAP_OUT].name
3487 && (strcmp(rmap_name, filter->map[RMAP_OUT].name) == 0))
3488 filter->map[RMAP_OUT].map = map;
3489
3490 if (filter->usmap.name && (strcmp(rmap_name, filter->usmap.name) == 0))
3491 filter->usmap.map = map;
3492
3493 if (filter->advmap.aname
3494 && (strcmp(rmap_name, filter->advmap.aname) == 0)) {
3495 filter->advmap.amap = map;
3496 }
3497
3498 if (filter->advmap.cname
3499 && (strcmp(rmap_name, filter->advmap.cname) == 0)) {
3500 filter->advmap.cmap = map;
3501 }
3502
3503 if (peer->default_rmap[afi][safi].name
3504 && (strcmp(rmap_name, peer->default_rmap[afi][safi].name) == 0))
3505 peer->default_rmap[afi][safi].map = map;
3506
3507 /* Notify BGP conditional advertisement scanner percess */
3508 peer->advmap_config_change[afi][safi] = true;
3509 }
3510
3511 static void bgp_route_map_update_peer_group(const char *rmap_name,
3512 struct route_map *map,
3513 struct bgp *bgp)
3514 {
3515 struct peer_group *group;
3516 struct listnode *node, *nnode;
3517 struct bgp_filter *filter;
3518 int afi, safi;
3519 int direct;
3520
3521 if (!bgp)
3522 return;
3523
3524 /* All the peers have been updated correctly already. This is
3525 * just updating the placeholder data. No real update required.
3526 */
3527 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
3528 FOREACH_AFI_SAFI (afi, safi) {
3529 filter = &group->conf->filter[afi][safi];
3530
3531 for (direct = RMAP_IN; direct < RMAP_MAX; direct++) {
3532 if ((filter->map[direct].name)
3533 && (strcmp(rmap_name,
3534 filter->map[direct].name)
3535 == 0))
3536 filter->map[direct].map = map;
3537 }
3538
3539 if (filter->usmap.name
3540 && (strcmp(rmap_name, filter->usmap.name) == 0))
3541 filter->usmap.map = map;
3542 }
3543 }
3544 }
3545
3546 /*
3547 * Note that if an extreme number (tens of thousands) of route-maps are in use
3548 * and if bgp has an extreme number of peers, network statements, etc then this
3549 * function can consume a lot of cycles. This is due to this function being
3550 * called for each route-map and within this function we walk the list of peers,
3551 * network statements, etc looking to see if they use this route-map.
3552 */
3553 static void bgp_route_map_process_update(struct bgp *bgp, const char *rmap_name,
3554 int route_update)
3555 {
3556 int i;
3557 bool matched;
3558 afi_t afi;
3559 safi_t safi;
3560 struct peer *peer;
3561 struct bgp_dest *bn;
3562 struct bgp_static *bgp_static;
3563 struct bgp_aggregate *aggregate;
3564 struct listnode *node, *nnode;
3565 struct route_map *map;
3566 char buf[INET6_ADDRSTRLEN];
3567
3568 map = route_map_lookup_by_name(rmap_name);
3569
3570 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
3571
3572 /* Ignore dummy peer-group structure */
3573 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
3574 continue;
3575
3576 FOREACH_AFI_SAFI (afi, safi) {
3577 /* process in/out/import/export/default-orig
3578 * route-maps */
3579 bgp_route_map_process_peer(rmap_name, map, peer, afi,
3580 safi, route_update);
3581 }
3582 }
3583
3584 /* for outbound/default-orig route-maps, process for groups */
3585 update_group_policy_update(bgp, BGP_POLICY_ROUTE_MAP, rmap_name,
3586 route_update, 0);
3587
3588 /* update peer-group config (template) */
3589 bgp_route_map_update_peer_group(rmap_name, map, bgp);
3590
3591 FOREACH_AFI_SAFI (afi, safi) {
3592 /* For table route-map updates. */
3593 if (!bgp_fibupd_safi(safi))
3594 continue;
3595
3596 if (bgp->table_map[afi][safi].name
3597 && (strcmp(rmap_name, bgp->table_map[afi][safi].name)
3598 == 0)) {
3599
3600 /* bgp->table_map[afi][safi].map is NULL.
3601 * i.e Route map creation event.
3602 * So update applied_counter.
3603 * If it is not NULL, i.e It may be routemap updation or
3604 * deletion. so no need to update the counter.
3605 */
3606 if (!bgp->table_map[afi][safi].map)
3607 route_map_counter_increment(map);
3608 bgp->table_map[afi][safi].map = map;
3609
3610 if (BGP_DEBUG(zebra, ZEBRA))
3611 zlog_debug(
3612 "Processing route_map %s(%s:%s) update on table map",
3613 rmap_name, afi2str(afi),
3614 safi2str(safi));
3615 if (route_update)
3616 bgp_zebra_announce_table(bgp, afi, safi);
3617 }
3618
3619 /* For network route-map updates. */
3620 for (bn = bgp_table_top(bgp->route[afi][safi]); bn;
3621 bn = bgp_route_next(bn)) {
3622 bgp_static = bgp_dest_get_bgp_static_info(bn);
3623 if (!bgp_static)
3624 continue;
3625
3626 if (!bgp_static->rmap.name
3627 || (strcmp(rmap_name, bgp_static->rmap.name) != 0))
3628 continue;
3629
3630 if (!bgp_static->rmap.map)
3631 route_map_counter_increment(map);
3632
3633 bgp_static->rmap.map = map;
3634
3635 if (route_update && !bgp_static->backdoor) {
3636 const struct prefix *bn_p =
3637 bgp_dest_get_prefix(bn);
3638
3639 if (bgp_debug_zebra(bn_p))
3640 zlog_debug(
3641 "Processing route_map %s(%s:%s) update on static route %s",
3642 rmap_name, afi2str(afi),
3643 safi2str(safi),
3644 inet_ntop(bn_p->family,
3645 &bn_p->u.prefix, buf,
3646 INET6_ADDRSTRLEN));
3647 bgp_static_update(bgp, bn_p, bgp_static, afi,
3648 safi);
3649 }
3650 }
3651
3652 /* For aggregate-address route-map updates. */
3653 for (bn = bgp_table_top(bgp->aggregate[afi][safi]); bn;
3654 bn = bgp_route_next(bn)) {
3655 aggregate = bgp_dest_get_bgp_aggregate_info(bn);
3656 if (!aggregate)
3657 continue;
3658
3659 matched = false;
3660
3661 /* Update suppress map pointer. */
3662 if (aggregate->suppress_map_name
3663 && strmatch(aggregate->suppress_map_name,
3664 rmap_name)) {
3665 if (aggregate->rmap.map == NULL)
3666 route_map_counter_increment(map);
3667
3668 aggregate->suppress_map = map;
3669
3670 bgp_aggregate_toggle_suppressed(
3671 aggregate, bgp, bgp_dest_get_prefix(bn),
3672 afi, safi, false);
3673
3674 matched = true;
3675 }
3676
3677 if (aggregate->rmap.name
3678 && strmatch(rmap_name, aggregate->rmap.name)) {
3679 if (aggregate->rmap.map == NULL)
3680 route_map_counter_increment(map);
3681
3682 aggregate->rmap.map = map;
3683
3684 matched = true;
3685 }
3686
3687 if (matched && route_update) {
3688 const struct prefix *bn_p =
3689 bgp_dest_get_prefix(bn);
3690
3691 if (bgp_debug_zebra(bn_p))
3692 zlog_debug(
3693 "Processing route_map %s(%s:%s) update on aggregate-address route %s",
3694 rmap_name, afi2str(afi),
3695 safi2str(safi),
3696 inet_ntop(bn_p->family,
3697 &bn_p->u.prefix, buf,
3698 INET6_ADDRSTRLEN));
3699 bgp_aggregate_route(bgp, bn_p, afi, safi,
3700 aggregate);
3701 }
3702 }
3703 }
3704
3705 /* For redistribute route-map updates. */
3706 for (afi = AFI_IP; afi < AFI_MAX; afi++)
3707 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
3708 struct list *red_list;
3709 struct bgp_redist *red;
3710
3711 red_list = bgp->redist[afi][i];
3712 if (!red_list)
3713 continue;
3714
3715 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
3716 if (!red->rmap.name
3717 || (strcmp(rmap_name, red->rmap.name) != 0))
3718 continue;
3719
3720 if (!red->rmap.map)
3721 route_map_counter_increment(map);
3722
3723 red->rmap.map = map;
3724
3725 if (!route_update)
3726 continue;
3727
3728 if (BGP_DEBUG(zebra, ZEBRA))
3729 zlog_debug(
3730 "Processing route_map %s(%s:%s) update on redistributed routes",
3731 rmap_name, afi2str(afi),
3732 safi2str(safi));
3733
3734 bgp_redistribute_resend(bgp, afi, i,
3735 red->instance);
3736 }
3737 }
3738
3739 /* for type5 command route-maps */
3740 FOREACH_AFI_SAFI (afi, safi) {
3741 if (!bgp->adv_cmd_rmap[afi][safi].name
3742 || strcmp(rmap_name, bgp->adv_cmd_rmap[afi][safi].name)
3743 != 0)
3744 continue;
3745
3746 /* Make sure the route-map is populated here if not already done */
3747 bgp->adv_cmd_rmap[afi][safi].map = map;
3748
3749 if (BGP_DEBUG(zebra, ZEBRA))
3750 zlog_debug(
3751 "Processing route_map %s(%s:%s) update on advertise type5 route command",
3752 rmap_name, afi2str(afi), safi2str(safi));
3753
3754 if (route_update && advertise_type5_routes(bgp, afi)) {
3755 bgp_evpn_withdraw_type5_routes(bgp, afi, safi);
3756 bgp_evpn_advertise_type5_routes(bgp, afi, safi);
3757 }
3758 }
3759 }
3760
3761 static void bgp_route_map_process_update_cb(char *rmap_name)
3762 {
3763 struct listnode *node, *nnode;
3764 struct bgp *bgp;
3765
3766 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
3767 bgp_route_map_process_update(bgp, rmap_name, 1);
3768
3769 #ifdef ENABLE_BGP_VNC
3770 /* zlog_debug("%s: calling vnc_routemap_update", __func__); */
3771 vnc_routemap_update(bgp, __func__);
3772 #endif
3773 }
3774
3775 vpn_policy_routemap_event(rmap_name);
3776 }
3777
3778 int bgp_route_map_update_timer(struct thread *thread)
3779 {
3780 bm->t_rmap_update = NULL;
3781
3782 route_map_walk_update_list(bgp_route_map_process_update_cb);
3783
3784 return 0;
3785 }
3786
3787 static void bgp_route_map_mark_update(const char *rmap_name)
3788 {
3789 struct listnode *node, *nnode;
3790 struct bgp *bgp;
3791
3792 /* If new update is received before the current timer timed out,
3793 * turn it off and start a new timer.
3794 */
3795 THREAD_OFF(bm->t_rmap_update);
3796
3797 /* rmap_update_timer of 0 means don't do route updates */
3798 if (bm->rmap_update_timer) {
3799 thread_add_timer(bm->master, bgp_route_map_update_timer,
3800 NULL, bm->rmap_update_timer,
3801 &bm->t_rmap_update);
3802
3803 /* Signal the groups that a route-map update event has
3804 * started */
3805 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3806 update_group_policy_update(bgp,
3807 BGP_POLICY_ROUTE_MAP,
3808 rmap_name, 1, 1);
3809 } else {
3810 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3811 bgp_route_map_process_update(bgp, rmap_name, 0);
3812 #ifdef ENABLE_BGP_VNC
3813 zlog_debug("%s: calling vnc_routemap_update", __func__);
3814 vnc_routemap_update(bgp, __func__);
3815 #endif
3816 }
3817 }
3818
3819 static void bgp_route_map_add(const char *rmap_name)
3820 {
3821 if (route_map_mark_updated(rmap_name) == 0)
3822 bgp_route_map_mark_update(rmap_name);
3823
3824 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
3825 }
3826
3827 static void bgp_route_map_delete(const char *rmap_name)
3828 {
3829 if (route_map_mark_updated(rmap_name) == 0)
3830 bgp_route_map_mark_update(rmap_name);
3831
3832 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_DELETED);
3833 }
3834
3835 static void bgp_route_map_event(const char *rmap_name)
3836 {
3837 if (route_map_mark_updated(rmap_name) == 0)
3838 bgp_route_map_mark_update(rmap_name);
3839
3840 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
3841 }
3842
3843 DEFUN_YANG (match_mac_address,
3844 match_mac_address_cmd,
3845 "match mac address WORD",
3846 MATCH_STR
3847 "mac address\n"
3848 "Match address of route\n"
3849 "MAC Access-list name\n")
3850 {
3851 const char *xpath =
3852 "./match-condition[condition='frr-bgp-route-map:mac-address-list']";
3853 char xpath_value[XPATH_MAXLEN];
3854
3855 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
3856 snprintf(xpath_value, sizeof(xpath_value),
3857 "%s/rmap-match-condition/frr-bgp-route-map:list-name", xpath);
3858 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[3]->arg);
3859
3860 return nb_cli_apply_changes(vty, NULL);
3861 }
3862
3863 DEFUN_YANG (no_match_mac_address,
3864 no_match_mac_address_cmd,
3865 "no match mac address WORD",
3866 NO_STR
3867 MATCH_STR
3868 "mac\n"
3869 "Match address of route\n"
3870 "MAC acess-list name\n")
3871 {
3872 const char *xpath =
3873 "./match-condition[condition='frr-bgp-route-map:mac-address-list']";
3874
3875 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
3876 return nb_cli_apply_changes(vty, NULL);
3877 }
3878
3879 /*
3880 * Helper to handle the case of the user passing in a number or type string
3881 */
3882 static const char *parse_evpn_rt_type(const char *num_rt_type)
3883 {
3884 switch (num_rt_type[0]) {
3885 case '1':
3886 return "ead";
3887 case '2':
3888 return "macip";
3889 case '3':
3890 return "multicast";
3891 case '4':
3892 return "es";
3893 case '5':
3894 return "prefix";
3895 default:
3896 break;
3897 }
3898
3899 /* Was already full type string */
3900 return num_rt_type;
3901 }
3902
3903 DEFUN_YANG (match_evpn_route_type,
3904 match_evpn_route_type_cmd,
3905 "match evpn route-type <macip|2|multicast|3|prefix|5>",
3906 MATCH_STR
3907 EVPN_HELP_STR
3908 EVPN_TYPE_HELP_STR
3909 EVPN_TYPE_2_HELP_STR
3910 EVPN_TYPE_2_HELP_STR
3911 EVPN_TYPE_3_HELP_STR
3912 EVPN_TYPE_3_HELP_STR
3913 EVPN_TYPE_5_HELP_STR
3914 EVPN_TYPE_5_HELP_STR)
3915 {
3916 const char *xpath =
3917 "./match-condition[condition='frr-bgp-route-map:evpn-route-type']";
3918 char xpath_value[XPATH_MAXLEN];
3919
3920 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
3921 snprintf(xpath_value, sizeof(xpath_value),
3922 "%s/rmap-match-condition/frr-bgp-route-map:evpn-route-type",
3923 xpath);
3924 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
3925 parse_evpn_rt_type(argv[3]->arg));
3926
3927 return nb_cli_apply_changes(vty, NULL);
3928 }
3929
3930 DEFUN_YANG (no_match_evpn_route_type,
3931 no_match_evpn_route_type_cmd,
3932 "no match evpn route-type <macip|2|multicast|3|prefix|5>",
3933 NO_STR
3934 MATCH_STR
3935 EVPN_HELP_STR
3936 EVPN_TYPE_HELP_STR
3937 EVPN_TYPE_2_HELP_STR
3938 EVPN_TYPE_2_HELP_STR
3939 EVPN_TYPE_3_HELP_STR
3940 EVPN_TYPE_3_HELP_STR
3941 EVPN_TYPE_5_HELP_STR
3942 EVPN_TYPE_5_HELP_STR)
3943 {
3944 const char *xpath =
3945 "./match-condition[condition='frr-bgp-route-map:evpn-route-type']";
3946
3947 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
3948
3949 return nb_cli_apply_changes(vty, NULL);
3950 }
3951
3952
3953 DEFUN_YANG (match_evpn_vni,
3954 match_evpn_vni_cmd,
3955 "match evpn vni " CMD_VNI_RANGE,
3956 MATCH_STR
3957 EVPN_HELP_STR
3958 "Match VNI\n"
3959 "VNI ID\n")
3960 {
3961 const char *xpath =
3962 "./match-condition[condition='frr-bgp-route-map:evpn-vni']";
3963 char xpath_value[XPATH_MAXLEN];
3964
3965 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
3966 snprintf(xpath_value, sizeof(xpath_value),
3967 "%s/rmap-match-condition/frr-bgp-route-map:evpn-vni", xpath);
3968 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[3]->arg);
3969
3970 return nb_cli_apply_changes(vty, NULL);
3971 }
3972
3973 DEFUN_YANG (no_match_evpn_vni,
3974 no_match_evpn_vni_cmd,
3975 "no match evpn vni " CMD_VNI_RANGE,
3976 NO_STR
3977 MATCH_STR
3978 EVPN_HELP_STR
3979 "Match VNI\n"
3980 "VNI ID\n")
3981 {
3982 const char *xpath =
3983 "./match-condition[condition='frr-bgp-route-map:evpn-vni']";
3984 char xpath_value[XPATH_MAXLEN];
3985
3986 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
3987 snprintf(xpath_value, sizeof(xpath_value),
3988 "%s/rmap-match-condition/frr-bgp-route-map:evpn-vni", xpath);
3989 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY, argv[3]->arg);
3990
3991 return nb_cli_apply_changes(vty, NULL);
3992 }
3993
3994 DEFUN_YANG (match_evpn_default_route,
3995 match_evpn_default_route_cmd,
3996 "match evpn default-route",
3997 MATCH_STR
3998 EVPN_HELP_STR
3999 "default EVPN type-5 route\n")
4000 {
4001 const char *xpath =
4002 "./match-condition[condition='frr-bgp-route-map:evpn-default-route']";
4003 char xpath_value[XPATH_MAXLEN];
4004
4005 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4006
4007 snprintf(xpath_value, sizeof(xpath_value),
4008 "%s/rmap-match-condition/frr-bgp-route-map:evpn-default-route",
4009 xpath);
4010 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, NULL);
4011
4012 return nb_cli_apply_changes(vty, NULL);
4013 }
4014
4015 DEFUN_YANG (no_match_evpn_default_route,
4016 no_match_evpn_default_route_cmd,
4017 "no match evpn default-route",
4018 NO_STR
4019 MATCH_STR
4020 EVPN_HELP_STR
4021 "default EVPN type-5 route\n")
4022 {
4023 const char *xpath =
4024 "./match-condition[condition='frr-bgp-route-map:evpn-default-route']";
4025
4026 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4027
4028 return nb_cli_apply_changes(vty, NULL);
4029 }
4030
4031 DEFUN_YANG (match_evpn_rd,
4032 match_evpn_rd_cmd,
4033 "match evpn rd ASN:NN_OR_IP-ADDRESS:NN",
4034 MATCH_STR
4035 EVPN_HELP_STR
4036 "Route Distinguisher\n"
4037 "ASN:XX or A.B.C.D:XX\n")
4038 {
4039 const char *xpath =
4040 "./match-condition[condition='frr-bgp-route-map:evpn-rd']";
4041 char xpath_value[XPATH_MAXLEN];
4042
4043 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4044 snprintf(
4045 xpath_value, sizeof(xpath_value),
4046 "%s/rmap-match-condition/frr-bgp-route-map:route-distinguisher",
4047 xpath);
4048 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[3]->arg);
4049
4050 return nb_cli_apply_changes(vty, NULL);
4051 }
4052
4053 DEFUN_YANG (no_match_evpn_rd,
4054 no_match_evpn_rd_cmd,
4055 "no match evpn rd ASN:NN_OR_IP-ADDRESS:NN",
4056 NO_STR
4057 MATCH_STR
4058 EVPN_HELP_STR
4059 "Route Distinguisher\n"
4060 "ASN:XX or A.B.C.D:XX\n")
4061 {
4062 const char *xpath =
4063 "./match-condition[condition='frr-bgp-route-map:evpn-rd']";
4064
4065 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4066 return nb_cli_apply_changes(vty, NULL);
4067 }
4068
4069 DEFPY_YANG(match_vrl_source_vrf,
4070 match_vrl_source_vrf_cmd,
4071 "match source-vrf NAME$vrf_name",
4072 MATCH_STR
4073 "source vrf\n"
4074 "The VRF name\n")
4075 {
4076 const char *xpath =
4077 "./match-condition[condition='frr-bgp-route-map:source-vrf']";
4078 char xpath_value[XPATH_MAXLEN];
4079
4080 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4081 snprintf(xpath_value, sizeof(xpath_value),
4082 "%s/rmap-match-condition/frr-bgp-route-map:source-vrf", xpath);
4083 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, vrf_name);
4084
4085 return nb_cli_apply_changes(vty, NULL);
4086 }
4087
4088 DEFPY_YANG(no_match_vrl_source_vrf,
4089 no_match_vrl_source_vrf_cmd,
4090 "no match source-vrf NAME$vrf_name",
4091 NO_STR MATCH_STR
4092 "source vrf\n"
4093 "The VRF name\n")
4094 {
4095 const char *xpath =
4096 "./match-condition[condition='frr-bgp-route-map:source-vrf']";
4097
4098 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4099 return nb_cli_apply_changes(vty, NULL);
4100 }
4101
4102 DEFPY_YANG (match_peer,
4103 match_peer_cmd,
4104 "match peer <A.B.C.D$addrv4|X:X::X:X$addrv6|WORD$intf>",
4105 MATCH_STR
4106 "Match peer address\n"
4107 "IP address of peer\n"
4108 "IPv6 address of peer\n"
4109 "Interface name of peer\n")
4110 {
4111 const char *xpath =
4112 "./match-condition[condition='frr-bgp-route-map:peer']";
4113 char xpath_value[XPATH_MAXLEN];
4114
4115 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4116
4117 if (addrv4_str) {
4118 snprintf(
4119 xpath_value, sizeof(xpath_value),
4120 "%s/rmap-match-condition/frr-bgp-route-map:peer-ipv4-address",
4121 xpath);
4122 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4123 addrv4_str);
4124 } else if (addrv6_str) {
4125 snprintf(
4126 xpath_value, sizeof(xpath_value),
4127 "%s/rmap-match-condition/frr-bgp-route-map:peer-ipv6-address",
4128 xpath);
4129 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4130 addrv6_str);
4131 } else {
4132 snprintf(
4133 xpath_value, sizeof(xpath_value),
4134 "%s/rmap-match-condition/frr-bgp-route-map:peer-interface",
4135 xpath);
4136 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, intf);
4137 }
4138
4139 return nb_cli_apply_changes(vty, NULL);
4140 }
4141
4142 DEFUN_YANG (match_peer_local,
4143 match_peer_local_cmd,
4144 "match peer local",
4145 MATCH_STR
4146 "Match peer address\n"
4147 "Static or Redistributed routes\n")
4148 {
4149 const char *xpath =
4150 "./match-condition[condition='frr-bgp-route-map:peer']";
4151 char xpath_value[XPATH_MAXLEN];
4152
4153 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4154
4155 snprintf(xpath_value, sizeof(xpath_value),
4156 "%s/rmap-match-condition/frr-bgp-route-map:peer-local", xpath);
4157 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
4158
4159 return nb_cli_apply_changes(vty, NULL);
4160 }
4161
4162 DEFUN_YANG (no_match_peer,
4163 no_match_peer_cmd,
4164 "no match peer [<local|A.B.C.D|X:X::X:X|WORD>]",
4165 NO_STR
4166 MATCH_STR
4167 "Match peer address\n"
4168 "Static or Redistributed routes\n"
4169 "IP address of peer\n"
4170 "IPv6 address of peer\n"
4171 "Interface name of peer\n")
4172 {
4173 const char *xpath =
4174 "./match-condition[condition='frr-bgp-route-map:peer']";
4175
4176 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4177
4178 return nb_cli_apply_changes(vty, NULL);
4179 }
4180
4181 #ifdef HAVE_SCRIPTING
4182 DEFUN_YANG (match_script,
4183 match_script_cmd,
4184 "[no] match script WORD",
4185 NO_STR
4186 MATCH_STR
4187 "Execute script to determine match\n"
4188 "The script name to run, without .lua; e.g. 'myroutemap' to run myroutemap.lua\n")
4189 {
4190 bool no = strmatch(argv[0]->text, "no");
4191 int i = 0;
4192 argv_find(argv, argc, "WORD", &i);
4193 const char *script = argv[i]->arg;
4194 const char *xpath =
4195 "./match-condition[condition='frr-bgp-route-map:match-script']";
4196 char xpath_value[XPATH_MAXLEN];
4197
4198 if (no) {
4199 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4200 snprintf(xpath_value, sizeof(xpath_value),
4201 "%s/rmap-match-condition/frr-bgp-route-map:script",
4202 xpath);
4203 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY,
4204 script);
4205
4206 return nb_cli_apply_changes(vty, NULL);
4207 }
4208
4209 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4210 snprintf(xpath_value, sizeof(xpath_value),
4211 "%s/rmap-match-condition/frr-bgp-route-map:script",
4212 xpath);
4213 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4214 script);
4215
4216 return nb_cli_apply_changes(vty, NULL);
4217 }
4218 #endif /* HAVE_SCRIPTING */
4219
4220 /* match probability */
4221 DEFUN_YANG (match_probability,
4222 match_probability_cmd,
4223 "match probability (0-100)",
4224 MATCH_STR
4225 "Match portion of routes defined by percentage value\n"
4226 "Percentage of routes\n")
4227 {
4228 int idx_number = 2;
4229
4230 const char *xpath =
4231 "./match-condition[condition='frr-bgp-route-map:probability']";
4232 char xpath_value[XPATH_MAXLEN];
4233
4234 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4235 snprintf(xpath_value, sizeof(xpath_value),
4236 "%s/rmap-match-condition/frr-bgp-route-map:probability",
4237 xpath);
4238 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4239 argv[idx_number]->arg);
4240
4241 return nb_cli_apply_changes(vty, NULL);
4242 }
4243
4244
4245 DEFUN_YANG (no_match_probability,
4246 no_match_probability_cmd,
4247 "no match probability [(1-99)]",
4248 NO_STR
4249 MATCH_STR
4250 "Match portion of routes defined by percentage value\n"
4251 "Percentage of routes\n")
4252 {
4253 int idx_number = 3;
4254 const char *xpath =
4255 "./match-condition[condition='frr-bgp-route-map:probability']";
4256 char xpath_value[XPATH_MAXLEN];
4257
4258 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4259
4260 if (argc <= idx_number)
4261 return nb_cli_apply_changes(vty, NULL);
4262
4263 snprintf(xpath_value, sizeof(xpath_value),
4264 "%s/rmap-match-condition/frr-bgp-route-map:probability",
4265 xpath);
4266 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY,
4267 argv[idx_number]->arg);
4268
4269 return nb_cli_apply_changes(vty, NULL);
4270 }
4271
4272
4273 DEFPY_YANG (match_ip_route_source,
4274 match_ip_route_source_cmd,
4275 "match ip route-source <(1-199)|(1300-2699)|WORD>",
4276 MATCH_STR
4277 IP_STR
4278 "Match advertising source address of route\n"
4279 "IP access-list number\n"
4280 "IP access-list number (expanded range)\n"
4281 "IP standard access-list name\n")
4282 {
4283 const char *xpath =
4284 "./match-condition[condition='frr-bgp-route-map:ip-route-source']";
4285 char xpath_value[XPATH_MAXLEN + 32];
4286 int idx_acl = 3;
4287
4288 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4289 snprintf(xpath_value, sizeof(xpath_value),
4290 "%s/rmap-match-condition/frr-bgp-route-map:list-name",
4291 xpath);
4292 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4293 argv[idx_acl]->arg);
4294
4295 return nb_cli_apply_changes(vty, NULL);
4296 }
4297
4298
4299 DEFUN_YANG (no_match_ip_route_source,
4300 no_match_ip_route_source_cmd,
4301 "no match ip route-source [<(1-199)|(1300-2699)|WORD>]",
4302 NO_STR
4303 MATCH_STR
4304 IP_STR
4305 "Match advertising source address of route\n"
4306 "IP access-list number\n"
4307 "IP access-list number (expanded range)\n"
4308 "IP standard access-list name\n")
4309 {
4310 const char *xpath =
4311 "./match-condition[condition='frr-bgp-route-map:ip-route-source']";
4312
4313 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4314 return nb_cli_apply_changes(vty, NULL);
4315 }
4316
4317 DEFUN_YANG (match_ip_route_source_prefix_list,
4318 match_ip_route_source_prefix_list_cmd,
4319 "match ip route-source prefix-list WORD",
4320 MATCH_STR
4321 IP_STR
4322 "Match advertising source address of route\n"
4323 "Match entries of prefix-lists\n"
4324 "IP prefix-list name\n")
4325 {
4326 int idx_word = 4;
4327 const char *xpath =
4328 "./match-condition[condition='frr-bgp-route-map:ip-route-source-prefix-list']";
4329 char xpath_value[XPATH_MAXLEN + 32];
4330
4331 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4332 snprintf(xpath_value, sizeof(xpath_value),
4333 "%s/rmap-match-condition/frr-bgp-route-map:list-name", xpath);
4334 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4335 argv[idx_word]->arg);
4336
4337 return nb_cli_apply_changes(vty, NULL);
4338 }
4339
4340
4341 DEFUN_YANG (no_match_ip_route_source_prefix_list,
4342 no_match_ip_route_source_prefix_list_cmd,
4343 "no match ip route-source prefix-list [WORD]",
4344 NO_STR
4345 MATCH_STR
4346 IP_STR
4347 "Match advertising source address of route\n"
4348 "Match entries of prefix-lists\n"
4349 "IP prefix-list name\n")
4350 {
4351 const char *xpath =
4352 "./match-condition[condition='frr-bgp-route-map:ip-route-source-prefix-list']";
4353
4354 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4355 return nb_cli_apply_changes(vty, NULL);
4356 }
4357
4358 DEFUN_YANG (match_local_pref,
4359 match_local_pref_cmd,
4360 "match local-preference (0-4294967295)",
4361 MATCH_STR
4362 "Match local-preference of route\n"
4363 "Metric value\n")
4364 {
4365 int idx_number = 2;
4366
4367 const char *xpath =
4368 "./match-condition[condition='frr-bgp-route-map:match-local-preference']";
4369 char xpath_value[XPATH_MAXLEN];
4370
4371 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4372 snprintf(xpath_value, sizeof(xpath_value),
4373 "%s/rmap-match-condition/frr-bgp-route-map:local-preference",
4374 xpath);
4375 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4376 argv[idx_number]->arg);
4377
4378 return nb_cli_apply_changes(vty, NULL);
4379 }
4380
4381
4382 DEFUN_YANG (no_match_local_pref,
4383 no_match_local_pref_cmd,
4384 "no match local-preference [(0-4294967295)]",
4385 NO_STR
4386 MATCH_STR
4387 "Match local preference of route\n"
4388 "Local preference value\n")
4389 {
4390 int idx_localpref = 3;
4391 const char *xpath =
4392 "./match-condition[condition='frr-bgp-route-map:match-local-preference']";
4393 char xpath_value[XPATH_MAXLEN];
4394
4395 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4396
4397 if (argc <= idx_localpref)
4398 return nb_cli_apply_changes(vty, NULL);
4399
4400 snprintf(xpath_value, sizeof(xpath_value),
4401 "%s/rmap-match-condition/frr-bgp-route-map:local-preference",
4402 xpath);
4403 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY,
4404 argv[idx_localpref]->arg);
4405
4406 return nb_cli_apply_changes(vty, NULL);
4407 }
4408
4409
4410 DEFPY_YANG (match_community,
4411 match_community_cmd,
4412 "match community <(1-99)|(100-500)|WORD> [exact-match]",
4413 MATCH_STR
4414 "Match BGP community list\n"
4415 "Community-list number (standard)\n"
4416 "Community-list number (expanded)\n"
4417 "Community-list name\n"
4418 "Do exact matching of communities\n")
4419 {
4420 const char *xpath =
4421 "./match-condition[condition='frr-bgp-route-map:match-community']";
4422 char xpath_value[XPATH_MAXLEN];
4423 char xpath_match[XPATH_MAXLEN];
4424 int idx_comm_list = 2;
4425
4426 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4427
4428 snprintf(
4429 xpath_value, sizeof(xpath_value),
4430 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name",
4431 xpath);
4432 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[idx_comm_list]->arg);
4433
4434 if (argc == 4) {
4435 snprintf(
4436 xpath_match, sizeof(xpath_match),
4437 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
4438 xpath);
4439 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
4440 "true");
4441 } else {
4442 snprintf(
4443 xpath_match, sizeof(xpath_match),
4444 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
4445 xpath);
4446 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
4447 "false");
4448 }
4449
4450 return nb_cli_apply_changes(vty, NULL);
4451 }
4452
4453 DEFUN_YANG (no_match_community,
4454 no_match_community_cmd,
4455 "no match community [<(1-99)|(100-500)|WORD> [exact-match]]",
4456 NO_STR
4457 MATCH_STR
4458 "Match BGP community list\n"
4459 "Community-list number (standard)\n"
4460 "Community-list number (expanded)\n"
4461 "Community-list name\n"
4462 "Do exact matching of communities\n")
4463 {
4464 const char *xpath =
4465 "./match-condition[condition='frr-bgp-route-map:match-community']";
4466
4467 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4468 return nb_cli_apply_changes(vty, NULL);
4469 }
4470
4471 DEFPY_YANG (match_lcommunity,
4472 match_lcommunity_cmd,
4473 "match large-community <(1-99)|(100-500)|WORD> [exact-match]",
4474 MATCH_STR
4475 "Match BGP large community list\n"
4476 "Large Community-list number (standard)\n"
4477 "Large Community-list number (expanded)\n"
4478 "Large Community-list name\n"
4479 "Do exact matching of communities\n")
4480 {
4481 const char *xpath =
4482 "./match-condition[condition='frr-bgp-route-map:match-large-community']";
4483 char xpath_value[XPATH_MAXLEN];
4484 char xpath_match[XPATH_MAXLEN];
4485 int idx_lcomm_list = 2;
4486
4487 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4488
4489 snprintf(
4490 xpath_value, sizeof(xpath_value),
4491 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name",
4492 xpath);
4493 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[idx_lcomm_list]->arg);
4494
4495 if (argc == 4) {
4496 snprintf(
4497 xpath_match, sizeof(xpath_match),
4498 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
4499 xpath);
4500 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
4501 "true");
4502 } else {
4503 snprintf(
4504 xpath_match, sizeof(xpath_match),
4505 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
4506 xpath);
4507 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
4508 "false");
4509 }
4510
4511 return nb_cli_apply_changes(vty, NULL);
4512 }
4513
4514 DEFUN_YANG (no_match_lcommunity,
4515 no_match_lcommunity_cmd,
4516 "no match large-community [<(1-99)|(100-500)|WORD> [exact-match]]",
4517 NO_STR
4518 MATCH_STR
4519 "Match BGP large community list\n"
4520 "Large Community-list number (standard)\n"
4521 "Large Community-list number (expanded)\n"
4522 "Large Community-list name\n"
4523 "Do exact matching of communities\n")
4524 {
4525 const char *xpath =
4526 "./match-condition[condition='frr-bgp-route-map:match-large-community']";
4527
4528 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4529 return nb_cli_apply_changes(vty, NULL);
4530 }
4531
4532 DEFPY_YANG (match_ecommunity,
4533 match_ecommunity_cmd,
4534 "match extcommunity <(1-99)|(100-500)|WORD>",
4535 MATCH_STR
4536 "Match BGP/VPN extended community list\n"
4537 "Extended community-list number (standard)\n"
4538 "Extended community-list number (expanded)\n"
4539 "Extended community-list name\n")
4540 {
4541 const char *xpath =
4542 "./match-condition[condition='frr-bgp-route-map:match-extcommunity']";
4543 char xpath_value[XPATH_MAXLEN];
4544 int idx_comm_list = 2;
4545
4546 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4547
4548 snprintf(
4549 xpath_value, sizeof(xpath_value),
4550 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name",
4551 xpath);
4552 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[idx_comm_list]->arg);
4553
4554 return nb_cli_apply_changes(vty, NULL);
4555 }
4556
4557
4558 DEFUN_YANG (no_match_ecommunity,
4559 no_match_ecommunity_cmd,
4560 "no match extcommunity [<(1-99)|(100-500)|WORD>]",
4561 NO_STR
4562 MATCH_STR
4563 "Match BGP/VPN extended community list\n"
4564 "Extended community-list number (standard)\n"
4565 "Extended community-list number (expanded)\n"
4566 "Extended community-list name\n")
4567 {
4568 const char *xpath =
4569 "./match-condition[condition='frr-bgp-route-map:match-extcommunity']";
4570
4571 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4572 return nb_cli_apply_changes(vty, NULL);
4573 }
4574
4575
4576 DEFUN_YANG (match_aspath,
4577 match_aspath_cmd,
4578 "match as-path WORD",
4579 MATCH_STR
4580 "Match BGP AS path list\n"
4581 "AS path access-list name\n")
4582 {
4583 int idx_word = 2;
4584
4585 const char *xpath =
4586 "./match-condition[condition='frr-bgp-route-map:as-path-list']";
4587 char xpath_value[XPATH_MAXLEN];
4588
4589 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4590 snprintf(xpath_value, sizeof(xpath_value),
4591 "%s/rmap-match-condition/frr-bgp-route-map:list-name", xpath);
4592 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4593 argv[idx_word]->arg);
4594
4595 return nb_cli_apply_changes(vty, NULL);
4596 }
4597
4598
4599 DEFUN_YANG (no_match_aspath,
4600 no_match_aspath_cmd,
4601 "no match as-path [WORD]",
4602 NO_STR
4603 MATCH_STR
4604 "Match BGP AS path list\n"
4605 "AS path access-list name\n")
4606 {
4607 const char *xpath =
4608 "./match-condition[condition='frr-bgp-route-map:as-path-list']";
4609
4610 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4611
4612 return nb_cli_apply_changes(vty, NULL);
4613 }
4614
4615 DEFUN_YANG (match_origin,
4616 match_origin_cmd,
4617 "match origin <egp|igp|incomplete>",
4618 MATCH_STR
4619 "BGP origin code\n"
4620 "remote EGP\n"
4621 "local IGP\n"
4622 "unknown heritage\n")
4623 {
4624 int idx_origin = 2;
4625 const char *origin_type;
4626 const char *xpath =
4627 "./match-condition[condition='frr-bgp-route-map:match-origin']";
4628 char xpath_value[XPATH_MAXLEN];
4629
4630 if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
4631 origin_type = "igp";
4632 else if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
4633 origin_type = "egp";
4634 else if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
4635 origin_type = "incomplete";
4636 else {
4637 vty_out(vty, "%% Invalid match origin type\n");
4638 return CMD_WARNING_CONFIG_FAILED;
4639 }
4640
4641 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4642 snprintf(xpath_value, sizeof(xpath_value),
4643 "%s/rmap-match-condition/frr-bgp-route-map:origin", xpath);
4644 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, origin_type);
4645
4646 return nb_cli_apply_changes(vty, NULL);
4647 }
4648
4649
4650 DEFUN_YANG (no_match_origin,
4651 no_match_origin_cmd,
4652 "no match origin [<egp|igp|incomplete>]",
4653 NO_STR
4654 MATCH_STR
4655 "BGP origin code\n"
4656 "remote EGP\n"
4657 "local IGP\n"
4658 "unknown heritage\n")
4659 {
4660 const char *xpath =
4661 "./match-condition[condition='frr-bgp-route-map:match-origin']";
4662 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4663 return nb_cli_apply_changes(vty, NULL);
4664 }
4665
4666 DEFUN_YANG (set_table_id,
4667 set_table_id_cmd,
4668 "set table (1-4294967295)",
4669 SET_STR
4670 "export route to non-main kernel table\n"
4671 "Kernel routing table id\n")
4672 {
4673 int idx_number = 2;
4674 const char *xpath = "./set-action[action='frr-bgp-route-map:table']";
4675 char xpath_value[XPATH_MAXLEN];
4676
4677 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4678 snprintf(xpath_value, sizeof(xpath_value),
4679 "%s/rmap-set-action/frr-bgp-route-map:table", xpath);
4680 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4681 argv[idx_number]->arg);
4682 return nb_cli_apply_changes(vty, NULL);
4683 }
4684
4685 DEFUN_YANG (no_set_table_id,
4686 no_set_table_id_cmd,
4687 "no set table",
4688 NO_STR
4689 SET_STR
4690 "export route to non-main kernel table\n")
4691 {
4692 const char *xpath = "./set-action[action='frr-bgp-route-map:table']";
4693 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4694 return nb_cli_apply_changes(vty, NULL);
4695 }
4696
4697 DEFUN_YANG (set_ip_nexthop_peer,
4698 set_ip_nexthop_peer_cmd,
4699 "[no] set ip next-hop peer-address",
4700 NO_STR
4701 SET_STR
4702 IP_STR
4703 "Next hop address\n"
4704 "Use peer address (for BGP only)\n")
4705 {
4706 char xpath_value[XPATH_MAXLEN];
4707 const char *xpath =
4708 "./set-action[action='frr-bgp-route-map:set-ipv4-nexthop']";
4709
4710 if (strmatch(argv[0]->text, "no"))
4711 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4712 else {
4713 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4714 snprintf(xpath_value, sizeof(xpath_value),
4715 "%s/rmap-set-action/frr-bgp-route-map:ipv4-nexthop",
4716 xpath);
4717 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4718 "peer-address");
4719 }
4720 return nb_cli_apply_changes(vty, NULL);
4721 }
4722
4723 DEFUN_YANG (set_ip_nexthop_unchanged,
4724 set_ip_nexthop_unchanged_cmd,
4725 "[no] set ip next-hop unchanged",
4726 NO_STR
4727 SET_STR
4728 IP_STR
4729 "Next hop address\n"
4730 "Don't modify existing Next hop address\n")
4731 {
4732 char xpath_value[XPATH_MAXLEN];
4733 const char *xpath =
4734 "./set-action[action='frr-bgp-route-map:set-ipv4-nexthop']";
4735
4736 if (strmatch(argv[0]->text, "no"))
4737 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4738 else {
4739 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4740 snprintf(xpath_value, sizeof(xpath_value),
4741 "%s/rmap-set-action/frr-bgp-route-map:ipv4-nexthop",
4742 xpath);
4743 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4744 "unchanged");
4745 }
4746 return nb_cli_apply_changes(vty, NULL);
4747 }
4748
4749 DEFUN_YANG (set_distance,
4750 set_distance_cmd,
4751 "set distance (0-255)",
4752 SET_STR
4753 "BGP Administrative Distance to use\n"
4754 "Distance value\n")
4755 {
4756 int idx_number = 2;
4757 const char *xpath = "./set-action[action='frr-bgp-route-map:distance']";
4758 char xpath_value[XPATH_MAXLEN];
4759
4760 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4761 snprintf(xpath_value, sizeof(xpath_value),
4762 "%s/rmap-set-action/frr-bgp-route-map:distance", xpath);
4763 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4764 argv[idx_number]->arg);
4765 return nb_cli_apply_changes(vty, NULL);
4766 }
4767
4768 DEFUN_YANG (no_set_distance,
4769 no_set_distance_cmd,
4770 "no set distance [(0-255)]",
4771 NO_STR SET_STR
4772 "BGP Administrative Distance to use\n"
4773 "Distance value\n")
4774 {
4775 const char *xpath = "./set-action[action='frr-bgp-route-map:distance']";
4776
4777 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4778 return nb_cli_apply_changes(vty, NULL);
4779 }
4780
4781 DEFUN_YANG (set_local_pref,
4782 set_local_pref_cmd,
4783 "set local-preference WORD",
4784 SET_STR
4785 "BGP local preference path attribute\n"
4786 "Preference value (0-4294967295)\n")
4787 {
4788 int idx_number = 2;
4789 const char *xpath =
4790 "./set-action[action='frr-bgp-route-map:set-local-preference']";
4791 char xpath_value[XPATH_MAXLEN];
4792
4793 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4794 snprintf(xpath_value, sizeof(xpath_value),
4795 "%s/rmap-set-action/frr-bgp-route-map:local-pref", xpath);
4796 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4797 argv[idx_number]->arg);
4798 return nb_cli_apply_changes(vty, NULL);
4799 }
4800
4801 DEFUN_YANG (no_set_local_pref,
4802 no_set_local_pref_cmd,
4803 "no set local-preference [WORD]",
4804 NO_STR
4805 SET_STR
4806 "BGP local preference path attribute\n"
4807 "Preference value (0-4294967295)\n")
4808 {
4809 const char *xpath =
4810 "./set-action[action='frr-bgp-route-map:set-local-preference']";
4811
4812 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4813 return nb_cli_apply_changes(vty, NULL);
4814 }
4815
4816 DEFUN_YANG (set_weight,
4817 set_weight_cmd,
4818 "set weight (0-4294967295)",
4819 SET_STR
4820 "BGP weight for routing table\n"
4821 "Weight value\n")
4822 {
4823 int idx_number = 2;
4824 const char *xpath = "./set-action[action='frr-bgp-route-map:weight']";
4825 char xpath_value[XPATH_MAXLEN];
4826
4827 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4828 snprintf(xpath_value, sizeof(xpath_value),
4829 "%s/rmap-set-action/frr-bgp-route-map:weight", xpath);
4830 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4831 argv[idx_number]->arg);
4832 return nb_cli_apply_changes(vty, NULL);
4833 }
4834
4835 DEFUN_YANG (no_set_weight,
4836 no_set_weight_cmd,
4837 "no set weight [(0-4294967295)]",
4838 NO_STR
4839 SET_STR
4840 "BGP weight for routing table\n"
4841 "Weight value\n")
4842 {
4843 const char *xpath = "./set-action[action='frr-bgp-route-map:weight']";
4844
4845 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4846 return nb_cli_apply_changes(vty, NULL);
4847 }
4848
4849 DEFUN_YANG (set_label_index,
4850 set_label_index_cmd,
4851 "set label-index (0-1048560)",
4852 SET_STR
4853 "Label index to associate with the prefix\n"
4854 "Label index value\n")
4855 {
4856 int idx_number = 2;
4857 const char *xpath =
4858 "./set-action[action='frr-bgp-route-map:label-index']";
4859 char xpath_value[XPATH_MAXLEN];
4860
4861 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4862 snprintf(xpath_value, sizeof(xpath_value),
4863 "%s/rmap-set-action/frr-bgp-route-map:label-index", xpath);
4864 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4865 argv[idx_number]->arg);
4866 return nb_cli_apply_changes(vty, NULL);
4867 }
4868
4869 DEFUN_YANG (no_set_label_index,
4870 no_set_label_index_cmd,
4871 "no set label-index [(0-1048560)]",
4872 NO_STR
4873 SET_STR
4874 "Label index to associate with the prefix\n"
4875 "Label index value\n")
4876 {
4877 const char *xpath =
4878 "./set-action[action='frr-bgp-route-map:label-index']";
4879
4880 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4881 return nb_cli_apply_changes(vty, NULL);
4882 }
4883
4884 DEFUN_YANG (set_aspath_prepend_asn,
4885 set_aspath_prepend_asn_cmd,
4886 "set as-path prepend (1-4294967295)...",
4887 SET_STR
4888 "Transform BGP AS_PATH attribute\n"
4889 "Prepend to the as-path\n"
4890 "AS number\n")
4891 {
4892 int idx_asn = 3;
4893 int ret;
4894 char *str;
4895
4896 str = argv_concat(argv, argc, idx_asn);
4897
4898 const char *xpath =
4899 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
4900 char xpath_value[XPATH_MAXLEN];
4901
4902 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4903 snprintf(xpath_value, sizeof(xpath_value),
4904 "%s/rmap-set-action/frr-bgp-route-map:prepend-as-path", xpath);
4905 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
4906 ret = nb_cli_apply_changes(vty, NULL);
4907 XFREE(MTYPE_TMP, str);
4908 return ret;
4909 }
4910
4911 DEFUN_YANG (set_aspath_prepend_lastas,
4912 set_aspath_prepend_lastas_cmd,
4913 "set as-path prepend last-as (1-10)",
4914 SET_STR
4915 "Transform BGP AS_PATH attribute\n"
4916 "Prepend to the as-path\n"
4917 "Use the peer's AS-number\n"
4918 "Number of times to insert\n")
4919 {
4920 int idx_num = 4;
4921
4922 const char *xpath =
4923 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
4924 char xpath_value[XPATH_MAXLEN];
4925
4926 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4927 snprintf(xpath_value, sizeof(xpath_value),
4928 "%s/rmap-set-action/frr-bgp-route-map:last-as", xpath);
4929 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4930 argv[idx_num]->arg);
4931 return nb_cli_apply_changes(vty, NULL);
4932 }
4933
4934 DEFUN_YANG (no_set_aspath_prepend,
4935 no_set_aspath_prepend_cmd,
4936 "no set as-path prepend [(1-4294967295)]",
4937 NO_STR
4938 SET_STR
4939 "Transform BGP AS_PATH attribute\n"
4940 "Prepend to the as-path\n"
4941 "AS number\n")
4942 {
4943 const char *xpath =
4944 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
4945
4946 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4947 return nb_cli_apply_changes(vty, NULL);
4948 }
4949
4950 DEFUN_YANG (no_set_aspath_prepend_lastas,
4951 no_set_aspath_prepend_lastas_cmd,
4952 "no set as-path prepend last-as [(1-10)]",
4953 NO_STR
4954 SET_STR
4955 "Transform BGP AS_PATH attribute\n"
4956 "Prepend to the as-path\n"
4957 "Use the peers AS-number\n"
4958 "Number of times to insert\n")
4959 {
4960 const char *xpath =
4961 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
4962
4963 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4964 return nb_cli_apply_changes(vty, NULL);
4965 }
4966
4967 DEFUN_YANG (set_aspath_exclude,
4968 set_aspath_exclude_cmd,
4969 "set as-path exclude (1-4294967295)...",
4970 SET_STR
4971 "Transform BGP AS-path attribute\n"
4972 "Exclude from the as-path\n"
4973 "AS number\n")
4974 {
4975 int idx_asn = 3;
4976 int ret;
4977 char *str;
4978
4979 str = argv_concat(argv, argc, idx_asn);
4980
4981 const char *xpath =
4982 "./set-action[action='frr-bgp-route-map:as-path-exclude']";
4983 char xpath_value[XPATH_MAXLEN];
4984
4985 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4986 snprintf(xpath_value, sizeof(xpath_value),
4987 "%s/rmap-set-action/frr-bgp-route-map:exclude-as-path", xpath);
4988 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
4989 ret = nb_cli_apply_changes(vty, NULL);
4990 XFREE(MTYPE_TMP, str);
4991 return ret;
4992 }
4993
4994 DEFUN_YANG (no_set_aspath_exclude,
4995 no_set_aspath_exclude_cmd,
4996 "no set as-path exclude (1-4294967295)...",
4997 NO_STR
4998 SET_STR
4999 "Transform BGP AS_PATH attribute\n"
5000 "Exclude from the as-path\n"
5001 "AS number\n")
5002 {
5003 const char *xpath =
5004 "./set-action[action='frr-bgp-route-map:as-path-exclude']";
5005
5006 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5007 return nb_cli_apply_changes(vty, NULL);
5008 }
5009
5010 ALIAS_YANG (no_set_aspath_exclude, no_set_aspath_exclude_all_cmd,
5011 "no set as-path exclude",
5012 NO_STR SET_STR
5013 "Transform BGP AS_PATH attribute\n"
5014 "Exclude from the as-path\n")
5015
5016 DEFUN_YANG (set_community,
5017 set_community_cmd,
5018 "set community AA:NN...",
5019 SET_STR
5020 "BGP community attribute\n"
5021 COMMUNITY_VAL_STR)
5022 {
5023 int idx_aa_nn = 2;
5024 int i;
5025 int first = 0;
5026 int additive = 0;
5027 struct buffer *b;
5028 struct community *com = NULL;
5029 char *str;
5030 char *argstr = NULL;
5031 int ret;
5032
5033 const char *xpath =
5034 "./set-action[action='frr-bgp-route-map:set-community']";
5035 char xpath_value[XPATH_MAXLEN];
5036
5037 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5038 snprintf(xpath_value, sizeof(xpath_value),
5039 "%s/rmap-set-action/frr-bgp-route-map:community-string",
5040 xpath);
5041
5042 b = buffer_new(1024);
5043
5044 for (i = idx_aa_nn; i < argc; i++) {
5045 if (strncmp(argv[i]->arg, "additive", strlen(argv[i]->arg))
5046 == 0) {
5047 additive = 1;
5048 continue;
5049 }
5050
5051 if (first)
5052 buffer_putc(b, ' ');
5053 else
5054 first = 1;
5055
5056 if (strncmp(argv[i]->arg, "internet", strlen(argv[i]->arg))
5057 == 0) {
5058 buffer_putstr(b, "internet");
5059 continue;
5060 }
5061 if (strncmp(argv[i]->arg, "local-AS", strlen(argv[i]->arg))
5062 == 0) {
5063 buffer_putstr(b, "local-AS");
5064 continue;
5065 }
5066 if (strncmp(argv[i]->arg, "no-a", strlen("no-a")) == 0
5067 && strncmp(argv[i]->arg, "no-advertise",
5068 strlen(argv[i]->arg))
5069 == 0) {
5070 buffer_putstr(b, "no-advertise");
5071 continue;
5072 }
5073 if (strncmp(argv[i]->arg, "no-e", strlen("no-e")) == 0
5074 && strncmp(argv[i]->arg, "no-export", strlen(argv[i]->arg))
5075 == 0) {
5076 buffer_putstr(b, "no-export");
5077 continue;
5078 }
5079 if (strncmp(argv[i]->arg, "blackhole", strlen(argv[i]->arg))
5080 == 0) {
5081 buffer_putstr(b, "blackhole");
5082 continue;
5083 }
5084 if (strncmp(argv[i]->arg, "graceful-shutdown",
5085 strlen(argv[i]->arg))
5086 == 0) {
5087 buffer_putstr(b, "graceful-shutdown");
5088 continue;
5089 }
5090 buffer_putstr(b, argv[i]->arg);
5091 }
5092 buffer_putc(b, '\0');
5093
5094 /* Fetch result string then compile it to communities attribute. */
5095 str = buffer_getstr(b);
5096 buffer_free(b);
5097
5098 if (str) {
5099 com = community_str2com(str);
5100 XFREE(MTYPE_TMP, str);
5101 }
5102
5103 /* Can't compile user input into communities attribute. */
5104 if (!com) {
5105 vty_out(vty, "%% Malformed communities attribute\n");
5106 return CMD_WARNING_CONFIG_FAILED;
5107 }
5108
5109 /* Set communites attribute string. */
5110 str = community_str(com, false);
5111
5112 if (additive) {
5113 size_t argstr_sz = strlen(str) + strlen(" additive") + 1;
5114 argstr = XCALLOC(MTYPE_TMP, argstr_sz);
5115 strlcpy(argstr, str, argstr_sz);
5116 strlcat(argstr, " additive", argstr_sz);
5117 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argstr);
5118 } else
5119 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
5120
5121 ret = nb_cli_apply_changes(vty, NULL);
5122
5123 if (argstr)
5124 XFREE(MTYPE_TMP, argstr);
5125 community_free(&com);
5126
5127 return ret;
5128 }
5129
5130 DEFUN_YANG (set_community_none,
5131 set_community_none_cmd,
5132 "set community none",
5133 SET_STR
5134 "BGP community attribute\n"
5135 "No community attribute\n")
5136 {
5137 const char *xpath =
5138 "./set-action[action='frr-bgp-route-map:set-community']";
5139 char xpath_value[XPATH_MAXLEN];
5140
5141 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5142
5143 snprintf(xpath_value, sizeof(xpath_value),
5144 "%s/rmap-set-action/frr-bgp-route-map:community-none", xpath);
5145 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
5146 return nb_cli_apply_changes(vty, NULL);
5147 }
5148
5149 DEFUN_YANG (no_set_community,
5150 no_set_community_cmd,
5151 "no set community AA:NN...",
5152 NO_STR
5153 SET_STR
5154 "BGP community attribute\n"
5155 COMMUNITY_VAL_STR)
5156 {
5157 const char *xpath =
5158 "./set-action[action='frr-bgp-route-map:set-community']";
5159
5160 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5161 return nb_cli_apply_changes(vty, NULL);
5162 }
5163
5164 ALIAS_YANG (no_set_community,
5165 no_set_community_short_cmd,
5166 "no set community",
5167 NO_STR
5168 SET_STR
5169 "BGP community attribute\n")
5170
5171 DEFPY_YANG (set_community_delete,
5172 set_community_delete_cmd,
5173 "set comm-list <(1-99)|(100-500)|WORD> delete",
5174 SET_STR
5175 "set BGP community list (for deletion)\n"
5176 "Community-list number (standard)\n"
5177 "Community-list number (expanded)\n"
5178 "Community-list name\n"
5179 "Delete matching communities\n")
5180 {
5181 const char *xpath =
5182 "./set-action[action='frr-bgp-route-map:comm-list-delete']";
5183 char xpath_value[XPATH_MAXLEN];
5184 int idx_comm_list = 2;
5185
5186 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5187
5188 snprintf(xpath_value, sizeof(xpath_value),
5189 "%s/rmap-set-action/frr-bgp-route-map:comm-list-name",
5190 xpath);
5191 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5192 argv[idx_comm_list]->arg);
5193
5194 return nb_cli_apply_changes(vty, NULL);
5195
5196 }
5197
5198 DEFUN_YANG (no_set_community_delete,
5199 no_set_community_delete_cmd,
5200 "no set comm-list [<(1-99)|(100-500)|WORD> delete]",
5201 NO_STR
5202 SET_STR
5203 "set BGP community list (for deletion)\n"
5204 "Community-list number (standard)\n"
5205 "Community-list number (expanded)\n"
5206 "Community-list name\n"
5207 "Delete matching communities\n")
5208 {
5209 const char *xpath =
5210 "./set-action[action='frr-bgp-route-map:comm-list-delete']";
5211
5212 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5213 return nb_cli_apply_changes(vty, NULL);
5214 }
5215
5216 DEFUN_YANG (set_lcommunity,
5217 set_lcommunity_cmd,
5218 "set large-community AA:BB:CC...",
5219 SET_STR
5220 "BGP large community attribute\n"
5221 "Large Community number in aa:bb:cc format or additive\n")
5222 {
5223 char *str;
5224 int ret;
5225 const char *xpath =
5226 "./set-action[action='frr-bgp-route-map:set-large-community']";
5227 char xpath_value[XPATH_MAXLEN];
5228
5229 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5230
5231 snprintf(xpath_value, sizeof(xpath_value),
5232 "%s/rmap-set-action/frr-bgp-route-map:large-community-string",
5233 xpath);
5234 str = argv_concat(argv, argc, 2);
5235 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
5236 ret = nb_cli_apply_changes(vty, NULL);
5237 XFREE(MTYPE_TMP, str);
5238 return ret;
5239 }
5240
5241 DEFUN_YANG (set_lcommunity_none,
5242 set_lcommunity_none_cmd,
5243 "set large-community none",
5244 SET_STR
5245 "BGP large community attribute\n"
5246 "No large community attribute\n")
5247 {
5248 const char *xpath =
5249 "./set-action[action='frr-bgp-route-map:set-large-community']";
5250 char xpath_value[XPATH_MAXLEN];
5251
5252 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5253
5254 snprintf(xpath_value, sizeof(xpath_value),
5255 "%s/rmap-set-action/frr-bgp-route-map:large-community-none",
5256 xpath);
5257 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
5258 return nb_cli_apply_changes(vty, NULL);
5259 }
5260
5261 DEFUN_YANG (no_set_lcommunity,
5262 no_set_lcommunity_cmd,
5263 "no set large-community none",
5264 NO_STR
5265 SET_STR
5266 "BGP large community attribute\n"
5267 "No community attribute\n")
5268 {
5269 const char *xpath =
5270 "./set-action[action='frr-bgp-route-map:set-large-community']";
5271
5272 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5273 return nb_cli_apply_changes(vty, NULL);
5274 }
5275
5276 DEFUN_YANG (no_set_lcommunity1,
5277 no_set_lcommunity1_cmd,
5278 "no set large-community AA:BB:CC...",
5279 NO_STR
5280 SET_STR
5281 "BGP large community attribute\n"
5282 "Large community in AA:BB:CC... format or additive\n")
5283 {
5284 const char *xpath =
5285 "./set-action[action='frr-bgp-route-map:set-large-community']";
5286
5287 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5288 return nb_cli_apply_changes(vty, NULL);
5289 }
5290
5291 ALIAS_YANG (no_set_lcommunity1,
5292 no_set_lcommunity1_short_cmd,
5293 "no set large-community",
5294 NO_STR
5295 SET_STR
5296 "BGP large community attribute\n")
5297
5298 DEFPY_YANG (set_lcommunity_delete,
5299 set_lcommunity_delete_cmd,
5300 "set large-comm-list <(1-99)|(100-500)|WORD> delete",
5301 SET_STR
5302 "set BGP large community list (for deletion)\n"
5303 "Large Community-list number (standard)\n"
5304 "Large Communitly-list number (expanded)\n"
5305 "Large Community-list name\n"
5306 "Delete matching large communities\n")
5307 {
5308 const char *xpath =
5309 "./set-action[action='frr-bgp-route-map:large-comm-list-delete']";
5310 char xpath_value[XPATH_MAXLEN];
5311 int idx_lcomm_list = 2;
5312
5313 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5314
5315 snprintf(xpath_value, sizeof(xpath_value),
5316 "%s/rmap-set-action/frr-bgp-route-map:comm-list-name",
5317 xpath);
5318 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5319 argv[idx_lcomm_list]->arg);
5320
5321 return nb_cli_apply_changes(vty, NULL);
5322 }
5323
5324 DEFUN_YANG (no_set_lcommunity_delete,
5325 no_set_lcommunity_delete_cmd,
5326 "no set large-comm-list <(1-99)|(100-500)|WORD> [delete]",
5327 NO_STR
5328 SET_STR
5329 "set BGP large community list (for deletion)\n"
5330 "Large Community-list number (standard)\n"
5331 "Large Communitly-list number (expanded)\n"
5332 "Large Community-list name\n"
5333 "Delete matching large communities\n")
5334 {
5335 const char *xpath =
5336 "./set-action[action='frr-bgp-route-map:large-comm-list-delete']";
5337
5338 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5339 return nb_cli_apply_changes(vty, NULL);
5340 }
5341
5342 ALIAS_YANG (no_set_lcommunity_delete,
5343 no_set_lcommunity_delete_short_cmd,
5344 "no set large-comm-list",
5345 NO_STR
5346 SET_STR
5347 "set BGP large community list (for deletion)\n")
5348
5349 DEFUN_YANG (set_ecommunity_rt,
5350 set_ecommunity_rt_cmd,
5351 "set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
5352 SET_STR
5353 "BGP extended community attribute\n"
5354 "Route Target extended community\n"
5355 "VPN extended community\n")
5356 {
5357 int idx_asn_nn = 3;
5358 char *str;
5359 int ret;
5360 const char *xpath =
5361 "./set-action[action='frr-bgp-route-map:set-extcommunity-rt']";
5362 char xpath_value[XPATH_MAXLEN];
5363
5364 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5365
5366 snprintf(xpath_value, sizeof(xpath_value),
5367 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-rt", xpath);
5368 str = argv_concat(argv, argc, idx_asn_nn);
5369 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
5370 ret = nb_cli_apply_changes(vty, NULL);
5371 XFREE(MTYPE_TMP, str);
5372 return ret;
5373 }
5374
5375 DEFUN_YANG (no_set_ecommunity_rt,
5376 no_set_ecommunity_rt_cmd,
5377 "no set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
5378 NO_STR
5379 SET_STR
5380 "BGP extended community attribute\n"
5381 "Route Target extended community\n"
5382 "VPN extended community\n")
5383 {
5384 const char *xpath =
5385 "./set-action[action='frr-bgp-route-map:set-extcommunity-rt']";
5386 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5387 return nb_cli_apply_changes(vty, NULL);
5388 }
5389
5390 ALIAS_YANG (no_set_ecommunity_rt,
5391 no_set_ecommunity_rt_short_cmd,
5392 "no set extcommunity rt",
5393 NO_STR
5394 SET_STR
5395 "BGP extended community attribute\n"
5396 "Route Target extended community\n")
5397
5398 DEFUN_YANG (set_ecommunity_soo,
5399 set_ecommunity_soo_cmd,
5400 "set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
5401 SET_STR
5402 "BGP extended community attribute\n"
5403 "Site-of-Origin extended community\n"
5404 "VPN extended community\n")
5405 {
5406 int idx_asn_nn = 3;
5407 char *str;
5408 int ret;
5409 const char *xpath =
5410 "./set-action[action='frr-bgp-route-map:set-extcommunity-soo']";
5411 char xpath_value[XPATH_MAXLEN];
5412
5413 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5414
5415 snprintf(xpath_value, sizeof(xpath_value),
5416 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-soo",
5417 xpath);
5418 str = argv_concat(argv, argc, idx_asn_nn);
5419 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
5420 ret = nb_cli_apply_changes(vty, NULL);
5421 XFREE(MTYPE_TMP, str);
5422 return ret;
5423 }
5424
5425 DEFUN_YANG (no_set_ecommunity_soo,
5426 no_set_ecommunity_soo_cmd,
5427 "no set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
5428 NO_STR
5429 SET_STR
5430 "BGP extended community attribute\n"
5431 "Site-of-Origin extended community\n"
5432 "VPN extended community\n")
5433 {
5434 const char *xpath =
5435 "./set-action[action='frr-bgp-route-map:set-extcommunity-soo']";
5436 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5437 return nb_cli_apply_changes(vty, NULL);
5438 }
5439
5440 ALIAS_YANG (no_set_ecommunity_soo,
5441 no_set_ecommunity_soo_short_cmd,
5442 "no set extcommunity soo",
5443 NO_STR
5444 SET_STR
5445 "GP extended community attribute\n"
5446 "Site-of-Origin extended community\n")
5447
5448 DEFUN_YANG (set_ecommunity_lb,
5449 set_ecommunity_lb_cmd,
5450 "set extcommunity bandwidth <(1-25600)|cumulative|num-multipaths> [non-transitive]",
5451 SET_STR
5452 "BGP extended community attribute\n"
5453 "Link bandwidth extended community\n"
5454 "Bandwidth value in Mbps\n"
5455 "Cumulative bandwidth of all multipaths (outbound-only)\n"
5456 "Internally computed bandwidth based on number of multipaths (outbound-only)\n"
5457 "Attribute is set as non-transitive\n")
5458 {
5459 int idx_lb = 3;
5460 int idx_non_transitive = 4;
5461 const char *xpath =
5462 "./set-action[action='frr-bgp-route-map:set-extcommunity-lb']";
5463 char xpath_lb_type[XPATH_MAXLEN];
5464 char xpath_bandwidth[XPATH_MAXLEN];
5465 char xpath_non_transitive[XPATH_MAXLEN];
5466
5467 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5468
5469 snprintf(xpath_lb_type, sizeof(xpath_lb_type),
5470 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-lb/lb-type",
5471 xpath);
5472 snprintf(xpath_bandwidth, sizeof(xpath_bandwidth),
5473 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-lb/bandwidth",
5474 xpath);
5475 snprintf(xpath_non_transitive, sizeof(xpath_non_transitive),
5476 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-lb/two-octet-as-specific",
5477 xpath);
5478
5479 if ((strcmp(argv[idx_lb]->arg, "cumulative")) == 0)
5480 nb_cli_enqueue_change(vty, xpath_lb_type, NB_OP_MODIFY,
5481 "cumulative-bandwidth");
5482 else if ((strcmp(argv[idx_lb]->arg, "num-multipaths")) == 0)
5483 nb_cli_enqueue_change(vty, xpath_lb_type, NB_OP_MODIFY,
5484 "computed-bandwidth");
5485 else {
5486 nb_cli_enqueue_change(vty, xpath_lb_type, NB_OP_MODIFY,
5487 "explicit-bandwidth");
5488 nb_cli_enqueue_change(vty, xpath_bandwidth, NB_OP_MODIFY,
5489 argv[idx_lb]->arg);
5490 }
5491
5492 if (argv[idx_non_transitive])
5493 nb_cli_enqueue_change(vty, xpath_non_transitive, NB_OP_MODIFY,
5494 "true");
5495 else
5496 nb_cli_enqueue_change(vty, xpath_non_transitive, NB_OP_MODIFY,
5497 "false");
5498
5499 return nb_cli_apply_changes(vty, NULL);
5500 }
5501
5502 DEFUN_YANG (no_set_ecommunity_lb,
5503 no_set_ecommunity_lb_cmd,
5504 "no set extcommunity bandwidth <(1-25600)|cumulative|num-multipaths> [non-transitive]",
5505 NO_STR
5506 SET_STR
5507 "BGP extended community attribute\n"
5508 "Link bandwidth extended community\n"
5509 "Bandwidth value in Mbps\n"
5510 "Cumulative bandwidth of all multipaths (outbound-only)\n"
5511 "Internally computed bandwidth based on number of multipaths (outbound-only)\n"
5512 "Attribute is set as non-transitive\n")
5513 {
5514 const char *xpath =
5515 "./set-action[action='frr-bgp-route-map:set-extcommunity-lb']";
5516
5517 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5518 return nb_cli_apply_changes(vty, NULL);
5519 }
5520
5521 ALIAS_YANG (no_set_ecommunity_lb,
5522 no_set_ecommunity_lb_short_cmd,
5523 "no set extcommunity bandwidth",
5524 NO_STR
5525 SET_STR
5526 "BGP extended community attribute\n"
5527 "Link bandwidth extended community\n")
5528
5529 DEFUN_YANG (set_origin,
5530 set_origin_cmd,
5531 "set origin <egp|igp|incomplete>",
5532 SET_STR
5533 "BGP origin code\n"
5534 "remote EGP\n"
5535 "local IGP\n"
5536 "unknown heritage\n")
5537 {
5538 int idx_origin = 2;
5539 const char *origin_type;
5540 const char *xpath =
5541 "./set-action[action='frr-bgp-route-map:set-origin']";
5542 char xpath_value[XPATH_MAXLEN];
5543
5544 if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
5545 origin_type = "igp";
5546 else if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
5547 origin_type = "egp";
5548 else if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
5549 origin_type = "incomplete";
5550 else {
5551 vty_out(vty, "%% Invalid match origin type\n");
5552 return CMD_WARNING_CONFIG_FAILED;
5553 }
5554
5555 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5556 snprintf(xpath_value, sizeof(xpath_value),
5557 "%s/rmap-set-action/frr-bgp-route-map:origin", xpath);
5558 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, origin_type);
5559
5560 return nb_cli_apply_changes(vty, NULL);
5561 }
5562
5563 DEFUN_YANG (no_set_origin,
5564 no_set_origin_cmd,
5565 "no set origin [<egp|igp|incomplete>]",
5566 NO_STR
5567 SET_STR
5568 "BGP origin code\n"
5569 "remote EGP\n"
5570 "local IGP\n"
5571 "unknown heritage\n")
5572 {
5573 const char *xpath =
5574 "./set-action[action='frr-bgp-route-map:set-origin']";
5575
5576 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5577 return nb_cli_apply_changes(vty, NULL);
5578 }
5579
5580 DEFUN_YANG (set_atomic_aggregate,
5581 set_atomic_aggregate_cmd,
5582 "set atomic-aggregate",
5583 SET_STR
5584 "BGP atomic aggregate attribute\n" )
5585 {
5586 const char *xpath =
5587 "./set-action[action='frr-bgp-route-map:atomic-aggregate']";
5588 char xpath_value[XPATH_MAXLEN];
5589
5590 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5591 snprintf(xpath_value, sizeof(xpath_value),
5592 "%s/rmap-set-action/frr-bgp-route-map:atomic-aggregate",
5593 xpath);
5594 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, NULL);
5595
5596 return nb_cli_apply_changes(vty, NULL);
5597 }
5598
5599 DEFUN_YANG (no_set_atomic_aggregate,
5600 no_set_atomic_aggregate_cmd,
5601 "no set atomic-aggregate",
5602 NO_STR
5603 SET_STR
5604 "BGP atomic aggregate attribute\n" )
5605 {
5606 const char *xpath =
5607 "./set-action[action='frr-bgp-route-map:atomic-aggregate']";
5608
5609 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5610 return nb_cli_apply_changes(vty, NULL);
5611 }
5612
5613 DEFUN_YANG (set_aggregator_as,
5614 set_aggregator_as_cmd,
5615 "set aggregator as (1-4294967295) A.B.C.D",
5616 SET_STR
5617 "BGP aggregator attribute\n"
5618 "AS number of aggregator\n"
5619 "AS number\n"
5620 "IP address of aggregator\n")
5621 {
5622 int idx_number = 3;
5623 int idx_ipv4 = 4;
5624 char xpath_asn[XPATH_MAXLEN];
5625 char xpath_addr[XPATH_MAXLEN];
5626 const char *xpath =
5627 "./set-action[action='frr-bgp-route-map:aggregator']";
5628
5629 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5630
5631 snprintf(
5632 xpath_asn, sizeof(xpath_asn),
5633 "%s/rmap-set-action/frr-bgp-route-map:aggregator/aggregator-asn",
5634 xpath);
5635 nb_cli_enqueue_change(vty, xpath_asn, NB_OP_MODIFY,
5636 argv[idx_number]->arg);
5637
5638 snprintf(
5639 xpath_addr, sizeof(xpath_addr),
5640 "%s/rmap-set-action/frr-bgp-route-map:aggregator/aggregator-address",
5641 xpath);
5642 nb_cli_enqueue_change(vty, xpath_addr, NB_OP_MODIFY,
5643 argv[idx_ipv4]->arg);
5644
5645 return nb_cli_apply_changes(vty, NULL);
5646 }
5647
5648 DEFUN_YANG (no_set_aggregator_as,
5649 no_set_aggregator_as_cmd,
5650 "no set aggregator as [(1-4294967295) A.B.C.D]",
5651 NO_STR
5652 SET_STR
5653 "BGP aggregator attribute\n"
5654 "AS number of aggregator\n"
5655 "AS number\n"
5656 "IP address of aggregator\n")
5657 {
5658 const char *xpath =
5659 "./set-action[action='frr-bgp-route-map:aggregator']";
5660
5661 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5662 return nb_cli_apply_changes(vty, NULL);
5663 }
5664
5665 DEFUN_YANG (match_ipv6_next_hop,
5666 match_ipv6_next_hop_cmd,
5667 "match ipv6 next-hop X:X::X:X",
5668 MATCH_STR
5669 IPV6_STR
5670 "Match IPv6 next-hop address of route\n"
5671 "IPv6 address of next hop\n")
5672 {
5673 const char *xpath =
5674 "./match-condition[condition='frr-bgp-route-map:ipv6-nexthop']";
5675 char xpath_value[XPATH_MAXLEN];
5676
5677 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5678 snprintf(xpath_value, sizeof(xpath_value),
5679 "%s/rmap-match-condition/frr-bgp-route-map:ipv6-address",
5680 xpath);
5681 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[3]->arg);
5682
5683 return nb_cli_apply_changes(vty, NULL);
5684 }
5685
5686 DEFUN_YANG (no_match_ipv6_next_hop,
5687 no_match_ipv6_next_hop_cmd,
5688 "no match ipv6 next-hop X:X::X:X",
5689 NO_STR
5690 MATCH_STR
5691 IPV6_STR
5692 "Match IPv6 next-hop address of route\n"
5693 "IPv6 address of next hop\n")
5694 {
5695 const char *xpath =
5696 "./match-condition[condition='frr-bgp-route-map:ipv6-nexthop']";
5697
5698 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5699 return nb_cli_apply_changes(vty, NULL);
5700 }
5701
5702 DEFPY_YANG (match_ipv4_next_hop,
5703 match_ipv4_next_hop_cmd,
5704 "match ip next-hop address A.B.C.D",
5705 MATCH_STR
5706 IP_STR
5707 "Match IP next-hop address of route\n"
5708 "IP address\n"
5709 "IP address of next-hop\n")
5710 {
5711 const char *xpath =
5712 "./match-condition[condition='frr-bgp-route-map:ipv4-nexthop']";
5713 char xpath_value[XPATH_MAXLEN];
5714
5715 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5716 snprintf(xpath_value, sizeof(xpath_value),
5717 "%s/rmap-match-condition/frr-bgp-route-map:ipv4-address",
5718 xpath);
5719 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[4]->arg);
5720
5721 return nb_cli_apply_changes(vty, NULL);
5722 }
5723
5724 DEFPY_YANG (no_match_ipv4_next_hop,
5725 no_match_ipv4_next_hop_cmd,
5726 "no match ip next-hop address [A.B.C.D]",
5727 NO_STR
5728 MATCH_STR
5729 IP_STR
5730 "Match IP next-hop address of route\n"
5731 "IP address\n"
5732 "IP address of next-hop\n")
5733 {
5734 const char *xpath =
5735 "./match-condition[condition='frr-bgp-route-map:ipv4-nexthop']";
5736
5737 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5738 return nb_cli_apply_changes(vty, NULL);
5739 }
5740
5741 DEFUN_YANG (set_ipv6_nexthop_peer,
5742 set_ipv6_nexthop_peer_cmd,
5743 "set ipv6 next-hop peer-address",
5744 SET_STR
5745 IPV6_STR
5746 "Next hop address\n"
5747 "Use peer address (for BGP only)\n")
5748 {
5749 const char *xpath =
5750 "./set-action[action='frr-bgp-route-map:ipv6-peer-address']";
5751 char xpath_value[XPATH_MAXLEN];
5752
5753 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5754 snprintf(xpath_value, sizeof(xpath_value),
5755 "%s/rmap-set-action/frr-bgp-route-map:preference", xpath);
5756 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
5757
5758 return nb_cli_apply_changes(vty, NULL);
5759 }
5760
5761 DEFUN_YANG (no_set_ipv6_nexthop_peer,
5762 no_set_ipv6_nexthop_peer_cmd,
5763 "no set ipv6 next-hop peer-address",
5764 NO_STR
5765 SET_STR
5766 IPV6_STR
5767 "IPv6 next-hop address\n"
5768 "Use peer address (for BGP only)\n")
5769 {
5770 const char *xpath =
5771 "./set-action[action='frr-bgp-route-map:ipv6-peer-address']";
5772
5773 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5774 return nb_cli_apply_changes(vty, NULL);
5775 }
5776
5777 DEFUN_YANG (set_ipv6_nexthop_prefer_global,
5778 set_ipv6_nexthop_prefer_global_cmd,
5779 "set ipv6 next-hop prefer-global",
5780 SET_STR
5781 IPV6_STR
5782 "IPv6 next-hop address\n"
5783 "Prefer global over link-local if both exist\n")
5784 {
5785 const char *xpath =
5786 "./set-action[action='frr-bgp-route-map:ipv6-prefer-global']";
5787 char xpath_value[XPATH_MAXLEN];
5788
5789 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5790 snprintf(xpath_value, sizeof(xpath_value),
5791 "%s/rmap-set-action/frr-bgp-route-map:preference", xpath);
5792 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
5793
5794 return nb_cli_apply_changes(vty, NULL);
5795 }
5796
5797 DEFUN_YANG (no_set_ipv6_nexthop_prefer_global,
5798 no_set_ipv6_nexthop_prefer_global_cmd,
5799 "no set ipv6 next-hop prefer-global",
5800 NO_STR
5801 SET_STR
5802 IPV6_STR
5803 "IPv6 next-hop address\n"
5804 "Prefer global over link-local if both exist\n")
5805 {
5806 const char *xpath =
5807 "./set-action[action='frr-bgp-route-map:ipv6-prefer-global']";
5808
5809 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5810 return nb_cli_apply_changes(vty, NULL);
5811 }
5812
5813 DEFUN_YANG (set_ipv6_nexthop_global,
5814 set_ipv6_nexthop_global_cmd,
5815 "set ipv6 next-hop global X:X::X:X",
5816 SET_STR
5817 IPV6_STR
5818 "IPv6 next-hop address\n"
5819 "IPv6 global address\n"
5820 "IPv6 address of next hop\n")
5821 {
5822 int idx_ipv6 = 4;
5823 const char *xpath =
5824 "./set-action[action='frr-bgp-route-map:ipv6-nexthop-global']";
5825 char xpath_value[XPATH_MAXLEN];
5826
5827 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5828 snprintf(xpath_value, sizeof(xpath_value),
5829 "%s/rmap-set-action/frr-bgp-route-map:ipv6-address", xpath);
5830 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5831 argv[idx_ipv6]->arg);
5832
5833 return nb_cli_apply_changes(vty, NULL);
5834 }
5835
5836 DEFUN_YANG (no_set_ipv6_nexthop_global,
5837 no_set_ipv6_nexthop_global_cmd,
5838 "no set ipv6 next-hop global X:X::X:X",
5839 NO_STR
5840 SET_STR
5841 IPV6_STR
5842 "IPv6 next-hop address\n"
5843 "IPv6 global address\n"
5844 "IPv6 address of next hop\n")
5845 {
5846 const char *xpath =
5847 "./set-action[action='frr-bgp-route-map:ipv6-nexthop-global']";
5848
5849 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5850 return nb_cli_apply_changes(vty, NULL);
5851 }
5852
5853 #ifdef KEEP_OLD_VPN_COMMANDS
5854 DEFUN_YANG (set_vpn_nexthop,
5855 set_vpn_nexthop_cmd,
5856 "set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
5857 SET_STR
5858 "VPNv4 information\n"
5859 "VPN next-hop address\n"
5860 "IP address of next hop\n"
5861 "VPNv6 information\n"
5862 "VPN next-hop address\n"
5863 "IPv6 address of next hop\n")
5864 {
5865 int idx_ip = 3;
5866 afi_t afi;
5867 int idx = 0;
5868 char xpath_value[XPATH_MAXLEN];
5869
5870 if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
5871 if (afi == AFI_IP) {
5872 const char *xpath =
5873 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
5874
5875 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5876 snprintf(
5877 xpath_value, sizeof(xpath_value),
5878 "%s/rmap-set-action/frr-bgp-route-map:ipv4-address",
5879 xpath);
5880 } else {
5881 const char *xpath =
5882 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
5883
5884 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5885 snprintf(
5886 xpath_value, sizeof(xpath_value),
5887 "%s/rmap-set-action/frr-bgp-route-map:ipv6-address",
5888 xpath);
5889 }
5890
5891 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5892 argv[idx_ip]->arg);
5893
5894 return nb_cli_apply_changes(vty, NULL);
5895 }
5896
5897 return CMD_SUCCESS;
5898 }
5899
5900 DEFUN_YANG (no_set_vpn_nexthop,
5901 no_set_vpn_nexthop_cmd,
5902 "no set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
5903 NO_STR
5904 SET_STR
5905 "VPNv4 information\n"
5906 "VPN next-hop address\n"
5907 "IP address of next hop\n"
5908 "VPNv6 information\n"
5909 "VPN next-hop address\n"
5910 "IPv6 address of next hop\n")
5911 {
5912 afi_t afi;
5913 int idx = 0;
5914
5915 if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
5916 if (afi == AFI_IP) {
5917 const char *xpath =
5918 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
5919 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5920 } else {
5921 const char *xpath =
5922 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
5923 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5924 }
5925 return nb_cli_apply_changes(vty, NULL);
5926 }
5927 return CMD_SUCCESS;
5928 }
5929 #endif /* KEEP_OLD_VPN_COMMANDS */
5930
5931 DEFUN_YANG (set_ipx_vpn_nexthop,
5932 set_ipx_vpn_nexthop_cmd,
5933 "set <ipv4|ipv6> vpn next-hop <A.B.C.D|X:X::X:X>",
5934 SET_STR
5935 "IPv4 information\n"
5936 "IPv6 information\n"
5937 "VPN information\n"
5938 "VPN next-hop address\n"
5939 "IP address of next hop\n"
5940 "IPv6 address of next hop\n")
5941 {
5942 int idx_ip = 4;
5943 afi_t afi;
5944 int idx = 0;
5945 char xpath_value[XPATH_MAXLEN];
5946
5947 if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
5948 if (afi == AFI_IP) {
5949 const char *xpath =
5950 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
5951
5952 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5953 snprintf(
5954 xpath_value, sizeof(xpath_value),
5955 "%s/rmap-set-action/frr-bgp-route-map:ipv4-address",
5956 xpath);
5957 } else {
5958 const char *xpath =
5959 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
5960
5961 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5962 snprintf(
5963 xpath_value, sizeof(xpath_value),
5964 "%s/rmap-set-action/frr-bgp-route-map:ipv6-address",
5965 xpath);
5966 }
5967 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5968 argv[idx_ip]->arg);
5969 return nb_cli_apply_changes(vty, NULL);
5970 }
5971 return CMD_SUCCESS;
5972 }
5973
5974 DEFUN_YANG (no_set_ipx_vpn_nexthop,
5975 no_set_ipx_vpn_nexthop_cmd,
5976 "no set <ipv4|ipv6> vpn next-hop [<A.B.C.D|X:X::X:X>]",
5977 NO_STR
5978 SET_STR
5979 "IPv4 information\n"
5980 "IPv6 information\n"
5981 "VPN information\n"
5982 "VPN next-hop address\n"
5983 "IP address of next hop\n"
5984 "IPv6 address of next hop\n")
5985 {
5986 afi_t afi;
5987 int idx = 0;
5988
5989 if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
5990 if (afi == AFI_IP) {
5991 const char *xpath =
5992 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
5993 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5994 } else {
5995 const char *xpath =
5996 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
5997 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5998 }
5999 return nb_cli_apply_changes(vty, NULL);
6000 }
6001 return CMD_SUCCESS;
6002 }
6003
6004 DEFUN_YANG (set_originator_id,
6005 set_originator_id_cmd,
6006 "set originator-id A.B.C.D",
6007 SET_STR
6008 "BGP originator ID attribute\n"
6009 "IP address of originator\n")
6010 {
6011 int idx_ipv4 = 2;
6012 const char *xpath =
6013 "./set-action[action='frr-bgp-route-map:originator-id']";
6014 char xpath_value[XPATH_MAXLEN];
6015
6016 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6017 snprintf(xpath_value, sizeof(xpath_value),
6018 "%s/rmap-set-action/frr-bgp-route-map:originator-id", xpath);
6019 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6020 argv[idx_ipv4]->arg);
6021
6022 return nb_cli_apply_changes(vty, NULL);
6023 }
6024
6025 DEFUN_YANG (no_set_originator_id,
6026 no_set_originator_id_cmd,
6027 "no set originator-id [A.B.C.D]",
6028 NO_STR
6029 SET_STR
6030 "BGP originator ID attribute\n"
6031 "IP address of originator\n")
6032 {
6033 const char *xpath =
6034 "./set-action[action='frr-bgp-route-map:originator-id']";
6035
6036 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6037 return nb_cli_apply_changes(vty, NULL);
6038 }
6039
6040 /* Initialization of route map. */
6041 void bgp_route_map_init(void)
6042 {
6043 route_map_init();
6044
6045 route_map_add_hook(bgp_route_map_add);
6046 route_map_delete_hook(bgp_route_map_delete);
6047 route_map_event_hook(bgp_route_map_event);
6048
6049 route_map_match_interface_hook(generic_match_add);
6050 route_map_no_match_interface_hook(generic_match_delete);
6051
6052 route_map_match_ip_address_hook(generic_match_add);
6053 route_map_no_match_ip_address_hook(generic_match_delete);
6054
6055 route_map_match_ip_address_prefix_list_hook(generic_match_add);
6056 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
6057
6058 route_map_match_ip_next_hop_hook(generic_match_add);
6059 route_map_no_match_ip_next_hop_hook(generic_match_delete);
6060
6061 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
6062 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
6063
6064 route_map_match_ip_next_hop_type_hook(generic_match_add);
6065 route_map_no_match_ip_next_hop_type_hook(generic_match_delete);
6066
6067 route_map_match_ipv6_address_hook(generic_match_add);
6068 route_map_no_match_ipv6_address_hook(generic_match_delete);
6069
6070 route_map_match_ipv6_address_prefix_list_hook(generic_match_add);
6071 route_map_no_match_ipv6_address_prefix_list_hook(generic_match_delete);
6072
6073 route_map_match_ipv6_next_hop_type_hook(generic_match_add);
6074 route_map_no_match_ipv6_next_hop_type_hook(generic_match_delete);
6075
6076 route_map_match_metric_hook(generic_match_add);
6077 route_map_no_match_metric_hook(generic_match_delete);
6078
6079 route_map_match_tag_hook(generic_match_add);
6080 route_map_no_match_tag_hook(generic_match_delete);
6081
6082 route_map_set_srte_color_hook(generic_set_add);
6083 route_map_no_set_srte_color_hook(generic_set_delete);
6084
6085 route_map_set_ip_nexthop_hook(generic_set_add);
6086 route_map_no_set_ip_nexthop_hook(generic_set_delete);
6087
6088 route_map_set_ipv6_nexthop_local_hook(generic_set_add);
6089 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete);
6090
6091 route_map_set_metric_hook(generic_set_add);
6092 route_map_no_set_metric_hook(generic_set_delete);
6093
6094 route_map_set_tag_hook(generic_set_add);
6095 route_map_no_set_tag_hook(generic_set_delete);
6096
6097 route_map_install_match(&route_match_peer_cmd);
6098 route_map_install_match(&route_match_local_pref_cmd);
6099 #ifdef HAVE_SCRIPTING
6100 route_map_install_match(&route_match_script_cmd);
6101 #endif
6102 route_map_install_match(&route_match_ip_address_cmd);
6103 route_map_install_match(&route_match_ip_next_hop_cmd);
6104 route_map_install_match(&route_match_ip_route_source_cmd);
6105 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
6106 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
6107 route_map_install_match(&route_match_ip_next_hop_type_cmd);
6108 route_map_install_match(&route_match_ip_route_source_prefix_list_cmd);
6109 route_map_install_match(&route_match_aspath_cmd);
6110 route_map_install_match(&route_match_community_cmd);
6111 route_map_install_match(&route_match_lcommunity_cmd);
6112 route_map_install_match(&route_match_ecommunity_cmd);
6113 route_map_install_match(&route_match_local_pref_cmd);
6114 route_map_install_match(&route_match_metric_cmd);
6115 route_map_install_match(&route_match_origin_cmd);
6116 route_map_install_match(&route_match_probability_cmd);
6117 route_map_install_match(&route_match_interface_cmd);
6118 route_map_install_match(&route_match_tag_cmd);
6119 route_map_install_match(&route_match_mac_address_cmd);
6120 route_map_install_match(&route_match_evpn_vni_cmd);
6121 route_map_install_match(&route_match_evpn_route_type_cmd);
6122 route_map_install_match(&route_match_evpn_rd_cmd);
6123 route_map_install_match(&route_match_evpn_default_route_cmd);
6124 route_map_install_match(&route_match_vrl_source_vrf_cmd);
6125
6126 route_map_install_set(&route_set_table_id_cmd);
6127 route_map_install_set(&route_set_srte_color_cmd);
6128 route_map_install_set(&route_set_ip_nexthop_cmd);
6129 route_map_install_set(&route_set_local_pref_cmd);
6130 route_map_install_set(&route_set_weight_cmd);
6131 route_map_install_set(&route_set_label_index_cmd);
6132 route_map_install_set(&route_set_metric_cmd);
6133 route_map_install_set(&route_set_distance_cmd);
6134 route_map_install_set(&route_set_aspath_prepend_cmd);
6135 route_map_install_set(&route_set_aspath_exclude_cmd);
6136 route_map_install_set(&route_set_origin_cmd);
6137 route_map_install_set(&route_set_atomic_aggregate_cmd);
6138 route_map_install_set(&route_set_aggregator_as_cmd);
6139 route_map_install_set(&route_set_community_cmd);
6140 route_map_install_set(&route_set_community_delete_cmd);
6141 route_map_install_set(&route_set_lcommunity_cmd);
6142 route_map_install_set(&route_set_lcommunity_delete_cmd);
6143 route_map_install_set(&route_set_vpnv4_nexthop_cmd);
6144 route_map_install_set(&route_set_vpnv6_nexthop_cmd);
6145 route_map_install_set(&route_set_originator_id_cmd);
6146 route_map_install_set(&route_set_ecommunity_rt_cmd);
6147 route_map_install_set(&route_set_ecommunity_soo_cmd);
6148 route_map_install_set(&route_set_ecommunity_lb_cmd);
6149 route_map_install_set(&route_set_tag_cmd);
6150 route_map_install_set(&route_set_label_index_cmd);
6151
6152 install_element(RMAP_NODE, &match_peer_cmd);
6153 install_element(RMAP_NODE, &match_peer_local_cmd);
6154 install_element(RMAP_NODE, &no_match_peer_cmd);
6155 install_element(RMAP_NODE, &match_ip_route_source_cmd);
6156 install_element(RMAP_NODE, &no_match_ip_route_source_cmd);
6157 install_element(RMAP_NODE, &match_ip_route_source_prefix_list_cmd);
6158 install_element(RMAP_NODE, &no_match_ip_route_source_prefix_list_cmd);
6159 install_element(RMAP_NODE, &match_mac_address_cmd);
6160 install_element(RMAP_NODE, &no_match_mac_address_cmd);
6161 install_element(RMAP_NODE, &match_evpn_vni_cmd);
6162 install_element(RMAP_NODE, &no_match_evpn_vni_cmd);
6163 install_element(RMAP_NODE, &match_evpn_route_type_cmd);
6164 install_element(RMAP_NODE, &no_match_evpn_route_type_cmd);
6165 install_element(RMAP_NODE, &match_evpn_rd_cmd);
6166 install_element(RMAP_NODE, &no_match_evpn_rd_cmd);
6167 install_element(RMAP_NODE, &match_evpn_default_route_cmd);
6168 install_element(RMAP_NODE, &no_match_evpn_default_route_cmd);
6169 install_element(RMAP_NODE, &match_vrl_source_vrf_cmd);
6170 install_element(RMAP_NODE, &no_match_vrl_source_vrf_cmd);
6171
6172 install_element(RMAP_NODE, &match_aspath_cmd);
6173 install_element(RMAP_NODE, &no_match_aspath_cmd);
6174 install_element(RMAP_NODE, &match_local_pref_cmd);
6175 install_element(RMAP_NODE, &no_match_local_pref_cmd);
6176 install_element(RMAP_NODE, &match_community_cmd);
6177 install_element(RMAP_NODE, &no_match_community_cmd);
6178 install_element(RMAP_NODE, &match_lcommunity_cmd);
6179 install_element(RMAP_NODE, &no_match_lcommunity_cmd);
6180 install_element(RMAP_NODE, &match_ecommunity_cmd);
6181 install_element(RMAP_NODE, &no_match_ecommunity_cmd);
6182 install_element(RMAP_NODE, &match_origin_cmd);
6183 install_element(RMAP_NODE, &no_match_origin_cmd);
6184 install_element(RMAP_NODE, &match_probability_cmd);
6185 install_element(RMAP_NODE, &no_match_probability_cmd);
6186
6187 install_element(RMAP_NODE, &no_set_table_id_cmd);
6188 install_element(RMAP_NODE, &set_table_id_cmd);
6189 install_element(RMAP_NODE, &set_ip_nexthop_peer_cmd);
6190 install_element(RMAP_NODE, &set_ip_nexthop_unchanged_cmd);
6191 install_element(RMAP_NODE, &set_local_pref_cmd);
6192 install_element(RMAP_NODE, &set_distance_cmd);
6193 install_element(RMAP_NODE, &no_set_distance_cmd);
6194 install_element(RMAP_NODE, &no_set_local_pref_cmd);
6195 install_element(RMAP_NODE, &set_weight_cmd);
6196 install_element(RMAP_NODE, &set_label_index_cmd);
6197 install_element(RMAP_NODE, &no_set_weight_cmd);
6198 install_element(RMAP_NODE, &no_set_label_index_cmd);
6199 install_element(RMAP_NODE, &set_aspath_prepend_asn_cmd);
6200 install_element(RMAP_NODE, &set_aspath_prepend_lastas_cmd);
6201 install_element(RMAP_NODE, &set_aspath_exclude_cmd);
6202 install_element(RMAP_NODE, &no_set_aspath_prepend_cmd);
6203 install_element(RMAP_NODE, &no_set_aspath_prepend_lastas_cmd);
6204 install_element(RMAP_NODE, &no_set_aspath_exclude_cmd);
6205 install_element(RMAP_NODE, &no_set_aspath_exclude_all_cmd);
6206 install_element(RMAP_NODE, &set_origin_cmd);
6207 install_element(RMAP_NODE, &no_set_origin_cmd);
6208 install_element(RMAP_NODE, &set_atomic_aggregate_cmd);
6209 install_element(RMAP_NODE, &no_set_atomic_aggregate_cmd);
6210 install_element(RMAP_NODE, &set_aggregator_as_cmd);
6211 install_element(RMAP_NODE, &no_set_aggregator_as_cmd);
6212 install_element(RMAP_NODE, &set_community_cmd);
6213 install_element(RMAP_NODE, &set_community_none_cmd);
6214 install_element(RMAP_NODE, &no_set_community_cmd);
6215 install_element(RMAP_NODE, &no_set_community_short_cmd);
6216 install_element(RMAP_NODE, &set_community_delete_cmd);
6217 install_element(RMAP_NODE, &no_set_community_delete_cmd);
6218 install_element(RMAP_NODE, &set_lcommunity_cmd);
6219 install_element(RMAP_NODE, &set_lcommunity_none_cmd);
6220 install_element(RMAP_NODE, &no_set_lcommunity_cmd);
6221 install_element(RMAP_NODE, &no_set_lcommunity1_cmd);
6222 install_element(RMAP_NODE, &no_set_lcommunity1_short_cmd);
6223 install_element(RMAP_NODE, &set_lcommunity_delete_cmd);
6224 install_element(RMAP_NODE, &no_set_lcommunity_delete_cmd);
6225 install_element(RMAP_NODE, &no_set_lcommunity_delete_short_cmd);
6226 install_element(RMAP_NODE, &set_ecommunity_rt_cmd);
6227 install_element(RMAP_NODE, &no_set_ecommunity_rt_cmd);
6228 install_element(RMAP_NODE, &no_set_ecommunity_rt_short_cmd);
6229 install_element(RMAP_NODE, &set_ecommunity_soo_cmd);
6230 install_element(RMAP_NODE, &no_set_ecommunity_soo_cmd);
6231 install_element(RMAP_NODE, &no_set_ecommunity_soo_short_cmd);
6232 install_element(RMAP_NODE, &set_ecommunity_lb_cmd);
6233 install_element(RMAP_NODE, &no_set_ecommunity_lb_cmd);
6234 install_element(RMAP_NODE, &no_set_ecommunity_lb_short_cmd);
6235 #ifdef KEEP_OLD_VPN_COMMANDS
6236 install_element(RMAP_NODE, &set_vpn_nexthop_cmd);
6237 install_element(RMAP_NODE, &no_set_vpn_nexthop_cmd);
6238 #endif /* KEEP_OLD_VPN_COMMANDS */
6239 install_element(RMAP_NODE, &set_ipx_vpn_nexthop_cmd);
6240 install_element(RMAP_NODE, &no_set_ipx_vpn_nexthop_cmd);
6241 install_element(RMAP_NODE, &set_originator_id_cmd);
6242 install_element(RMAP_NODE, &no_set_originator_id_cmd);
6243
6244 route_map_install_match(&route_match_ipv6_address_cmd);
6245 route_map_install_match(&route_match_ipv6_next_hop_cmd);
6246 route_map_install_match(&route_match_ipv4_next_hop_cmd);
6247 route_map_install_match(&route_match_ipv6_address_prefix_list_cmd);
6248 route_map_install_match(&route_match_ipv6_next_hop_type_cmd);
6249 route_map_install_set(&route_set_ipv6_nexthop_global_cmd);
6250 route_map_install_set(&route_set_ipv6_nexthop_prefer_global_cmd);
6251 route_map_install_set(&route_set_ipv6_nexthop_local_cmd);
6252 route_map_install_set(&route_set_ipv6_nexthop_peer_cmd);
6253
6254 install_element(RMAP_NODE, &match_ipv6_next_hop_cmd);
6255 install_element(RMAP_NODE, &no_match_ipv6_next_hop_cmd);
6256 install_element(RMAP_NODE, &match_ipv4_next_hop_cmd);
6257 install_element(RMAP_NODE, &no_match_ipv4_next_hop_cmd);
6258 install_element(RMAP_NODE, &set_ipv6_nexthop_global_cmd);
6259 install_element(RMAP_NODE, &no_set_ipv6_nexthop_global_cmd);
6260 install_element(RMAP_NODE, &set_ipv6_nexthop_prefer_global_cmd);
6261 install_element(RMAP_NODE, &no_set_ipv6_nexthop_prefer_global_cmd);
6262 install_element(RMAP_NODE, &set_ipv6_nexthop_peer_cmd);
6263 install_element(RMAP_NODE, &no_set_ipv6_nexthop_peer_cmd);
6264 #ifdef HAVE_SCRIPTING
6265 install_element(RMAP_NODE, &match_script_cmd);
6266 #endif
6267 }
6268
6269 void bgp_route_map_terminate(void)
6270 {
6271 /* ToDo: Cleanup all the used memory */
6272 route_map_finish();
6273 }