some task queueing improvements:
[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 ike_sa_id_t *to_delete;
197 iterator_t *iterator;
198 payload_t *payload;
199
200 /* handle NO_ADDITIONAL_SAS notify */
201 iterator = message->get_payload_iterator(message);
202 while (iterator->iterate(iterator, (void**)&payload))
203 {
204 if (payload->get_type(payload) == NOTIFY)
205 {
206 notify_payload_t *notify = (notify_payload_t*)payload;
207
208 if (notify->get_notify_type(notify) == NO_ADDITIONAL_SAS)
209 {
210 DBG1(DBG_IKE, "peer seems to not support IKE rekeying, "
211 "starting reauthentication");
212 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
213 charon->processor->queue_job(charon->processor,
214 (job_t*)rekey_ike_sa_job_create(
215 this->ike_sa->get_id(this->ike_sa), TRUE));
216 iterator->destroy(iterator);
217 return SUCCESS;
218 }
219 }
220 }
221 iterator->destroy(iterator);
222
223 switch (this->ike_init->task.process(&this->ike_init->task, message))
224 {
225 case FAILED:
226 /* rekeying failed, fallback to old SA */
227 if (!(this->collision && (
228 this->collision->get_type(this->collision) == IKE_DELETE ||
229 this->collision->get_type(this->collision) == IKE_REAUTH)))
230 {
231 job_t *job;
232 u_int32_t retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
233 job = (job_t*)rekey_ike_sa_job_create(
234 this->ike_sa->get_id(this->ike_sa), FALSE);
235 DBG1(DBG_IKE, "IKE_SA rekeying failed, "
236 "trying again in %d seconds", retry);
237 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
238 charon->scheduler->schedule_job(charon->scheduler, job, retry * 1000);
239 }
240 return SUCCESS;
241 case NEED_MORE:
242 /* bad dh group, try again */
243 this->ike_init->task.migrate(&this->ike_init->task, this->new_sa);
244 return NEED_MORE;
245 default:
246 break;
247 }
248
249 this->new_sa->set_state(this->new_sa, IKE_ESTABLISHED);
250 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%D]...%H[%D]",
251 this->new_sa->get_name(this->new_sa),
252 this->new_sa->get_unique_id(this->new_sa),
253 this->ike_sa->get_my_host(this->ike_sa),
254 this->ike_sa->get_my_id(this->ike_sa),
255 this->ike_sa->get_other_host(this->ike_sa),
256 this->ike_sa->get_other_id(this->ike_sa));
257
258 to_delete = this->ike_sa->get_id(this->ike_sa);
259
260 /* check for collisions */
261 if (this->collision &&
262 this->collision->get_type(this->collision) == IKE_REKEY)
263 {
264 chunk_t this_nonce, other_nonce;
265 host_t *host;
266 private_ike_rekey_t *other = (private_ike_rekey_t*)this->collision;
267
268 this_nonce = this->ike_init->get_lower_nonce(this->ike_init);
269 other_nonce = other->ike_init->get_lower_nonce(other->ike_init);
270
271 /* if we have the lower nonce, delete rekeyed SA. If not, delete
272 * the redundant. */
273 if (memcmp(this_nonce.ptr, other_nonce.ptr,
274 min(this_nonce.len, other_nonce.len)) < 0)
275 {
276 DBG1(DBG_IKE, "IKE_SA rekey collision won, deleting rekeyed IKE_SA");
277 charon->ike_sa_manager->checkin(charon->ike_sa_manager, other->new_sa);
278 }
279 else
280 {
281 DBG1(DBG_IKE, "IKE_SA rekey collision lost, deleting redundant IKE_SA");
282 /* apply host for a proper delete */
283 host = this->ike_sa->get_my_host(this->ike_sa);
284 this->new_sa->set_my_host(this->new_sa, host->clone(host));
285 host = this->ike_sa->get_other_host(this->ike_sa);
286 this->new_sa->set_other_host(this->new_sa, host->clone(host));
287 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
288 to_delete = this->new_sa->get_id(this->new_sa);
289 charon->ike_sa_manager->checkin(charon->ike_sa_manager, this->new_sa);
290 /* inherit to other->new_sa in destroy() */
291 this->new_sa = other->new_sa;
292 other->new_sa = NULL;
293 }
294 /* set threads active IKE_SA after checkin */
295 charon->bus->set_sa(charon->bus, this->ike_sa);
296 }
297
298 /* rekeying successful, delete the IKE_SA using a subtask */
299 this->ike_delete = ike_delete_create(this->ike_sa, TRUE);
300 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i_delete;
301 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i_delete;
302
303 return NEED_MORE;
304 }
305
306 /**
307 * Implementation of task_t.get_type
308 */
309 static task_type_t get_type(private_ike_rekey_t *this)
310 {
311 return IKE_REKEY;
312 }
313
314 static void collide(private_ike_rekey_t* this, task_t *other)
315 {
316 DESTROY_IF(this->collision);
317 this->collision = other;
318 }
319
320 /**
321 * Implementation of task_t.migrate
322 */
323 static void migrate(private_ike_rekey_t *this, ike_sa_t *ike_sa)
324 {
325 if (this->ike_init)
326 {
327 this->ike_init->task.destroy(&this->ike_init->task);
328 }
329 if (this->ike_delete)
330 {
331 this->ike_delete->task.destroy(&this->ike_delete->task);
332 }
333 if (this->new_sa)
334 {
335 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
336 this->new_sa);
337 /* set threads active IKE_SA after checkin */
338 charon->bus->set_sa(charon->bus, this->ike_sa);
339 }
340 DESTROY_IF(this->collision);
341
342 this->collision = NULL;
343 this->ike_sa = ike_sa;
344 this->new_sa = NULL;
345 this->ike_init = NULL;
346 this->ike_delete = NULL;
347 }
348
349 /**
350 * Implementation of task_t.destroy
351 */
352 static void destroy(private_ike_rekey_t *this)
353 {
354 if (this->new_sa)
355 {
356 if (this->new_sa->get_state(this->new_sa) == IKE_ESTABLISHED &&
357 this->new_sa->inherit(this->new_sa, this->ike_sa) != DESTROY_ME)
358 {
359 charon->ike_sa_manager->checkin(charon->ike_sa_manager, this->new_sa);
360 }
361 else
362 {
363 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
364 this->new_sa);
365 }
366 /* set threads active IKE_SA after checkin */
367 charon->bus->set_sa(charon->bus, this->ike_sa);
368 }
369 if (this->ike_init)
370 {
371 this->ike_init->task.destroy(&this->ike_init->task);
372 }
373 if (this->ike_delete)
374 {
375 this->ike_delete->task.destroy(&this->ike_delete->task);
376 }
377 DESTROY_IF(this->collision);
378 free(this);
379 }
380
381 /*
382 * Described in header.
383 */
384 ike_rekey_t *ike_rekey_create(ike_sa_t *ike_sa, bool initiator)
385 {
386 private_ike_rekey_t *this = malloc_thing(private_ike_rekey_t);
387
388 this->public.collide = (void(*)(ike_rekey_t*,task_t*))collide;
389 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
390 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
391 this->public.task.destroy = (void(*)(task_t*))destroy;
392 if (initiator)
393 {
394 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
395 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
396 }
397 else
398 {
399 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
400 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
401 }
402
403 this->ike_sa = ike_sa;
404 this->new_sa = NULL;
405 this->ike_init = NULL;
406 this->ike_delete = NULL;
407 this->initiator = initiator;
408 this->collision = NULL;
409
410 return &this->public;
411 }