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
29 #include "bgpd/bgpd.h"
30 #include "bgpd/bgp_lcommunity.h"
31 #include "bgpd/bgp_aspath.h"
33 /* Hash of community attribute. */
34 static struct hash
*lcomhash
;
36 /* Allocate a new lcommunities. */
37 static struct lcommunity
*lcommunity_new(void)
39 return (struct lcommunity
*)XCALLOC(MTYPE_LCOMMUNITY
,
40 sizeof(struct lcommunity
));
43 /* Allocate lcommunities. */
44 void lcommunity_free(struct lcommunity
**lcom
)
47 XFREE(MTYPE_LCOMMUNITY_VAL
, (*lcom
)->val
);
49 XFREE(MTYPE_LCOMMUNITY_STR
, (*lcom
)->str
);
50 XFREE(MTYPE_LCOMMUNITY
, *lcom
);
54 static void lcommunity_hash_free(struct lcommunity
*lcom
)
56 lcommunity_free(&lcom
);
59 /* Add a new Large Communities value to Large Communities
60 Attribute structure. When the value is already exists in the
61 structure, we don't add the value. Newly added value is sorted by
62 numerical order. When the value is added to the structure return 1
64 static int lcommunity_add_val(struct lcommunity
*lcom
,
65 struct lcommunity_val
*lval
)
71 /* When this is fist value, just add it. */
72 if (lcom
->val
== NULL
) {
74 lcom
->val
= XMALLOC(MTYPE_LCOMMUNITY_VAL
, lcom_length(lcom
));
75 memcpy(lcom
->val
, lval
->val
, LCOMMUNITY_SIZE
);
79 /* If the value already exists in the structure return 0. */
81 for (p
= lcom
->val
; c
< lcom
->size
; p
+= LCOMMUNITY_SIZE
, c
++) {
82 ret
= memcmp(p
, lval
->val
, LCOMMUNITY_SIZE
);
89 /* Add the value to the structure with numerical sorting. */
92 XREALLOC(MTYPE_LCOMMUNITY_VAL
, lcom
->val
, lcom_length(lcom
));
94 memmove(lcom
->val
+ (c
+ 1) * LCOMMUNITY_SIZE
,
95 lcom
->val
+ c
* LCOMMUNITY_SIZE
,
96 (lcom
->size
- 1 - c
) * LCOMMUNITY_SIZE
);
97 memcpy(lcom
->val
+ c
* LCOMMUNITY_SIZE
, lval
->val
, LCOMMUNITY_SIZE
);
102 /* This function takes pointer to Large Communites strucutre then
103 create a new Large Communities structure by uniq and sort each
104 Large Communities value. */
105 struct lcommunity
*lcommunity_uniq_sort(struct lcommunity
*lcom
)
108 struct lcommunity
*new;
109 struct lcommunity_val
*lval
;
114 new = lcommunity_new();
116 for (i
= 0; i
< lcom
->size
; i
++) {
117 lval
= (struct lcommunity_val
*)(lcom
->val
118 + (i
* LCOMMUNITY_SIZE
));
119 lcommunity_add_val(new, lval
);
124 /* Parse Large Communites Attribute in BGP packet. */
125 struct lcommunity
*lcommunity_parse(u_int8_t
*pnt
, u_short length
)
127 struct lcommunity tmp
;
128 struct lcommunity
*new;
131 if (length
% LCOMMUNITY_SIZE
)
134 /* Prepare tmporary structure for making a new Large Communities
136 tmp
.size
= length
/ LCOMMUNITY_SIZE
;
139 /* Create a new Large Communities Attribute by uniq and sort each
140 Large Communities value */
141 new = lcommunity_uniq_sort(&tmp
);
143 return lcommunity_intern(new);
146 /* Duplicate the Large Communities Attribute structure. */
147 struct lcommunity
*lcommunity_dup(struct lcommunity
*lcom
)
149 struct lcommunity
*new;
151 new = XCALLOC(MTYPE_LCOMMUNITY
, sizeof(struct lcommunity
));
152 new->size
= lcom
->size
;
154 new->val
= XMALLOC(MTYPE_LCOMMUNITY_VAL
, lcom_length(lcom
));
155 memcpy(new->val
, lcom
->val
, lcom_length(lcom
));
161 /* Retrun string representation of communities attribute. */
162 char *lcommunity_str(struct lcommunity
*lcom
)
166 lcommunity_lcom2str(lcom
, LCOMMUNITY_FORMAT_DISPLAY
);
170 /* Merge two Large Communities Attribute structure. */
171 struct lcommunity
*lcommunity_merge(struct lcommunity
*lcom1
,
172 struct lcommunity
*lcom2
)
176 XREALLOC(MTYPE_LCOMMUNITY_VAL
, lcom1
->val
,
177 lcom_length(lcom1
) + lcom_length(lcom2
));
180 XMALLOC(MTYPE_LCOMMUNITY_VAL
,
181 lcom_length(lcom1
) + lcom_length(lcom2
));
183 memcpy(lcom1
->val
+ lcom_length(lcom1
), lcom2
->val
, lcom_length(lcom2
));
184 lcom1
->size
+= lcom2
->size
;
189 /* Intern Large Communities Attribute. */
190 struct lcommunity
*lcommunity_intern(struct lcommunity
*lcom
)
192 struct lcommunity
*find
;
194 assert(lcom
->refcnt
== 0);
196 find
= (struct lcommunity
*)hash_get(lcomhash
, lcom
, hash_alloc_intern
);
199 lcommunity_free(&lcom
);
205 lcommunity_lcom2str(find
, LCOMMUNITY_FORMAT_DISPLAY
);
210 /* Unintern Large Communities Attribute. */
211 void lcommunity_unintern(struct lcommunity
**lcom
)
213 struct lcommunity
*ret
;
218 /* Pull off from hash. */
219 if ((*lcom
)->refcnt
== 0) {
220 /* Large community must be in the hash. */
221 ret
= (struct lcommunity
*)hash_release(lcomhash
, *lcom
);
224 lcommunity_free(lcom
);
228 /* Utility function to make hash key. */
229 unsigned int lcommunity_hash_make(void *arg
)
231 const struct lcommunity
*lcom
= arg
;
232 int size
= lcom_length(lcom
);
233 u_int8_t
*pnt
= lcom
->val
;
234 unsigned int key
= 0;
237 for (c
= 0; c
< size
; c
+= LCOMMUNITY_SIZE
) {
255 /* Compare two Large Communities Attribute structure. */
256 int lcommunity_cmp(const void *arg1
, const void *arg2
)
258 const struct lcommunity
*lcom1
= arg1
;
259 const struct lcommunity
*lcom2
= arg2
;
261 return (lcom1
->size
== lcom2
->size
262 && memcmp(lcom1
->val
, lcom2
->val
, lcom_length(lcom1
))
266 /* Return communities hash. */
267 struct hash
*lcommunity_hash(void)
272 /* Initialize Large Comminities related hash. */
273 void lcommunity_init(void)
275 lcomhash
= hash_create(lcommunity_hash_make
, lcommunity_cmp
, NULL
);
278 void lcommunity_finish(void)
280 hash_clean(lcomhash
, (void (*)(void *))lcommunity_hash_free
);
285 /* Large Communities token enum. */
286 enum lcommunity_token
{
287 lcommunity_token_unknown
= 0,
288 lcommunity_token_val
,
291 /* Get next Large Communities token from the string. */
292 static const char *lcommunity_gettoken(const char *str
,
293 struct lcommunity_val
*lval
,
294 enum lcommunity_token
*token
)
298 /* Skip white space. */
299 while (isspace((int)*p
)) {
304 /* Check the end of the line. */
308 /* Community value. */
309 if (isdigit((int)*p
)) {
312 u_int32_t globaladmin
= 0;
313 u_int32_t localdata1
= 0;
314 u_int32_t localdata2
= 0;
316 while (isdigit((int)*p
) || *p
== ':') {
318 if (separator
== 2) {
319 *token
= lcommunity_token_unknown
;
324 if (separator
== 1) {
325 globaladmin
= localdata2
;
327 localdata1
= localdata2
;
334 localdata2
+= (*p
- '0');
339 *token
= lcommunity_token_unknown
;
344 * Copy the large comm.
346 lval
->val
[0] = (globaladmin
>> 24) & 0xff;
347 lval
->val
[1] = (globaladmin
>> 16) & 0xff;
348 lval
->val
[2] = (globaladmin
>> 8) & 0xff;
349 lval
->val
[3] = globaladmin
& 0xff;
350 lval
->val
[4] = (localdata1
>> 24) & 0xff;
351 lval
->val
[5] = (localdata1
>> 16) & 0xff;
352 lval
->val
[6] = (localdata1
>> 8) & 0xff;
353 lval
->val
[7] = localdata1
& 0xff;
354 lval
->val
[8] = (localdata2
>> 24) & 0xff;
355 lval
->val
[9] = (localdata2
>> 16) & 0xff;
356 lval
->val
[10] = (localdata2
>> 8) & 0xff;
357 lval
->val
[11] = localdata2
& 0xff;
359 *token
= lcommunity_token_val
;
362 *token
= lcommunity_token_unknown
;
367 Convert string to large community attribute.
368 When type is already known, please specify both str and type.
370 When string includes keyword for each large community value.
371 Please specify keyword_included as non-zero value.
373 struct lcommunity
*lcommunity_str2com(const char *str
)
375 struct lcommunity
*lcom
= NULL
;
376 enum lcommunity_token token
= lcommunity_token_unknown
;
377 struct lcommunity_val lval
;
379 while ((str
= lcommunity_gettoken(str
, &lval
, &token
))) {
381 case lcommunity_token_val
:
383 lcom
= lcommunity_new();
384 lcommunity_add_val(lcom
, &lval
);
386 case lcommunity_token_unknown
:
389 lcommunity_free(&lcom
);
396 int lcommunity_include(struct lcommunity
*lcom
, u_char
*ptr
)
401 lcom_ptr
= lcom
->val
;
402 for (i
= 0; i
< lcom
->size
; i
++) {
403 lcom_ptr
+= (i
* LCOMMUNITY_SIZE
);
404 if (memcmp(ptr
, lcom_ptr
, LCOMMUNITY_SIZE
) == 0)
410 /* Convert large community attribute to string.
411 The large coms will be in 65535:65531:0 format.
413 char *lcommunity_lcom2str(struct lcommunity
*lcom
, int format
)
417 #define LCOMMUNITY_STR_DEFAULT_LEN 40
423 u_int32_t globaladmin
, localdata1
, localdata2
;
425 if (lcom
->size
== 0) {
426 str_buf
= XMALLOC(MTYPE_LCOMMUNITY_STR
, 1);
431 /* Prepare buffer. */
432 str_buf
= XMALLOC(MTYPE_LCOMMUNITY_STR
, LCOMMUNITY_STR_DEFAULT_LEN
+ 1);
433 str_size
= LCOMMUNITY_STR_DEFAULT_LEN
+ 1;
436 for (i
= 0; i
< lcom
->size
; i
++) {
437 /* Make it sure size is enough. */
438 while (str_pnt
+ LCOMMUNITY_STR_DEFAULT_LEN
>= str_size
) {
440 str_buf
= XREALLOC(MTYPE_LCOMMUNITY_STR
, str_buf
,
444 /* Space between each value. */
446 str_buf
[str_pnt
++] = ' ';
448 pnt
= lcom
->val
+ (i
* LCOMMUNITY_SIZE
);
450 globaladmin
= (*pnt
++ << 24);
451 globaladmin
|= (*pnt
++ << 16);
452 globaladmin
|= (*pnt
++ << 8);
453 globaladmin
|= (*pnt
++);
455 localdata1
= (*pnt
++ << 24);
456 localdata1
|= (*pnt
++ << 16);
457 localdata1
|= (*pnt
++ << 8);
458 localdata1
|= (*pnt
++);
460 localdata2
= (*pnt
++ << 24);
461 localdata2
|= (*pnt
++ << 16);
462 localdata2
|= (*pnt
++ << 8);
463 localdata2
|= (*pnt
++);
465 len
= sprintf(str_buf
+ str_pnt
, "%u:%u:%u", globaladmin
,
466 localdata1
, localdata2
);
473 int lcommunity_match(const struct lcommunity
*lcom1
,
474 const struct lcommunity
*lcom2
)
479 if (lcom1
== NULL
&& lcom2
== NULL
)
482 if (lcom1
== NULL
|| lcom2
== NULL
)
485 if (lcom1
->size
< lcom2
->size
)
488 /* Every community on com2 needs to be on com1 for this to match */
489 while (i
< lcom1
->size
&& j
< lcom2
->size
) {
490 if (memcmp(lcom1
->val
+ (i
* LCOMMUNITY_SIZE
), lcom2
->val
+ (j
* LCOMMUNITY_SIZE
),
497 if (j
== lcom2
->size
)
503 /* Delete one lcommunity. */
504 void lcommunity_del_val(struct lcommunity
*lcom
, u_char
*ptr
)
512 while (i
< lcom
->size
) {
513 if (memcmp(lcom
->val
+ i
* LCOMMUNITY_SIZE
, ptr
,
516 c
= lcom
->size
- i
- 1;
519 memmove(lcom
->val
+ i
* LCOMMUNITY_SIZE
,
520 lcom
->val
+ (i
+ 1) * LCOMMUNITY_SIZE
,
521 c
* LCOMMUNITY_SIZE
);
527 XREALLOC(MTYPE_COMMUNITY_VAL
, lcom
->val
,
530 XFREE(MTYPE_COMMUNITY_VAL
, lcom
->val
);