2005-12-04 19:05:52 +00:00
|
|
|
/*
|
2007-02-28 14:04:36 +00:00
|
|
|
* Copyright (C) 2005-2007 Martin Willi
|
2006-06-22 06:36:28 +00:00
|
|
|
* Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-12-04 19:05:52 +00:00
|
|
|
* 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.
|
2008-03-13 14:14:44 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2005-12-04 19:05:52 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
#define _GNU_SOURCE
|
2006-04-26 12:28:02 +00:00
|
|
|
#include "child_sa.h"
|
2005-12-04 19:05:52 +00:00
|
|
|
|
2006-09-08 13:10:52 +00:00
|
|
|
#include <stdio.h>
|
2006-08-28 08:45:22 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2006-02-10 08:20:06 +00:00
|
|
|
#include <daemon.h>
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
ENUM(child_sa_state_names, CHILD_CREATED, CHILD_DELETING,
|
|
|
|
"CREATED",
|
|
|
|
"ROUTED",
|
|
|
|
"INSTALLED",
|
|
|
|
"REKEYING",
|
|
|
|
"DELETING",
|
|
|
|
);
|
2006-07-13 08:26:54 +00:00
|
|
|
|
2006-03-16 15:25:06 +00:00
|
|
|
typedef struct sa_policy_t sa_policy_t;
|
2006-03-01 15:56:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Struct used to store information for a policy. This
|
|
|
|
* is needed since we must provide all this information
|
|
|
|
* for deleting a policy...
|
|
|
|
*/
|
2006-03-16 15:25:06 +00:00
|
|
|
struct sa_policy_t {
|
2006-07-18 12:53:54 +00:00
|
|
|
/**
|
|
|
|
* Traffic selector for us
|
|
|
|
*/
|
|
|
|
traffic_selector_t *my_ts;
|
2006-03-01 15:56:51 +00:00
|
|
|
|
|
|
|
/**
|
2006-07-18 12:53:54 +00:00
|
|
|
* Traffic selector for other
|
2006-03-01 15:56:51 +00:00
|
|
|
*/
|
2006-07-18 12:53:54 +00:00
|
|
|
traffic_selector_t *other_ts;
|
2006-03-01 15:56:51 +00:00
|
|
|
};
|
|
|
|
|
2005-12-04 19:05:52 +00:00
|
|
|
typedef struct private_child_sa_t private_child_sa_t;
|
|
|
|
|
|
|
|
/**
|
2007-07-03 13:49:29 +00:00
|
|
|
* Private data of a child_sa_t object.
|
2005-12-04 19:05:52 +00:00
|
|
|
*/
|
|
|
|
struct private_child_sa_t {
|
|
|
|
/**
|
2005-12-07 08:13:22 +00:00
|
|
|
* Public interface of child_sa_t.
|
2005-12-04 19:05:52 +00:00
|
|
|
*/
|
|
|
|
child_sa_t public;
|
|
|
|
|
2006-06-07 13:26:23 +00:00
|
|
|
struct {
|
|
|
|
/** address of peer */
|
|
|
|
host_t *addr;
|
2006-09-12 13:50:14 +00:00
|
|
|
/** id of peer */
|
|
|
|
identification_t *id;
|
2006-06-07 13:26:23 +00:00
|
|
|
/** actual used SPI, 0 if unused */
|
|
|
|
u_int32_t spi;
|
|
|
|
} me, other;
|
2006-02-10 08:20:06 +00:00
|
|
|
|
2006-06-15 11:06:22 +00:00
|
|
|
/**
|
|
|
|
* Allocated SPI for a ESP proposal candidates
|
|
|
|
*/
|
|
|
|
u_int32_t alloc_esp_spi;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocated SPI for a AH proposal candidates
|
|
|
|
*/
|
|
|
|
u_int32_t alloc_ah_spi;
|
|
|
|
|
2006-02-10 08:20:06 +00:00
|
|
|
/**
|
2006-06-07 13:26:23 +00:00
|
|
|
* Protocol used to protect this SA, ESP|AH
|
2006-02-10 08:20:06 +00:00
|
|
|
*/
|
2006-06-07 13:26:23 +00:00
|
|
|
protocol_id_t protocol;
|
2006-02-16 09:55:07 +00:00
|
|
|
|
|
|
|
/**
|
2006-06-07 13:26:23 +00:00
|
|
|
* List containing sa_policy_t objects
|
2006-03-01 15:56:51 +00:00
|
|
|
*/
|
2006-06-07 13:26:23 +00:00
|
|
|
linked_list_t *policies;
|
2006-03-01 15:56:51 +00:00
|
|
|
|
2006-07-20 10:09:32 +00:00
|
|
|
/**
|
|
|
|
* Seperate list for local traffic selectors
|
|
|
|
*/
|
|
|
|
linked_list_t *my_ts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seperate list for remote traffic selectors
|
|
|
|
*/
|
|
|
|
linked_list_t *other_ts;
|
|
|
|
|
2006-03-01 15:56:51 +00:00
|
|
|
/**
|
2006-06-07 13:26:23 +00:00
|
|
|
* reqid used for this child_sa
|
2006-03-01 15:56:51 +00:00
|
|
|
*/
|
2006-06-07 13:26:23 +00:00
|
|
|
u_int32_t reqid;
|
2006-03-01 15:56:51 +00:00
|
|
|
|
2006-07-20 14:57:49 +00:00
|
|
|
/**
|
|
|
|
* encryption algorithm used for this SA
|
|
|
|
*/
|
2008-03-26 10:06:45 +00:00
|
|
|
u_int16_t enc_alg;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* key size of enc_alg
|
|
|
|
*/
|
|
|
|
u_int16_t enc_size;
|
2006-07-20 14:57:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* integrity protection algorithm used for this SA
|
|
|
|
*/
|
2008-03-26 10:06:45 +00:00
|
|
|
u_int16_t int_alg;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* key size of int_alg
|
|
|
|
*/
|
|
|
|
u_int16_t int_size;
|
2006-07-20 14:57:49 +00:00
|
|
|
|
2006-06-12 07:33:20 +00:00
|
|
|
/**
|
|
|
|
* time, on which SA was installed
|
|
|
|
*/
|
|
|
|
time_t install_time;
|
|
|
|
|
2007-03-20 14:56:41 +00:00
|
|
|
/**
|
|
|
|
* absolute time when rekeying is sceduled
|
|
|
|
*/
|
|
|
|
time_t rekey_time;
|
|
|
|
|
2006-07-13 08:26:54 +00:00
|
|
|
/**
|
|
|
|
* state of the CHILD_SA
|
|
|
|
*/
|
|
|
|
child_sa_state_t state;
|
2006-09-25 05:58:45 +00:00
|
|
|
|
2006-09-08 06:12:02 +00:00
|
|
|
/**
|
2007-06-27 13:10:55 +00:00
|
|
|
* Specifies if UDP encapsulation is enabled (NAT traversal)
|
2006-09-08 06:12:02 +00:00
|
|
|
*/
|
2007-06-27 13:10:55 +00:00
|
|
|
bool encap;
|
2006-12-21 14:35:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mode this SA uses, tunnel/transport
|
|
|
|
*/
|
|
|
|
mode_t mode;
|
2007-02-28 14:04:36 +00:00
|
|
|
|
2007-03-01 12:31:58 +00:00
|
|
|
/**
|
|
|
|
* virtual IP assinged to local host
|
|
|
|
*/
|
|
|
|
host_t *virtual_ip;
|
|
|
|
|
2007-02-28 14:04:36 +00:00
|
|
|
/**
|
2007-04-10 06:01:03 +00:00
|
|
|
* config used to create this child
|
2007-02-28 14:04:36 +00:00
|
|
|
*/
|
2007-04-10 06:01:03 +00:00
|
|
|
child_cfg_t *config;
|
2007-07-03 13:49:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* cached interface name for iptables
|
|
|
|
*/
|
|
|
|
char *iface;
|
2005-12-04 19:05:52 +00:00
|
|
|
};
|
|
|
|
|
2006-08-25 09:07:37 +00:00
|
|
|
/**
|
|
|
|
* Implementation of child_sa_t.get_name.
|
|
|
|
*/
|
|
|
|
static char *get_name(private_child_sa_t *this)
|
|
|
|
{
|
2007-04-10 06:01:03 +00:00
|
|
|
return this->config->get_name(this->config);
|
2006-08-25 09:07:37 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 14:23:15 +00:00
|
|
|
/**
|
|
|
|
* Implements child_sa_t.get_reqid
|
|
|
|
*/
|
|
|
|
static u_int32_t get_reqid(private_child_sa_t *this)
|
|
|
|
{
|
|
|
|
return this->reqid;
|
|
|
|
}
|
2006-06-07 13:26:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements child_sa_t.get_spi
|
|
|
|
*/
|
|
|
|
u_int32_t get_spi(private_child_sa_t *this, bool inbound)
|
|
|
|
{
|
|
|
|
if (inbound)
|
|
|
|
{
|
|
|
|
return this->me.spi;
|
|
|
|
}
|
|
|
|
return this->other.spi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements child_sa_t.get_protocol
|
|
|
|
*/
|
|
|
|
protocol_id_t get_protocol(private_child_sa_t *this)
|
|
|
|
{
|
|
|
|
return this->protocol;
|
|
|
|
}
|
2006-05-31 14:23:15 +00:00
|
|
|
|
2006-07-13 08:26:54 +00:00
|
|
|
/**
|
|
|
|
* Implements child_sa_t.get_state
|
|
|
|
*/
|
|
|
|
static child_sa_state_t get_state(private_child_sa_t *this)
|
|
|
|
{
|
|
|
|
return this->state;
|
|
|
|
}
|
|
|
|
|
2007-02-28 14:04:36 +00:00
|
|
|
/**
|
2007-04-10 06:01:03 +00:00
|
|
|
* Implements child_sa_t.get_config
|
2007-02-28 14:04:36 +00:00
|
|
|
*/
|
2007-04-10 06:01:03 +00:00
|
|
|
static child_cfg_t* get_config(private_child_sa_t *this)
|
2007-02-28 14:04:36 +00:00
|
|
|
{
|
2007-04-10 06:01:03 +00:00
|
|
|
return this->config;
|
2007-02-28 14:04:36 +00:00
|
|
|
}
|
|
|
|
|
2007-04-11 12:14:51 +00:00
|
|
|
/**
|
|
|
|
* Implementation of child_sa_t.get_stats.
|
|
|
|
*/
|
|
|
|
static void get_stats(private_child_sa_t *this, mode_t *mode,
|
|
|
|
encryption_algorithm_t *encr_algo, size_t *encr_len,
|
|
|
|
integrity_algorithm_t *int_algo, size_t *int_len,
|
|
|
|
u_int32_t *rekey, u_int32_t *use_in, u_int32_t *use_out,
|
|
|
|
u_int32_t *use_fwd)
|
|
|
|
{
|
|
|
|
sa_policy_t *policy;
|
|
|
|
iterator_t *iterator;
|
|
|
|
u_int32_t in = 0, out = 0, fwd = 0, time;
|
|
|
|
|
|
|
|
iterator = this->policies->create_iterator(this->policies, TRUE);
|
|
|
|
while (iterator->iterate(iterator, (void**)&policy))
|
|
|
|
{
|
|
|
|
|
|
|
|
if (charon->kernel_interface->query_policy(charon->kernel_interface,
|
|
|
|
policy->other_ts, policy->my_ts, POLICY_IN, &time) == SUCCESS)
|
|
|
|
{
|
|
|
|
in = max(in, time);
|
|
|
|
}
|
|
|
|
if (charon->kernel_interface->query_policy(charon->kernel_interface,
|
|
|
|
policy->my_ts, policy->other_ts, POLICY_OUT, &time) == SUCCESS)
|
|
|
|
{
|
|
|
|
out = max(out, time);
|
|
|
|
}
|
|
|
|
if (charon->kernel_interface->query_policy(charon->kernel_interface,
|
|
|
|
policy->other_ts, policy->my_ts, POLICY_FWD, &time) == SUCCESS)
|
|
|
|
{
|
|
|
|
fwd = max(fwd, time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iterator->destroy(iterator);
|
|
|
|
|
|
|
|
*mode = this->mode;
|
2008-03-26 10:06:45 +00:00
|
|
|
*encr_algo = this->enc_alg;
|
|
|
|
*encr_len = this->enc_size;
|
|
|
|
*int_algo = this->int_alg;
|
|
|
|
*int_len = this->int_size;
|
2007-04-11 12:14:51 +00:00
|
|
|
*rekey = this->rekey_time;
|
|
|
|
*use_in = in;
|
|
|
|
*use_out = out;
|
|
|
|
*use_fwd = fwd;
|
|
|
|
}
|
|
|
|
|
2006-09-12 13:50:14 +00:00
|
|
|
/**
|
|
|
|
* Run the up/down script
|
|
|
|
*/
|
|
|
|
static void updown(private_child_sa_t *this, bool up)
|
|
|
|
{
|
2006-10-24 14:20:45 +00:00
|
|
|
sa_policy_t *policy;
|
2006-09-12 13:50:14 +00:00
|
|
|
iterator_t *iterator;
|
2007-02-28 14:04:36 +00:00
|
|
|
char *script;
|
2006-09-12 13:50:14 +00:00
|
|
|
|
2007-04-10 06:01:03 +00:00
|
|
|
script = this->config->get_updown(this->config);
|
2007-02-28 14:04:36 +00:00
|
|
|
|
|
|
|
if (script == NULL)
|
2006-09-12 13:50:14 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator = this->policies->create_iterator(this->policies, TRUE);
|
2006-10-24 14:20:45 +00:00
|
|
|
while (iterator->iterate(iterator, (void**)&policy))
|
2006-09-12 13:50:14 +00:00
|
|
|
{
|
|
|
|
char command[1024];
|
|
|
|
char *my_client, *other_client, *my_client_mask, *other_client_mask;
|
2007-03-01 12:31:58 +00:00
|
|
|
char *pos, *virtual_ip;
|
2006-09-12 13:50:14 +00:00
|
|
|
FILE *shell;
|
2007-07-03 13:49:29 +00:00
|
|
|
|
2006-09-12 13:50:14 +00:00
|
|
|
/* get subnet/bits from string */
|
2006-10-18 11:46:13 +00:00
|
|
|
asprintf(&my_client, "%R", policy->my_ts);
|
2006-09-12 13:50:14 +00:00
|
|
|
pos = strchr(my_client, '/');
|
|
|
|
*pos = '\0';
|
|
|
|
my_client_mask = pos + 1;
|
|
|
|
pos = strchr(my_client_mask, '[');
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
*pos = '\0';
|
|
|
|
}
|
2006-10-18 11:46:13 +00:00
|
|
|
asprintf(&other_client, "%R", policy->other_ts);
|
2006-09-12 13:50:14 +00:00
|
|
|
pos = strchr(other_client, '/');
|
|
|
|
*pos = '\0';
|
|
|
|
other_client_mask = pos + 1;
|
|
|
|
pos = strchr(other_client_mask, '[');
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
*pos = '\0';
|
|
|
|
}
|
2007-03-01 07:45:43 +00:00
|
|
|
|
2007-07-03 13:49:29 +00:00
|
|
|
if (this->virtual_ip)
|
|
|
|
{
|
|
|
|
asprintf(&virtual_ip, "PLUTO_MY_SOURCEIP='%H' ",
|
|
|
|
this->virtual_ip);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asprintf(&virtual_ip, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we cache the iface name, as it may not be available when
|
|
|
|
* the SA gets deleted */
|
|
|
|
if (up)
|
|
|
|
{
|
|
|
|
free(this->iface);
|
|
|
|
this->iface = charon->kernel_interface->get_interface(
|
|
|
|
charon->kernel_interface, this->me.addr);
|
|
|
|
}
|
2006-09-12 13:50:14 +00:00
|
|
|
|
|
|
|
/* build the command with all env variables.
|
2007-03-01 07:45:43 +00:00
|
|
|
* TODO: PLUTO_PEER_CA and PLUTO_NEXT_HOP are currently missing
|
|
|
|
*/
|
2006-09-12 13:50:14 +00:00
|
|
|
snprintf(command, sizeof(command),
|
|
|
|
"2>&1 "
|
|
|
|
"PLUTO_VERSION='1.1' "
|
|
|
|
"PLUTO_VERB='%s%s%s' "
|
|
|
|
"PLUTO_CONNECTION='%s' "
|
|
|
|
"PLUTO_INTERFACE='%s' "
|
|
|
|
"PLUTO_REQID='%u' "
|
2006-09-27 14:14:44 +00:00
|
|
|
"PLUTO_ME='%H' "
|
|
|
|
"PLUTO_MY_ID='%D' "
|
2006-09-12 13:50:14 +00:00
|
|
|
"PLUTO_MY_CLIENT='%s/%s' "
|
|
|
|
"PLUTO_MY_CLIENT_NET='%s' "
|
|
|
|
"PLUTO_MY_CLIENT_MASK='%s' "
|
|
|
|
"PLUTO_MY_PORT='%u' "
|
|
|
|
"PLUTO_MY_PROTOCOL='%u' "
|
2006-09-27 14:14:44 +00:00
|
|
|
"PLUTO_PEER='%H' "
|
|
|
|
"PLUTO_PEER_ID='%D' "
|
2006-09-12 13:50:14 +00:00
|
|
|
"PLUTO_PEER_CLIENT='%s/%s' "
|
|
|
|
"PLUTO_PEER_CLIENT_NET='%s' "
|
|
|
|
"PLUTO_PEER_CLIENT_MASK='%s' "
|
|
|
|
"PLUTO_PEER_PORT='%u' "
|
|
|
|
"PLUTO_PEER_PROTOCOL='%u' "
|
2006-09-25 05:58:45 +00:00
|
|
|
"%s"
|
2007-03-01 07:45:43 +00:00
|
|
|
"%s"
|
2006-09-12 13:50:14 +00:00
|
|
|
"%s",
|
|
|
|
up ? "up" : "down",
|
2006-10-18 11:46:13 +00:00
|
|
|
policy->my_ts->is_host(policy->my_ts,
|
|
|
|
this->me.addr) ? "-host" : "-client",
|
2007-11-06 13:45:54 +00:00
|
|
|
this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-v6",
|
2007-04-10 06:01:03 +00:00
|
|
|
this->config->get_name(this->config),
|
2007-07-03 13:49:29 +00:00
|
|
|
this->iface ? this->iface : "unknown",
|
2006-09-12 13:50:14 +00:00
|
|
|
this->reqid,
|
2006-09-27 14:14:44 +00:00
|
|
|
this->me.addr,
|
|
|
|
this->me.id,
|
2006-09-12 13:50:14 +00:00
|
|
|
my_client, my_client_mask,
|
|
|
|
my_client, my_client_mask,
|
|
|
|
policy->my_ts->get_from_port(policy->my_ts),
|
|
|
|
policy->my_ts->get_protocol(policy->my_ts),
|
2006-09-27 14:14:44 +00:00
|
|
|
this->other.addr,
|
|
|
|
this->other.id,
|
2006-09-12 13:50:14 +00:00
|
|
|
other_client, other_client_mask,
|
|
|
|
other_client, other_client_mask,
|
|
|
|
policy->other_ts->get_from_port(policy->other_ts),
|
|
|
|
policy->other_ts->get_protocol(policy->other_ts),
|
2007-03-01 07:45:43 +00:00
|
|
|
virtual_ip,
|
2007-04-10 06:01:03 +00:00
|
|
|
this->config->get_hostaccess(this->config) ?
|
2007-03-01 07:45:43 +00:00
|
|
|
"PLUTO_HOST_ACCESS='1' " : "",
|
|
|
|
script);
|
2006-09-12 13:50:14 +00:00
|
|
|
free(my_client);
|
|
|
|
free(other_client);
|
2007-03-01 07:45:43 +00:00
|
|
|
free(virtual_ip);
|
2006-09-12 13:50:14 +00:00
|
|
|
|
2007-07-02 09:49:22 +00:00
|
|
|
DBG3(DBG_CHD, "running updown script: %s", command);
|
2006-09-12 13:50:14 +00:00
|
|
|
shell = popen(command, "r");
|
|
|
|
|
|
|
|
if (shell == NULL)
|
|
|
|
{
|
2007-02-28 14:04:36 +00:00
|
|
|
DBG1(DBG_CHD, "could not execute updown script '%s'", script);
|
2006-09-12 13:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
char resp[128];
|
|
|
|
|
|
|
|
if (fgets(resp, sizeof(resp), shell) == NULL)
|
|
|
|
{
|
|
|
|
if (ferror(shell))
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_CHD, "error reading output from updown script");
|
2006-09-12 13:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *e = resp + strlen(resp);
|
|
|
|
if (e > resp && e[-1] == '\n')
|
|
|
|
{ /* trim trailing '\n' */
|
|
|
|
e[-1] = '\0';
|
|
|
|
}
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_CHD, "updown: %s", resp);
|
2006-09-12 13:50:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(shell);
|
|
|
|
}
|
|
|
|
iterator->destroy(iterator);
|
|
|
|
}
|
|
|
|
|
2006-07-13 08:26:54 +00:00
|
|
|
/**
|
|
|
|
* Implements child_sa_t.set_state
|
|
|
|
*/
|
|
|
|
static void set_state(private_child_sa_t *this, child_sa_state_t state)
|
|
|
|
{
|
|
|
|
this->state = state;
|
2006-09-12 13:50:14 +00:00
|
|
|
if (state == CHILD_INSTALLED)
|
|
|
|
{
|
|
|
|
updown(this, TRUE);
|
|
|
|
}
|
2006-07-13 08:26:54 +00:00
|
|
|
}
|
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/**
|
|
|
|
* Allocate SPI for a single proposal
|
|
|
|
*/
|
|
|
|
static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
|
|
|
|
{
|
|
|
|
protocol_id_t protocol = proposal->get_protocol(proposal);
|
|
|
|
|
|
|
|
if (protocol == PROTO_AH)
|
|
|
|
{
|
|
|
|
/* get a new spi for AH, if not already done */
|
|
|
|
if (this->alloc_ah_spi == 0)
|
|
|
|
{
|
|
|
|
if (charon->kernel_interface->get_spi(
|
|
|
|
charon->kernel_interface,
|
|
|
|
this->other.addr, this->me.addr,
|
|
|
|
PROTO_AH, this->reqid,
|
|
|
|
&this->alloc_ah_spi) != SUCCESS)
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proposal->set_spi(proposal, this->alloc_ah_spi);
|
|
|
|
}
|
|
|
|
if (protocol == PROTO_ESP)
|
|
|
|
{
|
|
|
|
/* get a new spi for ESP, if not already done */
|
|
|
|
if (this->alloc_esp_spi == 0)
|
|
|
|
{
|
|
|
|
if (charon->kernel_interface->get_spi(
|
|
|
|
charon->kernel_interface,
|
|
|
|
this->other.addr, this->me.addr,
|
|
|
|
PROTO_ESP, this->reqid,
|
|
|
|
&this->alloc_esp_spi) != SUCCESS)
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proposal->set_spi(proposal, this->alloc_esp_spi);
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-04 19:05:52 +00:00
|
|
|
/**
|
2006-02-16 09:55:07 +00:00
|
|
|
* Implements child_sa_t.alloc
|
2005-12-04 19:05:52 +00:00
|
|
|
*/
|
2006-02-16 09:55:07 +00:00
|
|
|
static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
|
2005-12-04 19:05:52 +00:00
|
|
|
{
|
2006-02-16 09:55:07 +00:00
|
|
|
iterator_t *iterator;
|
|
|
|
proposal_t *proposal;
|
|
|
|
|
2006-06-15 11:06:22 +00:00
|
|
|
/* iterator through proposals to update spis */
|
2006-02-16 09:55:07 +00:00
|
|
|
iterator = proposals->create_iterator(proposals, TRUE);
|
2006-10-24 14:20:45 +00:00
|
|
|
while(iterator->iterate(iterator, (void**)&proposal))
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
2006-06-22 06:36:28 +00:00
|
|
|
if (alloc_proposal(this, proposal) != SUCCESS)
|
2006-06-15 11:06:22 +00:00
|
|
|
{
|
2006-06-22 06:36:28 +00:00
|
|
|
iterator->destroy(iterator);
|
|
|
|
return FAILED;
|
2006-06-15 11:06:22 +00:00
|
|
|
}
|
2006-02-16 09:55:07 +00:00
|
|
|
}
|
|
|
|
iterator->destroy(iterator);
|
|
|
|
return SUCCESS;
|
2005-12-04 19:05:52 +00:00
|
|
|
}
|
|
|
|
|
2006-12-21 14:35:17 +00:00
|
|
|
static status_t install(private_child_sa_t *this, proposal_t *proposal,
|
|
|
|
mode_t mode, prf_plus_t *prf_plus, bool mine)
|
2005-12-04 19:05:52 +00:00
|
|
|
{
|
2008-03-26 10:06:45 +00:00
|
|
|
u_int32_t spi, soft, hard;
|
2006-02-16 09:55:07 +00:00
|
|
|
host_t *src;
|
|
|
|
host_t *dst;
|
|
|
|
status_t status;
|
2006-02-10 08:20:06 +00:00
|
|
|
|
2006-06-07 13:26:23 +00:00
|
|
|
this->protocol = proposal->get_protocol(proposal);
|
|
|
|
|
|
|
|
/* now we have to decide which spi to use. Use self allocated, if "mine",
|
2006-06-15 11:06:22 +00:00
|
|
|
* or the one in the proposal, if not "mine" (others). Additionally,
|
|
|
|
* source and dest host switch depending on the role */
|
2006-06-07 13:26:23 +00:00
|
|
|
if (mine)
|
2006-02-10 08:20:06 +00:00
|
|
|
{
|
2006-06-15 11:06:22 +00:00
|
|
|
/* if we have allocated SPIs for AH and ESP, we must delete the unused
|
|
|
|
* one. */
|
|
|
|
if (this->protocol == PROTO_ESP)
|
|
|
|
{
|
|
|
|
this->me.spi = this->alloc_esp_spi;
|
|
|
|
if (this->alloc_ah_spi)
|
|
|
|
{
|
|
|
|
charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
|
|
|
|
this->alloc_ah_spi, PROTO_AH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->me.spi = this->alloc_ah_spi;
|
|
|
|
if (this->alloc_esp_spi)
|
|
|
|
{
|
|
|
|
charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
|
|
|
|
this->alloc_esp_spi, PROTO_ESP);
|
|
|
|
}
|
|
|
|
}
|
2006-06-07 13:26:23 +00:00
|
|
|
spi = this->me.spi;
|
2006-06-15 11:06:22 +00:00
|
|
|
dst = this->me.addr;
|
|
|
|
src = this->other.addr;
|
2006-02-10 08:20:06 +00:00
|
|
|
}
|
2006-06-07 13:26:23 +00:00
|
|
|
else
|
|
|
|
{
|
2006-06-15 11:06:22 +00:00
|
|
|
this->other.spi = proposal->get_spi(proposal);
|
|
|
|
spi = this->other.spi;
|
|
|
|
src = this->me.addr;
|
|
|
|
dst = this->other.addr;
|
2006-06-07 13:26:23 +00:00
|
|
|
}
|
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
|
2006-10-18 11:46:13 +00:00
|
|
|
protocol_id_names, this->protocol);
|
2006-06-09 07:31:30 +00:00
|
|
|
|
|
|
|
/* select encryption algo */
|
2008-03-26 10:06:45 +00:00
|
|
|
if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
|
|
|
|
&this->enc_alg, &this->enc_size))
|
2006-06-07 13:26:23 +00:00
|
|
|
{
|
2008-03-26 10:06:45 +00:00
|
|
|
DBG2(DBG_CHD, " using %N for encryption",
|
|
|
|
encryption_algorithm_names, this->enc_alg);
|
2006-06-07 13:26:23 +00:00
|
|
|
}
|
|
|
|
|
2006-06-09 07:31:30 +00:00
|
|
|
/* select integrity algo */
|
2008-03-26 10:06:45 +00:00
|
|
|
if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
|
|
|
|
&this->int_alg, &this->int_size))
|
2006-06-07 13:26:23 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_CHD, " using %N for integrity",
|
2008-03-26 10:06:45 +00:00
|
|
|
integrity_algorithm_names, this->int_alg);
|
2006-06-07 13:26:23 +00:00
|
|
|
}
|
2007-04-10 06:01:03 +00:00
|
|
|
soft = this->config->get_lifetime(this->config, TRUE);
|
|
|
|
hard = this->config->get_lifetime(this->config, FALSE);
|
2006-06-22 06:36:28 +00:00
|
|
|
|
2006-06-09 07:31:30 +00:00
|
|
|
/* send SA down to the kernel */
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
|
2006-06-07 13:26:23 +00:00
|
|
|
status = charon->kernel_interface->add_sa(charon->kernel_interface,
|
2008-03-26 10:06:45 +00:00
|
|
|
src, dst, spi, this->protocol, this->reqid, mine ? soft : 0, hard,
|
|
|
|
this->enc_alg, this->enc_size, this->int_alg, this->int_size,
|
|
|
|
prf_plus, mode, this->encap, mine);
|
|
|
|
|
2006-06-12 07:33:20 +00:00
|
|
|
this->install_time = time(NULL);
|
2007-04-11 12:14:51 +00:00
|
|
|
this->rekey_time = this->install_time + soft;
|
2006-06-07 13:26:23 +00:00
|
|
|
return status;
|
2006-02-16 09:55:07 +00:00
|
|
|
}
|
|
|
|
|
2006-12-21 14:35:17 +00:00
|
|
|
static status_t add(private_child_sa_t *this, proposal_t *proposal,
|
|
|
|
mode_t mode, prf_plus_t *prf_plus)
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
2006-06-19 08:11:42 +00:00
|
|
|
u_int32_t outbound_spi, inbound_spi;
|
2006-02-16 09:55:07 +00:00
|
|
|
|
2006-06-19 08:11:42 +00:00
|
|
|
/* backup outbound spi, as alloc overwrites it */
|
|
|
|
outbound_spi = proposal->get_spi(proposal);
|
2006-02-16 09:55:07 +00:00
|
|
|
|
2006-06-19 08:11:42 +00:00
|
|
|
/* get SPIs inbound SAs */
|
2006-06-22 06:36:28 +00:00
|
|
|
if (alloc_proposal(this, proposal) != SUCCESS)
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
2006-06-19 08:11:42 +00:00
|
|
|
inbound_spi = proposal->get_spi(proposal);
|
2006-02-16 09:55:07 +00:00
|
|
|
|
2006-06-19 08:11:42 +00:00
|
|
|
/* install inbound SAs */
|
2006-12-21 14:35:17 +00:00
|
|
|
if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
2006-02-22 16:14:40 +00:00
|
|
|
|
2006-06-19 08:11:42 +00:00
|
|
|
/* install outbound SAs, restore spi*/
|
|
|
|
proposal->set_spi(proposal, outbound_spi);
|
2006-12-21 14:35:17 +00:00
|
|
|
if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
|
2006-06-19 08:11:42 +00:00
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
proposal->set_spi(proposal, inbound_spi);
|
|
|
|
|
2006-02-16 09:55:07 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-12-21 14:35:17 +00:00
|
|
|
static status_t update(private_child_sa_t *this, proposal_t *proposal,
|
|
|
|
mode_t mode, prf_plus_t *prf_plus)
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
2006-06-19 08:11:42 +00:00
|
|
|
u_int32_t inbound_spi;
|
|
|
|
|
|
|
|
/* backup received spi, as install() overwrites it */
|
|
|
|
inbound_spi = proposal->get_spi(proposal);
|
|
|
|
|
|
|
|
/* install outbound SAs */
|
2006-12-21 14:35:17 +00:00
|
|
|
if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
2006-06-19 08:11:42 +00:00
|
|
|
|
|
|
|
/* restore spi */
|
|
|
|
proposal->set_spi(proposal, inbound_spi);
|
|
|
|
/* install inbound SAs */
|
2006-12-21 14:35:17 +00:00
|
|
|
if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
2006-02-22 16:14:40 +00:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-12-21 14:35:17 +00:00
|
|
|
static status_t add_policies(private_child_sa_t *this,
|
|
|
|
linked_list_t *my_ts_list,
|
|
|
|
linked_list_t *other_ts_list, mode_t mode)
|
2006-02-22 16:14:40 +00:00
|
|
|
{
|
2006-03-01 15:56:51 +00:00
|
|
|
iterator_t *my_iter, *other_iter;
|
|
|
|
traffic_selector_t *my_ts, *other_ts;
|
2006-09-08 13:10:52 +00:00
|
|
|
/* use low prio for ROUTED policies */
|
|
|
|
bool high_prio = (this->state != CHILD_CREATED);
|
2006-02-22 16:14:40 +00:00
|
|
|
|
2006-03-01 15:56:51 +00:00
|
|
|
/* iterate over both lists */
|
|
|
|
my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
|
|
|
|
other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
|
2006-10-24 14:20:45 +00:00
|
|
|
while (my_iter->iterate(my_iter, (void**)&my_ts))
|
2006-02-22 16:14:40 +00:00
|
|
|
{
|
2006-03-01 15:56:51 +00:00
|
|
|
other_iter->reset(other_iter);
|
2006-10-24 14:20:45 +00:00
|
|
|
while (other_iter->iterate(other_iter, (void**)&other_ts))
|
2006-03-01 15:56:51 +00:00
|
|
|
{
|
|
|
|
/* set up policies for every entry in my_ts_list to every entry in other_ts_list */
|
|
|
|
status_t status;
|
2006-07-18 12:53:54 +00:00
|
|
|
sa_policy_t *policy;
|
2006-03-01 15:56:51 +00:00
|
|
|
|
2006-07-18 12:53:54 +00:00
|
|
|
if (my_ts->get_type(my_ts) != other_ts->get_type(other_ts))
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_CHD,
|
2007-05-15 19:03:23 +00:00
|
|
|
"CHILD_SA policy uses two different IP families - ignored");
|
2006-07-18 12:53:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-05 13:11:55 +00:00
|
|
|
/* only set up policies if protocol matches, or if one is zero (any) */
|
|
|
|
if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts) &&
|
|
|
|
my_ts->get_protocol(my_ts) && other_ts->get_protocol(other_ts))
|
2006-03-01 15:56:51 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_CHD,
|
2007-05-15 19:03:23 +00:00
|
|
|
"CHILD_SA policy uses two different protocols - ignored");
|
2006-03-01 15:56:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* install 3 policies: out, in and forward */
|
|
|
|
status = charon->kernel_interface->add_policy(charon->kernel_interface,
|
2006-12-21 14:35:17 +00:00
|
|
|
this->me.addr, this->other.addr, my_ts, other_ts, POLICY_OUT,
|
2007-07-02 09:49:22 +00:00
|
|
|
this->protocol, this->reqid, high_prio, mode);
|
2006-07-18 12:53:54 +00:00
|
|
|
|
2006-03-01 15:56:51 +00:00
|
|
|
status |= charon->kernel_interface->add_policy(charon->kernel_interface,
|
2006-12-21 14:35:17 +00:00
|
|
|
this->other.addr, this->me.addr, other_ts, my_ts, POLICY_IN,
|
2007-07-02 09:49:22 +00:00
|
|
|
this->protocol, this->reqid, high_prio, mode);
|
2006-07-18 12:53:54 +00:00
|
|
|
|
2006-03-01 15:56:51 +00:00
|
|
|
status |= charon->kernel_interface->add_policy(charon->kernel_interface,
|
2006-12-21 14:35:17 +00:00
|
|
|
this->other.addr, this->me.addr, other_ts, my_ts, POLICY_FWD,
|
2007-07-02 09:49:22 +00:00
|
|
|
this->protocol, this->reqid, high_prio, mode);
|
2006-03-01 15:56:51 +00:00
|
|
|
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
my_iter->destroy(my_iter);
|
|
|
|
other_iter->destroy(other_iter);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2006-07-18 12:53:54 +00:00
|
|
|
/* store policy to delete/update them later */
|
|
|
|
policy = malloc_thing(sa_policy_t);
|
|
|
|
policy->my_ts = my_ts->clone(my_ts);
|
|
|
|
policy->other_ts = other_ts->clone(other_ts);
|
2007-04-10 06:01:03 +00:00
|
|
|
this->policies->insert_last(this->policies, policy);
|
2006-07-20 10:09:32 +00:00
|
|
|
/* add to separate list to query them via get_*_traffic_selectors() */
|
2007-04-10 06:01:03 +00:00
|
|
|
this->my_ts->insert_last(this->my_ts, policy->my_ts);
|
|
|
|
this->other_ts->insert_last(this->other_ts, policy->other_ts);
|
2006-03-01 15:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
my_iter->destroy(my_iter);
|
|
|
|
other_iter->destroy(other_iter);
|
2006-07-21 13:31:53 +00:00
|
|
|
|
|
|
|
/* switch to routed state if no SAD entry set up */
|
|
|
|
if (this->state == CHILD_CREATED)
|
|
|
|
{
|
|
|
|
this->state = CHILD_ROUTED;
|
|
|
|
}
|
2006-12-21 14:35:17 +00:00
|
|
|
/* needed to update hosts */
|
|
|
|
this->mode = mode;
|
2006-03-01 15:56:51 +00:00
|
|
|
return SUCCESS;
|
2006-02-16 09:55:07 +00:00
|
|
|
}
|
|
|
|
|
2006-07-20 10:09:32 +00:00
|
|
|
/**
|
2007-04-10 06:01:03 +00:00
|
|
|
* Implementation of child_sa_t.get_traffic_selectors.
|
2006-07-20 10:09:32 +00:00
|
|
|
*/
|
2007-04-10 06:01:03 +00:00
|
|
|
static linked_list_t *get_traffic_selectors(private_child_sa_t *this, bool local)
|
2006-07-20 10:09:32 +00:00
|
|
|
{
|
2007-04-10 06:01:03 +00:00
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
return this->my_ts;
|
|
|
|
}
|
2006-07-20 10:09:32 +00:00
|
|
|
return this->other_ts;
|
|
|
|
}
|
|
|
|
|
2006-06-23 14:02:30 +00:00
|
|
|
/**
|
|
|
|
* Implementation of child_sa_t.get_use_time
|
|
|
|
*/
|
|
|
|
static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
|
|
|
|
{
|
2006-07-18 12:53:54 +00:00
|
|
|
iterator_t *iterator;
|
|
|
|
sa_policy_t *policy;
|
2006-07-27 12:18:40 +00:00
|
|
|
status_t status = FAILED;
|
2006-06-23 14:02:30 +00:00
|
|
|
|
|
|
|
*use_time = UNDEFINED_TIME;
|
2006-07-18 12:53:54 +00:00
|
|
|
|
|
|
|
iterator = this->policies->create_iterator(this->policies, TRUE);
|
|
|
|
while (iterator->iterate(iterator, (void**)&policy))
|
2006-07-12 11:42:36 +00:00
|
|
|
{
|
2006-07-18 12:53:54 +00:00
|
|
|
if (inbound)
|
|
|
|
{
|
|
|
|
time_t in = UNDEFINED_TIME, fwd = UNDEFINED_TIME;
|
|
|
|
|
|
|
|
status = charon->kernel_interface->query_policy(
|
|
|
|
charon->kernel_interface,
|
|
|
|
policy->other_ts, policy->my_ts,
|
|
|
|
POLICY_IN, (u_int32_t*)&in);
|
|
|
|
status |= charon->kernel_interface->query_policy(
|
|
|
|
charon->kernel_interface,
|
|
|
|
policy->other_ts, policy->my_ts,
|
|
|
|
POLICY_FWD, (u_int32_t*)&fwd);
|
|
|
|
*use_time = max(in, fwd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = charon->kernel_interface->query_policy(
|
|
|
|
charon->kernel_interface,
|
|
|
|
policy->my_ts, policy->other_ts,
|
|
|
|
POLICY_OUT, (u_int32_t*)use_time);
|
|
|
|
}
|
2006-07-12 11:42:36 +00:00
|
|
|
}
|
2006-07-18 12:53:54 +00:00
|
|
|
iterator->destroy(iterator);
|
2006-07-12 11:42:36 +00:00
|
|
|
return status;
|
2006-06-23 14:02:30 +00:00
|
|
|
}
|
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/**
|
|
|
|
* Implementation of child_sa_t.update_hosts.
|
|
|
|
*/
|
2007-06-27 13:10:55 +00:00
|
|
|
static status_t update_hosts(private_child_sa_t *this,
|
|
|
|
host_t *me, host_t *other, bool encap)
|
2006-06-22 06:36:28 +00:00
|
|
|
{
|
2007-06-26 13:04:13 +00:00
|
|
|
/* anything changed at all? */
|
2007-07-03 12:32:38 +00:00
|
|
|
if (me->equals(me, this->me.addr) &&
|
|
|
|
other->equals(other, this->other.addr) && this->encap == encap)
|
2006-06-22 06:36:28 +00:00
|
|
|
{
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2007-07-02 09:49:22 +00:00
|
|
|
/* run updown script to remove iptables rules */
|
|
|
|
updown(this, FALSE);
|
|
|
|
|
2007-07-03 12:32:38 +00:00
|
|
|
this->encap = encap;
|
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/* update our (initator) SAs */
|
2007-07-02 12:55:07 +00:00
|
|
|
charon->kernel_interface->update_sa(charon->kernel_interface, this->me.spi,
|
2007-07-03 12:32:38 +00:00
|
|
|
this->protocol, this->other.addr, this->me.addr, other, me, encap);
|
2006-06-22 06:36:28 +00:00
|
|
|
/* update his (responder) SAs */
|
2007-07-02 12:55:07 +00:00
|
|
|
charon->kernel_interface->update_sa(charon->kernel_interface, this->other.spi,
|
2007-07-03 12:32:38 +00:00
|
|
|
this->protocol, this->me.addr, this->other.addr, me, other, encap);
|
2006-06-22 06:36:28 +00:00
|
|
|
|
|
|
|
/* update policies */
|
2007-06-26 13:04:13 +00:00
|
|
|
if (!me->ip_equals(me, this->me.addr) ||
|
|
|
|
!other->ip_equals(other, this->other.addr))
|
2006-06-22 06:36:28 +00:00
|
|
|
{
|
2007-06-26 13:04:13 +00:00
|
|
|
iterator_t *iterator;
|
|
|
|
sa_policy_t *policy;
|
|
|
|
|
|
|
|
/* always use high priorities, as hosts getting updated are INSTALLED */
|
|
|
|
iterator = this->policies->create_iterator(this->policies, TRUE);
|
|
|
|
while (iterator->iterate(iterator, (void**)&policy))
|
2006-06-22 06:36:28 +00:00
|
|
|
{
|
2007-07-02 09:49:22 +00:00
|
|
|
/* remove old policies first */
|
|
|
|
charon->kernel_interface->del_policy(charon->kernel_interface,
|
|
|
|
policy->my_ts, policy->other_ts, POLICY_OUT);
|
|
|
|
charon->kernel_interface->del_policy(charon->kernel_interface,
|
|
|
|
policy->other_ts, policy->my_ts, POLICY_IN);
|
|
|
|
charon->kernel_interface->del_policy(charon->kernel_interface,
|
|
|
|
policy->other_ts, policy->my_ts, POLICY_FWD);
|
|
|
|
|
|
|
|
/* check wether we have to update a "dynamic" traffic selector */
|
|
|
|
if (!me->ip_equals(me, this->me.addr) &&
|
|
|
|
policy->my_ts->is_host(policy->my_ts, this->me.addr))
|
|
|
|
{
|
|
|
|
policy->my_ts->set_address(policy->my_ts, me);
|
|
|
|
}
|
|
|
|
if (!other->ip_equals(other, this->other.addr) &&
|
|
|
|
policy->other_ts->is_host(policy->other_ts, this->other.addr))
|
|
|
|
{
|
|
|
|
policy->other_ts->set_address(policy->other_ts, other);
|
|
|
|
}
|
2007-09-24 12:15:25 +00:00
|
|
|
|
|
|
|
/* we reinstall the virtual IP to handle interface romaing
|
|
|
|
* correctly */
|
|
|
|
if (this->virtual_ip)
|
|
|
|
{
|
|
|
|
charon->kernel_interface->del_ip(charon->kernel_interface,
|
|
|
|
this->virtual_ip);
|
|
|
|
charon->kernel_interface->add_ip(charon->kernel_interface,
|
|
|
|
this->virtual_ip, me);
|
|
|
|
}
|
2007-07-02 09:49:22 +00:00
|
|
|
|
|
|
|
/* reinstall updated policies */
|
2007-07-02 12:55:07 +00:00
|
|
|
charon->kernel_interface->add_policy(charon->kernel_interface,
|
|
|
|
me, other, policy->my_ts, policy->other_ts, POLICY_OUT,
|
|
|
|
this->protocol, this->reqid, TRUE, this->mode);
|
|
|
|
charon->kernel_interface->add_policy(charon->kernel_interface,
|
|
|
|
other, me, policy->other_ts, policy->my_ts, POLICY_IN,
|
|
|
|
this->protocol, this->reqid, TRUE, this->mode);
|
|
|
|
charon->kernel_interface->add_policy(charon->kernel_interface,
|
|
|
|
other, me, policy->other_ts, policy->my_ts, POLICY_FWD,
|
|
|
|
this->protocol, this->reqid, TRUE, this->mode);
|
2006-06-22 06:36:28 +00:00
|
|
|
}
|
2007-06-26 13:04:13 +00:00
|
|
|
iterator->destroy(iterator);
|
2006-06-22 06:36:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-02 09:49:22 +00:00
|
|
|
/* apply hosts */
|
2007-06-26 13:04:13 +00:00
|
|
|
if (!me->equals(me, this->me.addr))
|
2006-06-22 06:36:28 +00:00
|
|
|
{
|
|
|
|
this->me.addr->destroy(this->me.addr);
|
2007-06-26 13:04:13 +00:00
|
|
|
this->me.addr = me->clone(me);
|
2006-06-22 06:36:28 +00:00
|
|
|
}
|
2007-07-02 12:55:07 +00:00
|
|
|
if (!other->equals(other, this->other.addr))
|
2006-06-22 06:36:28 +00:00
|
|
|
{
|
|
|
|
this->other.addr->destroy(this->other.addr);
|
2007-06-26 13:04:13 +00:00
|
|
|
this->other.addr = other->clone(other);
|
|
|
|
}
|
2007-07-02 09:49:22 +00:00
|
|
|
|
|
|
|
/* install new iptables rules */
|
|
|
|
updown(this, TRUE);
|
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-03-01 12:31:58 +00:00
|
|
|
/**
|
|
|
|
* Implementation of child_sa_t.set_virtual_ip.
|
|
|
|
*/
|
|
|
|
static void set_virtual_ip(private_child_sa_t *this, host_t *ip)
|
|
|
|
{
|
|
|
|
this->virtual_ip = ip->clone(ip);
|
|
|
|
}
|
|
|
|
|
2006-02-16 09:55:07 +00:00
|
|
|
/**
|
|
|
|
* Implementation of child_sa_t.destroy.
|
|
|
|
*/
|
|
|
|
static void destroy(private_child_sa_t *this)
|
|
|
|
{
|
2006-03-16 15:25:06 +00:00
|
|
|
sa_policy_t *policy;
|
2006-03-01 15:56:51 +00:00
|
|
|
|
2006-09-12 13:50:14 +00:00
|
|
|
if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
|
|
|
|
{
|
|
|
|
updown(this, FALSE);
|
|
|
|
}
|
|
|
|
|
2006-03-01 15:56:51 +00:00
|
|
|
/* delete SAs in the kernel, if they are set up */
|
2006-06-15 11:06:22 +00:00
|
|
|
if (this->me.spi)
|
2006-03-01 15:56:51 +00:00
|
|
|
{
|
|
|
|
charon->kernel_interface->del_sa(charon->kernel_interface,
|
2006-06-09 15:12:43 +00:00
|
|
|
this->me.addr, this->me.spi, this->protocol);
|
2006-06-15 11:06:22 +00:00
|
|
|
}
|
|
|
|
if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
|
|
|
|
{
|
|
|
|
charon->kernel_interface->del_sa(charon->kernel_interface,
|
|
|
|
this->me.addr, this->alloc_esp_spi, PROTO_ESP);
|
|
|
|
}
|
|
|
|
if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
|
|
|
|
{
|
|
|
|
charon->kernel_interface->del_sa(charon->kernel_interface,
|
|
|
|
this->me.addr, this->alloc_ah_spi, PROTO_AH);
|
|
|
|
}
|
|
|
|
if (this->other.spi)
|
|
|
|
{
|
2006-03-01 15:56:51 +00:00
|
|
|
charon->kernel_interface->del_sa(charon->kernel_interface,
|
2006-06-09 15:12:43 +00:00
|
|
|
this->other.addr, this->other.spi, this->protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete all policies in the kernel */
|
|
|
|
while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
|
|
|
|
{
|
2006-07-12 11:42:36 +00:00
|
|
|
/* let rekeyed policies, as they are used by another child_sa */
|
|
|
|
charon->kernel_interface->del_policy(charon->kernel_interface,
|
2006-07-18 12:53:54 +00:00
|
|
|
policy->my_ts, policy->other_ts,
|
|
|
|
POLICY_OUT);
|
2006-07-12 11:42:36 +00:00
|
|
|
|
|
|
|
charon->kernel_interface->del_policy(charon->kernel_interface,
|
2006-07-18 12:53:54 +00:00
|
|
|
policy->other_ts, policy->my_ts,
|
|
|
|
POLICY_IN);
|
2006-07-12 11:42:36 +00:00
|
|
|
|
|
|
|
charon->kernel_interface->del_policy(charon->kernel_interface,
|
2006-07-18 12:53:54 +00:00
|
|
|
policy->other_ts, policy->my_ts,
|
|
|
|
POLICY_FWD);
|
|
|
|
policy->my_ts->destroy(policy->my_ts);
|
|
|
|
policy->other_ts->destroy(policy->other_ts);
|
2006-06-09 15:12:43 +00:00
|
|
|
free(policy);
|
2006-03-01 15:56:51 +00:00
|
|
|
}
|
2006-06-09 15:12:43 +00:00
|
|
|
this->policies->destroy(this->policies);
|
2006-07-10 14:24:04 +00:00
|
|
|
|
2006-07-20 10:09:32 +00:00
|
|
|
this->my_ts->destroy(this->my_ts);
|
|
|
|
this->other_ts->destroy(this->other_ts);
|
2006-07-10 14:24:04 +00:00
|
|
|
this->me.addr->destroy(this->me.addr);
|
|
|
|
this->other.addr->destroy(this->other.addr);
|
2006-09-12 13:50:14 +00:00
|
|
|
this->me.id->destroy(this->me.id);
|
|
|
|
this->other.id->destroy(this->other.id);
|
2007-04-10 06:01:03 +00:00
|
|
|
this->config->destroy(this->config);
|
2007-07-03 13:49:29 +00:00
|
|
|
free(this->iface);
|
2007-03-01 12:31:58 +00:00
|
|
|
DESTROY_IF(this->virtual_ip);
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2006-02-16 09:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header.
|
|
|
|
*/
|
2007-02-28 14:04:36 +00:00
|
|
|
child_sa_t * child_sa_create(host_t *me, host_t* other,
|
2006-09-12 13:50:14 +00:00
|
|
|
identification_t *my_id, identification_t *other_id,
|
2007-06-27 13:10:55 +00:00
|
|
|
child_cfg_t *config, u_int32_t rekey, bool encap)
|
2006-02-16 09:55:07 +00:00
|
|
|
{
|
2007-02-28 14:04:36 +00:00
|
|
|
static u_int32_t reqid = 0;
|
2006-04-10 08:07:38 +00:00
|
|
|
private_child_sa_t *this = malloc_thing(private_child_sa_t);
|
2006-02-16 09:55:07 +00:00
|
|
|
|
|
|
|
/* public functions */
|
2006-08-25 09:07:37 +00:00
|
|
|
this->public.get_name = (char*(*)(child_sa_t*))get_name;
|
2006-05-31 14:23:15 +00:00
|
|
|
this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
|
2006-06-07 13:26:23 +00:00
|
|
|
this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
|
|
|
|
this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
|
2007-04-11 12:14:51 +00:00
|
|
|
this->public.get_stats = (void(*)(child_sa_t*, mode_t*,encryption_algorithm_t*,size_t*,integrity_algorithm_t*,size_t*,u_int32_t*,u_int32_t*,u_int32_t*,u_int32_t*))get_stats;
|
2006-02-16 09:55:07 +00:00
|
|
|
this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
|
2006-12-21 14:35:17 +00:00
|
|
|
this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
|
|
|
|
this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
|
2007-06-27 13:10:55 +00:00
|
|
|
this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,bool))update_hosts;
|
2006-12-21 14:35:17 +00:00
|
|
|
this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
|
2007-04-10 06:01:03 +00:00
|
|
|
this->public.get_traffic_selectors = (linked_list_t*(*)(child_sa_t*,bool))get_traffic_selectors;
|
2006-06-23 14:02:30 +00:00
|
|
|
this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
|
2006-07-13 08:26:54 +00:00
|
|
|
this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
|
|
|
|
this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
|
2007-04-10 06:01:03 +00:00
|
|
|
this->public.get_config = (child_cfg_t*(*)(child_sa_t*))get_config;
|
2007-03-01 12:31:58 +00:00
|
|
|
this->public.set_virtual_ip = (void(*)(child_sa_t*,host_t*))set_virtual_ip;
|
2006-02-16 09:55:07 +00:00
|
|
|
this->public.destroy = (void(*)(child_sa_t*))destroy;
|
|
|
|
|
|
|
|
/* private data */
|
2006-07-10 14:24:04 +00:00
|
|
|
this->me.addr = me->clone(me);
|
|
|
|
this->other.addr = other->clone(other);
|
2006-09-12 13:50:14 +00:00
|
|
|
this->me.id = my_id->clone(my_id);
|
|
|
|
this->other.id = other_id->clone(other_id);
|
2006-06-07 13:26:23 +00:00
|
|
|
this->me.spi = 0;
|
|
|
|
this->other.spi = 0;
|
2006-06-15 11:06:22 +00:00
|
|
|
this->alloc_ah_spi = 0;
|
|
|
|
this->alloc_esp_spi = 0;
|
2007-06-27 13:10:55 +00:00
|
|
|
this->encap = encap;
|
2006-07-13 08:26:54 +00:00
|
|
|
this->state = CHILD_CREATED;
|
2006-06-12 08:36:41 +00:00
|
|
|
/* reuse old reqid if we are rekeying an existing CHILD_SA */
|
|
|
|
this->reqid = rekey ? rekey : ++reqid;
|
2008-03-26 10:06:45 +00:00
|
|
|
this->enc_alg = ENCR_UNDEFINED;
|
|
|
|
this->enc_size = 0;
|
|
|
|
this->int_alg = AUTH_UNDEFINED;
|
|
|
|
this->int_size = 0;
|
2006-03-01 15:56:51 +00:00
|
|
|
this->policies = linked_list_create();
|
2006-07-20 10:09:32 +00:00
|
|
|
this->my_ts = linked_list_create();
|
|
|
|
this->other_ts = linked_list_create();
|
2006-06-07 13:26:23 +00:00
|
|
|
this->protocol = PROTO_NONE;
|
2006-12-21 14:35:17 +00:00
|
|
|
this->mode = MODE_TUNNEL;
|
2007-03-01 12:31:58 +00:00
|
|
|
this->virtual_ip = NULL;
|
2007-07-03 13:49:29 +00:00
|
|
|
this->iface = NULL;
|
2007-04-10 06:01:03 +00:00
|
|
|
this->config = config;
|
|
|
|
config->get_ref(config);
|
2005-12-04 19:05:52 +00:00
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
return &this->public;
|
2005-12-04 19:05:52 +00:00
|
|
|
}
|