]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_clist.c
Merge pull request #13670 from donaldsharp/coverity_99
[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) {
d62a17ae 662 if (entry->style == COMMUNITY_LIST_STANDARD) {
b6dbdee5
DA
663 if (community_include(entry->u.com, COMMUNITY_INTERNET))
664 return entry->direct == COMMUNITY_PERMIT;
665
d62a17ae 666 if (community_match(com, entry->u.com))
88f1c947 667 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 668 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
669 if (community_regexp_match(com, entry->reg))
88f1c947 670 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 671 }
672 }
88f1c947 673 return false;
8708b74f 674}
675
88f1c947 676bool lcommunity_list_match(struct lcommunity *lcom, struct community_list *list)
57d187bc 677{
d62a17ae 678 struct community_entry *entry;
679
680 for (entry = list->head; entry; entry = entry->next) {
d62a17ae 681 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
682 if (lcommunity_match(lcom, entry->u.lcom))
88f1c947 683 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 684 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
685 if (lcommunity_regexp_match(lcom, entry->reg))
88f1c947 686 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 687 }
688 }
88f1c947 689 return false;
57d187bc
JS
690}
691
f8463998 692
693/* Perform exact matching. In case of expanded large-community-list, do
694 * same thing as lcommunity_list_match().
695 */
88f1c947
DA
696bool lcommunity_list_exact_match(struct lcommunity *lcom,
697 struct community_list *list)
f8463998 698{
699 struct community_entry *entry;
700
701 for (entry = list->head; entry; entry = entry->next) {
f8463998 702 if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) {
0d5b3cb9 703 if (lcommunity_cmp(lcom, entry->u.lcom))
88f1c947 704 return entry->direct == COMMUNITY_PERMIT;
f8463998 705 } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) {
706 if (lcommunity_regexp_match(lcom, entry->reg))
88f1c947 707 return entry->direct == COMMUNITY_PERMIT;
f8463998 708 }
709 }
88f1c947 710 return false;
f8463998 711}
712
88f1c947 713bool ecommunity_list_match(struct ecommunity *ecom, struct community_list *list)
8708b74f 714{
d62a17ae 715 struct community_entry *entry;
716
717 for (entry = list->head; entry; entry = entry->next) {
d62a17ae 718 if (entry->style == EXTCOMMUNITY_LIST_STANDARD) {
719 if (ecommunity_match(ecom, entry->u.ecom))
88f1c947 720 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 721 } else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) {
722 if (ecommunity_regexp_match(ecom, entry->reg))
88f1c947 723 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 724 }
725 }
88f1c947 726 return false;
718e3744 727}
728
729/* Perform exact matching. In case of expanded community-list, do
730 same thing as community_list_match(). */
88f1c947
DA
731bool community_list_exact_match(struct community *com,
732 struct community_list *list)
718e3744 733{
d62a17ae 734 struct community_entry *entry;
735
736 for (entry = list->head; entry; entry = entry->next) {
d62a17ae 737 if (entry->style == COMMUNITY_LIST_STANDARD) {
738 if (community_include(entry->u.com, COMMUNITY_INTERNET))
88f1c947 739 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 740
741 if (community_cmp(com, entry->u.com))
88f1c947 742 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 743 } else if (entry->style == COMMUNITY_LIST_EXPANDED) {
744 if (community_regexp_match(com, entry->reg))
88f1c947 745 return entry->direct == COMMUNITY_PERMIT;
d62a17ae 746 }
747 }
88f1c947 748 return false;
718e3744 749}
750
8708b74f 751/* Delete all permitted communities in the list from com. */
d62a17ae 752struct community *community_list_match_delete(struct community *com,
753 struct community_list *list)
718e3744 754{
d62a17ae 755 struct community_entry *entry;
d7c0a89a
QY
756 uint32_t val;
757 uint32_t com_index_to_delete[com->size];
d62a17ae 758 int delete_index = 0;
759 int i;
760
761 /* Loop over each community value and evaluate each against the
762 * community-list. If we need to delete a community value add its index
ceead39c 763 * to com_index_to_delete.
d62a17ae 764 */
765 for (i = 0; i < com->size; i++) {
766 val = community_val_get(com, i);
767
768 for (entry = list->head; entry; entry = entry->next) {
75aaf03f
DS
769 if ((entry->style == COMMUNITY_LIST_STANDARD) &&
770 (community_include(entry->u.com,
771 COMMUNITY_INTERNET) ||
772 community_include(entry->u.com, val))) {
d62a17ae 773 if (entry->direct == COMMUNITY_PERMIT) {
774 com_index_to_delete[delete_index] = i;
775 delete_index++;
776 }
777 break;
75aaf03f
DS
778 } else if ((entry->style == COMMUNITY_LIST_EXPANDED) &&
779 community_regexp_include(entry->reg, com,
780 i)) {
d62a17ae 781 if (entry->direct == COMMUNITY_PERMIT) {
782 com_index_to_delete[delete_index] = i;
783 delete_index++;
784 }
785 break;
786 }
787 }
788 }
5cbea288 789
d62a17ae 790 /* Delete all of the communities we flagged for deletion */
791 for (i = delete_index - 1; i >= 0; i--) {
792 val = community_val_get(com, com_index_to_delete[i]);
0743b61d 793 val = htonl(val);
d62a17ae 794 community_del_val(com, &val);
795 }
5cbea288 796
d62a17ae 797 return com;
718e3744 798}
799
800/* To avoid duplicated entry in the community-list, this function
801 compares specified entry to existing entry. */
88f1c947
DA
802static bool community_list_dup_check(struct community_list *list,
803 struct community_entry *new)
718e3744 804{
d62a17ae 805 struct community_entry *entry;
806
807 for (entry = list->head; entry; entry = entry->next) {
808 if (entry->style != new->style)
809 continue;
810
811 if (entry->direct != new->direct)
812 continue;
813
d62a17ae 814 switch (entry->style) {
815 case COMMUNITY_LIST_STANDARD:
816 if (community_cmp(entry->u.com, new->u.com))
88f1c947 817 return true;
d62a17ae 818 break;
819 case LARGE_COMMUNITY_LIST_STANDARD:
820 if (lcommunity_cmp(entry->u.lcom, new->u.lcom))
88f1c947 821 return true;
d62a17ae 822 break;
823 case EXTCOMMUNITY_LIST_STANDARD:
824 if (ecommunity_cmp(entry->u.ecom, new->u.ecom))
88f1c947 825 return true;
d62a17ae 826 break;
827 case COMMUNITY_LIST_EXPANDED:
828 case EXTCOMMUNITY_LIST_EXPANDED:
829 case LARGE_COMMUNITY_LIST_EXPANDED:
830 if (strcmp(entry->config, new->config) == 0)
88f1c947 831 return true;
d62a17ae 832 break;
833 default:
834 break;
835 }
836 }
88f1c947 837 return false;
718e3744 838}
6b0655a2 839
718e3744 840/* Set community-list. */
d62a17ae 841int community_list_set(struct community_list_handler *ch, const char *name,
2f8cc0e5 842 const char *str, const char *seq, int direct, int style)
718e3744 843{
d62a17ae 844 struct community_entry *entry = NULL;
845 struct community_list *list;
846 struct community *com = NULL;
847 regex_t *regex = NULL;
2f8cc0e5
DA
848 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
849
850 if (seq)
851 seqnum = (int64_t)atol(seq);
d62a17ae 852
853 /* Get community list. */
854 list = community_list_get(ch, name, COMMUNITY_LIST_MASTER);
855
856 /* When community-list already has entry, new entry should have same
857 style. If you want to have mixed style community-list, you can
858 comment out this check. */
859 if (!community_list_empty_p(list)) {
860 struct community_entry *first;
861
862 first = list->head;
863
864 if (style != first->style) {
865 return (first->style == COMMUNITY_LIST_STANDARD
866 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
867 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
868 }
fee6e4e4 869 }
718e3744 870
1f703838
DS
871 if (style == COMMUNITY_LIST_STANDARD)
872 com = community_str2com(str);
873 else
874 regex = bgp_regcomp(str);
8708b74f 875
1f703838
DS
876 if (!com && !regex)
877 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
718e3744 878
d62a17ae 879 entry = community_entry_new();
880 entry->direct = direct;
881 entry->style = style;
d62a17ae 882 entry->u.com = com;
883 entry->reg = regex;
2f8cc0e5 884 entry->seq = seqnum;
d62a17ae 885 entry->config =
886 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
887
888 /* Do not put duplicated community entry. */
889 if (community_list_dup_check(list, entry))
890 community_entry_free(entry);
891 else {
2f8cc0e5
DA
892 community_list_entry_add(list, entry, ch,
893 COMMUNITY_LIST_MASTER);
d62a17ae 894 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED);
895 }
718e3744 896
d62a17ae 897 return 0;
718e3744 898}
899
813d4307 900/* Unset community-list */
d62a17ae 901int community_list_unset(struct community_list_handler *ch, const char *name,
2f8cc0e5
DA
902 const char *str, const char *seq, int direct,
903 int style)
718e3744 904{
3571a6a2 905 struct community_list_master *cm = NULL;
d62a17ae 906 struct community_entry *entry = NULL;
907 struct community_list *list;
908 struct community *com = NULL;
909
910 /* Lookup community list. */
e237b0d2 911 list = community_list_lookup(ch, name, 0, COMMUNITY_LIST_MASTER);
d62a17ae 912 if (list == NULL)
913 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
914
3571a6a2 915 cm = community_list_master_lookup(ch, COMMUNITY_LIST_MASTER);
d62a17ae 916 /* Delete all of entry belongs to this community-list. */
7298a8e1 917 if (!str) {
3571a6a2 918 community_list_delete(cm, list);
d62a17ae 919 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
920 return 0;
921 }
718e3744 922
7298a8e1
QY
923 if (style == COMMUNITY_LIST_STANDARD)
924 com = community_str2com(str);
718e3744 925
d62a17ae 926 if (com) {
927 entry = community_list_entry_lookup(list, com, direct);
3c1f53de 928 community_free(&com);
d62a17ae 929 } else
930 entry = community_list_entry_lookup(list, str, direct);
fee6e4e4 931
d62a17ae 932 if (!entry)
933 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
718e3744 934
3571a6a2 935 community_list_entry_delete(cm, list, entry);
d62a17ae 936 route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
718e3744 937
d62a17ae 938 return 0;
718e3744 939}
940
57d187bc 941/* Delete all permitted large communities in the list from com. */
d62a17ae 942struct lcommunity *lcommunity_list_match_delete(struct lcommunity *lcom,
943 struct community_list *list)
57d187bc 944{
d62a17ae 945 struct community_entry *entry;
d7c0a89a
QY
946 uint32_t com_index_to_delete[lcom->size];
947 uint8_t *ptr;
d62a17ae 948 int delete_index = 0;
949 int i;
950
951 /* Loop over each lcommunity value and evaluate each against the
952 * community-list. If we need to delete a community value add its index
ceead39c 953 * to com_index_to_delete.
d62a17ae 954 */
d62a17ae 955 for (i = 0; i < lcom->size; i++) {
534fc195 956 ptr = lcom->val + (i * LCOMMUNITY_SIZE);
d62a17ae 957 for (entry = list->head; entry; entry = entry->next) {
75aaf03f
DS
958 if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD) &&
959 lcommunity_include(entry->u.lcom, ptr)) {
d62a17ae 960 if (entry->direct == COMMUNITY_PERMIT) {
961 com_index_to_delete[delete_index] = i;
962 delete_index++;
963 }
964 break;
965 }
966
75aaf03f
DS
967 else if ((entry->style ==
968 LARGE_COMMUNITY_LIST_EXPANDED) &&
969 lcommunity_regexp_include(entry->reg, lcom,
970 i)) {
d62a17ae 971 if (entry->direct == COMMUNITY_PERMIT) {
972 com_index_to_delete[delete_index] = i;
973 delete_index++;
974 }
975 break;
976 }
977 }
978 }
57d187bc 979
d62a17ae 980 /* Delete all of the communities we flagged for deletion */
d62a17ae 981 for (i = delete_index - 1; i >= 0; i--) {
0a7dce9b 982 ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE);
d62a17ae 983 lcommunity_del_val(lcom, ptr);
984 }
57d187bc 985
d62a17ae 986 return lcom;
57d187bc
JS
987}
988
5bd66e2d 989/* Helper to check if every octet do not exceed UINT_MAX */
c850908b 990bool lcommunity_list_valid(const char *community, int style)
5bd66e2d 991{
bc2c9ae6
DA
992 int octets;
993 char **splits, **communities;
c850908b 994 char *endptr;
bc2c9ae6 995 int num, num_communities;
c850908b
WC
996 regex_t *regres;
997 int invalid = 0;
5bd66e2d 998
bc2c9ae6 999 frrstr_split(community, " ", &communities, &num_communities);
5bd66e2d 1000
bc2c9ae6
DA
1001 for (int j = 0; j < num_communities; j++) {
1002 octets = 0;
1003 frrstr_split(communities[j], ":", &splits, &num);
1004
1005 for (int i = 0; i < num; i++) {
bc2c9ae6 1006 if (strlen(splits[i]) == 0)
c850908b
WC
1007 /* There is no digit to check */
1008 invalid++;
1009
1010 if (style == LARGE_COMMUNITY_LIST_STANDARD) {
1011 if (*splits[i] == '-')
1012 /* Must not be negative */
1013 invalid++;
1014 else if (strtoul(splits[i], &endptr, 10)
1015 > UINT_MAX)
1016 /* Larger than 4 octets */
1017 invalid++;
1018 else if (*endptr)
1019 /* Not all characters were digits */
1020 invalid++;
1021 } else {
1022 regres = bgp_regcomp(communities[j]);
1023 if (!regres)
1024 /* malformed regex */
1025 invalid++;
1026 else
1027 bgp_regex_free(regres);
1028 }
bc2c9ae6
DA
1029
1030 octets++;
1031 XFREE(MTYPE_TMP, splits[i]);
1032 }
1033 XFREE(MTYPE_TMP, splits);
1034
c850908b
WC
1035 if (octets != 3)
1036 invalid++;
5bd66e2d 1037
bc2c9ae6 1038 XFREE(MTYPE_TMP, communities[j]);
5bd66e2d 1039 }
bc2c9ae6 1040 XFREE(MTYPE_TMP, communities);
5bd66e2d 1041
c850908b 1042 return (invalid > 0) ? false : true;
5bd66e2d
DA
1043}
1044
57d187bc 1045/* Set lcommunity-list. */
d62a17ae 1046int lcommunity_list_set(struct community_list_handler *ch, const char *name,
2f8cc0e5 1047 const char *str, const char *seq, int direct, int style)
57d187bc 1048{
d62a17ae 1049 struct community_entry *entry = NULL;
1050 struct community_list *list;
1051 struct lcommunity *lcom = NULL;
1052 regex_t *regex = NULL;
2f8cc0e5
DA
1053 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
1054
1055 if (seq)
1056 seqnum = (int64_t)atol(seq);
d62a17ae 1057
1058 /* Get community list. */
1059 list = community_list_get(ch, name, LARGE_COMMUNITY_LIST_MASTER);
1060
1061 /* When community-list already has entry, new entry should have same
1062 style. If you want to have mixed style community-list, you can
1063 comment out this check. */
1064 if (!community_list_empty_p(list)) {
1065 struct community_entry *first;
1066
1067 first = list->head;
1068
1069 if (style != first->style) {
1070 return (first->style == COMMUNITY_LIST_STANDARD
1071 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1072 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1073 }
1074 }
57d187bc 1075
d62a17ae 1076 if (str) {
1077 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1078 lcom = lcommunity_str2com(str);
1079 else
1080 regex = bgp_regcomp(str);
57d187bc 1081
d62a17ae 1082 if (!lcom && !regex)
1083 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1084 }
57d187bc 1085
d62a17ae 1086 entry = community_entry_new();
1087 entry->direct = direct;
1088 entry->style = style;
d62a17ae 1089 entry->u.lcom = lcom;
1090 entry->reg = regex;
2f8cc0e5 1091 entry->seq = seqnum;
8d9b8ed9
PM
1092 entry->config =
1093 (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
d62a17ae 1094
1095 /* Do not put duplicated community entry. */
1096 if (community_list_dup_check(list, entry))
1097 community_entry_free(entry);
35f6f850 1098 else {
2f8cc0e5
DA
1099 community_list_entry_add(list, entry, ch,
1100 LARGE_COMMUNITY_LIST_MASTER);
35f6f850 1101 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_ADDED);
1102 }
d62a17ae 1103
1104 return 0;
57d187bc
JS
1105}
1106
1107/* Unset community-list. When str is NULL, delete all of
1108 community-list entry belongs to the specified name. */
d62a17ae 1109int lcommunity_list_unset(struct community_list_handler *ch, const char *name,
2f8cc0e5
DA
1110 const char *str, const char *seq, int direct,
1111 int style)
57d187bc 1112{
3571a6a2 1113 struct community_list_master *cm = NULL;
d62a17ae 1114 struct community_entry *entry = NULL;
1115 struct community_list *list;
1116 struct lcommunity *lcom = NULL;
1117 regex_t *regex = NULL;
1118
1119 /* Lookup community list. */
e237b0d2 1120 list = community_list_lookup(ch, name, 0, LARGE_COMMUNITY_LIST_MASTER);
d62a17ae 1121 if (list == NULL)
1122 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1123
3571a6a2 1124 cm = community_list_master_lookup(ch, LARGE_COMMUNITY_LIST_MASTER);
d62a17ae 1125 /* Delete all of entry belongs to this community-list. */
1126 if (!str) {
3571a6a2 1127 community_list_delete(cm, list);
35f6f850 1128 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
d62a17ae 1129 return 0;
1130 }
57d187bc 1131
d62a17ae 1132 if (style == LARGE_COMMUNITY_LIST_STANDARD)
1133 lcom = lcommunity_str2com(str);
1134 else
1135 regex = bgp_regcomp(str);
57d187bc 1136
d62a17ae 1137 if (!lcom && !regex)
1138 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
57d187bc 1139
d62a17ae 1140 if (lcom)
1141 entry = community_list_entry_lookup(list, lcom, direct);
1142 else
1143 entry = community_list_entry_lookup(list, str, direct);
57d187bc 1144
d62a17ae 1145 if (lcom)
1146 lcommunity_free(&lcom);
1147 if (regex)
1148 bgp_regex_free(regex);
57d187bc 1149
d62a17ae 1150 if (!entry)
1151 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
57d187bc 1152
3571a6a2 1153 community_list_entry_delete(cm, list, entry);
35f6f850 1154 route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
57d187bc 1155
d62a17ae 1156 return 0;
57d187bc
JS
1157}
1158
718e3744 1159/* Set extcommunity-list. */
d62a17ae 1160int extcommunity_list_set(struct community_list_handler *ch, const char *name,
2f8cc0e5
DA
1161 const char *str, const char *seq, int direct,
1162 int style)
718e3744 1163{
d62a17ae 1164 struct community_entry *entry = NULL;
1165 struct community_list *list;
1166 struct ecommunity *ecom = NULL;
1167 regex_t *regex = NULL;
2f8cc0e5
DA
1168 int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO;
1169
1170 if (seq)
1171 seqnum = (int64_t)atol(seq);
718e3744 1172
fa301630 1173 if (str == NULL)
1174 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1175
d62a17ae 1176 /* Get community list. */
1177 list = community_list_get(ch, name, EXTCOMMUNITY_LIST_MASTER);
718e3744 1178
d62a17ae 1179 /* When community-list already has entry, new entry should have same
1180 style. If you want to have mixed style community-list, you can
1181 comment out this check. */
1182 if (!community_list_empty_p(list)) {
1183 struct community_entry *first;
718e3744 1184
d62a17ae 1185 first = list->head;
718e3744 1186
d62a17ae 1187 if (style != first->style) {
1188 return (first->style == EXTCOMMUNITY_LIST_STANDARD
1189 ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
1190 : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
1191 }
fee6e4e4 1192 }
718e3744 1193
d62a17ae 1194 if (style == EXTCOMMUNITY_LIST_STANDARD)
1195 ecom = ecommunity_str2com(str, 0, 1);
1196 else
1197 regex = bgp_regcomp(str);
1198
1199 if (!ecom && !regex)
1200 return COMMUNITY_LIST_ERR_MALFORMED_VAL;
1201
1202 if (ecom)
1203 ecom->str =
1204 ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
1205
1206 entry = community_entry_new();
1207 entry->direct = direct;
1208 entry->style = style;
d62a17ae 1209 if (ecom)
1210 entry->config = ecommunity_ecom2str(
1211 ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0);
1212 else if (regex)
1213 entry->config = XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str);
1214
1215 entry->u.ecom = ecom;
1216 entry->reg = regex;
2f8cc0e5 1217 entry->seq = seqnum;
d62a17ae 1218
1219 /* Do not put duplicated community entry. */
1220 if (community_list_dup_check(list, entry))
1221 community_entry_free(entry);
1222 else {
2f8cc0e5
DA
1223 community_list_entry_add(list, entry, ch,
1224 EXTCOMMUNITY_LIST_MASTER);
d62a17ae 1225 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED);
1226 }
718e3744 1227
d62a17ae 1228 return 0;
718e3744 1229}
1230
7298a8e1
QY
1231/* Unset extcommunity-list.
1232 *
1233 * When str is NULL, delete all extcommunity-list entries belonging to the
1234 * specified name.
1235 */
d62a17ae 1236int extcommunity_list_unset(struct community_list_handler *ch, const char *name,
2f8cc0e5
DA
1237 const char *str, const char *seq, int direct,
1238 int style)
718e3744 1239{
3571a6a2 1240 struct community_list_master *cm = NULL;
d62a17ae 1241 struct community_entry *entry = NULL;
1242 struct community_list *list;
1243 struct ecommunity *ecom = NULL;
1244
1245 /* Lookup extcommunity list. */
e237b0d2 1246 list = community_list_lookup(ch, name, 0, EXTCOMMUNITY_LIST_MASTER);
d62a17ae 1247 if (list == NULL)
1248 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
1249
3571a6a2 1250 cm = community_list_master_lookup(ch, EXTCOMMUNITY_LIST_MASTER);
d62a17ae 1251 /* Delete all of entry belongs to this extcommunity-list. */
7298a8e1 1252 if (!str) {
3571a6a2 1253 community_list_delete(cm, list);
d62a17ae 1254 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
1255 return 0;
1256 }
718e3744 1257
7298a8e1
QY
1258 if (style == EXTCOMMUNITY_LIST_STANDARD)
1259 ecom = ecommunity_str2com(str, 0, 1);
718e3744 1260
d62a17ae 1261 if (ecom) {
1262 entry = community_list_entry_lookup(list, ecom, direct);
1263 ecommunity_free(&ecom);
1264 } else
1265 entry = community_list_entry_lookup(list, str, direct);
fee6e4e4 1266
d62a17ae 1267 if (!entry)
1268 return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
718e3744 1269
3571a6a2 1270 community_list_entry_delete(cm, list, entry);
d62a17ae 1271 route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
718e3744 1272
d62a17ae 1273 return 0;
718e3744 1274}
1275
1276/* Initializa community-list. Return community-list handler. */
d62a17ae 1277struct community_list_handler *community_list_init(void)
718e3744 1278{
d62a17ae 1279 struct community_list_handler *ch;
1280 ch = XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER,
1281 sizeof(struct community_list_handler));
3571a6a2
DS
1282
1283 ch->community_list.hash =
1284 hash_create_size(4, bgp_clist_hash_key_community_list,
1285 bgp_clist_hash_cmp_community_list,
1286 "Community List Number Quick Lookup");
1287
1288 ch->extcommunity_list.hash =
1289 hash_create_size(4, bgp_clist_hash_key_community_list,
1290 bgp_clist_hash_cmp_community_list,
1291 "Extended Community List Quick Lookup");
1292
1293 ch->lcommunity_list.hash =
1294 hash_create_size(4, bgp_clist_hash_key_community_list,
1295 bgp_clist_hash_cmp_community_list,
1296 "Large Community List Quick Lookup");
1297
d62a17ae 1298 return ch;
718e3744 1299}
1300
1301/* Terminate community-list. */
d62a17ae 1302void community_list_terminate(struct community_list_handler *ch)
718e3744 1303{
d62a17ae 1304 struct community_list_master *cm;
1305 struct community_list *list;
1306
1307 cm = &ch->community_list;
1308 while ((list = cm->num.head) != NULL)
3571a6a2 1309 community_list_delete(cm, list);
d62a17ae 1310 while ((list = cm->str.head) != NULL)
3571a6a2
DS
1311 community_list_delete(cm, list);
1312 hash_free(cm->hash);
d62a17ae 1313
1314 cm = &ch->lcommunity_list;
1315 while ((list = cm->num.head) != NULL)
3571a6a2 1316 community_list_delete(cm, list);
d62a17ae 1317 while ((list = cm->str.head) != NULL)
3571a6a2
DS
1318 community_list_delete(cm, list);
1319 hash_free(cm->hash);
d62a17ae 1320
1321 cm = &ch->extcommunity_list;
1322 while ((list = cm->num.head) != NULL)
3571a6a2 1323 community_list_delete(cm, list);
d62a17ae 1324 while ((list = cm->str.head) != NULL)
3571a6a2
DS
1325 community_list_delete(cm, list);
1326 hash_free(cm->hash);
d62a17ae 1327
1328 XFREE(MTYPE_COMMUNITY_LIST_HANDLER, ch);
718e3744 1329}
225096bc
DA
1330
1331static int bgp_community_list_vector_walker(struct hash_bucket *bucket,
1332 void *data)
1333{
1334 vector *comps = data;
1335 struct community_list *list = bucket->data;
1336
1337 vector_set(*comps, XSTRDUP(MTYPE_COMPLETION, list->name));
1338
1339 return 1;
1340}
1341
1342static void bgp_community_list_cmd_completion(vector comps,
1343 struct cmd_token *token)
1344{
1345 struct community_list_master *cm;
1346
1347 cm = community_list_master_lookup(bgp_clist, COMMUNITY_LIST_MASTER);
1348
1349 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1350}
1351
1352static void bgp_lcommunity_list_cmd_completion(vector comps,
1353 struct cmd_token *token)
1354{
1355 struct community_list_master *cm;
1356
1357 cm = community_list_master_lookup(bgp_clist,
1358 LARGE_COMMUNITY_LIST_MASTER);
1359
1360 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1361}
1362
1363static void bgp_extcommunity_list_cmd_completion(vector comps,
1364 struct cmd_token *token)
1365{
1366 struct community_list_master *cm;
1367
1368 cm = community_list_master_lookup(bgp_clist, EXTCOMMUNITY_LIST_MASTER);
1369
1370 hash_walk(cm->hash, bgp_community_list_vector_walker, &comps);
1371}
1372
1373static const struct cmd_variable_handler community_list_handlers[] = {
1374 {.tokenname = "COMMUNITY_LIST_NAME",
1375 .completions = bgp_community_list_cmd_completion},
1376 {.completions = NULL}};
1377
1378static const struct cmd_variable_handler lcommunity_list_handlers[] = {
1379 {.tokenname = "LCOMMUNITY_LIST_NAME",
1380 .completions = bgp_lcommunity_list_cmd_completion},
1381 {.completions = NULL}};
1382
1383static const struct cmd_variable_handler extcommunity_list_handlers[] = {
1384 {.tokenname = "EXTCOMMUNITY_LIST_NAME",
1385 .completions = bgp_extcommunity_list_cmd_completion},
1386 {.completions = NULL}};
1387
1388void bgp_community_list_command_completion_setup(void)
1389{
1390 cmd_variable_handler_register(community_list_handlers);
1391 cmd_variable_handler_register(lcommunity_list_handlers);
1392 cmd_variable_handler_register(extcommunity_list_handlers);
1393}