2 * Copyright (C) 2005-2008 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "ike_rekey.h"
20 #include <encoding/payloads/notify_payload.h>
21 #include <sa/ikev2/tasks/ike_init.h>
22 #include <sa/ikev2/tasks/ike_delete.h>
23 #include <processing/jobs/delete_ike_sa_job.h>
24 #include <processing/jobs/rekey_ike_sa_job.h>
27 typedef struct private_ike_rekey_t private_ike_rekey_t
;
30 * Private members of a ike_rekey_t task.
32 struct private_ike_rekey_t
{
35 * Public methods and task_t interface.
45 * New IKE_SA which replaces the current one
50 * Are we the initiator?
55 * the TASK_IKE_INIT task which is reused to simplify rekeying
60 * IKE_DELETE task to delete the old IKE_SA after rekeying was successful
62 ike_delete_t
*ike_delete
;
65 * colliding task detected by the task manager
71 * Establish the new replacement IKE_SA
73 static void establish_new(private_ike_rekey_t
*this)
77 this->new_sa
->set_state(this->new_sa
, IKE_ESTABLISHED
);
78 DBG0(DBG_IKE
, "IKE_SA %s[%d] rekeyed between %H[%Y]...%H[%Y]",
79 this->new_sa
->get_name(this->new_sa
),
80 this->new_sa
->get_unique_id(this->new_sa
),
81 this->ike_sa
->get_my_host(this->ike_sa
),
82 this->ike_sa
->get_my_id(this->ike_sa
),
83 this->ike_sa
->get_other_host(this->ike_sa
),
84 this->ike_sa
->get_other_id(this->ike_sa
));
86 this->new_sa
->inherit(this->new_sa
, this->ike_sa
);
87 charon
->bus
->ike_rekey(charon
->bus
, this->ike_sa
, this->new_sa
);
88 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, this->new_sa
);
90 /* set threads active IKE_SA after checkin */
91 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
95 METHOD(task_t
, process_r_delete
, status_t
,
96 private_ike_rekey_t
*this, message_t
*message
)
99 return this->ike_delete
->task
.process(&this->ike_delete
->task
, message
);
102 METHOD(task_t
, build_r_delete
, status_t
,
103 private_ike_rekey_t
*this, message_t
*message
)
105 return this->ike_delete
->task
.build(&this->ike_delete
->task
, message
);
108 METHOD(task_t
, build_i_delete
, status_t
,
109 private_ike_rekey_t
*this, message_t
*message
)
111 /* update exchange type to INFORMATIONAL for the delete */
112 message
->set_exchange_type(message
, INFORMATIONAL
);
114 return this->ike_delete
->task
.build(&this->ike_delete
->task
, message
);
117 METHOD(task_t
, process_i_delete
, status_t
,
118 private_ike_rekey_t
*this, message_t
*message
)
120 return this->ike_delete
->task
.process(&this->ike_delete
->task
, message
);
123 METHOD(task_t
, build_i
, status_t
,
124 private_ike_rekey_t
*this, message_t
*message
)
126 ike_version_t version
;
127 peer_cfg_t
*peer_cfg
;
130 /* create new SA only on first try */
131 if (this->new_sa
== NULL
)
133 version
= this->ike_sa
->get_version(this->ike_sa
);
134 this->new_sa
= charon
->ike_sa_manager
->checkout_new(
135 charon
->ike_sa_manager
, version
, TRUE
);
137 { /* shouldn't happen */
140 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
141 other_host
= this->ike_sa
->get_other_host(this->ike_sa
);
142 this->new_sa
->set_peer_cfg(this->new_sa
, peer_cfg
);
143 this->new_sa
->set_other_host(this->new_sa
, other_host
->clone(other_host
));
144 this->ike_init
= ike_init_create(this->new_sa
, TRUE
, this->ike_sa
);
145 this->ike_sa
->set_state(this->ike_sa
, IKE_REKEYING
);
147 this->ike_init
->task
.build(&this->ike_init
->task
, message
);
152 METHOD(task_t
, process_r
, status_t
,
153 private_ike_rekey_t
*this, message_t
*message
)
155 enumerator_t
*enumerator
;
156 peer_cfg_t
*peer_cfg
;
157 child_sa_t
*child_sa
;
159 if (this->ike_sa
->get_state(this->ike_sa
) == IKE_DELETING
)
161 DBG1(DBG_IKE
, "peer initiated rekeying, but we are deleting");
165 enumerator
= this->ike_sa
->create_child_sa_enumerator(this->ike_sa
);
166 while (enumerator
->enumerate(enumerator
, (void**)&child_sa
))
168 switch (child_sa
->get_state(child_sa
))
173 /* we do not allow rekeying while we have children in-progress */
174 DBG1(DBG_IKE
, "peer initiated rekeying, but a child is half-open");
175 enumerator
->destroy(enumerator
);
181 enumerator
->destroy(enumerator
);
183 this->new_sa
= charon
->ike_sa_manager
->checkout_new(charon
->ike_sa_manager
,
184 this->ike_sa
->get_version(this->ike_sa
), FALSE
);
186 { /* shouldn't happen */
190 peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
191 this->new_sa
->set_peer_cfg(this->new_sa
, peer_cfg
);
192 this->ike_init
= ike_init_create(this->new_sa
, FALSE
, this->ike_sa
);
193 this->ike_init
->task
.process(&this->ike_init
->task
, message
);
198 METHOD(task_t
, build_r
, status_t
,
199 private_ike_rekey_t
*this, message_t
*message
)
201 if (this->new_sa
== NULL
)
203 /* IKE_SA/a CHILD_SA is in an inacceptable state, deny rekeying */
204 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
208 if (this->ike_init
->task
.build(&this->ike_init
->task
, message
) == FAILED
)
213 this->ike_sa
->set_state(this->ike_sa
, IKE_REKEYING
);
215 /* rekeying successful, delete the IKE_SA using a subtask */
216 this->ike_delete
= ike_delete_create(this->ike_sa
, FALSE
);
217 this->public.task
.build
= _build_r_delete
;
218 this->public.task
.process
= _process_r_delete
;
223 METHOD(task_t
, process_i
, status_t
,
224 private_ike_rekey_t
*this, message_t
*message
)
226 if (message
->get_notify(message
, NO_ADDITIONAL_SAS
))
228 DBG1(DBG_IKE
, "peer seems to not support IKE rekeying, "
229 "starting reauthentication");
230 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
231 lib
->processor
->queue_job(lib
->processor
,
232 (job_t
*)rekey_ike_sa_job_create(
233 this->ike_sa
->get_id(this->ike_sa
), TRUE
));
237 switch (this->ike_init
->task
.process(&this->ike_init
->task
, message
))
240 /* rekeying failed, fallback to old SA */
241 if (!(this->collision
&& (
242 this->collision
->get_type(this->collision
) == TASK_IKE_DELETE
||
243 this->collision
->get_type(this->collision
) == TASK_IKE_REAUTH
)))
246 u_int32_t retry
= RETRY_INTERVAL
- (random() % RETRY_JITTER
);
247 job
= (job_t
*)rekey_ike_sa_job_create(
248 this->ike_sa
->get_id(this->ike_sa
), FALSE
);
249 DBG1(DBG_IKE
, "IKE_SA rekeying failed, "
250 "trying again in %d seconds", retry
);
251 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
252 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, retry
);
256 /* bad dh group, try again */
257 this->ike_init
->task
.migrate(&this->ike_init
->task
, this->new_sa
);
263 /* check for collisions */
264 if (this->collision
&&
265 this->collision
->get_type(this->collision
) == TASK_IKE_REKEY
)
267 private_ike_rekey_t
*other
= (private_ike_rekey_t
*)this->collision
;
269 /* ike_init can be NULL, if child_sa is half-open */
273 chunk_t this_nonce
, other_nonce
;
275 this_nonce
= this->ike_init
->get_lower_nonce(this->ike_init
);
276 other_nonce
= other
->ike_init
->get_lower_nonce(other
->ike_init
);
278 /* if we have the lower nonce, delete rekeyed SA. If not, delete
280 if (memcmp(this_nonce
.ptr
, other_nonce
.ptr
,
281 min(this_nonce
.len
, other_nonce
.len
)) > 0)
283 /* peer should delete this SA. Add a timeout just in case. */
284 job_t
*job
= (job_t
*)delete_ike_sa_job_create(
285 other
->new_sa
->get_id(other
->new_sa
), TRUE
);
286 lib
->scheduler
->schedule_job(lib
->scheduler
, job
, 10);
287 DBG1(DBG_IKE
, "IKE_SA rekey collision won, waiting for delete");
288 charon
->ike_sa_manager
->checkin(charon
->ike_sa_manager
, other
->new_sa
);
289 other
->new_sa
= NULL
;
293 DBG1(DBG_IKE
, "IKE_SA rekey collision lost, "
294 "deleting redundant IKE_SA");
295 /* apply host for a proper delete */
296 host
= this->ike_sa
->get_my_host(this->ike_sa
);
297 this->new_sa
->set_my_host(this->new_sa
, host
->clone(host
));
298 host
= this->ike_sa
->get_other_host(this->ike_sa
);
299 this->new_sa
->set_other_host(this->new_sa
, host
->clone(host
));
300 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
301 this->new_sa
->set_state(this->new_sa
, IKE_REKEYING
);
302 if (this->new_sa
->delete(this->new_sa
) == DESTROY_ME
)
304 this->new_sa
->destroy(this->new_sa
);
308 charon
->ike_sa_manager
->checkin(
309 charon
->ike_sa_manager
, this->new_sa
);
310 /* set threads active IKE_SA after checkin */
311 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
314 establish_new(other
);
318 /* set threads active IKE_SA after checkin */
319 charon
->bus
->set_sa(charon
->bus
, this->ike_sa
);
324 /* rekeying successful, delete the IKE_SA using a subtask */
325 this->ike_delete
= ike_delete_create(this->ike_sa
, TRUE
);
326 this->public.task
.build
= _build_i_delete
;
327 this->public.task
.process
= _process_i_delete
;
332 METHOD(task_t
, get_type
, task_type_t
,
333 private_ike_rekey_t
*this)
335 return TASK_IKE_REKEY
;
338 METHOD(ike_rekey_t
, collide
, void,
339 private_ike_rekey_t
* this, task_t
*other
)
341 DBG1(DBG_IKE
, "detected %N collision with %N", task_type_names
,
342 TASK_IKE_REKEY
, task_type_names
, other
->get_type(other
));
343 DESTROY_IF(this->collision
);
344 this->collision
= other
;
347 METHOD(task_t
, migrate
, void,
348 private_ike_rekey_t
*this, ike_sa_t
*ike_sa
)
352 this->ike_init
->task
.destroy(&this->ike_init
->task
);
354 if (this->ike_delete
)
356 this->ike_delete
->task
.destroy(&this->ike_delete
->task
);
358 DESTROY_IF(this->new_sa
);
359 DESTROY_IF(this->collision
);
361 this->collision
= NULL
;
362 this->ike_sa
= ike_sa
;
364 this->ike_init
= NULL
;
365 this->ike_delete
= NULL
;
368 METHOD(task_t
, destroy
, void,
369 private_ike_rekey_t
*this)
373 this->ike_init
->task
.destroy(&this->ike_init
->task
);
375 if (this->ike_delete
)
377 this->ike_delete
->task
.destroy(&this->ike_delete
->task
);
379 DESTROY_IF(this->new_sa
);
380 DESTROY_IF(this->collision
);
385 * Described in header.
387 ike_rekey_t
*ike_rekey_create(ike_sa_t
*ike_sa
, bool initiator
)
389 private_ike_rekey_t
*this;
394 .get_type
= _get_type
,
396 .process
= _process_r
,
403 .initiator
= initiator
,
407 this->public.task
.build
= _build_i
;
408 this->public.task
.process
= _process_i
;
411 return &this->public;