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