2005-12-04 19:05:52 +00:00
|
|
|
/*
|
2012-02-24 10:07:34 +00:00
|
|
|
* Copyright (C) 2008-2012 Tobias Brunner
|
2005-12-04 19:05:52 +00:00
|
|
|
* Hochschule fuer Technik Rapperswil
|
2010-02-23 15:34:34 +00:00
|
|
|
* Copyright (C) 2010 Martin Willi
|
|
|
|
* Copyright (C) 2010 revosec AG
|
2005-12-04 19:05:52 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-09-13 12:36:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Nanoteq Pty Ltd
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2005-12-04 19:05:52 +00:00
|
|
|
#include "kernel_interface.h"
|
|
|
|
|
2010-07-12 08:57:46 +00:00
|
|
|
#include <debug.h>
|
2010-07-06 11:02:01 +00:00
|
|
|
#include <threading/mutex.h>
|
|
|
|
#include <utils/linked_list.h>
|
2007-09-03 12:37:25 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
typedef struct private_kernel_interface_t private_kernel_interface_t;
|
2006-03-02 09:56:30 +00:00
|
|
|
|
2012-09-13 12:36:04 +00:00
|
|
|
typedef struct kernel_algorithm_t kernel_algorithm_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mapping of IKE algorithms to kernel-specific algorithm identifiers
|
|
|
|
*/
|
|
|
|
struct kernel_algorithm_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform type of the algorithm
|
|
|
|
*/
|
|
|
|
transform_type_t type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifier specified in IKE
|
|
|
|
*/
|
|
|
|
u_int16_t ike;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifier as defined in pfkeyv2.h
|
|
|
|
*/
|
|
|
|
u_int16_t kernel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the algorithm in linux crypto API
|
|
|
|
*/
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
2006-07-12 11:42:36 +00:00
|
|
|
/**
|
2008-09-25 07:56:58 +00:00
|
|
|
* Private data of a kernel_interface_t object.
|
2006-07-12 11:42:36 +00:00
|
|
|
*/
|
2008-09-25 07:56:58 +00:00
|
|
|
struct private_kernel_interface_t {
|
2006-06-09 07:31:30 +00:00
|
|
|
|
|
|
|
/**
|
2008-09-25 07:56:58 +00:00
|
|
|
* Public part of kernel_interface_t object.
|
2006-06-09 07:31:30 +00:00
|
|
|
*/
|
2008-09-25 07:56:58 +00:00
|
|
|
kernel_interface_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-06-29 09:59:43 +00:00
|
|
|
/**
|
|
|
|
* Registered IPsec constructor
|
|
|
|
*/
|
|
|
|
kernel_ipsec_constructor_t ipsec_constructor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registered net constructor
|
|
|
|
*/
|
|
|
|
kernel_net_constructor_t net_constructor;
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* ipsec interface
|
|
|
|
*/
|
|
|
|
kernel_ipsec_t *ipsec;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* network interface
|
|
|
|
*/
|
|
|
|
kernel_net_t *net;
|
2010-07-06 11:02:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mutex for listeners
|
|
|
|
*/
|
|
|
|
mutex_t *mutex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list of registered listeners
|
|
|
|
*/
|
|
|
|
linked_list_t *listeners;
|
2012-09-13 12:36:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mutex for algorithm mappings
|
|
|
|
*/
|
|
|
|
mutex_t *mutex_algs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of algorithm mappings (kernel_algorithm_t*)
|
|
|
|
*/
|
|
|
|
linked_list_t *algorithms;
|
2008-05-08 16:19:11 +00:00
|
|
|
};
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, get_spi, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *src, host_t *dst,
|
2010-07-12 08:35:19 +00:00
|
|
|
u_int8_t protocol, u_int32_t reqid, u_int32_t *spi)
|
2006-06-09 07:31:30 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->ipsec->get_spi(this->ipsec, src, dst, protocol, reqid, spi);
|
2006-06-09 07:31:30 +00:00
|
|
|
}
|
2005-12-04 19:05:52 +00:00
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, get_cpi, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *src, host_t *dst,
|
|
|
|
u_int32_t reqid, u_int16_t *cpi)
|
2007-03-03 13:04:07 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->ipsec->get_cpi(this->ipsec, src, dst, reqid, cpi);
|
2007-03-03 13:04:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, add_sa, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *src, host_t *dst,
|
2010-11-30 16:17:30 +00:00
|
|
|
u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
|
|
|
|
u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
|
2010-07-02 21:45:57 +00:00
|
|
|
u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode, u_int16_t ipcomp,
|
2011-04-18 13:16:23 +00:00
|
|
|
u_int16_t cpi, bool encap, bool esn, bool inbound,
|
|
|
|
traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->ipsec->add_sa(this->ipsec, src, dst, spi, protocol, reqid,
|
2010-11-30 16:17:30 +00:00
|
|
|
mark, tfc, lifetime, enc_alg, enc_key, int_alg, int_key, mode,
|
2011-04-18 13:16:23 +00:00
|
|
|
ipcomp, cpi, encap, esn, inbound, src_ts, dst_ts);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2006-07-12 11:42:36 +00:00
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, update_sa, status_t,
|
2010-07-12 08:35:19 +00:00
|
|
|
private_kernel_interface_t *this, u_int32_t spi, u_int8_t protocol,
|
2010-02-23 15:34:34 +00:00
|
|
|
u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
|
2010-07-02 21:45:57 +00:00
|
|
|
bool encap, bool new_encap, mark_t mark)
|
2007-03-03 13:04:07 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2008-11-11 09:22:00 +00:00
|
|
|
return this->ipsec->update_sa(this->ipsec, spi, protocol, cpi, src, dst,
|
2010-07-02 21:45:57 +00:00
|
|
|
new_src, new_dst, encap, new_encap, mark);
|
2007-03-03 13:04:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, query_sa, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *src, host_t *dst,
|
2010-07-12 08:35:19 +00:00
|
|
|
u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes)
|
2009-07-30 19:33:19 +00:00
|
|
|
{
|
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2010-07-02 21:45:57 +00:00
|
|
|
return this->ipsec->query_sa(this->ipsec, src, dst, spi, protocol, mark, bytes);
|
2009-07-30 19:33:19 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, del_sa, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *src, host_t *dst, u_int32_t spi,
|
2010-07-12 08:35:19 +00:00
|
|
|
u_int8_t protocol, u_int16_t cpi, mark_t mark)
|
2007-03-03 14:56:24 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2010-07-02 21:45:57 +00:00
|
|
|
return this->ipsec->del_sa(this->ipsec, src, dst, spi, protocol, cpi, mark);
|
2007-03-03 14:56:24 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 11:37:16 +00:00
|
|
|
METHOD(kernel_interface_t, flush_sas, status_t,
|
|
|
|
private_kernel_interface_t *this)
|
|
|
|
{
|
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
return this->ipsec->flush_sas(this->ipsec);
|
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, add_policy, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *src, host_t *dst,
|
|
|
|
traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
|
2010-08-03 09:49:28 +00:00
|
|
|
policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
|
2011-07-27 11:41:35 +00:00
|
|
|
mark_t mark, policy_priority_t priority)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->ipsec->add_policy(this->ipsec, src, dst, src_ts, dst_ts,
|
2011-07-27 11:41:35 +00:00
|
|
|
direction, type, sa, mark, priority);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2006-07-12 11:42:36 +00:00
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, query_policy, status_t,
|
|
|
|
private_kernel_interface_t *this, traffic_selector_t *src_ts,
|
2010-07-02 21:45:57 +00:00
|
|
|
traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
|
|
|
|
u_int32_t *use_time)
|
2008-06-26 08:59:39 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2010-02-23 15:34:34 +00:00
|
|
|
return this->ipsec->query_policy(this->ipsec, src_ts, dst_ts,
|
2010-07-02 21:45:57 +00:00
|
|
|
direction, mark, use_time);
|
2008-06-26 08:59:39 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, del_policy, status_t,
|
|
|
|
private_kernel_interface_t *this, traffic_selector_t *src_ts,
|
2011-05-13 10:50:29 +00:00
|
|
|
traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
|
2011-07-27 11:41:35 +00:00
|
|
|
mark_t mark, policy_priority_t priority)
|
2008-06-26 08:59:39 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2010-02-23 15:34:34 +00:00
|
|
|
return this->ipsec->del_policy(this->ipsec, src_ts, dst_ts,
|
2011-07-27 11:41:35 +00:00
|
|
|
direction, reqid, mark, priority);
|
2008-06-26 08:59:39 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 11:37:16 +00:00
|
|
|
METHOD(kernel_interface_t, flush_policies, status_t,
|
|
|
|
private_kernel_interface_t *this)
|
|
|
|
{
|
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
return this->ipsec->flush_policies(this->ipsec);
|
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, get_source_addr, host_t*,
|
|
|
|
private_kernel_interface_t *this, host_t *dest, host_t *src)
|
2006-07-12 11:42:36 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-08 08:23:46 +00:00
|
|
|
return this->net->get_source_addr(this->net, dest, src);
|
2007-03-03 13:04:07 +00:00
|
|
|
}
|
2007-02-28 14:04:36 +00:00
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, get_nexthop, host_t*,
|
2012-09-18 15:55:38 +00:00
|
|
|
private_kernel_interface_t *this, host_t *dest, host_t *src)
|
2007-03-03 13:04:07 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-09-18 15:55:38 +00:00
|
|
|
return this->net->get_nexthop(this->net, dest, src);
|
2007-03-03 13:04:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, get_interface, char*,
|
|
|
|
private_kernel_interface_t *this, host_t *host)
|
2007-03-03 13:04:07 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->net->get_interface(this->net, host);
|
2007-03-03 13:04:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, create_address_enumerator, enumerator_t*,
|
|
|
|
private_kernel_interface_t *this, bool include_down_ifaces,
|
|
|
|
bool include_virtual_ips)
|
2007-03-03 13:04:07 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return enumerator_create_empty();
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->net->create_address_enumerator(this->net, include_down_ifaces,
|
2010-02-23 15:34:34 +00:00
|
|
|
include_virtual_ips);
|
2006-07-12 11:42:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, add_ip, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *virtual_ip, host_t *iface_ip)
|
2007-03-03 13:04:07 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->net->add_ip(this->net, virtual_ip, iface_ip);
|
2007-03-03 13:04:07 +00:00
|
|
|
}
|
2007-03-02 17:08:38 +00:00
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, del_ip, status_t,
|
|
|
|
private_kernel_interface_t *this, host_t *virtual_ip)
|
2006-07-12 11:42:36 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->net->del_ip(this->net, virtual_ip);
|
2007-06-14 15:16:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, add_route, status_t,
|
|
|
|
private_kernel_interface_t *this, chunk_t dst_net,
|
|
|
|
u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
|
2007-06-14 15:16:15 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2010-02-23 15:34:34 +00:00
|
|
|
return this->net->add_route(this->net, dst_net, prefixlen, gateway,
|
|
|
|
src_ip, if_name);
|
2007-06-14 15:16:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, del_route, status_t,
|
|
|
|
private_kernel_interface_t *this, chunk_t dst_net,
|
|
|
|
u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
|
2007-09-03 12:37:25 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
2010-02-23 15:34:34 +00:00
|
|
|
return this->net->del_route(this->net, dst_net, prefixlen, gateway,
|
|
|
|
src_ip, if_name);
|
2007-09-03 12:37:25 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 16:28:23 +00:00
|
|
|
METHOD(kernel_interface_t, bypass_socket, bool,
|
|
|
|
private_kernel_interface_t *this, int fd, int family)
|
|
|
|
{
|
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return this->ipsec->bypass_socket(this->ipsec, fd, family);
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
|
2012-02-24 10:07:34 +00:00
|
|
|
METHOD(kernel_interface_t, enable_udp_decap, bool,
|
|
|
|
private_kernel_interface_t *this, int fd, int family, u_int16_t port)
|
|
|
|
{
|
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return this->ipsec->enable_udp_decap(this->ipsec, fd, family, port);
|
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, get_address_by_ts, status_t,
|
|
|
|
private_kernel_interface_t *this, traffic_selector_t *ts, host_t **ip)
|
2008-10-14 08:46:31 +00:00
|
|
|
{
|
|
|
|
enumerator_t *addrs;
|
|
|
|
host_t *host;
|
|
|
|
int family;
|
|
|
|
bool found = FALSE;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-10-14 08:46:31 +00:00
|
|
|
DBG2(DBG_KNL, "getting a local address in traffic selector %R", ts);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-10-14 08:46:31 +00:00
|
|
|
/* if we have a family which includes localhost, we do not
|
|
|
|
* search for an IP, we use the default */
|
|
|
|
family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-10-14 08:46:31 +00:00
|
|
|
if (family == AF_INET)
|
|
|
|
{
|
|
|
|
host = host_create_from_string("127.0.0.1", 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
host = host_create_from_string("::1", 0);
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-10-14 08:46:31 +00:00
|
|
|
if (ts->includes(ts, host))
|
|
|
|
{
|
|
|
|
*ip = host_create_any(family);
|
|
|
|
host->destroy(host);
|
|
|
|
DBG2(DBG_KNL, "using host %H", *ip);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
host->destroy(host);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-11-14 13:04:22 +00:00
|
|
|
addrs = create_address_enumerator(this, TRUE, TRUE);
|
2008-10-14 08:46:31 +00:00
|
|
|
while (addrs->enumerate(addrs, (void**)&host))
|
|
|
|
{
|
|
|
|
if (ts->includes(ts, host))
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
*ip = host->clone(host);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addrs->destroy(addrs);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-10-14 08:46:31 +00:00
|
|
|
if (!found)
|
|
|
|
{
|
2012-02-13 10:41:20 +00:00
|
|
|
DBG2(DBG_KNL, "no local address found in traffic selector %R", ts);
|
2008-10-14 08:46:31 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-10-14 08:46:31 +00:00
|
|
|
DBG2(DBG_KNL, "using host %H", *ip);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, add_ipsec_interface, void,
|
|
|
|
private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
|
2008-08-18 11:07:26 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->ipsec)
|
|
|
|
{
|
2011-06-29 09:59:43 +00:00
|
|
|
this->ipsec_constructor = constructor;
|
2009-06-02 07:59:40 +00:00
|
|
|
this->ipsec = constructor();
|
|
|
|
}
|
2008-08-18 11:07:26 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, remove_ipsec_interface, void,
|
|
|
|
private_kernel_interface_t *this, kernel_ipsec_constructor_t constructor)
|
2007-06-14 15:16:15 +00:00
|
|
|
{
|
2012-06-25 17:00:00 +00:00
|
|
|
if (constructor == this->ipsec_constructor && this->ipsec)
|
2011-06-29 09:59:43 +00:00
|
|
|
{
|
|
|
|
this->ipsec->destroy(this->ipsec);
|
|
|
|
this->ipsec = NULL;
|
|
|
|
}
|
2007-06-14 15:16:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, add_net_interface, void,
|
|
|
|
private_kernel_interface_t *this, kernel_net_constructor_t constructor)
|
2007-06-14 15:16:15 +00:00
|
|
|
{
|
2009-06-02 07:59:40 +00:00
|
|
|
if (!this->net)
|
|
|
|
{
|
2011-06-29 09:59:43 +00:00
|
|
|
this->net_constructor = constructor;
|
2009-06-02 07:59:40 +00:00
|
|
|
this->net = constructor();
|
|
|
|
}
|
2007-06-14 15:16:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, remove_net_interface, void,
|
|
|
|
private_kernel_interface_t *this, kernel_net_constructor_t constructor)
|
2007-06-14 15:16:15 +00:00
|
|
|
{
|
2012-06-25 17:00:00 +00:00
|
|
|
if (constructor == this->net_constructor && this->net)
|
2011-06-29 09:59:43 +00:00
|
|
|
{
|
|
|
|
this->net->destroy(this->net);
|
|
|
|
this->net = NULL;
|
|
|
|
}
|
2006-07-12 11:42:36 +00:00
|
|
|
}
|
2005-12-05 16:09:42 +00:00
|
|
|
|
2010-07-06 11:02:01 +00:00
|
|
|
METHOD(kernel_interface_t, add_listener, void,
|
|
|
|
private_kernel_interface_t *this, kernel_listener_t *listener)
|
|
|
|
{
|
|
|
|
this->mutex->lock(this->mutex);
|
|
|
|
this->listeners->insert_last(this->listeners, listener);
|
|
|
|
this->mutex->unlock(this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_interface_t, remove_listener, void,
|
|
|
|
private_kernel_interface_t *this, kernel_listener_t *listener)
|
|
|
|
{
|
|
|
|
this->mutex->lock(this->mutex);
|
|
|
|
this->listeners->remove(this->listeners, listener, NULL);
|
|
|
|
this->mutex->unlock(this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_interface_t, acquire, void,
|
|
|
|
private_kernel_interface_t *this, u_int32_t reqid,
|
|
|
|
traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
|
|
|
|
{
|
|
|
|
kernel_listener_t *listener;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
this->mutex->lock(this->mutex);
|
|
|
|
enumerator = this->listeners->create_enumerator(this->listeners);
|
|
|
|
while (enumerator->enumerate(enumerator, &listener))
|
|
|
|
{
|
2010-07-06 14:09:06 +00:00
|
|
|
if (listener->acquire &&
|
|
|
|
!listener->acquire(listener, reqid, src_ts, dst_ts))
|
2010-07-06 11:02:01 +00:00
|
|
|
{
|
|
|
|
this->listeners->remove_at(this->listeners, enumerator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->mutex->unlock(this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_interface_t, expire, void,
|
2010-07-12 08:35:19 +00:00
|
|
|
private_kernel_interface_t *this, u_int32_t reqid, u_int8_t protocol,
|
2010-07-06 11:02:01 +00:00
|
|
|
u_int32_t spi, bool hard)
|
|
|
|
{
|
|
|
|
kernel_listener_t *listener;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
this->mutex->lock(this->mutex);
|
|
|
|
enumerator = this->listeners->create_enumerator(this->listeners);
|
|
|
|
while (enumerator->enumerate(enumerator, &listener))
|
|
|
|
{
|
2010-07-06 14:09:06 +00:00
|
|
|
if (listener->expire &&
|
|
|
|
!listener->expire(listener, reqid, protocol, spi, hard))
|
2010-07-06 11:02:01 +00:00
|
|
|
{
|
|
|
|
this->listeners->remove_at(this->listeners, enumerator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->mutex->unlock(this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_interface_t, mapping, void,
|
|
|
|
private_kernel_interface_t *this, u_int32_t reqid, u_int32_t spi,
|
|
|
|
host_t *remote)
|
|
|
|
{
|
|
|
|
kernel_listener_t *listener;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
this->mutex->lock(this->mutex);
|
|
|
|
enumerator = this->listeners->create_enumerator(this->listeners);
|
|
|
|
while (enumerator->enumerate(enumerator, &listener))
|
|
|
|
{
|
2010-07-06 14:09:06 +00:00
|
|
|
if (listener->mapping &&
|
|
|
|
!listener->mapping(listener, reqid, spi, remote))
|
2010-07-06 11:02:01 +00:00
|
|
|
{
|
|
|
|
this->listeners->remove_at(this->listeners, enumerator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->mutex->unlock(this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_interface_t, migrate, void,
|
|
|
|
private_kernel_interface_t *this, u_int32_t reqid,
|
|
|
|
traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
|
|
|
|
policy_dir_t direction, host_t *local, host_t *remote)
|
|
|
|
{
|
|
|
|
kernel_listener_t *listener;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
this->mutex->lock(this->mutex);
|
|
|
|
enumerator = this->listeners->create_enumerator(this->listeners);
|
|
|
|
while (enumerator->enumerate(enumerator, &listener))
|
|
|
|
{
|
2010-07-06 14:09:06 +00:00
|
|
|
if (listener->migrate &&
|
|
|
|
!listener->migrate(listener, reqid, src_ts, dst_ts, direction,
|
2010-07-06 11:02:01 +00:00
|
|
|
local, remote))
|
|
|
|
{
|
|
|
|
this->listeners->remove_at(this->listeners, enumerator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->mutex->unlock(this->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool call_roam(kernel_listener_t *listener, bool *roam)
|
|
|
|
{
|
2010-07-06 14:09:06 +00:00
|
|
|
return listener->roam && !listener->roam(listener, *roam);
|
2010-07-06 11:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_interface_t, roam, void,
|
|
|
|
private_kernel_interface_t *this, bool address)
|
|
|
|
{
|
|
|
|
this->mutex->lock(this->mutex);
|
|
|
|
this->listeners->remove(this->listeners, &address, (void*)call_roam);
|
|
|
|
this->mutex->unlock(this->mutex);
|
|
|
|
}
|
|
|
|
|
2012-09-13 12:36:04 +00:00
|
|
|
METHOD(kernel_interface_t, register_algorithm, void,
|
|
|
|
private_kernel_interface_t *this, u_int16_t alg_id, transform_type_t type,
|
|
|
|
u_int16_t kernel_id, char *kernel_name)
|
|
|
|
{
|
|
|
|
kernel_algorithm_t *algorithm;
|
|
|
|
|
|
|
|
INIT(algorithm,
|
|
|
|
.type = type,
|
|
|
|
.ike = alg_id,
|
|
|
|
.kernel = kernel_id,
|
|
|
|
.name = strdup(kernel_name),
|
|
|
|
);
|
|
|
|
|
|
|
|
this->mutex_algs->lock(this->mutex_algs);
|
|
|
|
this->algorithms->insert_first(this->algorithms, algorithm);
|
|
|
|
this->mutex_algs->unlock(this->mutex_algs);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_interface_t, lookup_algorithm, bool,
|
|
|
|
private_kernel_interface_t *this, u_int16_t alg_id, transform_type_t type,
|
|
|
|
u_int16_t *kernel_id, char **kernel_name)
|
|
|
|
{
|
|
|
|
kernel_algorithm_t *algorithm;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
bool found = FALSE;
|
|
|
|
|
|
|
|
this->mutex_algs->lock(this->mutex_algs);
|
|
|
|
enumerator = this->algorithms->create_enumerator(this->algorithms);
|
|
|
|
while (enumerator->enumerate(enumerator, &algorithm))
|
|
|
|
{
|
|
|
|
if (algorithm->type == type && algorithm->ike == alg_id)
|
|
|
|
{
|
|
|
|
if (kernel_id)
|
|
|
|
{
|
|
|
|
*kernel_id = algorithm->kernel;
|
|
|
|
}
|
|
|
|
if (kernel_name)
|
|
|
|
{
|
|
|
|
*kernel_name = algorithm->name;
|
|
|
|
}
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->mutex_algs->unlock(this->mutex_algs);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2010-02-23 15:34:34 +00:00
|
|
|
METHOD(kernel_interface_t, destroy, void,
|
|
|
|
private_kernel_interface_t *this)
|
2005-12-05 16:09:42 +00:00
|
|
|
{
|
2012-09-13 12:36:04 +00:00
|
|
|
kernel_algorithm_t *algorithm;
|
|
|
|
|
|
|
|
while (this->algorithms->remove_first(this->algorithms,
|
|
|
|
(void**)&algorithm) == SUCCESS)
|
|
|
|
{
|
|
|
|
free(algorithm->name);
|
|
|
|
free(algorithm);
|
|
|
|
}
|
|
|
|
this->algorithms->destroy(this->algorithms);
|
|
|
|
this->mutex_algs->destroy(this->mutex_algs);
|
2008-09-25 07:56:58 +00:00
|
|
|
DESTROY_IF(this->ipsec);
|
|
|
|
DESTROY_IF(this->net);
|
2010-07-06 11:02:01 +00:00
|
|
|
this->listeners->destroy(this->listeners);
|
2012-09-13 12:36:04 +00:00
|
|
|
this->mutex->destroy(this->mutex);
|
2008-09-25 07:56:58 +00:00
|
|
|
free(this);
|
2005-12-05 16:09:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/*
|
|
|
|
* Described in header-file
|
2006-07-12 11:42:36 +00:00
|
|
|
*/
|
2008-09-25 07:56:58 +00:00
|
|
|
kernel_interface_t *kernel_interface_create()
|
2005-12-04 19:05:52 +00:00
|
|
|
{
|
2010-02-23 15:34:34 +00:00
|
|
|
private_kernel_interface_t *this;
|
|
|
|
|
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.get_spi = _get_spi,
|
|
|
|
.get_cpi = _get_cpi,
|
|
|
|
.add_sa = _add_sa,
|
|
|
|
.update_sa = _update_sa,
|
|
|
|
.query_sa = _query_sa,
|
|
|
|
.del_sa = _del_sa,
|
2011-10-21 11:37:16 +00:00
|
|
|
.flush_sas = _flush_sas,
|
2010-02-23 15:34:34 +00:00
|
|
|
.add_policy = _add_policy,
|
|
|
|
.query_policy = _query_policy,
|
|
|
|
.del_policy = _del_policy,
|
2011-10-21 11:37:16 +00:00
|
|
|
.flush_policies = _flush_policies,
|
2010-02-23 15:34:34 +00:00
|
|
|
.get_source_addr = _get_source_addr,
|
|
|
|
.get_nexthop = _get_nexthop,
|
|
|
|
.get_interface = _get_interface,
|
|
|
|
.create_address_enumerator = _create_address_enumerator,
|
|
|
|
.add_ip = _add_ip,
|
|
|
|
.del_ip = _del_ip,
|
|
|
|
.add_route = _add_route,
|
|
|
|
.del_route = _del_route,
|
2010-02-23 16:28:23 +00:00
|
|
|
.bypass_socket = _bypass_socket,
|
2012-02-24 10:07:34 +00:00
|
|
|
.enable_udp_decap = _enable_udp_decap,
|
2010-02-23 15:34:34 +00:00
|
|
|
|
|
|
|
.get_address_by_ts = _get_address_by_ts,
|
|
|
|
.add_ipsec_interface = _add_ipsec_interface,
|
|
|
|
.remove_ipsec_interface = _remove_ipsec_interface,
|
|
|
|
.add_net_interface = _add_net_interface,
|
|
|
|
.remove_net_interface = _remove_net_interface,
|
2010-07-06 11:02:01 +00:00
|
|
|
|
|
|
|
.add_listener = _add_listener,
|
|
|
|
.remove_listener = _remove_listener,
|
2012-09-13 12:36:04 +00:00
|
|
|
.register_algorithm = _register_algorithm,
|
|
|
|
.lookup_algorithm = _lookup_algorithm,
|
2010-07-06 11:02:01 +00:00
|
|
|
.acquire = _acquire,
|
|
|
|
.expire = _expire,
|
|
|
|
.mapping = _mapping,
|
|
|
|
.migrate = _migrate,
|
|
|
|
.roam = _roam,
|
2010-02-23 15:34:34 +00:00
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
2010-07-06 11:02:01 +00:00
|
|
|
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
|
|
|
|
.listeners = linked_list_create(),
|
2012-09-13 12:36:04 +00:00
|
|
|
.mutex_algs = mutex_create(MUTEX_TYPE_DEFAULT),
|
|
|
|
.algorithms = linked_list_create(),
|
2010-02-23 15:34:34 +00:00
|
|
|
);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-07-12 11:42:36 +00:00
|
|
|
return &this->public;
|
2005-12-04 19:05:52 +00:00
|
|
|
}
|
2010-02-23 15:34:34 +00:00
|
|
|
|