test_p2p_grpform.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. # P2P group formation 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 logging
  7. logger = logging.getLogger()
  8. import time
  9. import threading
  10. import Queue
  11. import os
  12. import hostapd
  13. import hwsim_utils
  14. import utils
  15. def check_grpform_results(i_res, r_res):
  16. if i_res['result'] != 'success' or r_res['result'] != 'success':
  17. raise Exception("Failed group formation")
  18. if i_res['ssid'] != r_res['ssid']:
  19. raise Exception("SSID mismatch")
  20. if i_res['freq'] != r_res['freq']:
  21. raise Exception("freq mismatch")
  22. if 'go_neg_freq' in r_res and i_res['go_neg_freq'] != r_res['go_neg_freq']:
  23. raise Exception("go_neg_freq mismatch")
  24. if i_res['freq'] != i_res['go_neg_freq']:
  25. raise Exception("freq/go_neg_freq mismatch")
  26. if i_res['role'] != i_res['go_neg_role']:
  27. raise Exception("role/go_neg_role mismatch")
  28. if 'go_neg_role' in r_res and r_res['role'] != r_res['go_neg_role']:
  29. raise Exception("role/go_neg_role mismatch")
  30. if i_res['go_dev_addr'] != r_res['go_dev_addr']:
  31. raise Exception("GO Device Address mismatch")
  32. def go_neg_init(i_dev, r_dev, pin, i_method, i_intent, res):
  33. logger.debug("Initiate GO Negotiation from i_dev")
  34. try:
  35. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent)
  36. logger.debug("i_res: " + str(i_res))
  37. except Exception, e:
  38. i_res = None
  39. logger.info("go_neg_init thread caught an exception from p2p_go_neg_init: " + str(e))
  40. res.put(i_res)
  41. def go_neg_pin(i_dev, r_dev, i_intent=None, r_intent=None, i_method='enter', r_method='display'):
  42. r_dev.p2p_listen()
  43. i_dev.p2p_listen()
  44. pin = r_dev.wps_read_pin()
  45. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  46. r_dev.dump_monitor()
  47. res = Queue.Queue()
  48. t = threading.Thread(target=go_neg_init, args=(i_dev, r_dev, pin, i_method, i_intent, res))
  49. t.start()
  50. logger.debug("Wait for GO Negotiation Request on r_dev")
  51. ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  52. if ev is None:
  53. raise Exception("GO Negotiation timed out")
  54. r_dev.dump_monitor()
  55. logger.debug("Re-initiate GO Negotiation from r_dev")
  56. r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, timeout=20)
  57. logger.debug("r_res: " + str(r_res))
  58. r_dev.dump_monitor()
  59. t.join()
  60. i_res = res.get()
  61. if i_res is None:
  62. raise Exception("go_neg_init thread failed")
  63. logger.debug("i_res: " + str(i_res))
  64. logger.info("Group formed")
  65. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  66. i_dev.dump_monitor()
  67. return [i_res, r_res]
  68. def go_neg_pin_authorized(i_dev, r_dev, i_intent=None, r_intent=None, expect_failure=False, i_go_neg_status=None, i_method='enter', r_method='display', test_data=True, i_freq=None, r_freq=None):
  69. i_dev.p2p_listen()
  70. pin = r_dev.wps_read_pin()
  71. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  72. r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, freq=r_freq)
  73. r_dev.p2p_listen()
  74. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent, expect_failure=expect_failure, freq=i_freq)
  75. r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
  76. logger.debug("i_res: " + str(i_res))
  77. logger.debug("r_res: " + str(r_res))
  78. r_dev.dump_monitor()
  79. i_dev.dump_monitor()
  80. if i_go_neg_status:
  81. if i_res['result'] != 'go-neg-failed':
  82. raise Exception("Expected GO Negotiation failure not reported")
  83. if i_res['status'] != i_go_neg_status:
  84. raise Exception("Expected GO Negotiation status not seen")
  85. if expect_failure:
  86. return
  87. logger.info("Group formed")
  88. if test_data:
  89. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  90. return [i_res, r_res]
  91. def go_neg_init_pbc(i_dev, r_dev, i_intent, res, freq, provdisc):
  92. logger.debug("Initiate GO Negotiation from i_dev")
  93. try:
  94. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc",
  95. timeout=20, go_intent=i_intent, freq=freq,
  96. provdisc=provdisc)
  97. logger.debug("i_res: " + str(i_res))
  98. except Exception, e:
  99. i_res = None
  100. logger.info("go_neg_init_pbc thread caught an exception from p2p_go_neg_init: " + str(e))
  101. res.put(i_res)
  102. def go_neg_pbc(i_dev, r_dev, i_intent=None, r_intent=None, i_freq=None, r_freq=None, provdisc=False, r_listen=False):
  103. if r_listen:
  104. r_dev.p2p_listen()
  105. else:
  106. r_dev.p2p_find(social=True)
  107. i_dev.p2p_find(social=True)
  108. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  109. r_dev.dump_monitor()
  110. res = Queue.Queue()
  111. t = threading.Thread(target=go_neg_init_pbc, args=(i_dev, r_dev, i_intent, res, i_freq, provdisc))
  112. t.start()
  113. logger.debug("Wait for GO Negotiation Request on r_dev")
  114. ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  115. if ev is None:
  116. raise Exception("GO Negotiation timed out")
  117. r_dev.dump_monitor()
  118. logger.debug("Re-initiate GO Negotiation from r_dev")
  119. r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), None, "pbc",
  120. go_intent=r_intent, timeout=20, freq=r_freq)
  121. logger.debug("r_res: " + str(r_res))
  122. r_dev.dump_monitor()
  123. t.join()
  124. i_res = res.get()
  125. if i_res is None:
  126. raise Exception("go_neg_init_pbc thread failed")
  127. logger.debug("i_res: " + str(i_res))
  128. logger.info("Group formed")
  129. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  130. i_dev.dump_monitor()
  131. return [i_res, r_res]
  132. def remove_group(dev1, dev2):
  133. dev1.remove_group()
  134. try:
  135. dev2.remove_group()
  136. except:
  137. pass
  138. def test_grpform(dev):
  139. """P2P group formation using PIN and authorized connection (init -> GO)"""
  140. try:
  141. dev[0].request("SET p2p_group_idle 2")
  142. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  143. r_dev=dev[1], r_intent=0)
  144. check_grpform_results(i_res, r_res)
  145. dev[1].remove_group()
  146. ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  147. if ev is None:
  148. raise Exception("GO did not remove group on idle timeout")
  149. if "GO reason=IDLE" not in ev:
  150. raise Exception("Unexpected group removal event: " + ev)
  151. finally:
  152. dev[0].request("SET p2p_group_idle 0")
  153. def test_grpform_a(dev):
  154. """P2P group formation using PIN and authorized connection (init -> GO) (init: group iface)"""
  155. dev[0].request("SET p2p_no_group_iface 0")
  156. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  157. r_dev=dev[1], r_intent=0)
  158. if "p2p-wlan" not in i_res['ifname']:
  159. raise Exception("Unexpected group interface name")
  160. check_grpform_results(i_res, r_res)
  161. remove_group(dev[0], dev[1])
  162. if i_res['ifname'] in utils.get_ifnames():
  163. raise Exception("Group interface netdev was not removed")
  164. def test_grpform_b(dev):
  165. """P2P group formation using PIN and authorized connection (init -> GO) (resp: group iface)"""
  166. dev[1].request("SET p2p_no_group_iface 0")
  167. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  168. r_dev=dev[1], r_intent=0)
  169. if "p2p-wlan" not in r_res['ifname']:
  170. raise Exception("Unexpected group interface name")
  171. check_grpform_results(i_res, r_res)
  172. remove_group(dev[0], dev[1])
  173. if r_res['ifname'] in utils.get_ifnames():
  174. raise Exception("Group interface netdev was not removed")
  175. def test_grpform_c(dev):
  176. """P2P group formation using PIN and authorized connection (init -> GO) (group iface)"""
  177. dev[0].request("SET p2p_no_group_iface 0")
  178. dev[1].request("SET p2p_no_group_iface 0")
  179. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  180. r_dev=dev[1], r_intent=0)
  181. if "p2p-wlan" not in i_res['ifname']:
  182. raise Exception("Unexpected group interface name")
  183. if "p2p-wlan" not in r_res['ifname']:
  184. raise Exception("Unexpected group interface name")
  185. check_grpform_results(i_res, r_res)
  186. remove_group(dev[0], dev[1])
  187. if i_res['ifname'] in utils.get_ifnames():
  188. raise Exception("Group interface netdev was not removed")
  189. if r_res['ifname'] in utils.get_ifnames():
  190. raise Exception("Group interface netdev was not removed")
  191. def test_grpform2(dev):
  192. """P2P group formation using PIN and authorized connection (resp -> GO)"""
  193. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
  194. remove_group(dev[0], dev[1])
  195. def test_grpform2_c(dev):
  196. """P2P group formation using PIN and authorized connection (resp -> GO) (group iface)"""
  197. dev[0].request("SET p2p_no_group_iface 0")
  198. dev[1].request("SET p2p_no_group_iface 0")
  199. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
  200. remove_group(dev[0], dev[1])
  201. if i_res['ifname'] in utils.get_ifnames():
  202. raise Exception("Group interface netdev was not removed")
  203. if r_res['ifname'] in utils.get_ifnames():
  204. raise Exception("Group interface netdev was not removed")
  205. def test_grpform3(dev):
  206. """P2P group formation using PIN and re-init GO Negotiation"""
  207. go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  208. remove_group(dev[0], dev[1])
  209. def test_grpform3_c(dev):
  210. """P2P group formation using PIN and re-init GO Negotiation (group iface)"""
  211. dev[0].request("SET p2p_no_group_iface 0")
  212. dev[1].request("SET p2p_no_group_iface 0")
  213. [i_res, r_res] = go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  214. remove_group(dev[0], dev[1])
  215. if i_res['ifname'] in utils.get_ifnames():
  216. raise Exception("Group interface netdev was not removed")
  217. if r_res['ifname'] in utils.get_ifnames():
  218. raise Exception("Group interface netdev was not removed")
  219. def test_grpform_pbc(dev):
  220. """P2P group formation using PBC and re-init GO Negotiation"""
  221. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  222. check_grpform_results(i_res, r_res)
  223. if i_res['role'] != 'GO' or r_res['role'] != 'client':
  224. raise Exception("Unexpected device roles")
  225. remove_group(dev[0], dev[1])
  226. def test_grpform_pd(dev):
  227. """P2P group formation with PD-before-GO-Neg workaround"""
  228. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], provdisc=True, r_dev=dev[1], r_listen=True)
  229. check_grpform_results(i_res, r_res)
  230. remove_group(dev[0], dev[1])
  231. def test_grpform_ext_listen(dev):
  232. """P2P group formation with extended listen timing enabled"""
  233. try:
  234. if "OK" not in dev[0].global_request("P2P_EXT_LISTEN 100 50000"):
  235. raise Exception("Failed to set extended listen timing")
  236. if "OK" not in dev[1].global_request("P2P_EXT_LISTEN 200 40000"):
  237. raise Exception("Failed to set extended listen timing")
  238. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], provdisc=True, r_dev=dev[1], r_listen=True)
  239. check_grpform_results(i_res, r_res)
  240. peer1 = dev[0].get_peer(dev[1].p2p_dev_addr())
  241. if peer1['ext_listen_interval'] != "40000":
  242. raise Exception("Extended listen interval not discovered correctly")
  243. if peer1['ext_listen_period'] != "200":
  244. raise Exception("Extended listen period not discovered correctly")
  245. peer0 = dev[1].get_peer(dev[0].p2p_dev_addr())
  246. if peer0['ext_listen_interval'] != "50000":
  247. raise Exception("Extended listen interval not discovered correctly")
  248. if peer0['ext_listen_period'] != "100":
  249. raise Exception("Extended listen period not discovered correctly")
  250. remove_group(dev[0], dev[1])
  251. finally:
  252. if "OK" not in dev[0].global_request("P2P_EXT_LISTEN"):
  253. raise Exception("Failed to clear extended listen timing")
  254. if "OK" not in dev[1].global_request("P2P_EXT_LISTEN"):
  255. raise Exception("Failed to clear extended listen timing")
  256. def test_both_go_intent_15(dev):
  257. """P2P GO Negotiation with both devices using GO intent 15"""
  258. go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=15, expect_failure=True, i_go_neg_status=9)
  259. def test_both_go_neg_display(dev):
  260. """P2P GO Negotiation with both devices trying to display PIN"""
  261. go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='display', r_method='display')
  262. def test_both_go_neg_enter(dev):
  263. """P2P GO Negotiation with both devices trying to enter PIN"""
  264. go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='enter', r_method='enter')
  265. def test_go_neg_pbc_vs_pin(dev):
  266. """P2P GO Negotiation with one device using PBC and the other PIN"""
  267. addr0 = dev[0].p2p_dev_addr()
  268. addr1 = dev[1].p2p_dev_addr()
  269. dev[1].p2p_listen()
  270. if not dev[0].discover_peer(addr1):
  271. raise Exception("Could not discover peer")
  272. dev[0].p2p_listen()
  273. if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc auth"):
  274. raise Exception("Failed to authorize GO Neg")
  275. if not dev[1].discover_peer(addr0):
  276. raise Exception("Could not discover peer")
  277. if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " 12345670 display"):
  278. raise Exception("Failed to initiate GO Neg")
  279. ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
  280. if ev is None:
  281. raise Exception("GO Negotiation failure timed out")
  282. if "status=10" not in ev:
  283. raise Exception("Unexpected failure reason: " + ev)
  284. def test_go_neg_pin_vs_pbc(dev):
  285. """P2P GO Negotiation with one device using PIN and the other PBC"""
  286. addr0 = dev[0].p2p_dev_addr()
  287. addr1 = dev[1].p2p_dev_addr()
  288. dev[1].p2p_listen()
  289. if not dev[0].discover_peer(addr1):
  290. raise Exception("Could not discover peer")
  291. dev[0].p2p_listen()
  292. if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " 12345670 display auth"):
  293. raise Exception("Failed to authorize GO Neg")
  294. if not dev[1].discover_peer(addr0):
  295. raise Exception("Could not discover peer")
  296. if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " pbc"):
  297. raise Exception("Failed to initiate GO Neg")
  298. ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
  299. if ev is None:
  300. raise Exception("GO Negotiation failure timed out")
  301. if "status=10" not in ev:
  302. raise Exception("Unexpected failure reason: " + ev)
  303. def test_grpform_per_sta_psk(dev):
  304. """P2P group formation with per-STA PSKs"""
  305. dev[0].request("P2P_SET per_sta_psk 1")
  306. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  307. check_grpform_results(i_res, r_res)
  308. pin = dev[2].wps_read_pin()
  309. dev[0].p2p_go_authorize_client(pin)
  310. c_res = dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  311. check_grpform_results(i_res, c_res)
  312. if r_res['psk'] == c_res['psk']:
  313. raise Exception("Same PSK assigned for both clients")
  314. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  315. dev[0].remove_group()
  316. dev[1].wait_go_ending_session()
  317. dev[2].wait_go_ending_session()
  318. def test_grpform_per_sta_psk_wps(dev):
  319. """P2P group formation with per-STA PSKs with non-P2P WPS STA"""
  320. dev[0].request("P2P_SET per_sta_psk 1")
  321. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  322. check_grpform_results(i_res, r_res)
  323. dev[0].p2p_go_authorize_client_pbc()
  324. dev[2].request("WPS_PBC")
  325. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  326. if ev is None:
  327. raise Exception("Association with the GO timed out")
  328. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  329. dev[0].remove_group()
  330. dev[2].request("DISCONNECT")
  331. dev[1].wait_go_ending_session()
  332. def test_grpform_force_chan_go(dev):
  333. """P2P group formation forced channel selection by GO"""
  334. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  335. i_freq=2432,
  336. r_dev=dev[1], r_intent=0,
  337. test_data=False)
  338. check_grpform_results(i_res, r_res)
  339. if i_res['freq'] != "2432":
  340. raise Exception("Unexpected channel - did not follow GO's forced channel")
  341. remove_group(dev[0], dev[1])
  342. def test_grpform_force_chan_cli(dev):
  343. """P2P group formation forced channel selection by client"""
  344. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  345. i_freq=2417,
  346. r_dev=dev[1], r_intent=15,
  347. test_data=False)
  348. check_grpform_results(i_res, r_res)
  349. if i_res['freq'] != "2417":
  350. raise Exception("Unexpected channel - did not follow GO's forced channel")
  351. remove_group(dev[0], dev[1])
  352. def test_grpform_force_chan_conflict(dev):
  353. """P2P group formation fails due to forced channel mismatch"""
  354. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
  355. r_dev=dev[1], r_intent=15, r_freq=2427,
  356. expect_failure=True, i_go_neg_status=7)
  357. def test_grpform_pref_chan_go(dev):
  358. """P2P group formation preferred channel selection by GO"""
  359. dev[0].request("SET p2p_pref_chan 81:7")
  360. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  361. r_dev=dev[1], r_intent=0,
  362. test_data=False)
  363. check_grpform_results(i_res, r_res)
  364. if i_res['freq'] != "2442":
  365. raise Exception("Unexpected channel - did not follow GO's p2p_pref_chan")
  366. remove_group(dev[0], dev[1])
  367. def test_grpform_pref_chan_go_overridden(dev):
  368. """P2P group formation preferred channel selection by GO overridden by client"""
  369. dev[1].request("SET p2p_pref_chan 81:7")
  370. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  371. i_freq=2422,
  372. r_dev=dev[1], r_intent=15,
  373. test_data=False)
  374. check_grpform_results(i_res, r_res)
  375. if i_res['freq'] != "2422":
  376. raise Exception("Unexpected channel - did not follow client's forced channel")
  377. remove_group(dev[0], dev[1])
  378. def test_grpform_no_go_freq_forcing_chan(dev):
  379. """P2P group formation with no-GO freq forcing channel"""
  380. dev[1].request("SET p2p_no_go_freq 100-200,300,4000-6000")
  381. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  382. r_dev=dev[1], r_intent=15,
  383. test_data=False)
  384. check_grpform_results(i_res, r_res)
  385. if int(i_res['freq']) > 4000:
  386. raise Exception("Unexpected channel - did not follow no-GO freq")
  387. remove_group(dev[0], dev[1])
  388. def test_grpform_no_go_freq_conflict(dev):
  389. """P2P group formation fails due to no-GO range forced by client"""
  390. dev[1].request("SET p2p_no_go_freq 2000-3000")
  391. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
  392. r_dev=dev[1], r_intent=15,
  393. expect_failure=True, i_go_neg_status=7)
  394. def test_grpform_no_5ghz_world_roaming(dev):
  395. """P2P group formation with world roaming regulatory"""
  396. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  397. r_dev=dev[1], r_intent=15,
  398. test_data=False)
  399. check_grpform_results(i_res, r_res)
  400. if int(i_res['freq']) > 4000:
  401. raise Exception("Unexpected channel - did not follow world roaming rules")
  402. remove_group(dev[0], dev[1])
  403. def test_grpform_no_5ghz_add_cli(dev):
  404. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1"""
  405. dev[0].request("SET p2p_add_cli_chan 1")
  406. dev[1].request("SET p2p_add_cli_chan 1")
  407. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  408. r_dev=dev[1], r_intent=14,
  409. test_data=False)
  410. check_grpform_results(i_res, r_res)
  411. if int(i_res['freq']) > 4000:
  412. raise Exception("Unexpected channel - did not follow world roaming rules")
  413. remove_group(dev[0], dev[1])
  414. def test_grpform_no_5ghz_add_cli2(dev):
  415. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse)"""
  416. dev[0].request("SET p2p_add_cli_chan 1")
  417. dev[1].request("SET p2p_add_cli_chan 1")
  418. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=14,
  419. r_dev=dev[1], r_intent=0,
  420. test_data=False)
  421. check_grpform_results(i_res, r_res)
  422. if int(i_res['freq']) > 4000:
  423. raise Exception("Unexpected channel - did not follow world roaming rules")
  424. remove_group(dev[0], dev[1])
  425. def test_grpform_no_5ghz_add_cli3(dev):
  426. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (intent 15)"""
  427. dev[0].request("SET p2p_add_cli_chan 1")
  428. dev[1].request("SET p2p_add_cli_chan 1")
  429. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  430. r_dev=dev[1], r_intent=15,
  431. test_data=False)
  432. check_grpform_results(i_res, r_res)
  433. if int(i_res['freq']) > 4000:
  434. raise Exception("Unexpected channel - did not follow world roaming rules")
  435. remove_group(dev[0], dev[1])
  436. def test_grpform_no_5ghz_add_cli4(dev):
  437. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse; intent 15)"""
  438. dev[0].request("SET p2p_add_cli_chan 1")
  439. dev[1].request("SET p2p_add_cli_chan 1")
  440. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  441. r_dev=dev[1], r_intent=0,
  442. test_data=False)
  443. check_grpform_results(i_res, r_res)
  444. if int(i_res['freq']) > 4000:
  445. raise Exception("Unexpected channel - did not follow world roaming rules")
  446. remove_group(dev[0], dev[1])
  447. def test_grpform_incorrect_pin(dev):
  448. """P2P GO Negotiation with incorrect PIN"""
  449. dev[1].p2p_listen()
  450. pin = dev[1].wps_read_pin()
  451. addr1 = dev[1].p2p_dev_addr()
  452. if not dev[0].discover_peer(addr1):
  453. raise Exception("Peer not found")
  454. dev[1].p2p_go_neg_auth(dev[0].p2p_dev_addr(), pin, 'display', go_intent=0)
  455. dev[0].request("P2P_CONNECT " + addr1 + " 00000000 enter go_intent=15")
  456. ev = dev[1].wait_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=10)
  457. if ev is None:
  458. raise Exception("Group formation failure timed out")
  459. ev = dev[0].wait_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=5)
  460. if ev is None:
  461. raise Exception("Group formation failure timed out")
  462. def test_grpform_reject(dev):
  463. """User rejecting group formation attempt by a P2P peer"""
  464. addr0 = dev[0].p2p_dev_addr()
  465. dev[0].p2p_listen()
  466. dev[1].p2p_go_neg_init(addr0, None, "pbc")
  467. ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  468. if ev is None:
  469. raise Exception("GO Negotiation timed out")
  470. if "FAIL" in dev[0].global_request("P2P_REJECT " + ev.split(' ')[1]):
  471. raise Exception("P2P_REJECT failed")
  472. dev[1].request("P2P_STOP_FIND")
  473. dev[1].p2p_go_neg_init(addr0, None, "pbc")
  474. ev = dev[1].wait_global_event(["GO-NEG-FAILURE"], timeout=10)
  475. if ev is None:
  476. raise Exception("Rejection not reported")
  477. if "status=11" not in ev:
  478. raise Exception("Unexpected status code in rejection")
  479. def test_grpform_pd_no_probe_resp(dev):
  480. """GO Negotiation after PD, but no Probe Response"""
  481. addr0 = dev[0].p2p_dev_addr()
  482. addr1 = dev[1].p2p_dev_addr()
  483. dev[0].p2p_listen()
  484. if not dev[1].discover_peer(addr0):
  485. raise Exception("Peer not found")
  486. dev[1].p2p_stop_find()
  487. dev[0].p2p_stop_find()
  488. peer = dev[0].get_peer(addr1)
  489. if peer['listen_freq'] == '0':
  490. raise Exception("Peer listen frequency not learned from Probe Request")
  491. time.sleep(0.3)
  492. dev[0].request("P2P_FLUSH")
  493. dev[0].p2p_listen()
  494. dev[1].global_request("P2P_PROV_DISC " + addr0 + " display")
  495. ev = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=5)
  496. if ev is None:
  497. raise Exception("PD Request timed out")
  498. ev = dev[1].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=5)
  499. if ev is None:
  500. raise Exception("PD Response timed out")
  501. peer = dev[0].get_peer(addr1)
  502. if peer['listen_freq'] != '0':
  503. raise Exception("Peer listen frequency learned unexpectedly from PD Request")
  504. pin = dev[0].wps_read_pin()
  505. if "FAIL" in dev[1].request("P2P_CONNECT " + addr0 + " " + pin + " enter"):
  506. raise Exception("P2P_CONNECT on initiator failed")
  507. ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
  508. if ev is None:
  509. raise Exception("GO Negotiation start timed out")
  510. peer = dev[0].get_peer(addr1)
  511. if peer['listen_freq'] == '0':
  512. raise Exception("Peer listen frequency not learned from PD followed by GO Neg Req")
  513. if "FAIL" in dev[0].request("P2P_CONNECT " + addr1 + " " + pin + " display"):
  514. raise Exception("P2P_CONNECT on responder failed")
  515. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  516. if ev is None:
  517. raise Exception("Group formation timed out")
  518. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  519. if ev is None:
  520. raise Exception("Group formation timed out")
  521. def test_go_neg_two_peers(dev):
  522. """P2P GO Negotiation rejected due to already started negotiation with another peer"""
  523. addr0 = dev[0].p2p_dev_addr()
  524. addr1 = dev[1].p2p_dev_addr()
  525. addr2 = dev[2].p2p_dev_addr()
  526. dev[1].p2p_listen()
  527. dev[2].p2p_listen()
  528. if not dev[0].discover_peer(addr1):
  529. raise Exception("Could not discover peer")
  530. if not dev[0].discover_peer(addr2):
  531. raise Exception("Could not discover peer")
  532. if "OK" not in dev[0].request("P2P_CONNECT " + addr2 + " pbc auth"):
  533. raise Exception("Failed to authorize GO Neg")
  534. dev[0].p2p_listen()
  535. if not dev[2].discover_peer(addr0):
  536. raise Exception("Could not discover peer")
  537. if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc"):
  538. raise Exception("Failed to initiate GO Neg")
  539. ev = dev[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
  540. if ev is None:
  541. raise Exception("timeout on GO Neg RX event")
  542. dev[2].request("P2P_CONNECT " + addr0 + " pbc")
  543. ev = dev[2].wait_global_event(["GO-NEG-FAILURE"], timeout=10)
  544. if ev is None:
  545. raise Exception("Rejection not reported")
  546. if "status=5" not in ev:
  547. raise Exception("Unexpected status code in rejection: " + ev)
  548. def clear_pbc_overlap(dev, ifname):
  549. hapd_global = hostapd.HostapdGlobal()
  550. hapd_global.remove(ifname)
  551. dev[0].p2p_stop_find()
  552. dev[1].p2p_stop_find()
  553. dev[0].dump_monitor()
  554. dev[1].dump_monitor()
  555. time.sleep(0.1)
  556. dev[0].request("BSS_FLUSH 0")
  557. dev[0].request("SCAN freq=2412 only_new=1")
  558. dev[1].request("BSS_FLUSH 0")
  559. dev[1].request("SCAN freq=2412 only_new=1")
  560. time.sleep(1)
  561. def test_grpform_pbc_overlap(dev, apdev):
  562. """P2P group formation during PBC overlap"""
  563. params = { "ssid": "wps", "eap_server": "1", "wps_state": "1" }
  564. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  565. hapd.request("WPS_PBC")
  566. time.sleep(0.1)
  567. # Since P2P Client scan case is now optimzied to use a specific SSID, the
  568. # WPS AP will not reply to that and the scan after GO Negotiation can quite
  569. # likely miss the AP due to dwell time being short enoguh to miss the Beacon
  570. # frame. This has made the test case somewhat pointless, but keep it here
  571. # for now with an additional scan to confirm that PBC detection works if
  572. # there is a BSS entry for a overlapping AP.
  573. for i in range(0, 5):
  574. dev[0].scan(freq="2412")
  575. if dev[0].get_bss(apdev[0]['bssid']) is not None:
  576. break
  577. addr0 = dev[0].p2p_dev_addr()
  578. addr1 = dev[1].p2p_dev_addr()
  579. dev[0].p2p_listen()
  580. if not dev[1].discover_peer(addr0):
  581. raise Exception("Could not discover peer")
  582. dev[1].p2p_listen()
  583. if not dev[0].discover_peer(addr1):
  584. raise Exception("Could not discover peer")
  585. dev[0].p2p_listen()
  586. if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc auth go_intent=0"):
  587. raise Exception("Failed to authorize GO Neg")
  588. if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " pbc go_intent=15 freq=2412"):
  589. raise Exception("Failed to initiate GO Neg")
  590. ev = dev[0].wait_global_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  591. if ev is None:
  592. raise Exception("PBC overlap not reported")
  593. clear_pbc_overlap(dev, apdev[0]['ifname'])
  594. def test_grpform_pbc_overlap_group_iface(dev, apdev):
  595. """P2P group formation during PBC overlap using group interfaces"""
  596. # Note: Need to include P2P IE from the AP to get the P2P interface BSS
  597. # update use this information.
  598. params = { "ssid": "wps", "eap_server": "1", "wps_state": "1",
  599. "beacon_int": "15", 'manage_p2p': '1' }
  600. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  601. hapd.request("WPS_PBC")
  602. dev[0].request("SET p2p_no_group_iface 0")
  603. dev[1].request("SET p2p_no_group_iface 0")
  604. addr0 = dev[0].p2p_dev_addr()
  605. addr1 = dev[1].p2p_dev_addr()
  606. dev[0].p2p_listen()
  607. if not dev[1].discover_peer(addr0):
  608. raise Exception("Could not discover peer")
  609. dev[1].p2p_listen()
  610. if not dev[0].discover_peer(addr1):
  611. raise Exception("Could not discover peer")
  612. dev[0].p2p_stop_find()
  613. dev[0].scan(freq="2412")
  614. dev[0].p2p_listen()
  615. if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc auth go_intent=0"):
  616. raise Exception("Failed to authorize GO Neg")
  617. if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " pbc go_intent=15 freq=2412"):
  618. raise Exception("Failed to initiate GO Neg")
  619. ev = dev[0].wait_global_event(["WPS-OVERLAP-DETECTED",
  620. "P2P-GROUP-FORMATION-SUCCESS"], timeout=15)
  621. if ev is None or "WPS-OVERLAP-DETECTED" not in ev:
  622. # Do not report this as failure since the P2P group formation case
  623. # using a separate group interface has limited chances of "seeing" the
  624. # overlapping AP due to a per-SSID scan and no prior scan operations on
  625. # the group interface.
  626. logger.info("PBC overlap not reported")
  627. clear_pbc_overlap(dev, apdev[0]['ifname'])
  628. def test_grpform_goneg_fail_with_group_iface(dev):
  629. """P2P group formation fails while using group interface"""
  630. dev[0].request("SET p2p_no_group_iface 0")
  631. dev[1].p2p_listen()
  632. peer = dev[1].p2p_dev_addr()
  633. if not dev[0].discover_peer(peer):
  634. raise Exception("Peer " + peer + " not found")
  635. if "OK" not in dev[1].request("P2P_REJECT " + dev[0].p2p_dev_addr()):
  636. raise Exception("P2P_REJECT failed")
  637. if "OK" not in dev[0].request("P2P_CONNECT " + peer + " pbc"):
  638. raise Exception("P2P_CONNECT failed")
  639. ev = dev[0].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
  640. if ev is None:
  641. raise Exception("GO Negotiation failure timed out")
  642. def test_grpform_cred_ready_timeout(dev, apdev, params):
  643. """P2P GO Negotiation wait for credentials to become ready [long]"""
  644. if not params['long']:
  645. logger.info("Skip test case with long duration due to --long not specified")
  646. return "skip"
  647. dev[1].p2p_listen()
  648. addr1 = dev[1].p2p_dev_addr()
  649. if not dev[0].discover_peer(addr1):
  650. raise Exception("Peer " + addr1 + " not found")
  651. start = os.times()[4]
  652. if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " 12345670 display"):
  653. raise Exception("Failed to initiate GO Neg")
  654. ev = dev[0].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=200)
  655. if ev is None:
  656. raise Exception("GO Negotiation failure timed out")
  657. end = os.times()[4]
  658. logger.info("GO Negotiation wait time: {} seconds".format(end - start))
  659. if end - start < 120:
  660. raise Exception("Too short GO Negotiation wait time: {}".format(end - start))