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