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