]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_ecommunity.c
tools: Convert type int functions to bool where possible
[mirror_frr.git] / bgpd / bgp_ecommunity.c
CommitLineData
718e3744 1/* BGP Extended Communities Attribute
896014f4
DL
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 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
19 */
718e3744 20
21#include <zebra.h>
22
23#include "hash.h"
24#include "memory.h"
25#include "prefix.h"
26#include "command.h"
3f9c7369 27#include "queue.h"
039f3a34 28#include "filter.h"
3f65c5b1 29#include "jhash.h"
937652c6 30#include "stream.h"
718e3744 31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_ecommunity.h"
57d187bc 34#include "bgpd/bgp_lcommunity.h"
0b2aa3a0 35#include "bgpd/bgp_aspath.h"
a8d72b61 36#include "bgpd/bgp_flowspec_private.h"
dacf6ec1 37#include "bgpd/bgp_pbr.h"
a8d72b61
PG
38
39/* struct used to dump the rate contained in FS set traffic-rate EC */
40union traffic_rate {
41 float rate_float;
42 uint8_t rate_byte[4];
43};
718e3744 44
45/* Hash of community attribute. */
ffa4e2c4 46static struct hash *ecomhash;
6b0655a2 47
718e3744 48/* Allocate a new ecommunities. */
d62a17ae 49struct ecommunity *ecommunity_new(void)
718e3744 50{
9f5dc319 51 return XCALLOC(MTYPE_ECOMMUNITY, sizeof(struct ecommunity));
718e3744 52}
53
c7ee6c35
DS
54void ecommunity_strfree(char **s)
55{
56 XFREE(MTYPE_ECOMMUNITY_STR, *s);
57}
58
718e3744 59/* Allocate ecommunities. */
d62a17ae 60void ecommunity_free(struct ecommunity **ecom)
718e3744 61{
0a22ddfb
QY
62 XFREE(MTYPE_ECOMMUNITY_VAL, (*ecom)->val);
63 XFREE(MTYPE_ECOMMUNITY_STR, (*ecom)->str);
d62a17ae 64 XFREE(MTYPE_ECOMMUNITY, *ecom);
718e3744 65}
66
d62a17ae 67static void ecommunity_hash_free(struct ecommunity *ecom)
7bae2fb9 68{
d62a17ae 69 ecommunity_free(&ecom);
7bae2fb9
LB
70}
71
72
718e3744 73/* Add a new Extended Communities value to Extended Communities
74 Attribute structure. When the value is already exists in the
75 structure, we don't add the value. Newly added value is sorted by
76 numerical order. When the value is added to the structure return 1
77 else return 0. */
d62a17ae 78int ecommunity_add_val(struct ecommunity *ecom, struct ecommunity_val *eval)
718e3744 79{
d62a17ae 80 int c;
81
91085f97 82 /* When this is fist value, just add it. */
d62a17ae 83 if (ecom->val == NULL) {
91085f97
QY
84 ecom->size = 1;
85 ecom->val = XCALLOC(MTYPE_ECOMMUNITY_VAL, ECOMMUNITY_SIZE);
d62a17ae 86 memcpy(ecom->val, eval->val, ECOMMUNITY_SIZE);
87 return 1;
88 }
89
90 /* If the value already exists in the structure return 0. */
91 c = 0;
91085f97
QY
92 for (uint8_t *p = ecom->val; c < ecom->size;
93 p += ECOMMUNITY_SIZE, c++) {
94 int ret = memcmp(p, eval->val, ECOMMUNITY_SIZE);
d62a17ae 95 if (ret == 0)
96 return 0;
91085f97 97 else if (ret > 0)
d62a17ae 98 break;
99 }
100
101 /* Add the value to the structure with numerical sorting. */
102 ecom->size++;
91085f97
QY
103 ecom->val = XREALLOC(MTYPE_ECOMMUNITY_VAL, ecom->val,
104 ecom->size * ECOMMUNITY_SIZE);
d62a17ae 105
91085f97
QY
106 memmove(ecom->val + ((c + 1) * ECOMMUNITY_SIZE),
107 ecom->val + (c * ECOMMUNITY_SIZE),
d62a17ae 108 (ecom->size - 1 - c) * ECOMMUNITY_SIZE);
91085f97 109 memcpy(ecom->val + (c * ECOMMUNITY_SIZE), eval->val, ECOMMUNITY_SIZE);
d62a17ae 110
111 return 1;
718e3744 112}
113
114/* This function takes pointer to Extended Communites strucutre then
115 create a new Extended Communities structure by uniq and sort each
e6b6a564 116 Extended Communities value. */
d62a17ae 117struct ecommunity *ecommunity_uniq_sort(struct ecommunity *ecom)
718e3744 118{
d62a17ae 119 int i;
120 struct ecommunity *new;
121 struct ecommunity_val *eval;
122
123 if (!ecom)
124 return NULL;
125
126 new = ecommunity_new();
127
128 for (i = 0; i < ecom->size; i++) {
129 eval = (struct ecommunity_val *)(ecom->val
130 + (i * ECOMMUNITY_SIZE));
131 ecommunity_add_val(new, eval);
132 }
133 return new;
718e3744 134}
135
136/* Parse Extended Communites Attribute in BGP packet. */
d7c0a89a 137struct ecommunity *ecommunity_parse(uint8_t *pnt, unsigned short length)
718e3744 138{
d62a17ae 139 struct ecommunity tmp;
140 struct ecommunity *new;
718e3744 141
d62a17ae 142 /* Length check. */
143 if (length % ECOMMUNITY_SIZE)
144 return NULL;
718e3744 145
d62a17ae 146 /* Prepare tmporary structure for making a new Extended Communities
147 Attribute. */
148 tmp.size = length / ECOMMUNITY_SIZE;
149 tmp.val = pnt;
718e3744 150
d62a17ae 151 /* Create a new Extended Communities Attribute by uniq and sort each
152 Extended Communities value */
153 new = ecommunity_uniq_sort(&tmp);
718e3744 154
d62a17ae 155 return ecommunity_intern(new);
718e3744 156}
157
158/* Duplicate the Extended Communities Attribute structure. */
d62a17ae 159struct ecommunity *ecommunity_dup(struct ecommunity *ecom)
718e3744 160{
d62a17ae 161 struct ecommunity *new;
162
163 new = XCALLOC(MTYPE_ECOMMUNITY, sizeof(struct ecommunity));
164 new->size = ecom->size;
165 if (new->size) {
166 new->val = XMALLOC(MTYPE_ECOMMUNITY_VAL,
167 ecom->size * ECOMMUNITY_SIZE);
168 memcpy(new->val, ecom->val, ecom->size * ECOMMUNITY_SIZE);
169 } else
170 new->val = NULL;
171 return new;
718e3744 172}
173
4372df71 174/* Retrun string representation of communities attribute. */
d62a17ae 175char *ecommunity_str(struct ecommunity *ecom)
4372df71 176{
d62a17ae 177 if (!ecom->str)
178 ecom->str =
179 ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
180 return ecom->str;
4372df71 181}
182
718e3744 183/* Merge two Extended Communities Attribute structure. */
d62a17ae 184struct ecommunity *ecommunity_merge(struct ecommunity *ecom1,
185 struct ecommunity *ecom2)
718e3744 186{
d62a17ae 187 if (ecom1->val)
188 ecom1->val =
189 XREALLOC(MTYPE_ECOMMUNITY_VAL, ecom1->val,
190 (ecom1->size + ecom2->size) * ECOMMUNITY_SIZE);
191 else
192 ecom1->val =
193 XMALLOC(MTYPE_ECOMMUNITY_VAL,
194 (ecom1->size + ecom2->size) * ECOMMUNITY_SIZE);
195
196 memcpy(ecom1->val + (ecom1->size * ECOMMUNITY_SIZE), ecom2->val,
197 ecom2->size * ECOMMUNITY_SIZE);
198 ecom1->size += ecom2->size;
199
200 return ecom1;
718e3744 201}
202
203/* Intern Extended Communities Attribute. */
d62a17ae 204struct ecommunity *ecommunity_intern(struct ecommunity *ecom)
718e3744 205{
d62a17ae 206 struct ecommunity *find;
718e3744 207
d62a17ae 208 assert(ecom->refcnt == 0);
718e3744 209
d62a17ae 210 find = (struct ecommunity *)hash_get(ecomhash, ecom, hash_alloc_intern);
718e3744 211
d62a17ae 212 if (find != ecom)
213 ecommunity_free(&ecom);
718e3744 214
d62a17ae 215 find->refcnt++;
718e3744 216
d62a17ae 217 if (!find->str)
218 find->str =
219 ecommunity_ecom2str(find, ECOMMUNITY_FORMAT_DISPLAY, 0);
718e3744 220
d62a17ae 221 return find;
718e3744 222}
223
224/* Unintern Extended Communities Attribute. */
d62a17ae 225void ecommunity_unintern(struct ecommunity **ecom)
718e3744 226{
d62a17ae 227 struct ecommunity *ret;
228
229 if ((*ecom)->refcnt)
230 (*ecom)->refcnt--;
231
232 /* Pull off from hash. */
233 if ((*ecom)->refcnt == 0) {
234 /* Extended community must be in the hash. */
235 ret = (struct ecommunity *)hash_release(ecomhash, *ecom);
236 assert(ret != NULL);
237
238 ecommunity_free(ecom);
239 }
718e3744 240}
241
242/* Utinity function to make hash key. */
d8b87afe 243unsigned int ecommunity_hash_make(const void *arg)
718e3744 244{
d62a17ae 245 const struct ecommunity *ecom = arg;
246 int size = ecom->size * ECOMMUNITY_SIZE;
d62a17ae 247
3f65c5b1 248 return jhash(ecom->val, size, 0x564321ab);
718e3744 249}
250
251/* Compare two Extended Communities Attribute structure. */
74df8d6d 252bool ecommunity_cmp(const void *arg1, const void *arg2)
718e3744 253{
d62a17ae 254 const struct ecommunity *ecom1 = arg1;
255 const struct ecommunity *ecom2 = arg2;
256
257 if (ecom1 == NULL && ecom2 == NULL)
74df8d6d 258 return true;
813d4307 259
d62a17ae 260 if (ecom1 == NULL || ecom2 == NULL)
74df8d6d 261 return false;
813d4307 262
d62a17ae 263 return (ecom1->size == ecom2->size
264 && memcmp(ecom1->val, ecom2->val, ecom1->size * ECOMMUNITY_SIZE)
265 == 0);
718e3744 266}
267
268/* Initialize Extended Comminities related hash. */
d62a17ae 269void ecommunity_init(void)
718e3744 270{
996c9314 271 ecomhash = hash_create(ecommunity_hash_make, ecommunity_cmp,
3f65c5b1 272 "BGP ecommunity hash");
718e3744 273}
228da428 274
d62a17ae 275void ecommunity_finish(void)
228da428 276{
d62a17ae 277 hash_clean(ecomhash, (void (*)(void *))ecommunity_hash_free);
278 hash_free(ecomhash);
279 ecomhash = NULL;
228da428 280}
6b0655a2 281
718e3744 282/* Extended Communities token enum. */
d62a17ae 283enum ecommunity_token {
284 ecommunity_token_unknown = 0,
285 ecommunity_token_rt,
286 ecommunity_token_soo,
287 ecommunity_token_val,
718e3744 288};
289
c5900768 290/*
291 * Encode BGP extended community from passed values. Supports types
292 * defined in RFC 4360 and well-known sub-types.
293 */
d7c0a89a
QY
294static int ecommunity_encode(uint8_t type, uint8_t sub_type, int trans, as_t as,
295 struct in_addr ip, uint32_t val,
d62a17ae 296 struct ecommunity_val *eval)
c5900768 297{
d62a17ae 298 assert(eval);
299 if (type == ECOMMUNITY_ENCODE_AS) {
300 if (as > BGP_AS_MAX)
301 return -1;
302 } else if (type == ECOMMUNITY_ENCODE_IP
303 || type == ECOMMUNITY_ENCODE_AS4) {
304 if (val > UINT16_MAX)
305 return -1;
306 }
307
308 /* Fill in the values. */
309 eval->val[0] = type;
310 if (!trans)
311 eval->val[0] |= ECOMMUNITY_FLAG_NON_TRANSITIVE;
312 eval->val[1] = sub_type;
313 if (type == ECOMMUNITY_ENCODE_AS) {
314 eval->val[2] = (as >> 8) & 0xff;
315 eval->val[3] = as & 0xff;
316 eval->val[4] = (val >> 24) & 0xff;
317 eval->val[5] = (val >> 16) & 0xff;
318 eval->val[6] = (val >> 8) & 0xff;
319 eval->val[7] = val & 0xff;
320 } else if (type == ECOMMUNITY_ENCODE_IP) {
321 memcpy(&eval->val[2], &ip, sizeof(struct in_addr));
322 eval->val[6] = (val >> 8) & 0xff;
323 eval->val[7] = val & 0xff;
324 } else {
325 eval->val[2] = (as >> 24) & 0xff;
326 eval->val[3] = (as >> 16) & 0xff;
327 eval->val[4] = (as >> 8) & 0xff;
328 eval->val[5] = as & 0xff;
329 eval->val[6] = (val >> 8) & 0xff;
330 eval->val[7] = val & 0xff;
331 }
332
333 return 0;
c5900768 334}
335
718e3744 336/* Get next Extended Communities token from the string. */
d62a17ae 337static const char *ecommunity_gettoken(const char *str,
338 struct ecommunity_val *eval,
339 enum ecommunity_token *token)
718e3744 340{
d62a17ae 341 int ret;
342 int dot = 0;
343 int digit = 0;
344 int separator = 0;
345 const char *p = str;
346 char *endptr;
347 struct in_addr ip;
348 as_t as = 0;
d7c0a89a
QY
349 uint32_t val = 0;
350 uint8_t ecomm_type;
d62a17ae 351 char buf[INET_ADDRSTRLEN + 1];
352
353 /* Skip white space. */
fefa5e0f 354 while (isspace((unsigned char)*p)) {
d62a17ae 355 p++;
356 str++;
718e3744 357 }
d62a17ae 358
359 /* Check the end of the line. */
360 if (*p == '\0')
361 return NULL;
362
363 /* "rt" and "soo" keyword parse. */
fefa5e0f 364 if (!isdigit((unsigned char)*p)) {
d62a17ae 365 /* "rt" match check. */
fefa5e0f 366 if (tolower((unsigned char)*p) == 'r') {
d62a17ae 367 p++;
fefa5e0f 368 if (tolower((unsigned char)*p) == 't') {
d62a17ae 369 p++;
370 *token = ecommunity_token_rt;
371 return p;
372 }
fefa5e0f 373 if (isspace((unsigned char)*p) || *p == '\0') {
d62a17ae 374 *token = ecommunity_token_rt;
375 return p;
376 }
377 goto error;
718e3744 378 }
d62a17ae 379 /* "soo" match check. */
fefa5e0f 380 else if (tolower((unsigned char)*p) == 's') {
d62a17ae 381 p++;
fefa5e0f 382 if (tolower((unsigned char)*p) == 'o') {
d62a17ae 383 p++;
fefa5e0f 384 if (tolower((unsigned char)*p) == 'o') {
d62a17ae 385 p++;
386 *token = ecommunity_token_soo;
387 return p;
388 }
fefa5e0f 389 if (isspace((unsigned char)*p) || *p == '\0') {
d62a17ae 390 *token = ecommunity_token_soo;
391 return p;
392 }
393 goto error;
394 }
fefa5e0f 395 if (isspace((unsigned char)*p) || *p == '\0') {
d62a17ae 396 *token = ecommunity_token_soo;
397 return p;
398 }
399 goto error;
718e3744 400 }
d62a17ae 401 goto error;
718e3744 402 }
d62a17ae 403
404 /* What a mess, there are several possibilities:
405 *
406 * a) A.B.C.D:MN
407 * b) EF:OPQR
408 * c) GHJK:MN
409 *
410 * A.B.C.D: Four Byte IP
411 * EF: Two byte ASN
412 * GHJK: Four-byte ASN
413 * MN: Two byte value
414 * OPQR: Four byte value
415 *
416 */
fefa5e0f 417 while (isdigit((unsigned char)*p) || *p == ':' || *p == '.') {
d62a17ae 418 if (*p == ':') {
419 if (separator)
420 goto error;
421
422 separator = 1;
423 digit = 0;
424
425 if ((p - str) > INET_ADDRSTRLEN)
426 goto error;
427 memset(buf, 0, INET_ADDRSTRLEN + 1);
428 memcpy(buf, str, p - str);
429
430 if (dot) {
431 /* Parsing A.B.C.D in:
432 * A.B.C.D:MN
433 */
434 ret = inet_aton(buf, &ip);
435 if (ret == 0)
436 goto error;
437 } else {
438 /* ASN */
439 as = strtoul(buf, &endptr, 10);
440 if (*endptr != '\0' || as == BGP_AS4_MAX)
441 goto error;
442 }
443 } else if (*p == '.') {
444 if (separator)
445 goto error;
446 dot++;
447 if (dot > 4)
448 goto error;
449 } else {
450 digit = 1;
451
452 /* We're past the IP/ASN part */
453 if (separator) {
454 val *= 10;
455 val += (*p - '0');
456 }
457 }
458 p++;
718e3744 459 }
d62a17ae 460
461 /* Low digit part must be there. */
462 if (!digit || !separator)
463 goto error;
464
465 /* Encode result into extended community. */
466 if (dot)
467 ecomm_type = ECOMMUNITY_ENCODE_IP;
468 else if (as > BGP_AS_MAX)
469 ecomm_type = ECOMMUNITY_ENCODE_AS4;
470 else
471 ecomm_type = ECOMMUNITY_ENCODE_AS;
472 if (ecommunity_encode(ecomm_type, 0, 1, as, ip, val, eval))
473 goto error;
474 *token = ecommunity_token_val;
475 return p;
476
477error:
478 *token = ecommunity_token_unknown;
479 return p;
718e3744 480}
481
d62a17ae 482/* Convert string to extended community attribute.
718e3744 483
484 When type is already known, please specify both str and type. str
485 should not include keyword such as "rt" and "soo". Type is
486 ECOMMUNITY_ROUTE_TARGET or ECOMMUNITY_SITE_ORIGIN.
487 keyword_included should be zero.
488
489 For example route-map's "set extcommunity" command case:
490
491 "rt 100:1 100:2 100:3" -> str = "100:1 100:2 100:3"
d62a17ae 492 type = ECOMMUNITY_ROUTE_TARGET
493 keyword_included = 0
718e3744 494
495 "soo 100:1" -> str = "100:1"
d62a17ae 496 type = ECOMMUNITY_SITE_ORIGIN
497 keyword_included = 0
718e3744 498
499 When string includes keyword for each extended community value.
500 Please specify keyword_included as non-zero value.
501
502 For example standard extcommunity-list case:
503
504 "rt 100:1 rt 100:2 soo 100:1" -> str = "rt 100:1 rt 100:2 soo 100:1"
d62a17ae 505 type = 0
506 keyword_include = 1
718e3744 507*/
d62a17ae 508struct ecommunity *ecommunity_str2com(const char *str, int type,
509 int keyword_included)
718e3744 510{
d62a17ae 511 struct ecommunity *ecom = NULL;
512 enum ecommunity_token token = ecommunity_token_unknown;
513 struct ecommunity_val eval;
514 int keyword = 0;
515
516 while ((str = ecommunity_gettoken(str, &eval, &token))) {
517 switch (token) {
518 case ecommunity_token_rt:
519 case ecommunity_token_soo:
520 if (!keyword_included || keyword) {
521 if (ecom)
522 ecommunity_free(&ecom);
523 return NULL;
524 }
525 keyword = 1;
526
527 if (token == ecommunity_token_rt) {
528 type = ECOMMUNITY_ROUTE_TARGET;
529 }
530 if (token == ecommunity_token_soo) {
531 type = ECOMMUNITY_SITE_ORIGIN;
532 }
533 break;
534 case ecommunity_token_val:
535 if (keyword_included) {
536 if (!keyword) {
537 if (ecom)
538 ecommunity_free(&ecom);
539 return NULL;
540 }
541 keyword = 0;
542 }
543 if (ecom == NULL)
544 ecom = ecommunity_new();
545 eval.val[1] = type;
546 ecommunity_add_val(ecom, &eval);
547 break;
548 case ecommunity_token_unknown:
549 default:
550 if (ecom)
551 ecommunity_free(&ecom);
552 return NULL;
718e3744 553 }
718e3744 554 }
d62a17ae 555 return ecom;
718e3744 556}
557
91085f97
QY
558static int ecommunity_rt_soo_str(char *buf, size_t bufsz, uint8_t *pnt,
559 int type, int sub_type, int format)
c5900768 560{
d62a17ae 561 int len = 0;
562 const char *prefix;
563
564 /* For parse Extended Community attribute tupple. */
5a0ccebf
QY
565 struct ecommunity_as eas;
566 struct ecommunity_ip eip;
d62a17ae 567
568
569 /* Determine prefix for string, if any. */
570 switch (format) {
571 case ECOMMUNITY_FORMAT_COMMUNITY_LIST:
572 prefix = (sub_type == ECOMMUNITY_ROUTE_TARGET ? "rt " : "soo ");
573 break;
574 case ECOMMUNITY_FORMAT_DISPLAY:
575 prefix = (sub_type == ECOMMUNITY_ROUTE_TARGET ? "RT:" : "SoO:");
576 break;
577 case ECOMMUNITY_FORMAT_ROUTE_MAP:
578 prefix = "";
579 break;
580 default:
581 prefix = "";
582 break;
583 }
584
585 /* Put string into buffer. */
586 if (type == ECOMMUNITY_ENCODE_AS4) {
937652c6 587 pnt = ptr_get_be32(pnt, &eas.as);
d62a17ae 588 eas.val = (*pnt++ << 8);
589 eas.val |= (*pnt++);
590
91085f97 591 len = snprintf(buf, bufsz, "%s%u:%u", prefix, eas.as, eas.val);
d62a17ae 592 } else if (type == ECOMMUNITY_ENCODE_AS) {
593 eas.as = (*pnt++ << 8);
594 eas.as |= (*pnt++);
937652c6 595 pnt = ptr_get_be32(pnt, &eas.val);
d62a17ae 596
91085f97 597 len = snprintf(buf, bufsz, "%s%u:%u", prefix, eas.as, eas.val);
d62a17ae 598 } else if (type == ECOMMUNITY_ENCODE_IP) {
599 memcpy(&eip.ip, pnt, 4);
600 pnt += 4;
601 eip.val = (*pnt++ << 8);
602 eip.val |= (*pnt++);
603
91085f97
QY
604 len = snprintf(buf, bufsz, "%s%s:%u", prefix, inet_ntoa(eip.ip),
605 eip.val);
d62a17ae 606 }
91085f97
QY
607
608 /* consume value */
609 (void)pnt;
d62a17ae 610
611 return len;
c5900768 612}
613
d62a17ae 614/* Convert extended community attribute to string.
718e3744 615
616 Due to historical reason of industry standard implementation, there
617 are three types of format.
618
619 route-map set extcommunity format
d62a17ae 620 "rt 100:1 100:2"
621 "soo 100:3"
718e3744 622
623 extcommunity-list
d62a17ae 624 "rt 100:1 rt 100:2 soo 100:3"
718e3744 625
716b2d8a 626 "show [ip] bgp" and extcommunity-list regular expression matching
d62a17ae 627 "RT:100:1 RT:100:2 SoO:100:3"
718e3744 628
629 For each formath please use below definition for format:
630
631 ECOMMUNITY_FORMAT_ROUTE_MAP
632 ECOMMUNITY_FORMAT_COMMUNITY_LIST
633 ECOMMUNITY_FORMAT_DISPLAY
e82202b7 634
d62a17ae 635 Filter is added to display only ECOMMUNITY_ROUTE_TARGET in some cases.
e82202b7 636 0 value displays all
718e3744 637*/
d62a17ae 638char *ecommunity_ecom2str(struct ecommunity *ecom, int format, int filter)
718e3744 639{
d62a17ae 640 int i;
d7c0a89a 641 uint8_t *pnt;
1ef3c51f
PG
642 uint8_t type = 0;
643 uint8_t sub_type = 0;
91085f97 644#define ECOMMUNITY_STRLEN 64
d62a17ae 645 int str_size;
d62a17ae 646 char *str_buf;
d62a17ae 647
91085f97
QY
648 if (ecom->size == 0)
649 return XCALLOC(MTYPE_ECOMMUNITY_STR, 1);
650
651 /* ecom strlen + space + null term */
652 str_size = (ecom->size * (ECOMMUNITY_STRLEN + 1)) + 1;
653 str_buf = XCALLOC(MTYPE_ECOMMUNITY_STR, str_size);
d62a17ae 654
91085f97 655 char encbuf[128];
d62a17ae 656
657 for (i = 0; i < ecom->size; i++) {
658 int unk_ecom = 0;
91085f97 659 memset(encbuf, 0x00, sizeof(encbuf));
d62a17ae 660
661 /* Space between each value. */
91085f97
QY
662 if (i > 0)
663 strlcat(str_buf, " ", str_size);
d62a17ae 664
91085f97 665 /* Retrieve value field */
d62a17ae 666 pnt = ecom->val + (i * 8);
667
91085f97 668 /* High-order octet is the type */
d62a17ae 669 type = *pnt++;
670
671 if (type == ECOMMUNITY_ENCODE_AS || type == ECOMMUNITY_ENCODE_IP
672 || type == ECOMMUNITY_ENCODE_AS4) {
673 /* Low-order octet of type. */
674 sub_type = *pnt++;
675 if (sub_type != ECOMMUNITY_ROUTE_TARGET
2551b26e
PG
676 && sub_type != ECOMMUNITY_SITE_ORIGIN) {
677 if (sub_type ==
678 ECOMMUNITY_FLOWSPEC_REDIRECT_IPV4 &&
679 type == ECOMMUNITY_ENCODE_IP) {
680 struct in_addr *ipv4 =
681 (struct in_addr *)pnt;
682 char ipv4str[INET_ADDRSTRLEN];
683
684 inet_ntop(AF_INET, ipv4,
685 ipv4str,
686 INET_ADDRSTRLEN);
91085f97
QY
687 snprintf(encbuf, sizeof(encbuf),
688 "NH:%s:%d", ipv4str, pnt[5]);
2551b26e
PG
689 } else
690 unk_ecom = 1;
91085f97
QY
691 } else {
692 ecommunity_rt_soo_str(encbuf, sizeof(encbuf),
693 pnt, type, sub_type,
694 format);
695 }
d62a17ae 696 } else if (type == ECOMMUNITY_ENCODE_OPAQUE) {
697 if (filter == ECOMMUNITY_ROUTE_TARGET)
698 continue;
699 if (*pnt == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP) {
700 uint16_t tunneltype;
701 memcpy(&tunneltype, pnt + 5, 2);
702 tunneltype = ntohs(tunneltype);
91085f97
QY
703
704 snprintf(encbuf, sizeof(encbuf), "ET:%d",
705 tunneltype);
996c9314 706 } else if (*pnt == ECOMMUNITY_EVPN_SUBTYPE_DEF_GW) {
91085f97
QY
707 strlcpy(encbuf, "Default Gateway",
708 sizeof(encbuf));
709 } else {
d62a17ae 710 unk_ecom = 1;
91085f97 711 }
d62a17ae 712 } else if (type == ECOMMUNITY_ENCODE_EVPN) {
713 if (filter == ECOMMUNITY_ROUTE_TARGET)
714 continue;
bc59a672
MK
715 if (*pnt == ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC) {
716 struct ethaddr rmac;
d62a17ae 717 pnt++;
bc59a672 718 memcpy(&rmac, pnt, ETH_ALEN);
91085f97
QY
719
720 snprintf(encbuf, sizeof(encbuf),
721 "Rmac:%02x:%02x:%02x:%02x:%02x:%02x",
722 (uint8_t)rmac.octet[0],
723 (uint8_t)rmac.octet[1],
724 (uint8_t)rmac.octet[2],
725 (uint8_t)rmac.octet[3],
726 (uint8_t)rmac.octet[4],
727 (uint8_t)rmac.octet[5]);
d62a17ae 728 } else if (*pnt
729 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
d7c0a89a
QY
730 uint32_t seqnum;
731 uint8_t flags = *++pnt;
d62a17ae 732
733 memcpy(&seqnum, pnt + 2, 4);
734 seqnum = ntohl(seqnum);
91085f97
QY
735
736 snprintf(encbuf, sizeof(encbuf), "MM:%u",
737 seqnum);
738
739 if (CHECK_FLAG(
740 flags,
741 ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY))
742 strlcat(encbuf, ", sticky MAC",
743 sizeof(encbuf));
5cc359b2
CS
744 } else if (*pnt == ECOMMUNITY_EVPN_SUBTYPE_ND) {
745 uint8_t flags = *++pnt;
746
91085f97
QY
747 if (CHECK_FLAG(
748 flags,
749 ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG))
750 strlcpy(encbuf, "ND:Router Flag",
751 sizeof(encbuf));
d62a17ae 752 } else
753 unk_ecom = 1;
b72220fc
PG
754 } else if (type == ECOMMUNITY_ENCODE_REDIRECT_IP_NH) {
755 sub_type = *pnt++;
756 if (sub_type == ECOMMUNITY_REDIRECT_IP_NH) {
91085f97
QY
757 snprintf(encbuf, sizeof(encbuf),
758 "FS:redirect IP 0x%x", *(pnt + 5));
b72220fc
PG
759 } else
760 unk_ecom = 1;
1ef3c51f
PG
761 } else if (type == ECOMMUNITY_ENCODE_TRANS_EXP ||
762 type == ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 ||
763 type == ECOMMUNITY_EXTENDED_COMMUNITY_PART_3) {
a8d72b61 764 sub_type = *pnt++;
1ef3c51f 765 if (sub_type == ECOMMUNITY_REDIRECT_VRF) {
91085f97
QY
766 char buf[16] = {};
767 ecommunity_rt_soo_str(
768 buf, sizeof(buf), (uint8_t *)pnt,
769 type & ~ECOMMUNITY_ENCODE_TRANS_EXP,
770 ECOMMUNITY_ROUTE_TARGET,
771 ECOMMUNITY_FORMAT_DISPLAY);
772 snprintf(encbuf, sizeof(encbuf),
773 "FS:redirect VRF %s", buf);
1ef3c51f
PG
774 } else if (type != ECOMMUNITY_ENCODE_TRANS_EXP)
775 unk_ecom = 1;
776 else if (sub_type == ECOMMUNITY_TRAFFIC_ACTION) {
a8d72b61 777 char action[64];
a8d72b61
PG
778
779 if (*(pnt+3) ==
780 1 << FLOWSPEC_TRAFFIC_ACTION_TERMINAL)
91085f97
QY
781 strlcpy(action, "terminate (apply)",
782 sizeof(action));
a8d72b61 783 else
91085f97
QY
784 strlcpy(action, "eval stops",
785 sizeof(action));
786
a8d72b61
PG
787 if (*(pnt+3) ==
788 1 << FLOWSPEC_TRAFFIC_ACTION_SAMPLE)
91085f97
QY
789 strlcat(action, ", sample",
790 sizeof(action));
791
792
793 snprintf(encbuf, sizeof(encbuf), "FS:action %s",
794 action);
a8d72b61
PG
795 } else if (sub_type == ECOMMUNITY_TRAFFIC_RATE) {
796 union traffic_rate data;
797
798 data.rate_byte[3] = *(pnt+2);
799 data.rate_byte[2] = *(pnt+3);
800 data.rate_byte[1] = *(pnt+4);
801 data.rate_byte[0] = *(pnt+5);
91085f97
QY
802 snprintf(encbuf, sizeof(encbuf), "FS:rate %f",
803 data.rate_float);
a8d72b61 804 } else if (sub_type == ECOMMUNITY_TRAFFIC_MARKING) {
91085f97
QY
805 snprintf(encbuf, sizeof(encbuf),
806 "FS:marking %u", *(pnt + 5));
50f74cf1 807 } else if (*pnt
808 == ECOMMUNITY_EVPN_SUBTYPE_ES_IMPORT_RT) {
809 struct ethaddr mac;
2bb9eff4 810
50f74cf1 811 memcpy(&mac, pnt, ETH_ALEN);
91085f97
QY
812
813 snprintf(
814 encbuf, sizeof(encbuf),
50f74cf1 815 "ES-Import-Rt:%02x:%02x:%02x:%02x:%02x:%02x",
816 (uint8_t)mac.octet[0],
817 (uint8_t)mac.octet[1],
818 (uint8_t)mac.octet[2],
819 (uint8_t)mac.octet[3],
820 (uint8_t)mac.octet[4],
821 (uint8_t)mac.octet[5]);
a8d72b61
PG
822 } else
823 unk_ecom = 1;
1ef3c51f
PG
824 } else {
825 sub_type = *pnt++;
d62a17ae 826 unk_ecom = 1;
1ef3c51f 827 }
d62a17ae 828
829 if (unk_ecom)
91085f97
QY
830 snprintf(encbuf, sizeof(encbuf), "UNK:%d, %d", type,
831 sub_type);
d62a17ae 832
91085f97
QY
833 int r = strlcat(str_buf, encbuf, str_size);
834 assert(r < str_size);
94431dbc
C
835 }
836
d62a17ae 837 return str_buf;
718e3744 838}
4372df71 839
d62a17ae 840int ecommunity_match(const struct ecommunity *ecom1,
841 const struct ecommunity *ecom2)
4372df71 842{
d62a17ae 843 int i = 0;
844 int j = 0;
845
846 if (ecom1 == NULL && ecom2 == NULL)
847 return 1;
848
849 if (ecom1 == NULL || ecom2 == NULL)
850 return 0;
851
852 if (ecom1->size < ecom2->size)
853 return 0;
854
855 /* Every community on com2 needs to be on com1 for this to match */
856 while (i < ecom1->size && j < ecom2->size) {
857 if (memcmp(ecom1->val + i * ECOMMUNITY_SIZE,
858 ecom2->val + j * ECOMMUNITY_SIZE, ECOMMUNITY_SIZE)
859 == 0)
860 j++;
861 i++;
862 }
863
864 if (j == ecom2->size)
865 return 1;
866 else
867 return 0;
4372df71 868}
1e27ef50
PG
869
870/* return first occurence of type */
d62a17ae 871extern struct ecommunity_val *ecommunity_lookup(const struct ecommunity *ecom,
872 uint8_t type, uint8_t subtype)
1e27ef50 873{
d7c0a89a 874 uint8_t *p;
d62a17ae 875 int c;
876
877 /* If the value already exists in the structure return 0. */
878 c = 0;
879 for (p = ecom->val; c < ecom->size; p += ECOMMUNITY_SIZE, c++) {
880 if (p == NULL) {
881 continue;
882 }
883 if (p[0] == type && p[1] == subtype)
884 return (struct ecommunity_val *)p;
885 }
886 return NULL;
1e27ef50
PG
887}
888
889/* remove ext. community matching type and subtype
890 * return 1 on success ( removed ), 0 otherwise (not present)
891 */
d62a17ae 892extern int ecommunity_strip(struct ecommunity *ecom, uint8_t type,
893 uint8_t subtype)
1e27ef50 894{
003bc275 895 uint8_t *p, *q, *new;
d62a17ae 896 int c, found = 0;
897 /* When this is fist value, just add it. */
898 if (ecom == NULL || ecom->val == NULL) {
899 return 0;
900 }
901
003bc275 902 /* Check if any existing ext community matches. */
903 /* Certain extended communities like the Route Target can be present
904 * multiple times, handle that.
905 */
d62a17ae 906 c = 0;
907 for (p = ecom->val; c < ecom->size; p += ECOMMUNITY_SIZE, c++) {
003bc275 908 if (p[0] == type && p[1] == subtype)
909 found++;
d62a17ae 910 }
003bc275 911 /* If no matching ext community exists, return. */
d62a17ae 912 if (found == 0)
913 return 0;
003bc275 914
915 /* Handle the case where everything needs to be stripped. */
916 if (found == ecom->size) {
917 XFREE(MTYPE_ECOMMUNITY_VAL, ecom->val);
918 ecom->size = 0;
919 return 1;
920 }
921
922 /* Strip matching ext community(ies). */
923 new = XMALLOC(MTYPE_ECOMMUNITY_VAL,
924 (ecom->size - found) * ECOMMUNITY_SIZE);
925 q = new;
926 for (c = 0, p = ecom->val; c < ecom->size; c++, p += ECOMMUNITY_SIZE) {
927 if (!(p[0] == type && p[1] == subtype)) {
928 memcpy(q, p, ECOMMUNITY_SIZE);
929 q += ECOMMUNITY_SIZE;
930 }
931 }
932 XFREE(MTYPE_ECOMMUNITY_VAL, ecom->val);
933 ecom->val = new;
934 ecom->size -= found;
d62a17ae 935 return 1;
1e27ef50 936}
44338987 937
938/*
939 * Remove specified extended community value from extended community.
940 * Returns 1 if value was present (and hence, removed), 0 otherwise.
941 */
942int ecommunity_del_val(struct ecommunity *ecom, struct ecommunity_val *eval)
943{
f27f864e 944 uint8_t *p;
44338987 945 int c, found = 0;
946
947 /* Make sure specified value exists. */
948 if (ecom == NULL || ecom->val == NULL)
949 return 0;
950 c = 0;
951 for (p = ecom->val; c < ecom->size; p += ECOMMUNITY_SIZE, c++) {
952 if (!memcmp(p, eval->val, ECOMMUNITY_SIZE)) {
953 found = 1;
954 break;
955 }
956 }
957 if (found == 0)
958 return 0;
959
960 /* Delete the selected value */
961 ecom->size--;
962 p = XMALLOC(MTYPE_ECOMMUNITY_VAL, ecom->size * ECOMMUNITY_SIZE);
963 if (c != 0)
964 memcpy(p, ecom->val, c * ECOMMUNITY_SIZE);
965 if ((ecom->size - c) != 0)
966 memcpy(p + (c)*ECOMMUNITY_SIZE,
967 ecom->val + (c + 1) * ECOMMUNITY_SIZE,
968 (ecom->size - c) * ECOMMUNITY_SIZE);
e6a6870b 969 XFREE(MTYPE_ECOMMUNITY_VAL, ecom->val);
44338987 970 ecom->val = p;
971 return 1;
972}
dacf6ec1
PG
973
974int ecommunity_fill_pbr_action(struct ecommunity_val *ecom_eval,
975 struct bgp_pbr_entry_action *api)
976{
977 if (ecom_eval->val[1] == ECOMMUNITY_TRAFFIC_RATE) {
978 api->action = ACTION_TRAFFICRATE;
979 api->u.r.rate_info[3] = ecom_eval->val[4];
980 api->u.r.rate_info[2] = ecom_eval->val[5];
981 api->u.r.rate_info[1] = ecom_eval->val[6];
982 api->u.r.rate_info[0] = ecom_eval->val[7];
983 } else if (ecom_eval->val[1] == ECOMMUNITY_TRAFFIC_ACTION) {
984 api->action = ACTION_TRAFFIC_ACTION;
985 /* else distribute code is set by default */
986 if (ecom_eval->val[5] & (1 << FLOWSPEC_TRAFFIC_ACTION_TERMINAL))
987 api->u.za.filter |= TRAFFIC_ACTION_TERMINATE;
988 else
989 api->u.za.filter |= TRAFFIC_ACTION_DISTRIBUTE;
990 if (ecom_eval->val[5] == 1 << FLOWSPEC_TRAFFIC_ACTION_SAMPLE)
991 api->u.za.filter |= TRAFFIC_ACTION_SAMPLE;
992
993 } else if (ecom_eval->val[1] == ECOMMUNITY_TRAFFIC_MARKING) {
994 api->action = ACTION_MARKING;
995 api->u.marking_dscp = ecom_eval->val[7];
996 } else if (ecom_eval->val[1] == ECOMMUNITY_REDIRECT_VRF) {
997 /* must use external function */
998 return 0;
999 } else if (ecom_eval->val[1] == ECOMMUNITY_REDIRECT_IP_NH) {
1000 /* see draft-ietf-idr-flowspec-redirect-ip-02
1001 * Q1: how come a ext. community can host ipv6 address
1002 * Q2 : from cisco documentation:
1003 * Announces the reachability of one or more flowspec NLRI.
1004 * When a BGP speaker receives an UPDATE message with the
1005 * redirect-to-IP extended community, it is expected to
1006 * create a traffic filtering rule for every flow-spec
1007 * NLRI in the message that has this path as its best
1008 * path. The filter entry matches the IP packets
1009 * described in the NLRI field and redirects them or
1010 * copies them towards the IPv4 or IPv6 address specified
1011 * in the 'Network Address of Next- Hop'
1012 * field of the associated MP_REACH_NLRI.
1013 */
1014 struct ecommunity_ip *ip_ecom = (struct ecommunity_ip *)
1015 ecom_eval + 2;
1016
1017 api->u.zr.redirect_ip_v4 = ip_ecom->ip;
1018 } else
1019 return -1;
1020 return 0;
1021}
5b820d9e
NT
1022
1023static struct ecommunity *bgp_aggr_ecommunity_lookup(
1024 struct bgp_aggregate *aggregate,
1025 struct ecommunity *ecommunity)
1026{
1027 return hash_lookup(aggregate->ecommunity_hash, ecommunity);
1028}
1029
1030static void *bgp_aggr_ecommunty_hash_alloc(void *p)
1031{
1032 struct ecommunity *ref = (struct ecommunity *)p;
1033 struct ecommunity *ecommunity = NULL;
1034
1035 ecommunity = ecommunity_dup(ref);
1036 return ecommunity;
1037}
1038
7f5818fb 1039static void bgp_aggr_ecommunity_prepare(struct hash_bucket *hb, void *arg)
5b820d9e 1040{
5b820d9e
NT
1041 struct ecommunity *hb_ecommunity = hb->data;
1042 struct ecommunity **aggr_ecommunity = arg;
1043
4edd83f9 1044 if (*aggr_ecommunity)
1045 *aggr_ecommunity = ecommunity_merge(*aggr_ecommunity,
1046 hb_ecommunity);
1047 else
5b820d9e
NT
1048 *aggr_ecommunity = ecommunity_dup(hb_ecommunity);
1049}
1050
1051void bgp_aggr_ecommunity_remove(void *arg)
1052{
1053 struct ecommunity *ecommunity = arg;
1054
1055 ecommunity_free(&ecommunity);
1056}
1057
1058void bgp_compute_aggregate_ecommunity(struct bgp_aggregate *aggregate,
1059 struct ecommunity *ecommunity)
4edd83f9 1060{
1061 bgp_compute_aggregate_ecommunity_hash(aggregate, ecommunity);
1062 bgp_compute_aggregate_ecommunity_val(aggregate);
1063}
1064
1065
1066void bgp_compute_aggregate_ecommunity_hash(struct bgp_aggregate *aggregate,
1067 struct ecommunity *ecommunity)
5b820d9e
NT
1068{
1069 struct ecommunity *aggr_ecommunity = NULL;
1070
1071 if ((aggregate == NULL) || (ecommunity == NULL))
1072 return;
1073
1074 /* Create hash if not already created.
1075 */
1076 if (aggregate->ecommunity_hash == NULL)
1077 aggregate->ecommunity_hash = hash_create(
1078 ecommunity_hash_make, ecommunity_cmp,
1079 "BGP Aggregator ecommunity hash");
1080
1081 aggr_ecommunity = bgp_aggr_ecommunity_lookup(aggregate, ecommunity);
1082 if (aggr_ecommunity == NULL) {
1083 /* Insert ecommunity into hash.
1084 */
1085 aggr_ecommunity = hash_get(aggregate->ecommunity_hash,
1086 ecommunity,
1087 bgp_aggr_ecommunty_hash_alloc);
4edd83f9 1088 }
5b820d9e 1089
4edd83f9 1090 /* Increment reference counter.
1091 */
1092 aggr_ecommunity->refcnt++;
1093}
5b820d9e 1094
4edd83f9 1095void bgp_compute_aggregate_ecommunity_val(struct bgp_aggregate *aggregate)
1096{
1097 struct ecommunity *ecommerge = NULL;
1098
1099 if (aggregate == NULL)
1100 return;
1101
1102 /* Re-compute aggregate's ecommunity.
1103 */
1104 if (aggregate->ecommunity)
1105 ecommunity_free(&aggregate->ecommunity);
1106 if (aggregate->ecommunity_hash
1107 && aggregate->ecommunity_hash->count) {
5b820d9e
NT
1108 hash_iterate(aggregate->ecommunity_hash,
1109 bgp_aggr_ecommunity_prepare,
1110 &aggregate->ecommunity);
4edd83f9 1111 ecommerge = aggregate->ecommunity;
1112 aggregate->ecommunity = ecommunity_uniq_sort(ecommerge);
1113 if (ecommerge)
1114 ecommunity_free(&ecommerge);
5b820d9e 1115 }
5b820d9e
NT
1116}
1117
1118void bgp_remove_ecommunity_from_aggregate(struct bgp_aggregate *aggregate,
1119 struct ecommunity *ecommunity)
1120{
1121 struct ecommunity *aggr_ecommunity = NULL;
1122 struct ecommunity *ret_ecomm = NULL;
1123
4edd83f9 1124 if ((!aggregate)
1125 || (!aggregate->ecommunity_hash)
1126 || (!ecommunity))
5b820d9e
NT
1127 return;
1128
1129 /* Look-up the ecommunity in the hash.
1130 */
1131 aggr_ecommunity = bgp_aggr_ecommunity_lookup(aggregate, ecommunity);
1132 if (aggr_ecommunity) {
1133 aggr_ecommunity->refcnt--;
1134
1135 if (aggr_ecommunity->refcnt == 0) {
1136 ret_ecomm = hash_release(aggregate->ecommunity_hash,
1137 aggr_ecommunity);
1138 ecommunity_free(&ret_ecomm);
4edd83f9 1139 bgp_compute_aggregate_ecommunity_val(aggregate);
1140 }
1141 }
1142}
1143
1144void bgp_remove_ecomm_from_aggregate_hash(struct bgp_aggregate *aggregate,
1145 struct ecommunity *ecommunity)
1146{
1147
1148 struct ecommunity *aggr_ecommunity = NULL;
1149 struct ecommunity *ret_ecomm = NULL;
5b820d9e 1150
4edd83f9 1151 if ((!aggregate)
1152 || (!aggregate->ecommunity_hash)
1153 || (!ecommunity))
1154 return;
5b820d9e 1155
4edd83f9 1156 /* Look-up the ecommunity in the hash.
1157 */
1158 aggr_ecommunity = bgp_aggr_ecommunity_lookup(aggregate, ecommunity);
1159 if (aggr_ecommunity) {
1160 aggr_ecommunity->refcnt--;
1161
1162 if (aggr_ecommunity->refcnt == 0) {
1163 ret_ecomm = hash_release(aggregate->ecommunity_hash,
1164 aggr_ecommunity);
1165 ecommunity_free(&ret_ecomm);
5b820d9e
NT
1166 }
1167 }
1168}