test_gas.py 32 KB

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