]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_clist.c
bgpd: Add ability to clean lcommunity hash
[mirror_frr.git] / bgpd / bgp_clist.c
CommitLineData
718e3744 1/* BGP community-list and extcommunity-list.
2 Copyright (C) 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "command.h"
24#include "prefix.h"
25#include "memory.h"
3f9c7369 26#include "queue.h"
039f3a34 27#include "filter.h"
718e3744 28
29#include "bgpd/bgpd.h"
30#include "bgpd/bgp_community.h"
31#include "bgpd/bgp_ecommunity.h"
57d187bc 32#include "bgpd/bgp_lcommunity.h"
718e3744 33#include "bgpd/bgp_aspath.h"
34#include "bgpd/bgp_regex.h"
35#include "bgpd/bgp_clist.h"
6b0655a2 36
718e3744 37/* Lookup master structure for community-list or
38 extcommunity-list. */
39struct community_list_master *
fee6e4e4 40community_list_master_lookup (struct community_list_handler *ch, int master)
718e3744 41{
42 if (ch)
fee6e4e4 43 switch (master)
718e3744 44 {
fee6e4e4 45 case COMMUNITY_LIST_MASTER:
46 return &ch->community_list;
fee6e4e4 47 case EXTCOMMUNITY_LIST_MASTER:
48 return &ch->extcommunity_list;
57d187bc
JS
49 case LARGE_COMMUNITY_LIST_MASTER:
50 return &ch->lcommunity_list;
718e3744 51 }
52 return NULL;
53}
54
55/* Allocate a new community list entry. */
94f2b392 56static struct community_entry *
66e5cd87 57community_entry_new (void)
718e3744 58{
393deb9b 59 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
718e3744 60}
61
62/* Free community list entry. */
94f2b392 63static void
718e3744 64community_entry_free (struct community_entry *entry)
65{
66 switch (entry->style)
67 {
68 case COMMUNITY_LIST_STANDARD:
69 if (entry->u.com)
8708b74f 70 community_free (entry->u.com);
718e3744 71 break;
57d187bc
JS
72 case LARGE_COMMUNITY_LIST_STANDARD:
73 if (entry->u.lcom)
74 lcommunity_free (&entry->u.lcom);
75 break;
718e3744 76 case EXTCOMMUNITY_LIST_STANDARD:
77 /* In case of standard extcommunity-list, configuration string
8708b74f 78 is made by ecommunity_ecom2str(). */
718e3744 79 if (entry->config)
8708b74f 80 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
718e3744 81 if (entry->u.ecom)
f6f434b2 82 ecommunity_free (&entry->u.ecom);
718e3744 83 break;
84 case COMMUNITY_LIST_EXPANDED:
85 case EXTCOMMUNITY_LIST_EXPANDED:
57d187bc 86 case LARGE_COMMUNITY_LIST_EXPANDED:
718e3744 87 if (entry->config)
8708b74f 88 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
718e3744 89 if (entry->reg)
8708b74f 90 bgp_regex_free (entry->reg);
718e3744 91 default:
92 break;
93 }
94 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
95}
96
97/* Allocate a new community-list. */
94f2b392 98static struct community_list *
66e5cd87 99community_list_new (void)
718e3744 100{
393deb9b 101 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
718e3744 102}
103
104/* Free community-list. */
94f2b392 105static void
718e3744 106community_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
94f2b392 113static struct community_list *
718e3744 114community_list_insert (struct community_list_handler *ch,
fee6e4e4 115 const char *name, int master)
718e3744 116{
fd79ac91 117 size_t i;
718e3744 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. */
fee6e4e4 125 cm = community_list_master_lookup (ch, master);
8708b74f 126 if (!cm)
718e3744 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]))
8708b74f 138 number = (number * 10) + (name[i] - '0');
718e3744 139 else
8708b74f 140 break;
718e3744 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)
8708b74f 152 if (atol (point->name) >= number)
153 break;
718e3744 154 }
155 else
156 {
157 new->sort = COMMUNITY_LIST_STRING;
158
159 /* Set access_list to string list. */
160 list = &cm->str;
8708b74f 161
718e3744 162 /* Set point to insertion point. */
163 for (point = list->head; point; point = point->next)
8708b74f 164 if (strcmp (point->name, name) >= 0)
165 break;
718e3744 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
207struct community_list *
208community_list_lookup (struct community_list_handler *ch,
fee6e4e4 209 const char *name, int master)
718e3744 210{
211 struct community_list *list;
212 struct community_list_master *cm;
213
8708b74f 214 if (!name)
718e3744 215 return NULL;
216
fee6e4e4 217 cm = community_list_master_lookup (ch, master);
8708b74f 218 if (!cm)
718e3744 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
94f2b392 231static struct community_list *
fee6e4e4 232community_list_get (struct community_list_handler *ch,
233 const char *name, int master)
718e3744 234{
235 struct community_list *list;
236
fee6e4e4 237 list = community_list_lookup (ch, name, master);
8708b74f 238 if (!list)
fee6e4e4 239 list = community_list_insert (ch, name, master);
718e3744 240 return list;
241}
242
94f2b392 243static void
718e3744 244community_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
94f2b392 270static int
718e3744 271community_list_empty_p (struct community_list *list)
272{
273 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
274}
6b0655a2 275
718e3744 276/* Add community-list entry to the list. */
277static void
8708b74f 278community_list_entry_add (struct community_list *list,
279 struct community_entry *entry)
718e3744 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. */
292static void
293community_list_entry_delete (struct community_list *list,
8708b74f 294 struct community_entry *entry, int style)
718e3744 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. */
313static struct community_entry *
fd79ac91 314community_list_entry_lookup (struct community_list *list, const void *arg,
8708b74f 315 int direct)
718e3744 316{
317 struct community_entry *entry;
318
319 for (entry = list->head; entry; entry = entry->next)
320 {
321 switch (entry->style)
8708b74f 322 {
323 case COMMUNITY_LIST_STANDARD:
813d4307 324 if (entry->direct == direct && community_cmp (entry->u.com, arg))
8708b74f 325 return entry;
326 break;
327 case EXTCOMMUNITY_LIST_STANDARD:
813d4307 328 if (entry->direct == direct && ecommunity_cmp (entry->u.ecom, arg))
8708b74f 329 return entry;
330 break;
57d187bc
JS
331 case LARGE_COMMUNITY_LIST_STANDARD:
332 if (entry->direct == direct && lcommunity_cmp (entry->u.lcom, arg))
333 return entry;
334 break;
8708b74f 335 case COMMUNITY_LIST_EXPANDED:
336 case EXTCOMMUNITY_LIST_EXPANDED:
57d187bc 337 case LARGE_COMMUNITY_LIST_EXPANDED:
813d4307 338 if (entry->direct == direct && strcmp (entry->config, arg) == 0)
8708b74f 339 return entry;
340 break;
341 default:
342 break;
343 }
718e3744 344 }
345 return NULL;
346}
6b0655a2 347
5cbea288
DS
348static char *
349community_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. */
416static int
417community_regexp_include (regex_t * reg, struct community *com, int i)
418{
b84ee83b
CF
419 char *str;
420 int rv;
5cbea288
DS
421
422 /* When there is no communities attribute it is treated as empty
423 * string. */
424 if (com == NULL || com->size == 0)
b84ee83b 425 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
5cbea288
DS
426 else
427 str = community_str_get (com, i);
428
429 /* Regular expression match. */
b84ee83b
CF
430 rv = regexec (reg, str, 0, NULL, 0);
431
432 XFREE(MTYPE_COMMUNITY_STR, str);
433
434 if (rv == 0)
5cbea288
DS
435 return 1;
436
437 /* No match. */
438 return 0;
439}
440
718e3744 441/* Internal function to perform regular expression match for community
442 attribute. */
443static int
8708b74f 444community_regexp_match (struct community *com, regex_t * reg)
718e3744 445{
fd79ac91 446 const char *str;
718e3744 447
448 /* When there is no communities attribute it is treated as empty
449 string. */
450 if (com == NULL || com->size == 0)
451 str = "";
452 else
453 str = community_str (com);
454
455 /* Regular expression match. */
456 if (regexec (reg, str, 0, NULL, 0) == 0)
457 return 1;
458
459 /* No match. */
460 return 0;
461}
462
57d187bc
JS
463static char *
464lcommunity_str_get (struct lcommunity *lcom, int i)
465{
466 struct lcommunity_val lcomval;
467 u_int32_t globaladmin;
468 u_int32_t localdata1;
469 u_int32_t localdata2;
470 char *str;
471 u_char *ptr;
472 char *pnt;
473
474 ptr = lcom->val;
475 ptr += (i * LCOMMUNITY_SIZE);
476
477 memcpy (&lcomval, ptr, LCOMMUNITY_SIZE);
478
479 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
480 str = pnt = XMALLOC (MTYPE_LCOMMUNITY_STR, 48);
481
482 ptr = (u_char *)lcomval.val;
483 globaladmin = (*ptr++ << 24);
484 globaladmin |= (*ptr++ << 16);
485 globaladmin |= (*ptr++ << 8);
486 globaladmin |= (*ptr++);
487
488 localdata1 = (*ptr++ << 24);
489 localdata1 |= (*ptr++ << 16);
490 localdata1 |= (*ptr++ << 8);
491 localdata1 |= (*ptr++);
492
493 localdata2 = (*ptr++ << 24);
494 localdata2 |= (*ptr++ << 16);
495 localdata2 |= (*ptr++ << 8);
496 localdata2 |= (*ptr++);
497
498 sprintf (pnt, "%u:%u:%u", globaladmin, localdata1, localdata2);
499 pnt += strlen (pnt);
500 *pnt = '\0';
501
502 return str;
503}
504
505/* Internal function to perform regular expression match for
506 * * a single community. */
507static int
508lcommunity_regexp_include (regex_t * reg, struct lcommunity *lcom, int i)
509{
510 const char *str;
511
512 /* When there is no communities attribute it is treated as empty
513 * string. */
514 if (lcom == NULL || lcom->size == 0)
515 str = "";
516 else
517 str = lcommunity_str_get (lcom, i);
518
519 /* Regular expression match. */
520 if (regexec (reg, str, 0, NULL, 0) == 0)
521 return 1;
522
523 /* No match. */
524 return 0;
525}
526
527static int
528lcommunity_regexp_match (struct lcommunity *com, regex_t * reg)
529{
530 const char *str;
531
532 /* When there is no communities attribute it is treated as empty
533 string. */
534 if (com == NULL || com->size == 0)
535 str = "";
536 else
537 str = lcommunity_str (com);
538
539 /* Regular expression match. */
540 if (regexec (reg, str, 0, NULL, 0) == 0)
541 return 1;
542
543 /* No match. */
544 return 0;
545}
546
547
8708b74f 548static int
549ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
550{
fd79ac91 551 const char *str;
8708b74f 552
553 /* When there is no communities attribute it is treated as empty
554 string. */
555 if (ecom == NULL || ecom->size == 0)
556 str = "";
557 else
558 str = ecommunity_str (ecom);
559
560 /* Regular expression match. */
561 if (regexec (reg, str, 0, NULL, 0) == 0)
562 return 1;
563
564 /* No match. */
565 return 0;
566}
567
ffd0c037 568#if 0
718e3744 569/* Delete community attribute using regular expression match. Return
570 modified communites attribute. */
571static struct community *
8708b74f 572community_regexp_delete (struct community *com, regex_t * reg)
718e3744 573{
574 int i;
575 u_int32_t comval;
576 /* Maximum is "65535:65535" + '\0'. */
577 char c[12];
fd79ac91 578 const char *str;
718e3744 579
8708b74f 580 if (!com)
718e3744 581 return NULL;
582
583 i = 0;
584 while (i < com->size)
585 {
586 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
587 comval = ntohl (comval);
588
589 switch (comval)
8708b74f 590 {
591 case COMMUNITY_INTERNET:
592 str = "internet";
593 break;
594 case COMMUNITY_NO_EXPORT:
595 str = "no-export";
596 break;
597 case COMMUNITY_NO_ADVERTISE:
598 str = "no-advertise";
599 break;
600 case COMMUNITY_LOCAL_AS:
601 str = "local-AS";
602 break;
603 default:
604 sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF, comval & 0xFFFF);
605 str = c;
606 break;
607 }
718e3744 608
609 if (regexec (reg, str, 0, NULL, 0) == 0)
8708b74f 610 community_del_val (com, com_nthval (com, i));
718e3744 611 else
8708b74f 612 i++;
718e3744 613 }
614 return com;
615}
ffd0c037 616#endif
718e3744 617
618/* When given community attribute matches to the community-list return
619 1 else return 0. */
620int
621community_list_match (struct community *com, 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)
8708b74f 628 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 629
630 if (entry->style == COMMUNITY_LIST_STANDARD)
8708b74f 631 {
632 if (community_include (entry->u.com, COMMUNITY_INTERNET))
633 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 634
8708b74f 635 if (community_match (com, entry->u.com))
636 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
637 }
718e3744 638 else if (entry->style == COMMUNITY_LIST_EXPANDED)
8708b74f 639 {
640 if (community_regexp_match (com, entry->reg))
641 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
642 }
643 }
644 return 0;
645}
646
57d187bc
JS
647int
648lcommunity_list_match (struct lcommunity *lcom, 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 == LARGE_COMMUNITY_LIST_STANDARD)
658 {
659 if (lcommunity_match (lcom, entry->u.lcom))
660 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
661 }
662 else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED)
663 {
664 if (lcommunity_regexp_match (lcom, entry->reg))
665 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
666 }
667 }
668 return 0;
669}
670
8708b74f 671int
672ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
673{
674 struct community_entry *entry;
675
676 for (entry = list->head; entry; entry = entry->next)
677 {
678 if (entry->any)
679 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
680
681 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
682 {
683 if (ecommunity_match (ecom, entry->u.ecom))
684 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
685 }
686 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
687 {
688 if (ecommunity_regexp_match (ecom, entry->reg))
689 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
690 }
718e3744 691 }
692 return 0;
693}
694
695/* Perform exact matching. In case of expanded community-list, do
696 same thing as community_list_match(). */
697int
8708b74f 698community_list_exact_match (struct community *com,
699 struct community_list *list)
718e3744 700{
701 struct community_entry *entry;
702
703 for (entry = list->head; entry; entry = entry->next)
704 {
705 if (entry->any)
8708b74f 706 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 707
708 if (entry->style == COMMUNITY_LIST_STANDARD)
8708b74f 709 {
710 if (community_include (entry->u.com, COMMUNITY_INTERNET))
711 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 712
8708b74f 713 if (community_cmp (com, entry->u.com))
714 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
715 }
718e3744 716 else if (entry->style == COMMUNITY_LIST_EXPANDED)
8708b74f 717 {
718 if (community_regexp_match (com, entry->reg))
719 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
720 }
718e3744 721 }
722 return 0;
723}
724
8708b74f 725/* Delete all permitted communities in the list from com. */
718e3744 726struct community *
727community_list_match_delete (struct community *com,
8708b74f 728 struct community_list *list)
718e3744 729{
730 struct community_entry *entry;
5cbea288
DS
731 u_int32_t val;
732 u_int32_t com_index_to_delete[com->size];
733 int delete_index = 0;
734 int i;
718e3744 735
5cbea288
DS
736 /* Loop over each community value and evaluate each against the
737 * community-list. If we need to delete a community value add its index to
738 * com_index_to_delete.
739 */
740 for (i = 0; i < com->size; i++)
718e3744 741 {
5cbea288
DS
742 val = community_val_get (com, i);
743
744 for (entry = list->head; entry; entry = entry->next)
8708b74f 745 {
5cbea288 746 if (entry->any)
847375b9 747 {
5cbea288
DS
748 if (entry->direct == COMMUNITY_PERMIT)
749 {
750 com_index_to_delete[delete_index] = i;
751 delete_index++;
752 }
753 break;
847375b9 754 }
718e3744 755
5cbea288
DS
756 else if ((entry->style == COMMUNITY_LIST_STANDARD)
757 && (community_include (entry->u.com, COMMUNITY_INTERNET)
758 || community_include (entry->u.com, val) ))
759 {
847375b9 760 if (entry->direct == COMMUNITY_PERMIT)
5cbea288
DS
761 {
762 com_index_to_delete[delete_index] = i;
763 delete_index++;
764 }
765 break;
766 }
767
768 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
769 && community_regexp_include (entry->reg, com, i))
770 {
771 if (entry->direct == COMMUNITY_PERMIT)
772 {
773 com_index_to_delete[delete_index] = i;
774 delete_index++;
775 }
776 break;
777 }
778 }
779 }
780
781 /* Delete all of the communities we flagged for deletion */
782 for (i = delete_index-1; i >= 0; i--)
783 {
784 val = community_val_get (com, com_index_to_delete[i]);
785 community_del_val (com, &val);
718e3744 786 }
5cbea288 787
718e3744 788 return com;
789}
790
791/* To avoid duplicated entry in the community-list, this function
792 compares specified entry to existing entry. */
94f2b392 793static int
8708b74f 794community_list_dup_check (struct community_list *list,
795 struct community_entry *new)
718e3744 796{
797 struct community_entry *entry;
8708b74f 798
718e3744 799 for (entry = list->head; entry; entry = entry->next)
800 {
801 if (entry->style != new->style)
8708b74f 802 continue;
718e3744 803
804 if (entry->direct != new->direct)
8708b74f 805 continue;
718e3744 806
807 if (entry->any != new->any)
8708b74f 808 continue;
718e3744 809
810 if (entry->any)
8708b74f 811 return 1;
718e3744 812
813 switch (entry->style)
8708b74f 814 {
815 case COMMUNITY_LIST_STANDARD:
816 if (community_cmp (entry->u.com, new->u.com))
817 return 1;
818 break;
57d187bc
JS
819 case LARGE_COMMUNITY_LIST_STANDARD:
820 if (lcommunity_cmp (entry->u.lcom, new->u.lcom))
821 return 1;
822 break;
8708b74f 823 case EXTCOMMUNITY_LIST_STANDARD:
824 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
825 return 1;
826 break;
827 case COMMUNITY_LIST_EXPANDED:
828 case EXTCOMMUNITY_LIST_EXPANDED:
57d187bc 829 case LARGE_COMMUNITY_LIST_EXPANDED:
8708b74f 830 if (strcmp (entry->config, new->config) == 0)
831 return 1;
832 break;
833 default:
834 break;
835 }
718e3744 836 }
837 return 0;
838}
6b0655a2 839
718e3744 840/* Set community-list. */
841int
842community_list_set (struct community_list_handler *ch,
fd79ac91 843 const char *name, const char *str, int direct, int style)
718e3744 844{
fee6e4e4 845 struct community_entry *entry = NULL;
718e3744 846 struct community_list *list;
fee6e4e4 847 struct community *com = NULL;
848 regex_t *regex = NULL;
718e3744 849
850 /* Get community list. */
fee6e4e4 851 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
718e3744 852
853 /* When community-list already has entry, new entry should have same
854 style. If you want to have mixed style community-list, you can
855 comment out this check. */
8708b74f 856 if (!community_list_empty_p (list))
718e3744 857 {
858 struct community_entry *first;
859
860 first = list->head;
861
fee6e4e4 862 if (style != first->style)
863 {
864 return (first->style == COMMUNITY_LIST_STANDARD
865 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
866 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
867 }
718e3744 868 }
869
fee6e4e4 870 if (str)
718e3744 871 {
fee6e4e4 872 if (style == COMMUNITY_LIST_STANDARD)
873 com = community_str2com (str);
718e3744 874 else
fee6e4e4 875 regex = bgp_regcomp (str);
8708b74f 876
fee6e4e4 877 if (! com && ! regex)
878 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
718e3744 879 }
880
fee6e4e4 881 entry = community_entry_new ();
882 entry->direct = direct;
883 entry->style = style;
884 entry->any = (str ? 0 : 1);
885 entry->u.com = com;
886 entry->reg = regex;
887 entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
888
718e3744 889 /* Do not put duplicated community entry. */
890 if (community_list_dup_check (list, entry))
891 community_entry_free (entry);
892 else
518f0eb1
DS
893 {
894 community_list_entry_add (list, entry);
895 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
896 }
718e3744 897
898 return 0;
899}
900
813d4307 901/* Unset community-list */
718e3744 902int
903community_list_unset (struct community_list_handler *ch,
fd79ac91 904 const char *name, const char *str,
813d4307 905 int direct, int style, int delete_all)
718e3744 906{
fee6e4e4 907 struct community_entry *entry = NULL;
718e3744 908 struct community_list *list;
fee6e4e4 909 struct community *com = NULL;
718e3744 910
911 /* Lookup community list. */
fee6e4e4 912 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
718e3744 913 if (list == NULL)
914 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
915
916 /* Delete all of entry belongs to this community-list. */
813d4307 917 if (delete_all)
718e3744 918 {
919 community_list_delete (list);
518f0eb1 920 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 921 return 0;
922 }
923
fee6e4e4 924 if (style == COMMUNITY_LIST_STANDARD)
813d4307
DW
925 {
926 if (str)
927 com = community_str2com (str);
928 }
718e3744 929
fee6e4e4 930 if (com)
813d4307
DW
931 {
932 entry = community_list_entry_lookup (list, com, direct);
933 community_free (com);
934 }
fee6e4e4 935 else
936 entry = community_list_entry_lookup (list, str, direct);
937
8708b74f 938 if (!entry)
718e3744 939 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
940
941 community_list_entry_delete (list, entry, style);
518f0eb1 942 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 943
944 return 0;
945}
946
57d187bc
JS
947/* Delete all permitted large communities in the list from com. */
948struct lcommunity *
949lcommunity_list_match_delete (struct lcommunity *lcom,
950 struct community_list *list)
951{
952 struct community_entry *entry;
953 u_int32_t com_index_to_delete[lcom->size];
954 u_char *ptr;
955 int delete_index = 0;
956 int i;
957
958 /* Loop over each lcommunity value and evaluate each against the
959 * community-list. If we need to delete a community value add its index to
960 * com_index_to_delete.
961 */
962 ptr = lcom->val;
963 for (i = 0; i < lcom->size; i++)
964 {
965 ptr += (i * LCOMMUNITY_SIZE);
966 for (entry = list->head; entry; entry = entry->next)
967 {
968 if (entry->any)
969 {
970 if (entry->direct == COMMUNITY_PERMIT)
971 {
972 com_index_to_delete[delete_index] = i;
973 delete_index++;
974 }
975 break;
976 }
977
978 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
979 && lcommunity_include (entry->u.lcom, ptr) )
980 {
981 if (entry->direct == COMMUNITY_PERMIT)
982 {
983 com_index_to_delete[delete_index] = i;
984 delete_index++;
985 }
986 break;
987 }
988
989 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
990 && lcommunity_regexp_include (entry->reg, lcom, i))
991 {
992 if (entry->direct == COMMUNITY_PERMIT)
993 {
994 com_index_to_delete[delete_index] = i;
995 delete_index++;
996 }
997 break;
998 }
999 }
1000 }
1001
1002 /* Delete all of the communities we flagged for deletion */
1003 ptr = lcom->val;
1004 for (i = delete_index-1; i >= 0; i--)
1005 {
1006 ptr += (com_index_to_delete[i] * LCOMMUNITY_SIZE);
1007 lcommunity_del_val (lcom, ptr);
1008 }
1009
1010 return lcom;
1011}
1012
1013/* Set lcommunity-list. */
1014int
1015lcommunity_list_set (struct community_list_handler *ch,
1016 const char *name, const char *str, int direct, int style)
1017{
1018 struct community_entry *entry = NULL;
1019 struct community_list *list;
1020 struct lcommunity *lcom = NULL;
1021 regex_t *regex = NULL;
1022
1023 /* Get community list. */
1024 list = community_list_get (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1025
1026 /* When community-list already has entry, new entry should have same
1027 style. If you want to have mixed style community-list, you can
1028 comment out this check. */
1029 if (!community_list_empty_p (list))
1030 {
1031 struct community_entry *first;
1032
1033 first = list->head;
1034
1035 if (style != first->style)
1036 {
1037 return (first->style == COMMUNITY_LIST_STANDARD
1038 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1039 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1040 }
1041 }
1042
1043 if (str)
1044 {
1045 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1046 lcom = lcommunity_str2com (str);
1047 else
1048 regex = bgp_regcomp (str);
1049
1050 if (! lcom && ! regex)
1051 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1052 }
1053
1054 entry = community_entry_new ();
1055 entry->direct = direct;
1056 entry->style = style;
1057 entry->any = (str ? 0 : 1);
1058 entry->u.lcom = lcom;
1059 entry->reg = regex;
1060 if (lcom)
1061 entry->config = lcommunity_lcom2str (lcom, LCOMMUNITY_FORMAT_COMMUNITY_LIST);
1062 else if (regex)
1063 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1064 else
1065 entry->config = NULL;
1066
1067 /* Do not put duplicated community entry. */
1068 if (community_list_dup_check (list, entry))
1069 community_entry_free (entry);
1070 else
1071 community_list_entry_add (list, entry);
1072
1073 return 0;
1074}
1075
1076/* Unset community-list. When str is NULL, delete all of
1077 community-list entry belongs to the specified name. */
1078int
1079lcommunity_list_unset (struct community_list_handler *ch,
1080 const char *name, const char *str,
1081 int direct, int style)
1082{
1083 struct community_entry *entry = NULL;
1084 struct community_list *list;
1085 struct lcommunity *lcom = NULL;
1086 regex_t *regex = NULL;
1087
1088 /* Lookup community list. */
1089 list = community_list_lookup (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1090 if (list == NULL)
1091 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1092
1093 /* Delete all of entry belongs to this community-list. */
1094 if (!str)
1095 {
1096 community_list_delete (list);
1097 return 0;
1098 }
1099
1100 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1101 lcom = lcommunity_str2com (str);
1102 else
1103 regex = bgp_regcomp (str);
1104
1105 if (! lcom && ! regex)
1106 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1107
1108 if (lcom)
1109 entry = community_list_entry_lookup (list, lcom, direct);
1110 else
1111 entry = community_list_entry_lookup (list, str, direct);
1112
1113 if (lcom)
1114 lcommunity_free (&lcom);
1115 if (regex)
1116 bgp_regex_free (regex);
1117
1118 if (!entry)
1119 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1120
1121 community_list_entry_delete (list, entry, style);
1122
1123 return 0;
1124}
1125
718e3744 1126/* Set extcommunity-list. */
1127int
1128extcommunity_list_set (struct community_list_handler *ch,
fd79ac91 1129 const char *name, const char *str,
1130 int direct, int style)
718e3744 1131{
fee6e4e4 1132 struct community_entry *entry = NULL;
718e3744 1133 struct community_list *list;
fee6e4e4 1134 struct ecommunity *ecom = NULL;
1135 regex_t *regex = NULL;
718e3744 1136
1137 entry = NULL;
1138
1139 /* Get community list. */
fee6e4e4 1140 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 1141
1142 /* When community-list already has entry, new entry should have same
1143 style. If you want to have mixed style community-list, you can
1144 comment out this check. */
8708b74f 1145 if (!community_list_empty_p (list))
718e3744 1146 {
1147 struct community_entry *first;
1148
1149 first = list->head;
1150
fee6e4e4 1151 if (style != first->style)
1152 {
1153 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1154 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1155 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1156 }
718e3744 1157 }
1158
fee6e4e4 1159 if (str)
718e3744 1160 {
fee6e4e4 1161 if (style == EXTCOMMUNITY_LIST_STANDARD)
1162 ecom = ecommunity_str2com (str, 0, 1);
718e3744 1163 else
fee6e4e4 1164 regex = bgp_regcomp (str);
8708b74f 1165
fee6e4e4 1166 if (! ecom && ! regex)
1167 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
718e3744 1168 }
1169
fee6e4e4 1170 if (ecom)
1171 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
1172
1173 entry = community_entry_new ();
1174 entry->direct = direct;
1175 entry->style = style;
1176 entry->any = (str ? 0 : 1);
1177 if (ecom)
1178 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
1179 else if (regex)
1180 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1181 else
1182 entry->config = NULL;
1183 entry->u.ecom = ecom;
1184 entry->reg = regex;
1185
718e3744 1186 /* Do not put duplicated community entry. */
1187 if (community_list_dup_check (list, entry))
1188 community_entry_free (entry);
1189 else
518f0eb1
DS
1190 {
1191 community_list_entry_add (list, entry);
1192 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1193 }
718e3744 1194
1195 return 0;
1196}
1197
1198/* Unset extcommunity-list. When str is NULL, delete all of
1199 extcommunity-list entry belongs to the specified name. */
1200int
1201extcommunity_list_unset (struct community_list_handler *ch,
fd79ac91 1202 const char *name, const char *str,
813d4307 1203 int direct, int style, int delete_all)
718e3744 1204{
fee6e4e4 1205 struct community_entry *entry = NULL;
718e3744 1206 struct community_list *list;
1207 struct ecommunity *ecom = NULL;
718e3744 1208
1209 /* Lookup extcommunity list. */
fee6e4e4 1210 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 1211 if (list == NULL)
1212 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1213
1214 /* Delete all of entry belongs to this extcommunity-list. */
813d4307 1215 if (delete_all)
718e3744 1216 {
1217 community_list_delete (list);
518f0eb1 1218 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 1219 return 0;
1220 }
1221
fee6e4e4 1222 if (style == EXTCOMMUNITY_LIST_STANDARD)
813d4307
DW
1223 {
1224 if (str)
1225 ecom = ecommunity_str2com (str, 0, 1);
1226 }
718e3744 1227
fee6e4e4 1228 if (ecom)
813d4307
DW
1229 {
1230 entry = community_list_entry_lookup (list, ecom, direct);
1231 ecommunity_free (&ecom);
1232 }
fee6e4e4 1233 else
1234 entry = community_list_entry_lookup (list, str, direct);
1235
8708b74f 1236 if (!entry)
718e3744 1237 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1238
1239 community_list_entry_delete (list, entry, style);
518f0eb1 1240 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 1241
1242 return 0;
1243}
1244
1245/* Initializa community-list. Return community-list handler. */
1246struct community_list_handler *
94f2b392 1247community_list_init (void)
718e3744 1248{
1249 struct community_list_handler *ch;
1250 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
8708b74f 1251 sizeof (struct community_list_handler));
718e3744 1252 return ch;
1253}
1254
1255/* Terminate community-list. */
228da428 1256void
718e3744 1257community_list_terminate (struct community_list_handler *ch)
1258{
1259 struct community_list_master *cm;
1260 struct community_list *list;
1261
1262 cm = &ch->community_list;
1263 while ((list = cm->num.head) != NULL)
1264 community_list_delete (list);
1265 while ((list = cm->str.head) != NULL)
1266 community_list_delete (list);
1267
57d187bc
JS
1268 cm = &ch->lcommunity_list;
1269 while ((list = cm->num.head) != NULL)
1270 community_list_delete (list);
1271 while ((list = cm->str.head) != NULL)
1272 community_list_delete (list);
1273
718e3744 1274 cm = &ch->extcommunity_list;
1275 while ((list = cm->num.head) != NULL)
1276 community_list_delete (list);
1277 while ((list = cm->str.head) != NULL)
1278 community_list_delete (list);
1279
1280 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
1281}