}
/**
- * implements socket_t.is_local_address
- */
-static bool is_local_address(private_socket_t *this, host_t *host, char **dev)
-{
-#ifdef HAVE_GETIFADDRS
- struct ifaddrs *list;
- struct ifaddrs *cur;
- bool found = FALSE;
-
- if (getifaddrs(&list) < 0)
- {
- return FALSE;
- }
-
- for (cur = list; cur != NULL; cur = cur->ifa_next)
- {
- if (!(cur->ifa_flags & IFF_UP))
- {
- /* ignore interface which are down */
- continue;
- }
-
- if (cur->ifa_addr == NULL ||
- cur->ifa_addr->sa_family != host->get_family(host))
- {
- /* no match in family */
- continue;
- }
-
- switch (cur->ifa_addr->sa_family)
- {
- case AF_INET:
- {
- struct sockaddr_in *listed, *requested;
- listed = (struct sockaddr_in*)cur->ifa_addr;
- requested = (struct sockaddr_in*)host->get_sockaddr(host);
- if (listed->sin_addr.s_addr == requested->sin_addr.s_addr)
- {
- found = TRUE;
- }
- break;
- }
- case AF_INET6:
- {
- struct sockaddr_in6 *listed, *requested;
- listed = (struct sockaddr_in6*)cur->ifa_addr;
- requested = (struct sockaddr_in6*)host->get_sockaddr(host);
- if (memcmp(&listed->sin6_addr, &requested->sin6_addr,
- sizeof(listed->sin6_addr)) == 0)
- {
- found = TRUE;
- }
- break;
- }
- default:
- break;
- }
-
- if (found)
- {
- if (dev && cur->ifa_name)
- {
- /* return interface name, if requested */
- *dev = strdup(cur->ifa_name);
- }
- break;
- }
- }
- freeifaddrs(list);
- return found;
-#else /* !HAVE_GETIFADDRS */
-
- int skt = socket(AF_INET, SOCK_DGRAM, 0);
- struct ifconf conf;
- struct ifreq reqs[16];
-
- conf.ifc_len = sizeof(reqs);
- conf.ifc_req = reqs;
-
- if (ioctl(skt, SIOCGIFCONF, &conf) == -1)
- {
- DBG1(DBG_NET, "checking address using ioctl() failed: %m");
- close(skt);
- return FALSE;
- }
- close(skt);
-
- while (conf.ifc_len >= sizeof(struct ifreq))
- {
- /* only IPv4 supported yet */
- if (conf.ifc_req->ifr_addr.sa_family == AF_INET &&
- host->get_family(host) == AF_INET)
- {
- struct sockaddr_in *listed, *requested;
- listed = (struct sockaddr_in*)&conf.ifc_req->ifr_addr;
- requested = (struct sockaddr_in*)host->get_sockaddr(host);
- if (listed->sin_addr.s_addr == requested->sin_addr.s_addr)
- {
- return TRUE;
- }
- }
- conf.ifc_len -= sizeof(struct ifreq);
- conf.ifc_req++;
- }
- return FALSE;
-#endif /* HAVE_GETIFADDRS */
-}
-
-/**
- * implements socket_t.create_local_address_list
- */
-static linked_list_t* create_local_address_list(private_socket_t *this)
-{
-#ifdef HAVE_GETIFADDRS
- struct ifaddrs *list;
- struct ifaddrs *cur;
- host_t *host;
- linked_list_t *result = linked_list_create();
-
- if (getifaddrs(&list) < 0)
- {
- return result;
- }
-
- for (cur = list; cur != NULL; cur = cur->ifa_next)
- {
- if (!(cur->ifa_flags & IFF_UP))
- {
- /* ignore interface which are down */
- continue;
- }
-
- host = host_create_from_sockaddr(cur->ifa_addr);
- if (host)
- {
- /* we use always the IKEv2 port. This is relevant for
- * natd payload hashing. */
- host->set_port(host, this->port);
- result->insert_last(result, host);
- }
- }
- freeifaddrs(list);
- return result;
-#else /* !HAVE_GETIFADDRS */
- int skt = socket(AF_INET, SOCK_DGRAM, 0);
- struct ifconf conf;
- struct ifreq reqs[16];
- linked_list_t *result = linked_list_create();
- host_t *host;
-
- conf.ifc_len = sizeof(reqs);
- conf.ifc_req = reqs;
-
- if (ioctl(skt, SIOCGIFCONF, &conf) == -1)
- {
- DBG1(DBG_NET, "getting address list using ioctl() failed: %m");
- close(skt);
- return FALSE;
- }
- close(skt);
-
- while (conf.ifc_len >= sizeof(struct ifreq))
- {
- /* only IPv4 supported yet */
- if (conf.ifc_req->ifr_addr.sa_family == AF_INET)
- {
- host = host_create_from_sockaddr(&conf.ifc_req->ifr_addr);
- if (host)
- {
- /* we use always the IKEv2 port. This is relevant for
- * natd payload hashing. */
- host->set_port(host, this->port);
- result->insert_last(result, host);
- }
- }
- conf.ifc_len -= sizeof(struct ifreq);
- conf.ifc_req++;
- }
- return result;
-#endif /* HAVE_GETIFADDRS */
-}
-
-/**
* open a socket to send packets
*/
static int open_send_socket(private_socket_t *this, int family, u_int16_t port)
/* public functions */
this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
- this->public.is_local_address = (bool(*)(socket_t*, host_t*,char**))is_local_address;
- this->public.create_local_address_list = (linked_list_t*(*)(socket_t*))create_local_address_list;
this->public.destroy = (void(*)(socket_t*)) destroy;
this->port = port;
*/
/*
- * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
+ * Copyright (C) 2005-2007 Martin Willi
* Copyright (C) 2006-2007 Tobias Brunner
+ * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
* Copyright (C) 2006 Daniel Roethlisberger
- * Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil
* Copyright (C) 2003 Herbert Xu.
*
- * Contains modified parts from pluto.
+ * Based on xfrm code from pluto.
*
* 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(this);
}
+typedef struct address_entry_t address_entry_t;
+
+/**
+ * an address found on the system, containg address and interface info
+ */
+struct address_entry_t {
+
+ /** address of this entry */
+ host_t *host;
+
+ /** interface index */
+ int ifindex;
+
+ /** name of the index */
+ char ifname[IFNAMSIZ];
+};
+
+/**
+ * destroy an address entry
+ */
+static void address_entry_destroy(address_entry_t *this)
+{
+ this->host->destroy(this->host);
+ free(this);
+}
+
typedef struct private_kernel_interface_t private_kernel_interface_t;
/**
unsigned char response[512];
struct nlmsghdr *hdr;
struct sockaddr_nl addr;
- socklen_t addr_len;
+ socklen_t addr_len = sizeof(addr);
int len;
hdr = (struct nlmsghdr*)response;
{
DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
DBG1(DBG_KNL, "creating acquire job for CHILD_SA with reqid %d",
- reqid);
+ reqid);
job = (job_t*)acquire_job_create(reqid);
charon->job_queue->add(charon->job_queue, job);
}
DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
DBG1(DBG_KNL, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
expire->hard ? "delete" : "rekey", protocol_id_names,
- protocol, ntohl(spi), reqid);
+ protocol, spi, reqid);
if (expire->hard)
{
job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
tmp.ptr = buf;
msg = (struct nlmsghdr*)tmp.ptr;
+ memset(&addr, 0, sizeof(addr));
+ addr.nl_family = AF_NETLINK;
+ addr.nl_pid = getpid();
+ addr.nl_groups = 0;
+ addr_len = sizeof(addr);
+
len = recvfrom(socket, tmp.ptr, tmp.len, 0,
(struct sockaddr*)&addr, &addr_len);
free(out);
return FAILED;
}
+
/**
- * Implements kernel_interface_t.create_address_list.
* Create a list of local addresses.
*/
static linked_list_t *create_address_list(private_kernel_interface_t *this)
{
- char request[128];
+ char request[BUFFER_SIZE];
struct nlmsghdr *out, *hdr;
struct rtgenmsg *msg;
size_t len;
- chunk_t chunk;
- host_t *host;
linked_list_t *list;
DBG2(DBG_IKE, "getting local address list");
struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
struct rtattr *rta = IFA_RTA(msg);
size_t rtasize = IFA_PAYLOAD (hdr);
+ host_t *host = NULL;
+ char *name = NULL;
+ chunk_t chunk;
while(RTA_OK(rta, rtasize))
{
- if (rta->rta_type == IFA_ADDRESS)
+ switch (rta->rta_type)
{
- chunk.ptr = RTA_DATA(rta);
- chunk.len = RTA_PAYLOAD(rta);
-
- /* we set the interface on the port of the host */
- host = host_create_from_chunk(msg->ifa_family, chunk,
- msg->ifa_index);
- if (host)
- {
- list->insert_last(list, host);
- }
+ case IFA_ADDRESS:
+ chunk.ptr = RTA_DATA(rta);
+ chunk.len = RTA_PAYLOAD(rta);
+ host = host_create_from_chunk(msg->ifa_family,
+ chunk, 0);
+ break;
+ case IFA_LABEL:
+ name = RTA_DATA(rta);
}
rta = RTA_NEXT(rta, rtasize);
}
+
+ if (host)
+ {
+ address_entry_t *entry;
+
+ entry = malloc_thing(address_entry_t);
+ entry->host = host;
+ entry->ifindex = msg->ifa_index;
+ if (name)
+ {
+ memcpy(entry->ifname, name, IFNAMSIZ);
+ }
+ else
+ {
+ strcpy(entry->ifname, "(unknown)");
+ }
+ list->insert_last(list, entry);
+ }
hdr = NLMSG_NEXT(hdr, len);
continue;
}
{
DBG1(DBG_IKE, "unable to get local address list");
}
+
return list;
}
/**
- * implementation of kernel_interface_t.is_local_address
+ * Implements kernel_interface_t.create_address_list.
*/
-static bool is_local_address(private_kernel_interface_t *this, host_t* ip)
+static linked_list_t *create_address_list_public(private_kernel_interface_t *this)
+{
+ linked_list_t *result, *list;
+ address_entry_t *entry;
+
+ result = linked_list_create();
+ list = create_address_list(this);
+ while (list->remove_last(list, (void**)&entry) == SUCCESS)
+ {
+ result->insert_last(result, entry->host);
+ free(entry);
+ }
+ list->destroy(list);
+
+ return result;
+}
+
+/**
+ * implementation of kernel_interface_t.get_interface_name
+ */
+static char *get_interface_name(private_kernel_interface_t *this, host_t* ip)
{
linked_list_t *list;
- host_t *host;
- bool found = FALSE;
+ address_entry_t *entry;
+ char *name = NULL;
- DBG2(DBG_IKE, "checking if host %H is local", ip);
+ DBG2(DBG_IKE, "getting interface name for %H", ip);
list = create_address_list(this);
- while (!found && list->remove_last(list, (void**)&host) == SUCCESS)
+ while (!name && list->remove_last(list, (void**)&entry) == SUCCESS)
{
- if (host->ip_equals(host, ip))
+ if (ip->ip_equals(ip, entry->host))
{
- found = TRUE;
+ name = strdup(entry->ifname);
}
- host->destroy(host);
+ address_entry_destroy(entry);
}
- list->destroy_offset(list, offsetof(host_t, destroy));
+ list->destroy_function(list, (void*)address_entry_destroy);
- DBG2(DBG_IKE, "%H is %slocal", ip, found ? "" : "not ");
-
- return found;
+ if (name)
+ {
+ DBG2(DBG_IKE, "%H is on interface %s", ip, name);
+ }
+ else
+ {
+ DBG1(DBG_IKE, "%H is not a local address", ip);
+ }
+ return name;
}
/**
static status_t get_address_by_ts(private_kernel_interface_t *this,
traffic_selector_t *ts, host_t **ip)
{
+ address_entry_t *entry;
host_t *host;
int family;
linked_list_t *list;
host->destroy(host);
list = create_address_list(this);
- while (!found && list->remove_last(list, (void**)&host) == SUCCESS)
+ while (!found && list->remove_last(list, (void**)&entry) == SUCCESS)
{
- if (ts->includes(ts, host))
+ if (ts->includes(ts, entry->host))
{
found = TRUE;
- *ip = host;
- }
- else
- {
- host->destroy(host);
+ *ip = entry->host->clone(entry->host);
}
+ address_entry_destroy(entry);
}
- list->destroy_offset(list, offsetof(host_t, destroy));
+ list->destroy_function(list, (void*)address_entry_destroy);
if (!found)
{
- DBG2(DBG_IKE, "no local address found in traffic selector %R", ts);
+ DBG1(DBG_IKE, "no local address found in traffic selector %R", ts);
return FAILED;
}
DBG2(DBG_IKE, "using host %H", *ip);
/**
* get the interface of a local address
*/
-static int get_interface(private_kernel_interface_t *this, host_t* ip)
+static int get_interface_index(private_kernel_interface_t *this, host_t* ip)
{
linked_list_t *list;
- host_t *host;
+ address_entry_t *entry;
int ifindex = 0;
DBG2(DBG_IKE, "getting iface for %H", ip);
list = create_address_list(this);
- while (!ifindex && list->remove_last(list, (void**)&host) == SUCCESS)
+ while (!ifindex && list->remove_last(list, (void**)&entry) == SUCCESS)
{
- if (host->ip_equals(host, ip))
+ if (ip->ip_equals(ip, entry->host))
{
- ifindex = host->get_port(host);
+ ifindex = entry->ifindex;
}
- host->destroy(host);
+ address_entry_destroy(entry);
}
- list->destroy_offset(list, offsetof(host_t, destroy));
+ list->destroy_function(list, (void*)address_entry_destroy);
if (ifindex == 0)
{
static status_t manage_srcroute(private_kernel_interface_t *this, int nlmsg_type,
int flags, route_entry_t *route)
{
+ unsigned char request[BUFFER_SIZE];
struct nlmsghdr *hdr;
struct rtmsg *msg;
- unsigned char request[BUFFER_SIZE];
chunk_t chunk;
/* if route is 0.0.0.0/0, we can't install it, as it would
* overwrite the default route. Instead, we add two routes:
- * 0.0.0.0/1 and 128.0.0.0/1 */
+ * 0.0.0.0/1 and 128.0.0.0/1
+ * TODO: use metrics instead */
if (route->prefixlen == 0)
{
route_entry_t half;
DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
- targetif = get_interface(this, iface_ip);
+ targetif = get_interface_index(this, iface_ip);
if (targetif == 0)
{
DBG1(DBG_KNL, "unable to add virtual IP %H, no iface found for %H",
DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
- targetif = get_interface(this, iface_ip);
+ targetif = get_interface_index(this, iface_ip);
if (targetif == 0)
{
DBG1(DBG_KNL, "unable to delete virtual IP %H, no iface found for %H",
return FAILED;
}
- DBG2(DBG_KNL, "got SPI %x for reqid %d", received_spi, reqid);
+ DBG2(DBG_KNL, "got SPI 0x%x for reqid %d", received_spi, reqid);
*spi = received_spi;
return SUCCESS;
memset(&request, 0, sizeof(request));
- DBG2(DBG_KNL, "adding SAD entry with SPI %x", spi);
+ DBG2(DBG_KNL, "adding SAD entry with SPI 0x%x", spi);
hdr = (struct nlmsghdr*)request;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
{
- DBG1(DBG_KNL, "unalbe to add SAD entry with SPI %x", spi);
+ DBG1(DBG_KNL, "unalbe to add SAD entry with SPI 0x%x", spi);
return FAILED;
}
return SUCCESS;
memset(&request, 0, sizeof(request));
- DBG2(DBG_KNL, "querying SAD entry with SPI %x", spi);
+ DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x", spi);
hdr = (struct nlmsghdr*)request;
hdr->nlmsg_flags = NLM_F_REQUEST;
}
if (sa == NULL)
{
- DBG1(DBG_KNL, "unable to update SAD entry with SPI %x", spi);
+ DBG1(DBG_KNL, "unable to update SAD entry with SPI 0x%x", spi);
free(out);
return FAILED;
}
- DBG2(DBG_KNL, "updating SAD entry with SPI %x", spi);
+ DBG2(DBG_KNL, "updating SAD entry with SPI 0x%x", spi);
hdr = out;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
}
if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
{
- DBG1(DBG_KNL, "unalbe to update SAD entry with SPI %x", spi);
+ DBG1(DBG_KNL, "unalbe to update SAD entry with SPI 0x%x", spi);
free(out);
return FAILED;
}
struct xfrm_usersa_info *sa = NULL;
size_t len;
- DBG2(DBG_KNL, "querying SAD entry with SPI %x", spi);
+ DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x", spi);
memset(&request, 0, sizeof(request));
hdr = (struct nlmsghdr*)request;
if (sa == NULL)
{
- DBG1(DBG_KNL, "unable to query SAD entry with SPI %x", spi);
+ DBG1(DBG_KNL, "unable to query SAD entry with SPI 0x%x", spi);
free(out);
return FAILED;
}
memset(&request, 0, sizeof(request));
- DBG2(DBG_KNL, "deleting SAD entry with SPI %x", spi);
+ DBG2(DBG_KNL, "deleting SAD entry with SPI 0x%x", spi);
hdr = (struct nlmsghdr*)request;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
{
- DBG1(DBG_KNL, "unalbe to delete SAD entry with SPI %x", spi);
+ DBG1(DBG_KNL, "unalbe to delete SAD entry with SPI 0x%x", spi);
return FAILED;
}
- DBG2(DBG_KNL, "deleted SAD entry with SPI %x", spi);
+ DBG2(DBG_KNL, "deleted SAD entry with SPI 0x%x", spi);
return SUCCESS;
}
policy->route = malloc_thing(route_entry_t);
if (get_address_by_ts(this, dst_ts, &policy->route->src_ip) == SUCCESS)
{
- policy->route->if_index = get_interface(this, dst);
+ policy->route->if_index = get_interface_index(this, dst);
policy->route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
memcpy(policy->route->dst_net.ptr, &policy->sel.saddr, policy->route->dst_net.len);
policy->route->prefixlen = policy->sel.prefixlen_s;
if (manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
policy->route) != SUCCESS)
{
+ DBG1(DBG_KNL, "unable to install source route for %H",
+ policy->route->src_ip);
route_entry_destroy(policy->route);
policy->route = NULL;
}
this->public.add_policy = (status_t(*)(kernel_interface_t*,host_t*,host_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,protocol_id_t,u_int32_t,bool,mode_t,bool))add_policy;
this->public.query_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
this->public.del_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
+
+ this->public.get_interface = (char*(*)(kernel_interface_t*,host_t*))get_interface_name;
+ this->public.create_address_list = (linked_list_t*(*)(kernel_interface_t*))create_address_list_public;
this->public.add_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) add_ip;
this->public.del_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) del_ip;
this->public.destroy = (void(*)(kernel_interface_t*)) destroy;