test_gas.py 62 KB

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