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