]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_clist.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / bgpd / bgp_clist.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP community-list and extcommunity-list.
3 * Copyright (C) 1999 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #include "command.h"
9 #include "prefix.h"
10 #include "memory.h"
11 #include "queue.h"
12 #include "filter.h"
13 #include "stream.h"
14 #include "jhash.h"
15 #include "frrstr.h"
16
17 #include "bgpd/bgpd.h"
18 #include "bgpd/bgp_community.h"
19 #include "bgpd/bgp_ecommunity.h"
20 #include "bgpd/bgp_lcommunity.h"
21 #include "bgpd/bgp_community_alias.h"
22 #include "bgpd/bgp_aspath.h"
23 #include "bgpd/bgp_regex.h"
24 #include "bgpd/bgp_clist.h"
25
26 /* Calculate new sequential number. */
27 static int64_t bgp_clist_new_seq_get(struct community_list *list)
28 {
29 int64_t maxseq;
30 int64_t newseq;
31 struct community_entry *entry;
32
33 maxseq = 0;
34
35 for (entry = list->head; entry; entry = entry->next) {
36 if (maxseq < entry->seq)
37 maxseq = entry->seq;
38 }
39
40 newseq = ((maxseq / 5) * 5) + 5;
41
42 return (newseq > UINT_MAX) ? UINT_MAX : newseq;
43 }
44
45 /* Return community-list entry which has same seq number. */
46 static struct community_entry *bgp_clist_seq_check(struct community_list *list,
47 int64_t seq)
48 {
49 struct community_entry *entry;
50
51 for (entry = list->head; entry; entry = entry->next)
52 if (entry->seq == seq)
53 return entry;
54 return NULL;
55 }
56
57 static uint32_t bgp_clist_hash_key_community_list(const void *data)
58 {
59 struct community_list *cl = (struct community_list *) data;
60
61 if (cl->name_hash)
62 return cl->name_hash;
63
64 cl->name_hash = bgp_clist_hash_key(cl->name);
65 return cl->name_hash;
66 }
67
68 static bool bgp_clist_hash_cmp_community_list(const void *a1, const void *a2)
69 {
70 const struct community_list *cl1 = a1;
71 const struct community_list *cl2 = a2;
72
73 if (cl1->name_hash != cl2->name_hash)
74 return false;
75
76 if (strcmp(cl1->name, cl2->name) == 0)
77 return true;
78
79 return false;
80 }
81
82 /* Lookup master structure for community-list or
83 extcommunity-list. */
84 struct community_list_master *
85 community_list_master_lookup(struct community_list_handler *ch, int master)
86 {
87 if (ch)
88 switch (master) {
89 case COMMUNITY_LIST_MASTER:
90 return &ch->community_list;
91 case EXTCOMMUNITY_LIST_MASTER:
92 return &ch->extcommunity_list;
93 case LARGE_COMMUNITY_LIST_MASTER:
94 return &ch->lcommunity_list;
95 }
96 return NULL;
97 }
98
99 /* Allocate a new community list entry. */
100 static struct community_entry *community_entry_new(void)
101 {
102 return XCALLOC(MTYPE_COMMUNITY_LIST_ENTRY,
103 sizeof(struct community_entry));
104 }
105
106 /* Free community list entry. */
107 static void community_entry_free(struct community_entry *entry)
108 {
109 switch (entry->style) {
110 case COMMUNITY_LIST_STANDARD:
111 if (entry->u.com)
112 community_free(&entry->u.com);
113 break;
114 case LARGE_COMMUNITY_LIST_STANDARD:
115 if (entry->u.lcom)
116 lcommunity_free(&entry->u.lcom);
117 break;
118 case EXTCOMMUNITY_LIST_STANDARD:
119 /* In case of standard extcommunity-list, configuration string
120 is made by ecommunity_ecom2str(). */
121 XFREE(MTYPE_ECOMMUNITY_STR, entry->config);
122 if (entry->u.ecom)
123 ecommunity_free(&entry->u.ecom);
124 break;
125 case COMMUNITY_LIST_EXPANDED:
126 case EXTCOMMUNITY_LIST_EXPANDED:
127 case LARGE_COMMUNITY_LIST_EXPANDED:
128 XFREE(MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
129 if (entry->reg)
130 bgp_regex_free(entry->reg);
131 default:
132 break;
133 }
134 XFREE(MTYPE_COMMUNITY_LIST_ENTRY, entry);
135 }
136
137 /* Allocate a new community-list. */
138 static struct community_list *community_list_new(void)
139 {
140 return XCALLOC(MTYPE_COMMUNITY_LIST, sizeof(struct community_list));
141 }
142
143 /* Free community-list. */
144 static void community_list_free(struct community_list *list)
145 {
146 XFREE(MTYPE_COMMUNITY_LIST_NAME, list->name);
147 XFREE(MTYPE_COMMUNITY_LIST, list);
148 }
149
150 static struct community_list *
151 community_list_insert(struct community_list_handler *ch, const char *name,
152 int master)
153 {
154 size_t i;
155 long number;
156 struct community_list *new;
157 struct community_list *point;
158 struct community_list_list *list;
159 struct community_list_master *cm;
160
161 /* Lookup community-list master. */
162 cm = community_list_master_lookup(ch, master);
163 if (!cm)
164 return NULL;
165
166 /* Allocate new community_list and copy given name. */
167 new = community_list_new();
168 new->name = XSTRDUP(MTYPE_COMMUNITY_LIST_NAME, name);
169 new->name_hash = bgp_clist_hash_key_community_list(new);
170
171 /* Save for later */
172 (void)hash_get(cm->hash, new, hash_alloc_intern);
173
174 /* If name is made by all digit character. We treat it as
175 number. */
176 for (number = 0, i = 0; i < strlen(name); i++) {
177 if (isdigit((unsigned char)name[i]))
178 number = (number * 10) + (name[i] - '0');
179 else
180 break;
181 }
182
183 /* In case of name is all digit character */
184 if (i == strlen(name)) {
185 new->sort = COMMUNITY_LIST_NUMBER;
186
187 /* Set access_list to number list. */
188 list = &cm->num;
189
190 for (point = list->head; point; point = point->next)
191 if (atol(point->name) >= number)
192 break;
193 } else {
194 new->sort = COMMUNITY_LIST_STRING;
195
196 /* Set access_list to string list. */
197 list = &cm->str;
198
199 /* Set point to insertion point. */
200 for (point = list->head; point; point = point->next)
201 if (strcmp(point->name, name) >= 0)
202 break;
203 }
204
205 /* Link to upper list. */
206 new->parent = list;
207
208 /* In case of this is the first element of master. */
209 if (list->head == NULL) {
210 list->head = list->tail = new;
211 return new;
212 }
213
214 /* In case of insertion is made at the tail of access_list. */
215 if (point == NULL) {
216 new->prev = list->tail;
217 list->tail->next = new;
218 list->tail = new;
219 return new;
220 }
221
222 /* In case of insertion is made at the head of access_list. */
223 if (point == list->head) {
224 new->next = list->head;
225 list->head->prev = new;
226 list->head = new;
227 return new;
228 }
229
230 /* Insertion is made at middle of the access_list. */
231 new->next = point;
232 new->prev = point->prev;
233
234 if (point->prev)
235 point->prev->next = new;
236 point->prev = new;
237
238 return new;
239 }
240
241 struct community_list *community_list_lookup(struct community_list_handler *ch,
242 const char *name,
243 uint32_t name_hash,
244 int master)
245 {
246 struct community_list lookup;
247 struct community_list_master *cm;
248
249 if (!name)
250 return NULL;
251
252 cm = community_list_master_lookup(ch, master);
253 if (!cm)
254 return NULL;
255
256 lookup.name = (char *)name;
257 lookup.name_hash = name_hash;
258 return hash_get(cm->hash, &lookup, NULL);
259 }
260
261 static struct community_list *
262 community_list_get(struct community_list_handler *ch, const char *name,
263 int master)
264 {
265 struct community_list *list;
266
267 list = community_list_lookup(ch, name, 0, master);
268 if (!list)
269 list = community_list_insert(ch, name, master);
270 return list;
271 }
272
273 static void community_list_delete(struct community_list_master *cm,
274 struct community_list *list)
275 {
276 struct community_list_list *clist;
277 struct community_entry *entry, *next;
278
279 for (entry = list->head; entry; entry = next) {
280 next = entry->next;
281 community_entry_free(entry);
282 }
283
284 clist = list->parent;
285
286 if (list->next)
287 list->next->prev = list->prev;
288 else
289 clist->tail = list->prev;
290
291 if (list->prev)
292 list->prev->next = list->next;
293 else
294 clist->head = list->next;
295
296 hash_release(cm->hash, list);
297 community_list_free(list);
298 }
299
300 static bool community_list_empty_p(struct community_list *list)
301 {
302 return list->head == NULL && list->tail == NULL;
303 }
304
305 /* Delete community-list entry from the list. */
306 static void community_list_entry_delete(struct community_list_master *cm,
307 struct community_list *list,
308 struct community_entry *entry)
309 {
310 if (entry->next)
311 entry->next->prev = entry->prev;
312 else
313 list->tail = entry->prev;
314
315 if (entry->prev)
316 entry->prev->next = entry->next;
317 else
318 list->head = entry->next;
319
320 community_entry_free(entry);
321
322 if (community_list_empty_p(list))
323 community_list_delete(cm, list);
324 }
325
326 /*
327 * Replace community-list entry in the list. Note that entry is the new one
328 * and replace is one one being replaced.
329 */
330 static void community_list_entry_replace(struct community_list *list,
331 struct community_entry *replace,
332 struct community_entry *entry)
333 {
334 if (replace->next) {
335 entry->next = replace->next;
336 replace->next->prev = entry;
337 } else {
338 entry->next = NULL;
339 list->tail = entry;
340 }
341
342 if (replace->prev) {
343 entry->prev = replace->prev;
344 replace->prev->next = entry;
345 } else {
346 entry->prev = NULL;
347 list->head = entry;
348 }
349
350 community_entry_free(replace);
351 }
352
353 /* Add community-list entry to the list. */
354 static void community_list_entry_add(struct community_list *list,
355 struct community_entry *entry,
356 struct community_list_handler *ch,
357 int master)
358 {
359 struct community_entry *replace;
360 struct community_entry *point;
361
362 /* Automatic assignment of seq no. */
363 if (entry->seq == COMMUNITY_SEQ_NUMBER_AUTO)
364 entry->seq = bgp_clist_new_seq_get(list);
365
366 if (list->tail && entry->seq > list->tail->seq)
367 point = NULL;
368 else {
369 replace = bgp_clist_seq_check(list, entry->seq);
370 if (replace) {
371 community_list_entry_replace(list, replace, entry);
372 return;
373 }
374
375 /* Check insert point. */
376 for (point = list->head; point; point = point->next)
377 if (point->seq >= entry->seq)
378 break;
379 }
380
381 /* In case of this is the first element of the list. */
382 entry->next = point;
383
384 if (point) {
385 if (point->prev)
386 point->prev->next = entry;
387 else
388 list->head = entry;
389
390 entry->prev = point->prev;
391 point->prev = entry;
392 } else {
393 if (list->tail)
394 list->tail->next = entry;
395 else
396 list->head = entry;
397
398 entry->prev = list->tail;
399 list->tail = entry;
400 }
401 }
402
403 /* Lookup community-list entry from the list. */
404 static struct community_entry *
405 community_list_entry_lookup(struct community_list *list, const void *arg,
406 int direct)
407 {
408 struct community_entry *entry;
409
410 for (entry = list->head; entry; entry = entry->next) {
411 switch (entry->style) {
412 case COMMUNITY_LIST_STANDARD:
413 if (entry->direct == direct
414 && community_cmp(entry->u.com, arg))
415 return entry;
416 break;
417 case EXTCOMMUNITY_LIST_STANDARD:
418 if (entry->direct == direct
419 && ecommunity_cmp(entry->u.ecom, arg))
420 return entry;
421 break;
422 case LARGE_COMMUNITY_LIST_STANDARD:
423 if (entry->direct == direct
424 && lcommunity_cmp(entry->u.lcom, arg))
425 return entry;
426 break;
427 case COMMUNITY_LIST_EXPANDED:
428 case EXTCOMMUNITY_LIST_EXPANDED:
429 case LARGE_COMMUNITY_LIST_EXPANDED:
430 if (entry->direct == direct
431 && strcmp(entry->config, arg) == 0)
432 return entry;
433 break;
434 default:
435 break;
436 }
437 }
438 return NULL;
439 }
440
441 static char *community_str_get(struct community *com, int i)
442 {
443 uint32_t comval;
444 uint16_t as;
445 uint16_t val;
446 char *str;
447
448 memcpy(&comval, com_nthval(com, i), sizeof(uint32_t));
449 comval = ntohl(comval);
450
451 switch (comval) {
452 #if CONFDATE > 20230801
453 CPP_NOTICE("Deprecate COMMUNITY_INTERNET BGP community")
454 #endif
455 case COMMUNITY_INTERNET:
456 str = XSTRDUP(MTYPE_COMMUNITY_STR, "internet");
457 zlog_warn("`internet` community is deprecated");
458 break;
459 case COMMUNITY_GSHUT:
460 str = XSTRDUP(MTYPE_COMMUNITY_STR, "graceful-shutdown");
461 break;
462 case COMMUNITY_ACCEPT_OWN:
463 str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own");
464 break;
465 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4:
466 str = XSTRDUP(MTYPE_COMMUNITY_STR,
467 "route-filter-translated-v4");
468 break;
469 case COMMUNITY_ROUTE_FILTER_v4:
470 str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v4");
471 break;
472 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6:
473 str = XSTRDUP(MTYPE_COMMUNITY_STR,
474 "route-filter-translated-v6");
475 break;
476 case COMMUNITY_ROUTE_FILTER_v6:
477 str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v6");
478 break;
479 case COMMUNITY_LLGR_STALE:
480 str = XSTRDUP(MTYPE_COMMUNITY_STR, "llgr-stale");
481 break;
482 case COMMUNITY_NO_LLGR:
483 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-llgr");
484 break;
485 case COMMUNITY_ACCEPT_OWN_NEXTHOP:
486 str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own-nexthop");
487 break;
488 case COMMUNITY_BLACKHOLE:
489 str = XSTRDUP(MTYPE_COMMUNITY_STR, "blackhole");
490 break;
491 case COMMUNITY_NO_EXPORT:
492 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-export");
493 break;
494 case COMMUNITY_NO_ADVERTISE:
495 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-advertise");
496 break;
497 case COMMUNITY_LOCAL_AS:
498 str = XSTRDUP(MTYPE_COMMUNITY_STR, "local-AS");
499 break;
500 case COMMUNITY_NO_PEER:
501 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-peer");
502 break;
503 default:
504 str = XSTRDUP(MTYPE_COMMUNITY_STR, "65536:65535");
505 as = (comval >> 16) & 0xFFFF;
506 val = comval & 0xFFFF;
507 snprintf(str, strlen(str), "%u:%d", as, val);
508 break;
509 }
510
511 return str;
512 }
513
514 /* Internal function to perform regular expression match for
515 * a single community. */
516 static bool community_regexp_include(regex_t *reg, struct community *com, int i)
517 {
518 char *str;
519 int rv;
520
521 /* When there is no communities attribute it is treated as empty string.
522 */
523 if (com == NULL || com->size == 0)
524 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
525 else
526 str = community_str_get(com, i);
527
528 /* Regular expression match. */
529 rv = regexec(reg, str, 0, NULL, 0);
530
531 XFREE(MTYPE_COMMUNITY_STR, str);
532
533 return rv == 0;
534 }
535
536 /* Internal function to perform regular expression match for community
537 attribute. */
538 static bool community_regexp_match(struct community *com, regex_t *reg)
539 {
540 const char *str;
541 char *regstr;
542 int rv;
543
544 /* When there is no communities attribute it is treated as empty
545 string. */
546 if (com == NULL || com->size == 0)
547 str = "";
548 else
549 str = community_str(com, false, true);
550
551 regstr = bgp_alias2community_str(str);
552
553 /* Regular expression match. */
554 rv = regexec(reg, regstr, 0, NULL, 0);
555
556 XFREE(MTYPE_TMP, regstr);
557
558 return rv == 0;
559 }
560
561 static char *lcommunity_str_get(struct lcommunity *lcom, int i)
562 {
563 struct lcommunity_val lcomval;
564 uint32_t globaladmin;
565 uint32_t localdata1;
566 uint32_t localdata2;
567 char *str;
568 const uint8_t *ptr;
569
570 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
571
572 memcpy(&lcomval, ptr, LCOMMUNITY_SIZE);
573
574 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
575 ptr = (uint8_t *)lcomval.val;
576 ptr = ptr_get_be32(ptr, &globaladmin);
577 ptr = ptr_get_be32(ptr, &localdata1);
578 ptr = ptr_get_be32(ptr, &localdata2);
579 (void)ptr; /* consume value */
580
581 str = XMALLOC(MTYPE_LCOMMUNITY_STR, 48);
582 snprintf(str, 48, "%u:%u:%u", globaladmin, localdata1, localdata2);
583
584 return str;
585 }
586
587 /* Internal function to perform regular expression match for
588 * a single community. */
589 static bool lcommunity_regexp_include(regex_t *reg, struct lcommunity *lcom,
590 int i)
591 {
592 char *str;
593
594 /* When there is no communities attribute it is treated as empty string.
595 */
596 if (lcom == NULL || lcom->size == 0)
597 str = XSTRDUP(MTYPE_LCOMMUNITY_STR, "");
598 else
599 str = lcommunity_str_get(lcom, i);
600
601 /* Regular expression match. */
602 if (regexec(reg, str, 0, NULL, 0) == 0) {
603 XFREE(MTYPE_LCOMMUNITY_STR, str);
604 return true;
605 }
606
607 XFREE(MTYPE_LCOMMUNITY_STR, str);
608 /* No match. */
609 return false;
610 }
611
612 static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
613 {
614 const char *str;
615 char *regstr;
616 int rv;
617
618 /* When there is no communities attribute it is treated as empty
619 string. */
620 if (com == NULL || com->size == 0)
621 str = "";
622 else
623 str = lcommunity_str(com, false, true);
624
625 regstr = bgp_alias2community_str(str);
626
627 /* Regular expression match. */
628 rv = regexec(reg, regstr, 0, NULL, 0);
629
630 XFREE(MTYPE_TMP, regstr);
631
632 return rv == 0;
633 }
634
635
636 static bool ecommunity_regexp_match(struct ecommunity *ecom, regex_t *reg)
637 {
638 const char *str;
639
640 /* When there is no communities attribute it is treated as empty
641 string. */
642 if (ecom == NULL || ecom->size == 0)
643 str = "";
644 else
645 str = ecommunity_str(ecom);
646
647 /* Regular expression match. */
648 if (regexec(reg, str, 0, NULL, 0) == 0)
649 return true;
650
651 /* No match. */
652 return false;
653 }
654
655 /* When given community attribute matches to the community-list return
656 1 else return 0. */
657 bool community_list_match(struct community *com, struct community_list *list)
658 {
659 struct community_entry *entry;
660
661 for (entry = list->head; entry; entry = entry->next) {
662 if (entry->any)
663 return entry->direct == COMMUNITY_PERMIT;
664
665 if (entry->style == COMMUNITY_LIST_STANDARD) {
666 if (community_include(entry->u.com, COMMUNITY_INTERNET))
667 return entry->direct == COMMUNITY_PERMIT;
668
669 if (community_match(com, entry->u.com))
670 return entry->direct == COMMUNITY_PERMIT;
671 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
672 if (community_regexp_match(com, entry->reg))
673 return entry->direct == COMMUNITY_PERMIT;
674 }
675 }
676 return false;
677 }
678
679 bool lcommunity_list_match(struct lcommunity *lcom, struct community_list *list)
680 {
681 struct community_entry *entry;
682
683 for (entry = list->head; entry; entry = entry->next) {
684 if (entry->any)
685 return entry->direct == COMMUNITY_PERMIT;
686
687 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
688 if (lcommunity_match(lcom, entry->u.lcom))
689 return entry->direct == COMMUNITY_PERMIT;
690 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
691 if (lcommunity_regexp_match(lcom, entry->reg))
692 return entry->direct == COMMUNITY_PERMIT;
693 }
694 }
695 return false;
696 }
697
698
699 /* Perform exact matching. In case of expanded large-community-list, do
700 * same thing as lcommunity_list_match().
701 */
702 bool lcommunity_list_exact_match(struct lcommunity *lcom,
703 struct community_list *list)
704 {
705 struct community_entry *entry;
706
707 for (entry = list->head; entry; entry = entry->next) {
708 if (entry->any)
709 return entry->direct == COMMUNITY_PERMIT;
710
711 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
712 if (lcommunity_cmp(lcom, entry->u.lcom))
713 return entry->direct == COMMUNITY_PERMIT;
714 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
715 if (lcommunity_regexp_match(lcom, entry->reg))
716 return entry->direct == COMMUNITY_PERMIT;
717 }
718 }
719 return false;
720 }
721
722 bool ecommunity_list_match(struct ecommunity *ecom, struct community_list *list)
723 {
724 struct community_entry *entry;
725
726 for (entry = list->head; entry; entry = entry->next) {
727 if (entry->any)
728 return entry->direct == COMMUNITY_PERMIT;
729
730 if (entry->style == EXTCOMMUNITY_LIST_STANDARD) {
731 if (ecommunity_match(ecom, entry->u.ecom))
732 return entry->direct == COMMUNITY_PERMIT;
733 } else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) {
734 if (ecommunity_regexp_match(ecom, entry->reg))
735 return entry->direct == COMMUNITY_PERMIT;
736 }
737 }
738 return false;
739 }
740
741 /* Perform exact matching. In case of expanded community-list, do
742 same thing as community_list_match(). */
743 bool community_list_exact_match(struct community *com,
744 struct community_list *list)
745 {
746 struct community_entry *entry;
747
748 for (entry = list->head; entry; entry = entry->next) {
749 if (entry->any)
750 return entry->direct == COMMUNITY_PERMIT;
751
752 if (entry->style == COMMUNITY_LIST_STANDARD) {
753 if (community_include(entry->u.com, COMMUNITY_INTERNET))
754 return entry->direct == COMMUNITY_PERMIT;
755
756 if (community_cmp(com, entry->u.com))
757 return entry->direct == COMMUNITY_PERMIT;
758 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
759 if (community_regexp_match(com, entry->reg))
760 return entry->direct == COMMUNITY_PERMIT;
761 }
762 }
763 return false;
764 }
765
766 /* Delete all permitted communities in the list from com. */
767 struct community *community_list_match_delete(struct community *com,
768 struct community_list *list)
769 {
770 struct community_entry *entry;
771 uint32_t val;
772 uint32_t com_index_to_delete[com->size];
773 int delete_index = 0;
774 int i;
775
776 /* Loop over each community value and evaluate each against the
777 * community-list. If we need to delete a community value add its index
778 * to com_index_to_delete.
779 */
780 for (i = 0; i < com->size; i++) {
781 val = community_val_get(com, i);
782
783 for (entry = list->head; entry; entry = entry->next) {
784 if (entry->any) {
785 if (entry->direct == COMMUNITY_PERMIT) {
786 com_index_to_delete[delete_index] = i;
787 delete_index++;
788 }
789 break;
790 }
791
792 else if ((entry->style == COMMUNITY_LIST_STANDARD)
793 && (community_include(entry->u.com,
794 COMMUNITY_INTERNET)
795 || community_include(entry->u.com, val))) {
796 if (entry->direct == COMMUNITY_PERMIT) {
797 com_index_to_delete[delete_index] = i;
798 delete_index++;
799 }
800 break;
801 }
802
803 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
804 && community_regexp_include(entry->reg, com,
805 i)) {
806 if (entry->direct == COMMUNITY_PERMIT) {
807 com_index_to_delete[delete_index] = i;
808 delete_index++;
809 }
810 break;
811 }
812 }
813 }
814
815 /* Delete all of the communities we flagged for deletion */
816 for (i = delete_index - 1; i >= 0; i--) {
817 val = community_val_get(com, com_index_to_delete[i]);
818 val = htonl(val);
819 community_del_val(com, &val);
820 }
821
822 return com;
823 }
824
825 /* To avoid duplicated entry in the community-list, this function
826 compares specified entry to existing entry. */
827 static bool community_list_dup_check(struct community_list *list,
828 struct community_entry *new)
829 {
830 struct community_entry *entry;
831
832 for (entry = list->head; entry; entry = entry->next) {
833 if (entry->style != new->style)
834 continue;
835
836 if (entry->direct != new->direct)
837 continue;
838
839 if (entry->any != new->any)
840 continue;
841
842 if (entry->any)
843 return true;
844
845 switch (entry->style) {
846 case COMMUNITY_LIST_STANDARD:
847 if (community_cmp(entry->u.com, new->u.com))
848 return true;
849 break;
850 case LARGE_COMMUNITY_LIST_STANDARD:
851 if (lcommunity_cmp(entry->u.lcom, new->u.lcom))
852 return true;
853 break;
854 case EXTCOMMUNITY_LIST_STANDARD:
855 if (ecommunity_cmp(entry->u.ecom, new->u.ecom))
856 return true;
857 break;
858 case COMMUNITY_LIST_EXPANDED:
859 case EXTCOMMUNITY_LIST_EXPANDED:
860 case LARGE_COMMUNITY_LIST_EXPANDED:
861 if (strcmp(entry->config, new->config) == 0)
862 return true;
863 break;
864 default:
865 break;
866 }
867 }
868 return false;
869 }
870
871 /* Set community-list. */
872 int community_list_set(struct community_list_handler *ch, const char *name,
873 const char *str, const char *seq, int direct, int style)
874 {
875 struct community_entry *entry = NULL;
876 struct community_list *list;
877 struct community *com = NULL;
878 regex_t *regex = NULL;
879 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
880
881 if (seq)
882 seqnum = (int64_t)atol(seq);
883
884 /* Get community list. */
885 list = community_list_get(ch, name, COMMUNITY_LIST_MASTER);
886
887 /* When community-list already has entry, new entry should have same
888 style. If you want to have mixed style community-list, you can
889 comment out this check. */
890 if (!community_list_empty_p(list)) {
891 struct community_entry *first;
892
893 first = list->head;
894
895 if (style != first->style) {
896 return (first->style == COMMUNITY_LIST_STANDARD
897 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
898 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
899 }
900 }
901
902 if (str) {
903 if (style == COMMUNITY_LIST_STANDARD)
904 com = community_str2com(str);
905 else
906 regex = bgp_regcomp(str);
907
908 if (!com && !regex)
909 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
910 }
911
912 entry = community_entry_new();
913 entry->direct = direct;
914 entry->style = style;
915 entry->any = (str ? false : true);
916 entry->u.com = com;
917 entry->reg = regex;
918 entry->seq = seqnum;
919 entry->config =
920 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
921
922 /* Do not put duplicated community entry. */
923 if (community_list_dup_check(list, entry))
924 community_entry_free(entry);
925 else {
926 community_list_entry_add(list, entry, ch,
927 COMMUNITY_LIST_MASTER);
928 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
929 }
930
931 return 0;
932 }
933
934 /* Unset community-list */
935 int community_list_unset(struct community_list_handler *ch, const char *name,
936 const char *str, const char *seq, int direct,
937 int style)
938 {
939 struct community_list_master *cm = NULL;
940 struct community_entry *entry = NULL;
941 struct community_list *list;
942 struct community *com = NULL;
943
944 /* Lookup community list. */
945 list = community_list_lookup(ch, name, 0, COMMUNITY_LIST_MASTER);
946 if (list == NULL)
947 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
948
949 cm = community_list_master_lookup(ch, COMMUNITY_LIST_MASTER);
950 /* Delete all of entry belongs to this community-list. */
951 if (!str) {
952 community_list_delete(cm, list);
953 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
954 return 0;
955 }
956
957 if (style == COMMUNITY_LIST_STANDARD)
958 com = community_str2com(str);
959
960 if (com) {
961 entry = community_list_entry_lookup(list, com, direct);
962 community_free(&com);
963 } else
964 entry = community_list_entry_lookup(list, str, direct);
965
966 if (!entry)
967 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
968
969 community_list_entry_delete(cm, list, entry);
970 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
971
972 return 0;
973 }
974
975 /* Delete all permitted large communities in the list from com. */
976 struct lcommunity *lcommunity_list_match_delete(struct lcommunity *lcom,
977 struct community_list *list)
978 {
979 struct community_entry *entry;
980 uint32_t com_index_to_delete[lcom->size];
981 uint8_t *ptr;
982 int delete_index = 0;
983 int i;
984
985 /* Loop over each lcommunity value and evaluate each against the
986 * community-list. If we need to delete a community value add its index
987 * to com_index_to_delete.
988 */
989 for (i = 0; i < lcom->size; i++) {
990 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
991 for (entry = list->head; entry; entry = entry->next) {
992 if (entry->any) {
993 if (entry->direct == COMMUNITY_PERMIT) {
994 com_index_to_delete[delete_index] = i;
995 delete_index++;
996 }
997 break;
998 }
999
1000 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
1001 && lcommunity_include(entry->u.lcom, ptr)) {
1002 if (entry->direct == COMMUNITY_PERMIT) {
1003 com_index_to_delete[delete_index] = i;
1004 delete_index++;
1005 }
1006 break;
1007 }
1008
1009 else if ((entry->style == LARGE_COMMUNITY_LIST_EXPANDED)
1010 && lcommunity_regexp_include(entry->reg, lcom,
1011 i)) {
1012 if (entry->direct == COMMUNITY_PERMIT) {
1013 com_index_to_delete[delete_index] = i;
1014 delete_index++;
1015 }
1016 break;
1017 }
1018 }
1019 }
1020
1021 /* Delete all of the communities we flagged for deletion */
1022 for (i = delete_index - 1; i >= 0; i--) {
1023 ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE);
1024 lcommunity_del_val(lcom, ptr);
1025 }
1026
1027 return lcom;
1028 }
1029
1030 /* Helper to check if every octet do not exceed UINT_MAX */
1031 bool lcommunity_list_valid(const char *community, int style)
1032 {
1033 int octets;
1034 char **splits, **communities;
1035 char *endptr;
1036 int num, num_communities;
1037 regex_t *regres;
1038 int invalid = 0;
1039
1040 frrstr_split(community, " ", &communities, &num_communities);
1041
1042 for (int j = 0; j < num_communities; j++) {
1043 octets = 0;
1044 frrstr_split(communities[j], ":", &splits, &num);
1045
1046 for (int i = 0; i < num; i++) {
1047 if (strlen(splits[i]) == 0)
1048 /* There is no digit to check */
1049 invalid++;
1050
1051 if (style == LARGE_COMMUNITY_LIST_STANDARD) {
1052 if (*splits[i] == '-')
1053 /* Must not be negative */
1054 invalid++;
1055 else if (strtoul(splits[i], &endptr, 10)
1056 > UINT_MAX)
1057 /* Larger than 4 octets */
1058 invalid++;
1059 else if (*endptr)
1060 /* Not all characters were digits */
1061 invalid++;
1062 } else {
1063 regres = bgp_regcomp(communities[j]);
1064 if (!regres)
1065 /* malformed regex */
1066 invalid++;
1067 else
1068 bgp_regex_free(regres);
1069 }
1070
1071 octets++;
1072 XFREE(MTYPE_TMP, splits[i]);
1073 }
1074 XFREE(MTYPE_TMP, splits);
1075
1076 if (octets != 3)
1077 invalid++;
1078
1079 XFREE(MTYPE_TMP, communities[j]);
1080 }
1081 XFREE(MTYPE_TMP, communities);
1082
1083 return (invalid > 0) ? false : true;
1084 }
1085
1086 /* Set lcommunity-list. */
1087 int lcommunity_list_set(struct community_list_handler *ch, const char *name,
1088 const char *str, const char *seq, int direct, int style)
1089 {
1090 struct community_entry *entry = NULL;
1091 struct community_list *list;
1092 struct lcommunity *lcom = NULL;
1093 regex_t *regex = NULL;
1094 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
1095
1096 if (seq)
1097 seqnum = (int64_t)atol(seq);
1098
1099 /* Get community list. */
1100 list = community_list_get(ch, name, LARGE_COMMUNITY_LIST_MASTER);
1101
1102 /* When community-list already has entry, new entry should have same
1103 style. If you want to have mixed style community-list, you can
1104 comment out this check. */
1105 if (!community_list_empty_p(list)) {
1106 struct community_entry *first;
1107
1108 first = list->head;
1109
1110 if (style != first->style) {
1111 return (first->style == COMMUNITY_LIST_STANDARD
1112 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1113 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1114 }
1115 }
1116
1117 if (str) {
1118 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1119 lcom = lcommunity_str2com(str);
1120 else
1121 regex = bgp_regcomp(str);
1122
1123 if (!lcom && !regex)
1124 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1125 }
1126
1127 entry = community_entry_new();
1128 entry->direct = direct;
1129 entry->style = style;
1130 entry->any = (str ? false : true);
1131 entry->u.lcom = lcom;
1132 entry->reg = regex;
1133 entry->seq = seqnum;
1134 entry->config =
1135 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
1136
1137 /* Do not put duplicated community entry. */
1138 if (community_list_dup_check(list, entry))
1139 community_entry_free(entry);
1140 else {
1141 community_list_entry_add(list, entry, ch,
1142 LARGE_COMMUNITY_LIST_MASTER);
1143 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_ADDED);
1144 }
1145
1146 return 0;
1147 }
1148
1149 /* Unset community-list. When str is NULL, delete all of
1150 community-list entry belongs to the specified name. */
1151 int lcommunity_list_unset(struct community_list_handler *ch, const char *name,
1152 const char *str, const char *seq, int direct,
1153 int style)
1154 {
1155 struct community_list_master *cm = NULL;
1156 struct community_entry *entry = NULL;
1157 struct community_list *list;
1158 struct lcommunity *lcom = NULL;
1159 regex_t *regex = NULL;
1160
1161 /* Lookup community list. */
1162 list = community_list_lookup(ch, name, 0, LARGE_COMMUNITY_LIST_MASTER);
1163 if (list == NULL)
1164 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1165
1166 cm = community_list_master_lookup(ch, LARGE_COMMUNITY_LIST_MASTER);
1167 /* Delete all of entry belongs to this community-list. */
1168 if (!str) {
1169 community_list_delete(cm, list);
1170 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
1171 return 0;
1172 }
1173
1174 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1175 lcom = lcommunity_str2com(str);
1176 else
1177 regex = bgp_regcomp(str);
1178
1179 if (!lcom && !regex)
1180 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1181
1182 if (lcom)
1183 entry = community_list_entry_lookup(list, lcom, direct);
1184 else
1185 entry = community_list_entry_lookup(list, str, direct);
1186
1187 if (lcom)
1188 lcommunity_free(&lcom);
1189 if (regex)
1190 bgp_regex_free(regex);
1191
1192 if (!entry)
1193 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1194
1195 community_list_entry_delete(cm, list, entry);
1196 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
1197
1198 return 0;
1199 }
1200
1201 /* Set extcommunity-list. */
1202 int extcommunity_list_set(struct community_list_handler *ch, const char *name,
1203 const char *str, const char *seq, int direct,
1204 int style)
1205 {
1206 struct community_entry *entry = NULL;
1207 struct community_list *list;
1208 struct ecommunity *ecom = NULL;
1209 regex_t *regex = NULL;
1210 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
1211
1212 if (seq)
1213 seqnum = (int64_t)atol(seq);
1214
1215 if (str == NULL)
1216 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1217
1218 /* Get community list. */
1219 list = community_list_get(ch, name, EXTCOMMUNITY_LIST_MASTER);
1220
1221 /* When community-list already has entry, new entry should have same
1222 style. If you want to have mixed style community-list, you can
1223 comment out this check. */
1224 if (!community_list_empty_p(list)) {
1225 struct community_entry *first;
1226
1227 first = list->head;
1228
1229 if (style != first->style) {
1230 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1231 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1232 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1233 }
1234 }
1235
1236 if (style == EXTCOMMUNITY_LIST_STANDARD)
1237 ecom = ecommunity_str2com(str, 0, 1);
1238 else
1239 regex = bgp_regcomp(str);
1240
1241 if (!ecom && !regex)
1242 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1243
1244 if (ecom)
1245 ecom->str =
1246 ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
1247
1248 entry = community_entry_new();
1249 entry->direct = direct;
1250 entry->style = style;
1251 entry->any = false;
1252 if (ecom)
1253 entry->config = ecommunity_ecom2str(
1254 ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0);
1255 else if (regex)
1256 entry->config = XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str);
1257
1258 entry->u.ecom = ecom;
1259 entry->reg = regex;
1260 entry->seq = seqnum;
1261
1262 /* Do not put duplicated community entry. */
1263 if (community_list_dup_check(list, entry))
1264 community_entry_free(entry);
1265 else {
1266 community_list_entry_add(list, entry, ch,
1267 EXTCOMMUNITY_LIST_MASTER);
1268 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1269 }
1270
1271 return 0;
1272 }
1273
1274 /* Unset extcommunity-list.
1275 *
1276 * When str is NULL, delete all extcommunity-list entries belonging to the
1277 * specified name.
1278 */
1279 int extcommunity_list_unset(struct community_list_handler *ch, const char *name,
1280 const char *str, const char *seq, int direct,
1281 int style)
1282 {
1283 struct community_list_master *cm = NULL;
1284 struct community_entry *entry = NULL;
1285 struct community_list *list;
1286 struct ecommunity *ecom = NULL;
1287
1288 /* Lookup extcommunity list. */
1289 list = community_list_lookup(ch, name, 0, EXTCOMMUNITY_LIST_MASTER);
1290 if (list == NULL)
1291 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1292
1293 cm = community_list_master_lookup(ch, EXTCOMMUNITY_LIST_MASTER);
1294 /* Delete all of entry belongs to this extcommunity-list. */
1295 if (!str) {
1296 community_list_delete(cm, list);
1297 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1298 return 0;
1299 }
1300
1301 if (style == EXTCOMMUNITY_LIST_STANDARD)
1302 ecom = ecommunity_str2com(str, 0, 1);
1303
1304 if (ecom) {
1305 entry = community_list_entry_lookup(list, ecom, direct);
1306 ecommunity_free(&ecom);
1307 } else
1308 entry = community_list_entry_lookup(list, str, direct);
1309
1310 if (!entry)
1311 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1312
1313 community_list_entry_delete(cm, list, entry);
1314 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1315
1316 return 0;
1317 }
1318
1319 /* Initializa community-list. Return community-list handler. */
1320 struct community_list_handler *community_list_init(void)
1321 {
1322 struct community_list_handler *ch;
1323 ch = XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER,
1324 sizeof(struct community_list_handler));
1325
1326 ch->community_list.hash =
1327 hash_create_size(4, bgp_clist_hash_key_community_list,
1328 bgp_clist_hash_cmp_community_list,
1329 "Community List Number Quick Lookup");
1330
1331 ch->extcommunity_list.hash =
1332 hash_create_size(4, bgp_clist_hash_key_community_list,
1333 bgp_clist_hash_cmp_community_list,
1334 "Extended Community List Quick Lookup");
1335
1336 ch->lcommunity_list.hash =
1337 hash_create_size(4, bgp_clist_hash_key_community_list,
1338 bgp_clist_hash_cmp_community_list,
1339 "Large Community List Quick Lookup");
1340
1341 return ch;
1342 }
1343
1344 /* Terminate community-list. */
1345 void community_list_terminate(struct community_list_handler *ch)
1346 {
1347 struct community_list_master *cm;
1348 struct community_list *list;
1349
1350 cm = &ch->community_list;
1351 while ((list = cm->num.head) != NULL)
1352 community_list_delete(cm, list);
1353 while ((list = cm->str.head) != NULL)
1354 community_list_delete(cm, list);
1355 hash_free(cm->hash);
1356
1357 cm = &ch->lcommunity_list;
1358 while ((list = cm->num.head) != NULL)
1359 community_list_delete(cm, list);
1360 while ((list = cm->str.head) != NULL)
1361 community_list_delete(cm, list);
1362 hash_free(cm->hash);
1363
1364 cm = &ch->extcommunity_list;
1365 while ((list = cm->num.head) != NULL)
1366 community_list_delete(cm, list);
1367 while ((list = cm->str.head) != NULL)
1368 community_list_delete(cm, list);
1369 hash_free(cm->hash);
1370
1371 XFREE(MTYPE_COMMUNITY_LIST_HANDLER, ch);
1372 }
1373
1374 static int bgp_community_list_vector_walker(struct hash_bucket *bucket,
1375 void *data)
1376 {
1377 vector *comps = data;
1378 struct community_list *list = bucket->data;
1379
1380 vector_set(*comps, XSTRDUP(MTYPE_COMPLETION, list->name));
1381
1382 return 1;
1383 }
1384
1385 static void bgp_community_list_cmd_completion(vector comps,
1386 struct cmd_token *token)
1387 {
1388 struct community_list_master *cm;
1389
1390 cm = community_list_master_lookup(bgp_clist, COMMUNITY_LIST_MASTER);
1391
1392 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1393 }
1394
1395 static void bgp_lcommunity_list_cmd_completion(vector comps,
1396 struct cmd_token *token)
1397 {
1398 struct community_list_master *cm;
1399
1400 cm = community_list_master_lookup(bgp_clist,
1401 LARGE_COMMUNITY_LIST_MASTER);
1402
1403 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1404 }
1405
1406 static void bgp_extcommunity_list_cmd_completion(vector comps,
1407 struct cmd_token *token)
1408 {
1409 struct community_list_master *cm;
1410
1411 cm = community_list_master_lookup(bgp_clist, EXTCOMMUNITY_LIST_MASTER);
1412
1413 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1414 }
1415
1416 static const struct cmd_variable_handler community_list_handlers[] = {
1417 {.tokenname = "COMMUNITY_LIST_NAME",
1418 .completions = bgp_community_list_cmd_completion},
1419 {.completions = NULL}};
1420
1421 static const struct cmd_variable_handler lcommunity_list_handlers[] = {
1422 {.tokenname = "LCOMMUNITY_LIST_NAME",
1423 .completions = bgp_lcommunity_list_cmd_completion},
1424 {.completions = NULL}};
1425
1426 static const struct cmd_variable_handler extcommunity_list_handlers[] = {
1427 {.tokenname = "EXTCOMMUNITY_LIST_NAME",
1428 .completions = bgp_extcommunity_list_cmd_completion},
1429 {.completions = NULL}};
1430
1431 void bgp_community_list_command_completion_setup(void)
1432 {
1433 cmd_variable_handler_register(community_list_handlers);
1434 cmd_variable_handler_register(lcommunity_list_handlers);
1435 cmd_variable_handler_register(extcommunity_list_handlers);
1436 }