OpenWrt 用 hostapd 作为 Radius 服务器配置 WPAx-EAP 认证

hostapd 代码中有一个集成的 Radius 服务器,占用资源比较少。对于想简单尝试一下 EAP 认证的个人用户来说已经完全够用,配置起来也要比 freeradius3 等软件方便。然而 OpenWrt 的相关软件包在编译的时候并没有开启相关的编译选项,所以需要手动更改该软件包的参数进行编译。

相关软件编译

我的 OpenWrt 是从源码编译的,所以直接配置好了各项工具。对于使用官方镜像或镜像生成器的用户,可以使用官方提供的 SDK 设置好编译环境。

OpenWrt 同时使用 hostapd 与 wpa_supplicant ,并对代码打了补丁使得这两个程序编译成一个 multicall binary ,即 wpad 。根据自己的需要,勾选 wpad-openssl 或 hostapd-openssl 其中一项。不要使用 wolfssl 的版本,有 bug 会导致 hostapd 无法正常处理数据。

编辑 package/network/services/hostapd/files/hostapd-full.config ,启用如下编译选项:

CONFIG_DRIVER_NONE=y
CONFIG_RADIUS_SERVER=y

编译软件包:

make package/hostapd/compile -j $($(nproc)+1) 

根据自己的情况可以选择直接用 opkg 覆盖该软件包,或整合入镜像文件中。

Radius 服务器配置

这里以 EAP-TLS 为例:

mkdir -p /etc/hostapd/certs
touch /etc/hostapd/hostapd.{conf,eap_user,radius_clients}

/etc/hostapd/hostapd.conf:

driver=none
logger_syslog=127
logger_syslog_level=2
logger_stdout=127
logger_stdout_level=2

eap_server=1
eap_user_file=/etc/hostapd/hostapd.eap_user
server_cert=/etc/hostapd/certs/eap.crt
private_key=/etc/hostapd/certs/eap.key
ca_cert=/etc/hostapd/certs/ca_chain.pem
check_crl=0
radius_server_clients=/etc/hostapd/hostapd.radius_clients
radius_server_auth_port=1812

/etc/hostapd/hostapd.eap_user:

* TLS

/etc/hostapd/hostapd.radius_clients:

127.0.0.1       your_radius_key
192.168.1.0/24  if_you_wanna_test_it

尝试启动 hostapd ,查看是否有报错:

/usr/sbin/hostapd -s /etc/hostapd/hostapd.conf

设置最好不要把 radius 服务器绑到某个 /etc/config/wireless 配置下,这样太过 hack ,我们新建一个系统服务:

/etc/init.d/hostapd-radius:

#!/bin/sh /etc/rc.common

START=90
USE_PROCD=1
NAME=hostapd-radius

start_service() {
    if [ -x "/usr/sbin/hostapd" ]; then
        procd_open_instance hostapd-radius
        procd_set_param command /usr/sbin/hostapd -s /etc/hostapd/hostapd.conf
        procd_set_param respawn 3600 1 0
        [ -x /sbin/ujail -a -e /etc/capabilities/wpad.json ] && {
                procd_add_jail hostapd-radius
                procd_set_param capabilities /etc/capabilities/wpad.json
                procd_set_param user network
                procd_set_param group network
                procd_set_param no_new_privs 1
        }
        procd_close_instance
    fi
}

启用并启动服务:

/etc/init.d/hostapd-radius enable
/etc/init.d/hostapd-radius start

配置网络使用 Radius 服务器

可以在 LuCI 里设置,也可以通过命令:

uci set wireless.@wifi-iface[0].encryption=wpa2
uci set wireless.@wifi-iface[0].auth_server='127.0.0.1'
uci set wireless.@wifi-iface[0].auth_secret='your_radius_key'
uci commit wireless
wifi reload

暂时不要用 WPA3 或 WPA2/3 Mixed 的 EAP ,因为客户端的支持并不好。 Mixed 下 Windows 正常连接为 WPA2-EAP , iOS 无法正常连接。 WPA3 下 Windows 无法正常识别加密类型, iOS 显示无法连接。这个问题比较难排查,就这样吧。

最后吐槽下 OpenWrt 文档很老旧,和现在的情况一点也不一样。