test_gas.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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, skip_with_fips
  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. skip_with_fips(dev[0])
  161. bssid = apdev[0]['bssid']
  162. params = hs20_ap_params()
  163. params['hessid'] = bssid
  164. hostapd.add_ap(apdev[0]['ifname'], params)
  165. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  166. logger.debug("Start concurrent connect and GAS request")
  167. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  168. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  169. password="password", phase2="auth=MSCHAPV2",
  170. ca_cert="auth_serv/ca.pem", wait_connect=False,
  171. scan_freq="2412")
  172. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  173. if "FAIL" in req:
  174. raise Exception("GAS query request rejected")
  175. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  176. timeout=20)
  177. if ev is None:
  178. raise Exception("Operation timed out")
  179. if "CTRL-EVENT-CONNECTED" not in ev:
  180. raise Exception("Unexpected operation order")
  181. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  182. timeout=20)
  183. if ev is None:
  184. raise Exception("Operation timed out")
  185. if "GAS-RESPONSE-INFO" not in ev:
  186. raise Exception("Unexpected operation order")
  187. get_gas_response(dev[0], bssid, ev)
  188. dev[0].request("DISCONNECT")
  189. dev[0].wait_disconnected(timeout=5)
  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_connected(timeout=20, error="Operation tiemd out")
  206. if "CTRL-EVENT-CONNECTED" not in ev:
  207. raise Exception("Unexpected operation order")
  208. def test_gas_fragment(dev, apdev):
  209. """GAS fragmentation"""
  210. hapd = start_ap(apdev[0])
  211. hapd.set("gas_frag_limit", "50")
  212. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  213. dev[0].request("FETCH_ANQP")
  214. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=1)
  215. if ev is None:
  216. raise Exception("No GAS-QUERY-DONE event")
  217. if "result=SUCCESS" not in ev:
  218. raise Exception("Unexpected GAS result: " + ev)
  219. for i in range(0, 13):
  220. ev = dev[0].wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
  221. if ev is None:
  222. raise Exception("Operation timed out")
  223. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=1)
  224. if ev is None:
  225. raise Exception("No ANQP-QUERY-DONE event")
  226. if "result=SUCCESS" not in ev:
  227. raise Exception("Unexpected ANQP result: " + ev)
  228. def test_gas_comeback_delay(dev, apdev):
  229. """GAS fragmentation"""
  230. hapd = start_ap(apdev[0])
  231. hapd.set("gas_comeback_delay", "500")
  232. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  233. dev[0].request("FETCH_ANQP")
  234. for i in range(0, 6):
  235. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  236. if ev is None:
  237. raise Exception("Operation timed out")
  238. def test_gas_stop_fetch_anqp(dev, apdev):
  239. """Stop FETCH_ANQP operation"""
  240. hapd = start_ap(apdev[0])
  241. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  242. hapd.set("ext_mgmt_frame_handling", "1")
  243. dev[0].request("FETCH_ANQP")
  244. dev[0].request("STOP_FETCH_ANQP")
  245. hapd.set("ext_mgmt_frame_handling", "0")
  246. ev = dev[0].wait_event(["RX-ANQP", "GAS-QUERY-DONE"], timeout=10)
  247. if ev is None:
  248. raise Exception("GAS-QUERY-DONE timed out")
  249. if "RX-ANQP" in ev:
  250. raise Exception("Unexpected ANQP response received")
  251. def test_gas_anqp_get(dev, apdev):
  252. """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
  253. hapd = start_ap(apdev[0])
  254. bssid = apdev[0]['bssid']
  255. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  256. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
  257. raise Exception("ANQP_GET command failed")
  258. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  259. if ev is None:
  260. raise Exception("GAS query start timed out")
  261. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  262. if ev is None:
  263. raise Exception("GAS query timed out")
  264. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  265. if ev is None or "Venue Name" not in ev:
  266. raise Exception("Did not receive Venue Name")
  267. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  268. if ev is None or "Domain Name list" not in ev:
  269. raise Exception("Did not receive Domain Name list")
  270. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  271. if ev is None or "Operator Friendly Name" not in ev:
  272. raise Exception("Did not receive Operator Friendly Name")
  273. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  274. if ev is None or "WAN Metrics" not in ev:
  275. raise Exception("Did not receive WAN Metrics")
  276. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  277. if ev is None:
  278. raise Exception("ANQP-QUERY-DONE event not seen")
  279. if "result=SUCCESS" not in ev:
  280. raise Exception("Unexpected result: " + ev)
  281. if "OK" not in dev[0].request("HS20_ANQP_GET " + bssid + " 3,4"):
  282. raise Exception("ANQP_GET command failed")
  283. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  284. if ev is None or "Operator Friendly Name" not in ev:
  285. raise Exception("Did not receive Operator Friendly Name")
  286. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  287. if ev is None or "WAN Metrics" not in ev:
  288. raise Exception("Did not receive WAN Metrics")
  289. cmds = [ "",
  290. "foo",
  291. "00:11:22:33:44:55 258,hs20:-1",
  292. "00:11:22:33:44:55 258,hs20:0",
  293. "00:11:22:33:44:55 258,hs20:32",
  294. "00:11:22:33:44:55 hs20:-1",
  295. "00:11:22:33:44:55 hs20:0",
  296. "00:11:22:33:44:55 hs20: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("ANQP_GET " + cmd):
  302. raise Exception("Invalid ANQP_GET accepted")
  303. cmds = [ "",
  304. "foo",
  305. "00:11:22:33:44:55 -1",
  306. "00:11:22:33:44:55 0",
  307. "00:11:22:33:44:55 32",
  308. "00:11:22:33:44:55",
  309. "00:11:22:33:44:55 ",
  310. "00:11:22:33:44:55 0" ]
  311. for cmd in cmds:
  312. if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
  313. raise Exception("Invalid HS20_ANQP_GET accepted")
  314. def expect_gas_result(dev, result, status=None):
  315. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  316. if ev is None:
  317. raise Exception("GAS query timed out")
  318. if "result=" + result not in ev:
  319. raise Exception("Unexpected GAS query result")
  320. if status and "status_code=" + str(status) + ' ' not in ev:
  321. raise Exception("Unexpected GAS status code")
  322. def anqp_get(dev, bssid, id):
  323. if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
  324. raise Exception("ANQP_GET command failed")
  325. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  326. if ev is None:
  327. raise Exception("GAS query start timed out")
  328. def test_gas_timeout(dev, apdev):
  329. """GAS timeout"""
  330. hapd = start_ap(apdev[0])
  331. bssid = apdev[0]['bssid']
  332. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  333. hapd.set("ext_mgmt_frame_handling", "1")
  334. anqp_get(dev[0], bssid, 263)
  335. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  336. if ev is None:
  337. raise Exception("MGMT RX wait timed out")
  338. expect_gas_result(dev[0], "TIMEOUT")
  339. MGMT_SUBTYPE_ACTION = 13
  340. ACTION_CATEG_PUBLIC = 4
  341. GAS_INITIAL_REQUEST = 10
  342. GAS_INITIAL_RESPONSE = 11
  343. GAS_COMEBACK_REQUEST = 12
  344. GAS_COMEBACK_RESPONSE = 13
  345. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  346. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  347. def anqp_adv_proto():
  348. return struct.pack('BBBB', 108, 2, 127, 0)
  349. def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
  350. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  351. dialog_token, status_code, comeback_delay) + anqp_adv_proto()
  352. def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
  353. if more:
  354. id |= 0x80
  355. if bogus_adv_proto:
  356. adv = struct.pack('BBBB', 108, 2, 127, 1)
  357. else:
  358. adv = anqp_adv_proto()
  359. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  360. dialog_token, status_code, id, comeback_delay) + adv
  361. def gas_rx(hapd):
  362. count = 0
  363. while count < 30:
  364. count = count + 1
  365. query = hapd.mgmt_rx()
  366. if query is None:
  367. raise Exception("Action frame not received")
  368. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  369. continue
  370. payload = query['payload']
  371. if len(payload) < 2:
  372. continue
  373. (category, action) = struct.unpack('BB', payload[0:2])
  374. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  375. continue
  376. return query
  377. raise Exception("No Action frame received")
  378. def parse_gas(payload):
  379. pos = payload
  380. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  381. if category != ACTION_CATEG_PUBLIC:
  382. return None
  383. if action not in GAS_ACTIONS:
  384. return None
  385. gas = {}
  386. gas['action'] = action
  387. pos = pos[3:]
  388. if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
  389. return None
  390. gas['dialog_token'] = dialog_token
  391. if action == GAS_INITIAL_RESPONSE:
  392. if len(pos) < 4:
  393. return None
  394. (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
  395. gas['status_code'] = status_code
  396. gas['comeback_delay'] = comeback_delay
  397. if action == GAS_COMEBACK_RESPONSE:
  398. if len(pos) < 5:
  399. return None
  400. (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
  401. gas['status_code'] = status_code
  402. gas['frag'] = frag
  403. gas['comeback_delay'] = comeback_delay
  404. return gas
  405. def action_response(req):
  406. resp = {}
  407. resp['fc'] = req['fc']
  408. resp['da'] = req['sa']
  409. resp['sa'] = req['da']
  410. resp['bssid'] = req['bssid']
  411. return resp
  412. def send_gas_resp(hapd, resp):
  413. hapd.mgmt_tx(resp)
  414. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  415. if ev is None:
  416. raise Exception("Missing TX status for GAS response")
  417. if "ok=1" not in ev:
  418. raise Exception("GAS response not acknowledged")
  419. def test_gas_invalid_response_type(dev, apdev):
  420. """GAS invalid response type"""
  421. hapd = start_ap(apdev[0])
  422. bssid = apdev[0]['bssid']
  423. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  424. hapd.set("ext_mgmt_frame_handling", "1")
  425. anqp_get(dev[0], bssid, 263)
  426. query = gas_rx(hapd)
  427. gas = parse_gas(query['payload'])
  428. resp = action_response(query)
  429. # GAS Comeback Response instead of GAS Initial Response
  430. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  431. send_gas_resp(hapd, resp)
  432. # station drops the invalid frame, so this needs to result in GAS timeout
  433. expect_gas_result(dev[0], "TIMEOUT")
  434. def test_gas_failure_status_code(dev, apdev):
  435. """GAS failure status code"""
  436. hapd = start_ap(apdev[0])
  437. bssid = apdev[0]['bssid']
  438. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  439. hapd.set("ext_mgmt_frame_handling", "1")
  440. anqp_get(dev[0], bssid, 263)
  441. query = gas_rx(hapd)
  442. gas = parse_gas(query['payload'])
  443. resp = action_response(query)
  444. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  445. send_gas_resp(hapd, resp)
  446. expect_gas_result(dev[0], "FAILURE")
  447. def test_gas_malformed(dev, apdev):
  448. """GAS malformed response frames"""
  449. hapd = start_ap(apdev[0])
  450. bssid = apdev[0]['bssid']
  451. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  452. hapd.set("ext_mgmt_frame_handling", "1")
  453. anqp_get(dev[0], bssid, 263)
  454. query = gas_rx(hapd)
  455. gas = parse_gas(query['payload'])
  456. resp = action_response(query)
  457. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  458. GAS_COMEBACK_RESPONSE,
  459. gas['dialog_token'], 0)
  460. hapd.mgmt_tx(resp)
  461. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  462. GAS_COMEBACK_RESPONSE,
  463. gas['dialog_token'], 0, 0)
  464. hapd.mgmt_tx(resp)
  465. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  466. gas['dialog_token'], 0, 0)
  467. resp['payload'] = hdr + struct.pack('B', 108)
  468. hapd.mgmt_tx(resp)
  469. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  470. hapd.mgmt_tx(resp)
  471. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  472. hapd.mgmt_tx(resp)
  473. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  474. hapd.mgmt_tx(resp)
  475. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  476. hapd.mgmt_tx(resp)
  477. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  478. hapd.mgmt_tx(resp)
  479. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  480. hapd.mgmt_tx(resp)
  481. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  482. hapd.mgmt_tx(resp)
  483. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  484. hapd.mgmt_tx(resp)
  485. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  486. hapd.mgmt_tx(resp)
  487. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  488. hapd.mgmt_tx(resp)
  489. # Station drops invalid frames, but the last of the responses is valid from
  490. # GAS view point even though it has an extra octet in the end and the ANQP
  491. # part of the response is not valid. This is reported as successfully
  492. # completed GAS exchange.
  493. expect_gas_result(dev[0], "SUCCESS")
  494. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  495. if ev is None:
  496. raise Exception("ANQP-QUERY-DONE not reported")
  497. if "result=INVALID_FRAME" not in ev:
  498. raise Exception("Unexpected result: " + ev)
  499. def init_gas(hapd, bssid, dev):
  500. anqp_get(dev, bssid, 263)
  501. query = gas_rx(hapd)
  502. gas = parse_gas(query['payload'])
  503. dialog_token = gas['dialog_token']
  504. resp = action_response(query)
  505. resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
  506. send_gas_resp(hapd, resp)
  507. query = gas_rx(hapd)
  508. gas = parse_gas(query['payload'])
  509. if gas['action'] != GAS_COMEBACK_REQUEST:
  510. raise Exception("Unexpected request action")
  511. if gas['dialog_token'] != dialog_token:
  512. raise Exception("Unexpected dialog token change")
  513. return query, dialog_token
  514. def test_gas_malformed_comeback_resp(dev, apdev):
  515. """GAS malformed comeback response frames"""
  516. hapd = start_ap(apdev[0])
  517. bssid = apdev[0]['bssid']
  518. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  519. hapd.set("ext_mgmt_frame_handling", "1")
  520. logger.debug("Non-zero status code in comeback response")
  521. query, dialog_token = init_gas(hapd, bssid, dev[0])
  522. resp = action_response(query)
  523. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
  524. send_gas_resp(hapd, resp)
  525. expect_gas_result(dev[0], "FAILURE", status=2)
  526. logger.debug("Different advertisement protocol in comeback response")
  527. query, dialog_token = init_gas(hapd, bssid, dev[0])
  528. resp = action_response(query)
  529. resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
  530. send_gas_resp(hapd, resp)
  531. expect_gas_result(dev[0], "PEER_ERROR")
  532. logger.debug("Non-zero frag id and comeback delay in comeback response")
  533. query, dialog_token = init_gas(hapd, bssid, dev[0])
  534. resp = action_response(query)
  535. resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
  536. send_gas_resp(hapd, resp)
  537. expect_gas_result(dev[0], "PEER_ERROR")
  538. logger.debug("Unexpected frag id in comeback response")
  539. query, dialog_token = init_gas(hapd, bssid, dev[0])
  540. resp = action_response(query)
  541. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  542. send_gas_resp(hapd, resp)
  543. expect_gas_result(dev[0], "PEER_ERROR")
  544. logger.debug("Empty fragment and replay in comeback response")
  545. query, dialog_token = init_gas(hapd, bssid, dev[0])
  546. resp = action_response(query)
  547. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  548. send_gas_resp(hapd, resp)
  549. query = gas_rx(hapd)
  550. gas = parse_gas(query['payload'])
  551. if gas['action'] != GAS_COMEBACK_REQUEST:
  552. raise Exception("Unexpected request action")
  553. if gas['dialog_token'] != dialog_token:
  554. raise Exception("Unexpected dialog token change")
  555. resp = action_response(query)
  556. resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
  557. send_gas_resp(hapd, resp)
  558. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  559. send_gas_resp(hapd, resp)
  560. expect_gas_result(dev[0], "SUCCESS")
  561. logger.debug("Unexpected initial response when waiting for comeback response")
  562. query, dialog_token = init_gas(hapd, bssid, dev[0])
  563. resp = action_response(query)
  564. resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
  565. send_gas_resp(hapd, resp)
  566. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  567. if ev is not None:
  568. raise Exception("Unexpected management frame")
  569. expect_gas_result(dev[0], "TIMEOUT")
  570. logger.debug("Too short comeback response")
  571. query, dialog_token = init_gas(hapd, bssid, dev[0])
  572. resp = action_response(query)
  573. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  574. GAS_COMEBACK_RESPONSE, dialog_token, 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("Too short comeback response(2)")
  581. query, dialog_token = init_gas(hapd, bssid, dev[0])
  582. resp = action_response(query)
  583. resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
  584. GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
  585. 0)
  586. send_gas_resp(hapd, resp)
  587. ev = hapd.wait_event(["MGMT-RX"], timeout=1)
  588. if ev is not None:
  589. raise Exception("Unexpected management frame")
  590. expect_gas_result(dev[0], "TIMEOUT")
  591. logger.debug("Maximum comeback response fragment claiming more fragments")
  592. query, dialog_token = init_gas(hapd, bssid, dev[0])
  593. resp = action_response(query)
  594. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  595. send_gas_resp(hapd, resp)
  596. for i in range(1, 129):
  597. query = gas_rx(hapd)
  598. gas = parse_gas(query['payload'])
  599. if gas['action'] != GAS_COMEBACK_REQUEST:
  600. raise Exception("Unexpected request action")
  601. if gas['dialog_token'] != dialog_token:
  602. raise Exception("Unexpected dialog token change")
  603. resp = action_response(query)
  604. resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
  605. send_gas_resp(hapd, resp)
  606. expect_gas_result(dev[0], "PEER_ERROR")
  607. def test_gas_comeback_resp_additional_delay(dev, apdev):
  608. """GAS comeback response requesting additional delay"""
  609. hapd = start_ap(apdev[0])
  610. bssid = apdev[0]['bssid']
  611. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  612. hapd.set("ext_mgmt_frame_handling", "1")
  613. query, dialog_token = init_gas(hapd, bssid, dev[0])
  614. for i in range(0, 2):
  615. resp = action_response(query)
  616. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
  617. send_gas_resp(hapd, resp)
  618. query = gas_rx(hapd)
  619. gas = parse_gas(query['payload'])
  620. if gas['action'] != GAS_COMEBACK_REQUEST:
  621. raise Exception("Unexpected request action")
  622. if gas['dialog_token'] != dialog_token:
  623. raise Exception("Unexpected dialog token change")
  624. resp = action_response(query)
  625. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
  626. send_gas_resp(hapd, resp)
  627. expect_gas_result(dev[0], "SUCCESS")
  628. def test_gas_unknown_adv_proto(dev, apdev):
  629. """Unknown advertisement protocol id"""
  630. bssid = apdev[0]['bssid']
  631. params = hs20_ap_params()
  632. params['hessid'] = bssid
  633. hostapd.add_ap(apdev[0]['ifname'], params)
  634. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  635. req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
  636. if "FAIL" in req:
  637. raise Exception("GAS query request rejected")
  638. expect_gas_result(dev[0], "FAILURE", "59")
  639. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  640. if ev is None:
  641. raise Exception("GAS query timed out")
  642. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  643. res = re.split(exp, ev)
  644. if len(res) < 6:
  645. raise Exception("Could not parse GAS-RESPONSE-INFO")
  646. if res[2] != bssid:
  647. raise Exception("Unexpected BSSID in response")
  648. status = res[4]
  649. if status != "59":
  650. raise Exception("Unexpected GAS-RESPONSE-INFO status")
  651. def test_gas_max_pending(dev, apdev):
  652. """GAS and maximum pending query limit"""
  653. hapd = start_ap(apdev[0])
  654. hapd.set("gas_frag_limit", "50")
  655. bssid = apdev[0]['bssid']
  656. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  657. wpas.interface_add("wlan5")
  658. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  659. raise Exception("Failed to set listen channel")
  660. if "OK" not in wpas.p2p_listen():
  661. raise Exception("Failed to start listen state")
  662. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  663. raise Exception("Failed to enable external management frame handling")
  664. anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
  665. gas = struct.pack('<H', len(anqp_query)) + anqp_query
  666. for dialog_token in range(1, 10):
  667. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
  668. dialog_token) + anqp_adv_proto() + gas
  669. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  670. if "OK" not in wpas.request(req):
  671. raise Exception("Could not send management frame")
  672. resp = wpas.mgmt_rx()
  673. if resp is None:
  674. raise Exception("MGMT-RX timeout")
  675. if 'payload' not in resp:
  676. raise Exception("Missing payload")
  677. gresp = parse_gas(resp['payload'])
  678. if gresp['dialog_token'] != dialog_token:
  679. raise Exception("Dialog token mismatch")
  680. status_code = gresp['status_code']
  681. if dialog_token < 9 and status_code != 0:
  682. raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
  683. if dialog_token > 8 and status_code == 0:
  684. raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
  685. def test_gas_no_pending(dev, apdev):
  686. """GAS and no pending query for comeback request"""
  687. hapd = start_ap(apdev[0])
  688. bssid = apdev[0]['bssid']
  689. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  690. wpas.interface_add("wlan5")
  691. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  692. raise Exception("Failed to set listen channel")
  693. if "OK" not in wpas.p2p_listen():
  694. raise Exception("Failed to start listen state")
  695. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  696. raise Exception("Failed to enable external management frame handling")
  697. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
  698. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  699. if "OK" not in wpas.request(req):
  700. raise Exception("Could not send management frame")
  701. resp = wpas.mgmt_rx()
  702. if resp is None:
  703. raise Exception("MGMT-RX timeout")
  704. if 'payload' not in resp:
  705. raise Exception("Missing payload")
  706. gresp = parse_gas(resp['payload'])
  707. status_code = gresp['status_code']
  708. if status_code != 60:
  709. raise Exception("Unexpected status code {} (expected 60)".format(status_code))
  710. def test_gas_missing_payload(dev, apdev):
  711. """No action code in the query frame"""
  712. bssid = apdev[0]['bssid']
  713. params = hs20_ap_params()
  714. params['hessid'] = bssid
  715. hostapd.add_ap(apdev[0]['ifname'], params)
  716. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  717. cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
  718. if "FAIL" in dev[0].request(cmd):
  719. raise Exception("Could not send test Action frame")
  720. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  721. if ev is None:
  722. raise Exception("Timeout on MGMT-TX-STATUS")
  723. if "result=SUCCESS" not in ev:
  724. raise Exception("AP did not ack Action frame")
  725. cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
  726. if "FAIL" in dev[0].request(cmd):
  727. raise Exception("Could not send test Action frame")
  728. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  729. if ev is None:
  730. raise Exception("Timeout on MGMT-TX-STATUS")
  731. if "result=SUCCESS" not in ev:
  732. raise Exception("AP did not ack Action frame")
  733. def test_gas_query_deinit(dev, apdev):
  734. """Pending GAS/ANQP query during deinit"""
  735. hapd = start_ap(apdev[0])
  736. bssid = apdev[0]['bssid']
  737. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  738. wpas.interface_add("wlan5")
  739. wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
  740. id = wpas.request("RADIO_WORK add block-work")
  741. if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
  742. raise Exception("ANQP_GET command failed")
  743. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  744. if ev is None:
  745. raise Exception("Timeout while waiting radio work to start")
  746. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  747. if ev is None:
  748. raise Exception("Timeout while waiting radio work to start (2)")
  749. # Remove the interface while the gas-query radio work is still pending and
  750. # GAS query has not yet been started.
  751. wpas.interface_remove("wlan5")
  752. def test_gas_anqp_oom_wpas(dev, apdev):
  753. """GAS/ANQP query and OOM in wpa_supplicant"""
  754. hapd = start_ap(apdev[0])
  755. bssid = apdev[0]['bssid']
  756. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  757. with alloc_fail(dev[0], 1, "gas_build_req"):
  758. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  759. raise Exception("Unexpected ANQP_GET command success (OOM)")
  760. def test_gas_anqp_oom_hapd(dev, apdev):
  761. """GAS/ANQP query and OOM in hostapd"""
  762. hapd = start_ap(apdev[0])
  763. bssid = apdev[0]['bssid']
  764. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  765. with alloc_fail(hapd, 1, "gas_build_resp"):
  766. # This query will time out due to the AP not sending a response (OOM).
  767. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  768. raise Exception("ANQP_GET command failed")
  769. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  770. if ev is None:
  771. raise Exception("GAS query start timed out")
  772. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  773. if ev is None:
  774. raise Exception("GAS query timed out")
  775. if "result=TIMEOUT" not in ev:
  776. raise Exception("Unexpected result: " + ev)
  777. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  778. if ev is None:
  779. raise Exception("ANQP-QUERY-DONE event not seen")
  780. if "result=FAILURE" not in ev:
  781. raise Exception("Unexpected result: " + ev)
  782. with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
  783. hapd.set("gas_frag_limit", "50")
  784. # This query will time out due to the AP not sending a response (OOM).
  785. print dev[0].request("FETCH_ANQP")
  786. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  787. if ev is None:
  788. raise Exception("GAS query start timed out")
  789. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  790. if ev is None:
  791. raise Exception("GAS query timed out")
  792. if "result=TIMEOUT" not in ev:
  793. raise Exception("Unexpected result: " + ev)
  794. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  795. if ev is None:
  796. raise Exception("ANQP-QUERY-DONE event not seen")
  797. if "result=FAILURE" not in ev:
  798. raise Exception("Unexpected result: " + ev)