test_gas.py 73 KB

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