]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_clist.c
3def97c73d132eac0cdfb99cc0710f590689a8cc
[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_lcommunity.h"
33 #include "bgpd/bgp_aspath.h"
34 #include "bgpd/bgp_regex.h"
35 #include "bgpd/bgp_clist.h"
36
37 /* Lookup master structure for community-list or
38 extcommunity-list. */
39 struct community_list_master *
40 community_list_master_lookup (struct community_list_handler *ch, int master)
41 {
42 if (ch)
43 switch (master)
44 {
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;
51 }
52 return NULL;
53 }
54
55 /* Allocate a new community list entry. */
56 static struct community_entry *
57 community_entry_new (void)
58 {
59 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
60 }
61
62 /* Free community list entry. */
63 static void
64 community_entry_free (struct community_entry *entry)
65 {
66 switch (entry->style)
67 {
68 case COMMUNITY_LIST_STANDARD:
69 if (entry->u.com)
70 community_free (entry->u.com);
71 break;
72 case LARGE_COMMUNITY_LIST_STANDARD:
73 if (entry->u.lcom)
74 lcommunity_free (&entry->u.lcom);
75 break;
76 case EXTCOMMUNITY_LIST_STANDARD:
77 /* In case of standard extcommunity-list, configuration string
78 is made by ecommunity_ecom2str(). */
79 if (entry->config)
80 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
81 if (entry->u.ecom)
82 ecommunity_free (&entry->u.ecom);
83 break;
84 case COMMUNITY_LIST_EXPANDED:
85 case EXTCOMMUNITY_LIST_EXPANDED:
86 case LARGE_COMMUNITY_LIST_EXPANDED:
87 if (entry->config)
88 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
89 if (entry->reg)
90 bgp_regex_free (entry->reg);
91 default:
92 break;
93 }
94 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
95 }
96
97 /* Allocate a new community-list. */
98 static struct community_list *
99 community_list_new (void)
100 {
101 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
102 }
103
104 /* Free community-list. */
105 static void
106 community_list_free (struct community_list *list)
107 {
108 if (list->name)
109 XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name);
110 XFREE (MTYPE_COMMUNITY_LIST, list);
111 }
112
113 static struct community_list *
114 community_list_insert (struct community_list_handler *ch,
115 const char *name, int master)
116 {
117 size_t i;
118 long number;
119 struct community_list *new;
120 struct community_list *point;
121 struct community_list_list *list;
122 struct community_list_master *cm;
123
124 /* Lookup community-list master. */
125 cm = community_list_master_lookup (ch, master);
126 if (!cm)
127 return NULL;
128
129 /* Allocate new community_list and copy given name. */
130 new = community_list_new ();
131 new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name);
132
133 /* If name is made by all digit character. We treat it as
134 number. */
135 for (number = 0, i = 0; i < strlen (name); i++)
136 {
137 if (isdigit ((int) name[i]))
138 number = (number * 10) + (name[i] - '0');
139 else
140 break;
141 }
142
143 /* In case of name is all digit character */
144 if (i == strlen (name))
145 {
146 new->sort = COMMUNITY_LIST_NUMBER;
147
148 /* Set access_list to number list. */
149 list = &cm->num;
150
151 for (point = list->head; point; point = point->next)
152 if (atol (point->name) >= number)
153 break;
154 }
155 else
156 {
157 new->sort = COMMUNITY_LIST_STRING;
158
159 /* Set access_list to string list. */
160 list = &cm->str;
161
162 /* Set point to insertion point. */
163 for (point = list->head; point; point = point->next)
164 if (strcmp (point->name, name) >= 0)
165 break;
166 }
167
168 /* Link to upper list. */
169 new->parent = list;
170
171 /* In case of this is the first element of master. */
172 if (list->head == NULL)
173 {
174 list->head = list->tail = new;
175 return new;
176 }
177
178 /* In case of insertion is made at the tail of access_list. */
179 if (point == NULL)
180 {
181 new->prev = list->tail;
182 list->tail->next = new;
183 list->tail = new;
184 return new;
185 }
186
187 /* In case of insertion is made at the head of access_list. */
188 if (point == list->head)
189 {
190 new->next = list->head;
191 list->head->prev = new;
192 list->head = new;
193 return new;
194 }
195
196 /* Insertion is made at middle of the access_list. */
197 new->next = point;
198 new->prev = point->prev;
199
200 if (point->prev)
201 point->prev->next = new;
202 point->prev = new;
203
204 return new;
205 }
206
207 struct community_list *
208 community_list_lookup (struct community_list_handler *ch,
209 const char *name, int master)
210 {
211 struct community_list *list;
212 struct community_list_master *cm;
213
214 if (!name)
215 return NULL;
216
217 cm = community_list_master_lookup (ch, master);
218 if (!cm)
219 return NULL;
220
221 for (list = cm->num.head; list; list = list->next)
222 if (strcmp (list->name, name) == 0)
223 return list;
224 for (list = cm->str.head; list; list = list->next)
225 if (strcmp (list->name, name) == 0)
226 return list;
227
228 return NULL;
229 }
230
231 static struct community_list *
232 community_list_get (struct community_list_handler *ch,
233 const char *name, int master)
234 {
235 struct community_list *list;
236
237 list = community_list_lookup (ch, name, master);
238 if (!list)
239 list = community_list_insert (ch, name, master);
240 return list;
241 }
242
243 static void
244 community_list_delete (struct community_list *list)
245 {
246 struct community_list_list *clist;
247 struct community_entry *entry, *next;
248
249 for (entry = list->head; entry; entry = next)
250 {
251 next = entry->next;
252 community_entry_free (entry);
253 }
254
255 clist = list->parent;
256
257 if (list->next)
258 list->next->prev = list->prev;
259 else
260 clist->tail = list->prev;
261
262 if (list->prev)
263 list->prev->next = list->next;
264 else
265 clist->head = list->next;
266
267 community_list_free (list);
268 }
269
270 static int
271 community_list_empty_p (struct community_list *list)
272 {
273 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
274 }
275
276 /* Add community-list entry to the list. */
277 static void
278 community_list_entry_add (struct community_list *list,
279 struct community_entry *entry)
280 {
281 entry->next = NULL;
282 entry->prev = list->tail;
283
284 if (list->tail)
285 list->tail->next = entry;
286 else
287 list->head = entry;
288 list->tail = entry;
289 }
290
291 /* Delete community-list entry from the list. */
292 static void
293 community_list_entry_delete (struct community_list *list,
294 struct community_entry *entry, int style)
295 {
296 if (entry->next)
297 entry->next->prev = entry->prev;
298 else
299 list->tail = entry->prev;
300
301 if (entry->prev)
302 entry->prev->next = entry->next;
303 else
304 list->head = entry->next;
305
306 community_entry_free (entry);
307
308 if (community_list_empty_p (list))
309 community_list_delete (list);
310 }
311
312 /* Lookup community-list entry from the list. */
313 static struct community_entry *
314 community_list_entry_lookup (struct community_list *list, const void *arg,
315 int direct)
316 {
317 struct community_entry *entry;
318
319 for (entry = list->head; entry; entry = entry->next)
320 {
321 switch (entry->style)
322 {
323 case COMMUNITY_LIST_STANDARD:
324 if (entry->direct == direct && community_cmp (entry->u.com, arg))
325 return entry;
326 break;
327 case EXTCOMMUNITY_LIST_STANDARD:
328 if (entry->direct == direct && ecommunity_cmp (entry->u.ecom, arg))
329 return entry;
330 break;
331 case LARGE_COMMUNITY_LIST_STANDARD:
332 if (entry->direct == direct && lcommunity_cmp (entry->u.lcom, arg))
333 return entry;
334 break;
335 case COMMUNITY_LIST_EXPANDED:
336 case EXTCOMMUNITY_LIST_EXPANDED:
337 case LARGE_COMMUNITY_LIST_EXPANDED:
338 if (entry->direct == direct && strcmp (entry->config, arg) == 0)
339 return entry;
340 break;
341 default:
342 break;
343 }
344 }
345 return NULL;
346 }
347
348 static char *
349 community_str_get (struct community *com, int i)
350 {
351 int len;
352 u_int32_t comval;
353 u_int16_t as;
354 u_int16_t val;
355 char *str;
356 char *pnt;
357
358 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
359 comval = ntohl (comval);
360
361 switch (comval)
362 {
363 case COMMUNITY_INTERNET:
364 len = strlen (" internet");
365 break;
366 case COMMUNITY_NO_EXPORT:
367 len = strlen (" no-export");
368 break;
369 case COMMUNITY_NO_ADVERTISE:
370 len = strlen (" no-advertise");
371 break;
372 case COMMUNITY_LOCAL_AS:
373 len = strlen (" local-AS");
374 break;
375 default:
376 len = strlen (" 65536:65535");
377 break;
378 }
379
380 /* Allocate memory. */
381 str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
382
383 switch (comval)
384 {
385 case COMMUNITY_INTERNET:
386 strcpy (pnt, "internet");
387 pnt += strlen ("internet");
388 break;
389 case COMMUNITY_NO_EXPORT:
390 strcpy (pnt, "no-export");
391 pnt += strlen ("no-export");
392 break;
393 case COMMUNITY_NO_ADVERTISE:
394 strcpy (pnt, "no-advertise");
395 pnt += strlen ("no-advertise");
396 break;
397 case COMMUNITY_LOCAL_AS:
398 strcpy (pnt, "local-AS");
399 pnt += strlen ("local-AS");
400 break;
401 default:
402 as = (comval >> 16) & 0xFFFF;
403 val = comval & 0xFFFF;
404 sprintf (pnt, "%u:%d", as, val);
405 pnt += strlen (pnt);
406 break;
407 }
408
409 *pnt = '\0';
410
411 return str;
412 }
413
414 /* Internal function to perform regular expression match for
415 * a single community. */
416 static int
417 community_regexp_include (regex_t * reg, struct community *com, int i)
418 {
419 char *str;
420 int rv;
421
422 /* When there is no communities attribute it is treated as empty string. */
423 if (com == NULL || com->size == 0)
424 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
425 else
426 str = community_str_get (com, i);
427
428 /* Regular expression match. */
429 rv = regexec (reg, str, 0, NULL, 0);
430
431 XFREE(MTYPE_COMMUNITY_STR, str);
432
433 if (rv == 0)
434 return 1;
435
436 /* No match. */
437 return 0;
438 }
439
440 /* Internal function to perform regular expression match for community
441 attribute. */
442 static int
443 community_regexp_match (struct community *com, regex_t * reg)
444 {
445 const char *str;
446
447 /* When there is no communities attribute it is treated as empty
448 string. */
449 if (com == NULL || com->size == 0)
450 str = "";
451 else
452 str = community_str (com);
453
454 /* Regular expression match. */
455 if (regexec (reg, str, 0, NULL, 0) == 0)
456 return 1;
457
458 /* No match. */
459 return 0;
460 }
461
462 static char *
463 lcommunity_str_get (struct lcommunity *lcom, int i)
464 {
465 struct lcommunity_val lcomval;
466 u_int32_t globaladmin;
467 u_int32_t localdata1;
468 u_int32_t localdata2;
469 char *str;
470 u_char *ptr;
471 char *pnt;
472
473 ptr = lcom->val;
474 ptr += (i * LCOMMUNITY_SIZE);
475
476 memcpy (&lcomval, ptr, LCOMMUNITY_SIZE);
477
478 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
479 str = pnt = XMALLOC (MTYPE_LCOMMUNITY_STR, 48);
480
481 ptr = (u_char *)lcomval.val;
482 globaladmin = (*ptr++ << 24);
483 globaladmin |= (*ptr++ << 16);
484 globaladmin |= (*ptr++ << 8);
485 globaladmin |= (*ptr++);
486
487 localdata1 = (*ptr++ << 24);
488 localdata1 |= (*ptr++ << 16);
489 localdata1 |= (*ptr++ << 8);
490 localdata1 |= (*ptr++);
491
492 localdata2 = (*ptr++ << 24);
493 localdata2 |= (*ptr++ << 16);
494 localdata2 |= (*ptr++ << 8);
495 localdata2 |= (*ptr++);
496
497 sprintf (pnt, "%u:%u:%u", globaladmin, localdata1, localdata2);
498 pnt += strlen (pnt);
499 *pnt = '\0';
500
501 return str;
502 }
503
504 /* Internal function to perform regular expression match for
505 * a single community. */
506 static int
507 lcommunity_regexp_include (regex_t * reg, struct lcommunity *lcom, int i)
508 {
509 char *str;
510
511 /* When there is no communities attribute it is treated as empty string. */
512 if (lcom == NULL || lcom->size == 0)
513 str = XSTRDUP (MTYPE_LCOMMUNITY_STR, "");
514 else
515 str = lcommunity_str_get (lcom, i);
516
517 /* Regular expression match. */
518 if (regexec (reg, str, 0, NULL, 0) == 0)
519 {
520 XFREE (MTYPE_LCOMMUNITY_STR, str);
521 return 1;
522 }
523
524 XFREE (MTYPE_LCOMMUNITY_STR, str);
525 /* No match. */
526 return 0;
527 }
528
529 static int
530 lcommunity_regexp_match (struct lcommunity *com, regex_t * reg)
531 {
532 const char *str;
533
534 /* When there is no communities attribute it is treated as empty
535 string. */
536 if (com == NULL || com->size == 0)
537 str = "";
538 else
539 str = lcommunity_str (com);
540
541 /* Regular expression match. */
542 if (regexec (reg, str, 0, NULL, 0) == 0)
543 return 1;
544
545 /* No match. */
546 return 0;
547 }
548
549
550 static int
551 ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
552 {
553 const char *str;
554
555 /* When there is no communities attribute it is treated as empty
556 string. */
557 if (ecom == NULL || ecom->size == 0)
558 str = "";
559 else
560 str = ecommunity_str (ecom);
561
562 /* Regular expression match. */
563 if (regexec (reg, str, 0, NULL, 0) == 0)
564 return 1;
565
566 /* No match. */
567 return 0;
568 }
569
570 #if 0
571 /* Delete community attribute using regular expression match. Return
572 modified communites attribute. */
573 static struct community *
574 community_regexp_delete (struct community *com, regex_t * reg)
575 {
576 int i;
577 u_int32_t comval;
578 /* Maximum is "65535:65535" + '\0'. */
579 char c[12];
580 const char *str;
581
582 if (!com)
583 return NULL;
584
585 i = 0;
586 while (i < com->size)
587 {
588 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
589 comval = ntohl (comval);
590
591 switch (comval)
592 {
593 case COMMUNITY_INTERNET:
594 str = "internet";
595 break;
596 case COMMUNITY_NO_EXPORT:
597 str = "no-export";
598 break;
599 case COMMUNITY_NO_ADVERTISE:
600 str = "no-advertise";
601 break;
602 case COMMUNITY_LOCAL_AS:
603 str = "local-AS";
604 break;
605 default:
606 sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF, comval & 0xFFFF);
607 str = c;
608 break;
609 }
610
611 if (regexec (reg, str, 0, NULL, 0) == 0)
612 community_del_val (com, com_nthval (com, i));
613 else
614 i++;
615 }
616 return com;
617 }
618 #endif
619
620 /* When given community attribute matches to the community-list return
621 1 else return 0. */
622 int
623 community_list_match (struct community *com, struct community_list *list)
624 {
625 struct community_entry *entry;
626
627 for (entry = list->head; entry; entry = entry->next)
628 {
629 if (entry->any)
630 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
631
632 if (entry->style == COMMUNITY_LIST_STANDARD)
633 {
634 if (community_include (entry->u.com, COMMUNITY_INTERNET))
635 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
636
637 if (community_match (com, entry->u.com))
638 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
639 }
640 else if (entry->style == COMMUNITY_LIST_EXPANDED)
641 {
642 if (community_regexp_match (com, entry->reg))
643 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
644 }
645 }
646 return 0;
647 }
648
649 int
650 lcommunity_list_match (struct lcommunity *lcom, struct community_list *list)
651 {
652 struct community_entry *entry;
653
654 for (entry = list->head; entry; entry = entry->next)
655 {
656 if (entry->any)
657 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
658
659 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD)
660 {
661 if (lcommunity_match (lcom, entry->u.lcom))
662 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
663 }
664 else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED)
665 {
666 if (lcommunity_regexp_match (lcom, entry->reg))
667 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
668 }
669 }
670 return 0;
671 }
672
673 int
674 ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
675 {
676 struct community_entry *entry;
677
678 for (entry = list->head; entry; entry = entry->next)
679 {
680 if (entry->any)
681 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
682
683 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
684 {
685 if (ecommunity_match (ecom, entry->u.ecom))
686 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
687 }
688 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
689 {
690 if (ecommunity_regexp_match (ecom, entry->reg))
691 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
692 }
693 }
694 return 0;
695 }
696
697 /* Perform exact matching. In case of expanded community-list, do
698 same thing as community_list_match(). */
699 int
700 community_list_exact_match (struct community *com,
701 struct community_list *list)
702 {
703 struct community_entry *entry;
704
705 for (entry = list->head; entry; entry = entry->next)
706 {
707 if (entry->any)
708 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
709
710 if (entry->style == COMMUNITY_LIST_STANDARD)
711 {
712 if (community_include (entry->u.com, COMMUNITY_INTERNET))
713 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
714
715 if (community_cmp (com, entry->u.com))
716 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
717 }
718 else if (entry->style == COMMUNITY_LIST_EXPANDED)
719 {
720 if (community_regexp_match (com, entry->reg))
721 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
722 }
723 }
724 return 0;
725 }
726
727 /* Delete all permitted communities in the list from com. */
728 struct community *
729 community_list_match_delete (struct community *com,
730 struct community_list *list)
731 {
732 struct community_entry *entry;
733 u_int32_t val;
734 u_int32_t com_index_to_delete[com->size];
735 int delete_index = 0;
736 int i;
737
738 /* Loop over each community value and evaluate each against the
739 * community-list. If we need to delete a community value add its index to
740 * com_index_to_delete.
741 */
742 for (i = 0; i < com->size; i++)
743 {
744 val = community_val_get (com, i);
745
746 for (entry = list->head; entry; entry = entry->next)
747 {
748 if (entry->any)
749 {
750 if (entry->direct == COMMUNITY_PERMIT)
751 {
752 com_index_to_delete[delete_index] = i;
753 delete_index++;
754 }
755 break;
756 }
757
758 else if ((entry->style == COMMUNITY_LIST_STANDARD)
759 && (community_include (entry->u.com, COMMUNITY_INTERNET)
760 || community_include (entry->u.com, val) ))
761 {
762 if (entry->direct == COMMUNITY_PERMIT)
763 {
764 com_index_to_delete[delete_index] = i;
765 delete_index++;
766 }
767 break;
768 }
769
770 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
771 && community_regexp_include (entry->reg, com, i))
772 {
773 if (entry->direct == COMMUNITY_PERMIT)
774 {
775 com_index_to_delete[delete_index] = i;
776 delete_index++;
777 }
778 break;
779 }
780 }
781 }
782
783 /* Delete all of the communities we flagged for deletion */
784 for (i = delete_index-1; i >= 0; i--)
785 {
786 val = community_val_get (com, com_index_to_delete[i]);
787 community_del_val (com, &val);
788 }
789
790 return com;
791 }
792
793 /* To avoid duplicated entry in the community-list, this function
794 compares specified entry to existing entry. */
795 static int
796 community_list_dup_check (struct community_list *list,
797 struct community_entry *new)
798 {
799 struct community_entry *entry;
800
801 for (entry = list->head; entry; entry = entry->next)
802 {
803 if (entry->style != new->style)
804 continue;
805
806 if (entry->direct != new->direct)
807 continue;
808
809 if (entry->any != new->any)
810 continue;
811
812 if (entry->any)
813 return 1;
814
815 switch (entry->style)
816 {
817 case COMMUNITY_LIST_STANDARD:
818 if (community_cmp (entry->u.com, new->u.com))
819 return 1;
820 break;
821 case LARGE_COMMUNITY_LIST_STANDARD:
822 if (lcommunity_cmp (entry->u.lcom, new->u.lcom))
823 return 1;
824 break;
825 case EXTCOMMUNITY_LIST_STANDARD:
826 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
827 return 1;
828 break;
829 case COMMUNITY_LIST_EXPANDED:
830 case EXTCOMMUNITY_LIST_EXPANDED:
831 case LARGE_COMMUNITY_LIST_EXPANDED:
832 if (strcmp (entry->config, new->config) == 0)
833 return 1;
834 break;
835 default:
836 break;
837 }
838 }
839 return 0;
840 }
841
842 /* Set community-list. */
843 int
844 community_list_set (struct community_list_handler *ch,
845 const char *name, const char *str, int direct, int style)
846 {
847 struct community_entry *entry = NULL;
848 struct community_list *list;
849 struct community *com = NULL;
850 regex_t *regex = NULL;
851
852 /* Get community list. */
853 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
854
855 /* When community-list already has entry, new entry should have same
856 style. If you want to have mixed style community-list, you can
857 comment out this check. */
858 if (!community_list_empty_p (list))
859 {
860 struct community_entry *first;
861
862 first = list->head;
863
864 if (style != first->style)
865 {
866 return (first->style == COMMUNITY_LIST_STANDARD
867 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
868 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
869 }
870 }
871
872 if (str)
873 {
874 if (style == COMMUNITY_LIST_STANDARD)
875 com = community_str2com (str);
876 else
877 regex = bgp_regcomp (str);
878
879 if (! com && ! regex)
880 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
881 }
882
883 entry = community_entry_new ();
884 entry->direct = direct;
885 entry->style = style;
886 entry->any = (str ? 0 : 1);
887 entry->u.com = com;
888 entry->reg = regex;
889 entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
890
891 /* Do not put duplicated community entry. */
892 if (community_list_dup_check (list, entry))
893 community_entry_free (entry);
894 else
895 {
896 community_list_entry_add (list, entry);
897 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
898 }
899
900 return 0;
901 }
902
903 /* Unset community-list */
904 int
905 community_list_unset (struct community_list_handler *ch,
906 const char *name, const char *str,
907 int direct, int style, int delete_all)
908 {
909 struct community_entry *entry = NULL;
910 struct community_list *list;
911 struct community *com = NULL;
912
913 /* Lookup community list. */
914 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
915 if (list == NULL)
916 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
917
918 /* Delete all of entry belongs to this community-list. */
919 if (delete_all)
920 {
921 community_list_delete (list);
922 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
923 return 0;
924 }
925
926 if (style == COMMUNITY_LIST_STANDARD)
927 {
928 if (str)
929 com = community_str2com (str);
930 }
931
932 if (com)
933 {
934 entry = community_list_entry_lookup (list, com, direct);
935 community_free (com);
936 }
937 else
938 entry = community_list_entry_lookup (list, str, direct);
939
940 if (!entry)
941 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
942
943 community_list_entry_delete (list, entry, style);
944 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
945
946 return 0;
947 }
948
949 /* Delete all permitted large communities in the list from com. */
950 struct lcommunity *
951 lcommunity_list_match_delete (struct lcommunity *lcom,
952 struct community_list *list)
953 {
954 struct community_entry *entry;
955 u_int32_t com_index_to_delete[lcom->size];
956 u_char *ptr;
957 int delete_index = 0;
958 int i;
959
960 /* Loop over each lcommunity value and evaluate each against the
961 * community-list. If we need to delete a community value add its index to
962 * com_index_to_delete.
963 */
964 ptr = lcom->val;
965 for (i = 0; i < lcom->size; i++)
966 {
967 ptr += (i * LCOMMUNITY_SIZE);
968 for (entry = list->head; entry; entry = entry->next)
969 {
970 if (entry->any)
971 {
972 if (entry->direct == COMMUNITY_PERMIT)
973 {
974 com_index_to_delete[delete_index] = i;
975 delete_index++;
976 }
977 break;
978 }
979
980 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
981 && lcommunity_include (entry->u.lcom, ptr) )
982 {
983 if (entry->direct == COMMUNITY_PERMIT)
984 {
985 com_index_to_delete[delete_index] = i;
986 delete_index++;
987 }
988 break;
989 }
990
991 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
992 && lcommunity_regexp_include (entry->reg, lcom, i))
993 {
994 if (entry->direct == COMMUNITY_PERMIT)
995 {
996 com_index_to_delete[delete_index] = i;
997 delete_index++;
998 }
999 break;
1000 }
1001 }
1002 }
1003
1004 /* Delete all of the communities we flagged for deletion */
1005 ptr = lcom->val;
1006 for (i = delete_index-1; i >= 0; i--)
1007 {
1008 ptr += (com_index_to_delete[i] * LCOMMUNITY_SIZE);
1009 lcommunity_del_val (lcom, ptr);
1010 }
1011
1012 return lcom;
1013 }
1014
1015 /* Set lcommunity-list. */
1016 int
1017 lcommunity_list_set (struct community_list_handler *ch,
1018 const char *name, const char *str, int direct, int style)
1019 {
1020 struct community_entry *entry = NULL;
1021 struct community_list *list;
1022 struct lcommunity *lcom = NULL;
1023 regex_t *regex = NULL;
1024
1025 /* Get community list. */
1026 list = community_list_get (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1027
1028 /* When community-list already has entry, new entry should have same
1029 style. If you want to have mixed style community-list, you can
1030 comment out this check. */
1031 if (!community_list_empty_p (list))
1032 {
1033 struct community_entry *first;
1034
1035 first = list->head;
1036
1037 if (style != first->style)
1038 {
1039 return (first->style == COMMUNITY_LIST_STANDARD
1040 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1041 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1042 }
1043 }
1044
1045 if (str)
1046 {
1047 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1048 lcom = lcommunity_str2com (str);
1049 else
1050 regex = bgp_regcomp (str);
1051
1052 if (! lcom && ! regex)
1053 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1054 }
1055
1056 entry = community_entry_new ();
1057 entry->direct = direct;
1058 entry->style = style;
1059 entry->any = (str ? 0 : 1);
1060 entry->u.lcom = lcom;
1061 entry->reg = regex;
1062 if (lcom)
1063 entry->config = lcommunity_lcom2str (lcom, LCOMMUNITY_FORMAT_COMMUNITY_LIST);
1064 else if (regex)
1065 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1066 else
1067 entry->config = NULL;
1068
1069 /* Do not put duplicated community entry. */
1070 if (community_list_dup_check (list, entry))
1071 community_entry_free (entry);
1072 else
1073 community_list_entry_add (list, entry);
1074
1075 return 0;
1076 }
1077
1078 /* Unset community-list. When str is NULL, delete all of
1079 community-list entry belongs to the specified name. */
1080 int
1081 lcommunity_list_unset (struct community_list_handler *ch,
1082 const char *name, const char *str,
1083 int direct, int style)
1084 {
1085 struct community_entry *entry = NULL;
1086 struct community_list *list;
1087 struct lcommunity *lcom = NULL;
1088 regex_t *regex = NULL;
1089
1090 /* Lookup community list. */
1091 list = community_list_lookup (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1092 if (list == NULL)
1093 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1094
1095 /* Delete all of entry belongs to this community-list. */
1096 if (!str)
1097 {
1098 community_list_delete (list);
1099 return 0;
1100 }
1101
1102 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1103 lcom = lcommunity_str2com (str);
1104 else
1105 regex = bgp_regcomp (str);
1106
1107 if (! lcom && ! regex)
1108 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1109
1110 if (lcom)
1111 entry = community_list_entry_lookup (list, lcom, direct);
1112 else
1113 entry = community_list_entry_lookup (list, str, direct);
1114
1115 if (lcom)
1116 lcommunity_free (&lcom);
1117 if (regex)
1118 bgp_regex_free (regex);
1119
1120 if (!entry)
1121 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1122
1123 community_list_entry_delete (list, entry, style);
1124
1125 return 0;
1126 }
1127
1128 /* Set extcommunity-list. */
1129 int
1130 extcommunity_list_set (struct community_list_handler *ch,
1131 const char *name, const char *str,
1132 int direct, int style)
1133 {
1134 struct community_entry *entry = NULL;
1135 struct community_list *list;
1136 struct ecommunity *ecom = NULL;
1137 regex_t *regex = NULL;
1138
1139 entry = NULL;
1140
1141 /* Get community list. */
1142 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
1143
1144 /* When community-list already has entry, new entry should have same
1145 style. If you want to have mixed style community-list, you can
1146 comment out this check. */
1147 if (!community_list_empty_p (list))
1148 {
1149 struct community_entry *first;
1150
1151 first = list->head;
1152
1153 if (style != first->style)
1154 {
1155 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1156 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1157 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1158 }
1159 }
1160
1161 if (str)
1162 {
1163 if (style == EXTCOMMUNITY_LIST_STANDARD)
1164 ecom = ecommunity_str2com (str, 0, 1);
1165 else
1166 regex = bgp_regcomp (str);
1167
1168 if (! ecom && ! regex)
1169 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1170 }
1171
1172 if (ecom)
1173 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
1174
1175 entry = community_entry_new ();
1176 entry->direct = direct;
1177 entry->style = style;
1178 entry->any = (str ? 0 : 1);
1179 if (ecom)
1180 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0);
1181 else if (regex)
1182 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1183 else
1184 entry->config = NULL;
1185 entry->u.ecom = ecom;
1186 entry->reg = regex;
1187
1188 /* Do not put duplicated community entry. */
1189 if (community_list_dup_check (list, entry))
1190 community_entry_free (entry);
1191 else
1192 {
1193 community_list_entry_add (list, entry);
1194 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1195 }
1196
1197 return 0;
1198 }
1199
1200 /* Unset extcommunity-list. When str is NULL, delete all of
1201 extcommunity-list entry belongs to the specified name. */
1202 int
1203 extcommunity_list_unset (struct community_list_handler *ch,
1204 const char *name, const char *str,
1205 int direct, int style, int delete_all)
1206 {
1207 struct community_entry *entry = NULL;
1208 struct community_list *list;
1209 struct ecommunity *ecom = NULL;
1210
1211 /* Lookup extcommunity list. */
1212 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
1213 if (list == NULL)
1214 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1215
1216 /* Delete all of entry belongs to this extcommunity-list. */
1217 if (delete_all)
1218 {
1219 community_list_delete (list);
1220 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1221 return 0;
1222 }
1223
1224 if (style == EXTCOMMUNITY_LIST_STANDARD)
1225 {
1226 if (str)
1227 ecom = ecommunity_str2com (str, 0, 1);
1228 }
1229
1230 if (ecom)
1231 {
1232 entry = community_list_entry_lookup (list, ecom, direct);
1233 ecommunity_free (&ecom);
1234 }
1235 else
1236 entry = community_list_entry_lookup (list, str, direct);
1237
1238 if (!entry)
1239 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1240
1241 community_list_entry_delete (list, entry, style);
1242 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1243
1244 return 0;
1245 }
1246
1247 /* Initializa community-list. Return community-list handler. */
1248 struct community_list_handler *
1249 community_list_init (void)
1250 {
1251 struct community_list_handler *ch;
1252 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
1253 sizeof (struct community_list_handler));
1254 return ch;
1255 }
1256
1257 /* Terminate community-list. */
1258 void
1259 community_list_terminate (struct community_list_handler *ch)
1260 {
1261 struct community_list_master *cm;
1262 struct community_list *list;
1263
1264 cm = &ch->community_list;
1265 while ((list = cm->num.head) != NULL)
1266 community_list_delete (list);
1267 while ((list = cm->str.head) != NULL)
1268 community_list_delete (list);
1269
1270 cm = &ch->lcommunity_list;
1271 while ((list = cm->num.head) != NULL)
1272 community_list_delete (list);
1273 while ((list = cm->str.head) != NULL)
1274 community_list_delete (list);
1275
1276 cm = &ch->extcommunity_list;
1277 while ((list = cm->num.head) != NULL)
1278 community_list_delete (list);
1279 while ((list = cm->str.head) != NULL)
1280 community_list_delete (list);
1281
1282 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
1283 }