bgp_ecommunity.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* BGP Extended Communities Attribute
  2. Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "hash.h"
  18. #include "memory.h"
  19. #include "prefix.h"
  20. #include "command.h"
  21. #include "bgpd/bgpd.h"
  22. #include "bgpd/bgp_ecommunity.h"
  23. /* Hash of community attribute. */
  24. struct hash *ecomhash;
  25. /* Allocate a new ecommunities. */
  26. struct ecommunity *
  27. ecommunity_new ()
  28. {
  29. return (struct ecommunity *) XCALLOC (MTYPE_ECOMMUNITY,
  30. sizeof (struct ecommunity));
  31. }
  32. /* Allocate ecommunities. */
  33. void
  34. ecommunity_free (struct ecommunity *ecom)
  35. {
  36. if (ecom->val)
  37. XFREE (MTYPE_ECOMMUNITY_VAL, ecom->val);
  38. if (ecom->str)
  39. XFREE (MTYPE_ECOMMUNITY_STR, ecom->str);
  40. XFREE (MTYPE_ECOMMUNITY, ecom);
  41. }
  42. /* Add a new Extended Communities value to Extended Communities
  43. Attribute structure. When the value is already exists in the
  44. structure, we don't add the value. Newly added value is sorted by
  45. numerical order. When the value is added to the structure return 1
  46. else return 0. */
  47. static int
  48. ecommunity_add_val (struct ecommunity *ecom, struct ecommunity_val *eval)
  49. {
  50. u_int8_t *p;
  51. int ret;
  52. int c;
  53. /* When this is fist value, just add it. */
  54. if (ecom->val == NULL)
  55. {
  56. ecom->size++;
  57. ecom->val = XMALLOC (MTYPE_ECOMMUNITY_VAL, ecom_length (ecom));
  58. memcpy (ecom->val, eval->val, ECOMMUNITY_SIZE);
  59. return 1;
  60. }
  61. /* If the value already exists in the structure return 0. */
  62. c = 0;
  63. for (p = ecom->val; c < ecom->size; p += ECOMMUNITY_SIZE, c++)
  64. {
  65. ret = memcmp (p, eval->val, ECOMMUNITY_SIZE);
  66. if (ret == 0)
  67. return 0;
  68. if (ret > 0)
  69. break;
  70. }
  71. /* Add the value to the structure with numerical sorting. */
  72. ecom->size++;
  73. ecom->val = XREALLOC (MTYPE_ECOMMUNITY_VAL, ecom->val, ecom_length (ecom));
  74. memmove (ecom->val + (c + 1) * ECOMMUNITY_SIZE,
  75. ecom->val + c * ECOMMUNITY_SIZE,
  76. (ecom->size - 1 - c) * ECOMMUNITY_SIZE);
  77. memcpy (ecom->val + c * ECOMMUNITY_SIZE, eval->val, ECOMMUNITY_SIZE);
  78. return 1;
  79. }
  80. /* This function takes pointer to Extended Communites strucutre then
  81. create a new Extended Communities structure by uniq and sort each
  82. Exteneded Communities value. */
  83. static struct ecommunity *
  84. ecommunity_uniq_sort (struct ecommunity *ecom)
  85. {
  86. int i;
  87. struct ecommunity *new;
  88. struct ecommunity_val *eval;
  89. if (! ecom)
  90. return NULL;
  91. new = ecommunity_new ();;
  92. for (i = 0; i < ecom->size; i++)
  93. {
  94. eval = (struct ecommunity_val *) (ecom->val + (i * ECOMMUNITY_SIZE));
  95. ecommunity_add_val (new, eval);
  96. }
  97. return new;
  98. }
  99. /* Parse Extended Communites Attribute in BGP packet. */
  100. struct ecommunity *
  101. ecommunity_parse (u_int8_t *pnt, u_short length)
  102. {
  103. struct ecommunity tmp;
  104. struct ecommunity *new;
  105. /* Length check. */
  106. if (length % ECOMMUNITY_SIZE)
  107. return NULL;
  108. /* Prepare tmporary structure for making a new Extended Communities
  109. Attribute. */
  110. tmp.size = length / ECOMMUNITY_SIZE;
  111. tmp.val = pnt;
  112. /* Create a new Extended Communities Attribute by uniq and sort each
  113. Extended Communities value */
  114. new = ecommunity_uniq_sort (&tmp);
  115. return ecommunity_intern (new);
  116. }
  117. /* Duplicate the Extended Communities Attribute structure. */
  118. struct ecommunity *
  119. ecommunity_dup (struct ecommunity *ecom)
  120. {
  121. struct ecommunity *new;
  122. new = XCALLOC (MTYPE_ECOMMUNITY, sizeof (struct ecommunity));
  123. new->size = ecom->size;
  124. if (new->size)
  125. {
  126. new->val = XMALLOC (MTYPE_ECOMMUNITY_VAL, ecom->size * ECOMMUNITY_SIZE);
  127. memcpy (new->val, ecom->val, ecom->size * ECOMMUNITY_SIZE);
  128. }
  129. else
  130. new->val = NULL;
  131. return new;
  132. }
  133. /* Retrun string representation of communities attribute. */
  134. char *
  135. ecommunity_str (struct ecommunity *ecom)
  136. {
  137. if (! ecom->str)
  138. ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
  139. return ecom->str;
  140. }
  141. /* Merge two Extended Communities Attribute structure. */
  142. struct ecommunity *
  143. ecommunity_merge (struct ecommunity *ecom1, struct ecommunity *ecom2)
  144. {
  145. if (ecom1->val)
  146. ecom1->val = XREALLOC (MTYPE_ECOMMUNITY_VAL, ecom1->val,
  147. (ecom1->size + ecom2->size) * ECOMMUNITY_SIZE);
  148. else
  149. ecom1->val = XMALLOC (MTYPE_ECOMMUNITY_VAL,
  150. (ecom1->size + ecom2->size) * ECOMMUNITY_SIZE);
  151. memcpy (ecom1->val + (ecom1->size * ECOMMUNITY_SIZE),
  152. ecom2->val, ecom2->size * ECOMMUNITY_SIZE);
  153. ecom1->size += ecom2->size;
  154. return ecom1;
  155. }
  156. /* Intern Extended Communities Attribute. */
  157. struct ecommunity *
  158. ecommunity_intern (struct ecommunity *ecom)
  159. {
  160. struct ecommunity *find;
  161. assert (ecom->refcnt == 0);
  162. find = (struct ecommunity *) hash_get (ecomhash, ecom, hash_alloc_intern);
  163. if (find != ecom)
  164. ecommunity_free (ecom);
  165. find->refcnt++;
  166. if (! find->str)
  167. find->str = ecommunity_ecom2str (find, ECOMMUNITY_FORMAT_DISPLAY);
  168. return find;
  169. }
  170. /* Unintern Extended Communities Attribute. */
  171. void
  172. ecommunity_unintern (struct ecommunity *ecom)
  173. {
  174. struct ecommunity *ret;
  175. if (ecom->refcnt)
  176. ecom->refcnt--;
  177. /* Pull off from hash. */
  178. if (ecom->refcnt == 0)
  179. {
  180. /* Extended community must be in the hash. */
  181. ret = (struct ecommunity *) hash_release (ecomhash, ecom);
  182. assert (ret != NULL);
  183. ecommunity_free (ecom);
  184. }
  185. }
  186. /* Utinity function to make hash key. */
  187. unsigned int
  188. ecommunity_hash_make (struct ecommunity *ecom)
  189. {
  190. int c;
  191. unsigned int key;
  192. u_int8_t *pnt;
  193. key = 0;
  194. pnt = ecom->val;
  195. for (c = 0; c < ecom->size * ECOMMUNITY_SIZE; c++)
  196. key += pnt[c];
  197. return key;
  198. }
  199. /* Compare two Extended Communities Attribute structure. */
  200. int
  201. ecommunity_cmp (const struct ecommunity *ecom1,
  202. const struct ecommunity *ecom2)
  203. {
  204. if (ecom1->size == ecom2->size
  205. && memcmp (ecom1->val, ecom2->val, ecom1->size * ECOMMUNITY_SIZE) == 0)
  206. return 1;
  207. return 0;
  208. }
  209. /* Initialize Extended Comminities related hash. */
  210. void
  211. ecommunity_init ()
  212. {
  213. ecomhash = hash_create (ecommunity_hash_make, ecommunity_cmp);
  214. }
  215. /* Extended Communities token enum. */
  216. enum ecommunity_token
  217. {
  218. ecommunity_token_rt,
  219. ecommunity_token_soo,
  220. ecommunity_token_val,
  221. ecommunity_token_unknown
  222. };
  223. /* Get next Extended Communities token from the string. */
  224. static const char *
  225. ecommunity_gettoken (const char *str, struct ecommunity_val *eval,
  226. enum ecommunity_token *token)
  227. {
  228. int ret;
  229. int dot = 0;
  230. int digit = 0;
  231. int separator = 0;
  232. u_int32_t val_low = 0;
  233. u_int32_t val_high = 0;
  234. const char *p = str;
  235. struct in_addr ip;
  236. char ipstr[INET_ADDRSTRLEN + 1];
  237. /* Skip white space. */
  238. while (isspace ((int) *p))
  239. {
  240. p++;
  241. str++;
  242. }
  243. /* Check the end of the line. */
  244. if (*p == '\0')
  245. return NULL;
  246. /* "rt" and "soo" keyword parse. */
  247. if (! isdigit ((int) *p))
  248. {
  249. /* "rt" match check. */
  250. if (tolower ((int) *p) == 'r')
  251. {
  252. p++;
  253. if (tolower ((int) *p) == 't')
  254. {
  255. p++;
  256. *token = ecommunity_token_rt;
  257. return p;
  258. }
  259. if (isspace ((int) *p) || *p == '\0')
  260. {
  261. *token = ecommunity_token_rt;
  262. return p;
  263. }
  264. goto error;
  265. }
  266. /* "soo" match check. */
  267. else if (tolower ((int) *p) == 's')
  268. {
  269. p++;
  270. if (tolower ((int) *p) == 'o')
  271. {
  272. p++;
  273. if (tolower ((int) *p) == 'o')
  274. {
  275. p++;
  276. *token = ecommunity_token_soo;
  277. return p;
  278. }
  279. if (isspace ((int) *p) || *p == '\0')
  280. {
  281. *token = ecommunity_token_soo;
  282. return p;
  283. }
  284. goto error;
  285. }
  286. if (isspace ((int) *p) || *p == '\0')
  287. {
  288. *token = ecommunity_token_soo;
  289. return p;
  290. }
  291. goto error;
  292. }
  293. goto error;
  294. }
  295. while (isdigit ((int) *p) || *p == ':' || *p == '.')
  296. {
  297. if (*p == ':')
  298. {
  299. if (separator)
  300. goto error;
  301. separator = 1;
  302. digit = 0;
  303. if (dot)
  304. {
  305. if ((p - str) > INET_ADDRSTRLEN)
  306. goto error;
  307. memset (ipstr, 0, INET_ADDRSTRLEN + 1);
  308. memcpy (ipstr, str, p - str);
  309. ret = inet_aton (ipstr, &ip);
  310. if (ret == 0)
  311. goto error;
  312. }
  313. else
  314. val_high = val_low;
  315. val_low = 0;
  316. }
  317. else if (*p == '.')
  318. {
  319. if (separator)
  320. goto error;
  321. dot++;
  322. if (dot > 4)
  323. goto error;
  324. }
  325. else
  326. {
  327. digit = 1;
  328. val_low *= 10;
  329. val_low += (*p - '0');
  330. }
  331. p++;
  332. }
  333. /* Low digit part must be there. */
  334. if (! digit || ! separator)
  335. goto error;
  336. /* Encode result into routing distinguisher. */
  337. if (dot)
  338. {
  339. eval->val[0] = ECOMMUNITY_ENCODE_IP;
  340. eval->val[1] = 0;
  341. memcpy (&eval->val[2], &ip, sizeof (struct in_addr));
  342. eval->val[6] = (val_low >> 8) & 0xff;
  343. eval->val[7] = val_low & 0xff;
  344. }
  345. else
  346. {
  347. eval->val[0] = ECOMMUNITY_ENCODE_AS;
  348. eval->val[1] = 0;
  349. eval->val[2] = (val_high >>8) & 0xff;
  350. eval->val[3] = val_high & 0xff;
  351. eval->val[4] = (val_low >>24) & 0xff;
  352. eval->val[5] = (val_low >>16) & 0xff;
  353. eval->val[6] = (val_low >>8) & 0xff;
  354. eval->val[7] = val_low & 0xff;
  355. }
  356. *token = ecommunity_token_val;
  357. return p;
  358. error:
  359. *token = ecommunity_token_unknown;
  360. return p;
  361. }
  362. /* Convert string to extended community attribute.
  363. When type is already known, please specify both str and type. str
  364. should not include keyword such as "rt" and "soo". Type is
  365. ECOMMUNITY_ROUTE_TARGET or ECOMMUNITY_SITE_ORIGIN.
  366. keyword_included should be zero.
  367. For example route-map's "set extcommunity" command case:
  368. "rt 100:1 100:2 100:3" -> str = "100:1 100:2 100:3"
  369. type = ECOMMUNITY_ROUTE_TARGET
  370. keyword_included = 0
  371. "soo 100:1" -> str = "100:1"
  372. type = ECOMMUNITY_SITE_ORIGIN
  373. keyword_included = 0
  374. When string includes keyword for each extended community value.
  375. Please specify keyword_included as non-zero value.
  376. For example standard extcommunity-list case:
  377. "rt 100:1 rt 100:2 soo 100:1" -> str = "rt 100:1 rt 100:2 soo 100:1"
  378. type = 0
  379. keyword_include = 1
  380. */
  381. struct ecommunity *
  382. ecommunity_str2com (const char *str, int type, int keyword_included)
  383. {
  384. struct ecommunity *ecom = NULL;
  385. enum ecommunity_token token;
  386. struct ecommunity_val eval;
  387. int keyword = 0;
  388. while ((str = ecommunity_gettoken (str, &eval, &token)))
  389. {
  390. switch (token)
  391. {
  392. case ecommunity_token_rt:
  393. case ecommunity_token_soo:
  394. if (! keyword_included || keyword)
  395. {
  396. if (ecom)
  397. ecommunity_free (ecom);
  398. return NULL;
  399. }
  400. keyword = 1;
  401. if (token == ecommunity_token_rt)
  402. {
  403. type = ECOMMUNITY_ROUTE_TARGET;
  404. }
  405. if (token == ecommunity_token_soo)
  406. {
  407. type = ECOMMUNITY_SITE_ORIGIN;
  408. }
  409. break;
  410. case ecommunity_token_val:
  411. if (keyword_included)
  412. {
  413. if (! keyword)
  414. {
  415. if (ecom)
  416. ecommunity_free (ecom);
  417. return NULL;
  418. }
  419. keyword = 0;
  420. }
  421. if (ecom == NULL)
  422. ecom = ecommunity_new ();
  423. eval.val[1] = type;
  424. ecommunity_add_val (ecom, &eval);
  425. break;
  426. case ecommunity_token_unknown:
  427. default:
  428. if (ecom)
  429. ecommunity_free (ecom);
  430. return NULL;
  431. break;
  432. }
  433. }
  434. return ecom;
  435. }
  436. /* Convert extended community attribute to string.
  437. Due to historical reason of industry standard implementation, there
  438. are three types of format.
  439. route-map set extcommunity format
  440. "rt 100:1 100:2"
  441. "soo 100:3"
  442. extcommunity-list
  443. "rt 100:1 rt 100:2 soo 100:3"
  444. "show ip bgp" and extcommunity-list regular expression matching
  445. "RT:100:1 RT:100:2 SoO:100:3"
  446. For each formath please use below definition for format:
  447. ECOMMUNITY_FORMAT_ROUTE_MAP
  448. ECOMMUNITY_FORMAT_COMMUNITY_LIST
  449. ECOMMUNITY_FORMAT_DISPLAY
  450. */
  451. char *
  452. ecommunity_ecom2str (struct ecommunity *ecom, int format)
  453. {
  454. int i;
  455. u_int8_t *pnt;
  456. int encode = 0;
  457. int type = 0;
  458. #define ECOMMUNITY_STR_DEFAULT_LEN 26
  459. int str_size;
  460. int str_pnt;
  461. char *str_buf;
  462. const char *prefix;
  463. int len = 0;
  464. int first = 1;
  465. /* For parse Extended Community attribute tupple. */
  466. struct ecommunity_as
  467. {
  468. as_t as;
  469. u_int32_t val;
  470. } eas;
  471. struct ecommunity_ip
  472. {
  473. struct in_addr ip;
  474. u_int16_t val;
  475. } eip;
  476. if (ecom->size == 0)
  477. {
  478. str_buf = XMALLOC (MTYPE_ECOMMUNITY_STR, 1);
  479. str_buf[0] = '\0';
  480. return str_buf;
  481. }
  482. /* Prepare buffer. */
  483. str_buf = XMALLOC (MTYPE_ECOMMUNITY_STR, ECOMMUNITY_STR_DEFAULT_LEN + 1);
  484. str_size = ECOMMUNITY_STR_DEFAULT_LEN + 1;
  485. str_pnt = 0;
  486. for (i = 0; i < ecom->size; i++)
  487. {
  488. /* Space between each value. */
  489. if (! first)
  490. str_buf[str_pnt++] = ' ';
  491. pnt = ecom->val + (i * 8);
  492. /* High-order octet of type. */
  493. encode = *pnt++;
  494. if (encode != ECOMMUNITY_ENCODE_AS && encode != ECOMMUNITY_ENCODE_IP)
  495. {
  496. len = sprintf (str_buf + str_pnt, "?");
  497. str_pnt += len;
  498. first = 0;
  499. continue;
  500. }
  501. /* Low-order octet of type. */
  502. type = *pnt++;
  503. if (type != ECOMMUNITY_ROUTE_TARGET && type != ECOMMUNITY_SITE_ORIGIN)
  504. {
  505. len = sprintf (str_buf + str_pnt, "?");
  506. str_pnt += len;
  507. first = 0;
  508. continue;
  509. }
  510. switch (format)
  511. {
  512. case ECOMMUNITY_FORMAT_COMMUNITY_LIST:
  513. prefix = (type == ECOMMUNITY_ROUTE_TARGET ? "rt " : "soo ");
  514. break;
  515. case ECOMMUNITY_FORMAT_DISPLAY:
  516. prefix = (type == ECOMMUNITY_ROUTE_TARGET ? "RT:" : "SoO:");
  517. break;
  518. case ECOMMUNITY_FORMAT_ROUTE_MAP:
  519. prefix = "";
  520. break;
  521. default:
  522. prefix = "";
  523. break;
  524. }
  525. /* Make it sure size is enough. */
  526. while (str_pnt + ECOMMUNITY_STR_DEFAULT_LEN >= str_size)
  527. {
  528. str_size *= 2;
  529. str_buf = XREALLOC (MTYPE_ECOMMUNITY_STR, str_buf, str_size);
  530. }
  531. /* Put string into buffer. */
  532. if (encode == ECOMMUNITY_ENCODE_AS)
  533. {
  534. eas.as = (*pnt++ << 8);
  535. eas.as |= (*pnt++);
  536. eas.val = (*pnt++ << 24);
  537. eas.val |= (*pnt++ << 16);
  538. eas.val |= (*pnt++ << 8);
  539. eas.val |= (*pnt++);
  540. len = sprintf (str_buf + str_pnt, "%s%d:%d", prefix,
  541. eas.as, eas.val);
  542. str_pnt += len;
  543. first = 0;
  544. }
  545. else if (encode == ECOMMUNITY_ENCODE_IP)
  546. {
  547. memcpy (&eip.ip, pnt, 4);
  548. pnt += 4;
  549. eip.val = (*pnt++ << 8);
  550. eip.val |= (*pnt++);
  551. len = sprintf (str_buf + str_pnt, "%s%s:%d", prefix,
  552. inet_ntoa (eip.ip), eip.val);
  553. str_pnt += len;
  554. first = 0;
  555. }
  556. }
  557. return str_buf;
  558. }
  559. int
  560. ecommunity_match (const struct ecommunity *ecom1,
  561. const struct ecommunity *ecom2)
  562. {
  563. int i = 0;
  564. int j = 0;
  565. if (ecom1 == NULL && ecom2 == NULL)
  566. return 1;
  567. if (ecom1 == NULL || ecom2 == NULL)
  568. return 0;
  569. if (ecom1->size < ecom2->size)
  570. return 0;
  571. /* Every community on com2 needs to be on com1 for this to match */
  572. while (i < ecom1->size && j < ecom2->size)
  573. {
  574. if (memcmp (ecom1->val + i, ecom2->val + j, ECOMMUNITY_SIZE) == 0)
  575. j++;
  576. i++;
  577. }
  578. if (j == ecom2->size)
  579. return 1;
  580. else
  581. return 0;
  582. }