test_gas.py 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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_delete_at_deinit(dev, apdev):
  737. """GAS query deleted at deinit"""
  738. hapd = start_ap(apdev[0])
  739. hapd.set("gas_comeback_delay", "1000")
  740. bssid = apdev[0]['bssid']
  741. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  742. wpas.interface_add("wlan5")
  743. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  744. wpas.request("ANQP_GET " + bssid + " 258")
  745. wpas.global_request("INTERFACE_REMOVE " + wpas.ifname)
  746. ev = wpas.wait_event(["GAS-QUERY-DONE"], timeout=2)
  747. del wpas
  748. if ev is None:
  749. raise Exception("GAS-QUERY-DONE not seen")
  750. if "result=DELETED_AT_DEINIT" not in ev:
  751. raise Exception("Unexpected result code: " + ev)
  752. def test_gas_missing_payload(dev, apdev):
  753. """No action code in the query frame"""
  754. bssid = apdev[0]['bssid']
  755. params = hs20_ap_params()
  756. params['hessid'] = bssid
  757. hostapd.add_ap(apdev[0], params)
  758. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  759. cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
  760. if "FAIL" in dev[0].request(cmd):
  761. raise Exception("Could not send test Action frame")
  762. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  763. if ev is None:
  764. raise Exception("Timeout on MGMT-TX-STATUS")
  765. if "result=SUCCESS" not in ev:
  766. raise Exception("AP did not ack Action frame")
  767. cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
  768. if "FAIL" in dev[0].request(cmd):
  769. raise Exception("Could not send test Action frame")
  770. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  771. if ev is None:
  772. raise Exception("Timeout on MGMT-TX-STATUS")
  773. if "result=SUCCESS" not in ev:
  774. raise Exception("AP did not ack Action frame")
  775. def test_gas_query_deinit(dev, apdev):
  776. """Pending GAS/ANQP query during deinit"""
  777. hapd = start_ap(apdev[0])
  778. bssid = apdev[0]['bssid']
  779. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  780. wpas.interface_add("wlan5")
  781. wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
  782. id = wpas.request("RADIO_WORK add block-work")
  783. if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
  784. raise Exception("ANQP_GET command failed")
  785. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  786. if ev is None:
  787. raise Exception("Timeout while waiting radio work to start")
  788. ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
  789. if ev is None:
  790. raise Exception("Timeout while waiting radio work to start (2)")
  791. # Remove the interface while the gas-query radio work is still pending and
  792. # GAS query has not yet been started.
  793. wpas.interface_remove("wlan5")
  794. def test_gas_anqp_oom_wpas(dev, apdev):
  795. """GAS/ANQP query and OOM in wpa_supplicant"""
  796. hapd = start_ap(apdev[0])
  797. bssid = apdev[0]['bssid']
  798. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  799. with alloc_fail(dev[0], 1, "wpa_bss_anqp_alloc"):
  800. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  801. raise Exception("ANQP_GET command failed")
  802. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
  803. if ev is None:
  804. raise Exception("ANQP query did not complete")
  805. with alloc_fail(dev[0], 1, "gas_build_req"):
  806. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  807. raise Exception("Unexpected ANQP_GET command success (OOM)")
  808. def test_gas_anqp_oom_hapd(dev, apdev):
  809. """GAS/ANQP query and OOM in hostapd"""
  810. hapd = start_ap(apdev[0])
  811. bssid = apdev[0]['bssid']
  812. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  813. with alloc_fail(hapd, 1, "gas_build_resp"):
  814. # This query will time out due to the AP not sending a response (OOM).
  815. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  816. raise Exception("ANQP_GET command failed")
  817. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  818. if ev is None:
  819. raise Exception("GAS query start timed out")
  820. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  821. if ev is None:
  822. raise Exception("GAS query timed out")
  823. if "result=TIMEOUT" not in ev:
  824. raise Exception("Unexpected result: " + ev)
  825. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  826. if ev is None:
  827. raise Exception("ANQP-QUERY-DONE event not seen")
  828. if "result=FAILURE" not in ev:
  829. raise Exception("Unexpected result: " + ev)
  830. with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
  831. hapd.set("gas_frag_limit", "50")
  832. # The first attempt of this query will time out due to the AP not
  833. # sending a response (OOM), but the retry succeeds.
  834. dev[0].request("FETCH_ANQP")
  835. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  836. if ev is None:
  837. raise Exception("GAS query start timed out")
  838. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  839. if ev is None:
  840. raise Exception("GAS query timed out")
  841. if "result=SUCCESS" not in ev:
  842. raise Exception("Unexpected result: " + ev)
  843. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  844. if ev is None:
  845. raise Exception("ANQP-QUERY-DONE event not seen")
  846. if "result=SUCCESS" not in ev:
  847. raise Exception("Unexpected result: " + ev)
  848. def test_gas_anqp_extra_elements(dev, apdev):
  849. """GAS/ANQP and extra ANQP elements"""
  850. geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100"
  851. civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5"
  852. held_uri = "https://held.example.com/location"
  853. held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri
  854. supl_fqdn = "supl.example.com"
  855. supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn
  856. public_id = binascii.hexlify(held + supl)
  857. params = { "ssid": "gas/anqp",
  858. "interworking": "1",
  859. "anqp_elem": [ "265:" + geo_loc,
  860. "266:" + civic_loc,
  861. "262:1122334455",
  862. "267:" + public_id,
  863. "275:01020304",
  864. "60000:01",
  865. "299:0102" ] }
  866. hapd = hostapd.add_ap(apdev[0], params)
  867. bssid = apdev[0]['bssid']
  868. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  869. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 265,266"):
  870. raise Exception("ANQP_GET command failed")
  871. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  872. if ev is None:
  873. raise Exception("GAS query timed out")
  874. bss = dev[0].get_bss(bssid)
  875. if 'anqp[265]' not in bss:
  876. raise Exception("AP Geospatial Location ANQP-element not seen")
  877. if bss['anqp[265]'] != geo_loc:
  878. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  879. if 'anqp[266]' not in bss:
  880. raise Exception("AP Civic Location ANQP-element not seen")
  881. if bss['anqp[266]'] != civic_loc:
  882. raise Exception("Unexpected AP Civic Location ANQP-element value: " + bss['anqp[266]'])
  883. dev[1].scan_for_bss(bssid, freq="2412", force_scan=True)
  884. 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"):
  885. raise Exception("ANQP_GET command failed")
  886. ev = dev[1].wait_event(["GAS-QUERY-DONE"], timeout=10)
  887. if ev is None:
  888. raise Exception("GAS query timed out")
  889. bss = dev[1].get_bss(bssid)
  890. if 'anqp[265]' not in bss:
  891. raise Exception("AP Geospatial Location ANQP-element not seen")
  892. if bss['anqp[265]'] != geo_loc:
  893. raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
  894. if 'anqp[266]' in bss:
  895. raise Exception("AP Civic Location ANQP-element unexpectedly seen")
  896. if 'anqp[267]' not in bss:
  897. raise Exception("AP Location Public Identifier ANQP-element not seen")
  898. if bss['anqp[267]'] != public_id:
  899. raise Exception("Unexpected AP Location Public Identifier ANQP-element value: " + bss['anqp[267]'])
  900. if 'anqp[275]' not in bss:
  901. raise Exception("ANQP-element Info ID 275 not seen")
  902. if bss['anqp[275]'] != "01020304":
  903. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  904. if 'anqp[299]' not in bss:
  905. raise Exception("ANQP-element Info ID 299 not seen")
  906. if bss['anqp[299]'] != "0102":
  907. raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
  908. if 'anqp_ip_addr_type_availability' not in bss:
  909. raise Exception("ANQP-element Info ID 292 not seen")
  910. if bss['anqp_ip_addr_type_availability'] != "1122334455":
  911. raise Exception("Unexpected AP ANQP-element Info ID 262 value: " + bss['anqp_ip_addr_type_availability'])
  912. def test_gas_anqp_address3_not_assoc(dev, apdev, params):
  913. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when not associated"""
  914. try:
  915. _test_gas_anqp_address3_not_assoc(dev, apdev, params)
  916. finally:
  917. dev[0].request("SET gas_address3 0")
  918. def _test_gas_anqp_address3_not_assoc(dev, apdev, params):
  919. hapd = start_ap(apdev[0])
  920. bssid = apdev[0]['bssid']
  921. if "OK" not in dev[0].request("SET gas_address3 1"):
  922. raise Exception("Failed to set gas_address3")
  923. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  924. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  925. raise Exception("ANQP_GET command failed")
  926. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  927. if ev is None:
  928. raise Exception("GAS query start timed out")
  929. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  930. if ev is None:
  931. raise Exception("GAS query timed out")
  932. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  933. if ev is None or "Venue Name" not in ev:
  934. raise Exception("Did not receive Venue Name")
  935. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  936. if ev is None:
  937. raise Exception("ANQP-QUERY-DONE event not seen")
  938. if "result=SUCCESS" not in ev:
  939. raise Exception("Unexpected result: " + ev)
  940. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  941. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  942. display=["wlan.bssid"])
  943. res = out.splitlines()
  944. if len(res) != 2:
  945. raise Exception("Unexpected number of GAS frames")
  946. if res[0] != 'ff:ff:ff:ff:ff:ff':
  947. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  948. if res[1] != 'ff:ff:ff:ff:ff:ff':
  949. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  950. def test_gas_anqp_address3_assoc(dev, apdev, params):
  951. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when associated"""
  952. try:
  953. _test_gas_anqp_address3_assoc(dev, apdev, params)
  954. finally:
  955. dev[0].request("SET gas_address3 0")
  956. def _test_gas_anqp_address3_assoc(dev, apdev, params):
  957. hapd = start_ap(apdev[0])
  958. bssid = apdev[0]['bssid']
  959. if "OK" not in dev[0].request("SET gas_address3 1"):
  960. raise Exception("Failed to set gas_address3")
  961. dev[0].scan_for_bss(bssid, freq="2412")
  962. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  963. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  964. password="password", phase2="auth=MSCHAPV2",
  965. ca_cert="auth_serv/ca.pem", scan_freq="2412")
  966. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  967. raise Exception("ANQP_GET command failed")
  968. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  969. if ev is None:
  970. raise Exception("GAS query start timed out")
  971. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  972. if ev is None:
  973. raise Exception("GAS query timed out")
  974. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  975. if ev is None or "Venue Name" not in ev:
  976. raise Exception("Did not receive Venue Name")
  977. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  978. if ev is None:
  979. raise Exception("ANQP-QUERY-DONE event not seen")
  980. if "result=SUCCESS" not in ev:
  981. raise Exception("Unexpected result: " + ev)
  982. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  983. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  984. display=["wlan.bssid"])
  985. res = out.splitlines()
  986. if len(res) != 2:
  987. raise Exception("Unexpected number of GAS frames")
  988. if res[0] != bssid:
  989. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  990. if res[1] != bssid:
  991. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  992. def test_gas_anqp_address3_ap_forced(dev, apdev, params):
  993. """GAS/ANQP query using IEEE 802.11 compliant Address 3 value on AP"""
  994. hapd = start_ap(apdev[0])
  995. bssid = apdev[0]['bssid']
  996. hapd.set("gas_address3", "1")
  997. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  998. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  999. raise Exception("ANQP_GET command failed")
  1000. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1001. if ev is None:
  1002. raise Exception("GAS query start timed out")
  1003. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1004. if ev is None:
  1005. raise Exception("GAS query timed out")
  1006. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1007. if ev is None or "Venue Name" not in ev:
  1008. raise Exception("Did not receive Venue Name")
  1009. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1010. if ev is None:
  1011. raise Exception("ANQP-QUERY-DONE event not seen")
  1012. if "result=SUCCESS" not in ev:
  1013. raise Exception("Unexpected result: " + ev)
  1014. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1015. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1016. display=["wlan.bssid"])
  1017. res = out.splitlines()
  1018. if len(res) != 2:
  1019. raise Exception("Unexpected number of GAS frames")
  1020. if res[0] != bssid:
  1021. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1022. if res[1] != 'ff:ff:ff:ff:ff:ff':
  1023. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1024. def test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1025. """GAS/ANQP query using IEEE 802.11 non-compliant Address 3 (AP)"""
  1026. try:
  1027. _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params)
  1028. finally:
  1029. dev[0].request("SET gas_address3 0")
  1030. def _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
  1031. hapd = start_ap(apdev[0])
  1032. bssid = apdev[0]['bssid']
  1033. hapd.set("gas_address3", "2")
  1034. if "OK" not in dev[0].request("SET gas_address3 1"):
  1035. raise Exception("Failed to set gas_address3")
  1036. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1037. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1038. raise Exception("ANQP_GET command failed")
  1039. ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
  1040. if ev is None:
  1041. raise Exception("GAS query start timed out")
  1042. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
  1043. if ev is None:
  1044. raise Exception("GAS query timed out")
  1045. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  1046. if ev is None or "Venue Name" not in ev:
  1047. raise Exception("Did not receive Venue Name")
  1048. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  1049. if ev is None:
  1050. raise Exception("ANQP-QUERY-DONE event not seen")
  1051. if "result=SUCCESS" not in ev:
  1052. raise Exception("Unexpected result: " + ev)
  1053. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  1054. "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
  1055. display=["wlan.bssid"])
  1056. res = out.splitlines()
  1057. if len(res) != 2:
  1058. raise Exception("Unexpected number of GAS frames")
  1059. if res[0] != 'ff:ff:ff:ff:ff:ff':
  1060. raise Exception("GAS request used unexpected Address3 field value: " + res[0])
  1061. if res[1] != bssid:
  1062. raise Exception("GAS response used unexpected Address3 field value: " + res[1])
  1063. def test_gas_prot_vs_not_prot(dev, apdev, params):
  1064. """GAS/ANQP query protected vs. not protected"""
  1065. hapd = start_ap(apdev[0])
  1066. bssid = apdev[0]['bssid']
  1067. dev[0].scan_for_bss(bssid, freq="2412")
  1068. dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
  1069. identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
  1070. password="password", phase2="auth=MSCHAPV2",
  1071. ca_cert="auth_serv/ca.pem", scan_freq="2412",
  1072. ieee80211w="2")
  1073. if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1074. raise Exception("ANQP_GET command failed")
  1075. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1076. if ev is None:
  1077. raise Exception("No GAS-QUERY-DONE event")
  1078. if "result=SUCCESS" not in ev:
  1079. raise Exception("Unexpected GAS result: " + ev)
  1080. # GAS: Drop unexpected unprotected GAS frame when PMF is enabled
  1081. dev[0].request("SET ext_mgmt_frame_handling 1")
  1082. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1083. dev[0].request("SET ext_mgmt_frame_handling 0")
  1084. if "OK" not in res:
  1085. raise Exception("MGMT_RX_PROCESS failed")
  1086. dev[0].request("DISCONNECT")
  1087. dev[0].wait_disconnected()
  1088. # GAS: No pending query found for 02:00:00:00:03:00 dialog token 0
  1089. dev[0].request("SET ext_mgmt_frame_handling 1")
  1090. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
  1091. dev[0].request("SET ext_mgmt_frame_handling 0")
  1092. if "OK" not in res:
  1093. raise Exception("MGMT_RX_PROCESS failed")
  1094. # GAS: Drop unexpected protected GAS frame when PMF is disabled
  1095. dev[0].request("SET ext_mgmt_frame_handling 1")
  1096. res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000090b00000005006c027f000000")
  1097. dev[0].request("SET ext_mgmt_frame_handling 0")
  1098. if "OK" not in res:
  1099. raise Exception("MGMT_RX_PROCESS failed")
  1100. def test_gas_failures(dev, apdev):
  1101. """GAS failure cases"""
  1102. hapd = start_ap(apdev[0])
  1103. hapd.set("gas_comeback_delay", "5")
  1104. bssid = apdev[0]['bssid']
  1105. hapd2 = start_ap(apdev[1])
  1106. bssid2 = apdev[1]['bssid']
  1107. dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
  1108. dev[0].scan_for_bss(bssid2, freq="2412")
  1109. tests = [ (bssid, "gas_build_req;gas_query_tx_comeback_req"),
  1110. (bssid, "gas_query_tx;gas_query_tx_comeback_req"),
  1111. (bssid, "gas_query_append;gas_query_rx_comeback"),
  1112. (bssid2, "gas_query_append;gas_query_rx_initial"),
  1113. (bssid2, "wpabuf_alloc_copy;gas_query_rx_initial"),
  1114. (bssid, "gas_query_tx;gas_query_tx_initial_req") ]
  1115. for addr,func in tests:
  1116. with alloc_fail(dev[0], 1, func):
  1117. dev[0].request("ANQP_GET " + addr + " 258")
  1118. ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
  1119. if ev is None:
  1120. raise Exception("No GAS-QUERY-DONE seen")
  1121. if "result=INTERNAL_ERROR" not in ev:
  1122. raise Exception("Unexpected result code: " + ev)
  1123. dev[0].dump_monitor()
  1124. tests = [ "=gas_query_req", "radio_add_work;gas_query_req" ]
  1125. for func in tests:
  1126. with alloc_fail(dev[0], 1, func):
  1127. if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
  1128. raise Exception("ANQP_GET succeeded unexpectedly during OOM")
  1129. dev[0].dump_monitor()
  1130. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  1131. wpas.interface_add("wlan5")
  1132. wpas.scan_for_bss(bssid2, freq="2412")
  1133. wpas.request("SET preassoc_mac_addr 1111")
  1134. wpas.request("ANQP_GET " + bssid2 + " 258")
  1135. ev = wpas.wait_event(["Failed to assign random MAC address for GAS"],
  1136. timeout=5)
  1137. wpas.request("SET preassoc_mac_addr 0")
  1138. if ev is None:
  1139. raise Exception("No random MAC address error seen")