bgp_clist.c 21 KB

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