test_scan.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. # Scanning tests
  2. # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. from remotehost import remote_compatible
  7. import time
  8. import logging
  9. logger = logging.getLogger()
  10. import os
  11. import subprocess
  12. import hostapd
  13. from wpasupplicant import WpaSupplicant
  14. from utils import HwsimSkip, fail_test, alloc_fail, wait_fail_trigger
  15. from tshark import run_tshark
  16. def check_scan(dev, params, other_started=False, test_busy=False):
  17. if not other_started:
  18. dev.dump_monitor()
  19. id = dev.request("SCAN " + params)
  20. if "FAIL" in id:
  21. raise Exception("Failed to start scan")
  22. id = int(id)
  23. if test_busy:
  24. if "FAIL-BUSY" not in dev.request("SCAN"):
  25. raise Exception("SCAN command while already scanning not rejected")
  26. if other_started:
  27. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  28. if ev is None:
  29. raise Exception("Other scan did not start")
  30. if "id=" + str(id) in ev:
  31. raise Exception("Own scan id unexpectedly included in start event")
  32. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  33. if ev is None:
  34. raise Exception("Other scan did not complete")
  35. if "id=" + str(id) in ev:
  36. raise Exception("Own scan id unexpectedly included in completed event")
  37. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  38. if ev is None:
  39. raise Exception("Scan did not start")
  40. if "id=" + str(id) not in ev:
  41. raise Exception("Scan id not included in start event")
  42. if test_busy:
  43. if "FAIL-BUSY" not in dev.request("SCAN"):
  44. raise Exception("SCAN command while already scanning not rejected")
  45. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  46. if ev is None:
  47. raise Exception("Scan did not complete")
  48. if "id=" + str(id) not in ev:
  49. raise Exception("Scan id not included in completed event")
  50. def check_scan_retry(dev, params, bssid):
  51. for i in range(0, 5):
  52. check_scan(dev, "freq=2412-2462,5180 use_id=1")
  53. if int(dev.get_bss(bssid)['age']) <= 1:
  54. return
  55. raise Exception("Unexpectedly old BSS entry")
  56. @remote_compatible
  57. def test_scan(dev, apdev):
  58. """Control interface behavior on scan parameters"""
  59. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  60. bssid = apdev[0]['bssid']
  61. logger.info("Full scan")
  62. check_scan(dev[0], "use_id=1", test_busy=True)
  63. logger.info("Limited channel scan")
  64. check_scan_retry(dev[0], "freq=2412-2462,5180 use_id=1", bssid)
  65. # wait long enough to allow next scans to be verified not to find the AP
  66. time.sleep(2)
  67. logger.info("Passive single-channel scan")
  68. check_scan(dev[0], "freq=2457 passive=1 use_id=1")
  69. logger.info("Active single-channel scan")
  70. check_scan(dev[0], "freq=2452 passive=0 use_id=1")
  71. if int(dev[0].get_bss(bssid)['age']) < 2:
  72. raise Exception("Unexpectedly updated BSS entry")
  73. logger.info("Active single-channel scan on AP's operating channel")
  74. check_scan_retry(dev[0], "freq=2412 passive=0 use_id=1", bssid)
  75. @remote_compatible
  76. def test_scan_tsf(dev, apdev):
  77. """Scan and TSF updates from Beacon/Probe Response frames"""
  78. hostapd.add_ap(apdev[0], { "ssid": "test-scan",
  79. 'beacon_int': "100" })
  80. bssid = apdev[0]['bssid']
  81. tsf = []
  82. for passive in [ 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1 ]:
  83. check_scan(dev[0], "freq=2412 passive=%d use_id=1" % passive)
  84. bss = dev[0].get_bss(bssid)
  85. if bss:
  86. tsf.append(int(bss['tsf']))
  87. logger.info("TSF: " + bss['tsf'])
  88. if tsf[-3] <= tsf[-4]:
  89. # For now, only write this in the log without failing the test case
  90. # since mac80211_hwsim does not yet update the Timestamp field in
  91. # Probe Response frames.
  92. logger.info("Probe Response did not update TSF")
  93. #raise Exception("Probe Response did not update TSF")
  94. if tsf[-1] <= tsf[-3]:
  95. raise Exception("Beacon did not update TSF")
  96. if 0 in tsf:
  97. raise Exception("0 TSF reported")
  98. @remote_compatible
  99. def test_scan_only(dev, apdev):
  100. """Control interface behavior on scan parameters with type=only"""
  101. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  102. bssid = apdev[0]['bssid']
  103. logger.info("Full scan")
  104. check_scan(dev[0], "type=only use_id=1")
  105. logger.info("Limited channel scan")
  106. check_scan_retry(dev[0], "type=only freq=2412-2462,5180 use_id=1", bssid)
  107. # wait long enough to allow next scans to be verified not to find the AP
  108. time.sleep(2)
  109. logger.info("Passive single-channel scan")
  110. check_scan(dev[0], "type=only freq=2457 passive=1 use_id=1")
  111. logger.info("Active single-channel scan")
  112. check_scan(dev[0], "type=only freq=2452 passive=0 use_id=1")
  113. if int(dev[0].get_bss(bssid)['age']) < 2:
  114. raise Exception("Unexpectedly updated BSS entry")
  115. logger.info("Active single-channel scan on AP's operating channel")
  116. check_scan_retry(dev[0], "type=only freq=2412 passive=0 use_id=1", bssid)
  117. @remote_compatible
  118. def test_scan_external_trigger(dev, apdev):
  119. """Avoid operations during externally triggered scan"""
  120. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  121. bssid = apdev[0]['bssid']
  122. dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger'])
  123. check_scan(dev[0], "use_id=1", other_started=True)
  124. def test_scan_bss_expiration_count(dev, apdev):
  125. """BSS entry expiration based on scan results without match"""
  126. if "FAIL" not in dev[0].request("BSS_EXPIRE_COUNT 0"):
  127. raise Exception("Invalid BSS_EXPIRE_COUNT accepted")
  128. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  129. raise Exception("BSS_EXPIRE_COUNT failed")
  130. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  131. bssid = apdev[0]['bssid']
  132. dev[0].scan(freq="2412", only_new=True)
  133. if bssid not in dev[0].request("SCAN_RESULTS"):
  134. raise Exception("BSS not found in initial scan")
  135. hapd.request("DISABLE")
  136. dev[0].scan(freq="2412", only_new=True)
  137. if bssid not in dev[0].request("SCAN_RESULTS"):
  138. raise Exception("BSS not found in first scan without match")
  139. dev[0].scan(freq="2412", only_new=True)
  140. if bssid in dev[0].request("SCAN_RESULTS"):
  141. raise Exception("BSS found after two scans without match")
  142. @remote_compatible
  143. def test_scan_bss_expiration_age(dev, apdev):
  144. """BSS entry expiration based on age"""
  145. try:
  146. if "FAIL" not in dev[0].request("BSS_EXPIRE_AGE COUNT 9"):
  147. raise Exception("Invalid BSS_EXPIRE_AGE accepted")
  148. if "OK" not in dev[0].request("BSS_EXPIRE_AGE 10"):
  149. raise Exception("BSS_EXPIRE_AGE failed")
  150. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  151. bssid = apdev[0]['bssid']
  152. # Allow couple more retries to avoid reporting errors during heavy load
  153. for i in range(5):
  154. dev[0].scan(freq="2412")
  155. if bssid in dev[0].request("SCAN_RESULTS"):
  156. break
  157. if bssid not in dev[0].request("SCAN_RESULTS"):
  158. raise Exception("BSS not found in initial scan")
  159. hapd.request("DISABLE")
  160. logger.info("Waiting for BSS entry to expire")
  161. time.sleep(7)
  162. if bssid not in dev[0].request("SCAN_RESULTS"):
  163. raise Exception("BSS expired too quickly")
  164. ev = dev[0].wait_event(["CTRL-EVENT-BSS-REMOVED"], timeout=15)
  165. if ev is None:
  166. raise Exception("BSS entry expiration timed out")
  167. if bssid in dev[0].request("SCAN_RESULTS"):
  168. raise Exception("BSS not removed after expiration time")
  169. finally:
  170. dev[0].request("BSS_EXPIRE_AGE 180")
  171. @remote_compatible
  172. def test_scan_filter(dev, apdev):
  173. """Filter scan results based on SSID"""
  174. try:
  175. if "OK" not in dev[0].request("SET filter_ssids 1"):
  176. raise Exception("SET failed")
  177. id = dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  178. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  179. bssid = apdev[0]['bssid']
  180. hostapd.add_ap(apdev[1], { "ssid": "test-scan2" })
  181. bssid2 = apdev[1]['bssid']
  182. dev[0].scan(freq="2412", only_new=True)
  183. if bssid not in dev[0].request("SCAN_RESULTS"):
  184. raise Exception("BSS not found in scan results")
  185. if bssid2 in dev[0].request("SCAN_RESULTS"):
  186. raise Exception("Unexpected BSS found in scan results")
  187. dev[0].set_network_quoted(id, "ssid", "")
  188. dev[0].scan(freq="2412")
  189. id2 = dev[0].connect("test", key_mgmt="NONE", only_add_network=True)
  190. dev[0].scan(freq="2412")
  191. finally:
  192. dev[0].request("SET filter_ssids 0")
  193. @remote_compatible
  194. def test_scan_int(dev, apdev):
  195. """scan interval configuration"""
  196. try:
  197. if "FAIL" not in dev[0].request("SCAN_INTERVAL -1"):
  198. raise Exception("Accepted invalid scan interval")
  199. if "OK" not in dev[0].request("SCAN_INTERVAL 1"):
  200. raise Exception("Failed to set scan interval")
  201. dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
  202. wait_connect=False)
  203. times = {}
  204. for i in range(0, 3):
  205. logger.info("Waiting for scan to start")
  206. start = os.times()[4]
  207. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  208. if ev is None:
  209. raise Exception("did not start a scan")
  210. stop = os.times()[4]
  211. times[i] = stop - start
  212. logger.info("Waiting for scan to complete")
  213. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  214. if ev is None:
  215. raise Exception("did not complete a scan")
  216. logger.info("times=" + str(times))
  217. if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
  218. raise Exception("Unexpected scan timing: " + str(times))
  219. finally:
  220. dev[0].request("SCAN_INTERVAL 5")
  221. def test_scan_bss_operations(dev, apdev):
  222. """Control interface behavior on BSS parameters"""
  223. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  224. bssid = apdev[0]['bssid']
  225. hostapd.add_ap(apdev[1], { "ssid": "test2-scan" })
  226. bssid2 = apdev[1]['bssid']
  227. dev[0].scan(freq="2412")
  228. dev[0].scan(freq="2412")
  229. dev[0].scan(freq="2412")
  230. id1 = dev[0].request("BSS FIRST MASK=0x1").splitlines()[0].split('=')[1]
  231. id2 = dev[0].request("BSS LAST MASK=0x1").splitlines()[0].split('=')[1]
  232. res = dev[0].request("BSS RANGE=ALL MASK=0x20001")
  233. if "id=" + id1 not in res:
  234. raise Exception("Missing BSS " + id1)
  235. if "id=" + id2 not in res:
  236. raise Exception("Missing BSS " + id2)
  237. if "====" not in res:
  238. raise Exception("Missing delim")
  239. if "####" not in res:
  240. raise Exception("Missing end")
  241. res = dev[0].request("BSS RANGE=ALL MASK=0")
  242. if "id=" + id1 not in res:
  243. raise Exception("Missing BSS " + id1)
  244. if "id=" + id2 not in res:
  245. raise Exception("Missing BSS " + id2)
  246. if "====" in res:
  247. raise Exception("Unexpected delim")
  248. if "####" in res:
  249. raise Exception("Unexpected end delim")
  250. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  251. if len(res) != 2:
  252. raise Exception("Unexpected result: " + str(res))
  253. res = dev[0].request("BSS FIRST MASK=0x1")
  254. if "id=" + id1 not in res:
  255. raise Exception("Unexpected result: " + res)
  256. res = dev[0].request("BSS LAST MASK=0x1")
  257. if "id=" + id2 not in res:
  258. raise Exception("Unexpected result: " + res)
  259. res = dev[0].request("BSS ID-" + id1 + " MASK=0x1")
  260. if "id=" + id1 not in res:
  261. raise Exception("Unexpected result: " + res)
  262. res = dev[0].request("BSS NEXT-" + id1 + " MASK=0x1")
  263. if "id=" + id2 not in res:
  264. raise Exception("Unexpected result: " + res)
  265. res = dev[0].request("BSS NEXT-" + id2 + " MASK=0x1")
  266. if "id=" in res:
  267. raise Exception("Unexpected result: " + res)
  268. if len(dev[0].request("BSS RANGE=" + id2 + " MASK=0x1").splitlines()) != 0:
  269. raise Exception("Unexpected RANGE=1 result")
  270. if len(dev[0].request("BSS RANGE=" + id1 + "- MASK=0x1").splitlines()) != 2:
  271. raise Exception("Unexpected RANGE=0- result")
  272. if len(dev[0].request("BSS RANGE=-" + id2 + " MASK=0x1").splitlines()) != 2:
  273. raise Exception("Unexpected RANGE=-1 result")
  274. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id2 + " MASK=0x1").splitlines()) != 2:
  275. raise Exception("Unexpected RANGE=0-1 result")
  276. if len(dev[0].request("BSS RANGE=" + id2 + "-" + id2 + " MASK=0x1").splitlines()) != 1:
  277. raise Exception("Unexpected RANGE=1-1 result")
  278. if len(dev[0].request("BSS RANGE=" + str(int(id2) + 1) + "-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 0:
  279. raise Exception("Unexpected RANGE=2-10 result")
  280. if len(dev[0].request("BSS RANGE=0-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 2:
  281. raise Exception("Unexpected RANGE=0-10 result")
  282. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id1 + " MASK=0x1").splitlines()) != 1:
  283. raise Exception("Unexpected RANGE=0-0 result")
  284. res = dev[0].request("BSS p2p_dev_addr=FOO")
  285. if "FAIL" in res or "id=" in res:
  286. raise Exception("Unexpected result: " + res)
  287. res = dev[0].request("BSS p2p_dev_addr=00:11:22:33:44:55")
  288. if "FAIL" in res or "id=" in res:
  289. raise Exception("Unexpected result: " + res)
  290. dev[0].request("BSS_FLUSH 1000")
  291. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  292. if len(res) != 2:
  293. raise Exception("Unexpected result after BSS_FLUSH 1000")
  294. dev[0].request("BSS_FLUSH 0")
  295. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  296. if len(res) != 0:
  297. raise Exception("Unexpected result after BSS_FLUSH 0")
  298. @remote_compatible
  299. def test_scan_and_interface_disabled(dev, apdev):
  300. """Scan operation when interface gets disabled"""
  301. try:
  302. dev[0].request("SCAN")
  303. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  304. if ev is None:
  305. raise Exception("Scan did not start")
  306. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  307. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=7)
  308. if ev is not None:
  309. raise Exception("Scan completed unexpectedly")
  310. # verify that scan is rejected
  311. if "FAIL" not in dev[0].request("SCAN"):
  312. raise Exception("New scan request was accepted unexpectedly")
  313. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  314. dev[0].scan(freq="2412")
  315. finally:
  316. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  317. @remote_compatible
  318. def test_scan_for_auth(dev, apdev):
  319. """cfg80211 workaround with scan-for-auth"""
  320. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  321. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  322. # Block sme-connect radio work with an external radio work item, so that
  323. # SELECT_NETWORK can decide to use fast associate without a new scan while
  324. # cfg80211 still has the matching BSS entry, but the actual connection is
  325. # not yet started.
  326. id = dev[0].request("RADIO_WORK add block-work")
  327. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  328. if ev is None:
  329. raise Exception("Timeout while waiting radio work to start")
  330. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  331. wait_connect=False)
  332. dev[0].dump_monitor()
  333. # Clear cfg80211 BSS table.
  334. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger',
  335. 'freq', '2457', 'flush'])
  336. if res != 0:
  337. raise HwsimSkip("iw scan trigger flush not supported")
  338. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  339. if ev is None:
  340. raise Exception("External flush scan timed out")
  341. # Release blocking radio work to allow connection to go through with the
  342. # cfg80211 BSS entry missing.
  343. dev[0].request("RADIO_WORK done " + id)
  344. dev[0].wait_connected(timeout=15)
  345. @remote_compatible
  346. def test_scan_for_auth_fail(dev, apdev):
  347. """cfg80211 workaround with scan-for-auth failing"""
  348. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  349. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  350. # Block sme-connect radio work with an external radio work item, so that
  351. # SELECT_NETWORK can decide to use fast associate without a new scan while
  352. # cfg80211 still has the matching BSS entry, but the actual connection is
  353. # not yet started.
  354. id = dev[0].request("RADIO_WORK add block-work")
  355. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  356. if ev is None:
  357. raise Exception("Timeout while waiting radio work to start")
  358. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  359. wait_connect=False)
  360. dev[0].dump_monitor()
  361. hapd.disable()
  362. # Clear cfg80211 BSS table.
  363. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger',
  364. 'freq', '2457', 'flush'])
  365. if res != 0:
  366. raise HwsimSkip("iw scan trigger flush not supported")
  367. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  368. if ev is None:
  369. raise Exception("External flush scan timed out")
  370. # Release blocking radio work to allow connection to go through with the
  371. # cfg80211 BSS entry missing.
  372. dev[0].request("RADIO_WORK done " + id)
  373. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS",
  374. "CTRL-EVENT-CONNECTED"], 15)
  375. if ev is None:
  376. raise Exception("Scan event missing")
  377. if "CTRL-EVENT-CONNECTED" in ev:
  378. raise Exception("Unexpected connection")
  379. dev[0].request("DISCONNECT")
  380. @remote_compatible
  381. def test_scan_for_auth_wep(dev, apdev):
  382. """cfg80211 scan-for-auth workaround with WEP keys"""
  383. dev[0].flush_scan_cache()
  384. hapd = hostapd.add_ap(apdev[0],
  385. { "ssid": "wep", "wep_key0": '"abcde"',
  386. "auth_algs": "2" })
  387. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  388. # Block sme-connect radio work with an external radio work item, so that
  389. # SELECT_NETWORK can decide to use fast associate without a new scan while
  390. # cfg80211 still has the matching BSS entry, but the actual connection is
  391. # not yet started.
  392. id = dev[0].request("RADIO_WORK add block-work")
  393. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  394. if ev is None:
  395. raise Exception("Timeout while waiting radio work to start")
  396. dev[0].connect("wep", key_mgmt="NONE", wep_key0='"abcde"',
  397. auth_alg="SHARED", scan_freq="2412", wait_connect=False)
  398. dev[0].dump_monitor()
  399. # Clear cfg80211 BSS table.
  400. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger',
  401. 'freq', '2457', 'flush'])
  402. if res != 0:
  403. raise HwsimSkip("iw scan trigger flush not supported")
  404. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  405. if ev is None:
  406. raise Exception("External flush scan timed out")
  407. # Release blocking radio work to allow connection to go through with the
  408. # cfg80211 BSS entry missing.
  409. dev[0].request("RADIO_WORK done " + id)
  410. dev[0].wait_connected(timeout=15)
  411. @remote_compatible
  412. def test_scan_hidden(dev, apdev):
  413. """Control interface behavior on scan parameters"""
  414. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan",
  415. "ignore_broadcast_ssid": "1" })
  416. bssid = apdev[0]['bssid']
  417. check_scan(dev[0], "freq=2412 use_id=1")
  418. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  419. raise Exception("BSS unexpectedly found in initial scan")
  420. id1 = dev[0].connect("foo", key_mgmt="NONE", scan_ssid="1",
  421. only_add_network=True)
  422. id2 = dev[0].connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  423. only_add_network=True)
  424. id3 = dev[0].connect("bar", key_mgmt="NONE", only_add_network=True)
  425. check_scan(dev[0], "freq=2412 use_id=1")
  426. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  427. raise Exception("BSS unexpectedly found in scan")
  428. # Allow multiple attempts to be more robust under heavy CPU load that can
  429. # result in Probe Response frames getting sent only after the station has
  430. # already stopped waiting for the response on the channel.
  431. found = False
  432. for i in range(10):
  433. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id2, id3))
  434. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  435. found = True
  436. break
  437. if not found:
  438. raise Exception("BSS not found in scan")
  439. if "FAIL" not in dev[0].request("SCAN scan_id=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17"):
  440. raise Exception("Too many scan_id values accepted")
  441. # Duplicate SSID removal
  442. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id1, id2))
  443. dev[0].request("REMOVE_NETWORK all")
  444. hapd.disable()
  445. dev[0].flush_scan_cache(freq=2432)
  446. dev[0].flush_scan_cache()
  447. def test_scan_and_bss_entry_removed(dev, apdev):
  448. """Last scan result and connect work processing on BSS entry update"""
  449. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  450. "eap_server": "1",
  451. "wps_state": "2" })
  452. bssid = apdev[0]['bssid']
  453. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  454. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  455. # Add a BSS entry
  456. dev[0].scan_for_bss(bssid, freq="2412")
  457. wpas.scan_for_bss(bssid, freq="2412")
  458. # Start a connect radio work with a blocking entry preventing this from
  459. # proceeding; this stores a pointer to the selected BSS entry.
  460. id = dev[0].request("RADIO_WORK add block-work")
  461. w_id = wpas.request("RADIO_WORK add block-work")
  462. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  463. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  464. nid = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  465. wait_connect=False)
  466. w_nid = wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  467. wait_connect=False)
  468. time.sleep(0.1)
  469. # Remove the BSS entry
  470. dev[0].request("BSS_FLUSH 0")
  471. wpas.request("BSS_FLUSH 0")
  472. # Allow the connect radio work to continue. The bss entry stored in the
  473. # pending connect work is now stale. This will result in the connection
  474. # attempt failing since the BSS entry does not exist.
  475. dev[0].request("RADIO_WORK done " + id)
  476. wpas.request("RADIO_WORK done " + w_id)
  477. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  478. if ev is not None:
  479. raise Exception("Unexpected connection")
  480. dev[0].remove_network(nid)
  481. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  482. if ev is not None:
  483. raise Exception("Unexpected connection")
  484. wpas.remove_network(w_nid)
  485. time.sleep(0.5)
  486. dev[0].request("BSS_FLUSH 0")
  487. wpas.request("BSS_FLUSH 0")
  488. # Add a BSS entry
  489. dev[0].scan_for_bss(bssid, freq="2412")
  490. wpas.scan_for_bss(bssid, freq="2412")
  491. # Start a connect radio work with a blocking entry preventing this from
  492. # proceeding; this stores a pointer to the selected BSS entry.
  493. id = dev[0].request("RADIO_WORK add block-work")
  494. w_id = wpas.request("RADIO_WORK add block-work")
  495. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  496. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  497. # Schedule a connection based on the current BSS entry.
  498. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  499. wait_connect=False)
  500. wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  501. wait_connect=False)
  502. # Update scan results with results that have longer set of IEs so that new
  503. # memory needs to be allocated for the BSS entry.
  504. hapd.request("WPS_PBC")
  505. time.sleep(0.1)
  506. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger', 'freq', '2412'])
  507. subprocess.call(['iw', wpas.ifname, 'scan', 'trigger', 'freq', '2412'])
  508. time.sleep(0.1)
  509. # Allow the connect radio work to continue. The bss entry stored in the
  510. # pending connect work becomes stale during the scan and it must have been
  511. # updated for the connection to work.
  512. dev[0].request("RADIO_WORK done " + id)
  513. wpas.request("RADIO_WORK done " + w_id)
  514. dev[0].wait_connected(timeout=15, error="No connection (sme-connect)")
  515. wpas.wait_connected(timeout=15, error="No connection (connect)")
  516. dev[0].request("DISCONNECT")
  517. wpas.request("DISCONNECT")
  518. dev[0].flush_scan_cache()
  519. wpas.flush_scan_cache()
  520. @remote_compatible
  521. def test_scan_reqs_with_non_scan_radio_work(dev, apdev):
  522. """SCAN commands while non-scan radio_work is in progress"""
  523. id = dev[0].request("RADIO_WORK add test-work-a")
  524. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  525. if ev is None:
  526. raise Exception("Timeout while waiting radio work to start")
  527. if "OK" not in dev[0].request("SCAN"):
  528. raise Exception("SCAN failed")
  529. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  530. raise Exception("SCAN accepted while one is already pending")
  531. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  532. raise Exception("SCAN accepted while one is already pending")
  533. res = dev[0].request("RADIO_WORK show").splitlines()
  534. count = 0
  535. for l in res:
  536. if "scan" in l:
  537. count += 1
  538. if count != 1:
  539. logger.info(res)
  540. raise Exception("Unexpected number of scan radio work items")
  541. dev[0].dump_monitor()
  542. dev[0].request("RADIO_WORK done " + id)
  543. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  544. if ev is None:
  545. raise Exception("Scan did not start")
  546. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  547. raise Exception("SCAN accepted while one is already in progress")
  548. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  549. if ev is None:
  550. print "Scan did not complete"
  551. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.2)
  552. if ev is not None:
  553. raise Exception("Unexpected scan started")
  554. def test_scan_setband(dev, apdev):
  555. """Band selection for scan operations"""
  556. try:
  557. hapd = None
  558. hapd2 = None
  559. params = { "ssid": "test-setband",
  560. "hw_mode": "a",
  561. "channel": "36",
  562. "country_code": "US" }
  563. hapd = hostapd.add_ap(apdev[0], params)
  564. bssid = apdev[0]['bssid']
  565. params = { "ssid": "test-setband",
  566. "hw_mode": "g",
  567. "channel": "1" }
  568. hapd2 = hostapd.add_ap(apdev[1], params)
  569. bssid2 = apdev[1]['bssid']
  570. if "FAIL" not in dev[0].request("SET setband FOO"):
  571. raise Exception("Invalid set setband accepted")
  572. if "OK" not in dev[0].request("SET setband AUTO"):
  573. raise Exception("Failed to set setband")
  574. if "OK" not in dev[1].request("SET setband 5G"):
  575. raise Exception("Failed to set setband")
  576. if "OK" not in dev[2].request("SET setband 2G"):
  577. raise Exception("Failed to set setband")
  578. # Allow a retry to avoid reporting errors during heavy load
  579. for j in range(5):
  580. for i in range(3):
  581. dev[i].request("SCAN only_new=1")
  582. for i in range(3):
  583. ev = dev[i].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
  584. if ev is None:
  585. raise Exception("Scan timed out")
  586. res0 = dev[0].request("SCAN_RESULTS")
  587. res1 = dev[1].request("SCAN_RESULTS")
  588. res2 = dev[2].request("SCAN_RESULTS")
  589. if bssid in res0 and bssid2 in res0 and bssid in res1 and bssid2 in res2:
  590. break
  591. res = dev[0].request("SCAN_RESULTS")
  592. if bssid not in res or bssid2 not in res:
  593. raise Exception("Missing scan result(0)")
  594. res = dev[1].request("SCAN_RESULTS")
  595. if bssid not in res:
  596. raise Exception("Missing scan result(1)")
  597. if bssid2 in res:
  598. raise Exception("Unexpected scan result(1)")
  599. res = dev[2].request("SCAN_RESULTS")
  600. if bssid2 not in res:
  601. raise Exception("Missing scan result(2)")
  602. if bssid in res:
  603. raise Exception("Unexpected scan result(2)")
  604. finally:
  605. if hapd:
  606. hapd.request("DISABLE")
  607. if hapd2:
  608. hapd2.request("DISABLE")
  609. subprocess.call(['iw', 'reg', 'set', '00'])
  610. for i in range(3):
  611. dev[i].request("SET setband AUTO")
  612. dev[i].flush_scan_cache()
  613. @remote_compatible
  614. def test_scan_hidden_many(dev, apdev):
  615. """scan_ssid=1 with large number of profile with hidden SSID"""
  616. try:
  617. _test_scan_hidden_many(dev, apdev)
  618. finally:
  619. dev[0].flush_scan_cache(freq=2432)
  620. dev[0].flush_scan_cache()
  621. dev[0].request("SCAN_INTERVAL 5")
  622. def _test_scan_hidden_many(dev, apdev):
  623. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan-ssid",
  624. "ignore_broadcast_ssid": "1" })
  625. bssid = apdev[0]['bssid']
  626. dev[0].request("SCAN_INTERVAL 1")
  627. for i in range(5):
  628. id = dev[0].add_network()
  629. dev[0].set_network_quoted(id, "ssid", "foo")
  630. dev[0].set_network(id, "key_mgmt", "NONE")
  631. dev[0].set_network(id, "disabled", "0")
  632. dev[0].set_network(id, "scan_freq", "2412")
  633. dev[0].set_network(id, "scan_ssid", "1")
  634. dev[0].set_network_quoted(id, "ssid", "test-scan-ssid")
  635. dev[0].set_network(id, "key_mgmt", "NONE")
  636. dev[0].set_network(id, "disabled", "0")
  637. dev[0].set_network(id, "scan_freq", "2412")
  638. dev[0].set_network(id, "scan_ssid", "1")
  639. for i in range(5):
  640. id = dev[0].add_network()
  641. dev[0].set_network_quoted(id, "ssid", "foo")
  642. dev[0].set_network(id, "key_mgmt", "NONE")
  643. dev[0].set_network(id, "disabled", "0")
  644. dev[0].set_network(id, "scan_freq", "2412")
  645. dev[0].set_network(id, "scan_ssid", "1")
  646. dev[0].request("REASSOCIATE")
  647. dev[0].wait_connected(timeout=30)
  648. dev[0].request("REMOVE_NETWORK all")
  649. hapd.disable()
  650. def test_scan_random_mac(dev, apdev, params):
  651. """Random MAC address in scans"""
  652. try:
  653. _test_scan_random_mac(dev, apdev, params)
  654. finally:
  655. dev[0].request("MAC_RAND_SCAN all enable=0")
  656. def _test_scan_random_mac(dev, apdev, params):
  657. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  658. bssid = apdev[0]['bssid']
  659. tests = [ "",
  660. "addr=foo",
  661. "mask=foo",
  662. "enable=1",
  663. "all enable=1 mask=00:11:22:33:44:55",
  664. "all enable=1 addr=00:11:22:33:44:55",
  665. "all enable=1 addr=01:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  666. "all enable=1 addr=00:11:22:33:44:55 mask=fe:ff:ff:ff:ff:ff",
  667. "enable=2 scan sched pno all",
  668. "pno enable=1",
  669. "all enable=2",
  670. "foo" ]
  671. for args in tests:
  672. if "FAIL" not in dev[0].request("MAC_RAND_SCAN " + args):
  673. raise Exception("Invalid MAC_RAND_SCAN accepted: " + args)
  674. if dev[0].get_driver_status_field('capa.mac_addr_rand_scan_supported') != '1':
  675. raise HwsimSkip("Driver does not support random MAC address for scanning")
  676. tests = [ "all enable=1",
  677. "all enable=1 addr=f2:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  678. "all enable=1 addr=f2:11:33:00:00:00 mask=ff:ff:ff:00:00:00" ]
  679. for args in tests:
  680. dev[0].request("MAC_RAND_SCAN " + args)
  681. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  682. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  683. "wlan.fc.type_subtype == 4", ["wlan.ta" ])
  684. if out is not None:
  685. addr = out.splitlines()
  686. logger.info("Probe Request frames seen from: " + str(addr))
  687. if dev[0].own_addr() in addr:
  688. raise Exception("Real address used to transmit Probe Request frame")
  689. if "f2:11:22:33:44:55" not in addr:
  690. raise Exception("Fully configured random address not seen")
  691. found = False
  692. for a in addr:
  693. if a.startswith('f2:11:33'):
  694. found = True
  695. break
  696. if not found:
  697. raise Exception("Fixed OUI random address not seen")
  698. @remote_compatible
  699. def test_scan_trigger_failure(dev, apdev):
  700. """Scan trigger to the driver failing"""
  701. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  702. bssid = apdev[0]['bssid']
  703. if "OK" not in dev[0].request("SET test_failure 1"):
  704. raise Exception("Failed to set test_failure")
  705. if "OK" not in dev[0].request("SCAN"):
  706. raise Exception("SCAN command failed")
  707. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  708. if ev is None:
  709. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  710. if "retry=1" in ev:
  711. raise Exception("Unexpected scan retry indicated")
  712. if dev[0].get_status_field('wpa_state') == "SCANNING":
  713. raise Exception("wpa_state SCANNING not cleared")
  714. id = dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412",
  715. only_add_network=True)
  716. dev[0].select_network(id)
  717. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  718. if ev is None:
  719. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  720. if "retry=1" not in ev:
  721. raise Exception("No scan retry indicated for connection")
  722. if dev[0].get_status_field('wpa_state') == "SCANNING":
  723. raise Exception("wpa_state SCANNING not cleared")
  724. dev[0].request("SET test_failure 0")
  725. dev[0].wait_connected()
  726. dev[0].request("SET test_failure 1")
  727. if "OK" not in dev[0].request("SCAN"):
  728. raise Exception("SCAN command failed")
  729. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  730. if ev is None:
  731. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  732. if "retry=1" in ev:
  733. raise Exception("Unexpected scan retry indicated")
  734. if dev[0].get_status_field('wpa_state') != "COMPLETED":
  735. raise Exception("wpa_state COMPLETED not restored")
  736. dev[0].request("SET test_failure 0")
  737. @remote_compatible
  738. def test_scan_specify_ssid(dev, apdev):
  739. """Control interface behavior on scan SSID parameter"""
  740. dev[0].flush_scan_cache()
  741. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-hidden",
  742. "ignore_broadcast_ssid": "1" })
  743. bssid = apdev[0]['bssid']
  744. check_scan(dev[0], "freq=2412 use_id=1 ssid 414243")
  745. bss = dev[0].get_bss(bssid)
  746. if bss is not None and bss['ssid'] == 'test-hidden':
  747. raise Exception("BSS entry for hidden AP present unexpectedly")
  748. # Allow couple more retries to avoid reporting errors during heavy load
  749. for i in range(5):
  750. check_scan(dev[0], "freq=2412 ssid 414243 ssid 746573742d68696464656e ssid 616263313233 use_id=1")
  751. bss = dev[0].get_bss(bssid)
  752. if bss and 'test-hidden' in dev[0].request("SCAN_RESULTS"):
  753. break
  754. if bss is None:
  755. raise Exception("BSS entry for hidden AP not found")
  756. if 'test-hidden' not in dev[0].request("SCAN_RESULTS"):
  757. raise Exception("Expected SSID not included in the scan results")
  758. hapd.disable()
  759. dev[0].flush_scan_cache(freq=2432)
  760. dev[0].flush_scan_cache()
  761. if "FAIL" not in dev[0].request("SCAN ssid foo"):
  762. raise Exception("Invalid SCAN command accepted")
  763. @remote_compatible
  764. def test_scan_ap_scan_2_ap_mode(dev, apdev):
  765. """AP_SCAN 2 AP mode and scan()"""
  766. try:
  767. _test_scan_ap_scan_2_ap_mode(dev, apdev)
  768. finally:
  769. dev[0].request("AP_SCAN 1")
  770. def _test_scan_ap_scan_2_ap_mode(dev, apdev):
  771. if "OK" not in dev[0].request("AP_SCAN 2"):
  772. raise Exception("Failed to set AP_SCAN 2")
  773. id = dev[0].add_network()
  774. dev[0].set_network(id, "mode", "2")
  775. dev[0].set_network_quoted(id, "ssid", "wpas-ap-open")
  776. dev[0].set_network(id, "key_mgmt", "NONE")
  777. dev[0].set_network(id, "frequency", "2412")
  778. dev[0].set_network(id, "scan_freq", "2412")
  779. dev[0].set_network(id, "disabled", "0")
  780. dev[0].select_network(id)
  781. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=5)
  782. if ev is None:
  783. raise Exception("AP failed to start")
  784. with fail_test(dev[0], 1, "wpa_driver_nl80211_scan"):
  785. if "OK" not in dev[0].request("SCAN freq=2412"):
  786. raise Exception("SCAN command failed unexpectedly")
  787. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED",
  788. "AP-DISABLED"], timeout=5)
  789. if ev is None:
  790. raise Exception("CTRL-EVENT-SCAN-FAILED not seen")
  791. if "AP-DISABLED" in ev:
  792. raise Exception("Unexpected AP-DISABLED event")
  793. if "retry=1" in ev:
  794. # Wait for the retry to scan happen
  795. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED",
  796. "AP-DISABLED"], timeout=5)
  797. if ev is None:
  798. raise Exception("CTRL-EVENT-SCAN-FAILED not seen - retry")
  799. if "AP-DISABLED" in ev:
  800. raise Exception("Unexpected AP-DISABLED event - retry")
  801. dev[1].connect("wpas-ap-open", key_mgmt="NONE", scan_freq="2412")
  802. dev[1].request("DISCONNECT")
  803. dev[1].wait_disconnected()
  804. dev[0].request("DISCONNECT")
  805. dev[0].wait_disconnected()
  806. def test_scan_bss_expiration_on_ssid_change(dev, apdev):
  807. """BSS entry expiration when AP changes SSID"""
  808. dev[0].flush_scan_cache()
  809. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  810. bssid = apdev[0]['bssid']
  811. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  812. hapd.request("DISABLE")
  813. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  814. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 3"):
  815. raise Exception("BSS_EXPIRE_COUNT failed")
  816. dev[0].scan(freq="2412")
  817. dev[0].scan(freq="2412")
  818. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  819. raise Exception("BSS_EXPIRE_COUNT failed")
  820. res = dev[0].request("SCAN_RESULTS")
  821. if "test-scan" not in res:
  822. raise Exception("The first SSID not in scan results")
  823. if "open" not in res:
  824. raise Exception("The second SSID not in scan results")
  825. dev[0].connect("open", key_mgmt="NONE")
  826. dev[0].request("BSS_FLUSH 0")
  827. res = dev[0].request("SCAN_RESULTS")
  828. if "test-scan" in res:
  829. raise Exception("The BSS entry with the old SSID was not removed")
  830. dev[0].request("DISCONNECT")
  831. dev[0].wait_disconnected()
  832. def test_scan_dfs(dev, apdev, params):
  833. """Scan on DFS channels"""
  834. try:
  835. _test_scan_dfs(dev, apdev, params)
  836. finally:
  837. subprocess.call(['iw', 'reg', 'set', '00'])
  838. def _test_scan_dfs(dev, apdev, params):
  839. subprocess.call(['iw', 'reg', 'set', 'US'])
  840. for i in range(2):
  841. for j in range(5):
  842. ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
  843. if ev is None:
  844. raise Exception("No regdom change event")
  845. if "alpha2=US" in ev:
  846. break
  847. dev[i].dump_monitor()
  848. if "OK" not in dev[0].request("SCAN"):
  849. raise Exception("SCAN command failed")
  850. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  851. if ev is None:
  852. raise Exception("Scan did not complete")
  853. if "OK" not in dev[0].request("SCAN freq=2412,5180,5260,5500,5600,5745"):
  854. raise Exception("SCAN command failed")
  855. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  856. if ev is None:
  857. raise Exception("Scan did not complete")
  858. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  859. "wlan.fc.type_subtype == 4", [ "radiotap.channel.freq" ])
  860. if out is not None:
  861. freq = out.splitlines()
  862. freq = [int(f) for f in freq]
  863. freq = list(set(freq))
  864. freq.sort()
  865. logger.info("Active scan seen on channels: " + str(freq))
  866. for f in freq:
  867. if (f >= 5260 and f <= 5320) or (f >= 5500 and f <= 5700):
  868. raise Exception("Active scan on DFS channel: %d" % f)
  869. if f in [ 2467, 2472 ]:
  870. raise Exception("Active scan on US-disallowed channel: %d" % f)
  871. @remote_compatible
  872. def test_scan_abort(dev, apdev):
  873. """Aborting a full scan"""
  874. dev[0].request("SCAN")
  875. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  876. if ev is None:
  877. raise Exception("Scan did not start")
  878. if "OK" not in dev[0].request("ABORT_SCAN"):
  879. raise Exception("ABORT_SCAN command failed")
  880. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=2)
  881. if ev is None:
  882. raise Exception("Scan did not terminate")
  883. @remote_compatible
  884. def test_scan_abort_on_connect(dev, apdev):
  885. """Aborting a full scan on connection request"""
  886. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  887. bssid = apdev[0]['bssid']
  888. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  889. dev[0].dump_monitor()
  890. dev[0].request("SCAN")
  891. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  892. if ev is None:
  893. raise Exception("Scan did not start")
  894. dev[0].connect("test-scan", key_mgmt="NONE")
  895. @remote_compatible
  896. def test_scan_ext(dev, apdev):
  897. """Custom IE in Probe Request frame"""
  898. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  899. bssid = apdev[0]['bssid']
  900. try:
  901. if "OK" not in dev[0].request("VENDOR_ELEM_ADD 14 dd050011223300"):
  902. raise Exception("VENDOR_ELEM_ADD failed")
  903. check_scan(dev[0], "freq=2412 use_id=1")
  904. finally:
  905. dev[0].request("VENDOR_ELEM_REMOVE 14 *")
  906. def test_scan_fail(dev, apdev):
  907. """Scan failures"""
  908. with fail_test(dev[0], 1, "wpa_driver_nl80211_scan"):
  909. dev[0].request("DISCONNECT")
  910. if "OK" not in dev[0].request("SCAN freq=2412"):
  911. raise Exception("SCAN failed")
  912. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  913. if ev is None:
  914. raise Exception("Did not see scan failure event")
  915. dev[0].dump_monitor()
  916. for i in range(1, 5):
  917. with alloc_fail(dev[0], i,
  918. "wpa_scan_clone_params;wpa_supplicant_trigger_scan"):
  919. if "OK" not in dev[0].request("SCAN ssid 112233 freq=2412"):
  920. raise Exception("SCAN failed")
  921. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  922. if ev is None:
  923. raise Exception("Did not see scan failure event")
  924. dev[0].dump_monitor()
  925. with alloc_fail(dev[0], 1, "radio_add_work;wpa_supplicant_trigger_scan"):
  926. if "OK" not in dev[0].request("SCAN freq=2412"):
  927. raise Exception("SCAN failed")
  928. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  929. if ev is None:
  930. raise Exception("Did not see scan failure event")
  931. dev[0].dump_monitor()
  932. try:
  933. if "OK" not in dev[0].request("SET filter_ssids 1"):
  934. raise Exception("SET failed")
  935. id = dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  936. with alloc_fail(dev[0], 1, "wpa_supplicant_build_filter_ssids"):
  937. # While the filter list cannot be created due to memory allocation
  938. # failure, this scan is expected to be completed without SSID
  939. # filtering.
  940. if "OK" not in dev[0].request("SCAN freq=2412"):
  941. raise Exception("SCAN failed")
  942. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  943. if ev is None:
  944. raise Exception("Scan did not complete")
  945. dev[0].remove_network(id)
  946. finally:
  947. dev[0].request("SET filter_ssids 0")
  948. dev[0].dump_monitor()
  949. with alloc_fail(dev[0], 1, "nl80211_get_scan_results"):
  950. if "OK" not in dev[0].request("SCAN freq=2412"):
  951. raise Exception("SCAN failed")
  952. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  953. if ev is None:
  954. raise Exception("Did not see scan started event")
  955. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  956. dev[0].dump_monitor()
  957. try:
  958. if "OK" not in dev[0].request("SET setband 2G"):
  959. raise Exception("SET setband failed")
  960. with alloc_fail(dev[0], 1, "=wpa_setband_scan_freqs_list"):
  961. # While the frequency list cannot be created due to memory
  962. # allocation failure, this scan is expected to be completed without
  963. # frequency filtering.
  964. if "OK" not in dev[0].request("SCAN"):
  965. raise Exception("SCAN failed")
  966. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  967. dev[0].request("ABORT_SCAN")
  968. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  969. if ev is None:
  970. raise Exception("Scan did not complete")
  971. finally:
  972. dev[0].request("SET setband AUTO")
  973. dev[0].dump_monitor()
  974. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  975. wpas.interface_add("wlan5")
  976. wpas.request("SET preassoc_mac_addr 1")
  977. with fail_test(wpas, 1, "nl80211_set_mac_addr;wpas_trigger_scan_cb"):
  978. if "OK" not in wpas.request("SCAN freq=2412"):
  979. raise Exception("SCAN failed")
  980. ev = wpas.wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  981. if ev is None:
  982. raise Exception("Did not see scan failure event")
  983. wpas.request("SET preassoc_mac_addr 0")
  984. wpas.dump_monitor()
  985. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  986. with alloc_fail(dev[0], 1, "wpa_bss_add"):
  987. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  988. @remote_compatible
  989. def test_scan_freq_list(dev, apdev):
  990. """Scan with SET freq_list and scan_cur_freq"""
  991. try:
  992. if "OK" not in dev[0].request("SET freq_list 2412 2417"):
  993. raise Exception("SET freq_list failed")
  994. check_scan(dev[0], "use_id=1")
  995. finally:
  996. dev[0].request("SET freq_list ")
  997. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  998. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412")
  999. try:
  1000. if "OK" not in dev[0].request("SET scan_cur_freq 1"):
  1001. raise Exception("SET scan_cur_freq failed")
  1002. check_scan(dev[0], "use_id=1")
  1003. finally:
  1004. dev[0].request("SET scan_cur_freq 0")
  1005. dev[0].request("REMOVE_NETWORK all")
  1006. dev[0].wait_disconnected()
  1007. def test_scan_bss_limit(dev, apdev):
  1008. """Scan and wpa_supplicant BSS entry limit"""
  1009. try:
  1010. _test_scan_bss_limit(dev, apdev)
  1011. finally:
  1012. dev[0].request("SET bss_max_count 200")
  1013. pass
  1014. def _test_scan_bss_limit(dev, apdev):
  1015. # Trigger 'Increasing the MAX BSS count to 2 because all BSSes are in use.
  1016. # We should normally not get here!' message by limiting the maximum BSS
  1017. # count to one so that the second AP would not fit in the BSS list and the
  1018. # first AP cannot be removed from the list since it is still in use.
  1019. dev[0].request("SET bss_max_count 1")
  1020. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1021. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412")
  1022. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "test-scan-2",
  1023. "channel": "6" })
  1024. dev[0].scan_for_bss(apdev[1]['bssid'], freq=2437, force_scan=True)