826d6e1928ea1be8f434151f79c8cfd305376091
[strongswan.git] / src / libcharon / 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
17 #include "ike_rekey.h"
18
19 #include <daemon.h>
20 #include <encoding/payloads/notify_payload.h>
21 #include <sa/tasks/ike_init.h>
22 #include <sa/tasks/ike_delete.h>
23 #include <processing/jobs/delete_ike_sa_job.h>
24 #include <processing/jobs/rekey_ike_sa_job.h>
25
26
27 typedef struct private_ike_rekey_t private_ike_rekey_t;
28
29 /**
30 * Private members of a ike_rekey_t task.
31 */
32 struct private_ike_rekey_t {
33
34 /**
35 * Public methods and task_t interface.
36 */
37 ike_rekey_t public;
38
39 /**
40 * Assigned IKE_SA.
41 */
42 ike_sa_t *ike_sa;
43
44 /**
45 * New IKE_SA which replaces the current one
46 */
47 ike_sa_t *new_sa;
48
49 /**
50 * Are we the initiator?
51 */
52 bool initiator;
53
54 /**
55 * the IKE_INIT task which is reused to simplify rekeying
56 */
57 ike_init_t *ike_init;
58
59 /**
60 * IKE_DELETE task to delete the old IKE_SA after rekeying was successful
61 */
62 ike_delete_t *ike_delete;
63
64 /**
65 * colliding task detected by the task manager
66 */
67 task_t *collision;
68 };
69
70 /**
71 * Establish the new replacement IKE_SA
72 */
73 static void establish_new(private_ike_rekey_t *this)
74 {
75 if (this->new_sa)
76 {
77 this->new_sa->set_state(this->new_sa, IKE_ESTABLISHED);
78 DBG0(DBG_IKE, "IKE_SA %s[%d] rekeyed between %H[%Y]...%H[%Y]",
79 this->new_sa->get_name(this->new_sa),
80 this->new_sa->get_unique_id(this->new_sa),
81 this->ike_sa->get_my_host(this->ike_sa),
82 this->ike_sa->get_my_id(this->ike_sa),
83 this->ike_sa->get_other_host(this->ike_sa),
84 this->ike_sa->get_other_id(this->ike_sa));
85
86 this->new_sa->inherit(this->new_sa, this->ike_sa);
87 charon->bus->ike_rekey(charon->bus, this->ike_sa, this->new_sa);
88 charon->ike_sa_manager->checkin(charon->ike_sa_manager, this->new_sa);
89 this->new_sa = NULL;
90 /* set threads active IKE_SA after checkin */
91 charon->bus->set_sa(charon->bus, this->ike_sa);
92 }
93 }
94
95 METHOD(task_t, process_r_delete, status_t,
96 private_ike_rekey_t *this, message_t *message)
97 {
98 establish_new(this);
99 return this->ike_delete->task.process(&this->ike_delete->task, message);
100 }
101
102 METHOD(task_t, build_r_delete, status_t,
103 private_ike_rekey_t *this, message_t *message)
104 {
105 return this->ike_delete->task.build(&this->ike_delete->task, message);
106 }
107
108 METHOD(task_t, build_i_delete, status_t,
109 private_ike_rekey_t *this, message_t *message)
110 {
111 /* update exchange type to INFORMATIONAL for the delete */
112 message->set_exchange_type(message, INFORMATIONAL);
113
114 return this->ike_delete->task.build(&this->ike_delete->task, message);
115 }
116
117 METHOD(task_t, process_i_delete, status_t,
118 private_ike_rekey_t *this, message_t *message)
119 {
120 return this->ike_delete->task.process(&this->ike_delete->task, message);
121 }
122
123 METHOD(task_t, build_i, status_t,
124 private_ike_rekey_t *this, message_t *message)
125 {
126 peer_cfg_t *peer_cfg;
127 host_t *other_host;
128
129 /* create new SA only on first try */
130 if (this->new_sa == NULL)
131 {
132 this->new_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
133 TRUE);
134
135 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
136 other_host = this->ike_sa->get_other_host(this->ike_sa);
137 this->new_sa->set_peer_cfg(this->new_sa, peer_cfg);
138 this->new_sa->set_other_host(this->new_sa, other_host->clone(other_host));
139 this->ike_init = ike_init_create(this->new_sa, TRUE, this->ike_sa);
140 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
141 }
142 this->ike_init->task.build(&this->ike_init->task, message);
143
144 return NEED_MORE;
145 }
146
147 METHOD(task_t, process_r, status_t,
148 private_ike_rekey_t *this, message_t *message)
149 {
150 enumerator_t *enumerator;
151 peer_cfg_t *peer_cfg;
152 child_sa_t *child_sa;
153
154 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
155 {
156 DBG1(DBG_IKE, "peer initiated rekeying, but we are deleting");
157 return NEED_MORE;
158 }
159
160 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
161 while (enumerator->enumerate(enumerator, (void**)&child_sa))
162 {
163 switch (child_sa->get_state(child_sa))
164 {
165 case CHILD_CREATED:
166 case CHILD_REKEYING:
167 case CHILD_DELETING:
168 /* we do not allow rekeying while we have children in-progress */
169 DBG1(DBG_IKE, "peer initiated rekeying, but a child is half-open");
170 enumerator->destroy(enumerator);
171 return NEED_MORE;
172 default:
173 break;
174 }
175 }
176 enumerator->destroy(enumerator);
177
178 this->new_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
179 FALSE);
180
181 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
182 this->new_sa->set_peer_cfg(this->new_sa, peer_cfg);
183 this->ike_init = ike_init_create(this->new_sa, FALSE, this->ike_sa);
184 this->ike_init->task.process(&this->ike_init->task, message);
185
186 return NEED_MORE;
187 }
188
189 METHOD(task_t, build_r, status_t,
190 private_ike_rekey_t *this, message_t *message)
191 {
192 if (this->new_sa == NULL)
193 {
194 /* IKE_SA/a CHILD_SA is in an inacceptable state, deny rekeying */
195 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
196 return SUCCESS;
197 }
198
199 if (this->ike_init->task.build(&this->ike_init->task, message) == FAILED)
200 {
201 return SUCCESS;
202 }
203
204 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
205
206 /* rekeying successful, delete the IKE_SA using a subtask */
207 this->ike_delete = ike_delete_create(this->ike_sa, FALSE);
208 this->public.task.build = _build_r_delete;
209 this->public.task.process = _process_r_delete;
210
211 return NEED_MORE;
212 }
213
214 METHOD(task_t, process_i, status_t,
215 private_ike_rekey_t *this, message_t *message)
216 {
217 if (message->get_notify(message, NO_ADDITIONAL_SAS))
218 {
219 DBG1(DBG_IKE, "peer seems to not support IKE rekeying, "
220 "starting reauthentication");
221 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
222 lib->processor->queue_job(lib->processor,
223 (job_t*)rekey_ike_sa_job_create(
224 this->ike_sa->get_id(this->ike_sa), TRUE));
225 return SUCCESS;
226 }
227
228 switch (this->ike_init->task.process(&this->ike_init->task, message))
229 {
230 case FAILED:
231 /* rekeying failed, fallback to old SA */
232 if (!(this->collision && (
233 this->collision->get_type(this->collision) == IKE_DELETE ||
234 this->collision->get_type(this->collision) == IKE_REAUTH)))
235 {
236 job_t *job;
237 u_int32_t retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
238 job = (job_t*)rekey_ike_sa_job_create(
239 this->ike_sa->get_id(this->ike_sa), FALSE);
240 DBG1(DBG_IKE, "IKE_SA rekeying failed, "
241 "trying again in %d seconds", retry);
242 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
243 lib->scheduler->schedule_job(lib->scheduler, job, retry);
244 }
245 return SUCCESS;
246 case NEED_MORE:
247 /* bad dh group, try again */
248 this->ike_init->task.migrate(&this->ike_init->task, this->new_sa);
249 return NEED_MORE;
250 default:
251 break;
252 }
253
254 /* check for collisions */
255 if (this->collision &&
256 this->collision->get_type(this->collision) == IKE_REKEY)
257 {
258 private_ike_rekey_t *other = (private_ike_rekey_t*)this->collision;
259
260 /* ike_init can be NULL, if child_sa is half-open */
261 if (other->ike_init)
262 {
263 host_t *host;
264 chunk_t this_nonce, other_nonce;
265
266 this_nonce = this->ike_init->get_lower_nonce(this->ike_init);
267 other_nonce = other->ike_init->get_lower_nonce(other->ike_init);
268
269 /* if we have the lower nonce, delete rekeyed SA. If not, delete
270 * the redundant. */
271 if (memcmp(this_nonce.ptr, other_nonce.ptr,
272 min(this_nonce.len, other_nonce.len)) > 0)
273 {
274 /* peer should delete this SA. Add a timeout just in case. */
275 job_t *job = (job_t*)delete_ike_sa_job_create(
276 other->new_sa->get_id(other->new_sa), TRUE);
277 lib->scheduler->schedule_job(lib->scheduler, job, 10);
278 DBG1(DBG_IKE, "IKE_SA rekey collision won, waiting for delete");
279 charon->ike_sa_manager->checkin(charon->ike_sa_manager, other->new_sa);
280 other->new_sa = NULL;
281 }
282 else
283 {
284 DBG1(DBG_IKE, "IKE_SA rekey collision lost, "
285 "deleting redundant IKE_SA");
286 /* apply host for a proper delete */
287 host = this->ike_sa->get_my_host(this->ike_sa);
288 this->new_sa->set_my_host(this->new_sa, host->clone(host));
289 host = this->ike_sa->get_other_host(this->ike_sa);
290 this->new_sa->set_other_host(this->new_sa, host->clone(host));
291 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
292 this->new_sa->set_state(this->new_sa, IKE_REKEYING);
293 if (this->new_sa->delete(this->new_sa) == DESTROY_ME)
294 {
295 this->new_sa->destroy(this->new_sa);
296 }
297 else
298 {
299 charon->ike_sa_manager->checkin(
300 charon->ike_sa_manager, this->new_sa);
301 /* set threads active IKE_SA after checkin */
302 charon->bus->set_sa(charon->bus, this->ike_sa);
303 }
304 this->new_sa = NULL;
305 establish_new(other);
306 return SUCCESS;
307 }
308 }
309 /* set threads active IKE_SA after checkin */
310 charon->bus->set_sa(charon->bus, this->ike_sa);
311 }
312
313 establish_new(this);
314
315 /* rekeying successful, delete the IKE_SA using a subtask */
316 this->ike_delete = ike_delete_create(this->ike_sa, TRUE);
317 this->public.task.build = _build_i_delete;
318 this->public.task.process = _process_i_delete;
319
320 return NEED_MORE;
321 }
322
323 METHOD(task_t, get_type, task_type_t,
324 private_ike_rekey_t *this)
325 {
326 return IKE_REKEY;
327 }
328
329 METHOD(ike_rekey_t, collide, void,
330 private_ike_rekey_t* this, task_t *other)
331 {
332 DBG1(DBG_IKE, "detected %N collision with %N", task_type_names, IKE_REKEY,
333 task_type_names, other->get_type(other));
334 DESTROY_IF(this->collision);
335 this->collision = other;
336 }
337
338 METHOD(task_t, migrate, void,
339 private_ike_rekey_t *this, ike_sa_t *ike_sa)
340 {
341 if (this->ike_init)
342 {
343 this->ike_init->task.destroy(&this->ike_init->task);
344 }
345 if (this->ike_delete)
346 {
347 this->ike_delete->task.destroy(&this->ike_delete->task);
348 }
349 DESTROY_IF(this->new_sa);
350 DESTROY_IF(this->collision);
351
352 this->collision = NULL;
353 this->ike_sa = ike_sa;
354 this->new_sa = NULL;
355 this->ike_init = NULL;
356 this->ike_delete = NULL;
357 }
358
359 METHOD(task_t, destroy, void,
360 private_ike_rekey_t *this)
361 {
362 if (this->ike_init)
363 {
364 this->ike_init->task.destroy(&this->ike_init->task);
365 }
366 if (this->ike_delete)
367 {
368 this->ike_delete->task.destroy(&this->ike_delete->task);
369 }
370 DESTROY_IF(this->new_sa);
371 DESTROY_IF(this->collision);
372 free(this);
373 }
374
375 /*
376 * Described in header.
377 */
378 ike_rekey_t *ike_rekey_create(ike_sa_t *ike_sa, bool initiator)
379 {
380 private_ike_rekey_t *this;
381
382 INIT(this,
383 .public = {
384 .task = {
385 .get_type = _get_type,
386 .build = _build_r,
387 .process = _process_r,
388 .migrate = _migrate,
389 .destroy = _destroy,
390 },
391 .collide = _collide,
392 },
393 .ike_sa = ike_sa,
394 .initiator = initiator,
395 );
396 if (initiator)
397 {
398 this->public.task.build = _build_i;
399 this->public.task.process = _process_i;
400 }
401
402 return &this->public;
403 }