ike-rekey: Make sure to ignore task when detecting collisions if ike-init subtask...
[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 * TRUE if rekeying can't be handled temporarily
73 */
74 bool failed_temporarily;
75 };
76
77 /**
78 * Schedule a retry if rekeying temporary failed
79 */
80 static void schedule_delayed_rekey(private_ike_rekey_t *this)
81 {
82 uint32_t retry;
83 job_t *job;
84
85 retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
86 job = (job_t*)rekey_ike_sa_job_create(
87 this->ike_sa->get_id(this->ike_sa), FALSE);
88 DBG1(DBG_IKE, "IKE_SA rekeying failed, trying again in %d seconds", retry);
89 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
90 lib->scheduler->schedule_job(lib->scheduler, job, retry);
91 }
92
93 /**
94 * Check if an IKE_SA has any queued tasks, return initiation job
95 */
96 static job_t* check_queued_tasks(ike_sa_t *ike_sa)
97 {
98 enumerator_t *enumerator;
99 task_t *task;
100 job_t *job = NULL;
101
102 enumerator = ike_sa->create_task_enumerator(ike_sa, TASK_QUEUE_QUEUED);
103 if (enumerator->enumerate(enumerator, &task))
104 {
105 job = (job_t*)initiate_tasks_job_create(ike_sa->get_id(ike_sa));
106 }
107 enumerator->destroy(enumerator);
108 return job;
109 }
110
111 /**
112 * Establish the new replacement IKE_SA
113 */
114 static void establish_new(private_ike_rekey_t *this)
115 {
116 if (this->new_sa)
117 {
118 job_t *job;
119
120 this->new_sa->set_state(this->new_sa, IKE_ESTABLISHED);
121 DBG0(DBG_IKE, "IKE_SA %s[%d] rekeyed between %H[%Y]...%H[%Y]",
122 this->new_sa->get_name(this->new_sa),
123 this->new_sa->get_unique_id(this->new_sa),
124 this->ike_sa->get_my_host(this->ike_sa),
125 this->ike_sa->get_my_id(this->ike_sa),
126 this->ike_sa->get_other_host(this->ike_sa),
127 this->ike_sa->get_other_id(this->ike_sa));
128
129 this->new_sa->inherit_post(this->new_sa, this->ike_sa);
130 charon->bus->ike_rekey(charon->bus, this->ike_sa, this->new_sa);
131 job = check_queued_tasks(this->new_sa);
132 /* don't queue job before checkin(), as the IKE_SA is not yet
133 * registered at the manager */
134 charon->ike_sa_manager->checkin(charon->ike_sa_manager, this->new_sa);
135 if (job)
136 {
137 lib->processor->queue_job(lib->processor, job);
138 }
139 this->new_sa = NULL;
140 charon->bus->set_sa(charon->bus, this->ike_sa);
141
142 this->ike_sa->set_state(this->ike_sa, IKE_REKEYED);
143 }
144 }
145
146 METHOD(task_t, build_i_delete, status_t,
147 private_ike_rekey_t *this, message_t *message)
148 {
149 /* update exchange type to INFORMATIONAL for the delete */
150 message->set_exchange_type(message, INFORMATIONAL);
151
152 return this->ike_delete->task.build(&this->ike_delete->task, message);
153 }
154
155 METHOD(task_t, process_i_delete, status_t,
156 private_ike_rekey_t *this, message_t *message)
157 {
158 return this->ike_delete->task.process(&this->ike_delete->task, message);
159 }
160
161 METHOD(task_t, build_i, status_t,
162 private_ike_rekey_t *this, message_t *message)
163 {
164 ike_version_t version;
165
166 /* create new SA only on first try */
167 if (this->new_sa == NULL)
168 {
169 version = this->ike_sa->get_version(this->ike_sa);
170 this->new_sa = charon->ike_sa_manager->checkout_new(
171 charon->ike_sa_manager, version, TRUE);
172 if (!this->new_sa)
173 { /* shouldn't happen */
174 return FAILED;
175 }
176 this->new_sa->inherit_pre(this->new_sa, this->ike_sa);
177 this->ike_init = ike_init_create(this->new_sa, TRUE, this->ike_sa);
178 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
179 }
180 this->ike_init->task.build(&this->ike_init->task, message);
181
182 return NEED_MORE;
183 }
184
185 METHOD(task_t, process_r, status_t,
186 private_ike_rekey_t *this, message_t *message)
187 {
188 enumerator_t *enumerator;
189 child_sa_t *child_sa;
190
191 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
192 {
193 DBG1(DBG_IKE, "peer initiated rekeying, but we are deleting");
194 this->failed_temporarily = TRUE;
195 return NEED_MORE;
196 }
197
198 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
199 while (enumerator->enumerate(enumerator, (void**)&child_sa))
200 {
201 switch (child_sa->get_state(child_sa))
202 {
203 case CHILD_CREATED:
204 case CHILD_REKEYING:
205 case CHILD_RETRYING:
206 case CHILD_DELETING:
207 /* we do not allow rekeying while we have children in-progress */
208 DBG1(DBG_IKE, "peer initiated rekeying, but a child is half-open");
209 enumerator->destroy(enumerator);
210 this->failed_temporarily = TRUE;
211 return NEED_MORE;
212 default:
213 break;
214 }
215 }
216 enumerator->destroy(enumerator);
217
218 this->new_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
219 this->ike_sa->get_version(this->ike_sa), FALSE);
220 if (!this->new_sa)
221 { /* shouldn't happen */
222 return FAILED;
223 }
224 this->new_sa->inherit_pre(this->new_sa, this->ike_sa);
225 this->ike_init = ike_init_create(this->new_sa, FALSE, this->ike_sa);
226 this->ike_init->task.process(&this->ike_init->task, message);
227
228 return NEED_MORE;
229 }
230
231 METHOD(task_t, build_r, status_t,
232 private_ike_rekey_t *this, message_t *message)
233 {
234 if (this->failed_temporarily)
235 {
236 message->add_notify(message, TRUE, TEMPORARY_FAILURE, chunk_empty);
237 return SUCCESS;
238 }
239 if (this->new_sa == NULL)
240 {
241 /* IKE_SA/a CHILD_SA is in an inacceptable state, deny rekeying */
242 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
243 return SUCCESS;
244 }
245 if (this->ike_init->task.build(&this->ike_init->task, message) == FAILED)
246 {
247 this->ike_init->task.destroy(&this->ike_init->task);
248 this->ike_init = NULL;
249 charon->bus->set_sa(charon->bus, this->ike_sa);
250 return SUCCESS;
251 }
252 charon->bus->set_sa(charon->bus, this->ike_sa);
253
254 if (this->ike_sa->get_state(this->ike_sa) != IKE_REKEYING)
255 { /* in case of a collision we let the initiating task handle this */
256 establish_new(this);
257 /* make sure the IKE_SA is gone in case the peer fails to delete it */
258 lib->scheduler->schedule_job(lib->scheduler, (job_t*)
259 delete_ike_sa_job_create(this->ike_sa->get_id(this->ike_sa), TRUE),
260 90);
261 }
262 return SUCCESS;
263 }
264
265 /**
266 * Conclude any undetected rekey collision.
267 *
268 * If the peer does not detect the collision it will delete this IKE_SA.
269 * Depending on when our request reaches the peer and we receive the delete
270 * this may get called at different times.
271 *
272 * Returns TRUE if there was a collision, FALSE otherwise.
273 */
274 static bool conclude_undetected_collision(private_ike_rekey_t *this)
275 {
276 if (this->collision &&
277 this->collision->get_type(this->collision) == TASK_IKE_REKEY)
278 {
279 DBG1(DBG_IKE, "peer did not notice IKE_SA rekey collision, abort "
280 "active rekeying");
281 establish_new((private_ike_rekey_t*)this->collision);
282 return TRUE;
283 }
284 return FALSE;
285 }
286
287 METHOD(task_t, process_i, status_t,
288 private_ike_rekey_t *this, message_t *message)
289 {
290 if (message->get_notify(message, NO_ADDITIONAL_SAS))
291 {
292 DBG1(DBG_IKE, "peer seems to not support IKE rekeying, "
293 "starting reauthentication");
294 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
295 lib->processor->queue_job(lib->processor,
296 (job_t*)rekey_ike_sa_job_create(
297 this->ike_sa->get_id(this->ike_sa), TRUE));
298 return SUCCESS;
299 }
300
301 switch (this->ike_init->task.process(&this->ike_init->task, message))
302 {
303 case FAILED:
304 /* rekeying failed, fallback to old SA */
305 if (!conclude_undetected_collision(this))
306 {
307 schedule_delayed_rekey(this);
308 }
309 return SUCCESS;
310 case NEED_MORE:
311 /* bad dh group, try again */
312 this->ike_init->task.migrate(&this->ike_init->task, this->new_sa);
313 return NEED_MORE;
314 default:
315 break;
316 }
317
318 /* check for collisions */
319 if (this->collision &&
320 this->collision->get_type(this->collision) == TASK_IKE_REKEY)
321 {
322 private_ike_rekey_t *other = (private_ike_rekey_t*)this->collision;
323 host_t *host;
324 chunk_t this_nonce, other_nonce;
325
326 this_nonce = this->ike_init->get_lower_nonce(this->ike_init);
327 other_nonce = other->ike_init->get_lower_nonce(other->ike_init);
328
329 /* if we have the lower nonce, delete rekeyed SA. If not, delete
330 * the redundant. */
331 if (memcmp(this_nonce.ptr, other_nonce.ptr,
332 min(this_nonce.len, other_nonce.len)) < 0)
333 {
334 DBG1(DBG_IKE, "IKE_SA rekey collision lost, deleting redundant "
335 "IKE_SA %s[%d]", this->new_sa->get_name(this->new_sa),
336 this->new_sa->get_unique_id(this->new_sa));
337 /* apply host for a proper delete */
338 host = this->ike_sa->get_my_host(this->ike_sa);
339 this->new_sa->set_my_host(this->new_sa, host->clone(host));
340 host = this->ike_sa->get_other_host(this->ike_sa);
341 this->new_sa->set_other_host(this->new_sa, host->clone(host));
342 /* IKE_SAs in state IKE_REKEYED are silently deleted, so we use
343 * IKE_REKEYING */
344 this->new_sa->set_state(this->new_sa, IKE_REKEYING);
345 if (this->new_sa->delete(this->new_sa) == DESTROY_ME)
346 {
347 this->new_sa->destroy(this->new_sa);
348 }
349 else
350 {
351 charon->ike_sa_manager->checkin(charon->ike_sa_manager,
352 this->new_sa);
353 }
354 charon->bus->set_sa(charon->bus, this->ike_sa);
355 this->new_sa = NULL;
356 establish_new(other);
357 return SUCCESS;
358 }
359 /* peer should delete this SA. Add a timeout just in case. */
360 job_t *job = (job_t*)delete_ike_sa_job_create(
361 other->new_sa->get_id(other->new_sa), TRUE);
362 lib->scheduler->schedule_job(lib->scheduler, job,
363 HALF_OPEN_IKE_SA_TIMEOUT);
364 DBG1(DBG_IKE, "IKE_SA rekey collision won, waiting for delete for "
365 "redundant IKE_SA %s[%d]", other->new_sa->get_name(other->new_sa),
366 other->new_sa->get_unique_id(other->new_sa));
367 other->new_sa->set_state(other->new_sa, IKE_REKEYED);
368 charon->ike_sa_manager->checkin(charon->ike_sa_manager, other->new_sa);
369 other->new_sa = NULL;
370 charon->bus->set_sa(charon->bus, this->ike_sa);
371 }
372
373 establish_new(this);
374
375 /* rekeying successful, delete the IKE_SA using a subtask */
376 this->ike_delete = ike_delete_create(this->ike_sa, TRUE);
377 this->public.task.build = _build_i_delete;
378 this->public.task.process = _process_i_delete;
379
380 return NEED_MORE;
381 }
382
383 METHOD(task_t, get_type, task_type_t,
384 private_ike_rekey_t *this)
385 {
386 return TASK_IKE_REKEY;
387 }
388
389 METHOD(ike_rekey_t, did_collide, bool,
390 private_ike_rekey_t *this)
391 {
392 return this->collision &&
393 this->collision->get_type(this->collision) == TASK_IKE_REKEY;
394 }
395
396 METHOD(ike_rekey_t, collide, void,
397 private_ike_rekey_t* this, task_t *other)
398 {
399 DBG1(DBG_IKE, "detected %N collision with %N", task_type_names,
400 TASK_IKE_REKEY, task_type_names, other->get_type(other));
401
402 switch (other->get_type(other))
403 {
404 case TASK_IKE_DELETE:
405 conclude_undetected_collision(this);
406 other->destroy(other);
407 return;
408 case TASK_IKE_REKEY:
409 {
410 private_ike_rekey_t *rekey = (private_ike_rekey_t*)other;
411
412 if (!rekey->ike_init)
413 {
414 DBG1(DBG_IKE, "colliding exchange did not result in an IKE_SA, "
415 "ignore");
416 other->destroy(other);
417 return;
418 }
419 break;
420 }
421 default:
422 break;
423 }
424 DESTROY_IF(this->collision);
425 this->collision = other;
426 }
427
428 /**
429 * Cleanup the task
430 */
431 static void cleanup(private_ike_rekey_t *this)
432 {
433 ike_sa_t *cur_sa;
434
435 if (this->ike_init)
436 {
437 this->ike_init->task.destroy(&this->ike_init->task);
438 }
439 if (this->ike_delete)
440 {
441 this->ike_delete->task.destroy(&this->ike_delete->task);
442 }
443 cur_sa = charon->bus->get_sa(charon->bus);
444 DESTROY_IF(this->new_sa);
445 charon->bus->set_sa(charon->bus, cur_sa);
446 DESTROY_IF(this->collision);
447 }
448
449 METHOD(task_t, migrate, void,
450 private_ike_rekey_t *this, ike_sa_t *ike_sa)
451 {
452 cleanup(this);
453 this->collision = NULL;
454 this->ike_sa = ike_sa;
455 this->new_sa = NULL;
456 this->ike_init = NULL;
457 this->ike_delete = NULL;
458 }
459
460 METHOD(task_t, destroy, void,
461 private_ike_rekey_t *this)
462 {
463 cleanup(this);
464 free(this);
465 }
466
467 /*
468 * Described in header.
469 */
470 ike_rekey_t *ike_rekey_create(ike_sa_t *ike_sa, bool initiator)
471 {
472 private_ike_rekey_t *this;
473
474 INIT(this,
475 .public = {
476 .task = {
477 .get_type = _get_type,
478 .build = _build_r,
479 .process = _process_r,
480 .migrate = _migrate,
481 .destroy = _destroy,
482 },
483 .did_collide = _did_collide,
484 .collide = _collide,
485 },
486 .ike_sa = ike_sa,
487 .initiator = initiator,
488 );
489 if (initiator)
490 {
491 this->public.task.build = _build_i;
492 this->public.task.process = _process_i;
493 }
494
495 return &this->public;
496 }