test_gas.py 31 KB

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