test_gas.py 32 KB

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