1 /* Community attribute related functions.
2 * Copyright (C) 1998, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
28 #include "bgpd/bgp_memory.h"
29 #include "bgpd/bgp_community.h"
31 /* Hash of community attribute. */
32 static struct hash
*comhash
;
34 /* Allocate a new communities value. */
35 static struct community
*community_new(void)
37 return XCALLOC(MTYPE_COMMUNITY
, sizeof(struct community
));
40 /* Free communities value. */
41 void community_free(struct community
**com
)
43 XFREE(MTYPE_COMMUNITY_VAL
, (*com
)->val
);
44 XFREE(MTYPE_COMMUNITY_STR
, (*com
)->str
);
47 json_object_free((*com
)->json
);
51 XFREE(MTYPE_COMMUNITY
, (*com
));
54 /* Add one community value to the community. */
55 static void community_add_val(struct community
*com
, uint32_t val
)
59 com
->val
= XREALLOC(MTYPE_COMMUNITY_VAL
, com
->val
,
62 com
->val
= XMALLOC(MTYPE_COMMUNITY_VAL
, com_length(com
));
65 memcpy(com_lastval(com
), &val
, sizeof(uint32_t));
68 /* Delete one community. */
69 void community_del_val(struct community
*com
, uint32_t *val
)
77 while (i
< com
->size
) {
78 if (memcmp(com
->val
+ i
, val
, sizeof(uint32_t)) == 0) {
79 c
= com
->size
- i
- 1;
82 memmove(com
->val
+ i
, com
->val
+ (i
+ 1),
88 com
->val
= XREALLOC(MTYPE_COMMUNITY_VAL
,
89 com
->val
, com_length(com
));
91 XFREE(MTYPE_COMMUNITY_VAL
, com
->val
);
100 /* Delete all communities listed in com2 from com1 */
101 struct community
*community_delete(struct community
*com1
,
102 struct community
*com2
)
106 while (i
< com2
->size
) {
107 community_del_val(com1
, com2
->val
+ i
);
114 /* Callback function from qsort(). */
115 static int community_compare(const void *a1
, const void *a2
)
120 memcpy(&v1
, a1
, sizeof(uint32_t));
121 memcpy(&v2
, a2
, sizeof(uint32_t));
132 int community_include(struct community
*com
, uint32_t val
)
138 for (i
= 0; i
< com
->size
; i
++)
139 if (memcmp(&val
, com_nthval(com
, i
), sizeof(uint32_t)) == 0)
145 uint32_t community_val_get(struct community
*com
, int i
)
150 p
= (uint8_t *)com
->val
;
153 memcpy(&val
, p
, sizeof(uint32_t));
158 /* Sort and uniq given community. */
159 struct community
*community_uniq_sort(struct community
*com
)
162 struct community
*new;
168 new = community_new();
171 for (i
= 0; i
< com
->size
; i
++) {
172 val
= community_val_get(com
, i
);
174 if (!community_include(new, val
))
175 community_add_val(new, val
);
178 qsort(new->val
, new->size
, sizeof(uint32_t), community_compare
);
183 /* Convert communities attribute to string.
185 For Well-known communities value, below keyword is used.
188 0xFFFF0000 "graceful-shutdown"
189 0xFFFF0001 "accept-own"
190 0xFFFF0002 "route-filter-translated-v4"
191 0xFFFF0003 "route-filter-v4"
192 0xFFFF0004 "route-filter-translated-v6"
193 0xFFFF0005 "route-filter-v6"
194 0xFFFF0006 "llgr-stale"
196 0xFFFF0008 "accept-own-nexthop"
197 0xFFFF029A "blackhole"
198 0xFFFFFF01 "no-export"
199 0xFFFFFF02 "no-advertise"
200 0xFFFFFF03 "local-AS"
203 For other values, "AS:VAL" format is used. */
204 static void set_community_string(struct community
*com
, bool make_json
)
214 json_object
*json_community_list
= NULL
;
215 json_object
*json_string
= NULL
;
221 com
->json
= json_object_new_object();
222 json_community_list
= json_object_new_array();
225 /* When communities attribute is empty. */
226 if (com
->size
== 0) {
227 str
= XMALLOC(MTYPE_COMMUNITY_STR
, 1);
231 json_object_string_add(com
->json
, "string", "");
232 json_object_object_add(com
->json
, "list",
233 json_community_list
);
239 /* Memory allocation is time consuming work. So we calculate
240 required string length first. */
243 for (i
= 0; i
< com
->size
; i
++) {
244 memcpy(&comval
, com_nthval(com
, i
), sizeof(uint32_t));
245 comval
= ntohl(comval
);
248 case COMMUNITY_INTERNET
:
249 len
+= strlen(" internet");
251 case COMMUNITY_GSHUT
:
252 len
+= strlen(" graceful-shutdown");
254 case COMMUNITY_ACCEPT_OWN
:
255 len
+= strlen(" accept-own");
257 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
:
258 len
+= strlen(" route-filter-translated-v4");
260 case COMMUNITY_ROUTE_FILTER_v4
:
261 len
+= strlen(" route-filter-v4");
263 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
:
264 len
+= strlen(" route-filter-translated-v6");
266 case COMMUNITY_ROUTE_FILTER_v6
:
267 len
+= strlen(" route-filter-v6");
269 case COMMUNITY_LLGR_STALE
:
270 len
+= strlen(" llgr-stale");
272 case COMMUNITY_NO_LLGR
:
273 len
+= strlen(" no-llgr");
275 case COMMUNITY_ACCEPT_OWN_NEXTHOP
:
276 len
+= strlen(" accept-own-nexthop");
278 case COMMUNITY_BLACKHOLE
:
279 len
+= strlen(" blackhole");
281 case COMMUNITY_NO_EXPORT
:
282 len
+= strlen(" no-export");
284 case COMMUNITY_NO_ADVERTISE
:
285 len
+= strlen(" no-advertise");
287 case COMMUNITY_LOCAL_AS
:
288 len
+= strlen(" local-AS");
290 case COMMUNITY_NO_PEER
:
291 len
+= strlen(" no-peer");
294 len
+= strlen(" 65536:65535");
299 /* Allocate memory. */
300 str
= pnt
= XMALLOC(MTYPE_COMMUNITY_STR
, len
);
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
);
314 case COMMUNITY_INTERNET
:
315 strcpy(pnt
, "internet");
316 pnt
+= strlen("internet");
319 json_object_new_string("internet");
320 json_object_array_add(json_community_list
,
324 case COMMUNITY_GSHUT
:
325 strcpy(pnt
, "graceful-shutdown");
326 pnt
+= strlen("graceful-shutdown");
328 json_string
= json_object_new_string(
330 json_object_array_add(json_community_list
,
334 case COMMUNITY_ACCEPT_OWN
:
335 strcpy(pnt
, "accept-own");
336 pnt
+= strlen("accept-own");
338 json_string
= json_object_new_string(
340 json_object_array_add(json_community_list
,
344 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
:
345 strcpy(pnt
, "route-filter-translated-v4");
346 pnt
+= strlen("route-filter-translated-v4");
348 json_string
= json_object_new_string(
349 "routeFilterTranslatedV4");
350 json_object_array_add(json_community_list
,
354 case COMMUNITY_ROUTE_FILTER_v4
:
355 strcpy(pnt
, "route-filter-v4");
356 pnt
+= strlen("route-filter-v4");
358 json_string
= json_object_new_string(
360 json_object_array_add(json_community_list
,
364 case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
:
365 strcpy(pnt
, "route-filter-translated-v6");
366 pnt
+= strlen("route-filter-translated-v6");
368 json_string
= json_object_new_string(
369 "routeFilterTranslatedV6");
370 json_object_array_add(json_community_list
,
374 case COMMUNITY_ROUTE_FILTER_v6
:
375 strcpy(pnt
, "route-filter-v6");
376 pnt
+= strlen("route-filter-v6");
378 json_string
= json_object_new_string(
380 json_object_array_add(json_community_list
,
384 case COMMUNITY_LLGR_STALE
:
385 strcpy(pnt
, "llgr-stale");
386 pnt
+= strlen("llgr-stale");
388 json_string
= json_object_new_string(
390 json_object_array_add(json_community_list
,
394 case COMMUNITY_NO_LLGR
:
395 strcpy(pnt
, "no-llgr");
396 pnt
+= strlen("no-llgr");
398 json_string
= json_object_new_string(
400 json_object_array_add(json_community_list
,
404 case COMMUNITY_ACCEPT_OWN_NEXTHOP
:
405 strcpy(pnt
, "accept-own-nexthop");
406 pnt
+= strlen("accept-own-nexthop");
408 json_string
= json_object_new_string(
410 json_object_array_add(json_community_list
,
414 case COMMUNITY_BLACKHOLE
:
415 strcpy(pnt
, "blackhole");
416 pnt
+= strlen("blackhole");
418 json_string
= json_object_new_string(
420 json_object_array_add(json_community_list
,
424 case COMMUNITY_NO_EXPORT
:
425 strcpy(pnt
, "no-export");
426 pnt
+= strlen("no-export");
429 json_object_new_string("noExport");
430 json_object_array_add(json_community_list
,
434 case COMMUNITY_NO_ADVERTISE
:
435 strcpy(pnt
, "no-advertise");
436 pnt
+= strlen("no-advertise");
439 json_object_new_string("noAdvertise");
440 json_object_array_add(json_community_list
,
444 case COMMUNITY_LOCAL_AS
:
445 strcpy(pnt
, "local-AS");
446 pnt
+= strlen("local-AS");
448 json_string
= json_object_new_string("localAs");
449 json_object_array_add(json_community_list
,
453 case COMMUNITY_NO_PEER
:
454 strcpy(pnt
, "no-peer");
455 pnt
+= strlen("no-peer");
457 json_string
= json_object_new_string("noPeer");
458 json_object_array_add(json_community_list
,
463 as
= (comval
>> 16) & 0xFFFF;
464 val
= comval
& 0xFFFF;
465 sprintf(pnt
, "%u:%d", as
, val
);
467 json_string
= json_object_new_string(pnt
);
468 json_object_array_add(json_community_list
,
478 json_object_string_add(com
->json
, "string", str
);
479 json_object_object_add(com
->json
, "list", json_community_list
);
484 /* Intern communities attribute. */
485 struct community
*community_intern(struct community
*com
)
487 struct community
*find
;
489 /* Assert this community structure is not interned. */
490 assert(com
->refcnt
== 0);
492 /* Lookup community hash. */
493 find
= (struct community
*)hash_get(comhash
, com
, hash_alloc_intern
);
495 /* Arguemnt com is allocated temporary. So when it is not used in
496 hash, it should be freed. */
498 community_free(&com
);
500 /* Increment refrence counter. */
505 set_community_string(find
, false);
510 /* Free community attribute. */
511 void community_unintern(struct community
**com
)
513 struct community
*ret
;
518 /* Pull off from hash. */
519 if ((*com
)->refcnt
== 0) {
520 /* Community value com must exist in hash. */
521 ret
= (struct community
*)hash_release(comhash
, *com
);
528 /* Create new community attribute. */
529 struct community
*community_parse(uint32_t *pnt
, unsigned short length
)
531 struct community tmp
;
532 struct community
*new;
534 /* If length is malformed return NULL. */
538 /* Make temporary community for hash look up. */
539 tmp
.size
= length
/ 4;
542 new = community_uniq_sort(&tmp
);
544 return community_intern(new);
547 struct community
*community_dup(struct community
*com
)
549 struct community
*new;
551 new = XCALLOC(MTYPE_COMMUNITY
, sizeof(struct community
));
552 new->size
= com
->size
;
554 new->val
= XMALLOC(MTYPE_COMMUNITY_VAL
, com
->size
* 4);
555 memcpy(new->val
, com
->val
, com
->size
* 4);
561 /* Retrun string representation of communities attribute. */
562 char *community_str(struct community
*com
, bool make_json
)
567 if (make_json
&& !com
->json
&& com
->str
)
568 XFREE(MTYPE_COMMUNITY_STR
, com
->str
);
571 set_community_string(com
, make_json
);
575 /* Make hash value of community attribute. This function is used by
577 unsigned int community_hash_make(struct community
*com
)
579 uint32_t *pnt
= (uint32_t *)com
->val
;
581 return jhash2(pnt
, com
->size
, 0x43ea96c1);
584 int community_match(const struct community
*com1
, const struct community
*com2
)
589 if (com1
== NULL
&& com2
== NULL
)
592 if (com1
== NULL
|| com2
== NULL
)
595 if (com1
->size
< com2
->size
)
598 /* Every community on com2 needs to be on com1 for this to match */
599 while (i
< com1
->size
&& j
< com2
->size
) {
600 if (memcmp(com1
->val
+ i
, com2
->val
+ j
, sizeof(uint32_t)) == 0)
611 /* If two aspath have same value then return 1 else return 0. This
612 function is used by hash package. */
613 bool community_cmp(const struct community
*com1
, const struct community
*com2
)
615 if (com1
== NULL
&& com2
== NULL
)
617 if (com1
== NULL
|| com2
== NULL
)
620 if (com1
->size
== com2
->size
)
621 if (memcmp(com1
->val
, com2
->val
, com1
->size
* 4) == 0)
626 /* Add com2 to the end of com1. */
627 struct community
*community_merge(struct community
*com1
,
628 struct community
*com2
)
631 com1
->val
= XREALLOC(MTYPE_COMMUNITY_VAL
, com1
->val
,
632 (com1
->size
+ com2
->size
) * 4);
634 com1
->val
= XMALLOC(MTYPE_COMMUNITY_VAL
,
635 (com1
->size
+ com2
->size
) * 4);
637 memcpy(com1
->val
+ com1
->size
, com2
->val
, com2
->size
* 4);
638 com1
->size
+= com2
->size
;
643 /* Community token enum. */
644 enum community_token
{
646 community_token_gshut
,
647 community_token_accept_own
,
648 community_token_route_filter_translated_v4
,
649 community_token_route_filter_v4
,
650 community_token_route_filter_translated_v6
,
651 community_token_route_filter_v6
,
652 community_token_llgr_stale
,
653 community_token_no_llgr
,
654 community_token_accept_own_nexthop
,
655 community_token_blackhole
,
656 community_token_no_export
,
657 community_token_no_advertise
,
658 community_token_local_as
,
659 community_token_no_peer
,
660 community_token_unknown
663 /* Get next community token from string. */
665 community_gettoken(const char *buf
, enum community_token
*token
, uint32_t *val
)
669 /* Skip white space. */
670 while (isspace((int)*p
))
673 /* Check the end of the line. */
677 /* Well known community string check. */
678 if (isalpha((int)*p
)) {
679 if (strncmp(p
, "internet", strlen("internet")) == 0) {
680 *val
= COMMUNITY_INTERNET
;
681 *token
= community_token_no_export
;
682 p
+= strlen("internet");
685 if (strncmp(p
, "graceful-shutdown", strlen("graceful-shutdown"))
687 *val
= COMMUNITY_GSHUT
;
688 *token
= community_token_gshut
;
689 p
+= strlen("graceful-shutdown");
692 if (strncmp(p
, "accept-own", strlen("accept-own"))
694 *val
= COMMUNITY_ACCEPT_OWN
;
695 *token
= community_token_accept_own
;
696 p
+= strlen("accept-own");
699 if (strncmp(p
, "route-filter-translated-v4",
700 strlen("route-filter-translated-v4"))
702 *val
= COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
;
703 *token
= community_token_route_filter_translated_v4
;
704 p
+= strlen("route-filter-translated-v4");
707 if (strncmp(p
, "route-filter-v4", strlen("route-filter-v4"))
709 *val
= COMMUNITY_ROUTE_FILTER_v4
;
710 *token
= community_token_route_filter_v4
;
711 p
+= strlen("route-filter-v4");
714 if (strncmp(p
, "route-filter-translated-v6",
715 strlen("route-filter-translated-v6"))
717 *val
= COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
;
718 *token
= community_token_route_filter_translated_v6
;
719 p
+= strlen("route-filter-translated-v6");
722 if (strncmp(p
, "route-filter-v6", strlen("route-filter-v6"))
724 *val
= COMMUNITY_ROUTE_FILTER_v6
;
725 *token
= community_token_route_filter_v6
;
726 p
+= strlen("route-filter-v6");
729 if (strncmp(p
, "llgr-stale", strlen("llgr-stale"))
731 *val
= COMMUNITY_LLGR_STALE
;
732 *token
= community_token_llgr_stale
;
733 p
+= strlen("llgr-stale");
736 if (strncmp(p
, "no-llgr", strlen("no-llgr"))
738 *val
= COMMUNITY_NO_LLGR
;
739 *token
= community_token_no_llgr
;
740 p
+= strlen("no-llgr");
743 if (strncmp(p
, "accept-own-nexthop",
744 strlen("accept-own-nexthop"))
746 *val
= COMMUNITY_ACCEPT_OWN_NEXTHOP
;
747 *token
= community_token_accept_own_nexthop
;
748 p
+= strlen("accept-own-nexthop");
751 if (strncmp(p
, "blackhole", strlen("blackhole"))
753 *val
= COMMUNITY_BLACKHOLE
;
754 *token
= community_token_blackhole
;
755 p
+= strlen("blackhole");
758 if (strncmp(p
, "no-export", strlen("no-export")) == 0) {
759 *val
= COMMUNITY_NO_EXPORT
;
760 *token
= community_token_no_export
;
761 p
+= strlen("no-export");
764 if (strncmp(p
, "no-advertise", strlen("no-advertise")) == 0) {
765 *val
= COMMUNITY_NO_ADVERTISE
;
766 *token
= community_token_no_advertise
;
767 p
+= strlen("no-advertise");
770 if (strncmp(p
, "local-AS", strlen("local-AS")) == 0) {
771 *val
= COMMUNITY_LOCAL_AS
;
772 *token
= community_token_local_as
;
773 p
+= strlen("local-AS");
776 if (strncmp(p
, "no-peer", strlen("no-peer")) == 0) {
777 *val
= COMMUNITY_NO_PEER
;
778 *token
= community_token_no_peer
;
779 p
+= strlen("no-peer");
783 /* Unknown string. */
784 *token
= community_token_unknown
;
788 /* Community value. */
789 if (isdigit((int)*p
)) {
792 uint32_t community_low
= 0;
793 uint32_t community_high
= 0;
795 while (isdigit((int)*p
) || *p
== ':') {
798 *token
= community_token_unknown
;
804 if (community_low
> UINT16_MAX
) {
806 community_token_unknown
;
810 community_high
= community_low
<< 16;
816 community_low
+= (*p
- '0');
821 *token
= community_token_unknown
;
825 if (community_low
> UINT16_MAX
) {
826 *token
= community_token_unknown
;
830 *val
= community_high
+ community_low
;
831 *token
= community_token_val
;
834 *token
= community_token_unknown
;
838 /* convert string to community structure */
839 struct community
*community_str2com(const char *str
)
841 struct community
*com
= NULL
;
842 struct community
*com_sort
= NULL
;
844 enum community_token token
= community_token_unknown
;
847 str
= community_gettoken(str
, &token
, &val
);
850 case community_token_val
:
851 case community_token_gshut
:
852 case community_token_accept_own
:
853 case community_token_route_filter_translated_v4
:
854 case community_token_route_filter_v4
:
855 case community_token_route_filter_translated_v6
:
856 case community_token_route_filter_v6
:
857 case community_token_llgr_stale
:
858 case community_token_no_llgr
:
859 case community_token_accept_own_nexthop
:
860 case community_token_blackhole
:
861 case community_token_no_export
:
862 case community_token_no_advertise
:
863 case community_token_local_as
:
864 case community_token_no_peer
:
866 com
= community_new();
869 community_add_val(com
, val
);
871 case community_token_unknown
:
873 community_free(&com
);
878 com_sort
= community_uniq_sort(com
);
879 community_free(&com
);
884 /* Return communities hash entry count. */
885 unsigned long community_count(void)
887 return comhash
->count
;
890 /* Return communities hash. */
891 struct hash
*community_hash(void)
896 /* Initialize comminity related hash. */
897 void community_init(void)
900 hash_create((unsigned int (*)(void *))community_hash_make
,
901 (bool (*)(const void *, const void *))community_cmp
,
902 "BGP Community Hash");
905 void community_finish(void)
911 static struct community
*bgp_aggr_community_lookup(
912 struct bgp_aggregate
*aggregate
,
913 struct community
*community
)
915 return hash_lookup(aggregate
->community_hash
, community
);
918 static void *bgp_aggr_communty_hash_alloc(void *p
)
920 struct community
*ref
= (struct community
*)p
;
921 struct community
*community
= NULL
;
923 community
= community_dup(ref
);
927 static void bgp_aggr_community_prepare(struct hash_backet
*hb
, void *arg
)
929 struct community
*commerge
= NULL
;
930 struct community
*hb_community
= hb
->data
;
931 struct community
**aggr_community
= arg
;
933 if (*aggr_community
) {
934 commerge
= community_merge(*aggr_community
, hb_community
);
935 *aggr_community
= community_uniq_sort(commerge
);
936 community_free(&commerge
);
938 *aggr_community
= community_dup(hb_community
);
941 void bgp_aggr_community_remove(void *arg
)
943 struct community
*community
= arg
;
945 community_free(&community
);
948 void bgp_compute_aggregate_community(struct bgp_aggregate
*aggregate
,
949 struct community
*community
)
951 struct community
*aggr_community
= NULL
;
953 if ((aggregate
== NULL
) || (community
== NULL
))
956 /* Create hash if not already created.
958 if (aggregate
->community_hash
== NULL
)
959 aggregate
->community_hash
= hash_create(
960 (unsigned int (*)(void *))community_hash_make
,
961 (bool (*)(const void *, const void *))community_cmp
,
962 "BGP Aggregator community hash");
964 aggr_community
= bgp_aggr_community_lookup(aggregate
, community
);
965 if (aggr_community
== NULL
) {
966 /* Insert community into hash.
968 aggr_community
= hash_get(aggregate
->community_hash
, community
,
969 bgp_aggr_communty_hash_alloc
);
971 /* Re-compute aggregate's community.
973 if (aggregate
->community
)
974 community_free(&aggregate
->community
);
976 hash_iterate(aggregate
->community_hash
,
977 bgp_aggr_community_prepare
,
978 &aggregate
->community
);
981 /* Increment refernce counter.
983 aggr_community
->refcnt
++;
986 void bgp_remove_community_from_aggregate(struct bgp_aggregate
*aggregate
,
987 struct community
*community
)
989 struct community
*aggr_community
= NULL
;
990 struct community
*ret_comm
= NULL
;
992 if ((aggregate
== NULL
) || (community
== NULL
))
995 if (aggregate
->community_hash
== NULL
)
998 /* Look-up the community in the hash.
1000 aggr_community
= bgp_aggr_community_lookup(aggregate
, community
);
1001 if (aggr_community
) {
1002 aggr_community
->refcnt
--;
1004 if (aggr_community
->refcnt
== 0) {
1005 ret_comm
= hash_release(aggregate
->community_hash
,
1007 community_free(&ret_comm
);
1009 community_free(&aggregate
->community
);
1011 /* Compute aggregate's community.
1013 hash_iterate(aggregate
->community_hash
,
1014 bgp_aggr_community_prepare
,
1015 &aggregate
->community
);