test_scan.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. import time
  7. import logging
  8. logger = logging.getLogger()
  9. import os
  10. import subprocess
  11. import hostapd
  12. from wpasupplicant import WpaSupplicant
  13. from utils import HwsimSkip
  14. from tshark import run_tshark
  15. def check_scan(dev, params, other_started=False, test_busy=False):
  16. if not other_started:
  17. dev.dump_monitor()
  18. id = dev.request("SCAN " + params)
  19. if "FAIL" in id:
  20. raise Exception("Failed to start scan")
  21. id = int(id)
  22. if test_busy:
  23. if "FAIL-BUSY" not in dev.request("SCAN"):
  24. raise Exception("SCAN command while already scanning not rejected")
  25. if other_started:
  26. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  27. if ev is None:
  28. raise Exception("Other scan did not start")
  29. if "id=" + str(id) in ev:
  30. raise Exception("Own scan id unexpectedly included in start event")
  31. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  32. if ev is None:
  33. raise Exception("Other scan did not complete")
  34. if "id=" + str(id) in ev:
  35. raise Exception("Own scan id unexpectedly included in completed event")
  36. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  37. if ev is None:
  38. raise Exception("Scan did not start")
  39. if "id=" + str(id) not in ev:
  40. raise Exception("Scan id not included in start event")
  41. if test_busy:
  42. if "FAIL-BUSY" not in dev.request("SCAN"):
  43. raise Exception("SCAN command while already scanning not rejected")
  44. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  45. if ev is None:
  46. raise Exception("Scan did not complete")
  47. if "id=" + str(id) not in ev:
  48. raise Exception("Scan id not included in completed event")
  49. def check_scan_retry(dev, params, bssid):
  50. for i in range(0, 5):
  51. check_scan(dev, "freq=2412-2462,5180 use_id=1")
  52. if int(dev.get_bss(bssid)['age']) <= 1:
  53. return
  54. raise Exception("Unexpectedly old BSS entry")
  55. def test_scan(dev, apdev):
  56. """Control interface behavior on scan parameters"""
  57. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  58. bssid = apdev[0]['bssid']
  59. logger.info("Full scan")
  60. check_scan(dev[0], "use_id=1", test_busy=True)
  61. logger.info("Limited channel scan")
  62. check_scan_retry(dev[0], "freq=2412-2462,5180 use_id=1", bssid)
  63. # wait long enough to allow next scans to be verified not to find the AP
  64. time.sleep(2)
  65. logger.info("Passive single-channel scan")
  66. check_scan(dev[0], "freq=2457 passive=1 use_id=1")
  67. logger.info("Active single-channel scan")
  68. check_scan(dev[0], "freq=2452 passive=0 use_id=1")
  69. if int(dev[0].get_bss(bssid)['age']) < 2:
  70. raise Exception("Unexpectedly updated BSS entry")
  71. logger.info("Active single-channel scan on AP's operating channel")
  72. check_scan_retry(dev[0], "freq=2412 passive=0 use_id=1", bssid)
  73. def test_scan_only(dev, apdev):
  74. """Control interface behavior on scan parameters with type=only"""
  75. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  76. bssid = apdev[0]['bssid']
  77. logger.info("Full scan")
  78. check_scan(dev[0], "type=only use_id=1")
  79. logger.info("Limited channel scan")
  80. check_scan_retry(dev[0], "type=only freq=2412-2462,5180 use_id=1", bssid)
  81. # wait long enough to allow next scans to be verified not to find the AP
  82. time.sleep(2)
  83. logger.info("Passive single-channel scan")
  84. check_scan(dev[0], "type=only freq=2457 passive=1 use_id=1")
  85. logger.info("Active single-channel scan")
  86. check_scan(dev[0], "type=only freq=2452 passive=0 use_id=1")
  87. if int(dev[0].get_bss(bssid)['age']) < 2:
  88. raise Exception("Unexpectedly updated BSS entry")
  89. logger.info("Active single-channel scan on AP's operating channel")
  90. check_scan_retry(dev[0], "type=only freq=2412 passive=0 use_id=1", bssid)
  91. def test_scan_external_trigger(dev, apdev):
  92. """Avoid operations during externally triggered scan"""
  93. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  94. bssid = apdev[0]['bssid']
  95. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger'])
  96. check_scan(dev[0], "use_id=1", other_started=True)
  97. def test_scan_bss_expiration_count(dev, apdev):
  98. """BSS entry expiration based on scan results without match"""
  99. if "FAIL" not in dev[0].request("BSS_EXPIRE_COUNT 0"):
  100. raise Exception("Invalid BSS_EXPIRE_COUNT accepted")
  101. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  102. raise Exception("BSS_EXPIRE_COUNT failed")
  103. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  104. bssid = apdev[0]['bssid']
  105. dev[0].scan(freq="2412", only_new=True)
  106. if bssid not in dev[0].request("SCAN_RESULTS"):
  107. raise Exception("BSS not found in initial scan")
  108. hapd.request("DISABLE")
  109. dev[0].scan(freq="2412", only_new=True)
  110. if bssid not in dev[0].request("SCAN_RESULTS"):
  111. raise Exception("BSS not found in first scan without match")
  112. dev[0].scan(freq="2412", only_new=True)
  113. if bssid in dev[0].request("SCAN_RESULTS"):
  114. raise Exception("BSS found after two scans without match")
  115. def test_scan_bss_expiration_age(dev, apdev):
  116. """BSS entry expiration based on age"""
  117. try:
  118. if "FAIL" not in dev[0].request("BSS_EXPIRE_AGE COUNT 9"):
  119. raise Exception("Invalid BSS_EXPIRE_AGE accepted")
  120. if "OK" not in dev[0].request("BSS_EXPIRE_AGE 10"):
  121. raise Exception("BSS_EXPIRE_AGE failed")
  122. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  123. bssid = apdev[0]['bssid']
  124. dev[0].scan(freq="2412")
  125. if bssid not in dev[0].request("SCAN_RESULTS"):
  126. raise Exception("BSS not found in initial scan")
  127. hapd.request("DISABLE")
  128. logger.info("Waiting for BSS entry to expire")
  129. time.sleep(7)
  130. if bssid not in dev[0].request("SCAN_RESULTS"):
  131. raise Exception("BSS expired too quickly")
  132. ev = dev[0].wait_event(["CTRL-EVENT-BSS-REMOVED"], timeout=15)
  133. if ev is None:
  134. raise Exception("BSS entry expiration timed out")
  135. if bssid in dev[0].request("SCAN_RESULTS"):
  136. raise Exception("BSS not removed after expiration time")
  137. finally:
  138. dev[0].request("BSS_EXPIRE_AGE 180")
  139. def test_scan_filter(dev, apdev):
  140. """Filter scan results based on SSID"""
  141. try:
  142. if "OK" not in dev[0].request("SET filter_ssids 1"):
  143. raise Exception("SET failed")
  144. id = dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  145. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  146. bssid = apdev[0]['bssid']
  147. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test-scan2" })
  148. bssid2 = apdev[1]['bssid']
  149. dev[0].scan(freq="2412", only_new=True)
  150. if bssid not in dev[0].request("SCAN_RESULTS"):
  151. raise Exception("BSS not found in scan results")
  152. if bssid2 in dev[0].request("SCAN_RESULTS"):
  153. raise Exception("Unexpected BSS found in scan results")
  154. dev[0].set_network_quoted(id, "ssid", "")
  155. dev[0].scan(freq="2412")
  156. id2 = dev[0].connect("test", key_mgmt="NONE", only_add_network=True)
  157. dev[0].scan(freq="2412")
  158. finally:
  159. dev[0].request("SET filter_ssids 0")
  160. def test_scan_int(dev, apdev):
  161. """scan interval configuration"""
  162. try:
  163. if "FAIL" not in dev[0].request("SCAN_INTERVAL -1"):
  164. raise Exception("Accepted invalid scan interval")
  165. if "OK" not in dev[0].request("SCAN_INTERVAL 1"):
  166. raise Exception("Failed to set scan interval")
  167. dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
  168. wait_connect=False)
  169. times = {}
  170. for i in range(0, 3):
  171. logger.info("Waiting for scan to start")
  172. start = os.times()[4]
  173. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  174. if ev is None:
  175. raise Exception("did not start a scan")
  176. stop = os.times()[4]
  177. times[i] = stop - start
  178. logger.info("Waiting for scan to complete")
  179. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  180. if ev is None:
  181. raise Exception("did not complete a scan")
  182. logger.info("times=" + str(times))
  183. if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
  184. raise Exception("Unexpected scan timing: " + str(times))
  185. finally:
  186. dev[0].request("SCAN_INTERVAL 5")
  187. def test_scan_bss_operations(dev, apdev):
  188. """Control interface behavior on BSS parameters"""
  189. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  190. bssid = apdev[0]['bssid']
  191. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test2-scan" })
  192. bssid2 = apdev[1]['bssid']
  193. dev[0].scan(freq="2412")
  194. dev[0].scan(freq="2412")
  195. dev[0].scan(freq="2412")
  196. id1 = dev[0].request("BSS FIRST MASK=0x1").splitlines()[0].split('=')[1]
  197. id2 = dev[0].request("BSS LAST MASK=0x1").splitlines()[0].split('=')[1]
  198. res = dev[0].request("BSS RANGE=ALL MASK=0x20001")
  199. if "id=" + id1 not in res:
  200. raise Exception("Missing BSS " + id1)
  201. if "id=" + id2 not in res:
  202. raise Exception("Missing BSS " + id2)
  203. if "====" not in res:
  204. raise Exception("Missing delim")
  205. if "####" not in res:
  206. raise Exception("Missing end")
  207. res = dev[0].request("BSS RANGE=ALL MASK=0")
  208. if "id=" + id1 not in res:
  209. raise Exception("Missing BSS " + id1)
  210. if "id=" + id2 not in res:
  211. raise Exception("Missing BSS " + id2)
  212. if "====" in res:
  213. raise Exception("Unexpected delim")
  214. if "####" in res:
  215. raise Exception("Unexpected end delim")
  216. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  217. if len(res) != 2:
  218. raise Exception("Unexpected result: " + str(res))
  219. res = dev[0].request("BSS FIRST MASK=0x1")
  220. if "id=" + id1 not in res:
  221. raise Exception("Unexpected result: " + res)
  222. res = dev[0].request("BSS LAST MASK=0x1")
  223. if "id=" + id2 not in res:
  224. raise Exception("Unexpected result: " + res)
  225. res = dev[0].request("BSS ID-" + id1 + " MASK=0x1")
  226. if "id=" + id1 not in res:
  227. raise Exception("Unexpected result: " + res)
  228. res = dev[0].request("BSS NEXT-" + id1 + " MASK=0x1")
  229. if "id=" + id2 not in res:
  230. raise Exception("Unexpected result: " + res)
  231. res = dev[0].request("BSS NEXT-" + id2 + " MASK=0x1")
  232. if "id=" in res:
  233. raise Exception("Unexpected result: " + res)
  234. if len(dev[0].request("BSS RANGE=" + id2 + " MASK=0x1").splitlines()) != 0:
  235. raise Exception("Unexpected RANGE=1 result")
  236. if len(dev[0].request("BSS RANGE=" + id1 + "- MASK=0x1").splitlines()) != 2:
  237. raise Exception("Unexpected RANGE=0- result")
  238. if len(dev[0].request("BSS RANGE=-" + id2 + " MASK=0x1").splitlines()) != 2:
  239. raise Exception("Unexpected RANGE=-1 result")
  240. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id2 + " MASK=0x1").splitlines()) != 2:
  241. raise Exception("Unexpected RANGE=0-1 result")
  242. if len(dev[0].request("BSS RANGE=" + id2 + "-" + id2 + " MASK=0x1").splitlines()) != 1:
  243. raise Exception("Unexpected RANGE=1-1 result")
  244. if len(dev[0].request("BSS RANGE=" + str(int(id2) + 1) + "-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 0:
  245. raise Exception("Unexpected RANGE=2-10 result")
  246. if len(dev[0].request("BSS RANGE=0-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 2:
  247. raise Exception("Unexpected RANGE=0-10 result")
  248. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id1 + " MASK=0x1").splitlines()) != 1:
  249. raise Exception("Unexpected RANGE=0-0 result")
  250. res = dev[0].request("BSS p2p_dev_addr=FOO")
  251. if "FAIL" in res or "id=" in res:
  252. raise Exception("Unexpected result: " + res)
  253. res = dev[0].request("BSS p2p_dev_addr=00:11:22:33:44:55")
  254. if "FAIL" in res or "id=" in res:
  255. raise Exception("Unexpected result: " + res)
  256. dev[0].request("BSS_FLUSH 1000")
  257. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  258. if len(res) != 2:
  259. raise Exception("Unexpected result after BSS_FLUSH 1000")
  260. dev[0].request("BSS_FLUSH 0")
  261. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  262. if len(res) != 0:
  263. raise Exception("Unexpected result after BSS_FLUSH 0")
  264. def test_scan_and_interface_disabled(dev, apdev):
  265. """Scan operation when interface gets disabled"""
  266. try:
  267. dev[0].request("SCAN")
  268. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  269. if ev is None:
  270. raise Exception("Scan did not start")
  271. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  272. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=7)
  273. if ev is not None:
  274. raise Exception("Scan completed unexpectedly")
  275. # verify that scan is rejected
  276. if "FAIL" not in dev[0].request("SCAN"):
  277. raise Exception("New scan request was accepted unexpectedly")
  278. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  279. dev[0].scan(freq="2412")
  280. finally:
  281. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  282. def test_scan_for_auth(dev, apdev):
  283. """cfg80211 workaround with scan-for-auth"""
  284. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  285. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  286. # Block sme-connect radio work with an external radio work item, so that
  287. # SELECT_NETWORK can decide to use fast associate without a new scan while
  288. # cfg80211 still has the matching BSS entry, but the actual connection is
  289. # not yet started.
  290. id = dev[0].request("RADIO_WORK add block-work")
  291. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  292. if ev is None:
  293. raise Exception("Timeout while waiting radio work to start")
  294. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  295. wait_connect=False)
  296. dev[0].dump_monitor()
  297. # Clear cfg80211 BSS table.
  298. try:
  299. subprocess.check_call(['iw', dev[0].ifname, 'scan', 'trigger',
  300. 'freq', '2457', 'flush'])
  301. except subprocess.CalledProcessError, e:
  302. raise HwsimSkip("iw scan trigger flush not supported")
  303. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  304. if ev is None:
  305. raise Exception("External flush scan timed out")
  306. # Release blocking radio work to allow connection to go through with the
  307. # cfg80211 BSS entry missing.
  308. dev[0].request("RADIO_WORK done " + id)
  309. dev[0].wait_connected(timeout=15)
  310. def test_scan_for_auth_fail(dev, apdev):
  311. """cfg80211 workaround with scan-for-auth failing"""
  312. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  313. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  314. # Block sme-connect radio work with an external radio work item, so that
  315. # SELECT_NETWORK can decide to use fast associate without a new scan while
  316. # cfg80211 still has the matching BSS entry, but the actual connection is
  317. # not yet started.
  318. id = dev[0].request("RADIO_WORK add block-work")
  319. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  320. if ev is None:
  321. raise Exception("Timeout while waiting radio work to start")
  322. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  323. wait_connect=False)
  324. dev[0].dump_monitor()
  325. hapd.disable()
  326. # Clear cfg80211 BSS table.
  327. try:
  328. subprocess.check_call(['iw', dev[0].ifname, 'scan', 'trigger',
  329. 'freq', '2457', 'flush'])
  330. except subprocess.CalledProcessError, e:
  331. raise HwsimSkip("iw scan trigger flush not supported")
  332. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  333. if ev is None:
  334. raise Exception("External flush scan timed out")
  335. # Release blocking radio work to allow connection to go through with the
  336. # cfg80211 BSS entry missing.
  337. dev[0].request("RADIO_WORK done " + id)
  338. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS",
  339. "CTRL-EVENT-CONNECTED"], 15)
  340. if ev is None:
  341. raise Exception("Scan event missing")
  342. if "CTRL-EVENT-CONNECTED" in ev:
  343. raise Exception("Unexpected connection")
  344. dev[0].request("DISCONNECT")
  345. def test_scan_for_auth_wep(dev, apdev):
  346. """cfg80211 scan-for-auth workaround with WEP keys"""
  347. dev[0].flush_scan_cache()
  348. hapd = hostapd.add_ap(apdev[0]['ifname'],
  349. { "ssid": "wep", "wep_key0": '"abcde"',
  350. "auth_algs": "2" })
  351. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  352. # Block sme-connect radio work with an external radio work item, so that
  353. # SELECT_NETWORK can decide to use fast associate without a new scan while
  354. # cfg80211 still has the matching BSS entry, but the actual connection is
  355. # not yet started.
  356. id = dev[0].request("RADIO_WORK add block-work")
  357. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  358. if ev is None:
  359. raise Exception("Timeout while waiting radio work to start")
  360. dev[0].connect("wep", key_mgmt="NONE", wep_key0='"abcde"',
  361. auth_alg="SHARED", scan_freq="2412", wait_connect=False)
  362. dev[0].dump_monitor()
  363. # Clear cfg80211 BSS table.
  364. try:
  365. subprocess.check_call(['iw', dev[0].ifname, 'scan', 'trigger',
  366. 'freq', '2457', 'flush'])
  367. except subprocess.CalledProcessError, e:
  368. raise HwsimSkip("iw scan trigger flush not supported")
  369. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  370. if ev is None:
  371. raise Exception("External flush scan timed out")
  372. # Release blocking radio work to allow connection to go through with the
  373. # cfg80211 BSS entry missing.
  374. dev[0].request("RADIO_WORK done " + id)
  375. dev[0].wait_connected(timeout=15)
  376. def test_scan_hidden(dev, apdev):
  377. """Control interface behavior on scan parameters"""
  378. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan",
  379. "ignore_broadcast_ssid": "1" })
  380. bssid = apdev[0]['bssid']
  381. check_scan(dev[0], "freq=2412 use_id=1")
  382. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  383. raise Exception("BSS unexpectedly found in initial scan")
  384. id1 = dev[0].connect("foo", key_mgmt="NONE", scan_ssid="1",
  385. only_add_network=True)
  386. id2 = dev[0].connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  387. only_add_network=True)
  388. id3 = dev[0].connect("bar", key_mgmt="NONE", only_add_network=True)
  389. check_scan(dev[0], "freq=2412 use_id=1")
  390. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  391. raise Exception("BSS unexpectedly found in scan")
  392. # Allow multiple attempts to be more robust under heavy CPU load that can
  393. # result in Probe Response frames getting sent only after the station has
  394. # already stopped waiting for the response on the channel.
  395. found = False
  396. for i in range(10):
  397. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id2, id3))
  398. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  399. found = True
  400. break
  401. if not found:
  402. raise Exception("BSS not found in scan")
  403. 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"):
  404. raise Exception("Too many scan_id values accepted")
  405. dev[0].request("REMOVE_NETWORK all")
  406. hapd.disable()
  407. dev[0].flush_scan_cache(freq=2432)
  408. dev[0].flush_scan_cache()
  409. def test_scan_and_bss_entry_removed(dev, apdev):
  410. """Last scan result and connect work processing on BSS entry update"""
  411. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open",
  412. "eap_server": "1",
  413. "wps_state": "2" })
  414. bssid = apdev[0]['bssid']
  415. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  416. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  417. # Add a BSS entry
  418. dev[0].scan_for_bss(bssid, freq="2412")
  419. wpas.scan_for_bss(bssid, freq="2412")
  420. # Start a connect radio work with a blocking entry preventing this from
  421. # proceeding; this stores a pointer to the selected BSS entry.
  422. id = dev[0].request("RADIO_WORK add block-work")
  423. w_id = wpas.request("RADIO_WORK add block-work")
  424. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  425. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  426. nid = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  427. wait_connect=False)
  428. w_nid = wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  429. wait_connect=False)
  430. time.sleep(0.1)
  431. # Remove the BSS entry
  432. dev[0].request("BSS_FLUSH 0")
  433. wpas.request("BSS_FLUSH 0")
  434. # Allow the connect radio work to continue. The bss entry stored in the
  435. # pending connect work is now stale. This will result in the connection
  436. # attempt failing since the BSS entry does not exist.
  437. dev[0].request("RADIO_WORK done " + id)
  438. wpas.request("RADIO_WORK done " + w_id)
  439. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  440. if ev is not None:
  441. raise Exception("Unexpected connection")
  442. dev[0].remove_network(nid)
  443. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  444. if ev is not None:
  445. raise Exception("Unexpected connection")
  446. wpas.remove_network(w_nid)
  447. time.sleep(0.5)
  448. dev[0].request("BSS_FLUSH 0")
  449. wpas.request("BSS_FLUSH 0")
  450. # Add a BSS entry
  451. dev[0].scan_for_bss(bssid, freq="2412")
  452. wpas.scan_for_bss(bssid, freq="2412")
  453. # Start a connect radio work with a blocking entry preventing this from
  454. # proceeding; this stores a pointer to the selected BSS entry.
  455. id = dev[0].request("RADIO_WORK add block-work")
  456. w_id = wpas.request("RADIO_WORK add block-work")
  457. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  458. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  459. # Schedule a connection based on the current BSS entry.
  460. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  461. wait_connect=False)
  462. wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  463. wait_connect=False)
  464. # Update scan results with results that have longer set of IEs so that new
  465. # memory needs to be allocated for the BSS entry.
  466. hapd.request("WPS_PBC")
  467. time.sleep(0.1)
  468. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger', 'freq', '2412'])
  469. subprocess.call(['iw', wpas.ifname, 'scan', 'trigger', 'freq', '2412'])
  470. time.sleep(0.1)
  471. # Allow the connect radio work to continue. The bss entry stored in the
  472. # pending connect work becomes stale during the scan and it must have been
  473. # updated for the connection to work.
  474. dev[0].request("RADIO_WORK done " + id)
  475. wpas.request("RADIO_WORK done " + w_id)
  476. dev[0].wait_connected(timeout=15, error="No connection (sme-connect)")
  477. wpas.wait_connected(timeout=15, error="No connection (connect)")
  478. def test_scan_reqs_with_non_scan_radio_work(dev, apdev):
  479. """SCAN commands while non-scan radio_work is in progress"""
  480. id = dev[0].request("RADIO_WORK add test-work-a")
  481. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  482. if ev is None:
  483. raise Exception("Timeout while waiting radio work to start")
  484. if "OK" not in dev[0].request("SCAN"):
  485. raise Exception("SCAN failed")
  486. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  487. raise Exception("SCAN accepted while one is already pending")
  488. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  489. raise Exception("SCAN accepted while one is already pending")
  490. res = dev[0].request("RADIO_WORK show").splitlines()
  491. count = 0
  492. for l in res:
  493. if "scan" in l:
  494. count += 1
  495. if count != 1:
  496. logger.info(res)
  497. raise Exception("Unexpected number of scan radio work items")
  498. dev[0].dump_monitor()
  499. dev[0].request("RADIO_WORK done " + id)
  500. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  501. if ev is None:
  502. raise Exception("Scan did not start")
  503. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  504. raise Exception("SCAN accepted while one is already in progress")
  505. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  506. if ev is None:
  507. print "Scan did not complete"
  508. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.2)
  509. if ev is not None:
  510. raise Exception("Unexpected scan started")
  511. def test_scan_setband(dev, apdev):
  512. """Band selection for scan operations"""
  513. try:
  514. hapd = None
  515. hapd2 = None
  516. params = { "ssid": "test-setband",
  517. "hw_mode": "a",
  518. "channel": "36",
  519. "country_code": "US" }
  520. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  521. bssid = apdev[0]['bssid']
  522. params = { "ssid": "test-setband",
  523. "hw_mode": "g",
  524. "channel": "1" }
  525. hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
  526. bssid2 = apdev[1]['bssid']
  527. if "FAIL" not in dev[0].request("SET setband FOO"):
  528. raise Exception("Invalid set setband accepted")
  529. if "OK" not in dev[0].request("SET setband AUTO"):
  530. raise Exception("Failed to set setband")
  531. if "OK" not in dev[1].request("SET setband 5G"):
  532. raise Exception("Failed to set setband")
  533. if "OK" not in dev[2].request("SET setband 2G"):
  534. raise Exception("Failed to set setband")
  535. for i in range(3):
  536. dev[i].request("SCAN only_new=1")
  537. for i in range(3):
  538. ev = dev[i].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
  539. if ev is None:
  540. raise Exception("Scan timed out")
  541. res = dev[0].request("SCAN_RESULTS")
  542. if bssid not in res or bssid2 not in res:
  543. raise Exception("Missing scan result(0)")
  544. res = dev[1].request("SCAN_RESULTS")
  545. if bssid not in res:
  546. raise Exception("Missing scan result(1)")
  547. if bssid2 in res:
  548. raise Exception("Unexpected scan result(1)")
  549. res = dev[2].request("SCAN_RESULTS")
  550. if bssid2 not in res:
  551. raise Exception("Missing scan result(2)")
  552. if bssid in res:
  553. raise Exception("Unexpected scan result(2)")
  554. finally:
  555. if hapd:
  556. hapd.request("DISABLE")
  557. if hapd2:
  558. hapd2.request("DISABLE")
  559. subprocess.call(['iw', 'reg', 'set', '00'])
  560. for i in range(3):
  561. dev[i].request("SET setband AUTO")
  562. dev[i].flush_scan_cache()
  563. def test_scan_hidden_many(dev, apdev):
  564. """scan_ssid=1 with large number of profile with hidden SSID"""
  565. try:
  566. _test_scan_hidden_many(dev, apdev)
  567. finally:
  568. dev[0].flush_scan_cache(freq=2432)
  569. dev[0].flush_scan_cache()
  570. dev[0].request("SCAN_INTERVAL 5")
  571. def _test_scan_hidden_many(dev, apdev):
  572. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan-ssid",
  573. "ignore_broadcast_ssid": "1" })
  574. bssid = apdev[0]['bssid']
  575. dev[0].request("SCAN_INTERVAL 1")
  576. for i in range(5):
  577. id = dev[0].add_network()
  578. dev[0].set_network_quoted(id, "ssid", "foo")
  579. dev[0].set_network(id, "key_mgmt", "NONE")
  580. dev[0].set_network(id, "disabled", "0")
  581. dev[0].set_network(id, "scan_freq", "2412")
  582. dev[0].set_network(id, "scan_ssid", "1")
  583. dev[0].set_network_quoted(id, "ssid", "test-scan-ssid")
  584. dev[0].set_network(id, "key_mgmt", "NONE")
  585. dev[0].set_network(id, "disabled", "0")
  586. dev[0].set_network(id, "scan_freq", "2412")
  587. dev[0].set_network(id, "scan_ssid", "1")
  588. for i in range(5):
  589. id = dev[0].add_network()
  590. dev[0].set_network_quoted(id, "ssid", "foo")
  591. dev[0].set_network(id, "key_mgmt", "NONE")
  592. dev[0].set_network(id, "disabled", "0")
  593. dev[0].set_network(id, "scan_freq", "2412")
  594. dev[0].set_network(id, "scan_ssid", "1")
  595. dev[0].request("REASSOCIATE")
  596. dev[0].wait_connected(timeout=30)
  597. dev[0].request("REMOVE_NETWORK all")
  598. hapd.disable()
  599. def test_scan_random_mac(dev, apdev, params):
  600. """Random MAC address in scans"""
  601. try:
  602. _test_scan_random_mac(dev, apdev, params)
  603. finally:
  604. dev[0].request("MAC_RAND_SCAN all enable=0")
  605. def _test_scan_random_mac(dev, apdev, params):
  606. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  607. bssid = apdev[0]['bssid']
  608. tests = [ "",
  609. "addr=foo",
  610. "mask=foo",
  611. "enable=1",
  612. "all enable=1 mask=00:11:22:33:44:55",
  613. "all enable=1 addr=00:11:22:33:44:55",
  614. "all enable=1 addr=01:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  615. "all enable=1 addr=00:11:22:33:44:55 mask=fe:ff:ff:ff:ff:ff",
  616. "enable=2 scan sched pno all",
  617. "pno enable=1",
  618. "all enable=2",
  619. "foo" ]
  620. for args in tests:
  621. if "FAIL" not in dev[0].request("MAC_RAND_SCAN " + args):
  622. raise Exception("Invalid MAC_RAND_SCAN accepted: " + args)
  623. if dev[0].get_driver_status_field('capa.mac_addr_rand_scan_supported') != '1':
  624. raise HwsimSkip("Driver does not support random MAC address for scanning")
  625. tests = [ "all enable=1",
  626. "all enable=1 addr=f2:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  627. "all enable=1 addr=f2:11:33:00:00:00 mask=ff:ff:ff:00:00:00" ]
  628. for args in tests:
  629. dev[0].request("MAC_RAND_SCAN " + args)
  630. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  631. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  632. "wlan.fc.type_subtype == 4", ["wlan.ta" ])
  633. if out is not None:
  634. addr = out.splitlines()
  635. logger.info("Probe Request frames seen from: " + str(addr))
  636. if dev[0].own_addr() in addr:
  637. raise Exception("Real address used to transmit Probe Request frame")
  638. if "f2:11:22:33:44:55" not in addr:
  639. raise Exception("Fully configured random address not seen")
  640. found = False
  641. for a in addr:
  642. if a.startswith('f2:11:33'):
  643. found = True
  644. break
  645. if not found:
  646. raise Exception("Fixed OUI random address not seen")
  647. def test_scan_trigger_failure(dev, apdev):
  648. """Scan trigger to the driver failing"""
  649. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  650. bssid = apdev[0]['bssid']
  651. if "OK" not in dev[0].request("SET test_failure 1"):
  652. raise Exception("Failed to set test_failure")
  653. if "OK" not in dev[0].request("SCAN"):
  654. raise Exception("SCAN command failed")
  655. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  656. if ev is None:
  657. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  658. if "retry=1" in ev:
  659. raise Exception("Unexpected scan retry indicated")
  660. if dev[0].get_status_field('wpa_state') == "SCANNING":
  661. raise Exception("wpa_state SCANNING not cleared")
  662. id = dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412",
  663. only_add_network=True)
  664. dev[0].select_network(id)
  665. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  666. if ev is None:
  667. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  668. if "retry=1" not in ev:
  669. raise Exception("No scan retry indicated for connection")
  670. if dev[0].get_status_field('wpa_state') == "SCANNING":
  671. raise Exception("wpa_state SCANNING not cleared")
  672. dev[0].request("SET test_failure 0")
  673. dev[0].wait_connected()
  674. dev[0].request("SET test_failure 1")
  675. if "OK" not in dev[0].request("SCAN"):
  676. raise Exception("SCAN command failed")
  677. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  678. if ev is None:
  679. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  680. if "retry=1" in ev:
  681. raise Exception("Unexpected scan retry indicated")
  682. if dev[0].get_status_field('wpa_state') != "COMPLETED":
  683. raise Exception("wpa_state COMPLETED not restored")
  684. dev[0].request("SET test_failure 0")