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