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