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