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
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_community.h"
30 #include "bgpd/bgp_ecommunity.h"
31 #include "bgpd/bgp_lcommunity.h"
32 #include "bgpd/bgp_aspath.h"
33 #include "bgpd/bgp_regex.h"
34 #include "bgpd/bgp_clist.h"
36 /* Lookup master structure for community-list or
38 struct community_list_master *
39 community_list_master_lookup (struct community_list_handler *ch, int master)
44 case COMMUNITY_LIST_MASTER:
45 return &ch->community_list;
46 case EXTCOMMUNITY_LIST_MASTER:
47 return &ch->extcommunity_list;
48 case LARGE_COMMUNITY_LIST_MASTER:
49 return &ch->lcommunity_list;
54 /* Allocate a new community list entry. */
55 static struct community_entry *
56 community_entry_new (void)
58 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
61 /* Free community list entry. */
63 community_entry_free (struct community_entry *entry)
67 case COMMUNITY_LIST_STANDARD:
69 community_free (entry->u.com);
71 case LARGE_COMMUNITY_LIST_STANDARD:
73 lcommunity_free (&entry->u.lcom);
75 case EXTCOMMUNITY_LIST_STANDARD:
76 /* In case of standard extcommunity-list, configuration string
77 is made by ecommunity_ecom2str(). */
79 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
81 ecommunity_free (&entry->u.ecom);
83 case COMMUNITY_LIST_EXPANDED:
84 case EXTCOMMUNITY_LIST_EXPANDED:
85 case LARGE_COMMUNITY_LIST_EXPANDED:
87 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
89 bgp_regex_free (entry->reg);
93 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
96 /* Allocate a new community-list. */
97 static struct community_list *
98 community_list_new (void)
100 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
103 /* Free community-list. */
105 community_list_free (struct community_list *list)
108 XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name);
109 XFREE (MTYPE_COMMUNITY_LIST, list);
112 static struct community_list *
113 community_list_insert (struct community_list_handler *ch,
114 const char *name, int master)
118 struct community_list *new;
119 struct community_list *point;
120 struct community_list_list *list;
121 struct community_list_master *cm;
123 /* Lookup community-list master. */
124 cm = community_list_master_lookup (ch, master);
128 /* Allocate new community_list and copy given name. */
129 new = community_list_new ();
130 new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name);
132 /* If name is made by all digit character. We treat it as
134 for (number = 0, i = 0; i < strlen (name); i++)
136 if (isdigit ((int) name[i]))
137 number = (number * 10) + (name[i] - '0');
142 /* In case of name is all digit character */
143 if (i == strlen (name))
145 new->sort = COMMUNITY_LIST_NUMBER;
147 /* Set access_list to number list. */
150 for (point = list->head; point; point = point->next)
151 if (atol (point->name) >= number)
156 new->sort = COMMUNITY_LIST_STRING;
158 /* Set access_list to string list. */
161 /* Set point to insertion point. */
162 for (point = list->head; point; point = point->next)
163 if (strcmp (point->name, name) >= 0)
167 /* Link to upper list. */
170 /* In case of this is the first element of master. */
171 if (list->head == NULL)
173 list->head = list->tail = new;
177 /* In case of insertion is made at the tail of access_list. */
180 new->prev = list->tail;
181 list->tail->next = new;
186 /* In case of insertion is made at the head of access_list. */
187 if (point == list->head)
189 new->next = list->head;
190 list->head->prev = new;
195 /* Insertion is made at middle of the access_list. */
197 new->prev = point->prev;
200 point->prev->next = new;
206 struct community_list *
207 community_list_lookup (struct community_list_handler *ch,
208 const char *name, int master)
210 struct community_list *list;
211 struct community_list_master *cm;
216 cm = community_list_master_lookup (ch, master);
220 for (list = cm->num.head; list; list = list->next)
221 if (strcmp (list->name, name) == 0)
223 for (list = cm->str.head; list; list = list->next)
224 if (strcmp (list->name, name) == 0)
230 static struct community_list *
231 community_list_get (struct community_list_handler *ch,
232 const char *name, int master)
234 struct community_list *list;
236 list = community_list_lookup (ch, name, master);
238 list = community_list_insert (ch, name, master);
243 community_list_delete (struct community_list *list)
245 struct community_list_list *clist;
246 struct community_entry *entry, *next;
248 for (entry = list->head; entry; entry = next)
251 community_entry_free (entry);
254 clist = list->parent;
257 list->next->prev = list->prev;
259 clist->tail = list->prev;
262 list->prev->next = list->next;
264 clist->head = list->next;
266 community_list_free (list);
270 community_list_empty_p (struct community_list *list)
272 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
275 /* Add community-list entry to the list. */
277 community_list_entry_add (struct community_list *list,
278 struct community_entry *entry)
281 entry->prev = list->tail;
284 list->tail->next = entry;
290 /* Delete community-list entry from the list. */
292 community_list_entry_delete (struct community_list *list,
293 struct community_entry *entry, int style)
296 entry->next->prev = entry->prev;
298 list->tail = entry->prev;
301 entry->prev->next = entry->next;
303 list->head = entry->next;
305 community_entry_free (entry);
307 if (community_list_empty_p (list))
308 community_list_delete (list);
311 /* Lookup community-list entry from the list. */
312 static struct community_entry *
313 community_list_entry_lookup (struct community_list *list, const void *arg,
316 struct community_entry *entry;
318 for (entry = list->head; entry; entry = entry->next)
320 switch (entry->style)
322 case COMMUNITY_LIST_STANDARD:
323 if (community_cmp (entry->u.com, arg))
326 case LARGE_COMMUNITY_LIST_STANDARD:
327 if (lcommunity_cmp (entry->u.lcom, arg))
330 case EXTCOMMUNITY_LIST_STANDARD:
331 if (ecommunity_cmp (entry->u.ecom, arg))
334 case COMMUNITY_LIST_EXPANDED:
335 case EXTCOMMUNITY_LIST_EXPANDED:
336 case LARGE_COMMUNITY_LIST_EXPANDED:
337 if (strcmp (entry->config, arg) == 0)
348 community_str_get (struct community *com, int i)
357 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
358 comval = ntohl (comval);
362 case COMMUNITY_INTERNET:
363 len = strlen (" internet");
365 case COMMUNITY_NO_EXPORT:
366 len = strlen (" no-export");
368 case COMMUNITY_NO_ADVERTISE:
369 len = strlen (" no-advertise");
371 case COMMUNITY_LOCAL_AS:
372 len = strlen (" local-AS");
375 len = strlen (" 65536:65535");
379 /* Allocate memory. */
380 str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
384 case COMMUNITY_INTERNET:
385 strcpy (pnt, "internet");
386 pnt += strlen ("internet");
388 case COMMUNITY_NO_EXPORT:
389 strcpy (pnt, "no-export");
390 pnt += strlen ("no-export");
392 case COMMUNITY_NO_ADVERTISE:
393 strcpy (pnt, "no-advertise");
394 pnt += strlen ("no-advertise");
396 case COMMUNITY_LOCAL_AS:
397 strcpy (pnt, "local-AS");
398 pnt += strlen ("local-AS");
401 as = (comval >> 16) & 0xFFFF;
402 val = comval & 0xFFFF;
403 sprintf (pnt, "%u:%d", as, val);
413 /* Internal function to perform regular expression match for
414 * * a single community. */
416 community_regexp_include (regex_t * reg, struct community *com, int i)
421 /* When there is no communities attribute it is treated as empty
423 if (com == NULL || com->size == 0)
424 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
426 str = community_str_get (com, i);
428 /* Regular expression match. */
429 rv = regexec (reg, str, 0, NULL, 0);
431 XFREE(MTYPE_COMMUNITY_STR, str);
440 /* Internal function to perform regular expression match for community
443 community_regexp_match (struct community *com, regex_t * reg)
447 /* When there is no communities attribute it is treated as empty
449 if (com == NULL || com->size == 0)
452 str = community_str (com);
454 /* Regular expression match. */
455 if (regexec (reg, str, 0, NULL, 0) == 0)
463 lcommunity_str_get (struct lcommunity *lcom, int i)
465 struct lcommunity_val lcomval;
466 u_int32_t globaladmin;
467 u_int32_t localdata1;
468 u_int32_t localdata2;
474 ptr += (i * LCOMMUNITY_SIZE);
476 memcpy (&lcomval, ptr, LCOMMUNITY_SIZE);
478 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
479 str = pnt = XMALLOC (MTYPE_LCOMMUNITY_STR, 48);
481 ptr = (u_char *)lcomval.val;
482 globaladmin = (*ptr++ << 24);
483 globaladmin |= (*ptr++ << 16);
484 globaladmin |= (*ptr++ << 8);
485 globaladmin |= (*ptr++);
487 localdata1 = (*ptr++ << 24);
488 localdata1 |= (*ptr++ << 16);
489 localdata1 |= (*ptr++ << 8);
490 localdata1 |= (*ptr++);
492 localdata2 = (*ptr++ << 24);
493 localdata2 |= (*ptr++ << 16);
494 localdata2 |= (*ptr++ << 8);
495 localdata2 |= (*ptr++);
497 sprintf (pnt, "%u:%u:%u", globaladmin, localdata1, localdata2);
504 /* Internal function to perform regular expression match for
505 * * a single community. */
507 lcommunity_regexp_include (regex_t * reg, struct lcommunity *lcom, int i)
511 /* When there is no communities attribute it is treated as empty
513 if (lcom == NULL || lcom->size == 0)
516 str = lcommunity_str_get (lcom, i);
518 /* Regular expression match. */
519 if (regexec (reg, str, 0, NULL, 0) == 0)
527 lcommunity_regexp_match (struct lcommunity *com, regex_t * reg)
531 /* When there is no communities attribute it is treated as empty
533 if (com == NULL || com->size == 0)
536 str = lcommunity_str (com);
538 /* Regular expression match. */
539 if (regexec (reg, str, 0, NULL, 0) == 0)
548 ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
552 /* When there is no communities attribute it is treated as empty
554 if (ecom == NULL || ecom->size == 0)
557 str = ecommunity_str (ecom);
559 /* Regular expression match. */
560 if (regexec (reg, str, 0, NULL, 0) == 0)
567 /* When given community attribute matches to the community-list return
570 community_list_match (struct community *com, struct community_list *list)
572 struct community_entry *entry;
574 for (entry = list->head; entry; entry = entry->next)
577 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
579 if (entry->style == COMMUNITY_LIST_STANDARD)
581 if (community_include (entry->u.com, COMMUNITY_INTERNET))
582 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
584 if (community_match (com, entry->u.com))
585 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
587 else if (entry->style == COMMUNITY_LIST_EXPANDED)
589 if (community_regexp_match (com, entry->reg))
590 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
597 lcommunity_list_match (struct lcommunity *lcom, struct community_list *list)
599 struct community_entry *entry;
601 for (entry = list->head; entry; entry = entry->next)
604 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
606 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD)
608 if (lcommunity_match (lcom, entry->u.lcom))
609 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
611 else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED)
613 if (lcommunity_regexp_match (lcom, entry->reg))
614 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
621 ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
623 struct community_entry *entry;
625 for (entry = list->head; entry; entry = entry->next)
628 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
630 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
632 if (ecommunity_match (ecom, entry->u.ecom))
633 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
635 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
637 if (ecommunity_regexp_match (ecom, entry->reg))
638 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
644 /* Perform exact matching. In case of expanded community-list, do
645 same thing as community_list_match(). */
647 community_list_exact_match (struct community *com,
648 struct community_list *list)
650 struct community_entry *entry;
652 for (entry = list->head; entry; entry = entry->next)
655 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
657 if (entry->style == COMMUNITY_LIST_STANDARD)
659 if (community_include (entry->u.com, COMMUNITY_INTERNET))
660 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
662 if (community_cmp (com, entry->u.com))
663 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
665 else if (entry->style == COMMUNITY_LIST_EXPANDED)
667 if (community_regexp_match (com, entry->reg))
668 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
674 /* Delete all permitted communities in the list from com. */
676 community_list_match_delete (struct community *com,
677 struct community_list *list)
679 struct community_entry *entry;
681 u_int32_t com_index_to_delete[com->size];
682 int delete_index = 0;
685 /* Loop over each community value and evaluate each against the
686 * community-list. If we need to delete a community value add its index to
687 * com_index_to_delete.
689 for (i = 0; i < com->size; i++)
691 val = community_val_get (com, i);
693 for (entry = list->head; entry; entry = entry->next)
697 if (entry->direct == COMMUNITY_PERMIT)
699 com_index_to_delete[delete_index] = i;
705 else if ((entry->style == COMMUNITY_LIST_STANDARD)
706 && (community_include (entry->u.com, COMMUNITY_INTERNET)
707 || community_include (entry->u.com, val) ))
709 if (entry->direct == COMMUNITY_PERMIT)
711 com_index_to_delete[delete_index] = i;
717 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
718 && community_regexp_include (entry->reg, com, i))
720 if (entry->direct == COMMUNITY_PERMIT)
722 com_index_to_delete[delete_index] = i;
730 /* Delete all of the communities we flagged for deletion */
731 for (i = delete_index-1; i >= 0; i--)
733 val = community_val_get (com, com_index_to_delete[i]);
734 community_del_val (com, &val);
740 /* To avoid duplicated entry in the community-list, this function
741 compares specified entry to existing entry. */
743 community_list_dup_check (struct community_list *list,
744 struct community_entry *new)
746 struct community_entry *entry;
748 for (entry = list->head; entry; entry = entry->next)
750 if (entry->style != new->style)
753 if (entry->direct != new->direct)
756 if (entry->any != new->any)
762 switch (entry->style)
764 case COMMUNITY_LIST_STANDARD:
765 if (community_cmp (entry->u.com, new->u.com))
768 case EXTCOMMUNITY_LIST_STANDARD:
769 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
772 case LARGE_COMMUNITY_LIST_STANDARD:
773 if (lcommunity_cmp (entry->u.lcom, new->u.lcom))
776 case COMMUNITY_LIST_EXPANDED:
777 case EXTCOMMUNITY_LIST_EXPANDED:
778 case LARGE_COMMUNITY_LIST_EXPANDED:
779 if (entry->config && new->config
780 && strcmp (entry->config, new->config) == 0)
782 if (!entry->config && !new->config)
792 /* Set community-list. */
794 community_list_set (struct community_list_handler *ch,
795 const char *name, const char *str, int direct, int style)
797 struct community_entry *entry = NULL;
798 struct community_list *list;
799 struct community *com = NULL;
800 regex_t *regex = NULL;
802 /* Get community list. */
803 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
805 /* When community-list already has entry, new entry should have same
806 style. If you want to have mixed style community-list, you can
807 comment out this check. */
808 if (!community_list_empty_p (list))
810 struct community_entry *first;
814 if (style != first->style)
816 return (first->style == COMMUNITY_LIST_STANDARD
817 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
818 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
824 if (style == COMMUNITY_LIST_STANDARD)
825 com = community_str2com (str);
827 regex = bgp_regcomp (str);
829 if (! com && ! regex)
830 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
833 entry = community_entry_new ();
834 entry->direct = direct;
835 entry->style = style;
836 entry->any = (str ? 0 : 1);
839 entry->config = (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);
850 /* Unset community-list. When str is NULL, delete all of
851 community-list entry belongs to the specified name. */
853 community_list_unset (struct community_list_handler *ch,
854 const char *name, const char *str,
855 int direct, int style)
857 struct community_entry *entry = NULL;
858 struct community_list *list;
859 struct community *com = NULL;
860 regex_t *regex = NULL;
862 /* Lookup community list. */
863 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
865 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
867 /* Delete all of entry belongs to this community-list. */
870 community_list_delete (list);
874 if (style == COMMUNITY_LIST_STANDARD)
875 com = community_str2com (str);
877 regex = bgp_regcomp (str);
879 if (! com && ! regex)
880 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
883 entry = community_list_entry_lookup (list, com, direct);
885 entry = community_list_entry_lookup (list, str, direct);
888 community_free (com);
890 bgp_regex_free (regex);
893 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
895 community_list_entry_delete (list, entry, style);
900 /* Delete all permitted large communities in the list from com. */
902 lcommunity_list_match_delete (struct lcommunity *lcom,
903 struct community_list *list)
905 struct community_entry *entry;
906 u_int32_t com_index_to_delete[lcom->size];
908 int delete_index = 0;
911 /* Loop over each lcommunity value and evaluate each against the
912 * community-list. If we need to delete a community value add its index to
913 * com_index_to_delete.
916 for (i = 0; i < lcom->size; i++)
918 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
919 for (entry = list->head; entry; entry = entry->next)
923 if (entry->direct == COMMUNITY_PERMIT)
925 com_index_to_delete[delete_index] = i;
931 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
932 && lcommunity_include (entry->u.lcom, ptr) )
934 if (entry->direct == COMMUNITY_PERMIT)
936 com_index_to_delete[delete_index] = i;
942 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
944 && lcommunity_regexp_include (entry->reg, lcom, i))
946 if (entry->direct == COMMUNITY_PERMIT)
948 com_index_to_delete[delete_index] = i;
956 /* Delete all of the communities we flagged for deletion */
958 for (i = delete_index-1; i >= 0; i--)
960 ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE);
961 lcommunity_del_val (lcom, ptr);
967 /* Set lcommunity-list. */
969 lcommunity_list_set (struct community_list_handler *ch,
970 const char *name, const char *str, int direct, int style)
972 struct community_entry *entry = NULL;
973 struct community_list *list;
974 struct lcommunity *lcom = NULL;
975 regex_t *regex = NULL;
977 /* Get community list. */
978 list = community_list_get (ch, name, LARGE_COMMUNITY_LIST_MASTER);
980 /* When community-list already has entry, new entry should have same
981 style. If you want to have mixed style community-list, you can
982 comment out this check. */
983 if (!community_list_empty_p (list))
985 struct community_entry *first;
989 if (style != first->style)
991 return (first->style == COMMUNITY_LIST_STANDARD
992 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
993 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
999 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1000 lcom = lcommunity_str2com (str);
1002 regex = bgp_regcomp (str);
1004 if (! lcom && ! regex)
1005 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1008 entry = community_entry_new ();
1009 entry->direct = direct;
1010 entry->style = style;
1011 entry->any = (str ? 0 : 1);
1012 entry->u.lcom = lcom;
1015 entry->config = lcommunity_lcom2str (lcom, LCOMMUNITY_FORMAT_COMMUNITY_LIST);
1017 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1019 entry->config = NULL;
1021 /* Do not put duplicated community entry. */
1022 if (community_list_dup_check (list, entry))
1023 community_entry_free (entry);
1025 community_list_entry_add (list, entry);
1030 /* Unset community-list. When str is NULL, delete all of
1031 community-list entry belongs to the specified name. */
1033 lcommunity_list_unset (struct community_list_handler *ch,
1034 const char *name, const char *str,
1035 int direct, int style)
1037 struct community_entry *entry = NULL;
1038 struct community_list *list;
1039 struct lcommunity *lcom = NULL;
1040 regex_t *regex = NULL;
1042 /* Lookup community list. */
1043 list = community_list_lookup (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1045 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1047 /* Delete all of entry belongs to this community-list. */
1050 community_list_delete (list);
1054 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1055 lcom = lcommunity_str2com (str);
1057 regex = bgp_regcomp (str);
1059 if (! lcom && ! regex)
1060 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1063 entry = community_list_entry_lookup (list, lcom, direct);
1065 entry = community_list_entry_lookup (list, str, direct);
1068 lcommunity_free (&lcom);
1070 bgp_regex_free (regex);
1073 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1075 community_list_entry_delete (list, entry, style);
1080 /* Set extcommunity-list. */
1082 extcommunity_list_set (struct community_list_handler *ch,
1083 const char *name, const char *str,
1084 int direct, int style)
1086 struct community_entry *entry = NULL;
1087 struct community_list *list;
1088 struct ecommunity *ecom = NULL;
1089 regex_t *regex = NULL;
1093 /* Get community list. */
1094 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
1096 /* When community-list already has entry, new entry should have same
1097 style. If you want to have mixed style community-list, you can
1098 comment out this check. */
1099 if (!community_list_empty_p (list))
1101 struct community_entry *first;
1105 if (style != first->style)
1107 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1108 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1109 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1115 if (style == EXTCOMMUNITY_LIST_STANDARD)
1116 ecom = ecommunity_str2com (str, 0, 1);
1118 regex = bgp_regcomp (str);
1120 if (! ecom && ! regex)
1121 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1125 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
1127 entry = community_entry_new ();
1128 entry->direct = direct;
1129 entry->style = style;
1130 entry->any = (str ? 0 : 1);
1132 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
1134 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1136 entry->config = NULL;
1137 entry->u.ecom = ecom;
1140 /* Do not put duplicated community entry. */
1141 if (community_list_dup_check (list, entry))
1142 community_entry_free (entry);
1144 community_list_entry_add (list, entry);
1149 /* Unset extcommunity-list. When str is NULL, delete all of
1150 extcommunity-list entry belongs to the specified name. */
1152 extcommunity_list_unset (struct community_list_handler *ch,
1153 const char *name, const char *str,
1154 int direct, int style)
1156 struct community_entry *entry = NULL;
1157 struct community_list *list;
1158 struct ecommunity *ecom = NULL;
1159 regex_t *regex = NULL;
1161 /* Lookup extcommunity list. */
1162 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
1164 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1166 /* Delete all of entry belongs to this extcommunity-list. */
1169 community_list_delete (list);
1173 if (style == EXTCOMMUNITY_LIST_STANDARD)
1174 ecom = ecommunity_str2com (str, 0, 1);
1176 regex = bgp_regcomp (str);
1178 if (! ecom && ! regex)
1179 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1182 entry = community_list_entry_lookup (list, ecom, direct);
1184 entry = community_list_entry_lookup (list, str, direct);
1187 ecommunity_free (&ecom);
1189 bgp_regex_free (regex);
1192 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1194 community_list_entry_delete (list, entry, style);
1199 /* Initializa community-list. Return community-list handler. */
1200 struct community_list_handler *
1201 community_list_init (void)
1203 struct community_list_handler *ch;
1204 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
1205 sizeof (struct community_list_handler));
1209 /* Terminate community-list. */
1211 community_list_terminate (struct community_list_handler *ch)
1213 struct community_list_master *cm;
1214 struct community_list *list;
1216 cm = &ch->community_list;
1217 while ((list = cm->num.head) != NULL)
1218 community_list_delete (list);
1219 while ((list = cm->str.head) != NULL)
1220 community_list_delete (list);
1222 cm = &ch->lcommunity_list;
1223 while ((list = cm->num.head) != NULL)
1224 community_list_delete (list);
1225 while ((list = cm->str.head) != NULL)
1226 community_list_delete (list);
1228 cm = &ch->extcommunity_list;
1229 while ((list = cm->num.head) != NULL)
1230 community_list_delete (list);
1231 while ((list = cm->str.head) != NULL)
1232 community_list_delete (list);
1234 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);