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