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