test_p2p_messages.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. # P2P protocol tests for various messages
  2. # Copyright (c) 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 binascii
  7. import struct
  8. import time
  9. import logging
  10. logger = logging.getLogger()
  11. import hostapd
  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_VENDOR_SPECIFIC = 221
  45. P2P_SC_SUCCESS = 0
  46. P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE = 1
  47. P2P_SC_FAIL_INCOMPATIBLE_PARAMS = 2
  48. P2P_SC_FAIL_LIMIT_REACHED = 3
  49. P2P_SC_FAIL_INVALID_PARAMS = 4
  50. P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE = 5
  51. P2P_SC_FAIL_PREV_PROTOCOL_ERROR = 6
  52. P2P_SC_FAIL_NO_COMMON_CHANNELS = 7
  53. P2P_SC_FAIL_UNKNOWN_GROUP = 8
  54. P2P_SC_FAIL_BOTH_GO_INTENT_15 = 9
  55. P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD = 10
  56. P2P_SC_FAIL_REJECTED_BY_USER = 11
  57. WSC_ATTR_CONFIG_METHODS = 0x1008
  58. WLAN_EID_SSID = 0
  59. WLAN_EID_SUPP_RATES = 1
  60. WLAN_EID_VENDOR_SPECIFIC = 221
  61. def ie_ssid(ssid):
  62. return struct.pack("<BB", WLAN_EID_SSID, len(ssid)) + ssid
  63. def ie_supp_rates():
  64. return struct.pack("<BBBBBBBBBB", WLAN_EID_SUPP_RATES, 8,
  65. 2*6, 2*9, 2*12, 2*18, 2*24, 2*36, 2*48, 2*54)
  66. def ie_p2p(attrs):
  67. return struct.pack("<BBBBBB", WLAN_EID_VENDOR_SPECIFIC, 4 + len(attrs),
  68. 0x50, 0x6f, 0x9a, 9) + attrs
  69. def ie_wsc(attrs):
  70. return struct.pack("<BBBBBB", WLAN_EID_VENDOR_SPECIFIC, 4 + len(attrs),
  71. 0x00, 0x50, 0xf2, 4) + attrs
  72. def wsc_attr_config_methods(methods=0):
  73. return struct.pack(">HHH", WSC_ATTR_CONFIG_METHODS, 2, methods)
  74. def p2p_attr_status(status=P2P_SC_SUCCESS):
  75. return struct.pack("<BHB", P2P_ATTR_STATUS, 1, status)
  76. def p2p_attr_minor_reason_code(code=0):
  77. return struct.pack("<BHB", P2P_ATTR_MINOR_REASON_CODE, 1, code)
  78. def p2p_attr_capability(dev_capab=0, group_capab=0):
  79. return struct.pack("<BHBB", P2P_ATTR_CAPABILITY, 2, dev_capab, group_capab)
  80. def p2p_attr_device_id(addr):
  81. val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
  82. t = (P2P_ATTR_DEVICE_ID, 6) + val
  83. return struct.pack('<BH6B', *t)
  84. def p2p_attr_go_intent(go_intent=0, tie_breaker=0):
  85. return struct.pack("<BHB", P2P_ATTR_GROUP_OWNER_INTENT, 1,
  86. (go_intent << 1) | (tie_breaker & 0x01))
  87. def p2p_attr_config_timeout(go_config_timeout=0, client_config_timeout=0):
  88. return struct.pack("<BHBB", P2P_ATTR_CONFIGURATION_TIMEOUT, 2,
  89. go_config_timeout, client_config_timeout)
  90. def p2p_attr_listen_channel(op_class=81, chan=1):
  91. return struct.pack("<BHBBBBB", P2P_ATTR_LISTEN_CHANNEL, 5,
  92. 0x58, 0x58, 0x04, op_class, chan)
  93. def p2p_attr_group_bssid(addr):
  94. val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
  95. t = (P2P_ATTR_GROUP_BSSID, 6) + val
  96. return struct.pack('<BH6B', *t)
  97. def p2p_attr_ext_listen_timing(period=0, interval=0):
  98. return struct.pack("<BHHH", P2P_ATTR_EXT_LISTEN_TIMING, 4, period, interval)
  99. def p2p_attr_intended_interface_addr(addr):
  100. val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
  101. t = (P2P_ATTR_INTENDED_INTERFACE_ADDR, 6) + val
  102. return struct.pack('<BH6B', *t)
  103. def p2p_attr_manageability(bitmap=0):
  104. return struct.pack("<BHB", P2P_ATTR_MANAGEABILITY, 1, bitmap)
  105. def p2p_attr_channel_list():
  106. return struct.pack("<BH3BBB11B", P2P_ATTR_CHANNEL_LIST, 16,
  107. 0x58, 0x58, 0x04,
  108. 81, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
  109. def p2p_attr_device_info(addr, name="Test", config_methods=0, dev_type="00010050F2040001"):
  110. val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
  111. val2 = struct.unpack('8B', binascii.unhexlify(dev_type))
  112. t = (P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 4 + len(name)) + val + (config_methods,) + val2 + (0,)
  113. return struct.pack("<BH6BH8BB", *t) + struct.pack('>HH', 0x1011, len(name)) +name
  114. def p2p_attr_group_id(addr, ssid):
  115. val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
  116. t = (P2P_ATTR_GROUP_ID, 6 + len(ssid)) + val
  117. return struct.pack('<BH6B', *t) + ssid
  118. def p2p_attr_operating_channel(op_class=81, chan=1):
  119. return struct.pack("<BHBBBBB", P2P_ATTR_OPERATING_CHANNEL, 5,
  120. 0x58, 0x58, 0x04, op_class, chan)
  121. def p2p_attr_invitation_flags(bitmap=0):
  122. return struct.pack("<BHB", P2P_ATTR_INVITATION_FLAGS, 1, bitmap)
  123. def p2p_hdr_helper(dst, src, type=None, dialog_token=1, req=True):
  124. msg = {}
  125. msg['fc'] = MGMT_SUBTYPE_ACTION << 4
  126. msg['da'] = dst
  127. msg['sa'] = src
  128. if req:
  129. msg['bssid'] = dst
  130. else:
  131. msg['bssid'] = src
  132. msg['payload'] = struct.pack("<BBBBBB",
  133. ACTION_CATEG_PUBLIC, 9, 0x50, 0x6f, 0x9a, 9)
  134. if type is not None:
  135. msg['payload'] += struct.pack("<B", type)
  136. if dialog_token:
  137. msg['payload'] += struct.pack("<B", dialog_token)
  138. return msg
  139. def p2p_hdr(dst, src, type=None, dialog_token=1):
  140. return p2p_hdr_helper(dst, src, type, dialog_token, True)
  141. def p2p_hdr_resp(dst, src, type=None, dialog_token=1):
  142. return p2p_hdr_helper(dst, src, type, dialog_token, False)
  143. def start_p2p(dev, apdev):
  144. addr0 = dev[0].p2p_dev_addr()
  145. dev[0].p2p_listen()
  146. dev[1].p2p_find(social=True)
  147. ev = dev[1].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
  148. if ev is None:
  149. raise Exception("Device discovery timed out")
  150. dev[1].p2p_stop_find()
  151. peer = dev[1].get_peer(addr0)
  152. bssid = apdev[0]['bssid']
  153. params = { 'ssid': "test", 'beacon_int': "2000" }
  154. if peer['listen_freq'] == "2412":
  155. params['channel'] = '1'
  156. elif peer['listen_freq'] == "2437":
  157. params['channel'] = '6'
  158. elif peer['listen_freq'] == "2462":
  159. params['channel'] = '11'
  160. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  161. hapd.set("ext_mgmt_frame_handling", "1")
  162. return addr0, bssid, hapd, int(params['channel'])
  163. def p2p_probe(hapd, src, chan=1):
  164. msg = {}
  165. msg['fc'] = MGMT_SUBTYPE_PROBE_REQ << 4
  166. msg['da'] = "ff:ff:ff:ff:ff:ff"
  167. msg['sa'] = src
  168. msg['bssid'] = "ff:ff:ff:ff:ff:ff"
  169. attrs = p2p_attr_listen_channel(chan=chan)
  170. msg['payload'] = ie_ssid("DIRECT-") + ie_supp_rates() + ie_p2p(attrs)
  171. hapd.mgmt_tx(msg)
  172. def parse_p2p_public_action(payload):
  173. pos = payload
  174. (category, action) = struct.unpack('BB', pos[0:2])
  175. if category != ACTION_CATEG_PUBLIC:
  176. return None
  177. if action != 9:
  178. return None
  179. pos = pos[2:]
  180. (oui1,oui2,oui3,subtype) = struct.unpack('BBBB', pos[0:4])
  181. if oui1 != 0x50 or oui2 != 0x6f or oui3 != 0x9a or subtype != 9:
  182. return None
  183. pos = pos[4:]
  184. (subtype,dialog_token) = struct.unpack('BB', pos[0:2])
  185. p2p = {}
  186. p2p['subtype'] = subtype
  187. p2p['dialog_token'] = dialog_token
  188. pos = pos[2:]
  189. p2p['elements'] = pos
  190. while len(pos) > 2:
  191. (id,elen) = struct.unpack('BB', pos[0:2])
  192. pos = pos[2:]
  193. if elen > len(pos):
  194. raise Exception("Truncated IE in P2P Public Action frame (elen=%d left=%d)" % (elen, len(pos)))
  195. if id == WLAN_EID_VENDOR_SPECIFIC:
  196. if elen < 4:
  197. raise Exception("Too short vendor specific IE in P2P Public Action frame (elen=%d)" % elen)
  198. (oui1,oui2,oui3,subtype) = struct.unpack('BBBB', pos[0:4])
  199. if oui1 == 0x50 and oui2 == 0x6f and oui3 == 0x9a and subtype == 9:
  200. if 'p2p' in p2p:
  201. p2p['p2p'] += pos[4:elen]
  202. else:
  203. p2p['p2p'] = pos[4:elen]
  204. if oui1 == 0x00 and oui2 == 0x50 and oui3 == 0xf2 and subtype == 4:
  205. p2p['wsc'] = pos[4:elen]
  206. pos = pos[elen:]
  207. if len(pos) > 0:
  208. raise Exception("Invalid element in P2P Public Action frame")
  209. if 'p2p' in p2p:
  210. p2p['p2p_attrs'] = {}
  211. pos = p2p['p2p']
  212. while len(pos) >= 3:
  213. (id,alen) = struct.unpack('<BH', pos[0:3])
  214. pos = pos[3:]
  215. if alen > len(pos):
  216. logger.info("P2P payload: " + binascii.hexlify(p2p['p2p']))
  217. raise Exception("Truncated P2P attribute in P2P Public Action frame (alen=%d left=%d p2p-payload=%d)" % (alen, len(pos), len(p2p['p2p'])))
  218. p2p['p2p_attrs'][id] = pos[0:alen]
  219. pos = pos[alen:]
  220. if P2P_ATTR_STATUS in p2p['p2p_attrs']:
  221. p2p['p2p_status'] = struct.unpack('B', p2p['p2p_attrs'][P2P_ATTR_STATUS])[0]
  222. if 'wsc' in p2p:
  223. p2p['wsc_attrs'] = {}
  224. pos = p2p['wsc']
  225. while len(pos) >= 4:
  226. (id,alen) = struct.unpack('>HH', pos[0:4])
  227. pos = pos[4:]
  228. if alen > len(pos):
  229. logger.info("WSC payload: " + binascii.hexlify(p2p['wsc']))
  230. raise Exception("Truncated WSC attribute in P2P Public Action frame (alen=%d left=%d wsc-payload=%d)" % (alen, len(pos), len(p2p['wsc'])))
  231. p2p['wsc_attrs'][id] = pos[0:alen]
  232. pos = pos[alen:]
  233. return p2p
  234. def test_p2p_msg_empty(dev, apdev):
  235. """P2P protocol test: empty P2P Public Action frame"""
  236. dst, src, hapd, channel = start_p2p(dev, apdev)
  237. msg = p2p_hdr(dst, src)
  238. hapd.mgmt_tx(msg)
  239. def test_p2p_msg_invitation_req(dev, apdev):
  240. """P2P protocol tests for invitation request processing"""
  241. dst, src, hapd, channel = start_p2p(dev, apdev)
  242. # Empty P2P Invitation Request (missing dialog token)
  243. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=None)
  244. hapd.mgmt_tx(msg)
  245. dialog_token = 0
  246. # Various p2p_parse() failure cases due to invalid attributes
  247. # Too short attribute header
  248. dialog_token += 1
  249. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  250. attrs = struct.pack("<BB", P2P_ATTR_CAPABILITY, 0)
  251. msg['payload'] += ie_p2p(attrs)
  252. hapd.mgmt_tx(msg)
  253. # Minimal attribute underflow
  254. dialog_token += 1
  255. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  256. attrs = struct.pack("<BH", P2P_ATTR_CAPABILITY, 1)
  257. msg['payload'] += ie_p2p(attrs)
  258. hapd.mgmt_tx(msg)
  259. # Large attribute underflow
  260. dialog_token += 1
  261. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  262. attrs = struct.pack("<BHB", P2P_ATTR_CAPABILITY, 0xffff, 1)
  263. msg['payload'] += ie_p2p(attrs)
  264. hapd.mgmt_tx(msg)
  265. # Too short Capability attribute
  266. dialog_token += 1
  267. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  268. attrs = struct.pack("<BHB", P2P_ATTR_CAPABILITY, 1, 0)
  269. msg['payload'] += ie_p2p(attrs)
  270. hapd.mgmt_tx(msg)
  271. # Too short Device ID attribute
  272. dialog_token += 1
  273. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  274. val = struct.unpack('5B', binascii.unhexlify("1122334455"))
  275. t = (P2P_ATTR_DEVICE_ID, 5) + val
  276. attrs = struct.pack('<BH5B', *t)
  277. msg['payload'] += ie_p2p(attrs)
  278. hapd.mgmt_tx(msg)
  279. # Too short GO Intent attribute
  280. dialog_token += 1
  281. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  282. attrs = struct.pack("<BH", P2P_ATTR_GROUP_OWNER_INTENT, 0)
  283. msg['payload'] += ie_p2p(attrs)
  284. hapd.mgmt_tx(msg)
  285. # Too short Status attribute
  286. dialog_token += 1
  287. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  288. attrs = struct.pack("<BH", P2P_ATTR_STATUS, 0)
  289. msg['payload'] += ie_p2p(attrs)
  290. hapd.mgmt_tx(msg)
  291. # null Listen channel and too short Listen Channel attribute
  292. dialog_token += 1
  293. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  294. attrs = struct.pack("<BH", P2P_ATTR_LISTEN_CHANNEL, 0)
  295. attrs += struct.pack("<BHB", P2P_ATTR_LISTEN_CHANNEL, 1, 0)
  296. msg['payload'] += ie_p2p(attrs)
  297. hapd.mgmt_tx(msg)
  298. # null Operating channel and too short Operating Channel attribute
  299. dialog_token += 1
  300. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  301. attrs = struct.pack("<BH", P2P_ATTR_OPERATING_CHANNEL, 0)
  302. attrs += struct.pack("<BHB", P2P_ATTR_OPERATING_CHANNEL, 1, 0)
  303. msg['payload'] += ie_p2p(attrs)
  304. hapd.mgmt_tx(msg)
  305. # Too short Channel List attribute
  306. dialog_token += 1
  307. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  308. attrs = struct.pack("<BHBB", P2P_ATTR_CHANNEL_LIST, 2, 1, 2)
  309. msg['payload'] += ie_p2p(attrs)
  310. hapd.mgmt_tx(msg)
  311. # Too short Device Info attribute
  312. dialog_token += 1
  313. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  314. attrs = struct.pack("<BHBB", P2P_ATTR_DEVICE_INFO, 2, 1, 2)
  315. msg['payload'] += ie_p2p(attrs)
  316. hapd.mgmt_tx(msg)
  317. # Truncated Secondary Device Types in Device Info attribute
  318. dialog_token += 1
  319. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  320. attrs = struct.pack("<BH6BH8BB", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1,
  321. 0, 0, 0, 0, 0, 0,
  322. 0,
  323. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22,
  324. 255)
  325. msg['payload'] += ie_p2p(attrs)
  326. hapd.mgmt_tx(msg)
  327. # Missing Device Name in Device Info attribute
  328. dialog_token += 1
  329. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  330. attrs = struct.pack("<BH6BH8BB8B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8,
  331. 0, 0, 0, 0, 0, 0,
  332. 0,
  333. 0, 0, 0, 0, 0, 0, 0, 0,
  334. 1,
  335. 1, 2, 3, 4, 5, 6, 7, 8)
  336. msg['payload'] += ie_p2p(attrs)
  337. hapd.mgmt_tx(msg)
  338. # Invalid Device Name header in Device Info attribute
  339. dialog_token += 1
  340. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  341. attrs = struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4,
  342. 0, 0, 0, 0, 0, 0,
  343. 0,
  344. 0, 0, 0, 0, 0, 0, 0, 0,
  345. 1,
  346. 1, 2, 3, 4, 5, 6, 7, 8,
  347. 0x11, 0x12, 0, 0)
  348. msg['payload'] += ie_p2p(attrs)
  349. hapd.mgmt_tx(msg)
  350. # Invalid Device Name header length in Device Info attribute
  351. dialog_token += 1
  352. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  353. attrs = struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4,
  354. 0, 0, 0, 0, 0, 0,
  355. 0,
  356. 0, 0, 0, 0, 0, 0, 0, 0,
  357. 1,
  358. 1, 2, 3, 4, 5, 6, 7, 8,
  359. 0x10, 0x11, 0xff, 0xff)
  360. msg['payload'] += ie_p2p(attrs)
  361. hapd.mgmt_tx(msg)
  362. # Invalid Device Name header length in Device Info attribute
  363. dialog_token += 1
  364. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  365. devname = 'A'
  366. attrs = struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4 + len(devname),
  367. 0, 0, 0, 0, 0, 0,
  368. 0,
  369. 0, 0, 0, 0, 0, 0, 0, 0,
  370. 1,
  371. 1, 2, 3, 4, 5, 6, 7, 8,
  372. 0x10, 0x11, 0, len(devname) + 1) + devname
  373. msg['payload'] += ie_p2p(attrs)
  374. hapd.mgmt_tx(msg)
  375. # Device Name filtering and too long Device Name in Device Info attribute
  376. dialog_token += 1
  377. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  378. attrs = struct.pack("<BH6BH8BB8B4B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4 + 4,
  379. 0, 0, 0, 0, 0, 0,
  380. 0,
  381. 0, 0, 0, 0, 0, 0, 0, 0,
  382. 1,
  383. 1, 2, 3, 4, 5, 6, 7, 8,
  384. 0x10, 0x11, 0, 4,
  385. 64, 9, 0, 64)
  386. devname = '123456789012345678901234567890123'
  387. attrs += struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4 + len(devname),
  388. 0, 0, 0, 0, 0, 0,
  389. 0,
  390. 0, 0, 0, 0, 0, 0, 0, 0,
  391. 1,
  392. 1, 2, 3, 4, 5, 6, 7, 8,
  393. 0x10, 0x11, 0, len(devname)) + devname
  394. msg['payload'] += ie_p2p(attrs)
  395. hapd.mgmt_tx(msg)
  396. # Too short Configuration Timeout attribute
  397. dialog_token += 1
  398. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  399. attrs = struct.pack("<BHB", P2P_ATTR_CONFIGURATION_TIMEOUT, 1, 1)
  400. msg['payload'] += ie_p2p(attrs)
  401. hapd.mgmt_tx(msg)
  402. # Too short Intended P2P Interface Address attribute
  403. dialog_token += 1
  404. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  405. attrs = struct.pack("<BHB", P2P_ATTR_INTENDED_INTERFACE_ADDR, 1, 1)
  406. msg['payload'] += ie_p2p(attrs)
  407. hapd.mgmt_tx(msg)
  408. # Too short P2P Group BSSID attribute
  409. dialog_token += 1
  410. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  411. attrs = struct.pack("<BHB", P2P_ATTR_GROUP_BSSID, 1, 1)
  412. msg['payload'] += ie_p2p(attrs)
  413. hapd.mgmt_tx(msg)
  414. # Too short P2P Group ID attribute
  415. dialog_token += 1
  416. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  417. attrs = struct.pack("<BHB", P2P_ATTR_GROUP_ID, 1, 1)
  418. msg['payload'] += ie_p2p(attrs)
  419. hapd.mgmt_tx(msg)
  420. # Too long P2P Group ID attribute
  421. dialog_token += 1
  422. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  423. attrs = struct.pack("<BH6B", P2P_ATTR_GROUP_ID, 6 + 33, 0, 0, 0, 0, 0, 0) + "123456789012345678901234567890123"
  424. msg['payload'] += ie_p2p(attrs)
  425. hapd.mgmt_tx(msg)
  426. # Too short Invitation Flags attribute
  427. dialog_token += 1
  428. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  429. attrs = struct.pack("<BH", P2P_ATTR_INVITATION_FLAGS, 0)
  430. msg['payload'] += ie_p2p(attrs)
  431. hapd.mgmt_tx(msg)
  432. # Valid and too short Manageability attribute
  433. dialog_token += 1
  434. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  435. attrs = p2p_attr_manageability()
  436. attrs += struct.pack("<BH", P2P_ATTR_MANAGEABILITY, 0)
  437. msg['payload'] += ie_p2p(attrs)
  438. hapd.mgmt_tx(msg)
  439. # Too short NoA attribute
  440. dialog_token += 1
  441. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  442. attrs = struct.pack("<BHB", P2P_ATTR_NOTICE_OF_ABSENCE, 1, 1)
  443. msg['payload'] += ie_p2p(attrs)
  444. hapd.mgmt_tx(msg)
  445. # Valid and too short Extended Listen Timing attributes
  446. dialog_token += 1
  447. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  448. attrs = p2p_attr_ext_listen_timing(period=100, interval=50)
  449. attrs += struct.pack("<BHBBB", P2P_ATTR_EXT_LISTEN_TIMING, 3, 0, 0, 0)
  450. msg['payload'] += ie_p2p(attrs)
  451. hapd.mgmt_tx(msg)
  452. # Valid and too short Minor Reason Code attributes
  453. dialog_token += 1
  454. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  455. attrs = p2p_attr_minor_reason_code(code=2)
  456. attrs += struct.pack("<BH", P2P_ATTR_MINOR_REASON_CODE, 0)
  457. msg['payload'] += ie_p2p(attrs)
  458. hapd.mgmt_tx(msg)
  459. # Unknown attribute and too short OOB GO Negotiation Channel attribute
  460. dialog_token += 1
  461. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  462. attrs = struct.pack("<BHB", 99, 1, 1)
  463. attrs += struct.pack("<BHB", P2P_ATTR_OOB_GO_NEG_CHANNEL, 1, 1)
  464. msg['payload'] += ie_p2p(attrs)
  465. hapd.mgmt_tx(msg)
  466. if hapd.mgmt_rx(timeout=0.5) is not None:
  467. raise Exception("Unexpected management frame received")
  468. dev[0].dump_monitor()
  469. dialog_token += 1
  470. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  471. attrs = p2p_attr_config_timeout()
  472. attrs += p2p_attr_invitation_flags()
  473. attrs += p2p_attr_operating_channel()
  474. attrs += p2p_attr_group_bssid(src)
  475. attrs += p2p_attr_channel_list()
  476. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  477. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  478. msg['payload'] += ie_p2p(attrs)
  479. hapd.mgmt_tx(msg)
  480. ev = dev[0].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
  481. if ev is None:
  482. raise Exception("Timeout on device found event")
  483. ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=5)
  484. if ev is None:
  485. raise Exception("Timeout on invitation event " + str(dialog_token))
  486. if hapd.mgmt_rx(timeout=1) is None:
  487. raise Exception("No invitation response " + str(dialog_token))
  488. time.sleep(0.1)
  489. dev[0].dump_monitor()
  490. dialog_token += 1
  491. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  492. attrs = p2p_attr_config_timeout()
  493. attrs += p2p_attr_invitation_flags()
  494. attrs += p2p_attr_operating_channel()
  495. attrs += p2p_attr_group_bssid(src)
  496. attrs += p2p_attr_channel_list()
  497. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  498. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  499. msg['payload'] += ie_p2p(attrs)
  500. hapd.mgmt_tx(msg)
  501. ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=5)
  502. if ev is None:
  503. raise Exception("Timeout on invitation event " + str(dialog_token))
  504. if hapd.mgmt_rx(timeout=1) is None:
  505. raise Exception("No invitation response " + str(dialog_token))
  506. time.sleep(0.1)
  507. dev[0].dump_monitor()
  508. dialog_token += 1
  509. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  510. #attrs = p2p_attr_config_timeout()
  511. attrs = p2p_attr_invitation_flags()
  512. attrs += p2p_attr_operating_channel()
  513. attrs += p2p_attr_group_bssid(src)
  514. attrs += p2p_attr_channel_list()
  515. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  516. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  517. msg['payload'] += ie_p2p(attrs)
  518. hapd.mgmt_tx(msg)
  519. if hapd.mgmt_rx(timeout=1) is None:
  520. raise Exception("No invitation response " + str(dialog_token))
  521. time.sleep(0.1)
  522. dev[0].dump_monitor()
  523. dialog_token += 1
  524. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  525. attrs = p2p_attr_config_timeout()
  526. #attrs = p2p_attr_invitation_flags()
  527. attrs += p2p_attr_operating_channel()
  528. attrs += p2p_attr_group_bssid(src)
  529. attrs += p2p_attr_channel_list()
  530. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  531. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  532. msg['payload'] += ie_p2p(attrs)
  533. hapd.mgmt_tx(msg)
  534. if hapd.mgmt_rx(timeout=1) is None:
  535. raise Exception("No invitation response " + str(dialog_token))
  536. time.sleep(0.1)
  537. dev[0].dump_monitor()
  538. dialog_token += 1
  539. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  540. attrs = p2p_attr_config_timeout()
  541. attrs = p2p_attr_invitation_flags()
  542. #attrs += p2p_attr_operating_channel()
  543. attrs += p2p_attr_group_bssid(src)
  544. attrs += p2p_attr_channel_list()
  545. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  546. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  547. msg['payload'] += ie_p2p(attrs)
  548. hapd.mgmt_tx(msg)
  549. if hapd.mgmt_rx(timeout=1) is None:
  550. raise Exception("No invitation response " + str(dialog_token))
  551. time.sleep(0.1)
  552. dev[0].dump_monitor()
  553. dialog_token += 1
  554. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  555. attrs = p2p_attr_config_timeout()
  556. attrs = p2p_attr_invitation_flags()
  557. attrs += p2p_attr_operating_channel()
  558. #attrs += p2p_attr_group_bssid(src)
  559. attrs += p2p_attr_channel_list()
  560. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  561. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  562. msg['payload'] += ie_p2p(attrs)
  563. hapd.mgmt_tx(msg)
  564. if hapd.mgmt_rx(timeout=1) is None:
  565. raise Exception("No invitation response " + str(dialog_token))
  566. time.sleep(0.1)
  567. dev[0].dump_monitor()
  568. dialog_token += 1
  569. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  570. attrs = p2p_attr_config_timeout()
  571. attrs = p2p_attr_invitation_flags()
  572. attrs += p2p_attr_operating_channel()
  573. attrs += p2p_attr_group_bssid(src)
  574. #attrs += p2p_attr_channel_list()
  575. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  576. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  577. msg['payload'] += ie_p2p(attrs)
  578. hapd.mgmt_tx(msg)
  579. if hapd.mgmt_rx(timeout=1) is None:
  580. raise Exception("No invitation response " + str(dialog_token))
  581. time.sleep(0.1)
  582. dev[0].dump_monitor()
  583. dialog_token += 1
  584. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  585. attrs = p2p_attr_config_timeout()
  586. attrs = p2p_attr_invitation_flags()
  587. attrs += p2p_attr_operating_channel()
  588. attrs += p2p_attr_group_bssid(src)
  589. attrs += p2p_attr_channel_list()
  590. #attrs += p2p_attr_group_id(src, "DIRECT-foo")
  591. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  592. msg['payload'] += ie_p2p(attrs)
  593. hapd.mgmt_tx(msg)
  594. if hapd.mgmt_rx(timeout=1) is None:
  595. raise Exception("No invitation response " + str(dialog_token))
  596. time.sleep(0.1)
  597. dev[0].dump_monitor()
  598. dialog_token += 1
  599. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  600. attrs = p2p_attr_config_timeout()
  601. attrs = p2p_attr_invitation_flags()
  602. attrs += p2p_attr_operating_channel()
  603. attrs += p2p_attr_group_bssid(src)
  604. attrs += p2p_attr_channel_list()
  605. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  606. #attrs += p2p_attr_device_info(src, config_methods=0x0108)
  607. msg['payload'] += ie_p2p(attrs)
  608. hapd.mgmt_tx(msg)
  609. if hapd.mgmt_rx(timeout=1) is None:
  610. raise Exception("No invitation response " + str(dialog_token))
  611. time.sleep(0.1)
  612. dev[0].dump_monitor()
  613. dialog_token += 1
  614. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  615. hapd.mgmt_tx(msg)
  616. if hapd.mgmt_rx(timeout=1) is None:
  617. raise Exception("No invitation response " + str(dialog_token))
  618. def test_p2p_msg_invitation_req_unknown(dev, apdev):
  619. """P2P protocol tests for invitation request from unknown peer"""
  620. dst, src, hapd, channel = start_p2p(dev, apdev)
  621. dialog_token = 0
  622. dialog_token += 1
  623. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  624. attrs = p2p_attr_config_timeout()
  625. attrs += p2p_attr_invitation_flags()
  626. attrs += p2p_attr_operating_channel()
  627. attrs += p2p_attr_group_bssid(src)
  628. attrs += p2p_attr_channel_list()
  629. #attrs += p2p_attr_group_id(src, "DIRECT-foo")
  630. #attrs += p2p_attr_device_info(src, config_methods=0x0108)
  631. msg['payload'] += ie_p2p(attrs)
  632. hapd.mgmt_tx(msg)
  633. ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=5)
  634. if ev is None:
  635. raise Exception("Timeout on invitation event " + str(dialog_token))
  636. if hapd.mgmt_rx(timeout=1) is None:
  637. raise Exception("No invitation response " + str(dialog_token))
  638. def test_p2p_msg_invitation_no_common_channels(dev, apdev):
  639. """P2P protocol tests for invitation request without common channels"""
  640. dst, src, hapd, channel = start_p2p(dev, apdev)
  641. dialog_token = 0
  642. dialog_token += 1
  643. msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
  644. attrs = p2p_attr_config_timeout()
  645. attrs += p2p_attr_invitation_flags()
  646. attrs += p2p_attr_operating_channel()
  647. attrs += p2p_attr_group_bssid(src)
  648. attrs += struct.pack("<BH3BBB", P2P_ATTR_CHANNEL_LIST, 5,
  649. 0x58, 0x58, 0x04,
  650. 81, 0)
  651. attrs += p2p_attr_group_id(src, "DIRECT-foo")
  652. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  653. msg['payload'] += ie_p2p(attrs)
  654. hapd.mgmt_tx(msg)
  655. if hapd.mgmt_rx(timeout=1) is None:
  656. raise Exception("No invitation response " + str(dialog_token))
  657. ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=0.1)
  658. if ev is not None:
  659. raise Exception("Unexpected invitation event")
  660. def test_p2p_msg_pd_req(dev, apdev):
  661. """P2P protocol tests for provision discovery request processing"""
  662. dst, src, hapd, channel = start_p2p(dev, apdev)
  663. dialog_token = 0
  664. # Too short attribute header
  665. dialog_token += 1
  666. msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
  667. attrs = struct.pack("<BB", P2P_ATTR_CAPABILITY, 0)
  668. msg['payload'] += ie_p2p(attrs)
  669. hapd.mgmt_tx(msg)
  670. if hapd.mgmt_rx(timeout=0.5) is not None:
  671. raise Exception("Unexpected management frame received")
  672. # No attributes
  673. dialog_token += 1
  674. msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
  675. attrs = ""
  676. msg['payload'] += ie_p2p(attrs)
  677. hapd.mgmt_tx(msg)
  678. if hapd.mgmt_rx(timeout=1) is None:
  679. raise Exception("No PD response " + str(dialog_token))
  680. # Valid request
  681. time.sleep(0.1)
  682. dialog_token += 1
  683. msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
  684. attrs = wsc_attr_config_methods(methods=0x1008)
  685. msg['payload'] += ie_wsc(attrs)
  686. attrs = p2p_attr_capability()
  687. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  688. msg['payload'] += ie_p2p(attrs)
  689. hapd.mgmt_tx(msg)
  690. ev = dev[0].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
  691. if ev is None:
  692. raise Exception("Timeout on device found event")
  693. ev = dev[0].wait_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=5)
  694. if ev is None:
  695. raise Exception("Timeout on PD event")
  696. if hapd.mgmt_rx(timeout=1) is None:
  697. raise Exception("No PD response " + str(dialog_token))
  698. # Unknown group
  699. time.sleep(0.1)
  700. dialog_token += 1
  701. msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
  702. attrs = wsc_attr_config_methods(methods=0x1008)
  703. msg['payload'] += ie_wsc(attrs)
  704. attrs = p2p_attr_capability()
  705. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  706. attrs += p2p_attr_group_id("02:02:02:02:02:02", "DIRECT-foo")
  707. msg['payload'] += ie_p2p(attrs)
  708. hapd.mgmt_tx(msg)
  709. if hapd.mgmt_rx(timeout=1) is None:
  710. raise Exception("No PD response " + str(dialog_token))
  711. ev = dev[0].wait_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=1)
  712. if ev is not None:
  713. raise Exception("Unexpected PD event")
  714. # Listen channel is not yet known
  715. if "FAIL" not in dev[0].global_request("P2P_PROV_DISC " + src + " display"):
  716. raise Exception("Unexpected P2P_PROV_DISC success")
  717. # Unknown peer
  718. if "FAIL" not in dev[0].global_request("P2P_PROV_DISC 02:03:04:05:06:07 display"):
  719. raise Exception("Unexpected P2P_PROV_DISC success (2)")
  720. def test_p2p_msg_pd(dev, apdev):
  721. """P2P protocol tests for provision discovery request processing (known)"""
  722. dst, src, hapd, channel = start_p2p(dev, apdev)
  723. dialog_token = 0
  724. p2p_probe(hapd, src, chan=channel)
  725. time.sleep(0.1)
  726. # Valid request
  727. dialog_token += 1
  728. msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
  729. attrs = wsc_attr_config_methods(methods=0x1008)
  730. msg['payload'] += ie_wsc(attrs)
  731. attrs = p2p_attr_capability()
  732. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  733. msg['payload'] += ie_p2p(attrs)
  734. hapd.mgmt_tx(msg)
  735. ev = dev[0].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
  736. if ev is None:
  737. raise Exception("Timeout on device found event")
  738. ev = dev[0].wait_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=5)
  739. if ev is None:
  740. raise Exception("Timeout on PD event")
  741. if hapd.mgmt_rx(timeout=1) is None:
  742. raise Exception("No PD response " + str(dialog_token))
  743. if "FAIL" in dev[0].global_request("P2P_PROV_DISC " + src + " display"):
  744. raise Exception("Unexpected P2P_PROV_DISC failure")
  745. frame = hapd.mgmt_rx(timeout=1)
  746. if frame is None:
  747. raise Exception("No PD request " + str(dialog_token))
  748. p2p = parse_p2p_public_action(frame['payload'])
  749. if p2p is None:
  750. raise Exception("Failed to parse PD request")
  751. # invalid dialog token
  752. msg = p2p_hdr_resp(dst, src, type=P2P_PROV_DISC_RESP,
  753. dialog_token=p2p['dialog_token'] + 1)
  754. hapd.mgmt_tx(msg)
  755. ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=0.1)
  756. if ev is not None:
  757. raise Exception("Unexpected PD result event")
  758. # valid dialog token
  759. msg = p2p_hdr_resp(dst, src, type=P2P_PROV_DISC_RESP,
  760. dialog_token=p2p['dialog_token'])
  761. hapd.mgmt_tx(msg)
  762. ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=5)
  763. if ev is None:
  764. raise Exception("Timeout on PD result event")
  765. # valid dialog token
  766. msg = p2p_hdr_resp(dst, src, type=P2P_PROV_DISC_RESP,
  767. dialog_token=p2p['dialog_token'])
  768. hapd.mgmt_tx(msg)
  769. ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=0.1)
  770. if ev is not None:
  771. raise Exception("Unexpected PD result event")
  772. def check_p2p_response(hapd, dialog_token, status):
  773. resp = hapd.mgmt_rx(timeout=1)
  774. if resp is None:
  775. raise Exception("No GO Neg Response " + str(dialog_token))
  776. p2p = parse_p2p_public_action(resp['payload'])
  777. if p2p is None:
  778. raise Exception("Not a P2P Public Action frame " + str(dialog_token))
  779. if dialog_token != p2p['dialog_token']:
  780. raise Exception("Unexpected dialog token in response")
  781. if p2p['p2p_status'] != status:
  782. raise Exception("Unexpected status code %s in response (expected %d)" % (p2p['p2p_status'], status))
  783. def test_p2p_msg_go_neg_both_start(dev, apdev):
  784. """P2P protocol test for simultaneous GO Neg initiation"""
  785. addr0 = dev[0].p2p_dev_addr()
  786. addr1 = dev[1].p2p_dev_addr()
  787. dev[0].p2p_listen()
  788. dev[1].discover_peer(addr0)
  789. dev[1].p2p_listen()
  790. dev[0].discover_peer(addr1)
  791. dev[0].p2p_listen()
  792. if "FAIL" in dev[0].request("SET ext_mgmt_frame_handling 1"):
  793. raise Exception("Failed to enable external management frame handling")
  794. if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
  795. raise Exception("Failed to enable external management frame handling")
  796. dev[0].request("P2P_CONNECT {} pbc".format(addr1))
  797. dev[1].request("P2P_CONNECT {} pbc".format(addr0))
  798. msg = dev[0].mgmt_rx()
  799. if msg is None:
  800. raise Exception("MGMT-RX timeout")
  801. msg = dev[1].mgmt_rx()
  802. if msg is None:
  803. raise Exception("MGMT-RX timeout(2)")
  804. if "FAIL" in dev[0].request("SET ext_mgmt_frame_handling 0"):
  805. raise Exception("Failed to disable external management frame handling")
  806. ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=2)
  807. if ev is not None:
  808. raise Exception("Unexpected GO Neg success")
  809. if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 0"):
  810. raise Exception("Failed to disable external management frame handling")
  811. ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=10)
  812. if ev is None:
  813. raise Exception("GO Neg did not succeed")
  814. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=5);
  815. if ev is None:
  816. raise Exception("Group formation not succeed")
  817. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=5);
  818. if ev is None:
  819. raise Exception("Group formation not succeed")
  820. def test_p2p_msg_go_neg_req(dev, apdev):
  821. """P2P protocol tests for invitation request from unknown peer"""
  822. dst, src, hapd, channel = start_p2p(dev, apdev)
  823. dialog_token = 0
  824. # invalid attribute
  825. dialog_token += 1
  826. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  827. attrs = struct.pack("<BB", P2P_ATTR_CAPABILITY, 0)
  828. msg['payload'] += ie_p2p(attrs)
  829. hapd.mgmt_tx(msg)
  830. frame = hapd.mgmt_rx(timeout=0.1)
  831. if frame is not None:
  832. print frame
  833. raise Exception("Unexpected GO Neg Response")
  834. # missing atributes
  835. dialog_token += 1
  836. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  837. attrs = p2p_attr_capability()
  838. attrs += p2p_attr_go_intent()
  839. attrs += p2p_attr_config_timeout()
  840. #attrs += p2p_attr_listen_channel()
  841. attrs += p2p_attr_ext_listen_timing()
  842. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  843. attrs += p2p_attr_channel_list()
  844. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  845. attrs += p2p_attr_operating_channel()
  846. msg['payload'] += ie_p2p(attrs)
  847. hapd.mgmt_tx(msg)
  848. if hapd.mgmt_rx(timeout=1) is None:
  849. raise Exception("No GO Neg Response " + str(dialog_token))
  850. time.sleep(0.1)
  851. dialog_token += 1
  852. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  853. attrs = p2p_attr_capability()
  854. attrs += p2p_attr_go_intent()
  855. attrs += p2p_attr_config_timeout()
  856. attrs += p2p_attr_listen_channel()
  857. attrs += p2p_attr_ext_listen_timing()
  858. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  859. attrs += p2p_attr_channel_list()
  860. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  861. #attrs += p2p_attr_operating_channel()
  862. msg['payload'] += ie_p2p(attrs)
  863. hapd.mgmt_tx(msg)
  864. if hapd.mgmt_rx(timeout=1) is None:
  865. raise Exception("No GO Neg Response " + str(dialog_token))
  866. time.sleep(0.1)
  867. dialog_token += 1
  868. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  869. attrs = p2p_attr_capability()
  870. attrs += p2p_attr_go_intent()
  871. attrs += p2p_attr_config_timeout()
  872. attrs += p2p_attr_listen_channel()
  873. attrs += p2p_attr_ext_listen_timing()
  874. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  875. #attrs += p2p_attr_channel_list()
  876. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  877. attrs += p2p_attr_operating_channel()
  878. msg['payload'] += ie_p2p(attrs)
  879. hapd.mgmt_tx(msg)
  880. if hapd.mgmt_rx(timeout=1) is None:
  881. raise Exception("No GO Neg Response " + str(dialog_token))
  882. time.sleep(0.1)
  883. dialog_token += 1
  884. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  885. attrs = p2p_attr_capability()
  886. attrs += p2p_attr_go_intent()
  887. attrs += p2p_attr_config_timeout()
  888. attrs += p2p_attr_listen_channel()
  889. attrs += p2p_attr_ext_listen_timing()
  890. #attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  891. attrs += p2p_attr_channel_list()
  892. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  893. attrs += p2p_attr_operating_channel()
  894. msg['payload'] += ie_p2p(attrs)
  895. hapd.mgmt_tx(msg)
  896. if hapd.mgmt_rx(timeout=1) is None:
  897. raise Exception("No GO Neg Response " + str(dialog_token))
  898. time.sleep(0.1)
  899. dialog_token += 1
  900. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  901. attrs = p2p_attr_capability()
  902. attrs += p2p_attr_go_intent()
  903. attrs += p2p_attr_config_timeout()
  904. attrs += p2p_attr_listen_channel()
  905. attrs += p2p_attr_ext_listen_timing()
  906. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  907. attrs += p2p_attr_channel_list()
  908. #attrs += p2p_attr_device_info(src, config_methods=0x0108)
  909. attrs += p2p_attr_operating_channel()
  910. msg['payload'] += ie_p2p(attrs)
  911. hapd.mgmt_tx(msg)
  912. if hapd.mgmt_rx(timeout=1) is None:
  913. raise Exception("No GO Neg Response " + str(dialog_token))
  914. time.sleep(0.1)
  915. # SA != P2P Device address
  916. dialog_token += 1
  917. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  918. attrs = p2p_attr_capability()
  919. attrs += p2p_attr_go_intent()
  920. attrs += p2p_attr_config_timeout()
  921. attrs += p2p_attr_listen_channel()
  922. attrs += p2p_attr_ext_listen_timing()
  923. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  924. attrs += p2p_attr_channel_list()
  925. attrs += p2p_attr_device_info("02:02:02:02:02:02", config_methods=0x0108)
  926. attrs += p2p_attr_operating_channel()
  927. msg['payload'] += ie_p2p(attrs)
  928. hapd.mgmt_tx(msg)
  929. if hapd.mgmt_rx(timeout=1) is None:
  930. raise Exception("No GO Neg Response " + str(dialog_token))
  931. time.sleep(0.1)
  932. # unexpected Status attribute
  933. dialog_token += 1
  934. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  935. attrs = p2p_attr_capability()
  936. attrs += p2p_attr_go_intent()
  937. attrs += p2p_attr_config_timeout()
  938. attrs += p2p_attr_listen_channel()
  939. attrs += p2p_attr_ext_listen_timing()
  940. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  941. attrs += p2p_attr_channel_list()
  942. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  943. attrs += p2p_attr_operating_channel()
  944. attrs += p2p_attr_status(status=P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
  945. msg['payload'] += ie_p2p(attrs)
  946. hapd.mgmt_tx(msg)
  947. if hapd.mgmt_rx(timeout=1) is None:
  948. raise Exception("No GO Neg Response(1) " + str(dialog_token))
  949. time.sleep(0.1)
  950. # valid (with workarounds) GO Neg Req
  951. dialog_token += 1
  952. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  953. #attrs = p2p_attr_capability()
  954. #attrs += p2p_attr_go_intent()
  955. #attrs += p2p_attr_config_timeout()
  956. attrs = p2p_attr_listen_channel()
  957. attrs += p2p_attr_ext_listen_timing()
  958. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  959. attrs += p2p_attr_channel_list()
  960. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  961. attrs += p2p_attr_operating_channel()
  962. msg['payload'] += ie_p2p(attrs)
  963. hapd.mgmt_tx(msg)
  964. check_p2p_response(hapd, dialog_token,
  965. P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
  966. ev = dev[0].wait_event(["P2P-GO-NEG-REQUEST"], timeout=1)
  967. if ev is None:
  968. raise Exception("Timeout on GO Neg event " + str(dialog_token))
  969. dev[0].request("P2P_CONNECT " + src + " 12345670 display auth")
  970. # ready - missing attributes (with workarounds) GO Neg Req
  971. time.sleep(0.1)
  972. dialog_token += 1
  973. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  974. #attrs = p2p_attr_capability()
  975. #attrs += p2p_attr_go_intent()
  976. #attrs += p2p_attr_config_timeout()
  977. attrs = p2p_attr_listen_channel()
  978. attrs += p2p_attr_ext_listen_timing()
  979. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  980. attrs += p2p_attr_channel_list()
  981. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  982. attrs += p2p_attr_operating_channel()
  983. msg['payload'] += ie_p2p(attrs)
  984. hapd.mgmt_tx(msg)
  985. if hapd.mgmt_rx(timeout=1) is None:
  986. raise Exception("No GO Neg Response " + str(dialog_token))
  987. # ready - invalid GO Intent GO Neg Req
  988. time.sleep(0.1)
  989. dialog_token += 1
  990. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  991. #attrs = p2p_attr_capability()
  992. attrs = p2p_attr_go_intent(go_intent=16)
  993. #attrs += p2p_attr_config_timeout()
  994. attrs += p2p_attr_listen_channel()
  995. attrs += p2p_attr_ext_listen_timing()
  996. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  997. attrs += p2p_attr_channel_list()
  998. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  999. attrs += p2p_attr_operating_channel()
  1000. msg['payload'] += ie_p2p(attrs)
  1001. hapd.mgmt_tx(msg)
  1002. check_p2p_response(hapd, dialog_token, P2P_SC_FAIL_INVALID_PARAMS)
  1003. # ready - invalid Channel List
  1004. time.sleep(0.1)
  1005. dialog_token += 1
  1006. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  1007. attrs = p2p_attr_capability()
  1008. attrs += p2p_attr_go_intent()
  1009. attrs += p2p_attr_config_timeout()
  1010. attrs += p2p_attr_listen_channel()
  1011. attrs += p2p_attr_ext_listen_timing()
  1012. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  1013. attrs += struct.pack("<BH3BBB11B", P2P_ATTR_CHANNEL_LIST, 16,
  1014. 0x58, 0x58, 0x04,
  1015. 81, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
  1016. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  1017. attrs += p2p_attr_operating_channel()
  1018. msg['payload'] += ie_p2p(attrs)
  1019. hapd.mgmt_tx(msg)
  1020. check_p2p_response(hapd, dialog_token, P2P_SC_FAIL_NO_COMMON_CHANNELS)
  1021. # ready - invalid GO Neg Req (unsupported Device Password ID)
  1022. time.sleep(0.1)
  1023. dialog_token += 1
  1024. msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
  1025. attrs = p2p_attr_capability()
  1026. attrs += p2p_attr_go_intent()
  1027. attrs += p2p_attr_config_timeout()
  1028. attrs += p2p_attr_listen_channel()
  1029. attrs += p2p_attr_ext_listen_timing()
  1030. attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
  1031. # very long channel list
  1032. attrs += struct.pack("<BH3BBB11B30B", P2P_ATTR_CHANNEL_LIST, 46,
  1033. 0x58, 0x58, 0x04,
  1034. 81, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  1035. 1, 1, 1, 2, 1, 2, 3, 1, 3, 4, 1, 4, 5, 1, 5,
  1036. 6, 1, 6, 7, 1, 7, 8, 1, 8, 9, 1, 9, 10, 1, 10)
  1037. attrs += p2p_attr_device_info(src, config_methods=0x0108)
  1038. attrs += p2p_attr_operating_channel()
  1039. msg['payload'] += ie_p2p(attrs)
  1040. hapd.mgmt_tx(msg)
  1041. check_p2p_response(hapd, dialog_token, P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD)
  1042. def test_p2p_msg_go_neg_req_reject(dev, apdev):
  1043. """P2P protocol tests for user reject incorrectly in GO Neg Req"""
  1044. addr0 = dev[0].p2p_dev_addr()
  1045. addr1 = dev[1].p2p_dev_addr()
  1046. dev[0].p2p_listen()
  1047. dev[1].discover_peer(addr0)
  1048. dev[1].group_request("P2P_CONNECT " + addr0 + " pbc")
  1049. ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=10)
  1050. if ev is None:
  1051. raise Exception("Timeout on GO Neg Req")
  1052. peer = dev[0].get_peer(addr1)
  1053. dev[0].p2p_stop_find()
  1054. msg = p2p_hdr(addr1, addr0, type=P2P_GO_NEG_REQ, dialog_token=123)
  1055. attrs = p2p_attr_capability()
  1056. attrs += p2p_attr_status(status=P2P_SC_FAIL_REJECTED_BY_USER)
  1057. attrs += p2p_attr_go_intent()
  1058. attrs += p2p_attr_config_timeout()
  1059. attrs += p2p_attr_listen_channel()
  1060. attrs += p2p_attr_ext_listen_timing()
  1061. attrs += p2p_attr_intended_interface_addr(addr0)
  1062. attrs += p2p_attr_channel_list()
  1063. attrs += p2p_attr_device_info(addr0, config_methods=0x0108)
  1064. attrs += p2p_attr_operating_channel()
  1065. msg['payload'] += ie_p2p(attrs)
  1066. for i in range(0, 20):
  1067. if "FAIL" in dev[0].request("MGMT_TX {} {} freq={} wait_time=10 no_cck=1 action={}".format(addr1, addr1, peer['listen_freq'], binascii.hexlify(msg['payload']))):
  1068. raise Exception("Failed to send Action frame")
  1069. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  1070. if ev is None:
  1071. raise Exception("Timeout on MGMT-TX-STATUS")
  1072. if "result=SUCCESS" in ev:
  1073. break
  1074. time.sleep(0.01)
  1075. if "result=SUCCESS" not in ev:
  1076. raise Exception("Peer did not ack GO Neg Req")
  1077. ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=5)
  1078. if ev is None:
  1079. raise Exception("GO Negotiation failure not reported")
  1080. if "status=%d" % P2P_SC_FAIL_REJECTED_BY_USER not in ev:
  1081. raise Exception("Unexpected failure reason: " + ev)