blob: bb06028b0ca776157c09d496787437d0d827d61d [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"
Donald Sharp04907292016-01-07 10:03:01 -050026#include "filter.h"
paul718e3742002-12-13 20:15:29 +000027
28#include "bgpd/bgpd.h"
29#include "bgpd/bgp_community.h"
30#include "bgpd/bgp_ecommunity.h"
31#include "bgpd/bgp_aspath.h"
32#include "bgpd/bgp_regex.h"
33#include "bgpd/bgp_clist.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020034
paul718e3742002-12-13 20:15:29 +000035/* Lookup master structure for community-list or
36 extcommunity-list. */
37struct community_list_master *
hassofee6e4e2005-02-02 16:29:31 +000038community_list_master_lookup (struct community_list_handler *ch, int master)
paul718e3742002-12-13 20:15:29 +000039{
40 if (ch)
hassofee6e4e2005-02-02 16:29:31 +000041 switch (master)
paul718e3742002-12-13 20:15:29 +000042 {
hassofee6e4e2005-02-02 16:29:31 +000043 case COMMUNITY_LIST_MASTER:
44 return &ch->community_list;
hassofee6e4e2005-02-02 16:29:31 +000045 case EXTCOMMUNITY_LIST_MASTER:
46 return &ch->extcommunity_list;
paul718e3742002-12-13 20:15:29 +000047 }
48 return NULL;
49}
50
51/* Allocate a new community list entry. */
paul94f2b392005-06-28 12:44:16 +000052static struct community_entry *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080053community_entry_new (void)
paul718e3742002-12-13 20:15:29 +000054{
Stephen Hemminger393deb92008-08-18 14:13:29 -070055 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
paul718e3742002-12-13 20:15:29 +000056}
57
58/* Free community list entry. */
paul94f2b392005-06-28 12:44:16 +000059static void
paul718e3742002-12-13 20:15:29 +000060community_entry_free (struct community_entry *entry)
61{
62 switch (entry->style)
63 {
64 case COMMUNITY_LIST_STANDARD:
65 if (entry->u.com)
paul8708b742003-06-07 02:03:11 +000066 community_free (entry->u.com);
paul718e3742002-12-13 20:15:29 +000067 break;
68 case EXTCOMMUNITY_LIST_STANDARD:
69 /* In case of standard extcommunity-list, configuration string
paul8708b742003-06-07 02:03:11 +000070 is made by ecommunity_ecom2str(). */
paul718e3742002-12-13 20:15:29 +000071 if (entry->config)
paul8708b742003-06-07 02:03:11 +000072 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
paul718e3742002-12-13 20:15:29 +000073 if (entry->u.ecom)
Paul Jakmaf6f434b2010-11-23 21:28:03 +000074 ecommunity_free (&entry->u.ecom);
paul718e3742002-12-13 20:15:29 +000075 break;
76 case COMMUNITY_LIST_EXPANDED:
77 case EXTCOMMUNITY_LIST_EXPANDED:
78 if (entry->config)
paul8708b742003-06-07 02:03:11 +000079 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
paul718e3742002-12-13 20:15:29 +000080 if (entry->reg)
paul8708b742003-06-07 02:03:11 +000081 bgp_regex_free (entry->reg);
paul718e3742002-12-13 20:15:29 +000082 default:
83 break;
84 }
85 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
86}
87
88/* Allocate a new community-list. */
paul94f2b392005-06-28 12:44:16 +000089static struct community_list *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080090community_list_new (void)
paul718e3742002-12-13 20:15:29 +000091{
Stephen Hemminger393deb92008-08-18 14:13:29 -070092 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
paul718e3742002-12-13 20:15:29 +000093}
94
95/* Free community-list. */
paul94f2b392005-06-28 12:44:16 +000096static void
paul718e3742002-12-13 20:15:29 +000097community_list_free (struct community_list *list)
98{
99 if (list->name)
100 XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name);
101 XFREE (MTYPE_COMMUNITY_LIST, list);
102}
103
paul94f2b392005-06-28 12:44:16 +0000104static struct community_list *
paul718e3742002-12-13 20:15:29 +0000105community_list_insert (struct community_list_handler *ch,
hassofee6e4e2005-02-02 16:29:31 +0000106 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000107{
paulfd79ac92004-10-13 05:06:08 +0000108 size_t i;
paul718e3742002-12-13 20:15:29 +0000109 long number;
110 struct community_list *new;
111 struct community_list *point;
112 struct community_list_list *list;
113 struct community_list_master *cm;
114
115 /* Lookup community-list master. */
hassofee6e4e2005-02-02 16:29:31 +0000116 cm = community_list_master_lookup (ch, master);
paul8708b742003-06-07 02:03:11 +0000117 if (!cm)
paul718e3742002-12-13 20:15:29 +0000118 return NULL;
119
120 /* Allocate new community_list and copy given name. */
121 new = community_list_new ();
122 new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name);
123
124 /* If name is made by all digit character. We treat it as
125 number. */
126 for (number = 0, i = 0; i < strlen (name); i++)
127 {
128 if (isdigit ((int) name[i]))
paul8708b742003-06-07 02:03:11 +0000129 number = (number * 10) + (name[i] - '0');
paul718e3742002-12-13 20:15:29 +0000130 else
paul8708b742003-06-07 02:03:11 +0000131 break;
paul718e3742002-12-13 20:15:29 +0000132 }
133
134 /* In case of name is all digit character */
135 if (i == strlen (name))
136 {
137 new->sort = COMMUNITY_LIST_NUMBER;
138
139 /* Set access_list to number list. */
140 list = &cm->num;
141
142 for (point = list->head; point; point = point->next)
paul8708b742003-06-07 02:03:11 +0000143 if (atol (point->name) >= number)
144 break;
paul718e3742002-12-13 20:15:29 +0000145 }
146 else
147 {
148 new->sort = COMMUNITY_LIST_STRING;
149
150 /* Set access_list to string list. */
151 list = &cm->str;
paul8708b742003-06-07 02:03:11 +0000152
paul718e3742002-12-13 20:15:29 +0000153 /* Set point to insertion point. */
154 for (point = list->head; point; point = point->next)
paul8708b742003-06-07 02:03:11 +0000155 if (strcmp (point->name, name) >= 0)
156 break;
paul718e3742002-12-13 20:15:29 +0000157 }
158
159 /* Link to upper list. */
160 new->parent = list;
161
162 /* In case of this is the first element of master. */
163 if (list->head == NULL)
164 {
165 list->head = list->tail = new;
166 return new;
167 }
168
169 /* In case of insertion is made at the tail of access_list. */
170 if (point == NULL)
171 {
172 new->prev = list->tail;
173 list->tail->next = new;
174 list->tail = new;
175 return new;
176 }
177
178 /* In case of insertion is made at the head of access_list. */
179 if (point == list->head)
180 {
181 new->next = list->head;
182 list->head->prev = new;
183 list->head = new;
184 return new;
185 }
186
187 /* Insertion is made at middle of the access_list. */
188 new->next = point;
189 new->prev = point->prev;
190
191 if (point->prev)
192 point->prev->next = new;
193 point->prev = new;
194
195 return new;
196}
197
198struct community_list *
199community_list_lookup (struct community_list_handler *ch,
hassofee6e4e2005-02-02 16:29:31 +0000200 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000201{
202 struct community_list *list;
203 struct community_list_master *cm;
204
paul8708b742003-06-07 02:03:11 +0000205 if (!name)
paul718e3742002-12-13 20:15:29 +0000206 return NULL;
207
hassofee6e4e2005-02-02 16:29:31 +0000208 cm = community_list_master_lookup (ch, master);
paul8708b742003-06-07 02:03:11 +0000209 if (!cm)
paul718e3742002-12-13 20:15:29 +0000210 return NULL;
211
212 for (list = cm->num.head; list; list = list->next)
213 if (strcmp (list->name, name) == 0)
214 return list;
215 for (list = cm->str.head; list; list = list->next)
216 if (strcmp (list->name, name) == 0)
217 return list;
218
219 return NULL;
220}
221
paul94f2b392005-06-28 12:44:16 +0000222static struct community_list *
hassofee6e4e2005-02-02 16:29:31 +0000223community_list_get (struct community_list_handler *ch,
224 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000225{
226 struct community_list *list;
227
hassofee6e4e2005-02-02 16:29:31 +0000228 list = community_list_lookup (ch, name, master);
paul8708b742003-06-07 02:03:11 +0000229 if (!list)
hassofee6e4e2005-02-02 16:29:31 +0000230 list = community_list_insert (ch, name, master);
paul718e3742002-12-13 20:15:29 +0000231 return list;
232}
233
paul94f2b392005-06-28 12:44:16 +0000234static void
paul718e3742002-12-13 20:15:29 +0000235community_list_delete (struct community_list *list)
236{
237 struct community_list_list *clist;
238 struct community_entry *entry, *next;
239
240 for (entry = list->head; entry; entry = next)
241 {
242 next = entry->next;
243 community_entry_free (entry);
244 }
245
246 clist = list->parent;
247
248 if (list->next)
249 list->next->prev = list->prev;
250 else
251 clist->tail = list->prev;
252
253 if (list->prev)
254 list->prev->next = list->next;
255 else
256 clist->head = list->next;
257
258 community_list_free (list);
259}
260
paul94f2b392005-06-28 12:44:16 +0000261static int
paul718e3742002-12-13 20:15:29 +0000262community_list_empty_p (struct community_list *list)
263{
264 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
265}
David Lamparter6b0655a2014-06-04 06:53:35 +0200266
paul718e3742002-12-13 20:15:29 +0000267/* Add community-list entry to the list. */
268static void
paul8708b742003-06-07 02:03:11 +0000269community_list_entry_add (struct community_list *list,
270 struct community_entry *entry)
paul718e3742002-12-13 20:15:29 +0000271{
272 entry->next = NULL;
273 entry->prev = list->tail;
274
275 if (list->tail)
276 list->tail->next = entry;
277 else
278 list->head = entry;
279 list->tail = entry;
280}
281
282/* Delete community-list entry from the list. */
283static void
284community_list_entry_delete (struct community_list *list,
paul8708b742003-06-07 02:03:11 +0000285 struct community_entry *entry, int style)
paul718e3742002-12-13 20:15:29 +0000286{
287 if (entry->next)
288 entry->next->prev = entry->prev;
289 else
290 list->tail = entry->prev;
291
292 if (entry->prev)
293 entry->prev->next = entry->next;
294 else
295 list->head = entry->next;
296
297 community_entry_free (entry);
298
299 if (community_list_empty_p (list))
300 community_list_delete (list);
301}
302
303/* Lookup community-list entry from the list. */
304static struct community_entry *
paulfd79ac92004-10-13 05:06:08 +0000305community_list_entry_lookup (struct community_list *list, const void *arg,
paul8708b742003-06-07 02:03:11 +0000306 int direct)
paul718e3742002-12-13 20:15:29 +0000307{
308 struct community_entry *entry;
309
310 for (entry = list->head; entry; entry = entry->next)
311 {
312 switch (entry->style)
paul8708b742003-06-07 02:03:11 +0000313 {
314 case COMMUNITY_LIST_STANDARD:
315 if (community_cmp (entry->u.com, arg))
316 return entry;
317 break;
318 case EXTCOMMUNITY_LIST_STANDARD:
319 if (ecommunity_cmp (entry->u.ecom, arg))
320 return entry;
321 break;
322 case COMMUNITY_LIST_EXPANDED:
323 case EXTCOMMUNITY_LIST_EXPANDED:
324 if (strcmp (entry->config, arg) == 0)
325 return entry;
326 break;
327 default:
328 break;
329 }
paul718e3742002-12-13 20:15:29 +0000330 }
331 return NULL;
332}
David Lamparter6b0655a2014-06-04 06:53:35 +0200333
Daniel Waltond3ac7332015-08-24 10:19:10 -0400334static char *
335community_str_get (struct community *com, int i)
336{
337 int len;
338 u_int32_t comval;
339 u_int16_t as;
340 u_int16_t val;
341 char *str;
342 char *pnt;
343
344 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
345 comval = ntohl (comval);
346
347 switch (comval)
348 {
349 case COMMUNITY_INTERNET:
350 len = strlen (" internet");
351 break;
352 case COMMUNITY_NO_EXPORT:
353 len = strlen (" no-export");
354 break;
355 case COMMUNITY_NO_ADVERTISE:
356 len = strlen (" no-advertise");
357 break;
358 case COMMUNITY_LOCAL_AS:
359 len = strlen (" local-AS");
360 break;
361 default:
362 len = strlen (" 65536:65535");
363 break;
364 }
365
366 /* Allocate memory. */
367 str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
368
369 switch (comval)
370 {
371 case COMMUNITY_INTERNET:
372 strcpy (pnt, "internet");
373 pnt += strlen ("internet");
374 break;
375 case COMMUNITY_NO_EXPORT:
376 strcpy (pnt, "no-export");
377 pnt += strlen ("no-export");
378 break;
379 case COMMUNITY_NO_ADVERTISE:
380 strcpy (pnt, "no-advertise");
381 pnt += strlen ("no-advertise");
382 break;
383 case COMMUNITY_LOCAL_AS:
384 strcpy (pnt, "local-AS");
385 pnt += strlen ("local-AS");
386 break;
387 default:
388 as = (comval >> 16) & 0xFFFF;
389 val = comval & 0xFFFF;
390 sprintf (pnt, "%u:%d", as, val);
391 pnt += strlen (pnt);
392 break;
393 }
394
395 *pnt = '\0';
396
397 return str;
398}
399
400/* Internal function to perform regular expression match for
401 * * a single community. */
402static int
403community_regexp_include (regex_t * reg, struct community *com, int i)
404{
405 const char *str;
406
407 /* When there is no communities attribute it is treated as empty
408 * string. */
409 if (com == NULL || com->size == 0)
410 str = "";
411 else
412 str = community_str_get (com, i);
413
414 /* Regular expression match. */
415 if (regexec (reg, str, 0, NULL, 0) == 0)
416 return 1;
417
418 /* No match. */
419 return 0;
420}
421
paul718e3742002-12-13 20:15:29 +0000422/* Internal function to perform regular expression match for community
423 attribute. */
424static int
paul8708b742003-06-07 02:03:11 +0000425community_regexp_match (struct community *com, regex_t * reg)
paul718e3742002-12-13 20:15:29 +0000426{
paulfd79ac92004-10-13 05:06:08 +0000427 const char *str;
paul718e3742002-12-13 20:15:29 +0000428
429 /* When there is no communities attribute it is treated as empty
430 string. */
431 if (com == NULL || com->size == 0)
432 str = "";
433 else
434 str = community_str (com);
435
436 /* Regular expression match. */
437 if (regexec (reg, str, 0, NULL, 0) == 0)
438 return 1;
439
440 /* No match. */
441 return 0;
442}
443
paul8708b742003-06-07 02:03:11 +0000444static int
445ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
446{
paulfd79ac92004-10-13 05:06:08 +0000447 const char *str;
paul8708b742003-06-07 02:03:11 +0000448
449 /* When there is no communities attribute it is treated as empty
450 string. */
451 if (ecom == NULL || ecom->size == 0)
452 str = "";
453 else
454 str = ecommunity_str (ecom);
455
456 /* Regular expression match. */
457 if (regexec (reg, str, 0, NULL, 0) == 0)
458 return 1;
459
460 /* No match. */
461 return 0;
462}
463
paul718e3742002-12-13 20:15:29 +0000464/* When given community attribute matches to the community-list return
465 1 else return 0. */
466int
467community_list_match (struct community *com, struct community_list *list)
468{
469 struct community_entry *entry;
470
471 for (entry = list->head; entry; entry = entry->next)
472 {
473 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000474 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000475
476 if (entry->style == COMMUNITY_LIST_STANDARD)
paul8708b742003-06-07 02:03:11 +0000477 {
478 if (community_include (entry->u.com, COMMUNITY_INTERNET))
479 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000480
paul8708b742003-06-07 02:03:11 +0000481 if (community_match (com, entry->u.com))
482 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
483 }
paul718e3742002-12-13 20:15:29 +0000484 else if (entry->style == COMMUNITY_LIST_EXPANDED)
paul8708b742003-06-07 02:03:11 +0000485 {
486 if (community_regexp_match (com, entry->reg))
487 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
488 }
489 }
490 return 0;
491}
492
493int
494ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
495{
496 struct community_entry *entry;
497
498 for (entry = list->head; entry; entry = entry->next)
499 {
500 if (entry->any)
501 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
502
503 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
504 {
505 if (ecommunity_match (ecom, entry->u.ecom))
506 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
507 }
508 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
509 {
510 if (ecommunity_regexp_match (ecom, entry->reg))
511 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
512 }
paul718e3742002-12-13 20:15:29 +0000513 }
514 return 0;
515}
516
517/* Perform exact matching. In case of expanded community-list, do
518 same thing as community_list_match(). */
519int
paul8708b742003-06-07 02:03:11 +0000520community_list_exact_match (struct community *com,
521 struct community_list *list)
paul718e3742002-12-13 20:15:29 +0000522{
523 struct community_entry *entry;
524
525 for (entry = list->head; entry; entry = entry->next)
526 {
527 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000528 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000529
530 if (entry->style == COMMUNITY_LIST_STANDARD)
paul8708b742003-06-07 02:03:11 +0000531 {
532 if (community_include (entry->u.com, COMMUNITY_INTERNET))
533 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000534
paul8708b742003-06-07 02:03:11 +0000535 if (community_cmp (com, entry->u.com))
536 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
537 }
paul718e3742002-12-13 20:15:29 +0000538 else if (entry->style == COMMUNITY_LIST_EXPANDED)
paul8708b742003-06-07 02:03:11 +0000539 {
540 if (community_regexp_match (com, entry->reg))
541 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
542 }
paul718e3742002-12-13 20:15:29 +0000543 }
544 return 0;
545}
546
paul8708b742003-06-07 02:03:11 +0000547/* Delete all permitted communities in the list from com. */
paul718e3742002-12-13 20:15:29 +0000548struct community *
549community_list_match_delete (struct community *com,
paul8708b742003-06-07 02:03:11 +0000550 struct community_list *list)
paul718e3742002-12-13 20:15:29 +0000551{
552 struct community_entry *entry;
Daniel Waltond3ac7332015-08-24 10:19:10 -0400553 u_int32_t val;
554 u_int32_t com_index_to_delete[com->size];
555 int delete_index = 0;
556 int i;
paul718e3742002-12-13 20:15:29 +0000557
Daniel Waltond3ac7332015-08-24 10:19:10 -0400558 /* Loop over each community value and evaluate each against the
559 * community-list. If we need to delete a community value add its index to
560 * com_index_to_delete.
561 */
562 for (i = 0; i < com->size; i++)
paul718e3742002-12-13 20:15:29 +0000563 {
Daniel Waltond3ac7332015-08-24 10:19:10 -0400564 val = community_val_get (com, i);
paul718e3742002-12-13 20:15:29 +0000565
Daniel Waltond3ac7332015-08-24 10:19:10 -0400566 for (entry = list->head; entry; entry = entry->next)
paul8708b742003-06-07 02:03:11 +0000567 {
Daniel Waltond3ac7332015-08-24 10:19:10 -0400568 if (entry->any)
569 {
paul847375b2003-06-09 18:48:31 +0000570 if (entry->direct == COMMUNITY_PERMIT)
Daniel Waltond3ac7332015-08-24 10:19:10 -0400571 {
572 com_index_to_delete[delete_index] = i;
573 delete_index++;
574 }
575 break;
576 }
577
578 else if ((entry->style == COMMUNITY_LIST_STANDARD)
579 && (community_include (entry->u.com, COMMUNITY_INTERNET)
580 || community_include (entry->u.com, val) ))
581 {
582 if (entry->direct == COMMUNITY_PERMIT)
583 {
584 com_index_to_delete[delete_index] = i;
585 delete_index++;
586 }
587 break;
588 }
589
590 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
591 && community_regexp_include (entry->reg, com, i))
592 {
593 if (entry->direct == COMMUNITY_PERMIT)
594 {
595 com_index_to_delete[delete_index] = i;
596 delete_index++;
597 }
598 break;
599 }
600 }
601 }
602
603 /* Delete all of the communities we flagged for deletion */
604 for (i = delete_index-1; i >= 0; i--)
605 {
606 val = community_val_get (com, com_index_to_delete[i]);
607 community_del_val (com, &val);
paul718e3742002-12-13 20:15:29 +0000608 }
Daniel Waltond3ac7332015-08-24 10:19:10 -0400609
paul718e3742002-12-13 20:15:29 +0000610 return com;
611}
612
613/* To avoid duplicated entry in the community-list, this function
614 compares specified entry to existing entry. */
paul94f2b392005-06-28 12:44:16 +0000615static int
paul8708b742003-06-07 02:03:11 +0000616community_list_dup_check (struct community_list *list,
617 struct community_entry *new)
paul718e3742002-12-13 20:15:29 +0000618{
619 struct community_entry *entry;
paul8708b742003-06-07 02:03:11 +0000620
paul718e3742002-12-13 20:15:29 +0000621 for (entry = list->head; entry; entry = entry->next)
622 {
623 if (entry->style != new->style)
paul8708b742003-06-07 02:03:11 +0000624 continue;
paul718e3742002-12-13 20:15:29 +0000625
626 if (entry->direct != new->direct)
paul8708b742003-06-07 02:03:11 +0000627 continue;
paul718e3742002-12-13 20:15:29 +0000628
629 if (entry->any != new->any)
paul8708b742003-06-07 02:03:11 +0000630 continue;
paul718e3742002-12-13 20:15:29 +0000631
632 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000633 return 1;
paul718e3742002-12-13 20:15:29 +0000634
635 switch (entry->style)
paul8708b742003-06-07 02:03:11 +0000636 {
637 case COMMUNITY_LIST_STANDARD:
638 if (community_cmp (entry->u.com, new->u.com))
639 return 1;
640 break;
641 case EXTCOMMUNITY_LIST_STANDARD:
642 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
643 return 1;
644 break;
645 case COMMUNITY_LIST_EXPANDED:
646 case EXTCOMMUNITY_LIST_EXPANDED:
647 if (strcmp (entry->config, new->config) == 0)
648 return 1;
649 break;
650 default:
651 break;
652 }
paul718e3742002-12-13 20:15:29 +0000653 }
654 return 0;
655}
David Lamparter6b0655a2014-06-04 06:53:35 +0200656
paul718e3742002-12-13 20:15:29 +0000657/* Set community-list. */
658int
659community_list_set (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000660 const char *name, const char *str, int direct, int style)
paul718e3742002-12-13 20:15:29 +0000661{
hassofee6e4e2005-02-02 16:29:31 +0000662 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000663 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000664 struct community *com = NULL;
665 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000666
667 /* Get community list. */
hassofee6e4e2005-02-02 16:29:31 +0000668 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000669
670 /* When community-list already has entry, new entry should have same
671 style. If you want to have mixed style community-list, you can
672 comment out this check. */
paul8708b742003-06-07 02:03:11 +0000673 if (!community_list_empty_p (list))
paul718e3742002-12-13 20:15:29 +0000674 {
675 struct community_entry *first;
676
677 first = list->head;
678
hassofee6e4e2005-02-02 16:29:31 +0000679 if (style != first->style)
680 {
681 return (first->style == COMMUNITY_LIST_STANDARD
682 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
683 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
684 }
paul718e3742002-12-13 20:15:29 +0000685 }
686
hassofee6e4e2005-02-02 16:29:31 +0000687 if (str)
paul718e3742002-12-13 20:15:29 +0000688 {
hassofee6e4e2005-02-02 16:29:31 +0000689 if (style == COMMUNITY_LIST_STANDARD)
690 com = community_str2com (str);
paul718e3742002-12-13 20:15:29 +0000691 else
hassofee6e4e2005-02-02 16:29:31 +0000692 regex = bgp_regcomp (str);
paul8708b742003-06-07 02:03:11 +0000693
hassofee6e4e2005-02-02 16:29:31 +0000694 if (! com && ! regex)
695 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000696 }
697
hassofee6e4e2005-02-02 16:29:31 +0000698 entry = community_entry_new ();
699 entry->direct = direct;
700 entry->style = style;
701 entry->any = (str ? 0 : 1);
702 entry->u.com = com;
703 entry->reg = regex;
704 entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
705
paul718e3742002-12-13 20:15:29 +0000706 /* Do not put duplicated community entry. */
707 if (community_list_dup_check (list, entry))
708 community_entry_free (entry);
709 else
710 community_list_entry_add (list, entry);
711
712 return 0;
713}
714
715/* Unset community-list. When str is NULL, delete all of
716 community-list entry belongs to the specified name. */
717int
718community_list_unset (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000719 const char *name, const char *str,
720 int direct, int style)
paul718e3742002-12-13 20:15:29 +0000721{
hassofee6e4e2005-02-02 16:29:31 +0000722 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000723 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000724 struct community *com = NULL;
725 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000726
727 /* Lookup community list. */
hassofee6e4e2005-02-02 16:29:31 +0000728 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000729 if (list == NULL)
730 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
731
732 /* Delete all of entry belongs to this community-list. */
paul8708b742003-06-07 02:03:11 +0000733 if (!str)
paul718e3742002-12-13 20:15:29 +0000734 {
735 community_list_delete (list);
736 return 0;
737 }
738
hassofee6e4e2005-02-02 16:29:31 +0000739 if (style == COMMUNITY_LIST_STANDARD)
740 com = community_str2com (str);
741 else
742 regex = bgp_regcomp (str);
paul718e3742002-12-13 20:15:29 +0000743
hassofee6e4e2005-02-02 16:29:31 +0000744 if (! com && ! regex)
745 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000746
hassofee6e4e2005-02-02 16:29:31 +0000747 if (com)
748 entry = community_list_entry_lookup (list, com, direct);
749 else
750 entry = community_list_entry_lookup (list, str, direct);
751
752 if (com)
753 community_free (com);
754 if (regex)
755 bgp_regex_free (regex);
paul718e3742002-12-13 20:15:29 +0000756
paul8708b742003-06-07 02:03:11 +0000757 if (!entry)
paul718e3742002-12-13 20:15:29 +0000758 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
759
760 community_list_entry_delete (list, entry, style);
761
762 return 0;
763}
764
765/* Set extcommunity-list. */
766int
767extcommunity_list_set (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000768 const char *name, const char *str,
769 int direct, int style)
paul718e3742002-12-13 20:15:29 +0000770{
hassofee6e4e2005-02-02 16:29:31 +0000771 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000772 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000773 struct ecommunity *ecom = NULL;
774 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000775
776 entry = NULL;
777
778 /* Get community list. */
hassofee6e4e2005-02-02 16:29:31 +0000779 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000780
781 /* When community-list already has entry, new entry should have same
782 style. If you want to have mixed style community-list, you can
783 comment out this check. */
paul8708b742003-06-07 02:03:11 +0000784 if (!community_list_empty_p (list))
paul718e3742002-12-13 20:15:29 +0000785 {
786 struct community_entry *first;
787
788 first = list->head;
789
hassofee6e4e2005-02-02 16:29:31 +0000790 if (style != first->style)
791 {
792 return (first->style == EXTCOMMUNITY_LIST_STANDARD
793 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
794 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
795 }
paul718e3742002-12-13 20:15:29 +0000796 }
797
hassofee6e4e2005-02-02 16:29:31 +0000798 if (str)
paul718e3742002-12-13 20:15:29 +0000799 {
hassofee6e4e2005-02-02 16:29:31 +0000800 if (style == EXTCOMMUNITY_LIST_STANDARD)
801 ecom = ecommunity_str2com (str, 0, 1);
paul718e3742002-12-13 20:15:29 +0000802 else
hassofee6e4e2005-02-02 16:29:31 +0000803 regex = bgp_regcomp (str);
804
805 if (! ecom && ! regex)
806 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000807 }
hassofee6e4e2005-02-02 16:29:31 +0000808
809 if (ecom)
810 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
811
812 entry = community_entry_new ();
813 entry->direct = direct;
814 entry->style = style;
815 entry->any = (str ? 0 : 1);
816 if (ecom)
817 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
818 else if (regex)
819 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
paul718e3742002-12-13 20:15:29 +0000820 else
hassofee6e4e2005-02-02 16:29:31 +0000821 entry->config = NULL;
822 entry->u.ecom = ecom;
823 entry->reg = regex;
paul718e3742002-12-13 20:15:29 +0000824
825 /* Do not put duplicated community entry. */
826 if (community_list_dup_check (list, entry))
827 community_entry_free (entry);
828 else
829 community_list_entry_add (list, entry);
830
831 return 0;
832}
833
834/* Unset extcommunity-list. When str is NULL, delete all of
835 extcommunity-list entry belongs to the specified name. */
836int
837extcommunity_list_unset (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000838 const char *name, const char *str,
839 int direct, int style)
paul718e3742002-12-13 20:15:29 +0000840{
hassofee6e4e2005-02-02 16:29:31 +0000841 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000842 struct community_list *list;
843 struct ecommunity *ecom = NULL;
hassofee6e4e2005-02-02 16:29:31 +0000844 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000845
846 /* Lookup extcommunity list. */
hassofee6e4e2005-02-02 16:29:31 +0000847 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000848 if (list == NULL)
849 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
850
851 /* Delete all of entry belongs to this extcommunity-list. */
paul8708b742003-06-07 02:03:11 +0000852 if (!str)
paul718e3742002-12-13 20:15:29 +0000853 {
854 community_list_delete (list);
855 return 0;
856 }
857
hassofee6e4e2005-02-02 16:29:31 +0000858 if (style == EXTCOMMUNITY_LIST_STANDARD)
859 ecom = ecommunity_str2com (str, 0, 1);
860 else
861 regex = bgp_regcomp (str);
paul718e3742002-12-13 20:15:29 +0000862
hassofee6e4e2005-02-02 16:29:31 +0000863 if (! ecom && ! regex)
864 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000865
hassofee6e4e2005-02-02 16:29:31 +0000866 if (ecom)
867 entry = community_list_entry_lookup (list, ecom, direct);
868 else
869 entry = community_list_entry_lookup (list, str, direct);
870
871 if (ecom)
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000872 ecommunity_free (&ecom);
hassofee6e4e2005-02-02 16:29:31 +0000873 if (regex)
874 bgp_regex_free (regex);
paul718e3742002-12-13 20:15:29 +0000875
paul8708b742003-06-07 02:03:11 +0000876 if (!entry)
paul718e3742002-12-13 20:15:29 +0000877 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
878
879 community_list_entry_delete (list, entry, style);
880
881 return 0;
882}
883
884/* Initializa community-list. Return community-list handler. */
885struct community_list_handler *
paul94f2b392005-06-28 12:44:16 +0000886community_list_init (void)
paul718e3742002-12-13 20:15:29 +0000887{
888 struct community_list_handler *ch;
889 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
paul8708b742003-06-07 02:03:11 +0000890 sizeof (struct community_list_handler));
paul718e3742002-12-13 20:15:29 +0000891 return ch;
892}
893
894/* Terminate community-list. */
Chris Caputo228da422009-07-18 05:44:03 +0000895void
paul718e3742002-12-13 20:15:29 +0000896community_list_terminate (struct community_list_handler *ch)
897{
898 struct community_list_master *cm;
899 struct community_list *list;
900
901 cm = &ch->community_list;
902 while ((list = cm->num.head) != NULL)
903 community_list_delete (list);
904 while ((list = cm->str.head) != NULL)
905 community_list_delete (list);
906
907 cm = &ch->extcommunity_list;
908 while ((list = cm->num.head) != NULL)
909 community_list_delete (list);
910 while ((list = cm->str.head) != NULL)
911 community_list_delete (list);
912
913 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
914}