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