Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_wext.py
1 # Deprecated WEXT driver interface in wpa_supplicant
2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import logging
8 logger = logging.getLogger()
9 import os
10
11 import hostapd
12 import hwsim_utils
13 from wpasupplicant import WpaSupplicant
14 from utils import HwsimSkip, skip_with_fips
15 from test_rfkill import get_rfkill
16
17 def get_wext_interface():
18     if not os.path.exists("/proc/net/wireless"):
19         raise HwsimSkip("WEXT support not included in the kernel")
20
21     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
22     try:
23         wpas.interface_add("wlan5", driver="wext")
24     except Exception, e:
25         wpas.close_ctrl()
26         raise HwsimSkip("WEXT driver support not included in wpa_supplicant")
27     return wpas
28
29 def test_wext_open(dev, apdev):
30     """WEXT driver interface with open network"""
31     wpas = get_wext_interface()
32
33     params = { "ssid": "wext-open" }
34     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
35
36     wpas.connect("wext-open", key_mgmt="NONE")
37     hwsim_utils.test_connectivity(wpas, hapd)
38
39 def test_wext_wpa2_psk(dev, apdev):
40     """WEXT driver interface with WPA2-PSK"""
41     wpas = get_wext_interface()
42
43     params = hostapd.wpa2_params(ssid="wext-wpa2-psk", passphrase="12345678")
44     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
45
46     wpas.connect("wext-wpa2-psk", psk="12345678")
47     hwsim_utils.test_connectivity(wpas, hapd)
48     if "RSSI=" not in wpas.request("SIGNAL_POLL"):
49         raise Exception("Missing RSSI from SIGNAL_POLL")
50
51     wpas.dump_monitor()
52     hapd.request("DEAUTHENTICATE " + wpas.p2p_interface_addr())
53     wpas.wait_disconnected(timeout=15)
54
55 def test_wext_wpa_psk(dev, apdev):
56     """WEXT driver interface with WPA-PSK"""
57     skip_with_fips(dev[0])
58     wpas = get_wext_interface()
59
60     params = hostapd.wpa_params(ssid="wext-wpa-psk", passphrase="12345678")
61     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
62     testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
63     if not os.path.exists(testfile):
64         wpas.close_ctrl()
65         raise HwsimSkip("tkip_mic_test not supported in mac80211")
66
67     wpas.connect("wext-wpa-psk", psk="12345678")
68     hwsim_utils.test_connectivity(wpas, hapd)
69
70     with open(testfile, "w") as f:
71         f.write(wpas.p2p_interface_addr())
72     ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
73     if ev is not None:
74         raise Exception("Unexpected disconnection on first Michael MIC failure")
75
76     with open(testfile, "w") as f:
77         f.write("ff:ff:ff:ff:ff:ff")
78     ev = wpas.wait_disconnected(timeout=10,
79                                 error="No disconnection after two Michael MIC failures")
80     if "reason=14 locally_generated=1" not in ev:
81         raise Exception("Unexpected disconnection reason: " + ev)
82
83 def test_wext_pmksa_cache(dev, apdev):
84     """PMKSA caching with WEXT"""
85     wpas = get_wext_interface()
86
87     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
88     hostapd.add_ap(apdev[0]['ifname'], params)
89     bssid = apdev[0]['bssid']
90     wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
91                    eap="GPSK", identity="gpsk user",
92                    password="abcdefghijklmnop0123456789abcdef",
93                    scan_freq="2412")
94     pmksa = wpas.get_pmksa(bssid)
95     if pmksa is None:
96         raise Exception("No PMKSA cache entry created")
97     if pmksa['opportunistic'] != '0':
98         raise Exception("Unexpected opportunistic PMKSA cache entry")
99
100     hostapd.add_ap(apdev[1]['ifname'], params)
101     bssid2 = apdev[1]['bssid']
102
103     wpas.dump_monitor()
104     logger.info("Roam to AP2")
105     # It can take some time for the second AP to become ready to reply to Probe
106     # Request frames especially under heavy CPU load, so allow couple of rounds
107     # of scanning to avoid reporting errors incorrectly just because of scans
108     # not having seen the target AP.
109     for i in range(3):
110         wpas.scan()
111         if wpas.get_bss(bssid2) is not None:
112             break
113         logger.info("Scan again to find target AP")
114     wpas.request("ROAM " + bssid2)
115     ev = wpas.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
116     if ev is None:
117         raise Exception("EAP success timed out")
118     wpas.wait_connected(timeout=10, error="Roaming timed out")
119     pmksa2 = wpas.get_pmksa(bssid2)
120     if pmksa2 is None:
121         raise Exception("No PMKSA cache entry found")
122     if pmksa2['opportunistic'] != '0':
123         raise Exception("Unexpected opportunistic PMKSA cache entry")
124
125     wpas.dump_monitor()
126     logger.info("Roam back to AP1")
127     wpas.scan()
128     wpas.request("ROAM " + bssid)
129     ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED",
130                           "CTRL-EVENT-CONNECTED"], timeout=15)
131     if ev is None:
132         raise Exception("Roaming with the AP timed out")
133     if "CTRL-EVENT-EAP-STARTED" in ev:
134         raise Exception("Unexpected EAP exchange")
135     pmksa1b = wpas.get_pmksa(bssid)
136     if pmksa1b is None:
137         raise Exception("No PMKSA cache entry found")
138     if pmksa['pmkid'] != pmksa1b['pmkid']:
139         raise Exception("Unexpected PMKID change for AP1")
140
141     wpas.dump_monitor()
142     if "FAIL" in wpas.request("PMKSA_FLUSH"):
143         raise Exception("PMKSA_FLUSH failed")
144     if wpas.get_pmksa(bssid) is not None or wpas.get_pmksa(bssid2) is not None:
145         raise Exception("PMKSA_FLUSH did not remove PMKSA entries")
146     wpas.wait_disconnected(timeout=5)
147     wpas.wait_connected(timeout=15, error="Reconnection timed out")
148
149 def test_wext_wep_open_auth(dev, apdev):
150     """WEP Open System authentication"""
151     wpas = get_wext_interface()
152
153     hapd = hostapd.add_ap(apdev[0]['ifname'],
154                           { "ssid": "wep-open",
155                             "wep_key0": '"hello"' })
156     wpas.connect("wep-open", key_mgmt="NONE", wep_key0='"hello"',
157                  scan_freq="2412")
158     hwsim_utils.test_connectivity(wpas, hapd)
159     if "[WEP]" not in wpas.request("SCAN_RESULTS"):
160         raise Exception("WEP flag not indicated in scan results")
161
162 def test_wext_wep_shared_key_auth(dev, apdev):
163     """WEP Shared Key authentication"""
164     wpas = get_wext_interface()
165
166     hapd = hostapd.add_ap(apdev[0]['ifname'],
167                           { "ssid": "wep-shared-key",
168                             "wep_key0": '"hello12345678"',
169                             "auth_algs": "2" })
170     wpas.connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
171                  wep_key0='"hello12345678"', scan_freq="2412")
172     hwsim_utils.test_connectivity(wpas, hapd)
173     wpas.request("REMOVE_NETWORK all")
174     wpas.wait_disconnected(timeout=5)
175     wpas.connect("wep-shared-key", key_mgmt="NONE", auth_alg="OPEN SHARED",
176                  wep_key0='"hello12345678"', scan_freq="2412")
177
178 def test_wext_pmf(dev, apdev):
179     """WEXT driver interface with WPA2-PSK and PMF"""
180     wpas = get_wext_interface()
181
182     params = hostapd.wpa2_params(ssid="wext-wpa2-psk", passphrase="12345678")
183     params["wpa_key_mgmt"] = "WPA-PSK-SHA256";
184     params["ieee80211w"] = "2";
185     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
186
187     wpas.connect("wext-wpa2-psk", psk="12345678", ieee80211w="1",
188                  key_mgmt="WPA-PSK WPA-PSK-SHA256", proto="WPA2",
189                  scan_freq="2412")
190     hwsim_utils.test_connectivity(wpas, hapd)
191
192     addr = wpas.p2p_interface_addr()
193     hapd.request("DEAUTHENTICATE " + addr)
194     wpas.wait_disconnected(timeout=5)
195
196 def test_wext_scan_hidden(dev, apdev):
197     """WEXT with hidden SSID"""
198     wpas = get_wext_interface()
199
200     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan",
201                                                 "ignore_broadcast_ssid": "1" })
202     hapd2 = hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test-scan2",
203                                                  "ignore_broadcast_ssid": "1" })
204
205     id1 = wpas.connect("test-scan", key_mgmt="NONE", scan_ssid="1",
206                        only_add_network=True)
207
208     wpas.request("SCAN scan_id=%d" % id1)
209
210     ev = wpas.wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
211     if ev is None:
212         raise Exception("Scan did not complete")
213
214     if "test-scan" not in wpas.request("SCAN_RESULTS"):
215         raise Exception("Did not find hidden SSID in scan")
216
217     id = wpas.connect("test-scan2", key_mgmt="NONE", scan_ssid="1",
218                       only_add_network=True)
219     wpas.connect_network(id, timeout=30)
220     wpas.request("DISCONNECT")
221     hapd2.disable()
222     hapd.disable()
223     wpas.interface_remove("wlan5")
224     wpas.interface_add("wlan5")
225     wpas.flush_scan_cache(freq=2412)
226     wpas.flush_scan_cache()
227
228 def test_wext_rfkill(dev, apdev):
229     """WEXT and rfkill block/unblock"""
230     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
231     wpas.interface_add("wlan5")
232     rfk = get_rfkill(wpas)
233     wpas.interface_remove("wlan5")
234
235     wpas = get_wext_interface()
236
237     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
238     wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
239     try:
240         logger.info("rfkill block")
241         rfk.block()
242         wpas.wait_disconnected(timeout=10,
243                                error="Missing disconnection event on rfkill block")
244
245         logger.info("rfkill unblock")
246         rfk.unblock()
247         wpas.wait_connected(timeout=20,
248                             error="Missing connection event on rfkill unblock")
249         hwsim_utils.test_connectivity(wpas, hapd)
250     finally:
251         rfk.unblock()