]>
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" |
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. */ |
41 | static 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 | ||
1bb379bf | 47 | maxseq = 0; |
2f8cc0e5 DA |
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. */ | |
60 | static 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 | 71 | static 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 | ||
82 | static 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. */ | |
98 | struct community_list_master * | |
d62a17ae | 99 | community_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 | 114 | static 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 | 121 | static 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 | 152 | static 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 | 158 | static 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 | 164 | static struct community_list * |
d62a17ae | 165 | community_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 | 255 | struct 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 | 275 | static struct community_list * |
d62a17ae | 276 | community_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 |
287 | static 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 | ||
88f1c947 | 314 | static bool community_list_empty_p(struct community_list *list) |
718e3744 | 315 | { |
88f1c947 | 316 | return list->head == NULL && list->tail == NULL; |
718e3744 | 317 | } |
6b0655a2 | 318 | |
718e3744 | 319 | /* Delete community-list entry from the list. */ |
3571a6a2 DS |
320 | static 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 | ||
96e2fd25 DS |
340 | /* |
341 | * Replace community-list entry in the list. Note that entry is the new one | |
342 | * and replace is one one being replaced. | |
343 | */ | |
344 | static void community_list_entry_replace(struct community_list *list, | |
345 | struct community_entry *replace, | |
346 | struct community_entry *entry) | |
347 | { | |
348 | if (replace->next) { | |
349 | entry->next = replace->next; | |
350 | replace->next->prev = entry; | |
351 | } else { | |
352 | entry->next = NULL; | |
353 | list->tail = entry; | |
354 | } | |
355 | ||
356 | if (replace->prev) { | |
357 | entry->prev = replace->prev; | |
358 | replace->prev->next = entry; | |
359 | } else { | |
360 | entry->prev = NULL; | |
361 | list->head = entry; | |
362 | } | |
363 | ||
364 | community_entry_free(replace); | |
365 | } | |
366 | ||
2f8cc0e5 DA |
367 | /* Add community-list entry to the list. */ |
368 | static void community_list_entry_add(struct community_list *list, | |
369 | struct community_entry *entry, | |
370 | struct community_list_handler *ch, | |
371 | int master) | |
372 | { | |
2f8cc0e5 DA |
373 | struct community_entry *replace; |
374 | struct community_entry *point; | |
375 | ||
2f8cc0e5 DA |
376 | /* Automatic assignment of seq no. */ |
377 | if (entry->seq == COMMUNITY_SEQ_NUMBER_AUTO) | |
378 | entry->seq = bgp_clist_new_seq_get(list); | |
379 | ||
380 | if (list->tail && entry->seq > list->tail->seq) | |
381 | point = NULL; | |
382 | else { | |
383 | replace = bgp_clist_seq_check(list, entry->seq); | |
96e2fd25 DS |
384 | if (replace) { |
385 | community_list_entry_replace(list, replace, entry); | |
386 | return; | |
387 | } | |
2f8cc0e5 DA |
388 | |
389 | /* Check insert point. */ | |
390 | for (point = list->head; point; point = point->next) | |
391 | if (point->seq >= entry->seq) | |
392 | break; | |
393 | } | |
394 | ||
395 | /* In case of this is the first element of the list. */ | |
396 | entry->next = point; | |
397 | ||
398 | if (point) { | |
399 | if (point->prev) | |
400 | point->prev->next = entry; | |
401 | else | |
402 | list->head = entry; | |
403 | ||
404 | entry->prev = point->prev; | |
405 | point->prev = entry; | |
406 | } else { | |
407 | if (list->tail) | |
408 | list->tail->next = entry; | |
409 | else | |
410 | list->head = entry; | |
411 | ||
412 | entry->prev = list->tail; | |
413 | list->tail = entry; | |
414 | } | |
415 | } | |
416 | ||
718e3744 | 417 | /* Lookup community-list entry from the list. */ |
418 | static struct community_entry * | |
d62a17ae | 419 | community_list_entry_lookup(struct community_list *list, const void *arg, |
420 | int direct) | |
718e3744 | 421 | { |
d62a17ae | 422 | struct community_entry *entry; |
423 | ||
424 | for (entry = list->head; entry; entry = entry->next) { | |
425 | switch (entry->style) { | |
426 | case COMMUNITY_LIST_STANDARD: | |
427 | if (entry->direct == direct | |
428 | && community_cmp(entry->u.com, arg)) | |
429 | return entry; | |
430 | break; | |
431 | case EXTCOMMUNITY_LIST_STANDARD: | |
432 | if (entry->direct == direct | |
433 | && ecommunity_cmp(entry->u.ecom, arg)) | |
434 | return entry; | |
435 | break; | |
436 | case LARGE_COMMUNITY_LIST_STANDARD: | |
437 | if (entry->direct == direct | |
438 | && lcommunity_cmp(entry->u.lcom, arg)) | |
439 | return entry; | |
440 | break; | |
441 | case COMMUNITY_LIST_EXPANDED: | |
442 | case EXTCOMMUNITY_LIST_EXPANDED: | |
443 | case LARGE_COMMUNITY_LIST_EXPANDED: | |
444 | if (entry->direct == direct | |
445 | && strcmp(entry->config, arg) == 0) | |
446 | return entry; | |
447 | break; | |
448 | default: | |
449 | break; | |
450 | } | |
451 | } | |
452 | return NULL; | |
718e3744 | 453 | } |
6b0655a2 | 454 | |
d62a17ae | 455 | static char *community_str_get(struct community *com, int i) |
5cbea288 | 456 | { |
d7c0a89a QY |
457 | uint32_t comval; |
458 | uint16_t as; | |
459 | uint16_t val; | |
d62a17ae | 460 | char *str; |
d62a17ae | 461 | |
d7c0a89a | 462 | memcpy(&comval, com_nthval(com, i), sizeof(uint32_t)); |
d62a17ae | 463 | comval = ntohl(comval); |
464 | ||
465 | switch (comval) { | |
466 | case COMMUNITY_INTERNET: | |
aeab4a80 | 467 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "internet"); |
d62a17ae | 468 | break; |
aa861c10 | 469 | case COMMUNITY_GSHUT: |
aeab4a80 | 470 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "graceful-shutdown"); |
aa861c10 C |
471 | break; |
472 | case COMMUNITY_ACCEPT_OWN: | |
aeab4a80 | 473 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own"); |
aa861c10 C |
474 | break; |
475 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4: | |
aeab4a80 QY |
476 | str = XSTRDUP(MTYPE_COMMUNITY_STR, |
477 | "route-filter-translated-v4"); | |
aa861c10 C |
478 | break; |
479 | case COMMUNITY_ROUTE_FILTER_v4: | |
aeab4a80 | 480 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v4"); |
aa861c10 C |
481 | break; |
482 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6: | |
aeab4a80 QY |
483 | str = XSTRDUP(MTYPE_COMMUNITY_STR, |
484 | "route-filter-translated-v6"); | |
aa861c10 C |
485 | break; |
486 | case COMMUNITY_ROUTE_FILTER_v6: | |
aeab4a80 | 487 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v6"); |
aa861c10 C |
488 | break; |
489 | case COMMUNITY_LLGR_STALE: | |
aeab4a80 | 490 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "llgr-stale"); |
aa861c10 C |
491 | break; |
492 | case COMMUNITY_NO_LLGR: | |
aeab4a80 | 493 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-llgr"); |
aa861c10 C |
494 | break; |
495 | case COMMUNITY_ACCEPT_OWN_NEXTHOP: | |
aeab4a80 | 496 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own-nexthop"); |
aa861c10 C |
497 | break; |
498 | case COMMUNITY_BLACKHOLE: | |
aeab4a80 | 499 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "blackhole"); |
aa861c10 | 500 | break; |
d62a17ae | 501 | case COMMUNITY_NO_EXPORT: |
aeab4a80 | 502 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-export"); |
d62a17ae | 503 | break; |
504 | case COMMUNITY_NO_ADVERTISE: | |
aeab4a80 | 505 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-advertise"); |
d62a17ae | 506 | break; |
507 | case COMMUNITY_LOCAL_AS: | |
aeab4a80 | 508 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "local-AS"); |
d62a17ae | 509 | break; |
aa861c10 | 510 | case COMMUNITY_NO_PEER: |
aeab4a80 | 511 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-peer"); |
7f323236 | 512 | break; |
809d6365 | 513 | default: |
aeab4a80 | 514 | str = XSTRDUP(MTYPE_COMMUNITY_STR, "65536:65535"); |
d62a17ae | 515 | as = (comval >> 16) & 0xFFFF; |
516 | val = comval & 0xFFFF; | |
aeab4a80 | 517 | snprintf(str, strlen(str), "%u:%d", as, val); |
d62a17ae | 518 | break; |
519 | } | |
5cbea288 | 520 | |
d62a17ae | 521 | return str; |
5cbea288 DS |
522 | } |
523 | ||
524 | /* Internal function to perform regular expression match for | |
2acb4ac2 | 525 | * a single community. */ |
88f1c947 | 526 | static bool community_regexp_include(regex_t *reg, struct community *com, int i) |
5cbea288 | 527 | { |
d62a17ae | 528 | char *str; |
529 | int rv; | |
5cbea288 | 530 | |
d62a17ae | 531 | /* When there is no communities attribute it is treated as empty string. |
532 | */ | |
533 | if (com == NULL || com->size == 0) | |
534 | str = XSTRDUP(MTYPE_COMMUNITY_STR, ""); | |
535 | else | |
536 | str = community_str_get(com, i); | |
5cbea288 | 537 | |
d62a17ae | 538 | /* Regular expression match. */ |
539 | rv = regexec(reg, str, 0, NULL, 0); | |
b84ee83b | 540 | |
d62a17ae | 541 | XFREE(MTYPE_COMMUNITY_STR, str); |
b84ee83b | 542 | |
88f1c947 | 543 | return rv == 0; |
5cbea288 DS |
544 | } |
545 | ||
718e3744 | 546 | /* Internal function to perform regular expression match for community |
547 | attribute. */ | |
88f1c947 | 548 | static bool community_regexp_match(struct community *com, regex_t *reg) |
718e3744 | 549 | { |
d62a17ae | 550 | const char *str; |
718e3744 | 551 | |
d62a17ae | 552 | /* When there is no communities attribute it is treated as empty |
553 | string. */ | |
554 | if (com == NULL || com->size == 0) | |
555 | str = ""; | |
556 | else | |
a69ea8ae | 557 | str = community_str(com, false); |
718e3744 | 558 | |
d62a17ae | 559 | /* Regular expression match. */ |
560 | if (regexec(reg, str, 0, NULL, 0) == 0) | |
88f1c947 | 561 | return true; |
718e3744 | 562 | |
d62a17ae | 563 | /* No match. */ |
88f1c947 | 564 | return false; |
718e3744 | 565 | } |
566 | ||
d62a17ae | 567 | static char *lcommunity_str_get(struct lcommunity *lcom, int i) |
57d187bc | 568 | { |
d62a17ae | 569 | struct lcommunity_val lcomval; |
d7c0a89a QY |
570 | uint32_t globaladmin; |
571 | uint32_t localdata1; | |
572 | uint32_t localdata2; | |
d62a17ae | 573 | char *str; |
1be1693e | 574 | const uint8_t *ptr; |
d62a17ae | 575 | |
ff9104a2 | 576 | ptr = lcom->val + (i * LCOMMUNITY_SIZE); |
d62a17ae | 577 | |
578 | memcpy(&lcomval, ptr, LCOMMUNITY_SIZE); | |
579 | ||
580 | /* Allocate memory. 48 bytes taken off bgp_lcommunity.c */ | |
d7c0a89a | 581 | ptr = (uint8_t *)lcomval.val; |
937652c6 DL |
582 | ptr = ptr_get_be32(ptr, &globaladmin); |
583 | ptr = ptr_get_be32(ptr, &localdata1); | |
584 | ptr = ptr_get_be32(ptr, &localdata2); | |
585 | (void)ptr; /* consume value */ | |
d62a17ae | 586 | |
fc746f1c QY |
587 | str = XMALLOC(MTYPE_LCOMMUNITY_STR, 48); |
588 | snprintf(str, 48, "%u:%u:%u", globaladmin, localdata1, localdata2); | |
d62a17ae | 589 | |
590 | return str; | |
57d187bc JS |
591 | } |
592 | ||
593 | /* Internal function to perform regular expression match for | |
2acb4ac2 | 594 | * a single community. */ |
88f1c947 DA |
595 | static bool lcommunity_regexp_include(regex_t *reg, struct lcommunity *lcom, |
596 | int i) | |
57d187bc | 597 | { |
d62a17ae | 598 | char *str; |
599 | ||
600 | /* When there is no communities attribute it is treated as empty string. | |
601 | */ | |
602 | if (lcom == NULL || lcom->size == 0) | |
603 | str = XSTRDUP(MTYPE_LCOMMUNITY_STR, ""); | |
604 | else | |
605 | str = lcommunity_str_get(lcom, i); | |
606 | ||
607 | /* Regular expression match. */ | |
608 | if (regexec(reg, str, 0, NULL, 0) == 0) { | |
609 | XFREE(MTYPE_LCOMMUNITY_STR, str); | |
88f1c947 | 610 | return true; |
d62a17ae | 611 | } |
57d187bc | 612 | |
d62a17ae | 613 | XFREE(MTYPE_LCOMMUNITY_STR, str); |
614 | /* No match. */ | |
88f1c947 | 615 | return false; |
57d187bc JS |
616 | } |
617 | ||
88f1c947 | 618 | static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg) |
57d187bc | 619 | { |
d62a17ae | 620 | const char *str; |
57d187bc | 621 | |
d62a17ae | 622 | /* When there is no communities attribute it is treated as empty |
623 | string. */ | |
624 | if (com == NULL || com->size == 0) | |
625 | str = ""; | |
626 | else | |
8d9b8ed9 | 627 | str = lcommunity_str(com, false); |
57d187bc | 628 | |
d62a17ae | 629 | /* Regular expression match. */ |
630 | if (regexec(reg, str, 0, NULL, 0) == 0) | |
88f1c947 | 631 | return true; |
57d187bc | 632 | |
d62a17ae | 633 | /* No match. */ |
88f1c947 | 634 | return false; |
57d187bc JS |
635 | } |
636 | ||
637 | ||
88f1c947 | 638 | static bool ecommunity_regexp_match(struct ecommunity *ecom, regex_t *reg) |
8708b74f | 639 | { |
d62a17ae | 640 | const char *str; |
8708b74f | 641 | |
d62a17ae | 642 | /* When there is no communities attribute it is treated as empty |
643 | string. */ | |
644 | if (ecom == NULL || ecom->size == 0) | |
645 | str = ""; | |
646 | else | |
647 | str = ecommunity_str(ecom); | |
8708b74f | 648 | |
d62a17ae | 649 | /* Regular expression match. */ |
650 | if (regexec(reg, str, 0, NULL, 0) == 0) | |
88f1c947 | 651 | return true; |
8708b74f | 652 | |
d62a17ae | 653 | /* No match. */ |
88f1c947 | 654 | return false; |
8708b74f | 655 | } |
656 | ||
ffd0c037 | 657 | #if 0 |
718e3744 | 658 | /* Delete community attribute using regular expression match. Return |
659 | modified communites attribute. */ | |
660 | static struct community * | |
8708b74f | 661 | community_regexp_delete (struct community *com, regex_t * reg) |
718e3744 | 662 | { |
aa861c10 C |
663 | int i; |
664 | uint32_t comval; | |
665 | /* Maximum is "65535:65535" + '\0'. */ | |
666 | char c[12]; | |
667 | const char *str; | |
668 | ||
669 | if (!com) | |
670 | return NULL; | |
671 | ||
672 | i = 0; | |
673 | while (i < com->size) | |
674 | { | |
0d6f7fd6 | 675 | memcpy (&comval, com_nthval (com, i), sizeof(uint32_t)); |
aa861c10 C |
676 | comval = ntohl (comval); |
677 | ||
678 | switch (comval) { | |
679 | case COMMUNITY_INTERNET: | |
680 | str = "internet"; | |
681 | break; | |
682 | case COMMUNITY_ACCEPT_OWN: | |
683 | str = "accept-own"; | |
684 | break; | |
685 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4: | |
686 | str = "route-filter-translated-v4"; | |
687 | break; | |
688 | case COMMUNITY_ROUTE_FILTER_v4: | |
689 | str = "route-filter-v4"; | |
690 | break; | |
691 | case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6: | |
692 | str = "route-filter-translated-v6"; | |
693 | break; | |
694 | case COMMUNITY_ROUTE_FILTER_v6: | |
695 | str = "route-filter-v6"; | |
696 | break; | |
697 | case COMMUNITY_LLGR_STALE: | |
698 | str = "llgr-stale"; | |
699 | break; | |
700 | case COMMUNITY_NO_LLGR: | |
701 | str = "no-llgr"; | |
702 | break; | |
703 | case COMMUNITY_ACCEPT_OWN_NEXTHOP: | |
704 | str = "accept-own-nexthop"; | |
705 | break; | |
706 | case COMMUNITY_BLACKHOLE: | |
707 | str = "blackhole"; | |
708 | break; | |
709 | case COMMUNITY_NO_EXPORT: | |
710 | str = "no-export"; | |
711 | break; | |
712 | case COMMUNITY_NO_ADVERTISE: | |
713 | str = "no-advertise"; | |
714 | break; | |
715 | case COMMUNITY_LOCAL_AS: | |
716 | str = "local-AS"; | |
717 | break; | |
718 | case COMMUNITY_NO_PEER: | |
719 | str = "no-peer"; | |
720 | break; | |
721 | default: | |
722 | sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF, | |
723 | comval & 0xFFFF); | |
724 | str = c; | |
725 | break; | |
726 | } | |
727 | ||
728 | if (regexec (reg, str, 0, NULL, 0) == 0) | |
729 | community_del_val (com, com_nthval (com, i)); | |
730 | else | |
731 | i++; | |
732 | } | |
733 | return com; | |
718e3744 | 734 | } |
ffd0c037 | 735 | #endif |
718e3744 | 736 | |
737 | /* When given community attribute matches to the community-list return | |
738 | 1 else return 0. */ | |
88f1c947 | 739 | bool community_list_match(struct community *com, struct community_list *list) |
718e3744 | 740 | { |
d62a17ae | 741 | struct community_entry *entry; |
742 | ||
743 | for (entry = list->head; entry; entry = entry->next) { | |
744 | if (entry->any) | |
88f1c947 | 745 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 746 | |
747 | if (entry->style == COMMUNITY_LIST_STANDARD) { | |
748 | if (community_include(entry->u.com, COMMUNITY_INTERNET)) | |
88f1c947 | 749 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 750 | |
751 | if (community_match(com, entry->u.com)) | |
88f1c947 | 752 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 753 | } else if (entry->style == COMMUNITY_LIST_EXPANDED) { |
754 | if (community_regexp_match(com, entry->reg)) | |
88f1c947 | 755 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 756 | } |
757 | } | |
88f1c947 | 758 | return false; |
8708b74f | 759 | } |
760 | ||
88f1c947 | 761 | bool lcommunity_list_match(struct lcommunity *lcom, struct community_list *list) |
57d187bc | 762 | { |
d62a17ae | 763 | struct community_entry *entry; |
764 | ||
765 | for (entry = list->head; entry; entry = entry->next) { | |
766 | if (entry->any) | |
88f1c947 | 767 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 768 | |
769 | if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) { | |
770 | if (lcommunity_match(lcom, entry->u.lcom)) | |
88f1c947 | 771 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 772 | } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) { |
773 | if (lcommunity_regexp_match(lcom, entry->reg)) | |
88f1c947 | 774 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 775 | } |
776 | } | |
88f1c947 | 777 | return false; |
57d187bc JS |
778 | } |
779 | ||
f8463998 | 780 | |
781 | /* Perform exact matching. In case of expanded large-community-list, do | |
782 | * same thing as lcommunity_list_match(). | |
783 | */ | |
88f1c947 DA |
784 | bool lcommunity_list_exact_match(struct lcommunity *lcom, |
785 | struct community_list *list) | |
f8463998 | 786 | { |
787 | struct community_entry *entry; | |
788 | ||
789 | for (entry = list->head; entry; entry = entry->next) { | |
790 | if (entry->any) | |
88f1c947 | 791 | return entry->direct == COMMUNITY_PERMIT; |
f8463998 | 792 | |
793 | if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) { | |
794 | if (lcommunity_cmp(lcom, entry->u.com)) | |
88f1c947 | 795 | return entry->direct == COMMUNITY_PERMIT; |
f8463998 | 796 | } else if (entry->style == LARGE_COMMUNITY_LIST_EXPANDED) { |
797 | if (lcommunity_regexp_match(lcom, entry->reg)) | |
88f1c947 | 798 | return entry->direct == COMMUNITY_PERMIT; |
f8463998 | 799 | } |
800 | } | |
88f1c947 | 801 | return false; |
f8463998 | 802 | } |
803 | ||
88f1c947 | 804 | bool ecommunity_list_match(struct ecommunity *ecom, struct community_list *list) |
8708b74f | 805 | { |
d62a17ae | 806 | struct community_entry *entry; |
807 | ||
808 | for (entry = list->head; entry; entry = entry->next) { | |
809 | if (entry->any) | |
88f1c947 | 810 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 811 | |
812 | if (entry->style == EXTCOMMUNITY_LIST_STANDARD) { | |
813 | if (ecommunity_match(ecom, entry->u.ecom)) | |
88f1c947 | 814 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 815 | } else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED) { |
816 | if (ecommunity_regexp_match(ecom, entry->reg)) | |
88f1c947 | 817 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 818 | } |
819 | } | |
88f1c947 | 820 | return false; |
718e3744 | 821 | } |
822 | ||
823 | /* Perform exact matching. In case of expanded community-list, do | |
824 | same thing as community_list_match(). */ | |
88f1c947 DA |
825 | bool community_list_exact_match(struct community *com, |
826 | struct community_list *list) | |
718e3744 | 827 | { |
d62a17ae | 828 | struct community_entry *entry; |
829 | ||
830 | for (entry = list->head; entry; entry = entry->next) { | |
831 | if (entry->any) | |
88f1c947 | 832 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 833 | |
834 | if (entry->style == COMMUNITY_LIST_STANDARD) { | |
835 | if (community_include(entry->u.com, COMMUNITY_INTERNET)) | |
88f1c947 | 836 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 837 | |
838 | if (community_cmp(com, entry->u.com)) | |
88f1c947 | 839 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 840 | } else if (entry->style == COMMUNITY_LIST_EXPANDED) { |
841 | if (community_regexp_match(com, entry->reg)) | |
88f1c947 | 842 | return entry->direct == COMMUNITY_PERMIT; |
d62a17ae | 843 | } |
844 | } | |
88f1c947 | 845 | return false; |
718e3744 | 846 | } |
847 | ||
8708b74f | 848 | /* Delete all permitted communities in the list from com. */ |
d62a17ae | 849 | struct community *community_list_match_delete(struct community *com, |
850 | struct community_list *list) | |
718e3744 | 851 | { |
d62a17ae | 852 | struct community_entry *entry; |
d7c0a89a QY |
853 | uint32_t val; |
854 | uint32_t com_index_to_delete[com->size]; | |
d62a17ae | 855 | int delete_index = 0; |
856 | int i; | |
857 | ||
858 | /* Loop over each community value and evaluate each against the | |
859 | * community-list. If we need to delete a community value add its index | |
ceead39c | 860 | * to com_index_to_delete. |
d62a17ae | 861 | */ |
862 | for (i = 0; i < com->size; i++) { | |
863 | val = community_val_get(com, i); | |
864 | ||
865 | for (entry = list->head; entry; entry = entry->next) { | |
866 | if (entry->any) { | |
867 | if (entry->direct == COMMUNITY_PERMIT) { | |
868 | com_index_to_delete[delete_index] = i; | |
869 | delete_index++; | |
870 | } | |
871 | break; | |
872 | } | |
873 | ||
874 | else if ((entry->style == COMMUNITY_LIST_STANDARD) | |
875 | && (community_include(entry->u.com, | |
876 | COMMUNITY_INTERNET) | |
877 | || community_include(entry->u.com, val))) { | |
878 | if (entry->direct == COMMUNITY_PERMIT) { | |
879 | com_index_to_delete[delete_index] = i; | |
880 | delete_index++; | |
881 | } | |
882 | break; | |
883 | } | |
884 | ||
885 | else if ((entry->style == COMMUNITY_LIST_EXPANDED) | |
886 | && community_regexp_include(entry->reg, com, | |
887 | i)) { | |
888 | if (entry->direct == COMMUNITY_PERMIT) { | |
889 | com_index_to_delete[delete_index] = i; | |
890 | delete_index++; | |
891 | } | |
892 | break; | |
893 | } | |
894 | } | |
895 | } | |
5cbea288 | 896 | |
d62a17ae | 897 | /* Delete all of the communities we flagged for deletion */ |
898 | for (i = delete_index - 1; i >= 0; i--) { | |
899 | val = community_val_get(com, com_index_to_delete[i]); | |
0743b61d | 900 | val = htonl(val); |
d62a17ae | 901 | community_del_val(com, &val); |
902 | } | |
5cbea288 | 903 | |
d62a17ae | 904 | return com; |
718e3744 | 905 | } |
906 | ||
907 | /* To avoid duplicated entry in the community-list, this function | |
908 | compares specified entry to existing entry. */ | |
88f1c947 DA |
909 | static bool community_list_dup_check(struct community_list *list, |
910 | struct community_entry *new) | |
718e3744 | 911 | { |
d62a17ae | 912 | struct community_entry *entry; |
913 | ||
914 | for (entry = list->head; entry; entry = entry->next) { | |
915 | if (entry->style != new->style) | |
916 | continue; | |
917 | ||
918 | if (entry->direct != new->direct) | |
919 | continue; | |
920 | ||
921 | if (entry->any != new->any) | |
922 | continue; | |
923 | ||
924 | if (entry->any) | |
88f1c947 | 925 | return true; |
d62a17ae | 926 | |
927 | switch (entry->style) { | |
928 | case COMMUNITY_LIST_STANDARD: | |
929 | if (community_cmp(entry->u.com, new->u.com)) | |
88f1c947 | 930 | return true; |
d62a17ae | 931 | break; |
932 | case LARGE_COMMUNITY_LIST_STANDARD: | |
933 | if (lcommunity_cmp(entry->u.lcom, new->u.lcom)) | |
88f1c947 | 934 | return true; |
d62a17ae | 935 | break; |
936 | case EXTCOMMUNITY_LIST_STANDARD: | |
937 | if (ecommunity_cmp(entry->u.ecom, new->u.ecom)) | |
88f1c947 | 938 | return true; |
d62a17ae | 939 | break; |
940 | case COMMUNITY_LIST_EXPANDED: | |
941 | case EXTCOMMUNITY_LIST_EXPANDED: | |
942 | case LARGE_COMMUNITY_LIST_EXPANDED: | |
943 | if (strcmp(entry->config, new->config) == 0) | |
88f1c947 | 944 | return true; |
d62a17ae | 945 | break; |
946 | default: | |
947 | break; | |
948 | } | |
949 | } | |
88f1c947 | 950 | return false; |
718e3744 | 951 | } |
6b0655a2 | 952 | |
718e3744 | 953 | /* Set community-list. */ |
d62a17ae | 954 | int community_list_set(struct community_list_handler *ch, const char *name, |
2f8cc0e5 | 955 | const char *str, const char *seq, int direct, int style) |
718e3744 | 956 | { |
d62a17ae | 957 | struct community_entry *entry = NULL; |
958 | struct community_list *list; | |
959 | struct community *com = NULL; | |
960 | regex_t *regex = NULL; | |
2f8cc0e5 DA |
961 | int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO; |
962 | ||
963 | if (seq) | |
964 | seqnum = (int64_t)atol(seq); | |
d62a17ae | 965 | |
966 | /* Get community list. */ | |
967 | list = community_list_get(ch, name, COMMUNITY_LIST_MASTER); | |
968 | ||
969 | /* When community-list already has entry, new entry should have same | |
970 | style. If you want to have mixed style community-list, you can | |
971 | comment out this check. */ | |
972 | if (!community_list_empty_p(list)) { | |
973 | struct community_entry *first; | |
974 | ||
975 | first = list->head; | |
976 | ||
977 | if (style != first->style) { | |
978 | return (first->style == COMMUNITY_LIST_STANDARD | |
979 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT | |
980 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); | |
981 | } | |
fee6e4e4 | 982 | } |
718e3744 | 983 | |
d62a17ae | 984 | if (str) { |
985 | if (style == COMMUNITY_LIST_STANDARD) | |
986 | com = community_str2com(str); | |
987 | else | |
988 | regex = bgp_regcomp(str); | |
8708b74f | 989 | |
d62a17ae | 990 | if (!com && !regex) |
991 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
992 | } | |
718e3744 | 993 | |
d62a17ae | 994 | entry = community_entry_new(); |
995 | entry->direct = direct; | |
996 | entry->style = style; | |
d3f6c580 | 997 | entry->any = (str ? false : true); |
d62a17ae | 998 | entry->u.com = com; |
999 | entry->reg = regex; | |
2f8cc0e5 | 1000 | entry->seq = seqnum; |
d62a17ae | 1001 | entry->config = |
1002 | (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL); | |
1003 | ||
1004 | /* Do not put duplicated community entry. */ | |
1005 | if (community_list_dup_check(list, entry)) | |
1006 | community_entry_free(entry); | |
1007 | else { | |
2f8cc0e5 DA |
1008 | community_list_entry_add(list, entry, ch, |
1009 | COMMUNITY_LIST_MASTER); | |
d62a17ae | 1010 | route_map_notify_dependencies(name, RMAP_EVENT_CLIST_ADDED); |
1011 | } | |
718e3744 | 1012 | |
d62a17ae | 1013 | return 0; |
718e3744 | 1014 | } |
1015 | ||
813d4307 | 1016 | /* Unset community-list */ |
d62a17ae | 1017 | int community_list_unset(struct community_list_handler *ch, const char *name, |
2f8cc0e5 DA |
1018 | const char *str, const char *seq, int direct, |
1019 | int style) | |
718e3744 | 1020 | { |
3571a6a2 | 1021 | struct community_list_master *cm = NULL; |
d62a17ae | 1022 | struct community_entry *entry = NULL; |
1023 | struct community_list *list; | |
1024 | struct community *com = NULL; | |
1025 | ||
1026 | /* Lookup community list. */ | |
e237b0d2 | 1027 | list = community_list_lookup(ch, name, 0, COMMUNITY_LIST_MASTER); |
d62a17ae | 1028 | if (list == NULL) |
1029 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
1030 | ||
3571a6a2 | 1031 | cm = community_list_master_lookup(ch, COMMUNITY_LIST_MASTER); |
d62a17ae | 1032 | /* Delete all of entry belongs to this community-list. */ |
7298a8e1 | 1033 | if (!str) { |
3571a6a2 | 1034 | community_list_delete(cm, list); |
d62a17ae | 1035 | route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED); |
1036 | return 0; | |
1037 | } | |
718e3744 | 1038 | |
7298a8e1 QY |
1039 | if (style == COMMUNITY_LIST_STANDARD) |
1040 | com = community_str2com(str); | |
718e3744 | 1041 | |
d62a17ae | 1042 | if (com) { |
1043 | entry = community_list_entry_lookup(list, com, direct); | |
3c1f53de | 1044 | community_free(&com); |
d62a17ae | 1045 | } else |
1046 | entry = community_list_entry_lookup(list, str, direct); | |
fee6e4e4 | 1047 | |
d62a17ae | 1048 | if (!entry) |
1049 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
718e3744 | 1050 | |
3571a6a2 | 1051 | community_list_entry_delete(cm, list, entry); |
d62a17ae | 1052 | route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED); |
718e3744 | 1053 | |
d62a17ae | 1054 | return 0; |
718e3744 | 1055 | } |
1056 | ||
57d187bc | 1057 | /* Delete all permitted large communities in the list from com. */ |
d62a17ae | 1058 | struct lcommunity *lcommunity_list_match_delete(struct lcommunity *lcom, |
1059 | struct community_list *list) | |
57d187bc | 1060 | { |
d62a17ae | 1061 | struct community_entry *entry; |
d7c0a89a QY |
1062 | uint32_t com_index_to_delete[lcom->size]; |
1063 | uint8_t *ptr; | |
d62a17ae | 1064 | int delete_index = 0; |
1065 | int i; | |
1066 | ||
1067 | /* Loop over each lcommunity value and evaluate each against the | |
1068 | * community-list. If we need to delete a community value add its index | |
ceead39c | 1069 | * to com_index_to_delete. |
d62a17ae | 1070 | */ |
d62a17ae | 1071 | for (i = 0; i < lcom->size; i++) { |
534fc195 | 1072 | ptr = lcom->val + (i * LCOMMUNITY_SIZE); |
d62a17ae | 1073 | for (entry = list->head; entry; entry = entry->next) { |
1074 | if (entry->any) { | |
1075 | if (entry->direct == COMMUNITY_PERMIT) { | |
1076 | com_index_to_delete[delete_index] = i; | |
1077 | delete_index++; | |
1078 | } | |
1079 | break; | |
1080 | } | |
1081 | ||
1082 | else if ((entry->style == LARGE_COMMUNITY_LIST_STANDARD) | |
1083 | && lcommunity_include(entry->u.lcom, ptr)) { | |
1084 | if (entry->direct == COMMUNITY_PERMIT) { | |
1085 | com_index_to_delete[delete_index] = i; | |
1086 | delete_index++; | |
1087 | } | |
1088 | break; | |
1089 | } | |
1090 | ||
cde8d696 | 1091 | else if ((entry->style == LARGE_COMMUNITY_LIST_EXPANDED) |
d62a17ae | 1092 | && lcommunity_regexp_include(entry->reg, lcom, |
1093 | i)) { | |
1094 | if (entry->direct == COMMUNITY_PERMIT) { | |
1095 | com_index_to_delete[delete_index] = i; | |
1096 | delete_index++; | |
1097 | } | |
1098 | break; | |
1099 | } | |
1100 | } | |
1101 | } | |
57d187bc | 1102 | |
d62a17ae | 1103 | /* Delete all of the communities we flagged for deletion */ |
d62a17ae | 1104 | for (i = delete_index - 1; i >= 0; i--) { |
0a7dce9b | 1105 | ptr = lcom->val + (com_index_to_delete[i] * LCOMMUNITY_SIZE); |
d62a17ae | 1106 | lcommunity_del_val(lcom, ptr); |
1107 | } | |
57d187bc | 1108 | |
d62a17ae | 1109 | return lcom; |
57d187bc JS |
1110 | } |
1111 | ||
5bd66e2d | 1112 | /* Helper to check if every octet do not exceed UINT_MAX */ |
88f1c947 | 1113 | static bool lcommunity_list_valid(const char *community) |
5bd66e2d | 1114 | { |
bc2c9ae6 DA |
1115 | int octets; |
1116 | char **splits, **communities; | |
1117 | int num, num_communities; | |
5bd66e2d | 1118 | |
bc2c9ae6 | 1119 | frrstr_split(community, " ", &communities, &num_communities); |
5bd66e2d | 1120 | |
bc2c9ae6 DA |
1121 | for (int j = 0; j < num_communities; j++) { |
1122 | octets = 0; | |
1123 | frrstr_split(communities[j], ":", &splits, &num); | |
1124 | ||
1125 | for (int i = 0; i < num; i++) { | |
1126 | if (strtoul(splits[i], NULL, 10) > UINT_MAX) | |
1127 | return false; | |
5bd66e2d | 1128 | |
bc2c9ae6 DA |
1129 | if (strlen(splits[i]) == 0) |
1130 | return false; | |
1131 | ||
1132 | octets++; | |
1133 | XFREE(MTYPE_TMP, splits[i]); | |
1134 | } | |
1135 | XFREE(MTYPE_TMP, splits); | |
1136 | ||
1137 | if (octets < 3) | |
88f1c947 | 1138 | return false; |
5bd66e2d | 1139 | |
bc2c9ae6 | 1140 | XFREE(MTYPE_TMP, communities[j]); |
5bd66e2d | 1141 | } |
bc2c9ae6 | 1142 | XFREE(MTYPE_TMP, communities); |
5bd66e2d | 1143 | |
88f1c947 | 1144 | return true; |
5bd66e2d DA |
1145 | } |
1146 | ||
57d187bc | 1147 | /* Set lcommunity-list. */ |
d62a17ae | 1148 | int lcommunity_list_set(struct community_list_handler *ch, const char *name, |
2f8cc0e5 | 1149 | const char *str, const char *seq, int direct, int style) |
57d187bc | 1150 | { |
d62a17ae | 1151 | struct community_entry *entry = NULL; |
1152 | struct community_list *list; | |
1153 | struct lcommunity *lcom = NULL; | |
1154 | regex_t *regex = NULL; | |
2f8cc0e5 DA |
1155 | int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO; |
1156 | ||
1157 | if (seq) | |
1158 | seqnum = (int64_t)atol(seq); | |
d62a17ae | 1159 | |
1160 | /* Get community list. */ | |
1161 | list = community_list_get(ch, name, LARGE_COMMUNITY_LIST_MASTER); | |
1162 | ||
1163 | /* When community-list already has entry, new entry should have same | |
1164 | style. If you want to have mixed style community-list, you can | |
1165 | comment out this check. */ | |
1166 | if (!community_list_empty_p(list)) { | |
1167 | struct community_entry *first; | |
1168 | ||
1169 | first = list->head; | |
1170 | ||
1171 | if (style != first->style) { | |
1172 | return (first->style == COMMUNITY_LIST_STANDARD | |
1173 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT | |
1174 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); | |
1175 | } | |
1176 | } | |
57d187bc | 1177 | |
d62a17ae | 1178 | if (str) { |
5bd66e2d DA |
1179 | if (!lcommunity_list_valid(str)) |
1180 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
1181 | ||
d62a17ae | 1182 | if (style == LARGE_COMMUNITY_LIST_STANDARD) |
1183 | lcom = lcommunity_str2com(str); | |
1184 | else | |
1185 | regex = bgp_regcomp(str); | |
57d187bc | 1186 | |
d62a17ae | 1187 | if (!lcom && !regex) |
1188 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
1189 | } | |
57d187bc | 1190 | |
d62a17ae | 1191 | entry = community_entry_new(); |
1192 | entry->direct = direct; | |
1193 | entry->style = style; | |
d3f6c580 | 1194 | entry->any = (str ? false : true); |
d62a17ae | 1195 | entry->u.lcom = lcom; |
1196 | entry->reg = regex; | |
2f8cc0e5 | 1197 | entry->seq = seqnum; |
8d9b8ed9 PM |
1198 | entry->config = |
1199 | (regex ? XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL); | |
d62a17ae | 1200 | |
1201 | /* Do not put duplicated community entry. */ | |
1202 | if (community_list_dup_check(list, entry)) | |
1203 | community_entry_free(entry); | |
35f6f850 | 1204 | else { |
2f8cc0e5 DA |
1205 | community_list_entry_add(list, entry, ch, |
1206 | LARGE_COMMUNITY_LIST_MASTER); | |
35f6f850 | 1207 | route_map_notify_dependencies(name, RMAP_EVENT_LLIST_ADDED); |
1208 | } | |
d62a17ae | 1209 | |
1210 | return 0; | |
57d187bc JS |
1211 | } |
1212 | ||
1213 | /* Unset community-list. When str is NULL, delete all of | |
1214 | community-list entry belongs to the specified name. */ | |
d62a17ae | 1215 | int lcommunity_list_unset(struct community_list_handler *ch, const char *name, |
2f8cc0e5 DA |
1216 | const char *str, const char *seq, int direct, |
1217 | int style) | |
57d187bc | 1218 | { |
3571a6a2 | 1219 | struct community_list_master *cm = NULL; |
d62a17ae | 1220 | struct community_entry *entry = NULL; |
1221 | struct community_list *list; | |
1222 | struct lcommunity *lcom = NULL; | |
1223 | regex_t *regex = NULL; | |
1224 | ||
1225 | /* Lookup community list. */ | |
e237b0d2 | 1226 | list = community_list_lookup(ch, name, 0, LARGE_COMMUNITY_LIST_MASTER); |
d62a17ae | 1227 | if (list == NULL) |
1228 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
1229 | ||
3571a6a2 | 1230 | cm = community_list_master_lookup(ch, LARGE_COMMUNITY_LIST_MASTER); |
d62a17ae | 1231 | /* Delete all of entry belongs to this community-list. */ |
1232 | if (!str) { | |
3571a6a2 | 1233 | community_list_delete(cm, list); |
35f6f850 | 1234 | route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED); |
d62a17ae | 1235 | return 0; |
1236 | } | |
57d187bc | 1237 | |
d62a17ae | 1238 | if (style == LARGE_COMMUNITY_LIST_STANDARD) |
1239 | lcom = lcommunity_str2com(str); | |
1240 | else | |
1241 | regex = bgp_regcomp(str); | |
57d187bc | 1242 | |
d62a17ae | 1243 | if (!lcom && !regex) |
1244 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
57d187bc | 1245 | |
d62a17ae | 1246 | if (lcom) |
1247 | entry = community_list_entry_lookup(list, lcom, direct); | |
1248 | else | |
1249 | entry = community_list_entry_lookup(list, str, direct); | |
57d187bc | 1250 | |
d62a17ae | 1251 | if (lcom) |
1252 | lcommunity_free(&lcom); | |
1253 | if (regex) | |
1254 | bgp_regex_free(regex); | |
57d187bc | 1255 | |
d62a17ae | 1256 | if (!entry) |
1257 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
57d187bc | 1258 | |
3571a6a2 | 1259 | community_list_entry_delete(cm, list, entry); |
35f6f850 | 1260 | route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED); |
57d187bc | 1261 | |
d62a17ae | 1262 | return 0; |
57d187bc JS |
1263 | } |
1264 | ||
718e3744 | 1265 | /* Set extcommunity-list. */ |
d62a17ae | 1266 | int extcommunity_list_set(struct community_list_handler *ch, const char *name, |
2f8cc0e5 DA |
1267 | const char *str, const char *seq, int direct, |
1268 | int style) | |
718e3744 | 1269 | { |
d62a17ae | 1270 | struct community_entry *entry = NULL; |
1271 | struct community_list *list; | |
1272 | struct ecommunity *ecom = NULL; | |
1273 | regex_t *regex = NULL; | |
2f8cc0e5 DA |
1274 | int64_t seqnum = COMMUNITY_SEQ_NUMBER_AUTO; |
1275 | ||
1276 | if (seq) | |
1277 | seqnum = (int64_t)atol(seq); | |
718e3744 | 1278 | |
fa301630 | 1279 | if (str == NULL) |
1280 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
1281 | ||
d62a17ae | 1282 | /* Get community list. */ |
1283 | list = community_list_get(ch, name, EXTCOMMUNITY_LIST_MASTER); | |
718e3744 | 1284 | |
d62a17ae | 1285 | /* When community-list already has entry, new entry should have same |
1286 | style. If you want to have mixed style community-list, you can | |
1287 | comment out this check. */ | |
1288 | if (!community_list_empty_p(list)) { | |
1289 | struct community_entry *first; | |
718e3744 | 1290 | |
d62a17ae | 1291 | first = list->head; |
718e3744 | 1292 | |
d62a17ae | 1293 | if (style != first->style) { |
1294 | return (first->style == EXTCOMMUNITY_LIST_STANDARD | |
1295 | ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT | |
1296 | : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT); | |
1297 | } | |
fee6e4e4 | 1298 | } |
718e3744 | 1299 | |
d62a17ae | 1300 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
1301 | ecom = ecommunity_str2com(str, 0, 1); | |
1302 | else | |
1303 | regex = bgp_regcomp(str); | |
1304 | ||
1305 | if (!ecom && !regex) | |
1306 | return COMMUNITY_LIST_ERR_MALFORMED_VAL; | |
1307 | ||
1308 | if (ecom) | |
1309 | ecom->str = | |
1310 | ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_DISPLAY, 0); | |
1311 | ||
1312 | entry = community_entry_new(); | |
1313 | entry->direct = direct; | |
1314 | entry->style = style; | |
d3f6c580 | 1315 | entry->any = false; |
d62a17ae | 1316 | if (ecom) |
1317 | entry->config = ecommunity_ecom2str( | |
1318 | ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0); | |
1319 | else if (regex) | |
1320 | entry->config = XSTRDUP(MTYPE_COMMUNITY_LIST_CONFIG, str); | |
1321 | ||
1322 | entry->u.ecom = ecom; | |
1323 | entry->reg = regex; | |
2f8cc0e5 | 1324 | entry->seq = seqnum; |
d62a17ae | 1325 | |
1326 | /* Do not put duplicated community entry. */ | |
1327 | if (community_list_dup_check(list, entry)) | |
1328 | community_entry_free(entry); | |
1329 | else { | |
2f8cc0e5 DA |
1330 | community_list_entry_add(list, entry, ch, |
1331 | EXTCOMMUNITY_LIST_MASTER); | |
d62a17ae | 1332 | route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_ADDED); |
1333 | } | |
718e3744 | 1334 | |
d62a17ae | 1335 | return 0; |
718e3744 | 1336 | } |
1337 | ||
7298a8e1 QY |
1338 | /* Unset extcommunity-list. |
1339 | * | |
1340 | * When str is NULL, delete all extcommunity-list entries belonging to the | |
1341 | * specified name. | |
1342 | */ | |
d62a17ae | 1343 | int extcommunity_list_unset(struct community_list_handler *ch, const char *name, |
2f8cc0e5 DA |
1344 | const char *str, const char *seq, int direct, |
1345 | int style) | |
718e3744 | 1346 | { |
3571a6a2 | 1347 | struct community_list_master *cm = NULL; |
d62a17ae | 1348 | struct community_entry *entry = NULL; |
1349 | struct community_list *list; | |
1350 | struct ecommunity *ecom = NULL; | |
1351 | ||
1352 | /* Lookup extcommunity list. */ | |
e237b0d2 | 1353 | list = community_list_lookup(ch, name, 0, EXTCOMMUNITY_LIST_MASTER); |
d62a17ae | 1354 | if (list == NULL) |
1355 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
1356 | ||
3571a6a2 | 1357 | cm = community_list_master_lookup(ch, EXTCOMMUNITY_LIST_MASTER); |
d62a17ae | 1358 | /* Delete all of entry belongs to this extcommunity-list. */ |
7298a8e1 | 1359 | if (!str) { |
3571a6a2 | 1360 | community_list_delete(cm, list); |
d62a17ae | 1361 | route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED); |
1362 | return 0; | |
1363 | } | |
718e3744 | 1364 | |
7298a8e1 QY |
1365 | if (style == EXTCOMMUNITY_LIST_STANDARD) |
1366 | ecom = ecommunity_str2com(str, 0, 1); | |
718e3744 | 1367 | |
d62a17ae | 1368 | if (ecom) { |
1369 | entry = community_list_entry_lookup(list, ecom, direct); | |
1370 | ecommunity_free(&ecom); | |
1371 | } else | |
1372 | entry = community_list_entry_lookup(list, str, direct); | |
fee6e4e4 | 1373 | |
d62a17ae | 1374 | if (!entry) |
1375 | return COMMUNITY_LIST_ERR_CANT_FIND_LIST; | |
718e3744 | 1376 | |
3571a6a2 | 1377 | community_list_entry_delete(cm, list, entry); |
d62a17ae | 1378 | route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED); |
718e3744 | 1379 | |
d62a17ae | 1380 | return 0; |
718e3744 | 1381 | } |
1382 | ||
1383 | /* Initializa community-list. Return community-list handler. */ | |
d62a17ae | 1384 | struct community_list_handler *community_list_init(void) |
718e3744 | 1385 | { |
d62a17ae | 1386 | struct community_list_handler *ch; |
1387 | ch = XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER, | |
1388 | sizeof(struct community_list_handler)); | |
3571a6a2 DS |
1389 | |
1390 | ch->community_list.hash = | |
1391 | hash_create_size(4, bgp_clist_hash_key_community_list, | |
1392 | bgp_clist_hash_cmp_community_list, | |
1393 | "Community List Number Quick Lookup"); | |
1394 | ||
1395 | ch->extcommunity_list.hash = | |
1396 | hash_create_size(4, bgp_clist_hash_key_community_list, | |
1397 | bgp_clist_hash_cmp_community_list, | |
1398 | "Extended Community List Quick Lookup"); | |
1399 | ||
1400 | ch->lcommunity_list.hash = | |
1401 | hash_create_size(4, bgp_clist_hash_key_community_list, | |
1402 | bgp_clist_hash_cmp_community_list, | |
1403 | "Large Community List Quick Lookup"); | |
1404 | ||
d62a17ae | 1405 | return ch; |
718e3744 | 1406 | } |
1407 | ||
1408 | /* Terminate community-list. */ | |
d62a17ae | 1409 | void community_list_terminate(struct community_list_handler *ch) |
718e3744 | 1410 | { |
d62a17ae | 1411 | struct community_list_master *cm; |
1412 | struct community_list *list; | |
1413 | ||
1414 | cm = &ch->community_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 | cm = &ch->lcommunity_list; | |
1422 | while ((list = cm->num.head) != NULL) | |
3571a6a2 | 1423 | community_list_delete(cm, list); |
d62a17ae | 1424 | while ((list = cm->str.head) != NULL) |
3571a6a2 DS |
1425 | community_list_delete(cm, list); |
1426 | hash_free(cm->hash); | |
d62a17ae | 1427 | |
1428 | cm = &ch->extcommunity_list; | |
1429 | while ((list = cm->num.head) != NULL) | |
3571a6a2 | 1430 | community_list_delete(cm, list); |
d62a17ae | 1431 | while ((list = cm->str.head) != NULL) |
3571a6a2 DS |
1432 | community_list_delete(cm, list); |
1433 | hash_free(cm->hash); | |
d62a17ae | 1434 | |
1435 | XFREE(MTYPE_COMMUNITY_LIST_HANDLER, ch); | |
718e3744 | 1436 | } |