test_gas.py 62 KB

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