]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_ecommunity.c
Merge remote-tracking branch 'origin/master' into pim_lib_work2
[mirror_frr.git] / bgpd / bgp_ecommunity.c
1 /* BGP Extended Communities Attribute
2 Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
3
4 This file is part of GNU Zebra.
5
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
9 later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include <zebra.h>
22
23 #include "hash.h"
24 #include "memory.h"
25 #include "prefix.h"
26 #include "command.h"
27 #include "queue.h"
28 #include "filter.h"
29
30 #include "bgpd/bgpd.h"
31 #include "bgpd/bgp_ecommunity.h"
32 #include "bgpd/bgp_aspath.h"
33
34 /* Hash of community attribute. */
35 static struct hash *ecomhash;
36
37 /* Allocate a new ecommunities. */
38 struct ecommunity *
39 ecommunity_new (void)
40 {
41 return (struct ecommunity *) XCALLOC (MTYPE_ECOMMUNITY,
42 sizeof (struct ecommunity));
43 }
44
45 /* Allocate ecommunities. */
46 void
47 ecommunity_free (struct ecommunity **ecom)
48 {
49 if ((*ecom)->val)
50 XFREE (MTYPE_ECOMMUNITY_VAL, (*ecom)->val);
51 if ((*ecom)->str)
52 XFREE (MTYPE_ECOMMUNITY_STR, (*ecom)->str);
53 XFREE (MTYPE_ECOMMUNITY, *ecom);
54 ecom = NULL;
55 }
56
57 static void
58 ecommunity_hash_free (struct ecommunity *ecom)
59 {
60 ecommunity_free(&ecom);
61 }
62
63
64 /* Add a new Extended Communities value to Extended Communities
65 Attribute structure. When the value is already exists in the
66 structure, we don't add the value. Newly added value is sorted by
67 numerical order. When the value is added to the structure return 1
68 else return 0. */
69 int
70 ecommunity_add_val (struct ecommunity *ecom, struct ecommunity_val *eval)
71 {
72 u_int8_t *p;
73 int ret;
74 int c;
75
76 /* When this is fist value, just add it. */
77 if (ecom->val == NULL)
78 {
79 ecom->size++;
80 ecom->val = XMALLOC (MTYPE_ECOMMUNITY_VAL, ecom_length (ecom));
81 memcpy (ecom->val, eval->val, ECOMMUNITY_SIZE);
82 return 1;
83 }
84
85 /* If the value already exists in the structure return 0. */
86 c = 0;
87 for (p = ecom->val; c < ecom->size; p += ECOMMUNITY_SIZE, c++)
88 {
89 ret = memcmp (p, eval->val, ECOMMUNITY_SIZE);
90 if (ret == 0)
91 return 0;
92 if (ret > 0)
93 break;
94 }
95
96 /* Add the value to the structure with numerical sorting. */
97 ecom->size++;
98 ecom->val = XREALLOC (MTYPE_ECOMMUNITY_VAL, ecom->val, ecom_length (ecom));
99
100 memmove (ecom->val + (c + 1) * ECOMMUNITY_SIZE,
101 ecom->val + c * ECOMMUNITY_SIZE,
102 (ecom->size - 1 - c) * ECOMMUNITY_SIZE);
103 memcpy (ecom->val + c * ECOMMUNITY_SIZE, eval->val, ECOMMUNITY_SIZE);
104
105 return 1;
106 }
107
108 /* This function takes pointer to Extended Communites strucutre then
109 create a new Extended Communities structure by uniq and sort each
110 Extended Communities value. */
111 struct ecommunity *
112 ecommunity_uniq_sort (struct ecommunity *ecom)
113 {
114 int i;
115 struct ecommunity *new;
116 struct ecommunity_val *eval;
117
118 if (! ecom)
119 return NULL;
120
121 new = ecommunity_new ();
122
123 for (i = 0; i < ecom->size; i++)
124 {
125 eval = (struct ecommunity_val *) (ecom->val + (i * ECOMMUNITY_SIZE));
126 ecommunity_add_val (new, eval);
127 }
128 return new;
129 }
130
131 /* Parse Extended Communites Attribute in BGP packet. */
132 struct ecommunity *
133 ecommunity_parse (u_int8_t *pnt, u_short length)
134 {
135 struct ecommunity tmp;
136 struct ecommunity *new;
137
138 /* Length check. */
139 if (length % ECOMMUNITY_SIZE)
140 return NULL;
141
142 /* Prepare tmporary structure for making a new Extended Communities
143 Attribute. */
144 tmp.size = length / ECOMMUNITY_SIZE;
145 tmp.val = pnt;
146
147 /* Create a new Extended Communities Attribute by uniq and sort each
148 Extended Communities value */
149 new = ecommunity_uniq_sort (&tmp);
150
151 return ecommunity_intern (new);
152 }
153
154 /* Duplicate the Extended Communities Attribute structure. */
155 struct ecommunity *
156 ecommunity_dup (struct ecommunity *ecom)
157 {
158 struct ecommunity *new;
159
160 new = XCALLOC (MTYPE_ECOMMUNITY, sizeof (struct ecommunity));
161 new->size = ecom->size;
162 if (new->size)
163 {
164 new->val = XMALLOC (MTYPE_ECOMMUNITY_VAL, ecom->size * ECOMMUNITY_SIZE);
165 memcpy (new->val, ecom->val, ecom->size * ECOMMUNITY_SIZE);
166 }
167 else
168 new->val = NULL;
169 return new;
170 }
171
172 /* Retrun string representation of communities attribute. */
173 char *
174 ecommunity_str (struct ecommunity *ecom)
175 {
176 if (! ecom->str)
177 ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
178 return ecom->str;
179 }
180
181 /* Merge two Extended Communities Attribute structure. */
182 struct ecommunity *
183 ecommunity_merge (struct ecommunity *ecom1, struct ecommunity *ecom2)
184 {
185 if (ecom1->val)
186 ecom1->val = XREALLOC (MTYPE_ECOMMUNITY_VAL, ecom1->val,
187 (ecom1->size + ecom2->size) * ECOMMUNITY_SIZE);
188 else
189 ecom1->val = XMALLOC (MTYPE_ECOMMUNITY_VAL,
190 (ecom1->size + ecom2->size) * ECOMMUNITY_SIZE);
191
192 memcpy (ecom1->val + (ecom1->size * ECOMMUNITY_SIZE),
193 ecom2->val, ecom2->size * ECOMMUNITY_SIZE);
194 ecom1->size += ecom2->size;
195
196 return ecom1;
197 }
198
199 /* Intern Extended Communities Attribute. */
200 struct ecommunity *
201 ecommunity_intern (struct ecommunity *ecom)
202 {
203 struct ecommunity *find;
204
205 assert (ecom->refcnt == 0);
206
207 find = (struct ecommunity *) hash_get (ecomhash, ecom, hash_alloc_intern);
208
209 if (find != ecom)
210 ecommunity_free (&ecom);
211
212 find->refcnt++;
213
214 if (! find->str)
215 find->str = ecommunity_ecom2str (find, ECOMMUNITY_FORMAT_DISPLAY);
216
217 return find;
218 }
219
220 /* Unintern Extended Communities Attribute. */
221 void
222 ecommunity_unintern (struct ecommunity **ecom)
223 {
224 struct ecommunity *ret;
225
226 if ((*ecom)->refcnt)
227 (*ecom)->refcnt--;
228
229 /* Pull off from hash. */
230 if ((*ecom)->refcnt == 0)
231 {
232 /* Extended community must be in the hash. */
233 ret = (struct ecommunity *) hash_release (ecomhash, *ecom);
234 assert (ret != NULL);
235
236 ecommunity_free (ecom);
237 }
238 }
239
240 /* Utinity function to make hash key. */
241 unsigned int
242 ecommunity_hash_make (void *arg)
243 {
244 const struct ecommunity *ecom = arg;
245 int size = ecom->size * ECOMMUNITY_SIZE;
246 u_int8_t *pnt = ecom->val;
247 unsigned int key = 0;
248 int c;
249
250 for (c = 0; c < size; c += ECOMMUNITY_SIZE)
251 {
252 key += pnt[c];
253 key += pnt[c + 1];
254 key += pnt[c + 2];
255 key += pnt[c + 3];
256 key += pnt[c + 4];
257 key += pnt[c + 5];
258 key += pnt[c + 6];
259 key += pnt[c + 7];
260 }
261
262 return key;
263 }
264
265 /* Compare two Extended Communities Attribute structure. */
266 int
267 ecommunity_cmp (const void *arg1, const void *arg2)
268 {
269 const struct ecommunity *ecom1 = arg1;
270 const struct ecommunity *ecom2 = arg2;
271
272 if (ecom1 == NULL && ecom2 == NULL)
273 return 1;
274
275 if (ecom1 == NULL || ecom2 == NULL)
276 return 0;
277
278 return (ecom1->size == ecom2->size
279 && memcmp (ecom1->val, ecom2->val, ecom1->size * ECOMMUNITY_SIZE) == 0);
280 }
281
282 /* Initialize Extended Comminities related hash. */
283 void
284 ecommunity_init (void)
285 {
286 ecomhash = hash_create (ecommunity_hash_make, ecommunity_cmp);
287 }
288
289 void
290 ecommunity_finish (void)
291 {
292 hash_clean (ecomhash, (void (*)(void *))ecommunity_hash_free);
293 hash_free (ecomhash);
294 ecomhash = NULL;
295 }
296
297 /* Extended Communities token enum. */
298 enum ecommunity_token
299 {
300 ecommunity_token_unknown = 0,
301 ecommunity_token_rt,
302 ecommunity_token_soo,
303 ecommunity_token_val,
304 };
305
306 /* Get next Extended Communities token from the string. */
307 static const char *
308 ecommunity_gettoken (const char *str, struct ecommunity_val *eval,
309 enum ecommunity_token *token)
310 {
311 int ret;
312 int dot = 0;
313 int digit = 0;
314 int separator = 0;
315 const char *p = str;
316 char *endptr;
317 struct in_addr ip;
318 as_t as = 0;
319 u_int32_t val = 0;
320 char buf[INET_ADDRSTRLEN + 1];
321
322 /* Skip white space. */
323 while (isspace ((int) *p))
324 {
325 p++;
326 str++;
327 }
328
329 /* Check the end of the line. */
330 if (*p == '\0')
331 return NULL;
332
333 /* "rt" and "soo" keyword parse. */
334 if (! isdigit ((int) *p))
335 {
336 /* "rt" match check. */
337 if (tolower ((int) *p) == 'r')
338 {
339 p++;
340 if (tolower ((int) *p) == 't')
341 {
342 p++;
343 *token = ecommunity_token_rt;
344 return p;
345 }
346 if (isspace ((int) *p) || *p == '\0')
347 {
348 *token = ecommunity_token_rt;
349 return p;
350 }
351 goto error;
352 }
353 /* "soo" match check. */
354 else if (tolower ((int) *p) == 's')
355 {
356 p++;
357 if (tolower ((int) *p) == 'o')
358 {
359 p++;
360 if (tolower ((int) *p) == 'o')
361 {
362 p++;
363 *token = ecommunity_token_soo;
364 return p;
365 }
366 if (isspace ((int) *p) || *p == '\0')
367 {
368 *token = ecommunity_token_soo;
369 return p;
370 }
371 goto error;
372 }
373 if (isspace ((int) *p) || *p == '\0')
374 {
375 *token = ecommunity_token_soo;
376 return p;
377 }
378 goto error;
379 }
380 goto error;
381 }
382
383 /* What a mess, there are several possibilities:
384 *
385 * a) A.B.C.D:MN
386 * b) EF:OPQR
387 * c) GHJK:MN
388 *
389 * A.B.C.D: Four Byte IP
390 * EF: Two byte ASN
391 * GHJK: Four-byte ASN
392 * MN: Two byte value
393 * OPQR: Four byte value
394 *
395 */
396 while (isdigit ((int) *p) || *p == ':' || *p == '.')
397 {
398 if (*p == ':')
399 {
400 if (separator)
401 goto error;
402
403 separator = 1;
404 digit = 0;
405
406 if ((p - str) > INET_ADDRSTRLEN)
407 goto error;
408 memset (buf, 0, INET_ADDRSTRLEN + 1);
409 memcpy (buf, str, p - str);
410
411 if (dot)
412 {
413 /* Parsing A.B.C.D in:
414 * A.B.C.D:MN
415 */
416 ret = inet_aton (buf, &ip);
417 if (ret == 0)
418 goto error;
419 }
420 else
421 {
422 /* ASN */
423 as = strtoul (buf, &endptr, 10);
424 if (*endptr != '\0' || as == BGP_AS4_MAX)
425 goto error;
426 }
427 }
428 else if (*p == '.')
429 {
430 if (separator)
431 goto error;
432 dot++;
433 if (dot > 4)
434 goto error;
435 }
436 else
437 {
438 digit = 1;
439
440 /* We're past the IP/ASN part */
441 if (separator)
442 {
443 val *= 10;
444 val += (*p - '0');
445 }
446 }
447 p++;
448 }
449
450 /* Low digit part must be there. */
451 if (!digit || !separator)
452 goto error;
453
454 /* Encode result into routing distinguisher. */
455 if (dot)
456 {
457 if (val > UINT16_MAX)
458 goto error;
459
460 eval->val[0] = ECOMMUNITY_ENCODE_IP;
461 eval->val[1] = 0;
462 memcpy (&eval->val[2], &ip, sizeof (struct in_addr));
463 eval->val[6] = (val >> 8) & 0xff;
464 eval->val[7] = val & 0xff;
465 }
466 else if (as > BGP_AS_MAX)
467 {
468 if (val > UINT16_MAX)
469 goto error;
470
471 eval->val[0] = ECOMMUNITY_ENCODE_AS4;
472 eval->val[1] = 0;
473 eval->val[2] = (as >>24) & 0xff;
474 eval->val[3] = (as >>16) & 0xff;
475 eval->val[4] = (as >>8) & 0xff;
476 eval->val[5] = as & 0xff;
477 eval->val[6] = (val >> 8) & 0xff;
478 eval->val[7] = val & 0xff;
479 }
480 else
481 {
482 eval->val[0] = ECOMMUNITY_ENCODE_AS;
483 eval->val[1] = 0;
484
485 eval->val[2] = (as >>8) & 0xff;
486 eval->val[3] = as & 0xff;
487 eval->val[4] = (val >>24) & 0xff;
488 eval->val[5] = (val >>16) & 0xff;
489 eval->val[6] = (val >>8) & 0xff;
490 eval->val[7] = val & 0xff;
491 }
492 *token = ecommunity_token_val;
493 return p;
494
495 error:
496 *token = ecommunity_token_unknown;
497 return p;
498 }
499
500 /* Convert string to extended community attribute.
501
502 When type is already known, please specify both str and type. str
503 should not include keyword such as "rt" and "soo". Type is
504 ECOMMUNITY_ROUTE_TARGET or ECOMMUNITY_SITE_ORIGIN.
505 keyword_included should be zero.
506
507 For example route-map's "set extcommunity" command case:
508
509 "rt 100:1 100:2 100:3" -> str = "100:1 100:2 100:3"
510 type = ECOMMUNITY_ROUTE_TARGET
511 keyword_included = 0
512
513 "soo 100:1" -> str = "100:1"
514 type = ECOMMUNITY_SITE_ORIGIN
515 keyword_included = 0
516
517 When string includes keyword for each extended community value.
518 Please specify keyword_included as non-zero value.
519
520 For example standard extcommunity-list case:
521
522 "rt 100:1 rt 100:2 soo 100:1" -> str = "rt 100:1 rt 100:2 soo 100:1"
523 type = 0
524 keyword_include = 1
525 */
526 struct ecommunity *
527 ecommunity_str2com (const char *str, int type, int keyword_included)
528 {
529 struct ecommunity *ecom = NULL;
530 enum ecommunity_token token = ecommunity_token_unknown;
531 struct ecommunity_val eval;
532 int keyword = 0;
533
534 while ((str = ecommunity_gettoken (str, &eval, &token)))
535 {
536 switch (token)
537 {
538 case ecommunity_token_rt:
539 case ecommunity_token_soo:
540 if (! keyword_included || keyword)
541 {
542 if (ecom)
543 ecommunity_free (&ecom);
544 return NULL;
545 }
546 keyword = 1;
547
548 if (token == ecommunity_token_rt)
549 {
550 type = ECOMMUNITY_ROUTE_TARGET;
551 }
552 if (token == ecommunity_token_soo)
553 {
554 type = ECOMMUNITY_SITE_ORIGIN;
555 }
556 break;
557 case ecommunity_token_val:
558 if (keyword_included)
559 {
560 if (! keyword)
561 {
562 if (ecom)
563 ecommunity_free (&ecom);
564 return NULL;
565 }
566 keyword = 0;
567 }
568 if (ecom == NULL)
569 ecom = ecommunity_new ();
570 eval.val[1] = type;
571 ecommunity_add_val (ecom, &eval);
572 break;
573 case ecommunity_token_unknown:
574 default:
575 if (ecom)
576 ecommunity_free (&ecom);
577 return NULL;
578 }
579 }
580 return ecom;
581 }
582
583 /* Convert extended community attribute to string.
584
585 Due to historical reason of industry standard implementation, there
586 are three types of format.
587
588 route-map set extcommunity format
589 "rt 100:1 100:2"
590 "soo 100:3"
591
592 extcommunity-list
593 "rt 100:1 rt 100:2 soo 100:3"
594
595 "show [ip] bgp" and extcommunity-list regular expression matching
596 "RT:100:1 RT:100:2 SoO:100:3"
597
598 For each formath please use below definition for format:
599
600 ECOMMUNITY_FORMAT_ROUTE_MAP
601 ECOMMUNITY_FORMAT_COMMUNITY_LIST
602 ECOMMUNITY_FORMAT_DISPLAY
603 */
604 char *
605 ecommunity_ecom2str (struct ecommunity *ecom, int format)
606 {
607 int i;
608 u_int8_t *pnt;
609 int encode = 0;
610 int type = 0;
611 #define ECOMMUNITY_STR_DEFAULT_LEN 27
612 int str_size;
613 int str_pnt;
614 char *str_buf;
615 const char *prefix;
616 int len = 0;
617 int first = 1;
618
619 /* For parse Extended Community attribute tupple. */
620 struct ecommunity_as
621 {
622 as_t as;
623 u_int32_t val;
624 } eas;
625
626 struct ecommunity_ip
627 {
628 struct in_addr ip;
629 u_int16_t val;
630 } eip;
631
632 if (ecom->size == 0)
633 {
634 str_buf = XMALLOC (MTYPE_ECOMMUNITY_STR, 1);
635 str_buf[0] = '\0';
636 return str_buf;
637 }
638
639 /* Prepare buffer. */
640 str_buf = XMALLOC (MTYPE_ECOMMUNITY_STR, ECOMMUNITY_STR_DEFAULT_LEN + 1);
641 str_size = ECOMMUNITY_STR_DEFAULT_LEN + 1;
642 str_pnt = 0;
643
644 for (i = 0; i < ecom->size; i++)
645 {
646 /* Make it sure size is enough. */
647 while (str_pnt + ECOMMUNITY_STR_DEFAULT_LEN >= str_size)
648 {
649 str_size *= 2;
650 str_buf = XREALLOC (MTYPE_ECOMMUNITY_STR, str_buf, str_size);
651 }
652
653 /* Space between each value. */
654 if (! first)
655 str_buf[str_pnt++] = ' ';
656
657 pnt = ecom->val + (i * 8);
658
659 /* High-order octet of type. */
660 encode = *pnt++;
661
662 switch (encode)
663 {
664 case ECOMMUNITY_ENCODE_AS:
665 case ECOMMUNITY_ENCODE_IP:
666 case ECOMMUNITY_ENCODE_AS4:
667 break;
668
669 case ECOMMUNITY_ENCODE_OPAQUE:
670 if (*pnt == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP)
671 {
672 uint16_t tunneltype;
673 memcpy (&tunneltype, pnt + 5, 2);
674 tunneltype = ntohs(tunneltype);
675 len = sprintf (str_buf + str_pnt, "ET:%d", tunneltype);
676 str_pnt += len;
677 first = 0;
678 continue;
679 }
680 /* fall through */
681
682 default:
683 len = sprintf (str_buf + str_pnt, "?");
684 str_pnt += len;
685 first = 0;
686 continue;
687 }
688
689 /* Low-order octet of type. */
690 type = *pnt++;
691 if (type != ECOMMUNITY_ROUTE_TARGET && type != ECOMMUNITY_SITE_ORIGIN)
692 {
693 len = sprintf (str_buf + str_pnt, "?");
694 str_pnt += len;
695 first = 0;
696 continue;
697 }
698
699 switch (format)
700 {
701 case ECOMMUNITY_FORMAT_COMMUNITY_LIST:
702 prefix = (type == ECOMMUNITY_ROUTE_TARGET ? "rt " : "soo ");
703 break;
704 case ECOMMUNITY_FORMAT_DISPLAY:
705 prefix = (type == ECOMMUNITY_ROUTE_TARGET ? "RT:" : "SoO:");
706 break;
707 case ECOMMUNITY_FORMAT_ROUTE_MAP:
708 prefix = "";
709 break;
710 default:
711 prefix = "";
712 break;
713 }
714
715 /* Put string into buffer. */
716 if (encode == ECOMMUNITY_ENCODE_AS4)
717 {
718 eas.as = (*pnt++ << 24);
719 eas.as |= (*pnt++ << 16);
720 eas.as |= (*pnt++ << 8);
721 eas.as |= (*pnt++);
722
723 eas.val = (*pnt++ << 8);
724 eas.val |= (*pnt++);
725
726 len = sprintf( str_buf + str_pnt, "%s%u:%u", prefix,
727 eas.as, eas.val );
728 str_pnt += len;
729 first = 0;
730 }
731 if (encode == ECOMMUNITY_ENCODE_AS)
732 {
733 eas.as = (*pnt++ << 8);
734 eas.as |= (*pnt++);
735
736 eas.val = (*pnt++ << 24);
737 eas.val |= (*pnt++ << 16);
738 eas.val |= (*pnt++ << 8);
739 eas.val |= (*pnt++);
740
741 len = sprintf (str_buf + str_pnt, "%s%u:%u", prefix,
742 eas.as, eas.val);
743 str_pnt += len;
744 first = 0;
745 }
746 else if (encode == ECOMMUNITY_ENCODE_IP)
747 {
748 memcpy (&eip.ip, pnt, 4);
749 pnt += 4;
750 eip.val = (*pnt++ << 8);
751 eip.val |= (*pnt++);
752
753 len = sprintf (str_buf + str_pnt, "%s%s:%u", prefix,
754 inet_ntoa (eip.ip), eip.val);
755 str_pnt += len;
756 first = 0;
757 }
758 }
759 return str_buf;
760 }
761
762 int
763 ecommunity_match (const struct ecommunity *ecom1,
764 const struct ecommunity *ecom2)
765 {
766 int i = 0;
767 int j = 0;
768
769 if (ecom1 == NULL && ecom2 == NULL)
770 return 1;
771
772 if (ecom1 == NULL || ecom2 == NULL)
773 return 0;
774
775 if (ecom1->size < ecom2->size)
776 return 0;
777
778 /* Every community on com2 needs to be on com1 for this to match */
779 while (i < ecom1->size && j < ecom2->size)
780 {
781 if (memcmp (ecom1->val + i * ECOMMUNITY_SIZE,
782 ecom2->val + j * ECOMMUNITY_SIZE,
783 ECOMMUNITY_SIZE) == 0)
784 j++;
785 i++;
786 }
787
788 if (j == ecom2->size)
789 return 1;
790 else
791 return 0;
792 }