test_gas.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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. from remotehost import remote_compatible
  8. import time
  9. import binascii
  10. import logging
  11. logger = logging.getLogger()
  12. import os
  13. import re
  14. import struct
  15. import hostapd
  16. from wpasupplicant import WpaSupplicant
  17. from tshark import run_tshark
  18. from utils import alloc_fail, wait_fail_trigger, skip_with_fips
  19. from hwsim import HWSimRadio
  20. def hs20_ap_params():
  21. params = hostapd.wpa2_params(ssid="test-gas")
  22. params['wpa_key_mgmt'] = "WPA-EAP"
  23. params['ieee80211w'] = "1"
  24. params['ieee8021x'] = "1"
  25. params['auth_server_addr'] = "127.0.0.1"
  26. params['auth_server_port'] = "1812"
  27. params['auth_server_shared_secret'] = "radius"
  28. params['interworking'] = "1"
  29. params['access_network_type'] = "14"
  30. params['internet'] = "1"
  31. params['asra'] = "0"
  32. params['esr'] = "0"
  33. params['uesa'] = "0"
  34. params['venue_group'] = "7"
  35. params['venue_type'] = "1"
  36. params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
  37. params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
  38. "fedcba" ]
  39. params['domain_name'] = "example.com,another.example.com"
  40. params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
  41. "0,another.example.com" ]
  42. params['anqp_3gpp_cell_net'] = "244,91"
  43. params['network_auth_type'] = "02http://www.example.com/redirect/me/here/"
  44. params['ipaddr_type_availability'] = "14"
  45. params['hs20'] = "1"
  46. params['hs20_oper_friendly_name'] = [ "eng:Example operator", "fin:Esimerkkioperaattori" ]
  47. params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
  48. params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
  49. params['hs20_operating_class'] = "5173"
  50. return params
  51. def start_ap(ap):
  52. params = hs20_ap_params()
  53. params['hessid'] = ap['bssid']
  54. return hostapd.add_ap(ap, params)
  55. def get_gas_response(dev, bssid, info, allow_fetch_failure=False,
  56. extra_test=False):
  57. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  58. res = re.split(exp, info)
  59. if len(res) < 6:
  60. raise Exception("Could not parse GAS-RESPONSE-INFO")
  61. if res[2] != bssid:
  62. raise Exception("Unexpected BSSID in response")
  63. token = res[3]
  64. status = res[4]
  65. if status != "0":
  66. raise Exception("GAS query failed")
  67. resp_len = res[5]
  68. if resp_len == "-1":
  69. raise Exception("GAS query reported invalid response length")
  70. if int(resp_len) > 2000:
  71. raise Exception("Unexpected long GAS response")
  72. if extra_test:
  73. if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " 123456"):
  74. raise Exception("Invalid dialog token accepted")
  75. if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 10000,10001"):
  76. raise Exception("Invalid range accepted")
  77. if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0,10000"):
  78. raise Exception("Invalid range accepted")
  79. if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0"):
  80. raise Exception("Invalid GAS_RESPONSE_GET accepted")
  81. res1_2 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 1,2")
  82. res5_3 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 5,3")
  83. resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
  84. if "FAIL" in resp:
  85. if allow_fetch_failure:
  86. logger.debug("GAS response was not available anymore")
  87. return
  88. raise Exception("Could not fetch GAS response")
  89. if len(resp) != int(resp_len) * 2:
  90. raise Exception("Unexpected GAS response length")
  91. logger.debug("GAS response: " + resp)
  92. if extra_test:
  93. if resp[2:6] != res1_2:
  94. raise Exception("Unexpected response substring res1_2: " + res1_2)
  95. if resp[10:16] != res5_3:
  96. raise Exception("Unexpected response substring res5_3: " + res5_3)
  97. def test_gas_generic(dev, apdev):
  98. """Generic GAS query"""
  99. bssid = apdev[0]['bssid']
  100. params = hs20_ap_params()
  101. params['hessid'] = bssid
  102. hostapd.add_ap(apdev[0], params)
  103. cmds = [ "foo",
  104. "00:11:22:33:44:55",
  105. "00:11:22:33:44:55 ",
  106. "00:11:22:33:44:55 ",
  107. "00:11:22:33:44:55 1",
  108. "00:11:22:33:44:55 1 1234",
  109. "00:11:22:33:44:55 qq",
  110. "00:11:22:33:44:55 qq 1234",
  111. "00:11:22:33:44:55 00 1",
  112. "00:11:22:33:44:55 00 123",
  113. "00:11:22:33:44:55 00 ",
  114. "00:11:22:33:44:55 00 qq" ]
  115. for cmd in cmds:
  116. if "FAIL" not in dev[0].request("GAS_REQUEST " + cmd):
  117. raise Exception("Invalid GAS_REQUEST accepted: " + cmd)
  118. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  119. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  120. if "FAIL" in req:
  121. raise Exception("GAS query request rejected")
  122. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  123. if ev is None:
  124. raise Exception("GAS query timed out")
  125. get_gas_response(dev[0], bssid, ev, extra_test=True)
  126. if "FAIL" not in dev[0].request("GAS_RESPONSE_GET ff"):
  127. raise Exception("Invalid GAS_RESPONSE_GET accepted")
  128. def test_gas_concurrent_scan(dev, apdev):
  129. """Generic GAS queries with concurrent scan operation"""
  130. bssid = apdev[0]['bssid']
  131. params = hs20_ap_params()
  132. params['hessid'] = bssid
  133. hostapd.add_ap(apdev[0], params)
  134. # get BSS entry available to allow GAS query
  135. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  136. logger.info("Request concurrent operations")
  137. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  138. if "FAIL" in req:
  139. raise Exception("GAS query request rejected")
  140. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
  141. if "FAIL" in req:
  142. raise Exception("GAS query request rejected")
  143. dev[0].scan(no_wait=True)
  144. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
  145. if "FAIL" in req:
  146. raise Exception("GAS query request rejected")
  147. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
  148. if "FAIL" in req:
  149. raise Exception("GAS query request rejected")
  150. responses = 0
  151. for i in range(0, 5):
  152. ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
  153. timeout=10)
  154. if ev is None:
  155. raise Exception("Operation timed out")
  156. if "GAS-RESPONSE-INFO" in ev:
  157. responses = responses + 1
  158. get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
  159. if responses != 4:
  160. raise Exception("Unexpected number of GAS responses")
  161. def test_gas_concurrent_connect(dev, apdev):
  162. """Generic GAS queries with concurrent connection operation"""
  163. skip_with_fips(dev[0])
  164. bssid = apdev[0]['bssid']
  165. params = hs20_ap_params()
  166. params['hessid'] = bssid
  167. hostapd.add_ap(apdev[0], params)
  168. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  169. logger.debug("Start concurrent connect and GAS request")
  170. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  171. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  172. password="password", phase2="auth=MSCHAPV2",
  173. ca_cert="auth_serv/ca.pem", wait_connect=False,
  174. scan_freq="2412")
  175. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  176. if "FAIL" in req:
  177. raise Exception("GAS query request rejected")
  178. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  179. timeout=20)
  180. if ev is None:
  181. raise Exception("Operation timed out")
  182. if "CTRL-EVENT-CONNECTED" not in ev:
  183. raise Exception("Unexpected operation order")
  184. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
  185. timeout=20)
  186. if ev is None:
  187. raise Exception("Operation timed out")
  188. if "GAS-RESPONSE-INFO" not in ev:
  189. raise Exception("Unexpected operation order")
  190. get_gas_response(dev[0], bssid, ev)
  191. dev[0].request("DISCONNECT")
  192. dev[0].wait_disconnected(timeout=5)
  193. logger.debug("Wait six seconds for expiration of connect-without-scan")
  194. time.sleep(6)
  195. dev[0].dump_monitor()
  196. logger.debug("Start concurrent GAS request and connect")
  197. req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
  198. if "FAIL" in req:
  199. raise Exception("GAS query request rejected")
  200. dev[0].request("RECONNECT")
  201. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  202. if ev is None:
  203. raise Exception("Operation timed out")
  204. get_gas_response(dev[0], bssid, ev)
  205. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
  206. if ev is None:
  207. raise Exception("No new scan results reported")
  208. ev = dev[0].wait_connected(timeout=20, error="Operation tiemd out")
  209. if "CTRL-EVENT-CONNECTED" not in ev:
  210. raise Exception("Unexpected operation order")
  211. def gas_fragment_and_comeback(dev, apdev, frag_limit=0, comeback_delay=0):
  212. hapd = start_ap(apdev)
  213. if frag_limit:
  214. hapd.set("gas_frag_limit", str(frag_limit))
  215. if comeback_delay:
  216. hapd.set("gas_comeback_delay", str(comeback_delay))
  217. dev.scan_for_bss(apdev['bssid'], freq="2412", force_scan=True)
  218. dev.request("FETCH_ANQP")
  219. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=5)
  220. if ev is None:
  221. raise Exception("No GAS-QUERY-DONE event")
  222. if "result=SUCCESS" not in ev:
  223. raise Exception("Unexpected GAS result: " + ev)
  224. for i in range(0, 13):
  225. ev = dev.wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
  226. if ev is None:
  227. raise Exception("Operation timed out")
  228. ev = dev.wait_event(["ANQP-QUERY-DONE"], timeout=1)
  229. if ev is None:
  230. raise Exception("No ANQP-QUERY-DONE event")
  231. if "result=SUCCESS" not in ev:
  232. raise Exception("Unexpected ANQP result: " + ev)
  233. def test_gas_fragment(dev, apdev):
  234. """GAS fragmentation"""
  235. gas_fragment_and_comeback(dev[0], apdev[0], frag_limit=50)
  236. def test_gas_fragment_mcc(dev, apdev):
  237. """GAS fragmentation with mac80211_hwsim MCC enabled"""
  238. with HWSimRadio(n_channels=2) as (radio, iface):
  239. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  240. wpas.interface_add(iface)
  241. gas_fragment_and_comeback(wpas, apdev[0], frag_limit=50)
  242. def test_gas_fragment_with_comeback_delay(dev, apdev):
  243. """GAS fragmentation and comeback delay"""
  244. gas_fragment_and_comeback(dev[0], apdev[0], frag_limit=50,
  245. comeback_delay=500)
  246. def test_gas_fragment_with_comeback_delay_mcc(dev, apdev):
  247. """GAS fragmentation and comeback delay with mac80211_hwsim MCC enabled"""
  248. with HWSimRadio(n_channels=2) as (radio, iface):
  249. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  250. wpas.interface_add(iface)
  251. gas_fragment_and_comeback(wpas, apdev[0], frag_limit=50,
  252. comeback_delay=500)
  253. def test_gas_comeback_delay(dev, apdev):
  254. """GAS comeback delay"""
  255. hapd = start_ap(apdev[0])
  256. hapd.set("gas_comeback_delay", "500")
  257. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  258. dev[0].request("FETCH_ANQP")
  259. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  260. raise Exception("SCAN accepted during FETCH_ANQP")
  261. for i in range(0, 6):
  262. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  263. if ev is None:
  264. raise Exception("Operation timed out")
  265. @remote_compatible
  266. def test_gas_stop_fetch_anqp(dev, apdev):
  267. """Stop FETCH_ANQP operation"""
  268. hapd = start_ap(apdev[0])
  269. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  270. hapd.set("ext_mgmt_frame_handling", "1")
  271. dev[0].request("FETCH_ANQP")
  272. dev[0].request("STOP_FETCH_ANQP")
  273. hapd.set("ext_mgmt_frame_handling", "0")
  274. ev = dev[0].wait_event(["RX-ANQP", "GAS-QUERY-DONE"], timeout=10)
  275. if ev is None:
  276. raise Exception("GAS-QUERY-DONE timed out")
  277. if "RX-ANQP" in ev:
  278. raise Exception("Unexpected ANQP response received")
  279. def test_gas_anqp_get(dev, apdev):
  280. """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
  281. hapd = start_ap(apdev[0])
  282. bssid = apdev[0]['bssid']
  283. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  284. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
  285. raise Exception("ANQP_GET command failed")
  286. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  287. if ev is None:
  288. raise Exception("GAS query start timed out")
  289. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  290. if ev is None:
  291. raise Exception("GAS query timed out")
  292. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  293. if ev is None or "Venue Name" not in ev:
  294. raise Exception("Did not receive Venue Name")
  295. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  296. if ev is None or "Domain Name list" not in ev:
  297. raise Exception("Did not receive Domain Name list")
  298. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  299. if ev is None or "Operator Friendly Name" not in ev:
  300. raise Exception("Did not receive Operator Friendly Name")
  301. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  302. if ev is None or "WAN Metrics" not in ev:
  303. raise Exception("Did not receive WAN Metrics")
  304. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  305. if ev is None:
  306. raise Exception("ANQP-QUERY-DONE event not seen")
  307. if "result=SUCCESS" not in ev:
  308. raise Exception("Unexpected result: " + ev)
  309. if "OK" not in dev[0].request("HS20_ANQP_GET " + bssid + " 3,4"):
  310. raise Exception("ANQP_GET command failed")
  311. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  312. if ev is None or "Operator Friendly Name" not in ev:
  313. raise Exception("Did not receive Operator Friendly Name")
  314. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  315. if ev is None or "WAN Metrics" not in ev:
  316. raise Exception("Did not receive WAN Metrics")
  317. cmds = [ "",
  318. "foo",
  319. "00:11:22:33:44:55 258,hs20:-1",
  320. "00:11:22:33:44:55 258,hs20:0",
  321. "00:11:22:33:44:55 258,hs20:32",
  322. "00:11:22:33:44:55 hs20:-1",
  323. "00:11:22:33:44:55 hs20:0",
  324. "00:11:22:33:44:55 hs20:32",
  325. "00:11:22:33:44:55",
  326. "00:11:22:33:44:55 ",
  327. "00:11:22:33:44:55 0" ]
  328. for cmd in cmds:
  329. if "FAIL" not in dev[0].request("ANQP_GET " + cmd):
  330. raise Exception("Invalid ANQP_GET accepted")
  331. cmds = [ "",
  332. "foo",
  333. "00:11:22:33:44:55 -1",
  334. "00:11:22:33:44:55 0",
  335. "00:11:22:33:44:55 32",
  336. "00:11:22:33:44:55",
  337. "00:11:22:33:44:55 ",
  338. "00:11:22:33:44:55 0" ]
  339. for cmd in cmds:
  340. if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
  341. raise Exception("Invalid HS20_ANQP_GET accepted")
  342. def expect_gas_result(dev, result, status=None):
  343. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  344. if ev is None:
  345. raise Exception("GAS query timed out")
  346. if "result=" + result not in ev:
  347. raise Exception("Unexpected GAS query result")
  348. if status and "status_code=" + str(status) + ' ' not in ev:
  349. raise Exception("Unexpected GAS status code")
  350. def anqp_get(dev, bssid, id):
  351. if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
  352. raise Exception("ANQP_GET command failed")
  353. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  354. if ev is None:
  355. raise Exception("GAS query start timed out")
  356. def test_gas_timeout(dev, apdev):
  357. """GAS timeout"""
  358. hapd = start_ap(apdev[0])
  359. bssid = apdev[0]['bssid']
  360. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  361. hapd.set("ext_mgmt_frame_handling", "1")
  362. anqp_get(dev[0], bssid, 263)
  363. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  364. if ev is None:
  365. raise Exception("MGMT RX wait timed out")
  366. expect_gas_result(dev[0], "TIMEOUT")
  367. MGMT_SUBTYPE_ACTION = 13
  368. ACTION_CATEG_PUBLIC = 4
  369. GAS_INITIAL_REQUEST = 10
  370. GAS_INITIAL_RESPONSE = 11
  371. GAS_COMEBACK_REQUEST = 12
  372. GAS_COMEBACK_RESPONSE = 13
  373. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  374. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  375. def anqp_adv_proto():
  376. return struct.pack('BBBB', 108, 2, 127, 0)
  377. def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
  378. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  379. dialog_token, status_code, comeback_delay) + anqp_adv_proto()
  380. def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
  381. if more:
  382. id |= 0x80
  383. if bogus_adv_proto:
  384. adv = struct.pack('BBBB', 108, 2, 127, 1)
  385. else:
  386. adv = anqp_adv_proto()
  387. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  388. dialog_token, status_code, id, comeback_delay) + adv
  389. def gas_rx(hapd):
  390. count = 0
  391. while count < 30:
  392. count = count + 1
  393. query = hapd.mgmt_rx()
  394. if query is None:
  395. raise Exception("Action frame not received")
  396. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  397. continue
  398. payload = query['payload']
  399. if len(payload) < 2:
  400. continue
  401. (category, action) = struct.unpack('BB', payload[0:2])
  402. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  403. continue
  404. return query
  405. raise Exception("No Action frame received")
  406. def parse_gas(payload):
  407. pos = payload
  408. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  409. if category != ACTION_CATEG_PUBLIC:
  410. return None
  411. if action not in GAS_ACTIONS:
  412. return None
  413. gas = {}
  414. gas['action'] = action
  415. pos = pos[3:]
  416. if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
  417. return None
  418. gas['dialog_token'] = dialog_token
  419. if action == GAS_INITIAL_RESPONSE:
  420. if len(pos) < 4:
  421. return None
  422. (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
  423. gas['status_code'] = status_code
  424. gas['comeback_delay'] = comeback_delay
  425. if action == GAS_COMEBACK_RESPONSE:
  426. if len(pos) < 5:
  427. return None
  428. (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
  429. gas['status_code'] = status_code
  430. gas['frag'] = frag
  431. gas['comeback_delay'] = comeback_delay
  432. return gas
  433. def action_response(req):
  434. resp = {}
  435. resp['fc'] = req['fc']
  436. resp['da'] = req['sa']
  437. resp['sa'] = req['da']
  438. resp['bssid'] = req['bssid']
  439. return resp
  440. def send_gas_resp(hapd, resp):
  441. hapd.mgmt_tx(resp)
  442. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  443. if ev is None:
  444. raise Exception("Missing TX status for GAS response")
  445. if "ok=1" not in ev:
  446. raise Exception("GAS response not acknowledged")
  447. def test_gas_invalid_response_type(dev, apdev):
  448. """GAS invalid response type"""
  449. hapd = start_ap(apdev[0])
  450. bssid = apdev[0]['bssid']
  451. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  452. hapd.set("ext_mgmt_frame_handling", "1")
  453. anqp_get(dev[0], bssid, 263)
  454. query = gas_rx(hapd)
  455. gas = parse_gas(query['payload'])
  456. resp = action_response(query)
  457. # GAS Comeback Response instead of GAS Initial Response
  458. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  459. send_gas_resp(hapd, resp)
  460. # station drops the invalid frame, so this needs to result in GAS timeout
  461. expect_gas_result(dev[0], "TIMEOUT")
  462. def test_gas_failure_status_code(dev, apdev):
  463. """GAS failure status code"""
  464. hapd = start_ap(apdev[0])
  465. bssid = apdev[0]['bssid']
  466. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  467. hapd.set("ext_mgmt_frame_handling", "1")
  468. anqp_get(dev[0], bssid, 263)
  469. query = gas_rx(hapd)
  470. gas = parse_gas(query['payload'])
  471. resp = action_response(query)
  472. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  473. send_gas_resp(hapd, resp)
  474. expect_gas_result(dev[0], "FAILURE")
  475. def test_gas_malformed(dev, apdev):
  476. """GAS malformed response frames"""
  477. hapd = start_ap(apdev[0])
  478. bssid = apdev[0]['bssid']
  479. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  480. hapd.set("ext_mgmt_frame_handling", "1")
  481. anqp_get(dev[0], bssid, 263)
  482. query = gas_rx(hapd)
  483. gas = parse_gas(query['payload'])
  484. resp = action_response(query)
  485. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  486. GAS_COMEBACK_RESPONSE,
  487. gas['dialog_token'], 0)
  488. hapd.mgmt_tx(resp)
  489. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  490. GAS_COMEBACK_RESPONSE,
  491. gas['dialog_token'], 0, 0)
  492. hapd.mgmt_tx(resp)
  493. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  494. gas['dialog_token'], 0, 0)
  495. resp['payload'] = hdr + struct.pack('B', 108)
  496. hapd.mgmt_tx(resp)
  497. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  498. hapd.mgmt_tx(resp)
  499. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  500. hapd.mgmt_tx(resp)
  501. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  502. hapd.mgmt_tx(resp)
  503. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  504. hapd.mgmt_tx(resp)
  505. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  506. hapd.mgmt_tx(resp)
  507. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  508. hapd.mgmt_tx(resp)
  509. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  510. hapd.mgmt_tx(resp)
  511. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  512. hapd.mgmt_tx(resp)
  513. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  514. hapd.mgmt_tx(resp)
  515. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  516. hapd.mgmt_tx(resp)
  517. # Station drops invalid frames, but the last of the responses is valid from
  518. # GAS view point even though it has an extra octet in the end and the ANQP
  519. # part of the response is not valid. This is reported as successfully
  520. # completed GAS exchange.
  521. expect_gas_result(dev[0], "SUCCESS")
  522. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  523. if ev is None:
  524. raise Exception("ANQP-QUERY-DONE not reported")
  525. if "result=INVALID_FRAME" not in ev:
  526. raise Exception("Unexpected result: " + ev)
  527. def init_gas(hapd, bssid, dev):
  528. anqp_get(dev, bssid, 263)
  529. query = gas_rx(hapd)
  530. gas = parse_gas(query['payload'])
  531. dialog_token = gas['dialog_token']
  532. resp = action_response(query)
  533. resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
  534. send_gas_resp(hapd, resp)
  535. query = gas_rx(hapd)
  536. gas = parse_gas(query['payload'])
  537. if gas['action'] != GAS_COMEBACK_REQUEST:
  538. raise Exception("Unexpected request action")
  539. if gas['dialog_token'] != dialog_token:
  540. raise Exception("Unexpected dialog token change")
  541. return query, dialog_token
  542. def allow_gas_initial_req(hapd, dialog_token):
  543. msg = hapd.mgmt_rx(timeout=1)
  544. if msg is not None:
  545. gas = parse_gas(msg['payload'])
  546. if gas['action'] != GAS_INITIAL_REQUEST or dialog_token == gas['dialog_token']:
  547. raise Exception("Unexpected management frame")
  548. def test_gas_malformed_comeback_resp(dev, apdev):
  549. """GAS malformed comeback response frames"""
  550. hapd = start_ap(apdev[0])
  551. bssid = apdev[0]['bssid']
  552. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  553. hapd.set("ext_mgmt_frame_handling", "1")
  554. logger.debug("Non-zero status code in comeback response")
  555. query, dialog_token = init_gas(hapd, bssid, dev[0])
  556. resp = action_response(query)
  557. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
  558. send_gas_resp(hapd, resp)
  559. expect_gas_result(dev[0], "FAILURE", status=2)
  560. logger.debug("Different advertisement protocol in comeback response")
  561. query, dialog_token = init_gas(hapd, bssid, dev[0])
  562. resp = action_response(query)
  563. resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
  564. send_gas_resp(hapd, resp)
  565. expect_gas_result(dev[0], "PEER_ERROR")
  566. logger.debug("Non-zero frag id and comeback delay in comeback response")
  567. query, dialog_token = init_gas(hapd, bssid, dev[0])
  568. resp = action_response(query)
  569. resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
  570. send_gas_resp(hapd, resp)
  571. expect_gas_result(dev[0], "PEER_ERROR")
  572. logger.debug("Unexpected frag id in comeback response")
  573. query, dialog_token = init_gas(hapd, bssid, dev[0])
  574. resp = action_response(query)
  575. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  576. send_gas_resp(hapd, resp)
  577. expect_gas_result(dev[0], "PEER_ERROR")
  578. logger.debug("Empty fragment and replay in comeback response")
  579. query, dialog_token = init_gas(hapd, bssid, dev[0])
  580. resp = action_response(query)
  581. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  582. send_gas_resp(hapd, resp)
  583. query = gas_rx(hapd)
  584. gas = parse_gas(query['payload'])
  585. if gas['action'] != GAS_COMEBACK_REQUEST:
  586. raise Exception("Unexpected request action")
  587. if gas['dialog_token'] != dialog_token:
  588. raise Exception("Unexpected dialog token change")
  589. resp = action_response(query)
  590. resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
  591. send_gas_resp(hapd, resp)
  592. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  593. send_gas_resp(hapd, resp)
  594. expect_gas_result(dev[0], "SUCCESS")
  595. logger.debug("Unexpected initial response when waiting for comeback response")
  596. query, dialog_token = init_gas(hapd, bssid, dev[0])
  597. resp = action_response(query)
  598. resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
  599. send_gas_resp(hapd, resp)
  600. allow_gas_initial_req(hapd, dialog_token)
  601. expect_gas_result(dev[0], "TIMEOUT")
  602. logger.debug("Too short comeback response")
  603. query, dialog_token = init_gas(hapd, bssid, dev[0])
  604. resp = action_response(query)
  605. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  606. GAS_COMEBACK_RESPONSE, dialog_token, 0)
  607. send_gas_resp(hapd, resp)
  608. allow_gas_initial_req(hapd, dialog_token)
  609. expect_gas_result(dev[0], "TIMEOUT")
  610. logger.debug("Too short comeback response(2)")
  611. query, dialog_token = init_gas(hapd, bssid, dev[0])
  612. resp = action_response(query)
  613. resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
  614. GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
  615. 0)
  616. send_gas_resp(hapd, resp)
  617. allow_gas_initial_req(hapd, dialog_token)
  618. expect_gas_result(dev[0], "TIMEOUT")
  619. logger.debug("Maximum comeback response fragment claiming more fragments")
  620. query, dialog_token = init_gas(hapd, bssid, dev[0])
  621. resp = action_response(query)
  622. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  623. send_gas_resp(hapd, resp)
  624. for i in range(1, 129):
  625. query = gas_rx(hapd)
  626. gas = parse_gas(query['payload'])
  627. if gas['action'] != GAS_COMEBACK_REQUEST:
  628. raise Exception("Unexpected request action")
  629. if gas['dialog_token'] != dialog_token:
  630. raise Exception("Unexpected dialog token change")
  631. resp = action_response(query)
  632. resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
  633. send_gas_resp(hapd, resp)
  634. expect_gas_result(dev[0], "PEER_ERROR")
  635. def test_gas_comeback_resp_additional_delay(dev, apdev):
  636. """GAS comeback response requesting additional delay"""
  637. hapd = start_ap(apdev[0])
  638. bssid = apdev[0]['bssid']
  639. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  640. hapd.set("ext_mgmt_frame_handling", "1")
  641. query, dialog_token = init_gas(hapd, bssid, dev[0])
  642. for i in range(0, 2):
  643. resp = action_response(query)
  644. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
  645. send_gas_resp(hapd, resp)
  646. query = gas_rx(hapd)
  647. gas = parse_gas(query['payload'])
  648. if gas['action'] != GAS_COMEBACK_REQUEST:
  649. raise Exception("Unexpected request action")
  650. if gas['dialog_token'] != dialog_token:
  651. raise Exception("Unexpected dialog token change")
  652. resp = action_response(query)
  653. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
  654. send_gas_resp(hapd, resp)
  655. expect_gas_result(dev[0], "SUCCESS")
  656. def test_gas_unknown_adv_proto(dev, apdev):
  657. """Unknown advertisement protocol id"""
  658. bssid = apdev[0]['bssid']
  659. params = hs20_ap_params()
  660. params['hessid'] = bssid
  661. hostapd.add_ap(apdev[0], params)
  662. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  663. req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
  664. if "FAIL" in req:
  665. raise Exception("GAS query request rejected")
  666. expect_gas_result(dev[0], "FAILURE", "59")
  667. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  668. if ev is None:
  669. raise Exception("GAS query timed out")
  670. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  671. res = re.split(exp, ev)
  672. if len(res) < 6:
  673. raise Exception("Could not parse GAS-RESPONSE-INFO")
  674. if res[2] != bssid:
  675. raise Exception("Unexpected BSSID in response")
  676. status = res[4]
  677. if status != "59":
  678. raise Exception("Unexpected GAS-RESPONSE-INFO status")
  679. def test_gas_request_oom(dev, apdev):
  680. """GAS_REQUEST OOM"""
  681. bssid = apdev[0]['bssid']
  682. params = hs20_ap_params()
  683. params['hessid'] = bssid
  684. hostapd.add_ap(apdev[0], params)
  685. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  686. with alloc_fail(dev[0], 1, "gas_build_req;gas_send_request"):
  687. if "FAIL" not in dev[0].request("GAS_REQUEST " + bssid + " 42"):
  688. raise Exception("GAS query request rejected")
  689. with alloc_fail(dev[0], 1, "gas_query_req;gas_send_request"):
  690. if "FAIL" not in dev[0].request("GAS_REQUEST " + bssid + " 42"):
  691. raise Exception("GAS query request rejected")
  692. with alloc_fail(dev[0], 1, "wpabuf_dup;gas_resp_cb"):
  693. if "OK" not in dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101"):
  694. raise Exception("GAS query request rejected")
  695. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  696. if ev is None:
  697. raise Exception("No GAS response")
  698. if "status_code=0" not in ev:
  699. raise Exception("GAS response indicated a failure")
  700. def test_gas_max_pending(dev, apdev):
  701. """GAS and maximum pending query limit"""
  702. hapd = start_ap(apdev[0])
  703. hapd.set("gas_frag_limit", "50")
  704. bssid = apdev[0]['bssid']
  705. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  706. wpas.interface_add("wlan5")
  707. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  708. raise Exception("Failed to set listen channel")
  709. if "OK" not in wpas.p2p_listen():
  710. raise Exception("Failed to start listen state")
  711. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  712. raise Exception("Failed to enable external management frame handling")
  713. anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
  714. gas = struct.pack('<H', len(anqp_query)) + anqp_query
  715. for dialog_token in range(1, 10):
  716. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
  717. dialog_token) + anqp_adv_proto() + gas
  718. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  719. if "OK" not in wpas.request(req):
  720. raise Exception("Could not send management frame")
  721. resp = wpas.mgmt_rx()
  722. if resp is None:
  723. raise Exception("MGMT-RX timeout")
  724. if 'payload' not in resp:
  725. raise Exception("Missing payload")
  726. gresp = parse_gas(resp['payload'])
  727. if gresp['dialog_token'] != dialog_token:
  728. raise Exception("Dialog token mismatch")
  729. status_code = gresp['status_code']
  730. if dialog_token < 9 and status_code != 0:
  731. raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
  732. if dialog_token > 8 and status_code == 0:
  733. raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
  734. def test_gas_no_pending(dev, apdev):
  735. """GAS and no pending query for comeback request"""
  736. hapd = start_ap(apdev[0])
  737. bssid = apdev[0]['bssid']
  738. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  739. wpas.interface_add("wlan5")
  740. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  741. raise Exception("Failed to set listen channel")
  742. if "OK" not in wpas.p2p_listen():
  743. raise Exception("Failed to start listen state")
  744. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  745. raise Exception("Failed to enable external management frame handling")
  746. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
  747. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  748. if "OK" not in wpas.request(req):
  749. raise Exception("Could not send management frame")
  750. resp = wpas.mgmt_rx()
  751. if resp is None:
  752. raise Exception("MGMT-RX timeout")
  753. if 'payload' not in resp:
  754. raise Exception("Missing payload")
  755. gresp = parse_gas(resp['payload'])
  756. status_code = gresp['status_code']
  757. if status_code != 60:
  758. raise Exception("Unexpected status code {} (expected 60)".format(status_code))
  759. def test_gas_delete_at_deinit(dev, apdev):
  760. """GAS query deleted at deinit"""
  761. hapd = start_ap(apdev[0])
  762. hapd.set("gas_comeback_delay", "1000")
  763. bssid = apdev[0]['bssid']
  764. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  765. wpas.interface_add("wlan5")
  766. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  767. wpas.request("ANQP_GET " + bssid + " 258")
  768. wpas.global_request("INTERFACE_REMOVE " + wpas.ifname)
  769. ev = wpas.wait_event(["GAS-QUERY-DONE"], timeout=2)
  770. del wpas
  771. if ev is None:
  772. raise Exception("GAS-QUERY-DONE not seen")
  773. if "result=DELETED_AT_DEINIT" not in ev:
  774. raise Exception("Unexpected result code: " + ev)
  775. def test_gas_missing_payload(dev, apdev):
  776. """No action code in the query frame"""
  777. bssid = apdev[0]['bssid']
  778. params = hs20_ap_params()
  779. params['hessid'] = bssid
  780. hostapd.add_ap(apdev[0], params)
  781. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  782. cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
  783. if "FAIL" in dev[0].request(cmd):
  784. raise Exception("Could not send test Action frame")
  785. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  786. if ev is None:
  787. raise Exception("Timeout on MGMT-TX-STATUS")
  788. if "result=SUCCESS" not in ev:
  789. raise Exception("AP did not ack Action frame")
  790. cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
  791. if "FAIL" in dev[0].request(cmd):
  792. raise Exception("Could not send test Action frame")
  793. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  794. if ev is None:
  795. raise Exception("Timeout on MGMT-TX-STATUS")
  796. if "result=SUCCESS" not in ev:
  797. raise Exception("AP did not ack Action frame")
  798. def test_gas_query_deinit(dev, apdev):
  799. """Pending GAS/ANQP query during deinit"""
  800. hapd = start_ap(apdev[0])
  801. bssid = apdev[0]['bssid']
  802. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  803. wpas.interface_add("wlan5")
  804. wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
  805. id = wpas.request("RADIO_WORK add block-work")
  806. if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
  807. raise Exception("ANQP_GET command failed")
  808. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  809. if ev is None:
  810. raise Exception("Timeout while waiting radio work to start")
  811. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  812. if ev is None:
  813. raise Exception("Timeout while waiting radio work to start (2)")
  814. # Remove the interface while the gas-query radio work is still pending and
  815. # GAS query has not yet been started.
  816. wpas.interface_remove("wlan5")
  817. @remote_compatible
  818. def test_gas_anqp_oom_wpas(dev, apdev):
  819. """GAS/ANQP query and OOM in wpa_supplicant"""
  820. hapd = start_ap(apdev[0])
  821. bssid = apdev[0]['bssid']
  822. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  823. with alloc_fail(dev[0], 1, "wpa_bss_anqp_alloc"):
  824. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  825. raise Exception("ANQP_GET command failed")
  826. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  827. if ev is None:
  828. raise Exception("ANQP query did not complete")
  829. with alloc_fail(dev[0], 1, "gas_build_req"):
  830. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  831. raise Exception("Unexpected ANQP_GET command success (OOM)")
  832. def test_gas_anqp_oom_hapd(dev, apdev):
  833. """GAS/ANQP query and OOM in hostapd"""
  834. hapd = start_ap(apdev[0])
  835. bssid = apdev[0]['bssid']
  836. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  837. with alloc_fail(hapd, 1, "gas_build_resp"):
  838. # This query will time out due to the AP not sending a response (OOM).
  839. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  840. raise Exception("ANQP_GET command failed")
  841. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  842. if ev is None:
  843. raise Exception("GAS query start timed out")
  844. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  845. if ev is None:
  846. raise Exception("GAS query timed out")
  847. if "result=TIMEOUT" not in ev:
  848. raise Exception("Unexpected result: " + ev)
  849. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  850. if ev is None:
  851. raise Exception("ANQP-QUERY-DONE event not seen")
  852. if "result=FAILURE" not in ev:
  853. raise Exception("Unexpected result: " + ev)
  854. with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
  855. hapd.set("gas_frag_limit", "50")
  856. # The first attempt of this query will time out due to the AP not
  857. # sending a response (OOM), but the retry succeeds.
  858. dev[0].request("FETCH_ANQP")
  859. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  860. if ev is None:
  861. raise Exception("GAS query start timed out")
  862. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  863. if ev is None:
  864. raise Exception("GAS query timed out")
  865. if "result=SUCCESS" not in ev:
  866. raise Exception("Unexpected result: " + ev)
  867. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  868. if ev is None:
  869. raise Exception("ANQP-QUERY-DONE event not seen")
  870. if "result=SUCCESS" not in ev:
  871. raise Exception("Unexpected result: " + ev)
  872. def test_gas_anqp_extra_elements(dev, apdev):
  873. """GAS/ANQP and extra ANQP elements"""
  874. geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100"
  875. civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5"
  876. held_uri = "https://held.example.com/location"
  877. held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri
  878. supl_fqdn = "supl.example.com"
  879. supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn
  880. public_id = binascii.hexlify(held + supl)
  881. params = { "ssid": "gas/anqp",
  882. "interworking": "1",
  883. "anqp_elem": [ "265:" + geo_loc,
  884. "266:" + civic_loc,
  885. "262:1122334455",
  886. "267:" + public_id,
  887. "275:01020304",
  888. "60000:01",
  889. "299:0102" ] }
  890. hapd = hostapd.add_ap(apdev[0], params)
  891. bssid = apdev[0]['bssid']
  892. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  893. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 265,266"):
  894. raise Exception("ANQP_GET command failed")
  895. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  896. if ev is None:
  897. raise Exception("GAS query timed out")
  898. bss = dev[0].get_bss(bssid)
  899. if 'anqp[265]' not in bss:
  900. raise Exception("AP Geospatial Location ANQP-element not seen")
  901. if bss['anqp[265]'] != geo_loc:
  902. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  903. if 'anqp[266]' not in bss:
  904. raise Exception("AP Civic Location ANQP-element not seen")
  905. if bss['anqp[266]'] != civic_loc:
  906. raise Exception("Unexpected AP Civic Location ANQP-element value: " + bss['anqp[266]'])
  907. dev[1].scan_for_bss(bssid, freq="2412", force_scan=True)
  908. 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"):
  909. raise Exception("ANQP_GET command failed")
  910. ev = dev[1].wait_event(["GAS-QUERY-DONE"], timeout=10)
  911. if ev is None:
  912. raise Exception("GAS query timed out")
  913. bss = dev[1].get_bss(bssid)
  914. if 'anqp[265]' not in bss:
  915. raise Exception("AP Geospatial Location ANQP-element not seen")
  916. if bss['anqp[265]'] != geo_loc:
  917. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  918. if 'anqp[266]' in bss:
  919. raise Exception("AP Civic Location ANQP-element unexpectedly seen")
  920. if 'anqp[267]' not in bss:
  921. raise Exception("AP Location Public Identifier ANQP-element not seen")
  922. if bss['anqp[267]'] != public_id:
  923. raise Exception("Unexpected AP Location Public Identifier ANQP-element value: " + bss['anqp[267]'])
  924. if 'anqp[275]' not in bss:
  925. raise Exception("ANQP-element Info ID 275 not seen")
  926. if bss['anqp[275]'] != "01020304":
  927. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  928. if 'anqp[299]' not in bss:
  929. raise Exception("ANQP-element Info ID 299 not seen")
  930. if bss['anqp[299]'] != "0102":
  931. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  932. if 'anqp_ip_addr_type_availability' not in bss:
  933. raise Exception("ANQP-element Info ID 292 not seen")
  934. if bss['anqp_ip_addr_type_availability'] != "1122334455":
  935. raise Exception("Unexpected AP ANQP-element Info ID 262 value: " + bss['anqp_ip_addr_type_availability'])
  936. def test_gas_anqp_address3_not_assoc(dev, apdev, params):
  937. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when not associated"""
  938. try:
  939. _test_gas_anqp_address3_not_assoc(dev, apdev, params)
  940. finally:
  941. dev[0].request("SET gas_address3 0")
  942. def _test_gas_anqp_address3_not_assoc(dev, apdev, params):
  943. hapd = start_ap(apdev[0])
  944. bssid = apdev[0]['bssid']
  945. if "OK" not in dev[0].request("SET gas_address3 1"):
  946. raise Exception("Failed to set gas_address3")
  947. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  948. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  949. raise Exception("ANQP_GET command failed")
  950. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  951. if ev is None:
  952. raise Exception("GAS query start timed out")
  953. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  954. if ev is None:
  955. raise Exception("GAS query timed out")
  956. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  957. if ev is None or "Venue Name" not in ev:
  958. raise Exception("Did not receive Venue Name")
  959. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  960. if ev is None:
  961. raise Exception("ANQP-QUERY-DONE event not seen")
  962. if "result=SUCCESS" not in ev:
  963. raise Exception("Unexpected result: " + ev)
  964. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  965. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  966. display=["wlan.bssid"])
  967. res = out.splitlines()
  968. if len(res) != 2:
  969. raise Exception("Unexpected number of GAS frames")
  970. if res[0] != 'ff:ff:ff:ff:ff:ff':
  971. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  972. if res[1] != 'ff:ff:ff:ff:ff:ff':
  973. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  974. def test_gas_anqp_address3_assoc(dev, apdev, params):
  975. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when associated"""
  976. try:
  977. _test_gas_anqp_address3_assoc(dev, apdev, params)
  978. finally:
  979. dev[0].request("SET gas_address3 0")
  980. def _test_gas_anqp_address3_assoc(dev, apdev, params):
  981. hapd = start_ap(apdev[0])
  982. bssid = apdev[0]['bssid']
  983. if "OK" not in dev[0].request("SET gas_address3 1"):
  984. raise Exception("Failed to set gas_address3")
  985. dev[0].scan_for_bss(bssid, freq="2412")
  986. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  987. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  988. password="password", phase2="auth=MSCHAPV2",
  989. ca_cert="auth_serv/ca.pem", scan_freq="2412")
  990. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  991. raise Exception("ANQP_GET command failed")
  992. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  993. if ev is None:
  994. raise Exception("GAS query start timed out")
  995. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  996. if ev is None:
  997. raise Exception("GAS query timed out")
  998. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  999. if ev is None or "Venue Name" not in ev:
  1000. raise Exception("Did not receive Venue Name")
  1001. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1002. if ev is None:
  1003. raise Exception("ANQP-QUERY-DONE event not seen")
  1004. if "result=SUCCESS" not in ev:
  1005. raise Exception("Unexpected result: " + ev)
  1006. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1007. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1008. display=["wlan.bssid"])
  1009. res = out.splitlines()
  1010. if len(res) != 2:
  1011. raise Exception("Unexpected number of GAS frames")
  1012. if res[0] != bssid:
  1013. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1014. if res[1] != bssid:
  1015. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1016. def test_gas_anqp_address3_ap_forced(dev, apdev, params):
  1017. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value on AP"""
  1018. hapd = start_ap(apdev[0])
  1019. bssid = apdev[0]['bssid']
  1020. hapd.set("gas_address3", "1")
  1021. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1022. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1023. raise Exception("ANQP_GET command failed")
  1024. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1025. if ev is None:
  1026. raise Exception("GAS query start timed out")
  1027. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1028. if ev is None:
  1029. raise Exception("GAS query timed out")
  1030. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1031. if ev is None or "Venue Name" not in ev:
  1032. raise Exception("Did not receive Venue Name")
  1033. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1034. if ev is None:
  1035. raise Exception("ANQP-QUERY-DONE event not seen")
  1036. if "result=SUCCESS" not in ev:
  1037. raise Exception("Unexpected result: " + ev)
  1038. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1039. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1040. display=["wlan.bssid"])
  1041. res = out.splitlines()
  1042. if len(res) != 2:
  1043. raise Exception("Unexpected number of GAS frames")
  1044. if res[0] != bssid:
  1045. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1046. if res[1] != 'ff:ff:ff:ff:ff:ff':
  1047. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1048. def test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1049. """GAS/ANQP query using IEEE 802.11 non-compliant Address 3 (AP)"""
  1050. try:
  1051. _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params)
  1052. finally:
  1053. dev[0].request("SET gas_address3 0")
  1054. def _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1055. hapd = start_ap(apdev[0])
  1056. bssid = apdev[0]['bssid']
  1057. hapd.set("gas_address3", "2")
  1058. if "OK" not in dev[0].request("SET gas_address3 1"):
  1059. raise Exception("Failed to set gas_address3")
  1060. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1061. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1062. raise Exception("ANQP_GET command failed")
  1063. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1064. if ev is None:
  1065. raise Exception("GAS query start timed out")
  1066. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1067. if ev is None:
  1068. raise Exception("GAS query timed out")
  1069. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1070. if ev is None or "Venue Name" not in ev:
  1071. raise Exception("Did not receive Venue Name")
  1072. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1073. if ev is None:
  1074. raise Exception("ANQP-QUERY-DONE event not seen")
  1075. if "result=SUCCESS" not in ev:
  1076. raise Exception("Unexpected result: " + ev)
  1077. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1078. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1079. display=["wlan.bssid"])
  1080. res = out.splitlines()
  1081. if len(res) != 2:
  1082. raise Exception("Unexpected number of GAS frames")
  1083. if res[0] != 'ff:ff:ff:ff:ff:ff':
  1084. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1085. if res[1] != bssid:
  1086. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1087. def test_gas_prot_vs_not_prot(dev, apdev, params):
  1088. """GAS/ANQP query protected vs. not protected"""
  1089. hapd = start_ap(apdev[0])
  1090. bssid = apdev[0]['bssid']
  1091. dev[0].scan_for_bss(bssid, freq="2412")
  1092. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  1093. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  1094. password="password", phase2="auth=MSCHAPV2",
  1095. ca_cert="auth_serv/ca.pem", scan_freq="2412",
  1096. ieee80211w="2")
  1097. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1098. raise Exception("ANQP_GET command failed")
  1099. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1100. if ev is None:
  1101. raise Exception("No GAS-QUERY-DONE event")
  1102. if "result=SUCCESS" not in ev:
  1103. raise Exception("Unexpected GAS result: " + ev)
  1104. # GAS: Drop unexpected unprotected GAS frame when PMF is enabled
  1105. dev[0].request("SET ext_mgmt_frame_handling 1")
  1106. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1107. dev[0].request("SET ext_mgmt_frame_handling 0")
  1108. if "OK" not in res:
  1109. raise Exception("MGMT_RX_PROCESS failed")
  1110. dev[0].request("DISCONNECT")
  1111. dev[0].wait_disconnected()
  1112. # GAS: No pending query found for 02:00:00:00:03:00 dialog token 0
  1113. dev[0].request("SET ext_mgmt_frame_handling 1")
  1114. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1115. dev[0].request("SET ext_mgmt_frame_handling 0")
  1116. if "OK" not in res:
  1117. raise Exception("MGMT_RX_PROCESS failed")
  1118. # GAS: Drop unexpected protected GAS frame when PMF is disabled
  1119. dev[0].request("SET ext_mgmt_frame_handling 1")
  1120. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000090b00000005006c027f000000")
  1121. dev[0].request("SET ext_mgmt_frame_handling 0")
  1122. if "OK" not in res:
  1123. raise Exception("MGMT_RX_PROCESS failed")
  1124. def test_gas_failures(dev, apdev):
  1125. """GAS failure cases"""
  1126. hapd = start_ap(apdev[0])
  1127. hapd.set("gas_comeback_delay", "5")
  1128. bssid = apdev[0]['bssid']
  1129. hapd2 = start_ap(apdev[1])
  1130. bssid2 = apdev[1]['bssid']
  1131. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1132. dev[0].scan_for_bss(bssid2, freq="2412")
  1133. tests = [ (bssid, "gas_build_req;gas_query_tx_comeback_req"),
  1134. (bssid, "gas_query_tx;gas_query_tx_comeback_req"),
  1135. (bssid, "gas_query_append;gas_query_rx_comeback"),
  1136. (bssid2, "gas_query_append;gas_query_rx_initial"),
  1137. (bssid2, "wpabuf_alloc_copy;gas_query_rx_initial"),
  1138. (bssid, "gas_query_tx;gas_query_tx_initial_req") ]
  1139. for addr,func in tests:
  1140. with alloc_fail(dev[0], 1, func):
  1141. dev[0].request("ANQP_GET " + addr + " 258")
  1142. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1143. if ev is None:
  1144. raise Exception("No GAS-QUERY-DONE seen")
  1145. if "result=INTERNAL_ERROR" not in ev:
  1146. raise Exception("Unexpected result code: " + ev)
  1147. dev[0].dump_monitor()
  1148. tests = [ "=gas_query_req", "radio_add_work;gas_query_req" ]
  1149. for func in tests:
  1150. with alloc_fail(dev[0], 1, func):
  1151. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1152. raise Exception("ANQP_GET succeeded unexpectedly during OOM")
  1153. dev[0].dump_monitor()
  1154. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  1155. wpas.interface_add("wlan5")
  1156. wpas.scan_for_bss(bssid2, freq="2412")
  1157. wpas.request("SET preassoc_mac_addr 1111")
  1158. wpas.request("ANQP_GET " + bssid2 + " 258")
  1159. ev = wpas.wait_event(["Failed to assign random MAC address for GAS"],
  1160. timeout=5)
  1161. wpas.request("SET preassoc_mac_addr 0")
  1162. if ev is None:
  1163. raise Exception("No random MAC address error seen")