Skip to content

fix(usb): stable gadget MAC so the host NIC keeps one MAC across reboots#828

Open
BeaconCat wants to merge 1 commit into
sipeed:mainfrom
BeaconCat:fix/usb-gadget-stable-mac
Open

fix(usb): stable gadget MAC so the host NIC keeps one MAC across reboots#828
BeaconCat wants to merge 1 commit into
sipeed:mainfrom
BeaconCat:fix/usb-gadget-stable-mac

Conversation

@BeaconCat

Copy link
Copy Markdown
Contributor

Problem

Plugging one NanoKVM (Cube) into a PC over USB ends up creating many network adapters / MAC addresses on the host — ~10 after a few reboots.

Root cause: the USB network gadget rndis.usb0 / ncm.usb0 is created in S03usbdev without setting dev_addr or host_addr, so the kernel assigns a random MAC to both the device side and the host side on every bind. S10uuid later pins the device-side usb0 with ip link set, but the host_addr — the MAC the attached PC's RNDIS/NCM adapter uses — stays random and changes on every re-enumeration/reboot. Windows then registers a brand-new adapter each time, piling up stale adapters/MACs and breaking anything that keys off the MAC (DHCP reservations, WoL, firewall rules…).

Fix

Derive both MACs deterministically from the chip UID (the same sha512sum /sys/class/cvi-base/base_uid scheme S10uuid already uses for eth0/usb0), set before binding the gadget:

dev_addr  = 48:da:35:6e:<uid>
host_addr = 48:da:35:6d:<uid>

Guarded on a non-empty UID; falls back to the old random behaviour if base_uid is unavailable. Applies to both the ncm and rndis paths.

Verified on hardware (NanoKVM Cube)

host_addr (MAC the PC sees)
before a6:3f:5d:8a:7d:52 (random, changes every boot)
after, reboot #1 48:da:35:6d:26:62
after, reboot #2 48:da:35:6d:26:62 (stable)

Fixes #740

The USB network gadget (rndis.usb0 / ncm.usb0) is created without dev_addr or
host_addr, so the kernel picks a random MAC for both the device side and the
host side on every bind. S10uuid later pins the device-side usb0 via
'ip link set', but the host_addr -- the MAC the attached PC's RNDIS/NCM adapter
uses -- stays random and changes on every re-enumeration/reboot. The host then
registers a brand-new network adapter each time, piling up stale adapters/MACs
(e.g. ~10 after a few reboots) and breaking anything that keys off the MAC.

Derive both MACs from the chip UID (same scheme S10uuid already uses for eth0
and usb0), before binding the gadget:
  dev_addr  = 48:da:35:6e:<uid>
  host_addr = 48:da:35:6d:<uid>

Verified on a NanoKVM Cube: host_addr went from random a6:3f:5d:8a:7d:52 to a
stable 48:da:35:6d:26:62, identical across two reboots.

Fixes sipeed#740
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

usb0 as network: how to keep the same mac address?

1 participant