]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_routemap.c
Merge pull request #12969 from opensourcerouting/ospfd-nssa
[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 event *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 EVENT_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 event_add_timer(bm->master, bgp_route_map_update_timer, NULL,
4373 bm->rmap_update_timer, &bm->t_rmap_update);
4374
4375 /* Signal the groups that a route-map update event has
4376 * started */
4377 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
4378 update_group_policy_update(bgp, BGP_POLICY_ROUTE_MAP,
4379 rmap_name, true, 1);
4380 } else {
4381 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
4382 bgp_route_map_process_update(bgp, rmap_name, false);
4383 #ifdef ENABLE_BGP_VNC
4384 vnc_routemap_update(bgp, __func__);
4385 #endif
4386 }
4387
4388 vpn_policy_routemap_event(rmap_name);
4389 }
4390 }
4391
4392 static void bgp_route_map_add(const char *rmap_name)
4393 {
4394 if (route_map_mark_updated(rmap_name) == 0)
4395 bgp_route_map_mark_update(rmap_name);
4396
4397 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
4398 }
4399
4400 static void bgp_route_map_delete(const char *rmap_name)
4401 {
4402 if (route_map_mark_updated(rmap_name) == 0)
4403 bgp_route_map_mark_update(rmap_name);
4404
4405 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_DELETED);
4406 }
4407
4408 static void bgp_route_map_event(const char *rmap_name)
4409 {
4410 if (route_map_mark_updated(rmap_name) == 0)
4411 bgp_route_map_mark_update(rmap_name);
4412
4413 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
4414 }
4415
4416 DEFUN_YANG (match_mac_address,
4417 match_mac_address_cmd,
4418 "match mac address ACCESSLIST_MAC_NAME",
4419 MATCH_STR
4420 "mac address\n"
4421 "Match address of route\n"
4422 "MAC Access-list name\n")
4423 {
4424 const char *xpath =
4425 "./match-condition[condition='frr-bgp-route-map:mac-address-list']";
4426 char xpath_value[XPATH_MAXLEN];
4427
4428 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4429 snprintf(xpath_value, sizeof(xpath_value),
4430 "%s/rmap-match-condition/frr-bgp-route-map:list-name", xpath);
4431 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[3]->arg);
4432
4433 return nb_cli_apply_changes(vty, NULL);
4434 }
4435
4436 DEFUN_YANG (no_match_mac_address,
4437 no_match_mac_address_cmd,
4438 "no match mac address ACCESSLIST_MAC_NAME",
4439 NO_STR
4440 MATCH_STR
4441 "mac\n"
4442 "Match address of route\n"
4443 "MAC acess-list name\n")
4444 {
4445 const char *xpath =
4446 "./match-condition[condition='frr-bgp-route-map:mac-address-list']";
4447
4448 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4449 return nb_cli_apply_changes(vty, NULL);
4450 }
4451
4452 /*
4453 * Helper to handle the case of the user passing in a number or type string
4454 */
4455 static const char *parse_evpn_rt_type(const char *num_rt_type)
4456 {
4457 switch (num_rt_type[0]) {
4458 case '1':
4459 return "ead";
4460 case '2':
4461 return "macip";
4462 case '3':
4463 return "multicast";
4464 case '4':
4465 return "es";
4466 case '5':
4467 return "prefix";
4468 default:
4469 break;
4470 }
4471
4472 /* Was already full type string */
4473 return num_rt_type;
4474 }
4475
4476 DEFUN_YANG (match_evpn_route_type,
4477 match_evpn_route_type_cmd,
4478 "match evpn route-type <ead|1|macip|2|multicast|3|es|4|prefix|5>",
4479 MATCH_STR
4480 EVPN_HELP_STR
4481 EVPN_TYPE_HELP_STR
4482 EVPN_TYPE_1_HELP_STR
4483 EVPN_TYPE_1_HELP_STR
4484 EVPN_TYPE_2_HELP_STR
4485 EVPN_TYPE_2_HELP_STR
4486 EVPN_TYPE_3_HELP_STR
4487 EVPN_TYPE_3_HELP_STR
4488 EVPN_TYPE_4_HELP_STR
4489 EVPN_TYPE_4_HELP_STR
4490 EVPN_TYPE_5_HELP_STR
4491 EVPN_TYPE_5_HELP_STR)
4492 {
4493 const char *xpath =
4494 "./match-condition[condition='frr-bgp-route-map:evpn-route-type']";
4495 char xpath_value[XPATH_MAXLEN];
4496
4497 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4498 snprintf(xpath_value, sizeof(xpath_value),
4499 "%s/rmap-match-condition/frr-bgp-route-map:evpn-route-type",
4500 xpath);
4501 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4502 parse_evpn_rt_type(argv[3]->arg));
4503
4504 return nb_cli_apply_changes(vty, NULL);
4505 }
4506
4507 DEFUN_YANG (no_match_evpn_route_type,
4508 no_match_evpn_route_type_cmd,
4509 "no match evpn route-type <ead|1|macip|2|multicast|3|es|4|prefix|5>",
4510 NO_STR
4511 MATCH_STR
4512 EVPN_HELP_STR
4513 EVPN_TYPE_HELP_STR
4514 EVPN_TYPE_1_HELP_STR
4515 EVPN_TYPE_1_HELP_STR
4516 EVPN_TYPE_2_HELP_STR
4517 EVPN_TYPE_2_HELP_STR
4518 EVPN_TYPE_3_HELP_STR
4519 EVPN_TYPE_3_HELP_STR
4520 EVPN_TYPE_4_HELP_STR
4521 EVPN_TYPE_4_HELP_STR
4522 EVPN_TYPE_5_HELP_STR
4523 EVPN_TYPE_5_HELP_STR)
4524 {
4525 const char *xpath =
4526 "./match-condition[condition='frr-bgp-route-map:evpn-route-type']";
4527
4528 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4529
4530 return nb_cli_apply_changes(vty, NULL);
4531 }
4532
4533
4534 DEFUN_YANG (match_evpn_vni,
4535 match_evpn_vni_cmd,
4536 "match evpn vni " CMD_VNI_RANGE,
4537 MATCH_STR
4538 EVPN_HELP_STR
4539 "Match VNI\n"
4540 "VNI ID\n")
4541 {
4542 const char *xpath =
4543 "./match-condition[condition='frr-bgp-route-map:evpn-vni']";
4544 char xpath_value[XPATH_MAXLEN];
4545
4546 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4547 snprintf(xpath_value, sizeof(xpath_value),
4548 "%s/rmap-match-condition/frr-bgp-route-map:evpn-vni", xpath);
4549 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[3]->arg);
4550
4551 return nb_cli_apply_changes(vty, NULL);
4552 }
4553
4554 DEFUN_YANG (no_match_evpn_vni,
4555 no_match_evpn_vni_cmd,
4556 "no match evpn vni " CMD_VNI_RANGE,
4557 NO_STR
4558 MATCH_STR
4559 EVPN_HELP_STR
4560 "Match VNI\n"
4561 "VNI ID\n")
4562 {
4563 const char *xpath =
4564 "./match-condition[condition='frr-bgp-route-map:evpn-vni']";
4565 char xpath_value[XPATH_MAXLEN];
4566
4567 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4568 snprintf(xpath_value, sizeof(xpath_value),
4569 "%s/rmap-match-condition/frr-bgp-route-map:evpn-vni", xpath);
4570 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY, argv[3]->arg);
4571
4572 return nb_cli_apply_changes(vty, NULL);
4573 }
4574
4575 DEFUN_YANG (match_evpn_default_route,
4576 match_evpn_default_route_cmd,
4577 "match evpn default-route",
4578 MATCH_STR
4579 EVPN_HELP_STR
4580 "default EVPN type-5 route\n")
4581 {
4582 const char *xpath =
4583 "./match-condition[condition='frr-bgp-route-map:evpn-default-route']";
4584 char xpath_value[XPATH_MAXLEN];
4585
4586 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4587
4588 snprintf(xpath_value, sizeof(xpath_value),
4589 "%s/rmap-match-condition/frr-bgp-route-map:evpn-default-route",
4590 xpath);
4591 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, NULL);
4592
4593 return nb_cli_apply_changes(vty, NULL);
4594 }
4595
4596 DEFUN_YANG (no_match_evpn_default_route,
4597 no_match_evpn_default_route_cmd,
4598 "no match evpn default-route",
4599 NO_STR
4600 MATCH_STR
4601 EVPN_HELP_STR
4602 "default EVPN type-5 route\n")
4603 {
4604 const char *xpath =
4605 "./match-condition[condition='frr-bgp-route-map:evpn-default-route']";
4606
4607 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4608
4609 return nb_cli_apply_changes(vty, NULL);
4610 }
4611
4612 DEFUN_YANG (match_evpn_rd,
4613 match_evpn_rd_cmd,
4614 "match evpn rd ASN:NN_OR_IP-ADDRESS:NN",
4615 MATCH_STR
4616 EVPN_HELP_STR
4617 "Route Distinguisher\n"
4618 "ASN:XX or A.B.C.D:XX\n")
4619 {
4620 const char *xpath =
4621 "./match-condition[condition='frr-bgp-route-map:evpn-rd']";
4622 char xpath_value[XPATH_MAXLEN];
4623
4624 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4625 snprintf(
4626 xpath_value, sizeof(xpath_value),
4627 "%s/rmap-match-condition/frr-bgp-route-map:route-distinguisher",
4628 xpath);
4629 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[3]->arg);
4630
4631 return nb_cli_apply_changes(vty, NULL);
4632 }
4633
4634 DEFUN_YANG (no_match_evpn_rd,
4635 no_match_evpn_rd_cmd,
4636 "no match evpn rd ASN:NN_OR_IP-ADDRESS:NN",
4637 NO_STR
4638 MATCH_STR
4639 EVPN_HELP_STR
4640 "Route Distinguisher\n"
4641 "ASN:XX or A.B.C.D:XX\n")
4642 {
4643 const char *xpath =
4644 "./match-condition[condition='frr-bgp-route-map:evpn-rd']";
4645
4646 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4647 return nb_cli_apply_changes(vty, NULL);
4648 }
4649
4650 DEFUN_YANG (set_evpn_gw_ip_ipv4,
4651 set_evpn_gw_ip_ipv4_cmd,
4652 "set evpn gateway-ip ipv4 A.B.C.D",
4653 SET_STR
4654 EVPN_HELP_STR
4655 "Set gateway IP for prefix advertisement route\n"
4656 "IPv4 address\n"
4657 "Gateway IP address in IPv4 format\n")
4658 {
4659 int ret;
4660 union sockunion su;
4661 const char *xpath =
4662 "./set-action[action='frr-bgp-route-map:set-evpn-gateway-ip-ipv4']";
4663 char xpath_value[XPATH_MAXLEN];
4664
4665 ret = str2sockunion(argv[4]->arg, &su);
4666 if (ret < 0) {
4667 vty_out(vty, "%% Malformed gateway IP\n");
4668 return CMD_WARNING_CONFIG_FAILED;
4669 }
4670
4671 if (su.sin.sin_addr.s_addr == 0 ||
4672 !ipv4_unicast_valid(&su.sin.sin_addr)) {
4673 vty_out(vty,
4674 "%% Gateway IP cannot be 0.0.0.0, multicast or reserved\n");
4675 return CMD_WARNING_CONFIG_FAILED;
4676 }
4677
4678 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4679
4680 snprintf(xpath_value, sizeof(xpath_value),
4681 "%s/rmap-set-action/frr-bgp-route-map:evpn-gateway-ip-ipv4",
4682 xpath);
4683
4684 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[4]->arg);
4685 return nb_cli_apply_changes(vty, NULL);
4686 }
4687
4688 DEFUN_YANG (no_set_evpn_gw_ip_ipv4,
4689 no_set_evpn_gw_ip_ipv4_cmd,
4690 "no set evpn gateway-ip ipv4 A.B.C.D",
4691 NO_STR
4692 SET_STR
4693 EVPN_HELP_STR
4694 "Set gateway IP for prefix advertisement route\n"
4695 "IPv4 address\n"
4696 "Gateway IP address in IPv4 format\n")
4697 {
4698 int ret;
4699 union sockunion su;
4700 const char *xpath =
4701 "./set-action[action='frr-bgp-route-map:set-evpn-gateway-ip-ipv4']";
4702
4703 ret = str2sockunion(argv[5]->arg, &su);
4704 if (ret < 0) {
4705 vty_out(vty, "%% Malformed gateway IP\n");
4706 return CMD_WARNING_CONFIG_FAILED;
4707 }
4708
4709 if (su.sin.sin_addr.s_addr == 0 ||
4710 !ipv4_unicast_valid(&su.sin.sin_addr)) {
4711 vty_out(vty,
4712 "%% Gateway IP cannot be 0.0.0.0, multicast or reserved\n");
4713 return CMD_WARNING_CONFIG_FAILED;
4714 }
4715
4716 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4717
4718 return nb_cli_apply_changes(vty, NULL);
4719 }
4720
4721 DEFUN_YANG (set_evpn_gw_ip_ipv6,
4722 set_evpn_gw_ip_ipv6_cmd,
4723 "set evpn gateway-ip ipv6 X:X::X:X",
4724 SET_STR
4725 EVPN_HELP_STR
4726 "Set gateway IP for prefix advertisement route\n"
4727 "IPv6 address\n"
4728 "Gateway IP address in IPv6 format\n")
4729 {
4730 int ret;
4731 union sockunion su;
4732 const char *xpath =
4733 "./set-action[action='frr-bgp-route-map:set-evpn-gateway-ip-ipv6']";
4734 char xpath_value[XPATH_MAXLEN];
4735
4736 ret = str2sockunion(argv[4]->arg, &su);
4737 if (ret < 0) {
4738 vty_out(vty, "%% Malformed gateway IP\n");
4739 return CMD_WARNING_CONFIG_FAILED;
4740 }
4741
4742 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr)
4743 || IN6_IS_ADDR_MULTICAST(&su.sin6.sin6_addr)) {
4744 vty_out(vty,
4745 "%% Gateway IP cannot be a linklocal or multicast address\n");
4746 return CMD_WARNING_CONFIG_FAILED;
4747 }
4748
4749 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4750
4751 snprintf(xpath_value, sizeof(xpath_value),
4752 "%s/rmap-set-action/frr-bgp-route-map:evpn-gateway-ip-ipv6",
4753 xpath);
4754
4755 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[4]->arg);
4756 return nb_cli_apply_changes(vty, NULL);
4757 }
4758
4759 DEFUN_YANG (no_set_evpn_gw_ip_ipv6,
4760 no_set_evpn_gw_ip_ipv6_cmd,
4761 "no set evpn gateway-ip ipv6 X:X::X:X",
4762 NO_STR
4763 SET_STR
4764 EVPN_HELP_STR
4765 "Set gateway IP for prefix advertisement route\n"
4766 "IPv4 address\n"
4767 "Gateway IP address in IPv4 format\n")
4768 {
4769 int ret;
4770 union sockunion su;
4771 const char *xpath =
4772 "./set-action[action='frr-bgp-route-map:set-evpn-gateway-ip-ipv6']";
4773
4774 ret = str2sockunion(argv[5]->arg, &su);
4775 if (ret < 0) {
4776 vty_out(vty, "%% Malformed gateway IP\n");
4777 return CMD_WARNING_CONFIG_FAILED;
4778 }
4779
4780 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr)
4781 || IN6_IS_ADDR_MULTICAST(&su.sin6.sin6_addr)) {
4782 vty_out(vty,
4783 "%% Gateway IP cannot be a linklocal or multicast address\n");
4784 return CMD_WARNING_CONFIG_FAILED;
4785 }
4786
4787 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4788
4789 return nb_cli_apply_changes(vty, NULL);
4790 }
4791
4792 DEFPY_YANG(match_vrl_source_vrf,
4793 match_vrl_source_vrf_cmd,
4794 "match source-vrf NAME$vrf_name",
4795 MATCH_STR
4796 "source vrf\n"
4797 "The VRF name\n")
4798 {
4799 const char *xpath =
4800 "./match-condition[condition='frr-bgp-route-map:source-vrf']";
4801 char xpath_value[XPATH_MAXLEN];
4802
4803 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4804 snprintf(xpath_value, sizeof(xpath_value),
4805 "%s/rmap-match-condition/frr-bgp-route-map:source-vrf", xpath);
4806 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, vrf_name);
4807
4808 return nb_cli_apply_changes(vty, NULL);
4809 }
4810
4811 DEFPY_YANG(no_match_vrl_source_vrf,
4812 no_match_vrl_source_vrf_cmd,
4813 "no match source-vrf NAME$vrf_name",
4814 NO_STR MATCH_STR
4815 "source vrf\n"
4816 "The VRF name\n")
4817 {
4818 const char *xpath =
4819 "./match-condition[condition='frr-bgp-route-map:source-vrf']";
4820
4821 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4822 return nb_cli_apply_changes(vty, NULL);
4823 }
4824
4825 DEFPY_YANG (match_peer,
4826 match_peer_cmd,
4827 "match peer <A.B.C.D$addrv4|X:X::X:X$addrv6|WORD$intf>",
4828 MATCH_STR
4829 "Match peer address\n"
4830 "IP address of peer\n"
4831 "IPv6 address of peer\n"
4832 "Interface name of peer or peer group name\n")
4833 {
4834 const char *xpath =
4835 "./match-condition[condition='frr-bgp-route-map:peer']";
4836 char xpath_value[XPATH_MAXLEN];
4837
4838 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4839
4840 if (addrv4_str) {
4841 snprintf(
4842 xpath_value, sizeof(xpath_value),
4843 "%s/rmap-match-condition/frr-bgp-route-map:peer-ipv4-address",
4844 xpath);
4845 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4846 addrv4_str);
4847 } else if (addrv6_str) {
4848 snprintf(
4849 xpath_value, sizeof(xpath_value),
4850 "%s/rmap-match-condition/frr-bgp-route-map:peer-ipv6-address",
4851 xpath);
4852 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4853 addrv6_str);
4854 } else {
4855 snprintf(
4856 xpath_value, sizeof(xpath_value),
4857 "%s/rmap-match-condition/frr-bgp-route-map:peer-interface",
4858 xpath);
4859 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, intf);
4860 }
4861
4862 return nb_cli_apply_changes(vty, NULL);
4863 }
4864
4865 DEFUN_YANG (match_peer_local,
4866 match_peer_local_cmd,
4867 "match peer local",
4868 MATCH_STR
4869 "Match peer address\n"
4870 "Static or Redistributed routes\n")
4871 {
4872 const char *xpath =
4873 "./match-condition[condition='frr-bgp-route-map:peer']";
4874 char xpath_value[XPATH_MAXLEN];
4875
4876 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4877
4878 snprintf(xpath_value, sizeof(xpath_value),
4879 "%s/rmap-match-condition/frr-bgp-route-map:peer-local", xpath);
4880 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
4881
4882 return nb_cli_apply_changes(vty, NULL);
4883 }
4884
4885 DEFUN_YANG (no_match_peer,
4886 no_match_peer_cmd,
4887 "no match peer [<local|A.B.C.D|X:X::X:X|WORD>]",
4888 NO_STR
4889 MATCH_STR
4890 "Match peer address\n"
4891 "Static or Redistributed routes\n"
4892 "IP address of peer\n"
4893 "IPv6 address of peer\n"
4894 "Interface name of peer\n")
4895 {
4896 const char *xpath =
4897 "./match-condition[condition='frr-bgp-route-map:peer']";
4898
4899 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4900
4901 return nb_cli_apply_changes(vty, NULL);
4902 }
4903
4904 #ifdef HAVE_SCRIPTING
4905 DEFUN_YANG (match_script,
4906 match_script_cmd,
4907 "[no] match script WORD",
4908 NO_STR
4909 MATCH_STR
4910 "Execute script to determine match\n"
4911 "The script name to run, without .lua; e.g. 'myroutemap' to run myroutemap.lua\n")
4912 {
4913 bool no = strmatch(argv[0]->text, "no");
4914 int i = 0;
4915 argv_find(argv, argc, "WORD", &i);
4916 const char *script = argv[i]->arg;
4917 const char *xpath =
4918 "./match-condition[condition='frr-bgp-route-map:match-script']";
4919 char xpath_value[XPATH_MAXLEN];
4920
4921 if (no) {
4922 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4923 snprintf(xpath_value, sizeof(xpath_value),
4924 "%s/rmap-match-condition/frr-bgp-route-map:script",
4925 xpath);
4926 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY,
4927 script);
4928
4929 return nb_cli_apply_changes(vty, NULL);
4930 }
4931
4932 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4933 snprintf(xpath_value, sizeof(xpath_value),
4934 "%s/rmap-match-condition/frr-bgp-route-map:script",
4935 xpath);
4936 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4937 script);
4938
4939 return nb_cli_apply_changes(vty, NULL);
4940 }
4941 #endif /* HAVE_SCRIPTING */
4942
4943 /* match probability */
4944 DEFUN_YANG (match_probability,
4945 match_probability_cmd,
4946 "match probability (0-100)",
4947 MATCH_STR
4948 "Match portion of routes defined by percentage value\n"
4949 "Percentage of routes\n")
4950 {
4951 int idx_number = 2;
4952
4953 const char *xpath =
4954 "./match-condition[condition='frr-bgp-route-map:probability']";
4955 char xpath_value[XPATH_MAXLEN];
4956
4957 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
4958 snprintf(xpath_value, sizeof(xpath_value),
4959 "%s/rmap-match-condition/frr-bgp-route-map:probability",
4960 xpath);
4961 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
4962 argv[idx_number]->arg);
4963
4964 return nb_cli_apply_changes(vty, NULL);
4965 }
4966
4967
4968 DEFUN_YANG (no_match_probability,
4969 no_match_probability_cmd,
4970 "no match probability [(1-99)]",
4971 NO_STR
4972 MATCH_STR
4973 "Match portion of routes defined by percentage value\n"
4974 "Percentage of routes\n")
4975 {
4976 int idx_number = 3;
4977 const char *xpath =
4978 "./match-condition[condition='frr-bgp-route-map:probability']";
4979 char xpath_value[XPATH_MAXLEN];
4980
4981 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
4982
4983 if (argc <= idx_number)
4984 return nb_cli_apply_changes(vty, NULL);
4985
4986 snprintf(xpath_value, sizeof(xpath_value),
4987 "%s/rmap-match-condition/frr-bgp-route-map:probability",
4988 xpath);
4989 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY,
4990 argv[idx_number]->arg);
4991
4992 return nb_cli_apply_changes(vty, NULL);
4993 }
4994
4995
4996 DEFPY_YANG (match_ip_route_source,
4997 match_ip_route_source_cmd,
4998 "match ip route-source ACCESSLIST4_NAME",
4999 MATCH_STR
5000 IP_STR
5001 "Match advertising source address of route\n"
5002 "IP Access-list name\n")
5003 {
5004 const char *xpath =
5005 "./match-condition[condition='frr-bgp-route-map:ip-route-source']";
5006 char xpath_value[XPATH_MAXLEN + 32];
5007 int idx_acl = 3;
5008
5009 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5010 snprintf(xpath_value, sizeof(xpath_value),
5011 "%s/rmap-match-condition/frr-bgp-route-map:list-name",
5012 xpath);
5013 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5014 argv[idx_acl]->arg);
5015
5016 return nb_cli_apply_changes(vty, NULL);
5017 }
5018
5019
5020 DEFUN_YANG (no_match_ip_route_source,
5021 no_match_ip_route_source_cmd,
5022 "no match ip route-source [ACCESSLIST4_NAME]",
5023 NO_STR
5024 MATCH_STR
5025 IP_STR
5026 "Match advertising source address of route\n"
5027 "IP Access-list name\n")
5028 {
5029 const char *xpath =
5030 "./match-condition[condition='frr-bgp-route-map:ip-route-source']";
5031
5032 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5033 return nb_cli_apply_changes(vty, NULL);
5034 }
5035
5036 DEFUN_YANG (match_ip_route_source_prefix_list,
5037 match_ip_route_source_prefix_list_cmd,
5038 "match ip route-source prefix-list PREFIXLIST_NAME",
5039 MATCH_STR
5040 IP_STR
5041 "Match advertising source address of route\n"
5042 "Match entries of prefix-lists\n"
5043 "IP prefix-list name\n")
5044 {
5045 int idx_word = 4;
5046 const char *xpath =
5047 "./match-condition[condition='frr-bgp-route-map:ip-route-source-prefix-list']";
5048 char xpath_value[XPATH_MAXLEN + 32];
5049
5050 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5051 snprintf(xpath_value, sizeof(xpath_value),
5052 "%s/rmap-match-condition/frr-bgp-route-map:list-name", xpath);
5053 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5054 argv[idx_word]->arg);
5055
5056 return nb_cli_apply_changes(vty, NULL);
5057 }
5058
5059
5060 DEFUN_YANG (no_match_ip_route_source_prefix_list,
5061 no_match_ip_route_source_prefix_list_cmd,
5062 "no match ip route-source prefix-list [PREFIXLIST_NAME]",
5063 NO_STR
5064 MATCH_STR
5065 IP_STR
5066 "Match advertising source address of route\n"
5067 "Match entries of prefix-lists\n"
5068 "IP prefix-list name\n")
5069 {
5070 const char *xpath =
5071 "./match-condition[condition='frr-bgp-route-map:ip-route-source-prefix-list']";
5072
5073 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5074 return nb_cli_apply_changes(vty, NULL);
5075 }
5076
5077 DEFUN_YANG (match_local_pref,
5078 match_local_pref_cmd,
5079 "match local-preference (0-4294967295)",
5080 MATCH_STR
5081 "Match local-preference of route\n"
5082 "Metric value\n")
5083 {
5084 int idx_number = 2;
5085
5086 const char *xpath =
5087 "./match-condition[condition='frr-bgp-route-map:match-local-preference']";
5088 char xpath_value[XPATH_MAXLEN];
5089
5090 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5091 snprintf(xpath_value, sizeof(xpath_value),
5092 "%s/rmap-match-condition/frr-bgp-route-map:local-preference",
5093 xpath);
5094 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5095 argv[idx_number]->arg);
5096
5097 return nb_cli_apply_changes(vty, NULL);
5098 }
5099
5100
5101 DEFUN_YANG (no_match_local_pref,
5102 no_match_local_pref_cmd,
5103 "no match local-preference [(0-4294967295)]",
5104 NO_STR
5105 MATCH_STR
5106 "Match local preference of route\n"
5107 "Local preference value\n")
5108 {
5109 int idx_localpref = 3;
5110 const char *xpath =
5111 "./match-condition[condition='frr-bgp-route-map:match-local-preference']";
5112 char xpath_value[XPATH_MAXLEN];
5113
5114 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5115
5116 if (argc <= idx_localpref)
5117 return nb_cli_apply_changes(vty, NULL);
5118
5119 snprintf(xpath_value, sizeof(xpath_value),
5120 "%s/rmap-match-condition/frr-bgp-route-map:local-preference",
5121 xpath);
5122 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY,
5123 argv[idx_localpref]->arg);
5124
5125 return nb_cli_apply_changes(vty, NULL);
5126 }
5127
5128 DEFUN_YANG(match_alias, match_alias_cmd, "match alias ALIAS_NAME",
5129 MATCH_STR
5130 "Match BGP community alias name\n"
5131 "BGP community alias name\n")
5132 {
5133 const char *alias = argv[2]->arg;
5134 struct community_alias ca1;
5135 struct community_alias *lookup_alias;
5136
5137 const char *xpath =
5138 "./match-condition[condition='frr-bgp-route-map:match-alias']";
5139 char xpath_value[XPATH_MAXLEN];
5140
5141 memset(&ca1, 0, sizeof(ca1));
5142 strlcpy(ca1.alias, alias, sizeof(ca1.alias));
5143 lookup_alias = bgp_ca_alias_lookup(&ca1);
5144 if (!lookup_alias) {
5145 vty_out(vty, "%% BGP alias name '%s' does not exist\n", alias);
5146 return CMD_WARNING_CONFIG_FAILED;
5147 }
5148
5149 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5150 snprintf(xpath_value, sizeof(xpath_value),
5151 "%s/rmap-match-condition/frr-bgp-route-map:alias", xpath);
5152 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, alias);
5153
5154 return nb_cli_apply_changes(vty, NULL);
5155 }
5156
5157
5158 DEFUN_YANG(no_match_alias, no_match_alias_cmd, "no match alias [ALIAS_NAME]",
5159 NO_STR MATCH_STR
5160 "Match BGP community alias name\n"
5161 "BGP community alias name\n")
5162 {
5163 int idx_alias = 3;
5164 const char *xpath =
5165 "./match-condition[condition='frr-bgp-route-map:match-alias']";
5166 char xpath_value[XPATH_MAXLEN];
5167
5168 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5169
5170 if (argc <= idx_alias)
5171 return nb_cli_apply_changes(vty, NULL);
5172
5173 snprintf(xpath_value, sizeof(xpath_value),
5174 "%s/rmap-match-condition/frr-bgp-route-map:alias", xpath);
5175 nb_cli_enqueue_change(vty, xpath_value, NB_OP_DESTROY,
5176 argv[idx_alias]->arg);
5177
5178 return nb_cli_apply_changes(vty, NULL);
5179 }
5180
5181 DEFPY_YANG (match_community,
5182 match_community_cmd,
5183 "match community <(1-99)|(100-500)|COMMUNITY_LIST_NAME> [exact-match]",
5184 MATCH_STR
5185 "Match BGP community list\n"
5186 "Community-list number (standard)\n"
5187 "Community-list number (expanded)\n"
5188 "Community-list name\n"
5189 "Do exact matching of communities\n")
5190 {
5191 const char *xpath =
5192 "./match-condition[condition='frr-bgp-route-map:match-community']";
5193 char xpath_value[XPATH_MAXLEN];
5194 char xpath_match[XPATH_MAXLEN];
5195 int idx_comm_list = 2;
5196
5197 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5198
5199 snprintf(
5200 xpath_value, sizeof(xpath_value),
5201 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name",
5202 xpath);
5203 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[idx_comm_list]->arg);
5204
5205 if (argc == 4) {
5206 snprintf(
5207 xpath_match, sizeof(xpath_match),
5208 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
5209 xpath);
5210 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
5211 "true");
5212 } else {
5213 snprintf(
5214 xpath_match, sizeof(xpath_match),
5215 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
5216 xpath);
5217 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
5218 "false");
5219 }
5220
5221 return nb_cli_apply_changes(vty, NULL);
5222 }
5223
5224 DEFUN_YANG (no_match_community,
5225 no_match_community_cmd,
5226 "no match community [<(1-99)|(100-500)|COMMUNITY_LIST_NAME> [exact-match]]",
5227 NO_STR
5228 MATCH_STR
5229 "Match BGP community list\n"
5230 "Community-list number (standard)\n"
5231 "Community-list number (expanded)\n"
5232 "Community-list name\n"
5233 "Do exact matching of communities\n")
5234 {
5235 const char *xpath =
5236 "./match-condition[condition='frr-bgp-route-map:match-community']";
5237
5238 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5239 return nb_cli_apply_changes(vty, NULL);
5240 }
5241
5242 DEFPY_YANG (match_lcommunity,
5243 match_lcommunity_cmd,
5244 "match large-community <(1-99)|(100-500)|LCOMMUNITY_LIST_NAME> [exact-match]",
5245 MATCH_STR
5246 "Match BGP large community list\n"
5247 "Large Community-list number (standard)\n"
5248 "Large Community-list number (expanded)\n"
5249 "Large Community-list name\n"
5250 "Do exact matching of communities\n")
5251 {
5252 const char *xpath =
5253 "./match-condition[condition='frr-bgp-route-map:match-large-community']";
5254 char xpath_value[XPATH_MAXLEN];
5255 char xpath_match[XPATH_MAXLEN];
5256 int idx_lcomm_list = 2;
5257
5258 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5259
5260 snprintf(
5261 xpath_value, sizeof(xpath_value),
5262 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name",
5263 xpath);
5264 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[idx_lcomm_list]->arg);
5265
5266 if (argc == 4) {
5267 snprintf(
5268 xpath_match, sizeof(xpath_match),
5269 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
5270 xpath);
5271 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
5272 "true");
5273 } else {
5274 snprintf(
5275 xpath_match, sizeof(xpath_match),
5276 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name-exact-match",
5277 xpath);
5278 nb_cli_enqueue_change(vty, xpath_match, NB_OP_MODIFY,
5279 "false");
5280 }
5281
5282 return nb_cli_apply_changes(vty, NULL);
5283 }
5284
5285 DEFUN_YANG (no_match_lcommunity,
5286 no_match_lcommunity_cmd,
5287 "no match large-community [<(1-99)|(100-500)|LCOMMUNITY_LIST_NAME> [exact-match]]",
5288 NO_STR
5289 MATCH_STR
5290 "Match BGP large community list\n"
5291 "Large Community-list number (standard)\n"
5292 "Large Community-list number (expanded)\n"
5293 "Large Community-list name\n"
5294 "Do exact matching of communities\n")
5295 {
5296 const char *xpath =
5297 "./match-condition[condition='frr-bgp-route-map:match-large-community']";
5298
5299 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5300 return nb_cli_apply_changes(vty, NULL);
5301 }
5302
5303 DEFPY_YANG (match_ecommunity,
5304 match_ecommunity_cmd,
5305 "match extcommunity <(1-99)|(100-500)|EXTCOMMUNITY_LIST_NAME>",
5306 MATCH_STR
5307 "Match BGP/VPN extended community list\n"
5308 "Extended community-list number (standard)\n"
5309 "Extended community-list number (expanded)\n"
5310 "Extended community-list name\n")
5311 {
5312 const char *xpath =
5313 "./match-condition[condition='frr-bgp-route-map:match-extcommunity']";
5314 char xpath_value[XPATH_MAXLEN];
5315 int idx_comm_list = 2;
5316
5317 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5318
5319 snprintf(
5320 xpath_value, sizeof(xpath_value),
5321 "%s/rmap-match-condition/frr-bgp-route-map:comm-list/comm-list-name",
5322 xpath);
5323 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[idx_comm_list]->arg);
5324
5325 return nb_cli_apply_changes(vty, NULL);
5326 }
5327
5328
5329 DEFUN_YANG (no_match_ecommunity,
5330 no_match_ecommunity_cmd,
5331 "no match extcommunity [<(1-99)|(100-500)|EXTCOMMUNITY_LIST_NAME>]",
5332 NO_STR
5333 MATCH_STR
5334 "Match BGP/VPN extended community list\n"
5335 "Extended community-list number (standard)\n"
5336 "Extended community-list number (expanded)\n"
5337 "Extended community-list name\n")
5338 {
5339 const char *xpath =
5340 "./match-condition[condition='frr-bgp-route-map:match-extcommunity']";
5341
5342 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5343 return nb_cli_apply_changes(vty, NULL);
5344 }
5345
5346
5347 DEFUN_YANG (match_aspath,
5348 match_aspath_cmd,
5349 "match as-path AS_PATH_FILTER_NAME",
5350 MATCH_STR
5351 "Match BGP AS path list\n"
5352 "AS path access-list name\n")
5353 {
5354 int idx_word = 2;
5355
5356 const char *xpath =
5357 "./match-condition[condition='frr-bgp-route-map:as-path-list']";
5358 char xpath_value[XPATH_MAXLEN];
5359
5360 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5361 snprintf(xpath_value, sizeof(xpath_value),
5362 "%s/rmap-match-condition/frr-bgp-route-map:list-name", xpath);
5363 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5364 argv[idx_word]->arg);
5365
5366 return nb_cli_apply_changes(vty, NULL);
5367 }
5368
5369
5370 DEFUN_YANG (no_match_aspath,
5371 no_match_aspath_cmd,
5372 "no match as-path [AS_PATH_FILTER_NAME]",
5373 NO_STR
5374 MATCH_STR
5375 "Match BGP AS path list\n"
5376 "AS path access-list name\n")
5377 {
5378 const char *xpath =
5379 "./match-condition[condition='frr-bgp-route-map:as-path-list']";
5380
5381 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5382
5383 return nb_cli_apply_changes(vty, NULL);
5384 }
5385
5386 DEFUN_YANG (match_origin,
5387 match_origin_cmd,
5388 "match origin <egp|igp|incomplete>",
5389 MATCH_STR
5390 "BGP origin code\n"
5391 "remote EGP\n"
5392 "local IGP\n"
5393 "unknown heritage\n")
5394 {
5395 int idx_origin = 2;
5396 const char *origin_type;
5397 const char *xpath =
5398 "./match-condition[condition='frr-bgp-route-map:match-origin']";
5399 char xpath_value[XPATH_MAXLEN];
5400
5401 if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
5402 origin_type = "igp";
5403 else if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
5404 origin_type = "egp";
5405 else if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
5406 origin_type = "incomplete";
5407 else {
5408 vty_out(vty, "%% Invalid match origin type\n");
5409 return CMD_WARNING_CONFIG_FAILED;
5410 }
5411
5412 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5413 snprintf(xpath_value, sizeof(xpath_value),
5414 "%s/rmap-match-condition/frr-bgp-route-map:origin", xpath);
5415 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, origin_type);
5416
5417 return nb_cli_apply_changes(vty, NULL);
5418 }
5419
5420
5421 DEFUN_YANG (no_match_origin,
5422 no_match_origin_cmd,
5423 "no match origin [<egp|igp|incomplete>]",
5424 NO_STR
5425 MATCH_STR
5426 "BGP origin code\n"
5427 "remote EGP\n"
5428 "local IGP\n"
5429 "unknown heritage\n")
5430 {
5431 const char *xpath =
5432 "./match-condition[condition='frr-bgp-route-map:match-origin']";
5433 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5434 return nb_cli_apply_changes(vty, NULL);
5435 }
5436
5437 DEFUN_YANG (set_table_id,
5438 set_table_id_cmd,
5439 "set table (1-4294967295)",
5440 SET_STR
5441 "export route to non-main kernel table\n"
5442 "Kernel routing table id\n")
5443 {
5444 int idx_number = 2;
5445 const char *xpath = "./set-action[action='frr-bgp-route-map:table']";
5446 char xpath_value[XPATH_MAXLEN];
5447
5448 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5449 snprintf(xpath_value, sizeof(xpath_value),
5450 "%s/rmap-set-action/frr-bgp-route-map:table", xpath);
5451 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5452 argv[idx_number]->arg);
5453 return nb_cli_apply_changes(vty, NULL);
5454 }
5455
5456 DEFUN_YANG (no_set_table_id,
5457 no_set_table_id_cmd,
5458 "no set table",
5459 NO_STR
5460 SET_STR
5461 "export route to non-main kernel table\n")
5462 {
5463 const char *xpath = "./set-action[action='frr-bgp-route-map:table']";
5464 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5465 return nb_cli_apply_changes(vty, NULL);
5466 }
5467
5468 DEFUN_YANG (set_ip_nexthop_peer,
5469 set_ip_nexthop_peer_cmd,
5470 "[no] set ip next-hop peer-address",
5471 NO_STR
5472 SET_STR
5473 IP_STR
5474 "Next hop address\n"
5475 "Use peer address (for BGP only)\n")
5476 {
5477 char xpath_value[XPATH_MAXLEN];
5478 const char *xpath =
5479 "./set-action[action='frr-bgp-route-map:set-ipv4-nexthop']";
5480
5481 if (strmatch(argv[0]->text, "no"))
5482 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5483 else {
5484 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5485 snprintf(xpath_value, sizeof(xpath_value),
5486 "%s/rmap-set-action/frr-bgp-route-map:ipv4-nexthop",
5487 xpath);
5488 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5489 "peer-address");
5490 }
5491 return nb_cli_apply_changes(vty, NULL);
5492 }
5493
5494 DEFUN_YANG (set_ip_nexthop_unchanged,
5495 set_ip_nexthop_unchanged_cmd,
5496 "[no] set ip next-hop unchanged",
5497 NO_STR
5498 SET_STR
5499 IP_STR
5500 "Next hop address\n"
5501 "Don't modify existing Next hop address\n")
5502 {
5503 char xpath_value[XPATH_MAXLEN];
5504 const char *xpath =
5505 "./set-action[action='frr-bgp-route-map:set-ipv4-nexthop']";
5506
5507 if (strmatch(argv[0]->text, "no"))
5508 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5509 else {
5510 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5511 snprintf(xpath_value, sizeof(xpath_value),
5512 "%s/rmap-set-action/frr-bgp-route-map:ipv4-nexthop",
5513 xpath);
5514 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5515 "unchanged");
5516 }
5517 return nb_cli_apply_changes(vty, NULL);
5518 }
5519
5520 DEFUN_YANG (set_distance,
5521 set_distance_cmd,
5522 "set distance (1-255)",
5523 SET_STR
5524 "BGP Administrative Distance to use\n"
5525 "Distance value\n")
5526 {
5527 int idx_number = 2;
5528 const char *xpath = "./set-action[action='frr-bgp-route-map:distance']";
5529 char xpath_value[XPATH_MAXLEN];
5530
5531 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5532 snprintf(xpath_value, sizeof(xpath_value),
5533 "%s/rmap-set-action/frr-bgp-route-map:distance", xpath);
5534 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5535 argv[idx_number]->arg);
5536 return nb_cli_apply_changes(vty, NULL);
5537 }
5538
5539 DEFUN_YANG (no_set_distance,
5540 no_set_distance_cmd,
5541 "no set distance [(1-255)]",
5542 NO_STR SET_STR
5543 "BGP Administrative Distance to use\n"
5544 "Distance value\n")
5545 {
5546 const char *xpath = "./set-action[action='frr-bgp-route-map:distance']";
5547
5548 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5549 return nb_cli_apply_changes(vty, NULL);
5550 }
5551
5552 DEFPY_YANG(set_l3vpn_nexthop_encapsulation, set_l3vpn_nexthop_encapsulation_cmd,
5553 "[no] set l3vpn next-hop encapsulation gre",
5554 NO_STR SET_STR
5555 "L3VPN operations\n"
5556 "Next hop Information\n"
5557 "Encapsulation options (for BGP only)\n"
5558 "Accept L3VPN traffic over GRE encapsulation\n")
5559 {
5560 const char *xpath =
5561 "./set-action[action='frr-bgp-route-map:set-l3vpn-nexthop-encapsulation']";
5562 const char *xpath_value =
5563 "./set-action[action='frr-bgp-route-map:set-l3vpn-nexthop-encapsulation']/rmap-set-action/frr-bgp-route-map:l3vpn-nexthop-encapsulation";
5564 enum nb_operation operation;
5565
5566 if (no)
5567 operation = NB_OP_DESTROY;
5568 else
5569 operation = NB_OP_CREATE;
5570
5571 nb_cli_enqueue_change(vty, xpath, operation, NULL);
5572 if (operation == NB_OP_DESTROY)
5573 return nb_cli_apply_changes(vty, NULL);
5574
5575 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "gre");
5576
5577 return nb_cli_apply_changes(vty, NULL);
5578 }
5579
5580 DEFUN_YANG (set_local_pref,
5581 set_local_pref_cmd,
5582 "set local-preference WORD",
5583 SET_STR
5584 "BGP local preference path attribute\n"
5585 "Preference value (0-4294967295)\n")
5586 {
5587 int idx_number = 2;
5588 const char *xpath =
5589 "./set-action[action='frr-bgp-route-map:set-local-preference']";
5590 char xpath_value[XPATH_MAXLEN];
5591
5592 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5593 snprintf(xpath_value, sizeof(xpath_value),
5594 "%s/rmap-set-action/frr-bgp-route-map:local-pref", xpath);
5595 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5596 argv[idx_number]->arg);
5597 return nb_cli_apply_changes(vty, NULL);
5598 }
5599
5600 DEFUN_YANG (no_set_local_pref,
5601 no_set_local_pref_cmd,
5602 "no set local-preference [WORD]",
5603 NO_STR
5604 SET_STR
5605 "BGP local preference path attribute\n"
5606 "Preference value (0-4294967295)\n")
5607 {
5608 const char *xpath =
5609 "./set-action[action='frr-bgp-route-map:set-local-preference']";
5610
5611 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5612 return nb_cli_apply_changes(vty, NULL);
5613 }
5614
5615 DEFUN_YANG (set_weight,
5616 set_weight_cmd,
5617 "set weight (0-4294967295)",
5618 SET_STR
5619 "BGP weight for routing table\n"
5620 "Weight value\n")
5621 {
5622 int idx_number = 2;
5623 const char *xpath = "./set-action[action='frr-bgp-route-map:weight']";
5624 char xpath_value[XPATH_MAXLEN];
5625
5626 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5627 snprintf(xpath_value, sizeof(xpath_value),
5628 "%s/rmap-set-action/frr-bgp-route-map:weight", xpath);
5629 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5630 argv[idx_number]->arg);
5631 return nb_cli_apply_changes(vty, NULL);
5632 }
5633
5634 DEFUN_YANG (no_set_weight,
5635 no_set_weight_cmd,
5636 "no set weight [(0-4294967295)]",
5637 NO_STR
5638 SET_STR
5639 "BGP weight for routing table\n"
5640 "Weight value\n")
5641 {
5642 const char *xpath = "./set-action[action='frr-bgp-route-map:weight']";
5643
5644 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5645 return nb_cli_apply_changes(vty, NULL);
5646 }
5647
5648 DEFUN_YANG (set_label_index,
5649 set_label_index_cmd,
5650 "set label-index (0-1048560)",
5651 SET_STR
5652 "Label index to associate with the prefix\n"
5653 "Label index value\n")
5654 {
5655 int idx_number = 2;
5656 const char *xpath =
5657 "./set-action[action='frr-bgp-route-map:label-index']";
5658 char xpath_value[XPATH_MAXLEN];
5659
5660 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5661 snprintf(xpath_value, sizeof(xpath_value),
5662 "%s/rmap-set-action/frr-bgp-route-map:label-index", xpath);
5663 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5664 argv[idx_number]->arg);
5665 return nb_cli_apply_changes(vty, NULL);
5666 }
5667
5668 DEFUN_YANG (no_set_label_index,
5669 no_set_label_index_cmd,
5670 "no set label-index [(0-1048560)]",
5671 NO_STR
5672 SET_STR
5673 "Label index to associate with the prefix\n"
5674 "Label index value\n")
5675 {
5676 const char *xpath =
5677 "./set-action[action='frr-bgp-route-map:label-index']";
5678
5679 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5680 return nb_cli_apply_changes(vty, NULL);
5681 }
5682
5683 DEFUN_YANG (set_aspath_prepend_asn,
5684 set_aspath_prepend_asn_cmd,
5685 "set as-path prepend ASNUM...",
5686 SET_STR
5687 "Transform BGP AS_PATH attribute\n"
5688 "Prepend to the as-path\n"
5689 AS_STR)
5690 {
5691 int idx_asn = 3;
5692 int ret;
5693 char *str;
5694 struct aspath *aspath;
5695
5696 str = argv_concat(argv, argc, idx_asn);
5697
5698 const char *xpath =
5699 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
5700 char xpath_value[XPATH_MAXLEN];
5701
5702 aspath = route_aspath_compile(str);
5703 if (!aspath) {
5704 vty_out(vty, "%% Invalid AS path value %s\n", str);
5705 return CMD_WARNING_CONFIG_FAILED;
5706 }
5707 route_aspath_free(aspath);
5708 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5709 snprintf(xpath_value, sizeof(xpath_value),
5710 "%s/rmap-set-action/frr-bgp-route-map:prepend-as-path", xpath);
5711 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
5712 ret = nb_cli_apply_changes(vty, NULL);
5713 XFREE(MTYPE_TMP, str);
5714 return ret;
5715 }
5716
5717 DEFUN_YANG (set_aspath_prepend_lastas,
5718 set_aspath_prepend_lastas_cmd,
5719 "set as-path prepend last-as (1-10)",
5720 SET_STR
5721 "Transform BGP AS_PATH attribute\n"
5722 "Prepend to the as-path\n"
5723 "Use the last AS-number in the as-path\n"
5724 "Number of times to insert\n")
5725 {
5726 int idx_num = 4;
5727
5728 const char *xpath =
5729 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
5730 char xpath_value[XPATH_MAXLEN];
5731
5732 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5733 snprintf(xpath_value, sizeof(xpath_value),
5734 "%s/rmap-set-action/frr-bgp-route-map:last-as", xpath);
5735 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
5736 argv[idx_num]->arg);
5737 return nb_cli_apply_changes(vty, NULL);
5738 }
5739
5740 DEFPY_YANG (set_aspath_replace_asn,
5741 set_aspath_replace_asn_cmd,
5742 "set as-path replace <any|ASNUM>$replace",
5743 SET_STR
5744 "Transform BGP AS_PATH attribute\n"
5745 "Replace AS number to local AS number\n"
5746 "Replace any AS number to local AS number\n"
5747 "Replace a specific AS number in plain or dotted format to local AS number\n")
5748 {
5749 const char *xpath =
5750 "./set-action[action='frr-bgp-route-map:as-path-replace']";
5751 char xpath_value[XPATH_MAXLEN];
5752 as_t as_value;
5753
5754 if (!strmatch(replace, "any") && !asn_str2asn(replace, &as_value)) {
5755 vty_out(vty, "%% Invalid AS value %s\n", replace);
5756 return CMD_WARNING_CONFIG_FAILED;
5757 }
5758
5759 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5760 snprintf(xpath_value, sizeof(xpath_value),
5761 "%s/rmap-set-action/frr-bgp-route-map:replace-as-path", xpath);
5762 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, replace);
5763 return nb_cli_apply_changes(vty, NULL);
5764 }
5765
5766 DEFPY_YANG (no_set_aspath_replace_asn,
5767 no_set_aspath_replace_asn_cmd,
5768 "no set as-path replace [<any|ASNUM>]",
5769 NO_STR
5770 SET_STR
5771 "Transform BGP AS_PATH attribute\n"
5772 "Replace AS number to local AS number\n"
5773 "Replace any AS number to local AS number\n"
5774 "Replace a specific AS number in plain or dotted format to local AS number\n")
5775 {
5776 const char *xpath =
5777 "./set-action[action='frr-bgp-route-map:as-path-replace']";
5778
5779 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5780 return nb_cli_apply_changes(vty, NULL);
5781 }
5782
5783 DEFUN_YANG (no_set_aspath_prepend,
5784 no_set_aspath_prepend_cmd,
5785 "no set as-path prepend [ASNUM]",
5786 NO_STR
5787 SET_STR
5788 "Transform BGP AS_PATH attribute\n"
5789 "Prepend to the as-path\n"
5790 AS_STR)
5791 {
5792 const char *xpath =
5793 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
5794
5795 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5796 return nb_cli_apply_changes(vty, NULL);
5797 }
5798
5799 DEFUN_YANG (no_set_aspath_prepend_lastas,
5800 no_set_aspath_prepend_lastas_cmd,
5801 "no set as-path prepend last-as [(1-10)]",
5802 NO_STR
5803 SET_STR
5804 "Transform BGP AS_PATH attribute\n"
5805 "Prepend to the as-path\n"
5806 "Use the peers AS-number\n"
5807 "Number of times to insert\n")
5808 {
5809 const char *xpath =
5810 "./set-action[action='frr-bgp-route-map:as-path-prepend']";
5811
5812 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5813 return nb_cli_apply_changes(vty, NULL);
5814 }
5815
5816 DEFUN_YANG (set_aspath_exclude,
5817 set_aspath_exclude_cmd,
5818 "set as-path exclude ASNUM...",
5819 SET_STR
5820 "Transform BGP AS-path attribute\n"
5821 "Exclude from the as-path\n"
5822 AS_STR)
5823 {
5824 int idx_asn = 3;
5825 int ret;
5826 char *str;
5827 struct aspath *aspath;
5828
5829 str = argv_concat(argv, argc, idx_asn);
5830
5831 const char *xpath =
5832 "./set-action[action='frr-bgp-route-map:as-path-exclude']";
5833 char xpath_value[XPATH_MAXLEN];
5834
5835 aspath = route_aspath_compile(str);
5836 if (!aspath) {
5837 vty_out(vty, "%% Invalid AS path value %s\n", str);
5838 return CMD_WARNING_CONFIG_FAILED;
5839 }
5840 route_aspath_free(aspath);
5841 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5842 snprintf(xpath_value, sizeof(xpath_value),
5843 "%s/rmap-set-action/frr-bgp-route-map:exclude-as-path", xpath);
5844 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
5845 ret = nb_cli_apply_changes(vty, NULL);
5846 XFREE(MTYPE_TMP, str);
5847 return ret;
5848 }
5849
5850 DEFUN_YANG (no_set_aspath_exclude,
5851 no_set_aspath_exclude_cmd,
5852 "no set as-path exclude ASNUM...",
5853 NO_STR
5854 SET_STR
5855 "Transform BGP AS_PATH attribute\n"
5856 "Exclude from the as-path\n"
5857 "AS number\n")
5858 {
5859 const char *xpath =
5860 "./set-action[action='frr-bgp-route-map:as-path-exclude']";
5861
5862 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
5863 return nb_cli_apply_changes(vty, NULL);
5864 }
5865
5866 ALIAS_YANG (no_set_aspath_exclude, no_set_aspath_exclude_all_cmd,
5867 "no set as-path exclude",
5868 NO_STR SET_STR
5869 "Transform BGP AS_PATH attribute\n"
5870 "Exclude from the as-path\n")
5871
5872 DEFUN_YANG (set_community,
5873 set_community_cmd,
5874 "set community AA:NN...",
5875 SET_STR
5876 "BGP community attribute\n"
5877 COMMUNITY_VAL_STR)
5878 {
5879 int idx_aa_nn = 2;
5880 int i;
5881 int first = 0;
5882 int additive = 0;
5883 struct buffer *b;
5884 struct community *com = NULL;
5885 char *str;
5886 char *argstr = NULL;
5887 int ret;
5888
5889 const char *xpath =
5890 "./set-action[action='frr-bgp-route-map:set-community']";
5891 char xpath_value[XPATH_MAXLEN];
5892
5893 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
5894 snprintf(xpath_value, sizeof(xpath_value),
5895 "%s/rmap-set-action/frr-bgp-route-map:community-string",
5896 xpath);
5897
5898 b = buffer_new(1024);
5899
5900 for (i = idx_aa_nn; i < argc; i++) {
5901 if (strncmp(argv[i]->arg, "additive", strlen(argv[i]->arg))
5902 == 0) {
5903 additive = 1;
5904 continue;
5905 }
5906
5907 if (first)
5908 buffer_putc(b, ' ');
5909 else
5910 first = 1;
5911
5912 #if CONFDATE > 20230801
5913 CPP_NOTICE("Deprecate COMMUNITY_INTERNET BGP community")
5914 #endif
5915 if (strncmp(argv[i]->arg, "internet", strlen(argv[i]->arg))
5916 == 0) {
5917 buffer_putstr(b, "internet");
5918 vty_out(vty, "%% `internet` community is deprecated\n");
5919 zlog_warn("`internet` community is deprecated");
5920 continue;
5921 }
5922 if (strncmp(argv[i]->arg, "local-AS", strlen(argv[i]->arg))
5923 == 0) {
5924 buffer_putstr(b, "local-AS");
5925 continue;
5926 }
5927 if (strncmp(argv[i]->arg, "no-a", strlen("no-a")) == 0
5928 && strncmp(argv[i]->arg, "no-advertise",
5929 strlen(argv[i]->arg))
5930 == 0) {
5931 buffer_putstr(b, "no-advertise");
5932 continue;
5933 }
5934 if (strncmp(argv[i]->arg, "no-e", strlen("no-e")) == 0
5935 && strncmp(argv[i]->arg, "no-export", strlen(argv[i]->arg))
5936 == 0) {
5937 buffer_putstr(b, "no-export");
5938 continue;
5939 }
5940 if (strncmp(argv[i]->arg, "blackhole", strlen(argv[i]->arg))
5941 == 0) {
5942 buffer_putstr(b, "blackhole");
5943 continue;
5944 }
5945 if (strncmp(argv[i]->arg, "graceful-shutdown",
5946 strlen(argv[i]->arg))
5947 == 0) {
5948 buffer_putstr(b, "graceful-shutdown");
5949 continue;
5950 }
5951 buffer_putstr(b, argv[i]->arg);
5952 }
5953 buffer_putc(b, '\0');
5954
5955 /* Fetch result string then compile it to communities attribute. */
5956 str = buffer_getstr(b);
5957 buffer_free(b);
5958
5959 if (str)
5960 com = community_str2com(str);
5961
5962 /* Can't compile user input into communities attribute. */
5963 if (!com) {
5964 vty_out(vty, "%% Malformed communities attribute '%s'\n", str);
5965 XFREE(MTYPE_TMP, str);
5966 return CMD_WARNING_CONFIG_FAILED;
5967 }
5968 XFREE(MTYPE_TMP, str);
5969
5970 /* Set communites attribute string. */
5971 str = community_str(com, false, false);
5972
5973 if (additive) {
5974 size_t argstr_sz = strlen(str) + strlen(" additive") + 1;
5975 argstr = XCALLOC(MTYPE_TMP, argstr_sz);
5976 strlcpy(argstr, str, argstr_sz);
5977 strlcat(argstr, " additive", argstr_sz);
5978 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argstr);
5979 } else
5980 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
5981
5982 ret = nb_cli_apply_changes(vty, NULL);
5983
5984 if (argstr)
5985 XFREE(MTYPE_TMP, argstr);
5986 community_free(&com);
5987
5988 return ret;
5989 }
5990
5991 DEFUN_YANG (set_community_none,
5992 set_community_none_cmd,
5993 "set community none",
5994 SET_STR
5995 "BGP community attribute\n"
5996 "No community attribute\n")
5997 {
5998 const char *xpath =
5999 "./set-action[action='frr-bgp-route-map:set-community']";
6000 char xpath_value[XPATH_MAXLEN];
6001
6002 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6003
6004 snprintf(xpath_value, sizeof(xpath_value),
6005 "%s/rmap-set-action/frr-bgp-route-map:community-none", xpath);
6006 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
6007 return nb_cli_apply_changes(vty, NULL);
6008 }
6009
6010 DEFUN_YANG (no_set_community,
6011 no_set_community_cmd,
6012 "no set community AA:NN...",
6013 NO_STR
6014 SET_STR
6015 "BGP community attribute\n"
6016 COMMUNITY_VAL_STR)
6017 {
6018 const char *xpath =
6019 "./set-action[action='frr-bgp-route-map:set-community']";
6020
6021 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6022 return nb_cli_apply_changes(vty, NULL);
6023 }
6024
6025 ALIAS_YANG (no_set_community,
6026 no_set_community_short_cmd,
6027 "no set community",
6028 NO_STR
6029 SET_STR
6030 "BGP community attribute\n")
6031
6032 DEFPY_YANG (set_community_delete,
6033 set_community_delete_cmd,
6034 "set comm-list <(1-99)|(100-500)|COMMUNITY_LIST_NAME> delete",
6035 SET_STR
6036 "set BGP community list (for deletion)\n"
6037 "Community-list number (standard)\n"
6038 "Community-list number (expanded)\n"
6039 "Community-list name\n"
6040 "Delete matching communities\n")
6041 {
6042 const char *xpath =
6043 "./set-action[action='frr-bgp-route-map:comm-list-delete']";
6044 char xpath_value[XPATH_MAXLEN];
6045 int idx_comm_list = 2;
6046
6047 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6048
6049 snprintf(xpath_value, sizeof(xpath_value),
6050 "%s/rmap-set-action/frr-bgp-route-map:comm-list-name",
6051 xpath);
6052 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6053 argv[idx_comm_list]->arg);
6054
6055 return nb_cli_apply_changes(vty, NULL);
6056
6057 }
6058
6059 DEFUN_YANG (no_set_community_delete,
6060 no_set_community_delete_cmd,
6061 "no set comm-list [<(1-99)|(100-500)|COMMUNITY_LIST_NAME> delete]",
6062 NO_STR
6063 SET_STR
6064 "set BGP community list (for deletion)\n"
6065 "Community-list number (standard)\n"
6066 "Community-list number (expanded)\n"
6067 "Community-list name\n"
6068 "Delete matching communities\n")
6069 {
6070 const char *xpath =
6071 "./set-action[action='frr-bgp-route-map:comm-list-delete']";
6072
6073 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6074 return nb_cli_apply_changes(vty, NULL);
6075 }
6076
6077 DEFUN_YANG (set_lcommunity,
6078 set_lcommunity_cmd,
6079 "set large-community AA:BB:CC...",
6080 SET_STR
6081 "BGP large community attribute\n"
6082 "Large Community number in aa:bb:cc format or additive\n")
6083 {
6084 char *str;
6085 int ret;
6086 const char *xpath =
6087 "./set-action[action='frr-bgp-route-map:set-large-community']";
6088 char xpath_value[XPATH_MAXLEN];
6089
6090 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6091
6092 snprintf(xpath_value, sizeof(xpath_value),
6093 "%s/rmap-set-action/frr-bgp-route-map:large-community-string",
6094 xpath);
6095 str = argv_concat(argv, argc, 2);
6096 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
6097 ret = nb_cli_apply_changes(vty, NULL);
6098 XFREE(MTYPE_TMP, str);
6099 return ret;
6100 }
6101
6102 DEFUN_YANG (set_lcommunity_none,
6103 set_lcommunity_none_cmd,
6104 "set large-community none",
6105 SET_STR
6106 "BGP large community attribute\n"
6107 "No large community attribute\n")
6108 {
6109 const char *xpath =
6110 "./set-action[action='frr-bgp-route-map:set-large-community']";
6111 char xpath_value[XPATH_MAXLEN];
6112
6113 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6114
6115 snprintf(xpath_value, sizeof(xpath_value),
6116 "%s/rmap-set-action/frr-bgp-route-map:large-community-none",
6117 xpath);
6118 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
6119 return nb_cli_apply_changes(vty, NULL);
6120 }
6121
6122 DEFUN_YANG (no_set_lcommunity,
6123 no_set_lcommunity_cmd,
6124 "no set large-community none",
6125 NO_STR
6126 SET_STR
6127 "BGP large community attribute\n"
6128 "No community attribute\n")
6129 {
6130 const char *xpath =
6131 "./set-action[action='frr-bgp-route-map:set-large-community']";
6132
6133 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6134 return nb_cli_apply_changes(vty, NULL);
6135 }
6136
6137 DEFUN_YANG (no_set_lcommunity1,
6138 no_set_lcommunity1_cmd,
6139 "no set large-community AA:BB:CC...",
6140 NO_STR
6141 SET_STR
6142 "BGP large community attribute\n"
6143 "Large community in AA:BB:CC... format or additive\n")
6144 {
6145 const char *xpath =
6146 "./set-action[action='frr-bgp-route-map:set-large-community']";
6147
6148 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6149 return nb_cli_apply_changes(vty, NULL);
6150 }
6151
6152 ALIAS_YANG (no_set_lcommunity1,
6153 no_set_lcommunity1_short_cmd,
6154 "no set large-community",
6155 NO_STR
6156 SET_STR
6157 "BGP large community attribute\n")
6158
6159 DEFPY_YANG (set_lcommunity_delete,
6160 set_lcommunity_delete_cmd,
6161 "set large-comm-list <(1-99)|(100-500)|LCOMMUNITY_LIST_NAME> delete",
6162 SET_STR
6163 "set BGP large community list (for deletion)\n"
6164 "Large Community-list number (standard)\n"
6165 "Large Communitly-list number (expanded)\n"
6166 "Large Community-list name\n"
6167 "Delete matching large communities\n")
6168 {
6169 const char *xpath =
6170 "./set-action[action='frr-bgp-route-map:large-comm-list-delete']";
6171 char xpath_value[XPATH_MAXLEN];
6172 int idx_lcomm_list = 2;
6173
6174 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6175
6176 snprintf(xpath_value, sizeof(xpath_value),
6177 "%s/rmap-set-action/frr-bgp-route-map:comm-list-name",
6178 xpath);
6179 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6180 argv[idx_lcomm_list]->arg);
6181
6182 return nb_cli_apply_changes(vty, NULL);
6183 }
6184
6185 DEFUN_YANG (no_set_lcommunity_delete,
6186 no_set_lcommunity_delete_cmd,
6187 "no set large-comm-list <(1-99)|(100-500)|LCOMMUNITY_LIST_NAME> [delete]",
6188 NO_STR
6189 SET_STR
6190 "set BGP large community list (for deletion)\n"
6191 "Large Community-list number (standard)\n"
6192 "Large Communitly-list number (expanded)\n"
6193 "Large Community-list name\n"
6194 "Delete matching large communities\n")
6195 {
6196 const char *xpath =
6197 "./set-action[action='frr-bgp-route-map:large-comm-list-delete']";
6198
6199 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6200 return nb_cli_apply_changes(vty, NULL);
6201 }
6202
6203 ALIAS_YANG (no_set_lcommunity_delete,
6204 no_set_lcommunity_delete_short_cmd,
6205 "no set large-comm-list",
6206 NO_STR
6207 SET_STR
6208 "set BGP large community list (for deletion)\n")
6209
6210 DEFUN_YANG (set_ecommunity_rt,
6211 set_ecommunity_rt_cmd,
6212 "set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
6213 SET_STR
6214 "BGP extended community attribute\n"
6215 "Route Target extended community\n"
6216 "VPN extended community\n")
6217 {
6218 int idx_asn_nn = 3;
6219 char *str;
6220 int ret;
6221 const char *xpath =
6222 "./set-action[action='frr-bgp-route-map:set-extcommunity-rt']";
6223 char xpath_value[XPATH_MAXLEN];
6224
6225 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6226
6227 snprintf(xpath_value, sizeof(xpath_value),
6228 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-rt", xpath);
6229 str = argv_concat(argv, argc, idx_asn_nn);
6230 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
6231 ret = nb_cli_apply_changes(vty, NULL);
6232 XFREE(MTYPE_TMP, str);
6233 return ret;
6234 }
6235
6236 DEFUN_YANG (no_set_ecommunity_rt,
6237 no_set_ecommunity_rt_cmd,
6238 "no set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
6239 NO_STR
6240 SET_STR
6241 "BGP extended community attribute\n"
6242 "Route Target extended community\n"
6243 "VPN extended community\n")
6244 {
6245 const char *xpath =
6246 "./set-action[action='frr-bgp-route-map:set-extcommunity-rt']";
6247 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6248 return nb_cli_apply_changes(vty, NULL);
6249 }
6250
6251 ALIAS_YANG (no_set_ecommunity_rt,
6252 no_set_ecommunity_rt_short_cmd,
6253 "no set extcommunity rt",
6254 NO_STR
6255 SET_STR
6256 "BGP extended community attribute\n"
6257 "Route Target extended community\n")
6258
6259 DEFUN_YANG (set_ecommunity_soo,
6260 set_ecommunity_soo_cmd,
6261 "set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
6262 SET_STR
6263 "BGP extended community attribute\n"
6264 "Site-of-Origin extended community\n"
6265 "VPN extended community\n")
6266 {
6267 int idx_asn_nn = 3;
6268 char *str;
6269 int ret;
6270 const char *xpath =
6271 "./set-action[action='frr-bgp-route-map:set-extcommunity-soo']";
6272 char xpath_value[XPATH_MAXLEN];
6273
6274 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6275
6276 snprintf(xpath_value, sizeof(xpath_value),
6277 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-soo",
6278 xpath);
6279 str = argv_concat(argv, argc, idx_asn_nn);
6280 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, str);
6281 ret = nb_cli_apply_changes(vty, NULL);
6282 XFREE(MTYPE_TMP, str);
6283 return ret;
6284 }
6285
6286 DEFUN_YANG (no_set_ecommunity_soo,
6287 no_set_ecommunity_soo_cmd,
6288 "no set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
6289 NO_STR
6290 SET_STR
6291 "BGP extended community attribute\n"
6292 "Site-of-Origin extended community\n"
6293 "VPN extended community\n")
6294 {
6295 const char *xpath =
6296 "./set-action[action='frr-bgp-route-map:set-extcommunity-soo']";
6297 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6298 return nb_cli_apply_changes(vty, NULL);
6299 }
6300
6301 ALIAS_YANG (no_set_ecommunity_soo,
6302 no_set_ecommunity_soo_short_cmd,
6303 "no set extcommunity soo",
6304 NO_STR
6305 SET_STR
6306 "GP extended community attribute\n"
6307 "Site-of-Origin extended community\n")
6308
6309 DEFUN_YANG(set_ecommunity_none, set_ecommunity_none_cmd,
6310 "set extcommunity none",
6311 SET_STR
6312 "BGP extended community attribute\n"
6313 "No extended community attribute\n")
6314 {
6315 const char *xpath =
6316 "./set-action[action='frr-bgp-route-map:set-extcommunity-none']";
6317 char xpath_value[XPATH_MAXLEN];
6318
6319 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6320
6321 snprintf(xpath_value, sizeof(xpath_value),
6322 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-none",
6323 xpath);
6324 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
6325 return nb_cli_apply_changes(vty, NULL);
6326 }
6327
6328 DEFUN_YANG(no_set_ecommunity_none, no_set_ecommunity_none_cmd,
6329 "no set extcommunity none",
6330 NO_STR SET_STR
6331 "BGP extended community attribute\n"
6332 "No extended community attribute\n")
6333 {
6334 const char *xpath =
6335 "./set-action[action='frr-bgp-route-map:set-extcommunity-none']";
6336 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6337 return nb_cli_apply_changes(vty, NULL);
6338 }
6339
6340 DEFUN_YANG (set_ecommunity_lb,
6341 set_ecommunity_lb_cmd,
6342 "set extcommunity bandwidth <(1-25600)|cumulative|num-multipaths> [non-transitive]",
6343 SET_STR
6344 "BGP extended community attribute\n"
6345 "Link bandwidth extended community\n"
6346 "Bandwidth value in Mbps\n"
6347 "Cumulative bandwidth of all multipaths (outbound-only)\n"
6348 "Internally computed bandwidth based on number of multipaths (outbound-only)\n"
6349 "Attribute is set as non-transitive\n")
6350 {
6351 int idx_lb = 3;
6352 int idx_non_transitive = 0;
6353 const char *xpath =
6354 "./set-action[action='frr-bgp-route-map:set-extcommunity-lb']";
6355 char xpath_lb_type[XPATH_MAXLEN];
6356 char xpath_bandwidth[XPATH_MAXLEN];
6357 char xpath_non_transitive[XPATH_MAXLEN];
6358
6359 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6360
6361 snprintf(xpath_lb_type, sizeof(xpath_lb_type),
6362 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-lb/lb-type",
6363 xpath);
6364 snprintf(xpath_bandwidth, sizeof(xpath_bandwidth),
6365 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-lb/bandwidth",
6366 xpath);
6367 snprintf(xpath_non_transitive, sizeof(xpath_non_transitive),
6368 "%s/rmap-set-action/frr-bgp-route-map:extcommunity-lb/two-octet-as-specific",
6369 xpath);
6370
6371 if ((strcmp(argv[idx_lb]->arg, "cumulative")) == 0)
6372 nb_cli_enqueue_change(vty, xpath_lb_type, NB_OP_MODIFY,
6373 "cumulative-bandwidth");
6374 else if ((strcmp(argv[idx_lb]->arg, "num-multipaths")) == 0)
6375 nb_cli_enqueue_change(vty, xpath_lb_type, NB_OP_MODIFY,
6376 "computed-bandwidth");
6377 else {
6378 nb_cli_enqueue_change(vty, xpath_lb_type, NB_OP_MODIFY,
6379 "explicit-bandwidth");
6380 nb_cli_enqueue_change(vty, xpath_bandwidth, NB_OP_MODIFY,
6381 argv[idx_lb]->arg);
6382 }
6383
6384 if (argv_find(argv, argc, "non-transitive", &idx_non_transitive))
6385 nb_cli_enqueue_change(vty, xpath_non_transitive, NB_OP_MODIFY,
6386 "true");
6387 else
6388 nb_cli_enqueue_change(vty, xpath_non_transitive, NB_OP_MODIFY,
6389 "false");
6390
6391 return nb_cli_apply_changes(vty, NULL);
6392 }
6393
6394 DEFUN_YANG (no_set_ecommunity_lb,
6395 no_set_ecommunity_lb_cmd,
6396 "no set extcommunity bandwidth <(1-25600)|cumulative|num-multipaths> [non-transitive]",
6397 NO_STR
6398 SET_STR
6399 "BGP extended community attribute\n"
6400 "Link bandwidth extended community\n"
6401 "Bandwidth value in Mbps\n"
6402 "Cumulative bandwidth of all multipaths (outbound-only)\n"
6403 "Internally computed bandwidth based on number of multipaths (outbound-only)\n"
6404 "Attribute is set as non-transitive\n")
6405 {
6406 const char *xpath =
6407 "./set-action[action='frr-bgp-route-map:set-extcommunity-lb']";
6408
6409 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6410 return nb_cli_apply_changes(vty, NULL);
6411 }
6412
6413 ALIAS_YANG (no_set_ecommunity_lb,
6414 no_set_ecommunity_lb_short_cmd,
6415 "no set extcommunity bandwidth",
6416 NO_STR
6417 SET_STR
6418 "BGP extended community attribute\n"
6419 "Link bandwidth extended community\n")
6420
6421 DEFUN_YANG (set_origin,
6422 set_origin_cmd,
6423 "set origin <egp|igp|incomplete>",
6424 SET_STR
6425 "BGP origin code\n"
6426 "remote EGP\n"
6427 "local IGP\n"
6428 "unknown heritage\n")
6429 {
6430 int idx_origin = 2;
6431 const char *origin_type;
6432 const char *xpath =
6433 "./set-action[action='frr-bgp-route-map:set-origin']";
6434 char xpath_value[XPATH_MAXLEN];
6435
6436 if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
6437 origin_type = "igp";
6438 else if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
6439 origin_type = "egp";
6440 else if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
6441 origin_type = "incomplete";
6442 else {
6443 vty_out(vty, "%% Invalid match origin type\n");
6444 return CMD_WARNING_CONFIG_FAILED;
6445 }
6446
6447 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6448 snprintf(xpath_value, sizeof(xpath_value),
6449 "%s/rmap-set-action/frr-bgp-route-map:origin", xpath);
6450 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, origin_type);
6451
6452 return nb_cli_apply_changes(vty, NULL);
6453 }
6454
6455 DEFUN_YANG (no_set_origin,
6456 no_set_origin_cmd,
6457 "no set origin [<egp|igp|incomplete>]",
6458 NO_STR
6459 SET_STR
6460 "BGP origin code\n"
6461 "remote EGP\n"
6462 "local IGP\n"
6463 "unknown heritage\n")
6464 {
6465 const char *xpath =
6466 "./set-action[action='frr-bgp-route-map:set-origin']";
6467
6468 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6469 return nb_cli_apply_changes(vty, NULL);
6470 }
6471
6472 DEFUN_YANG (set_atomic_aggregate,
6473 set_atomic_aggregate_cmd,
6474 "set atomic-aggregate",
6475 SET_STR
6476 "BGP atomic aggregate attribute\n" )
6477 {
6478 const char *xpath =
6479 "./set-action[action='frr-bgp-route-map:atomic-aggregate']";
6480 char xpath_value[XPATH_MAXLEN];
6481
6482 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6483 snprintf(xpath_value, sizeof(xpath_value),
6484 "%s/rmap-set-action/frr-bgp-route-map:atomic-aggregate",
6485 xpath);
6486 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, NULL);
6487
6488 return nb_cli_apply_changes(vty, NULL);
6489 }
6490
6491 DEFUN_YANG (no_set_atomic_aggregate,
6492 no_set_atomic_aggregate_cmd,
6493 "no set atomic-aggregate",
6494 NO_STR
6495 SET_STR
6496 "BGP atomic aggregate attribute\n" )
6497 {
6498 const char *xpath =
6499 "./set-action[action='frr-bgp-route-map:atomic-aggregate']";
6500
6501 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6502 return nb_cli_apply_changes(vty, NULL);
6503 }
6504
6505 DEFPY_YANG (set_aigp_metric,
6506 set_aigp_metric_cmd,
6507 "set aigp-metric <igp-metric|(1-4294967295)>$aigp_metric",
6508 SET_STR
6509 "BGP AIGP attribute (AIGP Metric TLV)\n"
6510 "AIGP Metric value from IGP protocol\n"
6511 "Manual AIGP Metric value\n")
6512 {
6513 const char *xpath =
6514 "./set-action[action='frr-bgp-route-map:aigp-metric']";
6515 char xpath_value[XPATH_MAXLEN];
6516
6517 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6518 snprintf(xpath_value, sizeof(xpath_value),
6519 "%s/rmap-set-action/frr-bgp-route-map:aigp-metric", xpath);
6520 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, aigp_metric);
6521
6522 return nb_cli_apply_changes(vty, NULL);
6523 }
6524
6525 DEFPY_YANG (no_set_aigp_metric,
6526 no_set_aigp_metric_cmd,
6527 "no set aigp-metric [<igp-metric|(1-4294967295)>]",
6528 NO_STR
6529 SET_STR
6530 "BGP AIGP attribute (AIGP Metric TLV)\n"
6531 "AIGP Metric value from IGP protocol\n"
6532 "Manual AIGP Metric value\n")
6533 {
6534 const char *xpath =
6535 "./set-action[action='frr-bgp-route-map:aigp-metric']";
6536
6537 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6538 return nb_cli_apply_changes(vty, NULL);
6539 }
6540
6541 DEFUN_YANG (set_aggregator_as,
6542 set_aggregator_as_cmd,
6543 "set aggregator as ASNUM A.B.C.D",
6544 SET_STR
6545 "BGP aggregator attribute\n"
6546 "AS number of aggregator\n"
6547 AS_STR
6548 "IP address of aggregator\n")
6549 {
6550 int idx_number = 3;
6551 int idx_ipv4 = 4;
6552 char xpath_asn[XPATH_MAXLEN];
6553 char xpath_addr[XPATH_MAXLEN];
6554 const char *xpath =
6555 "./set-action[action='frr-bgp-route-map:aggregator']";
6556 as_t as_value;
6557
6558 if (!asn_str2asn(argv[idx_number]->arg, &as_value)) {
6559 vty_out(vty, "%% Invalid AS value %s\n", argv[idx_number]->arg);
6560 return CMD_WARNING_CONFIG_FAILED;
6561 }
6562
6563 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6564
6565 snprintf(
6566 xpath_asn, sizeof(xpath_asn),
6567 "%s/rmap-set-action/frr-bgp-route-map:aggregator/aggregator-asn",
6568 xpath);
6569 nb_cli_enqueue_change(vty, xpath_asn, NB_OP_MODIFY,
6570 argv[idx_number]->arg);
6571
6572 snprintf(
6573 xpath_addr, sizeof(xpath_addr),
6574 "%s/rmap-set-action/frr-bgp-route-map:aggregator/aggregator-address",
6575 xpath);
6576 nb_cli_enqueue_change(vty, xpath_addr, NB_OP_MODIFY,
6577 argv[idx_ipv4]->arg);
6578
6579 return nb_cli_apply_changes(vty, NULL);
6580 }
6581
6582 DEFUN_YANG (no_set_aggregator_as,
6583 no_set_aggregator_as_cmd,
6584 "no set aggregator as [ASNUM A.B.C.D]",
6585 NO_STR
6586 SET_STR
6587 "BGP aggregator attribute\n"
6588 "AS number of aggregator\n"
6589 AS_STR
6590 "IP address of aggregator\n")
6591 {
6592 const char *xpath =
6593 "./set-action[action='frr-bgp-route-map:aggregator']";
6594
6595 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6596 return nb_cli_apply_changes(vty, NULL);
6597 }
6598
6599 DEFUN_YANG (match_ipv6_next_hop,
6600 match_ipv6_next_hop_cmd,
6601 "match ipv6 next-hop ACCESSLIST6_NAME",
6602 MATCH_STR
6603 IPV6_STR
6604 "Match IPv6 next-hop address of route\n"
6605 "IPv6 access-list name\n")
6606 {
6607 const char *xpath =
6608 "./match-condition[condition='frr-route-map:ipv6-next-hop-list']";
6609 char xpath_value[XPATH_MAXLEN];
6610
6611 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6612 snprintf(xpath_value, sizeof(xpath_value),
6613 "%s/rmap-match-condition/list-name", xpath);
6614 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6615 argv[argc - 1]->arg);
6616
6617 return nb_cli_apply_changes(vty, NULL);
6618 }
6619
6620 DEFUN_YANG (no_match_ipv6_next_hop,
6621 no_match_ipv6_next_hop_cmd,
6622 "no match ipv6 next-hop [ACCESSLIST6_NAME]",
6623 NO_STR
6624 MATCH_STR
6625 IPV6_STR
6626 "Match IPv6 next-hop address of route\n"
6627 "IPv6 access-list name\n")
6628 {
6629 const char *xpath =
6630 "./match-condition[condition='frr-route-map:ipv6-next-hop-list']";
6631
6632 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6633 return nb_cli_apply_changes(vty, NULL);
6634 }
6635
6636 DEFUN_YANG (match_ipv6_next_hop_address,
6637 match_ipv6_next_hop_address_cmd,
6638 "match ipv6 next-hop address X:X::X:X",
6639 MATCH_STR
6640 IPV6_STR
6641 "Match IPv6 next-hop address of route\n"
6642 "IPv6 address\n"
6643 "IPv6 address of next hop\n")
6644 {
6645 const char *xpath =
6646 "./match-condition[condition='frr-bgp-route-map:ipv6-nexthop']";
6647 char xpath_value[XPATH_MAXLEN];
6648
6649 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6650 snprintf(xpath_value, sizeof(xpath_value),
6651 "%s/rmap-match-condition/frr-bgp-route-map:ipv6-address",
6652 xpath);
6653 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6654 argv[argc - 1]->arg);
6655
6656 return nb_cli_apply_changes(vty, NULL);
6657 }
6658
6659 DEFUN_YANG (no_match_ipv6_next_hop_address,
6660 no_match_ipv6_next_hop_address_cmd,
6661 "no match ipv6 next-hop address X:X::X:X",
6662 NO_STR
6663 MATCH_STR
6664 IPV6_STR
6665 "Match IPv6 next-hop address of route\n"
6666 "IPv6 address\n"
6667 "IPv6 address of next hop\n")
6668 {
6669 const char *xpath =
6670 "./match-condition[condition='frr-bgp-route-map:ipv6-nexthop']";
6671
6672 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6673 return nb_cli_apply_changes(vty, NULL);
6674 }
6675
6676 ALIAS_HIDDEN (match_ipv6_next_hop_address,
6677 match_ipv6_next_hop_old_cmd,
6678 "match ipv6 next-hop X:X::X:X",
6679 MATCH_STR
6680 IPV6_STR
6681 "Match IPv6 next-hop address of route\n"
6682 "IPv6 address of next hop\n")
6683
6684 ALIAS_HIDDEN (no_match_ipv6_next_hop_address,
6685 no_match_ipv6_next_hop_old_cmd,
6686 "no match ipv6 next-hop X:X::X:X",
6687 NO_STR
6688 MATCH_STR
6689 IPV6_STR
6690 "Match IPv6 next-hop address of route\n"
6691 "IPv6 address of next hop\n")
6692
6693 DEFUN_YANG (match_ipv6_next_hop_prefix_list,
6694 match_ipv6_next_hop_prefix_list_cmd,
6695 "match ipv6 next-hop prefix-list PREFIXLIST_NAME",
6696 MATCH_STR
6697 IPV6_STR
6698 "Match IPv6 next-hop address of route\n"
6699 "Match entries by prefix-list\n"
6700 "IPv6 prefix-list name\n")
6701 {
6702 const char *xpath =
6703 "./match-condition[condition='frr-route-map:ipv6-next-hop-prefix-list']";
6704 char xpath_value[XPATH_MAXLEN];
6705
6706 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6707 snprintf(xpath_value, sizeof(xpath_value),
6708 "%s/rmap-match-condition/list-name", xpath);
6709 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6710 argv[argc - 1]->arg);
6711
6712 return nb_cli_apply_changes(vty, NULL);
6713 }
6714
6715 DEFUN_YANG (no_match_ipv6_next_hop_prefix_list,
6716 no_match_ipv6_next_hop_prefix_list_cmd,
6717 "no match ipv6 next-hop prefix-list [PREFIXLIST_NAME]",
6718 NO_STR
6719 MATCH_STR
6720 IPV6_STR
6721 "Match IPv6 next-hop address of route\n"
6722 "Match entries by prefix-list\n"
6723 "IPv6 prefix-list name\n")
6724 {
6725 const char *xpath =
6726 "./match-condition[condition='frr-route-map:ipv6-next-hop-prefix-list']";
6727
6728 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6729 return nb_cli_apply_changes(vty, NULL);
6730 }
6731
6732 DEFPY_YANG (match_ipv4_next_hop,
6733 match_ipv4_next_hop_cmd,
6734 "match ip next-hop address A.B.C.D",
6735 MATCH_STR
6736 IP_STR
6737 "Match IP next-hop address of route\n"
6738 "IP address\n"
6739 "IP address of next-hop\n")
6740 {
6741 const char *xpath =
6742 "./match-condition[condition='frr-bgp-route-map:ipv4-nexthop']";
6743 char xpath_value[XPATH_MAXLEN];
6744
6745 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6746 snprintf(xpath_value, sizeof(xpath_value),
6747 "%s/rmap-match-condition/frr-bgp-route-map:ipv4-address",
6748 xpath);
6749 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, argv[4]->arg);
6750
6751 return nb_cli_apply_changes(vty, NULL);
6752 }
6753
6754 DEFPY_YANG (no_match_ipv4_next_hop,
6755 no_match_ipv4_next_hop_cmd,
6756 "no match ip next-hop address [A.B.C.D]",
6757 NO_STR
6758 MATCH_STR
6759 IP_STR
6760 "Match IP next-hop address of route\n"
6761 "IP address\n"
6762 "IP address of next-hop\n")
6763 {
6764 const char *xpath =
6765 "./match-condition[condition='frr-bgp-route-map:ipv4-nexthop']";
6766
6767 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6768 return nb_cli_apply_changes(vty, NULL);
6769 }
6770
6771 DEFUN_YANG (set_ipv6_nexthop_peer,
6772 set_ipv6_nexthop_peer_cmd,
6773 "set ipv6 next-hop peer-address",
6774 SET_STR
6775 IPV6_STR
6776 "Next hop address\n"
6777 "Use peer address (for BGP only)\n")
6778 {
6779 const char *xpath =
6780 "./set-action[action='frr-bgp-route-map:ipv6-peer-address']";
6781 char xpath_value[XPATH_MAXLEN];
6782
6783 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6784 snprintf(xpath_value, sizeof(xpath_value),
6785 "%s/rmap-set-action/frr-bgp-route-map:preference", xpath);
6786 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
6787
6788 return nb_cli_apply_changes(vty, NULL);
6789 }
6790
6791 DEFUN_YANG (no_set_ipv6_nexthop_peer,
6792 no_set_ipv6_nexthop_peer_cmd,
6793 "no set ipv6 next-hop peer-address",
6794 NO_STR
6795 SET_STR
6796 IPV6_STR
6797 "IPv6 next-hop address\n"
6798 "Use peer address (for BGP only)\n")
6799 {
6800 const char *xpath =
6801 "./set-action[action='frr-bgp-route-map:ipv6-peer-address']";
6802
6803 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6804 return nb_cli_apply_changes(vty, NULL);
6805 }
6806
6807 DEFUN_YANG (set_ipv6_nexthop_prefer_global,
6808 set_ipv6_nexthop_prefer_global_cmd,
6809 "set ipv6 next-hop prefer-global",
6810 SET_STR
6811 IPV6_STR
6812 "IPv6 next-hop address\n"
6813 "Prefer global over link-local if both exist\n")
6814 {
6815 const char *xpath =
6816 "./set-action[action='frr-bgp-route-map:ipv6-prefer-global']";
6817 char xpath_value[XPATH_MAXLEN];
6818
6819 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6820 snprintf(xpath_value, sizeof(xpath_value),
6821 "%s/rmap-set-action/frr-bgp-route-map:preference", xpath);
6822 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, "true");
6823
6824 return nb_cli_apply_changes(vty, NULL);
6825 }
6826
6827 DEFUN_YANG (no_set_ipv6_nexthop_prefer_global,
6828 no_set_ipv6_nexthop_prefer_global_cmd,
6829 "no set ipv6 next-hop prefer-global",
6830 NO_STR
6831 SET_STR
6832 IPV6_STR
6833 "IPv6 next-hop address\n"
6834 "Prefer global over link-local if both exist\n")
6835 {
6836 const char *xpath =
6837 "./set-action[action='frr-bgp-route-map:ipv6-prefer-global']";
6838
6839 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6840 return nb_cli_apply_changes(vty, NULL);
6841 }
6842
6843 DEFUN_YANG (set_ipv6_nexthop_global,
6844 set_ipv6_nexthop_global_cmd,
6845 "set ipv6 next-hop global X:X::X:X",
6846 SET_STR
6847 IPV6_STR
6848 "IPv6 next-hop address\n"
6849 "IPv6 global address\n"
6850 "IPv6 address of next hop\n")
6851 {
6852 int idx_ipv6 = 4;
6853 const char *xpath =
6854 "./set-action[action='frr-bgp-route-map:ipv6-nexthop-global']";
6855 char xpath_value[XPATH_MAXLEN];
6856
6857 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6858 snprintf(xpath_value, sizeof(xpath_value),
6859 "%s/rmap-set-action/frr-bgp-route-map:ipv6-address", xpath);
6860 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6861 argv[idx_ipv6]->arg);
6862
6863 return nb_cli_apply_changes(vty, NULL);
6864 }
6865
6866 DEFUN_YANG (no_set_ipv6_nexthop_global,
6867 no_set_ipv6_nexthop_global_cmd,
6868 "no set ipv6 next-hop global X:X::X:X",
6869 NO_STR
6870 SET_STR
6871 IPV6_STR
6872 "IPv6 next-hop address\n"
6873 "IPv6 global address\n"
6874 "IPv6 address of next hop\n")
6875 {
6876 const char *xpath =
6877 "./set-action[action='frr-bgp-route-map:ipv6-nexthop-global']";
6878
6879 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6880 return nb_cli_apply_changes(vty, NULL);
6881 }
6882
6883 #ifdef KEEP_OLD_VPN_COMMANDS
6884 DEFUN_YANG (set_vpn_nexthop,
6885 set_vpn_nexthop_cmd,
6886 "set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
6887 SET_STR
6888 "VPNv4 information\n"
6889 "VPN next-hop address\n"
6890 "IP address of next hop\n"
6891 "VPNv6 information\n"
6892 "VPN next-hop address\n"
6893 "IPv6 address of next hop\n")
6894 {
6895 int idx_ip = 3;
6896 afi_t afi;
6897 int idx = 0;
6898 char xpath_value[XPATH_MAXLEN];
6899
6900 if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
6901 if (afi == AFI_IP) {
6902 const char *xpath =
6903 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
6904
6905 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6906 snprintf(
6907 xpath_value, sizeof(xpath_value),
6908 "%s/rmap-set-action/frr-bgp-route-map:ipv4-address",
6909 xpath);
6910 } else {
6911 const char *xpath =
6912 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
6913
6914 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6915 snprintf(
6916 xpath_value, sizeof(xpath_value),
6917 "%s/rmap-set-action/frr-bgp-route-map:ipv6-address",
6918 xpath);
6919 }
6920
6921 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
6922 argv[idx_ip]->arg);
6923
6924 return nb_cli_apply_changes(vty, NULL);
6925 }
6926
6927 return CMD_SUCCESS;
6928 }
6929
6930 DEFUN_YANG (no_set_vpn_nexthop,
6931 no_set_vpn_nexthop_cmd,
6932 "no set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
6933 NO_STR
6934 SET_STR
6935 "VPNv4 information\n"
6936 "VPN next-hop address\n"
6937 "IP address of next hop\n"
6938 "VPNv6 information\n"
6939 "VPN next-hop address\n"
6940 "IPv6 address of next hop\n")
6941 {
6942 afi_t afi;
6943 int idx = 0;
6944
6945 if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
6946 if (afi == AFI_IP) {
6947 const char *xpath =
6948 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
6949 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6950 } else {
6951 const char *xpath =
6952 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
6953 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
6954 }
6955 return nb_cli_apply_changes(vty, NULL);
6956 }
6957 return CMD_SUCCESS;
6958 }
6959 #endif /* KEEP_OLD_VPN_COMMANDS */
6960
6961 DEFPY_YANG (set_ipx_vpn_nexthop,
6962 set_ipx_vpn_nexthop_cmd,
6963 "set <ipv4|ipv6> vpn next-hop <A.B.C.D$addrv4|X:X::X:X$addrv6>",
6964 SET_STR
6965 "IPv4 information\n"
6966 "IPv6 information\n"
6967 "VPN information\n"
6968 "VPN next-hop address\n"
6969 "IP address of next hop\n"
6970 "IPv6 address of next hop\n")
6971 {
6972 int idx_ip = 4;
6973 afi_t afi;
6974 int idx = 0;
6975 char xpath_value[XPATH_MAXLEN];
6976
6977 if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
6978 if (afi == AFI_IP) {
6979 if (addrv6_str) {
6980 vty_out(vty, "%% IPv4 next-hop expected\n");
6981 return CMD_WARNING_CONFIG_FAILED;
6982 }
6983
6984 const char *xpath =
6985 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
6986
6987 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
6988 snprintf(
6989 xpath_value, sizeof(xpath_value),
6990 "%s/rmap-set-action/frr-bgp-route-map:ipv4-address",
6991 xpath);
6992 } else {
6993 if (addrv4_str) {
6994 vty_out(vty, "%% IPv6 next-hop expected\n");
6995 return CMD_WARNING_CONFIG_FAILED;
6996 }
6997
6998 const char *xpath =
6999 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
7000
7001 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
7002 snprintf(
7003 xpath_value, sizeof(xpath_value),
7004 "%s/rmap-set-action/frr-bgp-route-map:ipv6-address",
7005 xpath);
7006 }
7007 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
7008 argv[idx_ip]->arg);
7009 return nb_cli_apply_changes(vty, NULL);
7010 }
7011 return CMD_SUCCESS;
7012 }
7013
7014 DEFUN_YANG (no_set_ipx_vpn_nexthop,
7015 no_set_ipx_vpn_nexthop_cmd,
7016 "no set <ipv4|ipv6> vpn next-hop [<A.B.C.D|X:X::X:X>]",
7017 NO_STR
7018 SET_STR
7019 "IPv4 information\n"
7020 "IPv6 information\n"
7021 "VPN information\n"
7022 "VPN next-hop address\n"
7023 "IP address of next hop\n"
7024 "IPv6 address of next hop\n")
7025 {
7026 afi_t afi;
7027 int idx = 0;
7028
7029 if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
7030 if (afi == AFI_IP) {
7031 const char *xpath =
7032 "./set-action[action='frr-bgp-route-map:ipv4-vpn-address']";
7033 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
7034 } else {
7035 const char *xpath =
7036 "./set-action[action='frr-bgp-route-map:ipv6-vpn-address']";
7037 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
7038 }
7039 return nb_cli_apply_changes(vty, NULL);
7040 }
7041 return CMD_SUCCESS;
7042 }
7043
7044 DEFUN_YANG (set_originator_id,
7045 set_originator_id_cmd,
7046 "set originator-id A.B.C.D",
7047 SET_STR
7048 "BGP originator ID attribute\n"
7049 "IP address of originator\n")
7050 {
7051 int idx_ipv4 = 2;
7052 const char *xpath =
7053 "./set-action[action='frr-bgp-route-map:originator-id']";
7054 char xpath_value[XPATH_MAXLEN];
7055
7056 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
7057 snprintf(xpath_value, sizeof(xpath_value),
7058 "%s/rmap-set-action/frr-bgp-route-map:originator-id", xpath);
7059 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
7060 argv[idx_ipv4]->arg);
7061
7062 return nb_cli_apply_changes(vty, NULL);
7063 }
7064
7065 DEFUN_YANG (no_set_originator_id,
7066 no_set_originator_id_cmd,
7067 "no set originator-id [A.B.C.D]",
7068 NO_STR
7069 SET_STR
7070 "BGP originator ID attribute\n"
7071 "IP address of originator\n")
7072 {
7073 const char *xpath =
7074 "./set-action[action='frr-bgp-route-map:originator-id']";
7075
7076 nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
7077 return nb_cli_apply_changes(vty, NULL);
7078 }
7079
7080 DEFPY_YANG (match_rpki_extcommunity,
7081 match_rpki_extcommunity_cmd,
7082 "[no$no] match rpki-extcommunity <valid|invalid|notfound>",
7083 NO_STR
7084 MATCH_STR
7085 "BGP RPKI (Origin Validation State) extended community attribute\n"
7086 "Valid prefix\n"
7087 "Invalid prefix\n"
7088 "Prefix not found\n")
7089 {
7090 const char *xpath =
7091 "./match-condition[condition='frr-bgp-route-map:rpki-extcommunity']";
7092 char xpath_value[XPATH_MAXLEN];
7093
7094 nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
7095
7096 if (!no) {
7097 snprintf(
7098 xpath_value, sizeof(xpath_value),
7099 "%s/rmap-match-condition/frr-bgp-route-map:rpki-extcommunity",
7100 xpath);
7101 nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
7102 argv[2]->arg);
7103 }
7104
7105 return nb_cli_apply_changes(vty, NULL);
7106 }
7107
7108 /* Initialization of route map. */
7109 void bgp_route_map_init(void)
7110 {
7111 route_map_init();
7112
7113 route_map_add_hook(bgp_route_map_add);
7114 route_map_delete_hook(bgp_route_map_delete);
7115 route_map_event_hook(bgp_route_map_event);
7116
7117 route_map_match_interface_hook(generic_match_add);
7118 route_map_no_match_interface_hook(generic_match_delete);
7119
7120 route_map_match_ip_address_hook(generic_match_add);
7121 route_map_no_match_ip_address_hook(generic_match_delete);
7122
7123 route_map_match_ip_address_prefix_list_hook(generic_match_add);
7124 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
7125
7126 route_map_match_ip_next_hop_hook(generic_match_add);
7127 route_map_no_match_ip_next_hop_hook(generic_match_delete);
7128
7129 route_map_match_ipv6_next_hop_hook(generic_match_add);
7130 route_map_no_match_ipv6_next_hop_hook(generic_match_delete);
7131
7132 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
7133 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
7134
7135 route_map_match_ip_next_hop_type_hook(generic_match_add);
7136 route_map_no_match_ip_next_hop_type_hook(generic_match_delete);
7137
7138 route_map_match_ipv6_address_hook(generic_match_add);
7139 route_map_no_match_ipv6_address_hook(generic_match_delete);
7140
7141 route_map_match_ipv6_address_prefix_list_hook(generic_match_add);
7142 route_map_no_match_ipv6_address_prefix_list_hook(generic_match_delete);
7143
7144 route_map_match_ipv6_next_hop_type_hook(generic_match_add);
7145 route_map_no_match_ipv6_next_hop_type_hook(generic_match_delete);
7146
7147 route_map_match_ipv6_next_hop_prefix_list_hook(generic_match_add);
7148 route_map_no_match_ipv6_next_hop_prefix_list_hook(generic_match_delete);
7149
7150 route_map_match_metric_hook(generic_match_add);
7151 route_map_no_match_metric_hook(generic_match_delete);
7152
7153 route_map_match_tag_hook(generic_match_add);
7154 route_map_no_match_tag_hook(generic_match_delete);
7155
7156 route_map_set_srte_color_hook(generic_set_add);
7157 route_map_no_set_srte_color_hook(generic_set_delete);
7158
7159 route_map_set_ip_nexthop_hook(generic_set_add);
7160 route_map_no_set_ip_nexthop_hook(generic_set_delete);
7161
7162 route_map_set_ipv6_nexthop_local_hook(generic_set_add);
7163 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete);
7164
7165 route_map_set_metric_hook(generic_set_add);
7166 route_map_no_set_metric_hook(generic_set_delete);
7167
7168 route_map_set_tag_hook(generic_set_add);
7169 route_map_no_set_tag_hook(generic_set_delete);
7170
7171 route_map_install_match(&route_match_peer_cmd);
7172 route_map_install_match(&route_match_alias_cmd);
7173 route_map_install_match(&route_match_local_pref_cmd);
7174 #ifdef HAVE_SCRIPTING
7175 route_map_install_match(&route_match_script_cmd);
7176 #endif
7177 route_map_install_match(&route_match_ip_address_cmd);
7178 route_map_install_match(&route_match_ip_next_hop_cmd);
7179 route_map_install_match(&route_match_ip_route_source_cmd);
7180 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
7181 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
7182 route_map_install_match(&route_match_ip_next_hop_type_cmd);
7183 route_map_install_match(&route_match_ip_route_source_prefix_list_cmd);
7184 route_map_install_match(&route_match_aspath_cmd);
7185 route_map_install_match(&route_match_community_cmd);
7186 route_map_install_match(&route_match_lcommunity_cmd);
7187 route_map_install_match(&route_match_ecommunity_cmd);
7188 route_map_install_match(&route_match_local_pref_cmd);
7189 route_map_install_match(&route_match_metric_cmd);
7190 route_map_install_match(&route_match_origin_cmd);
7191 route_map_install_match(&route_match_probability_cmd);
7192 route_map_install_match(&route_match_interface_cmd);
7193 route_map_install_match(&route_match_tag_cmd);
7194 route_map_install_match(&route_match_mac_address_cmd);
7195 route_map_install_match(&route_match_evpn_vni_cmd);
7196 route_map_install_match(&route_match_evpn_route_type_cmd);
7197 route_map_install_match(&route_match_evpn_rd_cmd);
7198 route_map_install_match(&route_match_evpn_default_route_cmd);
7199 route_map_install_match(&route_match_vrl_source_vrf_cmd);
7200
7201 route_map_install_set(&route_set_evpn_gateway_ip_ipv4_cmd);
7202 route_map_install_set(&route_set_evpn_gateway_ip_ipv6_cmd);
7203 route_map_install_set(&route_set_table_id_cmd);
7204 route_map_install_set(&route_set_srte_color_cmd);
7205 route_map_install_set(&route_set_ip_nexthop_cmd);
7206 route_map_install_set(&route_set_local_pref_cmd);
7207 route_map_install_set(&route_set_weight_cmd);
7208 route_map_install_set(&route_set_label_index_cmd);
7209 route_map_install_set(&route_set_metric_cmd);
7210 route_map_install_set(&route_set_distance_cmd);
7211 route_map_install_set(&route_set_aspath_prepend_cmd);
7212 route_map_install_set(&route_set_aspath_exclude_cmd);
7213 route_map_install_set(&route_set_aspath_replace_cmd);
7214 route_map_install_set(&route_set_origin_cmd);
7215 route_map_install_set(&route_set_atomic_aggregate_cmd);
7216 route_map_install_set(&route_set_aigp_metric_cmd);
7217 route_map_install_set(&route_set_aggregator_as_cmd);
7218 route_map_install_set(&route_set_community_cmd);
7219 route_map_install_set(&route_set_community_delete_cmd);
7220 route_map_install_set(&route_set_lcommunity_cmd);
7221 route_map_install_set(&route_set_lcommunity_delete_cmd);
7222 route_map_install_set(&route_set_vpnv4_nexthop_cmd);
7223 route_map_install_set(&route_set_vpnv6_nexthop_cmd);
7224 route_map_install_set(&route_set_originator_id_cmd);
7225 route_map_install_set(&route_set_ecommunity_rt_cmd);
7226 route_map_install_set(&route_set_ecommunity_soo_cmd);
7227 route_map_install_set(&route_set_ecommunity_lb_cmd);
7228 route_map_install_set(&route_set_ecommunity_none_cmd);
7229 route_map_install_set(&route_set_tag_cmd);
7230 route_map_install_set(&route_set_label_index_cmd);
7231 route_map_install_set(&route_set_l3vpn_nexthop_encapsulation_cmd);
7232
7233 install_element(RMAP_NODE, &match_peer_cmd);
7234 install_element(RMAP_NODE, &match_peer_local_cmd);
7235 install_element(RMAP_NODE, &no_match_peer_cmd);
7236 install_element(RMAP_NODE, &match_ip_route_source_cmd);
7237 install_element(RMAP_NODE, &no_match_ip_route_source_cmd);
7238 install_element(RMAP_NODE, &match_ip_route_source_prefix_list_cmd);
7239 install_element(RMAP_NODE, &no_match_ip_route_source_prefix_list_cmd);
7240 install_element(RMAP_NODE, &match_mac_address_cmd);
7241 install_element(RMAP_NODE, &no_match_mac_address_cmd);
7242 install_element(RMAP_NODE, &match_evpn_vni_cmd);
7243 install_element(RMAP_NODE, &no_match_evpn_vni_cmd);
7244 install_element(RMAP_NODE, &match_evpn_route_type_cmd);
7245 install_element(RMAP_NODE, &no_match_evpn_route_type_cmd);
7246 install_element(RMAP_NODE, &match_evpn_rd_cmd);
7247 install_element(RMAP_NODE, &no_match_evpn_rd_cmd);
7248 install_element(RMAP_NODE, &match_evpn_default_route_cmd);
7249 install_element(RMAP_NODE, &no_match_evpn_default_route_cmd);
7250 install_element(RMAP_NODE, &set_evpn_gw_ip_ipv4_cmd);
7251 install_element(RMAP_NODE, &no_set_evpn_gw_ip_ipv4_cmd);
7252 install_element(RMAP_NODE, &set_evpn_gw_ip_ipv6_cmd);
7253 install_element(RMAP_NODE, &no_set_evpn_gw_ip_ipv6_cmd);
7254 install_element(RMAP_NODE, &match_vrl_source_vrf_cmd);
7255 install_element(RMAP_NODE, &no_match_vrl_source_vrf_cmd);
7256
7257 install_element(RMAP_NODE, &match_aspath_cmd);
7258 install_element(RMAP_NODE, &no_match_aspath_cmd);
7259 install_element(RMAP_NODE, &match_local_pref_cmd);
7260 install_element(RMAP_NODE, &no_match_local_pref_cmd);
7261 install_element(RMAP_NODE, &match_alias_cmd);
7262 install_element(RMAP_NODE, &no_match_alias_cmd);
7263 install_element(RMAP_NODE, &match_community_cmd);
7264 install_element(RMAP_NODE, &no_match_community_cmd);
7265 install_element(RMAP_NODE, &match_lcommunity_cmd);
7266 install_element(RMAP_NODE, &no_match_lcommunity_cmd);
7267 install_element(RMAP_NODE, &match_ecommunity_cmd);
7268 install_element(RMAP_NODE, &no_match_ecommunity_cmd);
7269 install_element(RMAP_NODE, &match_origin_cmd);
7270 install_element(RMAP_NODE, &no_match_origin_cmd);
7271 install_element(RMAP_NODE, &match_probability_cmd);
7272 install_element(RMAP_NODE, &no_match_probability_cmd);
7273
7274 install_element(RMAP_NODE, &no_set_table_id_cmd);
7275 install_element(RMAP_NODE, &set_table_id_cmd);
7276 install_element(RMAP_NODE, &set_ip_nexthop_peer_cmd);
7277 install_element(RMAP_NODE, &set_ip_nexthop_unchanged_cmd);
7278 install_element(RMAP_NODE, &set_local_pref_cmd);
7279 install_element(RMAP_NODE, &set_distance_cmd);
7280 install_element(RMAP_NODE, &no_set_distance_cmd);
7281 install_element(RMAP_NODE, &no_set_local_pref_cmd);
7282 install_element(RMAP_NODE, &set_weight_cmd);
7283 install_element(RMAP_NODE, &set_label_index_cmd);
7284 install_element(RMAP_NODE, &no_set_weight_cmd);
7285 install_element(RMAP_NODE, &no_set_label_index_cmd);
7286 install_element(RMAP_NODE, &set_aspath_prepend_asn_cmd);
7287 install_element(RMAP_NODE, &set_aspath_prepend_lastas_cmd);
7288 install_element(RMAP_NODE, &set_aspath_exclude_cmd);
7289 install_element(RMAP_NODE, &set_aspath_replace_asn_cmd);
7290 install_element(RMAP_NODE, &no_set_aspath_prepend_cmd);
7291 install_element(RMAP_NODE, &no_set_aspath_prepend_lastas_cmd);
7292 install_element(RMAP_NODE, &no_set_aspath_exclude_cmd);
7293 install_element(RMAP_NODE, &no_set_aspath_exclude_all_cmd);
7294 install_element(RMAP_NODE, &no_set_aspath_replace_asn_cmd);
7295 install_element(RMAP_NODE, &set_origin_cmd);
7296 install_element(RMAP_NODE, &no_set_origin_cmd);
7297 install_element(RMAP_NODE, &set_atomic_aggregate_cmd);
7298 install_element(RMAP_NODE, &no_set_atomic_aggregate_cmd);
7299 install_element(RMAP_NODE, &set_aigp_metric_cmd);
7300 install_element(RMAP_NODE, &no_set_aigp_metric_cmd);
7301 install_element(RMAP_NODE, &set_aggregator_as_cmd);
7302 install_element(RMAP_NODE, &no_set_aggregator_as_cmd);
7303 install_element(RMAP_NODE, &set_community_cmd);
7304 install_element(RMAP_NODE, &set_community_none_cmd);
7305 install_element(RMAP_NODE, &no_set_community_cmd);
7306 install_element(RMAP_NODE, &no_set_community_short_cmd);
7307 install_element(RMAP_NODE, &set_community_delete_cmd);
7308 install_element(RMAP_NODE, &no_set_community_delete_cmd);
7309 install_element(RMAP_NODE, &set_lcommunity_cmd);
7310 install_element(RMAP_NODE, &set_lcommunity_none_cmd);
7311 install_element(RMAP_NODE, &no_set_lcommunity_cmd);
7312 install_element(RMAP_NODE, &no_set_lcommunity1_cmd);
7313 install_element(RMAP_NODE, &no_set_lcommunity1_short_cmd);
7314 install_element(RMAP_NODE, &set_lcommunity_delete_cmd);
7315 install_element(RMAP_NODE, &no_set_lcommunity_delete_cmd);
7316 install_element(RMAP_NODE, &no_set_lcommunity_delete_short_cmd);
7317 install_element(RMAP_NODE, &set_ecommunity_rt_cmd);
7318 install_element(RMAP_NODE, &no_set_ecommunity_rt_cmd);
7319 install_element(RMAP_NODE, &no_set_ecommunity_rt_short_cmd);
7320 install_element(RMAP_NODE, &set_ecommunity_soo_cmd);
7321 install_element(RMAP_NODE, &no_set_ecommunity_soo_cmd);
7322 install_element(RMAP_NODE, &no_set_ecommunity_soo_short_cmd);
7323 install_element(RMAP_NODE, &set_ecommunity_lb_cmd);
7324 install_element(RMAP_NODE, &no_set_ecommunity_lb_cmd);
7325 install_element(RMAP_NODE, &no_set_ecommunity_lb_short_cmd);
7326 install_element(RMAP_NODE, &set_ecommunity_none_cmd);
7327 install_element(RMAP_NODE, &no_set_ecommunity_none_cmd);
7328 #ifdef KEEP_OLD_VPN_COMMANDS
7329 install_element(RMAP_NODE, &set_vpn_nexthop_cmd);
7330 install_element(RMAP_NODE, &no_set_vpn_nexthop_cmd);
7331 #endif /* KEEP_OLD_VPN_COMMANDS */
7332 install_element(RMAP_NODE, &set_ipx_vpn_nexthop_cmd);
7333 install_element(RMAP_NODE, &no_set_ipx_vpn_nexthop_cmd);
7334 install_element(RMAP_NODE, &set_originator_id_cmd);
7335 install_element(RMAP_NODE, &no_set_originator_id_cmd);
7336 install_element(RMAP_NODE, &set_l3vpn_nexthop_encapsulation_cmd);
7337
7338 route_map_install_match(&route_match_ipv6_address_cmd);
7339 route_map_install_match(&route_match_ipv6_next_hop_cmd);
7340 route_map_install_match(&route_match_ipv6_next_hop_address_cmd);
7341 route_map_install_match(&route_match_ipv6_next_hop_prefix_list_cmd);
7342 route_map_install_match(&route_match_ipv4_next_hop_cmd);
7343 route_map_install_match(&route_match_ipv6_address_prefix_list_cmd);
7344 route_map_install_match(&route_match_ipv6_next_hop_type_cmd);
7345 route_map_install_set(&route_set_ipv6_nexthop_global_cmd);
7346 route_map_install_set(&route_set_ipv6_nexthop_prefer_global_cmd);
7347 route_map_install_set(&route_set_ipv6_nexthop_local_cmd);
7348 route_map_install_set(&route_set_ipv6_nexthop_peer_cmd);
7349 route_map_install_match(&route_match_rpki_extcommunity_cmd);
7350
7351 install_element(RMAP_NODE, &match_ipv6_next_hop_cmd);
7352 install_element(RMAP_NODE, &match_ipv6_next_hop_address_cmd);
7353 install_element(RMAP_NODE, &match_ipv6_next_hop_prefix_list_cmd);
7354 install_element(RMAP_NODE, &no_match_ipv6_next_hop_cmd);
7355 install_element(RMAP_NODE, &no_match_ipv6_next_hop_address_cmd);
7356 install_element(RMAP_NODE, &no_match_ipv6_next_hop_prefix_list_cmd);
7357 install_element(RMAP_NODE, &match_ipv6_next_hop_old_cmd);
7358 install_element(RMAP_NODE, &no_match_ipv6_next_hop_old_cmd);
7359 install_element(RMAP_NODE, &match_ipv4_next_hop_cmd);
7360 install_element(RMAP_NODE, &no_match_ipv4_next_hop_cmd);
7361 install_element(RMAP_NODE, &set_ipv6_nexthop_global_cmd);
7362 install_element(RMAP_NODE, &no_set_ipv6_nexthop_global_cmd);
7363 install_element(RMAP_NODE, &set_ipv6_nexthop_prefer_global_cmd);
7364 install_element(RMAP_NODE, &no_set_ipv6_nexthop_prefer_global_cmd);
7365 install_element(RMAP_NODE, &set_ipv6_nexthop_peer_cmd);
7366 install_element(RMAP_NODE, &no_set_ipv6_nexthop_peer_cmd);
7367 install_element(RMAP_NODE, &match_rpki_extcommunity_cmd);
7368 #ifdef HAVE_SCRIPTING
7369 install_element(RMAP_NODE, &match_script_cmd);
7370 #endif
7371 }
7372
7373 void bgp_route_map_terminate(void)
7374 {
7375 /* ToDo: Cleanup all the used memory */
7376 route_map_finish();
7377 }