test_gas.py 41 KB

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