2 * Copyright (C) 2005-2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 * Copyright (C) 2008-2016 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/thread.h>
27 #include <threading/condvar.h>
28 #include <threading/mutex.h>
29 #include <threading/rwlock.h>
30 #include <collections/linked_list.h>
31 #include <crypto/hashers/hasher.h>
32 #include <processing/jobs/delete_ike_sa_job.h>
34 /* the default size of the hash table (MUST be a power of 2) */
35 #define DEFAULT_HASHTABLE_SIZE 1
37 /* the maximum size of the hash table (MUST be a power of 2) */
38 #define MAX_HASHTABLE_SIZE (1 << 30)
40 /* the default number of segments (MUST be a power of 2) */
41 #define DEFAULT_SEGMENT_COUNT 1
43 typedef struct entry_t entry_t
;
46 * An entry in the linked list, contains IKE_SA, locking and lookup data.
51 * Number of threads waiting for this ike_sa_t object.
56 * Condvar where threads can wait until ike_sa_t object is free for use again.
61 * Thread by which this IKE_SA is currently checked out, if any
63 thread_t
*checked_out
;
66 * Does this SA drives out new threads?
68 bool driveout_new_threads
;
71 * Does this SA drives out waiting threads?
73 bool driveout_waiting_threads
;
76 * Identification of an IKE_SA (SPIs).
78 ike_sa_id_t
*ike_sa_id
;
81 * The contained ike_sa_t object.
86 * hash of the IKE_SA_INIT message, used to detect retransmissions
91 * remote host address, required for DoS detection and duplicate
92 * checking (host with same my_id and other_id is *not* considered
93 * a duplicate if the address family differs)
98 * As responder: Is this SA half-open?
103 * own identity, required for duplicate checking
105 identification_t
*my_id
;
108 * remote identity, required for duplicate checking
110 identification_t
*other_id
;
113 * message ID or hash of currently processing message, -1 if none
115 u_int32_t processing
;
119 * Implementation of entry_t.destroy.
121 static status_t
entry_destroy(entry_t
*this)
123 /* also destroy IKE SA */
124 this->ike_sa
->destroy(this->ike_sa
);
125 this->ike_sa_id
->destroy(this->ike_sa_id
);
126 chunk_free(&this->init_hash
);
127 DESTROY_IF(this->other
);
128 DESTROY_IF(this->my_id
);
129 DESTROY_IF(this->other_id
);
130 this->condvar
->destroy(this->condvar
);
136 * Creates a new entry for the ike_sa_t list.
138 static entry_t
*entry_create()
143 .condvar
= condvar_create(CONDVAR_TYPE_DEFAULT
),
151 * Function that matches entry_t objects by ike_sa_id_t.
153 static bool entry_match_by_id(entry_t
*entry
, ike_sa_id_t
*id
)
155 if (id
->equals(id
, entry
->ike_sa_id
))
159 if ((id
->get_responder_spi(id
) == 0 ||
160 entry
->ike_sa_id
->get_responder_spi(entry
->ike_sa_id
) == 0) &&
161 (id
->get_ike_version(id
) == IKEV1_MAJOR_VERSION
||
162 id
->is_initiator(id
) == entry
->ike_sa_id
->is_initiator(entry
->ike_sa_id
)) &&
163 id
->get_initiator_spi(id
) == entry
->ike_sa_id
->get_initiator_spi(entry
->ike_sa_id
))
165 /* this is TRUE for IKE_SAs that we initiated but have not yet received a response */
172 * Function that matches entry_t objects by ike_sa_t pointers.
174 static bool entry_match_by_sa(entry_t
*entry
, ike_sa_t
*ike_sa
)
176 return entry
->ike_sa
== ike_sa
;
180 * Hash function for ike_sa_id_t objects.
182 static u_int
ike_sa_id_hash(ike_sa_id_t
*ike_sa_id
)
184 /* IKEv2 does not mandate random SPIs (RFC 5996, 2.6), they just have to be
185 * locally unique, so we use our randomly allocated SPI whether we are
186 * initiator or responder to ensure a good distribution. The latter is not
187 * possible for IKEv1 as we don't know whether we are original initiator or
188 * not (based on the IKE header). But as RFC 2408, section 2.5.3 proposes
189 * SPIs (Cookies) to be allocated near random (we allocate them randomly
190 * anyway) it seems safe to always use the initiator SPI. */
191 if (ike_sa_id
->get_ike_version(ike_sa_id
) == IKEV1_MAJOR_VERSION
||
192 ike_sa_id
->is_initiator(ike_sa_id
))
194 return ike_sa_id
->get_initiator_spi(ike_sa_id
);
196 return ike_sa_id
->get_responder_spi(ike_sa_id
);
199 typedef struct half_open_t half_open_t
;
202 * Struct to manage half-open IKE_SAs per peer.
205 /** chunk of remote host address */
208 /** the number of half-open IKE_SAs with that host */
211 /** the number of half-open IKE_SAs we responded to with that host */
212 u_int count_responder
;
216 * Destroys a half_open_t object.
218 static void half_open_destroy(half_open_t
*this)
220 chunk_free(&this->other
);
224 typedef struct connected_peers_t connected_peers_t
;
226 struct connected_peers_t
{
228 identification_t
*my_id
;
230 /** remote identity */
231 identification_t
*other_id
;
233 /** ip address family of peer */
236 /** list of ike_sa_id_t objects of IKE_SAs between the two identities */
240 static void connected_peers_destroy(connected_peers_t
*this)
242 this->my_id
->destroy(this->my_id
);
243 this->other_id
->destroy(this->other_id
);
244 this->sas
->destroy(this->sas
);
249 * Function that matches connected_peers_t objects by the given ids.
251 static inline bool connected_peers_match(connected_peers_t
*connected_peers
,
252 identification_t
*my_id
, identification_t
*other_id
,
255 return my_id
->equals(my_id
, connected_peers
->my_id
) &&
256 other_id
->equals(other_id
, connected_peers
->other_id
) &&
257 (!family
|| family
== connected_peers
->family
);
260 typedef struct init_hash_t init_hash_t
;
263 /** hash of IKE_SA_INIT or initial phase1 message (data is not cloned) */
266 /** our SPI allocated for the IKE_SA based on this message */
270 typedef struct segment_t segment_t
;
273 * Struct to manage segments of the hash table.
276 /** mutex to access a segment exclusively */
279 /** the number of entries in this segment */
283 typedef struct shareable_segment_t shareable_segment_t
;
286 * Struct to manage segments of the "half-open" and "connected peers" hash tables.
288 struct shareable_segment_t
{
289 /** rwlock to access a segment non-/exclusively */
292 /** the number of entries in this segment - in case of the "half-open table"
293 * it's the sum of all half_open_t.count in a segment. */
297 typedef struct table_item_t table_item_t
;
300 * Instead of using linked_list_t for each bucket we store the data in our own
301 * list to save memory.
303 struct table_item_t
{
304 /** data of this item */
307 /** next item in the overflow list */
311 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t
;
314 * Additional private members of ike_sa_manager_t.
316 struct private_ike_sa_manager_t
{
318 * Public interface of ike_sa_manager_t.
320 ike_sa_manager_t
public;
323 * Hash table with entries for the ike_sa_t objects.
325 table_item_t
**ike_sa_table
;
328 * The size of the hash table.
333 * Mask to map the hashes to table rows.
338 * Segments of the hash table.
343 * The number of segments.
348 * Mask to map a table row to a segment.
353 * Hash table with half_open_t objects.
355 table_item_t
**half_open_table
;
358 * Segments of the "half-open" hash table.
360 shareable_segment_t
*half_open_segments
;
363 * Total number of half-open IKE_SAs.
365 refcount_t half_open_count
;
368 * Total number of half-open IKE_SAs as responder.
370 refcount_t half_open_count_responder
;
373 * Hash table with connected_peers_t objects.
375 table_item_t
**connected_peers_table
;
378 * Segments of the "connected peers" hash table.
380 shareable_segment_t
*connected_peers_segments
;
383 * Hash table with init_hash_t objects.
385 table_item_t
**init_hashes_table
;
388 * Segments of the "hashes" hash table.
390 segment_t
*init_hashes_segments
;
393 * RNG to get random SPIs for our side
398 * Registered callback for IKE SPIs
406 * Lock to access the RNG instance and the callback
411 * reuse existing IKE_SAs in checkout_by_config
416 * Configured IKE_SA limit, if any
422 * Acquire a lock to access the segment of the table row with the given index.
423 * It also works with the segment index directly.
425 static inline void lock_single_segment(private_ike_sa_manager_t
*this,
428 mutex_t
*lock
= this->segments
[index
& this->segment_mask
].mutex
;
433 * Release the lock required to access the segment of the table row with the given index.
434 * It also works with the segment index directly.
436 static inline void unlock_single_segment(private_ike_sa_manager_t
*this,
439 mutex_t
*lock
= this->segments
[index
& this->segment_mask
].mutex
;
446 static void lock_all_segments(private_ike_sa_manager_t
*this)
450 for (i
= 0; i
< this->segment_count
; i
++)
452 this->segments
[i
].mutex
->lock(this->segments
[i
].mutex
);
457 * Unlock all segments
459 static void unlock_all_segments(private_ike_sa_manager_t
*this)
463 for (i
= 0; i
< this->segment_count
; i
++)
465 this->segments
[i
].mutex
->unlock(this->segments
[i
].mutex
);
469 typedef struct private_enumerator_t private_enumerator_t
;
472 * hash table enumerator implementation
474 struct private_enumerator_t
{
477 * implements enumerator interface
479 enumerator_t enumerator
;
482 * associated ike_sa_manager_t
484 private_ike_sa_manager_t
*manager
;
487 * current segment index
492 * currently enumerating entry
497 * current table row index
504 table_item_t
*current
;
507 * previous table item
512 METHOD(enumerator_t
, enumerate
, bool,
513 private_enumerator_t
*this, entry_t
**entry
, u_int
*segment
)
517 this->entry
->condvar
->signal(this->entry
->condvar
);
520 while (this->segment
< this->manager
->segment_count
)
522 while (this->row
< this->manager
->table_size
)
524 this->prev
= this->current
;
527 this->current
= this->current
->next
;
531 lock_single_segment(this->manager
, this->segment
);
532 this->current
= this->manager
->ike_sa_table
[this->row
];
536 *entry
= this->entry
= this->current
->value
;
537 *segment
= this->segment
;
540 unlock_single_segment(this->manager
, this->segment
);
541 this->row
+= this->manager
->segment_count
;
544 this->row
= this->segment
;
549 METHOD(enumerator_t
, enumerator_destroy
, void,
550 private_enumerator_t
*this)
554 this->entry
->condvar
->signal(this->entry
->condvar
);
558 unlock_single_segment(this->manager
, this->segment
);
564 * Creates an enumerator to enumerate the entries in the hash table.
566 static enumerator_t
* create_table_enumerator(private_ike_sa_manager_t
*this)
568 private_enumerator_t
*enumerator
;
572 .enumerate
= (void*)_enumerate
,
573 .destroy
= _enumerator_destroy
,
577 return &enumerator
->enumerator
;
581 * Put an entry into the hash table.
582 * Note: The caller has to unlock the returned segment.
584 static u_int
put_entry(private_ike_sa_manager_t
*this, entry_t
*entry
)
586 table_item_t
*current
, *item
;
593 row
= ike_sa_id_hash(entry
->ike_sa_id
) & this->table_mask
;
594 segment
= row
& this->segment_mask
;
596 lock_single_segment(this, segment
);
597 current
= this->ike_sa_table
[row
];
599 { /* insert at the front of current bucket */
600 item
->next
= current
;
602 this->ike_sa_table
[row
] = item
;
603 this->segments
[segment
].count
++;
608 * Remove an entry from the hash table.
609 * Note: The caller MUST have a lock on the segment of this entry.
611 static void remove_entry(private_ike_sa_manager_t
*this, entry_t
*entry
)
613 table_item_t
*item
, *prev
= NULL
;
616 row
= ike_sa_id_hash(entry
->ike_sa_id
) & this->table_mask
;
617 segment
= row
& this->segment_mask
;
618 item
= this->ike_sa_table
[row
];
621 if (item
->value
== entry
)
625 prev
->next
= item
->next
;
629 this->ike_sa_table
[row
] = item
->next
;
631 this->segments
[segment
].count
--;
641 * Remove the entry at the current enumerator position.
643 static void remove_entry_at(private_enumerator_t
*this)
648 table_item_t
*current
= this->current
;
650 this->manager
->segments
[this->segment
].count
--;
651 this->current
= this->prev
;
655 this->prev
->next
= current
->next
;
659 this->manager
->ike_sa_table
[this->row
] = current
->next
;
660 unlock_single_segment(this->manager
, this->segment
);
667 * Find an entry using the provided match function to compare the entries for
670 static status_t
get_entry_by_match_function(private_ike_sa_manager_t
*this,
671 ike_sa_id_t
*ike_sa_id
, entry_t
**entry
, u_int
*segment
,
672 linked_list_match_t match
, void *param
)
677 row
= ike_sa_id_hash(ike_sa_id
) & this->table_mask
;
678 seg
= row
& this->segment_mask
;
680 lock_single_segment(this, seg
);
681 item
= this->ike_sa_table
[row
];
684 if (match(item
->value
, param
))
686 *entry
= item
->value
;
688 /* the locked segment has to be unlocked by the caller */
693 unlock_single_segment(this, seg
);
698 * Find an entry by ike_sa_id_t.
699 * Note: On SUCCESS, the caller has to unlock the segment.
701 static status_t
get_entry_by_id(private_ike_sa_manager_t
*this,
702 ike_sa_id_t
*ike_sa_id
, entry_t
**entry
, u_int
*segment
)
704 return get_entry_by_match_function(this, ike_sa_id
, entry
, segment
,
705 (linked_list_match_t
)entry_match_by_id
, ike_sa_id
);
709 * Find an entry by IKE_SA pointer.
710 * Note: On SUCCESS, the caller has to unlock the segment.
712 static status_t
get_entry_by_sa(private_ike_sa_manager_t
*this,
713 ike_sa_id_t
*ike_sa_id
, ike_sa_t
*ike_sa
, entry_t
**entry
, u_int
*segment
)
715 return get_entry_by_match_function(this, ike_sa_id
, entry
, segment
,
716 (linked_list_match_t
)entry_match_by_sa
, ike_sa
);
720 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
723 static bool wait_for_entry(private_ike_sa_manager_t
*this, entry_t
*entry
,
726 if (entry
->driveout_new_threads
)
728 /* we are not allowed to get this */
731 while (entry
->checked_out
&& !entry
->driveout_waiting_threads
)
733 /* so wait until we can get it for us.
734 * we register us as waiting. */
735 entry
->waiting_threads
++;
736 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
737 entry
->waiting_threads
--;
739 /* hm, a deletion request forbids us to get this SA, get next one */
740 if (entry
->driveout_waiting_threads
)
742 /* we must signal here, others may be waiting on it, too */
743 entry
->condvar
->signal(entry
->condvar
);
750 * Put a half-open SA into the hash table.
752 static void put_half_open(private_ike_sa_manager_t
*this, entry_t
*entry
)
758 half_open_t
*half_open
;
761 ike_id
= entry
->ike_sa_id
;
762 addr
= entry
->other
->get_address(entry
->other
);
763 row
= chunk_hash(addr
) & this->table_mask
;
764 segment
= row
& this->segment_mask
;
765 lock
= this->half_open_segments
[segment
].lock
;
766 lock
->write_lock(lock
);
767 item
= this->half_open_table
[row
];
770 half_open
= item
->value
;
772 if (chunk_equals(addr
, half_open
->other
))
782 .other
= chunk_clone(addr
),
786 .next
= this->half_open_table
[row
],
788 this->half_open_table
[row
] = item
;
791 ref_get(&this->half_open_count
);
792 if (!ike_id
->is_initiator(ike_id
))
794 half_open
->count_responder
++;
795 ref_get(&this->half_open_count_responder
);
797 this->half_open_segments
[segment
].count
++;
802 * Remove a half-open SA from the hash table.
804 static void remove_half_open(private_ike_sa_manager_t
*this, entry_t
*entry
)
806 table_item_t
*item
, *prev
= NULL
;
812 ike_id
= entry
->ike_sa_id
;
813 addr
= entry
->other
->get_address(entry
->other
);
814 row
= chunk_hash(addr
) & this->table_mask
;
815 segment
= row
& this->segment_mask
;
816 lock
= this->half_open_segments
[segment
].lock
;
817 lock
->write_lock(lock
);
818 item
= this->half_open_table
[row
];
821 half_open_t
*half_open
= item
->value
;
823 if (chunk_equals(addr
, half_open
->other
))
825 if (!ike_id
->is_initiator(ike_id
))
827 half_open
->count_responder
--;
828 ignore_result(ref_put(&this->half_open_count_responder
));
830 ignore_result(ref_put(&this->half_open_count
));
831 if (--half_open
->count
== 0)
835 prev
->next
= item
->next
;
839 this->half_open_table
[row
] = item
->next
;
841 half_open_destroy(half_open
);
844 this->half_open_segments
[segment
].count
--;
854 * Put an SA between two peers into the hash table.
856 static void put_connected_peers(private_ike_sa_manager_t
*this, entry_t
*entry
)
861 connected_peers_t
*connected_peers
;
862 chunk_t my_id
, other_id
;
865 my_id
= entry
->my_id
->get_encoding(entry
->my_id
);
866 other_id
= entry
->other_id
->get_encoding(entry
->other_id
);
867 family
= entry
->other
->get_family(entry
->other
);
868 row
= chunk_hash_inc(other_id
, chunk_hash(my_id
)) & this->table_mask
;
869 segment
= row
& this->segment_mask
;
870 lock
= this->connected_peers_segments
[segment
].lock
;
871 lock
->write_lock(lock
);
872 item
= this->connected_peers_table
[row
];
875 connected_peers
= item
->value
;
877 if (connected_peers_match(connected_peers
, entry
->my_id
,
878 entry
->other_id
, family
))
880 if (connected_peers
->sas
->find_first(connected_peers
->sas
,
881 (linked_list_match_t
)entry
->ike_sa_id
->equals
,
882 NULL
, entry
->ike_sa_id
) == SUCCESS
)
894 INIT(connected_peers
,
895 .my_id
= entry
->my_id
->clone(entry
->my_id
),
896 .other_id
= entry
->other_id
->clone(entry
->other_id
),
898 .sas
= linked_list_create(),
901 .value
= connected_peers
,
902 .next
= this->connected_peers_table
[row
],
904 this->connected_peers_table
[row
] = item
;
906 connected_peers
->sas
->insert_last(connected_peers
->sas
,
907 entry
->ike_sa_id
->clone(entry
->ike_sa_id
));
908 this->connected_peers_segments
[segment
].count
++;
913 * Remove an SA between two peers from the hash table.
915 static void remove_connected_peers(private_ike_sa_manager_t
*this, entry_t
*entry
)
917 table_item_t
*item
, *prev
= NULL
;
920 chunk_t my_id
, other_id
;
923 my_id
= entry
->my_id
->get_encoding(entry
->my_id
);
924 other_id
= entry
->other_id
->get_encoding(entry
->other_id
);
925 family
= entry
->other
->get_family(entry
->other
);
927 row
= chunk_hash_inc(other_id
, chunk_hash(my_id
)) & this->table_mask
;
928 segment
= row
& this->segment_mask
;
930 lock
= this->connected_peers_segments
[segment
].lock
;
931 lock
->write_lock(lock
);
932 item
= this->connected_peers_table
[row
];
935 connected_peers_t
*current
= item
->value
;
937 if (connected_peers_match(current
, entry
->my_id
, entry
->other_id
,
940 enumerator_t
*enumerator
;
941 ike_sa_id_t
*ike_sa_id
;
943 enumerator
= current
->sas
->create_enumerator(current
->sas
);
944 while (enumerator
->enumerate(enumerator
, &ike_sa_id
))
946 if (ike_sa_id
->equals(ike_sa_id
, entry
->ike_sa_id
))
948 current
->sas
->remove_at(current
->sas
, enumerator
);
949 ike_sa_id
->destroy(ike_sa_id
);
950 this->connected_peers_segments
[segment
].count
--;
954 enumerator
->destroy(enumerator
);
955 if (current
->sas
->get_count(current
->sas
) == 0)
959 prev
->next
= item
->next
;
963 this->connected_peers_table
[row
] = item
->next
;
965 connected_peers_destroy(current
);
977 * Get a random SPI for new IKE_SAs
979 static u_int64_t
get_spi(private_ike_sa_manager_t
*this)
983 this->spi_lock
->read_lock(this->spi_lock
);
986 spi
= this->spi_cb
.cb(this->spi_cb
.data
);
988 else if (!this->rng
||
989 !this->rng
->get_bytes(this->rng
, sizeof(spi
), (u_int8_t
*)&spi
))
993 this->spi_lock
->unlock(this->spi_lock
);
998 * Calculate the hash of the initial IKE message. Memory for the hash is
999 * allocated on success.
1001 * @returns TRUE on success
1003 static bool get_init_hash(hasher_t
*hasher
, message_t
*message
, chunk_t
*hash
)
1007 if (message
->get_first_payload_type(message
) == PLV1_FRAGMENT
)
1008 { /* only hash the source IP, port and SPI for fragmented init messages */
1012 src
= message
->get_source(message
);
1013 if (!hasher
->allocate_hash(hasher
, src
->get_address(src
), NULL
))
1017 port
= src
->get_port(src
);
1018 if (!hasher
->allocate_hash(hasher
, chunk_from_thing(port
), NULL
))
1022 spi
= message
->get_initiator_spi(message
);
1023 return hasher
->allocate_hash(hasher
, chunk_from_thing(spi
), hash
);
1025 if (message
->get_exchange_type(message
) == ID_PROT
)
1026 { /* include the source for Main Mode as the hash will be the same if
1027 * SPIs are reused by two initiators that use the same proposal */
1028 src
= message
->get_source(message
);
1030 if (!hasher
->allocate_hash(hasher
, src
->get_address(src
), NULL
))
1035 return hasher
->allocate_hash(hasher
, message
->get_packet_data(message
), hash
);
1039 * Check if we already have created an IKE_SA based on the initial IKE message
1040 * with the given hash.
1041 * If not the hash is stored, the hash data is not(!) cloned.
1043 * Also, the local SPI is returned. In case of a retransmit this is already
1044 * stored together with the hash, otherwise it is newly allocated and should
1045 * be used to create the IKE_SA.
1047 * @returns ALREADY_DONE if the message with the given hash has been seen before
1048 * NOT_FOUND if the message hash was not found
1049 * FAILED if the SPI allocation failed
1051 static status_t
check_and_put_init_hash(private_ike_sa_manager_t
*this,
1052 chunk_t init_hash
, u_int64_t
*our_spi
)
1060 row
= chunk_hash(init_hash
) & this->table_mask
;
1061 segment
= row
& this->segment_mask
;
1062 mutex
= this->init_hashes_segments
[segment
].mutex
;
1064 item
= this->init_hashes_table
[row
];
1067 init_hash_t
*current
= item
->value
;
1069 if (chunk_equals(init_hash
, current
->hash
))
1071 *our_spi
= current
->our_spi
;
1072 mutex
->unlock(mutex
);
1073 return ALREADY_DONE
;
1078 spi
= get_spi(this);
1086 .len
= init_hash
.len
,
1087 .ptr
= init_hash
.ptr
,
1093 .next
= this->init_hashes_table
[row
],
1095 this->init_hashes_table
[row
] = item
;
1096 *our_spi
= init
->our_spi
;
1097 mutex
->unlock(mutex
);
1102 * Remove the hash of an initial IKE message from the cache.
1104 static void remove_init_hash(private_ike_sa_manager_t
*this, chunk_t init_hash
)
1106 table_item_t
*item
, *prev
= NULL
;
1110 row
= chunk_hash(init_hash
) & this->table_mask
;
1111 segment
= row
& this->segment_mask
;
1112 mutex
= this->init_hashes_segments
[segment
].mutex
;
1114 item
= this->init_hashes_table
[row
];
1117 init_hash_t
*current
= item
->value
;
1119 if (chunk_equals(init_hash
, current
->hash
))
1123 prev
->next
= item
->next
;
1127 this->init_hashes_table
[row
] = item
->next
;
1136 mutex
->unlock(mutex
);
1139 METHOD(ike_sa_manager_t
, checkout
, ike_sa_t
*,
1140 private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
)
1142 ike_sa_t
*ike_sa
= NULL
;
1146 DBG2(DBG_MGR
, "checkout IKE_SA");
1148 if (get_entry_by_id(this, ike_sa_id
, &entry
, &segment
) == SUCCESS
)
1150 if (wait_for_entry(this, entry
, segment
))
1152 entry
->checked_out
= thread_current();
1153 ike_sa
= entry
->ike_sa
;
1154 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1155 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1157 unlock_single_segment(this, segment
);
1159 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1163 METHOD(ike_sa_manager_t
, checkout_new
, ike_sa_t
*,
1164 private_ike_sa_manager_t
* this, ike_version_t version
, bool initiator
)
1166 ike_sa_id_t
*ike_sa_id
;
1168 u_int8_t ike_version
;
1171 ike_version
= version
== IKEV1 ? IKEV1_MAJOR_VERSION
: IKEV2_MAJOR_VERSION
;
1173 spi
= get_spi(this);
1176 DBG1(DBG_MGR
, "failed to allocate SPI for new IKE_SA");
1182 ike_sa_id
= ike_sa_id_create(ike_version
, spi
, 0, TRUE
);
1186 ike_sa_id
= ike_sa_id_create(ike_version
, 0, spi
, FALSE
);
1188 ike_sa
= ike_sa_create(ike_sa_id
, initiator
, version
);
1189 ike_sa_id
->destroy(ike_sa_id
);
1193 DBG2(DBG_MGR
, "created IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1194 ike_sa
->get_unique_id(ike_sa
));
1200 * Get the message ID or message hash to detect early retransmissions
1202 static u_int32_t
get_message_id_or_hash(message_t
*message
)
1204 if (message
->get_major_version(message
) == IKEV1_MAJOR_VERSION
)
1206 /* Use a hash for IKEv1 Phase 1, where we don't have a MID, and Quick
1207 * Mode, where all three messages use the same message ID */
1208 if (message
->get_message_id(message
) == 0 ||
1209 message
->get_exchange_type(message
) == QUICK_MODE
)
1211 return chunk_hash(message
->get_packet_data(message
));
1214 return message
->get_message_id(message
);
1217 METHOD(ike_sa_manager_t
, checkout_by_message
, ike_sa_t
*,
1218 private_ike_sa_manager_t
* this, message_t
*message
)
1222 ike_sa_t
*ike_sa
= NULL
;
1224 ike_version_t ike_version
;
1225 bool is_init
= FALSE
;
1227 id
= message
->get_ike_sa_id(message
);
1228 /* clone the IKE_SA ID so we can modify the initiator flag */
1230 id
->switch_initiator(id
);
1232 DBG2(DBG_MGR
, "checkout IKE_SA by message");
1234 if (id
->get_responder_spi(id
) == 0 &&
1235 message
->get_message_id(message
) == 0)
1237 if (message
->get_major_version(message
) == IKEV2_MAJOR_VERSION
)
1239 if (message
->get_exchange_type(message
) == IKE_SA_INIT
&&
1240 message
->get_request(message
))
1242 ike_version
= IKEV2
;
1248 if (message
->get_exchange_type(message
) == ID_PROT
||
1249 message
->get_exchange_type(message
) == AGGRESSIVE
)
1251 ike_version
= IKEV1
;
1253 if (id
->is_initiator(id
))
1254 { /* not set in IKEv1, switch back before applying to new SA */
1255 id
->switch_initiator(id
);
1267 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1268 if (!hasher
|| !get_init_hash(hasher
, message
, &hash
))
1270 DBG1(DBG_MGR
, "ignoring message, failed to hash message");
1275 hasher
->destroy(hasher
);
1277 /* ensure this is not a retransmit of an already handled init message */
1278 switch (check_and_put_init_hash(this, hash
, &our_spi
))
1281 { /* we've not seen this packet yet, create a new IKE_SA */
1282 if (!this->ikesa_limit
||
1283 this->public.get_count(&this->public) < this->ikesa_limit
)
1285 id
->set_responder_spi(id
, our_spi
);
1286 ike_sa
= ike_sa_create(id
, FALSE
, ike_version
);
1289 entry
= entry_create();
1290 entry
->ike_sa
= ike_sa
;
1291 entry
->ike_sa_id
= id
;
1292 entry
->processing
= get_message_id_or_hash(message
);
1293 entry
->init_hash
= hash
;
1295 segment
= put_entry(this, entry
);
1296 entry
->checked_out
= thread_current();
1297 unlock_single_segment(this, segment
);
1299 DBG2(DBG_MGR
, "created IKE_SA %s[%u]",
1300 ike_sa
->get_name(ike_sa
),
1301 ike_sa
->get_unique_id(ike_sa
));
1303 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1308 DBG1(DBG_MGR
, "creating IKE_SA failed, ignoring message");
1313 DBG1(DBG_MGR
, "ignoring %N, hitting IKE_SA limit (%u)",
1314 exchange_type_names
, message
->get_exchange_type(message
),
1317 remove_init_hash(this, hash
);
1323 { /* we failed to allocate an SPI */
1326 DBG1(DBG_MGR
, "ignoring message, failed to allocate SPI");
1333 /* it looks like we already handled this init message to some degree */
1334 id
->set_responder_spi(id
, our_spi
);
1338 if (get_entry_by_id(this, id
, &entry
, &segment
) == SUCCESS
)
1340 /* only check out if we are not already processing it. */
1341 if (entry
->processing
== get_message_id_or_hash(message
))
1343 DBG1(DBG_MGR
, "ignoring request with ID %u, already processing",
1346 else if (wait_for_entry(this, entry
, segment
))
1348 ike_sa_id_t
*ike_id
;
1350 ike_id
= entry
->ike_sa
->get_id(entry
->ike_sa
);
1351 entry
->checked_out
= thread_current();
1352 if (message
->get_first_payload_type(message
) != PLV1_FRAGMENT
&&
1353 message
->get_first_payload_type(message
) != PLV2_FRAGMENT
)
1354 { /* TODO-FRAG: this fails if there are unencrypted payloads */
1355 entry
->processing
= get_message_id_or_hash(message
);
1357 if (ike_id
->get_responder_spi(ike_id
) == 0)
1359 ike_id
->set_responder_spi(ike_id
, id
->get_responder_spi(id
));
1361 ike_sa
= entry
->ike_sa
;
1362 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1363 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1365 unlock_single_segment(this, segment
);
1369 charon
->bus
->alert(charon
->bus
, ALERT_INVALID_IKE_SPI
, message
);
1372 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1376 METHOD(ike_sa_manager_t
, checkout_by_config
, ike_sa_t
*,
1377 private_ike_sa_manager_t
*this, peer_cfg_t
*peer_cfg
)
1379 enumerator_t
*enumerator
;
1381 ike_sa_t
*ike_sa
= NULL
;
1382 peer_cfg_t
*current_peer
;
1383 ike_cfg_t
*current_ike
;
1386 DBG2(DBG_MGR
, "checkout IKE_SA by config");
1388 if (!this->reuse_ikesa
&& peer_cfg
->get_ike_version(peer_cfg
) != IKEV1
)
1389 { /* IKE_SA reuse disabled by config (not possible for IKEv1) */
1390 ike_sa
= checkout_new(this, peer_cfg
->get_ike_version(peer_cfg
), TRUE
);
1391 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1395 enumerator
= create_table_enumerator(this);
1396 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1398 if (!wait_for_entry(this, entry
, segment
))
1402 if (entry
->ike_sa
->get_state(entry
->ike_sa
) == IKE_DELETING
)
1403 { /* skip IKE_SAs which are not usable, wake other waiting threads */
1404 entry
->condvar
->signal(entry
->condvar
);
1408 current_peer
= entry
->ike_sa
->get_peer_cfg(entry
->ike_sa
);
1409 if (current_peer
&& current_peer
->equals(current_peer
, peer_cfg
))
1411 current_ike
= current_peer
->get_ike_cfg(current_peer
);
1412 if (current_ike
->equals(current_ike
, peer_cfg
->get_ike_cfg(peer_cfg
)))
1414 entry
->checked_out
= thread_current();
1415 ike_sa
= entry
->ike_sa
;
1416 DBG2(DBG_MGR
, "found existing IKE_SA %u with a '%s' config",
1417 ike_sa
->get_unique_id(ike_sa
),
1418 current_peer
->get_name(current_peer
));
1422 /* other threads might be waiting for this entry */
1423 entry
->condvar
->signal(entry
->condvar
);
1425 enumerator
->destroy(enumerator
);
1428 { /* no IKE_SA using such a config, hand out a new */
1429 ike_sa
= checkout_new(this, peer_cfg
->get_ike_version(peer_cfg
), TRUE
);
1431 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1435 METHOD(ike_sa_manager_t
, checkout_by_id
, ike_sa_t
*,
1436 private_ike_sa_manager_t
*this, u_int32_t id
)
1438 enumerator_t
*enumerator
;
1440 ike_sa_t
*ike_sa
= NULL
;
1443 DBG2(DBG_MGR
, "checkout IKE_SA by ID %u", id
);
1445 enumerator
= create_table_enumerator(this);
1446 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1448 if (wait_for_entry(this, entry
, segment
))
1450 if (entry
->ike_sa
->get_unique_id(entry
->ike_sa
) == id
)
1452 ike_sa
= entry
->ike_sa
;
1453 entry
->checked_out
= thread_current();
1456 /* other threads might be waiting for this entry */
1457 entry
->condvar
->signal(entry
->condvar
);
1460 enumerator
->destroy(enumerator
);
1464 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1465 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1467 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1471 METHOD(ike_sa_manager_t
, checkout_by_name
, ike_sa_t
*,
1472 private_ike_sa_manager_t
*this, char *name
, bool child
)
1474 enumerator_t
*enumerator
, *children
;
1476 ike_sa_t
*ike_sa
= NULL
;
1477 child_sa_t
*child_sa
;
1480 enumerator
= create_table_enumerator(this);
1481 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1483 if (wait_for_entry(this, entry
, segment
))
1485 /* look for a child with such a policy name ... */
1488 children
= entry
->ike_sa
->create_child_sa_enumerator(entry
->ike_sa
);
1489 while (children
->enumerate(children
, (void**)&child_sa
))
1491 if (streq(child_sa
->get_name(child_sa
), name
))
1493 ike_sa
= entry
->ike_sa
;
1497 children
->destroy(children
);
1499 else /* ... or for a IKE_SA with such a connection name */
1501 if (streq(entry
->ike_sa
->get_name(entry
->ike_sa
), name
))
1503 ike_sa
= entry
->ike_sa
;
1506 /* got one, return */
1509 entry
->checked_out
= thread_current();
1510 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1511 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1514 /* other threads might be waiting for this entry */
1515 entry
->condvar
->signal(entry
->condvar
);
1518 enumerator
->destroy(enumerator
);
1520 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1525 * enumerator filter function, waiting variant
1527 static bool enumerator_filter_wait(private_ike_sa_manager_t
*this,
1528 entry_t
**in
, ike_sa_t
**out
, u_int
*segment
)
1530 if (wait_for_entry(this, *in
, *segment
))
1532 *out
= (*in
)->ike_sa
;
1533 charon
->bus
->set_sa(charon
->bus
, *out
);
1540 * enumerator filter function, skipping variant
1542 static bool enumerator_filter_skip(private_ike_sa_manager_t
*this,
1543 entry_t
**in
, ike_sa_t
**out
, u_int
*segment
)
1545 if (!(*in
)->driveout_new_threads
&&
1546 !(*in
)->driveout_waiting_threads
&&
1547 !(*in
)->checked_out
)
1549 *out
= (*in
)->ike_sa
;
1550 charon
->bus
->set_sa(charon
->bus
, *out
);
1557 * Reset threads SA after enumeration
1559 static void reset_sa(void *data
)
1561 charon
->bus
->set_sa(charon
->bus
, NULL
);
1564 METHOD(ike_sa_manager_t
, create_enumerator
, enumerator_t
*,
1565 private_ike_sa_manager_t
* this, bool wait
)
1567 return enumerator_create_filter(create_table_enumerator(this),
1568 wait ?
(void*)enumerator_filter_wait
: (void*)enumerator_filter_skip
,
1572 METHOD(ike_sa_manager_t
, checkin
, void,
1573 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
1575 /* to check the SA back in, we look for the pointer of the ike_sa
1577 * The lookup is done by initiator SPI, so even if the SPI has changed (e.g.
1578 * on reception of a IKE_SA_INIT response) the lookup will work but
1579 * updating of the SPI MAY be necessary...
1582 ike_sa_id_t
*ike_sa_id
;
1584 identification_t
*my_id
, *other_id
;
1587 ike_sa_id
= ike_sa
->get_id(ike_sa
);
1588 my_id
= ike_sa
->get_my_id(ike_sa
);
1589 other_id
= ike_sa
->get_other_eap_id(ike_sa
);
1590 other
= ike_sa
->get_other_host(ike_sa
);
1592 DBG2(DBG_MGR
, "checkin IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1593 ike_sa
->get_unique_id(ike_sa
));
1595 /* look for the entry */
1596 if (get_entry_by_sa(this, ike_sa_id
, ike_sa
, &entry
, &segment
) == SUCCESS
)
1598 /* ike_sa_id must be updated */
1599 entry
->ike_sa_id
->replace_values(entry
->ike_sa_id
, ike_sa
->get_id(ike_sa
));
1600 /* signal waiting threads */
1601 entry
->checked_out
= NULL
;
1602 entry
->processing
= -1;
1603 /* check if this SA is half-open */
1604 if (entry
->half_open
&& ike_sa
->get_state(ike_sa
) != IKE_CONNECTING
)
1606 /* not half open anymore */
1607 entry
->half_open
= FALSE
;
1608 remove_half_open(this, entry
);
1610 else if (entry
->half_open
&& !other
->ip_equals(other
, entry
->other
))
1612 /* the other host's IP has changed, we must update the hash table */
1613 remove_half_open(this, entry
);
1614 DESTROY_IF(entry
->other
);
1615 entry
->other
= other
->clone(other
);
1616 put_half_open(this, entry
);
1618 else if (!entry
->half_open
&&
1619 ike_sa
->get_state(ike_sa
) == IKE_CONNECTING
)
1621 /* this is a new half-open SA */
1622 entry
->half_open
= TRUE
;
1623 entry
->other
= other
->clone(other
);
1624 put_half_open(this, entry
);
1626 DBG2(DBG_MGR
, "check-in of IKE_SA successful.");
1627 entry
->condvar
->signal(entry
->condvar
);
1631 entry
= entry_create();
1632 entry
->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
1633 entry
->ike_sa
= ike_sa
;
1634 if (ike_sa
->get_state(ike_sa
) == IKE_CONNECTING
)
1636 entry
->half_open
= TRUE
;
1637 entry
->other
= other
->clone(other
);
1638 put_half_open(this, entry
);
1640 segment
= put_entry(this, entry
);
1643 /* apply identities for duplicate test */
1644 if ((ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
||
1645 ike_sa
->get_state(ike_sa
) == IKE_PASSIVE
) &&
1646 entry
->my_id
== NULL
&& entry
->other_id
== NULL
)
1648 if (ike_sa
->get_version(ike_sa
) == IKEV1
)
1650 /* If authenticated and received INITIAL_CONTACT,
1651 * delete any existing IKE_SAs with that peer. */
1652 if (ike_sa
->has_condition(ike_sa
, COND_INIT_CONTACT_SEEN
))
1654 /* We can't hold the segment locked while checking the
1655 * uniqueness as this could lead to deadlocks. We mark the
1656 * entry as checked out while we release the lock so no other
1657 * thread can acquire it. Since it is not yet in the list of
1658 * connected peers that will not cause a deadlock as no other
1659 * caller of check_unqiueness() will try to check out this SA */
1660 entry
->checked_out
= thread_current();
1661 unlock_single_segment(this, segment
);
1663 this->public.check_uniqueness(&this->public, ike_sa
, TRUE
);
1664 ike_sa
->set_condition(ike_sa
, COND_INIT_CONTACT_SEEN
, FALSE
);
1666 /* The entry could have been modified in the mean time, e.g.
1667 * because another SA was added/removed next to it or another
1668 * thread is waiting, but it should still exist, so there is no
1669 * need for a lookup via get_entry_by... */
1670 lock_single_segment(this, segment
);
1671 entry
->checked_out
= NULL
;
1672 /* We already signaled waiting threads above, we have to do that
1673 * again after checking the SA out and back in again. */
1674 entry
->condvar
->signal(entry
->condvar
);
1678 entry
->my_id
= my_id
->clone(my_id
);
1679 entry
->other_id
= other_id
->clone(other_id
);
1682 entry
->other
= other
->clone(other
);
1684 put_connected_peers(this, entry
);
1687 unlock_single_segment(this, segment
);
1689 charon
->bus
->set_sa(charon
->bus
, NULL
);
1692 METHOD(ike_sa_manager_t
, checkin_and_destroy
, void,
1693 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
1695 /* deletion is a bit complex, we must ensure that no thread is waiting for
1697 * We take this SA from the table, and start signaling while threads
1698 * are in the condvar.
1701 ike_sa_id_t
*ike_sa_id
;
1704 ike_sa_id
= ike_sa
->get_id(ike_sa
);
1706 DBG2(DBG_MGR
, "checkin and destroy IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1707 ike_sa
->get_unique_id(ike_sa
));
1709 if (get_entry_by_sa(this, ike_sa_id
, ike_sa
, &entry
, &segment
) == SUCCESS
)
1711 if (entry
->driveout_waiting_threads
&& entry
->driveout_new_threads
)
1712 { /* it looks like flush() has been called and the SA is being deleted
1713 * anyway, just check it in */
1714 DBG2(DBG_MGR
, "ignored check-in and destroy of IKE_SA during shutdown");
1715 entry
->checked_out
= NULL
;
1716 entry
->condvar
->broadcast(entry
->condvar
);
1717 unlock_single_segment(this, segment
);
1721 /* drive out waiting threads, as we are in hurry */
1722 entry
->driveout_waiting_threads
= TRUE
;
1723 /* mark it, so no new threads can get this entry */
1724 entry
->driveout_new_threads
= TRUE
;
1725 /* wait until all workers have done their work */
1726 while (entry
->waiting_threads
)
1729 entry
->condvar
->broadcast(entry
->condvar
);
1730 /* they will wake us again when their work is done */
1731 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
1733 remove_entry(this, entry
);
1734 unlock_single_segment(this, segment
);
1736 if (entry
->half_open
)
1738 remove_half_open(this, entry
);
1740 if (entry
->my_id
&& entry
->other_id
)
1742 remove_connected_peers(this, entry
);
1744 if (entry
->init_hash
.ptr
)
1746 remove_init_hash(this, entry
->init_hash
);
1749 entry_destroy(entry
);
1751 DBG2(DBG_MGR
, "check-in and destroy of IKE_SA successful");
1755 DBG1(DBG_MGR
, "tried to check-in and delete nonexisting IKE_SA");
1756 ike_sa
->destroy(ike_sa
);
1758 charon
->bus
->set_sa(charon
->bus
, NULL
);
1762 * Cleanup function for create_id_enumerator
1764 static void id_enumerator_cleanup(linked_list_t
*ids
)
1766 ids
->destroy_offset(ids
, offsetof(ike_sa_id_t
, destroy
));
1769 METHOD(ike_sa_manager_t
, create_id_enumerator
, enumerator_t
*,
1770 private_ike_sa_manager_t
*this, identification_t
*me
,
1771 identification_t
*other
, int family
)
1776 linked_list_t
*ids
= NULL
;
1778 row
= chunk_hash_inc(other
->get_encoding(other
),
1779 chunk_hash(me
->get_encoding(me
))) & this->table_mask
;
1780 segment
= row
& this->segment_mask
;
1782 lock
= this->connected_peers_segments
[segment
].lock
;
1783 lock
->read_lock(lock
);
1784 item
= this->connected_peers_table
[row
];
1787 connected_peers_t
*current
= item
->value
;
1789 if (connected_peers_match(current
, me
, other
, family
))
1791 ids
= current
->sas
->clone_offset(current
->sas
,
1792 offsetof(ike_sa_id_t
, clone
));
1801 return enumerator_create_empty();
1803 return enumerator_create_cleaner(ids
->create_enumerator(ids
),
1804 (void*)id_enumerator_cleanup
, ids
);
1808 * Move all CHILD_SAs and virtual IPs from old to new
1810 static void adopt_children_and_vips(ike_sa_t
*old
, ike_sa_t
*new)
1812 enumerator_t
*enumerator
;
1813 child_sa_t
*child_sa
;
1815 int chcount
= 0, vipcount
= 0;
1817 charon
->bus
->children_migrate(charon
->bus
, new->get_id(new),
1818 new->get_unique_id(new));
1819 enumerator
= old
->create_child_sa_enumerator(old
);
1820 while (enumerator
->enumerate(enumerator
, &child_sa
))
1822 old
->remove_child_sa(old
, enumerator
);
1823 new->add_child_sa(new, child_sa
);
1826 enumerator
->destroy(enumerator
);
1828 enumerator
= old
->create_virtual_ip_enumerator(old
, FALSE
);
1829 while (enumerator
->enumerate(enumerator
, &vip
))
1831 new->add_virtual_ip(new, FALSE
, vip
);
1834 enumerator
->destroy(enumerator
);
1835 /* this does not release the addresses, which is good, but it does trigger
1836 * an assign_vips(FALSE) event... */
1837 old
->clear_virtual_ips(old
, FALSE
);
1838 /* ...trigger the analogous event on the new SA */
1839 charon
->bus
->set_sa(charon
->bus
, new);
1840 charon
->bus
->assign_vips(charon
->bus
, new, TRUE
);
1841 charon
->bus
->children_migrate(charon
->bus
, NULL
, 0);
1842 charon
->bus
->set_sa(charon
->bus
, old
);
1844 if (chcount
|| vipcount
)
1846 DBG1(DBG_IKE
, "detected reauth of existing IKE_SA, adopting %d "
1847 "children and %d virtual IPs", chcount
, vipcount
);
1852 * Delete an existing IKE_SA due to a unique replace policy
1854 static status_t
enforce_replace(private_ike_sa_manager_t
*this,
1855 ike_sa_t
*duplicate
, ike_sa_t
*new,
1856 identification_t
*other
, host_t
*host
)
1858 charon
->bus
->alert(charon
->bus
, ALERT_UNIQUE_REPLACE
);
1860 if (host
->equals(host
, duplicate
->get_other_host(duplicate
)))
1862 /* looks like a reauthentication attempt */
1863 if (!new->has_condition(new, COND_INIT_CONTACT_SEEN
) &&
1864 new->get_version(new) == IKEV1
)
1866 /* IKEv1 implicitly takes over children, IKEv2 recreates them
1868 adopt_children_and_vips(duplicate
, new);
1870 /* For IKEv1 we have to delay the delete for the old IKE_SA. Some
1871 * peers need to complete the new SA first, otherwise the quick modes
1872 * might get lost. For IKEv2 we do the same, as we want overlapping
1873 * CHILD_SAs to keep connectivity up. */
1874 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
1875 delete_ike_sa_job_create(duplicate
->get_id(duplicate
), TRUE
), 10);
1878 DBG1(DBG_IKE
, "deleting duplicate IKE_SA for peer '%Y' due to "
1879 "uniqueness policy", other
);
1880 return duplicate
->delete(duplicate
);
1883 METHOD(ike_sa_manager_t
, check_uniqueness
, bool,
1884 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
, bool force_replace
)
1886 bool cancel
= FALSE
;
1887 peer_cfg_t
*peer_cfg
;
1888 unique_policy_t policy
;
1889 enumerator_t
*enumerator
;
1890 ike_sa_id_t
*id
= NULL
;
1891 identification_t
*me
, *other
;
1894 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
1895 policy
= peer_cfg
->get_unique_policy(peer_cfg
);
1896 if (policy
== UNIQUE_NEVER
|| (policy
== UNIQUE_NO
&& !force_replace
))
1900 me
= ike_sa
->get_my_id(ike_sa
);
1901 other
= ike_sa
->get_other_eap_id(ike_sa
);
1902 other_host
= ike_sa
->get_other_host(ike_sa
);
1904 enumerator
= create_id_enumerator(this, me
, other
,
1905 other_host
->get_family(other_host
));
1906 while (enumerator
->enumerate(enumerator
, &id
))
1908 status_t status
= SUCCESS
;
1909 ike_sa_t
*duplicate
;
1911 duplicate
= checkout(this, id
);
1918 DBG1(DBG_IKE
, "destroying duplicate IKE_SA for peer '%Y', "
1919 "received INITIAL_CONTACT", other
);
1920 charon
->bus
->ike_updown(charon
->bus
, duplicate
, FALSE
);
1921 checkin_and_destroy(this, duplicate
);
1924 peer_cfg
= duplicate
->get_peer_cfg(duplicate
);
1925 if (peer_cfg
&& peer_cfg
->equals(peer_cfg
, ike_sa
->get_peer_cfg(ike_sa
)))
1927 switch (duplicate
->get_state(duplicate
))
1929 case IKE_ESTABLISHED
:
1933 case UNIQUE_REPLACE
:
1934 status
= enforce_replace(this, duplicate
, ike_sa
,
1938 /* potential reauthentication? */
1939 if (!other_host
->equals(other_host
,
1940 duplicate
->get_other_host(duplicate
)))
1943 /* we keep the first IKE_SA and delete all
1944 * other duplicates that might exist */
1945 policy
= UNIQUE_REPLACE
;
1956 if (status
== DESTROY_ME
)
1958 checkin_and_destroy(this, duplicate
);
1962 checkin(this, duplicate
);
1965 enumerator
->destroy(enumerator
);
1966 /* reset thread's current IKE_SA after checkin */
1967 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1971 METHOD(ike_sa_manager_t
, has_contact
, bool,
1972 private_ike_sa_manager_t
*this, identification_t
*me
,
1973 identification_t
*other
, int family
)
1980 row
= chunk_hash_inc(other
->get_encoding(other
),
1981 chunk_hash(me
->get_encoding(me
))) & this->table_mask
;
1982 segment
= row
& this->segment_mask
;
1983 lock
= this->connected_peers_segments
[segment
].lock
;
1984 lock
->read_lock(lock
);
1985 item
= this->connected_peers_table
[row
];
1988 if (connected_peers_match(item
->value
, me
, other
, family
))
2000 METHOD(ike_sa_manager_t
, get_count
, u_int
,
2001 private_ike_sa_manager_t
*this)
2003 u_int segment
, count
= 0;
2006 for (segment
= 0; segment
< this->segment_count
; segment
++)
2008 mutex
= this->segments
[segment
& this->segment_mask
].mutex
;
2010 count
+= this->segments
[segment
].count
;
2011 mutex
->unlock(mutex
);
2016 METHOD(ike_sa_manager_t
, get_half_open_count
, u_int
,
2017 private_ike_sa_manager_t
*this, host_t
*ip
, bool responder_only
)
2027 addr
= ip
->get_address(ip
);
2028 row
= chunk_hash(addr
) & this->table_mask
;
2029 segment
= row
& this->segment_mask
;
2030 lock
= this->half_open_segments
[segment
].lock
;
2031 lock
->read_lock(lock
);
2032 item
= this->half_open_table
[row
];
2035 half_open_t
*half_open
= item
->value
;
2037 if (chunk_equals(addr
, half_open
->other
))
2039 count
= responder_only ? half_open
->count_responder
2049 count
= responder_only ?
(u_int
)ref_cur(&this->half_open_count_responder
)
2050 : (u_int
)ref_cur(&this->half_open_count
);
2055 METHOD(ike_sa_manager_t
, set_spi_cb
, void,
2056 private_ike_sa_manager_t
*this, spi_cb_t callback
, void *data
)
2058 this->spi_lock
->write_lock(this->spi_lock
);
2059 this->spi_cb
.cb
= callback
;
2060 this->spi_cb
.data
= data
;
2061 this->spi_lock
->unlock(this->spi_lock
);
2064 METHOD(ike_sa_manager_t
, flush
, void,
2065 private_ike_sa_manager_t
*this)
2067 /* destroy all list entries */
2068 enumerator_t
*enumerator
;
2072 lock_all_segments(this);
2073 DBG2(DBG_MGR
, "going to destroy IKE_SA manager and all managed IKE_SA's");
2074 /* Step 1: drive out all waiting threads */
2075 DBG2(DBG_MGR
, "set driveout flags for all stored IKE_SA's");
2076 enumerator
= create_table_enumerator(this);
2077 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2079 /* do not accept new threads, drive out waiting threads */
2080 entry
->driveout_new_threads
= TRUE
;
2081 entry
->driveout_waiting_threads
= TRUE
;
2083 enumerator
->destroy(enumerator
);
2084 DBG2(DBG_MGR
, "wait for all threads to leave IKE_SA's");
2085 /* Step 2: wait until all are gone */
2086 enumerator
= create_table_enumerator(this);
2087 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2089 while (entry
->waiting_threads
|| entry
->checked_out
)
2092 entry
->condvar
->broadcast(entry
->condvar
);
2093 /* go sleeping until they are gone */
2094 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
2097 enumerator
->destroy(enumerator
);
2098 DBG2(DBG_MGR
, "delete all IKE_SA's");
2099 /* Step 3: initiate deletion of all IKE_SAs */
2100 enumerator
= create_table_enumerator(this);
2101 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2103 charon
->bus
->set_sa(charon
->bus
, entry
->ike_sa
);
2104 if (entry
->ike_sa
->get_version(entry
->ike_sa
) == IKEV2
)
2105 { /* as the delete never gets processed, fire down events */
2106 switch (entry
->ike_sa
->get_state(entry
->ike_sa
))
2108 case IKE_ESTABLISHED
:
2111 charon
->bus
->ike_updown(charon
->bus
, entry
->ike_sa
, FALSE
);
2117 entry
->ike_sa
->delete(entry
->ike_sa
);
2119 enumerator
->destroy(enumerator
);
2121 DBG2(DBG_MGR
, "destroy all entries");
2122 /* Step 4: destroy all entries */
2123 enumerator
= create_table_enumerator(this);
2124 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2126 charon
->bus
->set_sa(charon
->bus
, entry
->ike_sa
);
2127 if (entry
->half_open
)
2129 remove_half_open(this, entry
);
2131 if (entry
->my_id
&& entry
->other_id
)
2133 remove_connected_peers(this, entry
);
2135 if (entry
->init_hash
.ptr
)
2137 remove_init_hash(this, entry
->init_hash
);
2139 remove_entry_at((private_enumerator_t
*)enumerator
);
2140 entry_destroy(entry
);
2142 enumerator
->destroy(enumerator
);
2143 charon
->bus
->set_sa(charon
->bus
, NULL
);
2144 unlock_all_segments(this);
2146 this->spi_lock
->write_lock(this->spi_lock
);
2147 this->rng
->destroy(this->rng
);
2149 this->spi_cb
.cb
= NULL
;
2150 this->spi_cb
.data
= NULL
;
2151 this->spi_lock
->unlock(this->spi_lock
);
2154 METHOD(ike_sa_manager_t
, destroy
, void,
2155 private_ike_sa_manager_t
*this)
2159 /* these are already cleared in flush() above */
2160 free(this->ike_sa_table
);
2161 free(this->half_open_table
);
2162 free(this->connected_peers_table
);
2163 free(this->init_hashes_table
);
2164 for (i
= 0; i
< this->segment_count
; i
++)
2166 this->segments
[i
].mutex
->destroy(this->segments
[i
].mutex
);
2167 this->half_open_segments
[i
].lock
->destroy(this->half_open_segments
[i
].lock
);
2168 this->connected_peers_segments
[i
].lock
->destroy(this->connected_peers_segments
[i
].lock
);
2169 this->init_hashes_segments
[i
].mutex
->destroy(this->init_hashes_segments
[i
].mutex
);
2171 free(this->segments
);
2172 free(this->half_open_segments
);
2173 free(this->connected_peers_segments
);
2174 free(this->init_hashes_segments
);
2176 this->spi_lock
->destroy(this->spi_lock
);
2181 * This function returns the next-highest power of two for the given number.
2182 * The algorithm works by setting all bits on the right-hand side of the most
2183 * significant 1 to 1 and then increments the whole number so it rolls over
2184 * to the nearest power of two. Note: returns 0 for n == 0
2186 static u_int
get_nearest_powerof2(u_int n
)
2191 for (i
= 1; i
< sizeof(u_int
) * 8; i
<<= 1)
2199 * Described in header.
2201 ike_sa_manager_t
*ike_sa_manager_create()
2203 private_ike_sa_manager_t
*this;
2208 .checkout
= _checkout
,
2209 .checkout_new
= _checkout_new
,
2210 .checkout_by_message
= _checkout_by_message
,
2211 .checkout_by_config
= _checkout_by_config
,
2212 .checkout_by_id
= _checkout_by_id
,
2213 .checkout_by_name
= _checkout_by_name
,
2214 .check_uniqueness
= _check_uniqueness
,
2215 .has_contact
= _has_contact
,
2216 .create_enumerator
= _create_enumerator
,
2217 .create_id_enumerator
= _create_id_enumerator
,
2218 .checkin
= _checkin
,
2219 .checkin_and_destroy
= _checkin_and_destroy
,
2220 .get_count
= _get_count
,
2221 .get_half_open_count
= _get_half_open_count
,
2223 .set_spi_cb
= _set_spi_cb
,
2224 .destroy
= _destroy
,
2228 this->rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
2229 if (this->rng
== NULL
)
2231 DBG1(DBG_MGR
, "manager initialization failed, no RNG supported");
2235 this->spi_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
2237 this->ikesa_limit
= lib
->settings
->get_int(lib
->settings
,
2238 "%s.ikesa_limit", 0, lib
->ns
);
2240 this->table_size
= get_nearest_powerof2(lib
->settings
->get_int(
2241 lib
->settings
, "%s.ikesa_table_size",
2242 DEFAULT_HASHTABLE_SIZE
, lib
->ns
));
2243 this->table_size
= max(1, min(this->table_size
, MAX_HASHTABLE_SIZE
));
2244 this->table_mask
= this->table_size
- 1;
2246 this->segment_count
= get_nearest_powerof2(lib
->settings
->get_int(
2247 lib
->settings
, "%s.ikesa_table_segments",
2248 DEFAULT_SEGMENT_COUNT
, lib
->ns
));
2249 this->segment_count
= max(1, min(this->segment_count
, this->table_size
));
2250 this->segment_mask
= this->segment_count
- 1;
2252 this->ike_sa_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2253 this->segments
= (segment_t
*)calloc(this->segment_count
, sizeof(segment_t
));
2254 for (i
= 0; i
< this->segment_count
; i
++)
2256 this->segments
[i
].mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
);
2257 this->segments
[i
].count
= 0;
2260 /* we use the same table parameters for the table to track half-open SAs */
2261 this->half_open_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2262 this->half_open_segments
= calloc(this->segment_count
, sizeof(shareable_segment_t
));
2263 for (i
= 0; i
< this->segment_count
; i
++)
2265 this->half_open_segments
[i
].lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
2266 this->half_open_segments
[i
].count
= 0;
2269 /* also for the hash table used for duplicate tests */
2270 this->connected_peers_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2271 this->connected_peers_segments
= calloc(this->segment_count
, sizeof(shareable_segment_t
));
2272 for (i
= 0; i
< this->segment_count
; i
++)
2274 this->connected_peers_segments
[i
].lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
2275 this->connected_peers_segments
[i
].count
= 0;
2278 /* and again for the table of hashes of seen initial IKE messages */
2279 this->init_hashes_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2280 this->init_hashes_segments
= calloc(this->segment_count
, sizeof(segment_t
));
2281 for (i
= 0; i
< this->segment_count
; i
++)
2283 this->init_hashes_segments
[i
].mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
);
2284 this->init_hashes_segments
[i
].count
= 0;
2287 this->reuse_ikesa
= lib
->settings
->get_bool(lib
->settings
,
2288 "%s.reuse_ikesa", TRUE
, lib
->ns
);
2289 return &this->public;