improved log output for checkout_by_message()
[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: %J, %d IKE_SAs in manager",
326 ike_sa_id, 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 pthread_mutex_lock(&this->mutex);
361 this->ike_sa_list->insert_last(this->ike_sa_list, entry);
362 entry->checked_out = TRUE;
363 pthread_mutex_unlock(&this->mutex);
364 DBG2(DBG_MGR, "created IKE_SA: %J, %d IKE_SAs in manager",
365 id, this->ike_sa_list->get_count(this->ike_sa_list));
366 return entry->ike_sa;
367 }
368
369 /**
370 * Implementation of of ike_sa_manager.checkout_by_id.
371 */
372 static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
373 message_t *message)
374 {
375 entry_t *entry;
376 ike_sa_t *ike_sa = NULL;
377 ike_sa_id_t *id = message->get_ike_sa_id(message);
378 id = id->clone(id);
379 id->switch_initiator(id);
380
381 DBG2(DBG_MGR, "checkout IKE_SA: %J by message, %d IKE_SAs in manager",
382 id, this->ike_sa_list->get_count(this->ike_sa_list));
383
384 if (message->get_request(message) &&
385 message->get_exchange_type(message) == IKE_SA_INIT)
386 {
387 /* IKE_SA_INIT request. Check for an IKE_SA with such a message hash. */
388 iterator_t *iterator;
389 chunk_t data, hash;
390
391 data = message->get_packet_data(message);
392 this->hasher->allocate_hash(this->hasher, data, &hash);
393 chunk_free(&data);
394
395 pthread_mutex_lock(&this->mutex);
396 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
397 while (iterator->iterate(iterator, (void**)&entry))
398 {
399 if (chunk_equals(hash, entry->init_hash))
400 {
401 if (entry->message_id == 0)
402 {
403 iterator->destroy(iterator);
404 pthread_mutex_unlock(&this->mutex);
405 chunk_free(&hash);
406 id->destroy(id);
407 DBG1(DBG_MGR, "ignoring IKE_SA_INIT, already processing");
408 return NULL;
409 }
410 else if (wait_for_entry(this, entry))
411 {
412 DBG2(DBG_MGR, "IKE_SA checked out by hash");
413 entry->checked_out = TRUE;
414 entry->message_id = message->get_message_id(message);
415 ike_sa = entry->ike_sa;
416 }
417 break;
418 }
419 }
420 iterator->destroy(iterator);
421 pthread_mutex_unlock(&this->mutex);
422
423 if (ike_sa == NULL)
424 {
425 if (id->get_responder_spi(id) == 0 &&
426 message->get_exchange_type(message) == IKE_SA_INIT)
427 {
428 /* no IKE_SA found, create a new one */
429 id->set_responder_spi(id, get_next_spi(this));
430 entry = entry_create(id);
431
432 pthread_mutex_lock(&this->mutex);
433 this->ike_sa_list->insert_last(this->ike_sa_list, entry);
434 entry->checked_out = TRUE;
435 entry->message_id = message->get_message_id(message);
436 pthread_mutex_unlock(&this->mutex);
437 entry->init_hash = hash;
438 ike_sa = entry->ike_sa;
439 }
440 else
441 {
442 DBG1(DBG_MGR, "ignoring message for %J, no such IKE_SA", id);
443 }
444 }
445 else
446 {
447 chunk_free(&hash);
448 }
449 id->destroy(id);
450 charon->bus->set_sa(charon->bus, ike_sa);
451 return ike_sa;
452 }
453
454 pthread_mutex_lock(&(this->mutex));
455 if (get_entry_by_id(this, id, &entry) == SUCCESS)
456 {
457 /* only check out if we are not processing this request */
458 if (message->get_request(message) &&
459 message->get_message_id(message) == entry->message_id)
460 {
461 DBG1(DBG_MGR, "ignoring request with ID %d, already processing",
462 entry->message_id);
463 }
464 else if (wait_for_entry(this, entry))
465 {
466 ike_sa_id_t *ike_id = entry->ike_sa->get_id(entry->ike_sa);
467 DBG2(DBG_MGR, "IKE_SA successfully checked out");
468 entry->checked_out = TRUE;
469 entry->message_id = message->get_message_id(message);
470 if (ike_id->get_responder_spi(ike_id) == 0)
471 {
472 ike_id->set_responder_spi(ike_id, id->get_responder_spi(id));
473 }
474 ike_sa = entry->ike_sa;
475 }
476 }
477 pthread_mutex_unlock(&this->mutex);
478 id->destroy(id);
479 charon->bus->set_sa(charon->bus, ike_sa);
480 return ike_sa;
481 }
482
483 /**
484 * Implementation of of ike_sa_manager.checkout_by_id.
485 */
486 static ike_sa_t* checkout_by_peer(private_ike_sa_manager_t *this,
487 host_t *my_host, host_t *other_host,
488 identification_t *my_id,
489 identification_t *other_id)
490 {
491 iterator_t *iterator;
492 entry_t *entry;
493 ike_sa_t *ike_sa = NULL;
494
495 pthread_mutex_lock(&(this->mutex));
496
497 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
498 while (iterator->iterate(iterator, (void**)&entry))
499 {
500 identification_t *found_my_id, *found_other_id;
501 host_t *found_my_host, *found_other_host;
502 int wc;
503
504 if (!wait_for_entry(this, entry))
505 {
506 continue;
507 }
508
509 if (entry->ike_sa->get_state(entry->ike_sa) == IKE_DELETING)
510 {
511 /* skip IKE_SA which are not useable */
512 continue;
513 }
514
515 found_my_id = entry->ike_sa->get_my_id(entry->ike_sa);
516 found_other_id = entry->ike_sa->get_other_id(entry->ike_sa);
517 found_my_host = entry->ike_sa->get_my_host(entry->ike_sa);
518 found_other_host = entry->ike_sa->get_other_host(entry->ike_sa);
519
520 if (found_my_id->get_type(found_my_id) == ID_ANY &&
521 found_other_id->get_type(found_other_id) == ID_ANY)
522 {
523 /* IKE_SA has no IDs yet, so we can't use it */
524 continue;
525 }
526
527 /* compare ID and hosts. Supplied ID may contain wildcards, and IP
528 * may be %any. */
529 if ((found_my_host->is_anyaddr(found_my_host) ||
530 my_host->ip_equals(my_host, found_my_host)) &&
531 (found_other_host->is_anyaddr(found_other_host) ||
532 other_host->ip_equals(other_host, found_other_host)) &&
533 found_my_id->matches(found_my_id, my_id, &wc) &&
534 found_other_id->matches(found_other_id, other_id, &wc))
535 {
536 /* looks good, we take this one */
537 DBG2(DBG_MGR, "found an existing IKE_SA for %H[%D]...%H[%D]",
538 my_host, other_host, my_id, other_id);
539 entry->checked_out = TRUE;
540 ike_sa = entry->ike_sa;
541 }
542 }
543 iterator->destroy(iterator);
544
545 if (!ike_sa)
546 {
547 u_int64_t initiator_spi;
548 entry_t *new_entry;
549 ike_sa_id_t *new_ike_sa_id;
550
551 initiator_spi = get_next_spi(this);
552 new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
553 new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
554
555 /* create entry */
556 new_entry = entry_create(new_ike_sa_id);
557 DBG2(DBG_MGR, "created IKE_SA: %J", new_ike_sa_id);
558 new_ike_sa_id->destroy(new_ike_sa_id);
559
560 this->ike_sa_list->insert_last(this->ike_sa_list, new_entry);
561
562 /* check ike_sa out */
563 DBG2(DBG_MGR, "new IKE_SA created for IDs [%D]...[%D]", my_id, other_id);
564 new_entry->checked_out = TRUE;
565 ike_sa = new_entry->ike_sa;
566 }
567 pthread_mutex_unlock(&(this->mutex));
568 charon->bus->set_sa(charon->bus, ike_sa);
569 return ike_sa;
570 }
571
572 /**
573 * Implementation of of ike_sa_manager.checkout_by_id.
574 */
575 static ike_sa_t* checkout_by_id(private_ike_sa_manager_t *this, u_int32_t id,
576 bool child)
577 {
578 iterator_t *iterator, *children;
579 entry_t *entry;
580 ike_sa_t *ike_sa = NULL;
581 child_sa_t *child_sa;
582
583 pthread_mutex_lock(&(this->mutex));
584
585 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
586 while (iterator->iterate(iterator, (void**)&entry))
587 {
588 if (wait_for_entry(this, entry))
589 {
590 /* look for a child with such a reqid ... */
591 if (child)
592 {
593 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
594 while (children->iterate(children, (void**)&child_sa))
595 {
596 if (child_sa->get_reqid(child_sa) == id)
597 {
598 ike_sa = entry->ike_sa;
599 break;
600 }
601 }
602 children->destroy(children);
603 }
604 else /* ... or for a IKE_SA with such a unique id */
605 {
606 if (entry->ike_sa->get_unique_id(entry->ike_sa) == id)
607 {
608 ike_sa = entry->ike_sa;
609 }
610 }
611 /* got one, return */
612 if (ike_sa)
613 {
614 entry->checked_out = TRUE;
615 break;
616 }
617 }
618 }
619 iterator->destroy(iterator);
620 pthread_mutex_unlock(&(this->mutex));
621
622 charon->bus->set_sa(charon->bus, ike_sa);
623 return ike_sa;
624 }
625
626 /**
627 * Implementation of of ike_sa_manager.checkout_by_name.
628 */
629 static ike_sa_t* checkout_by_name(private_ike_sa_manager_t *this, char *name,
630 bool child)
631 {
632 iterator_t *iterator, *children;
633 entry_t *entry;
634 ike_sa_t *ike_sa = NULL;
635 child_sa_t *child_sa;
636
637 pthread_mutex_lock(&(this->mutex));
638
639 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
640 while (iterator->iterate(iterator, (void**)&entry))
641 {
642 if (wait_for_entry(this, entry))
643 {
644 /* look for a child with such a policy name ... */
645 if (child)
646 {
647 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
648 while (children->iterate(children, (void**)&child_sa))
649 {
650 if (streq(child_sa->get_name(child_sa), name))
651 {
652 ike_sa = entry->ike_sa;
653 break;
654 }
655 }
656 children->destroy(children);
657 }
658 else /* ... or for a IKE_SA with such a connection name */
659 {
660 if (streq(entry->ike_sa->get_name(entry->ike_sa), name))
661 {
662 ike_sa = entry->ike_sa;
663 }
664 }
665 /* got one, return */
666 if (ike_sa)
667 {
668 entry->checked_out = TRUE;
669 break;
670 }
671 }
672 }
673 iterator->destroy(iterator);
674 pthread_mutex_unlock(&(this->mutex));
675
676 charon->bus->set_sa(charon->bus, ike_sa);
677 return ike_sa;
678 }
679
680 /**
681 * Iterator hook for iterate, gets ike_sas instead of entries
682 */
683 static bool iterator_hook(private_ike_sa_manager_t* this, entry_t *in,
684 ike_sa_t **out)
685 {
686 /* check out entry */
687 if (wait_for_entry(this, in))
688 {
689 *out = in->ike_sa;
690 return TRUE;
691 }
692 return FALSE;
693 }
694
695 /**
696 * Implementation of ike_sa_manager_t.create_iterator.
697 */
698 static iterator_t *create_iterator(private_ike_sa_manager_t* this)
699 {
700 iterator_t *iterator = this->ike_sa_list->create_iterator_locked(
701 this->ike_sa_list, &this->mutex);
702 /* register hook to iterator over ike_sas, not entries */
703 iterator->set_iterator_hook(iterator, (iterator_hook_t*)iterator_hook, this);
704 return iterator;
705 }
706
707 /**
708 * Implementation of ike_sa_manager_t.checkin.
709 */
710 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
711 {
712 /* to check the SA back in, we look for the pointer of the ike_sa
713 * in all entries.
714 * We can't search by SPI's since the MAY have changed (e.g. on reception
715 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
716 */
717 status_t retval;
718 entry_t *entry;
719 ike_sa_id_t *ike_sa_id;
720
721 ike_sa_id = ike_sa->get_id(ike_sa);
722
723 DBG2(DBG_MGR, "checkin IKE_SA: %J", ike_sa_id);
724
725 pthread_mutex_lock(&(this->mutex));
726
727 /* look for the entry */
728 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
729 {
730 /* ike_sa_id must be updated */
731 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
732 /* signal waiting threads */
733 entry->checked_out = FALSE;
734 entry->message_id = -1;
735 DBG2(DBG_MGR, "check-in of IKE_SA successful.");
736 pthread_cond_signal(&(entry->condvar));
737 retval = SUCCESS;
738 }
739 else
740 {
741 DBG2(DBG_MGR, "tried to check in nonexisting IKE_SA");
742 /* this SA is no more, this REALLY should not happen */
743 retval = NOT_FOUND;
744 }
745
746 DBG2(DBG_MGR, "%d IKE_SAs in manager now",
747 this->ike_sa_list->get_count(this->ike_sa_list));
748 pthread_mutex_unlock(&(this->mutex));
749
750 charon->bus->set_sa(charon->bus, NULL);
751 return retval;
752 }
753
754
755 /**
756 * Implementation of ike_sa_manager_t.checkin_and_destroy.
757 */
758 static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
759 {
760 /* deletion is a bit complex, we must garant that no thread is waiting for
761 * this SA.
762 * We take this SA from the list, and start signaling while threads
763 * are in the condvar.
764 */
765 entry_t *entry;
766 status_t retval;
767 ike_sa_id_t *ike_sa_id;
768
769 ike_sa_id = ike_sa->get_id(ike_sa);
770 DBG2(DBG_MGR, "checkin and destroy IKE_SA: %J", ike_sa_id);
771
772 pthread_mutex_lock(&(this->mutex));
773
774 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
775 {
776 /* drive out waiting threads, as we are in hurry */
777 entry->driveout_waiting_threads = TRUE;
778
779 delete_entry(this, entry);
780
781 DBG2(DBG_MGR, "check-in and destroy of IKE_SA successful");
782 retval = SUCCESS;
783 }
784 else
785 {
786 DBG2(DBG_MGR, "tried to check-in and delete nonexisting IKE_SA");
787 retval = NOT_FOUND;
788 }
789
790 pthread_mutex_unlock(&(this->mutex));
791 charon->bus->set_sa(charon->bus, ike_sa);
792 return retval;
793 }
794
795 /**
796 * Implementation of ike_sa_manager_t.get_half_open_count.
797 */
798 static int get_half_open_count(private_ike_sa_manager_t *this, host_t *ip)
799 {
800 iterator_t *iterator;
801 entry_t *entry;
802 int count = 0;
803
804 pthread_mutex_lock(&(this->mutex));
805 iterator = this->ike_sa_list->create_iterator(this->ike_sa_list, TRUE);
806 while (iterator->iterate(iterator, (void**)&entry))
807 {
808 /* we check if we have a responder CONNECTING IKE_SA without checkout */
809 if (!entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
810 entry->ike_sa->get_state(entry->ike_sa) == IKE_CONNECTING)
811 {
812 /* if we have a host, we have wait until no other uses the IKE_SA */
813 if (ip)
814 {
815 if (wait_for_entry(this, entry) && ip->ip_equals(ip,
816 entry->ike_sa->get_other_host(entry->ike_sa)))
817 {
818 count++;
819 }
820 }
821 else
822 {
823 count++;
824 }
825 }
826 }
827 iterator->destroy(iterator);
828
829 pthread_mutex_unlock(&(this->mutex));
830 return count;
831 }
832
833 /**
834 * Implementation of ike_sa_manager_t.destroy.
835 */
836 static void destroy(private_ike_sa_manager_t *this)
837 {
838 /* destroy all list entries */
839 linked_list_t *list = this->ike_sa_list;
840 iterator_t *iterator;
841 entry_t *entry;
842
843 pthread_mutex_lock(&(this->mutex));
844 DBG2(DBG_MGR, "going to destroy IKE_SA manager and all managed IKE_SA's");
845 /* Step 1: drive out all waiting threads */
846 DBG2(DBG_MGR, "set driveout flags for all stored IKE_SA's");
847 iterator = list->create_iterator(list, TRUE);
848 while (iterator->iterate(iterator, (void**)&entry))
849 {
850 /* do not accept new threads, drive out waiting threads */
851 entry->driveout_new_threads = TRUE;
852 entry->driveout_waiting_threads = TRUE;
853 }
854 DBG2(DBG_MGR, "wait for all threads to leave IKE_SA's");
855 /* Step 2: wait until all are gone */
856 iterator->reset(iterator);
857 while (iterator->iterate(iterator, (void**)&entry))
858 {
859 while (entry->waiting_threads)
860 {
861 /* wake up all */
862 pthread_cond_broadcast(&(entry->condvar));
863 /* go sleeping until they are gone */
864 pthread_cond_wait(&(entry->condvar), &(this->mutex));
865 }
866 }
867 DBG2(DBG_MGR, "delete all IKE_SA's");
868 /* Step 3: initiate deletion of all IKE_SAs */
869 iterator->reset(iterator);
870 while (iterator->iterate(iterator, (void**)&entry))
871 {
872 entry->ike_sa->delete(entry->ike_sa);
873 }
874 iterator->destroy(iterator);
875
876 DBG2(DBG_MGR, "destroy all entries");
877 /* Step 4: destroy all entries */
878 list->destroy_function(list, (void*)entry_destroy);
879 pthread_mutex_unlock(&(this->mutex));
880
881 this->randomizer->destroy(this->randomizer);
882 this->hasher->destroy(this->hasher);
883
884 free(this);
885 }
886
887 /*
888 * Described in header.
889 */
890 ike_sa_manager_t *ike_sa_manager_create()
891 {
892 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
893
894 /* assign public functions */
895 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
896 this->public.checkout = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_id_t*))checkout;
897 this->public.checkout_new = (ike_sa_t*(*)(ike_sa_manager_t*,bool))checkout_new;
898 this->public.checkout_by_message = (ike_sa_t*(*)(ike_sa_manager_t*,message_t*))checkout_by_message;
899 this->public.checkout_by_peer = (ike_sa_t*(*)(ike_sa_manager_t*,host_t*,host_t*,identification_t*,identification_t*))checkout_by_peer;
900 this->public.checkout_by_id = (ike_sa_t*(*)(ike_sa_manager_t*,u_int32_t,bool))checkout_by_id;
901 this->public.checkout_by_name = (ike_sa_t*(*)(ike_sa_manager_t*,char*,bool))checkout_by_name;
902 this->public.create_iterator = (iterator_t*(*)(ike_sa_manager_t*))create_iterator;
903 this->public.checkin = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
904 this->public.checkin_and_destroy = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
905 this->public.get_half_open_count = (int(*)(ike_sa_manager_t*,host_t*))get_half_open_count;
906
907 /* initialize private variables */
908 this->ike_sa_list = linked_list_create();
909 pthread_mutex_init(&this->mutex, NULL);
910 this->randomizer = randomizer_create();
911 this->hasher = hasher_create(HASH_SHA1);
912
913 return &this->public;
914 }