test_gas.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. # GAS tests
  2. # Copyright (c) 2013, Qualcomm Atheros, Inc.
  3. # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
  4. #
  5. # This software may be distributed under the terms of the BSD license.
  6. # See README for more details.
  7. import time
  8. import binascii
  9. import logging
  10. logger = logging.getLogger()
  11. import re
  12. import struct
  13. import hostapd
  14. def hs20_ap_params():
  15. params = hostapd.wpa2_params(ssid="test-gas")
  16. params['wpa_key_mgmt'] = "WPA-EAP"
  17. params['ieee80211w'] = "1"
  18. params['ieee8021x'] = "1"
  19. params['auth_server_addr'] = "127.0.0.1"
  20. params['auth_server_port'] = "1812"
  21. params['auth_server_shared_secret'] = "radius"
  22. params['interworking'] = "1"
  23. params['access_network_type'] = "14"
  24. params['internet'] = "1"
  25. params['asra'] = "0"
  26. params['esr'] = "0"
  27. params['uesa'] = "0"
  28. params['venue_group'] = "7"
  29. params['venue_type'] = "1"
  30. params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
  31. params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
  32. "fedcba" ]
  33. params['domain_name'] = "example.com,another.example.com"
  34. params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
  35. "0,another.example.com" ]
  36. params['anqp_3gpp_cell_net'] = "244,91"
  37. params['network_auth_type'] = "02http://www.example.com/redirect/me/here/"
  38. params['ipaddr_type_availability'] = "14"
  39. params['hs20'] = "1"
  40. params['hs20_oper_friendly_name'] = [ "eng:Example operator", "fin:Esimerkkioperaattori" ]
  41. params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
  42. params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
  43. params['hs20_operating_class'] = "5173"
  44. return params
  45. def start_ap(ap):
  46. params = hs20_ap_params()
  47. params['hessid'] = ap['bssid']
  48. hostapd.add_ap(ap['ifname'], params)
  49. return hostapd.Hostapd(ap['ifname'])
  50. def get_gas_response(dev, bssid, info, allow_fetch_failure=False):
  51. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  52. res = re.split(exp, info)
  53. if len(res) < 6:
  54. raise Exception("Could not parse GAS-RESPONSE-INFO")
  55. if res[2] != bssid:
  56. raise Exception("Unexpected BSSID in response")
  57. token = res[3]
  58. status = res[4]
  59. if status != "0":
  60. raise Exception("GAS query failed")
  61. resp_len = res[5]
  62. if resp_len == "-1":
  63. raise Exception("GAS query reported invalid response length")
  64. if int(resp_len) > 2000:
  65. raise Exception("Unexpected long GAS response")
  66. resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
  67. if "FAIL" in resp:
  68. if allow_fetch_failure:
  69. logger.debug("GAS response was not available anymore")
  70. return
  71. raise Exception("Could not fetch GAS response")
  72. if len(resp) != int(resp_len) * 2:
  73. raise Exception("Unexpected GAS response length")
  74. logger.debug("GAS response: " + resp)
  75. def test_gas_generic(dev, apdev):
  76. """Generic GAS query"""
  77. bssid = apdev[0]['bssid']
  78. params = hs20_ap_params()
  79. params['hessid'] = bssid
  80. hostapd.add_ap(apdev[0]['ifname'], params)
  81. dev[0].scan(freq="2412")
  82. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  83. if "FAIL" in req:
  84. raise Exception("GAS query request rejected")
  85. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  86. if ev is None:
  87. raise Exception("GAS query timed out")
  88. get_gas_response(dev[0], bssid, ev)
  89. def test_gas_concurrent_scan(dev, apdev):
  90. """Generic GAS queries with concurrent scan operation"""
  91. bssid = apdev[0]['bssid']
  92. params = hs20_ap_params()
  93. params['hessid'] = bssid
  94. hostapd.add_ap(apdev[0]['ifname'], params)
  95. # get BSS entry available to allow GAS query
  96. dev[0].scan(freq="2412")
  97. logger.info("Request concurrent operations")
  98. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  99. if "FAIL" in req:
  100. raise Exception("GAS query request rejected")
  101. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
  102. if "FAIL" in req:
  103. raise Exception("GAS query request rejected")
  104. dev[0].scan(no_wait=True)
  105. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
  106. if "FAIL" in req:
  107. raise Exception("GAS query request rejected")
  108. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
  109. if "FAIL" in req:
  110. raise Exception("GAS query request rejected")
  111. responses = 0
  112. for i in range(0, 5):
  113. ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
  114. timeout=10)
  115. if ev is None:
  116. raise Exception("Operation timed out")
  117. if "GAS-RESPONSE-INFO" in ev:
  118. responses = responses + 1
  119. get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
  120. if responses != 4:
  121. raise Exception("Unexpected number of GAS responses")
  122. def test_gas_concurrent_connect(dev, apdev):
  123. """Generic GAS queries with concurrent connection operation"""
  124. bssid = apdev[0]['bssid']
  125. params = hs20_ap_params()
  126. params['hessid'] = bssid
  127. hostapd.add_ap(apdev[0]['ifname'], params)
  128. dev[0].scan(freq="2412")
  129. logger.debug("Start concurrent connect and GAS request")
  130. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  131. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  132. password="password", phase2="auth=MSCHAPV2",
  133. ca_cert="auth_serv/ca.pem", wait_connect=False,
  134. scan_freq="2412")
  135. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  136. if "FAIL" in req:
  137. raise Exception("GAS query request rejected")
  138. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  139. timeout=20)
  140. if ev is None:
  141. raise Exception("Operation timed out")
  142. if "CTRL-EVENT-CONNECTED" not in ev:
  143. raise Exception("Unexpected operation order")
  144. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  145. timeout=20)
  146. if ev is None:
  147. raise Exception("Operation timed out")
  148. if "GAS-RESPONSE-INFO" not in ev:
  149. raise Exception("Unexpected operation order")
  150. get_gas_response(dev[0], bssid, ev)
  151. dev[0].request("DISCONNECT")
  152. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
  153. if ev is None:
  154. raise Exception("Disconnection timed out")
  155. logger.debug("Wait six seconds for expiration of connect-without-scan")
  156. time.sleep(6)
  157. dev[0].dump_monitor()
  158. logger.debug("Start concurrent GAS request and connect")
  159. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  160. if "FAIL" in req:
  161. raise Exception("GAS query request rejected")
  162. dev[0].request("RECONNECT")
  163. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  164. if ev is None:
  165. raise Exception("Operation timed out")
  166. get_gas_response(dev[0], bssid, ev)
  167. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
  168. if ev is None:
  169. raise Exception("No new scan results reported")
  170. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  171. if ev is None:
  172. raise Exception("Operation timed out")
  173. if "CTRL-EVENT-CONNECTED" not in ev:
  174. raise Exception("Unexpected operation order")
  175. def test_gas_fragment(dev, apdev):
  176. """GAS fragmentation"""
  177. hapd = start_ap(apdev[0])
  178. hapd.set("gas_frag_limit", "50")
  179. dev[0].scan(freq="2412")
  180. dev[0].request("FETCH_ANQP")
  181. for i in range(0, 13):
  182. ev = dev[0].wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
  183. if ev is None:
  184. raise Exception("Operation timed out")
  185. def test_gas_comeback_delay(dev, apdev):
  186. """GAS fragmentation"""
  187. hapd = start_ap(apdev[0])
  188. hapd.set("gas_comeback_delay", "500")
  189. dev[0].scan(freq="2412")
  190. dev[0].request("FETCH_ANQP")
  191. for i in range(0, 6):
  192. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  193. if ev is None:
  194. raise Exception("Operation timed out")
  195. def test_gas_anqp_get(dev, apdev):
  196. """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
  197. hapd = start_ap(apdev[0])
  198. bssid = apdev[0]['bssid']
  199. dev[0].scan(freq="2412")
  200. dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4")
  201. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  202. if ev is None:
  203. raise Exception("GAS query start timed out")
  204. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  205. if ev is None:
  206. raise Exception("GAS query timed out")
  207. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  208. if ev is None or "Venue Name" not in ev:
  209. raise Exception("Did not receive Venue Name")
  210. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  211. if ev is None or "Domain Name list" not in ev:
  212. raise Exception("Did not receive Domain Name list")
  213. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  214. if ev is None or "Operator Friendly Name" not in ev:
  215. raise Exception("Did not receive Operator Friendly Name")
  216. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  217. if ev is None or "WAN Metrics" not in ev:
  218. raise Exception("Did not receive WAN Metrics")
  219. dev[0].request("HS20_ANQP_GET " + bssid + " 3,4")
  220. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  221. if ev is None or "Operator Friendly Name" not in ev:
  222. raise Exception("Did not receive Operator Friendly Name")
  223. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  224. if ev is None or "WAN Metrics" not in ev:
  225. raise Exception("Did not receive WAN Metrics")
  226. def expect_gas_result(dev, result, status=None):
  227. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  228. if ev is None:
  229. raise Exception("GAS query timed out")
  230. if "result=" + result not in ev:
  231. raise Exception("Unexpected GAS query result")
  232. if status and "status_code=" + str(status) + ' ' not in ev:
  233. raise Exception("Unexpected GAS status code")
  234. def anqp_get(dev, bssid, id):
  235. dev.request("ANQP_GET " + bssid + " " + str(id))
  236. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  237. if ev is None:
  238. raise Exception("GAS query start timed out")
  239. def test_gas_timeout(dev, apdev):
  240. """GAS timeout"""
  241. hapd = start_ap(apdev[0])
  242. bssid = apdev[0]['bssid']
  243. dev[0].scan(freq="2412")
  244. hapd.set("ext_mgmt_frame_handling", "1")
  245. anqp_get(dev[0], bssid, 263)
  246. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  247. if ev is None:
  248. raise Exception("MGMT RX wait timed out")
  249. expect_gas_result(dev[0], "TIMEOUT")
  250. MGMT_SUBTYPE_ACTION = 13
  251. ACTION_CATEG_PUBLIC = 4
  252. GAS_INITIAL_REQUEST = 10
  253. GAS_INITIAL_RESPONSE = 11
  254. GAS_COMEBACK_REQUEST = 12
  255. GAS_COMEBACK_RESPONSE = 13
  256. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  257. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  258. def anqp_adv_proto():
  259. return struct.pack('BBBB', 108, 2, 127, 0)
  260. def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
  261. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  262. dialog_token, status_code, comeback_delay) + anqp_adv_proto()
  263. def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
  264. if more:
  265. id |= 0x80
  266. if bogus_adv_proto:
  267. adv = struct.pack('BBBB', 108, 2, 127, 1)
  268. else:
  269. adv = anqp_adv_proto()
  270. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  271. dialog_token, status_code, id, comeback_delay) + adv
  272. def gas_rx(hapd):
  273. count = 0
  274. while count < 30:
  275. count = count + 1
  276. query = hapd.mgmt_rx()
  277. if query is None:
  278. raise Exception("Action frame not received")
  279. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  280. continue
  281. payload = query['payload']
  282. if len(payload) < 2:
  283. continue
  284. (category, action) = struct.unpack('BB', payload[0:2])
  285. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  286. continue
  287. return query
  288. raise Exception("No Action frame received")
  289. def parse_gas(payload):
  290. pos = payload
  291. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  292. if category != ACTION_CATEG_PUBLIC:
  293. return None
  294. if action not in GAS_ACTIONS:
  295. return None
  296. gas = {}
  297. gas['action'] = action
  298. pos = pos[3:]
  299. if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
  300. return None
  301. gas['dialog_token'] = dialog_token
  302. return gas
  303. def action_response(req):
  304. resp = {}
  305. resp['fc'] = req['fc']
  306. resp['da'] = req['sa']
  307. resp['sa'] = req['da']
  308. resp['bssid'] = req['bssid']
  309. return resp
  310. def send_gas_resp(hapd, resp):
  311. hapd.mgmt_tx(resp)
  312. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  313. if ev is None:
  314. raise Exception("Missing TX status for GAS response")
  315. if "ok=1" not in ev:
  316. raise Exception("GAS response not acknowledged")
  317. def test_gas_invalid_response_type(dev, apdev):
  318. """GAS invalid response type"""
  319. hapd = start_ap(apdev[0])
  320. bssid = apdev[0]['bssid']
  321. dev[0].scan(freq="2412")
  322. hapd.set("ext_mgmt_frame_handling", "1")
  323. anqp_get(dev[0], bssid, 263)
  324. query = gas_rx(hapd)
  325. gas = parse_gas(query['payload'])
  326. resp = action_response(query)
  327. # GAS Comeback Response instead of GAS Initial Response
  328. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  329. send_gas_resp(hapd, resp)
  330. # station drops the invalid frame, so this needs to result in GAS timeout
  331. expect_gas_result(dev[0], "TIMEOUT")
  332. def test_gas_failure_status_code(dev, apdev):
  333. """GAS failure status code"""
  334. hapd = start_ap(apdev[0])
  335. bssid = apdev[0]['bssid']
  336. dev[0].scan(freq="2412")
  337. hapd.set("ext_mgmt_frame_handling", "1")
  338. anqp_get(dev[0], bssid, 263)
  339. query = gas_rx(hapd)
  340. gas = parse_gas(query['payload'])
  341. resp = action_response(query)
  342. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  343. send_gas_resp(hapd, resp)
  344. expect_gas_result(dev[0], "FAILURE")
  345. def test_gas_malformed(dev, apdev):
  346. """GAS malformed response frames"""
  347. hapd = start_ap(apdev[0])
  348. bssid = apdev[0]['bssid']
  349. dev[0].scan(freq="2412")
  350. hapd.set("ext_mgmt_frame_handling", "1")
  351. anqp_get(dev[0], bssid, 263)
  352. query = gas_rx(hapd)
  353. gas = parse_gas(query['payload'])
  354. resp = action_response(query)
  355. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  356. GAS_COMEBACK_RESPONSE,
  357. gas['dialog_token'], 0)
  358. hapd.mgmt_tx(resp)
  359. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  360. GAS_COMEBACK_RESPONSE,
  361. gas['dialog_token'], 0, 0)
  362. hapd.mgmt_tx(resp)
  363. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  364. gas['dialog_token'], 0, 0)
  365. resp['payload'] = hdr + struct.pack('B', 108)
  366. hapd.mgmt_tx(resp)
  367. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  368. hapd.mgmt_tx(resp)
  369. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  370. hapd.mgmt_tx(resp)
  371. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  372. hapd.mgmt_tx(resp)
  373. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  374. hapd.mgmt_tx(resp)
  375. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  376. hapd.mgmt_tx(resp)
  377. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  378. hapd.mgmt_tx(resp)
  379. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  380. hapd.mgmt_tx(resp)
  381. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  382. hapd.mgmt_tx(resp)
  383. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  384. hapd.mgmt_tx(resp)
  385. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  386. hapd.mgmt_tx(resp)
  387. # Station drops invalid frames, but the last of the responses is valid from
  388. # GAS view point even though it has an extra octet in the end and the ANQP
  389. # part of the response is not valid. This is reported as successfulyl
  390. # completed GAS exchange.
  391. expect_gas_result(dev[0], "SUCCESS")
  392. def init_gas(hapd, bssid, dev):
  393. anqp_get(dev, bssid, 263)
  394. query = gas_rx(hapd)
  395. gas = parse_gas(query['payload'])
  396. dialog_token = gas['dialog_token']
  397. resp = action_response(query)
  398. resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
  399. send_gas_resp(hapd, resp)
  400. query = gas_rx(hapd)
  401. gas = parse_gas(query['payload'])
  402. if gas['action'] != GAS_COMEBACK_REQUEST:
  403. raise Exception("Unexpected request action")
  404. if gas['dialog_token'] != dialog_token:
  405. raise Exception("Unexpected dialog token change")
  406. return query, dialog_token
  407. def test_gas_malformed_comeback_resp(dev, apdev):
  408. """GAS malformed comeback response frames"""
  409. hapd = start_ap(apdev[0])
  410. bssid = apdev[0]['bssid']
  411. dev[0].scan(freq="2412")
  412. hapd.set("ext_mgmt_frame_handling", "1")
  413. logger.debug("Non-zero status code in comeback response")
  414. query, dialog_token = init_gas(hapd, bssid, dev[0])
  415. resp = action_response(query)
  416. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
  417. send_gas_resp(hapd, resp)
  418. expect_gas_result(dev[0], "FAILURE", status=2)
  419. logger.debug("Different advertisement protocol in comeback response")
  420. query, dialog_token = init_gas(hapd, bssid, dev[0])
  421. resp = action_response(query)
  422. resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
  423. send_gas_resp(hapd, resp)
  424. expect_gas_result(dev[0], "PEER_ERROR")
  425. logger.debug("Non-zero frag id and comeback delay in comeback response")
  426. query, dialog_token = init_gas(hapd, bssid, dev[0])
  427. resp = action_response(query)
  428. resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
  429. send_gas_resp(hapd, resp)
  430. expect_gas_result(dev[0], "PEER_ERROR")
  431. logger.debug("Unexpected frag id in comeback response")
  432. query, dialog_token = init_gas(hapd, bssid, dev[0])
  433. resp = action_response(query)
  434. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  435. send_gas_resp(hapd, resp)
  436. expect_gas_result(dev[0], "PEER_ERROR")
  437. logger.debug("Empty fragment and replay in comeback response")
  438. query, dialog_token = init_gas(hapd, bssid, dev[0])
  439. resp = action_response(query)
  440. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  441. send_gas_resp(hapd, resp)
  442. query = gas_rx(hapd)
  443. gas = parse_gas(query['payload'])
  444. if gas['action'] != GAS_COMEBACK_REQUEST:
  445. raise Exception("Unexpected request action")
  446. if gas['dialog_token'] != dialog_token:
  447. raise Exception("Unexpected dialog token change")
  448. resp = action_response(query)
  449. resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
  450. send_gas_resp(hapd, resp)
  451. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  452. send_gas_resp(hapd, resp)
  453. expect_gas_result(dev[0], "SUCCESS")
  454. logger.debug("Unexpected initial response when waiting for comeback response")
  455. query, dialog_token = init_gas(hapd, bssid, dev[0])
  456. resp = action_response(query)
  457. resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
  458. send_gas_resp(hapd, resp)
  459. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  460. if ev is not None:
  461. raise Exception("Unexpected management frame")
  462. expect_gas_result(dev[0], "TIMEOUT")
  463. logger.debug("Too short comeback response")
  464. query, dialog_token = init_gas(hapd, bssid, dev[0])
  465. resp = action_response(query)
  466. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  467. GAS_COMEBACK_RESPONSE, dialog_token, 0)
  468. send_gas_resp(hapd, resp)
  469. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  470. if ev is not None:
  471. raise Exception("Unexpected management frame")
  472. expect_gas_result(dev[0], "TIMEOUT")
  473. logger.debug("Too short comeback response(2)")
  474. query, dialog_token = init_gas(hapd, bssid, dev[0])
  475. resp = action_response(query)
  476. resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
  477. GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
  478. 0)
  479. send_gas_resp(hapd, resp)
  480. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  481. if ev is not None:
  482. raise Exception("Unexpected management frame")
  483. expect_gas_result(dev[0], "TIMEOUT")
  484. logger.debug("Maximum comeback response fragment claiming more fragments")
  485. query, dialog_token = init_gas(hapd, bssid, dev[0])
  486. resp = action_response(query)
  487. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  488. send_gas_resp(hapd, resp)
  489. for i in range(1, 129):
  490. query = gas_rx(hapd)
  491. gas = parse_gas(query['payload'])
  492. if gas['action'] != GAS_COMEBACK_REQUEST:
  493. raise Exception("Unexpected request action")
  494. if gas['dialog_token'] != dialog_token:
  495. raise Exception("Unexpected dialog token change")
  496. resp = action_response(query)
  497. resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
  498. send_gas_resp(hapd, resp)
  499. expect_gas_result(dev[0], "PEER_ERROR")
  500. def test_gas_comeback_resp_additional_delay(dev, apdev):
  501. """GAS comeback response requesting additional delay"""
  502. hapd = start_ap(apdev[0])
  503. bssid = apdev[0]['bssid']
  504. dev[0].scan(freq="2412")
  505. hapd.set("ext_mgmt_frame_handling", "1")
  506. query, dialog_token = init_gas(hapd, bssid, dev[0])
  507. for i in range(0, 2):
  508. resp = action_response(query)
  509. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
  510. send_gas_resp(hapd, resp)
  511. query = gas_rx(hapd)
  512. gas = parse_gas(query['payload'])
  513. if gas['action'] != GAS_COMEBACK_REQUEST:
  514. raise Exception("Unexpected request action")
  515. if gas['dialog_token'] != dialog_token:
  516. raise Exception("Unexpected dialog token change")
  517. resp = action_response(query)
  518. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
  519. send_gas_resp(hapd, resp)
  520. expect_gas_result(dev[0], "SUCCESS")
  521. def test_gas_unknown_adv_proto(dev, apdev):
  522. """Unknown advertisement protocol id"""
  523. bssid = apdev[0]['bssid']
  524. params = hs20_ap_params()
  525. params['hessid'] = bssid
  526. hostapd.add_ap(apdev[0]['ifname'], params)
  527. dev[0].scan(freq="2412")
  528. req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
  529. if "FAIL" in req:
  530. raise Exception("GAS query request rejected")
  531. expect_gas_result(dev[0], "FAILURE", "59")
  532. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  533. if ev is None:
  534. raise Exception("GAS query timed out")
  535. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  536. res = re.split(exp, ev)
  537. if len(res) < 6:
  538. raise Exception("Could not parse GAS-RESPONSE-INFO")
  539. if res[2] != bssid:
  540. raise Exception("Unexpected BSSID in response")
  541. status = res[4]
  542. if status != "59":
  543. raise Exception("Unexpected GAS-RESPONSE-INFO status")