4 * @brief Implementation of the child_rekey task.
9 * Copyright (C) 2005-2007 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include "child_rekey.h"
27 #include <encoding/payloads/notify_payload.h>
28 #include <sa/tasks/child_create.h>
31 typedef struct private_child_rekey_t private_child_rekey_t
;
34 * Private members of a child_rekey_t task.
36 struct private_child_rekey_t
{
39 * Public methods and task_t interface.
49 * Are we the initiator?
54 * the CHILD_CREATE task which is reused to simplify rekeying
56 child_create_t
*child_create
;
59 * CHILD_SA which gets rekeyed
64 * colliding task, may be delete or rekey
70 * find a child using the REKEY_SA notify
72 static void find_child(private_child_rekey_t
*this, message_t
*message
)
77 iterator
= message
->get_payload_iterator(message
);
78 while (iterator
->iterate(iterator
, (void**)&payload
))
80 notify_payload_t
*notify
;
82 protocol_id_t protocol
;
84 if (payload
->get_type(payload
) != NOTIFY
)
89 notify
= (notify_payload_t
*)payload
;
90 protocol
= notify
->get_protocol_id(notify
);
91 spi
= notify
->get_spi(notify
);
93 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
97 this->child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
,
102 iterator
->destroy(iterator
);
106 * Implementation of task_t.build for initiator
108 static status_t
build_i(private_child_rekey_t
*this, message_t
*message
)
110 notify_payload_t
*notify
;
111 protocol_id_t protocol
;
112 u_int32_t spi
, reqid
;
114 /* we just need the rekey notify ... */
115 protocol
= this->child_sa
->get_protocol(this->child_sa
);
116 spi
= this->child_sa
->get_spi(this->child_sa
, TRUE
);
117 notify
= notify_payload_create_from_protocol_and_type(protocol
, REKEY_SA
);
118 notify
->set_spi(notify
, spi
);
119 message
->add_payload(message
, (payload_t
*)notify
);
121 /* ... our CHILD_CREATE task does the hard work for us. */
122 reqid
= this->child_sa
->get_reqid(this->child_sa
);
123 this->child_create
->use_reqid(this->child_create
, reqid
);
124 this->child_create
->task
.build(&this->child_create
->task
, message
);
126 this->child_sa
->set_state(this->child_sa
, CHILD_REKEYING
);
132 * Implementation of task_t.process for initiator
134 static status_t
process_r(private_child_rekey_t
*this, message_t
*message
)
136 /* let the CHILD_CREATE task process the message */
137 this->child_create
->task
.process(&this->child_create
->task
, message
);
139 find_child(this, message
);
145 * Implementation of task_t.build for responder
147 static status_t
build_r(private_child_rekey_t
*this, message_t
*message
)
151 if (this->child_sa
== NULL
||
152 this->child_sa
->get_state(this->child_sa
) == CHILD_DELETING
)
154 DBG1(DBG_IKE
, "unable to rekey, CHILD_SA not found");
155 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
159 /* let the CHILD_CREATE task build the response */
160 reqid
= this->child_sa
->get_reqid(this->child_sa
);
161 this->child_create
->use_reqid(this->child_create
, reqid
);
162 this->child_create
->task
.build(&this->child_create
->task
, message
);
164 if (message
->get_payload(message
, SECURITY_ASSOCIATION
) == NULL
)
166 /* rekeying failed, reuse old child */
167 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
168 /* TODO: reschedule rekeying */
172 this->child_sa
->set_state(this->child_sa
, CHILD_REKEYING
);
178 * Implementation of task_t.process for initiator
180 static status_t
process_i(private_child_rekey_t
*this, message_t
*message
)
182 protocol_id_t protocol
;
184 child_sa_t
*to_delete
;
186 this->child_create
->task
.process(&this->child_create
->task
, message
);
187 if (message
->get_payload(message
, SECURITY_ASSOCIATION
) == NULL
)
189 /* establishing new child failed, reuse old. but not when we
190 * recieved a delete in the meantime */
191 if (!(this->collision
&&
192 this->collision
->get_type(this->collision
) == CHILD_DELETE
))
194 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
195 /* TODO: rescedule rekeying */
200 to_delete
= this->child_sa
;
202 /* check for rekey collisions */
203 if (this->collision
&&
204 this->collision
->get_type(this->collision
) == CHILD_REKEY
)
206 chunk_t this_nonce
, other_nonce
;
207 private_child_rekey_t
*other
= (private_child_rekey_t
*)this->collision
;
209 this_nonce
= this->child_create
->get_lower_nonce(this->child_create
);
210 other_nonce
= other
->child_create
->get_lower_nonce(other
->child_create
);
212 /* if we have the lower nonce, delete rekeyed SA. If not, delete
214 if (memcmp(this_nonce
.ptr
, other_nonce
.ptr
,
215 min(this_nonce
.len
, other_nonce
.len
)) < 0)
217 DBG1(DBG_IKE
, "CHILD_SA rekey collision won, deleting rekeyed child");
221 DBG1(DBG_IKE
, "CHILD_SA rekey collision lost, deleting redundant child");
222 to_delete
= this->child_create
->get_child(this->child_create
);
223 if (to_delete
== NULL
)
225 /* ooops, should not happen, fallback */
226 to_delete
= this->child_sa
;
231 spi
= to_delete
->get_spi(to_delete
, TRUE
);
232 protocol
= to_delete
->get_protocol(to_delete
);
233 if (this->ike_sa
->delete_child_sa(this->ike_sa
, protocol
, spi
) != SUCCESS
)
241 * Implementation of task_t.get_type
243 static task_type_t
get_type(private_child_rekey_t
*this)
249 * Implementation of child_rekey_t.collide
251 static void collide(private_child_rekey_t
*this, task_t
*other
)
253 DESTROY_IF(this->collision
);
254 this->collision
= other
;
258 * Implementation of task_t.migrate
260 static void migrate(private_child_rekey_t
*this, ike_sa_t
*ike_sa
)
262 this->child_create
->task
.migrate(&this->child_create
->task
, ike_sa
);
263 DESTROY_IF(this->collision
);
265 this->ike_sa
= ike_sa
;
266 this->collision
= NULL
;
270 * Implementation of task_t.destroy
272 static void destroy(private_child_rekey_t
*this)
274 this->child_create
->task
.destroy(&this->child_create
->task
);
275 DESTROY_IF(this->collision
);
280 * Described in header.
282 child_rekey_t
*child_rekey_create(ike_sa_t
*ike_sa
, child_sa_t
*child_sa
)
284 private_child_rekey_t
*this = malloc_thing(private_child_rekey_t
);
287 this->public.collide
= (void (*)(child_rekey_t
*,task_t
*))collide
;
288 this->public.task
.get_type
= (task_type_t(*)(task_t
*))get_type
;
289 this->public.task
.migrate
= (void(*)(task_t
*,ike_sa_t
*))migrate
;
290 this->public.task
.destroy
= (void(*)(task_t
*))destroy
;
291 if (child_sa
!= NULL
)
293 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_i
;
294 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_i
;
295 this->initiator
= TRUE
;
296 policy
= child_sa
->get_policy(child_sa
);
297 this->child_create
= child_create_create(ike_sa
, policy
);
301 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_r
;
302 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_r
;
303 this->initiator
= FALSE
;
304 this->child_create
= child_create_create(ike_sa
, NULL
);
307 this->ike_sa
= ike_sa
;
308 this->child_sa
= child_sa
;
309 this->collision
= NULL
;
311 return &this->public;