FreeBSD Bugzilla – Attachment 245616 Details for
Bug 274092
if_smsc.c needs to use ether_gen_addr instead of read_random for more stable MAC address
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
use bootargs
if_smsc.c.bootargs.diff (text/plain), 4.75 KB, created by
Ronald Klop
on 2023-10-14 14:25:36 UTC
(
hide
)
Description:
use bootargs
Filename:
MIME Type:
Creator:
Ronald Klop
Created:
2023-10-14 14:25:36 UTC
Size:
4.75 KB
patch
obsolete
>diff --git a/sys/dev/usb/net/if_smsc.c b/sys/dev/usb/net/if_smsc.c >index ec8197229f..90b7091ff1 100644 >--- a/sys/dev/usb/net/if_smsc.c >+++ b/sys/dev/usb/net/if_smsc.c >@@ -179,6 +179,8 @@ static const struct usb_device_id smsc_devs[] = { > #define ETHER_IS_VALID(addr) \ > (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr)) > >+#define BOOTARGS_SMSC95XX "smsc95xx.macaddr" >+ > static device_probe_t smsc_probe; > static device_attach_t smsc_attach; > static device_detach_t smsc_detach; >@@ -1538,6 +1540,64 @@ smsc_ioctl(if_t ifp, u_long cmd, caddr_t data) > return (rc); > } > >+static bool >+smsc_get_smsc95xx_macaddr(device_t dev, char* bootargs, size_t bootargs_len, struct usb_ether *ue) >+{ >+ int values[6]; >+ int i; >+ char* p; >+ >+ device_printf(dev, "sizeof(*bootargs)=%lu %lu\n", bootargs_len, strnlen(bootargs, bootargs_len)); >+ p = strnstr(bootargs, BOOTARGS_SMSC95XX, bootargs_len); >+ device_printf(dev, "mac1=%s. %lu\n", p, strlen(BOOTARGS_SMSC95XX)); >+ >+ if (6 != sscanf(p, BOOTARGS_SMSC95XX "=%x:%x:%x:%x:%x:%x%*c", >+ &values[0], &values[1], &values[2], >+ &values[3], &values[4], &values[5])) { >+ device_printf(dev, "invalid mac from bootargs '%s'.\n", p); >+ return false; >+ } >+ >+ for (i = 0; i < ETHER_ADDR_LEN; ++i) >+ ue->ue_eaddr[i] = values[i]; >+ >+ printf("mac=%x:%x:%x:%x:%x:%x.\n", >+ ue->ue_eaddr[0], ue->ue_eaddr[1], ue->ue_eaddr[2], >+ ue->ue_eaddr[3], ue->ue_eaddr[4], ue->ue_eaddr[5]); >+ return true; >+} >+ >+/** >+ * Raspberry Pi is known to pass smsc95xx.macaddr=XX:XX:XX:XX:XX:XX via bootargs. >+ */ >+static int >+smsc_bootargs_get_mac_addr(device_t dev, struct usb_ether *ue) { >+ char bootargs[2048]; /* early stack supposedly big enough */ >+ phandle_t node; >+ >+ /* only use bootargs for the first device to prevent duplicate mac addresses */ >+ device_printf(dev, "unit=%d\n", device_get_unit(dev)); >+ if (device_get_unit(dev) != 0) >+ return 1; >+ node = OF_finddevice("/chosen"); >+ if (node == -1) >+ return 1; >+ device_printf(dev, "/chosen found\n"); >+ if (OF_getproplen(node, "bootargs") > sizeof(bootargs)) { >+ device_printf(dev, "bootargs too large for buffer"); >+ return 1; >+ } >+ if (OF_getprop(node, "bootargs", bootargs, sizeof(bootargs)) == -1) >+ return 1; >+ device_printf(dev, "bootargs: %s.\n", bootargs); >+ if (!smsc_get_smsc95xx_macaddr(dev, bootargs, sizeof(bootargs), ue)) >+ return 1; >+ device_printf(dev, "Ethernet address found in bootargs %x:%x:%x:%x:%x:%x.\n", >+ ue->ue_eaddr[0], ue->ue_eaddr[1], ue->ue_eaddr[2], >+ ue->ue_eaddr[3], ue->ue_eaddr[4], ue->ue_eaddr[5]); >+ return 0; >+} >+ > /** > * smsc_attach_post - Called after the driver attached to the USB interface > * @ue: the USB ethernet device >@@ -1586,10 +1646,23 @@ smsc_attach_post(struct usb_ether *ue) > if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) > err = usb_fdt_get_mac_addr(sc->sc_ue.ue_dev, &sc->sc_ue); > #endif >+#if 1 > if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) { >+ err = smsc_bootargs_get_mac_addr(sc->sc_ue.ue_dev, &sc->sc_ue); >+ } >+#endif >+ if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) { >+#if 1 > read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN); > sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */ > sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */ >+#else >+ struct ether_addr eaddr; >+ device_printf(ue->ue_dev, "No MAC address found. Using ether_gen_addr(). %p\n", ue->ue_ifp); >+ ether_gen_addr(ue->ue_ifp, &eaddr); >+ for (int i = 0; i < ETHER_ADDR_LEN; i++) >+ sc->sc_ue.ue_eaddr[i] = eaddr.octet[i]; >+#endif > } > } > >diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c >index 2cbe0ea98f..da285f79e6 100644 >--- a/sys/net/if_ethersubr.c >+++ b/sys/net/if_ethersubr.c >@@ -1499,6 +1499,7 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) > char digest[SHA1_RESULTLEN]; > char jailname[MAXHOSTNAMELEN]; > >+if_printf(ifp, "getcredhostuuid %s\n", __FILE__); > getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid)); > if (strncmp(uuid, DEFAULT_HOSTUUID, sizeof(uuid)) == 0) { > /* Fall back to a random mac address. */ >@@ -1507,6 +1508,7 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) > > /* If each (vnet) jail would also have a unique hostuuid this would not > * be necessary. */ >+if_printf(ifp, "getjailname %s\n", __FILE__); > getjailname(curthread->td_ucred, jailname, sizeof(jailname)); > sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, if_name(ifp), > jailname); >@@ -1515,6 +1517,7 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) > goto rando; > } > >+if_printf(ifp, "SHA1 %s\n", __FILE__); > SHA1Init(&ctx); > SHA1Update(&ctx, buf, sz); > SHA1Final(digest, &ctx); >@@ -1530,6 +1533,7 @@ ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) > > return; > rando: >+if_printf(ifp, "arc4rand %s\n", __FILE__); > arc4rand(hwaddr, sizeof(*hwaddr), 0); > /* Unicast */ > hwaddr->octet[0] &= 0xFE;
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 274092
:
245237
|
245239
|
245577
|
245603
|
245616
|
246108
|
246128
|
246231