2 * Copyright (C) 2005-2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 * Copyright (C) 2008 Tobias Brunner
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 #include "ike_sa_manager.h"
24 #include <sa/ike_sa_id.h>
26 #include <threading/condvar.h>
27 #include <threading/mutex.h>
28 #include <threading/rwlock.h>
29 #include <utils/linked_list.h>
30 #include <crypto/hashers/hasher.h>
32 /* the default size of the hash table (MUST be a power of 2) */
33 #define DEFAULT_HASHTABLE_SIZE 1
35 /* the maximum size of the hash table (MUST be a power of 2) */
36 #define MAX_HASHTABLE_SIZE (1 << 30)
38 /* the default number of segments (MUST be a power of 2) */
39 #define DEFAULT_SEGMENT_COUNT 1
41 typedef struct entry_t entry_t
;
44 * An entry in the linked list, contains IKE_SA, locking and lookup data.
49 * Number of threads waiting for this ike_sa_t object.
54 * Condvar where threads can wait until ike_sa_t object is free for use again.
59 * Is this ike_sa currently checked out?
64 * Does this SA drives out new threads?
66 bool driveout_new_threads
;
69 * Does this SA drives out waiting threads?
71 bool driveout_waiting_threads
;
74 * Identification of an IKE_SA (SPIs).
76 ike_sa_id_t
*ike_sa_id
;
79 * The contained ike_sa_t object.
84 * hash of the IKE_SA_INIT message, used to detect retransmissions
89 * remote host address, required for DoS detection and duplicate
90 * checking (host with same my_id and other_id is *not* considered
91 * a duplicate if the address family differs)
96 * As responder: Is this SA half-open?
101 * own identity, required for duplicate checking
103 identification_t
*my_id
;
106 * remote identity, required for duplicate checking
108 identification_t
*other_id
;
111 * message ID currently processing, if any
113 u_int32_t message_id
;
117 * Implementation of entry_t.destroy.
119 static status_t
entry_destroy(entry_t
*this)
121 /* also destroy IKE SA */
122 this->ike_sa
->destroy(this->ike_sa
);
123 this->ike_sa_id
->destroy(this->ike_sa_id
);
124 chunk_free(&this->init_hash
);
125 DESTROY_IF(this->other
);
126 DESTROY_IF(this->my_id
);
127 DESTROY_IF(this->other_id
);
128 this->condvar
->destroy(this->condvar
);
134 * Creates a new entry for the ike_sa_t list.
136 static entry_t
*entry_create()
138 entry_t
*this = malloc_thing(entry_t
);
140 this->waiting_threads
= 0;
141 this->condvar
= condvar_create(CONDVAR_TYPE_DEFAULT
);
143 /* we set checkout flag when we really give it out */
144 this->checked_out
= FALSE
;
145 this->driveout_new_threads
= FALSE
;
146 this->driveout_waiting_threads
= FALSE
;
147 this->message_id
= -1;
148 this->init_hash
= chunk_empty
;
150 this->half_open
= FALSE
;
152 this->other_id
= NULL
;
153 this->ike_sa_id
= NULL
;
160 * Function that matches entry_t objects by initiator SPI and the hash of the
161 * IKE_SA_INIT message.
163 static bool entry_match_by_hash(entry_t
*entry
, ike_sa_id_t
*id
, chunk_t
*hash
)
165 return id
->get_responder_spi(id
) == 0 &&
166 id
->is_initiator(id
) == entry
->ike_sa_id
->is_initiator(entry
->ike_sa_id
) &&
167 id
->get_initiator_spi(id
) == entry
->ike_sa_id
->get_initiator_spi(entry
->ike_sa_id
) &&
168 chunk_equals(*hash
, entry
->init_hash
);
172 * Function that matches entry_t objects by ike_sa_id_t.
174 static bool entry_match_by_id(entry_t
*entry
, ike_sa_id_t
*id
)
176 if (id
->equals(id
, entry
->ike_sa_id
))
180 if ((id
->get_responder_spi(id
) == 0 ||
181 entry
->ike_sa_id
->get_responder_spi(entry
->ike_sa_id
) == 0) &&
182 id
->is_initiator(id
) == entry
->ike_sa_id
->is_initiator(entry
->ike_sa_id
) &&
183 id
->get_initiator_spi(id
) == entry
->ike_sa_id
->get_initiator_spi(entry
->ike_sa_id
))
185 /* this is TRUE for IKE_SAs that we initiated but have not yet received a response */
192 * Function that matches entry_t objects by ike_sa_t pointers.
194 static bool entry_match_by_sa(entry_t
*entry
, ike_sa_t
*ike_sa
)
196 return entry
->ike_sa
== ike_sa
;
200 * Hash function for ike_sa_id_t objects.
202 static u_int
ike_sa_id_hash(ike_sa_id_t
*ike_sa_id
)
204 /* we always use initiator spi as key */
205 return ike_sa_id
->get_initiator_spi(ike_sa_id
);
208 typedef struct half_open_t half_open_t
;
211 * Struct to manage half-open IKE_SAs per peer.
214 /** chunk of remote host address */
217 /** the number of half-open IKE_SAs with that host */
222 * Destroys a half_open_t object.
224 static void half_open_destroy(half_open_t
*this)
226 chunk_free(&this->other
);
231 * Function that matches half_open_t objects by the given IP address chunk.
233 static bool half_open_match(half_open_t
*half_open
, chunk_t
*addr
)
235 return chunk_equals(*addr
, half_open
->other
);
238 typedef struct connected_peers_t connected_peers_t
;
240 struct connected_peers_t
{
242 identification_t
*my_id
;
244 /** remote identity */
245 identification_t
*other_id
;
247 /** ip address family of peer */
250 /** list of ike_sa_id_t objects of IKE_SAs between the two identities */
254 static void connected_peers_destroy(connected_peers_t
*this)
256 this->my_id
->destroy(this->my_id
);
257 this->other_id
->destroy(this->other_id
);
258 this->sas
->destroy(this->sas
);
263 * Function that matches connected_peers_t objects by the given ids.
265 static bool connected_peers_match(connected_peers_t
*connected_peers
,
266 identification_t
*my_id
, identification_t
*other_id
,
269 return my_id
->equals(my_id
, connected_peers
->my_id
) &&
270 other_id
->equals(other_id
, connected_peers
->other_id
) &&
271 family
== connected_peers
->family
;
274 typedef struct segment_t segment_t
;
277 * Struct to manage segments of the hash table.
280 /** mutex to access a segment exclusively */
283 /** the number of entries in this segment */
287 typedef struct shareable_segment_t shareable_segment_t
;
290 * Struct to manage segments of the "half-open" and "connected peers" hash tables.
292 struct shareable_segment_t
{
293 /** rwlock to access a segment non-/exclusively */
296 /** the number of entries in this segment - in case of the "half-open table"
297 * it's the sum of all half_open_t.count in a segment. */
301 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t
;
304 * Additional private members of ike_sa_manager_t.
306 struct private_ike_sa_manager_t
{
308 * Public interface of ike_sa_manager_t.
310 ike_sa_manager_t
public;
313 * Hash table with entries for the ike_sa_t objects.
315 linked_list_t
**ike_sa_table
;
318 * The size of the hash table.
323 * Mask to map the hashes to table rows.
328 * Segments of the hash table.
333 * The number of segments.
338 * Mask to map a table row to a segment.
343 * Hash table with half_open_t objects.
345 linked_list_t
**half_open_table
;
348 * Segments of the "half-open" hash table.
350 shareable_segment_t
*half_open_segments
;
353 * Hash table with connected_peers_t objects.
355 linked_list_t
**connected_peers_table
;
358 * Segments of the "connected peers" hash table.
360 shareable_segment_t
*connected_peers_segments
;
363 * RNG to get random SPIs for our side
368 * SHA1 hasher for IKE_SA_INIT retransmit detection
373 * reuse existing IKE_SAs in checkout_by_config
379 * Acquire a lock to access the segment of the table row with the given index.
380 * It also works with the segment index directly.
382 static void lock_single_segment(private_ike_sa_manager_t
*this, u_int index
)
384 mutex_t
*lock
= this->segments
[index
& this->segment_mask
].mutex
;
390 * Release the lock required to access the segment of the table row with the given index.
391 * It also works with the segment index directly.
393 static void unlock_single_segment(private_ike_sa_manager_t
*this, u_int index
)
395 mutex_t
*lock
= this->segments
[index
& this->segment_mask
].mutex
;
403 static void lock_all_segments(private_ike_sa_manager_t
*this)
407 for (i
= 0; i
< this->segment_count
; i
++)
409 this->segments
[i
].mutex
->lock(this->segments
[i
].mutex
);
414 * Unlock all segments
416 static void unlock_all_segments(private_ike_sa_manager_t
*this)
420 for (i
= 0; i
< this->segment_count
; i
++)
422 this->segments
[i
].mutex
->unlock(this->segments
[i
].mutex
);
426 typedef struct private_enumerator_t private_enumerator_t
;
429 * hash table enumerator implementation
431 struct private_enumerator_t
{
434 * implements enumerator interface
436 enumerator_t enumerator
;
439 * associated ike_sa_manager_t
441 private_ike_sa_manager_t
*manager
;
444 * current segment index
449 * currently enumerating entry
454 * current table row index
459 * enumerator for the current table row
461 enumerator_t
*current
;
464 METHOD(enumerator_t
, enumerate
, bool,
465 private_enumerator_t
*this, entry_t
**entry
, u_int
*segment
)
469 this->entry
->condvar
->signal(this->entry
->condvar
);
472 while (this->segment
< this->manager
->segment_count
)
474 while (this->row
< this->manager
->table_size
)
480 if (this->current
->enumerate(this->current
, &item
))
482 *entry
= this->entry
= item
;
483 *segment
= this->segment
;
486 this->current
->destroy(this->current
);
487 this->current
= NULL
;
488 unlock_single_segment(this->manager
, this->segment
);
494 lock_single_segment(this->manager
, this->segment
);
495 if ((list
= this->manager
->ike_sa_table
[this->row
]) != NULL
&&
496 list
->get_count(list
))
498 this->current
= list
->create_enumerator(list
);
501 unlock_single_segment(this->manager
, this->segment
);
503 this->row
+= this->manager
->segment_count
;
506 this->row
= this->segment
;
511 METHOD(enumerator_t
, enumerator_destroy
, void,
512 private_enumerator_t
*this)
516 this->entry
->condvar
->signal(this->entry
->condvar
);
520 this->current
->destroy(this->current
);
521 unlock_single_segment(this->manager
, this->segment
);
527 * Creates an enumerator to enumerate the entries in the hash table.
529 static enumerator_t
* create_table_enumerator(private_ike_sa_manager_t
*this)
531 private_enumerator_t
*enumerator
;
535 .enumerate
= (void*)_enumerate
,
536 .destroy
= _enumerator_destroy
,
540 return &enumerator
->enumerator
;
544 * Put an entry into the hash table.
545 * Note: The caller has to unlock the returned segment.
547 static u_int
put_entry(private_ike_sa_manager_t
*this, entry_t
*entry
)
552 row
= ike_sa_id_hash(entry
->ike_sa_id
) & this->table_mask
;
553 segment
= row
& this->segment_mask
;
555 lock_single_segment(this, segment
);
556 list
= this->ike_sa_table
[row
];
559 list
= this->ike_sa_table
[row
] = linked_list_create();
561 list
->insert_last(list
, entry
);
562 this->segments
[segment
].count
++;
567 * Remove an entry from the hash table.
568 * Note: The caller MUST have a lock on the segment of this entry.
570 static void remove_entry(private_ike_sa_manager_t
*this, entry_t
*entry
)
575 row
= ike_sa_id_hash(entry
->ike_sa_id
) & this->table_mask
;
576 segment
= row
& this->segment_mask
;
577 list
= this->ike_sa_table
[row
];
581 enumerator_t
*enumerator
;
583 enumerator
= list
->create_enumerator(list
);
584 while (enumerator
->enumerate(enumerator
, ¤t
))
586 if (current
== entry
)
588 list
->remove_at(list
, enumerator
);
589 this->segments
[segment
].count
--;
593 enumerator
->destroy(enumerator
);
598 * Remove the entry at the current enumerator position.
600 static void remove_entry_at(private_enumerator_t
*this)
605 linked_list_t
*list
= this->manager
->ike_sa_table
[this->row
];
606 list
->remove_at(list
, this->current
);
607 this->manager
->segments
[this->segment
].count
--;
612 * Find an entry using the provided match function to compare the entries for
615 static status_t
get_entry_by_match_function(private_ike_sa_manager_t
*this,
616 ike_sa_id_t
*ike_sa_id
, entry_t
**entry
, u_int
*segment
,
617 linked_list_match_t match
, void *p1
, void *p2
)
623 row
= ike_sa_id_hash(ike_sa_id
) & this->table_mask
;
624 seg
= row
& this->segment_mask
;
626 lock_single_segment(this, seg
);
627 list
= this->ike_sa_table
[row
];
630 if (list
->find_first(list
, match
, (void**)¤t
, p1
, p2
) == SUCCESS
)
634 /* the locked segment has to be unlocked by the caller */
638 unlock_single_segment(this, seg
);
643 * Find an entry by ike_sa_id_t.
644 * Note: On SUCCESS, the caller has to unlock the segment.
646 static status_t
get_entry_by_id(private_ike_sa_manager_t
*this,
647 ike_sa_id_t
*ike_sa_id
, entry_t
**entry
, u_int
*segment
)
649 return get_entry_by_match_function(this, ike_sa_id
, entry
, segment
,
650 (linked_list_match_t
)entry_match_by_id
, ike_sa_id
, NULL
);
654 * Find an entry by initiator SPI and IKE_SA_INIT hash.
655 * Note: On SUCCESS, the caller has to unlock the segment.
657 static status_t
get_entry_by_hash(private_ike_sa_manager_t
*this,
658 ike_sa_id_t
*ike_sa_id
, chunk_t hash
, entry_t
**entry
, u_int
*segment
)
660 return get_entry_by_match_function(this, ike_sa_id
, entry
, segment
,
661 (linked_list_match_t
)entry_match_by_hash
, ike_sa_id
, &hash
);
665 * Find an entry by IKE_SA pointer.
666 * Note: On SUCCESS, the caller has to unlock the segment.
668 static status_t
get_entry_by_sa(private_ike_sa_manager_t
*this,
669 ike_sa_id_t
*ike_sa_id
, ike_sa_t
*ike_sa
, entry_t
**entry
, u_int
*segment
)
671 return get_entry_by_match_function(this, ike_sa_id
, entry
, segment
,
672 (linked_list_match_t
)entry_match_by_sa
, ike_sa
, NULL
);
676 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
679 static bool wait_for_entry(private_ike_sa_manager_t
*this, entry_t
*entry
,
682 if (entry
->driveout_new_threads
)
684 /* we are not allowed to get this */
687 while (entry
->checked_out
&& !entry
->driveout_waiting_threads
)
689 /* so wait until we can get it for us.
690 * we register us as waiting. */
691 entry
->waiting_threads
++;
692 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
693 entry
->waiting_threads
--;
695 /* hm, a deletion request forbids us to get this SA, get next one */
696 if (entry
->driveout_waiting_threads
)
698 /* we must signal here, others may be waiting on it, too */
699 entry
->condvar
->signal(entry
->condvar
);
706 * Put a half-open SA into the hash table.
708 static void put_half_open(private_ike_sa_manager_t
*this, entry_t
*entry
)
710 half_open_t
*half_open
= NULL
;
716 addr
= entry
->other
->get_address(entry
->other
);
717 row
= chunk_hash(addr
) & this->table_mask
;
718 segment
= row
& this->segment_mask
;
719 lock
= this->half_open_segments
[segment
].lock
;
720 lock
->write_lock(lock
);
721 list
= this->half_open_table
[row
];
724 half_open_t
*current
;
726 if (list
->find_first(list
, (linked_list_match_t
)half_open_match
,
727 (void**)¤t
, &addr
) == SUCCESS
)
731 this->half_open_segments
[segment
].count
++;
736 list
= this->half_open_table
[row
] = linked_list_create();
742 .other
= chunk_clone(addr
),
745 list
->insert_last(list
, half_open
);
746 this->half_open_segments
[segment
].count
++;
752 * Remove a half-open SA from the hash table.
754 static void remove_half_open(private_ike_sa_manager_t
*this, entry_t
*entry
)
761 addr
= entry
->other
->get_address(entry
->other
);
762 row
= chunk_hash(addr
) & this->table_mask
;
763 segment
= row
& this->segment_mask
;
764 lock
= this->half_open_segments
[segment
].lock
;
765 lock
->write_lock(lock
);
766 list
= this->half_open_table
[row
];
769 half_open_t
*current
;
770 enumerator_t
*enumerator
;
772 enumerator
= list
->create_enumerator(list
);
773 while (enumerator
->enumerate(enumerator
, ¤t
))
775 if (half_open_match(current
, &addr
))
777 if (--current
->count
== 0)
779 list
->remove_at(list
, enumerator
);
780 half_open_destroy(current
);
782 this->half_open_segments
[segment
].count
--;
786 enumerator
->destroy(enumerator
);
792 * Put an SA between two peers into the hash table.
794 static void put_connected_peers(private_ike_sa_manager_t
*this, entry_t
*entry
)
796 connected_peers_t
*connected_peers
= NULL
;
797 chunk_t my_id
, other_id
;
802 my_id
= entry
->my_id
->get_encoding(entry
->my_id
);
803 other_id
= entry
->other_id
->get_encoding(entry
->other_id
);
804 row
= chunk_hash_inc(other_id
, chunk_hash(my_id
)) & this->table_mask
;
805 segment
= row
& this->segment_mask
;
806 lock
= this->connected_peers_segments
[segment
].lock
;
807 lock
->write_lock(lock
);
808 list
= this->connected_peers_table
[row
];
811 connected_peers_t
*current
;
813 if (list
->find_first(list
, (linked_list_match_t
)connected_peers_match
,
814 (void**)¤t
, entry
->my_id
, entry
->other_id
,
815 (uintptr_t)entry
->other
->get_family(entry
->other
)) == SUCCESS
)
817 connected_peers
= current
;
818 if (connected_peers
->sas
->find_first(connected_peers
->sas
,
819 (linked_list_match_t
)entry
->ike_sa_id
->equals
,
820 NULL
, entry
->ike_sa_id
) == SUCCESS
)
829 list
= this->connected_peers_table
[row
] = linked_list_create();
832 if (!connected_peers
)
834 INIT(connected_peers
,
835 .my_id
= entry
->my_id
->clone(entry
->my_id
),
836 .other_id
= entry
->other_id
->clone(entry
->other_id
),
837 .family
= entry
->other
->get_family(entry
->other
),
838 .sas
= linked_list_create(),
840 list
->insert_last(list
, connected_peers
);
842 connected_peers
->sas
->insert_last(connected_peers
->sas
,
843 entry
->ike_sa_id
->clone(entry
->ike_sa_id
));
844 this->connected_peers_segments
[segment
].count
++;
849 * Remove an SA between two peers from the hash table.
851 static void remove_connected_peers(private_ike_sa_manager_t
*this, entry_t
*entry
)
853 chunk_t my_id
, other_id
;
858 my_id
= entry
->my_id
->get_encoding(entry
->my_id
);
859 other_id
= entry
->other_id
->get_encoding(entry
->other_id
);
860 row
= chunk_hash_inc(other_id
, chunk_hash(my_id
)) & this->table_mask
;
861 segment
= row
& this->segment_mask
;
863 lock
= this->connected_peers_segments
[segment
].lock
;
864 lock
->write_lock(lock
);
865 list
= this->connected_peers_table
[row
];
868 connected_peers_t
*current
;
869 enumerator_t
*enumerator
;
871 enumerator
= list
->create_enumerator(list
);
872 while (enumerator
->enumerate(enumerator
, ¤t
))
874 if (connected_peers_match(current
, entry
->my_id
, entry
->other_id
,
875 (uintptr_t)entry
->other
->get_family(entry
->other
)))
877 ike_sa_id_t
*ike_sa_id
;
880 inner
= current
->sas
->create_enumerator(current
->sas
);
881 while (inner
->enumerate(inner
, &ike_sa_id
))
883 if (ike_sa_id
->equals(ike_sa_id
, entry
->ike_sa_id
))
885 current
->sas
->remove_at(current
->sas
, inner
);
886 ike_sa_id
->destroy(ike_sa_id
);
887 this->connected_peers_segments
[segment
].count
--;
891 inner
->destroy(inner
);
892 if (current
->sas
->get_count(current
->sas
) == 0)
894 list
->remove_at(list
, enumerator
);
895 connected_peers_destroy(current
);
900 enumerator
->destroy(enumerator
);
906 * Get a random SPI for new IKE_SAs
908 static u_int64_t
get_spi(private_ike_sa_manager_t
*this)
914 this->rng
->get_bytes(this->rng
, sizeof(spi
), (u_int8_t
*)&spi
);
919 METHOD(ike_sa_manager_t
, checkout
, ike_sa_t
*,
920 private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
)
922 ike_sa_t
*ike_sa
= NULL
;
926 DBG2(DBG_MGR
, "checkout IKE_SA");
928 if (get_entry_by_id(this, ike_sa_id
, &entry
, &segment
) == SUCCESS
)
930 if (wait_for_entry(this, entry
, segment
))
932 entry
->checked_out
= TRUE
;
933 ike_sa
= entry
->ike_sa
;
934 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
935 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
937 unlock_single_segment(this, segment
);
939 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
943 METHOD(ike_sa_manager_t
, checkout_new
, ike_sa_t
*,
944 private_ike_sa_manager_t
* this, bool initiator
)
946 ike_sa_id_t
*ike_sa_id
;
951 ike_sa_id
= ike_sa_id_create(get_spi(this), 0, TRUE
);
955 ike_sa_id
= ike_sa_id_create(0, get_spi(this), FALSE
);
957 ike_sa
= ike_sa_create(ike_sa_id
);
958 ike_sa_id
->destroy(ike_sa_id
);
960 DBG2(DBG_MGR
, "created IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
961 ike_sa
->get_unique_id(ike_sa
));
966 METHOD(ike_sa_manager_t
, checkout_by_message
, ike_sa_t
*,
967 private_ike_sa_manager_t
* this, message_t
*message
)
971 ike_sa_t
*ike_sa
= NULL
;
974 id
= message
->get_ike_sa_id(message
);
976 id
->switch_initiator(id
);
978 DBG2(DBG_MGR
, "checkout IKE_SA by message");
980 if (message
->get_request(message
) &&
981 message
->get_exchange_type(message
) == IKE_SA_INIT
&&
984 /* IKE_SA_INIT request. Check for an IKE_SA with such a message hash. */
987 data
= message
->get_packet_data(message
);
988 this->hasher
->allocate_hash(this->hasher
, data
, &hash
);
991 if (get_entry_by_hash(this, id
, hash
, &entry
, &segment
) == SUCCESS
)
993 if (entry
->message_id
== 0)
995 unlock_single_segment(this, segment
);
998 DBG1(DBG_MGR
, "ignoring IKE_SA_INIT, already processing");
1001 else if (wait_for_entry(this, entry
, segment
))
1003 entry
->checked_out
= TRUE
;
1004 entry
->message_id
= message
->get_message_id(message
);
1005 ike_sa
= entry
->ike_sa
;
1006 DBG2(DBG_MGR
, "IKE_SA %s[%u] checked out by hash",
1007 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1009 unlock_single_segment(this, segment
);
1014 if (id
->get_responder_spi(id
) == 0 &&
1015 message
->get_exchange_type(message
) == IKE_SA_INIT
)
1017 /* no IKE_SA found, create a new one */
1018 id
->set_responder_spi(id
, get_spi(this));
1019 entry
= entry_create();
1020 entry
->ike_sa
= ike_sa_create(id
);
1021 entry
->ike_sa_id
= id
->clone(id
);
1023 segment
= put_entry(this, entry
);
1024 entry
->checked_out
= TRUE
;
1025 unlock_single_segment(this, segment
);
1027 entry
->message_id
= message
->get_message_id(message
);
1028 entry
->init_hash
= hash
;
1029 ike_sa
= entry
->ike_sa
;
1031 DBG2(DBG_MGR
, "created IKE_SA %s[%u]",
1032 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1037 DBG1(DBG_MGR
, "ignoring message, no such IKE_SA");
1045 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1049 if (get_entry_by_id(this, id
, &entry
, &segment
) == SUCCESS
)
1051 /* only check out if we are not processing this request */
1052 if (message
->get_request(message
) &&
1053 message
->get_message_id(message
) == entry
->message_id
)
1055 DBG1(DBG_MGR
, "ignoring request with ID %d, already processing",
1058 else if (wait_for_entry(this, entry
, segment
))
1060 ike_sa_id_t
*ike_id
= entry
->ike_sa
->get_id(entry
->ike_sa
);
1061 entry
->checked_out
= TRUE
;
1062 entry
->message_id
= message
->get_message_id(message
);
1063 if (ike_id
->get_responder_spi(ike_id
) == 0)
1065 ike_id
->set_responder_spi(ike_id
, id
->get_responder_spi(id
));
1067 ike_sa
= entry
->ike_sa
;
1068 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1069 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1071 unlock_single_segment(this, segment
);
1074 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1078 METHOD(ike_sa_manager_t
, checkout_by_config
, ike_sa_t
*,
1079 private_ike_sa_manager_t
*this, peer_cfg_t
*peer_cfg
)
1081 enumerator_t
*enumerator
;
1083 ike_sa_t
*ike_sa
= NULL
;
1084 peer_cfg_t
*current_peer
;
1085 ike_cfg_t
*current_ike
;
1088 DBG2(DBG_MGR
, "checkout IKE_SA by config");
1090 if (!this->reuse_ikesa
)
1091 { /* IKE_SA reuse disable by config */
1092 ike_sa
= checkout_new(this, TRUE
);
1093 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1097 enumerator
= create_table_enumerator(this);
1098 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1100 if (!wait_for_entry(this, entry
, segment
))
1104 if (entry
->ike_sa
->get_state(entry
->ike_sa
) == IKE_DELETING
)
1105 { /* skip IKE_SAs which are not usable */
1109 current_peer
= entry
->ike_sa
->get_peer_cfg(entry
->ike_sa
);
1110 if (current_peer
&& current_peer
->equals(current_peer
, peer_cfg
))
1112 current_ike
= current_peer
->get_ike_cfg(current_peer
);
1113 if (current_ike
->equals(current_ike
, peer_cfg
->get_ike_cfg(peer_cfg
)))
1115 entry
->checked_out
= TRUE
;
1116 ike_sa
= entry
->ike_sa
;
1117 DBG2(DBG_MGR
, "found existing IKE_SA %u with a '%s' config",
1118 ike_sa
->get_unique_id(ike_sa
),
1119 current_peer
->get_name(current_peer
));
1124 enumerator
->destroy(enumerator
);
1127 { /* no IKE_SA using such a config, hand out a new */
1128 ike_sa
= checkout_new(this, TRUE
);
1130 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1134 METHOD(ike_sa_manager_t
, checkout_by_id
, ike_sa_t
*,
1135 private_ike_sa_manager_t
*this, u_int32_t id
, bool child
)
1137 enumerator_t
*enumerator
;
1138 iterator_t
*children
;
1140 ike_sa_t
*ike_sa
= NULL
;
1141 child_sa_t
*child_sa
;
1144 DBG2(DBG_MGR
, "checkout IKE_SA by ID");
1146 enumerator
= create_table_enumerator(this);
1147 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1149 if (wait_for_entry(this, entry
, segment
))
1151 /* look for a child with such a reqid ... */
1154 children
= entry
->ike_sa
->create_child_sa_iterator(entry
->ike_sa
);
1155 while (children
->iterate(children
, (void**)&child_sa
))
1157 if (child_sa
->get_reqid(child_sa
) == id
)
1159 ike_sa
= entry
->ike_sa
;
1163 children
->destroy(children
);
1165 else /* ... or for a IKE_SA with such a unique id */
1167 if (entry
->ike_sa
->get_unique_id(entry
->ike_sa
) == id
)
1169 ike_sa
= entry
->ike_sa
;
1172 /* got one, return */
1175 entry
->checked_out
= TRUE
;
1176 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1177 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1182 enumerator
->destroy(enumerator
);
1184 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1188 METHOD(ike_sa_manager_t
, checkout_by_name
, ike_sa_t
*,
1189 private_ike_sa_manager_t
*this, char *name
, bool child
)
1191 enumerator_t
*enumerator
;
1192 iterator_t
*children
;
1194 ike_sa_t
*ike_sa
= NULL
;
1195 child_sa_t
*child_sa
;
1198 enumerator
= create_table_enumerator(this);
1199 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1201 if (wait_for_entry(this, entry
, segment
))
1203 /* look for a child with such a policy name ... */
1206 children
= entry
->ike_sa
->create_child_sa_iterator(entry
->ike_sa
);
1207 while (children
->iterate(children
, (void**)&child_sa
))
1209 if (streq(child_sa
->get_name(child_sa
), name
))
1211 ike_sa
= entry
->ike_sa
;
1215 children
->destroy(children
);
1217 else /* ... or for a IKE_SA with such a connection name */
1219 if (streq(entry
->ike_sa
->get_name(entry
->ike_sa
), name
))
1221 ike_sa
= entry
->ike_sa
;
1224 /* got one, return */
1227 entry
->checked_out
= TRUE
;
1228 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1229 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1234 enumerator
->destroy(enumerator
);
1236 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1241 * enumerator filter function, waiting variant
1243 static bool enumerator_filter_wait(private_ike_sa_manager_t
*this,
1244 entry_t
**in
, ike_sa_t
**out
, u_int
*segment
)
1246 if (wait_for_entry(this, *in
, *segment
))
1248 *out
= (*in
)->ike_sa
;
1255 * enumerator filter function, skipping variant
1257 static bool enumerator_filter_skip(private_ike_sa_manager_t
*this,
1258 entry_t
**in
, ike_sa_t
**out
, u_int
*segment
)
1260 if (!(*in
)->driveout_new_threads
&&
1261 !(*in
)->driveout_waiting_threads
&&
1262 !(*in
)->checked_out
)
1264 *out
= (*in
)->ike_sa
;
1270 METHOD(ike_sa_manager_t
, create_enumerator
, enumerator_t
*,
1271 private_ike_sa_manager_t
* this, bool wait
)
1273 return enumerator_create_filter(create_table_enumerator(this),
1274 wait ?
(void*)enumerator_filter_wait
: (void*)enumerator_filter_skip
,
1278 METHOD(ike_sa_manager_t
, checkin
, void,
1279 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
1281 /* to check the SA back in, we look for the pointer of the ike_sa
1283 * The lookup is done by initiator SPI, so even if the SPI has changed (e.g.
1284 * on reception of a IKE_SA_INIT response) the lookup will work but
1285 * updating of the SPI MAY be necessary...
1288 ike_sa_id_t
*ike_sa_id
;
1290 identification_t
*my_id
, *other_id
;
1293 ike_sa_id
= ike_sa
->get_id(ike_sa
);
1294 my_id
= ike_sa
->get_my_id(ike_sa
);
1295 other_id
= ike_sa
->get_other_id(ike_sa
);
1296 other
= ike_sa
->get_other_host(ike_sa
);
1298 DBG2(DBG_MGR
, "checkin IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1299 ike_sa
->get_unique_id(ike_sa
));
1301 /* look for the entry */
1302 if (get_entry_by_sa(this, ike_sa_id
, ike_sa
, &entry
, &segment
) == SUCCESS
)
1304 /* ike_sa_id must be updated */
1305 entry
->ike_sa_id
->replace_values(entry
->ike_sa_id
, ike_sa
->get_id(ike_sa
));
1306 /* signal waiting threads */
1307 entry
->checked_out
= FALSE
;
1308 entry
->message_id
= -1;
1309 /* check if this SA is half-open */
1310 if (entry
->half_open
&& ike_sa
->get_state(ike_sa
) != IKE_CONNECTING
)
1312 /* not half open anymore */
1313 entry
->half_open
= FALSE
;
1314 remove_half_open(this, entry
);
1316 else if (entry
->half_open
&& !other
->ip_equals(other
, entry
->other
))
1318 /* the other host's IP has changed, we must update the hash table */
1319 remove_half_open(this, entry
);
1320 DESTROY_IF(entry
->other
);
1321 entry
->other
= other
->clone(other
);
1322 put_half_open(this, entry
);
1324 else if (!entry
->half_open
&&
1325 !entry
->ike_sa_id
->is_initiator(entry
->ike_sa_id
) &&
1326 ike_sa
->get_state(ike_sa
) == IKE_CONNECTING
)
1328 /* this is a new half-open SA */
1329 entry
->half_open
= TRUE
;
1330 entry
->other
= other
->clone(other
);
1331 put_half_open(this, entry
);
1333 DBG2(DBG_MGR
, "check-in of IKE_SA successful.");
1334 entry
->condvar
->signal(entry
->condvar
);
1338 entry
= entry_create();
1339 entry
->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
1340 entry
->ike_sa
= ike_sa
;
1341 segment
= put_entry(this, entry
);
1344 /* apply identities for duplicate test */
1345 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
&&
1346 entry
->my_id
== NULL
&& entry
->other_id
== NULL
)
1348 entry
->my_id
= my_id
->clone(my_id
);
1349 entry
->other_id
= other_id
->clone(other_id
);
1352 entry
->other
= other
->clone(other
);
1354 put_connected_peers(this, entry
);
1357 unlock_single_segment(this, segment
);
1359 charon
->bus
->set_sa(charon
->bus
, NULL
);
1362 METHOD(ike_sa_manager_t
, checkin_and_destroy
, void,
1363 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
1365 /* deletion is a bit complex, we must ensure that no thread is waiting for
1367 * We take this SA from the table, and start signaling while threads
1368 * are in the condvar.
1371 ike_sa_id_t
*ike_sa_id
;
1374 ike_sa_id
= ike_sa
->get_id(ike_sa
);
1376 DBG2(DBG_MGR
, "checkin and destroy IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1377 ike_sa
->get_unique_id(ike_sa
));
1379 if (get_entry_by_sa(this, ike_sa_id
, ike_sa
, &entry
, &segment
) == SUCCESS
)
1381 /* drive out waiting threads, as we are in hurry */
1382 entry
->driveout_waiting_threads
= TRUE
;
1383 /* mark it, so no new threads can get this entry */
1384 entry
->driveout_new_threads
= TRUE
;
1385 /* wait until all workers have done their work */
1386 while (entry
->waiting_threads
)
1389 entry
->condvar
->broadcast(entry
->condvar
);
1390 /* they will wake us again when their work is done */
1391 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
1393 remove_entry(this, entry
);
1394 unlock_single_segment(this, segment
);
1396 if (entry
->half_open
)
1398 remove_half_open(this, entry
);
1400 if (entry
->my_id
&& entry
->other_id
)
1402 remove_connected_peers(this, entry
);
1405 entry_destroy(entry
);
1407 DBG2(DBG_MGR
, "check-in and destroy of IKE_SA successful");
1411 DBG1(DBG_MGR
, "tried to check-in and delete nonexisting IKE_SA");
1412 ike_sa
->destroy(ike_sa
);
1414 charon
->bus
->set_sa(charon
->bus
, NULL
);
1417 METHOD(ike_sa_manager_t
, check_uniqueness
, bool,
1418 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
, bool force_replace
)
1420 bool cancel
= FALSE
;
1421 peer_cfg_t
*peer_cfg
;
1422 unique_policy_t policy
;
1423 linked_list_t
*list
, *duplicate_ids
= NULL
;
1424 enumerator_t
*enumerator
;
1425 ike_sa_id_t
*duplicate_id
= NULL
;
1426 identification_t
*me
, *other
;
1430 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
1431 policy
= peer_cfg
->get_unique_policy(peer_cfg
);
1432 if (policy
== UNIQUE_NO
&& !force_replace
)
1437 me
= ike_sa
->get_my_id(ike_sa
);
1438 other
= ike_sa
->get_other_id(ike_sa
);
1440 row
= chunk_hash_inc(other
->get_encoding(other
),
1441 chunk_hash(me
->get_encoding(me
))) & this->table_mask
;
1442 segment
= row
& this->segment_mask
;
1444 lock
= this->connected_peers_segments
[segment
& this->segment_mask
].lock
;
1445 lock
->read_lock(lock
);
1446 list
= this->connected_peers_table
[row
];
1449 connected_peers_t
*current
;
1452 other_host
= ike_sa
->get_other_host(ike_sa
);
1453 if (list
->find_first(list
, (linked_list_match_t
)connected_peers_match
,
1454 (void**)¤t
, me
, other
,
1455 (uintptr_t)other_host
->get_family(other_host
)) == SUCCESS
)
1457 /* clone the list, so we can release the lock */
1458 duplicate_ids
= current
->sas
->clone_offset(current
->sas
,
1459 offsetof(ike_sa_id_t
, clone
));
1469 enumerator
= duplicate_ids
->create_enumerator(duplicate_ids
);
1470 while (enumerator
->enumerate(enumerator
, &duplicate_id
))
1472 status_t status
= SUCCESS
;
1473 ike_sa_t
*duplicate
;
1475 duplicate
= checkout(this, duplicate_id
);
1482 DBG1(DBG_IKE
, "destroying duplicate IKE_SA for peer '%Y', "
1483 "received INITIAL_CONTACT", other
);
1484 checkin_and_destroy(this, duplicate
);
1487 peer_cfg
= duplicate
->get_peer_cfg(duplicate
);
1488 if (peer_cfg
&& peer_cfg
->equals(peer_cfg
, ike_sa
->get_peer_cfg(ike_sa
)))
1490 switch (duplicate
->get_state(duplicate
))
1492 case IKE_ESTABLISHED
:
1496 case UNIQUE_REPLACE
:
1497 DBG1(DBG_IKE
, "deleting duplicate IKE_SA for peer "
1498 "'%Y' due to uniqueness policy", other
);
1499 status
= duplicate
->delete(duplicate
);
1503 /* we keep the first IKE_SA and delete all
1504 * other duplicates that might exist */
1505 policy
= UNIQUE_REPLACE
;
1515 if (status
== DESTROY_ME
)
1517 checkin_and_destroy(this, duplicate
);
1521 checkin(this, duplicate
);
1524 enumerator
->destroy(enumerator
);
1525 duplicate_ids
->destroy_offset(duplicate_ids
, offsetof(ike_sa_id_t
, destroy
));
1526 /* reset thread's current IKE_SA after checkin */
1527 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1531 METHOD(ike_sa_manager_t
, has_contact
, bool,
1532 private_ike_sa_manager_t
*this, identification_t
*me
,
1533 identification_t
*other
, int family
)
1535 linked_list_t
*list
;
1540 row
= chunk_hash_inc(other
->get_encoding(other
),
1541 chunk_hash(me
->get_encoding(me
))) & this->table_mask
;
1542 segment
= row
& this->segment_mask
;
1543 lock
= this->connected_peers_segments
[segment
& this->segment_mask
].lock
;
1544 lock
->read_lock(lock
);
1545 list
= this->connected_peers_table
[row
];
1548 if (list
->find_first(list
, (linked_list_match_t
)connected_peers_match
,
1549 NULL
, me
, other
, family
) == SUCCESS
)
1559 METHOD(ike_sa_manager_t
, get_half_open_count
, int,
1560 private_ike_sa_manager_t
*this, host_t
*ip
)
1562 linked_list_t
*list
;
1570 addr
= ip
->get_address(ip
);
1571 row
= chunk_hash(addr
) & this->table_mask
;
1572 segment
= row
& this->segment_mask
;
1573 lock
= this->half_open_segments
[segment
& this->segment_mask
].lock
;
1574 lock
->read_lock(lock
);
1575 if ((list
= this->half_open_table
[row
]) != NULL
)
1577 half_open_t
*current
;
1579 if (list
->find_first(list
, (linked_list_match_t
)half_open_match
,
1580 (void**)¤t
, &addr
) == SUCCESS
)
1582 count
= current
->count
;
1589 for (segment
= 0; segment
< this->segment_count
; segment
++)
1591 lock
= this->half_open_segments
[segment
& this->segment_mask
].lock
;
1592 lock
->read_lock(lock
);
1593 count
+= this->half_open_segments
[segment
].count
;
1600 METHOD(ike_sa_manager_t
, flush
, void,
1601 private_ike_sa_manager_t
*this)
1603 /* destroy all list entries */
1604 enumerator_t
*enumerator
;
1608 lock_all_segments(this);
1609 DBG2(DBG_MGR
, "going to destroy IKE_SA manager and all managed IKE_SA's");
1610 /* Step 1: drive out all waiting threads */
1611 DBG2(DBG_MGR
, "set driveout flags for all stored IKE_SA's");
1612 enumerator
= create_table_enumerator(this);
1613 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1615 /* do not accept new threads, drive out waiting threads */
1616 entry
->driveout_new_threads
= TRUE
;
1617 entry
->driveout_waiting_threads
= TRUE
;
1619 enumerator
->destroy(enumerator
);
1620 DBG2(DBG_MGR
, "wait for all threads to leave IKE_SA's");
1621 /* Step 2: wait until all are gone */
1622 enumerator
= create_table_enumerator(this);
1623 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1625 while (entry
->waiting_threads
|| entry
->checked_out
)
1628 entry
->condvar
->broadcast(entry
->condvar
);
1629 /* go sleeping until they are gone */
1630 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
1633 enumerator
->destroy(enumerator
);
1634 DBG2(DBG_MGR
, "delete all IKE_SA's");
1635 /* Step 3: initiate deletion of all IKE_SAs */
1636 enumerator
= create_table_enumerator(this);
1637 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1639 charon
->bus
->set_sa(charon
->bus
, entry
->ike_sa
);
1640 /* as the delete never gets processed, fire down events */
1641 switch (entry
->ike_sa
->get_state(entry
->ike_sa
))
1643 case IKE_ESTABLISHED
:
1646 charon
->bus
->ike_updown(charon
->bus
, entry
->ike_sa
, FALSE
);
1651 entry
->ike_sa
->delete(entry
->ike_sa
);
1653 enumerator
->destroy(enumerator
);
1655 DBG2(DBG_MGR
, "destroy all entries");
1656 /* Step 4: destroy all entries */
1657 enumerator
= create_table_enumerator(this);
1658 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1660 charon
->bus
->set_sa(charon
->bus
, entry
->ike_sa
);
1661 if (entry
->half_open
)
1663 remove_half_open(this, entry
);
1665 if (entry
->my_id
&& entry
->other_id
)
1667 remove_connected_peers(this, entry
);
1669 remove_entry_at((private_enumerator_t
*)enumerator
);
1670 entry_destroy(entry
);
1672 enumerator
->destroy(enumerator
);
1673 charon
->bus
->set_sa(charon
->bus
, NULL
);
1674 unlock_all_segments(this);
1676 this->rng
->destroy(this->rng
);
1678 this->hasher
->destroy(this->hasher
);
1679 this->hasher
= NULL
;
1682 METHOD(ike_sa_manager_t
, destroy
, void,
1683 private_ike_sa_manager_t
*this)
1687 for (i
= 0; i
< this->table_size
; i
++)
1689 DESTROY_IF(this->ike_sa_table
[i
]);
1690 DESTROY_IF(this->half_open_table
[i
]);
1691 DESTROY_IF(this->connected_peers_table
[i
]);
1693 free(this->ike_sa_table
);
1694 free(this->half_open_table
);
1695 free(this->connected_peers_table
);
1696 for (i
= 0; i
< this->segment_count
; i
++)
1698 this->segments
[i
].mutex
->destroy(this->segments
[i
].mutex
);
1699 this->half_open_segments
[i
].lock
->destroy(this->half_open_segments
[i
].lock
);
1700 this->connected_peers_segments
[i
].lock
->destroy(this->connected_peers_segments
[i
].lock
);
1702 free(this->segments
);
1703 free(this->half_open_segments
);
1704 free(this->connected_peers_segments
);
1710 * This function returns the next-highest power of two for the given number.
1711 * The algorithm works by setting all bits on the right-hand side of the most
1712 * significant 1 to 1 and then increments the whole number so it rolls over
1713 * to the nearest power of two. Note: returns 0 for n == 0
1715 static u_int
get_nearest_powerof2(u_int n
)
1720 for (i
= 1; i
< sizeof(u_int
) * 8; i
<<= 1)
1728 * Described in header.
1730 ike_sa_manager_t
*ike_sa_manager_create()
1732 private_ike_sa_manager_t
*this;
1737 .checkout
= _checkout
,
1738 .checkout_new
= _checkout_new
,
1739 .checkout_by_message
= _checkout_by_message
,
1740 .checkout_by_config
= _checkout_by_config
,
1741 .checkout_by_id
= _checkout_by_id
,
1742 .checkout_by_name
= _checkout_by_name
,
1743 .check_uniqueness
= _check_uniqueness
,
1744 .has_contact
= _has_contact
,
1745 .create_enumerator
= _create_enumerator
,
1746 .checkin
= _checkin
,
1747 .checkin_and_destroy
= _checkin_and_destroy
,
1748 .get_half_open_count
= _get_half_open_count
,
1750 .destroy
= _destroy
,
1754 this->hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_PREFERRED
);
1755 if (this->hasher
== NULL
)
1757 DBG1(DBG_MGR
, "manager initialization failed, no hasher supported");
1761 this->rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
1762 if (this->rng
== NULL
)
1764 DBG1(DBG_MGR
, "manager initialization failed, no RNG supported");
1765 this->hasher
->destroy(this->hasher
);
1770 this->table_size
= get_nearest_powerof2(lib
->settings
->get_int(lib
->settings
,
1771 "charon.ikesa_table_size", DEFAULT_HASHTABLE_SIZE
));
1772 this->table_size
= max(1, min(this->table_size
, MAX_HASHTABLE_SIZE
));
1773 this->table_mask
= this->table_size
- 1;
1775 this->segment_count
= get_nearest_powerof2(lib
->settings
->get_int(lib
->settings
,
1776 "charon.ikesa_table_segments", DEFAULT_SEGMENT_COUNT
));
1777 this->segment_count
= max(1, min(this->segment_count
, this->table_size
));
1778 this->segment_mask
= this->segment_count
- 1;
1779 this->ike_sa_table
= calloc(this->table_size
, sizeof(linked_list_t
*));
1781 this->segments
= (segment_t
*)calloc(this->segment_count
, sizeof(segment_t
));
1782 for (i
= 0; i
< this->segment_count
; i
++)
1784 this->segments
[i
].mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
);
1785 this->segments
[i
].count
= 0;
1788 /* we use the same table parameters for the table to track half-open SAs */
1789 this->half_open_table
= calloc(this->table_size
, sizeof(linked_list_t
*));
1790 this->half_open_segments
= calloc(this->segment_count
, sizeof(shareable_segment_t
));
1791 for (i
= 0; i
< this->segment_count
; i
++)
1793 this->half_open_segments
[i
].lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
1794 this->half_open_segments
[i
].count
= 0;
1797 /* also for the hash table used for duplicate tests */
1798 this->connected_peers_table
= calloc(this->table_size
, sizeof(linked_list_t
*));
1799 this->connected_peers_segments
= calloc(this->segment_count
, sizeof(shareable_segment_t
));
1800 for (i
= 0; i
< this->segment_count
; i
++)
1802 this->connected_peers_segments
[i
].lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
1803 this->connected_peers_segments
[i
].count
= 0;
1806 this->reuse_ikesa
= lib
->settings
->get_bool(lib
->settings
,
1807 "charon.reuse_ikesa", TRUE
);
1808 return &this->public;