test_gas.py 24 KB

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