openwrt - network¶
https://openwrt.org/docs/guide-user/network/wifi/start
https://openwrt.org/docs/guide-user/network/wifi/basic
https://openwrt.org/docs/guide-user/network/wifi/relay_configuration
[[toc]]
查看网络配置¶
重启网络使配置生效¶
root@Thingoo:~# /etc/init.d/network restart
root@Thingoo:~# /etc/init.d/network reload
root@Thingoo:~# wifi down && wifi up
开启 Wi-Fi¶
查看当前网络¶
root@Thingoo:/# ifconfig
br-lan Link encap:Ethernet HWaddr 0C:EF:AF:D2:A9:B9
inet addr:192.168.8.1 Bcast:192.168.8.255 Mask:255.255.255.0
inet6 addr: fe80::eef:afff:fed2:a9b9/64 Scope:Link
inet6 addr: fdff:c5b2:821c::1/60 Scope:Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:574264 errors:0 dropped:0 overruns:0 frame:0
TX packets:1217669 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:48102567 (45.8 MiB) TX bytes:1634912136 (1.5 GiB)
eth0 Link encap:Ethernet HWaddr 0C:EF:AF:D2:A9:B8
inet6 addr: fe80::eef:afff:fed2:a9b8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1225320 errors:0 dropped:0 overruns:0 frame:0
TX packets:579507 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1641989813 (1.5 GiB) TX bytes:59132909 (56.3 MiB)
Interrupt:5
eth0.1 Link encap:Ethernet HWaddr 0C:EF:AF:D2:A9:B8
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1 errors:0 dropped:1 overruns:0 frame:0
TX packets:3544 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:46 (46.0 B) TX bytes:244206 (238.4 KiB)
eth0.2 Link encap:Ethernet HWaddr 0C:EF:AF:D2:A9:B8
inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::eef:afff:fed2:a9b8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1176423 errors:0 dropped:10706 overruns:0 frame:0
TX packets:575928 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1617670919 (1.5 GiB) TX bytes:56565373 (53.9 MiB)
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:294 errors:0 dropped:0 overruns:0 frame:0
TX packets:294 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:41961 (40.9 KiB) TX bytes:41961 (40.9 KiB)
ra0 Link encap:Ethernet HWaddr 0C:EF:AF:D2:A9:B8
inet6 addr: fe80::eef:afff:fed2:a9b8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:641847 errors:830 dropped:0 overruns:0 frame:0
TX packets:1228723 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:70965355 (67.6 MiB) TX bytes:1630822247 (1.5 GiB)
Interrupt:6
网络设备名 | 说明 |
---|---|
br-lan | 虚拟设备,LAN 口桥接设备,包含通过 LAN 口和 WAN 口连入系统的设备统一桥接 |
eth0 | 真实设备,CPU 内部到交换机芯片之间只有一个接口 |
eth0.1 | 虚拟设备,由 VLAN 划分的有线的 LAN 口,VLAN 编号为 1 |
eth0.2 | 虚拟设备,由 VLAN 划分的有线的 LAN 口,VLAN编号为 2 |
lo | 虚拟设备,回环设备 |
ra0 | 真实设备,启动 Wi-Fi 后将会产生此无线设备 |
pppoe-wan | 虚拟设备,是 PPPoE 拨号上网成功后产生的 |
查看 br-lan 桥状态¶
root@Thingoo:/# brctl show
bridge name bridge id STP enabled interfaces
br-lan 7fff.0cefafd2a9b9 no eth0.1
ra0
查看 wifi 无线状态¶
root@Thingoo:~# iwinfo
mesh0 ESSID: "12345678"
Access Point: AC:23:3F:C0:3C:D0
Mode: Mesh Point Channel: 3 (2.422 GHz)
Tx-Power: 20 dBm Link Quality: unknown/70
Signal: unknown Noise: unknown
Bit Rate: unknown
Encryption: none
Type: nl80211 HW Mode(s): 802.11bgn
Hardware: 14C3:7628 14C3:0004 [MediaTek MT76x8]
TX power offset: none
Frequency offset: none
Supports VAPs: yes PHY name: phy0
wlan0 ESSID: "GW-AC233FC03CD0"
Access Point: AE:23:3F:C0:3C:D0
Mode: Master Channel: 3 (2.422 GHz)
Tx-Power: 20 dBm Link Quality: unknown/70
Signal: unknown Noise: unknown
Bit Rate: unknown
Encryption: none
Type: nl80211 HW Mode(s): 802.11bgn
Hardware: 14C3:7628 14C3:0004 [MediaTek MT76x8]
TX power offset: none
Frequency offset: none
Supports VAPs: yes PHY name: phy0
root@Thingoo:/# iw dev wlan0 station dump
Station fc:7c:02:7e:ab:af (on wlan0)
inactive time: 16180 ms
rx bytes: 30553431
rx packets: 177870
tx bytes: 20417
tx packets: 478
tx retries: 24
tx failed: 1
beacon loss: 0
beacon rx: 83770
rx drop misc: 30
signal: -40 [-40, -44] dBm
signal avg: -40 [-40, -43] dBm
beacon signal avg: -40 dBm
tx bitrate: 6.0 MBit/s
rx bitrate: 585.1 MBit/s VHT-MCS 6 80MHz short GI VHT-NSS 2
rx duration: 2322 us
last ack signal:0 dBm
expected throughput: 60.333Mbps
authorized: yes
authenticated: yes
associated: yes
preamble: long
WMM/WME: yes
MFP: no
TDLS peer: no
DTIM period: 2
beacon interval:100
short slot time:yes
connected time: 8766 seconds
查看当前 VLAN 划分¶
root@Thingoo:/# swconfig dev switch0 show
Global attributes:
enable_vlan: 1
alternate_vlan_disable: 0
bc_storm_protect: 0
led_frequency: 0
Port 0:
disable: 0
doubletag: 0
untag: 1
led: 5
lan: 1
recv_bad: 0
recv_good: 2205
tr_bad: 0
tr_good: 52155
pvid: 2
link: port:0 link:up speed:100baseT full-duplex
Port 1:
disable: 0
doubletag: 0
untag: 1
led: 5
lan: 1
recv_bad: 0
recv_good: 0
tr_bad: 0
tr_good: 0
pvid: 1
link: port:1 link:down
Port 2:
disable: 0
doubletag: 0
untag: 1
led: 5
lan: 1
recv_bad: 0
recv_good: 0
tr_bad: 0
tr_good: 0
pvid: 1
link: port:2 link:down
Port 3:
disable: 0
doubletag: 0
untag: 1
led: 5
lan: 1
recv_bad: 0
recv_good: 0
tr_bad: 0
tr_good: 0
pvid: 1
link: port:3 link:down
Port 4:
disable: 0
doubletag: 0
untag: 1
led: 5
lan: 1
recv_bad: 0
recv_good: 0
tr_bad: 0
tr_good: 0
pvid: 1
link: port:4 link:down
Port 5:
disable: 1
doubletag: 0
untag: 0
led: ???
lan: 1
recv_bad: 0
recv_good: 0
tr_bad: 0
tr_good: 0
pvid: 0
link: port:5 link:down
Port 6:
disable: 0
doubletag: 0
untag: 0
led: ???
lan: ???
recv_bad: ???
recv_good: ???
tr_bad: ???
tr_good: ???
pvid: 0
link: port:6 link:up speed:1000baseT full-duplex
VLAN 1:
ports: 1 2 3 4 6t
VLAN 2:
ports: 0 6t
::: tip
哪个网口是 LAN, 哪个是 WAN, 是由 VLAN 划分的。VLAN1 为 LAN 口,包含 1、2、3、4 接口;VLAN2 为 WAN 口,包含了 0 接口。
:::
配置 WAN 口外网¶
查看 WAN 口配置¶
root@Thingoo:/# uci show network.wan
network.wan=interface
network.wan.force_link='1'
network.wan.macaddr='0c:ef:af:d2:a9:b8'
network.wan.proto='dhcp'
network.wan.ifname='eth0.2'
选项 | 说明 | 可选值 |
---|---|---|
ifname | 设备接口名 | eth0.2 |
proto | 协议类型 | static:静态 IP 地址 dhcp:动态获取 IP 地址 pppoe:拨号上网 pptp:远程 VPN 服务器 3g:连接 3G/4G 无线电话网络 |
macaddr | WAN 口 MAC 地址,修改该地址即可实现 MAC 地址克隆 | 首次数据根据 factory 分区内参数自动生成 |
动态获取 IP 选项¶
选项 | 说明 | 可选值及说明 |
---|---|---|
proto | 协议类型 | dhcp |
ifname | 设备名称 | eth0.2 |
macaddr | MAC 地址 | 根据 factory 分区自动生成的值 |
mtu | 最大数据包大小,默认不用设置 | 数值 |
reqopts | 在向 DHCP 服务器发出请求时增加附加的 DHCP 信息 | 字符串 |
dns | 使用指定的 DNS 服务器地址替代获得的 DNS | 字符串 |
指定静态 IP 选项¶
选项 | 说明 | 可选值及说明 |
---|---|---|
proto | 协议类型 | static |
ifname | 设备名称 | eth0.2 |
macaddr | MAC 地址 | 根据 factory 分区自动生成的值 |
mtu | 最大数据包大小,默认不用设置 | 数值 |
ipaddr | WAN 口的 IP 地址 | 字符串 |
netmask | WAN 口的子网掩码 | 字符串 |
gateway | 默认网关 | 字符串 |
broadcast | 广播地址 | 字符串 |
dns | 使用指定的 DNS 服务器地址替代获得的 DNS | 字符串 |
PPPOE 拨号上网选项¶
选项 | 说明 | 可选值及说明 |
---|---|---|
proto | 协议类型 | pppoe |
ifname | 设备名称 | eth0.2 |
macaddr | MAC 地址 | 根据 factory 分区自动生成的值 |
mtu | 最大数据包大小,默认不用设置 | 数值 |
username | 拨号使用的帐号 | 字符串 |
password | 拨号使用的密码 | 字符串 |
ac | 使用指定的访问集中器进行连接 | 字符串 |
service | 连接的服务名称 | 字符串 |
connect | 连接时候执行的外部脚本 | 字符串 |
disconnect | 断开连接时执行的外部脚本 | 字符串 |
demand | 等待多久没有活动就断开 PPPOE 连接 | 数字,单位秒 |
dns | DNS 服务器地址 | 字符串 |
pppd_options | 用于 pppd 进程执行时候的附加参数 | 字符串 |
配置 LAN 口服务¶
::: tip
LAN 口下的设备可以通过 WAN 口接入网络,也可以直接访问设备上的各项功能(系统防火墙对 LAN 口默认不做任何拦截)。
:::
查看 LAN 口配置¶
root@Thingoo:~# uci show network.lan
network.lan=interface
network.lan.force_link='1'
network.lan.macaddr='0c:ef:af:d2:a9:b9'
network.lan.type='bridge'
network.lan.proto='static'
network.lan.ipaddr='192.168.8.1'
network.lan.netmask='255.255.255.0'
network.lan.ip6assign='60'
network.lan.ifname='eth0.1'
选项 | 说明 | 可选值及说明 |
---|---|---|
ifname | 设备名称 | eth0.1 |
proto | 协议类型 | static |
macaddr | MAC 地址 | 根据 factory 分区自动生成的值 |
type | 网络类型 | bridge,桥模式(这样才有交换机功能) |
ipaddr | LAN 口的 IP 地址,用于局域网内其它设备访问路由器 | 字符串 |
netmask | LAN 口的子网掩码 | 字符串 |
::: warning
修改过 LAN 口的配置后要重启网络以及 DHCP 服务。
:::
配置无线网络¶
查看无线网络配置¶
root@Thingoo:~# cat /etc/config/wireless
config wifi-device 'radio0'
option type 'ralink'
option variant 'mt7628'
option country 'CN'
option hwmode '11bgn'
option htmode 'HT40'
option channel 'auto'
option disabled '0'
config wifi-iface 'ap'
option device 'radio0'
option mode 'ap'
option network 'lan'
option ifname 'ra0'
option ssid 'Thingoo-A9B8'
option hidden '0'
option encryption 'psk2'
option key 'passworkd'
config wifi-iface 'sta'
option device 'radio0'
option disabled '1'
option mode 'sta'
option network 'wwan'
option ifname 'apcli0'
option ssid 'UplinkAp'
option key 'SecretKey'
wifi-device 选项参数¶
选项 | 说明 | 可选值及说明 |
---|---|---|
type | 设备类型 | ralink |
channel | 无线信道,不同的国家支持的信道不同 | auto 或 1~13 |
hwmode | 无线协议类型 | 11bgn: IEEE802.11b + IEEE802.11g + IEEE802.11n |
htmode | 无线频宽 | HT20、HT40 |
disable | 关闭无线设备 | 0:启用;1:禁用 |
country | 国家类型,跟支持的频道有关,中国为 CN,支持 1~13 | CN:中国 |
wifi-iface 选项参数¶
选项 | 说明 | 可选值及说明 |
---|---|---|
device | 关联的无线设备 | radio0 |
network | 关联网络设备类型 | lan:表示桥接到 LAN 网上 wwan:表示启用无线中继 |
mode | 无线工作模式 | ap:热点模式 sta:客户端模式 |
ssid | 无线的名称 | 字符串 |
hidden | 隐藏无线名称 | 0:显示名称 1:隐藏名称 |
encryption | 无线加密方式 | none:不加密 psk:WPA-PSK 模式 psk2:WPA-PSK2 模式 psk-mixed:WPA-PSK / WPA-PSK2 混合模式 |
key | 无线密钥 | 字符串,长度为 8~64 个 ASCII 字符 |
::: tip
修改过无线配置后需要使用命令 wifi
使之生效
:::
查看无线网络状态¶
root@Thingoo:~# iwinfo
ra0 ESSID: "Thingoo-A9B8"
Access Point: 0C:EF:AF:D2:A9:B8
Mode: Client Channel: 7 (2.442 GHz)
Tx-Power: 18 dBm Link Quality: 10/100
Signal: -256 dBm Noise: -82 dBm
Bit Rate: 150.0 MBit/s
Encryption: unknown
Type: wext HW Mode(s): 802.11bg
Hardware: unknown [Generic WEXT]
TX power offset: unknown
Frequency offset: unknown
Supports VAPs: no PHY name: ra0
搜索范围内的其它无线设备¶
root@Thingoo:~# iwinfo ra0 scan
Cell 01 - Address: 50:64:2B:4B:02:4E
ESSID: "Xiaomi_024D"
Mode: Master Channel: 11
Signal: -256 dBm Quality: 55/100
Encryption: WPA2 PSK (TKIP, AES-OCB)
Cell 02 - Address: 44:97:5A:EC:C5:14
ESSID: "FAST_C514"
Mode: Master Channel: 1
Signal: -256 dBm Quality: 47/100
Encryption: WPA2 PSK (AES-OCB)
配置例子¶
以下例子里面有mac地址,不要直接拷贝。
以太网模式¶
网线连接上级网络
# vi /etc/config/network
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'
config globals 'globals'
option ula_prefix 'fd77:3f61:abac::/48'
config interface 'lan'
option type 'bridge'
# 内网lan interface使用的是静态ip,下面已经指定了静态ip和掩码
option proto 'static'
option ipaddr '192.168.99.1'
option netmask '255.255.255.0'
option ip6assign '60'
config device 'lan_dev'
option name 'eth0'
option macaddr 'ac:23:3f:c0:3c:f0'
config switch
option name 'switch0'
option reset '1'
option enable_vlan '0'
config interface 'wan'
option ifname 'eth0'
# 外网wan interface使用的是dhcp,不用指定静态ip和掩码
option proto 'dhcp'
# vi /etc/config/wireless
config wifi-device 'radio0'
option type 'mac80211'
option channel 'auto'
option country 'CN' # 指定国家地区,用于遵循当地的无线射频的法规(https://openwrt.org/docs/guide-user/network/wifi/wifi_countrycode)
# 指定wifi的信道范围
option channels '1-13'
option hwmode '11g'
option path 'platform/10300000.wmac'
# htmode使用20MHz的频宽
option htmode 'HT20'
option disabled '0'
config wifi-iface 'ap_radio_2G_0'
option device 'radio0'
# network为lan,表用使用的是/etc/config/network中的lan interface的配置,也就是对于连接了这个热点的网络的客户端都为lan interface指定的网段
option network 'lan'
# mode为ap,表明网关自身发射ap热点
option mode 'ap'
option ssid 'GW-AC233FC03CF0'
option encryption 'none'
WiFi模式¶
wifi连接上级wifi热点
# vi /etc/config/network
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'
config globals 'globals'
option ula_prefix 'fd77:3f61:abac::/48'
config interface 'lan'
option type 'bridge'
option proto 'static'
option ipaddr '192.168.99.1'
option netmask '255.255.255.0'
option ip6assign '60'
config device 'lan_dev'
option name 'eth0'
option macaddr 'ac:23:3f:c0:3c:f0'
config switch
option name 'switch0'
option reset '1'
option enable_vlan '0'
config interface 'wan'
option proto 'dhcp'
# 当proto为dhcp时,下面ipaddr netmask gateway无效(当静态ip地址才有效),但dns有效
option ipaddr '192.168.0.2'
option netmask '255.255.255.0'
option gateway '192.168.0.1'
list dns '8.8.8.8'
list dns '114.114.114.114'
# vi /etc/config/wireless
config wifi-device 'radio0'
option type 'mac80211'
option channel 'auto'
option channels '1-13'
option hwmode '11g'
option path 'platform/10300000.wmac'
option htmode 'HT20'
option disabled '0'
config wifi-iface 'ap_radio_2G_0'
option device 'radio0'
option network 'lan'
option mode 'ap'
option ssid 'GW-AC233FC03CF0'
option encryption 'none'
config wifi-iface 'sta_radio_2G_0'
# network参数为wan,说明使用的网络interface是wan接口的(也就是/etc/config/network中的interface wan配置)
option network 'wan'
option device 'radio0'
# mode 为sta,表明作为station模式连接这个上级wifi热点
option mode 'sta'
option disabled '0'
option ssid '@PHICOMM_AE'
option cipher 'auto'
option encrypt 'psk2'
option encryption 'psk2'
option key 'beacon888'