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
31 #include "bgpd/bgpd.h"
32 #include "bgpd/bgp_community.h"
33 #include "bgpd/bgp_ecommunity.h"
34 #include "bgpd/bgp_lcommunity.h"
35 #include "bgpd/bgp_aspath.h"
36 #include "bgpd/bgp_regex.h"
37 #include "bgpd/bgp_clist.h"
39 static uint32_t bgp_clist_hash_key_community_list(const void *data
)
41 struct community_list
*cl
= (struct community_list
*) data
;
46 cl
->name_hash
= bgp_clist_hash_key(cl
->name
);
50 static bool bgp_clist_hash_cmp_community_list(const void *a1
, const void *a2
)
52 const struct community_list
*cl1
= a1
;
53 const struct community_list
*cl2
= a2
;
55 if (cl1
->name_hash
!= cl2
->name_hash
)
58 if (strcmp(cl1
->name
, cl2
->name
) == 0)
64 /* Lookup master structure for community-list or
66 struct community_list_master
*
67 community_list_master_lookup(struct community_list_handler
*ch
, int master
)
71 case COMMUNITY_LIST_MASTER
:
72 return &ch
->community_list
;
73 case EXTCOMMUNITY_LIST_MASTER
:
74 return &ch
->extcommunity_list
;
75 case LARGE_COMMUNITY_LIST_MASTER
:
76 return &ch
->lcommunity_list
;
81 /* Allocate a new community list entry. */
82 static struct community_entry
*community_entry_new(void)
84 return XCALLOC(MTYPE_COMMUNITY_LIST_ENTRY
,
85 sizeof(struct community_entry
));
88 /* Free community list entry. */
89 static void community_entry_free(struct community_entry
*entry
)
91 switch (entry
->style
) {
92 case COMMUNITY_LIST_STANDARD
:
94 community_free(&entry
->u
.com
);
96 case LARGE_COMMUNITY_LIST_STANDARD
:
98 lcommunity_free(&entry
->u
.lcom
);
100 case EXTCOMMUNITY_LIST_STANDARD
:
101 /* In case of standard extcommunity-list, configuration string
102 is made by ecommunity_ecom2str(). */
103 XFREE(MTYPE_ECOMMUNITY_STR
, entry
->config
);
105 ecommunity_free(&entry
->u
.ecom
);
107 case COMMUNITY_LIST_EXPANDED
:
108 case EXTCOMMUNITY_LIST_EXPANDED
:
109 case LARGE_COMMUNITY_LIST_EXPANDED
:
110 XFREE(MTYPE_COMMUNITY_LIST_CONFIG
, entry
->config
);
112 bgp_regex_free(entry
->reg
);
116 XFREE(MTYPE_COMMUNITY_LIST_ENTRY
, entry
);
119 /* Allocate a new community-list. */
120 static struct community_list
*community_list_new(void)
122 return XCALLOC(MTYPE_COMMUNITY_LIST
, sizeof(struct community_list
));
125 /* Free community-list. */
126 static void community_list_free(struct community_list
*list
)
128 XFREE(MTYPE_COMMUNITY_LIST_NAME
, list
->name
);
129 XFREE(MTYPE_COMMUNITY_LIST
, list
);
132 static struct community_list
*
133 community_list_insert(struct community_list_handler
*ch
, const char *name
,
138 struct community_list
*new;
139 struct community_list
*point
;
140 struct community_list_list
*list
;
141 struct community_list_master
*cm
;
143 /* Lookup community-list master. */
144 cm
= community_list_master_lookup(ch
, master
);
148 /* Allocate new community_list and copy given name. */
149 new = community_list_new();
150 new->name
= XSTRDUP(MTYPE_COMMUNITY_LIST_NAME
, name
);
151 new->name_hash
= bgp_clist_hash_key_community_list(new);
154 hash_get(cm
->hash
, new, hash_alloc_intern
);
156 /* If name is made by all digit character. We treat it as
158 for (number
= 0, i
= 0; i
< strlen(name
); i
++) {
159 if (isdigit((int)name
[i
]))
160 number
= (number
* 10) + (name
[i
] - '0');
165 /* In case of name is all digit character */
166 if (i
== strlen(name
)) {
167 new->sort
= COMMUNITY_LIST_NUMBER
;
169 /* Set access_list to number list. */
172 for (point
= list
->head
; point
; point
= point
->next
)
173 if (atol(point
->name
) >= number
)
176 new->sort
= COMMUNITY_LIST_STRING
;
178 /* Set access_list to string list. */
181 /* Set point to insertion point. */
182 for (point
= list
->head
; point
; point
= point
->next
)
183 if (strcmp(point
->name
, name
) >= 0)
187 /* Link to upper list. */
190 /* In case of this is the first element of master. */
191 if (list
->head
== NULL
) {
192 list
->head
= list
->tail
= new;
196 /* In case of insertion is made at the tail of access_list. */
198 new->prev
= list
->tail
;
199 list
->tail
->next
= new;
204 /* In case of insertion is made at the head of access_list. */
205 if (point
== list
->head
) {
206 new->next
= list
->head
;
207 list
->head
->prev
= new;
212 /* Insertion is made at middle of the access_list. */
214 new->prev
= point
->prev
;
217 point
->prev
->next
= new;
223 struct community_list
*community_list_lookup(struct community_list_handler
*ch
,
228 struct community_list lookup
;
229 struct community_list_master
*cm
;
234 cm
= community_list_master_lookup(ch
, master
);
238 lookup
.name
= (char *)name
;
239 lookup
.name_hash
= name_hash
;
240 return hash_get(cm
->hash
, &lookup
, NULL
);
243 static struct community_list
*
244 community_list_get(struct community_list_handler
*ch
, const char *name
,
247 struct community_list
*list
;
249 list
= community_list_lookup(ch
, name
, 0, master
);
251 list
= community_list_insert(ch
, name
, master
);
255 static void community_list_delete(struct community_list_master
*cm
,
256 struct community_list
*list
)
258 struct community_list_list
*clist
;
259 struct community_entry
*entry
, *next
;
261 for (entry
= list
->head
; entry
; entry
= next
) {
263 community_entry_free(entry
);
266 clist
= list
->parent
;
269 list
->next
->prev
= list
->prev
;
271 clist
->tail
= list
->prev
;
274 list
->prev
->next
= list
->next
;
276 clist
->head
= list
->next
;
278 hash_release(cm
->hash
, list
);
279 community_list_free(list
);
282 static int community_list_empty_p(struct community_list
*list
)
284 return (list
->head
== NULL
&& list
->tail
== NULL
) ? 1 : 0;
287 /* Add community-list entry to the list. */
288 static void community_list_entry_add(struct community_list
*list
,
289 struct community_entry
*entry
)
292 entry
->prev
= list
->tail
;
295 list
->tail
->next
= entry
;
301 /* Delete community-list entry from the list. */
302 static void community_list_entry_delete(struct community_list_master
*cm
,
303 struct community_list
*list
,
304 struct community_entry
*entry
)
307 entry
->next
->prev
= entry
->prev
;
309 list
->tail
= entry
->prev
;
312 entry
->prev
->next
= entry
->next
;
314 list
->head
= entry
->next
;
316 community_entry_free(entry
);
318 if (community_list_empty_p(list
))
319 community_list_delete(cm
, list
);
322 /* Lookup community-list entry from the list. */
323 static struct community_entry
*
324 community_list_entry_lookup(struct community_list
*list
, const void *arg
,
327 struct community_entry
*entry
;
329 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
330 switch (entry
->style
) {
331 case COMMUNITY_LIST_STANDARD
:
332 if (entry
->direct
== direct
333 && community_cmp(entry
->u
.com
, arg
))
336 case EXTCOMMUNITY_LIST_STANDARD
:
337 if (entry
->direct
== direct
338 && ecommunity_cmp(entry
->u
.ecom
, arg
))
341 case LARGE_COMMUNITY_LIST_STANDARD
:
342 if (entry
->direct
== direct
343 && lcommunity_cmp(entry
->u
.lcom
, arg
))
346 case COMMUNITY_LIST_EXPANDED
:
347 case EXTCOMMUNITY_LIST_EXPANDED
:
348 case LARGE_COMMUNITY_LIST_EXPANDED
:
349 if (entry
->direct
== direct
350 && strcmp(entry
->config
, arg
) == 0)
360 static char *community_str_get(struct community
*com
, int i
)
367 memcpy(&comval
, com_nthval(com
, i
), sizeof(uint32_t));
368 comval
= ntohl(comval
);
371 case COMMUNITY_INTERNET
:
372 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "internet");
374 case COMMUNITY_GSHUT
:
375 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "graceful-shutdown");
377 case COMMUNITY_ACCEPT_OWN
:
378 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "accept-own");
380 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
:
381 str
= XSTRDUP(MTYPE_COMMUNITY_STR
,
382 "route-filter-translated-v4");
384 case COMMUNITY_ROUTE_FILTER_v4
:
385 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "route-filter-v4");
387 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
:
388 str
= XSTRDUP(MTYPE_COMMUNITY_STR
,
389 "route-filter-translated-v6");
391 case COMMUNITY_ROUTE_FILTER_v6
:
392 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "route-filter-v6");
394 case COMMUNITY_LLGR_STALE
:
395 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "llgr-stale");
397 case COMMUNITY_NO_LLGR
:
398 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-llgr");
400 case COMMUNITY_ACCEPT_OWN_NEXTHOP
:
401 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "accept-own-nexthop");
403 case COMMUNITY_BLACKHOLE
:
404 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "blackhole");
406 case COMMUNITY_NO_EXPORT
:
407 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-export");
409 case COMMUNITY_NO_ADVERTISE
:
410 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-advertise");
412 case COMMUNITY_LOCAL_AS
:
413 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "local-AS");
415 case COMMUNITY_NO_PEER
:
416 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "no-peer");
419 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "65536:65535");
420 as
= (comval
>> 16) & 0xFFFF;
421 val
= comval
& 0xFFFF;
422 snprintf(str
, strlen(str
), "%u:%d", as
, val
);
429 /* Internal function to perform regular expression match for
430 * a single community. */
431 static int community_regexp_include(regex_t
*reg
, struct community
*com
, int i
)
436 /* When there is no communities attribute it is treated as empty string.
438 if (com
== NULL
|| com
->size
== 0)
439 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "");
441 str
= community_str_get(com
, i
);
443 /* Regular expression match. */
444 rv
= regexec(reg
, str
, 0, NULL
, 0);
446 XFREE(MTYPE_COMMUNITY_STR
, str
);
455 /* Internal function to perform regular expression match for community
457 static int community_regexp_match(struct community
*com
, regex_t
*reg
)
461 /* When there is no communities attribute it is treated as empty
463 if (com
== NULL
|| com
->size
== 0)
466 str
= community_str(com
, false);
468 /* Regular expression match. */
469 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
476 static char *lcommunity_str_get(struct lcommunity
*lcom
, int i
)
478 struct lcommunity_val lcomval
;
479 uint32_t globaladmin
;
486 ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
488 memcpy(&lcomval
, ptr
, LCOMMUNITY_SIZE
);
490 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
491 str
= pnt
= XMALLOC(MTYPE_LCOMMUNITY_STR
, 48);
493 ptr
= (uint8_t *)lcomval
.val
;
494 ptr
= ptr_get_be32(ptr
, &globaladmin
);
495 ptr
= ptr_get_be32(ptr
, &localdata1
);
496 ptr
= ptr_get_be32(ptr
, &localdata2
);
497 (void)ptr
; /* consume value */
499 sprintf(pnt
, "%u:%u:%u", globaladmin
, localdata1
, localdata2
);
506 /* Internal function to perform regular expression match for
507 * a single community. */
508 static int lcommunity_regexp_include(regex_t
*reg
, struct lcommunity
*lcom
,
513 /* When there is no communities attribute it is treated as empty string.
515 if (lcom
== NULL
|| lcom
->size
== 0)
516 str
= XSTRDUP(MTYPE_LCOMMUNITY_STR
, "");
518 str
= lcommunity_str_get(lcom
, i
);
520 /* Regular expression match. */
521 if (regexec(reg
, str
, 0, NULL
, 0) == 0) {
522 XFREE(MTYPE_LCOMMUNITY_STR
, str
);
526 XFREE(MTYPE_LCOMMUNITY_STR
, str
);
531 static int lcommunity_regexp_match(struct lcommunity
*com
, regex_t
*reg
)
535 /* When there is no communities attribute it is treated as empty
537 if (com
== NULL
|| com
->size
== 0)
540 str
= lcommunity_str(com
, false);
542 /* Regular expression match. */
543 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
551 static int ecommunity_regexp_match(struct ecommunity
*ecom
, regex_t
*reg
)
555 /* When there is no communities attribute it is treated as empty
557 if (ecom
== NULL
|| ecom
->size
== 0)
560 str
= ecommunity_str(ecom
);
562 /* Regular expression match. */
563 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
571 /* Delete community attribute using regular expression match. Return
572 modified communites attribute. */
573 static struct community
*
574 community_regexp_delete (struct community
*com
, regex_t
* reg
)
578 /* Maximum is "65535:65535" + '\0'. */
586 while (i
< com
->size
)
588 memcpy (&comval
, com_nthval (com
, i
), sizeof (uint32_t));
589 comval
= ntohl (comval
);
592 case COMMUNITY_INTERNET
:
595 case COMMUNITY_ACCEPT_OWN
:
598 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
:
599 str
= "route-filter-translated-v4";
601 case COMMUNITY_ROUTE_FILTER_v4
:
602 str
= "route-filter-v4";
604 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
:
605 str
= "route-filter-translated-v6";
607 case COMMUNITY_ROUTE_FILTER_v6
:
608 str
= "route-filter-v6";
610 case COMMUNITY_LLGR_STALE
:
613 case COMMUNITY_NO_LLGR
:
616 case COMMUNITY_ACCEPT_OWN_NEXTHOP
:
617 str
= "accept-own-nexthop";
619 case COMMUNITY_BLACKHOLE
:
622 case COMMUNITY_NO_EXPORT
:
625 case COMMUNITY_NO_ADVERTISE
:
626 str
= "no-advertise";
628 case COMMUNITY_LOCAL_AS
:
631 case COMMUNITY_NO_PEER
:
635 sprintf (c
, "%d:%d", (comval
>> 16) & 0xFFFF,
641 if (regexec (reg
, str
, 0, NULL
, 0) == 0)
642 community_del_val (com
, com_nthval (com
, i
));
650 /* When given community attribute matches to the community-list return
652 int community_list_match(struct community
*com
, struct community_list
*list
)
654 struct community_entry
*entry
;
656 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
658 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
660 if (entry
->style
== COMMUNITY_LIST_STANDARD
) {
661 if (community_include(entry
->u
.com
, COMMUNITY_INTERNET
))
662 return entry
->direct
== COMMUNITY_PERMIT
? 1
665 if (community_match(com
, entry
->u
.com
))
666 return entry
->direct
== COMMUNITY_PERMIT
? 1
668 } else if (entry
->style
== COMMUNITY_LIST_EXPANDED
) {
669 if (community_regexp_match(com
, entry
->reg
))
670 return entry
->direct
== COMMUNITY_PERMIT
? 1
677 int lcommunity_list_match(struct lcommunity
*lcom
, struct community_list
*list
)
679 struct community_entry
*entry
;
681 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
683 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
685 if (entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
) {
686 if (lcommunity_match(lcom
, entry
->u
.lcom
))
687 return entry
->direct
== COMMUNITY_PERMIT
? 1
689 } else if (entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
) {
690 if (lcommunity_regexp_match(lcom
, entry
->reg
))
691 return entry
->direct
== COMMUNITY_PERMIT
? 1
698 int ecommunity_list_match(struct ecommunity
*ecom
, struct community_list
*list
)
700 struct community_entry
*entry
;
702 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
704 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
706 if (entry
->style
== EXTCOMMUNITY_LIST_STANDARD
) {
707 if (ecommunity_match(ecom
, entry
->u
.ecom
))
708 return entry
->direct
== COMMUNITY_PERMIT
? 1
710 } else if (entry
->style
== EXTCOMMUNITY_LIST_EXPANDED
) {
711 if (ecommunity_regexp_match(ecom
, entry
->reg
))
712 return entry
->direct
== COMMUNITY_PERMIT
? 1
719 /* Perform exact matching. In case of expanded community-list, do
720 same thing as community_list_match(). */
721 int community_list_exact_match(struct community
*com
,
722 struct community_list
*list
)
724 struct community_entry
*entry
;
726 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
728 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
730 if (entry
->style
== COMMUNITY_LIST_STANDARD
) {
731 if (community_include(entry
->u
.com
, COMMUNITY_INTERNET
))
732 return entry
->direct
== COMMUNITY_PERMIT
? 1
735 if (community_cmp(com
, entry
->u
.com
))
736 return entry
->direct
== COMMUNITY_PERMIT
? 1
738 } else if (entry
->style
== COMMUNITY_LIST_EXPANDED
) {
739 if (community_regexp_match(com
, entry
->reg
))
740 return entry
->direct
== COMMUNITY_PERMIT
? 1
747 /* Delete all permitted communities in the list from com. */
748 struct community
*community_list_match_delete(struct community
*com
,
749 struct community_list
*list
)
751 struct community_entry
*entry
;
753 uint32_t com_index_to_delete
[com
->size
];
754 int delete_index
= 0;
757 /* Loop over each community value and evaluate each against the
758 * community-list. If we need to delete a community value add its index
759 * to com_index_to_delete.
761 for (i
= 0; i
< com
->size
; i
++) {
762 val
= community_val_get(com
, i
);
764 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
766 if (entry
->direct
== COMMUNITY_PERMIT
) {
767 com_index_to_delete
[delete_index
] = i
;
773 else if ((entry
->style
== COMMUNITY_LIST_STANDARD
)
774 && (community_include(entry
->u
.com
,
776 || community_include(entry
->u
.com
, val
))) {
777 if (entry
->direct
== COMMUNITY_PERMIT
) {
778 com_index_to_delete
[delete_index
] = i
;
784 else if ((entry
->style
== COMMUNITY_LIST_EXPANDED
)
785 && community_regexp_include(entry
->reg
, com
,
787 if (entry
->direct
== COMMUNITY_PERMIT
) {
788 com_index_to_delete
[delete_index
] = i
;
796 /* Delete all of the communities we flagged for deletion */
797 for (i
= delete_index
- 1; i
>= 0; i
--) {
798 val
= community_val_get(com
, com_index_to_delete
[i
]);
799 community_del_val(com
, &val
);
805 /* To avoid duplicated entry in the community-list, this function
806 compares specified entry to existing entry. */
807 static int community_list_dup_check(struct community_list
*list
,
808 struct community_entry
*new)
810 struct community_entry
*entry
;
812 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
813 if (entry
->style
!= new->style
)
816 if (entry
->direct
!= new->direct
)
819 if (entry
->any
!= new->any
)
825 switch (entry
->style
) {
826 case COMMUNITY_LIST_STANDARD
:
827 if (community_cmp(entry
->u
.com
, new->u
.com
))
830 case LARGE_COMMUNITY_LIST_STANDARD
:
831 if (lcommunity_cmp(entry
->u
.lcom
, new->u
.lcom
))
834 case EXTCOMMUNITY_LIST_STANDARD
:
835 if (ecommunity_cmp(entry
->u
.ecom
, new->u
.ecom
))
838 case COMMUNITY_LIST_EXPANDED
:
839 case EXTCOMMUNITY_LIST_EXPANDED
:
840 case LARGE_COMMUNITY_LIST_EXPANDED
:
841 if (strcmp(entry
->config
, new->config
) == 0)
851 /* Set community-list. */
852 int community_list_set(struct community_list_handler
*ch
, const char *name
,
853 const char *str
, int direct
, int style
)
855 struct community_entry
*entry
= NULL
;
856 struct community_list
*list
;
857 struct community
*com
= NULL
;
858 regex_t
*regex
= NULL
;
860 /* Get community list. */
861 list
= community_list_get(ch
, name
, COMMUNITY_LIST_MASTER
);
863 /* When community-list already has entry, new entry should have same
864 style. If you want to have mixed style community-list, you can
865 comment out this check. */
866 if (!community_list_empty_p(list
)) {
867 struct community_entry
*first
;
871 if (style
!= first
->style
) {
872 return (first
->style
== COMMUNITY_LIST_STANDARD
873 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
874 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
879 if (style
== COMMUNITY_LIST_STANDARD
)
880 com
= community_str2com(str
);
882 regex
= bgp_regcomp(str
);
885 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
888 entry
= community_entry_new();
889 entry
->direct
= direct
;
890 entry
->style
= style
;
891 entry
->any
= (str
? 0 : 1);
895 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
897 /* Do not put duplicated community entry. */
898 if (community_list_dup_check(list
, entry
))
899 community_entry_free(entry
);
901 community_list_entry_add(list
, entry
);
902 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_ADDED
);
908 /* Unset community-list */
909 int community_list_unset(struct community_list_handler
*ch
, const char *name
,
910 const char *str
, int direct
, int style
)
912 struct community_list_master
*cm
= NULL
;
913 struct community_entry
*entry
= NULL
;
914 struct community_list
*list
;
915 struct community
*com
= NULL
;
917 /* Lookup community list. */
918 list
= community_list_lookup(ch
, name
, 0, COMMUNITY_LIST_MASTER
);
920 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
922 cm
= community_list_master_lookup(ch
, COMMUNITY_LIST_MASTER
);
923 /* Delete all of entry belongs to this community-list. */
925 community_list_delete(cm
, list
);
926 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
930 if (style
== COMMUNITY_LIST_STANDARD
)
931 com
= community_str2com(str
);
934 entry
= community_list_entry_lookup(list
, com
, direct
);
935 community_free(&com
);
937 entry
= community_list_entry_lookup(list
, str
, direct
);
940 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
942 community_list_entry_delete(cm
, list
, entry
);
943 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
948 /* Delete all permitted large communities in the list from com. */
949 struct lcommunity
*lcommunity_list_match_delete(struct lcommunity
*lcom
,
950 struct community_list
*list
)
952 struct community_entry
*entry
;
953 uint32_t com_index_to_delete
[lcom
->size
];
955 int delete_index
= 0;
958 /* Loop over each lcommunity value and evaluate each against the
959 * community-list. If we need to delete a community value add its index
960 * to com_index_to_delete.
962 for (i
= 0; i
< lcom
->size
; i
++) {
963 ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
964 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
966 if (entry
->direct
== COMMUNITY_PERMIT
) {
967 com_index_to_delete
[delete_index
] = i
;
973 else if ((entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
)
974 && lcommunity_include(entry
->u
.lcom
, ptr
)) {
975 if (entry
->direct
== COMMUNITY_PERMIT
) {
976 com_index_to_delete
[delete_index
] = i
;
982 else if ((entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
)
983 && lcommunity_regexp_include(entry
->reg
, lcom
,
985 if (entry
->direct
== COMMUNITY_PERMIT
) {
986 com_index_to_delete
[delete_index
] = i
;
994 /* Delete all of the communities we flagged for deletion */
995 for (i
= delete_index
- 1; i
>= 0; i
--) {
996 ptr
= lcom
->val
+ (com_index_to_delete
[i
] * LCOMMUNITY_SIZE
);
997 lcommunity_del_val(lcom
, ptr
);
1003 /* Set lcommunity-list. */
1004 int lcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
1005 const char *str
, int direct
, int style
)
1007 struct community_entry
*entry
= NULL
;
1008 struct community_list
*list
;
1009 struct lcommunity
*lcom
= NULL
;
1010 regex_t
*regex
= NULL
;
1012 /* Get community list. */
1013 list
= community_list_get(ch
, name
, LARGE_COMMUNITY_LIST_MASTER
);
1015 /* When community-list already has entry, new entry should have same
1016 style. If you want to have mixed style community-list, you can
1017 comment out this check. */
1018 if (!community_list_empty_p(list
)) {
1019 struct community_entry
*first
;
1023 if (style
!= first
->style
) {
1024 return (first
->style
== COMMUNITY_LIST_STANDARD
1025 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1026 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
1031 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
1032 lcom
= lcommunity_str2com(str
);
1034 regex
= bgp_regcomp(str
);
1036 if (!lcom
&& !regex
)
1037 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1040 entry
= community_entry_new();
1041 entry
->direct
= direct
;
1042 entry
->style
= style
;
1043 entry
->any
= (str
? 0 : 1);
1044 entry
->u
.lcom
= lcom
;
1047 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
1049 /* Do not put duplicated community entry. */
1050 if (community_list_dup_check(list
, entry
))
1051 community_entry_free(entry
);
1053 community_list_entry_add(list
, entry
);
1054 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_ADDED
);
1060 /* Unset community-list. When str is NULL, delete all of
1061 community-list entry belongs to the specified name. */
1062 int lcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1063 const char *str
, int direct
, int style
)
1065 struct community_list_master
*cm
= NULL
;
1066 struct community_entry
*entry
= NULL
;
1067 struct community_list
*list
;
1068 struct lcommunity
*lcom
= NULL
;
1069 regex_t
*regex
= NULL
;
1071 /* Lookup community list. */
1072 list
= community_list_lookup(ch
, name
, 0, LARGE_COMMUNITY_LIST_MASTER
);
1074 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1076 cm
= community_list_master_lookup(ch
, LARGE_COMMUNITY_LIST_MASTER
);
1077 /* Delete all of entry belongs to this community-list. */
1079 community_list_delete(cm
, list
);
1080 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_DELETED
);
1084 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
1085 lcom
= lcommunity_str2com(str
);
1087 regex
= bgp_regcomp(str
);
1089 if (!lcom
&& !regex
)
1090 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1093 entry
= community_list_entry_lookup(list
, lcom
, direct
);
1095 entry
= community_list_entry_lookup(list
, str
, direct
);
1098 lcommunity_free(&lcom
);
1100 bgp_regex_free(regex
);
1103 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1105 community_list_entry_delete(cm
, list
, entry
);
1106 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_DELETED
);
1111 /* Set extcommunity-list. */
1112 int extcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
1113 const char *str
, int direct
, int style
)
1115 struct community_entry
*entry
= NULL
;
1116 struct community_list
*list
;
1117 struct ecommunity
*ecom
= NULL
;
1118 regex_t
*regex
= NULL
;
1121 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1123 /* Get community list. */
1124 list
= community_list_get(ch
, name
, EXTCOMMUNITY_LIST_MASTER
);
1126 /* When community-list already has entry, new entry should have same
1127 style. If you want to have mixed style community-list, you can
1128 comment out this check. */
1129 if (!community_list_empty_p(list
)) {
1130 struct community_entry
*first
;
1134 if (style
!= first
->style
) {
1135 return (first
->style
== EXTCOMMUNITY_LIST_STANDARD
1136 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1137 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
1141 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1142 ecom
= ecommunity_str2com(str
, 0, 1);
1144 regex
= bgp_regcomp(str
);
1146 if (!ecom
&& !regex
)
1147 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1151 ecommunity_ecom2str(ecom
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1153 entry
= community_entry_new();
1154 entry
->direct
= direct
;
1155 entry
->style
= style
;
1158 entry
->config
= ecommunity_ecom2str(
1159 ecom
, ECOMMUNITY_FORMAT_COMMUNITY_LIST
, 0);
1161 entry
->config
= XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
);
1163 entry
->u
.ecom
= ecom
;
1166 /* Do not put duplicated community entry. */
1167 if (community_list_dup_check(list
, entry
))
1168 community_entry_free(entry
);
1170 community_list_entry_add(list
, entry
);
1171 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_ADDED
);
1177 /* Unset extcommunity-list.
1179 * When str is NULL, delete all extcommunity-list entries belonging to the
1182 int extcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1183 const char *str
, int direct
, int style
)
1185 struct community_list_master
*cm
= NULL
;
1186 struct community_entry
*entry
= NULL
;
1187 struct community_list
*list
;
1188 struct ecommunity
*ecom
= NULL
;
1190 /* Lookup extcommunity list. */
1191 list
= community_list_lookup(ch
, name
, 0, EXTCOMMUNITY_LIST_MASTER
);
1193 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1195 cm
= community_list_master_lookup(ch
, EXTCOMMUNITY_LIST_MASTER
);
1196 /* Delete all of entry belongs to this extcommunity-list. */
1198 community_list_delete(cm
, list
);
1199 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1203 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1204 ecom
= ecommunity_str2com(str
, 0, 1);
1207 entry
= community_list_entry_lookup(list
, ecom
, direct
);
1208 ecommunity_free(&ecom
);
1210 entry
= community_list_entry_lookup(list
, str
, direct
);
1213 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1215 community_list_entry_delete(cm
, list
, entry
);
1216 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1221 /* Initializa community-list. Return community-list handler. */
1222 struct community_list_handler
*community_list_init(void)
1224 struct community_list_handler
*ch
;
1225 ch
= XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER
,
1226 sizeof(struct community_list_handler
));
1228 ch
->community_list
.hash
=
1229 hash_create_size(4, bgp_clist_hash_key_community_list
,
1230 bgp_clist_hash_cmp_community_list
,
1231 "Community List Number Quick Lookup");
1233 ch
->extcommunity_list
.hash
=
1234 hash_create_size(4, bgp_clist_hash_key_community_list
,
1235 bgp_clist_hash_cmp_community_list
,
1236 "Extended Community List Quick Lookup");
1238 ch
->lcommunity_list
.hash
=
1239 hash_create_size(4, bgp_clist_hash_key_community_list
,
1240 bgp_clist_hash_cmp_community_list
,
1241 "Large Community List Quick Lookup");
1246 /* Terminate community-list. */
1247 void community_list_terminate(struct community_list_handler
*ch
)
1249 struct community_list_master
*cm
;
1250 struct community_list
*list
;
1252 cm
= &ch
->community_list
;
1253 while ((list
= cm
->num
.head
) != NULL
)
1254 community_list_delete(cm
, list
);
1255 while ((list
= cm
->str
.head
) != NULL
)
1256 community_list_delete(cm
, list
);
1257 hash_free(cm
->hash
);
1259 cm
= &ch
->lcommunity_list
;
1260 while ((list
= cm
->num
.head
) != NULL
)
1261 community_list_delete(cm
, list
);
1262 while ((list
= cm
->str
.head
) != NULL
)
1263 community_list_delete(cm
, list
);
1264 hash_free(cm
->hash
);
1266 cm
= &ch
->extcommunity_list
;
1267 while ((list
= cm
->num
.head
) != NULL
)
1268 community_list_delete(cm
, list
);
1269 while ((list
= cm
->str
.head
) != NULL
)
1270 community_list_delete(cm
, list
);
1271 hash_free(cm
->hash
);
1273 XFREE(MTYPE_COMMUNITY_LIST_HANDLER
, ch
);