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