rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Pi Zero device mode MAC address

Fri Jan 22, 2016 9:18 pm

Trying out device mode with g-cdc module, serial part is o.k. host sees /dev/ttyACM0 and I can login. The ethernet side is there, host sees interface usb0 but MAC address appears to be a random number. Anybody know where this is set?
If I recall correctly pi's with ethernet derive their mac address from their serial number, if possible I would like to do the same for the zero.
Running kernel "4.4.0+ #833 Mon Jan 11 18:33:20 GMT 2016 armv6l GNU/Linux"
/proc/cmdline contains "....smsc95xx.macaddr=B8:27:EB:4B:85:B8....."
ifconfig on pi zero gives usb0 HWaddr 52:6a:79:32:e5:0a

ifconfig on host gave usb0 HWaddr as ea:9b:f2:62:58:9b the first time
followed by be:be:bb:71:a9:34 after rebooting pi zero.

All thoughts welcome,
Roger

mattmiller
Posts: 2247
Joined: Thu Feb 05, 2015 11:25 pm

Re: Pi Zero device mode MAC address

Fri Jan 22, 2016 11:03 pm

Don't know answer but was wondering what the practical issue/problem is?

As the Pi is going to be directly connected to another computer - does it matter?

genuinely just curious :)

Matthew

plugwash
Forum Moderator
Forum Moderator
Posts: 3748
Joined: Wed Dec 28, 2011 11:45 pm

Re: Pi Zero device mode MAC address

Sat Jan 23, 2016 12:14 am

As you see in your tests an "ethernet gadget" actually has TWO mac addresses. One for each end (just as you would have two MAC addresses if you connected two PCs with a crossover cable).

It seems that by default both are randomly generated but you can override them by passing parameters when loading the gadget driver.

http://linux-sunxi.org/USB_Gadget#Loadi ... _device.29

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: Pi Zero device mode MAC address

Sat Jan 23, 2016 11:17 am

Partly I don't like random MAC addresses that change on every reboot, and partly because I use the MAC address to unambiguously identify which device I am talking to.
mattmiller wrote:Don't know answer but was wondering what the practical issue/problem is?

As the Pi is going to be directly connected to another computer - does it matter?

genuinely just curious :)

Matthew

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: Pi Zero device mode MAC address

Sat Jan 23, 2016 11:24 am

That clears up quite a lot of things, thank you.
One further question, are both random MAC addresses generated by the "gadget" end, or does each end generate its own.
Roger
plugwash wrote:As you see in your tests an "ethernet gadget" actually has TWO mac addresses. One for each end (just as you would have two MAC addresses if you connected two PCs with a crossover cable).

It seems that by default both are randomly generated but you can override them by passing parameters when loading the gadget driver.

http://linux-sunxi.org/USB_Gadget#Loadi ... _device.29

plugwash
Forum Moderator
Forum Moderator
Posts: 3748
Joined: Wed Dec 28, 2011 11:45 pm

Re: Pi Zero device mode MAC address

Sat Jan 23, 2016 11:27 am

rotwang wrote: One further question, are both random MAC addresses generated by the "gadget" end, or does each end generate its own.
That page on linux-sunxi seems to imply that both are generated by the "gadget" end but I don't know for sure.

User avatar
thagrol
Posts: 5277
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Pi Zero device mode MAC address

Sat Jan 23, 2016 1:51 pm

I got g_cdc to use a "fixed" MAC address as follows:

Having loaded the g_cdc module run:

Code: Select all

dmesg|grep MAC
to get the current MAC addresses.

Make a note of both MAC addresses.

When loading g_cdc instead of using modprobe g_cdc use

Code: Select all

modprobe dev_addr=<MAC address 1> host_addr=<MAC address 2>
e.g.

Code: Select all

modprobe g_cdc dev_addr=02:9a:a2:35:63:0e host_addr=d2:d2:c7:d3:ed:90
I'm currently running g_cdc from /etc/rc.local so can't tell you where to put the parameters if you're using /etc/modules or some other method.

The above should also work for g_ether.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: Pi Zero device mode MAC address

Sat Jan 23, 2016 2:50 pm

Following information, shamelessly ripped from a post by nmaas87 on the pull request thread makes sense of it all.
Begin extract :-

IMHO:
I think the host_addr and dev_addr are important in the fact that they are different. Yes, normally you are completly right, a ethernet/network interface is only allowed to have one mac address. But, I think the OTG Ethernet does emulate a "complete network" in that sense that you can see the OTG connection between host pc and pi zero not only as ONE network interface, but instead two.
So you got the ethernet interface on your pizero, which has the dev_addr and you got another ethernet interface on the pc, which needs to have a different mac, the host_addr. And those two virtual ethernet interfaces do communicate with each other.

HOST PC ------------------------------------------- OTG Ethernet ----------------------------------------------- PiZero
equals
HOST PC --- HOST Virtual Ethernet Card --- Virtual Wire ---- PiZero Virtual Ethernet Card ---- PiZero

So that would make sense - but I could be complelty wrong here.
End extract

Coupled with an entry in /etc/modprode.d/g_cdc this may be my solution.

P.S. Any idea how it came to be called g_cdc, which is a blindingly non-obvious clue to implementing an ethernet device, or is this one of those "seemed like a good idea once and it's too late to change it now"(TM) things.
Roger

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: Pi Zero device mode MAC address

Sun Jan 24, 2016 10:53 am

Thanks to everybody who chipped in with information, I think I have a handle on this now.
Next step is to write everything down, then start from a blank microSD, follow my own steps and check that it works.

g_cdc is particularly interesting, since I can use ssh, scp and family and still have a login on what amounts to an out-of-band channel via the serial part to check whats going on.

Again many thanks to all,
Roger

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: Pi Zero device mode MAC address

Thu Jan 28, 2016 7:05 pm

I think I now have a handle on the whole thing now, to the stage where I can open two terminal windows on my host desktop and start one session on the Pi Zero via minicom and the OTG usb port, and start another session on the second terminal via ssh.

I can also, from the minicom session, connect back to my host desktop by doing ssh on the Pi Zero. If you try this, be very, very sure which session you are typing into. (Ask Me How I Know(TM)).

The really important thing is to visualise the connection as two virtual ethernet interfaces connected via a switch, which means each virtual interface requires both a MAC address AND an IP address and the MAC addresses, and the IP addresses MUST be different, as they would be for a real connection.

I now need to go through the whole process, starting from a blank SD card, just to make sure I have noted all the steps correctly, before I post here as a Howto in the next day or so.
Roger

echomancer
Posts: 13
Joined: Fri Feb 27, 2015 4:29 am

Re: Pi Zero device mode MAC address

Tue Feb 02, 2016 3:24 am

Any updates on the how to?

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: Pi Zero device mode MAC address

Tue Feb 02, 2016 9:01 pm

Ah yes, just going through the tedious process of following my own written instructions just to make sure what I thought I did matches with what I actually did.
Statutary warning -- Here be dragons -- make sure you do this on a fresh micro-sd card. I've clocked up 20+ hours running with no problems, but some of this software is officially alpha -- you've been warned.

In essence, you follow gbaman's guide, running rpi-update to get a 4.4 series kernel, but I recommend using g_cdc rather then g_ether, since this will allow you a serial login as well as ssh. You'll need the serial login if/when you mess up the network addressing.
Then at the pi zero end you have to setup two MAC address, one IP address plus a route and a nameserver,
and at the host end you need to setup an IP adress, two iptables lines and turn on forwarding. Your Pi zero can then access the internet via the host's connection, and can be accessed from the host via minicom and via ssh (simultaneously if you like).

The afternoon I was able to do "apt-get update" and "apt-get upgrade" direct from the pi zero, plus "apt-get install" one or two things I needed.

Bar any outrageous typing errors and forgetting where to insert "sudo", the Howto should be ready to go in the next couple of days, "real soon now"(TM).
Roger
echomancer wrote:Any updates on the how to?

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: Pi Zero device mode MAC address and internet access

Thu Feb 04, 2016 11:37 am

Sorry for a further delay on this, it turns out, on proofreading, that what I had written leads to a chicken and egg situation, whereby you need the connectivity to get the update that gives you the connectivity. The setup works, it's just the explanation of how to get there that's defective, in particular at one point I had written about using a virtual ethernet connection to setup that same connection. So I need to rewrite that whole section using only the serial link, doing the necessary edits, then rebooting the PiZero to bring up the ethernet link, which actually looks more logical and easy to explain.

Progress so far -
Using only a USB cable between PiZero and my desktop, from the desktop I can -
Use minicom to login over a virtual serial port connection
Use ssh to login over virtual ethernet connection
From a minicom or ssh session running on the PiZer0 I can -
Access the internet - apt-get update, apt-get upgrade, apt-get install and all that.

So far I have clocked up 20+ hours using just this USB connection and it's been rock solid, despite the 4.4 kernel being alpha status, so everything is looking good except for the documentation, but I guess that's just the normal state of the world.
Roger

blimpyway
Posts: 622
Joined: Mon Mar 19, 2018 1:18 pm

Re: Pi Zero device mode MAC address

Mon Mar 19, 2018 1:43 pm

Hi,

I had (and solved) the same problem networking a Pi Zero with my ubuntu 14.0.4 laptop via usb cable.

The problem is the Pi's g_cdc module (loaded in /boot/cmdline.txt) generates a new random MAC for both Pi Zero and my laptop (host) every time I connect them together (and PI Zero restarts)

Every time I can connect them manually via Ubuntu's desktop network manager by going into "click network icon" -> "Edit Connections" -> click on "Wired connection 7" -> "IPv4 settings" -> chose Method as 'Shared to other computers'

But when I reconnect or reboot the Pi Zero it generates a new HOST MAC on usb0 and ubuntu takes it as another different adapter called "Wired connection 8" so the above configuration is ignored.
==========
This behaviour can be fixed by loging in PI via getty over usb serial link using minicom and issuing the command:
pi@raspberrypi:~$ sudo echo 'options g_cdc host_addr=XX:XX:XX:XX:XX:XX' > /etc/modprobe.d/g_cdc.conf

Where "XX:XX:XX:XX:XX:XX" is any mac address identified previously in ubuntu's network manager.

This way the Pi Zero assigns a fixed MAC address to the "host end" of the usb link every time it restarts and ubuntu will re-use automatically the associated saved configuration for that respective mac.

Regards.

Return to “General discussion”