2 * Copyright (C) 2005-2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 * Copyright (C) 2008-2015 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 <collections/linked_list.h>
30 #include <crypto/hashers/hasher.h>
31 #include <processing/jobs/delete_ike_sa_job.h>
33 /* the default size of the hash table (MUST be a power of 2) */
34 #define DEFAULT_HASHTABLE_SIZE 1
36 /* the maximum size of the hash table (MUST be a power of 2) */
37 #define MAX_HASHTABLE_SIZE (1 << 30)
39 /* the default number of segments (MUST be a power of 2) */
40 #define DEFAULT_SEGMENT_COUNT 1
42 typedef struct entry_t entry_t
;
45 * An entry in the linked list, contains IKE_SA, locking and lookup data.
50 * Number of threads waiting for this ike_sa_t object.
55 * Condvar where threads can wait until ike_sa_t object is free for use again.
60 * Is this ike_sa currently checked out?
65 * Does this SA drives out new threads?
67 bool driveout_new_threads
;
70 * Does this SA drives out waiting threads?
72 bool driveout_waiting_threads
;
75 * Identification of an IKE_SA (SPIs).
77 ike_sa_id_t
*ike_sa_id
;
80 * The contained ike_sa_t object.
85 * hash of the IKE_SA_INIT message, used to detect retransmissions
90 * remote host address, required for DoS detection and duplicate
91 * checking (host with same my_id and other_id is *not* considered
92 * a duplicate if the address family differs)
97 * As responder: Is this SA half-open?
102 * own identity, required for duplicate checking
104 identification_t
*my_id
;
107 * remote identity, required for duplicate checking
109 identification_t
*other_id
;
112 * message ID or hash of currently processing message, -1 if none
114 u_int32_t processing
;
118 * Implementation of entry_t.destroy.
120 static status_t
entry_destroy(entry_t
*this)
122 /* also destroy IKE SA */
123 this->ike_sa
->destroy(this->ike_sa
);
124 this->ike_sa_id
->destroy(this->ike_sa_id
);
125 chunk_free(&this->init_hash
);
126 DESTROY_IF(this->other
);
127 DESTROY_IF(this->my_id
);
128 DESTROY_IF(this->other_id
);
129 this->condvar
->destroy(this->condvar
);
135 * Creates a new entry for the ike_sa_t list.
137 static entry_t
*entry_create()
142 .condvar
= condvar_create(CONDVAR_TYPE_DEFAULT
),
150 * Function that matches entry_t objects by ike_sa_id_t.
152 static bool entry_match_by_id(entry_t
*entry
, ike_sa_id_t
*id
)
154 if (id
->equals(id
, entry
->ike_sa_id
))
158 if ((id
->get_responder_spi(id
) == 0 ||
159 entry
->ike_sa_id
->get_responder_spi(entry
->ike_sa_id
) == 0) &&
160 (id
->get_ike_version(id
) == IKEV1_MAJOR_VERSION
||
161 id
->is_initiator(id
) == entry
->ike_sa_id
->is_initiator(entry
->ike_sa_id
)) &&
162 id
->get_initiator_spi(id
) == entry
->ike_sa_id
->get_initiator_spi(entry
->ike_sa_id
))
164 /* this is TRUE for IKE_SAs that we initiated but have not yet received a response */
171 * Function that matches entry_t objects by ike_sa_t pointers.
173 static bool entry_match_by_sa(entry_t
*entry
, ike_sa_t
*ike_sa
)
175 return entry
->ike_sa
== ike_sa
;
179 * Hash function for ike_sa_id_t objects.
181 static u_int
ike_sa_id_hash(ike_sa_id_t
*ike_sa_id
)
183 /* IKEv2 does not mandate random SPIs (RFC 5996, 2.6), they just have to be
184 * locally unique, so we use our randomly allocated SPI whether we are
185 * initiator or responder to ensure a good distribution. The latter is not
186 * possible for IKEv1 as we don't know whether we are original initiator or
187 * not (based on the IKE header). But as RFC 2408, section 2.5.3 proposes
188 * SPIs (Cookies) to be allocated near random (we allocate them randomly
189 * anyway) it seems safe to always use the initiator SPI. */
190 if (ike_sa_id
->get_ike_version(ike_sa_id
) == IKEV1_MAJOR_VERSION
||
191 ike_sa_id
->is_initiator(ike_sa_id
))
193 return ike_sa_id
->get_initiator_spi(ike_sa_id
);
195 return ike_sa_id
->get_responder_spi(ike_sa_id
);
198 typedef struct half_open_t half_open_t
;
201 * Struct to manage half-open IKE_SAs per peer.
204 /** chunk of remote host address */
207 /** the number of half-open IKE_SAs with that host */
210 /** the number of half-open IKE_SAs we responded to with that host */
211 u_int count_responder
;
215 * Destroys a half_open_t object.
217 static void half_open_destroy(half_open_t
*this)
219 chunk_free(&this->other
);
223 typedef struct connected_peers_t connected_peers_t
;
225 struct connected_peers_t
{
227 identification_t
*my_id
;
229 /** remote identity */
230 identification_t
*other_id
;
232 /** ip address family of peer */
235 /** list of ike_sa_id_t objects of IKE_SAs between the two identities */
239 static void connected_peers_destroy(connected_peers_t
*this)
241 this->my_id
->destroy(this->my_id
);
242 this->other_id
->destroy(this->other_id
);
243 this->sas
->destroy(this->sas
);
248 * Function that matches connected_peers_t objects by the given ids.
250 static inline bool connected_peers_match(connected_peers_t
*connected_peers
,
251 identification_t
*my_id
, identification_t
*other_id
,
254 return my_id
->equals(my_id
, connected_peers
->my_id
) &&
255 other_id
->equals(other_id
, connected_peers
->other_id
) &&
256 (!family
|| family
== connected_peers
->family
);
259 typedef struct init_hash_t init_hash_t
;
262 /** hash of IKE_SA_INIT or initial phase1 message (data is not cloned) */
265 /** our SPI allocated for the IKE_SA based on this message */
269 typedef struct segment_t segment_t
;
272 * Struct to manage segments of the hash table.
275 /** mutex to access a segment exclusively */
278 /** the number of entries in this segment */
282 typedef struct shareable_segment_t shareable_segment_t
;
285 * Struct to manage segments of the "half-open" and "connected peers" hash tables.
287 struct shareable_segment_t
{
288 /** rwlock to access a segment non-/exclusively */
291 /** the number of entries in this segment - in case of the "half-open table"
292 * it's the sum of all half_open_t.count in a segment. */
296 typedef struct table_item_t table_item_t
;
299 * Instead of using linked_list_t for each bucket we store the data in our own
300 * list to save memory.
302 struct table_item_t
{
303 /** data of this item */
306 /** next item in the overflow list */
310 typedef struct private_ike_sa_manager_t private_ike_sa_manager_t
;
313 * Additional private members of ike_sa_manager_t.
315 struct private_ike_sa_manager_t
{
317 * Public interface of ike_sa_manager_t.
319 ike_sa_manager_t
public;
322 * Hash table with entries for the ike_sa_t objects.
324 table_item_t
**ike_sa_table
;
327 * The size of the hash table.
332 * Mask to map the hashes to table rows.
337 * Segments of the hash table.
342 * The number of segments.
347 * Mask to map a table row to a segment.
352 * Hash table with half_open_t objects.
354 table_item_t
**half_open_table
;
357 * Segments of the "half-open" hash table.
359 shareable_segment_t
*half_open_segments
;
362 * Total number of half-open IKE_SAs.
364 refcount_t half_open_count
;
367 * Total number of half-open IKE_SAs as responder.
369 refcount_t half_open_count_responder
;
372 * Hash table with connected_peers_t objects.
374 table_item_t
**connected_peers_table
;
377 * Segments of the "connected peers" hash table.
379 shareable_segment_t
*connected_peers_segments
;
382 * Hash table with init_hash_t objects.
384 table_item_t
**init_hashes_table
;
387 * Segments of the "hashes" hash table.
389 segment_t
*init_hashes_segments
;
392 * RNG to get random SPIs for our side
397 * Registered callback for IKE SPIs
405 * Lock to access the RNG instance and the callback
410 * reuse existing IKE_SAs in checkout_by_config
415 * Configured IKE_SA limit, if any
421 * Acquire a lock to access the segment of the table row with the given index.
422 * It also works with the segment index directly.
424 static inline void lock_single_segment(private_ike_sa_manager_t
*this,
427 mutex_t
*lock
= this->segments
[index
& this->segment_mask
].mutex
;
432 * Release the lock required to access the segment of the table row with the given index.
433 * It also works with the segment index directly.
435 static inline void unlock_single_segment(private_ike_sa_manager_t
*this,
438 mutex_t
*lock
= this->segments
[index
& this->segment_mask
].mutex
;
445 static void lock_all_segments(private_ike_sa_manager_t
*this)
449 for (i
= 0; i
< this->segment_count
; i
++)
451 this->segments
[i
].mutex
->lock(this->segments
[i
].mutex
);
456 * Unlock all segments
458 static void unlock_all_segments(private_ike_sa_manager_t
*this)
462 for (i
= 0; i
< this->segment_count
; i
++)
464 this->segments
[i
].mutex
->unlock(this->segments
[i
].mutex
);
468 typedef struct private_enumerator_t private_enumerator_t
;
471 * hash table enumerator implementation
473 struct private_enumerator_t
{
476 * implements enumerator interface
478 enumerator_t enumerator
;
481 * associated ike_sa_manager_t
483 private_ike_sa_manager_t
*manager
;
486 * current segment index
491 * currently enumerating entry
496 * current table row index
503 table_item_t
*current
;
506 * previous table item
511 METHOD(enumerator_t
, enumerate
, bool,
512 private_enumerator_t
*this, entry_t
**entry
, u_int
*segment
)
516 this->entry
->condvar
->signal(this->entry
->condvar
);
519 while (this->segment
< this->manager
->segment_count
)
521 while (this->row
< this->manager
->table_size
)
523 this->prev
= this->current
;
526 this->current
= this->current
->next
;
530 lock_single_segment(this->manager
, this->segment
);
531 this->current
= this->manager
->ike_sa_table
[this->row
];
535 *entry
= this->entry
= this->current
->value
;
536 *segment
= this->segment
;
539 unlock_single_segment(this->manager
, this->segment
);
540 this->row
+= this->manager
->segment_count
;
543 this->row
= this->segment
;
548 METHOD(enumerator_t
, enumerator_destroy
, void,
549 private_enumerator_t
*this)
553 this->entry
->condvar
->signal(this->entry
->condvar
);
557 unlock_single_segment(this->manager
, this->segment
);
563 * Creates an enumerator to enumerate the entries in the hash table.
565 static enumerator_t
* create_table_enumerator(private_ike_sa_manager_t
*this)
567 private_enumerator_t
*enumerator
;
571 .enumerate
= (void*)_enumerate
,
572 .destroy
= _enumerator_destroy
,
576 return &enumerator
->enumerator
;
580 * Put an entry into the hash table.
581 * Note: The caller has to unlock the returned segment.
583 static u_int
put_entry(private_ike_sa_manager_t
*this, entry_t
*entry
)
585 table_item_t
*current
, *item
;
592 row
= ike_sa_id_hash(entry
->ike_sa_id
) & this->table_mask
;
593 segment
= row
& this->segment_mask
;
595 lock_single_segment(this, segment
);
596 current
= this->ike_sa_table
[row
];
598 { /* insert at the front of current bucket */
599 item
->next
= current
;
601 this->ike_sa_table
[row
] = item
;
602 this->segments
[segment
].count
++;
607 * Remove an entry from the hash table.
608 * Note: The caller MUST have a lock on the segment of this entry.
610 static void remove_entry(private_ike_sa_manager_t
*this, entry_t
*entry
)
612 table_item_t
*item
, *prev
= NULL
;
615 row
= ike_sa_id_hash(entry
->ike_sa_id
) & this->table_mask
;
616 segment
= row
& this->segment_mask
;
617 item
= this->ike_sa_table
[row
];
620 if (item
->value
== entry
)
624 prev
->next
= item
->next
;
628 this->ike_sa_table
[row
] = item
->next
;
630 this->segments
[segment
].count
--;
640 * Remove the entry at the current enumerator position.
642 static void remove_entry_at(private_enumerator_t
*this)
647 table_item_t
*current
= this->current
;
649 this->manager
->segments
[this->segment
].count
--;
650 this->current
= this->prev
;
654 this->prev
->next
= current
->next
;
658 this->manager
->ike_sa_table
[this->row
] = current
->next
;
659 unlock_single_segment(this->manager
, this->segment
);
666 * Find an entry using the provided match function to compare the entries for
669 static status_t
get_entry_by_match_function(private_ike_sa_manager_t
*this,
670 ike_sa_id_t
*ike_sa_id
, entry_t
**entry
, u_int
*segment
,
671 linked_list_match_t match
, void *param
)
676 row
= ike_sa_id_hash(ike_sa_id
) & this->table_mask
;
677 seg
= row
& this->segment_mask
;
679 lock_single_segment(this, seg
);
680 item
= this->ike_sa_table
[row
];
683 if (match(item
->value
, param
))
685 *entry
= item
->value
;
687 /* the locked segment has to be unlocked by the caller */
692 unlock_single_segment(this, seg
);
697 * Find an entry by ike_sa_id_t.
698 * Note: On SUCCESS, the caller has to unlock the segment.
700 static status_t
get_entry_by_id(private_ike_sa_manager_t
*this,
701 ike_sa_id_t
*ike_sa_id
, entry_t
**entry
, u_int
*segment
)
703 return get_entry_by_match_function(this, ike_sa_id
, entry
, segment
,
704 (linked_list_match_t
)entry_match_by_id
, ike_sa_id
);
708 * Find an entry by IKE_SA pointer.
709 * Note: On SUCCESS, the caller has to unlock the segment.
711 static status_t
get_entry_by_sa(private_ike_sa_manager_t
*this,
712 ike_sa_id_t
*ike_sa_id
, ike_sa_t
*ike_sa
, entry_t
**entry
, u_int
*segment
)
714 return get_entry_by_match_function(this, ike_sa_id
, entry
, segment
,
715 (linked_list_match_t
)entry_match_by_sa
, ike_sa
);
719 * Wait until no other thread is using an IKE_SA, return FALSE if entry not
722 static bool wait_for_entry(private_ike_sa_manager_t
*this, entry_t
*entry
,
725 if (entry
->driveout_new_threads
)
727 /* we are not allowed to get this */
730 while (entry
->checked_out
&& !entry
->driveout_waiting_threads
)
732 /* so wait until we can get it for us.
733 * we register us as waiting. */
734 entry
->waiting_threads
++;
735 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
736 entry
->waiting_threads
--;
738 /* hm, a deletion request forbids us to get this SA, get next one */
739 if (entry
->driveout_waiting_threads
)
741 /* we must signal here, others may be waiting on it, too */
742 entry
->condvar
->signal(entry
->condvar
);
749 * Put a half-open SA into the hash table.
751 static void put_half_open(private_ike_sa_manager_t
*this, entry_t
*entry
)
757 half_open_t
*half_open
;
760 ike_id
= entry
->ike_sa_id
;
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 item
= this->half_open_table
[row
];
769 half_open
= item
->value
;
771 if (chunk_equals(addr
, half_open
->other
))
781 .other
= chunk_clone(addr
),
785 .next
= this->half_open_table
[row
],
787 this->half_open_table
[row
] = item
;
790 ref_get(&this->half_open_count
);
791 if (!ike_id
->is_initiator(ike_id
))
793 half_open
->count_responder
++;
794 ref_get(&this->half_open_count_responder
);
796 this->half_open_segments
[segment
].count
++;
801 * Remove a half-open SA from the hash table.
803 static void remove_half_open(private_ike_sa_manager_t
*this, entry_t
*entry
)
805 table_item_t
*item
, *prev
= NULL
;
811 ike_id
= entry
->ike_sa_id
;
812 addr
= entry
->other
->get_address(entry
->other
);
813 row
= chunk_hash(addr
) & this->table_mask
;
814 segment
= row
& this->segment_mask
;
815 lock
= this->half_open_segments
[segment
].lock
;
816 lock
->write_lock(lock
);
817 item
= this->half_open_table
[row
];
820 half_open_t
*half_open
= item
->value
;
822 if (chunk_equals(addr
, half_open
->other
))
824 if (!ike_id
->is_initiator(ike_id
))
826 half_open
->count_responder
--;
827 ignore_result(ref_put(&this->half_open_count_responder
));
829 ignore_result(ref_put(&this->half_open_count
));
830 if (--half_open
->count
== 0)
834 prev
->next
= item
->next
;
838 this->half_open_table
[row
] = item
->next
;
840 half_open_destroy(half_open
);
843 this->half_open_segments
[segment
].count
--;
853 * Put an SA between two peers into the hash table.
855 static void put_connected_peers(private_ike_sa_manager_t
*this, entry_t
*entry
)
860 connected_peers_t
*connected_peers
;
861 chunk_t my_id
, other_id
;
864 my_id
= entry
->my_id
->get_encoding(entry
->my_id
);
865 other_id
= entry
->other_id
->get_encoding(entry
->other_id
);
866 family
= entry
->other
->get_family(entry
->other
);
867 row
= chunk_hash_inc(other_id
, chunk_hash(my_id
)) & this->table_mask
;
868 segment
= row
& this->segment_mask
;
869 lock
= this->connected_peers_segments
[segment
].lock
;
870 lock
->write_lock(lock
);
871 item
= this->connected_peers_table
[row
];
874 connected_peers
= item
->value
;
876 if (connected_peers_match(connected_peers
, entry
->my_id
,
877 entry
->other_id
, family
))
879 if (connected_peers
->sas
->find_first(connected_peers
->sas
,
880 (linked_list_match_t
)entry
->ike_sa_id
->equals
,
881 NULL
, entry
->ike_sa_id
) == SUCCESS
)
893 INIT(connected_peers
,
894 .my_id
= entry
->my_id
->clone(entry
->my_id
),
895 .other_id
= entry
->other_id
->clone(entry
->other_id
),
897 .sas
= linked_list_create(),
900 .value
= connected_peers
,
901 .next
= this->connected_peers_table
[row
],
903 this->connected_peers_table
[row
] = item
;
905 connected_peers
->sas
->insert_last(connected_peers
->sas
,
906 entry
->ike_sa_id
->clone(entry
->ike_sa_id
));
907 this->connected_peers_segments
[segment
].count
++;
912 * Remove an SA between two peers from the hash table.
914 static void remove_connected_peers(private_ike_sa_manager_t
*this, entry_t
*entry
)
916 table_item_t
*item
, *prev
= NULL
;
919 chunk_t my_id
, other_id
;
922 my_id
= entry
->my_id
->get_encoding(entry
->my_id
);
923 other_id
= entry
->other_id
->get_encoding(entry
->other_id
);
924 family
= entry
->other
->get_family(entry
->other
);
926 row
= chunk_hash_inc(other_id
, chunk_hash(my_id
)) & this->table_mask
;
927 segment
= row
& this->segment_mask
;
929 lock
= this->connected_peers_segments
[segment
].lock
;
930 lock
->write_lock(lock
);
931 item
= this->connected_peers_table
[row
];
934 connected_peers_t
*current
= item
->value
;
936 if (connected_peers_match(current
, entry
->my_id
, entry
->other_id
,
939 enumerator_t
*enumerator
;
940 ike_sa_id_t
*ike_sa_id
;
942 enumerator
= current
->sas
->create_enumerator(current
->sas
);
943 while (enumerator
->enumerate(enumerator
, &ike_sa_id
))
945 if (ike_sa_id
->equals(ike_sa_id
, entry
->ike_sa_id
))
947 current
->sas
->remove_at(current
->sas
, enumerator
);
948 ike_sa_id
->destroy(ike_sa_id
);
949 this->connected_peers_segments
[segment
].count
--;
953 enumerator
->destroy(enumerator
);
954 if (current
->sas
->get_count(current
->sas
) == 0)
958 prev
->next
= item
->next
;
962 this->connected_peers_table
[row
] = item
->next
;
964 connected_peers_destroy(current
);
976 * Get a random SPI for new IKE_SAs
978 static u_int64_t
get_spi(private_ike_sa_manager_t
*this)
982 this->spi_lock
->read_lock(this->spi_lock
);
985 spi
= this->spi_cb
.cb(this->spi_cb
.data
);
987 else if (!this->rng
||
988 !this->rng
->get_bytes(this->rng
, sizeof(spi
), (u_int8_t
*)&spi
))
992 this->spi_lock
->unlock(this->spi_lock
);
997 * Calculate the hash of the initial IKE message. Memory for the hash is
998 * allocated on success.
1000 * @returns TRUE on success
1002 static bool get_init_hash(hasher_t
*hasher
, message_t
*message
, chunk_t
*hash
)
1006 if (message
->get_first_payload_type(message
) == PLV1_FRAGMENT
)
1007 { /* only hash the source IP, port and SPI for fragmented init messages */
1011 src
= message
->get_source(message
);
1012 if (!hasher
->allocate_hash(hasher
, src
->get_address(src
), NULL
))
1016 port
= src
->get_port(src
);
1017 if (!hasher
->allocate_hash(hasher
, chunk_from_thing(port
), NULL
))
1021 spi
= message
->get_initiator_spi(message
);
1022 return hasher
->allocate_hash(hasher
, chunk_from_thing(spi
), hash
);
1024 if (message
->get_exchange_type(message
) == ID_PROT
)
1025 { /* include the source for Main Mode as the hash will be the same if
1026 * SPIs are reused by two initiators that use the same proposal */
1027 src
= message
->get_source(message
);
1029 if (!hasher
->allocate_hash(hasher
, src
->get_address(src
), NULL
))
1034 return hasher
->allocate_hash(hasher
, message
->get_packet_data(message
), hash
);
1038 * Check if we already have created an IKE_SA based on the initial IKE message
1039 * with the given hash.
1040 * If not the hash is stored, the hash data is not(!) cloned.
1042 * Also, the local SPI is returned. In case of a retransmit this is already
1043 * stored together with the hash, otherwise it is newly allocated and should
1044 * be used to create the IKE_SA.
1046 * @returns ALREADY_DONE if the message with the given hash has been seen before
1047 * NOT_FOUND if the message hash was not found
1048 * FAILED if the SPI allocation failed
1050 static status_t
check_and_put_init_hash(private_ike_sa_manager_t
*this,
1051 chunk_t init_hash
, u_int64_t
*our_spi
)
1059 row
= chunk_hash(init_hash
) & this->table_mask
;
1060 segment
= row
& this->segment_mask
;
1061 mutex
= this->init_hashes_segments
[segment
].mutex
;
1063 item
= this->init_hashes_table
[row
];
1066 init_hash_t
*current
= item
->value
;
1068 if (chunk_equals(init_hash
, current
->hash
))
1070 *our_spi
= current
->our_spi
;
1071 mutex
->unlock(mutex
);
1072 return ALREADY_DONE
;
1077 spi
= get_spi(this);
1085 .len
= init_hash
.len
,
1086 .ptr
= init_hash
.ptr
,
1092 .next
= this->init_hashes_table
[row
],
1094 this->init_hashes_table
[row
] = item
;
1095 *our_spi
= init
->our_spi
;
1096 mutex
->unlock(mutex
);
1101 * Remove the hash of an initial IKE message from the cache.
1103 static void remove_init_hash(private_ike_sa_manager_t
*this, chunk_t init_hash
)
1105 table_item_t
*item
, *prev
= NULL
;
1109 row
= chunk_hash(init_hash
) & this->table_mask
;
1110 segment
= row
& this->segment_mask
;
1111 mutex
= this->init_hashes_segments
[segment
].mutex
;
1113 item
= this->init_hashes_table
[row
];
1116 init_hash_t
*current
= item
->value
;
1118 if (chunk_equals(init_hash
, current
->hash
))
1122 prev
->next
= item
->next
;
1126 this->init_hashes_table
[row
] = item
->next
;
1135 mutex
->unlock(mutex
);
1138 METHOD(ike_sa_manager_t
, checkout
, ike_sa_t
*,
1139 private_ike_sa_manager_t
*this, ike_sa_id_t
*ike_sa_id
)
1141 ike_sa_t
*ike_sa
= NULL
;
1145 DBG2(DBG_MGR
, "checkout IKE_SA");
1147 if (get_entry_by_id(this, ike_sa_id
, &entry
, &segment
) == SUCCESS
)
1149 if (wait_for_entry(this, entry
, segment
))
1151 entry
->checked_out
= TRUE
;
1152 ike_sa
= entry
->ike_sa
;
1153 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1154 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1156 unlock_single_segment(this, segment
);
1158 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1162 METHOD(ike_sa_manager_t
, checkout_new
, ike_sa_t
*,
1163 private_ike_sa_manager_t
* this, ike_version_t version
, bool initiator
)
1165 ike_sa_id_t
*ike_sa_id
;
1167 u_int8_t ike_version
;
1170 ike_version
= version
== IKEV1 ? IKEV1_MAJOR_VERSION
: IKEV2_MAJOR_VERSION
;
1172 spi
= get_spi(this);
1175 DBG1(DBG_MGR
, "failed to allocate SPI for new IKE_SA");
1181 ike_sa_id
= ike_sa_id_create(ike_version
, spi
, 0, TRUE
);
1185 ike_sa_id
= ike_sa_id_create(ike_version
, 0, spi
, FALSE
);
1187 ike_sa
= ike_sa_create(ike_sa_id
, initiator
, version
);
1188 ike_sa_id
->destroy(ike_sa_id
);
1192 DBG2(DBG_MGR
, "created IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1193 ike_sa
->get_unique_id(ike_sa
));
1199 * Get the message ID or message hash to detect early retransmissions
1201 static u_int32_t
get_message_id_or_hash(message_t
*message
)
1203 if (message
->get_major_version(message
) == IKEV1_MAJOR_VERSION
)
1205 /* Use a hash for IKEv1 Phase 1, where we don't have a MID, and Quick
1206 * Mode, where all three messages use the same message ID */
1207 if (message
->get_message_id(message
) == 0 ||
1208 message
->get_exchange_type(message
) == QUICK_MODE
)
1210 return chunk_hash(message
->get_packet_data(message
));
1213 return message
->get_message_id(message
);
1216 METHOD(ike_sa_manager_t
, checkout_by_message
, ike_sa_t
*,
1217 private_ike_sa_manager_t
* this, message_t
*message
)
1221 ike_sa_t
*ike_sa
= NULL
;
1223 ike_version_t ike_version
;
1224 bool is_init
= FALSE
;
1226 id
= message
->get_ike_sa_id(message
);
1227 /* clone the IKE_SA ID so we can modify the initiator flag */
1229 id
->switch_initiator(id
);
1231 DBG2(DBG_MGR
, "checkout IKE_SA by message");
1233 if (id
->get_responder_spi(id
) == 0 &&
1234 message
->get_message_id(message
) == 0)
1236 if (message
->get_major_version(message
) == IKEV2_MAJOR_VERSION
)
1238 if (message
->get_exchange_type(message
) == IKE_SA_INIT
&&
1239 message
->get_request(message
))
1241 ike_version
= IKEV2
;
1247 if (message
->get_exchange_type(message
) == ID_PROT
||
1248 message
->get_exchange_type(message
) == AGGRESSIVE
)
1250 ike_version
= IKEV1
;
1252 if (id
->is_initiator(id
))
1253 { /* not set in IKEv1, switch back before applying to new SA */
1254 id
->switch_initiator(id
);
1266 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1267 if (!hasher
|| !get_init_hash(hasher
, message
, &hash
))
1269 DBG1(DBG_MGR
, "ignoring message, failed to hash message");
1274 hasher
->destroy(hasher
);
1276 /* ensure this is not a retransmit of an already handled init message */
1277 switch (check_and_put_init_hash(this, hash
, &our_spi
))
1280 { /* we've not seen this packet yet, create a new IKE_SA */
1281 if (!this->ikesa_limit
||
1282 this->public.get_count(&this->public) < this->ikesa_limit
)
1284 id
->set_responder_spi(id
, our_spi
);
1285 ike_sa
= ike_sa_create(id
, FALSE
, ike_version
);
1288 entry
= entry_create();
1289 entry
->ike_sa
= ike_sa
;
1290 entry
->ike_sa_id
= id
;
1291 entry
->processing
= get_message_id_or_hash(message
);
1292 entry
->init_hash
= hash
;
1294 segment
= put_entry(this, entry
);
1295 entry
->checked_out
= TRUE
;
1296 unlock_single_segment(this, segment
);
1298 DBG2(DBG_MGR
, "created IKE_SA %s[%u]",
1299 ike_sa
->get_name(ike_sa
),
1300 ike_sa
->get_unique_id(ike_sa
));
1302 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1307 DBG1(DBG_MGR
, "creating IKE_SA failed, ignoring message");
1312 DBG1(DBG_MGR
, "ignoring %N, hitting IKE_SA limit (%u)",
1313 exchange_type_names
, message
->get_exchange_type(message
),
1316 remove_init_hash(this, hash
);
1322 { /* we failed to allocate an SPI */
1325 DBG1(DBG_MGR
, "ignoring message, failed to allocate SPI");
1332 /* it looks like we already handled this init message to some degree */
1333 id
->set_responder_spi(id
, our_spi
);
1337 if (get_entry_by_id(this, id
, &entry
, &segment
) == SUCCESS
)
1339 /* only check out if we are not already processing it. */
1340 if (entry
->processing
== get_message_id_or_hash(message
))
1342 DBG1(DBG_MGR
, "ignoring request with ID %u, already processing",
1345 else if (wait_for_entry(this, entry
, segment
))
1347 ike_sa_id_t
*ike_id
;
1349 ike_id
= entry
->ike_sa
->get_id(entry
->ike_sa
);
1350 entry
->checked_out
= TRUE
;
1351 if (message
->get_first_payload_type(message
) != PLV1_FRAGMENT
&&
1352 message
->get_first_payload_type(message
) != PLV2_FRAGMENT
)
1353 { /* TODO-FRAG: this fails if there are unencrypted payloads */
1354 entry
->processing
= get_message_id_or_hash(message
);
1356 if (ike_id
->get_responder_spi(ike_id
) == 0)
1358 ike_id
->set_responder_spi(ike_id
, id
->get_responder_spi(id
));
1360 ike_sa
= entry
->ike_sa
;
1361 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1362 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1364 unlock_single_segment(this, segment
);
1368 charon
->bus
->alert(charon
->bus
, ALERT_INVALID_IKE_SPI
, message
);
1371 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1375 METHOD(ike_sa_manager_t
, checkout_by_config
, ike_sa_t
*,
1376 private_ike_sa_manager_t
*this, peer_cfg_t
*peer_cfg
)
1378 enumerator_t
*enumerator
;
1380 ike_sa_t
*ike_sa
= NULL
;
1381 peer_cfg_t
*current_peer
;
1382 ike_cfg_t
*current_ike
;
1385 DBG2(DBG_MGR
, "checkout IKE_SA by config");
1387 if (!this->reuse_ikesa
&& peer_cfg
->get_ike_version(peer_cfg
) != IKEV1
)
1388 { /* IKE_SA reuse disabled by config (not possible for IKEv1) */
1389 ike_sa
= checkout_new(this, peer_cfg
->get_ike_version(peer_cfg
), TRUE
);
1390 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1394 enumerator
= create_table_enumerator(this);
1395 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1397 if (!wait_for_entry(this, entry
, segment
))
1401 if (entry
->ike_sa
->get_state(entry
->ike_sa
) == IKE_DELETING
)
1402 { /* skip IKE_SAs which are not usable, wake other waiting threads */
1403 entry
->condvar
->signal(entry
->condvar
);
1407 current_peer
= entry
->ike_sa
->get_peer_cfg(entry
->ike_sa
);
1408 if (current_peer
&& current_peer
->equals(current_peer
, peer_cfg
))
1410 current_ike
= current_peer
->get_ike_cfg(current_peer
);
1411 if (current_ike
->equals(current_ike
, peer_cfg
->get_ike_cfg(peer_cfg
)))
1413 entry
->checked_out
= TRUE
;
1414 ike_sa
= entry
->ike_sa
;
1415 DBG2(DBG_MGR
, "found existing IKE_SA %u with a '%s' config",
1416 ike_sa
->get_unique_id(ike_sa
),
1417 current_peer
->get_name(current_peer
));
1421 /* other threads might be waiting for this entry */
1422 entry
->condvar
->signal(entry
->condvar
);
1424 enumerator
->destroy(enumerator
);
1427 { /* no IKE_SA using such a config, hand out a new */
1428 ike_sa
= checkout_new(this, peer_cfg
->get_ike_version(peer_cfg
), TRUE
);
1430 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1434 METHOD(ike_sa_manager_t
, checkout_by_id
, ike_sa_t
*,
1435 private_ike_sa_manager_t
*this, u_int32_t id
)
1437 enumerator_t
*enumerator
;
1439 ike_sa_t
*ike_sa
= NULL
;
1442 DBG2(DBG_MGR
, "checkout IKE_SA by ID %u", id
);
1444 enumerator
= create_table_enumerator(this);
1445 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1447 if (wait_for_entry(this, entry
, segment
))
1449 if (entry
->ike_sa
->get_unique_id(entry
->ike_sa
) == id
)
1451 ike_sa
= entry
->ike_sa
;
1452 entry
->checked_out
= TRUE
;
1455 /* other threads might be waiting for this entry */
1456 entry
->condvar
->signal(entry
->condvar
);
1459 enumerator
->destroy(enumerator
);
1463 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1464 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1466 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1470 METHOD(ike_sa_manager_t
, checkout_by_name
, ike_sa_t
*,
1471 private_ike_sa_manager_t
*this, char *name
, bool child
)
1473 enumerator_t
*enumerator
, *children
;
1475 ike_sa_t
*ike_sa
= NULL
;
1476 child_sa_t
*child_sa
;
1479 enumerator
= create_table_enumerator(this);
1480 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
1482 if (wait_for_entry(this, entry
, segment
))
1484 /* look for a child with such a policy name ... */
1487 children
= entry
->ike_sa
->create_child_sa_enumerator(entry
->ike_sa
);
1488 while (children
->enumerate(children
, (void**)&child_sa
))
1490 if (streq(child_sa
->get_name(child_sa
), name
))
1492 ike_sa
= entry
->ike_sa
;
1496 children
->destroy(children
);
1498 else /* ... or for a IKE_SA with such a connection name */
1500 if (streq(entry
->ike_sa
->get_name(entry
->ike_sa
), name
))
1502 ike_sa
= entry
->ike_sa
;
1505 /* got one, return */
1508 entry
->checked_out
= TRUE
;
1509 DBG2(DBG_MGR
, "IKE_SA %s[%u] successfully checked out",
1510 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
));
1513 /* other threads might be waiting for this entry */
1514 entry
->condvar
->signal(entry
->condvar
);
1517 enumerator
->destroy(enumerator
);
1519 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1524 * enumerator filter function, waiting variant
1526 static bool enumerator_filter_wait(private_ike_sa_manager_t
*this,
1527 entry_t
**in
, ike_sa_t
**out
, u_int
*segment
)
1529 if (wait_for_entry(this, *in
, *segment
))
1531 *out
= (*in
)->ike_sa
;
1532 charon
->bus
->set_sa(charon
->bus
, *out
);
1539 * enumerator filter function, skipping variant
1541 static bool enumerator_filter_skip(private_ike_sa_manager_t
*this,
1542 entry_t
**in
, ike_sa_t
**out
, u_int
*segment
)
1544 if (!(*in
)->driveout_new_threads
&&
1545 !(*in
)->driveout_waiting_threads
&&
1546 !(*in
)->checked_out
)
1548 *out
= (*in
)->ike_sa
;
1549 charon
->bus
->set_sa(charon
->bus
, *out
);
1556 * Reset threads SA after enumeration
1558 static void reset_sa(void *data
)
1560 charon
->bus
->set_sa(charon
->bus
, NULL
);
1563 METHOD(ike_sa_manager_t
, create_enumerator
, enumerator_t
*,
1564 private_ike_sa_manager_t
* this, bool wait
)
1566 return enumerator_create_filter(create_table_enumerator(this),
1567 wait ?
(void*)enumerator_filter_wait
: (void*)enumerator_filter_skip
,
1571 METHOD(ike_sa_manager_t
, checkin
, void,
1572 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
1574 /* to check the SA back in, we look for the pointer of the ike_sa
1576 * The lookup is done by initiator SPI, so even if the SPI has changed (e.g.
1577 * on reception of a IKE_SA_INIT response) the lookup will work but
1578 * updating of the SPI MAY be necessary...
1581 ike_sa_id_t
*ike_sa_id
;
1583 identification_t
*my_id
, *other_id
;
1586 ike_sa_id
= ike_sa
->get_id(ike_sa
);
1587 my_id
= ike_sa
->get_my_id(ike_sa
);
1588 other_id
= ike_sa
->get_other_eap_id(ike_sa
);
1589 other
= ike_sa
->get_other_host(ike_sa
);
1591 DBG2(DBG_MGR
, "checkin IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1592 ike_sa
->get_unique_id(ike_sa
));
1594 /* look for the entry */
1595 if (get_entry_by_sa(this, ike_sa_id
, ike_sa
, &entry
, &segment
) == SUCCESS
)
1597 /* ike_sa_id must be updated */
1598 entry
->ike_sa_id
->replace_values(entry
->ike_sa_id
, ike_sa
->get_id(ike_sa
));
1599 /* signal waiting threads */
1600 entry
->checked_out
= FALSE
;
1601 entry
->processing
= -1;
1602 /* check if this SA is half-open */
1603 if (entry
->half_open
&& ike_sa
->get_state(ike_sa
) != IKE_CONNECTING
)
1605 /* not half open anymore */
1606 entry
->half_open
= FALSE
;
1607 remove_half_open(this, entry
);
1609 else if (entry
->half_open
&& !other
->ip_equals(other
, entry
->other
))
1611 /* the other host's IP has changed, we must update the hash table */
1612 remove_half_open(this, entry
);
1613 DESTROY_IF(entry
->other
);
1614 entry
->other
= other
->clone(other
);
1615 put_half_open(this, entry
);
1617 else if (!entry
->half_open
&&
1618 ike_sa
->get_state(ike_sa
) == IKE_CONNECTING
)
1620 /* this is a new half-open SA */
1621 entry
->half_open
= TRUE
;
1622 entry
->other
= other
->clone(other
);
1623 put_half_open(this, entry
);
1625 DBG2(DBG_MGR
, "check-in of IKE_SA successful.");
1626 entry
->condvar
->signal(entry
->condvar
);
1630 entry
= entry_create();
1631 entry
->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
1632 entry
->ike_sa
= ike_sa
;
1633 if (ike_sa
->get_state(ike_sa
) == IKE_CONNECTING
)
1635 entry
->half_open
= TRUE
;
1636 entry
->other
= other
->clone(other
);
1637 put_half_open(this, entry
);
1639 segment
= put_entry(this, entry
);
1642 /* apply identities for duplicate test */
1643 if ((ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
||
1644 ike_sa
->get_state(ike_sa
) == IKE_PASSIVE
) &&
1645 entry
->my_id
== NULL
&& entry
->other_id
== NULL
)
1647 if (ike_sa
->get_version(ike_sa
) == IKEV1
)
1649 /* If authenticated and received INITIAL_CONTACT,
1650 * delete any existing IKE_SAs with that peer. */
1651 if (ike_sa
->has_condition(ike_sa
, COND_INIT_CONTACT_SEEN
))
1653 /* We can't hold the segment locked while checking the
1654 * uniqueness as this could lead to deadlocks. We mark the
1655 * entry as checked out while we release the lock so no other
1656 * thread can acquire it. Since it is not yet in the list of
1657 * connected peers that will not cause a deadlock as no other
1658 * caller of check_unqiueness() will try to check out this SA */
1659 entry
->checked_out
= TRUE
;
1660 unlock_single_segment(this, segment
);
1662 this->public.check_uniqueness(&this->public, ike_sa
, TRUE
);
1663 ike_sa
->set_condition(ike_sa
, COND_INIT_CONTACT_SEEN
, FALSE
);
1665 /* The entry could have been modified in the mean time, e.g.
1666 * because another SA was added/removed next to it or another
1667 * thread is waiting, but it should still exist, so there is no
1668 * need for a lookup via get_entry_by... */
1669 lock_single_segment(this, segment
);
1670 entry
->checked_out
= FALSE
;
1671 /* We already signaled waiting threads above, we have to do that
1672 * again after checking the SA out and back in again. */
1673 entry
->condvar
->signal(entry
->condvar
);
1677 entry
->my_id
= my_id
->clone(my_id
);
1678 entry
->other_id
= other_id
->clone(other_id
);
1681 entry
->other
= other
->clone(other
);
1683 put_connected_peers(this, entry
);
1686 unlock_single_segment(this, segment
);
1688 charon
->bus
->set_sa(charon
->bus
, NULL
);
1691 METHOD(ike_sa_manager_t
, checkin_and_destroy
, void,
1692 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
)
1694 /* deletion is a bit complex, we must ensure that no thread is waiting for
1696 * We take this SA from the table, and start signaling while threads
1697 * are in the condvar.
1700 ike_sa_id_t
*ike_sa_id
;
1703 ike_sa_id
= ike_sa
->get_id(ike_sa
);
1705 DBG2(DBG_MGR
, "checkin and destroy IKE_SA %s[%u]", ike_sa
->get_name(ike_sa
),
1706 ike_sa
->get_unique_id(ike_sa
));
1708 if (get_entry_by_sa(this, ike_sa_id
, ike_sa
, &entry
, &segment
) == SUCCESS
)
1710 if (entry
->driveout_waiting_threads
&& entry
->driveout_new_threads
)
1711 { /* it looks like flush() has been called and the SA is being deleted
1712 * anyway, just check it in */
1713 DBG2(DBG_MGR
, "ignored check-in and destroy of IKE_SA during shutdown");
1714 entry
->checked_out
= FALSE
;
1715 entry
->condvar
->broadcast(entry
->condvar
);
1716 unlock_single_segment(this, segment
);
1720 /* drive out waiting threads, as we are in hurry */
1721 entry
->driveout_waiting_threads
= TRUE
;
1722 /* mark it, so no new threads can get this entry */
1723 entry
->driveout_new_threads
= TRUE
;
1724 /* wait until all workers have done their work */
1725 while (entry
->waiting_threads
)
1728 entry
->condvar
->broadcast(entry
->condvar
);
1729 /* they will wake us again when their work is done */
1730 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
1732 remove_entry(this, entry
);
1733 unlock_single_segment(this, segment
);
1735 if (entry
->half_open
)
1737 remove_half_open(this, entry
);
1739 if (entry
->my_id
&& entry
->other_id
)
1741 remove_connected_peers(this, entry
);
1743 if (entry
->init_hash
.ptr
)
1745 remove_init_hash(this, entry
->init_hash
);
1748 entry_destroy(entry
);
1750 DBG2(DBG_MGR
, "check-in and destroy of IKE_SA successful");
1754 DBG1(DBG_MGR
, "tried to check-in and delete nonexisting IKE_SA");
1755 ike_sa
->destroy(ike_sa
);
1757 charon
->bus
->set_sa(charon
->bus
, NULL
);
1761 * Cleanup function for create_id_enumerator
1763 static void id_enumerator_cleanup(linked_list_t
*ids
)
1765 ids
->destroy_offset(ids
, offsetof(ike_sa_id_t
, destroy
));
1768 METHOD(ike_sa_manager_t
, create_id_enumerator
, enumerator_t
*,
1769 private_ike_sa_manager_t
*this, identification_t
*me
,
1770 identification_t
*other
, int family
)
1775 linked_list_t
*ids
= NULL
;
1777 row
= chunk_hash_inc(other
->get_encoding(other
),
1778 chunk_hash(me
->get_encoding(me
))) & this->table_mask
;
1779 segment
= row
& this->segment_mask
;
1781 lock
= this->connected_peers_segments
[segment
].lock
;
1782 lock
->read_lock(lock
);
1783 item
= this->connected_peers_table
[row
];
1786 connected_peers_t
*current
= item
->value
;
1788 if (connected_peers_match(current
, me
, other
, family
))
1790 ids
= current
->sas
->clone_offset(current
->sas
,
1791 offsetof(ike_sa_id_t
, clone
));
1800 return enumerator_create_empty();
1802 return enumerator_create_cleaner(ids
->create_enumerator(ids
),
1803 (void*)id_enumerator_cleanup
, ids
);
1807 * Move all CHILD_SAs and virtual IPs from old to new
1809 static void adopt_children_and_vips(ike_sa_t
*old
, ike_sa_t
*new)
1811 enumerator_t
*enumerator
;
1812 child_sa_t
*child_sa
;
1814 int chcount
= 0, vipcount
= 0;
1816 charon
->bus
->children_migrate(charon
->bus
, new->get_id(new),
1817 new->get_unique_id(new));
1818 enumerator
= old
->create_child_sa_enumerator(old
);
1819 while (enumerator
->enumerate(enumerator
, &child_sa
))
1821 old
->remove_child_sa(old
, enumerator
);
1822 new->add_child_sa(new, child_sa
);
1825 enumerator
->destroy(enumerator
);
1827 enumerator
= old
->create_virtual_ip_enumerator(old
, FALSE
);
1828 while (enumerator
->enumerate(enumerator
, &vip
))
1830 new->add_virtual_ip(new, FALSE
, vip
);
1833 enumerator
->destroy(enumerator
);
1834 /* this does not release the addresses, which is good, but it does trigger
1835 * an assign_vips(FALSE) event... */
1836 old
->clear_virtual_ips(old
, FALSE
);
1837 /* ...trigger the analogous event on the new SA */
1838 charon
->bus
->set_sa(charon
->bus
, new);
1839 charon
->bus
->assign_vips(charon
->bus
, new, TRUE
);
1840 charon
->bus
->children_migrate(charon
->bus
, NULL
, 0);
1841 charon
->bus
->set_sa(charon
->bus
, old
);
1843 if (chcount
|| vipcount
)
1845 DBG1(DBG_IKE
, "detected reauth of existing IKE_SA, adopting %d "
1846 "children and %d virtual IPs", chcount
, vipcount
);
1851 * Delete an existing IKE_SA due to a unique replace policy
1853 static status_t
enforce_replace(private_ike_sa_manager_t
*this,
1854 ike_sa_t
*duplicate
, ike_sa_t
*new,
1855 identification_t
*other
, host_t
*host
)
1857 charon
->bus
->alert(charon
->bus
, ALERT_UNIQUE_REPLACE
);
1859 if (host
->equals(host
, duplicate
->get_other_host(duplicate
)))
1861 /* looks like a reauthentication attempt */
1862 if (!new->has_condition(new, COND_INIT_CONTACT_SEEN
) &&
1863 new->get_version(new) == IKEV1
)
1865 /* IKEv1 implicitly takes over children, IKEv2 recreates them
1867 adopt_children_and_vips(duplicate
, new);
1869 /* For IKEv1 we have to delay the delete for the old IKE_SA. Some
1870 * peers need to complete the new SA first, otherwise the quick modes
1871 * might get lost. For IKEv2 we do the same, as we want overlapping
1872 * CHILD_SAs to keep connectivity up. */
1873 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)
1874 delete_ike_sa_job_create(duplicate
->get_id(duplicate
), TRUE
), 10);
1877 DBG1(DBG_IKE
, "deleting duplicate IKE_SA for peer '%Y' due to "
1878 "uniqueness policy", other
);
1879 return duplicate
->delete(duplicate
);
1882 METHOD(ike_sa_manager_t
, check_uniqueness
, bool,
1883 private_ike_sa_manager_t
*this, ike_sa_t
*ike_sa
, bool force_replace
)
1885 bool cancel
= FALSE
;
1886 peer_cfg_t
*peer_cfg
;
1887 unique_policy_t policy
;
1888 enumerator_t
*enumerator
;
1889 ike_sa_id_t
*id
= NULL
;
1890 identification_t
*me
, *other
;
1893 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
1894 policy
= peer_cfg
->get_unique_policy(peer_cfg
);
1895 if (policy
== UNIQUE_NEVER
|| (policy
== UNIQUE_NO
&& !force_replace
))
1899 me
= ike_sa
->get_my_id(ike_sa
);
1900 other
= ike_sa
->get_other_eap_id(ike_sa
);
1901 other_host
= ike_sa
->get_other_host(ike_sa
);
1903 enumerator
= create_id_enumerator(this, me
, other
,
1904 other_host
->get_family(other_host
));
1905 while (enumerator
->enumerate(enumerator
, &id
))
1907 status_t status
= SUCCESS
;
1908 ike_sa_t
*duplicate
;
1910 duplicate
= checkout(this, id
);
1917 DBG1(DBG_IKE
, "destroying duplicate IKE_SA for peer '%Y', "
1918 "received INITIAL_CONTACT", other
);
1919 charon
->bus
->ike_updown(charon
->bus
, duplicate
, FALSE
);
1920 checkin_and_destroy(this, duplicate
);
1923 peer_cfg
= duplicate
->get_peer_cfg(duplicate
);
1924 if (peer_cfg
&& peer_cfg
->equals(peer_cfg
, ike_sa
->get_peer_cfg(ike_sa
)))
1926 switch (duplicate
->get_state(duplicate
))
1928 case IKE_ESTABLISHED
:
1932 case UNIQUE_REPLACE
:
1933 status
= enforce_replace(this, duplicate
, ike_sa
,
1937 /* potential reauthentication? */
1938 if (!other_host
->equals(other_host
,
1939 duplicate
->get_other_host(duplicate
)))
1942 /* we keep the first IKE_SA and delete all
1943 * other duplicates that might exist */
1944 policy
= UNIQUE_REPLACE
;
1955 if (status
== DESTROY_ME
)
1957 checkin_and_destroy(this, duplicate
);
1961 checkin(this, duplicate
);
1964 enumerator
->destroy(enumerator
);
1965 /* reset thread's current IKE_SA after checkin */
1966 charon
->bus
->set_sa(charon
->bus
, ike_sa
);
1970 METHOD(ike_sa_manager_t
, has_contact
, bool,
1971 private_ike_sa_manager_t
*this, identification_t
*me
,
1972 identification_t
*other
, int family
)
1979 row
= chunk_hash_inc(other
->get_encoding(other
),
1980 chunk_hash(me
->get_encoding(me
))) & this->table_mask
;
1981 segment
= row
& this->segment_mask
;
1982 lock
= this->connected_peers_segments
[segment
].lock
;
1983 lock
->read_lock(lock
);
1984 item
= this->connected_peers_table
[row
];
1987 if (connected_peers_match(item
->value
, me
, other
, family
))
1999 METHOD(ike_sa_manager_t
, get_count
, u_int
,
2000 private_ike_sa_manager_t
*this)
2002 u_int segment
, count
= 0;
2005 for (segment
= 0; segment
< this->segment_count
; segment
++)
2007 mutex
= this->segments
[segment
& this->segment_mask
].mutex
;
2009 count
+= this->segments
[segment
].count
;
2010 mutex
->unlock(mutex
);
2015 METHOD(ike_sa_manager_t
, get_half_open_count
, u_int
,
2016 private_ike_sa_manager_t
*this, host_t
*ip
, bool responder_only
)
2026 addr
= ip
->get_address(ip
);
2027 row
= chunk_hash(addr
) & this->table_mask
;
2028 segment
= row
& this->segment_mask
;
2029 lock
= this->half_open_segments
[segment
].lock
;
2030 lock
->read_lock(lock
);
2031 item
= this->half_open_table
[row
];
2034 half_open_t
*half_open
= item
->value
;
2036 if (chunk_equals(addr
, half_open
->other
))
2038 count
= responder_only ? half_open
->count_responder
2048 count
= responder_only ?
(u_int
)ref_cur(&this->half_open_count_responder
)
2049 : (u_int
)ref_cur(&this->half_open_count
);
2054 METHOD(ike_sa_manager_t
, set_spi_cb
, void,
2055 private_ike_sa_manager_t
*this, spi_cb_t callback
, void *data
)
2057 this->spi_lock
->write_lock(this->spi_lock
);
2058 this->spi_cb
.cb
= callback
;
2059 this->spi_cb
.data
= data
;
2060 this->spi_lock
->unlock(this->spi_lock
);
2063 METHOD(ike_sa_manager_t
, flush
, void,
2064 private_ike_sa_manager_t
*this)
2066 /* destroy all list entries */
2067 enumerator_t
*enumerator
;
2071 lock_all_segments(this);
2072 DBG2(DBG_MGR
, "going to destroy IKE_SA manager and all managed IKE_SA's");
2073 /* Step 1: drive out all waiting threads */
2074 DBG2(DBG_MGR
, "set driveout flags for all stored IKE_SA's");
2075 enumerator
= create_table_enumerator(this);
2076 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2078 /* do not accept new threads, drive out waiting threads */
2079 entry
->driveout_new_threads
= TRUE
;
2080 entry
->driveout_waiting_threads
= TRUE
;
2082 enumerator
->destroy(enumerator
);
2083 DBG2(DBG_MGR
, "wait for all threads to leave IKE_SA's");
2084 /* Step 2: wait until all are gone */
2085 enumerator
= create_table_enumerator(this);
2086 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2088 while (entry
->waiting_threads
|| entry
->checked_out
)
2091 entry
->condvar
->broadcast(entry
->condvar
);
2092 /* go sleeping until they are gone */
2093 entry
->condvar
->wait(entry
->condvar
, this->segments
[segment
].mutex
);
2096 enumerator
->destroy(enumerator
);
2097 DBG2(DBG_MGR
, "delete all IKE_SA's");
2098 /* Step 3: initiate deletion of all IKE_SAs */
2099 enumerator
= create_table_enumerator(this);
2100 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2102 charon
->bus
->set_sa(charon
->bus
, entry
->ike_sa
);
2103 if (entry
->ike_sa
->get_version(entry
->ike_sa
) == IKEV2
)
2104 { /* as the delete never gets processed, fire down events */
2105 switch (entry
->ike_sa
->get_state(entry
->ike_sa
))
2107 case IKE_ESTABLISHED
:
2110 charon
->bus
->ike_updown(charon
->bus
, entry
->ike_sa
, FALSE
);
2116 entry
->ike_sa
->delete(entry
->ike_sa
);
2118 enumerator
->destroy(enumerator
);
2120 DBG2(DBG_MGR
, "destroy all entries");
2121 /* Step 4: destroy all entries */
2122 enumerator
= create_table_enumerator(this);
2123 while (enumerator
->enumerate(enumerator
, &entry
, &segment
))
2125 charon
->bus
->set_sa(charon
->bus
, entry
->ike_sa
);
2126 if (entry
->half_open
)
2128 remove_half_open(this, entry
);
2130 if (entry
->my_id
&& entry
->other_id
)
2132 remove_connected_peers(this, entry
);
2134 if (entry
->init_hash
.ptr
)
2136 remove_init_hash(this, entry
->init_hash
);
2138 remove_entry_at((private_enumerator_t
*)enumerator
);
2139 entry_destroy(entry
);
2141 enumerator
->destroy(enumerator
);
2142 charon
->bus
->set_sa(charon
->bus
, NULL
);
2143 unlock_all_segments(this);
2145 this->spi_lock
->write_lock(this->spi_lock
);
2146 this->rng
->destroy(this->rng
);
2148 this->spi_cb
.cb
= NULL
;
2149 this->spi_cb
.data
= NULL
;
2150 this->spi_lock
->unlock(this->spi_lock
);
2153 METHOD(ike_sa_manager_t
, destroy
, void,
2154 private_ike_sa_manager_t
*this)
2158 /* these are already cleared in flush() above */
2159 free(this->ike_sa_table
);
2160 free(this->half_open_table
);
2161 free(this->connected_peers_table
);
2162 free(this->init_hashes_table
);
2163 for (i
= 0; i
< this->segment_count
; i
++)
2165 this->segments
[i
].mutex
->destroy(this->segments
[i
].mutex
);
2166 this->half_open_segments
[i
].lock
->destroy(this->half_open_segments
[i
].lock
);
2167 this->connected_peers_segments
[i
].lock
->destroy(this->connected_peers_segments
[i
].lock
);
2168 this->init_hashes_segments
[i
].mutex
->destroy(this->init_hashes_segments
[i
].mutex
);
2170 free(this->segments
);
2171 free(this->half_open_segments
);
2172 free(this->connected_peers_segments
);
2173 free(this->init_hashes_segments
);
2175 this->spi_lock
->destroy(this->spi_lock
);
2180 * This function returns the next-highest power of two for the given number.
2181 * The algorithm works by setting all bits on the right-hand side of the most
2182 * significant 1 to 1 and then increments the whole number so it rolls over
2183 * to the nearest power of two. Note: returns 0 for n == 0
2185 static u_int
get_nearest_powerof2(u_int n
)
2190 for (i
= 1; i
< sizeof(u_int
) * 8; i
<<= 1)
2198 * Described in header.
2200 ike_sa_manager_t
*ike_sa_manager_create()
2202 private_ike_sa_manager_t
*this;
2207 .checkout
= _checkout
,
2208 .checkout_new
= _checkout_new
,
2209 .checkout_by_message
= _checkout_by_message
,
2210 .checkout_by_config
= _checkout_by_config
,
2211 .checkout_by_id
= _checkout_by_id
,
2212 .checkout_by_name
= _checkout_by_name
,
2213 .check_uniqueness
= _check_uniqueness
,
2214 .has_contact
= _has_contact
,
2215 .create_enumerator
= _create_enumerator
,
2216 .create_id_enumerator
= _create_id_enumerator
,
2217 .checkin
= _checkin
,
2218 .checkin_and_destroy
= _checkin_and_destroy
,
2219 .get_count
= _get_count
,
2220 .get_half_open_count
= _get_half_open_count
,
2222 .set_spi_cb
= _set_spi_cb
,
2223 .destroy
= _destroy
,
2227 this->rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
2228 if (this->rng
== NULL
)
2230 DBG1(DBG_MGR
, "manager initialization failed, no RNG supported");
2234 this->spi_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
2236 this->ikesa_limit
= lib
->settings
->get_int(lib
->settings
,
2237 "%s.ikesa_limit", 0, lib
->ns
);
2239 this->table_size
= get_nearest_powerof2(lib
->settings
->get_int(
2240 lib
->settings
, "%s.ikesa_table_size",
2241 DEFAULT_HASHTABLE_SIZE
, lib
->ns
));
2242 this->table_size
= max(1, min(this->table_size
, MAX_HASHTABLE_SIZE
));
2243 this->table_mask
= this->table_size
- 1;
2245 this->segment_count
= get_nearest_powerof2(lib
->settings
->get_int(
2246 lib
->settings
, "%s.ikesa_table_segments",
2247 DEFAULT_SEGMENT_COUNT
, lib
->ns
));
2248 this->segment_count
= max(1, min(this->segment_count
, this->table_size
));
2249 this->segment_mask
= this->segment_count
- 1;
2251 this->ike_sa_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2252 this->segments
= (segment_t
*)calloc(this->segment_count
, sizeof(segment_t
));
2253 for (i
= 0; i
< this->segment_count
; i
++)
2255 this->segments
[i
].mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
);
2256 this->segments
[i
].count
= 0;
2259 /* we use the same table parameters for the table to track half-open SAs */
2260 this->half_open_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2261 this->half_open_segments
= calloc(this->segment_count
, sizeof(shareable_segment_t
));
2262 for (i
= 0; i
< this->segment_count
; i
++)
2264 this->half_open_segments
[i
].lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
2265 this->half_open_segments
[i
].count
= 0;
2268 /* also for the hash table used for duplicate tests */
2269 this->connected_peers_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2270 this->connected_peers_segments
= calloc(this->segment_count
, sizeof(shareable_segment_t
));
2271 for (i
= 0; i
< this->segment_count
; i
++)
2273 this->connected_peers_segments
[i
].lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
2274 this->connected_peers_segments
[i
].count
= 0;
2277 /* and again for the table of hashes of seen initial IKE messages */
2278 this->init_hashes_table
= calloc(this->table_size
, sizeof(table_item_t
*));
2279 this->init_hashes_segments
= calloc(this->segment_count
, sizeof(segment_t
));
2280 for (i
= 0; i
< this->segment_count
; i
++)
2282 this->init_hashes_segments
[i
].mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
);
2283 this->init_hashes_segments
[i
].count
= 0;
2286 this->reuse_ikesa
= lib
->settings
->get_bool(lib
->settings
,
2287 "%s.reuse_ikesa", TRUE
, lib
->ns
);
2288 return &this->public;