05db057fb8d63ed83be228fbba835597e913255b
[strongswan.git] / src / libcharon / sa / 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/tasks/child_create.h>
22 #include <sa/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 u_int32_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 * Implementation of task_t.build for initiator, after rekeying
91 */
92 static status_t build_i_delete(private_child_rekey_t *this, message_t *message)
93 {
94 /* update exchange type to INFORMATIONAL for the delete */
95 message->set_exchange_type(message, INFORMATIONAL);
96
97 return this->child_delete->task.build(&this->child_delete->task, message);
98 }
99
100 /**
101 * Implementation of task_t.process for initiator, after rekeying
102 */
103 static status_t process_i_delete(private_child_rekey_t *this, message_t *message)
104 {
105 return this->child_delete->task.process(&this->child_delete->task, message);
106 }
107
108 /**
109 * find a child using the REKEY_SA notify
110 */
111 static void find_child(private_child_rekey_t *this, message_t *message)
112 {
113 notify_payload_t *notify;
114 protocol_id_t protocol;
115 u_int32_t spi;
116
117 notify = message->get_notify(message, REKEY_SA);
118 if (notify)
119 {
120 protocol = notify->get_protocol_id(notify);
121 spi = notify->get_spi(notify);
122
123 if (protocol == PROTO_ESP || protocol == PROTO_AH)
124 {
125 this->child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol,
126 spi, FALSE);
127 }
128 }
129 }
130
131 /**
132 * Implementation of task_t.build for initiator
133 */
134 static status_t build_i(private_child_rekey_t *this, message_t *message)
135 {
136 notify_payload_t *notify;
137 u_int32_t reqid;
138 child_cfg_t *config;
139
140 this->child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
141 this->spi, TRUE);
142 if (!this->child_sa)
143 { /* check if it is an outbound CHILD_SA */
144 this->child_sa = this->ike_sa->get_child_sa(this->ike_sa, this->protocol,
145 this->spi, FALSE);
146 if (!this->child_sa)
147 { /* CHILD_SA is gone, unable to rekey. As an empty CREATE_CHILD_SA
148 * exchange is invalid, we fall back to an INFORMATIONAL exchange.*/
149 message->set_exchange_type(message, INFORMATIONAL);
150 return SUCCESS;
151 }
152 /* we work only with the inbound SPI */
153 this->spi = this->child_sa->get_spi(this->child_sa, TRUE);
154 }
155 config = this->child_sa->get_config(this->child_sa);
156
157 /* we just need the rekey notify ... */
158 notify = notify_payload_create_from_protocol_and_type(this->protocol,
159 REKEY_SA);
160 notify->set_spi(notify, this->spi);
161 message->add_payload(message, (payload_t*)notify);
162
163 /* ... our CHILD_CREATE task does the hard work for us. */
164 if (!this->child_create)
165 {
166 this->child_create = child_create_create(this->ike_sa, config, TRUE,
167 NULL, NULL);
168 }
169 reqid = this->child_sa->get_reqid(this->child_sa);
170 this->child_create->use_reqid(this->child_create, reqid);
171 this->child_create->task.build(&this->child_create->task, message);
172
173 this->child_sa->set_state(this->child_sa, CHILD_REKEYING);
174
175 return NEED_MORE;
176 }
177
178 /**
179 * Implementation of task_t.process for initiator
180 */
181 static status_t process_r(private_child_rekey_t *this, message_t *message)
182 {
183 /* let the CHILD_CREATE task process the message */
184 this->child_create->task.process(&this->child_create->task, message);
185
186 find_child(this, message);
187
188 return NEED_MORE;
189 }
190
191 /**
192 * Implementation of task_t.build for responder
193 */
194 static status_t build_r(private_child_rekey_t *this, message_t *message)
195 {
196 u_int32_t reqid;
197
198 if (this->child_sa == NULL ||
199 this->child_sa->get_state(this->child_sa) == CHILD_DELETING)
200 {
201 DBG1(DBG_IKE, "unable to rekey, CHILD_SA not found");
202 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
203 return SUCCESS;
204 }
205
206 /* let the CHILD_CREATE task build the response */
207 reqid = this->child_sa->get_reqid(this->child_sa);
208 this->child_create->use_reqid(this->child_create, reqid);
209 this->child_create->task.build(&this->child_create->task, message);
210
211 if (message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
212 {
213 /* rekeying failed, reuse old child */
214 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
215 return SUCCESS;
216 }
217
218 this->child_sa->set_state(this->child_sa, CHILD_REKEYING);
219
220 /* invoke rekey hook */
221 charon->bus->child_rekey(charon->bus, this->child_sa,
222 this->child_create->get_child(this->child_create));
223 return SUCCESS;
224 }
225
226 /**
227 * Handle a rekey collision
228 */
229 static child_sa_t *handle_collision(private_child_rekey_t *this)
230 {
231 child_sa_t *to_delete;
232
233 if (this->collision->get_type(this->collision) == CHILD_REKEY)
234 {
235 chunk_t this_nonce, other_nonce;
236 private_child_rekey_t *other = (private_child_rekey_t*)this->collision;
237
238 this_nonce = this->child_create->get_lower_nonce(this->child_create);
239 other_nonce = other->child_create->get_lower_nonce(other->child_create);
240
241 /* if we have the lower nonce, delete rekeyed SA. If not, delete
242 * the redundant. */
243 if (memcmp(this_nonce.ptr, other_nonce.ptr,
244 min(this_nonce.len, other_nonce.len)) > 0)
245 {
246 child_sa_t *child_sa;
247
248 DBG1(DBG_IKE, "CHILD_SA rekey collision won, deleting old child");
249 to_delete = this->child_sa;
250 /* don't touch child other created, it has already been deleted */
251 if (!this->other_child_destroyed)
252 {
253 /* disable close action for the redundand child */
254 child_sa = other->child_create->get_child(other->child_create);
255 if (child_sa)
256 {
257 child_sa->set_close_action(child_sa, ACTION_NONE);
258 }
259 }
260 }
261 else
262 {
263 DBG1(DBG_IKE, "CHILD_SA rekey collision lost, "
264 "deleting rekeyed child");
265 to_delete = this->child_create->get_child(this->child_create);
266 }
267 }
268 else
269 { /* CHILD_DELETE */
270 child_delete_t *del = (child_delete_t*)this->collision;
271
272 /* we didn't had a chance to compare the nonces, so we delete
273 * the CHILD_SA the other is not deleting. */
274 if (del->get_child(del) != this->child_sa)
275 {
276 DBG1(DBG_IKE, "CHILD_SA rekey/delete collision, "
277 "deleting rekeyed child");
278 to_delete = this->child_sa;
279 }
280 else
281 {
282 DBG1(DBG_IKE, "CHILD_SA rekey/delete collision, "
283 "deleting redundant child");
284 to_delete = this->child_create->get_child(this->child_create);
285 }
286 }
287 return to_delete;
288 }
289
290 /**
291 * Implementation of task_t.process for initiator
292 */
293 static status_t process_i(private_child_rekey_t *this, message_t *message)
294 {
295 protocol_id_t protocol;
296 u_int32_t spi;
297 child_sa_t *to_delete;
298
299 if (message->get_notify(message, NO_ADDITIONAL_SAS))
300 {
301 DBG1(DBG_IKE, "peer seems to not support CHILD_SA rekeying, "
302 "starting reauthentication");
303 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
304 lib->processor->queue_job(lib->processor,
305 (job_t*)rekey_ike_sa_job_create(
306 this->ike_sa->get_id(this->ike_sa), TRUE));
307 return SUCCESS;
308 }
309
310 if (this->child_create->task.process(&this->child_create->task,
311 message) == NEED_MORE)
312 {
313 /* bad DH group while rekeying, try again */
314 this->child_create->task.migrate(&this->child_create->task, this->ike_sa);
315 return NEED_MORE;
316 }
317 if (message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
318 {
319 /* establishing new child failed, reuse old. but not when we
320 * recieved a delete in the meantime */
321 if (!(this->collision &&
322 this->collision->get_type(this->collision) == CHILD_DELETE))
323 {
324 job_t *job;
325 u_int32_t retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
326
327 job = (job_t*)rekey_child_sa_job_create(
328 this->child_sa->get_reqid(this->child_sa),
329 this->child_sa->get_protocol(this->child_sa),
330 this->child_sa->get_spi(this->child_sa, TRUE));
331 DBG1(DBG_IKE, "CHILD_SA rekeying failed, "
332 "trying again in %d seconds", retry);
333 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
334 lib->scheduler->schedule_job(lib->scheduler, job, retry);
335 }
336 return SUCCESS;
337 }
338
339 /* check for rekey collisions */
340 if (this->collision)
341 {
342 to_delete = handle_collision(this);
343 }
344 else
345 {
346 to_delete = this->child_sa;
347 }
348
349 if (to_delete != this->child_create->get_child(this->child_create))
350 { /* invoke rekey hook if rekeying successful */
351 charon->bus->child_rekey(charon->bus, this->child_sa,
352 this->child_create->get_child(this->child_create));
353 }
354
355 if (to_delete == NULL)
356 {
357 return SUCCESS;
358 }
359 spi = to_delete->get_spi(to_delete, TRUE);
360 protocol = to_delete->get_protocol(to_delete);
361
362 /* rekeying done, delete the obsolete CHILD_SA using a subtask */
363 this->child_delete = child_delete_create(this->ike_sa, protocol, spi);
364 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i_delete;
365 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i_delete;
366
367 return NEED_MORE;
368 }
369
370 /**
371 * Implementation of task_t.get_type
372 */
373 static task_type_t get_type(private_child_rekey_t *this)
374 {
375 return CHILD_REKEY;
376 }
377
378 /**
379 * Implementation of child_rekey_t.collide
380 */
381 static void collide(private_child_rekey_t *this, task_t *other)
382 {
383 /* the task manager only detects exchange collision, but not if
384 * the collision is for the same child. we check it here. */
385 if (other->get_type(other) == CHILD_REKEY)
386 {
387 private_child_rekey_t *rekey = (private_child_rekey_t*)other;
388 if (rekey->child_sa != this->child_sa)
389 {
390 /* not the same child => no collision */
391 other->destroy(other);
392 return;
393 }
394 }
395 else if (other->get_type(other) == CHILD_DELETE)
396 {
397 child_delete_t *del = (child_delete_t*)other;
398 if (del->get_child(del) == this->child_create->get_child(this->child_create))
399 {
400 /* peer deletes redundant child created in collision */
401 this->other_child_destroyed = TRUE;
402 other->destroy(other);
403 return;
404 }
405 if (del->get_child(del) != this->child_sa)
406 {
407 /* not the same child => no collision */
408 other->destroy(other);
409 return;
410 }
411 }
412 else
413 {
414 /* any other task is not critical for collisisions, ignore */
415 other->destroy(other);
416 return;
417 }
418 DBG1(DBG_IKE, "detected %N collision with %N", task_type_names, CHILD_REKEY,
419 task_type_names, other->get_type(other));
420 DESTROY_IF(this->collision);
421 this->collision = other;
422 }
423
424 /**
425 * Implementation of task_t.migrate
426 */
427 static void migrate(private_child_rekey_t *this, ike_sa_t *ike_sa)
428 {
429 if (this->child_create)
430 {
431 this->child_create->task.migrate(&this->child_create->task, ike_sa);
432 }
433 if (this->child_delete)
434 {
435 this->child_delete->task.migrate(&this->child_delete->task, ike_sa);
436 }
437 DESTROY_IF(this->collision);
438
439 this->ike_sa = ike_sa;
440 this->collision = NULL;
441 }
442
443 /**
444 * Implementation of task_t.destroy
445 */
446 static void destroy(private_child_rekey_t *this)
447 {
448 if (this->child_create)
449 {
450 this->child_create->task.destroy(&this->child_create->task);
451 }
452 if (this->child_delete)
453 {
454 this->child_delete->task.destroy(&this->child_delete->task);
455 }
456 DESTROY_IF(this->collision);
457 free(this);
458 }
459
460 /*
461 * Described in header.
462 */
463 child_rekey_t *child_rekey_create(ike_sa_t *ike_sa, protocol_id_t protocol,
464 u_int32_t spi)
465 {
466 private_child_rekey_t *this = malloc_thing(private_child_rekey_t);
467
468 this->public.collide = (void (*)(child_rekey_t*,task_t*))collide;
469 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
470 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
471 this->public.task.destroy = (void(*)(task_t*))destroy;
472 if (protocol != PROTO_NONE)
473 {
474 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
475 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
476 this->initiator = TRUE;
477 this->child_create = NULL;
478 }
479 else
480 {
481 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
482 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
483 this->initiator = FALSE;
484 this->child_create = child_create_create(ike_sa, NULL, TRUE, NULL, NULL);
485 }
486
487 this->ike_sa = ike_sa;
488 this->child_sa = NULL;
489 this->protocol = protocol;
490 this->spi = spi;
491 this->collision = NULL;
492 this->child_delete = NULL;
493 this->other_child_destroyed = FALSE;
494
495 return &this->public;
496 }