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