1 /* Prefix list functions.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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.
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.
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
27 #include "sockunion.h"
34 #include "plist_int.h"
36 DEFINE_MTYPE_STATIC(LIB
, PREFIX_LIST
, "Prefix List")
37 DEFINE_MTYPE_STATIC(LIB
, MPREFIX_LIST_STR
, "Prefix List Str")
38 DEFINE_MTYPE_STATIC(LIB
, PREFIX_LIST_ENTRY
, "Prefix List Entry")
39 DEFINE_MTYPE_STATIC(LIB
, PREFIX_LIST_TRIE
, "Prefix List Trie Table")
41 /* not currently changeable, code assumes bytes further down */
43 #define PLC_LEN (1 << PLC_BITS)
44 #define PLC_MAXLEVELV4 2 /* /24 for IPv4 */
45 #define PLC_MAXLEVELV6 4 /* /48 for IPv6 */
46 #define PLC_MAXLEVEL 4 /* max(v4,v6) */
50 struct pltrie_table
*next_table
;
51 struct prefix_list_entry
*final_chain
;
54 struct prefix_list_entry
*up_chain
;
58 struct pltrie_entry entries
[PLC_LEN
];
61 /* List of struct prefix_list. */
62 struct prefix_list_list
{
63 struct prefix_list
*head
;
64 struct prefix_list
*tail
;
67 /* Master structure of prefix_list. */
68 struct prefix_master
{
69 /* List of prefix_list which name is number. */
70 struct prefix_list_list num
;
72 /* List of prefix_list which name is string. */
73 struct prefix_list_list str
;
75 /* Whether sequential number is used. */
78 /* The latest update. */
79 struct prefix_list
*recent
;
81 /* Hook function which is executed when new prefix_list is added. */
82 void (*add_hook
)(struct prefix_list
*);
84 /* Hook function which is executed when prefix_list is deleted. */
85 void (*delete_hook
)(struct prefix_list
*);
87 /* number of bytes that have a trie level */
91 /* Static structure of IPv4 prefix_list's master. */
92 static struct prefix_master prefix_master_ipv4
= {
93 {NULL
, NULL
}, {NULL
, NULL
}, 1, NULL
, NULL
, NULL
, PLC_MAXLEVELV4
,
96 /* Static structure of IPv6 prefix-list's master. */
97 static struct prefix_master prefix_master_ipv6
= {
98 {NULL
, NULL
}, {NULL
, NULL
}, 1, NULL
, NULL
, NULL
, PLC_MAXLEVELV6
,
101 /* Static structure of BGP ORF prefix_list's master. */
102 static struct prefix_master prefix_master_orf_v4
= {
103 {NULL
, NULL
}, {NULL
, NULL
}, 1, NULL
, NULL
, NULL
, PLC_MAXLEVELV4
,
106 /* Static structure of BGP ORF prefix_list's master. */
107 static struct prefix_master prefix_master_orf_v6
= {
108 {NULL
, NULL
}, {NULL
, NULL
}, 1, NULL
, NULL
, NULL
, PLC_MAXLEVELV6
,
111 static struct prefix_master
*prefix_master_get(afi_t afi
, int orf
)
114 return orf
? &prefix_master_orf_v4
: &prefix_master_ipv4
;
116 return orf
? &prefix_master_orf_v6
: &prefix_master_ipv6
;
120 const char *prefix_list_name(struct prefix_list
*plist
)
125 afi_t
prefix_list_afi(struct prefix_list
*plist
)
127 if (plist
->master
== &prefix_master_ipv4
128 || plist
->master
== &prefix_master_orf_v4
)
133 /* Lookup prefix_list from list of prefix_list by name. */
134 static struct prefix_list
*prefix_list_lookup_do(afi_t afi
, int orf
,
137 struct prefix_list
*plist
;
138 struct prefix_master
*master
;
143 master
= prefix_master_get(afi
, orf
);
147 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
148 if (strcmp(plist
->name
, name
) == 0)
151 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
152 if (strcmp(plist
->name
, name
) == 0)
158 struct prefix_list
*prefix_list_lookup(afi_t afi
, const char *name
)
160 return prefix_list_lookup_do(afi
, 0, name
);
163 struct prefix_list
*prefix_bgp_orf_lookup(afi_t afi
, const char *name
)
165 return prefix_list_lookup_do(afi
, 1, name
);
168 static struct prefix_list
*prefix_list_new(void)
170 struct prefix_list
*new;
172 new = XCALLOC(MTYPE_PREFIX_LIST
, sizeof(struct prefix_list
));
176 static void prefix_list_free(struct prefix_list
*plist
)
178 XFREE(MTYPE_PREFIX_LIST
, plist
);
181 static struct prefix_list_entry
*prefix_list_entry_new(void)
183 struct prefix_list_entry
*new;
185 new = XCALLOC(MTYPE_PREFIX_LIST_ENTRY
,
186 sizeof(struct prefix_list_entry
));
190 static void prefix_list_entry_free(struct prefix_list_entry
*pentry
)
192 XFREE(MTYPE_PREFIX_LIST_ENTRY
, pentry
);
195 /* Insert new prefix list to list of prefix_list. Each prefix_list
196 is sorted by the name. */
197 static struct prefix_list
*prefix_list_insert(afi_t afi
, int orf
,
202 struct prefix_list
*plist
;
203 struct prefix_list
*point
;
204 struct prefix_list_list
*list
;
205 struct prefix_master
*master
;
207 master
= prefix_master_get(afi
, orf
);
211 /* Allocate new prefix_list and copy given name. */
212 plist
= prefix_list_new();
213 plist
->name
= XSTRDUP(MTYPE_MPREFIX_LIST_STR
, name
);
214 plist
->master
= master
;
216 XCALLOC(MTYPE_PREFIX_LIST_TRIE
, sizeof(struct pltrie_table
));
218 /* If name is made by all digit character. We treat it as
220 for (number
= 0, i
= 0; i
< strlen(name
); i
++) {
221 if (isdigit((int)name
[i
]))
222 number
= (number
* 10) + (name
[i
] - '0');
227 /* In case of name is all digit character */
228 if (i
== strlen(name
)) {
229 plist
->type
= PREFIX_TYPE_NUMBER
;
231 /* Set prefix_list to number list. */
234 for (point
= list
->head
; point
; point
= point
->next
)
235 if (atol(point
->name
) >= number
)
238 plist
->type
= PREFIX_TYPE_STRING
;
240 /* Set prefix_list to string list. */
243 /* Set point to insertion point. */
244 for (point
= list
->head
; point
; point
= point
->next
)
245 if (strcmp(point
->name
, name
) >= 0)
249 /* In case of this is the first element of master. */
250 if (list
->head
== NULL
) {
251 list
->head
= list
->tail
= plist
;
255 /* In case of insertion is made at the tail of access_list. */
257 plist
->prev
= list
->tail
;
258 list
->tail
->next
= plist
;
263 /* In case of insertion is made at the head of access_list. */
264 if (point
== list
->head
) {
265 plist
->next
= list
->head
;
266 list
->head
->prev
= plist
;
271 /* Insertion is made at middle of the access_list. */
273 plist
->prev
= point
->prev
;
276 point
->prev
->next
= plist
;
282 static struct prefix_list
*prefix_list_get(afi_t afi
, int orf
, const char *name
)
284 struct prefix_list
*plist
;
286 plist
= prefix_list_lookup_do(afi
, orf
, name
);
289 plist
= prefix_list_insert(afi
, orf
, name
);
293 static void prefix_list_trie_del(struct prefix_list
*plist
,
294 struct prefix_list_entry
*pentry
);
296 /* Delete prefix-list from prefix_list_master and free it. */
297 static void prefix_list_delete(struct prefix_list
*plist
)
299 struct prefix_list_list
*list
;
300 struct prefix_master
*master
;
301 struct prefix_list_entry
*pentry
;
302 struct prefix_list_entry
*next
;
304 /* If prefix-list contain prefix_list_entry free all of it. */
305 for (pentry
= plist
->head
; pentry
; pentry
= next
) {
307 prefix_list_trie_del(plist
, pentry
);
308 prefix_list_entry_free(pentry
);
312 master
= plist
->master
;
314 if (plist
->type
== PREFIX_TYPE_NUMBER
)
320 plist
->next
->prev
= plist
->prev
;
322 list
->tail
= plist
->prev
;
325 plist
->prev
->next
= plist
->next
;
327 list
->head
= plist
->next
;
329 XFREE(MTYPE_TMP
, plist
->desc
);
331 /* Make sure master's recent changed prefix-list information is
333 master
->recent
= NULL
;
335 route_map_notify_dependencies(plist
->name
, RMAP_EVENT_PLIST_DELETED
);
337 if (master
->delete_hook
)
338 (*master
->delete_hook
)(plist
);
340 XFREE(MTYPE_MPREFIX_LIST_STR
, plist
->name
);
342 XFREE(MTYPE_PREFIX_LIST_TRIE
, plist
->trie
);
344 prefix_list_free(plist
);
347 static struct prefix_list_entry
*
348 prefix_list_entry_make(struct prefix
*prefix
, enum prefix_list_type type
,
349 int64_t seq
, int le
, int ge
, int any
)
351 struct prefix_list_entry
*pentry
;
353 pentry
= prefix_list_entry_new();
358 prefix_copy(&pentry
->prefix
, prefix
);
367 /* Add hook function. */
368 void prefix_list_add_hook(void (*func
)(struct prefix_list
*plist
))
370 prefix_master_ipv4
.add_hook
= func
;
371 prefix_master_ipv6
.add_hook
= func
;
374 /* Delete hook function. */
375 void prefix_list_delete_hook(void (*func
)(struct prefix_list
*plist
))
377 prefix_master_ipv4
.delete_hook
= func
;
378 prefix_master_ipv6
.delete_hook
= func
;
381 /* Calculate new sequential number. */
382 static int64_t prefix_new_seq_get(struct prefix_list
*plist
)
386 struct prefix_list_entry
*pentry
;
390 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
391 if (maxseq
< pentry
->seq
)
392 maxseq
= pentry
->seq
;
395 newseq
= ((maxseq
/ 5) * 5) + 5;
397 return (newseq
> UINT_MAX
) ? UINT_MAX
: newseq
;
400 /* Return prefix list entry which has same seq number. */
401 static struct prefix_list_entry
*prefix_seq_check(struct prefix_list
*plist
,
404 struct prefix_list_entry
*pentry
;
406 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
)
407 if (pentry
->seq
== seq
)
412 static struct prefix_list_entry
*
413 prefix_list_entry_lookup(struct prefix_list
*plist
, struct prefix
*prefix
,
414 enum prefix_list_type type
, int64_t seq
,
417 struct prefix_list_entry
*pentry
;
419 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
)
420 if (prefix_same(&pentry
->prefix
, prefix
)
421 && pentry
->type
== type
) {
422 if (seq
>= 0 && pentry
->seq
!= seq
)
425 if (pentry
->le
!= le
)
427 if (pentry
->ge
!= ge
)
436 static void trie_walk_affected(size_t validbits
, struct pltrie_table
*table
,
437 uint8_t byte
, struct prefix_list_entry
*object
,
438 void (*fn
)(struct prefix_list_entry
*object
,
439 struct prefix_list_entry
**updptr
))
444 if (validbits
> PLC_BITS
) {
445 fn(object
, &table
->entries
[byte
].final_chain
);
449 mask
= (1 << (8 - validbits
)) - 1;
450 for (bwalk
= byte
& ~mask
; bwalk
<= byte
+ mask
; bwalk
++) {
451 fn(object
, &table
->entries
[bwalk
].up_chain
);
455 static void trie_uninstall_fn(struct prefix_list_entry
*object
,
456 struct prefix_list_entry
**updptr
)
458 for (; *updptr
; updptr
= &(*updptr
)->next_best
)
459 if (*updptr
== object
) {
460 *updptr
= object
->next_best
;
465 static int trie_table_empty(struct pltrie_table
*table
)
468 for (i
= 0; i
< PLC_LEN
; i
++)
469 if (table
->entries
[i
].next_table
|| table
->entries
[i
].up_chain
)
474 static void prefix_list_trie_del(struct prefix_list
*plist
,
475 struct prefix_list_entry
*pentry
)
477 size_t depth
, maxdepth
= plist
->master
->trie_depth
;
478 uint8_t *bytes
= pentry
->prefix
.u
.val
;
479 size_t validbits
= pentry
->prefix
.prefixlen
;
480 struct pltrie_table
*table
, **tables
[PLC_MAXLEVEL
];
483 for (depth
= 0; validbits
> PLC_BITS
&& depth
< maxdepth
- 1; depth
++) {
484 uint8_t byte
= bytes
[depth
];
485 assert(table
->entries
[byte
].next_table
);
487 tables
[depth
+ 1] = &table
->entries
[byte
].next_table
;
488 table
= table
->entries
[byte
].next_table
;
490 validbits
-= PLC_BITS
;
493 trie_walk_affected(validbits
, table
, bytes
[depth
], pentry
,
496 for (; depth
> 0; depth
--)
497 if (trie_table_empty(*tables
[depth
])) {
498 XFREE(MTYPE_PREFIX_LIST_TRIE
, *tables
[depth
]);
499 *tables
[depth
] = NULL
;
504 static void prefix_list_entry_delete(struct prefix_list
*plist
,
505 struct prefix_list_entry
*pentry
,
508 if (plist
== NULL
|| pentry
== NULL
)
511 prefix_list_trie_del(plist
, pentry
);
514 pentry
->prev
->next
= pentry
->next
;
516 plist
->head
= pentry
->next
;
518 pentry
->next
->prev
= pentry
->prev
;
520 plist
->tail
= pentry
->prev
;
522 prefix_list_entry_free(pentry
);
527 route_map_notify_dependencies(plist
->name
,
528 RMAP_EVENT_PLIST_DELETED
);
529 if (plist
->master
->delete_hook
)
530 (*plist
->master
->delete_hook
)(plist
);
532 if (plist
->head
== NULL
&& plist
->tail
== NULL
533 && plist
->desc
== NULL
)
534 prefix_list_delete(plist
);
536 plist
->master
->recent
= plist
;
540 static void trie_install_fn(struct prefix_list_entry
*object
,
541 struct prefix_list_entry
**updptr
)
544 if (*updptr
== object
)
546 if ((*updptr
)->prefix
.prefixlen
< object
->prefix
.prefixlen
)
548 if ((*updptr
)->prefix
.prefixlen
== object
->prefix
.prefixlen
549 && (*updptr
)->seq
> object
->seq
)
551 updptr
= &(*updptr
)->next_best
;
554 if (!object
->next_best
)
555 object
->next_best
= *updptr
;
557 assert(object
->next_best
== *updptr
|| !*updptr
);
562 static void prefix_list_trie_add(struct prefix_list
*plist
,
563 struct prefix_list_entry
*pentry
)
565 size_t depth
= plist
->master
->trie_depth
;
566 uint8_t *bytes
= pentry
->prefix
.u
.val
;
567 size_t validbits
= pentry
->prefix
.prefixlen
;
568 struct pltrie_table
*table
;
571 while (validbits
> PLC_BITS
&& depth
> 1) {
572 if (!table
->entries
[*bytes
].next_table
)
573 table
->entries
[*bytes
].next_table
=
574 XCALLOC(MTYPE_PREFIX_LIST_TRIE
,
575 sizeof(struct pltrie_table
));
576 table
= table
->entries
[*bytes
].next_table
;
579 validbits
-= PLC_BITS
;
582 trie_walk_affected(validbits
, table
, *bytes
, pentry
, trie_install_fn
);
585 static void prefix_list_entry_add(struct prefix_list
*plist
,
586 struct prefix_list_entry
*pentry
)
588 struct prefix_list_entry
*replace
;
589 struct prefix_list_entry
*point
;
591 /* Automatic asignment of seq no. */
592 if (pentry
->seq
== -1)
593 pentry
->seq
= prefix_new_seq_get(plist
);
595 if (plist
->tail
&& pentry
->seq
> plist
->tail
->seq
)
598 /* Is there any same seq prefix list entry? */
599 replace
= prefix_seq_check(plist
, pentry
->seq
);
601 prefix_list_entry_delete(plist
, replace
, 0);
603 /* Check insert point. */
604 for (point
= plist
->head
; point
; point
= point
->next
)
605 if (point
->seq
>= pentry
->seq
)
609 /* In case of this is the first element of the list. */
610 pentry
->next
= point
;
614 point
->prev
->next
= pentry
;
616 plist
->head
= pentry
;
618 pentry
->prev
= point
->prev
;
619 point
->prev
= pentry
;
622 plist
->tail
->next
= pentry
;
624 plist
->head
= pentry
;
626 pentry
->prev
= plist
->tail
;
627 plist
->tail
= pentry
;
630 prefix_list_trie_add(plist
, pentry
);
632 /* Increment count. */
635 /* Run hook function. */
636 if (plist
->master
->add_hook
)
637 (*plist
->master
->add_hook
)(plist
);
639 route_map_notify_dependencies(plist
->name
, RMAP_EVENT_PLIST_ADDED
);
640 plist
->master
->recent
= plist
;
643 /* Return string of prefix_list_type. */
644 static const char *prefix_list_type_str(struct prefix_list_entry
*pentry
)
646 switch (pentry
->type
) {
656 static int prefix_list_entry_match(struct prefix_list_entry
*pentry
,
657 const struct prefix
*p
)
661 if (pentry
->prefix
.family
!= p
->family
)
664 ret
= prefix_match(&pentry
->prefix
, p
);
668 /* In case of le nor ge is specified, exact match is performed. */
669 if (!pentry
->le
&& !pentry
->ge
) {
670 if (pentry
->prefix
.prefixlen
!= p
->prefixlen
)
674 if (p
->prefixlen
> pentry
->le
)
678 if (p
->prefixlen
< pentry
->ge
)
684 enum prefix_list_type
prefix_list_apply_which_prefix(
685 struct prefix_list
*plist
,
686 const struct prefix
**which
,
689 struct prefix_list_entry
*pentry
, *pbest
= NULL
;
691 const struct prefix
*p
= (const struct prefix
*)object
;
692 const uint8_t *byte
= p
->u
.val
;
694 size_t validbits
= p
->prefixlen
;
695 struct pltrie_table
*table
;
703 if (plist
->count
== 0) {
706 return PREFIX_PERMIT
;
709 depth
= plist
->master
->trie_depth
;
712 for (pentry
= table
->entries
[*byte
].up_chain
; pentry
;
713 pentry
= pentry
->next_best
) {
714 if (pbest
&& pbest
->seq
< pentry
->seq
)
716 if (prefix_list_entry_match(pentry
, p
))
720 if (validbits
<= PLC_BITS
)
722 validbits
-= PLC_BITS
;
725 if (!table
->entries
[*byte
].next_table
)
728 table
= table
->entries
[*byte
].next_table
;
733 for (pentry
= table
->entries
[*byte
].final_chain
; pentry
;
734 pentry
= pentry
->next_best
) {
735 if (pbest
&& pbest
->seq
< pentry
->seq
)
737 if (prefix_list_entry_match(pentry
, p
))
745 *which
= &pbest
->prefix
;
757 static void __attribute__((unused
)) prefix_list_print(struct prefix_list
*plist
)
759 struct prefix_list_entry
*pentry
;
764 printf("ip prefix-list %s: %d entries\n", plist
->name
, plist
->count
);
766 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
768 printf("any %s\n", prefix_list_type_str(pentry
));
775 printf(" seq %" PRId64
" %s %s/%d", pentry
->seq
,
776 prefix_list_type_str(pentry
),
777 inet_ntop(p
->family
, p
->u
.val
, buf
, BUFSIZ
),
780 printf(" ge %d", pentry
->ge
);
782 printf(" le %d", pentry
->le
);
788 /* Retrun 1 when plist already include pentry policy. */
789 static struct prefix_list_entry
*
790 prefix_entry_dup_check(struct prefix_list
*plist
, struct prefix_list_entry
*new)
792 size_t depth
, maxdepth
= plist
->master
->trie_depth
;
793 uint8_t byte
, *bytes
= new->prefix
.u
.val
;
794 size_t validbits
= new->prefix
.prefixlen
;
795 struct pltrie_table
*table
;
796 struct prefix_list_entry
*pentry
;
800 seq
= prefix_new_seq_get(plist
);
805 for (depth
= 0; validbits
> PLC_BITS
&& depth
< maxdepth
- 1; depth
++) {
807 if (!table
->entries
[byte
].next_table
)
810 table
= table
->entries
[byte
].next_table
;
811 validbits
-= PLC_BITS
;
815 if (validbits
> PLC_BITS
)
816 pentry
= table
->entries
[byte
].final_chain
;
818 pentry
= table
->entries
[byte
].up_chain
;
820 for (; pentry
; pentry
= pentry
->next_best
) {
821 if (prefix_same(&pentry
->prefix
, &new->prefix
)
822 && pentry
->type
== new->type
&& pentry
->le
== new->le
823 && pentry
->ge
== new->ge
&& pentry
->seq
!= seq
)
829 static int vty_invalid_prefix_range(struct vty
*vty
, const char *prefix
)
832 "%% Invalid prefix range for %s, make sure: len < ge-value <= le-value\n",
834 return CMD_WARNING_CONFIG_FAILED
;
837 static int vty_prefix_list_install(struct vty
*vty
, afi_t afi
, const char *name
,
838 const char *seq
, const char *typestr
,
839 const char *prefix
, const char *ge
,
843 enum prefix_list_type type
;
844 struct prefix_list
*plist
;
845 struct prefix_list_entry
*pentry
;
846 struct prefix_list_entry
*dup
;
847 struct prefix p
, p_tmp
;
853 if (name
== NULL
|| prefix
== NULL
|| typestr
== NULL
) {
854 vty_out(vty
, "%% Missing prefix or type\n");
855 return CMD_WARNING_CONFIG_FAILED
;
858 /* Sequential number. */
860 seqnum
= (int64_t)atol(seq
);
862 /* ge and le number */
868 /* Check filter type. */
869 if (strncmp("permit", typestr
, 1) == 0)
870 type
= PREFIX_PERMIT
;
871 else if (strncmp("deny", typestr
, 1) == 0)
874 vty_out(vty
, "%% prefix type must be permit or deny\n");
875 return CMD_WARNING_CONFIG_FAILED
;
878 /* "any" is special token for matching any IPv4 addresses. */
881 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
882 ret
= str2prefix_ipv4("0.0.0.0/0",
883 (struct prefix_ipv4
*)&p
);
885 lenum
= IPV4_MAX_BITLEN
;
888 ret
= str2prefix_ipv4(prefix
, (struct prefix_ipv4
*)&p
);
891 vty_out(vty
, "%% Malformed IPv4 prefix\n");
892 return CMD_WARNING_CONFIG_FAILED
;
895 /* make a copy to verify prefix matches mask length */
896 prefix_copy(&p_tmp
, &p
);
897 apply_mask_ipv4((struct prefix_ipv4
*)&p_tmp
);
901 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
902 ret
= str2prefix_ipv6("::/0", (struct prefix_ipv6
*)&p
);
904 lenum
= IPV6_MAX_BITLEN
;
907 ret
= str2prefix_ipv6(prefix
, (struct prefix_ipv6
*)&p
);
910 vty_out(vty
, "%% Malformed IPv6 prefix\n");
911 return CMD_WARNING_CONFIG_FAILED
;
914 /* make a copy to verify prefix matches mask length */
915 prefix_copy(&p_tmp
, &p
);
916 apply_mask_ipv6((struct prefix_ipv6
*)&p_tmp
);
921 vty_out(vty
, "%% Unrecognized AFI (%d)\n", afi
);
922 return CMD_WARNING_CONFIG_FAILED
;
926 /* If prefix has bits not under the mask, adjust it to fit */
927 if (!prefix_same(&p_tmp
, &p
)) {
928 char buf
[PREFIX2STR_BUFFER
];
929 char buf_tmp
[PREFIX2STR_BUFFER
];
930 prefix2str(&p
, buf
, sizeof(buf
));
931 prefix2str(&p_tmp
, buf_tmp
, sizeof(buf_tmp
));
933 "%% Prefix-list %s prefix changed from %s to %s to match length\n",
936 "Prefix-list %s prefix changed from %s to %s to match length",
941 /* ge and le check. */
942 if (genum
&& (genum
<= p
.prefixlen
))
943 return vty_invalid_prefix_range(vty
, prefix
);
945 if (lenum
&& (lenum
< p
.prefixlen
))
946 return vty_invalid_prefix_range(vty
, prefix
);
948 if (lenum
&& (genum
> lenum
))
949 return vty_invalid_prefix_range(vty
, prefix
);
951 if (genum
&& (lenum
== (afi
== AFI_IP
? 32 : 128)))
954 /* Get prefix_list with name. */
955 plist
= prefix_list_get(afi
, 0, name
);
957 /* Make prefix entry. */
958 pentry
= prefix_list_entry_make(&p
, type
, seqnum
, lenum
, genum
, any
);
960 /* Check same policy. */
961 dup
= prefix_entry_dup_check(plist
, pentry
);
964 prefix_list_entry_free(pentry
);
968 /* Install new filter to the access_list. */
969 prefix_list_entry_add(plist
, pentry
);
974 static int vty_prefix_list_uninstall(struct vty
*vty
, afi_t afi
,
975 const char *name
, const char *seq
,
976 const char *typestr
, const char *prefix
,
977 const char *ge
, const char *le
)
980 enum prefix_list_type type
;
981 struct prefix_list
*plist
;
982 struct prefix_list_entry
*pentry
;
988 /* Check prefix list name. */
989 plist
= prefix_list_lookup(afi
, name
);
991 vty_out(vty
, "%% Can't find specified prefix-list\n");
992 return CMD_WARNING_CONFIG_FAILED
;
995 /* Only prefix-list name specified, delete the entire prefix-list. */
996 if (seq
== NULL
&& typestr
== NULL
&& prefix
== NULL
&& ge
== NULL
998 prefix_list_delete(plist
);
1002 /* Check sequence number. */
1004 seqnum
= (int64_t)atol(seq
);
1006 /* Sequence number specified, but nothing else. */
1007 if (seq
&& typestr
== NULL
&& prefix
== NULL
&& ge
== NULL
1009 pentry
= prefix_seq_check(plist
, seqnum
);
1011 if (pentry
== NULL
) {
1013 "%% Can't find prefix-list %s with sequence number %" PRIu64
"\n",
1015 return CMD_WARNING_CONFIG_FAILED
;
1018 prefix_list_entry_delete(plist
, pentry
, 1);
1022 /* ge and le number */
1028 /* We must have, at a minimum, both the type and prefix here */
1029 if ((typestr
== NULL
) || (prefix
== NULL
))
1030 return CMD_WARNING_CONFIG_FAILED
;
1032 /* Check of filter type. */
1033 if (strncmp("permit", typestr
, 1) == 0)
1034 type
= PREFIX_PERMIT
;
1035 else if (strncmp("deny", typestr
, 1) == 0)
1038 vty_out(vty
, "%% prefix type must be permit or deny\n");
1039 return CMD_WARNING_CONFIG_FAILED
;
1042 /* "any" is special token for matching any IPv4 addresses. */
1043 if (afi
== AFI_IP
) {
1044 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
1045 ret
= str2prefix_ipv4("0.0.0.0/0",
1046 (struct prefix_ipv4
*)&p
);
1048 lenum
= IPV4_MAX_BITLEN
;
1050 ret
= str2prefix_ipv4(prefix
, (struct prefix_ipv4
*)&p
);
1053 vty_out(vty
, "%% Malformed IPv4 prefix\n");
1054 return CMD_WARNING_CONFIG_FAILED
;
1056 } else if (afi
== AFI_IP6
) {
1057 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
1058 ret
= str2prefix_ipv6("::/0", (struct prefix_ipv6
*)&p
);
1060 lenum
= IPV6_MAX_BITLEN
;
1062 ret
= str2prefix_ipv6(prefix
, (struct prefix_ipv6
*)&p
);
1065 vty_out(vty
, "%% Malformed IPv6 prefix\n");
1066 return CMD_WARNING_CONFIG_FAILED
;
1070 /* Lookup prefix entry. */
1072 prefix_list_entry_lookup(plist
, &p
, type
, seqnum
, lenum
, genum
);
1074 if (pentry
== NULL
) {
1075 vty_out(vty
, "%% Can't find specified prefix-list\n");
1076 return CMD_WARNING_CONFIG_FAILED
;
1079 /* Install new filter to the access_list. */
1080 prefix_list_entry_delete(plist
, pentry
, 1);
1085 static int vty_prefix_list_desc_unset(struct vty
*vty
, afi_t afi
,
1088 struct prefix_list
*plist
;
1090 plist
= prefix_list_lookup(afi
, name
);
1092 vty_out(vty
, "%% Can't find specified prefix-list\n");
1093 return CMD_WARNING_CONFIG_FAILED
;
1097 XFREE(MTYPE_TMP
, plist
->desc
);
1101 if (plist
->head
== NULL
&& plist
->tail
== NULL
&& plist
->desc
== NULL
)
1102 prefix_list_delete(plist
);
1116 static void vty_show_prefix_entry(struct vty
*vty
, afi_t afi
,
1117 struct prefix_list
*plist
,
1118 struct prefix_master
*master
,
1119 enum display_type dtype
, int seqnum
)
1121 struct prefix_list_entry
*pentry
;
1123 /* Print the name of the protocol */
1124 vty_out(vty
, "%s: ", frr_protoname
);
1126 if (dtype
== normal_display
) {
1127 vty_out(vty
, "ip%s prefix-list %s: %d entries\n",
1128 afi
== AFI_IP
? "" : "v6", plist
->name
, plist
->count
);
1130 vty_out(vty
, " Description: %s\n", plist
->desc
);
1131 } else if (dtype
== summary_display
|| dtype
== detail_display
) {
1132 vty_out(vty
, "ip%s prefix-list %s:\n",
1133 afi
== AFI_IP
? "" : "v6", plist
->name
);
1136 vty_out(vty
, " Description: %s\n", plist
->desc
);
1139 " count: %d, range entries: %d, sequences: %" PRId64
" - %" PRId64
"\n",
1140 plist
->count
, plist
->rangecount
,
1141 plist
->head
? plist
->head
->seq
: 0,
1142 plist
->tail
? plist
->tail
->seq
: 0);
1145 if (dtype
!= summary_display
) {
1146 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1147 if (dtype
== sequential_display
1148 && pentry
->seq
!= seqnum
)
1154 vty_out(vty
, "seq %" PRId64
" ", pentry
->seq
);
1156 vty_out(vty
, "%s ", prefix_list_type_str(pentry
));
1159 vty_out(vty
, "any");
1161 struct prefix
*p
= &pentry
->prefix
;
1164 vty_out(vty
, "%s/%d",
1165 inet_ntop(p
->family
, p
->u
.val
, buf
,
1170 vty_out(vty
, " ge %d", pentry
->ge
);
1172 vty_out(vty
, " le %d", pentry
->le
);
1175 if (dtype
== detail_display
1176 || dtype
== sequential_display
)
1177 vty_out(vty
, " (hit count: %ld, refcount: %ld)",
1178 pentry
->hitcnt
, pentry
->refcnt
);
1185 static int vty_show_prefix_list(struct vty
*vty
, afi_t afi
, const char *name
,
1186 const char *seq
, enum display_type dtype
)
1188 struct prefix_list
*plist
;
1189 struct prefix_master
*master
;
1192 master
= prefix_master_get(afi
, 0);
1197 seqnum
= (int64_t)atol(seq
);
1200 plist
= prefix_list_lookup(afi
, name
);
1202 vty_out(vty
, "%% Can't find specified prefix-list\n");
1205 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
, seqnum
);
1207 if (dtype
== detail_display
|| dtype
== summary_display
) {
1210 "Prefix-list with the last deletion/insertion: %s\n",
1211 master
->recent
->name
);
1214 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
1215 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
,
1218 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
1219 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
,
1226 static int vty_show_prefix_list_prefix(struct vty
*vty
, afi_t afi
,
1227 const char *name
, const char *prefix
,
1228 enum display_type type
)
1230 struct prefix_list
*plist
;
1231 struct prefix_list_entry
*pentry
;
1236 plist
= prefix_list_lookup(afi
, name
);
1238 vty_out(vty
, "%% Can't find specified prefix-list\n");
1242 ret
= str2prefix(prefix
, &p
);
1244 vty_out(vty
, "%% prefix is malformed\n");
1248 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1251 if (type
== normal_display
|| type
== first_match_display
)
1252 if (prefix_same(&p
, &pentry
->prefix
))
1255 if (type
== longer_display
) {
1256 if ((p
.family
== pentry
->prefix
.family
)
1257 && (prefix_match(&p
, &pentry
->prefix
)))
1262 vty_out(vty
, " seq %" PRId64
" %s ", pentry
->seq
,
1263 prefix_list_type_str(pentry
));
1266 vty_out(vty
, "any");
1268 struct prefix
*pf
= &pentry
->prefix
;
1271 vty_out(vty
, "%s/%d",
1272 inet_ntop(pf
->family
, pf
->u
.val
, buf
,
1277 vty_out(vty
, " ge %d", pentry
->ge
);
1279 vty_out(vty
, " le %d", pentry
->le
);
1282 if (type
== normal_display
1283 || type
== first_match_display
)
1284 vty_out(vty
, " (hit count: %ld, refcount: %ld)",
1285 pentry
->hitcnt
, pentry
->refcnt
);
1289 if (type
== first_match_display
)
1296 static int vty_clear_prefix_list(struct vty
*vty
, afi_t afi
, const char *name
,
1299 struct prefix_master
*master
;
1300 struct prefix_list
*plist
;
1301 struct prefix_list_entry
*pentry
;
1305 master
= prefix_master_get(afi
, 0);
1309 if (name
== NULL
&& prefix
== NULL
) {
1310 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
1311 for (pentry
= plist
->head
; pentry
;
1312 pentry
= pentry
->next
)
1315 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
1316 for (pentry
= plist
->head
; pentry
;
1317 pentry
= pentry
->next
)
1320 plist
= prefix_list_lookup(afi
, name
);
1322 vty_out(vty
, "%% Can't find specified prefix-list\n");
1327 ret
= str2prefix(prefix
, &p
);
1329 vty_out(vty
, "%% prefix is malformed\n");
1334 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1336 if (pentry
->prefix
.family
== p
.family
1337 && prefix_match(&pentry
->prefix
, &p
))
1346 #ifndef VTYSH_EXTRACT_PL
1347 #include "lib/plist_clippy.c"
1350 DEFPY (ip_prefix_list
,
1352 "ip prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|A.B.C.D/M$dest [{ge (0-32)|le (0-32)}]>",
1355 "Name of a prefix list\n"
1356 "sequence number of an entry\n"
1358 "Specify packets to reject\n"
1359 "Specify packets to forward\n"
1360 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1361 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1362 "Minimum prefix length to be matched\n"
1363 "Minimum prefix length\n"
1364 "Maximum prefix length to be matched\n"
1365 "Maximum prefix length\n")
1367 return vty_prefix_list_install(vty
, AFI_IP
, prefix_list
, seq_str
,
1368 action
, dest
, ge_str
, le_str
);
1371 DEFPY (no_ip_prefix_list
,
1372 no_ip_prefix_list_cmd
,
1373 "no ip prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|A.B.C.D/M$dest [{ge (0-32)|le (0-32)}]>",
1377 "Name of a prefix list\n"
1378 "sequence number of an entry\n"
1380 "Specify packets to reject\n"
1381 "Specify packets to forward\n"
1382 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1383 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1384 "Minimum prefix length to be matched\n"
1385 "Minimum prefix length\n"
1386 "Maximum prefix length to be matched\n"
1387 "Maximum prefix length\n")
1389 return vty_prefix_list_uninstall(vty
, AFI_IP
, prefix_list
, seq_str
,
1390 action
, dest
, ge_str
, le_str
);
1393 DEFPY(no_ip_prefix_list_seq
, no_ip_prefix_list_seq_cmd
,
1394 "no ip prefix-list WORD seq (1-4294967295)",
1395 NO_STR IP_STR PREFIX_LIST_STR
1396 "Name of a prefix list\n"
1397 "sequence number of an entry\n"
1398 "Sequence number\n")
1400 return vty_prefix_list_uninstall(vty
, AFI_IP
, prefix_list
, seq_str
,
1401 NULL
, NULL
, NULL
, NULL
);
1404 DEFPY (no_ip_prefix_list_all
,
1405 no_ip_prefix_list_all_cmd
,
1406 "no ip prefix-list WORD",
1410 "Name of a prefix list\n")
1412 return vty_prefix_list_uninstall(vty
, AFI_IP
, prefix_list
, NULL
, NULL
,
1416 DEFPY (ip_prefix_list_sequence_number
,
1417 ip_prefix_list_sequence_number_cmd
,
1418 "[no] ip prefix-list sequence-number",
1422 "Include/exclude sequence numbers in NVGEN\n")
1424 prefix_master_ipv4
.seqnum
= no
? false : true;
1428 DEFUN (ip_prefix_list_description
,
1429 ip_prefix_list_description_cmd
,
1430 "ip prefix-list WORD description LINE...",
1433 "Name of a prefix list\n"
1434 "Prefix-list specific description\n"
1435 "Up to 80 characters describing this prefix-list\n")
1439 struct prefix_list
*plist
;
1441 plist
= prefix_list_get(AFI_IP
, 0, argv
[idx_word
]->arg
);
1444 XFREE(MTYPE_TMP
, plist
->desc
);
1447 plist
->desc
= argv_concat(argv
, argc
, idx_line
);
1452 DEFUN (no_ip_prefix_list_description
,
1453 no_ip_prefix_list_description_cmd
,
1454 "no ip prefix-list WORD description",
1458 "Name of a prefix list\n"
1459 "Prefix-list specific description\n")
1462 return vty_prefix_list_desc_unset(vty
, AFI_IP
, argv
[idx_word
]->arg
);
1466 DEFUN (no_ip_prefix_list_description_comment
,
1467 no_ip_prefix_list_description_comment_cmd
,
1468 "no ip prefix-list WORD description LINE...",
1472 "Name of a prefix list\n"
1473 "Prefix-list specific description\n"
1474 "Up to 80 characters describing this prefix-list\n")
1476 return no_ip_prefix_list_description(self
, vty
, argc
, argv
);
1479 DEFPY (show_ip_prefix_list
,
1480 show_ip_prefix_list_cmd
,
1481 "show ip prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1485 "Name of a prefix list\n"
1486 "sequence number of an entry\n"
1487 "Sequence number\n")
1489 enum display_type dtype
= normal_display
;
1491 dtype
= sequential_display
;
1493 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, arg_str
, dtype
);
1496 DEFPY (show_ip_prefix_list_prefix
,
1497 show_ip_prefix_list_prefix_cmd
,
1498 "show ip prefix-list WORD A.B.C.D/M$prefix [longer$dl|first-match$dfm]",
1502 "Name of a prefix list\n"
1503 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1504 "Lookup longer prefix\n"
1505 "First matched prefix\n")
1507 enum display_type dtype
= normal_display
;
1509 dtype
= longer_display
;
1511 dtype
= first_match_display
;
1513 return vty_show_prefix_list_prefix(vty
, AFI_IP
, prefix_list
, prefix_str
,
1517 DEFPY (show_ip_prefix_list_summary
,
1518 show_ip_prefix_list_summary_cmd
,
1519 "show ip prefix-list summary [WORD$prefix_list]",
1523 "Summary of prefix lists\n"
1524 "Name of a prefix list\n")
1526 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, NULL
,
1530 DEFPY (show_ip_prefix_list_detail
,
1531 show_ip_prefix_list_detail_cmd
,
1532 "show ip prefix-list detail [WORD$prefix_list]",
1536 "Detail of prefix lists\n"
1537 "Name of a prefix list\n")
1539 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, NULL
,
1543 DEFPY (clear_ip_prefix_list
,
1544 clear_ip_prefix_list_cmd
,
1545 "clear ip prefix-list [WORD [A.B.C.D/M$prefix]]",
1549 "Name of a prefix list\n"
1550 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
1552 return vty_clear_prefix_list(vty
, AFI_IP
, prefix_list
, prefix_str
);
1555 DEFPY (ipv6_prefix_list
,
1556 ipv6_prefix_list_cmd
,
1557 "ipv6 prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|X:X::X:X/M$dest [{ge (0-128)|le (0-128)}]>",
1560 "Name of a prefix list\n"
1561 "sequence number of an entry\n"
1563 "Specify packets to reject\n"
1564 "Specify packets to forward\n"
1565 "Any prefix match. Same as \"::0/0 le 128\"\n"
1566 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1567 "Maximum prefix length to be matched\n"
1568 "Maximum prefix length\n"
1569 "Minimum prefix length to be matched\n"
1570 "Minimum prefix length\n")
1572 return vty_prefix_list_install(vty
, AFI_IP6
, prefix_list
, seq_str
,
1573 action
, dest
, ge_str
, le_str
);
1576 DEFPY (no_ipv6_prefix_list
,
1577 no_ipv6_prefix_list_cmd
,
1578 "no ipv6 prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|X:X::X:X/M$dest [{ge (0-128)|le (0-128)}]>",
1582 "Name of a prefix list\n"
1583 "sequence number of an entry\n"
1585 "Specify packets to reject\n"
1586 "Specify packets to forward\n"
1587 "Any prefix match. Same as \"::0/0 le 128\"\n"
1588 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1589 "Maximum prefix length to be matched\n"
1590 "Maximum prefix length\n"
1591 "Minimum prefix length to be matched\n"
1592 "Minimum prefix length\n")
1594 return vty_prefix_list_uninstall(vty
, AFI_IP6
, prefix_list
, seq_str
,
1595 action
, dest
, ge_str
, le_str
);
1598 DEFPY (no_ipv6_prefix_list_all
,
1599 no_ipv6_prefix_list_all_cmd
,
1600 "no ipv6 prefix-list WORD",
1604 "Name of a prefix list\n")
1606 return vty_prefix_list_uninstall(vty
, AFI_IP6
, prefix_list
, NULL
, NULL
,
1610 DEFPY (ipv6_prefix_list_sequence_number
,
1611 ipv6_prefix_list_sequence_number_cmd
,
1612 "[no] ipv6 prefix-list sequence-number",
1616 "Include/exclude sequence numbers in NVGEN\n")
1618 prefix_master_ipv6
.seqnum
= no
? false : true;
1622 DEFUN (ipv6_prefix_list_description
,
1623 ipv6_prefix_list_description_cmd
,
1624 "ipv6 prefix-list WORD description LINE...",
1627 "Name of a prefix list\n"
1628 "Prefix-list specific description\n"
1629 "Up to 80 characters describing this prefix-list\n")
1633 struct prefix_list
*plist
;
1635 plist
= prefix_list_get(AFI_IP6
, 0, argv
[idx_word
]->arg
);
1638 XFREE(MTYPE_TMP
, plist
->desc
);
1641 plist
->desc
= argv_concat(argv
, argc
, iddx_line
);
1646 DEFUN (no_ipv6_prefix_list_description
,
1647 no_ipv6_prefix_list_description_cmd
,
1648 "no ipv6 prefix-list WORD description",
1652 "Name of a prefix list\n"
1653 "Prefix-list specific description\n")
1656 return vty_prefix_list_desc_unset(vty
, AFI_IP6
, argv
[idx_word
]->arg
);
1660 DEFUN (no_ipv6_prefix_list_description_comment
,
1661 no_ipv6_prefix_list_description_comment_cmd
,
1662 "no ipv6 prefix-list WORD description LINE...",
1666 "Name of a prefix list\n"
1667 "Prefix-list specific description\n"
1668 "Up to 80 characters describing this prefix-list\n")
1670 return no_ipv6_prefix_list_description(self
, vty
, argc
, argv
);
1674 DEFPY (show_ipv6_prefix_list
,
1675 show_ipv6_prefix_list_cmd
,
1676 "show ipv6 prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1680 "Name of a prefix list\n"
1681 "sequence number of an entry\n"
1682 "Sequence number\n")
1684 enum display_type dtype
= normal_display
;
1686 dtype
= sequential_display
;
1688 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, arg_str
, dtype
);
1691 DEFPY (show_ipv6_prefix_list_prefix
,
1692 show_ipv6_prefix_list_prefix_cmd
,
1693 "show ipv6 prefix-list WORD X:X::X:X/M$prefix [longer$dl|first-match$dfm]",
1697 "Name of a prefix list\n"
1698 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1699 "Lookup longer prefix\n"
1700 "First matched prefix\n")
1702 enum display_type dtype
= normal_display
;
1704 dtype
= longer_display
;
1706 dtype
= first_match_display
;
1708 return vty_show_prefix_list_prefix(vty
, AFI_IP6
, prefix_list
,
1712 DEFPY (show_ipv6_prefix_list_summary
,
1713 show_ipv6_prefix_list_summary_cmd
,
1714 "show ipv6 prefix-list summary [WORD$prefix-list]",
1718 "Summary of prefix lists\n"
1719 "Name of a prefix list\n")
1721 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, NULL
,
1725 DEFPY (show_ipv6_prefix_list_detail
,
1726 show_ipv6_prefix_list_detail_cmd
,
1727 "show ipv6 prefix-list detail [WORD$prefix-list]",
1731 "Detail of prefix lists\n"
1732 "Name of a prefix list\n")
1734 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, NULL
,
1738 DEFPY (clear_ipv6_prefix_list
,
1739 clear_ipv6_prefix_list_cmd
,
1740 "clear ipv6 prefix-list [WORD [X:X::X:X/M$prefix]]",
1744 "Name of a prefix list\n"
1745 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
1747 return vty_clear_prefix_list(vty
, AFI_IP6
, prefix_list
, prefix_str
);
1750 /* Configuration write function. */
1751 static int config_write_prefix_afi(afi_t afi
, struct vty
*vty
)
1753 struct prefix_list
*plist
;
1754 struct prefix_list_entry
*pentry
;
1755 struct prefix_master
*master
;
1758 master
= prefix_master_get(afi
, 0);
1762 if (!master
->seqnum
) {
1763 vty_out(vty
, "no ip%s prefix-list sequence-number\n",
1764 afi
== AFI_IP
? "" : "v6");
1765 vty_out(vty
, "!\n");
1768 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
) {
1770 vty_out(vty
, "ip%s prefix-list %s description %s\n",
1771 afi
== AFI_IP
? "" : "v6", plist
->name
,
1776 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1777 vty_out(vty
, "ip%s prefix-list %s ",
1778 afi
== AFI_IP
? "" : "v6", plist
->name
);
1781 vty_out(vty
, "seq %" PRId64
" ", pentry
->seq
);
1783 vty_out(vty
, "%s ", prefix_list_type_str(pentry
));
1786 vty_out(vty
, "any");
1788 struct prefix
*p
= &pentry
->prefix
;
1791 vty_out(vty
, "%s/%d",
1792 inet_ntop(p
->family
, p
->u
.val
, buf
,
1797 vty_out(vty
, " ge %d", pentry
->ge
);
1799 vty_out(vty
, " le %d", pentry
->le
);
1804 /* vty_out (vty, "!\n"); */
1807 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
) {
1809 vty_out(vty
, "ip%s prefix-list %s description %s\n",
1810 afi
== AFI_IP
? "" : "v6", plist
->name
,
1815 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1816 vty_out(vty
, "ip%s prefix-list %s ",
1817 afi
== AFI_IP
? "" : "v6", plist
->name
);
1820 vty_out(vty
, "seq %" PRId64
" ", pentry
->seq
);
1822 vty_out(vty
, "%s", prefix_list_type_str(pentry
));
1825 vty_out(vty
, " any");
1827 struct prefix
*p
= &pentry
->prefix
;
1830 vty_out(vty
, " %s/%d",
1831 inet_ntop(p
->family
, p
->u
.val
, buf
,
1836 vty_out(vty
, " ge %d", pentry
->ge
);
1838 vty_out(vty
, " le %d", pentry
->le
);
1848 struct stream
*prefix_bgp_orf_entry(struct stream
*s
, struct prefix_list
*plist
,
1849 uint8_t init_flag
, uint8_t permit_flag
,
1852 struct prefix_list_entry
*pentry
;
1857 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1858 uint8_t flag
= init_flag
;
1859 struct prefix
*p
= &pentry
->prefix
;
1861 flag
|= (pentry
->type
== PREFIX_PERMIT
? permit_flag
1863 stream_putc(s
, flag
);
1864 stream_putl(s
, (uint32_t)pentry
->seq
);
1865 stream_putc(s
, (uint8_t)pentry
->ge
);
1866 stream_putc(s
, (uint8_t)pentry
->le
);
1867 stream_put_prefix(s
, p
);
1873 int prefix_bgp_orf_set(char *name
, afi_t afi
, struct orf_prefix
*orfp
,
1874 int permit
, int set
)
1876 struct prefix_list
*plist
;
1877 struct prefix_list_entry
*pentry
;
1879 /* ge and le value check */
1880 if (orfp
->ge
&& orfp
->ge
<= orfp
->p
.prefixlen
)
1881 return CMD_WARNING_CONFIG_FAILED
;
1882 if (orfp
->le
&& orfp
->le
<= orfp
->p
.prefixlen
)
1883 return CMD_WARNING_CONFIG_FAILED
;
1884 if (orfp
->le
&& orfp
->ge
> orfp
->le
)
1885 return CMD_WARNING_CONFIG_FAILED
;
1887 if (orfp
->ge
&& orfp
->le
== (afi
== AFI_IP
? 32 : 128))
1890 plist
= prefix_list_get(afi
, 1, name
);
1892 return CMD_WARNING_CONFIG_FAILED
;
1895 pentry
= prefix_list_entry_make(
1896 &orfp
->p
, (permit
? PREFIX_PERMIT
: PREFIX_DENY
),
1897 orfp
->seq
, orfp
->le
, orfp
->ge
, 0);
1899 if (prefix_entry_dup_check(plist
, pentry
)) {
1900 prefix_list_entry_free(pentry
);
1901 return CMD_WARNING_CONFIG_FAILED
;
1904 prefix_list_entry_add(plist
, pentry
);
1906 pentry
= prefix_list_entry_lookup(
1907 plist
, &orfp
->p
, (permit
? PREFIX_PERMIT
: PREFIX_DENY
),
1908 orfp
->seq
, orfp
->le
, orfp
->ge
);
1911 return CMD_WARNING_CONFIG_FAILED
;
1913 prefix_list_entry_delete(plist
, pentry
, 1);
1919 void prefix_bgp_orf_remove_all(afi_t afi
, char *name
)
1921 struct prefix_list
*plist
;
1923 plist
= prefix_bgp_orf_lookup(afi
, name
);
1925 prefix_list_delete(plist
);
1928 /* return prefix count */
1929 int prefix_bgp_show_prefix_list(struct vty
*vty
, afi_t afi
, char *name
,
1932 struct prefix_list
*plist
;
1933 struct prefix_list_entry
*pentry
;
1934 json_object
*json
= NULL
;
1935 json_object
*json_prefix
= NULL
;
1936 json_object
*json_list
= NULL
;
1938 plist
= prefix_bgp_orf_lookup(afi
, name
);
1943 return plist
->count
;
1946 json
= json_object_new_object();
1947 json_prefix
= json_object_new_object();
1948 json_list
= json_object_new_object();
1950 json_object_int_add(json_prefix
, "prefixListCounter",
1952 json_object_string_add(json_prefix
, "prefixListName",
1955 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1956 struct prefix
*p
= &pentry
->prefix
;
1960 sprintf(buf_a
, "%s/%d",
1961 inet_ntop(p
->family
, p
->u
.val
, buf_b
,
1965 json_object_int_add(json_list
, "seq", pentry
->seq
);
1966 json_object_string_add(json_list
, "seqPrefixListType",
1967 prefix_list_type_str(pentry
));
1970 json_object_int_add(json_list
, "ge",
1973 json_object_int_add(json_list
, "le",
1976 json_object_object_add(json_prefix
, buf_a
, json_list
);
1979 json_object_object_add(json
, "ipPrefixList",
1982 json_object_object_add(json
, "ipv6PrefixList",
1985 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
1986 json
, JSON_C_TO_STRING_PRETTY
));
1987 json_object_free(json
);
1989 vty_out(vty
, "ip%s prefix-list %s: %d entries\n",
1990 afi
== AFI_IP
? "" : "v6", plist
->name
, plist
->count
);
1992 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1993 struct prefix
*p
= &pentry
->prefix
;
1996 vty_out(vty
, " seq %" PRId64
" %s %s/%d",
1998 prefix_list_type_str(pentry
),
1999 inet_ntop(p
->family
, p
->u
.val
, buf
, BUFSIZ
),
2003 vty_out(vty
, " ge %d", pentry
->ge
);
2005 vty_out(vty
, " le %d", pentry
->le
);
2010 return plist
->count
;
2013 static void prefix_list_reset_afi(afi_t afi
, int orf
)
2015 struct prefix_list
*plist
;
2016 struct prefix_list
*next
;
2017 struct prefix_master
*master
;
2019 master
= prefix_master_get(afi
, orf
);
2023 for (plist
= master
->num
.head
; plist
; plist
= next
) {
2025 prefix_list_delete(plist
);
2027 for (plist
= master
->str
.head
; plist
; plist
= next
) {
2029 prefix_list_delete(plist
);
2032 assert(master
->num
.head
== NULL
);
2033 assert(master
->num
.tail
== NULL
);
2035 assert(master
->str
.head
== NULL
);
2036 assert(master
->str
.tail
== NULL
);
2039 master
->recent
= NULL
;
2043 /* Prefix-list node. */
2044 static struct cmd_node prefix_node
= {PREFIX_NODE
,
2045 "", /* Prefix list has no interface. */
2048 static int config_write_prefix_ipv4(struct vty
*vty
)
2050 return config_write_prefix_afi(AFI_IP
, vty
);
2053 static void plist_autocomplete_afi(afi_t afi
, vector comps
,
2054 struct cmd_token
*token
)
2056 struct prefix_list
*plist
;
2057 struct prefix_master
*master
;
2059 master
= prefix_master_get(afi
, 0);
2063 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
2064 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, plist
->name
));
2065 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
2066 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, plist
->name
));
2069 static void plist_autocomplete(vector comps
, struct cmd_token
*token
)
2071 plist_autocomplete_afi(AFI_IP
, comps
, token
);
2072 plist_autocomplete_afi(AFI_IP6
, comps
, token
);
2075 static const struct cmd_variable_handler plist_var_handlers
[] = {
2076 {/* "prefix-list WORD" */
2077 .varname
= "prefix_list",
2078 .completions
= plist_autocomplete
},
2079 {.completions
= NULL
}};
2082 static void prefix_list_init_ipv4(void)
2084 install_node(&prefix_node
, config_write_prefix_ipv4
);
2086 install_element(CONFIG_NODE
, &ip_prefix_list_cmd
);
2087 install_element(CONFIG_NODE
, &no_ip_prefix_list_cmd
);
2088 install_element(CONFIG_NODE
, &no_ip_prefix_list_seq_cmd
);
2089 install_element(CONFIG_NODE
, &no_ip_prefix_list_all_cmd
);
2091 install_element(CONFIG_NODE
, &ip_prefix_list_description_cmd
);
2092 install_element(CONFIG_NODE
, &no_ip_prefix_list_description_cmd
);
2093 install_element(CONFIG_NODE
,
2094 &no_ip_prefix_list_description_comment_cmd
);
2096 install_element(CONFIG_NODE
, &ip_prefix_list_sequence_number_cmd
);
2098 install_element(VIEW_NODE
, &show_ip_prefix_list_cmd
);
2099 install_element(VIEW_NODE
, &show_ip_prefix_list_prefix_cmd
);
2100 install_element(VIEW_NODE
, &show_ip_prefix_list_summary_cmd
);
2101 install_element(VIEW_NODE
, &show_ip_prefix_list_detail_cmd
);
2103 install_element(ENABLE_NODE
, &clear_ip_prefix_list_cmd
);
2106 /* Prefix-list node. */
2107 static struct cmd_node prefix_ipv6_node
= {
2108 PREFIX_IPV6_NODE
, "", /* Prefix list has no interface. */
2111 static int config_write_prefix_ipv6(struct vty
*vty
)
2113 return config_write_prefix_afi(AFI_IP6
, vty
);
2116 static void prefix_list_init_ipv6(void)
2118 install_node(&prefix_ipv6_node
, config_write_prefix_ipv6
);
2120 install_element(CONFIG_NODE
, &ipv6_prefix_list_cmd
);
2121 install_element(CONFIG_NODE
, &no_ipv6_prefix_list_cmd
);
2122 install_element(CONFIG_NODE
, &no_ipv6_prefix_list_all_cmd
);
2124 install_element(CONFIG_NODE
, &ipv6_prefix_list_description_cmd
);
2125 install_element(CONFIG_NODE
, &no_ipv6_prefix_list_description_cmd
);
2126 install_element(CONFIG_NODE
,
2127 &no_ipv6_prefix_list_description_comment_cmd
);
2129 install_element(CONFIG_NODE
, &ipv6_prefix_list_sequence_number_cmd
);
2131 install_element(VIEW_NODE
, &show_ipv6_prefix_list_cmd
);
2132 install_element(VIEW_NODE
, &show_ipv6_prefix_list_prefix_cmd
);
2133 install_element(VIEW_NODE
, &show_ipv6_prefix_list_summary_cmd
);
2134 install_element(VIEW_NODE
, &show_ipv6_prefix_list_detail_cmd
);
2136 install_element(ENABLE_NODE
, &clear_ipv6_prefix_list_cmd
);
2139 void prefix_list_init(void)
2141 cmd_variable_handler_register(plist_var_handlers
);
2143 prefix_list_init_ipv4();
2144 prefix_list_init_ipv6();
2147 void prefix_list_reset(void)
2149 prefix_list_reset_afi(AFI_IP
, 0);
2150 prefix_list_reset_afi(AFI_IP6
, 0);
2151 prefix_list_reset_afi(AFI_IP
, 1);
2152 prefix_list_reset_afi(AFI_IP6
, 1);