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
699 /* Perform exact matching. In case of expanded large-community-list, do
700 * same thing as lcommunity_list_match().
702 int lcommunity_list_exact_match(struct lcommunity
*lcom
,
703 struct community_list
*list
)
705 struct community_entry
*entry
;
707 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
709 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
711 if (entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
) {
712 if (lcommunity_cmp(lcom
, entry
->u
.com
))
713 return entry
->direct
== COMMUNITY_PERMIT
? 1
715 } else if (entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
) {
716 if (lcommunity_regexp_match(lcom
, entry
->reg
))
717 return entry
->direct
== COMMUNITY_PERMIT
? 1
724 int ecommunity_list_match(struct ecommunity
*ecom
, struct community_list
*list
)
726 struct community_entry
*entry
;
728 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
730 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
732 if (entry
->style
== EXTCOMMUNITY_LIST_STANDARD
) {
733 if (ecommunity_match(ecom
, entry
->u
.ecom
))
734 return entry
->direct
== COMMUNITY_PERMIT
? 1
736 } else if (entry
->style
== EXTCOMMUNITY_LIST_EXPANDED
) {
737 if (ecommunity_regexp_match(ecom
, entry
->reg
))
738 return entry
->direct
== COMMUNITY_PERMIT
? 1
745 /* Perform exact matching. In case of expanded community-list, do
746 same thing as community_list_match(). */
747 int community_list_exact_match(struct community
*com
,
748 struct community_list
*list
)
750 struct community_entry
*entry
;
752 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
754 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
756 if (entry
->style
== COMMUNITY_LIST_STANDARD
) {
757 if (community_include(entry
->u
.com
, COMMUNITY_INTERNET
))
758 return entry
->direct
== COMMUNITY_PERMIT
? 1
761 if (community_cmp(com
, entry
->u
.com
))
762 return entry
->direct
== COMMUNITY_PERMIT
? 1
764 } else if (entry
->style
== COMMUNITY_LIST_EXPANDED
) {
765 if (community_regexp_match(com
, entry
->reg
))
766 return entry
->direct
== COMMUNITY_PERMIT
? 1
773 /* Delete all permitted communities in the list from com. */
774 struct community
*community_list_match_delete(struct community
*com
,
775 struct community_list
*list
)
777 struct community_entry
*entry
;
779 uint32_t com_index_to_delete
[com
->size
];
780 int delete_index
= 0;
783 /* Loop over each community value and evaluate each against the
784 * community-list. If we need to delete a community value add its index
785 * to com_index_to_delete.
787 for (i
= 0; i
< com
->size
; i
++) {
788 val
= community_val_get(com
, i
);
790 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
792 if (entry
->direct
== COMMUNITY_PERMIT
) {
793 com_index_to_delete
[delete_index
] = i
;
799 else if ((entry
->style
== COMMUNITY_LIST_STANDARD
)
800 && (community_include(entry
->u
.com
,
802 || community_include(entry
->u
.com
, val
))) {
803 if (entry
->direct
== COMMUNITY_PERMIT
) {
804 com_index_to_delete
[delete_index
] = i
;
810 else if ((entry
->style
== COMMUNITY_LIST_EXPANDED
)
811 && community_regexp_include(entry
->reg
, com
,
813 if (entry
->direct
== COMMUNITY_PERMIT
) {
814 com_index_to_delete
[delete_index
] = i
;
822 /* Delete all of the communities we flagged for deletion */
823 for (i
= delete_index
- 1; i
>= 0; i
--) {
824 val
= community_val_get(com
, com_index_to_delete
[i
]);
825 community_del_val(com
, &val
);
831 /* To avoid duplicated entry in the community-list, this function
832 compares specified entry to existing entry. */
833 static int community_list_dup_check(struct community_list
*list
,
834 struct community_entry
*new)
836 struct community_entry
*entry
;
838 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
839 if (entry
->style
!= new->style
)
842 if (entry
->direct
!= new->direct
)
845 if (entry
->any
!= new->any
)
851 switch (entry
->style
) {
852 case COMMUNITY_LIST_STANDARD
:
853 if (community_cmp(entry
->u
.com
, new->u
.com
))
856 case LARGE_COMMUNITY_LIST_STANDARD
:
857 if (lcommunity_cmp(entry
->u
.lcom
, new->u
.lcom
))
860 case EXTCOMMUNITY_LIST_STANDARD
:
861 if (ecommunity_cmp(entry
->u
.ecom
, new->u
.ecom
))
864 case COMMUNITY_LIST_EXPANDED
:
865 case EXTCOMMUNITY_LIST_EXPANDED
:
866 case LARGE_COMMUNITY_LIST_EXPANDED
:
867 if (strcmp(entry
->config
, new->config
) == 0)
877 /* Set community-list. */
878 int community_list_set(struct community_list_handler
*ch
, const char *name
,
879 const char *str
, int direct
, int style
)
881 struct community_entry
*entry
= NULL
;
882 struct community_list
*list
;
883 struct community
*com
= NULL
;
884 regex_t
*regex
= NULL
;
886 /* Get community list. */
887 list
= community_list_get(ch
, name
, COMMUNITY_LIST_MASTER
);
889 /* When community-list already has entry, new entry should have same
890 style. If you want to have mixed style community-list, you can
891 comment out this check. */
892 if (!community_list_empty_p(list
)) {
893 struct community_entry
*first
;
897 if (style
!= first
->style
) {
898 return (first
->style
== COMMUNITY_LIST_STANDARD
899 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
900 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
905 if (style
== COMMUNITY_LIST_STANDARD
)
906 com
= community_str2com(str
);
908 regex
= bgp_regcomp(str
);
911 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
914 entry
= community_entry_new();
915 entry
->direct
= direct
;
916 entry
->style
= style
;
917 entry
->any
= (str
? 0 : 1);
921 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
923 /* Do not put duplicated community entry. */
924 if (community_list_dup_check(list
, entry
))
925 community_entry_free(entry
);
927 community_list_entry_add(list
, entry
);
928 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_ADDED
);
934 /* Unset community-list */
935 int community_list_unset(struct community_list_handler
*ch
, const char *name
,
936 const char *str
, int direct
, int style
)
938 struct community_list_master
*cm
= NULL
;
939 struct community_entry
*entry
= NULL
;
940 struct community_list
*list
;
941 struct community
*com
= NULL
;
943 /* Lookup community list. */
944 list
= community_list_lookup(ch
, name
, 0, COMMUNITY_LIST_MASTER
);
946 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
948 cm
= community_list_master_lookup(ch
, COMMUNITY_LIST_MASTER
);
949 /* Delete all of entry belongs to this community-list. */
951 community_list_delete(cm
, list
);
952 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
956 if (style
== COMMUNITY_LIST_STANDARD
)
957 com
= community_str2com(str
);
960 entry
= community_list_entry_lookup(list
, com
, direct
);
961 community_free(&com
);
963 entry
= community_list_entry_lookup(list
, str
, direct
);
966 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
968 community_list_entry_delete(cm
, list
, entry
);
969 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
974 /* Delete all permitted large communities in the list from com. */
975 struct lcommunity
*lcommunity_list_match_delete(struct lcommunity
*lcom
,
976 struct community_list
*list
)
978 struct community_entry
*entry
;
979 uint32_t com_index_to_delete
[lcom
->size
];
981 int delete_index
= 0;
984 /* Loop over each lcommunity value and evaluate each against the
985 * community-list. If we need to delete a community value add its index
986 * to com_index_to_delete.
988 for (i
= 0; i
< lcom
->size
; i
++) {
989 ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
990 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
992 if (entry
->direct
== COMMUNITY_PERMIT
) {
993 com_index_to_delete
[delete_index
] = i
;
999 else if ((entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
)
1000 && lcommunity_include(entry
->u
.lcom
, ptr
)) {
1001 if (entry
->direct
== COMMUNITY_PERMIT
) {
1002 com_index_to_delete
[delete_index
] = i
;
1008 else if ((entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
)
1009 && lcommunity_regexp_include(entry
->reg
, lcom
,
1011 if (entry
->direct
== COMMUNITY_PERMIT
) {
1012 com_index_to_delete
[delete_index
] = i
;
1020 /* Delete all of the communities we flagged for deletion */
1021 for (i
= delete_index
- 1; i
>= 0; i
--) {
1022 ptr
= lcom
->val
+ (com_index_to_delete
[i
] * LCOMMUNITY_SIZE
);
1023 lcommunity_del_val(lcom
, ptr
);
1029 /* Set lcommunity-list. */
1030 int lcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
1031 const char *str
, int direct
, int style
)
1033 struct community_entry
*entry
= NULL
;
1034 struct community_list
*list
;
1035 struct lcommunity
*lcom
= NULL
;
1036 regex_t
*regex
= NULL
;
1038 /* Get community list. */
1039 list
= community_list_get(ch
, name
, LARGE_COMMUNITY_LIST_MASTER
);
1041 /* When community-list already has entry, new entry should have same
1042 style. If you want to have mixed style community-list, you can
1043 comment out this check. */
1044 if (!community_list_empty_p(list
)) {
1045 struct community_entry
*first
;
1049 if (style
!= first
->style
) {
1050 return (first
->style
== COMMUNITY_LIST_STANDARD
1051 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1052 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
1057 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
1058 lcom
= lcommunity_str2com(str
);
1060 regex
= bgp_regcomp(str
);
1062 if (!lcom
&& !regex
)
1063 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1066 entry
= community_entry_new();
1067 entry
->direct
= direct
;
1068 entry
->style
= style
;
1069 entry
->any
= (str
? 0 : 1);
1070 entry
->u
.lcom
= lcom
;
1073 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
1075 /* Do not put duplicated community entry. */
1076 if (community_list_dup_check(list
, entry
))
1077 community_entry_free(entry
);
1079 community_list_entry_add(list
, entry
);
1080 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_ADDED
);
1086 /* Unset community-list. When str is NULL, delete all of
1087 community-list entry belongs to the specified name. */
1088 int lcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1089 const char *str
, int direct
, int style
)
1091 struct community_list_master
*cm
= NULL
;
1092 struct community_entry
*entry
= NULL
;
1093 struct community_list
*list
;
1094 struct lcommunity
*lcom
= NULL
;
1095 regex_t
*regex
= NULL
;
1097 /* Lookup community list. */
1098 list
= community_list_lookup(ch
, name
, 0, LARGE_COMMUNITY_LIST_MASTER
);
1100 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1102 cm
= community_list_master_lookup(ch
, LARGE_COMMUNITY_LIST_MASTER
);
1103 /* Delete all of entry belongs to this community-list. */
1105 community_list_delete(cm
, list
);
1106 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_DELETED
);
1110 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
1111 lcom
= lcommunity_str2com(str
);
1113 regex
= bgp_regcomp(str
);
1115 if (!lcom
&& !regex
)
1116 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1119 entry
= community_list_entry_lookup(list
, lcom
, direct
);
1121 entry
= community_list_entry_lookup(list
, str
, direct
);
1124 lcommunity_free(&lcom
);
1126 bgp_regex_free(regex
);
1129 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1131 community_list_entry_delete(cm
, list
, entry
);
1132 route_map_notify_dependencies(name
, RMAP_EVENT_LLIST_DELETED
);
1137 /* Set extcommunity-list. */
1138 int extcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
1139 const char *str
, int direct
, int style
)
1141 struct community_entry
*entry
= NULL
;
1142 struct community_list
*list
;
1143 struct ecommunity
*ecom
= NULL
;
1144 regex_t
*regex
= NULL
;
1147 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1149 /* Get community list. */
1150 list
= community_list_get(ch
, name
, EXTCOMMUNITY_LIST_MASTER
);
1152 /* When community-list already has entry, new entry should have same
1153 style. If you want to have mixed style community-list, you can
1154 comment out this check. */
1155 if (!community_list_empty_p(list
)) {
1156 struct community_entry
*first
;
1160 if (style
!= first
->style
) {
1161 return (first
->style
== EXTCOMMUNITY_LIST_STANDARD
1162 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1163 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
1167 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1168 ecom
= ecommunity_str2com(str
, 0, 1);
1170 regex
= bgp_regcomp(str
);
1172 if (!ecom
&& !regex
)
1173 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1177 ecommunity_ecom2str(ecom
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1179 entry
= community_entry_new();
1180 entry
->direct
= direct
;
1181 entry
->style
= style
;
1184 entry
->config
= ecommunity_ecom2str(
1185 ecom
, ECOMMUNITY_FORMAT_COMMUNITY_LIST
, 0);
1187 entry
->config
= XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
);
1189 entry
->u
.ecom
= ecom
;
1192 /* Do not put duplicated community entry. */
1193 if (community_list_dup_check(list
, entry
))
1194 community_entry_free(entry
);
1196 community_list_entry_add(list
, entry
);
1197 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_ADDED
);
1203 /* Unset extcommunity-list.
1205 * When str is NULL, delete all extcommunity-list entries belonging to the
1208 int extcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1209 const char *str
, int direct
, int style
)
1211 struct community_list_master
*cm
= NULL
;
1212 struct community_entry
*entry
= NULL
;
1213 struct community_list
*list
;
1214 struct ecommunity
*ecom
= NULL
;
1216 /* Lookup extcommunity list. */
1217 list
= community_list_lookup(ch
, name
, 0, EXTCOMMUNITY_LIST_MASTER
);
1219 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1221 cm
= community_list_master_lookup(ch
, EXTCOMMUNITY_LIST_MASTER
);
1222 /* Delete all of entry belongs to this extcommunity-list. */
1224 community_list_delete(cm
, list
);
1225 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1229 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1230 ecom
= ecommunity_str2com(str
, 0, 1);
1233 entry
= community_list_entry_lookup(list
, ecom
, direct
);
1234 ecommunity_free(&ecom
);
1236 entry
= community_list_entry_lookup(list
, str
, direct
);
1239 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1241 community_list_entry_delete(cm
, list
, entry
);
1242 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1247 /* Initializa community-list. Return community-list handler. */
1248 struct community_list_handler
*community_list_init(void)
1250 struct community_list_handler
*ch
;
1251 ch
= XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER
,
1252 sizeof(struct community_list_handler
));
1254 ch
->community_list
.hash
=
1255 hash_create_size(4, bgp_clist_hash_key_community_list
,
1256 bgp_clist_hash_cmp_community_list
,
1257 "Community List Number Quick Lookup");
1259 ch
->extcommunity_list
.hash
=
1260 hash_create_size(4, bgp_clist_hash_key_community_list
,
1261 bgp_clist_hash_cmp_community_list
,
1262 "Extended Community List Quick Lookup");
1264 ch
->lcommunity_list
.hash
=
1265 hash_create_size(4, bgp_clist_hash_key_community_list
,
1266 bgp_clist_hash_cmp_community_list
,
1267 "Large Community List Quick Lookup");
1272 /* Terminate community-list. */
1273 void community_list_terminate(struct community_list_handler
*ch
)
1275 struct community_list_master
*cm
;
1276 struct community_list
*list
;
1278 cm
= &ch
->community_list
;
1279 while ((list
= cm
->num
.head
) != NULL
)
1280 community_list_delete(cm
, list
);
1281 while ((list
= cm
->str
.head
) != NULL
)
1282 community_list_delete(cm
, list
);
1283 hash_free(cm
->hash
);
1285 cm
= &ch
->lcommunity_list
;
1286 while ((list
= cm
->num
.head
) != NULL
)
1287 community_list_delete(cm
, list
);
1288 while ((list
= cm
->str
.head
) != NULL
)
1289 community_list_delete(cm
, list
);
1290 hash_free(cm
->hash
);
1292 cm
= &ch
->extcommunity_list
;
1293 while ((list
= cm
->num
.head
) != NULL
)
1294 community_list_delete(cm
, list
);
1295 while ((list
= cm
->str
.head
) != NULL
)
1296 community_list_delete(cm
, list
);
1297 hash_free(cm
->hash
);
1299 XFREE(MTYPE_COMMUNITY_LIST_HANDLER
, ch
);