test_gas.py 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. for cmd in cmds:
  329. if "FAIL" not in dev[0].request("ANQP_GET " + cmd):
  330. raise Exception("Invalid ANQP_GET accepted")
  331. cmds = [ "",
  332. "foo",
  333. "00:11:22:33:44:55 -1",
  334. "00:11:22:33:44:55 0",
  335. "00:11:22:33:44:55 32",
  336. "00:11:22:33:44:55",
  337. "00:11:22:33:44:55 ",
  338. "00:11:22:33:44:55 0" ]
  339. for cmd in cmds:
  340. if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
  341. raise Exception("Invalid HS20_ANQP_GET accepted")
  342. def expect_gas_result(dev, result, status=None):
  343. ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
  344. if ev is None:
  345. raise Exception("GAS query timed out")
  346. if "result=" + result not in ev:
  347. raise Exception("Unexpected GAS query result")
  348. if status and "status_code=" + str(status) + ' ' not in ev:
  349. raise Exception("Unexpected GAS status code")
  350. def anqp_get(dev, bssid, id):
  351. if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
  352. raise Exception("ANQP_GET command failed")
  353. ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
  354. if ev is None:
  355. raise Exception("GAS query start timed out")
  356. def test_gas_timeout(dev, apdev):
  357. """GAS timeout"""
  358. hapd = start_ap(apdev[0])
  359. bssid = apdev[0]['bssid']
  360. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  361. hapd.set("ext_mgmt_frame_handling", "1")
  362. anqp_get(dev[0], bssid, 263)
  363. ev = hapd.wait_event(["MGMT-RX"], timeout=5)
  364. if ev is None:
  365. raise Exception("MGMT RX wait timed out")
  366. expect_gas_result(dev[0], "TIMEOUT")
  367. MGMT_SUBTYPE_ACTION = 13
  368. ACTION_CATEG_PUBLIC = 4
  369. GAS_INITIAL_REQUEST = 10
  370. GAS_INITIAL_RESPONSE = 11
  371. GAS_COMEBACK_REQUEST = 12
  372. GAS_COMEBACK_RESPONSE = 13
  373. GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
  374. GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
  375. def anqp_adv_proto():
  376. return struct.pack('BBBB', 108, 2, 127, 0)
  377. def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
  378. return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  379. dialog_token, status_code, comeback_delay) + anqp_adv_proto()
  380. def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
  381. if more:
  382. id |= 0x80
  383. if bogus_adv_proto:
  384. adv = struct.pack('BBBB', 108, 2, 127, 1)
  385. else:
  386. adv = anqp_adv_proto()
  387. return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
  388. dialog_token, status_code, id, comeback_delay) + adv
  389. def gas_rx(hapd):
  390. count = 0
  391. while count < 30:
  392. count = count + 1
  393. query = hapd.mgmt_rx()
  394. if query is None:
  395. raise Exception("Action frame not received")
  396. if query['subtype'] != MGMT_SUBTYPE_ACTION:
  397. continue
  398. payload = query['payload']
  399. if len(payload) < 2:
  400. continue
  401. (category, action) = struct.unpack('BB', payload[0:2])
  402. if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
  403. continue
  404. return query
  405. raise Exception("No Action frame received")
  406. def parse_gas(payload):
  407. pos = payload
  408. (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
  409. if category != ACTION_CATEG_PUBLIC:
  410. return None
  411. if action not in GAS_ACTIONS:
  412. return None
  413. gas = {}
  414. gas['action'] = action
  415. pos = pos[3:]
  416. if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
  417. return None
  418. gas['dialog_token'] = dialog_token
  419. if action == GAS_INITIAL_RESPONSE:
  420. if len(pos) < 4:
  421. return None
  422. (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
  423. gas['status_code'] = status_code
  424. gas['comeback_delay'] = comeback_delay
  425. if action == GAS_COMEBACK_RESPONSE:
  426. if len(pos) < 5:
  427. return None
  428. (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
  429. gas['status_code'] = status_code
  430. gas['frag'] = frag
  431. gas['comeback_delay'] = comeback_delay
  432. return gas
  433. def action_response(req):
  434. resp = {}
  435. resp['fc'] = req['fc']
  436. resp['da'] = req['sa']
  437. resp['sa'] = req['da']
  438. resp['bssid'] = req['bssid']
  439. return resp
  440. def send_gas_resp(hapd, resp):
  441. hapd.mgmt_tx(resp)
  442. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  443. if ev is None:
  444. raise Exception("Missing TX status for GAS response")
  445. if "ok=1" not in ev:
  446. raise Exception("GAS response not acknowledged")
  447. def test_gas_invalid_response_type(dev, apdev):
  448. """GAS invalid response type"""
  449. hapd = start_ap(apdev[0])
  450. bssid = apdev[0]['bssid']
  451. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  452. hapd.set("ext_mgmt_frame_handling", "1")
  453. anqp_get(dev[0], bssid, 263)
  454. query = gas_rx(hapd)
  455. gas = parse_gas(query['payload'])
  456. resp = action_response(query)
  457. # GAS Comeback Response instead of GAS Initial Response
  458. resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
  459. send_gas_resp(hapd, resp)
  460. # station drops the invalid frame, so this needs to result in GAS timeout
  461. expect_gas_result(dev[0], "TIMEOUT")
  462. def test_gas_failure_status_code(dev, apdev):
  463. """GAS failure status code"""
  464. hapd = start_ap(apdev[0])
  465. bssid = apdev[0]['bssid']
  466. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  467. hapd.set("ext_mgmt_frame_handling", "1")
  468. anqp_get(dev[0], bssid, 263)
  469. query = gas_rx(hapd)
  470. gas = parse_gas(query['payload'])
  471. resp = action_response(query)
  472. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
  473. send_gas_resp(hapd, resp)
  474. expect_gas_result(dev[0], "FAILURE")
  475. def test_gas_malformed(dev, apdev):
  476. """GAS malformed response frames"""
  477. hapd = start_ap(apdev[0])
  478. bssid = apdev[0]['bssid']
  479. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  480. hapd.set("ext_mgmt_frame_handling", "1")
  481. anqp_get(dev[0], bssid, 263)
  482. query = gas_rx(hapd)
  483. gas = parse_gas(query['payload'])
  484. resp = action_response(query)
  485. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  486. GAS_COMEBACK_RESPONSE,
  487. gas['dialog_token'], 0)
  488. hapd.mgmt_tx(resp)
  489. resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
  490. GAS_COMEBACK_RESPONSE,
  491. gas['dialog_token'], 0, 0)
  492. hapd.mgmt_tx(resp)
  493. hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
  494. gas['dialog_token'], 0, 0)
  495. resp['payload'] = hdr + struct.pack('B', 108)
  496. hapd.mgmt_tx(resp)
  497. resp['payload'] = hdr + struct.pack('BB', 108, 0)
  498. hapd.mgmt_tx(resp)
  499. resp['payload'] = hdr + struct.pack('BB', 108, 1)
  500. hapd.mgmt_tx(resp)
  501. resp['payload'] = hdr + struct.pack('BB', 108, 255)
  502. hapd.mgmt_tx(resp)
  503. resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
  504. hapd.mgmt_tx(resp)
  505. resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
  506. hapd.mgmt_tx(resp)
  507. resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
  508. hapd.mgmt_tx(resp)
  509. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
  510. hapd.mgmt_tx(resp)
  511. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
  512. hapd.mgmt_tx(resp)
  513. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
  514. hapd.mgmt_tx(resp)
  515. resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
  516. hapd.mgmt_tx(resp)
  517. # Station drops invalid frames, but the last of the responses is valid from
  518. # GAS view point even though it has an extra octet in the end and the ANQP
  519. # part of the response is not valid. This is reported as successfully
  520. # completed GAS exchange.
  521. expect_gas_result(dev[0], "SUCCESS")
  522. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  523. if ev is None:
  524. raise Exception("ANQP-QUERY-DONE not reported")
  525. if "result=INVALID_FRAME" not in ev:
  526. raise Exception("Unexpected result: " + ev)
  527. def init_gas(hapd, bssid, dev):
  528. anqp_get(dev, bssid, 263)
  529. query = gas_rx(hapd)
  530. gas = parse_gas(query['payload'])
  531. dialog_token = gas['dialog_token']
  532. resp = action_response(query)
  533. resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
  534. send_gas_resp(hapd, resp)
  535. query = gas_rx(hapd)
  536. gas = parse_gas(query['payload'])
  537. if gas['action'] != GAS_COMEBACK_REQUEST:
  538. raise Exception("Unexpected request action")
  539. if gas['dialog_token'] != dialog_token:
  540. raise Exception("Unexpected dialog token change")
  541. return query, dialog_token
  542. def allow_gas_initial_req(hapd, dialog_token):
  543. msg = hapd.mgmt_rx(timeout=1)
  544. if msg is not None:
  545. gas = parse_gas(msg['payload'])
  546. if gas['action'] != GAS_INITIAL_REQUEST or dialog_token == gas['dialog_token']:
  547. raise Exception("Unexpected management frame")
  548. def test_gas_malformed_comeback_resp(dev, apdev):
  549. """GAS malformed comeback response frames"""
  550. hapd = start_ap(apdev[0])
  551. bssid = apdev[0]['bssid']
  552. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  553. hapd.set("ext_mgmt_frame_handling", "1")
  554. logger.debug("Non-zero status code in comeback response")
  555. query, dialog_token = init_gas(hapd, bssid, dev[0])
  556. resp = action_response(query)
  557. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
  558. send_gas_resp(hapd, resp)
  559. expect_gas_result(dev[0], "FAILURE", status=2)
  560. logger.debug("Different advertisement protocol in comeback response")
  561. query, dialog_token = init_gas(hapd, bssid, dev[0])
  562. resp = action_response(query)
  563. resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
  564. send_gas_resp(hapd, resp)
  565. expect_gas_result(dev[0], "PEER_ERROR")
  566. logger.debug("Non-zero frag id and comeback delay in comeback response")
  567. query, dialog_token = init_gas(hapd, bssid, dev[0])
  568. resp = action_response(query)
  569. resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
  570. send_gas_resp(hapd, resp)
  571. expect_gas_result(dev[0], "PEER_ERROR")
  572. logger.debug("Unexpected frag id in comeback response")
  573. query, dialog_token = init_gas(hapd, bssid, dev[0])
  574. resp = action_response(query)
  575. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  576. send_gas_resp(hapd, resp)
  577. expect_gas_result(dev[0], "PEER_ERROR")
  578. logger.debug("Empty fragment and replay in comeback response")
  579. query, dialog_token = init_gas(hapd, bssid, dev[0])
  580. resp = action_response(query)
  581. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  582. send_gas_resp(hapd, resp)
  583. query = gas_rx(hapd)
  584. gas = parse_gas(query['payload'])
  585. if gas['action'] != GAS_COMEBACK_REQUEST:
  586. raise Exception("Unexpected request action")
  587. if gas['dialog_token'] != dialog_token:
  588. raise Exception("Unexpected dialog token change")
  589. resp = action_response(query)
  590. resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
  591. send_gas_resp(hapd, resp)
  592. resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
  593. send_gas_resp(hapd, resp)
  594. expect_gas_result(dev[0], "SUCCESS")
  595. logger.debug("Unexpected initial response when waiting for comeback response")
  596. query, dialog_token = init_gas(hapd, bssid, dev[0])
  597. resp = action_response(query)
  598. resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
  599. send_gas_resp(hapd, resp)
  600. allow_gas_initial_req(hapd, dialog_token)
  601. expect_gas_result(dev[0], "TIMEOUT")
  602. logger.debug("Too short comeback response")
  603. query, dialog_token = init_gas(hapd, bssid, dev[0])
  604. resp = action_response(query)
  605. resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
  606. GAS_COMEBACK_RESPONSE, dialog_token, 0)
  607. send_gas_resp(hapd, resp)
  608. allow_gas_initial_req(hapd, dialog_token)
  609. expect_gas_result(dev[0], "TIMEOUT")
  610. logger.debug("Too short comeback response(2)")
  611. query, dialog_token = init_gas(hapd, bssid, dev[0])
  612. resp = action_response(query)
  613. resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
  614. GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
  615. 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("Maximum comeback response fragment claiming more fragments")
  620. query, dialog_token = init_gas(hapd, bssid, dev[0])
  621. resp = action_response(query)
  622. resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
  623. send_gas_resp(hapd, resp)
  624. for i in range(1, 129):
  625. query = gas_rx(hapd)
  626. gas = parse_gas(query['payload'])
  627. if gas['action'] != GAS_COMEBACK_REQUEST:
  628. raise Exception("Unexpected request action")
  629. if gas['dialog_token'] != dialog_token:
  630. raise Exception("Unexpected dialog token change")
  631. resp = action_response(query)
  632. resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
  633. send_gas_resp(hapd, resp)
  634. expect_gas_result(dev[0], "PEER_ERROR")
  635. def test_gas_comeback_resp_additional_delay(dev, apdev):
  636. """GAS comeback response requesting additional delay"""
  637. hapd = start_ap(apdev[0])
  638. bssid = apdev[0]['bssid']
  639. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  640. hapd.set("ext_mgmt_frame_handling", "1")
  641. query, dialog_token = init_gas(hapd, bssid, dev[0])
  642. for i in range(0, 2):
  643. resp = action_response(query)
  644. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
  645. send_gas_resp(hapd, resp)
  646. query = gas_rx(hapd)
  647. gas = parse_gas(query['payload'])
  648. if gas['action'] != GAS_COMEBACK_REQUEST:
  649. raise Exception("Unexpected request action")
  650. if gas['dialog_token'] != dialog_token:
  651. raise Exception("Unexpected dialog token change")
  652. resp = action_response(query)
  653. resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
  654. send_gas_resp(hapd, resp)
  655. expect_gas_result(dev[0], "SUCCESS")
  656. def test_gas_unknown_adv_proto(dev, apdev):
  657. """Unknown advertisement protocol id"""
  658. bssid = apdev[0]['bssid']
  659. params = hs20_ap_params()
  660. params['hessid'] = bssid
  661. hostapd.add_ap(apdev[0], params)
  662. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  663. req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
  664. if "FAIL" in req:
  665. raise Exception("GAS query request rejected")
  666. expect_gas_result(dev[0], "FAILURE", "59")
  667. ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
  668. if ev is None:
  669. raise Exception("GAS query timed out")
  670. exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
  671. res = re.split(exp, ev)
  672. if len(res) < 6:
  673. raise Exception("Could not parse GAS-RESPONSE-INFO")
  674. if res[2] != bssid:
  675. raise Exception("Unexpected BSSID in response")
  676. status = res[4]
  677. if status != "59":
  678. raise Exception("Unexpected GAS-RESPONSE-INFO status")
  679. def test_gas_max_pending(dev, apdev):
  680. """GAS and maximum pending query limit"""
  681. hapd = start_ap(apdev[0])
  682. hapd.set("gas_frag_limit", "50")
  683. bssid = apdev[0]['bssid']
  684. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  685. wpas.interface_add("wlan5")
  686. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  687. raise Exception("Failed to set listen channel")
  688. if "OK" not in wpas.p2p_listen():
  689. raise Exception("Failed to start listen state")
  690. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  691. raise Exception("Failed to enable external management frame handling")
  692. anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
  693. gas = struct.pack('<H', len(anqp_query)) + anqp_query
  694. for dialog_token in range(1, 10):
  695. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
  696. dialog_token) + anqp_adv_proto() + gas
  697. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  698. if "OK" not in wpas.request(req):
  699. raise Exception("Could not send management frame")
  700. resp = wpas.mgmt_rx()
  701. if resp is None:
  702. raise Exception("MGMT-RX timeout")
  703. if 'payload' not in resp:
  704. raise Exception("Missing payload")
  705. gresp = parse_gas(resp['payload'])
  706. if gresp['dialog_token'] != dialog_token:
  707. raise Exception("Dialog token mismatch")
  708. status_code = gresp['status_code']
  709. if dialog_token < 9 and status_code != 0:
  710. raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
  711. if dialog_token > 8 and status_code == 0:
  712. raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
  713. def test_gas_no_pending(dev, apdev):
  714. """GAS and no pending query for comeback request"""
  715. hapd = start_ap(apdev[0])
  716. bssid = apdev[0]['bssid']
  717. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  718. wpas.interface_add("wlan5")
  719. if "OK" not in wpas.request("P2P_SET listen_channel 1"):
  720. raise Exception("Failed to set listen channel")
  721. if "OK" not in wpas.p2p_listen():
  722. raise Exception("Failed to start listen state")
  723. if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
  724. raise Exception("Failed to enable external management frame handling")
  725. msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
  726. req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
  727. if "OK" not in wpas.request(req):
  728. raise Exception("Could not send management frame")
  729. resp = wpas.mgmt_rx()
  730. if resp is None:
  731. raise Exception("MGMT-RX timeout")
  732. if 'payload' not in resp:
  733. raise Exception("Missing payload")
  734. gresp = parse_gas(resp['payload'])
  735. status_code = gresp['status_code']
  736. if status_code != 60:
  737. raise Exception("Unexpected status code {} (expected 60)".format(status_code))
  738. def test_gas_delete_at_deinit(dev, apdev):
  739. """GAS query deleted at deinit"""
  740. hapd = start_ap(apdev[0])
  741. hapd.set("gas_comeback_delay", "1000")
  742. bssid = apdev[0]['bssid']
  743. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  744. wpas.interface_add("wlan5")
  745. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  746. wpas.request("ANQP_GET " + bssid + " 258")
  747. wpas.global_request("INTERFACE_REMOVE " + wpas.ifname)
  748. ev = wpas.wait_event(["GAS-QUERY-DONE"], timeout=2)
  749. del wpas
  750. if ev is None:
  751. raise Exception("GAS-QUERY-DONE not seen")
  752. if "result=DELETED_AT_DEINIT" not in ev:
  753. raise Exception("Unexpected result code: " + ev)
  754. def test_gas_missing_payload(dev, apdev):
  755. """No action code in the query frame"""
  756. bssid = apdev[0]['bssid']
  757. params = hs20_ap_params()
  758. params['hessid'] = bssid
  759. hostapd.add_ap(apdev[0], params)
  760. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  761. cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
  762. if "FAIL" in dev[0].request(cmd):
  763. raise Exception("Could not send test Action frame")
  764. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  765. if ev is None:
  766. raise Exception("Timeout on MGMT-TX-STATUS")
  767. if "result=SUCCESS" not in ev:
  768. raise Exception("AP did not ack Action frame")
  769. cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
  770. if "FAIL" in dev[0].request(cmd):
  771. raise Exception("Could not send test Action frame")
  772. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  773. if ev is None:
  774. raise Exception("Timeout on MGMT-TX-STATUS")
  775. if "result=SUCCESS" not in ev:
  776. raise Exception("AP did not ack Action frame")
  777. def test_gas_query_deinit(dev, apdev):
  778. """Pending GAS/ANQP query during deinit"""
  779. hapd = start_ap(apdev[0])
  780. bssid = apdev[0]['bssid']
  781. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  782. wpas.interface_add("wlan5")
  783. wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
  784. id = wpas.request("RADIO_WORK add block-work")
  785. if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
  786. raise Exception("ANQP_GET command failed")
  787. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  788. if ev is None:
  789. raise Exception("Timeout while waiting radio work to start")
  790. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  791. if ev is None:
  792. raise Exception("Timeout while waiting radio work to start (2)")
  793. # Remove the interface while the gas-query radio work is still pending and
  794. # GAS query has not yet been started.
  795. wpas.interface_remove("wlan5")
  796. @remote_compatible
  797. def test_gas_anqp_oom_wpas(dev, apdev):
  798. """GAS/ANQP query and OOM in wpa_supplicant"""
  799. hapd = start_ap(apdev[0])
  800. bssid = apdev[0]['bssid']
  801. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  802. with alloc_fail(dev[0], 1, "wpa_bss_anqp_alloc"):
  803. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  804. raise Exception("ANQP_GET command failed")
  805. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  806. if ev is None:
  807. raise Exception("ANQP query did not complete")
  808. with alloc_fail(dev[0], 1, "gas_build_req"):
  809. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  810. raise Exception("Unexpected ANQP_GET command success (OOM)")
  811. def test_gas_anqp_oom_hapd(dev, apdev):
  812. """GAS/ANQP query and OOM in hostapd"""
  813. hapd = start_ap(apdev[0])
  814. bssid = apdev[0]['bssid']
  815. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  816. with alloc_fail(hapd, 1, "gas_build_resp"):
  817. # This query will time out due to the AP not sending a response (OOM).
  818. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  819. raise Exception("ANQP_GET command failed")
  820. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  821. if ev is None:
  822. raise Exception("GAS query start timed out")
  823. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  824. if ev is None:
  825. raise Exception("GAS query timed out")
  826. if "result=TIMEOUT" not in ev:
  827. raise Exception("Unexpected result: " + ev)
  828. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  829. if ev is None:
  830. raise Exception("ANQP-QUERY-DONE event not seen")
  831. if "result=FAILURE" not in ev:
  832. raise Exception("Unexpected result: " + ev)
  833. with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
  834. hapd.set("gas_frag_limit", "50")
  835. # The first attempt of this query will time out due to the AP not
  836. # sending a response (OOM), but the retry succeeds.
  837. dev[0].request("FETCH_ANQP")
  838. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  839. if ev is None:
  840. raise Exception("GAS query start timed out")
  841. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  842. if ev is None:
  843. raise Exception("GAS query timed out")
  844. if "result=SUCCESS" not in ev:
  845. raise Exception("Unexpected result: " + ev)
  846. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  847. if ev is None:
  848. raise Exception("ANQP-QUERY-DONE event not seen")
  849. if "result=SUCCESS" not in ev:
  850. raise Exception("Unexpected result: " + ev)
  851. def test_gas_anqp_extra_elements(dev, apdev):
  852. """GAS/ANQP and extra ANQP elements"""
  853. geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100"
  854. civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5"
  855. held_uri = "https://held.example.com/location"
  856. held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri
  857. supl_fqdn = "supl.example.com"
  858. supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn
  859. public_id = binascii.hexlify(held + supl)
  860. params = { "ssid": "gas/anqp",
  861. "interworking": "1",
  862. "anqp_elem": [ "265:" + geo_loc,
  863. "266:" + civic_loc,
  864. "262:1122334455",
  865. "267:" + public_id,
  866. "275:01020304",
  867. "60000:01",
  868. "299:0102" ] }
  869. hapd = hostapd.add_ap(apdev[0], params)
  870. bssid = apdev[0]['bssid']
  871. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  872. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 265,266"):
  873. raise Exception("ANQP_GET command failed")
  874. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  875. if ev is None:
  876. raise Exception("GAS query timed out")
  877. bss = dev[0].get_bss(bssid)
  878. if 'anqp[265]' not in bss:
  879. raise Exception("AP Geospatial Location ANQP-element not seen")
  880. if bss['anqp[265]'] != geo_loc:
  881. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  882. if 'anqp[266]' not in bss:
  883. raise Exception("AP Civic Location ANQP-element not seen")
  884. if bss['anqp[266]'] != civic_loc:
  885. raise Exception("Unexpected AP Civic Location ANQP-element value: " + bss['anqp[266]'])
  886. dev[1].scan_for_bss(bssid, freq="2412", force_scan=True)
  887. 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"):
  888. raise Exception("ANQP_GET command failed")
  889. ev = dev[1].wait_event(["GAS-QUERY-DONE"], timeout=10)
  890. if ev is None:
  891. raise Exception("GAS query timed out")
  892. bss = dev[1].get_bss(bssid)
  893. if 'anqp[265]' not in bss:
  894. raise Exception("AP Geospatial Location ANQP-element not seen")
  895. if bss['anqp[265]'] != geo_loc:
  896. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  897. if 'anqp[266]' in bss:
  898. raise Exception("AP Civic Location ANQP-element unexpectedly seen")
  899. if 'anqp[267]' not in bss:
  900. raise Exception("AP Location Public Identifier ANQP-element not seen")
  901. if bss['anqp[267]'] != public_id:
  902. raise Exception("Unexpected AP Location Public Identifier ANQP-element value: " + bss['anqp[267]'])
  903. if 'anqp[275]' not in bss:
  904. raise Exception("ANQP-element Info ID 275 not seen")
  905. if bss['anqp[275]'] != "01020304":
  906. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  907. if 'anqp[299]' not in bss:
  908. raise Exception("ANQP-element Info ID 299 not seen")
  909. if bss['anqp[299]'] != "0102":
  910. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  911. if 'anqp_ip_addr_type_availability' not in bss:
  912. raise Exception("ANQP-element Info ID 292 not seen")
  913. if bss['anqp_ip_addr_type_availability'] != "1122334455":
  914. raise Exception("Unexpected AP ANQP-element Info ID 262 value: " + bss['anqp_ip_addr_type_availability'])
  915. def test_gas_anqp_address3_not_assoc(dev, apdev, params):
  916. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when not associated"""
  917. try:
  918. _test_gas_anqp_address3_not_assoc(dev, apdev, params)
  919. finally:
  920. dev[0].request("SET gas_address3 0")
  921. def _test_gas_anqp_address3_not_assoc(dev, apdev, params):
  922. hapd = start_ap(apdev[0])
  923. bssid = apdev[0]['bssid']
  924. if "OK" not in dev[0].request("SET gas_address3 1"):
  925. raise Exception("Failed to set gas_address3")
  926. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  927. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  928. raise Exception("ANQP_GET command failed")
  929. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  930. if ev is None:
  931. raise Exception("GAS query start timed out")
  932. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  933. if ev is None:
  934. raise Exception("GAS query timed out")
  935. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  936. if ev is None or "Venue Name" not in ev:
  937. raise Exception("Did not receive Venue Name")
  938. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  939. if ev is None:
  940. raise Exception("ANQP-QUERY-DONE event not seen")
  941. if "result=SUCCESS" not in ev:
  942. raise Exception("Unexpected result: " + ev)
  943. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  944. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  945. display=["wlan.bssid"])
  946. res = out.splitlines()
  947. if len(res) != 2:
  948. raise Exception("Unexpected number of GAS frames")
  949. if res[0] != 'ff:ff:ff:ff:ff:ff':
  950. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  951. if res[1] != 'ff:ff:ff:ff:ff:ff':
  952. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  953. def test_gas_anqp_address3_assoc(dev, apdev, params):
  954. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when associated"""
  955. try:
  956. _test_gas_anqp_address3_assoc(dev, apdev, params)
  957. finally:
  958. dev[0].request("SET gas_address3 0")
  959. def _test_gas_anqp_address3_assoc(dev, apdev, params):
  960. hapd = start_ap(apdev[0])
  961. bssid = apdev[0]['bssid']
  962. if "OK" not in dev[0].request("SET gas_address3 1"):
  963. raise Exception("Failed to set gas_address3")
  964. dev[0].scan_for_bss(bssid, freq="2412")
  965. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  966. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  967. password="password", phase2="auth=MSCHAPV2",
  968. ca_cert="auth_serv/ca.pem", scan_freq="2412")
  969. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  970. raise Exception("ANQP_GET command failed")
  971. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  972. if ev is None:
  973. raise Exception("GAS query start timed out")
  974. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  975. if ev is None:
  976. raise Exception("GAS query timed out")
  977. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  978. if ev is None or "Venue Name" not in ev:
  979. raise Exception("Did not receive Venue Name")
  980. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  981. if ev is None:
  982. raise Exception("ANQP-QUERY-DONE event not seen")
  983. if "result=SUCCESS" not in ev:
  984. raise Exception("Unexpected result: " + ev)
  985. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  986. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  987. display=["wlan.bssid"])
  988. res = out.splitlines()
  989. if len(res) != 2:
  990. raise Exception("Unexpected number of GAS frames")
  991. if res[0] != bssid:
  992. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  993. if res[1] != bssid:
  994. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  995. def test_gas_anqp_address3_ap_forced(dev, apdev, params):
  996. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value on AP"""
  997. hapd = start_ap(apdev[0])
  998. bssid = apdev[0]['bssid']
  999. hapd.set("gas_address3", "1")
  1000. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1001. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1002. raise Exception("ANQP_GET command failed")
  1003. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1004. if ev is None:
  1005. raise Exception("GAS query start timed out")
  1006. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1007. if ev is None:
  1008. raise Exception("GAS query timed out")
  1009. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1010. if ev is None or "Venue Name" not in ev:
  1011. raise Exception("Did not receive Venue Name")
  1012. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1013. if ev is None:
  1014. raise Exception("ANQP-QUERY-DONE event not seen")
  1015. if "result=SUCCESS" not in ev:
  1016. raise Exception("Unexpected result: " + ev)
  1017. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1018. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1019. display=["wlan.bssid"])
  1020. res = out.splitlines()
  1021. if len(res) != 2:
  1022. raise Exception("Unexpected number of GAS frames")
  1023. if res[0] != bssid:
  1024. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1025. if res[1] != 'ff:ff:ff:ff:ff:ff':
  1026. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1027. def test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1028. """GAS/ANQP query using IEEE 802.11 non-compliant Address 3 (AP)"""
  1029. try:
  1030. _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params)
  1031. finally:
  1032. dev[0].request("SET gas_address3 0")
  1033. def _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1034. hapd = start_ap(apdev[0])
  1035. bssid = apdev[0]['bssid']
  1036. hapd.set("gas_address3", "2")
  1037. if "OK" not in dev[0].request("SET gas_address3 1"):
  1038. raise Exception("Failed to set gas_address3")
  1039. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1040. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1041. raise Exception("ANQP_GET command failed")
  1042. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1043. if ev is None:
  1044. raise Exception("GAS query start timed out")
  1045. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1046. if ev is None:
  1047. raise Exception("GAS query timed out")
  1048. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1049. if ev is None or "Venue Name" not in ev:
  1050. raise Exception("Did not receive Venue Name")
  1051. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1052. if ev is None:
  1053. raise Exception("ANQP-QUERY-DONE event not seen")
  1054. if "result=SUCCESS" not in ev:
  1055. raise Exception("Unexpected result: " + ev)
  1056. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1057. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1058. display=["wlan.bssid"])
  1059. res = out.splitlines()
  1060. if len(res) != 2:
  1061. raise Exception("Unexpected number of GAS frames")
  1062. if res[0] != 'ff:ff:ff:ff:ff:ff':
  1063. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1064. if res[1] != bssid:
  1065. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1066. def test_gas_prot_vs_not_prot(dev, apdev, params):
  1067. """GAS/ANQP query protected vs. not protected"""
  1068. hapd = start_ap(apdev[0])
  1069. bssid = apdev[0]['bssid']
  1070. dev[0].scan_for_bss(bssid, freq="2412")
  1071. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  1072. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  1073. password="password", phase2="auth=MSCHAPV2",
  1074. ca_cert="auth_serv/ca.pem", scan_freq="2412",
  1075. ieee80211w="2")
  1076. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1077. raise Exception("ANQP_GET command failed")
  1078. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1079. if ev is None:
  1080. raise Exception("No GAS-QUERY-DONE event")
  1081. if "result=SUCCESS" not in ev:
  1082. raise Exception("Unexpected GAS result: " + ev)
  1083. # GAS: Drop unexpected unprotected GAS frame when PMF is enabled
  1084. dev[0].request("SET ext_mgmt_frame_handling 1")
  1085. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1086. dev[0].request("SET ext_mgmt_frame_handling 0")
  1087. if "OK" not in res:
  1088. raise Exception("MGMT_RX_PROCESS failed")
  1089. dev[0].request("DISCONNECT")
  1090. dev[0].wait_disconnected()
  1091. # GAS: No pending query found for 02:00:00:00:03:00 dialog token 0
  1092. dev[0].request("SET ext_mgmt_frame_handling 1")
  1093. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1094. dev[0].request("SET ext_mgmt_frame_handling 0")
  1095. if "OK" not in res:
  1096. raise Exception("MGMT_RX_PROCESS failed")
  1097. # GAS: Drop unexpected protected GAS frame when PMF is disabled
  1098. dev[0].request("SET ext_mgmt_frame_handling 1")
  1099. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000090b00000005006c027f000000")
  1100. dev[0].request("SET ext_mgmt_frame_handling 0")
  1101. if "OK" not in res:
  1102. raise Exception("MGMT_RX_PROCESS failed")
  1103. def test_gas_failures(dev, apdev):
  1104. """GAS failure cases"""
  1105. hapd = start_ap(apdev[0])
  1106. hapd.set("gas_comeback_delay", "5")
  1107. bssid = apdev[0]['bssid']
  1108. hapd2 = start_ap(apdev[1])
  1109. bssid2 = apdev[1]['bssid']
  1110. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1111. dev[0].scan_for_bss(bssid2, freq="2412")
  1112. tests = [ (bssid, "gas_build_req;gas_query_tx_comeback_req"),
  1113. (bssid, "gas_query_tx;gas_query_tx_comeback_req"),
  1114. (bssid, "gas_query_append;gas_query_rx_comeback"),
  1115. (bssid2, "gas_query_append;gas_query_rx_initial"),
  1116. (bssid2, "wpabuf_alloc_copy;gas_query_rx_initial"),
  1117. (bssid, "gas_query_tx;gas_query_tx_initial_req") ]
  1118. for addr,func in tests:
  1119. with alloc_fail(dev[0], 1, func):
  1120. dev[0].request("ANQP_GET " + addr + " 258")
  1121. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1122. if ev is None:
  1123. raise Exception("No GAS-QUERY-DONE seen")
  1124. if "result=INTERNAL_ERROR" not in ev:
  1125. raise Exception("Unexpected result code: " + ev)
  1126. dev[0].dump_monitor()
  1127. tests = [ "=gas_query_req", "radio_add_work;gas_query_req" ]
  1128. for func in tests:
  1129. with alloc_fail(dev[0], 1, func):
  1130. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1131. raise Exception("ANQP_GET succeeded unexpectedly during OOM")
  1132. dev[0].dump_monitor()
  1133. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  1134. wpas.interface_add("wlan5")
  1135. wpas.scan_for_bss(bssid2, freq="2412")
  1136. wpas.request("SET preassoc_mac_addr 1111")
  1137. wpas.request("ANQP_GET " + bssid2 + " 258")
  1138. ev = wpas.wait_event(["Failed to assign random MAC address for GAS"],
  1139. timeout=5)
  1140. wpas.request("SET preassoc_mac_addr 0")
  1141. if ev is None:
  1142. raise Exception("No random MAC address error seen")