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