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