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