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