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