]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_clist.c
Merge remote-tracking branch 'origin/stable/2.0'
[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 const 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 = "";
514 else
515 str = lcommunity_str_get (lcom, i);
516
517 /* Regular expression match. */
518 if (regexec (reg, str, 0, NULL, 0) == 0)
519 return 1;
520
521 /* No match. */
522 return 0;
523 }
524
525 static int
526 lcommunity_regexp_match (struct lcommunity *com, regex_t * reg)
527 {
528 const char *str;
529
530 /* When there is no communities attribute it is treated as empty
531 string. */
532 if (com == NULL || com->size == 0)
533 str = "";
534 else
535 str = lcommunity_str (com);
536
537 /* Regular expression match. */
538 if (regexec (reg, str, 0, NULL, 0) == 0)
539 return 1;
540
541 /* No match. */
542 return 0;
543 }
544
545
546 static int
547 ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
548 {
549 const char *str;
550
551 /* When there is no communities attribute it is treated as empty
552 string. */
553 if (ecom == NULL || ecom->size == 0)
554 str = "";
555 else
556 str = ecommunity_str (ecom);
557
558 /* Regular expression match. */
559 if (regexec (reg, str, 0, NULL, 0) == 0)
560 return 1;
561
562 /* No match. */
563 return 0;
564 }
565
566 #if 0
567 /* Delete community attribute using regular expression match. Return
568 modified communites attribute. */
569 static struct community *
570 community_regexp_delete (struct community *com, regex_t * reg)
571 {
572 int i;
573 u_int32_t comval;
574 /* Maximum is "65535:65535" + '\0'. */
575 char c[12];
576 const char *str;
577
578 if (!com)
579 return NULL;
580
581 i = 0;
582 while (i < com->size)
583 {
584 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
585 comval = ntohl (comval);
586
587 switch (comval)
588 {
589 case COMMUNITY_INTERNET:
590 str = "internet";
591 break;
592 case COMMUNITY_NO_EXPORT:
593 str = "no-export";
594 break;
595 case COMMUNITY_NO_ADVERTISE:
596 str = "no-advertise";
597 break;
598 case COMMUNITY_LOCAL_AS:
599 str = "local-AS";
600 break;
601 default:
602 sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF, comval & 0xFFFF);
603 str = c;
604 break;
605 }
606
607 if (regexec (reg, str, 0, NULL, 0) == 0)
608 community_del_val (com, com_nthval (com, i));
609 else
610 i++;
611 }
612 return com;
613 }
614 #endif
615
616 /* When given community attribute matches to the community-list return
617 1 else return 0. */
618 int
619 community_list_match (struct community *com, struct community_list *list)
620 {
621 struct community_entry *entry;
622
623 for (entry = list->head; entry; entry = entry->next)
624 {
625 if (entry->any)
626 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
627
628 if (entry->style == COMMUNITY_LIST_STANDARD)
629 {
630 if (community_include (entry->u.com, COMMUNITY_INTERNET))
631 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
632
633 if (community_match (com, entry->u.com))
634 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
635 }
636 else if (entry->style == COMMUNITY_LIST_EXPANDED)
637 {
638 if (community_regexp_match (com, entry->reg))
639 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
640 }
641 }
642 return 0;
643 }
644
645 int
646 lcommunity_list_match (struct lcommunity *lcom, struct community_list *list)
647 {
648 struct community_entry *entry;
649
650 for (entry = list->head; entry; entry = entry->next)
651 {
652 if (entry->any)
653 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
654
655 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD)
656 {
657 if (lcommunity_match (lcom, entry->u.lcom))
658 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
659 }
660 else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED)
661 {
662 if (lcommunity_regexp_match (lcom, entry->reg))
663 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
664 }
665 }
666 return 0;
667 }
668
669 int
670 ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
671 {
672 struct community_entry *entry;
673
674 for (entry = list->head; entry; entry = entry->next)
675 {
676 if (entry->any)
677 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
678
679 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
680 {
681 if (ecommunity_match (ecom, entry->u.ecom))
682 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
683 }
684 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
685 {
686 if (ecommunity_regexp_match (ecom, entry->reg))
687 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
688 }
689 }
690 return 0;
691 }
692
693 /* Perform exact matching. In case of expanded community-list, do
694 same thing as community_list_match(). */
695 int
696 community_list_exact_match (struct community *com,
697 struct community_list *list)
698 {
699 struct community_entry *entry;
700
701 for (entry = list->head; entry; entry = entry->next)
702 {
703 if (entry->any)
704 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
705
706 if (entry->style == COMMUNITY_LIST_STANDARD)
707 {
708 if (community_include (entry->u.com, COMMUNITY_INTERNET))
709 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
710
711 if (community_cmp (com, entry->u.com))
712 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
713 }
714 else if (entry->style == COMMUNITY_LIST_EXPANDED)
715 {
716 if (community_regexp_match (com, entry->reg))
717 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718 }
719 }
720 return 0;
721 }
722
723 /* Delete all permitted communities in the list from com. */
724 struct community *
725 community_list_match_delete (struct community *com,
726 struct community_list *list)
727 {
728 struct community_entry *entry;
729 u_int32_t val;
730 u_int32_t com_index_to_delete[com->size];
731 int delete_index = 0;
732 int i;
733
734 /* Loop over each community value and evaluate each against the
735 * community-list. If we need to delete a community value add its index to
736 * com_index_to_delete.
737 */
738 for (i = 0; i < com->size; i++)
739 {
740 val = community_val_get (com, i);
741
742 for (entry = list->head; entry; entry = entry->next)
743 {
744 if (entry->any)
745 {
746 if (entry->direct == COMMUNITY_PERMIT)
747 {
748 com_index_to_delete[delete_index] = i;
749 delete_index++;
750 }
751 break;
752 }
753
754 else if ((entry->style == COMMUNITY_LIST_STANDARD)
755 && (community_include (entry->u.com, COMMUNITY_INTERNET)
756 || community_include (entry->u.com, val) ))
757 {
758 if (entry->direct == COMMUNITY_PERMIT)
759 {
760 com_index_to_delete[delete_index] = i;
761 delete_index++;
762 }
763 break;
764 }
765
766 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
767 && community_regexp_include (entry->reg, com, i))
768 {
769 if (entry->direct == COMMUNITY_PERMIT)
770 {
771 com_index_to_delete[delete_index] = i;
772 delete_index++;
773 }
774 break;
775 }
776 }
777 }
778
779 /* Delete all of the communities we flagged for deletion */
780 for (i = delete_index-1; i >= 0; i--)
781 {
782 val = community_val_get (com, com_index_to_delete[i]);
783 community_del_val (com, &val);
784 }
785
786 return com;
787 }
788
789 /* To avoid duplicated entry in the community-list, this function
790 compares specified entry to existing entry. */
791 static int
792 community_list_dup_check (struct community_list *list,
793 struct community_entry *new)
794 {
795 struct community_entry *entry;
796
797 for (entry = list->head; entry; entry = entry->next)
798 {
799 if (entry->style != new->style)
800 continue;
801
802 if (entry->direct != new->direct)
803 continue;
804
805 if (entry->any != new->any)
806 continue;
807
808 if (entry->any)
809 return 1;
810
811 switch (entry->style)
812 {
813 case COMMUNITY_LIST_STANDARD:
814 if (community_cmp (entry->u.com, new->u.com))
815 return 1;
816 break;
817 case LARGE_COMMUNITY_LIST_STANDARD:
818 if (lcommunity_cmp (entry->u.lcom, new->u.lcom))
819 return 1;
820 break;
821 case EXTCOMMUNITY_LIST_STANDARD:
822 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
823 return 1;
824 break;
825 case COMMUNITY_LIST_EXPANDED:
826 case EXTCOMMUNITY_LIST_EXPANDED:
827 case LARGE_COMMUNITY_LIST_EXPANDED:
828 if (strcmp (entry->config, new->config) == 0)
829 return 1;
830 break;
831 default:
832 break;
833 }
834 }
835 return 0;
836 }
837
838 /* Set community-list. */
839 int
840 community_list_set (struct community_list_handler *ch,
841 const char *name, const char *str, int direct, int style)
842 {
843 struct community_entry *entry = NULL;
844 struct community_list *list;
845 struct community *com = NULL;
846 regex_t *regex = NULL;
847
848 /* Get community list. */
849 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
850
851 /* When community-list already has entry, new entry should have same
852 style. If you want to have mixed style community-list, you can
853 comment out this check. */
854 if (!community_list_empty_p (list))
855 {
856 struct community_entry *first;
857
858 first = list->head;
859
860 if (style != first->style)
861 {
862 return (first->style == COMMUNITY_LIST_STANDARD
863 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
864 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
865 }
866 }
867
868 if (str)
869 {
870 if (style == COMMUNITY_LIST_STANDARD)
871 com = community_str2com (str);
872 else
873 regex = bgp_regcomp (str);
874
875 if (! com && ! regex)
876 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
877 }
878
879 entry = community_entry_new ();
880 entry->direct = direct;
881 entry->style = style;
882 entry->any = (str ? 0 : 1);
883 entry->u.com = com;
884 entry->reg = regex;
885 entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
886
887 /* Do not put duplicated community entry. */
888 if (community_list_dup_check (list, entry))
889 community_entry_free (entry);
890 else
891 {
892 community_list_entry_add (list, entry);
893 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
894 }
895
896 return 0;
897 }
898
899 /* Unset community-list */
900 int
901 community_list_unset (struct community_list_handler *ch,
902 const char *name, const char *str,
903 int direct, int style, int delete_all)
904 {
905 struct community_entry *entry = NULL;
906 struct community_list *list;
907 struct community *com = NULL;
908
909 /* Lookup community list. */
910 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
911 if (list == NULL)
912 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
913
914 /* Delete all of entry belongs to this community-list. */
915 if (delete_all)
916 {
917 community_list_delete (list);
918 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
919 return 0;
920 }
921
922 if (style == COMMUNITY_LIST_STANDARD)
923 {
924 if (str)
925 com = community_str2com (str);
926 }
927
928 if (com)
929 {
930 entry = community_list_entry_lookup (list, com, direct);
931 community_free (com);
932 }
933 else
934 entry = community_list_entry_lookup (list, str, direct);
935
936 if (!entry)
937 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
938
939 community_list_entry_delete (list, entry, style);
940 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
941
942 return 0;
943 }
944
945 /* Delete all permitted large communities in the list from com. */
946 struct lcommunity *
947 lcommunity_list_match_delete (struct lcommunity *lcom,
948 struct community_list *list)
949 {
950 struct community_entry *entry;
951 u_int32_t com_index_to_delete[lcom->size];
952 u_char *ptr;
953 int delete_index = 0;
954 int i;
955
956 /* Loop over each lcommunity value and evaluate each against the
957 * community-list. If we need to delete a community value add its index to
958 * com_index_to_delete.
959 */
960 ptr = lcom->val;
961 for (i = 0; i < lcom->size; i++)
962 {
963 ptr += (i * LCOMMUNITY_SIZE);
964 for (entry = list->head; entry; entry = entry->next)
965 {
966 if (entry->any)
967 {
968 if (entry->direct == COMMUNITY_PERMIT)
969 {
970 com_index_to_delete[delete_index] = i;
971 delete_index++;
972 }
973 break;
974 }
975
976 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
977 && lcommunity_include (entry->u.lcom, ptr) )
978 {
979 if (entry->direct == COMMUNITY_PERMIT)
980 {
981 com_index_to_delete[delete_index] = i;
982 delete_index++;
983 }
984 break;
985 }
986
987 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
988 && lcommunity_regexp_include (entry->reg, lcom, i))
989 {
990 if (entry->direct == COMMUNITY_PERMIT)
991 {
992 com_index_to_delete[delete_index] = i;
993 delete_index++;
994 }
995 break;
996 }
997 }
998 }
999
1000 /* Delete all of the communities we flagged for deletion */
1001 ptr = lcom->val;
1002 for (i = delete_index-1; i >= 0; i--)
1003 {
1004 ptr += (com_index_to_delete[i] * LCOMMUNITY_SIZE);
1005 lcommunity_del_val (lcom, ptr);
1006 }
1007
1008 return lcom;
1009 }
1010
1011 /* Set lcommunity-list. */
1012 int
1013 lcommunity_list_set (struct community_list_handler *ch,
1014 const char *name, const char *str, int direct, int style)
1015 {
1016 struct community_entry *entry = NULL;
1017 struct community_list *list;
1018 struct lcommunity *lcom = NULL;
1019 regex_t *regex = NULL;
1020
1021 /* Get community list. */
1022 list = community_list_get (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1023
1024 /* When community-list already has entry, new entry should have same
1025 style. If you want to have mixed style community-list, you can
1026 comment out this check. */
1027 if (!community_list_empty_p (list))
1028 {
1029 struct community_entry *first;
1030
1031 first = list->head;
1032
1033 if (style != first->style)
1034 {
1035 return (first->style == COMMUNITY_LIST_STANDARD
1036 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1037 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1038 }
1039 }
1040
1041 if (str)
1042 {
1043 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1044 lcom = lcommunity_str2com (str);
1045 else
1046 regex = bgp_regcomp (str);
1047
1048 if (! lcom && ! regex)
1049 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1050 }
1051
1052 entry = community_entry_new ();
1053 entry->direct = direct;
1054 entry->style = style;
1055 entry->any = (str ? 0 : 1);
1056 entry->u.lcom = lcom;
1057 entry->reg = regex;
1058 if (lcom)
1059 entry->config = lcommunity_lcom2str (lcom, LCOMMUNITY_FORMAT_COMMUNITY_LIST);
1060 else if (regex)
1061 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1062 else
1063 entry->config = NULL;
1064
1065 /* Do not put duplicated community entry. */
1066 if (community_list_dup_check (list, entry))
1067 community_entry_free (entry);
1068 else
1069 community_list_entry_add (list, entry);
1070
1071 return 0;
1072 }
1073
1074 /* Unset community-list. When str is NULL, delete all of
1075 community-list entry belongs to the specified name. */
1076 int
1077 lcommunity_list_unset (struct community_list_handler *ch,
1078 const char *name, const char *str,
1079 int direct, int style)
1080 {
1081 struct community_entry *entry = NULL;
1082 struct community_list *list;
1083 struct lcommunity *lcom = NULL;
1084 regex_t *regex = NULL;
1085
1086 /* Lookup community list. */
1087 list = community_list_lookup (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1088 if (list == NULL)
1089 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1090
1091 /* Delete all of entry belongs to this community-list. */
1092 if (!str)
1093 {
1094 community_list_delete (list);
1095 return 0;
1096 }
1097
1098 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1099 lcom = lcommunity_str2com (str);
1100 else
1101 regex = bgp_regcomp (str);
1102
1103 if (! lcom && ! regex)
1104 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1105
1106 if (lcom)
1107 entry = community_list_entry_lookup (list, lcom, direct);
1108 else
1109 entry = community_list_entry_lookup (list, str, direct);
1110
1111 if (lcom)
1112 lcommunity_free (&lcom);
1113 if (regex)
1114 bgp_regex_free (regex);
1115
1116 if (!entry)
1117 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1118
1119 community_list_entry_delete (list, entry, style);
1120
1121 return 0;
1122 }
1123
1124 /* Set extcommunity-list. */
1125 int
1126 extcommunity_list_set (struct community_list_handler *ch,
1127 const char *name, const char *str,
1128 int direct, int style)
1129 {
1130 struct community_entry *entry = NULL;
1131 struct community_list *list;
1132 struct ecommunity *ecom = NULL;
1133 regex_t *regex = NULL;
1134
1135 entry = NULL;
1136
1137 /* Get community list. */
1138 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
1139
1140 /* When community-list already has entry, new entry should have same
1141 style. If you want to have mixed style community-list, you can
1142 comment out this check. */
1143 if (!community_list_empty_p (list))
1144 {
1145 struct community_entry *first;
1146
1147 first = list->head;
1148
1149 if (style != first->style)
1150 {
1151 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1152 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1153 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1154 }
1155 }
1156
1157 if (str)
1158 {
1159 if (style == EXTCOMMUNITY_LIST_STANDARD)
1160 ecom = ecommunity_str2com (str, 0, 1);
1161 else
1162 regex = bgp_regcomp (str);
1163
1164 if (! ecom && ! regex)
1165 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1166 }
1167
1168 if (ecom)
1169 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
1170
1171 entry = community_entry_new ();
1172 entry->direct = direct;
1173 entry->style = style;
1174 entry->any = (str ? 0 : 1);
1175 if (ecom)
1176 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0);
1177 else if (regex)
1178 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1179 else
1180 entry->config = NULL;
1181 entry->u.ecom = ecom;
1182 entry->reg = regex;
1183
1184 /* Do not put duplicated community entry. */
1185 if (community_list_dup_check (list, entry))
1186 community_entry_free (entry);
1187 else
1188 {
1189 community_list_entry_add (list, entry);
1190 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1191 }
1192
1193 return 0;
1194 }
1195
1196 /* Unset extcommunity-list. When str is NULL, delete all of
1197 extcommunity-list entry belongs to the specified name. */
1198 int
1199 extcommunity_list_unset (struct community_list_handler *ch,
1200 const char *name, const char *str,
1201 int direct, int style, int delete_all)
1202 {
1203 struct community_entry *entry = NULL;
1204 struct community_list *list;
1205 struct ecommunity *ecom = NULL;
1206
1207 /* Lookup extcommunity list. */
1208 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
1209 if (list == NULL)
1210 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1211
1212 /* Delete all of entry belongs to this extcommunity-list. */
1213 if (delete_all)
1214 {
1215 community_list_delete (list);
1216 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1217 return 0;
1218 }
1219
1220 if (style == EXTCOMMUNITY_LIST_STANDARD)
1221 {
1222 if (str)
1223 ecom = ecommunity_str2com (str, 0, 1);
1224 }
1225
1226 if (ecom)
1227 {
1228 entry = community_list_entry_lookup (list, ecom, direct);
1229 ecommunity_free (&ecom);
1230 }
1231 else
1232 entry = community_list_entry_lookup (list, str, direct);
1233
1234 if (!entry)
1235 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1236
1237 community_list_entry_delete (list, entry, style);
1238 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1239
1240 return 0;
1241 }
1242
1243 /* Initializa community-list. Return community-list handler. */
1244 struct community_list_handler *
1245 community_list_init (void)
1246 {
1247 struct community_list_handler *ch;
1248 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
1249 sizeof (struct community_list_handler));
1250 return ch;
1251 }
1252
1253 /* Terminate community-list. */
1254 void
1255 community_list_terminate (struct community_list_handler *ch)
1256 {
1257 struct community_list_master *cm;
1258 struct community_list *list;
1259
1260 cm = &ch->community_list;
1261 while ((list = cm->num.head) != NULL)
1262 community_list_delete (list);
1263 while ((list = cm->str.head) != NULL)
1264 community_list_delete (list);
1265
1266 cm = &ch->lcommunity_list;
1267 while ((list = cm->num.head) != NULL)
1268 community_list_delete (list);
1269 while ((list = cm->str.head) != NULL)
1270 community_list_delete (list);
1271
1272 cm = &ch->extcommunity_list;
1273 while ((list = cm->num.head) != NULL)
1274 community_list_delete (list);
1275 while ((list = cm->str.head) != NULL)
1276 community_list_delete (list);
1277
1278 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
1279 }