p2p_utils.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. # P2P helper functions
  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 logging
  7. logger = logging.getLogger()
  8. import threading
  9. import time
  10. import Queue
  11. import hwsim_utils
  12. MGMT_SUBTYPE_PROBE_REQ = 4
  13. MGMT_SUBTYPE_ACTION = 13
  14. ACTION_CATEG_PUBLIC = 4
  15. P2P_GO_NEG_REQ = 0
  16. P2P_GO_NEG_RESP = 1
  17. P2P_GO_NEG_CONF = 2
  18. P2P_INVITATION_REQ = 3
  19. P2P_INVITATION_RESP = 4
  20. P2P_DEV_DISC_REQ = 5
  21. P2P_DEV_DISC_RESP = 6
  22. P2P_PROV_DISC_REQ = 7
  23. P2P_PROV_DISC_RESP = 8
  24. P2P_ATTR_STATUS = 0
  25. P2P_ATTR_MINOR_REASON_CODE = 1
  26. P2P_ATTR_CAPABILITY = 2
  27. P2P_ATTR_DEVICE_ID = 3
  28. P2P_ATTR_GROUP_OWNER_INTENT = 4
  29. P2P_ATTR_CONFIGURATION_TIMEOUT = 5
  30. P2P_ATTR_LISTEN_CHANNEL = 6
  31. P2P_ATTR_GROUP_BSSID = 7
  32. P2P_ATTR_EXT_LISTEN_TIMING = 8
  33. P2P_ATTR_INTENDED_INTERFACE_ADDR = 9
  34. P2P_ATTR_MANAGEABILITY = 10
  35. P2P_ATTR_CHANNEL_LIST = 11
  36. P2P_ATTR_NOTICE_OF_ABSENCE = 12
  37. P2P_ATTR_DEVICE_INFO = 13
  38. P2P_ATTR_GROUP_INFO = 14
  39. P2P_ATTR_GROUP_ID = 15
  40. P2P_ATTR_INTERFACE = 16
  41. P2P_ATTR_OPERATING_CHANNEL = 17
  42. P2P_ATTR_INVITATION_FLAGS = 18
  43. P2P_ATTR_OOB_GO_NEG_CHANNEL = 19
  44. P2P_ATTR_SERVICE_HASH = 21
  45. P2P_ATTR_SESSION_INFORMATION_DATA = 22
  46. P2P_ATTR_CONNECTION_CAPABILITY = 23
  47. P2P_ATTR_ADVERTISEMENT_ID = 24
  48. P2P_ATTR_ADVERTISED_SERVICE = 25
  49. P2P_ATTR_SESSION_ID = 26
  50. P2P_ATTR_FEATURE_CAPABILITY = 27
  51. P2P_ATTR_PERSISTENT_GROUP = 28
  52. P2P_ATTR_VENDOR_SPECIFIC = 221
  53. P2P_SC_SUCCESS = 0
  54. P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE = 1
  55. P2P_SC_FAIL_INCOMPATIBLE_PARAMS = 2
  56. P2P_SC_FAIL_LIMIT_REACHED = 3
  57. P2P_SC_FAIL_INVALID_PARAMS = 4
  58. P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE = 5
  59. P2P_SC_FAIL_PREV_PROTOCOL_ERROR = 6
  60. P2P_SC_FAIL_NO_COMMON_CHANNELS = 7
  61. P2P_SC_FAIL_UNKNOWN_GROUP = 8
  62. P2P_SC_FAIL_BOTH_GO_INTENT_15 = 9
  63. P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD = 10
  64. P2P_SC_FAIL_REJECTED_BY_USER = 11
  65. WSC_ATTR_CONFIG_METHODS = 0x1008
  66. WLAN_EID_SSID = 0
  67. WLAN_EID_SUPP_RATES = 1
  68. WLAN_EID_VENDOR_SPECIFIC = 221
  69. def go_neg_pin_authorized_persistent(i_dev, r_dev, i_intent=None, r_intent=None,
  70. i_method='enter', r_method='display',
  71. test_data=True, r_listen=True):
  72. if r_listen:
  73. r_dev.p2p_listen()
  74. i_dev.p2p_listen()
  75. pin = r_dev.wps_read_pin()
  76. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  77. r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), pin, r_method,
  78. go_intent=r_intent, persistent=True)
  79. if r_listen:
  80. r_dev.p2p_listen()
  81. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method,
  82. timeout=20, go_intent=i_intent,
  83. persistent=True)
  84. r_res = r_dev.p2p_go_neg_auth_result()
  85. logger.debug("i_res: " + str(i_res))
  86. logger.debug("r_res: " + str(r_res))
  87. r_dev.dump_monitor()
  88. i_dev.dump_monitor()
  89. logger.info("Group formed")
  90. if test_data:
  91. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  92. return [i_res, r_res]
  93. def terminate_group(go, cli):
  94. logger.info("Terminate persistent group")
  95. go.remove_group()
  96. cli.wait_go_ending_session()
  97. def invite(inv, resp, extra=None, persistent_reconnect=True, use_listen=True):
  98. addr = resp.p2p_dev_addr()
  99. if persistent_reconnect:
  100. resp.global_request("SET persistent_reconnect 1")
  101. else:
  102. resp.global_request("SET persistent_reconnect 0")
  103. if use_listen:
  104. resp.p2p_listen()
  105. else:
  106. resp.p2p_find(social=True)
  107. if not inv.discover_peer(addr, social=True):
  108. raise Exception("Peer " + addr + " not found")
  109. inv.dump_monitor()
  110. peer = inv.get_peer(addr)
  111. cmd = "P2P_INVITE persistent=" + peer['persistent'] + " peer=" + addr
  112. if extra:
  113. cmd = cmd + " " + extra
  114. inv.global_request(cmd)
  115. def check_result(go, cli):
  116. ev = go.wait_global_event(["P2P-GROUP-STARTED",
  117. "Failed to start AP functionality"], timeout=30)
  118. if ev is None:
  119. raise Exception("Timeout on group re-invocation (on GO)")
  120. if "P2P-GROUP-STARTED" not in ev:
  121. raise Exception("GO failed to start the group for re-invocation")
  122. if "[PERSISTENT]" not in ev:
  123. raise Exception("Re-invoked group not marked persistent")
  124. go_res = go.group_form_result(ev)
  125. if go_res['role'] != 'GO':
  126. raise Exception("Persistent group GO did not become GO")
  127. if not go_res['persistent']:
  128. raise Exception("Persistent group not re-invoked as persistent (GO)")
  129. ev = cli.wait_global_event(["P2P-GROUP-STARTED"], timeout=30)
  130. if ev is None:
  131. raise Exception("Timeout on group re-invocation (on client)")
  132. if "[PERSISTENT]" not in ev:
  133. raise Exception("Re-invoked group not marked persistent")
  134. cli_res = cli.group_form_result(ev)
  135. if cli_res['role'] != 'client':
  136. raise Exception("Persistent group client did not become client")
  137. if not cli_res['persistent']:
  138. raise Exception("Persistent group not re-invoked as persistent (cli)")
  139. return [go_res, cli_res]
  140. def form(go, cli, test_data=True, reverse_init=False, r_listen=True):
  141. logger.info("Form a persistent group")
  142. if reverse_init:
  143. [i_res, r_res] = go_neg_pin_authorized_persistent(i_dev=cli, i_intent=0,
  144. r_dev=go, r_intent=15,
  145. test_data=test_data,
  146. r_listen=r_listen)
  147. else:
  148. [i_res, r_res] = go_neg_pin_authorized_persistent(i_dev=go, i_intent=15,
  149. r_dev=cli, r_intent=0,
  150. test_data=test_data,
  151. r_listen=r_listen)
  152. if not i_res['persistent'] or not r_res['persistent']:
  153. raise Exception("Formed group was not persistent")
  154. terminate_group(go, cli)
  155. if reverse_init:
  156. return r_res
  157. else:
  158. return i_res
  159. def invite_from_cli(go, cli, terminate=True):
  160. logger.info("Re-invoke persistent group from client")
  161. invite(cli, go)
  162. [go_res, cli_res] = check_result(go, cli)
  163. hwsim_utils.test_connectivity_p2p(go, cli)
  164. if terminate:
  165. terminate_group(go, cli)
  166. return [go_res, cli_res]
  167. def invite_from_go(go, cli, terminate=True):
  168. logger.info("Re-invoke persistent group from GO")
  169. invite(go, cli)
  170. [go_res, cli_res] = check_result(go, cli)
  171. hwsim_utils.test_connectivity_p2p(go, cli)
  172. if terminate:
  173. terminate_group(go, cli)
  174. return [go_res, cli_res]
  175. def autogo(go, freq=None, persistent=None):
  176. logger.info("Start autonomous GO " + go.ifname)
  177. res = go.p2p_start_go(freq=freq, persistent=persistent)
  178. logger.debug("res: " + str(res))
  179. return res
  180. def connect_cli(go, client, social=False, freq=None):
  181. logger.info("Try to connect the client to the GO")
  182. pin = client.wps_read_pin()
  183. go.p2p_go_authorize_client(pin)
  184. res = client.p2p_connect_group(go.p2p_dev_addr(), pin, timeout=60,
  185. social=social, freq=freq)
  186. logger.info("Client connected")
  187. hwsim_utils.test_connectivity_p2p(go, client)
  188. return res
  189. def check_grpform_results(i_res, r_res):
  190. if i_res['result'] != 'success' or r_res['result'] != 'success':
  191. raise Exception("Failed group formation")
  192. if i_res['ssid'] != r_res['ssid']:
  193. raise Exception("SSID mismatch")
  194. if i_res['freq'] != r_res['freq']:
  195. raise Exception("freq mismatch")
  196. if 'go_neg_freq' in r_res and i_res['go_neg_freq'] != r_res['go_neg_freq']:
  197. raise Exception("go_neg_freq mismatch")
  198. if i_res['freq'] != i_res['go_neg_freq']:
  199. raise Exception("freq/go_neg_freq mismatch")
  200. if i_res['role'] != i_res['go_neg_role']:
  201. raise Exception("role/go_neg_role mismatch")
  202. if 'go_neg_role' in r_res and r_res['role'] != r_res['go_neg_role']:
  203. raise Exception("role/go_neg_role mismatch")
  204. if i_res['go_dev_addr'] != r_res['go_dev_addr']:
  205. raise Exception("GO Device Address mismatch")
  206. def go_neg_init(i_dev, r_dev, pin, i_method, i_intent, res):
  207. logger.debug("Initiate GO Negotiation from i_dev")
  208. try:
  209. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent)
  210. logger.debug("i_res: " + str(i_res))
  211. except Exception, e:
  212. i_res = None
  213. logger.info("go_neg_init thread caught an exception from p2p_go_neg_init: " + str(e))
  214. res.put(i_res)
  215. def go_neg_pin(i_dev, r_dev, i_intent=None, r_intent=None, i_method='enter', r_method='display'):
  216. r_dev.p2p_listen()
  217. i_dev.p2p_listen()
  218. pin = r_dev.wps_read_pin()
  219. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  220. r_dev.dump_monitor()
  221. res = Queue.Queue()
  222. t = threading.Thread(target=go_neg_init, args=(i_dev, r_dev, pin, i_method, i_intent, res))
  223. t.start()
  224. logger.debug("Wait for GO Negotiation Request on r_dev")
  225. ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  226. if ev is None:
  227. raise Exception("GO Negotiation timed out")
  228. r_dev.dump_monitor()
  229. logger.debug("Re-initiate GO Negotiation from r_dev")
  230. r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, timeout=20)
  231. logger.debug("r_res: " + str(r_res))
  232. r_dev.dump_monitor()
  233. t.join()
  234. i_res = res.get()
  235. if i_res is None:
  236. raise Exception("go_neg_init thread failed")
  237. logger.debug("i_res: " + str(i_res))
  238. logger.info("Group formed")
  239. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  240. i_dev.dump_monitor()
  241. return [i_res, r_res]
  242. def go_neg_pin_authorized(i_dev, r_dev, i_intent=None, r_intent=None,
  243. expect_failure=False, i_go_neg_status=None,
  244. i_method='enter', r_method='display', test_data=True,
  245. i_freq=None, r_freq=None,
  246. i_freq2=None, r_freq2=None,
  247. i_max_oper_chwidth=None, r_max_oper_chwidth=None,
  248. i_ht40=False, i_vht=False, r_ht40=False, r_vht=False):
  249. i_dev.p2p_listen()
  250. pin = r_dev.wps_read_pin()
  251. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  252. r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), pin, r_method,
  253. go_intent=r_intent, freq=r_freq, freq2=r_freq2,
  254. max_oper_chwidth=r_max_oper_chwidth, ht40=r_ht40,
  255. vht=r_vht)
  256. r_dev.p2p_listen()
  257. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method,
  258. timeout=20, go_intent=i_intent,
  259. expect_failure=expect_failure, freq=i_freq,
  260. freq2=i_freq2,
  261. max_oper_chwidth=i_max_oper_chwidth,
  262. ht40=i_ht40, vht=i_vht)
  263. r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
  264. logger.debug("i_res: " + str(i_res))
  265. logger.debug("r_res: " + str(r_res))
  266. r_dev.dump_monitor()
  267. i_dev.dump_monitor()
  268. if i_go_neg_status:
  269. if i_res['result'] != 'go-neg-failed':
  270. raise Exception("Expected GO Negotiation failure not reported")
  271. if i_res['status'] != i_go_neg_status:
  272. raise Exception("Expected GO Negotiation status not seen")
  273. if expect_failure:
  274. return
  275. logger.info("Group formed")
  276. if test_data:
  277. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  278. return [i_res, r_res]
  279. def go_neg_init_pbc(i_dev, r_dev, i_intent, res, freq, provdisc):
  280. logger.debug("Initiate GO Negotiation from i_dev")
  281. try:
  282. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc",
  283. timeout=20, go_intent=i_intent, freq=freq,
  284. provdisc=provdisc)
  285. logger.debug("i_res: " + str(i_res))
  286. except Exception, e:
  287. i_res = None
  288. logger.info("go_neg_init_pbc thread caught an exception from p2p_go_neg_init: " + str(e))
  289. res.put(i_res)
  290. 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):
  291. if r_listen:
  292. r_dev.p2p_listen()
  293. else:
  294. r_dev.p2p_find(social=True)
  295. i_dev.p2p_find(social=True)
  296. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  297. r_dev.dump_monitor()
  298. res = Queue.Queue()
  299. t = threading.Thread(target=go_neg_init_pbc, args=(i_dev, r_dev, i_intent, res, i_freq, provdisc))
  300. t.start()
  301. logger.debug("Wait for GO Negotiation Request on r_dev")
  302. ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  303. if ev is None:
  304. raise Exception("GO Negotiation timed out")
  305. r_dev.dump_monitor()
  306. # Allow some time for the GO Neg Resp to go out before initializing new
  307. # GO Negotiation.
  308. time.sleep(0.2)
  309. logger.debug("Re-initiate GO Negotiation from r_dev")
  310. r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), None, "pbc",
  311. go_intent=r_intent, timeout=20, freq=r_freq)
  312. logger.debug("r_res: " + str(r_res))
  313. r_dev.dump_monitor()
  314. t.join()
  315. i_res = res.get()
  316. if i_res is None:
  317. raise Exception("go_neg_init_pbc thread failed")
  318. logger.debug("i_res: " + str(i_res))
  319. logger.info("Group formed")
  320. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  321. i_dev.dump_monitor()
  322. return [i_res, r_res]
  323. def go_neg_pbc_authorized(i_dev, r_dev, i_intent=None, r_intent=None,
  324. expect_failure=False, i_freq=None, r_freq=None):
  325. i_dev.p2p_listen()
  326. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  327. r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), None, "pbc",
  328. go_intent=r_intent, freq=r_freq)
  329. r_dev.p2p_listen()
  330. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc", timeout=20,
  331. go_intent=i_intent,
  332. expect_failure=expect_failure, freq=i_freq)
  333. r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
  334. logger.debug("i_res: " + str(i_res))
  335. logger.debug("r_res: " + str(r_res))
  336. r_dev.dump_monitor()
  337. i_dev.dump_monitor()
  338. if expect_failure:
  339. return
  340. logger.info("Group formed")
  341. return [i_res, r_res]
  342. def remove_group(dev1, dev2):
  343. dev1.remove_group()
  344. try:
  345. dev2.remove_group()
  346. except:
  347. pass