0945b6b72f471926bc5899a8eb48fcd3a55f3e07
[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-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <pthread.h>
25 #include <string.h>
26
27 #include "ike_sa_manager.h"
28
29 #include <daemon.h>
30 #include <sa/ike_sa_id.h>
31 #include <bus/bus.h>
32 #include <utils/linked_list.h>
33
34 typedef struct entry_t entry_t;
35
36 /**
37 * An entry in the linked list, contains IKE_SA, locking and lookup data.
38 */
39 struct entry_t {
40
41 /**
42 * Number of threads waiting for this ike_sa_t object.
43 */
44 int waiting_threads;
45
46 /**
47 * Condvar where threads can wait until ike_sa_t object is free for use again.
48 */
49 pthread_cond_t condvar;
50
51 /**
52 * Is this ike_sa currently checked out?
53 */
54 bool checked_out;
55
56 /**
57 * Does this SA drives out new threads?
58 */
59 bool driveout_new_threads;
60
61 /**
62 * Does this SA drives out waiting threads?
63 */
64 bool driveout_waiting_threads;
65
66 /**
67 * Identifiaction of an IKE_SA (SPIs).
68 */
69 ike_sa_id_t *ike_sa_id;
70
71 /**
72 * The contained ike_sa_t object.
73 */
74 ike_sa_t *ike_sa;
75 };
76
77 /**
78 * Implementation of entry_t.destroy.
79 */
80 static status_t entry_destroy(entry_t *this)
81 {
82 /* also destroy IKE SA */
83 this->ike_sa->destroy(this->ike_sa);
84 this->ike_sa_id->destroy(this->ike_sa_id);
85 free(this);
86 return SUCCESS;
87 }
88
89 /**
90 * Creates a new entry for the ike_sa_t list.
91 */
92 static entry_t *entry_create(ike_sa_id_t *ike_sa_id)
93 {
94 entry_t *this = malloc_thing(entry_t);
95
96 this->waiting_threads = 0;
97 pthread_cond_init(&(this->condvar), NULL);
98
99 /* we set checkout flag when we really give it out */
100 this->checked_out = FALSE;
101 this->driveout_new_threads = FALSE;
102 this->driveout_waiting_threads = FALSE;
103
104 /* ike_sa_id is always cloned */
105 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
106
107 /* create new ike_sa */
108 this->ike_sa = ike_sa_create(ike_sa_id);
109
110 return this;
111 }
112
113
114 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t;
115
116 /**
117 * Additional private members of ike_sa_manager_t.
118 */
119 struct private_ike_sa_manager_t {
120 /**
121 * Public interface of ike_sa_manager_t.
122 */
123 ike_sa_manager_t public;
124
125 /**
126 * Lock for exclusivly accessing the manager.
127 */
128 pthread_mutex_t mutex;
129
130 /**
131 * Linked list with entries for the ike_sa_t objects.
132 */
133 linked_list_t *ike_sa_list;
134
135 /**
136 * A randomizer, to get random SPIs for our side
137 */
138 randomizer_t *randomizer;
139 };
140
141 /**
142 * Implementation of private_ike_sa_manager_t.get_entry_by_id.
143 */
144 static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, entry_t **entry)
145 {
146 linked_list_t *list = this->ike_sa_list;
147 iterator_t *iterator;
148 entry_t *current;
149 status_t status;
150
151 /* create iterator over list of ike_sa's */
152 iterator = list->create_iterator(list, TRUE);
153
154 /* default status */
155 status = NOT_FOUND;
156
157 while (iterator->iterate(iterator, (void**)&current))
158 {
159 if (current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id))
160 {
161 DBG2(SIG_DBG_MGR, "found entry by both SPIs");
162 *entry = current;
163 status = SUCCESS;
164 break;
165 }
166 if (ike_sa_id->get_responder_spi(ike_sa_id) == 0 ||
167 current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0)
168 {
169 /* seems to be a half ready ike_sa */
170 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) ==
171 ike_sa_id->get_initiator_spi(ike_sa_id)) &&
172 (current->ike_sa_id->is_initiator(ike_sa_id) ==
173 ike_sa_id->is_initiator(current->ike_sa_id)))
174 {
175 DBG2(SIG_DBG_MGR, "found entry by initiator SPI");
176 *entry = current;
177 status = SUCCESS;
178 break;
179 }
180 }
181 }
182
183 iterator->destroy(iterator);
184 return status;
185 }
186
187 /**
188 * Implementation of private_ike_sa_manager_t.get_entry_by_sa.
189 */
190 static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, entry_t **entry)
191 {
192 linked_list_t *list = this->ike_sa_list;
193 iterator_t *iterator;
194 status_t status;
195
196 iterator = list->create_iterator(list, TRUE);
197
198 /* default status */
199 status = NOT_FOUND;
200
201 while (iterator->has_next(iterator))
202 {
203 entry_t *current;
204 iterator->current(iterator, (void**)&current);
205 /* only pointers are compared */
206 if (current->ike_sa == ike_sa)
207 {
208 DBG2(SIG_DBG_MGR, "found entry by pointer");
209 *entry = current;
210 status = SUCCESS;
211 break;
212 }
213 }
214 iterator->destroy(iterator);
215
216 return status;
217 }
218
219 /**
220 * Implementation of private_ike_sa_manager_s.delete_entry.
221 */
222 static status_t delete_entry(private_ike_sa_manager_t *this, entry_t *entry)
223 {
224 linked_list_t *list = this->ike_sa_list;
225 iterator_t *iterator;
226 status_t status;
227
228 iterator = list->create_iterator(list, TRUE);
229
230 status = NOT_FOUND;
231
232 while (iterator->has_next(iterator))
233 {
234 entry_t *current;
235 iterator->current(iterator, (void**)&current);
236 if (current == entry)
237 {
238 /* mark it, so now new threads can get this entry */
239 entry->driveout_new_threads = TRUE;
240 /* wait until all workers have done their work */
241 while (entry->waiting_threads)
242 {
243 /* wake up all */
244 pthread_cond_broadcast(&(entry->condvar));
245 /* they will wake us again when their work is done */
246 pthread_cond_wait(&(entry->condvar), &(this->mutex));
247 }
248
249 DBG2(SIG_DBG_MGR, "found entry by pointer, deleting it");
250 iterator->remove(iterator);
251 entry_destroy(entry);
252 status = SUCCESS;
253 break;
254 }
255 }
256 iterator->destroy(iterator);
257 return status;
258 }
259
260 /**
261 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
262 * acquireable
263 */
264 static bool wait_for_entry(private_ike_sa_manager_t *this, entry_t *entry)
265 {
266 if (entry->driveout_new_threads)
267 {
268 /* we are not allowed to get this */
269 return FALSE;
270 }
271 while (entry->checked_out && !entry->driveout_waiting_threads)
272 {
273 /* so wait until we can get it for us.
274 * we register us as waiting. */
275 entry->waiting_threads++;
276 pthread_cond_wait(&(entry->condvar), &(this->mutex));
277 entry->waiting_threads--;
278 }
279 /* hm, a deletion request forbids us to get this SA, get next one */
280 if (entry->driveout_waiting_threads)
281 {
282 /* we must signal here, others may be waiting on it, too */
283 pthread_cond_signal(&(entry->condvar));
284 return FALSE;
285 }
286 return TRUE;
287 }
288
289 /**
290 * Implementation of private_ike_sa_manager_t.get_next_spi.
291 */
292 static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
293 {
294 u_int64_t spi;
295
296 this->randomizer->get_pseudo_random_bytes(this->randomizer, 8, (u_int8_t*)&spi);
297
298 return spi;
299 }
300
301 /**
302 * Implementation of of ike_sa_manager.checkout_by_id.
303 */
304 static ike_sa_t* checkout_by_id(private_ike_sa_manager_t *this,
305 host_t *my_host,
306 host_t *other_host,
307 identification_t *my_id,
308 identification_t *other_id)
309 {
310 iterator_t *iterator;
311 ike_sa_t *ike_sa = NULL;
312
313 pthread_mutex_lock(&(this->mutex));
314
315 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
316 while (iterator->has_next(iterator))
317 {
318 entry_t *entry;
319 identification_t *found_my_id, *found_other_id;
320 host_t *found_my_host, *found_other_host;
321 int wc;
322
323 iterator->current(iterator, (void**)&entry);
324 if (!wait_for_entry(this, entry))
325 {
326 continue;
327 }
328
329 found_my_id = entry->ike_sa->get_my_id(entry->ike_sa);
330 found_other_id = entry->ike_sa->get_other_id(entry->ike_sa);
331 found_my_host = entry->ike_sa->get_my_host(entry->ike_sa);
332 found_other_host = entry->ike_sa->get_other_host(entry->ike_sa);
333
334 if (found_my_id->get_type(found_my_id) == ID_ANY &&
335 found_other_id->get_type(found_other_id) == ID_ANY)
336 {
337 /* IKE_SA has no IDs yet, so we can't use it */
338 continue;
339 }
340
341 /* compare ID and hosts. Supplied ID may contain wildcards, and IP
342 * may be %any. */
343 if ((found_my_host->is_anyaddr(found_my_host) ||
344 my_host->ip_equals(my_host, found_my_host)) &&
345 (found_other_host->is_anyaddr(found_other_host) ||
346 other_host->ip_equals(other_host, found_other_host)) &&
347 found_my_id->matches(found_my_id, my_id, &wc) &&
348 found_other_id->matches(found_other_id, other_id, &wc))
349 {
350 /* looks good, we take this one */
351 DBG2(SIG_DBG_MGR, "found an existing IKE_SA for %H[%D]...%H[%D]",
352 my_host, other_host, my_id, other_id);
353 entry->checked_out = TRUE;
354 ike_sa = entry->ike_sa;
355 }
356 }
357 iterator->destroy(iterator);
358
359 if (!ike_sa)
360 {
361 u_int64_t initiator_spi;
362 entry_t *new_entry;
363 ike_sa_id_t *new_ike_sa_id;
364
365 initiator_spi = get_next_spi(this);
366 new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
367 new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
368
369 /* create entry */
370 new_entry = entry_create(new_ike_sa_id);
371 DBG2(SIG_DBG_MGR, "created IKE_SA: %J", new_ike_sa_id);
372 new_ike_sa_id->destroy(new_ike_sa_id);
373
374 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
375
376 /* check ike_sa out */
377 DBG2(SIG_DBG_MGR, "new IKE_SA created for IDs [%D]...[%D]", my_id, other_id);
378 new_entry->checked_out = TRUE;
379 ike_sa = new_entry->ike_sa;
380 }
381 pthread_mutex_unlock(&(this->mutex));
382 charon->bus->set_sa(charon->bus, ike_sa);
383 return ike_sa;
384 }
385
386 /**
387 * Implementation of of ike_sa_manager.checkout.
388 */
389 static ike_sa_t* checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
390 {
391 bool responder_spi_set;
392 bool initiator_spi_set;
393 bool original_initiator;
394 ike_sa_t *ike_sa = NULL;
395
396 DBG2(SIG_DBG_MGR, "checkout IKE_SA: %J", ike_sa_id);
397
398 DBG2(SIG_DBG_MGR, "%d IKE_SAs in manager",
399 this->ike_sa_list->get_count(this->ike_sa_list));
400
401 /* each access is locked */
402 pthread_mutex_lock(&(this->mutex));
403
404 responder_spi_set = ike_sa_id->get_responder_spi(ike_sa_id);
405 initiator_spi_set = ike_sa_id->get_initiator_spi(ike_sa_id);
406 original_initiator = ike_sa_id->is_initiator(ike_sa_id);
407
408 if ((initiator_spi_set && responder_spi_set) ||
409 ((initiator_spi_set && !responder_spi_set) && (original_initiator)))
410 {
411 /* we SHOULD have an IKE_SA for these SPIs in the list,
412 * if not, we can't handle the request...
413 */
414 entry_t *entry;
415 /* look for the entry */
416 if (get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
417 {
418 if (wait_for_entry(this, entry))
419 {
420 DBG2(SIG_DBG_MGR, "IKE_SA successfully checked out");
421 /* ok, this IKE_SA is finally ours */
422 entry->checked_out = TRUE;
423 ike_sa = entry->ike_sa;
424 }
425 else
426 {
427 DBG2(SIG_DBG_MGR, "IKE_SA found, but not allowed to check it out");
428 }
429 }
430 else
431 {
432 DBG2(SIG_DBG_MGR, "IKE_SA not stored in list");
433 /* looks like there is no such IKE_SA, better luck next time... */
434 }
435 }
436 else if ((initiator_spi_set && !responder_spi_set) && (!original_initiator))
437 {
438 /* an IKE_SA_INIT from an another endpoint,
439 * he is the initiator.
440 * For simplicity, we do NOT check for retransmitted
441 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
442 * Request (even a retransmitted one) will result in a
443 * IKE_SA. This could be improved...
444 */
445 u_int64_t responder_spi;
446 entry_t *new_entry;
447
448 /* set SPIs, we are the responder */
449 responder_spi = get_next_spi(this);
450
451 /* we also set arguments spi, so its still valid */
452 ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
453
454 /* create entry */
455 new_entry = entry_create(ike_sa_id);
456
457 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
458
459 /* check ike_sa out */
460 DBG2(SIG_DBG_MGR, "IKE_SA added to list of known IKE_SAs");
461 new_entry->checked_out = TRUE;
462 ike_sa = new_entry->ike_sa;
463 }
464 else if (!initiator_spi_set && !responder_spi_set && original_initiator)
465 {
466 /* checkout of a new and unused IKE_SA, used for rekeying */
467 entry_t *new_entry;
468
469 ike_sa_id->set_initiator_spi(ike_sa_id, get_next_spi(this));
470 /* create entry */
471 new_entry = entry_create(ike_sa_id);
472 DBG2(SIG_DBG_MGR, "created IKE_SA: %J", ike_sa_id);
473
474 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
475
476 /* check ike_sa out */
477 new_entry->checked_out = TRUE;
478 ike_sa = new_entry->ike_sa;
479 }
480 else
481 {
482 /* responder set, initiator not: here is something seriously wrong! */
483 DBG2(SIG_DBG_MGR, "invalid IKE_SA SPIs");
484 }
485
486 pthread_mutex_unlock(&(this->mutex));
487
488 charon->bus->set_sa(charon->bus, ike_sa);
489 return ike_sa;
490 }
491
492 /**
493 * Implementation of of ike_sa_manager.checkout_by_child.
494 */
495 static ike_sa_t* checkout_by_child(private_ike_sa_manager_t *this,
496 u_int32_t reqid)
497 {
498 iterator_t *iterator;
499 ike_sa_t *ike_sa = NULL;
500
501 pthread_mutex_lock(&(this->mutex));
502
503 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
504 while (iterator->has_next(iterator))
505 {
506 entry_t *entry;
507
508 iterator->current(iterator, (void**)&entry);
509 if (wait_for_entry(this, entry))
510 {
511 /* ok, access is exclusive for us, check for child */
512 if (entry->ike_sa->has_child_sa(entry->ike_sa, reqid))
513 {
514 /* match */
515 entry->checked_out = TRUE;
516 ike_sa = entry->ike_sa;
517 break;
518 }
519 }
520 }
521 iterator->destroy(iterator);
522 pthread_mutex_unlock(&(this->mutex));
523
524 charon->bus->set_sa(charon->bus, ike_sa);
525 return ike_sa;
526 }
527
528 /**
529 * Iterator hook for iterate
530 */
531 static bool iterate(iterator_t *iterator, void **value)
532 {
533 if (iterator->has_next(iterator))
534 {
535 entry_t *entry;
536 iterator->current(iterator, (void**)&entry);
537 *value = entry->ike_sa;
538 return TRUE;
539 }
540 return FALSE;
541 }
542
543 /**
544 * Implementation of ike_sa_manager_t.create_iterator.
545 */
546 static iterator_t *create_iterator(private_ike_sa_manager_t* this)
547 {
548 iterator_t *iterator = this->ike_sa_list->create_iterator_locked(
549 this->ike_sa_list, &this->mutex);
550 /* overload iterator */
551 iterator->iterate = iterate;
552 return iterator;
553 }
554
555 /**
556 * Implementation of ike_sa_manager_t.checkin.
557 */
558 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
559 {
560 /* to check the SA back in, we look for the pointer of the ike_sa
561 * in all entries.
562 * We can't search by SPI's since the MAY have changed (e.g. on reception
563 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
564 */
565 status_t retval;
566 entry_t *entry;
567 ike_sa_id_t *ike_sa_id;
568
569 ike_sa_id = ike_sa->get_id(ike_sa);
570
571 DBG2(SIG_DBG_MGR, "checkin IKE_SA: %J", ike_sa_id);
572
573 pthread_mutex_lock(&(this->mutex));
574
575 /* look for the entry */
576 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
577 {
578 /* ike_sa_id must be updated */
579 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
580 /* signal waiting threads */
581 entry->checked_out = FALSE;
582 DBG2(SIG_DBG_MGR, "check-in of IKE_SA successful.");
583 pthread_cond_signal(&(entry->condvar));
584 retval = SUCCESS;
585 }
586 else
587 {
588 DBG2(SIG_DBG_MGR, "tried to check in nonexisting IKE_SA");
589 /* this SA is no more, this REALLY should not happen */
590 retval = NOT_FOUND;
591 }
592
593 DBG2(SIG_DBG_MGR, "%d IKE_SAs in manager now",
594 this->ike_sa_list->get_count(this->ike_sa_list));
595 pthread_mutex_unlock(&(this->mutex));
596
597 charon->bus->set_sa(charon->bus, NULL);
598 return retval;
599 }
600
601
602 /**
603 * Implementation of ike_sa_manager_t.checkin_and_destroy.
604 */
605 static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
606 {
607 /* deletion is a bit complex, we must garant that no thread is waiting for
608 * this SA.
609 * We take this SA from the list, and start signaling while threads
610 * are in the condvar.
611 */
612 entry_t *entry;
613 status_t retval;
614 ike_sa_id_t *ike_sa_id;
615
616 ike_sa_id = ike_sa->get_id(ike_sa);
617 DBG2(SIG_DBG_MGR, "checkin and destroy IKE_SA: %J", ike_sa_id);
618
619 pthread_mutex_lock(&(this->mutex));
620
621 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
622 {
623 /* drive out waiting threads, as we are in hurry */
624 entry->driveout_waiting_threads = TRUE;
625
626 delete_entry(this, entry);
627
628 DBG2(SIG_DBG_MGR, "check-in and destroy of IKE_SA successful");
629 retval = SUCCESS;
630 }
631 else
632 {
633 DBG2(SIG_DBG_MGR, "tried to check-in and delete nonexisting IKE_SA");
634 retval = NOT_FOUND;
635 }
636
637 pthread_mutex_unlock(&(this->mutex));
638 charon->bus->set_sa(charon->bus, ike_sa);
639 return retval;
640 }
641
642 /**
643 * Implementation of ike_sa_manager_t.delete.
644 */
645 static status_t delete_(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
646 {
647 /* deletion is a bit complex, we must garant that no thread is waiting for
648 * this SA.
649 * We take this SA from the list, and start signaling while threads
650 * are in the condvar.
651 */
652 entry_t *entry;
653 status_t retval;
654
655 DBG2(SIG_DBG_MGR, "delete IKE_SA: %J", ike_sa_id);
656
657 pthread_mutex_lock(&(this->mutex));
658
659 if (get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
660 {
661 /* we try a delete. If it succeeds, our job is done here. The
662 * other peer will reply, and the IKE SA gets the finally deleted...
663 */
664 if (entry->ike_sa->delete(entry->ike_sa) == SUCCESS)
665 {
666 DBG2(SIG_DBG_MGR, "initiated delete for IKE_SA");
667 }
668 /* but if the IKE SA is not in a state where the deletion is
669 * negotiated with the other peer, we can destroy the IKE SA on our own.
670 */
671 else
672 {
673
674 }
675 retval = SUCCESS;
676 }
677 else
678 {
679 DBG2(SIG_DBG_MGR, "tried to delete nonexisting IKE_SA");
680 retval = NOT_FOUND;
681 }
682
683 pthread_mutex_unlock(&(this->mutex));
684 return retval;
685 }
686
687 /**
688 * Implementation of ike_sa_manager_t.delete_by_name.
689 */
690 static status_t delete_by_name(private_ike_sa_manager_t *this, char *name)
691 {
692 iterator_t *iterator;
693 iterator_t *child_iter;
694 entry_t *entry;
695 size_t name_len = strlen(name);
696
697 pthread_mutex_lock(&(this->mutex));
698
699 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
700 while (iterator->iterate(iterator, (void**)&entry))
701 {
702 if (wait_for_entry(this, entry))
703 {
704 /* delete ike_sa if:
705 * name{x} matches completely
706 * name{} matches by name
707 * name matches by name
708 */
709 bool del = FALSE;
710 char *ike_name;
711 char *child_name;
712 child_sa_t *child_sa;
713
714 ike_name = entry->ike_sa->get_name(entry->ike_sa);
715 /* check if "name{x}" matches completely */
716 if (strcmp(name, ike_name) == 0)
717 {
718 del = TRUE;
719 }
720 /* check if name is in form of "name{}" and matches to ike_name */
721 else if (name_len > 1 &&
722 name[name_len - 2] == '{' && name[name_len - 1] == '}' &&
723 strlen(ike_name) > name_len &&
724 ike_name[name_len - 2] == '{' &&
725 strncmp(name, ike_name, name_len - 2) == 0)
726 {
727 del = TRUE;
728 }
729 /* finally, check if name is "name" and matches ike_name */
730 else if (name_len == strchr(ike_name, '{') - ike_name &&
731 strncmp(name, ike_name, name_len) == 0)
732 {
733 del = TRUE;
734 }
735
736 if (del)
737 {
738 if (entry->ike_sa->delete(entry->ike_sa) == DESTROY_ME)
739 {
740 delete_entry(this, entry);
741 iterator->reset(iterator);
742 }
743 /* no need to check children, as we delete all */
744 continue;
745 }
746
747 /* and now the same game for all children. delete child_sa if:
748 * name[x] matches completely
749 * name[] matches by name
750 * name matches by name
751 */
752 child_iter = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
753 while (child_iter->iterate(child_iter, (void**)&child_sa))
754 {
755 /* skip ROUTED children, they have their "unroute" command */
756 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
757 {
758 continue;
759 }
760
761 child_name = child_sa->get_name(child_sa);
762 del = FALSE;
763 /* check if "name[x]" matches completely */
764 if (strcmp(name, child_name) == 0)
765 {
766 del = TRUE;
767 }
768 /* check if name is in form of "name[]" and matches to child_name */
769 else if (name_len > 1 &&
770 name[name_len - 2] == '[' && name[name_len - 1] == ']' &&
771 strlen(child_name) > name_len &&
772 child_name[name_len - 2] == '[' &&
773 strncmp(name, child_name, name_len - 2) == 0)
774 {
775 del = TRUE;
776 }
777 /* finally, check if name is "name" and matches child_name */
778 else if (name_len == strchr(child_name, '[') - child_name &&
779 strncmp(name, child_name, name_len) == 0)
780 {
781 del = TRUE;
782 }
783 if (del)
784 {
785 if (entry->ike_sa->delete_child_sa(entry->ike_sa,
786 child_sa->get_protocol(child_sa),
787 child_sa->get_spi(child_sa, TRUE)) == DESTROY_ME)
788 {
789 /* when a fatal error occurs, we are responsible to
790 * remove the IKE_SA */
791 delete_entry(this, entry);
792 iterator->reset(iterator);
793 break;
794 }
795 }
796 }
797 child_iter->destroy(child_iter);
798 }
799 }
800 iterator->destroy(iterator);
801 pthread_mutex_unlock(&(this->mutex));
802
803 return SUCCESS;
804 }
805
806 /**
807 * Implementation of ike_sa_manager_t.destroy.
808 */
809 static void destroy(private_ike_sa_manager_t *this)
810 {
811 /* destroy all list entries */
812 linked_list_t *list = this->ike_sa_list;
813 iterator_t *iterator;
814 entry_t *entry;
815
816 pthread_mutex_lock(&(this->mutex));
817 DBG2(SIG_DBG_MGR, "going to destroy IKE_SA manager and all managed IKE_SA's");
818 /* Step 1: drive out all waiting threads */
819 DBG2(SIG_DBG_MGR, "set driveout flags for all stored IKE_SA's");
820 iterator = list->create_iterator(list, TRUE);
821 while (iterator->has_next(iterator))
822 {
823 iterator->current(iterator, (void**)&entry);
824 /* do not accept new threads, drive out waiting threads */
825 entry->driveout_new_threads = TRUE;
826 entry->driveout_waiting_threads = TRUE;
827 }
828 DBG2(SIG_DBG_MGR, "wait for all threads to leave IKE_SA's");
829 /* Step 2: wait until all are gone */
830 iterator->reset(iterator);
831 while (iterator->has_next(iterator))
832 {
833 iterator->current(iterator, (void**)&entry);
834 while (entry->waiting_threads)
835 {
836 /* wake up all */
837 pthread_cond_broadcast(&(entry->condvar));
838 /* go sleeping until they are gone */
839 pthread_cond_wait(&(entry->condvar), &(this->mutex));
840 }
841 }
842 DBG2(SIG_DBG_MGR, "delete all IKE_SA's");
843 /* Step 3: initiate deletion of all IKE_SAs */
844 iterator->reset(iterator);
845 while (iterator->has_next(iterator))
846 {
847 iterator->current(iterator, (void**)&entry);
848 entry->ike_sa->delete(entry->ike_sa);
849 }
850 iterator->destroy(iterator);
851
852 DBG2(SIG_DBG_MGR, "destroy all entries");
853 /* Step 4: destroy all entries */
854 list->destroy_function(list, (void*)entry_destroy);
855 pthread_mutex_unlock(&(this->mutex));
856
857 this->randomizer->destroy(this->randomizer);
858
859 free(this);
860 }
861
862 /*
863 * Described in header.
864 */
865 ike_sa_manager_t *ike_sa_manager_create()
866 {
867 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
868
869 /* assign public functions */
870 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
871 this->public.checkout_by_id = (ike_sa_t*(*)(ike_sa_manager_t*,host_t*,host_t*,identification_t*,identification_t*))checkout_by_id;
872 this->public.checkout = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_id_t*))checkout;
873 this->public.checkout_by_child = (ike_sa_t*(*)(ike_sa_manager_t*,u_int32_t))checkout_by_child;
874 this->public.create_iterator = (iterator_t*(*)(ike_sa_manager_t*))create_iterator;
875 this->public.checkin = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
876 this->public.delete = (status_t(*)(ike_sa_manager_t*,ike_sa_id_t*))delete_;
877 this->public.delete_by_name = (status_t(*)(ike_sa_manager_t*,char*))delete_by_name;
878 this->public.checkin_and_destroy = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
879
880 /* initialize private variables */
881 this->ike_sa_list = linked_list_create();
882 pthread_mutex_init(&(this->mutex), NULL);
883 this->randomizer = randomizer_create();
884
885 return (ike_sa_manager_t*)this;
886 }