blob: 13bdf8e8413afd5397dab7c6916e5078cc04fdcc [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"
Job Snijders3334bab2017-01-20 14:47:12 +000031#include "bgpd/bgp_lcommunity.h"
paul718e3742002-12-13 20:15:29 +000032#include "bgpd/bgp_aspath.h"
33#include "bgpd/bgp_regex.h"
34#include "bgpd/bgp_clist.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020035
paul718e3742002-12-13 20:15:29 +000036/* Lookup master structure for community-list or
37 extcommunity-list. */
38struct community_list_master *
hassofee6e4e2005-02-02 16:29:31 +000039community_list_master_lookup (struct community_list_handler *ch, int master)
paul718e3742002-12-13 20:15:29 +000040{
41 if (ch)
hassofee6e4e2005-02-02 16:29:31 +000042 switch (master)
paul718e3742002-12-13 20:15:29 +000043 {
hassofee6e4e2005-02-02 16:29:31 +000044 case COMMUNITY_LIST_MASTER:
45 return &ch->community_list;
hassofee6e4e2005-02-02 16:29:31 +000046 case EXTCOMMUNITY_LIST_MASTER:
47 return &ch->extcommunity_list;
Job Snijders3334bab2017-01-20 14:47:12 +000048 case LARGE_COMMUNITY_LIST_MASTER:
49 return &ch->lcommunity_list;
paul718e3742002-12-13 20:15:29 +000050 }
51 return NULL;
52}
53
54/* Allocate a new community list entry. */
paul94f2b392005-06-28 12:44:16 +000055static struct community_entry *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080056community_entry_new (void)
paul718e3742002-12-13 20:15:29 +000057{
Stephen Hemminger393deb92008-08-18 14:13:29 -070058 return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
paul718e3742002-12-13 20:15:29 +000059}
60
61/* Free community list entry. */
paul94f2b392005-06-28 12:44:16 +000062static void
paul718e3742002-12-13 20:15:29 +000063community_entry_free (struct community_entry *entry)
64{
65 switch (entry->style)
66 {
67 case COMMUNITY_LIST_STANDARD:
68 if (entry->u.com)
paul8708b742003-06-07 02:03:11 +000069 community_free (entry->u.com);
paul718e3742002-12-13 20:15:29 +000070 break;
Job Snijders3334bab2017-01-20 14:47:12 +000071 case LARGE_COMMUNITY_LIST_STANDARD:
72 if (entry->u.lcom)
73 lcommunity_free (&entry->u.lcom);
74 break;
paul718e3742002-12-13 20:15:29 +000075 case EXTCOMMUNITY_LIST_STANDARD:
76 /* In case of standard extcommunity-list, configuration string
paul8708b742003-06-07 02:03:11 +000077 is made by ecommunity_ecom2str(). */
paul718e3742002-12-13 20:15:29 +000078 if (entry->config)
paul8708b742003-06-07 02:03:11 +000079 XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
paul718e3742002-12-13 20:15:29 +000080 if (entry->u.ecom)
Paul Jakmaf6f434b2010-11-23 21:28:03 +000081 ecommunity_free (&entry->u.ecom);
paul718e3742002-12-13 20:15:29 +000082 break;
83 case COMMUNITY_LIST_EXPANDED:
84 case EXTCOMMUNITY_LIST_EXPANDED:
Job Snijders3334bab2017-01-20 14:47:12 +000085 case LARGE_COMMUNITY_LIST_EXPANDED:
paul718e3742002-12-13 20:15:29 +000086 if (entry->config)
paul8708b742003-06-07 02:03:11 +000087 XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
paul718e3742002-12-13 20:15:29 +000088 if (entry->reg)
paul8708b742003-06-07 02:03:11 +000089 bgp_regex_free (entry->reg);
paul718e3742002-12-13 20:15:29 +000090 default:
91 break;
92 }
93 XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
94}
95
96/* Allocate a new community-list. */
paul94f2b392005-06-28 12:44:16 +000097static struct community_list *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080098community_list_new (void)
paul718e3742002-12-13 20:15:29 +000099{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700100 return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
paul718e3742002-12-13 20:15:29 +0000101}
102
103/* Free community-list. */
paul94f2b392005-06-28 12:44:16 +0000104static void
paul718e3742002-12-13 20:15:29 +0000105community_list_free (struct community_list *list)
106{
107 if (list->name)
108 XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name);
109 XFREE (MTYPE_COMMUNITY_LIST, list);
110}
111
paul94f2b392005-06-28 12:44:16 +0000112static struct community_list *
paul718e3742002-12-13 20:15:29 +0000113community_list_insert (struct community_list_handler *ch,
hassofee6e4e2005-02-02 16:29:31 +0000114 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000115{
paulfd79ac92004-10-13 05:06:08 +0000116 size_t i;
paul718e3742002-12-13 20:15:29 +0000117 long number;
118 struct community_list *new;
119 struct community_list *point;
120 struct community_list_list *list;
121 struct community_list_master *cm;
122
123 /* Lookup community-list master. */
hassofee6e4e2005-02-02 16:29:31 +0000124 cm = community_list_master_lookup (ch, master);
paul8708b742003-06-07 02:03:11 +0000125 if (!cm)
paul718e3742002-12-13 20:15:29 +0000126 return NULL;
127
128 /* Allocate new community_list and copy given name. */
129 new = community_list_new ();
130 new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name);
131
132 /* If name is made by all digit character. We treat it as
133 number. */
134 for (number = 0, i = 0; i < strlen (name); i++)
135 {
136 if (isdigit ((int) name[i]))
paul8708b742003-06-07 02:03:11 +0000137 number = (number * 10) + (name[i] - '0');
paul718e3742002-12-13 20:15:29 +0000138 else
paul8708b742003-06-07 02:03:11 +0000139 break;
paul718e3742002-12-13 20:15:29 +0000140 }
141
142 /* In case of name is all digit character */
143 if (i == strlen (name))
144 {
145 new->sort = COMMUNITY_LIST_NUMBER;
146
147 /* Set access_list to number list. */
148 list = &cm->num;
149
150 for (point = list->head; point; point = point->next)
paul8708b742003-06-07 02:03:11 +0000151 if (atol (point->name) >= number)
152 break;
paul718e3742002-12-13 20:15:29 +0000153 }
154 else
155 {
156 new->sort = COMMUNITY_LIST_STRING;
157
158 /* Set access_list to string list. */
159 list = &cm->str;
paul8708b742003-06-07 02:03:11 +0000160
paul718e3742002-12-13 20:15:29 +0000161 /* Set point to insertion point. */
162 for (point = list->head; point; point = point->next)
paul8708b742003-06-07 02:03:11 +0000163 if (strcmp (point->name, name) >= 0)
164 break;
paul718e3742002-12-13 20:15:29 +0000165 }
166
167 /* Link to upper list. */
168 new->parent = list;
169
170 /* In case of this is the first element of master. */
171 if (list->head == NULL)
172 {
173 list->head = list->tail = new;
174 return new;
175 }
176
177 /* In case of insertion is made at the tail of access_list. */
178 if (point == NULL)
179 {
180 new->prev = list->tail;
181 list->tail->next = new;
182 list->tail = new;
183 return new;
184 }
185
186 /* In case of insertion is made at the head of access_list. */
187 if (point == list->head)
188 {
189 new->next = list->head;
190 list->head->prev = new;
191 list->head = new;
192 return new;
193 }
194
195 /* Insertion is made at middle of the access_list. */
196 new->next = point;
197 new->prev = point->prev;
198
199 if (point->prev)
200 point->prev->next = new;
201 point->prev = new;
202
203 return new;
204}
205
206struct community_list *
207community_list_lookup (struct community_list_handler *ch,
hassofee6e4e2005-02-02 16:29:31 +0000208 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000209{
210 struct community_list *list;
211 struct community_list_master *cm;
212
paul8708b742003-06-07 02:03:11 +0000213 if (!name)
paul718e3742002-12-13 20:15:29 +0000214 return NULL;
215
hassofee6e4e2005-02-02 16:29:31 +0000216 cm = community_list_master_lookup (ch, master);
paul8708b742003-06-07 02:03:11 +0000217 if (!cm)
paul718e3742002-12-13 20:15:29 +0000218 return NULL;
219
220 for (list = cm->num.head; list; list = list->next)
221 if (strcmp (list->name, name) == 0)
222 return list;
223 for (list = cm->str.head; list; list = list->next)
224 if (strcmp (list->name, name) == 0)
225 return list;
226
227 return NULL;
228}
229
paul94f2b392005-06-28 12:44:16 +0000230static struct community_list *
hassofee6e4e2005-02-02 16:29:31 +0000231community_list_get (struct community_list_handler *ch,
232 const char *name, int master)
paul718e3742002-12-13 20:15:29 +0000233{
234 struct community_list *list;
235
hassofee6e4e2005-02-02 16:29:31 +0000236 list = community_list_lookup (ch, name, master);
paul8708b742003-06-07 02:03:11 +0000237 if (!list)
hassofee6e4e2005-02-02 16:29:31 +0000238 list = community_list_insert (ch, name, master);
paul718e3742002-12-13 20:15:29 +0000239 return list;
240}
241
paul94f2b392005-06-28 12:44:16 +0000242static void
paul718e3742002-12-13 20:15:29 +0000243community_list_delete (struct community_list *list)
244{
245 struct community_list_list *clist;
246 struct community_entry *entry, *next;
247
248 for (entry = list->head; entry; entry = next)
249 {
250 next = entry->next;
251 community_entry_free (entry);
252 }
253
254 clist = list->parent;
255
256 if (list->next)
257 list->next->prev = list->prev;
258 else
259 clist->tail = list->prev;
260
261 if (list->prev)
262 list->prev->next = list->next;
263 else
264 clist->head = list->next;
265
266 community_list_free (list);
267}
268
paul94f2b392005-06-28 12:44:16 +0000269static int
paul718e3742002-12-13 20:15:29 +0000270community_list_empty_p (struct community_list *list)
271{
272 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
273}
David Lamparter6b0655a2014-06-04 06:53:35 +0200274
paul718e3742002-12-13 20:15:29 +0000275/* Add community-list entry to the list. */
276static void
paul8708b742003-06-07 02:03:11 +0000277community_list_entry_add (struct community_list *list,
278 struct community_entry *entry)
paul718e3742002-12-13 20:15:29 +0000279{
280 entry->next = NULL;
281 entry->prev = list->tail;
282
283 if (list->tail)
284 list->tail->next = entry;
285 else
286 list->head = entry;
287 list->tail = entry;
288}
289
290/* Delete community-list entry from the list. */
291static void
292community_list_entry_delete (struct community_list *list,
paul8708b742003-06-07 02:03:11 +0000293 struct community_entry *entry, int style)
paul718e3742002-12-13 20:15:29 +0000294{
295 if (entry->next)
296 entry->next->prev = entry->prev;
297 else
298 list->tail = entry->prev;
299
300 if (entry->prev)
301 entry->prev->next = entry->next;
302 else
303 list->head = entry->next;
304
305 community_entry_free (entry);
306
307 if (community_list_empty_p (list))
308 community_list_delete (list);
309}
310
311/* Lookup community-list entry from the list. */
312static struct community_entry *
paulfd79ac92004-10-13 05:06:08 +0000313community_list_entry_lookup (struct community_list *list, const void *arg,
paul8708b742003-06-07 02:03:11 +0000314 int direct)
paul718e3742002-12-13 20:15:29 +0000315{
316 struct community_entry *entry;
317
318 for (entry = list->head; entry; entry = entry->next)
319 {
320 switch (entry->style)
paul8708b742003-06-07 02:03:11 +0000321 {
322 case COMMUNITY_LIST_STANDARD:
323 if (community_cmp (entry->u.com, arg))
324 return entry;
325 break;
Job Snijders3334bab2017-01-20 14:47:12 +0000326 case LARGE_COMMUNITY_LIST_STANDARD:
327 if (lcommunity_cmp (entry->u.lcom, arg))
328 return entry;
329 break;
paul8708b742003-06-07 02:03:11 +0000330 case EXTCOMMUNITY_LIST_STANDARD:
331 if (ecommunity_cmp (entry->u.ecom, arg))
332 return entry;
333 break;
334 case COMMUNITY_LIST_EXPANDED:
335 case EXTCOMMUNITY_LIST_EXPANDED:
Job Snijders3334bab2017-01-20 14:47:12 +0000336 case LARGE_COMMUNITY_LIST_EXPANDED:
paul8708b742003-06-07 02:03:11 +0000337 if (strcmp (entry->config, arg) == 0)
338 return entry;
339 break;
340 default:
341 break;
342 }
paul718e3742002-12-13 20:15:29 +0000343 }
344 return NULL;
345}
David Lamparter6b0655a2014-06-04 06:53:35 +0200346
Daniel Waltond3ac7332015-08-24 10:19:10 -0400347static char *
348community_str_get (struct community *com, int i)
349{
350 int len;
351 u_int32_t comval;
352 u_int16_t as;
353 u_int16_t val;
354 char *str;
355 char *pnt;
356
357 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
358 comval = ntohl (comval);
359
360 switch (comval)
361 {
362 case COMMUNITY_INTERNET:
363 len = strlen (" internet");
364 break;
365 case COMMUNITY_NO_EXPORT:
366 len = strlen (" no-export");
367 break;
368 case COMMUNITY_NO_ADVERTISE:
369 len = strlen (" no-advertise");
370 break;
371 case COMMUNITY_LOCAL_AS:
372 len = strlen (" local-AS");
373 break;
374 default:
375 len = strlen (" 65536:65535");
376 break;
377 }
378
379 /* Allocate memory. */
380 str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
381
382 switch (comval)
383 {
384 case COMMUNITY_INTERNET:
385 strcpy (pnt, "internet");
386 pnt += strlen ("internet");
387 break;
388 case COMMUNITY_NO_EXPORT:
389 strcpy (pnt, "no-export");
390 pnt += strlen ("no-export");
391 break;
392 case COMMUNITY_NO_ADVERTISE:
393 strcpy (pnt, "no-advertise");
394 pnt += strlen ("no-advertise");
395 break;
396 case COMMUNITY_LOCAL_AS:
397 strcpy (pnt, "local-AS");
398 pnt += strlen ("local-AS");
399 break;
400 default:
401 as = (comval >> 16) & 0xFFFF;
402 val = comval & 0xFFFF;
403 sprintf (pnt, "%u:%d", as, val);
404 pnt += strlen (pnt);
405 break;
406 }
407
408 *pnt = '\0';
409
410 return str;
411}
412
413/* Internal function to perform regular expression match for
414 * * a single community. */
415static int
416community_regexp_include (regex_t * reg, struct community *com, int i)
417{
Christian Franke32e41f72016-06-14 20:06:59 +0200418 char *str;
419 int rv;
Daniel Waltond3ac7332015-08-24 10:19:10 -0400420
421 /* When there is no communities attribute it is treated as empty
422 * string. */
423 if (com == NULL || com->size == 0)
Christian Franke32e41f72016-06-14 20:06:59 +0200424 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
Daniel Waltond3ac7332015-08-24 10:19:10 -0400425 else
426 str = community_str_get (com, i);
427
428 /* Regular expression match. */
Christian Franke32e41f72016-06-14 20:06:59 +0200429 rv = regexec (reg, str, 0, NULL, 0);
430
431 XFREE(MTYPE_COMMUNITY_STR, str);
432
433 if (rv == 0)
Daniel Waltond3ac7332015-08-24 10:19:10 -0400434 return 1;
435
436 /* No match. */
437 return 0;
438}
439
paul718e3742002-12-13 20:15:29 +0000440/* Internal function to perform regular expression match for community
441 attribute. */
442static int
paul8708b742003-06-07 02:03:11 +0000443community_regexp_match (struct community *com, regex_t * reg)
paul718e3742002-12-13 20:15:29 +0000444{
paulfd79ac92004-10-13 05:06:08 +0000445 const char *str;
paul718e3742002-12-13 20:15:29 +0000446
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
Job Snijders3334bab2017-01-20 14:47:12 +0000462static 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
505 * * a single community. */
506static int
507lcommunity_regexp_include (regex_t * reg, struct lcommunity *lcom, int i)
508{
509 const char *str;
510
511 /* When there is no communities attribute it is treated as empty
512 * string. */
513 if (lcom == NULL || lcom->size == 0)
514 str = "";
515 else
516 str = lcommunity_str_get (lcom, i);
517
518 /* Regular expression match. */
519 if (regexec (reg, str, 0, NULL, 0) == 0)
520 return 1;
521
522 /* No match. */
523 return 0;
524}
525
526static int
527lcommunity_regexp_match (struct lcommunity *com, regex_t * reg)
528{
529 const char *str;
530
531 /* When there is no communities attribute it is treated as empty
532 string. */
533 if (com == NULL || com->size == 0)
534 str = "";
535 else
536 str = lcommunity_str (com);
537
538 /* Regular expression match. */
539 if (regexec (reg, str, 0, NULL, 0) == 0)
540 return 1;
541
542 /* No match. */
543 return 0;
544}
545
546
paul8708b742003-06-07 02:03:11 +0000547static int
548ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
549{
paulfd79ac92004-10-13 05:06:08 +0000550 const char *str;
paul8708b742003-06-07 02:03:11 +0000551
552 /* When there is no communities attribute it is treated as empty
553 string. */
554 if (ecom == NULL || ecom->size == 0)
555 str = "";
556 else
557 str = ecommunity_str (ecom);
558
559 /* Regular expression match. */
560 if (regexec (reg, str, 0, NULL, 0) == 0)
561 return 1;
562
563 /* No match. */
564 return 0;
565}
566
paul718e3742002-12-13 20:15:29 +0000567/* When given community attribute matches to the community-list return
568 1 else return 0. */
569int
570community_list_match (struct community *com, struct community_list *list)
571{
572 struct community_entry *entry;
573
574 for (entry = list->head; entry; entry = entry->next)
575 {
576 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000577 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000578
579 if (entry->style == COMMUNITY_LIST_STANDARD)
paul8708b742003-06-07 02:03:11 +0000580 {
581 if (community_include (entry->u.com, COMMUNITY_INTERNET))
582 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000583
paul8708b742003-06-07 02:03:11 +0000584 if (community_match (com, entry->u.com))
585 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
586 }
paul718e3742002-12-13 20:15:29 +0000587 else if (entry->style == COMMUNITY_LIST_EXPANDED)
paul8708b742003-06-07 02:03:11 +0000588 {
589 if (community_regexp_match (com, entry->reg))
590 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
591 }
592 }
593 return 0;
594}
595
596int
Job Snijders3334bab2017-01-20 14:47:12 +0000597lcommunity_list_match (struct lcommunity *lcom, struct community_list *list)
598{
599 struct community_entry *entry;
600
601 for (entry = list->head; entry; entry = entry->next)
602 {
603 if (entry->any)
604 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
605
606 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD)
607 {
608 if (lcommunity_match (lcom, entry->u.lcom))
609 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
610 }
611 else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED)
612 {
613 if (lcommunity_regexp_match (lcom, entry->reg))
614 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
615 }
616 }
617 return 0;
618}
619
620int
paul8708b742003-06-07 02:03:11 +0000621ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
622{
623 struct community_entry *entry;
624
625 for (entry = list->head; entry; entry = entry->next)
626 {
627 if (entry->any)
628 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
629
630 if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
631 {
632 if (ecommunity_match (ecom, entry->u.ecom))
633 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
634 }
635 else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
636 {
637 if (ecommunity_regexp_match (ecom, entry->reg))
638 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
639 }
paul718e3742002-12-13 20:15:29 +0000640 }
641 return 0;
642}
643
644/* Perform exact matching. In case of expanded community-list, do
645 same thing as community_list_match(). */
646int
paul8708b742003-06-07 02:03:11 +0000647community_list_exact_match (struct community *com,
648 struct community_list *list)
paul718e3742002-12-13 20:15:29 +0000649{
650 struct community_entry *entry;
651
652 for (entry = list->head; entry; entry = entry->next)
653 {
654 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000655 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000656
657 if (entry->style == COMMUNITY_LIST_STANDARD)
paul8708b742003-06-07 02:03:11 +0000658 {
659 if (community_include (entry->u.com, COMMUNITY_INTERNET))
660 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
paul718e3742002-12-13 20:15:29 +0000661
paul8708b742003-06-07 02:03:11 +0000662 if (community_cmp (com, entry->u.com))
663 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
664 }
paul718e3742002-12-13 20:15:29 +0000665 else if (entry->style == COMMUNITY_LIST_EXPANDED)
paul8708b742003-06-07 02:03:11 +0000666 {
667 if (community_regexp_match (com, entry->reg))
668 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
669 }
paul718e3742002-12-13 20:15:29 +0000670 }
671 return 0;
672}
673
paul8708b742003-06-07 02:03:11 +0000674/* Delete all permitted communities in the list from com. */
paul718e3742002-12-13 20:15:29 +0000675struct community *
676community_list_match_delete (struct community *com,
paul8708b742003-06-07 02:03:11 +0000677 struct community_list *list)
paul718e3742002-12-13 20:15:29 +0000678{
679 struct community_entry *entry;
Daniel Waltond3ac7332015-08-24 10:19:10 -0400680 u_int32_t val;
681 u_int32_t com_index_to_delete[com->size];
682 int delete_index = 0;
683 int i;
paul718e3742002-12-13 20:15:29 +0000684
Daniel Waltond3ac7332015-08-24 10:19:10 -0400685 /* Loop over each community value and evaluate each against the
686 * community-list. If we need to delete a community value add its index to
687 * com_index_to_delete.
688 */
689 for (i = 0; i < com->size; i++)
paul718e3742002-12-13 20:15:29 +0000690 {
Daniel Waltond3ac7332015-08-24 10:19:10 -0400691 val = community_val_get (com, i);
paul718e3742002-12-13 20:15:29 +0000692
Daniel Waltond3ac7332015-08-24 10:19:10 -0400693 for (entry = list->head; entry; entry = entry->next)
paul8708b742003-06-07 02:03:11 +0000694 {
Daniel Waltond3ac7332015-08-24 10:19:10 -0400695 if (entry->any)
696 {
paul847375b2003-06-09 18:48:31 +0000697 if (entry->direct == COMMUNITY_PERMIT)
Daniel Waltond3ac7332015-08-24 10:19:10 -0400698 {
699 com_index_to_delete[delete_index] = i;
700 delete_index++;
701 }
702 break;
703 }
704
705 else if ((entry->style == COMMUNITY_LIST_STANDARD)
706 && (community_include (entry->u.com, COMMUNITY_INTERNET)
707 || community_include (entry->u.com, val) ))
708 {
709 if (entry->direct == COMMUNITY_PERMIT)
710 {
711 com_index_to_delete[delete_index] = i;
712 delete_index++;
713 }
714 break;
715 }
716
717 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
718 && community_regexp_include (entry->reg, com, i))
719 {
720 if (entry->direct == COMMUNITY_PERMIT)
721 {
722 com_index_to_delete[delete_index] = i;
723 delete_index++;
724 }
725 break;
726 }
727 }
728 }
729
730 /* Delete all of the communities we flagged for deletion */
731 for (i = delete_index-1; i >= 0; i--)
732 {
733 val = community_val_get (com, com_index_to_delete[i]);
734 community_del_val (com, &val);
paul718e3742002-12-13 20:15:29 +0000735 }
Daniel Waltond3ac7332015-08-24 10:19:10 -0400736
paul718e3742002-12-13 20:15:29 +0000737 return com;
738}
739
740/* To avoid duplicated entry in the community-list, this function
741 compares specified entry to existing entry. */
paul94f2b392005-06-28 12:44:16 +0000742static int
paul8708b742003-06-07 02:03:11 +0000743community_list_dup_check (struct community_list *list,
744 struct community_entry *new)
paul718e3742002-12-13 20:15:29 +0000745{
746 struct community_entry *entry;
paul8708b742003-06-07 02:03:11 +0000747
paul718e3742002-12-13 20:15:29 +0000748 for (entry = list->head; entry; entry = entry->next)
749 {
750 if (entry->style != new->style)
paul8708b742003-06-07 02:03:11 +0000751 continue;
paul718e3742002-12-13 20:15:29 +0000752
753 if (entry->direct != new->direct)
paul8708b742003-06-07 02:03:11 +0000754 continue;
paul718e3742002-12-13 20:15:29 +0000755
756 if (entry->any != new->any)
paul8708b742003-06-07 02:03:11 +0000757 continue;
paul718e3742002-12-13 20:15:29 +0000758
759 if (entry->any)
paul8708b742003-06-07 02:03:11 +0000760 return 1;
paul718e3742002-12-13 20:15:29 +0000761
762 switch (entry->style)
paul8708b742003-06-07 02:03:11 +0000763 {
764 case COMMUNITY_LIST_STANDARD:
765 if (community_cmp (entry->u.com, new->u.com))
766 return 1;
767 break;
768 case EXTCOMMUNITY_LIST_STANDARD:
769 if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
770 return 1;
771 break;
Job Snijders3334bab2017-01-20 14:47:12 +0000772 case LARGE_COMMUNITY_LIST_STANDARD:
773 if (lcommunity_cmp (entry->u.lcom, new->u.lcom))
774 return 1;
775 break;
paul8708b742003-06-07 02:03:11 +0000776 case COMMUNITY_LIST_EXPANDED:
777 case EXTCOMMUNITY_LIST_EXPANDED:
Job Snijders3334bab2017-01-20 14:47:12 +0000778 case LARGE_COMMUNITY_LIST_EXPANDED:
Christian Frankec0a613f2016-06-06 22:22:15 +0200779 if (entry->config && new->config
780 && strcmp (entry->config, new->config) == 0)
781 return 1;
782 if (!entry->config && !new->config)
paul8708b742003-06-07 02:03:11 +0000783 return 1;
784 break;
785 default:
786 break;
787 }
paul718e3742002-12-13 20:15:29 +0000788 }
789 return 0;
790}
David Lamparter6b0655a2014-06-04 06:53:35 +0200791
paul718e3742002-12-13 20:15:29 +0000792/* Set community-list. */
793int
794community_list_set (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000795 const char *name, const char *str, int direct, int style)
paul718e3742002-12-13 20:15:29 +0000796{
hassofee6e4e2005-02-02 16:29:31 +0000797 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000798 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000799 struct community *com = NULL;
800 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000801
802 /* Get community list. */
hassofee6e4e2005-02-02 16:29:31 +0000803 list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000804
805 /* When community-list already has entry, new entry should have same
806 style. If you want to have mixed style community-list, you can
807 comment out this check. */
paul8708b742003-06-07 02:03:11 +0000808 if (!community_list_empty_p (list))
paul718e3742002-12-13 20:15:29 +0000809 {
810 struct community_entry *first;
811
812 first = list->head;
813
hassofee6e4e2005-02-02 16:29:31 +0000814 if (style != first->style)
815 {
816 return (first->style == COMMUNITY_LIST_STANDARD
817 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
818 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
819 }
paul718e3742002-12-13 20:15:29 +0000820 }
821
hassofee6e4e2005-02-02 16:29:31 +0000822 if (str)
paul718e3742002-12-13 20:15:29 +0000823 {
hassofee6e4e2005-02-02 16:29:31 +0000824 if (style == COMMUNITY_LIST_STANDARD)
825 com = community_str2com (str);
paul718e3742002-12-13 20:15:29 +0000826 else
hassofee6e4e2005-02-02 16:29:31 +0000827 regex = bgp_regcomp (str);
paul8708b742003-06-07 02:03:11 +0000828
hassofee6e4e2005-02-02 16:29:31 +0000829 if (! com && ! regex)
830 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000831 }
832
hassofee6e4e2005-02-02 16:29:31 +0000833 entry = community_entry_new ();
834 entry->direct = direct;
835 entry->style = style;
836 entry->any = (str ? 0 : 1);
837 entry->u.com = com;
838 entry->reg = regex;
839 entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
840
paul718e3742002-12-13 20:15:29 +0000841 /* Do not put duplicated community entry. */
842 if (community_list_dup_check (list, entry))
843 community_entry_free (entry);
844 else
845 community_list_entry_add (list, entry);
846
847 return 0;
848}
849
850/* Unset community-list. When str is NULL, delete all of
851 community-list entry belongs to the specified name. */
852int
853community_list_unset (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +0000854 const char *name, const char *str,
855 int direct, int style)
paul718e3742002-12-13 20:15:29 +0000856{
hassofee6e4e2005-02-02 16:29:31 +0000857 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +0000858 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +0000859 struct community *com = NULL;
860 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +0000861
862 /* Lookup community list. */
hassofee6e4e2005-02-02 16:29:31 +0000863 list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +0000864 if (list == NULL)
865 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
866
867 /* Delete all of entry belongs to this community-list. */
paul8708b742003-06-07 02:03:11 +0000868 if (!str)
paul718e3742002-12-13 20:15:29 +0000869 {
870 community_list_delete (list);
871 return 0;
872 }
873
hassofee6e4e2005-02-02 16:29:31 +0000874 if (style == COMMUNITY_LIST_STANDARD)
875 com = community_str2com (str);
876 else
877 regex = bgp_regcomp (str);
paul718e3742002-12-13 20:15:29 +0000878
hassofee6e4e2005-02-02 16:29:31 +0000879 if (! com && ! regex)
880 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +0000881
hassofee6e4e2005-02-02 16:29:31 +0000882 if (com)
883 entry = community_list_entry_lookup (list, com, direct);
884 else
885 entry = community_list_entry_lookup (list, str, direct);
886
887 if (com)
888 community_free (com);
889 if (regex)
890 bgp_regex_free (regex);
paul718e3742002-12-13 20:15:29 +0000891
paul8708b742003-06-07 02:03:11 +0000892 if (!entry)
paul718e3742002-12-13 20:15:29 +0000893 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
894
895 community_list_entry_delete (list, entry, style);
896
897 return 0;
898}
899
Job Snijders3334bab2017-01-20 14:47:12 +0000900/* Delete all permitted large communities in the list from com. */
901struct lcommunity *
902lcommunity_list_match_delete (struct lcommunity *lcom,
903 struct community_list *list)
904{
905 struct community_entry *entry;
906 u_int32_t com_index_to_delete[lcom->size];
907 u_char *ptr;
908 int delete_index = 0;
909 int i;
910
911 /* Loop over each lcommunity value and evaluate each against the
912 * community-list. If we need to delete a community value add its index to
913 * com_index_to_delete.
914 */
915
916 for (i = 0; i < lcom->size; i++)
917 {
918 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
919 for (entry = list->head; entry; entry = entry->next)
920 {
921 if (entry->any)
922 {
923 if (entry->direct == COMMUNITY_PERMIT)
924 {
925 com_index_to_delete[delete_index] = i;
926 delete_index++;
927 }
928 break;
929 }
930
931 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
932 && lcommunity_include (entry->u.lcom, ptr) )
933 {
934 if (entry->direct == COMMUNITY_PERMIT)
935 {
936 com_index_to_delete[delete_index] = i;
937 delete_index++;
938 }
939 break;
940 }
941
942 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
943 && entry->reg
944 && lcommunity_regexp_include (entry->reg, lcom, i))
945 {
946 if (entry->direct == COMMUNITY_PERMIT)
947 {
948 com_index_to_delete[delete_index] = i;
949 delete_index++;
950 }
951 break;
952 }
953 }
954 }
955
956 /* Delete all of the communities we flagged for deletion */
957
958 for (i = delete_index-1; i >= 0; i--)
959 {
960 ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE);
961 lcommunity_del_val (lcom, ptr);
962 }
963
964 return lcom;
965}
966
967/* Set lcommunity-list. */
968int
969lcommunity_list_set (struct community_list_handler *ch,
970 const char *name, const char *str, int direct, int style)
971{
972 struct community_entry *entry = NULL;
973 struct community_list *list;
974 struct lcommunity *lcom = NULL;
975 regex_t *regex = NULL;
976
977 /* Get community list. */
978 list = community_list_get (ch, name, LARGE_COMMUNITY_LIST_MASTER);
979
980 /* When community-list already has entry, new entry should have same
981 style. If you want to have mixed style community-list, you can
982 comment out this check. */
983 if (!community_list_empty_p (list))
984 {
985 struct community_entry *first;
986
987 first = list->head;
988
989 if (style != first->style)
990 {
991 return (first->style == COMMUNITY_LIST_STANDARD
992 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
993 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
994 }
995 }
996
997 if (str)
998 {
999 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1000 lcom = lcommunity_str2com (str);
1001 else
1002 regex = bgp_regcomp (str);
1003
1004 if (! lcom && ! regex)
1005 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1006 }
1007
1008 entry = community_entry_new ();
1009 entry->direct = direct;
1010 entry->style = style;
1011 entry->any = (str ? 0 : 1);
1012 entry->u.lcom = lcom;
1013 entry->reg = regex;
1014 if (lcom)
1015 entry->config = lcommunity_lcom2str (lcom, LCOMMUNITY_FORMAT_COMMUNITY_LIST);
1016 else if (regex)
1017 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
1018 else
1019 entry->config = NULL;
1020
1021 /* Do not put duplicated community entry. */
1022 if (community_list_dup_check (list, entry))
1023 community_entry_free (entry);
1024 else
1025 community_list_entry_add (list, entry);
1026
1027 return 0;
1028}
1029
1030/* Unset community-list. When str is NULL, delete all of
1031 community-list entry belongs to the specified name. */
1032int
1033lcommunity_list_unset (struct community_list_handler *ch,
1034 const char *name, const char *str,
1035 int direct, int style)
1036{
1037 struct community_entry *entry = NULL;
1038 struct community_list *list;
1039 struct lcommunity *lcom = NULL;
1040 regex_t *regex = NULL;
1041
1042 /* Lookup community list. */
1043 list = community_list_lookup (ch, name, LARGE_COMMUNITY_LIST_MASTER);
1044 if (list == NULL)
1045 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1046
1047 /* Delete all of entry belongs to this community-list. */
1048 if (!str)
1049 {
1050 community_list_delete (list);
1051 return 0;
1052 }
1053
1054 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1055 lcom = lcommunity_str2com (str);
1056 else
1057 regex = bgp_regcomp (str);
1058
1059 if (! lcom && ! regex)
1060 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1061
1062 if (lcom)
1063 entry = community_list_entry_lookup (list, lcom, direct);
1064 else
1065 entry = community_list_entry_lookup (list, str, direct);
1066
1067 if (lcom)
1068 lcommunity_free (&lcom);
1069 if (regex)
1070 bgp_regex_free (regex);
1071
1072 if (!entry)
1073 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1074
1075 community_list_entry_delete (list, entry, style);
1076
1077 return 0;
1078}
1079
paul718e3742002-12-13 20:15:29 +00001080/* Set extcommunity-list. */
1081int
1082extcommunity_list_set (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +00001083 const char *name, const char *str,
1084 int direct, int style)
paul718e3742002-12-13 20:15:29 +00001085{
hassofee6e4e2005-02-02 16:29:31 +00001086 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +00001087 struct community_list *list;
hassofee6e4e2005-02-02 16:29:31 +00001088 struct ecommunity *ecom = NULL;
1089 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +00001090
1091 entry = NULL;
1092
1093 /* Get community list. */
hassofee6e4e2005-02-02 16:29:31 +00001094 list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +00001095
1096 /* When community-list already has entry, new entry should have same
1097 style. If you want to have mixed style community-list, you can
1098 comment out this check. */
paul8708b742003-06-07 02:03:11 +00001099 if (!community_list_empty_p (list))
paul718e3742002-12-13 20:15:29 +00001100 {
1101 struct community_entry *first;
1102
1103 first = list->head;
1104
hassofee6e4e2005-02-02 16:29:31 +00001105 if (style != first->style)
1106 {
1107 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1108 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1109 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1110 }
paul718e3742002-12-13 20:15:29 +00001111 }
1112
hassofee6e4e2005-02-02 16:29:31 +00001113 if (str)
paul718e3742002-12-13 20:15:29 +00001114 {
hassofee6e4e2005-02-02 16:29:31 +00001115 if (style == EXTCOMMUNITY_LIST_STANDARD)
1116 ecom = ecommunity_str2com (str, 0, 1);
paul718e3742002-12-13 20:15:29 +00001117 else
hassofee6e4e2005-02-02 16:29:31 +00001118 regex = bgp_regcomp (str);
1119
1120 if (! ecom && ! regex)
1121 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +00001122 }
hassofee6e4e2005-02-02 16:29:31 +00001123
1124 if (ecom)
1125 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
1126
1127 entry = community_entry_new ();
1128 entry->direct = direct;
1129 entry->style = style;
1130 entry->any = (str ? 0 : 1);
1131 if (ecom)
1132 entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
1133 else if (regex)
1134 entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
paul718e3742002-12-13 20:15:29 +00001135 else
hassofee6e4e2005-02-02 16:29:31 +00001136 entry->config = NULL;
1137 entry->u.ecom = ecom;
1138 entry->reg = regex;
paul718e3742002-12-13 20:15:29 +00001139
1140 /* Do not put duplicated community entry. */
1141 if (community_list_dup_check (list, entry))
1142 community_entry_free (entry);
1143 else
1144 community_list_entry_add (list, entry);
1145
1146 return 0;
1147}
1148
1149/* Unset extcommunity-list. When str is NULL, delete all of
1150 extcommunity-list entry belongs to the specified name. */
1151int
1152extcommunity_list_unset (struct community_list_handler *ch,
paulfd79ac92004-10-13 05:06:08 +00001153 const char *name, const char *str,
1154 int direct, int style)
paul718e3742002-12-13 20:15:29 +00001155{
hassofee6e4e2005-02-02 16:29:31 +00001156 struct community_entry *entry = NULL;
paul718e3742002-12-13 20:15:29 +00001157 struct community_list *list;
1158 struct ecommunity *ecom = NULL;
hassofee6e4e2005-02-02 16:29:31 +00001159 regex_t *regex = NULL;
paul718e3742002-12-13 20:15:29 +00001160
1161 /* Lookup extcommunity list. */
hassofee6e4e2005-02-02 16:29:31 +00001162 list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
paul718e3742002-12-13 20:15:29 +00001163 if (list == NULL)
1164 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1165
1166 /* Delete all of entry belongs to this extcommunity-list. */
paul8708b742003-06-07 02:03:11 +00001167 if (!str)
paul718e3742002-12-13 20:15:29 +00001168 {
1169 community_list_delete (list);
1170 return 0;
1171 }
1172
hassofee6e4e2005-02-02 16:29:31 +00001173 if (style == EXTCOMMUNITY_LIST_STANDARD)
1174 ecom = ecommunity_str2com (str, 0, 1);
1175 else
1176 regex = bgp_regcomp (str);
paul718e3742002-12-13 20:15:29 +00001177
hassofee6e4e2005-02-02 16:29:31 +00001178 if (! ecom && ! regex)
1179 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
paul718e3742002-12-13 20:15:29 +00001180
hassofee6e4e2005-02-02 16:29:31 +00001181 if (ecom)
1182 entry = community_list_entry_lookup (list, ecom, direct);
1183 else
1184 entry = community_list_entry_lookup (list, str, direct);
1185
1186 if (ecom)
Paul Jakmaf6f434b2010-11-23 21:28:03 +00001187 ecommunity_free (&ecom);
hassofee6e4e2005-02-02 16:29:31 +00001188 if (regex)
1189 bgp_regex_free (regex);
paul718e3742002-12-13 20:15:29 +00001190
paul8708b742003-06-07 02:03:11 +00001191 if (!entry)
paul718e3742002-12-13 20:15:29 +00001192 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1193
1194 community_list_entry_delete (list, entry, style);
1195
1196 return 0;
1197}
1198
1199/* Initializa community-list. Return community-list handler. */
1200struct community_list_handler *
paul94f2b392005-06-28 12:44:16 +00001201community_list_init (void)
paul718e3742002-12-13 20:15:29 +00001202{
1203 struct community_list_handler *ch;
1204 ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
paul8708b742003-06-07 02:03:11 +00001205 sizeof (struct community_list_handler));
paul718e3742002-12-13 20:15:29 +00001206 return ch;
1207}
1208
1209/* Terminate community-list. */
Chris Caputo228da422009-07-18 05:44:03 +00001210void
paul718e3742002-12-13 20:15:29 +00001211community_list_terminate (struct community_list_handler *ch)
1212{
1213 struct community_list_master *cm;
1214 struct community_list *list;
1215
1216 cm = &ch->community_list;
1217 while ((list = cm->num.head) != NULL)
1218 community_list_delete (list);
1219 while ((list = cm->str.head) != NULL)
1220 community_list_delete (list);
1221
Job Snijders3334bab2017-01-20 14:47:12 +00001222 cm = &ch->lcommunity_list;
1223 while ((list = cm->num.head) != NULL)
1224 community_list_delete (list);
1225 while ((list = cm->str.head) != NULL)
1226 community_list_delete (list);
1227
paul718e3742002-12-13 20:15:29 +00001228 cm = &ch->extcommunity_list;
1229 while ((list = cm->num.head) != NULL)
1230 community_list_delete (list);
1231 while ((list = cm->str.head) != NULL)
1232 community_list_delete (list);
1233
1234 XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
1235}