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 (struct community
*)XCALLOC(MTYPE_COMMUNITY
,
38 sizeof(struct community
));
41 /* Free communities value. */
42 void community_free(struct community
*com
)
45 XFREE(MTYPE_COMMUNITY_VAL
, com
->val
);
47 XFREE(MTYPE_COMMUNITY_STR
, com
->str
);
50 json_object_free(com
->json
);
54 XFREE(MTYPE_COMMUNITY
, com
);
57 /* Add one community value to the community. */
58 static void community_add_val(struct community
*com
, u_int32_t val
)
62 com
->val
= XREALLOC(MTYPE_COMMUNITY_VAL
, com
->val
,
65 com
->val
= XMALLOC(MTYPE_COMMUNITY_VAL
, com_length(com
));
68 memcpy(com_lastval(com
), &val
, sizeof(u_int32_t
));
71 /* Delete one community. */
72 void community_del_val(struct community
*com
, u_int32_t
*val
)
80 while (i
< com
->size
) {
81 if (memcmp(com
->val
+ i
, val
, sizeof(u_int32_t
)) == 0) {
82 c
= com
->size
- i
- 1;
85 memmove(com
->val
+ i
, com
->val
+ (i
+ 1),
91 com
->val
= XREALLOC(MTYPE_COMMUNITY_VAL
,
92 com
->val
, com_length(com
));
94 XFREE(MTYPE_COMMUNITY_VAL
, com
->val
);
103 /* Delete all communities listed in com2 from com1 */
104 struct community
*community_delete(struct community
*com1
,
105 struct community
*com2
)
109 while (i
< com2
->size
) {
110 community_del_val(com1
, com2
->val
+ i
);
117 /* Callback function from qsort(). */
118 static int community_compare(const void *a1
, const void *a2
)
123 memcpy(&v1
, a1
, sizeof(u_int32_t
));
124 memcpy(&v2
, a2
, sizeof(u_int32_t
));
135 int community_include(struct community
*com
, u_int32_t val
)
141 for (i
= 0; i
< com
->size
; i
++)
142 if (memcmp(&val
, com_nthval(com
, i
), sizeof(u_int32_t
)) == 0)
148 u_int32_t
community_val_get(struct community
*com
, int i
)
153 p
= (u_char
*)com
->val
;
156 memcpy(&val
, p
, sizeof(u_int32_t
));
161 /* Sort and uniq given community. */
162 struct community
*community_uniq_sort(struct community
*com
)
165 struct community
*new;
171 new = community_new();
175 for (i
= 0; i
< com
->size
; i
++) {
176 val
= community_val_get(com
, i
);
178 if (!community_include(new, val
))
179 community_add_val(new, val
);
182 qsort(new->val
, new->size
, sizeof(u_int32_t
), community_compare
);
187 /* Convert communities attribute to string.
189 For Well-known communities value, below keyword is used.
192 0xFFFFFF01 "no-export"
193 0xFFFFFF02 "no-advertise"
194 0xFFFFFF03 "local-AS"
195 0xFFFF0000 "graceful-shutdown"
197 For other values, "AS:VAL" format is used. */
198 static void set_community_string(struct community
*com
)
208 json_object
*json_community_list
= NULL
;
209 json_object
*json_string
= NULL
;
214 com
->json
= json_object_new_object();
215 json_community_list
= json_object_new_array();
217 /* When communities attribute is empty. */
218 if (com
->size
== 0) {
219 str
= XMALLOC(MTYPE_COMMUNITY_STR
, 1);
222 json_object_string_add(com
->json
, "string", "");
223 json_object_object_add(com
->json
, "list", json_community_list
);
228 /* Memory allocation is time consuming work. So we calculate
229 required string length first. */
232 for (i
= 0; i
< com
->size
; i
++) {
233 memcpy(&comval
, com_nthval(com
, i
), sizeof(u_int32_t
));
234 comval
= ntohl(comval
);
237 case COMMUNITY_INTERNET
:
238 len
+= strlen(" internet");
240 case COMMUNITY_NO_EXPORT
:
241 len
+= strlen(" no-export");
243 case COMMUNITY_NO_ADVERTISE
:
244 len
+= strlen(" no-advertise");
246 case COMMUNITY_LOCAL_AS
:
247 len
+= strlen(" local-AS");
249 case COMMUNITY_GSHUT
:
250 len
+= strlen(" graceful-shutdown");
253 len
+= strlen(" 65536:65535");
258 /* Allocate memory. */
259 str
= pnt
= XMALLOC(MTYPE_COMMUNITY_STR
, len
);
262 /* Fill in string. */
263 for (i
= 0; i
< com
->size
; i
++) {
264 memcpy(&comval
, com_nthval(com
, i
), sizeof(u_int32_t
));
265 comval
= ntohl(comval
);
273 case COMMUNITY_INTERNET
:
274 strcpy(pnt
, "internet");
275 pnt
+= strlen("internet");
276 json_string
= json_object_new_string("internet");
277 json_object_array_add(json_community_list
, json_string
);
279 case COMMUNITY_NO_EXPORT
:
280 strcpy(pnt
, "no-export");
281 pnt
+= strlen("no-export");
282 json_string
= json_object_new_string("noExport");
283 json_object_array_add(json_community_list
, json_string
);
285 case COMMUNITY_NO_ADVERTISE
:
286 strcpy(pnt
, "no-advertise");
287 pnt
+= strlen("no-advertise");
288 json_string
= json_object_new_string("noAdvertise");
289 json_object_array_add(json_community_list
, json_string
);
291 case COMMUNITY_LOCAL_AS
:
292 strcpy(pnt
, "local-AS");
293 pnt
+= strlen("local-AS");
294 json_string
= json_object_new_string("localAs");
295 json_object_array_add(json_community_list
, json_string
);
297 case COMMUNITY_GSHUT
:
298 strcpy(pnt
, "graceful-shutdown");
299 pnt
+= strlen("graceful-shutdown");
300 json_string
= json_object_new_string("gracefulShutdown");
301 json_object_array_add(json_community_list
, json_string
);
304 as
= (comval
>> 16) & 0xFFFF;
305 val
= comval
& 0xFFFF;
306 sprintf(pnt
, "%u:%d", as
, val
);
307 json_string
= json_object_new_string(pnt
);
308 json_object_array_add(json_community_list
, json_string
);
315 json_object_string_add(com
->json
, "string", str
);
316 json_object_object_add(com
->json
, "list", json_community_list
);
320 /* Intern communities attribute. */
321 struct community
*community_intern(struct community
*com
)
323 struct community
*find
;
325 /* Assert this community structure is not interned. */
326 assert(com
->refcnt
== 0);
328 /* Lookup community hash. */
329 find
= (struct community
*)hash_get(comhash
, com
, hash_alloc_intern
);
331 /* Arguemnt com is allocated temporary. So when it is not used in
332 hash, it should be freed. */
336 /* Increment refrence counter. */
341 set_community_string(find
);
346 /* Free community attribute. */
347 void community_unintern(struct community
**com
)
349 struct community
*ret
;
354 /* Pull off from hash. */
355 if ((*com
)->refcnt
== 0) {
356 /* Community value com must exist in hash. */
357 ret
= (struct community
*)hash_release(comhash
, *com
);
360 community_free(*com
);
365 /* Create new community attribute. */
366 struct community
*community_parse(u_int32_t
*pnt
, u_short length
)
368 struct community tmp
;
369 struct community
*new;
371 /* If length is malformed return NULL. */
375 /* Make temporary community for hash look up. */
376 tmp
.size
= length
/ 4;
379 new = community_uniq_sort(&tmp
);
381 return community_intern(new);
384 struct community
*community_dup(struct community
*com
)
386 struct community
*new;
388 new = XCALLOC(MTYPE_COMMUNITY
, sizeof(struct community
));
389 new->size
= com
->size
;
391 new->val
= XMALLOC(MTYPE_COMMUNITY_VAL
, com
->size
* 4);
392 memcpy(new->val
, com
->val
, com
->size
* 4);
398 /* Retrun string representation of communities attribute. */
399 char *community_str(struct community
*com
)
405 set_community_string(com
);
409 /* Make hash value of community attribute. This function is used by
411 unsigned int community_hash_make(struct community
*com
)
413 u_int32_t
*pnt
= (u_int32_t
*)com
->val
;
415 return jhash2(pnt
, com
->size
, 0x43ea96c1);
418 int community_match(const struct community
*com1
, const struct community
*com2
)
423 if (com1
== NULL
&& com2
== NULL
)
426 if (com1
== NULL
|| com2
== NULL
)
429 if (com1
->size
< com2
->size
)
432 /* Every community on com2 needs to be on com1 for this to match */
433 while (i
< com1
->size
&& j
< com2
->size
) {
434 if (memcmp(com1
->val
+ i
, com2
->val
+ j
, sizeof(u_int32_t
))
446 /* If two aspath have same value then return 1 else return 0. This
447 function is used by hash package. */
448 int community_cmp(const struct community
*com1
, const struct community
*com2
)
450 if (com1
== NULL
&& com2
== NULL
)
452 if (com1
== NULL
|| com2
== NULL
)
455 if (com1
->size
== com2
->size
)
456 if (memcmp(com1
->val
, com2
->val
, com1
->size
* 4) == 0)
461 /* Add com2 to the end of com1. */
462 struct community
*community_merge(struct community
*com1
,
463 struct community
*com2
)
466 com1
->val
= XREALLOC(MTYPE_COMMUNITY_VAL
, com1
->val
,
467 (com1
->size
+ com2
->size
) * 4);
469 com1
->val
= XMALLOC(MTYPE_COMMUNITY_VAL
,
470 (com1
->size
+ com2
->size
) * 4);
472 memcpy(com1
->val
+ com1
->size
, com2
->val
, com2
->size
* 4);
473 com1
->size
+= com2
->size
;
478 /* Community token enum. */
479 enum community_token
{
481 community_token_no_export
,
482 community_token_no_advertise
,
483 community_token_local_as
,
484 community_token_gshut
,
485 community_token_unknown
488 /* Get next community token from string. */
490 community_gettoken(const char *buf
, enum community_token
*token
, u_int32_t
*val
)
494 /* Skip white space. */
495 while (isspace((int)*p
))
498 /* Check the end of the line. */
502 /* Well known community string check. */
503 if (isalpha((int)*p
)) {
504 if (strncmp(p
, "internet", strlen("internet")) == 0) {
505 *val
= COMMUNITY_INTERNET
;
506 *token
= community_token_no_export
;
507 p
+= strlen("internet");
510 if (strncmp(p
, "no-export", strlen("no-export")) == 0) {
511 *val
= COMMUNITY_NO_EXPORT
;
512 *token
= community_token_no_export
;
513 p
+= strlen("no-export");
516 if (strncmp(p
, "no-advertise", strlen("no-advertise")) == 0) {
517 *val
= COMMUNITY_NO_ADVERTISE
;
518 *token
= community_token_no_advertise
;
519 p
+= strlen("no-advertise");
522 if (strncmp(p
, "local-AS", strlen("local-AS")) == 0) {
523 *val
= COMMUNITY_LOCAL_AS
;
524 *token
= community_token_local_as
;
525 p
+= strlen("local-AS");
528 if (strncmp(p
, "graceful-shutdown", strlen("graceful-shutdown")) == 0) {
529 *val
= COMMUNITY_GSHUT
;
530 *token
= community_token_gshut
;
531 p
+= strlen("graceful-shutdown");
535 /* Unknown string. */
536 *token
= community_token_unknown
;
540 /* Community value. */
541 if (isdigit((int)*p
)) {
544 u_int32_t community_low
= 0;
545 u_int32_t community_high
= 0;
547 while (isdigit((int)*p
) || *p
== ':') {
550 *token
= community_token_unknown
;
556 if (community_low
> UINT16_MAX
) {
558 community_token_unknown
;
562 community_high
= community_low
<< 16;
568 community_low
+= (*p
- '0');
573 *token
= community_token_unknown
;
577 if (community_low
> UINT16_MAX
) {
578 *token
= community_token_unknown
;
582 *val
= community_high
+ community_low
;
583 *token
= community_token_val
;
586 *token
= community_token_unknown
;
590 /* convert string to community structure */
591 struct community
*community_str2com(const char *str
)
593 struct community
*com
= NULL
;
594 struct community
*com_sort
= NULL
;
596 enum community_token token
= community_token_unknown
;
599 str
= community_gettoken(str
, &token
, &val
);
602 case community_token_val
:
603 case community_token_no_export
:
604 case community_token_no_advertise
:
605 case community_token_local_as
:
606 case community_token_gshut
:
608 com
= community_new();
611 community_add_val(com
, val
);
613 case community_token_unknown
:
624 com_sort
= community_uniq_sort(com
);
630 /* Return communities hash entry count. */
631 unsigned long community_count(void)
633 return comhash
->count
;
636 /* Return communities hash. */
637 struct hash
*community_hash(void)
642 /* Initialize comminity related hash. */
643 void community_init(void)
645 comhash
= hash_create(
646 (unsigned int (*)(void *))community_hash_make
,
647 (int (*)(const void *, const void *))community_cmp
,
648 "BGP Community Hash");
651 void community_finish(void)