Page 1 of 1

udev for stable MAC address: Raspberry Pi Zero gadget mode

Posted: Mon Jan 16, 2017 8:55 pm
by scruss
I've got a Raspberry Pi Zero in Gadget Mode attached to a Linux laptop. It appears on the USB bus as

Code: Select all

Bus 003 Device 019: ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget
It's currently appearing as interface enp0s20u1 with a MAC address of 66:e0:93:30:33:73, and I'm currently remotely logged on via ssh into Raspbian Lite. This is good.

What's not so good, though, is that the MAC address and interface on the Linux box seem to be completely random on startup: it was enp0s20u2u2 before, with the MAC set to 8a:94:1c:a1:f5:a2. Needless to say, this makes having a stable IP address to log in to rather difficult

Is there a way to have the Raspberry Pi Zero produce a consistent MAC address? Failing that, any suggestions for udev rules for the connected Linux box to identify the same Zero on the same MAC?

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Posted: Tue Jan 17, 2017 6:16 am
by elkberry
Does this one here may help? http://linux-sunxi.org/USB_Gadget/Ethernet says...

/etc/modprobe.d/g_ether.conf:
options g_ether host_addr=00:11:22:33:44:55

Device name depends on the USB port where you plug in your device.

[SOLVED] stable MAC address: Raspberry Pi Zero gadget mode

Posted: Tue Jan 17, 2017 6:28 pm
by scruss
Thank you! That was a most useful lead.

If you do this once on your Raspberry Pi Zero:

Code: Select all

echo 'options g_ether host_addr='$(dmesg | awk '/: HOST MAC/{print $NF}')' dev_addr='$(dmesg | awk '/: MAC/{print $NF}') | sudo tee /etc/modprobe.d/g_ether.conf
you'll end up with stable network addressing.

Update: you probably want to do the cmdline.txt thing below as well/instead, as this stopped working reliably for me. Editing /boot/cmdline.txt seems to be more reliable.

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Posted: Sat Jan 21, 2017 4:22 am
by tweak42
You can also pass the kernel parameters for static MAC addresses to the g_ether module in the cmdline.txt
I prefer this method because it's on the FAT boot partition and easily added when enabling gadget mode.

Code: Select all

modules-load=dwc2,g_ether g_ether.host_addr=00:22:82:ff:ff:20 g_ether.dev_addr=00:22:82:ff:ff:22
g_ether.dev_addr is the Pi Zero interface.
g_ether.host_addr is the host PC interface.

viewtopic.php?t=166002&p=1069725#p1069670

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Posted: Sat Jan 21, 2017 5:13 pm
by scruss
Yup, that's another way to do it. While it does have the advantage of being on the boot partition for easy access on other systems, mucking about with cmdline.txt programmatically is not something I feel confident about doing.

It would be nice if there were a startup config for the Zero that, noticing it was in Gadget mode, would cache the MAC addresses for reuse. Stability in network addressing is a good thing.

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Posted: Sat Feb 18, 2017 4:29 am
by scruss
I meant to add that I've found that certain systems (like my Ubuntu laptop) timeout the network connection for USB networking if it's not immediately available. What I've found helpful is start the Zero with a power bank/cable into the POWER socket, then when the Zero appears to have booted, plug in the USB data cable too. It should then be immediately recognized at the MAC addresses you specified. You can - if you're feeling brave - take the power connector out once you have a data connection.

Re: udev for stable MAC address: Raspberry Pi Zero gadget mode

Posted: Sun Mar 04, 2018 9:38 pm
by Schorschi
Who is the idiot, yes I say idiot, that thought a dynamic MAC address by default is a good thing? This is a horrible issue for network security design and control systems. The default SHOULD BE STATIC, have total human intelligence for network device design dropped sharply recently?

Re: udev for stable MAC address: Raspberry Pi Zero gadget mode

Posted: Mon Mar 05, 2018 12:35 pm
by HawaiianPi
Schorschi wrote:
Sun Mar 04, 2018 9:38 pm
Who is the idiot, yes I say idiot, that thought a dynamic MAC address by default is a good thing? This is a horrible issue for network security design and control systems. The default SHOULD BE STATIC, have total human intelligence for network device design dropped sharply recently?
The Pi Zero has no Ethernet hardware, so there is no hard coded Ethernet MAC address. USB Gadget Mode has been a work-in-progress for awhile now, and there have been some improvements (it's much easier to setup now), but there is still room for further improvement.

Code: Select all

echo 'options g_ether host_addr='$(dmesg | awk '/: HOST MAC/{print $NF}')' dev_addr='$(dmesg | awk '/: MAC/{print $NF}') | sudo tee /etc/modprobe.d/g_ether.conf
Worked for me, and has continued to work.

I honestly don't understand the point of USB Gadget Mode. It's really been nothing more than a curiosity to me, so apparently I've missed something. What does connecting a far less powerful computer to a far more powerful computer accomplish? I can see some possible uses in education, which is what the Raspberry Pi was created for, but other than that I don't get it.

With Raspbian Stretch it's possible to pre-configure an SD card to boot any Pi, connect to a wireless network and SSH into it, so why would I use USB Gadget Mode over doing that?

Anyway, like I said, it's an amusing curiosity to play with, but I don't find it very useful to tether a Pi to my main PC when I have the option of running them independently.

Re: udev for stable MAC address: Raspberry Pi Zero gadget mode

Posted: Wed Mar 07, 2018 2:43 am
by ejolson
Schorschi wrote:
Sun Mar 04, 2018 9:38 pm
Who is the idiot, yes I say idiot, that thought a dynamic MAC address by default is a good thing? This is a horrible issue for network security design and control systems. The default SHOULD BE STATIC, have total human intelligence for network device design dropped sharply recently?
Using MAC addresses for security in any situation is about as insecure as using IP numbers. In this case we are using a virtual Ethernet device over USB. I don't think it is likely someone could secretly plug a Zero into my USB hub without me noticing. At the same time, it can be inconvenient for the MAC address to change with every reboot. Another example of preventing this using rpiboot is described in my post on a cluster of Pi Zero computers.