test_gas.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. def expect_gas_result(dev, result, status=None):
  220. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  221. if ev is None:
  222. raise Exception("GAS query timed out")
  223. if "result=" + result not in ev:
  224. raise Exception("Unexpected GAS query result")
  225. if status and "status_code=" + str(status) + ' ' not in ev:
  226. raise Exception("Unexpected GAS status code")
  227. def anqp_get(dev, bssid, id):
  228. dev.request("ANQP_GET " + bssid + " " + str(id))
  229. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  230. if ev is None:
  231. raise Exception("GAS query start timed out")
  232. def test_gas_timeout(dev, apdev):
  233. """GAS timeout"""
  234. hapd = start_ap(apdev[0])
  235. bssid = apdev[0]['bssid']
  236. dev[0].scan(freq="2412")
  237. hapd.set("ext_mgmt_frame_handling", "1")
  238. anqp_get(dev[0], bssid, 263)
  239. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  240. if ev is None:
  241. raise Exception("MGMT RX wait timed out")
  242. expect_gas_result(dev[0], "TIMEOUT")
  243. MGMT_SUBTYPE_ACTION = 13
  244. ACTION_CATEG_PUBLIC = 4
  245. GAS_INITIAL_REQUEST = 10
  246. GAS_INITIAL_RESPONSE = 11
  247. GAS_COMEBACK_REQUEST = 12
  248. GAS_COMEBACK_RESPONSE = 13
  249. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  250. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  251. def anqp_adv_proto():
  252. return struct.pack('BBBB', 108, 2, 127, 0)
  253. def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
  254. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  255. dialog_token, status_code, comeback_delay) + anqp_adv_proto()
  256. def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
  257. if more:
  258. id |= 0x80
  259. if bogus_adv_proto:
  260. adv = struct.pack('BBBB', 108, 2, 127, 1)
  261. else:
  262. adv = anqp_adv_proto()
  263. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  264. dialog_token, status_code, id, comeback_delay) + adv
  265. def gas_rx(hapd):
  266. count = 0
  267. while count < 30:
  268. count = count + 1
  269. query = hapd.mgmt_rx()
  270. if query is None:
  271. raise Exception("Action frame not received")
  272. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  273. continue
  274. payload = query['payload']
  275. if len(payload) < 2:
  276. continue
  277. (category, action) = struct.unpack('BB', payload[0:2])
  278. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  279. continue
  280. return query
  281. raise Exception("No Action frame received")
  282. def parse_gas(payload):
  283. pos = payload
  284. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  285. if category != ACTION_CATEG_PUBLIC:
  286. return None
  287. if action not in GAS_ACTIONS:
  288. return None
  289. gas = {}
  290. gas['action'] = action
  291. pos = pos[3:]
  292. if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
  293. return None
  294. gas['dialog_token'] = dialog_token
  295. return gas
  296. def action_response(req):
  297. resp = {}
  298. resp['fc'] = req['fc']
  299. resp['da'] = req['sa']
  300. resp['sa'] = req['da']
  301. resp['bssid'] = req['bssid']
  302. return resp
  303. def send_gas_resp(hapd, resp):
  304. hapd.mgmt_tx(resp)
  305. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  306. if ev is None:
  307. raise Exception("Missing TX status for GAS response")
  308. if "ok=1" not in ev:
  309. raise Exception("GAS response not acknowledged")
  310. def test_gas_invalid_response_type(dev, apdev):
  311. """GAS invalid response type"""
  312. hapd = start_ap(apdev[0])
  313. bssid = apdev[0]['bssid']
  314. dev[0].scan(freq="2412")
  315. hapd.set("ext_mgmt_frame_handling", "1")
  316. anqp_get(dev[0], bssid, 263)
  317. query = gas_rx(hapd)
  318. gas = parse_gas(query['payload'])
  319. resp = action_response(query)
  320. # GAS Comeback Response instead of GAS Initial Response
  321. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  322. send_gas_resp(hapd, resp)
  323. # station drops the invalid frame, so this needs to result in GAS timeout
  324. expect_gas_result(dev[0], "TIMEOUT")
  325. def test_gas_failure_status_code(dev, apdev):
  326. """GAS failure status code"""
  327. hapd = start_ap(apdev[0])
  328. bssid = apdev[0]['bssid']
  329. dev[0].scan(freq="2412")
  330. hapd.set("ext_mgmt_frame_handling", "1")
  331. anqp_get(dev[0], bssid, 263)
  332. query = gas_rx(hapd)
  333. gas = parse_gas(query['payload'])
  334. resp = action_response(query)
  335. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  336. send_gas_resp(hapd, resp)
  337. expect_gas_result(dev[0], "FAILURE")
  338. def test_gas_malformed(dev, apdev):
  339. """GAS malformed response frames"""
  340. hapd = start_ap(apdev[0])
  341. bssid = apdev[0]['bssid']
  342. dev[0].scan(freq="2412")
  343. hapd.set("ext_mgmt_frame_handling", "1")
  344. anqp_get(dev[0], bssid, 263)
  345. query = gas_rx(hapd)
  346. gas = parse_gas(query['payload'])
  347. resp = action_response(query)
  348. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  349. GAS_COMEBACK_RESPONSE,
  350. gas['dialog_token'], 0)
  351. hapd.mgmt_tx(resp)
  352. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  353. GAS_COMEBACK_RESPONSE,
  354. gas['dialog_token'], 0, 0)
  355. hapd.mgmt_tx(resp)
  356. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  357. gas['dialog_token'], 0, 0)
  358. resp['payload'] = hdr + struct.pack('B', 108)
  359. hapd.mgmt_tx(resp)
  360. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  361. hapd.mgmt_tx(resp)
  362. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  363. hapd.mgmt_tx(resp)
  364. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  365. hapd.mgmt_tx(resp)
  366. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  367. hapd.mgmt_tx(resp)
  368. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  369. hapd.mgmt_tx(resp)
  370. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  371. hapd.mgmt_tx(resp)
  372. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  373. hapd.mgmt_tx(resp)
  374. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  375. hapd.mgmt_tx(resp)
  376. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  377. hapd.mgmt_tx(resp)
  378. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  379. hapd.mgmt_tx(resp)
  380. # Station drops invalid frames, but the last of the responses is valid from
  381. # GAS view point even though it has an extra octet in the end and the ANQP
  382. # part of the response is not valid. This is reported as successfulyl
  383. # completed GAS exchange.
  384. expect_gas_result(dev[0], "SUCCESS")
  385. def init_gas(hapd, bssid, dev):
  386. anqp_get(dev, bssid, 263)
  387. query = gas_rx(hapd)
  388. gas = parse_gas(query['payload'])
  389. dialog_token = gas['dialog_token']
  390. resp = action_response(query)
  391. resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
  392. send_gas_resp(hapd, resp)
  393. query = gas_rx(hapd)
  394. gas = parse_gas(query['payload'])
  395. if gas['action'] != GAS_COMEBACK_REQUEST:
  396. raise Exception("Unexpected request action")
  397. if gas['dialog_token'] != dialog_token:
  398. raise Exception("Unexpected dialog token change")
  399. return query, dialog_token
  400. def test_gas_malformed_comeback_resp(dev, apdev):
  401. """GAS malformed comeback response frames"""
  402. hapd = start_ap(apdev[0])
  403. bssid = apdev[0]['bssid']
  404. dev[0].scan(freq="2412")
  405. hapd.set("ext_mgmt_frame_handling", "1")
  406. logger.debug("Non-zero status code in comeback response")
  407. query, dialog_token = init_gas(hapd, bssid, dev[0])
  408. resp = action_response(query)
  409. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
  410. send_gas_resp(hapd, resp)
  411. expect_gas_result(dev[0], "FAILURE", status=2)
  412. logger.debug("Different advertisement protocol in comeback response")
  413. query, dialog_token = init_gas(hapd, bssid, dev[0])
  414. resp = action_response(query)
  415. resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
  416. send_gas_resp(hapd, resp)
  417. expect_gas_result(dev[0], "PEER_ERROR")
  418. logger.debug("Non-zero frag id and comeback delay in comeback response")
  419. query, dialog_token = init_gas(hapd, bssid, dev[0])
  420. resp = action_response(query)
  421. resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
  422. send_gas_resp(hapd, resp)
  423. expect_gas_result(dev[0], "PEER_ERROR")
  424. logger.debug("Unexpected frag id in comeback response")
  425. query, dialog_token = init_gas(hapd, bssid, dev[0])
  426. resp = action_response(query)
  427. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  428. send_gas_resp(hapd, resp)
  429. expect_gas_result(dev[0], "PEER_ERROR")
  430. logger.debug("Empty fragment and replay in comeback response")
  431. query, dialog_token = init_gas(hapd, bssid, dev[0])
  432. resp = action_response(query)
  433. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  434. send_gas_resp(hapd, resp)
  435. query = gas_rx(hapd)
  436. gas = parse_gas(query['payload'])
  437. if gas['action'] != GAS_COMEBACK_REQUEST:
  438. raise Exception("Unexpected request action")
  439. if gas['dialog_token'] != dialog_token:
  440. raise Exception("Unexpected dialog token change")
  441. resp = action_response(query)
  442. resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
  443. send_gas_resp(hapd, resp)
  444. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  445. send_gas_resp(hapd, resp)
  446. expect_gas_result(dev[0], "SUCCESS")
  447. logger.debug("Unexpected initial response when waiting for comeback response")
  448. query, dialog_token = init_gas(hapd, bssid, dev[0])
  449. resp = action_response(query)
  450. resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
  451. send_gas_resp(hapd, resp)
  452. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  453. if ev is not None:
  454. raise Exception("Unexpected management frame")
  455. expect_gas_result(dev[0], "TIMEOUT")
  456. logger.debug("Too short comeback response")
  457. query, dialog_token = init_gas(hapd, bssid, dev[0])
  458. resp = action_response(query)
  459. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  460. GAS_COMEBACK_RESPONSE, dialog_token, 0)
  461. send_gas_resp(hapd, resp)
  462. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  463. if ev is not None:
  464. raise Exception("Unexpected management frame")
  465. expect_gas_result(dev[0], "TIMEOUT")
  466. logger.debug("Too short comeback response(2)")
  467. query, dialog_token = init_gas(hapd, bssid, dev[0])
  468. resp = action_response(query)
  469. resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
  470. GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
  471. 0)
  472. send_gas_resp(hapd, resp)
  473. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  474. if ev is not None:
  475. raise Exception("Unexpected management frame")
  476. expect_gas_result(dev[0], "TIMEOUT")
  477. logger.debug("Maximum comeback response fragment claiming more fragments")
  478. query, dialog_token = init_gas(hapd, bssid, dev[0])
  479. resp = action_response(query)
  480. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  481. send_gas_resp(hapd, resp)
  482. for i in range(1, 129):
  483. query = gas_rx(hapd)
  484. gas = parse_gas(query['payload'])
  485. if gas['action'] != GAS_COMEBACK_REQUEST:
  486. raise Exception("Unexpected request action")
  487. if gas['dialog_token'] != dialog_token:
  488. raise Exception("Unexpected dialog token change")
  489. resp = action_response(query)
  490. resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
  491. send_gas_resp(hapd, resp)
  492. expect_gas_result(dev[0], "PEER_ERROR")
  493. def test_gas_comeback_resp_additional_delay(dev, apdev):
  494. """GAS comeback response requesting additional delay"""
  495. hapd = start_ap(apdev[0])
  496. bssid = apdev[0]['bssid']
  497. dev[0].scan(freq="2412")
  498. hapd.set("ext_mgmt_frame_handling", "1")
  499. query, dialog_token = init_gas(hapd, bssid, dev[0])
  500. for i in range(0, 2):
  501. resp = action_response(query)
  502. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
  503. send_gas_resp(hapd, resp)
  504. query = gas_rx(hapd)
  505. gas = parse_gas(query['payload'])
  506. if gas['action'] != GAS_COMEBACK_REQUEST:
  507. raise Exception("Unexpected request action")
  508. if gas['dialog_token'] != dialog_token:
  509. raise Exception("Unexpected dialog token change")
  510. resp = action_response(query)
  511. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
  512. send_gas_resp(hapd, resp)
  513. expect_gas_result(dev[0], "SUCCESS")
  514. def test_gas_unknown_adv_proto(dev, apdev):
  515. """Unknown advertisement protocol id"""
  516. bssid = apdev[0]['bssid']
  517. params = hs20_ap_params()
  518. params['hessid'] = bssid
  519. hostapd.add_ap(apdev[0]['ifname'], params)
  520. dev[0].scan(freq="2412")
  521. req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
  522. if "FAIL" in req:
  523. raise Exception("GAS query request rejected")
  524. expect_gas_result(dev[0], "FAILURE", "59")
  525. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  526. if ev is None:
  527. raise Exception("GAS query timed out")
  528. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  529. res = re.split(exp, ev)
  530. if len(res) < 6:
  531. raise Exception("Could not parse GAS-RESPONSE-INFO")
  532. if res[2] != bssid:
  533. raise Exception("Unexpected BSSID in response")
  534. status = res[4]
  535. if status != "59":
  536. raise Exception("Unexpected GAS-RESPONSE-INFO status")