bgp_clist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /* BGP community-list and extcommunity-list.
  2. Copyright (C) 1999 Kunihiro Ishiguro
  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 "command.h"
  18. #include "prefix.h"
  19. #include "memory.h"
  20. #include "filter.h"
  21. #include "bgpd/bgpd.h"
  22. #include "bgpd/bgp_community.h"
  23. #include "bgpd/bgp_ecommunity.h"
  24. #include "bgpd/bgp_aspath.h"
  25. #include "bgpd/bgp_regex.h"
  26. #include "bgpd/bgp_clist.h"
  27. /* Lookup master structure for community-list or
  28. extcommunity-list. */
  29. struct community_list_master *
  30. community_list_master_lookup (struct community_list_handler *ch, int master)
  31. {
  32. if (ch)
  33. switch (master)
  34. {
  35. case COMMUNITY_LIST_MASTER:
  36. return &ch->community_list;
  37. case EXTCOMMUNITY_LIST_MASTER:
  38. return &ch->extcommunity_list;
  39. }
  40. return NULL;
  41. }
  42. /* Allocate a new community list entry. */
  43. static struct community_entry *
  44. community_entry_new (void)
  45. {
  46. return XCALLOC (MTYPE_COMMUNITY_LIST_ENTRY, sizeof (struct community_entry));
  47. }
  48. /* Free community list entry. */
  49. static void
  50. community_entry_free (struct community_entry *entry)
  51. {
  52. switch (entry->style)
  53. {
  54. case COMMUNITY_LIST_STANDARD:
  55. if (entry->u.com)
  56. community_free (entry->u.com);
  57. break;
  58. case EXTCOMMUNITY_LIST_STANDARD:
  59. /* In case of standard extcommunity-list, configuration string
  60. is made by ecommunity_ecom2str(). */
  61. if (entry->config)
  62. XFREE (MTYPE_ECOMMUNITY_STR, entry->config);
  63. if (entry->u.ecom)
  64. ecommunity_free (&entry->u.ecom);
  65. break;
  66. case COMMUNITY_LIST_EXPANDED:
  67. case EXTCOMMUNITY_LIST_EXPANDED:
  68. if (entry->config)
  69. XFREE (MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
  70. if (entry->reg)
  71. bgp_regex_free (entry->reg);
  72. default:
  73. break;
  74. }
  75. XFREE (MTYPE_COMMUNITY_LIST_ENTRY, entry);
  76. }
  77. /* Allocate a new community-list. */
  78. static struct community_list *
  79. community_list_new (void)
  80. {
  81. return XCALLOC (MTYPE_COMMUNITY_LIST, sizeof (struct community_list));
  82. }
  83. /* Free community-list. */
  84. static void
  85. community_list_free (struct community_list *list)
  86. {
  87. if (list->name)
  88. XFREE (MTYPE_COMMUNITY_LIST_NAME, list->name);
  89. XFREE (MTYPE_COMMUNITY_LIST, list);
  90. }
  91. static struct community_list *
  92. community_list_insert (struct community_list_handler *ch,
  93. const char *name, int master)
  94. {
  95. size_t i;
  96. long number;
  97. struct community_list *new;
  98. struct community_list *point;
  99. struct community_list_list *list;
  100. struct community_list_master *cm;
  101. /* Lookup community-list master. */
  102. cm = community_list_master_lookup (ch, master);
  103. if (!cm)
  104. return NULL;
  105. /* Allocate new community_list and copy given name. */
  106. new = community_list_new ();
  107. new->name = XSTRDUP (MTYPE_COMMUNITY_LIST_NAME, name);
  108. /* If name is made by all digit character. We treat it as
  109. number. */
  110. for (number = 0, i = 0; i < strlen (name); i++)
  111. {
  112. if (isdigit ((int) name[i]))
  113. number = (number * 10) + (name[i] - '0');
  114. else
  115. break;
  116. }
  117. /* In case of name is all digit character */
  118. if (i == strlen (name))
  119. {
  120. new->sort = COMMUNITY_LIST_NUMBER;
  121. /* Set access_list to number list. */
  122. list = &cm->num;
  123. for (point = list->head; point; point = point->next)
  124. if (atol (point->name) >= number)
  125. break;
  126. }
  127. else
  128. {
  129. new->sort = COMMUNITY_LIST_STRING;
  130. /* Set access_list to string list. */
  131. list = &cm->str;
  132. /* Set point to insertion point. */
  133. for (point = list->head; point; point = point->next)
  134. if (strcmp (point->name, name) >= 0)
  135. break;
  136. }
  137. /* Link to upper list. */
  138. new->parent = list;
  139. /* In case of this is the first element of master. */
  140. if (list->head == NULL)
  141. {
  142. list->head = list->tail = new;
  143. return new;
  144. }
  145. /* In case of insertion is made at the tail of access_list. */
  146. if (point == NULL)
  147. {
  148. new->prev = list->tail;
  149. list->tail->next = new;
  150. list->tail = new;
  151. return new;
  152. }
  153. /* In case of insertion is made at the head of access_list. */
  154. if (point == list->head)
  155. {
  156. new->next = list->head;
  157. list->head->prev = new;
  158. list->head = new;
  159. return new;
  160. }
  161. /* Insertion is made at middle of the access_list. */
  162. new->next = point;
  163. new->prev = point->prev;
  164. if (point->prev)
  165. point->prev->next = new;
  166. point->prev = new;
  167. return new;
  168. }
  169. struct community_list *
  170. community_list_lookup (struct community_list_handler *ch,
  171. const char *name, int master)
  172. {
  173. struct community_list *list;
  174. struct community_list_master *cm;
  175. if (!name)
  176. return NULL;
  177. cm = community_list_master_lookup (ch, master);
  178. if (!cm)
  179. return NULL;
  180. for (list = cm->num.head; list; list = list->next)
  181. if (strcmp (list->name, name) == 0)
  182. return list;
  183. for (list = cm->str.head; list; list = list->next)
  184. if (strcmp (list->name, name) == 0)
  185. return list;
  186. return NULL;
  187. }
  188. static struct community_list *
  189. community_list_get (struct community_list_handler *ch,
  190. const char *name, int master)
  191. {
  192. struct community_list *list;
  193. list = community_list_lookup (ch, name, master);
  194. if (!list)
  195. list = community_list_insert (ch, name, master);
  196. return list;
  197. }
  198. static void
  199. community_list_delete (struct community_list *list)
  200. {
  201. struct community_list_list *clist;
  202. struct community_entry *entry, *next;
  203. for (entry = list->head; entry; entry = next)
  204. {
  205. next = entry->next;
  206. community_entry_free (entry);
  207. }
  208. clist = list->parent;
  209. if (list->next)
  210. list->next->prev = list->prev;
  211. else
  212. clist->tail = list->prev;
  213. if (list->prev)
  214. list->prev->next = list->next;
  215. else
  216. clist->head = list->next;
  217. community_list_free (list);
  218. }
  219. static int
  220. community_list_empty_p (struct community_list *list)
  221. {
  222. return (list->head == NULL && list->tail == NULL) ? 1 : 0;
  223. }
  224. /* Add community-list entry to the list. */
  225. static void
  226. community_list_entry_add (struct community_list *list,
  227. struct community_entry *entry)
  228. {
  229. entry->next = NULL;
  230. entry->prev = list->tail;
  231. if (list->tail)
  232. list->tail->next = entry;
  233. else
  234. list->head = entry;
  235. list->tail = entry;
  236. }
  237. /* Delete community-list entry from the list. */
  238. static void
  239. community_list_entry_delete (struct community_list *list,
  240. struct community_entry *entry, int style)
  241. {
  242. if (entry->next)
  243. entry->next->prev = entry->prev;
  244. else
  245. list->tail = entry->prev;
  246. if (entry->prev)
  247. entry->prev->next = entry->next;
  248. else
  249. list->head = entry->next;
  250. community_entry_free (entry);
  251. if (community_list_empty_p (list))
  252. community_list_delete (list);
  253. }
  254. /* Lookup community-list entry from the list. */
  255. static struct community_entry *
  256. community_list_entry_lookup (struct community_list *list, const void *arg,
  257. int direct)
  258. {
  259. struct community_entry *entry;
  260. for (entry = list->head; entry; entry = entry->next)
  261. {
  262. switch (entry->style)
  263. {
  264. case COMMUNITY_LIST_STANDARD:
  265. if (community_cmp (entry->u.com, arg))
  266. return entry;
  267. break;
  268. case EXTCOMMUNITY_LIST_STANDARD:
  269. if (ecommunity_cmp (entry->u.ecom, arg))
  270. return entry;
  271. break;
  272. case COMMUNITY_LIST_EXPANDED:
  273. case EXTCOMMUNITY_LIST_EXPANDED:
  274. if (strcmp (entry->config, arg) == 0)
  275. return entry;
  276. break;
  277. default:
  278. break;
  279. }
  280. }
  281. return NULL;
  282. }
  283. static char *
  284. community_str_get (struct community *com, int i)
  285. {
  286. int len;
  287. u_int32_t comval;
  288. u_int16_t as;
  289. u_int16_t val;
  290. char *str;
  291. char *pnt;
  292. memcpy (&comval, com_nthval (com, i), sizeof (u_int32_t));
  293. comval = ntohl (comval);
  294. switch (comval)
  295. {
  296. case COMMUNITY_INTERNET:
  297. len = strlen (" internet");
  298. break;
  299. case COMMUNITY_NO_EXPORT:
  300. len = strlen (" no-export");
  301. break;
  302. case COMMUNITY_NO_ADVERTISE:
  303. len = strlen (" no-advertise");
  304. break;
  305. case COMMUNITY_LOCAL_AS:
  306. len = strlen (" local-AS");
  307. break;
  308. default:
  309. len = strlen (" 65536:65535");
  310. break;
  311. }
  312. /* Allocate memory. */
  313. str = pnt = XMALLOC (MTYPE_COMMUNITY_STR, len);
  314. switch (comval)
  315. {
  316. case COMMUNITY_INTERNET:
  317. strcpy (pnt, "internet");
  318. pnt += strlen ("internet");
  319. break;
  320. case COMMUNITY_NO_EXPORT:
  321. strcpy (pnt, "no-export");
  322. pnt += strlen ("no-export");
  323. break;
  324. case COMMUNITY_NO_ADVERTISE:
  325. strcpy (pnt, "no-advertise");
  326. pnt += strlen ("no-advertise");
  327. break;
  328. case COMMUNITY_LOCAL_AS:
  329. strcpy (pnt, "local-AS");
  330. pnt += strlen ("local-AS");
  331. break;
  332. default:
  333. as = (comval >> 16) & 0xFFFF;
  334. val = comval & 0xFFFF;
  335. sprintf (pnt, "%u:%d", as, val);
  336. pnt += strlen (pnt);
  337. break;
  338. }
  339. *pnt = '\0';
  340. return str;
  341. }
  342. /* Internal function to perform regular expression match for
  343. * * a single community. */
  344. static int
  345. community_regexp_include (regex_t * reg, struct community *com, int i)
  346. {
  347. const char *str;
  348. /* When there is no communities attribute it is treated as empty
  349. * string. */
  350. if (com == NULL || com->size == 0)
  351. str = "";
  352. else
  353. str = community_str_get (com, i);
  354. /* Regular expression match. */
  355. if (regexec (reg, str, 0, NULL, 0) == 0)
  356. return 1;
  357. /* No match. */
  358. return 0;
  359. }
  360. /* Internal function to perform regular expression match for community
  361. attribute. */
  362. static int
  363. community_regexp_match (struct community *com, regex_t * reg)
  364. {
  365. const char *str;
  366. /* When there is no communities attribute it is treated as empty
  367. string. */
  368. if (com == NULL || com->size == 0)
  369. str = "";
  370. else
  371. str = community_str (com);
  372. /* Regular expression match. */
  373. if (regexec (reg, str, 0, NULL, 0) == 0)
  374. return 1;
  375. /* No match. */
  376. return 0;
  377. }
  378. static int
  379. ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
  380. {
  381. const char *str;
  382. /* When there is no communities attribute it is treated as empty
  383. string. */
  384. if (ecom == NULL || ecom->size == 0)
  385. str = "";
  386. else
  387. str = ecommunity_str (ecom);
  388. /* Regular expression match. */
  389. if (regexec (reg, str, 0, NULL, 0) == 0)
  390. return 1;
  391. /* No match. */
  392. return 0;
  393. }
  394. /* When given community attribute matches to the community-list return
  395. 1 else return 0. */
  396. int
  397. community_list_match (struct community *com, struct community_list *list)
  398. {
  399. struct community_entry *entry;
  400. for (entry = list->head; entry; entry = entry->next)
  401. {
  402. if (entry->any)
  403. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  404. if (entry->style == COMMUNITY_LIST_STANDARD)
  405. {
  406. if (community_include (entry->u.com, COMMUNITY_INTERNET))
  407. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  408. if (community_match (com, entry->u.com))
  409. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  410. }
  411. else if (entry->style == COMMUNITY_LIST_EXPANDED)
  412. {
  413. if (community_regexp_match (com, entry->reg))
  414. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  415. }
  416. }
  417. return 0;
  418. }
  419. int
  420. ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
  421. {
  422. struct community_entry *entry;
  423. for (entry = list->head; entry; entry = entry->next)
  424. {
  425. if (entry->any)
  426. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  427. if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
  428. {
  429. if (ecommunity_match (ecom, entry->u.ecom))
  430. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  431. }
  432. else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
  433. {
  434. if (ecommunity_regexp_match (ecom, entry->reg))
  435. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  436. }
  437. }
  438. return 0;
  439. }
  440. /* Perform exact matching. In case of expanded community-list, do
  441. same thing as community_list_match(). */
  442. int
  443. community_list_exact_match (struct community *com,
  444. struct community_list *list)
  445. {
  446. struct community_entry *entry;
  447. for (entry = list->head; entry; entry = entry->next)
  448. {
  449. if (entry->any)
  450. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  451. if (entry->style == COMMUNITY_LIST_STANDARD)
  452. {
  453. if (community_include (entry->u.com, COMMUNITY_INTERNET))
  454. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  455. if (community_cmp (com, entry->u.com))
  456. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  457. }
  458. else if (entry->style == COMMUNITY_LIST_EXPANDED)
  459. {
  460. if (community_regexp_match (com, entry->reg))
  461. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  462. }
  463. }
  464. return 0;
  465. }
  466. /* Delete all permitted communities in the list from com. */
  467. struct community *
  468. community_list_match_delete (struct community *com,
  469. struct community_list *list)
  470. {
  471. struct community_entry *entry;
  472. u_int32_t val;
  473. u_int32_t com_index_to_delete[com->size];
  474. int delete_index = 0;
  475. int i;
  476. /* Loop over each community value and evaluate each against the
  477. * community-list. If we need to delete a community value add its index to
  478. * com_index_to_delete.
  479. */
  480. for (i = 0; i < com->size; i++)
  481. {
  482. val = community_val_get (com, i);
  483. for (entry = list->head; entry; entry = entry->next)
  484. {
  485. if (entry->any)
  486. {
  487. if (entry->direct == COMMUNITY_PERMIT)
  488. {
  489. com_index_to_delete[delete_index] = i;
  490. delete_index++;
  491. }
  492. break;
  493. }
  494. else if ((entry->style == COMMUNITY_LIST_STANDARD)
  495. && (community_include (entry->u.com, COMMUNITY_INTERNET)
  496. || community_include (entry->u.com, val) ))
  497. {
  498. if (entry->direct == COMMUNITY_PERMIT)
  499. {
  500. com_index_to_delete[delete_index] = i;
  501. delete_index++;
  502. }
  503. break;
  504. }
  505. else if ((entry->style == COMMUNITY_LIST_EXPANDED)
  506. && community_regexp_include (entry->reg, com, i))
  507. {
  508. if (entry->direct == COMMUNITY_PERMIT)
  509. {
  510. com_index_to_delete[delete_index] = i;
  511. delete_index++;
  512. }
  513. break;
  514. }
  515. }
  516. }
  517. /* Delete all of the communities we flagged for deletion */
  518. for (i = delete_index-1; i >= 0; i--)
  519. {
  520. val = community_val_get (com, com_index_to_delete[i]);
  521. community_del_val (com, &val);
  522. }
  523. return com;
  524. }
  525. /* To avoid duplicated entry in the community-list, this function
  526. compares specified entry to existing entry. */
  527. static int
  528. community_list_dup_check (struct community_list *list,
  529. struct community_entry *new)
  530. {
  531. struct community_entry *entry;
  532. for (entry = list->head; entry; entry = entry->next)
  533. {
  534. if (entry->style != new->style)
  535. continue;
  536. if (entry->direct != new->direct)
  537. continue;
  538. if (entry->any != new->any)
  539. continue;
  540. if (entry->any)
  541. return 1;
  542. switch (entry->style)
  543. {
  544. case COMMUNITY_LIST_STANDARD:
  545. if (community_cmp (entry->u.com, new->u.com))
  546. return 1;
  547. break;
  548. case EXTCOMMUNITY_LIST_STANDARD:
  549. if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
  550. return 1;
  551. break;
  552. case COMMUNITY_LIST_EXPANDED:
  553. case EXTCOMMUNITY_LIST_EXPANDED:
  554. if (strcmp (entry->config, new->config) == 0)
  555. return 1;
  556. break;
  557. default:
  558. break;
  559. }
  560. }
  561. return 0;
  562. }
  563. /* Set community-list. */
  564. int
  565. community_list_set (struct community_list_handler *ch,
  566. const char *name, const char *str, int direct, int style)
  567. {
  568. struct community_entry *entry = NULL;
  569. struct community_list *list;
  570. struct community *com = NULL;
  571. regex_t *regex = NULL;
  572. /* Get community list. */
  573. list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
  574. /* When community-list already has entry, new entry should have same
  575. style. If you want to have mixed style community-list, you can
  576. comment out this check. */
  577. if (!community_list_empty_p (list))
  578. {
  579. struct community_entry *first;
  580. first = list->head;
  581. if (style != first->style)
  582. {
  583. return (first->style == COMMUNITY_LIST_STANDARD
  584. ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
  585. : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
  586. }
  587. }
  588. if (str)
  589. {
  590. if (style == COMMUNITY_LIST_STANDARD)
  591. com = community_str2com (str);
  592. else
  593. regex = bgp_regcomp (str);
  594. if (! com && ! regex)
  595. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  596. }
  597. entry = community_entry_new ();
  598. entry->direct = direct;
  599. entry->style = style;
  600. entry->any = (str ? 0 : 1);
  601. entry->u.com = com;
  602. entry->reg = regex;
  603. entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
  604. /* Do not put duplicated community entry. */
  605. if (community_list_dup_check (list, entry))
  606. community_entry_free (entry);
  607. else
  608. community_list_entry_add (list, entry);
  609. return 0;
  610. }
  611. /* Unset community-list. When str is NULL, delete all of
  612. community-list entry belongs to the specified name. */
  613. int
  614. community_list_unset (struct community_list_handler *ch,
  615. const char *name, const char *str,
  616. int direct, int style)
  617. {
  618. struct community_entry *entry = NULL;
  619. struct community_list *list;
  620. struct community *com = NULL;
  621. regex_t *regex = NULL;
  622. /* Lookup community list. */
  623. list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
  624. if (list == NULL)
  625. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  626. /* Delete all of entry belongs to this community-list. */
  627. if (!str)
  628. {
  629. community_list_delete (list);
  630. return 0;
  631. }
  632. if (style == COMMUNITY_LIST_STANDARD)
  633. com = community_str2com (str);
  634. else
  635. regex = bgp_regcomp (str);
  636. if (! com && ! regex)
  637. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  638. if (com)
  639. entry = community_list_entry_lookup (list, com, direct);
  640. else
  641. entry = community_list_entry_lookup (list, str, direct);
  642. if (com)
  643. community_free (com);
  644. if (regex)
  645. bgp_regex_free (regex);
  646. if (!entry)
  647. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  648. community_list_entry_delete (list, entry, style);
  649. return 0;
  650. }
  651. /* Set extcommunity-list. */
  652. int
  653. extcommunity_list_set (struct community_list_handler *ch,
  654. const char *name, const char *str,
  655. int direct, int style)
  656. {
  657. struct community_entry *entry = NULL;
  658. struct community_list *list;
  659. struct ecommunity *ecom = NULL;
  660. regex_t *regex = NULL;
  661. entry = NULL;
  662. /* Get community list. */
  663. list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
  664. /* When community-list already has entry, new entry should have same
  665. style. If you want to have mixed style community-list, you can
  666. comment out this check. */
  667. if (!community_list_empty_p (list))
  668. {
  669. struct community_entry *first;
  670. first = list->head;
  671. if (style != first->style)
  672. {
  673. return (first->style == EXTCOMMUNITY_LIST_STANDARD
  674. ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
  675. : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
  676. }
  677. }
  678. if (str)
  679. {
  680. if (style == EXTCOMMUNITY_LIST_STANDARD)
  681. ecom = ecommunity_str2com (str, 0, 1);
  682. else
  683. regex = bgp_regcomp (str);
  684. if (! ecom && ! regex)
  685. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  686. }
  687. if (ecom)
  688. ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
  689. entry = community_entry_new ();
  690. entry->direct = direct;
  691. entry->style = style;
  692. entry->any = (str ? 0 : 1);
  693. if (ecom)
  694. entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
  695. else if (regex)
  696. entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
  697. else
  698. entry->config = NULL;
  699. entry->u.ecom = ecom;
  700. entry->reg = regex;
  701. /* Do not put duplicated community entry. */
  702. if (community_list_dup_check (list, entry))
  703. community_entry_free (entry);
  704. else
  705. community_list_entry_add (list, entry);
  706. return 0;
  707. }
  708. /* Unset extcommunity-list. When str is NULL, delete all of
  709. extcommunity-list entry belongs to the specified name. */
  710. int
  711. extcommunity_list_unset (struct community_list_handler *ch,
  712. const char *name, const char *str,
  713. int direct, int style)
  714. {
  715. struct community_entry *entry = NULL;
  716. struct community_list *list;
  717. struct ecommunity *ecom = NULL;
  718. regex_t *regex = NULL;
  719. /* Lookup extcommunity list. */
  720. list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
  721. if (list == NULL)
  722. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  723. /* Delete all of entry belongs to this extcommunity-list. */
  724. if (!str)
  725. {
  726. community_list_delete (list);
  727. return 0;
  728. }
  729. if (style == EXTCOMMUNITY_LIST_STANDARD)
  730. ecom = ecommunity_str2com (str, 0, 1);
  731. else
  732. regex = bgp_regcomp (str);
  733. if (! ecom && ! regex)
  734. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  735. if (ecom)
  736. entry = community_list_entry_lookup (list, ecom, direct);
  737. else
  738. entry = community_list_entry_lookup (list, str, direct);
  739. if (ecom)
  740. ecommunity_free (&ecom);
  741. if (regex)
  742. bgp_regex_free (regex);
  743. if (!entry)
  744. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  745. community_list_entry_delete (list, entry, style);
  746. return 0;
  747. }
  748. /* Initializa community-list. Return community-list handler. */
  749. struct community_list_handler *
  750. community_list_init (void)
  751. {
  752. struct community_list_handler *ch;
  753. ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
  754. sizeof (struct community_list_handler));
  755. return ch;
  756. }
  757. /* Terminate community-list. */
  758. void
  759. community_list_terminate (struct community_list_handler *ch)
  760. {
  761. struct community_list_master *cm;
  762. struct community_list *list;
  763. cm = &ch->community_list;
  764. while ((list = cm->num.head) != NULL)
  765. community_list_delete (list);
  766. while ((list = cm->str.head) != NULL)
  767. community_list_delete (list);
  768. cm = &ch->extcommunity_list;
  769. while ((list = cm->num.head) != NULL)
  770. community_list_delete (list);
  771. while ((list = cm->str.head) != NULL)
  772. community_list_delete (list);
  773. XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
  774. }