9373e96431a76858fc6491afc332ad0298dc909b
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_rekey.c
1 /*
2 * Copyright (C) 2015-2016 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * HSR Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "ike_rekey.h"
19
20 #include <daemon.h>
21 #include <encoding/payloads/notify_payload.h>
22 #include <sa/ikev2/tasks/ike_init.h>
23 #include <sa/ikev2/tasks/ike_delete.h>
24 #include <processing/jobs/delete_ike_sa_job.h>
25 #include <processing/jobs/rekey_ike_sa_job.h>
26 #include <processing/jobs/initiate_tasks_job.h>
27
28
29 typedef struct private_ike_rekey_t private_ike_rekey_t;
30
31 /**
32 * Private members of a ike_rekey_t task.
33 */
34 struct private_ike_rekey_t {
35
36 /**
37 * Public methods and task_t interface.
38 */
39 ike_rekey_t public;
40
41 /**
42 * Assigned IKE_SA.
43 */
44 ike_sa_t *ike_sa;
45
46 /**
47 * New IKE_SA which replaces the current one
48 */
49 ike_sa_t *new_sa;
50
51 /**
52 * Are we the initiator?
53 */
54 bool initiator;
55
56 /**
57 * the TASK_IKE_INIT task which is reused to simplify rekeying
58 */
59 ike_init_t *ike_init;
60
61 /**
62 * IKE_DELETE task to delete the old IKE_SA after rekeying was successful
63 */
64 ike_delete_t *ike_delete;
65
66 /**
67 * colliding task detected by the task manager
68 */
69 task_t *collision;
70 };
71
72 /**
73 * Check if an IKE_SA has any queued tasks, return initiation job
74 */
75 static job_t* check_queued_tasks(ike_sa_t *ike_sa)
76 {
77 enumerator_t *enumerator;
78 task_t *task;
79 job_t *job = NULL;
80
81 enumerator = ike_sa->create_task_enumerator(ike_sa, TASK_QUEUE_QUEUED);
82 if (enumerator->enumerate(enumerator, &task))
83 {
84 job = (job_t*)initiate_tasks_job_create(ike_sa->get_id(ike_sa));
85 }
86 enumerator->destroy(enumerator);
87 return job;
88 }
89
90 /**
91 * Establish the new replacement IKE_SA
92 */
93 static void establish_new(private_ike_rekey_t *this)
94 {
95 if (this->new_sa)
96 {
97 job_t *job;
98
99 this->new_sa->set_state(this->new_sa, IKE_ESTABLISHED);
100 DBG0(DBG_IKE, "IKE_SA %s[%d] rekeyed between %H[%Y]...%H[%Y]",
101 this->new_sa->get_name(this->new_sa),
102 this->new_sa->get_unique_id(this->new_sa),
103 this->ike_sa->get_my_host(this->ike_sa),
104 this->ike_sa->get_my_id(this->ike_sa),
105 this->ike_sa->get_other_host(this->ike_sa),
106 this->ike_sa->get_other_id(this->ike_sa));
107
108 this->new_sa->inherit_post(this->new_sa, this->ike_sa);
109 charon->bus->ike_rekey(charon->bus, this->ike_sa, this->new_sa);
110 job = check_queued_tasks(this->new_sa);
111 /* don't queue job before checkin(), as the IKE_SA is not yet
112 * registered at the manager */
113 charon->ike_sa_manager->checkin(charon->ike_sa_manager, this->new_sa);
114 if (job)
115 {
116 lib->processor->queue_job(lib->processor, job);
117 }
118 this->new_sa = NULL;
119 charon->bus->set_sa(charon->bus, this->ike_sa);
120
121 this->ike_sa->set_state(this->ike_sa, IKE_REKEYED);
122 }
123 }
124
125 METHOD(task_t, build_i_delete, status_t,
126 private_ike_rekey_t *this, message_t *message)
127 {
128 /* update exchange type to INFORMATIONAL for the delete */
129 message->set_exchange_type(message, INFORMATIONAL);
130
131 return this->ike_delete->task.build(&this->ike_delete->task, message);
132 }
133
134 METHOD(task_t, process_i_delete, status_t,
135 private_ike_rekey_t *this, message_t *message)
136 {
137 return this->ike_delete->task.process(&this->ike_delete->task, message);
138 }
139
140 METHOD(task_t, build_i, status_t,
141 private_ike_rekey_t *this, message_t *message)
142 {
143 ike_version_t version;
144
145 /* create new SA only on first try */
146 if (this->new_sa == NULL)
147 {
148 version = this->ike_sa->get_version(this->ike_sa);
149 this->new_sa = charon->ike_sa_manager->checkout_new(
150 charon->ike_sa_manager, version, TRUE);
151 if (!this->new_sa)
152 { /* shouldn't happen */
153 return FAILED;
154 }
155 this->new_sa->inherit_pre(this->new_sa, this->ike_sa);
156 this->ike_init = ike_init_create(this->new_sa, TRUE, this->ike_sa);
157 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
158 }
159 this->ike_init->task.build(&this->ike_init->task, message);
160
161 return NEED_MORE;
162 }
163
164 METHOD(task_t, process_r, status_t,
165 private_ike_rekey_t *this, message_t *message)
166 {
167 enumerator_t *enumerator;
168 child_sa_t *child_sa;
169
170 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
171 {
172 DBG1(DBG_IKE, "peer initiated rekeying, but we are deleting");
173 return NEED_MORE;
174 }
175
176 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
177 while (enumerator->enumerate(enumerator, (void**)&child_sa))
178 {
179 switch (child_sa->get_state(child_sa))
180 {
181 case CHILD_CREATED:
182 case CHILD_REKEYING:
183 case CHILD_RETRYING:
184 case CHILD_DELETING:
185 /* we do not allow rekeying while we have children in-progress */
186 DBG1(DBG_IKE, "peer initiated rekeying, but a child is half-open");
187 enumerator->destroy(enumerator);
188 return NEED_MORE;
189 default:
190 break;
191 }
192 }
193 enumerator->destroy(enumerator);
194
195 this->new_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
196 this->ike_sa->get_version(this->ike_sa), FALSE);
197 if (!this->new_sa)
198 { /* shouldn't happen */
199 return FAILED;
200 }
201 this->new_sa->inherit_pre(this->new_sa, this->ike_sa);
202 this->ike_init = ike_init_create(this->new_sa, FALSE, this->ike_sa);
203 this->ike_init->task.process(&this->ike_init->task, message);
204
205 return NEED_MORE;
206 }
207
208 METHOD(task_t, build_r, status_t,
209 private_ike_rekey_t *this, message_t *message)
210 {
211 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
212 {
213 message->add_notify(message, TRUE, TEMPORARY_FAILURE, chunk_empty);
214 return SUCCESS;
215 }
216 if (this->new_sa == NULL)
217 {
218 /* IKE_SA/a CHILD_SA is in an inacceptable state, deny rekeying */
219 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
220 return SUCCESS;
221 }
222
223 if (this->ike_init->task.build(&this->ike_init->task, message) == FAILED)
224 {
225 charon->bus->set_sa(charon->bus, this->ike_sa);
226 return SUCCESS;
227 }
228 charon->bus->set_sa(charon->bus, this->ike_sa);
229
230 if (this->ike_sa->get_state(this->ike_sa) != IKE_REKEYING)
231 { /* in case of a collision we let the initiating task handle this */
232 establish_new(this);
233 /* make sure the IKE_SA is gone in case the peer fails to delete it */
234 lib->scheduler->schedule_job(lib->scheduler, (job_t*)
235 delete_ike_sa_job_create(this->ike_sa->get_id(this->ike_sa), TRUE),
236 90);
237 }
238 return SUCCESS;
239 }
240
241 METHOD(task_t, process_i, status_t,
242 private_ike_rekey_t *this, message_t *message)
243 {
244 if (message->get_notify(message, NO_ADDITIONAL_SAS))
245 {
246 DBG1(DBG_IKE, "peer seems to not support IKE rekeying, "
247 "starting reauthentication");
248 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
249 lib->processor->queue_job(lib->processor,
250 (job_t*)rekey_ike_sa_job_create(
251 this->ike_sa->get_id(this->ike_sa), TRUE));
252 return SUCCESS;
253 }
254
255 switch (this->ike_init->task.process(&this->ike_init->task, message))
256 {
257 case FAILED:
258 /* rekeying failed, fallback to old SA */
259 if (!(this->collision && (
260 this->collision->get_type(this->collision) == TASK_IKE_DELETE ||
261 this->collision->get_type(this->collision) == TASK_IKE_REAUTH)))
262 {
263 job_t *job;
264 uint32_t retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
265 job = (job_t*)rekey_ike_sa_job_create(
266 this->ike_sa->get_id(this->ike_sa), FALSE);
267 DBG1(DBG_IKE, "IKE_SA rekeying failed, "
268 "trying again in %d seconds", retry);
269 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
270 lib->scheduler->schedule_job(lib->scheduler, job, retry);
271 }
272 return SUCCESS;
273 case NEED_MORE:
274 /* bad dh group, try again */
275 this->ike_init->task.migrate(&this->ike_init->task, this->new_sa);
276 return NEED_MORE;
277 default:
278 break;
279 }
280
281 /* check for collisions */
282 if (this->collision &&
283 this->collision->get_type(this->collision) == TASK_IKE_REKEY)
284 {
285 private_ike_rekey_t *other = (private_ike_rekey_t*)this->collision;
286
287 /* ike_init can be NULL, if child_sa is half-open */
288 if (other->ike_init)
289 {
290 host_t *host;
291 chunk_t this_nonce, other_nonce;
292
293 this_nonce = this->ike_init->get_lower_nonce(this->ike_init);
294 other_nonce = other->ike_init->get_lower_nonce(other->ike_init);
295
296 /* if we have the lower nonce, delete rekeyed SA. If not, delete
297 * the redundant. */
298 if (memcmp(this_nonce.ptr, other_nonce.ptr,
299 min(this_nonce.len, other_nonce.len)) > 0)
300 {
301 /* peer should delete this SA. Add a timeout just in case. */
302 job_t *job = (job_t*)delete_ike_sa_job_create(
303 other->new_sa->get_id(other->new_sa), TRUE);
304 lib->scheduler->schedule_job(lib->scheduler, job,
305 HALF_OPEN_IKE_SA_TIMEOUT);
306 DBG1(DBG_IKE, "IKE_SA rekey collision won, waiting for delete "
307 "for redundant IKE_SA %s[%d]",
308 other->new_sa->get_name(other->new_sa),
309 other->new_sa->get_unique_id(other->new_sa));
310 other->new_sa->set_state(other->new_sa, IKE_REKEYED);
311 charon->ike_sa_manager->checkin(charon->ike_sa_manager,
312 other->new_sa);
313 other->new_sa = NULL;
314 }
315 else
316 {
317 DBG1(DBG_IKE, "IKE_SA rekey collision lost, deleting redundant "
318 "IKE_SA %s[%d]", this->new_sa->get_name(this->new_sa),
319 this->new_sa->get_unique_id(this->new_sa));
320 /* apply host for a proper delete */
321 host = this->ike_sa->get_my_host(this->ike_sa);
322 this->new_sa->set_my_host(this->new_sa, host->clone(host));
323 host = this->ike_sa->get_other_host(this->ike_sa);
324 this->new_sa->set_other_host(this->new_sa, host->clone(host));
325 /* IKE_SAs in state IKE_REKEYED are silently deleted, so we use
326 * IKE_REKEYING */
327 this->new_sa->set_state(this->new_sa, IKE_REKEYING);
328 if (this->new_sa->delete(this->new_sa) == DESTROY_ME)
329 {
330 this->new_sa->destroy(this->new_sa);
331 }
332 else
333 {
334 charon->ike_sa_manager->checkin(charon->ike_sa_manager,
335 this->new_sa);
336 }
337 charon->bus->set_sa(charon->bus, this->ike_sa);
338 this->new_sa = NULL;
339 establish_new(other);
340 return SUCCESS;
341 }
342 }
343 charon->bus->set_sa(charon->bus, this->ike_sa);
344 }
345
346 establish_new(this);
347
348 /* rekeying successful, delete the IKE_SA using a subtask */
349 this->ike_delete = ike_delete_create(this->ike_sa, TRUE);
350 this->public.task.build = _build_i_delete;
351 this->public.task.process = _process_i_delete;
352
353 return NEED_MORE;
354 }
355
356 METHOD(task_t, get_type, task_type_t,
357 private_ike_rekey_t *this)
358 {
359 return TASK_IKE_REKEY;
360 }
361
362 METHOD(ike_rekey_t, collide, void,
363 private_ike_rekey_t* this, task_t *other)
364 {
365 DBG1(DBG_IKE, "detected %N collision with %N", task_type_names,
366 TASK_IKE_REKEY, task_type_names, other->get_type(other));
367 DESTROY_IF(this->collision);
368 this->collision = other;
369 }
370
371 /**
372 * Cleanup the task
373 */
374 static void cleanup(private_ike_rekey_t *this)
375 {
376 ike_sa_t *cur_sa;
377
378 if (this->ike_init)
379 {
380 this->ike_init->task.destroy(&this->ike_init->task);
381 }
382 if (this->ike_delete)
383 {
384 this->ike_delete->task.destroy(&this->ike_delete->task);
385 }
386 cur_sa = charon->bus->get_sa(charon->bus);
387 DESTROY_IF(this->new_sa);
388 charon->bus->set_sa(charon->bus, cur_sa);
389 DESTROY_IF(this->collision);
390 }
391
392 METHOD(task_t, migrate, void,
393 private_ike_rekey_t *this, ike_sa_t *ike_sa)
394 {
395 cleanup(this);
396 this->collision = NULL;
397 this->ike_sa = ike_sa;
398 this->new_sa = NULL;
399 this->ike_init = NULL;
400 this->ike_delete = NULL;
401 }
402
403 METHOD(task_t, destroy, void,
404 private_ike_rekey_t *this)
405 {
406 cleanup(this);
407 free(this);
408 }
409
410 /*
411 * Described in header.
412 */
413 ike_rekey_t *ike_rekey_create(ike_sa_t *ike_sa, bool initiator)
414 {
415 private_ike_rekey_t *this;
416
417 INIT(this,
418 .public = {
419 .task = {
420 .get_type = _get_type,
421 .build = _build_r,
422 .process = _process_r,
423 .migrate = _migrate,
424 .destroy = _destroy,
425 },
426 .collide = _collide,
427 },
428 .ike_sa = ike_sa,
429 .initiator = initiator,
430 );
431 if (initiator)
432 {
433 this->public.task.build = _build_i;
434 this->public.task.process = _process_i;
435 }
436
437 return &this->public;
438 }