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