child-rekey: Don't change state to INSTALLED if it was already REKEYING
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / child_rekey.c
1 /*
2 * Copyright (C) 2005-2007 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include "child_rekey.h"
18
19 #include <daemon.h>
20 #include <encoding/payloads/notify_payload.h>
21 #include <sa/ikev2/tasks/child_create.h>
22 #include <sa/ikev2/tasks/child_delete.h>
23 #include <processing/jobs/rekey_child_sa_job.h>
24 #include <processing/jobs/rekey_ike_sa_job.h>
25
26
27 typedef struct private_child_rekey_t private_child_rekey_t;
28
29 /**
30 * Private members of a child_rekey_t task.
31 */
32 struct private_child_rekey_t {
33
34 /**
35 * Public methods and task_t interface.
36 */
37 child_rekey_t public;
38
39 /**
40 * Assigned IKE_SA.
41 */
42 ike_sa_t *ike_sa;
43
44 /**
45 * Are we the initiator?
46 */
47 bool initiator;
48
49 /**
50 * Protocol of CHILD_SA to rekey
51 */
52 protocol_id_t protocol;
53
54 /**
55 * Inbound SPI of CHILD_SA to rekey
56 */
57 uint32_t spi;
58
59 /**
60 * the CHILD_CREATE task which is reused to simplify rekeying
61 */
62 child_create_t *child_create;
63
64 /**
65 * the CHILD_DELETE task to delete rekeyed CHILD_SA
66 */
67 child_delete_t *child_delete;
68
69 /**
70 * CHILD_SA which gets rekeyed
71 */
72 child_sa_t *child_sa;
73
74 /**
75 * colliding task, may be delete or rekey
76 */
77 task_t *collision;
78
79 /**
80 * Indicate that peer destroyed the redundant child from collision.
81 * This happens if a peer's delete notification for the redundant
82 * child gets processed before the rekey job. If so, we must not
83 * touch the child created in the collision since it points to
84 * memory already freed.
85 */
86 bool other_child_destroyed;
87 };
88
89 /**
90 * Schedule a retry if rekeying temporary failed
91 */
92 static void schedule_delayed_rekey(private_child_rekey_t *this)
93 {
94 uint32_t retry;
95 job_t *job;
96
97 retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
98 job = (job_t*)rekey_child_sa_job_create(
99 this->child_sa->get_protocol(this->child_sa),
100 this->child_sa->get_spi(this->child_sa, TRUE),
101 this->ike_sa->get_my_host(this->ike_sa));
102 DBG1(DBG_IKE, "CHILD_SA rekeying failed, trying again in %d seconds", retry);
103 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
104 lib->scheduler->schedule_job(lib->scheduler, job, retry);
105 }
106
107 /**
108 * Implementation of task_t.build for initiator, after rekeying
109 */
110 static status_t build_i_delete(private_child_rekey_t *this, message_t *message)
111 {
112 /* update exchange type to INFORMATIONAL for the delete */
113 message->set_exchange_type(message, INFORMATIONAL);
114
115 return this->child_delete->task.build(&this->child_delete->task, message);
116 }
117
118 /**
119 * Implementation of task_t.process for initiator, after rekeying
120 */
121 static status_t process_i_delete(private_child_rekey_t *this, message_t *message)
122 {
123 return this->child_delete->task.process(&this->child_delete->task, message);
124 }
125
126 /**
127 * find a child using the REKEY_SA notify
128 */
129 static void find_child(private_child_rekey_t *this, message_t *message)
130 {
131 notify_payload_t *notify;
132 protocol_id_t protocol;
133 uint32_t spi;
134
135 notify = message->get_notify(message, REKEY_SA);
136 if (notify)
137 {
138 protocol = notify->get_protocol_id(notify);
139 spi = notify->get_spi(notify);
140
141 if (protocol == PROTO_ESP || protocol == PROTO_AH)
142 {
143 this->child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol,
144 spi, FALSE);
145 }
146 }
147 }
148
149 METHOD(task_t, build_i, status_t,
150 private_child_rekey_t *this, message_t *message)
151 {
152 notify_payload_t *notify;
153 uint32_t reqid;
154 child_cfg_t *config;
155
156 this->child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
157 this->spi, TRUE);
158 if (!this->child_sa)
159 { /* check if it is an outbound CHILD_SA */
160 this->child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
161 this->spi, FALSE);
162 if (!this->child_sa)
163 { /* CHILD_SA is gone, unable to rekey. As an empty CREATE_CHILD_SA
164 * exchange is invalid, we fall back to an INFORMATIONAL exchange.*/
165 message->set_exchange_type(message, INFORMATIONAL);
166 return SUCCESS;
167 }
168 /* we work only with the inbound SPI */
169 this->spi = this->child_sa->get_spi(this->child_sa, TRUE);
170 }
171 config = this->child_sa->get_config(this->child_sa);
172
173
174 /* our CHILD_CREATE task does the hard work for us */
175 if (!this->child_create)
176 {
177 this->child_create = child_create_create(this->ike_sa,
178 config->get_ref(config), TRUE, NULL, NULL);
179 }
180 reqid = this->child_sa->get_reqid(this->child_sa);
181 this->child_create->use_reqid(this->child_create, reqid);
182 this->child_create->use_marks(this->child_create,
183 this->child_sa->get_mark(this->child_sa, TRUE).value,
184 this->child_sa->get_mark(this->child_sa, FALSE).value);
185
186 if (this->child_create->task.build(&this->child_create->task,
187 message) != NEED_MORE)
188 {
189 schedule_delayed_rekey(this);
190 return FAILED;
191 }
192 if (message->get_exchange_type(message) == CREATE_CHILD_SA)
193 {
194 /* don't add the notify if the CHILD_CREATE task changed the exchange */
195 notify = notify_payload_create_from_protocol_and_type(PLV2_NOTIFY,
196 this->protocol, REKEY_SA);
197 notify->set_spi(notify, this->spi);
198 message->add_payload(message, (payload_t*)notify);
199 }
200 this->child_sa->set_state(this->child_sa, CHILD_REKEYING);
201
202 return NEED_MORE;
203 }
204
205 METHOD(task_t, process_r, status_t,
206 private_child_rekey_t *this, message_t *message)
207 {
208 /* let the CHILD_CREATE task process the message */
209 this->child_create->task.process(&this->child_create->task, message);
210
211 find_child(this, message);
212
213 return NEED_MORE;
214 }
215
216 METHOD(task_t, build_r, status_t,
217 private_child_rekey_t *this, message_t *message)
218 {
219 child_cfg_t *config;
220 uint32_t reqid;
221 child_sa_state_t state;
222
223 if (!this->child_sa)
224 {
225 DBG1(DBG_IKE, "unable to rekey, CHILD_SA not found");
226 message->add_notify(message, TRUE, CHILD_SA_NOT_FOUND, chunk_empty);
227 return SUCCESS;
228 }
229 if (this->child_sa->get_state(this->child_sa) == CHILD_DELETING)
230 {
231 DBG1(DBG_IKE, "unable to rekey, we are deleting the CHILD_SA");
232 message->add_notify(message, TRUE, TEMPORARY_FAILURE, chunk_empty);
233 return SUCCESS;
234 }
235
236 /* let the CHILD_CREATE task build the response */
237 reqid = this->child_sa->get_reqid(this->child_sa);
238 this->child_create->use_reqid(this->child_create, reqid);
239 this->child_create->use_marks(this->child_create,
240 this->child_sa->get_mark(this->child_sa, TRUE).value,
241 this->child_sa->get_mark(this->child_sa, FALSE).value);
242 config = this->child_sa->get_config(this->child_sa);
243 this->child_create->set_config(this->child_create, config->get_ref(config));
244 this->child_create->task.build(&this->child_create->task, message);
245
246 state = this->child_sa->get_state(this->child_sa);
247 this->child_sa->set_state(this->child_sa, CHILD_REKEYING);
248
249 if (message->get_payload(message, PLV2_SECURITY_ASSOCIATION) == NULL)
250 { /* rekeying failed, reuse old child */
251 this->child_sa->set_state(this->child_sa, state);
252 return SUCCESS;
253 }
254
255 this->child_sa->set_state(this->child_sa, CHILD_REKEYED);
256
257 /* invoke rekey hook */
258 charon->bus->child_rekey(charon->bus, this->child_sa,
259 this->child_create->get_child(this->child_create));
260 return SUCCESS;
261 }
262
263 /**
264 * Handle a rekey collision
265 */
266 static child_sa_t *handle_collision(private_child_rekey_t *this)
267 {
268 child_sa_t *to_delete;
269
270 if (this->collision->get_type(this->collision) == TASK_CHILD_REKEY)
271 {
272 chunk_t this_nonce, other_nonce;
273 private_child_rekey_t *other = (private_child_rekey_t*)this->collision;
274
275 this_nonce = this->child_create->get_lower_nonce(this->child_create);
276 other_nonce = other->child_create->get_lower_nonce(other->child_create);
277
278 /* if we have the lower nonce, delete rekeyed SA. If not, delete
279 * the redundant. */
280 if (memcmp(this_nonce.ptr, other_nonce.ptr,
281 min(this_nonce.len, other_nonce.len)) > 0)
282 {
283 child_sa_t *child_sa;
284
285 DBG1(DBG_IKE, "CHILD_SA rekey collision won, deleting old child");
286 to_delete = this->child_sa;
287 /* don't touch child other created, it has already been deleted */
288 if (!this->other_child_destroyed)
289 {
290 /* disable close action and updown event for redundant child */
291 child_sa = other->child_create->get_child(other->child_create);
292 if (child_sa)
293 {
294 child_sa->set_close_action(child_sa, ACTION_NONE);
295 if (child_sa->get_state(child_sa) != CHILD_REKEYED)
296 {
297 child_sa->set_state(child_sa, CHILD_REKEYED);
298 }
299 }
300 }
301 }
302 else
303 {
304 DBG1(DBG_IKE, "CHILD_SA rekey collision lost, "
305 "deleting rekeyed child");
306 to_delete = this->child_create->get_child(this->child_create);
307 }
308 }
309 else
310 { /* CHILD_DELETE */
311 child_delete_t *del = (child_delete_t*)this->collision;
312
313 /* we didn't had a chance to compare the nonces, so we delete
314 * the CHILD_SA the other is not deleting. */
315 if (del->get_child(del) != this->child_sa)
316 {
317 DBG1(DBG_IKE, "CHILD_SA rekey/delete collision, "
318 "deleting rekeyed child");
319 to_delete = this->child_sa;
320 }
321 else
322 {
323 DBG1(DBG_IKE, "CHILD_SA rekey/delete collision, "
324 "deleting redundant child");
325 to_delete = this->child_create->get_child(this->child_create);
326 }
327 }
328 return to_delete;
329 }
330
331 METHOD(task_t, process_i, status_t,
332 private_child_rekey_t *this, message_t *message)
333 {
334 protocol_id_t protocol;
335 uint32_t spi;
336 child_sa_t *to_delete;
337
338 if (message->get_notify(message, NO_ADDITIONAL_SAS))
339 {
340 DBG1(DBG_IKE, "peer seems to not support CHILD_SA rekeying, "
341 "starting reauthentication");
342 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
343 lib->processor->queue_job(lib->processor,
344 (job_t*)rekey_ike_sa_job_create(
345 this->ike_sa->get_id(this->ike_sa), TRUE));
346 return SUCCESS;
347 }
348 if (message->get_notify(message, CHILD_SA_NOT_FOUND))
349 {
350 child_cfg_t *child_cfg;
351 uint32_t reqid;
352
353 if (this->collision &&
354 this->collision->get_type(this->collision) == TASK_CHILD_DELETE)
355 { /* ignore this error if we already deleted the CHILD_SA on the
356 * peer's behalf (could happen if the other peer does not detect
357 * the collision and did not respond with TEMPORARY_FAILURE) */
358 return SUCCESS;
359 }
360 DBG1(DBG_IKE, "peer didn't find the CHILD_SA we tried to rekey");
361 /* FIXME: according to RFC 7296 we should only create a new CHILD_SA if
362 * it does not exist yet, we currently have no good way of checking for
363 * that (we could go by name, but that might be tricky e.g. due to
364 * narrowing) */
365 spi = this->child_sa->get_spi(this->child_sa, TRUE);
366 reqid = this->child_sa->get_reqid(this->child_sa);
367 protocol = this->child_sa->get_protocol(this->child_sa);
368 child_cfg = this->child_sa->get_config(this->child_sa);
369 child_cfg->get_ref(child_cfg);
370 charon->bus->child_updown(charon->bus, this->child_sa, FALSE);
371 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
372 return this->ike_sa->initiate(this->ike_sa,
373 child_cfg->get_ref(child_cfg), reqid,
374 NULL, NULL);
375 }
376
377 if (this->child_create->task.process(&this->child_create->task,
378 message) == NEED_MORE)
379 {
380 /* bad DH group while rekeying, retry, or failure requiring deletion */
381 return NEED_MORE;
382 }
383 if (message->get_payload(message, PLV2_SECURITY_ASSOCIATION) == NULL)
384 {
385 /* establishing new child failed, reuse old and try again. but not when
386 * we received a delete in the meantime */
387 if (!this->collision ||
388 this->collision->get_type(this->collision) != TASK_CHILD_DELETE)
389 {
390 schedule_delayed_rekey(this);
391 }
392 return SUCCESS;
393 }
394
395 /* check for rekey collisions */
396 if (this->collision)
397 {
398 to_delete = handle_collision(this);
399 }
400 else
401 {
402 to_delete = this->child_sa;
403 }
404
405 if (to_delete != this->child_create->get_child(this->child_create))
406 { /* invoke rekey hook if rekeying successful */
407 charon->bus->child_rekey(charon->bus, this->child_sa,
408 this->child_create->get_child(this->child_create));
409 }
410
411 if (to_delete == NULL)
412 {
413 return SUCCESS;
414 }
415 /* disable updown event for redundant CHILD_SA */
416 if (to_delete->get_state(to_delete) != CHILD_REKEYED)
417 {
418 to_delete->set_state(to_delete, CHILD_REKEYED);
419 }
420 spi = to_delete->get_spi(to_delete, TRUE);
421 protocol = to_delete->get_protocol(to_delete);
422
423 /* rekeying done, delete the obsolete CHILD_SA using a subtask */
424 this->child_delete = child_delete_create(this->ike_sa, protocol, spi, FALSE);
425 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i_delete;
426 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i_delete;
427
428 return NEED_MORE;
429 }
430
431 METHOD(task_t, get_type, task_type_t,
432 private_child_rekey_t *this)
433 {
434 return TASK_CHILD_REKEY;
435 }
436
437 METHOD(child_rekey_t, collide, void,
438 private_child_rekey_t *this, task_t *other)
439 {
440 /* the task manager only detects exchange collision, but not if
441 * the collision is for the same child. we check it here. */
442 if (other->get_type(other) == TASK_CHILD_REKEY)
443 {
444 private_child_rekey_t *rekey = (private_child_rekey_t*)other;
445 if (rekey->child_sa != this->child_sa)
446 {
447 /* not the same child => no collision */
448 other->destroy(other);
449 return;
450 }
451 }
452 else if (other->get_type(other) == TASK_CHILD_DELETE)
453 {
454 child_delete_t *del = (child_delete_t*)other;
455 if (this->collision &&
456 this->collision->get_type(this->collision) == TASK_CHILD_REKEY)
457 {
458 private_child_rekey_t *rekey;
459
460 rekey = (private_child_rekey_t*)this->collision;
461 if (del->get_child(del) == rekey->child_create->get_child(rekey->child_create))
462 {
463 /* peer deletes redundant child created in collision */
464 this->other_child_destroyed = TRUE;
465 other->destroy(other);
466 return;
467 }
468 }
469 if (del->get_child(del) != this->child_sa)
470 {
471 /* not the same child => no collision */
472 other->destroy(other);
473 return;
474 }
475 }
476 else
477 {
478 /* any other task is not critical for collisisions, ignore */
479 other->destroy(other);
480 return;
481 }
482 DBG1(DBG_IKE, "detected %N collision with %N", task_type_names,
483 TASK_CHILD_REKEY, task_type_names, other->get_type(other));
484 DESTROY_IF(this->collision);
485 this->collision = other;
486 }
487
488 METHOD(task_t, migrate, void,
489 private_child_rekey_t *this, ike_sa_t *ike_sa)
490 {
491 if (this->child_create)
492 {
493 this->child_create->task.migrate(&this->child_create->task, ike_sa);
494 }
495 if (this->child_delete)
496 {
497 this->child_delete->task.migrate(&this->child_delete->task, ike_sa);
498 }
499 DESTROY_IF(this->collision);
500
501 this->ike_sa = ike_sa;
502 this->collision = NULL;
503 }
504
505 METHOD(task_t, destroy, void,
506 private_child_rekey_t *this)
507 {
508 if (this->child_create)
509 {
510 this->child_create->task.destroy(&this->child_create->task);
511 }
512 if (this->child_delete)
513 {
514 this->child_delete->task.destroy(&this->child_delete->task);
515 }
516 DESTROY_IF(this->collision);
517 free(this);
518 }
519
520 /*
521 * Described in header.
522 */
523 child_rekey_t *child_rekey_create(ike_sa_t *ike_sa, protocol_id_t protocol,
524 uint32_t spi)
525 {
526 private_child_rekey_t *this;
527
528 INIT(this,
529 .public = {
530 .task = {
531 .get_type = _get_type,
532 .migrate = _migrate,
533 .destroy = _destroy,
534 },
535 .collide = _collide,
536 },
537 .ike_sa = ike_sa,
538 .protocol = protocol,
539 .spi = spi,
540 );
541
542 if (protocol != PROTO_NONE)
543 {
544 this->public.task.build = _build_i;
545 this->public.task.process = _process_i;
546 this->initiator = TRUE;
547 this->child_create = NULL;
548 }
549 else
550 {
551 this->public.task.build = _build_r;
552 this->public.task.process = _process_r;
553 this->initiator = FALSE;
554 this->child_create = child_create_create(ike_sa, NULL, TRUE, NULL, NULL);
555 }
556
557 return &this->public;
558 }