树莓派zero,不像zeroW,具有板载wifi可以直接接入网络。当然也可以用usb无线网卡。
如果没有usb网卡,也可以用Android手机接入usb-host,进行usb网络共享,让树莓派zero接入网络!

RNDIS是指Remote NDIS,基于USB实现RNDIS实际上就是TCP/IP over USB,就是在USB设备上跑TCP/IP,让USB设备看上去像一块网卡。

基于buildroot

一、linux kernel 配置
buildroot 默认是开启RNDIS模块的,如果没有开启,则需要进行手动配置,make kinux-menuconfig
进入如下目录

> Device Drivers > Network device support > USB Network Adapters

重点三项设置

{*}   Multi-purpose USB Networking Framework
{M}     CDC Ethernet support (smart devices such as cable modems)
{M}     Host for RNDIS and ActiveSync devices

二、查看已经加载的模块lsmod

[root@buildroot ~]#lsmod
Module                  Size  Used by    Not tainted
ipv6                  446464 10 [permanent]

目前系统仅加载了ipv6模块,而没有rndis_host
手动加载:modprobe rndis_host

[root@buildroot ~]#modprobe rndis_host
[ 1322.908781] usbcore: registered new interface driver cdc_ether
[ 1322.928632] rndis_host 1-1:1.0 eth0: register 'rndis_host' at usb-20980000.usb-1, RNDIS device, 00:00:00:00:00:00
[ 1322.946885] usbcore: registered new interface driver rndis_host
[root@buildroot ~]#lsmod
Module                  Size  Used by    Not tainted
rndis_host             16384  0
cdc_ether              16384  1 rndis_host
ipv6                  446464 10 [permanent]

手动加载完成,可以看到rndis_host、cdc_ether

三、将Android手机,接入树莓派0 usb-host接口
同时手机端,需要打开USB 网络共享

[root@buildroot ~]#[ 1485.716551] Indeed it is in host mode hprt0 = 00021501
[ 1485.936457] usb 1-1: new high-speed USB device number 3 using dwc_otg
[ 1485.947337] Indeed it is in host mode hprt0 = 00001101
[ 1486.199099] usb 1-1: New USB device found, idVendor=04e8, idProduct=6860, bcdDevice= 4.00
[ 1486.215091] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1486.226310] usb 1-1: Product: SAMSUNG_Android
[ 1486.234578] usb 1-1: Manufacturer: SAMSUNG
[ 1486.242461] usb 1-1: SerialNumber: b6a47799
[ 1489.516650] usb 1-1: USB disconnect, device number 3
[ 1489.726539] Indeed it is in host mode hprt0 = 00021501
[ 1489.936459] usb 1-1: new high-speed USB device number 4 using dwc_otg
[ 1489.947237] Indeed it is in host mode hprt0 = 00001101
[ 1490.198290] usb 1-1: New USB device found, idVendor=04e8, idProduct=6863, bcdDevice= 4.00
[ 1490.214325] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1490.225674] usb 1-1: Product: SAMSUNG_Android
[ 1490.234194] usb 1-1: Manufacturer: SAMSUNG
[ 1490.242332] usb 1-1: SerialNumber: b6a47799
[ 1490.257713] rndis_host 1-1:1.0 eth0: register 'rndis_host' at usb-20980000.usb-1, RNDIS device, 00:00:00:00:00:00

[root@buildroot ~]#

系统log可以看到,rndis_host 1-1:1.0 eth0,已经加载!并且已经识别到手机类型:三星

不同的手机,识别出来可能不一样,也可能是eth1,或者usb0之类!

也可以通过 dmesg 命令查看!
如果没有看到rndis_host 1-1:1.0 eth0字样,说明rndis设备没有被正确识别!

四、查看网络配置ifconfig

[root@buildroot ~]#ifconfig
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

如果没有看到 eth0或usb0,再查看 ifconfig -a

[root@buildroot ~]#ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

ifconfig -a,可以看到 eth0 已经识别了,但MAC地址没有正确读出来

HWaddr 00:00:00:00:00:00

udhcpc -i eth0 ,提示ifconfig: SIOCSIFFLAGS: Cannot assign requested address

[root@buildroot /etc/network]#udhcpc -i eth0
udhcpc: started, v1.31.1
ifconfig: SIOCSIFFLAGS: Cannot assign requested address
ifconfig: SIOCSIFFLAGS: Cannot assign requested address
udhcpc: sending discover
udhcpc: sendto: Network is down
udhcpc: read error: Network is down, reopening socket
udhcpc: sending discover
udhcpc: sendto: Network is down
udhcpc: read error: Network is down, reopening socket
udhcpc: sending discover
udhcpc: sendto: Network is down
udhcpc: read error: Network is down, reopening socket
udhcpc: read error: Network is down, reopening socket

其实,这是三星手机的问题,需要手动设置mac地址。换华为手机,到这一步就可以正常上网!

五、设置MAC地址
两种方式设置MAC地址:
1、命令设置

ifconfig eth0 down
ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF
ifconfig eth0 up

2、修改文件/etc/network/interfaces。此方式,重启也可以生效

[root@buildroot /etc/network]#vi interfaces

增加eth0相关描述

# interface file auto-generated by buildroot

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
                hwaddress ether AA:BB:CC:DD:EE:FF
#               address 192.168.2.2
#               netmask 255.255.255.0
#               network 192.168.2.0
#               broadcast 192.168.2.255
#               gateway 192.168.2.1

重新初始化网络,/etc/init.d/

[root@buildroot /etc/init.d]#./S40network restart

执行完成后,即可看到eth0已经生效!

[root@buildroot /etc/network]#ifconfig eth0 down
[root@buildroot /etc/network]#ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF
[root@buildroot /etc/network]#ifconfig eth0 up
[root@buildroot /etc/network]#
[root@buildroot /etc/network]#
[root@buildroot /etc/network]#ifconfig
eth0      Link encap:Ethernet  HWaddr AA:BB:CC:DD:EE:FF
          inet6 addr: fe80::a8bb:ccff:fedd:eeff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:26 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:5894 (5.7 KiB)  TX bytes:780 (780.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

但此时还不能上网,因为还没有获取到正确的IP地址!执行udhcpc -i eth0

[root@buildroot /etc/network]#udhcpc -i eth0
udhcpc: started, v1.31.1
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending select for 192.168.42.132
udhcpc: lease of 192.168.42.132 obtained, lease time 3600
deleting routers
adding dns 192.168.42.129

获取到正确的IP地址后!ping www.baidu.com 测试!

[root@buildroot /etc/network]#ping www.baidu.com
PING www.baidu.com (180.101.49.11): 56 data bytes
64 bytes from 180.101.49.11: seq=0 ttl=51 time=14.582 ms
64 bytes from 180.101.49.11: seq=1 ttl=51 time=21.231 ms
64 bytes from 180.101.49.11: seq=2 ttl=51 time=20.914 ms
64 bytes from 180.101.49.11: seq=3 ttl=51 time=20.953 ms
^C
--- www.baidu.com ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 14.582/19.420/21.231 ms

此时网络已经接通!

六、设置开机自动加载rndis_host模块
以上设置比较繁琐,,并且每次开机都要重新设置!

/etc/init.d下新建文件S10modules

#!/bin/sh

# Load other user-defined modules

for file in /etc/init.d/*.modules ; do

[ -x $file ] && $file

done

# Load modules (for backward compatibility with VARs)

if [ -f /etc/init.d/rc.modules ]; then

/etc/init.d/rc.modules

fi

/etc/init.d下新建文件rndis_host.modules

#!/bin/sh

modprobe rndis_host.ko

设置权限755

[root@buildroot /etc/init.d]#chmod 755 S10modules
[root@buildroot /etc/init.d]#chmod 755 rndis_host.modules

重启reboot

[    2.929930] usb 1-1: new high-speed USB device number 2 using dwc_otg
[    2.941021] Indeed it is in host mode hprt0 = 00001101
[    3.131470] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
[    3.213589] usb 1-1: New USB device found, idVendor=04e8, idProduct=6863, bcdDevice= 4.00
[    3.229033] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[    3.240050] usb 1-1: Product: SAMSUNG_Android
[    3.248262] usb 1-1: Manufacturer: SAMSUNG
[    3.256080] usb 1-1: SerialNumber: b6a47799
Starting syslogd: OK
Starting klogd: OK
Running sysctl: OK
[    3.578345] usbcore: registered new interface driver cdc_ether
[    3.596246] rndis_host 1-1:1.0 eth0: register 'rndis_host' at usb-20980000.usb-1, RNDIS device, 00:00:00:00:00:00
[    3.615653] usbcore: registered new interface driver rndis_host
hello world
Initializing random number generator: OK
Saving random seed: [    3.742622] random: dd: uninitialized urandom read (512 bytes read)
OK
Starting network: [    4.116974] NET: Registered protocol family 10
[    4.128752] Segment Routing with IPv6
udhcpc: started, v1.31.1
[    4.257709] random: mktemp: uninitialized urandom read (6 bytes read)
udhcpc: sending discover
udhcpc: sending select for 192.168.42.132
udhcpc: lease of 192.168.42.132 obtained, lease time 3600
deleting routers
[    4.440324] random: mktemp: uninitialized urandom read (6 bytes read)
adding dns 192.168.42.129
OK

Welcome to Buildroot for eric
buildroot login: root
Password:
[root@buildroot ~]#
[root@buildroot ~]#
[root@buildroot ~]#ping www.baidu.com
PING www.baidu.com (180.101.49.12): 56 data bytes
64 bytes from 180.101.49.12: seq=0 ttl=51 time=19.564 ms
64 bytes from 180.101.49.12: seq=1 ttl=51 time=14.150 ms
64 bytes from 180.101.49.12: seq=2 ttl=51 time=19.795 ms
^C
--- www.baidu.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 14.150/17.836/19.795 ms
[root@buildroot ~]#

可以看到,开机后自动加载rndis_host 模块!
也可以,将rndis驱动,在内核编译的时候,设置成静态编译,就不需要动态加载。

还不支持热插拔,重新接入手机,需要再次初始化网络
[root@buildroot /etc/init.d]#./S40network restart

改进:
以上做法,只是手动配置加载rndis_host模块,buildroot是支持动态自动加载模块的,只是默认并未打开!打开后,就不需要手动创建S10modules了!
1、打开动态加载模块功能
make meunconfig
System configuration —>/dev management
选择
Dynamic using devtmpfs + mdev

/dev management (Dynamic using devtmpfs + mdev)  --->

2、重新编译packet,make busybox。builtroot output/target/etc/init.d目录自动生成S10mdev

eric@eric-PC:~/Documents/work/linux-c/buildroot-2020.02.1/output/target/etc/init.d$ ls
rcK  rcS  S01syslogd  S02klogd  S02sysctl  S10mdev  S20urandom  S40network  S50userstart

3、重新编译生成镜像

-------------------------------------------仅此记录-------------------------------------------