ipsec_sa_mgr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Copyright (C) 2012-2017 Tobias Brunner
  3. * Copyright (C) 2012 Giuliano Grassi
  4. * Copyright (C) 2012 Ralf Sager
  5. * HSR Hochschule fuer Technik Rapperswil
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. */
  17. #include "ipsec.h"
  18. #include "ipsec_sa_mgr.h"
  19. #include <utils/debug.h>
  20. #include <library.h>
  21. #include <processing/jobs/callback_job.h>
  22. #include <threading/condvar.h>
  23. #include <threading/mutex.h>
  24. #include <collections/hashtable.h>
  25. #include <collections/linked_list.h>
  26. typedef struct private_ipsec_sa_mgr_t private_ipsec_sa_mgr_t;
  27. /**
  28. * Private additions to ipsec_sa_mgr_t.
  29. */
  30. struct private_ipsec_sa_mgr_t {
  31. /**
  32. * Public members of ipsec_sa_mgr_t.
  33. */
  34. ipsec_sa_mgr_t public;
  35. /**
  36. * Installed SAs
  37. */
  38. linked_list_t *sas;
  39. /**
  40. * SPIs allocated using get_spi()
  41. */
  42. hashtable_t *allocated_spis;
  43. /**
  44. * Mutex used to synchronize access to the SA manager
  45. */
  46. mutex_t *mutex;
  47. /**
  48. * RNG used to generate SPIs
  49. */
  50. rng_t *rng;
  51. };
  52. /**
  53. * Struct to keep track of locked IPsec SAs
  54. */
  55. typedef struct {
  56. /**
  57. * IPsec SA
  58. */
  59. ipsec_sa_t *sa;
  60. /**
  61. * Set if this SA is currently in use by a thread
  62. */
  63. bool locked;
  64. /**
  65. * Condvar used by threads to wait for this entry
  66. */
  67. condvar_t *condvar;
  68. /**
  69. * Number of threads waiting for this entry
  70. */
  71. u_int waiting_threads;
  72. /**
  73. * Set if this entry is awaiting deletion
  74. */
  75. bool awaits_deletion;
  76. } ipsec_sa_entry_t;
  77. /**
  78. * Helper struct for expiration events
  79. */
  80. typedef struct {
  81. /**
  82. * IPsec SA manager
  83. */
  84. private_ipsec_sa_mgr_t *manager;
  85. /**
  86. * Entry that expired
  87. */
  88. ipsec_sa_entry_t *entry;
  89. /**
  90. * SPI of the expired entry
  91. */
  92. uint32_t spi;
  93. /**
  94. * 0 if this is a hard expire, otherwise the offset in s (soft->hard)
  95. */
  96. uint32_t hard_offset;
  97. } ipsec_sa_expired_t;
  98. /*
  99. * Used for the hash table of allocated SPIs
  100. */
  101. static bool spi_equals(uint32_t *spi, uint32_t *other_spi)
  102. {
  103. return *spi == *other_spi;
  104. }
  105. static u_int spi_hash(uint32_t *spi)
  106. {
  107. return chunk_hash(chunk_from_thing(*spi));
  108. }
  109. /**
  110. * Create an SA entry
  111. */
  112. static ipsec_sa_entry_t *create_entry(ipsec_sa_t *sa)
  113. {
  114. ipsec_sa_entry_t *this;
  115. INIT(this,
  116. .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
  117. .sa = sa,
  118. );
  119. return this;
  120. }
  121. /**
  122. * Destroy an SA entry
  123. */
  124. static void destroy_entry(ipsec_sa_entry_t *entry)
  125. {
  126. entry->condvar->destroy(entry->condvar);
  127. entry->sa->destroy(entry->sa);
  128. free(entry);
  129. }
  130. /**
  131. * Makes sure an entry is safe to remove
  132. * Must be called with this->mutex held.
  133. *
  134. * @return TRUE if entry can be removed, FALSE if entry is already
  135. * being removed by another thread
  136. */
  137. static bool wait_remove_entry(private_ipsec_sa_mgr_t *this,
  138. ipsec_sa_entry_t *entry)
  139. {
  140. if (entry->awaits_deletion)
  141. {
  142. /* this will be deleted by another thread already */
  143. return FALSE;
  144. }
  145. entry->awaits_deletion = TRUE;
  146. while (entry->locked)
  147. {
  148. entry->condvar->wait(entry->condvar, this->mutex);
  149. }
  150. while (entry->waiting_threads > 0)
  151. {
  152. entry->condvar->broadcast(entry->condvar);
  153. entry->condvar->wait(entry->condvar, this->mutex);
  154. }
  155. return TRUE;
  156. }
  157. /**
  158. * Waits until an is available and then locks it.
  159. * Must only be called with this->mutex held
  160. */
  161. static bool wait_for_entry(private_ipsec_sa_mgr_t *this,
  162. ipsec_sa_entry_t *entry)
  163. {
  164. while (entry->locked && !entry->awaits_deletion)
  165. {
  166. entry->waiting_threads++;
  167. entry->condvar->wait(entry->condvar, this->mutex);
  168. entry->waiting_threads--;
  169. }
  170. if (entry->awaits_deletion)
  171. {
  172. /* others may still be waiting, */
  173. entry->condvar->signal(entry->condvar);
  174. return FALSE;
  175. }
  176. entry->locked = TRUE;
  177. return TRUE;
  178. }
  179. /**
  180. * Flushes all entries
  181. * Must be called with this->mutex held.
  182. */
  183. static void flush_entries(private_ipsec_sa_mgr_t *this)
  184. {
  185. ipsec_sa_entry_t *current;
  186. enumerator_t *enumerator;
  187. DBG2(DBG_ESP, "flushing SAD");
  188. enumerator = this->sas->create_enumerator(this->sas);
  189. while (enumerator->enumerate(enumerator, (void**)&current))
  190. {
  191. if (wait_remove_entry(this, current))
  192. {
  193. this->sas->remove_at(this->sas, enumerator);
  194. destroy_entry(current);
  195. }
  196. }
  197. enumerator->destroy(enumerator);
  198. }
  199. CALLBACK(match_entry_by_sa_ptr, bool,
  200. ipsec_sa_entry_t *item, va_list args)
  201. {
  202. ipsec_sa_t *sa;
  203. VA_ARGS_VGET(args, sa);
  204. return item->sa == sa;
  205. }
  206. CALLBACK(match_entry_by_spi_inbound, bool,
  207. ipsec_sa_entry_t *item, va_list args)
  208. {
  209. uint32_t spi;
  210. int inbound;
  211. VA_ARGS_VGET(args, spi, inbound);
  212. return item->sa->get_spi(item->sa) == spi &&
  213. item->sa->is_inbound(item->sa) == inbound;
  214. }
  215. static bool match_entry_by_spi_src_dst(ipsec_sa_entry_t *item, uint32_t spi,
  216. host_t *src, host_t *dst)
  217. {
  218. return item->sa->match_by_spi_src_dst(item->sa, spi, src, dst);
  219. }
  220. CALLBACK(match_entry_by_spi_src_dst_cb, bool,
  221. ipsec_sa_entry_t *item, va_list args)
  222. {
  223. host_t *src, *dst;
  224. uint32_t spi;
  225. VA_ARGS_VGET(args, spi, src, dst);
  226. return match_entry_by_spi_src_dst(item, spi, src, dst);
  227. }
  228. CALLBACK(match_entry_by_reqid_inbound, bool,
  229. ipsec_sa_entry_t *item, va_list args)
  230. {
  231. uint32_t reqid;
  232. int inbound;
  233. VA_ARGS_VGET(args, reqid, inbound);
  234. return item->sa->match_by_reqid(item->sa, reqid, inbound);
  235. }
  236. CALLBACK(match_entry_by_spi_dst, bool,
  237. ipsec_sa_entry_t *item, va_list args)
  238. {
  239. host_t *dst;
  240. uint32_t spi;
  241. VA_ARGS_VGET(args, spi, dst);
  242. return item->sa->match_by_spi_dst(item->sa, spi, dst);
  243. }
  244. /**
  245. * Remove an entry
  246. */
  247. static bool remove_entry(private_ipsec_sa_mgr_t *this, ipsec_sa_entry_t *entry)
  248. {
  249. ipsec_sa_entry_t *current;
  250. enumerator_t *enumerator;
  251. bool removed = FALSE;
  252. enumerator = this->sas->create_enumerator(this->sas);
  253. while (enumerator->enumerate(enumerator, (void**)&current))
  254. {
  255. if (current == entry)
  256. {
  257. if (wait_remove_entry(this, current))
  258. {
  259. this->sas->remove_at(this->sas, enumerator);
  260. removed = TRUE;
  261. }
  262. break;
  263. }
  264. }
  265. enumerator->destroy(enumerator);
  266. return removed;
  267. }
  268. /**
  269. * Callback for expiration events
  270. */
  271. static job_requeue_t sa_expired(ipsec_sa_expired_t *expired)
  272. {
  273. private_ipsec_sa_mgr_t *this = expired->manager;
  274. this->mutex->lock(this->mutex);
  275. if (this->sas->find_first(this->sas, NULL, (void**)&expired->entry) &&
  276. expired->spi == expired->entry->sa->get_spi(expired->entry->sa))
  277. { /* only if we find the right SA at this pointer location */
  278. uint32_t hard_offset;
  279. hard_offset = expired->hard_offset;
  280. expired->entry->sa->expire(expired->entry->sa, hard_offset == 0);
  281. if (hard_offset)
  282. { /* soft limit reached, schedule hard expire */
  283. expired->hard_offset = 0;
  284. this->mutex->unlock(this->mutex);
  285. return JOB_RESCHEDULE(hard_offset);
  286. }
  287. /* hard limit reached */
  288. if (remove_entry(this, expired->entry))
  289. {
  290. destroy_entry(expired->entry);
  291. }
  292. }
  293. this->mutex->unlock(this->mutex);
  294. return JOB_REQUEUE_NONE;
  295. }
  296. /**
  297. * Schedule a job to handle IPsec SA expiration
  298. */
  299. static void schedule_expiration(private_ipsec_sa_mgr_t *this,
  300. ipsec_sa_entry_t *entry)
  301. {
  302. lifetime_cfg_t *lifetime = entry->sa->get_lifetime(entry->sa);
  303. ipsec_sa_expired_t *expired;
  304. callback_job_t *job;
  305. uint32_t timeout;
  306. if (!lifetime->time.life)
  307. { /* no expiration at all */
  308. return;
  309. }
  310. INIT(expired,
  311. .manager = this,
  312. .entry = entry,
  313. .spi = entry->sa->get_spi(entry->sa),
  314. );
  315. /* schedule a rekey first, a hard timeout will be scheduled then, if any */
  316. expired->hard_offset = lifetime->time.life - lifetime->time.rekey;
  317. timeout = lifetime->time.rekey;
  318. if (lifetime->time.life <= lifetime->time.rekey ||
  319. lifetime->time.rekey == 0)
  320. { /* no rekey, schedule hard timeout */
  321. expired->hard_offset = 0;
  322. timeout = lifetime->time.life;
  323. }
  324. job = callback_job_create((callback_job_cb_t)sa_expired, expired,
  325. (callback_job_cleanup_t)free, NULL);
  326. lib->scheduler->schedule_job(lib->scheduler, (job_t*)job, timeout);
  327. }
  328. /**
  329. * Remove all allocated SPIs
  330. */
  331. static void flush_allocated_spis(private_ipsec_sa_mgr_t *this)
  332. {
  333. enumerator_t *enumerator;
  334. uint32_t *current;
  335. DBG2(DBG_ESP, "flushing allocated SPIs");
  336. enumerator = this->allocated_spis->create_enumerator(this->allocated_spis);
  337. while (enumerator->enumerate(enumerator, NULL, (void**)&current))
  338. {
  339. this->allocated_spis->remove_at(this->allocated_spis, enumerator);
  340. DBG2(DBG_ESP, " removed allocated SPI %.8x", ntohl(*current));
  341. free(current);
  342. }
  343. enumerator->destroy(enumerator);
  344. }
  345. /**
  346. * Pre-allocate an SPI for an inbound SA
  347. */
  348. static bool allocate_spi(private_ipsec_sa_mgr_t *this, uint32_t spi)
  349. {
  350. uint32_t *spi_alloc;
  351. if (this->allocated_spis->get(this->allocated_spis, &spi) ||
  352. this->sas->find_first(this->sas, match_entry_by_spi_inbound,
  353. NULL, spi, TRUE))
  354. {
  355. return FALSE;
  356. }
  357. spi_alloc = malloc_thing(uint32_t);
  358. *spi_alloc = spi;
  359. this->allocated_spis->put(this->allocated_spis, spi_alloc, spi_alloc);
  360. return TRUE;
  361. }
  362. METHOD(ipsec_sa_mgr_t, get_spi, status_t,
  363. private_ipsec_sa_mgr_t *this, host_t *src, host_t *dst, uint8_t protocol,
  364. uint32_t *spi)
  365. {
  366. uint32_t spi_min, spi_max, spi_new;
  367. spi_min = lib->settings->get_int(lib->settings, "%s.spi_min",
  368. 0x00000100, lib->ns);
  369. spi_max = lib->settings->get_int(lib->settings, "%s.spi_max",
  370. 0xffffffff, lib->ns);
  371. if (spi_min > spi_max)
  372. {
  373. spi_new = spi_min;
  374. spi_min = spi_max;
  375. spi_max = spi_new;
  376. }
  377. /* make sure the SPI is valid (not in range 0-255) */
  378. spi_min = max(spi_min, 0x00000100);
  379. spi_max = max(spi_max, 0x00000100);
  380. this->mutex->lock(this->mutex);
  381. if (!this->rng)
  382. {
  383. this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
  384. if (!this->rng)
  385. {
  386. this->mutex->unlock(this->mutex);
  387. DBG1(DBG_ESP, "failed to create RNG for SPI generation");
  388. return FAILED;
  389. }
  390. }
  391. do
  392. {
  393. if (!this->rng->get_bytes(this->rng, sizeof(spi_new),
  394. (uint8_t*)&spi_new))
  395. {
  396. this->mutex->unlock(this->mutex);
  397. DBG1(DBG_ESP, "failed to allocate SPI");
  398. return FAILED;
  399. }
  400. spi_new = spi_min + spi_new % (spi_max - spi_min + 1);
  401. spi_new = htonl(spi_new);
  402. }
  403. while (!allocate_spi(this, spi_new));
  404. this->mutex->unlock(this->mutex);
  405. *spi = spi_new;
  406. DBG2(DBG_ESP, "allocated SPI %.8x", ntohl(*spi));
  407. return SUCCESS;
  408. }
  409. METHOD(ipsec_sa_mgr_t, add_sa, status_t,
  410. private_ipsec_sa_mgr_t *this, host_t *src, host_t *dst, uint32_t spi,
  411. uint8_t protocol, uint32_t reqid, mark_t mark, uint32_t tfc,
  412. lifetime_cfg_t *lifetime, uint16_t enc_alg, chunk_t enc_key,
  413. uint16_t int_alg, chunk_t int_key, ipsec_mode_t mode, uint16_t ipcomp,
  414. uint16_t cpi, bool initiator, bool encap, bool esn, bool inbound,
  415. bool update)
  416. {
  417. ipsec_sa_entry_t *entry;
  418. ipsec_sa_t *sa_new;
  419. DBG2(DBG_ESP, "adding SAD entry with SPI %.8x and reqid {%u}",
  420. ntohl(spi), reqid);
  421. DBG2(DBG_ESP, " using encryption algorithm %N with key size %d",
  422. encryption_algorithm_names, enc_alg, enc_key.len * 8);
  423. DBG2(DBG_ESP, " using integrity algorithm %N with key size %d",
  424. integrity_algorithm_names, int_alg, int_key.len * 8);
  425. sa_new = ipsec_sa_create(spi, src, dst, protocol, reqid, mark, tfc,
  426. lifetime, enc_alg, enc_key, int_alg, int_key, mode,
  427. ipcomp, cpi, encap, esn, inbound);
  428. if (!sa_new)
  429. {
  430. DBG1(DBG_ESP, "failed to create SAD entry");
  431. return FAILED;
  432. }
  433. this->mutex->lock(this->mutex);
  434. if (update)
  435. { /* remove any pre-allocated SPIs */
  436. uint32_t *spi_alloc;
  437. spi_alloc = this->allocated_spis->remove(this->allocated_spis, &spi);
  438. free(spi_alloc);
  439. }
  440. if (this->sas->find_first(this->sas, match_entry_by_spi_src_dst_cb, NULL,
  441. spi, src, dst))
  442. {
  443. this->mutex->unlock(this->mutex);
  444. DBG1(DBG_ESP, "failed to install SAD entry: already installed");
  445. sa_new->destroy(sa_new);
  446. return FAILED;
  447. }
  448. entry = create_entry(sa_new);
  449. schedule_expiration(this, entry);
  450. this->sas->insert_first(this->sas, entry);
  451. this->mutex->unlock(this->mutex);
  452. return SUCCESS;
  453. }
  454. METHOD(ipsec_sa_mgr_t, update_sa, status_t,
  455. private_ipsec_sa_mgr_t *this, uint32_t spi, uint8_t protocol,
  456. uint16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
  457. bool encap, bool new_encap, mark_t mark)
  458. {
  459. ipsec_sa_entry_t *entry = NULL;
  460. DBG2(DBG_ESP, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
  461. ntohl(spi), src, dst, new_src, new_dst);
  462. if (!new_encap)
  463. {
  464. DBG1(DBG_ESP, "failed to update SAD entry: can't deactivate UDP "
  465. "encapsulation");
  466. return NOT_SUPPORTED;
  467. }
  468. this->mutex->lock(this->mutex);
  469. if (this->sas->find_first(this->sas, match_entry_by_spi_src_dst_cb,
  470. (void**)&entry, spi, src, dst) &&
  471. wait_for_entry(this, entry))
  472. {
  473. entry->sa->set_source(entry->sa, new_src);
  474. entry->sa->set_destination(entry->sa, new_dst);
  475. /* checkin the entry */
  476. entry->locked = FALSE;
  477. entry->condvar->signal(entry->condvar);
  478. }
  479. this->mutex->unlock(this->mutex);
  480. if (!entry)
  481. {
  482. DBG1(DBG_ESP, "failed to update SAD entry: not found");
  483. return FAILED;
  484. }
  485. return SUCCESS;
  486. }
  487. METHOD(ipsec_sa_mgr_t, query_sa, status_t,
  488. private_ipsec_sa_mgr_t *this, host_t *src, host_t *dst,
  489. uint32_t spi, uint8_t protocol, mark_t mark,
  490. uint64_t *bytes, uint64_t *packets, time_t *time)
  491. {
  492. ipsec_sa_entry_t *entry = NULL;
  493. this->mutex->lock(this->mutex);
  494. if (this->sas->find_first(this->sas, match_entry_by_spi_src_dst_cb,
  495. (void**)&entry, spi, src, dst) &&
  496. wait_for_entry(this, entry))
  497. {
  498. entry->sa->get_usestats(entry->sa, bytes, packets, time);
  499. /* checkin the entry */
  500. entry->locked = FALSE;
  501. entry->condvar->signal(entry->condvar);
  502. }
  503. this->mutex->unlock(this->mutex);
  504. return entry ? SUCCESS : NOT_FOUND;
  505. }
  506. METHOD(ipsec_sa_mgr_t, del_sa, status_t,
  507. private_ipsec_sa_mgr_t *this, host_t *src, host_t *dst, uint32_t spi,
  508. uint8_t protocol, uint16_t cpi, mark_t mark)
  509. {
  510. ipsec_sa_entry_t *current, *found = NULL;
  511. enumerator_t *enumerator;
  512. this->mutex->lock(this->mutex);
  513. enumerator = this->sas->create_enumerator(this->sas);
  514. while (enumerator->enumerate(enumerator, (void**)&current))
  515. {
  516. if (match_entry_by_spi_src_dst(current, spi, src, dst))
  517. {
  518. if (wait_remove_entry(this, current))
  519. {
  520. this->sas->remove_at(this->sas, enumerator);
  521. found = current;
  522. }
  523. break;
  524. }
  525. }
  526. enumerator->destroy(enumerator);
  527. this->mutex->unlock(this->mutex);
  528. if (found)
  529. {
  530. DBG2(DBG_ESP, "deleted %sbound SAD entry with SPI %.8x",
  531. found->sa->is_inbound(found->sa) ? "in" : "out", ntohl(spi));
  532. destroy_entry(found);
  533. return SUCCESS;
  534. }
  535. return FAILED;
  536. }
  537. METHOD(ipsec_sa_mgr_t, checkout_by_reqid, ipsec_sa_t*,
  538. private_ipsec_sa_mgr_t *this, uint32_t reqid, bool inbound)
  539. {
  540. ipsec_sa_entry_t *entry;
  541. ipsec_sa_t *sa = NULL;
  542. this->mutex->lock(this->mutex);
  543. if (this->sas->find_first(this->sas, match_entry_by_reqid_inbound,
  544. (void**)&entry, reqid, inbound) &&
  545. wait_for_entry(this, entry))
  546. {
  547. sa = entry->sa;
  548. }
  549. this->mutex->unlock(this->mutex);
  550. return sa;
  551. }
  552. METHOD(ipsec_sa_mgr_t, checkout_by_spi, ipsec_sa_t*,
  553. private_ipsec_sa_mgr_t *this, uint32_t spi, host_t *dst)
  554. {
  555. ipsec_sa_entry_t *entry;
  556. ipsec_sa_t *sa = NULL;
  557. this->mutex->lock(this->mutex);
  558. if (this->sas->find_first(this->sas, match_entry_by_spi_dst,
  559. (void**)&entry, spi, dst) &&
  560. wait_for_entry(this, entry))
  561. {
  562. sa = entry->sa;
  563. }
  564. this->mutex->unlock(this->mutex);
  565. return sa;
  566. }
  567. METHOD(ipsec_sa_mgr_t, checkin, void,
  568. private_ipsec_sa_mgr_t *this, ipsec_sa_t *sa)
  569. {
  570. ipsec_sa_entry_t *entry;
  571. this->mutex->lock(this->mutex);
  572. if (this->sas->find_first(this->sas, match_entry_by_sa_ptr,
  573. (void**)&entry, sa))
  574. {
  575. if (entry->locked)
  576. {
  577. entry->locked = FALSE;
  578. entry->condvar->signal(entry->condvar);
  579. }
  580. }
  581. this->mutex->unlock(this->mutex);
  582. }
  583. METHOD(ipsec_sa_mgr_t, flush_sas, status_t,
  584. private_ipsec_sa_mgr_t *this)
  585. {
  586. this->mutex->lock(this->mutex);
  587. flush_entries(this);
  588. this->mutex->unlock(this->mutex);
  589. return SUCCESS;
  590. }
  591. METHOD(ipsec_sa_mgr_t, destroy, void,
  592. private_ipsec_sa_mgr_t *this)
  593. {
  594. this->mutex->lock(this->mutex);
  595. flush_entries(this);
  596. flush_allocated_spis(this);
  597. this->mutex->unlock(this->mutex);
  598. this->allocated_spis->destroy(this->allocated_spis);
  599. this->sas->destroy(this->sas);
  600. this->mutex->destroy(this->mutex);
  601. DESTROY_IF(this->rng);
  602. free(this);
  603. }
  604. /**
  605. * Described in header.
  606. */
  607. ipsec_sa_mgr_t *ipsec_sa_mgr_create()
  608. {
  609. private_ipsec_sa_mgr_t *this;
  610. INIT(this,
  611. .public = {
  612. .get_spi = _get_spi,
  613. .add_sa = _add_sa,
  614. .update_sa = _update_sa,
  615. .query_sa = _query_sa,
  616. .del_sa = _del_sa,
  617. .checkout_by_spi = _checkout_by_spi,
  618. .checkout_by_reqid = _checkout_by_reqid,
  619. .checkin = _checkin,
  620. .flush_sas = _flush_sas,
  621. .destroy = _destroy,
  622. },
  623. .sas = linked_list_create(),
  624. .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
  625. .allocated_spis = hashtable_create((hashtable_hash_t)spi_hash,
  626. (hashtable_equals_t)spi_equals, 16),
  627. );
  628. return &this->public;
  629. }