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
30 #include "bgpd/bgpd.h"
31 #include "bgpd/bgp_community.h"
32 #include "bgpd/bgp_ecommunity.h"
33 #include "bgpd/bgp_lcommunity.h"
34 #include "bgpd/bgp_aspath.h"
35 #include "bgpd/bgp_regex.h"
36 #include "bgpd/bgp_clist.h"
38 /* Lookup master structure for community-list or
40 struct community_list_master
*
41 community_list_master_lookup(struct community_list_handler
*ch
, int master
)
45 case COMMUNITY_LIST_MASTER
:
46 return &ch
->community_list
;
47 case EXTCOMMUNITY_LIST_MASTER
:
48 return &ch
->extcommunity_list
;
49 case LARGE_COMMUNITY_LIST_MASTER
:
50 return &ch
->lcommunity_list
;
55 /* Allocate a new community list entry. */
56 static struct community_entry
*community_entry_new(void)
58 return XCALLOC(MTYPE_COMMUNITY_LIST_ENTRY
,
59 sizeof(struct community_entry
));
62 /* Free community list entry. */
63 static void community_entry_free(struct community_entry
*entry
)
65 switch (entry
->style
) {
66 case COMMUNITY_LIST_STANDARD
:
68 community_free(entry
->u
.com
);
70 case LARGE_COMMUNITY_LIST_STANDARD
:
72 lcommunity_free(&entry
->u
.lcom
);
74 case EXTCOMMUNITY_LIST_STANDARD
:
75 /* In case of standard extcommunity-list, configuration string
76 is made by ecommunity_ecom2str(). */
78 XFREE(MTYPE_ECOMMUNITY_STR
, entry
->config
);
80 ecommunity_free(&entry
->u
.ecom
);
82 case COMMUNITY_LIST_EXPANDED
:
83 case EXTCOMMUNITY_LIST_EXPANDED
:
84 case LARGE_COMMUNITY_LIST_EXPANDED
:
86 XFREE(MTYPE_COMMUNITY_LIST_CONFIG
, entry
->config
);
88 bgp_regex_free(entry
->reg
);
92 XFREE(MTYPE_COMMUNITY_LIST_ENTRY
, entry
);
95 /* Allocate a new community-list. */
96 static struct community_list
*community_list_new(void)
98 return XCALLOC(MTYPE_COMMUNITY_LIST
, sizeof(struct community_list
));
101 /* Free community-list. */
102 static void community_list_free(struct community_list
*list
)
105 XFREE(MTYPE_COMMUNITY_LIST_NAME
, list
->name
);
106 XFREE(MTYPE_COMMUNITY_LIST
, list
);
109 static struct community_list
*
110 community_list_insert(struct community_list_handler
*ch
, const char *name
,
115 struct community_list
*new;
116 struct community_list
*point
;
117 struct community_list_list
*list
;
118 struct community_list_master
*cm
;
120 /* Lookup community-list master. */
121 cm
= community_list_master_lookup(ch
, master
);
125 /* Allocate new community_list and copy given name. */
126 new = community_list_new();
127 new->name
= XSTRDUP(MTYPE_COMMUNITY_LIST_NAME
, name
);
129 /* If name is made by all digit character. We treat it as
131 for (number
= 0, i
= 0; i
< strlen(name
); i
++) {
132 if (isdigit((int)name
[i
]))
133 number
= (number
* 10) + (name
[i
] - '0');
138 /* In case of name is all digit character */
139 if (i
== strlen(name
)) {
140 new->sort
= COMMUNITY_LIST_NUMBER
;
142 /* Set access_list to number list. */
145 for (point
= list
->head
; point
; point
= point
->next
)
146 if (atol(point
->name
) >= number
)
149 new->sort
= COMMUNITY_LIST_STRING
;
151 /* Set access_list to string list. */
154 /* Set point to insertion point. */
155 for (point
= list
->head
; point
; point
= point
->next
)
156 if (strcmp(point
->name
, name
) >= 0)
160 /* Link to upper list. */
163 /* In case of this is the first element of master. */
164 if (list
->head
== NULL
) {
165 list
->head
= list
->tail
= new;
169 /* In case of insertion is made at the tail of access_list. */
171 new->prev
= list
->tail
;
172 list
->tail
->next
= new;
177 /* In case of insertion is made at the head of access_list. */
178 if (point
== list
->head
) {
179 new->next
= list
->head
;
180 list
->head
->prev
= new;
185 /* Insertion is made at middle of the access_list. */
187 new->prev
= point
->prev
;
190 point
->prev
->next
= new;
196 struct community_list
*community_list_lookup(struct community_list_handler
*ch
,
197 const char *name
, int master
)
199 struct community_list
*list
;
200 struct community_list_master
*cm
;
205 cm
= community_list_master_lookup(ch
, master
);
209 for (list
= cm
->num
.head
; list
; list
= list
->next
)
210 if (strcmp(list
->name
, name
) == 0)
212 for (list
= cm
->str
.head
; list
; list
= list
->next
)
213 if (strcmp(list
->name
, name
) == 0)
219 static struct community_list
*
220 community_list_get(struct community_list_handler
*ch
, const char *name
,
223 struct community_list
*list
;
225 list
= community_list_lookup(ch
, name
, master
);
227 list
= community_list_insert(ch
, name
, master
);
231 static void community_list_delete(struct community_list
*list
)
233 struct community_list_list
*clist
;
234 struct community_entry
*entry
, *next
;
236 for (entry
= list
->head
; entry
; entry
= next
) {
238 community_entry_free(entry
);
241 clist
= list
->parent
;
244 list
->next
->prev
= list
->prev
;
246 clist
->tail
= list
->prev
;
249 list
->prev
->next
= list
->next
;
251 clist
->head
= list
->next
;
253 community_list_free(list
);
256 static int community_list_empty_p(struct community_list
*list
)
258 return (list
->head
== NULL
&& list
->tail
== NULL
) ? 1 : 0;
261 /* Add community-list entry to the list. */
262 static void community_list_entry_add(struct community_list
*list
,
263 struct community_entry
*entry
)
266 entry
->prev
= list
->tail
;
269 list
->tail
->next
= entry
;
275 /* Delete community-list entry from the list. */
276 static void community_list_entry_delete(struct community_list
*list
,
277 struct community_entry
*entry
)
280 entry
->next
->prev
= entry
->prev
;
282 list
->tail
= entry
->prev
;
285 entry
->prev
->next
= entry
->next
;
287 list
->head
= entry
->next
;
289 community_entry_free(entry
);
291 if (community_list_empty_p(list
))
292 community_list_delete(list
);
295 /* Lookup community-list entry from the list. */
296 static struct community_entry
*
297 community_list_entry_lookup(struct community_list
*list
, const void *arg
,
300 struct community_entry
*entry
;
302 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
303 switch (entry
->style
) {
304 case COMMUNITY_LIST_STANDARD
:
305 if (entry
->direct
== direct
306 && community_cmp(entry
->u
.com
, arg
))
309 case EXTCOMMUNITY_LIST_STANDARD
:
310 if (entry
->direct
== direct
311 && ecommunity_cmp(entry
->u
.ecom
, arg
))
314 case LARGE_COMMUNITY_LIST_STANDARD
:
315 if (entry
->direct
== direct
316 && lcommunity_cmp(entry
->u
.lcom
, arg
))
319 case COMMUNITY_LIST_EXPANDED
:
320 case EXTCOMMUNITY_LIST_EXPANDED
:
321 case LARGE_COMMUNITY_LIST_EXPANDED
:
322 if (entry
->direct
== direct
323 && strcmp(entry
->config
, arg
) == 0)
333 static char *community_str_get(struct community
*com
, int i
)
342 memcpy(&comval
, com_nthval(com
, i
), sizeof(uint32_t));
343 comval
= ntohl(comval
);
346 case COMMUNITY_INTERNET
:
347 len
= strlen(" internet");
349 case COMMUNITY_NO_EXPORT
:
350 len
= strlen(" no-export");
352 case COMMUNITY_NO_ADVERTISE
:
353 len
= strlen(" no-advertise");
355 case COMMUNITY_LOCAL_AS
:
356 len
= strlen(" local-AS");
358 case COMMUNITY_GSHUT
:
359 len
= strlen(" graceful-shutdown");
362 len
= strlen(" 65536:65535");
366 /* Allocate memory. */
367 str
= pnt
= XMALLOC(MTYPE_COMMUNITY_STR
, len
);
370 case COMMUNITY_INTERNET
:
371 strcpy(pnt
, "internet");
372 pnt
+= strlen("internet");
374 case COMMUNITY_NO_EXPORT
:
375 strcpy(pnt
, "no-export");
376 pnt
+= strlen("no-export");
378 case COMMUNITY_NO_ADVERTISE
:
379 strcpy(pnt
, "no-advertise");
380 pnt
+= strlen("no-advertise");
382 case COMMUNITY_LOCAL_AS
:
383 strcpy(pnt
, "local-AS");
384 pnt
+= strlen("local-AS");
386 case COMMUNITY_GSHUT
:
387 strcpy(pnt
, "graceful-shutdown");
388 pnt
+= strlen("graceful-shutdown");
391 as
= (comval
>> 16) & 0xFFFF;
392 val
= comval
& 0xFFFF;
393 sprintf(pnt
, "%u:%d", as
, val
);
403 /* Internal function to perform regular expression match for
404 * a single community. */
405 static int community_regexp_include(regex_t
*reg
, struct community
*com
, int i
)
410 /* When there is no communities attribute it is treated as empty string.
412 if (com
== NULL
|| com
->size
== 0)
413 str
= XSTRDUP(MTYPE_COMMUNITY_STR
, "");
415 str
= community_str_get(com
, i
);
417 /* Regular expression match. */
418 rv
= regexec(reg
, str
, 0, NULL
, 0);
420 XFREE(MTYPE_COMMUNITY_STR
, str
);
429 /* Internal function to perform regular expression match for community
431 static int community_regexp_match(struct community
*com
, regex_t
*reg
)
435 /* When there is no communities attribute it is treated as empty
437 if (com
== NULL
|| com
->size
== 0)
440 str
= community_str(com
, false);
442 /* Regular expression match. */
443 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
450 static char *lcommunity_str_get(struct lcommunity
*lcom
, int i
)
452 struct lcommunity_val lcomval
;
453 uint32_t globaladmin
;
460 ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
462 memcpy(&lcomval
, ptr
, LCOMMUNITY_SIZE
);
464 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
465 str
= pnt
= XMALLOC(MTYPE_LCOMMUNITY_STR
, 48);
467 ptr
= (uint8_t *)lcomval
.val
;
468 ptr
= ptr_get_be32(ptr
, &globaladmin
);
469 ptr
= ptr_get_be32(ptr
, &localdata1
);
470 ptr
= ptr_get_be32(ptr
, &localdata2
);
471 (void)ptr
; /* consume value */
473 sprintf(pnt
, "%u:%u:%u", globaladmin
, localdata1
, localdata2
);
480 /* Internal function to perform regular expression match for
481 * a single community. */
482 static int lcommunity_regexp_include(regex_t
*reg
, struct lcommunity
*lcom
,
487 /* When there is no communities attribute it is treated as empty string.
489 if (lcom
== NULL
|| lcom
->size
== 0)
490 str
= XSTRDUP(MTYPE_LCOMMUNITY_STR
, "");
492 str
= lcommunity_str_get(lcom
, i
);
494 /* Regular expression match. */
495 if (regexec(reg
, str
, 0, NULL
, 0) == 0) {
496 XFREE(MTYPE_LCOMMUNITY_STR
, str
);
500 XFREE(MTYPE_LCOMMUNITY_STR
, str
);
505 static int lcommunity_regexp_match(struct lcommunity
*com
, regex_t
*reg
)
509 /* When there is no communities attribute it is treated as empty
511 if (com
== NULL
|| com
->size
== 0)
514 str
= lcommunity_str(com
, false);
516 /* Regular expression match. */
517 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
525 static int ecommunity_regexp_match(struct ecommunity
*ecom
, regex_t
*reg
)
529 /* When there is no communities attribute it is treated as empty
531 if (ecom
== NULL
|| ecom
->size
== 0)
534 str
= ecommunity_str(ecom
);
536 /* Regular expression match. */
537 if (regexec(reg
, str
, 0, NULL
, 0) == 0)
545 /* Delete community attribute using regular expression match. Return
546 modified communites attribute. */
547 static struct community
*
548 community_regexp_delete (struct community
*com
, regex_t
* reg
)
552 /* Maximum is "65535:65535" + '\0'. */
560 while (i
< com
->size
)
562 memcpy (&comval
, com_nthval (com
, i
), sizeof (uint32_t));
563 comval
= ntohl (comval
);
567 case COMMUNITY_INTERNET
:
570 case COMMUNITY_NO_EXPORT
:
573 case COMMUNITY_NO_ADVERTISE
:
574 str
= "no-advertise";
576 case COMMUNITY_LOCAL_AS
:
580 sprintf (c
, "%d:%d", (comval
>> 16) & 0xFFFF, comval
& 0xFFFF);
585 if (regexec (reg
, str
, 0, NULL
, 0) == 0)
586 community_del_val (com
, com_nthval (com
, i
));
594 /* When given community attribute matches to the community-list return
596 int community_list_match(struct community
*com
, struct community_list
*list
)
598 struct community_entry
*entry
;
600 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
602 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
604 if (entry
->style
== COMMUNITY_LIST_STANDARD
) {
605 if (community_include(entry
->u
.com
, COMMUNITY_INTERNET
))
606 return entry
->direct
== COMMUNITY_PERMIT
? 1
609 if (community_match(com
, entry
->u
.com
))
610 return entry
->direct
== COMMUNITY_PERMIT
? 1
612 } else if (entry
->style
== COMMUNITY_LIST_EXPANDED
) {
613 if (community_regexp_match(com
, entry
->reg
))
614 return entry
->direct
== COMMUNITY_PERMIT
? 1
621 int lcommunity_list_match(struct lcommunity
*lcom
, struct community_list
*list
)
623 struct community_entry
*entry
;
625 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
627 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
629 if (entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
) {
630 if (lcommunity_match(lcom
, entry
->u
.lcom
))
631 return entry
->direct
== COMMUNITY_PERMIT
? 1
633 } else if (entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
) {
634 if (lcommunity_regexp_match(lcom
, entry
->reg
))
635 return entry
->direct
== COMMUNITY_PERMIT
? 1
642 int ecommunity_list_match(struct ecommunity
*ecom
, struct community_list
*list
)
644 struct community_entry
*entry
;
646 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
648 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
650 if (entry
->style
== EXTCOMMUNITY_LIST_STANDARD
) {
651 if (ecommunity_match(ecom
, entry
->u
.ecom
))
652 return entry
->direct
== COMMUNITY_PERMIT
? 1
654 } else if (entry
->style
== EXTCOMMUNITY_LIST_EXPANDED
) {
655 if (ecommunity_regexp_match(ecom
, entry
->reg
))
656 return entry
->direct
== COMMUNITY_PERMIT
? 1
663 /* Perform exact matching. In case of expanded community-list, do
664 same thing as community_list_match(). */
665 int community_list_exact_match(struct community
*com
,
666 struct community_list
*list
)
668 struct community_entry
*entry
;
670 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
672 return entry
->direct
== COMMUNITY_PERMIT
? 1 : 0;
674 if (entry
->style
== COMMUNITY_LIST_STANDARD
) {
675 if (community_include(entry
->u
.com
, COMMUNITY_INTERNET
))
676 return entry
->direct
== COMMUNITY_PERMIT
? 1
679 if (community_cmp(com
, entry
->u
.com
))
680 return entry
->direct
== COMMUNITY_PERMIT
? 1
682 } else if (entry
->style
== COMMUNITY_LIST_EXPANDED
) {
683 if (community_regexp_match(com
, entry
->reg
))
684 return entry
->direct
== COMMUNITY_PERMIT
? 1
691 /* Delete all permitted communities in the list from com. */
692 struct community
*community_list_match_delete(struct community
*com
,
693 struct community_list
*list
)
695 struct community_entry
*entry
;
697 uint32_t com_index_to_delete
[com
->size
];
698 int delete_index
= 0;
701 /* Loop over each community value and evaluate each against the
702 * community-list. If we need to delete a community value add its index
703 * to com_index_to_delete.
705 for (i
= 0; i
< com
->size
; i
++) {
706 val
= community_val_get(com
, i
);
708 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
710 if (entry
->direct
== COMMUNITY_PERMIT
) {
711 com_index_to_delete
[delete_index
] = i
;
717 else if ((entry
->style
== COMMUNITY_LIST_STANDARD
)
718 && (community_include(entry
->u
.com
,
720 || community_include(entry
->u
.com
, val
))) {
721 if (entry
->direct
== COMMUNITY_PERMIT
) {
722 com_index_to_delete
[delete_index
] = i
;
728 else if ((entry
->style
== COMMUNITY_LIST_EXPANDED
)
729 && community_regexp_include(entry
->reg
, com
,
731 if (entry
->direct
== COMMUNITY_PERMIT
) {
732 com_index_to_delete
[delete_index
] = i
;
740 /* Delete all of the communities we flagged for deletion */
741 for (i
= delete_index
- 1; i
>= 0; i
--) {
742 val
= community_val_get(com
, com_index_to_delete
[i
]);
743 community_del_val(com
, &val
);
749 /* To avoid duplicated entry in the community-list, this function
750 compares specified entry to existing entry. */
751 static int community_list_dup_check(struct community_list
*list
,
752 struct community_entry
*new)
754 struct community_entry
*entry
;
756 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
757 if (entry
->style
!= new->style
)
760 if (entry
->direct
!= new->direct
)
763 if (entry
->any
!= new->any
)
769 switch (entry
->style
) {
770 case COMMUNITY_LIST_STANDARD
:
771 if (community_cmp(entry
->u
.com
, new->u
.com
))
774 case LARGE_COMMUNITY_LIST_STANDARD
:
775 if (lcommunity_cmp(entry
->u
.lcom
, new->u
.lcom
))
778 case EXTCOMMUNITY_LIST_STANDARD
:
779 if (ecommunity_cmp(entry
->u
.ecom
, new->u
.ecom
))
782 case COMMUNITY_LIST_EXPANDED
:
783 case EXTCOMMUNITY_LIST_EXPANDED
:
784 case LARGE_COMMUNITY_LIST_EXPANDED
:
785 if (strcmp(entry
->config
, new->config
) == 0)
795 /* Set community-list. */
796 int community_list_set(struct community_list_handler
*ch
, const char *name
,
797 const char *str
, int direct
, int style
)
799 struct community_entry
*entry
= NULL
;
800 struct community_list
*list
;
801 struct community
*com
= NULL
;
802 regex_t
*regex
= NULL
;
804 /* Get community list. */
805 list
= community_list_get(ch
, name
, COMMUNITY_LIST_MASTER
);
807 /* When community-list already has entry, new entry should have same
808 style. If you want to have mixed style community-list, you can
809 comment out this check. */
810 if (!community_list_empty_p(list
)) {
811 struct community_entry
*first
;
815 if (style
!= first
->style
) {
816 return (first
->style
== COMMUNITY_LIST_STANDARD
817 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
818 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
823 if (style
== COMMUNITY_LIST_STANDARD
)
824 com
= community_str2com(str
);
826 regex
= bgp_regcomp(str
);
829 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
832 entry
= community_entry_new();
833 entry
->direct
= direct
;
834 entry
->style
= style
;
835 entry
->any
= (str
? 0 : 1);
839 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
841 /* Do not put duplicated community entry. */
842 if (community_list_dup_check(list
, entry
))
843 community_entry_free(entry
);
845 community_list_entry_add(list
, entry
);
846 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_ADDED
);
852 /* Unset community-list */
853 int community_list_unset(struct community_list_handler
*ch
, const char *name
,
854 const char *str
, int direct
, int style
)
856 struct community_entry
*entry
= NULL
;
857 struct community_list
*list
;
858 struct community
*com
= NULL
;
860 /* Lookup community list. */
861 list
= community_list_lookup(ch
, name
, COMMUNITY_LIST_MASTER
);
863 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
865 /* Delete all of entry belongs to this community-list. */
867 community_list_delete(list
);
868 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
872 if (style
== COMMUNITY_LIST_STANDARD
)
873 com
= community_str2com(str
);
876 entry
= community_list_entry_lookup(list
, com
, direct
);
879 entry
= community_list_entry_lookup(list
, str
, direct
);
882 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
884 community_list_entry_delete(list
, entry
);
885 route_map_notify_dependencies(name
, RMAP_EVENT_CLIST_DELETED
);
890 /* Delete all permitted large communities in the list from com. */
891 struct lcommunity
*lcommunity_list_match_delete(struct lcommunity
*lcom
,
892 struct community_list
*list
)
894 struct community_entry
*entry
;
895 uint32_t com_index_to_delete
[lcom
->size
];
897 int delete_index
= 0;
900 /* Loop over each lcommunity value and evaluate each against the
901 * community-list. If we need to delete a community value add its index
902 * to com_index_to_delete.
904 for (i
= 0; i
< lcom
->size
; i
++) {
905 ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
906 for (entry
= list
->head
; entry
; entry
= entry
->next
) {
908 if (entry
->direct
== COMMUNITY_PERMIT
) {
909 com_index_to_delete
[delete_index
] = i
;
915 else if ((entry
->style
== LARGE_COMMUNITY_LIST_STANDARD
)
916 && lcommunity_include(entry
->u
.lcom
, ptr
)) {
917 if (entry
->direct
== COMMUNITY_PERMIT
) {
918 com_index_to_delete
[delete_index
] = i
;
924 else if ((entry
->style
== LARGE_COMMUNITY_LIST_EXPANDED
)
925 && lcommunity_regexp_include(entry
->reg
, lcom
,
927 if (entry
->direct
== COMMUNITY_PERMIT
) {
928 com_index_to_delete
[delete_index
] = i
;
936 /* Delete all of the communities we flagged for deletion */
937 for (i
= delete_index
- 1; i
>= 0; i
--) {
938 ptr
= lcom
->val
+ (com_index_to_delete
[i
] * LCOMMUNITY_SIZE
);
939 lcommunity_del_val(lcom
, ptr
);
945 /* Set lcommunity-list. */
946 int lcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
947 const char *str
, int direct
, int style
)
949 struct community_entry
*entry
= NULL
;
950 struct community_list
*list
;
951 struct lcommunity
*lcom
= NULL
;
952 regex_t
*regex
= NULL
;
954 /* Get community list. */
955 list
= community_list_get(ch
, name
, LARGE_COMMUNITY_LIST_MASTER
);
957 /* When community-list already has entry, new entry should have same
958 style. If you want to have mixed style community-list, you can
959 comment out this check. */
960 if (!community_list_empty_p(list
)) {
961 struct community_entry
*first
;
965 if (style
!= first
->style
) {
966 return (first
->style
== COMMUNITY_LIST_STANDARD
967 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
968 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
973 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
974 lcom
= lcommunity_str2com(str
);
976 regex
= bgp_regcomp(str
);
979 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
982 entry
= community_entry_new();
983 entry
->direct
= direct
;
984 entry
->style
= style
;
985 entry
->any
= (str
? 0 : 1);
986 entry
->u
.lcom
= lcom
;
989 (regex
? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
) : NULL
);
991 /* Do not put duplicated community entry. */
992 if (community_list_dup_check(list
, entry
))
993 community_entry_free(entry
);
995 community_list_entry_add(list
, entry
);
1000 /* Unset community-list. When str is NULL, delete all of
1001 community-list entry belongs to the specified name. */
1002 int lcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1003 const char *str
, int direct
, int style
)
1005 struct community_entry
*entry
= NULL
;
1006 struct community_list
*list
;
1007 struct lcommunity
*lcom
= NULL
;
1008 regex_t
*regex
= NULL
;
1010 /* Lookup community list. */
1011 list
= community_list_lookup(ch
, name
, LARGE_COMMUNITY_LIST_MASTER
);
1013 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1015 /* Delete all of entry belongs to this community-list. */
1017 community_list_delete(list
);
1021 if (style
== LARGE_COMMUNITY_LIST_STANDARD
)
1022 lcom
= lcommunity_str2com(str
);
1024 regex
= bgp_regcomp(str
);
1026 if (!lcom
&& !regex
)
1027 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1030 entry
= community_list_entry_lookup(list
, lcom
, direct
);
1032 entry
= community_list_entry_lookup(list
, str
, direct
);
1035 lcommunity_free(&lcom
);
1037 bgp_regex_free(regex
);
1040 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1042 community_list_entry_delete(list
, entry
);
1047 /* Set extcommunity-list. */
1048 int extcommunity_list_set(struct community_list_handler
*ch
, const char *name
,
1049 const char *str
, int direct
, int style
)
1051 struct community_entry
*entry
= NULL
;
1052 struct community_list
*list
;
1053 struct ecommunity
*ecom
= NULL
;
1054 regex_t
*regex
= NULL
;
1057 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1059 /* Get community list. */
1060 list
= community_list_get(ch
, name
, EXTCOMMUNITY_LIST_MASTER
);
1062 /* When community-list already has entry, new entry should have same
1063 style. If you want to have mixed style community-list, you can
1064 comment out this check. */
1065 if (!community_list_empty_p(list
)) {
1066 struct community_entry
*first
;
1070 if (style
!= first
->style
) {
1071 return (first
->style
== EXTCOMMUNITY_LIST_STANDARD
1072 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1073 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT
);
1077 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1078 ecom
= ecommunity_str2com(str
, 0, 1);
1080 regex
= bgp_regcomp(str
);
1082 if (!ecom
&& !regex
)
1083 return COMMUNITY_LIST_ERR_MALFORMED_VAL
;
1087 ecommunity_ecom2str(ecom
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1089 entry
= community_entry_new();
1090 entry
->direct
= direct
;
1091 entry
->style
= style
;
1094 entry
->config
= ecommunity_ecom2str(
1095 ecom
, ECOMMUNITY_FORMAT_COMMUNITY_LIST
, 0);
1097 entry
->config
= XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG
, str
);
1099 entry
->u
.ecom
= ecom
;
1102 /* Do not put duplicated community entry. */
1103 if (community_list_dup_check(list
, entry
))
1104 community_entry_free(entry
);
1106 community_list_entry_add(list
, entry
);
1107 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_ADDED
);
1113 /* Unset extcommunity-list.
1115 * When str is NULL, delete all extcommunity-list entries belonging to the
1118 int extcommunity_list_unset(struct community_list_handler
*ch
, const char *name
,
1119 const char *str
, int direct
, int style
)
1121 struct community_entry
*entry
= NULL
;
1122 struct community_list
*list
;
1123 struct ecommunity
*ecom
= NULL
;
1125 /* Lookup extcommunity list. */
1126 list
= community_list_lookup(ch
, name
, EXTCOMMUNITY_LIST_MASTER
);
1128 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1130 /* Delete all of entry belongs to this extcommunity-list. */
1132 community_list_delete(list
);
1133 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1137 if (style
== EXTCOMMUNITY_LIST_STANDARD
)
1138 ecom
= ecommunity_str2com(str
, 0, 1);
1141 entry
= community_list_entry_lookup(list
, ecom
, direct
);
1142 ecommunity_free(&ecom
);
1144 entry
= community_list_entry_lookup(list
, str
, direct
);
1147 return COMMUNITY_LIST_ERR_CANT_FIND_LIST
;
1149 community_list_entry_delete(list
, entry
);
1150 route_map_notify_dependencies(name
, RMAP_EVENT_ECLIST_DELETED
);
1155 /* Initializa community-list. Return community-list handler. */
1156 struct community_list_handler
*community_list_init(void)
1158 struct community_list_handler
*ch
;
1159 ch
= XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER
,
1160 sizeof(struct community_list_handler
));
1164 /* Terminate community-list. */
1165 void community_list_terminate(struct community_list_handler
*ch
)
1167 struct community_list_master
*cm
;
1168 struct community_list
*list
;
1170 cm
= &ch
->community_list
;
1171 while ((list
= cm
->num
.head
) != NULL
)
1172 community_list_delete(list
);
1173 while ((list
= cm
->str
.head
) != NULL
)
1174 community_list_delete(list
);
1176 cm
= &ch
->lcommunity_list
;
1177 while ((list
= cm
->num
.head
) != NULL
)
1178 community_list_delete(list
);
1179 while ((list
= cm
->str
.head
) != NULL
)
1180 community_list_delete(list
);
1182 cm
= &ch
->extcommunity_list
;
1183 while ((list
= cm
->num
.head
) != NULL
)
1184 community_list_delete(list
);
1185 while ((list
= cm
->str
.head
) != NULL
)
1186 community_list_delete(list
);
1188 XFREE(MTYPE_COMMUNITY_LIST_HANDLER
, ch
);