]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_clist.c
Merge pull request #73 from opensourcerouting/ldpd-json
[mirror_frr.git] / bgpd / bgp_clist.c
CommitLineData
718e3744 1/* BGP community-list and extcommunity-list.
2 Copyright (C) 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "command.h"
24#include "prefix.h"
25#include "memory.h"
3f9c7369 26#include "queue.h"
039f3a34 27#include "filter.h"
718e3744 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"
6b0655a2 35
718e3744 36/* Lookup master structure for community-list or
37 extcommunity-list. */
38struct community_list_master *
fee6e4e4 39community_list_master_lookup (struct community_list_handler *ch, int master)
718e3744 40{
41 if (ch)
fee6e4e4 42 switch (master)
718e3744 43 {
fee6e4e4 44 case COMMUNITY_LIST_MASTER:
45 return &ch->community_list;
fee6e4e4 46 case EXTCOMMUNITY_LIST_MASTER:
47 return &ch->extcommunity_list;
718e3744 48 }
49 return NULL;
50}
51
52/* Allocate a new community list entry. */
94f2b392 53static struct community_entry *
66e5cd87 54community_entry_new (void)
718e3744 55{
393deb9b 56 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
718e3744 57}
58
59/* Free community list entry. */
94f2b392 60static void
718e3744 61community_entry_free (struct community_entry *entry)
62{
63 switch (entry->style)
64 {
65 case COMMUNITY_LIST_STANDARD:
66 if (entry->u.com)
8708b74f 67 community_free (entry->u.com);
718e3744 68 break;
69 case EXTCOMMUNITY_LIST_STANDARD:
70 /* In case of standard extcommunity-list, configuration string
8708b74f 71 is made by ecommunity_ecom2str(). */
718e3744 72 if (entry->config)
8708b74f 73 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
718e3744 74 if (entry->u.ecom)
f6f434b2 75 ecommunity_free (&entry->u.ecom);
718e3744 76 break;
77 case COMMUNITY_LIST_EXPANDED:
78 case EXTCOMMUNITY_LIST_EXPANDED:
79 if (entry->config)
8708b74f 80 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
718e3744 81 if (entry->reg)
8708b74f 82 bgp_regex_free (entry->reg);
718e3744 83 default:
84 break;
85 }
86 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
87}
88
89/* Allocate a new community-list. */
94f2b392 90static struct community_list *
66e5cd87 91community_list_new (void)
718e3744 92{
393deb9b 93 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
718e3744 94}
95
96/* Free community-list. */
94f2b392 97static void
718e3744 98community_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
94f2b392 105static struct community_list *
718e3744 106community_list_insert (struct community_list_handler *ch,
fee6e4e4 107 const char *name, int master)
718e3744 108{
fd79ac91 109 size_t i;
718e3744 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. */
fee6e4e4 117 cm = community_list_master_lookup (ch, master);
8708b74f 118 if (!cm)
718e3744 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]))
8708b74f 130 number = (number * 10) + (name[i] - '0');
718e3744 131 else
8708b74f 132 break;
718e3744 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)
8708b74f 144 if (atol (point->name) >= number)
145 break;
718e3744 146 }
147 else
148 {
149 new->sort = COMMUNITY_LIST_STRING;
150
151 /* Set access_list to string list. */
152 list = &cm->str;
8708b74f 153
718e3744 154 /* Set point to insertion point. */
155 for (point = list->head; point; point = point->next)
8708b74f 156 if (strcmp (point->name, name) >= 0)
157 break;
718e3744 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
199struct community_list *
200community_list_lookup (struct community_list_handler *ch,
fee6e4e4 201 const char *name, int master)
718e3744 202{
203 struct community_list *list;
204 struct community_list_master *cm;
205
8708b74f 206 if (!name)
718e3744 207 return NULL;
208
fee6e4e4 209 cm = community_list_master_lookup (ch, master);
8708b74f 210 if (!cm)
718e3744 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
94f2b392 223static struct community_list *
fee6e4e4 224community_list_get (struct community_list_handler *ch,
225 const char *name, int master)
718e3744 226{
227 struct community_list *list;
228
fee6e4e4 229 list = community_list_lookup (ch, name, master);
8708b74f 230 if (!list)
fee6e4e4 231 list = community_list_insert (ch, name, master);
718e3744 232 return list;
233}
234
94f2b392 235static void
718e3744 236community_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
94f2b392 262static int
718e3744 263community_list_empty_p (struct community_list *list)
264{
265 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
266}
6b0655a2 267
718e3744 268/* Add community-list entry to the list. */
269static void
8708b74f 270community_list_entry_add (struct community_list *list,
271 struct community_entry *entry)
718e3744 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. */
284static void
285community_list_entry_delete (struct community_list *list,
8708b74f 286 struct community_entry *entry, int style)
718e3744 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. */
305static struct community_entry *
fd79ac91 306community_list_entry_lookup (struct community_list *list, const void *arg,
8708b74f 307 int direct)
718e3744 308{
309 struct community_entry *entry;
310
311 for (entry = list->head; entry; entry = entry->next)
312 {
313 switch (entry->style)
8708b74f 314 {
315 case COMMUNITY_LIST_STANDARD:
813d4307 316 if (entry->direct == direct && community_cmp (entry->u.com, arg))
8708b74f 317 return entry;
318 break;
319 case EXTCOMMUNITY_LIST_STANDARD:
813d4307 320 if (entry->direct == direct && ecommunity_cmp (entry->u.ecom, arg))
8708b74f 321 return entry;
322 break;
323 case COMMUNITY_LIST_EXPANDED:
324 case EXTCOMMUNITY_LIST_EXPANDED:
813d4307 325 if (entry->direct == direct && strcmp (entry->config, arg) == 0)
8708b74f 326 return entry;
327 break;
328 default:
329 break;
330 }
718e3744 331 }
332 return NULL;
333}
6b0655a2 334
5cbea288
DS
335static char *
336community_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. */
403static int
404community_regexp_include (regex_t * reg, struct community *com, int i)
405{
b84ee83b
CF
406 char *str;
407 int rv;
5cbea288
DS
408
409 /* When there is no communities attribute it is treated as empty
410 * string. */
411 if (com == NULL || com->size == 0)
b84ee83b 412 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
5cbea288
DS
413 else
414 str = community_str_get (com, i);
415
416 /* Regular expression match. */
b84ee83b
CF
417 rv = regexec (reg, str, 0, NULL, 0);
418
419 XFREE(MTYPE_COMMUNITY_STR, str);
420
421 if (rv == 0)
5cbea288
DS
422 return 1;
423
424 /* No match. */
425 return 0;
426}
427
718e3744 428/* Internal function to perform regular expression match for community
429 attribute. */
430static int
8708b74f 431community_regexp_match (struct community *com, regex_t * reg)
718e3744 432{
fd79ac91 433 const char *str;
718e3744 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
8708b74f 450static int
451ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
452{
fd79ac91 453 const char *str;
8708b74f 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
ffd0c037 470#if 0
718e3744 471/* Delete community attribute using regular expression match. Return
472 modified communites attribute. */
473static struct community *
8708b74f 474community_regexp_delete (struct community *com, regex_t * reg)
718e3744 475{
476 int i;
477 u_int32_t comval;
478 /* Maximum is "65535:65535" + '\0'. */
479 char c[12];
fd79ac91 480 const char *str;
718e3744 481
8708b74f 482 if (!com)
718e3744 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)
8708b74f 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 }
718e3744 510
511 if (regexec (reg, str, 0, NULL, 0) == 0)
8708b74f 512 community_del_val (com, com_nthval (com, i));
718e3744 513 else
8708b74f 514 i++;
718e3744 515 }
516 return com;
517}
ffd0c037 518#endif
718e3744 519
520/* When given community attribute matches to the community-list return
521 1 else return 0. */
522int
523community_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)
8708b74f 530 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 531
532 if (entry->style == COMMUNITY_LIST_STANDARD)
8708b74f 533 {
534 if (community_include (entry->u.com, COMMUNITY_INTERNET))
535 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 536
8708b74f 537 if (community_match (com, entry->u.com))
538 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
539 }
718e3744 540 else if (entry->style == COMMUNITY_LIST_EXPANDED)
8708b74f 541 {
542 if (community_regexp_match (com, entry->reg))
543 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
544 }
545 }
546 return 0;
547}
548
549int
550ecommunity_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 }
718e3744 569 }
570 return 0;
571}
572
573/* Perform exact matching. In case of expanded community-list, do
574 same thing as community_list_match(). */
575int
8708b74f 576community_list_exact_match (struct community *com,
577 struct community_list *list)
718e3744 578{
579 struct community_entry *entry;
580
581 for (entry = list->head; entry; entry = entry->next)
582 {
583 if (entry->any)
8708b74f 584 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 585
586 if (entry->style == COMMUNITY_LIST_STANDARD)
8708b74f 587 {
588 if (community_include (entry->u.com, COMMUNITY_INTERNET))
589 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 590
8708b74f 591 if (community_cmp (com, entry->u.com))
592 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
593 }
718e3744 594 else if (entry->style == COMMUNITY_LIST_EXPANDED)
8708b74f 595 {
596 if (community_regexp_match (com, entry->reg))
597 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
598 }
718e3744 599 }
600 return 0;
601}
602
8708b74f 603/* Delete all permitted communities in the list from com. */
718e3744 604struct community *
605community_list_match_delete (struct community *com,
8708b74f 606 struct community_list *list)
718e3744 607{
608 struct community_entry *entry;
5cbea288
DS
609 u_int32_t val;
610 u_int32_t com_index_to_delete[com->size];
611 int delete_index = 0;
612 int i;
718e3744 613
5cbea288
DS
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++)
718e3744 619 {
5cbea288
DS
620 val = community_val_get (com, i);
621
622 for (entry = list->head; entry; entry = entry->next)
8708b74f 623 {
5cbea288 624 if (entry->any)
847375b9 625 {
5cbea288
DS
626 if (entry->direct == COMMUNITY_PERMIT)
627 {
628 com_index_to_delete[delete_index] = i;
629 delete_index++;
630 }
631 break;
847375b9 632 }
718e3744 633
5cbea288
DS
634 else if ((entry->style == COMMUNITY_LIST_STANDARD)
635 && (community_include (entry->u.com, COMMUNITY_INTERNET)
636 || community_include (entry->u.com, val) ))
637 {
847375b9 638 if (entry->direct == COMMUNITY_PERMIT)
5cbea288
DS
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);
718e3744 664 }
5cbea288 665
718e3744 666 return com;
667}
668
669/* To avoid duplicated entry in the community-list, this function
670 compares specified entry to existing entry. */
94f2b392 671static int
8708b74f 672community_list_dup_check (struct community_list *list,
673 struct community_entry *new)
718e3744 674{
675 struct community_entry *entry;
8708b74f 676
718e3744 677 for (entry = list->head; entry; entry = entry->next)
678 {
679 if (entry->style != new->style)
8708b74f 680 continue;
718e3744 681
682 if (entry->direct != new->direct)
8708b74f 683 continue;
718e3744 684
685 if (entry->any != new->any)
8708b74f 686 continue;
718e3744 687
688 if (entry->any)
8708b74f 689 return 1;
718e3744 690
691 switch (entry->style)
8708b74f 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 }
718e3744 709 }
710 return 0;
711}
6b0655a2 712
718e3744 713/* Set community-list. */
714int
715community_list_set (struct community_list_handler *ch,
fd79ac91 716 const char *name, const char *str, int direct, int style)
718e3744 717{
fee6e4e4 718 struct community_entry *entry = NULL;
718e3744 719 struct community_list *list;
fee6e4e4 720 struct community *com = NULL;
721 regex_t *regex = NULL;
718e3744 722
723 /* Get community list. */
fee6e4e4 724 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
718e3744 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. */
8708b74f 729 if (!community_list_empty_p (list))
718e3744 730 {
731 struct community_entry *first;
732
733 first = list->head;
734
fee6e4e4 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 }
718e3744 741 }
742
fee6e4e4 743 if (str)
718e3744 744 {
fee6e4e4 745 if (style == COMMUNITY_LIST_STANDARD)
746 com = community_str2com (str);
718e3744 747 else
fee6e4e4 748 regex = bgp_regcomp (str);
8708b74f 749
fee6e4e4 750 if (! com && ! regex)
751 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
718e3744 752 }
753
fee6e4e4 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
718e3744 762 /* Do not put duplicated community entry. */
763 if (community_list_dup_check (list, entry))
764 community_entry_free (entry);
765 else
518f0eb1
DS
766 {
767 community_list_entry_add (list, entry);
768 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
769 }
718e3744 770
771 return 0;
772}
773
813d4307 774/* Unset community-list */
718e3744 775int
776community_list_unset (struct community_list_handler *ch,
fd79ac91 777 const char *name, const char *str,
813d4307 778 int direct, int style, int delete_all)
718e3744 779{
fee6e4e4 780 struct community_entry *entry = NULL;
718e3744 781 struct community_list *list;
fee6e4e4 782 struct community *com = NULL;
718e3744 783
784 /* Lookup community list. */
fee6e4e4 785 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
718e3744 786 if (list == NULL)
787 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
788
789 /* Delete all of entry belongs to this community-list. */
813d4307 790 if (delete_all)
718e3744 791 {
792 community_list_delete (list);
518f0eb1 793 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 794 return 0;
795 }
796
fee6e4e4 797 if (style == COMMUNITY_LIST_STANDARD)
813d4307
DW
798 {
799 if (str)
800 com = community_str2com (str);
801 }
718e3744 802
fee6e4e4 803 if (com)
813d4307
DW
804 {
805 entry = community_list_entry_lookup (list, com, direct);
806 community_free (com);
807 }
fee6e4e4 808 else
809 entry = community_list_entry_lookup (list, str, direct);
810
8708b74f 811 if (!entry)
718e3744 812 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
813
814 community_list_entry_delete (list, entry, style);
518f0eb1 815 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 816
817 return 0;
818}
819
820/* Set extcommunity-list. */
821int
822extcommunity_list_set (struct community_list_handler *ch,
fd79ac91 823 const char *name, const char *str,
824 int direct, int style)
718e3744 825{
fee6e4e4 826 struct community_entry *entry = NULL;
718e3744 827 struct community_list *list;
fee6e4e4 828 struct ecommunity *ecom = NULL;
829 regex_t *regex = NULL;
718e3744 830
831 entry = NULL;
832
833 /* Get community list. */
fee6e4e4 834 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 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. */
8708b74f 839 if (!community_list_empty_p (list))
718e3744 840 {
841 struct community_entry *first;
842
843 first = list->head;
844
fee6e4e4 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 }
718e3744 851 }
852
fee6e4e4 853 if (str)
718e3744 854 {
fee6e4e4 855 if (style == EXTCOMMUNITY_LIST_STANDARD)
856 ecom = ecommunity_str2com (str, 0, 1);
718e3744 857 else
fee6e4e4 858 regex = bgp_regcomp (str);
8708b74f 859
fee6e4e4 860 if (! ecom && ! regex)
861 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
718e3744 862 }
863
fee6e4e4 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
718e3744 880 /* Do not put duplicated community entry. */
881 if (community_list_dup_check (list, entry))
882 community_entry_free (entry);
883 else
518f0eb1
DS
884 {
885 community_list_entry_add (list, entry);
886 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
887 }
718e3744 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. */
894int
895extcommunity_list_unset (struct community_list_handler *ch,
fd79ac91 896 const char *name, const char *str,
813d4307 897 int direct, int style, int delete_all)
718e3744 898{
fee6e4e4 899 struct community_entry *entry = NULL;
718e3744 900 struct community_list *list;
901 struct ecommunity *ecom = NULL;
718e3744 902
903 /* Lookup extcommunity list. */
fee6e4e4 904 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 905 if (list == NULL)
906 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
907
908 /* Delete all of entry belongs to this extcommunity-list. */
813d4307 909 if (delete_all)
718e3744 910 {
911 community_list_delete (list);
518f0eb1 912 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 913 return 0;
914 }
915
fee6e4e4 916 if (style == EXTCOMMUNITY_LIST_STANDARD)
813d4307
DW
917 {
918 if (str)
919 ecom = ecommunity_str2com (str, 0, 1);
920 }
718e3744 921
fee6e4e4 922 if (ecom)
813d4307
DW
923 {
924 entry = community_list_entry_lookup (list, ecom, direct);
925 ecommunity_free (&ecom);
926 }
fee6e4e4 927 else
928 entry = community_list_entry_lookup (list, str, direct);
929
8708b74f 930 if (!entry)
718e3744 931 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
932
933 community_list_entry_delete (list, entry, style);
518f0eb1 934 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 935
936 return 0;
937}
938
939/* Initializa community-list. Return community-list handler. */
940struct community_list_handler *
94f2b392 941community_list_init (void)
718e3744 942{
943 struct community_list_handler *ch;
944 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
8708b74f 945 sizeof (struct community_list_handler));
718e3744 946 return ch;
947}
948
949/* Terminate community-list. */
228da428 950void
718e3744 951community_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}