Sunday, September 4, 2016

Pi Zero Ethernet gadget

So I'm sitting here on Labor Day weekend (US) waiting for Hurricane Hermine to reach NJ. It's predicted to follow a similar path as Sandy. Needless to say it's making us all a little nervous in the North East. All's quiet on the Eastern Front (that's a weather joke).

While waiting I decided to give the Raspberry Pi Zero a try. I followed the directions at Andrew Mulholland's Setting up Pi Zero OTG. I loaded up Raspian Jessie lite, config'd it as a Ethernet gadget and plugged it's USB into my Debian box. Up popped USB0 with a link-local address (169.254.x.x/16). I can then ssh pi@raspberrypi.local to reach it. Cool, I have a nice Tardis USB hub that the Pi will fit perfectly into.

Now comes the fun! I want to update the software. But the link-local addresses aren't really route-able (to simplify the discussion, don't do it). So how do I get the Zero connected to the internet? Well one easy route is with several networking statements and a sub interface (USB0:1). I'd put this under the category of more advanced network setup for Linux because I'll be configuring 3 machines to handle this. I've decided not do use NAT as it's unnecessary on the inside of my network. So here is the configuration commands for the various servers:

Notes: Let me start by saying that I've got a lot of networking experience and I do a lot of experimenting on my network. I've built IP tunnels, VRRP, RIPv2, OSPF and IPv6 on my systems. I'm skipping a lot of details I don't think you'll need but hopefully I haven't ignored something important. I've arbitrarily chosen to give the Pi Zero the IP address of 192.168.3.14 (USB0:1), the USB Host: 192.168.3.1 (USB0:1), the USB Host Ethernet: 192.168.0.10 (the eth0 or wlan0), The firewall is 192.168.0.1 and it also has a DNS server on it. You may need to adjust the network addresses for your own use. Also I have Quagga with RIPv2 running (not required) so I could add static routes there and my other servers would learn the route. Finally, I've not made any of this permanent except for the firewall.

For the Pi Zero (as root):

 # On the Pi Zero
sudo bash
ifconfig usb0:1 192.168.3.14
echo -e "#\nnameserver 192.168.0.1" >/etc/resolv.conf
route add default gw 192.168.3.1 usb0:1

Bonus statements for the Pi Zero, I mounted a Samba server (my NAS) to the Pi. You'll need to define the SMB_USER, SMB_PSWD and I've used the user pi and group pi in this example. On the Pi Zero (as root):

 # Make sure that cifs-utils is loaded
mkdir -p /cifs
mount.cifs //samba/{$SMB_USER}/zero /cifs \
  -o user=${SMB_USER},pass=${SMB_PSWD},\
  dir_mode=0755,file_mode=0755,uid=pigid=pi

For the USB Host (as root):

 # On the USB Host
echo 1 >/proc/sys/net/ipv4/ip_forward
# Or
sysctl -w net.ipv4.ip_forward=1
# vi /etc/sysctl.conf
# net.ipv4.ip_forward = 1
ifconfig usb0:1 192.168.3.1

On the firewall I've translated the commands to the typical Linux commands. You'll need to adjust these for your particular router. Mine is actually a Ubiquiti ERLite-3 and uses very different statements. On the firewall (as root):

 # On the router
route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.0.10
# Or
ip route add 192.168.3.0/24 via 192.168.0.10 dev eth0

Now that all has been said and done I'll let you know that the Pi Zero still supports the ssh pi@raspberrypi.local. I'm working on 2 different Tardis USB Hubs. I think one will remain a USB hub with the Pi as an Ethernet Gadget while the other I will turn into a remote USB Hub (a Pi, Wireless Dongle and the remote USB code).

No comments: