Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Wed Feb 17, 2016 1:45 pm

I am trying to use my raspberry pi zero together with my raspberry pi 2 and I want to share network access from my raspberry pi 2 to my raspberry pi zero. I can easily create a local network connection between my raspberry pis, but I have a problem sharing network access.

First of I followed this guide to turn my raspberry pi zero into a ethernet gadget. Then I added this to /etc/network/interfaces:

Code: Select all

    allow-hotplug usb0
    iface usb0 inet static
            address 10.0.1.2
            netmask 255.255.255.0
            gateway 10.0.0.1
I configured usb0 on my raspberry pi 2 like this:

Code: Select all

    allow-hotplug usb0
    iface usb0 inet static
            address 10.0.1.1
            netmask 255.255.255.0
            gateway 10.0.0.1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I could ssh into my raspberry pi zero with ssh [email protected] but my next goal was to share network access, so I could run stuff like ping google.com on my raspberry pi zero. I found this guide which explains how to forward network access from wlan0 to eth0 and I tried doing the same with usb0.

I added pre-up iptables-restore < /etc/network/iptables.conf to /etc/network/interfaces. This is the content of my iptables.conf file:

Code: Select all

    # Generated by iptables-save v1.4.21 on Tue Feb 16 16:41:38 2016
    *mangle
    :PREROUTING ACCEPT [472:58950]
    :INPUT ACCEPT [324:32281]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [30694:35635555]
    :POSTROUTING ACCEPT [30694:35635555]
    COMMIT
    # Completed on Tue Feb 16 16:41:38 2016
    # Generated by iptables-save v1.4.21 on Tue Feb 16 16:41:38 2016
    *filter
    :INPUT ACCEPT [57:4180]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [2194:2536976]
    -A FORWARD -i usb0 -j ACCEPT
    -A FORWARD -i wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    COMMIT
    # Completed on Tue Feb 16 16:41:38 2016
    # Generated by iptables-save v1.4.21 on Tue Feb 16 16:41:38 2016
    *nat
    :PREROUTING ACCEPT [6:821]
    :INPUT ACCEPT [1:242]
    :OUTPUT ACCEPT [0:0]
    :POSTROUTING ACCEPT [0:0]
    -A POSTROUTING -o wlan0 -j SNAT --to-source 10.0.0.115
    COMMIT
    # Completed on Tue Feb 16 16:41:38 2016
I enabled ip forwarding in the kernel by uncommenting this line in etc/sysctl.conf

Code: Select all

    # Uncomment the next line to enable packet forwarding for IPv4
    net.ipv4.ip_forward=1
Finally I executed sysctl --system.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Unfortunently network is still unreachable on my raspberry pi zero after rebooting both my raspberry pi 2 and raspberry pi zero. This is the output of route on my raspberry pi 2:

Code: Select all

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         10.0.0.1        0.0.0.0         UG    0      0        0 wlan0
    10.0.0.0        *               255.255.255.0   U     0      0        0 wlan0
    10.0.1.0        *               255.255.255.0   U     0      0        0 usb0


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

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Thu Feb 18, 2016 11:20 am

Your gateway settings on USB0 are wrong. On the zero the gateway addresss should be the address of the pi2. On the pi2 no gateway should be set on USB0 (one should be set on the interface the pi2 uses to connect to the internet).

Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Thu Feb 18, 2016 12:01 pm

plugwash wrote:Your gateway settings on USB0 are wrong. On the zero the gateway addresss should be the address of the pi2. On the pi2 no gateway should be set on USB0 (one should be set on the interface the pi2 uses to connect to the internet).
So I changed /etc/network/interfaces on the pi zero to:

Code: Select all

source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
allow-hotplug usb0
iface usb0 inet static
        address 10.0.1.2
        netmask 255.255.255.0
        gateway 10.0.2.1
And on the pi 2:

Code: Select all

auto lo
iface lo inet loopback

allow-hotplug wlan0
iface wlan0 inet static
address 10.0.0.115
gateway 10.0.0.1
netmask 255.255.255.0
dns-nameservers 10.0.0.1 8.8.8.8 8.8.4.4
wpa-conf /etc/wpa.conf

allow-hotplug usb0
iface usb0 inet static
address 10.0.1.1
netmask 255.255.255.0

pre-up iptables-restore < /etc/network/iptables.conf
This is the output of route on the pi zero:

Code: Select all

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.1.0        *               255.255.255.0   U     0      0        0 usb0
link-local      *               255.255.0.0     U     203    0        0 usb0
Unfortunately stuff like ping 10.0.0.150 still fails ("connect: Network is unreachable").

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

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Thu Feb 18, 2016 12:09 pm

Erm, according to what you just pasted you set the gateway on the pi0 to 10.0.2.1 but the address on the pi2 to 10.0.1.1

Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Thu Feb 18, 2016 12:15 pm

plugwash wrote:Erm, according to what you just pasted you set the gateway on the pi0 to 10.0.2.1 but the address on the pi2 to 10.0.1.1
Ahah, thanks plugwash. How silly of me... :oops:

After correctly setting the gateway to 10.0.1.1 on the pi zero and rebooting everything, I can do ping 10.0.0.150 and ping google.com from my pi zero via merely one cable plugged into my pi zero!

Thanks a lot for the help!

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

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Thu Feb 18, 2016 1:19 pm

Glad you found a solution that works for you, my solution was somewhat different, but whatever works for you.

You may find it worthwhile to investigate using the g_cdc module rather then g_ether, it's main advantage being that it appears to the host as a usb-serial port (/dev/ttyACM0) in addition to an ethernet port. This is extremely useful when setting up, as it means you can get a serial login to set up networking (via minicom from your host), as opposed to using ssh, where you need to get everything network related right before you can login at all. The other big advantage is that if you need to reboot the Pi Zero, minicom will hang in there and re-connect, as opposed to ssh dropping the connection.

Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Thu Feb 18, 2016 4:52 pm

rotwang wrote:Glad you found a solution that works for you, my solution was somewhat different, but whatever works for you.

You may find it worthwhile to investigate using the g_cdc module rather then g_ether, it's main advantage being that it appears to the host as a usb-serial port (/dev/ttyACM0) in addition to an ethernet port. This is extremely useful when setting up, as it means you can get a serial login to set up networking (via minicom from your host), as opposed to using ssh, where you need to get everything network related right before you can login at all. The other big advantage is that if you need to reboot the Pi Zero, minicom will hang in there and re-connect, as opposed to ssh dropping the connection.
Yeah, I used a USB to TTL cable to get a serial console where I could configure networking. Thanks for the tip by the way!

Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Sun Feb 21, 2016 6:32 pm

So after a couple of days this solution works really good. I have one small problem however. If I reboot my rpi 2, the network connection will not automatically reestablish itself and I have to restart my rpi zero aswell.

Can I somehow make sure the connection between my rpi zero and rpi 2 is reestablished if I restart my rpi 2?

Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Mon Feb 22, 2016 1:03 pm

Does anyone have any suggestions? :?:

User avatar
DougieLawson
Posts: 36510
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Mon Feb 22, 2016 1:10 pm

I assume there's an

Code: Select all

iface usb0 inet manual
line in your interfaces file (or something like that). Have you got a matchng

Code: Select all

auto usb0
or

Code: Select all

allow-hotplug usb0
in your inerfaces file?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Mon Feb 22, 2016 3:41 pm

DougieLawson wrote:I assume there's an

Code: Select all

iface usb0 inet manual
line in your interfaces file (or something like that). Have you got a matchng

Code: Select all

auto usb0
or

Code: Select all

allow-hotplug usb0
in your inerfaces file?
This is my interface file on the pi 2:

Code: Select all

auto lo
iface lo inet loopback

allow-hotplug wlan0
iface wlan0 inet static
address 10.0.0.115
gateway 10.0.0.1
netmask 255.255.255.0
dns-nameservers 10.0.0.1 8.8.8.8 8.8.4.4
wpa-conf /etc/wpa.conf

allow-hotplug usb0
iface usb0 inet static
address 10.0.1.1
netmask 255.255.255.0

pre-up iptables-restore < /etc/network/iptables.conf
This is the interface file on my pi zero:

Code: Select all

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug usb0
iface usb0 inet static
        address 10.0.1.2
        netmask 255.255.255.0
        gateway 10.0.1.1
	dns-nameservers 10.0.1.1 8.8.8.8 8.8.4.4
Again, this works fine. Only I have an issue where rebooting the pi 2 causes the network access on the pi zero to drop and I have to reboot it aswell. Maybe this can be "solved" by rebooting the pi zero whenever the pi 2 reboots?

User avatar
DougieLawson
Posts: 36510
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Mon Feb 22, 2016 11:49 pm

Is it running Rasbian Wheezy or Raspbian Jessie?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Linkan
Posts: 30
Joined: Tue Feb 10, 2015 3:56 pm

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Mon Feb 22, 2016 11:58 pm

DougieLawson wrote:Is it running Rasbian Wheezy or Raspbian Jessie?
Latest version of Raspbian Jessie downloaded from raspberrypi.org.

User avatar
DougieLawson
Posts: 36510
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: IPv4 forwarding from RPi 2 to RPi zero via OTG cable

Tue Feb 23, 2016 12:59 am

Then you need to undo all the changes you've made to /etc/network/interfaces and simply add two lines for your USB network device (if it doesn't appear as eth0 or wlan0).

All network configuration stuff is done in /etc/dhcpcd.conf on Jessie.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “General discussion”