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