Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TalalMash/c20e6aa237e1f123ddf9686a07a1bfed to your computer and use it in GitHub Desktop.
Save TalalMash/c20e6aa237e1f123ddf9686a07a1bfed to your computer and use it in GitHub Desktop.
USB ethernet gadget on Raspberry Pi OS example compatible with Windows 10 1709+ (driverless), MacOS, iOS 13+, Android

USB ethernet gadget on Raspberry Pi OS compatible with:

  • Windows 10 1709+ (driverless)
  • MacOS
  • iOS 13+
  • Android (RNDIS is used for Android 6 and below) *1
  • ChromeOS *2
  • Linux 4.x+ *2

*1 Android will not request for an IP address if gateway is empty or internet is not found after the first connection, requiring a reboot. To mitigate, add captive portal redirect page or toggle airplane mode.

References:

https://jordemort.dev/blog/why-android-cant-use-cdc-ethernet/

https://news.ycombinator.com/item?id=44219405#:~:text=I%20wrote%20this,a%20CDC%20adapter)

TODO

  • *2 Prefer EEM/ECM for Linux by taking down RNDIS on link up hotplug event
#/etc/dnsmasq.d/usb-interfaces.conf
#Android won't setup routes without DHCP IPv4 on some phones, IPv6 only does not work.
interface=br-usb
dhcp-range=10.0.98.10,10.0.98.200,12h
bind-interfaces
#!/bin/sh
#Enable dwc2 in /boot/firmware/cmdline and config
modprobe libcomposite
ID_VENDOR="0x1d6b"
ID_PRODUCT="0x0104"
MAC_HOST0="b8:27:eb:c3:ba:09"
#MAC_DEV0="f2:fe:9f:95:db:0a" local bit does not work on Android
MAC_DEV0="f0:fe:9f:95:db:0a"
MAC_HOST1="b8:27:eb:e8:05:0f"
#MAC_DEV1="e6:d5:8e:eb:20:36" local bit does not work on Android
MAC_DEV1="e4:d5:8e:eb:20:36"
cd /sys/kernel/config/usb_gadget/
mkdir custom
cd custom
echo "0x0200" > bcdUSB
echo "0x02" > bDeviceClass
echo "0x00" > bDeviceSubClass
echo "0x3066" > bcdDevice
echo $ID_VENDOR > idVendor
echo $ID_PRODUCT > idProduct
echo "1" > os_desc/use
echo "0xcd" > os_desc/b_vendor_code
echo "MSFT100" > os_desc/qw_sign
mkdir strings/0x409
echo "9112473" > strings/0x409/serialnumber
echo "Example" > strings/0x409/manufacturer
echo "Example Pi Edition" > strings/0x409/product
mkdir configs/c.1
mkdir configs/c.1/strings/0x409
echo "CDC ECM+RNDIS" > configs/c.1/strings/0x409/configuration
mkdir functions/ecm.usb0
mkdir functions/rndis.usb0
echo "RNDIS" > functions/rndis.usb0/os_desc/interface.rndis/compatible_id
echo "5162001" > functions/rndis.usb0/os_desc/interface.rndis/sub_compatible_id
echo $MAC_HOST0 > functions/rndis.usb0/host_addr
echo $MAC_DEV0 > functions/rndis.usb0/dev_addr
echo $MAC_HOST1 > functions/ecm.usb0/host_addr
echo $MAC_DEV1 > functions/ecm.usb0/dev_addr
ln -s functions/rndis.usb0 configs/c.1
ln -s configs/c.1 os_desc
ln -s functions/ecm.usb0 configs/c.1/
echo "0x00" > bDeviceClass
ls /sys/class/udc > UDC
sleep 1
ip link add name br-usb type bridge
ip link set br-usb up
ip link set usb0 up
ip link set usb1 up
ip link set usb0 master br-usb
ip link set usb1 master br-usb
ifconfig br-usb up 10.0.98.1 netmask 255.255.255.248
systemctl restart dnsmasq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment