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