test_gas.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. # GAS tests
  2. # Copyright (c) 2013, Qualcomm Atheros, Inc.
  3. # Copyright (c) 2013-2015, 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. from utils import alloc_fail, skip_with_fips
  16. from hwsim import HWSimRadio
  17. def hs20_ap_params():
  18. params = hostapd.wpa2_params(ssid="test-gas")
  19. params['wpa_key_mgmt'] = "WPA-EAP"
  20. params['ieee80211w'] = "1"
  21. params['ieee8021x'] = "1"
  22. params['auth_server_addr'] = "127.0.0.1"
  23. params['auth_server_port'] = "1812"
  24. params['auth_server_shared_secret'] = "radius"
  25. params['interworking'] = "1"
  26. params['access_network_type'] = "14"
  27. params['internet'] = "1"
  28. params['asra'] = "0"
  29. params['esr'] = "0"
  30. params['uesa'] = "0"
  31. params['venue_group'] = "7"
  32. params['venue_type'] = "1"
  33. params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
  34. params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
  35. "fedcba" ]
  36. params['domain_name'] = "example.com,another.example.com"
  37. params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
  38. "0,another.example.com" ]
  39. params['anqp_3gpp_cell_net'] = "244,91"
  40. params['network_auth_type'] = "02http://www.example.com/redirect/me/here/"
  41. params['ipaddr_type_availability'] = "14"
  42. params['hs20'] = "1"
  43. params['hs20_oper_friendly_name'] = [ "eng:Example operator", "fin:Esimerkkioperaattori" ]
  44. params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
  45. params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
  46. params['hs20_operating_class'] = "5173"
  47. return params
  48. def start_ap(ap):
  49. params = hs20_ap_params()
  50. params['hessid'] = ap['bssid']
  51. return hostapd.add_ap(ap, params)
  52. def get_gas_response(dev, bssid, info, allow_fetch_failure=False,
  53. extra_test=False):
  54. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  55. res = re.split(exp, info)
  56. if len(res) < 6:
  57. raise Exception("Could not parse GAS-RESPONSE-INFO")
  58. if res[2] != bssid:
  59. raise Exception("Unexpected BSSID in response")
  60. token = res[3]
  61. status = res[4]
  62. if status != "0":
  63. raise Exception("GAS query failed")
  64. resp_len = res[5]
  65. if resp_len == "-1":
  66. raise Exception("GAS query reported invalid response length")
  67. if int(resp_len) > 2000:
  68. raise Exception("Unexpected long GAS response")
  69. if extra_test:
  70. if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " 123456"):
  71. raise Exception("Invalid dialog token accepted")
  72. if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 10000,10001"):
  73. raise Exception("Invalid range accepted")
  74. if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0,10000"):
  75. raise Exception("Invalid range accepted")
  76. if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0"):
  77. raise Exception("Invalid GAS_RESPONSE_GET accepted")
  78. res1_2 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 1,2")
  79. res5_3 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 5,3")
  80. resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
  81. if "FAIL" in resp:
  82. if allow_fetch_failure:
  83. logger.debug("GAS response was not available anymore")
  84. return
  85. raise Exception("Could not fetch GAS response")
  86. if len(resp) != int(resp_len) * 2:
  87. raise Exception("Unexpected GAS response length")
  88. logger.debug("GAS response: " + resp)
  89. if extra_test:
  90. if resp[2:6] != res1_2:
  91. raise Exception("Unexpected response substring res1_2: " + res1_2)
  92. if resp[10:16] != res5_3:
  93. raise Exception("Unexpected response substring res5_3: " + res5_3)
  94. def test_gas_generic(dev, apdev):
  95. """Generic GAS query"""
  96. bssid = apdev[0]['bssid']
  97. params = hs20_ap_params()
  98. params['hessid'] = bssid
  99. hostapd.add_ap(apdev[0], params)
  100. cmds = [ "foo",
  101. "00:11:22:33:44:55",
  102. "00:11:22:33:44:55 ",
  103. "00:11:22:33:44:55 ",
  104. "00:11:22:33:44:55 1",
  105. "00:11:22:33:44:55 1 1234",
  106. "00:11:22:33:44:55 qq",
  107. "00:11:22:33:44:55 qq 1234",
  108. "00:11:22:33:44:55 00 1",
  109. "00:11:22:33:44:55 00 123",
  110. "00:11:22:33:44:55 00 ",
  111. "00:11:22:33:44:55 00 qq" ]
  112. for cmd in cmds:
  113. if "FAIL" not in dev[0].request("GAS_REQUEST " + cmd):
  114. raise Exception("Invalid GAS_REQUEST accepted: " + cmd)
  115. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  116. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  117. if "FAIL" in req:
  118. raise Exception("GAS query request rejected")
  119. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  120. if ev is None:
  121. raise Exception("GAS query timed out")
  122. get_gas_response(dev[0], bssid, ev, extra_test=True)
  123. if "FAIL" not in dev[0].request("GAS_RESPONSE_GET ff"):
  124. raise Exception("Invalid GAS_RESPONSE_GET accepted")
  125. def test_gas_concurrent_scan(dev, apdev):
  126. """Generic GAS queries with concurrent scan operation"""
  127. bssid = apdev[0]['bssid']
  128. params = hs20_ap_params()
  129. params['hessid'] = bssid
  130. hostapd.add_ap(apdev[0], params)
  131. # get BSS entry available to allow GAS query
  132. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  133. logger.info("Request concurrent operations")
  134. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  135. if "FAIL" in req:
  136. raise Exception("GAS query request rejected")
  137. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
  138. if "FAIL" in req:
  139. raise Exception("GAS query request rejected")
  140. dev[0].scan(no_wait=True)
  141. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
  142. if "FAIL" in req:
  143. raise Exception("GAS query request rejected")
  144. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
  145. if "FAIL" in req:
  146. raise Exception("GAS query request rejected")
  147. responses = 0
  148. for i in range(0, 5):
  149. ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
  150. timeout=10)
  151. if ev is None:
  152. raise Exception("Operation timed out")
  153. if "GAS-RESPONSE-INFO" in ev:
  154. responses = responses + 1
  155. get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
  156. if responses != 4:
  157. raise Exception("Unexpected number of GAS responses")
  158. def test_gas_concurrent_connect(dev, apdev):
  159. """Generic GAS queries with concurrent connection operation"""
  160. skip_with_fips(dev[0])
  161. bssid = apdev[0]['bssid']
  162. params = hs20_ap_params()
  163. params['hessid'] = bssid
  164. hostapd.add_ap(apdev[0], params)
  165. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  166. logger.debug("Start concurrent connect and GAS request")
  167. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  168. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  169. password="password", phase2="auth=MSCHAPV2",
  170. ca_cert="auth_serv/ca.pem", wait_connect=False,
  171. scan_freq="2412")
  172. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  173. if "FAIL" in req:
  174. raise Exception("GAS query request rejected")
  175. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  176. timeout=20)
  177. if ev is None:
  178. raise Exception("Operation timed out")
  179. if "CTRL-EVENT-CONNECTED" not in ev:
  180. raise Exception("Unexpected operation order")
  181. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  182. timeout=20)
  183. if ev is None:
  184. raise Exception("Operation timed out")
  185. if "GAS-RESPONSE-INFO" not in ev:
  186. raise Exception("Unexpected operation order")
  187. get_gas_response(dev[0], bssid, ev)
  188. dev[0].request("DISCONNECT")
  189. dev[0].wait_disconnected(timeout=5)
  190. logger.debug("Wait six seconds for expiration of connect-without-scan")
  191. time.sleep(6)
  192. dev[0].dump_monitor()
  193. logger.debug("Start concurrent GAS request and connect")
  194. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  195. if "FAIL" in req:
  196. raise Exception("GAS query request rejected")
  197. dev[0].request("RECONNECT")
  198. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  199. if ev is None:
  200. raise Exception("Operation timed out")
  201. get_gas_response(dev[0], bssid, ev)
  202. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
  203. if ev is None:
  204. raise Exception("No new scan results reported")
  205. ev = dev[0].wait_connected(timeout=20, error="Operation tiemd out")
  206. if "CTRL-EVENT-CONNECTED" not in ev:
  207. raise Exception("Unexpected operation order")
  208. def gas_fragment_and_comeback(dev, apdev, frag_limit=0, comeback_delay=0):
  209. hapd = start_ap(apdev)
  210. if frag_limit:
  211. hapd.set("gas_frag_limit", str(frag_limit))
  212. if comeback_delay:
  213. hapd.set("gas_comeback_delay", str(comeback_delay))
  214. dev.scan_for_bss(apdev['bssid'], freq="2412", force_scan=True)
  215. dev.request("FETCH_ANQP")
  216. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=5)
  217. if ev is None:
  218. raise Exception("No GAS-QUERY-DONE event")
  219. if "result=SUCCESS" not in ev:
  220. raise Exception("Unexpected GAS result: " + ev)
  221. for i in range(0, 13):
  222. ev = dev.wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
  223. if ev is None:
  224. raise Exception("Operation timed out")
  225. ev = dev.wait_event(["ANQP-QUERY-DONE"], timeout=1)
  226. if ev is None:
  227. raise Exception("No ANQP-QUERY-DONE event")
  228. if "result=SUCCESS" not in ev:
  229. raise Exception("Unexpected ANQP result: " + ev)
  230. def test_gas_fragment(dev, apdev):
  231. """GAS fragmentation"""
  232. gas_fragment_and_comeback(dev[0], apdev[0], frag_limit=50)
  233. def test_gas_fragment_mcc(dev, apdev):
  234. """GAS fragmentation with mac80211_hwsim MCC enabled"""
  235. with HWSimRadio(n_channels=2) as (radio, iface):
  236. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  237. wpas.interface_add(iface)
  238. gas_fragment_and_comeback(wpas, apdev[0], frag_limit=50)
  239. def test_gas_fragment_with_comeback_delay(dev, apdev):
  240. """GAS fragmentation and comeback delay"""
  241. gas_fragment_and_comeback(dev[0], apdev[0], frag_limit=50,
  242. comeback_delay=500)
  243. def test_gas_fragment_with_comeback_delay_mcc(dev, apdev):
  244. """GAS fragmentation and comeback delay with mac80211_hwsim MCC enabled"""
  245. with HWSimRadio(n_channels=2) as (radio, iface):
  246. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  247. wpas.interface_add(iface)
  248. gas_fragment_and_comeback(wpas, apdev[0], frag_limit=50,
  249. comeback_delay=500)
  250. def test_gas_comeback_delay(dev, apdev):
  251. """GAS comeback delay"""
  252. hapd = start_ap(apdev[0])
  253. hapd.set("gas_comeback_delay", "500")
  254. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  255. dev[0].request("FETCH_ANQP")
  256. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  257. raise Exception("SCAN accepted during FETCH_ANQP")
  258. for i in range(0, 6):
  259. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  260. if ev is None:
  261. raise Exception("Operation timed out")
  262. def test_gas_stop_fetch_anqp(dev, apdev):
  263. """Stop FETCH_ANQP operation"""
  264. hapd = start_ap(apdev[0])
  265. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  266. hapd.set("ext_mgmt_frame_handling", "1")
  267. dev[0].request("FETCH_ANQP")
  268. dev[0].request("STOP_FETCH_ANQP")
  269. hapd.set("ext_mgmt_frame_handling", "0")
  270. ev = dev[0].wait_event(["RX-ANQP", "GAS-QUERY-DONE"], timeout=10)
  271. if ev is None:
  272. raise Exception("GAS-QUERY-DONE timed out")
  273. if "RX-ANQP" in ev:
  274. raise Exception("Unexpected ANQP response received")
  275. def test_gas_anqp_get(dev, apdev):
  276. """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
  277. hapd = start_ap(apdev[0])
  278. bssid = apdev[0]['bssid']
  279. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  280. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
  281. raise Exception("ANQP_GET command failed")
  282. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  283. if ev is None:
  284. raise Exception("GAS query start timed out")
  285. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  286. if ev is None:
  287. raise Exception("GAS query timed out")
  288. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  289. if ev is None or "Venue Name" not in ev:
  290. raise Exception("Did not receive Venue Name")
  291. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  292. if ev is None or "Domain Name list" not in ev:
  293. raise Exception("Did not receive Domain Name list")
  294. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  295. if ev is None or "Operator Friendly Name" not in ev:
  296. raise Exception("Did not receive Operator Friendly Name")
  297. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  298. if ev is None or "WAN Metrics" not in ev:
  299. raise Exception("Did not receive WAN Metrics")
  300. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  301. if ev is None:
  302. raise Exception("ANQP-QUERY-DONE event not seen")
  303. if "result=SUCCESS" not in ev:
  304. raise Exception("Unexpected result: " + ev)
  305. if "OK" not in dev[0].request("HS20_ANQP_GET " + bssid + " 3,4"):
  306. raise Exception("ANQP_GET command failed")
  307. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  308. if ev is None or "Operator Friendly Name" not in ev:
  309. raise Exception("Did not receive Operator Friendly Name")
  310. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  311. if ev is None or "WAN Metrics" not in ev:
  312. raise Exception("Did not receive WAN Metrics")
  313. cmds = [ "",
  314. "foo",
  315. "00:11:22:33:44:55 258,hs20:-1",
  316. "00:11:22:33:44:55 258,hs20:0",
  317. "00:11:22:33:44:55 258,hs20:32",
  318. "00:11:22:33:44:55 hs20:-1",
  319. "00:11:22:33:44:55 hs20:0",
  320. "00:11:22:33:44:55 hs20:32",
  321. "00:11:22:33:44:55",
  322. "00:11:22:33:44:55 ",
  323. "00:11:22:33:44:55 0" ]
  324. for cmd in cmds:
  325. if "FAIL" not in dev[0].request("ANQP_GET " + cmd):
  326. raise Exception("Invalid ANQP_GET accepted")
  327. cmds = [ "",
  328. "foo",
  329. "00:11:22:33:44:55 -1",
  330. "00:11:22:33:44:55 0",
  331. "00:11:22:33:44:55 32",
  332. "00:11:22:33:44:55",
  333. "00:11:22:33:44:55 ",
  334. "00:11:22:33:44:55 0" ]
  335. for cmd in cmds:
  336. if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
  337. raise Exception("Invalid HS20_ANQP_GET accepted")
  338. def expect_gas_result(dev, result, status=None):
  339. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  340. if ev is None:
  341. raise Exception("GAS query timed out")
  342. if "result=" + result not in ev:
  343. raise Exception("Unexpected GAS query result")
  344. if status and "status_code=" + str(status) + ' ' not in ev:
  345. raise Exception("Unexpected GAS status code")
  346. def anqp_get(dev, bssid, id):
  347. if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
  348. raise Exception("ANQP_GET command failed")
  349. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  350. if ev is None:
  351. raise Exception("GAS query start timed out")
  352. def test_gas_timeout(dev, apdev):
  353. """GAS timeout"""
  354. hapd = start_ap(apdev[0])
  355. bssid = apdev[0]['bssid']
  356. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  357. hapd.set("ext_mgmt_frame_handling", "1")
  358. anqp_get(dev[0], bssid, 263)
  359. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  360. if ev is None:
  361. raise Exception("MGMT RX wait timed out")
  362. expect_gas_result(dev[0], "TIMEOUT")
  363. MGMT_SUBTYPE_ACTION = 13
  364. ACTION_CATEG_PUBLIC = 4
  365. GAS_INITIAL_REQUEST = 10
  366. GAS_INITIAL_RESPONSE = 11
  367. GAS_COMEBACK_REQUEST = 12
  368. GAS_COMEBACK_RESPONSE = 13
  369. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  370. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  371. def anqp_adv_proto():
  372. return struct.pack('BBBB', 108, 2, 127, 0)
  373. def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
  374. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  375. dialog_token, status_code, comeback_delay) + anqp_adv_proto()
  376. def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
  377. if more:
  378. id |= 0x80
  379. if bogus_adv_proto:
  380. adv = struct.pack('BBBB', 108, 2, 127, 1)
  381. else:
  382. adv = anqp_adv_proto()
  383. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  384. dialog_token, status_code, id, comeback_delay) + adv
  385. def gas_rx(hapd):
  386. count = 0
  387. while count < 30:
  388. count = count + 1
  389. query = hapd.mgmt_rx()
  390. if query is None:
  391. raise Exception("Action frame not received")
  392. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  393. continue
  394. payload = query['payload']
  395. if len(payload) < 2:
  396. continue
  397. (category, action) = struct.unpack('BB', payload[0:2])
  398. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  399. continue
  400. return query
  401. raise Exception("No Action frame received")
  402. def parse_gas(payload):
  403. pos = payload
  404. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  405. if category != ACTION_CATEG_PUBLIC:
  406. return None
  407. if action not in GAS_ACTIONS:
  408. return None
  409. gas = {}
  410. gas['action'] = action
  411. pos = pos[3:]
  412. if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
  413. return None
  414. gas['dialog_token'] = dialog_token
  415. if action == GAS_INITIAL_RESPONSE:
  416. if len(pos) < 4:
  417. return None
  418. (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
  419. gas['status_code'] = status_code
  420. gas['comeback_delay'] = comeback_delay
  421. if action == GAS_COMEBACK_RESPONSE:
  422. if len(pos) < 5:
  423. return None
  424. (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
  425. gas['status_code'] = status_code
  426. gas['frag'] = frag
  427. gas['comeback_delay'] = comeback_delay
  428. return gas
  429. def action_response(req):
  430. resp = {}
  431. resp['fc'] = req['fc']
  432. resp['da'] = req['sa']
  433. resp['sa'] = req['da']
  434. resp['bssid'] = req['bssid']
  435. return resp
  436. def send_gas_resp(hapd, resp):
  437. hapd.mgmt_tx(resp)
  438. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  439. if ev is None:
  440. raise Exception("Missing TX status for GAS response")
  441. if "ok=1" not in ev:
  442. raise Exception("GAS response not acknowledged")
  443. def test_gas_invalid_response_type(dev, apdev):
  444. """GAS invalid response type"""
  445. hapd = start_ap(apdev[0])
  446. bssid = apdev[0]['bssid']
  447. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  448. hapd.set("ext_mgmt_frame_handling", "1")
  449. anqp_get(dev[0], bssid, 263)
  450. query = gas_rx(hapd)
  451. gas = parse_gas(query['payload'])
  452. resp = action_response(query)
  453. # GAS Comeback Response instead of GAS Initial Response
  454. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  455. send_gas_resp(hapd, resp)
  456. # station drops the invalid frame, so this needs to result in GAS timeout
  457. expect_gas_result(dev[0], "TIMEOUT")
  458. def test_gas_failure_status_code(dev, apdev):
  459. """GAS failure status code"""
  460. hapd = start_ap(apdev[0])
  461. bssid = apdev[0]['bssid']
  462. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  463. hapd.set("ext_mgmt_frame_handling", "1")
  464. anqp_get(dev[0], bssid, 263)
  465. query = gas_rx(hapd)
  466. gas = parse_gas(query['payload'])
  467. resp = action_response(query)
  468. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  469. send_gas_resp(hapd, resp)
  470. expect_gas_result(dev[0], "FAILURE")
  471. def test_gas_malformed(dev, apdev):
  472. """GAS malformed response frames"""
  473. hapd = start_ap(apdev[0])
  474. bssid = apdev[0]['bssid']
  475. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  476. hapd.set("ext_mgmt_frame_handling", "1")
  477. anqp_get(dev[0], bssid, 263)
  478. query = gas_rx(hapd)
  479. gas = parse_gas(query['payload'])
  480. resp = action_response(query)
  481. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  482. GAS_COMEBACK_RESPONSE,
  483. gas['dialog_token'], 0)
  484. hapd.mgmt_tx(resp)
  485. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  486. GAS_COMEBACK_RESPONSE,
  487. gas['dialog_token'], 0, 0)
  488. hapd.mgmt_tx(resp)
  489. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  490. gas['dialog_token'], 0, 0)
  491. resp['payload'] = hdr + struct.pack('B', 108)
  492. hapd.mgmt_tx(resp)
  493. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  494. hapd.mgmt_tx(resp)
  495. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  496. hapd.mgmt_tx(resp)
  497. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  498. hapd.mgmt_tx(resp)
  499. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  500. hapd.mgmt_tx(resp)
  501. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  502. hapd.mgmt_tx(resp)
  503. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  504. hapd.mgmt_tx(resp)
  505. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  506. hapd.mgmt_tx(resp)
  507. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  508. hapd.mgmt_tx(resp)
  509. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  510. hapd.mgmt_tx(resp)
  511. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  512. hapd.mgmt_tx(resp)
  513. # Station drops invalid frames, but the last of the responses is valid from
  514. # GAS view point even though it has an extra octet in the end and the ANQP
  515. # part of the response is not valid. This is reported as successfully
  516. # completed GAS exchange.
  517. expect_gas_result(dev[0], "SUCCESS")
  518. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  519. if ev is None:
  520. raise Exception("ANQP-QUERY-DONE not reported")
  521. if "result=INVALID_FRAME" not in ev:
  522. raise Exception("Unexpected result: " + ev)
  523. def init_gas(hapd, bssid, dev):
  524. anqp_get(dev, bssid, 263)
  525. query = gas_rx(hapd)
  526. gas = parse_gas(query['payload'])
  527. dialog_token = gas['dialog_token']
  528. resp = action_response(query)
  529. resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
  530. send_gas_resp(hapd, resp)
  531. query = gas_rx(hapd)
  532. gas = parse_gas(query['payload'])
  533. if gas['action'] != GAS_COMEBACK_REQUEST:
  534. raise Exception("Unexpected request action")
  535. if gas['dialog_token'] != dialog_token:
  536. raise Exception("Unexpected dialog token change")
  537. return query, dialog_token
  538. def allow_gas_initial_req(hapd, dialog_token):
  539. msg = hapd.mgmt_rx(timeout=1)
  540. if msg is not None:
  541. gas = parse_gas(msg['payload'])
  542. if gas['action'] != GAS_INITIAL_REQUEST or dialog_token == gas['dialog_token']:
  543. raise Exception("Unexpected management frame")
  544. def test_gas_malformed_comeback_resp(dev, apdev):
  545. """GAS malformed comeback response frames"""
  546. hapd = start_ap(apdev[0])
  547. bssid = apdev[0]['bssid']
  548. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  549. hapd.set("ext_mgmt_frame_handling", "1")
  550. logger.debug("Non-zero status code in comeback response")
  551. query, dialog_token = init_gas(hapd, bssid, dev[0])
  552. resp = action_response(query)
  553. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
  554. send_gas_resp(hapd, resp)
  555. expect_gas_result(dev[0], "FAILURE", status=2)
  556. logger.debug("Different advertisement protocol in comeback response")
  557. query, dialog_token = init_gas(hapd, bssid, dev[0])
  558. resp = action_response(query)
  559. resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
  560. send_gas_resp(hapd, resp)
  561. expect_gas_result(dev[0], "PEER_ERROR")
  562. logger.debug("Non-zero frag id and comeback delay in comeback response")
  563. query, dialog_token = init_gas(hapd, bssid, dev[0])
  564. resp = action_response(query)
  565. resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
  566. send_gas_resp(hapd, resp)
  567. expect_gas_result(dev[0], "PEER_ERROR")
  568. logger.debug("Unexpected frag id in comeback response")
  569. query, dialog_token = init_gas(hapd, bssid, dev[0])
  570. resp = action_response(query)
  571. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  572. send_gas_resp(hapd, resp)
  573. expect_gas_result(dev[0], "PEER_ERROR")
  574. logger.debug("Empty fragment and replay in comeback response")
  575. query, dialog_token = init_gas(hapd, bssid, dev[0])
  576. resp = action_response(query)
  577. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  578. send_gas_resp(hapd, resp)
  579. query = gas_rx(hapd)
  580. gas = parse_gas(query['payload'])
  581. if gas['action'] != GAS_COMEBACK_REQUEST:
  582. raise Exception("Unexpected request action")
  583. if gas['dialog_token'] != dialog_token:
  584. raise Exception("Unexpected dialog token change")
  585. resp = action_response(query)
  586. resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
  587. send_gas_resp(hapd, resp)
  588. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  589. send_gas_resp(hapd, resp)
  590. expect_gas_result(dev[0], "SUCCESS")
  591. logger.debug("Unexpected initial response when waiting for comeback response")
  592. query, dialog_token = init_gas(hapd, bssid, dev[0])
  593. resp = action_response(query)
  594. resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
  595. send_gas_resp(hapd, resp)
  596. allow_gas_initial_req(hapd, dialog_token)
  597. expect_gas_result(dev[0], "TIMEOUT")
  598. logger.debug("Too short comeback response")
  599. query, dialog_token = init_gas(hapd, bssid, dev[0])
  600. resp = action_response(query)
  601. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  602. GAS_COMEBACK_RESPONSE, dialog_token, 0)
  603. send_gas_resp(hapd, resp)
  604. allow_gas_initial_req(hapd, dialog_token)
  605. expect_gas_result(dev[0], "TIMEOUT")
  606. logger.debug("Too short comeback response(2)")
  607. query, dialog_token = init_gas(hapd, bssid, dev[0])
  608. resp = action_response(query)
  609. resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
  610. GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
  611. 0)
  612. send_gas_resp(hapd, resp)
  613. allow_gas_initial_req(hapd, dialog_token)
  614. expect_gas_result(dev[0], "TIMEOUT")
  615. logger.debug("Maximum comeback response fragment claiming more fragments")
  616. query, dialog_token = init_gas(hapd, bssid, dev[0])
  617. resp = action_response(query)
  618. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  619. send_gas_resp(hapd, resp)
  620. for i in range(1, 129):
  621. query = gas_rx(hapd)
  622. gas = parse_gas(query['payload'])
  623. if gas['action'] != GAS_COMEBACK_REQUEST:
  624. raise Exception("Unexpected request action")
  625. if gas['dialog_token'] != dialog_token:
  626. raise Exception("Unexpected dialog token change")
  627. resp = action_response(query)
  628. resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
  629. send_gas_resp(hapd, resp)
  630. expect_gas_result(dev[0], "PEER_ERROR")
  631. def test_gas_comeback_resp_additional_delay(dev, apdev):
  632. """GAS comeback response requesting additional delay"""
  633. hapd = start_ap(apdev[0])
  634. bssid = apdev[0]['bssid']
  635. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  636. hapd.set("ext_mgmt_frame_handling", "1")
  637. query, dialog_token = init_gas(hapd, bssid, dev[0])
  638. for i in range(0, 2):
  639. resp = action_response(query)
  640. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
  641. send_gas_resp(hapd, resp)
  642. query = gas_rx(hapd)
  643. gas = parse_gas(query['payload'])
  644. if gas['action'] != GAS_COMEBACK_REQUEST:
  645. raise Exception("Unexpected request action")
  646. if gas['dialog_token'] != dialog_token:
  647. raise Exception("Unexpected dialog token change")
  648. resp = action_response(query)
  649. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
  650. send_gas_resp(hapd, resp)
  651. expect_gas_result(dev[0], "SUCCESS")
  652. def test_gas_unknown_adv_proto(dev, apdev):
  653. """Unknown advertisement protocol id"""
  654. bssid = apdev[0]['bssid']
  655. params = hs20_ap_params()
  656. params['hessid'] = bssid
  657. hostapd.add_ap(apdev[0], params)
  658. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  659. req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
  660. if "FAIL" in req:
  661. raise Exception("GAS query request rejected")
  662. expect_gas_result(dev[0], "FAILURE", "59")
  663. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  664. if ev is None:
  665. raise Exception("GAS query timed out")
  666. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  667. res = re.split(exp, ev)
  668. if len(res) < 6:
  669. raise Exception("Could not parse GAS-RESPONSE-INFO")
  670. if res[2] != bssid:
  671. raise Exception("Unexpected BSSID in response")
  672. status = res[4]
  673. if status != "59":
  674. raise Exception("Unexpected GAS-RESPONSE-INFO status")
  675. def test_gas_max_pending(dev, apdev):
  676. """GAS and maximum pending query limit"""
  677. hapd = start_ap(apdev[0])
  678. hapd.set("gas_frag_limit", "50")
  679. bssid = apdev[0]['bssid']
  680. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  681. wpas.interface_add("wlan5")
  682. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  683. raise Exception("Failed to set listen channel")
  684. if "OK" not in wpas.p2p_listen():
  685. raise Exception("Failed to start listen state")
  686. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  687. raise Exception("Failed to enable external management frame handling")
  688. anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
  689. gas = struct.pack('<H', len(anqp_query)) + anqp_query
  690. for dialog_token in range(1, 10):
  691. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
  692. dialog_token) + anqp_adv_proto() + gas
  693. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  694. if "OK" not in wpas.request(req):
  695. raise Exception("Could not send management frame")
  696. resp = wpas.mgmt_rx()
  697. if resp is None:
  698. raise Exception("MGMT-RX timeout")
  699. if 'payload' not in resp:
  700. raise Exception("Missing payload")
  701. gresp = parse_gas(resp['payload'])
  702. if gresp['dialog_token'] != dialog_token:
  703. raise Exception("Dialog token mismatch")
  704. status_code = gresp['status_code']
  705. if dialog_token < 9 and status_code != 0:
  706. raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
  707. if dialog_token > 8 and status_code == 0:
  708. raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
  709. def test_gas_no_pending(dev, apdev):
  710. """GAS and no pending query for comeback request"""
  711. hapd = start_ap(apdev[0])
  712. bssid = apdev[0]['bssid']
  713. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  714. wpas.interface_add("wlan5")
  715. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  716. raise Exception("Failed to set listen channel")
  717. if "OK" not in wpas.p2p_listen():
  718. raise Exception("Failed to start listen state")
  719. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  720. raise Exception("Failed to enable external management frame handling")
  721. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
  722. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  723. if "OK" not in wpas.request(req):
  724. raise Exception("Could not send management frame")
  725. resp = wpas.mgmt_rx()
  726. if resp is None:
  727. raise Exception("MGMT-RX timeout")
  728. if 'payload' not in resp:
  729. raise Exception("Missing payload")
  730. gresp = parse_gas(resp['payload'])
  731. status_code = gresp['status_code']
  732. if status_code != 60:
  733. raise Exception("Unexpected status code {} (expected 60)".format(status_code))
  734. def test_gas_missing_payload(dev, apdev):
  735. """No action code in the query frame"""
  736. bssid = apdev[0]['bssid']
  737. params = hs20_ap_params()
  738. params['hessid'] = bssid
  739. hostapd.add_ap(apdev[0], params)
  740. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  741. cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
  742. if "FAIL" in dev[0].request(cmd):
  743. raise Exception("Could not send test Action frame")
  744. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  745. if ev is None:
  746. raise Exception("Timeout on MGMT-TX-STATUS")
  747. if "result=SUCCESS" not in ev:
  748. raise Exception("AP did not ack Action frame")
  749. cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
  750. if "FAIL" in dev[0].request(cmd):
  751. raise Exception("Could not send test Action frame")
  752. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  753. if ev is None:
  754. raise Exception("Timeout on MGMT-TX-STATUS")
  755. if "result=SUCCESS" not in ev:
  756. raise Exception("AP did not ack Action frame")
  757. def test_gas_query_deinit(dev, apdev):
  758. """Pending GAS/ANQP query during deinit"""
  759. hapd = start_ap(apdev[0])
  760. bssid = apdev[0]['bssid']
  761. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  762. wpas.interface_add("wlan5")
  763. wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
  764. id = wpas.request("RADIO_WORK add block-work")
  765. if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
  766. raise Exception("ANQP_GET command failed")
  767. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  768. if ev is None:
  769. raise Exception("Timeout while waiting radio work to start")
  770. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  771. if ev is None:
  772. raise Exception("Timeout while waiting radio work to start (2)")
  773. # Remove the interface while the gas-query radio work is still pending and
  774. # GAS query has not yet been started.
  775. wpas.interface_remove("wlan5")
  776. def test_gas_anqp_oom_wpas(dev, apdev):
  777. """GAS/ANQP query and OOM in wpa_supplicant"""
  778. hapd = start_ap(apdev[0])
  779. bssid = apdev[0]['bssid']
  780. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  781. with alloc_fail(dev[0], 1, "gas_build_req"):
  782. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  783. raise Exception("Unexpected ANQP_GET command success (OOM)")
  784. def test_gas_anqp_oom_hapd(dev, apdev):
  785. """GAS/ANQP query and OOM in hostapd"""
  786. hapd = start_ap(apdev[0])
  787. bssid = apdev[0]['bssid']
  788. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  789. with alloc_fail(hapd, 1, "gas_build_resp"):
  790. # This query will time out due to the AP not sending a response (OOM).
  791. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  792. raise Exception("ANQP_GET command failed")
  793. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  794. if ev is None:
  795. raise Exception("GAS query start timed out")
  796. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  797. if ev is None:
  798. raise Exception("GAS query timed out")
  799. if "result=TIMEOUT" not in ev:
  800. raise Exception("Unexpected result: " + ev)
  801. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  802. if ev is None:
  803. raise Exception("ANQP-QUERY-DONE event not seen")
  804. if "result=FAILURE" not in ev:
  805. raise Exception("Unexpected result: " + ev)
  806. with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
  807. hapd.set("gas_frag_limit", "50")
  808. # The first attempt of this query will time out due to the AP not
  809. # sending a response (OOM), but the retry succeeds.
  810. dev[0].request("FETCH_ANQP")
  811. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  812. if ev is None:
  813. raise Exception("GAS query start timed out")
  814. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  815. if ev is None:
  816. raise Exception("GAS query timed out")
  817. if "result=SUCCESS" not in ev:
  818. raise Exception("Unexpected result: " + ev)
  819. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  820. if ev is None:
  821. raise Exception("ANQP-QUERY-DONE event not seen")
  822. if "result=SUCCESS" not in ev:
  823. raise Exception("Unexpected result: " + ev)
  824. def test_gas_anqp_extra_elements(dev, apdev):
  825. """GAS/ANQP and extra ANQP elements"""
  826. geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100"
  827. civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5"
  828. held_uri = "https://held.example.com/location"
  829. held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri
  830. supl_fqdn = "supl.example.com"
  831. supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn
  832. public_id = binascii.hexlify(held + supl)
  833. params = { "ssid": "gas/anqp",
  834. "interworking": "1",
  835. "anqp_elem": [ "265:" + geo_loc,
  836. "266:" + civic_loc,
  837. "262:1122334455",
  838. "267:" + public_id,
  839. "275:01020304",
  840. "60000:01",
  841. "299:0102" ] }
  842. hapd = hostapd.add_ap(apdev[0], params)
  843. bssid = apdev[0]['bssid']
  844. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  845. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 265,266"):
  846. raise Exception("ANQP_GET command failed")
  847. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  848. if ev is None:
  849. raise Exception("GAS query timed out")
  850. bss = dev[0].get_bss(bssid)
  851. if 'anqp[265]' not in bss:
  852. raise Exception("AP Geospatial Location ANQP-element not seen")
  853. if bss['anqp[265]'] != geo_loc:
  854. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  855. if 'anqp[266]' not in bss:
  856. raise Exception("AP Civic Location ANQP-element not seen")
  857. if bss['anqp[266]'] != civic_loc:
  858. raise Exception("Unexpected AP Civic Location ANQP-element value: " + bss['anqp[266]'])
  859. dev[1].scan_for_bss(bssid, freq="2412", force_scan=True)
  860. if "OK" not in dev[1].request("ANQP_GET " + bssid + " 257,258,259,260,261,262,263,264,265,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299"):
  861. raise Exception("ANQP_GET command failed")
  862. ev = dev[1].wait_event(["GAS-QUERY-DONE"], timeout=10)
  863. if ev is None:
  864. raise Exception("GAS query timed out")
  865. bss = dev[1].get_bss(bssid)
  866. if 'anqp[265]' not in bss:
  867. raise Exception("AP Geospatial Location ANQP-element not seen")
  868. if bss['anqp[265]'] != geo_loc:
  869. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  870. if 'anqp[266]' in bss:
  871. raise Exception("AP Civic Location ANQP-element unexpectedly seen")
  872. if 'anqp[267]' not in bss:
  873. raise Exception("AP Location Public Identifier ANQP-element not seen")
  874. if bss['anqp[267]'] != public_id:
  875. raise Exception("Unexpected AP Location Public Identifier ANQP-element value: " + bss['anqp[267]'])
  876. if 'anqp[275]' not in bss:
  877. raise Exception("ANQP-element Info ID 275 not seen")
  878. if bss['anqp[275]'] != "01020304":
  879. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  880. if 'anqp[299]' not in bss:
  881. raise Exception("ANQP-element Info ID 299 not seen")
  882. if bss['anqp[299]'] != "0102":
  883. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  884. if 'anqp_ip_addr_type_availability' not in bss:
  885. raise Exception("ANQP-element Info ID 292 not seen")
  886. if bss['anqp_ip_addr_type_availability'] != "1122334455":
  887. raise Exception("Unexpected AP ANQP-element Info ID 262 value: " + bss['anqp_ip_addr_type_availability'])