How to set up a Raspberry Pi Zero as an OTG g_ether gadget?

Ask questions about Arch Linux ARM. Please search before making a new topic.

How to set up a Raspberry Pi Zero as an OTG g_ether gadget?

Postby arabic » Thu Apr 04, 2019 11:29 pm

I'm trying to set up my RPi Zero as RNDIS/OTG/g_ether/dwc2/whatever gadget. I installed Arch as described in the Wiki and then ran the following commands:

$this->bbcode_second_pass_code('', '
echo "dwc2" | sudo tee -a root/etc/modules-load.d/rapsberrypi.conf
echo "g_ether" | sudo tee -a root/etc/modules-load.d/rapsberrypi.conf
echo "dtoverlay=dwc2" | sudo tee -a boot/config.txt
echo "options g_ether dev_addr=$SOME_MAC_ADDRESS host_addr=$ANOTHER_MAC_ADDRESS " | sudo tee root/etc/modprobe.d/g_ether.conf
# /etc/netctl/usbpizero
Description='g_ether gadget'
Interface=usb0
Connection=ethernet
IP=dhcp
')

with the MAC addressed filled in with random ones. It boots and I cannot seem to find an error message regarding g_ether. The network adapter even shows up in Windows 10, but it states "Network cable unplugged" with a red cross next to the adapter icon. I will provide more printouts on demand, but for now I am interested in whether I am missing an obvious step. I also tried replacing the newline characters in config.txt in editor.exe in Windows to get CR LF, but this did not seem to help. However I meay have missed one at the end.
arabic
 
Posts: 1
Joined: Thu Apr 04, 2019 11:20 pm

Re: How to set up a Raspberry Pi Zero as an OTG g_ether gadg

Postby summers » Fri Apr 05, 2019 9:47 am

Obvious problem is that you'll what to run dhcp as a *server* on the RPiZ; you have set it up as a client.

You'll also need to make sure you set up routing correctly on whatever connects to the WAN. That takes some though, e.g. if the WAN doesn't know *all* addresses inside the LAN, you'll need to set up a masquerade, either via ip tables or nf tables.

What will help is logging directly on the RPiZ so you can see what has come up. Attaching via the UART is the obvious direction. As much as anything debugging is far easier when you can see what is happening .... your friends are $this->bbcode_second_pass_code('', 'ip a
ip route')

As an example, what I do on my pocket beagle, where I use the libcomposite/systemd network set up is:
/etc/systemd/network/usb0.network
$this->bbcode_second_pass_code('', '[Match]
Name=usb0

[Network]
DNS=192.168.2.1
IPv6PrivacyExtensions=true

[Address]
Address=192.168.7.18/30

[Route]
Gateway=192.168.7.17')
So I bring up the usb ethernet with an ip address, and also with the knowledge of where the gateway to the WAN is and also my DNS server (192.168.2.1 is my ADSL router ...)

dhcpd is running:
$this->bbcode_second_pass_code('', '[summers@pocketbeagle ~]$ systemctl status dhcpd4@usb0
* dhcpd4@usb0.service - IPv4 DHCP server on usb0
Loaded: loaded (/etc/systemd/system/dhcpd4@.service; enabled; vendor preset:>
Active: active (running) since Thu 2019-02-21 13:46:27 UTC; 1 months 12 days>
Process: 1697 ExecStart=/usr/bin/dhcpd -4 -q -pf /run/dhcpd4.pid usb0 (code=e>
Main PID: 1702 (dhcpd)
Tasks: 1 (limit: 1041)
Memory: 4.9M
CGroup: /system.slice/system-dhcpd4.slice/dhcpd4@usb0.service
`-1702 /usr/bin/dhcpd -4 -q -pf /run/dhcpd4.pid usb0
')
dhcpd is configured
$this->bbcode_second_pass_code('', '[summers@pocketbeagle ~]$ more /etc/dhcpd.conf
subnet 192.168.7.16 netmask 255.255.255.252 {
range 192.168.7.17 192.168.7.17;
}
')
Checking all these is far easier if you can log onto the RPiZ ....

Oh yes 192.168.7.17 has an interesting route
$this->bbcode_second_pass_code('', '[summers@nas ~]$ ip route
default via 192.168.2.1 dev eth0 proto dhcp src 192.168.2.111 metric 1024
192.168.2.0/24 dev eth0 proto kernel scope link src 192.168.2.111
192.168.2.1 dev eth0 proto dhcp scope link src 192.168.2.111 metric 1024
192.168.7.0/30 dev usb0 proto kernel scope link src 192.168.7.1
192.168.7.16/30 dev usb1 proto dhcp scope link src 192.168.7.17 metric 205
')
And that machine also has nftables running
$this->bbcode_second_pass_code('', '[summers@nas ~]$ sudo nft list ruleset
table ip nat {
chain prerouting {
type nat hook prerouting priority 0; policy accept;
}

chain postrouting {
type nat hook postrouting priority 100; policy accept;
masquerade
}
}
table inet filter {
chain input {
type filter hook input priority 0; policy accept;
ct state { established, related } accept
ct state invalid drop
iifname "lo" accept
ip protocol icmp accept
ip6 nexthdr ipv6-icmp accept
tcp dport ssh accept
meta nfproto ipv4 reject
}

chain forward {
type filter hook forward priority 0; policy accept;
}

chain output {
type filter hook output priority 0; policy accept;
}
}
')
And to make routing on the LAN easier, the DNS server 192.168.2.1 has:
$this->bbcode_second_pass_code('', 'root@DavidSummers:~# ip route
default via 195.166.130.251 dev pppoa-wan
192.168.2.0/24 dev br-lan scope link src 192.168.2.1
192.168.7.0/24 via 192.168.2.111 dev br-lan
195.166.130.251 dev pppoa-wan scope link src 84.93.181.88
')
So it knows that 192.168.7.0/24 lives on 192.168.2.111 which is the LAN name of 192.168.7.17 (e.g. that machine has a shed load of ethernet, all with different addresses ....)
summers
 
Posts: 995
Joined: Sat Sep 06, 2014 12:56 pm


Return to User Questions

Who is online

Users browsing this forum: No registered users and 5 guests