]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_clist.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[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->style == COMMUNITY_LIST_STANDARD) {
663 if (community_include(entry->u.com, COMMUNITY_INTERNET))
664 return entry->direct == COMMUNITY_PERMIT;
665
666 if (community_match(com, entry->u.com))
667 return entry->direct == COMMUNITY_PERMIT;
668 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
669 if (community_regexp_match(com, entry->reg))
670 return entry->direct == COMMUNITY_PERMIT;
671 }
672 }
673 return false;
674 }
675
676 bool lcommunity_list_match(struct lcommunity *lcom, struct community_list *list)
677 {
678 struct community_entry *entry;
679
680 for (entry = list->head; entry; entry = entry->next) {
681 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
682 if (lcommunity_match(lcom, entry->u.lcom))
683 return entry->direct == COMMUNITY_PERMIT;
684 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
685 if (lcommunity_regexp_match(lcom, entry->reg))
686 return entry->direct == COMMUNITY_PERMIT;
687 }
688 }
689 return false;
690 }
691
692
693 /* Perform exact matching. In case of expanded large-community-list, do
694 * same thing as lcommunity_list_match().
695 */
696 bool lcommunity_list_exact_match(struct lcommunity *lcom,
697 struct community_list *list)
698 {
699 struct community_entry *entry;
700
701 for (entry = list->head; entry; entry = entry->next) {
702 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
703 if (lcommunity_cmp(lcom, entry->u.lcom))
704 return entry->direct == COMMUNITY_PERMIT;
705 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
706 if (lcommunity_regexp_match(lcom, entry->reg))
707 return entry->direct == COMMUNITY_PERMIT;
708 }
709 }
710 return false;
711 }
712
713 bool ecommunity_list_match(struct ecommunity *ecom, struct community_list *list)
714 {
715 struct community_entry *entry;
716
717 for (entry = list->head; entry; entry = entry->next) {
718 if (entry->style == EXTCOMMUNITY_LIST_STANDARD) {
719 if (ecommunity_match(ecom, entry->u.ecom))
720 return entry->direct == COMMUNITY_PERMIT;
721 } else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) {
722 if (ecommunity_regexp_match(ecom, entry->reg))
723 return entry->direct == COMMUNITY_PERMIT;
724 }
725 }
726 return false;
727 }
728
729 /* Perform exact matching. In case of expanded community-list, do
730 same thing as community_list_match(). */
731 bool community_list_exact_match(struct community *com,
732 struct community_list *list)
733 {
734 struct community_entry *entry;
735
736 for (entry = list->head; entry; entry = entry->next) {
737 if (entry->style == COMMUNITY_LIST_STANDARD) {
738 if (community_include(entry->u.com, COMMUNITY_INTERNET))
739 return entry->direct == COMMUNITY_PERMIT;
740
741 if (community_cmp(com, entry->u.com))
742 return entry->direct == COMMUNITY_PERMIT;
743 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
744 if (community_regexp_match(com, entry->reg))
745 return entry->direct == COMMUNITY_PERMIT;
746 }
747 }
748 return false;
749 }
750
751 /* Delete all permitted communities in the list from com. */
752 struct community *community_list_match_delete(struct community *com,
753 struct community_list *list)
754 {
755 struct community_entry *entry;
756 uint32_t val;
757 uint32_t com_index_to_delete[com->size];
758 int delete_index = 0;
759 int i;
760
761 /* Loop over each community value and evaluate each against the
762 * community-list. If we need to delete a community value add its index
763 * to com_index_to_delete.
764 */
765 for (i = 0; i < com->size; i++) {
766 val = community_val_get(com, i);
767
768 for (entry = list->head; entry; entry = entry->next) {
769 if ((entry->style == COMMUNITY_LIST_STANDARD) &&
770 (community_include(entry->u.com,
771 COMMUNITY_INTERNET) ||
772 community_include(entry->u.com, val))) {
773 if (entry->direct == COMMUNITY_PERMIT) {
774 com_index_to_delete[delete_index] = i;
775 delete_index++;
776 }
777 break;
778 } else if ((entry->style == COMMUNITY_LIST_EXPANDED) &&
779 community_regexp_include(entry->reg, com,
780 i)) {
781 if (entry->direct == COMMUNITY_PERMIT) {
782 com_index_to_delete[delete_index] = i;
783 delete_index++;
784 }
785 break;
786 }
787 }
788 }
789
790 /* Delete all of the communities we flagged for deletion */
791 for (i = delete_index - 1; i >= 0; i--) {
792 val = community_val_get(com, com_index_to_delete[i]);
793 val = htonl(val);
794 community_del_val(com, &val);
795 }
796
797 return com;
798 }
799
800 /* To avoid duplicated entry in the community-list, this function
801 compares specified entry to existing entry. */
802 static bool community_list_dup_check(struct community_list *list,
803 struct community_entry *new)
804 {
805 struct community_entry *entry;
806
807 for (entry = list->head; entry; entry = entry->next) {
808 if (entry->style != new->style)
809 continue;
810
811 if (entry->direct != new->direct)
812 continue;
813
814 switch (entry->style) {
815 case COMMUNITY_LIST_STANDARD:
816 if (community_cmp(entry->u.com, new->u.com))
817 return true;
818 break;
819 case LARGE_COMMUNITY_LIST_STANDARD:
820 if (lcommunity_cmp(entry->u.lcom, new->u.lcom))
821 return true;
822 break;
823 case EXTCOMMUNITY_LIST_STANDARD:
824 if (ecommunity_cmp(entry->u.ecom, new->u.ecom))
825 return true;
826 break;
827 case COMMUNITY_LIST_EXPANDED:
828 case EXTCOMMUNITY_LIST_EXPANDED:
829 case LARGE_COMMUNITY_LIST_EXPANDED:
830 if (strcmp(entry->config, new->config) == 0)
831 return true;
832 break;
833 default:
834 break;
835 }
836 }
837 return false;
838 }
839
840 /* Set community-list. */
841 int community_list_set(struct community_list_handler *ch, const char *name,
842 const char *str, const char *seq, int direct, int style)
843 {
844 struct community_entry *entry = NULL;
845 struct community_list *list;
846 struct community *com = NULL;
847 regex_t *regex = NULL;
848 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
849
850 if (seq)
851 seqnum = (int64_t)atol(seq);
852
853 /* Get community list. */
854 list = community_list_get(ch, name, COMMUNITY_LIST_MASTER);
855
856 /* When community-list already has entry, new entry should have same
857 style. If you want to have mixed style community-list, you can
858 comment out this check. */
859 if (!community_list_empty_p(list)) {
860 struct community_entry *first;
861
862 first = list->head;
863
864 if (style != first->style) {
865 return (first->style == COMMUNITY_LIST_STANDARD
866 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
867 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
868 }
869 }
870
871 if (style == COMMUNITY_LIST_STANDARD)
872 com = community_str2com(str);
873 else
874 regex = bgp_regcomp(str);
875
876 if (!com && !regex)
877 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
878
879 entry = community_entry_new();
880 entry->direct = direct;
881 entry->style = style;
882 entry->u.com = com;
883 entry->reg = regex;
884 entry->seq = seqnum;
885 entry->config =
886 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
887
888 /* Do not put duplicated community entry. */
889 if (community_list_dup_check(list, entry))
890 community_entry_free(entry);
891 else {
892 community_list_entry_add(list, entry, ch,
893 COMMUNITY_LIST_MASTER);
894 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
895 }
896
897 return 0;
898 }
899
900 /* Unset community-list */
901 int community_list_unset(struct community_list_handler *ch, const char *name,
902 const char *str, const char *seq, int direct,
903 int style)
904 {
905 struct community_list_master *cm = NULL;
906 struct community_entry *entry = NULL;
907 struct community_list *list;
908 struct community *com = NULL;
909
910 /* Lookup community list. */
911 list = community_list_lookup(ch, name, 0, COMMUNITY_LIST_MASTER);
912 if (list == NULL)
913 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
914
915 cm = community_list_master_lookup(ch, COMMUNITY_LIST_MASTER);
916 /* Delete all of entry belongs to this community-list. */
917 if (!str) {
918 community_list_delete(cm, list);
919 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
920 return 0;
921 }
922
923 if (style == COMMUNITY_LIST_STANDARD)
924 com = community_str2com(str);
925
926 if (com) {
927 entry = community_list_entry_lookup(list, com, direct);
928 community_free(&com);
929 } else
930 entry = community_list_entry_lookup(list, str, direct);
931
932 if (!entry)
933 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
934
935 community_list_entry_delete(cm, list, entry);
936 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
937
938 return 0;
939 }
940
941 /* Delete all permitted large communities in the list from com. */
942 struct lcommunity *lcommunity_list_match_delete(struct lcommunity *lcom,
943 struct community_list *list)
944 {
945 struct community_entry *entry;
946 uint32_t com_index_to_delete[lcom->size];
947 uint8_t *ptr;
948 int delete_index = 0;
949 int i;
950
951 /* Loop over each lcommunity value and evaluate each against the
952 * community-list. If we need to delete a community value add its index
953 * to com_index_to_delete.
954 */
955 for (i = 0; i < lcom->size; i++) {
956 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
957 for (entry = list->head; entry; entry = entry->next) {
958 if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD) &&
959 lcommunity_include(entry->u.lcom, ptr)) {
960 if (entry->direct == COMMUNITY_PERMIT) {
961 com_index_to_delete[delete_index] = i;
962 delete_index++;
963 }
964 break;
965 }
966
967 else if ((entry->style ==
968 LARGE_COMMUNITY_LIST_EXPANDED) &&
969 lcommunity_regexp_include(entry->reg, lcom,
970 i)) {
971 if (entry->direct == COMMUNITY_PERMIT) {
972 com_index_to_delete[delete_index] = i;
973 delete_index++;
974 }
975 break;
976 }
977 }
978 }
979
980 /* Delete all of the communities we flagged for deletion */
981 for (i = delete_index - 1; i >= 0; i--) {
982 ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE);
983 lcommunity_del_val(lcom, ptr);
984 }
985
986 return lcom;
987 }
988
989 /* Helper to check if every octet do not exceed UINT_MAX */
990 bool lcommunity_list_valid(const char *community, int style)
991 {
992 int octets;
993 char **splits, **communities;
994 char *endptr;
995 int num, num_communities;
996 regex_t *regres;
997 int invalid = 0;
998
999 frrstr_split(community, " ", &communities, &num_communities);
1000
1001 for (int j = 0; j < num_communities; j++) {
1002 octets = 0;
1003 frrstr_split(communities[j], ":", &splits, &num);
1004
1005 for (int i = 0; i < num; i++) {
1006 if (strlen(splits[i]) == 0)
1007 /* There is no digit to check */
1008 invalid++;
1009
1010 if (style == LARGE_COMMUNITY_LIST_STANDARD) {
1011 if (*splits[i] == '-')
1012 /* Must not be negative */
1013 invalid++;
1014 else if (strtoul(splits[i], &endptr, 10)
1015 > UINT_MAX)
1016 /* Larger than 4 octets */
1017 invalid++;
1018 else if (*endptr)
1019 /* Not all characters were digits */
1020 invalid++;
1021 } else {
1022 regres = bgp_regcomp(communities[j]);
1023 if (!regres)
1024 /* malformed regex */
1025 invalid++;
1026 else
1027 bgp_regex_free(regres);
1028 }
1029
1030 octets++;
1031 XFREE(MTYPE_TMP, splits[i]);
1032 }
1033 XFREE(MTYPE_TMP, splits);
1034
1035 if (octets != 3)
1036 invalid++;
1037
1038 XFREE(MTYPE_TMP, communities[j]);
1039 }
1040 XFREE(MTYPE_TMP, communities);
1041
1042 return (invalid > 0) ? false : true;
1043 }
1044
1045 /* Set lcommunity-list. */
1046 int lcommunity_list_set(struct community_list_handler *ch, const char *name,
1047 const char *str, const char *seq, int direct, int style)
1048 {
1049 struct community_entry *entry = NULL;
1050 struct community_list *list;
1051 struct lcommunity *lcom = NULL;
1052 regex_t *regex = NULL;
1053 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
1054
1055 if (seq)
1056 seqnum = (int64_t)atol(seq);
1057
1058 /* Get community list. */
1059 list = community_list_get(ch, name, LARGE_COMMUNITY_LIST_MASTER);
1060
1061 /* When community-list already has entry, new entry should have same
1062 style. If you want to have mixed style community-list, you can
1063 comment out this check. */
1064 if (!community_list_empty_p(list)) {
1065 struct community_entry *first;
1066
1067 first = list->head;
1068
1069 if (style != first->style) {
1070 return (first->style == COMMUNITY_LIST_STANDARD
1071 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1072 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1073 }
1074 }
1075
1076 if (str) {
1077 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1078 lcom = lcommunity_str2com(str);
1079 else
1080 regex = bgp_regcomp(str);
1081
1082 if (!lcom && !regex)
1083 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1084 }
1085
1086 entry = community_entry_new();
1087 entry->direct = direct;
1088 entry->style = style;
1089 entry->u.lcom = lcom;
1090 entry->reg = regex;
1091 entry->seq = seqnum;
1092 entry->config =
1093 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
1094
1095 /* Do not put duplicated community entry. */
1096 if (community_list_dup_check(list, entry))
1097 community_entry_free(entry);
1098 else {
1099 community_list_entry_add(list, entry, ch,
1100 LARGE_COMMUNITY_LIST_MASTER);
1101 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_ADDED);
1102 }
1103
1104 return 0;
1105 }
1106
1107 /* Unset community-list. When str is NULL, delete all of
1108 community-list entry belongs to the specified name. */
1109 int lcommunity_list_unset(struct community_list_handler *ch, const char *name,
1110 const char *str, const char *seq, int direct,
1111 int style)
1112 {
1113 struct community_list_master *cm = NULL;
1114 struct community_entry *entry = NULL;
1115 struct community_list *list;
1116 struct lcommunity *lcom = NULL;
1117 regex_t *regex = NULL;
1118
1119 /* Lookup community list. */
1120 list = community_list_lookup(ch, name, 0, LARGE_COMMUNITY_LIST_MASTER);
1121 if (list == NULL)
1122 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1123
1124 cm = community_list_master_lookup(ch, LARGE_COMMUNITY_LIST_MASTER);
1125 /* Delete all of entry belongs to this community-list. */
1126 if (!str) {
1127 community_list_delete(cm, list);
1128 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
1129 return 0;
1130 }
1131
1132 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1133 lcom = lcommunity_str2com(str);
1134 else
1135 regex = bgp_regcomp(str);
1136
1137 if (!lcom && !regex)
1138 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1139
1140 if (lcom)
1141 entry = community_list_entry_lookup(list, lcom, direct);
1142 else
1143 entry = community_list_entry_lookup(list, str, direct);
1144
1145 if (lcom)
1146 lcommunity_free(&lcom);
1147 if (regex)
1148 bgp_regex_free(regex);
1149
1150 if (!entry)
1151 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1152
1153 community_list_entry_delete(cm, list, entry);
1154 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
1155
1156 return 0;
1157 }
1158
1159 /* Set extcommunity-list. */
1160 int extcommunity_list_set(struct community_list_handler *ch, const char *name,
1161 const char *str, const char *seq, int direct,
1162 int style)
1163 {
1164 struct community_entry *entry = NULL;
1165 struct community_list *list;
1166 struct ecommunity *ecom = NULL;
1167 regex_t *regex = NULL;
1168 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
1169
1170 if (seq)
1171 seqnum = (int64_t)atol(seq);
1172
1173 if (str == NULL)
1174 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1175
1176 /* Get community list. */
1177 list = community_list_get(ch, name, EXTCOMMUNITY_LIST_MASTER);
1178
1179 /* When community-list already has entry, new entry should have same
1180 style. If you want to have mixed style community-list, you can
1181 comment out this check. */
1182 if (!community_list_empty_p(list)) {
1183 struct community_entry *first;
1184
1185 first = list->head;
1186
1187 if (style != first->style) {
1188 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1189 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1190 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1191 }
1192 }
1193
1194 if (style == EXTCOMMUNITY_LIST_STANDARD)
1195 ecom = ecommunity_str2com(str, 0, 1);
1196 else
1197 regex = bgp_regcomp(str);
1198
1199 if (!ecom && !regex)
1200 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1201
1202 if (ecom)
1203 ecom->str =
1204 ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
1205
1206 entry = community_entry_new();
1207 entry->direct = direct;
1208 entry->style = style;
1209 if (ecom)
1210 entry->config = ecommunity_ecom2str(
1211 ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0);
1212 else if (regex)
1213 entry->config = XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str);
1214
1215 entry->u.ecom = ecom;
1216 entry->reg = regex;
1217 entry->seq = seqnum;
1218
1219 /* Do not put duplicated community entry. */
1220 if (community_list_dup_check(list, entry))
1221 community_entry_free(entry);
1222 else {
1223 community_list_entry_add(list, entry, ch,
1224 EXTCOMMUNITY_LIST_MASTER);
1225 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1226 }
1227
1228 return 0;
1229 }
1230
1231 /* Unset extcommunity-list.
1232 *
1233 * When str is NULL, delete all extcommunity-list entries belonging to the
1234 * specified name.
1235 */
1236 int extcommunity_list_unset(struct community_list_handler *ch, const char *name,
1237 const char *str, const char *seq, int direct,
1238 int style)
1239 {
1240 struct community_list_master *cm = NULL;
1241 struct community_entry *entry = NULL;
1242 struct community_list *list;
1243 struct ecommunity *ecom = NULL;
1244
1245 /* Lookup extcommunity list. */
1246 list = community_list_lookup(ch, name, 0, EXTCOMMUNITY_LIST_MASTER);
1247 if (list == NULL)
1248 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1249
1250 cm = community_list_master_lookup(ch, EXTCOMMUNITY_LIST_MASTER);
1251 /* Delete all of entry belongs to this extcommunity-list. */
1252 if (!str) {
1253 community_list_delete(cm, list);
1254 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1255 return 0;
1256 }
1257
1258 if (style == EXTCOMMUNITY_LIST_STANDARD)
1259 ecom = ecommunity_str2com(str, 0, 1);
1260
1261 if (ecom) {
1262 entry = community_list_entry_lookup(list, ecom, direct);
1263 ecommunity_free(&ecom);
1264 } else
1265 entry = community_list_entry_lookup(list, str, direct);
1266
1267 if (!entry)
1268 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1269
1270 community_list_entry_delete(cm, list, entry);
1271 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1272
1273 return 0;
1274 }
1275
1276 /* Initializa community-list. Return community-list handler. */
1277 struct community_list_handler *community_list_init(void)
1278 {
1279 struct community_list_handler *ch;
1280 ch = XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER,
1281 sizeof(struct community_list_handler));
1282
1283 ch->community_list.hash =
1284 hash_create_size(4, bgp_clist_hash_key_community_list,
1285 bgp_clist_hash_cmp_community_list,
1286 "Community List Number Quick Lookup");
1287
1288 ch->extcommunity_list.hash =
1289 hash_create_size(4, bgp_clist_hash_key_community_list,
1290 bgp_clist_hash_cmp_community_list,
1291 "Extended Community List Quick Lookup");
1292
1293 ch->lcommunity_list.hash =
1294 hash_create_size(4, bgp_clist_hash_key_community_list,
1295 bgp_clist_hash_cmp_community_list,
1296 "Large Community List Quick Lookup");
1297
1298 return ch;
1299 }
1300
1301 /* Terminate community-list. */
1302 void community_list_terminate(struct community_list_handler *ch)
1303 {
1304 struct community_list_master *cm;
1305 struct community_list *list;
1306
1307 cm = &ch->community_list;
1308 while ((list = cm->num.head) != NULL)
1309 community_list_delete(cm, list);
1310 while ((list = cm->str.head) != NULL)
1311 community_list_delete(cm, list);
1312 hash_free(cm->hash);
1313
1314 cm = &ch->lcommunity_list;
1315 while ((list = cm->num.head) != NULL)
1316 community_list_delete(cm, list);
1317 while ((list = cm->str.head) != NULL)
1318 community_list_delete(cm, list);
1319 hash_free(cm->hash);
1320
1321 cm = &ch->extcommunity_list;
1322 while ((list = cm->num.head) != NULL)
1323 community_list_delete(cm, list);
1324 while ((list = cm->str.head) != NULL)
1325 community_list_delete(cm, list);
1326 hash_free(cm->hash);
1327
1328 XFREE(MTYPE_COMMUNITY_LIST_HANDLER, ch);
1329 }
1330
1331 static int bgp_community_list_vector_walker(struct hash_bucket *bucket,
1332 void *data)
1333 {
1334 vector *comps = data;
1335 struct community_list *list = bucket->data;
1336
1337 vector_set(*comps, XSTRDUP(MTYPE_COMPLETION, list->name));
1338
1339 return 1;
1340 }
1341
1342 static void bgp_community_list_cmd_completion(vector comps,
1343 struct cmd_token *token)
1344 {
1345 struct community_list_master *cm;
1346
1347 cm = community_list_master_lookup(bgp_clist, COMMUNITY_LIST_MASTER);
1348
1349 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1350 }
1351
1352 static void bgp_lcommunity_list_cmd_completion(vector comps,
1353 struct cmd_token *token)
1354 {
1355 struct community_list_master *cm;
1356
1357 cm = community_list_master_lookup(bgp_clist,
1358 LARGE_COMMUNITY_LIST_MASTER);
1359
1360 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1361 }
1362
1363 static void bgp_extcommunity_list_cmd_completion(vector comps,
1364 struct cmd_token *token)
1365 {
1366 struct community_list_master *cm;
1367
1368 cm = community_list_master_lookup(bgp_clist, EXTCOMMUNITY_LIST_MASTER);
1369
1370 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1371 }
1372
1373 static const struct cmd_variable_handler community_list_handlers[] = {
1374 {.tokenname = "COMMUNITY_LIST_NAME",
1375 .completions = bgp_community_list_cmd_completion},
1376 {.completions = NULL}};
1377
1378 static const struct cmd_variable_handler lcommunity_list_handlers[] = {
1379 {.tokenname = "LCOMMUNITY_LIST_NAME",
1380 .completions = bgp_lcommunity_list_cmd_completion},
1381 {.completions = NULL}};
1382
1383 static const struct cmd_variable_handler extcommunity_list_handlers[] = {
1384 {.tokenname = "EXTCOMMUNITY_LIST_NAME",
1385 .completions = bgp_extcommunity_list_cmd_completion},
1386 {.completions = NULL}};
1387
1388 void bgp_community_list_command_completion_setup(void)
1389 {
1390 cmd_variable_handler_register(community_list_handlers);
1391 cmd_variable_handler_register(lcommunity_list_handlers);
1392 cmd_variable_handler_register(extcommunity_list_handlers);
1393 }