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
;
330 XFREE(MTYPE_TMP
, plist
->desc
);
332 /* Make sure master's recent changed prefix-list information is
334 master
->recent
= NULL
;
336 route_map_notify_dependencies(plist
->name
, RMAP_EVENT_PLIST_DELETED
);
338 if (master
->delete_hook
)
339 (*master
->delete_hook
)(plist
);
342 XFREE(MTYPE_MPREFIX_LIST_STR
, plist
->name
);
344 XFREE(MTYPE_PREFIX_LIST_TRIE
, plist
->trie
);
346 prefix_list_free(plist
);
349 static struct prefix_list_entry
*
350 prefix_list_entry_make(struct prefix
*prefix
, enum prefix_list_type type
,
351 int64_t seq
, int le
, int ge
, int any
)
353 struct prefix_list_entry
*pentry
;
355 pentry
= prefix_list_entry_new();
360 prefix_copy(&pentry
->prefix
, prefix
);
369 /* Add hook function. */
370 void prefix_list_add_hook(void (*func
)(struct prefix_list
*plist
))
372 prefix_master_ipv4
.add_hook
= func
;
373 prefix_master_ipv6
.add_hook
= func
;
376 /* Delete hook function. */
377 void prefix_list_delete_hook(void (*func
)(struct prefix_list
*plist
))
379 prefix_master_ipv4
.delete_hook
= func
;
380 prefix_master_ipv6
.delete_hook
= func
;
383 /* Calculate new sequential number. */
384 static int64_t prefix_new_seq_get(struct prefix_list
*plist
)
388 struct prefix_list_entry
*pentry
;
392 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
393 if (maxseq
< pentry
->seq
)
394 maxseq
= pentry
->seq
;
397 newseq
= ((maxseq
/ 5) * 5) + 5;
399 return (newseq
> UINT_MAX
) ? UINT_MAX
: newseq
;
402 /* Return prefix list entry which has same seq number. */
403 static struct prefix_list_entry
*prefix_seq_check(struct prefix_list
*plist
,
406 struct prefix_list_entry
*pentry
;
408 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
)
409 if (pentry
->seq
== seq
)
414 static struct prefix_list_entry
*
415 prefix_list_entry_lookup(struct prefix_list
*plist
, struct prefix
*prefix
,
416 enum prefix_list_type type
, int64_t seq
,
419 struct prefix_list_entry
*pentry
;
421 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
)
422 if (prefix_same(&pentry
->prefix
, prefix
)
423 && pentry
->type
== type
) {
424 if (seq
>= 0 && pentry
->seq
!= seq
)
427 if (pentry
->le
!= le
)
429 if (pentry
->ge
!= ge
)
438 static void trie_walk_affected(size_t validbits
, struct pltrie_table
*table
,
439 uint8_t byte
, struct prefix_list_entry
*object
,
440 void (*fn
)(struct prefix_list_entry
*object
,
441 struct prefix_list_entry
**updptr
))
446 if (validbits
> PLC_BITS
) {
447 fn(object
, &table
->entries
[byte
].final_chain
);
451 mask
= (1 << (8 - validbits
)) - 1;
452 for (bwalk
= byte
& ~mask
; bwalk
<= byte
+ mask
; bwalk
++) {
453 fn(object
, &table
->entries
[bwalk
].up_chain
);
457 static void trie_uninstall_fn(struct prefix_list_entry
*object
,
458 struct prefix_list_entry
**updptr
)
460 for (; *updptr
; updptr
= &(*updptr
)->next_best
)
461 if (*updptr
== object
) {
462 *updptr
= object
->next_best
;
467 static int trie_table_empty(struct pltrie_table
*table
)
470 for (i
= 0; i
< PLC_LEN
; i
++)
471 if (table
->entries
[i
].next_table
|| table
->entries
[i
].up_chain
)
476 static void prefix_list_trie_del(struct prefix_list
*plist
,
477 struct prefix_list_entry
*pentry
)
479 size_t depth
, maxdepth
= plist
->master
->trie_depth
;
480 uint8_t *bytes
= pentry
->prefix
.u
.val
;
481 size_t validbits
= pentry
->prefix
.prefixlen
;
482 struct pltrie_table
*table
, **tables
[PLC_MAXLEVEL
];
485 for (depth
= 0; validbits
> PLC_BITS
&& depth
< maxdepth
- 1; depth
++) {
486 uint8_t byte
= bytes
[depth
];
487 assert(table
->entries
[byte
].next_table
);
489 tables
[depth
+ 1] = &table
->entries
[byte
].next_table
;
490 table
= table
->entries
[byte
].next_table
;
492 validbits
-= PLC_BITS
;
495 trie_walk_affected(validbits
, table
, bytes
[depth
], pentry
,
498 for (; depth
> 0; depth
--)
499 if (trie_table_empty(*tables
[depth
])) {
500 XFREE(MTYPE_PREFIX_LIST_TRIE
, *tables
[depth
]);
501 *tables
[depth
] = NULL
;
506 static void prefix_list_entry_delete(struct prefix_list
*plist
,
507 struct prefix_list_entry
*pentry
,
510 if (plist
== NULL
|| pentry
== NULL
)
513 prefix_list_trie_del(plist
, pentry
);
516 pentry
->prev
->next
= pentry
->next
;
518 plist
->head
= pentry
->next
;
520 pentry
->next
->prev
= pentry
->prev
;
522 plist
->tail
= pentry
->prev
;
524 prefix_list_entry_free(pentry
);
529 route_map_notify_dependencies(plist
->name
,
530 RMAP_EVENT_PLIST_DELETED
);
531 if (plist
->master
->delete_hook
)
532 (*plist
->master
->delete_hook
)(plist
);
534 if (plist
->head
== NULL
&& plist
->tail
== NULL
535 && plist
->desc
== NULL
)
536 prefix_list_delete(plist
);
538 plist
->master
->recent
= plist
;
542 static void trie_install_fn(struct prefix_list_entry
*object
,
543 struct prefix_list_entry
**updptr
)
546 if (*updptr
== object
)
548 if ((*updptr
)->prefix
.prefixlen
< object
->prefix
.prefixlen
)
550 if ((*updptr
)->prefix
.prefixlen
== object
->prefix
.prefixlen
551 && (*updptr
)->seq
> object
->seq
)
553 updptr
= &(*updptr
)->next_best
;
556 if (!object
->next_best
)
557 object
->next_best
= *updptr
;
559 assert(object
->next_best
== *updptr
|| !*updptr
);
564 static void prefix_list_trie_add(struct prefix_list
*plist
,
565 struct prefix_list_entry
*pentry
)
567 size_t depth
= plist
->master
->trie_depth
;
568 uint8_t *bytes
= pentry
->prefix
.u
.val
;
569 size_t validbits
= pentry
->prefix
.prefixlen
;
570 struct pltrie_table
*table
;
573 while (validbits
> PLC_BITS
&& depth
> 1) {
574 if (!table
->entries
[*bytes
].next_table
)
575 table
->entries
[*bytes
].next_table
=
576 XCALLOC(MTYPE_PREFIX_LIST_TRIE
,
577 sizeof(struct pltrie_table
));
578 table
= table
->entries
[*bytes
].next_table
;
581 validbits
-= PLC_BITS
;
584 trie_walk_affected(validbits
, table
, *bytes
, pentry
, trie_install_fn
);
587 static void prefix_list_entry_add(struct prefix_list
*plist
,
588 struct prefix_list_entry
*pentry
)
590 struct prefix_list_entry
*replace
;
591 struct prefix_list_entry
*point
;
593 /* Automatic asignment of seq no. */
594 if (pentry
->seq
== -1)
595 pentry
->seq
= prefix_new_seq_get(plist
);
597 if (plist
->tail
&& pentry
->seq
> plist
->tail
->seq
)
600 /* Is there any same seq prefix list entry? */
601 replace
= prefix_seq_check(plist
, pentry
->seq
);
603 prefix_list_entry_delete(plist
, replace
, 0);
605 /* Check insert point. */
606 for (point
= plist
->head
; point
; point
= point
->next
)
607 if (point
->seq
>= pentry
->seq
)
611 /* In case of this is the first element of the list. */
612 pentry
->next
= point
;
616 point
->prev
->next
= pentry
;
618 plist
->head
= pentry
;
620 pentry
->prev
= point
->prev
;
621 point
->prev
= pentry
;
624 plist
->tail
->next
= pentry
;
626 plist
->head
= pentry
;
628 pentry
->prev
= plist
->tail
;
629 plist
->tail
= pentry
;
632 prefix_list_trie_add(plist
, pentry
);
634 /* Increment count. */
637 /* Run hook function. */
638 if (plist
->master
->add_hook
)
639 (*plist
->master
->add_hook
)(plist
);
641 route_map_notify_dependencies(plist
->name
, RMAP_EVENT_PLIST_ADDED
);
642 plist
->master
->recent
= plist
;
645 /* Return string of prefix_list_type. */
646 static const char *prefix_list_type_str(struct prefix_list_entry
*pentry
)
648 switch (pentry
->type
) {
658 static int prefix_list_entry_match(struct prefix_list_entry
*pentry
,
659 const struct prefix
*p
)
663 if (pentry
->prefix
.family
!= p
->family
)
666 ret
= prefix_match(&pentry
->prefix
, p
);
670 /* In case of le nor ge is specified, exact match is performed. */
671 if (!pentry
->le
&& !pentry
->ge
) {
672 if (pentry
->prefix
.prefixlen
!= p
->prefixlen
)
676 if (p
->prefixlen
> pentry
->le
)
680 if (p
->prefixlen
< pentry
->ge
)
686 enum prefix_list_type
prefix_list_apply_which_prefix(
687 struct prefix_list
*plist
,
688 const struct prefix
**which
,
691 struct prefix_list_entry
*pentry
, *pbest
= NULL
;
693 const struct prefix
*p
= (const struct prefix
*)object
;
694 const uint8_t *byte
= p
->u
.val
;
696 size_t validbits
= p
->prefixlen
;
697 struct pltrie_table
*table
;
705 if (plist
->count
== 0) {
708 return PREFIX_PERMIT
;
711 depth
= plist
->master
->trie_depth
;
714 for (pentry
= table
->entries
[*byte
].up_chain
; pentry
;
715 pentry
= pentry
->next_best
) {
716 if (pbest
&& pbest
->seq
< pentry
->seq
)
718 if (prefix_list_entry_match(pentry
, p
))
722 if (validbits
<= PLC_BITS
)
724 validbits
-= PLC_BITS
;
727 if (!table
->entries
[*byte
].next_table
)
730 table
= table
->entries
[*byte
].next_table
;
735 for (pentry
= table
->entries
[*byte
].final_chain
; pentry
;
736 pentry
= pentry
->next_best
) {
737 if (pbest
&& pbest
->seq
< pentry
->seq
)
739 if (prefix_list_entry_match(pentry
, p
))
747 *which
= &pbest
->prefix
;
758 static void __attribute__((unused
)) prefix_list_print(struct prefix_list
*plist
)
760 struct prefix_list_entry
*pentry
;
765 printf("ip prefix-list %s: %d entries\n", plist
->name
, plist
->count
);
767 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
769 printf("any %s\n", prefix_list_type_str(pentry
));
776 printf(" seq %" PRId64
" %s %s/%d", pentry
->seq
,
777 prefix_list_type_str(pentry
),
778 inet_ntop(p
->family
, p
->u
.val
, buf
, BUFSIZ
),
781 printf(" ge %d", pentry
->ge
);
783 printf(" le %d", pentry
->le
);
789 /* Retrun 1 when plist already include pentry policy. */
790 static struct prefix_list_entry
*
791 prefix_entry_dup_check(struct prefix_list
*plist
, struct prefix_list_entry
*new)
793 size_t depth
, maxdepth
= plist
->master
->trie_depth
;
794 uint8_t byte
, *bytes
= new->prefix
.u
.val
;
795 size_t validbits
= new->prefix
.prefixlen
;
796 struct pltrie_table
*table
;
797 struct prefix_list_entry
*pentry
;
801 seq
= prefix_new_seq_get(plist
);
806 for (depth
= 0; validbits
> PLC_BITS
&& depth
< maxdepth
- 1; depth
++) {
808 if (!table
->entries
[byte
].next_table
)
811 table
= table
->entries
[byte
].next_table
;
812 validbits
-= PLC_BITS
;
816 if (validbits
> PLC_BITS
)
817 pentry
= table
->entries
[byte
].final_chain
;
819 pentry
= table
->entries
[byte
].up_chain
;
821 for (; pentry
; pentry
= pentry
->next_best
) {
822 if (prefix_same(&pentry
->prefix
, &new->prefix
)
823 && pentry
->type
== new->type
&& pentry
->le
== new->le
824 && pentry
->ge
== new->ge
&& pentry
->seq
!= seq
)
830 static int vty_invalid_prefix_range(struct vty
*vty
, const char *prefix
)
833 "%% Invalid prefix range for %s, make sure: len < ge-value <= le-value\n",
835 return CMD_WARNING_CONFIG_FAILED
;
838 static int vty_prefix_list_install(struct vty
*vty
, afi_t afi
, const char *name
,
839 const char *seq
, const char *typestr
,
840 const char *prefix
, const char *ge
,
844 enum prefix_list_type type
;
845 struct prefix_list
*plist
;
846 struct prefix_list_entry
*pentry
;
847 struct prefix_list_entry
*dup
;
848 struct prefix p
, p_tmp
;
854 if (name
== NULL
|| prefix
== NULL
|| typestr
== NULL
) {
855 vty_out(vty
, "%% Missing prefix or type\n");
856 return CMD_WARNING_CONFIG_FAILED
;
859 /* Sequential number. */
861 seqnum
= (int64_t)atol(seq
);
863 /* ge and le number */
869 /* Check filter type. */
870 if (strncmp("permit", typestr
, 1) == 0)
871 type
= PREFIX_PERMIT
;
872 else if (strncmp("deny", typestr
, 1) == 0)
875 vty_out(vty
, "%% prefix type must be permit or deny\n");
876 return CMD_WARNING_CONFIG_FAILED
;
879 /* "any" is special token for matching any IPv4 addresses. */
882 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
883 ret
= str2prefix_ipv4("0.0.0.0/0",
884 (struct prefix_ipv4
*)&p
);
886 lenum
= IPV4_MAX_BITLEN
;
889 ret
= str2prefix_ipv4(prefix
, (struct prefix_ipv4
*)&p
);
892 vty_out(vty
, "%% Malformed IPv4 prefix\n");
893 return CMD_WARNING_CONFIG_FAILED
;
896 /* make a copy to verify prefix matches mask length */
897 prefix_copy(&p_tmp
, &p
);
898 apply_mask_ipv4((struct prefix_ipv4
*)&p_tmp
);
902 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
903 ret
= str2prefix_ipv6("::/0", (struct prefix_ipv6
*)&p
);
905 lenum
= IPV6_MAX_BITLEN
;
908 ret
= str2prefix_ipv6(prefix
, (struct prefix_ipv6
*)&p
);
911 vty_out(vty
, "%% Malformed IPv6 prefix\n");
912 return CMD_WARNING_CONFIG_FAILED
;
915 /* make a copy to verify prefix matches mask length */
916 prefix_copy(&p_tmp
, &p
);
917 apply_mask_ipv6((struct prefix_ipv6
*)&p_tmp
);
922 vty_out(vty
, "%% Unrecognized AFI (%d)\n", afi
);
923 return CMD_WARNING_CONFIG_FAILED
;
927 /* If prefix has bits not under the mask, adjust it to fit */
928 if (!prefix_same(&p_tmp
, &p
)) {
929 char buf
[PREFIX2STR_BUFFER
];
930 char buf_tmp
[PREFIX2STR_BUFFER
];
931 prefix2str(&p
, buf
, sizeof(buf
));
932 prefix2str(&p_tmp
, buf_tmp
, sizeof(buf_tmp
));
934 "%% Prefix-list %s prefix changed from %s to %s to match length\n",
937 "Prefix-list %s prefix changed from %s to %s to match length",
942 /* ge and le check. */
943 if (genum
&& (genum
<= p
.prefixlen
))
944 return vty_invalid_prefix_range(vty
, prefix
);
946 if (lenum
&& (lenum
< p
.prefixlen
))
947 return vty_invalid_prefix_range(vty
, prefix
);
949 if (lenum
&& (genum
> lenum
))
950 return vty_invalid_prefix_range(vty
, prefix
);
952 if (genum
&& (lenum
== (afi
== AFI_IP
? 32 : 128)))
955 /* Get prefix_list with name. */
956 plist
= prefix_list_get(afi
, 0, name
);
958 /* Make prefix entry. */
959 pentry
= prefix_list_entry_make(&p
, type
, seqnum
, lenum
, genum
, any
);
961 /* Check same policy. */
962 dup
= prefix_entry_dup_check(plist
, pentry
);
965 prefix_list_entry_free(pentry
);
969 /* Install new filter to the access_list. */
970 prefix_list_entry_add(plist
, pentry
);
975 static int vty_prefix_list_uninstall(struct vty
*vty
, afi_t afi
,
976 const char *name
, const char *seq
,
977 const char *typestr
, const char *prefix
,
978 const char *ge
, const char *le
)
981 enum prefix_list_type type
;
982 struct prefix_list
*plist
;
983 struct prefix_list_entry
*pentry
;
989 /* Check prefix list name. */
990 plist
= prefix_list_lookup(afi
, name
);
992 vty_out(vty
, "%% Can't find specified prefix-list\n");
993 return CMD_WARNING_CONFIG_FAILED
;
996 /* Only prefix-list name specified, delete the entire prefix-list. */
997 if (seq
== NULL
&& typestr
== NULL
&& prefix
== NULL
&& ge
== NULL
999 prefix_list_delete(plist
);
1003 /* We must have, at a minimum, both the type and prefix here */
1004 if ((typestr
== NULL
) || (prefix
== NULL
)) {
1005 vty_out(vty
, "%% Both prefix and type required\n");
1006 return CMD_WARNING_CONFIG_FAILED
;
1009 /* Check sequence number. */
1011 seqnum
= (int64_t)atol(seq
);
1013 /* ge and le number */
1019 /* Check of filter type. */
1020 if (strncmp("permit", typestr
, 1) == 0)
1021 type
= PREFIX_PERMIT
;
1022 else if (strncmp("deny", typestr
, 1) == 0)
1025 vty_out(vty
, "%% prefix type must be permit or deny\n");
1026 return CMD_WARNING_CONFIG_FAILED
;
1029 /* "any" is special token for matching any IPv4 addresses. */
1030 if (afi
== AFI_IP
) {
1031 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
1032 ret
= str2prefix_ipv4("0.0.0.0/0",
1033 (struct prefix_ipv4
*)&p
);
1035 lenum
= IPV4_MAX_BITLEN
;
1037 ret
= str2prefix_ipv4(prefix
, (struct prefix_ipv4
*)&p
);
1040 vty_out(vty
, "%% Malformed IPv4 prefix\n");
1041 return CMD_WARNING_CONFIG_FAILED
;
1043 } else if (afi
== AFI_IP6
) {
1044 if (strncmp("any", prefix
, strlen(prefix
)) == 0) {
1045 ret
= str2prefix_ipv6("::/0", (struct prefix_ipv6
*)&p
);
1047 lenum
= IPV6_MAX_BITLEN
;
1049 ret
= str2prefix_ipv6(prefix
, (struct prefix_ipv6
*)&p
);
1052 vty_out(vty
, "%% Malformed IPv6 prefix\n");
1053 return CMD_WARNING_CONFIG_FAILED
;
1057 /* Lookup prefix entry. */
1059 prefix_list_entry_lookup(plist
, &p
, type
, seqnum
, lenum
, genum
);
1061 if (pentry
== NULL
) {
1062 vty_out(vty
, "%% Can't find specified prefix-list\n");
1063 return CMD_WARNING_CONFIG_FAILED
;
1066 /* Install new filter to the access_list. */
1067 prefix_list_entry_delete(plist
, pentry
, 1);
1072 static int vty_prefix_list_desc_unset(struct vty
*vty
, afi_t afi
,
1075 struct prefix_list
*plist
;
1077 plist
= prefix_list_lookup(afi
, name
);
1079 vty_out(vty
, "%% Can't find specified prefix-list\n");
1080 return CMD_WARNING_CONFIG_FAILED
;
1084 XFREE(MTYPE_TMP
, plist
->desc
);
1088 if (plist
->head
== NULL
&& plist
->tail
== NULL
&& plist
->desc
== NULL
)
1089 prefix_list_delete(plist
);
1103 static void vty_show_prefix_entry(struct vty
*vty
, afi_t afi
,
1104 struct prefix_list
*plist
,
1105 struct prefix_master
*master
,
1106 enum display_type dtype
, int seqnum
)
1108 struct prefix_list_entry
*pentry
;
1110 /* Print the name of the protocol */
1111 vty_out(vty
, "%s: ", frr_protoname
);
1113 if (dtype
== normal_display
) {
1114 vty_out(vty
, "ip%s prefix-list %s: %d entries\n",
1115 afi
== AFI_IP
? "" : "v6", plist
->name
, plist
->count
);
1117 vty_out(vty
, " Description: %s\n", plist
->desc
);
1118 } else if (dtype
== summary_display
|| dtype
== detail_display
) {
1119 vty_out(vty
, "ip%s prefix-list %s:\n",
1120 afi
== AFI_IP
? "" : "v6", plist
->name
);
1123 vty_out(vty
, " Description: %s\n", plist
->desc
);
1126 " count: %d, range entries: %d, sequences: %" PRId64
" - %" PRId64
"\n",
1127 plist
->count
, plist
->rangecount
,
1128 plist
->head
? plist
->head
->seq
: 0,
1129 plist
->tail
? plist
->tail
->seq
: 0);
1132 if (dtype
!= summary_display
) {
1133 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1134 if (dtype
== sequential_display
1135 && pentry
->seq
!= seqnum
)
1141 vty_out(vty
, "seq %" PRId64
" ", pentry
->seq
);
1143 vty_out(vty
, "%s ", prefix_list_type_str(pentry
));
1146 vty_out(vty
, "any");
1148 struct prefix
*p
= &pentry
->prefix
;
1151 vty_out(vty
, "%s/%d",
1152 inet_ntop(p
->family
, p
->u
.val
, buf
,
1157 vty_out(vty
, " ge %d", pentry
->ge
);
1159 vty_out(vty
, " le %d", pentry
->le
);
1162 if (dtype
== detail_display
1163 || dtype
== sequential_display
)
1164 vty_out(vty
, " (hit count: %ld, refcount: %ld)",
1165 pentry
->hitcnt
, pentry
->refcnt
);
1172 static int vty_show_prefix_list(struct vty
*vty
, afi_t afi
, const char *name
,
1173 const char *seq
, enum display_type dtype
)
1175 struct prefix_list
*plist
;
1176 struct prefix_master
*master
;
1179 master
= prefix_master_get(afi
, 0);
1184 seqnum
= (int64_t)atol(seq
);
1187 plist
= prefix_list_lookup(afi
, name
);
1189 vty_out(vty
, "%% Can't find specified prefix-list\n");
1192 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
, seqnum
);
1194 if (dtype
== detail_display
|| dtype
== summary_display
) {
1197 "Prefix-list with the last deletion/insertion: %s\n",
1198 master
->recent
->name
);
1201 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
1202 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
,
1205 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
1206 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
,
1213 static int vty_show_prefix_list_prefix(struct vty
*vty
, afi_t afi
,
1214 const char *name
, const char *prefix
,
1215 enum display_type type
)
1217 struct prefix_list
*plist
;
1218 struct prefix_list_entry
*pentry
;
1223 plist
= prefix_list_lookup(afi
, name
);
1225 vty_out(vty
, "%% Can't find specified prefix-list\n");
1229 ret
= str2prefix(prefix
, &p
);
1231 vty_out(vty
, "%% prefix is malformed\n");
1235 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1238 if (type
== normal_display
|| type
== first_match_display
)
1239 if (prefix_same(&p
, &pentry
->prefix
))
1242 if (type
== longer_display
) {
1243 if ((p
.family
== pentry
->prefix
.family
)
1244 && (prefix_match(&p
, &pentry
->prefix
)))
1249 vty_out(vty
, " seq %" PRId64
" %s ", pentry
->seq
,
1250 prefix_list_type_str(pentry
));
1253 vty_out(vty
, "any");
1255 struct prefix
*pf
= &pentry
->prefix
;
1258 vty_out(vty
, "%s/%d",
1259 inet_ntop(pf
->family
, pf
->u
.val
, buf
,
1264 vty_out(vty
, " ge %d", pentry
->ge
);
1266 vty_out(vty
, " le %d", pentry
->le
);
1269 if (type
== normal_display
1270 || type
== first_match_display
)
1271 vty_out(vty
, " (hit count: %ld, refcount: %ld)",
1272 pentry
->hitcnt
, pentry
->refcnt
);
1276 if (type
== first_match_display
)
1283 static int vty_clear_prefix_list(struct vty
*vty
, afi_t afi
, const char *name
,
1286 struct prefix_master
*master
;
1287 struct prefix_list
*plist
;
1288 struct prefix_list_entry
*pentry
;
1292 master
= prefix_master_get(afi
, 0);
1296 if (name
== NULL
&& prefix
== NULL
) {
1297 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
1298 for (pentry
= plist
->head
; pentry
;
1299 pentry
= pentry
->next
)
1302 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
1303 for (pentry
= plist
->head
; pentry
;
1304 pentry
= pentry
->next
)
1307 plist
= prefix_list_lookup(afi
, name
);
1309 vty_out(vty
, "%% Can't find specified prefix-list\n");
1314 ret
= str2prefix(prefix
, &p
);
1316 vty_out(vty
, "%% prefix is malformed\n");
1321 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1323 if (pentry
->prefix
.family
== p
.family
1324 && prefix_match(&pentry
->prefix
, &p
))
1333 #ifndef VTYSH_EXTRACT_PL
1334 #include "lib/plist_clippy.c"
1337 DEFPY (ip_prefix_list
,
1339 "ip prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|A.B.C.D/M$dest [{ge (0-32)|le (0-32)}]>",
1342 "Name of a prefix list\n"
1343 "sequence number of an entry\n"
1345 "Specify packets to reject\n"
1346 "Specify packets to forward\n"
1347 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1348 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1349 "Minimum prefix length to be matched\n"
1350 "Minimum prefix length\n"
1351 "Maximum prefix length to be matched\n"
1352 "Maximum prefix length\n")
1354 return vty_prefix_list_install(vty
, AFI_IP
, prefix_list
, seq_str
,
1355 action
, dest
, ge_str
, le_str
);
1358 DEFPY (no_ip_prefix_list
,
1359 no_ip_prefix_list_cmd
,
1360 "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)}]>",
1364 "Name of a prefix list\n"
1365 "sequence number of an entry\n"
1367 "Specify packets to reject\n"
1368 "Specify packets to forward\n"
1369 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1370 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1371 "Minimum prefix length to be matched\n"
1372 "Minimum prefix length\n"
1373 "Maximum prefix length to be matched\n"
1374 "Maximum prefix length\n")
1376 return vty_prefix_list_uninstall(vty
, AFI_IP
, prefix_list
, seq_str
,
1377 action
, dest
, ge_str
, le_str
);
1380 DEFPY (no_ip_prefix_list_all
,
1381 no_ip_prefix_list_all_cmd
,
1382 "no ip prefix-list WORD",
1386 "Name of a prefix list\n")
1388 return vty_prefix_list_uninstall(vty
, AFI_IP
, prefix_list
, NULL
, NULL
,
1392 DEFPY (ip_prefix_list_sequence_number
,
1393 ip_prefix_list_sequence_number_cmd
,
1394 "[no] ip prefix-list sequence-number",
1398 "Include/exclude sequence numbers in NVGEN\n")
1400 prefix_master_ipv4
.seqnum
= no
? false : true;
1404 DEFUN (ip_prefix_list_description
,
1405 ip_prefix_list_description_cmd
,
1406 "ip prefix-list WORD description LINE...",
1409 "Name of a prefix list\n"
1410 "Prefix-list specific description\n"
1411 "Up to 80 characters describing this prefix-list\n")
1415 struct prefix_list
*plist
;
1417 plist
= prefix_list_get(AFI_IP
, 0, argv
[idx_word
]->arg
);
1420 XFREE(MTYPE_TMP
, plist
->desc
);
1423 plist
->desc
= argv_concat(argv
, argc
, idx_line
);
1428 DEFUN (no_ip_prefix_list_description
,
1429 no_ip_prefix_list_description_cmd
,
1430 "no ip prefix-list WORD description",
1434 "Name of a prefix list\n"
1435 "Prefix-list specific description\n")
1438 return vty_prefix_list_desc_unset(vty
, AFI_IP
, argv
[idx_word
]->arg
);
1442 DEFUN (no_ip_prefix_list_description_comment
,
1443 no_ip_prefix_list_description_comment_cmd
,
1444 "no ip prefix-list WORD description LINE...",
1448 "Name of a prefix list\n"
1449 "Prefix-list specific description\n"
1450 "Up to 80 characters describing this prefix-list\n")
1452 return no_ip_prefix_list_description(self
, vty
, argc
, argv
);
1455 DEFPY (show_ip_prefix_list
,
1456 show_ip_prefix_list_cmd
,
1457 "show ip prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1461 "Name of a prefix list\n"
1462 "sequence number of an entry\n"
1463 "Sequence number\n")
1465 enum display_type dtype
= normal_display
;
1467 dtype
= sequential_display
;
1469 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, arg_str
, dtype
);
1472 DEFPY (show_ip_prefix_list_prefix
,
1473 show_ip_prefix_list_prefix_cmd
,
1474 "show ip prefix-list WORD A.B.C.D/M$prefix [longer$dl|first-match$dfm]",
1478 "Name of a prefix list\n"
1479 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1480 "Lookup longer prefix\n"
1481 "First matched prefix\n")
1483 enum display_type dtype
= normal_display
;
1485 dtype
= longer_display
;
1487 dtype
= first_match_display
;
1489 return vty_show_prefix_list_prefix(vty
, AFI_IP
, prefix_list
, prefix_str
,
1493 DEFPY (show_ip_prefix_list_summary
,
1494 show_ip_prefix_list_summary_cmd
,
1495 "show ip prefix-list summary [WORD$prefix_list]",
1499 "Summary of prefix lists\n"
1500 "Name of a prefix list\n")
1502 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, NULL
,
1506 DEFPY (show_ip_prefix_list_detail
,
1507 show_ip_prefix_list_detail_cmd
,
1508 "show ip prefix-list detail [WORD$prefix_list]",
1512 "Detail of prefix lists\n"
1513 "Name of a prefix list\n")
1515 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, NULL
,
1519 DEFPY (clear_ip_prefix_list
,
1520 clear_ip_prefix_list_cmd
,
1521 "clear ip prefix-list [WORD [A.B.C.D/M$prefix]]",
1525 "Name of a prefix list\n"
1526 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
1528 return vty_clear_prefix_list(vty
, AFI_IP
, prefix_list
, prefix_str
);
1531 DEFPY (ipv6_prefix_list
,
1532 ipv6_prefix_list_cmd
,
1533 "ipv6 prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|X:X::X:X/M$dest [{ge (0-128)|le (0-128)}]>",
1536 "Name of a prefix list\n"
1537 "sequence number of an entry\n"
1539 "Specify packets to reject\n"
1540 "Specify packets to forward\n"
1541 "Any prefix match. Same as \"::0/0 le 128\"\n"
1542 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1543 "Maximum prefix length to be matched\n"
1544 "Maximum prefix length\n"
1545 "Minimum prefix length to be matched\n"
1546 "Minimum prefix length\n")
1548 return vty_prefix_list_install(vty
, AFI_IP6
, prefix_list
, seq_str
,
1549 action
, dest
, ge_str
, le_str
);
1552 DEFPY (no_ipv6_prefix_list
,
1553 no_ipv6_prefix_list_cmd
,
1554 "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)}]>",
1558 "Name of a prefix list\n"
1559 "sequence number of an entry\n"
1561 "Specify packets to reject\n"
1562 "Specify packets to forward\n"
1563 "Any prefix match. Same as \"::0/0 le 128\"\n"
1564 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1565 "Maximum prefix length to be matched\n"
1566 "Maximum prefix length\n"
1567 "Minimum prefix length to be matched\n"
1568 "Minimum prefix length\n")
1570 return vty_prefix_list_uninstall(vty
, AFI_IP6
, prefix_list
, seq_str
,
1571 action
, dest
, ge_str
, le_str
);
1574 DEFPY (no_ipv6_prefix_list_all
,
1575 no_ipv6_prefix_list_all_cmd
,
1576 "no ipv6 prefix-list WORD",
1580 "Name of a prefix list\n")
1582 return vty_prefix_list_uninstall(vty
, AFI_IP6
, prefix_list
, NULL
, NULL
,
1586 DEFPY (ipv6_prefix_list_sequence_number
,
1587 ipv6_prefix_list_sequence_number_cmd
,
1588 "[no] ipv6 prefix-list sequence-number",
1592 "Include/exclude sequence numbers in NVGEN\n")
1594 prefix_master_ipv6
.seqnum
= no
? false : true;
1598 DEFUN (ipv6_prefix_list_description
,
1599 ipv6_prefix_list_description_cmd
,
1600 "ipv6 prefix-list WORD description LINE...",
1603 "Name of a prefix list\n"
1604 "Prefix-list specific description\n"
1605 "Up to 80 characters describing this prefix-list\n")
1609 struct prefix_list
*plist
;
1611 plist
= prefix_list_get(AFI_IP6
, 0, argv
[idx_word
]->arg
);
1614 XFREE(MTYPE_TMP
, plist
->desc
);
1617 plist
->desc
= argv_concat(argv
, argc
, iddx_line
);
1622 DEFUN (no_ipv6_prefix_list_description
,
1623 no_ipv6_prefix_list_description_cmd
,
1624 "no ipv6 prefix-list WORD description",
1628 "Name of a prefix list\n"
1629 "Prefix-list specific description\n")
1632 return vty_prefix_list_desc_unset(vty
, AFI_IP6
, argv
[idx_word
]->arg
);
1636 DEFUN (no_ipv6_prefix_list_description_comment
,
1637 no_ipv6_prefix_list_description_comment_cmd
,
1638 "no ipv6 prefix-list WORD description LINE...",
1642 "Name of a prefix list\n"
1643 "Prefix-list specific description\n"
1644 "Up to 80 characters describing this prefix-list\n")
1646 return no_ipv6_prefix_list_description(self
, vty
, argc
, argv
);
1650 DEFPY (show_ipv6_prefix_list
,
1651 show_ipv6_prefix_list_cmd
,
1652 "show ipv6 prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1656 "Name of a prefix list\n"
1657 "sequence number of an entry\n"
1658 "Sequence number\n")
1660 enum display_type dtype
= normal_display
;
1662 dtype
= sequential_display
;
1664 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, arg_str
, dtype
);
1667 DEFPY (show_ipv6_prefix_list_prefix
,
1668 show_ipv6_prefix_list_prefix_cmd
,
1669 "show ipv6 prefix-list WORD X:X::X:X/M$prefix [longer$dl|first-match$dfm]",
1673 "Name of a prefix list\n"
1674 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1675 "Lookup longer prefix\n"
1676 "First matched prefix\n")
1678 enum display_type dtype
= normal_display
;
1680 dtype
= longer_display
;
1682 dtype
= first_match_display
;
1684 return vty_show_prefix_list_prefix(vty
, AFI_IP6
, prefix_list
,
1688 DEFPY (show_ipv6_prefix_list_summary
,
1689 show_ipv6_prefix_list_summary_cmd
,
1690 "show ipv6 prefix-list summary [WORD$prefix-list]",
1694 "Summary of prefix lists\n"
1695 "Name of a prefix list\n")
1697 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, NULL
,
1701 DEFPY (show_ipv6_prefix_list_detail
,
1702 show_ipv6_prefix_list_detail_cmd
,
1703 "show ipv6 prefix-list detail [WORD$prefix-list]",
1707 "Detail of prefix lists\n"
1708 "Name of a prefix list\n")
1710 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, NULL
,
1714 DEFPY (clear_ipv6_prefix_list
,
1715 clear_ipv6_prefix_list_cmd
,
1716 "clear ipv6 prefix-list [WORD [X:X::X:X/M$prefix]]",
1720 "Name of a prefix list\n"
1721 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
1723 return vty_clear_prefix_list(vty
, AFI_IP6
, prefix_list
, prefix_str
);
1726 /* Configuration write function. */
1727 static int config_write_prefix_afi(afi_t afi
, struct vty
*vty
)
1729 struct prefix_list
*plist
;
1730 struct prefix_list_entry
*pentry
;
1731 struct prefix_master
*master
;
1734 master
= prefix_master_get(afi
, 0);
1738 if (!master
->seqnum
) {
1739 vty_out(vty
, "no ip%s prefix-list sequence-number\n",
1740 afi
== AFI_IP
? "" : "v6");
1741 vty_out(vty
, "!\n");
1744 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
) {
1746 vty_out(vty
, "ip%s prefix-list %s description %s\n",
1747 afi
== AFI_IP
? "" : "v6", plist
->name
,
1752 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1753 vty_out(vty
, "ip%s prefix-list %s ",
1754 afi
== AFI_IP
? "" : "v6", plist
->name
);
1757 vty_out(vty
, "seq %" PRId64
" ", pentry
->seq
);
1759 vty_out(vty
, "%s ", prefix_list_type_str(pentry
));
1762 vty_out(vty
, "any");
1764 struct prefix
*p
= &pentry
->prefix
;
1767 vty_out(vty
, "%s/%d",
1768 inet_ntop(p
->family
, p
->u
.val
, buf
,
1773 vty_out(vty
, " ge %d", pentry
->ge
);
1775 vty_out(vty
, " le %d", pentry
->le
);
1780 /* vty_out (vty, "!\n"); */
1783 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
) {
1785 vty_out(vty
, "ip%s prefix-list %s description %s\n",
1786 afi
== AFI_IP
? "" : "v6", plist
->name
,
1791 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1792 vty_out(vty
, "ip%s prefix-list %s ",
1793 afi
== AFI_IP
? "" : "v6", plist
->name
);
1796 vty_out(vty
, "seq %" PRId64
" ", pentry
->seq
);
1798 vty_out(vty
, "%s", prefix_list_type_str(pentry
));
1801 vty_out(vty
, " any");
1803 struct prefix
*p
= &pentry
->prefix
;
1806 vty_out(vty
, " %s/%d",
1807 inet_ntop(p
->family
, p
->u
.val
, buf
,
1812 vty_out(vty
, " ge %d", pentry
->ge
);
1814 vty_out(vty
, " le %d", pentry
->le
);
1824 struct stream
*prefix_bgp_orf_entry(struct stream
*s
, struct prefix_list
*plist
,
1825 uint8_t init_flag
, uint8_t permit_flag
,
1828 struct prefix_list_entry
*pentry
;
1833 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1834 uint8_t flag
= init_flag
;
1835 struct prefix
*p
= &pentry
->prefix
;
1837 flag
|= (pentry
->type
== PREFIX_PERMIT
? permit_flag
1839 stream_putc(s
, flag
);
1840 stream_putl(s
, (uint32_t)pentry
->seq
);
1841 stream_putc(s
, (uint8_t)pentry
->ge
);
1842 stream_putc(s
, (uint8_t)pentry
->le
);
1843 stream_put_prefix(s
, p
);
1849 int prefix_bgp_orf_set(char *name
, afi_t afi
, struct orf_prefix
*orfp
,
1850 int permit
, int set
)
1852 struct prefix_list
*plist
;
1853 struct prefix_list_entry
*pentry
;
1855 /* ge and le value check */
1856 if (orfp
->ge
&& orfp
->ge
<= orfp
->p
.prefixlen
)
1857 return CMD_WARNING_CONFIG_FAILED
;
1858 if (orfp
->le
&& orfp
->le
<= orfp
->p
.prefixlen
)
1859 return CMD_WARNING_CONFIG_FAILED
;
1860 if (orfp
->le
&& orfp
->ge
> orfp
->le
)
1861 return CMD_WARNING_CONFIG_FAILED
;
1863 if (orfp
->ge
&& orfp
->le
== (afi
== AFI_IP
? 32 : 128))
1866 plist
= prefix_list_get(afi
, 1, name
);
1868 return CMD_WARNING_CONFIG_FAILED
;
1871 pentry
= prefix_list_entry_make(
1872 &orfp
->p
, (permit
? PREFIX_PERMIT
: PREFIX_DENY
),
1873 orfp
->seq
, orfp
->le
, orfp
->ge
, 0);
1875 if (prefix_entry_dup_check(plist
, pentry
)) {
1876 prefix_list_entry_free(pentry
);
1877 return CMD_WARNING_CONFIG_FAILED
;
1880 prefix_list_entry_add(plist
, pentry
);
1882 pentry
= prefix_list_entry_lookup(
1883 plist
, &orfp
->p
, (permit
? PREFIX_PERMIT
: PREFIX_DENY
),
1884 orfp
->seq
, orfp
->le
, orfp
->ge
);
1887 return CMD_WARNING_CONFIG_FAILED
;
1889 prefix_list_entry_delete(plist
, pentry
, 1);
1895 void prefix_bgp_orf_remove_all(afi_t afi
, char *name
)
1897 struct prefix_list
*plist
;
1899 plist
= prefix_bgp_orf_lookup(afi
, name
);
1901 prefix_list_delete(plist
);
1904 /* return prefix count */
1905 int prefix_bgp_show_prefix_list(struct vty
*vty
, afi_t afi
, char *name
,
1908 struct prefix_list
*plist
;
1909 struct prefix_list_entry
*pentry
;
1910 json_object
*json
= NULL
;
1911 json_object
*json_prefix
= NULL
;
1912 json_object
*json_list
= NULL
;
1914 plist
= prefix_bgp_orf_lookup(afi
, name
);
1919 return plist
->count
;
1922 json
= json_object_new_object();
1923 json_prefix
= json_object_new_object();
1924 json_list
= json_object_new_object();
1926 json_object_int_add(json_prefix
, "prefixListCounter",
1928 json_object_string_add(json_prefix
, "prefixListName",
1931 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1932 struct prefix
*p
= &pentry
->prefix
;
1936 sprintf(buf_a
, "%s/%d",
1937 inet_ntop(p
->family
, p
->u
.val
, buf_b
,
1941 json_object_int_add(json_list
, "seq", pentry
->seq
);
1942 json_object_string_add(json_list
, "seqPrefixListType",
1943 prefix_list_type_str(pentry
));
1946 json_object_int_add(json_list
, "ge",
1949 json_object_int_add(json_list
, "le",
1952 json_object_object_add(json_prefix
, buf_a
, json_list
);
1955 json_object_object_add(json
, "ipPrefixList",
1958 json_object_object_add(json
, "ipv6PrefixList",
1961 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
1962 json
, JSON_C_TO_STRING_PRETTY
));
1963 json_object_free(json
);
1965 vty_out(vty
, "ip%s prefix-list %s: %d entries\n",
1966 afi
== AFI_IP
? "" : "v6", plist
->name
, plist
->count
);
1968 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1969 struct prefix
*p
= &pentry
->prefix
;
1972 vty_out(vty
, " seq %" PRId64
" %s %s/%d",
1974 prefix_list_type_str(pentry
),
1975 inet_ntop(p
->family
, p
->u
.val
, buf
, BUFSIZ
),
1979 vty_out(vty
, " ge %d", pentry
->ge
);
1981 vty_out(vty
, " le %d", pentry
->le
);
1986 return plist
->count
;
1989 static void prefix_list_reset_afi(afi_t afi
, int orf
)
1991 struct prefix_list
*plist
;
1992 struct prefix_list
*next
;
1993 struct prefix_master
*master
;
1995 master
= prefix_master_get(afi
, orf
);
1999 for (plist
= master
->num
.head
; plist
; plist
= next
) {
2001 prefix_list_delete(plist
);
2003 for (plist
= master
->str
.head
; plist
; plist
= next
) {
2005 prefix_list_delete(plist
);
2008 assert(master
->num
.head
== NULL
);
2009 assert(master
->num
.tail
== NULL
);
2011 assert(master
->str
.head
== NULL
);
2012 assert(master
->str
.tail
== NULL
);
2015 master
->recent
= NULL
;
2019 /* Prefix-list node. */
2020 static struct cmd_node prefix_node
= {PREFIX_NODE
,
2021 "", /* Prefix list has no interface. */
2024 static int config_write_prefix_ipv4(struct vty
*vty
)
2026 return config_write_prefix_afi(AFI_IP
, vty
);
2029 static void plist_autocomplete_afi(afi_t afi
, vector comps
,
2030 struct cmd_token
*token
)
2032 struct prefix_list
*plist
;
2033 struct prefix_master
*master
;
2035 master
= prefix_master_get(afi
, 0);
2039 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
2040 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, plist
->name
));
2041 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
2042 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, plist
->name
));
2045 static void plist_autocomplete(vector comps
, struct cmd_token
*token
)
2047 plist_autocomplete_afi(AFI_IP
, comps
, token
);
2048 plist_autocomplete_afi(AFI_IP6
, comps
, token
);
2051 static const struct cmd_variable_handler plist_var_handlers
[] = {
2052 {/* "prefix-list WORD" */
2053 .varname
= "prefix_list",
2054 .completions
= plist_autocomplete
},
2055 {.completions
= NULL
}};
2058 static void prefix_list_init_ipv4(void)
2060 install_node(&prefix_node
, config_write_prefix_ipv4
);
2062 install_element(CONFIG_NODE
, &ip_prefix_list_cmd
);
2063 install_element(CONFIG_NODE
, &no_ip_prefix_list_cmd
);
2064 install_element(CONFIG_NODE
, &no_ip_prefix_list_all_cmd
);
2066 install_element(CONFIG_NODE
, &ip_prefix_list_description_cmd
);
2067 install_element(CONFIG_NODE
, &no_ip_prefix_list_description_cmd
);
2068 install_element(CONFIG_NODE
,
2069 &no_ip_prefix_list_description_comment_cmd
);
2071 install_element(CONFIG_NODE
, &ip_prefix_list_sequence_number_cmd
);
2073 install_element(VIEW_NODE
, &show_ip_prefix_list_cmd
);
2074 install_element(VIEW_NODE
, &show_ip_prefix_list_prefix_cmd
);
2075 install_element(VIEW_NODE
, &show_ip_prefix_list_summary_cmd
);
2076 install_element(VIEW_NODE
, &show_ip_prefix_list_detail_cmd
);
2078 install_element(ENABLE_NODE
, &clear_ip_prefix_list_cmd
);
2081 /* Prefix-list node. */
2082 static struct cmd_node prefix_ipv6_node
= {
2083 PREFIX_IPV6_NODE
, "", /* Prefix list has no interface. */
2086 static int config_write_prefix_ipv6(struct vty
*vty
)
2088 return config_write_prefix_afi(AFI_IP6
, vty
);
2091 static void prefix_list_init_ipv6(void)
2093 install_node(&prefix_ipv6_node
, config_write_prefix_ipv6
);
2095 install_element(CONFIG_NODE
, &ipv6_prefix_list_cmd
);
2096 install_element(CONFIG_NODE
, &no_ipv6_prefix_list_cmd
);
2097 install_element(CONFIG_NODE
, &no_ipv6_prefix_list_all_cmd
);
2099 install_element(CONFIG_NODE
, &ipv6_prefix_list_description_cmd
);
2100 install_element(CONFIG_NODE
, &no_ipv6_prefix_list_description_cmd
);
2101 install_element(CONFIG_NODE
,
2102 &no_ipv6_prefix_list_description_comment_cmd
);
2104 install_element(CONFIG_NODE
, &ipv6_prefix_list_sequence_number_cmd
);
2106 install_element(VIEW_NODE
, &show_ipv6_prefix_list_cmd
);
2107 install_element(VIEW_NODE
, &show_ipv6_prefix_list_prefix_cmd
);
2108 install_element(VIEW_NODE
, &show_ipv6_prefix_list_summary_cmd
);
2109 install_element(VIEW_NODE
, &show_ipv6_prefix_list_detail_cmd
);
2111 install_element(ENABLE_NODE
, &clear_ipv6_prefix_list_cmd
);
2114 void prefix_list_init()
2116 cmd_variable_handler_register(plist_var_handlers
);
2118 prefix_list_init_ipv4();
2119 prefix_list_init_ipv6();
2122 void prefix_list_reset()
2124 prefix_list_reset_afi(AFI_IP
, 0);
2125 prefix_list_reset_afi(AFI_IP6
, 0);
2126 prefix_list_reset_afi(AFI_IP
, 1);
2127 prefix_list_reset_afi(AFI_IP6
, 1);