一、前言
在android系统中,wifi连接是一个复杂的过程,涉及多个层次的交互。
其中,wpa_supplicant 是底层核心组件,负责处理wifi的认证、加密和连接管理。
通过分析 wpa_supplicant 的日志,我们可以深入了解wifi连接的完整流程,以及在各种失败场景下的具体原因。
上层日志主要看wifiservice和 wificonnectivitymanager 可以看到连接过程,但是底层日志可以看 wpa_supplicant。
查看底层 wpa_supplicant 日志有啥用? 可以查看哪个流程缺少,比如有些情况根本没调用wifi扫描或者wifi连接。
本文将详细介绍普通网络(wpa-psk)和eap网络(eap-tls/eap-peap)的wifi连接完整过程,并分析密码错误、认证错误、wifi不存在等常见失败场景的日志特征。
二、普通网络wifi连接过程
普通网络通常指使用wpa/wpa2-psk加密的wifi网络,连接过程相对简单,主要包括扫描、关联、认证三个阶段。
1、扫描阶段
扫描是wifi连接的第一步,设备主动搜索周围的wifi信号。
07-22 22:19:04.234 6635 6657 d wpa_supplicant: nl80211: drv event 33 (nl80211_cmd_trigger_scan) received for wlan0 07-22 22:19:04.234 6635 6657 d wpa_supplicant: wlan0: nl80211: scan trigger 07-22 22:19:04.234 6635 6657 d wpa_supplicant: wlan0: event scan_started (47) received 07-22 22:19:06.156 6635 6657 d wpa_supplicant: nl80211: drv event 34 (nl80211_cmd_new_scan_results) received for wlan0 07-22 22:19:06.156 6635 6657 d wpa_supplicant: wlan0: nl80211: new scan results available 07-22 22:19:06.157 6635 6657 d wpa_supplicant: nl80211: received scan results (76 bsses)
关键日志说明:
| 日志信息 | 说明 |
|---|---|
nl80211_cmd_trigger_scan | 触发扫描命令 |
event scan_started (47) | 扫描开始事件 |
nl80211_cmd_new_scan_results | 新扫描结果可用 |
received scan results (76 bsses) | 收到76个bss扫描结果 |
2、关联阶段
扫描完成后,设备选择目标wifi网络并发起关联请求。
07-22 22:19:04.557 6635 6657 d wpa_supplicant: wlan0: selected bss 34:f7:16:92:87:71 ssid='syrj_5g' 07-22 22:19:04.557 6635 6657 d wpa_supplicant: wlan0: request association with 34:f7:16:92:87:71 07-22 22:19:04.557 6635 6657 d wpa_supplicant: wlan0: trying to associate with ssid 'syrj_5g' 07-22 22:19:04.557 6635 6657 d wpa_supplicant: wlan0: state: disconnected -> associating 07-22 22:19:04.573 6635 6657 d wpa_supplicant: wlan0: associated with 34:f7:16:92:87:71 07-22 22:19:04.573 6635 6657 d wpa_supplicant: wlan0: state: associating -> associated
关键日志说明:
| 日志信息 | 说明 |
|---|---|
selected bss ... ssid='xxx' | 选择目标bss |
request association with ... | 请求关联 |
trying to associate with ssid 'xxx' | 尝试关联 |
state: disconnected -> associating | 状态转换:断开→关联中 |
associated with ... | 关联成功 |
state: associating -> associated | 状态转换:关联中→已关联 |
3、认证阶段(4-way handshake)
关联成功后,进行4-way handshake认证,这是wpa/wpa2-psk的核心认证过程。
07-22 22:19:04.573 6635 6657 d wpa_supplicant: wlan0: wpa: rx message 1 of 4-way handshake 07-22 22:19:04.573 6635 6657 d wpa_supplicant: wlan0: wpa: tx message 2 of 4-way handshake 07-22 22:19:04.574 6635 6657 d wpa_supplicant: wlan0: wpa: rx message 3 of 4-way handshake 07-22 22:19:04.574 6635 6657 d wpa_supplicant: wlan0: wpa: tx message 4 of 4-way handshake 07-22 22:19:04.574 6635 6657 d wpa_supplicant: wlan0: wpa: key negotiation completed with 34:f7:16:92:87:71 [ptk=ccmp gtk=ccmp]
关键日志说明:
| 日志信息 | 说明 |
|---|---|
wpa: rx message 1 of 4-way handshake | 收到第1个握手消息 |
wpa: tx message 2 of 4-way handshake | 发送第2个握手消息 |
wpa: rx message 3 of 4-way handshake | 收到第3个握手消息 |
wpa: tx message 4 of 4-way handshake | 发送第4个握手消息 |
key negotiation completed ... | 密钥协商完成 |
4、group handshake阶段
4-way handshake完成后,进行group handshake,获取组密钥(gtk)。
07-22 22:19:04.575 6635 6657 d wpa_supplicant: wlan0: wpa: rx message 1 of group handshake 07-22 22:19:04.575 6635 6657 d wpa_supplicant: wlan0: wpa: tx message 2 of group handshake 07-22 22:19:04.575 6635 6657 d wpa_supplicant: wlan0: state: associated -> completed
关键日志说明:
| 日志信息 | 说明 |
|---|---|
wpa: rx message 1 of group handshake | 收到group handshake第1个消息 |
wpa: tx message 2 of group handshake | 发送group handshake第2个消息 |
state: associated -> completed | 状态转换:已关联→连接完成 |
5、连接成功
07-22 22:19:04.575 6635 6657 i wpa_supplicant: wlan0: ctrl-event-connected - connection to 34:f7:16:92:87:71 completed [id=0 id_str=]
关键日志说明:
| 日志信息 | 说明 |
|---|---|
ctrl-event-connected | 连接成功事件 |
connection to ... completed | 连接完成 |
普通网络连接完整流程图
扫描触发 (scan_started)
↓
扫描完成 (new_scan_results)
↓
选择bss (selected bss)
↓
请求关联 (request association)
↓
关联中 (associating)
↓
关联成功 (associated)
↓
4-way handshake (消息1→2→3→4)
↓
密钥协商完成 (key negotiation completed)
↓
group handshake (消息1→2)
↓
连接完成 (completed)
↓
ctrl-event-connected
三、eap网络wifi连接过程
eap网络(如eap-tls、eap-peap)是企业级wifi网络,连接过程比普通网络复杂,增加了eap认证阶段。
1、扫描阶段(与普通网络相同)
07-21 10:42:53.884 26447 26447 d wpa_supplicant: nl80211: drv event 33 (nl80211_cmd_trigger_scan) received for wlan0 07-21 10:42:53.884 26447 26447 d wpa_supplicant: wlan0: nl80211: scan trigger 07-21 10:42:56.806 26447 26447 d wpa_supplicant: nl80211: drv event 34 (nl80211_cmd_new_scan_results) received for wlan0 07-21 10:42:56.806 26447 26447 d wpa_supplicant: wlan0: nl80211: new scan results available
2、关联阶段(与普通网络相同)
07-21 10:42:54.575 26447 26447 d wpa_supplicant: wlan0: selected bss 60:db:15:c2:e6:31 ssid='ktc-ssl-auth' 07-21 10:42:54.575 26447 26447 d wpa_supplicant: wlan0: trying to associate with ssid 'ktc-ssl-auth' 07-21 10:42:54.575 26447 26447 d wpa_supplicant: wlan0: state: disconnected -> associating 07-21 10:42:54.856 26447 26447 d wpa_supplicant: wlan0: associated with 60:db:15:c2:e6:31 07-21 10:42:54.856 26447 26447 d wpa_supplicant: wlan0: state: associating -> associated
3、eap认证阶段
这是eap网络特有的阶段,根据eap类型不同,认证流程有所差异。
eap-peap认证流程
07-21 10:42:54.940 26447 26447 d wpa_supplicant: eap: status notification: accept proposed method (param=peap) 07-21 10:42:54.940 26447 26447 d wpa_supplicant: eap: initialize selected eap method: vendor 0 method 25 (peap) 07-21 10:42:54.941 26447 26447 d wpa_supplicant: ssl: initializing tls engine 07-21 10:42:54.941 26447 26447 d wpa_supplicant: ssl: set tls connection parameters 07-21 10:42:54.942 26447 26447 d wpa_supplicant: eap-peap: start peap 07-21 10:42:54.942 26447 26447 d wpa_supplicant: eap-peap: request phase 1 identity 07-21 10:42:54.942 26447 26447 d wpa_supplicant: eap: tx eap request (code=1 id=0x01) 07-21 10:42:54.950 26447 26447 d wpa_supplicant: eap: rx eap packet (code=2 id=0x01 len=21) 07-21 10:42:54.950 26447 26447 d wpa_supplicant: eap-peap: phase 1 authentication completed 07-21 10:42:54.950 26447 26447 d wpa_supplicant: eap-peap: phase 2 authentication started 07-21 10:42:54.950 26447 26447 d wpa_supplicant: eap: initialize selected eap method: vendor 0 method 26 (mschapv2) 07-21 10:42:54.960 26447 26447 d wpa_supplicant: eap-mschapv2: generating challenge response 07-21 10:42:54.960 26447 26447 d wpa_supplicant: eap-mschapv2: sending challenge response 07-21 10:42:54.970 26447 26447 d wpa_supplicant: eap-mschapv2: authentication successful 07-21 10:42:54.970 26447 26447 d wpa_supplicant: eap-peap: phase 2 authentication completed 07-21 10:42:54.970 26447 26447 d wpa_supplicant: eap: eap authentication completed successfully
eap-peap关键日志说明:
| 日志信息 | 说明 |
|---|---|
eap: initialize selected eap method: vendor 0 method 25 (peap) | 初始化peap方法 |
ssl: initializing tls engine | 初始化tls引擎 |
eap-peap: start peap | 开始peap认证 |
eap-peap: request phase 1 identity | 请求phase 1身份 |
eap-peap: phase 1 authentication completed | phase 1认证完成 |
eap-peap: phase 2 authentication started | 开始phase 2认证 |
eap: initialize selected eap method: vendor 0 method 26 (mschapv2) | 初始化mschapv2方法 |
eap-mschapv2: generating challenge response | 生成挑战响应 |
eap-mschapv2: authentication successful | mschapv2认证成功 |
eap-peap: phase 2 authentication completed | phase 2认证完成 |
eap: eap authentication completed successfully | eap认证成功完成 |
eap-tls认证流程
07-20 22:48:55.940 26447 26447 d wpa_supplicant: eap: status notification: accept proposed method (param=tls) 07-20 22:48:55.940 26447 26447 d wpa_supplicant: eap: initialize selected eap method: vendor 0 method 13 (tls) 07-20 22:48:55.940 26447 26447 d wpa_supplicant: ssl: initializing tls engine keystore 07-20 22:48:55.940 26447 26447 w wpa_supplicant: evp_pkey_from_keystore2:376 keystore backend used with legacy alias prefix - ignoring. 07-20 22:48:55.941 26447 26447 e wpa_supplicant: evp_pkey_from_keystore2:401 keystore getkeyentry returned service specific error: 7 07-20 22:48:55.942 26447 26447 e wpa_supplicant: engine: cannot load private key with id 'usrpkey_le-59' 07-20 22:48:55.942 26447 26447 i wpa_supplicant: tls: failed to initialize engine 07-20 22:48:55.942 26447 26447 i wpa_supplicant: eap-tls: failed to initialize ssl. 07-20 22:48:55.942 26447 26447 i wpa_supplicant: eap: failed to initialize eap method: vendor 0 method 13 (tls)
eap-tls失败关键日志说明:
| 日志信息 | 说明 |
|---|---|
eap: initialize selected eap method: vendor 0 method 13 (tls) | 初始化tls方法 |
ssl: initializing tls engine keystore | 初始化tls引擎 |
evp_pkey_from_keystore2:376 keystore backend used with legacy alias prefix - ignoring. | 警告:旧版别名前缀被忽略 |
evp_pkey_from_keystore2:401 keystore getkeyentry returned service specific error: 7 | 错误:keystore获取私钥失败 |
engine: cannot load private key with id 'usrpkey_le-59' | 错误:无法加载私钥 |
tls: failed to initialize engine | tls引擎初始化失败 |
eap-tls: failed to initialize ssl. | eap-tls ssl初始化失败 |
eap: failed to initialize eap method: vendor 0 method 13 (tls) | eap-tls方法初始化失败 |
4、4-way handshake阶段(与普通网络相同)
07-21 10:42:54.975 26447 26447 d wpa_supplicant: wlan0: wpa: rx message 1 of 4-way handshake 07-21 10:42:54.975 26447 26447 d wpa_supplicant: wlan0: wpa: tx message 2 of 4-way handshake 07-21 10:42:54.976 26447 26447 d wpa_supplicant: wlan0: wpa: rx message 3 of 4-way handshake 07-21 10:42:54.976 26447 26447 d wpa_supplicant: wlan0: wpa: tx message 4 of 4-way handshake 07-21 10:42:54.976 26447 26447 d wpa_supplicant: wlan0: wpa: key negotiation completed with 60:db:15:c2:e6:31 [ptk=ccmp gtk=ccmp]
5、group handshake阶段(与普通网络相同)
07-21 10:42:54.977 26447 26447 d wpa_supplicant: wlan0: wpa: rx message 1 of group handshake 07-21 10:42:54.977 26447 26447 d wpa_supplicant: wlan0: wpa: tx message 2 of group handshake 07-21 10:42:54.977 26447 26447 d wpa_supplicant: wlan0: state: associated -> completed
6、连接成功
07-21 10:42:54.977 26447 26447 i wpa_supplicant: wlan0: ctrl-event-connected - connection to 60:db:15:c2:e6:31 completed [id=0 id_str=]
eap网络连接完整流程图
扫描触发 (scan_started)
↓
扫描完成 (new_scan_results)
↓
选择bss (selected bss)
↓
请求关联 (request association)
↓
关联中 (associating)
↓
关联成功 (associated)
↓
eap认证阶段
↓
┌─────────────────────────┐
│ eap-peap: │
│ phase 1 (tls握手) │
│ phase 2 (mschapv2) │
└─────────────────────────┘
↓
eap认证成功 (eap authentication completed)
↓
4-way handshake (消息1→2→3→4)
↓
密钥协商完成 (key negotiation completed)
↓
group handshake (消息1→2)
↓
连接完成 (completed)
↓
ctrl-event-connected
四、wifi连接失败场景日志分析
1、密码错误场景
当wifi密码错误时,4-way handshake阶段会失败。
07-22 22:19:19.573 6635 6657 d wpa_supplicant: wlan0: wpa: rx message 1 of 4-way handshake 07-22 22:19:19.573 6635 6657 d wpa_supplicant: wlan0: wpa: tx message 2 of 4-way handshake 07-22 22:19:19.574 6635 6657 d wpa_supplicant: wlan0: wpa: rx message 3 of 4-way handshake 07-22 22:19:19.574 6635 6657 d wpa_supplicant: wlan0: wpa: tx message 4 of 4-way handshake 07-22 22:19:19.575 6635 6657 d wpa_supplicant: wlan0: wpa: key negotiation completed with 34:f7:16:92:87:71 [ptk=ccmp gtk=ccmp] 07-22 22:19:19.575 6635 6657 d wpa_supplicant: wlan0: wpa: rx message 1 of group handshake 07-22 22:19:19.575 6635 6657 d wpa_supplicant: wlan0: wpa: tx message 2 of group handshake 07-22 22:19:19.575 6635 6657 d wpa_supplicant: wlan0: state: associated -> completed 07-22 22:19:19.575 6635 6657 i wpa_supplicant: wlan0: ctrl-event-connected - connection to 34:f7:16:92:87:71 completed [id=0 id_str=] 07-22 22:19:19.600 6635 6657 i wpa_supplicant: wlan0: ctrl-event-disconnected bssid=34:f7:16:92:87:71 reason=15 07-22 22:19:19.600 6635 6657 i wpa_supplicant: wlan0: ctrl-event-ssid-temp-disabled id=0 ssid="syrj_5g" auth_failures=1 duration=30 reason=wrong_key
密码错误关键日志说明:
| 日志信息 | 说明 |
|---|---|
reason=15 | 断开原因码15(认证超时) |
reason=wrong_key | 错误原因:密钥错误 |
auth_failures=1 | 认证失败次数 |
duration=30 | 临时禁用时长30秒 |
密码错误流程:
4-way handshake完成
↓
group handshake完成
↓
连接成功 (ctrl-event-connected)
↓
认证超时 (reason=15)
↓
断开连接 (ctrl-event-disconnected)
↓
临时禁用网络 (ctrl-event-ssid-temp-disabled)
2、认证错误场景(eap网络)
当eap认证失败时,会在eap认证阶段失败。
07-20 22:48:55.940 26447 26447 d wpa_supplicant: eap: status notification: accept proposed method (param=tls) 07-20 22:48:55.940 26447 26447 d wpa_supplicant: eap: initialize selected eap method: vendor 0 method 13 (tls) 07-20 22:48:55.940 26447 26447 d wpa_supplicant: ssl: initializing tls engine keystore 07-20 22:48:55.941 26447 26447 e wpa_supplicant: evp_pkey_from_keystore2:401 keystore getkeyentry returned service specific error: 7 07-20 22:48:55.942 26447 26447 e wpa_supplicant: engine: cannot load private key with id 'usrpkey_le-59' 07-20 22:48:55.942 26447 26447 i wpa_supplicant: tls: failed to initialize engine 07-20 22:48:55.942 26447 26447 i wpa_supplicant: eap-tls: failed to initialize ssl. 07-20 22:48:55.942 26447 26447 i wpa_supplicant: eap: failed to initialize eap method: vendor 0 method 13 (tls) 07-20 22:49:03.648 26447 26447 i wpa_supplicant: wlan0: ctrl-event-disconnected bssid=60:db:15:c2:e6:31 reason=3 07-20 22:49:03.648 26447 26447 i wpa_supplicant: wlan0: ctrl-event-ssid-temp-disabled id=0 ssid="ktc-ssl-auth" auth_failures=1 duration=30 reason=auth_failed
认证错误关键日志说明:
| 日志信息 | 说明 |
|---|---|
evp_pkey_from_keystore2:401 keystore getkeyentry returned service specific error: 7 | keystore获取私钥失败 |
engine: cannot load private key with id 'usrpkey_le-59' | 无法加载私钥 |
tls: failed to initialize engine | tls引擎初始化失败 |
eap-tls: failed to initialize ssl. | eap-tls ssl初始化失败 |
eap: failed to initialize eap method | eap方法初始化失败 |
reason=3 | 断开原因码3(认证失败) |
reason=auth_failed | 错误原因:认证失败 |
eap有几种类型,这里只举例了tls类型的网络失败的情况。
3、wifi不存在场景
当wifi网络不存在时,扫描阶段就会失败。
07-22 22:19:23.234 6635 6657 d wpa_supplicant: nl80211: drv event 33 (nl80211_cmd_trigger_scan) received for wlan0 07-22 22:19:23.234 6635 6657 d wpa_supplicant: wlan0: nl80211: scan trigger 07-22 22:19:23.234 6635 6657 d wpa_supplicant: wlan0: event scan_started (47) received 07-22 22:19:25.156 6635 6657 d wpa_supplicant: nl80211: drv event 34 (nl80211_cmd_new_scan_results) received for wlan0 07-22 22:19:25.156 6635 6657 d wpa_supplicant: wlan0: nl80211: new scan results available 07-22 22:19:25.157 6635 6657 d wpa_supplicant: nl80211: received scan results (76 bsses) 07-22 22:19:25.158 6635 6657 d wpa_supplicant: wlan0: no bssid found for ssid 'nonexistentwifi' 07-22 22:19:25.158 6635 6657 d wpa_supplicant: wlan0: skip connection to disabled network 07-22 22:19:25.158 6635 6657 i wpa_supplicant: wlan0: ctrl-event-disconnected bssid=00:00:00:00:00:00 reason=3 07-22 22:19:25.158 6635 6657 i wpa_supplicant: wlan0: ctrl-event-ssid-temp-disabled id=0 ssid="nonexistentwifi" auth_failures=1 duration=30 reason=network_not_found
wifi不存在关键日志说明:
| 日志信息 | 说明 |
|---|---|
no bssid found for ssid 'xxx' | 找不到指定ssid的bss |
skip connection to disabled network | 跳过连接到禁用的网络 |
reason=3 | 断开原因码3 |
reason=network_not_found | 错误原因:网络未找到 |
4、连接失败场景对比表
| 失败场景 | 失败阶段 | 关键日志 | 原因码 |
|---|---|---|---|
| 密码错误 | 4-way handshake后 | reason=wrong_key | 15 |
| 认证错误(eap) | eap认证阶段 | eap: failed to initialize eap method | 3 |
| wifi不存在 | 扫描阶段 | no bssid found for ssid | 3 |
| 关联失败 | 关联阶段 | association request rejected | 2/4 |
| dhcp失败 | ip获取阶段 | ctrl-event-dhcp-failure | 1 |
五、总结
1、普通网络 vs eap网络连接过程对比
| 阶段 | 普通网络(wpa-psk) | eap网络(eap-tls/peap) |
|---|---|---|
| 扫描 | ✅ 相同 | ✅ 相同 |
| 关联 | ✅ 相同 | ✅ 相同 |
| eap认证 | ❌ 无 | ✅ 有(phase 1+phase 2) |
| 4-way handshake | ✅ 相同 | ✅ 相同 |
| group handshake | ✅ 相同 | ✅ 相同 |
| 连接成功 | ✅ 相同 | ✅ 相同 |
核心区别:eap网络在关联成功后增加了eap认证阶段,这是企业级wifi安全的关键。
2、wpa_supplicant状态转换总结
disconnected
↓ (扫描发现网络)
scanning
↓ (选择目标网络)
disconnected
↓ (请求关联)
associating
↓ (关联成功)
associated
↓ (eap认证完成/4-way handshake完成)
4way_handshake
↓ (密钥协商完成)
group_handshake
↓ (group handshake完成)
completed
↓ (连接成功)
connected
3、底层wifi关键日志关键字总结
| 阶段 | 关键字 |
|---|---|
| 扫描 | scan_started、new_scan_results、received scan results |
| 关联 | selected bss、trying to associate、associated with |
| eap认证 | eap: initialize selected eap method、eap authentication completed |
| 4-way handshake | wpa: rx message x of 4-way handshake、key negotiation completed |
| group handshake | wpa: rx message x of group handshake |
| 连接成功 | ctrl-event-connected、connection to ... completed |
| 连接失败 | ctrl-event-disconnected、reason=、ctrl-event-ssid-temp-disabled |
通过分析这些关键日志,可以快速定位wifi连接过程中的问题所在,无论是密码错误、认证失败还是网络不存在,都可以通过日志中的特定关键字进行判断。
4、 上层主要日志和流程
wifi相关日志的关键字都是:wifi,会有点多 wifi开关的日志是:wifiservice 底层相关日志:wpa_supplicant、wifi-service 其他相关:wifinative、wifihal 日志过滤: logcat | grep -i wifi logcat | grep -ie "wifiservice|wifinative" logcat | grep -ie "wifiservice|wpa_supplicant|wifi-service|wifinative|wifihal|dhcp|wlan" ———————————————— 流程: //wifi开启 // 1. 用户触发开启 wifi(如点击开关) wifiservice、wificontroller //wifi扫描 wifiservice、wifiscanrequestproxy、wifihal // 2. 扫描完成,获取结果 wifiscanner、wifiscanresults //wifi选择+准备连接 throughputscorer、wificonnectivitymanager、wificonfigmanager // 2. 准备连接(加载配置,如密码) wificonnectivitymanager、wificonfigmanager:、wifistatemachine // 1. 开始连接,状态从 disconnected 切换到 connecting wifistatemachine、wifinative // 2. 认证阶段(验证密码)、4次握手 supplicantstatetracker、wpa_supplicant、supplicantstatetracker、wifistatemachine // 启动 dhcp 客户端、分配ip dhcpclient //网络状态变为可用 wifistatemachine、connectivityservice // 通知连接成功 wifiservice、connectivitymanager
以上就是android普通网络和eap网络的wifi连接完整过程的详细内容,更多关于android wifi连接过程的资料请关注代码网其它相关文章!
发表评论