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