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