test_p2p_autogo.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. # P2P autonomous GO test cases
  2. # Copyright (c) 2013-2014, 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 time
  7. import subprocess
  8. import logging
  9. logger = logging.getLogger()
  10. import hwsim_utils
  11. import utils
  12. from wlantest import Wlantest
  13. from wpasupplicant import WpaSupplicant
  14. def autogo(go, freq=None, persistent=None):
  15. logger.info("Start autonomous GO " + go.ifname)
  16. res = go.p2p_start_go(freq=freq, persistent=persistent)
  17. logger.debug("res: " + str(res))
  18. return res
  19. def connect_cli(go, client, social=False, freq=None):
  20. logger.info("Try to connect the client to the GO")
  21. pin = client.wps_read_pin()
  22. go.p2p_go_authorize_client(pin)
  23. res = client.p2p_connect_group(go.p2p_dev_addr(), pin, timeout=60,
  24. social=social, freq=freq)
  25. logger.info("Client connected")
  26. hwsim_utils.test_connectivity_p2p(go, client)
  27. return res
  28. def test_autogo(dev):
  29. """P2P autonomous GO and client joining group"""
  30. addr0 = dev[0].p2p_dev_addr()
  31. addr2 = dev[2].p2p_dev_addr()
  32. res = autogo(dev[0])
  33. if "p2p-wlan" in res['ifname']:
  34. raise Exception("Unexpected group interface name on GO")
  35. res = connect_cli(dev[0], dev[1])
  36. if "p2p-wlan" in res['ifname']:
  37. raise Exception("Unexpected group interface name on client")
  38. bss = dev[1].get_bss("p2p_dev_addr=" + addr0)
  39. if bss['bssid'] != dev[0].p2p_interface_addr():
  40. raise Exception("Unexpected BSSID in the BSS entry for the GO")
  41. id = bss['id']
  42. bss = dev[1].get_bss("ID-" + id)
  43. if bss['id'] != id:
  44. raise Exception("Could not find BSS entry based on id")
  45. res = dev[1].request("BSS RANGE=" + id + "- MASK=0x1")
  46. if "id=" + id not in res:
  47. raise Exception("Could not find BSS entry based on id range")
  48. res = dev[1].request("SCAN_RESULTS")
  49. if "[P2P]" not in res:
  50. raise Exception("P2P flag missing from scan results: " + res)
  51. # Presence request to increase testing coverage
  52. if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400"):
  53. raise Exception("Could not send presence request")
  54. ev = dev[1].wait_event(["P2P-PRESENCE-RESPONSE"])
  55. if ev is None:
  56. raise Exception("Timeout while waiting for Presence Response")
  57. if not dev[2].discover_peer(addr0):
  58. raise Exception("Could not discover GO")
  59. dev[0].dump_monitor()
  60. dev[2].global_request("P2P_PROV_DISC " + addr0 + " display join")
  61. ev = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=10)
  62. if ev is None:
  63. raise Exception("GO did not report P2P-PROV-DISC-SHOW-PIN")
  64. if "p2p_dev_addr=" + addr2 not in ev:
  65. raise Exception("Unexpected P2P Device Address in event: " + ev)
  66. if "group=" + dev[0].group_ifname not in ev:
  67. raise Exception("Unexpected group interface in event: " + ev)
  68. ev = dev[2].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=10)
  69. if ev is None:
  70. raise Exception("P2P-PROV-DISC-ENTER-PIN not reported")
  71. dev[0].remove_group()
  72. dev[1].wait_go_ending_session()
  73. def test_autogo2(dev):
  74. """P2P autonomous GO with a separate group interface and client joining group"""
  75. dev[0].request("SET p2p_no_group_iface 0")
  76. res = autogo(dev[0], freq=2437)
  77. if "p2p-wlan" not in res['ifname']:
  78. raise Exception("Unexpected group interface name on GO")
  79. if res['ifname'] not in utils.get_ifnames():
  80. raise Exception("Could not find group interface netdev")
  81. connect_cli(dev[0], dev[1], social=True, freq=2437)
  82. dev[0].remove_group()
  83. dev[1].wait_go_ending_session()
  84. if res['ifname'] in utils.get_ifnames():
  85. raise Exception("Group interface netdev was not removed")
  86. def test_autogo3(dev):
  87. """P2P autonomous GO and client with a separate group interface joining group"""
  88. dev[1].request("SET p2p_no_group_iface 0")
  89. autogo(dev[0], freq=2462)
  90. res = connect_cli(dev[0], dev[1], social=True, freq=2462)
  91. if "p2p-wlan" not in res['ifname']:
  92. raise Exception("Unexpected group interface name on client")
  93. if res['ifname'] not in utils.get_ifnames():
  94. raise Exception("Could not find group interface netdev")
  95. dev[0].remove_group()
  96. dev[1].wait_go_ending_session()
  97. dev[1].ping()
  98. if res['ifname'] in utils.get_ifnames():
  99. raise Exception("Group interface netdev was not removed")
  100. def test_autogo4(dev):
  101. """P2P autonomous GO and client joining group (both with a separate group interface)"""
  102. dev[0].request("SET p2p_no_group_iface 0")
  103. dev[1].request("SET p2p_no_group_iface 0")
  104. res1 = autogo(dev[0], freq=2412)
  105. res2 = connect_cli(dev[0], dev[1], social=True, freq=2412)
  106. if "p2p-wlan" not in res1['ifname']:
  107. raise Exception("Unexpected group interface name on GO")
  108. if "p2p-wlan" not in res2['ifname']:
  109. raise Exception("Unexpected group interface name on client")
  110. ifnames = utils.get_ifnames()
  111. if res1['ifname'] not in ifnames:
  112. raise Exception("Could not find GO group interface netdev")
  113. if res2['ifname'] not in ifnames:
  114. raise Exception("Could not find client group interface netdev")
  115. dev[0].remove_group()
  116. dev[1].wait_go_ending_session()
  117. dev[1].ping()
  118. ifnames = utils.get_ifnames()
  119. if res1['ifname'] in ifnames:
  120. raise Exception("GO group interface netdev was not removed")
  121. if res2['ifname'] in ifnames:
  122. raise Exception("Client group interface netdev was not removed")
  123. def test_autogo_m2d(dev):
  124. """P2P autonomous GO and clients not authorized"""
  125. autogo(dev[0], freq=2412)
  126. go_addr = dev[0].p2p_dev_addr()
  127. dev[1].request("SET p2p_no_group_iface 0")
  128. if not dev[1].discover_peer(go_addr, social=True):
  129. raise Exception("GO " + go_addr + " not found")
  130. dev[1].dump_monitor()
  131. if not dev[2].discover_peer(go_addr, social=True):
  132. raise Exception("GO " + go_addr + " not found")
  133. dev[2].dump_monitor()
  134. logger.info("Trying to join the group when GO has not authorized the client")
  135. pin = dev[1].wps_read_pin()
  136. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  137. if "OK" not in dev[1].global_request(cmd):
  138. raise Exception("P2P_CONNECT join failed")
  139. pin = dev[2].wps_read_pin()
  140. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  141. if "OK" not in dev[2].global_request(cmd):
  142. raise Exception("P2P_CONNECT join failed")
  143. ev = dev[1].wait_global_event(["WPS-M2D"], timeout=10)
  144. if ev is None:
  145. raise Exception("No global M2D event")
  146. ifaces = dev[1].request("INTERFACES").splitlines()
  147. iface = ifaces[0] if "p2p-wlan" in ifaces[0] else ifaces[1]
  148. wpas = WpaSupplicant(ifname=iface)
  149. ev = wpas.wait_event(["WPS-M2D"], timeout=10)
  150. if ev is None:
  151. raise Exception("No M2D event on group interface")
  152. ev = dev[2].wait_global_event(["WPS-M2D"], timeout=10)
  153. if ev is None:
  154. raise Exception("No global M2D event (2)")
  155. ev = dev[2].wait_event(["WPS-M2D"], timeout=10)
  156. if ev is None:
  157. raise Exception("No M2D event on group interface (2)")
  158. def test_autogo_fail(dev):
  159. """P2P autonomous GO and incorrect PIN"""
  160. autogo(dev[0], freq=2412)
  161. go_addr = dev[0].p2p_dev_addr()
  162. dev[0].p2p_go_authorize_client("00000000")
  163. dev[1].request("SET p2p_no_group_iface 0")
  164. if not dev[1].discover_peer(go_addr, social=True):
  165. raise Exception("GO " + go_addr + " not found")
  166. dev[1].dump_monitor()
  167. logger.info("Trying to join the group when GO has not authorized the client")
  168. pin = dev[1].wps_read_pin()
  169. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  170. if "OK" not in dev[1].global_request(cmd):
  171. raise Exception("P2P_CONNECT join failed")
  172. ev = dev[1].wait_global_event(["WPS-FAIL"], timeout=10)
  173. if ev is None:
  174. raise Exception("No global WPS-FAIL event")
  175. def test_autogo_2cli(dev):
  176. """P2P autonomous GO and two clients joining group"""
  177. autogo(dev[0], freq=2412)
  178. connect_cli(dev[0], dev[1], social=True, freq=2412)
  179. connect_cli(dev[0], dev[2], social=True, freq=2412)
  180. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  181. dev[0].global_request("P2P_REMOVE_CLIENT " + dev[1].p2p_dev_addr())
  182. dev[1].wait_go_ending_session()
  183. dev[0].remove_group()
  184. dev[2].wait_go_ending_session()
  185. def test_autogo_pbc(dev):
  186. """P2P autonomous GO and PBC"""
  187. dev[1].request("SET p2p_no_group_iface 0")
  188. autogo(dev[0], freq=2412)
  189. if "FAIL" not in dev[0].group_request("WPS_PBC p2p_dev_addr=00:11:22:33:44"):
  190. raise Exception("Invalid WPS_PBC succeeded")
  191. if "OK" not in dev[0].group_request("WPS_PBC p2p_dev_addr=" + dev[1].p2p_dev_addr()):
  192. raise Exception("WPS_PBC failed")
  193. dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=0,
  194. social=True)
  195. ev = dev[2].wait_event(["WPS-M2D"], timeout=15)
  196. if ev is None:
  197. raise Exception("WPS-M2D not reported")
  198. if "config_error=12" not in ev:
  199. raise Exception("Unexpected config_error: " + ev)
  200. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=15,
  201. social=True)
  202. def test_autogo_tdls(dev):
  203. """P2P autonomous GO and two clients using TDLS"""
  204. wt = Wlantest()
  205. go = dev[0]
  206. logger.info("Start autonomous GO with fixed parameters " + go.ifname)
  207. id = go.add_network()
  208. go.set_network_quoted(id, "ssid", "DIRECT-tdls")
  209. go.set_network_quoted(id, "psk", "12345678")
  210. go.set_network(id, "mode", "3")
  211. go.set_network(id, "disabled", "2")
  212. res = go.p2p_start_go(persistent=id, freq="2462")
  213. logger.debug("res: " + str(res))
  214. wt.flush()
  215. wt.add_passphrase("12345678")
  216. connect_cli(go, dev[1], social=True, freq=2462)
  217. connect_cli(go, dev[2], social=True, freq=2462)
  218. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  219. bssid = dev[0].p2p_interface_addr()
  220. addr1 = dev[1].p2p_interface_addr()
  221. addr2 = dev[2].p2p_interface_addr()
  222. dev[1].tdls_setup(addr2)
  223. time.sleep(1)
  224. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  225. conf = wt.get_tdls_counter("setup_conf_ok", bssid, addr1, addr2);
  226. if conf == 0:
  227. raise Exception("No TDLS Setup Confirm (success) seen")
  228. dl = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
  229. if dl == 0:
  230. raise Exception("No valid frames through direct link")
  231. wt.tdls_clear(bssid, addr1, addr2);
  232. dev[1].tdls_teardown(addr2)
  233. time.sleep(1)
  234. teardown = wt.get_tdls_counter("teardown", bssid, addr1, addr2);
  235. if teardown == 0:
  236. raise Exception("No TDLS Setup Teardown seen")
  237. wt.tdls_clear(bssid, addr1, addr2);
  238. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  239. ap_path = wt.get_tdls_counter("valid_ap_path", bssid, addr1, addr2);
  240. if ap_path == 0:
  241. raise Exception("No valid frames via AP path")
  242. direct_link = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
  243. if direct_link > 0:
  244. raise Exception("Unexpected frames through direct link")
  245. idirect_link = wt.get_tdls_counter("invalid_direct_link", bssid, addr1,
  246. addr2);
  247. if idirect_link > 0:
  248. raise Exception("Unexpected frames through direct link (invalid)")
  249. dev[2].remove_group()
  250. dev[1].remove_group()
  251. dev[0].remove_group()
  252. def test_autogo_legacy(dev):
  253. """P2P autonomous GO and legacy clients"""
  254. res = autogo(dev[0], freq=2462)
  255. if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
  256. raise Exception("passphrase mismatch")
  257. if dev[0].request("P2P_GET_PASSPHRASE") != res['passphrase']:
  258. raise Exception("passphrase mismatch(2)")
  259. logger.info("Connect P2P client")
  260. connect_cli(dev[0], dev[1], social=True, freq=2462)
  261. logger.info("Connect legacy WPS client")
  262. pin = dev[2].wps_read_pin()
  263. dev[0].p2p_go_authorize_client(pin)
  264. dev[2].request("P2P_SET disabled 1")
  265. dev[2].dump_monitor()
  266. dev[2].request("WPS_PIN any " + pin)
  267. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  268. if ev is None:
  269. raise Exception("Association with the GO timed out")
  270. status = dev[2].get_status()
  271. if status['wpa_state'] != 'COMPLETED':
  272. raise Exception("Not fully connected")
  273. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  274. dev[2].request("DISCONNECT")
  275. logger.info("Connect legacy non-WPS client")
  276. dev[2].request("FLUSH")
  277. dev[2].request("P2P_SET disabled 1")
  278. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  279. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  280. scan_freq=res['freq'])
  281. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  282. dev[2].request("DISCONNECT")
  283. dev[0].remove_group()
  284. dev[1].wait_go_ending_session()
  285. def test_autogo_chan_switch(dev):
  286. """P2P autonomous GO switching channels"""
  287. autogo(dev[0], freq=2417)
  288. connect_cli(dev[0], dev[1])
  289. res = dev[0].request("CHAN_SWITCH 5 2422")
  290. if "FAIL" in res:
  291. # for now, skip test since mac80211_hwsim support is not yet widely
  292. # deployed
  293. return 'skip'
  294. ev = dev[0].wait_event(["AP-CSA-FINISHED"], timeout=10)
  295. if ev is None:
  296. raise Exception("CSA finished event timed out")
  297. if "freq=2422" not in ev:
  298. raise Exception("Unexpected cahnnel in CSA finished event")
  299. dev[0].dump_monitor()
  300. dev[1].dump_monitor()
  301. time.sleep(0.1)
  302. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  303. def test_autogo_extra_cred(dev):
  304. """P2P autonomous GO sending two WPS credentials"""
  305. if "FAIL" in dev[0].request("SET wps_testing_dummy_cred 1"):
  306. raise Exception("Failed to enable test mode")
  307. autogo(dev[0], freq=2412)
  308. connect_cli(dev[0], dev[1], social=True, freq=2412)
  309. dev[0].remove_group()
  310. dev[1].wait_go_ending_session()
  311. def test_autogo_ifdown(dev):
  312. """P2P autonomous GO and external ifdown"""
  313. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  314. wpas.interface_add("wlan5")
  315. res = autogo(wpas)
  316. wpas.dump_monitor()
  317. wpas.interface_remove("wlan5")
  318. wpas.interface_add("wlan5")
  319. res = autogo(wpas)
  320. wpas.dump_monitor()
  321. subprocess.call(['sudo', 'ifconfig', res['ifname'], 'down'])
  322. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  323. if ev is None:
  324. raise Exception("Group removal not reported")
  325. if res['ifname'] not in ev:
  326. raise Exception("Unexpected group removal event: " + ev)
  327. def test_autogo_start_during_scan(dev):
  328. """P2P autonomous GO started during ongoing manual scan"""
  329. try:
  330. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  331. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  332. raise Exception("Failed to set autoscan")
  333. autogo(dev[0], freq=2462)
  334. connect_cli(dev[0], dev[1], social=True, freq=2462)
  335. dev[0].remove_group()
  336. dev[1].wait_go_ending_session()
  337. finally:
  338. dev[0].request("AUTOSCAN ")
  339. def test_autogo_passphrase_len(dev):
  340. """P2P autonomous GO and longer passphrase"""
  341. try:
  342. if "OK" not in dev[0].request("SET p2p_passphrase_len 13"):
  343. raise Exception("Failed to set passphrase length")
  344. res = autogo(dev[0], freq=2412)
  345. if len(res['passphrase']) != 13:
  346. raise Exception("Unexpected passphrase length")
  347. if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
  348. raise Exception("passphrase mismatch")
  349. logger.info("Connect P2P client")
  350. connect_cli(dev[0], dev[1], social=True, freq=2412)
  351. logger.info("Connect legacy WPS client")
  352. pin = dev[2].wps_read_pin()
  353. dev[0].p2p_go_authorize_client(pin)
  354. dev[2].request("P2P_SET disabled 1")
  355. dev[2].dump_monitor()
  356. dev[2].request("WPS_PIN any " + pin)
  357. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  358. if ev is None:
  359. raise Exception("Association with the GO timed out")
  360. status = dev[2].get_status()
  361. if status['wpa_state'] != 'COMPLETED':
  362. raise Exception("Not fully connected")
  363. dev[2].request("DISCONNECT")
  364. logger.info("Connect legacy non-WPS client")
  365. dev[2].request("FLUSH")
  366. dev[2].request("P2P_SET disabled 1")
  367. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  368. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  369. scan_freq=res['freq'])
  370. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  371. dev[2].request("DISCONNECT")
  372. dev[0].remove_group()
  373. dev[1].wait_go_ending_session()
  374. finally:
  375. dev[0].request("SET p2p_passphrase_len 8")
  376. def test_autogo_bridge(dev):
  377. """P2P autonomous GO in a bridge"""
  378. try:
  379. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  380. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  381. raise Exception("Failed to set autoscan")
  382. autogo(dev[0])
  383. subprocess.call(['sudo', 'brctl', 'addbr', 'p2p-br0'])
  384. subprocess.call(['sudo', 'brctl', 'setfd', 'p2p-br0', '0'])
  385. subprocess.call(['sudo', 'brctl', 'addif', 'p2p-br0', dev[0].ifname])
  386. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'up'])
  387. time.sleep(0.1)
  388. subprocess.call(['sudo', 'brctl', 'delif', 'p2p-br0', dev[0].ifname])
  389. time.sleep(0.1)
  390. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'down'])
  391. time.sleep(0.1)
  392. subprocess.call(['sudo', 'brctl', 'delbr', 'p2p-br0'])
  393. ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=1)
  394. if ev is not None:
  395. raise Exception("P2P group removed unexpectedly")
  396. if dev[0].get_status_field('wpa_state') != "COMPLETED":
  397. raise Exception("Unexpected wpa_state")
  398. dev[0].remove_group()
  399. finally:
  400. dev[0].request("AUTOSCAN ")
  401. subprocess.Popen(['sudo', 'brctl', 'delif', 'p2p-br0', dev[0].ifname],
  402. stderr=open('/dev/null', 'w'))
  403. subprocess.Popen(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'down'],
  404. stderr=open('/dev/null', 'w'))
  405. subprocess.Popen(['sudo', 'brctl', 'delbr', 'p2p-br0'],
  406. stderr=open('/dev/null', 'w'))