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