test_p2p_autogo.py 19 KB

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