OpenWrt无线AP+STA配置
一、概述
AP,即Access Point Mode,允许无线接入,家里的无线路由器,大部都是这个模式。
STA,即Station Mode,作为无线终端,连接AP用,手机wifi等,大部都是这个模式。
AP+STA配置,允许OpenWrt路由器,既能提供无线接入功能的AP,又能连接到其他AP,继续提供上网功能,即作为一个Repeater。
完成AP+STA配置,首先需要硬件支持,所以首先阅读芯片及驱动手册,确认支持AP+STA,确认支持WDS。本次以Qualcomm Atheros QCA9531为例。
二、配置修改
主要涉及dhcp、firewall、network、wireless的配置修改。
下面未做说明的其他配置,都可以采用默认。
1. /etc/config/dhcp
config dnsmasq
option domainneeded '1'
option boguspriv '1'
option filterwin2k '0'
option localise_queries '1'
option rebind_protection '0' <---设置为0
option rebind_localhost '1'
option local '/lan/'
option domain 'lan'
option expandhosts '1'
option nonegcache '0'
option authoritative '1'
option readethers '1'
option leasefile '/tmp/dhcp.leases'
option resolvfile '/tmp/resolv.conf.auto'
option localservice '1'
2. /etc/config/network
config interface 'lan'
option ifname 'eth0.1'
option force_link '1'
option type 'bridge'
option proto 'static'
option ipaddr '192.168.2.1' <---注意,这个LAN的IP,也是AP时的IP,作为STA模式时,连接的
网关也可能是192.168.0.X或192.168.1.X等常用的地址,要
保证AP模式的IP与STA连接的网关在不同网段。所以这里以
192.168.2.X为例。
option netmask '255.255.255.0'
option ip6assign '60'
config interface 'wwan' <---增加一个wirless wan的接口,作为STA,名字可以随意定义。
option proto 'dhcp' <---启动dhcp client,用于STA从对端AP获得IP。
3. /etc/config/wireless
config wifi-device radio0
option type mac80211
option channel 11
option hwmode 11g
option htmode 'HT20'
config wifi-iface <---增加这一段
option device radio0 <---设备名,一般是ifconfig显示出的无线网卡名,与下面AP的设备名相同。
option network wwan <---与/etc/config/network新定义的interface相同。
option mode sta <---STA模式
option encryption psk2 <---对端AP的加密模式
option key 123456789 <---对端AP的密码
option ssid test <---对端AP的SSID
config wifi-iface
option device radio0
option network lan
option mode ap
option ssid OpenWrt
option encryption none
4. /etc/config/firewall
config zone
option name wan
list network wan
list network wan6
list network wwan <---增加一个wwan接口,与/etc/config/network新定义的interface相同。
option input REJECT
option output ACCEPT
option forward REJECT
option masq 1
option mtu_fix 1
完成以上配置,重启。
三、问题
当STA模式未成功连接对端AP时,OpenWrt会将自己的AP模式也disable掉,所以OpenWrt的SSID在其他无线设备上搜索不到了,这是为什么呢?
因为无线网卡在作为STA模式时,如果未连接,将进行scan,从而影响了AP模式的运行。
那么有没有办法让STA未连接时,AP保持active呢?
1. 还原单AP配置
参考OpenWrt WiKi,当STA连接不成功,一段时间timeout后,恢复单AP的配置。
1)单AP和AP+STA配置备份
cp /etc/config/wireless.original /etc/config/wireless.ap-only
cp /etc/config/wireless /etc/config/wireless.ap+sta
2)安装iwinfo package
make menuconfig,在菜单中选中iwinfo,然后全编译(make V=99)或单编译(make package/iwinfo/compile V=99)
Utilities --->
<*> iwinfo.......................... Generalized Wireless Information utility
3)制作脚本fix_sta_ap.sh,并copy至/usr/local/bin/fix_sta_ap.sh
#!/bin/sh
#
# Fix loss of AP when STA (Client) mode fails by reverting to default
# AP only configuration. Default AP configuration is assumed to be in
# /etc/config/wireless.ap-only
#
TIMEOUT=30
SLEEP=3
sta_err=0
while [ $(iwinfo | grep -c "ESSID: unknown") -ge 1 ]; do
let sta_err=$sta_err+1
if [ $((sta_err * SLEEP)) -ge $TIMEOUT ]; then
cp /etc/config/wireless.ap-only /etc/config/wireless
wifi up
# uncomment the following lines to try AP+STA after reboot
# sleep 3
# cp /etc/config/wireless.ap+sta /etc/config/wireless
break
fi
sleep $SLEEP
done
4)更改权限
chmod +x /usr/local/bin/fix_sta_ap.sh
5)修改/etc/rc.local
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
/bin/sh /usr/local/bin/fix_sta_ap.sh > /dev/null &
exit 0
依照这个思路,还可以进行很多扩展,如制作脚本定时启动STA连接等。缺点也很明显,频繁的wifi up/down造成网络不稳。
2. 使用vap_ind参数
VAP的概念
A “Virtual Access Point” is a logical entity that exists within a physical Access Point (AP). When a single physical AP supports multiple “Virtual APs”, each Virtual AP appears to stations (STAs) to be an independent physical AP, even though only a single physical AP is present.
A VAP can be either a client node (managed node) or an infrastructure node (master node).
阅读Qualcomm提供的driver CLI说明文档, 并经过多次试验,找到了这个参数:
Parameter | Format | Description |
---|---|---|
vap_ind get_vap_ind | iwpriv athN vap_ind 1/0 | Enables/disables VAP WDS independance set |
执行iwpriv ath0 vap_ind 1
后,果然AP Mode的SSID不消失了。
扩展:
vap_ind这个参数是通过iwpriv工具配置的,无线配置有很多工具,比如iwpriv、iwconfig、wlanconfig等等,阅读驱动手册,可以得到芯片所支持的参数。
如果出现参数不支持,那是因为OpenWrt使用的wireless驱动不支持。
如果确信驱动可以支持,则可以尝试修改wifi解析config的相关脚本,比如OpenWrt默认package\kernel\mac80211\files\lib\wifi\mac80211.sh
可以使用config_get
获取config项。
如在适当位置增加:
#support independent repeater mode
config_get vap_ind "$vif" vap_ind
[ -n "$vap_ind" ] && iwpriv "$ifname" vap_ind "${vap_ind}"