Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/usb allocate a random mac address if on-chip mac is ...



details:   https://anonhg.NetBSD.org/src/rev/aaa2d80c58e4
branches:  trunk
changeset: 966158:aaa2d80c58e4
user:      bad <bad%NetBSD.org@localhost>
date:      Wed Oct 16 13:11:16 2019 +0000

description:
allocate a random mac address if on-chip mac is all zeroes.

helps ure(4) on NanoPi R1.

partly from ganbold@freebsd r346052.

XXX pullup-9

diffstat:

 sys/dev/usb/if_ure.c |  19 +++++++++++++++++--
 1 files changed, 17 insertions(+), 2 deletions(-)

diffs (61 lines):

diff -r 825148d31287 -r aaa2d80c58e4 sys/dev/usb/if_ure.c
--- a/sys/dev/usb/if_ure.c      Wed Oct 16 13:02:51 2019 +0000
+++ b/sys/dev/usb/if_ure.c      Wed Oct 16 13:11:16 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_ure.c,v 1.32 2019/10/16 13:02:51 bad Exp $  */
+/*     $NetBSD: if_ure.c,v 1.33 2019/10/16 13:11:16 bad Exp $  */
 /*     $OpenBSD: if_ure.c,v 1.10 2018/11/02 21:32:30 jcs Exp $ */
 
 /*-
@@ -30,7 +30,7 @@
 /* RealTek RTL8152/RTL8153 10/100/Gigabit USB Ethernet device */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1.32 2019/10/16 13:02:51 bad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1.33 2019/10/16 13:11:16 bad Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -38,6 +38,7 @@
 #endif
 
 #include <sys/param.h>
+#include <sys/cprng.h>
 
 #include <net/route.h>
 
@@ -66,6 +67,9 @@
 #define DPRINTFN(n, x)
 #endif
 
+#define ETHER_IS_ZERO(addr) \
+       (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
+
 static const struct usb_devno ure_devs[] = {
        { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8152 },
        { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8153 }
@@ -848,6 +852,7 @@
        uint16_t ver;
        uint8_t eaddr[8]; /* 2byte padded */
        char *devinfop;
+       uint32_t maclo, machi;
 
        aprint_naive("\n");
        aprint_normal("\n");
@@ -949,6 +954,16 @@
                ure_read_mem(un, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr,
                    sizeof(eaddr));
        usbnet_unlock(un);
+       if (ETHER_IS_ZERO(eaddr)) {
+               maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
+               machi = cprng_strong32() & 0xffff;
+               eaddr[0] = maclo & 0xff;
+               eaddr[1] = (maclo >> 8) & 0xff;
+               eaddr[2] = (maclo >> 16) & 0xff;
+               eaddr[3] = (maclo >> 24) & 0xff;
+               eaddr[4] = machi & 0xff;
+               eaddr[5] = (machi >> 8) & 0xff;
+       }
        memcpy(un->un_eaddr, eaddr, sizeof un->un_eaddr);
 
        struct ifnet *ifp = usbnet_ifp(un);



Home | Main Index | Thread Index | Old Index