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 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((unsigned char)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 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 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
) {
306 route_map_notify_pentry_dependencies(plist
->name
, pentry
,
307 RMAP_EVENT_PLIST_DELETED
);
309 prefix_list_trie_del(plist
, pentry
);
310 prefix_list_entry_free(pentry
);
314 master
= plist
->master
;
316 if (plist
->type
== PREFIX_TYPE_NUMBER
)
322 plist
->next
->prev
= plist
->prev
;
324 list
->tail
= plist
->prev
;
327 plist
->prev
->next
= plist
->next
;
329 list
->head
= plist
->next
;
331 XFREE(MTYPE_TMP
, plist
->desc
);
333 /* Make sure master's recent changed prefix-list information is
335 master
->recent
= NULL
;
337 route_map_notify_dependencies(plist
->name
, RMAP_EVENT_PLIST_DELETED
);
339 if (master
->delete_hook
)
340 (*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
, bool 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 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 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
]);
505 void prefix_list_entry_delete(struct prefix_list
*plist
,
506 struct prefix_list_entry
*pentry
,
509 if (plist
== NULL
|| pentry
== NULL
)
512 prefix_list_trie_del(plist
, pentry
);
515 pentry
->prev
->next
= pentry
->next
;
517 plist
->head
= pentry
->next
;
519 pentry
->next
->prev
= pentry
->prev
;
521 plist
->tail
= pentry
->prev
;
523 route_map_notify_pentry_dependencies(plist
->name
, pentry
,
524 RMAP_EVENT_PLIST_DELETED
);
525 prefix_list_entry_free(pentry
);
530 route_map_notify_dependencies(plist
->name
,
531 RMAP_EVENT_PLIST_DELETED
);
532 if (plist
->master
->delete_hook
)
533 (*plist
->master
->delete_hook
)(plist
);
535 if (plist
->head
== NULL
&& plist
->tail
== NULL
536 && plist
->desc
== NULL
)
537 prefix_list_delete(plist
);
539 plist
->master
->recent
= plist
;
543 static void trie_install_fn(struct prefix_list_entry
*object
,
544 struct prefix_list_entry
**updptr
)
547 if (*updptr
== object
)
549 if ((*updptr
)->prefix
.prefixlen
< object
->prefix
.prefixlen
)
551 if ((*updptr
)->prefix
.prefixlen
== object
->prefix
.prefixlen
552 && (*updptr
)->seq
> object
->seq
)
554 updptr
= &(*updptr
)->next_best
;
557 if (!object
->next_best
)
558 object
->next_best
= *updptr
;
560 assert(object
->next_best
== *updptr
|| !*updptr
);
565 static void prefix_list_trie_add(struct prefix_list
*plist
,
566 struct prefix_list_entry
*pentry
)
568 size_t depth
= plist
->master
->trie_depth
;
569 uint8_t *bytes
= pentry
->prefix
.u
.val
;
570 size_t validbits
= pentry
->prefix
.prefixlen
;
571 struct pltrie_table
*table
;
574 while (validbits
> PLC_BITS
&& depth
> 1) {
575 if (!table
->entries
[*bytes
].next_table
)
576 table
->entries
[*bytes
].next_table
=
577 XCALLOC(MTYPE_PREFIX_LIST_TRIE
,
578 sizeof(struct pltrie_table
));
579 table
= table
->entries
[*bytes
].next_table
;
582 validbits
-= PLC_BITS
;
585 trie_walk_affected(validbits
, table
, *bytes
, pentry
, trie_install_fn
);
588 static void prefix_list_entry_add(struct prefix_list
*plist
,
589 struct prefix_list_entry
*pentry
)
591 struct prefix_list_entry
*replace
;
592 struct prefix_list_entry
*point
;
594 /* Automatic asignment of seq no. */
595 if (pentry
->seq
== -1)
596 pentry
->seq
= prefix_new_seq_get(plist
);
598 if (plist
->tail
&& pentry
->seq
> plist
->tail
->seq
)
601 /* Is there any same seq prefix list entry? */
602 replace
= prefix_seq_check(plist
, pentry
->seq
);
604 prefix_list_entry_delete(plist
, replace
, 0);
606 /* Check insert point. */
607 for (point
= plist
->head
; point
; point
= point
->next
)
608 if (point
->seq
>= pentry
->seq
)
612 /* In case of this is the first element of the list. */
613 pentry
->next
= point
;
617 point
->prev
->next
= pentry
;
619 plist
->head
= pentry
;
621 pentry
->prev
= point
->prev
;
622 point
->prev
= pentry
;
625 plist
->tail
->next
= pentry
;
627 plist
->head
= pentry
;
629 pentry
->prev
= plist
->tail
;
630 plist
->tail
= pentry
;
633 prefix_list_trie_add(plist
, pentry
);
635 /* Increment count. */
638 route_map_notify_pentry_dependencies(plist
->name
, pentry
,
639 RMAP_EVENT_PLIST_ADDED
);
641 /* Run hook function. */
642 if (plist
->master
->add_hook
)
643 (*plist
->master
->add_hook
)(plist
);
645 route_map_notify_dependencies(plist
->name
, RMAP_EVENT_PLIST_ADDED
);
646 plist
->master
->recent
= plist
;
649 /* Return string of prefix_list_type. */
650 static const char *prefix_list_type_str(struct prefix_list_entry
*pentry
)
652 switch (pentry
->type
) {
662 static int prefix_list_entry_match(struct prefix_list_entry
*pentry
,
663 const struct prefix
*p
)
667 if (pentry
->prefix
.family
!= p
->family
)
670 ret
= prefix_match(&pentry
->prefix
, p
);
674 /* In case of le nor ge is specified, exact match is performed. */
675 if (!pentry
->le
&& !pentry
->ge
) {
676 if (pentry
->prefix
.prefixlen
!= p
->prefixlen
)
680 if (p
->prefixlen
> pentry
->le
)
684 if (p
->prefixlen
< pentry
->ge
)
690 enum prefix_list_type
prefix_list_apply_which_prefix(
691 struct prefix_list
*plist
,
692 const struct prefix
**which
,
695 struct prefix_list_entry
*pentry
, *pbest
= NULL
;
697 const struct prefix
*p
= (const struct prefix
*)object
;
698 const uint8_t *byte
= p
->u
.val
;
700 size_t validbits
= p
->prefixlen
;
701 struct pltrie_table
*table
;
709 if (plist
->count
== 0) {
712 return PREFIX_PERMIT
;
715 depth
= plist
->master
->trie_depth
;
718 for (pentry
= table
->entries
[*byte
].up_chain
; pentry
;
719 pentry
= pentry
->next_best
) {
720 if (pbest
&& pbest
->seq
< pentry
->seq
)
722 if (prefix_list_entry_match(pentry
, p
))
726 if (validbits
<= PLC_BITS
)
728 validbits
-= PLC_BITS
;
731 if (!table
->entries
[*byte
].next_table
)
734 table
= table
->entries
[*byte
].next_table
;
739 for (pentry
= table
->entries
[*byte
].final_chain
; pentry
;
740 pentry
= pentry
->next_best
) {
741 if (pbest
&& pbest
->seq
< pentry
->seq
)
743 if (prefix_list_entry_match(pentry
, p
))
751 *which
= &pbest
->prefix
;
763 static void __attribute__((unused
)) prefix_list_print(struct prefix_list
*plist
)
765 struct prefix_list_entry
*pentry
;
770 printf("ip prefix-list %s: %d entries\n", plist
->name
, plist
->count
);
772 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
774 printf("any %s\n", prefix_list_type_str(pentry
));
781 printf(" seq %lld %s %s/%d", (long long)pentry
->seq
,
782 prefix_list_type_str(pentry
),
783 inet_ntop(p
->family
, p
->u
.val
, buf
, BUFSIZ
),
786 printf(" ge %d", pentry
->ge
);
788 printf(" le %d", pentry
->le
);
794 /* Retrun 1 when plist already include pentry policy. */
795 static struct prefix_list_entry
*
796 prefix_entry_dup_check(struct prefix_list
*plist
, struct prefix_list_entry
*new)
798 size_t depth
, maxdepth
= plist
->master
->trie_depth
;
799 uint8_t byte
, *bytes
= new->prefix
.u
.val
;
800 size_t validbits
= new->prefix
.prefixlen
;
801 struct pltrie_table
*table
;
802 struct prefix_list_entry
*pentry
;
806 seq
= prefix_new_seq_get(plist
);
811 for (depth
= 0; validbits
> PLC_BITS
&& depth
< maxdepth
- 1; depth
++) {
813 if (!table
->entries
[byte
].next_table
)
816 table
= table
->entries
[byte
].next_table
;
817 validbits
-= PLC_BITS
;
821 if (validbits
> PLC_BITS
)
822 pentry
= table
->entries
[byte
].final_chain
;
824 pentry
= table
->entries
[byte
].up_chain
;
826 for (; pentry
; pentry
= pentry
->next_best
) {
827 if (prefix_same(&pentry
->prefix
, &new->prefix
)
828 && pentry
->type
== new->type
&& pentry
->le
== new->le
829 && pentry
->ge
== new->ge
&& pentry
->seq
!= seq
)
844 static void vty_show_prefix_entry(struct vty
*vty
, afi_t afi
,
845 struct prefix_list
*plist
,
846 struct prefix_master
*master
,
847 enum display_type dtype
, int seqnum
)
849 struct prefix_list_entry
*pentry
;
851 /* Print the name of the protocol */
852 vty_out(vty
, "%s: ", frr_protoname
);
854 if (dtype
== normal_display
) {
855 vty_out(vty
, "ip%s prefix-list %s: %d entries\n",
856 afi
== AFI_IP
? "" : "v6", plist
->name
, plist
->count
);
858 vty_out(vty
, " Description: %s\n", plist
->desc
);
859 } else if (dtype
== summary_display
|| dtype
== detail_display
) {
860 vty_out(vty
, "ip%s prefix-list %s:\n",
861 afi
== AFI_IP
? "" : "v6", plist
->name
);
864 vty_out(vty
, " Description: %s\n", plist
->desc
);
867 " count: %d, range entries: %d, sequences: %" PRId64
" - %" PRId64
"\n",
868 plist
->count
, plist
->rangecount
,
869 plist
->head
? plist
->head
->seq
: 0,
870 plist
->tail
? plist
->tail
->seq
: 0);
873 if (dtype
!= summary_display
) {
874 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
875 if (dtype
== sequential_display
876 && pentry
->seq
!= seqnum
)
882 vty_out(vty
, "seq %" PRId64
" ", pentry
->seq
);
884 vty_out(vty
, "%s ", prefix_list_type_str(pentry
));
889 struct prefix
*p
= &pentry
->prefix
;
892 vty_out(vty
, "%s/%d",
893 inet_ntop(p
->family
, p
->u
.val
, buf
,
898 vty_out(vty
, " ge %d", pentry
->ge
);
900 vty_out(vty
, " le %d", pentry
->le
);
903 if (dtype
== detail_display
904 || dtype
== sequential_display
)
905 vty_out(vty
, " (hit count: %ld, refcount: %ld)",
906 pentry
->hitcnt
, pentry
->refcnt
);
913 static int vty_show_prefix_list(struct vty
*vty
, afi_t afi
, const char *name
,
914 const char *seq
, enum display_type dtype
)
916 struct prefix_list
*plist
;
917 struct prefix_master
*master
;
920 master
= prefix_master_get(afi
, 0);
925 seqnum
= (int64_t)atol(seq
);
928 plist
= prefix_list_lookup(afi
, name
);
930 vty_out(vty
, "%% Can't find specified prefix-list\n");
933 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
, seqnum
);
935 if (dtype
== detail_display
|| dtype
== summary_display
) {
938 "Prefix-list with the last deletion/insertion: %s\n",
939 master
->recent
->name
);
942 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
943 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
,
946 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
947 vty_show_prefix_entry(vty
, afi
, plist
, master
, dtype
,
954 static int vty_show_prefix_list_prefix(struct vty
*vty
, afi_t afi
,
955 const char *name
, const char *prefix
,
956 enum display_type type
)
958 struct prefix_list
*plist
;
959 struct prefix_list_entry
*pentry
;
964 plist
= prefix_list_lookup(afi
, name
);
966 vty_out(vty
, "%% Can't find specified prefix-list\n");
970 ret
= str2prefix(prefix
, &p
);
972 vty_out(vty
, "%% prefix is malformed\n");
976 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
979 if (type
== normal_display
|| type
== first_match_display
)
980 if (prefix_same(&p
, &pentry
->prefix
))
983 if (type
== longer_display
) {
984 if ((p
.family
== pentry
->prefix
.family
)
985 && (prefix_match(&p
, &pentry
->prefix
)))
990 vty_out(vty
, " seq %" PRId64
" %s ", pentry
->seq
,
991 prefix_list_type_str(pentry
));
996 struct prefix
*pf
= &pentry
->prefix
;
999 vty_out(vty
, "%s/%d",
1000 inet_ntop(pf
->family
, pf
->u
.val
, buf
,
1005 vty_out(vty
, " ge %d", pentry
->ge
);
1007 vty_out(vty
, " le %d", pentry
->le
);
1010 if (type
== normal_display
1011 || type
== first_match_display
)
1012 vty_out(vty
, " (hit count: %ld, refcount: %ld)",
1013 pentry
->hitcnt
, pentry
->refcnt
);
1017 if (type
== first_match_display
)
1024 static int vty_clear_prefix_list(struct vty
*vty
, afi_t afi
, const char *name
,
1027 struct prefix_master
*master
;
1028 struct prefix_list
*plist
;
1029 struct prefix_list_entry
*pentry
;
1033 master
= prefix_master_get(afi
, 0);
1037 if (name
== NULL
&& prefix
== NULL
) {
1038 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
1039 for (pentry
= plist
->head
; pentry
;
1040 pentry
= pentry
->next
)
1043 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
1044 for (pentry
= plist
->head
; pentry
;
1045 pentry
= pentry
->next
)
1048 plist
= prefix_list_lookup(afi
, name
);
1050 vty_out(vty
, "%% Can't find specified prefix-list\n");
1055 ret
= str2prefix(prefix
, &p
);
1057 vty_out(vty
, "%% prefix is malformed\n");
1062 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1064 if (pentry
->prefix
.family
== p
.family
1065 && prefix_match(&pentry
->prefix
, &p
))
1074 #ifndef VTYSH_EXTRACT_PL
1075 #include "lib/plist_clippy.c"
1078 DEFPY (ip_prefix_list_sequence_number
,
1079 ip_prefix_list_sequence_number_cmd
,
1080 "[no] ip prefix-list sequence-number",
1084 "Include/exclude sequence numbers in NVGEN\n")
1086 prefix_master_ipv4
.seqnum
= no
? false : true;
1091 DEFPY (show_ip_prefix_list
,
1092 show_ip_prefix_list_cmd
,
1093 "show ip prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1097 "Name of a prefix list\n"
1098 "sequence number of an entry\n"
1099 "Sequence number\n")
1101 enum display_type dtype
= normal_display
;
1103 dtype
= sequential_display
;
1105 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, arg_str
, dtype
);
1108 DEFPY (show_ip_prefix_list_prefix
,
1109 show_ip_prefix_list_prefix_cmd
,
1110 "show ip prefix-list WORD A.B.C.D/M$prefix [longer$dl|first-match$dfm]",
1114 "Name of a prefix list\n"
1115 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1116 "Lookup longer prefix\n"
1117 "First matched prefix\n")
1119 enum display_type dtype
= normal_display
;
1121 dtype
= longer_display
;
1123 dtype
= first_match_display
;
1125 return vty_show_prefix_list_prefix(vty
, AFI_IP
, prefix_list
, prefix_str
,
1129 DEFPY (show_ip_prefix_list_summary
,
1130 show_ip_prefix_list_summary_cmd
,
1131 "show ip prefix-list summary [WORD$prefix_list]",
1135 "Summary of prefix lists\n"
1136 "Name of a prefix list\n")
1138 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, NULL
,
1142 DEFPY (show_ip_prefix_list_detail
,
1143 show_ip_prefix_list_detail_cmd
,
1144 "show ip prefix-list detail [WORD$prefix_list]",
1148 "Detail of prefix lists\n"
1149 "Name of a prefix list\n")
1151 return vty_show_prefix_list(vty
, AFI_IP
, prefix_list
, NULL
,
1155 DEFPY (clear_ip_prefix_list
,
1156 clear_ip_prefix_list_cmd
,
1157 "clear ip prefix-list [WORD [A.B.C.D/M$prefix]]",
1161 "Name of a prefix list\n"
1162 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
1164 return vty_clear_prefix_list(vty
, AFI_IP
, prefix_list
, prefix_str
);
1167 DEFPY (ipv6_prefix_list_sequence_number
,
1168 ipv6_prefix_list_sequence_number_cmd
,
1169 "[no] ipv6 prefix-list sequence-number",
1173 "Include/exclude sequence numbers in NVGEN\n")
1175 prefix_master_ipv6
.seqnum
= no
? false : true;
1179 DEFPY (show_ipv6_prefix_list
,
1180 show_ipv6_prefix_list_cmd
,
1181 "show ipv6 prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1185 "Name of a prefix list\n"
1186 "sequence number of an entry\n"
1187 "Sequence number\n")
1189 enum display_type dtype
= normal_display
;
1191 dtype
= sequential_display
;
1193 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, arg_str
, dtype
);
1196 DEFPY (show_ipv6_prefix_list_prefix
,
1197 show_ipv6_prefix_list_prefix_cmd
,
1198 "show ipv6 prefix-list WORD X:X::X:X/M$prefix [longer$dl|first-match$dfm]",
1202 "Name of a prefix list\n"
1203 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1204 "Lookup longer prefix\n"
1205 "First matched prefix\n")
1207 enum display_type dtype
= normal_display
;
1209 dtype
= longer_display
;
1211 dtype
= first_match_display
;
1213 return vty_show_prefix_list_prefix(vty
, AFI_IP6
, prefix_list
,
1217 DEFPY (show_ipv6_prefix_list_summary
,
1218 show_ipv6_prefix_list_summary_cmd
,
1219 "show ipv6 prefix-list summary [WORD$prefix-list]",
1223 "Summary of prefix lists\n"
1224 "Name of a prefix list\n")
1226 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, NULL
,
1230 DEFPY (show_ipv6_prefix_list_detail
,
1231 show_ipv6_prefix_list_detail_cmd
,
1232 "show ipv6 prefix-list detail [WORD$prefix-list]",
1236 "Detail of prefix lists\n"
1237 "Name of a prefix list\n")
1239 return vty_show_prefix_list(vty
, AFI_IP6
, prefix_list
, NULL
,
1243 DEFPY (clear_ipv6_prefix_list
,
1244 clear_ipv6_prefix_list_cmd
,
1245 "clear ipv6 prefix-list [WORD [X:X::X:X/M$prefix]]",
1249 "Name of a prefix list\n"
1250 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
1252 return vty_clear_prefix_list(vty
, AFI_IP6
, prefix_list
, prefix_str
);
1255 struct stream
*prefix_bgp_orf_entry(struct stream
*s
, struct prefix_list
*plist
,
1256 uint8_t init_flag
, uint8_t permit_flag
,
1259 struct prefix_list_entry
*pentry
;
1264 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1265 uint8_t flag
= init_flag
;
1266 struct prefix
*p
= &pentry
->prefix
;
1268 flag
|= (pentry
->type
== PREFIX_PERMIT
? permit_flag
1270 stream_putc(s
, flag
);
1271 stream_putl(s
, (uint32_t)pentry
->seq
);
1272 stream_putc(s
, (uint8_t)pentry
->ge
);
1273 stream_putc(s
, (uint8_t)pentry
->le
);
1274 stream_put_prefix(s
, p
);
1280 int prefix_bgp_orf_set(char *name
, afi_t afi
, struct orf_prefix
*orfp
,
1281 int permit
, int set
)
1283 struct prefix_list
*plist
;
1284 struct prefix_list_entry
*pentry
;
1286 /* ge and le value check */
1287 if (orfp
->ge
&& orfp
->ge
<= orfp
->p
.prefixlen
)
1288 return CMD_WARNING_CONFIG_FAILED
;
1289 if (orfp
->le
&& orfp
->le
<= orfp
->p
.prefixlen
)
1290 return CMD_WARNING_CONFIG_FAILED
;
1291 if (orfp
->le
&& orfp
->ge
> orfp
->le
)
1292 return CMD_WARNING_CONFIG_FAILED
;
1294 if (orfp
->ge
&& orfp
->le
== (afi
== AFI_IP
? 32 : 128))
1297 plist
= prefix_list_get(afi
, 1, name
);
1299 return CMD_WARNING_CONFIG_FAILED
;
1301 apply_mask(&orfp
->p
);
1304 pentry
= prefix_list_entry_make(
1305 &orfp
->p
, (permit
? PREFIX_PERMIT
: PREFIX_DENY
),
1306 orfp
->seq
, orfp
->le
, orfp
->ge
, false);
1308 if (prefix_entry_dup_check(plist
, pentry
)) {
1309 prefix_list_entry_free(pentry
);
1310 return CMD_WARNING_CONFIG_FAILED
;
1313 prefix_list_entry_add(plist
, pentry
);
1315 pentry
= prefix_list_entry_lookup(
1316 plist
, &orfp
->p
, (permit
? PREFIX_PERMIT
: PREFIX_DENY
),
1317 orfp
->seq
, orfp
->le
, orfp
->ge
);
1320 return CMD_WARNING_CONFIG_FAILED
;
1322 prefix_list_entry_delete(plist
, pentry
, 1);
1328 void prefix_bgp_orf_remove_all(afi_t afi
, char *name
)
1330 struct prefix_list
*plist
;
1332 plist
= prefix_bgp_orf_lookup(afi
, name
);
1334 prefix_list_delete(plist
);
1337 /* return prefix count */
1338 int prefix_bgp_show_prefix_list(struct vty
*vty
, afi_t afi
, char *name
,
1341 struct prefix_list
*plist
;
1342 struct prefix_list_entry
*pentry
;
1343 json_object
*json
= NULL
;
1344 json_object
*json_prefix
= NULL
;
1345 json_object
*json_list
= NULL
;
1347 plist
= prefix_bgp_orf_lookup(afi
, name
);
1352 return plist
->count
;
1355 json
= json_object_new_object();
1356 json_prefix
= json_object_new_object();
1357 json_list
= json_object_new_object();
1359 json_object_int_add(json_prefix
, "prefixListCounter",
1361 json_object_string_add(json_prefix
, "prefixListName",
1364 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1365 struct prefix
*p
= &pentry
->prefix
;
1369 snprintf(buf_a
, sizeof(buf_a
), "%s/%d",
1370 inet_ntop(p
->family
, p
->u
.val
, buf_b
, BUFSIZ
),
1373 json_object_int_add(json_list
, "seq", pentry
->seq
);
1374 json_object_string_add(json_list
, "seqPrefixListType",
1375 prefix_list_type_str(pentry
));
1378 json_object_int_add(json_list
, "ge",
1381 json_object_int_add(json_list
, "le",
1384 json_object_object_add(json_prefix
, buf_a
, json_list
);
1387 json_object_object_add(json
, "ipPrefixList",
1390 json_object_object_add(json
, "ipv6PrefixList",
1393 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
1394 json
, JSON_C_TO_STRING_PRETTY
));
1395 json_object_free(json
);
1397 vty_out(vty
, "ip%s prefix-list %s: %d entries\n",
1398 afi
== AFI_IP
? "" : "v6", plist
->name
, plist
->count
);
1400 for (pentry
= plist
->head
; pentry
; pentry
= pentry
->next
) {
1401 struct prefix
*p
= &pentry
->prefix
;
1404 vty_out(vty
, " seq %" PRId64
" %s %s/%d",
1406 prefix_list_type_str(pentry
),
1407 inet_ntop(p
->family
, p
->u
.val
, buf
, BUFSIZ
),
1411 vty_out(vty
, " ge %d", pentry
->ge
);
1413 vty_out(vty
, " le %d", pentry
->le
);
1418 return plist
->count
;
1421 static void prefix_list_reset_afi(afi_t afi
, int orf
)
1423 struct prefix_list
*plist
;
1424 struct prefix_list
*next
;
1425 struct prefix_master
*master
;
1427 master
= prefix_master_get(afi
, orf
);
1431 for (plist
= master
->num
.head
; plist
; plist
= next
) {
1433 prefix_list_delete(plist
);
1435 for (plist
= master
->str
.head
; plist
; plist
= next
) {
1437 prefix_list_delete(plist
);
1440 assert(master
->num
.head
== NULL
);
1441 assert(master
->num
.tail
== NULL
);
1443 assert(master
->str
.head
== NULL
);
1444 assert(master
->str
.tail
== NULL
);
1446 master
->seqnum
= true;
1447 master
->recent
= NULL
;
1450 /* Prefix-list node. */
1451 static struct cmd_node prefix_node
= {
1452 .name
= "ipv4 prefix list",
1453 .node
= PREFIX_NODE
,
1457 static void plist_autocomplete_afi(afi_t afi
, vector comps
,
1458 struct cmd_token
*token
)
1460 struct prefix_list
*plist
;
1461 struct prefix_master
*master
;
1463 master
= prefix_master_get(afi
, 0);
1467 for (plist
= master
->str
.head
; plist
; plist
= plist
->next
)
1468 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, plist
->name
));
1469 for (plist
= master
->num
.head
; plist
; plist
= plist
->next
)
1470 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, plist
->name
));
1473 static void plist_autocomplete(vector comps
, struct cmd_token
*token
)
1475 plist_autocomplete_afi(AFI_IP
, comps
, token
);
1476 plist_autocomplete_afi(AFI_IP6
, comps
, token
);
1479 static const struct cmd_variable_handler plist_var_handlers
[] = {
1480 {/* "prefix-list WORD" */
1481 .varname
= "prefix_list",
1482 .completions
= plist_autocomplete
},
1483 {.completions
= NULL
}};
1486 static void prefix_list_init_ipv4(void)
1488 install_node(&prefix_node
);
1490 install_element(CONFIG_NODE
, &ip_prefix_list_sequence_number_cmd
);
1492 install_element(VIEW_NODE
, &show_ip_prefix_list_cmd
);
1493 install_element(VIEW_NODE
, &show_ip_prefix_list_prefix_cmd
);
1494 install_element(VIEW_NODE
, &show_ip_prefix_list_summary_cmd
);
1495 install_element(VIEW_NODE
, &show_ip_prefix_list_detail_cmd
);
1497 install_element(ENABLE_NODE
, &clear_ip_prefix_list_cmd
);
1500 /* Prefix-list node. */
1501 static struct cmd_node prefix_ipv6_node
= {
1502 .name
= "ipv6 prefix list",
1503 .node
= PREFIX_IPV6_NODE
,
1507 static void prefix_list_init_ipv6(void)
1509 install_node(&prefix_ipv6_node
);
1511 install_element(CONFIG_NODE
, &ipv6_prefix_list_sequence_number_cmd
);
1513 install_element(VIEW_NODE
, &show_ipv6_prefix_list_cmd
);
1514 install_element(VIEW_NODE
, &show_ipv6_prefix_list_prefix_cmd
);
1515 install_element(VIEW_NODE
, &show_ipv6_prefix_list_summary_cmd
);
1516 install_element(VIEW_NODE
, &show_ipv6_prefix_list_detail_cmd
);
1518 install_element(ENABLE_NODE
, &clear_ipv6_prefix_list_cmd
);
1521 void prefix_list_init(void)
1523 cmd_variable_handler_register(plist_var_handlers
);
1525 prefix_list_init_ipv4();
1526 prefix_list_init_ipv6();
1529 void prefix_list_reset(void)
1531 prefix_list_reset_afi(AFI_IP
, 0);
1532 prefix_list_reset_afi(AFI_IP6
, 0);
1533 prefix_list_reset_afi(AFI_IP
, 1);
1534 prefix_list_reset_afi(AFI_IP6
, 1);