2007-02-28 14:04:36 +00:00
|
|
|
/**
|
|
|
|
* @file child_rekey.c
|
|
|
|
*
|
|
|
|
* @brief Implementation of the child_rekey task.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2005-2007 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "child_rekey.h"
|
|
|
|
|
|
|
|
#include <daemon.h>
|
|
|
|
#include <encoding/payloads/notify_payload.h>
|
|
|
|
#include <sa/tasks/child_create.h>
|
2007-03-22 07:34:42 +00:00
|
|
|
#include <sa/tasks/child_delete.h>
|
2007-04-10 06:01:03 +00:00
|
|
|
#include <processing/jobs/rekey_child_sa_job.h>
|
2007-02-28 14:04:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct private_child_rekey_t private_child_rekey_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private members of a child_rekey_t task.
|
|
|
|
*/
|
|
|
|
struct private_child_rekey_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public methods and task_t interface.
|
|
|
|
*/
|
|
|
|
child_rekey_t public;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigned IKE_SA.
|
|
|
|
*/
|
|
|
|
ike_sa_t *ike_sa;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Are we the initiator?
|
|
|
|
*/
|
|
|
|
bool initiator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the CHILD_CREATE task which is reused to simplify rekeying
|
|
|
|
*/
|
|
|
|
child_create_t *child_create;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CHILD_SA which gets rekeyed
|
|
|
|
*/
|
|
|
|
child_sa_t *child_sa;
|
|
|
|
|
|
|
|
/**
|
2007-03-21 09:25:36 +00:00
|
|
|
* colliding task, may be delete or rekey
|
2007-02-28 14:04:36 +00:00
|
|
|
*/
|
2007-03-21 09:25:36 +00:00
|
|
|
task_t *collision;
|
2007-02-28 14:04:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find a child using the REKEY_SA notify
|
|
|
|
*/
|
|
|
|
static void find_child(private_child_rekey_t *this, message_t *message)
|
|
|
|
{
|
|
|
|
iterator_t *iterator;
|
|
|
|
payload_t *payload;
|
|
|
|
|
|
|
|
iterator = message->get_payload_iterator(message);
|
|
|
|
while (iterator->iterate(iterator, (void**)&payload))
|
|
|
|
{
|
|
|
|
notify_payload_t *notify;
|
|
|
|
u_int32_t spi;
|
|
|
|
protocol_id_t protocol;
|
|
|
|
|
|
|
|
if (payload->get_type(payload) != NOTIFY)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
notify = (notify_payload_t*)payload;
|
|
|
|
protocol = notify->get_protocol_id(notify);
|
|
|
|
spi = notify->get_spi(notify);
|
|
|
|
|
|
|
|
if (protocol != PROTO_ESP && protocol != PROTO_AH)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this->child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol,
|
|
|
|
spi, FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
iterator->destroy(iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of task_t.build for initiator
|
|
|
|
*/
|
|
|
|
static status_t build_i(private_child_rekey_t *this, message_t *message)
|
|
|
|
{
|
|
|
|
notify_payload_t *notify;
|
|
|
|
protocol_id_t protocol;
|
|
|
|
u_int32_t spi, reqid;
|
|
|
|
|
2007-03-06 20:56:58 +00:00
|
|
|
/* we just need the rekey notify ... */
|
2007-02-28 14:04:36 +00:00
|
|
|
protocol = this->child_sa->get_protocol(this->child_sa);
|
|
|
|
spi = this->child_sa->get_spi(this->child_sa, TRUE);
|
|
|
|
notify = notify_payload_create_from_protocol_and_type(protocol, REKEY_SA);
|
|
|
|
notify->set_spi(notify, spi);
|
|
|
|
message->add_payload(message, (payload_t*)notify);
|
2007-03-06 20:56:58 +00:00
|
|
|
|
|
|
|
/* ... our CHILD_CREATE task does the hard work for us. */
|
|
|
|
reqid = this->child_sa->get_reqid(this->child_sa);
|
|
|
|
this->child_create->use_reqid(this->child_create, reqid);
|
|
|
|
this->child_create->task.build(&this->child_create->task, message);
|
2007-02-28 14:04:36 +00:00
|
|
|
|
|
|
|
this->child_sa->set_state(this->child_sa, CHILD_REKEYING);
|
|
|
|
|
|
|
|
return NEED_MORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of task_t.process for initiator
|
|
|
|
*/
|
|
|
|
static status_t process_r(private_child_rekey_t *this, message_t *message)
|
|
|
|
{
|
|
|
|
/* let the CHILD_CREATE task process the message */
|
|
|
|
this->child_create->task.process(&this->child_create->task, message);
|
|
|
|
|
|
|
|
find_child(this, message);
|
|
|
|
|
|
|
|
return NEED_MORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of task_t.build for responder
|
|
|
|
*/
|
|
|
|
static status_t build_r(private_child_rekey_t *this, message_t *message)
|
|
|
|
{
|
|
|
|
u_int32_t reqid;
|
|
|
|
|
|
|
|
if (this->child_sa == NULL ||
|
|
|
|
this->child_sa->get_state(this->child_sa) == CHILD_DELETING)
|
|
|
|
{
|
2007-03-21 09:25:36 +00:00
|
|
|
DBG1(DBG_IKE, "unable to rekey, CHILD_SA not found");
|
2007-02-28 14:04:36 +00:00
|
|
|
message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* let the CHILD_CREATE task build the response */
|
|
|
|
reqid = this->child_sa->get_reqid(this->child_sa);
|
|
|
|
this->child_create->use_reqid(this->child_create, reqid);
|
|
|
|
this->child_create->task.build(&this->child_create->task, message);
|
2007-03-20 16:13:21 +00:00
|
|
|
|
|
|
|
if (message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
|
|
|
|
{
|
|
|
|
/* rekeying failed, reuse old child */
|
|
|
|
this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-02-28 14:04:36 +00:00
|
|
|
this->child_sa->set_state(this->child_sa, CHILD_REKEYING);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of task_t.process for initiator
|
|
|
|
*/
|
|
|
|
static status_t process_i(private_child_rekey_t *this, message_t *message)
|
|
|
|
{
|
|
|
|
protocol_id_t protocol;
|
|
|
|
u_int32_t spi;
|
2007-03-21 09:25:36 +00:00
|
|
|
child_sa_t *to_delete;
|
2007-02-28 14:04:36 +00:00
|
|
|
|
|
|
|
this->child_create->task.process(&this->child_create->task, message);
|
2007-03-20 16:13:21 +00:00
|
|
|
if (message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
|
2007-02-28 14:04:36 +00:00
|
|
|
{
|
2007-03-21 09:25:36 +00:00
|
|
|
/* establishing new child failed, reuse old. but not when we
|
|
|
|
* recieved a delete in the meantime */
|
|
|
|
if (!(this->collision &&
|
|
|
|
this->collision->get_type(this->collision) == CHILD_DELETE))
|
|
|
|
{
|
2007-03-21 16:11:14 +00:00
|
|
|
job_t *job;
|
2007-04-11 07:20:39 +00:00
|
|
|
u_int32_t retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
|
|
|
|
|
2007-03-21 16:11:14 +00:00
|
|
|
job = (job_t*)rekey_child_sa_job_create(
|
|
|
|
this->child_sa->get_reqid(this->child_sa),
|
|
|
|
this->child_sa->get_protocol(this->child_sa),
|
|
|
|
this->child_sa->get_spi(this->child_sa, TRUE));
|
|
|
|
DBG1(DBG_IKE, "CHILD_SA rekeying failed, "
|
|
|
|
"trying again in %d seconds", retry);
|
2007-03-21 09:25:36 +00:00
|
|
|
this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
|
2007-03-21 16:11:14 +00:00
|
|
|
charon->event_queue->add_relative(charon->event_queue, job, retry * 1000);
|
2007-03-21 09:25:36 +00:00
|
|
|
}
|
2007-03-20 16:13:21 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-03-21 09:25:36 +00:00
|
|
|
to_delete = this->child_sa;
|
|
|
|
|
|
|
|
/* check for rekey collisions */
|
|
|
|
if (this->collision &&
|
|
|
|
this->collision->get_type(this->collision) == CHILD_REKEY)
|
|
|
|
{
|
2007-03-21 14:42:49 +00:00
|
|
|
chunk_t this_nonce, other_nonce;
|
2007-03-21 09:25:36 +00:00
|
|
|
private_child_rekey_t *other = (private_child_rekey_t*)this->collision;
|
|
|
|
|
2007-03-21 14:42:49 +00:00
|
|
|
this_nonce = this->child_create->get_lower_nonce(this->child_create);
|
|
|
|
other_nonce = other->child_create->get_lower_nonce(other->child_create);
|
|
|
|
|
2007-03-21 09:25:36 +00:00
|
|
|
/* if we have the lower nonce, delete rekeyed SA. If not, delete
|
|
|
|
* the redundant. */
|
2007-03-21 14:42:49 +00:00
|
|
|
if (memcmp(this_nonce.ptr, other_nonce.ptr,
|
|
|
|
min(this_nonce.len, other_nonce.len)) < 0)
|
2007-03-21 09:25:36 +00:00
|
|
|
{
|
|
|
|
DBG1(DBG_IKE, "CHILD_SA rekey collision won, deleting rekeyed child");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBG1(DBG_IKE, "CHILD_SA rekey collision lost, deleting redundant child");
|
|
|
|
to_delete = this->child_create->get_child(this->child_create);
|
|
|
|
if (to_delete == NULL)
|
|
|
|
{
|
|
|
|
/* ooops, should not happen, fallback */
|
|
|
|
to_delete = this->child_sa;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spi = to_delete->get_spi(to_delete, TRUE);
|
|
|
|
protocol = to_delete->get_protocol(to_delete);
|
2007-02-28 14:04:36 +00:00
|
|
|
if (this->ike_sa->delete_child_sa(this->ike_sa, protocol, spi) != SUCCESS)
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of task_t.get_type
|
|
|
|
*/
|
|
|
|
static task_type_t get_type(private_child_rekey_t *this)
|
|
|
|
{
|
|
|
|
return CHILD_REKEY;
|
|
|
|
}
|
|
|
|
|
2007-03-21 09:25:36 +00:00
|
|
|
/**
|
|
|
|
* Implementation of child_rekey_t.collide
|
|
|
|
*/
|
|
|
|
static void collide(private_child_rekey_t *this, task_t *other)
|
|
|
|
{
|
2007-03-22 07:34:42 +00:00
|
|
|
/* the task manager only detects exchange collision, but not if
|
|
|
|
* the collision is for the same child. we check it here. */
|
|
|
|
if (other->get_type(other) == CHILD_REKEY)
|
|
|
|
{
|
|
|
|
private_child_rekey_t *rekey = (private_child_rekey_t*)other;
|
|
|
|
if (rekey == NULL || rekey->child_sa != this->child_sa)
|
|
|
|
{
|
|
|
|
/* not the same child => no collision */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (other->get_type(other) == CHILD_DELETE)
|
|
|
|
{
|
|
|
|
child_delete_t *del = (child_delete_t*)other;
|
|
|
|
if (del == NULL || del->get_child(del) != this->child_sa)
|
|
|
|
{
|
|
|
|
/* not the same child => no collision */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* any other task is not critical for collisisions, ignore */
|
|
|
|
return;
|
|
|
|
}
|
2007-03-21 09:25:36 +00:00
|
|
|
DESTROY_IF(this->collision);
|
|
|
|
this->collision = other;
|
|
|
|
}
|
|
|
|
|
2007-02-28 14:04:36 +00:00
|
|
|
/**
|
|
|
|
* Implementation of task_t.migrate
|
|
|
|
*/
|
|
|
|
static void migrate(private_child_rekey_t *this, ike_sa_t *ike_sa)
|
|
|
|
{
|
|
|
|
this->child_create->task.migrate(&this->child_create->task, ike_sa);
|
2007-03-21 14:42:49 +00:00
|
|
|
DESTROY_IF(this->collision);
|
2007-02-28 14:04:36 +00:00
|
|
|
|
|
|
|
this->ike_sa = ike_sa;
|
2007-03-21 09:25:36 +00:00
|
|
|
this->collision = NULL;
|
2007-02-28 14:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of task_t.destroy
|
|
|
|
*/
|
|
|
|
static void destroy(private_child_rekey_t *this)
|
|
|
|
{
|
|
|
|
this->child_create->task.destroy(&this->child_create->task);
|
2007-03-21 09:25:36 +00:00
|
|
|
DESTROY_IF(this->collision);
|
2007-02-28 14:04:36 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
child_rekey_t *child_rekey_create(ike_sa_t *ike_sa, child_sa_t *child_sa)
|
|
|
|
{
|
2007-04-10 06:01:03 +00:00
|
|
|
child_cfg_t *config;
|
2007-02-28 14:04:36 +00:00
|
|
|
private_child_rekey_t *this = malloc_thing(private_child_rekey_t);
|
|
|
|
|
2007-03-21 09:25:36 +00:00
|
|
|
this->public.collide = (void (*)(child_rekey_t*,task_t*))collide;
|
2007-02-28 14:04:36 +00:00
|
|
|
this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
|
|
|
|
this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
|
|
|
|
this->public.task.destroy = (void(*)(task_t*))destroy;
|
|
|
|
if (child_sa != NULL)
|
|
|
|
{
|
|
|
|
this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
|
|
|
|
this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
|
|
|
|
this->initiator = TRUE;
|
2007-04-10 06:01:03 +00:00
|
|
|
config = child_sa->get_config(child_sa);
|
|
|
|
this->child_create = child_create_create(ike_sa, config);
|
2007-02-28 14:04:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
|
|
|
|
this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
|
|
|
|
this->initiator = FALSE;
|
|
|
|
this->child_create = child_create_create(ike_sa, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->ike_sa = ike_sa;
|
|
|
|
this->child_sa = child_sa;
|
2007-03-21 09:25:36 +00:00
|
|
|
this->collision = NULL;
|
2007-02-28 14:04:36 +00:00
|
|
|
|
|
|
|
return &this->public;
|
|
|
|
}
|