test_gas.py 28 KB

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