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