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