test_gas.py 55 KB

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