Raspberry logo

Connecting your Raspberry Pi to OpenWRT over USB

This document shows you how to connect your favorite gadget to the network using USB. The Raspberry Pi will connect over USB in gadget mode to your home gateway running OpenWRT. The solution on Raspberry Pi uses the dwc2 driver to put your USB interface in OTG mode, combined with the g_ether module to emulate ethernet over USB. On the OpenWRT gateway the solution makes use of the kmod-usb-net-rndis package.

This solution is very useful if you have a regular Raspberry Pi Zero without wireless and no simple way to connect. It can also help on e.g. Raspberry Pi Zero W when your wireless performance isn't good enough and you don't have an ethernet dongle. Or if you want to connect a regulary Raspberry Pi without using an ethernet interface.

Setup

This setup shows a GL-MT300N V2 router with cabling from left to right: Ethernet WAN, USB power, USB to Raspberry Pi Zero

Enable gadget mode on Raspberry Pi

Log on to your Raspberry Pi (running Buster or newer) and apply the following changes.

Add to /boot/config.txt at the end of the file:

dtoverlay=dwc2 

Add to cmdline.txt in /boot right after rootwait, but before other text:

modules-load=dwc2,g_ether 

This should similar to

console=serial0,115200 console=tty1 root=PARTUUID=a61534ae-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether

Change /etc/network/interfaces to

# interfaces(5) file used by ifup(8) and ifdown(8)
 # Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
 # Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
 auto lo
iface lo inet loopback
 allow-hotplug wlan0
allow-hotplug usb0
allow-hotplug eth0
 iface eth0 inet manual
 link-speed 100
 link-duplex full
 ethernet-autoneg off
 iface wlan0 inet manual
 wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
 iface usb0 inet manual

Reboot for the changes to take effect.

Enable USB host on OpenWRT

Log on to your OpenWRT router and make the following changes.

Install ethernet over USB kernel module package on OpenWRT using CLI

opkg update
opkg install kmod-usb-net-rndis

or via Luci interface under install software packages kmod-usb-net-rndis

If all is well you should now see a new interface usb0 on your router.

ScreenshotLuci

Add usb0 to the LAN interface using Luci under Network/Interfaces and edit LAN/Physical Settings/Interface

Checking throughput with iperf

Install iperf on OpenWRT

opkg update
opkg install iperf

Start iperf in server mode

iperf -s

Log on to Raspberry Pi and install iperf

sudo apt update
sudo apt install iperf

Run iperf in client mode with the target IP address of the router running iperf in server mode

iperf -c 192.168.8.1

You should see some output like

pi@raspberrypi:~ $ iperf -c 192.168.8.1
------------------------------------------------------------ Client connecting to 192.168.8.1, TCP port 5001
TCP window size:  105 KByte (default)
------------------------------------------------------------ [ 3] local 192.168.8.190 port 59342 connected with 192.168.8.1 port 5001
[ ID] Interval       Transfer     Bandwidth
[ 3]  0.0-10.0 sec   152 MBytes   127 Mbits/sec

A network throughput of 127 Mbits/sec is not a bad result for a USB 2.0 connection.

This should work with any OpenWRT router with a USB connection. Although I had some problems with my Linksys WRT3200ACM.
Also any Debian 10 or Raspian OS Buster (or later) should work for any Raspberry Pi (except pico)