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