test_gas.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #!/usr/bin/python
  2. #
  3. # GAS tests
  4. # Copyright (c) 2013, Qualcomm Atheros, Inc.
  5. # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
  6. #
  7. # This software may be distributed under the terms of the BSD license.
  8. # See README for more details.
  9. import time
  10. import binascii
  11. import logging
  12. logger = logging.getLogger()
  13. import re
  14. import struct
  15. import hostapd
  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. 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. resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
  69. if "FAIL" in resp:
  70. if allow_fetch_failure:
  71. logger.debug("GAS response was not available anymore")
  72. return
  73. raise Exception("Could not fetch GAS response")
  74. if len(resp) != int(resp_len) * 2:
  75. raise Exception("Unexpected GAS response length")
  76. logger.debug("GAS response: " + resp)
  77. def test_gas_generic(dev, apdev):
  78. """Generic GAS query"""
  79. bssid = apdev[0]['bssid']
  80. params = hs20_ap_params()
  81. params['hessid'] = bssid
  82. hostapd.add_ap(apdev[0]['ifname'], params)
  83. dev[0].scan()
  84. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  85. if "FAIL" in req:
  86. raise Exception("GAS query request rejected")
  87. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  88. if ev is None:
  89. raise Exception("GAS query timed out")
  90. get_gas_response(dev[0], bssid, ev)
  91. def test_gas_concurrent_scan(dev, apdev):
  92. """Generic GAS queries with concurrent scan operation"""
  93. bssid = apdev[0]['bssid']
  94. params = hs20_ap_params()
  95. params['hessid'] = bssid
  96. hostapd.add_ap(apdev[0]['ifname'], params)
  97. # get BSS entry available to allow GAS query
  98. dev[0].scan(freq="2412")
  99. logger.info("Request concurrent operations")
  100. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  101. if "FAIL" in req:
  102. raise Exception("GAS query request rejected")
  103. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
  104. if "FAIL" in req:
  105. raise Exception("GAS query request rejected")
  106. dev[0].scan(no_wait=True)
  107. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
  108. if "FAIL" in req:
  109. raise Exception("GAS query request rejected")
  110. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
  111. if "FAIL" in req:
  112. raise Exception("GAS query request rejected")
  113. responses = 0
  114. for i in range(0, 5):
  115. ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
  116. timeout=10)
  117. if ev is None:
  118. raise Exception("Operation timed out")
  119. if "GAS-RESPONSE-INFO" in ev:
  120. responses = responses + 1
  121. get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
  122. if responses != 4:
  123. raise Exception("Unexpected number of GAS responses")
  124. def test_gas_concurrent_connect(dev, apdev):
  125. """Generic GAS queries with concurrent connection operation"""
  126. bssid = apdev[0]['bssid']
  127. params = hs20_ap_params()
  128. params['hessid'] = bssid
  129. hostapd.add_ap(apdev[0]['ifname'], params)
  130. dev[0].scan(freq="2412")
  131. logger.debug("Start concurrent connect and GAS request")
  132. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  133. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  134. password="password", phase2="auth=MSCHAPV2",
  135. ca_cert="auth_serv/ca.pem", wait_connect=False,
  136. scan_freq="2412")
  137. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  138. if "FAIL" in req:
  139. raise Exception("GAS query request rejected")
  140. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  141. timeout=20)
  142. if ev is None:
  143. raise Exception("Operation timed out")
  144. if "CTRL-EVENT-CONNECTED" not in ev:
  145. raise Exception("Unexpected operation order")
  146. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  147. timeout=20)
  148. if ev is None:
  149. raise Exception("Operation timed out")
  150. if "GAS-RESPONSE-INFO" not in ev:
  151. raise Exception("Unexpected operation order")
  152. get_gas_response(dev[0], bssid, ev)
  153. dev[0].request("DISCONNECT")
  154. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
  155. if ev is None:
  156. raise Exception("Disconnection timed out")
  157. logger.debug("Wait six seconds for expiration of connect-without-scan")
  158. time.sleep(6)
  159. logger.debug("Start concurrent GAS request and connect")
  160. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  161. if "FAIL" in req:
  162. raise Exception("GAS query request rejected")
  163. dev[0].request("RECONNECT")
  164. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  165. if ev is None:
  166. raise Exception("Operation timed out")
  167. get_gas_response(dev[0], bssid, ev)
  168. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
  169. if ev is None:
  170. raise Exception("No new scan results reported")
  171. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  172. if ev is None:
  173. raise Exception("Operation timed out")
  174. if "CTRL-EVENT-CONNECTED" not in ev:
  175. raise Exception("Unexpected operation order")
  176. def test_gas_fragment(dev, apdev):
  177. """GAS fragmentation"""
  178. hapd = start_ap(apdev[0])
  179. hapd.set("gas_frag_limit", "50")
  180. dev[0].scan(freq="2412")
  181. dev[0].request("FETCH_ANQP")
  182. for i in range(0, 13):
  183. ev = dev[0].wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
  184. if ev is None:
  185. raise Exception("Operation timed out")
  186. def test_gas_comeback_delay(dev, apdev):
  187. """GAS fragmentation"""
  188. hapd = start_ap(apdev[0])
  189. hapd.set("gas_comeback_delay", "500")
  190. dev[0].scan(freq="2412")
  191. dev[0].request("FETCH_ANQP")
  192. for i in range(0, 6):
  193. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  194. if ev is None:
  195. raise Exception("Operation timed out")
  196. def expect_gas_result(dev, result):
  197. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  198. if ev is None:
  199. raise Exception("GAS query timed out")
  200. if "result=" + result not in ev:
  201. raise Exception("Unexpected GAS query result")
  202. def anqp_get(dev, bssid, id):
  203. dev.request("ANQP_GET " + bssid + " " + str(id))
  204. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  205. if ev is None:
  206. raise Exception("GAS query start timed out")
  207. def test_gas_timeout(dev, apdev):
  208. """GAS timeout"""
  209. hapd = start_ap(apdev[0])
  210. bssid = apdev[0]['bssid']
  211. dev[0].scan(freq="2412")
  212. hapd.set("ext_mgmt_frame_handling", "1")
  213. anqp_get(dev[0], bssid, 263)
  214. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  215. if ev is None:
  216. raise Exception("MGMT RX wait timed out")
  217. expect_gas_result(dev[0], "TIMEOUT")
  218. MGMT_SUBTYPE_ACTION = 13
  219. ACTION_CATEG_PUBLIC = 4
  220. GAS_INITIAL_REQUEST = 10
  221. GAS_INITIAL_RESPONSE = 11
  222. GAS_COMEBACK_REQUEST = 12
  223. GAS_COMEBACK_RESPONSE = 13
  224. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  225. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  226. def anqp_adv_proto():
  227. return struct.pack('BBBB', 108, 2, 127, 0)
  228. def anqp_initial_resp(dialog_token, status_code):
  229. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  230. dialog_token, status_code, 0) + anqp_adv_proto()
  231. def anqp_comeback_resp(dialog_token):
  232. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  233. dialog_token, 0, 0, 0) + anqp_adv_proto()
  234. def gas_rx(hapd):
  235. count = 0
  236. while count < 30:
  237. count = count + 1
  238. query = hapd.mgmt_rx()
  239. if query is None:
  240. raise Exception("Action frame not received")
  241. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  242. continue
  243. payload = query['payload']
  244. if len(payload) < 2:
  245. continue
  246. (category, action) = struct.unpack('BB', payload[0:2])
  247. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  248. continue
  249. return query
  250. raise Exception("No Action frame received")
  251. def parse_gas(payload):
  252. pos = payload
  253. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  254. if category != ACTION_CATEG_PUBLIC:
  255. return None
  256. if action not in GAS_ACTIONS:
  257. return None
  258. gas = {}
  259. gas['action'] = action
  260. pos = pos[3:]
  261. if len(pos) < 1:
  262. return None
  263. gas['dialog_token'] = dialog_token
  264. return gas
  265. def action_response(req):
  266. resp = {}
  267. resp['fc'] = req['fc']
  268. resp['da'] = req['sa']
  269. resp['sa'] = req['da']
  270. resp['bssid'] = req['bssid']
  271. return resp
  272. def test_gas_invalid_response_type(dev, apdev):
  273. """GAS invalid response type"""
  274. hapd = start_ap(apdev[0])
  275. bssid = apdev[0]['bssid']
  276. dev[0].scan(freq="2412")
  277. hapd.set("ext_mgmt_frame_handling", "1")
  278. anqp_get(dev[0], bssid, 263)
  279. query = gas_rx(hapd)
  280. gas = parse_gas(query['payload'])
  281. resp = action_response(query)
  282. # GAS Comeback Response instead of GAS Initial Response
  283. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  284. hapd.mgmt_tx(resp)
  285. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  286. if ev is None:
  287. raise Exception("Missing TX status for GAS response")
  288. if "ok=1" not in ev:
  289. raise Exception("GAS response not acknowledged")
  290. # station drops the invalid frame, so this needs to result in GAS timeout
  291. expect_gas_result(dev[0], "TIMEOUT")
  292. def test_gas_failure_status_code(dev, apdev):
  293. """GAS failure status code"""
  294. hapd = start_ap(apdev[0])
  295. bssid = apdev[0]['bssid']
  296. dev[0].scan(freq="2412")
  297. hapd.set("ext_mgmt_frame_handling", "1")
  298. anqp_get(dev[0], bssid, 263)
  299. query = gas_rx(hapd)
  300. gas = parse_gas(query['payload'])
  301. resp = action_response(query)
  302. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  303. hapd.mgmt_tx(resp)
  304. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  305. if ev is None:
  306. raise Exception("Missing TX status for GAS response")
  307. if "ok=1" not in ev:
  308. raise Exception("GAS response not acknowledged")
  309. expect_gas_result(dev[0], "FAILURE")
  310. def test_gas_malformed(dev, apdev):
  311. """GAS malformed response frames"""
  312. hapd = start_ap(apdev[0])
  313. bssid = apdev[0]['bssid']
  314. dev[0].scan(freq="2412")
  315. hapd.set("ext_mgmt_frame_handling", "1")
  316. anqp_get(dev[0], bssid, 263)
  317. query = gas_rx(hapd)
  318. gas = parse_gas(query['payload'])
  319. resp = action_response(query)
  320. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  321. GAS_COMEBACK_RESPONSE,
  322. gas['dialog_token'], 0)
  323. hapd.mgmt_tx(resp)
  324. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  325. GAS_COMEBACK_RESPONSE,
  326. gas['dialog_token'], 0, 0)
  327. hapd.mgmt_tx(resp)
  328. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  329. gas['dialog_token'], 0, 0)
  330. resp['payload'] = hdr + struct.pack('B', 108)
  331. hapd.mgmt_tx(resp)
  332. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  333. hapd.mgmt_tx(resp)
  334. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  335. hapd.mgmt_tx(resp)
  336. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  337. hapd.mgmt_tx(resp)
  338. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  339. hapd.mgmt_tx(resp)
  340. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  341. hapd.mgmt_tx(resp)
  342. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  343. hapd.mgmt_tx(resp)
  344. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  345. hapd.mgmt_tx(resp)
  346. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  347. hapd.mgmt_tx(resp)
  348. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  349. hapd.mgmt_tx(resp)
  350. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  351. hapd.mgmt_tx(resp)
  352. # Station drops invalid frames, but the last of the responses is valid from
  353. # GAS view point even though it has an extra octet in the end and the ANQP
  354. # part of the response is not valid. This is reported as successfulyl
  355. # completed GAS exchange.
  356. expect_gas_result(dev[0], "SUCCESS")