test_gas.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/usr/bin/python
  2. #
  3. # GAS tests
  4. # Copyright (c) 2013, Qualcomm Atheros, Inc.
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import time
  9. import binascii
  10. import logging
  11. logger = logging.getLogger()
  12. import re
  13. import struct
  14. import hostapd
  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. return params
  39. def start_ap(ap):
  40. params = hs20_ap_params()
  41. params['hessid'] = ap['bssid']
  42. hostapd.add_ap(ap['ifname'], params)
  43. return hostapd.Hostapd(ap['ifname'])
  44. def get_gas_response(dev, bssid, info, allow_fetch_failure=False):
  45. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  46. res = re.split(exp, info)
  47. if len(res) < 6:
  48. raise Exception("Could not parse GAS-RESPONSE-INFO")
  49. if res[2] != bssid:
  50. raise Exception("Unexpected BSSID in response")
  51. token = res[3]
  52. status = res[4]
  53. if status != "0":
  54. raise Exception("GAS query failed")
  55. resp_len = res[5]
  56. if resp_len == "-1":
  57. raise Exception("GAS query reported invalid response length")
  58. if int(resp_len) > 2000:
  59. raise Exception("Unexpected long GAS response")
  60. resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
  61. if "FAIL" in resp:
  62. if allow_fetch_failure:
  63. logger.debug("GAS response was not available anymore")
  64. return
  65. raise Exception("Could not fetch GAS response")
  66. if len(resp) != int(resp_len) * 2:
  67. raise Exception("Unexpected GAS response length")
  68. logger.debug("GAS response: " + resp)
  69. def test_gas_generic(dev, apdev):
  70. """Generic GAS query"""
  71. bssid = apdev[0]['bssid']
  72. params = hs20_ap_params()
  73. params['hessid'] = bssid
  74. hostapd.add_ap(apdev[0]['ifname'], params)
  75. dev[0].scan()
  76. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  77. if "FAIL" in req:
  78. raise Exception("GAS query request rejected")
  79. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  80. if ev is None:
  81. raise Exception("GAS query timed out")
  82. get_gas_response(dev[0], bssid, ev)
  83. def test_gas_concurrent_scan(dev, apdev):
  84. """Generic GAS queries with concurrent scan operation"""
  85. bssid = apdev[0]['bssid']
  86. params = hs20_ap_params()
  87. params['hessid'] = bssid
  88. hostapd.add_ap(apdev[0]['ifname'], params)
  89. dev[0].scan()
  90. logger.info("Request concurrent operations")
  91. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  92. if "FAIL" in req:
  93. raise Exception("GAS query request rejected")
  94. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
  95. if "FAIL" in req:
  96. raise Exception("GAS query request rejected")
  97. dev[0].request("SCAN")
  98. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
  99. if "FAIL" in req:
  100. raise Exception("GAS query request rejected")
  101. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
  102. if "FAIL" in req:
  103. raise Exception("GAS query request rejected")
  104. responses = 0
  105. for i in range(0, 5):
  106. ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
  107. timeout=10)
  108. if ev is None:
  109. raise Exception("Operation timed out")
  110. if "GAS-RESPONSE-INFO" in ev:
  111. responses = responses + 1
  112. get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
  113. if responses != 4:
  114. raise Exception("Unexpected number of GAS responses")
  115. def test_gas_concurrent_connect(dev, apdev):
  116. """Generic GAS queries with concurrent connection operation"""
  117. bssid = apdev[0]['bssid']
  118. params = hs20_ap_params()
  119. params['hessid'] = bssid
  120. hostapd.add_ap(apdev[0]['ifname'], params)
  121. dev[0].scan()
  122. logger.debug("Start concurrent connect and GAS request")
  123. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  124. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  125. password="password", phase2="auth=MSCHAPV2",
  126. ca_cert="auth_serv/ca.pem", wait_connect=False)
  127. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  128. if "FAIL" in req:
  129. raise Exception("GAS query request rejected")
  130. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  131. timeout=20)
  132. if ev is None:
  133. raise Exception("Operation timed out")
  134. if "CTRL-EVENT-CONNECTED" not in ev:
  135. raise Exception("Unexpected operation order")
  136. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  137. timeout=20)
  138. if ev is None:
  139. raise Exception("Operation timed out")
  140. if "GAS-RESPONSE-INFO" not in ev:
  141. raise Exception("Unexpected operation order")
  142. get_gas_response(dev[0], bssid, ev)
  143. dev[0].request("DISCONNECT")
  144. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
  145. if ev is None:
  146. raise Exception("Disconnection timed out")
  147. logger.debug("Wait six seconds for expiration of connect-without-scan")
  148. time.sleep(6)
  149. logger.debug("Start concurrent GAS request and connect")
  150. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  151. if "FAIL" in req:
  152. raise Exception("GAS query request rejected")
  153. dev[0].request("RECONNECT")
  154. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  155. if ev is None:
  156. raise Exception("Operation timed out")
  157. get_gas_response(dev[0], bssid, ev)
  158. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
  159. if ev is None:
  160. raise Exception("No new scan results reported")
  161. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  162. if ev is None:
  163. raise Exception("Operation timed out")
  164. if "CTRL-EVENT-CONNECTED" not in ev:
  165. raise Exception("Unexpected operation order")
  166. def test_gas_fragment(dev, apdev):
  167. """GAS fragmentation"""
  168. hapd = start_ap(apdev[0])
  169. hapd.set("gas_frag_limit", "50")
  170. dev[0].scan(freq="2412")
  171. dev[0].request("FETCH_ANQP")
  172. for i in range(0, 6):
  173. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  174. if ev is None:
  175. raise Exception("Operation timed out")
  176. def test_gas_comeback_delay(dev, apdev):
  177. """GAS fragmentation"""
  178. hapd = start_ap(apdev[0])
  179. hapd.set("gas_comeback_delay", "500")
  180. dev[0].scan(freq="2412")
  181. dev[0].request("FETCH_ANQP")
  182. for i in range(0, 6):
  183. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  184. if ev is None:
  185. raise Exception("Operation timed out")
  186. def expect_gas_result(dev, result):
  187. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  188. if ev is None:
  189. raise Exception("GAS query timed out")
  190. if "result=" + result not in ev:
  191. raise Exception("Unexpected GAS query result")
  192. def anqp_get(dev, bssid, id):
  193. dev.request("ANQP_GET " + bssid + " " + str(id))
  194. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  195. if ev is None:
  196. raise Exception("GAS query start timed out")
  197. def test_gas_timeout(dev, apdev):
  198. """GAS timeout"""
  199. hapd = start_ap(apdev[0])
  200. bssid = apdev[0]['bssid']
  201. dev[0].scan(freq="2412")
  202. hapd.set("ext_mgmt_frame_handling", "1")
  203. anqp_get(dev[0], bssid, 263)
  204. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  205. if ev is None:
  206. raise Exception("MGMT RX wait timed out")
  207. expect_gas_result(dev[0], "TIMEOUT")
  208. MGMT_SUBTYPE_ACTION = 13
  209. ACTION_CATEG_PUBLIC = 4
  210. GAS_INITIAL_REQUEST = 10
  211. GAS_INITIAL_RESPONSE = 11
  212. GAS_COMEBACK_REQUEST = 12
  213. GAS_COMEBACK_RESPONSE = 13
  214. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  215. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  216. def anqp_adv_proto():
  217. return struct.pack('BBBB', 108, 2, 127, 0)
  218. def anqp_comeback_resp(dialog_token):
  219. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  220. dialog_token, 0, 0, 0) + anqp_adv_proto()
  221. def gas_rx(hapd):
  222. count = 0
  223. while count < 30:
  224. count = count + 1
  225. query = hapd.mgmt_rx()
  226. if query is None:
  227. raise Exception("Action frame not received")
  228. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  229. continue
  230. payload = query['payload']
  231. if len(payload) < 2:
  232. continue
  233. (category, action) = struct.unpack('BB', payload[0:2])
  234. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  235. continue
  236. return query
  237. raise Exception("No Action frame received")
  238. def parse_gas(payload):
  239. pos = payload
  240. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  241. if category != ACTION_CATEG_PUBLIC:
  242. return None
  243. if action not in GAS_ACTIONS:
  244. return None
  245. gas = {}
  246. gas['action'] = action
  247. pos = pos[3:]
  248. if len(pos) < 1:
  249. return None
  250. gas['dialog_token'] = dialog_token
  251. return gas
  252. def action_response(req):
  253. resp = {}
  254. resp['fc'] = req['fc']
  255. resp['da'] = req['sa']
  256. resp['sa'] = req['da']
  257. resp['bssid'] = req['bssid']
  258. return resp
  259. def test_gas_invalid_response_type(dev, apdev):
  260. """GAS invalid response type"""
  261. hapd = start_ap(apdev[0])
  262. bssid = apdev[0]['bssid']
  263. dev[0].scan(freq="2412")
  264. hapd.set("ext_mgmt_frame_handling", "1")
  265. anqp_get(dev[0], bssid, 263)
  266. query = gas_rx(hapd)
  267. gas = parse_gas(query['payload'])
  268. resp = action_response(query)
  269. # GAS Comeback Response instead of GAS Initial Response
  270. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  271. hapd.mgmt_tx(resp)
  272. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  273. if ev is None:
  274. raise Exception("Missing TX status for GAS response")
  275. if "ok=1" not in ev:
  276. raise Exception("GAS response not acknowledged")
  277. # station drops the invalid frame, so this needs to result in GAS timeout
  278. expect_gas_result(dev[0], "TIMEOUT")