]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_clist.c
bgpd: lcommunity: fix whitespace & copyright
[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:
2acb4ac2 46 return &ch->community_list;
fee6e4e4 47 case EXTCOMMUNITY_LIST_MASTER:
2acb4ac2 48 return &ch->extcommunity_list;
57d187bc 49 case LARGE_COMMUNITY_LIST_MASTER:
2acb4ac2 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
2acb4ac2 415 * a single community. */
5cbea288
DS
416static int
417community_regexp_include (regex_t * reg, struct community *com, int i)
418{
b84ee83b
CF
419 char *str;
420 int rv;
5cbea288 421
2acb4ac2 422 /* When there is no communities attribute it is treated as empty string. */
5cbea288 423 if (com == NULL || com->size == 0)
b84ee83b 424 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
5cbea288
DS
425 else
426 str = community_str_get (com, i);
427
428 /* Regular expression match. */
b84ee83b
CF
429 rv = regexec (reg, str, 0, NULL, 0);
430
431 XFREE(MTYPE_COMMUNITY_STR, str);
432
433 if (rv == 0)
5cbea288
DS
434 return 1;
435
436 /* No match. */
437 return 0;
438}
439
718e3744 440/* Internal function to perform regular expression match for community
441 attribute. */
442static int
8708b74f 443community_regexp_match (struct community *com, regex_t * reg)
718e3744 444{
fd79ac91 445 const char *str;
718e3744 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
57d187bc
JS
462static char *
463lcommunity_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
2acb4ac2 505 * a single community. */
57d187bc
JS
506static int
507lcommunity_regexp_include (regex_t * reg, struct lcommunity *lcom, int i)
508{
509 const char *str;
510
2acb4ac2 511 /* When there is no communities attribute it is treated as empty string. */
57d187bc
JS
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
525static int
526lcommunity_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
8708b74f 546static int
547ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
548{
fd79ac91 549 const char *str;
8708b74f 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
ffd0c037 566#if 0
718e3744 567/* Delete community attribute using regular expression match. Return
568 modified communites attribute. */
569static struct community *
8708b74f 570community_regexp_delete (struct community *com, regex_t * reg)
718e3744 571{
572 int i;
573 u_int32_t comval;
574 /* Maximum is "65535:65535" + '\0'. */
575 char c[12];
fd79ac91 576 const char *str;
718e3744 577
8708b74f 578 if (!com)
718e3744 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)
8708b74f 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 }
718e3744 606
607 if (regexec (reg, str, 0, NULL, 0) == 0)
8708b74f 608 community_del_val (com, com_nthval (com, i));
718e3744 609 else
8708b74f 610 i++;
718e3744 611 }
612 return com;
613}
ffd0c037 614#endif
718e3744 615
616/* When given community attribute matches to the community-list return
617 1 else return 0. */
618int
619community_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)
8708b74f 626 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 627
628 if (entry->style == COMMUNITY_LIST_STANDARD)
8708b74f 629 {
630 if (community_include (entry->u.com, COMMUNITY_INTERNET))
631 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 632
8708b74f 633 if (community_match (com, entry->u.com))
634 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
635 }
718e3744 636 else if (entry->style == COMMUNITY_LIST_EXPANDED)
8708b74f 637 {
638 if (community_regexp_match (com, entry->reg))
639 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
640 }
641 }
642 return 0;
643}
644
57d187bc
JS
645int
646lcommunity_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
8708b74f 669int
670ecommunity_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 }
718e3744 689 }
690 return 0;
691}
692
693/* Perform exact matching. In case of expanded community-list, do
694 same thing as community_list_match(). */
695int
8708b74f 696community_list_exact_match (struct community *com,
697 struct community_list *list)
718e3744 698{
699 struct community_entry *entry;
700
701 for (entry = list->head; entry; entry = entry->next)
702 {
703 if (entry->any)
8708b74f 704 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 705
706 if (entry->style == COMMUNITY_LIST_STANDARD)
8708b74f 707 {
708 if (community_include (entry->u.com, COMMUNITY_INTERNET))
709 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718e3744 710
8708b74f 711 if (community_cmp (com, entry->u.com))
712 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
713 }
718e3744 714 else if (entry->style == COMMUNITY_LIST_EXPANDED)
8708b74f 715 {
716 if (community_regexp_match (com, entry->reg))
717 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
718 }
718e3744 719 }
720 return 0;
721}
722
8708b74f 723/* Delete all permitted communities in the list from com. */
718e3744 724struct community *
725community_list_match_delete (struct community *com,
8708b74f 726 struct community_list *list)
718e3744 727{
728 struct community_entry *entry;
5cbea288
DS
729 u_int32_t val;
730 u_int32_t com_index_to_delete[com->size];
731 int delete_index = 0;
732 int i;
718e3744 733
5cbea288
DS
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++)
718e3744 739 {
5cbea288
DS
740 val = community_val_get (com, i);
741
742 for (entry = list->head; entry; entry = entry->next)
8708b74f 743 {
5cbea288 744 if (entry->any)
847375b9 745 {
5cbea288
DS
746 if (entry->direct == COMMUNITY_PERMIT)
747 {
748 com_index_to_delete[delete_index] = i;
749 delete_index++;
750 }
751 break;
847375b9 752 }
718e3744 753
5cbea288
DS
754 else if ((entry->style == COMMUNITY_LIST_STANDARD)
755 && (community_include (entry->u.com, COMMUNITY_INTERNET)
756 || community_include (entry->u.com, val) ))
757 {
847375b9 758 if (entry->direct == COMMUNITY_PERMIT)
5cbea288
DS
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);
718e3744 784 }
5cbea288 785
718e3744 786 return com;
787}
788
789/* To avoid duplicated entry in the community-list, this function
790 compares specified entry to existing entry. */
94f2b392 791static int
8708b74f 792community_list_dup_check (struct community_list *list,
793 struct community_entry *new)
718e3744 794{
795 struct community_entry *entry;
8708b74f 796
718e3744 797 for (entry = list->head; entry; entry = entry->next)
798 {
799 if (entry->style != new->style)
8708b74f 800 continue;
718e3744 801
802 if (entry->direct != new->direct)
8708b74f 803 continue;
718e3744 804
805 if (entry->any != new->any)
8708b74f 806 continue;
718e3744 807
808 if (entry->any)
8708b74f 809 return 1;
718e3744 810
811 switch (entry->style)
8708b74f 812 {
813 case COMMUNITY_LIST_STANDARD:
814 if (community_cmp (entry->u.com, new->u.com))
815 return 1;
816 break;
57d187bc
JS
817 case LARGE_COMMUNITY_LIST_STANDARD:
818 if (lcommunity_cmp (entry->u.lcom, new->u.lcom))
819 return 1;
820 break;
8708b74f 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:
2acb4ac2 827 case LARGE_COMMUNITY_LIST_EXPANDED:
8708b74f 828 if (strcmp (entry->config, new->config) == 0)
829 return 1;
830 break;
831 default:
832 break;
833 }
718e3744 834 }
835 return 0;
836}
6b0655a2 837
718e3744 838/* Set community-list. */
839int
840community_list_set (struct community_list_handler *ch,
fd79ac91 841 const char *name, const char *str, int direct, int style)
718e3744 842{
fee6e4e4 843 struct community_entry *entry = NULL;
718e3744 844 struct community_list *list;
fee6e4e4 845 struct community *com = NULL;
846 regex_t *regex = NULL;
718e3744 847
848 /* Get community list. */
fee6e4e4 849 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
718e3744 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. */
8708b74f 854 if (!community_list_empty_p (list))
718e3744 855 {
856 struct community_entry *first;
857
858 first = list->head;
859
fee6e4e4 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 }
718e3744 866 }
867
fee6e4e4 868 if (str)
718e3744 869 {
fee6e4e4 870 if (style == COMMUNITY_LIST_STANDARD)
871 com = community_str2com (str);
718e3744 872 else
fee6e4e4 873 regex = bgp_regcomp (str);
8708b74f 874
fee6e4e4 875 if (! com && ! regex)
876 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
718e3744 877 }
878
fee6e4e4 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
718e3744 887 /* Do not put duplicated community entry. */
888 if (community_list_dup_check (list, entry))
889 community_entry_free (entry);
890 else
518f0eb1
DS
891 {
892 community_list_entry_add (list, entry);
893 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
894 }
718e3744 895
896 return 0;
897}
898
813d4307 899/* Unset community-list */
718e3744 900int
901community_list_unset (struct community_list_handler *ch,
fd79ac91 902 const char *name, const char *str,
813d4307 903 int direct, int style, int delete_all)
718e3744 904{
fee6e4e4 905 struct community_entry *entry = NULL;
718e3744 906 struct community_list *list;
fee6e4e4 907 struct community *com = NULL;
718e3744 908
909 /* Lookup community list. */
fee6e4e4 910 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
718e3744 911 if (list == NULL)
912 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
913
914 /* Delete all of entry belongs to this community-list. */
813d4307 915 if (delete_all)
718e3744 916 {
917 community_list_delete (list);
518f0eb1 918 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 919 return 0;
920 }
921
fee6e4e4 922 if (style == COMMUNITY_LIST_STANDARD)
813d4307
DW
923 {
924 if (str)
925 com = community_str2com (str);
926 }
718e3744 927
fee6e4e4 928 if (com)
813d4307
DW
929 {
930 entry = community_list_entry_lookup (list, com, direct);
931 community_free (com);
932 }
fee6e4e4 933 else
934 entry = community_list_entry_lookup (list, str, direct);
935
8708b74f 936 if (!entry)
718e3744 937 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
938
939 community_list_entry_delete (list, entry, style);
518f0eb1 940 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 941
942 return 0;
943}
944
57d187bc
JS
945/* Delete all permitted large communities in the list from com. */
946struct lcommunity *
947lcommunity_list_match_delete (struct lcommunity *lcom,
2acb4ac2 948 struct community_list *list)
57d187bc
JS
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. */
1012int
1013lcommunity_list_set (struct community_list_handler *ch,
2acb4ac2 1014 const char *name, const char *str, int direct, int style)
57d187bc
JS
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)
2acb4ac2
DL
1034 {
1035 return (first->style == COMMUNITY_LIST_STANDARD
1036 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1037 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1038 }
57d187bc
JS
1039 }
1040
1041 if (str)
1042 {
1043 if (style == LARGE_COMMUNITY_LIST_STANDARD)
2acb4ac2 1044 lcom = lcommunity_str2com (str);
57d187bc 1045 else
2acb4ac2 1046 regex = bgp_regcomp (str);
57d187bc
JS
1047
1048 if (! lcom && ! regex)
2acb4ac2 1049 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
57d187bc
JS
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. */
1076int
1077lcommunity_list_unset (struct community_list_handler *ch,
2acb4ac2
DL
1078 const char *name, const char *str,
1079 int direct, int style)
57d187bc
JS
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
718e3744 1124/* Set extcommunity-list. */
1125int
1126extcommunity_list_set (struct community_list_handler *ch,
fd79ac91 1127 const char *name, const char *str,
1128 int direct, int style)
718e3744 1129{
fee6e4e4 1130 struct community_entry *entry = NULL;
718e3744 1131 struct community_list *list;
fee6e4e4 1132 struct ecommunity *ecom = NULL;
1133 regex_t *regex = NULL;
718e3744 1134
1135 entry = NULL;
1136
1137 /* Get community list. */
fee6e4e4 1138 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 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. */
8708b74f 1143 if (!community_list_empty_p (list))
718e3744 1144 {
1145 struct community_entry *first;
1146
1147 first = list->head;
1148
fee6e4e4 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 }
718e3744 1155 }
1156
fee6e4e4 1157 if (str)
718e3744 1158 {
fee6e4e4 1159 if (style == EXTCOMMUNITY_LIST_STANDARD)
1160 ecom = ecommunity_str2com (str, 0, 1);
718e3744 1161 else
fee6e4e4 1162 regex = bgp_regcomp (str);
8708b74f 1163
fee6e4e4 1164 if (! ecom && ! regex)
1165 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
718e3744 1166 }
1167
fee6e4e4 1168 if (ecom)
1169 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
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);
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
718e3744 1184 /* Do not put duplicated community entry. */
1185 if (community_list_dup_check (list, entry))
1186 community_entry_free (entry);
1187 else
518f0eb1
DS
1188 {
1189 community_list_entry_add (list, entry);
1190 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1191 }
718e3744 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. */
1198int
1199extcommunity_list_unset (struct community_list_handler *ch,
fd79ac91 1200 const char *name, const char *str,
813d4307 1201 int direct, int style, int delete_all)
718e3744 1202{
fee6e4e4 1203 struct community_entry *entry = NULL;
718e3744 1204 struct community_list *list;
1205 struct ecommunity *ecom = NULL;
718e3744 1206
1207 /* Lookup extcommunity list. */
fee6e4e4 1208 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 1209 if (list == NULL)
1210 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1211
1212 /* Delete all of entry belongs to this extcommunity-list. */
813d4307 1213 if (delete_all)
718e3744 1214 {
1215 community_list_delete (list);
518f0eb1 1216 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 1217 return 0;
1218 }
1219
fee6e4e4 1220 if (style == EXTCOMMUNITY_LIST_STANDARD)
813d4307
DW
1221 {
1222 if (str)
1223 ecom = ecommunity_str2com (str, 0, 1);
1224 }
718e3744 1225
fee6e4e4 1226 if (ecom)
813d4307
DW
1227 {
1228 entry = community_list_entry_lookup (list, ecom, direct);
1229 ecommunity_free (&ecom);
1230 }
fee6e4e4 1231 else
1232 entry = community_list_entry_lookup (list, str, direct);
1233
8708b74f 1234 if (!entry)
718e3744 1235 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1236
1237 community_list_entry_delete (list, entry, style);
518f0eb1 1238 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 1239
1240 return 0;
1241}
1242
1243/* Initializa community-list. Return community-list handler. */
1244struct community_list_handler *
94f2b392 1245community_list_init (void)
718e3744 1246{
1247 struct community_list_handler *ch;
1248 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
8708b74f 1249 sizeof (struct community_list_handler));
718e3744 1250 return ch;
1251}
1252
1253/* Terminate community-list. */
228da428 1254void
718e3744 1255community_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
57d187bc
JS
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
718e3744 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}