]> git.proxmox.com Git - mirror_frr.git/blame - lib/plist.c
lib: do not test before XFREE
[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++) {
fefa5e0f 221 if (isdigit((unsigned char)name[i]))
d62a17ae 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]);
d62a17ae 499 }
7e111b6b
DL
500}
501
d62a17ae 502
503static void prefix_list_entry_delete(struct prefix_list *plist,
504 struct prefix_list_entry *pentry,
505 int update_list)
7e111b6b 506{
d62a17ae 507 if (plist == NULL || pentry == NULL)
508 return;
509
510 prefix_list_trie_del(plist, pentry);
7e111b6b 511
d62a17ae 512 if (pentry->prev)
513 pentry->prev->next = pentry->next;
514 else
515 plist->head = pentry->next;
516 if (pentry->next)
517 pentry->next->prev = pentry->prev;
518 else
519 plist->tail = pentry->prev;
7e111b6b 520
d62a17ae 521 prefix_list_entry_free(pentry);
7e111b6b 522
d62a17ae 523 plist->count--;
7e111b6b 524
d62a17ae 525 if (update_list) {
526 route_map_notify_dependencies(plist->name,
527 RMAP_EVENT_PLIST_DELETED);
528 if (plist->master->delete_hook)
529 (*plist->master->delete_hook)(plist);
7e111b6b 530
d62a17ae 531 if (plist->head == NULL && plist->tail == NULL
532 && plist->desc == NULL)
533 prefix_list_delete(plist);
534 else
535 plist->master->recent = plist;
536 }
7e111b6b
DL
537}
538
d62a17ae 539static void trie_install_fn(struct prefix_list_entry *object,
540 struct prefix_list_entry **updptr)
541{
542 while (*updptr) {
543 if (*updptr == object)
544 return;
545 if ((*updptr)->prefix.prefixlen < object->prefix.prefixlen)
546 break;
b4e55fc5
DL
547 if ((*updptr)->prefix.prefixlen == object->prefix.prefixlen
548 && (*updptr)->seq > object->seq)
d62a17ae 549 break;
550 updptr = &(*updptr)->next_best;
551 }
552
553 if (!object->next_best)
554 object->next_best = *updptr;
555 else
556 assert(object->next_best == *updptr || !*updptr);
557
558 *updptr = object;
559}
7e111b6b 560
d62a17ae 561static void prefix_list_trie_add(struct prefix_list *plist,
562 struct prefix_list_entry *pentry)
718e3744 563{
d62a17ae 564 size_t depth = plist->master->trie_depth;
9eaec2ae 565 uint8_t *bytes = pentry->prefix.u.val;
d62a17ae 566 size_t validbits = pentry->prefix.prefixlen;
567 struct pltrie_table *table;
568
569 table = plist->trie;
570 while (validbits > PLC_BITS && depth > 1) {
571 if (!table->entries[*bytes].next_table)
572 table->entries[*bytes].next_table =
573 XCALLOC(MTYPE_PREFIX_LIST_TRIE,
574 sizeof(struct pltrie_table));
575 table = table->entries[*bytes].next_table;
576 bytes++;
577 depth--;
578 validbits -= PLC_BITS;
579 }
a31ff449 580
d62a17ae 581 trie_walk_affected(validbits, table, *bytes, pentry, trie_install_fn);
582}
a31ff449 583
d62a17ae 584static void prefix_list_entry_add(struct prefix_list *plist,
585 struct prefix_list_entry *pentry)
586{
587 struct prefix_list_entry *replace;
588 struct prefix_list_entry *point;
718e3744 589
d62a17ae 590 /* Automatic asignment of seq no. */
591 if (pentry->seq == -1)
592 pentry->seq = prefix_new_seq_get(plist);
718e3744 593
d62a17ae 594 if (plist->tail && pentry->seq > plist->tail->seq)
595 point = NULL;
596 else {
597 /* Is there any same seq prefix list entry? */
598 replace = prefix_seq_check(plist, pentry->seq);
599 if (replace)
600 prefix_list_entry_delete(plist, replace, 0);
718e3744 601
d62a17ae 602 /* Check insert point. */
603 for (point = plist->head; point; point = point->next)
604 if (point->seq >= pentry->seq)
605 break;
606 }
607
608 /* In case of this is the first element of the list. */
609 pentry->next = point;
610
611 if (point) {
612 if (point->prev)
613 point->prev->next = pentry;
614 else
615 plist->head = pentry;
616
617 pentry->prev = point->prev;
618 point->prev = pentry;
619 } else {
620 if (plist->tail)
621 plist->tail->next = pentry;
622 else
623 plist->head = pentry;
624
625 pentry->prev = plist->tail;
626 plist->tail = pentry;
627 }
718e3744 628
d62a17ae 629 prefix_list_trie_add(plist, pentry);
630
631 /* Increment count. */
632 plist->count++;
633
634 /* Run hook function. */
635 if (plist->master->add_hook)
636 (*plist->master->add_hook)(plist);
637
638 route_map_notify_dependencies(plist->name, RMAP_EVENT_PLIST_ADDED);
718e3744 639 plist->master->recent = plist;
718e3744 640}
641
642/* Return string of prefix_list_type. */
d62a17ae 643static const char *prefix_list_type_str(struct prefix_list_entry *pentry)
644{
645 switch (pentry->type) {
646 case PREFIX_PERMIT:
647 return "permit";
648 case PREFIX_DENY:
649 return "deny";
650 default:
651 return "";
652 }
653}
654
655static int prefix_list_entry_match(struct prefix_list_entry *pentry,
123214ef 656 const struct prefix *p)
d62a17ae 657{
658 int ret;
659
4f374985
DS
660 if (pentry->prefix.family != p->family)
661 return 0;
662
d62a17ae 663 ret = prefix_match(&pentry->prefix, p);
664 if (!ret)
665 return 0;
666
667 /* In case of le nor ge is specified, exact match is performed. */
668 if (!pentry->le && !pentry->ge) {
669 if (pentry->prefix.prefixlen != p->prefixlen)
670 return 0;
671 } else {
672 if (pentry->le)
673 if (p->prefixlen > pentry->le)
674 return 0;
675
676 if (pentry->ge)
677 if (p->prefixlen < pentry->ge)
678 return 0;
679 }
680 return 1;
681}
682
123214ef
MS
683enum prefix_list_type prefix_list_apply_which_prefix(
684 struct prefix_list *plist,
685 const struct prefix **which,
686 const void *object)
d62a17ae 687{
688 struct prefix_list_entry *pentry, *pbest = NULL;
689
123214ef
MS
690 const struct prefix *p = (const struct prefix *)object;
691 const uint8_t *byte = p->u.val;
d62a17ae 692 size_t depth;
693 size_t validbits = p->prefixlen;
694 struct pltrie_table *table;
695
ce94edc7
DS
696 if (plist == NULL) {
697 if (which)
698 *which = NULL;
d62a17ae 699 return PREFIX_DENY;
ce94edc7 700 }
d62a17ae 701
ce94edc7
DS
702 if (plist->count == 0) {
703 if (which)
704 *which = NULL;
d62a17ae 705 return PREFIX_PERMIT;
ce94edc7 706 }
d62a17ae 707
708 depth = plist->master->trie_depth;
709 table = plist->trie;
710 while (1) {
711 for (pentry = table->entries[*byte].up_chain; pentry;
712 pentry = pentry->next_best) {
713 if (pbest && pbest->seq < pentry->seq)
714 continue;
715 if (prefix_list_entry_match(pentry, p))
716 pbest = pentry;
717 }
718
719 if (validbits <= PLC_BITS)
720 break;
721 validbits -= PLC_BITS;
722
723 if (--depth) {
724 if (!table->entries[*byte].next_table)
725 break;
726
727 table = table->entries[*byte].next_table;
728 byte++;
729 continue;
730 }
731
732 for (pentry = table->entries[*byte].final_chain; pentry;
733 pentry = pentry->next_best) {
734 if (pbest && pbest->seq < pentry->seq)
735 continue;
736 if (prefix_list_entry_match(pentry, p))
737 pbest = pentry;
738 }
739 break;
740 }
741
ce94edc7
DS
742 if (which) {
743 if (pbest)
744 *which = &pbest->prefix;
745 else
746 *which = NULL;
747 }
748
d62a17ae 749 if (pbest == NULL)
750 return PREFIX_DENY;
751
0277936a 752 pbest->hitcnt++;
d62a17ae 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;
718e3744 922 }
718e3744 923
d62a17ae 924 /* If prefix has bits not under the mask, adjust it to fit */
925 if (!prefix_same(&p_tmp, &p)) {
926 char buf[PREFIX2STR_BUFFER];
927 char buf_tmp[PREFIX2STR_BUFFER];
928 prefix2str(&p, buf, sizeof(buf));
929 prefix2str(&p_tmp, buf_tmp, sizeof(buf_tmp));
0e411ce6
DS
930 vty_out(vty,
931 "%% Prefix-list %s prefix changed from %s to %s to match length\n",
d62a17ae 932 name, buf, buf_tmp);
ade6974d
QY
933 zlog_info(
934 "Prefix-list %s prefix changed from %s to %s to match length",
935 name, buf, buf_tmp);
d62a17ae 936 p = p_tmp;
718e3744 937 }
ddb13fd3 938
d62a17ae 939 /* ge and le check. */
940 if (genum && (genum <= p.prefixlen))
941 return vty_invalid_prefix_range(vty, prefix);
942
4015e918 943 if (lenum && (lenum < p.prefixlen))
d62a17ae 944 return vty_invalid_prefix_range(vty, prefix);
945
946 if (lenum && (genum > lenum))
947 return vty_invalid_prefix_range(vty, prefix);
948
949 if (genum && (lenum == (afi == AFI_IP ? 32 : 128)))
950 lenum = 0;
951
952 /* Get prefix_list with name. */
953 plist = prefix_list_get(afi, 0, name);
954
955 /* Make prefix entry. */
956 pentry = prefix_list_entry_make(&p, type, seqnum, lenum, genum, any);
957
958 /* Check same policy. */
959 dup = prefix_entry_dup_check(plist, pentry);
960
961 if (dup) {
962 prefix_list_entry_free(pentry);
963 return CMD_SUCCESS;
718e3744 964 }
718e3744 965
d62a17ae 966 /* Install new filter to the access_list. */
967 prefix_list_entry_add(plist, pentry);
968
969 return CMD_SUCCESS;
970}
971
972static int vty_prefix_list_uninstall(struct vty *vty, afi_t afi,
973 const char *name, const char *seq,
974 const char *typestr, const char *prefix,
975 const char *ge, const char *le)
976{
977 int ret;
978 enum prefix_list_type type;
979 struct prefix_list *plist;
980 struct prefix_list_entry *pentry;
981 struct prefix p;
24512fbd 982 int64_t seqnum = -1;
d62a17ae 983 int lenum = 0;
984 int genum = 0;
985
986 /* Check prefix list name. */
987 plist = prefix_list_lookup(afi, name);
988 if (!plist) {
989 vty_out(vty, "%% Can't find specified prefix-list\n");
990 return CMD_WARNING_CONFIG_FAILED;
718e3744 991 }
d62a17ae 992
993 /* Only prefix-list name specified, delete the entire prefix-list. */
994 if (seq == NULL && typestr == NULL && prefix == NULL && ge == NULL
995 && le == NULL) {
996 prefix_list_delete(plist);
997 return CMD_SUCCESS;
718e3744 998 }
718e3744 999
d62a17ae 1000 /* Check sequence number. */
1001 if (seq)
24512fbd 1002 seqnum = (int64_t)atol(seq);
d62a17ae 1003
7e200e86
DA
1004 /* Sequence number specified, but nothing else. */
1005 if (seq && typestr == NULL && prefix == NULL && ge == NULL
1006 && le == NULL) {
1007 pentry = prefix_seq_check(plist, seqnum);
1008
1009 if (pentry == NULL) {
1010 vty_out(vty,
9a8a7b0e 1011 "%% Can't find prefix-list %s with sequence number %" PRIu64 "\n",
7e200e86
DA
1012 name, seqnum);
1013 return CMD_WARNING_CONFIG_FAILED;
1014 }
1015
1016 prefix_list_entry_delete(plist, pentry, 1);
1017 return CMD_SUCCESS;
1018 }
1019
d62a17ae 1020 /* ge and le number */
1021 if (ge)
1022 genum = atoi(ge);
1023 if (le)
1024 lenum = atoi(le);
1025
7e200e86
DA
1026 /* We must have, at a minimum, both the type and prefix here */
1027 if ((typestr == NULL) || (prefix == NULL))
1028 return CMD_WARNING_CONFIG_FAILED;
1029
d62a17ae 1030 /* Check of filter type. */
1031 if (strncmp("permit", typestr, 1) == 0)
1032 type = PREFIX_PERMIT;
1033 else if (strncmp("deny", typestr, 1) == 0)
1034 type = PREFIX_DENY;
1035 else {
1036 vty_out(vty, "%% prefix type must be permit or deny\n");
1037 return CMD_WARNING_CONFIG_FAILED;
1038 }
718e3744 1039
d62a17ae 1040 /* "any" is special token for matching any IPv4 addresses. */
1041 if (afi == AFI_IP) {
1042 if (strncmp("any", prefix, strlen(prefix)) == 0) {
1043 ret = str2prefix_ipv4("0.0.0.0/0",
1044 (struct prefix_ipv4 *)&p);
1045 genum = 0;
1046 lenum = IPV4_MAX_BITLEN;
1047 } else
1048 ret = str2prefix_ipv4(prefix, (struct prefix_ipv4 *)&p);
1049
1050 if (ret <= 0) {
1051 vty_out(vty, "%% Malformed IPv4 prefix\n");
1052 return CMD_WARNING_CONFIG_FAILED;
1053 }
1054 } else if (afi == AFI_IP6) {
1055 if (strncmp("any", prefix, strlen(prefix)) == 0) {
1056 ret = str2prefix_ipv6("::/0", (struct prefix_ipv6 *)&p);
1057 genum = 0;
1058 lenum = IPV6_MAX_BITLEN;
1059 } else
1060 ret = str2prefix_ipv6(prefix, (struct prefix_ipv6 *)&p);
1061
1062 if (ret <= 0) {
1063 vty_out(vty, "%% Malformed IPv6 prefix\n");
1064 return CMD_WARNING_CONFIG_FAILED;
1065 }
1066 }
718e3744 1067
d62a17ae 1068 /* Lookup prefix entry. */
1069 pentry =
1070 prefix_list_entry_lookup(plist, &p, type, seqnum, lenum, genum);
718e3744 1071
d62a17ae 1072 if (pentry == NULL) {
1073 vty_out(vty, "%% Can't find specified prefix-list\n");
1074 return CMD_WARNING_CONFIG_FAILED;
1075 }
1076
1077 /* Install new filter to the access_list. */
1078 prefix_list_entry_delete(plist, pentry, 1);
1079
1080 return CMD_SUCCESS;
718e3744 1081}
1082
d62a17ae 1083static int vty_prefix_list_desc_unset(struct vty *vty, afi_t afi,
1084 const char *name)
718e3744 1085{
d62a17ae 1086 struct prefix_list *plist;
718e3744 1087
d62a17ae 1088 plist = prefix_list_lookup(afi, name);
1089 if (!plist) {
1090 vty_out(vty, "%% Can't find specified prefix-list\n");
1091 return CMD_WARNING_CONFIG_FAILED;
1092 }
718e3744 1093
e1b36e13 1094 XFREE(MTYPE_TMP, plist->desc);
718e3744 1095
d62a17ae 1096 if (plist->head == NULL && plist->tail == NULL && plist->desc == NULL)
1097 prefix_list_delete(plist);
718e3744 1098
d62a17ae 1099 return CMD_SUCCESS;
718e3744 1100}
1101
d62a17ae 1102enum display_type {
1103 normal_display,
1104 summary_display,
1105 detail_display,
1106 sequential_display,
1107 longer_display,
1108 first_match_display
718e3744 1109};
1110
d62a17ae 1111static void vty_show_prefix_entry(struct vty *vty, afi_t afi,
1112 struct prefix_list *plist,
1113 struct prefix_master *master,
1114 enum display_type dtype, int seqnum)
1115{
1116 struct prefix_list_entry *pentry;
1117
1118 /* Print the name of the protocol */
1119 vty_out(vty, "%s: ", frr_protoname);
1120
1121 if (dtype == normal_display) {
1122 vty_out(vty, "ip%s prefix-list %s: %d entries\n",
1123 afi == AFI_IP ? "" : "v6", plist->name, plist->count);
1124 if (plist->desc)
1125 vty_out(vty, " Description: %s\n", plist->desc);
1126 } else if (dtype == summary_display || dtype == detail_display) {
1127 vty_out(vty, "ip%s prefix-list %s:\n",
1128 afi == AFI_IP ? "" : "v6", plist->name);
1129
1130 if (plist->desc)
1131 vty_out(vty, " Description: %s\n", plist->desc);
1132
1133 vty_out(vty,
24512fbd 1134 " count: %d, range entries: %d, sequences: %" PRId64 " - %" PRId64 "\n",
d62a17ae 1135 plist->count, plist->rangecount,
1136 plist->head ? plist->head->seq : 0,
1137 plist->tail ? plist->tail->seq : 0);
718e3744 1138 }
718e3744 1139
d62a17ae 1140 if (dtype != summary_display) {
1141 for (pentry = plist->head; pentry; pentry = pentry->next) {
1142 if (dtype == sequential_display
1143 && pentry->seq != seqnum)
1144 continue;
718e3744 1145
d62a17ae 1146 vty_out(vty, " ");
718e3744 1147
d62a17ae 1148 if (master->seqnum)
24512fbd 1149 vty_out(vty, "seq %" PRId64 " ", pentry->seq);
718e3744 1150
d62a17ae 1151 vty_out(vty, "%s ", prefix_list_type_str(pentry));
718e3744 1152
d62a17ae 1153 if (pentry->any)
1154 vty_out(vty, "any");
1155 else {
1156 struct prefix *p = &pentry->prefix;
1157 char buf[BUFSIZ];
1158
1159 vty_out(vty, "%s/%d",
9eaec2ae 1160 inet_ntop(p->family, p->u.val, buf,
d62a17ae 1161 BUFSIZ),
1162 p->prefixlen);
1163
1164 if (pentry->ge)
1165 vty_out(vty, " ge %d", pentry->ge);
1166 if (pentry->le)
1167 vty_out(vty, " le %d", pentry->le);
1168 }
1169
1170 if (dtype == detail_display
1171 || dtype == sequential_display)
1172 vty_out(vty, " (hit count: %ld, refcount: %ld)",
1173 pentry->hitcnt, pentry->refcnt);
1174
1175 vty_out(vty, "\n");
1176 }
718e3744 1177 }
718e3744 1178}
1179
d62a17ae 1180static int vty_show_prefix_list(struct vty *vty, afi_t afi, const char *name,
1181 const char *seq, enum display_type dtype)
1182{
1183 struct prefix_list *plist;
1184 struct prefix_master *master;
24512fbd 1185 int64_t seqnum = 0;
d62a17ae 1186
1187 master = prefix_master_get(afi, 0);
1188 if (master == NULL)
1189 return CMD_WARNING;
1190
1191 if (seq)
24512fbd 1192 seqnum = (int64_t)atol(seq);
d62a17ae 1193
1194 if (name) {
1195 plist = prefix_list_lookup(afi, name);
1196 if (!plist) {
1197 vty_out(vty, "%% Can't find specified prefix-list\n");
1198 return CMD_WARNING;
1199 }
1200 vty_show_prefix_entry(vty, afi, plist, master, dtype, seqnum);
1201 } else {
1202 if (dtype == detail_display || dtype == summary_display) {
1203 if (master->recent)
1204 vty_out(vty,
1205 "Prefix-list with the last deletion/insertion: %s\n",
1206 master->recent->name);
1207 }
1208
1209 for (plist = master->num.head; plist; plist = plist->next)
1210 vty_show_prefix_entry(vty, afi, plist, master, dtype,
1211 seqnum);
1212
1213 for (plist = master->str.head; plist; plist = plist->next)
1214 vty_show_prefix_entry(vty, afi, plist, master, dtype,
1215 seqnum);
1216 }
718e3744 1217
d62a17ae 1218 return CMD_SUCCESS;
1219}
718e3744 1220
d62a17ae 1221static int vty_show_prefix_list_prefix(struct vty *vty, afi_t afi,
1222 const char *name, const char *prefix,
1223 enum display_type type)
1224{
1225 struct prefix_list *plist;
1226 struct prefix_list_entry *pentry;
1227 struct prefix p;
1228 int ret;
1229 int match;
718e3744 1230
d62a17ae 1231 plist = prefix_list_lookup(afi, name);
1232 if (!plist) {
1233 vty_out(vty, "%% Can't find specified prefix-list\n");
1234 return CMD_WARNING;
718e3744 1235 }
1236
d62a17ae 1237 ret = str2prefix(prefix, &p);
1238 if (ret <= 0) {
1239 vty_out(vty, "%% prefix is malformed\n");
1240 return CMD_WARNING;
718e3744 1241 }
1242
d62a17ae 1243 for (pentry = plist->head; pentry; pentry = pentry->next) {
1244 match = 0;
1245
1246 if (type == normal_display || type == first_match_display)
1247 if (prefix_same(&p, &pentry->prefix))
1248 match = 1;
1249
4f374985 1250 if (type == longer_display) {
996c9314
LB
1251 if ((p.family == pentry->prefix.family)
1252 && (prefix_match(&p, &pentry->prefix)))
d62a17ae 1253 match = 1;
4f374985 1254 }
d62a17ae 1255
1256 if (match) {
24512fbd 1257 vty_out(vty, " seq %" PRId64 " %s ", pentry->seq,
d62a17ae 1258 prefix_list_type_str(pentry));
1259
1260 if (pentry->any)
1261 vty_out(vty, "any");
1262 else {
7fe96307 1263 struct prefix *pf = &pentry->prefix;
d62a17ae 1264 char buf[BUFSIZ];
1265
1266 vty_out(vty, "%s/%d",
7fe96307 1267 inet_ntop(pf->family, pf->u.val, buf,
d62a17ae 1268 BUFSIZ),
7fe96307 1269 pf->prefixlen);
d62a17ae 1270
1271 if (pentry->ge)
1272 vty_out(vty, " ge %d", pentry->ge);
1273 if (pentry->le)
1274 vty_out(vty, " le %d", pentry->le);
1275 }
1276
1277 if (type == normal_display
1278 || type == first_match_display)
1279 vty_out(vty, " (hit count: %ld, refcount: %ld)",
1280 pentry->hitcnt, pentry->refcnt);
1281
1282 vty_out(vty, "\n");
1283
1284 if (type == first_match_display)
1285 return CMD_SUCCESS;
1286 }
718e3744 1287 }
d62a17ae 1288 return CMD_SUCCESS;
1289}
1290
1291static int vty_clear_prefix_list(struct vty *vty, afi_t afi, const char *name,
1292 const char *prefix)
1293{
1294 struct prefix_master *master;
1295 struct prefix_list *plist;
1296 struct prefix_list_entry *pentry;
1297 int ret;
1298 struct prefix p;
1299
1300 master = prefix_master_get(afi, 0);
1301 if (master == NULL)
1302 return CMD_WARNING;
1303
1304 if (name == NULL && prefix == NULL) {
1305 for (plist = master->num.head; plist; plist = plist->next)
1306 for (pentry = plist->head; pentry;
1307 pentry = pentry->next)
1308 pentry->hitcnt = 0;
1309
1310 for (plist = master->str.head; plist; plist = plist->next)
1311 for (pentry = plist->head; pentry;
1312 pentry = pentry->next)
1313 pentry->hitcnt = 0;
1314 } else {
1315 plist = prefix_list_lookup(afi, name);
1316 if (!plist) {
1317 vty_out(vty, "%% Can't find specified prefix-list\n");
1318 return CMD_WARNING;
1319 }
1320
1321 if (prefix) {
1322 ret = str2prefix(prefix, &p);
1323 if (ret <= 0) {
1324 vty_out(vty, "%% prefix is malformed\n");
1325 return CMD_WARNING;
1326 }
1327 }
1328
1329 for (pentry = plist->head; pentry; pentry = pentry->next) {
1330 if (prefix) {
996c9314
LB
1331 if (pentry->prefix.family == p.family
1332 && prefix_match(&pentry->prefix, &p))
d62a17ae 1333 pentry->hitcnt = 0;
1334 } else
1335 pentry->hitcnt = 0;
1336 }
1337 }
1338 return CMD_SUCCESS;
718e3744 1339}
6b0655a2 1340
6cdff321 1341#ifndef VTYSH_EXTRACT_PL
2e4c2296 1342#include "lib/plist_clippy.c"
6cdff321
DL
1343#endif
1344
1345DEFPY (ip_prefix_list,
1346 ip_prefix_list_cmd,
1347 "ip prefix-list WORD [seq (1-4294967295)] <deny|permit>$action <any$dest|A.B.C.D/M$dest [{ge (0-32)|le (0-32)}]>",
1348 IP_STR
718e3744 1349 PREFIX_LIST_STR
1350 "Name of a prefix list\n"
1351 "sequence number of an entry\n"
1352 "Sequence number\n"
1353 "Specify packets to reject\n"
1354 "Specify packets to forward\n"
6cdff321
DL
1355 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1356 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1357 "Minimum prefix length to be matched\n"
1358 "Minimum prefix length\n"
718e3744 1359 "Maximum prefix length to be matched\n"
1360 "Maximum prefix length\n")
1361{
d62a17ae 1362 return vty_prefix_list_install(vty, AFI_IP, prefix_list, seq_str,
1363 action, dest, ge_str, le_str);
718e3744 1364}
1365
6cdff321
DL
1366DEFPY (no_ip_prefix_list,
1367 no_ip_prefix_list_cmd,
1368 "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)}]>",
1369 NO_STR
1370 IP_STR
718e3744 1371 PREFIX_LIST_STR
1372 "Name of a prefix list\n"
1373 "sequence number of an entry\n"
1374 "Sequence number\n"
1375 "Specify packets to reject\n"
1376 "Specify packets to forward\n"
6cdff321
DL
1377 "Any prefix match. Same as \"0.0.0.0/0 le 32\"\n"
1378 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
718e3744 1379 "Minimum prefix length to be matched\n"
6cdff321
DL
1380 "Minimum prefix length\n"
1381 "Maximum prefix length to be matched\n"
1382 "Maximum prefix length\n")
718e3744 1383{
d62a17ae 1384 return vty_prefix_list_uninstall(vty, AFI_IP, prefix_list, seq_str,
1385 action, dest, ge_str, le_str);
718e3744 1386}
1387
7e200e86
DA
1388DEFPY(no_ip_prefix_list_seq, no_ip_prefix_list_seq_cmd,
1389 "no ip prefix-list WORD seq (1-4294967295)",
1390 NO_STR IP_STR PREFIX_LIST_STR
1391 "Name of a prefix list\n"
1392 "sequence number of an entry\n"
1393 "Sequence number\n")
1394{
1395 return vty_prefix_list_uninstall(vty, AFI_IP, prefix_list, seq_str,
1396 NULL, NULL, NULL, NULL);
1397}
1398
6cdff321
DL
1399DEFPY (no_ip_prefix_list_all,
1400 no_ip_prefix_list_all_cmd,
1401 "no ip prefix-list WORD",
718e3744 1402 NO_STR
6cdff321 1403 IP_STR
718e3744 1404 PREFIX_LIST_STR
1405 "Name of a prefix list\n")
1406{
d62a17ae 1407 return vty_prefix_list_uninstall(vty, AFI_IP, prefix_list, NULL, NULL,
1408 NULL, NULL, NULL);
718e3744 1409}
1410
6cdff321
DL
1411DEFPY (ip_prefix_list_sequence_number,
1412 ip_prefix_list_sequence_number_cmd,
1413 "[no] ip prefix-list sequence-number",
718e3744 1414 NO_STR
6cdff321 1415 IP_STR
718e3744 1416 PREFIX_LIST_STR
6cdff321 1417 "Include/exclude sequence numbers in NVGEN\n")
718e3744 1418{
e11d84ad 1419 prefix_master_ipv4.seqnum = no ? false : true;
d62a17ae 1420 return CMD_SUCCESS;
718e3744 1421}
1422
6cdff321
DL
1423DEFUN (ip_prefix_list_description,
1424 ip_prefix_list_description_cmd,
1425 "ip prefix-list WORD description LINE...",
1426 IP_STR
718e3744 1427 PREFIX_LIST_STR
1428 "Name of a prefix list\n"
6cdff321
DL
1429 "Prefix-list specific description\n"
1430 "Up to 80 characters describing this prefix-list\n")
718e3744 1431{
d62a17ae 1432 int idx_word = 2;
1433 int idx_line = 4;
1434 struct prefix_list *plist;
1435
1436 plist = prefix_list_get(AFI_IP, 0, argv[idx_word]->arg);
6cdff321 1437
d62a17ae 1438 if (plist->desc) {
1439 XFREE(MTYPE_TMP, plist->desc);
1440 plist->desc = NULL;
1441 }
1442 plist->desc = argv_concat(argv, argc, idx_line);
6cdff321 1443
d62a17ae 1444 return CMD_SUCCESS;
718e3744 1445}
1446
6cdff321
DL
1447DEFUN (no_ip_prefix_list_description,
1448 no_ip_prefix_list_description_cmd,
1449 "no ip prefix-list WORD description",
718e3744 1450 NO_STR
6cdff321 1451 IP_STR
718e3744 1452 PREFIX_LIST_STR
1453 "Name of a prefix list\n"
6cdff321 1454 "Prefix-list specific description\n")
718e3744 1455{
d62a17ae 1456 int idx_word = 3;
1457 return vty_prefix_list_desc_unset(vty, AFI_IP, argv[idx_word]->arg);
718e3744 1458}
1459
6cdff321
DL
1460/* ALIAS_FIXME */
1461DEFUN (no_ip_prefix_list_description_comment,
1462 no_ip_prefix_list_description_comment_cmd,
1463 "no ip prefix-list WORD description LINE...",
718e3744 1464 NO_STR
6cdff321 1465 IP_STR
718e3744 1466 PREFIX_LIST_STR
1467 "Name of a prefix list\n"
6cdff321
DL
1468 "Prefix-list specific description\n"
1469 "Up to 80 characters describing this prefix-list\n")
718e3744 1470{
d62a17ae 1471 return no_ip_prefix_list_description(self, vty, argc, argv);
718e3744 1472}
1473
6cdff321
DL
1474DEFPY (show_ip_prefix_list,
1475 show_ip_prefix_list_cmd,
1476 "show ip prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
1477 SHOW_STR
1478 IP_STR
718e3744 1479 PREFIX_LIST_STR
1480 "Name of a prefix list\n"
6cdff321
DL
1481 "sequence number of an entry\n"
1482 "Sequence number\n")
718e3744 1483{
d62a17ae 1484 enum display_type dtype = normal_display;
1485 if (dseq)
1486 dtype = sequential_display;
6cdff321 1487
d62a17ae 1488 return vty_show_prefix_list(vty, AFI_IP, prefix_list, arg_str, dtype);
718e3744 1489}
1490
6cdff321
DL
1491DEFPY (show_ip_prefix_list_prefix,
1492 show_ip_prefix_list_prefix_cmd,
1493 "show ip prefix-list WORD A.B.C.D/M$prefix [longer$dl|first-match$dfm]",
1494 SHOW_STR
1495 IP_STR
718e3744 1496 PREFIX_LIST_STR
1497 "Name of a prefix list\n"
6cdff321
DL
1498 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
1499 "Lookup longer prefix\n"
1500 "First matched prefix\n")
718e3744 1501{
d62a17ae 1502 enum display_type dtype = normal_display;
1503 if (dl)
1504 dtype = longer_display;
1505 else if (dfm)
1506 dtype = first_match_display;
6cdff321 1507
d62a17ae 1508 return vty_show_prefix_list_prefix(vty, AFI_IP, prefix_list, prefix_str,
1509 dtype);
718e3744 1510}
1511
6cdff321
DL
1512DEFPY (show_ip_prefix_list_summary,
1513 show_ip_prefix_list_summary_cmd,
1514 "show ip prefix-list summary [WORD$prefix_list]",
1515 SHOW_STR
1516 IP_STR
718e3744 1517 PREFIX_LIST_STR
6cdff321
DL
1518 "Summary of prefix lists\n"
1519 "Name of a prefix list\n")
718e3744 1520{
d62a17ae 1521 return vty_show_prefix_list(vty, AFI_IP, prefix_list, NULL,
1522 summary_display);
718e3744 1523}
1524
6cdff321
DL
1525DEFPY (show_ip_prefix_list_detail,
1526 show_ip_prefix_list_detail_cmd,
1527 "show ip prefix-list detail [WORD$prefix_list]",
1528 SHOW_STR
1529 IP_STR
1530 PREFIX_LIST_STR
1531 "Detail of prefix lists\n"
1532 "Name of a prefix list\n")
1533{
d62a17ae 1534 return vty_show_prefix_list(vty, AFI_IP, prefix_list, NULL,
1535 detail_display);
6cdff321
DL
1536}
1537
1538DEFPY (clear_ip_prefix_list,
1539 clear_ip_prefix_list_cmd,
1540 "clear ip prefix-list [WORD [A.B.C.D/M$prefix]]",
1541 CLEAR_STR
1542 IP_STR
718e3744 1543 PREFIX_LIST_STR
1544 "Name of a prefix list\n"
6cdff321 1545 "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n")
718e3744 1546{
d62a17ae 1547 return vty_clear_prefix_list(vty, AFI_IP, prefix_list, prefix_str);
6cdff321
DL
1548}
1549
1550DEFPY (ipv6_prefix_list,
1551 ipv6_prefix_list_cmd,
1552 "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 IPV6_STR
1554 PREFIX_LIST_STR
1555 "Name of a prefix list\n"
1556 "sequence number of an entry\n"
1557 "Sequence number\n"
1558 "Specify packets to reject\n"
1559 "Specify packets to forward\n"
6cdff321 1560 "Any prefix match. Same as \"::0/0 le 128\"\n"
718e3744 1561 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1562 "Maximum prefix length to be matched\n"
6cdff321
DL
1563 "Maximum prefix length\n"
1564 "Minimum prefix length to be matched\n"
1565 "Minimum prefix length\n")
718e3744 1566{
d62a17ae 1567 return vty_prefix_list_install(vty, AFI_IP6, prefix_list, seq_str,
1568 action, dest, ge_str, le_str);
718e3744 1569}
1570
6cdff321
DL
1571DEFPY (no_ipv6_prefix_list,
1572 no_ipv6_prefix_list_cmd,
1573 "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 1574 NO_STR
1575 IPV6_STR
1576 PREFIX_LIST_STR
1577 "Name of a prefix list\n"
1578 "sequence number of an entry\n"
1579 "Sequence number\n"
1580 "Specify packets to reject\n"
1581 "Specify packets to forward\n"
6cdff321 1582 "Any prefix match. Same as \"::0/0 le 128\"\n"
718e3744 1583 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
1584 "Maximum prefix length to be matched\n"
1585 "Maximum prefix length\n"
1586 "Minimum prefix length to be matched\n"
1587 "Minimum prefix length\n")
1588{
d62a17ae 1589 return vty_prefix_list_uninstall(vty, AFI_IP6, prefix_list, seq_str,
1590 action, dest, ge_str, le_str);
718e3744 1591}
1592
6cdff321
DL
1593DEFPY (no_ipv6_prefix_list_all,
1594 no_ipv6_prefix_list_all_cmd,
1595 "no ipv6 prefix-list WORD",
1596 NO_STR
718e3744 1597 IPV6_STR
1598 PREFIX_LIST_STR
6cdff321 1599 "Name of a prefix list\n")
718e3744 1600{
d62a17ae 1601 return vty_prefix_list_uninstall(vty, AFI_IP6, prefix_list, NULL, NULL,
1602 NULL, NULL, NULL);
718e3744 1603}
1604
6cdff321
DL
1605DEFPY (ipv6_prefix_list_sequence_number,
1606 ipv6_prefix_list_sequence_number_cmd,
1607 "[no] ipv6 prefix-list sequence-number",
718e3744 1608 NO_STR
1609 IPV6_STR
1610 PREFIX_LIST_STR
1611 "Include/exclude sequence numbers in NVGEN\n")
1612{
e11d84ad 1613 prefix_master_ipv6.seqnum = no ? false : true;
d62a17ae 1614 return CMD_SUCCESS;
718e3744 1615}
1616
1617DEFUN (ipv6_prefix_list_description,
1618 ipv6_prefix_list_description_cmd,
e961923c 1619 "ipv6 prefix-list WORD description LINE...",
718e3744 1620 IPV6_STR
1621 PREFIX_LIST_STR
1622 "Name of a prefix list\n"
1623 "Prefix-list specific description\n"
1624 "Up to 80 characters describing this prefix-list\n")
1625{
d62a17ae 1626 int idx_word = 2;
1627 int iddx_line = 4;
1628 struct prefix_list *plist;
718e3744 1629
d62a17ae 1630 plist = prefix_list_get(AFI_IP6, 0, argv[idx_word]->arg);
1631
1632 if (plist->desc) {
1633 XFREE(MTYPE_TMP, plist->desc);
1634 plist->desc = NULL;
1635 }
1636 plist->desc = argv_concat(argv, argc, iddx_line);
718e3744 1637
d62a17ae 1638 return CMD_SUCCESS;
c349116d 1639}
718e3744 1640
1641DEFUN (no_ipv6_prefix_list_description,
1642 no_ipv6_prefix_list_description_cmd,
1643 "no ipv6 prefix-list WORD description",
1644 NO_STR
1645 IPV6_STR
1646 PREFIX_LIST_STR
1647 "Name of a prefix list\n"
1648 "Prefix-list specific description\n")
1649{
d62a17ae 1650 int idx_word = 3;
1651 return vty_prefix_list_desc_unset(vty, AFI_IP6, argv[idx_word]->arg);
718e3744 1652}
1653
d862bffb
QY
1654/* ALIAS_FIXME */
1655DEFUN (no_ipv6_prefix_list_description_comment,
1656 no_ipv6_prefix_list_description_comment_cmd,
1657 "no ipv6 prefix-list WORD description LINE...",
1658 NO_STR
1659 IPV6_STR
1660 PREFIX_LIST_STR
1661 "Name of a prefix list\n"
1662 "Prefix-list specific description\n"
1663 "Up to 80 characters describing this prefix-list\n")
1664{
d62a17ae 1665 return no_ipv6_prefix_list_description(self, vty, argc, argv);
d862bffb
QY
1666}
1667
718e3744 1668
6cdff321 1669DEFPY (show_ipv6_prefix_list,
718e3744 1670 show_ipv6_prefix_list_cmd,
6cdff321 1671 "show ipv6 prefix-list [WORD [seq$dseq (1-4294967295)$arg]]",
718e3744 1672 SHOW_STR
1673 IPV6_STR
1674 PREFIX_LIST_STR
1675 "Name of a prefix list\n"
1676 "sequence number of an entry\n"
7111c1a0 1677 "Sequence number\n")
718e3744 1678{
d62a17ae 1679 enum display_type dtype = normal_display;
1680 if (dseq)
1681 dtype = sequential_display;
718e3744 1682
d62a17ae 1683 return vty_show_prefix_list(vty, AFI_IP6, prefix_list, arg_str, dtype);
718e3744 1684}
1685
6cdff321
DL
1686DEFPY (show_ipv6_prefix_list_prefix,
1687 show_ipv6_prefix_list_prefix_cmd,
1688 "show ipv6 prefix-list WORD X:X::X:X/M$prefix [longer$dl|first-match$dfm]",
718e3744 1689 SHOW_STR
1690 IPV6_STR
1691 PREFIX_LIST_STR
1692 "Name of a prefix list\n"
1693 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n"
6cdff321 1694 "Lookup longer prefix\n"
718e3744 1695 "First matched prefix\n")
1696{
d62a17ae 1697 enum display_type dtype = normal_display;
1698 if (dl)
1699 dtype = longer_display;
1700 else if (dfm)
1701 dtype = first_match_display;
718e3744 1702
d62a17ae 1703 return vty_show_prefix_list_prefix(vty, AFI_IP6, prefix_list,
1704 prefix_str, dtype);
718e3744 1705}
1706
6cdff321
DL
1707DEFPY (show_ipv6_prefix_list_summary,
1708 show_ipv6_prefix_list_summary_cmd,
1709 "show ipv6 prefix-list summary [WORD$prefix-list]",
718e3744 1710 SHOW_STR
1711 IPV6_STR
1712 PREFIX_LIST_STR
1713 "Summary of prefix lists\n"
1714 "Name of a prefix list\n")
1715{
d62a17ae 1716 return vty_show_prefix_list(vty, AFI_IP6, prefix_list, NULL,
1717 summary_display);
718e3744 1718}
1719
6cdff321 1720DEFPY (show_ipv6_prefix_list_detail,
718e3744 1721 show_ipv6_prefix_list_detail_cmd,
6cdff321 1722 "show ipv6 prefix-list detail [WORD$prefix-list]",
718e3744 1723 SHOW_STR
1724 IPV6_STR
1725 PREFIX_LIST_STR
1726 "Detail of prefix lists\n"
1727 "Name of a prefix list\n")
1728{
d62a17ae 1729 return vty_show_prefix_list(vty, AFI_IP6, prefix_list, NULL,
1730 detail_display);
718e3744 1731}
1732
6cdff321 1733DEFPY (clear_ipv6_prefix_list,
718e3744 1734 clear_ipv6_prefix_list_cmd,
6cdff321 1735 "clear ipv6 prefix-list [WORD [X:X::X:X/M$prefix]]",
718e3744 1736 CLEAR_STR
1737 IPV6_STR
1738 PREFIX_LIST_STR
1739 "Name of a prefix list\n"
1740 "IPv6 prefix <network>/<length>, e.g., 3ffe::/16\n")
1741{
d62a17ae 1742 return vty_clear_prefix_list(vty, AFI_IP6, prefix_list, prefix_str);
718e3744 1743}
6b0655a2 1744
718e3744 1745/* Configuration write function. */
d62a17ae 1746static int config_write_prefix_afi(afi_t afi, struct vty *vty)
1747{
1748 struct prefix_list *plist;
1749 struct prefix_list_entry *pentry;
1750 struct prefix_master *master;
1751 int write = 0;
1752
1753 master = prefix_master_get(afi, 0);
1754 if (master == NULL)
1755 return 0;
718e3744 1756
d62a17ae 1757 if (!master->seqnum) {
1758 vty_out(vty, "no ip%s prefix-list sequence-number\n",
1759 afi == AFI_IP ? "" : "v6");
1760 vty_out(vty, "!\n");
718e3744 1761 }
d62a17ae 1762
1763 for (plist = master->num.head; plist; plist = plist->next) {
1764 if (plist->desc) {
1765 vty_out(vty, "ip%s prefix-list %s description %s\n",
1766 afi == AFI_IP ? "" : "v6", plist->name,
1767 plist->desc);
1768 write++;
1769 }
1770
1771 for (pentry = plist->head; pentry; pentry = pentry->next) {
1772 vty_out(vty, "ip%s prefix-list %s ",
1773 afi == AFI_IP ? "" : "v6", plist->name);
1774
1775 if (master->seqnum)
24512fbd 1776 vty_out(vty, "seq %" PRId64 " ", pentry->seq);
d62a17ae 1777
1778 vty_out(vty, "%s ", prefix_list_type_str(pentry));
1779
1780 if (pentry->any)
1781 vty_out(vty, "any");
1782 else {
1783 struct prefix *p = &pentry->prefix;
1784 char buf[BUFSIZ];
1785
1786 vty_out(vty, "%s/%d",
9eaec2ae 1787 inet_ntop(p->family, p->u.val, buf,
d62a17ae 1788 BUFSIZ),
1789 p->prefixlen);
1790
1791 if (pentry->ge)
1792 vty_out(vty, " ge %d", pentry->ge);
1793 if (pentry->le)
1794 vty_out(vty, " le %d", pentry->le);
1795 }
1796 vty_out(vty, "\n");
1797 write++;
1798 }
1799 /* vty_out (vty, "!\n"); */
718e3744 1800 }
1801
d62a17ae 1802 for (plist = master->str.head; plist; plist = plist->next) {
1803 if (plist->desc) {
1804 vty_out(vty, "ip%s prefix-list %s description %s\n",
1805 afi == AFI_IP ? "" : "v6", plist->name,
1806 plist->desc);
1807 write++;
1808 }
1809
1810 for (pentry = plist->head; pentry; pentry = pentry->next) {
1811 vty_out(vty, "ip%s prefix-list %s ",
1812 afi == AFI_IP ? "" : "v6", plist->name);
1813
1814 if (master->seqnum)
24512fbd 1815 vty_out(vty, "seq %" PRId64 " ", pentry->seq);
d62a17ae 1816
1817 vty_out(vty, "%s", prefix_list_type_str(pentry));
1818
1819 if (pentry->any)
1820 vty_out(vty, " any");
1821 else {
1822 struct prefix *p = &pentry->prefix;
1823 char buf[BUFSIZ];
1824
1825 vty_out(vty, " %s/%d",
9eaec2ae 1826 inet_ntop(p->family, p->u.val, buf,
d62a17ae 1827 BUFSIZ),
1828 p->prefixlen);
1829
1830 if (pentry->ge)
1831 vty_out(vty, " ge %d", pentry->ge);
1832 if (pentry->le)
1833 vty_out(vty, " le %d", pentry->le);
1834 }
1835 vty_out(vty, "\n");
1836 write++;
1837 }
718e3744 1838 }
d62a17ae 1839
1840 return write;
718e3744 1841}
1842
d62a17ae 1843struct stream *prefix_bgp_orf_entry(struct stream *s, struct prefix_list *plist,
d7c0a89a
QY
1844 uint8_t init_flag, uint8_t permit_flag,
1845 uint8_t deny_flag)
718e3744 1846{
d62a17ae 1847 struct prefix_list_entry *pentry;
718e3744 1848
d62a17ae 1849 if (!plist)
1850 return s;
718e3744 1851
d62a17ae 1852 for (pentry = plist->head; pentry; pentry = pentry->next) {
d7c0a89a 1853 uint8_t flag = init_flag;
d62a17ae 1854 struct prefix *p = &pentry->prefix;
718e3744 1855
d62a17ae 1856 flag |= (pentry->type == PREFIX_PERMIT ? permit_flag
1857 : deny_flag);
1858 stream_putc(s, flag);
d7c0a89a
QY
1859 stream_putl(s, (uint32_t)pentry->seq);
1860 stream_putc(s, (uint8_t)pentry->ge);
1861 stream_putc(s, (uint8_t)pentry->le);
d62a17ae 1862 stream_put_prefix(s, p);
1863 }
718e3744 1864
d62a17ae 1865 return s;
718e3744 1866}
1867
d62a17ae 1868int prefix_bgp_orf_set(char *name, afi_t afi, struct orf_prefix *orfp,
1869 int permit, int set)
718e3744 1870{
d62a17ae 1871 struct prefix_list *plist;
1872 struct prefix_list_entry *pentry;
718e3744 1873
d62a17ae 1874 /* ge and le value check */
1875 if (orfp->ge && orfp->ge <= orfp->p.prefixlen)
1876 return CMD_WARNING_CONFIG_FAILED;
1877 if (orfp->le && orfp->le <= orfp->p.prefixlen)
1878 return CMD_WARNING_CONFIG_FAILED;
1879 if (orfp->le && orfp->ge > orfp->le)
1880 return CMD_WARNING_CONFIG_FAILED;
718e3744 1881
d62a17ae 1882 if (orfp->ge && orfp->le == (afi == AFI_IP ? 32 : 128))
1883 orfp->le = 0;
718e3744 1884
d62a17ae 1885 plist = prefix_list_get(afi, 1, name);
1886 if (!plist)
1887 return CMD_WARNING_CONFIG_FAILED;
718e3744 1888
3dbedfbd
QY
1889 apply_mask(&orfp->p);
1890
d62a17ae 1891 if (set) {
1892 pentry = prefix_list_entry_make(
1893 &orfp->p, (permit ? PREFIX_PERMIT : PREFIX_DENY),
1894 orfp->seq, orfp->le, orfp->ge, 0);
718e3744 1895
d62a17ae 1896 if (prefix_entry_dup_check(plist, pentry)) {
1897 prefix_list_entry_free(pentry);
1898 return CMD_WARNING_CONFIG_FAILED;
1899 }
718e3744 1900
d62a17ae 1901 prefix_list_entry_add(plist, pentry);
1902 } else {
1903 pentry = prefix_list_entry_lookup(
1904 plist, &orfp->p, (permit ? PREFIX_PERMIT : PREFIX_DENY),
1905 orfp->seq, orfp->le, orfp->ge);
718e3744 1906
d62a17ae 1907 if (!pentry)
1908 return CMD_WARNING_CONFIG_FAILED;
718e3744 1909
d62a17ae 1910 prefix_list_entry_delete(plist, pentry, 1);
1911 }
718e3744 1912
d62a17ae 1913 return CMD_SUCCESS;
718e3744 1914}
1915
d62a17ae 1916void prefix_bgp_orf_remove_all(afi_t afi, char *name)
718e3744 1917{
d62a17ae 1918 struct prefix_list *plist;
718e3744 1919
d62a17ae 1920 plist = prefix_bgp_orf_lookup(afi, name);
1921 if (plist)
1922 prefix_list_delete(plist);
718e3744 1923}
1924
1925/* return prefix count */
d62a17ae 1926int prefix_bgp_show_prefix_list(struct vty *vty, afi_t afi, char *name,
9f049418 1927 bool use_json)
d62a17ae 1928{
1929 struct prefix_list *plist;
1930 struct prefix_list_entry *pentry;
1931 json_object *json = NULL;
1932 json_object *json_prefix = NULL;
1933 json_object *json_list = NULL;
1934
1935 plist = prefix_bgp_orf_lookup(afi, name);
1936 if (!plist)
1937 return 0;
1938
1939 if (!vty)
1940 return plist->count;
1941
1942 if (use_json) {
1943 json = json_object_new_object();
1944 json_prefix = json_object_new_object();
1945 json_list = json_object_new_object();
1946
1947 json_object_int_add(json_prefix, "prefixListCounter",
1948 plist->count);
1949 json_object_string_add(json_prefix, "prefixListName",
1950 plist->name);
1951
1952 for (pentry = plist->head; pentry; pentry = pentry->next) {
1953 struct prefix *p = &pentry->prefix;
1954 char buf_a[BUFSIZ];
1955 char buf_b[BUFSIZ];
1956
1957 sprintf(buf_a, "%s/%d",
9eaec2ae 1958 inet_ntop(p->family, p->u.val, buf_b,
d62a17ae 1959 BUFSIZ),
1960 p->prefixlen);
1961
1962 json_object_int_add(json_list, "seq", pentry->seq);
1963 json_object_string_add(json_list, "seqPrefixListType",
1964 prefix_list_type_str(pentry));
1965
1966 if (pentry->ge)
1967 json_object_int_add(json_list, "ge",
1968 pentry->ge);
1969 if (pentry->le)
1970 json_object_int_add(json_list, "le",
1971 pentry->le);
1972
1973 json_object_object_add(json_prefix, buf_a, json_list);
1974 }
1975 if (afi == AFI_IP)
1976 json_object_object_add(json, "ipPrefixList",
1977 json_prefix);
1978 else
1979 json_object_object_add(json, "ipv6PrefixList",
1980 json_prefix);
1981
9d303b37
DL
1982 vty_out(vty, "%s\n", json_object_to_json_string_ext(
1983 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 1984 json_object_free(json);
1985 } else {
1986 vty_out(vty, "ip%s prefix-list %s: %d entries\n",
1987 afi == AFI_IP ? "" : "v6", plist->name, plist->count);
1988
1989 for (pentry = plist->head; pentry; pentry = pentry->next) {
1990 struct prefix *p = &pentry->prefix;
1991 char buf[BUFSIZ];
1992
24512fbd
DS
1993 vty_out(vty, " seq %" PRId64 " %s %s/%d",
1994 pentry->seq,
d62a17ae 1995 prefix_list_type_str(pentry),
9eaec2ae 1996 inet_ntop(p->family, p->u.val, buf, BUFSIZ),
d62a17ae 1997 p->prefixlen);
1998
1999 if (pentry->ge)
2000 vty_out(vty, " ge %d", pentry->ge);
2001 if (pentry->le)
2002 vty_out(vty, " le %d", pentry->le);
2003
2004 vty_out(vty, "\n");
2005 }
2006 }
2007 return plist->count;
2008}
2009
2010static void prefix_list_reset_afi(afi_t afi, int orf)
2011{
2012 struct prefix_list *plist;
2013 struct prefix_list *next;
2014 struct prefix_master *master;
2015
2016 master = prefix_master_get(afi, orf);
2017 if (master == NULL)
2018 return;
2019
2020 for (plist = master->num.head; plist; plist = next) {
2021 next = plist->next;
2022 prefix_list_delete(plist);
2023 }
2024 for (plist = master->str.head; plist; plist = next) {
2025 next = plist->next;
2026 prefix_list_delete(plist);
2027 }
2028
2029 assert(master->num.head == NULL);
2030 assert(master->num.tail == NULL);
2031
2032 assert(master->str.head == NULL);
2033 assert(master->str.tail == NULL);
2034
2035 master->seqnum = 1;
2036 master->recent = NULL;
718e3744 2037}
2038
2039
2040/* Prefix-list node. */
d62a17ae 2041static struct cmd_node prefix_node = {PREFIX_NODE,
2042 "", /* Prefix list has no interface. */
2043 1};
718e3744 2044
d62a17ae 2045static int config_write_prefix_ipv4(struct vty *vty)
718e3744 2046{
d62a17ae 2047 return config_write_prefix_afi(AFI_IP, vty);
718e3744 2048}
2049
d62a17ae 2050static void plist_autocomplete_afi(afi_t afi, vector comps,
2051 struct cmd_token *token)
70d44c5c 2052{
d62a17ae 2053 struct prefix_list *plist;
2054 struct prefix_master *master;
70d44c5c 2055
d62a17ae 2056 master = prefix_master_get(afi, 0);
2057 if (master == NULL)
2058 return;
70d44c5c 2059
d62a17ae 2060 for (plist = master->str.head; plist; plist = plist->next)
2061 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, plist->name));
2062 for (plist = master->num.head; plist; plist = plist->next)
2063 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, plist->name));
70d44c5c
DL
2064}
2065
d62a17ae 2066static void plist_autocomplete(vector comps, struct cmd_token *token)
70d44c5c 2067{
d62a17ae 2068 plist_autocomplete_afi(AFI_IP, comps, token);
2069 plist_autocomplete_afi(AFI_IP6, comps, token);
70d44c5c
DL
2070}
2071
2072static const struct cmd_variable_handler plist_var_handlers[] = {
d62a17ae 2073 {/* "prefix-list WORD" */
2074 .varname = "prefix_list",
2075 .completions = plist_autocomplete},
2076 {.completions = NULL}};
70d44c5c
DL
2077
2078
d62a17ae 2079static void prefix_list_init_ipv4(void)
718e3744 2080{
d62a17ae 2081 install_node(&prefix_node, config_write_prefix_ipv4);
718e3744 2082
d62a17ae 2083 install_element(CONFIG_NODE, &ip_prefix_list_cmd);
2084 install_element(CONFIG_NODE, &no_ip_prefix_list_cmd);
7e200e86 2085 install_element(CONFIG_NODE, &no_ip_prefix_list_seq_cmd);
d62a17ae 2086 install_element(CONFIG_NODE, &no_ip_prefix_list_all_cmd);
718e3744 2087
d62a17ae 2088 install_element(CONFIG_NODE, &ip_prefix_list_description_cmd);
2089 install_element(CONFIG_NODE, &no_ip_prefix_list_description_cmd);
2090 install_element(CONFIG_NODE,
2091 &no_ip_prefix_list_description_comment_cmd);
718e3744 2092
d62a17ae 2093 install_element(CONFIG_NODE, &ip_prefix_list_sequence_number_cmd);
718e3744 2094
d62a17ae 2095 install_element(VIEW_NODE, &show_ip_prefix_list_cmd);
2096 install_element(VIEW_NODE, &show_ip_prefix_list_prefix_cmd);
2097 install_element(VIEW_NODE, &show_ip_prefix_list_summary_cmd);
2098 install_element(VIEW_NODE, &show_ip_prefix_list_detail_cmd);
718e3744 2099
d62a17ae 2100 install_element(ENABLE_NODE, &clear_ip_prefix_list_cmd);
718e3744 2101}
2102
718e3744 2103/* Prefix-list node. */
d62a17ae 2104static struct cmd_node prefix_ipv6_node = {
2105 PREFIX_IPV6_NODE, "", /* Prefix list has no interface. */
2106 1};
718e3744 2107
d62a17ae 2108static int config_write_prefix_ipv6(struct vty *vty)
718e3744 2109{
d62a17ae 2110 return config_write_prefix_afi(AFI_IP6, vty);
718e3744 2111}
2112
d62a17ae 2113static void prefix_list_init_ipv6(void)
718e3744 2114{
d62a17ae 2115 install_node(&prefix_ipv6_node, config_write_prefix_ipv6);
718e3744 2116
d62a17ae 2117 install_element(CONFIG_NODE, &ipv6_prefix_list_cmd);
2118 install_element(CONFIG_NODE, &no_ipv6_prefix_list_cmd);
2119 install_element(CONFIG_NODE, &no_ipv6_prefix_list_all_cmd);
718e3744 2120
d62a17ae 2121 install_element(CONFIG_NODE, &ipv6_prefix_list_description_cmd);
2122 install_element(CONFIG_NODE, &no_ipv6_prefix_list_description_cmd);
2123 install_element(CONFIG_NODE,
2124 &no_ipv6_prefix_list_description_comment_cmd);
718e3744 2125
d62a17ae 2126 install_element(CONFIG_NODE, &ipv6_prefix_list_sequence_number_cmd);
718e3744 2127
d62a17ae 2128 install_element(VIEW_NODE, &show_ipv6_prefix_list_cmd);
2129 install_element(VIEW_NODE, &show_ipv6_prefix_list_prefix_cmd);
2130 install_element(VIEW_NODE, &show_ipv6_prefix_list_summary_cmd);
2131 install_element(VIEW_NODE, &show_ipv6_prefix_list_detail_cmd);
718e3744 2132
d62a17ae 2133 install_element(ENABLE_NODE, &clear_ipv6_prefix_list_cmd);
718e3744 2134}
718e3744 2135
4d762f26 2136void prefix_list_init(void)
718e3744 2137{
d62a17ae 2138 cmd_variable_handler_register(plist_var_handlers);
70d44c5c 2139
d62a17ae 2140 prefix_list_init_ipv4();
2141 prefix_list_init_ipv6();
718e3744 2142}
2143
4d762f26 2144void prefix_list_reset(void)
718e3744 2145{
d62a17ae 2146 prefix_list_reset_afi(AFI_IP, 0);
2147 prefix_list_reset_afi(AFI_IP6, 0);
2148 prefix_list_reset_afi(AFI_IP, 1);
2149 prefix_list_reset_afi(AFI_IP6, 1);
718e3744 2150}