test_p2p_device.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # cfg80211 P2P Device
  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. import logging
  7. logger = logging.getLogger()
  8. import time
  9. from wpasupplicant import WpaSupplicant
  10. from p2p_utils import *
  11. from test_nfc_p2p import set_ip_addr_info, check_ip_addr, grpform_events
  12. from hwsim import HWSimRadio
  13. from utils import HwsimSkip
  14. import hostapd
  15. import hwsim_utils
  16. def test_p2p_device_grpform(dev, apdev):
  17. """P2P group formation with driver using cfg80211 P2P Device"""
  18. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  19. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  20. wpas.interface_add(iface)
  21. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  22. r_dev=wpas, r_intent=0)
  23. check_grpform_results(i_res, r_res)
  24. wpas.dump_monitor()
  25. remove_group(dev[0], wpas)
  26. wpas.dump_monitor()
  27. res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
  28. lines = res.splitlines()
  29. found = False
  30. for l in lines:
  31. try:
  32. [name,value] = l.split('=', 1)
  33. if name == "wdev_id":
  34. found = True
  35. break
  36. except ValueError:
  37. pass
  38. if not found:
  39. raise Exception("wdev_id not found")
  40. def test_p2p_device_grpform2(dev, apdev):
  41. """P2P group formation with driver using cfg80211 P2P Device (reverse)"""
  42. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  43. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  44. wpas.interface_add(iface)
  45. [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
  46. r_dev=dev[0], r_intent=0)
  47. check_grpform_results(i_res, r_res)
  48. wpas.dump_monitor()
  49. remove_group(wpas, dev[0])
  50. wpas.dump_monitor()
  51. def test_p2p_device_group_remove(dev, apdev):
  52. """P2P group removal via the P2P ctrl interface with driver using cfg80211 P2P Device"""
  53. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  54. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  55. wpas.interface_add(iface)
  56. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  57. r_dev=wpas, r_intent=0)
  58. check_grpform_results(i_res, r_res)
  59. # Issue the remove request on the interface which will be removed
  60. p2p_iface_wpas = WpaSupplicant(ifname=r_res['ifname'])
  61. res = p2p_iface_wpas.request("P2P_GROUP_REMOVE *")
  62. if "OK" not in res:
  63. raise Exception("Failed to remove P2P group")
  64. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  65. if ev is None:
  66. raise Exception("Group removal event not received")
  67. if not wpas.global_ping():
  68. raise Exception("Could not ping global ctrl_iface after group removal")
  69. def test_p2p_device_concurrent_scan(dev, apdev):
  70. """Concurrent P2P and station mode scans with driver using cfg80211 P2P Device"""
  71. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  72. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  73. wpas.interface_add(iface)
  74. wpas.p2p_find()
  75. time.sleep(0.1)
  76. wpas.request("SCAN")
  77. ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
  78. if ev is None:
  79. raise Exception("Station mode scan did not start")
  80. def test_p2p_device_nfc_invite(dev, apdev):
  81. """P2P NFC invitiation with driver using cfg80211 P2P Device"""
  82. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  83. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  84. wpas.interface_add(iface)
  85. set_ip_addr_info(dev[0])
  86. logger.info("Start autonomous GO")
  87. dev[0].p2p_start_go()
  88. logger.info("Write NFC Tag on the P2P Client")
  89. res = wpas.global_request("P2P_LISTEN")
  90. if "FAIL" in res:
  91. raise Exception("Failed to start Listen mode")
  92. wpas.dump_monitor()
  93. pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
  94. if "FAIL" in pw:
  95. raise Exception("Failed to generate password token")
  96. res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
  97. if "FAIL" in res:
  98. raise Exception("Failed to enable NFC Tag for P2P static handover")
  99. sel = wpas.global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  100. if "FAIL" in sel:
  101. raise Exception("Failed to generate NFC connection handover select")
  102. wpas.dump_monitor()
  103. logger.info("Read NFC Tag on the GO to trigger invitation")
  104. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
  105. if "FAIL" in res:
  106. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  107. ev = wpas.wait_global_event(grpform_events, timeout=20)
  108. if ev is None:
  109. raise Exception("Joining the group timed out")
  110. res = wpas.group_form_result(ev)
  111. wpas.dump_monitor()
  112. hwsim_utils.test_connectivity_p2p(dev[0], wpas)
  113. check_ip_addr(res)
  114. wpas.dump_monitor()
  115. def test_p2p_device_misuses(dev, apdev):
  116. """cfg80211 P2P Device misuses"""
  117. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  118. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  119. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  120. wpas.interface_add(iface)
  121. # Add a normal network profile to the P2P Device management only
  122. # interface to verify that it does not get used.
  123. id = int(wpas.global_request('IFNAME=p2p-dev-%s ADD_NETWORK' % iface).strip())
  124. wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d ssid "open"' % (iface, id))
  125. wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d key_mgmt NONE' % (iface, id))
  126. wpas.global_request('IFNAME=p2p-dev-%s ENABLE_NETWORK %d' % (iface, id))
  127. # Scan requests get ignored on p2p-dev
  128. wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
  129. dev[0].p2p_start_go(freq=2412)
  130. addr = dev[0].p2p_interface_addr()
  131. wpas.scan_for_bss(addr, freq=2412)
  132. wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
  133. hwsim_utils.test_connectivity(wpas, hapd)
  134. pin = wpas.wps_read_pin()
  135. dev[0].p2p_go_authorize_client(pin)
  136. res = wpas.p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
  137. social=True, freq=2412)
  138. hwsim_utils.test_connectivity_p2p(dev[0], wpas)
  139. # Optimize scan-after-disconnect
  140. wpas.group_request("SET_NETWORK 0 scan_freq 2412")
  141. dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
  142. ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
  143. if ev is None:
  144. raise Exception("Did not see disconnect event on P2P group interface")
  145. dev[0].remove_group()
  146. ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  147. if ev is None:
  148. raise Exception("Scan not started")
  149. ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
  150. if ev is None:
  151. raise Exception("Scan not completed")
  152. time.sleep(1)
  153. hwsim_utils.test_connectivity(wpas, hapd)
  154. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=0.1)
  155. if ev is not None:
  156. raise Exception("Unexpected disconnection event received from hostapd")
  157. ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
  158. if ev is not None:
  159. raise Exception("Unexpected disconnection event received from wpa_supplicant")
  160. wpas.request("DISCONNECT")
  161. wpas.wait_disconnected()
  162. def test_p2p_device_incorrect_command_interface(dev, apdev):
  163. """cfg80211 P2P Device and P2P_* command on incorrect interface"""
  164. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  165. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  166. wpas.interface_add(iface)
  167. dev[0].p2p_listen()
  168. wpas.request('P2P_FIND type=social')
  169. ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
  170. if ev is None:
  171. raise Exception("Peer not found")
  172. ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
  173. if ev is not None:
  174. raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
  175. wpas.dump_monitor()
  176. pin = wpas.wps_read_pin()
  177. dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
  178. freq=2412)
  179. wpas.request('P2P_STOP_FIND')
  180. wpas.dump_monitor()
  181. if "OK" not in wpas.request('P2P_CONNECT ' + dev[0].p2p_dev_addr() + ' ' + pin + ' display go_intent=1'):
  182. raise Exception("P2P_CONNECT failed")
  183. ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  184. if ev is None:
  185. raise Exception("Group formation timed out")
  186. wpas.group_form_result(ev)
  187. wpas.dump_monitor()
  188. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  189. if ev is None:
  190. raise Exception("Group formation timed out(2)")
  191. dev[0].group_form_result(ev)
  192. dev[0].remove_group()
  193. wpas.wait_go_ending_session()
  194. wpas.dump_monitor()
  195. def test_p2p_device_incorrect_command_interface2(dev, apdev):
  196. """cfg80211 P2P Device and P2P_GROUP_ADD command on incorrect interface"""
  197. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  198. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  199. wpas.interface_add(iface)
  200. if "OK" not in wpas.request('P2P_GROUP_ADD'):
  201. raise Exception("P2P_GROUP_ADD failed")
  202. ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  203. if ev is None:
  204. raise Exception("Group formation timed out")
  205. res = wpas.group_form_result(ev)
  206. wpas.dump_monitor()
  207. logger.info("Group results: " + str(res))
  208. wpas.remove_group()
  209. if not res['ifname'].startswith('p2p-' + iface + '-'):
  210. raise Exception("Unexpected group ifname: " + res['ifname'])
  211. wpas.dump_monitor()
  212. def test_p2p_device_grpform_timeout_client(dev, apdev):
  213. """P2P group formation timeout on client with cfg80211 P2P Device"""
  214. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  215. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  216. wpas.interface_add(iface)
  217. addr0 = dev[0].p2p_dev_addr()
  218. addr5 = wpas.p2p_dev_addr()
  219. wpas.p2p_listen()
  220. dev[0].discover_peer(addr5)
  221. dev[0].p2p_listen()
  222. wpas.discover_peer(addr0)
  223. wpas.p2p_ext_listen(100, 150)
  224. dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=15 auth")
  225. wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=0")
  226. ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
  227. if ev is None:
  228. raise Exception("GO Negotiation did not succeed")
  229. ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
  230. if ev is None:
  231. raise Exception("WPS did not succeed (GO)")
  232. if "OK" not in dev[0].global_request("P2P_CANCEL"):
  233. wpas.global_request("P2P_CANCEL")
  234. del wpas
  235. raise HwsimSkip("Did not manage to cancel group formation")
  236. dev[0].dump_monitor()
  237. ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
  238. if ev is None:
  239. raise Exception("WPS did not succeed (Client)")
  240. dev[0].dump_monitor()
  241. ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
  242. if ev is None:
  243. raise Exception("Group formation timeout not seen on client")
  244. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
  245. if ev is None:
  246. raise Exception("Group removal not seen on client")
  247. wpas.p2p_cancel_ext_listen()
  248. time.sleep(0.1)
  249. ifaces = wpas.global_request("INTERFACES")
  250. logger.info("Remaining interfaces: " + ifaces)
  251. del wpas
  252. if "p2p-" + iface + "-" in ifaces:
  253. raise Exception("Group interface still present after failure")
  254. def test_p2p_device_grpform_timeout_go(dev, apdev):
  255. """P2P group formation timeout on GO with cfg80211 P2P Device"""
  256. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  257. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  258. wpas.interface_add(iface)
  259. addr0 = dev[0].p2p_dev_addr()
  260. addr5 = wpas.p2p_dev_addr()
  261. wpas.p2p_listen()
  262. dev[0].discover_peer(addr5)
  263. dev[0].p2p_listen()
  264. wpas.discover_peer(addr0)
  265. wpas.p2p_ext_listen(100, 150)
  266. dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=0 auth")
  267. wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=15")
  268. ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
  269. if ev is None:
  270. raise Exception("GO Negotiation did not succeed")
  271. ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
  272. if ev is None:
  273. raise Exception("WPS did not succeed (Client)")
  274. if "OK" not in dev[0].global_request("P2P_CANCEL"):
  275. if "OK" not in dev[0].global_request("P2P_GROUP_REMOVE *"):
  276. wpas.global_request("P2P_CANCEL")
  277. del wpas
  278. raise HwsimSkip("Did not manage to cancel group formation")
  279. dev[0].dump_monitor()
  280. ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
  281. if ev is None:
  282. raise Exception("WPS did not succeed (GO)")
  283. dev[0].dump_monitor()
  284. ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
  285. if ev is None:
  286. raise Exception("Group formation timeout not seen on GO")
  287. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
  288. if ev is None:
  289. raise Exception("Group removal not seen on GO")
  290. wpas.p2p_cancel_ext_listen()
  291. time.sleep(0.1)
  292. ifaces = wpas.global_request("INTERFACES")
  293. logger.info("Remaining interfaces: " + ifaces)
  294. del wpas
  295. if "p2p-" + iface + "-" in ifaces:
  296. raise Exception("Group interface still present after failure")