]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_clist.c
bgpd: Fix `ip as-path access-list ...` breakage
[mirror_frr.git] / bgpd / bgp_clist.c
1 /* BGP community-list and extcommunity-list.
2 Copyright (C) 1999 Kunihiro Ishiguro
3
4 This file is part of GNU Zebra.
5
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
9 later version.
10
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.
15
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
19 02111-1307, USA. */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "prefix.h"
25 #include "memory.h"
26 #include "queue.h"
27 #include "filter.h"
28
29 #include "bgpd/bgpd.h"
30 #include "bgpd/bgp_community.h"
31 #include "bgpd/bgp_ecommunity.h"
32 #include "bgpd/bgp_aspath.h"
33 #include "bgpd/bgp_regex.h"
34 #include "bgpd/bgp_clist.h"
35
36 /* Lookup master structure for community-list or
37 extcommunity-list. */
38 struct community_list_master *
39 community_list_master_lookup (struct community_list_handler *ch, int master)
40 {
41 if (ch)
42 switch (master)
43 {
44 case COMMUNITY_LIST_MASTER:
45 return &ch->community_list;
46 case EXTCOMMUNITY_LIST_MASTER:
47 return &ch->extcommunity_list;
48 }
49 return NULL;
50 }
51
52 /* Allocate a new community list entry. */
53 static struct community_entry *
54 community_entry_new (void)
55 {
56 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
57 }
58
59 /* Free community list entry. */
60 static void
61 community_entry_free (struct community_entry *entry)
62 {
63 switch (entry->style)
64 {
65 case COMMUNITY_LIST_STANDARD:
66 if (entry->u.com)
67 community_free (entry->u.com);
68 break;
69 case EXTCOMMUNITY_LIST_STANDARD:
70 /* In case of standard extcommunity-list, configuration string
71 is made by ecommunity_ecom2str(). */
72 if (entry->config)
73 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
74 if (entry->u.ecom)
75 ecommunity_free (&entry->u.ecom);
76 break;
77 case COMMUNITY_LIST_EXPANDED:
78 case EXTCOMMUNITY_LIST_EXPANDED:
79 if (entry->config)
80 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
81 if (entry->reg)
82 bgp_regex_free (entry->reg);
83 default:
84 break;
85 }
86 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
87 }
88
89 /* Allocate a new community-list. */
90 static struct community_list *
91 community_list_new (void)
92 {
93 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
94 }
95
96 /* Free community-list. */
97 static void
98 community_list_free (struct community_list *list)
99 {
100 if (list->name)
101 XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name);
102 XFREE (MTYPE_COMMUNITY_LIST, list);
103 }
104
105 static struct community_list *
106 community_list_insert (struct community_list_handler *ch,
107 const char *name, int master)
108 {
109 size_t i;
110 long number;
111 struct community_list *new;
112 struct community_list *point;
113 struct community_list_list *list;
114 struct community_list_master *cm;
115
116 /* Lookup community-list master. */
117 cm = community_list_master_lookup (ch, master);
118 if (!cm)
119 return NULL;
120
121 /* Allocate new community_list and copy given name. */
122 new = community_list_new ();
123 new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name);
124
125 /* If name is made by all digit character. We treat it as
126 number. */
127 for (number = 0, i = 0; i < strlen (name); i++)
128 {
129 if (isdigit ((int) name[i]))
130 number = (number * 10) + (name[i] - '0');
131 else
132 break;
133 }
134
135 /* In case of name is all digit character */
136 if (i == strlen (name))
137 {
138 new->sort = COMMUNITY_LIST_NUMBER;
139
140 /* Set access_list to number list. */
141 list = &cm->num;
142
143 for (point = list->head; point; point = point->next)
144 if (atol (point->name) >= number)
145 break;
146 }
147 else
148 {
149 new->sort = COMMUNITY_LIST_STRING;
150
151 /* Set access_list to string list. */
152 list = &cm->str;
153
154 /* Set point to insertion point. */
155 for (point = list->head; point; point = point->next)
156 if (strcmp (point->name, name) >= 0)
157 break;
158 }
159
160 /* Link to upper list. */
161 new->parent = list;
162
163 /* In case of this is the first element of master. */
164 if (list->head == NULL)
165 {
166 list->head = list->tail = new;
167 return new;
168 }
169
170 /* In case of insertion is made at the tail of access_list. */
171 if (point == NULL)
172 {
173 new->prev = list->tail;
174 list->tail->next = new;
175 list->tail = new;
176 return new;
177 }
178
179 /* In case of insertion is made at the head of access_list. */
180 if (point == list->head)
181 {
182 new->next = list->head;
183 list->head->prev = new;
184 list->head = new;
185 return new;
186 }
187
188 /* Insertion is made at middle of the access_list. */
189 new->next = point;
190 new->prev = point->prev;
191
192 if (point->prev)
193 point->prev->next = new;
194 point->prev = new;
195
196 return new;
197 }
198
199 struct community_list *
200 community_list_lookup (struct community_list_handler *ch,
201 const char *name, int master)
202 {
203 struct community_list *list;
204 struct community_list_master *cm;
205
206 if (!name)
207 return NULL;
208
209 cm = community_list_master_lookup (ch, master);
210 if (!cm)
211 return NULL;
212
213 for (list = cm->num.head; list; list = list->next)
214 if (strcmp (list->name, name) == 0)
215 return list;
216 for (list = cm->str.head; list; list = list->next)
217 if (strcmp (list->name, name) == 0)
218 return list;
219
220 return NULL;
221 }
222
223 static struct community_list *
224 community_list_get (struct community_list_handler *ch,
225 const char *name, int master)
226 {
227 struct community_list *list;
228
229 list = community_list_lookup (ch, name, master);
230 if (!list)
231 list = community_list_insert (ch, name, master);
232 return list;
233 }
234
235 static void
236 community_list_delete (struct community_list *list)
237 {
238 struct community_list_list *clist;
239 struct community_entry *entry, *next;
240
241 for (entry = list->head; entry; entry = next)
242 {
243 next = entry->next;
244 community_entry_free (entry);
245 }
246
247 clist = list->parent;
248
249 if (list->next)
250 list->next->prev = list->prev;
251 else
252 clist->tail = list->prev;
253
254 if (list->prev)
255 list->prev->next = list->next;
256 else
257 clist->head = list->next;
258
259 community_list_free (list);
260 }
261
262 static int
263 community_list_empty_p (struct community_list *list)
264 {
265 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
266 }
267
268 /* Add community-list entry to the list. */
269 static void
270 community_list_entry_add (struct community_list *list,
271 struct community_entry *entry)
272 {
273 entry->next = NULL;
274 entry->prev = list->tail;
275
276 if (list->tail)
277 list->tail->next = entry;
278 else
279 list->head = entry;
280 list->tail = entry;
281 }
282
283 /* Delete community-list entry from the list. */
284 static void
285 community_list_entry_delete (struct community_list *list,
286 struct community_entry *entry, int style)
287 {
288 if (entry->next)
289 entry->next->prev = entry->prev;
290 else
291 list->tail = entry->prev;
292
293 if (entry->prev)
294 entry->prev->next = entry->next;
295 else
296 list->head = entry->next;
297
298 community_entry_free (entry);
299
300 if (community_list_empty_p (list))
301 community_list_delete (list);
302 }
303
304 /* Lookup community-list entry from the list. */
305 static struct community_entry *
306 community_list_entry_lookup (struct community_list *list, const void *arg,
307 int direct)
308 {
309 struct community_entry *entry;
310
311 for (entry = list->head; entry; entry = entry->next)
312 {
313 switch (entry->style)
314 {
315 case COMMUNITY_LIST_STANDARD:
316 if (entry->direct == direct && community_cmp (entry->u.com, arg))
317 return entry;
318 break;
319 case EXTCOMMUNITY_LIST_STANDARD:
320 if (entry->direct == direct && ecommunity_cmp (entry->u.ecom, arg))
321 return entry;
322 break;
323 case COMMUNITY_LIST_EXPANDED:
324 case EXTCOMMUNITY_LIST_EXPANDED:
325 if (entry->direct == direct && strcmp (entry->config, arg) == 0)
326 return entry;
327 break;
328 default:
329 break;
330 }
331 }
332 return NULL;
333 }
334
335 static char *
336 community_str_get (struct community *com, int i)
337 {
338 int len;
339 u_int32_t comval;
340 u_int16_t as;
341 u_int16_t val;
342 char *str;
343 char *pnt;
344
345 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
346 comval = ntohl (comval);
347
348 switch (comval)
349 {
350 case COMMUNITY_INTERNET:
351 len = strlen (" internet");
352 break;
353 case COMMUNITY_NO_EXPORT:
354 len = strlen (" no-export");
355 break;
356 case COMMUNITY_NO_ADVERTISE:
357 len = strlen (" no-advertise");
358 break;
359 case COMMUNITY_LOCAL_AS:
360 len = strlen (" local-AS");
361 break;
362 default:
363 len = strlen (" 65536:65535");
364 break;
365 }
366
367 /* Allocate memory. */
368 str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
369
370 switch (comval)
371 {
372 case COMMUNITY_INTERNET:
373 strcpy (pnt, "internet");
374 pnt += strlen ("internet");
375 break;
376 case COMMUNITY_NO_EXPORT:
377 strcpy (pnt, "no-export");
378 pnt += strlen ("no-export");
379 break;
380 case COMMUNITY_NO_ADVERTISE:
381 strcpy (pnt, "no-advertise");
382 pnt += strlen ("no-advertise");
383 break;
384 case COMMUNITY_LOCAL_AS:
385 strcpy (pnt, "local-AS");
386 pnt += strlen ("local-AS");
387 break;
388 default:
389 as = (comval >> 16) & 0xFFFF;
390 val = comval & 0xFFFF;
391 sprintf (pnt, "%u:%d", as, val);
392 pnt += strlen (pnt);
393 break;
394 }
395
396 *pnt = '\0';
397
398 return str;
399 }
400
401 /* Internal function to perform regular expression match for
402 * * a single community. */
403 static int
404 community_regexp_include (regex_t * reg, struct community *com, int i)
405 {
406 char *str;
407 int rv;
408
409 /* When there is no communities attribute it is treated as empty
410 * string. */
411 if (com == NULL || com->size == 0)
412 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
413 else
414 str = community_str_get (com, i);
415
416 /* Regular expression match. */
417 rv = regexec (reg, str, 0, NULL, 0);
418
419 XFREE(MTYPE_COMMUNITY_STR, str);
420
421 if (rv == 0)
422 return 1;
423
424 /* No match. */
425 return 0;
426 }
427
428 /* Internal function to perform regular expression match for community
429 attribute. */
430 static int
431 community_regexp_match (struct community *com, regex_t * reg)
432 {
433 const char *str;
434
435 /* When there is no communities attribute it is treated as empty
436 string. */
437 if (com == NULL || com->size == 0)
438 str = "";
439 else
440 str = community_str (com);
441
442 /* Regular expression match. */
443 if (regexec (reg, str, 0, NULL, 0) == 0)
444 return 1;
445
446 /* No match. */
447 return 0;
448 }
449
450 static int
451 ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
452 {
453 const char *str;
454
455 /* When there is no communities attribute it is treated as empty
456 string. */
457 if (ecom == NULL || ecom->size == 0)
458 str = "";
459 else
460 str = ecommunity_str (ecom);
461
462 /* Regular expression match. */
463 if (regexec (reg, str, 0, NULL, 0) == 0)
464 return 1;
465
466 /* No match. */
467 return 0;
468 }
469
470 #if 0
471 /* Delete community attribute using regular expression match. Return
472 modified communites attribute. */
473 static struct community *
474 community_regexp_delete (struct community *com, regex_t * reg)
475 {
476 int i;
477 u_int32_t comval;
478 /* Maximum is "65535:65535" + '\0'. */
479 char c[12];
480 const char *str;
481
482 if (!com)
483 return NULL;
484
485 i = 0;
486 while (i < com->size)
487 {
488 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
489 comval = ntohl (comval);
490
491 switch (comval)
492 {
493 case COMMUNITY_INTERNET:
494 str = "internet";
495 break;
496 case COMMUNITY_NO_EXPORT:
497 str = "no-export";
498 break;
499 case COMMUNITY_NO_ADVERTISE:
500 str = "no-advertise";
501 break;
502 case COMMUNITY_LOCAL_AS:
503 str = "local-AS";
504 break;
505 default:
506 sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF, comval & 0xFFFF);
507 str = c;
508 break;
509 }
510
511 if (regexec (reg, str, 0, NULL, 0) == 0)
512 community_del_val (com, com_nthval (com, i));
513 else
514 i++;
515 }
516 return com;
517 }
518 #endif
519
520 /* When given community attribute matches to the community-list return
521 1 else return 0. */
522 int
523 community_list_match (struct community *com, struct community_list *list)
524 {
525 struct community_entry *entry;
526
527 for (entry = list->head; entry; entry = entry->next)
528 {
529 if (entry->any)
530 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
531
532 if (entry->style == COMMUNITY_LIST_STANDARD)
533 {
534 if (community_include (entry->u.com, COMMUNITY_INTERNET))
535 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
536
537 if (community_match (com, entry->u.com))
538 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
539 }
540 else if (entry->style == COMMUNITY_LIST_EXPANDED)
541 {
542 if (community_regexp_match (com, entry->reg))
543 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
544 }
545 }
546 return 0;
547 }
548
549 int
550 ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
551 {
552 struct community_entry *entry;
553
554 for (entry = list->head; entry; entry = entry->next)
555 {
556 if (entry->any)
557 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
558
559 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
560 {
561 if (ecommunity_match (ecom, entry->u.ecom))
562 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
563 }
564 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
565 {
566 if (ecommunity_regexp_match (ecom, entry->reg))
567 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
568 }
569 }
570 return 0;
571 }
572
573 /* Perform exact matching. In case of expanded community-list, do
574 same thing as community_list_match(). */
575 int
576 community_list_exact_match (struct community *com,
577 struct community_list *list)
578 {
579 struct community_entry *entry;
580
581 for (entry = list->head; entry; entry = entry->next)
582 {
583 if (entry->any)
584 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
585
586 if (entry->style == COMMUNITY_LIST_STANDARD)
587 {
588 if (community_include (entry->u.com, COMMUNITY_INTERNET))
589 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
590
591 if (community_cmp (com, entry->u.com))
592 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
593 }
594 else if (entry->style == COMMUNITY_LIST_EXPANDED)
595 {
596 if (community_regexp_match (com, entry->reg))
597 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
598 }
599 }
600 return 0;
601 }
602
603 /* Delete all permitted communities in the list from com. */
604 struct community *
605 community_list_match_delete (struct community *com,
606 struct community_list *list)
607 {
608 struct community_entry *entry;
609 u_int32_t val;
610 u_int32_t com_index_to_delete[com->size];
611 int delete_index = 0;
612 int i;
613
614 /* Loop over each community value and evaluate each against the
615 * community-list. If we need to delete a community value add its index to
616 * com_index_to_delete.
617 */
618 for (i = 0; i < com->size; i++)
619 {
620 val = community_val_get (com, i);
621
622 for (entry = list->head; entry; entry = entry->next)
623 {
624 if (entry->any)
625 {
626 if (entry->direct == COMMUNITY_PERMIT)
627 {
628 com_index_to_delete[delete_index] = i;
629 delete_index++;
630 }
631 break;
632 }
633
634 else if ((entry->style == COMMUNITY_LIST_STANDARD)
635 && (community_include (entry->u.com, COMMUNITY_INTERNET)
636 || community_include (entry->u.com, val) ))
637 {
638 if (entry->direct == COMMUNITY_PERMIT)
639 {
640 com_index_to_delete[delete_index] = i;
641 delete_index++;
642 }
643 break;
644 }
645
646 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
647 && community_regexp_include (entry->reg, com, i))
648 {
649 if (entry->direct == COMMUNITY_PERMIT)
650 {
651 com_index_to_delete[delete_index] = i;
652 delete_index++;
653 }
654 break;
655 }
656 }
657 }
658
659 /* Delete all of the communities we flagged for deletion */
660 for (i = delete_index-1; i >= 0; i--)
661 {
662 val = community_val_get (com, com_index_to_delete[i]);
663 community_del_val (com, &val);
664 }
665
666 return com;
667 }
668
669 /* To avoid duplicated entry in the community-list, this function
670 compares specified entry to existing entry. */
671 static int
672 community_list_dup_check (struct community_list *list,
673 struct community_entry *new)
674 {
675 struct community_entry *entry;
676
677 for (entry = list->head; entry; entry = entry->next)
678 {
679 if (entry->style != new->style)
680 continue;
681
682 if (entry->direct != new->direct)
683 continue;
684
685 if (entry->any != new->any)
686 continue;
687
688 if (entry->any)
689 return 1;
690
691 switch (entry->style)
692 {
693 case COMMUNITY_LIST_STANDARD:
694 if (community_cmp (entry->u.com, new->u.com))
695 return 1;
696 break;
697 case EXTCOMMUNITY_LIST_STANDARD:
698 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
699 return 1;
700 break;
701 case COMMUNITY_LIST_EXPANDED:
702 case EXTCOMMUNITY_LIST_EXPANDED:
703 if (strcmp (entry->config, new->config) == 0)
704 return 1;
705 break;
706 default:
707 break;
708 }
709 }
710 return 0;
711 }
712
713 /* Set community-list. */
714 int
715 community_list_set (struct community_list_handler *ch,
716 const char *name, const char *str, int direct, int style)
717 {
718 struct community_entry *entry = NULL;
719 struct community_list *list;
720 struct community *com = NULL;
721 regex_t *regex = NULL;
722
723 /* Get community list. */
724 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
725
726 /* When community-list already has entry, new entry should have same
727 style. If you want to have mixed style community-list, you can
728 comment out this check. */
729 if (!community_list_empty_p (list))
730 {
731 struct community_entry *first;
732
733 first = list->head;
734
735 if (style != first->style)
736 {
737 return (first->style == COMMUNITY_LIST_STANDARD
738 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
739 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
740 }
741 }
742
743 if (str)
744 {
745 if (style == COMMUNITY_LIST_STANDARD)
746 com = community_str2com (str);
747 else
748 regex = bgp_regcomp (str);
749
750 if (! com && ! regex)
751 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
752 }
753
754 entry = community_entry_new ();
755 entry->direct = direct;
756 entry->style = style;
757 entry->any = (str ? 0 : 1);
758 entry->u.com = com;
759 entry->reg = regex;
760 entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
761
762 /* Do not put duplicated community entry. */
763 if (community_list_dup_check (list, entry))
764 community_entry_free (entry);
765 else
766 {
767 community_list_entry_add (list, entry);
768 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
769 }
770
771 return 0;
772 }
773
774 /* Unset community-list */
775 int
776 community_list_unset (struct community_list_handler *ch,
777 const char *name, const char *str,
778 int direct, int style, int delete_all)
779 {
780 struct community_entry *entry = NULL;
781 struct community_list *list;
782 struct community *com = NULL;
783
784 /* Lookup community list. */
785 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
786 if (list == NULL)
787 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
788
789 /* Delete all of entry belongs to this community-list. */
790 if (delete_all)
791 {
792 community_list_delete (list);
793 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
794 return 0;
795 }
796
797 if (style == COMMUNITY_LIST_STANDARD)
798 {
799 if (str)
800 com = community_str2com (str);
801 }
802
803 if (com)
804 {
805 entry = community_list_entry_lookup (list, com, direct);
806 community_free (com);
807 }
808 else
809 entry = community_list_entry_lookup (list, str, direct);
810
811 if (!entry)
812 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
813
814 community_list_entry_delete (list, entry, style);
815 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
816
817 return 0;
818 }
819
820 /* Set extcommunity-list. */
821 int
822 extcommunity_list_set (struct community_list_handler *ch,
823 const char *name, const char *str,
824 int direct, int style)
825 {
826 struct community_entry *entry = NULL;
827 struct community_list *list;
828 struct ecommunity *ecom = NULL;
829 regex_t *regex = NULL;
830
831 entry = NULL;
832
833 /* Get community list. */
834 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
835
836 /* When community-list already has entry, new entry should have same
837 style. If you want to have mixed style community-list, you can
838 comment out this check. */
839 if (!community_list_empty_p (list))
840 {
841 struct community_entry *first;
842
843 first = list->head;
844
845 if (style != first->style)
846 {
847 return (first->style == EXTCOMMUNITY_LIST_STANDARD
848 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
849 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
850 }
851 }
852
853 if (str)
854 {
855 if (style == EXTCOMMUNITY_LIST_STANDARD)
856 ecom = ecommunity_str2com (str, 0, 1);
857 else
858 regex = bgp_regcomp (str);
859
860 if (! ecom && ! regex)
861 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
862 }
863
864 if (ecom)
865 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
866
867 entry = community_entry_new ();
868 entry->direct = direct;
869 entry->style = style;
870 entry->any = (str ? 0 : 1);
871 if (ecom)
872 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
873 else if (regex)
874 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
875 else
876 entry->config = NULL;
877 entry->u.ecom = ecom;
878 entry->reg = regex;
879
880 /* Do not put duplicated community entry. */
881 if (community_list_dup_check (list, entry))
882 community_entry_free (entry);
883 else
884 {
885 community_list_entry_add (list, entry);
886 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
887 }
888
889 return 0;
890 }
891
892 /* Unset extcommunity-list. When str is NULL, delete all of
893 extcommunity-list entry belongs to the specified name. */
894 int
895 extcommunity_list_unset (struct community_list_handler *ch,
896 const char *name, const char *str,
897 int direct, int style, int delete_all)
898 {
899 struct community_entry *entry = NULL;
900 struct community_list *list;
901 struct ecommunity *ecom = NULL;
902
903 /* Lookup extcommunity list. */
904 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
905 if (list == NULL)
906 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
907
908 /* Delete all of entry belongs to this extcommunity-list. */
909 if (delete_all)
910 {
911 community_list_delete (list);
912 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
913 return 0;
914 }
915
916 if (style == EXTCOMMUNITY_LIST_STANDARD)
917 {
918 if (str)
919 ecom = ecommunity_str2com (str, 0, 1);
920 }
921
922 if (ecom)
923 {
924 entry = community_list_entry_lookup (list, ecom, direct);
925 ecommunity_free (&ecom);
926 }
927 else
928 entry = community_list_entry_lookup (list, str, direct);
929
930 if (!entry)
931 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
932
933 community_list_entry_delete (list, entry, style);
934 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
935
936 return 0;
937 }
938
939 /* Initializa community-list. Return community-list handler. */
940 struct community_list_handler *
941 community_list_init (void)
942 {
943 struct community_list_handler *ch;
944 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
945 sizeof (struct community_list_handler));
946 return ch;
947 }
948
949 /* Terminate community-list. */
950 void
951 community_list_terminate (struct community_list_handler *ch)
952 {
953 struct community_list_master *cm;
954 struct community_list *list;
955
956 cm = &ch->community_list;
957 while ((list = cm->num.head) != NULL)
958 community_list_delete (list);
959 while ((list = cm->str.head) != NULL)
960 community_list_delete (list);
961
962 cm = &ch->extcommunity_list;
963 while ((list = cm->num.head) != NULL)
964 community_list_delete (list);
965 while ((list = cm->str.head) != NULL)
966 community_list_delete (list);
967
968 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
969 }