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