nothing changed
[strongswan.git] / Source / charon / ike_sa_manager.c
1 /**
2 * @file ike_sa_manager.c
3 *
4 * @brief Central point for managing IKE-SAs (creation, locking, deleting...)
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <pthread.h>
24 #include <string.h>
25
26 #include "ike_sa_manager.h"
27
28 #include "globals.h"
29 #include "ike_sa_id.h"
30 #include "utils/allocator.h"
31 #include "utils/logger.h"
32 #include "utils/logger_manager.h"
33 #include "utils/linked_list.h"
34
35 /**
36 * @brief An entry in the linked list, contains IKE_SA, locking and lookup data.
37 */
38 typedef struct ike_sa_entry_s ike_sa_entry_t;
39 struct ike_sa_entry_s {
40 /**
41 * destructor, also destroys ike_sa
42 */
43 status_t (*destroy) (ike_sa_entry_t *this);
44 /**
45 * Number of threads waiting for this ike_sa
46 */
47 int waiting_threads;
48 /**
49 * condvar where threads can wait until it's free again
50 */
51 pthread_cond_t condvar;
52 /**
53 * is this ike_sa currently checked out?
54 */
55 bool checked_out;
56 /**
57 * Does this SA drives out new threads?
58 */
59 bool driveout_new_threads;
60 /**
61 * Does this SA drives out waiting threads?
62 */
63 bool driveout_waiting_threads;
64 /**
65 * identifiaction of ike_sa (SPIs)
66 */
67 ike_sa_id_t *ike_sa_id;
68 /**
69 * the contained ike_sa
70 */
71 ike_sa_t *ike_sa;
72 };
73
74 /**
75 * @see ike_sa_entry_t.destroy
76 */
77 static status_t ike_sa_entry_destroy(ike_sa_entry_t *this)
78 {
79 /* also destroy IKE SA */
80 this->ike_sa->destroy(this->ike_sa);
81 this->ike_sa_id->destroy(this->ike_sa_id);
82 allocator_free(this);
83 return SUCCESS;
84 }
85
86 /**
87 * @brief creates a new entry for the ike_sa list
88 *
89 * This constructor additionaly creates a new and empty SA
90 *
91 * @param ike_sa_id the associated ike_sa_id_t, will be cloned
92 * @return created entry, with ike_sa and ike_sa_id
93 */
94 static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id)
95 {
96 ike_sa_entry_t *this = allocator_alloc_thing(ike_sa_entry_t);
97
98 if (this == NULL)
99 {
100 return NULL;
101 }
102
103 /* destroy function */
104 this->destroy = ike_sa_entry_destroy;
105
106 this->waiting_threads = 0;
107 pthread_cond_init(&(this->condvar), NULL);
108
109 /* we set checkout flag when we really give it out */
110 this->checked_out = FALSE;
111 this->driveout_new_threads = FALSE;
112 this->driveout_waiting_threads = FALSE;
113
114 /* ike_sa_id is always cloned */
115 ike_sa_id->clone(ike_sa_id, &(this->ike_sa_id));
116 if (this->ike_sa_id == NULL)
117 {
118 allocator_free(this);
119 return NULL;
120 }
121
122 /* create new ike_sa */
123 this->ike_sa = ike_sa_create(ike_sa_id);
124 if (this->ike_sa == NULL)
125 {
126 this->ike_sa_id->destroy(this->ike_sa_id);
127 allocator_free(this);
128 return NULL;
129
130 }
131 return this;
132 }
133
134 /**
135 * Additional private members to ike_sa_manager_t
136 */
137 typedef struct private_ike_sa_manager_s private_ike_sa_manager_t;
138 struct private_ike_sa_manager_s {
139 /**
140 * Public members
141 */
142 ike_sa_manager_t public;
143
144 /**
145 * @brief get next spi
146 *
147 * we give out SPIs incremental
148 *
149 * @param this the ike_sa_manager
150 * @param spi[out] spi will be written here
151 * @return SUCCESS or,
152 * OUT_OF_RES when we already served 2^64 SPIs ;-)
153 */
154 status_t (*get_next_spi) (private_ike_sa_manager_t *this, u_int64_t *spi);
155
156 /**
157 * @brief find the ike_sa_entry in the list by SPIs
158 *
159 * This function simply iterates over the linked list. A hash-table
160 * would be more efficient when storing a lot of IKE_SAs...
161 *
162 * @param this the ike_sa_manager containing the list
163 * @param ike_sa_id id of the ike_sa, containing SPIs
164 * @param entry[out] pointer to set to the found entry
165 * @return
166 * - SUCCESS when found,
167 * - NOT_FOUND when no such ike_sa_id in list
168 * - OUT_OF_RES
169 */
170 status_t (*get_entry_by_id) (private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry);
171
172 /**
173 * @brief find the ike_sa_entry in the list by pointer to SA.
174 *
175 * This function simply iterates over the linked list. A hash-table
176 * would be more efficient when storing a lot of IKE_SAs...
177 *
178 * @param this the ike_sa_manager containing the list
179 * @param ike_sa pointer to the ike_sa
180 * @param entry[out] pointer to set to the found entry
181 * @return
182 * - SUCCESS when found,
183 * - NOT_FOUND when no such ike_sa_id in list
184 * - OUT_OF_RES
185 */
186 status_t (*get_entry_by_sa) (private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry);
187
188 /**
189 * @brief delete an entry from the linked list
190 *
191 * @param this the ike_sa_manager containing the list
192 * @param entry entry to delete
193 * @return
194 * - SUCCESS when found,
195 * - NOT_FOUND when no such ike_sa_id in list
196 */
197 status_t (*delete_entry) (private_ike_sa_manager_t *this, ike_sa_entry_t *entry);
198
199 /**
200 * lock for exclusivly accessing the manager
201 */
202 pthread_mutex_t mutex;
203
204 /**
205 * Logger used for this IKE SA Manager
206 */
207 logger_t *logger;
208
209 /**
210 * Linked list with entries for the ike_sa
211 */
212 linked_list_t *ike_sa_list;
213
214 /**
215 * Next SPI, needed for incremental creation of SPIs
216 */
217 u_int64_t next_spi;
218 };
219
220
221 /**
222 * @see private_ike_sa_manager_t.get_entry_by_id
223 */
224 static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry)
225 {
226 linked_list_t *list = this->ike_sa_list;
227 linked_list_iterator_t *iterator;
228 status_t status;
229
230 /* create iterator over list of ike_sa's */
231 status = list->create_iterator(list, &iterator, TRUE);
232 if (status != SUCCESS)
233 {
234 this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status));
235 /* out of res */
236 return status;
237 }
238
239 /* default status */
240 status = NOT_FOUND;
241
242
243 while (iterator->has_next(iterator))
244 {
245 ike_sa_entry_t *current;
246 bool are_equal = FALSE;
247
248 iterator->current(iterator, (void**)&current);
249 if (current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0) {
250 /* seems to be a half ready ike_sa */
251 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) == ike_sa_id->get_initiator_spi(ike_sa_id))
252 && (ike_sa_id->is_initiator(ike_sa_id) == current->ike_sa_id->is_initiator(current->ike_sa_id)))
253 {
254 this->logger->log(this->logger,CONTROL | MOST,"Found entry by initiator spi %d",ike_sa_id->get_initiator_spi(ike_sa_id));
255 *entry = current;
256 status = SUCCESS;
257 break;
258 }
259 }
260 current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id, &are_equal);
261 if (are_equal)
262 {
263 this->logger->log(this->logger,CONTROL | MOST,"Found entry by full ID");
264 *entry = current;
265 status = SUCCESS;
266 break;
267 }
268 }
269
270 iterator->destroy(iterator);
271 return status;
272 }
273
274 /**
275 * @see private_ike_sa_manager_t.get_entry_by_sa
276 */
277 static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry)
278 {
279 linked_list_t *list = this->ike_sa_list;
280 linked_list_iterator_t *iterator;
281 status_t status;
282
283 status = list->create_iterator(list, &iterator, TRUE);
284 if (status != SUCCESS)
285 {
286 this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status));
287 return status;
288 }
289
290 /* default status */
291 status = NOT_FOUND;
292
293 while (iterator->has_next(iterator))
294 {
295 ike_sa_entry_t *current;
296 iterator->current(iterator, (void**)&current);
297 /* only pointers are compared */
298 if (current->ike_sa == ike_sa)
299 {
300 this->logger->log(this->logger,CONTROL | MOST,"Found entry by pointer");
301 *entry = current;
302 status = SUCCESS;
303 break;
304 }
305 }
306 iterator->destroy(iterator);
307 return status;
308 }
309
310 /**
311 * @see private_ike_sa_manager_s.delete_entry
312 */
313 static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry)
314 {
315 linked_list_t *list = this->ike_sa_list;
316 linked_list_iterator_t *iterator;
317 status_t status;
318
319 status = list->create_iterator(list, &iterator, TRUE);
320
321 if (status != SUCCESS)
322 {
323 this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status));
324 return status;
325 }
326
327 status = NOT_FOUND;
328
329 while (iterator->has_next(iterator))
330 {
331 ike_sa_entry_t *current;
332 iterator->current(iterator, (void**)&current);
333 if (current == entry)
334 {
335 this->logger->log(this->logger,CONTROL | MOST,"Found entry by pointer. Going to delete it.");
336 list->remove(list, iterator);
337 entry->destroy(entry);
338 status = SUCCESS;
339 break;
340 }
341 }
342 iterator->destroy(iterator);
343 return status;
344 }
345
346
347 /**
348 * @see private_ike_sa_manager_t.get_next_spi
349 */
350 static status_t get_next_spi(private_ike_sa_manager_t *this, u_int64_t *spi)
351 {
352 this->next_spi++;
353 if (this->next_spi == 0) {
354 /* our software ran so incredible stable, we have no more
355 * SPIs to give away :-/. */
356 this->logger->log(this->logger,CONTROL | MOST,"No more SPI values available. WOW!");
357 return OUT_OF_RES;
358 }
359 *spi = this->next_spi;
360 return SUCCESS;
361 }
362
363 /**
364 * @see ike_sa_manager_s.checkout_ike_sa
365 */
366 static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_t **ike_sa)
367 {
368 bool responder_spi_set;
369 bool initiator_spi_set;
370 status_t retval;
371
372 /* each access is locked */
373 pthread_mutex_lock(&(this->mutex));
374
375 responder_spi_set = (FALSE != ike_sa_id->get_responder_spi(ike_sa_id));
376 initiator_spi_set = (FALSE != ike_sa_id->get_initiator_spi(ike_sa_id));
377
378 if (initiator_spi_set && responder_spi_set)
379 {
380 /* we SHOULD have an IKE_SA for these SPIs in the list,
381 * if not, we can't handle the request...
382 */
383 ike_sa_entry_t *entry;
384 /* look for the entry */
385 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
386 {
387 /* can we give this ike_sa out to new requesters?*/
388 if (entry->driveout_new_threads)
389 {
390 this->logger->log(this->logger,CONTROL|MORE,"Drive out new thread for existing IKE_SA");
391 /* no we can't */
392 retval = NOT_FOUND;
393 }
394 else
395 {
396 /* is this IKE_SA already checked out ??
397 * are we welcome to get this SA ? */
398 while (entry->checked_out && !entry->driveout_waiting_threads)
399 {
400 /* so wait until we can get it for us.
401 * we register us as waiting.
402 */
403 entry->waiting_threads++;
404 pthread_cond_wait(&(entry->condvar), &(this->mutex));
405 entry->waiting_threads--;
406 }
407
408 /* hm, a deletion request forbids us to get this SA, go home */
409 if (entry->driveout_waiting_threads)
410 {
411 /* we must signal here, others are interested that we leave */
412 pthread_cond_signal(&(entry->condvar));
413 this->logger->log(this->logger,CONTROL|MORE,"Drive out waiting thread for existing IKE_SA");
414 retval = NOT_FOUND;
415 }
416 else
417 {
418 this->logger->log(this->logger,CONTROL|MOST,"IKE SA successfully checked out");
419 /* ok, this IKE_SA is finally ours */
420 entry->checked_out = TRUE;
421 *ike_sa = entry->ike_sa;
422 /* DON'T use return, we must unlock the mutex! */
423 retval = SUCCESS;
424 }
425 }
426 }
427 else
428 {
429 this->logger->log(this->logger,ERROR | MORE,"IKE SA not stored in known IKE_SA list");
430 /* looks like there is no such IKE_SA, better luck next time... */
431 /* DON'T use return, we must unlock the mutex! */
432 retval = NOT_FOUND;
433 }
434 }
435 else if (initiator_spi_set && !responder_spi_set)
436 {
437 /* an IKE_SA_INIT from an another endpoint,
438 * he is the initiator.
439 * For simplicity, we do NOT check for retransmitted
440 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
441 * Request (even a retransmitted one) will result in a
442 * IKE_SA. This could be improved...
443 */
444 u_int64_t responder_spi;
445 ike_sa_entry_t *new_ike_sa_entry;
446
447
448 /* set SPIs, we are the responder */
449 retval = this->get_next_spi(this, &responder_spi);
450
451 if (retval == SUCCESS)
452 { /* next SPI could be created */
453
454 /* we also set arguments spi, so its still valid */
455 ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
456
457 /* create entry */
458 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
459 if (new_ike_sa_entry != NULL)
460 {
461 retval = this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
462 if (retval == SUCCESS)
463 {
464 /* check ike_sa out */
465 this->logger->log(this->logger,CONTROL | MORE ,"IKE_SA added to list of known IKE_SA's");
466 new_ike_sa_entry->checked_out = TRUE;
467 *ike_sa = new_ike_sa_entry->ike_sa;
468
469 /* DON'T use return, we must unlock the mutex! */
470 }
471 else
472 {
473 /* ike_sa_entry could not be added to list*/
474 this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be added to list");
475 }
476 }
477 else
478 {
479 /* new ike_sa_entry could not be created */
480 this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be created");
481 }
482 }
483 else
484 { /* next SPI could not be created */
485 this->logger->log(this->logger,ERROR,"Fatal error: Next SPI could not be created");
486 }
487
488 }
489 else if (!initiator_spi_set && !responder_spi_set)
490 {
491 /* creation of an IKE_SA from local site,
492 * we are the initiator!
493 */
494 u_int64_t initiator_spi;
495 ike_sa_entry_t *new_ike_sa_entry;
496
497 retval = this->get_next_spi(this, &initiator_spi);
498 if (retval == SUCCESS)
499 {
500 /* we also set arguments SPI, so its still valid */
501 ike_sa_id->set_initiator_spi(ike_sa_id, initiator_spi);
502
503 /* create entry */
504 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
505 if (new_ike_sa_entry != NULL)
506 {
507 retval = this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
508
509 if (retval == SUCCESS)
510 {
511 /* check ike_sa out */
512 this->logger->log(this->logger,CONTROL | MORE ,"New IKE_SA created and added to list of known IKE_SA's");
513 new_ike_sa_entry->checked_out = TRUE;
514 *ike_sa = new_ike_sa_entry->ike_sa;
515
516 /* DON'T use return, we must unlock the mutex! */
517 }
518 else
519 {
520 /* ike_sa_entry could not be added to list*/
521 this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be added to list");
522 }
523 }
524 else
525 {
526 /* new ike_sa_entry could not be created */
527 this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be created");
528 }
529 }
530 else
531 {
532 /* next SPI could not be created */
533 this->logger->log(this->logger,ERROR,"Fatal error: Next SPI could not be created");
534 }
535
536
537 }
538 else
539 {
540 /* responder set, initiator not: here is something seriously wrong! */
541 this->logger->log(this->logger,ERROR | MORE,"Invalid IKE_SA SPI's");
542 /* DON'T use return, we must unlock the mutex! */
543 retval = INVALID_ARG;
544 }
545
546 pthread_mutex_unlock(&(this->mutex));
547 /* OK, unlocked... */
548 return retval;
549 }
550
551 /**
552 * Implements ike_sa_manager_t-function checkin.
553 * @see ike_sa_manager_t.checkin.
554 */
555 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
556 {
557 /* to check the SA back in, we look for the pointer of the ike_sa
558 * in all entries.
559 * We can't search by SPI's since the MAY have changed (e.g. on reception
560 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
561 */
562 status_t retval;
563 ike_sa_entry_t *entry;
564
565 pthread_mutex_lock(&(this->mutex));
566
567 /* look for the entry */
568 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
569 {
570 /* ike_sa_id must be updated */
571 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
572 /* signal waiting threads */
573 entry->checked_out = FALSE;
574 this->logger->log(this->logger,CONTROL | MORE,"Checkin of IKE_SA successful.");
575 pthread_cond_signal(&(entry->condvar));
576 retval = SUCCESS;
577 }
578 else
579 {
580 this->logger->log(this->logger,ERROR,"Fatal Error: Tried to checkin nonexisting IKE_SA");
581 /* this SA is no more, this REALLY should not happen */
582 retval = NOT_FOUND;
583 }
584 pthread_mutex_unlock(&(this->mutex));
585 return retval;
586 }
587
588
589 /**
590 * Implements ike_sa_manager_t-function checkin_and_delete.
591 * @see ike_sa_manager_t.checkin_and_delete.
592 */
593 static status_t checkin_and_delete(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
594 {
595 /* deletion is a bit complex, we must garant that no thread is waiting for
596 * this SA.
597 * We take this SA from the list, and start signaling while threads
598 * are in the condvar.
599 */
600 ike_sa_entry_t *entry;
601 status_t retval;
602
603 pthread_mutex_lock(&(this->mutex));
604
605 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
606 {
607 /* mark it, so now new threads can acquire this SA */
608 entry->driveout_new_threads = TRUE;
609 /* additionaly, drive out waiting threads */
610 entry->driveout_waiting_threads = TRUE;
611
612 /* wait until all workers have done their work */
613 while (entry->waiting_threads > 0)
614 {
615 /* let the other threads do some work*/
616 pthread_cond_signal(&(entry->condvar));
617 /* and the nice thing, they will wake us again when their work is done */
618 pthread_cond_wait(&(entry->condvar), &(this->mutex));
619 }
620 /* ok, we are alone now, no threads waiting in the entry's condvar */
621 this->delete_entry(this, entry);
622 this->logger->log(this->logger,CONTROL | MORE,"Checkin and delete of IKE_SA successful");
623 retval = SUCCESS;
624 }
625 else
626 {
627 this->logger->log(this->logger,ERROR,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA");
628 retval = NOT_FOUND;
629 }
630
631 pthread_mutex_unlock(&(this->mutex));
632 return retval;
633 }
634
635 /**
636 * Implements ike_sa_manager_t-function delete.
637 * @see ike_sa_manager_t.delete.
638 */
639 static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
640 {
641 /* deletion is a bit complex, we must garant that no thread is waiting for
642 * this SA.
643 * We take this SA from the list, and start signaling while threads
644 * are in the condvar.
645 */
646 ike_sa_entry_t *entry;
647 status_t retval;
648
649 pthread_mutex_lock(&(this->mutex));
650
651 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
652 {
653 /* mark it, so now new threads can acquire this SA */
654 entry->driveout_new_threads = TRUE;
655
656 /* wait until all workers have done their work */
657 while (entry->waiting_threads)
658 {
659 /* wake up all */
660 pthread_cond_signal(&(entry->condvar));
661 /* and the nice thing, they will wake us again when their work is done */
662 pthread_cond_wait(&(entry->condvar), &(this->mutex));
663 }
664 /* ok, we are alone now, no threads waiting in the entry's condvar */
665 this->delete_entry(this, entry);
666 this->logger->log(this->logger,CONTROL | MORE,"Delete of IKE_SA successful");
667 retval = SUCCESS;
668 }
669 else
670 {
671 this->logger->log(this->logger,ERROR,"Fatal Error: Tried to delete nonexisting IKE_SA");
672 retval = NOT_FOUND;
673 }
674
675 pthread_mutex_unlock(&(this->mutex));
676 return retval;
677 }
678
679 /**
680 * Implements ike_sa_manager_t-function destroy.
681 * @see ike_sa_manager_t.destroy.
682 */
683 static status_t destroy(private_ike_sa_manager_t *this)
684 {
685 /* destroy all list entries */
686 linked_list_t *list = this->ike_sa_list;
687 linked_list_iterator_t *iterator;
688 status_t status;
689
690 pthread_mutex_lock(&(this->mutex));
691
692 this->logger->log(this->logger,CONTROL | MORE,"Going to destroy IKE_SA manager and all managed IKE_SA's");
693
694 /* Step 1: drive out all waiting threads */
695 status = list->create_iterator(list, &iterator, TRUE);
696
697 if (status != SUCCESS)
698 {
699 this->logger->log(this->logger,ERROR,"Fatal Error: Create of iterator while destroying IKE_SA-Manager failed");
700 return FAILED;
701 }
702
703 this->logger->log(this->logger,CONTROL | MOST,"Set driveout flags for all stored IKE_SA's");
704 while (iterator->has_next(iterator))
705 {
706 ike_sa_entry_t *entry;
707 iterator->current(iterator, (void**)&entry);
708 /* do not accept new threads, drive out waiting threads */
709 entry->driveout_new_threads = TRUE;
710 entry->driveout_waiting_threads = TRUE;
711 }
712
713 this->logger->log(this->logger,CONTROL | MOST,"Wait for all threads to leave IKE_SA's");
714 /* Step 2: wait until all are gone */
715 iterator->reset(iterator);
716 while (iterator->has_next(iterator))
717 {
718 ike_sa_entry_t *entry;
719 iterator->current(iterator, (void**)&entry);
720 while (entry->waiting_threads)
721 {
722 /* wake up all */
723 pthread_cond_signal(&(entry->condvar));
724 /* go sleeping until they are gone */
725 pthread_cond_wait(&(entry->condvar), &(this->mutex));
726 }
727 }
728 this->logger->log(this->logger,CONTROL | MOST,"Delete all IKE_SA's");
729 /* Step 3: delete all entries */
730 iterator->reset(iterator);
731 while (iterator->has_next(iterator))
732 {
733 ike_sa_entry_t *entry;
734 iterator->current(iterator, (void**)&entry);
735 this->delete_entry(this, entry);
736 }
737 iterator->destroy(iterator);
738 list->destroy(list);
739 pthread_mutex_unlock(&(this->mutex));
740
741 /* destroy logger at end */
742 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
743
744 allocator_free(this);
745
746 return SUCCESS;
747 }
748
749 /*
750 * Described in header
751 */
752 ike_sa_manager_t *ike_sa_manager_create()
753 {
754 private_ike_sa_manager_t *this = allocator_alloc_thing(private_ike_sa_manager_t);
755
756 /* assign public functions */
757 this->public.destroy = (status_t(*)(ike_sa_manager_t*))destroy;
758 this->public.checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id, ike_sa_t **sa))checkout;
759 this->public.checkin = (status_t(*)(ike_sa_manager_t*, ike_sa_t *sa))checkin;
760 this->public.delete = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id))delete;
761 this->public.checkin_and_delete = (status_t(*)(ike_sa_manager_t*, ike_sa_t *ike_sa))checkin_and_delete;
762
763 /* initialize private functions */
764 this->get_next_spi = get_next_spi;
765 this->get_entry_by_sa = get_entry_by_sa;
766 this->get_entry_by_id = get_entry_by_id;
767 this->delete_entry = delete_entry;
768
769 /* initialize private variables */
770 this->logger = global_logger_manager->create_logger(global_logger_manager,IKE_SA_MANAGER,NULL);
771 if (this->logger == NULL)
772 {
773 allocator_free(this);
774 return NULL;
775 }
776
777 this->ike_sa_list = linked_list_create();
778 if (this->ike_sa_list == NULL)
779 {
780 this->logger->log(this->logger,ERROR,"Fatal error: Failed to create list for managed IKE_SA");
781 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
782 allocator_free(this);
783 return NULL;
784 }
785
786 pthread_mutex_init(&(this->mutex), NULL);
787
788 this->next_spi = 0;
789
790 return (ike_sa_manager_t*)this;
791 }