]> git.ipfire.org Git - thirdparty/hostap.git/blame - tests/hwsim/test_ap_wps.py
WPS: Fix clear-from-timeout handling to avoid race condition
[thirdparty/hostap.git] / tests / hwsim / test_ap_wps.py
CommitLineData
302b7a1b
JM
1#!/usr/bin/python
2#
3# WPS tests
4# Copyright (c) 2013, Jouni Malinen <j@w1.fi>
5#
6# This software may be distributed under the terms of the BSD license.
7# See README for more details.
8
9import time
10import subprocess
11import logging
c9aa4308 12logger = logging.getLogger()
302b7a1b
JM
13
14import hwsim_utils
15import hostapd
16
ae3ad328 17def test_ap_wps_init(dev, apdev):
302b7a1b
JM
18 """Initial AP configuration with first WPS Enrollee"""
19 ssid = "test-wps"
ae3ad328 20 hostapd.add_ap(apdev[0]['ifname'],
302b7a1b 21 { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
ae3ad328 22 hapd = hostapd.Hostapd(apdev[0]['ifname'])
302b7a1b
JM
23 logger.info("WPS provisioning step")
24 hapd.request("WPS_PBC")
d671a420
JM
25 if "PBC Status: Active" not in hapd.request("WPS_GET_STATUS"):
26 raise Exception("PBC status not shown correctly")
6dacb8e9 27 dev[0].request("SET ignore_old_scan_res 1")
302b7a1b
JM
28 dev[0].dump_monitor()
29 dev[0].request("WPS_PBC")
853b49a0 30 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
302b7a1b
JM
31 if ev is None:
32 raise Exception("Association with the AP timed out")
33 status = dev[0].get_status()
ae3ad328 34 if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
302b7a1b
JM
35 raise Exception("Not fully connected")
36 if status['ssid'] != ssid:
37 raise Exception("Unexpected SSID")
38 if status['pairwise_cipher'] != 'CCMP':
39 raise Exception("Unexpected encryption configuration")
40 if status['key_mgmt'] != 'WPA2-PSK':
41 raise Exception("Unexpected key_mgmt")
42
d671a420
JM
43 status = hapd.request("WPS_GET_STATUS")
44 if "PBC Status: Disabled" not in status:
45 raise Exception("PBC status not shown correctly")
46 if "Last WPS result: Success" not in status:
47 raise Exception("Last WPS result not shown correctly")
48 if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
49 raise Exception("Peer address not shown correctly")
50
18030dc0
JM
51def test_ap_wps_init_2ap_pbc(dev, apdev):
52 """Initial two-radio AP configuration with first WPS PBC Enrollee"""
53 ssid = "test-wps"
54 params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
55 hostapd.add_ap(apdev[0]['ifname'], params)
56 hostapd.add_ap(apdev[1]['ifname'], params)
57 hapd = hostapd.Hostapd(apdev[0]['ifname'])
58 logger.info("WPS provisioning step")
59 hapd.request("WPS_PBC")
60 dev[0].request("SET ignore_old_scan_res 1")
61 dev[0].scan(freq="2412")
62 bss = dev[0].get_bss(apdev[0]['bssid'])
63 if "[WPS-PBC]" not in bss['flags']:
64 raise Exception("WPS-PBC flag missing from AP1")
65 bss = dev[0].get_bss(apdev[1]['bssid'])
66 if "[WPS-PBC]" not in bss['flags']:
67 raise Exception("WPS-PBC flag missing from AP2")
68 dev[0].dump_monitor()
69 dev[0].request("WPS_PBC")
70 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
71 if ev is None:
72 raise Exception("Association with the AP timed out")
73
74 dev[1].request("SET ignore_old_scan_res 1")
75 dev[1].scan(freq="2412")
76 bss = dev[1].get_bss(apdev[0]['bssid'])
77 if "[WPS-PBC]" in bss['flags']:
78 raise Exception("WPS-PBC flag not cleared from AP1")
79 bss = dev[1].get_bss(apdev[1]['bssid'])
80 if "[WPS-PBC]" in bss['flags']:
81 raise Exception("WPS-PBC flag bit ckeared from AP2")
82
83def test_ap_wps_init_2ap_pin(dev, apdev):
84 """Initial two-radio AP configuration with first WPS PIN Enrollee"""
85 ssid = "test-wps"
86 params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
87 hostapd.add_ap(apdev[0]['ifname'], params)
88 hostapd.add_ap(apdev[1]['ifname'], params)
89 hapd = hostapd.Hostapd(apdev[0]['ifname'])
90 logger.info("WPS provisioning step")
91 pin = dev[0].wps_read_pin()
92 hapd.request("WPS_PIN any " + pin)
93 dev[0].request("SET ignore_old_scan_res 1")
94 dev[0].scan(freq="2412")
95 bss = dev[0].get_bss(apdev[0]['bssid'])
96 if "[WPS-AUTH]" not in bss['flags']:
97 raise Exception("WPS-AUTH flag missing from AP1")
98 bss = dev[0].get_bss(apdev[1]['bssid'])
99 if "[WPS-AUTH]" not in bss['flags']:
100 raise Exception("WPS-AUTH flag missing from AP2")
101 dev[0].dump_monitor()
102 dev[0].request("WPS_PIN any " + pin)
103 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
104 if ev is None:
105 raise Exception("Association with the AP timed out")
106
107 dev[1].request("SET ignore_old_scan_res 1")
108 dev[1].scan(freq="2412")
109 bss = dev[1].get_bss(apdev[0]['bssid'])
110 if "[WPS-AUTH]" in bss['flags']:
111 raise Exception("WPS-AUTH flag not cleared from AP1")
112 bss = dev[1].get_bss(apdev[1]['bssid'])
113 if "[WPS-AUTH]" in bss['flags']:
114 raise Exception("WPS-AUTH flag bit ckeared from AP2")
115
35831e94
JM
116def test_ap_wps_init_through_wps_config(dev, apdev):
117 """Initial AP configuration using wps_config command"""
118 ssid = "test-wps-init-config"
119 hostapd.add_ap(apdev[0]['ifname'],
120 { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
121 hapd = hostapd.Hostapd(apdev[0]['ifname'])
122 if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
123 raise Exception("WPS_CONFIG command failed")
124 dev[0].connect(ssid, psk="12345678", scan_freq="2412", proto="WPA2",
125 pairwise="CCMP", group="CCMP")
126
ae3ad328 127def test_ap_wps_conf(dev, apdev):
302b7a1b
JM
128 """WPS PBC provisioning with configured AP"""
129 ssid = "test-wps-conf"
ae3ad328 130 hostapd.add_ap(apdev[0]['ifname'],
302b7a1b
JM
131 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
132 "wpa_passphrase": "12345678", "wpa": "2",
133 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
ae3ad328 134 hapd = hostapd.Hostapd(apdev[0]['ifname'])
302b7a1b
JM
135 logger.info("WPS provisioning step")
136 hapd.request("WPS_PBC")
137 dev[0].dump_monitor()
138 dev[0].request("WPS_PBC")
7e3f110b 139 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
302b7a1b
JM
140 if ev is None:
141 raise Exception("Association with the AP timed out")
142 status = dev[0].get_status()
ae3ad328 143 if status['wpa_state'] != 'COMPLETED':
302b7a1b 144 raise Exception("Not fully connected")
ae3ad328
JM
145 if status['bssid'] != apdev[0]['bssid']:
146 raise Exception("Unexpected BSSID")
302b7a1b
JM
147 if status['ssid'] != ssid:
148 raise Exception("Unexpected SSID")
149 if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
150 raise Exception("Unexpected encryption configuration")
151 if status['key_mgmt'] != 'WPA2-PSK':
152 raise Exception("Unexpected key_mgmt")
153
097cd9cd
JM
154 sta = hapd.get_sta(dev[0].p2p_interface_addr())
155 if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
156 raise Exception("Device name not available in STA command")
157
04e62788
JM
158def test_ap_wps_twice(dev, apdev):
159 """WPS provisioning with twice to change passphrase"""
160 ssid = "test-wps-twice"
161 params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
162 "wpa_passphrase": "12345678", "wpa": "2",
163 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
164 hostapd.add_ap(apdev[0]['ifname'], params)
165 hapd = hostapd.Hostapd(apdev[0]['ifname'])
166 logger.info("WPS provisioning step")
167 hapd.request("WPS_PBC")
168 dev[0].request("SET ignore_old_scan_res 1")
169 dev[0].dump_monitor()
170 dev[0].request("WPS_PBC")
171 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
172 if ev is None:
173 raise Exception("Association with the AP timed out")
174 dev[0].request("DISCONNECT")
175
176 logger.info("Restart AP with different passphrase and re-run WPS")
177 hapd_global = hostapd.HostapdGlobal()
178 hapd_global.remove(apdev[0]['ifname'])
179 params['wpa_passphrase'] = 'another passphrase'
180 hostapd.add_ap(apdev[0]['ifname'], params)
181 hapd = hostapd.Hostapd(apdev[0]['ifname'])
182 logger.info("WPS provisioning step")
183 hapd.request("WPS_PBC")
184 dev[0].dump_monitor()
185 dev[0].request("WPS_PBC")
186 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
187 if ev is None:
188 raise Exception("Association with the AP timed out")
189 networks = dev[0].list_networks()
190 if len(networks) > 1:
191 raise Exception("Unexpected duplicated network block present")
192
d658205a
JM
193def test_ap_wps_incorrect_pin(dev, apdev):
194 """WPS PIN provisioning with incorrect PIN"""
195 ssid = "test-wps-incorrect-pin"
196 hostapd.add_ap(apdev[0]['ifname'],
197 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
198 "wpa_passphrase": "12345678", "wpa": "2",
199 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
200 hapd = hostapd.Hostapd(apdev[0]['ifname'])
201
202 logger.info("WPS provisioning attempt 1")
203 hapd.request("WPS_PIN any 12345670")
204 dev[0].request("SET ignore_old_scan_res 1")
205 dev[0].dump_monitor()
206 dev[0].request("WPS_PIN any 55554444")
207 ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
208 if ev is None:
209 raise Exception("WPS operation timed out")
210 if "config_error=18" not in ev:
211 raise Exception("Incorrect config_error reported")
212 if "msg=8" not in ev:
213 raise Exception("PIN error detected on incorrect message")
214 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
215 if ev is None:
216 raise Exception("Timeout on disconnection event")
217 dev[0].request("WPS_CANCEL")
218 # if a scan was in progress, wait for it to complete before trying WPS again
219 ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
220
d671a420
JM
221 status = hapd.request("WPS_GET_STATUS")
222 if "Last WPS result: Failed" not in status:
223 raise Exception("WPS failure result not shown correctly")
224
d658205a
JM
225 logger.info("WPS provisioning attempt 2")
226 hapd.request("WPS_PIN any 12345670")
227 dev[0].dump_monitor()
228 dev[0].request("WPS_PIN any 12344444")
229 ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
230 if ev is None:
231 raise Exception("WPS operation timed out")
232 if "config_error=18" not in ev:
233 raise Exception("Incorrect config_error reported")
234 if "msg=10" not in ev:
235 raise Exception("PIN error detected on incorrect message")
236 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
237 if ev is None:
238 raise Exception("Timeout on disconnection event")
239
ae3ad328 240def test_ap_wps_conf_pin(dev, apdev):
302b7a1b
JM
241 """WPS PIN provisioning with configured AP"""
242 ssid = "test-wps-conf-pin"
ae3ad328 243 hostapd.add_ap(apdev[0]['ifname'],
302b7a1b
JM
244 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
245 "wpa_passphrase": "12345678", "wpa": "2",
246 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
ae3ad328 247 hapd = hostapd.Hostapd(apdev[0]['ifname'])
302b7a1b
JM
248 logger.info("WPS provisioning step")
249 pin = dev[0].wps_read_pin()
250 hapd.request("WPS_PIN any " + pin)
6dacb8e9 251 dev[0].request("SET ignore_old_scan_res 1")
302b7a1b
JM
252 dev[0].dump_monitor()
253 dev[0].request("WPS_PIN any " + pin)
7e3f110b 254 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
302b7a1b
JM
255 if ev is None:
256 raise Exception("Association with the AP timed out")
257 status = dev[0].get_status()
ae3ad328 258 if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
302b7a1b
JM
259 raise Exception("Not fully connected")
260 if status['ssid'] != ssid:
261 raise Exception("Unexpected SSID")
262 if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
263 raise Exception("Unexpected encryption configuration")
264 if status['key_mgmt'] != 'WPA2-PSK':
265 raise Exception("Unexpected key_mgmt")
266
362ba6de
JM
267 dev[1].request("SET ignore_old_scan_res 1")
268 dev[1].scan(freq="2412")
269 bss = dev[1].get_bss(apdev[0]['bssid'])
270 if "[WPS-AUTH]" in bss['flags']:
271 raise Exception("WPS-AUTH flag not cleared")
a60a6d6b
JM
272 logger.info("Try to connect from another station using the same PIN")
273 dev[1].request("WPS_PIN any " + pin)
274 ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
275 if ev is None:
276 raise Exception("Operation timed out")
277 if "WPS-M2D" not in ev:
278 raise Exception("Unexpected WPS operation started")
362ba6de 279
e9129860
JM
280def test_ap_wps_conf_pin_2sta(dev, apdev):
281 """Two stations trying to use WPS PIN at the same time"""
282 ssid = "test-wps-conf-pin2"
283 hostapd.add_ap(apdev[0]['ifname'],
284 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
285 "wpa_passphrase": "12345678", "wpa": "2",
286 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
287 hapd = hostapd.Hostapd(apdev[0]['ifname'])
288 logger.info("WPS provisioning step")
289 pin = "12345670"
290 pin2 = "55554444"
291 hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
292 hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
293 dev[0].request("SET ignore_old_scan_res 1")
294 dev[0].dump_monitor()
295 dev[1].request("SET ignore_old_scan_res 1")
296 dev[1].dump_monitor()
297 dev[0].request("WPS_PIN any " + pin)
298 dev[1].request("WPS_PIN any " + pin)
299 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
300 if ev is None:
301 raise Exception("Association with the AP timed out")
302 ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
303 if ev is None:
304 raise Exception("Association with the AP timed out")
305
ae3ad328 306def test_ap_wps_reg_connect(dev, apdev):
302b7a1b 307 """WPS registrar using AP PIN to connect"""
803edd1c 308 ssid = "test-wps-reg-ap-pin"
302b7a1b 309 appin = "12345670"
ae3ad328 310 hostapd.add_ap(apdev[0]['ifname'],
302b7a1b
JM
311 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
312 "wpa_passphrase": "12345678", "wpa": "2",
313 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
314 "ap_pin": appin})
315 logger.info("WPS provisioning step")
803edd1c 316 dev[0].request("SET ignore_old_scan_res 1")
302b7a1b 317 dev[0].dump_monitor()
6edaee9c 318 dev[0].wps_reg(apdev[0]['bssid'], appin)
302b7a1b 319 status = dev[0].get_status()
ae3ad328 320 if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
302b7a1b
JM
321 raise Exception("Not fully connected")
322 if status['ssid'] != ssid:
323 raise Exception("Unexpected SSID")
324 if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
325 raise Exception("Unexpected encryption configuration")
326 if status['key_mgmt'] != 'WPA2-PSK':
327 raise Exception("Unexpected key_mgmt")
328
e4357b19
JM
329def test_ap_wps_random_ap_pin(dev, apdev):
330 """WPS registrar using random AP PIN"""
331 ssid = "test-wps-reg-random-ap-pin"
332 ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
333 hostapd.add_ap(apdev[0]['ifname'],
334 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
335 "wpa_passphrase": "12345678", "wpa": "2",
336 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
337 "device_name": "Wireless AP", "manufacturer": "Company",
338 "model_name": "WAP", "model_number": "123",
339 "serial_number": "12345", "device_type": "6-0050F204-1",
340 "os_version": "01020300",
341 "config_methods": "label push_button",
342 "uuid": ap_uuid, "upnp_iface": "lo" })
343 hapd = hostapd.Hostapd(apdev[0]['ifname'])
344 appin = hapd.request("WPS_AP_PIN random")
345 if "FAIL" in appin:
346 raise Exception("Could not generate random AP PIN")
347 if appin not in hapd.request("WPS_AP_PIN get"):
348 raise Exception("Could not fetch current AP PIN")
349 logger.info("WPS provisioning step")
350 dev[0].request("SET ignore_old_scan_res 1")
351 dev[0].wps_reg(apdev[0]['bssid'], appin)
352
353 hapd.request("WPS_AP_PIN disable")
354 logger.info("WPS provisioning step with AP PIN disabled")
355 dev[1].request("SET ignore_old_scan_res 1")
356 dev[1].request("WPS_REG " + apdev[0]['bssid'] + " " + appin)
357 ev = dev[1].wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
358 if ev is None:
359 raise Exception("WPS operation timed out")
360 if "WPS-SUCCESS" in ev:
361 raise Exception("WPS operation succeeded unexpectedly")
362 if "config_error=15" not in ev:
363 raise Exception("WPS setup locked state was not reported correctly")
364
ae3ad328 365def test_ap_wps_reg_config(dev, apdev):
302b7a1b
JM
366 """WPS registrar configuring and AP using AP PIN"""
367 ssid = "test-wps-init-ap-pin"
368 appin = "12345670"
ae3ad328 369 hostapd.add_ap(apdev[0]['ifname'],
302b7a1b
JM
370 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
371 "ap_pin": appin})
372 logger.info("WPS configuration step")
803edd1c 373 dev[0].request("SET ignore_old_scan_res 1")
302b7a1b
JM
374 dev[0].dump_monitor()
375 new_ssid = "wps-new-ssid"
376 new_passphrase = "1234567890"
6edaee9c
JM
377 dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
378 new_passphrase)
302b7a1b 379 status = dev[0].get_status()
ae3ad328 380 if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
302b7a1b
JM
381 raise Exception("Not fully connected")
382 if status['ssid'] != new_ssid:
383 raise Exception("Unexpected SSID")
384 if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
385 raise Exception("Unexpected encryption configuration")
386 if status['key_mgmt'] != 'WPA2-PSK':
387 raise Exception("Unexpected key_mgmt")
388
eeefe187
JM
389def test_ap_wps_reg_config_tkip(dev, apdev):
390 """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
391 ssid = "test-wps-init-ap"
392 appin = "12345670"
393 hostapd.add_ap(apdev[0]['ifname'],
394 { "ssid": ssid, "eap_server": "1", "wps_state": "1",
395 "ap_pin": appin})
396 logger.info("WPS configuration step")
397 dev[0].request("SET ignore_old_scan_res 1")
398 dev[0].request("SET wps_version_number 0x10")
399 dev[0].dump_monitor()
400 new_ssid = "wps-new-ssid-with-tkip"
401 new_passphrase = "1234567890"
402 dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
403 new_passphrase)
404 logger.info("Re-connect to verify WPA2 mixed mode")
405 dev[0].request("DISCONNECT")
406 id = 0
407 dev[0].set_network(id, "pairwise", "CCMP")
408 dev[0].set_network(id, "proto", "RSN")
409 dev[0].connect_network(id)
410 status = dev[0].get_status()
411 if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
412 raise Exception("Not fully connected")
413 if status['ssid'] != new_ssid:
414 raise Exception("Unexpected SSID")
415 if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
416 raise Exception("Unexpected encryption configuration")
417 if status['key_mgmt'] != 'WPA2-PSK':
418 raise Exception("Unexpected key_mgmt")
419
6645ff50
JM
420def test_ap_wps_setup_locked(dev, apdev):
421 """WPS registrar locking up AP setup on AP PIN failures"""
422 ssid = "test-wps-incorrect-ap-pin"
423 appin = "12345670"
424 hostapd.add_ap(apdev[0]['ifname'],
425 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
426 "wpa_passphrase": "12345678", "wpa": "2",
427 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
428 "ap_pin": appin})
429 dev[0].request("SET ignore_old_scan_res 1")
430 new_ssid = "wps-new-ssid-test"
431 new_passphrase = "1234567890"
432
433 ap_setup_locked=False
434 for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
435 dev[0].dump_monitor()
436 logger.info("Try incorrect AP PIN - attempt " + pin)
437 dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
438 "CCMP", new_passphrase, no_wait=True)
439 ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
440 if ev is None:
441 raise Exception("Timeout on receiving WPS operation failure event")
442 if "CTRL-EVENT-CONNECTED" in ev:
443 raise Exception("Unexpected connection")
444 if "config_error=15" in ev:
445 logger.info("AP Setup Locked")
446 ap_setup_locked=True
447 elif "config_error=18" not in ev:
448 raise Exception("config_error=18 not reported")
449 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
450 if ev is None:
451 raise Exception("Timeout on disconnection event")
452 time.sleep(0.1)
453 if not ap_setup_locked:
454 raise Exception("AP setup was not locked")
455
d671a420
JM
456 hapd = hostapd.Hostapd(apdev[0]['ifname'])
457 status = hapd.request("WPS_GET_STATUS")
458 if "Last WPS result: Failed" not in status:
459 raise Exception("WPS failure result not shown correctly")
460 if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
461 raise Exception("Peer address not shown correctly")
462
6645ff50
JM
463 time.sleep(0.5)
464 dev[0].dump_monitor()
465 logger.info("WPS provisioning step")
466 pin = dev[0].wps_read_pin()
467 hapd = hostapd.Hostapd(apdev[0]['ifname'])
468 hapd.request("WPS_PIN any " + pin)
469 dev[0].request("WPS_PIN any " + pin)
470 ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
471 if ev is None:
472 raise Exception("WPS success was not reported")
473 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
474 if ev is None:
475 raise Exception("Association with the AP timed out")
476
ae3ad328 477def test_ap_wps_pbc_overlap_2ap(dev, apdev):
302b7a1b 478 """WPS PBC session overlap with two active APs"""
ae3ad328 479 hostapd.add_ap(apdev[0]['ifname'],
302b7a1b
JM
480 { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
481 "wpa_passphrase": "12345678", "wpa": "2",
482 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
483 "wps_independent": "1"})
ae3ad328 484 hostapd.add_ap(apdev[1]['ifname'],
302b7a1b
JM
485 { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
486 "wpa_passphrase": "123456789", "wpa": "2",
487 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
488 "wps_independent": "1"})
ae3ad328 489 hapd = hostapd.Hostapd(apdev[0]['ifname'])
302b7a1b 490 hapd.request("WPS_PBC")
ae3ad328 491 hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
302b7a1b
JM
492 hapd2.request("WPS_PBC")
493 logger.info("WPS provisioning step")
494 dev[0].dump_monitor()
495 dev[0].request("WPS_PBC")
496 ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
497 if ev is None:
498 raise Exception("PBC session overlap not detected")
499
ae3ad328 500def test_ap_wps_pbc_overlap_2sta(dev, apdev):
302b7a1b
JM
501 """WPS PBC session overlap with two active STAs"""
502 ssid = "test-wps-pbc-overlap"
ae3ad328 503 hostapd.add_ap(apdev[0]['ifname'],
302b7a1b
JM
504 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
505 "wpa_passphrase": "12345678", "wpa": "2",
506 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
ae3ad328 507 hapd = hostapd.Hostapd(apdev[0]['ifname'])
302b7a1b
JM
508 logger.info("WPS provisioning step")
509 hapd.request("WPS_PBC")
803edd1c 510 dev[0].request("SET ignore_old_scan_res 1")
803edd1c 511 dev[1].request("SET ignore_old_scan_res 1")
302b7a1b
JM
512 dev[0].dump_monitor()
513 dev[1].dump_monitor()
514 dev[0].request("WPS_PBC")
515 dev[1].request("WPS_PBC")
516 ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
517 if ev is None:
518 raise Exception("PBC session overlap not detected (dev0)")
519 if "config_error=12" not in ev:
520 raise Exception("PBC session overlap not correctly reported (dev0)")
521 ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
522 if ev is None:
523 raise Exception("PBC session overlap not detected (dev1)")
524 if "config_error=12" not in ev:
525 raise Exception("PBC session overlap not correctly reported (dev1)")
6edaee9c 526
71afe834
JM
527def test_ap_wps_cancel(dev, apdev):
528 """WPS AP cancelling enabled config method"""
529 ssid = "test-wps-ap-cancel"
530 hostapd.add_ap(apdev[0]['ifname'],
531 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
532 "wpa_passphrase": "12345678", "wpa": "2",
533 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
534 bssid = apdev[0]['bssid']
535 hapd = hostapd.Hostapd(apdev[0]['ifname'])
536
537 logger.info("Verify PBC enable/cancel")
538 hapd.request("WPS_PBC")
539 dev[0].request("SET ignore_old_scan_res 1")
540 dev[0].scan(freq="2412")
541 bss = dev[0].get_bss(apdev[0]['bssid'])
542 if "[WPS-PBC]" not in bss['flags']:
543 raise Exception("WPS-PBC flag missing")
544 if "FAIL" in hapd.request("WPS_CANCEL"):
545 raise Exception("WPS_CANCEL failed")
546 dev[0].scan(freq="2412")
547 bss = dev[0].get_bss(apdev[0]['bssid'])
548 if "[WPS-PBC]" in bss['flags']:
549 raise Exception("WPS-PBC flag not cleared")
550
551 logger.info("Verify PIN enable/cancel")
552 hapd.request("WPS_PIN any 12345670")
553 dev[0].scan(freq="2412")
554 bss = dev[0].get_bss(apdev[0]['bssid'])
555 if "[WPS-AUTH]" not in bss['flags']:
556 raise Exception("WPS-AUTH flag missing")
557 if "FAIL" in hapd.request("WPS_CANCEL"):
558 raise Exception("WPS_CANCEL failed")
559 dev[0].scan(freq="2412")
560 bss = dev[0].get_bss(apdev[0]['bssid'])
561 if "[WPS-AUTH]" in bss['flags']:
562 raise Exception("WPS-AUTH flag not cleared")
563
6edaee9c
JM
564def test_ap_wps_er_add_enrollee(dev, apdev):
565 """WPS ER configuring AP and adding a new enrollee using PIN"""
566 ssid = "wps-er-add-enrollee"
567 ap_pin = "12345670"
568 ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
569 hostapd.add_ap(apdev[0]['ifname'],
570 { "ssid": ssid, "eap_server": "1", "wps_state": "1",
571 "device_name": "Wireless AP", "manufacturer": "Company",
572 "model_name": "WAP", "model_number": "123",
573 "serial_number": "12345", "device_type": "6-0050F204-1",
574 "os_version": "01020300",
575 "config_methods": "label push_button",
576 "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
577 logger.info("WPS configuration step")
578 new_passphrase = "1234567890"
579 dev[0].dump_monitor()
580 dev[0].request("SET ignore_old_scan_res 1")
581 dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
582 new_passphrase)
583 status = dev[0].get_status()
584 if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
585 raise Exception("Not fully connected")
586 if status['ssid'] != ssid:
587 raise Exception("Unexpected SSID")
588 if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
589 raise Exception("Unexpected encryption configuration")
590 if status['key_mgmt'] != 'WPA2-PSK':
591 raise Exception("Unexpected key_mgmt")
592
593 logger.info("Start ER")
594 dev[0].request("WPS_ER_START ifname=lo")
595 ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
596 if ev is None:
597 raise Exception("AP discovery timed out")
598 if ap_uuid not in ev:
599 raise Exception("Expected AP UUID not found")
600
601 logger.info("Learn AP configuration through UPnP")
602 dev[0].dump_monitor()
603 dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
604 ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
605 if ev is None:
606 raise Exception("AP learn timed out")
607 if ap_uuid not in ev:
608 raise Exception("Expected AP UUID not in settings")
609 if "ssid=" + ssid not in ev:
610 raise Exception("Expected SSID not in settings")
611 if "key=" + new_passphrase not in ev:
612 raise Exception("Expected passphrase not in settings")
613
614 logger.info("Add Enrollee using ER")
615 pin = dev[1].wps_read_pin()
616 dev[0].dump_monitor()
617 dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
618 dev[1].request("SET ignore_old_scan_res 1")
619 dev[1].dump_monitor()
620 dev[1].request("WPS_PIN any " + pin)
846be889 621 ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
6edaee9c
JM
622 if ev is None:
623 raise Exception("Enrollee did not report success")
624 ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
625 if ev is None:
626 raise Exception("Association with the AP timed out")
627 ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
628 if ev is None:
629 raise Exception("WPS ER did not report success")
630 hwsim_utils.test_connectivity_sta(dev[0], dev[1])
631
38ae43de
JM
632 logger.info("Verify registrar selection behavior")
633 dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
634 dev[1].request("DISCONNECT")
635 dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
636 dev[1].scan(freq="2412")
637 bss = dev[1].get_bss(apdev[0]['bssid'])
638 if "[WPS-AUTH]" not in bss['flags']:
639 raise Exception("WPS-AUTH flag missing")
640
641 logger.info("Stop ER")
642 dev[0].dump_monitor()
643 dev[0].request("WPS_ER_STOP")
644 ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
645 if ev is None:
646 raise Exception("WPS ER unsubscription timed out")
647
648 dev[1].scan(freq="2412")
649 bss = dev[1].get_bss(apdev[0]['bssid'])
650 if "[WPS-AUTH]" in bss['flags']:
651 raise Exception("WPS-AUTH flag not removed")
652
6edaee9c
JM
653def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
654 """WPS ER connected to AP and adding a new enrollee using PBC"""
655 ssid = "wps-er-add-enrollee-pbc"
656 ap_pin = "12345670"
657 ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
658 hostapd.add_ap(apdev[0]['ifname'],
659 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
660 "wpa_passphrase": "12345678", "wpa": "2",
661 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
662 "device_name": "Wireless AP", "manufacturer": "Company",
663 "model_name": "WAP", "model_number": "123",
664 "serial_number": "12345", "device_type": "6-0050F204-1",
665 "os_version": "01020300",
666 "config_methods": "label push_button",
667 "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
668 logger.info("Learn AP configuration")
669 dev[0].dump_monitor()
670 dev[0].request("SET ignore_old_scan_res 1")
671 dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
672 status = dev[0].get_status()
673 if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
674 raise Exception("Not fully connected")
675
676 logger.info("Start ER")
677 dev[0].request("WPS_ER_START ifname=lo")
678 ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
679 if ev is None:
680 raise Exception("AP discovery timed out")
681 if ap_uuid not in ev:
682 raise Exception("Expected AP UUID not found")
683
684 logger.info("Use learned network configuration on ER")
685 dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
686
687 logger.info("Add Enrollee using ER and PBC")
688 dev[0].dump_monitor()
689 enrollee = dev[1].p2p_interface_addr()
690 dev[1].request("SET ignore_old_scan_res 1")
691 dev[1].dump_monitor()
692 dev[1].request("WPS_PBC")
693
694 ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
695 if ev is None:
696 raise Exception("Enrollee discovery timed out")
697 if enrollee not in ev:
698 raise Exception("Expected Enrollee not found")
699 dev[0].request("WPS_ER_PBC " + enrollee)
700
701 ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
702 if ev is None:
703 raise Exception("Enrollee did not report success")
704 ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
705 if ev is None:
706 raise Exception("Association with the AP timed out")
707 ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
708 if ev is None:
709 raise Exception("WPS ER did not report success")
710 hwsim_utils.test_connectivity_sta(dev[0], dev[1])
bff3ac5b 711
800bcf4e
JM
712 # verify BSSID selection of the AP instead of UUID
713 if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
714 raise Exception("Could not select AP based on BSSID")
715
be923570
JM
716def test_ap_wps_er_config_ap(dev, apdev):
717 """WPS ER configuring AP over UPnP"""
718 ssid = "wps-er-ap-config"
719 ap_pin = "12345670"
720 ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
721 hostapd.add_ap(apdev[0]['ifname'],
722 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
723 "wpa_passphrase": "12345678", "wpa": "2",
724 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
725 "device_name": "Wireless AP", "manufacturer": "Company",
726 "model_name": "WAP", "model_number": "123",
727 "serial_number": "12345", "device_type": "6-0050F204-1",
728 "os_version": "01020300",
729 "config_methods": "label push_button",
730 "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
731
732 logger.info("Connect ER to the AP")
733 dev[0].connect(ssid, psk="12345678", scan_freq="2412")
734
735 logger.info("WPS configuration step")
736 dev[0].request("WPS_ER_START ifname=lo")
737 ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
738 if ev is None:
739 raise Exception("AP discovery timed out")
740 if ap_uuid not in ev:
741 raise Exception("Expected AP UUID not found")
742 new_passphrase = "1234567890"
743 dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
744 ssid.encode("hex") + " WPA2PSK CCMP " +
745 new_passphrase.encode("hex"))
746 ev = dev[0].wait_event(["WPS-SUCCESS"])
747 if ev is None:
748 raise Exception("WPS ER configuration operation timed out")
749 dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
750 dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
751
bff3ac5b
JM
752def test_ap_wps_fragmentation(dev, apdev):
753 """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
754 ssid = "test-wps-fragmentation"
755 hostapd.add_ap(apdev[0]['ifname'],
756 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
757 "wpa_passphrase": "12345678", "wpa": "3",
758 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
759 "wpa_pairwise": "TKIP",
760 "fragment_size": "50" })
761 hapd = hostapd.Hostapd(apdev[0]['ifname'])
762 logger.info("WPS provisioning step")
763 hapd.request("WPS_PBC")
764 dev[0].request("SET ignore_old_scan_res 1")
765 dev[0].dump_monitor()
766 dev[0].request("SET wps_fragment_size 50")
767 dev[0].request("WPS_PBC")
768 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
769 if ev is None:
770 raise Exception("Association with the AP timed out")
771 status = dev[0].get_status()
772 if status['wpa_state'] != 'COMPLETED':
773 raise Exception("Not fully connected")
774 if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
775 raise Exception("Unexpected encryption configuration")
776 if status['key_mgmt'] != 'WPA2-PSK':
777 raise Exception("Unexpected key_mgmt")
10ea6848
JM
778
779def test_ap_wps_new_version_sta(dev, apdev):
780 """WPS compatibility with new version number on the station"""
781 ssid = "test-wps-ver"
782 hostapd.add_ap(apdev[0]['ifname'],
783 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
784 "wpa_passphrase": "12345678", "wpa": "2",
785 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
786 hapd = hostapd.Hostapd(apdev[0]['ifname'])
787 logger.info("WPS provisioning step")
788 hapd.request("WPS_PBC")
789 dev[0].request("SET ignore_old_scan_res 1")
790 dev[0].dump_monitor()
791 dev[0].request("SET wps_version_number 0x43")
792 dev[0].request("WPS_PBC")
793 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
794 if ev is None:
795 raise Exception("Association with the AP timed out")
796
797def test_ap_wps_new_version_ap(dev, apdev):
798 """WPS compatibility with new version number on the AP"""
799 ssid = "test-wps-ver"
800 hostapd.add_ap(apdev[0]['ifname'],
801 { "ssid": ssid, "eap_server": "1", "wps_state": "2",
802 "wpa_passphrase": "12345678", "wpa": "2",
803 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
804 hapd = hostapd.Hostapd(apdev[0]['ifname'])
805 logger.info("WPS provisioning step")
806 if "FAIL" in hapd.request("SET wps_version_number 0x43"):
807 raise Exception("Failed to enable test functionality")
808 hapd.request("WPS_PBC")
809 dev[0].request("SET ignore_old_scan_res 1")
810 dev[0].dump_monitor()
811 dev[0].request("WPS_PBC")
812 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
813 hapd.request("SET wps_version_number 0x20")
814 if ev is None:
815 raise Exception("Association with the AP timed out")
3bdf7d7f
JM
816
817def test_ap_wps_check_pin(dev, apdev):
818 """Verify PIN checking through control interface"""
819 hostapd.add_ap(apdev[0]['ifname'],
820 { "ssid": "wps", "eap_server": "1", "wps_state": "2",
821 "wpa_passphrase": "12345678", "wpa": "2",
822 "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
823 hapd = hostapd.Hostapd(apdev[0]['ifname'])
824 for t in [ ("12345670", "12345670"),
825 ("12345678", "FAIL-CHECKSUM"),
826 ("1234-5670", "12345670"),
827 ("1234 5670", "12345670"),
828 ("1-2.3:4 5670", "12345670") ]:
829 res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
830 res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
831 if res != res2:
832 raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
833 if res != t[1]:
834 raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))