[U-Boot] [PATCH 1/1] sandbox: clone host Ethernet address

Joe Hershberger joe.hershberger at ni.com
Mon Oct 15 21:57:24 UTC 2018


On Sun, Oct 14, 2018 at 7:16 PM Heinrich Schuchardt <xypron.glpk at gmx.de> wrote:
>
> Wireless routers may not accept multiple MAC addresses to bind via the same
> WLAN session. To make the sandbox usable in a wireless environment we
> therefore should clone the host Ethernet address.
>
> Without the patch DHCP is possible via cable but not via WLAN.

This is definitely the wrong approach. By doing this you make it such
that packet filters are equal for the host and the U-Boot sandbox.
That means they can easily cross-talk between stacks and all kinds of
screwy things will happen.

For a situation like this, you should do something like setup a NAT in
your host firewall and run the sandbox against that interface.

> Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
> ---
>  arch/sandbox/cpu/eth-raw-os.c         | 21 +++++++++++++++++++++
>  arch/sandbox/include/asm/eth-raw-os.h | 11 +++++++++++
>  drivers/net/sandbox-raw-bus.c         |  9 +++++++++
>  drivers/net/sandbox-raw.c             | 10 ----------
>  include/configs/sandbox.h             |  6 +-----
>  5 files changed, 42 insertions(+), 15 deletions(-)
>
> diff --git a/arch/sandbox/cpu/eth-raw-os.c b/arch/sandbox/cpu/eth-raw-os.c
> index 75bfaa4c90..f9295d5771 100644
> --- a/arch/sandbox/cpu/eth-raw-os.c
> +++ b/arch/sandbox/cpu/eth-raw-os.c
> @@ -33,6 +33,27 @@ void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr)
>         if_freenameindex((struct if_nameindex *)ptr);
>  }
>
> +int sandbox_eth_raw_os_hwaddr(const char *ifname, unsigned char *hwaddr)
> +{
> +       int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
> +       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, SIOCGIFHWADDR, &ifr);
> +       if (ret < 0) {
> +               ret = -errno;
> +               goto out;
> +       }
> +       memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
> +out:
> +       close(fd);
> +       return ret;
> +}
> +
>  int sandbox_eth_raw_os_is_local(const char *ifname)
>  {
>         int fd = socket(AF_INET, SOCK_DGRAM, 0);
> diff --git a/arch/sandbox/include/asm/eth-raw-os.h b/arch/sandbox/include/asm/eth-raw-os.h
> index 0b511db70c..2efa702d96 100644
> --- a/arch/sandbox/include/asm/eth-raw-os.h
> +++ b/arch/sandbox/include/asm/eth-raw-os.h
> @@ -45,6 +45,17 @@ struct sandbox_eth_raw_if_nameindex *sandbox_eth_raw_if_nameindex(void);
>  /* Free the data structure of enumerated network interfaces */
>  void sandbox_eth_raw_if_freenameindex(struct sandbox_eth_raw_if_nameindex *ptr);
>
> +/**
> + * sandbox_eth_raw_os_hwaddr() - get MAC address
> + *
> + * Returns the MAC address.
> + *
> + * @ifname:    the interface name on the host
> + * @hwaddr:    MAC address
> + * Returns:    0 on success
> + */
> +int sandbox_eth_raw_os_hwaddr(const char *ifname, unsigned char *hwaddr);
> +
>  /*
>   * Check if the interface named "ifname" is a localhost interface or not.
>   * ifname - the interface name on the host to check
> diff --git a/drivers/net/sandbox-raw-bus.c b/drivers/net/sandbox-raw-bus.c
> index 76d65afe6c..f41484abf8 100644
> --- a/drivers/net/sandbox-raw-bus.c
> +++ b/drivers/net/sandbox-raw-bus.c
> @@ -26,7 +26,9 @@ static int eth_raw_bus_post_bind(struct udevice *dev)
>
>         dev_read_u32(dev, "skip-localhost", &skip_localhost);
>         for (i = ni; !(i->if_index == 0 && !i->if_name); i++) {
> +               int ret;
>                 int local = sandbox_eth_raw_os_is_local(i->if_name);
> +               struct eth_pdata *pdata = dev->platdata;
>
>                 if (local < 0)
>                         continue;
> @@ -39,6 +41,13 @@ static int eth_raw_bus_post_bind(struct udevice *dev)
>                 device_bind_driver(dev, "eth_sandbox_raw", ub_ifname, &child);
>
>                 device_set_name_alloced(child);
> +
> +               /* Clone the host Ethernet address */
> +               pdata = child->platdata;
> +               ret = sandbox_eth_raw_os_hwaddr(i->if_name, pdata->enetaddr);
> +               if (ret)
> +                       net_random_ethaddr(pdata->enetaddr);
> +
>                 device_probe(child);
>                 priv = dev_get_priv(child);
>                 if (priv) {
> diff --git a/drivers/net/sandbox-raw.c b/drivers/net/sandbox-raw.c
> index 09cc678ebd..0d1fd4d931 100644
> --- a/drivers/net/sandbox-raw.c
> +++ b/drivers/net/sandbox-raw.c
> @@ -130,21 +130,11 @@ static void sb_eth_raw_stop(struct udevice *dev)
>         sandbox_eth_raw_os_stop(priv);
>  }
>
> -static int sb_eth_raw_read_rom_hwaddr(struct udevice *dev)
> -{
> -       struct eth_pdata *pdata = dev_get_platdata(dev);
> -
> -       net_random_ethaddr(pdata->enetaddr);
> -
> -       return 0;
> -}
> -
>  static const struct eth_ops sb_eth_raw_ops = {
>         .start                  = sb_eth_raw_start,
>         .send                   = sb_eth_raw_send,
>         .recv                   = sb_eth_raw_recv,
>         .stop                   = sb_eth_raw_stop,
> -       .read_rom_hwaddr        = sb_eth_raw_read_rom_hwaddr,
>  };
>
>  static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
> diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
> index e36a5fec0e..3feb7608af 100644
> --- a/include/configs/sandbox.h
> +++ b/include/configs/sandbox.h
> @@ -95,11 +95,7 @@
>                                         "stderr=serial,vidconsole\0"
>  #endif
>
> -#define SANDBOX_ETH_SETTINGS           "ethaddr=00:00:11:22:33:44\0" \
> -                                       "eth1addr=00:00:11:22:33:45\0" \
> -                                       "eth3addr=00:00:11:22:33:46\0" \
> -                                       "eth5addr=00:00:11:22:33:47\0" \

I'm pretty sure the test/dm/eth.c tests count on these values, so you
are breaking unit tests by removing them.

> -                                       "ipaddr=1.2.3.4\0"
> +#define SANDBOX_ETH_SETTINGS           "ipaddr=1.2.3.4\0"
>
>  #define MEM_LAYOUT_ENV_SETTINGS \
>         "bootm_size=0x10000000\0" \
> --
> 2.19.1
>
> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> https://lists.denx.de/listinfo/u-boot


More information about the U-Boot mailing list