fixed netlink socket receiver code
authorMartin Willi <martin@strongswan.org>
Sat, 3 Mar 2007 14:56:24 +0000 (14:56 -0000)
committerMartin Willi <martin@strongswan.org>
Sat, 3 Mar 2007 14:56:24 +0000 (14:56 -0000)
implemented interface enumeration code with netlink: no getifaddrs reqired anymore

src/charon/daemon.c
src/charon/network/socket.c
src/charon/network/socket.h
src/charon/sa/child_sa.c
src/charon/sa/tasks/ike_natd.c
src/charon/threads/kernel_interface.c
src/charon/threads/kernel_interface.h
src/charon/threads/stroke_interface.c

index 3bac57f..4a14340 100644 (file)
@@ -487,7 +487,7 @@ int main(int argc, char *argv[])
        }
        
        /* log socket info */
-       list = charon->socket->create_local_address_list(charon->socket);
+       list = charon->kernel_interface->create_address_list(charon->kernel_interface);
        DBG1(DBG_NET, "listening on %d addresses:", list->get_count(list));
        while (list->remove_first(list, (void**)&host) == SUCCESS)
        {
index 13e2c28..596c26f 100644 (file)
@@ -436,189 +436,6 @@ status_t sender(private_socket_t *this, packet_t *packet)
 }
 
 /**
- * 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)
@@ -871,8 +688,6 @@ socket_t *socket_create(u_int16_t port, u_int16_t natt_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;
index 70d7b94..ef60fa7 100644 (file)
@@ -88,27 +88,6 @@ struct socket_t {
        status_t (*send) (socket_t *this, packet_t *packet);
        
        /**
-        * @brief Check if an address is an address of this host.
-        *
-        * If the name parameter is not NULL, a string is allocated which
-        * holds the interfaces name. 
-        *
-        * @param this                  socket_t object to work on
-        * @param host                  address to check
-        * @param name[out]             interface name on which address is used
-        * @return                              TRUE if local address, FALSE otherwise
-        */
-       bool (*is_local_address) (socket_t *this, host_t *host, char **name);
-       
-       /**
-        * @brief Create a list of hosts with all local addresses.
-        *
-        * @param this                  socket_t object to work on
-        * @return                              list with host_t objects
-        */
-       linked_list_t *(*create_local_address_list) (socket_t *this);
-       
-       /**
         * @brief Destroy sockets.
         * 
         * close sockets and destroy socket_t object
index 76e0dc0..ba9520c 100644 (file)
@@ -261,7 +261,8 @@ static void updown(private_child_sa_t *this, bool up)
                 asprintf(&virtual_ip, "");
         }
 
-               charon->socket->is_local_address(charon->socket, this->me.addr, &ifname);
+               ifname = charon->kernel_interface->get_interface(charon->kernel_interface, 
+                                                                                                                this->me.addr);
                
                /* build the command with all env variables.
                 * TODO: PLUTO_PEER_CA and PLUTO_NEXT_HOP are currently missing
@@ -295,7 +296,7 @@ static void updown(private_child_sa_t *this, bool up)
                                                        this->me.addr) ? "-host" : "-client",
                                 this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-ipv6",
                                 this->policy->get_name(this->policy),
-                                ifname,
+                                ifname ? ifname : "(unknown)",
                                 this->reqid,
                                 this->me.addr,
                                 this->me.id,
index a25b6e0..8e36b2e 100644 (file)
@@ -249,7 +249,7 @@ static status_t build_i(private_ike_natd_t *this, message_t *message)
        if (host->is_anyaddr(host))
        {
                /* TODO: we could get the src address from netlink!? */
-               list = charon->socket->create_local_address_list(charon->socket);
+               list = charon->kernel_interface->create_address_list(charon->kernel_interface);
                while (list->remove_first(list, (void**)&host) == SUCCESS)
                {
                        notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
index 7d30c21..25a281a 100644 (file)
@@ -6,15 +6,15 @@
  */
 
 /*
- * 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
@@ -235,6 +235,32 @@ static void vip_entry_destroy(vip_entry_t *this)
        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;
 
 /**
@@ -419,7 +445,7 @@ static void receive_events(private_kernel_interface_t *this)
                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;
@@ -470,7 +496,7 @@ static void receive_events(private_kernel_interface_t *this)
                        {
                                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);
                        }
@@ -491,7 +517,7 @@ static void receive_events(private_kernel_interface_t *this)
                        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);
@@ -556,6 +582,12 @@ static status_t netlink_send(int socket, struct nlmsghdr *in,
                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);
                
@@ -655,18 +687,16 @@ static status_t netlink_send_ack(int socket, struct nlmsghdr *in)
        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");
@@ -694,24 +724,43 @@ static linked_list_t *create_address_list(private_kernel_interface_t *this)
                                        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;
                                }
@@ -729,34 +778,61 @@ static linked_list_t *create_address_list(private_kernel_interface_t *this)
        {
                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;
 }
 
 /**
@@ -766,6 +842,7 @@ static bool is_local_address(private_kernel_interface_t *this, host_t* ip)
 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;
@@ -796,23 +873,20 @@ static status_t get_address_by_ts(private_kernel_interface_t *this,
        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);
@@ -822,24 +896,24 @@ static status_t get_address_by_ts(private_kernel_interface_t *this,
 /**
  * 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)
        {
@@ -888,14 +962,15 @@ static status_t manage_ipaddr(private_kernel_interface_t *this, int nlmsg_type,
 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;
@@ -951,7 +1026,7 @@ static status_t add_ip(private_kernel_interface_t *this,
 
        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",
@@ -1005,7 +1080,7 @@ static status_t del_ip(private_kernel_interface_t *this,
 
        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",
@@ -1111,7 +1186,7 @@ static status_t get_spi(private_kernel_interface_t *this,
                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;
@@ -1136,7 +1211,7 @@ static status_t add_sa(private_kernel_interface_t *this,
        
        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;
@@ -1253,7 +1328,7 @@ static status_t add_sa(private_kernel_interface_t *this,
        
        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;
@@ -1276,7 +1351,7 @@ static status_t update_sa(private_kernel_interface_t *this,
        
        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;
@@ -1319,12 +1394,12 @@ static status_t update_sa(private_kernel_interface_t *this,
        }
        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;   
@@ -1360,7 +1435,7 @@ static status_t update_sa(private_kernel_interface_t *this,
        }
        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;
        }
@@ -1386,7 +1461,7 @@ static status_t query_sa(private_kernel_interface_t *this, host_t *dst,
        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;
@@ -1431,7 +1506,7 @@ static status_t query_sa(private_kernel_interface_t *this, host_t *dst,
        
        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;
        }
@@ -1453,7 +1528,7 @@ static status_t del_sa(private_kernel_interface_t *this, host_t *dst,
        
        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;
@@ -1468,10 +1543,10 @@ static status_t del_sa(private_kernel_interface_t *this, host_t *dst,
        
        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;
 }
 
@@ -1598,7 +1673,7 @@ static status_t add_policy(private_kernel_interface_t *this,
                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;
@@ -1606,6 +1681,8 @@ static status_t add_policy(private_kernel_interface_t *this,
                        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;
                        }
@@ -1804,6 +1881,9 @@ kernel_interface_t *kernel_interface_create()
        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;
index 805a2b8..34b06f5 100644 (file)
@@ -264,23 +264,39 @@ struct kernel_interface_t {
                                                        policy_dir_t direction);
        
        /**
+        * @brief Get the interface name of a local address.
+        *
+        * @param this                  calling object
+        * @param host                  address to get interface name from
+        * @return                              allocated interface name, or NULL if not found
+        */
+       char* (*get_interface) (kernel_interface_t *this, host_t *host);
+       
+       /**
+        * @brief Creates a list of all local addresses.
+        *
+        * @param this                  calling object
+        * @return                              allocated list with host_t objects
+        */
+       linked_list_t *(*create_address_list) (kernel_interface_t *this);
+       
+       /**
         * @brief Add a virtual IP to an interface.
         *
         * Virtual IPs are attached to an interface. If an IP is added multiple
         * times, the IP is refcounted and not removed until del_ip() was called
         * as many times as add_ip().
-        * The virtual IP is attached to the interface used to reach a specified
-        * destination host.
+        * The virtual IP is attached to the interface where the iface_ip is found.
         *
         * @param this                  calling object
         * @param virtual_ip    virtual ip address to assign
-        * @param dst_ip                destination host to select outgoing interface
+        * @param iface_ip              IP of an interface to attach virtual IP
         * @return
         *                                              - SUCCESS
         *                                              - FAILED if kernel comm failed
         */
        status_t (*add_ip) (kernel_interface_t *this, host_t *virtual_ip,
-                                               host_t *dst_ip);
+                                               host_t *iface_ip);
        
        /**
         * @brief Remove a virtual IP from an interface.
@@ -289,13 +305,13 @@ struct kernel_interface_t {
         *
         * @param this                  calling object
         * @param virtual_ip    virtual ip address to assign
-        * @param dst_ip                destination host to select outgoing interface
+        * @param iface_ip              IP of an interface to remove virtual IP from
         * @return
         *                                              - SUCCESS
         *                                              - FAILED if kernel comm failed
         */
        status_t (*del_ip) (kernel_interface_t *this, host_t *virtual_ip,
-                                               host_t *dst_ip);
+                                               host_t *iface_ip);
        
        /**
         * @brief Destroys a kernel_interface object.
index c93712b..268fe56 100755 (executable)
@@ -200,6 +200,7 @@ static void stroke_add_conn(stroke_msg_t *msg, FILE *out)
        host_t *my_vip = NULL, *other_vip = NULL;
        proposal_t *proposal;
        traffic_selector_t *my_ts, *other_ts;
+       char *interface;
        
        pop_string(msg, &msg->add_conn.name);
        pop_string(msg, &msg->add_conn.me.address);
@@ -253,8 +254,10 @@ static void stroke_add_conn(stroke_msg_t *msg, FILE *out)
                my_host->destroy(my_host);
                return;
        }
-
-       if (charon->socket->is_local_address(charon->socket, other_host, NULL))
+       
+       interface = charon->kernel_interface->get_interface(charon->kernel_interface, 
+                                                                                                               other_host);
+       if (interface)
        {
                stroke_end_t tmp_end;
                host_t *tmp_host;
@@ -268,11 +271,18 @@ static void stroke_add_conn(stroke_msg_t *msg, FILE *out)
                tmp_end = msg->add_conn.me;
                msg->add_conn.me = msg->add_conn.other;
                msg->add_conn.other = tmp_end;
+               free(interface);
        }
-       else if (!charon->socket->is_local_address(charon->socket, my_host, NULL))
+       if (!interface)
        {
-               DBG1(DBG_CFG, "left nor right host is our side, aborting\n");
-               goto destroy_hosts;
+               interface = charon->kernel_interface->get_interface(
+                                                                                       charon->kernel_interface, my_host);
+               if (!interface)
+               {
+                       DBG1(DBG_CFG, "left nor right host is our side, aborting\n");
+                       goto destroy_hosts;
+               }
+               free(interface);
        }
 
        my_id = identification_create_from_string(msg->add_conn.me.id ?
@@ -897,7 +907,7 @@ static void stroke_statusall(stroke_msg_t *msg, FILE *out)
                        charon->job_queue->get_count(charon->job_queue));
        fprintf(out, " scheduled events: %d\n",
                        charon->event_queue->get_count(charon->event_queue));
-       list = charon->socket->create_local_address_list(charon->socket);
+       list = charon->kernel_interface->create_address_list(charon->kernel_interface);
 
        fprintf(out, "Listening on %d IP addresses:\n", list->get_count(list));
        while (list->remove_first(list, (void**)&host) == SUCCESS)