2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 logger = logging.getLogger()
11 from wpasupplicant import WpaSupplicant
12 from p2p_utils import *
13 from test_nfc_p2p import set_ip_addr_info, check_ip_addr, grpform_events
14 from hwsim import HWSimRadio
15 from utils import HwsimSkip
19 def test_p2p_device_grpform(dev, apdev):
20 """P2P group formation with driver using cfg80211 P2P Device"""
21 with HWSimRadio(use_p2p_device=True) as (radio, iface):
22 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
23 wpas.interface_add(iface)
24 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
25 r_dev=wpas, r_intent=0)
26 check_grpform_results(i_res, r_res)
28 remove_group(dev[0], wpas)
30 if not r_res['ifname'].startswith('p2p-' + iface):
31 raise Exception("Unexpected group ifname: " + r_res['ifname'])
33 res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
34 lines = res.splitlines()
38 [name,value] = l.split('=', 1)
45 raise Exception("wdev_id not found")
47 def test_p2p_device_grpform2(dev, apdev):
48 """P2P group formation with driver using cfg80211 P2P Device (reverse)"""
49 with HWSimRadio(use_p2p_device=True) as (radio, iface):
50 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
51 wpas.interface_add(iface)
52 [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
53 r_dev=dev[0], r_intent=0)
54 check_grpform_results(i_res, r_res)
56 remove_group(wpas, dev[0])
58 if not i_res['ifname'].startswith('p2p-' + iface):
59 raise Exception("Unexpected group ifname: " + i_res['ifname'])
61 def test_p2p_device_grpform_no_group_iface(dev, apdev):
62 """P2P group formation with driver using cfg80211 P2P Device but no separate group interface"""
63 with HWSimRadio(use_p2p_device=True) as (radio, iface):
64 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
65 wpas.interface_add(iface)
66 wpas.global_request("SET p2p_no_group_iface 1")
67 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
68 r_dev=wpas, r_intent=0)
69 check_grpform_results(i_res, r_res)
71 remove_group(dev[0], wpas)
73 if r_res['ifname'] != iface:
74 raise Exception("Unexpected group ifname: " + r_res['ifname'])
76 def test_p2p_device_grpform_no_group_iface2(dev, apdev):
77 """P2P group formation with driver using cfg80211 P2P Device but no separate group interface (reverse)"""
78 with HWSimRadio(use_p2p_device=True) as (radio, iface):
79 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
80 wpas.interface_add(iface)
81 wpas.global_request("SET p2p_no_group_iface 1")
82 [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
83 r_dev=dev[0], r_intent=0)
84 check_grpform_results(i_res, r_res)
86 remove_group(dev[0], wpas)
88 if i_res['ifname'] != iface:
89 raise Exception("Unexpected group ifname: " + i_res['ifname'])
91 def test_p2p_device_group_remove(dev, apdev):
92 """P2P group removal via the P2P ctrl interface with driver using cfg80211 P2P Device"""
93 with HWSimRadio(use_p2p_device=True) as (radio, iface):
94 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
95 wpas.interface_add(iface)
96 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
97 r_dev=wpas, r_intent=0)
98 check_grpform_results(i_res, r_res)
99 # Issue the remove request on the interface which will be removed
100 p2p_iface_wpas = WpaSupplicant(ifname=r_res['ifname'])
101 res = p2p_iface_wpas.request("P2P_GROUP_REMOVE *")
103 raise Exception("Failed to remove P2P group")
104 ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
106 raise Exception("Group removal event not received")
107 if not wpas.global_ping():
108 raise Exception("Could not ping global ctrl_iface after group removal")
110 def test_p2p_device_concurrent_scan(dev, apdev):
111 """Concurrent P2P and station mode scans with driver using cfg80211 P2P Device"""
112 with HWSimRadio(use_p2p_device=True) as (radio, iface):
113 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
114 wpas.interface_add(iface)
118 ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
120 raise Exception("Station mode scan did not start")
122 def test_p2p_device_nfc_invite(dev, apdev):
123 """P2P NFC invitation with driver using cfg80211 P2P Device"""
124 run_p2p_device_nfc_invite(dev, apdev, 0)
126 def test_p2p_device_nfc_invite_no_group_iface(dev, apdev):
127 """P2P NFC invitation with driver using cfg80211 P2P Device (no separate group interface)"""
128 run_p2p_device_nfc_invite(dev, apdev, 1)
130 def run_p2p_device_nfc_invite(dev, apdev, no_group_iface):
131 with HWSimRadio(use_p2p_device=True) as (radio, iface):
132 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
133 wpas.interface_add(iface)
134 wpas.global_request("SET p2p_no_group_iface %d" % no_group_iface)
136 set_ip_addr_info(dev[0])
137 logger.info("Start autonomous GO")
138 dev[0].p2p_start_go()
140 logger.info("Write NFC Tag on the P2P Client")
141 res = wpas.global_request("P2P_LISTEN")
143 raise Exception("Failed to start Listen mode")
145 pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
147 raise Exception("Failed to generate password token")
148 res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
150 raise Exception("Failed to enable NFC Tag for P2P static handover")
151 sel = wpas.global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
153 raise Exception("Failed to generate NFC connection handover select")
156 logger.info("Read NFC Tag on the GO to trigger invitation")
157 res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
159 raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
161 ev = wpas.wait_global_event(grpform_events, timeout=20)
163 raise Exception("Joining the group timed out")
164 res = wpas.group_form_result(ev)
166 hwsim_utils.test_connectivity_p2p(dev[0], wpas)
170 def test_p2p_device_misuses(dev, apdev):
171 """cfg80211 P2P Device misuses"""
172 hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
173 with HWSimRadio(use_p2p_device=True) as (radio, iface):
174 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
175 wpas.interface_add(iface)
177 # Add a normal network profile to the P2P Device management only
178 # interface to verify that it does not get used.
179 id = int(wpas.global_request('IFNAME=p2p-dev-%s ADD_NETWORK' % iface).strip())
180 wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d ssid "open"' % (iface, id))
181 wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d key_mgmt NONE' % (iface, id))
182 wpas.global_request('IFNAME=p2p-dev-%s ENABLE_NETWORK %d' % (iface, id))
184 # Scan requests get ignored on p2p-dev
185 wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
187 dev[0].p2p_start_go(freq=2412)
188 addr = dev[0].p2p_interface_addr()
189 wpas.scan_for_bss(addr, freq=2412)
190 wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
191 hwsim_utils.test_connectivity(wpas, hapd)
193 pin = wpas.wps_read_pin()
194 dev[0].p2p_go_authorize_client(pin)
195 res = wpas.p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
196 social=True, freq=2412)
197 hwsim_utils.test_connectivity_p2p(dev[0], wpas)
199 # Optimize scan-after-disconnect
200 wpas.group_request("SET_NETWORK 0 scan_freq 2412")
202 dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
203 ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
205 raise Exception("Did not see disconnect event on P2P group interface")
206 dev[0].remove_group()
208 ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
210 raise Exception("Scan not started")
211 ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
213 raise Exception("Scan not completed")
215 hwsim_utils.test_connectivity(wpas, hapd)
217 ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=0.1)
219 raise Exception("Unexpected disconnection event received from hostapd")
220 ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
222 raise Exception("Unexpected disconnection event received from wpa_supplicant")
224 wpas.request("DISCONNECT")
225 wpas.wait_disconnected()
227 def test_p2p_device_incorrect_command_interface(dev, apdev):
228 """cfg80211 P2P Device and P2P_* command on incorrect interface"""
229 with HWSimRadio(use_p2p_device=True) as (radio, iface):
230 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
231 wpas.interface_add(iface)
234 wpas.request('P2P_FIND type=social')
235 ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
237 raise Exception("Peer not found")
238 ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
240 raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
243 pin = wpas.wps_read_pin()
244 dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
246 wpas.request('P2P_STOP_FIND')
248 if "OK" not in wpas.request('P2P_CONNECT ' + dev[0].p2p_dev_addr() + ' ' + pin + ' display go_intent=1'):
249 raise Exception("P2P_CONNECT failed")
251 ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
253 raise Exception("Group formation timed out")
254 wpas.group_form_result(ev)
257 ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
259 raise Exception("Group formation timed out(2)")
260 dev[0].group_form_result(ev)
262 dev[0].remove_group()
263 wpas.wait_go_ending_session()
266 def test_p2p_device_incorrect_command_interface2(dev, apdev):
267 """cfg80211 P2P Device and P2P_GROUP_ADD command on incorrect interface"""
268 with HWSimRadio(use_p2p_device=True) as (radio, iface):
269 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
270 wpas.interface_add(iface)
272 if "OK" not in wpas.request('P2P_GROUP_ADD'):
273 raise Exception("P2P_GROUP_ADD failed")
274 ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
276 raise Exception("Group formation timed out")
277 res = wpas.group_form_result(ev)
279 logger.info("Group results: " + str(res))
281 if not res['ifname'].startswith('p2p-' + iface + '-'):
282 raise Exception("Unexpected group ifname: " + res['ifname'])
285 def test_p2p_device_grpform_timeout_client(dev, apdev):
286 """P2P group formation timeout on client with cfg80211 P2P Device"""
287 with HWSimRadio(use_p2p_device=True) as (radio, iface):
288 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
289 wpas.interface_add(iface)
290 addr0 = dev[0].p2p_dev_addr()
291 addr5 = wpas.p2p_dev_addr()
293 dev[0].discover_peer(addr5)
295 wpas.discover_peer(addr0)
296 wpas.p2p_ext_listen(100, 150)
297 dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=15 auth")
298 wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=0")
299 ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
301 raise Exception("GO Negotiation did not succeed")
302 ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
304 raise Exception("WPS did not succeed (GO)")
305 if "OK" not in dev[0].global_request("P2P_CANCEL"):
306 wpas.global_request("P2P_CANCEL")
308 raise HwsimSkip("Did not manage to cancel group formation")
309 dev[0].dump_monitor()
310 ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
312 raise Exception("WPS did not succeed (Client)")
313 dev[0].dump_monitor()
314 ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
316 raise Exception("Group formation timeout not seen on client")
317 ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
319 raise Exception("Group removal not seen on client")
320 wpas.p2p_cancel_ext_listen()
322 ifaces = wpas.global_request("INTERFACES")
323 logger.info("Remaining interfaces: " + ifaces)
325 if "p2p-" + iface + "-" in ifaces:
326 raise Exception("Group interface still present after failure")
328 def test_p2p_device_grpform_timeout_go(dev, apdev):
329 """P2P group formation timeout on GO with cfg80211 P2P Device"""
330 with HWSimRadio(use_p2p_device=True) as (radio, iface):
331 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
332 wpas.interface_add(iface)
333 addr0 = dev[0].p2p_dev_addr()
334 addr5 = wpas.p2p_dev_addr()
336 dev[0].discover_peer(addr5)
338 wpas.discover_peer(addr0)
339 wpas.p2p_ext_listen(100, 150)
340 dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=0 auth")
341 wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=15")
342 ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
344 raise Exception("GO Negotiation did not succeed")
345 ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
347 raise Exception("WPS did not succeed (Client)")
348 if "OK" not in dev[0].global_request("P2P_CANCEL"):
349 if "OK" not in dev[0].global_request("P2P_GROUP_REMOVE *"):
350 wpas.global_request("P2P_CANCEL")
352 raise HwsimSkip("Did not manage to cancel group formation")
353 dev[0].dump_monitor()
354 ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
356 raise Exception("WPS did not succeed (GO)")
357 dev[0].dump_monitor()
358 ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
360 raise Exception("Group formation timeout not seen on GO")
361 ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
363 raise Exception("Group removal not seen on GO")
364 wpas.p2p_cancel_ext_listen()
366 ifaces = wpas.global_request("INTERFACES")
367 logger.info("Remaining interfaces: " + ifaces)
369 if "p2p-" + iface + "-" in ifaces:
370 raise Exception("Group interface still present after failure")
372 def test_p2p_device_autogo(dev, apdev):
373 """P2P autogo using cfg80211 P2P Device"""
374 with HWSimRadio(use_p2p_device=True) as (radio, iface):
375 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
376 wpas.interface_add(iface)
378 res = wpas.p2p_start_go()
379 if not res['ifname'].startswith('p2p-' + iface):
380 raise Exception("Unexpected group ifname: " + res['ifname'])
381 bssid = wpas.get_group_status_field('bssid')
383 dev[0].scan_for_bss(bssid, res['freq'])
384 connect_cli(wpas, dev[0], freq=res['freq'])
385 terminate_group(wpas, dev[0])
387 def test_p2p_device_autogo_no_group_iface(dev, apdev):
388 """P2P autogo using cfg80211 P2P Device (no separate group interface)"""
389 with HWSimRadio(use_p2p_device=True) as (radio, iface):
390 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
391 wpas.interface_add(iface)
392 wpas.global_request("SET p2p_no_group_iface 1")
394 res = wpas.p2p_start_go()
395 if res['ifname'] != iface:
396 raise Exception("Unexpected group ifname: " + res['ifname'])
397 bssid = wpas.get_group_status_field('bssid')
399 dev[0].scan_for_bss(bssid, res['freq'])
400 connect_cli(wpas, dev[0], freq=res['freq'])
401 terminate_group(wpas, dev[0])
403 def test_p2p_device_join(dev, apdev):
404 """P2P join-group using cfg80211 P2P Device"""
405 with HWSimRadio(use_p2p_device=True) as (radio, iface):
406 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
407 wpas.interface_add(iface)
409 res = dev[0].p2p_start_go()
410 bssid = dev[0].get_group_status_field('bssid')
412 wpas.scan_for_bss(bssid, res['freq'])
413 res2 = connect_cli(dev[0], wpas, freq=res['freq'])
414 if not res2['ifname'].startswith('p2p-' + iface):
415 raise Exception("Unexpected group ifname: " + res2['ifname'])
417 terminate_group(dev[0], wpas)
419 def test_p2p_device_join_no_group_iface(dev, apdev):
420 """P2P join-group using cfg80211 P2P Device (no separate group interface)"""
421 with HWSimRadio(use_p2p_device=True) as (radio, iface):
422 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
423 wpas.interface_add(iface)
424 wpas.global_request("SET p2p_no_group_iface 1")
426 res = dev[0].p2p_start_go()
427 bssid = dev[0].get_group_status_field('bssid')
429 wpas.scan_for_bss(bssid, res['freq'])
430 res2 = connect_cli(dev[0], wpas, freq=res['freq'])
431 if res2['ifname'] != iface:
432 raise Exception("Unexpected group ifname: " + res2['ifname'])
434 terminate_group(dev[0], wpas)
436 def test_p2p_device_persistent_group(dev):
437 """P2P persistent group formation and re-invocation with cfg80211 P2P Device"""
438 with HWSimRadio(use_p2p_device=True) as (radio, iface):
439 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
440 wpas.interface_add(iface)
441 wpas.global_request("SET p2p_no_group_iface 0")
444 invite_from_cli(dev[0], wpas)
445 invite_from_go(dev[0], wpas)
447 def test_p2p_device_persistent_group_no_group_iface(dev):
448 """P2P persistent group formation and re-invocation with cfg80211 P2P Device (no separate group interface)"""
449 with HWSimRadio(use_p2p_device=True) as (radio, iface):
450 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
451 wpas.interface_add(iface)
452 wpas.global_request("SET p2p_no_group_iface 1")
455 invite_from_cli(dev[0], wpas)
456 invite_from_go(dev[0], wpas)
458 def test_p2p_device_persistent_group2(dev):
459 """P2P persistent group formation and re-invocation (reverse) with cfg80211 P2P Device"""
460 with HWSimRadio(use_p2p_device=True) as (radio, iface):
461 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
462 wpas.interface_add(iface)
463 wpas.global_request("SET p2p_no_group_iface 0")
466 invite_from_cli(wpas, dev[0])
467 invite_from_go(wpas, dev[0])
469 def test_p2p_device_persistent_group2_no_group_iface(dev):
470 """P2P persistent group formation and re-invocation (reverse) with cfg80211 P2P Device (no separate group interface)"""
471 with HWSimRadio(use_p2p_device=True) as (radio, iface):
472 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
473 wpas.interface_add(iface)
474 wpas.global_request("SET p2p_no_group_iface 1")
477 invite_from_cli(wpas, dev[0])
478 invite_from_go(wpas, dev[0])
480 def p2p_device_group_conf(dev1, dev2):
481 dev1.global_request("SET p2p_group_idle 12")
482 dev1.global_request("SET p2p_go_freq_change_policy 2")
483 dev1.global_request("SET p2p_go_ctwindow 7")
485 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev1, i_intent=15,
486 r_dev=dev2, r_intent=0)
487 check_grpform_results(i_res, r_res)
489 if (dev1.group_request("GET p2p_group_idle") != "12" or
490 dev1.group_request("GET p2p_go_freq_change_policy") != "2" or
491 dev1.group_request("GET p2p_go_ctwindow") != "7"):
492 raise Exception("Unexpected configuration value")
494 remove_group(dev1, dev2)
495 dev1.global_request("P2P_FLUSH")
496 dev2.global_request("P2P_FLUSH")
498 def test_p2p_device_conf(dev, apdev):
499 """P2P configuration with cfg80211 P2P Device"""
500 with HWSimRadio(use_p2p_device=True) as (radio, iface):
501 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
502 wpas.interface_add(iface)
503 wpas.global_request("SET p2p_no_group_iface 1")
504 p2p_device_group_conf(wpas, dev[0])
505 wpas.global_request("SET p2p_no_group_iface 0")
506 p2p_device_group_conf(wpas, dev[0])