test_scan.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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(['sudo', '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. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger',
  299. 'freq', '2457', 'flush'])
  300. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  301. if ev is None:
  302. raise Exception("External flush scan timed out")
  303. # Release blocking radio work to allow connection to go through with the
  304. # cfg80211 BSS entry missing.
  305. dev[0].request("RADIO_WORK done " + id)
  306. dev[0].wait_connected(timeout=15)
  307. def test_scan_for_auth_fail(dev, apdev):
  308. """cfg80211 workaround with scan-for-auth failing"""
  309. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  310. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  311. # Block sme-connect radio work with an external radio work item, so that
  312. # SELECT_NETWORK can decide to use fast associate without a new scan while
  313. # cfg80211 still has the matching BSS entry, but the actual connection is
  314. # not yet started.
  315. id = dev[0].request("RADIO_WORK add block-work")
  316. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  317. if ev is None:
  318. raise Exception("Timeout while waiting radio work to start")
  319. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  320. wait_connect=False)
  321. dev[0].dump_monitor()
  322. hapd.disable()
  323. # Clear cfg80211 BSS table.
  324. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger',
  325. 'freq', '2457', 'flush'])
  326. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  327. if ev is None:
  328. raise Exception("External flush scan timed out")
  329. # Release blocking radio work to allow connection to go through with the
  330. # cfg80211 BSS entry missing.
  331. dev[0].request("RADIO_WORK done " + id)
  332. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS",
  333. "CTRL-EVENT-CONNECTED"], 15)
  334. if ev is None:
  335. raise Exception("Scan event missing")
  336. if "CTRL-EVENT-CONNECTED" in ev:
  337. raise Exception("Unexpected connection")
  338. dev[0].request("DISCONNECT")
  339. def test_scan_for_auth_wep(dev, apdev):
  340. """cfg80211 scan-for-auth workaround with WEP keys"""
  341. dev[0].flush_scan_cache()
  342. hapd = hostapd.add_ap(apdev[0]['ifname'],
  343. { "ssid": "wep", "wep_key0": '"abcde"',
  344. "auth_algs": "2" })
  345. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  346. # Block sme-connect radio work with an external radio work item, so that
  347. # SELECT_NETWORK can decide to use fast associate without a new scan while
  348. # cfg80211 still has the matching BSS entry, but the actual connection is
  349. # not yet started.
  350. id = dev[0].request("RADIO_WORK add block-work")
  351. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  352. if ev is None:
  353. raise Exception("Timeout while waiting radio work to start")
  354. dev[0].connect("wep", key_mgmt="NONE", wep_key0='"abcde"',
  355. auth_alg="SHARED", scan_freq="2412", wait_connect=False)
  356. dev[0].dump_monitor()
  357. # Clear cfg80211 BSS table.
  358. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger',
  359. 'freq', '2457', 'flush'])
  360. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  361. if ev is None:
  362. raise Exception("External flush scan timed out")
  363. # Release blocking radio work to allow connection to go through with the
  364. # cfg80211 BSS entry missing.
  365. dev[0].request("RADIO_WORK done " + id)
  366. dev[0].wait_connected(timeout=15)
  367. def test_scan_hidden(dev, apdev):
  368. """Control interface behavior on scan parameters"""
  369. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan",
  370. "ignore_broadcast_ssid": "1" })
  371. bssid = apdev[0]['bssid']
  372. check_scan(dev[0], "freq=2412 use_id=1")
  373. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  374. raise Exception("BSS unexpectedly found in initial scan")
  375. id1 = dev[0].connect("foo", key_mgmt="NONE", scan_ssid="1",
  376. only_add_network=True)
  377. id2 = dev[0].connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  378. only_add_network=True)
  379. id3 = dev[0].connect("bar", key_mgmt="NONE", only_add_network=True)
  380. check_scan(dev[0], "freq=2412 use_id=1")
  381. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  382. raise Exception("BSS unexpectedly found in scan")
  383. # Allow multiple attempts to be more robust under heavy CPU load that can
  384. # result in Probe Response frames getting sent only after the station has
  385. # already stopped waiting for the response on the channel.
  386. found = False
  387. for i in range(10):
  388. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id2, id3))
  389. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  390. found = True
  391. break
  392. if not found:
  393. raise Exception("BSS not found in scan")
  394. 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"):
  395. raise Exception("Too many scan_id values accepted")
  396. dev[0].request("REMOVE_NETWORK all")
  397. hapd.disable()
  398. dev[0].flush_scan_cache(freq=2432)
  399. dev[0].flush_scan_cache()
  400. def test_scan_and_bss_entry_removed(dev, apdev):
  401. """Last scan result and connect work processing on BSS entry update"""
  402. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open",
  403. "eap_server": "1",
  404. "wps_state": "2" })
  405. bssid = apdev[0]['bssid']
  406. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  407. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  408. # Add a BSS entry
  409. dev[0].scan_for_bss(bssid, freq="2412")
  410. wpas.scan_for_bss(bssid, freq="2412")
  411. # Start a connect radio work with a blocking entry preventing this from
  412. # proceeding; this stores a pointer to the selected BSS entry.
  413. id = dev[0].request("RADIO_WORK add block-work")
  414. w_id = wpas.request("RADIO_WORK add block-work")
  415. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  416. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  417. nid = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  418. wait_connect=False)
  419. w_nid = wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  420. wait_connect=False)
  421. time.sleep(0.1)
  422. # Remove the BSS entry
  423. dev[0].request("BSS_FLUSH 0")
  424. wpas.request("BSS_FLUSH 0")
  425. # Allow the connect radio work to continue. The bss entry stored in the
  426. # pending connect work is now stale. This will result in the connection
  427. # attempt failing since the BSS entry does not exist.
  428. dev[0].request("RADIO_WORK done " + id)
  429. wpas.request("RADIO_WORK done " + w_id)
  430. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  431. if ev is not None:
  432. raise Exception("Unexpected connection")
  433. dev[0].remove_network(nid)
  434. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  435. if ev is not None:
  436. raise Exception("Unexpected connection")
  437. wpas.remove_network(w_nid)
  438. time.sleep(0.5)
  439. dev[0].request("BSS_FLUSH 0")
  440. wpas.request("BSS_FLUSH 0")
  441. # Add a BSS entry
  442. dev[0].scan_for_bss(bssid, freq="2412")
  443. wpas.scan_for_bss(bssid, freq="2412")
  444. # Start a connect radio work with a blocking entry preventing this from
  445. # proceeding; this stores a pointer to the selected BSS entry.
  446. id = dev[0].request("RADIO_WORK add block-work")
  447. w_id = wpas.request("RADIO_WORK add block-work")
  448. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  449. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  450. # Schedule a connection based on the current BSS entry.
  451. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  452. wait_connect=False)
  453. wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  454. wait_connect=False)
  455. # Update scan results with results that have longer set of IEs so that new
  456. # memory needs to be allocated for the BSS entry.
  457. hapd.request("WPS_PBC")
  458. time.sleep(0.1)
  459. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger', 'freq', '2412'])
  460. subprocess.call(['iw', wpas.ifname, 'scan', 'trigger', 'freq', '2412'])
  461. time.sleep(0.1)
  462. # Allow the connect radio work to continue. The bss entry stored in the
  463. # pending connect work becomes stale during the scan and it must have been
  464. # updated for the connection to work.
  465. dev[0].request("RADIO_WORK done " + id)
  466. wpas.request("RADIO_WORK done " + w_id)
  467. dev[0].wait_connected(timeout=15, error="No connection (sme-connect)")
  468. wpas.wait_connected(timeout=15, error="No connection (connect)")
  469. def test_scan_reqs_with_non_scan_radio_work(dev, apdev):
  470. """SCAN commands while non-scan radio_work is in progress"""
  471. id = dev[0].request("RADIO_WORK add test-work-a")
  472. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  473. if ev is None:
  474. raise Exception("Timeout while waiting radio work to start")
  475. if "OK" not in dev[0].request("SCAN"):
  476. raise Exception("SCAN failed")
  477. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  478. raise Exception("SCAN accepted while one is already pending")
  479. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  480. raise Exception("SCAN accepted while one is already pending")
  481. res = dev[0].request("RADIO_WORK show").splitlines()
  482. count = 0
  483. for l in res:
  484. if "scan" in l:
  485. count += 1
  486. if count != 1:
  487. logger.info(res)
  488. raise Exception("Unexpected number of scan radio work items")
  489. dev[0].dump_monitor()
  490. dev[0].request("RADIO_WORK done " + id)
  491. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  492. if ev is None:
  493. raise Exception("Scan did not start")
  494. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  495. raise Exception("SCAN accepted while one is already in progress")
  496. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  497. if ev is None:
  498. print "Scan did not complete"
  499. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.2)
  500. if ev is not None:
  501. raise Exception("Unexpected scan started")
  502. def test_scan_setband(dev, apdev):
  503. """Band selection for scan operations"""
  504. try:
  505. hapd = None
  506. hapd2 = None
  507. params = { "ssid": "test-setband",
  508. "hw_mode": "a",
  509. "channel": "36",
  510. "country_code": "US" }
  511. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  512. bssid = apdev[0]['bssid']
  513. params = { "ssid": "test-setband",
  514. "hw_mode": "g",
  515. "channel": "1" }
  516. hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
  517. bssid2 = apdev[1]['bssid']
  518. if "FAIL" not in dev[0].request("SET setband FOO"):
  519. raise Exception("Invalid set setband accepted")
  520. if "OK" not in dev[0].request("SET setband AUTO"):
  521. raise Exception("Failed to set setband")
  522. if "OK" not in dev[1].request("SET setband 5G"):
  523. raise Exception("Failed to set setband")
  524. if "OK" not in dev[2].request("SET setband 2G"):
  525. raise Exception("Failed to set setband")
  526. for i in range(3):
  527. dev[i].request("SCAN only_new=1")
  528. for i in range(3):
  529. ev = dev[i].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
  530. if ev is None:
  531. raise Exception("Scan timed out")
  532. res = dev[0].request("SCAN_RESULTS")
  533. if bssid not in res or bssid2 not in res:
  534. raise Exception("Missing scan result(0)")
  535. res = dev[1].request("SCAN_RESULTS")
  536. if bssid not in res:
  537. raise Exception("Missing scan result(1)")
  538. if bssid2 in res:
  539. raise Exception("Unexpected scan result(1)")
  540. res = dev[2].request("SCAN_RESULTS")
  541. if bssid2 not in res:
  542. raise Exception("Missing scan result(2)")
  543. if bssid in res:
  544. raise Exception("Unexpected scan result(2)")
  545. finally:
  546. if hapd:
  547. hapd.request("DISABLE")
  548. if hapd2:
  549. hapd2.request("DISABLE")
  550. subprocess.call(['iw', 'reg', 'set', '00'])
  551. for i in range(3):
  552. dev[i].request("SET setband AUTO")
  553. dev[i].flush_scan_cache()
  554. def test_scan_hidden_many(dev, apdev):
  555. """scan_ssid=1 with large number of profile with hidden SSID"""
  556. try:
  557. _test_scan_hidden_many(dev, apdev)
  558. finally:
  559. dev[0].flush_scan_cache(freq=2432)
  560. dev[0].flush_scan_cache()
  561. dev[0].request("SCAN_INTERVAL 5")
  562. def _test_scan_hidden_many(dev, apdev):
  563. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan-ssid",
  564. "ignore_broadcast_ssid": "1" })
  565. bssid = apdev[0]['bssid']
  566. dev[0].request("SCAN_INTERVAL 1")
  567. for i in range(5):
  568. id = dev[0].add_network()
  569. dev[0].set_network_quoted(id, "ssid", "foo")
  570. dev[0].set_network(id, "key_mgmt", "NONE")
  571. dev[0].set_network(id, "disabled", "0")
  572. dev[0].set_network(id, "scan_freq", "2412")
  573. dev[0].set_network(id, "scan_ssid", "1")
  574. dev[0].set_network_quoted(id, "ssid", "test-scan-ssid")
  575. dev[0].set_network(id, "key_mgmt", "NONE")
  576. dev[0].set_network(id, "disabled", "0")
  577. dev[0].set_network(id, "scan_freq", "2412")
  578. dev[0].set_network(id, "scan_ssid", "1")
  579. for i in range(5):
  580. id = dev[0].add_network()
  581. dev[0].set_network_quoted(id, "ssid", "foo")
  582. dev[0].set_network(id, "key_mgmt", "NONE")
  583. dev[0].set_network(id, "disabled", "0")
  584. dev[0].set_network(id, "scan_freq", "2412")
  585. dev[0].set_network(id, "scan_ssid", "1")
  586. dev[0].request("REASSOCIATE")
  587. dev[0].wait_connected(timeout=30)
  588. dev[0].request("REMOVE_NETWORK all")
  589. hapd.disable()
  590. def test_scan_random_mac(dev, apdev, params):
  591. """Random MAC address in scans"""
  592. try:
  593. _test_scan_random_mac(dev, apdev, params)
  594. finally:
  595. dev[0].request("MAC_RAND_SCAN all enable=0")
  596. def _test_scan_random_mac(dev, apdev, params):
  597. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  598. bssid = apdev[0]['bssid']
  599. tests = [ "",
  600. "addr=foo",
  601. "mask=foo",
  602. "enable=1",
  603. "all enable=1 mask=00:11:22:33:44:55",
  604. "all enable=1 addr=00:11:22:33:44:55",
  605. "all enable=1 addr=01:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  606. "all enable=1 addr=00:11:22:33:44:55 mask=fe:ff:ff:ff:ff:ff",
  607. "enable=2 scan sched pno all",
  608. "pno enable=1",
  609. "all enable=2",
  610. "foo" ]
  611. for args in tests:
  612. if "FAIL" not in dev[0].request("MAC_RAND_SCAN " + args):
  613. raise Exception("Invalid MAC_RAND_SCAN accepted: " + args)
  614. if dev[0].get_driver_status_field('capa.mac_addr_rand_scan_supported') != '1':
  615. raise HwsimSkip("Driver does not support random MAC address for scanning")
  616. tests = [ "all enable=1",
  617. "all enable=1 addr=f2:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  618. "all enable=1 addr=f2:11:33:00:00:00 mask=ff:ff:ff:00:00:00" ]
  619. for args in tests:
  620. dev[0].request("MAC_RAND_SCAN " + args)
  621. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  622. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  623. "wlan.fc.type_subtype == 4", ["wlan.ta" ])
  624. if out is not None:
  625. addr = out.splitlines()
  626. logger.info("Probe Request frames seen from: " + str(addr))
  627. if dev[0].own_addr() in addr:
  628. raise Exception("Real address used to transmit Probe Request frame")
  629. if "f2:11:22:33:44:55" not in addr:
  630. raise Exception("Fully configured random address not seen")
  631. found = False
  632. for a in addr:
  633. if a.startswith('f2:11:33'):
  634. found = True
  635. break
  636. if not found:
  637. raise Exception("Fixed OUI random address not seen")