test_gas.py 35 KB

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