removed deprecated iterator methods (has_next & current)
[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 entry_t *current;
195 status_t status;
196
197 iterator = list->create_iterator(list, TRUE);
198
199 /* default status */
200 status = NOT_FOUND;
201
202 while (iterator->iterate(iterator, (void**)&current))
203 {
204 /* only pointers are compared */
205 if (current->ike_sa == ike_sa)
206 {
207 DBG2(SIG_DBG_MGR, "found entry by pointer");
208 *entry = current;
209 status = SUCCESS;
210 break;
211 }
212 }
213 iterator->destroy(iterator);
214
215 return status;
216 }
217
218 /**
219 * Implementation of private_ike_sa_manager_s.delete_entry.
220 */
221 static status_t delete_entry(private_ike_sa_manager_t *this, entry_t *entry)
222 {
223 linked_list_t *list = this->ike_sa_list;
224 iterator_t *iterator;
225 entry_t *current;
226 status_t status;
227
228 iterator = list->create_iterator(list, TRUE);
229
230 status = NOT_FOUND;
231
232 while (iterator->iterate(iterator, (void**)&current))
233 {
234 if (current == entry)
235 {
236 /* mark it, so now new threads can get this entry */
237 entry->driveout_new_threads = TRUE;
238 /* wait until all workers have done their work */
239 while (entry->waiting_threads)
240 {
241 /* wake up all */
242 pthread_cond_broadcast(&(entry->condvar));
243 /* they will wake us again when their work is done */
244 pthread_cond_wait(&(entry->condvar), &(this->mutex));
245 }
246
247 DBG2(SIG_DBG_MGR, "found entry by pointer, deleting it");
248 iterator->remove(iterator);
249 entry_destroy(entry);
250 status = SUCCESS;
251 break;
252 }
253 }
254 iterator->destroy(iterator);
255 return status;
256 }
257
258 /**
259 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
260 * acquireable
261 */
262 static bool wait_for_entry(private_ike_sa_manager_t *this, entry_t *entry)
263 {
264 if (entry->driveout_new_threads)
265 {
266 /* we are not allowed to get this */
267 return FALSE;
268 }
269 while (entry->checked_out && !entry->driveout_waiting_threads)
270 {
271 /* so wait until we can get it for us.
272 * we register us as waiting. */
273 entry->waiting_threads++;
274 pthread_cond_wait(&(entry->condvar), &(this->mutex));
275 entry->waiting_threads--;
276 }
277 /* hm, a deletion request forbids us to get this SA, get next one */
278 if (entry->driveout_waiting_threads)
279 {
280 /* we must signal here, others may be waiting on it, too */
281 pthread_cond_signal(&(entry->condvar));
282 return FALSE;
283 }
284 return TRUE;
285 }
286
287 /**
288 * Implementation of private_ike_sa_manager_t.get_next_spi.
289 */
290 static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
291 {
292 u_int64_t spi;
293
294 this->randomizer->get_pseudo_random_bytes(this->randomizer, 8, (u_int8_t*)&spi);
295
296 return spi;
297 }
298
299 /**
300 * Implementation of of ike_sa_manager.checkout_by_id.
301 */
302 static ike_sa_t* checkout_by_id(private_ike_sa_manager_t *this,
303 host_t *my_host,
304 host_t *other_host,
305 identification_t *my_id,
306 identification_t *other_id)
307 {
308 iterator_t *iterator;
309 entry_t *entry;
310 ike_sa_t *ike_sa = NULL;
311
312 pthread_mutex_lock(&(this->mutex));
313
314 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
315 while (iterator->iterate(iterator, (void**)&entry))
316 {
317 identification_t *found_my_id, *found_other_id;
318 host_t *found_my_host, *found_other_host;
319 int wc;
320
321 if (!wait_for_entry(this, entry))
322 {
323 continue;
324 }
325
326 found_my_id = entry->ike_sa->get_my_id(entry->ike_sa);
327 found_other_id = entry->ike_sa->get_other_id(entry->ike_sa);
328 found_my_host = entry->ike_sa->get_my_host(entry->ike_sa);
329 found_other_host = entry->ike_sa->get_other_host(entry->ike_sa);
330
331 if (found_my_id->get_type(found_my_id) == ID_ANY &&
332 found_other_id->get_type(found_other_id) == ID_ANY)
333 {
334 /* IKE_SA has no IDs yet, so we can't use it */
335 continue;
336 }
337
338 /* compare ID and hosts. Supplied ID may contain wildcards, and IP
339 * may be %any. */
340 if ((found_my_host->is_anyaddr(found_my_host) ||
341 my_host->ip_equals(my_host, found_my_host)) &&
342 (found_other_host->is_anyaddr(found_other_host) ||
343 other_host->ip_equals(other_host, found_other_host)) &&
344 found_my_id->matches(found_my_id, my_id, &wc) &&
345 found_other_id->matches(found_other_id, other_id, &wc))
346 {
347 /* looks good, we take this one */
348 DBG2(SIG_DBG_MGR, "found an existing IKE_SA for %H[%D]...%H[%D]",
349 my_host, other_host, my_id, other_id);
350 entry->checked_out = TRUE;
351 ike_sa = entry->ike_sa;
352 }
353 }
354 iterator->destroy(iterator);
355
356 if (!ike_sa)
357 {
358 u_int64_t initiator_spi;
359 entry_t *new_entry;
360 ike_sa_id_t *new_ike_sa_id;
361
362 initiator_spi = get_next_spi(this);
363 new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
364 new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
365
366 /* create entry */
367 new_entry = entry_create(new_ike_sa_id);
368 DBG2(SIG_DBG_MGR, "created IKE_SA: %J", new_ike_sa_id);
369 new_ike_sa_id->destroy(new_ike_sa_id);
370
371 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
372
373 /* check ike_sa out */
374 DBG2(SIG_DBG_MGR, "new IKE_SA created for IDs [%D]...[%D]", my_id, other_id);
375 new_entry->checked_out = TRUE;
376 ike_sa = new_entry->ike_sa;
377 }
378 pthread_mutex_unlock(&(this->mutex));
379 charon->bus->set_sa(charon->bus, ike_sa);
380 return ike_sa;
381 }
382
383 /**
384 * Implementation of of ike_sa_manager.checkout.
385 */
386 static ike_sa_t* checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
387 {
388 bool responder_spi_set;
389 bool initiator_spi_set;
390 bool original_initiator;
391 ike_sa_t *ike_sa = NULL;
392
393 DBG2(SIG_DBG_MGR, "checkout IKE_SA: %J", ike_sa_id);
394
395 DBG2(SIG_DBG_MGR, "%d IKE_SAs in manager",
396 this->ike_sa_list->get_count(this->ike_sa_list));
397
398 /* each access is locked */
399 pthread_mutex_lock(&(this->mutex));
400
401 responder_spi_set = ike_sa_id->get_responder_spi(ike_sa_id);
402 initiator_spi_set = ike_sa_id->get_initiator_spi(ike_sa_id);
403 original_initiator = ike_sa_id->is_initiator(ike_sa_id);
404
405 if ((initiator_spi_set && responder_spi_set) ||
406 ((initiator_spi_set && !responder_spi_set) && (original_initiator)))
407 {
408 /* we SHOULD have an IKE_SA for these SPIs in the list,
409 * if not, we can't handle the request...
410 */
411 entry_t *entry;
412 /* look for the entry */
413 if (get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
414 {
415 if (wait_for_entry(this, entry))
416 {
417 DBG2(SIG_DBG_MGR, "IKE_SA successfully checked out");
418 /* ok, this IKE_SA is finally ours */
419 entry->checked_out = TRUE;
420 ike_sa = entry->ike_sa;
421 }
422 else
423 {
424 DBG2(SIG_DBG_MGR, "IKE_SA found, but not allowed to check it out");
425 }
426 }
427 else
428 {
429 DBG2(SIG_DBG_MGR, "IKE_SA not stored in list");
430 /* looks like there is no such IKE_SA, better luck next time... */
431 }
432 }
433 else if ((initiator_spi_set && !responder_spi_set) && (!original_initiator))
434 {
435 /* an IKE_SA_INIT from an another endpoint,
436 * he is the initiator.
437 * For simplicity, we do NOT check for retransmitted
438 * IKE_SA_INIT-Requests here, so EVERY single IKE_SA_INIT-
439 * Request (even a retransmitted one) will result in a
440 * IKE_SA. This could be improved...
441 */
442 u_int64_t responder_spi;
443 entry_t *new_entry;
444
445 /* set SPIs, we are the responder */
446 responder_spi = get_next_spi(this);
447
448 /* we also set arguments spi, so its still valid */
449 ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
450
451 /* create entry */
452 new_entry = entry_create(ike_sa_id);
453
454 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
455
456 /* check ike_sa out */
457 DBG2(SIG_DBG_MGR, "IKE_SA added to list of known IKE_SAs");
458 new_entry->checked_out = TRUE;
459 ike_sa = new_entry->ike_sa;
460 }
461 else if (!initiator_spi_set && !responder_spi_set && original_initiator)
462 {
463 /* checkout of a new and unused IKE_SA, used for rekeying */
464 entry_t *new_entry;
465
466 ike_sa_id->set_initiator_spi(ike_sa_id, get_next_spi(this));
467 /* create entry */
468 new_entry = entry_create(ike_sa_id);
469 DBG2(SIG_DBG_MGR, "created IKE_SA: %J", ike_sa_id);
470
471 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
472
473 /* check ike_sa out */
474 new_entry->checked_out = TRUE;
475 ike_sa = new_entry->ike_sa;
476 }
477 else
478 {
479 /* responder set, initiator not: here is something seriously wrong! */
480 DBG2(SIG_DBG_MGR, "invalid IKE_SA SPIs");
481 }
482
483 pthread_mutex_unlock(&(this->mutex));
484
485 charon->bus->set_sa(charon->bus, ike_sa);
486 return ike_sa;
487 }
488
489 /**
490 * Implementation of of ike_sa_manager.checkout_by_child.
491 */
492 static ike_sa_t* checkout_by_child(private_ike_sa_manager_t *this,
493 u_int32_t reqid)
494 {
495 iterator_t *iterator;
496 entry_t *entry;
497 ike_sa_t *ike_sa = 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->iterate(iterator, (void**)&entry))
503 {
504 if (wait_for_entry(this, entry))
505 {
506 /* ok, access is exclusive for us, check for child */
507 if (entry->ike_sa->has_child_sa(entry->ike_sa, reqid))
508 {
509 /* match */
510 entry->checked_out = TRUE;
511 ike_sa = entry->ike_sa;
512 break;
513 }
514 }
515 }
516 iterator->destroy(iterator);
517 pthread_mutex_unlock(&(this->mutex));
518
519 charon->bus->set_sa(charon->bus, ike_sa);
520 return ike_sa;
521 }
522
523 /**
524 * Iterator hook for iterate, gets ike_sas instead of entries
525 */
526 static void* iterator_hook(void *value)
527 {
528 return ((entry_t*)value)->ike_sa;
529 }
530
531 /**
532 * Implementation of ike_sa_manager_t.create_iterator.
533 */
534 static iterator_t *create_iterator(private_ike_sa_manager_t* this)
535 {
536 iterator_t *iterator = this->ike_sa_list->create_iterator_locked(
537 this->ike_sa_list, &this->mutex);
538 /* register hook to iterator over ike_sas, not entries */
539 iterator->set_iterator_hook(iterator, iterator_hook);
540 return iterator;
541 }
542
543 /**
544 * Implementation of ike_sa_manager_t.checkin.
545 */
546 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
547 {
548 /* to check the SA back in, we look for the pointer of the ike_sa
549 * in all entries.
550 * We can't search by SPI's since the MAY have changed (e.g. on reception
551 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
552 */
553 status_t retval;
554 entry_t *entry;
555 ike_sa_id_t *ike_sa_id;
556
557 ike_sa_id = ike_sa->get_id(ike_sa);
558
559 DBG2(SIG_DBG_MGR, "checkin IKE_SA: %J", ike_sa_id);
560
561 pthread_mutex_lock(&(this->mutex));
562
563 /* look for the entry */
564 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
565 {
566 /* ike_sa_id must be updated */
567 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
568 /* signal waiting threads */
569 entry->checked_out = FALSE;
570 DBG2(SIG_DBG_MGR, "check-in of IKE_SA successful.");
571 pthread_cond_signal(&(entry->condvar));
572 retval = SUCCESS;
573 }
574 else
575 {
576 DBG2(SIG_DBG_MGR, "tried to check in nonexisting IKE_SA");
577 /* this SA is no more, this REALLY should not happen */
578 retval = NOT_FOUND;
579 }
580
581 DBG2(SIG_DBG_MGR, "%d IKE_SAs in manager now",
582 this->ike_sa_list->get_count(this->ike_sa_list));
583 pthread_mutex_unlock(&(this->mutex));
584
585 charon->bus->set_sa(charon->bus, NULL);
586 return retval;
587 }
588
589
590 /**
591 * Implementation of ike_sa_manager_t.checkin_and_destroy.
592 */
593 static status_t checkin_and_destroy(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 entry_t *entry;
601 status_t retval;
602 ike_sa_id_t *ike_sa_id;
603
604 ike_sa_id = ike_sa->get_id(ike_sa);
605 DBG2(SIG_DBG_MGR, "checkin and destroy IKE_SA: %J", ike_sa_id);
606
607 pthread_mutex_lock(&(this->mutex));
608
609 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
610 {
611 /* drive out waiting threads, as we are in hurry */
612 entry->driveout_waiting_threads = TRUE;
613
614 delete_entry(this, entry);
615
616 DBG2(SIG_DBG_MGR, "check-in and destroy of IKE_SA successful");
617 retval = SUCCESS;
618 }
619 else
620 {
621 DBG2(SIG_DBG_MGR, "tried to check-in and delete nonexisting IKE_SA");
622 retval = NOT_FOUND;
623 }
624
625 pthread_mutex_unlock(&(this->mutex));
626 charon->bus->set_sa(charon->bus, ike_sa);
627 return retval;
628 }
629
630 /**
631 * Implementation of ike_sa_manager_t.delete.
632 */
633 static status_t delete_(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
634 {
635 /* deletion is a bit complex, we must garant that no thread is waiting for
636 * this SA.
637 * We take this SA from the list, and start signaling while threads
638 * are in the condvar.
639 */
640 entry_t *entry;
641 status_t retval;
642
643 DBG2(SIG_DBG_MGR, "delete IKE_SA: %J", ike_sa_id);
644
645 pthread_mutex_lock(&(this->mutex));
646
647 if (get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
648 {
649 /* we try a delete. If it succeeds, our job is done here. The
650 * other peer will reply, and the IKE SA gets the finally deleted...
651 */
652 if (entry->ike_sa->delete(entry->ike_sa) == SUCCESS)
653 {
654 DBG2(SIG_DBG_MGR, "initiated delete for IKE_SA");
655 }
656 /* but if the IKE SA is not in a state where the deletion is
657 * negotiated with the other peer, we can destroy the IKE SA on our own.
658 */
659 else
660 {
661
662 }
663 retval = SUCCESS;
664 }
665 else
666 {
667 DBG2(SIG_DBG_MGR, "tried to delete nonexisting IKE_SA");
668 retval = NOT_FOUND;
669 }
670
671 pthread_mutex_unlock(&(this->mutex));
672 return retval;
673 }
674
675 /**
676 * Implementation of ike_sa_manager_t.delete_by_name.
677 */
678 static status_t delete_by_name(private_ike_sa_manager_t *this, char *name)
679 {
680 iterator_t *iterator;
681 iterator_t *child_iter;
682 entry_t *entry;
683 size_t name_len = strlen(name);
684
685 pthread_mutex_lock(&(this->mutex));
686
687 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
688 while (iterator->iterate(iterator, (void**)&entry))
689 {
690 if (wait_for_entry(this, entry))
691 {
692 /* delete ike_sa if:
693 * name{x} matches completely
694 * name{} matches by name
695 * name matches by name
696 */
697 bool del = FALSE;
698 char *ike_name;
699 char *child_name;
700 child_sa_t *child_sa;
701
702 ike_name = entry->ike_sa->get_name(entry->ike_sa);
703 /* check if "name{x}" matches completely */
704 if (strcmp(name, ike_name) == 0)
705 {
706 del = TRUE;
707 }
708 /* check if name is in form of "name{}" and matches to ike_name */
709 else if (name_len > 1 &&
710 name[name_len - 2] == '{' && name[name_len - 1] == '}' &&
711 strlen(ike_name) > name_len &&
712 ike_name[name_len - 2] == '{' &&
713 strncmp(name, ike_name, name_len - 2) == 0)
714 {
715 del = TRUE;
716 }
717 /* finally, check if name is "name" and matches ike_name */
718 else if (name_len == strchr(ike_name, '{') - ike_name &&
719 strncmp(name, ike_name, name_len) == 0)
720 {
721 del = TRUE;
722 }
723
724 if (del)
725 {
726 if (entry->ike_sa->delete(entry->ike_sa) == DESTROY_ME)
727 {
728 delete_entry(this, entry);
729 iterator->reset(iterator);
730 }
731 /* no need to check children, as we delete all */
732 continue;
733 }
734
735 /* and now the same game for all children. delete child_sa if:
736 * name[x] matches completely
737 * name[] matches by name
738 * name matches by name
739 */
740 child_iter = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
741 while (child_iter->iterate(child_iter, (void**)&child_sa))
742 {
743 /* skip ROUTED children, they have their "unroute" command */
744 if (child_sa->get_state(child_sa) == CHILD_ROUTED)
745 {
746 continue;
747 }
748
749 child_name = child_sa->get_name(child_sa);
750 del = FALSE;
751 /* check if "name[x]" matches completely */
752 if (strcmp(name, child_name) == 0)
753 {
754 del = TRUE;
755 }
756 /* check if name is in form of "name[]" and matches to child_name */
757 else if (name_len > 1 &&
758 name[name_len - 2] == '[' && name[name_len - 1] == ']' &&
759 strlen(child_name) > name_len &&
760 child_name[name_len - 2] == '[' &&
761 strncmp(name, child_name, name_len - 2) == 0)
762 {
763 del = TRUE;
764 }
765 /* finally, check if name is "name" and matches child_name */
766 else if (name_len == strchr(child_name, '[') - child_name &&
767 strncmp(name, child_name, name_len) == 0)
768 {
769 del = TRUE;
770 }
771 if (del)
772 {
773 if (entry->ike_sa->delete_child_sa(entry->ike_sa,
774 child_sa->get_protocol(child_sa),
775 child_sa->get_spi(child_sa, TRUE)) == DESTROY_ME)
776 {
777 /* when a fatal error occurs, we are responsible to
778 * remove the IKE_SA */
779 delete_entry(this, entry);
780 iterator->reset(iterator);
781 break;
782 }
783 }
784 }
785 child_iter->destroy(child_iter);
786 }
787 }
788 iterator->destroy(iterator);
789 pthread_mutex_unlock(&(this->mutex));
790
791 return SUCCESS;
792 }
793
794 /**
795 * Implementation of ike_sa_manager_t.destroy.
796 */
797 static void destroy(private_ike_sa_manager_t *this)
798 {
799 /* destroy all list entries */
800 linked_list_t *list = this->ike_sa_list;
801 iterator_t *iterator;
802 entry_t *entry;
803
804 pthread_mutex_lock(&(this->mutex));
805 DBG2(SIG_DBG_MGR, "going to destroy IKE_SA manager and all managed IKE_SA's");
806 /* Step 1: drive out all waiting threads */
807 DBG2(SIG_DBG_MGR, "set driveout flags for all stored IKE_SA's");
808 iterator = list->create_iterator(list, TRUE);
809 while (iterator->iterate(iterator, (void**)&entry))
810 {
811 /* do not accept new threads, drive out waiting threads */
812 entry->driveout_new_threads = TRUE;
813 entry->driveout_waiting_threads = TRUE;
814 }
815 DBG2(SIG_DBG_MGR, "wait for all threads to leave IKE_SA's");
816 /* Step 2: wait until all are gone */
817 iterator->reset(iterator);
818 while (iterator->iterate(iterator, (void**)&entry))
819 {
820 while (entry->waiting_threads)
821 {
822 /* wake up all */
823 pthread_cond_broadcast(&(entry->condvar));
824 /* go sleeping until they are gone */
825 pthread_cond_wait(&(entry->condvar), &(this->mutex));
826 }
827 }
828 DBG2(SIG_DBG_MGR, "delete all IKE_SA's");
829 /* Step 3: initiate deletion of all IKE_SAs */
830 iterator->reset(iterator);
831 while (iterator->iterate(iterator, (void**)&entry))
832 {
833 entry->ike_sa->delete(entry->ike_sa);
834 }
835 iterator->destroy(iterator);
836
837 DBG2(SIG_DBG_MGR, "destroy all entries");
838 /* Step 4: destroy all entries */
839 list->destroy_function(list, (void*)entry_destroy);
840 pthread_mutex_unlock(&(this->mutex));
841
842 this->randomizer->destroy(this->randomizer);
843
844 free(this);
845 }
846
847 /*
848 * Described in header.
849 */
850 ike_sa_manager_t *ike_sa_manager_create()
851 {
852 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
853
854 /* assign public functions */
855 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
856 this->public.checkout_by_id = (ike_sa_t*(*)(ike_sa_manager_t*,host_t*,host_t*,identification_t*,identification_t*))checkout_by_id;
857 this->public.checkout = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_id_t*))checkout;
858 this->public.checkout_by_child = (ike_sa_t*(*)(ike_sa_manager_t*,u_int32_t))checkout_by_child;
859 this->public.create_iterator = (iterator_t*(*)(ike_sa_manager_t*))create_iterator;
860 this->public.checkin = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
861 this->public.delete = (status_t(*)(ike_sa_manager_t*,ike_sa_id_t*))delete_;
862 this->public.delete_by_name = (status_t(*)(ike_sa_manager_t*,char*))delete_by_name;
863 this->public.checkin_and_destroy = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
864
865 /* initialize private variables */
866 this->ike_sa_list = linked_list_create();
867 pthread_mutex_init(&(this->mutex), NULL);
868 this->randomizer = randomizer_create();
869
870 return (ike_sa_manager_t*)this;
871 }