]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_community.c
Merge pull request #6031 from donaldsharp/debug_zebra_kernel
[mirror_frr.git] / bgpd / bgp_community.c
1 /* Community attribute related functions.
2 * Copyright (C) 1998, 2001 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 "hash.h"
25 #include "memory.h"
26 #include "jhash.h"
27
28 #include "bgpd/bgp_memory.h"
29 #include "bgpd/bgp_community.h"
30
31 /* Hash of community attribute. */
32 static struct hash *comhash;
33
34 /* Allocate a new communities value. */
35 static struct community *community_new(void)
36 {
37 return XCALLOC(MTYPE_COMMUNITY, sizeof(struct community));
38 }
39
40 /* Free communities value. */
41 void community_free(struct community **com)
42 {
43 XFREE(MTYPE_COMMUNITY_VAL, (*com)->val);
44 XFREE(MTYPE_COMMUNITY_STR, (*com)->str);
45
46 if ((*com)->json) {
47 json_object_free((*com)->json);
48 (*com)->json = NULL;
49 }
50
51 XFREE(MTYPE_COMMUNITY, (*com));
52 }
53
54 /* Add one community value to the community. */
55 static void community_add_val(struct community *com, uint32_t val)
56 {
57 com->size++;
58 if (com->val)
59 com->val = XREALLOC(MTYPE_COMMUNITY_VAL, com->val,
60 com_length(com));
61 else
62 com->val = XMALLOC(MTYPE_COMMUNITY_VAL, com_length(com));
63
64 val = htonl(val);
65 memcpy(com_lastval(com), &val, sizeof(uint32_t));
66 }
67
68 /* Delete one community. */
69 void community_del_val(struct community *com, uint32_t *val)
70 {
71 int i = 0;
72 int c = 0;
73
74 if (!com->val)
75 return;
76
77 while (i < com->size) {
78 if (memcmp(com->val + i, val, sizeof(uint32_t)) == 0) {
79 c = com->size - i - 1;
80
81 if (c > 0)
82 memmove(com->val + i, com->val + (i + 1),
83 c * sizeof(*val));
84
85 com->size--;
86
87 if (com->size > 0)
88 com->val = XREALLOC(MTYPE_COMMUNITY_VAL,
89 com->val, com_length(com));
90 else {
91 XFREE(MTYPE_COMMUNITY_VAL, com->val);
92 }
93 return;
94 }
95 i++;
96 }
97 }
98
99 /* Delete all communities listed in com2 from com1 */
100 struct community *community_delete(struct community *com1,
101 struct community *com2)
102 {
103 int i = 0;
104
105 while (i < com2->size) {
106 community_del_val(com1, com2->val + i);
107 i++;
108 }
109
110 return com1;
111 }
112
113 /* Callback function from qsort(). */
114 static int community_compare(const void *a1, const void *a2)
115 {
116 uint32_t v1;
117 uint32_t v2;
118
119 memcpy(&v1, a1, sizeof(uint32_t));
120 memcpy(&v2, a2, sizeof(uint32_t));
121 v1 = ntohl(v1);
122 v2 = ntohl(v2);
123
124 if (v1 < v2)
125 return -1;
126 if (v1 > v2)
127 return 1;
128 return 0;
129 }
130
131 bool community_include(struct community *com, uint32_t val)
132 {
133 int i;
134
135 val = htonl(val);
136
137 for (i = 0; i < com->size; i++)
138 if (memcmp(&val, com_nthval(com, i), sizeof(uint32_t)) == 0)
139 return true;
140 return false;
141 }
142
143 uint32_t community_val_get(struct community *com, int i)
144 {
145 uint8_t *p;
146 uint32_t val;
147
148 p = (uint8_t *)com->val;
149 p += (i * 4);
150
151 memcpy(&val, p, sizeof(uint32_t));
152
153 return ntohl(val);
154 }
155
156 /* Sort and uniq given community. */
157 struct community *community_uniq_sort(struct community *com)
158 {
159 int i;
160 struct community *new;
161 uint32_t val;
162
163 if (!com)
164 return NULL;
165
166 new = community_new();
167 new->json = NULL;
168
169 for (i = 0; i < com->size; i++) {
170 val = community_val_get(com, i);
171
172 if (!community_include(new, val))
173 community_add_val(new, val);
174 }
175
176 qsort(new->val, new->size, sizeof(uint32_t), community_compare);
177
178 return new;
179 }
180
181 /* Convert communities attribute to string.
182
183 For Well-known communities value, below keyword is used.
184
185 0x0 "internet"
186 0xFFFF0000 "graceful-shutdown"
187 0xFFFF0001 "accept-own"
188 0xFFFF0002 "route-filter-translated-v4"
189 0xFFFF0003 "route-filter-v4"
190 0xFFFF0004 "route-filter-translated-v6"
191 0xFFFF0005 "route-filter-v6"
192 0xFFFF0006 "llgr-stale"
193 0xFFFF0007 "no-llgr"
194 0xFFFF0008 "accept-own-nexthop"
195 0xFFFF029A "blackhole"
196 0xFFFFFF01 "no-export"
197 0xFFFFFF02 "no-advertise"
198 0xFFFFFF03 "local-AS"
199 0xFFFFFF04 "no-peer"
200
201 For other values, "AS:VAL" format is used. */
202 static void set_community_string(struct community *com, bool make_json)
203 {
204 int i;
205 char *str;
206 int len;
207 int first;
208 uint32_t comval;
209 uint16_t as;
210 uint16_t val;
211 json_object *json_community_list = NULL;
212 json_object *json_string = NULL;
213
214 if (!com)
215 return;
216
217 if (make_json) {
218 com->json = json_object_new_object();
219 json_community_list = json_object_new_array();
220 }
221
222 /* When communities attribute is empty. */
223 if (com->size == 0) {
224 str = XMALLOC(MTYPE_COMMUNITY_STR, 1);
225 str[0] = '\0';
226
227 if (make_json) {
228 json_object_string_add(com->json, "string", "");
229 json_object_object_add(com->json, "list",
230 json_community_list);
231 }
232 com->str = str;
233 return;
234 }
235
236 /* Memory allocation is time consuming work. So we calculate
237 required string length first. */
238 len = 0;
239
240 for (i = 0; i < com->size; i++) {
241 memcpy(&comval, com_nthval(com, i), sizeof(uint32_t));
242 comval = ntohl(comval);
243
244 switch (comval) {
245 case COMMUNITY_INTERNET:
246 len += strlen(" internet");
247 break;
248 case COMMUNITY_GSHUT:
249 len += strlen(" graceful-shutdown");
250 break;
251 case COMMUNITY_ACCEPT_OWN:
252 len += strlen(" accept-own");
253 break;
254 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4:
255 len += strlen(" route-filter-translated-v4");
256 break;
257 case COMMUNITY_ROUTE_FILTER_v4:
258 len += strlen(" route-filter-v4");
259 break;
260 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6:
261 len += strlen(" route-filter-translated-v6");
262 break;
263 case COMMUNITY_ROUTE_FILTER_v6:
264 len += strlen(" route-filter-v6");
265 break;
266 case COMMUNITY_LLGR_STALE:
267 len += strlen(" llgr-stale");
268 break;
269 case COMMUNITY_NO_LLGR:
270 len += strlen(" no-llgr");
271 break;
272 case COMMUNITY_ACCEPT_OWN_NEXTHOP:
273 len += strlen(" accept-own-nexthop");
274 break;
275 case COMMUNITY_BLACKHOLE:
276 len += strlen(" blackhole");
277 break;
278 case COMMUNITY_NO_EXPORT:
279 len += strlen(" no-export");
280 break;
281 case COMMUNITY_NO_ADVERTISE:
282 len += strlen(" no-advertise");
283 break;
284 case COMMUNITY_LOCAL_AS:
285 len += strlen(" local-AS");
286 break;
287 case COMMUNITY_NO_PEER:
288 len += strlen(" no-peer");
289 break;
290 default:
291 len += strlen(" 65536:65535");
292 break;
293 }
294 }
295
296 /* Allocate memory. */
297 str = XCALLOC(MTYPE_COMMUNITY_STR, len);
298 first = 1;
299
300 /* Fill in string. */
301 for (i = 0; i < com->size; i++) {
302 memcpy(&comval, com_nthval(com, i), sizeof(uint32_t));
303 comval = ntohl(comval);
304
305 if (first)
306 first = 0;
307 else
308 strlcat(str, " ", len);
309
310 switch (comval) {
311 case COMMUNITY_INTERNET:
312 strlcat(str, "internet", len);
313 if (make_json) {
314 json_string =
315 json_object_new_string("internet");
316 json_object_array_add(json_community_list,
317 json_string);
318 }
319 break;
320 case COMMUNITY_GSHUT:
321 strlcat(str, "graceful-shutdown", len);
322 if (make_json) {
323 json_string = json_object_new_string(
324 "gracefulShutdown");
325 json_object_array_add(json_community_list,
326 json_string);
327 }
328 break;
329 case COMMUNITY_ACCEPT_OWN:
330 strlcat(str, "accept-own", len);
331 if (make_json) {
332 json_string = json_object_new_string(
333 "acceptown");
334 json_object_array_add(json_community_list,
335 json_string);
336 }
337 break;
338 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4:
339 strlcat(str, "route-filter-translated-v4", len);
340 if (make_json) {
341 json_string = json_object_new_string(
342 "routeFilterTranslatedV4");
343 json_object_array_add(json_community_list,
344 json_string);
345 }
346 break;
347 case COMMUNITY_ROUTE_FILTER_v4:
348 strlcat(str, "route-filter-v4", len);
349 if (make_json) {
350 json_string = json_object_new_string(
351 "routeFilterV4");
352 json_object_array_add(json_community_list,
353 json_string);
354 }
355 break;
356 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6:
357 strlcat(str, "route-filter-translated-v6", len);
358 if (make_json) {
359 json_string = json_object_new_string(
360 "routeFilterTranslatedV6");
361 json_object_array_add(json_community_list,
362 json_string);
363 }
364 break;
365 case COMMUNITY_ROUTE_FILTER_v6:
366 strlcat(str, "route-filter-v6", len);
367 if (make_json) {
368 json_string = json_object_new_string(
369 "routeFilterV6");
370 json_object_array_add(json_community_list,
371 json_string);
372 }
373 break;
374 case COMMUNITY_LLGR_STALE:
375 strlcat(str, "llgr-stale", len);
376 if (make_json) {
377 json_string = json_object_new_string(
378 "llgrStale");
379 json_object_array_add(json_community_list,
380 json_string);
381 }
382 break;
383 case COMMUNITY_NO_LLGR:
384 strlcat(str, "no-llgr", len);
385 if (make_json) {
386 json_string = json_object_new_string(
387 "noLlgr");
388 json_object_array_add(json_community_list,
389 json_string);
390 }
391 break;
392 case COMMUNITY_ACCEPT_OWN_NEXTHOP:
393 strlcat(str, "accept-own-nexthop", len);
394 if (make_json) {
395 json_string = json_object_new_string(
396 "acceptownnexthop");
397 json_object_array_add(json_community_list,
398 json_string);
399 }
400 break;
401 case COMMUNITY_BLACKHOLE:
402 strlcat(str, "blackhole", len);
403 if (make_json) {
404 json_string = json_object_new_string(
405 "blackhole");
406 json_object_array_add(json_community_list,
407 json_string);
408 }
409 break;
410 case COMMUNITY_NO_EXPORT:
411 strlcat(str, "no-export", len);
412 if (make_json) {
413 json_string =
414 json_object_new_string("noExport");
415 json_object_array_add(json_community_list,
416 json_string);
417 }
418 break;
419 case COMMUNITY_NO_ADVERTISE:
420 strlcat(str, "no-advertise", len);
421 if (make_json) {
422 json_string =
423 json_object_new_string("noAdvertise");
424 json_object_array_add(json_community_list,
425 json_string);
426 }
427 break;
428 case COMMUNITY_LOCAL_AS:
429 strlcat(str, "local-AS", len);
430 if (make_json) {
431 json_string = json_object_new_string("localAs");
432 json_object_array_add(json_community_list,
433 json_string);
434 }
435 break;
436 case COMMUNITY_NO_PEER:
437 strlcat(str, "no-peer", len);
438 if (make_json) {
439 json_string = json_object_new_string("noPeer");
440 json_object_array_add(json_community_list,
441 json_string);
442 }
443 break;
444 default:
445 as = (comval >> 16) & 0xFFFF;
446 val = comval & 0xFFFF;
447 char buf[32];
448 snprintf(buf, sizeof(buf), "%u:%d", as, val);
449 strlcat(str, buf, len);
450 if (make_json) {
451 json_string = json_object_new_string(buf);
452 json_object_array_add(json_community_list,
453 json_string);
454 }
455 break;
456 }
457 }
458
459 if (make_json) {
460 json_object_string_add(com->json, "string", str);
461 json_object_object_add(com->json, "list", json_community_list);
462 }
463 com->str = str;
464 }
465
466 /* Intern communities attribute. */
467 struct community *community_intern(struct community *com)
468 {
469 struct community *find;
470
471 /* Assert this community structure is not interned. */
472 assert(com->refcnt == 0);
473
474 /* Lookup community hash. */
475 find = (struct community *)hash_get(comhash, com, hash_alloc_intern);
476
477 /* Arguemnt com is allocated temporary. So when it is not used in
478 hash, it should be freed. */
479 if (find != com)
480 community_free(&com);
481
482 /* Increment refrence counter. */
483 find->refcnt++;
484
485 /* Make string. */
486 if (!find->str)
487 set_community_string(find, false);
488
489 return find;
490 }
491
492 /* Free community attribute. */
493 void community_unintern(struct community **com)
494 {
495 struct community *ret;
496
497 if ((*com)->refcnt)
498 (*com)->refcnt--;
499
500 /* Pull off from hash. */
501 if ((*com)->refcnt == 0) {
502 /* Community value com must exist in hash. */
503 ret = (struct community *)hash_release(comhash, *com);
504 assert(ret != NULL);
505
506 community_free(com);
507 }
508 }
509
510 /* Create new community attribute. */
511 struct community *community_parse(uint32_t *pnt, unsigned short length)
512 {
513 struct community tmp;
514 struct community *new;
515
516 /* If length is malformed return NULL. */
517 if (length % 4)
518 return NULL;
519
520 /* Make temporary community for hash look up. */
521 tmp.size = length / 4;
522 tmp.val = pnt;
523
524 new = community_uniq_sort(&tmp);
525
526 return community_intern(new);
527 }
528
529 struct community *community_dup(struct community *com)
530 {
531 struct community *new;
532
533 new = XCALLOC(MTYPE_COMMUNITY, sizeof(struct community));
534 new->size = com->size;
535 if (new->size) {
536 new->val = XMALLOC(MTYPE_COMMUNITY_VAL, com->size * 4);
537 memcpy(new->val, com->val, com->size * 4);
538 } else
539 new->val = NULL;
540 return new;
541 }
542
543 /* Retrun string representation of communities attribute. */
544 char *community_str(struct community *com, bool make_json)
545 {
546 if (!com)
547 return NULL;
548
549 if (make_json && !com->json && com->str)
550 XFREE(MTYPE_COMMUNITY_STR, com->str);
551
552 if (!com->str)
553 set_community_string(com, make_json);
554 return com->str;
555 }
556
557 /* Make hash value of community attribute. This function is used by
558 hash package.*/
559 unsigned int community_hash_make(const struct community *com)
560 {
561 uint32_t *pnt = (uint32_t *)com->val;
562
563 return jhash2(pnt, com->size, 0x43ea96c1);
564 }
565
566 bool community_match(const struct community *com1, const struct community *com2)
567 {
568 int i = 0;
569 int j = 0;
570
571 if (com1 == NULL && com2 == NULL)
572 return true;
573
574 if (com1 == NULL || com2 == NULL)
575 return false;
576
577 if (com1->size < com2->size)
578 return false;
579
580 /* Every community on com2 needs to be on com1 for this to match */
581 while (i < com1->size && j < com2->size) {
582 if (memcmp(com1->val + i, com2->val + j, sizeof(uint32_t)) == 0)
583 j++;
584 i++;
585 }
586
587 if (j == com2->size)
588 return true;
589 else
590 return false;
591 }
592
593 /* If two aspath have same value then return 1 else return 0. This
594 function is used by hash package. */
595 bool community_cmp(const struct community *com1, const struct community *com2)
596 {
597 if (com1 == NULL && com2 == NULL)
598 return true;
599 if (com1 == NULL || com2 == NULL)
600 return false;
601
602 if (com1->size == com2->size)
603 if (memcmp(com1->val, com2->val, com1->size * 4) == 0)
604 return true;
605 return false;
606 }
607
608 /* Add com2 to the end of com1. */
609 struct community *community_merge(struct community *com1,
610 struct community *com2)
611 {
612 if (com1->val)
613 com1->val = XREALLOC(MTYPE_COMMUNITY_VAL, com1->val,
614 (com1->size + com2->size) * 4);
615 else
616 com1->val = XMALLOC(MTYPE_COMMUNITY_VAL,
617 (com1->size + com2->size) * 4);
618
619 memcpy(com1->val + com1->size, com2->val, com2->size * 4);
620 com1->size += com2->size;
621
622 return com1;
623 }
624
625 /* Community token enum. */
626 enum community_token {
627 community_token_val,
628 community_token_gshut,
629 community_token_accept_own,
630 community_token_route_filter_translated_v4,
631 community_token_route_filter_v4,
632 community_token_route_filter_translated_v6,
633 community_token_route_filter_v6,
634 community_token_llgr_stale,
635 community_token_no_llgr,
636 community_token_accept_own_nexthop,
637 community_token_blackhole,
638 community_token_no_export,
639 community_token_no_advertise,
640 community_token_local_as,
641 community_token_no_peer,
642 community_token_unknown
643 };
644
645 /* Get next community token from string. */
646 static const char *
647 community_gettoken(const char *buf, enum community_token *token, uint32_t *val)
648 {
649 const char *p = buf;
650
651 /* Skip white space. */
652 while (isspace((unsigned char)*p))
653 p++;
654
655 /* Check the end of the line. */
656 if (*p == '\0')
657 return NULL;
658
659 /* Well known community string check. */
660 if (isalpha((unsigned char)*p)) {
661 if (strncmp(p, "internet", strlen("internet")) == 0) {
662 *val = COMMUNITY_INTERNET;
663 *token = community_token_no_export;
664 p += strlen("internet");
665 return p;
666 }
667 if (strncmp(p, "graceful-shutdown", strlen("graceful-shutdown"))
668 == 0) {
669 *val = COMMUNITY_GSHUT;
670 *token = community_token_gshut;
671 p += strlen("graceful-shutdown");
672 return p;
673 }
674 if (strncmp(p, "accept-own", strlen("accept-own"))
675 == 0) {
676 *val = COMMUNITY_ACCEPT_OWN;
677 *token = community_token_accept_own;
678 p += strlen("accept-own");
679 return p;
680 }
681 if (strncmp(p, "route-filter-translated-v4",
682 strlen("route-filter-translated-v4"))
683 == 0) {
684 *val = COMMUNITY_ROUTE_FILTER_TRANSLATED_v4;
685 *token = community_token_route_filter_translated_v4;
686 p += strlen("route-filter-translated-v4");
687 return p;
688 }
689 if (strncmp(p, "route-filter-v4", strlen("route-filter-v4"))
690 == 0) {
691 *val = COMMUNITY_ROUTE_FILTER_v4;
692 *token = community_token_route_filter_v4;
693 p += strlen("route-filter-v4");
694 return p;
695 }
696 if (strncmp(p, "route-filter-translated-v6",
697 strlen("route-filter-translated-v6"))
698 == 0) {
699 *val = COMMUNITY_ROUTE_FILTER_TRANSLATED_v6;
700 *token = community_token_route_filter_translated_v6;
701 p += strlen("route-filter-translated-v6");
702 return p;
703 }
704 if (strncmp(p, "route-filter-v6", strlen("route-filter-v6"))
705 == 0) {
706 *val = COMMUNITY_ROUTE_FILTER_v6;
707 *token = community_token_route_filter_v6;
708 p += strlen("route-filter-v6");
709 return p;
710 }
711 if (strncmp(p, "llgr-stale", strlen("llgr-stale"))
712 == 0) {
713 *val = COMMUNITY_LLGR_STALE;
714 *token = community_token_llgr_stale;
715 p += strlen("llgr-stale");
716 return p;
717 }
718 if (strncmp(p, "no-llgr", strlen("no-llgr"))
719 == 0) {
720 *val = COMMUNITY_NO_LLGR;
721 *token = community_token_no_llgr;
722 p += strlen("no-llgr");
723 return p;
724 }
725 if (strncmp(p, "accept-own-nexthop",
726 strlen("accept-own-nexthop"))
727 == 0) {
728 *val = COMMUNITY_ACCEPT_OWN_NEXTHOP;
729 *token = community_token_accept_own_nexthop;
730 p += strlen("accept-own-nexthop");
731 return p;
732 }
733 if (strncmp(p, "blackhole", strlen("blackhole"))
734 == 0) {
735 *val = COMMUNITY_BLACKHOLE;
736 *token = community_token_blackhole;
737 p += strlen("blackhole");
738 return p;
739 }
740 if (strncmp(p, "no-export", strlen("no-export")) == 0) {
741 *val = COMMUNITY_NO_EXPORT;
742 *token = community_token_no_export;
743 p += strlen("no-export");
744 return p;
745 }
746 if (strncmp(p, "no-advertise", strlen("no-advertise")) == 0) {
747 *val = COMMUNITY_NO_ADVERTISE;
748 *token = community_token_no_advertise;
749 p += strlen("no-advertise");
750 return p;
751 }
752 if (strncmp(p, "local-AS", strlen("local-AS")) == 0) {
753 *val = COMMUNITY_LOCAL_AS;
754 *token = community_token_local_as;
755 p += strlen("local-AS");
756 return p;
757 }
758 if (strncmp(p, "no-peer", strlen("no-peer")) == 0) {
759 *val = COMMUNITY_NO_PEER;
760 *token = community_token_no_peer;
761 p += strlen("no-peer");
762 return p;
763 }
764
765 /* Unknown string. */
766 *token = community_token_unknown;
767 return NULL;
768 }
769
770 /* Community value. */
771 if (isdigit((unsigned char)*p)) {
772 int separator = 0;
773 int digit = 0;
774 uint32_t community_low = 0;
775 uint32_t community_high = 0;
776
777 while (isdigit((unsigned char)*p) || *p == ':') {
778 if (*p == ':') {
779 if (separator) {
780 *token = community_token_unknown;
781 return NULL;
782 } else {
783 separator = 1;
784 digit = 0;
785
786 if (community_low > UINT16_MAX) {
787 *token =
788 community_token_unknown;
789 return NULL;
790 }
791
792 community_high = community_low << 16;
793 community_low = 0;
794 }
795 } else {
796 digit = 1;
797 community_low *= 10;
798 community_low += (*p - '0');
799 }
800 p++;
801 }
802 if (!digit) {
803 *token = community_token_unknown;
804 return NULL;
805 }
806
807 if (community_low > UINT16_MAX) {
808 *token = community_token_unknown;
809 return NULL;
810 }
811
812 *val = community_high + community_low;
813 *token = community_token_val;
814 return p;
815 }
816 *token = community_token_unknown;
817 return NULL;
818 }
819
820 /* convert string to community structure */
821 struct community *community_str2com(const char *str)
822 {
823 struct community *com = NULL;
824 struct community *com_sort = NULL;
825 uint32_t val = 0;
826 enum community_token token = community_token_unknown;
827
828 do {
829 str = community_gettoken(str, &token, &val);
830
831 switch (token) {
832 case community_token_val:
833 case community_token_gshut:
834 case community_token_accept_own:
835 case community_token_route_filter_translated_v4:
836 case community_token_route_filter_v4:
837 case community_token_route_filter_translated_v6:
838 case community_token_route_filter_v6:
839 case community_token_llgr_stale:
840 case community_token_no_llgr:
841 case community_token_accept_own_nexthop:
842 case community_token_blackhole:
843 case community_token_no_export:
844 case community_token_no_advertise:
845 case community_token_local_as:
846 case community_token_no_peer:
847 if (com == NULL) {
848 com = community_new();
849 com->json = NULL;
850 }
851 community_add_val(com, val);
852 break;
853 case community_token_unknown:
854 if (com)
855 community_free(&com);
856 return NULL;
857 }
858 } while (str);
859
860 com_sort = community_uniq_sort(com);
861 community_free(&com);
862
863 return com_sort;
864 }
865
866 /* Return communities hash entry count. */
867 unsigned long community_count(void)
868 {
869 return comhash->count;
870 }
871
872 /* Return communities hash. */
873 struct hash *community_hash(void)
874 {
875 return comhash;
876 }
877
878 /* Initialize comminity related hash. */
879 void community_init(void)
880 {
881 comhash =
882 hash_create((unsigned int (*)(const void *))community_hash_make,
883 (bool (*)(const void *, const void *))community_cmp,
884 "BGP Community Hash");
885 }
886
887 void community_finish(void)
888 {
889 hash_free(comhash);
890 comhash = NULL;
891 }
892
893 static struct community *bgp_aggr_community_lookup(
894 struct bgp_aggregate *aggregate,
895 struct community *community)
896 {
897 return hash_lookup(aggregate->community_hash, community);
898 }
899
900 static void *bgp_aggr_communty_hash_alloc(void *p)
901 {
902 struct community *ref = (struct community *)p;
903 struct community *community = NULL;
904
905 community = community_dup(ref);
906 return community;
907 }
908
909 static void bgp_aggr_community_prepare(struct hash_bucket *hb, void *arg)
910 {
911 struct community *hb_community = hb->data;
912 struct community **aggr_community = arg;
913
914 if (*aggr_community)
915 *aggr_community = community_merge(*aggr_community,
916 hb_community);
917 else
918 *aggr_community = community_dup(hb_community);
919 }
920
921 void bgp_aggr_community_remove(void *arg)
922 {
923 struct community *community = arg;
924
925 community_free(&community);
926 }
927
928 void bgp_compute_aggregate_community(struct bgp_aggregate *aggregate,
929 struct community *community)
930 {
931 bgp_compute_aggregate_community_hash(aggregate, community);
932 bgp_compute_aggregate_community_val(aggregate);
933 }
934
935
936 void bgp_compute_aggregate_community_hash(struct bgp_aggregate *aggregate,
937 struct community *community)
938 {
939 struct community *aggr_community = NULL;
940
941 if ((aggregate == NULL) || (community == NULL))
942 return;
943
944 /* Create hash if not already created.
945 */
946 if (aggregate->community_hash == NULL)
947 aggregate->community_hash = hash_create(
948 (unsigned int (*)(const void *))community_hash_make,
949 (bool (*)(const void *, const void *))community_cmp,
950 "BGP Aggregator community hash");
951
952 aggr_community = bgp_aggr_community_lookup(aggregate, community);
953 if (aggr_community == NULL) {
954 /* Insert community into hash.
955 */
956 aggr_community = hash_get(aggregate->community_hash, community,
957 bgp_aggr_communty_hash_alloc);
958 }
959
960 /* Increment reference counter.
961 */
962 aggr_community->refcnt++;
963 }
964
965 void bgp_compute_aggregate_community_val(struct bgp_aggregate *aggregate)
966 {
967 struct community *commerge = NULL;
968
969 if (aggregate == NULL)
970 return;
971
972 /* Re-compute aggregate's community.
973 */
974 if (aggregate->community)
975 community_free(&aggregate->community);
976 if (aggregate->community_hash &&
977 aggregate->community_hash->count) {
978 hash_iterate(aggregate->community_hash,
979 bgp_aggr_community_prepare,
980 &aggregate->community);
981 commerge = aggregate->community;
982 aggregate->community = community_uniq_sort(commerge);
983 if (commerge)
984 community_free(&commerge);
985 }
986 }
987
988
989
990 void bgp_remove_community_from_aggregate(struct bgp_aggregate *aggregate,
991 struct community *community)
992 {
993 struct community *aggr_community = NULL;
994 struct community *ret_comm = NULL;
995
996 if ((!aggregate)
997 || (!aggregate->community_hash)
998 || (!community))
999 return;
1000
1001 /* Look-up the community in the hash.
1002 */
1003 aggr_community = bgp_aggr_community_lookup(aggregate, community);
1004 if (aggr_community) {
1005 aggr_community->refcnt--;
1006
1007 if (aggr_community->refcnt == 0) {
1008 ret_comm = hash_release(aggregate->community_hash,
1009 aggr_community);
1010 community_free(&ret_comm);
1011
1012 bgp_compute_aggregate_community_val(aggregate);
1013 }
1014 }
1015 }
1016
1017 void bgp_remove_comm_from_aggregate_hash(struct bgp_aggregate *aggregate,
1018 struct community *community)
1019 {
1020
1021 struct community *aggr_community = NULL;
1022 struct community *ret_comm = NULL;
1023
1024 if ((!aggregate)
1025 || (!aggregate->community_hash)
1026 || (!community))
1027 return;
1028
1029 /* Look-up the community in the hash.
1030 */
1031 aggr_community = bgp_aggr_community_lookup(aggregate, community);
1032 if (aggr_community) {
1033 aggr_community->refcnt--;
1034
1035 if (aggr_community->refcnt == 0) {
1036 ret_comm = hash_release(aggregate->community_hash,
1037 aggr_community);
1038 community_free(&ret_comm);
1039 }
1040 }
1041 }