1 /* BGP community-list and extcommunity-list.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
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"
40 static uint32_t bgp_clist_hash_key_community_list(const void *data
)
42 struct community_list
*cl
= (struct community_list
*) data
;
47 cl
->name_hash
= bgp_clist_hash_key(cl
->name
);
51 static bool bgp_clist_hash_cmp_community_list(const void *a1
, const void *a2
)
53 const struct community_list
*cl1
= a1
;
54 const struct community_list
*cl2
= a2
;
56 if (cl1
->name_hash
!= cl2
->name_hash
)
59 if (strcmp(cl1
->name
, cl2
->name
) == 0)
65 /* Lookup master structure for community-list or
67 struct community_list_master
*
68 community_list_master_lookup(struct community_list_handler
*ch
, int 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
;
82 /* Allocate a new community list entry. */
83 static struct community_entry
*community_entry_new(void)
85 return XCALLOC(MTYPE_COMMUNITY_LIST_ENTRY
,
86 sizeof(struct community_entry
));
89 /* Free community list entry. */
90 static void community_entry_free(struct community_entry
*entry
)
92 switch (entry
->style
) {
93 case COMMUNITY_LIST_STANDARD
:
95 community_free(&entry
->u
.com
);
97 case LARGE_COMMUNITY_LIST_STANDARD
:
99 lcommunity_free(&entry
->u
.lcom
);
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
);
106 ecommunity_free(&entry
->u
.ecom
);
108 case COMMUNITY_LIST_EXPANDED
:
109 case EXTCOMMUNITY_LIST_EXPANDED
:
110 case LARGE_COMMUNITY_LIST_EXPANDED
:
111 XFREE(MTYPE_COMMUNITY_LIST_CONFIG
, entry
->config
);
113 bgp_regex_free(entry
->reg
);
117 XFREE(MTYPE_COMMUNITY_LIST_ENTRY
, entry
);
120 /* Allocate a new community-list. */
121 static struct community_list
*community_list_new(void)
123 return XCALLOC(MTYPE_COMMUNITY_LIST
, sizeof(struct community_list
));
126 /* Free community-list. */
127 static void community_list_free(struct community_list
*list
)
129 XFREE(MTYPE_COMMUNITY_LIST_NAME
, list
->name
);
130 XFREE(MTYPE_COMMUNITY_LIST
, list
);
133 static struct community_list
*
134 community_list_insert(struct community_list_handler
*ch
, const char *name
,
139 struct community_list
*new;
140 struct community_list
*point
;
141 struct community_list_list
*list
;
142 struct community_list_master
*cm
;
144 /* Lookup community-list master. */
145 cm
= community_list_master_lookup(ch
, master
);
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);
155 hash_get(cm
->hash
, new, hash_alloc_intern
);
157 /* If name is made by all digit character. We treat it as
159 for (number
= 0, i
= 0; i
< strlen(name
); i
++) {
160 if (isdigit((int)name
[i
]))
161 number
= (number
* 10) + (name
[i
] - '0');
166 /* In case of name is all digit character */
167 if (i
== strlen(name
)) {
168 new->sort
= COMMUNITY_LIST_NUMBER
;
170 /* Set access_list to number list. */
173 for (point
= list
->head
; point
; point
= point
->next
)
174 if (atol(point
->name
) >= number
)
177 new->sort
= COMMUNITY_LIST_STRING
;
179 /* Set access_list to string list. */
182 /* Set point to insertion point. */
183 for (point
= list
->head
; point
; point
= point
->next
)
184 if (strcmp(point
->name
, name
) >= 0)
188 /* Link to upper list. */
191 /* In case of this is the first element of master. */
192 if (list
->head
== NULL
) {
193 list
->head
= list
->tail
= new;
197 /* In case of insertion is made at the tail of access_list. */
199 new->prev
= list
->tail
;
200 list
->tail
->next
= new;
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;
213 /* Insertion is made at middle of the access_list. */
215 new->prev
= point
->prev
;
218 point
->prev
->next
= new;
224 struct community_list
*community_list_lookup(struct community_list_handler
*ch
,
229 struct community_list lookup
;
230 struct community_list_master
*cm
;
235 cm
= community_list_master_lookup(ch
, master
);
239 lookup
.name
= (char *)name
;
240 lookup
.name_hash
= name_hash
;
241 return hash_get(cm
->hash
, &lookup
, NULL
);
244 static struct community_list
*
245 community_list_get(struct community_list_handler
*ch
, const char *name
,
248 struct community_list
*list
;
250 list
= community_list_lookup(ch
, name
, 0, master
);
252 list
= community_list_insert(ch
, name
, master
);
256 static void community_list_delete(struct community_list_master
*cm
,
257 struct community_list
*list
)
259 struct community_list_list
*clist
;
260 struct community_entry
*entry
, *next
;
262 for (entry
= list
->head
; entry
; entry
= next
) {
264 community_entry_free(entry
);
267 clist
= list
->parent
;
270 list
->next
->prev
= list
->prev
;
272 clist
->tail
= list
->prev
;
275 list
->prev
->next
= list
->next
;
277 clist
->head
= list
->next
;
279 hash_release(cm
->hash
, list
);
280 community_list_free(list
);
283 static int community_list_empty_p(struct community_list
*list
)
285 return (list
->head
== NULL
&& list
->tail
== NULL
) ? 1 : 0;
288 /* Add community-list entry to the list. */
289 static void community_list_entry_add(struct community_list
*list
,
290 struct community_entry
*entry
)
293 entry
->prev
= list
->tail
;
296 list
->tail
->next
= entry
;
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
)
308 entry
->next
->prev
= entry
->prev
;
310 list
->tail
= entry
->prev
;
313 entry
->prev
->next
= entry
->next
;
315 list
->head
= entry
->next
;
317 community_entry_free(entry
);
319 if (community_list_empty_p(list
))
320 community_list_delete(cm
, list
);
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
,
328 struct community_entry
*entry
;
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
))
337 case EXTCOMMUNITY_LIST_STANDARD
:
338 if (entry
->direct
== direct
339 && ecommunity_cmp(entry
->u
.ecom
, arg
))
342 case LARGE_COMMUNITY_LIST_STANDARD
:
343 if (entry
->direct
== direct
344 && lcommunity_cmp(entry
->u
.lcom
, arg
))
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)
361 static char *community_str_get(struct community
*com
, int i
)
368 memcpy(&comval
, com_nthval(com
, i
), sizeof(uint32_t));
369 comval
= ntohl(comval
);
372 case COMMUNITY_INTERNET
:
373 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "internet");
375 case COMMUNITY_GSHUT
:
376 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "graceful-shutdown");
378 case COMMUNITY_ACCEPT_OWN
:
379 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "accept-own");
381 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
:
382 str
= XSTRDUP(MTYPE_COMMUNITY_STR
,
383 "route-filter-translated-v4");
385 case COMMUNITY_ROUTE_FILTER_v4
:
386 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "route-filter-v4");
388 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
:
389 str
= XSTRDUP(MTYPE_COMMUNITY_STR
,
390 "route-filter-translated-v6");
392 case COMMUNITY_ROUTE_FILTER_v6
:
393 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "route-filter-v6");
395 case COMMUNITY_LLGR_STALE
:
396 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "llgr-stale");
398 case COMMUNITY_NO_LLGR
:
399 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-llgr");
401 case COMMUNITY_ACCEPT_OWN_NEXTHOP
:
402 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "accept-own-nexthop");
404 case COMMUNITY_BLACKHOLE
:
405 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "blackhole");
407 case COMMUNITY_NO_EXPORT
:
408 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-export");
410 case COMMUNITY_NO_ADVERTISE
:
411 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-advertise");
413 case COMMUNITY_LOCAL_AS
:
414 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "local-AS");
416 case COMMUNITY_NO_PEER
:
417 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-peer");
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
);
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
)
437 /* When there is no communities attribute it is treated as empty string.
439 if (com
== NULL
|| com
->size
== 0)
440 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "");
442 str
= community_str_get(com
, i
);
444 /* Regular expression match. */
445 rv
= regexec(reg
, str
, 0, NULL
, 0);
447 XFREE(MTYPE_COMMUNITY_STR
, str
);
456 /* Internal function to perform regular expression match for community
458 static int community_regexp_match(struct community
*com
, regex_t
*reg
)
462 /* When there is no communities attribute it is treated as empty
464 if (com
== NULL
|| com
->size
== 0)
467 str
= community_str(com
, false);
469 /* Regular expression match. */
470 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
477 static char *lcommunity_str_get(struct lcommunity
*lcom
, int i
)
479 struct lcommunity_val lcomval
;
480 uint32_t globaladmin
;
487 ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
489 memcpy(&lcomval
, ptr
, LCOMMUNITY_SIZE
);
491 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
492 str
= pnt
= XMALLOC(MTYPE_LCOMMUNITY_STR
, 48);
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 */
500 sprintf(pnt
, "%u:%u:%u", globaladmin
, localdata1
, localdata2
);
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
,
514 /* When there is no communities attribute it is treated as empty string.
516 if (lcom
== NULL
|| lcom
->size
== 0)
517 str
= XSTRDUP(MTYPE_LCOMMUNITY_STR
, "");
519 str
= lcommunity_str_get(lcom
, i
);
521 /* Regular expression match. */
522 if (regexec(reg
, str
, 0, NULL
, 0) == 0) {
523 XFREE(MTYPE_LCOMMUNITY_STR
, str
);
527 XFREE(MTYPE_LCOMMUNITY_STR
, str
);
532 static int lcommunity_regexp_match(struct lcommunity
*com
, regex_t
*reg
)
536 /* When there is no communities attribute it is treated as empty
538 if (com
== NULL
|| com
->size
== 0)
541 str
= lcommunity_str(com
, false);
543 /* Regular expression match. */
544 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
552 static int ecommunity_regexp_match(struct ecommunity
*ecom
, regex_t
*reg
)
556 /* When there is no communities attribute it is treated as empty
558 if (ecom
== NULL
|| ecom
->size
== 0)
561 str
= ecommunity_str(ecom
);
563 /* Regular expression match. */
564 if (regexec(reg
, str
, 0, NULL
, 0) == 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
)
579 /* Maximum is "65535:65535" + '\0'. */
587 while (i
< com
->size
)
589 memcpy (&comval
, com_nthval (com
, i
), sizeof (uint32_t));
590 comval
= ntohl (comval
);
593 case COMMUNITY_INTERNET
:
596 case COMMUNITY_ACCEPT_OWN
:
599 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
:
600 str
= "route-filter-translated-v4";
602 case COMMUNITY_ROUTE_FILTER_v4
:
603 str
= "route-filter-v4";
605 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
:
606 str
= "route-filter-translated-v6";
608 case COMMUNITY_ROUTE_FILTER_v6
:
609 str
= "route-filter-v6";
611 case COMMUNITY_LLGR_STALE
:
614 case COMMUNITY_NO_LLGR
:
617 case COMMUNITY_ACCEPT_OWN_NEXTHOP
:
618 str
= "accept-own-nexthop";
620 case COMMUNITY_BLACKHOLE
:
623 case COMMUNITY_NO_EXPORT
:
626 case COMMUNITY_NO_ADVERTISE
:
627 str
= "no-advertise";
629 case COMMUNITY_LOCAL_AS
:
632 case COMMUNITY_NO_PEER
:
636 sprintf (c
, "%d:%d", (comval
>> 16) & 0xFFFF,
642 if (regexec (reg
, str
, 0, NULL
, 0) == 0)
643 community_del_val (com
, com_nthval (com
, i
));
651 /* When given community attribute matches to the community-list return
653 int community_list_match(struct community
*com
, struct community_list
*list
)
655 struct community_entry
*entry
;
657 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
659 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
661 if (entry
->style
== COMMUNITY_LIST_STANDARD
) {
662 if (community_include(entry
->u
.com
, COMMUNITY_INTERNET
))
663 return entry
->direct
== COMMUNITY_PERMIT
? 1
666 if (community_match(com
, entry
->u
.com
))
667 return entry
->direct
== COMMUNITY_PERMIT
? 1
669 } else if (entry
->style
== COMMUNITY_LIST_EXPANDED
) {
670 if (community_regexp_match(com
, entry
->reg
))
671 return entry
->direct
== COMMUNITY_PERMIT
? 1
678 int lcommunity_list_match(struct lcommunity
*lcom
, struct community_list
*list
)
680 struct community_entry
*entry
;
682 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
684 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
686 if (entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
) {
687 if (lcommunity_match(lcom
, entry
->u
.lcom
))
688 return entry
->direct
== COMMUNITY_PERMIT
? 1
690 } else if (entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
) {
691 if (lcommunity_regexp_match(lcom
, entry
->reg
))
692 return entry
->direct
== COMMUNITY_PERMIT
? 1
700 /* Perform exact matching. In case of expanded large-community-list, do
701 * same thing as lcommunity_list_match().
703 int lcommunity_list_exact_match(struct lcommunity
*lcom
,
704 struct community_list
*list
)
706 struct community_entry
*entry
;
708 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
710 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
712 if (entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
) {
713 if (lcommunity_cmp(lcom
, entry
->u
.com
))
714 return entry
->direct
== COMMUNITY_PERMIT
? 1
716 } else if (entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
) {
717 if (lcommunity_regexp_match(lcom
, entry
->reg
))
718 return entry
->direct
== COMMUNITY_PERMIT
? 1
725 int ecommunity_list_match(struct ecommunity
*ecom
, struct community_list
*list
)
727 struct community_entry
*entry
;
729 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
731 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
733 if (entry
->style
== EXTCOMMUNITY_LIST_STANDARD
) {
734 if (ecommunity_match(ecom
, entry
->u
.ecom
))
735 return entry
->direct
== COMMUNITY_PERMIT
? 1
737 } else if (entry
->style
== EXTCOMMUNITY_LIST_EXPANDED
) {
738 if (ecommunity_regexp_match(ecom
, entry
->reg
))
739 return entry
->direct
== COMMUNITY_PERMIT
? 1
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
)
751 struct community_entry
*entry
;
753 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
755 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
757 if (entry
->style
== COMMUNITY_LIST_STANDARD
) {
758 if (community_include(entry
->u
.com
, COMMUNITY_INTERNET
))
759 return entry
->direct
== COMMUNITY_PERMIT
? 1
762 if (community_cmp(com
, entry
->u
.com
))
763 return entry
->direct
== COMMUNITY_PERMIT
? 1
765 } else if (entry
->style
== COMMUNITY_LIST_EXPANDED
) {
766 if (community_regexp_match(com
, entry
->reg
))
767 return entry
->direct
== COMMUNITY_PERMIT
? 1
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
)
778 struct community_entry
*entry
;
780 uint32_t com_index_to_delete
[com
->size
];
781 int delete_index
= 0;
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.
788 for (i
= 0; i
< com
->size
; i
++) {
789 val
= community_val_get(com
, i
);
791 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
793 if (entry
->direct
== COMMUNITY_PERMIT
) {
794 com_index_to_delete
[delete_index
] = i
;
800 else if ((entry
->style
== COMMUNITY_LIST_STANDARD
)
801 && (community_include(entry
->u
.com
,
803 || community_include(entry
->u
.com
, val
))) {
804 if (entry
->direct
== COMMUNITY_PERMIT
) {
805 com_index_to_delete
[delete_index
] = i
;
811 else if ((entry
->style
== COMMUNITY_LIST_EXPANDED
)
812 && community_regexp_include(entry
->reg
, com
,
814 if (entry
->direct
== COMMUNITY_PERMIT
) {
815 com_index_to_delete
[delete_index
] = i
;
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 community_del_val(com
, &val
);
832 /* To avoid duplicated entry in the community-list, this function
833 compares specified entry to existing entry. */
834 static int community_list_dup_check(struct community_list
*list
,
835 struct community_entry
*new)
837 struct community_entry
*entry
;
839 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
840 if (entry
->style
!= new->style
)
843 if (entry
->direct
!= new->direct
)
846 if (entry
->any
!= new->any
)
852 switch (entry
->style
) {
853 case COMMUNITY_LIST_STANDARD
:
854 if (community_cmp(entry
->u
.com
, new->u
.com
))
857 case LARGE_COMMUNITY_LIST_STANDARD
:
858 if (lcommunity_cmp(entry
->u
.lcom
, new->u
.lcom
))
861 case EXTCOMMUNITY_LIST_STANDARD
:
862 if (ecommunity_cmp(entry
->u
.ecom
, new->u
.ecom
))
865 case COMMUNITY_LIST_EXPANDED
:
866 case EXTCOMMUNITY_LIST_EXPANDED
:
867 case LARGE_COMMUNITY_LIST_EXPANDED
:
868 if (strcmp(entry
->config
, new->config
) == 0)
878 /* Set community-list. */
879 int community_list_set(struct community_list_handler
*ch
, const char *name
,
880 const char *str
, int direct
, int style
)
882 struct community_entry
*entry
= NULL
;
883 struct community_list
*list
;
884 struct community
*com
= NULL
;
885 regex_t
*regex
= NULL
;
887 /* Get community list. */
888 list
= community_list_get(ch
, name
, COMMUNITY_LIST_MASTER
);
890 /* When community-list already has entry, new entry should have same
891 style. If you want to have mixed style community-list, you can
892 comment out this check. */
893 if (!community_list_empty_p(list
)) {
894 struct community_entry
*first
;
898 if (style
!= first
->style
) {
899 return (first
->style
== COMMUNITY_LIST_STANDARD
900 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
901 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
906 if (style
== COMMUNITY_LIST_STANDARD
)
907 com
= community_str2com(str
);
909 regex
= bgp_regcomp(str
);
912 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
915 entry
= community_entry_new();
916 entry
->direct
= direct
;
917 entry
->style
= style
;
918 entry
->any
= (str
? 0 : 1);
922 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
924 /* Do not put duplicated community entry. */
925 if (community_list_dup_check(list
, entry
))
926 community_entry_free(entry
);
928 community_list_entry_add(list
, entry
);
929 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_ADDED
);
935 /* Unset community-list */
936 int community_list_unset(struct community_list_handler
*ch
, const char *name
,
937 const char *str
, int direct
, int style
)
939 struct community_list_master
*cm
= NULL
;
940 struct community_entry
*entry
= NULL
;
941 struct community_list
*list
;
942 struct community
*com
= NULL
;
944 /* Lookup community list. */
945 list
= community_list_lookup(ch
, name
, 0, COMMUNITY_LIST_MASTER
);
947 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
949 cm
= community_list_master_lookup(ch
, COMMUNITY_LIST_MASTER
);
950 /* Delete all of entry belongs to this community-list. */
952 community_list_delete(cm
, list
);
953 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
957 if (style
== COMMUNITY_LIST_STANDARD
)
958 com
= community_str2com(str
);
961 entry
= community_list_entry_lookup(list
, com
, direct
);
962 community_free(&com
);
964 entry
= community_list_entry_lookup(list
, str
, direct
);
967 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
969 community_list_entry_delete(cm
, list
, entry
);
970 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
975 /* Delete all permitted large communities in the list from com. */
976 struct lcommunity
*lcommunity_list_match_delete(struct lcommunity
*lcom
,
977 struct community_list
*list
)
979 struct community_entry
*entry
;
980 uint32_t com_index_to_delete
[lcom
->size
];
982 int delete_index
= 0;
985 /* Loop over each lcommunity value and evaluate each against the
986 * community-list. If we need to delete a community value add its index
987 * to com_index_to_delete.
989 for (i
= 0; i
< lcom
->size
; i
++) {
990 ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
991 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
993 if (entry
->direct
== COMMUNITY_PERMIT
) {
994 com_index_to_delete
[delete_index
] = i
;
1000 else if ((entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
)
1001 && lcommunity_include(entry
->u
.lcom
, ptr
)) {
1002 if (entry
->direct
== COMMUNITY_PERMIT
) {
1003 com_index_to_delete
[delete_index
] = i
;
1009 else if ((entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
)
1010 && lcommunity_regexp_include(entry
->reg
, lcom
,
1012 if (entry
->direct
== COMMUNITY_PERMIT
) {
1013 com_index_to_delete
[delete_index
] = i
;
1021 /* Delete all of the communities we flagged for deletion */
1022 for (i
= delete_index
- 1; i
>= 0; i
--) {
1023 ptr
= lcom
->val
+ (com_index_to_delete
[i
] * LCOMMUNITY_SIZE
);
1024 lcommunity_del_val(lcom
, ptr
);
1030 /* Helper to check if every octet do not exceed UINT_MAX */
1031 static int lcommunity_list_valid(const char *community
)
1037 frrstr_split(community
, ":", &splits
, &num
);
1039 for (int i
= 0; i
< num
; i
++) {
1040 if (strtoul(splits
[i
], NULL
, 10) > UINT_MAX
)
1043 if (strlen(splits
[i
]) == 0)
1047 XFREE(MTYPE_TMP
, splits
[i
]);
1049 XFREE(MTYPE_TMP
, splits
);
1057 /* Set lcommunity-list. */
1058 int lcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
1059 const char *str
, int direct
, int style
)
1061 struct community_entry
*entry
= NULL
;
1062 struct community_list
*list
;
1063 struct lcommunity
*lcom
= NULL
;
1064 regex_t
*regex
= NULL
;
1066 /* Get community list. */
1067 list
= community_list_get(ch
, name
, LARGE_COMMUNITY_LIST_MASTER
);
1069 /* When community-list already has entry, new entry should have same
1070 style. If you want to have mixed style community-list, you can
1071 comment out this check. */
1072 if (!community_list_empty_p(list
)) {
1073 struct community_entry
*first
;
1077 if (style
!= first
->style
) {
1078 return (first
->style
== COMMUNITY_LIST_STANDARD
1079 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1080 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
1085 if (!lcommunity_list_valid(str
))
1086 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1088 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
1089 lcom
= lcommunity_str2com(str
);
1091 regex
= bgp_regcomp(str
);
1093 if (!lcom
&& !regex
)
1094 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1097 entry
= community_entry_new();
1098 entry
->direct
= direct
;
1099 entry
->style
= style
;
1100 entry
->any
= (str
? 0 : 1);
1101 entry
->u
.lcom
= lcom
;
1104 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
1106 /* Do not put duplicated community entry. */
1107 if (community_list_dup_check(list
, entry
))
1108 community_entry_free(entry
);
1110 community_list_entry_add(list
, entry
);
1111 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_ADDED
);
1117 /* Unset community-list. When str is NULL, delete all of
1118 community-list entry belongs to the specified name. */
1119 int lcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1120 const char *str
, int direct
, int style
)
1122 struct community_list_master
*cm
= NULL
;
1123 struct community_entry
*entry
= NULL
;
1124 struct community_list
*list
;
1125 struct lcommunity
*lcom
= NULL
;
1126 regex_t
*regex
= NULL
;
1128 /* Lookup community list. */
1129 list
= community_list_lookup(ch
, name
, 0, LARGE_COMMUNITY_LIST_MASTER
);
1131 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1133 cm
= community_list_master_lookup(ch
, LARGE_COMMUNITY_LIST_MASTER
);
1134 /* Delete all of entry belongs to this community-list. */
1136 community_list_delete(cm
, list
);
1137 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_DELETED
);
1141 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
1142 lcom
= lcommunity_str2com(str
);
1144 regex
= bgp_regcomp(str
);
1146 if (!lcom
&& !regex
)
1147 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1150 entry
= community_list_entry_lookup(list
, lcom
, direct
);
1152 entry
= community_list_entry_lookup(list
, str
, direct
);
1155 lcommunity_free(&lcom
);
1157 bgp_regex_free(regex
);
1160 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1162 community_list_entry_delete(cm
, list
, entry
);
1163 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_DELETED
);
1168 /* Set extcommunity-list. */
1169 int extcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
1170 const char *str
, int direct
, int style
)
1172 struct community_entry
*entry
= NULL
;
1173 struct community_list
*list
;
1174 struct ecommunity
*ecom
= NULL
;
1175 regex_t
*regex
= NULL
;
1178 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1180 /* Get community list. */
1181 list
= community_list_get(ch
, name
, EXTCOMMUNITY_LIST_MASTER
);
1183 /* When community-list already has entry, new entry should have same
1184 style. If you want to have mixed style community-list, you can
1185 comment out this check. */
1186 if (!community_list_empty_p(list
)) {
1187 struct community_entry
*first
;
1191 if (style
!= first
->style
) {
1192 return (first
->style
== EXTCOMMUNITY_LIST_STANDARD
1193 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1194 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
1198 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1199 ecom
= ecommunity_str2com(str
, 0, 1);
1201 regex
= bgp_regcomp(str
);
1203 if (!ecom
&& !regex
)
1204 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1208 ecommunity_ecom2str(ecom
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1210 entry
= community_entry_new();
1211 entry
->direct
= direct
;
1212 entry
->style
= style
;
1215 entry
->config
= ecommunity_ecom2str(
1216 ecom
, ECOMMUNITY_FORMAT_COMMUNITY_LIST
, 0);
1218 entry
->config
= XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
);
1220 entry
->u
.ecom
= ecom
;
1223 /* Do not put duplicated community entry. */
1224 if (community_list_dup_check(list
, entry
))
1225 community_entry_free(entry
);
1227 community_list_entry_add(list
, entry
);
1228 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_ADDED
);
1234 /* Unset extcommunity-list.
1236 * When str is NULL, delete all extcommunity-list entries belonging to the
1239 int extcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1240 const char *str
, int direct
, int style
)
1242 struct community_list_master
*cm
= NULL
;
1243 struct community_entry
*entry
= NULL
;
1244 struct community_list
*list
;
1245 struct ecommunity
*ecom
= NULL
;
1247 /* Lookup extcommunity list. */
1248 list
= community_list_lookup(ch
, name
, 0, EXTCOMMUNITY_LIST_MASTER
);
1250 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1252 cm
= community_list_master_lookup(ch
, EXTCOMMUNITY_LIST_MASTER
);
1253 /* Delete all of entry belongs to this extcommunity-list. */
1255 community_list_delete(cm
, list
);
1256 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1260 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1261 ecom
= ecommunity_str2com(str
, 0, 1);
1264 entry
= community_list_entry_lookup(list
, ecom
, direct
);
1265 ecommunity_free(&ecom
);
1267 entry
= community_list_entry_lookup(list
, str
, direct
);
1270 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1272 community_list_entry_delete(cm
, list
, entry
);
1273 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1278 /* Initializa community-list. Return community-list handler. */
1279 struct community_list_handler
*community_list_init(void)
1281 struct community_list_handler
*ch
;
1282 ch
= XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER
,
1283 sizeof(struct community_list_handler
));
1285 ch
->community_list
.hash
=
1286 hash_create_size(4, bgp_clist_hash_key_community_list
,
1287 bgp_clist_hash_cmp_community_list
,
1288 "Community List Number Quick Lookup");
1290 ch
->extcommunity_list
.hash
=
1291 hash_create_size(4, bgp_clist_hash_key_community_list
,
1292 bgp_clist_hash_cmp_community_list
,
1293 "Extended Community List Quick Lookup");
1295 ch
->lcommunity_list
.hash
=
1296 hash_create_size(4, bgp_clist_hash_key_community_list
,
1297 bgp_clist_hash_cmp_community_list
,
1298 "Large Community List Quick Lookup");
1303 /* Terminate community-list. */
1304 void community_list_terminate(struct community_list_handler
*ch
)
1306 struct community_list_master
*cm
;
1307 struct community_list
*list
;
1309 cm
= &ch
->community_list
;
1310 while ((list
= cm
->num
.head
) != NULL
)
1311 community_list_delete(cm
, list
);
1312 while ((list
= cm
->str
.head
) != NULL
)
1313 community_list_delete(cm
, list
);
1314 hash_free(cm
->hash
);
1316 cm
= &ch
->lcommunity_list
;
1317 while ((list
= cm
->num
.head
) != NULL
)
1318 community_list_delete(cm
, list
);
1319 while ((list
= cm
->str
.head
) != NULL
)
1320 community_list_delete(cm
, list
);
1321 hash_free(cm
->hash
);
1323 cm
= &ch
->extcommunity_list
;
1324 while ((list
= cm
->num
.head
) != NULL
)
1325 community_list_delete(cm
, list
);
1326 while ((list
= cm
->str
.head
) != NULL
)
1327 community_list_delete(cm
, list
);
1328 hash_free(cm
->hash
);
1330 XFREE(MTYPE_COMMUNITY_LIST_HANDLER
, ch
);