USB Gadget Ethernet

USB Ethernet mainly implements using the device’s OTG interface in peripheral mode, simulating a network interface, allowing the host to connect to the device via USB and access the internet through the device. The following are the specific operations based on the LKD3568.

Operating Environment:

  • Ubuntu system PC

  • LKD3568

Kernel Settings

In the kernel directory, open the kernel configuration options menu:

cd kernel
ARCH=arm64 make menuconfig

After entering the kernel configuration menu, select: Device Drivers -> USB Support -> USB Gadget Support. Set USB Gadget Driver to compile as a module, then find the Ethernet Gadget (with CDC Ethernet support) option below and also select it to compile as a module. Also, select RNDIS support.

<M>   USB Gadget Drivers
<M>     USB functions configurable through configfs
<M>     Ethernet Gadget (with CDC Ethernet support)
[*]       RNDIS support (NEW)

Save the kernel configuration options menu:

ARCH=arm64 make savedefconfig

After saving, the SDK root directory will generate a defconfig file.

Replace the config:

cp defconfig arch/arm64/configs/rockchip_linux_defconfig

Then compile the kernel in the kernel directory:

cd ..
./build.sh kernel

After compiling, flash the kernel to the device. For the flashing process, please refer to the Upgrade Firmware page of the respective board’s wiki. Then, copy the following modules generated in the kernel directory to the device:

  • drivers/usb/gadget/function/u_ether.ko

  • drivers/usb/gadget/function/usb_f_ecm_subset.ko

  • drivers/usb/gadget/function/usb_f_ecm.ko

  • drivers/usb/gadget/function/usb_f_rndis.ko

  • drivers/usb/gadget/function/usb_f_eem.ko

  • drivers/usb/gadget/legacy/g_ether.ko

  • drivers/usb/gadget/libcomposite.ko

Then, on the device, load the above modules in sequence:

insmod libcomposite.ko
insmod u_ether.ko
insmod usb_f_ecm_subset.ko
insmod usb_f_rndis.ko
insmod usb_f_ecm.ko
insmod usb_f_eem.ko
insmod g_ether.ko

Note: Load libcomposite.ko and u_ether.ko first, then the other modules can be loaded.

IP Address Configuration

Connect the PC and the device’s OTG interface with a data cable. Execute the lsusb command on the PC to see the USB Ethernet device, indicating a successful connection.

neardi@Desktop:~$ lsusb
Bus 002 Device 003: ID 09da:5814 A4Tech Co., Ltd.
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 005: ID 04f2:b2ea Chicony Electronics Co., Ltd Integrated Camera [ThinkPad]
Bus 001 Device 004: ID 0a5c:21e6 Broadcom Corp. BCM20702 Bluetooth 4.0 [ThinkPad]
Bus 001 Device 006: ID 2207:0011 Fuzhou Rockchip Electronics Company RK3568 USB Gadget

On the device, configure the IP address of the usb0 network card:

ifconfig usb0 192.168.1.51

Note: The IP of usb0 and the wired network card eth0 should not be in the same subnet!

On the PC, configure the IP:

# Check the USB virtual network card first
neardi@Desktop:~$ ifconfig
enp0s20u2i1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.90  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::871c:b87e:1327:7fd4  prefixlen 64  scopeid 0x20<link>
        ether 46:fe:6e:97:ee:a6  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1  bytes 54 (54.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...

# Set the IP address of the USB network card to be in the same subnet as the device's usb0
neardi@Desktop:~$ sudo ifconfig enp0s20u2i1 192.168.1.100

# Set the default gateway: it should be the IP address of the device's usb0 because traffic will be forwarded through usb0
neardi@Desktop:~$ sudo route add default gw 192.168.1.101

After configuring the IPs of the device and the PC, they can ping each other, and the PC can also log in to the device using the ssh command.

Network Sharing to Allow PC Internet Access

On the device: first, enable IPv4 forwarding:

echo 1 > /proc/sys/net/ipv4/ip_forward

If you want to automatically enable forwarding after each reboot, modify the /etc/sysctl.conf file, setting net.ipv4.ip_forward to 1. After modifying the file parameters, execute sysctl -p to reload the /etc/sysctl.conf file, making IPv4 forwarding effective.

Add rules for traffic forwarding:

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 168.168.100.21

Now the PC host can access the internet. If the PC can ping the device’s usb0 and eth0 but cannot access the internet, modify the PC’s DNS by adding the following to /etc/resolv.conf:

nameserver 8.8.8.8
nameserver 8.8.4.4

Pay attention to the following during the configuration process:

  • Correspond the IP addresses on your device in the above steps, ensuring the USB virtual network card IP and the wired network IP are not in the same subnet;

  • The gateway of the PC’s virtual USB network card should be set to the IP address of the device’s virtual USB network card;

  • The virtual network card IP address, IP forwarding function, traffic forwarding rules, etc., on the device will revert after reboot. Users can search for information on how to set these configurations to apply automatically on boot.