bgp_clist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. char *str;
  348. int rv;
  349. /* When there is no communities attribute it is treated as empty
  350. * string. */
  351. if (com == NULL || com->size == 0)
  352. str = XSTRDUP(MTYPE_COMMUNITY_STR, "");
  353. else
  354. str = community_str_get (com, i);
  355. /* Regular expression match. */
  356. rv = regexec (reg, str, 0, NULL, 0);
  357. XFREE(MTYPE_COMMUNITY_STR, str);
  358. if (rv == 0)
  359. return 1;
  360. /* No match. */
  361. return 0;
  362. }
  363. /* Internal function to perform regular expression match for community
  364. attribute. */
  365. static int
  366. community_regexp_match (struct community *com, regex_t * reg)
  367. {
  368. const char *str;
  369. /* When there is no communities attribute it is treated as empty
  370. string. */
  371. if (com == NULL || com->size == 0)
  372. str = "";
  373. else
  374. str = community_str (com);
  375. /* Regular expression match. */
  376. if (regexec (reg, str, 0, NULL, 0) == 0)
  377. return 1;
  378. /* No match. */
  379. return 0;
  380. }
  381. static int
  382. ecommunity_regexp_match (struct ecommunity *ecom, regex_t * reg)
  383. {
  384. const char *str;
  385. /* When there is no communities attribute it is treated as empty
  386. string. */
  387. if (ecom == NULL || ecom->size == 0)
  388. str = "";
  389. else
  390. str = ecommunity_str (ecom);
  391. /* Regular expression match. */
  392. if (regexec (reg, str, 0, NULL, 0) == 0)
  393. return 1;
  394. /* No match. */
  395. return 0;
  396. }
  397. /* When given community attribute matches to the community-list return
  398. 1 else return 0. */
  399. int
  400. community_list_match (struct community *com, struct community_list *list)
  401. {
  402. struct community_entry *entry;
  403. for (entry = list->head; entry; entry = entry->next)
  404. {
  405. if (entry->any)
  406. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  407. if (entry->style == COMMUNITY_LIST_STANDARD)
  408. {
  409. if (community_include (entry->u.com, COMMUNITY_INTERNET))
  410. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  411. if (community_match (com, entry->u.com))
  412. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  413. }
  414. else if (entry->style == COMMUNITY_LIST_EXPANDED)
  415. {
  416. if (community_regexp_match (com, entry->reg))
  417. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  418. }
  419. }
  420. return 0;
  421. }
  422. int
  423. ecommunity_list_match (struct ecommunity *ecom, struct community_list *list)
  424. {
  425. struct community_entry *entry;
  426. for (entry = list->head; entry; entry = entry->next)
  427. {
  428. if (entry->any)
  429. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  430. if (entry->style == EXTCOMMUNITY_LIST_STANDARD)
  431. {
  432. if (ecommunity_match (ecom, entry->u.ecom))
  433. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  434. }
  435. else if (entry->style == EXTCOMMUNITY_LIST_EXPANDED)
  436. {
  437. if (ecommunity_regexp_match (ecom, entry->reg))
  438. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  439. }
  440. }
  441. return 0;
  442. }
  443. /* Perform exact matching. In case of expanded community-list, do
  444. same thing as community_list_match(). */
  445. int
  446. community_list_exact_match (struct community *com,
  447. struct community_list *list)
  448. {
  449. struct community_entry *entry;
  450. for (entry = list->head; entry; entry = entry->next)
  451. {
  452. if (entry->any)
  453. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  454. if (entry->style == COMMUNITY_LIST_STANDARD)
  455. {
  456. if (community_include (entry->u.com, COMMUNITY_INTERNET))
  457. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  458. if (community_cmp (com, entry->u.com))
  459. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  460. }
  461. else if (entry->style == COMMUNITY_LIST_EXPANDED)
  462. {
  463. if (community_regexp_match (com, entry->reg))
  464. return entry->direct == COMMUNITY_PERMIT ? 1 : 0;
  465. }
  466. }
  467. return 0;
  468. }
  469. /* Delete all permitted communities in the list from com. */
  470. struct community *
  471. community_list_match_delete (struct community *com,
  472. struct community_list *list)
  473. {
  474. struct community_entry *entry;
  475. u_int32_t val;
  476. u_int32_t com_index_to_delete[com->size];
  477. int delete_index = 0;
  478. int i;
  479. /* Loop over each community value and evaluate each against the
  480. * community-list. If we need to delete a community value add its index to
  481. * com_index_to_delete.
  482. */
  483. for (i = 0; i < com->size; i++)
  484. {
  485. val = community_val_get (com, i);
  486. for (entry = list->head; entry; entry = entry->next)
  487. {
  488. if (entry->any)
  489. {
  490. if (entry->direct == COMMUNITY_PERMIT)
  491. {
  492. com_index_to_delete[delete_index] = i;
  493. delete_index++;
  494. }
  495. break;
  496. }
  497. else if ((entry->style == COMMUNITY_LIST_STANDARD)
  498. && (community_include (entry->u.com, COMMUNITY_INTERNET)
  499. || community_include (entry->u.com, val) ))
  500. {
  501. if (entry->direct == COMMUNITY_PERMIT)
  502. {
  503. com_index_to_delete[delete_index] = i;
  504. delete_index++;
  505. }
  506. break;
  507. }
  508. else if ((entry->style == COMMUNITY_LIST_EXPANDED)
  509. && community_regexp_include (entry->reg, com, i))
  510. {
  511. if (entry->direct == COMMUNITY_PERMIT)
  512. {
  513. com_index_to_delete[delete_index] = i;
  514. delete_index++;
  515. }
  516. break;
  517. }
  518. }
  519. }
  520. /* Delete all of the communities we flagged for deletion */
  521. for (i = delete_index-1; i >= 0; i--)
  522. {
  523. val = community_val_get (com, com_index_to_delete[i]);
  524. community_del_val (com, &val);
  525. }
  526. return com;
  527. }
  528. /* To avoid duplicated entry in the community-list, this function
  529. compares specified entry to existing entry. */
  530. static int
  531. community_list_dup_check (struct community_list *list,
  532. struct community_entry *new)
  533. {
  534. struct community_entry *entry;
  535. for (entry = list->head; entry; entry = entry->next)
  536. {
  537. if (entry->style != new->style)
  538. continue;
  539. if (entry->direct != new->direct)
  540. continue;
  541. if (entry->any != new->any)
  542. continue;
  543. if (entry->any)
  544. return 1;
  545. switch (entry->style)
  546. {
  547. case COMMUNITY_LIST_STANDARD:
  548. if (community_cmp (entry->u.com, new->u.com))
  549. return 1;
  550. break;
  551. case EXTCOMMUNITY_LIST_STANDARD:
  552. if (ecommunity_cmp (entry->u.ecom, new->u.ecom))
  553. return 1;
  554. break;
  555. case COMMUNITY_LIST_EXPANDED:
  556. case EXTCOMMUNITY_LIST_EXPANDED:
  557. if (entry->config && new->config
  558. && strcmp (entry->config, new->config) == 0)
  559. return 1;
  560. if (!entry->config && !new->config)
  561. return 1;
  562. break;
  563. default:
  564. break;
  565. }
  566. }
  567. return 0;
  568. }
  569. /* Set community-list. */
  570. int
  571. community_list_set (struct community_list_handler *ch,
  572. const char *name, const char *str, int direct, int style)
  573. {
  574. struct community_entry *entry = NULL;
  575. struct community_list *list;
  576. struct community *com = NULL;
  577. regex_t *regex = NULL;
  578. /* Get community list. */
  579. list = community_list_get (ch, name, COMMUNITY_LIST_MASTER);
  580. /* When community-list already has entry, new entry should have same
  581. style. If you want to have mixed style community-list, you can
  582. comment out this check. */
  583. if (!community_list_empty_p (list))
  584. {
  585. struct community_entry *first;
  586. first = list->head;
  587. if (style != first->style)
  588. {
  589. return (first->style == COMMUNITY_LIST_STANDARD
  590. ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
  591. : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
  592. }
  593. }
  594. if (str)
  595. {
  596. if (style == COMMUNITY_LIST_STANDARD)
  597. com = community_str2com (str);
  598. else
  599. regex = bgp_regcomp (str);
  600. if (! com && ! regex)
  601. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  602. }
  603. entry = community_entry_new ();
  604. entry->direct = direct;
  605. entry->style = style;
  606. entry->any = (str ? 0 : 1);
  607. entry->u.com = com;
  608. entry->reg = regex;
  609. entry->config = (regex ? XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str) : NULL);
  610. /* Do not put duplicated community entry. */
  611. if (community_list_dup_check (list, entry))
  612. community_entry_free (entry);
  613. else
  614. community_list_entry_add (list, entry);
  615. return 0;
  616. }
  617. /* Unset community-list. When str is NULL, delete all of
  618. community-list entry belongs to the specified name. */
  619. int
  620. community_list_unset (struct community_list_handler *ch,
  621. const char *name, const char *str,
  622. int direct, int style)
  623. {
  624. struct community_entry *entry = NULL;
  625. struct community_list *list;
  626. struct community *com = NULL;
  627. regex_t *regex = NULL;
  628. /* Lookup community list. */
  629. list = community_list_lookup (ch, name, COMMUNITY_LIST_MASTER);
  630. if (list == NULL)
  631. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  632. /* Delete all of entry belongs to this community-list. */
  633. if (!str)
  634. {
  635. community_list_delete (list);
  636. return 0;
  637. }
  638. if (style == COMMUNITY_LIST_STANDARD)
  639. com = community_str2com (str);
  640. else
  641. regex = bgp_regcomp (str);
  642. if (! com && ! regex)
  643. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  644. if (com)
  645. entry = community_list_entry_lookup (list, com, direct);
  646. else
  647. entry = community_list_entry_lookup (list, str, direct);
  648. if (com)
  649. community_free (com);
  650. if (regex)
  651. bgp_regex_free (regex);
  652. if (!entry)
  653. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  654. community_list_entry_delete (list, entry, style);
  655. return 0;
  656. }
  657. /* Set extcommunity-list. */
  658. int
  659. extcommunity_list_set (struct community_list_handler *ch,
  660. const char *name, const char *str,
  661. int direct, int style)
  662. {
  663. struct community_entry *entry = NULL;
  664. struct community_list *list;
  665. struct ecommunity *ecom = NULL;
  666. regex_t *regex = NULL;
  667. entry = NULL;
  668. /* Get community list. */
  669. list = community_list_get (ch, name, EXTCOMMUNITY_LIST_MASTER);
  670. /* When community-list already has entry, new entry should have same
  671. style. If you want to have mixed style community-list, you can
  672. comment out this check. */
  673. if (!community_list_empty_p (list))
  674. {
  675. struct community_entry *first;
  676. first = list->head;
  677. if (style != first->style)
  678. {
  679. return (first->style == EXTCOMMUNITY_LIST_STANDARD
  680. ? COMMUNITY_LIST_ERR_STANDARD_CONFLICT
  681. : COMMUNITY_LIST_ERR_EXPANDED_CONFLICT);
  682. }
  683. }
  684. if (str)
  685. {
  686. if (style == EXTCOMMUNITY_LIST_STANDARD)
  687. ecom = ecommunity_str2com (str, 0, 1);
  688. else
  689. regex = bgp_regcomp (str);
  690. if (! ecom && ! regex)
  691. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  692. }
  693. if (ecom)
  694. ecom->str = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_DISPLAY);
  695. entry = community_entry_new ();
  696. entry->direct = direct;
  697. entry->style = style;
  698. entry->any = (str ? 0 : 1);
  699. if (ecom)
  700. entry->config = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
  701. else if (regex)
  702. entry->config = XSTRDUP (MTYPE_COMMUNITY_LIST_CONFIG, str);
  703. else
  704. entry->config = NULL;
  705. entry->u.ecom = ecom;
  706. entry->reg = regex;
  707. /* Do not put duplicated community entry. */
  708. if (community_list_dup_check (list, entry))
  709. community_entry_free (entry);
  710. else
  711. community_list_entry_add (list, entry);
  712. return 0;
  713. }
  714. /* Unset extcommunity-list. When str is NULL, delete all of
  715. extcommunity-list entry belongs to the specified name. */
  716. int
  717. extcommunity_list_unset (struct community_list_handler *ch,
  718. const char *name, const char *str,
  719. int direct, int style)
  720. {
  721. struct community_entry *entry = NULL;
  722. struct community_list *list;
  723. struct ecommunity *ecom = NULL;
  724. regex_t *regex = NULL;
  725. /* Lookup extcommunity list. */
  726. list = community_list_lookup (ch, name, EXTCOMMUNITY_LIST_MASTER);
  727. if (list == NULL)
  728. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  729. /* Delete all of entry belongs to this extcommunity-list. */
  730. if (!str)
  731. {
  732. community_list_delete (list);
  733. return 0;
  734. }
  735. if (style == EXTCOMMUNITY_LIST_STANDARD)
  736. ecom = ecommunity_str2com (str, 0, 1);
  737. else
  738. regex = bgp_regcomp (str);
  739. if (! ecom && ! regex)
  740. return COMMUNITY_LIST_ERR_MALFORMED_VAL;
  741. if (ecom)
  742. entry = community_list_entry_lookup (list, ecom, direct);
  743. else
  744. entry = community_list_entry_lookup (list, str, direct);
  745. if (ecom)
  746. ecommunity_free (&ecom);
  747. if (regex)
  748. bgp_regex_free (regex);
  749. if (!entry)
  750. return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
  751. community_list_entry_delete (list, entry, style);
  752. return 0;
  753. }
  754. /* Initializa community-list. Return community-list handler. */
  755. struct community_list_handler *
  756. community_list_init (void)
  757. {
  758. struct community_list_handler *ch;
  759. ch = XCALLOC (MTYPE_COMMUNITY_LIST_HANDLER,
  760. sizeof (struct community_list_handler));
  761. return ch;
  762. }
  763. /* Terminate community-list. */
  764. void
  765. community_list_terminate (struct community_list_handler *ch)
  766. {
  767. struct community_list_master *cm;
  768. struct community_list *list;
  769. cm = &ch->community_list;
  770. while ((list = cm->num.head) != NULL)
  771. community_list_delete (list);
  772. while ((list = cm->str.head) != NULL)
  773. community_list_delete (list);
  774. cm = &ch->extcommunity_list;
  775. while ((list = cm->num.head) != NULL)
  776. community_list_delete (list);
  777. while ((list = cm->str.head) != NULL)
  778. community_list_delete (list);
  779. XFREE (MTYPE_COMMUNITY_LIST_HANDLER, ch);
  780. }