zhangguanzhang's Blog

[持续更新] - Openwrt USB 网络

字数统计: 1.2k阅读时长: 6 min
2021/09/03 Share

about

记录下 openwrt 下 usb 网络的折腾,后续折腾这块内容的话也在这个文章内更新

N1 上的 usb 网络共享折腾

固件依赖

暂时没完全区分 usb 网络共享和 usb-cdc 的关系,所以我编译的时候把很多 usb-net-xxx 都编译进去了

听其他大佬说编译的时候主要有下面的包:

  • 安卓: kmod-usb-net kmod-usb-net-rndis
  • 苹果: kmod-usb-net-ipheth usbmuxd

建议下面这些也安装上方便调试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CONFIG_PACKAGE_luci-proto-3g=y
CONFIG_PACKAGE_luci-proto-ncm=y
CONFIG_PACKAGE_luci-proto-qmi=y

CONFIG_BUSYBOX_DEFAULT_LSPCI=y
CONFIG_BUSYBOX_DEFAULT_LSUSB=y
CONFIG_BUSYBOX_DEFAULT_IPROUTE=y

CONFIG_PACKAGE_usb-modeswitch=y
CONFIG_PACKAGE_usbutils=y
CONFIG_PACKAGE_usbreset=y
CONFIG_PACKAGE_qmi-utils=y
CONFIG_PACKAGE_libqmi=y

CONFIG_PACKAGE_bind-dig=y
CONFIG_PACKAGE_tcpdump=y

CONFIG_PACKAGE_pciutils=y

这些好像是 usb 4g 网卡的包,一般是 E3372(联通4G) 和 E8372(全网通4G版本) ,

缺省应该都可以用hilink模式,把下面这些驱动装上即可:

1
2
3
4
5
hilink mode: hilink mode 的网卡名 是  ethX 也可以说 usb0,ncm模式的则是wwan0, lsusb 或者 usb-mode -l 查看设备识别否
kmod-usb-net-rndis kmod-usb-net kmod-usb2 usb-modeswitch

NCM mode:
gcom kmod-usb-net-huawei-cdc-ncm kmod-usb2 usb-modeswitch kmod-usb-serial-wwan
1
2
CONFIG_PACKAGE_uqmi=y
kmod-usb-acm kmod-usb-net kmod-usb-net-qmi-wwan kmod-usb-ohci kmod-usb-serial kmod-usb-serial-option

实战

N1 建议开下无线,虽然辣鸡,但是它只有一个网口,方便我们操作。我是我的 redmi k30s usb 网络共享插给 N1,另一个手机连 N1 无线后 ssh 上去敲命令的。

先备份下网络配置文件,防止后面搞崩了。

1
cp /etc/config/network /etc/config/network.bak

插上去后有个 usb0 网卡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
root@OpenWrt:~# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/sit 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd :: permaddr d636:b99c:d56d::
4: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq master br-lan state DOWN group default qlen 1000
link/ether 0e:02:db:93:b8:20 brd ff:ff:ff:ff:ff:ff
5: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0
6: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether f2:09:65:33:97:88 brd ff:ff:ff:ff:ff:ff
7: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 7e:ad:dd:05:72:66 brd ff:ff:ff:ff:ff:ff
8: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br-lan state UP group default qlen 1000
link/ether 0e:02:db:93:b8:1f brd ff:ff:ff:ff:ff:ff
inet6 fe80::c02:dbff:fe93:b81f/64 scope link
valid_lft forever preferred_lft forever
9: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 0e:02:db:93:b8:20 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.1/24 brd 192.168.1.255 scope global br-lan
valid_lft forever preferred_lft forever
inet6 fd4e:8614:5748::1/60 scope global noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::c02:dbff:fe93:b820/64 scope link
valid_lft forever preferred_lft forever
10: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:14:ab:d8:da brd ff:ff:ff:ff:ff:ff
inet 172.31.0.1/24 brd 172.31.0.255 scope global docker0
valid_lft forever preferred_lft forever
11: usb0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 82:e0:7e:db:2c:25 brd ff:ff:ff:ff:ff:ff

使用 udhcpc 发送 dhcp 请求

1
2
3
4
5
6
root@OpenWrt:~# udhcpc -i usb0
udhcpc: started, v1.33.1
udhcpc: sending discover
udhcpc: sendto: Network is down
udhcpc: read error: Network is down, reopening socket
udhcpc: sending discover

查看下 usb,确实有我的 手机,居然要手动 up 它

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@OpenWrt:~# lsusb
Bus 001 Device 004: ID 2717:ff80 Xiaomi M2007J3SC
Bus 002 Device 001: ID 1d6b:0003 Linux 5.13.13-flippy-63+ xhci-hcd xHCI Host Controller
Bus 001 Device 001: ID 1d6b:0002 Linux 5.13.13-flippy-63+ xhci-hcd xHCI Host Controller

root@OpenWrt:~# ip link set usb0 up
root@OpenWrt:~# ip a s usb0

13: usb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
link/ether 0a:01:0f:46:3a:e6 brd ff:ff:ff:ff:ff:ff
inet6 fe80::801:fff:fe46:3ae6/64 scope link
valid_lft forever preferred_lft forever
root@OpenWrt:~# udhcpc -i usb0
udhcpc: started, v1.33.1
udhcpc: sending discover
udhcpc: sending select for 192.168.42.128
udhcpc: lease of 192.168.42.128 obtained, lease time 3599
udhcpc: ip addr add 192.168.42.128/255.255.255.0 broadcast 192.168.42.255 dev usb0
udhcpc: setting default routers: 192.168.42.129

请求过程中也把默认路由设置为它了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@OpenWrt:~# ip r g 1
1.0.0.0 via 192.168.42.129 dev usb0 src 192.168.42.128 uid 0
cache
root@OpenWrt:~# ping baudu.com
^C
root@OpenWrt:~# ping 114.114.114.114
PING 114.114.114.114 (114.114.114.114): 56 data bytes
64 bytes from 114.114.114.114: seq=0 ttl=64 time=73.218 ms
64 bytes from 114.114.114.114: seq=1 ttl=70 time=45.460 ms
64 bytes from 114.114.114.114: seq=2 ttl=71 time=43.823 ms
64 bytes from 114.114.114.114: seq=3 ttl=81 time=40.963 ms
^C
--- 114.114.114.114 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 40.963/50.866/73.218 ms
root@OpenWrt:~#

去 web 上,网络接口,把 wan 的口子(如果没有就添加),dhcp客户端,接口设选 usb0。添加后设置下新添加接口的 防火墙设置 ,将其设置为 wan:

参考:

CATALOG
  1. 1. about
    1. 1.1. N1 上的 usb 网络共享折腾
      1. 1.1.1. 固件依赖
      2. 1.1.2. 实战
      3. 1.1.3. 参考: