lib-interface: Add parsing for interface 'options'
[samba.git] / source3 / lib / interface.c
index 9d073bc08ca57e9c07f18604e886c3d29784a190..b8450316b6730db65ddf61bd6edcf09ed8181cbf 100644 (file)
@@ -19,6 +19,9 @@
 */
 
 #include "includes.h"
+#include "lib/socket/interfaces.h"
+#include "librpc/gen_ndr/ioctl.h"
+#include "lib/util/smb_strtox.h"
 
 static struct iface_struct *probed_ifaces;
 static int total_probed;
@@ -29,11 +32,11 @@ static struct interface *local_interfaces;
  Check if an IP is one of mine.
 **************************************************************************/
 
-bool ismyaddr(const struct sockaddr_storage *ip)
+bool ismyaddr(const struct sockaddr *ip)
 {
        struct interface *i;
        for (i=local_interfaces;i;i=i->next) {
-               if (addr_equal(&i->ip,ip)) {
+               if (sockaddr_equal((struct sockaddr *)&i->ip,ip)) {
                        return true;
                }
        }
@@ -44,14 +47,14 @@ bool ismyip_v4(struct in_addr ip)
 {
        struct sockaddr_storage ss;
        in_addr_to_sockaddr_storage(&ss, ip);
-       return ismyaddr(&ss);
+       return ismyaddr((struct sockaddr *)&ss);
 }
 
 /****************************************************************************
  Try and find an interface that matches an ip. If we cannot, return NULL.
 **************************************************************************/
 
-static struct interface *iface_find(const struct sockaddr_storage *ip,
+static struct interface *iface_find(const struct sockaddr *ip,
                                bool check_mask)
 {
        struct interface *i;
@@ -62,10 +65,10 @@ static struct interface *iface_find(const struct sockaddr_storage *ip,
 
        for (i=local_interfaces;i;i=i->next) {
                if (check_mask) {
-                       if (same_net(ip, &i->ip, &i->netmask)) {
+                       if (same_net(ip, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) {
                                return i;
                        }
-               } else if (addr_equal(&i->ip, ip)) {
+               } else if (sockaddr_equal((struct sockaddr *)&i->ip, ip)) {
                        return i;
                }
        }
@@ -77,11 +80,11 @@ static struct interface *iface_find(const struct sockaddr_storage *ip,
  Check if a packet is from a local (known) net.
 **************************************************************************/
 
-bool is_local_net(const struct sockaddr_storage *from)
+bool is_local_net(const struct sockaddr *from)
 {
        struct interface *i;
        for (i=local_interfaces;i;i=i->next) {
-               if (same_net(from, &i->ip, &i->netmask)) {
+               if (same_net(from, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) {
                        return true;
                }
        }
@@ -89,19 +92,11 @@ bool is_local_net(const struct sockaddr_storage *from)
 }
 
 #if defined(HAVE_IPV6)
-void setup_linklocal_scope_id(struct sockaddr_storage *pss)
+void setup_linklocal_scope_id(struct sockaddr *pss)
 {
        struct interface *i;
        for (i=local_interfaces;i;i=i->next) {
-               if (addr_equal(&i->ip,pss)) {
-                       struct sockaddr_in6 *psa6 =
-                               (struct sockaddr_in6 *)pss;
-                       psa6->sin6_scope_id = if_nametoindex(i->name);
-                       return;
-               }
-       }
-       for (i=local_interfaces;i;i=i->next) {
-               if (same_net(pss, &i->ip, &i->netmask)) {
+               if (sockaddr_equal((struct sockaddr *)&i->ip,pss)) {
                        struct sockaddr_in6 *psa6 =
                                (struct sockaddr_in6 *)pss;
                        psa6->sin6_scope_id = if_nametoindex(i->name);
@@ -120,7 +115,7 @@ bool is_local_net_v4(struct in_addr from)
        struct sockaddr_storage ss;
 
        in_addr_to_sockaddr_storage(&ss, from);
-       return is_local_net(&ss);
+       return is_local_net((struct sockaddr *)&ss);
 }
 
 /****************************************************************************
@@ -139,15 +134,18 @@ int iface_count(void)
 }
 
 /****************************************************************************
- How many interfaces do we have (v4 only) ?
+ How many non-loopback IPv4 interfaces do we have ?
 **************************************************************************/
 
-int iface_count_v4(void)
+int iface_count_v4_nl(void)
 {
        int ret = 0;
        struct interface *i;
 
        for (i=local_interfaces;i;i=i->next) {
+               if (is_loopback_addr((struct sockaddr *)&i->ip)) {
+                       continue;
+               }
                if (i->ip.ss_family == AF_INET) {
                        ret++;
                }
@@ -156,7 +154,8 @@ int iface_count_v4(void)
 }
 
 /****************************************************************************
- Return a pointer to the in_addr of the first IPv4 interface.
+ Return a pointer to the in_addr of the first IPv4 interface that's
+ not 0.0.0.0.
 **************************************************************************/
 
 const struct in_addr *first_ipv4_iface(void)
@@ -164,7 +163,9 @@ const struct in_addr *first_ipv4_iface(void)
        struct interface *i;
 
        for (i=local_interfaces;i ;i=i->next) {
-               if (i->ip.ss_family == AF_INET) {
+               if ((i->ip.ss_family == AF_INET) &&
+                   (!is_zero_ip_v4(((struct sockaddr_in *)&i->ip)->sin_addr)))
+               {
                        break;
                }
        }
@@ -270,7 +271,7 @@ const struct sockaddr_storage *iface_n_bcast(int n)
    an appropriate interface they return the requested field of the
    first known interface. */
 
-const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip)
+const struct sockaddr_storage *iface_ip(const struct sockaddr *ip)
 {
        struct interface *i = iface_find(ip, true);
        if (i) {
@@ -281,7 +282,7 @@ const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip)
         * matching address family. */
 
        for (i=local_interfaces;i;i=i->next) {
-               if (i->ip.ss_family == ip->ss_family) {
+               if (i->ip.ss_family == ip->sa_family) {
                        return &i->ip;
                }
        }
@@ -292,9 +293,9 @@ const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip)
   return True if a IP is directly reachable on one of our interfaces
 */
 
-bool iface_local(const struct sockaddr_storage *ip)
+bool iface_local(const struct sockaddr *ip)
 {
-       return iface_find(ip, True) ? true : false;
+       return iface_find(ip, true) ? true : false;
 }
 
 /****************************************************************************
@@ -306,7 +307,7 @@ static void add_interface(const struct iface_struct *ifs)
        char addr[INET6_ADDRSTRLEN];
        struct interface *iface;
 
-       if (iface_find(&ifs->ip, False)) {
+       if (iface_find((const struct sockaddr *)&ifs->ip, False)) {
                DEBUG(3,("add_interface: not adding duplicate interface %s\n",
                        print_sockaddr(addr, sizeof(addr), &ifs->ip) ));
                return;
@@ -334,6 +335,10 @@ static void add_interface(const struct iface_struct *ifs)
        iface->ip = ifs->ip;
        iface->netmask = ifs->netmask;
        iface->bcast = ifs->bcast;
+       iface->linkspeed = ifs->linkspeed;
+       iface->capability = ifs->capability;
+       iface->if_index = ifs->if_index;
+       iface->options = ifs->options;
 
        DLIST_ADD(local_interfaces, iface);
 
@@ -348,6 +353,72 @@ static void add_interface(const struct iface_struct *ifs)
                        &iface->netmask) ));
 }
 
+
+static void parse_extra_info(char *key,
+                            uint64_t *speed,
+                            uint32_t *cap,
+                            uint32_t *if_index,
+                            uint32_t *options)
+{
+       while (key != NULL && *key != '\0') {
+               char *next_key;
+               char *val;
+               int error = 0;
+
+               next_key = strchr_m(key, ',');
+               if (next_key != NULL) {
+                       *next_key++ = 0;
+               }
+
+               val = strchr_m(key, '=');
+               if (val != NULL) {
+                       *val++ = 0;
+
+                       if (strequal_m(key, "speed")) {
+                               *speed = (uint64_t)smb_strtoull(val,
+                                                               NULL,
+                                                               0,
+                                                               &error,
+                                                               SMB_STR_STANDARD);
+                               if (error != 0) {
+                                       DBG_DEBUG("Invalid speed value (%s)\n", val);
+                               }
+                       } else if (strequal_m(key, "capability")) {
+                               if (strequal_m(val, "RSS")) {
+                                       *cap |= FSCTL_NET_IFACE_RSS_CAPABLE;
+                               } else if (strequal(val, "RDMA")) {
+                                       *cap |= FSCTL_NET_IFACE_RDMA_CAPABLE;
+                               } else {
+                                       DBG_WARNING("Capability unknown: "
+                                                   "'%s'\n", val);
+                               }
+                       } else if (strequal_m(key, "if_index")) {
+                               *if_index = (uint32_t)smb_strtoul(val,
+                                                                 NULL,
+                                                                 0,
+                                                                 &error,
+                                                                 SMB_STR_STANDARD);
+                               if (error != 0) {
+                                       DBG_DEBUG("Invalid key value (%s)\n", val);
+                               }
+                       } else if (strequal_m(key, "options")) {
+                               if (strequal_m(val, "dynamic")) {
+                                       *options |= IFACE_DYNAMIC_OPTION;
+                               } else if (strequal_m(val, "nodynamic")) {
+                                       *options &= ~IFACE_DYNAMIC_OPTION;
+                               } else {
+                                       DBG_WARNING("Options unknown: "
+                                                   "'%s'\n", val);
+                               }
+                       } else {
+                               DBG_DEBUG("Key unknown: '%s'\n", key);
+                       }
+               }
+
+               key = next_key;
+       }
+}
+
 /****************************************************************************
  Interpret a single element from a interfaces= config line.
 
@@ -358,6 +429,26 @@ static void add_interface(const struct iface_struct *ifs)
  3) IP/masklen
  4) ip/mask
  5) bcast/mask
+
+ Additional information for an interface can be specified with
+ this extended syntax:
+
+    "interface[;key1=value1[,key2=value2[...]]]"
+
+ Note: The double quoting is important for the
+       smb.conf parser! Otherwise the ';' and ',' separates
+       two interfaces.
+
+ where
+ - keys known: 'speed', 'capability', 'if_index'
+ - speed is in bits per second
+ - capabilities known: 'RSS', 'RDMA'
+ - if_index should be used with care, because
+   these indexes should not conicide with indexes
+   the kernel sets...
+
+ Note: The specified values overwrite the autodetected values!
+
 ****************************************************************************/
 
 static void interpret_interface(char *token)
@@ -371,10 +462,51 @@ static void interpret_interface(char *token)
        int i;
        bool added=false;
        bool goodaddr = false;
+       uint64_t speed = 0;
+       uint32_t cap = FSCTL_NET_IFACE_NONE_CAPABLE;
+       uint32_t if_index = 0;
+       bool speed_set = false;
+       bool cap_set = false;
+       bool if_index_set = false;
+       uint32_t options = IFACE_NONE_OPTION;
+       bool options_set = false;
+
+       /*
+        * extract speed / capability information if present
+        */
+       p = strchr_m(token, ';');
+       if (p != NULL) {
+               *p++ = 0;
+               parse_extra_info(p, &speed, &cap, &if_index, &options);
+               if (speed != 0) {
+                       speed_set = true;
+               }
+               if (cap != FSCTL_NET_IFACE_NONE_CAPABLE) {
+                       cap_set = true;
+               }
+               if (if_index != 0) {
+                       if_index_set = true;
+               }
+               if (options != IFACE_NONE_OPTION) {
+                       options_set = true;
+               }
+       }
 
        /* first check if it is an interface name */
        for (i=0;i<total_probed;i++) {
                if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
+                       if (speed_set) {
+                               probed_ifaces[i].linkspeed = speed;
+                       }
+                       if (cap_set) {
+                               probed_ifaces[i].capability = cap;
+                       }
+                       if (if_index_set) {
+                               probed_ifaces[i].if_index = if_index;
+                       }
+                       if (options_set) {
+                                probed_ifaces[i].options = options;
+                       }
                        add_interface(&probed_ifaces[i]);
                        added = true;
                }
@@ -383,7 +515,6 @@ static void interpret_interface(char *token)
                return;
        }
 
-       /* maybe it is a DNS name */
        p = strchr_m(token,'/');
        if (p == NULL) {
                if (!interpret_string_addr(&ss, token, 0)) {
@@ -393,7 +524,21 @@ static void interpret_interface(char *token)
                }
 
                for (i=0;i<total_probed;i++) {
-                       if (addr_equal(&ss, &probed_ifaces[i].ip)) {
+                       if (sockaddr_equal((struct sockaddr *)&ss,
+                               (struct sockaddr *)&probed_ifaces[i].ip))
+                       {
+                               if (speed_set) {
+                                       probed_ifaces[i].linkspeed = speed;
+                               }
+                               if (cap_set) {
+                                       probed_ifaces[i].capability = cap;
+                               }
+                               if (if_index_set) {
+                                       probed_ifaces[i].if_index = if_index;
+                               }
+                               if (options_set) {
+                                       probed_ifaces[i].options = options;
+                               }
                                add_interface(&probed_ifaces[i]);
                                return;
                        }
@@ -425,9 +570,11 @@ static void interpret_interface(char *token)
                        return;
                }
        } else {
-               char *endp = NULL;
-               unsigned long val = strtoul(p, &endp, 0);
-               if (p == endp || (endp && *endp != '\0')) {
+               int error = 0;
+               unsigned long val;
+
+               val = smb_strtoul(p, NULL, 0, &error, SMB_STR_FULL_STR_CONV);
+               if (error != 0) {
                        DEBUG(2,("interpret_interface: "
                                "can't determine netmask value from %s\n",
                                p));
@@ -446,9 +593,12 @@ static void interpret_interface(char *token)
        make_net(&ss_net, &ss, &ss_mask);
 
        /* Maybe the first component was a broadcast address. */
-       if (addr_equal(&ss_bcast, &ss) || addr_equal(&ss_net, &ss)) {
+       if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) ||
+               sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) {
                for (i=0;i<total_probed;i++) {
-                       if (same_net(&ss, &probed_ifaces[i].ip, &ss_mask)) {
+                       if (same_net((struct sockaddr *)&ss, 
+                                                (struct sockaddr *)&probed_ifaces[i].ip, 
+                                                (struct sockaddr *)&ss_mask)) {
                                /* Temporarily replace netmask on
                                 * the detected interface - user knows
                                 * best.... */
@@ -460,6 +610,18 @@ static void interpret_interface(char *token)
                                        "config file on interface %s\n",
                                        p,
                                        probed_ifaces[i].name));
+                               if (speed_set) {
+                                       probed_ifaces[i].linkspeed = speed;
+                               }
+                               if (cap_set) {
+                                       probed_ifaces[i].capability = cap;
+                               }
+                               if (if_index_set) {
+                                       probed_ifaces[i].if_index = if_index;
+                               }
+                               if (options_set) {
+                                       probed_ifaces[i].options = options;
+                               }
                                add_interface(&probed_ifaces[i]);
                                probed_ifaces[i].netmask = saved_mask;
                                return;
@@ -477,11 +639,21 @@ static void interpret_interface(char *token)
                token));
 
        ZERO_STRUCT(ifs);
-       safe_strcpy(ifs.name, token, sizeof(ifs.name)-1);
+       (void)strlcpy(ifs.name, token, sizeof(ifs.name));
        ifs.flags = IFF_BROADCAST;
        ifs.ip = ss;
        ifs.netmask = ss_mask;
        ifs.bcast = ss_bcast;
+       if (if_index_set) {
+               ifs.if_index = if_index;
+       }
+       if (speed_set) {
+               ifs.linkspeed = speed;
+       } else {
+               ifs.linkspeed = 1000 * 1000 * 1000;
+       }
+       ifs.capability = cap;
+       ifs.options = options;
        add_interface(&ifs);
 }
 
@@ -491,31 +663,24 @@ static void interpret_interface(char *token)
 
 void load_interfaces(void)
 {
-       struct iface_struct ifaces[MAX_INTERFACES];
+       struct iface_struct *ifaces = NULL;
        const char **ptr = lp_interfaces();
        int i;
 
-       SAFE_FREE(probed_ifaces);
-
-       /* dump the current interfaces if any */
-       while (local_interfaces) {
-               struct interface *iface = local_interfaces;
-               DLIST_REMOVE(local_interfaces, local_interfaces);
-               SAFE_FREE(iface->name);
-               SAFE_FREE(iface);
-       }
+       gfree_interfaces();
 
        /* Probe the kernel for interfaces */
-       total_probed = get_interfaces(ifaces, MAX_INTERFACES);
+       total_probed = get_interfaces(talloc_tos(), &ifaces);
 
        if (total_probed > 0) {
-               probed_ifaces = (struct iface_struct *)memdup(ifaces,
+               probed_ifaces = (struct iface_struct *)smb_memdup(ifaces,
                                sizeof(ifaces[0])*total_probed);
                if (!probed_ifaces) {
-                       DEBUG(0,("ERROR: memdup failed\n"));
+                       DEBUG(0,("ERROR: smb_memdup failed\n"));
                        exit(1);
                }
        }
+       TALLOC_FREE(ifaces);
 
        /* if we don't have a interfaces line then use all broadcast capable
           interfaces except loopback */
@@ -568,14 +733,33 @@ void gfree_interfaces(void)
 
 bool interfaces_changed(void)
 {
+       bool ret = false;
        int n;
-       struct iface_struct ifaces[MAX_INTERFACES];
+       struct iface_struct *ifaces = NULL;
 
-       n = get_interfaces(ifaces, MAX_INTERFACES);
+       n = get_interfaces(talloc_tos(), &ifaces);
 
        if ((n > 0 )&& (n != total_probed ||
                        memcmp(ifaces, probed_ifaces, sizeof(ifaces[0])*n))) {
-               return true;
+               ret = true;
+       }
+
+       TALLOC_FREE(ifaces);
+       return ret;
+}
+
+/****************************************************************************
+ Return True if interface exists for given interface index
+**************************************************************************/
+
+bool interface_ifindex_exists(int if_index)
+{
+       struct interface *i;
+
+       for (i = local_interfaces; i != NULL; i = i->next) {
+               if (i->if_index == if_index) {
+                       return true;
+               }
        }
 
        return false;