test_p2p_autogo.py 32 KB

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