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>
29 #include <queues/jobs/rekey_child_sa_job.h>
32 typedef struct private_child_rekey_t private_child_rekey_t
;
35 * Private members of a child_rekey_t task.
37 struct private_child_rekey_t
{
40 * Public methods and task_t interface.
50 * Are we the initiator?
55 * the CHILD_CREATE task which is reused to simplify rekeying
57 child_create_t
*child_create
;
60 * CHILD_SA which gets rekeyed
65 * colliding task, may be delete or rekey
71 * find a child using the REKEY_SA notify
73 static void find_child(private_child_rekey_t
*this, message_t
*message
)
78 iterator
= message
->get_payload_iterator(message
);
79 while (iterator
->iterate(iterator
, (void**)&payload
))
81 notify_payload_t
*notify
;
83 protocol_id_t protocol
;
85 if (payload
->get_type(payload
) != NOTIFY
)
90 notify
= (notify_payload_t
*)payload
;
91 protocol
= notify
->get_protocol_id(notify
);
92 spi
= notify
->get_spi(notify
);
94 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
98 this->child_sa
= this->ike_sa
->get_child_sa(this->ike_sa
, protocol
,
103 iterator
->destroy(iterator
);
107 * Implementation of task_t.build for initiator
109 static status_t
build_i(private_child_rekey_t
*this, message_t
*message
)
111 notify_payload_t
*notify
;
112 protocol_id_t protocol
;
113 u_int32_t spi
, reqid
;
115 /* we just need the rekey notify ... */
116 protocol
= this->child_sa
->get_protocol(this->child_sa
);
117 spi
= this->child_sa
->get_spi(this->child_sa
, TRUE
);
118 notify
= notify_payload_create_from_protocol_and_type(protocol
, REKEY_SA
);
119 notify
->set_spi(notify
, spi
);
120 message
->add_payload(message
, (payload_t
*)notify
);
122 /* ... our CHILD_CREATE task does the hard work for us. */
123 reqid
= this->child_sa
->get_reqid(this->child_sa
);
124 this->child_create
->use_reqid(this->child_create
, reqid
);
125 this->child_create
->task
.build(&this->child_create
->task
, message
);
127 this->child_sa
->set_state(this->child_sa
, CHILD_REKEYING
);
133 * Implementation of task_t.process for initiator
135 static status_t
process_r(private_child_rekey_t
*this, message_t
*message
)
137 /* let the CHILD_CREATE task process the message */
138 this->child_create
->task
.process(&this->child_create
->task
, message
);
140 find_child(this, message
);
146 * Implementation of task_t.build for responder
148 static status_t
build_r(private_child_rekey_t
*this, message_t
*message
)
152 if (this->child_sa
== NULL
||
153 this->child_sa
->get_state(this->child_sa
) == CHILD_DELETING
)
155 DBG1(DBG_IKE
, "unable to rekey, CHILD_SA not found");
156 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
160 /* let the CHILD_CREATE task build the response */
161 reqid
= this->child_sa
->get_reqid(this->child_sa
);
162 this->child_create
->use_reqid(this->child_create
, reqid
);
163 this->child_create
->task
.build(&this->child_create
->task
, message
);
165 if (message
->get_payload(message
, SECURITY_ASSOCIATION
) == NULL
)
167 /* rekeying failed, reuse old child */
168 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
172 this->child_sa
->set_state(this->child_sa
, CHILD_REKEYING
);
177 * Implementation of task_t.process for initiator
179 static status_t
process_i(private_child_rekey_t
*this, message_t
*message
)
181 protocol_id_t protocol
;
183 child_sa_t
*to_delete
;
185 this->child_create
->task
.process(&this->child_create
->task
, message
);
186 if (message
->get_payload(message
, SECURITY_ASSOCIATION
) == NULL
)
188 /* establishing new child failed, reuse old. but not when we
189 * recieved a delete in the meantime */
190 if (!(this->collision
&&
191 this->collision
->get_type(this->collision
) == CHILD_DELETE
))
194 u_int32_t retry
= charon
->configuration
->get_retry_interval(
195 charon
->configuration
);
196 job
= (job_t
*)rekey_child_sa_job_create(
197 this->child_sa
->get_reqid(this->child_sa
),
198 this->child_sa
->get_protocol(this->child_sa
),
199 this->child_sa
->get_spi(this->child_sa
, TRUE
));
200 DBG1(DBG_IKE
, "CHILD_SA rekeying failed, "
201 "trying again in %d seconds", retry
);
202 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
203 charon
->event_queue
->add_relative(charon
->event_queue
, job
, retry
* 1000);
208 to_delete
= this->child_sa
;
210 /* check for rekey collisions */
211 if (this->collision
&&
212 this->collision
->get_type(this->collision
) == CHILD_REKEY
)
214 chunk_t this_nonce
, other_nonce
;
215 private_child_rekey_t
*other
= (private_child_rekey_t
*)this->collision
;
217 this_nonce
= this->child_create
->get_lower_nonce(this->child_create
);
218 other_nonce
= other
->child_create
->get_lower_nonce(other
->child_create
);
220 /* if we have the lower nonce, delete rekeyed SA. If not, delete
222 if (memcmp(this_nonce
.ptr
, other_nonce
.ptr
,
223 min(this_nonce
.len
, other_nonce
.len
)) < 0)
225 DBG1(DBG_IKE
, "CHILD_SA rekey collision won, deleting rekeyed child");
229 DBG1(DBG_IKE
, "CHILD_SA rekey collision lost, deleting redundant child");
230 to_delete
= this->child_create
->get_child(this->child_create
);
231 if (to_delete
== NULL
)
233 /* ooops, should not happen, fallback */
234 to_delete
= this->child_sa
;
239 spi
= to_delete
->get_spi(to_delete
, TRUE
);
240 protocol
= to_delete
->get_protocol(to_delete
);
241 if (this->ike_sa
->delete_child_sa(this->ike_sa
, protocol
, spi
) != SUCCESS
)
249 * Implementation of task_t.get_type
251 static task_type_t
get_type(private_child_rekey_t
*this)
257 * Implementation of child_rekey_t.collide
259 static void collide(private_child_rekey_t
*this, task_t
*other
)
261 DESTROY_IF(this->collision
);
262 this->collision
= other
;
266 * Implementation of task_t.migrate
268 static void migrate(private_child_rekey_t
*this, ike_sa_t
*ike_sa
)
270 this->child_create
->task
.migrate(&this->child_create
->task
, ike_sa
);
271 DESTROY_IF(this->collision
);
273 this->ike_sa
= ike_sa
;
274 this->collision
= NULL
;
278 * Implementation of task_t.destroy
280 static void destroy(private_child_rekey_t
*this)
282 this->child_create
->task
.destroy(&this->child_create
->task
);
283 DESTROY_IF(this->collision
);
288 * Described in header.
290 child_rekey_t
*child_rekey_create(ike_sa_t
*ike_sa
, child_sa_t
*child_sa
)
292 private_child_rekey_t
*this = malloc_thing(private_child_rekey_t
);
295 this->public.collide
= (void (*)(child_rekey_t
*,task_t
*))collide
;
296 this->public.task
.get_type
= (task_type_t(*)(task_t
*))get_type
;
297 this->public.task
.migrate
= (void(*)(task_t
*,ike_sa_t
*))migrate
;
298 this->public.task
.destroy
= (void(*)(task_t
*))destroy
;
299 if (child_sa
!= NULL
)
301 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_i
;
302 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_i
;
303 this->initiator
= TRUE
;
304 policy
= child_sa
->get_policy(child_sa
);
305 this->child_create
= child_create_create(ike_sa
, policy
);
309 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_r
;
310 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_r
;
311 this->initiator
= FALSE
;
312 this->child_create
= child_create_create(ike_sa
, NULL
);
315 this->ike_sa
= ike_sa
;
316 this->child_sa
= child_sa
;
317 this->collision
= NULL
;
319 return &this->public;