475898e2631e241e098da1477eddd3813e40ad08
[strongswan.git] / src / charon / sa / ike_sa_manager.c
1 /**
2 * @file ike_sa_manager.c
3 *
4 * @brief Implementation of ike_sa_mananger_t.
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 <daemon.h>
29 #include <sa/ike_sa_id.h>
30 #include <utils/logger.h>
31 #include <utils/logger_manager.h>
32 #include <utils/linked_list.h>
33
34 typedef struct ike_sa_entry_t ike_sa_entry_t;
35
36 /**
37 * An entry in the linked list, contains IKE_SA, locking and lookup data.
38 */
39 struct ike_sa_entry_t {
40 /**
41 * Destructor, also destroys associated ike_sa_t object.
42 */
43 status_t (*destroy) (ike_sa_entry_t *this);
44
45 /**
46 * Number of threads waiting for this ike_sa_t object.
47 */
48 int waiting_threads;
49
50 /**
51 * Condvar where threads can wait until ike_sa_t object is free for use again.
52 */
53 pthread_cond_t condvar;
54
55 /**
56 * Is this ike_sa currently checked out?
57 */
58 bool checked_out;
59
60 /**
61 * Does this SA drives out new threads?
62 */
63 bool driveout_new_threads;
64
65 /**
66 * Does this SA drives out waiting threads?
67 */
68 bool driveout_waiting_threads;
69
70 /**
71 * Identifiaction of an IKE_SA (SPIs).
72 */
73 ike_sa_id_t *ike_sa_id;
74
75 /**
76 * The contained ike_sa_t object.
77 */
78 ike_sa_t *ike_sa;
79 };
80
81 /**
82 * Implementation of ike_sa_entry_t.destroy.
83 */
84 static status_t ike_sa_entry_destroy(ike_sa_entry_t *this)
85 {
86 /* also destroy IKE SA */
87 this->ike_sa->destroy(this->ike_sa);
88 this->ike_sa_id->destroy(this->ike_sa_id);
89 free(this);
90 return SUCCESS;
91 }
92
93 /**
94 * @brief Creates a new entry for the ike_sa_t list.
95 *
96 * This constructor additionaly creates a new and empty SA.
97 *
98 * @param ike_sa_id The associated ike_sa_id_t, will be cloned
99 * @return ike_sa_entry_t object
100 */
101 static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id)
102 {
103 ike_sa_entry_t *this = malloc_thing(ike_sa_entry_t);
104
105 /* destroy function */
106 this->destroy = ike_sa_entry_destroy;
107
108 this->waiting_threads = 0;
109 pthread_cond_init(&(this->condvar), NULL);
110
111 /* we set checkout flag when we really give it out */
112 this->checked_out = FALSE;
113 this->driveout_new_threads = FALSE;
114 this->driveout_waiting_threads = FALSE;
115
116 /* ike_sa_id is always cloned */
117 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
118
119 /* create new ike_sa */
120 this->ike_sa = ike_sa_create(ike_sa_id);
121
122 return this;
123 }
124
125
126 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t;
127
128 /**
129 * Additional private members of ike_sa_manager_t.
130 */
131 struct private_ike_sa_manager_t {
132 /**
133 * Public interface of ike_sa_manager_t.
134 */
135 ike_sa_manager_t public;
136
137 /**
138 * @brief Get next spi.
139 *
140 * We give out SPIs from a pseudo random source
141 *
142 * @param this the ike_sa_manager
143 * @return the next spi
144 */
145 u_int64_t (*get_next_spi) (private_ike_sa_manager_t *this);
146
147 /**
148 * @brief Find the ike_sa_entry_t object in the list by SPIs.
149 *
150 * This function simply iterates over the linked list. A hash-table
151 * would be more efficient when storing a lot of IKE_SAs...
152 *
153 * @param this calling object
154 * @param ike_sa_id id of the ike_sa, containing SPIs
155 * @param[out] entry pointer to set to the found entry
156 * @return
157 * - SUCCESS when found,
158 * - NOT_FOUND when no such ike_sa_id in list
159 */
160 status_t (*get_entry_by_id) (private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry);
161
162 /**
163 * @brief Find the ike_sa_entry_t in the list by pointer to SA.
164 *
165 * This function simply iterates over the linked list. A hash-table
166 * would be more efficient when storing a lot of IKE_SAs...
167 *
168 * @param this calling object
169 * @param ike_sa pointer to the ike_sa
170 * @param[out] entry pointer to set to the found entry
171 * @return
172 * - SUCCESS when found,
173 * - NOT_FOUND when no such ike_sa_id in list
174 */
175 status_t (*get_entry_by_sa) (private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry);
176
177 /**
178 * @brief Felete an entry from the linked list.
179 *
180 * @param this calling object
181 * @param entry entry to delete
182 * @return
183 * - SUCCESS when found,
184 * - NOT_FOUND when no such ike_sa_id in list
185 */
186 status_t (*delete_entry) (private_ike_sa_manager_t *this, ike_sa_entry_t *entry);
187
188 /**
189 * Lock for exclusivly accessing the manager.
190 */
191 pthread_mutex_t mutex;
192
193 /**
194 * Logger used for this IKE SA Manager.
195 */
196 logger_t *logger;
197
198 /**
199 * Linked list with entries for the ike_sa_t objects.
200 */
201 linked_list_t *ike_sa_list;
202
203 /**
204 * A randomizer, to get random SPIs for our side
205 */
206 randomizer_t *randomizer;
207 };
208
209 /**
210 * Implementation of private_ike_sa_manager_t.get_entry_by_id.
211 */
212 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)
213 {
214 linked_list_t *list = this->ike_sa_list;
215 iterator_t *iterator;
216 status_t status;
217
218 /* create iterator over list of ike_sa's */
219 iterator = list->create_iterator(list, TRUE);
220
221 /* default status */
222 status = NOT_FOUND;
223
224 while (iterator->has_next(iterator))
225 {
226 ike_sa_entry_t *current;
227
228 iterator->current(iterator, (void**)&current);
229 if (current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0)
230 {
231 /* seems to be a half ready ike_sa */
232 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) == ike_sa_id->get_initiator_spi(ike_sa_id))
233 && (ike_sa_id->is_initiator(ike_sa_id) == current->ike_sa_id->is_initiator(current->ike_sa_id)))
234 {
235 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by initiator spi %d",
236 ike_sa_id->get_initiator_spi(ike_sa_id));
237 *entry = current;
238 status = SUCCESS;
239 break;
240 }
241 }
242 else if (ike_sa_id->get_responder_spi(ike_sa_id) == 0)
243 {
244 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) == ike_sa_id->get_initiator_spi(ike_sa_id))
245 && (ike_sa_id->is_initiator(ike_sa_id) == current->ike_sa_id->is_initiator(current->ike_sa_id)))
246 {
247 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by initiator spi %d",
248 ike_sa_id->get_initiator_spi(ike_sa_id));
249 *entry = current;
250 status = SUCCESS;
251 break;
252 }
253 }
254 if (current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id))
255 {
256 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by full ID");
257 *entry = current;
258 status = SUCCESS;
259 break;
260 }
261 }
262
263 iterator->destroy(iterator);
264 return status;
265 }
266
267 /**
268 * Implementation of private_ike_sa_manager_t.get_entry_by_sa.
269 */
270 static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry)
271 {
272 linked_list_t *list = this->ike_sa_list;
273 iterator_t *iterator;
274 status_t status;
275
276 iterator = list->create_iterator(list, TRUE);
277
278 /* default status */
279 status = NOT_FOUND;
280
281 while (iterator->has_next(iterator))
282 {
283 ike_sa_entry_t *current;
284 iterator->current(iterator, (void**)&current);
285 /* only pointers are compared */
286 if (current->ike_sa == ike_sa)
287 {
288 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by pointer");
289 *entry = current;
290 status = SUCCESS;
291 break;
292 }
293 }
294 iterator->destroy(iterator);
295
296 return status;
297 }
298
299 /**
300 * Implementation of private_ike_sa_manager_s.delete_entry.
301 */
302 static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry)
303 {
304 linked_list_t *list = this->ike_sa_list;
305 iterator_t *iterator;
306 status_t status;
307
308 iterator = list->create_iterator(list, TRUE);
309
310 status = NOT_FOUND;
311
312 while (iterator->has_next(iterator))
313 {
314 ike_sa_entry_t *current;
315 iterator->current(iterator, (void**)&current);
316 if (current == entry)
317 {
318 this->logger->log(this->logger, CONTROL|LEVEL2, "Found entry by pointer. Going to delete it.");
319 iterator->remove(iterator);
320 entry->destroy(entry);
321 status = SUCCESS;
322 break;
323 }
324 }
325 iterator->destroy(iterator);
326 return status;
327 }
328
329
330 /**
331 * Implementation of private_ike_sa_manager_t.get_next_spi.
332 */
333 static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
334 {
335 u_int64_t spi;
336
337 this->randomizer->get_pseudo_random_bytes(this->randomizer, 8, (u_int8_t*)&spi);
338
339 return spi;
340 }
341
342 /**
343 * Implementation of of ike_sa_manager.create_and_checkout.
344 */
345 static void create_and_checkout(private_ike_sa_manager_t *this,ike_sa_t **ike_sa)
346 {
347 u_int64_t initiator_spi;
348 ike_sa_entry_t *new_ike_sa_entry;
349 ike_sa_id_t *new_ike_sa_id;
350
351 initiator_spi = this->get_next_spi(this);
352 new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
353 new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
354
355 /* create entry */
356 new_ike_sa_entry = ike_sa_entry_create(new_ike_sa_id);
357 new_ike_sa_id->destroy(new_ike_sa_id);
358
359 /* each access is locked */
360 pthread_mutex_lock(&(this->mutex));
361
362 this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
363
364 /* check ike_sa out */
365 this->logger->log(this->logger, CONTROL|LEVEL1, "New IKE_SA created and added to list of known IKE_SA's");
366 new_ike_sa_entry->checked_out = TRUE;
367 *ike_sa = new_ike_sa_entry->ike_sa;
368
369 pthread_mutex_unlock(&(this->mutex));
370 }
371
372 /**
373 * Implementation of of ike_sa_manager.checkout.
374 */
375 static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_t **ike_sa)
376 {
377 bool responder_spi_set;
378 bool initiator_spi_set;
379 bool original_initiator;
380 status_t retval;
381
382 /* each access is locked */
383 pthread_mutex_lock(&(this->mutex));
384
385 responder_spi_set = (FALSE != ike_sa_id->get_responder_spi(ike_sa_id));
386 initiator_spi_set = (FALSE != ike_sa_id->get_initiator_spi(ike_sa_id));
387 original_initiator = ike_sa_id->is_initiator(ike_sa_id);
388
389 if ((initiator_spi_set && responder_spi_set) ||
390 ((initiator_spi_set && !responder_spi_set) && (original_initiator)))
391 {
392 /* we SHOULD have an IKE_SA for these SPIs in the list,
393 * if not, we can't handle the request...
394 */
395 ike_sa_entry_t *entry;
396 /* look for the entry */
397 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
398 {
399 /* can we give this ike_sa out to new requesters?*/
400 if (entry->driveout_new_threads)
401 {
402 this->logger->log(this->logger, CONTROL|LEVEL1, "Drive out new thread for existing IKE_SA");
403 /* no we can't */
404 retval = NOT_FOUND;
405 }
406 else
407 {
408 /* is this IKE_SA already checked out ??
409 * are we welcome to get this SA ? */
410 while (entry->checked_out && !entry->driveout_waiting_threads)
411 {
412 /* so wait until we can get it for us.
413 * we register us as waiting.
414 */
415 entry->waiting_threads++;
416 pthread_cond_wait(&(entry->condvar), &(this->mutex));
417 entry->waiting_threads--;
418 }
419
420 /* hm, a deletion request forbids us to get this SA, go home */
421 if (entry->driveout_waiting_threads)
422 {
423 /* we must signal here, others are interested that we leave */
424 pthread_cond_signal(&(entry->condvar));
425 this->logger->log(this->logger, CONTROL|LEVEL1, "Drive out waiting thread for existing IKE_SA");
426 retval = NOT_FOUND;
427 }
428 else
429 {
430 this->logger->log(this->logger, CONTROL|LEVEL2, "IKE SA successfully checked out");
431 /* ok, this IKE_SA is finally ours */
432 entry->checked_out = TRUE;
433 *ike_sa = entry->ike_sa;
434 /* DON'T use return, we must unlock the mutex! */
435 retval = SUCCESS;
436 }
437 }
438 }
439 else
440 {
441 this->logger->log(this->logger, ERROR|LEVEL1, "IKE SA not stored in known IKE_SA list");
442 /* looks like there is no such IKE_SA, better luck next time... */
443 /* DON'T use return, we must unlock the mutex! */
444 retval = NOT_FOUND;
445 }
446 }
447 else if ((initiator_spi_set && !responder_spi_set) && (!original_initiator))
448 {
449 /* an IKE_SA_INIT from an another endpoint,
450 * he is the initiator.
451 * For simplicity, we do NOT check for retransmitted
452 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
453 * Request (even a retransmitted one) will result in a
454 * IKE_SA. This could be improved...
455 */
456 u_int64_t responder_spi;
457 ike_sa_entry_t *new_ike_sa_entry;
458
459
460 /* set SPIs, we are the responder */
461 responder_spi = this->get_next_spi(this);
462
463 /* we also set arguments spi, so its still valid */
464 ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
465
466 /* create entry */
467 new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
468
469 this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
470
471 /* check ike_sa out */
472 this->logger->log(this->logger, CONTROL|LEVEL1 ,"IKE_SA added to list of known IKE_SA's");
473 new_ike_sa_entry->checked_out = TRUE;
474 *ike_sa = new_ike_sa_entry->ike_sa;
475
476 retval = CREATED;
477 }
478 else
479 {
480 /* responder set, initiator not: here is something seriously wrong! */
481 this->logger->log(this->logger, ERROR|LEVEL1, "Invalid IKE_SA SPI's");
482 /* DON'T use return, we must unlock the mutex! */
483 retval = INVALID_ARG;
484 }
485
486 pthread_mutex_unlock(&(this->mutex));
487 /* OK, unlocked... */
488 return retval;
489 }
490
491 /**
492 * Implementation of of ike_sa_manager.checkout_by_hosts.
493 */
494 static status_t checkout_by_hosts(private_ike_sa_manager_t *this, host_t *me, host_t *other, ike_sa_t **ike_sa)
495 {
496 iterator_t *iterator;
497 ike_sa_id_t *ike_sa_id = NULL;
498
499 pthread_mutex_lock(&(this->mutex));
500
501 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
502 while (iterator->has_next(iterator))
503 {
504 ike_sa_entry_t *current;
505 host_t *sa_me, *sa_other;
506
507 iterator->current(iterator, (void**)&current);
508 sa_me = current->ike_sa->get_my_host(current->ike_sa);
509 sa_other = current->ike_sa->get_other_host(current->ike_sa);
510
511 /* one end may be default/any, but not both */
512 if (me->is_anyaddr(me))
513 {
514 if (other->is_anyaddr(other))
515 {
516 break;
517 }
518 if (other->equals(other, sa_other))
519 {
520 /* other matches */
521 ike_sa_id = current->ike_sa_id;
522 }
523 }
524 else if (other->is_anyaddr(other))
525 {
526 if (me->equals(me, sa_me))
527 {
528 /* ME matches */
529 ike_sa_id = current->ike_sa_id;
530 }
531 }
532 else
533 {
534 if (me->equals(me, sa_me) && other->equals(other, sa_other))
535 {
536 /* both matches */
537 ike_sa_id = current->ike_sa_id;
538 }
539 }
540 }
541 iterator->destroy(iterator);
542 pthread_mutex_unlock(&(this->mutex));
543
544 if (ike_sa_id)
545 {
546 /* checkout is done in the checkout function, since its rather complex */
547 return checkout(this, ike_sa_id, ike_sa);
548 }
549 return NOT_FOUND;
550 }
551
552 /**
553 * Implementation of ike_sa_manager_t.get_ike_sa_list.
554 */
555 linked_list_t *get_ike_sa_list(private_ike_sa_manager_t* this)
556 {
557 linked_list_t *list;
558 iterator_t *iterator;
559
560 pthread_mutex_lock(&(this->mutex));
561
562 list = linked_list_create();
563 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
564 while (iterator->has_next(iterator))
565 {
566 ike_sa_entry_t *entry;
567 iterator->current(iterator, (void**)&entry);
568 list->insert_last(list, (void*)entry->ike_sa_id->clone(entry->ike_sa_id));
569 }
570 iterator->destroy(iterator);
571
572 pthread_mutex_unlock(&(this->mutex));
573 return list;
574 }
575
576 /**
577 * Implementation of ike_sa_manager_t.get_ike_sa_list_by_name.
578 */
579 linked_list_t *get_ike_sa_list_by_name(private_ike_sa_manager_t* this, const char *name)
580 {
581 linked_list_t *list;
582 iterator_t *iterator;
583
584 pthread_mutex_lock(&(this->mutex));
585
586 list = linked_list_create();
587 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
588 while (iterator->has_next(iterator))
589 {
590 ike_sa_entry_t *entry;
591 connection_t *connection;
592
593 iterator->current(iterator, (void**)&entry);
594 connection = entry->ike_sa->get_connection(entry->ike_sa);
595 if (strcmp(name, connection->get_name(connection)) == 0)
596 {
597 list->insert_last(list, (void*)entry->ike_sa_id->clone(entry->ike_sa_id));
598 }
599 }
600 iterator->destroy(iterator);
601
602 pthread_mutex_unlock(&(this->mutex));
603 return list;
604 }
605
606 /**
607 * Implementation of ike_sa_manager_t.log_status.
608 */
609 static void log_status(private_ike_sa_manager_t* this, logger_t* logger, char* name)
610 {
611 iterator_t *iterator;
612
613 pthread_mutex_lock(&(this->mutex));
614
615 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
616 while (iterator->has_next(iterator))
617 {
618 ike_sa_entry_t *entry;
619 iterator->current(iterator, (void**)&entry);
620 entry->ike_sa->log_status(entry->ike_sa, logger, name);
621 }
622 iterator->destroy(iterator);
623
624 pthread_mutex_unlock(&(this->mutex));
625 }
626
627 /**
628 * Implementation of ike_sa_manager_t.checkin.
629 */
630 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
631 {
632 /* to check the SA back in, we look for the pointer of the ike_sa
633 * in all entries.
634 * We can't search by SPI's since the MAY have changed (e.g. on reception
635 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
636 */
637 status_t retval;
638 ike_sa_entry_t *entry;
639
640 pthread_mutex_lock(&(this->mutex));
641
642 /* look for the entry */
643 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
644 {
645 /* ike_sa_id must be updated */
646 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
647 /* signal waiting threads */
648 entry->checked_out = FALSE;
649 this->logger->log(this->logger, CONTROL|LEVEL1, "Checkin of IKE_SA successful.");
650 pthread_cond_signal(&(entry->condvar));
651 retval = SUCCESS;
652 }
653 else
654 {
655 this->logger->log(this->logger, ERROR, "Tried to checkin nonexisting IKE_SA");
656 /* this SA is no more, this REALLY should not happen */
657 retval = NOT_FOUND;
658 }
659 pthread_mutex_unlock(&(this->mutex));
660 return retval;
661 }
662
663
664 /**
665 * Implementation of ike_sa_manager_t.checkin_and_destroy.
666 */
667 static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
668 {
669 /* deletion is a bit complex, we must garant that no thread is waiting for
670 * this SA.
671 * We take this SA from the list, and start signaling while threads
672 * are in the condvar.
673 */
674 ike_sa_entry_t *entry;
675 status_t retval;
676
677 pthread_mutex_lock(&(this->mutex));
678
679 if (this->get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
680 {
681 /* mark it, so now new threads can acquire this SA */
682 entry->driveout_new_threads = TRUE;
683 /* additionaly, drive out waiting threads */
684 entry->driveout_waiting_threads = TRUE;
685
686 /* wait until all workers have done their work */
687 while (entry->waiting_threads)
688 {
689 /* let the other threads leave the manager */
690 pthread_cond_broadcast(&(entry->condvar));
691 /* and the nice thing, they will wake us again when their work is done */
692 pthread_cond_wait(&(entry->condvar), &(this->mutex));
693 }
694 /* ok, we are alone now, no threads waiting in the entry's condvar */
695 this->delete_entry(this, entry);
696 this->logger->log(this->logger, CONTROL|LEVEL1, "Checkin and destroy of IKE_SA successful");
697 retval = SUCCESS;
698 }
699 else
700 {
701 this->logger->log(this->logger,ERROR, "Tried to checkin and delete nonexisting IKE_SA");
702 retval = NOT_FOUND;
703 }
704
705 pthread_mutex_unlock(&(this->mutex));
706 return retval;
707 }
708
709 /**
710 * Implementation of ike_sa_manager_t.delete.
711 */
712 static status_t delete_(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
713 {
714 /* deletion is a bit complex, we must garant that no thread is waiting for
715 * this SA.
716 * We take this SA from the list, and start signaling while threads
717 * are in the condvar.
718 */
719 ike_sa_entry_t *entry;
720 status_t retval;
721
722 pthread_mutex_lock(&(this->mutex));
723
724 if (this->get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
725 {
726 /* we try a delete. If it succeeds, our job is done here. The
727 * other peer will reply, and the IKE SA gets the finally deleted...
728 */
729 if (entry->ike_sa->delete(entry->ike_sa) == SUCCESS)
730 {
731 this->logger->log(this->logger, CONTROL|LEVEL1, "Initiated delete for IKE_SA");
732 }
733 /* but if the IKE SA is not in a state where the deletion is negotiated with
734 * the other peer, we can destroy the IKE SA on our own. For this, we must
735 * be sure that really NO other threads are waiting for this SA...
736 */
737 else
738 {
739 /* mark it, so now new threads can acquire this SA */
740 entry->driveout_new_threads = TRUE;
741 /* wait until all workers have done their work */
742 while (entry->waiting_threads)
743 {
744 /* wake up all */
745 pthread_cond_broadcast(&(entry->condvar));
746 /* and the nice thing, they will wake us again when their work is done */
747 pthread_cond_wait(&(entry->condvar), &(this->mutex));
748 }
749 /* ok, we are alone now, no threads waiting in the entry's condvar */
750 this->delete_entry(this, entry);
751 this->logger->log(this->logger, CONTROL|LEVEL1, "Destroyed IKE_SA");
752 }
753 retval = SUCCESS;
754 }
755 else
756 {
757 this->logger->log(this->logger,ERROR, "Tried to delete nonexisting IKE_SA");
758 retval = NOT_FOUND;
759 }
760
761 pthread_mutex_unlock(&(this->mutex));
762 return retval;
763 }
764
765 /**
766 * Implementation of ike_sa_manager_t.destroy.
767 */
768 static void destroy(private_ike_sa_manager_t *this)
769 {
770 /* destroy all list entries */
771 linked_list_t *list = this->ike_sa_list;
772 iterator_t *iterator;
773 ike_sa_entry_t *entry;
774
775 pthread_mutex_lock(&(this->mutex));
776
777 this->logger->log(this->logger,CONTROL | LEVEL1,"Going to destroy IKE_SA manager and all managed IKE_SA's");
778
779 /* Step 1: drive out all waiting threads */
780 iterator = list->create_iterator(list, TRUE);
781
782 this->logger->log(this->logger,CONTROL | LEVEL2,"Set driveout flags for all stored IKE_SA's");
783 while (iterator->has_next(iterator))
784 {
785 iterator->current(iterator, (void**)&entry);
786 /* do not accept new threads, drive out waiting threads */
787 entry->driveout_new_threads = TRUE;
788 entry->driveout_waiting_threads = TRUE;
789 }
790
791 this->logger->log(this->logger,CONTROL | LEVEL2,"Wait for all threads to leave IKE_SA's");
792 /* Step 2: wait until all are gone */
793 iterator->reset(iterator);
794 while (iterator->has_next(iterator))
795 {
796 iterator->current(iterator, (void**)&entry);
797 while (entry->waiting_threads)
798 {
799 /* wake up all */
800 pthread_cond_broadcast(&(entry->condvar));
801 /* go sleeping until they are gone */
802 pthread_cond_wait(&(entry->condvar), &(this->mutex));
803 }
804 }
805 this->logger->log(this->logger,CONTROL | LEVEL2,"Delete all IKE_SA's");
806 /* Step 3: delete all entries */
807 iterator->destroy(iterator);
808
809 while (list->get_count(list) > 0)
810 {
811 list->get_first(list, (void**)&entry);
812 this->delete_entry(this, entry);
813 }
814 list->destroy(list);
815 this->logger->log(this->logger,CONTROL | LEVEL2,"IKE_SA's deleted");
816 pthread_mutex_unlock(&(this->mutex));
817
818 this->randomizer->destroy(this->randomizer);
819
820 free(this);
821 }
822
823 /*
824 * Described in header.
825 */
826 ike_sa_manager_t *ike_sa_manager_create()
827 {
828 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
829
830 /* assign public functions */
831 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
832 this->public.create_and_checkout = (void(*)(ike_sa_manager_t*,ike_sa_t**))create_and_checkout;
833 this->public.checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t*,ike_sa_t**))checkout;
834 this->public.checkout_by_hosts = (status_t(*)(ike_sa_manager_t*,host_t*,host_t*,ike_sa_t**))checkout_by_hosts;
835 this->public.get_ike_sa_list = (linked_list_t*(*)(ike_sa_manager_t*))get_ike_sa_list;
836 this->public.get_ike_sa_list_by_name = (linked_list_t*(*)(ike_sa_manager_t*,const char*))get_ike_sa_list_by_name;
837 this->public.log_status = (void(*)(ike_sa_manager_t*,logger_t*,char*))log_status;
838 this->public.checkin = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
839 this->public.delete = (status_t(*)(ike_sa_manager_t*,ike_sa_id_t*))delete_;
840 this->public.checkin_and_destroy = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
841
842 /* initialize private functions */
843 this->get_next_spi = get_next_spi;
844 this->get_entry_by_sa = get_entry_by_sa;
845 this->get_entry_by_id = get_entry_by_id;
846 this->delete_entry = delete_entry;
847
848 /* initialize private variables */
849 this->logger = logger_manager->get_logger(logger_manager, IKE_SA_MANAGER);
850
851 this->ike_sa_list = linked_list_create();
852
853 pthread_mutex_init(&(this->mutex), NULL);
854
855 this->randomizer = randomizer_create();
856
857 return (ike_sa_manager_t*)this;
858 }