fixed possible deadlock in checkin_and_destroy
[strongswan.git] / src / charon / sa / ike_sa_manager.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
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/mutex.h>
28 #include <utils/linked_list.h>
29 #include <crypto/hashers/hasher.h>
30
31 /* the default size of the hash table (MUST be a power of 2) */
32 #define DEFAULT_HASHTABLE_SIZE 1
33
34 /* the maximum size of the hash table (MUST be a power of 2) */
35 #define MAX_HASHTABLE_SIZE (1 << 30)
36
37 /* the default number of segments (MUST be a power of 2) */
38 #define DEFAULT_SEGMENT_COUNT 1
39
40 typedef struct entry_t entry_t;
41
42 /**
43 * An entry in the linked list, contains IKE_SA, locking and lookup data.
44 */
45 struct entry_t {
46
47 /**
48 * Number of threads waiting for this ike_sa_t object.
49 */
50 int waiting_threads;
51
52 /**
53 * Condvar where threads can wait until ike_sa_t object is free for use again.
54 */
55 condvar_t *condvar;
56
57 /**
58 * Is this ike_sa currently checked out?
59 */
60 bool checked_out;
61
62 /**
63 * Does this SA drives out new threads?
64 */
65 bool driveout_new_threads;
66
67 /**
68 * Does this SA drives out waiting threads?
69 */
70 bool driveout_waiting_threads;
71
72 /**
73 * Identification of an IKE_SA (SPIs).
74 */
75 ike_sa_id_t *ike_sa_id;
76
77 /**
78 * The contained ike_sa_t object.
79 */
80 ike_sa_t *ike_sa;
81
82 /**
83 * hash of the IKE_SA_INIT message, used to detect retransmissions
84 */
85 chunk_t init_hash;
86
87 /**
88 * remote host address, required for DoS detection
89 */
90 host_t *other;
91
92 /**
93 * As responder: Is this SA half-open?
94 */
95 bool half_open;
96
97 /**
98 * own identity, required for duplicate checking
99 */
100 identification_t *my_id;
101
102 /**
103 * remote identity, required for duplicate checking
104 */
105 identification_t *other_id;
106
107 /**
108 * message ID currently processing, if any
109 */
110 u_int32_t message_id;
111 };
112
113 /**
114 * Implementation of entry_t.destroy.
115 */
116 static status_t entry_destroy(entry_t *this)
117 {
118 /* also destroy IKE SA */
119 this->ike_sa->destroy(this->ike_sa);
120 this->ike_sa_id->destroy(this->ike_sa_id);
121 chunk_free(&this->init_hash);
122 DESTROY_IF(this->other);
123 DESTROY_IF(this->my_id);
124 DESTROY_IF(this->other_id);
125 this->condvar->destroy(this->condvar);
126 free(this);
127 return SUCCESS;
128 }
129
130 /**
131 * Creates a new entry for the ike_sa_t list.
132 */
133 static entry_t *entry_create()
134 {
135 entry_t *this = malloc_thing(entry_t);
136
137 this->waiting_threads = 0;
138 this->condvar = condvar_create(CONDVAR_DEFAULT);
139
140 /* we set checkout flag when we really give it out */
141 this->checked_out = FALSE;
142 this->driveout_new_threads = FALSE;
143 this->driveout_waiting_threads = FALSE;
144 this->message_id = -1;
145 this->init_hash = chunk_empty;
146 this->other = NULL;
147 this->half_open = FALSE;
148 this->my_id = NULL;
149 this->other_id = NULL;
150 this->ike_sa_id = NULL;
151 this->ike_sa = NULL;
152
153 return this;
154 }
155
156 /**
157 * Function that matches entry_t objects by initiator SPI and the hash of the
158 * IKE_SA_INIT message.
159 */
160 static bool entry_match_by_hash(entry_t *entry, ike_sa_id_t *id, chunk_t *hash)
161 {
162 return id->get_responder_spi(id) == 0 &&
163 id->is_initiator(id) == entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
164 id->get_initiator_spi(id) == entry->ike_sa_id->get_initiator_spi(entry->ike_sa_id) &&
165 chunk_equals(*hash, entry->init_hash);
166 }
167
168 /**
169 * Function that matches entry_t objects by ike_sa_id_t.
170 */
171 static bool entry_match_by_id(entry_t *entry, ike_sa_id_t *id)
172 {
173 if (id->equals(id, entry->ike_sa_id))
174 {
175 return TRUE;
176 }
177 if ((id->get_responder_spi(id) == 0 ||
178 entry->ike_sa_id->get_responder_spi(entry->ike_sa_id) == 0) &&
179 id->is_initiator(id) == entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
180 id->get_initiator_spi(id) == entry->ike_sa_id->get_initiator_spi(entry->ike_sa_id))
181 {
182 /* this is TRUE for IKE_SAs that we initiated but have not yet received a response */
183 return TRUE;
184 }
185 return FALSE;
186 }
187
188 /**
189 * Function that matches entry_t objects by ike_sa_t pointers.
190 */
191 static bool entry_match_by_sa(entry_t *entry, ike_sa_t *ike_sa)
192 {
193 return entry->ike_sa == ike_sa;
194 }
195
196 /**
197 * Hash function for ike_sa_id_t objects.
198 */
199 static u_int ike_sa_id_hash(ike_sa_id_t *ike_sa_id)
200 {
201 /* we always use initiator spi as key */
202 return ike_sa_id->get_initiator_spi(ike_sa_id);
203 }
204
205 typedef struct half_open_t half_open_t;
206
207 /**
208 * Struct to manage half-open IKE_SAs per peer.
209 */
210 struct half_open_t {
211 /** chunk of remote host address */
212 chunk_t other;
213
214 /** the number of half-open IKE_SAs with that host */
215 u_int count;
216 };
217
218 /**
219 * Destroys a half_open_t object.
220 */
221 static void half_open_destroy(half_open_t *this)
222 {
223 chunk_free(&this->other);
224 free(this);
225 }
226
227 /**
228 * Function that matches half_open_t objects by the given IP address chunk.
229 */
230 static bool half_open_match(half_open_t *half_open, chunk_t *addr)
231 {
232 return chunk_equals(*addr, half_open->other);
233 }
234
235 typedef struct connected_peers_t connected_peers_t;
236
237 struct connected_peers_t {
238 /** own identity */
239 identification_t *my_id;
240
241 /** remote identity */
242 identification_t *other_id;
243
244 /** list of ike_sa_id_t objects of IKE_SAs between the two identities */
245 linked_list_t *sas;
246 };
247
248 static void connected_peers_destroy(connected_peers_t *this)
249 {
250 this->my_id->destroy(this->my_id);
251 this->other_id->destroy(this->other_id);
252 this->sas->destroy(this->sas);
253 free(this);
254 }
255
256 /**
257 * Function that matches connected_peers_t objects by the given ids.
258 */
259 static bool connected_peers_match(connected_peers_t *connected_peers,
260 identification_t *my_id, identification_t *other_id)
261 {
262 return my_id->equals(my_id, connected_peers->my_id) &&
263 other_id->equals(other_id, connected_peers->other_id);
264 }
265
266 typedef struct segment_t segment_t;
267
268 /**
269 * Struct to manage segments of the hash table.
270 */
271 struct segment_t {
272 /** mutex to access a segment exclusively */
273 mutex_t *mutex;
274
275 /** the number of entries in this segment */
276 u_int count;
277 };
278
279 typedef struct shareable_segment_t shareable_segment_t;
280
281 /**
282 * Struct to manage segments of the "half-open" and "connected peers" hash tables.
283 */
284 struct shareable_segment_t {
285 /** rwlock to access a segment non-/exclusively */
286 rwlock_t *lock;
287
288 /** the number of entries in this segment - in case of the "half-open table"
289 * it's the sum of all half_open_t.count in a segment. */
290 u_int count;
291 };
292
293 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t;
294
295 /**
296 * Additional private members of ike_sa_manager_t.
297 */
298 struct private_ike_sa_manager_t {
299 /**
300 * Public interface of ike_sa_manager_t.
301 */
302 ike_sa_manager_t public;
303
304 /**
305 * Hash table with entries for the ike_sa_t objects.
306 */
307 linked_list_t **ike_sa_table;
308
309 /**
310 * The size of the hash table.
311 */
312 u_int table_size;
313
314 /**
315 * Mask to map the hashes to table rows.
316 */
317 u_int table_mask;
318
319 /**
320 * Segments of the hash table.
321 */
322 segment_t *segments;
323
324 /**
325 * The number of segments.
326 */
327 u_int segment_count;
328
329 /**
330 * Mask to map a table row to a segment.
331 */
332 u_int segment_mask;
333
334 /**
335 * Hash table with half_open_t objects.
336 */
337 linked_list_t **half_open_table;
338
339 /**
340 * Segments of the "half-open" hash table.
341 */
342 shareable_segment_t *half_open_segments;
343
344 /**
345 * Hash table with connected_peers_t objects.
346 */
347 linked_list_t **connected_peers_table;
348
349 /**
350 * Segments of the "connected peers" hash table.
351 */
352 shareable_segment_t *connected_peers_segments;
353
354 /**
355 * RNG to get random SPIs for our side
356 */
357 rng_t *rng;
358
359 /**
360 * SHA1 hasher for IKE_SA_INIT retransmit detection
361 */
362 hasher_t *hasher;
363
364 /**
365 * reuse existing IKE_SAs in checkout_by_config
366 */
367 bool reuse_ikesa;
368 };
369
370 /**
371 * Acquire a lock to access the segment of the table row with the given index.
372 * It also works with the segment index directly.
373 */
374 static void lock_single_segment(private_ike_sa_manager_t *this, u_int index)
375 {
376 mutex_t *lock = this->segments[index & this->segment_mask].mutex;
377
378 lock->lock(lock);
379 }
380
381 /**
382 * Release the lock required to access the segment of the table row with the given index.
383 * It also works with the segment index directly.
384 */
385 static void unlock_single_segment(private_ike_sa_manager_t *this, u_int index)
386 {
387 mutex_t *lock = this->segments[index & this->segment_mask].mutex;
388
389 lock->unlock(lock);
390 }
391
392 /**
393 * Lock all segments
394 */
395 static void lock_all_segments(private_ike_sa_manager_t *this)
396 {
397 u_int i;
398
399 for (i = 0; i < this->segment_count; ++i)
400 {
401 this->segments[i].mutex->lock(this->segments[i].mutex);
402 }
403 }
404
405 /**
406 * Unlock all segments
407 */
408 static void unlock_all_segments(private_ike_sa_manager_t *this)
409 {
410 u_int i;
411
412 for (i = 0; i < this->segment_count; ++i)
413 {
414 this->segments[i].mutex->unlock(this->segments[i].mutex);
415 }
416 }
417
418 typedef struct private_enumerator_t private_enumerator_t;
419
420 /**
421 * hash table enumerator implementation
422 */
423 struct private_enumerator_t {
424
425 /**
426 * implements enumerator interface
427 */
428 enumerator_t enumerator;
429
430 /**
431 * associated ike_sa_manager_t
432 */
433 private_ike_sa_manager_t *manager;
434
435 /**
436 * current segment index
437 */
438 u_int segment;
439
440 /**
441 * current table row index
442 */
443 u_int row;
444
445 /**
446 * enumerator for the current table row
447 */
448 enumerator_t *current;
449 };
450
451 /**
452 * Implementation of private_enumerator_t.enumerator.enumerate.
453 */
454 static bool enumerate(private_enumerator_t *this, entry_t **entry, u_int *segment)
455 {
456 while (this->segment < this->manager->segment_count)
457 {
458 while (this->row < this->manager->table_size)
459 {
460 if (this->current)
461 {
462 entry_t *item;
463
464 if (this->current->enumerate(this->current, (void**)&item))
465 {
466 *entry = item;
467 *segment = this->segment;
468 return TRUE;
469 }
470 this->current->destroy(this->current);
471 this->current = NULL;
472 unlock_single_segment(this->manager, this->segment);
473 }
474 else
475 {
476 linked_list_t *list;
477
478 lock_single_segment(this->manager, this->segment);
479 if ((list = this->manager->ike_sa_table[this->row]) != NULL &&
480 list->get_count(list))
481 {
482 this->current = list->create_enumerator(list);
483 continue;
484 }
485 unlock_single_segment(this->manager, this->segment);
486 }
487 this->row += this->manager->segment_count;
488 }
489 this->segment++;
490 this->row = this->segment;
491 }
492 return FALSE;
493 }
494
495 /**
496 * Implementation of private_enumerator_t.enumerator.destroy.
497 */
498 static void enumerator_destroy(private_enumerator_t *this)
499 {
500 if (this->current)
501 {
502 this->current->destroy(this->current);
503 unlock_single_segment(this->manager, this->segment);
504 }
505 free(this);
506 }
507
508 /**
509 * Creates an enumerator to enumerate the entries in the hash table.
510 */
511 static enumerator_t* create_table_enumerator(private_ike_sa_manager_t *this)
512 {
513 private_enumerator_t *enumerator = malloc_thing(private_enumerator_t);
514
515 enumerator->enumerator.enumerate = (void*)enumerate;
516 enumerator->enumerator.destroy = (void*)enumerator_destroy;
517 enumerator->manager = this;
518 enumerator->segment = 0;
519 enumerator->row = 0;
520 enumerator->current = NULL;
521
522 return &enumerator->enumerator;
523 }
524
525 /**
526 * Put an entry into the hash table.
527 * Note: The caller has to unlock the returned segment.
528 */
529 static u_int put_entry(private_ike_sa_manager_t *this, entry_t *entry)
530 {
531 linked_list_t *list;
532 u_int row = ike_sa_id_hash(entry->ike_sa_id) & this->table_mask;
533 u_int segment = row & this->segment_mask;
534
535 lock_single_segment(this, segment);
536 if ((list = this->ike_sa_table[row]) == NULL)
537 {
538 list = this->ike_sa_table[row] = linked_list_create();
539 }
540 list->insert_last(list, entry);
541 this->segments[segment].count++;
542 return segment;
543 }
544
545 /**
546 * Remove an entry from the hash table.
547 * Note: The caller MUST have a lock on the segment of this entry.
548 */
549 static void remove_entry(private_ike_sa_manager_t *this, entry_t *entry)
550 {
551 linked_list_t *list;
552 u_int row = ike_sa_id_hash(entry->ike_sa_id) & this->table_mask;
553 u_int segment = row & this->segment_mask;
554
555 if ((list = this->ike_sa_table[row]) != NULL)
556 {
557 entry_t *current;
558
559 enumerator_t *enumerator = list->create_enumerator(list);
560 while (enumerator->enumerate(enumerator, &current))
561 {
562 if (current == entry)
563 {
564 list->remove_at(list, enumerator);
565 this->segments[segment].count--;
566 break;
567 }
568 }
569 enumerator->destroy(enumerator);
570 }
571 }
572
573 /**
574 * Remove the entry at the current enumerator position.
575 */
576 static void remove_entry_at(private_enumerator_t *this)
577 {
578 if (this->current)
579 {
580 linked_list_t *list = this->manager->ike_sa_table[this->row];
581 list->remove_at(list, this->current);
582 this->manager->segments[this->segment].count--;
583 }
584 }
585
586 /**
587 * Find an entry using the provided match function to compare the entries for
588 * equality.
589 */
590 static status_t get_entry_by_match_function(private_ike_sa_manager_t *this,
591 ike_sa_id_t *ike_sa_id, entry_t **entry, u_int *segment,
592 linked_list_match_t match, void *p1, void *p2)
593 {
594 entry_t *current;
595 linked_list_t *list;
596 u_int row = ike_sa_id_hash(ike_sa_id) & this->table_mask;
597 u_int seg = row & this->segment_mask;
598
599 lock_single_segment(this, seg);
600 if ((list = this->ike_sa_table[row]) != NULL)
601 {
602 if (list->find_first(list, match, (void**)&current, p1, p2) == SUCCESS)
603 {
604 *entry = current;
605 *segment = seg;
606 /* the locked segment has to be unlocked by the caller */
607 return SUCCESS;
608 }
609 }
610 unlock_single_segment(this, seg);
611 return NOT_FOUND;
612 }
613
614 /**
615 * Find an entry by ike_sa_id_t.
616 * Note: On SUCCESS, the caller has to unlock the segment.
617 */
618 static status_t get_entry_by_id(private_ike_sa_manager_t *this,
619 ike_sa_id_t *ike_sa_id, entry_t **entry, u_int *segment)
620 {
621 return get_entry_by_match_function(this, ike_sa_id, entry, segment,
622 (linked_list_match_t)entry_match_by_id, ike_sa_id, NULL);
623 }
624
625 /**
626 * Find an entry by initiator SPI and IKE_SA_INIT hash.
627 * Note: On SUCCESS, the caller has to unlock the segment.
628 */
629 static status_t get_entry_by_hash(private_ike_sa_manager_t *this,
630 ike_sa_id_t *ike_sa_id, chunk_t hash, entry_t **entry, u_int *segment)
631 {
632 return get_entry_by_match_function(this, ike_sa_id, entry, segment,
633 (linked_list_match_t)entry_match_by_hash, ike_sa_id, &hash);
634 }
635
636 /**
637 * Find an entry by IKE_SA pointer.
638 * Note: On SUCCESS, the caller has to unlock the segment.
639 */
640 static status_t get_entry_by_sa(private_ike_sa_manager_t *this,
641 ike_sa_id_t *ike_sa_id, ike_sa_t *ike_sa, entry_t **entry, u_int *segment)
642 {
643 return get_entry_by_match_function(this, ike_sa_id, entry, segment,
644 (linked_list_match_t)entry_match_by_sa, ike_sa, NULL);
645 }
646
647 /**
648 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
649 * acquirable.
650 */
651 static bool wait_for_entry(private_ike_sa_manager_t *this, entry_t *entry,
652 u_int segment)
653 {
654 if (entry->driveout_new_threads)
655 {
656 /* we are not allowed to get this */
657 return FALSE;
658 }
659 while (entry->checked_out && !entry->driveout_waiting_threads)
660 {
661 /* so wait until we can get it for us.
662 * we register us as waiting. */
663 entry->waiting_threads++;
664 entry->condvar->wait(entry->condvar, this->segments[segment].mutex);
665 entry->waiting_threads--;
666 }
667 /* hm, a deletion request forbids us to get this SA, get next one */
668 if (entry->driveout_waiting_threads)
669 {
670 /* we must signal here, others may be waiting on it, too */
671 entry->condvar->signal(entry->condvar);
672 return FALSE;
673 }
674 return TRUE;
675 }
676
677 /**
678 * Put a half-open SA into the hash table.
679 */
680 static void put_half_open(private_ike_sa_manager_t *this, entry_t *entry)
681 {
682 half_open_t *half_open = NULL;
683 linked_list_t *list;
684 chunk_t addr = entry->other->get_address(entry->other);
685 u_int row = chunk_hash(addr) & this->table_mask;
686 u_int segment = row & this->segment_mask;
687
688 rwlock_t *lock = this->half_open_segments[segment].lock;
689 lock->write_lock(lock);
690 if ((list = this->half_open_table[row]) == NULL)
691 {
692 list = this->half_open_table[row] = linked_list_create();
693 }
694 else
695 {
696 half_open_t *current;
697 if (list->find_first(list, (linked_list_match_t)half_open_match,
698 (void**)&current, &addr) == SUCCESS)
699 {
700 half_open = current;
701 half_open->count++;
702 this->half_open_segments[segment].count++;
703 }
704 }
705
706 if (!half_open)
707 {
708 half_open = malloc_thing(half_open_t);
709 half_open->other = chunk_clone(addr);
710 half_open->count = 1;
711 list->insert_last(list, half_open);
712 this->half_open_segments[segment].count++;
713 }
714 lock->unlock(lock);
715 }
716
717 /**
718 * Remove a half-open SA from the hash table.
719 */
720 static void remove_half_open(private_ike_sa_manager_t *this, entry_t *entry)
721 {
722 linked_list_t *list;
723 chunk_t addr = entry->other->get_address(entry->other);
724 u_int row = chunk_hash(addr) & this->table_mask;
725 u_int segment = row & this->segment_mask;
726
727 rwlock_t *lock = this->half_open_segments[segment].lock;
728 lock->write_lock(lock);
729 if ((list = this->half_open_table[row]) != NULL)
730 {
731 half_open_t *current;
732 enumerator_t *enumerator = list->create_enumerator(list);
733 while (enumerator->enumerate(enumerator, &current))
734 {
735 if (half_open_match(current, &addr))
736 {
737 if (--current->count == 0)
738 {
739 list->remove_at(list, enumerator);
740 half_open_destroy(current);
741 }
742 this->half_open_segments[segment].count--;
743 break;
744 }
745 }
746 enumerator->destroy(enumerator);
747 }
748 lock->unlock(lock);
749 }
750
751 /**
752 * Put an SA between two peers into the hash table.
753 */
754 static void put_connected_peers(private_ike_sa_manager_t *this, entry_t *entry)
755 {
756 linked_list_t *list;
757 connected_peers_t *connected_peers = NULL;
758 chunk_t my_id = entry->my_id->get_encoding(entry->my_id),
759 other_id = entry->other_id->get_encoding(entry->other_id);
760 u_int row = chunk_hash_inc(other_id, chunk_hash(my_id)) & this->table_mask;
761 u_int segment = row & this->segment_mask;
762
763 rwlock_t *lock = this->connected_peers_segments[segment].lock;
764 lock->write_lock(lock);
765 if ((list = this->connected_peers_table[row]) == NULL)
766 {
767 list = this->connected_peers_table[row] = linked_list_create();
768 }
769 else
770 {
771 connected_peers_t *current;
772 if (list->find_first(list, (linked_list_match_t)connected_peers_match,
773 (void**)&current, entry->my_id, entry->other_id) == SUCCESS)
774 {
775 connected_peers = current;
776 if (connected_peers->sas->find_first(connected_peers->sas,
777 (linked_list_match_t)entry->ike_sa_id->equals,
778 NULL, entry->ike_sa_id) == SUCCESS)
779 {
780 lock->unlock(lock);
781 return;
782 }
783 }
784 }
785
786 if (!connected_peers)
787 {
788 connected_peers = malloc_thing(connected_peers_t);
789 connected_peers->my_id = entry->my_id->clone(entry->my_id);
790 connected_peers->other_id = entry->other_id->clone(entry->other_id);
791 connected_peers->sas = linked_list_create();
792 list->insert_last(list, connected_peers);
793 }
794 connected_peers->sas->insert_last(connected_peers->sas,
795 entry->ike_sa_id->clone(entry->ike_sa_id));
796 this->connected_peers_segments[segment].count++;
797 lock->unlock(lock);
798 }
799
800 /**
801 * Remove an SA between two peers from the hash table.
802 */
803 static void remove_connected_peers(private_ike_sa_manager_t *this, entry_t *entry)
804 {
805 linked_list_t *list;
806 chunk_t my_id = entry->my_id->get_encoding(entry->my_id),
807 other_id = entry->other_id->get_encoding(entry->other_id);
808 u_int row = chunk_hash_inc(other_id, chunk_hash(my_id)) & this->table_mask;
809 u_int segment = row & this->segment_mask;
810
811 rwlock_t *lock = this->connected_peers_segments[segment].lock;
812 lock->write_lock(lock);
813 if ((list = this->connected_peers_table[row]) != NULL)
814 {
815 connected_peers_t *current;
816 enumerator_t *enumerator = list->create_enumerator(list);
817 while (enumerator->enumerate(enumerator, &current))
818 {
819 if (connected_peers_match(current, entry->my_id, entry->other_id))
820 {
821 ike_sa_id_t *ike_sa_id;
822 enumerator_t *inner = current->sas->create_enumerator(current->sas);
823 while (inner->enumerate(inner, &ike_sa_id))
824 {
825 if (ike_sa_id->equals(ike_sa_id, entry->ike_sa_id))
826 {
827 current->sas->remove_at(current->sas, inner);
828 ike_sa_id->destroy(ike_sa_id);
829 this->connected_peers_segments[segment].count--;
830 break;
831 }
832 }
833 inner->destroy(inner);
834 if (current->sas->get_count(current->sas) == 0)
835 {
836 list->remove_at(list, enumerator);
837 connected_peers_destroy(current);
838 }
839 break;
840 }
841 }
842 enumerator->destroy(enumerator);
843 }
844 lock->unlock(lock);
845 }
846
847 /**
848 * Implementation of private_ike_sa_manager_t.get_next_spi.
849 */
850 static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
851 {
852 u_int64_t spi;
853
854 this->rng->get_bytes(this->rng, sizeof(spi), (u_int8_t*)&spi);
855 return spi;
856 }
857
858 /**
859 * Implementation of of ike_sa_manager.checkout.
860 */
861 static ike_sa_t* checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
862 {
863 ike_sa_t *ike_sa = NULL;
864 entry_t *entry;
865 u_int segment;
866
867 DBG2(DBG_MGR, "checkout IKE_SA");
868
869 if (get_entry_by_id(this, ike_sa_id, &entry, &segment) == SUCCESS)
870 {
871 if (wait_for_entry(this, entry, segment))
872 {
873 DBG2(DBG_MGR, "IKE_SA successfully checked out");
874 entry->checked_out = TRUE;
875 ike_sa = entry->ike_sa;
876 }
877 unlock_single_segment(this, segment);
878 }
879 charon->bus->set_sa(charon->bus, ike_sa);
880 return ike_sa;
881 }
882
883 /**
884 * Implementation of of ike_sa_manager.checkout_new.
885 */
886 static ike_sa_t *checkout_new(private_ike_sa_manager_t* this, bool initiator)
887 {
888 entry_t *entry;
889 u_int segment;
890
891 entry = entry_create();
892 if (initiator)
893 {
894 entry->ike_sa_id = ike_sa_id_create(get_next_spi(this), 0, TRUE);
895 }
896 else
897 {
898 entry->ike_sa_id = ike_sa_id_create(0, get_next_spi(this), FALSE);
899 }
900 entry->ike_sa = ike_sa_create(entry->ike_sa_id);
901
902 segment = put_entry(this, entry);
903 entry->checked_out = TRUE;
904 unlock_single_segment(this, segment);
905
906 DBG2(DBG_MGR, "created IKE_SA");
907 return entry->ike_sa;
908 }
909
910 /**
911 * Implementation of of ike_sa_manager.checkout_by_message.
912 */
913 static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
914 message_t *message)
915 {
916 u_int segment;
917 entry_t *entry;
918 ike_sa_t *ike_sa = NULL;
919 ike_sa_id_t *id = message->get_ike_sa_id(message);
920
921 id = id->clone(id);
922 id->switch_initiator(id);
923
924 DBG2(DBG_MGR, "checkout IKE_SA by message");
925
926 if (message->get_request(message) &&
927 message->get_exchange_type(message) == IKE_SA_INIT)
928 {
929 /* IKE_SA_INIT request. Check for an IKE_SA with such a message hash. */
930 chunk_t data, hash;
931
932 data = message->get_packet_data(message);
933 this->hasher->allocate_hash(this->hasher, data, &hash);
934 chunk_free(&data);
935
936 if (get_entry_by_hash(this, id, hash, &entry, &segment) == SUCCESS)
937 {
938 if (entry->message_id == 0)
939 {
940 unlock_single_segment(this, segment);
941 chunk_free(&hash);
942 id->destroy(id);
943 DBG1(DBG_MGR, "ignoring IKE_SA_INIT, already processing");
944 return NULL;
945 }
946 else if (wait_for_entry(this, entry, segment))
947 {
948 DBG2(DBG_MGR, "IKE_SA checked out by hash");
949 entry->checked_out = TRUE;
950 entry->message_id = message->get_message_id(message);
951 ike_sa = entry->ike_sa;
952 }
953 unlock_single_segment(this, segment);
954 }
955
956 if (ike_sa == NULL)
957 {
958 if (id->get_responder_spi(id) == 0 &&
959 message->get_exchange_type(message) == IKE_SA_INIT)
960 {
961 /* no IKE_SA found, create a new one */
962 id->set_responder_spi(id, get_next_spi(this));
963 entry = entry_create();
964 entry->ike_sa = ike_sa_create(id);
965 entry->ike_sa_id = id->clone(id);
966
967 segment = put_entry(this, entry);
968 entry->checked_out = TRUE;
969 unlock_single_segment(this, segment);
970
971 entry->message_id = message->get_message_id(message);
972 entry->init_hash = hash;
973 ike_sa = entry->ike_sa;
974
975 DBG2(DBG_MGR, "created IKE_SA");
976 }
977 else
978 {
979 chunk_free(&hash);
980 DBG1(DBG_MGR, "ignoring message, no such IKE_SA");
981 }
982 }
983 else
984 {
985 chunk_free(&hash);
986 }
987 id->destroy(id);
988 charon->bus->set_sa(charon->bus, ike_sa);
989 return ike_sa;
990 }
991
992 if (get_entry_by_id(this, id, &entry, &segment) == SUCCESS)
993 {
994 /* only check out if we are not processing this request */
995 if (message->get_request(message) &&
996 message->get_message_id(message) == entry->message_id)
997 {
998 DBG1(DBG_MGR, "ignoring request with ID %d, already processing",
999 entry->message_id);
1000 }
1001 else if (wait_for_entry(this, entry, segment))
1002 {
1003 ike_sa_id_t *ike_id = entry->ike_sa->get_id(entry->ike_sa);
1004 DBG2(DBG_MGR, "IKE_SA successfully checked out");
1005 entry->checked_out = TRUE;
1006 entry->message_id = message->get_message_id(message);
1007 if (ike_id->get_responder_spi(ike_id) == 0)
1008 {
1009 ike_id->set_responder_spi(ike_id, id->get_responder_spi(id));
1010 }
1011 ike_sa = entry->ike_sa;
1012 }
1013 unlock_single_segment(this, segment);
1014 }
1015 id->destroy(id);
1016 charon->bus->set_sa(charon->bus, ike_sa);
1017 return ike_sa;
1018 }
1019
1020 /**
1021 * Implementation of of ike_sa_manager.checkout_by_config.
1022 */
1023 static ike_sa_t* checkout_by_config(private_ike_sa_manager_t *this,
1024 peer_cfg_t *peer_cfg)
1025 {
1026 enumerator_t *enumerator;
1027 entry_t *entry;
1028 ike_sa_t *ike_sa = NULL;
1029 identification_t *my_id, *other_id;
1030 host_t *my_host, *other_host;
1031 ike_cfg_t *ike_cfg;
1032 u_int segment;
1033
1034 if (!this->reuse_ikesa)
1035 { /* IKE_SA reuse disable by config */
1036 ike_sa = checkout_new(this, TRUE);
1037 charon->bus->set_sa(charon->bus, ike_sa);
1038 return ike_sa;
1039 }
1040
1041 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
1042 my_id = peer_cfg->get_my_id(peer_cfg);
1043 other_id = peer_cfg->get_other_id(peer_cfg);
1044 my_host = host_create_from_dns(ike_cfg->get_my_addr(ike_cfg), 0, 0);
1045 other_host = host_create_from_dns(ike_cfg->get_other_addr(ike_cfg), 0, 0);
1046
1047 if (my_host && other_host)
1048 {
1049 enumerator = create_table_enumerator(this);
1050 while (enumerator->enumerate(enumerator, &entry, &segment))
1051 {
1052 identification_t *found_my_id, *found_other_id;
1053 host_t *found_my_host, *found_other_host;
1054
1055 if (!wait_for_entry(this, entry, segment))
1056 {
1057 continue;
1058 }
1059
1060 if (entry->ike_sa->get_state(entry->ike_sa) == IKE_DELETING)
1061 {
1062 /* skip IKE_SAs which are not usable */
1063 continue;
1064 }
1065
1066 found_my_id = entry->ike_sa->get_my_id(entry->ike_sa);
1067 found_other_id = entry->ike_sa->get_other_id(entry->ike_sa);
1068 found_my_host = entry->ike_sa->get_my_host(entry->ike_sa);
1069 found_other_host = entry->ike_sa->get_other_host(entry->ike_sa);
1070
1071 if (found_my_id->get_type(found_my_id) == ID_ANY &&
1072 found_other_id->get_type(found_other_id) == ID_ANY)
1073 {
1074 /* IKE_SA has no IDs yet, so we can't use it */
1075 continue;
1076 }
1077 DBG2(DBG_MGR, "candidate IKE_SA for \n"
1078 " %H[%D]...%H[%D]\n"
1079 " %H[%D]...%H[%D]",
1080 my_host, my_id, other_host, other_id,
1081 found_my_host, found_my_id, found_other_host, found_other_id);
1082 /* compare ID and hosts. Supplied ID may contain wildcards, and IP
1083 * may be %any. */
1084 if ((my_host->is_anyaddr(my_host) ||
1085 my_host->ip_equals(my_host, found_my_host)) &&
1086 (other_host->is_anyaddr(other_host) ||
1087 other_host->ip_equals(other_host, found_other_host)) &&
1088 found_my_id->matches(found_my_id, my_id) &&
1089 found_other_id->matches(found_other_id, other_id) &&
1090 streq(peer_cfg->get_name(peer_cfg),
1091 entry->ike_sa->get_name(entry->ike_sa)))
1092 {
1093 /* looks good, we take this one */
1094 DBG2(DBG_MGR, "found an existing IKE_SA for %H[%D]...%H[%D]",
1095 my_host, my_id, other_host, other_id);
1096 entry->checked_out = TRUE;
1097 ike_sa = entry->ike_sa;
1098 break;
1099 }
1100 }
1101 enumerator->destroy(enumerator);
1102 }
1103 DESTROY_IF(my_host);
1104 DESTROY_IF(other_host);
1105
1106 if (!ike_sa)
1107 { /* no IKE_SA using such a config, hand out a new */
1108 ike_sa = checkout_new(this, TRUE);
1109 }
1110 charon->bus->set_sa(charon->bus, ike_sa);
1111 return ike_sa;
1112 }
1113
1114 /**
1115 * Implementation of of ike_sa_manager.checkout_by_id.
1116 */
1117 static ike_sa_t* checkout_by_id(private_ike_sa_manager_t *this, u_int32_t id,
1118 bool child)
1119 {
1120 enumerator_t *enumerator;
1121 iterator_t *children;
1122 entry_t *entry;
1123 ike_sa_t *ike_sa = NULL;
1124 child_sa_t *child_sa;
1125 u_int segment;
1126
1127 enumerator = create_table_enumerator(this);
1128 while (enumerator->enumerate(enumerator, &entry, &segment))
1129 {
1130 if (wait_for_entry(this, entry, segment))
1131 {
1132 /* look for a child with such a reqid ... */
1133 if (child)
1134 {
1135 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
1136 while (children->iterate(children, (void**)&child_sa))
1137 {
1138 if (child_sa->get_reqid(child_sa) == id)
1139 {
1140 ike_sa = entry->ike_sa;
1141 break;
1142 }
1143 }
1144 children->destroy(children);
1145 }
1146 else /* ... or for a IKE_SA with such a unique id */
1147 {
1148 if (entry->ike_sa->get_unique_id(entry->ike_sa) == id)
1149 {
1150 ike_sa = entry->ike_sa;
1151 }
1152 }
1153 /* got one, return */
1154 if (ike_sa)
1155 {
1156 entry->checked_out = TRUE;
1157 break;
1158 }
1159 }
1160 }
1161 enumerator->destroy(enumerator);
1162
1163 charon->bus->set_sa(charon->bus, ike_sa);
1164 return ike_sa;
1165 }
1166
1167 /**
1168 * Implementation of of ike_sa_manager.checkout_by_name.
1169 */
1170 static ike_sa_t* checkout_by_name(private_ike_sa_manager_t *this, char *name,
1171 bool child)
1172 {
1173 enumerator_t *enumerator;
1174 iterator_t *children;
1175 entry_t *entry;
1176 ike_sa_t *ike_sa = NULL;
1177 child_sa_t *child_sa;
1178 u_int segment;
1179
1180 enumerator = create_table_enumerator(this);
1181 while (enumerator->enumerate(enumerator, &entry, &segment))
1182 {
1183 if (wait_for_entry(this, entry, segment))
1184 {
1185 /* look for a child with such a policy name ... */
1186 if (child)
1187 {
1188 children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
1189 while (children->iterate(children, (void**)&child_sa))
1190 {
1191 if (streq(child_sa->get_name(child_sa), name))
1192 {
1193 ike_sa = entry->ike_sa;
1194 break;
1195 }
1196 }
1197 children->destroy(children);
1198 }
1199 else /* ... or for a IKE_SA with such a connection name */
1200 {
1201 if (streq(entry->ike_sa->get_name(entry->ike_sa), name))
1202 {
1203 ike_sa = entry->ike_sa;
1204 }
1205 }
1206 /* got one, return */
1207 if (ike_sa)
1208 {
1209 entry->checked_out = TRUE;
1210 break;
1211 }
1212 }
1213 }
1214 enumerator->destroy(enumerator);
1215
1216 charon->bus->set_sa(charon->bus, ike_sa);
1217 return ike_sa;
1218 }
1219
1220 /**
1221 * Implementation of ike_sa_manager_t.checkout_duplicate.
1222 */
1223 static ike_sa_t* checkout_duplicate(private_ike_sa_manager_t *this,
1224 ike_sa_t *ike_sa)
1225 {
1226 linked_list_t *list;
1227 entry_t *entry;
1228 ike_sa_id_t *duplicate_id = NULL;
1229 ike_sa_t *duplicate = NULL;
1230 identification_t *me, *other;
1231 u_int row, segment;
1232 rwlock_t *lock;
1233
1234 me = ike_sa->get_my_id(ike_sa);
1235 other = ike_sa->get_other_id(ike_sa);
1236
1237 row = chunk_hash_inc(other->get_encoding(other),
1238 chunk_hash(me->get_encoding(me))) & this->table_mask;
1239 segment = row & this->segment_mask;
1240
1241 lock = this->connected_peers_segments[segment & this->segment_mask].lock;
1242 lock->read_lock(lock);
1243 if ((list = this->connected_peers_table[row]) != NULL)
1244 {
1245 connected_peers_t *current;
1246 if (list->find_first(list, (linked_list_match_t)connected_peers_match,
1247 (void**)&current, me, other) == SUCCESS)
1248 {
1249 /* we just return the first ike_sa_id we have cached for these ids */
1250 current->sas->get_first(current->sas, (void**)&duplicate_id);
1251 }
1252 }
1253 lock->unlock(lock);
1254
1255 if (duplicate_id)
1256 {
1257 if (get_entry_by_id(this, duplicate_id, &entry, &segment) == SUCCESS)
1258 {
1259 if (wait_for_entry(this, entry, segment))
1260 {
1261 duplicate = entry->ike_sa;
1262 entry->checked_out = TRUE;
1263 }
1264 unlock_single_segment(this, segment);
1265 }
1266 }
1267 return duplicate;
1268 }
1269
1270 /**
1271 * enumerator filter function
1272 */
1273 static bool enumerator_filter(private_ike_sa_manager_t *this,
1274 entry_t **in, ike_sa_t **out, u_int *segment)
1275 {
1276 if (wait_for_entry(this, *in, *segment))
1277 {
1278 *out = (*in)->ike_sa;
1279 return TRUE;
1280 }
1281 return FALSE;
1282 }
1283
1284 /**
1285 * Implementation of ike_sa_manager_t.create_enumerator.
1286 */
1287 static enumerator_t *create_enumerator(private_ike_sa_manager_t* this)
1288 {
1289 return enumerator_create_filter(
1290 create_table_enumerator(this),
1291 (void*)enumerator_filter, this, NULL);
1292 }
1293
1294 /**
1295 * Implementation of ike_sa_manager_t.checkin.
1296 */
1297 static void checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
1298 {
1299 /* to check the SA back in, we look for the pointer of the ike_sa
1300 * in all entries.
1301 * The lookup is done by initiator SPI, so even if the SPI has changed (e.g.
1302 * on reception of a IKE_SA_INIT response) the lookup will work but
1303 * updating of the SPI MAY be necessary...
1304 */
1305 entry_t *entry;
1306 ike_sa_id_t *ike_sa_id;
1307 host_t *other;
1308 identification_t *my_id, *other_id;
1309 u_int segment;
1310
1311 ike_sa_id = ike_sa->get_id(ike_sa);
1312 my_id = ike_sa->get_my_id(ike_sa);
1313 other_id = ike_sa->get_other_id(ike_sa);
1314 other = ike_sa->get_other_host(ike_sa);
1315
1316 DBG2(DBG_MGR, "checkin IKE_SA");
1317
1318 /* look for the entry */
1319 if (get_entry_by_sa(this, ike_sa_id, ike_sa, &entry, &segment) == SUCCESS)
1320 {
1321 /* ike_sa_id must be updated */
1322 entry->ike_sa_id->replace_values(entry->ike_sa_id, ike_sa->get_id(ike_sa));
1323 /* signal waiting threads */
1324 entry->checked_out = FALSE;
1325 entry->message_id = -1;
1326 /* check if this SA is half-open */
1327 if (entry->half_open && ike_sa->get_state(ike_sa) != IKE_CONNECTING)
1328 {
1329 /* not half open anymore */
1330 entry->half_open = FALSE;
1331 remove_half_open(this, entry);
1332 }
1333 else if (entry->half_open && !other->ip_equals(other, entry->other))
1334 {
1335 /* the other host's IP has changed, we must update the hash table */
1336 remove_half_open(this, entry);
1337 DESTROY_IF(entry->other);
1338 entry->other = other->clone(other);
1339 put_half_open(this, entry);
1340 }
1341 else if (!entry->half_open &&
1342 !entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
1343 ike_sa->get_state(ike_sa) == IKE_CONNECTING)
1344 {
1345 /* this is a new half-open SA */
1346 entry->half_open = TRUE;
1347 entry->other = other->clone(other);
1348 put_half_open(this, entry);
1349 }
1350 DBG2(DBG_MGR, "check-in of IKE_SA successful.");
1351 entry->condvar->signal(entry->condvar);
1352 }
1353 else
1354 {
1355 entry = entry_create();
1356 entry->ike_sa_id = ike_sa_id->clone(ike_sa_id);
1357 entry->ike_sa = ike_sa;
1358 segment = put_entry(this, entry);
1359 }
1360
1361 /* apply identities for duplicate test (only as responder) */
1362 if (!entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
1363 (!entry->my_id || !entry->other_id))
1364 {
1365 if (!entry->my_id && my_id->get_type(my_id) != ID_ANY)
1366 {
1367 entry->my_id = my_id->clone(my_id);
1368 }
1369 if (!entry->other_id && other_id->get_type(other_id) != ID_ANY)
1370 {
1371 entry->other_id = other_id->clone(other_id);
1372 }
1373 if (entry->my_id && entry->other_id)
1374 {
1375 put_connected_peers(this, entry);
1376 }
1377 }
1378
1379 unlock_single_segment(this, segment);
1380
1381 charon->bus->set_sa(charon->bus, NULL);
1382 }
1383
1384 /**
1385 * Implementation of ike_sa_manager_t.checkin_and_destroy.
1386 */
1387 static void checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
1388 {
1389 /* deletion is a bit complex, we must ensure that no thread is waiting for
1390 * this SA.
1391 * We take this SA from the table, and start signaling while threads
1392 * are in the condvar.
1393 */
1394 entry_t *entry;
1395 ike_sa_id_t *ike_sa_id;
1396 u_int segment;
1397
1398 ike_sa_id = ike_sa->get_id(ike_sa);
1399
1400 DBG2(DBG_MGR, "checkin and destroy IKE_SA");
1401
1402 if (get_entry_by_sa(this, ike_sa_id, ike_sa, &entry, &segment) == SUCCESS)
1403 {
1404 /* drive out waiting threads, as we are in hurry */
1405 entry->driveout_waiting_threads = TRUE;
1406 /* mark it, so no new threads can get this entry */
1407 entry->driveout_new_threads = TRUE;
1408 /* wait until all workers have done their work */
1409 while (entry->waiting_threads)
1410 {
1411 /* wake up all */
1412 entry->condvar->broadcast(entry->condvar);
1413 /* they will wake us again when their work is done */
1414 entry->condvar->wait(entry->condvar, this->segments[segment].mutex);
1415 }
1416 remove_entry(this, entry);
1417 unlock_single_segment(this, segment);
1418
1419 if (entry->half_open)
1420 {
1421 remove_half_open(this, entry);
1422 }
1423 if (!entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
1424 entry->my_id && entry->other_id)
1425 {
1426 remove_connected_peers(this, entry);
1427 }
1428
1429 entry_destroy(entry);
1430
1431 DBG2(DBG_MGR, "check-in and destroy of IKE_SA successful");
1432 }
1433 else
1434 {
1435 DBG1(DBG_MGR, "tried to check-in and delete nonexisting IKE_SA");
1436 ike_sa->destroy(ike_sa);
1437 }
1438 charon->bus->set_sa(charon->bus, NULL);
1439 }
1440
1441 /**
1442 * Implementation of ike_sa_manager_t.get_half_open_count.
1443 */
1444 static int get_half_open_count(private_ike_sa_manager_t *this, host_t *ip)
1445 {
1446 int count = 0;
1447
1448 if (ip)
1449 {
1450 linked_list_t *list;
1451 chunk_t addr = ip->get_address(ip);
1452 u_int row = chunk_hash(addr) & this->table_mask;
1453 u_int segment = row & this->segment_mask;
1454
1455 rwlock_t *lock = this->half_open_segments[segment & this->segment_mask].lock;
1456 lock->read_lock(lock);
1457 if ((list = this->half_open_table[row]) != NULL)
1458 {
1459 half_open_t *current;
1460
1461 if (list->find_first(list, (linked_list_match_t)half_open_match,
1462 (void**)&current, &addr) == SUCCESS)
1463 {
1464 count = current->count;
1465 }
1466 }
1467 lock->unlock(lock);
1468 }
1469 else
1470 {
1471 u_int segment;
1472
1473 for (segment = 0; segment < this->segment_count; ++segment)
1474 {
1475 rwlock_t *lock;
1476 lock = this->half_open_segments[segment & this->segment_mask].lock;
1477 lock->read_lock(lock);
1478 count += this->half_open_segments[segment].count;
1479 lock->unlock(lock);
1480 }
1481 }
1482
1483 return count;
1484 }
1485
1486 /**
1487 * Implementation of ike_sa_manager_t.flush.
1488 */
1489 static void flush(private_ike_sa_manager_t *this)
1490 {
1491 /* destroy all list entries */
1492 enumerator_t *enumerator;
1493 entry_t *entry;
1494 u_int segment;
1495
1496 lock_all_segments(this);
1497 DBG2(DBG_MGR, "going to destroy IKE_SA manager and all managed IKE_SA's");
1498 /* Step 1: drive out all waiting threads */
1499 DBG2(DBG_MGR, "set driveout flags for all stored IKE_SA's");
1500 enumerator = create_table_enumerator(this);
1501 while (enumerator->enumerate(enumerator, &entry, &segment))
1502 {
1503 /* do not accept new threads, drive out waiting threads */
1504 entry->driveout_new_threads = TRUE;
1505 entry->driveout_waiting_threads = TRUE;
1506 }
1507 enumerator->destroy(enumerator);
1508 DBG2(DBG_MGR, "wait for all threads to leave IKE_SA's");
1509 /* Step 2: wait until all are gone */
1510 enumerator = create_table_enumerator(this);
1511 while (enumerator->enumerate(enumerator, &entry, &segment))
1512 {
1513 while (entry->waiting_threads || entry->checked_out)
1514 {
1515 /* wake up all */
1516 entry->condvar->broadcast(entry->condvar);
1517 /* go sleeping until they are gone */
1518 entry->condvar->wait(entry->condvar, this->segments[segment].mutex);
1519 }
1520 }
1521 enumerator->destroy(enumerator);
1522 DBG2(DBG_MGR, "delete all IKE_SA's");
1523 /* Step 3: initiate deletion of all IKE_SAs */
1524 enumerator = create_table_enumerator(this);
1525 while (enumerator->enumerate(enumerator, &entry, &segment))
1526 {
1527 charon->bus->set_sa(charon->bus, entry->ike_sa);
1528 entry->ike_sa->delete(entry->ike_sa);
1529 }
1530 enumerator->destroy(enumerator);
1531
1532 DBG2(DBG_MGR, "destroy all entries");
1533 /* Step 4: destroy all entries */
1534 enumerator = create_table_enumerator(this);
1535 while (enumerator->enumerate(enumerator, &entry, &segment))
1536 {
1537 charon->bus->set_sa(charon->bus, entry->ike_sa);
1538 if (entry->half_open)
1539 {
1540 remove_half_open(this, entry);
1541 }
1542 if (!entry->ike_sa_id->is_initiator(entry->ike_sa_id) &&
1543 entry->my_id && entry->other_id)
1544 {
1545 remove_connected_peers(this, entry);
1546 }
1547 remove_entry_at((private_enumerator_t*)enumerator);
1548 entry_destroy(entry);
1549 }
1550 enumerator->destroy(enumerator);
1551 charon->bus->set_sa(charon->bus, NULL);
1552 unlock_all_segments(this);
1553 }
1554
1555 /**
1556 * Implementation of ike_sa_manager_t.destroy.
1557 */
1558 static void destroy(private_ike_sa_manager_t *this)
1559 {
1560 u_int i;
1561
1562 for (i = 0; i < this->table_size; ++i)
1563 {
1564 linked_list_t *list;
1565
1566 if ((list = this->ike_sa_table[i]) != NULL)
1567 {
1568 list->destroy(list);
1569 }
1570 if ((list = this->half_open_table[i]) != NULL)
1571 {
1572 list->destroy(list);
1573 }
1574 if ((list = this->connected_peers_table[i]) != NULL)
1575 {
1576 list->destroy(list);
1577 }
1578 }
1579 free(this->ike_sa_table);
1580 free(this->half_open_table);
1581 free(this->connected_peers_table);
1582 for (i = 0; i < this->segment_count; ++i)
1583 {
1584 this->segments[i].mutex->destroy(this->segments[i].mutex);
1585 this->half_open_segments[i].lock->destroy(this->half_open_segments[i].lock);
1586 this->connected_peers_segments[i].lock->destroy(this->connected_peers_segments[i].lock);
1587 }
1588 free(this->segments);
1589 free(this->half_open_segments);
1590 free(this->connected_peers_segments);
1591
1592 this->rng->destroy(this->rng);
1593 this->hasher->destroy(this->hasher);
1594 free(this);
1595 }
1596
1597 /**
1598 * This function returns the next-highest power of two for the given number.
1599 * The algorithm works by setting all bits on the right-hand side of the most
1600 * significant 1 to 1 and then increments the whole number so it rolls over
1601 * to the nearest power of two. Note: returns 0 for n == 0
1602 */
1603 static u_int get_nearest_powerof2(u_int n)
1604 {
1605 u_int i;
1606
1607 --n;
1608 for (i = 1; i < sizeof(u_int) * 8; i <<= 1)
1609 {
1610 n |= n >> i;
1611 }
1612 return ++n;
1613 }
1614
1615 /*
1616 * Described in header.
1617 */
1618 ike_sa_manager_t *ike_sa_manager_create()
1619 {
1620 u_int i;
1621 private_ike_sa_manager_t *this = malloc_thing(private_ike_sa_manager_t);
1622
1623 /* assign public functions */
1624 this->public.flush = (void(*)(ike_sa_manager_t*))flush;
1625 this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
1626 this->public.checkout = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_id_t*))checkout;
1627 this->public.checkout_new = (ike_sa_t*(*)(ike_sa_manager_t*,bool))checkout_new;
1628 this->public.checkout_by_message = (ike_sa_t*(*)(ike_sa_manager_t*,message_t*))checkout_by_message;
1629 this->public.checkout_by_config = (ike_sa_t*(*)(ike_sa_manager_t*,peer_cfg_t*))checkout_by_config;
1630 this->public.checkout_by_id = (ike_sa_t*(*)(ike_sa_manager_t*,u_int32_t,bool))checkout_by_id;
1631 this->public.checkout_by_name = (ike_sa_t*(*)(ike_sa_manager_t*,char*,bool))checkout_by_name;
1632 this->public.checkout_duplicate = (ike_sa_t*(*)(ike_sa_manager_t*, ike_sa_t *ike_sa))checkout_duplicate;
1633 this->public.create_enumerator = (enumerator_t*(*)(ike_sa_manager_t*))create_enumerator;
1634 this->public.checkin = (void(*)(ike_sa_manager_t*,ike_sa_t*))checkin;
1635 this->public.checkin_and_destroy = (void(*)(ike_sa_manager_t*,ike_sa_t*))checkin_and_destroy;
1636 this->public.get_half_open_count = (int(*)(ike_sa_manager_t*,host_t*))get_half_open_count;
1637
1638 /* initialize private variables */
1639 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_PREFERRED);
1640 if (this->hasher == NULL)
1641 {
1642 DBG1(DBG_MGR, "manager initialization failed, no hasher supported");
1643 free(this);
1644 return NULL;
1645 }
1646 this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
1647 if (this->rng == NULL)
1648 {
1649 DBG1(DBG_MGR, "manager initialization failed, no RNG supported");
1650 this->hasher->destroy(this->hasher);
1651 free(this);
1652 return NULL;
1653 }
1654 this->table_size = get_nearest_powerof2(lib->settings->get_int(lib->settings,
1655 "charon.ikesa_table_size",
1656 DEFAULT_HASHTABLE_SIZE));
1657 this->table_size = max(1, min(this->table_size, MAX_HASHTABLE_SIZE));
1658 this->table_mask = this->table_size - 1;
1659
1660 this->segment_count = get_nearest_powerof2(lib->settings->get_int(lib->settings,
1661 "charon.ikesa_table_segments",
1662 DEFAULT_SEGMENT_COUNT));
1663 this->segment_count = max(1, min(this->segment_count, this->table_size));
1664 this->segment_mask = this->segment_count - 1;
1665
1666 this->ike_sa_table = (linked_list_t**)calloc(this->table_size, sizeof(linked_list_t*));
1667 memset(this->ike_sa_table, 0, this->table_size * sizeof(linked_list_t*));
1668
1669 this->segments = (segment_t*)calloc(this->segment_count, sizeof(segment_t));
1670 for (i = 0; i < this->segment_count; ++i)
1671 {
1672 this->segments[i].mutex = mutex_create(MUTEX_RECURSIVE);
1673 this->segments[i].count = 0;
1674 }
1675
1676 /* we use the same parameters as above for the hash table to track half-open SAs */
1677 this->half_open_table = (linked_list_t**)calloc(this->table_size, sizeof(linked_list_t*));
1678 memset(this->half_open_table, 0, this->table_size * sizeof(linked_list_t*));
1679
1680 this->half_open_segments = (shareable_segment_t*)calloc(this->segment_count, sizeof(shareable_segment_t));
1681 for (i = 0; i < this->segment_count; ++i)
1682 {
1683 this->half_open_segments[i].lock = rwlock_create(RWLOCK_DEFAULT);
1684 this->half_open_segments[i].count = 0;
1685 }
1686
1687 /* also for the hash table used for duplicate tests */
1688 this->connected_peers_table = (linked_list_t**)calloc(this->table_size, sizeof(linked_list_t*));
1689 memset(this->connected_peers_table, 0, this->table_size * sizeof(linked_list_t*));
1690
1691 this->connected_peers_segments = (shareable_segment_t*)calloc(this->segment_count, sizeof(shareable_segment_t));
1692 for (i = 0; i < this->segment_count; ++i)
1693 {
1694 this->connected_peers_segments[i].lock = rwlock_create(RWLOCK_DEFAULT);
1695 this->connected_peers_segments[i].count = 0;
1696 }
1697
1698 this->reuse_ikesa = lib->settings->get_bool(lib->settings,
1699 "charon.reuse_ikesa", TRUE);
1700 return &this->public;
1701 }