test_gas.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. def hs20_ap_params():
  15. params = hostapd.wpa2_params(ssid="test-gas")
  16. params['wpa_key_mgmt'] = "WPA-EAP"
  17. params['ieee80211w'] = "1"
  18. params['ieee8021x'] = "1"
  19. params['auth_server_addr'] = "127.0.0.1"
  20. params['auth_server_port'] = "1812"
  21. params['auth_server_shared_secret'] = "radius"
  22. params['interworking'] = "1"
  23. params['access_network_type'] = "14"
  24. params['internet'] = "1"
  25. params['asra'] = "0"
  26. params['esr'] = "0"
  27. params['uesa'] = "0"
  28. params['venue_group'] = "7"
  29. params['venue_type'] = "1"
  30. params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
  31. params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
  32. "fedcba" ]
  33. params['domain_name'] = "example.com,another.example.com"
  34. params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
  35. "0,another.example.com" ]
  36. params['anqp_3gpp_cell_net'] = "244,91"
  37. params['network_auth_type'] = "02http://www.example.com/redirect/me/here/"
  38. params['ipaddr_type_availability'] = "14"
  39. params['hs20'] = "1"
  40. params['hs20_oper_friendly_name'] = [ "eng:Example operator", "fin:Esimerkkioperaattori" ]
  41. params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
  42. params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
  43. params['hs20_operating_class'] = "5173"
  44. return params
  45. def start_ap(ap):
  46. params = hs20_ap_params()
  47. params['hessid'] = ap['bssid']
  48. hostapd.add_ap(ap['ifname'], params)
  49. return hostapd.Hostapd(ap['ifname'])
  50. def get_gas_response(dev, bssid, info, allow_fetch_failure=False):
  51. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  52. res = re.split(exp, info)
  53. if len(res) < 6:
  54. raise Exception("Could not parse GAS-RESPONSE-INFO")
  55. if res[2] != bssid:
  56. raise Exception("Unexpected BSSID in response")
  57. token = res[3]
  58. status = res[4]
  59. if status != "0":
  60. raise Exception("GAS query failed")
  61. resp_len = res[5]
  62. if resp_len == "-1":
  63. raise Exception("GAS query reported invalid response length")
  64. if int(resp_len) > 2000:
  65. raise Exception("Unexpected long GAS response")
  66. resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
  67. if "FAIL" in resp:
  68. if allow_fetch_failure:
  69. logger.debug("GAS response was not available anymore")
  70. return
  71. raise Exception("Could not fetch GAS response")
  72. if len(resp) != int(resp_len) * 2:
  73. raise Exception("Unexpected GAS response length")
  74. logger.debug("GAS response: " + resp)
  75. def test_gas_generic(dev, apdev):
  76. """Generic GAS query"""
  77. bssid = apdev[0]['bssid']
  78. params = hs20_ap_params()
  79. params['hessid'] = bssid
  80. hostapd.add_ap(apdev[0]['ifname'], params)
  81. dev[0].scan(freq="2412")
  82. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  83. if "FAIL" in req:
  84. raise Exception("GAS query request rejected")
  85. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  86. if ev is None:
  87. raise Exception("GAS query timed out")
  88. get_gas_response(dev[0], bssid, ev)
  89. def test_gas_concurrent_scan(dev, apdev):
  90. """Generic GAS queries with concurrent scan operation"""
  91. bssid = apdev[0]['bssid']
  92. params = hs20_ap_params()
  93. params['hessid'] = bssid
  94. hostapd.add_ap(apdev[0]['ifname'], params)
  95. # get BSS entry available to allow GAS query
  96. dev[0].scan(freq="2412")
  97. logger.info("Request concurrent operations")
  98. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  99. if "FAIL" in req:
  100. raise Exception("GAS query request rejected")
  101. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
  102. if "FAIL" in req:
  103. raise Exception("GAS query request rejected")
  104. dev[0].scan(no_wait=True)
  105. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
  106. if "FAIL" in req:
  107. raise Exception("GAS query request rejected")
  108. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
  109. if "FAIL" in req:
  110. raise Exception("GAS query request rejected")
  111. responses = 0
  112. for i in range(0, 5):
  113. ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
  114. timeout=10)
  115. if ev is None:
  116. raise Exception("Operation timed out")
  117. if "GAS-RESPONSE-INFO" in ev:
  118. responses = responses + 1
  119. get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
  120. if responses != 4:
  121. raise Exception("Unexpected number of GAS responses")
  122. def test_gas_concurrent_connect(dev, apdev):
  123. """Generic GAS queries with concurrent connection operation"""
  124. bssid = apdev[0]['bssid']
  125. params = hs20_ap_params()
  126. params['hessid'] = bssid
  127. hostapd.add_ap(apdev[0]['ifname'], params)
  128. dev[0].scan(freq="2412")
  129. logger.debug("Start concurrent connect and GAS request")
  130. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  131. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  132. password="password", phase2="auth=MSCHAPV2",
  133. ca_cert="auth_serv/ca.pem", wait_connect=False,
  134. scan_freq="2412")
  135. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  136. if "FAIL" in req:
  137. raise Exception("GAS query request rejected")
  138. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  139. timeout=20)
  140. if ev is None:
  141. raise Exception("Operation timed out")
  142. if "CTRL-EVENT-CONNECTED" not in ev:
  143. raise Exception("Unexpected operation order")
  144. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  145. timeout=20)
  146. if ev is None:
  147. raise Exception("Operation timed out")
  148. if "GAS-RESPONSE-INFO" not in ev:
  149. raise Exception("Unexpected operation order")
  150. get_gas_response(dev[0], bssid, ev)
  151. dev[0].request("DISCONNECT")
  152. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
  153. if ev is None:
  154. raise Exception("Disconnection timed out")
  155. logger.debug("Wait six seconds for expiration of connect-without-scan")
  156. time.sleep(6)
  157. dev[0].dump_monitor()
  158. logger.debug("Start concurrent GAS request and connect")
  159. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  160. if "FAIL" in req:
  161. raise Exception("GAS query request rejected")
  162. dev[0].request("RECONNECT")
  163. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  164. if ev is None:
  165. raise Exception("Operation timed out")
  166. get_gas_response(dev[0], bssid, ev)
  167. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
  168. if ev is None:
  169. raise Exception("No new scan results reported")
  170. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  171. if ev is None:
  172. raise Exception("Operation timed out")
  173. if "CTRL-EVENT-CONNECTED" not in ev:
  174. raise Exception("Unexpected operation order")
  175. def test_gas_fragment(dev, apdev):
  176. """GAS fragmentation"""
  177. hapd = start_ap(apdev[0])
  178. hapd.set("gas_frag_limit", "50")
  179. dev[0].scan(freq="2412")
  180. dev[0].request("FETCH_ANQP")
  181. for i in range(0, 13):
  182. ev = dev[0].wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
  183. if ev is None:
  184. raise Exception("Operation timed out")
  185. def test_gas_comeback_delay(dev, apdev):
  186. """GAS fragmentation"""
  187. hapd = start_ap(apdev[0])
  188. hapd.set("gas_comeback_delay", "500")
  189. dev[0].scan(freq="2412")
  190. dev[0].request("FETCH_ANQP")
  191. for i in range(0, 6):
  192. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  193. if ev is None:
  194. raise Exception("Operation timed out")
  195. def expect_gas_result(dev, result):
  196. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  197. if ev is None:
  198. raise Exception("GAS query timed out")
  199. if "result=" + result not in ev:
  200. raise Exception("Unexpected GAS query result")
  201. def anqp_get(dev, bssid, id):
  202. dev.request("ANQP_GET " + bssid + " " + str(id))
  203. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  204. if ev is None:
  205. raise Exception("GAS query start timed out")
  206. def test_gas_timeout(dev, apdev):
  207. """GAS timeout"""
  208. hapd = start_ap(apdev[0])
  209. bssid = apdev[0]['bssid']
  210. dev[0].scan(freq="2412")
  211. hapd.set("ext_mgmt_frame_handling", "1")
  212. anqp_get(dev[0], bssid, 263)
  213. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  214. if ev is None:
  215. raise Exception("MGMT RX wait timed out")
  216. expect_gas_result(dev[0], "TIMEOUT")
  217. MGMT_SUBTYPE_ACTION = 13
  218. ACTION_CATEG_PUBLIC = 4
  219. GAS_INITIAL_REQUEST = 10
  220. GAS_INITIAL_RESPONSE = 11
  221. GAS_COMEBACK_REQUEST = 12
  222. GAS_COMEBACK_RESPONSE = 13
  223. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  224. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  225. def anqp_adv_proto():
  226. return struct.pack('BBBB', 108, 2, 127, 0)
  227. def anqp_initial_resp(dialog_token, status_code):
  228. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  229. dialog_token, status_code, 0) + anqp_adv_proto()
  230. def anqp_comeback_resp(dialog_token):
  231. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  232. dialog_token, 0, 0, 0) + anqp_adv_proto()
  233. def gas_rx(hapd):
  234. count = 0
  235. while count < 30:
  236. count = count + 1
  237. query = hapd.mgmt_rx()
  238. if query is None:
  239. raise Exception("Action frame not received")
  240. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  241. continue
  242. payload = query['payload']
  243. if len(payload) < 2:
  244. continue
  245. (category, action) = struct.unpack('BB', payload[0:2])
  246. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  247. continue
  248. return query
  249. raise Exception("No Action frame received")
  250. def parse_gas(payload):
  251. pos = payload
  252. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  253. if category != ACTION_CATEG_PUBLIC:
  254. return None
  255. if action not in GAS_ACTIONS:
  256. return None
  257. gas = {}
  258. gas['action'] = action
  259. pos = pos[3:]
  260. if len(pos) < 1:
  261. return None
  262. gas['dialog_token'] = dialog_token
  263. return gas
  264. def action_response(req):
  265. resp = {}
  266. resp['fc'] = req['fc']
  267. resp['da'] = req['sa']
  268. resp['sa'] = req['da']
  269. resp['bssid'] = req['bssid']
  270. return resp
  271. def test_gas_invalid_response_type(dev, apdev):
  272. """GAS invalid response type"""
  273. hapd = start_ap(apdev[0])
  274. bssid = apdev[0]['bssid']
  275. dev[0].scan(freq="2412")
  276. hapd.set("ext_mgmt_frame_handling", "1")
  277. anqp_get(dev[0], bssid, 263)
  278. query = gas_rx(hapd)
  279. gas = parse_gas(query['payload'])
  280. resp = action_response(query)
  281. # GAS Comeback Response instead of GAS Initial Response
  282. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  283. hapd.mgmt_tx(resp)
  284. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  285. if ev is None:
  286. raise Exception("Missing TX status for GAS response")
  287. if "ok=1" not in ev:
  288. raise Exception("GAS response not acknowledged")
  289. # station drops the invalid frame, so this needs to result in GAS timeout
  290. expect_gas_result(dev[0], "TIMEOUT")
  291. def test_gas_failure_status_code(dev, apdev):
  292. """GAS failure status code"""
  293. hapd = start_ap(apdev[0])
  294. bssid = apdev[0]['bssid']
  295. dev[0].scan(freq="2412")
  296. hapd.set("ext_mgmt_frame_handling", "1")
  297. anqp_get(dev[0], bssid, 263)
  298. query = gas_rx(hapd)
  299. gas = parse_gas(query['payload'])
  300. resp = action_response(query)
  301. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  302. hapd.mgmt_tx(resp)
  303. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  304. if ev is None:
  305. raise Exception("Missing TX status for GAS response")
  306. if "ok=1" not in ev:
  307. raise Exception("GAS response not acknowledged")
  308. expect_gas_result(dev[0], "FAILURE")
  309. def test_gas_malformed(dev, apdev):
  310. """GAS malformed response frames"""
  311. hapd = start_ap(apdev[0])
  312. bssid = apdev[0]['bssid']
  313. dev[0].scan(freq="2412")
  314. hapd.set("ext_mgmt_frame_handling", "1")
  315. anqp_get(dev[0], bssid, 263)
  316. query = gas_rx(hapd)
  317. gas = parse_gas(query['payload'])
  318. resp = action_response(query)
  319. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  320. GAS_COMEBACK_RESPONSE,
  321. gas['dialog_token'], 0)
  322. hapd.mgmt_tx(resp)
  323. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  324. GAS_COMEBACK_RESPONSE,
  325. gas['dialog_token'], 0, 0)
  326. hapd.mgmt_tx(resp)
  327. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  328. gas['dialog_token'], 0, 0)
  329. resp['payload'] = hdr + struct.pack('B', 108)
  330. hapd.mgmt_tx(resp)
  331. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  332. hapd.mgmt_tx(resp)
  333. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  334. hapd.mgmt_tx(resp)
  335. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  336. hapd.mgmt_tx(resp)
  337. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  338. hapd.mgmt_tx(resp)
  339. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  340. hapd.mgmt_tx(resp)
  341. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  342. hapd.mgmt_tx(resp)
  343. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  344. hapd.mgmt_tx(resp)
  345. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  346. hapd.mgmt_tx(resp)
  347. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  348. hapd.mgmt_tx(resp)
  349. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  350. hapd.mgmt_tx(resp)
  351. # Station drops invalid frames, but the last of the responses is valid from
  352. # GAS view point even though it has an extra octet in the end and the ANQP
  353. # part of the response is not valid. This is reported as successfulyl
  354. # completed GAS exchange.
  355. expect_gas_result(dev[0], "SUCCESS")