]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_clist.c
bgpd: Strip `delete` keyword when looking up for communities
[mirror_frr.git] / bgpd / bgp_clist.c
CommitLineData
718e3744 1/* BGP community-list and extcommunity-list.
896014f4
DL
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
21#include <zebra.h>
22
23#include "command.h"
24#include "prefix.h"
25#include "memory.h"
3f9c7369 26#include "queue.h"
039f3a34 27#include "filter.h"
937652c6 28#include "stream.h"
3571a6a2 29#include "jhash.h"
5bd66e2d 30#include "frrstr.h"
718e3744 31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_community.h"
34#include "bgpd/bgp_ecommunity.h"
57d187bc 35#include "bgpd/bgp_lcommunity.h"
718e3744 36#include "bgpd/bgp_aspath.h"
37#include "bgpd/bgp_regex.h"
38#include "bgpd/bgp_clist.h"
6b0655a2 39
d8b87afe 40static uint32_t bgp_clist_hash_key_community_list(const void *data)
3571a6a2 41{
d8b87afe 42 struct community_list *cl = (struct community_list *) data;
3571a6a2 43
e237b0d2
DS
44 if (cl->name_hash)
45 return cl->name_hash;
46
47 cl->name_hash = bgp_clist_hash_key(cl->name);
48 return cl->name_hash;
3571a6a2
DS
49}
50
51static bool bgp_clist_hash_cmp_community_list(const void *a1, const void *a2)
52{
53 const struct community_list *cl1 = a1;
54 const struct community_list *cl2 = a2;
55
e237b0d2
DS
56 if (cl1->name_hash != cl2->name_hash)
57 return false;
58
3571a6a2
DS
59 if (strcmp(cl1->name, cl2->name) == 0)
60 return true;
61
62 return false;
63}
64
718e3744 65/* Lookup master structure for community-list or
66 extcommunity-list. */
67struct community_list_master *
d62a17ae 68community_list_master_lookup(struct community_list_handler *ch, int master)
718e3744 69{
d62a17ae 70 if (ch)
71 switch (master) {
72 case COMMUNITY_LIST_MASTER:
73 return &ch->community_list;
74 case EXTCOMMUNITY_LIST_MASTER:
75 return &ch->extcommunity_list;
76 case LARGE_COMMUNITY_LIST_MASTER:
77 return &ch->lcommunity_list;
78 }
79 return NULL;
718e3744 80}
81
82/* Allocate a new community list entry. */
d62a17ae 83static struct community_entry *community_entry_new(void)
718e3744 84{
d62a17ae 85 return XCALLOC(MTYPE_COMMUNITY_LIST_ENTRY,
86 sizeof(struct community_entry));
718e3744 87}
88
89/* Free community list entry. */
d62a17ae 90static void community_entry_free(struct community_entry *entry)
718e3744 91{
d62a17ae 92 switch (entry->style) {
93 case COMMUNITY_LIST_STANDARD:
94 if (entry->u.com)
3c1f53de 95 community_free(&entry->u.com);
d62a17ae 96 break;
97 case LARGE_COMMUNITY_LIST_STANDARD:
98 if (entry->u.lcom)
99 lcommunity_free(&entry->u.lcom);
100 break;
101 case EXTCOMMUNITY_LIST_STANDARD:
102 /* In case of standard extcommunity-list, configuration string
103 is made by ecommunity_ecom2str(). */
0a22ddfb 104 XFREE(MTYPE_ECOMMUNITY_STR, entry->config);
d62a17ae 105 if (entry->u.ecom)
106 ecommunity_free(&entry->u.ecom);
107 break;
108 case COMMUNITY_LIST_EXPANDED:
109 case EXTCOMMUNITY_LIST_EXPANDED:
110 case LARGE_COMMUNITY_LIST_EXPANDED:
0a22ddfb 111 XFREE(MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
d62a17ae 112 if (entry->reg)
113 bgp_regex_free(entry->reg);
114 default:
115 break;
116 }
117 XFREE(MTYPE_COMMUNITY_LIST_ENTRY, entry);
718e3744 118}
119
120/* Allocate a new community-list. */
d62a17ae 121static struct community_list *community_list_new(void)
718e3744 122{
d62a17ae 123 return XCALLOC(MTYPE_COMMUNITY_LIST, sizeof(struct community_list));
718e3744 124}
125
126/* Free community-list. */
d62a17ae 127static void community_list_free(struct community_list *list)
718e3744 128{
0a22ddfb 129 XFREE(MTYPE_COMMUNITY_LIST_NAME, list->name);
d62a17ae 130 XFREE(MTYPE_COMMUNITY_LIST, list);
718e3744 131}
132
94f2b392 133static struct community_list *
d62a17ae 134community_list_insert(struct community_list_handler *ch, const char *name,
135 int master)
718e3744 136{
d62a17ae 137 size_t i;
138 long number;
139 struct community_list *new;
140 struct community_list *point;
141 struct community_list_list *list;
142 struct community_list_master *cm;
143
144 /* Lookup community-list master. */
145 cm = community_list_master_lookup(ch, master);
146 if (!cm)
147 return NULL;
148
149 /* Allocate new community_list and copy given name. */
150 new = community_list_new();
151 new->name = XSTRDUP(MTYPE_COMMUNITY_LIST_NAME, name);
e237b0d2 152 new->name_hash = bgp_clist_hash_key_community_list(new);
d62a17ae 153
3571a6a2
DS
154 /* Save for later */
155 hash_get(cm->hash, new, hash_alloc_intern);
156
d62a17ae 157 /* If name is made by all digit character. We treat it as
158 number. */
159 for (number = 0, i = 0; i < strlen(name); i++) {
160 if (isdigit((int)name[i]))
161 number = (number * 10) + (name[i] - '0');
162 else
163 break;
164 }
718e3744 165
d62a17ae 166 /* In case of name is all digit character */
167 if (i == strlen(name)) {
168 new->sort = COMMUNITY_LIST_NUMBER;
718e3744 169
d62a17ae 170 /* Set access_list to number list. */
171 list = &cm->num;
718e3744 172
d62a17ae 173 for (point = list->head; point; point = point->next)
174 if (atol(point->name) >= number)
175 break;
176 } else {
177 new->sort = COMMUNITY_LIST_STRING;
718e3744 178
d62a17ae 179 /* Set access_list to string list. */
180 list = &cm->str;
718e3744 181
d62a17ae 182 /* Set point to insertion point. */
183 for (point = list->head; point; point = point->next)
184 if (strcmp(point->name, name) >= 0)
185 break;
186 }
718e3744 187
d62a17ae 188 /* Link to upper list. */
189 new->parent = list;
718e3744 190
d62a17ae 191 /* In case of this is the first element of master. */
192 if (list->head == NULL) {
193 list->head = list->tail = new;
194 return new;
195 }
718e3744 196
d62a17ae 197 /* In case of insertion is made at the tail of access_list. */
198 if (point == NULL) {
199 new->prev = list->tail;
200 list->tail->next = new;
201 list->tail = new;
202 return new;
203 }
718e3744 204
d62a17ae 205 /* In case of insertion is made at the head of access_list. */
206 if (point == list->head) {
207 new->next = list->head;
208 list->head->prev = new;
209 list->head = new;
210 return new;
211 }
718e3744 212
d62a17ae 213 /* Insertion is made at middle of the access_list. */
214 new->next = point;
215 new->prev = point->prev;
718e3744 216
d62a17ae 217 if (point->prev)
218 point->prev->next = new;
219 point->prev = new;
718e3744 220
d62a17ae 221 return new;
718e3744 222}
223
d62a17ae 224struct community_list *community_list_lookup(struct community_list_handler *ch,
e237b0d2
DS
225 const char *name,
226 uint32_t name_hash,
227 int master)
718e3744 228{
3571a6a2 229 struct community_list lookup;
d62a17ae 230 struct community_list_master *cm;
718e3744 231
d62a17ae 232 if (!name)
233 return NULL;
718e3744 234
d62a17ae 235 cm = community_list_master_lookup(ch, master);
236 if (!cm)
237 return NULL;
718e3744 238
3571a6a2 239 lookup.name = (char *)name;
e237b0d2 240 lookup.name_hash = name_hash;
3571a6a2 241 return hash_get(cm->hash, &lookup, NULL);
718e3744 242}
243
94f2b392 244static struct community_list *
d62a17ae 245community_list_get(struct community_list_handler *ch, const char *name,
246 int master)
718e3744 247{
d62a17ae 248 struct community_list *list;
718e3744 249
e237b0d2 250 list = community_list_lookup(ch, name, 0, master);
d62a17ae 251 if (!list)
252 list = community_list_insert(ch, name, master);
253 return list;
718e3744 254}
255
3571a6a2
DS
256static void community_list_delete(struct community_list_master *cm,
257 struct community_list *list)
718e3744 258{
d62a17ae 259 struct community_list_list *clist;
260 struct community_entry *entry, *next;
718e3744 261
d62a17ae 262 for (entry = list->head; entry; entry = next) {
263 next = entry->next;
264 community_entry_free(entry);
265 }
718e3744 266
d62a17ae 267 clist = list->parent;
718e3744 268
d62a17ae 269 if (list->next)
270 list->next->prev = list->prev;
271 else
272 clist->tail = list->prev;
718e3744 273
d62a17ae 274 if (list->prev)
275 list->prev->next = list->next;
276 else
277 clist->head = list->next;
718e3744 278
3571a6a2 279 hash_release(cm->hash, list);
d62a17ae 280 community_list_free(list);
718e3744 281}
282
d62a17ae 283static int community_list_empty_p(struct community_list *list)
718e3744 284{
d62a17ae 285 return (list->head == NULL && list->tail == NULL) ? 1 : 0;
718e3744 286}
6b0655a2 287
718e3744 288/* Add community-list entry to the list. */
d62a17ae 289static void community_list_entry_add(struct community_list *list,
290 struct community_entry *entry)
718e3744 291{
d62a17ae 292 entry->next = NULL;
293 entry->prev = list->tail;
294
295 if (list->tail)
296 list->tail->next = entry;
297 else
298 list->head = entry;
299 list->tail = entry;
718e3744 300}
301
302/* Delete community-list entry from the list. */
3571a6a2
DS
303static void community_list_entry_delete(struct community_list_master *cm,
304 struct community_list *list,
3f54c705 305 struct community_entry *entry)
718e3744 306{
d62a17ae 307 if (entry->next)
308 entry->next->prev = entry->prev;
309 else
310 list->tail = entry->prev;
718e3744 311
d62a17ae 312 if (entry->prev)
313 entry->prev->next = entry->next;
314 else
315 list->head = entry->next;
718e3744 316
d62a17ae 317 community_entry_free(entry);
718e3744 318
d62a17ae 319 if (community_list_empty_p(list))
3571a6a2 320 community_list_delete(cm, list);
718e3744 321}
322
323/* Lookup community-list entry from the list. */
324static struct community_entry *
d62a17ae 325community_list_entry_lookup(struct community_list *list, const void *arg,
326 int direct)
718e3744 327{
d62a17ae 328 struct community_entry *entry;
329
330 for (entry = list->head; entry; entry = entry->next) {
331 switch (entry->style) {
332 case COMMUNITY_LIST_STANDARD:
333 if (entry->direct == direct
334 && community_cmp(entry->u.com, arg))
335 return entry;
336 break;
337 case EXTCOMMUNITY_LIST_STANDARD:
338 if (entry->direct == direct
339 && ecommunity_cmp(entry->u.ecom, arg))
340 return entry;
341 break;
342 case LARGE_COMMUNITY_LIST_STANDARD:
343 if (entry->direct == direct
344 && lcommunity_cmp(entry->u.lcom, arg))
345 return entry;
346 break;
347 case COMMUNITY_LIST_EXPANDED:
348 case EXTCOMMUNITY_LIST_EXPANDED:
349 case LARGE_COMMUNITY_LIST_EXPANDED:
350 if (entry->direct == direct
351 && strcmp(entry->config, arg) == 0)
352 return entry;
353 break;
354 default:
355 break;
356 }
357 }
358 return NULL;
718e3744 359}
6b0655a2 360
d62a17ae 361static char *community_str_get(struct community *com, int i)
5cbea288 362{
d7c0a89a
QY
363 uint32_t comval;
364 uint16_t as;
365 uint16_t val;
d62a17ae 366 char *str;
d62a17ae 367
d7c0a89a 368 memcpy(&comval, com_nthval(com, i), sizeof(uint32_t));
d62a17ae 369 comval = ntohl(comval);
370
371 switch (comval) {
372 case COMMUNITY_INTERNET:
aeab4a80 373 str = XSTRDUP(MTYPE_COMMUNITY_STR, "internet");
d62a17ae 374 break;
aa861c10 375 case COMMUNITY_GSHUT:
aeab4a80 376 str = XSTRDUP(MTYPE_COMMUNITY_STR, "graceful-shutdown");
aa861c10
C
377 break;
378 case COMMUNITY_ACCEPT_OWN:
aeab4a80 379 str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own");
aa861c10
C
380 break;
381 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4:
aeab4a80
QY
382 str = XSTRDUP(MTYPE_COMMUNITY_STR,
383 "route-filter-translated-v4");
aa861c10
C
384 break;
385 case COMMUNITY_ROUTE_FILTER_v4:
aeab4a80 386 str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v4");
aa861c10
C
387 break;
388 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6:
aeab4a80
QY
389 str = XSTRDUP(MTYPE_COMMUNITY_STR,
390 "route-filter-translated-v6");
aa861c10
C
391 break;
392 case COMMUNITY_ROUTE_FILTER_v6:
aeab4a80 393 str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v6");
aa861c10
C
394 break;
395 case COMMUNITY_LLGR_STALE:
aeab4a80 396 str = XSTRDUP(MTYPE_COMMUNITY_STR, "llgr-stale");
aa861c10
C
397 break;
398 case COMMUNITY_NO_LLGR:
aeab4a80 399 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-llgr");
aa861c10
C
400 break;
401 case COMMUNITY_ACCEPT_OWN_NEXTHOP:
aeab4a80 402 str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own-nexthop");
aa861c10
C
403 break;
404 case COMMUNITY_BLACKHOLE:
aeab4a80 405 str = XSTRDUP(MTYPE_COMMUNITY_STR, "blackhole");
aa861c10 406 break;
d62a17ae 407 case COMMUNITY_NO_EXPORT:
aeab4a80 408 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-export");
d62a17ae 409 break;
410 case COMMUNITY_NO_ADVERTISE:
aeab4a80 411 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-advertise");
d62a17ae 412 break;
413 case COMMUNITY_LOCAL_AS:
aeab4a80 414 str = XSTRDUP(MTYPE_COMMUNITY_STR, "local-AS");
d62a17ae 415 break;
aa861c10 416 case COMMUNITY_NO_PEER:
aeab4a80 417 str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-peer");
7f323236 418 break;
809d6365 419 default:
aeab4a80 420 str = XSTRDUP(MTYPE_COMMUNITY_STR, "65536:65535");
d62a17ae 421 as = (comval >> 16) & 0xFFFF;
422 val = comval & 0xFFFF;
aeab4a80 423 snprintf(str, strlen(str), "%u:%d", as, val);
d62a17ae 424 break;
425 }
5cbea288 426
d62a17ae 427 return str;
5cbea288
DS
428}
429
430/* Internal function to perform regular expression match for
2acb4ac2 431 * a single community. */
d62a17ae 432static int community_regexp_include(regex_t *reg, struct community *com, int i)
5cbea288 433{
d62a17ae 434 char *str;
435 int rv;
5cbea288 436
d62a17ae 437 /* When there is no communities attribute it is treated as empty string.
438 */
439 if (com == NULL || com->size == 0)
440 str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
441 else
442 str = community_str_get(com, i);
5cbea288 443
d62a17ae 444 /* Regular expression match. */
445 rv = regexec(reg, str, 0, NULL, 0);
b84ee83b 446
d62a17ae 447 XFREE(MTYPE_COMMUNITY_STR, str);
b84ee83b 448
d62a17ae 449 if (rv == 0)
450 return 1;
5cbea288 451
d62a17ae 452 /* No match. */
453 return 0;
5cbea288
DS
454}
455
718e3744 456/* Internal function to perform regular expression match for community
457 attribute. */
d62a17ae 458static int community_regexp_match(struct community *com, regex_t *reg)
718e3744 459{
d62a17ae 460 const char *str;
718e3744 461
d62a17ae 462 /* When there is no communities attribute it is treated as empty
463 string. */
464 if (com == NULL || com->size == 0)
465 str = "";
466 else
a69ea8ae 467 str = community_str(com, false);
718e3744 468
d62a17ae 469 /* Regular expression match. */
470 if (regexec(reg, str, 0, NULL, 0) == 0)
471 return 1;
718e3744 472
d62a17ae 473 /* No match. */
474 return 0;
718e3744 475}
476
d62a17ae 477static char *lcommunity_str_get(struct lcommunity *lcom, int i)
57d187bc 478{
d62a17ae 479 struct lcommunity_val lcomval;
d7c0a89a
QY
480 uint32_t globaladmin;
481 uint32_t localdata1;
482 uint32_t localdata2;
d62a17ae 483 char *str;
d7c0a89a 484 uint8_t *ptr;
d62a17ae 485 char *pnt;
486
ff9104a2 487 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
d62a17ae 488
489 memcpy(&lcomval, ptr, LCOMMUNITY_SIZE);
490
491 /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */
492 str = pnt = XMALLOC(MTYPE_LCOMMUNITY_STR, 48);
493
d7c0a89a 494 ptr = (uint8_t *)lcomval.val;
937652c6
DL
495 ptr = ptr_get_be32(ptr, &globaladmin);
496 ptr = ptr_get_be32(ptr, &localdata1);
497 ptr = ptr_get_be32(ptr, &localdata2);
498 (void)ptr; /* consume value */
d62a17ae 499
500 sprintf(pnt, "%u:%u:%u", globaladmin, localdata1, localdata2);
501 pnt += strlen(pnt);
502 *pnt = '\0';
503
504 return str;
57d187bc
JS
505}
506
507/* Internal function to perform regular expression match for
2acb4ac2 508 * a single community. */
d62a17ae 509static int lcommunity_regexp_include(regex_t *reg, struct lcommunity *lcom,
510 int i)
57d187bc 511{
d62a17ae 512 char *str;
513
514 /* When there is no communities attribute it is treated as empty string.
515 */
516 if (lcom == NULL || lcom->size == 0)
517 str = XSTRDUP(MTYPE_LCOMMUNITY_STR, "");
518 else
519 str = lcommunity_str_get(lcom, i);
520
521 /* Regular expression match. */
522 if (regexec(reg, str, 0, NULL, 0) == 0) {
523 XFREE(MTYPE_LCOMMUNITY_STR, str);
524 return 1;
525 }
57d187bc 526
d62a17ae 527 XFREE(MTYPE_LCOMMUNITY_STR, str);
528 /* No match. */
529 return 0;
57d187bc
JS
530}
531
d62a17ae 532static int lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
57d187bc 533{
d62a17ae 534 const char *str;
57d187bc 535
d62a17ae 536 /* When there is no communities attribute it is treated as empty
537 string. */
538 if (com == NULL || com->size == 0)
539 str = "";
540 else
8d9b8ed9 541 str = lcommunity_str(com, false);
57d187bc 542
d62a17ae 543 /* Regular expression match. */
544 if (regexec(reg, str, 0, NULL, 0) == 0)
545 return 1;
57d187bc 546
d62a17ae 547 /* No match. */
548 return 0;
57d187bc
JS
549}
550
551
d62a17ae 552static int ecommunity_regexp_match(struct ecommunity *ecom, regex_t *reg)
8708b74f 553{
d62a17ae 554 const char *str;
8708b74f 555
d62a17ae 556 /* When there is no communities attribute it is treated as empty
557 string. */
558 if (ecom == NULL || ecom->size == 0)
559 str = "";
560 else
561 str = ecommunity_str(ecom);
8708b74f 562
d62a17ae 563 /* Regular expression match. */
564 if (regexec(reg, str, 0, NULL, 0) == 0)
565 return 1;
8708b74f 566
d62a17ae 567 /* No match. */
568 return 0;
8708b74f 569}
570
ffd0c037 571#if 0
718e3744 572/* Delete community attribute using regular expression match. Return
573 modified communites attribute. */
574static struct community *
8708b74f 575community_regexp_delete (struct community *com, regex_t * reg)
718e3744 576{
aa861c10
C
577 int i;
578 uint32_t comval;
579 /* Maximum is "65535:65535" + '\0'. */
580 char c[12];
581 const char *str;
582
583 if (!com)
584 return NULL;
585
586 i = 0;
587 while (i < com->size)
588 {
589 memcpy (&comval, com_nthval (com, i), sizeof (uint32_t));
590 comval = ntohl (comval);
591
592 switch (comval) {
593 case COMMUNITY_INTERNET:
594 str = "internet";
595 break;
596 case COMMUNITY_ACCEPT_OWN:
597 str = "accept-own";
598 break;
599 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4:
600 str = "route-filter-translated-v4";
601 break;
602 case COMMUNITY_ROUTE_FILTER_v4:
603 str = "route-filter-v4";
604 break;
605 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6:
606 str = "route-filter-translated-v6";
607 break;
608 case COMMUNITY_ROUTE_FILTER_v6:
609 str = "route-filter-v6";
610 break;
611 case COMMUNITY_LLGR_STALE:
612 str = "llgr-stale";
613 break;
614 case COMMUNITY_NO_LLGR:
615 str = "no-llgr";
616 break;
617 case COMMUNITY_ACCEPT_OWN_NEXTHOP:
618 str = "accept-own-nexthop";
619 break;
620 case COMMUNITY_BLACKHOLE:
621 str = "blackhole";
622 break;
623 case COMMUNITY_NO_EXPORT:
624 str = "no-export";
625 break;
626 case COMMUNITY_NO_ADVERTISE:
627 str = "no-advertise";
628 break;
629 case COMMUNITY_LOCAL_AS:
630 str = "local-AS";
631 break;
632 case COMMUNITY_NO_PEER:
633 str = "no-peer";
634 break;
635 default:
636 sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF,
637 comval & 0xFFFF);
638 str = c;
639 break;
640 }
641
642 if (regexec (reg, str, 0, NULL, 0) == 0)
643 community_del_val (com, com_nthval (com, i));
644 else
645 i++;
646 }
647 return com;
718e3744 648}
ffd0c037 649#endif
718e3744 650
651/* When given community attribute matches to the community-list return
652 1 else return 0. */
d62a17ae 653int community_list_match(struct community *com, struct community_list *list)
718e3744 654{
d62a17ae 655 struct community_entry *entry;
656
657 for (entry = list->head; entry; entry = entry->next) {
658 if (entry->any)
659 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
660
661 if (entry->style == COMMUNITY_LIST_STANDARD) {
662 if (community_include(entry->u.com, COMMUNITY_INTERNET))
663 return entry->direct == COMMUNITY_PERMIT ? 1
664 : 0;
665
666 if (community_match(com, entry->u.com))
667 return entry->direct == COMMUNITY_PERMIT ? 1
668 : 0;
669 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
670 if (community_regexp_match(com, entry->reg))
671 return entry->direct == COMMUNITY_PERMIT ? 1
672 : 0;
673 }
674 }
675 return 0;
8708b74f 676}
677
d62a17ae 678int lcommunity_list_match(struct lcommunity *lcom, struct community_list *list)
57d187bc 679{
d62a17ae 680 struct community_entry *entry;
681
682 for (entry = list->head; entry; entry = entry->next) {
683 if (entry->any)
684 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
685
686 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
687 if (lcommunity_match(lcom, entry->u.lcom))
688 return entry->direct == COMMUNITY_PERMIT ? 1
689 : 0;
690 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
691 if (lcommunity_regexp_match(lcom, entry->reg))
692 return entry->direct == COMMUNITY_PERMIT ? 1
693 : 0;
694 }
695 }
696 return 0;
57d187bc
JS
697}
698
f8463998 699
700/* Perform exact matching. In case of expanded large-community-list, do
701 * same thing as lcommunity_list_match().
702 */
703int lcommunity_list_exact_match(struct lcommunity *lcom,
704 struct community_list *list)
705{
706 struct community_entry *entry;
707
708 for (entry = list->head; entry; entry = entry->next) {
709 if (entry->any)
710 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
711
712 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
713 if (lcommunity_cmp(lcom, entry->u.com))
714 return entry->direct == COMMUNITY_PERMIT ? 1
715 : 0;
716 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
717 if (lcommunity_regexp_match(lcom, entry->reg))
718 return entry->direct == COMMUNITY_PERMIT ? 1
719 : 0;
720 }
721 }
722 return 0;
723}
724
d62a17ae 725int ecommunity_list_match(struct ecommunity *ecom, struct community_list *list)
8708b74f 726{
d62a17ae 727 struct community_entry *entry;
728
729 for (entry = list->head; entry; entry = entry->next) {
730 if (entry->any)
731 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
732
733 if (entry->style == EXTCOMMUNITY_LIST_STANDARD) {
734 if (ecommunity_match(ecom, entry->u.ecom))
735 return entry->direct == COMMUNITY_PERMIT ? 1
736 : 0;
737 } else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) {
738 if (ecommunity_regexp_match(ecom, entry->reg))
739 return entry->direct == COMMUNITY_PERMIT ? 1
740 : 0;
741 }
742 }
743 return 0;
718e3744 744}
745
746/* Perform exact matching. In case of expanded community-list, do
747 same thing as community_list_match(). */
d62a17ae 748int community_list_exact_match(struct community *com,
749 struct community_list *list)
718e3744 750{
d62a17ae 751 struct community_entry *entry;
752
753 for (entry = list->head; entry; entry = entry->next) {
754 if (entry->any)
755 return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
756
757 if (entry->style == COMMUNITY_LIST_STANDARD) {
758 if (community_include(entry->u.com, COMMUNITY_INTERNET))
759 return entry->direct == COMMUNITY_PERMIT ? 1
760 : 0;
761
762 if (community_cmp(com, entry->u.com))
763 return entry->direct == COMMUNITY_PERMIT ? 1
764 : 0;
765 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
766 if (community_regexp_match(com, entry->reg))
767 return entry->direct == COMMUNITY_PERMIT ? 1
768 : 0;
769 }
770 }
771 return 0;
718e3744 772}
773
8708b74f 774/* Delete all permitted communities in the list from com. */
d62a17ae 775struct community *community_list_match_delete(struct community *com,
776 struct community_list *list)
718e3744 777{
d62a17ae 778 struct community_entry *entry;
d7c0a89a
QY
779 uint32_t val;
780 uint32_t com_index_to_delete[com->size];
d62a17ae 781 int delete_index = 0;
782 int i;
783
784 /* Loop over each community value and evaluate each against the
785 * community-list. If we need to delete a community value add its index
ceead39c 786 * to com_index_to_delete.
d62a17ae 787 */
788 for (i = 0; i < com->size; i++) {
789 val = community_val_get(com, i);
790
791 for (entry = list->head; entry; entry = entry->next) {
792 if (entry->any) {
793 if (entry->direct == COMMUNITY_PERMIT) {
794 com_index_to_delete[delete_index] = i;
795 delete_index++;
796 }
797 break;
798 }
799
800 else if ((entry->style == COMMUNITY_LIST_STANDARD)
801 && (community_include(entry->u.com,
802 COMMUNITY_INTERNET)
803 || community_include(entry->u.com, val))) {
804 if (entry->direct == COMMUNITY_PERMIT) {
805 com_index_to_delete[delete_index] = i;
806 delete_index++;
807 }
808 break;
809 }
810
811 else if ((entry->style == COMMUNITY_LIST_EXPANDED)
812 && community_regexp_include(entry->reg, com,
813 i)) {
814 if (entry->direct == COMMUNITY_PERMIT) {
815 com_index_to_delete[delete_index] = i;
816 delete_index++;
817 }
818 break;
819 }
820 }
821 }
5cbea288 822
d62a17ae 823 /* Delete all of the communities we flagged for deletion */
824 for (i = delete_index - 1; i >= 0; i--) {
825 val = community_val_get(com, com_index_to_delete[i]);
826 community_del_val(com, &val);
827 }
5cbea288 828
d62a17ae 829 return com;
718e3744 830}
831
832/* To avoid duplicated entry in the community-list, this function
833 compares specified entry to existing entry. */
d62a17ae 834static int community_list_dup_check(struct community_list *list,
835 struct community_entry *new)
718e3744 836{
d62a17ae 837 struct community_entry *entry;
838
839 for (entry = list->head; entry; entry = entry->next) {
840 if (entry->style != new->style)
841 continue;
842
843 if (entry->direct != new->direct)
844 continue;
845
846 if (entry->any != new->any)
847 continue;
848
849 if (entry->any)
850 return 1;
851
852 switch (entry->style) {
853 case COMMUNITY_LIST_STANDARD:
854 if (community_cmp(entry->u.com, new->u.com))
855 return 1;
856 break;
857 case LARGE_COMMUNITY_LIST_STANDARD:
858 if (lcommunity_cmp(entry->u.lcom, new->u.lcom))
859 return 1;
860 break;
861 case EXTCOMMUNITY_LIST_STANDARD:
862 if (ecommunity_cmp(entry->u.ecom, new->u.ecom))
863 return 1;
864 break;
865 case COMMUNITY_LIST_EXPANDED:
866 case EXTCOMMUNITY_LIST_EXPANDED:
867 case LARGE_COMMUNITY_LIST_EXPANDED:
868 if (strcmp(entry->config, new->config) == 0)
869 return 1;
870 break;
871 default:
872 break;
873 }
874 }
875 return 0;
718e3744 876}
6b0655a2 877
718e3744 878/* Set community-list. */
d62a17ae 879int community_list_set(struct community_list_handler *ch, const char *name,
880 const char *str, int direct, int style)
718e3744 881{
d62a17ae 882 struct community_entry *entry = NULL;
883 struct community_list *list;
884 struct community *com = NULL;
885 regex_t *regex = NULL;
886
887 /* Get community list. */
888 list = community_list_get(ch, name, COMMUNITY_LIST_MASTER);
889
890 /* When community-list already has entry, new entry should have same
891 style. If you want to have mixed style community-list, you can
892 comment out this check. */
893 if (!community_list_empty_p(list)) {
894 struct community_entry *first;
895
896 first = list->head;
897
898 if (style != first->style) {
899 return (first->style == COMMUNITY_LIST_STANDARD
900 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
901 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
902 }
fee6e4e4 903 }
718e3744 904
d62a17ae 905 if (str) {
906 if (style == COMMUNITY_LIST_STANDARD)
907 com = community_str2com(str);
908 else
909 regex = bgp_regcomp(str);
8708b74f 910
d62a17ae 911 if (!com && !regex)
912 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
913 }
718e3744 914
d62a17ae 915 entry = community_entry_new();
916 entry->direct = direct;
917 entry->style = style;
918 entry->any = (str ? 0 : 1);
919 entry->u.com = com;
920 entry->reg = regex;
921 entry->config =
922 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
923
924 /* Do not put duplicated community entry. */
925 if (community_list_dup_check(list, entry))
926 community_entry_free(entry);
927 else {
928 community_list_entry_add(list, entry);
929 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
930 }
718e3744 931
d62a17ae 932 return 0;
718e3744 933}
934
813d4307 935/* Unset community-list */
d62a17ae 936int community_list_unset(struct community_list_handler *ch, const char *name,
7298a8e1 937 const char *str, int direct, int style)
718e3744 938{
3571a6a2 939 struct community_list_master *cm = NULL;
d62a17ae 940 struct community_entry *entry = NULL;
941 struct community_list *list;
942 struct community *com = NULL;
943
944 /* Lookup community list. */
e237b0d2 945 list = community_list_lookup(ch, name, 0, COMMUNITY_LIST_MASTER);
d62a17ae 946 if (list == NULL)
947 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
948
3571a6a2 949 cm = community_list_master_lookup(ch, COMMUNITY_LIST_MASTER);
d62a17ae 950 /* Delete all of entry belongs to this community-list. */
7298a8e1 951 if (!str) {
3571a6a2 952 community_list_delete(cm, list);
d62a17ae 953 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
954 return 0;
955 }
718e3744 956
7298a8e1
QY
957 if (style == COMMUNITY_LIST_STANDARD)
958 com = community_str2com(str);
718e3744 959
d62a17ae 960 if (com) {
961 entry = community_list_entry_lookup(list, com, direct);
3c1f53de 962 community_free(&com);
d62a17ae 963 } else
964 entry = community_list_entry_lookup(list, str, direct);
fee6e4e4 965
d62a17ae 966 if (!entry)
967 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
718e3744 968
3571a6a2 969 community_list_entry_delete(cm, list, entry);
d62a17ae 970 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 971
d62a17ae 972 return 0;
718e3744 973}
974
57d187bc 975/* Delete all permitted large communities in the list from com. */
d62a17ae 976struct lcommunity *lcommunity_list_match_delete(struct lcommunity *lcom,
977 struct community_list *list)
57d187bc 978{
d62a17ae 979 struct community_entry *entry;
d7c0a89a
QY
980 uint32_t com_index_to_delete[lcom->size];
981 uint8_t *ptr;
d62a17ae 982 int delete_index = 0;
983 int i;
984
985 /* Loop over each lcommunity value and evaluate each against the
986 * community-list. If we need to delete a community value add its index
ceead39c 987 * to com_index_to_delete.
d62a17ae 988 */
d62a17ae 989 for (i = 0; i < lcom->size; i++) {
534fc195 990 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
d62a17ae 991 for (entry = list->head; entry; entry = entry->next) {
992 if (entry->any) {
993 if (entry->direct == COMMUNITY_PERMIT) {
994 com_index_to_delete[delete_index] = i;
995 delete_index++;
996 }
997 break;
998 }
999
1000 else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD)
1001 && lcommunity_include(entry->u.lcom, ptr)) {
1002 if (entry->direct == COMMUNITY_PERMIT) {
1003 com_index_to_delete[delete_index] = i;
1004 delete_index++;
1005 }
1006 break;
1007 }
1008
cde8d696 1009 else if ((entry->style == LARGE_COMMUNITY_LIST_EXPANDED)
d62a17ae 1010 && lcommunity_regexp_include(entry->reg, lcom,
1011 i)) {
1012 if (entry->direct == COMMUNITY_PERMIT) {
1013 com_index_to_delete[delete_index] = i;
1014 delete_index++;
1015 }
1016 break;
1017 }
1018 }
1019 }
57d187bc 1020
d62a17ae 1021 /* Delete all of the communities we flagged for deletion */
d62a17ae 1022 for (i = delete_index - 1; i >= 0; i--) {
0a7dce9b 1023 ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE);
d62a17ae 1024 lcommunity_del_val(lcom, ptr);
1025 }
57d187bc 1026
d62a17ae 1027 return lcom;
57d187bc
JS
1028}
1029
5bd66e2d
DA
1030/* Helper to check if every octet do not exceed UINT_MAX */
1031static int lcommunity_list_valid(const char *community)
1032{
1033 int octets = 0;
1034 char **splits;
1035 int num;
1036
1037 frrstr_split(community, ":", &splits, &num);
1038
1039 for (int i = 0; i < num; i++) {
1040 if (strtoul(splits[i], NULL, 10) > UINT_MAX)
1041 return 0;
1042
1043 if (strlen(splits[i]) == 0)
1044 return 0;
1045
1046 octets++;
1047 XFREE(MTYPE_TMP, splits[i]);
1048 }
1049 XFREE(MTYPE_TMP, splits);
1050
1051 if (octets < 3)
1052 return 0;
1053
1054 return 1;
1055}
1056
57d187bc 1057/* Set lcommunity-list. */
d62a17ae 1058int lcommunity_list_set(struct community_list_handler *ch, const char *name,
1059 const char *str, int direct, int style)
57d187bc 1060{
d62a17ae 1061 struct community_entry *entry = NULL;
1062 struct community_list *list;
1063 struct lcommunity *lcom = NULL;
1064 regex_t *regex = NULL;
1065
1066 /* Get community list. */
1067 list = community_list_get(ch, name, LARGE_COMMUNITY_LIST_MASTER);
1068
1069 /* When community-list already has entry, new entry should have same
1070 style. If you want to have mixed style community-list, you can
1071 comment out this check. */
1072 if (!community_list_empty_p(list)) {
1073 struct community_entry *first;
1074
1075 first = list->head;
1076
1077 if (style != first->style) {
1078 return (first->style == COMMUNITY_LIST_STANDARD
1079 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1080 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1081 }
1082 }
57d187bc 1083
d62a17ae 1084 if (str) {
5bd66e2d
DA
1085 if (!lcommunity_list_valid(str))
1086 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1087
d62a17ae 1088 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1089 lcom = lcommunity_str2com(str);
1090 else
1091 regex = bgp_regcomp(str);
57d187bc 1092
d62a17ae 1093 if (!lcom && !regex)
1094 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1095 }
57d187bc 1096
d62a17ae 1097 entry = community_entry_new();
1098 entry->direct = direct;
1099 entry->style = style;
1100 entry->any = (str ? 0 : 1);
1101 entry->u.lcom = lcom;
1102 entry->reg = regex;
8d9b8ed9
PM
1103 entry->config =
1104 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
d62a17ae 1105
1106 /* Do not put duplicated community entry. */
1107 if (community_list_dup_check(list, entry))
1108 community_entry_free(entry);
35f6f850 1109 else {
d62a17ae 1110 community_list_entry_add(list, entry);
35f6f850 1111 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_ADDED);
1112 }
d62a17ae 1113
1114 return 0;
57d187bc
JS
1115}
1116
1117/* Unset community-list. When str is NULL, delete all of
1118 community-list entry belongs to the specified name. */
d62a17ae 1119int lcommunity_list_unset(struct community_list_handler *ch, const char *name,
1120 const char *str, int direct, int style)
57d187bc 1121{
3571a6a2 1122 struct community_list_master *cm = NULL;
d62a17ae 1123 struct community_entry *entry = NULL;
1124 struct community_list *list;
1125 struct lcommunity *lcom = NULL;
1126 regex_t *regex = NULL;
1127
1128 /* Lookup community list. */
e237b0d2 1129 list = community_list_lookup(ch, name, 0, LARGE_COMMUNITY_LIST_MASTER);
d62a17ae 1130 if (list == NULL)
1131 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1132
3571a6a2 1133 cm = community_list_master_lookup(ch, LARGE_COMMUNITY_LIST_MASTER);
d62a17ae 1134 /* Delete all of entry belongs to this community-list. */
1135 if (!str) {
3571a6a2 1136 community_list_delete(cm, list);
35f6f850 1137 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
d62a17ae 1138 return 0;
1139 }
57d187bc 1140
d62a17ae 1141 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1142 lcom = lcommunity_str2com(str);
1143 else
1144 regex = bgp_regcomp(str);
57d187bc 1145
d62a17ae 1146 if (!lcom && !regex)
1147 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
57d187bc 1148
d62a17ae 1149 if (lcom)
1150 entry = community_list_entry_lookup(list, lcom, direct);
1151 else
1152 entry = community_list_entry_lookup(list, str, direct);
57d187bc 1153
d62a17ae 1154 if (lcom)
1155 lcommunity_free(&lcom);
1156 if (regex)
1157 bgp_regex_free(regex);
57d187bc 1158
d62a17ae 1159 if (!entry)
1160 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
57d187bc 1161
3571a6a2 1162 community_list_entry_delete(cm, list, entry);
35f6f850 1163 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
57d187bc 1164
d62a17ae 1165 return 0;
57d187bc
JS
1166}
1167
718e3744 1168/* Set extcommunity-list. */
d62a17ae 1169int extcommunity_list_set(struct community_list_handler *ch, const char *name,
1170 const char *str, int direct, int style)
718e3744 1171{
d62a17ae 1172 struct community_entry *entry = NULL;
1173 struct community_list *list;
1174 struct ecommunity *ecom = NULL;
1175 regex_t *regex = NULL;
718e3744 1176
fa301630 1177 if (str == NULL)
1178 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1179
d62a17ae 1180 /* Get community list. */
1181 list = community_list_get(ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 1182
d62a17ae 1183 /* When community-list already has entry, new entry should have same
1184 style. If you want to have mixed style community-list, you can
1185 comment out this check. */
1186 if (!community_list_empty_p(list)) {
1187 struct community_entry *first;
718e3744 1188
d62a17ae 1189 first = list->head;
718e3744 1190
d62a17ae 1191 if (style != first->style) {
1192 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1193 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1194 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1195 }
fee6e4e4 1196 }
718e3744 1197
d62a17ae 1198 if (style == EXTCOMMUNITY_LIST_STANDARD)
1199 ecom = ecommunity_str2com(str, 0, 1);
1200 else
1201 regex = bgp_regcomp(str);
1202
1203 if (!ecom && !regex)
1204 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1205
1206 if (ecom)
1207 ecom->str =
1208 ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
1209
1210 entry = community_entry_new();
1211 entry->direct = direct;
1212 entry->style = style;
fa301630 1213 entry->any = 0;
d62a17ae 1214 if (ecom)
1215 entry->config = ecommunity_ecom2str(
1216 ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0);
1217 else if (regex)
1218 entry->config = XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str);
1219
1220 entry->u.ecom = ecom;
1221 entry->reg = regex;
1222
1223 /* Do not put duplicated community entry. */
1224 if (community_list_dup_check(list, entry))
1225 community_entry_free(entry);
1226 else {
1227 community_list_entry_add(list, entry);
1228 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1229 }
718e3744 1230
d62a17ae 1231 return 0;
718e3744 1232}
1233
7298a8e1
QY
1234/* Unset extcommunity-list.
1235 *
1236 * When str is NULL, delete all extcommunity-list entries belonging to the
1237 * specified name.
1238 */
d62a17ae 1239int extcommunity_list_unset(struct community_list_handler *ch, const char *name,
7298a8e1 1240 const char *str, int direct, int style)
718e3744 1241{
3571a6a2 1242 struct community_list_master *cm = NULL;
d62a17ae 1243 struct community_entry *entry = NULL;
1244 struct community_list *list;
1245 struct ecommunity *ecom = NULL;
1246
1247 /* Lookup extcommunity list. */
e237b0d2 1248 list = community_list_lookup(ch, name, 0, EXTCOMMUNITY_LIST_MASTER);
d62a17ae 1249 if (list == NULL)
1250 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1251
3571a6a2 1252 cm = community_list_master_lookup(ch, EXTCOMMUNITY_LIST_MASTER);
d62a17ae 1253 /* Delete all of entry belongs to this extcommunity-list. */
7298a8e1 1254 if (!str) {
3571a6a2 1255 community_list_delete(cm, list);
d62a17ae 1256 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1257 return 0;
1258 }
718e3744 1259
7298a8e1
QY
1260 if (style == EXTCOMMUNITY_LIST_STANDARD)
1261 ecom = ecommunity_str2com(str, 0, 1);
718e3744 1262
d62a17ae 1263 if (ecom) {
1264 entry = community_list_entry_lookup(list, ecom, direct);
1265 ecommunity_free(&ecom);
1266 } else
1267 entry = community_list_entry_lookup(list, str, direct);
fee6e4e4 1268
d62a17ae 1269 if (!entry)
1270 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
718e3744 1271
3571a6a2 1272 community_list_entry_delete(cm, list, entry);
d62a17ae 1273 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 1274
d62a17ae 1275 return 0;
718e3744 1276}
1277
1278/* Initializa community-list. Return community-list handler. */
d62a17ae 1279struct community_list_handler *community_list_init(void)
718e3744 1280{
d62a17ae 1281 struct community_list_handler *ch;
1282 ch = XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER,
1283 sizeof(struct community_list_handler));
3571a6a2
DS
1284
1285 ch->community_list.hash =
1286 hash_create_size(4, bgp_clist_hash_key_community_list,
1287 bgp_clist_hash_cmp_community_list,
1288 "Community List Number Quick Lookup");
1289
1290 ch->extcommunity_list.hash =
1291 hash_create_size(4, bgp_clist_hash_key_community_list,
1292 bgp_clist_hash_cmp_community_list,
1293 "Extended Community List Quick Lookup");
1294
1295 ch->lcommunity_list.hash =
1296 hash_create_size(4, bgp_clist_hash_key_community_list,
1297 bgp_clist_hash_cmp_community_list,
1298 "Large Community List Quick Lookup");
1299
d62a17ae 1300 return ch;
718e3744 1301}
1302
1303/* Terminate community-list. */
d62a17ae 1304void community_list_terminate(struct community_list_handler *ch)
718e3744 1305{
d62a17ae 1306 struct community_list_master *cm;
1307 struct community_list *list;
1308
1309 cm = &ch->community_list;
1310 while ((list = cm->num.head) != NULL)
3571a6a2 1311 community_list_delete(cm, list);
d62a17ae 1312 while ((list = cm->str.head) != NULL)
3571a6a2
DS
1313 community_list_delete(cm, list);
1314 hash_free(cm->hash);
d62a17ae 1315
1316 cm = &ch->lcommunity_list;
1317 while ((list = cm->num.head) != NULL)
3571a6a2 1318 community_list_delete(cm, list);
d62a17ae 1319 while ((list = cm->str.head) != NULL)
3571a6a2
DS
1320 community_list_delete(cm, list);
1321 hash_free(cm->hash);
d62a17ae 1322
1323 cm = &ch->extcommunity_list;
1324 while ((list = cm->num.head) != NULL)
3571a6a2 1325 community_list_delete(cm, list);
d62a17ae 1326 while ((list = cm->str.head) != NULL)
3571a6a2
DS
1327 community_list_delete(cm, list);
1328 hash_free(cm->hash);
d62a17ae 1329
1330 XFREE(MTYPE_COMMUNITY_LIST_HANDLER, ch);
718e3744 1331}