optimized half-open IKE_SA lookup (no checkout)
[strongswan.git] / src / charon / sa / ike_sa_manager.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <pthread.h>
20 #include <string.h>
21
22 #include "ike_sa_manager.h"
23
24 #include <daemon.h>
25 #include <sa/ike_sa_id.h>
26 #include <bus/bus.h>
27 #include <utils/linked_list.h>
28 #include <crypto/hashers/hasher.h>
29
30 typedef struct entry_t entry_t;
31
32 /**
33 * An entry in the linked list, contains IKE_SA, locking and lookup data.
34 */
35 struct entry_t {
36
37 /**
38 * Number of threads waiting for this ike_sa_t object.
39 */
40 int waiting_threads;
41
42 /**
43 * Condvar where threads can wait until ike_sa_t object is free for use again.
44 */
45 pthread_cond_t condvar;
46
47 /**
48 * Is this ike_sa currently checked out?
49 */
50 bool checked_out;
51
52 /**
53 * Does this SA drives out new threads?
54 */
55 bool driveout_new_threads;
56
57 /**
58 * Does this SA drives out waiting threads?
59 */
60 bool driveout_waiting_threads;
61
62 /**
63 * Identifiaction of an IKE_SA (SPIs).
64 */
65 ike_sa_id_t *ike_sa_id;
66
67 /**
68 * The contained ike_sa_t object.
69 */
70 ike_sa_t *ike_sa;
71
72 /**
73 * hash of the IKE_SA_INIT message, used to detect retransmissions
74 */
75 chunk_t init_hash;
76
77 /**
78 * remote host address, required for DoS detection
79 */
80 host_t *other;
81
82 /**
83 * message ID currently processing, if any
84 */
85 u_int32_t message_id;
86 };
87
88 /**
89 * Implementation of entry_t.destroy.
90 */
91 static status_t entry_destroy(entry_t *this)
92 {
93 /* also destroy IKE SA */
94 this->ike_sa->destroy(this->ike_sa);
95 this->ike_sa_id->destroy(this->ike_sa_id);
96 chunk_free(&this->init_hash);
97 DESTROY_IF(this->other);
98 free(this);
99 return SUCCESS;
100 }
101
102 /**
103 * Creates a new entry for the ike_sa_t list.
104 */
105 static entry_t *entry_create(ike_sa_id_t *ike_sa_id)
106 {
107 entry_t *this = malloc_thing(entry_t);
108
109 this->waiting_threads = 0;
110 pthread_cond_init(&this->condvar, NULL);
111
112 /* we set checkout flag when we really give it out */
113 this->checked_out = FALSE;
114 this->driveout_new_threads = FALSE;
115 this->driveout_waiting_threads = FALSE;
116 this->message_id = -1;
117 this->init_hash = chunk_empty;
118 this->other = NULL;
119
120 /* ike_sa_id is always cloned */
121 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
122
123 /* create new ike_sa */
124 this->ike_sa = ike_sa_create(ike_sa_id);
125
126 return this;
127 }
128
129
130 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t;
131
132 /**
133 * Additional private members of ike_sa_manager_t.
134 */
135 struct private_ike_sa_manager_t {
136 /**
137 * Public interface of ike_sa_manager_t.
138 */
139 ike_sa_manager_t public;
140
141 /**
142 * Lock for exclusivly accessing the manager.
143 */
144 pthread_mutex_t mutex;
145
146 /**
147 * Linked list with entries for the ike_sa_t objects.
148 */
149 linked_list_t *ike_sa_list;
150
151 /**
152 * RNG to get random SPIs for our side
153 */
154 rng_t *rng;
155
156 /**
157 * SHA1 hasher for IKE_SA_INIT retransmit detection
158 */
159 hasher_t *hasher;
160 };
161
162 /**
163 * Implementation of private_ike_sa_manager_t.get_entry_by_id.
164 */
165 static status_t get_entry_by_id(private_ike_sa_manager_t *this,
166 ike_sa_id_t *ike_sa_id, entry_t **entry)
167 {
168 enumerator_t *enumerator;
169 entry_t *current;
170 status_t status;
171
172 /* create enumerator over list of ike_sa's */
173 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
174
175 /* default status */
176 status = NOT_FOUND;
177
178 while (enumerator->enumerate(enumerator, &current))
179 {
180 if (current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id))
181 {
182 DBG2(DBG_MGR, "found entry by both SPIs");
183 *entry = current;
184 status = SUCCESS;
185 break;
186 }
187 if (ike_sa_id->get_responder_spi(ike_sa_id) == 0 ||
188 current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0)
189 {
190 /* seems to be a half ready ike_sa */
191 if ((current->ike_sa_id->get_initiator_spi(current->ike_sa_id) ==
192 ike_sa_id->get_initiator_spi(ike_sa_id)) &&
193 (current->ike_sa_id->is_initiator(ike_sa_id) ==
194 ike_sa_id->is_initiator(current->ike_sa_id)))
195 {
196 DBG2(DBG_MGR, "found entry by initiator SPI");
197 *entry = current;
198 status = SUCCESS;
199 break;
200 }
201 }
202 }
203
204 enumerator->destroy(enumerator);
205 return status;
206 }
207
208 /**
209 * Implementation of private_ike_sa_manager_t.get_entry_by_sa.
210 */
211 static status_t get_entry_by_sa(private_ike_sa_manager_t *this,
212 ike_sa_t *ike_sa, entry_t **entry)
213 {
214 enumerator_t *enumerator;
215 entry_t *current;
216 status_t status;
217
218 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
219
220 /* default status */
221 status = NOT_FOUND;
222
223 while (enumerator->enumerate(enumerator, &current))
224 {
225 /* only pointers are compared */
226 if (current->ike_sa == ike_sa)
227 {
228 DBG2(DBG_MGR, "found entry by pointer");
229 *entry = current;
230 status = SUCCESS;
231 break;
232 }
233 }
234 enumerator->destroy(enumerator);
235
236 return status;
237 }
238
239 /**
240 * Implementation of private_ike_sa_manager_s.delete_entry.
241 */
242 static status_t delete_entry(private_ike_sa_manager_t *this, entry_t *entry)
243 {
244 enumerator_t *enumerator;
245 entry_t *current;
246 status_t status;
247
248 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
249
250 status = NOT_FOUND;
251
252 while (enumerator->enumerate(enumerator, &current))
253 {
254 if (current == entry)
255 {
256 /* mark it, so now new threads can get this entry */
257 entry->driveout_new_threads = TRUE;
258 /* wait until all workers have done their work */
259 while (entry->waiting_threads)
260 {
261 /* wake up all */
262 pthread_cond_broadcast(&(entry->condvar));
263 /* they will wake us again when their work is done */
264 pthread_cond_wait(&(entry->condvar), &(this->mutex));
265 }
266
267 DBG2(DBG_MGR, "found entry by pointer, deleting it");
268 this->ike_sa_list->remove_at(this->ike_sa_list, enumerator);
269 entry_destroy(entry);
270 status = SUCCESS;
271 break;
272 }
273 }
274 enumerator->destroy(enumerator);
275 return status;
276 }
277
278 /**
279 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
280 * acquireable
281 */
282 static bool wait_for_entry(private_ike_sa_manager_t *this, entry_t *entry)
283 {
284 if (entry->driveout_new_threads)
285 {
286 /* we are not allowed to get this */
287 return FALSE;
288 }
289 while (entry->checked_out && !entry->driveout_waiting_threads)
290 {
291 /* so wait until we can get it for us.
292 * we register us as waiting. */
293 entry->waiting_threads++;
294 pthread_cond_wait(&(entry->condvar), &(this->mutex));
295 entry->waiting_threads--;
296 }
297 /* hm, a deletion request forbids us to get this SA, get next one */
298 if (entry->driveout_waiting_threads)
299 {
300 /* we must signal here, others may be waiting on it, too */
301 pthread_cond_signal(&(entry->condvar));
302 return FALSE;
303 }
304 return TRUE;
305 }
306
307 /**
308 * Implementation of private_ike_sa_manager_t.get_next_spi.
309 */
310 static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
311 {
312 u_int64_t spi;
313
314 this->rng->get_bytes(this->rng, sizeof(spi), (u_int8_t*)&spi);
315 return spi;
316 }
317
318 /**
319 * Implementation of of ike_sa_manager.checkout.
320 */
321 static ike_sa_t* checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
322 {
323 ike_sa_t *ike_sa = NULL;
324 entry_t *entry;
325
326 DBG2(DBG_MGR, "checkout IKE_SA, %d IKE_SAs in manager",
327 this->ike_sa_list->get_count(this->ike_sa_list));
328
329 pthread_mutex_lock(&(this->mutex));
330 if (get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
331 {
332 if (wait_for_entry(this, entry))
333 {
334 DBG2(DBG_MGR, "IKE_SA successfully checked out");
335 entry->checked_out = TRUE;
336 ike_sa = entry->ike_sa;
337 }
338 }
339 pthread_mutex_unlock(&this->mutex);
340 charon->bus->set_sa(charon->bus, ike_sa);
341 return ike_sa;
342 }
343
344 /**
345 * Implementation of of ike_sa_manager.checkout_new.
346 */
347 static ike_sa_t *checkout_new(private_ike_sa_manager_t* this, bool initiator)
348 {
349 entry_t *entry;
350 ike_sa_id_t *id;
351
352 if (initiator)
353 {
354 id = ike_sa_id_create(get_next_spi(this), 0, TRUE);
355 }
356 else
357 {
358 id = ike_sa_id_create(0, get_next_spi(this), FALSE);
359 }
360 entry = entry_create(id);
361 id->destroy(id);
362 pthread_mutex_lock(&this->mutex);
363 this->ike_sa_list->insert_last(this->ike_sa_list, entry);
364 entry->checked_out = TRUE;
365 pthread_mutex_unlock(&this->mutex);
366 DBG2(DBG_MGR, "created IKE_SA, %d IKE_SAs in manager",
367 this->ike_sa_list->get_count(this->ike_sa_list));
368 return entry->ike_sa;
369 }
370
371 /**
372 * Implementation of of ike_sa_manager.checkout_by_message.
373 */
374 static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
375 message_t *message)
376 {
377 entry_t *entry;
378 ike_sa_t *ike_sa = NULL;
379 ike_sa_id_t *id = message->get_ike_sa_id(message);
380 id = id->clone(id);
381 id->switch_initiator(id);
382
383 DBG2(DBG_MGR, "checkout IKE_SA by message, %d IKE_SAs in manager",
384 this->ike_sa_list->get_count(this->ike_sa_list));
385
386 if (message->get_request(message) &&
387 message->get_exchange_type(message) == IKE_SA_INIT)
388 {
389 /* IKE_SA_INIT request. Check for an IKE_SA with such a message hash. */
390 enumerator_t *enumerator;
391 chunk_t data, hash;
392
393 data = message->get_packet_data(message);
394 this->hasher->allocate_hash(this->hasher, data, &hash);
395 chunk_free(&data);
396
397 pthread_mutex_lock(&this->mutex);
398 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
399 while (enumerator->enumerate(enumerator, &entry))
400 {
401 if (chunk_equals(hash, entry->init_hash))
402 {
403 if (entry->message_id == 0)
404 {
405 enumerator->destroy(enumerator);
406 pthread_mutex_unlock(&this->mutex);
407 chunk_free(&hash);
408 id->destroy(id);
409 DBG1(DBG_MGR, "ignoring IKE_SA_INIT, already processing");
410 return NULL;
411 }
412 else if (wait_for_entry(this, entry))
413 {
414 DBG2(DBG_MGR, "IKE_SA checked out by hash");
415 entry->checked_out = TRUE;
416 entry->message_id = message->get_message_id(message);
417 ike_sa = entry->ike_sa;
418 }
419 break;
420 }
421 }
422 enumerator->destroy(enumerator);
423 pthread_mutex_unlock(&this->mutex);
424
425 if (ike_sa == NULL)
426 {
427 if (id->get_responder_spi(id) == 0 &&
428 message->get_exchange_type(message) == IKE_SA_INIT)
429 {
430 /* no IKE_SA found, create a new one */
431 id->set_responder_spi(id, get_next_spi(this));
432 entry = entry_create(id);
433
434 pthread_mutex_lock(&this->mutex);
435 this->ike_sa_list->insert_last(this->ike_sa_list, entry);
436 entry->checked_out = TRUE;
437 entry->message_id = message->get_message_id(message);
438 pthread_mutex_unlock(&this->mutex);
439 entry->init_hash = hash;
440 ike_sa = entry->ike_sa;
441 }
442 else
443 {
444 chunk_free(&hash);
445 DBG1(DBG_MGR, "ignoring message, no such IKE_SA");
446 }
447 }
448 else
449 {
450 chunk_free(&hash);
451 }
452 id->destroy(id);
453 charon->bus->set_sa(charon->bus, ike_sa);
454 return ike_sa;
455 }
456
457 pthread_mutex_lock(&(this->mutex));
458 if (get_entry_by_id(this, id, &entry) == SUCCESS)
459 {
460 /* only check out if we are not processing this request */
461 if (message->get_request(message) &&
462 message->get_message_id(message) == entry->message_id)
463 {
464 DBG1(DBG_MGR, "ignoring request with ID %d, already processing",
465 entry->message_id);
466 }
467 else if (wait_for_entry(this, entry))
468 {
469 ike_sa_id_t *ike_id = entry->ike_sa->get_id(entry->ike_sa);
470 DBG2(DBG_MGR, "IKE_SA successfully checked out");
471 entry->checked_out = TRUE;
472 entry->message_id = message->get_message_id(message);
473 if (ike_id->get_responder_spi(ike_id) == 0)
474 {
475 ike_id->set_responder_spi(ike_id, id->get_responder_spi(id));
476 }
477 ike_sa = entry->ike_sa;
478 }
479 }
480 pthread_mutex_unlock(&this->mutex);
481 id->destroy(id);
482 charon->bus->set_sa(charon->bus, ike_sa);
483 return ike_sa;
484 }
485
486 /**
487 * Implementation of of ike_sa_manager.checkout_by_config.
488 */
489 static ike_sa_t* checkout_by_config(private_ike_sa_manager_t *this,
490 peer_cfg_t *peer_cfg)
491 {
492 enumerator_t *enumerator;
493 entry_t *entry;
494 ike_sa_t *ike_sa = NULL;
495 identification_t *my_id, *other_id;
496 host_t *my_host, *other_host;
497 ike_cfg_t *ike_cfg;
498
499 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
500 my_host = ike_cfg->get_my_host(ike_cfg);
501 other_host = ike_cfg->get_other_host(ike_cfg);
502 my_id = peer_cfg->get_my_id(peer_cfg);
503 other_id = peer_cfg->get_other_id(peer_cfg);
504
505 pthread_mutex_lock(&(this->mutex));
506
507 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
508 while (enumerator->enumerate(enumerator, &entry))
509 {
510 identification_t *found_my_id, *found_other_id;
511 host_t *found_my_host, *found_other_host;
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) &&
545 found_other_id->matches(found_other_id, other_id) &&
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 enumerator->destroy(enumerator);
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 enumerator_t *enumerator;
593 iterator_t *children;
594 entry_t *entry;
595 ike_sa_t *ike_sa = NULL;
596 child_sa_t *child_sa;
597
598 pthread_mutex_lock(&(this->mutex));
599
600 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
601 while (enumerator->enumerate(enumerator, &entry))
602 {
603 if (wait_for_entry(this, entry))
604 {
605 /* look for a child with such a reqid ... */
606 if (child)
607 {
608 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
609 while (children->iterate(children, (void**)&child_sa))
610 {
611 if (child_sa->get_reqid(child_sa) == id)
612 {
613 ike_sa = entry->ike_sa;
614 break;
615 }
616 }
617 children->destroy(children);
618 }
619 else /* ... or for a IKE_SA with such a unique id */
620 {
621 if (entry->ike_sa->get_unique_id(entry->ike_sa) == id)
622 {
623 ike_sa = entry->ike_sa;
624 }
625 }
626 /* got one, return */
627 if (ike_sa)
628 {
629 entry->checked_out = TRUE;
630 break;
631 }
632 }
633 }
634 enumerator->destroy(enumerator);
635 pthread_mutex_unlock(&(this->mutex));
636
637 charon->bus->set_sa(charon->bus, ike_sa);
638 return ike_sa;
639 }
640
641 /**
642 * Implementation of of ike_sa_manager.checkout_by_name.
643 */
644 static ike_sa_t* checkout_by_name(private_ike_sa_manager_t *this, char *name,
645 bool child)
646 {
647 enumerator_t *enumerator;
648 iterator_t *children;
649 entry_t *entry;
650 ike_sa_t *ike_sa = NULL;
651 child_sa_t *child_sa;
652
653 pthread_mutex_lock(&(this->mutex));
654
655 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
656 while (enumerator->enumerate(enumerator, &entry))
657 {
658 if (wait_for_entry(this, entry))
659 {
660 /* look for a child with such a policy name ... */
661 if (child)
662 {
663 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
664 while (children->iterate(children, (void**)&child_sa))
665 {
666 if (streq(child_sa->get_name(child_sa), name))
667 {
668 ike_sa = entry->ike_sa;
669 break;
670 }
671 }
672 children->destroy(children);
673 }
674 else /* ... or for a IKE_SA with such a connection name */
675 {
676 if (streq(entry->ike_sa->get_name(entry->ike_sa), name))
677 {
678 ike_sa = entry->ike_sa;
679 }
680 }
681 /* got one, return */
682 if (ike_sa)
683 {
684 entry->checked_out = TRUE;
685 break;
686 }
687 }
688 }
689 enumerator->destroy(enumerator);
690 pthread_mutex_unlock(&(this->mutex));
691
692 charon->bus->set_sa(charon->bus, ike_sa);
693 return ike_sa;
694 }
695
696 /**
697 * Implementation of ike_sa_manager_t.checkout_duplicate.
698 */
699 static ike_sa_t* checkout_duplicate(private_ike_sa_manager_t *this,
700 ike_sa_t *ike_sa)
701 {
702 enumerator_t *enumerator;
703 entry_t *entry;
704 ike_sa_t *duplicate = NULL;
705 identification_t *me, *other;
706
707 me = ike_sa->get_my_id(ike_sa);
708 other = ike_sa->get_other_id(ike_sa);
709
710 pthread_mutex_lock(&this->mutex);
711 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
712 while (enumerator->enumerate(enumerator, &entry))
713 {
714 if (entry->ike_sa == ike_sa)
715 { /* self is not a duplicate */
716 continue;
717 }
718 if (wait_for_entry(this, entry))
719 {
720 if (me->equals(me, entry->ike_sa->get_my_id(entry->ike_sa)) &&
721 other->equals(other, entry->ike_sa->get_other_id(entry->ike_sa)))
722 {
723 duplicate = entry->ike_sa;
724 entry->checked_out = TRUE;
725 break;
726 }
727 }
728 }
729 enumerator->destroy(enumerator);
730 pthread_mutex_unlock(&this->mutex);
731 return duplicate;
732 }
733
734 /**
735 * enumerator cleanup function
736 */
737 static void enumerator_unlock(private_ike_sa_manager_t *this)
738 {
739 pthread_mutex_unlock(&this->mutex);
740 }
741
742 /**
743 * enumerator filter function
744 */
745 static bool enumerator_filter(private_ike_sa_manager_t *this,
746 entry_t **in, ike_sa_t **out)
747 {
748 if (wait_for_entry(this, *in))
749 {
750 *out = (*in)->ike_sa;
751 return TRUE;
752 }
753 return FALSE;
754 }
755
756 /**
757 * Implementation of ike_sa_manager_t.create_iterator.
758 */
759 static enumerator_t *create_enumerator(private_ike_sa_manager_t* this)
760 {
761 pthread_mutex_lock(&this->mutex);
762 return enumerator_create_filter(
763 this->ike_sa_list->create_enumerator(this->ike_sa_list),
764 (void*)enumerator_filter, this, (void*)enumerator_unlock);
765 }
766
767 /**
768 * Implementation of ike_sa_manager_t.checkin.
769 */
770 static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
771 {
772 /* to check the SA back in, we look for the pointer of the ike_sa
773 * in all entries.
774 * We can't search by SPI's since the MAY have changed (e.g. on reception
775 * of a IKE_SA_INIT response). Updating of the SPI MAY be necessary...
776 */
777 status_t retval;
778 entry_t *entry;
779 ike_sa_id_t *ike_sa_id;
780
781 ike_sa_id = ike_sa->get_id(ike_sa);
782
783 DBG2(DBG_MGR, "checkin IKE_SA");
784
785 pthread_mutex_lock(&(this->mutex));
786
787 /* look for the entry */
788 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
789 {
790 /* ike_sa_id must be updated */
791 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
792 /* signal waiting threads */
793 entry->checked_out = FALSE;
794 entry->message_id = -1;
795 /* apply remote address for DoS detection */
796 DESTROY_IF(entry->other);
797 entry->other = ike_sa->get_other_host(ike_sa);
798 entry->other = entry->other->clone(entry->other);
799 DBG2(DBG_MGR, "check-in of IKE_SA successful.");
800 pthread_cond_signal(&(entry->condvar));
801 retval = SUCCESS;
802 }
803 else
804 {
805 DBG2(DBG_MGR, "tried to check in nonexisting IKE_SA");
806 /* this SA is no more, this REALLY should not happen */
807 retval = NOT_FOUND;
808 }
809
810 DBG2(DBG_MGR, "%d IKE_SAs in manager now",
811 this->ike_sa_list->get_count(this->ike_sa_list));
812 pthread_mutex_unlock(&(this->mutex));
813
814 charon->bus->set_sa(charon->bus, NULL);
815 return retval;
816 }
817
818
819 /**
820 * Implementation of ike_sa_manager_t.checkin_and_destroy.
821 */
822 static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
823 {
824 /* deletion is a bit complex, we must garant that no thread is waiting for
825 * this SA.
826 * We take this SA from the list, and start signaling while threads
827 * are in the condvar.
828 */
829 entry_t *entry;
830 status_t retval;
831 ike_sa_id_t *ike_sa_id;
832
833 ike_sa_id = ike_sa->get_id(ike_sa);
834 DBG2(DBG_MGR, "checkin and destroy IKE_SA");
835 charon->bus->set_sa(charon->bus, NULL);
836
837 pthread_mutex_lock(&(this->mutex));
838
839 if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
840 {
841 /* drive out waiting threads, as we are in hurry */
842 entry->driveout_waiting_threads = TRUE;
843
844 delete_entry(this, entry);
845
846 DBG2(DBG_MGR, "check-in and destroy of IKE_SA successful");
847 retval = SUCCESS;
848 }
849 else
850 {
851 DBG2(DBG_MGR, "tried to check-in and delete nonexisting IKE_SA");
852 retval = NOT_FOUND;
853 }
854
855 pthread_mutex_unlock(&(this->mutex));
856 return retval;
857 }
858
859 /**
860 * Implementation of ike_sa_manager_t.get_half_open_count.
861 */
862 static int get_half_open_count(private_ike_sa_manager_t *this, host_t *ip)
863 {
864 enumerator_t *enumerator;
865 entry_t *entry;
866 int count = 0;
867
868 pthread_mutex_lock(&(this->mutex));
869 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
870 while (enumerator->enumerate(enumerator, &entry))
871 {
872 /* we check if we have a responder CONNECTING IKE_SA without checkout */
873 if (!entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
874 entry->ike_sa->get_state(entry->ike_sa) == IKE_CONNECTING)
875 {
876 /* if we have a host, count only matching IKE_SAs */
877 if (ip)
878 {
879 if (entry->other && ip->ip_equals(ip, entry->other))
880 {
881 count++;
882 }
883 }
884 else
885 {
886 count++;
887 }
888 }
889 }
890 enumerator->destroy(enumerator);
891
892 pthread_mutex_unlock(&(this->mutex));
893 return count;
894 }
895
896 /**
897 * Implementation of ike_sa_manager_t.destroy.
898 */
899 static void destroy(private_ike_sa_manager_t *this)
900 {
901 /* destroy all list entries */
902 enumerator_t *enumerator;
903 entry_t *entry;
904
905 pthread_mutex_lock(&(this->mutex));
906 DBG2(DBG_MGR, "going to destroy IKE_SA manager and all managed IKE_SA's");
907 /* Step 1: drive out all waiting threads */
908 DBG2(DBG_MGR, "set driveout flags for all stored IKE_SA's");
909 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
910 while (enumerator->enumerate(enumerator, &entry))
911 {
912 /* do not accept new threads, drive out waiting threads */
913 entry->driveout_new_threads = TRUE;
914 entry->driveout_waiting_threads = TRUE;
915 }
916 enumerator->destroy(enumerator);
917 DBG2(DBG_MGR, "wait for all threads to leave IKE_SA's");
918 /* Step 2: wait until all are gone */
919 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
920 while (enumerator->enumerate(enumerator, &entry))
921 {
922 while (entry->waiting_threads)
923 {
924 /* wake up all */
925 pthread_cond_broadcast(&(entry->condvar));
926 /* go sleeping until they are gone */
927 pthread_cond_wait(&(entry->condvar), &(this->mutex));
928 }
929 }
930 enumerator->destroy(enumerator);
931 DBG2(DBG_MGR, "delete all IKE_SA's");
932 /* Step 3: initiate deletion of all IKE_SAs */
933 enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
934 while (enumerator->enumerate(enumerator, &entry))
935 {
936 entry->ike_sa->delete(entry->ike_sa);
937 }
938 enumerator->destroy(enumerator);
939
940 DBG2(DBG_MGR, "destroy all entries");
941 /* Step 4: destroy all entries */
942 this->ike_sa_list->destroy_function(this->ike_sa_list, (void*)entry_destroy);
943 pthread_mutex_unlock(&(this->mutex));
944
945 this->rng->destroy(this->rng);
946 this->hasher->destroy(this->hasher);
947
948 free(this);
949 }
950
951 /*
952 * Described in header.
953 */
954 ike_sa_manager_t *ike_sa_manager_create()
955 {
956 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
957
958 /* assign public functions */
959 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
960 this->public.checkout = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_id_t*))checkout;
961 this->public.checkout_new = (ike_sa_t*(*)(ike_sa_manager_t*,bool))checkout_new;
962 this->public.checkout_by_message = (ike_sa_t*(*)(ike_sa_manager_t*,message_t*))checkout_by_message;
963 this->public.checkout_by_config = (ike_sa_t*(*)(ike_sa_manager_t*,peer_cfg_t*))checkout_by_config;
964 this->public.checkout_by_id = (ike_sa_t*(*)(ike_sa_manager_t*,u_int32_t,bool))checkout_by_id;
965 this->public.checkout_by_name = (ike_sa_t*(*)(ike_sa_manager_t*,char*,bool))checkout_by_name;
966 this->public.checkout_duplicate = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_t *ike_sa))checkout_duplicate;
967 this->public.create_enumerator = (enumerator_t*(*)(ike_sa_manager_t*))create_enumerator;
968 this->public.checkin = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
969 this->public.checkin_and_destroy = (status_t(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
970 this->public.get_half_open_count = (int(*)(ike_sa_manager_t*,host_t*))get_half_open_count;
971
972 /* initialize private variables */
973 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_PREFERRED);
974 if (this->hasher == NULL)
975 {
976 DBG1(DBG_MGR, "manager initialization failed, no hasher supported");
977 free(this);
978 return NULL;
979 }
980 this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
981 if (this->rng == NULL)
982 {
983 DBG1(DBG_MGR, "manager initialization failed, no RNG supported");
984 this->hasher->destroy(this->hasher);
985 free(this);
986 return NULL;
987 }
988 this->ike_sa_list = linked_list_create();
989 pthread_mutex_init(&this->mutex, NULL);
990 return &this->public;
991 }
992