test_gas.py 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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. run_gas_comeback_delay(dev, apdev, 500)
  256. def test_gas_comeback_delay_long(dev, apdev):
  257. """GAS long comeback delay"""
  258. run_gas_comeback_delay(dev, apdev, 2500)
  259. def run_gas_comeback_delay(dev, apdev, delay):
  260. hapd = start_ap(apdev[0])
  261. hapd.set("gas_comeback_delay", str(delay))
  262. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  263. dev[0].request("FETCH_ANQP")
  264. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  265. raise Exception("SCAN accepted during FETCH_ANQP")
  266. for i in range(0, 6):
  267. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  268. if ev is None:
  269. raise Exception("Operation timed out")
  270. @remote_compatible
  271. def test_gas_stop_fetch_anqp(dev, apdev):
  272. """Stop FETCH_ANQP operation"""
  273. hapd = start_ap(apdev[0])
  274. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  275. hapd.set("ext_mgmt_frame_handling", "1")
  276. dev[0].request("FETCH_ANQP")
  277. dev[0].request("STOP_FETCH_ANQP")
  278. hapd.set("ext_mgmt_frame_handling", "0")
  279. ev = dev[0].wait_event(["RX-ANQP", "GAS-QUERY-DONE"], timeout=10)
  280. if ev is None:
  281. raise Exception("GAS-QUERY-DONE timed out")
  282. if "RX-ANQP" in ev:
  283. raise Exception("Unexpected ANQP response received")
  284. def test_gas_anqp_get(dev, apdev):
  285. """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
  286. hapd = start_ap(apdev[0])
  287. bssid = apdev[0]['bssid']
  288. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  289. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
  290. raise Exception("ANQP_GET command failed")
  291. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  292. if ev is None:
  293. raise Exception("GAS query start timed out")
  294. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  295. if ev is None:
  296. raise Exception("GAS query timed out")
  297. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  298. if ev is None or "Venue Name" not in ev:
  299. raise Exception("Did not receive Venue Name")
  300. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  301. if ev is None or "Domain Name list" not in ev:
  302. raise Exception("Did not receive Domain Name list")
  303. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  304. if ev is None or "Operator Friendly Name" not in ev:
  305. raise Exception("Did not receive Operator Friendly Name")
  306. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  307. if ev is None or "WAN Metrics" not in ev:
  308. raise Exception("Did not receive WAN Metrics")
  309. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  310. if ev is None:
  311. raise Exception("ANQP-QUERY-DONE event not seen")
  312. if "result=SUCCESS" not in ev:
  313. raise Exception("Unexpected result: " + ev)
  314. if "OK" not in dev[0].request("HS20_ANQP_GET " + bssid + " 3,4"):
  315. raise Exception("ANQP_GET command failed")
  316. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  317. if ev is None or "Operator Friendly Name" not in ev:
  318. raise Exception("Did not receive Operator Friendly Name")
  319. ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
  320. if ev is None or "WAN Metrics" not in ev:
  321. raise Exception("Did not receive WAN Metrics")
  322. logger.info("Attempt an MBO request with an AP that does not support MBO")
  323. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 272,mbo:1"):
  324. raise Exception("ANQP_GET command failed (2)")
  325. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  326. if ev is None:
  327. raise Exception("GAS query start timed out (2)")
  328. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  329. if ev is None:
  330. raise Exception("GAS query timed out (2)")
  331. cmds = [ "",
  332. "foo",
  333. "00:11:22:33:44:55 258,hs20:-1",
  334. "00:11:22:33:44:55 258,hs20:0",
  335. "00:11:22:33:44:55 258,hs20:32",
  336. "00:11:22:33:44:55 hs20:-1",
  337. "00:11:22:33:44:55 hs20:0",
  338. "00:11:22:33:44:55 hs20:32",
  339. "00:11:22:33:44:55 mbo:-1",
  340. "00:11:22:33:44:55 mbo:0",
  341. "00:11:22:33:44:55 mbo:999",
  342. "00:11:22:33:44:55",
  343. "00:11:22:33:44:55 ",
  344. "00:11:22:33:44:55 0",
  345. "00:11:22:33:44:55 1" ]
  346. for cmd in cmds:
  347. if "FAIL" not in dev[0].request("ANQP_GET " + cmd):
  348. raise Exception("Invalid ANQP_GET accepted")
  349. cmds = [ "",
  350. "foo",
  351. "00:11:22:33:44:55 -1",
  352. "00:11:22:33:44:55 0",
  353. "00:11:22:33:44:55 32",
  354. "00:11:22:33:44:55",
  355. "00:11:22:33:44:55 ",
  356. "00:11:22:33:44:55 0",
  357. "00:11:22:33:44:55 1" ]
  358. for cmd in cmds:
  359. if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
  360. raise Exception("Invalid HS20_ANQP_GET accepted")
  361. def test_gas_anqp_get_oom(dev, apdev):
  362. """GAS/ANQP query OOM"""
  363. hapd = start_ap(apdev[0])
  364. bssid = apdev[0]['bssid']
  365. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  366. with alloc_fail(dev[0], 1, "wpabuf_alloc;anqp_send_req"):
  367. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
  368. raise Exception("ANQP_GET command accepted during OOM")
  369. with alloc_fail(dev[0], 1, "hs20_build_anqp_req;hs20_anqp_send_req"):
  370. if "FAIL" not in dev[0].request("HS20_ANQP_GET " + bssid + " 1"):
  371. raise Exception("HS20_ANQP_GET command accepted during OOM")
  372. with alloc_fail(dev[0], 1, "gas_query_req;hs20_anqp_send_req"):
  373. if "FAIL" not in dev[0].request("HS20_ANQP_GET " + bssid + " 1"):
  374. raise Exception("HS20_ANQP_GET command accepted during OOM")
  375. with alloc_fail(dev[0], 1, "=hs20_anqp_send_req"):
  376. if "FAIL" not in dev[0].request("REQ_HS20_ICON " + bssid + " w1fi_logo"):
  377. raise Exception("REQ_HS20_ICON command accepted during OOM")
  378. with alloc_fail(dev[0], 2, "=hs20_anqp_send_req"):
  379. if "FAIL" not in dev[0].request("REQ_HS20_ICON " + bssid + " w1fi_logo"):
  380. raise Exception("REQ_HS20_ICON command accepted during OOM")
  381. def test_gas_anqp_icon_binary_proto(dev, apdev):
  382. """GAS/ANQP and icon binary protocol testing"""
  383. hapd = start_ap(apdev[0])
  384. bssid = apdev[0]['bssid']
  385. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  386. hapd.set("ext_mgmt_frame_handling", "1")
  387. tests = [ '010000', '01000000', '00000000', '00030000', '00020000',
  388. '00000100', '0001ff0100ee', '0001ff0200ee' ]
  389. for test in tests:
  390. dev[0].request("HS20_ICON_REQUEST " + bssid + " w1fi_logo")
  391. query = gas_rx(hapd)
  392. gas = parse_gas(query['payload'])
  393. resp = action_response(query)
  394. data = binascii.unhexlify(test)
  395. data = binascii.unhexlify('506f9a110b00') + data
  396. data = struct.pack('<HHH', len(data) + 4, 0xdddd, len(data)) + data
  397. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + data
  398. send_gas_resp(hapd, resp)
  399. expect_gas_result(dev[0], "SUCCESS")
  400. def test_gas_anqp_hs20_proto(dev, apdev):
  401. """GAS/ANQP and Hotspot 2.0 element protocol testing"""
  402. hapd = start_ap(apdev[0])
  403. bssid = apdev[0]['bssid']
  404. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  405. hapd.set("ext_mgmt_frame_handling", "1")
  406. tests = [ '00', '0100', '0201', '0300', '0400', '0500', '0600', '0700',
  407. '0800', '0900', '0a00', '0b0000000000' ]
  408. for test in tests:
  409. dev[0].request("HS20_ANQP_GET " + bssid + " 3,4")
  410. query = gas_rx(hapd)
  411. gas = parse_gas(query['payload'])
  412. resp = action_response(query)
  413. data = binascii.unhexlify(test)
  414. data = binascii.unhexlify('506f9a11') + data
  415. data = struct.pack('<HHH', len(data) + 4, 0xdddd, len(data)) + data
  416. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + data
  417. send_gas_resp(hapd, resp)
  418. expect_gas_result(dev[0], "SUCCESS")
  419. def expect_gas_result(dev, result, status=None):
  420. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  421. if ev is None:
  422. raise Exception("GAS query timed out")
  423. if "result=" + result not in ev:
  424. raise Exception("Unexpected GAS query result")
  425. if status and "status_code=" + str(status) + ' ' not in ev:
  426. raise Exception("Unexpected GAS status code")
  427. def anqp_get(dev, bssid, id):
  428. if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
  429. raise Exception("ANQP_GET command failed")
  430. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  431. if ev is None:
  432. raise Exception("GAS query start timed out")
  433. def test_gas_timeout(dev, apdev):
  434. """GAS timeout"""
  435. hapd = start_ap(apdev[0])
  436. bssid = apdev[0]['bssid']
  437. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  438. hapd.set("ext_mgmt_frame_handling", "1")
  439. anqp_get(dev[0], bssid, 263)
  440. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  441. if ev is None:
  442. raise Exception("MGMT RX wait timed out")
  443. expect_gas_result(dev[0], "TIMEOUT")
  444. MGMT_SUBTYPE_ACTION = 13
  445. ACTION_CATEG_PUBLIC = 4
  446. GAS_INITIAL_REQUEST = 10
  447. GAS_INITIAL_RESPONSE = 11
  448. GAS_COMEBACK_REQUEST = 12
  449. GAS_COMEBACK_RESPONSE = 13
  450. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  451. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  452. def anqp_adv_proto():
  453. return struct.pack('BBBB', 108, 2, 127, 0)
  454. def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
  455. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  456. dialog_token, status_code, comeback_delay) + anqp_adv_proto()
  457. def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
  458. if more:
  459. id |= 0x80
  460. if bogus_adv_proto:
  461. adv = struct.pack('BBBB', 108, 2, 127, 1)
  462. else:
  463. adv = anqp_adv_proto()
  464. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  465. dialog_token, status_code, id, comeback_delay) + adv
  466. def gas_rx(hapd):
  467. count = 0
  468. while count < 30:
  469. count = count + 1
  470. query = hapd.mgmt_rx()
  471. if query is None:
  472. raise Exception("Action frame not received")
  473. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  474. continue
  475. payload = query['payload']
  476. if len(payload) < 2:
  477. continue
  478. (category, action) = struct.unpack('BB', payload[0:2])
  479. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  480. continue
  481. return query
  482. raise Exception("No Action frame received")
  483. def parse_gas(payload):
  484. pos = payload
  485. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  486. if category != ACTION_CATEG_PUBLIC:
  487. return None
  488. if action not in GAS_ACTIONS:
  489. return None
  490. gas = {}
  491. gas['action'] = action
  492. pos = pos[3:]
  493. if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
  494. return None
  495. gas['dialog_token'] = dialog_token
  496. if action == GAS_INITIAL_RESPONSE:
  497. if len(pos) < 4:
  498. return None
  499. (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
  500. gas['status_code'] = status_code
  501. gas['comeback_delay'] = comeback_delay
  502. if action == GAS_COMEBACK_RESPONSE:
  503. if len(pos) < 5:
  504. return None
  505. (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
  506. gas['status_code'] = status_code
  507. gas['frag'] = frag
  508. gas['comeback_delay'] = comeback_delay
  509. return gas
  510. def action_response(req):
  511. resp = {}
  512. resp['fc'] = req['fc']
  513. resp['da'] = req['sa']
  514. resp['sa'] = req['da']
  515. resp['bssid'] = req['bssid']
  516. return resp
  517. def send_gas_resp(hapd, resp):
  518. hapd.mgmt_tx(resp)
  519. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  520. if ev is None:
  521. raise Exception("Missing TX status for GAS response")
  522. if "ok=1" not in ev:
  523. raise Exception("GAS response not acknowledged")
  524. def test_gas_invalid_response_type(dev, apdev):
  525. """GAS invalid response type"""
  526. hapd = start_ap(apdev[0])
  527. bssid = apdev[0]['bssid']
  528. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  529. hapd.set("ext_mgmt_frame_handling", "1")
  530. anqp_get(dev[0], bssid, 263)
  531. query = gas_rx(hapd)
  532. gas = parse_gas(query['payload'])
  533. resp = action_response(query)
  534. # GAS Comeback Response instead of GAS Initial Response
  535. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  536. send_gas_resp(hapd, resp)
  537. # station drops the invalid frame, so this needs to result in GAS timeout
  538. expect_gas_result(dev[0], "TIMEOUT")
  539. def test_gas_failure_status_code(dev, apdev):
  540. """GAS failure status code"""
  541. hapd = start_ap(apdev[0])
  542. bssid = apdev[0]['bssid']
  543. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  544. hapd.set("ext_mgmt_frame_handling", "1")
  545. anqp_get(dev[0], bssid, 263)
  546. query = gas_rx(hapd)
  547. gas = parse_gas(query['payload'])
  548. resp = action_response(query)
  549. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  550. send_gas_resp(hapd, resp)
  551. expect_gas_result(dev[0], "FAILURE")
  552. def test_gas_malformed(dev, apdev):
  553. """GAS malformed response frames"""
  554. hapd = start_ap(apdev[0])
  555. bssid = apdev[0]['bssid']
  556. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  557. hapd.set("ext_mgmt_frame_handling", "1")
  558. anqp_get(dev[0], bssid, 263)
  559. query = gas_rx(hapd)
  560. gas = parse_gas(query['payload'])
  561. resp = action_response(query)
  562. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  563. GAS_COMEBACK_RESPONSE,
  564. gas['dialog_token'], 0)
  565. hapd.mgmt_tx(resp)
  566. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  567. GAS_COMEBACK_RESPONSE,
  568. gas['dialog_token'], 0, 0)
  569. hapd.mgmt_tx(resp)
  570. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  571. gas['dialog_token'], 0, 0)
  572. resp['payload'] = hdr + struct.pack('B', 108)
  573. hapd.mgmt_tx(resp)
  574. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  575. hapd.mgmt_tx(resp)
  576. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  577. hapd.mgmt_tx(resp)
  578. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  579. hapd.mgmt_tx(resp)
  580. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  581. hapd.mgmt_tx(resp)
  582. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  583. hapd.mgmt_tx(resp)
  584. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  585. hapd.mgmt_tx(resp)
  586. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  587. hapd.mgmt_tx(resp)
  588. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  589. hapd.mgmt_tx(resp)
  590. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  591. hapd.mgmt_tx(resp)
  592. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  593. hapd.mgmt_tx(resp)
  594. # Station drops invalid frames, but the last of the responses is valid from
  595. # GAS view point even though it has an extra octet in the end and the ANQP
  596. # part of the response is not valid. This is reported as successfully
  597. # completed GAS exchange.
  598. expect_gas_result(dev[0], "SUCCESS")
  599. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  600. if ev is None:
  601. raise Exception("ANQP-QUERY-DONE not reported")
  602. if "result=INVALID_FRAME" not in ev:
  603. raise Exception("Unexpected result: " + ev)
  604. def init_gas(hapd, bssid, dev):
  605. anqp_get(dev, bssid, 263)
  606. query = gas_rx(hapd)
  607. gas = parse_gas(query['payload'])
  608. dialog_token = gas['dialog_token']
  609. resp = action_response(query)
  610. resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
  611. send_gas_resp(hapd, resp)
  612. query = gas_rx(hapd)
  613. gas = parse_gas(query['payload'])
  614. if gas['action'] != GAS_COMEBACK_REQUEST:
  615. raise Exception("Unexpected request action")
  616. if gas['dialog_token'] != dialog_token:
  617. raise Exception("Unexpected dialog token change")
  618. return query, dialog_token
  619. def allow_gas_initial_req(hapd, dialog_token):
  620. msg = hapd.mgmt_rx(timeout=1)
  621. if msg is not None:
  622. gas = parse_gas(msg['payload'])
  623. if gas['action'] != GAS_INITIAL_REQUEST or dialog_token == gas['dialog_token']:
  624. raise Exception("Unexpected management frame")
  625. def test_gas_malformed_comeback_resp(dev, apdev):
  626. """GAS malformed comeback response frames"""
  627. hapd = start_ap(apdev[0])
  628. bssid = apdev[0]['bssid']
  629. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  630. hapd.set("ext_mgmt_frame_handling", "1")
  631. logger.debug("Non-zero status code in comeback response")
  632. query, dialog_token = init_gas(hapd, bssid, dev[0])
  633. resp = action_response(query)
  634. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
  635. send_gas_resp(hapd, resp)
  636. expect_gas_result(dev[0], "FAILURE", status=2)
  637. logger.debug("Different advertisement protocol in comeback response")
  638. query, dialog_token = init_gas(hapd, bssid, dev[0])
  639. resp = action_response(query)
  640. resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
  641. send_gas_resp(hapd, resp)
  642. expect_gas_result(dev[0], "PEER_ERROR")
  643. logger.debug("Non-zero frag id and comeback delay in comeback response")
  644. query, dialog_token = init_gas(hapd, bssid, dev[0])
  645. resp = action_response(query)
  646. resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
  647. send_gas_resp(hapd, resp)
  648. expect_gas_result(dev[0], "PEER_ERROR")
  649. logger.debug("Unexpected frag id in comeback response")
  650. query, dialog_token = init_gas(hapd, bssid, dev[0])
  651. resp = action_response(query)
  652. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  653. send_gas_resp(hapd, resp)
  654. expect_gas_result(dev[0], "PEER_ERROR")
  655. logger.debug("Empty fragment and replay in comeback response")
  656. query, dialog_token = init_gas(hapd, bssid, dev[0])
  657. resp = action_response(query)
  658. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  659. send_gas_resp(hapd, resp)
  660. query = gas_rx(hapd)
  661. gas = parse_gas(query['payload'])
  662. if gas['action'] != GAS_COMEBACK_REQUEST:
  663. raise Exception("Unexpected request action")
  664. if gas['dialog_token'] != dialog_token:
  665. raise Exception("Unexpected dialog token change")
  666. resp = action_response(query)
  667. resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
  668. send_gas_resp(hapd, resp)
  669. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  670. send_gas_resp(hapd, resp)
  671. expect_gas_result(dev[0], "SUCCESS")
  672. logger.debug("Unexpected initial response when waiting for comeback response")
  673. query, dialog_token = init_gas(hapd, bssid, dev[0])
  674. resp = action_response(query)
  675. resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
  676. send_gas_resp(hapd, resp)
  677. allow_gas_initial_req(hapd, dialog_token)
  678. expect_gas_result(dev[0], "TIMEOUT")
  679. logger.debug("Too short comeback response")
  680. query, dialog_token = init_gas(hapd, bssid, dev[0])
  681. resp = action_response(query)
  682. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  683. GAS_COMEBACK_RESPONSE, dialog_token, 0)
  684. send_gas_resp(hapd, resp)
  685. allow_gas_initial_req(hapd, dialog_token)
  686. expect_gas_result(dev[0], "TIMEOUT")
  687. logger.debug("Too short comeback response(2)")
  688. query, dialog_token = init_gas(hapd, bssid, dev[0])
  689. resp = action_response(query)
  690. resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
  691. GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
  692. 0)
  693. send_gas_resp(hapd, resp)
  694. allow_gas_initial_req(hapd, dialog_token)
  695. expect_gas_result(dev[0], "TIMEOUT")
  696. logger.debug("Maximum comeback response fragment claiming more fragments")
  697. query, dialog_token = init_gas(hapd, bssid, dev[0])
  698. resp = action_response(query)
  699. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  700. send_gas_resp(hapd, resp)
  701. for i in range(1, 129):
  702. query = gas_rx(hapd)
  703. gas = parse_gas(query['payload'])
  704. if gas['action'] != GAS_COMEBACK_REQUEST:
  705. raise Exception("Unexpected request action")
  706. if gas['dialog_token'] != dialog_token:
  707. raise Exception("Unexpected dialog token change")
  708. resp = action_response(query)
  709. resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
  710. send_gas_resp(hapd, resp)
  711. expect_gas_result(dev[0], "PEER_ERROR")
  712. def test_gas_comeback_resp_additional_delay(dev, apdev):
  713. """GAS comeback response requesting additional delay"""
  714. hapd = start_ap(apdev[0])
  715. bssid = apdev[0]['bssid']
  716. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  717. hapd.set("ext_mgmt_frame_handling", "1")
  718. query, dialog_token = init_gas(hapd, bssid, dev[0])
  719. for i in range(0, 2):
  720. resp = action_response(query)
  721. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
  722. send_gas_resp(hapd, resp)
  723. query = gas_rx(hapd)
  724. gas = parse_gas(query['payload'])
  725. if gas['action'] != GAS_COMEBACK_REQUEST:
  726. raise Exception("Unexpected request action")
  727. if gas['dialog_token'] != dialog_token:
  728. raise Exception("Unexpected dialog token change")
  729. resp = action_response(query)
  730. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
  731. send_gas_resp(hapd, resp)
  732. expect_gas_result(dev[0], "SUCCESS")
  733. def test_gas_unknown_adv_proto(dev, apdev):
  734. """Unknown advertisement protocol id"""
  735. bssid = apdev[0]['bssid']
  736. params = hs20_ap_params()
  737. params['hessid'] = bssid
  738. hostapd.add_ap(apdev[0], params)
  739. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  740. req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
  741. if "FAIL" in req:
  742. raise Exception("GAS query request rejected")
  743. expect_gas_result(dev[0], "FAILURE", "59")
  744. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  745. if ev is None:
  746. raise Exception("GAS query timed out")
  747. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  748. res = re.split(exp, ev)
  749. if len(res) < 6:
  750. raise Exception("Could not parse GAS-RESPONSE-INFO")
  751. if res[2] != bssid:
  752. raise Exception("Unexpected BSSID in response")
  753. status = res[4]
  754. if status != "59":
  755. raise Exception("Unexpected GAS-RESPONSE-INFO status")
  756. def test_gas_request_oom(dev, apdev):
  757. """GAS_REQUEST OOM"""
  758. bssid = apdev[0]['bssid']
  759. params = hs20_ap_params()
  760. params['hessid'] = bssid
  761. hostapd.add_ap(apdev[0], params)
  762. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  763. with alloc_fail(dev[0], 1, "gas_build_req;gas_send_request"):
  764. if "FAIL" not in dev[0].request("GAS_REQUEST " + bssid + " 42"):
  765. raise Exception("GAS query request rejected")
  766. with alloc_fail(dev[0], 1, "gas_query_req;gas_send_request"):
  767. if "FAIL" not in dev[0].request("GAS_REQUEST " + bssid + " 42"):
  768. raise Exception("GAS query request rejected")
  769. with alloc_fail(dev[0], 1, "wpabuf_dup;gas_resp_cb"):
  770. if "OK" not in dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101"):
  771. raise Exception("GAS query request rejected")
  772. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  773. if ev is None:
  774. raise Exception("No GAS response")
  775. if "status_code=0" not in ev:
  776. raise Exception("GAS response indicated a failure")
  777. def test_gas_max_pending(dev, apdev):
  778. """GAS and maximum pending query limit"""
  779. hapd = start_ap(apdev[0])
  780. hapd.set("gas_frag_limit", "50")
  781. bssid = apdev[0]['bssid']
  782. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  783. wpas.interface_add("wlan5")
  784. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  785. raise Exception("Failed to set listen channel")
  786. if "OK" not in wpas.p2p_listen():
  787. raise Exception("Failed to start listen state")
  788. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  789. raise Exception("Failed to enable external management frame handling")
  790. anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
  791. gas = struct.pack('<H', len(anqp_query)) + anqp_query
  792. for dialog_token in range(1, 10):
  793. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
  794. dialog_token) + anqp_adv_proto() + gas
  795. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  796. if "OK" not in wpas.request(req):
  797. raise Exception("Could not send management frame")
  798. resp = wpas.mgmt_rx()
  799. if resp is None:
  800. raise Exception("MGMT-RX timeout")
  801. if 'payload' not in resp:
  802. raise Exception("Missing payload")
  803. gresp = parse_gas(resp['payload'])
  804. if gresp['dialog_token'] != dialog_token:
  805. raise Exception("Dialog token mismatch")
  806. status_code = gresp['status_code']
  807. if dialog_token < 9 and status_code != 0:
  808. raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
  809. if dialog_token > 8 and status_code == 0:
  810. raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
  811. def test_gas_no_pending(dev, apdev):
  812. """GAS and no pending query for comeback request"""
  813. hapd = start_ap(apdev[0])
  814. bssid = apdev[0]['bssid']
  815. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  816. wpas.interface_add("wlan5")
  817. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  818. raise Exception("Failed to set listen channel")
  819. if "OK" not in wpas.p2p_listen():
  820. raise Exception("Failed to start listen state")
  821. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  822. raise Exception("Failed to enable external management frame handling")
  823. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
  824. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  825. if "OK" not in wpas.request(req):
  826. raise Exception("Could not send management frame")
  827. resp = wpas.mgmt_rx()
  828. if resp is None:
  829. raise Exception("MGMT-RX timeout")
  830. if 'payload' not in resp:
  831. raise Exception("Missing payload")
  832. gresp = parse_gas(resp['payload'])
  833. status_code = gresp['status_code']
  834. if status_code != 60:
  835. raise Exception("Unexpected status code {} (expected 60)".format(status_code))
  836. def test_gas_delete_at_deinit(dev, apdev):
  837. """GAS query deleted at deinit"""
  838. hapd = start_ap(apdev[0])
  839. hapd.set("gas_comeback_delay", "1000")
  840. bssid = apdev[0]['bssid']
  841. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  842. wpas.interface_add("wlan5")
  843. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  844. wpas.request("ANQP_GET " + bssid + " 258")
  845. wpas.global_request("INTERFACE_REMOVE " + wpas.ifname)
  846. ev = wpas.wait_event(["GAS-QUERY-DONE"], timeout=2)
  847. del wpas
  848. if ev is None:
  849. raise Exception("GAS-QUERY-DONE not seen")
  850. if "result=DELETED_AT_DEINIT" not in ev:
  851. raise Exception("Unexpected result code: " + ev)
  852. def test_gas_missing_payload(dev, apdev):
  853. """No action code in the query frame"""
  854. bssid = apdev[0]['bssid']
  855. params = hs20_ap_params()
  856. params['hessid'] = bssid
  857. hostapd.add_ap(apdev[0], params)
  858. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  859. cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
  860. if "FAIL" in dev[0].request(cmd):
  861. raise Exception("Could not send test Action frame")
  862. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  863. if ev is None:
  864. raise Exception("Timeout on MGMT-TX-STATUS")
  865. if "result=SUCCESS" not in ev:
  866. raise Exception("AP did not ack Action frame")
  867. cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
  868. if "FAIL" in dev[0].request(cmd):
  869. raise Exception("Could not send test Action frame")
  870. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  871. if ev is None:
  872. raise Exception("Timeout on MGMT-TX-STATUS")
  873. if "result=SUCCESS" not in ev:
  874. raise Exception("AP did not ack Action frame")
  875. def test_gas_query_deinit(dev, apdev):
  876. """Pending GAS/ANQP query during deinit"""
  877. hapd = start_ap(apdev[0])
  878. bssid = apdev[0]['bssid']
  879. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  880. wpas.interface_add("wlan5")
  881. wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
  882. id = wpas.request("RADIO_WORK add block-work")
  883. if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
  884. raise Exception("ANQP_GET command failed")
  885. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  886. if ev is None:
  887. raise Exception("Timeout while waiting radio work to start")
  888. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  889. if ev is None:
  890. raise Exception("Timeout while waiting radio work to start (2)")
  891. # Remove the interface while the gas-query radio work is still pending and
  892. # GAS query has not yet been started.
  893. wpas.interface_remove("wlan5")
  894. @remote_compatible
  895. def test_gas_anqp_oom_wpas(dev, apdev):
  896. """GAS/ANQP query and OOM in wpa_supplicant"""
  897. hapd = start_ap(apdev[0])
  898. bssid = apdev[0]['bssid']
  899. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  900. with alloc_fail(dev[0], 1, "wpa_bss_anqp_alloc"):
  901. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  902. raise Exception("ANQP_GET command failed")
  903. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  904. if ev is None:
  905. raise Exception("ANQP query did not complete")
  906. with alloc_fail(dev[0], 1, "gas_build_req"):
  907. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  908. raise Exception("Unexpected ANQP_GET command success (OOM)")
  909. def test_gas_anqp_oom_hapd(dev, apdev):
  910. """GAS/ANQP query and OOM in hostapd"""
  911. hapd = start_ap(apdev[0])
  912. bssid = apdev[0]['bssid']
  913. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  914. with alloc_fail(hapd, 1, "gas_build_resp"):
  915. # This query will time out due to the AP not sending a response (OOM).
  916. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  917. raise Exception("ANQP_GET command failed")
  918. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  919. if ev is None:
  920. raise Exception("GAS query start timed out")
  921. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  922. if ev is None:
  923. raise Exception("GAS query timed out")
  924. if "result=TIMEOUT" not in ev:
  925. raise Exception("Unexpected result: " + ev)
  926. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  927. if ev is None:
  928. raise Exception("ANQP-QUERY-DONE event not seen")
  929. if "result=FAILURE" not in ev:
  930. raise Exception("Unexpected result: " + ev)
  931. with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
  932. hapd.set("gas_frag_limit", "50")
  933. # The first attempt of this query will time out due to the AP not
  934. # sending a response (OOM), but the retry succeeds.
  935. dev[0].request("FETCH_ANQP")
  936. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  937. if ev is None:
  938. raise Exception("GAS query start timed out")
  939. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  940. if ev is None:
  941. raise Exception("GAS query timed out")
  942. if "result=SUCCESS" not in ev:
  943. raise Exception("Unexpected result: " + ev)
  944. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  945. if ev is None:
  946. raise Exception("ANQP-QUERY-DONE event not seen")
  947. if "result=SUCCESS" not in ev:
  948. raise Exception("Unexpected result: " + ev)
  949. def test_gas_anqp_extra_elements(dev, apdev):
  950. """GAS/ANQP and extra ANQP elements"""
  951. geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100"
  952. civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5"
  953. held_uri = "https://held.example.com/location"
  954. held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri
  955. supl_fqdn = "supl.example.com"
  956. supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn
  957. public_id = binascii.hexlify(held + supl)
  958. params = { "ssid": "gas/anqp",
  959. "interworking": "1",
  960. "anqp_elem": [ "265:" + geo_loc,
  961. "266:" + civic_loc,
  962. "262:1122334455",
  963. "267:" + public_id,
  964. "279:01020304",
  965. "60000:01",
  966. "299:0102" ] }
  967. hapd = hostapd.add_ap(apdev[0], params)
  968. bssid = apdev[0]['bssid']
  969. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  970. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 265,266"):
  971. raise Exception("ANQP_GET command failed")
  972. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  973. if ev is None:
  974. raise Exception("GAS query timed out")
  975. bss = dev[0].get_bss(bssid)
  976. if 'anqp[265]' not in bss:
  977. raise Exception("AP Geospatial Location ANQP-element not seen")
  978. if bss['anqp[265]'] != geo_loc:
  979. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  980. if 'anqp[266]' not in bss:
  981. raise Exception("AP Civic Location ANQP-element not seen")
  982. if bss['anqp[266]'] != civic_loc:
  983. raise Exception("Unexpected AP Civic Location ANQP-element value: " + bss['anqp[266]'])
  984. dev[1].scan_for_bss(bssid, freq="2412", force_scan=True)
  985. 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"):
  986. raise Exception("ANQP_GET command failed")
  987. ev = dev[1].wait_event(["GAS-QUERY-DONE"], timeout=10)
  988. if ev is None:
  989. raise Exception("GAS query timed out")
  990. bss = dev[1].get_bss(bssid)
  991. if 'anqp[265]' not in bss:
  992. raise Exception("AP Geospatial Location ANQP-element not seen")
  993. if bss['anqp[265]'] != geo_loc:
  994. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  995. if 'anqp[266]' in bss:
  996. raise Exception("AP Civic Location ANQP-element unexpectedly seen")
  997. if 'anqp[267]' not in bss:
  998. raise Exception("AP Location Public Identifier ANQP-element not seen")
  999. if bss['anqp[267]'] != public_id:
  1000. raise Exception("Unexpected AP Location Public Identifier ANQP-element value: " + bss['anqp[267]'])
  1001. if 'anqp[279]' not in bss:
  1002. raise Exception("ANQP-element Info ID 279 not seen")
  1003. if bss['anqp[279]'] != "01020304":
  1004. raise Exception("Unexpected AP ANQP-element Info ID 279 value: " + bss['anqp[279]'])
  1005. if 'anqp[299]' not in bss:
  1006. raise Exception("ANQP-element Info ID 299 not seen")
  1007. if bss['anqp[299]'] != "0102":
  1008. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  1009. if 'anqp_ip_addr_type_availability' not in bss:
  1010. raise Exception("ANQP-element Info ID 292 not seen")
  1011. if bss['anqp_ip_addr_type_availability'] != "1122334455":
  1012. raise Exception("Unexpected AP ANQP-element Info ID 262 value: " + bss['anqp_ip_addr_type_availability'])
  1013. def test_gas_anqp_address3_not_assoc(dev, apdev, params):
  1014. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when not associated"""
  1015. try:
  1016. _test_gas_anqp_address3_not_assoc(dev, apdev, params)
  1017. finally:
  1018. dev[0].request("SET gas_address3 0")
  1019. def _test_gas_anqp_address3_not_assoc(dev, apdev, params):
  1020. hapd = start_ap(apdev[0])
  1021. bssid = apdev[0]['bssid']
  1022. if "OK" not in dev[0].request("SET gas_address3 1"):
  1023. raise Exception("Failed to set gas_address3")
  1024. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1025. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1026. raise Exception("ANQP_GET command failed")
  1027. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1028. if ev is None:
  1029. raise Exception("GAS query start timed out")
  1030. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1031. if ev is None:
  1032. raise Exception("GAS query timed out")
  1033. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1034. if ev is None or "Venue Name" not in ev:
  1035. raise Exception("Did not receive Venue Name")
  1036. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1037. if ev is None:
  1038. raise Exception("ANQP-QUERY-DONE event not seen")
  1039. if "result=SUCCESS" not in ev:
  1040. raise Exception("Unexpected result: " + ev)
  1041. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1042. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1043. display=["wlan.bssid"])
  1044. res = out.splitlines()
  1045. if len(res) != 2:
  1046. raise Exception("Unexpected number of GAS frames")
  1047. if res[0] != 'ff:ff:ff:ff:ff:ff':
  1048. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1049. if res[1] != 'ff:ff:ff:ff:ff:ff':
  1050. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1051. def test_gas_anqp_address3_assoc(dev, apdev, params):
  1052. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when associated"""
  1053. try:
  1054. _test_gas_anqp_address3_assoc(dev, apdev, params)
  1055. finally:
  1056. dev[0].request("SET gas_address3 0")
  1057. def _test_gas_anqp_address3_assoc(dev, apdev, params):
  1058. hapd = start_ap(apdev[0])
  1059. bssid = apdev[0]['bssid']
  1060. if "OK" not in dev[0].request("SET gas_address3 1"):
  1061. raise Exception("Failed to set gas_address3")
  1062. dev[0].scan_for_bss(bssid, freq="2412")
  1063. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  1064. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  1065. password="password", phase2="auth=MSCHAPV2",
  1066. ca_cert="auth_serv/ca.pem", scan_freq="2412")
  1067. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1068. raise Exception("ANQP_GET command failed")
  1069. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1070. if ev is None:
  1071. raise Exception("GAS query start timed out")
  1072. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1073. if ev is None:
  1074. raise Exception("GAS query timed out")
  1075. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1076. if ev is None or "Venue Name" not in ev:
  1077. raise Exception("Did not receive Venue Name")
  1078. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1079. if ev is None:
  1080. raise Exception("ANQP-QUERY-DONE event not seen")
  1081. if "result=SUCCESS" not in ev:
  1082. raise Exception("Unexpected result: " + ev)
  1083. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1084. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1085. display=["wlan.bssid"])
  1086. res = out.splitlines()
  1087. if len(res) != 2:
  1088. raise Exception("Unexpected number of GAS frames")
  1089. if res[0] != bssid:
  1090. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1091. if res[1] != bssid:
  1092. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1093. def test_gas_anqp_address3_ap_forced(dev, apdev, params):
  1094. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value on AP"""
  1095. hapd = start_ap(apdev[0])
  1096. bssid = apdev[0]['bssid']
  1097. hapd.set("gas_address3", "1")
  1098. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1099. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1100. raise Exception("ANQP_GET command failed")
  1101. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1102. if ev is None:
  1103. raise Exception("GAS query start timed out")
  1104. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1105. if ev is None:
  1106. raise Exception("GAS query timed out")
  1107. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1108. if ev is None or "Venue Name" not in ev:
  1109. raise Exception("Did not receive Venue Name")
  1110. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1111. if ev is None:
  1112. raise Exception("ANQP-QUERY-DONE event not seen")
  1113. if "result=SUCCESS" not in ev:
  1114. raise Exception("Unexpected result: " + ev)
  1115. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1116. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1117. display=["wlan.bssid"])
  1118. res = out.splitlines()
  1119. if len(res) != 2:
  1120. raise Exception("Unexpected number of GAS frames")
  1121. if res[0] != bssid:
  1122. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1123. if res[1] != 'ff:ff:ff:ff:ff:ff':
  1124. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1125. def test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1126. """GAS/ANQP query using IEEE 802.11 non-compliant Address 3 (AP)"""
  1127. try:
  1128. _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params)
  1129. finally:
  1130. dev[0].request("SET gas_address3 0")
  1131. def _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1132. hapd = start_ap(apdev[0])
  1133. bssid = apdev[0]['bssid']
  1134. hapd.set("gas_address3", "2")
  1135. if "OK" not in dev[0].request("SET gas_address3 1"):
  1136. raise Exception("Failed to set gas_address3")
  1137. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1138. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1139. raise Exception("ANQP_GET command failed")
  1140. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1141. if ev is None:
  1142. raise Exception("GAS query start timed out")
  1143. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1144. if ev is None:
  1145. raise Exception("GAS query timed out")
  1146. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1147. if ev is None or "Venue Name" not in ev:
  1148. raise Exception("Did not receive Venue Name")
  1149. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1150. if ev is None:
  1151. raise Exception("ANQP-QUERY-DONE event not seen")
  1152. if "result=SUCCESS" not in ev:
  1153. raise Exception("Unexpected result: " + ev)
  1154. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1155. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1156. display=["wlan.bssid"])
  1157. res = out.splitlines()
  1158. if len(res) != 2:
  1159. raise Exception("Unexpected number of GAS frames")
  1160. if res[0] != 'ff:ff:ff:ff:ff:ff':
  1161. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1162. if res[1] != bssid:
  1163. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1164. def test_gas_prot_vs_not_prot(dev, apdev, params):
  1165. """GAS/ANQP query protected vs. not protected"""
  1166. hapd = start_ap(apdev[0])
  1167. bssid = apdev[0]['bssid']
  1168. dev[0].scan_for_bss(bssid, freq="2412")
  1169. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  1170. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  1171. password="password", phase2="auth=MSCHAPV2",
  1172. ca_cert="auth_serv/ca.pem", scan_freq="2412",
  1173. ieee80211w="2")
  1174. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1175. raise Exception("ANQP_GET command failed")
  1176. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1177. if ev is None:
  1178. raise Exception("No GAS-QUERY-DONE event")
  1179. if "result=SUCCESS" not in ev:
  1180. raise Exception("Unexpected GAS result: " + ev)
  1181. # GAS: Drop unexpected unprotected GAS frame when PMF is enabled
  1182. dev[0].request("SET ext_mgmt_frame_handling 1")
  1183. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1184. dev[0].request("SET ext_mgmt_frame_handling 0")
  1185. if "OK" not in res:
  1186. raise Exception("MGMT_RX_PROCESS failed")
  1187. dev[0].request("DISCONNECT")
  1188. dev[0].wait_disconnected()
  1189. # GAS: No pending query found for 02:00:00:00:03:00 dialog token 0
  1190. dev[0].request("SET ext_mgmt_frame_handling 1")
  1191. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1192. dev[0].request("SET ext_mgmt_frame_handling 0")
  1193. if "OK" not in res:
  1194. raise Exception("MGMT_RX_PROCESS failed")
  1195. # GAS: Drop unexpected protected GAS frame when PMF is disabled
  1196. dev[0].request("SET ext_mgmt_frame_handling 1")
  1197. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000090b00000005006c027f000000")
  1198. dev[0].request("SET ext_mgmt_frame_handling 0")
  1199. if "OK" not in res:
  1200. raise Exception("MGMT_RX_PROCESS failed")
  1201. def test_gas_failures(dev, apdev):
  1202. """GAS failure cases"""
  1203. hapd = start_ap(apdev[0])
  1204. hapd.set("gas_comeback_delay", "5")
  1205. bssid = apdev[0]['bssid']
  1206. hapd2 = start_ap(apdev[1])
  1207. bssid2 = apdev[1]['bssid']
  1208. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1209. dev[0].scan_for_bss(bssid2, freq="2412")
  1210. tests = [ (bssid, "gas_build_req;gas_query_tx_comeback_req"),
  1211. (bssid, "gas_query_tx;gas_query_tx_comeback_req"),
  1212. (bssid, "gas_query_append;gas_query_rx_comeback"),
  1213. (bssid2, "gas_query_append;gas_query_rx_initial"),
  1214. (bssid2, "wpabuf_alloc_copy;gas_query_rx_initial"),
  1215. (bssid, "gas_query_tx;gas_query_tx_initial_req") ]
  1216. for addr,func in tests:
  1217. with alloc_fail(dev[0], 1, func):
  1218. dev[0].request("ANQP_GET " + addr + " 258")
  1219. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1220. if ev is None:
  1221. raise Exception("No GAS-QUERY-DONE seen")
  1222. if "result=INTERNAL_ERROR" not in ev:
  1223. raise Exception("Unexpected result code: " + ev)
  1224. dev[0].dump_monitor()
  1225. tests = [ "=gas_query_req", "radio_add_work;gas_query_req" ]
  1226. for func in tests:
  1227. with alloc_fail(dev[0], 1, func):
  1228. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1229. raise Exception("ANQP_GET succeeded unexpectedly during OOM")
  1230. dev[0].dump_monitor()
  1231. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  1232. wpas.interface_add("wlan5")
  1233. wpas.scan_for_bss(bssid2, freq="2412")
  1234. wpas.request("SET preassoc_mac_addr 1111")
  1235. wpas.request("ANQP_GET " + bssid2 + " 258")
  1236. ev = wpas.wait_event(["Failed to assign random MAC address for GAS"],
  1237. timeout=5)
  1238. wpas.request("SET preassoc_mac_addr 0")
  1239. if ev is None:
  1240. raise Exception("No random MAC address error seen")
  1241. def test_gas_anqp_venue_url(dev, apdev):
  1242. """GAS/ANQP and Venue URL"""
  1243. venue_group = 1
  1244. venue_type = 13
  1245. venue_info = struct.pack('BB', venue_group, venue_type)
  1246. lang1 = "eng"
  1247. name1= "Example venue"
  1248. lang2 = "fin"
  1249. name2 = "Esimerkkipaikka"
  1250. venue1 = struct.pack('B', len(lang1 + name1)) + lang1 + name1
  1251. venue2 = struct.pack('B', len(lang2 + name2)) + lang2 + name2
  1252. venue_name = binascii.hexlify(venue_info + venue1 + venue2)
  1253. url1 = "http://example.com/venue"
  1254. url2 = "https://example.org/venue-info/"
  1255. duple1 = struct.pack('BB', 1 + len(url1), 0)
  1256. duple2 = struct.pack('BB', 1 + len(url2), 1)
  1257. venue_url = binascii.hexlify(duple1 + duple2)
  1258. params = { "ssid": "gas/anqp",
  1259. "interworking": "1",
  1260. "venue_group": str(venue_group),
  1261. "venue_type": str(venue_type),
  1262. "venue_name": [ lang1 + ":" + name1, lang2 + ":" + name2 ],
  1263. "anqp_elem": [ "277:" + venue_url ] }
  1264. hapd = hostapd.add_ap(apdev[0], params)
  1265. bssid = apdev[0]['bssid']
  1266. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1267. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 257,258,277"):
  1268. raise Exception("ANQP_GET command failed")
  1269. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1270. if ev is None:
  1271. raise Exception("GAS query timed out")
  1272. bss = dev[0].get_bss(bssid)
  1273. if 'anqp_venue_name' not in bss:
  1274. raise Exception("Venue Name ANQP-element not seen")
  1275. if bss['anqp_venue_name'] != venue_name:
  1276. raise Exception("Unexpected Venue Name ANQP-element value: " + bss['anqp_venue_name'])
  1277. if 'anqp[277]' not in bss:
  1278. raise Exception("Venue URL ANQP-element not seen")
  1279. if bss['anqp[277]'] != venue_url:
  1280. raise Exception("Unexpected Venue URL ANQP-element value: " + bss['anqp[277]'])
  1281. if 'anqp_capability_list' not in bss:
  1282. raise Exception("Capability List ANQP-element not seen")
  1283. ids = struct.pack('<HHH', 257, 258, 277)
  1284. if not bss['anqp_capability_list'].startswith(binascii.hexlify(ids)):
  1285. raise Exception("Unexpected Capability List ANQP-element value: " + bss['anqp_capability_list'])
  1286. def test_gas_anqp_capab_list(dev, apdev):
  1287. """GAS/ANQP and Capability List ANQP-element"""
  1288. params = { "ssid": "gas/anqp",
  1289. "interworking": "1" }
  1290. params["anqp_elem"] = []
  1291. for i in range(0, 400):
  1292. if i not in [ 257 ]:
  1293. params["anqp_elem"] += [ "%d:010203" % i ]
  1294. hapd = hostapd.add_ap(apdev[0], params)
  1295. bssid = apdev[0]['bssid']
  1296. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1297. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 257"):
  1298. raise Exception("ANQP_GET command failed")
  1299. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1300. if ev is None:
  1301. raise Exception("GAS query timed out")
  1302. bss = dev[0].get_bss(bssid)
  1303. if 'anqp_capability_list' not in bss:
  1304. raise Exception("Capability List ANQP-element not seen")
  1305. val = bss['anqp_capability_list']
  1306. logger.info("anqp_capability_list: " + val)
  1307. ids = []
  1308. while len(val) >= 4:
  1309. id_bin = binascii.unhexlify(val[0:4])
  1310. id = struct.unpack('<H', id_bin)[0]
  1311. if id == 0xdddd:
  1312. break
  1313. ids.append(id)
  1314. val = val[4:]
  1315. logger.info("InfoIDs: " + str(ids))
  1316. for i in range(257, 300):
  1317. if i in [ 273, 274 ]:
  1318. continue
  1319. if i not in ids:
  1320. raise Exception("Unexpected Capability List ANQP-element value (missing %d): %s" % (i, bss['anqp_capability_list']))