Hostapd 不仅仅是读取用户的配置文件,接收用户的管理信息,然后将这些配置管理信息配置到底层。Hostapd还支持的密钥管理和认证关联。如果在认证关联过程中出现异常,我们希望Hostapd能发送响应的事件来通知上层应用。
下面是一个简单的hostapd事件处理框图。Hostpad通过ctrl_interface将event发送出来。Hostapd_cli通过wap_ctrl itf接收到event后,调用相应的事件处理脚本,同时将事件相关的参数传给脚本。脚本将相应的事件传给相应的上层应用,由上层应用做相关的处理。
hostapd_general_event.sh 专门处理hostapd 发出的事件,可以用hostapd_cli将其启动起来
/usr/sbin/hostapd_cli -a /etc/hostapd_general_event.sh -B \
-P /var/run/hostapd_cli.pid.$ifname.general -i $ifname
在hostapd_general_event.sh中,当收到hostapd发出来的event后,通过socket发送到相应的应用进行处理。以下是收到来自hostapd事件CTRL_EVENT_AUTH_REJECT
$CTRL_EVENT_AUTH_REJECT
json_add_string wlan_inf "$1"
json_add_string sta_mac "$3"
ubus call usersock do_wlan_ap_sta_pw_mismatch_event "$(json_dump)"
下面从代码角度讲解整个event处理流程。
1)在hostapd侧会创建ctrl_iface, 通过ctrl_iface将事件发送出来
hostapd_ctrl_iface_init
hapd->ctrl_sock根据/var/run/hostapd/[ifname] 创建unix socket
eloop_register_read_sock(s, hostapd_ctrl_iface_receive,…), 将ctrl_iface socket加入eloop监控, hostapd_ctrl_iface_receive 接收来自ctrl_iface socket消息并处理
wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb) 注册wpa_msg 回调func
当WPA3模式下,输错密码,hostapd会进入下面函数发送event出来
handle_auth_sae
wpa_msg(hapd->msg_ctx, MSG_INFO,
WPA_EVENT_AUTH_REJECT --> "CTRL-EVENT-AUTH-REJECT "
MACSTR, MAC2STR(sta->addr));
wpa_msg() The output may be directed to stdout, stderr, and/or syslog based on configuration. This function is like wpa_printf(), but it also sends the same message to all attached ctrl_iface monitors.
wpa_msg
_wpa_msg //wpa_debug.c
wpa_msg_cb --> hostapd_ctrl_iface_msg_cb
hostapd_ctrl_iface_send(hapd->ctrl_sock, &hapd->ctrl_dst,…)
hostapd通过wpa_msg可以向外发送消息事件。
2)在hostapd_cli侧,启动的时候会指定处理事件的脚本和端口
hostapd_cli main()
action_file = /etc/hostapd_general_event.sh
ctrl_ifname = wlan0, wlan1, wlan0-vap3 and so on
hostapd_cli_reconnect
ctrl_conn =hostapd_cli_open_connection //建立和ctrl_sock连接
ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir); // cfile = /var/run/hostapd/[ifname]|
wpa_ctrl_attach(ctrl_conn)
register_event_handler(ctrl_conn)
hostapd_cli_action(ctrl_conn) //如果action_file不为空
hostapd_cli_recv_pending
hostapd_cli_action_process
hostapd_cli_exec(action_file, ctrl_ifname, …) //运行脚本,并将相应的ifname和event等信息作为参数传给脚本
3)hostapd_general_event.sh
事件脚本收到事件后,针对不同的事件做不同的处理。通常采用ubus方式将事件发送到相应的处理模块。ubus可以直接调用相应模块的method, 实现很灵活。