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