]> git.proxmox.com Git - mirror_frr.git/blob - lib/routemap.c
Merge remote-tracking branch 'origin/stable/2.0'
[mirror_frr.git] / lib / routemap.c
1 /* Route map function.
2 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "linklist.h"
24 #include "memory.h"
25 #include "vector.h"
26 #include "prefix.h"
27 #include "vty.h"
28 #include "routemap.h"
29 #include "command.h"
30 #include "log.h"
31 #include "log_int.h"
32 #include "hash.h"
33
34 DEFINE_MTYPE_STATIC(LIB, ROUTE_MAP, "Route map")
35 DEFINE_MTYPE( LIB, ROUTE_MAP_NAME, "Route map name")
36 DEFINE_MTYPE_STATIC(LIB, ROUTE_MAP_INDEX, "Route map index")
37 DEFINE_MTYPE( LIB, ROUTE_MAP_RULE, "Route map rule")
38 DEFINE_MTYPE_STATIC(LIB, ROUTE_MAP_RULE_STR, "Route map rule str")
39 DEFINE_MTYPE( LIB, ROUTE_MAP_COMPILED, "Route map compiled")
40 DEFINE_MTYPE_STATIC(LIB, ROUTE_MAP_DEP, "Route map dependency")
41
42 DEFINE_QOBJ_TYPE(route_map_index)
43 DEFINE_QOBJ_TYPE(route_map)
44
45 /* Vector for route match rules. */
46 static vector route_match_vec;
47
48 /* Vector for route set rules. */
49 static vector route_set_vec;
50
51 struct route_map_match_set_hooks
52 {
53 /* match interface */
54 int (*match_interface) (struct vty *vty,
55 struct route_map_index *index,
56 const char *command,
57 const char *arg,
58 route_map_event_t type);
59
60 /* no match interface */
61 int (*no_match_interface) (struct vty *vty,
62 struct route_map_index *index,
63 const char *command,
64 const char *arg,
65 route_map_event_t type);
66
67 /* match ip address */
68 int (*match_ip_address) (struct vty *vty,
69 struct route_map_index *index,
70 const char *command,
71 const char *arg,
72 route_map_event_t type);
73
74 /* no match ip address */
75 int (*no_match_ip_address) (struct vty *vty,
76 struct route_map_index *index,
77 const char *command,
78 const char *arg,
79 route_map_event_t type);
80
81 /* match ip address prefix list */
82 int (*match_ip_address_prefix_list) (struct vty *vty,
83 struct route_map_index *index,
84 const char *command,
85 const char *arg,
86 route_map_event_t type);
87
88 /* no match ip address prefix list */
89 int (*no_match_ip_address_prefix_list) (struct vty *vty,
90 struct route_map_index *index,
91 const char *command,
92 const char *arg,
93 route_map_event_t type);
94
95 /* match ip next hop */
96 int (*match_ip_next_hop) (struct vty *vty,
97 struct route_map_index *index,
98 const char *command,
99 const char *arg,
100 route_map_event_t type);
101
102 /* no match ip next hop */
103 int (*no_match_ip_next_hop) (struct vty *vty,
104 struct route_map_index *index,
105 const char *command,
106 const char *arg,
107 route_map_event_t type);
108
109 /* match ip next hop prefix list */
110 int (*match_ip_next_hop_prefix_list) (struct vty *vty,
111 struct route_map_index *index,
112 const char *command,
113 const char *arg,
114 route_map_event_t type);
115
116 /* no match ip next hop prefix list */
117 int (*no_match_ip_next_hop_prefix_list) (struct vty *vty,
118 struct route_map_index *index,
119 const char *command,
120 const char *arg,
121 route_map_event_t type);
122
123 /* match ipv6 address */
124 int (*match_ipv6_address) (struct vty *vty,
125 struct route_map_index *index,
126 const char *command,
127 const char *arg,
128 route_map_event_t type);
129
130 /* no match ipv6 address */
131 int (*no_match_ipv6_address) (struct vty *vty,
132 struct route_map_index *index,
133 const char *command,
134 const char *arg,
135 route_map_event_t type);
136
137
138 /* match ipv6 address prefix list */
139 int (*match_ipv6_address_prefix_list) (struct vty *vty,
140 struct route_map_index *index,
141 const char *command,
142 const char *arg,
143 route_map_event_t type);
144
145 /* no match ipv6 address prefix list */
146 int (*no_match_ipv6_address_prefix_list) (struct vty *vty,
147 struct route_map_index *index,
148 const char *command,
149 const char *arg,
150 route_map_event_t type);
151
152 /* match metric */
153 int (*match_metric) (struct vty *vty,
154 struct route_map_index *index,
155 const char *command,
156 const char *arg,
157 route_map_event_t type);
158
159 /* no match metric */
160 int (*no_match_metric) (struct vty *vty,
161 struct route_map_index *index,
162 const char *command,
163 const char *arg,
164 route_map_event_t type);
165
166 /* match tag */
167 int (*match_tag) (struct vty *vty,
168 struct route_map_index *index,
169 const char *command,
170 const char *arg,
171 route_map_event_t type);
172
173 /* no match tag */
174 int (*no_match_tag) (struct vty *vty,
175 struct route_map_index *index,
176 const char *command,
177 const char *arg,
178 route_map_event_t type);
179
180 /* set ip nexthop */
181 int (*set_ip_nexthop) (struct vty *vty,
182 struct route_map_index *index,
183 const char *command,
184 const char *arg);
185
186 /* no set ip nexthop */
187 int (*no_set_ip_nexthop) (struct vty *vty,
188 struct route_map_index *index,
189 const char *command,
190 const char *arg);
191
192 /* set ipv6 nexthop local */
193 int (*set_ipv6_nexthop_local) (struct vty *vty,
194 struct route_map_index *index,
195 const char *command,
196 const char *arg);
197
198 /* no set ipv6 nexthop local */
199 int (*no_set_ipv6_nexthop_local) (struct vty *vty,
200 struct route_map_index *index,
201 const char *command,
202 const char *arg);
203
204 /* set metric */
205 int (*set_metric) (struct vty *vty,
206 struct route_map_index *index,
207 const char *command,
208 const char *arg);
209
210 /* no set metric */
211 int (*no_set_metric) (struct vty *vty,
212 struct route_map_index *index,
213 const char *command,
214 const char *arg);
215
216 /* set tag */
217 int (*set_tag) (struct vty *vty,
218 struct route_map_index *index,
219 const char *command,
220 const char *arg);
221
222 /* no set tag */
223 int (*no_set_tag) (struct vty *vty,
224 struct route_map_index *index,
225 const char *command,
226 const char *arg);
227
228 };
229
230 struct route_map_match_set_hooks rmap_match_set_hook;
231
232 /* match interface */
233 void
234 route_map_match_interface_hook (int (*func) (struct vty *vty,
235 struct route_map_index *index,
236 const char *command,
237 const char *arg,
238 route_map_event_t type))
239 {
240 rmap_match_set_hook.match_interface = func;
241 }
242
243 /* no match interface */
244 void
245 route_map_no_match_interface_hook (int (*func) (struct vty *vty,
246 struct route_map_index *index,
247 const char *command,
248 const char *arg,
249 route_map_event_t type))
250 {
251 rmap_match_set_hook.no_match_interface = func;
252 }
253
254 /* match ip address */
255 void
256 route_map_match_ip_address_hook (int (*func) (struct vty *vty,
257 struct route_map_index *index,
258 const char *command,
259 const char *arg,
260 route_map_event_t type))
261 {
262 rmap_match_set_hook.match_ip_address = func;
263 }
264
265 /* no match ip address */
266 void
267 route_map_no_match_ip_address_hook (int (*func) (struct vty *vty,
268 struct route_map_index *index,
269 const char *command,
270 const char *arg,
271 route_map_event_t type))
272 {
273 rmap_match_set_hook.no_match_ip_address = func;
274 }
275
276 /* match ip address prefix list */
277 void
278 route_map_match_ip_address_prefix_list_hook (int (*func) (struct vty *vty,
279 struct route_map_index *index,
280 const char *command,
281 const char *arg,
282 route_map_event_t type))
283 {
284 rmap_match_set_hook.match_ip_address_prefix_list = func;
285 }
286
287 /* no match ip address prefix list */
288 void
289 route_map_no_match_ip_address_prefix_list_hook (int (*func) (struct vty *vty,
290 struct route_map_index *index,
291 const char *command,
292 const char *arg,
293 route_map_event_t type))
294 {
295 rmap_match_set_hook.no_match_ip_address_prefix_list = func;
296 }
297
298 /* match ip next hop */
299 void
300 route_map_match_ip_next_hop_hook (int (*func) (struct vty *vty,
301 struct route_map_index *index,
302 const char *command,
303 const char *arg,
304 route_map_event_t type))
305 {
306 rmap_match_set_hook.match_ip_next_hop = func;
307 }
308
309 /* no match ip next hop */
310 void
311 route_map_no_match_ip_next_hop_hook (int (*func) (struct vty *vty,
312 struct route_map_index *index,
313 const char *command,
314 const char *arg,
315 route_map_event_t type))
316 {
317 rmap_match_set_hook.no_match_ip_next_hop = func;
318 }
319
320 /* match ip next hop prefix list */
321 void
322 route_map_match_ip_next_hop_prefix_list_hook (int (*func) (struct vty *vty,
323 struct route_map_index *index,
324 const char *command,
325 const char *arg,
326 route_map_event_t type))
327 {
328 rmap_match_set_hook.match_ip_next_hop_prefix_list = func;
329 }
330
331 /* no match ip next hop prefix list */
332 void
333 route_map_no_match_ip_next_hop_prefix_list_hook (int (*func) (struct vty *vty,
334 struct route_map_index *index,
335 const char *command,
336 const char *arg,
337 route_map_event_t type))
338 {
339 rmap_match_set_hook.no_match_ip_next_hop_prefix_list = func;
340 }
341
342 /* match ipv6 address */
343 void
344 route_map_match_ipv6_address_hook (int (*func) (struct vty *vty,
345 struct route_map_index *index,
346 const char *command,
347 const char *arg,
348 route_map_event_t type))
349 {
350 rmap_match_set_hook.match_ipv6_address = func;
351 }
352
353 /* no match ipv6 address */
354 void
355 route_map_no_match_ipv6_address_hook (int (*func) (struct vty *vty,
356 struct route_map_index *index,
357 const char *command,
358 const char *arg,
359 route_map_event_t type))
360 {
361 rmap_match_set_hook.no_match_ipv6_address = func;
362 }
363
364
365 /* match ipv6 address prefix list */
366 void
367 route_map_match_ipv6_address_prefix_list_hook (int (*func) (struct vty *vty,
368 struct route_map_index *index,
369 const char *command,
370 const char *arg,
371 route_map_event_t type))
372 {
373 rmap_match_set_hook.match_ipv6_address_prefix_list = func;
374 }
375
376 /* no match ipv6 address prefix list */
377 void
378 route_map_no_match_ipv6_address_prefix_list_hook (int (*func) (struct vty *vty,
379 struct route_map_index *index,
380 const char *command,
381 const char *arg,
382 route_map_event_t type))
383 {
384 rmap_match_set_hook.no_match_ipv6_address_prefix_list = func;
385 }
386
387 /* match metric */
388 void
389 route_map_match_metric_hook (int (*func) (struct vty *vty,
390 struct route_map_index *index,
391 const char *command,
392 const char *arg,
393 route_map_event_t type))
394 {
395 rmap_match_set_hook.match_metric = func;
396 }
397
398 /* no match metric */
399 void
400 route_map_no_match_metric_hook (int (*func) (struct vty *vty,
401 struct route_map_index *index,
402 const char *command,
403 const char *arg,
404 route_map_event_t type))
405 {
406 rmap_match_set_hook.no_match_metric = func;
407 }
408
409 /* match tag */
410 void
411 route_map_match_tag_hook (int (*func) (struct vty *vty,
412 struct route_map_index *index,
413 const char *command,
414 const char *arg,
415 route_map_event_t type))
416 {
417 rmap_match_set_hook.match_tag = func;
418 }
419
420 /* no match tag */
421 void
422 route_map_no_match_tag_hook (int (*func) (struct vty *vty,
423 struct route_map_index *index,
424 const char *command,
425 const char *arg,
426 route_map_event_t type))
427 {
428 rmap_match_set_hook.no_match_tag = func;
429 }
430
431 /* set ip nexthop */
432 void
433 route_map_set_ip_nexthop_hook (int (*func) (struct vty *vty,
434 struct route_map_index *index,
435 const char *command,
436 const char *arg))
437 {
438 rmap_match_set_hook.set_ip_nexthop = func;
439 }
440
441 /* no set ip nexthop */
442 void
443 route_map_no_set_ip_nexthop_hook (int (*func) (struct vty *vty,
444 struct route_map_index *index,
445 const char *command,
446 const char *arg))
447 {
448 rmap_match_set_hook.no_set_ip_nexthop = func;
449 }
450
451 /* set ipv6 nexthop local */
452 void
453 route_map_set_ipv6_nexthop_local_hook (int (*func) (struct vty *vty,
454 struct route_map_index *index,
455 const char *command,
456 const char *arg))
457 {
458 rmap_match_set_hook.set_ipv6_nexthop_local = func;
459 }
460
461 /* no set ipv6 nexthop local */
462 void
463 route_map_no_set_ipv6_nexthop_local_hook (int (*func) (struct vty *vty,
464 struct route_map_index *index,
465 const char *command,
466 const char *arg))
467 {
468 rmap_match_set_hook.no_set_ipv6_nexthop_local = func;
469 }
470
471 /* set metric */
472 void
473 route_map_set_metric_hook (int (*func) (struct vty *vty,
474 struct route_map_index *index,
475 const char *command,
476 const char *arg))
477 {
478 rmap_match_set_hook.set_metric = func;
479 }
480
481 /* no set metric */
482 void
483 route_map_no_set_metric_hook (int (*func) (struct vty *vty,
484 struct route_map_index *index,
485 const char *command,
486 const char *arg))
487 {
488 rmap_match_set_hook.no_set_metric = func;
489 }
490
491 /* set tag */
492 void
493 route_map_set_tag_hook (int (*func) (struct vty *vty,
494 struct route_map_index *index,
495 const char *command,
496 const char *arg))
497 {
498 rmap_match_set_hook.set_tag = func;
499 }
500
501 /* no set tag */
502 void
503 route_map_no_set_tag_hook (int (*func) (struct vty *vty,
504 struct route_map_index *index,
505 const char *command,
506 const char *arg))
507 {
508 rmap_match_set_hook.no_set_tag = func;
509 }
510
511 int
512 generic_match_add (struct vty *vty, struct route_map_index *index,
513 const char *command, const char *arg,
514 route_map_event_t type)
515 {
516 int ret;
517
518 ret = route_map_add_match (index, command, arg);
519 if (ret)
520 {
521 switch (ret)
522 {
523 case RMAP_RULE_MISSING:
524 vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE);
525 return CMD_WARNING;
526 case RMAP_COMPILE_ERROR:
527 vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE);
528 return CMD_WARNING;
529 }
530 }
531
532 if (type != RMAP_EVENT_MATCH_ADDED)
533 {
534 route_map_upd8_dependency (type, arg, index->map->name);
535 }
536 return CMD_SUCCESS;
537 }
538
539 int
540 generic_match_delete (struct vty *vty, struct route_map_index *index,
541 const char *command, const char *arg,
542 route_map_event_t type)
543 {
544 int ret;
545 char *dep_name = NULL;
546 const char *tmpstr;
547 char *rmap_name = NULL;
548
549 if (type != RMAP_EVENT_MATCH_DELETED)
550 {
551 /* ignore the mundane, the types without any dependency */
552 if (arg == NULL)
553 {
554 if ((tmpstr = route_map_get_match_arg(index, command)) != NULL)
555 dep_name = XSTRDUP(MTYPE_ROUTE_MAP_RULE, tmpstr);
556 }
557 else
558 {
559 dep_name = XSTRDUP(MTYPE_ROUTE_MAP_RULE, arg);
560 }
561 rmap_name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, index->map->name);
562 }
563
564 ret = route_map_delete_match (index, command, dep_name);
565 if (ret)
566 {
567 switch (ret)
568 {
569 case RMAP_RULE_MISSING:
570 vty_out (vty, "%% BGP Can't find rule.%s", VTY_NEWLINE);
571 break;
572 case RMAP_COMPILE_ERROR:
573 vty_out (vty, "%% BGP Argument is malformed.%s", VTY_NEWLINE);
574 break;
575 }
576 if (dep_name)
577 XFREE(MTYPE_ROUTE_MAP_RULE, dep_name);
578 if (rmap_name)
579 XFREE(MTYPE_ROUTE_MAP_NAME, rmap_name);
580 return CMD_WARNING;
581 }
582
583 if (type != RMAP_EVENT_MATCH_DELETED && dep_name)
584 route_map_upd8_dependency(type, dep_name, rmap_name);
585
586 if (dep_name)
587 XFREE(MTYPE_ROUTE_MAP_RULE, dep_name);
588 if (rmap_name)
589 XFREE(MTYPE_ROUTE_MAP_NAME, rmap_name);
590
591 return CMD_SUCCESS;
592 }
593
594 int
595 generic_set_add (struct vty *vty, struct route_map_index *index,
596 const char *command, const char *arg)
597 {
598 int ret;
599
600 ret = route_map_add_set (index, command, arg);
601 if (ret)
602 {
603 switch (ret)
604 {
605 case RMAP_RULE_MISSING:
606 vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE);
607 return CMD_WARNING;
608 case RMAP_COMPILE_ERROR:
609 vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE);
610 return CMD_WARNING;
611 }
612 }
613 return CMD_SUCCESS;
614 }
615
616 int
617 generic_set_delete (struct vty *vty, struct route_map_index *index,
618 const char *command, const char *arg)
619 {
620 int ret;
621
622 ret = route_map_delete_set (index, command, arg);
623 if (ret)
624 {
625 switch (ret)
626 {
627 case RMAP_RULE_MISSING:
628 vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE);
629 return CMD_WARNING;
630 case RMAP_COMPILE_ERROR:
631 vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE);
632 return CMD_WARNING;
633 }
634 }
635 return CMD_SUCCESS;
636 }
637
638
639 /* Route map rule. This rule has both `match' rule and `set' rule. */
640 struct route_map_rule
641 {
642 /* Rule type. */
643 struct route_map_rule_cmd *cmd;
644
645 /* For pretty printing. */
646 char *rule_str;
647
648 /* Pre-compiled match rule. */
649 void *value;
650
651 /* Linked list. */
652 struct route_map_rule *next;
653 struct route_map_rule *prev;
654 };
655
656 /* Making route map list. */
657 struct route_map_list
658 {
659 struct route_map *head;
660 struct route_map *tail;
661
662 void (*add_hook) (const char *);
663 void (*delete_hook) (const char *);
664 void (*event_hook) (route_map_event_t, const char *);
665 };
666
667 /* Master list of route map. */
668 static struct route_map_list route_map_master = { NULL, NULL, NULL, NULL, NULL };
669 struct hash *route_map_master_hash = NULL;
670
671 static unsigned int
672 route_map_hash_key_make (void *p)
673 {
674 const struct route_map *map = p;
675 return string_hash_make (map->name);
676 }
677
678 static int
679 route_map_hash_cmp(const void *p1, const void *p2)
680 {
681 const struct route_map *map1 = p1;
682 const struct route_map *map2 = p2;
683
684 if (map1->deleted == map2->deleted)
685 {
686 if (map1->name && map2->name)
687 {
688 if (!strcmp (map1->name, map2->name))
689 {
690 return 1;
691 }
692 }
693 else if (!map1->name && !map2->name)
694 {
695 return 1;
696 }
697 }
698
699 return 0;
700 }
701
702 enum route_map_upd8_type
703 {
704 ROUTE_MAP_ADD = 1,
705 ROUTE_MAP_DEL,
706 };
707
708 /* all possible route-map dependency types */
709 enum route_map_dep_type
710 {
711 ROUTE_MAP_DEP_RMAP = 1,
712 ROUTE_MAP_DEP_CLIST,
713 ROUTE_MAP_DEP_ECLIST,
714 ROUTE_MAP_DEP_LCLIST,
715 ROUTE_MAP_DEP_PLIST,
716 ROUTE_MAP_DEP_ASPATH,
717 ROUTE_MAP_DEP_FILTER,
718 ROUTE_MAP_DEP_MAX,
719 };
720
721 struct route_map_dep
722 {
723 char *dep_name;
724 struct hash *dep_rmap_hash;
725 struct hash *this_hash; /* ptr to the hash structure this is part of */
726 };
727
728 /* Hashes maintaining dependency between various sublists used by route maps */
729 struct hash *route_map_dep_hash[ROUTE_MAP_DEP_MAX];
730
731 static unsigned int route_map_dep_hash_make_key (void *p);
732 static int route_map_dep_hash_cmp (const void *p1, const void *p2);
733 static void route_map_clear_all_references (char *rmap_name);
734 static void route_map_rule_delete (struct route_map_rule_list *,
735 struct route_map_rule *);
736 static int rmap_debug = 0;
737
738 static void
739 route_map_index_delete (struct route_map_index *, int);
740
741 /* New route map allocation. Please note route map's name must be
742 specified. */
743 static struct route_map *
744 route_map_new (const char *name)
745 {
746 struct route_map *new;
747
748 new = XCALLOC (MTYPE_ROUTE_MAP, sizeof (struct route_map));
749 new->name = XSTRDUP (MTYPE_ROUTE_MAP_NAME, name);
750 QOBJ_REG (new, route_map);
751 return new;
752 }
753
754 /* Add new name to route_map. */
755 static struct route_map *
756 route_map_add (const char *name)
757 {
758 struct route_map *map;
759 struct route_map_list *list;
760
761 map = route_map_new (name);
762 list = &route_map_master;
763
764 /* Add map to the hash */
765 hash_get(route_map_master_hash, map, hash_alloc_intern);
766
767 /* Add new entry to the head of the list to match how it is added in the
768 * hash table. This is to ensure that if the same route-map has been
769 * created more than once and then marked for deletion (which can happen
770 * if prior deletions haven't completed as BGP hasn't yet done the
771 * route-map processing), the order of the entities is the same in both
772 * the list and the hash table. Otherwise, since there is nothing to
773 * distinguish between the two entries, the wrong entry could get freed.
774 * TODO: This needs to be re-examined to handle it better - e.g., revive
775 * a deleted entry if the route-map is created again.
776 */
777 map->prev = NULL;
778 map->next = list->head;
779 if (list->head)
780 list->head->prev = map;
781 list->head = map;
782 if (!list->tail)
783 list->tail = map;
784
785 /* Execute hook. */
786 if (route_map_master.add_hook)
787 {
788 (*route_map_master.add_hook) (name);
789 route_map_notify_dependencies(name, RMAP_EVENT_CALL_ADDED);
790 }
791 return map;
792 }
793
794 /* this is supposed to be called post processing by
795 * the delete hook function. Don't invoke delete_hook
796 * again in this routine.
797 */
798 static void
799 route_map_free_map (struct route_map *map)
800 {
801 struct route_map_list *list;
802 struct route_map_index *index;
803
804 if (map == NULL)
805 return;
806
807 while ((index = map->head) != NULL)
808 route_map_index_delete (index, 0);
809
810 list = &route_map_master;
811
812 QOBJ_UNREG (map);
813
814 if (map->next)
815 map->next->prev = map->prev;
816 else
817 list->tail = map->prev;
818
819 if (map->prev)
820 map->prev->next = map->next;
821 else
822 list->head = map->next;
823
824 hash_release(route_map_master_hash, map);
825 XFREE (MTYPE_ROUTE_MAP_NAME, map->name);
826 XFREE (MTYPE_ROUTE_MAP, map);
827 }
828
829 /* Route map delete from list. */
830 static void
831 route_map_delete (struct route_map *map)
832 {
833 struct route_map_index *index;
834 char *name;
835
836 while ((index = map->head) != NULL)
837 route_map_index_delete (index, 0);
838
839 name = map->name;
840 map->head = NULL;
841
842 /* Clear all dependencies */
843 route_map_clear_all_references(name);
844 map->deleted = 1;
845 /* Execute deletion hook. */
846 if (route_map_master.delete_hook)
847 {
848 (*route_map_master.delete_hook) (name);
849 route_map_notify_dependencies(name, RMAP_EVENT_CALL_DELETED);
850 }
851
852 if (!map->to_be_processed)
853 {
854 route_map_free_map (map);
855 }
856 }
857
858 /* Lookup route map by route map name string. */
859 struct route_map *
860 route_map_lookup_by_name (const char *name)
861 {
862 struct route_map *map;
863 struct route_map tmp_map;
864
865 if (!name)
866 return NULL;
867
868 // map.deleted is 0 via memset
869 memset(&tmp_map, 0, sizeof(struct route_map));
870 tmp_map.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
871 map = hash_lookup(route_map_master_hash, &tmp_map);
872 XFREE(MTYPE_ROUTE_MAP_NAME, tmp_map.name);
873 return map;
874 }
875
876 int
877 route_map_mark_updated (const char *name, int del_later)
878 {
879 struct route_map *map;
880 int ret = -1;
881 struct route_map tmp_map;
882
883 if (!name)
884 return (ret);
885
886 map = route_map_lookup_by_name(name);
887
888 /* If we did not find the routemap with deleted=0 try again
889 * with deleted=1
890 */
891 if (!map)
892 {
893 memset(&tmp_map, 0, sizeof(struct route_map));
894 tmp_map.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
895 tmp_map.deleted = 1;
896 map = hash_lookup(route_map_master_hash, &tmp_map);
897 XFREE(MTYPE_ROUTE_MAP_NAME, tmp_map.name);
898 }
899
900 if (map)
901 {
902 map->to_be_processed = 1;
903 ret = 0;
904 }
905
906 return(ret);
907 }
908
909 int
910 route_map_clear_updated (struct route_map *map)
911 {
912 int ret = -1;
913
914 if (map)
915 {
916 map->to_be_processed = 0;
917 if (map->deleted)
918 route_map_free_map(map);
919 }
920
921 return (ret);
922 }
923
924 /* Lookup route map. If there isn't route map create one and return
925 it. */
926 static struct route_map *
927 route_map_get (const char *name)
928 {
929 struct route_map *map;
930
931 map = route_map_lookup_by_name (name);
932 if (map == NULL)
933 map = route_map_add (name);
934
935 return map;
936 }
937
938 void
939 route_map_walk_update_list (int (*route_map_update_fn) (char *name))
940 {
941 struct route_map *node;
942 struct route_map *nnode = NULL;
943
944 for (node = route_map_master.head; node; node = nnode)
945 {
946 if (node->to_be_processed)
947 {
948 /* DD: Should we add any thread yield code here */
949 route_map_update_fn(node->name);
950 nnode = node->next;
951 route_map_clear_updated(node);
952 }
953 else
954 nnode = node->next;
955 }
956 }
957
958 /* Return route map's type string. */
959 static const char *
960 route_map_type_str (enum route_map_type type)
961 {
962 switch (type)
963 {
964 case RMAP_PERMIT:
965 return "permit";
966 break;
967 case RMAP_DENY:
968 return "deny";
969 break;
970 default:
971 return "";
972 break;
973 }
974 }
975
976 static int
977 route_map_empty (struct route_map *map)
978 {
979 if (map->head == NULL && map->tail == NULL)
980 return 1;
981 else
982 return 0;
983 }
984
985 /* show route-map */
986 static void
987 vty_show_route_map_entry (struct vty *vty, struct route_map *map)
988 {
989 struct route_map_index *index;
990 struct route_map_rule *rule;
991
992 /* Print the name of the protocol */
993 if (zlog_default)
994 {
995 vty_out (vty, "%s", zlog_protoname());
996 if (zlog_default->instance)
997 vty_out (vty, " %d", zlog_default->instance);
998 }
999 vty_out (vty, ":%s", VTY_NEWLINE);
1000
1001 for (index = map->head; index; index = index->next)
1002 {
1003 vty_out (vty, "route-map %s, %s, sequence %d%s",
1004 map->name, route_map_type_str (index->type),
1005 index->pref, VTY_NEWLINE);
1006
1007 /* Description */
1008 if (index->description)
1009 vty_out (vty, " Description:%s %s%s", VTY_NEWLINE,
1010 index->description, VTY_NEWLINE);
1011
1012 /* Match clauses */
1013 vty_out (vty, " Match clauses:%s", VTY_NEWLINE);
1014 for (rule = index->match_list.head; rule; rule = rule->next)
1015 vty_out (vty, " %s %s%s",
1016 rule->cmd->str, rule->rule_str, VTY_NEWLINE);
1017
1018 vty_out (vty, " Set clauses:%s", VTY_NEWLINE);
1019 for (rule = index->set_list.head; rule; rule = rule->next)
1020 vty_out (vty, " %s %s%s",
1021 rule->cmd->str, rule->rule_str, VTY_NEWLINE);
1022
1023 /* Call clause */
1024 vty_out (vty, " Call clause:%s", VTY_NEWLINE);
1025 if (index->nextrm)
1026 vty_out (vty, " Call %s%s", index->nextrm, VTY_NEWLINE);
1027
1028 /* Exit Policy */
1029 vty_out (vty, " Action:%s", VTY_NEWLINE);
1030 if (index->exitpolicy == RMAP_GOTO)
1031 vty_out (vty, " Goto %d%s", index->nextpref, VTY_NEWLINE);
1032 else if (index->exitpolicy == RMAP_NEXT)
1033 vty_out (vty, " Continue to next entry%s", VTY_NEWLINE);
1034 else if (index->exitpolicy == RMAP_EXIT)
1035 vty_out (vty, " Exit routemap%s", VTY_NEWLINE);
1036 }
1037 }
1038
1039 static int
1040 vty_show_route_map (struct vty *vty, const char *name)
1041 {
1042 struct route_map *map;
1043
1044 if (name)
1045 {
1046 map = route_map_lookup_by_name (name);
1047
1048 if (map)
1049 {
1050 vty_show_route_map_entry (vty, map);
1051 return CMD_SUCCESS;
1052 }
1053 else
1054 {
1055 vty_out (vty, "%s", zlog_protoname());
1056 if (zlog_default && zlog_default->instance)
1057 vty_out (vty, " %d", zlog_default->instance);
1058 vty_out (vty, ": 'route-map %s' not found%s", name, VTY_NEWLINE);
1059 return CMD_SUCCESS;
1060 }
1061 }
1062 else
1063 {
1064 for (map = route_map_master.head; map; map = map->next)
1065 if (!map->deleted)
1066 vty_show_route_map_entry (vty, map);
1067 }
1068 return CMD_SUCCESS;
1069 }
1070
1071
1072 /* New route map allocation. Please note route map's name must be
1073 specified. */
1074 static struct route_map_index *
1075 route_map_index_new (void)
1076 {
1077 struct route_map_index *new;
1078
1079 new = XCALLOC (MTYPE_ROUTE_MAP_INDEX, sizeof (struct route_map_index));
1080 new->exitpolicy = RMAP_EXIT; /* Default to Cisco-style */
1081 QOBJ_REG (new, route_map_index);
1082 return new;
1083 }
1084
1085 /* Free route map index. */
1086 static void
1087 route_map_index_delete (struct route_map_index *index, int notify)
1088 {
1089 struct route_map_rule *rule;
1090
1091 QOBJ_UNREG (index);
1092
1093 /* Free route match. */
1094 while ((rule = index->match_list.head) != NULL)
1095 route_map_rule_delete (&index->match_list, rule);
1096
1097 /* Free route set. */
1098 while ((rule = index->set_list.head) != NULL)
1099 route_map_rule_delete (&index->set_list, rule);
1100
1101 /* Remove index from route map list. */
1102 if (index->next)
1103 index->next->prev = index->prev;
1104 else
1105 index->map->tail = index->prev;
1106
1107 if (index->prev)
1108 index->prev->next = index->next;
1109 else
1110 index->map->head = index->next;
1111
1112 /* Free 'char *nextrm' if not NULL */
1113 if (index->nextrm)
1114 XFREE (MTYPE_ROUTE_MAP_NAME, index->nextrm);
1115
1116 /* Execute event hook. */
1117 if (route_map_master.event_hook && notify)
1118 {
1119 (*route_map_master.event_hook) (RMAP_EVENT_INDEX_DELETED,
1120 index->map->name);
1121 route_map_notify_dependencies(index->map->name, RMAP_EVENT_CALL_ADDED);
1122 }
1123 XFREE (MTYPE_ROUTE_MAP_INDEX, index);
1124 }
1125
1126 /* Lookup index from route map. */
1127 static struct route_map_index *
1128 route_map_index_lookup (struct route_map *map, enum route_map_type type,
1129 int pref)
1130 {
1131 struct route_map_index *index;
1132
1133 for (index = map->head; index; index = index->next)
1134 if ((index->type == type || type == RMAP_ANY)
1135 && index->pref == pref)
1136 return index;
1137 return NULL;
1138 }
1139
1140 /* Add new index to route map. */
1141 static struct route_map_index *
1142 route_map_index_add (struct route_map *map, enum route_map_type type,
1143 int pref)
1144 {
1145 struct route_map_index *index;
1146 struct route_map_index *point;
1147
1148 /* Allocate new route map inex. */
1149 index = route_map_index_new ();
1150 index->map = map;
1151 index->type = type;
1152 index->pref = pref;
1153
1154 /* Compare preference. */
1155 for (point = map->head; point; point = point->next)
1156 if (point->pref >= pref)
1157 break;
1158
1159 if (map->head == NULL)
1160 {
1161 map->head = map->tail = index;
1162 }
1163 else if (point == NULL)
1164 {
1165 index->prev = map->tail;
1166 map->tail->next = index;
1167 map->tail = index;
1168 }
1169 else if (point == map->head)
1170 {
1171 index->next = map->head;
1172 map->head->prev = index;
1173 map->head = index;
1174 }
1175 else
1176 {
1177 index->next = point;
1178 index->prev = point->prev;
1179 if (point->prev)
1180 point->prev->next = index;
1181 point->prev = index;
1182 }
1183
1184 /* Execute event hook. */
1185 if (route_map_master.event_hook)
1186 {
1187 (*route_map_master.event_hook) (RMAP_EVENT_INDEX_ADDED,
1188 map->name);
1189 route_map_notify_dependencies (map->name, RMAP_EVENT_CALL_ADDED);
1190 }
1191 return index;
1192 }
1193
1194 /* Get route map index. */
1195 static struct route_map_index *
1196 route_map_index_get (struct route_map *map, enum route_map_type type,
1197 int pref)
1198 {
1199 struct route_map_index *index;
1200
1201 index = route_map_index_lookup (map, RMAP_ANY, pref);
1202 if (index && index->type != type)
1203 {
1204 /* Delete index from route map. */
1205 route_map_index_delete (index, 1);
1206 index = NULL;
1207 }
1208 if (index == NULL)
1209 index = route_map_index_add (map, type, pref);
1210 return index;
1211 }
1212
1213 /* New route map rule */
1214 static struct route_map_rule *
1215 route_map_rule_new (void)
1216 {
1217 struct route_map_rule *new;
1218
1219 new = XCALLOC (MTYPE_ROUTE_MAP_RULE, sizeof (struct route_map_rule));
1220 return new;
1221 }
1222
1223 /* Install rule command to the match list. */
1224 void
1225 route_map_install_match (struct route_map_rule_cmd *cmd)
1226 {
1227 vector_set (route_match_vec, cmd);
1228 }
1229
1230 /* Install rule command to the set list. */
1231 void
1232 route_map_install_set (struct route_map_rule_cmd *cmd)
1233 {
1234 vector_set (route_set_vec, cmd);
1235 }
1236
1237 /* Lookup rule command from match list. */
1238 static struct route_map_rule_cmd *
1239 route_map_lookup_match (const char *name)
1240 {
1241 unsigned int i;
1242 struct route_map_rule_cmd *rule;
1243
1244 for (i = 0; i < vector_active (route_match_vec); i++)
1245 if ((rule = vector_slot (route_match_vec, i)) != NULL)
1246 if (strcmp (rule->str, name) == 0)
1247 return rule;
1248 return NULL;
1249 }
1250
1251 /* Lookup rule command from set list. */
1252 static struct route_map_rule_cmd *
1253 route_map_lookup_set (const char *name)
1254 {
1255 unsigned int i;
1256 struct route_map_rule_cmd *rule;
1257
1258 for (i = 0; i < vector_active (route_set_vec); i++)
1259 if ((rule = vector_slot (route_set_vec, i)) != NULL)
1260 if (strcmp (rule->str, name) == 0)
1261 return rule;
1262 return NULL;
1263 }
1264
1265 /* Add match and set rule to rule list. */
1266 static void
1267 route_map_rule_add (struct route_map_rule_list *list,
1268 struct route_map_rule *rule)
1269 {
1270 rule->next = NULL;
1271 rule->prev = list->tail;
1272 if (list->tail)
1273 list->tail->next = rule;
1274 else
1275 list->head = rule;
1276 list->tail = rule;
1277 }
1278
1279 /* Delete rule from rule list. */
1280 static void
1281 route_map_rule_delete (struct route_map_rule_list *list,
1282 struct route_map_rule *rule)
1283 {
1284 if (rule->cmd->func_free)
1285 (*rule->cmd->func_free) (rule->value);
1286
1287 if (rule->rule_str)
1288 XFREE (MTYPE_ROUTE_MAP_RULE_STR, rule->rule_str);
1289
1290 if (rule->next)
1291 rule->next->prev = rule->prev;
1292 else
1293 list->tail = rule->prev;
1294 if (rule->prev)
1295 rule->prev->next = rule->next;
1296 else
1297 list->head = rule->next;
1298
1299 XFREE (MTYPE_ROUTE_MAP_RULE, rule);
1300 }
1301
1302 /* strcmp wrapper function which don't crush even argument is NULL. */
1303 static int
1304 rulecmp (const char *dst, const char *src)
1305 {
1306 if (dst == NULL)
1307 {
1308 if (src == NULL)
1309 return 0;
1310 else
1311 return 1;
1312 }
1313 else
1314 {
1315 if (src == NULL)
1316 return 1;
1317 else
1318 return strcmp (dst, src);
1319 }
1320 return 1;
1321 }
1322
1323 /* Use this to return the already specified argument for this match. This is
1324 * useful to get the specified argument with a route map match rule when the
1325 * rule is being deleted and the argument is not provided.
1326 */
1327 const char *
1328 route_map_get_match_arg(struct route_map_index *index, const char *match_name)
1329 {
1330 struct route_map_rule *rule;
1331 struct route_map_rule_cmd *cmd;
1332
1333 /* First lookup rule for add match statement. */
1334 cmd = route_map_lookup_match (match_name);
1335 if (cmd == NULL)
1336 return NULL;
1337
1338 for (rule = index->match_list.head; rule; rule = rule->next)
1339 if (rule->cmd == cmd && rule->rule_str != NULL)
1340 return (rule->rule_str);
1341
1342 return (NULL);
1343 }
1344
1345 /* Add match statement to route map. */
1346 int
1347 route_map_add_match (struct route_map_index *index, const char *match_name,
1348 const char *match_arg)
1349 {
1350 struct route_map_rule *rule;
1351 struct route_map_rule *next;
1352 struct route_map_rule_cmd *cmd;
1353 void *compile;
1354 int replaced = 0;
1355
1356 /* First lookup rule for add match statement. */
1357 cmd = route_map_lookup_match (match_name);
1358 if (cmd == NULL)
1359 return RMAP_RULE_MISSING;
1360
1361 /* Next call compile function for this match statement. */
1362 if (cmd->func_compile)
1363 {
1364 compile= (*cmd->func_compile)(match_arg);
1365 if (compile == NULL)
1366 return RMAP_COMPILE_ERROR;
1367 }
1368 else
1369 compile = NULL;
1370
1371 /* If argument is completely same ignore it. */
1372 for (rule = index->match_list.head; rule; rule = next)
1373 {
1374 next = rule->next;
1375 if (rule->cmd == cmd)
1376 {
1377 route_map_rule_delete (&index->match_list, rule);
1378 replaced = 1;
1379 }
1380 }
1381
1382 /* Add new route map match rule. */
1383 rule = route_map_rule_new ();
1384 rule->cmd = cmd;
1385 rule->value = compile;
1386 if (match_arg)
1387 rule->rule_str = XSTRDUP (MTYPE_ROUTE_MAP_RULE_STR, match_arg);
1388 else
1389 rule->rule_str = NULL;
1390
1391 /* Add new route match rule to linked list. */
1392 route_map_rule_add (&index->match_list, rule);
1393
1394 /* Execute event hook. */
1395 if (route_map_master.event_hook)
1396 {
1397 (*route_map_master.event_hook) (replaced ?
1398 RMAP_EVENT_MATCH_REPLACED:
1399 RMAP_EVENT_MATCH_ADDED,
1400 index->map->name);
1401 route_map_notify_dependencies(index->map->name, RMAP_EVENT_CALL_ADDED);
1402 }
1403
1404 return 0;
1405 }
1406
1407 /* Delete specified route match rule. */
1408 int
1409 route_map_delete_match (struct route_map_index *index, const char *match_name,
1410 const char *match_arg)
1411 {
1412 struct route_map_rule *rule;
1413 struct route_map_rule_cmd *cmd;
1414
1415 cmd = route_map_lookup_match (match_name);
1416 if (cmd == NULL)
1417 return 1;
1418
1419 for (rule = index->match_list.head; rule; rule = rule->next)
1420 if (rule->cmd == cmd &&
1421 (rulecmp (rule->rule_str, match_arg) == 0 || match_arg == NULL))
1422 {
1423 route_map_rule_delete (&index->match_list, rule);
1424 /* Execute event hook. */
1425 if (route_map_master.event_hook)
1426 {
1427 (*route_map_master.event_hook) (RMAP_EVENT_MATCH_DELETED,
1428 index->map->name);
1429 route_map_notify_dependencies(index->map->name, RMAP_EVENT_CALL_ADDED);
1430 }
1431 return 0;
1432 }
1433 /* Can't find matched rule. */
1434 return 1;
1435 }
1436
1437 /* Add route-map set statement to the route map. */
1438 int
1439 route_map_add_set (struct route_map_index *index, const char *set_name,
1440 const char *set_arg)
1441 {
1442 struct route_map_rule *rule;
1443 struct route_map_rule *next;
1444 struct route_map_rule_cmd *cmd;
1445 void *compile;
1446 int replaced = 0;
1447
1448 cmd = route_map_lookup_set (set_name);
1449 if (cmd == NULL)
1450 return RMAP_RULE_MISSING;
1451
1452 /* Next call compile function for this match statement. */
1453 if (cmd->func_compile)
1454 {
1455 compile= (*cmd->func_compile)(set_arg);
1456 if (compile == NULL)
1457 return RMAP_COMPILE_ERROR;
1458 }
1459 else
1460 compile = NULL;
1461
1462 /* Add by WJL. if old set command of same kind exist, delete it first
1463 to ensure only one set command of same kind exist under a
1464 route_map_index. */
1465 for (rule = index->set_list.head; rule; rule = next)
1466 {
1467 next = rule->next;
1468 if (rule->cmd == cmd)
1469 {
1470 route_map_rule_delete (&index->set_list, rule);
1471 replaced = 1;
1472 }
1473 }
1474
1475 /* Add new route map match rule. */
1476 rule = route_map_rule_new ();
1477 rule->cmd = cmd;
1478 rule->value = compile;
1479 if (set_arg)
1480 rule->rule_str = XSTRDUP (MTYPE_ROUTE_MAP_RULE_STR, set_arg);
1481 else
1482 rule->rule_str = NULL;
1483
1484 /* Add new route match rule to linked list. */
1485 route_map_rule_add (&index->set_list, rule);
1486
1487 /* Execute event hook. */
1488 if (route_map_master.event_hook)
1489 {
1490 (*route_map_master.event_hook) (replaced ?
1491 RMAP_EVENT_SET_REPLACED:
1492 RMAP_EVENT_SET_ADDED,
1493 index->map->name);
1494 route_map_notify_dependencies(index->map->name, RMAP_EVENT_CALL_ADDED);
1495 }
1496 return 0;
1497 }
1498
1499 /* Delete route map set rule. */
1500 int
1501 route_map_delete_set (struct route_map_index *index, const char *set_name,
1502 const char *set_arg)
1503 {
1504 struct route_map_rule *rule;
1505 struct route_map_rule_cmd *cmd;
1506
1507 cmd = route_map_lookup_set (set_name);
1508 if (cmd == NULL)
1509 return 1;
1510
1511 for (rule = index->set_list.head; rule; rule = rule->next)
1512 if ((rule->cmd == cmd) &&
1513 (rulecmp (rule->rule_str, set_arg) == 0 || set_arg == NULL))
1514 {
1515 route_map_rule_delete (&index->set_list, rule);
1516 /* Execute event hook. */
1517 if (route_map_master.event_hook)
1518 {
1519 (*route_map_master.event_hook) (RMAP_EVENT_SET_DELETED,
1520 index->map->name);
1521 route_map_notify_dependencies(index->map->name, RMAP_EVENT_CALL_ADDED);
1522 }
1523 return 0;
1524 }
1525 /* Can't find matched rule. */
1526 return 1;
1527 }
1528
1529 /* Apply route map's each index to the object.
1530
1531 The matrix for a route-map looks like this:
1532 (note, this includes the description for the "NEXT"
1533 and "GOTO" frobs now
1534
1535 Match | No Match
1536 |
1537 permit action | cont
1538 |
1539 ------------------+---------------
1540 |
1541 deny deny | cont
1542 |
1543
1544 action)
1545 -Apply Set statements, accept route
1546 -If Call statement is present jump to the specified route-map, if it
1547 denies the route we finish.
1548 -If NEXT is specified, goto NEXT statement
1549 -If GOTO is specified, goto the first clause where pref > nextpref
1550 -If nothing is specified, do as Cisco and finish
1551 deny)
1552 -Route is denied by route-map.
1553 cont)
1554 -Goto Next index
1555
1556 If we get no matches after we've processed all updates, then the route
1557 is dropped too.
1558
1559 Some notes on the new "CALL", "NEXT" and "GOTO"
1560 call WORD - If this clause is matched, then the set statements
1561 are executed and then we jump to route-map 'WORD'. If
1562 this route-map denies the route, we finish, in other case we
1563 do whatever the exit policy (EXIT, NEXT or GOTO) tells.
1564 on-match next - If this clause is matched, then the set statements
1565 are executed and then we drop through to the next clause
1566 on-match goto n - If this clause is matched, then the set statments
1567 are executed and then we goto the nth clause, or the
1568 first clause greater than this. In order to ensure
1569 route-maps *always* exit, you cannot jump backwards.
1570 Sorry ;)
1571
1572 We need to make sure our route-map processing matches the above
1573 */
1574
1575 static route_map_result_t
1576 route_map_apply_match (struct route_map_rule_list *match_list,
1577 struct prefix *prefix, route_map_object_t type,
1578 void *object)
1579 {
1580 route_map_result_t ret = RMAP_NOMATCH;
1581 struct route_map_rule *match;
1582
1583
1584 /* Check all match rule and if there is no match rule, go to the
1585 set statement. */
1586 if (!match_list->head)
1587 ret = RMAP_MATCH;
1588 else
1589 {
1590 for (match = match_list->head; match; match = match->next)
1591 {
1592 /* Try each match statement in turn, If any do not return
1593 RMAP_MATCH, return, otherwise continue on to next match
1594 statement. All match statements must match for end-result
1595 to be a match. */
1596 ret = (*match->cmd->func_apply) (match->value, prefix,
1597 type, object);
1598 if (ret != RMAP_MATCH)
1599 return ret;
1600 }
1601 }
1602 return ret;
1603 }
1604
1605 /* Apply route map to the object. */
1606 route_map_result_t
1607 route_map_apply (struct route_map *map, struct prefix *prefix,
1608 route_map_object_t type, void *object)
1609 {
1610 static int recursion = 0;
1611 int ret = 0;
1612 struct route_map_index *index;
1613 struct route_map_rule *set;
1614
1615 if (recursion > RMAP_RECURSION_LIMIT)
1616 {
1617 zlog_warn("route-map recursion limit (%d) reached, discarding route",
1618 RMAP_RECURSION_LIMIT);
1619 recursion = 0;
1620 return RMAP_DENYMATCH;
1621 }
1622
1623 if (map == NULL)
1624 return RMAP_DENYMATCH;
1625
1626 for (index = map->head; index; index = index->next)
1627 {
1628 /* Apply this index. */
1629 ret = route_map_apply_match (&index->match_list, prefix, type, object);
1630
1631 /* Now we apply the matrix from above */
1632 if (ret == RMAP_NOMATCH)
1633 /* 'cont' from matrix - continue to next route-map sequence */
1634 continue;
1635 else if (ret == RMAP_MATCH)
1636 {
1637 if (index->type == RMAP_PERMIT)
1638 /* 'action' */
1639 {
1640 /* permit+match must execute sets */
1641 for (set = index->set_list.head; set; set = set->next)
1642 ret = (*set->cmd->func_apply) (set->value, prefix,
1643 type, object);
1644
1645 /* Call another route-map if available */
1646 if (index->nextrm)
1647 {
1648 struct route_map *nextrm =
1649 route_map_lookup_by_name (index->nextrm);
1650
1651 if (nextrm) /* Target route-map found, jump to it */
1652 {
1653 recursion++;
1654 ret = route_map_apply (nextrm, prefix, type, object);
1655 recursion--;
1656 }
1657
1658 /* If nextrm returned 'deny', finish. */
1659 if (ret == RMAP_DENYMATCH)
1660 return ret;
1661 }
1662
1663 switch (index->exitpolicy)
1664 {
1665 case RMAP_EXIT:
1666 return ret;
1667 case RMAP_NEXT:
1668 continue;
1669 case RMAP_GOTO:
1670 {
1671 /* Find the next clause to jump to */
1672 struct route_map_index *next = index->next;
1673 int nextpref = index->nextpref;
1674
1675 while (next && next->pref < nextpref)
1676 {
1677 index = next;
1678 next = next->next;
1679 }
1680 if (next == NULL)
1681 {
1682 /* No clauses match! */
1683 return ret;
1684 }
1685 }
1686 }
1687 }
1688 else if (index->type == RMAP_DENY)
1689 /* 'deny' */
1690 {
1691 return RMAP_DENYMATCH;
1692 }
1693 }
1694 }
1695 /* Finally route-map does not match at all. */
1696 return RMAP_DENYMATCH;
1697 }
1698
1699 void
1700 route_map_add_hook (void (*func) (const char *))
1701 {
1702 route_map_master.add_hook = func;
1703 }
1704
1705 void
1706 route_map_delete_hook (void (*func) (const char *))
1707 {
1708 route_map_master.delete_hook = func;
1709 }
1710
1711 void
1712 route_map_event_hook (void (*func) (route_map_event_t, const char *))
1713 {
1714 route_map_master.event_hook = func;
1715 }
1716
1717 /* Routines for route map dependency lists and dependency processing */
1718 static int
1719 route_map_rmap_hash_cmp (const void *p1, const void *p2)
1720 {
1721 return (strcmp((const char *)p1, (const char *)p2) == 0);
1722 }
1723
1724 static int
1725 route_map_dep_hash_cmp (const void *p1, const void *p2)
1726 {
1727
1728 return (strcmp (((const struct route_map_dep *)p1)->dep_name, (const char *)p2) == 0);
1729 }
1730
1731 static void
1732 route_map_clear_reference(struct hash_backet *backet, void *arg)
1733 {
1734 struct route_map_dep *dep = (struct route_map_dep *)backet->data;
1735 char *rmap_name;
1736
1737 if (dep && arg)
1738 {
1739 rmap_name = (char *)hash_release(dep->dep_rmap_hash, (void *)arg);
1740 if (rmap_name)
1741 {
1742 XFREE(MTYPE_ROUTE_MAP_NAME, rmap_name);
1743 }
1744 if (!dep->dep_rmap_hash->count)
1745 {
1746 dep = hash_release(dep->this_hash, (void *)dep->dep_name);
1747 hash_free(dep->dep_rmap_hash);
1748 XFREE(MTYPE_ROUTE_MAP_NAME, dep->dep_name);
1749 XFREE(MTYPE_ROUTE_MAP_DEP, dep);
1750 }
1751 }
1752 }
1753
1754 static void
1755 route_map_clear_all_references (char *rmap_name)
1756 {
1757 int i;
1758
1759 for (i = 1; i < ROUTE_MAP_DEP_MAX; i++)
1760 {
1761 hash_iterate(route_map_dep_hash[i], route_map_clear_reference,
1762 (void *)rmap_name);
1763 }
1764 }
1765
1766 static void *
1767 route_map_dep_hash_alloc(void *p)
1768 {
1769 char *dep_name = (char *)p;
1770 struct route_map_dep *dep_entry;
1771
1772 dep_entry = XCALLOC(MTYPE_ROUTE_MAP_DEP, sizeof(struct route_map_dep));
1773 dep_entry->dep_name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, dep_name);
1774 dep_entry->dep_rmap_hash = hash_create(route_map_dep_hash_make_key,
1775 route_map_rmap_hash_cmp);
1776 dep_entry->this_hash = NULL;
1777
1778 return((void *)dep_entry);
1779 }
1780
1781 static void *
1782 route_map_name_hash_alloc(void *p)
1783 {
1784 return((void *)XSTRDUP(MTYPE_ROUTE_MAP_NAME, (const char *)p));
1785 }
1786
1787 static unsigned int
1788 route_map_dep_hash_make_key (void *p)
1789 {
1790 return (string_hash_make((char *)p));
1791 }
1792
1793 static void
1794 route_map_print_dependency (struct hash_backet *backet, void *data)
1795 {
1796 char *rmap_name = (char *)backet->data;
1797 char *dep_name = (char *)data;
1798
1799 if (rmap_name)
1800 zlog_debug("%s: Dependency for %s: %s", __FUNCTION__, dep_name, rmap_name);
1801 }
1802
1803 static int
1804 route_map_dep_update (struct hash *dephash, const char *dep_name,
1805 const char *rmap_name,
1806 route_map_event_t type)
1807 {
1808 struct route_map_dep *dep = NULL;
1809 char *ret_map_name;
1810 char *dname, *rname;
1811 int ret = 0;
1812
1813 dname = XSTRDUP(MTYPE_ROUTE_MAP_NAME, dep_name);
1814 rname = XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap_name);
1815
1816 switch (type)
1817 {
1818 case RMAP_EVENT_PLIST_ADDED:
1819 case RMAP_EVENT_CLIST_ADDED:
1820 case RMAP_EVENT_ECLIST_ADDED:
1821 case RMAP_EVENT_ASLIST_ADDED:
1822 case RMAP_EVENT_LLIST_ADDED:
1823 case RMAP_EVENT_CALL_ADDED:
1824 case RMAP_EVENT_FILTER_ADDED:
1825 if (rmap_debug)
1826 zlog_debug("%s: Adding dependency for %s in %s", __FUNCTION__,
1827 dep_name, rmap_name);
1828 dep = (struct route_map_dep *) hash_get (dephash, dname,
1829 route_map_dep_hash_alloc);
1830 if (!dep) {
1831 ret = -1;
1832 goto out;
1833 }
1834
1835 if (!dep->this_hash)
1836 dep->this_hash = dephash;
1837
1838 hash_get(dep->dep_rmap_hash, rname, route_map_name_hash_alloc);
1839 break;
1840 case RMAP_EVENT_PLIST_DELETED:
1841 case RMAP_EVENT_CLIST_DELETED:
1842 case RMAP_EVENT_ECLIST_DELETED:
1843 case RMAP_EVENT_ASLIST_DELETED:
1844 case RMAP_EVENT_LLIST_DELETED:
1845 case RMAP_EVENT_CALL_DELETED:
1846 case RMAP_EVENT_FILTER_DELETED:
1847 if (rmap_debug)
1848 zlog_debug("%s: Deleting dependency for %s in %s", __FUNCTION__,
1849 dep_name, rmap_name);
1850 dep = (struct route_map_dep *) hash_get (dephash, dname, NULL);
1851 if (!dep) {
1852 goto out;
1853 }
1854
1855 ret_map_name = (char *)hash_release(dep->dep_rmap_hash, rname);
1856 if (ret_map_name)
1857 XFREE(MTYPE_ROUTE_MAP_NAME, ret_map_name);
1858
1859 if (!dep->dep_rmap_hash->count)
1860 {
1861 dep = hash_release(dephash, dname);
1862 hash_free(dep->dep_rmap_hash);
1863 XFREE(MTYPE_ROUTE_MAP_NAME, dep->dep_name);
1864 XFREE(MTYPE_ROUTE_MAP_DEP, dep);
1865 dep = NULL;
1866 }
1867 break;
1868 default:
1869 break;
1870 }
1871
1872 if (dep)
1873 {
1874 if (rmap_debug)
1875 hash_iterate (dep->dep_rmap_hash, route_map_print_dependency, dname);
1876 }
1877
1878 out:
1879 XFREE(MTYPE_ROUTE_MAP_NAME, rname);
1880 XFREE(MTYPE_ROUTE_MAP_NAME, dname);
1881 return ret;
1882 }
1883
1884 static struct hash *
1885 route_map_get_dep_hash (route_map_event_t event)
1886 {
1887 struct hash *upd8_hash = NULL;
1888
1889 switch (event)
1890 {
1891 case RMAP_EVENT_PLIST_ADDED:
1892 case RMAP_EVENT_PLIST_DELETED:
1893 upd8_hash = route_map_dep_hash[ROUTE_MAP_DEP_PLIST];
1894 break;
1895 case RMAP_EVENT_CLIST_ADDED:
1896 case RMAP_EVENT_CLIST_DELETED:
1897 upd8_hash = route_map_dep_hash[ROUTE_MAP_DEP_CLIST];
1898 break;
1899 case RMAP_EVENT_ECLIST_ADDED:
1900 case RMAP_EVENT_ECLIST_DELETED:
1901 upd8_hash = route_map_dep_hash[ROUTE_MAP_DEP_ECLIST];
1902 break;
1903 case RMAP_EVENT_ASLIST_ADDED:
1904 case RMAP_EVENT_ASLIST_DELETED:
1905 upd8_hash = route_map_dep_hash[ROUTE_MAP_DEP_ASPATH];
1906 break;
1907 case RMAP_EVENT_LLIST_ADDED:
1908 case RMAP_EVENT_LLIST_DELETED:
1909 upd8_hash = route_map_dep_hash[ROUTE_MAP_DEP_LCLIST];
1910 break;
1911 case RMAP_EVENT_CALL_ADDED:
1912 case RMAP_EVENT_CALL_DELETED:
1913 upd8_hash = route_map_dep_hash[ROUTE_MAP_DEP_RMAP];
1914 break;
1915 case RMAP_EVENT_FILTER_ADDED:
1916 case RMAP_EVENT_FILTER_DELETED:
1917 upd8_hash = route_map_dep_hash[ROUTE_MAP_DEP_FILTER];
1918 break;
1919 default:
1920 upd8_hash = NULL;
1921 break;
1922 }
1923 return (upd8_hash);
1924 }
1925
1926 static void
1927 route_map_process_dependency (struct hash_backet *backet, void *data)
1928 {
1929 char *rmap_name;
1930 route_map_event_t type = (route_map_event_t)(ptrdiff_t)data;
1931
1932 rmap_name = (char *)backet->data;
1933
1934 if (rmap_name)
1935 {
1936 if (rmap_debug)
1937 zlog_debug("%s: Notifying %s of dependency", __FUNCTION__,
1938 rmap_name);
1939 if (route_map_master.event_hook)
1940 (*route_map_master.event_hook) (type, rmap_name);
1941 }
1942 }
1943
1944 void
1945 route_map_upd8_dependency (route_map_event_t type, const char *arg,
1946 const char *rmap_name)
1947 {
1948 struct hash *upd8_hash = NULL;
1949
1950 if ((upd8_hash = route_map_get_dep_hash(type)))
1951 route_map_dep_update (upd8_hash, arg, rmap_name, type);
1952 }
1953
1954 void
1955 route_map_notify_dependencies (const char *affected_name, route_map_event_t event)
1956 {
1957 struct route_map_dep *dep;
1958 struct hash *upd8_hash;
1959 char *name;
1960
1961 if (!affected_name)
1962 return;
1963
1964 name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, affected_name);
1965
1966 if ((upd8_hash = route_map_get_dep_hash(event)) == NULL)
1967 {
1968 XFREE (MTYPE_ROUTE_MAP_NAME, name);
1969 return;
1970 }
1971
1972 dep = (struct route_map_dep *)hash_get (upd8_hash, name,
1973 NULL);
1974 if (dep)
1975 {
1976 if (!dep->this_hash)
1977 dep->this_hash = upd8_hash;
1978
1979 hash_iterate (dep->dep_rmap_hash, route_map_process_dependency, (void *)event);
1980 }
1981
1982 XFREE (MTYPE_ROUTE_MAP_NAME, name);
1983 }
1984
1985
1986 /* VTY related functions. */
1987 DEFUN (match_interface,
1988 match_interface_cmd,
1989 "match interface WORD",
1990 MATCH_STR
1991 "match first hop interface of route\n"
1992 "Interface name\n")
1993 {
1994 int idx_word = 2;
1995 VTY_DECLVAR_CONTEXT (route_map_index, index);
1996
1997 if (rmap_match_set_hook.match_interface)
1998 return rmap_match_set_hook.match_interface (vty, index, "interface", argv[idx_word]->arg, RMAP_EVENT_MATCH_ADDED);
1999 return CMD_SUCCESS;
2000 }
2001
2002 DEFUN (no_match_interface,
2003 no_match_interface_cmd,
2004 "no match interface [WORD]",
2005 NO_STR
2006 MATCH_STR
2007 "Match first hop interface of route\n"
2008 "Interface name\n")
2009 {
2010 char *iface = (argc == 4) ? argv[3]->arg : NULL;
2011 VTY_DECLVAR_CONTEXT (route_map_index, index);
2012
2013 if (rmap_match_set_hook.no_match_interface)
2014 return rmap_match_set_hook.no_match_interface (vty, index, "interface", iface, RMAP_EVENT_MATCH_DELETED);
2015 return CMD_SUCCESS;
2016 }
2017
2018
2019 DEFUN (match_ip_address,
2020 match_ip_address_cmd,
2021 "match ip address <(1-199)|(1300-2699)|WORD>",
2022 MATCH_STR
2023 IP_STR
2024 "Match address of route\n"
2025 "IP access-list number\n"
2026 "IP access-list number (expanded range)\n"
2027 "IP Access-list name\n")
2028 {
2029 int idx_acl = 3;
2030 VTY_DECLVAR_CONTEXT (route_map_index, index);
2031
2032 if (rmap_match_set_hook.match_ip_address)
2033 return rmap_match_set_hook.match_ip_address (vty, index, "ip address", argv[idx_acl]->arg,
2034 RMAP_EVENT_FILTER_ADDED);
2035 return CMD_SUCCESS;
2036 }
2037
2038
2039 DEFUN (no_match_ip_address,
2040 no_match_ip_address_cmd,
2041 "no match ip address [<(1-199)|(1300-2699)|WORD>]",
2042 NO_STR
2043 MATCH_STR
2044 IP_STR
2045 "Match address of route\n"
2046 "IP access-list number\n"
2047 "IP access-list number (expanded range)\n"
2048 "IP Access-list name\n")
2049 {
2050 int idx_word = 4;
2051 VTY_DECLVAR_CONTEXT (route_map_index, index);
2052
2053 if (rmap_match_set_hook.no_match_ip_address)
2054 {
2055 if (argc <= idx_word)
2056 return rmap_match_set_hook.no_match_ip_address (vty, index, "ip address", NULL,
2057 RMAP_EVENT_FILTER_DELETED);
2058 return rmap_match_set_hook.no_match_ip_address (vty, index, "ip address", argv[idx_word]->arg,
2059 RMAP_EVENT_FILTER_DELETED);
2060 }
2061 return CMD_SUCCESS;
2062 }
2063
2064
2065 DEFUN (match_ip_address_prefix_list,
2066 match_ip_address_prefix_list_cmd,
2067 "match ip address prefix-list WORD",
2068 MATCH_STR
2069 IP_STR
2070 "Match address of route\n"
2071 "Match entries of prefix-lists\n"
2072 "IP prefix-list name\n")
2073 {
2074 int idx_word = 4;
2075 VTY_DECLVAR_CONTEXT (route_map_index, index);
2076
2077 if (rmap_match_set_hook.match_ip_address_prefix_list)
2078 return rmap_match_set_hook.match_ip_address_prefix_list (vty, index, "ip address prefix-list",
2079 argv[idx_word]->arg, RMAP_EVENT_PLIST_ADDED);
2080 return CMD_SUCCESS;
2081 }
2082
2083
2084 DEFUN (no_match_ip_address_prefix_list,
2085 no_match_ip_address_prefix_list_cmd,
2086 "no match ip address prefix-list [WORD]",
2087 NO_STR
2088 MATCH_STR
2089 IP_STR
2090 "Match address of route\n"
2091 "Match entries of prefix-lists\n"
2092 "IP prefix-list name\n")
2093 {
2094 int idx_word = 5;
2095 VTY_DECLVAR_CONTEXT (route_map_index, index);
2096
2097 if (rmap_match_set_hook.no_match_ip_address_prefix_list)
2098 {
2099 if (argc <= idx_word)
2100 return rmap_match_set_hook.no_match_ip_address_prefix_list (vty, index, "ip address prefix-list",
2101 NULL, RMAP_EVENT_PLIST_DELETED);
2102 return rmap_match_set_hook.no_match_ip_address_prefix_list(vty, index, "ip address prefix-list",
2103 argv[idx_word]->arg, RMAP_EVENT_PLIST_DELETED);
2104 }
2105 return CMD_SUCCESS;
2106 }
2107
2108
2109 DEFUN (match_ip_next_hop,
2110 match_ip_next_hop_cmd,
2111 "match ip next-hop <(1-199)|(1300-2699)|WORD>",
2112 MATCH_STR
2113 IP_STR
2114 "Match next-hop address of route\n"
2115 "IP access-list number\n"
2116 "IP access-list number (expanded range)\n"
2117 "IP Access-list name\n")
2118 {
2119 int idx_acl = 3;
2120 VTY_DECLVAR_CONTEXT (route_map_index, index);
2121
2122 if (rmap_match_set_hook.match_ip_next_hop)
2123 return rmap_match_set_hook.match_ip_next_hop (vty, index, "ip next-hop", argv[idx_acl]->arg,
2124 RMAP_EVENT_FILTER_ADDED);
2125 return CMD_SUCCESS;
2126 }
2127
2128
2129 DEFUN (no_match_ip_next_hop,
2130 no_match_ip_next_hop_cmd,
2131 "no match ip next-hop [<(1-199)|(1300-2699)|WORD>]",
2132 NO_STR
2133 MATCH_STR
2134 IP_STR
2135 "Match next-hop address of route\n"
2136 "IP access-list number\n"
2137 "IP access-list number (expanded range)\n"
2138 "IP Access-list name\n")
2139 {
2140 int idx_word = 4;
2141 VTY_DECLVAR_CONTEXT (route_map_index, index);
2142
2143 if (rmap_match_set_hook.no_match_ip_next_hop)
2144 {
2145 if (argc <= idx_word)
2146 return rmap_match_set_hook.no_match_ip_next_hop (vty, index, "ip next-hop", NULL,
2147 RMAP_EVENT_FILTER_DELETED);
2148 return rmap_match_set_hook.no_match_ip_next_hop (vty, index, "ip next-hop", argv[idx_word]->arg,
2149 RMAP_EVENT_FILTER_DELETED);
2150 }
2151 return CMD_SUCCESS;
2152 }
2153
2154
2155 DEFUN (match_ip_next_hop_prefix_list,
2156 match_ip_next_hop_prefix_list_cmd,
2157 "match ip next-hop prefix-list WORD",
2158 MATCH_STR
2159 IP_STR
2160 "Match next-hop address of route\n"
2161 "Match entries of prefix-lists\n"
2162 "IP prefix-list name\n")
2163 {
2164 int idx_word = 4;
2165 VTY_DECLVAR_CONTEXT (route_map_index, index);
2166
2167 if (rmap_match_set_hook.match_ip_next_hop_prefix_list)
2168 return rmap_match_set_hook.match_ip_next_hop_prefix_list (vty, index, "ip next-hop prefix-list",
2169 argv[idx_word]->arg, RMAP_EVENT_PLIST_ADDED);
2170 return CMD_SUCCESS;
2171 }
2172
2173 DEFUN (no_match_ip_next_hop_prefix_list,
2174 no_match_ip_next_hop_prefix_list_cmd,
2175 "no match ip next-hop prefix-list [WORD]",
2176 NO_STR
2177 MATCH_STR
2178 IP_STR
2179 "Match next-hop address of route\n"
2180 "Match entries of prefix-lists\n"
2181 "IP prefix-list name\n")
2182 {
2183 int idx_word = 5;
2184 VTY_DECLVAR_CONTEXT (route_map_index, index);
2185
2186 if (rmap_match_set_hook.no_match_ip_next_hop)
2187 {
2188 if (argc <= idx_word)
2189 return rmap_match_set_hook.no_match_ip_next_hop (vty, index, "ip next-hop prefix-list",
2190 NULL, RMAP_EVENT_PLIST_DELETED);
2191 return rmap_match_set_hook.no_match_ip_next_hop (vty, index, "ip next-hop prefix-list",
2192 argv[idx_word]->arg, RMAP_EVENT_PLIST_DELETED);
2193 }
2194 return CMD_SUCCESS;
2195 }
2196
2197
2198 DEFUN (match_ipv6_address,
2199 match_ipv6_address_cmd,
2200 "match ipv6 address WORD",
2201 MATCH_STR
2202 IPV6_STR
2203 "Match IPv6 address of route\n"
2204 "IPv6 access-list name\n")
2205 {
2206 int idx_word = 3;
2207 VTY_DECLVAR_CONTEXT (route_map_index, index);
2208
2209 if (rmap_match_set_hook.match_ipv6_address)
2210 return rmap_match_set_hook.match_ipv6_address (vty, index, "ipv6 address", argv[idx_word]->arg,
2211 RMAP_EVENT_FILTER_ADDED);
2212 return CMD_SUCCESS;
2213 }
2214
2215 DEFUN (no_match_ipv6_address,
2216 no_match_ipv6_address_cmd,
2217 "no match ipv6 address WORD",
2218 NO_STR
2219 MATCH_STR
2220 IPV6_STR
2221 "Match IPv6 address of route\n"
2222 "IPv6 access-list name\n")
2223 {
2224 int idx_word = 4;
2225 VTY_DECLVAR_CONTEXT (route_map_index, index);
2226
2227 if (rmap_match_set_hook.no_match_ipv6_address)
2228 return rmap_match_set_hook.no_match_ipv6_address (vty, index, "ipv6 address", argv[idx_word]->arg,
2229 RMAP_EVENT_FILTER_DELETED);
2230 return CMD_SUCCESS;
2231 }
2232
2233
2234 DEFUN (match_ipv6_address_prefix_list,
2235 match_ipv6_address_prefix_list_cmd,
2236 "match ipv6 address prefix-list WORD",
2237 MATCH_STR
2238 IPV6_STR
2239 "Match address of route\n"
2240 "Match entries of prefix-lists\n"
2241 "IP prefix-list name\n")
2242 {
2243 int idx_word = 4;
2244 VTY_DECLVAR_CONTEXT (route_map_index, index);
2245
2246 if (rmap_match_set_hook.match_ipv6_address_prefix_list)
2247 return rmap_match_set_hook.match_ipv6_address_prefix_list (vty, index, "ipv6 address prefix-list",
2248 argv[idx_word]->arg, RMAP_EVENT_PLIST_ADDED);
2249 return CMD_SUCCESS;
2250 }
2251
2252 DEFUN (no_match_ipv6_address_prefix_list,
2253 no_match_ipv6_address_prefix_list_cmd,
2254 "no match ipv6 address prefix-list WORD",
2255 NO_STR
2256 MATCH_STR
2257 IPV6_STR
2258 "Match address of route\n"
2259 "Match entries of prefix-lists\n"
2260 "IP prefix-list name\n")
2261 {
2262 int idx_word = 5;
2263 VTY_DECLVAR_CONTEXT (route_map_index, index);
2264
2265 if (rmap_match_set_hook.no_match_ipv6_address_prefix_list)
2266 return rmap_match_set_hook.no_match_ipv6_address_prefix_list(vty, index, "ipv6 address prefix-list",
2267 argv[idx_word]->arg, RMAP_EVENT_PLIST_DELETED);
2268 return CMD_SUCCESS;
2269 }
2270
2271
2272 DEFUN (match_metric,
2273 match_metric_cmd,
2274 "match metric (0-4294967295)",
2275 MATCH_STR
2276 "Match metric of route\n"
2277 "Metric value\n")
2278 {
2279 int idx_number = 2;
2280 VTY_DECLVAR_CONTEXT (route_map_index, index);
2281
2282 if (rmap_match_set_hook.match_metric)
2283 return rmap_match_set_hook.match_metric(vty, index, "metric", argv[idx_number]->arg,
2284 RMAP_EVENT_MATCH_ADDED);
2285 return CMD_SUCCESS;
2286 }
2287
2288
2289 DEFUN (no_match_metric,
2290 no_match_metric_cmd,
2291 "no match metric [(0-4294967295)]",
2292 NO_STR
2293 MATCH_STR
2294 "Match metric of route\n"
2295 "Metric value\n")
2296 {
2297 int idx_number = 3;
2298 VTY_DECLVAR_CONTEXT (route_map_index, index);
2299
2300 if (rmap_match_set_hook.no_match_metric)
2301 {
2302 if (argc <= idx_number)
2303 return rmap_match_set_hook.no_match_metric (vty, index, "metric",
2304 NULL, RMAP_EVENT_MATCH_DELETED);
2305 return rmap_match_set_hook.no_match_metric(vty, index, "metric",
2306 argv[idx_number]->arg,
2307 RMAP_EVENT_MATCH_DELETED);
2308 }
2309 return CMD_SUCCESS;
2310 }
2311
2312
2313 DEFUN (match_tag,
2314 match_tag_cmd,
2315 "match tag (1-4294967295)",
2316 MATCH_STR
2317 "Match tag of route\n"
2318 "Tag value\n")
2319 {
2320 int idx_number = 2;
2321 VTY_DECLVAR_CONTEXT (route_map_index, index);
2322
2323 if (rmap_match_set_hook.match_tag)
2324 return rmap_match_set_hook.match_tag(vty, index, "tag", argv[idx_number]->arg,
2325 RMAP_EVENT_MATCH_ADDED);
2326 return CMD_SUCCESS;
2327 }
2328
2329
2330 DEFUN (no_match_tag,
2331 no_match_tag_cmd,
2332 "no match tag [(1-4294967295)]",
2333 NO_STR
2334 MATCH_STR
2335 "Match tag of route\n"
2336 "Tag value\n")
2337 {
2338 VTY_DECLVAR_CONTEXT (route_map_index, index);
2339
2340 int idx = 0;
2341 char *arg = argv_find (argv, argc, "(1-4294967295)", &idx) ?
2342 argv[idx]->arg : NULL;
2343
2344 if (rmap_match_set_hook.no_match_tag)
2345 return rmap_match_set_hook.no_match_tag (vty, index, "tag", arg,
2346 RMAP_EVENT_MATCH_DELETED);
2347 return CMD_SUCCESS;
2348 }
2349
2350
2351 DEFUN (set_ip_nexthop,
2352 set_ip_nexthop_cmd,
2353 "set ip next-hop A.B.C.D",
2354 SET_STR
2355 IP_STR
2356 "Next hop address\n"
2357 "IP address of next hop\n")
2358 {
2359 int idx_ipv4 = 3;
2360 union sockunion su;
2361 int ret;
2362 VTY_DECLVAR_CONTEXT (route_map_index, index);
2363
2364 ret = str2sockunion (argv[idx_ipv4]->arg, &su);
2365 if (ret < 0)
2366 {
2367 vty_out (vty, "%% Malformed nexthop address%s", VTY_NEWLINE);
2368 return CMD_WARNING;
2369 }
2370 if (su.sin.sin_addr.s_addr == 0 ||
2371 IPV4_CLASS_DE(su.sin.sin_addr.s_addr))
2372 {
2373 vty_out (vty, "%% nexthop address cannot be 0.0.0.0, multicast "
2374 "or reserved%s", VTY_NEWLINE);
2375 return CMD_WARNING;
2376 }
2377
2378 if (rmap_match_set_hook.set_ip_nexthop)
2379 return rmap_match_set_hook.set_ip_nexthop(vty, index, "ip next-hop", argv[idx_ipv4]->arg);
2380 return CMD_SUCCESS;
2381 }
2382
2383
2384 DEFUN (no_set_ip_nexthop,
2385 no_set_ip_nexthop_cmd,
2386 "no set ip next-hop [<peer-address|A.B.C.D>]",
2387 NO_STR
2388 SET_STR
2389 IP_STR
2390 "Next hop address\n"
2391 "Use peer address (for BGP only)\n"
2392 "IP address of next hop\n")
2393 {
2394 int idx_peer = 4;
2395 VTY_DECLVAR_CONTEXT (route_map_index, index);
2396
2397 if (rmap_match_set_hook.no_set_ip_nexthop)
2398 {
2399 if (argc <= idx_peer)
2400 return rmap_match_set_hook.no_set_ip_nexthop (vty, index, "ip next-hop", NULL);
2401 return rmap_match_set_hook.no_set_ip_nexthop (vty, index, "ip next-hop", argv[idx_peer]->arg);
2402 }
2403 return CMD_SUCCESS;
2404 }
2405
2406
2407 DEFUN (set_ipv6_nexthop_local,
2408 set_ipv6_nexthop_local_cmd,
2409 "set ipv6 next-hop local X:X::X:X",
2410 SET_STR
2411 IPV6_STR
2412 "IPv6 next-hop address\n"
2413 "IPv6 local address\n"
2414 "IPv6 address of next hop\n")
2415 {
2416 int idx_ipv6 = 4;
2417 struct in6_addr addr;
2418 int ret;
2419 VTY_DECLVAR_CONTEXT (route_map_index, index);
2420
2421 ret = inet_pton (AF_INET6, argv[idx_ipv6]->arg, &addr);
2422 if (!ret)
2423 {
2424 vty_out (vty, "%% Malformed nexthop address%s", VTY_NEWLINE);
2425 return CMD_WARNING;
2426 }
2427 if (!IN6_IS_ADDR_LINKLOCAL(&addr))
2428 {
2429 vty_out (vty, "%% Invalid link-local nexthop address%s", VTY_NEWLINE);
2430 return CMD_WARNING;
2431 }
2432
2433 if (rmap_match_set_hook.set_ipv6_nexthop_local)
2434 return rmap_match_set_hook.set_ipv6_nexthop_local (vty, index, "ipv6 next-hop local", argv[idx_ipv6]->arg);
2435 return CMD_SUCCESS;
2436 }
2437
2438
2439 DEFUN (no_set_ipv6_nexthop_local,
2440 no_set_ipv6_nexthop_local_cmd,
2441 "no set ipv6 next-hop local [X:X::X:X]",
2442 NO_STR
2443 SET_STR
2444 IPV6_STR
2445 "IPv6 next-hop address\n"
2446 "IPv6 local address\n"
2447 "IPv6 address of next hop\n")
2448 {
2449 int idx_ipv6 = 5;
2450 VTY_DECLVAR_CONTEXT (route_map_index, index);
2451
2452 if (rmap_match_set_hook.no_set_ipv6_nexthop_local)
2453 {
2454 if (argc <= idx_ipv6)
2455 return rmap_match_set_hook.no_set_ipv6_nexthop_local (vty, index, "ipv6 next-hop local", NULL);
2456 return rmap_match_set_hook.no_set_ipv6_nexthop_local (vty, index, "ipv6 next-hop local", argv[5]->arg);
2457 }
2458 return CMD_SUCCESS;
2459 }
2460
2461 DEFUN (set_metric,
2462 set_metric_cmd,
2463 "set metric <(0-4294967295)|rtt|+rtt|-rtt|+metric|-metric>",
2464 SET_STR
2465 "Metric value for destination routing protocol\n"
2466 "Metric value\n"
2467 "Assign round trip time\n"
2468 "Add round trip time\n"
2469 "Subtract round trip time\n"
2470 "Add metric\n"
2471 "Subtract metric\n")
2472 {
2473 int idx_number = 2;
2474 VTY_DECLVAR_CONTEXT (route_map_index, index);
2475
2476 if (rmap_match_set_hook.set_metric)
2477 return rmap_match_set_hook.set_metric (vty, index, "metric", argv[idx_number]->arg);
2478 return CMD_SUCCESS;
2479 }
2480
2481
2482 DEFUN (no_set_metric,
2483 no_set_metric_cmd,
2484 "no set metric [(0-4294967295)]",
2485 NO_STR
2486 SET_STR
2487 "Metric value for destination routing protocol\n"
2488 "Metric value\n")
2489 {
2490 int idx_number = 3;
2491 VTY_DECLVAR_CONTEXT (route_map_index, index);
2492
2493 if (rmap_match_set_hook.no_set_metric)
2494 {
2495 if (argc <= idx_number)
2496 return rmap_match_set_hook.no_set_metric (vty, index, "metric", NULL);
2497 return rmap_match_set_hook.no_set_metric (vty, index, "metric", argv[idx_number]->arg);
2498 }
2499 return CMD_SUCCESS;
2500 }
2501
2502
2503 DEFUN (set_tag,
2504 set_tag_cmd,
2505 "set tag (1-4294967295)",
2506 SET_STR
2507 "Tag value for routing protocol\n"
2508 "Tag value\n")
2509 {
2510 VTY_DECLVAR_CONTEXT (route_map_index, index);
2511
2512 int idx_number = 2;
2513 if (rmap_match_set_hook.set_tag)
2514 return rmap_match_set_hook.set_tag (vty, index, "tag", argv[idx_number]->arg);
2515 return CMD_SUCCESS;
2516 }
2517
2518
2519 DEFUN (no_set_tag,
2520 no_set_tag_cmd,
2521 "no set tag [(1-4294967295)]",
2522 NO_STR
2523 SET_STR
2524 "Tag value for routing protocol\n"
2525 "Tag value\n")
2526 {
2527 VTY_DECLVAR_CONTEXT (route_map_index, index);
2528
2529 int idx_number = 3;
2530 if (rmap_match_set_hook.no_set_tag)
2531 {
2532 if (argc <= idx_number)
2533 return rmap_match_set_hook.no_set_tag (vty, index, "tag", NULL);
2534 return rmap_match_set_hook.no_set_tag (vty, index, "tag", argv[idx_number]->arg);
2535 }
2536 return CMD_SUCCESS;
2537 }
2538
2539
2540
2541 DEFUN_NOSH (route_map,
2542 route_map_cmd,
2543 "route-map WORD <deny|permit> (1-65535)",
2544 "Create route-map or enter route-map command mode\n"
2545 "Route map tag\n"
2546 "Route map denies set operations\n"
2547 "Route map permits set operations\n"
2548 "Sequence to insert to/delete from existing route-map entry\n")
2549 {
2550 int idx_word = 1;
2551 int idx_permit_deny = 2;
2552 int idx_number = 3;
2553 struct route_map *map;
2554 struct route_map_index *index;
2555 char *endptr = NULL;
2556 int permit = argv[idx_permit_deny]->arg[0] == 'p' ? RMAP_PERMIT : RMAP_DENY;
2557 unsigned long pref = strtoul (argv[idx_number]->arg, &endptr, 10);
2558 const char *mapname = argv[idx_word]->arg;
2559
2560 /* Get route map. */
2561 map = route_map_get (mapname);
2562 index = route_map_index_get (map, permit, pref);
2563
2564 VTY_PUSH_CONTEXT (RMAP_NODE, index);
2565 return CMD_SUCCESS;
2566 }
2567
2568 DEFUN (no_route_map_all,
2569 no_route_map_all_cmd,
2570 "no route-map WORD",
2571 NO_STR
2572 "Create route-map or enter route-map command mode\n"
2573 "Route map tag\n")
2574 {
2575 int idx_word = 2;
2576 const char *mapname = argv[idx_word]->arg;
2577 struct route_map *map;
2578
2579 map = route_map_lookup_by_name (mapname);
2580 if (map == NULL)
2581 {
2582 vty_out (vty, "%% Could not find route-map %s%s", mapname, VTY_NEWLINE);
2583 return CMD_WARNING;
2584 }
2585
2586 route_map_delete (map);
2587
2588 return CMD_SUCCESS;
2589 }
2590
2591 DEFUN (no_route_map,
2592 no_route_map_cmd,
2593 "no route-map WORD <deny|permit> (1-65535)",
2594 NO_STR
2595 "Create route-map or enter route-map command mode\n"
2596 "Route map tag\n"
2597 "Route map denies set operations\n"
2598 "Route map permits set operations\n"
2599 "Sequence to insert to/delete from existing route-map entry\n")
2600 {
2601 int idx_word = 2;
2602 int idx_permit_deny = 3;
2603 int idx_number = 4;
2604 struct route_map *map;
2605 struct route_map_index *index;
2606 char *endptr = NULL;
2607 int permit = argv[idx_permit_deny]->arg[0] == 'p' ? RMAP_PERMIT : RMAP_DENY;
2608 const char *prefstr = argv[idx_number]->arg;
2609 const char *mapname = argv[idx_word]->arg;
2610 unsigned long pref = strtoul (prefstr, &endptr, 10);
2611
2612 /* Existence check. */
2613 map = route_map_lookup_by_name (mapname);
2614 if (map == NULL)
2615 {
2616 vty_out (vty, "%% Could not find route-map %s%s", mapname, VTY_NEWLINE);
2617 return CMD_WARNING;
2618 }
2619
2620 /* Lookup route map index. */
2621 index = route_map_index_lookup (map, permit, pref);
2622 if (index == NULL)
2623 {
2624 vty_out (vty, "%% Could not find route-map entry %s %s%s",
2625 mapname, prefstr, VTY_NEWLINE);
2626 return CMD_WARNING;
2627 }
2628
2629 /* Delete index from route map. */
2630 route_map_index_delete (index, 1);
2631
2632 /* If this route rule is the last one, delete route map itself. */
2633 if (route_map_empty (map))
2634 route_map_delete (map);
2635
2636 return CMD_SUCCESS;
2637 }
2638
2639 DEFUN (rmap_onmatch_next,
2640 rmap_onmatch_next_cmd,
2641 "on-match next",
2642 "Exit policy on matches\n"
2643 "Next clause\n")
2644 {
2645 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2646
2647 if (index)
2648 {
2649 if (index->type == RMAP_DENY)
2650 {
2651 /* Under a deny clause, match means it's finished. No need to set next */
2652 vty_out (vty, "on-match next not supported under route-map deny%s",
2653 VTY_NEWLINE);
2654 return CMD_WARNING;
2655 }
2656 index->exitpolicy = RMAP_NEXT;
2657 }
2658 return CMD_SUCCESS;
2659 }
2660
2661 DEFUN (no_rmap_onmatch_next,
2662 no_rmap_onmatch_next_cmd,
2663 "no on-match next",
2664 NO_STR
2665 "Exit policy on matches\n"
2666 "Next clause\n")
2667 {
2668 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2669
2670 if (index)
2671 index->exitpolicy = RMAP_EXIT;
2672
2673 return CMD_SUCCESS;
2674 }
2675
2676 DEFUN (rmap_onmatch_goto,
2677 rmap_onmatch_goto_cmd,
2678 "on-match goto (1-65535)",
2679 "Exit policy on matches\n"
2680 "Goto Clause number\n"
2681 "Number\n")
2682 {
2683 int idx = 0;
2684 char *num = argv_find (argv, argc, "(1-65535)", &idx) ? argv[idx]->arg : NULL;
2685
2686 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2687 int d = 0;
2688
2689 if (index)
2690 {
2691 if (index->type == RMAP_DENY)
2692 {
2693 /* Under a deny clause, match means it's finished. No need to go anywhere */
2694 vty_out (vty, "on-match goto not supported under route-map deny%s",
2695 VTY_NEWLINE);
2696 return CMD_WARNING;
2697 }
2698
2699 if (num)
2700 VTY_GET_INTEGER_RANGE("route-map index", d, num, 1, 65535);
2701 else
2702 d = index->pref + 1;
2703
2704 if (d <= index->pref)
2705 {
2706 /* Can't allow you to do that, Dave */
2707 vty_out (vty, "can't jump backwards in route-maps%s", VTY_NEWLINE);
2708 return CMD_WARNING;
2709 }
2710 else
2711 {
2712 index->exitpolicy = RMAP_GOTO;
2713 index->nextpref = d;
2714 }
2715 }
2716 return CMD_SUCCESS;
2717 }
2718
2719 DEFUN (no_rmap_onmatch_goto,
2720 no_rmap_onmatch_goto_cmd,
2721 "no on-match goto",
2722 NO_STR
2723 "Exit policy on matches\n"
2724 "Goto Clause number\n")
2725 {
2726 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2727
2728 if (index)
2729 index->exitpolicy = RMAP_EXIT;
2730
2731 return CMD_SUCCESS;
2732 }
2733
2734 /* Cisco/GNU Zebra compatibility aliases */
2735 /* ALIAS_FIXME */
2736 DEFUN (rmap_continue,
2737 rmap_continue_cmd,
2738 "continue (1-65535)",
2739 "Continue on a different entry within the route-map\n"
2740 "Route-map entry sequence number\n")
2741 {
2742 return rmap_onmatch_goto (self, vty, argc, argv);
2743 }
2744
2745 /* ALIAS_FIXME */
2746 DEFUN (no_rmap_continue,
2747 no_rmap_continue_cmd,
2748 "no continue [(1-65535)]",
2749 NO_STR
2750 "Continue on a different entry within the route-map\n"
2751 "Route-map entry sequence number\n")
2752 {
2753 return no_rmap_onmatch_goto (self, vty, argc, argv);
2754 }
2755
2756
2757 DEFUN_NOSH (rmap_show_name,
2758 rmap_show_name_cmd,
2759 "show route-map [WORD]",
2760 SHOW_STR
2761 "route-map information\n"
2762 "route-map name\n")
2763 {
2764 int idx_word = 2;
2765 const char *name = (argc == 3) ? argv[idx_word]->arg : NULL;
2766 return vty_show_route_map (vty, name);
2767 }
2768
2769 DEFUN (rmap_call,
2770 rmap_call_cmd,
2771 "call WORD",
2772 "Jump to another Route-Map after match+set\n"
2773 "Target route-map name\n")
2774 {
2775 int idx_word = 1;
2776 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2777 const char *rmap = argv[idx_word]->arg;
2778
2779 assert(index);
2780
2781 if (index->nextrm)
2782 {
2783 route_map_upd8_dependency (RMAP_EVENT_CALL_DELETED,
2784 index->nextrm,
2785 index->map->name);
2786 XFREE (MTYPE_ROUTE_MAP_NAME, index->nextrm);
2787 }
2788 index->nextrm = XSTRDUP (MTYPE_ROUTE_MAP_NAME, rmap);
2789
2790 /* Execute event hook. */
2791 route_map_upd8_dependency (RMAP_EVENT_CALL_ADDED,
2792 index->nextrm,
2793 index->map->name);
2794 return CMD_SUCCESS;
2795 }
2796
2797 DEFUN (no_rmap_call,
2798 no_rmap_call_cmd,
2799 "no call",
2800 NO_STR
2801 "Jump to another Route-Map after match+set\n")
2802 {
2803 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2804
2805 if (index->nextrm)
2806 {
2807 route_map_upd8_dependency (RMAP_EVENT_CALL_DELETED,
2808 index->nextrm,
2809 index->map->name);
2810 XFREE (MTYPE_ROUTE_MAP_NAME, index->nextrm);
2811 index->nextrm = NULL;
2812 }
2813
2814 return CMD_SUCCESS;
2815 }
2816
2817 DEFUN (rmap_description,
2818 rmap_description_cmd,
2819 "description LINE...",
2820 "Route-map comment\n"
2821 "Comment describing this route-map rule\n")
2822 {
2823 int idx_line = 1;
2824 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2825
2826 if (index)
2827 {
2828 if (index->description)
2829 XFREE (MTYPE_TMP, index->description);
2830 index->description = argv_concat (argv, argc, idx_line);
2831 }
2832 return CMD_SUCCESS;
2833 }
2834
2835 DEFUN (no_rmap_description,
2836 no_rmap_description_cmd,
2837 "no description",
2838 NO_STR
2839 "Route-map comment\n")
2840 {
2841 struct route_map_index *index = VTY_GET_CONTEXT (route_map_index);
2842
2843 if (index)
2844 {
2845 if (index->description)
2846 XFREE (MTYPE_TMP, index->description);
2847 index->description = NULL;
2848 }
2849 return CMD_SUCCESS;
2850 }
2851
2852 /* Configuration write function. */
2853 static int
2854 route_map_config_write (struct vty *vty)
2855 {
2856 struct route_map *map;
2857 struct route_map_index *index;
2858 struct route_map_rule *rule;
2859 int first = 1;
2860 int write = 0;
2861
2862 for (map = route_map_master.head; map; map = map->next)
2863 for (index = map->head; index; index = index->next)
2864 {
2865 if (!first)
2866 vty_out (vty, "!%s", VTY_NEWLINE);
2867 else
2868 first = 0;
2869
2870 vty_out (vty, "route-map %s %s %d%s",
2871 map->name,
2872 route_map_type_str (index->type),
2873 index->pref, VTY_NEWLINE);
2874
2875 if (index->description)
2876 vty_out (vty, " description %s%s", index->description, VTY_NEWLINE);
2877
2878 for (rule = index->match_list.head; rule; rule = rule->next)
2879 vty_out (vty, " match %s %s%s", rule->cmd->str,
2880 rule->rule_str ? rule->rule_str : "",
2881 VTY_NEWLINE);
2882
2883 for (rule = index->set_list.head; rule; rule = rule->next)
2884 vty_out (vty, " set %s %s%s", rule->cmd->str,
2885 rule->rule_str ? rule->rule_str : "",
2886 VTY_NEWLINE);
2887 if (index->nextrm)
2888 vty_out (vty, " call %s%s", index->nextrm, VTY_NEWLINE);
2889 if (index->exitpolicy == RMAP_GOTO)
2890 vty_out (vty, " on-match goto %d%s", index->nextpref, VTY_NEWLINE);
2891 if (index->exitpolicy == RMAP_NEXT)
2892 vty_out (vty," on-match next%s", VTY_NEWLINE);
2893
2894 write++;
2895 }
2896 return write;
2897 }
2898
2899 /* Route map node structure. */
2900 static struct cmd_node rmap_node =
2901 {
2902 RMAP_NODE,
2903 "%s(config-route-map)# ",
2904 1
2905 };
2906
2907 /* Common route map rules */
2908
2909 void *
2910 route_map_rule_tag_compile (const char *arg)
2911 {
2912 unsigned long int tmp;
2913 char *endptr;
2914 route_tag_t *tag;
2915
2916 errno = 0;
2917 tmp = strtoul(arg, &endptr, 0);
2918 if (arg[0] == '\0' || *endptr != '\0' || errno || tmp > ROUTE_TAG_MAX)
2919 return NULL;
2920
2921 tag = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(*tag));
2922 *tag = tmp;
2923
2924 return tag;
2925 }
2926
2927 void
2928 route_map_rule_tag_free (void *rule)
2929 {
2930 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
2931 }
2932
2933 void
2934 route_map_finish (void)
2935 {
2936 int i;
2937
2938 vector_free (route_match_vec);
2939 route_match_vec = NULL;
2940 vector_free (route_set_vec);
2941 route_set_vec = NULL;
2942
2943 /* cleanup route_map */
2944 while (route_map_master.head)
2945 {
2946 struct route_map *map = route_map_master.head;
2947 map->to_be_processed = 0;
2948 route_map_delete (map);
2949 }
2950
2951 for (i = 1; i < ROUTE_MAP_DEP_MAX; i++)
2952 {
2953 hash_free(route_map_dep_hash[i]);
2954 route_map_dep_hash[i] = NULL;
2955 }
2956
2957 hash_free (route_map_master_hash);
2958 route_map_master_hash = NULL;
2959 }
2960
2961 static void rmap_autocomplete(vector comps, struct cmd_token *token)
2962 {
2963 struct route_map *map;
2964
2965 for (map = route_map_master.head; map; map = map->next)
2966 vector_set (comps, XSTRDUP (MTYPE_COMPLETION, map->name));
2967 }
2968
2969 static const struct cmd_variable_handler rmap_var_handlers[] = {
2970 {
2971 /* "route-map WORD" */
2972 .varname = "route_map",
2973 .completions = rmap_autocomplete
2974 }, {
2975 .tokenname = "ROUTEMAP_NAME",
2976 .completions = rmap_autocomplete
2977 }, {
2978 .tokenname = "RMAP_NAME",
2979 .completions = rmap_autocomplete
2980 }, {
2981 .completions = NULL
2982 }
2983 };
2984
2985 /* Initialization of route map vector. */
2986 void
2987 route_map_init (void)
2988 {
2989 int i;
2990
2991 /* Make vector for match and set. */
2992 route_match_vec = vector_init (1);
2993 route_set_vec = vector_init (1);
2994 route_map_master_hash = hash_create(route_map_hash_key_make, route_map_hash_cmp);
2995
2996 for (i = 1; i < ROUTE_MAP_DEP_MAX; i++)
2997 route_map_dep_hash[i] = hash_create(route_map_dep_hash_make_key,
2998 route_map_dep_hash_cmp);
2999
3000 cmd_variable_handler_register(rmap_var_handlers);
3001
3002 /* Install route map top node. */
3003 install_node (&rmap_node, route_map_config_write);
3004
3005 /* Install route map commands. */
3006 install_default (RMAP_NODE);
3007 install_element (CONFIG_NODE, &route_map_cmd);
3008 install_element (CONFIG_NODE, &no_route_map_cmd);
3009 install_element (CONFIG_NODE, &no_route_map_all_cmd);
3010
3011 /* Install the on-match stuff */
3012 install_element (RMAP_NODE, &route_map_cmd);
3013 install_element (RMAP_NODE, &rmap_onmatch_next_cmd);
3014 install_element (RMAP_NODE, &no_rmap_onmatch_next_cmd);
3015 install_element (RMAP_NODE, &rmap_onmatch_goto_cmd);
3016 install_element (RMAP_NODE, &no_rmap_onmatch_goto_cmd);
3017 install_element (RMAP_NODE, &rmap_continue_cmd);
3018 install_element (RMAP_NODE, &no_rmap_continue_cmd);
3019
3020 /* Install the continue stuff (ALIAS of on-match). */
3021
3022 /* Install the call stuff. */
3023 install_element (RMAP_NODE, &rmap_call_cmd);
3024 install_element (RMAP_NODE, &no_rmap_call_cmd);
3025
3026 /* Install description commands. */
3027 install_element (RMAP_NODE, &rmap_description_cmd);
3028 install_element (RMAP_NODE, &no_rmap_description_cmd);
3029
3030 /* Install show command */
3031 install_element (ENABLE_NODE, &rmap_show_name_cmd);
3032
3033 install_element (RMAP_NODE, &match_interface_cmd);
3034 install_element (RMAP_NODE, &no_match_interface_cmd);
3035
3036 install_element (RMAP_NODE, &match_ip_address_cmd);
3037 install_element (RMAP_NODE, &no_match_ip_address_cmd);
3038
3039 install_element (RMAP_NODE, &match_ip_address_prefix_list_cmd);
3040 install_element (RMAP_NODE, &no_match_ip_address_prefix_list_cmd);
3041
3042 install_element (RMAP_NODE, &match_ip_next_hop_cmd);
3043 install_element (RMAP_NODE, &no_match_ip_next_hop_cmd);
3044
3045 install_element (RMAP_NODE, &match_ip_next_hop_prefix_list_cmd);
3046 install_element (RMAP_NODE, &no_match_ip_next_hop_prefix_list_cmd);
3047
3048 install_element (RMAP_NODE, &match_ipv6_address_cmd);
3049 install_element (RMAP_NODE, &no_match_ipv6_address_cmd);
3050
3051 install_element (RMAP_NODE, &match_ipv6_address_prefix_list_cmd);
3052 install_element (RMAP_NODE, &no_match_ipv6_address_prefix_list_cmd);
3053
3054 install_element (RMAP_NODE, &match_metric_cmd);
3055 install_element (RMAP_NODE, &no_match_metric_cmd);
3056
3057 install_element (RMAP_NODE, &match_tag_cmd);
3058 install_element (RMAP_NODE, &no_match_tag_cmd);
3059
3060 install_element (RMAP_NODE, &set_ip_nexthop_cmd);
3061 install_element (RMAP_NODE, &no_set_ip_nexthop_cmd);
3062
3063 install_element (RMAP_NODE, &set_ipv6_nexthop_local_cmd);
3064 install_element (RMAP_NODE, &no_set_ipv6_nexthop_local_cmd);
3065
3066 install_element (RMAP_NODE, &set_metric_cmd);
3067 install_element (RMAP_NODE, &no_set_metric_cmd);
3068
3069 install_element (RMAP_NODE, &set_tag_cmd);
3070 install_element (RMAP_NODE, &no_set_tag_cmd);
3071
3072 }