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