sandbox: eth-raw: Add a function to ask the host about localhost
authorJoe Hershberger <joe.hershberger@ni.com>
Mon, 2 Jul 2018 19:47:51 +0000 (14:47 -0500)
committerJoe Hershberger <joe.hershberger@ni.com>
Thu, 26 Jul 2018 19:08:18 +0000 (14:08 -0500)
Instead of doing a simple string compare against "lo", look for the flag
that indicates a localhost interface.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
arch/sandbox/cpu/eth-raw-os.c
arch/sandbox/include/asm/eth-raw-os.h
drivers/net/sandbox-raw.c

index 82bf666..12ddb34 100644 (file)
@@ -1,9 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2015 National Instruments
- *
- * (C) Copyright 2015
- * Joe Hershberger <joe.hershberger@ni.com>
+ * Copyright (c) 2015-2018 National Instruments
+ * Copyright (c) 2015-2018 Joe Hershberger <joe.hershberger@ni.com>
  */
 
 #include <asm/eth-raw-os.h>
 #include <linux/if_ether.h>
 #include <linux/if_packet.h>
 
+int sandbox_eth_raw_os_is_local(const char *ifname)
+{
+       int fd = socket(AF_INET, SOCK_DGRAM, 0);
+       struct ifreq ifr;
+       int ret = 0;
+
+       if (fd < 0)
+               return -errno;
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+       ret = ioctl(fd, SIOCGIFFLAGS, &ifr);
+       if (ret < 0) {
+               ret = -errno;
+               goto out;
+       }
+       ret = !!(ifr.ifr_flags & IFF_LOOPBACK);
+out:
+       close(fd);
+       return ret;
+}
+
 static int _raw_packet_start(struct eth_sandbox_raw_priv *priv,
                             unsigned char *ethmac)
 {
index 760f5fb..edd09d2 100644 (file)
@@ -34,6 +34,14 @@ struct eth_sandbox_raw_priv {
        unsigned short local_bind_udp_port;
 };
 
+/*
+ * Check if the interface named "ifname" is a localhost interface or not.
+ * ifname - the interface name on the host to check
+ *
+ * returns - 0 if real interface, 1 if local, negative if error
+ */
+int sandbox_eth_raw_os_is_local(const char *ifname);
+
 int sandbox_eth_raw_os_start(struct eth_sandbox_raw_priv *priv,
                             unsigned char *ethmac);
 int sandbox_eth_raw_os_send(void *packet, int length,
index 317e964..c04b94c 100644 (file)
@@ -29,6 +29,8 @@ static int sb_eth_raw_start(struct udevice *dev)
        if (priv->local) {
                env_set("ipaddr", "127.0.0.1");
                env_set("serverip", "127.0.0.1");
+               net_ip = string_to_ip("127.0.0.1");
+               net_server_ip = net_ip;
        }
        return ret;
 }
@@ -140,6 +142,7 @@ static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
        struct eth_pdata *pdata = dev_get_platdata(dev);
        struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
        const char *ifname;
+       u32 local;
 
        pdata->iobase = dev_read_addr(dev);
 
@@ -147,10 +150,13 @@ static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
        if (ifname) {
                strncpy(priv->host_ifname, ifname, IFNAMSIZ);
                printf(": Using %s from DT\n", priv->host_ifname);
-               if (strcmp(ifname, "lo") == 0)
-                       priv->local = 1;
        }
 
+       local = sandbox_eth_raw_os_is_local(priv->host_ifname);
+       if (local < 0)
+               return local;
+       priv->local = local;
+
        return 0;
 }