test_p2p_autogo.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. # P2P autonomous GO test cases
  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 time
  7. import subprocess
  8. import logging
  9. logger = logging.getLogger()
  10. import hwsim_utils
  11. import utils
  12. from utils import HwsimSkip
  13. from wlantest import Wlantest
  14. from wpasupplicant import WpaSupplicant
  15. from p2p_utils import *
  16. from test_p2p_messages import mgmt_tx, parse_p2p_public_action
  17. def test_autogo(dev):
  18. """P2P autonomous GO and client joining group"""
  19. addr0 = dev[0].p2p_dev_addr()
  20. addr2 = dev[2].p2p_dev_addr()
  21. res = autogo(dev[0])
  22. if "p2p-wlan" in res['ifname']:
  23. raise Exception("Unexpected group interface name on GO")
  24. res = connect_cli(dev[0], dev[1])
  25. if "p2p-wlan" in res['ifname']:
  26. raise Exception("Unexpected group interface name on client")
  27. bss = dev[1].get_bss("p2p_dev_addr=" + addr0, res['ifname'])
  28. if not bss or bss['bssid'] != dev[0].p2p_interface_addr():
  29. raise Exception("Unexpected BSSID in the BSS entry for the GO")
  30. id = bss['id']
  31. bss = dev[1].get_bss("ID-" + id, res['ifname'])
  32. if not bss or bss['id'] != id:
  33. raise Exception("Could not find BSS entry based on id")
  34. res = dev[1].group_request("BSS RANGE=" + id + "- MASK=0x1")
  35. if "id=" + id not in res:
  36. raise Exception("Could not find BSS entry based on id range")
  37. res = dev[1].request("SCAN_RESULTS")
  38. if "[P2P]" not in res:
  39. raise Exception("P2P flag missing from scan results: " + res)
  40. # Presence request to increase testing coverage
  41. if "FAIL" not in dev[1].group_request("P2P_PRESENCE_REQ 30000"):
  42. raise Exception("Invald P2P_PRESENCE_REQ accepted")
  43. if "FAIL" not in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400 30001"):
  44. raise Exception("Invald P2P_PRESENCE_REQ accepted")
  45. if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400"):
  46. raise Exception("Could not send presence request")
  47. ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"], 10)
  48. if ev is None:
  49. raise Exception("Timeout while waiting for Presence Response")
  50. if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400 20000 102400"):
  51. raise Exception("Could not send presence request")
  52. ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"])
  53. if ev is None:
  54. raise Exception("Timeout while waiting for Presence Response")
  55. if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ"):
  56. raise Exception("Could not send presence request")
  57. ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"])
  58. if ev is None:
  59. raise Exception("Timeout while waiting for Presence Response")
  60. if not dev[2].discover_peer(addr0):
  61. raise Exception("Could not discover GO")
  62. dev[0].dump_monitor()
  63. dev[2].global_request("P2P_PROV_DISC " + addr0 + " display join")
  64. ev = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=10)
  65. if ev is None:
  66. raise Exception("GO did not report P2P-PROV-DISC-SHOW-PIN")
  67. if "p2p_dev_addr=" + addr2 not in ev:
  68. raise Exception("Unexpected P2P Device Address in event: " + ev)
  69. if "group=" + dev[0].group_ifname not in ev:
  70. raise Exception("Unexpected group interface in event: " + ev)
  71. ev = dev[2].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=10)
  72. if ev is None:
  73. raise Exception("P2P-PROV-DISC-ENTER-PIN not reported")
  74. dev[0].remove_group()
  75. dev[1].wait_go_ending_session()
  76. def test_autogo2(dev):
  77. """P2P autonomous GO with a separate group interface and client joining group"""
  78. dev[0].request("SET p2p_no_group_iface 0")
  79. res = autogo(dev[0], freq=2437)
  80. if "p2p-wlan" not in res['ifname']:
  81. raise Exception("Unexpected group interface name on GO")
  82. if res['ifname'] not in utils.get_ifnames():
  83. raise Exception("Could not find group interface netdev")
  84. connect_cli(dev[0], dev[1], social=True, freq=2437)
  85. dev[0].remove_group()
  86. dev[1].wait_go_ending_session()
  87. if res['ifname'] in utils.get_ifnames():
  88. raise Exception("Group interface netdev was not removed")
  89. def test_autogo3(dev):
  90. """P2P autonomous GO and client with a separate group interface joining group"""
  91. dev[1].request("SET p2p_no_group_iface 0")
  92. autogo(dev[0], freq=2462)
  93. res = connect_cli(dev[0], dev[1], social=True, freq=2462)
  94. if "p2p-wlan" not in res['ifname']:
  95. raise Exception("Unexpected group interface name on client")
  96. if res['ifname'] not in utils.get_ifnames():
  97. raise Exception("Could not find group interface netdev")
  98. dev[0].remove_group()
  99. dev[1].wait_go_ending_session()
  100. dev[1].ping()
  101. if res['ifname'] in utils.get_ifnames():
  102. raise Exception("Group interface netdev was not removed")
  103. def test_autogo4(dev):
  104. """P2P autonomous GO and client joining group (both with a separate group interface)"""
  105. dev[0].request("SET p2p_no_group_iface 0")
  106. dev[1].request("SET p2p_no_group_iface 0")
  107. res1 = autogo(dev[0], freq=2412)
  108. res2 = connect_cli(dev[0], dev[1], social=True, freq=2412)
  109. if "p2p-wlan" not in res1['ifname']:
  110. raise Exception("Unexpected group interface name on GO")
  111. if "p2p-wlan" not in res2['ifname']:
  112. raise Exception("Unexpected group interface name on client")
  113. ifnames = utils.get_ifnames()
  114. if res1['ifname'] not in ifnames:
  115. raise Exception("Could not find GO group interface netdev")
  116. if res2['ifname'] not in ifnames:
  117. raise Exception("Could not find client group interface netdev")
  118. dev[0].remove_group()
  119. dev[1].wait_go_ending_session()
  120. dev[1].ping()
  121. ifnames = utils.get_ifnames()
  122. if res1['ifname'] in ifnames:
  123. raise Exception("GO group interface netdev was not removed")
  124. if res2['ifname'] in ifnames:
  125. raise Exception("Client group interface netdev was not removed")
  126. def test_autogo_m2d(dev):
  127. """P2P autonomous GO and clients not authorized"""
  128. autogo(dev[0], freq=2412)
  129. go_addr = dev[0].p2p_dev_addr()
  130. dev[1].request("SET p2p_no_group_iface 0")
  131. if not dev[1].discover_peer(go_addr, social=True):
  132. raise Exception("GO " + go_addr + " not found")
  133. dev[1].dump_monitor()
  134. if not dev[2].discover_peer(go_addr, social=True):
  135. raise Exception("GO " + go_addr + " not found")
  136. dev[2].dump_monitor()
  137. logger.info("Trying to join the group when GO has not authorized the client")
  138. pin = dev[1].wps_read_pin()
  139. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  140. if "OK" not in dev[1].global_request(cmd):
  141. raise Exception("P2P_CONNECT join failed")
  142. pin = dev[2].wps_read_pin()
  143. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  144. if "OK" not in dev[2].global_request(cmd):
  145. raise Exception("P2P_CONNECT join failed")
  146. ev = dev[1].wait_global_event(["WPS-M2D"], timeout=16)
  147. if ev is None:
  148. raise Exception("No global M2D event")
  149. ifaces = dev[1].request("INTERFACES").splitlines()
  150. iface = ifaces[0] if "p2p-wlan" in ifaces[0] else ifaces[1]
  151. wpas = WpaSupplicant(ifname=iface)
  152. ev = wpas.wait_event(["WPS-M2D"], timeout=10)
  153. if ev is None:
  154. raise Exception("No M2D event on group interface")
  155. ev = dev[2].wait_global_event(["WPS-M2D"], timeout=10)
  156. if ev is None:
  157. raise Exception("No global M2D event (2)")
  158. ev = dev[2].wait_event(["WPS-M2D"], timeout=10)
  159. if ev is None:
  160. raise Exception("No M2D event on group interface (2)")
  161. def test_autogo_fail(dev):
  162. """P2P autonomous GO and incorrect PIN"""
  163. autogo(dev[0], freq=2412)
  164. go_addr = dev[0].p2p_dev_addr()
  165. dev[0].p2p_go_authorize_client("00000000")
  166. dev[1].request("SET p2p_no_group_iface 0")
  167. if not dev[1].discover_peer(go_addr, social=True):
  168. raise Exception("GO " + go_addr + " not found")
  169. dev[1].dump_monitor()
  170. logger.info("Trying to join the group when GO has not authorized the client")
  171. pin = dev[1].wps_read_pin()
  172. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  173. if "OK" not in dev[1].global_request(cmd):
  174. raise Exception("P2P_CONNECT join failed")
  175. ev = dev[1].wait_global_event(["WPS-FAIL"], timeout=10)
  176. if ev is None:
  177. raise Exception("No global WPS-FAIL event")
  178. def test_autogo_2cli(dev):
  179. """P2P autonomous GO and two clients joining group"""
  180. autogo(dev[0], freq=2412)
  181. connect_cli(dev[0], dev[1], social=True, freq=2412)
  182. connect_cli(dev[0], dev[2], social=True, freq=2412)
  183. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  184. dev[0].global_request("P2P_REMOVE_CLIENT " + dev[1].p2p_dev_addr())
  185. dev[1].wait_go_ending_session()
  186. dev[0].global_request("P2P_REMOVE_CLIENT iface=" + dev[2].p2p_interface_addr())
  187. dev[2].wait_go_ending_session()
  188. if "FAIL" not in dev[0].global_request("P2P_REMOVE_CLIENT foo"):
  189. raise Exception("Invalid P2P_REMOVE_CLIENT command accepted")
  190. dev[0].remove_group()
  191. def test_autogo_pbc(dev):
  192. """P2P autonomous GO and PBC"""
  193. dev[1].global_request("SET p2p_no_group_iface 0")
  194. autogo(dev[0], freq=2412)
  195. if "FAIL" not in dev[0].group_request("WPS_PBC p2p_dev_addr=00:11:22:33:44"):
  196. raise Exception("Invalid WPS_PBC succeeded")
  197. if "OK" not in dev[0].group_request("WPS_PBC p2p_dev_addr=" + dev[1].p2p_dev_addr()):
  198. raise Exception("WPS_PBC failed")
  199. dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=0,
  200. social=True)
  201. ev = dev[2].wait_global_event(["WPS-M2D"], timeout=15)
  202. if ev is None:
  203. raise Exception("WPS-M2D not reported")
  204. if "config_error=12" not in ev:
  205. raise Exception("Unexpected config_error: " + ev)
  206. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=15,
  207. social=True)
  208. def test_autogo_tdls(dev):
  209. """P2P autonomous GO and two clients using TDLS"""
  210. wt = Wlantest()
  211. go = dev[0]
  212. logger.info("Start autonomous GO with fixed parameters " + go.ifname)
  213. id = go.add_network()
  214. go.set_network_quoted(id, "ssid", "DIRECT-tdls")
  215. go.set_network_quoted(id, "psk", "12345678")
  216. go.set_network(id, "mode", "3")
  217. go.set_network(id, "disabled", "2")
  218. res = go.p2p_start_go(persistent=id, freq="2462")
  219. logger.debug("res: " + str(res))
  220. wt.flush()
  221. wt.add_passphrase("12345678")
  222. connect_cli(go, dev[1], social=True, freq=2462)
  223. connect_cli(go, dev[2], social=True, freq=2462)
  224. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  225. bssid = dev[0].p2p_interface_addr()
  226. addr1 = dev[1].p2p_interface_addr()
  227. addr2 = dev[2].p2p_interface_addr()
  228. dev[1].tdls_setup(addr2)
  229. time.sleep(1)
  230. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  231. conf = wt.get_tdls_counter("setup_conf_ok", bssid, addr1, addr2);
  232. if conf == 0:
  233. raise Exception("No TDLS Setup Confirm (success) seen")
  234. dl = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
  235. if dl == 0:
  236. raise Exception("No valid frames through direct link")
  237. wt.tdls_clear(bssid, addr1, addr2);
  238. dev[1].tdls_teardown(addr2)
  239. time.sleep(1)
  240. teardown = wt.get_tdls_counter("teardown", bssid, addr1, addr2);
  241. if teardown == 0:
  242. raise Exception("No TDLS Setup Teardown seen")
  243. wt.tdls_clear(bssid, addr1, addr2);
  244. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  245. ap_path = wt.get_tdls_counter("valid_ap_path", bssid, addr1, addr2);
  246. if ap_path == 0:
  247. raise Exception("No valid frames via AP path")
  248. direct_link = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
  249. if direct_link > 0:
  250. raise Exception("Unexpected frames through direct link")
  251. idirect_link = wt.get_tdls_counter("invalid_direct_link", bssid, addr1,
  252. addr2);
  253. if idirect_link > 0:
  254. raise Exception("Unexpected frames through direct link (invalid)")
  255. dev[2].remove_group()
  256. dev[1].remove_group()
  257. dev[0].remove_group()
  258. def test_autogo_legacy(dev):
  259. """P2P autonomous GO and legacy clients"""
  260. res = autogo(dev[0], freq=2462)
  261. if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
  262. raise Exception("passphrase mismatch")
  263. if dev[0].group_request("P2P_GET_PASSPHRASE") != res['passphrase']:
  264. raise Exception("passphrase mismatch(2)")
  265. logger.info("Connect P2P client")
  266. connect_cli(dev[0], dev[1], social=True, freq=2462)
  267. if "FAIL" not in dev[1].request("P2P_GET_PASSPHRASE"):
  268. raise Exception("P2P_GET_PASSPHRASE succeeded on P2P Client")
  269. logger.info("Connect legacy WPS client")
  270. pin = dev[2].wps_read_pin()
  271. dev[0].p2p_go_authorize_client(pin)
  272. dev[2].request("P2P_SET disabled 1")
  273. dev[2].dump_monitor()
  274. dev[2].request("WPS_PIN any " + pin)
  275. dev[2].wait_connected(timeout=30)
  276. status = dev[2].get_status()
  277. if status['wpa_state'] != 'COMPLETED':
  278. raise Exception("Not fully connected")
  279. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  280. dev[2].request("DISCONNECT")
  281. logger.info("Connect legacy non-WPS client")
  282. dev[2].request("FLUSH")
  283. dev[2].request("P2P_SET disabled 1")
  284. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  285. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  286. scan_freq=res['freq'])
  287. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  288. dev[2].request("DISCONNECT")
  289. dev[0].remove_group()
  290. dev[1].wait_go_ending_session()
  291. def test_autogo_chan_switch(dev):
  292. """P2P autonomous GO switching channels"""
  293. autogo(dev[0], freq=2417)
  294. connect_cli(dev[0], dev[1])
  295. res = dev[0].request("CHAN_SWITCH 5 2422")
  296. if "FAIL" in res:
  297. # for now, skip test since mac80211_hwsim support is not yet widely
  298. # deployed
  299. raise HwsimSkip("Assume mac80211_hwsim did not support channel switching")
  300. ev = dev[0].wait_event(["AP-CSA-FINISHED"], timeout=10)
  301. if ev is None:
  302. raise Exception("CSA finished event timed out")
  303. if "freq=2422" not in ev:
  304. raise Exception("Unexpected cahnnel in CSA finished event")
  305. dev[0].dump_monitor()
  306. dev[1].dump_monitor()
  307. time.sleep(0.1)
  308. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  309. def test_autogo_extra_cred(dev):
  310. """P2P autonomous GO sending two WPS credentials"""
  311. if "FAIL" in dev[0].request("SET wps_testing_dummy_cred 1"):
  312. raise Exception("Failed to enable test mode")
  313. autogo(dev[0], freq=2412)
  314. connect_cli(dev[0], dev[1], social=True, freq=2412)
  315. dev[0].remove_group()
  316. dev[1].wait_go_ending_session()
  317. def test_autogo_ifdown(dev):
  318. """P2P autonomous GO and external ifdown"""
  319. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  320. wpas.interface_add("wlan5")
  321. res = autogo(wpas)
  322. wpas.dump_monitor()
  323. wpas.interface_remove("wlan5")
  324. wpas.interface_add("wlan5")
  325. res = autogo(wpas)
  326. wpas.dump_monitor()
  327. subprocess.call(['ifconfig', res['ifname'], 'down'])
  328. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  329. if ev is None:
  330. raise Exception("Group removal not reported")
  331. if res['ifname'] not in ev:
  332. raise Exception("Unexpected group removal event: " + ev)
  333. def test_autogo_start_during_scan(dev):
  334. """P2P autonomous GO started during ongoing manual scan"""
  335. try:
  336. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  337. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  338. raise Exception("Failed to set autoscan")
  339. autogo(dev[0], freq=2462)
  340. connect_cli(dev[0], dev[1], social=True, freq=2462)
  341. dev[0].remove_group()
  342. dev[1].wait_go_ending_session()
  343. finally:
  344. dev[0].request("AUTOSCAN ")
  345. def test_autogo_passphrase_len(dev):
  346. """P2P autonomous GO and longer passphrase"""
  347. try:
  348. if "OK" not in dev[0].request("SET p2p_passphrase_len 13"):
  349. raise Exception("Failed to set passphrase length")
  350. res = autogo(dev[0], freq=2412)
  351. if len(res['passphrase']) != 13:
  352. raise Exception("Unexpected passphrase length")
  353. if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
  354. raise Exception("passphrase mismatch")
  355. logger.info("Connect P2P client")
  356. connect_cli(dev[0], dev[1], social=True, freq=2412)
  357. logger.info("Connect legacy WPS client")
  358. pin = dev[2].wps_read_pin()
  359. dev[0].p2p_go_authorize_client(pin)
  360. dev[2].request("P2P_SET disabled 1")
  361. dev[2].dump_monitor()
  362. dev[2].request("WPS_PIN any " + pin)
  363. dev[2].wait_connected(timeout=30)
  364. status = dev[2].get_status()
  365. if status['wpa_state'] != 'COMPLETED':
  366. raise Exception("Not fully connected")
  367. dev[2].request("DISCONNECT")
  368. logger.info("Connect legacy non-WPS client")
  369. dev[2].request("FLUSH")
  370. dev[2].request("P2P_SET disabled 1")
  371. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  372. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  373. scan_freq=res['freq'])
  374. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  375. dev[2].request("DISCONNECT")
  376. dev[0].remove_group()
  377. dev[1].wait_go_ending_session()
  378. finally:
  379. dev[0].request("SET p2p_passphrase_len 8")
  380. def test_autogo_bridge(dev):
  381. """P2P autonomous GO in a bridge"""
  382. try:
  383. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  384. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  385. raise Exception("Failed to set autoscan")
  386. autogo(dev[0])
  387. ifname = dev[0].get_group_ifname()
  388. subprocess.call(['brctl', 'addbr', 'p2p-br0'])
  389. subprocess.call(['brctl', 'setfd', 'p2p-br0', '0'])
  390. subprocess.call(['brctl', 'addif', 'p2p-br0', ifname])
  391. subprocess.call(['ip', 'link', 'set', 'dev', 'p2p-br0', 'up'])
  392. time.sleep(0.1)
  393. subprocess.call(['brctl', 'delif', 'p2p-br0', ifname])
  394. time.sleep(0.1)
  395. subprocess.call(['ip', 'link', 'set', 'dev', 'p2p-br0', 'down'])
  396. time.sleep(0.1)
  397. subprocess.call(['brctl', 'delbr', 'p2p-br0'])
  398. ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=1)
  399. if ev is not None:
  400. raise Exception("P2P group removed unexpectedly")
  401. if dev[0].get_group_status_field('wpa_state') != "COMPLETED":
  402. raise Exception("Unexpected wpa_state")
  403. dev[0].remove_group()
  404. finally:
  405. dev[0].request("AUTOSCAN ")
  406. subprocess.Popen(['brctl', 'delif', 'p2p-br0', ifname],
  407. stderr=open('/dev/null', 'w'))
  408. subprocess.Popen(['ip', 'link', 'set', 'dev', 'p2p-br0', 'down'],
  409. stderr=open('/dev/null', 'w'))
  410. subprocess.Popen(['brctl', 'delbr', 'p2p-br0'],
  411. stderr=open('/dev/null', 'w'))
  412. def test_presence_req_on_group_interface(dev):
  413. """P2P_PRESENCE_REQ on group interface"""
  414. dev[1].request("SET p2p_no_group_iface 0")
  415. res = autogo(dev[0], freq=2437)
  416. res = connect_cli(dev[0], dev[1], social=True, freq=2437)
  417. if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400"):
  418. raise Exception("Could not send presence request")
  419. ev = dev[1].wait_group_event(["P2P-PRESENCE-RESPONSE"])
  420. if ev is None:
  421. raise Exception("Timeout while waiting for Presence Response")
  422. dev[0].remove_group()
  423. dev[1].wait_go_ending_session()
  424. def test_autogo_join_auto_go_not_found(dev):
  425. """P2P_CONNECT-auto not finding GO"""
  426. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  427. wpas.interface_add("wlan5")
  428. wpas.request("P2P_SET listen_channel 1")
  429. wpas.global_request("SET p2p_no_group_iface 0")
  430. autogo(wpas, freq=2412)
  431. addr = wpas.p2p_dev_addr()
  432. bssid = wpas.p2p_interface_addr()
  433. wpas.dump_monitor()
  434. dev[1].global_request("SET p2p_no_group_iface 0")
  435. dev[1].scan_for_bss(bssid, freq=2412)
  436. # This makes the GO not show up in the scan iteration following the
  437. # P2P_CONNECT command by stopping beaconing and handling Probe Request
  438. # frames externally (but not really replying to them). P2P listen mode is
  439. # needed to keep the GO listening on the operating channel for the PD
  440. # exchange.
  441. if "OK" not in wpas.group_request("STOP_AP"):
  442. raise Exception("STOP_AP failed")
  443. wpas.dump_monitor()
  444. wpas.group_request("SET ext_mgmt_frame_handling 1")
  445. wpas.p2p_listen()
  446. wpas.dump_monitor()
  447. time.sleep(0.02)
  448. dev[1].global_request("P2P_CONNECT " + addr + " pbc auto")
  449. ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG-ENABLED"], 15)
  450. wpas.dump_monitor()
  451. if ev is None:
  452. raise Exception("Could not trigger old-scan-only case")
  453. return
  454. ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], 15)
  455. wpas.remove_group()
  456. if ev is None:
  457. raise Exception("Fallback to GO Negotiation not seen")
  458. if "reason=GO-not-found" not in ev:
  459. raise Exception("Unexpected reason for fallback: " + ev)
  460. wpas.dump_monitor()
  461. def test_autogo_join_auto(dev):
  462. """P2P_CONNECT-auto joining a group"""
  463. autogo(dev[0])
  464. addr = dev[0].p2p_dev_addr()
  465. if "OK" not in dev[1].global_request("P2P_CONNECT " + addr + " pbc auto"):
  466. raise Exception("P2P_CONNECT failed")
  467. ev = dev[0].wait_global_event(["P2P-PROV-DISC-PBC-REQ"], timeout=15)
  468. if ev is None:
  469. raise Exception("Timeout on P2P-PROV-DISC-PBC-REQ")
  470. if "group=" + dev[0].group_ifname not in ev:
  471. raise Exception("Unexpected PD event contents: " + ev)
  472. dev[0].group_request("WPS_PBC")
  473. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  474. if ev is None:
  475. raise Exception("Joining the group timed out")
  476. dev[1].group_form_result(ev)
  477. dev[0].remove_group()
  478. dev[1].wait_go_ending_session()
  479. dev[1].flush_scan_cache()
  480. def test_autogo_join_auto_go_neg(dev):
  481. """P2P_CONNECT-auto fallback to GO Neg"""
  482. dev[1].flush_scan_cache()
  483. dev[0].p2p_listen()
  484. addr = dev[0].p2p_dev_addr()
  485. if "OK" not in dev[1].global_request("P2P_CONNECT " + addr + " pbc auto"):
  486. raise Exception("P2P_CONNECT failed")
  487. ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  488. if ev is None:
  489. raise Exception("Timeout on P2P-GO-NEG-REQUEST")
  490. peer = ev.split(' ')[1]
  491. dev[0].p2p_go_neg_init(peer, None, "pbc", timeout=15, go_intent=15)
  492. ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], timeout=1)
  493. if ev is None:
  494. raise Exception("No P2P-FALLBACK-TO-GO-NEG event seen")
  495. if "P2P-FALLBACK-TO-GO-NEG-ENABLED" in ev:
  496. ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], timeout=1)
  497. if ev is None:
  498. raise Exception("No P2P-FALLBACK-TO-GO-NEG event seen")
  499. if "reason=peer-not-running-GO" not in ev:
  500. raise Exception("Unexpected reason: " + ev)
  501. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  502. if ev is None:
  503. raise Exception("Joining the group timed out")
  504. dev[1].group_form_result(ev)
  505. dev[0].remove_group()
  506. dev[1].wait_go_ending_session()
  507. dev[1].flush_scan_cache()
  508. def test_autogo_join_auto_go_neg_after_seeing_go(dev):
  509. """P2P_CONNECT-auto fallback to GO Neg after seeing GO"""
  510. autogo(dev[0], freq=2412)
  511. addr = dev[0].p2p_dev_addr()
  512. bssid = dev[0].p2p_interface_addr()
  513. dev[1].scan_for_bss(bssid, freq=2412)
  514. dev[0].remove_group()
  515. dev[0].p2p_listen()
  516. if "OK" not in dev[1].global_request("P2P_CONNECT " + addr + " pbc auto"):
  517. raise Exception("P2P_CONNECT failed")
  518. ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG-ENABLED"],
  519. timeout=15)
  520. if ev is None:
  521. raise Exception("No P2P-FALLBACK-TO-GO-NEG-ENABLED event seen")
  522. ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  523. if ev is None:
  524. raise Exception("Timeout on P2P-GO-NEG-REQUEST")
  525. peer = ev.split(' ')[1]
  526. dev[0].p2p_go_neg_init(peer, None, "pbc", timeout=15, go_intent=15)
  527. ev = dev[1].wait_global_event(["P2P-FALLBACK-TO-GO-NEG"], timeout=1)
  528. if ev is None:
  529. raise Exception("No P2P-FALLBACK-TO-GO-NEG event seen")
  530. if "reason=no-ACK-to-PD-Req" not in ev and "reason=PD-failed" not in ev:
  531. raise Exception("Unexpected reason: " + ev)
  532. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  533. if ev is None:
  534. raise Exception("Joining the group timed out")
  535. dev[1].group_form_result(ev)
  536. dev[0].remove_group()
  537. dev[1].wait_go_ending_session()
  538. dev[1].flush_scan_cache()
  539. def test_go_search_non_social(dev):
  540. """P2P_FIND with freq parameter to scan a single channel"""
  541. addr0 = dev[0].p2p_dev_addr()
  542. autogo(dev[0], freq=2422)
  543. dev[1].p2p_find(freq=2422)
  544. ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=3.5)
  545. if ev is None:
  546. raise Exception("Did not find GO quickly enough")
  547. dev[2].p2p_listen()
  548. ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
  549. if ev is None:
  550. raise Exception("Did not find peer")
  551. dev[2].p2p_stop_find()
  552. dev[1].p2p_stop_find()
  553. dev[0].remove_group()
  554. def test_autogo_many(dev):
  555. """P2P autonomous GO with large number of GO instances"""
  556. dev[0].request("SET p2p_no_group_iface 0")
  557. for i in range(100):
  558. if "OK" not in dev[0].global_request("P2P_GROUP_ADD freq=2412"):
  559. logger.info("Was able to add %d groups" % i)
  560. if i < 5:
  561. raise Exception("P2P_GROUP_ADD failed")
  562. stop_ev = dev[0].wait_global_event(["P2P-GROUP-REMOVE"], timeout=1)
  563. if stop_ev is not None:
  564. raise Exception("Unexpected P2P-GROUP-REMOVE event")
  565. break
  566. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
  567. if ev is None:
  568. raise Exception("GO start up timed out")
  569. dev[0].group_form_result(ev)
  570. for i in dev[0].global_request("INTERFACES").splitlines():
  571. dev[0].request("P2P_GROUP_REMOVE " + i)
  572. dev[0].dump_monitor()
  573. dev[0].request("P2P_GROUP_REMOVE *")
  574. def test_autogo_many_clients(dev):
  575. """P2P autonomous GO and many clients (P2P IE fragmentation)"""
  576. try:
  577. _test_autogo_many_clients(dev)
  578. finally:
  579. dev[0].global_request("SET device_name Device A")
  580. dev[1].global_request("SET device_name Device B")
  581. dev[2].global_request("SET device_name Device C")
  582. def _test_autogo_many_clients(dev):
  583. # These long device names will push the P2P IE contents beyond the limit
  584. # that requires fragmentation.
  585. name0 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  586. name1 = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  587. name2 = "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  588. name3 = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  589. dev[0].global_request("SET device_name " + name0)
  590. dev[1].global_request("SET device_name " + name1)
  591. dev[2].global_request("SET device_name " + name2)
  592. addr0 = dev[0].p2p_dev_addr()
  593. res = autogo(dev[0], freq=2412)
  594. bssid = dev[0].p2p_interface_addr()
  595. connect_cli(dev[0], dev[1], social=True, freq=2412)
  596. dev[0].dump_monitor()
  597. connect_cli(dev[0], dev[2], social=True, freq=2412)
  598. dev[0].dump_monitor()
  599. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  600. wpas.interface_add("wlan5")
  601. wpas.global_request("SET device_name " + name3)
  602. wpas.global_request("SET sec_device_type 1-11111111-1")
  603. wpas.global_request("SET sec_device_type 2-22222222-2")
  604. wpas.global_request("SET sec_device_type 3-33333333-3")
  605. wpas.global_request("SET sec_device_type 4-44444444-4")
  606. wpas.global_request("SET sec_device_type 5-55555555-5")
  607. connect_cli(dev[0], wpas, social=True, freq=2412)
  608. dev[0].dump_monitor()
  609. dev[1].dump_monitor()
  610. dev[1].p2p_find(freq=2412)
  611. ev1 = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
  612. if ev1 is None:
  613. raise Exception("Could not find peer (1)")
  614. ev2 = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
  615. if ev2 is None:
  616. raise Exception("Could not find peer (2)")
  617. ev3 = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
  618. if ev3 is None:
  619. raise Exception("Could not find peer (3)")
  620. dev[1].p2p_stop_find()
  621. for i in [ name0, name2, name3 ]:
  622. if i not in ev1 and i not in ev2 and i not in ev3:
  623. raise Exception('name "%s" not found' % i)
  624. def rx_pd_req(dev):
  625. msg = dev.mgmt_rx()
  626. if msg is None:
  627. raise Exception("MGMT-RX timeout")
  628. p2p = parse_p2p_public_action(msg['payload'])
  629. if p2p is None:
  630. raise Exception("Not a P2P Public Action frame " + str(dialog_token))
  631. if p2p['subtype'] != P2P_PROV_DISC_REQ:
  632. raise Exception("Unexpected subtype %d" % p2p['subtype'])
  633. p2p['freq'] = msg['freq']
  634. return p2p
  635. def test_autogo_scan(dev):
  636. """P2P autonomous GO and no P2P IE in Probe Response scan results"""
  637. addr0 = dev[0].p2p_dev_addr()
  638. addr1 = dev[1].p2p_dev_addr()
  639. dev[0].p2p_start_go(freq=2412, persistent=True)
  640. bssid = dev[0].p2p_interface_addr()
  641. dev[1].discover_peer(addr0)
  642. dev[1].p2p_stop_find()
  643. ev = dev[1].wait_global_event(["P2P-FIND-STOPPED"], timeout=2)
  644. time.sleep(0.1)
  645. dev[1].flush_scan_cache()
  646. pin = dev[1].wps_read_pin()
  647. dev[0].group_request("WPS_PIN any " + pin)
  648. try:
  649. dev[1].request("SET p2p_disabled 1")
  650. dev[1].request("SCAN freq=2412")
  651. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  652. if ev is None:
  653. raise Exception("Active scan did not complete")
  654. finally:
  655. dev[1].request("SET p2p_disabled 0")
  656. for i in range(2):
  657. dev[1].request("SCAN freq=2412 passive=1")
  658. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  659. if ev is None:
  660. raise Exception("Scan did not complete")
  661. # Disable management frame processing for a moment to skip Probe Response
  662. # frame with P2P IE.
  663. dev[0].group_request("SET ext_mgmt_frame_handling 1")
  664. dev[1].request("P2P_CONNECT " + bssid + " " + pin + " freq=2412 join")
  665. # Skip the first Probe Request frame
  666. ev = dev[0].wait_group_event(["MGMT-RX"], timeout=10)
  667. if ev is None:
  668. raise Exception("No Probe Request frame seen")
  669. if not ev.split(' ')[4].startswith("40"):
  670. raise Exception("Not a Probe Request frame")
  671. # Reply to PD Request while still filtering Probe Request frames
  672. msg = rx_pd_req(dev[0])
  673. mgmt_tx(dev[0], "MGMT_TX {} {} freq={} wait_time=10 no_cck=1 action={}".format(addr1, addr0, 2412, "0409506f9a0908%02xdd0a0050f204100800020008" % msg['dialog_token']))
  674. # Skip Probe Request frames until something else is received
  675. for i in range(10):
  676. ev = dev[0].wait_group_event(["MGMT-RX"], timeout=10)
  677. if ev is None:
  678. raise Exception("No frame seen")
  679. if not ev.split(' ')[4].startswith("40"):
  680. break
  681. # Allow wpa_supplicant to process authentication and association
  682. dev[0].group_request("SET ext_mgmt_frame_handling 0")
  683. # Joining the group should succeed and indicate persistent group based on
  684. # Beacon frame P2P IE.
  685. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=10)
  686. if ev is None:
  687. raise Exception("Failed to join group")
  688. if "[PERSISTENT]" not in ev:
  689. raise Exception("Did not recognize group as persistent")
  690. dev[0].remove_group()
  691. dev[1].wait_go_ending_session()
  692. def test_autogo_join_before_found(dev):
  693. """P2P client joining a group before having found GO Device Address"""
  694. dev[0].request("SET p2p_no_group_iface 0")
  695. res = autogo(dev[0], freq=2412)
  696. if "p2p-wlan" not in res['ifname']:
  697. raise Exception("Unexpected group interface name on GO")
  698. status = dev[0].get_group_status()
  699. bssid = status['bssid']
  700. pin = dev[1].wps_read_pin()
  701. dev[0].p2p_go_authorize_client(pin)
  702. cmd = "P2P_CONNECT " + bssid + " " + pin + " join freq=2412"
  703. if "OK" not in dev[1].global_request(cmd):
  704. raise Exception("P2P_CONNECT join failed")
  705. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  706. if ev is None:
  707. raise Exception("Joining the group timed out")
  708. dev[0].remove_group()
  709. dev[1].wait_go_ending_session()