]> git.proxmox.com Git - mirror_frr.git/blob - lib/distribute.c
Add user `frr` into group `frrvty`
[mirror_frr.git] / lib / distribute.c
1 /* Distribute list functions
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
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any 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
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "hash.h"
25 #include "if.h"
26 #include "filter.h"
27 #include "command.h"
28 #include "distribute.h"
29 #include "memory.h"
30
31 DEFINE_MTYPE_STATIC(LIB, DISTRIBUTE, "Distribute list")
32 DEFINE_MTYPE_STATIC(LIB, DISTRIBUTE_IFNAME, "Dist-list ifname")
33 DEFINE_MTYPE_STATIC(LIB, DISTRIBUTE_NAME, "Dist-list name")
34
35 /* Hash of distribute list. */
36 struct hash *disthash;
37
38 /* Hook functions. */
39 void (*distribute_add_hook) (struct distribute *);
40 void (*distribute_delete_hook) (struct distribute *);
41
42 static struct distribute *
43 distribute_new (void)
44 {
45 return XCALLOC (MTYPE_DISTRIBUTE, sizeof (struct distribute));
46 }
47
48 /* Free distribute object. */
49 static void
50 distribute_free (struct distribute *dist)
51 {
52 int i = 0;
53
54 if (dist->ifname)
55 XFREE (MTYPE_DISTRIBUTE_IFNAME, dist->ifname);
56
57 for (i = 0; i < DISTRIBUTE_MAX; i++)
58 if (dist->list[i])
59 XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[i]);
60
61 for (i = 0; i < DISTRIBUTE_MAX; i++)
62 if (dist->prefix[i])
63 XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[i]);
64
65 XFREE (MTYPE_DISTRIBUTE, dist);
66 }
67
68 static void
69 distribute_free_if_empty(struct distribute *dist)
70 {
71 int i;
72
73 for (i = 0; i < DISTRIBUTE_MAX; i++)
74 if (dist->list[i] != NULL || dist->prefix[i] != NULL)
75 return;
76
77 hash_release (disthash, dist);
78 distribute_free (dist);
79 }
80
81 /* Lookup interface's distribute list. */
82 struct distribute *
83 distribute_lookup (const char *ifname)
84 {
85 struct distribute key;
86 struct distribute *dist;
87
88 /* temporary reference */
89 key.ifname = (ifname) ? XSTRDUP(MTYPE_DISTRIBUTE_IFNAME, ifname) : NULL;
90
91 dist = hash_lookup (disthash, &key);
92
93 if (key.ifname)
94 XFREE(MTYPE_DISTRIBUTE_IFNAME, key.ifname);
95
96 return dist;
97 }
98
99 void
100 distribute_list_add_hook (void (*func) (struct distribute *))
101 {
102 distribute_add_hook = func;
103 }
104
105 void
106 distribute_list_delete_hook (void (*func) (struct distribute *))
107 {
108 distribute_delete_hook = func;
109 }
110
111 static void *
112 distribute_hash_alloc (struct distribute *arg)
113 {
114 struct distribute *dist;
115
116 dist = distribute_new ();
117 if (arg->ifname)
118 dist->ifname = XSTRDUP (MTYPE_DISTRIBUTE_IFNAME, arg->ifname);
119 else
120 dist->ifname = NULL;
121 return dist;
122 }
123
124 /* Make new distribute list and push into hash. */
125 static struct distribute *
126 distribute_get (const char *ifname)
127 {
128 struct distribute key;
129 struct distribute *ret;
130
131 /* temporary reference */
132 key.ifname = (ifname) ? XSTRDUP(MTYPE_DISTRIBUTE_IFNAME, ifname) : NULL;
133
134 ret = hash_get (disthash, &key, (void * (*) (void *))distribute_hash_alloc);
135
136 if (key.ifname)
137 XFREE(MTYPE_DISTRIBUTE_IFNAME, key.ifname);
138
139 return ret;
140 }
141
142 static unsigned int
143 distribute_hash_make (void *arg)
144 {
145 const struct distribute *dist = arg;
146
147 return dist->ifname ? string_hash_make (dist->ifname) : 0;
148 }
149
150 /* If two distribute-list have same value then return 1 else return
151 0. This function is used by hash package. */
152 static int
153 distribute_cmp (const struct distribute *dist1, const struct distribute *dist2)
154 {
155 if (dist1->ifname && dist2->ifname)
156 if (strcmp (dist1->ifname, dist2->ifname) == 0)
157 return 1;
158 if (! dist1->ifname && ! dist2->ifname)
159 return 1;
160 return 0;
161 }
162
163 /* Set access-list name to the distribute list. */
164 static struct distribute *
165 distribute_list_set (const char *ifname, enum distribute_type type,
166 const char *alist_name)
167 {
168 struct distribute *dist;
169
170 dist = distribute_get (ifname);
171
172 if (dist->list[type])
173 XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[type]);
174 dist->list[type] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, alist_name);
175
176 /* Apply this distribute-list to the interface. */
177 (*distribute_add_hook) (dist);
178
179 return dist;
180 }
181
182 /* Unset distribute-list. If matched distribute-list exist then
183 return 1. */
184 static int
185 distribute_list_unset (const char *ifname, enum distribute_type type,
186 const char *alist_name)
187 {
188 struct distribute *dist;
189
190 dist = distribute_lookup (ifname);
191 if (!dist)
192 return 0;
193
194 if (!dist->list[type])
195 return 0;
196 if (strcmp (dist->list[type], alist_name) != 0)
197 return 0;
198
199 XFREE(MTYPE_DISTRIBUTE_NAME, dist->list[type]);
200 dist->list[type] = NULL;
201
202 /* Apply this distribute-list to the interface. */
203 (*distribute_delete_hook) (dist);
204
205 /* If all dist are NULL, then free distribute list. */
206 distribute_free_if_empty(dist);
207 return 1;
208 }
209
210 /* Set access-list name to the distribute list. */
211 static struct distribute *
212 distribute_list_prefix_set (const char *ifname, enum distribute_type type,
213 const char *plist_name)
214 {
215 struct distribute *dist;
216
217 dist = distribute_get (ifname);
218
219 if (dist->prefix[type])
220 XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[type]);
221 dist->prefix[type] = XSTRDUP(MTYPE_DISTRIBUTE_NAME, plist_name);
222
223 /* Apply this distribute-list to the interface. */
224 (*distribute_add_hook) (dist);
225
226 return dist;
227 }
228
229 /* Unset distribute-list. If matched distribute-list exist then
230 return 1. */
231 static int
232 distribute_list_prefix_unset (const char *ifname, enum distribute_type type,
233 const char *plist_name)
234 {
235 struct distribute *dist;
236
237 dist = distribute_lookup (ifname);
238 if (!dist)
239 return 0;
240
241 if (!dist->prefix[type])
242 return 0;
243 if (strcmp (dist->prefix[type], plist_name) != 0)
244 return 0;
245
246 XFREE(MTYPE_DISTRIBUTE_NAME, dist->prefix[type]);
247 dist->prefix[type] = NULL;
248
249 /* Apply this distribute-list to the interface. */
250 (*distribute_delete_hook) (dist);
251
252 /* If all dist are NULL, then free distribute list. */
253 distribute_free_if_empty(dist);
254 return 1;
255 }
256
257 DEFUN (distribute_list_all,
258 distribute_list_all_cmd,
259 "distribute-list WORD (in|out)",
260 "Filter networks in routing updates\n"
261 "Access-list name\n"
262 "Filter incoming routing updates\n"
263 "Filter outgoing routing updates\n")
264 {
265 enum distribute_type type;
266
267 /* Check of distribute list type. */
268 if (strncmp (argv[1], "i", 1) == 0)
269 type = DISTRIBUTE_V4_IN;
270 else if (strncmp (argv[1], "o", 1) == 0)
271 type = DISTRIBUTE_V4_OUT;
272 else
273 {
274 vty_out (vty, "distribute list direction must be [in|out]%s",
275 VTY_NEWLINE);
276 return CMD_WARNING;
277 }
278
279 /* Get interface name corresponding distribute list. */
280 distribute_list_set (NULL, type, argv[0]);
281
282 return CMD_SUCCESS;
283 }
284
285 DEFUN (ipv6_distribute_list_all,
286 ipv6_distribute_list_all_cmd,
287 "ipv6 distribute-list WORD (in|out)",
288 "Filter networks in routing updates\n"
289 "Access-list name\n"
290 "Filter incoming routing updates\n"
291 "Filter outgoing routing updates\n")
292 {
293 enum distribute_type type;
294
295 /* Check of distribute list type. */
296 if (strncmp (argv[1], "i", 1) == 0)
297 type = DISTRIBUTE_V6_IN;
298 else if (strncmp (argv[1], "o", 1) == 0)
299 type = DISTRIBUTE_V6_OUT;
300 else
301 {
302 vty_out (vty, "distribute list direction must be [in|out]%s",
303 VTY_NEWLINE);
304 return CMD_WARNING;
305 }
306
307 /* Get interface name corresponding distribute list. */
308 distribute_list_set (NULL, type, argv[0]);
309
310 return CMD_SUCCESS;
311 }
312
313 ALIAS (ipv6_distribute_list_all,
314 ipv6_as_v4_distribute_list_all_cmd,
315 "distribute-list WORD (in|out)",
316 "Filter networks in routing updates\n"
317 "Access-list name\n"
318 "Filter incoming routing updates\n"
319 "Filter outgoing routing updates\n")
320
321 DEFUN (no_distribute_list_all,
322 no_distribute_list_all_cmd,
323 "no distribute-list WORD (in|out)",
324 NO_STR
325 "Filter networks in routing updates\n"
326 "Access-list name\n"
327 "Filter incoming routing updates\n"
328 "Filter outgoing routing updates\n")
329 {
330 int ret;
331 enum distribute_type type;
332
333 /* Check of distribute list type. */
334 if (strncmp (argv[1], "i", 1) == 0)
335 type = DISTRIBUTE_V4_IN;
336 else if (strncmp (argv[1], "o", 1) == 0)
337 type = DISTRIBUTE_V4_OUT;
338 else
339 {
340 vty_out (vty, "distribute list direction must be [in|out]%s",
341 VTY_NEWLINE);
342 return CMD_WARNING;
343 }
344
345 ret = distribute_list_unset (NULL, type, argv[0]);
346 if (! ret)
347 {
348 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
349 return CMD_WARNING;
350 }
351 return CMD_SUCCESS;
352 }
353
354 DEFUN (no_ipv6_distribute_list_all,
355 no_ipv6_distribute_list_all_cmd,
356 "no ipv6 distribute-list WORD (in|out)",
357 NO_STR
358 "Filter networks in routing updates\n"
359 "Access-list name\n"
360 "Filter incoming routing updates\n"
361 "Filter outgoing routing updates\n")
362 {
363 int ret;
364 enum distribute_type type;
365
366 /* Check of distribute list type. */
367 if (strncmp (argv[1], "i", 1) == 0)
368 type = DISTRIBUTE_V6_IN;
369 else if (strncmp (argv[1], "o", 1) == 0)
370 type = DISTRIBUTE_V6_OUT;
371 else
372 {
373 vty_out (vty, "distribute list direction must be [in|out]%s",
374 VTY_NEWLINE);
375 return CMD_WARNING;
376 }
377
378 ret = distribute_list_unset (NULL, type, argv[0]);
379 if (! ret)
380 {
381 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
382 return CMD_WARNING;
383 }
384 return CMD_SUCCESS;
385 }
386
387 ALIAS (no_ipv6_distribute_list_all,
388 no_ipv6_as_v4_distribute_list_all_cmd,
389 "no distribute-list WORD (in|out)",
390 NO_STR
391 "Filter networks in routing updates\n"
392 "Access-list name\n"
393 "Filter incoming routing updates\n"
394 "Filter outgoing routing updates\n")
395
396 DEFUN (distribute_list,
397 distribute_list_cmd,
398 "distribute-list WORD (in|out) WORD",
399 "Filter networks in routing updates\n"
400 "Access-list name\n"
401 "Filter incoming routing updates\n"
402 "Filter outgoing routing updates\n"
403 "Interface name\n")
404 {
405 enum distribute_type type;
406
407 /* Check of distribute list type. */
408 if (strncmp (argv[1], "i", 1) == 0)
409 type = DISTRIBUTE_V4_IN;
410 else if (strncmp (argv[1], "o", 1) == 0)
411 type = DISTRIBUTE_V4_OUT;
412 else
413 {
414 vty_out (vty, "distribute list direction must be [in|out]%s", VTY_NEWLINE);
415 return CMD_WARNING;
416 }
417
418 /* Get interface name corresponding distribute list. */
419 distribute_list_set (argv[2], type, argv[0]);
420
421 return CMD_SUCCESS;
422 }
423
424 DEFUN (ipv6_distribute_list,
425 ipv6_distribute_list_cmd,
426 "ipv6 distribute-list WORD (in|out) WORD",
427 "Filter networks in routing updates\n"
428 "Access-list name\n"
429 "Filter incoming routing updates\n"
430 "Filter outgoing routing updates\n"
431 "Interface name\n")
432 {
433 enum distribute_type type;
434
435 /* Check of distribute list type. */
436 if (strncmp (argv[1], "i", 1) == 0)
437 type = DISTRIBUTE_V6_IN;
438 else if (strncmp (argv[1], "o", 1) == 0)
439 type = DISTRIBUTE_V6_OUT;
440 else
441 {
442 vty_out (vty, "distribute list direction must be [in|out]%s", VTY_NEWLINE);
443 return CMD_WARNING;
444 }
445
446 /* Get interface name corresponding distribute list. */
447 distribute_list_set (argv[2], type, argv[0]);
448
449 return CMD_SUCCESS;
450 }
451
452 ALIAS (ipv6_distribute_list,
453 ipv6_as_v4_distribute_list_cmd,
454 "distribute-list WORD (in|out) WORD",
455 "Filter networks in routing updates\n"
456 "Access-list name\n"
457 "Filter incoming routing updates\n"
458 "Filter outgoing routing updates\n"
459 "Interface name\n")
460
461 DEFUN (no_distribute_list, no_distribute_list_cmd,
462 "no distribute-list WORD (in|out) WORD",
463 NO_STR
464 "Filter networks in routing updates\n"
465 "Access-list name\n"
466 "Filter incoming routing updates\n"
467 "Filter outgoing routing updates\n"
468 "Interface name\n")
469 {
470 int ret;
471 enum distribute_type type;
472
473 /* Check of distribute list type. */
474 if (strncmp (argv[1], "i", 1) == 0)
475 type = DISTRIBUTE_V4_IN;
476 else if (strncmp (argv[1], "o", 1) == 0)
477 type = DISTRIBUTE_V4_OUT;
478 else
479 {
480 vty_out (vty, "distribute list direction must be [in|out]%s", VTY_NEWLINE);
481 return CMD_WARNING;
482 }
483
484 ret = distribute_list_unset (argv[2], type, argv[0]);
485 if (! ret)
486 {
487 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
488 return CMD_WARNING;
489 }
490 return CMD_SUCCESS;
491 }
492
493 DEFUN (no_ipv6_distribute_list,
494 no_ipv6_distribute_list_cmd,
495 "no ipv6 distribute-list WORD (in|out) WORD",
496 NO_STR
497 "Filter networks in routing updates\n"
498 "Access-list name\n"
499 "Filter incoming routing updates\n"
500 "Filter outgoing routing updates\n"
501 "Interface name\n")
502 {
503 int ret;
504 enum distribute_type type;
505
506 /* Check of distribute list type. */
507 if (strncmp (argv[1], "i", 1) == 0)
508 type = DISTRIBUTE_V6_IN;
509 else if (strncmp (argv[1], "o", 1) == 0)
510 type = DISTRIBUTE_V6_OUT;
511 else
512 {
513 vty_out (vty, "distribute list direction must be [in|out]%s", VTY_NEWLINE);
514 return CMD_WARNING;
515 }
516
517 ret = distribute_list_unset (argv[2], type, argv[0]);
518 if (! ret)
519 {
520 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
521 return CMD_WARNING;
522 }
523 return CMD_SUCCESS;
524 }
525
526 ALIAS (no_ipv6_distribute_list,
527 no_ipv6_as_v4_distribute_list_cmd,
528 "no distribute-list WORD (in|out) WORD",
529 NO_STR
530 "Filter networks in routing updates\n"
531 "Access-list name\n"
532 "Filter incoming routing updates\n"
533 "Filter outgoing routing updates\n"
534 "Interface name\n")
535
536 DEFUN (distribute_list_prefix_all,
537 distribute_list_prefix_all_cmd,
538 "distribute-list prefix WORD (in|out)",
539 "Filter networks in routing updates\n"
540 "Filter prefixes in routing updates\n"
541 "Name of an IP prefix-list\n"
542 "Filter incoming routing updates\n"
543 "Filter outgoing routing updates\n")
544 {
545 enum distribute_type type;
546
547 /* Check of distribute list type. */
548 if (strncmp (argv[1], "i", 1) == 0)
549 type = DISTRIBUTE_V4_IN;
550 else if (strncmp (argv[1], "o", 1) == 0)
551 type = DISTRIBUTE_V4_OUT;
552 else
553 {
554 vty_out (vty, "distribute list direction must be [in|out]%s",
555 VTY_NEWLINE);
556 return CMD_WARNING;
557 }
558
559 /* Get interface name corresponding distribute list. */
560 distribute_list_prefix_set (NULL, type, argv[0]);
561
562 return CMD_SUCCESS;
563 }
564
565 DEFUN (ipv6_distribute_list_prefix_all,
566 ipv6_distribute_list_prefix_all_cmd,
567 "ipv6 distribute-list prefix WORD (in|out)",
568 "Filter networks in routing updates\n"
569 "Filter prefixes in routing updates\n"
570 "Name of an IP prefix-list\n"
571 "Filter incoming routing updates\n"
572 "Filter outgoing routing updates\n")
573 {
574 enum distribute_type type;
575
576 /* Check of distribute list type. */
577 if (strncmp (argv[1], "i", 1) == 0)
578 type = DISTRIBUTE_V6_IN;
579 else if (strncmp (argv[1], "o", 1) == 0)
580 type = DISTRIBUTE_V6_OUT;
581 else
582 {
583 vty_out (vty, "distribute list direction must be [in|out]%s",
584 VTY_NEWLINE);
585 return CMD_WARNING;
586 }
587
588 /* Get interface name corresponding distribute list. */
589 distribute_list_prefix_set (NULL, type, argv[0]);
590
591 return CMD_SUCCESS;
592 }
593
594 ALIAS (ipv6_distribute_list_prefix_all,
595 ipv6_as_v4_distribute_list_prefix_all_cmd,
596 "distribute-list prefix WORD (in|out)",
597 "Filter networks in routing updates\n"
598 "Filter prefixes in routing updates\n"
599 "Name of an IP prefix-list\n"
600 "Filter incoming routing updates\n"
601 "Filter outgoing routing updates\n")
602
603 DEFUN (no_distribute_list_prefix_all,
604 no_distribute_list_prefix_all_cmd,
605 "no distribute-list prefix WORD (in|out)",
606 NO_STR
607 "Filter networks in routing updates\n"
608 "Filter prefixes in routing updates\n"
609 "Name of an IP prefix-list\n"
610 "Filter incoming routing updates\n"
611 "Filter outgoing routing updates\n")
612 {
613 int ret;
614 enum distribute_type type;
615
616 /* Check of distribute list type. */
617 if (strncmp (argv[1], "i", 1) == 0)
618 type = DISTRIBUTE_V4_IN;
619 else if (strncmp (argv[1], "o", 1) == 0)
620 type = DISTRIBUTE_V4_OUT;
621 else
622 {
623 vty_out (vty, "distribute list direction must be [in|out]%s",
624 VTY_NEWLINE);
625 return CMD_WARNING;
626 }
627
628 ret = distribute_list_prefix_unset (NULL, type, argv[0]);
629 if (! ret)
630 {
631 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
632 return CMD_WARNING;
633 }
634 return CMD_SUCCESS;
635 }
636
637 DEFUN (no_ipv6_distribute_list_prefix_all,
638 no_ipv6_distribute_list_prefix_all_cmd,
639 "no ipv6 distribute-list prefix WORD (in|out)",
640 NO_STR
641 "Filter networks in routing updates\n"
642 "Filter prefixes in routing updates\n"
643 "Name of an IP prefix-list\n"
644 "Filter incoming routing updates\n"
645 "Filter outgoing routing updates\n")
646 {
647 int ret;
648 enum distribute_type type;
649
650 /* Check of distribute list type. */
651 if (strncmp (argv[1], "i", 1) == 0)
652 type = DISTRIBUTE_V6_IN;
653 else if (strncmp (argv[1], "o", 1) == 0)
654 type = DISTRIBUTE_V6_OUT;
655 else
656 {
657 vty_out (vty, "distribute list direction must be [in|out]%s",
658 VTY_NEWLINE);
659 return CMD_WARNING;
660 }
661
662 ret = distribute_list_prefix_unset (NULL, type, argv[0]);
663 if (! ret)
664 {
665 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
666 return CMD_WARNING;
667 }
668 return CMD_SUCCESS;
669 }
670
671 ALIAS (no_ipv6_distribute_list_prefix_all,
672 no_ipv6_as_v4_distribute_list_prefix_all_cmd,
673 "no distribute-list prefix WORD (in|out)",
674 NO_STR
675 "Filter networks in routing updates\n"
676 "Filter prefixes in routing updates\n"
677 "Name of an IP prefix-list\n"
678 "Filter incoming routing updates\n"
679 "Filter outgoing routing updates\n")
680
681 DEFUN (distribute_list_prefix, distribute_list_prefix_cmd,
682 "distribute-list prefix WORD (in|out) WORD",
683 "Filter networks in routing updates\n"
684 "Filter prefixes in routing updates\n"
685 "Name of an IP prefix-list\n"
686 "Filter incoming routing updates\n"
687 "Filter outgoing routing updates\n"
688 "Interface name\n")
689 {
690 enum distribute_type type;
691
692 /* Check of distribute list type. */
693 if (strncmp (argv[1], "i", 1) == 0)
694 type = DISTRIBUTE_V4_IN;
695 else if (strncmp (argv[1], "o", 1) == 0)
696 type = DISTRIBUTE_V4_OUT;
697 else
698 {
699 vty_out (vty, "distribute list direction must be [in|out]%s",
700 VTY_NEWLINE);
701 return CMD_WARNING;
702 }
703
704 /* Get interface name corresponding distribute list. */
705 distribute_list_prefix_set (argv[2], type, argv[0]);
706
707 return CMD_SUCCESS;
708 }
709
710 DEFUN (ipv6_distribute_list_prefix,
711 ipv6_distribute_list_prefix_cmd,
712 "ipv6 distribute-list prefix WORD (in|out) WORD",
713 "Filter networks in routing updates\n"
714 "Filter prefixes in routing updates\n"
715 "Name of an IP prefix-list\n"
716 "Filter incoming routing updates\n"
717 "Filter outgoing routing updates\n"
718 "Interface name\n")
719 {
720 enum distribute_type type;
721
722 /* Check of distribute list type. */
723 if (strncmp (argv[1], "i", 1) == 0)
724 type = DISTRIBUTE_V6_IN;
725 else if (strncmp (argv[1], "o", 1) == 0)
726 type = DISTRIBUTE_V6_OUT;
727 else
728 {
729 vty_out (vty, "distribute list direction must be [in|out]%s",
730 VTY_NEWLINE);
731 return CMD_WARNING;
732 }
733
734 /* Get interface name corresponding distribute list. */
735 distribute_list_prefix_set (argv[2], type, argv[0]);
736
737 return CMD_SUCCESS;
738 }
739
740 ALIAS (ipv6_distribute_list_prefix,
741 ipv6_as_v4_distribute_list_prefix_cmd,
742 "distribute-list prefix WORD (in|out) WORD",
743 "Filter networks in routing updates\n"
744 "Filter prefixes in routing updates\n"
745 "Name of an IP prefix-list\n"
746 "Filter incoming routing updates\n"
747 "Filter outgoing routing updates\n"
748 "Interface name\n")
749
750 DEFUN (no_distribute_list_prefix, no_distribute_list_prefix_cmd,
751 "no distribute-list prefix WORD (in|out) WORD",
752 NO_STR
753 "Filter networks in routing updates\n"
754 "Filter prefixes in routing updates\n"
755 "Name of an IP prefix-list\n"
756 "Filter incoming routing updates\n"
757 "Filter outgoing routing updates\n"
758 "Interface name\n")
759 {
760 int ret;
761 enum distribute_type type;
762
763 /* Check of distribute list type. */
764 if (strncmp (argv[1], "i", 1) == 0)
765 type = DISTRIBUTE_V4_IN;
766 else if (strncmp (argv[1], "o", 1) == 0)
767 type = DISTRIBUTE_V4_OUT;
768 else
769 {
770 vty_out (vty, "distribute list direction must be [in|out]%s",
771 VTY_NEWLINE);
772 return CMD_WARNING;
773 }
774
775 ret = distribute_list_prefix_unset (argv[2], type, argv[0]);
776 if (! ret)
777 {
778 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
779 return CMD_WARNING;
780 }
781 return CMD_SUCCESS;
782 }
783
784 DEFUN (no_ipv6_distribute_list_prefix,
785 no_ipv6_distribute_list_prefix_cmd,
786 "no ipv6 distribute-list prefix WORD (in|out) WORD",
787 NO_STR
788 "Filter networks in routing updates\n"
789 "Filter prefixes in routing updates\n"
790 "Name of an IP prefix-list\n"
791 "Filter incoming routing updates\n"
792 "Filter outgoing routing updates\n"
793 "Interface name\n")
794 {
795 int ret;
796 enum distribute_type type;
797
798 /* Check of distribute list type. */
799 if (strncmp (argv[1], "i", 1) == 0)
800 type = DISTRIBUTE_V6_IN;
801 else if (strncmp (argv[1], "o", 1) == 0)
802 type = DISTRIBUTE_V6_OUT;
803 else
804 {
805 vty_out (vty, "distribute list direction must be [in|out]%s",
806 VTY_NEWLINE);
807 return CMD_WARNING;
808 }
809
810 ret = distribute_list_prefix_unset (argv[2], type, argv[0]);
811 if (! ret)
812 {
813 vty_out (vty, "distribute list doesn't exist%s", VTY_NEWLINE);
814 return CMD_WARNING;
815 }
816 return CMD_SUCCESS;
817 }
818
819 ALIAS (no_ipv6_distribute_list_prefix,
820 no_ipv6_as_v4_distribute_list_prefix_cmd,
821 "no distribute-list prefix WORD (in|out) WORD",
822 NO_STR
823 "Filter networks in routing updates\n"
824 "Filter prefixes in routing updates\n"
825 "Name of an IP prefix-list\n"
826 "Filter incoming routing updates\n"
827 "Filter outgoing routing updates\n"
828 "Interface name\n")
829
830 static int
831 distribute_print (struct vty *vty, char *tab[], int is_prefix,
832 enum distribute_type type, int has_print)
833 {
834 if (tab[type]) {
835 vty_out (vty, "%s %s%s",
836 has_print ? "," : "",
837 is_prefix ? "(prefix-list) " : "",
838 tab[type]);
839 return 1;
840 }
841 return has_print;
842 }
843
844 int
845 config_show_distribute (struct vty *vty)
846 {
847 unsigned int i;
848 int has_print = 0;
849 struct hash_backet *mp;
850 struct distribute *dist;
851
852 /* Output filter configuration. */
853 dist = distribute_lookup (NULL);
854 vty_out(vty, " Outgoing update filter list for all interface is");
855 has_print = 0;
856 if (dist)
857 {
858 has_print = distribute_print(vty, dist->list, 0,
859 DISTRIBUTE_V4_OUT, has_print);
860 has_print = distribute_print(vty, dist->prefix, 1,
861 DISTRIBUTE_V4_OUT, has_print);
862 has_print = distribute_print(vty, dist->list, 0,
863 DISTRIBUTE_V6_OUT, has_print);
864 has_print = distribute_print(vty, dist->prefix, 1,
865 DISTRIBUTE_V6_OUT, has_print);
866 }
867 if (has_print)
868 vty_out (vty, "%s", VTY_NEWLINE);
869 else
870 vty_out (vty, " not set%s", VTY_NEWLINE);
871
872 for (i = 0; i < disthash->size; i++)
873 for (mp = disthash->index[i]; mp; mp = mp->next)
874 {
875 dist = mp->data;
876 if (dist->ifname)
877 {
878 vty_out (vty, " %s filtered by", dist->ifname);
879 has_print = 0;
880 has_print = distribute_print(vty, dist->list, 0,
881 DISTRIBUTE_V4_OUT, has_print);
882 has_print = distribute_print(vty, dist->prefix, 1,
883 DISTRIBUTE_V4_OUT, has_print);
884 has_print = distribute_print(vty, dist->list, 0,
885 DISTRIBUTE_V6_OUT, has_print);
886 has_print = distribute_print(vty, dist->prefix, 1,
887 DISTRIBUTE_V6_OUT, has_print);
888 if (has_print)
889 vty_out (vty, "%s", VTY_NEWLINE);
890 else
891 vty_out(vty, " nothing%s", VTY_NEWLINE);
892 }
893 }
894
895
896 /* Input filter configuration. */
897 dist = distribute_lookup (NULL);
898 vty_out(vty, " Incoming update filter list for all interface is");
899 has_print = 0;
900 if (dist)
901 {
902 has_print = distribute_print(vty, dist->list, 0,
903 DISTRIBUTE_V4_IN, has_print);
904 has_print = distribute_print(vty, dist->prefix, 1,
905 DISTRIBUTE_V4_IN, has_print);
906 has_print = distribute_print(vty, dist->list, 0,
907 DISTRIBUTE_V6_IN, has_print);
908 has_print = distribute_print(vty, dist->prefix, 1,
909 DISTRIBUTE_V6_IN, has_print);
910 }
911 if (has_print)
912 vty_out (vty, "%s", VTY_NEWLINE);
913 else
914 vty_out (vty, " not set%s", VTY_NEWLINE);
915
916 for (i = 0; i < disthash->size; i++)
917 for (mp = disthash->index[i]; mp; mp = mp->next)
918 {
919 dist = mp->data;
920 if (dist->ifname)
921 {
922 vty_out (vty, " %s filtered by", dist->ifname);
923 has_print = 0;
924 has_print = distribute_print(vty, dist->list, 0,
925 DISTRIBUTE_V4_IN, has_print);
926 has_print = distribute_print(vty, dist->prefix, 1,
927 DISTRIBUTE_V4_IN, has_print);
928 has_print = distribute_print(vty, dist->list, 0,
929 DISTRIBUTE_V6_IN, has_print);
930 has_print = distribute_print(vty, dist->prefix, 1,
931 DISTRIBUTE_V6_IN, has_print);
932 if (has_print)
933 vty_out (vty, "%s", VTY_NEWLINE);
934 else
935 vty_out(vty, " nothing%s", VTY_NEWLINE);
936 }
937 }
938 return 0;
939 }
940
941 /* Configuration write function. */
942 int
943 config_write_distribute (struct vty *vty)
944 {
945 unsigned int i;
946 int j;
947 int output, v6;
948 struct hash_backet *mp;
949 int write = 0;
950
951 for (i = 0; i < disthash->size; i++)
952 for (mp = disthash->index[i]; mp; mp = mp->next)
953 {
954 struct distribute *dist;
955
956 dist = mp->data;
957
958 for (j = 0; j < DISTRIBUTE_MAX; j++)
959 if (dist->list[j]) {
960 output = j == DISTRIBUTE_V4_OUT || j == DISTRIBUTE_V6_OUT;
961 v6 = j == DISTRIBUTE_V6_IN || j == DISTRIBUTE_V6_OUT;
962 vty_out (vty, " %sdistribute-list %s %s %s%s",
963 v6 ? "ipv6 " : "",
964 dist->list[j],
965 output ? "out" : "in",
966 dist->ifname ? dist->ifname : "",
967 VTY_NEWLINE);
968 write++;
969 }
970
971 for (j = 0; j < DISTRIBUTE_MAX; j++)
972 if (dist->prefix[j]) {
973 output = j == DISTRIBUTE_V4_OUT || j == DISTRIBUTE_V6_OUT;
974 v6 = j == DISTRIBUTE_V6_IN || j == DISTRIBUTE_V6_OUT;
975 vty_out (vty, " %sdistribute-list prefix %s %s %s%s",
976 v6 ? "ipv6 " : "",
977 dist->prefix[j],
978 output ? "out" : "in",
979 dist->ifname ? dist->ifname : "",
980 VTY_NEWLINE);
981 write++;
982 }
983 }
984 return write;
985 }
986
987 /* Clear all distribute list. */
988 void
989 distribute_list_reset ()
990 {
991 hash_clean (disthash, (void (*) (void *)) distribute_free);
992 }
993
994 /* Initialize distribute list related hash. */
995 void
996 distribute_list_init (int node)
997 {
998 disthash = hash_create (distribute_hash_make,
999 (int (*) (const void *, const void *)) distribute_cmp);
1000 /* install v4 */
1001 if (node == RIP_NODE) {
1002 install_element (node, &distribute_list_all_cmd);
1003 install_element (node, &no_distribute_list_all_cmd);
1004 install_element (node, &distribute_list_cmd);
1005 install_element (node, &no_distribute_list_cmd);
1006 install_element (node, &distribute_list_prefix_all_cmd);
1007 install_element (node, &no_distribute_list_prefix_all_cmd);
1008 install_element (node, &distribute_list_prefix_cmd);
1009 install_element (node, &no_distribute_list_prefix_cmd);
1010 }
1011
1012 /* install v6 */
1013 if (node == RIPNG_NODE) {
1014 install_element (node, &ipv6_distribute_list_all_cmd);
1015 install_element (node, &no_ipv6_distribute_list_all_cmd);
1016 install_element (node, &ipv6_distribute_list_cmd);
1017 install_element (node, &no_ipv6_distribute_list_cmd);
1018 install_element (node, &ipv6_distribute_list_prefix_all_cmd);
1019 install_element (node, &no_ipv6_distribute_list_prefix_all_cmd);
1020 install_element (node, &ipv6_distribute_list_prefix_cmd);
1021 install_element (node, &no_ipv6_distribute_list_prefix_cmd);
1022 }
1023
1024 /* install v4 syntax command for v6 only protocols. */
1025 if (node == RIPNG_NODE) {
1026 install_element (node, &ipv6_as_v4_distribute_list_all_cmd);
1027 install_element (node, &no_ipv6_as_v4_distribute_list_all_cmd);
1028 install_element (node, &ipv6_as_v4_distribute_list_cmd);
1029 install_element (node, &no_ipv6_as_v4_distribute_list_cmd);
1030 install_element (node, &ipv6_as_v4_distribute_list_prefix_all_cmd);
1031 install_element (node, &no_ipv6_as_v4_distribute_list_prefix_all_cmd);
1032 install_element (node, &ipv6_as_v4_distribute_list_prefix_cmd);
1033 install_element (node, &no_ipv6_as_v4_distribute_list_prefix_cmd);
1034 }
1035 }