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