test_gas.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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(freq="2412")
  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. dev[0].dump_monitor()
  160. logger.debug("Start concurrent GAS request and connect")
  161. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  162. if "FAIL" in req:
  163. raise Exception("GAS query request rejected")
  164. dev[0].request("RECONNECT")
  165. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  166. if ev is None:
  167. raise Exception("Operation timed out")
  168. get_gas_response(dev[0], bssid, ev)
  169. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
  170. if ev is None:
  171. raise Exception("No new scan results reported")
  172. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  173. if ev is None:
  174. raise Exception("Operation timed out")
  175. if "CTRL-EVENT-CONNECTED" not in ev:
  176. raise Exception("Unexpected operation order")
  177. def test_gas_fragment(dev, apdev):
  178. """GAS fragmentation"""
  179. hapd = start_ap(apdev[0])
  180. hapd.set("gas_frag_limit", "50")
  181. dev[0].scan(freq="2412")
  182. dev[0].request("FETCH_ANQP")
  183. for i in range(0, 13):
  184. ev = dev[0].wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
  185. if ev is None:
  186. raise Exception("Operation timed out")
  187. def test_gas_comeback_delay(dev, apdev):
  188. """GAS fragmentation"""
  189. hapd = start_ap(apdev[0])
  190. hapd.set("gas_comeback_delay", "500")
  191. dev[0].scan(freq="2412")
  192. dev[0].request("FETCH_ANQP")
  193. for i in range(0, 6):
  194. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  195. if ev is None:
  196. raise Exception("Operation timed out")
  197. def expect_gas_result(dev, result):
  198. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  199. if ev is None:
  200. raise Exception("GAS query timed out")
  201. if "result=" + result not in ev:
  202. raise Exception("Unexpected GAS query result")
  203. def anqp_get(dev, bssid, id):
  204. dev.request("ANQP_GET " + bssid + " " + str(id))
  205. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  206. if ev is None:
  207. raise Exception("GAS query start timed out")
  208. def test_gas_timeout(dev, apdev):
  209. """GAS timeout"""
  210. hapd = start_ap(apdev[0])
  211. bssid = apdev[0]['bssid']
  212. dev[0].scan(freq="2412")
  213. hapd.set("ext_mgmt_frame_handling", "1")
  214. anqp_get(dev[0], bssid, 263)
  215. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  216. if ev is None:
  217. raise Exception("MGMT RX wait timed out")
  218. expect_gas_result(dev[0], "TIMEOUT")
  219. MGMT_SUBTYPE_ACTION = 13
  220. ACTION_CATEG_PUBLIC = 4
  221. GAS_INITIAL_REQUEST = 10
  222. GAS_INITIAL_RESPONSE = 11
  223. GAS_COMEBACK_REQUEST = 12
  224. GAS_COMEBACK_RESPONSE = 13
  225. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  226. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  227. def anqp_adv_proto():
  228. return struct.pack('BBBB', 108, 2, 127, 0)
  229. def anqp_initial_resp(dialog_token, status_code):
  230. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  231. dialog_token, status_code, 0) + anqp_adv_proto()
  232. def anqp_comeback_resp(dialog_token):
  233. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  234. dialog_token, 0, 0, 0) + anqp_adv_proto()
  235. def gas_rx(hapd):
  236. count = 0
  237. while count < 30:
  238. count = count + 1
  239. query = hapd.mgmt_rx()
  240. if query is None:
  241. raise Exception("Action frame not received")
  242. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  243. continue
  244. payload = query['payload']
  245. if len(payload) < 2:
  246. continue
  247. (category, action) = struct.unpack('BB', payload[0:2])
  248. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  249. continue
  250. return query
  251. raise Exception("No Action frame received")
  252. def parse_gas(payload):
  253. pos = payload
  254. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  255. if category != ACTION_CATEG_PUBLIC:
  256. return None
  257. if action not in GAS_ACTIONS:
  258. return None
  259. gas = {}
  260. gas['action'] = action
  261. pos = pos[3:]
  262. if len(pos) < 1:
  263. return None
  264. gas['dialog_token'] = dialog_token
  265. return gas
  266. def action_response(req):
  267. resp = {}
  268. resp['fc'] = req['fc']
  269. resp['da'] = req['sa']
  270. resp['sa'] = req['da']
  271. resp['bssid'] = req['bssid']
  272. return resp
  273. def test_gas_invalid_response_type(dev, apdev):
  274. """GAS invalid response type"""
  275. hapd = start_ap(apdev[0])
  276. bssid = apdev[0]['bssid']
  277. dev[0].scan(freq="2412")
  278. hapd.set("ext_mgmt_frame_handling", "1")
  279. anqp_get(dev[0], bssid, 263)
  280. query = gas_rx(hapd)
  281. gas = parse_gas(query['payload'])
  282. resp = action_response(query)
  283. # GAS Comeback Response instead of GAS Initial Response
  284. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  285. hapd.mgmt_tx(resp)
  286. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  287. if ev is None:
  288. raise Exception("Missing TX status for GAS response")
  289. if "ok=1" not in ev:
  290. raise Exception("GAS response not acknowledged")
  291. # station drops the invalid frame, so this needs to result in GAS timeout
  292. expect_gas_result(dev[0], "TIMEOUT")
  293. def test_gas_failure_status_code(dev, apdev):
  294. """GAS failure status code"""
  295. hapd = start_ap(apdev[0])
  296. bssid = apdev[0]['bssid']
  297. dev[0].scan(freq="2412")
  298. hapd.set("ext_mgmt_frame_handling", "1")
  299. anqp_get(dev[0], bssid, 263)
  300. query = gas_rx(hapd)
  301. gas = parse_gas(query['payload'])
  302. resp = action_response(query)
  303. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  304. hapd.mgmt_tx(resp)
  305. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  306. if ev is None:
  307. raise Exception("Missing TX status for GAS response")
  308. if "ok=1" not in ev:
  309. raise Exception("GAS response not acknowledged")
  310. expect_gas_result(dev[0], "FAILURE")
  311. def test_gas_malformed(dev, apdev):
  312. """GAS malformed response frames"""
  313. hapd = start_ap(apdev[0])
  314. bssid = apdev[0]['bssid']
  315. dev[0].scan(freq="2412")
  316. hapd.set("ext_mgmt_frame_handling", "1")
  317. anqp_get(dev[0], bssid, 263)
  318. query = gas_rx(hapd)
  319. gas = parse_gas(query['payload'])
  320. resp = action_response(query)
  321. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  322. GAS_COMEBACK_RESPONSE,
  323. gas['dialog_token'], 0)
  324. hapd.mgmt_tx(resp)
  325. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  326. GAS_COMEBACK_RESPONSE,
  327. gas['dialog_token'], 0, 0)
  328. hapd.mgmt_tx(resp)
  329. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  330. gas['dialog_token'], 0, 0)
  331. resp['payload'] = hdr + struct.pack('B', 108)
  332. hapd.mgmt_tx(resp)
  333. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  334. hapd.mgmt_tx(resp)
  335. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  336. hapd.mgmt_tx(resp)
  337. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  338. hapd.mgmt_tx(resp)
  339. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  340. hapd.mgmt_tx(resp)
  341. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  342. hapd.mgmt_tx(resp)
  343. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  344. hapd.mgmt_tx(resp)
  345. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  346. hapd.mgmt_tx(resp)
  347. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  348. hapd.mgmt_tx(resp)
  349. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  350. hapd.mgmt_tx(resp)
  351. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  352. hapd.mgmt_tx(resp)
  353. # Station drops invalid frames, but the last of the responses is valid from
  354. # GAS view point even though it has an extra octet in the end and the ANQP
  355. # part of the response is not valid. This is reported as successfulyl
  356. # completed GAS exchange.
  357. expect_gas_result(dev[0], "SUCCESS")