blob: 68383adfd5718baf5fa653566b0734f748b3abae [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Community attribute related functions.
2 Copyright (C) 1998, 2001 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "hash.h"
24#include "memory.h"
25
26#include "bgpd/bgp_community.h"
27
28/* Hash of community attribute. */
Stephen Hemminger730394d2009-05-15 10:17:09 -070029static struct hash *comhash;
paul718e3742002-12-13 20:15:29 +000030
31/* Allocate a new communities value. */
paul94f2b392005-06-28 12:44:16 +000032static struct community *
33community_new (void)
paul718e3742002-12-13 20:15:29 +000034{
35 return (struct community *) XCALLOC (MTYPE_COMMUNITY,
36 sizeof (struct community));
37}
38
39/* Free communities value. */
40void
41community_free (struct community *com)
42{
43 if (com->val)
44 XFREE (MTYPE_COMMUNITY_VAL, com->val);
45 if (com->str)
46 XFREE (MTYPE_COMMUNITY_STR, com->str);
47 XFREE (MTYPE_COMMUNITY, com);
48}
49
50/* Add one community value to the community. */
paul94f2b392005-06-28 12:44:16 +000051static void
paul718e3742002-12-13 20:15:29 +000052community_add_val (struct community *com, u_int32_t val)
53{
54 com->size++;
55 if (com->val)
56 com->val = XREALLOC (MTYPE_COMMUNITY_VAL, com->val, com_length (com));
57 else
58 com->val = XMALLOC (MTYPE_COMMUNITY_VAL, com_length (com));
59
60 val = htonl (val);
61 memcpy (com_lastval (com), &val, sizeof (u_int32_t));
62}
63
64/* Delete one community. */
65void
66community_del_val (struct community *com, u_int32_t *val)
67{
68 int i = 0;
69 int c = 0;
70
71 if (! com->val)
72 return;
73
74 while (i < com->size)
75 {
76 if (memcmp (com->val + i, val, sizeof (u_int32_t)) == 0)
77 {
78 c = com->size -i -1;
79
80 if (c > 0)
Andrew J. Schorr8178b2e2009-05-29 09:15:20 -040081 memcpy (com->val + i, com->val + (i + 1), c * sizeof (*val));
paul718e3742002-12-13 20:15:29 +000082
83 com->size--;
84
85 if (com->size > 0)
86 com->val = XREALLOC (MTYPE_COMMUNITY_VAL, com->val,
87 com_length (com));
88 else
89 {
90 XFREE (MTYPE_COMMUNITY_VAL, com->val);
91 com->val = NULL;
92 }
93 return;
94 }
95 i++;
96 }
97}
98
99/* Delete all communities listed in com2 from com1 */
100struct community *
101community_delete (struct community *com1, struct community *com2)
102{
103 int i = 0;
104
105 while(i < com2->size)
106 {
107 community_del_val (com1, com2->val + i);
108 i++;
109 }
110
111 return com1;
112}
113
114/* Callback function from qsort(). */
paul94f2b392005-06-28 12:44:16 +0000115static int
paul718e3742002-12-13 20:15:29 +0000116community_compare (const void *a1, const void *a2)
117{
118 u_int32_t v1;
119 u_int32_t v2;
120
121 memcpy (&v1, a1, sizeof (u_int32_t));
122 memcpy (&v2, a2, sizeof (u_int32_t));
123 v1 = ntohl (v1);
124 v2 = ntohl (v2);
125
126 if (v1 < v2)
127 return -1;
128 if (v1 > v2)
129 return 1;
130 return 0;
131}
132
133int
134community_include (struct community *com, u_int32_t val)
135{
136 int i;
137
138 val = htonl (val);
139
140 for (i = 0; i < com->size; i++)
141 if (memcmp (&val, com_nthval (com, i), sizeof (u_int32_t)) == 0)
142 return 1;
143
144 return 0;
145}
146
paul94f2b392005-06-28 12:44:16 +0000147static u_int32_t
paul718e3742002-12-13 20:15:29 +0000148community_val_get (struct community *com, int i)
149{
150 u_char *p;
151 u_int32_t val;
152
153 p = (u_char *) com->val;
154 p += (i * 4);
155
156 memcpy (&val, p, sizeof (u_int32_t));
157
158 return ntohl (val);
159}
160
161/* Sort and uniq given community. */
162struct community *
163community_uniq_sort (struct community *com)
164{
165 int i;
166 struct community *new;
167 u_int32_t val;
168
169 if (! com)
170 return NULL;
171
172 new = community_new ();;
173
174 for (i = 0; i < com->size; i++)
175 {
176 val = community_val_get (com, i);
177
178 if (! community_include (new, val))
179 community_add_val (new, val);
180 }
181
182 qsort (new->val, new->size, sizeof (u_int32_t), community_compare);
183
184 return new;
185}
186
187/* Convert communities attribute to string.
188
189 For Well-known communities value, below keyword is used.
190
191 0x0 "internet"
192 0xFFFFFF01 "no-export"
193 0xFFFFFF02 "no-advertise"
194 0xFFFFFF03 "local-AS"
195
196 For other values, "AS:VAL" format is used. */
197static char *
198community_com2str (struct community *com)
199{
200 int i;
201 char *str;
202 char *pnt;
203 int len;
204 int first;
205 u_int32_t comval;
206 u_int16_t as;
207 u_int16_t val;
208
Paul Jakmab2ceea12007-09-07 14:24:55 +0000209 if (!com)
210 return NULL;
211
paul718e3742002-12-13 20:15:29 +0000212 /* When communities attribute is empty. */
213 if (com->size == 0)
214 {
215 str = XMALLOC (MTYPE_COMMUNITY_STR, 1);
216 str[0] = '\0';
217 return str;
218 }
219
220 /* Memory allocation is time consuming work. So we calculate
221 required string length first. */
222 len = 0;
223
224 for (i = 0; i < com->size; i++)
225 {
226 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
227 comval = ntohl (comval);
228
229 switch (comval)
230 {
231 case COMMUNITY_INTERNET:
232 len += strlen (" internet");
233 break;
234 case COMMUNITY_NO_EXPORT:
235 len += strlen (" no-export");
236 break;
237 case COMMUNITY_NO_ADVERTISE:
238 len += strlen (" no-advertise");
239 break;
240 case COMMUNITY_LOCAL_AS:
241 len += strlen (" local-AS");
242 break;
243 default:
244 len += strlen (" 65536:65535");
245 break;
246 }
247 }
248
249 /* Allocate memory. */
250 str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
251 first = 1;
252
253 /* Fill in string. */
254 for (i = 0; i < com->size; i++)
255 {
256 memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
257 comval = ntohl (comval);
258
259 if (first)
260 first = 0;
261 else
262 *pnt++ = ' ';
263
264 switch (comval)
265 {
266 case COMMUNITY_INTERNET:
267 strcpy (pnt, "internet");
268 pnt += strlen ("internet");
269 break;
270 case COMMUNITY_NO_EXPORT:
271 strcpy (pnt, "no-export");
272 pnt += strlen ("no-export");
273 break;
274 case COMMUNITY_NO_ADVERTISE:
275 strcpy (pnt, "no-advertise");
276 pnt += strlen ("no-advertise");
277 break;
278 case COMMUNITY_LOCAL_AS:
279 strcpy (pnt, "local-AS");
280 pnt += strlen ("local-AS");
281 break;
282 default:
283 as = (comval >> 16) & 0xFFFF;
284 val = comval & 0xFFFF;
Denis Ovsienkoaea339f2009-04-30 17:16:22 +0400285 sprintf (pnt, "%u:%d", as, val);
paul718e3742002-12-13 20:15:29 +0000286 pnt += strlen (pnt);
287 break;
288 }
289 }
290 *pnt = '\0';
291
292 return str;
293}
294
Michael Lambert2c9fd7e2010-07-24 12:44:07 -0400295/* Find an 'intern'ed community structure */
296struct community *
297community_lookup (struct community *com)
298{
299 return (struct community *) hash_lookup (comhash, com);
300}
301
paul718e3742002-12-13 20:15:29 +0000302/* Intern communities attribute. */
303struct community *
304community_intern (struct community *com)
305{
306 struct community *find;
307
308 /* Assert this community structure is not interned. */
309 assert (com->refcnt == 0);
310
311 /* Lookup community hash. */
312 find = (struct community *) hash_get (comhash, com, hash_alloc_intern);
313
314 /* Arguemnt com is allocated temporary. So when it is not used in
315 hash, it should be freed. */
316 if (find != com)
317 community_free (com);
318
319 /* Increment refrence counter. */
320 find->refcnt++;
321
322 /* Make string. */
323 if (! find->str)
324 find->str = community_com2str (find);
325
326 return find;
327}
328
329/* Free community attribute. */
330void
331community_unintern (struct community *com)
332{
333 struct community *ret;
334
335 if (com->refcnt)
336 com->refcnt--;
337
338 /* Pull off from hash. */
339 if (com->refcnt == 0)
340 {
341 /* Community value com must exist in hash. */
342 ret = (struct community *) hash_release (comhash, com);
343 assert (ret != NULL);
344
345 community_free (com);
346 }
347}
348
349/* Create new community attribute. */
350struct community *
paul5228ad22004-06-04 17:58:18 +0000351community_parse (u_int32_t *pnt, u_short length)
paul718e3742002-12-13 20:15:29 +0000352{
353 struct community tmp;
354 struct community *new;
355
356 /* If length is malformed return NULL. */
357 if (length % 4)
358 return NULL;
359
360 /* Make temporary community for hash look up. */
361 tmp.size = length / 4;
paul5228ad22004-06-04 17:58:18 +0000362 tmp.val = pnt;
paul718e3742002-12-13 20:15:29 +0000363
364 new = community_uniq_sort (&tmp);
365
366 return community_intern (new);
367}
368
369struct community *
370community_dup (struct community *com)
371{
372 struct community *new;
373
374 new = XCALLOC (MTYPE_COMMUNITY, sizeof (struct community));
375 new->size = com->size;
376 if (new->size)
377 {
378 new->val = XMALLOC (MTYPE_COMMUNITY_VAL, com->size * 4);
379 memcpy (new->val, com->val, com->size * 4);
380 }
381 else
382 new->val = NULL;
383 return new;
384}
385
386/* Retrun string representation of communities attribute. */
387char *
388community_str (struct community *com)
389{
Paul Jakmab2ceea12007-09-07 14:24:55 +0000390 if (!com)
391 return NULL;
392
paul718e3742002-12-13 20:15:29 +0000393 if (! com->str)
394 com->str = community_com2str (com);
395 return com->str;
396}
397
398/* Make hash value of community attribute. This function is used by
399 hash package.*/
400unsigned int
401community_hash_make (struct community *com)
402{
403 int c;
404 unsigned int key;
405 unsigned char *pnt;
406
407 key = 0;
408 pnt = (unsigned char *)com->val;
409
410 for(c = 0; c < com->size * 4; c++)
411 key += pnt[c];
412
413 return key;
414}
415
416int
paulfd79ac92004-10-13 05:06:08 +0000417community_match (const struct community *com1, const struct community *com2)
paul718e3742002-12-13 20:15:29 +0000418{
419 int i = 0;
420 int j = 0;
421
422 if (com1 == NULL && com2 == NULL)
423 return 1;
424
425 if (com1 == NULL || com2 == NULL)
426 return 0;
427
428 if (com1->size < com2->size)
429 return 0;
430
431 /* Every community on com2 needs to be on com1 for this to match */
432 while (i < com1->size && j < com2->size)
433 {
434 if (memcmp (com1->val + i, com2->val + j, sizeof (u_int32_t)) == 0)
435 j++;
436 i++;
437 }
438
439 if (j == com2->size)
440 return 1;
441 else
442 return 0;
443}
444
445/* If two aspath have same value then return 1 else return 0. This
446 function is used by hash package. */
447int
paulfd79ac92004-10-13 05:06:08 +0000448community_cmp (const struct community *com1, const struct community *com2)
paul718e3742002-12-13 20:15:29 +0000449{
450 if (com1 == NULL && com2 == NULL)
451 return 1;
452 if (com1 == NULL || com2 == NULL)
453 return 0;
454
455 if (com1->size == com2->size)
456 if (memcmp (com1->val, com2->val, com1->size * 4) == 0)
457 return 1;
458 return 0;
459}
460
461/* Add com2 to the end of com1. */
462struct community *
463community_merge (struct community *com1, struct community *com2)
464{
465 if (com1->val)
466 com1->val = XREALLOC (MTYPE_COMMUNITY_VAL, com1->val,
467 (com1->size + com2->size) * 4);
468 else
469 com1->val = XMALLOC (MTYPE_COMMUNITY_VAL, (com1->size + com2->size) * 4);
470
471 memcpy (com1->val + com1->size, com2->val, com2->size * 4);
472 com1->size += com2->size;
473
474 return com1;
475}
476
477/* Community token enum. */
478enum community_token
479{
480 community_token_val,
481 community_token_no_export,
482 community_token_no_advertise,
483 community_token_local_as,
484 community_token_unknown
485};
486
487/* Get next community token from string. */
paul94f2b392005-06-28 12:44:16 +0000488static const char *
paulfd79ac92004-10-13 05:06:08 +0000489community_gettoken (const char *buf, enum community_token *token,
490 u_int32_t *val)
paul718e3742002-12-13 20:15:29 +0000491{
paulfd79ac92004-10-13 05:06:08 +0000492 const char *p = buf;
paul718e3742002-12-13 20:15:29 +0000493
494 /* Skip white space. */
495 while (isspace ((int) *p))
496 p++;
497
498 /* Check the end of the line. */
499 if (*p == '\0')
500 return NULL;
501
502 /* Well known community string check. */
503 if (isalpha ((int) *p))
504 {
505 if (strncmp (p, "internet", strlen ("internet")) == 0)
506 {
507 *val = COMMUNITY_INTERNET;
508 *token = community_token_no_export;
509 p += strlen ("internet");
510 return p;
511 }
512 if (strncmp (p, "no-export", strlen ("no-export")) == 0)
513 {
514 *val = COMMUNITY_NO_EXPORT;
515 *token = community_token_no_export;
516 p += strlen ("no-export");
517 return p;
518 }
519 if (strncmp (p, "no-advertise", strlen ("no-advertise")) == 0)
520 {
521 *val = COMMUNITY_NO_ADVERTISE;
522 *token = community_token_no_advertise;
523 p += strlen ("no-advertise");
524 return p;
525 }
526 if (strncmp (p, "local-AS", strlen ("local-AS")) == 0)
527 {
528 *val = COMMUNITY_LOCAL_AS;
529 *token = community_token_local_as;
530 p += strlen ("local-AS");
531 return p;
532 }
533
534 /* Unknown string. */
535 *token = community_token_unknown;
Paul Jakma15aa6a12006-03-30 14:39:35 +0000536 return NULL;
paul718e3742002-12-13 20:15:29 +0000537 }
538
539 /* Community value. */
540 if (isdigit ((int) *p))
541 {
542 int separator = 0;
543 int digit = 0;
544 u_int32_t community_low = 0;
545 u_int32_t community_high = 0;
546
547 while (isdigit ((int) *p) || *p == ':')
548 {
549 if (*p == ':')
550 {
551 if (separator)
552 {
553 *token = community_token_unknown;
Paul Jakma15aa6a12006-03-30 14:39:35 +0000554 return NULL;
paul718e3742002-12-13 20:15:29 +0000555 }
556 else
557 {
558 separator = 1;
559 digit = 0;
560 community_high = community_low << 16;
561 community_low = 0;
562 }
563 }
564 else
565 {
566 digit = 1;
567 community_low *= 10;
568 community_low += (*p - '0');
569 }
570 p++;
571 }
572 if (! digit)
573 {
574 *token = community_token_unknown;
Paul Jakma15aa6a12006-03-30 14:39:35 +0000575 return NULL;
paul718e3742002-12-13 20:15:29 +0000576 }
577 *val = community_high + community_low;
578 *token = community_token_val;
579 return p;
580 }
581 *token = community_token_unknown;
Paul Jakma15aa6a12006-03-30 14:39:35 +0000582 return NULL;
paul718e3742002-12-13 20:15:29 +0000583}
584
585/* convert string to community structure */
586struct community *
paulfd79ac92004-10-13 05:06:08 +0000587community_str2com (const char *str)
paul718e3742002-12-13 20:15:29 +0000588{
589 struct community *com = NULL;
590 struct community *com_sort = NULL;
Paul Jakma851a1a52008-07-22 19:56:56 +0000591 u_int32_t val = 0;
592 enum community_token token = community_token_unknown;
paul718e3742002-12-13 20:15:29 +0000593
Paul Jakma15aa6a12006-03-30 14:39:35 +0000594 do
paul718e3742002-12-13 20:15:29 +0000595 {
Paul Jakma15aa6a12006-03-30 14:39:35 +0000596 str = community_gettoken (str, &token, &val);
597
paul718e3742002-12-13 20:15:29 +0000598 switch (token)
599 {
600 case community_token_val:
601 case community_token_no_export:
602 case community_token_no_advertise:
603 case community_token_local_as:
604 if (com == NULL)
605 com = community_new();
606 community_add_val (com, val);
607 break;
608 case community_token_unknown:
609 default:
610 if (com)
611 community_free (com);
Paul Jakma542bcb72006-05-08 14:25:09 +0000612 return NULL;
paul718e3742002-12-13 20:15:29 +0000613 }
Paul Jakma15aa6a12006-03-30 14:39:35 +0000614 } while (str);
paul718e3742002-12-13 20:15:29 +0000615
616 if (! com)
617 return NULL;
618
619 com_sort = community_uniq_sort (com);
620 community_free (com);
621
622 return com_sort;
623}
624
625/* Return communities hash entry count. */
626unsigned long
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800627community_count (void)
paul718e3742002-12-13 20:15:29 +0000628{
629 return comhash->count;
630}
631
632/* Return communities hash. */
633struct hash *
paul94f2b392005-06-28 12:44:16 +0000634community_hash (void)
paul718e3742002-12-13 20:15:29 +0000635{
636 return comhash;
637}
638
639/* Initialize comminity related hash. */
640void
paul94f2b392005-06-28 12:44:16 +0000641community_init (void)
paul718e3742002-12-13 20:15:29 +0000642{
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +0100643 comhash = hash_create ((unsigned int (*) (void *))community_hash_make,
644 (int (*) (const void *, const void *))community_cmp);
paul718e3742002-12-13 20:15:29 +0000645}
Chris Caputo228da422009-07-18 05:44:03 +0000646
647void
648community_finish (void)
649{
650 hash_free (comhash);
651 comhash = NULL;
652}