blob: db2cfa48de959523ed040baa30966136effad960 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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"
26
27#include "bgpd/bgpd.h"
28#include "bgpd/bgp_community.h"
29#include "bgpd/bgp_ecommunity.h"
30#include "bgpd/bgp_aspath.h"
31#include "bgpd/bgp_regex.h"
32#include "bgpd/bgp_clist.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020033
paul718e3742002-12-13 20:15:29 +000034/* Lookup master structure for community-list or
35 extcommunity-list. */
36struct community_list_master *
hassofee6e4e2005-02-02 16:29:31 +000037community_list_master_lookup (struct community_list_handler *ch, int master)
paul718e3742002-12-13 20:15:29 +000038{
39 if (ch)
hassofee6e4e2005-02-02 16:29:31 +000040 switch (master)
paul718e3742002-12-13 20:15:29 +000041 {
hassofee6e4e2005-02-02 16:29:31 +000042 case COMMUNITY_LIST_MASTER:
43 return &ch->community_list;
hassofee6e4e2005-02-02 16:29:31 +000044 case EXTCOMMUNITY_LIST_MASTER:
45 return &ch->extcommunity_list;
paul718e3742002-12-13 20:15:29 +000046 }
47 return NULL;
48}
49
50/* Allocate a new community list entry. */
paul94f2b392005-06-28 12:44:16 +000051static struct community_entry *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080052community_entry_new (void)
paul718e3742002-12-13 20:15:29 +000053{
Stephen Hemminger393deb92008-08-18 14:13:29 -070054 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
paul718e3742002-12-13 20:15:29 +000055}
56
57/* Free community list entry. */
paul94f2b392005-06-28 12:44:16 +000058static void
paul718e3742002-12-13 20:15:29 +000059community_entry_free (struct community_entry *entry)
60{
61 switch (entry->style)
62 {
63 case COMMUNITY_LIST_STANDARD:
64 if (entry->u.com)
paul8708b742003-06-07 02:03:11 +000065 community_free (entry->u.com);
paul718e3742002-12-13 20:15:29 +000066 break;
67 case EXTCOMMUNITY_LIST_STANDARD:
68 /* In case of standard extcommunity-list, configuration string
paul8708b742003-06-07 02:03:11 +000069 is made by ecommunity_ecom2str(). */
paul718e3742002-12-13 20:15:29 +000070 if (entry->config)
paul8708b742003-06-07 02:03:11 +000071 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
paul718e3742002-12-13 20:15:29 +000072 if (entry->u.ecom)
Paul Jakmaf6f434b2010-11-23 21:28:03 +000073 ecommunity_free (&entry->u.ecom);
paul718e3742002-12-13 20:15:29 +000074 break;
75 case COMMUNITY_LIST_EXPANDED:
76 case EXTCOMMUNITY_LIST_EXPANDED:
77 if (entry->config)
paul8708b742003-06-07 02:03:11 +000078 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
paul718e3742002-12-13 20:15:29 +000079 if (entry->reg)
paul8708b742003-06-07 02:03:11 +000080 bgp_regex_free (entry->reg);
paul718e3742002-12-13 20:15:29 +000081 default:
82 break;
83 }
84 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
85}
86
87/* Allocate a new community-list. */
paul94f2b392005-06-28 12:44:16 +000088static struct community_list *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080089community_list_new (void)
paul718e3742002-12-13 20:15:29 +000090{
Stephen Hemminger393deb92008-08-18 14:13:29 -070091 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
paul718e3742002-12-13 20:15:29 +000092}
93
94/* Free community-list. */
paul94f2b392005-06-28 12:44:16 +000095static void
paul718e3742002-12-13 20:15:29 +000096community_list_free (struct community_list *list)
97{
98 if (list->name)
99 XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name);
100 XFREE (MTYPE_COMMUNITY_LIST, list);
101}
102
paul94f2b392005-06-28 12:44:16 +0000103static struct community_list *
paul718e3742002-12-13 20:15:29 +0000104community_list_insert (struct community_list_handler *ch,
hassofee6e4e2005-02-02 16:29:31 +0000105 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000106{
paulfd79ac92004-10-13 05:06:08 +0000107 size_t i;
paul718e3742002-12-13 20:15:29 +0000108 long number;
109 struct community_list *new;
110 struct community_list *point;
111 struct community_list_list *list;
112 struct community_list_master *cm;
113
114 /* Lookup community-list master. */
hassofee6e4e2005-02-02 16:29:31 +0000115 cm = community_list_master_lookup (ch, master);
paul8708b742003-06-07 02:03:11 +0000116 if (!cm)
paul718e3742002-12-13 20:15:29 +0000117 return NULL;
118
119 /* Allocate new community_list and copy given name. */
120 new = community_list_new ();
121 new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name);
122
123 /* If name is made by all digit character. We treat it as
124 number. */
125 for (number = 0, i = 0; i < strlen (name); i++)
126 {
127 if (isdigit ((int) name[i]))
paul8708b742003-06-07 02:03:11 +0000128 number = (number * 10) + (name[i] - '0');
paul718e3742002-12-13 20:15:29 +0000129 else
paul8708b742003-06-07 02:03:11 +0000130 break;
paul718e3742002-12-13 20:15:29 +0000131 }
132
133 /* In case of name is all digit character */
134 if (i == strlen (name))
135 {
136 new->sort = COMMUNITY_LIST_NUMBER;
137
138 /* Set access_list to number list. */
139 list = &cm->num;
140
141 for (point = list->head; point; point = point->next)
paul8708b742003-06-07 02:03:11 +0000142 if (atol (point->name) >= number)
143 break;
paul718e3742002-12-13 20:15:29 +0000144 }
145 else
146 {
147 new->sort = COMMUNITY_LIST_STRING;
148
149 /* Set access_list to string list. */
150 list = &cm->str;
paul8708b742003-06-07 02:03:11 +0000151
paul718e3742002-12-13 20:15:29 +0000152 /* Set point to insertion point. */
153 for (point = list->head; point; point = point->next)
paul8708b742003-06-07 02:03:11 +0000154 if (strcmp (point->name, name) >= 0)
155 break;
paul718e3742002-12-13 20:15:29 +0000156 }
157
158 /* Link to upper list. */
159 new->parent = list;
160
161 /* In case of this is the first element of master. */
162 if (list->head == NULL)
163 {
164 list->head = list->tail = new;
165 return new;
166 }
167
168 /* In case of insertion is made at the tail of access_list. */
169 if (point == NULL)
170 {
171 new->prev = list->tail;
172 list->tail->next = new;
173 list->tail = new;
174 return new;
175 }
176
177 /* In case of insertion is made at the head of access_list. */
178 if (point == list->head)
179 {
180 new->next = list->head;
181 list->head->prev = new;
182 list->head = new;
183 return new;
184 }
185
186 /* Insertion is made at middle of the access_list. */
187 new->next = point;
188 new->prev = point->prev;
189
190 if (point->prev)
191 point->prev->next = new;
192 point->prev = new;
193
194 return new;
195}
196
197struct community_list *
198community_list_lookup (struct community_list_handler *ch,
hassofee6e4e2005-02-02 16:29:31 +0000199 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000200{
201 struct community_list *list;
202 struct community_list_master *cm;
203
paul8708b742003-06-07 02:03:11 +0000204 if (!name)
paul718e3742002-12-13 20:15:29 +0000205 return NULL;
206
hassofee6e4e2005-02-02 16:29:31 +0000207 cm = community_list_master_lookup (ch, master);
paul8708b742003-06-07 02:03:11 +0000208 if (!cm)
paul718e3742002-12-13 20:15:29 +0000209 return NULL;
210
211 for (list = cm->num.head; list; list = list->next)
212 if (strcmp (list->name, name) == 0)
213 return list;
214 for (list = cm->str.head; list; list = list->next)
215 if (strcmp (list->name, name) == 0)
216 return list;
217
218 return NULL;
219}
220
paul94f2b392005-06-28 12:44:16 +0000221static struct community_list *
hassofee6e4e2005-02-02 16:29:31 +0000222community_list_get (struct community_list_handler *ch,
223 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000224{
225 struct community_list *list;
226
hassofee6e4e2005-02-02 16:29:31 +0000227 list = community_list_lookup (ch, name, master);
paul8708b742003-06-07 02:03:11 +0000228 if (!list)
hassofee6e4e2005-02-02 16:29:31 +0000229 list = community_list_insert (ch, name, master);
paul718e3742002-12-13 20:15:29 +0000230 return list;
231}
232
paul94f2b392005-06-28 12:44:16 +0000233static void
paul718e3742002-12-13 20:15:29 +0000234community_list_delete (struct community_list *list)
235{
236 struct community_list_list *clist;
237 struct community_entry *entry, *next;
238
239 for (entry = list->head; entry; entry = next)
240 {
241 next = entry->next;
242 community_entry_free (entry);
243 }
244
245 clist = list->parent;
246
247 if (list->next)
248 list->next->prev = list->prev;
249 else
250 clist->tail = list->prev;
251
252 if (list->prev)
253 list->prev->next = list->next;
254 else
255 clist->head = list->next;
256
257 community_list_free (list);
258}
259
paul94f2b392005-06-28 12:44:16 +0000260static int
paul718e3742002-12-13 20:15:29 +0000261community_list_empty_p (struct community_list *list)
262{
263 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
264}
David Lamparter6b0655a2014-06-04 06:53:35 +0200265
paul718e3742002-12-13 20:15:29 +0000266/* Add community-list entry to the list. */
267static void
paul8708b742003-06-07 02:03:11 +0000268community_list_entry_add (struct community_list *list,
269 struct community_entry *entry)
paul718e3742002-12-13 20:15:29 +0000270{
271 entry->next = NULL;
272 entry->prev = list->tail;
273
274 if (list->tail)
275 list->tail->next = entry;
276 else
277 list->head = entry;
278 list->tail = entry;
279}
280
281/* Delete community-list entry from the list. */
282static void
283community_list_entry_delete (struct community_list *list,
paul8708b742003-06-07 02:03:11 +0000284 struct community_entry *entry, int style)
paul718e3742002-12-13 20:15:29 +0000285{
286 if (entry->next)
287 entry->next->prev = entry->prev;
288 else
289 list->tail = entry->prev;
290
291 if (entry->prev)
292 entry->prev->next = entry->next;
293 else
294 list->head = entry->next;
295
296 community_entry_free (entry);
297
298 if (community_list_empty_p (list))
299 community_list_delete (list);
300}
301
302/* Lookup community-list entry from the list. */
303static struct community_entry *
paulfd79ac92004-10-13 05:06:08 +0000304community_list_entry_lookup (struct community_list *list, const void *arg,
paul8708b742003-06-07 02:03:11 +0000305 int direct)
paul718e3742002-12-13 20:15:29 +0000306{
307 struct community_entry *entry;
308
309 for (entry = list->head; entry; entry = entry->next)
310 {
311 switch (entry->style)
paul8708b742003-06-07 02:03:11 +0000312 {
313 case COMMUNITY_LIST_STANDARD:
314 if (community_cmp (entry->u.com, arg))
315 return entry;
316 break;
317 case EXTCOMMUNITY_LIST_STANDARD:
318 if (ecommunity_cmp (entry->u.ecom, arg))
319 return entry;
320 break;
321 case COMMUNITY_LIST_EXPANDED:
322 case EXTCOMMUNITY_LIST_EXPANDED:
323 if (strcmp (entry->config, arg) == 0)
324 return entry;
325 break;
326 default:
327 break;
328 }
paul718e3742002-12-13 20:15:29 +0000329 }
330 return NULL;
331}
David Lamparter6b0655a2014-06-04 06:53:35 +0200332
Daniel Waltond3ac7332015-08-24 10:19:10 -0400333static char *
334community_str_get (struct community *com, int i)
335{
336 int len;
337 u_int32_t comval;
338 u_int16_t as;
339 u_int16_t val;
340 char *str;
341 char *pnt;
342
343 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
344 comval = ntohl (comval);
345
346 switch (comval)
347 {
348 case COMMUNITY_INTERNET:
349 len = strlen (" internet");
350 break;
351 case COMMUNITY_NO_EXPORT:
352 len = strlen (" no-export");
353 break;
354 case COMMUNITY_NO_ADVERTISE:
355 len = strlen (" no-advertise");
356 break;
357 case COMMUNITY_LOCAL_AS:
358 len = strlen (" local-AS");
359 break;
360 default:
361 len = strlen (" 65536:65535");
362 break;
363 }
364
365 /* Allocate memory. */
366 str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
367
368 switch (comval)
369 {
370 case COMMUNITY_INTERNET:
371 strcpy (pnt, "internet");
372 pnt += strlen ("internet");
373 break;
374 case COMMUNITY_NO_EXPORT:
375 strcpy (pnt, "no-export");
376 pnt += strlen ("no-export");
377 break;
378 case COMMUNITY_NO_ADVERTISE:
379 strcpy (pnt, "no-advertise");
380 pnt += strlen ("no-advertise");
381 break;
382 case COMMUNITY_LOCAL_AS:
383 strcpy (pnt, "local-AS");
384 pnt += strlen ("local-AS");
385 break;
386 default:
387 as = (comval >> 16) & 0xFFFF;
388 val = comval & 0xFFFF;
389 sprintf (pnt, "%u:%d", as, val);
390 pnt += strlen (pnt);
391 break;
392 }
393
394 *pnt = '\0';
395
396 return str;
397}
398
399/* Internal function to perform regular expression match for
400 * * a single community. */
401static int
402community_regexp_include (regex_t * reg, struct community *com, int i)
403{
404 const char *str;
405
406 /* When there is no communities attribute it is treated as empty
407 * string. */
408 if (com == NULL || com->size == 0)
409 str = "";
410 else
411 str = community_str_get (com, i);
412
413 /* Regular expression match. */
414 if (regexec (reg, str, 0, NULL, 0) == 0)
415 return 1;
416
417 /* No match. */
418 return 0;
419}
420
paul718e3742002-12-13 20:15:29 +0000421/* Internal function to perform regular expression match for community
422 attribute. */
423static int
paul8708b742003-06-07 02:03:11 +0000424community_regexp_match (struct community *com, regex_t * reg)
paul718e3742002-12-13 20:15:29 +0000425{
paulfd79ac92004-10-13 05:06:08 +0000426 const char *str;
paul718e3742002-12-13 20:15:29 +0000427
428 /* When there is no communities attribute it is treated as empty
429 string. */
430 if (com == NULL || com->size == 0)
431 str = "";
432 else
433 str = community_str (com);
434
435 /* Regular expression match. */
436 if (regexec (reg, str, 0, NULL, 0) == 0)
437 return 1;
438
439 /* No match. */
440 return 0;
441}
442
paul8708b742003-06-07 02:03:11 +0000443static int
444ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
445{
paulfd79ac92004-10-13 05:06:08 +0000446 const char *str;
paul8708b742003-06-07 02:03:11 +0000447
448 /* When there is no communities attribute it is treated as empty
449 string. */
450 if (ecom == NULL || ecom->size == 0)
451 str = "";
452 else
453 str = ecommunity_str (ecom);
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
paul718e3742002-12-13 20:15:29 +0000463/* When given community attribute matches to the community-list return
464 1 else return 0. */
465int
466community_list_match (struct community *com, struct community_list *list)
467{
468 struct community_entry *entry;
469
470 for (entry = list->head; entry; entry = entry->next)
471 {
472 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000473 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000474
475 if (entry->style == COMMUNITY_LIST_STANDARD)
paul8708b742003-06-07 02:03:11 +0000476 {
477 if (community_include (entry->u.com, COMMUNITY_INTERNET))
478 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000479
paul8708b742003-06-07 02:03:11 +0000480 if (community_match (com, entry->u.com))
481 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
482 }
paul718e3742002-12-13 20:15:29 +0000483 else if (entry->style == COMMUNITY_LIST_EXPANDED)
paul8708b742003-06-07 02:03:11 +0000484 {
485 if (community_regexp_match (com, entry->reg))
486 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
487 }
488 }
489 return 0;
490}
491
492int
493ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
494{
495 struct community_entry *entry;
496
497 for (entry = list->head; entry; entry = entry->next)
498 {
499 if (entry->any)
500 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
501
502 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
503 {
504 if (ecommunity_match (ecom, entry->u.ecom))
505 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
506 }
507 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
508 {
509 if (ecommunity_regexp_match (ecom, entry->reg))
510 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
511 }
paul718e3742002-12-13 20:15:29 +0000512 }
513 return 0;
514}
515
516/* Perform exact matching. In case of expanded community-list, do
517 same thing as community_list_match(). */
518int
paul8708b742003-06-07 02:03:11 +0000519community_list_exact_match (struct community *com,
520 struct community_list *list)
paul718e3742002-12-13 20:15:29 +0000521{
522 struct community_entry *entry;
523
524 for (entry = list->head; entry; entry = entry->next)
525 {
526 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000527 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000528
529 if (entry->style == COMMUNITY_LIST_STANDARD)
paul8708b742003-06-07 02:03:11 +0000530 {
531 if (community_include (entry->u.com, COMMUNITY_INTERNET))
532 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000533
paul8708b742003-06-07 02:03:11 +0000534 if (community_cmp (com, entry->u.com))
535 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
536 }
paul718e3742002-12-13 20:15:29 +0000537 else if (entry->style == COMMUNITY_LIST_EXPANDED)
paul8708b742003-06-07 02:03:11 +0000538 {
539 if (community_regexp_match (com, entry->reg))
540 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
541 }
paul718e3742002-12-13 20:15:29 +0000542 }
543 return 0;
544}
545
paul8708b742003-06-07 02:03:11 +0000546/* Delete all permitted communities in the list from com. */
paul718e3742002-12-13 20:15:29 +0000547struct community *
548community_list_match_delete (struct community *com,
paul8708b742003-06-07 02:03:11 +0000549 struct community_list *list)
paul718e3742002-12-13 20:15:29 +0000550{
551 struct community_entry *entry;
Daniel Waltond3ac7332015-08-24 10:19:10 -0400552 u_int32_t val;
553 u_int32_t com_index_to_delete[com->size];
554 int delete_index = 0;
555 int i;
paul718e3742002-12-13 20:15:29 +0000556
Daniel Waltond3ac7332015-08-24 10:19:10 -0400557 /* Loop over each community value and evaluate each against the
558 * community-list. If we need to delete a community value add its index to
559 * com_index_to_delete.
560 */
561 for (i = 0; i < com->size; i++)
paul718e3742002-12-13 20:15:29 +0000562 {
Daniel Waltond3ac7332015-08-24 10:19:10 -0400563 val = community_val_get (com, i);
paul718e3742002-12-13 20:15:29 +0000564
Daniel Waltond3ac7332015-08-24 10:19:10 -0400565 for (entry = list->head; entry; entry = entry->next)
paul8708b742003-06-07 02:03:11 +0000566 {
Daniel Waltond3ac7332015-08-24 10:19:10 -0400567 if (entry->any)
568 {
paul847375b2003-06-09 18:48:31 +0000569 if (entry->direct == COMMUNITY_PERMIT)
Daniel Waltond3ac7332015-08-24 10:19:10 -0400570 {
571 com_index_to_delete[delete_index] = i;
572 delete_index++;
573 }
574 break;
575 }
576
577 else if ((entry->style == COMMUNITY_LIST_STANDARD)
578 && (community_include (entry->u.com, COMMUNITY_INTERNET)
579 || community_include (entry->u.com, val) ))
580 {
581 if (entry->direct == COMMUNITY_PERMIT)
582 {
583 com_index_to_delete[delete_index] = i;
584 delete_index++;
585 }
586 break;
587 }
588
589 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
590 && community_regexp_include (entry->reg, com, i))
591 {
592 if (entry->direct == COMMUNITY_PERMIT)
593 {
594 com_index_to_delete[delete_index] = i;
595 delete_index++;
596 }
597 break;
598 }
599 }
600 }
601
602 /* Delete all of the communities we flagged for deletion */
603 for (i = delete_index-1; i >= 0; i--)
604 {
605 val = community_val_get (com, com_index_to_delete[i]);
606 community_del_val (com, &val);
paul718e3742002-12-13 20:15:29 +0000607 }
Daniel Waltond3ac7332015-08-24 10:19:10 -0400608
paul718e3742002-12-13 20:15:29 +0000609 return com;
610}
611
612/* To avoid duplicated entry in the community-list, this function
613 compares specified entry to existing entry. */
paul94f2b392005-06-28 12:44:16 +0000614static int
paul8708b742003-06-07 02:03:11 +0000615community_list_dup_check (struct community_list *list,
616 struct community_entry *new)
paul718e3742002-12-13 20:15:29 +0000617{
618 struct community_entry *entry;
paul8708b742003-06-07 02:03:11 +0000619
paul718e3742002-12-13 20:15:29 +0000620 for (entry = list->head; entry; entry = entry->next)
621 {
622 if (entry->style != new->style)
paul8708b742003-06-07 02:03:11 +0000623 continue;
paul718e3742002-12-13 20:15:29 +0000624
625 if (entry->direct != new->direct)
paul8708b742003-06-07 02:03:11 +0000626 continue;
paul718e3742002-12-13 20:15:29 +0000627
628 if (entry->any != new->any)
paul8708b742003-06-07 02:03:11 +0000629 continue;
paul718e3742002-12-13 20:15:29 +0000630
631 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000632 return 1;
paul718e3742002-12-13 20:15:29 +0000633
634 switch (entry->style)
paul8708b742003-06-07 02:03:11 +0000635 {
636 case COMMUNITY_LIST_STANDARD:
637 if (community_cmp (entry->u.com, new->u.com))
638 return 1;
639 break;
640 case EXTCOMMUNITY_LIST_STANDARD:
641 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
642 return 1;
643 break;
644 case COMMUNITY_LIST_EXPANDED:
645 case EXTCOMMUNITY_LIST_EXPANDED:
646 if (strcmp (entry->config, new->config) == 0)
647 return 1;
648 break;
649 default:
650 break;
651 }
paul718e3742002-12-13 20:15:29 +0000652 }
653 return 0;
654}
David Lamparter6b0655a2014-06-04 06:53:35 +0200655
paul718e3742002-12-13 20:15:29 +0000656/* Set community-list. */
657int
658community_list_set (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000659 const char *name, const char *str, int direct, int style)
paul718e3742002-12-13 20:15:29 +0000660{
hassofee6e4e2005-02-02 16:29:31 +0000661 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000662 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000663 struct community *com = NULL;
664 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000665
666 /* Get community list. */
hassofee6e4e2005-02-02 16:29:31 +0000667 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000668
669 /* When community-list already has entry, new entry should have same
670 style. If you want to have mixed style community-list, you can
671 comment out this check. */
paul8708b742003-06-07 02:03:11 +0000672 if (!community_list_empty_p (list))
paul718e3742002-12-13 20:15:29 +0000673 {
674 struct community_entry *first;
675
676 first = list->head;
677
hassofee6e4e2005-02-02 16:29:31 +0000678 if (style != first->style)
679 {
680 return (first->style == COMMUNITY_LIST_STANDARD
681 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
682 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
683 }
paul718e3742002-12-13 20:15:29 +0000684 }
685
hassofee6e4e2005-02-02 16:29:31 +0000686 if (str)
paul718e3742002-12-13 20:15:29 +0000687 {
hassofee6e4e2005-02-02 16:29:31 +0000688 if (style == COMMUNITY_LIST_STANDARD)
689 com = community_str2com (str);
paul718e3742002-12-13 20:15:29 +0000690 else
hassofee6e4e2005-02-02 16:29:31 +0000691 regex = bgp_regcomp (str);
paul8708b742003-06-07 02:03:11 +0000692
hassofee6e4e2005-02-02 16:29:31 +0000693 if (! com && ! regex)
694 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000695 }
696
hassofee6e4e2005-02-02 16:29:31 +0000697 entry = community_entry_new ();
698 entry->direct = direct;
699 entry->style = style;
700 entry->any = (str ? 0 : 1);
701 entry->u.com = com;
702 entry->reg = regex;
703 entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
704
paul718e3742002-12-13 20:15:29 +0000705 /* Do not put duplicated community entry. */
706 if (community_list_dup_check (list, entry))
707 community_entry_free (entry);
708 else
709 community_list_entry_add (list, entry);
710
711 return 0;
712}
713
714/* Unset community-list. When str is NULL, delete all of
715 community-list entry belongs to the specified name. */
716int
717community_list_unset (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000718 const char *name, const char *str,
719 int direct, int style)
paul718e3742002-12-13 20:15:29 +0000720{
hassofee6e4e2005-02-02 16:29:31 +0000721 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000722 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000723 struct community *com = NULL;
724 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000725
726 /* Lookup community list. */
hassofee6e4e2005-02-02 16:29:31 +0000727 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000728 if (list == NULL)
729 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
730
731 /* Delete all of entry belongs to this community-list. */
paul8708b742003-06-07 02:03:11 +0000732 if (!str)
paul718e3742002-12-13 20:15:29 +0000733 {
734 community_list_delete (list);
735 return 0;
736 }
737
hassofee6e4e2005-02-02 16:29:31 +0000738 if (style == COMMUNITY_LIST_STANDARD)
739 com = community_str2com (str);
740 else
741 regex = bgp_regcomp (str);
paul718e3742002-12-13 20:15:29 +0000742
hassofee6e4e2005-02-02 16:29:31 +0000743 if (! com && ! regex)
744 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000745
hassofee6e4e2005-02-02 16:29:31 +0000746 if (com)
747 entry = community_list_entry_lookup (list, com, direct);
748 else
749 entry = community_list_entry_lookup (list, str, direct);
750
751 if (com)
752 community_free (com);
753 if (regex)
754 bgp_regex_free (regex);
paul718e3742002-12-13 20:15:29 +0000755
paul8708b742003-06-07 02:03:11 +0000756 if (!entry)
paul718e3742002-12-13 20:15:29 +0000757 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
758
759 community_list_entry_delete (list, entry, style);
760
761 return 0;
762}
763
764/* Set extcommunity-list. */
765int
766extcommunity_list_set (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000767 const char *name, const char *str,
768 int direct, int style)
paul718e3742002-12-13 20:15:29 +0000769{
hassofee6e4e2005-02-02 16:29:31 +0000770 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000771 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000772 struct ecommunity *ecom = NULL;
773 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000774
775 entry = NULL;
776
777 /* Get community list. */
hassofee6e4e2005-02-02 16:29:31 +0000778 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000779
780 /* When community-list already has entry, new entry should have same
781 style. If you want to have mixed style community-list, you can
782 comment out this check. */
paul8708b742003-06-07 02:03:11 +0000783 if (!community_list_empty_p (list))
paul718e3742002-12-13 20:15:29 +0000784 {
785 struct community_entry *first;
786
787 first = list->head;
788
hassofee6e4e2005-02-02 16:29:31 +0000789 if (style != first->style)
790 {
791 return (first->style == EXTCOMMUNITY_LIST_STANDARD
792 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
793 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
794 }
paul718e3742002-12-13 20:15:29 +0000795 }
796
hassofee6e4e2005-02-02 16:29:31 +0000797 if (str)
paul718e3742002-12-13 20:15:29 +0000798 {
hassofee6e4e2005-02-02 16:29:31 +0000799 if (style == EXTCOMMUNITY_LIST_STANDARD)
800 ecom = ecommunity_str2com (str, 0, 1);
paul718e3742002-12-13 20:15:29 +0000801 else
hassofee6e4e2005-02-02 16:29:31 +0000802 regex = bgp_regcomp (str);
803
804 if (! ecom && ! regex)
805 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000806 }
hassofee6e4e2005-02-02 16:29:31 +0000807
808 if (ecom)
809 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
810
811 entry = community_entry_new ();
812 entry->direct = direct;
813 entry->style = style;
814 entry->any = (str ? 0 : 1);
815 if (ecom)
816 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
817 else if (regex)
818 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
paul718e3742002-12-13 20:15:29 +0000819 else
hassofee6e4e2005-02-02 16:29:31 +0000820 entry->config = NULL;
821 entry->u.ecom = ecom;
822 entry->reg = regex;
paul718e3742002-12-13 20:15:29 +0000823
824 /* Do not put duplicated community entry. */
825 if (community_list_dup_check (list, entry))
826 community_entry_free (entry);
827 else
828 community_list_entry_add (list, entry);
829
830 return 0;
831}
832
833/* Unset extcommunity-list. When str is NULL, delete all of
834 extcommunity-list entry belongs to the specified name. */
835int
836extcommunity_list_unset (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000837 const char *name, const char *str,
838 int direct, int style)
paul718e3742002-12-13 20:15:29 +0000839{
hassofee6e4e2005-02-02 16:29:31 +0000840 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000841 struct community_list *list;
842 struct ecommunity *ecom = NULL;
hassofee6e4e2005-02-02 16:29:31 +0000843 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000844
845 /* Lookup extcommunity list. */
hassofee6e4e2005-02-02 16:29:31 +0000846 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000847 if (list == NULL)
848 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
849
850 /* Delete all of entry belongs to this extcommunity-list. */
paul8708b742003-06-07 02:03:11 +0000851 if (!str)
paul718e3742002-12-13 20:15:29 +0000852 {
853 community_list_delete (list);
854 return 0;
855 }
856
hassofee6e4e2005-02-02 16:29:31 +0000857 if (style == EXTCOMMUNITY_LIST_STANDARD)
858 ecom = ecommunity_str2com (str, 0, 1);
859 else
860 regex = bgp_regcomp (str);
paul718e3742002-12-13 20:15:29 +0000861
hassofee6e4e2005-02-02 16:29:31 +0000862 if (! ecom && ! regex)
863 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000864
hassofee6e4e2005-02-02 16:29:31 +0000865 if (ecom)
866 entry = community_list_entry_lookup (list, ecom, direct);
867 else
868 entry = community_list_entry_lookup (list, str, direct);
869
870 if (ecom)
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000871 ecommunity_free (&ecom);
hassofee6e4e2005-02-02 16:29:31 +0000872 if (regex)
873 bgp_regex_free (regex);
paul718e3742002-12-13 20:15:29 +0000874
paul8708b742003-06-07 02:03:11 +0000875 if (!entry)
paul718e3742002-12-13 20:15:29 +0000876 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
877
878 community_list_entry_delete (list, entry, style);
879
880 return 0;
881}
882
883/* Initializa community-list. Return community-list handler. */
884struct community_list_handler *
paul94f2b392005-06-28 12:44:16 +0000885community_list_init (void)
paul718e3742002-12-13 20:15:29 +0000886{
887 struct community_list_handler *ch;
888 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
paul8708b742003-06-07 02:03:11 +0000889 sizeof (struct community_list_handler));
paul718e3742002-12-13 20:15:29 +0000890 return ch;
891}
892
893/* Terminate community-list. */
Chris Caputo228da422009-07-18 05:44:03 +0000894void
paul718e3742002-12-13 20:15:29 +0000895community_list_terminate (struct community_list_handler *ch)
896{
897 struct community_list_master *cm;
898 struct community_list *list;
899
900 cm = &ch->community_list;
901 while ((list = cm->num.head) != NULL)
902 community_list_delete (list);
903 while ((list = cm->str.head) != NULL)
904 community_list_delete (list);
905
906 cm = &ch->extcommunity_list;
907 while ((list = cm->num.head) != NULL)
908 community_list_delete (list);
909 while ((list = cm->str.head) != NULL)
910 community_list_delete (list);
911
912 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
913}