test_gas.py 49 KB

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