]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_routemap.c
Merge pull request #4887 from vishaldhingra/lcomm_json
[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
2877 != BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
2878 path->attr->mp_nexthop_len =
2879 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
2880 } else {
2881 path->attr->mp_nexthop_global = peer_address;
2882 if (path->attr->mp_nexthop_len == 0)
2883 path->attr->mp_nexthop_len =
2884 BGP_ATTR_NHLEN_IPV6_GLOBAL;
2885 }
2886
2887 } else if (CHECK_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT)) {
2888 /* The next hop value will be set as part of packet
2889 * rewrite.
2890 * Set the flags here to indicate that rewrite needs to
2891 * be done.
2892 * Also, clear the value - we clear both global and
2893 * link-local
2894 * nexthops, whether we send one or both is determined
2895 * elsewhere.
2896 */
2897 SET_FLAG(path->attr->rmap_change_flags,
2898 BATTR_RMAP_NEXTHOP_PEER_ADDRESS);
2899 /* clear next hop value. */
2900 memset(&(path->attr->mp_nexthop_global), 0,
2901 sizeof(struct in6_addr));
2902 memset(&(path->attr->mp_nexthop_local), 0,
2903 sizeof(struct in6_addr));
2904 }
2905 }
2906
2907 return RMAP_OKAY;
2908 }
2909
2910 /* Route map `ip next-hop' compile function. Given string is converted
2911 to struct in_addr structure. */
2912 static void *route_set_ipv6_nexthop_peer_compile(const char *arg)
2913 {
2914 int *rins = NULL;
2915
2916 rins = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(int));
2917 *rins = 1;
2918
2919 return rins;
2920 }
2921
2922 /* Free route map's compiled `ip next-hop' value. */
2923 static void route_set_ipv6_nexthop_peer_free(void *rule)
2924 {
2925 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
2926 }
2927
2928 /* Route map commands for ip nexthop set. */
2929 struct route_map_rule_cmd route_set_ipv6_nexthop_peer_cmd = {
2930 "ipv6 next-hop peer-address", route_set_ipv6_nexthop_peer,
2931 route_set_ipv6_nexthop_peer_compile, route_set_ipv6_nexthop_peer_free};
2932
2933 /* `set ipv4 vpn next-hop A.B.C.D' */
2934
2935 static enum route_map_cmd_result_t
2936 route_set_vpnv4_nexthop(void *rule, const struct prefix *prefix,
2937 route_map_object_t type, void *object)
2938 {
2939 struct in_addr *address;
2940 struct bgp_path_info *path;
2941
2942 if (type == RMAP_BGP) {
2943 /* Fetch routemap's rule information. */
2944 address = rule;
2945 path = object;
2946
2947 /* Set next hop value. */
2948 path->attr->mp_nexthop_global_in = *address;
2949 path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
2950 }
2951
2952 return RMAP_OKAY;
2953 }
2954
2955 static void *route_set_vpnv4_nexthop_compile(const char *arg)
2956 {
2957 int ret;
2958 struct in_addr *address;
2959
2960 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
2961
2962 ret = inet_aton(arg, address);
2963
2964 if (ret == 0) {
2965 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
2966 return NULL;
2967 }
2968
2969 return address;
2970 }
2971
2972 /* `set ipv6 vpn next-hop A.B.C.D' */
2973
2974 static enum route_map_cmd_result_t
2975 route_set_vpnv6_nexthop(void *rule, const struct prefix *prefix,
2976 route_map_object_t type, void *object)
2977 {
2978 struct in6_addr *address;
2979 struct bgp_path_info *path;
2980
2981 if (type == RMAP_BGP) {
2982 /* Fetch routemap's rule information. */
2983 address = rule;
2984 path = object;
2985
2986 /* Set next hop value. */
2987 memcpy(&path->attr->mp_nexthop_global, address,
2988 sizeof(struct in6_addr));
2989 path->attr->mp_nexthop_len = BGP_ATTR_NHLEN_VPNV6_GLOBAL;
2990 }
2991
2992 return RMAP_OKAY;
2993 }
2994
2995 static void *route_set_vpnv6_nexthop_compile(const char *arg)
2996 {
2997 int ret;
2998 struct in6_addr *address;
2999
3000 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
3001 ret = inet_pton(AF_INET6, arg, address);
3002
3003 if (ret == 0) {
3004 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3005 return NULL;
3006 }
3007
3008 return address;
3009 }
3010
3011 static void route_set_vpn_nexthop_free(void *rule)
3012 {
3013 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3014 }
3015
3016 /* Route map commands for ipv4 next-hop set. */
3017 struct route_map_rule_cmd route_set_vpnv4_nexthop_cmd = {
3018 "ipv4 vpn next-hop", route_set_vpnv4_nexthop,
3019 route_set_vpnv4_nexthop_compile, route_set_vpn_nexthop_free};
3020
3021 /* Route map commands for ipv6 next-hop set. */
3022 struct route_map_rule_cmd route_set_vpnv6_nexthop_cmd = {
3023 "ipv6 vpn next-hop", route_set_vpnv6_nexthop,
3024 route_set_vpnv6_nexthop_compile, route_set_vpn_nexthop_free};
3025
3026 /* `set originator-id' */
3027
3028 /* For origin set. */
3029 static enum route_map_cmd_result_t
3030 route_set_originator_id(void *rule, const struct prefix *prefix,
3031 route_map_object_t type, void *object)
3032 {
3033 struct in_addr *address;
3034 struct bgp_path_info *path;
3035
3036 if (type == RMAP_BGP) {
3037 address = rule;
3038 path = object;
3039
3040 path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID);
3041 path->attr->originator_id = *address;
3042 }
3043
3044 return RMAP_OKAY;
3045 }
3046
3047 /* Compile function for originator-id set. */
3048 static void *route_set_originator_id_compile(const char *arg)
3049 {
3050 int ret;
3051 struct in_addr *address;
3052
3053 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
3054
3055 ret = inet_aton(arg, address);
3056
3057 if (ret == 0) {
3058 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
3059 return NULL;
3060 }
3061
3062 return address;
3063 }
3064
3065 /* Compile function for originator_id set. */
3066 static void route_set_originator_id_free(void *rule)
3067 {
3068 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
3069 }
3070
3071 /* Set originator-id rule structure. */
3072 struct route_map_rule_cmd route_set_originator_id_cmd = {
3073 "originator-id", route_set_originator_id,
3074 route_set_originator_id_compile, route_set_originator_id_free,
3075 };
3076
3077 /* Add bgp route map rule. */
3078 static int bgp_route_match_add(struct vty *vty, const char *command,
3079 const char *arg, route_map_event_t type)
3080 {
3081 VTY_DECLVAR_CONTEXT(route_map_index, index);
3082 int retval = CMD_SUCCESS;
3083 enum rmap_compile_rets ret;
3084
3085 ret = route_map_add_match(index, command, arg, type);
3086 switch (ret) {
3087 case RMAP_RULE_MISSING:
3088 vty_out(vty, "%% BGP Can't find rule.\n");
3089 retval = CMD_WARNING_CONFIG_FAILED;
3090 break;
3091 case RMAP_COMPILE_ERROR:
3092 vty_out(vty, "%% BGP Argument is malformed.\n");
3093 retval = CMD_WARNING_CONFIG_FAILED;
3094 break;
3095 case RMAP_COMPILE_SUCCESS:
3096 /*
3097 * Intentionally doing nothing here.
3098 */
3099 break;
3100 }
3101
3102 return retval;
3103 }
3104
3105 /* Delete bgp route map rule. */
3106 static int bgp_route_match_delete(struct vty *vty, const char *command,
3107 const char *arg, route_map_event_t type)
3108 {
3109 VTY_DECLVAR_CONTEXT(route_map_index, index);
3110 enum rmap_compile_rets ret;
3111 int retval = CMD_SUCCESS;
3112 char *dep_name = NULL;
3113 const char *tmpstr;
3114 char *rmap_name = NULL;
3115
3116 if (type != RMAP_EVENT_MATCH_DELETED) {
3117 /* ignore the mundane, the types without any dependency */
3118 if (arg == NULL) {
3119 if ((tmpstr = route_map_get_match_arg(index, command))
3120 != NULL)
3121 dep_name =
3122 XSTRDUP(MTYPE_ROUTE_MAP_RULE, tmpstr);
3123 } else {
3124 dep_name = XSTRDUP(MTYPE_ROUTE_MAP_RULE, arg);
3125 }
3126 rmap_name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, index->map->name);
3127 }
3128
3129 ret = route_map_delete_match(index, command, dep_name, type);
3130 switch (ret) {
3131 case RMAP_RULE_MISSING:
3132 vty_out(vty, "%% BGP Can't find rule.\n");
3133 retval = CMD_WARNING_CONFIG_FAILED;
3134 break;
3135 case RMAP_COMPILE_ERROR:
3136 vty_out(vty, "%% BGP Argument is malformed.\n");
3137 retval = CMD_WARNING_CONFIG_FAILED;
3138 break;
3139 case RMAP_COMPILE_SUCCESS:
3140 /*
3141 * Nothing to do here
3142 */
3143 break;
3144 }
3145
3146 XFREE(MTYPE_ROUTE_MAP_RULE, dep_name);
3147 XFREE(MTYPE_ROUTE_MAP_NAME, rmap_name);
3148
3149 return retval;
3150 }
3151
3152 /*
3153 * This is the workhorse routine for processing in/out routemap
3154 * modifications.
3155 */
3156 static void bgp_route_map_process_peer(const char *rmap_name,
3157 struct route_map *map, struct peer *peer,
3158 int afi, int safi, int route_update)
3159 {
3160 struct bgp_filter *filter;
3161
3162 if (!peer || !rmap_name)
3163 return;
3164
3165 filter = &peer->filter[afi][safi];
3166 /*
3167 * in is for non-route-server clients,
3168 * out is for all peers
3169 */
3170 if (filter->map[RMAP_IN].name
3171 && (strcmp(rmap_name, filter->map[RMAP_IN].name) == 0)) {
3172 filter->map[RMAP_IN].map = map;
3173
3174 if (route_update && peer->status == Established) {
3175 if (CHECK_FLAG(peer->af_flags[afi][safi],
3176 PEER_FLAG_SOFT_RECONFIG)) {
3177 if (bgp_debug_update(peer, NULL, NULL, 1))
3178 zlog_debug(
3179 "Processing route_map %s update on peer %s (inbound, soft-reconfig)",
3180 rmap_name, peer->host);
3181
3182 bgp_soft_reconfig_in(peer, afi, safi);
3183 } else if (CHECK_FLAG(peer->cap,
3184 PEER_CAP_REFRESH_OLD_RCV)
3185 || CHECK_FLAG(peer->cap,
3186 PEER_CAP_REFRESH_NEW_RCV)) {
3187 if (bgp_debug_update(peer, NULL, NULL, 1))
3188 zlog_debug(
3189 "Processing route_map %s update on peer %s (inbound, route-refresh)",
3190 rmap_name, peer->host);
3191 bgp_route_refresh_send(peer, afi, safi, 0, 0,
3192 0);
3193 }
3194 }
3195 }
3196
3197 /*
3198 * For outbound, unsuppress and default-originate map change (content or
3199 * map created), merely update the "config" here, the actual route
3200 * announcement happens at the group level.
3201 */
3202 if (filter->map[RMAP_OUT].name
3203 && (strcmp(rmap_name, filter->map[RMAP_OUT].name) == 0))
3204 filter->map[RMAP_OUT].map = map;
3205
3206 if (filter->usmap.name && (strcmp(rmap_name, filter->usmap.name) == 0))
3207 filter->usmap.map = map;
3208
3209 if (peer->default_rmap[afi][safi].name
3210 && (strcmp(rmap_name, peer->default_rmap[afi][safi].name) == 0))
3211 peer->default_rmap[afi][safi].map = map;
3212 }
3213
3214 static void bgp_route_map_update_peer_group(const char *rmap_name,
3215 struct route_map *map,
3216 struct bgp *bgp)
3217 {
3218 struct peer_group *group;
3219 struct listnode *node, *nnode;
3220 struct bgp_filter *filter;
3221 int afi, safi;
3222 int direct;
3223
3224 if (!bgp)
3225 return;
3226
3227 /* All the peers have been updated correctly already. This is
3228 * just updating the placeholder data. No real update required.
3229 */
3230 for (ALL_LIST_ELEMENTS(bgp->group, node, nnode, group)) {
3231 FOREACH_AFI_SAFI (afi, safi) {
3232 filter = &group->conf->filter[afi][safi];
3233
3234 for (direct = RMAP_IN; direct < RMAP_MAX; direct++) {
3235 if ((filter->map[direct].name)
3236 && (strcmp(rmap_name,
3237 filter->map[direct].name)
3238 == 0))
3239 filter->map[direct].map = map;
3240 }
3241
3242 if (filter->usmap.name
3243 && (strcmp(rmap_name, filter->usmap.name) == 0))
3244 filter->usmap.map = map;
3245 }
3246 }
3247 }
3248
3249 /*
3250 * Note that if an extreme number (tens of thousands) of route-maps are in use
3251 * and if bgp has an extreme number of peers, network statements, etc then this
3252 * function can consume a lot of cycles. This is due to this function being
3253 * called for each route-map and within this function we walk the list of peers,
3254 * network statements, etc looking to see if they use this route-map.
3255 */
3256 static void bgp_route_map_process_update(struct bgp *bgp, const char *rmap_name,
3257 int route_update)
3258 {
3259 int i;
3260 afi_t afi;
3261 safi_t safi;
3262 struct peer *peer;
3263 struct bgp_node *bn;
3264 struct bgp_static *bgp_static;
3265 struct bgp_aggregate *aggregate;
3266 struct listnode *node, *nnode;
3267 struct route_map *map;
3268 char buf[INET6_ADDRSTRLEN];
3269
3270 map = route_map_lookup_by_name(rmap_name);
3271
3272 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
3273
3274 /* Ignore dummy peer-group structure */
3275 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
3276 continue;
3277
3278 FOREACH_AFI_SAFI (afi, safi) {
3279 /* process in/out/import/export/default-orig
3280 * route-maps */
3281 bgp_route_map_process_peer(rmap_name, map, peer, afi,
3282 safi, route_update);
3283 }
3284 }
3285
3286 /* for outbound/default-orig route-maps, process for groups */
3287 update_group_policy_update(bgp, BGP_POLICY_ROUTE_MAP, rmap_name,
3288 route_update, 0);
3289
3290 /* update peer-group config (template) */
3291 bgp_route_map_update_peer_group(rmap_name, map, bgp);
3292
3293 FOREACH_AFI_SAFI (afi, safi) {
3294 /* For table route-map updates. */
3295 if (!bgp_fibupd_safi(safi))
3296 continue;
3297
3298 if (bgp->table_map[afi][safi].name
3299 && (strcmp(rmap_name, bgp->table_map[afi][safi].name)
3300 == 0)) {
3301
3302 /* bgp->table_map[afi][safi].map is NULL.
3303 * i.e Route map creation event.
3304 * So update applied_counter.
3305 * If it is not NULL, i.e It may be routemap updation or
3306 * deletion. so no need to update the counter.
3307 */
3308 if (!bgp->table_map[afi][safi].map)
3309 route_map_counter_increment(map);
3310 bgp->table_map[afi][safi].map = map;
3311
3312 if (BGP_DEBUG(zebra, ZEBRA))
3313 zlog_debug(
3314 "Processing route_map %s update on "
3315 "table map",
3316 rmap_name);
3317 if (route_update)
3318 bgp_zebra_announce_table(bgp, afi, safi);
3319 }
3320
3321 /* For network route-map updates. */
3322 for (bn = bgp_table_top(bgp->route[afi][safi]); bn;
3323 bn = bgp_route_next(bn)) {
3324 bgp_static = bgp_node_get_bgp_static_info(bn);
3325 if (!bgp_static)
3326 continue;
3327
3328 if (!bgp_static->rmap.name
3329 || (strcmp(rmap_name, bgp_static->rmap.name) != 0))
3330 continue;
3331
3332 if (!bgp_static->rmap.map)
3333 route_map_counter_increment(map);
3334
3335 bgp_static->rmap.map = map;
3336
3337 if (route_update && !bgp_static->backdoor) {
3338 if (bgp_debug_zebra(&bn->p))
3339 zlog_debug(
3340 "Processing route_map %s update on static route %s",
3341 rmap_name,
3342 inet_ntop(bn->p.family,
3343 &bn->p.u.prefix, buf,
3344 INET6_ADDRSTRLEN));
3345 bgp_static_update(bgp, &bn->p, bgp_static, afi,
3346 safi);
3347 }
3348 }
3349
3350 /* For aggregate-address route-map updates. */
3351 for (bn = bgp_table_top(bgp->aggregate[afi][safi]); bn;
3352 bn = bgp_route_next(bn)) {
3353 aggregate = bgp_node_get_bgp_aggregate_info(bn);
3354 if (!aggregate)
3355 continue;
3356
3357 if (!aggregate->rmap.name
3358 || (strcmp(rmap_name, aggregate->rmap.name) != 0))
3359 continue;
3360
3361 if (!aggregate->rmap.map)
3362 route_map_counter_increment(map);
3363
3364 aggregate->rmap.map = map;
3365
3366 if (route_update) {
3367 if (bgp_debug_zebra(&bn->p))
3368 zlog_debug(
3369 "Processing route_map %s update on aggregate-address route %s",
3370 rmap_name,
3371 inet_ntop(bn->p.family,
3372 &bn->p.u.prefix, buf,
3373 INET6_ADDRSTRLEN));
3374 bgp_aggregate_route(bgp, &bn->p, afi, safi,
3375 aggregate);
3376 }
3377 }
3378 }
3379
3380 /* For redistribute route-map updates. */
3381 for (afi = AFI_IP; afi < AFI_MAX; afi++)
3382 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
3383 struct list *red_list;
3384 struct bgp_redist *red;
3385
3386 red_list = bgp->redist[afi][i];
3387 if (!red_list)
3388 continue;
3389
3390 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
3391 if (!red->rmap.name
3392 || (strcmp(rmap_name, red->rmap.name) != 0))
3393 continue;
3394
3395 if (!red->rmap.map)
3396 route_map_counter_increment(map);
3397
3398 red->rmap.map = map;
3399
3400 if (!route_update)
3401 continue;
3402
3403 if (BGP_DEBUG(zebra, ZEBRA))
3404 zlog_debug(
3405 "Processing route_map %s update on redistributed routes",
3406 rmap_name);
3407
3408 bgp_redistribute_resend(bgp, afi, i,
3409 red->instance);
3410 }
3411 }
3412
3413 /* for type5 command route-maps */
3414 FOREACH_AFI_SAFI (afi, safi) {
3415 if (!bgp->adv_cmd_rmap[afi][safi].name
3416 || strcmp(rmap_name, bgp->adv_cmd_rmap[afi][safi].name)
3417 != 0)
3418 continue;
3419
3420 /* Make sure the route-map is populated here if not already done */
3421 bgp->adv_cmd_rmap[afi][safi].map = map;
3422
3423 if (BGP_DEBUG(zebra, ZEBRA))
3424 zlog_debug(
3425 "Processing route_map %s update on advertise type5 route command",
3426 rmap_name);
3427
3428 if (route_update && advertise_type5_routes(bgp, afi)) {
3429 bgp_evpn_withdraw_type5_routes(bgp, afi, safi);
3430 bgp_evpn_advertise_type5_routes(bgp, afi, safi);
3431 }
3432 }
3433 }
3434
3435 static void bgp_route_map_process_update_cb(char *rmap_name)
3436 {
3437 struct listnode *node, *nnode;
3438 struct bgp *bgp;
3439
3440 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
3441 bgp_route_map_process_update(bgp, rmap_name, 1);
3442
3443 #if ENABLE_BGP_VNC
3444 /* zlog_debug("%s: calling vnc_routemap_update", __func__); */
3445 vnc_routemap_update(bgp, __func__);
3446 #endif
3447 }
3448
3449 vpn_policy_routemap_event(rmap_name);
3450 }
3451
3452 int bgp_route_map_update_timer(struct thread *thread)
3453 {
3454 bm->t_rmap_update = NULL;
3455
3456 route_map_walk_update_list(bgp_route_map_process_update_cb);
3457
3458 return (0);
3459 }
3460
3461 static void bgp_route_map_mark_update(const char *rmap_name)
3462 {
3463 struct listnode *node, *nnode;
3464 struct bgp *bgp;
3465
3466 /* If new update is received before the current timer timed out,
3467 * turn it off and start a new timer.
3468 */
3469 if (bm->t_rmap_update != NULL)
3470 THREAD_OFF(bm->t_rmap_update);
3471
3472 /* rmap_update_timer of 0 means don't do route updates */
3473 if (bm->rmap_update_timer) {
3474 thread_add_timer(bm->master, bgp_route_map_update_timer,
3475 NULL, bm->rmap_update_timer,
3476 &bm->t_rmap_update);
3477
3478 /* Signal the groups that a route-map update event has
3479 * started */
3480 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3481 update_group_policy_update(bgp,
3482 BGP_POLICY_ROUTE_MAP,
3483 rmap_name, 1, 1);
3484 } else {
3485 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
3486 bgp_route_map_process_update(bgp, rmap_name, 0);
3487 #if ENABLE_BGP_VNC
3488 zlog_debug("%s: calling vnc_routemap_update", __func__);
3489 vnc_routemap_update(bgp, __func__);
3490 #endif
3491 }
3492 }
3493
3494 static void bgp_route_map_add(const char *rmap_name)
3495 {
3496 if (route_map_mark_updated(rmap_name) == 0)
3497 bgp_route_map_mark_update(rmap_name);
3498
3499 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
3500 }
3501
3502 static void bgp_route_map_delete(const char *rmap_name)
3503 {
3504 if (route_map_mark_updated(rmap_name) == 0)
3505 bgp_route_map_mark_update(rmap_name);
3506
3507 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_DELETED);
3508 }
3509
3510 static void bgp_route_map_event(const char *rmap_name)
3511 {
3512 if (route_map_mark_updated(rmap_name) == 0)
3513 bgp_route_map_mark_update(rmap_name);
3514
3515 route_map_notify_dependencies(rmap_name, RMAP_EVENT_MATCH_ADDED);
3516 }
3517
3518 DEFUN (match_mac_address,
3519 match_mac_address_cmd,
3520 "match mac address WORD",
3521 MATCH_STR
3522 "mac address\n"
3523 "Match address of route\n"
3524 "MAC Access-list name\n")
3525 {
3526 return bgp_route_match_add(vty, "mac address", argv[3]->arg,
3527 RMAP_EVENT_FILTER_ADDED);
3528 }
3529
3530 DEFUN (no_match_mac_address,
3531 no_match_mac_address_cmd,
3532 "no match mac address WORD",
3533 NO_STR
3534 MATCH_STR
3535 "mac\n"
3536 "Match address of route\n"
3537 "MAC acess-list name\n")
3538 {
3539 return bgp_route_match_delete(vty, "mac address", argv[4]->arg,
3540 RMAP_EVENT_FILTER_DELETED);
3541 }
3542
3543 DEFUN (match_evpn_route_type,
3544 match_evpn_route_type_cmd,
3545 "match evpn route-type <macip | multicast | prefix>",
3546 MATCH_STR
3547 EVPN_HELP_STR
3548 "Match route-type\n"
3549 "mac-ip route\n"
3550 "IMET route\n"
3551 "prefix route\n")
3552 {
3553 return bgp_route_match_add(vty, "evpn route-type", argv[3]->arg,
3554 RMAP_EVENT_MATCH_ADDED);
3555 }
3556
3557 DEFUN (no_match_evpn_route_type,
3558 no_match_evpn_route_type_cmd,
3559 "no match evpn route-type <macip | multicast | prefix>",
3560 NO_STR
3561 MATCH_STR
3562 EVPN_HELP_STR
3563 "Match route-type\n"
3564 "mac-ip route\n"
3565 "IMET route\n"
3566 "prefix route\n")
3567 {
3568 return bgp_route_match_delete(vty, "evpn route-type", argv[4]->arg,
3569 RMAP_EVENT_MATCH_DELETED);
3570 }
3571
3572
3573 DEFUN (match_evpn_vni,
3574 match_evpn_vni_cmd,
3575 "match evpn vni " CMD_VNI_RANGE,
3576 MATCH_STR
3577 EVPN_HELP_STR
3578 "Match VNI\n"
3579 "VNI ID\n")
3580 {
3581 return bgp_route_match_add(vty, "evpn vni", argv[3]->arg,
3582 RMAP_EVENT_MATCH_ADDED);
3583 }
3584
3585 DEFUN (no_match_evpn_vni,
3586 no_match_evpn_vni_cmd,
3587 "no match evpn vni " CMD_VNI_RANGE,
3588 NO_STR
3589 MATCH_STR
3590 EVPN_HELP_STR
3591 "Match VNI\n"
3592 "VNI ID\n")
3593 {
3594 return bgp_route_match_delete(vty, "evpn vni", argv[4]->arg,
3595 RMAP_EVENT_MATCH_DELETED);
3596 }
3597
3598 DEFUN (match_evpn_default_route,
3599 match_evpn_default_route_cmd,
3600 "match evpn default-route",
3601 MATCH_STR
3602 EVPN_HELP_STR
3603 "default EVPN type-5 route\n")
3604 {
3605 return bgp_route_match_add(vty, "evpn default-route", NULL,
3606 RMAP_EVENT_MATCH_ADDED);
3607 }
3608
3609 DEFUN (no_match_evpn_default_route,
3610 no_match_evpn_default_route_cmd,
3611 "no match evpn default-route",
3612 NO_STR
3613 MATCH_STR
3614 EVPN_HELP_STR
3615 "default EVPN type-5 route\n")
3616 {
3617 return bgp_route_match_delete(vty, "evpn default-route", NULL,
3618 RMAP_EVENT_MATCH_DELETED);
3619 }
3620
3621 DEFPY(match_vrl_source_vrf,
3622 match_vrl_source_vrf_cmd,
3623 "match source-vrf NAME$vrf_name",
3624 MATCH_STR
3625 "source vrf\n"
3626 "The VRF name\n")
3627 {
3628 return bgp_route_match_add(vty, "source-vrf", vrf_name,
3629 RMAP_EVENT_MATCH_ADDED);
3630 }
3631
3632 DEFPY(no_match_vrl_source_vrf,
3633 no_match_vrl_source_vrf_cmd,
3634 "no match source-vrf NAME$vrf_name",
3635 NO_STR
3636 MATCH_STR
3637 "source vrf\n"
3638 "The VRF name\n")
3639 {
3640 return bgp_route_match_delete(vty, "source-vrf", vrf_name,
3641 RMAP_EVENT_MATCH_DELETED);
3642 }
3643
3644 DEFUN (match_peer,
3645 match_peer_cmd,
3646 "match peer <A.B.C.D|X:X::X:X|WORD>",
3647 MATCH_STR
3648 "Match peer address\n"
3649 "IP address of peer\n"
3650 "IPv6 address of peer\n"
3651 "Interface name of peer\n")
3652 {
3653 int idx_ip = 2;
3654 return bgp_route_match_add(vty, "peer", argv[idx_ip]->arg,
3655 RMAP_EVENT_MATCH_ADDED);
3656 }
3657
3658 DEFUN (match_peer_local,
3659 match_peer_local_cmd,
3660 "match peer local",
3661 MATCH_STR
3662 "Match peer address\n"
3663 "Static or Redistributed routes\n")
3664 {
3665 return bgp_route_match_add(vty, "peer", "local",
3666 RMAP_EVENT_MATCH_DELETED);
3667 }
3668
3669 DEFUN (no_match_peer,
3670 no_match_peer_cmd,
3671 "no match peer [<local|A.B.C.D|X:X::X:X|WORD>]",
3672 NO_STR
3673 MATCH_STR
3674 "Match peer address\n"
3675 "Static or Redistributed routes\n"
3676 "IP address of peer\n"
3677 "IPv6 address of peer\n"
3678 "Interface name of peer\n")
3679 {
3680 int idx_peer = 3;
3681
3682 if (argc <= idx_peer)
3683 return bgp_route_match_delete(vty, "peer", NULL,
3684 RMAP_EVENT_MATCH_DELETED);
3685 return bgp_route_match_delete(vty, "peer", argv[idx_peer]->arg,
3686 RMAP_EVENT_MATCH_DELETED);
3687 }
3688
3689 #if defined(HAVE_LUA)
3690 DEFUN (match_command,
3691 match_command_cmd,
3692 "match command WORD",
3693 MATCH_STR
3694 "Run a command to match\n"
3695 "The command to run\n")
3696 {
3697 return bgp_route_match_add(vty, "command", argv[2]->arg,
3698 RMAP_EVENT_FILTER_ADDED);
3699 }
3700
3701 DEFUN (no_match_command,
3702 no_match_command_cmd,
3703 "no match command WORD",
3704 NO_STR
3705 MATCH_STR
3706 "Run a command to match\n"
3707 "The command to run\n")
3708 {
3709 return bgp_route_match_delete(vty, "command", argv[3]->arg,
3710 RMAP_EVENT_FILTER_DELETED);
3711 }
3712 #endif
3713
3714 /* match probability */
3715 DEFUN (match_probability,
3716 match_probability_cmd,
3717 "match probability (0-100)",
3718 MATCH_STR
3719 "Match portion of routes defined by percentage value\n"
3720 "Percentage of routes\n")
3721 {
3722 int idx_number = 2;
3723 return bgp_route_match_add(vty, "probability", argv[idx_number]->arg,
3724 RMAP_EVENT_MATCH_ADDED);
3725 }
3726
3727
3728 DEFUN (no_match_probability,
3729 no_match_probability_cmd,
3730 "no match probability [(1-99)]",
3731 NO_STR
3732 MATCH_STR
3733 "Match portion of routes defined by percentage value\n"
3734 "Percentage of routes\n")
3735 {
3736 int idx_number = 3;
3737 if (argc <= idx_number)
3738 return bgp_route_match_delete(vty, "probability", NULL,
3739 RMAP_EVENT_MATCH_DELETED);
3740 return bgp_route_match_delete(vty, "probability", argv[idx_number]->arg,
3741 RMAP_EVENT_MATCH_DELETED);
3742 }
3743
3744
3745 DEFUN (match_ip_route_source,
3746 match_ip_route_source_cmd,
3747 "match ip route-source <(1-199)|(1300-2699)|WORD>",
3748 MATCH_STR
3749 IP_STR
3750 "Match advertising source address of route\n"
3751 "IP access-list number\n"
3752 "IP access-list number (expanded range)\n"
3753 "IP standard access-list name\n")
3754 {
3755 int idx_acl = 3;
3756 return bgp_route_match_add(vty, "ip route-source", argv[idx_acl]->arg,
3757 RMAP_EVENT_FILTER_ADDED);
3758 }
3759
3760
3761 DEFUN (no_match_ip_route_source,
3762 no_match_ip_route_source_cmd,
3763 "no match ip route-source [<(1-199)|(1300-2699)|WORD>]",
3764 NO_STR
3765 MATCH_STR
3766 IP_STR
3767 "Match advertising source address of route\n"
3768 "IP access-list number\n"
3769 "IP access-list number (expanded range)\n"
3770 "IP standard access-list name\n")
3771 {
3772 int idx_number = 4;
3773 if (argc <= idx_number)
3774 return bgp_route_match_delete(vty, "ip route-source", NULL,
3775 RMAP_EVENT_FILTER_DELETED);
3776 return bgp_route_match_delete(vty, "ip route-source",
3777 argv[idx_number]->arg,
3778 RMAP_EVENT_FILTER_DELETED);
3779 }
3780
3781
3782 DEFUN (match_ip_route_source_prefix_list,
3783 match_ip_route_source_prefix_list_cmd,
3784 "match ip route-source prefix-list WORD",
3785 MATCH_STR
3786 IP_STR
3787 "Match advertising source address of route\n"
3788 "Match entries of prefix-lists\n"
3789 "IP prefix-list name\n")
3790 {
3791 int idx_word = 4;
3792 return bgp_route_match_add(vty, "ip route-source prefix-list",
3793 argv[idx_word]->arg, RMAP_EVENT_PLIST_ADDED);
3794 }
3795
3796
3797 DEFUN (no_match_ip_route_source_prefix_list,
3798 no_match_ip_route_source_prefix_list_cmd,
3799 "no match ip route-source prefix-list [WORD]",
3800 NO_STR
3801 MATCH_STR
3802 IP_STR
3803 "Match advertising source address of route\n"
3804 "Match entries of prefix-lists\n"
3805 "IP prefix-list name\n")
3806 {
3807 int idx_word = 5;
3808 if (argc <= idx_word)
3809 return bgp_route_match_delete(vty,
3810 "ip route-source prefix-list",
3811 NULL, RMAP_EVENT_PLIST_DELETED);
3812 return bgp_route_match_delete(vty, "ip route-source prefix-list",
3813 argv[idx_word]->arg,
3814 RMAP_EVENT_PLIST_DELETED);
3815 }
3816
3817
3818 DEFUN (match_local_pref,
3819 match_local_pref_cmd,
3820 "match local-preference (0-4294967295)",
3821 MATCH_STR
3822 "Match local-preference of route\n"
3823 "Metric value\n")
3824 {
3825 int idx_number = 2;
3826 return bgp_route_match_add(vty, "local-preference",
3827 argv[idx_number]->arg,
3828 RMAP_EVENT_MATCH_ADDED);
3829 }
3830
3831
3832 DEFUN (no_match_local_pref,
3833 no_match_local_pref_cmd,
3834 "no match local-preference [(0-4294967295)]",
3835 NO_STR
3836 MATCH_STR
3837 "Match local preference of route\n"
3838 "Local preference value\n")
3839 {
3840 int idx_localpref = 3;
3841 if (argc <= idx_localpref)
3842 return bgp_route_match_delete(vty, "local-preference", NULL,
3843 RMAP_EVENT_MATCH_DELETED);
3844 return bgp_route_match_delete(vty, "local-preference",
3845 argv[idx_localpref]->arg,
3846 RMAP_EVENT_MATCH_DELETED);
3847 }
3848
3849
3850 DEFUN (match_community,
3851 match_community_cmd,
3852 "match community <(1-99)|(100-500)|WORD> [exact-match]",
3853 MATCH_STR
3854 "Match BGP community list\n"
3855 "Community-list number (standard)\n"
3856 "Community-list number (expanded)\n"
3857 "Community-list name\n"
3858 "Do exact matching of communities\n")
3859 {
3860 int idx_comm_list = 2;
3861 int ret;
3862 char *argstr;
3863
3864 if (argc == 4) {
3865 argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
3866 strlen(argv[idx_comm_list]->arg)
3867 + strlen("exact-match") + 2);
3868
3869 sprintf(argstr, "%s exact-match", argv[idx_comm_list]->arg);
3870 } else
3871 argstr = argv[idx_comm_list]->arg;
3872
3873 ret = bgp_route_match_add(vty, "community", argstr,
3874 RMAP_EVENT_CLIST_ADDED);
3875
3876 if (argstr != argv[idx_comm_list]->arg)
3877 XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
3878
3879 return ret;
3880 }
3881
3882 DEFUN (no_match_community,
3883 no_match_community_cmd,
3884 "no match community [<(1-99)|(100-500)|WORD> [exact-match]]",
3885 NO_STR
3886 MATCH_STR
3887 "Match BGP community list\n"
3888 "Community-list number (standard)\n"
3889 "Community-list number (expanded)\n"
3890 "Community-list name\n"
3891 "Do exact matching of communities\n")
3892 {
3893 return bgp_route_match_delete(vty, "community", NULL,
3894 RMAP_EVENT_CLIST_DELETED);
3895 }
3896
3897 DEFUN (match_lcommunity,
3898 match_lcommunity_cmd,
3899 "match large-community <(1-99)|(100-500)|WORD> [exact-match]",
3900 MATCH_STR
3901 "Match BGP large community list\n"
3902 "Large Community-list number (standard)\n"
3903 "Large Community-list number (expanded)\n"
3904 "Large Community-list name\n"
3905 "Do exact matching of communities\n")
3906 {
3907 int idx_lcomm_list = 2;
3908 int ret;
3909 char *argstr;
3910
3911 if (argc == 4) {
3912 argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
3913 strlen(argv[idx_lcomm_list]->arg)
3914 + strlen("exact-match") + 2);
3915
3916 sprintf(argstr, "%s exact-match", argv[idx_lcomm_list]->arg);
3917 } else
3918 argstr = argv[idx_lcomm_list]->arg;
3919
3920 ret = bgp_route_match_add(vty, "large-community", argstr,
3921 RMAP_EVENT_LLIST_ADDED);
3922 if (argstr != argv[idx_lcomm_list]->arg)
3923 XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
3924
3925 return ret;
3926 }
3927
3928 DEFUN (no_match_lcommunity,
3929 no_match_lcommunity_cmd,
3930 "no match large-community [<(1-99)|(100-500)|WORD> [exact-match]]",
3931 NO_STR
3932 MATCH_STR
3933 "Match BGP large community list\n"
3934 "Large Community-list number (standard)\n"
3935 "Large Community-list number (expanded)\n"
3936 "Large Community-list name\n"
3937 "Do exact matching of communities\n")
3938 {
3939 return bgp_route_match_delete(vty, "large-community", NULL,
3940 RMAP_EVENT_LLIST_DELETED);
3941 }
3942
3943 DEFUN (match_ecommunity,
3944 match_ecommunity_cmd,
3945 "match extcommunity <(1-99)|(100-500)|WORD>",
3946 MATCH_STR
3947 "Match BGP/VPN extended community list\n"
3948 "Extended community-list number (standard)\n"
3949 "Extended community-list number (expanded)\n"
3950 "Extended community-list name\n")
3951 {
3952 int idx_comm_list = 2;
3953 return bgp_route_match_add(vty, "extcommunity",
3954 argv[idx_comm_list]->arg,
3955 RMAP_EVENT_ECLIST_ADDED);
3956 }
3957
3958
3959 DEFUN (no_match_ecommunity,
3960 no_match_ecommunity_cmd,
3961 "no match extcommunity [<(1-99)|(100-500)|WORD>]",
3962 NO_STR
3963 MATCH_STR
3964 "Match BGP/VPN extended community list\n"
3965 "Extended community-list number (standard)\n"
3966 "Extended community-list number (expanded)\n"
3967 "Extended community-list name\n")
3968 {
3969 return bgp_route_match_delete(vty, "extcommunity", NULL,
3970 RMAP_EVENT_ECLIST_DELETED);
3971 }
3972
3973
3974 DEFUN (match_aspath,
3975 match_aspath_cmd,
3976 "match as-path WORD",
3977 MATCH_STR
3978 "Match BGP AS path list\n"
3979 "AS path access-list name\n")
3980 {
3981 int idx_word = 2;
3982 return bgp_route_match_add(vty, "as-path", argv[idx_word]->arg,
3983 RMAP_EVENT_ASLIST_ADDED);
3984 }
3985
3986
3987 DEFUN (no_match_aspath,
3988 no_match_aspath_cmd,
3989 "no match as-path [WORD]",
3990 NO_STR
3991 MATCH_STR
3992 "Match BGP AS path list\n"
3993 "AS path access-list name\n")
3994 {
3995 return bgp_route_match_delete(vty, "as-path", NULL,
3996 RMAP_EVENT_ASLIST_DELETED);
3997 }
3998
3999
4000 DEFUN (match_origin,
4001 match_origin_cmd,
4002 "match origin <egp|igp|incomplete>",
4003 MATCH_STR
4004 "BGP origin code\n"
4005 "remote EGP\n"
4006 "local IGP\n"
4007 "unknown heritage\n")
4008 {
4009 int idx_origin = 2;
4010 if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
4011 return bgp_route_match_add(vty, "origin", "igp",
4012 RMAP_EVENT_MATCH_ADDED);
4013 if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
4014 return bgp_route_match_add(vty, "origin", "egp",
4015 RMAP_EVENT_MATCH_ADDED);
4016 if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
4017 return bgp_route_match_add(vty, "origin", "incomplete",
4018 RMAP_EVENT_MATCH_ADDED);
4019
4020 vty_out(vty, "%% Invalid match origin type\n");
4021 return CMD_WARNING_CONFIG_FAILED;
4022 }
4023
4024
4025 DEFUN (no_match_origin,
4026 no_match_origin_cmd,
4027 "no match origin [<egp|igp|incomplete>]",
4028 NO_STR
4029 MATCH_STR
4030 "BGP origin code\n"
4031 "remote EGP\n"
4032 "local IGP\n"
4033 "unknown heritage\n")
4034 {
4035 return bgp_route_match_delete(vty, "origin", NULL,
4036 RMAP_EVENT_MATCH_DELETED);
4037 }
4038
4039 DEFUN (set_ip_nexthop_peer,
4040 set_ip_nexthop_peer_cmd,
4041 "[no] set ip next-hop peer-address",
4042 NO_STR
4043 SET_STR
4044 IP_STR
4045 "Next hop address\n"
4046 "Use peer address (for BGP only)\n")
4047 {
4048 int (*func)(struct vty *, struct route_map_index *, const char *,
4049 const char *) = strmatch(argv[0]->text, "no")
4050 ? generic_set_delete
4051 : generic_set_add;
4052
4053 return func(vty, VTY_GET_CONTEXT(route_map_index), "ip next-hop",
4054 "peer-address");
4055 }
4056
4057 DEFUN (set_ip_nexthop_unchanged,
4058 set_ip_nexthop_unchanged_cmd,
4059 "[no] set ip next-hop unchanged",
4060 NO_STR
4061 SET_STR
4062 IP_STR
4063 "Next hop address\n"
4064 "Don't modify existing Next hop address\n")
4065 {
4066 int (*func)(struct vty *, struct route_map_index *, const char *,
4067 const char *) = strmatch(argv[0]->text, "no")
4068 ? generic_set_delete
4069 : generic_set_add;
4070
4071 return func(vty, VTY_GET_CONTEXT(route_map_index), "ip next-hop",
4072 "unchanged");
4073 }
4074
4075
4076 DEFUN (set_local_pref,
4077 set_local_pref_cmd,
4078 "set local-preference (0-4294967295)",
4079 SET_STR
4080 "BGP local preference path attribute\n"
4081 "Preference value\n")
4082 {
4083 int idx_number = 2;
4084 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4085 "local-preference", argv[idx_number]->arg);
4086 }
4087
4088
4089 DEFUN (no_set_local_pref,
4090 no_set_local_pref_cmd,
4091 "no set local-preference [(0-4294967295)]",
4092 NO_STR
4093 SET_STR
4094 "BGP local preference path attribute\n"
4095 "Preference value\n")
4096 {
4097 int idx_localpref = 3;
4098 if (argc <= idx_localpref)
4099 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4100 "local-preference", NULL);
4101 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4102 "local-preference", argv[idx_localpref]->arg);
4103 }
4104
4105
4106 DEFUN (set_weight,
4107 set_weight_cmd,
4108 "set weight (0-4294967295)",
4109 SET_STR
4110 "BGP weight for routing table\n"
4111 "Weight value\n")
4112 {
4113 int idx_number = 2;
4114 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index), "weight",
4115 argv[idx_number]->arg);
4116 }
4117
4118
4119 DEFUN (no_set_weight,
4120 no_set_weight_cmd,
4121 "no set weight [(0-4294967295)]",
4122 NO_STR
4123 SET_STR
4124 "BGP weight for routing table\n"
4125 "Weight value\n")
4126 {
4127 int idx_weight = 3;
4128 if (argc <= idx_weight)
4129 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4130 "weight", NULL);
4131 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4132 "weight", argv[idx_weight]->arg);
4133 }
4134
4135 DEFUN (set_label_index,
4136 set_label_index_cmd,
4137 "set label-index (0-1048560)",
4138 SET_STR
4139 "Label index to associate with the prefix\n"
4140 "Label index value\n")
4141 {
4142 int idx_number = 2;
4143 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4144 "label-index", argv[idx_number]->arg);
4145 }
4146
4147 DEFUN (no_set_label_index,
4148 no_set_label_index_cmd,
4149 "no set label-index [(0-1048560)]",
4150 NO_STR
4151 SET_STR
4152 "Label index to associate with the prefix\n"
4153 "Label index value\n")
4154 {
4155 int idx_label_index = 3;
4156 if (argc <= idx_label_index)
4157 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4158 "label-index", NULL);
4159 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4160 "label-index", argv[idx_label_index]->arg);
4161 }
4162
4163 DEFUN (set_aspath_prepend_asn,
4164 set_aspath_prepend_asn_cmd,
4165 "set as-path prepend (1-4294967295)...",
4166 SET_STR
4167 "Transform BGP AS_PATH attribute\n"
4168 "Prepend to the as-path\n"
4169 "AS number\n")
4170 {
4171 int idx_asn = 3;
4172 int ret;
4173 char *str;
4174
4175 str = argv_concat(argv, argc, idx_asn);
4176 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4177 "as-path prepend", str);
4178 XFREE(MTYPE_TMP, str);
4179
4180 return ret;
4181 }
4182
4183 DEFUN (set_aspath_prepend_lastas,
4184 set_aspath_prepend_lastas_cmd,
4185 "set as-path prepend last-as (1-10)",
4186 SET_STR
4187 "Transform BGP AS_PATH attribute\n"
4188 "Prepend to the as-path\n"
4189 "Use the peer's AS-number\n"
4190 "Number of times to insert\n")
4191 {
4192 return set_aspath_prepend_asn(self, vty, argc, argv);
4193 }
4194
4195 DEFUN (no_set_aspath_prepend,
4196 no_set_aspath_prepend_cmd,
4197 "no set as-path prepend [(1-4294967295)]",
4198 NO_STR
4199 SET_STR
4200 "Transform BGP AS_PATH attribute\n"
4201 "Prepend to the as-path\n"
4202 "AS number\n")
4203 {
4204 int idx_asn = 4;
4205 int ret;
4206 char *str;
4207
4208 str = argv_concat(argv, argc, idx_asn);
4209 ret = generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4210 "as-path prepend", str);
4211 XFREE(MTYPE_TMP, str);
4212 return ret;
4213 }
4214
4215 DEFUN (no_set_aspath_prepend_lastas,
4216 no_set_aspath_prepend_lastas_cmd,
4217 "no set as-path prepend last-as [(1-10)]",
4218 NO_STR
4219 SET_STR
4220 "Transform BGP AS_PATH attribute\n"
4221 "Prepend to the as-path\n"
4222 "Use the peers AS-number\n"
4223 "Number of times to insert\n")
4224 {
4225 return no_set_aspath_prepend(self, vty, argc, argv);
4226 }
4227
4228 DEFUN (set_aspath_exclude,
4229 set_aspath_exclude_cmd,
4230 "set as-path exclude (1-4294967295)...",
4231 SET_STR
4232 "Transform BGP AS-path attribute\n"
4233 "Exclude from the as-path\n"
4234 "AS number\n")
4235 {
4236 int idx_asn = 3;
4237 int ret;
4238 char *str;
4239
4240 str = argv_concat(argv, argc, idx_asn);
4241 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4242 "as-path exclude", str);
4243 XFREE(MTYPE_TMP, str);
4244 return ret;
4245 }
4246
4247 DEFUN (no_set_aspath_exclude,
4248 no_set_aspath_exclude_cmd,
4249 "no set as-path exclude (1-4294967295)...",
4250 NO_STR
4251 SET_STR
4252 "Transform BGP AS_PATH attribute\n"
4253 "Exclude from the as-path\n"
4254 "AS number\n")
4255 {
4256 int idx_asn = 4;
4257 int ret;
4258 char *str;
4259
4260 str = argv_concat(argv, argc, idx_asn);
4261 ret = generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4262 "as-path exclude", str);
4263 XFREE(MTYPE_TMP, str);
4264 return ret;
4265 }
4266
4267 ALIAS(no_set_aspath_exclude, no_set_aspath_exclude_all_cmd,
4268 "no set as-path exclude",
4269 NO_STR SET_STR
4270 "Transform BGP AS_PATH attribute\n"
4271 "Exclude from the as-path\n")
4272
4273 DEFUN (set_community,
4274 set_community_cmd,
4275 "set community AA:NN...",
4276 SET_STR
4277 "BGP community attribute\n"
4278 COMMUNITY_VAL_STR)
4279 {
4280 int idx_aa_nn = 2;
4281 int i;
4282 int first = 0;
4283 int additive = 0;
4284 struct buffer *b;
4285 struct community *com = NULL;
4286 char *str;
4287 char *argstr;
4288 int ret;
4289
4290 b = buffer_new(1024);
4291
4292 for (i = idx_aa_nn; i < argc; i++) {
4293 if (strncmp(argv[i]->arg, "additive", strlen(argv[i]->arg))
4294 == 0) {
4295 additive = 1;
4296 continue;
4297 }
4298
4299 if (first)
4300 buffer_putc(b, ' ');
4301 else
4302 first = 1;
4303
4304 if (strncmp(argv[i]->arg, "internet", strlen(argv[i]->arg))
4305 == 0) {
4306 buffer_putstr(b, "internet");
4307 continue;
4308 }
4309 if (strncmp(argv[i]->arg, "local-AS", strlen(argv[i]->arg))
4310 == 0) {
4311 buffer_putstr(b, "local-AS");
4312 continue;
4313 }
4314 if (strncmp(argv[i]->arg, "no-a", strlen("no-a")) == 0
4315 && strncmp(argv[i]->arg, "no-advertise",
4316 strlen(argv[i]->arg))
4317 == 0) {
4318 buffer_putstr(b, "no-advertise");
4319 continue;
4320 }
4321 if (strncmp(argv[i]->arg, "no-e", strlen("no-e")) == 0
4322 && strncmp(argv[i]->arg, "no-export", strlen(argv[i]->arg))
4323 == 0) {
4324 buffer_putstr(b, "no-export");
4325 continue;
4326 }
4327 if (strncmp(argv[i]->arg, "graceful-shutdown",
4328 strlen(argv[i]->arg))
4329 == 0) {
4330 buffer_putstr(b, "graceful-shutdown");
4331 continue;
4332 }
4333 buffer_putstr(b, argv[i]->arg);
4334 }
4335 buffer_putc(b, '\0');
4336
4337 /* Fetch result string then compile it to communities attribute. */
4338 str = buffer_getstr(b);
4339 buffer_free(b);
4340
4341 if (str) {
4342 com = community_str2com(str);
4343 XFREE(MTYPE_TMP, str);
4344 }
4345
4346 /* Can't compile user input into communities attribute. */
4347 if (!com) {
4348 vty_out(vty, "%% Malformed communities attribute\n");
4349 return CMD_WARNING_CONFIG_FAILED;
4350 }
4351
4352 /* Set communites attribute string. */
4353 str = community_str(com, false);
4354
4355 if (additive) {
4356 size_t argstr_sz = strlen(str) + strlen(" additive") + 1;
4357 argstr = XCALLOC(MTYPE_TMP, argstr_sz);
4358 strlcpy(argstr, str, argstr_sz);
4359 strlcat(argstr, " additive", argstr_sz);
4360 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4361 "community", argstr);
4362 XFREE(MTYPE_TMP, argstr);
4363 } else
4364 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4365 "community", str);
4366
4367 community_free(&com);
4368
4369 return ret;
4370 }
4371
4372 DEFUN (set_community_none,
4373 set_community_none_cmd,
4374 "set community none",
4375 SET_STR
4376 "BGP community attribute\n"
4377 "No community attribute\n")
4378 {
4379 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4380 "community", "none");
4381 }
4382
4383 DEFUN (no_set_community,
4384 no_set_community_cmd,
4385 "no set community AA:NN...",
4386 NO_STR
4387 SET_STR
4388 "BGP community attribute\n"
4389 COMMUNITY_VAL_STR)
4390 {
4391 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4392 "community", NULL);
4393 }
4394
4395 ALIAS (no_set_community,
4396 no_set_community_short_cmd,
4397 "no set community",
4398 NO_STR
4399 SET_STR
4400 "BGP community attribute\n")
4401
4402
4403 DEFUN (set_community_delete,
4404 set_community_delete_cmd,
4405 "set comm-list <(1-99)|(100-500)|WORD> delete",
4406 SET_STR
4407 "set BGP community list (for deletion)\n"
4408 "Community-list number (standard)\n"
4409 "Community-list number (expanded)\n"
4410 "Community-list name\n"
4411 "Delete matching communities\n")
4412 {
4413 int idx_comm_list = 2;
4414 char *args;
4415
4416 args = argv_concat(argv, argc, idx_comm_list);
4417 generic_set_add(vty, VTY_GET_CONTEXT(route_map_index), "comm-list",
4418 args);
4419 XFREE(MTYPE_TMP, args);
4420
4421 return CMD_SUCCESS;
4422 }
4423
4424 DEFUN (no_set_community_delete,
4425 no_set_community_delete_cmd,
4426 "no set comm-list [<(1-99)|(100-500)|WORD> delete]",
4427 NO_STR
4428 SET_STR
4429 "set BGP community list (for deletion)\n"
4430 "Community-list number (standard)\n"
4431 "Community-list number (expanded)\n"
4432 "Community-list name\n"
4433 "Delete matching communities\n")
4434 {
4435 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4436 "comm-list", NULL);
4437 }
4438
4439 DEFUN (set_lcommunity,
4440 set_lcommunity_cmd,
4441 "set large-community AA:BB:CC...",
4442 SET_STR
4443 "BGP large community attribute\n"
4444 "Large Community number in aa:bb:cc format or additive\n")
4445 {
4446 int ret;
4447 char *str;
4448
4449 str = argv_concat(argv, argc, 2);
4450 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4451 "large-community", str);
4452 XFREE(MTYPE_TMP, str);
4453
4454 return ret;
4455 }
4456
4457 DEFUN (set_lcommunity_none,
4458 set_lcommunity_none_cmd,
4459 "set large-community none",
4460 SET_STR
4461 "BGP large community attribute\n"
4462 "No large community attribute\n")
4463 {
4464 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4465 "large-community", "none");
4466 }
4467
4468 DEFUN (no_set_lcommunity,
4469 no_set_lcommunity_cmd,
4470 "no set large-community none",
4471 NO_STR
4472 SET_STR
4473 "BGP large community attribute\n"
4474 "No community attribute\n")
4475 {
4476 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4477 "large-community", NULL);
4478 }
4479
4480 DEFUN (no_set_lcommunity1,
4481 no_set_lcommunity1_cmd,
4482 "no set large-community AA:BB:CC...",
4483 NO_STR
4484 SET_STR
4485 "BGP large community attribute\n"
4486 "Large community in AA:BB:CC... format or additive\n")
4487 {
4488 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4489 "large-community", NULL);
4490 }
4491
4492 ALIAS (no_set_lcommunity1,
4493 no_set_lcommunity1_short_cmd,
4494 "no set large-community",
4495 NO_STR
4496 SET_STR
4497 "BGP large community attribute\n")
4498
4499 DEFUN (set_lcommunity_delete,
4500 set_lcommunity_delete_cmd,
4501 "set large-comm-list <(1-99)|(100-500)|WORD> delete",
4502 SET_STR
4503 "set BGP large community list (for deletion)\n"
4504 "Large Community-list number (standard)\n"
4505 "Large Communitly-list number (expanded)\n"
4506 "Large Community-list name\n"
4507 "Delete matching large communities\n")
4508 {
4509 int idx_lcomm_list = 2;
4510 char *args;
4511
4512 args = argv_concat(argv, argc, idx_lcomm_list);
4513 generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4514 "large-comm-list", args);
4515 XFREE(MTYPE_TMP, args);
4516
4517 return CMD_SUCCESS;
4518 }
4519
4520 DEFUN (no_set_lcommunity_delete,
4521 no_set_lcommunity_delete_cmd,
4522 "no set large-comm-list <(1-99)|(100-500)|WORD> [delete]",
4523 NO_STR
4524 SET_STR
4525 "set BGP large community list (for deletion)\n"
4526 "Large Community-list number (standard)\n"
4527 "Large Communitly-list number (expanded)\n"
4528 "Large Community-list name\n"
4529 "Delete matching large communities\n")
4530 {
4531 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4532 "large-comm-list", NULL);
4533 }
4534
4535 ALIAS (no_set_lcommunity_delete,
4536 no_set_lcommunity_delete_short_cmd,
4537 "no set large-comm-list",
4538 NO_STR
4539 SET_STR
4540 "set BGP large community list (for deletion)\n")
4541
4542 DEFUN (set_ecommunity_rt,
4543 set_ecommunity_rt_cmd,
4544 "set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
4545 SET_STR
4546 "BGP extended community attribute\n"
4547 "Route Target extended community\n"
4548 "VPN extended community\n")
4549 {
4550 int idx_asn_nn = 3;
4551 int ret;
4552 char *str;
4553
4554 str = argv_concat(argv, argc, idx_asn_nn);
4555 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4556 "extcommunity rt", str);
4557 XFREE(MTYPE_TMP, str);
4558
4559 return ret;
4560 }
4561
4562 DEFUN (no_set_ecommunity_rt,
4563 no_set_ecommunity_rt_cmd,
4564 "no set extcommunity rt ASN:NN_OR_IP-ADDRESS:NN...",
4565 NO_STR
4566 SET_STR
4567 "BGP extended community attribute\n"
4568 "Route Target extended community\n"
4569 "VPN extended community\n")
4570 {
4571 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4572 "extcommunity rt", NULL);
4573 }
4574
4575 ALIAS (no_set_ecommunity_rt,
4576 no_set_ecommunity_rt_short_cmd,
4577 "no set extcommunity rt",
4578 NO_STR
4579 SET_STR
4580 "BGP extended community attribute\n"
4581 "Route Target extended community\n")
4582
4583 DEFUN (set_ecommunity_soo,
4584 set_ecommunity_soo_cmd,
4585 "set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
4586 SET_STR
4587 "BGP extended community attribute\n"
4588 "Site-of-Origin extended community\n"
4589 "VPN extended community\n")
4590 {
4591 int idx_asn_nn = 3;
4592 int ret;
4593 char *str;
4594
4595 str = argv_concat(argv, argc, idx_asn_nn);
4596 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4597 "extcommunity soo", str);
4598 XFREE(MTYPE_TMP, str);
4599 return ret;
4600 }
4601
4602
4603 DEFUN (no_set_ecommunity_soo,
4604 no_set_ecommunity_soo_cmd,
4605 "no set extcommunity soo ASN:NN_OR_IP-ADDRESS:NN...",
4606 NO_STR
4607 SET_STR
4608 "BGP extended community attribute\n"
4609 "Site-of-Origin extended community\n"
4610 "VPN extended community\n")
4611 {
4612 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4613 "extcommunity soo", NULL);
4614 }
4615
4616 ALIAS (no_set_ecommunity_soo,
4617 no_set_ecommunity_soo_short_cmd,
4618 "no set extcommunity soo",
4619 NO_STR
4620 SET_STR
4621 "GP extended community attribute\n"
4622 "Site-of-Origin extended community\n")
4623
4624 DEFUN (set_origin,
4625 set_origin_cmd,
4626 "set origin <egp|igp|incomplete>",
4627 SET_STR
4628 "BGP origin code\n"
4629 "remote EGP\n"
4630 "local IGP\n"
4631 "unknown heritage\n")
4632 {
4633 int idx_origin = 2;
4634 if (strncmp(argv[idx_origin]->arg, "igp", 2) == 0)
4635 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4636 "origin", "igp");
4637 if (strncmp(argv[idx_origin]->arg, "egp", 1) == 0)
4638 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4639 "origin", "egp");
4640 if (strncmp(argv[idx_origin]->arg, "incomplete", 2) == 0)
4641 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4642 "origin", "incomplete");
4643
4644 vty_out(vty, "%% Invalid set origin type\n");
4645 return CMD_WARNING_CONFIG_FAILED;
4646 }
4647
4648
4649 DEFUN (no_set_origin,
4650 no_set_origin_cmd,
4651 "no set origin [<egp|igp|incomplete>]",
4652 NO_STR
4653 SET_STR
4654 "BGP origin code\n"
4655 "remote EGP\n"
4656 "local IGP\n"
4657 "unknown heritage\n")
4658 {
4659 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4660 "origin", NULL);
4661 }
4662
4663
4664 DEFUN (set_atomic_aggregate,
4665 set_atomic_aggregate_cmd,
4666 "set atomic-aggregate",
4667 SET_STR
4668 "BGP atomic aggregate attribute\n" )
4669 {
4670 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4671 "atomic-aggregate", NULL);
4672 }
4673
4674 DEFUN (no_set_atomic_aggregate,
4675 no_set_atomic_aggregate_cmd,
4676 "no set atomic-aggregate",
4677 NO_STR
4678 SET_STR
4679 "BGP atomic aggregate attribute\n" )
4680 {
4681 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4682 "atomic-aggregate", NULL);
4683 }
4684
4685 DEFUN (set_aggregator_as,
4686 set_aggregator_as_cmd,
4687 "set aggregator as (1-4294967295) A.B.C.D",
4688 SET_STR
4689 "BGP aggregator attribute\n"
4690 "AS number of aggregator\n"
4691 "AS number\n"
4692 "IP address of aggregator\n")
4693 {
4694 int idx_number = 3;
4695 int idx_ipv4 = 4;
4696 int ret;
4697 struct in_addr address;
4698 char *argstr;
4699
4700 ret = inet_aton(argv[idx_ipv4]->arg, &address);
4701 if (ret == 0) {
4702 vty_out(vty, "Aggregator IP address is invalid\n");
4703 return CMD_WARNING_CONFIG_FAILED;
4704 }
4705
4706 argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
4707 strlen(argv[idx_number]->arg)
4708 + strlen(argv[idx_ipv4]->arg) + 2);
4709
4710 sprintf(argstr, "%s %s", argv[idx_number]->arg, argv[idx_ipv4]->arg);
4711
4712 ret = generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4713 "aggregator as", argstr);
4714
4715 XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
4716
4717 return ret;
4718 }
4719
4720
4721 DEFUN (no_set_aggregator_as,
4722 no_set_aggregator_as_cmd,
4723 "no set aggregator as [(1-4294967295) A.B.C.D]",
4724 NO_STR
4725 SET_STR
4726 "BGP aggregator attribute\n"
4727 "AS number of aggregator\n"
4728 "AS number\n"
4729 "IP address of aggregator\n")
4730 {
4731 int idx_asn = 4;
4732 int idx_ip = 5;
4733 int ret;
4734 struct in_addr address;
4735 char *argstr;
4736
4737 if (argc <= idx_asn)
4738 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4739 "aggregator as", NULL);
4740
4741 ret = inet_aton(argv[idx_ip]->arg, &address);
4742 if (ret == 0) {
4743 vty_out(vty, "Aggregator IP address is invalid\n");
4744 return CMD_WARNING_CONFIG_FAILED;
4745 }
4746
4747 argstr = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
4748 strlen(argv[idx_asn]->arg) + strlen(argv[idx_ip]->arg)
4749 + 2);
4750
4751 sprintf(argstr, "%s %s", argv[idx_asn]->arg, argv[idx_ip]->arg);
4752
4753 ret = generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4754 "aggregator as", argstr);
4755
4756 XFREE(MTYPE_ROUTE_MAP_COMPILED, argstr);
4757
4758 return ret;
4759 }
4760
4761 DEFUN (match_ipv6_next_hop,
4762 match_ipv6_next_hop_cmd,
4763 "match ipv6 next-hop X:X::X:X",
4764 MATCH_STR
4765 IPV6_STR
4766 "Match IPv6 next-hop address of route\n"
4767 "IPv6 address of next hop\n")
4768 {
4769 int idx_ipv6 = 3;
4770 return bgp_route_match_add(vty, "ipv6 next-hop", argv[idx_ipv6]->arg,
4771 RMAP_EVENT_MATCH_ADDED);
4772 }
4773
4774 DEFUN (no_match_ipv6_next_hop,
4775 no_match_ipv6_next_hop_cmd,
4776 "no match ipv6 next-hop X:X::X:X",
4777 NO_STR
4778 MATCH_STR
4779 IPV6_STR
4780 "Match IPv6 next-hop address of route\n"
4781 "IPv6 address of next hop\n")
4782 {
4783 int idx_ipv6 = 4;
4784 return bgp_route_match_delete(vty, "ipv6 next-hop", argv[idx_ipv6]->arg,
4785 RMAP_EVENT_MATCH_DELETED);
4786 }
4787
4788
4789 DEFUN (set_ipv6_nexthop_peer,
4790 set_ipv6_nexthop_peer_cmd,
4791 "set ipv6 next-hop peer-address",
4792 SET_STR
4793 IPV6_STR
4794 "Next hop address\n"
4795 "Use peer address (for BGP only)\n")
4796 {
4797 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4798 "ipv6 next-hop peer-address", NULL);
4799 }
4800
4801 DEFUN (no_set_ipv6_nexthop_peer,
4802 no_set_ipv6_nexthop_peer_cmd,
4803 "no set ipv6 next-hop peer-address",
4804 NO_STR
4805 SET_STR
4806 IPV6_STR
4807 "IPv6 next-hop address\n"
4808 "Use peer address (for BGP only)\n")
4809 {
4810 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4811 "ipv6 next-hop peer-address", NULL);
4812 }
4813
4814 DEFUN (set_ipv6_nexthop_prefer_global,
4815 set_ipv6_nexthop_prefer_global_cmd,
4816 "set ipv6 next-hop prefer-global",
4817 SET_STR
4818 IPV6_STR
4819 "IPv6 next-hop address\n"
4820 "Prefer global over link-local if both exist\n")
4821 {
4822 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4823 "ipv6 next-hop prefer-global", NULL);
4824 ;
4825 }
4826
4827 DEFUN (no_set_ipv6_nexthop_prefer_global,
4828 no_set_ipv6_nexthop_prefer_global_cmd,
4829 "no set ipv6 next-hop prefer-global",
4830 NO_STR
4831 SET_STR
4832 IPV6_STR
4833 "IPv6 next-hop address\n"
4834 "Prefer global over link-local if both exist\n")
4835 {
4836 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4837 "ipv6 next-hop prefer-global", NULL);
4838 }
4839
4840 DEFUN (set_ipv6_nexthop_global,
4841 set_ipv6_nexthop_global_cmd,
4842 "set ipv6 next-hop global X:X::X:X",
4843 SET_STR
4844 IPV6_STR
4845 "IPv6 next-hop address\n"
4846 "IPv6 global address\n"
4847 "IPv6 address of next hop\n")
4848 {
4849 int idx_ipv6 = 4;
4850 struct in6_addr addr;
4851 int ret;
4852
4853 ret = inet_pton(AF_INET6, argv[idx_ipv6]->arg, &addr);
4854 if (!ret) {
4855 vty_out(vty, "%% Malformed nexthop address\n");
4856 return CMD_WARNING_CONFIG_FAILED;
4857 }
4858 if (IN6_IS_ADDR_UNSPECIFIED(&addr) || IN6_IS_ADDR_LOOPBACK(&addr)
4859 || IN6_IS_ADDR_MULTICAST(&addr) || IN6_IS_ADDR_LINKLOCAL(&addr)) {
4860 vty_out(vty, "%% Invalid global nexthop address\n");
4861 return CMD_WARNING_CONFIG_FAILED;
4862 }
4863
4864 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
4865 "ipv6 next-hop global", argv[idx_ipv6]->arg);
4866 }
4867
4868
4869 DEFUN (no_set_ipv6_nexthop_global,
4870 no_set_ipv6_nexthop_global_cmd,
4871 "no set ipv6 next-hop global X:X::X:X",
4872 NO_STR
4873 SET_STR
4874 IPV6_STR
4875 "IPv6 next-hop address\n"
4876 "IPv6 global address\n"
4877 "IPv6 address of next hop\n")
4878 {
4879 int idx_ipv6 = 5;
4880 if (argc <= idx_ipv6)
4881 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4882 "ipv6 next-hop global", NULL);
4883 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
4884 "ipv6 next-hop global", argv[idx_ipv6]->arg);
4885 }
4886
4887 #ifdef KEEP_OLD_VPN_COMMANDS
4888 DEFUN (set_vpn_nexthop,
4889 set_vpn_nexthop_cmd,
4890 "set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
4891 SET_STR
4892 "VPNv4 information\n"
4893 "VPN next-hop address\n"
4894 "IP address of next hop\n"
4895 "VPNv6 information\n"
4896 "VPN next-hop address\n"
4897 "IPv6 address of next hop\n")
4898 {
4899 int idx_ip = 3;
4900 afi_t afi;
4901 int idx = 0;
4902
4903 if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
4904 if (afi == AFI_IP)
4905 return generic_set_add(
4906 vty, VTY_GET_CONTEXT(route_map_index),
4907 "ipv4 vpn next-hop", argv[idx_ip]->arg);
4908 else
4909 return generic_set_add(
4910 vty, VTY_GET_CONTEXT(route_map_index),
4911 "ipv6 vpn next-hop", argv[idx_ip]->arg);
4912 }
4913 return CMD_SUCCESS;
4914 }
4915
4916 DEFUN (no_set_vpn_nexthop,
4917 no_set_vpn_nexthop_cmd,
4918 "no set <vpnv4 next-hop A.B.C.D|vpnv6 next-hop X:X::X:X>",
4919 NO_STR
4920 SET_STR
4921 "VPNv4 information\n"
4922 "VPN next-hop address\n"
4923 "IP address of next hop\n"
4924 "VPNv6 information\n"
4925 "VPN next-hop address\n"
4926 "IPv6 address of next hop\n")
4927 {
4928 int idx_ip = 4;
4929 char *arg;
4930 afi_t afi;
4931 int idx = 0;
4932
4933 if (argc <= idx_ip)
4934 arg = NULL;
4935 else
4936 arg = argv[idx_ip]->arg;
4937 if (argv_find_and_parse_vpnvx(argv, argc, &idx, &afi)) {
4938 if (afi == AFI_IP)
4939 return generic_set_delete(
4940 vty, VTY_GET_CONTEXT(route_map_index),
4941 "ipv4 vpn next-hop", arg);
4942 else
4943 return generic_set_delete(
4944 vty, VTY_GET_CONTEXT(route_map_index),
4945 "ipv6 vpn next-hop", argv[idx_ip]->arg);
4946 }
4947 return CMD_SUCCESS;
4948 }
4949 #endif /* KEEP_OLD_VPN_COMMANDS */
4950
4951 DEFUN (set_ipx_vpn_nexthop,
4952 set_ipx_vpn_nexthop_cmd,
4953 "set <ipv4|ipv6> vpn next-hop <A.B.C.D|X:X::X:X>",
4954 SET_STR
4955 "IPv4 information\n"
4956 "IPv6 information\n"
4957 "VPN information\n"
4958 "VPN next-hop address\n"
4959 "IP address of next hop\n"
4960 "IPv6 address of next hop\n")
4961 {
4962 int idx_ip = 4;
4963 afi_t afi;
4964 int idx = 0;
4965
4966 if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
4967 if (afi == AFI_IP)
4968 return generic_set_add(
4969 vty, VTY_GET_CONTEXT(route_map_index),
4970 "ipv4 vpn next-hop", argv[idx_ip]->arg);
4971 else
4972 return generic_set_add(
4973 vty, VTY_GET_CONTEXT(route_map_index),
4974 "ipv6 vpn next-hop", argv[idx_ip]->arg);
4975 }
4976 return CMD_SUCCESS;
4977 }
4978
4979 DEFUN (no_set_ipx_vpn_nexthop,
4980 no_set_ipx_vpn_nexthop_cmd,
4981 "no set <ipv4|ipv6> vpn next-hop [<A.B.C.D|X:X::X:X>]",
4982 NO_STR
4983 SET_STR
4984 "IPv4 information\n"
4985 "IPv6 information\n"
4986 "VPN information\n"
4987 "VPN next-hop address\n"
4988 "IP address of next hop\n"
4989 "IPv6 address of next hop\n")
4990 {
4991 int idx_ip = 5;
4992 char *arg;
4993 afi_t afi;
4994 int idx = 0;
4995
4996 if (argc <= idx_ip)
4997 arg = NULL;
4998 else
4999 arg = argv[idx_ip]->arg;
5000 if (argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
5001 if (afi == AFI_IP)
5002 return generic_set_delete(
5003 vty, VTY_GET_CONTEXT(route_map_index),
5004 "ipv4 vpn next-hop", arg);
5005 else
5006 return generic_set_delete(
5007 vty, VTY_GET_CONTEXT(route_map_index),
5008 "ipv6 vpn next-hop", arg);
5009 }
5010 return CMD_SUCCESS;
5011 }
5012
5013 DEFUN (set_originator_id,
5014 set_originator_id_cmd,
5015 "set originator-id A.B.C.D",
5016 SET_STR
5017 "BGP originator ID attribute\n"
5018 "IP address of originator\n")
5019 {
5020 int idx_ipv4 = 2;
5021 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
5022 "originator-id", argv[idx_ipv4]->arg);
5023 }
5024
5025
5026 DEFUN (no_set_originator_id,
5027 no_set_originator_id_cmd,
5028 "no set originator-id [A.B.C.D]",
5029 NO_STR
5030 SET_STR
5031 "BGP originator ID attribute\n"
5032 "IP address of originator\n")
5033 {
5034 int idx = 0;
5035 char *arg =
5036 argv_find(argv, argc, "A.B.C.D", &idx) ? argv[idx]->arg : NULL;
5037
5038 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
5039 "originator-id", arg);
5040 }
5041
5042
5043 /* Initialization of route map. */
5044 void bgp_route_map_init(void)
5045 {
5046 route_map_init();
5047
5048 route_map_add_hook(bgp_route_map_add);
5049 route_map_delete_hook(bgp_route_map_delete);
5050 route_map_event_hook(bgp_route_map_event);
5051
5052 route_map_match_interface_hook(generic_match_add);
5053 route_map_no_match_interface_hook(generic_match_delete);
5054
5055 route_map_match_ip_address_hook(generic_match_add);
5056 route_map_no_match_ip_address_hook(generic_match_delete);
5057
5058 route_map_match_ip_address_prefix_list_hook(generic_match_add);
5059 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
5060
5061 route_map_match_ip_next_hop_hook(generic_match_add);
5062 route_map_no_match_ip_next_hop_hook(generic_match_delete);
5063
5064 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
5065 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
5066
5067 route_map_match_ip_next_hop_type_hook(generic_match_add);
5068 route_map_no_match_ip_next_hop_type_hook(generic_match_delete);
5069
5070 route_map_match_ipv6_address_hook(generic_match_add);
5071 route_map_no_match_ipv6_address_hook(generic_match_delete);
5072
5073 route_map_match_ipv6_address_prefix_list_hook(generic_match_add);
5074 route_map_no_match_ipv6_address_prefix_list_hook(generic_match_delete);
5075
5076 route_map_match_ipv6_next_hop_type_hook(generic_match_add);
5077 route_map_no_match_ipv6_next_hop_type_hook(generic_match_delete);
5078
5079 route_map_match_metric_hook(generic_match_add);
5080 route_map_no_match_metric_hook(generic_match_delete);
5081
5082 route_map_match_tag_hook(generic_match_add);
5083 route_map_no_match_tag_hook(generic_match_delete);
5084
5085 route_map_set_ip_nexthop_hook(generic_set_add);
5086 route_map_no_set_ip_nexthop_hook(generic_set_delete);
5087
5088 route_map_set_ipv6_nexthop_local_hook(generic_set_add);
5089 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete);
5090
5091 route_map_set_metric_hook(generic_set_add);
5092 route_map_no_set_metric_hook(generic_set_delete);
5093
5094 route_map_set_tag_hook(generic_set_add);
5095 route_map_no_set_tag_hook(generic_set_delete);
5096
5097 route_map_install_match(&route_match_peer_cmd);
5098 route_map_install_match(&route_match_local_pref_cmd);
5099 #if defined(HAVE_LUA)
5100 route_map_install_match(&route_match_command_cmd);
5101 #endif
5102 route_map_install_match(&route_match_ip_address_cmd);
5103 route_map_install_match(&route_match_ip_next_hop_cmd);
5104 route_map_install_match(&route_match_ip_route_source_cmd);
5105 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
5106 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
5107 route_map_install_match(&route_match_ip_next_hop_type_cmd);
5108 route_map_install_match(&route_match_ip_route_source_prefix_list_cmd);
5109 route_map_install_match(&route_match_aspath_cmd);
5110 route_map_install_match(&route_match_community_cmd);
5111 route_map_install_match(&route_match_lcommunity_cmd);
5112 route_map_install_match(&route_match_ecommunity_cmd);
5113 route_map_install_match(&route_match_local_pref_cmd);
5114 route_map_install_match(&route_match_metric_cmd);
5115 route_map_install_match(&route_match_origin_cmd);
5116 route_map_install_match(&route_match_probability_cmd);
5117 route_map_install_match(&route_match_interface_cmd);
5118 route_map_install_match(&route_match_tag_cmd);
5119 route_map_install_match(&route_match_mac_address_cmd);
5120 route_map_install_match(&route_match_evpn_vni_cmd);
5121 route_map_install_match(&route_match_evpn_route_type_cmd);
5122 route_map_install_match(&route_match_evpn_default_route_cmd);
5123 route_map_install_match(&route_match_vrl_source_vrf_cmd);
5124
5125 route_map_install_set(&route_set_ip_nexthop_cmd);
5126 route_map_install_set(&route_set_local_pref_cmd);
5127 route_map_install_set(&route_set_weight_cmd);
5128 route_map_install_set(&route_set_label_index_cmd);
5129 route_map_install_set(&route_set_metric_cmd);
5130 route_map_install_set(&route_set_aspath_prepend_cmd);
5131 route_map_install_set(&route_set_aspath_exclude_cmd);
5132 route_map_install_set(&route_set_origin_cmd);
5133 route_map_install_set(&route_set_atomic_aggregate_cmd);
5134 route_map_install_set(&route_set_aggregator_as_cmd);
5135 route_map_install_set(&route_set_community_cmd);
5136 route_map_install_set(&route_set_community_delete_cmd);
5137 route_map_install_set(&route_set_lcommunity_cmd);
5138 route_map_install_set(&route_set_lcommunity_delete_cmd);
5139 route_map_install_set(&route_set_vpnv4_nexthop_cmd);
5140 route_map_install_set(&route_set_vpnv6_nexthop_cmd);
5141 route_map_install_set(&route_set_originator_id_cmd);
5142 route_map_install_set(&route_set_ecommunity_rt_cmd);
5143 route_map_install_set(&route_set_ecommunity_soo_cmd);
5144 route_map_install_set(&route_set_tag_cmd);
5145 route_map_install_set(&route_set_label_index_cmd);
5146
5147 install_element(RMAP_NODE, &match_peer_cmd);
5148 install_element(RMAP_NODE, &match_peer_local_cmd);
5149 install_element(RMAP_NODE, &no_match_peer_cmd);
5150 install_element(RMAP_NODE, &match_ip_route_source_cmd);
5151 install_element(RMAP_NODE, &no_match_ip_route_source_cmd);
5152 install_element(RMAP_NODE, &match_ip_route_source_prefix_list_cmd);
5153 install_element(RMAP_NODE, &no_match_ip_route_source_prefix_list_cmd);
5154 install_element(RMAP_NODE, &match_mac_address_cmd);
5155 install_element(RMAP_NODE, &no_match_mac_address_cmd);
5156 install_element(RMAP_NODE, &match_evpn_vni_cmd);
5157 install_element(RMAP_NODE, &no_match_evpn_vni_cmd);
5158 install_element(RMAP_NODE, &match_evpn_route_type_cmd);
5159 install_element(RMAP_NODE, &no_match_evpn_route_type_cmd);
5160 install_element(RMAP_NODE, &match_evpn_default_route_cmd);
5161 install_element(RMAP_NODE, &no_match_evpn_default_route_cmd);
5162 install_element(RMAP_NODE, &match_vrl_source_vrf_cmd);
5163 install_element(RMAP_NODE, &no_match_vrl_source_vrf_cmd);
5164
5165 install_element(RMAP_NODE, &match_aspath_cmd);
5166 install_element(RMAP_NODE, &no_match_aspath_cmd);
5167 install_element(RMAP_NODE, &match_local_pref_cmd);
5168 install_element(RMAP_NODE, &no_match_local_pref_cmd);
5169 install_element(RMAP_NODE, &match_community_cmd);
5170 install_element(RMAP_NODE, &no_match_community_cmd);
5171 install_element(RMAP_NODE, &match_lcommunity_cmd);
5172 install_element(RMAP_NODE, &no_match_lcommunity_cmd);
5173 install_element(RMAP_NODE, &match_ecommunity_cmd);
5174 install_element(RMAP_NODE, &no_match_ecommunity_cmd);
5175 install_element(RMAP_NODE, &match_origin_cmd);
5176 install_element(RMAP_NODE, &no_match_origin_cmd);
5177 install_element(RMAP_NODE, &match_probability_cmd);
5178 install_element(RMAP_NODE, &no_match_probability_cmd);
5179
5180 install_element(RMAP_NODE, &set_ip_nexthop_peer_cmd);
5181 install_element(RMAP_NODE, &set_ip_nexthop_unchanged_cmd);
5182 install_element(RMAP_NODE, &set_local_pref_cmd);
5183 install_element(RMAP_NODE, &no_set_local_pref_cmd);
5184 install_element(RMAP_NODE, &set_weight_cmd);
5185 install_element(RMAP_NODE, &set_label_index_cmd);
5186 install_element(RMAP_NODE, &no_set_weight_cmd);
5187 install_element(RMAP_NODE, &no_set_label_index_cmd);
5188 install_element(RMAP_NODE, &set_aspath_prepend_asn_cmd);
5189 install_element(RMAP_NODE, &set_aspath_prepend_lastas_cmd);
5190 install_element(RMAP_NODE, &set_aspath_exclude_cmd);
5191 install_element(RMAP_NODE, &no_set_aspath_prepend_cmd);
5192 install_element(RMAP_NODE, &no_set_aspath_prepend_lastas_cmd);
5193 install_element(RMAP_NODE, &no_set_aspath_exclude_cmd);
5194 install_element(RMAP_NODE, &no_set_aspath_exclude_all_cmd);
5195 install_element(RMAP_NODE, &set_origin_cmd);
5196 install_element(RMAP_NODE, &no_set_origin_cmd);
5197 install_element(RMAP_NODE, &set_atomic_aggregate_cmd);
5198 install_element(RMAP_NODE, &no_set_atomic_aggregate_cmd);
5199 install_element(RMAP_NODE, &set_aggregator_as_cmd);
5200 install_element(RMAP_NODE, &no_set_aggregator_as_cmd);
5201 install_element(RMAP_NODE, &set_community_cmd);
5202 install_element(RMAP_NODE, &set_community_none_cmd);
5203 install_element(RMAP_NODE, &no_set_community_cmd);
5204 install_element(RMAP_NODE, &no_set_community_short_cmd);
5205 install_element(RMAP_NODE, &set_community_delete_cmd);
5206 install_element(RMAP_NODE, &no_set_community_delete_cmd);
5207 install_element(RMAP_NODE, &set_lcommunity_cmd);
5208 install_element(RMAP_NODE, &set_lcommunity_none_cmd);
5209 install_element(RMAP_NODE, &no_set_lcommunity_cmd);
5210 install_element(RMAP_NODE, &no_set_lcommunity1_cmd);
5211 install_element(RMAP_NODE, &no_set_lcommunity1_short_cmd);
5212 install_element(RMAP_NODE, &set_lcommunity_delete_cmd);
5213 install_element(RMAP_NODE, &no_set_lcommunity_delete_cmd);
5214 install_element(RMAP_NODE, &no_set_lcommunity_delete_short_cmd);
5215 install_element(RMAP_NODE, &set_ecommunity_rt_cmd);
5216 install_element(RMAP_NODE, &no_set_ecommunity_rt_cmd);
5217 install_element(RMAP_NODE, &no_set_ecommunity_rt_short_cmd);
5218 install_element(RMAP_NODE, &set_ecommunity_soo_cmd);
5219 install_element(RMAP_NODE, &no_set_ecommunity_soo_cmd);
5220 install_element(RMAP_NODE, &no_set_ecommunity_soo_short_cmd);
5221 #ifdef KEEP_OLD_VPN_COMMANDS
5222 install_element(RMAP_NODE, &set_vpn_nexthop_cmd);
5223 install_element(RMAP_NODE, &no_set_vpn_nexthop_cmd);
5224 #endif /* KEEP_OLD_VPN_COMMANDS */
5225 install_element(RMAP_NODE, &set_ipx_vpn_nexthop_cmd);
5226 install_element(RMAP_NODE, &no_set_ipx_vpn_nexthop_cmd);
5227 install_element(RMAP_NODE, &set_originator_id_cmd);
5228 install_element(RMAP_NODE, &no_set_originator_id_cmd);
5229
5230 route_map_install_match(&route_match_ipv6_address_cmd);
5231 route_map_install_match(&route_match_ipv6_next_hop_cmd);
5232 route_map_install_match(&route_match_ipv6_address_prefix_list_cmd);
5233 route_map_install_match(&route_match_ipv6_next_hop_type_cmd);
5234 route_map_install_set(&route_set_ipv6_nexthop_global_cmd);
5235 route_map_install_set(&route_set_ipv6_nexthop_prefer_global_cmd);
5236 route_map_install_set(&route_set_ipv6_nexthop_local_cmd);
5237 route_map_install_set(&route_set_ipv6_nexthop_peer_cmd);
5238
5239 install_element(RMAP_NODE, &match_ipv6_next_hop_cmd);
5240 install_element(RMAP_NODE, &no_match_ipv6_next_hop_cmd);
5241 install_element(RMAP_NODE, &set_ipv6_nexthop_global_cmd);
5242 install_element(RMAP_NODE, &no_set_ipv6_nexthop_global_cmd);
5243 install_element(RMAP_NODE, &set_ipv6_nexthop_prefer_global_cmd);
5244 install_element(RMAP_NODE, &no_set_ipv6_nexthop_prefer_global_cmd);
5245 install_element(RMAP_NODE, &set_ipv6_nexthop_peer_cmd);
5246 install_element(RMAP_NODE, &no_set_ipv6_nexthop_peer_cmd);
5247 #if defined(HAVE_LUA)
5248 install_element(RMAP_NODE, &match_command_cmd);
5249 install_element(RMAP_NODE, &no_match_command_cmd);
5250 #endif
5251 }
5252
5253 void bgp_route_map_terminate(void)
5254 {
5255 /* ToDo: Cleanup all the used memory */
5256 route_map_finish();
5257 }