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