split connections with different virtual IPs in different peer_cfgs
[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 * hash of the IKE_SA_INIT message, used to detect retransmissions
78 */
79 chunk_t init_hash;
80
81 /**
82 * message ID currently processing, if any
83 */
84 u_int32_t message_id;
85 };
86
87 /**
88 * Implementation of entry_t.destroy.
89 */
90 static status_t entry_destroy(entry_t *this)
91 {
92 /* also destroy IKE SA */
93 this->ike_sa->destroy(this->ike_sa);
94 this->ike_sa_id->destroy(this->ike_sa_id);
95 chunk_free(&this->init_hash);
96 free(this);
97 return SUCCESS;
98 }
99
100 /**
101 * Creates a new entry for the ike_sa_t list.
102 */
103 static entry_t *entry_create(ike_sa_id_t *ike_sa_id)
104 {
105 entry_t *this = malloc_thing(entry_t);
106
107 this->waiting_threads = 0;
108 pthread_cond_init(&this->condvar, NULL);
109
110 /* we set checkout flag when we really give it out */
111 this->checked_out = FALSE;
112 this->driveout_new_threads = FALSE;
113 this->driveout_waiting_threads = FALSE;
114 this->message_id = -1;
115 this->init_hash = chunk_empty;
116
117 /* ike_sa_id is always cloned */
118 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
119
120 /* create new ike_sa */
121 this->ike_sa = ike_sa_create(ike_sa_id);
122
123 return this;
124 }
125
126
127 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t;
128
129 /**
130 * Additional private members of ike_sa_manager_t.
131 */
132 struct private_ike_sa_manager_t {
133 /**
134 * Public interface of ike_sa_manager_t.
135 */
136 ike_sa_manager_t public;
137
138 /**
139 * Lock for exclusivly accessing the manager.
140 */
141 pthread_mutex_t mutex;
142
143 /**
144 * Linked list with entries for the ike_sa_t objects.
145 */
146 linked_list_t *ike_sa_list;
147
148 /**
149 * A randomizer, to get random SPIs for our side
150 */
151 randomizer_t *randomizer;
152
153 /**
154 * SHA1 hasher for IKE_SA_INIT retransmit detection
155 */
156 hasher_t *hasher;
157 };
158
159 /**
160 * Implementation of private_ike_sa_manager_t.get_entry_by_id.
161 */
162 static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, entry_t **entry)
163 {
164 linked_list_t *list = this->ike_sa_list;
165 iterator_t *iterator;
166 entry_t *current;
167 status_t status;
168
169 /* create iterator over list of ike_sa's */
170 iterator = list->create_iterator(list, TRUE);
171
172 /* default status */
173 status = NOT_FOUND;
174
175 while (iterator->iterate(iterator, (void**)&current))
176 {
177 if (current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id))
178 {
179 DBG2(DBG_MGR, "found entry by both SPIs");
180 *entry = current;
181 status = SUCCESS;
182 break;
183 }
184 if (ike_sa_id->get_responder_spi(ike_sa_id) == 0 ||
185 current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0)
186 {
187 /* seems to be a half ready ike_sa */
188 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) ==
189 ike_sa_id->get_initiator_spi(ike_sa_id)) &&
190 (current->ike_sa_id->is_initiator(ike_sa_id) ==
191 ike_sa_id->is_initiator(current->ike_sa_id)))
192 {
193 DBG2(DBG_MGR, "found entry by initiator SPI");
194 *entry = current;
195 status = SUCCESS;
196 break;
197 }
198 }
199 }
200
201 iterator->destroy(iterator);
202 return status;
203 }
204
205 /**
206 * Implementation of private_ike_sa_manager_t.get_entry_by_sa.
207 */
208 static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, entry_t **entry)
209 {
210 linked_list_t *list = this->ike_sa_list;
211 iterator_t *iterator;
212 entry_t *current;
213 status_t status;
214
215 iterator = list->create_iterator(list, TRUE);
216
217 /* default status */
218 status = NOT_FOUND;
219
220 while (iterator->iterate(iterator, (void**)&current))
221 {
222 /* only pointers are compared */
223 if (current->ike_sa == ike_sa)
224 {
225 DBG2(DBG_MGR, "found entry by pointer");
226 *entry = current;
227 status = SUCCESS;
228 break;
229 }
230 }
231 iterator->destroy(iterator);
232
233 return status;
234 }
235
236 /**
237 * Implementation of private_ike_sa_manager_s.delete_entry.
238 */
239 static status_t delete_entry(private_ike_sa_manager_t *this, entry_t *entry)
240 {
241 linked_list_t *list = this->ike_sa_list;
242 iterator_t *iterator;
243 entry_t *current;
244 status_t status;
245
246 iterator = list->create_iterator(list, TRUE);
247
248 status = NOT_FOUND;
249
250 while (iterator->iterate(iterator, (void**)&current))
251 {
252 if (current == entry)
253 {
254 /* mark it, so now new threads can get this entry */
255 entry->driveout_new_threads = TRUE;
256 /* wait until all workers have done their work */
257 while (entry->waiting_threads)
258 {
259 /* wake up all */
260 pthread_cond_broadcast(&(entry->condvar));
261 /* they will wake us again when their work is done */
262 pthread_cond_wait(&(entry->condvar), &(this->mutex));
263 }
264
265 DBG2(DBG_MGR, "found entry by pointer, deleting it");
266 iterator->remove(iterator);
267 entry_destroy(entry);
268 status = SUCCESS;
269 break;
270 }
271 }
272 iterator->destroy(iterator);
273 return status;
274 }
275
276 /**
277 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
278 * acquireable
279 */
280 static bool wait_for_entry(private_ike_sa_manager_t *this, entry_t *entry)
281 {
282 if (entry->driveout_new_threads)
283 {
284 /* we are not allowed to get this */
285 return FALSE;
286 }
287 while (entry->checked_out && !entry->driveout_waiting_threads)
288 {
289 /* so wait until we can get it for us.
290 * we register us as waiting. */
291 entry->waiting_threads++;
292 pthread_cond_wait(&(entry->condvar), &(this->mutex));
293 entry->waiting_threads--;
294 }
295 /* hm, a deletion request forbids us to get this SA, get next one */
296 if (entry->driveout_waiting_threads)
297 {
298 /* we must signal here, others may be waiting on it, too */
299 pthread_cond_signal(&(entry->condvar));
300 return FALSE;
301 }
302 return TRUE;
303 }
304
305 /**
306 * Implementation of private_ike_sa_manager_t.get_next_spi.
307 */
308 static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
309 {
310 u_int64_t spi;
311
312 this->randomizer->get_pseudo_random_bytes(this->randomizer, sizeof(spi),
313 (u_int8_t*)&spi);
314 return spi;
315 }
316
317 /**
318 * Implementation of of ike_sa_manager.checkout.
319 */
320 static ike_sa_t* checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
321 {
322 ike_sa_t *ike_sa = NULL;
323 entry_t *entry;
324
325 DBG2(DBG_MGR, "checkout IKE_SA, %d IKE_SAs in manager",
326 this->ike_sa_list->get_count(this->ike_sa_list));
327
328 pthread_mutex_lock(&(this->mutex));
329 if (get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
330 {
331 if (wait_for_entry(this, entry))
332 {
333 DBG2(DBG_MGR, "IKE_SA successfully checked out");
334 entry->checked_out = TRUE;
335 ike_sa = entry->ike_sa;
336 }
337 }
338 pthread_mutex_unlock(&this->mutex);
339 charon->bus->set_sa(charon->bus, ike_sa);
340 return ike_sa;
341 }
342
343 /**
344 * Implementation of of ike_sa_manager.checkout_new.
345 */
346 static ike_sa_t *checkout_new(private_ike_sa_manager_t* this, bool initiator)
347 {
348 entry_t *entry;
349 ike_sa_id_t *id;
350
351 if (initiator)
352 {
353 id = ike_sa_id_create(get_next_spi(this), 0, TRUE);
354 }
355 else
356 {
357 id = ike_sa_id_create(0, get_next_spi(this), FALSE);
358 }
359 entry = entry_create(id);
360 id->destroy(id);
361 pthread_mutex_lock(&this->mutex);
362 this->ike_sa_list->insert_last(this->ike_sa_list, entry);
363 entry->checked_out = TRUE;
364 pthread_mutex_unlock(&this->mutex);
365 DBG2(DBG_MGR, "created IKE_SA, %d IKE_SAs in manager",
366 this->ike_sa_list->get_count(this->ike_sa_list));
367 return entry->ike_sa;
368 }
369
370 /**
371 * Implementation of of ike_sa_manager.checkout_by_message.
372 */
373 static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
374 message_t *message)
375 {
376 entry_t *entry;
377 ike_sa_t *ike_sa = NULL;
378 ike_sa_id_t *id = message->get_ike_sa_id(message);
379 id = id->clone(id);
380 id->switch_initiator(id);
381
382 DBG2(DBG_MGR, "checkout IKE_SA by message, %d IKE_SAs in manager",
383 this->ike_sa_list->get_count(this->ike_sa_list));
384
385 if (message->get_request(message) &&
386 message->get_exchange_type(message) == IKE_SA_INIT)
387 {
388 /* IKE_SA_INIT request. Check for an IKE_SA with such a message hash. */
389 iterator_t *iterator;
390 chunk_t data, hash;
391
392 data = message->get_packet_data(message);
393 this->hasher->allocate_hash(this->hasher, data, &hash);
394 chunk_free(&data);
395
396 pthread_mutex_lock(&this->mutex);
397 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
398 while (iterator->iterate(iterator, (void**)&entry))
399 {
400 if (chunk_equals(hash, entry->init_hash))
401 {
402 if (entry->message_id == 0)
403 {
404 iterator->destroy(iterator);
405 pthread_mutex_unlock(&this->mutex);
406 chunk_free(&hash);
407 id->destroy(id);
408 DBG1(DBG_MGR, "ignoring IKE_SA_INIT, already processing");
409 return NULL;
410 }
411 else if (wait_for_entry(this, entry))
412 {
413 DBG2(DBG_MGR, "IKE_SA checked out by hash");
414 entry->checked_out = TRUE;
415 entry->message_id = message->get_message_id(message);
416 ike_sa = entry->ike_sa;
417 }
418 break;
419 }
420 }
421 iterator->destroy(iterator);
422 pthread_mutex_unlock(&this->mutex);
423
424 if (ike_sa == NULL)
425 {
426 if (id->get_responder_spi(id) == 0 &&
427 message->get_exchange_type(message) == IKE_SA_INIT)
428 {
429 /* no IKE_SA found, create a new one */
430 id->set_responder_spi(id, get_next_spi(this));
431 entry = entry_create(id);
432
433 pthread_mutex_lock(&this->mutex);
434 this->ike_sa_list->insert_last(this->ike_sa_list, entry);
435 entry->checked_out = TRUE;
436 entry->message_id = message->get_message_id(message);
437 pthread_mutex_unlock(&this->mutex);
438 entry->init_hash = hash;
439 ike_sa = entry->ike_sa;
440 }
441 else
442 {
443 chunk_free(&hash);
444 DBG1(DBG_MGR, "ignoring message, no such IKE_SA");
445 }
446 }
447 else
448 {
449 chunk_free(&hash);
450 }
451 id->destroy(id);
452 charon->bus->set_sa(charon->bus, ike_sa);
453 return ike_sa;
454 }
455
456 pthread_mutex_lock(&(this->mutex));
457 if (get_entry_by_id(this, id, &entry) == SUCCESS)
458 {
459 /* only check out if we are not processing this request */
460 if (message->get_request(message) &&
461 message->get_message_id(message) == entry->message_id)
462 {
463 DBG1(DBG_MGR, "ignoring request with ID %d, already processing",
464 entry->message_id);
465 }
466 else if (wait_for_entry(this, entry))
467 {
468 ike_sa_id_t *ike_id = entry->ike_sa->get_id(entry->ike_sa);
469 DBG2(DBG_MGR, "IKE_SA successfully checked out");
470 entry->checked_out = TRUE;
471 entry->message_id = message->get_message_id(message);
472 if (ike_id->get_responder_spi(ike_id) == 0)
473 {
474 ike_id->set_responder_spi(ike_id, id->get_responder_spi(id));
475 }
476 ike_sa = entry->ike_sa;
477 }
478 }
479 pthread_mutex_unlock(&this->mutex);
480 id->destroy(id);
481 charon->bus->set_sa(charon->bus, ike_sa);
482 return ike_sa;
483 }
484
485 /**
486 * Implementation of of ike_sa_manager.checkout_by_config.
487 */
488 static ike_sa_t* checkout_by_config(private_ike_sa_manager_t *this,
489 peer_cfg_t *peer_cfg)
490 {
491 iterator_t *iterator;
492 entry_t *entry;
493 ike_sa_t *ike_sa = NULL;
494 identification_t *my_id, *other_id;
495 host_t *my_host, *other_host;
496 ike_cfg_t *ike_cfg;
497
498 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
499 my_host = ike_cfg->get_my_host(ike_cfg);
500 other_host = ike_cfg->get_other_host(ike_cfg);
501 my_id = peer_cfg->get_my_id(peer_cfg);
502 other_id = peer_cfg->get_other_id(peer_cfg);
503
504 pthread_mutex_lock(&(this->mutex));
505
506 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
507 while (iterator->iterate(iterator, (void**)&entry))
508 {
509 identification_t *found_my_id, *found_other_id;
510 host_t *found_my_host, *found_other_host;
511 int wc;
512
513 if (!wait_for_entry(this, entry))
514 {
515 continue;
516 }
517
518 if (entry->ike_sa->get_state(entry->ike_sa) == IKE_DELETING)
519 {
520 /* skip IKE_SA which are not useable */
521 continue;
522 }
523
524 found_my_id = entry->ike_sa->get_my_id(entry->ike_sa);
525 found_other_id = entry->ike_sa->get_other_id(entry->ike_sa);
526 found_my_host = entry->ike_sa->get_my_host(entry->ike_sa);
527 found_other_host = entry->ike_sa->get_other_host(entry->ike_sa);
528
529 if (found_my_id->get_type(found_my_id) == ID_ANY &&
530 found_other_id->get_type(found_other_id) == ID_ANY)
531 {
532 /* IKE_SA has no IDs yet, so we can't use it */
533 continue;
534 }
535 DBG2(DBG_MGR, "candidate IKE_SA for \n\t%H[%D]...%H[%D]\n\t%H[%D]...%H[%D]",
536 my_host, my_id, other_host, other_id,
537 found_my_host, found_my_id, found_other_host, found_other_id);
538 /* compare ID and hosts. Supplied ID may contain wildcards, and IP
539 * may be %any. */
540 if ((my_host->is_anyaddr(my_host) ||
541 my_host->ip_equals(my_host, found_my_host)) &&
542 (other_host->is_anyaddr(other_host) ||
543 other_host->ip_equals(other_host, found_other_host)) &&
544 found_my_id->matches(found_my_id, my_id, &wc) &&
545 found_other_id->matches(found_other_id, other_id, &wc) &&
546 streq(peer_cfg->get_name(peer_cfg),
547 entry->ike_sa->get_name(entry->ike_sa)))
548 {
549 /* looks good, we take this one */
550 DBG2(DBG_MGR, "found an existing IKE_SA for %H[%D]...%H[%D]",
551 my_host, my_id, other_host, other_id);
552 entry->checked_out = TRUE;
553 ike_sa = entry->ike_sa;
554 break;
555 }
556 }
557 iterator->destroy(iterator);
558
559 if (!ike_sa)
560 {
561 u_int64_t initiator_spi;
562 entry_t *new_entry;
563 ike_sa_id_t *new_ike_sa_id;
564
565 initiator_spi = get_next_spi(this);
566 new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
567 new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
568
569 /* create entry */
570 new_entry = entry_create(new_ike_sa_id);
571 DBG2(DBG_MGR, "created IKE_SA");
572 new_ike_sa_id->destroy(new_ike_sa_id);
573
574 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
575
576 /* check ike_sa out */
577 DBG2(DBG_MGR, "new IKE_SA created for IDs [%D]...[%D]", my_id, other_id);
578 new_entry->checked_out = TRUE;
579 ike_sa = new_entry->ike_sa;
580 }
581 pthread_mutex_unlock(&(this->mutex));
582 charon->bus->set_sa(charon->bus, ike_sa);
583 return ike_sa;
584 }
585
586 /**
587 * Implementation of of ike_sa_manager.checkout_by_id.
588 */
589 static ike_sa_t* checkout_by_id(private_ike_sa_manager_t *this, u_int32_t id,
590 bool child)
591 {
592 iterator_t *iterator, *children;
593 entry_t *entry;
594 ike_sa_t *ike_sa = NULL;
595 child_sa_t *child_sa;
596
597 pthread_mutex_lock(&(this->mutex));
598
599 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
600 while (iterator->iterate(iterator, (void**)&entry))
601 {
602 if (wait_for_entry(this, entry))
603 {
604 /* look for a child with such a reqid ... */
605 if (child)
606 {
607 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
608 while (children->iterate(children, (void**)&child_sa))
609 {
610 if (child_sa->get_reqid(child_sa) == id)
611 {
612 ike_sa = entry->ike_sa;
613 break;
614 }
615 }
616 children->destroy(children);
617 }
618 else /* ... or for a IKE_SA with such a unique id */
619 {
620 if (entry->ike_sa->get_unique_id(entry->ike_sa) == id)
621 {
622 ike_sa = entry->ike_sa;
623 }
624 }
625 /* got one, return */
626 if (ike_sa)
627 {
628 entry->checked_out = TRUE;
629 break;
630 }
631 }
632 }
633 iterator->destroy(iterator);
634 pthread_mutex_unlock(&(this->mutex));
635
636 charon->bus->set_sa(charon->bus, ike_sa);
637 return ike_sa;
638 }
639
640 /**
641 * Implementation of of ike_sa_manager.checkout_by_name.
642 */
643 static ike_sa_t* checkout_by_name(private_ike_sa_manager_t *this, char *name,
644 bool child)
645 {
646 iterator_t *iterator, *children;
647 entry_t *entry;
648 ike_sa_t *ike_sa = NULL;
649 child_sa_t *child_sa;
650
651 pthread_mutex_lock(&(this->mutex));
652
653 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
654 while (iterator->iterate(iterator, (void**)&entry))
655 {
656 if (wait_for_entry(this, entry))
657 {
658 /* look for a child with such a policy name ... */
659 if (child)
660 {
661 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
662 while (children->iterate(children, (void**)&child_sa))
663 {
664 if (streq(child_sa->get_name(child_sa), name))
665 {
666 ike_sa = entry->ike_sa;
667 break;
668 }
669 }
670 children->destroy(children);
671 }
672 else /* ... or for a IKE_SA with such a connection name */
673 {
674 if (streq(entry->ike_sa->get_name(entry->ike_sa), name))
675 {
676 ike_sa = entry->ike_sa;
677 }
678 }
679 /* got one, return */
680 if (ike_sa)
681 {
682 entry->checked_out = TRUE;
683 break;
684 }
685 }
686 }
687 iterator->destroy(iterator);
688 pthread_mutex_unlock(&(this->mutex));
689
690 charon->bus->set_sa(charon->bus, ike_sa);
691 return ike_sa;
692 }
693
694 /**
695 * Iterator hook for iterate, gets ike_sas instead of entries
696 */
697 static hook_result_t iterator_hook(private_ike_sa_manager_t* this, entry_t *in,
698 ike_sa_t **out)
699 {
700 /* check out entry */
701 if (wait_for_entry(this, in))
702 {
703 *out = in->ike_sa;
704 return HOOK_NEXT;
705 }
706 return HOOK_SKIP;
707 }
708
709 /**
710 * Implementation of ike_sa_manager_t.create_iterator.
711 */
712 static iterator_t *create_iterator(private_ike_sa_manager_t* this)
713 {
714 iterator_t *iterator = this->ike_sa_list->create_iterator_locked(
715 this->ike_sa_list, &this->mutex);
716
717 /* register hook to iterator over ike_sas, not entries */
718 iterator->set_iterator_hook(iterator, (iterator_hook_t*)iterator_hook, this);
719 return iterator;
720 }
721
722 /**
723 * Implementation of ike_sa_manager_t.checkin.
724 */
725 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
726 {
727 /* to check the SA back in, we look for the pointer of the ike_sa
728 * in all entries.
729 * We can't search by SPI's since the MAY have changed (e.g. on reception
730 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
731 */
732 status_t retval;
733 entry_t *entry;
734 ike_sa_id_t *ike_sa_id;
735
736 ike_sa_id = ike_sa->get_id(ike_sa);
737
738 DBG2(DBG_MGR, "checkin IKE_SA");
739
740 pthread_mutex_lock(&(this->mutex));
741
742 /* look for the entry */
743 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
744 {
745 /* ike_sa_id must be updated */
746 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
747 /* signal waiting threads */
748 entry->checked_out = FALSE;
749 entry->message_id = -1;
750 DBG2(DBG_MGR, "check-in of IKE_SA successful.");
751 pthread_cond_signal(&(entry->condvar));
752 retval = SUCCESS;
753 }
754 else
755 {
756 DBG2(DBG_MGR, "tried to check in nonexisting IKE_SA");
757 /* this SA is no more, this REALLY should not happen */
758 retval = NOT_FOUND;
759 }
760
761 DBG2(DBG_MGR, "%d IKE_SAs in manager now",
762 this->ike_sa_list->get_count(this->ike_sa_list));
763 pthread_mutex_unlock(&(this->mutex));
764
765 charon->bus->set_sa(charon->bus, NULL);
766 return retval;
767 }
768
769
770 /**
771 * Implementation of ike_sa_manager_t.checkin_and_destroy.
772 */
773 static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
774 {
775 /* deletion is a bit complex, we must garant that no thread is waiting for
776 * this SA.
777 * We take this SA from the list, and start signaling while threads
778 * are in the condvar.
779 */
780 entry_t *entry;
781 status_t retval;
782 ike_sa_id_t *ike_sa_id;
783
784 ike_sa_id = ike_sa->get_id(ike_sa);
785 DBG2(DBG_MGR, "checkin and destroy IKE_SA");
786
787 pthread_mutex_lock(&(this->mutex));
788
789 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
790 {
791 /* drive out waiting threads, as we are in hurry */
792 entry->driveout_waiting_threads = TRUE;
793
794 delete_entry(this, entry);
795
796 DBG2(DBG_MGR, "check-in and destroy of IKE_SA successful");
797 retval = SUCCESS;
798 }
799 else
800 {
801 DBG2(DBG_MGR, "tried to check-in and delete nonexisting IKE_SA");
802 retval = NOT_FOUND;
803 }
804
805 pthread_mutex_unlock(&(this->mutex));
806 charon->bus->set_sa(charon->bus, ike_sa);
807 return retval;
808 }
809
810 /**
811 * Implementation of ike_sa_manager_t.get_half_open_count.
812 */
813 static int get_half_open_count(private_ike_sa_manager_t *this, host_t *ip)
814 {
815 iterator_t *iterator;
816 entry_t *entry;
817 int count = 0;
818
819 pthread_mutex_lock(&(this->mutex));
820 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
821 while (iterator->iterate(iterator, (void**)&entry))
822 {
823 /* we check if we have a responder CONNECTING IKE_SA without checkout */
824 if (!entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
825 entry->ike_sa->get_state(entry->ike_sa) == IKE_CONNECTING)
826 {
827 /* if we have a host, we have wait until no other uses the IKE_SA */
828 if (ip)
829 {
830 if (wait_for_entry(this, entry) && ip->ip_equals(ip,
831 entry->ike_sa->get_other_host(entry->ike_sa)))
832 {
833 count++;
834 }
835 }
836 else
837 {
838 count++;
839 }
840 }
841 }
842 iterator->destroy(iterator);
843
844 pthread_mutex_unlock(&(this->mutex));
845 return count;
846 }
847
848 /**
849 * Implementation of ike_sa_manager_t.destroy.
850 */
851 static void destroy(private_ike_sa_manager_t *this)
852 {
853 /* destroy all list entries */
854 linked_list_t *list = this->ike_sa_list;
855 iterator_t *iterator;
856 entry_t *entry;
857
858 pthread_mutex_lock(&(this->mutex));
859 DBG2(DBG_MGR, "going to destroy IKE_SA manager and all managed IKE_SA's");
860 /* Step 1: drive out all waiting threads */
861 DBG2(DBG_MGR, "set driveout flags for all stored IKE_SA's");
862 iterator = list->create_iterator(list, TRUE);
863 while (iterator->iterate(iterator, (void**)&entry))
864 {
865 /* do not accept new threads, drive out waiting threads */
866 entry->driveout_new_threads = TRUE;
867 entry->driveout_waiting_threads = TRUE;
868 }
869 DBG2(DBG_MGR, "wait for all threads to leave IKE_SA's");
870 /* Step 2: wait until all are gone */
871 iterator->reset(iterator);
872 while (iterator->iterate(iterator, (void**)&entry))
873 {
874 while (entry->waiting_threads)
875 {
876 /* wake up all */
877 pthread_cond_broadcast(&(entry->condvar));
878 /* go sleeping until they are gone */
879 pthread_cond_wait(&(entry->condvar), &(this->mutex));
880 }
881 }
882 DBG2(DBG_MGR, "delete all IKE_SA's");
883 /* Step 3: initiate deletion of all IKE_SAs */
884 iterator->reset(iterator);
885 while (iterator->iterate(iterator, (void**)&entry))
886 {
887 entry->ike_sa->delete(entry->ike_sa);
888 }
889 iterator->destroy(iterator);
890
891 DBG2(DBG_MGR, "destroy all entries");
892 /* Step 4: destroy all entries */
893 list->destroy_function(list, (void*)entry_destroy);
894 pthread_mutex_unlock(&(this->mutex));
895
896 this->randomizer->destroy(this->randomizer);
897 this->hasher->destroy(this->hasher);
898
899 free(this);
900 }
901
902 /*
903 * Described in header.
904 */
905 ike_sa_manager_t *ike_sa_manager_create()
906 {
907 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
908
909 /* assign public functions */
910 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
911 this->public.checkout = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_id_t*))checkout;
912 this->public.checkout_new = (ike_sa_t*(*)(ike_sa_manager_t*,bool))checkout_new;
913 this->public.checkout_by_message = (ike_sa_t*(*)(ike_sa_manager_t*,message_t*))checkout_by_message;
914 this->public.checkout_by_config = (ike_sa_t*(*)(ike_sa_manager_t*,peer_cfg_t*))checkout_by_config;
915 this->public.checkout_by_id = (ike_sa_t*(*)(ike_sa_manager_t*,u_int32_t,bool))checkout_by_id;
916 this->public.checkout_by_name = (ike_sa_t*(*)(ike_sa_manager_t*,char*,bool))checkout_by_name;
917 this->public.create_iterator = (iterator_t*(*)(ike_sa_manager_t*))create_iterator;
918 this->public.checkin = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
919 this->public.checkin_and_destroy = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
920 this->public.get_half_open_count = (int(*)(ike_sa_manager_t*,host_t*))get_half_open_count;
921
922 /* initialize private variables */
923 this->ike_sa_list = linked_list_create();
924 pthread_mutex_init(&this->mutex, NULL);
925 this->randomizer = randomizer_create();
926 this->hasher = hasher_create(HASH_SHA1);
927
928 return &this->public;
929 }