schedule_job uses seconds to support time values larger than 49 days
[strongswan.git] / src / charon / sa / tasks / ike_rekey.c
1 /*
2 * Copyright (C) 2005-2008 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 * $Id$
17 */
18
19 #include "ike_rekey.h"
20
21 #include <daemon.h>
22 #include <encoding/payloads/notify_payload.h>
23 #include <sa/tasks/ike_init.h>
24 #include <sa/tasks/ike_delete.h>
25 #include <processing/jobs/delete_ike_sa_job.h>
26 #include <processing/jobs/rekey_ike_sa_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 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 /**
73 * Implementation of task_t.build for initiator, after rekeying
74 */
75 static status_t build_i_delete(private_ike_rekey_t *this, message_t *message)
76 {
77 /* update exchange type to INFORMATIONAL for the delete */
78 message->set_exchange_type(message, INFORMATIONAL);
79
80 return this->ike_delete->task.build(&this->ike_delete->task, message);
81 }
82
83 /**
84 * Implementation of task_t.process for initiator, after rekeying
85 */
86 static status_t process_i_delete(private_ike_rekey_t *this, message_t *message)
87 {
88 return this->ike_delete->task.process(&this->ike_delete->task, message);
89 }
90
91 /**
92 * Implementation of task_t.build for initiator
93 */
94 static status_t build_i(private_ike_rekey_t *this, message_t *message)
95 {
96 peer_cfg_t *peer_cfg;
97 host_t *other_host;
98
99 /* create new SA only on first try */
100 if (this->new_sa == NULL)
101 {
102 this->new_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
103 TRUE);
104
105 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
106 other_host = this->ike_sa->get_other_host(this->ike_sa);
107 this->new_sa->set_peer_cfg(this->new_sa, peer_cfg);
108 this->new_sa->set_other_host(this->new_sa, other_host->clone(other_host));
109 this->ike_init = ike_init_create(this->new_sa, TRUE, this->ike_sa);
110 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
111 }
112 this->ike_init->task.build(&this->ike_init->task, message);
113
114 return NEED_MORE;
115 }
116
117 /**
118 * Implementation of task_t.process for responder
119 */
120 static status_t process_r(private_ike_rekey_t *this, message_t *message)
121 {
122 peer_cfg_t *peer_cfg;
123 iterator_t *iterator;
124 child_sa_t *child_sa;
125
126 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
127 {
128 DBG1(DBG_IKE, "peer initiated rekeying, but we are deleting");
129 return NEED_MORE;
130 }
131
132 iterator = this->ike_sa->create_child_sa_iterator(this->ike_sa);
133 while (iterator->iterate(iterator, (void**)&child_sa))
134 {
135 switch (child_sa->get_state(child_sa))
136 {
137 case CHILD_CREATED:
138 case CHILD_REKEYING:
139 case CHILD_DELETING:
140 /* we do not allow rekeying while we have children in-progress */
141 DBG1(DBG_IKE, "peer initiated rekeying, but a child is half-open");
142 iterator->destroy(iterator);
143 return NEED_MORE;
144 default:
145 break;
146 }
147 }
148 iterator->destroy(iterator);
149
150 this->new_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
151 FALSE);
152
153 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
154 this->new_sa->set_peer_cfg(this->new_sa, peer_cfg);
155 this->ike_init = ike_init_create(this->new_sa, FALSE, this->ike_sa);
156 this->ike_init->task.process(&this->ike_init->task, message);
157
158 return NEED_MORE;
159 }
160
161 /**
162 * Implementation of task_t.build for responder
163 */
164 static status_t build_r(private_ike_rekey_t *this, message_t *message)
165 {
166 if (this->new_sa == NULL)
167 {
168 /* IKE_SA/a CHILD_SA is in an inacceptable state, deny rekeying */
169 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
170 return SUCCESS;
171 }
172
173 if (this->ike_init->task.build(&this->ike_init->task, message) == FAILED)
174 {
175 return SUCCESS;
176 }
177
178 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
179 this->new_sa->set_state(this->new_sa, IKE_ESTABLISHED);
180 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%D]...%H[%D]",
181 this->new_sa->get_name(this->new_sa),
182 this->new_sa->get_unique_id(this->new_sa),
183 this->ike_sa->get_my_host(this->ike_sa),
184 this->ike_sa->get_my_id(this->ike_sa),
185 this->ike_sa->get_other_host(this->ike_sa),
186 this->ike_sa->get_other_id(this->ike_sa));
187
188 return SUCCESS;
189 }
190
191 /**
192 * Implementation of task_t.process for initiator
193 */
194 static status_t process_i(private_ike_rekey_t *this, message_t *message)
195 {
196 enumerator_t *enumerator;
197 payload_t *payload;
198
199 /* handle NO_ADDITIONAL_SAS notify */
200 enumerator = message->create_payload_enumerator(message);
201 while (enumerator->enumerate(enumerator, &payload))
202 {
203 if (payload->get_type(payload) == NOTIFY)
204 {
205 notify_payload_t *notify = (notify_payload_t*)payload;
206
207 if (notify->get_notify_type(notify) == NO_ADDITIONAL_SAS)
208 {
209 DBG1(DBG_IKE, "peer seems to not support IKE rekeying, "
210 "starting reauthentication");
211 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
212 charon->processor->queue_job(charon->processor,
213 (job_t*)rekey_ike_sa_job_create(
214 this->ike_sa->get_id(this->ike_sa), TRUE));
215 enumerator->destroy(enumerator);
216 return SUCCESS;
217 }
218 }
219 }
220 enumerator->destroy(enumerator);
221
222 switch (this->ike_init->task.process(&this->ike_init->task, message))
223 {
224 case FAILED:
225 /* rekeying failed, fallback to old SA */
226 if (!(this->collision && (
227 this->collision->get_type(this->collision) == IKE_DELETE ||
228 this->collision->get_type(this->collision) == IKE_REAUTH)))
229 {
230 job_t *job;
231 u_int32_t retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
232 job = (job_t*)rekey_ike_sa_job_create(
233 this->ike_sa->get_id(this->ike_sa), FALSE);
234 DBG1(DBG_IKE, "IKE_SA rekeying failed, "
235 "trying again in %d seconds", retry);
236 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
237 charon->scheduler->schedule_job(charon->scheduler, job, retry);
238 }
239 return SUCCESS;
240 case NEED_MORE:
241 /* bad dh group, try again */
242 this->ike_init->task.migrate(&this->ike_init->task, this->new_sa);
243 return NEED_MORE;
244 default:
245 break;
246 }
247
248 this->new_sa->set_state(this->new_sa, IKE_ESTABLISHED);
249 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%D]...%H[%D]",
250 this->new_sa->get_name(this->new_sa),
251 this->new_sa->get_unique_id(this->new_sa),
252 this->ike_sa->get_my_host(this->ike_sa),
253 this->ike_sa->get_my_id(this->ike_sa),
254 this->ike_sa->get_other_host(this->ike_sa),
255 this->ike_sa->get_other_id(this->ike_sa));
256
257 /* check for collisions */
258 if (this->collision &&
259 this->collision->get_type(this->collision) == IKE_REKEY)
260 {
261 chunk_t this_nonce, other_nonce;
262 host_t *host;
263 private_ike_rekey_t *other = (private_ike_rekey_t*)this->collision;
264
265 this_nonce = this->ike_init->get_lower_nonce(this->ike_init);
266 other_nonce = other->ike_init->get_lower_nonce(other->ike_init);
267
268 /* if we have the lower nonce, delete rekeyed SA. If not, delete
269 * the redundant. */
270 if (memcmp(this_nonce.ptr, other_nonce.ptr,
271 min(this_nonce.len, other_nonce.len)) < 0)
272 {
273 /* peer should delete this SA. Add a timeout just in case. */
274 job_t *job = (job_t*)delete_ike_sa_job_create(
275 other->new_sa->get_id(other->new_sa), TRUE);
276 charon->scheduler->schedule_job(charon->scheduler, job, 10);
277 DBG1(DBG_IKE, "IKE_SA rekey collision won, deleting rekeyed IKE_SA");
278 charon->ike_sa_manager->checkin(charon->ike_sa_manager, other->new_sa);
279 other->new_sa = NULL;
280 }
281 else
282 {
283 DBG1(DBG_IKE, "IKE_SA rekey collision lost, deleting redundant IKE_SA");
284 /* apply host for a proper delete */
285 host = this->ike_sa->get_my_host(this->ike_sa);
286 this->new_sa->set_my_host(this->new_sa, host->clone(host));
287 host = this->ike_sa->get_other_host(this->ike_sa);
288 this->new_sa->set_other_host(this->new_sa, host->clone(host));
289 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
290 if (this->new_sa->delete(this->new_sa) == DESTROY_ME)
291 {
292 charon->ike_sa_manager->checkin_and_destroy(
293 charon->ike_sa_manager, this->new_sa);
294 }
295 else
296 {
297 charon->ike_sa_manager->checkin(
298 charon->ike_sa_manager, this->new_sa);
299 }
300 /* set threads active IKE_SA after checkin */
301 charon->bus->set_sa(charon->bus, this->ike_sa);
302 /* inherit to other->new_sa in destroy() */
303 this->new_sa = other->new_sa;
304 other->new_sa = NULL;
305 return SUCCESS;
306 }
307 /* set threads active IKE_SA after checkin */
308 charon->bus->set_sa(charon->bus, this->ike_sa);
309 }
310
311 /* rekeying successful, delete the IKE_SA using a subtask */
312 this->ike_delete = ike_delete_create(this->ike_sa, TRUE);
313 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i_delete;
314 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i_delete;
315
316 return NEED_MORE;
317 }
318
319 /**
320 * Implementation of task_t.get_type
321 */
322 static task_type_t get_type(private_ike_rekey_t *this)
323 {
324 return IKE_REKEY;
325 }
326
327 static void collide(private_ike_rekey_t* this, task_t *other)
328 {
329 DESTROY_IF(this->collision);
330 this->collision = other;
331 }
332
333 /**
334 * Implementation of task_t.migrate
335 */
336 static void migrate(private_ike_rekey_t *this, ike_sa_t *ike_sa)
337 {
338 if (this->ike_init)
339 {
340 this->ike_init->task.destroy(&this->ike_init->task);
341 }
342 if (this->ike_delete)
343 {
344 this->ike_delete->task.destroy(&this->ike_delete->task);
345 }
346 if (this->new_sa)
347 {
348 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
349 this->new_sa);
350 /* set threads active IKE_SA after checkin */
351 charon->bus->set_sa(charon->bus, this->ike_sa);
352 }
353 DESTROY_IF(this->collision);
354
355 this->collision = NULL;
356 this->ike_sa = ike_sa;
357 this->new_sa = NULL;
358 this->ike_init = NULL;
359 this->ike_delete = NULL;
360 }
361
362 /**
363 * Implementation of task_t.destroy
364 */
365 static void destroy(private_ike_rekey_t *this)
366 {
367 if (this->new_sa)
368 {
369 if (this->new_sa->get_state(this->new_sa) == IKE_ESTABLISHED &&
370 this->new_sa->inherit(this->new_sa, this->ike_sa) != DESTROY_ME)
371 {
372 charon->ike_sa_manager->checkin(charon->ike_sa_manager, this->new_sa);
373 }
374 else
375 {
376 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
377 this->new_sa);
378 }
379 /* set threads active IKE_SA after checkin */
380 charon->bus->set_sa(charon->bus, this->ike_sa);
381 }
382 if (this->ike_init)
383 {
384 this->ike_init->task.destroy(&this->ike_init->task);
385 }
386 if (this->ike_delete)
387 {
388 this->ike_delete->task.destroy(&this->ike_delete->task);
389 }
390 DESTROY_IF(this->collision);
391 free(this);
392 }
393
394 /*
395 * Described in header.
396 */
397 ike_rekey_t *ike_rekey_create(ike_sa_t *ike_sa, bool initiator)
398 {
399 private_ike_rekey_t *this = malloc_thing(private_ike_rekey_t);
400
401 this->public.collide = (void(*)(ike_rekey_t*,task_t*))collide;
402 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
403 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
404 this->public.task.destroy = (void(*)(task_t*))destroy;
405 if (initiator)
406 {
407 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
408 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
409 }
410 else
411 {
412 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
413 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
414 }
415
416 this->ike_sa = ike_sa;
417 this->new_sa = NULL;
418 this->ike_init = NULL;
419 this->ike_delete = NULL;
420 this->initiator = initiator;
421 this->collision = NULL;
422
423 return &this->public;
424 }