bgp_ecommunity.c 18 KB

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