1 /* BGP Large Communities Attribute
3 * Copyright (C) 2016 Keyur Patel <keyur@arrcus.com>
5 * This file is part of FreeRangeRouting (FRR).
7 * FRR is free software; you can redistribute it and/or modify it under the
8 * terms of the GNU General Public License as published by the Free Software
9 * Foundation; either version 2, or (at your option) any later version.
11 * FRR is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
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
31 #include "bgpd/bgpd.h"
32 #include "bgpd/bgp_lcommunity.h"
33 #include "bgpd/bgp_aspath.h"
35 /* Hash of community attribute. */
36 static struct hash
*lcomhash
;
38 /* Allocate a new lcommunities. */
39 static struct lcommunity
*lcommunity_new(void)
41 return (struct lcommunity
*)XCALLOC(MTYPE_LCOMMUNITY
,
42 sizeof(struct lcommunity
));
45 /* Allocate lcommunities. */
46 void lcommunity_free(struct lcommunity
**lcom
)
49 XFREE(MTYPE_LCOMMUNITY_VAL
, (*lcom
)->val
);
51 XFREE(MTYPE_LCOMMUNITY_STR
, (*lcom
)->str
);
52 XFREE(MTYPE_LCOMMUNITY
, *lcom
);
56 static void lcommunity_hash_free(struct lcommunity
*lcom
)
58 lcommunity_free(&lcom
);
61 /* Add a new Large Communities value to Large Communities
62 Attribute structure. When the value is already exists in the
63 structure, we don't add the value. Newly added value is sorted by
64 numerical order. When the value is added to the structure return 1
66 static int lcommunity_add_val(struct lcommunity
*lcom
,
67 struct lcommunity_val
*lval
)
73 /* When this is fist value, just add it. */
74 if (lcom
->val
== NULL
) {
76 lcom
->val
= XMALLOC(MTYPE_LCOMMUNITY_VAL
, lcom_length(lcom
));
77 memcpy(lcom
->val
, lval
->val
, LCOMMUNITY_SIZE
);
81 /* If the value already exists in the structure return 0. */
83 for (p
= lcom
->val
; c
< lcom
->size
; p
+= LCOMMUNITY_SIZE
, c
++) {
84 ret
= memcmp(p
, lval
->val
, LCOMMUNITY_SIZE
);
91 /* Add the value to the structure with numerical sorting. */
94 XREALLOC(MTYPE_LCOMMUNITY_VAL
, lcom
->val
, lcom_length(lcom
));
96 memmove(lcom
->val
+ (c
+ 1) * LCOMMUNITY_SIZE
,
97 lcom
->val
+ c
* LCOMMUNITY_SIZE
,
98 (lcom
->size
- 1 - c
) * LCOMMUNITY_SIZE
);
99 memcpy(lcom
->val
+ c
* LCOMMUNITY_SIZE
, lval
->val
, LCOMMUNITY_SIZE
);
104 /* This function takes pointer to Large Communites strucutre then
105 create a new Large Communities structure by uniq and sort each
106 Large Communities value. */
107 struct lcommunity
*lcommunity_uniq_sort(struct lcommunity
*lcom
)
110 struct lcommunity
*new;
111 struct lcommunity_val
*lval
;
116 new = lcommunity_new();
118 for (i
= 0; i
< lcom
->size
; i
++) {
119 lval
= (struct lcommunity_val
*)(lcom
->val
120 + (i
* LCOMMUNITY_SIZE
));
121 lcommunity_add_val(new, lval
);
126 /* Parse Large Communites Attribute in BGP packet. */
127 struct lcommunity
*lcommunity_parse(uint8_t *pnt
, unsigned short length
)
129 struct lcommunity tmp
;
130 struct lcommunity
*new;
133 if (length
% LCOMMUNITY_SIZE
)
136 /* Prepare tmporary structure for making a new Large Communities
138 tmp
.size
= length
/ LCOMMUNITY_SIZE
;
141 /* Create a new Large Communities Attribute by uniq and sort each
142 Large Communities value */
143 new = lcommunity_uniq_sort(&tmp
);
145 return lcommunity_intern(new);
148 /* Duplicate the Large Communities Attribute structure. */
149 struct lcommunity
*lcommunity_dup(struct lcommunity
*lcom
)
151 struct lcommunity
*new;
153 new = lcommunity_new();
154 new->size
= lcom
->size
;
156 new->val
= XMALLOC(MTYPE_LCOMMUNITY_VAL
, lcom_length(lcom
));
157 memcpy(new->val
, lcom
->val
, lcom_length(lcom
));
163 /* Merge two Large Communities Attribute structure. */
164 struct lcommunity
*lcommunity_merge(struct lcommunity
*lcom1
,
165 struct lcommunity
*lcom2
)
168 lcom1
->val
= XREALLOC(MTYPE_LCOMMUNITY_VAL
, lcom1
->val
,
169 lcom_length(lcom1
) + lcom_length(lcom2
));
171 lcom1
->val
= XMALLOC(MTYPE_LCOMMUNITY_VAL
,
172 lcom_length(lcom1
) + lcom_length(lcom2
));
174 memcpy(lcom1
->val
+ lcom_length(lcom1
), lcom2
->val
, lcom_length(lcom2
));
175 lcom1
->size
+= lcom2
->size
;
180 static void set_lcommunity_string(struct lcommunity
*lcom
, bool make_json
)
188 uint32_t global
, local1
, local2
;
189 json_object
*json_lcommunity_list
= NULL
;
190 json_object
*json_string
= NULL
;
192 #define LCOMMUNITY_STR_DEFAULT_LEN 32
198 lcom
->json
= json_object_new_object();
199 json_lcommunity_list
= json_object_new_array();
202 if (lcom
->size
== 0) {
203 str_buf
= XMALLOC(MTYPE_LCOMMUNITY_STR
, 1);
207 json_object_string_add(lcom
->json
, "string", "");
208 json_object_object_add(lcom
->json
, "list",
209 json_lcommunity_list
);
217 XMALLOC(MTYPE_LCOMMUNITY_STR
,
218 (LCOMMUNITY_STR_DEFAULT_LEN
* lcom
->size
) + 1);
220 for (i
= 0; i
< lcom
->size
; i
++) {
226 pnt
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
227 pnt
= ptr_get_be32(pnt
, &global
);
228 pnt
= ptr_get_be32(pnt
, &local1
);
229 pnt
= ptr_get_be32(pnt
, &local2
);
232 len
= sprintf(str_pnt
, "%u:%u:%u", global
, local1
, local2
);
234 json_string
= json_object_new_string(str_pnt
);
235 json_object_array_add(json_lcommunity_list
,
243 XREALLOC(MTYPE_LCOMMUNITY_STR
, str_buf
, str_pnt
- str_buf
+ 1);
246 json_object_string_add(lcom
->json
, "string", str_buf
);
247 json_object_object_add(lcom
->json
, "list",
248 json_lcommunity_list
);
254 /* Intern Large Communities Attribute. */
255 struct lcommunity
*lcommunity_intern(struct lcommunity
*lcom
)
257 struct lcommunity
*find
;
259 assert(lcom
->refcnt
== 0);
261 find
= (struct lcommunity
*)hash_get(lcomhash
, lcom
, hash_alloc_intern
);
264 lcommunity_free(&lcom
);
269 set_lcommunity_string(find
, false);
274 /* Unintern Large Communities Attribute. */
275 void lcommunity_unintern(struct lcommunity
**lcom
)
277 struct lcommunity
*ret
;
282 /* Pull off from hash. */
283 if ((*lcom
)->refcnt
== 0) {
284 /* Large community must be in the hash. */
285 ret
= (struct lcommunity
*)hash_release(lcomhash
, *lcom
);
288 lcommunity_free(lcom
);
292 /* Retrun string representation of communities attribute. */
293 char *lcommunity_str(struct lcommunity
*lcom
, bool make_json
)
298 if (make_json
&& !lcom
->json
&& lcom
->str
)
299 XFREE(MTYPE_LCOMMUNITY_STR
, lcom
->str
);
302 set_lcommunity_string(lcom
, make_json
);
307 /* Utility function to make hash key. */
308 unsigned int lcommunity_hash_make(void *arg
)
310 const struct lcommunity
*lcom
= arg
;
311 int size
= lcom_length(lcom
);
313 return jhash(lcom
->val
, size
, 0xab125423);
316 /* Compare two Large Communities Attribute structure. */
317 int lcommunity_cmp(const void *arg1
, const void *arg2
)
319 const struct lcommunity
*lcom1
= arg1
;
320 const struct lcommunity
*lcom2
= arg2
;
322 return (lcom1
->size
== lcom2
->size
323 && memcmp(lcom1
->val
, lcom2
->val
, lcom_length(lcom1
)) == 0);
326 /* Return communities hash. */
327 struct hash
*lcommunity_hash(void)
332 /* Initialize Large Comminities related hash. */
333 void lcommunity_init(void)
335 lcomhash
= hash_create(lcommunity_hash_make
, lcommunity_cmp
,
336 "BGP lcommunity hash");
339 void lcommunity_finish(void)
341 hash_clean(lcomhash
, (void (*)(void *))lcommunity_hash_free
);
346 /* Large Communities token enum. */
347 enum lcommunity_token
{
348 lcommunity_token_unknown
= 0,
349 lcommunity_token_val
,
352 /* Get next Large Communities token from the string. */
353 static const char *lcommunity_gettoken(const char *str
,
354 struct lcommunity_val
*lval
,
355 enum lcommunity_token
*token
)
359 /* Skip white space. */
360 while (isspace((int)*p
)) {
365 /* Check the end of the line. */
369 /* Community value. */
370 if (isdigit((int)*p
)) {
373 uint32_t globaladmin
= 0;
374 uint32_t localdata1
= 0;
375 uint32_t localdata2
= 0;
377 while (isdigit((int)*p
) || *p
== ':') {
379 if (separator
== 2) {
380 *token
= lcommunity_token_unknown
;
385 if (separator
== 1) {
386 globaladmin
= localdata2
;
388 localdata1
= localdata2
;
395 localdata2
+= (*p
- '0');
400 *token
= lcommunity_token_unknown
;
405 * Copy the large comm.
407 lval
->val
[0] = (globaladmin
>> 24) & 0xff;
408 lval
->val
[1] = (globaladmin
>> 16) & 0xff;
409 lval
->val
[2] = (globaladmin
>> 8) & 0xff;
410 lval
->val
[3] = globaladmin
& 0xff;
411 lval
->val
[4] = (localdata1
>> 24) & 0xff;
412 lval
->val
[5] = (localdata1
>> 16) & 0xff;
413 lval
->val
[6] = (localdata1
>> 8) & 0xff;
414 lval
->val
[7] = localdata1
& 0xff;
415 lval
->val
[8] = (localdata2
>> 24) & 0xff;
416 lval
->val
[9] = (localdata2
>> 16) & 0xff;
417 lval
->val
[10] = (localdata2
>> 8) & 0xff;
418 lval
->val
[11] = localdata2
& 0xff;
420 *token
= lcommunity_token_val
;
423 *token
= lcommunity_token_unknown
;
428 Convert string to large community attribute.
429 When type is already known, please specify both str and type.
431 When string includes keyword for each large community value.
432 Please specify keyword_included as non-zero value.
434 struct lcommunity
*lcommunity_str2com(const char *str
)
436 struct lcommunity
*lcom
= NULL
;
437 enum lcommunity_token token
= lcommunity_token_unknown
;
438 struct lcommunity_val lval
;
440 while ((str
= lcommunity_gettoken(str
, &lval
, &token
))) {
442 case lcommunity_token_val
:
444 lcom
= lcommunity_new();
445 lcommunity_add_val(lcom
, &lval
);
447 case lcommunity_token_unknown
:
450 lcommunity_free(&lcom
);
457 int lcommunity_include(struct lcommunity
*lcom
, uint8_t *ptr
)
462 for (i
= 0; i
< lcom
->size
; i
++) {
463 lcom_ptr
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
464 if (memcmp(ptr
, lcom_ptr
, LCOMMUNITY_SIZE
) == 0)
470 int lcommunity_match(const struct lcommunity
*lcom1
,
471 const struct lcommunity
*lcom2
)
476 if (lcom1
== NULL
&& lcom2
== NULL
)
479 if (lcom1
== NULL
|| lcom2
== NULL
)
482 if (lcom1
->size
< lcom2
->size
)
485 /* Every community on com2 needs to be on com1 for this to match */
486 while (i
< lcom1
->size
&& j
< lcom2
->size
) {
487 if (memcmp(lcom1
->val
+ (i
* LCOMMUNITY_SIZE
),
488 lcom2
->val
+ (j
* LCOMMUNITY_SIZE
), LCOMMUNITY_SIZE
)
494 if (j
== lcom2
->size
)
500 /* Delete one lcommunity. */
501 void lcommunity_del_val(struct lcommunity
*lcom
, uint8_t *ptr
)
509 while (i
< lcom
->size
) {
510 if (memcmp(lcom
->val
+ i
* LCOMMUNITY_SIZE
, ptr
,
513 c
= lcom
->size
- i
- 1;
516 memmove(lcom
->val
+ i
* LCOMMUNITY_SIZE
,
517 lcom
->val
+ (i
+ 1) * LCOMMUNITY_SIZE
,
518 c
* LCOMMUNITY_SIZE
);
524 XREALLOC(MTYPE_LCOMMUNITY_VAL
,
525 lcom
->val
, lcom_length(lcom
));
527 XFREE(MTYPE_LCOMMUNITY_VAL
, lcom
->val
);