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