]> git.proxmox.com Git - mirror_frr.git/blame - lib/plist.c
snapcraft: Update bgpd to use newer rpki lib
[mirror_frr.git] / lib / plist.c
CommitLineData
718e3744 1/* Prefix list functions.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
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
718e3744 19 */
20
21#include <zebra.h>
22
23#include "prefix.h"
24#include "command.h"
25#include "memory.h"
26#include "plist.h"
27#include "sockunion.h"
28#include "buffer.h"
fbf5d033 29#include "log.h"
518f0eb1 30#include "routemap.h"
6f1ccc12 31#include "lib/json.h"
b85120bc 32#include "libfrr.h"
718e3744 33
a38401b6 34#include "plist_int.h"
718e3744 35
d62a17ae 36DEFINE_MTYPE_STATIC(LIB, PREFIX_LIST, "Prefix List")
37DEFINE_MTYPE_STATIC(LIB, MPREFIX_LIST_STR, "Prefix List Str")
4a1ab8e4 38DEFINE_MTYPE_STATIC(LIB, PREFIX_LIST_ENTRY, "Prefix List Entry")
d62a17ae 39DEFINE_MTYPE_STATIC(LIB, PREFIX_LIST_TRIE, "Prefix List Trie Table")
4a1ab8e4 40
7e111b6b
DL
41/* not currently changeable, code assumes bytes further down */
42#define PLC_BITS 8
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) */
47
48struct pltrie_entry {
49 union {
50 struct pltrie_table *next_table;
51 struct prefix_list_entry *final_chain;
52 };
53
54 struct prefix_list_entry *up_chain;
55};
56
57struct pltrie_table {
58 struct pltrie_entry entries[PLC_LEN];
59};
60
718e3744 61/* List of struct prefix_list. */
d62a17ae 62struct prefix_list_list {
63 struct prefix_list *head;
64 struct prefix_list *tail;
718e3744 65};
66
67/* Master structure of prefix_list. */
d62a17ae 68struct prefix_master {
69 /* List of prefix_list which name is number. */
70 struct prefix_list_list num;
718e3744 71
d62a17ae 72 /* List of prefix_list which name is string. */
73 struct prefix_list_list str;
718e3744 74
d62a17ae 75 /* Whether sequential number is used. */
e11d84ad 76 bool seqnum;
718e3744 77
d62a17ae 78 /* The latest update. */
79 struct prefix_list *recent;
718e3744 80
d62a17ae 81 /* Hook function which is executed when new prefix_list is added. */
82 void (*add_hook)(struct prefix_list *);
718e3744 83
d62a17ae 84 /* Hook function which is executed when prefix_list is deleted. */
85 void (*delete_hook)(struct prefix_list *);
7e111b6b 86
d62a17ae 87 /* number of bytes that have a trie level */
88 size_t trie_depth;
718e3744 89};
90
91/* Static structure of IPv4 prefix_list's master. */
d62a17ae 92static struct prefix_master prefix_master_ipv4 = {
93 {NULL, NULL}, {NULL, NULL}, 1, NULL, NULL, NULL, PLC_MAXLEVELV4,
718e3744 94};
95
718e3744 96/* Static structure of IPv6 prefix-list's master. */
d62a17ae 97static struct prefix_master prefix_master_ipv6 = {
98 {NULL, NULL}, {NULL, NULL}, 1, NULL, NULL, NULL, PLC_MAXLEVELV6,
718e3744 99};
718e3744 100
101/* Static structure of BGP ORF prefix_list's master. */
d62a17ae 102static struct prefix_master prefix_master_orf_v4 = {
103 {NULL, NULL}, {NULL, NULL}, 1, NULL, NULL, NULL, PLC_MAXLEVELV4,
c7da3d50
DL
104};
105
106/* Static structure of BGP ORF prefix_list's master. */
d62a17ae 107static struct prefix_master prefix_master_orf_v6 = {
108 {NULL, NULL}, {NULL, NULL}, 1, NULL, NULL, NULL, PLC_MAXLEVELV6,
718e3744 109};
6b0655a2 110
d62a17ae 111static struct prefix_master *prefix_master_get(afi_t afi, int orf)
718e3744 112{
d62a17ae 113 if (afi == AFI_IP)
114 return orf ? &prefix_master_orf_v4 : &prefix_master_ipv4;
115 if (afi == AFI_IP6)
116 return orf ? &prefix_master_orf_v6 : &prefix_master_ipv6;
117 return NULL;
718e3744 118}
119
d62a17ae 120const char *prefix_list_name(struct prefix_list *plist)
a38401b6 121{
d62a17ae 122 return plist->name;
a38401b6
DL
123}
124
427f8e61
DL
125afi_t prefix_list_afi(struct prefix_list *plist)
126{
127 if (plist->master == &prefix_master_ipv4
128 || plist->master == &prefix_master_orf_v4)
129 return AFI_IP;
130 return AFI_IP6;
131}
132
718e3744 133/* Lookup prefix_list from list of prefix_list by name. */
d62a17ae 134static struct prefix_list *prefix_list_lookup_do(afi_t afi, int orf,
135 const char *name)
718e3744 136{
d62a17ae 137 struct prefix_list *plist;
138 struct prefix_master *master;
718e3744 139
d62a17ae 140 if (name == NULL)
141 return NULL;
718e3744 142
d62a17ae 143 master = prefix_master_get(afi, orf);
144 if (master == NULL)
145 return NULL;
718e3744 146
d62a17ae 147 for (plist = master->num.head; plist; plist = plist->next)
148 if (strcmp(plist->name, name) == 0)
149 return plist;
718e3744 150
d62a17ae 151 for (plist = master->str.head; plist; plist = plist->next)
152 if (strcmp(plist->name, name) == 0)
153 return plist;
718e3744 154
d62a17ae 155 return NULL;
718e3744 156}
157
d62a17ae 158struct prefix_list *prefix_list_lookup(afi_t afi, const char *name)
c7da3d50 159{
d62a17ae 160 return prefix_list_lookup_do(afi, 0, name);
c7da3d50
DL
161}
162
d62a17ae 163struct prefix_list *prefix_bgp_orf_lookup(afi_t afi, const char *name)
c7da3d50 164{
d62a17ae 165 return prefix_list_lookup_do(afi, 1, name);
c7da3d50
DL
166}
167
d62a17ae 168static struct prefix_list *prefix_list_new(void)
718e3744 169{
d62a17ae 170 struct prefix_list *new;
718e3744 171
d62a17ae 172 new = XCALLOC(MTYPE_PREFIX_LIST, sizeof(struct prefix_list));
173 return new;
718e3744 174}
175
d62a17ae 176static void prefix_list_free(struct prefix_list *plist)
718e3744 177{
d62a17ae 178 XFREE(MTYPE_PREFIX_LIST, plist);
718e3744 179}
180
d62a17ae 181static struct prefix_list_entry *prefix_list_entry_new(void)
718e3744 182{
d62a17ae 183 struct prefix_list_entry *new;
718e3744 184
d62a17ae 185 new = XCALLOC(MTYPE_PREFIX_LIST_ENTRY,
186 sizeof(struct prefix_list_entry));
187 return new;
718e3744 188}
189
d62a17ae 190static void prefix_list_entry_free(struct prefix_list_entry *pentry)
718e3744 191{
d62a17ae 192 XFREE(MTYPE_PREFIX_LIST_ENTRY, pentry);
718e3744 193}
194
195/* Insert new prefix list to list of prefix_list. Each prefix_list
196 is sorted by the name. */
d62a17ae 197static struct prefix_list *prefix_list_insert(afi_t afi, int orf,
198 const char *name)
199{
200 unsigned int i;
201 long number;
202 struct prefix_list *plist;
203 struct prefix_list *point;
204 struct prefix_list_list *list;
205 struct prefix_master *master;
206
207 master = prefix_master_get(afi, orf);
208 if (master == NULL)
209 return NULL;
210
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;
215 plist->trie =
216 XCALLOC(MTYPE_PREFIX_LIST_TRIE, sizeof(struct pltrie_table));
217
218 /* If name is made by all digit character. We treat it as
219 number. */
220 for (number = 0, i = 0; i < strlen(name); i++) {
221 if (isdigit((int)name[i]))
222 number = (number * 10) + (name[i] - '0');
223 else
224 break;
225 }
98645ada 226
d62a17ae 227 /* In case of name is all digit character */
228 if (i == strlen(name)) {
229 plist->type = PREFIX_TYPE_NUMBER;
230
231 /* Set prefix_list to number list. */
232 list = &master->num;
233
234 for (point = list->head; point; point = point->next)
235 if (atol(point->name) >= number)
236 break;
237 } else {
238 plist->type = PREFIX_TYPE_STRING;
239
240 /* Set prefix_list to string list. */
241 list = &master->str;
242
243 /* Set point to insertion point. */
244 for (point = list->head; point; point = point->next)
245 if (strcmp(point->name, name) >= 0)
246 break;
247 }
248
249 /* In case of this is the first element of master. */
250 if (list->head == NULL) {
251 list->head = list->tail = plist;
252 return plist;
253 }
254
255 /* In case of insertion is made at the tail of access_list. */
256 if (point == NULL) {
257 plist->prev = list->tail;
258 list->tail->next = plist;
259 list->tail = plist;
260 return plist;
261 }
262
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;
267 list->head = plist;
268 return plist;
269 }
270
271 /* Insertion is made at middle of the access_list. */
272 plist->next = point;
273 plist->prev = point->prev;
274
275 if (point->prev)
276 point->prev->next = plist;
277 point->prev = plist;
278
279 return plist;
280}
281
282static struct prefix_list *prefix_list_get(afi_t afi, int orf, const char *name)
718e3744 283{
d62a17ae 284 struct prefix_list *plist;
285
286 plist = prefix_list_lookup_do(afi, orf, name);
718e3744 287
d62a17ae 288 if (plist == NULL)
289 plist = prefix_list_insert(afi, orf, name);
290 return plist;
291}
718e3744 292
d62a17ae 293static void prefix_list_trie_del(struct prefix_list *plist,
294 struct prefix_list_entry *pentry);
295
296/* Delete prefix-list from prefix_list_master and free it. */
297static void prefix_list_delete(struct prefix_list *plist)
298{
299 struct prefix_list_list *list;
300 struct prefix_master *master;
301 struct prefix_list_entry *pentry;
302 struct prefix_list_entry *next;
303
304 /* If prefix-list contain prefix_list_entry free all of it. */
305 for (pentry = plist->head; pentry; pentry = next) {
306 next = pentry->next;
307 prefix_list_trie_del(plist, pentry);
308 prefix_list_entry_free(pentry);
309 plist->count--;
310 }
718e3744 311
d62a17ae 312 master = plist->master;
718e3744 313
d62a17ae 314 if (plist->type == PREFIX_TYPE_NUMBER)
315 list = &master->num;
316 else
317 list = &master->str;
718e3744 318
d62a17ae 319 if (plist->next)
320 plist->next->prev = plist->prev;
321 else
322 list->tail = plist->prev;
718e3744 323
d62a17ae 324 if (plist->prev)
325 plist->prev->next = plist->next;
326 else
327 list->head = plist->next;
718e3744 328
0a22ddfb 329 XFREE(MTYPE_TMP, plist->desc);
718e3744 330
d62a17ae 331 /* Make sure master's recent changed prefix-list information is
332 cleared. */
333 master->recent = NULL;
518f0eb1 334
d62a17ae 335 route_map_notify_dependencies(plist->name, RMAP_EVENT_PLIST_DELETED);
518f0eb1 336
d62a17ae 337 if (master->delete_hook)
338 (*master->delete_hook)(plist);
518f0eb1 339
0a22ddfb 340 XFREE(MTYPE_MPREFIX_LIST_STR, plist->name);
7e111b6b 341
d62a17ae 342 XFREE(MTYPE_PREFIX_LIST_TRIE, plist->trie);
518f0eb1 343
d62a17ae 344 prefix_list_free(plist);
718e3744 345}
346
02ff83c5 347static struct prefix_list_entry *
d62a17ae 348prefix_list_entry_make(struct prefix *prefix, enum prefix_list_type type,
24512fbd 349 int64_t seq, int le, int ge, int any)
718e3744 350{
d62a17ae 351 struct prefix_list_entry *pentry;
718e3744 352
d62a17ae 353 pentry = prefix_list_entry_new();
718e3744 354
d62a17ae 355 if (any)
356 pentry->any = 1;
718e3744 357
d62a17ae 358 prefix_copy(&pentry->prefix, prefix);
359 pentry->type = type;
360 pentry->seq = seq;
361 pentry->le = le;
362 pentry->ge = ge;
718e3744 363
d62a17ae 364 return pentry;
718e3744 365}
366
367/* Add hook function. */
d62a17ae 368void prefix_list_add_hook(void (*func)(struct prefix_list *plist))
718e3744 369{
d62a17ae 370 prefix_master_ipv4.add_hook = func;
371 prefix_master_ipv6.add_hook = func;
718e3744 372}
373
374/* Delete hook function. */
d62a17ae 375void prefix_list_delete_hook(void (*func)(struct prefix_list *plist))
718e3744 376{
d62a17ae 377 prefix_master_ipv4.delete_hook = func;
378 prefix_master_ipv6.delete_hook = func;
718e3744 379}
380
381/* Calculate new sequential number. */
24512fbd 382static int64_t prefix_new_seq_get(struct prefix_list *plist)
718e3744 383{
24512fbd
DS
384 int64_t maxseq;
385 int64_t newseq;
d62a17ae 386 struct prefix_list_entry *pentry;
718e3744 387
d62a17ae 388 maxseq = newseq = 0;
718e3744 389
d62a17ae 390 for (pentry = plist->head; pentry; pentry = pentry->next) {
391 if (maxseq < pentry->seq)
392 maxseq = pentry->seq;
393 }
718e3744 394
d62a17ae 395 newseq = ((maxseq / 5) * 5) + 5;
396
fbc7ead7 397 return (newseq > UINT_MAX) ? UINT_MAX : newseq;
718e3744 398}
399
400/* Return prefix list entry which has same seq number. */
d62a17ae 401static struct prefix_list_entry *prefix_seq_check(struct prefix_list *plist,
24512fbd 402 int64_t seq)
718e3744 403{
d62a17ae 404 struct prefix_list_entry *pentry;
718e3744 405
d62a17ae 406 for (pentry = plist->head; pentry; pentry = pentry->next)
407 if (pentry->seq == seq)
408 return pentry;
409 return NULL;
718e3744 410}
411
02ff83c5 412static struct prefix_list_entry *
d62a17ae 413prefix_list_entry_lookup(struct prefix_list *plist, struct prefix *prefix,
24512fbd
DS
414 enum prefix_list_type type, int64_t seq,
415 int le, int ge)
718e3744 416{
d62a17ae 417 struct prefix_list_entry *pentry;
718e3744 418
d62a17ae 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)
423 continue;
718e3744 424
d62a17ae 425 if (pentry->le != le)
426 continue;
427 if (pentry->ge != ge)
428 continue;
718e3744 429
d62a17ae 430 return pentry;
431 }
718e3744 432
d62a17ae 433 return NULL;
718e3744 434}
435
d62a17ae 436static 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))
7e111b6b 440{
d62a17ae 441 uint8_t mask;
442 uint16_t bwalk;
7e111b6b 443
d62a17ae 444 if (validbits > PLC_BITS) {
445 fn(object, &table->entries[byte].final_chain);
446 return;
447 }
7e111b6b 448
d62a17ae 449 mask = (1 << (8 - validbits)) - 1;
450 for (bwalk = byte & ~mask; bwalk <= byte + mask; bwalk++) {
451 fn(object, &table->entries[bwalk].up_chain);
452 }
7e111b6b
DL
453}
454
d62a17ae 455static void trie_uninstall_fn(struct prefix_list_entry *object,
456 struct prefix_list_entry **updptr)
7e111b6b 457{
d62a17ae 458 for (; *updptr; updptr = &(*updptr)->next_best)
459 if (*updptr == object) {
460 *updptr = object->next_best;
461 break;
462 }
7e111b6b
DL
463}
464
d62a17ae 465static int trie_table_empty(struct pltrie_table *table)
7e111b6b 466{
d62a17ae 467 size_t i;
468 for (i = 0; i < PLC_LEN; i++)
469 if (table->entries[i].next_table || table->entries[i].up_chain)
470 return 0;
471 return 1;
472}
473
474static void prefix_list_trie_del(struct prefix_list *plist,
475 struct prefix_list_entry *pentry)
476{
477 size_t depth, maxdepth = plist->master->trie_depth;
9eaec2ae 478 uint8_t *bytes = pentry->prefix.u.val;
d62a17ae 479 size_t validbits = pentry->prefix.prefixlen;
480 struct pltrie_table *table, **tables[PLC_MAXLEVEL];
481
482 table = plist->trie;
483 for (depth = 0; validbits > PLC_BITS && depth < maxdepth - 1; depth++) {
484 uint8_t byte = bytes[depth];
485 assert(table->entries[byte].next_table);
486
487 tables[depth + 1] = &table->entries[byte].next_table;
488 table = table->entries[byte].next_table;
489
490 validbits -= PLC_BITS;
491 }
492
493 trie_walk_affected(validbits, table, bytes[depth], pentry,
494 trie_uninstall_fn);
495
496 for (; depth > 0; depth--)
497 if (trie_table_empty(*tables[depth])) {
498 XFREE(MTYPE_PREFIX_LIST_TRIE, *tables[depth]);
499 *tables[depth] = NULL;
500 }
7e111b6b
DL
501}
502
d62a17ae 503
504static void prefix_list_entry_delete(struct prefix_list *plist,
505 struct prefix_list_entry *pentry,
506 int update_list)
7e111b6b 507{
d62a17ae 508 if (plist == NULL || pentry == NULL)
509 return;
510
511 prefix_list_trie_del(plist, pentry);
7e111b6b 512
d62a17ae 513 if (pentry->prev)
514 pentry->prev->next = pentry->next;
515 else
516 plist->head = pentry->next;
517 if (pentry->next)
518 pentry->next->prev = pentry->prev;
519 else
520 plist->tail = pentry->prev;
7e111b6b 521
d62a17ae 522 prefix_list_entry_free(pentry);
7e111b6b 523
d62a17ae 524 plist->count--;
7e111b6b 525
d62a17ae 526 if (update_list) {
527 route_map_notify_dependencies(plist->name,
528 RMAP_EVENT_PLIST_DELETED);
529 if (plist->master->delete_hook)
530 (*plist->master->delete_hook)(plist);
7e111b6b 531
d62a17ae 532 if (plist->head == NULL && plist->tail == NULL
533 && plist->desc == NULL)
534 prefix_list_delete(plist);
535 else
536 plist->master->recent = plist;
537 }
7e111b6b
DL
538}
539
d62a17ae 540static void trie_install_fn(struct prefix_list_entry *object,
541 struct prefix_list_entry **updptr)
542{
543 while (*updptr) {
544 if (*updptr == object)
545 return;
546 if ((*updptr)->prefix.prefixlen < object->prefix.prefixlen)
547 break;
b4e55fc5
DL
548 if ((*updptr)->prefix.prefixlen == object->prefix.prefixlen
549 && (*updptr)->seq > object->seq)
d62a17ae 550 break;
551 updptr = &(*updptr)->next_best;
552 }
553
554 if (!object->next_best)
555 object->next_best = *updptr;
556 else
557 assert(object->next_best == *updptr || !*updptr);
558
559 *updptr = object;
560}
7e111b6b 561
d62a17ae 562static void prefix_list_trie_add(struct prefix_list *plist,
563 struct prefix_list_entry *pentry)
718e3744 564{
d62a17ae 565 size_t depth = plist->master->trie_depth;
9eaec2ae 566 uint8_t *bytes = pentry->prefix.u.val;
d62a17ae 567 size_t validbits = pentry->prefix.prefixlen;
568 struct pltrie_table *table;
569
570 table = plist->trie;
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;
577 bytes++;
578 depth--;
579 validbits -= PLC_BITS;
580 }
a31ff449 581
d62a17ae 582 trie_walk_affected(validbits, table, *bytes, pentry, trie_install_fn);
583}
a31ff449 584
d62a17ae 585static void prefix_list_entry_add(struct prefix_list *plist,
586 struct prefix_list_entry *pentry)
587{
588 struct prefix_list_entry *replace;
589 struct prefix_list_entry *point;
718e3744 590
d62a17ae 591 /* Automatic asignment of seq no. */
592 if (pentry->seq == -1)
593 pentry->seq = prefix_new_seq_get(plist);
718e3744 594
d62a17ae 595 if (plist->tail && pentry->seq > plist->tail->seq)
596 point = NULL;
597 else {
598 /* Is there any same seq prefix list entry? */
599 replace = prefix_seq_check(plist, pentry->seq);
600 if (replace)
601 prefix_list_entry_delete(plist, replace, 0);
718e3744 602
d62a17ae 603 /* Check insert point. */
604 for (point = plist->head; point; point = point->next)
605 if (point->seq >= pentry->seq)
606 break;
607 }
608
609 /* In case of this is the first element of the list. */
610 pentry->next = point;
611
612 if (point) {
613 if (point->prev)
614 point->prev->next = pentry;
615 else
616 plist->head = pentry;
617
618 pentry->prev = point->prev;
619 point->prev = pentry;
620 } else {
621 if (plist->tail)
622 plist->tail->next = pentry;
623 else
624 plist->head = pentry;
625
626 pentry->prev = plist->tail;
627 plist->tail = pentry;
628 }
718e3744 629
d62a17ae 630 prefix_list_trie_add(plist, pentry);
631
632 /* Increment count. */
633 plist->count++;
634
635 /* Run hook function. */
636 if (plist->master->add_hook)
637 (*plist->master->add_hook)(plist);
638
639 route_map_notify_dependencies(plist->name, RMAP_EVENT_PLIST_ADDED);
718e3744 640 plist->master->recent = plist;
718e3744 641}
642
643/* Return string of prefix_list_type. */
d62a17ae 644static const char *prefix_list_type_str(struct prefix_list_entry *pentry)
645{
646 switch (pentry->type) {
647 case PREFIX_PERMIT:
648 return "permit";
649 case PREFIX_DENY:
650 return "deny";
651 default:
652 return "";
653 }
654}
655
656static int prefix_list_entry_match(struct prefix_list_entry *pentry,
123214ef 657 const struct prefix *p)
d62a17ae 658{
659 int ret;
660
4f374985
DS
661 if (pentry->prefix.family != p->family)
662 return 0;
663
d62a17ae 664 ret = prefix_match(&pentry->prefix, p);
665 if (!ret)
666 return 0;
667
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)
671 return 0;
672 } else {
673 if (pentry->le)
674 if (p->prefixlen > pentry->le)
675 return 0;
676
677 if (pentry->ge)
678 if (p->prefixlen < pentry->ge)
679 return 0;
680 }
681 return 1;
682}
683
123214ef
MS
684enum prefix_list_type prefix_list_apply_which_prefix(
685 struct prefix_list *plist,
686 const struct prefix **which,
687 const void *object)
d62a17ae 688{
689 struct prefix_list_entry *pentry, *pbest = NULL;
690
123214ef
MS
691 const struct prefix *p = (const struct prefix *)object;
692 const uint8_t *byte = p->u.val;
d62a17ae 693 size_t depth;
694 size_t validbits = p->prefixlen;
695 struct pltrie_table *table;
696
ce94edc7
DS
697 if (plist == NULL) {
698 if (which)
699 *which = NULL;
d62a17ae 700 return PREFIX_DENY;
ce94edc7 701 }
d62a17ae 702
ce94edc7
DS
703 if (plist->count == 0) {
704 if (which)
705 *which = NULL;
d62a17ae 706 return PREFIX_PERMIT;
ce94edc7 707 }
d62a17ae 708
709 depth = plist->master->trie_depth;
710 table = plist->trie;
711 while (1) {
712 for (pentry = table->entries[*byte].up_chain; pentry;
713 pentry = pentry->next_best) {
714 if (pbest && pbest->seq < pentry->seq)
715 continue;
716 if (prefix_list_entry_match(pentry, p))
717 pbest = pentry;
718 }
719
720 if (validbits <= PLC_BITS)
721 break;
722 validbits -= PLC_BITS;
723
724 if (--depth) {
725 if (!table->entries[*byte].next_table)
726 break;
727
728 table = table->entries[*byte].next_table;
729 byte++;
730 continue;
731 }
732
733 for (pentry = table->entries[*byte].final_chain; pentry;
734 pentry = pentry->next_best) {
735 if (pbest && pbest->seq < pentry->seq)
736 continue;
737 if (prefix_list_entry_match(pentry, p))
738 pbest = pentry;
739 }
740 break;
741 }
742
ce94edc7
DS
743 if (which) {
744 if (pbest)
745 *which = &pbest->prefix;
746 else
747 *which = NULL;
748 }
749
d62a17ae 750 if (pbest == NULL)
751 return PREFIX_DENY;
752
753 return pbest->type;
754}
755
756static void __attribute__((unused)) prefix_list_print(struct prefix_list *plist)
757{
758 struct prefix_list_entry *pentry;
759
760 if (plist == NULL)
761 return;
762
763 printf("ip prefix-list %s: %d entries\n", plist->name, plist->count);
764
765 for (pentry = plist->head; pentry; pentry = pentry->next) {
766 if (pentry->any)
767 printf("any %s\n", prefix_list_type_str(pentry));
768 else {
769 struct prefix *p;
770 char buf[BUFSIZ];
771
772 p = &pentry->prefix;
773
24512fbd 774 printf(" seq %" PRId64 " %s %s/%d", pentry->seq,
d62a17ae 775 prefix_list_type_str(pentry),
9eaec2ae 776 inet_ntop(p->family, p->u.val, buf, BUFSIZ),
d62a17ae 777 p->prefixlen);
778 if (pentry->ge)
779 printf(" ge %d", pentry->ge);
780 if (pentry->le)
781 printf(" le %d", pentry->le);
782 printf("\n");
783 }
718e3744 784 }
718e3744 785}
6b0655a2 786
718e3744 787/* Retrun 1 when plist already include pentry policy. */
02ff83c5 788static struct prefix_list_entry *
d62a17ae 789prefix_entry_dup_check(struct prefix_list *plist, struct prefix_list_entry *new)
790{
791 size_t depth, maxdepth = plist->master->trie_depth;
9eaec2ae 792 uint8_t byte, *bytes = new->prefix.u.val;
d62a17ae 793 size_t validbits = new->prefix.prefixlen;
794 struct pltrie_table *table;
795 struct prefix_list_entry *pentry;
24512fbd 796 int64_t seq = 0;
d62a17ae 797
798 if (new->seq == -1)
799 seq = prefix_new_seq_get(plist);
800 else
801 seq = new->seq;
802
803 table = plist->trie;
804 for (depth = 0; validbits > PLC_BITS && depth < maxdepth - 1; depth++) {
805 byte = bytes[depth];
806 if (!table->entries[byte].next_table)
807 return NULL;
808
809 table = table->entries[byte].next_table;
810 validbits -= PLC_BITS;
718e3744 811 }
718e3744 812
d62a17ae 813 byte = bytes[depth];
814 if (validbits > PLC_BITS)
815 pentry = table->entries[byte].final_chain;
816 else
817 pentry = table->entries[byte].up_chain;
818
819 for (; pentry; pentry = pentry->next_best) {
820 if (prefix_same(&pentry->prefix, &new->prefix)
821 && pentry->type == new->type && pentry->le == new->le
822 && pentry->ge == new->ge && pentry->seq != seq)
823 return pentry;
824 }
825 return NULL;
826}
827
828static int vty_invalid_prefix_range(struct vty *vty, const char *prefix)
829{
830 vty_out(vty,
831 "%% Invalid prefix range for %s, make sure: len < ge-value <= le-value\n",
832 prefix);
833 return CMD_WARNING_CONFIG_FAILED;
834}
835
836static int vty_prefix_list_install(struct vty *vty, afi_t afi, const char *name,
837 const char *seq, const char *typestr,
838 const char *prefix, const char *ge,
839 const char *le)
840{
841 int ret;
842 enum prefix_list_type type;
843 struct prefix_list *plist;
844 struct prefix_list_entry *pentry;
845 struct prefix_list_entry *dup;
846 struct prefix p, p_tmp;
847 int any = 0;
24512fbd 848 int64_t seqnum = -1;
d62a17ae 849 int lenum = 0;
850 int genum = 0;
851
e59294e2 852 if (name == NULL || prefix == NULL || typestr == NULL) {
853 vty_out(vty, "%% Missing prefix or type\n");
854 return CMD_WARNING_CONFIG_FAILED;
855 }
856
d62a17ae 857 /* Sequential number. */
858 if (seq)
24512fbd 859 seqnum = (int64_t)atol(seq);
d62a17ae 860
861 /* ge and le number */
862 if (ge)
863 genum = atoi(ge);
864 if (le)
865 lenum = atoi(le);
866
867 /* Check filter type. */
868 if (strncmp("permit", typestr, 1) == 0)
869 type = PREFIX_PERMIT;
870 else if (strncmp("deny", typestr, 1) == 0)
871 type = PREFIX_DENY;
872 else {
873 vty_out(vty, "%% prefix type must be permit or deny\n");
874 return CMD_WARNING_CONFIG_FAILED;
718e3744 875 }
ddb13fd3 876
d62a17ae 877 /* "any" is special token for matching any IPv4 addresses. */
878 switch (afi) {
879 case AFI_IP:
880 if (strncmp("any", prefix, strlen(prefix)) == 0) {
881 ret = str2prefix_ipv4("0.0.0.0/0",
882 (struct prefix_ipv4 *)&p);
883 genum = 0;
884 lenum = IPV4_MAX_BITLEN;
885 any = 1;
886 } else
887 ret = str2prefix_ipv4(prefix, (struct prefix_ipv4 *)&p);
888
889 if (ret <= 0) {
890 vty_out(vty, "%% Malformed IPv4 prefix\n");
891 return CMD_WARNING_CONFIG_FAILED;
892 }
893
894 /* make a copy to verify prefix matches mask length */
895 prefix_copy(&p_tmp, &p);
896 apply_mask_ipv4((struct prefix_ipv4 *)&p_tmp);
897
898 break;
899 case AFI_IP6:
900 if (strncmp("any", prefix, strlen(prefix)) == 0) {
901 ret = str2prefix_ipv6("::/0", (struct prefix_ipv6 *)&p);
902 genum = 0;
903 lenum = IPV6_MAX_BITLEN;
904 any = 1;
905 } else
906 ret = str2prefix_ipv6(prefix, (struct prefix_ipv6 *)&p);
907
908 if (ret <= 0) {
909 vty_out(vty, "%% Malformed IPv6 prefix\n");
910 return CMD_WARNING_CONFIG_FAILED;
911 }
912
913 /* make a copy to verify prefix matches mask length */
914 prefix_copy(&p_tmp, &p);
915 apply_mask_ipv6((struct prefix_ipv6 *)&p_tmp);
916
917 break;
918 case AFI_L2VPN:
919 default:
920 vty_out(vty, "%% Unrecognized AFI (%d)\n", afi);
921 return CMD_WARNING_CONFIG_FAILED;
922 break;
718e3744 923 }
718e3744 924
d62a17ae 925 /* If prefix has bits not under the mask, adjust it to fit */
926 if (!prefix_same(&p_tmp, &p)) {
927 char buf[PREFIX2STR_BUFFER];
928 char buf_tmp[PREFIX2STR_BUFFER];
929 prefix2str(&p, buf, sizeof(buf));
930 prefix2str(&p_tmp, buf_tmp, sizeof(buf_tmp));
0e411ce6
DS
931 vty_out(vty,
932 "%% Prefix-list %s prefix changed from %s to %s to match length\n",
d62a17ae 933 name, buf, buf_tmp);
ade6974d
QY
934 zlog_info(
935 "Prefix-list %s prefix changed from %s to %s to match length",
936 name, buf, buf_tmp);
d62a17ae 937 p = p_tmp;
718e3744 938 }
ddb13fd3 939
d62a17ae 940 /* ge and le check. */
941 if (genum && (genum <= p.prefixlen))
942 return vty_invalid_prefix_range(vty, prefix);
943
4015e918 944 if (lenum && (lenum < p.prefixlen))
d62a17ae 945 return vty_invalid_prefix_range(vty, prefix);
946
947 if (lenum && (genum > lenum))
948 return vty_invalid_prefix_range(vty, prefix);
949
950 if (genum && (lenum == (afi == AFI_IP ? 32 : 128)))
951 lenum = 0;
952
953 /* Get prefix_list with name. */
954 plist = prefix_list_get(afi, 0, name);
955
956 /* Make prefix entry. */
957 pentry = prefix_list_entry_make(&p, type, seqnum, lenum, genum, any);
958
959 /* Check same policy. */
960 dup = prefix_entry_dup_check(plist, pentry);
961
962 if (dup) {
963 prefix_list_entry_free(pentry);
964 return CMD_SUCCESS;
718e3744 965 }
718e3744 966
d62a17ae 967 /* Install new filter to the access_list. */
968 prefix_list_entry_add(plist, pentry);
969
970 return CMD_SUCCESS;
971}
972
973static int vty_prefix_list_uninstall(struct vty *vty, afi_t afi,
974 const char *name, const char *seq,
975 const char *typestr, const char *prefix,
976 const char *ge, const char *le)
977{
978 int ret;
979 enum prefix_list_type type;
980 struct prefix_list *plist;
981 struct prefix_list_entry *pentry;
982 struct prefix p;
24512fbd 983 int64_t seqnum = -1;
d62a17ae 984 int lenum = 0;
985 int genum = 0;
986
987 /* Check prefix list name. */
988 plist = prefix_list_lookup(afi, name);
989 if (!plist) {
990 vty_out(vty, "%% Can't find specified prefix-list\n");
991 return CMD_WARNING_CONFIG_FAILED;
718e3744 992 }
d62a17ae 993
994 /* Only prefix-list name specified, delete the entire prefix-list. */
995 if (seq == NULL && typestr == NULL && prefix == NULL && ge == NULL
996 && le == NULL) {
997 prefix_list_delete(plist);
998 return CMD_SUCCESS;
718e3744 999 }
718e3744 1000
d62a17ae 1001 /* We must have, at a minimum, both the type and prefix here */
1002 if ((typestr == NULL) || (prefix == NULL)) {
1003 vty_out(vty, "%% Both prefix and type required\n");
1004 return CMD_WARNING_CONFIG_FAILED;
718e3744 1005 }
718e3744 1006
d62a17ae 1007 /* Check sequence number. */
1008 if (seq)
24512fbd 1009 seqnum = (int64_t)atol(seq);
d62a17ae 1010
1011 /* ge and le number */
1012 if (ge)
1013 genum = atoi(ge);
1014 if (le)
1015 lenum = atoi(le);
1016
1017 /* Check of filter type. */
1018 if (strncmp("permit", typestr, 1) == 0)
1019 type = PREFIX_PERMIT;
1020 else if (strncmp("deny", typestr, 1) == 0)
1021 type = PREFIX_DENY;
1022 else {
1023 vty_out(vty, "%% prefix type must be permit or deny\n");
1024 return CMD_WARNING_CONFIG_FAILED;
1025 }
718e3744 1026
d62a17ae 1027 /* "any" is special token for matching any IPv4 addresses. */
1028 if (afi == AFI_IP) {
1029 if (strncmp("any", prefix, strlen(prefix)) == 0) {
1030 ret = str2prefix_ipv4("0.0.0.0/0",
1031 (struct prefix_ipv4 *)&p);
1032 genum = 0;
1033 lenum = IPV4_MAX_BITLEN;
1034 } else
1035 ret = str2prefix_ipv4(prefix, (struct prefix_ipv4 *)&p);
1036
1037 if (ret <= 0) {
1038 vty_out(vty, "%% Malformed IPv4 prefix\n");
1039 return CMD_WARNING_CONFIG_FAILED;
1040 }
1041 } else if (afi == AFI_IP6) {
1042 if (strncmp("any", prefix, strlen(prefix)) == 0) {
1043 ret = str2prefix_ipv6("::/0", (struct prefix_ipv6 *)&p);
1044 genum = 0;
1045 lenum = IPV6_MAX_BITLEN;
1046 } else
1047 ret = str2prefix_ipv6(prefix, (struct prefix_ipv6 *)&p);
1048
1049 if (ret <= 0) {
1050 vty_out(vty, "%% Malformed IPv6 prefix\n");
1051 return CMD_WARNING_CONFIG_FAILED;
1052 }
1053 }
718e3744 1054
d62a17ae 1055 /* Lookup prefix entry. */
1056 pentry =
1057 prefix_list_entry_lookup(plist, &p, type, seqnum, lenum, genum);
718e3744 1058
d62a17ae 1059 if (pentry == NULL) {
1060 vty_out(vty, "%% Can't find specified prefix-list\n");
1061 return CMD_WARNING_CONFIG_FAILED;
1062 }
1063
1064 /* Install new filter to the access_list. */
1065 prefix_list_entry_delete(plist, pentry, 1);
1066
1067 return CMD_SUCCESS;
718e3744 1068}
1069
d62a17ae 1070static int vty_prefix_list_desc_unset(struct vty *vty, afi_t afi,
1071 const char *name)
718e3744 1072{
d62a17ae 1073 struct prefix_list *plist;
718e3744 1074
d62a17ae 1075 plist = prefix_list_lookup(afi, name);
1076 if (!plist) {
1077 vty_out(vty, "%% Can't find specified prefix-list\n");
1078 return CMD_WARNING_CONFIG_FAILED;
1079 }
718e3744 1080
d62a17ae 1081 if (plist->desc) {
1082 XFREE(MTYPE_TMP, plist->desc);
1083 plist->desc = NULL;
1084 }
718e3744 1085
d62a17ae 1086 if (plist->head == NULL && plist->tail == NULL && plist->desc == NULL)
1087 prefix_list_delete(plist);
718e3744 1088
d62a17ae 1089 return CMD_SUCCESS;
718e3744 1090}
1091
d62a17ae 1092enum display_type {
1093 normal_display,
1094 summary_display,
1095 detail_display,
1096 sequential_display,
1097 longer_display,
1098 first_match_display
718e3744 1099};
1100
d62a17ae 1101static void vty_show_prefix_entry(struct vty *vty, afi_t afi,
1102 struct prefix_list *plist,
1103 struct prefix_master *master,
1104 enum display_type dtype, int seqnum)
1105{
1106 struct prefix_list_entry *pentry;
1107
1108 /* Print the name of the protocol */
1109 vty_out(vty, "%s: ", frr_protoname);
1110
1111 if (dtype == normal_display) {
1112 vty_out(vty, "ip%s prefix-list %s: %d entries\n",
1113 afi == AFI_IP ? "" : "v6", plist->name, plist->count);
1114 if (plist->desc)
1115 vty_out(vty, " Description: %s\n", plist->desc);
1116 } else if (dtype == summary_display || dtype == detail_display) {
1117 vty_out(vty, "ip%s prefix-list %s:\n",
1118 afi == AFI_IP ? "" : "v6", plist->name);
1119
1120 if (plist->desc)
1121 vty_out(vty, " Description: %s\n", plist->desc);
1122
1123 vty_out(vty,
24512fbd 1124 " count: %d, range entries: %d, sequences: %" PRId64 " - %" PRId64 "\n",
d62a17ae 1125 plist->count, plist->rangecount,
1126 plist->head ? plist->head->seq : 0,
1127 plist->tail ? plist->tail->seq : 0);
718e3744 1128 }
718e3744 1129
d62a17ae 1130 if (dtype != summary_display) {
1131 for (pentry = plist->head; pentry; pentry = pentry->next) {
1132 if (dtype == sequential_display
1133 && pentry->seq != seqnum)
1134 continue;
718e3744 1135
d62a17ae 1136 vty_out(vty, " ");
718e3744 1137
d62a17ae 1138 if (master->seqnum)
24512fbd 1139 vty_out(vty, "seq %" PRId64 " ", pentry->seq);
718e3744 1140
d62a17ae 1141 vty_out(vty, "%s ", prefix_list_type_str(pentry));
718e3744 1142
d62a17ae 1143 if (pentry->any)
1144 vty_out(vty, "any");
1145 else {
1146 struct prefix *p = &pentry->prefix;
1147 char buf[BUFSIZ];
1148
1149 vty_out(vty, "%s/%d",
9eaec2ae 1150 inet_ntop(p->family, p->u.val, buf,
d62a17ae 1151 BUFSIZ),
1152 p->prefixlen);
1153
1154 if (pentry->ge)
1155 vty_out(vty, " ge %d", pentry->ge);
1156 if (pentry->le)
1157 vty_out(vty, " le %d", pentry->le);
1158 }
1159
1160 if (dtype == detail_display
1161 || dtype == sequential_display)
1162 vty_out(vty, " (hit count: %ld, refcount: %ld)",
1163 pentry->hitcnt, pentry->refcnt);
1164
1165 vty_out(vty, "\n");
1166 }
718e3744 1167 }
718e3744 1168}
1169
d62a17ae 1170static int vty_show_prefix_list(struct vty *vty, afi_t afi, const char *name,
1171 const char *seq, enum display_type dtype)
1172{
1173 struct prefix_list *plist;
1174 struct prefix_master *master;
24512fbd 1175 int64_t seqnum = 0;
d62a17ae 1176
1177 master = prefix_master_get(afi, 0);
1178 if (master == NULL)
1179 return CMD_WARNING;
1180
1181 if (seq)
24512fbd 1182 seqnum = (int64_t)atol(seq);
d62a17ae 1183
1184 if (name) {
1185 plist = prefix_list_lookup(afi, name);
1186 if (!plist) {
1187 vty_out(vty, "%% Can't find specified prefix-list\n");
1188 return CMD_WARNING;
1189 }
1190 vty_show_prefix_entry(vty, afi, plist, master, dtype, seqnum);
1191 } else {
1192 if (dtype == detail_display || dtype == summary_display) {
1193 if (master->recent)
1194 vty_out(vty,
1195 "Prefix-list with the last deletion/insertion: %s\n",
1196 master->recent->name);
1197 }
1198
1199 for (plist = master->num.head; plist; plist = plist->next)
1200 vty_show_prefix_entry(vty, afi, plist, master, dtype,
1201 seqnum);
1202
1203 for (plist = master->str.head; plist; plist = plist->next)
1204 vty_show_prefix_entry(vty, afi, plist, master, dtype,
1205 seqnum);
1206 }
718e3744 1207
d62a17ae 1208 return CMD_SUCCESS;
1209}
718e3744 1210
d62a17ae 1211static int vty_show_prefix_list_prefix(struct vty *vty, afi_t afi,
1212 const char *name, const char *prefix,
1213 enum display_type type)
1214{
1215 struct prefix_list *plist;
1216 struct prefix_list_entry *pentry;
1217 struct prefix p;
1218 int ret;
1219 int match;
718e3744 1220
d62a17ae 1221 plist = prefix_list_lookup(afi, name);
1222 if (!plist) {
1223 vty_out(vty, "%% Can't find specified prefix-list\n");
1224 return CMD_WARNING;
718e3744 1225 }
1226
d62a17ae 1227 ret = str2prefix(prefix, &p);
1228 if (ret <= 0) {
1229 vty_out(vty, "%% prefix is malformed\n");
1230 return CMD_WARNING;
718e3744 1231 }
1232
d62a17ae 1233 for (pentry = plist->head; pentry; pentry = pentry->next) {
1234 match = 0;
1235
1236 if (type == normal_display || type == first_match_display)
1237 if (prefix_same(&p, &pentry->prefix))
1238 match = 1;
1239
4f374985 1240 if (type == longer_display) {
996c9314
LB
1241 if ((p.family == pentry->prefix.family)
1242 && (prefix_match(&p, &pentry->prefix)))
d62a17ae 1243 match = 1;
4f374985 1244 }
d62a17ae 1245
1246 if (match) {
24512fbd 1247 vty_out(vty, " seq %" PRId64 " %s ", pentry->seq,
d62a17ae 1248 prefix_list_type_str(pentry));
1249
1250 if (pentry->any)
1251 vty_out(vty, "any");
1252 else {
7fe96307 1253 struct prefix *pf = &pentry->prefix;
d62a17ae 1254 char buf[BUFSIZ];
1255
1256 vty_out(vty, "%s/%d",
7fe96307 1257 inet_ntop(pf->family, pf->u.val, buf,
d62a17ae 1258 BUFSIZ),
7fe96307 1259 pf->prefixlen);
d62a17ae 1260
1261 if (pentry->ge)
1262 vty_out(vty, " ge %d", pentry->ge);
1263 if (pentry->le)
1264 vty_out(vty, " le %d", pentry->le);
1265 }
1266
1267 if (type == normal_display
1268 || type == first_match_display)
1269 vty_out(vty, " (hit count: %ld, refcount: %ld)",
1270 pentry->hitcnt, pentry->refcnt);
1271
1272 vty_out(vty, "\n");
1273
1274 if (type == first_match_display)
1275 return CMD_SUCCESS;
1276 }
718e3744 1277 }
d62a17ae 1278 return CMD_SUCCESS;
1279}
1280
1281static int vty_clear_prefix_list(struct vty *vty, afi_t afi, const char *name,
1282 const char *prefix)
1283{
1284 struct prefix_master *master;
1285 struct prefix_list *plist;
1286 struct prefix_list_entry *pentry;
1287 int ret;
1288 struct prefix p;
1289
1290 master = prefix_master_get(afi, 0);
1291 if (master == NULL)
1292 return CMD_WARNING;
1293
1294 if (name == NULL && prefix == NULL) {
1295 for (plist = master->num.head; plist; plist = plist->next)
1296 for (pentry = plist->head; pentry;
1297 pentry = pentry->next)
1298 pentry->hitcnt = 0;
1299
1300 for (plist = master->str.head; plist; plist = plist->next)
1301 for (pentry = plist->head; pentry;
1302 pentry = pentry->next)
1303 pentry->hitcnt = 0;
1304 } else {
1305 plist = prefix_list_lookup(afi, name);
1306 if (!plist) {
1307 vty_out(vty, "%% Can't find specified prefix-list\n");
1308 return CMD_WARNING;
1309 }
1310
1311 if (prefix) {
1312 ret = str2prefix(prefix, &p);
1313 if (ret <= 0) {
1314 vty_out(vty, "%% prefix is malformed\n");
1315 return CMD_WARNING;
1316 }
1317 }
1318
1319 for (pentry = plist->head; pentry; pentry = pentry->next) {
1320 if (prefix) {
996c9314
LB
1321 if (pentry->prefix.family == p.family
1322 && prefix_match(&pentry->prefix, &p))
d62a17ae 1323 pentry->hitcnt = 0;
1324 } else
1325 pentry->hitcnt = 0;
1326 }
1327 }
1328 return CMD_SUCCESS;
718e3744 1329}
6b0655a2 1330
6cdff321 1331#ifndef VTYSH_EXTRACT_PL
2e4c2296 1332#include "lib/plist_clippy.c"
6cdff321
DL
1333#endif
1334
1335DEFPY (ip_prefix_list,
1336 ip_prefix_list_cmd,
1337 "ip prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|A.B.C.D/M$dest [{ge (0-32)|le (0-32)}]>",
1338 IP_STR
718e3744 1339 PREFIX_LIST_STR
1340 "Name of a prefix list\n"
1341 "sequence number of an entry\n"
1342 "Sequence number\n"
1343 "Specify packets to reject\n"
1344 "Specify packets to forward\n"
6cdff321
DL
1345 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1346 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1347 "Minimum prefix length to be matched\n"
1348 "Minimum prefix length\n"
718e3744 1349 "Maximum prefix length to be matched\n"
1350 "Maximum prefix length\n")
1351{
d62a17ae 1352 return vty_prefix_list_install(vty, AFI_IP, prefix_list, seq_str,
1353 action, dest, ge_str, le_str);
718e3744 1354}
1355
6cdff321
DL
1356DEFPY (no_ip_prefix_list,
1357 no_ip_prefix_list_cmd,
1358 "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)}]>",
1359 NO_STR
1360 IP_STR
718e3744 1361 PREFIX_LIST_STR
1362 "Name of a prefix list\n"
1363 "sequence number of an entry\n"
1364 "Sequence number\n"
1365 "Specify packets to reject\n"
1366 "Specify packets to forward\n"
6cdff321
DL
1367 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1368 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
718e3744 1369 "Minimum prefix length to be matched\n"
6cdff321
DL
1370 "Minimum prefix length\n"
1371 "Maximum prefix length to be matched\n"
1372 "Maximum prefix length\n")
718e3744 1373{
d62a17ae 1374 return vty_prefix_list_uninstall(vty, AFI_IP, prefix_list, seq_str,
1375 action, dest, ge_str, le_str);
718e3744 1376}
1377
6cdff321
DL
1378DEFPY (no_ip_prefix_list_all,
1379 no_ip_prefix_list_all_cmd,
1380 "no ip prefix-list WORD",
718e3744 1381 NO_STR
6cdff321 1382 IP_STR
718e3744 1383 PREFIX_LIST_STR
1384 "Name of a prefix list\n")
1385{
d62a17ae 1386 return vty_prefix_list_uninstall(vty, AFI_IP, prefix_list, NULL, NULL,
1387 NULL, NULL, NULL);
718e3744 1388}
1389
6cdff321
DL
1390DEFPY (ip_prefix_list_sequence_number,
1391 ip_prefix_list_sequence_number_cmd,
1392 "[no] ip prefix-list sequence-number",
718e3744 1393 NO_STR
6cdff321 1394 IP_STR
718e3744 1395 PREFIX_LIST_STR
6cdff321 1396 "Include/exclude sequence numbers in NVGEN\n")
718e3744 1397{
e11d84ad 1398 prefix_master_ipv4.seqnum = no ? false : true;
d62a17ae 1399 return CMD_SUCCESS;
718e3744 1400}
1401
6cdff321
DL
1402DEFUN (ip_prefix_list_description,
1403 ip_prefix_list_description_cmd,
1404 "ip prefix-list WORD description LINE...",
1405 IP_STR
718e3744 1406 PREFIX_LIST_STR
1407 "Name of a prefix list\n"
6cdff321
DL
1408 "Prefix-list specific description\n"
1409 "Up to 80 characters describing this prefix-list\n")
718e3744 1410{
d62a17ae 1411 int idx_word = 2;
1412 int idx_line = 4;
1413 struct prefix_list *plist;
1414
1415 plist = prefix_list_get(AFI_IP, 0, argv[idx_word]->arg);
6cdff321 1416
d62a17ae 1417 if (plist->desc) {
1418 XFREE(MTYPE_TMP, plist->desc);
1419 plist->desc = NULL;
1420 }
1421 plist->desc = argv_concat(argv, argc, idx_line);
6cdff321 1422
d62a17ae 1423 return CMD_SUCCESS;
718e3744 1424}
1425
6cdff321
DL
1426DEFUN (no_ip_prefix_list_description,
1427 no_ip_prefix_list_description_cmd,
1428 "no ip prefix-list WORD description",
718e3744 1429 NO_STR
6cdff321 1430 IP_STR
718e3744 1431 PREFIX_LIST_STR
1432 "Name of a prefix list\n"
6cdff321 1433 "Prefix-list specific description\n")
718e3744 1434{
d62a17ae 1435 int idx_word = 3;
1436 return vty_prefix_list_desc_unset(vty, AFI_IP, argv[idx_word]->arg);
718e3744 1437}
1438
6cdff321
DL
1439/* ALIAS_FIXME */
1440DEFUN (no_ip_prefix_list_description_comment,
1441 no_ip_prefix_list_description_comment_cmd,
1442 "no ip prefix-list WORD description LINE...",
718e3744 1443 NO_STR
6cdff321 1444 IP_STR
718e3744 1445 PREFIX_LIST_STR
1446 "Name of a prefix list\n"
6cdff321
DL
1447 "Prefix-list specific description\n"
1448 "Up to 80 characters describing this prefix-list\n")
718e3744 1449{
d62a17ae 1450 return no_ip_prefix_list_description(self, vty, argc, argv);
718e3744 1451}
1452
6cdff321
DL
1453DEFPY (show_ip_prefix_list,
1454 show_ip_prefix_list_cmd,
1455 "show ip prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1456 SHOW_STR
1457 IP_STR
718e3744 1458 PREFIX_LIST_STR
1459 "Name of a prefix list\n"
6cdff321
DL
1460 "sequence number of an entry\n"
1461 "Sequence number\n")
718e3744 1462{
d62a17ae 1463 enum display_type dtype = normal_display;
1464 if (dseq)
1465 dtype = sequential_display;
6cdff321 1466
d62a17ae 1467 return vty_show_prefix_list(vty, AFI_IP, prefix_list, arg_str, dtype);
718e3744 1468}
1469
6cdff321
DL
1470DEFPY (show_ip_prefix_list_prefix,
1471 show_ip_prefix_list_prefix_cmd,
1472 "show ip prefix-list WORD A.B.C.D/M$prefix [longer$dl|first-match$dfm]",
1473 SHOW_STR
1474 IP_STR
718e3744 1475 PREFIX_LIST_STR
1476 "Name of a prefix list\n"
6cdff321
DL
1477 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1478 "Lookup longer prefix\n"
1479 "First matched prefix\n")
718e3744 1480{
d62a17ae 1481 enum display_type dtype = normal_display;
1482 if (dl)
1483 dtype = longer_display;
1484 else if (dfm)
1485 dtype = first_match_display;
6cdff321 1486
d62a17ae 1487 return vty_show_prefix_list_prefix(vty, AFI_IP, prefix_list, prefix_str,
1488 dtype);
718e3744 1489}
1490
6cdff321
DL
1491DEFPY (show_ip_prefix_list_summary,
1492 show_ip_prefix_list_summary_cmd,
1493 "show ip prefix-list summary [WORD$prefix_list]",
1494 SHOW_STR
1495 IP_STR
718e3744 1496 PREFIX_LIST_STR
6cdff321
DL
1497 "Summary of prefix lists\n"
1498 "Name of a prefix list\n")
718e3744 1499{
d62a17ae 1500 return vty_show_prefix_list(vty, AFI_IP, prefix_list, NULL,
1501 summary_display);
718e3744 1502}
1503
6cdff321
DL
1504DEFPY (show_ip_prefix_list_detail,
1505 show_ip_prefix_list_detail_cmd,
1506 "show ip prefix-list detail [WORD$prefix_list]",
1507 SHOW_STR
1508 IP_STR
1509 PREFIX_LIST_STR
1510 "Detail of prefix lists\n"
1511 "Name of a prefix list\n")
1512{
d62a17ae 1513 return vty_show_prefix_list(vty, AFI_IP, prefix_list, NULL,
1514 detail_display);
6cdff321
DL
1515}
1516
1517DEFPY (clear_ip_prefix_list,
1518 clear_ip_prefix_list_cmd,
1519 "clear ip prefix-list [WORD [A.B.C.D/M$prefix]]",
1520 CLEAR_STR
1521 IP_STR
718e3744 1522 PREFIX_LIST_STR
1523 "Name of a prefix list\n"
6cdff321 1524 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
718e3744 1525{
d62a17ae 1526 return vty_clear_prefix_list(vty, AFI_IP, prefix_list, prefix_str);
6cdff321
DL
1527}
1528
1529DEFPY (ipv6_prefix_list,
1530 ipv6_prefix_list_cmd,
1531 "ipv6 prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|X:X::X:X/M$dest [{ge (0-128)|le (0-128)}]>",
718e3744 1532 IPV6_STR
1533 PREFIX_LIST_STR
1534 "Name of a prefix list\n"
1535 "sequence number of an entry\n"
1536 "Sequence number\n"
1537 "Specify packets to reject\n"
1538 "Specify packets to forward\n"
6cdff321 1539 "Any prefix match. Same as \"::0/0 le 128\"\n"
718e3744 1540 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1541 "Maximum prefix length to be matched\n"
6cdff321
DL
1542 "Maximum prefix length\n"
1543 "Minimum prefix length to be matched\n"
1544 "Minimum prefix length\n")
718e3744 1545{
d62a17ae 1546 return vty_prefix_list_install(vty, AFI_IP6, prefix_list, seq_str,
1547 action, dest, ge_str, le_str);
718e3744 1548}
1549
6cdff321
DL
1550DEFPY (no_ipv6_prefix_list,
1551 no_ipv6_prefix_list_cmd,
1552 "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)}]>",
718e3744 1553 NO_STR
1554 IPV6_STR
1555 PREFIX_LIST_STR
1556 "Name of a prefix list\n"
1557 "sequence number of an entry\n"
1558 "Sequence number\n"
1559 "Specify packets to reject\n"
1560 "Specify packets to forward\n"
6cdff321 1561 "Any prefix match. Same as \"::0/0 le 128\"\n"
718e3744 1562 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1563 "Maximum prefix length to be matched\n"
1564 "Maximum prefix length\n"
1565 "Minimum prefix length to be matched\n"
1566 "Minimum prefix length\n")
1567{
d62a17ae 1568 return vty_prefix_list_uninstall(vty, AFI_IP6, prefix_list, seq_str,
1569 action, dest, ge_str, le_str);
718e3744 1570}
1571
6cdff321
DL
1572DEFPY (no_ipv6_prefix_list_all,
1573 no_ipv6_prefix_list_all_cmd,
1574 "no ipv6 prefix-list WORD",
1575 NO_STR
718e3744 1576 IPV6_STR
1577 PREFIX_LIST_STR
6cdff321 1578 "Name of a prefix list\n")
718e3744 1579{
d62a17ae 1580 return vty_prefix_list_uninstall(vty, AFI_IP6, prefix_list, NULL, NULL,
1581 NULL, NULL, NULL);
718e3744 1582}
1583
6cdff321
DL
1584DEFPY (ipv6_prefix_list_sequence_number,
1585 ipv6_prefix_list_sequence_number_cmd,
1586 "[no] ipv6 prefix-list sequence-number",
718e3744 1587 NO_STR
1588 IPV6_STR
1589 PREFIX_LIST_STR
1590 "Include/exclude sequence numbers in NVGEN\n")
1591{
e11d84ad 1592 prefix_master_ipv6.seqnum = no ? false : true;
d62a17ae 1593 return CMD_SUCCESS;
718e3744 1594}
1595
1596DEFUN (ipv6_prefix_list_description,
1597 ipv6_prefix_list_description_cmd,
e961923c 1598 "ipv6 prefix-list WORD description LINE...",
718e3744 1599 IPV6_STR
1600 PREFIX_LIST_STR
1601 "Name of a prefix list\n"
1602 "Prefix-list specific description\n"
1603 "Up to 80 characters describing this prefix-list\n")
1604{
d62a17ae 1605 int idx_word = 2;
1606 int iddx_line = 4;
1607 struct prefix_list *plist;
718e3744 1608
d62a17ae 1609 plist = prefix_list_get(AFI_IP6, 0, argv[idx_word]->arg);
1610
1611 if (plist->desc) {
1612 XFREE(MTYPE_TMP, plist->desc);
1613 plist->desc = NULL;
1614 }
1615 plist->desc = argv_concat(argv, argc, iddx_line);
718e3744 1616
d62a17ae 1617 return CMD_SUCCESS;
c349116d 1618}
718e3744 1619
1620DEFUN (no_ipv6_prefix_list_description,
1621 no_ipv6_prefix_list_description_cmd,
1622 "no ipv6 prefix-list WORD description",
1623 NO_STR
1624 IPV6_STR
1625 PREFIX_LIST_STR
1626 "Name of a prefix list\n"
1627 "Prefix-list specific description\n")
1628{
d62a17ae 1629 int idx_word = 3;
1630 return vty_prefix_list_desc_unset(vty, AFI_IP6, argv[idx_word]->arg);
718e3744 1631}
1632
d862bffb
QY
1633/* ALIAS_FIXME */
1634DEFUN (no_ipv6_prefix_list_description_comment,
1635 no_ipv6_prefix_list_description_comment_cmd,
1636 "no ipv6 prefix-list WORD description LINE...",
1637 NO_STR
1638 IPV6_STR
1639 PREFIX_LIST_STR
1640 "Name of a prefix list\n"
1641 "Prefix-list specific description\n"
1642 "Up to 80 characters describing this prefix-list\n")
1643{
d62a17ae 1644 return no_ipv6_prefix_list_description(self, vty, argc, argv);
d862bffb
QY
1645}
1646
718e3744 1647
6cdff321 1648DEFPY (show_ipv6_prefix_list,
718e3744 1649 show_ipv6_prefix_list_cmd,
6cdff321 1650 "show ipv6 prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
718e3744 1651 SHOW_STR
1652 IPV6_STR
1653 PREFIX_LIST_STR
1654 "Name of a prefix list\n"
1655 "sequence number of an entry\n"
7111c1a0 1656 "Sequence number\n")
718e3744 1657{
d62a17ae 1658 enum display_type dtype = normal_display;
1659 if (dseq)
1660 dtype = sequential_display;
718e3744 1661
d62a17ae 1662 return vty_show_prefix_list(vty, AFI_IP6, prefix_list, arg_str, dtype);
718e3744 1663}
1664
6cdff321
DL
1665DEFPY (show_ipv6_prefix_list_prefix,
1666 show_ipv6_prefix_list_prefix_cmd,
1667 "show ipv6 prefix-list WORD X:X::X:X/M$prefix [longer$dl|first-match$dfm]",
718e3744 1668 SHOW_STR
1669 IPV6_STR
1670 PREFIX_LIST_STR
1671 "Name of a prefix list\n"
1672 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
6cdff321 1673 "Lookup longer prefix\n"
718e3744 1674 "First matched prefix\n")
1675{
d62a17ae 1676 enum display_type dtype = normal_display;
1677 if (dl)
1678 dtype = longer_display;
1679 else if (dfm)
1680 dtype = first_match_display;
718e3744 1681
d62a17ae 1682 return vty_show_prefix_list_prefix(vty, AFI_IP6, prefix_list,
1683 prefix_str, dtype);
718e3744 1684}
1685
6cdff321
DL
1686DEFPY (show_ipv6_prefix_list_summary,
1687 show_ipv6_prefix_list_summary_cmd,
1688 "show ipv6 prefix-list summary [WORD$prefix-list]",
718e3744 1689 SHOW_STR
1690 IPV6_STR
1691 PREFIX_LIST_STR
1692 "Summary of prefix lists\n"
1693 "Name of a prefix list\n")
1694{
d62a17ae 1695 return vty_show_prefix_list(vty, AFI_IP6, prefix_list, NULL,
1696 summary_display);
718e3744 1697}
1698
6cdff321 1699DEFPY (show_ipv6_prefix_list_detail,
718e3744 1700 show_ipv6_prefix_list_detail_cmd,
6cdff321 1701 "show ipv6 prefix-list detail [WORD$prefix-list]",
718e3744 1702 SHOW_STR
1703 IPV6_STR
1704 PREFIX_LIST_STR
1705 "Detail of prefix lists\n"
1706 "Name of a prefix list\n")
1707{
d62a17ae 1708 return vty_show_prefix_list(vty, AFI_IP6, prefix_list, NULL,
1709 detail_display);
718e3744 1710}
1711
6cdff321 1712DEFPY (clear_ipv6_prefix_list,
718e3744 1713 clear_ipv6_prefix_list_cmd,
6cdff321 1714 "clear ipv6 prefix-list [WORD [X:X::X:X/M$prefix]]",
718e3744 1715 CLEAR_STR
1716 IPV6_STR
1717 PREFIX_LIST_STR
1718 "Name of a prefix list\n"
1719 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
1720{
d62a17ae 1721 return vty_clear_prefix_list(vty, AFI_IP6, prefix_list, prefix_str);
718e3744 1722}
6b0655a2 1723
718e3744 1724/* Configuration write function. */
d62a17ae 1725static int config_write_prefix_afi(afi_t afi, struct vty *vty)
1726{
1727 struct prefix_list *plist;
1728 struct prefix_list_entry *pentry;
1729 struct prefix_master *master;
1730 int write = 0;
1731
1732 master = prefix_master_get(afi, 0);
1733 if (master == NULL)
1734 return 0;
718e3744 1735
d62a17ae 1736 if (!master->seqnum) {
1737 vty_out(vty, "no ip%s prefix-list sequence-number\n",
1738 afi == AFI_IP ? "" : "v6");
1739 vty_out(vty, "!\n");
718e3744 1740 }
d62a17ae 1741
1742 for (plist = master->num.head; plist; plist = plist->next) {
1743 if (plist->desc) {
1744 vty_out(vty, "ip%s prefix-list %s description %s\n",
1745 afi == AFI_IP ? "" : "v6", plist->name,
1746 plist->desc);
1747 write++;
1748 }
1749
1750 for (pentry = plist->head; pentry; pentry = pentry->next) {
1751 vty_out(vty, "ip%s prefix-list %s ",
1752 afi == AFI_IP ? "" : "v6", plist->name);
1753
1754 if (master->seqnum)
24512fbd 1755 vty_out(vty, "seq %" PRId64 " ", pentry->seq);
d62a17ae 1756
1757 vty_out(vty, "%s ", prefix_list_type_str(pentry));
1758
1759 if (pentry->any)
1760 vty_out(vty, "any");
1761 else {
1762 struct prefix *p = &pentry->prefix;
1763 char buf[BUFSIZ];
1764
1765 vty_out(vty, "%s/%d",
9eaec2ae 1766 inet_ntop(p->family, p->u.val, buf,
d62a17ae 1767 BUFSIZ),
1768 p->prefixlen);
1769
1770 if (pentry->ge)
1771 vty_out(vty, " ge %d", pentry->ge);
1772 if (pentry->le)
1773 vty_out(vty, " le %d", pentry->le);
1774 }
1775 vty_out(vty, "\n");
1776 write++;
1777 }
1778 /* vty_out (vty, "!\n"); */
718e3744 1779 }
1780
d62a17ae 1781 for (plist = master->str.head; plist; plist = plist->next) {
1782 if (plist->desc) {
1783 vty_out(vty, "ip%s prefix-list %s description %s\n",
1784 afi == AFI_IP ? "" : "v6", plist->name,
1785 plist->desc);
1786 write++;
1787 }
1788
1789 for (pentry = plist->head; pentry; pentry = pentry->next) {
1790 vty_out(vty, "ip%s prefix-list %s ",
1791 afi == AFI_IP ? "" : "v6", plist->name);
1792
1793 if (master->seqnum)
24512fbd 1794 vty_out(vty, "seq %" PRId64 " ", pentry->seq);
d62a17ae 1795
1796 vty_out(vty, "%s", prefix_list_type_str(pentry));
1797
1798 if (pentry->any)
1799 vty_out(vty, " any");
1800 else {
1801 struct prefix *p = &pentry->prefix;
1802 char buf[BUFSIZ];
1803
1804 vty_out(vty, " %s/%d",
9eaec2ae 1805 inet_ntop(p->family, p->u.val, buf,
d62a17ae 1806 BUFSIZ),
1807 p->prefixlen);
1808
1809 if (pentry->ge)
1810 vty_out(vty, " ge %d", pentry->ge);
1811 if (pentry->le)
1812 vty_out(vty, " le %d", pentry->le);
1813 }
1814 vty_out(vty, "\n");
1815 write++;
1816 }
718e3744 1817 }
d62a17ae 1818
1819 return write;
718e3744 1820}
1821
d62a17ae 1822struct stream *prefix_bgp_orf_entry(struct stream *s, struct prefix_list *plist,
d7c0a89a
QY
1823 uint8_t init_flag, uint8_t permit_flag,
1824 uint8_t deny_flag)
718e3744 1825{
d62a17ae 1826 struct prefix_list_entry *pentry;
718e3744 1827
d62a17ae 1828 if (!plist)
1829 return s;
718e3744 1830
d62a17ae 1831 for (pentry = plist->head; pentry; pentry = pentry->next) {
d7c0a89a 1832 uint8_t flag = init_flag;
d62a17ae 1833 struct prefix *p = &pentry->prefix;
718e3744 1834
d62a17ae 1835 flag |= (pentry->type == PREFIX_PERMIT ? permit_flag
1836 : deny_flag);
1837 stream_putc(s, flag);
d7c0a89a
QY
1838 stream_putl(s, (uint32_t)pentry->seq);
1839 stream_putc(s, (uint8_t)pentry->ge);
1840 stream_putc(s, (uint8_t)pentry->le);
d62a17ae 1841 stream_put_prefix(s, p);
1842 }
718e3744 1843
d62a17ae 1844 return s;
718e3744 1845}
1846
d62a17ae 1847int prefix_bgp_orf_set(char *name, afi_t afi, struct orf_prefix *orfp,
1848 int permit, int set)
718e3744 1849{
d62a17ae 1850 struct prefix_list *plist;
1851 struct prefix_list_entry *pentry;
718e3744 1852
d62a17ae 1853 /* ge and le value check */
1854 if (orfp->ge && orfp->ge <= orfp->p.prefixlen)
1855 return CMD_WARNING_CONFIG_FAILED;
1856 if (orfp->le && orfp->le <= orfp->p.prefixlen)
1857 return CMD_WARNING_CONFIG_FAILED;
1858 if (orfp->le && orfp->ge > orfp->le)
1859 return CMD_WARNING_CONFIG_FAILED;
718e3744 1860
d62a17ae 1861 if (orfp->ge && orfp->le == (afi == AFI_IP ? 32 : 128))
1862 orfp->le = 0;
718e3744 1863
d62a17ae 1864 plist = prefix_list_get(afi, 1, name);
1865 if (!plist)
1866 return CMD_WARNING_CONFIG_FAILED;
718e3744 1867
d62a17ae 1868 if (set) {
1869 pentry = prefix_list_entry_make(
1870 &orfp->p, (permit ? PREFIX_PERMIT : PREFIX_DENY),
1871 orfp->seq, orfp->le, orfp->ge, 0);
718e3744 1872
d62a17ae 1873 if (prefix_entry_dup_check(plist, pentry)) {
1874 prefix_list_entry_free(pentry);
1875 return CMD_WARNING_CONFIG_FAILED;
1876 }
718e3744 1877
d62a17ae 1878 prefix_list_entry_add(plist, pentry);
1879 } else {
1880 pentry = prefix_list_entry_lookup(
1881 plist, &orfp->p, (permit ? PREFIX_PERMIT : PREFIX_DENY),
1882 orfp->seq, orfp->le, orfp->ge);
718e3744 1883
d62a17ae 1884 if (!pentry)
1885 return CMD_WARNING_CONFIG_FAILED;
718e3744 1886
d62a17ae 1887 prefix_list_entry_delete(plist, pentry, 1);
1888 }
718e3744 1889
d62a17ae 1890 return CMD_SUCCESS;
718e3744 1891}
1892
d62a17ae 1893void prefix_bgp_orf_remove_all(afi_t afi, char *name)
718e3744 1894{
d62a17ae 1895 struct prefix_list *plist;
718e3744 1896
d62a17ae 1897 plist = prefix_bgp_orf_lookup(afi, name);
1898 if (plist)
1899 prefix_list_delete(plist);
718e3744 1900}
1901
1902/* return prefix count */
d62a17ae 1903int prefix_bgp_show_prefix_list(struct vty *vty, afi_t afi, char *name,
9f049418 1904 bool use_json)
d62a17ae 1905{
1906 struct prefix_list *plist;
1907 struct prefix_list_entry *pentry;
1908 json_object *json = NULL;
1909 json_object *json_prefix = NULL;
1910 json_object *json_list = NULL;
1911
1912 plist = prefix_bgp_orf_lookup(afi, name);
1913 if (!plist)
1914 return 0;
1915
1916 if (!vty)
1917 return plist->count;
1918
1919 if (use_json) {
1920 json = json_object_new_object();
1921 json_prefix = json_object_new_object();
1922 json_list = json_object_new_object();
1923
1924 json_object_int_add(json_prefix, "prefixListCounter",
1925 plist->count);
1926 json_object_string_add(json_prefix, "prefixListName",
1927 plist->name);
1928
1929 for (pentry = plist->head; pentry; pentry = pentry->next) {
1930 struct prefix *p = &pentry->prefix;
1931 char buf_a[BUFSIZ];
1932 char buf_b[BUFSIZ];
1933
1934 sprintf(buf_a, "%s/%d",
9eaec2ae 1935 inet_ntop(p->family, p->u.val, buf_b,
d62a17ae 1936 BUFSIZ),
1937 p->prefixlen);
1938
1939 json_object_int_add(json_list, "seq", pentry->seq);
1940 json_object_string_add(json_list, "seqPrefixListType",
1941 prefix_list_type_str(pentry));
1942
1943 if (pentry->ge)
1944 json_object_int_add(json_list, "ge",
1945 pentry->ge);
1946 if (pentry->le)
1947 json_object_int_add(json_list, "le",
1948 pentry->le);
1949
1950 json_object_object_add(json_prefix, buf_a, json_list);
1951 }
1952 if (afi == AFI_IP)
1953 json_object_object_add(json, "ipPrefixList",
1954 json_prefix);
1955 else
1956 json_object_object_add(json, "ipv6PrefixList",
1957 json_prefix);
1958
9d303b37
DL
1959 vty_out(vty, "%s\n", json_object_to_json_string_ext(
1960 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 1961 json_object_free(json);
1962 } else {
1963 vty_out(vty, "ip%s prefix-list %s: %d entries\n",
1964 afi == AFI_IP ? "" : "v6", plist->name, plist->count);
1965
1966 for (pentry = plist->head; pentry; pentry = pentry->next) {
1967 struct prefix *p = &pentry->prefix;
1968 char buf[BUFSIZ];
1969
24512fbd
DS
1970 vty_out(vty, " seq %" PRId64 " %s %s/%d",
1971 pentry->seq,
d62a17ae 1972 prefix_list_type_str(pentry),
9eaec2ae 1973 inet_ntop(p->family, p->u.val, buf, BUFSIZ),
d62a17ae 1974 p->prefixlen);
1975
1976 if (pentry->ge)
1977 vty_out(vty, " ge %d", pentry->ge);
1978 if (pentry->le)
1979 vty_out(vty, " le %d", pentry->le);
1980
1981 vty_out(vty, "\n");
1982 }
1983 }
1984 return plist->count;
1985}
1986
1987static void prefix_list_reset_afi(afi_t afi, int orf)
1988{
1989 struct prefix_list *plist;
1990 struct prefix_list *next;
1991 struct prefix_master *master;
1992
1993 master = prefix_master_get(afi, orf);
1994 if (master == NULL)
1995 return;
1996
1997 for (plist = master->num.head; plist; plist = next) {
1998 next = plist->next;
1999 prefix_list_delete(plist);
2000 }
2001 for (plist = master->str.head; plist; plist = next) {
2002 next = plist->next;
2003 prefix_list_delete(plist);
2004 }
2005
2006 assert(master->num.head == NULL);
2007 assert(master->num.tail == NULL);
2008
2009 assert(master->str.head == NULL);
2010 assert(master->str.tail == NULL);
2011
2012 master->seqnum = 1;
2013 master->recent = NULL;
718e3744 2014}
2015
2016
2017/* Prefix-list node. */
d62a17ae 2018static struct cmd_node prefix_node = {PREFIX_NODE,
2019 "", /* Prefix list has no interface. */
2020 1};
718e3744 2021
d62a17ae 2022static int config_write_prefix_ipv4(struct vty *vty)
718e3744 2023{
d62a17ae 2024 return config_write_prefix_afi(AFI_IP, vty);
718e3744 2025}
2026
d62a17ae 2027static void plist_autocomplete_afi(afi_t afi, vector comps,
2028 struct cmd_token *token)
70d44c5c 2029{
d62a17ae 2030 struct prefix_list *plist;
2031 struct prefix_master *master;
70d44c5c 2032
d62a17ae 2033 master = prefix_master_get(afi, 0);
2034 if (master == NULL)
2035 return;
70d44c5c 2036
d62a17ae 2037 for (plist = master->str.head; plist; plist = plist->next)
2038 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, plist->name));
2039 for (plist = master->num.head; plist; plist = plist->next)
2040 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, plist->name));
70d44c5c
DL
2041}
2042
d62a17ae 2043static void plist_autocomplete(vector comps, struct cmd_token *token)
70d44c5c 2044{
d62a17ae 2045 plist_autocomplete_afi(AFI_IP, comps, token);
2046 plist_autocomplete_afi(AFI_IP6, comps, token);
70d44c5c
DL
2047}
2048
2049static const struct cmd_variable_handler plist_var_handlers[] = {
d62a17ae 2050 {/* "prefix-list WORD" */
2051 .varname = "prefix_list",
2052 .completions = plist_autocomplete},
2053 {.completions = NULL}};
70d44c5c
DL
2054
2055
d62a17ae 2056static void prefix_list_init_ipv4(void)
718e3744 2057{
d62a17ae 2058 install_node(&prefix_node, config_write_prefix_ipv4);
718e3744 2059
d62a17ae 2060 install_element(CONFIG_NODE, &ip_prefix_list_cmd);
2061 install_element(CONFIG_NODE, &no_ip_prefix_list_cmd);
2062 install_element(CONFIG_NODE, &no_ip_prefix_list_all_cmd);
718e3744 2063
d62a17ae 2064 install_element(CONFIG_NODE, &ip_prefix_list_description_cmd);
2065 install_element(CONFIG_NODE, &no_ip_prefix_list_description_cmd);
2066 install_element(CONFIG_NODE,
2067 &no_ip_prefix_list_description_comment_cmd);
718e3744 2068
d62a17ae 2069 install_element(CONFIG_NODE, &ip_prefix_list_sequence_number_cmd);
718e3744 2070
d62a17ae 2071 install_element(VIEW_NODE, &show_ip_prefix_list_cmd);
2072 install_element(VIEW_NODE, &show_ip_prefix_list_prefix_cmd);
2073 install_element(VIEW_NODE, &show_ip_prefix_list_summary_cmd);
2074 install_element(VIEW_NODE, &show_ip_prefix_list_detail_cmd);
718e3744 2075
d62a17ae 2076 install_element(ENABLE_NODE, &clear_ip_prefix_list_cmd);
718e3744 2077}
2078
718e3744 2079/* Prefix-list node. */
d62a17ae 2080static struct cmd_node prefix_ipv6_node = {
2081 PREFIX_IPV6_NODE, "", /* Prefix list has no interface. */
2082 1};
718e3744 2083
d62a17ae 2084static int config_write_prefix_ipv6(struct vty *vty)
718e3744 2085{
d62a17ae 2086 return config_write_prefix_afi(AFI_IP6, vty);
718e3744 2087}
2088
d62a17ae 2089static void prefix_list_init_ipv6(void)
718e3744 2090{
d62a17ae 2091 install_node(&prefix_ipv6_node, config_write_prefix_ipv6);
718e3744 2092
d62a17ae 2093 install_element(CONFIG_NODE, &ipv6_prefix_list_cmd);
2094 install_element(CONFIG_NODE, &no_ipv6_prefix_list_cmd);
2095 install_element(CONFIG_NODE, &no_ipv6_prefix_list_all_cmd);
718e3744 2096
d62a17ae 2097 install_element(CONFIG_NODE, &ipv6_prefix_list_description_cmd);
2098 install_element(CONFIG_NODE, &no_ipv6_prefix_list_description_cmd);
2099 install_element(CONFIG_NODE,
2100 &no_ipv6_prefix_list_description_comment_cmd);
718e3744 2101
d62a17ae 2102 install_element(CONFIG_NODE, &ipv6_prefix_list_sequence_number_cmd);
718e3744 2103
d62a17ae 2104 install_element(VIEW_NODE, &show_ipv6_prefix_list_cmd);
2105 install_element(VIEW_NODE, &show_ipv6_prefix_list_prefix_cmd);
2106 install_element(VIEW_NODE, &show_ipv6_prefix_list_summary_cmd);
2107 install_element(VIEW_NODE, &show_ipv6_prefix_list_detail_cmd);
718e3744 2108
d62a17ae 2109 install_element(ENABLE_NODE, &clear_ipv6_prefix_list_cmd);
718e3744 2110}
718e3744 2111
4d762f26 2112void prefix_list_init(void)
718e3744 2113{
d62a17ae 2114 cmd_variable_handler_register(plist_var_handlers);
70d44c5c 2115
d62a17ae 2116 prefix_list_init_ipv4();
2117 prefix_list_init_ipv6();
718e3744 2118}
2119
4d762f26 2120void prefix_list_reset(void)
718e3744 2121{
d62a17ae 2122 prefix_list_reset_afi(AFI_IP, 0);
2123 prefix_list_reset_afi(AFI_IP6, 0);
2124 prefix_list_reset_afi(AFI_IP, 1);
2125 prefix_list_reset_afi(AFI_IP6, 1);
718e3744 2126}