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