test_scan.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. # Scanning tests
  2. # Copyright (c) 2013, 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. def check_scan(dev, params, other_started=False):
  14. if not other_started:
  15. dev.dump_monitor()
  16. id = dev.request("SCAN " + params)
  17. if "FAIL" in id:
  18. raise Exception("Failed to start scan")
  19. id = int(id)
  20. if other_started:
  21. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  22. if ev is None:
  23. raise Exception("Other scan did not start")
  24. if "id=" + str(id) in ev:
  25. raise Exception("Own scan id unexpectedly included in start event")
  26. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  27. if ev is None:
  28. raise Exception("Other scan did not complete")
  29. if "id=" + str(id) in ev:
  30. raise Exception("Own scan id unexpectedly included in completed event")
  31. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  32. if ev is None:
  33. raise Exception("Scan did not start")
  34. if "id=" + str(id) not in ev:
  35. raise Exception("Scan id not included in start event")
  36. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  37. if ev is None:
  38. raise Exception("Scan did not complete")
  39. if "id=" + str(id) not in ev:
  40. raise Exception("Scan id not included in completed event")
  41. def check_scan_retry(dev, params, bssid):
  42. for i in range(0, 5):
  43. check_scan(dev, "freq=2412-2462,5180 use_id=1")
  44. if int(dev.get_bss(bssid)['age']) <= 1:
  45. return
  46. raise Exception("Unexpectedly old BSS entry")
  47. def test_scan(dev, apdev):
  48. """Control interface behavior on scan parameters"""
  49. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  50. bssid = apdev[0]['bssid']
  51. logger.info("Full scan")
  52. check_scan(dev[0], "use_id=1")
  53. logger.info("Limited channel scan")
  54. check_scan_retry(dev[0], "freq=2412-2462,5180 use_id=1", bssid)
  55. # wait long enough to allow next scans to be verified not to find the AP
  56. time.sleep(2)
  57. logger.info("Passive single-channel scan")
  58. check_scan(dev[0], "freq=2457 passive=1 use_id=1")
  59. logger.info("Active single-channel scan")
  60. check_scan(dev[0], "freq=2452 passive=0 use_id=1")
  61. if int(dev[0].get_bss(bssid)['age']) < 2:
  62. raise Exception("Unexpectedly updated BSS entry")
  63. logger.info("Active single-channel scan on AP's operating channel")
  64. check_scan_retry(dev[0], "freq=2412 passive=0 use_id=1", bssid)
  65. def test_scan_only(dev, apdev):
  66. """Control interface behavior on scan parameters with type=only"""
  67. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  68. bssid = apdev[0]['bssid']
  69. logger.info("Full scan")
  70. check_scan(dev[0], "type=only use_id=1")
  71. logger.info("Limited channel scan")
  72. check_scan_retry(dev[0], "type=only freq=2412-2462,5180 use_id=1", bssid)
  73. # wait long enough to allow next scans to be verified not to find the AP
  74. time.sleep(2)
  75. logger.info("Passive single-channel scan")
  76. check_scan(dev[0], "type=only freq=2457 passive=1 use_id=1")
  77. logger.info("Active single-channel scan")
  78. check_scan(dev[0], "type=only freq=2452 passive=0 use_id=1")
  79. if int(dev[0].get_bss(bssid)['age']) < 2:
  80. raise Exception("Unexpectedly updated BSS entry")
  81. logger.info("Active single-channel scan on AP's operating channel")
  82. check_scan_retry(dev[0], "type=only freq=2412 passive=0 use_id=1", bssid)
  83. def test_scan_external_trigger(dev, apdev):
  84. """Avoid operations during externally triggered scan"""
  85. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  86. bssid = apdev[0]['bssid']
  87. subprocess.call(['sudo', 'iw', dev[0].ifname, 'scan', 'trigger'])
  88. check_scan(dev[0], "use_id=1", other_started=True)
  89. def test_scan_bss_expiration_count(dev, apdev):
  90. """BSS entry expiration based on scan results without match"""
  91. if "FAIL" not in dev[0].request("BSS_EXPIRE_COUNT 0"):
  92. raise Exception("Invalid BSS_EXPIRE_COUNT accepted")
  93. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  94. raise Exception("BSS_EXPIRE_COUNT failed")
  95. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  96. bssid = apdev[0]['bssid']
  97. dev[0].scan(freq="2412", only_new=True)
  98. if bssid not in dev[0].request("SCAN_RESULTS"):
  99. raise Exception("BSS not found in initial scan")
  100. hapd.request("DISABLE")
  101. dev[0].scan(freq="2412", only_new=True)
  102. if bssid not in dev[0].request("SCAN_RESULTS"):
  103. raise Exception("BSS not found in first scan without match")
  104. dev[0].scan(freq="2412", only_new=True)
  105. if bssid in dev[0].request("SCAN_RESULTS"):
  106. raise Exception("BSS found after two scans without match")
  107. def test_scan_bss_expiration_age(dev, apdev):
  108. """BSS entry expiration based on age"""
  109. try:
  110. if "FAIL" not in dev[0].request("BSS_EXPIRE_AGE COUNT 9"):
  111. raise Exception("Invalid BSS_EXPIRE_AGE accepted")
  112. if "OK" not in dev[0].request("BSS_EXPIRE_AGE 10"):
  113. raise Exception("BSS_EXPIRE_AGE failed")
  114. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  115. bssid = apdev[0]['bssid']
  116. dev[0].scan(freq="2412")
  117. if bssid not in dev[0].request("SCAN_RESULTS"):
  118. raise Exception("BSS not found in initial scan")
  119. hapd.request("DISABLE")
  120. logger.info("Waiting for BSS entry to expire")
  121. time.sleep(7)
  122. if bssid not in dev[0].request("SCAN_RESULTS"):
  123. raise Exception("BSS expired too quickly")
  124. ev = dev[0].wait_event(["CTRL-EVENT-BSS-REMOVED"], timeout=15)
  125. if ev is None:
  126. raise Exception("BSS entry expiration timed out")
  127. if bssid in dev[0].request("SCAN_RESULTS"):
  128. raise Exception("BSS not removed after expiration time")
  129. finally:
  130. dev[0].request("BSS_EXPIRE_AGE 180")
  131. def test_scan_filter(dev, apdev):
  132. """Filter scan results based on SSID"""
  133. try:
  134. if "OK" not in dev[0].request("SET filter_ssids 1"):
  135. raise Exception("SET failed")
  136. dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  137. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  138. bssid = apdev[0]['bssid']
  139. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test-scan2" })
  140. bssid2 = apdev[1]['bssid']
  141. dev[0].scan(freq="2412", only_new=True)
  142. if bssid not in dev[0].request("SCAN_RESULTS"):
  143. raise Exception("BSS not found in scan results")
  144. if bssid2 in dev[0].request("SCAN_RESULTS"):
  145. raise Exception("Unexpected BSS found in scan results")
  146. finally:
  147. dev[0].request("SET filter_ssids 0")
  148. def test_scan_int(dev, apdev):
  149. """scan interval configuration"""
  150. try:
  151. if "FAIL" not in dev[0].request("SCAN_INTERVAL -1"):
  152. raise Exception("Accepted invalid scan interval")
  153. if "OK" not in dev[0].request("SCAN_INTERVAL 1"):
  154. raise Exception("Failed to set scan interval")
  155. dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
  156. wait_connect=False)
  157. times = {}
  158. for i in range(0, 3):
  159. logger.info("Waiting for scan to start")
  160. start = os.times()[4]
  161. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  162. if ev is None:
  163. raise Exception("did not start a scan")
  164. stop = os.times()[4]
  165. times[i] = stop - start
  166. logger.info("Waiting for scan to complete")
  167. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  168. if ev is None:
  169. raise Exception("did not complete a scan")
  170. print times
  171. if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
  172. raise Exception("Unexpected scan timing: " + str(times))
  173. finally:
  174. dev[0].request("SCAN_INTERVAL 5")
  175. def test_scan_bss_operations(dev, apdev):
  176. """Control interface behavior on BSS parameters"""
  177. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  178. bssid = apdev[0]['bssid']
  179. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test2-scan" })
  180. bssid2 = apdev[1]['bssid']
  181. dev[0].scan(freq="2412")
  182. dev[0].scan(freq="2412")
  183. dev[0].scan(freq="2412")
  184. id1 = dev[0].request("BSS FIRST MASK=0x1").splitlines()[0].split('=')[1]
  185. id2 = dev[0].request("BSS LAST MASK=0x1").splitlines()[0].split('=')[1]
  186. res = dev[0].request("BSS RANGE=ALL MASK=0x20001")
  187. if "id=" + id1 not in res:
  188. raise Exception("Missing BSS " + id1)
  189. if "id=" + id2 not in res:
  190. raise Exception("Missing BSS " + id2)
  191. if "====" not in res:
  192. raise Exception("Missing delim")
  193. if "####" not in res:
  194. raise Exception("Missing end")
  195. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  196. if len(res) != 2:
  197. raise Exception("Unexpected result")
  198. res = dev[0].request("BSS FIRST MASK=0x1")
  199. if "id=" + id1 not in res:
  200. raise Exception("Unexpected result: " + res)
  201. res = dev[0].request("BSS LAST MASK=0x1")
  202. if "id=" + id2 not in res:
  203. raise Exception("Unexpected result: " + res)
  204. res = dev[0].request("BSS ID-" + id1 + " MASK=0x1")
  205. if "id=" + id1 not in res:
  206. raise Exception("Unexpected result: " + res)
  207. res = dev[0].request("BSS NEXT-" + id1 + " MASK=0x1")
  208. if "id=" + id2 not in res:
  209. raise Exception("Unexpected result: " + res)
  210. if len(dev[0].request("BSS RANGE=" + id2 + " MASK=0x1").splitlines()) != 0:
  211. raise Exception("Unexpected RANGE=1 result")
  212. if len(dev[0].request("BSS RANGE=" + id1 + "- MASK=0x1").splitlines()) != 2:
  213. raise Exception("Unexpected RANGE=0- result")
  214. if len(dev[0].request("BSS RANGE=-" + id2 + " MASK=0x1").splitlines()) != 2:
  215. raise Exception("Unexpected RANGE=-1 result")
  216. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id2 + " MASK=0x1").splitlines()) != 2:
  217. raise Exception("Unexpected RANGE=0-1 result")
  218. if len(dev[0].request("BSS RANGE=" + id2 + "-" + id2 + " MASK=0x1").splitlines()) != 1:
  219. raise Exception("Unexpected RANGE=1-1 result")
  220. if len(dev[0].request("BSS RANGE=" + str(int(id2) + 1) + "-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 0:
  221. raise Exception("Unexpected RANGE=2-10 result")
  222. if len(dev[0].request("BSS RANGE=0-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 2:
  223. raise Exception("Unexpected RANGE=0-10 result")
  224. def test_scan_and_interface_disabled(dev, apdev):
  225. """Scan operation when interface gets disabled"""
  226. try:
  227. dev[0].request("SCAN")
  228. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  229. if ev is None:
  230. raise Exception("Scan did not start")
  231. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  232. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=7)
  233. if ev is not None:
  234. raise Exception("Scan completed unexpectedly")
  235. # verify that scan is rejected
  236. if "FAIL" not in dev[0].request("SCAN"):
  237. raise Exception("New scan request was accepted unexpectedly")
  238. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  239. dev[0].scan(freq="2412")
  240. finally:
  241. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  242. def test_scan_for_auth(dev, apdev):
  243. """cfg80211 workaround with scan-for-auth"""
  244. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  245. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  246. # Block sme-connect radio work with an external radio work item, so that
  247. # SELECT_NETWORK can decide to use fast associate without a new scan while
  248. # cfg80211 still has the matching BSS entry, but the actual connection is
  249. # not yet started.
  250. id = dev[0].request("RADIO_WORK add block-work")
  251. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  252. if ev is None:
  253. raise Exception("Timeout while waiting radio work to start")
  254. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  255. wait_connect=False)
  256. # Clear cfg80211 BSS table.
  257. subprocess.call(['sudo', 'iw', dev[0].ifname, 'scan', 'trigger',
  258. 'freq', '2462', 'flush'])
  259. time.sleep(0.1)
  260. # Release blocking radio work to allow connection to go through with the
  261. # cfg80211 BSS entry missing.
  262. dev[0].request("RADIO_WORK done " + id)
  263. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  264. if ev is None:
  265. raise Exception("Association with the AP timed out")
  266. def test_scan_hidden(dev, apdev):
  267. """Control interface behavior on scan parameters"""
  268. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan",
  269. "ignore_broadcast_ssid": "1" })
  270. bssid = apdev[0]['bssid']
  271. check_scan(dev[0], "freq=2412 use_id=1")
  272. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  273. raise Exception("BSS unexpectedly found in initial scan")
  274. id1 = dev[0].connect("foo", key_mgmt="NONE", scan_ssid="1",
  275. only_add_network=True)
  276. id2 = dev[0].connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  277. only_add_network=True)
  278. id3 = dev[0].connect("bar", key_mgmt="NONE", only_add_network=True)
  279. check_scan(dev[0], "freq=2412 use_id=1")
  280. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  281. raise Exception("BSS unexpectedly found in scan")
  282. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id2, id3))
  283. if "test-scan" not in dev[0].request("SCAN_RESULTS"):
  284. raise Exception("BSS not found in scan")
  285. def test_scan_and_bss_entry_removed(dev, apdev):
  286. """Last scan result and connect work processing on BSS entry update"""
  287. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open",
  288. "eap_server": "1",
  289. "wps_state": "2" })
  290. bssid = apdev[0]['bssid']
  291. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  292. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  293. # Add a BSS entry
  294. dev[0].scan_for_bss(bssid, freq="2412")
  295. wpas.scan_for_bss(bssid, freq="2412")
  296. # Start a connect radio work with a blocking entry preventing this from
  297. # proceeding; this stores a pointer to the selected BSS entry.
  298. id = dev[0].request("RADIO_WORK add block-work")
  299. w_id = wpas.request("RADIO_WORK add block-work")
  300. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  301. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  302. nid = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  303. wait_connect=False)
  304. w_nid = wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  305. wait_connect=False)
  306. time.sleep(0.1)
  307. # Remove the BSS entry
  308. dev[0].request("BSS_FLUSH 0")
  309. wpas.request("BSS_FLUSH 0")
  310. # Allow the connect radio work to continue. The bss entry stored in the
  311. # pending connect work is now stale. This will result in the connection
  312. # attempt failing since the BSS entry does not exist.
  313. dev[0].request("RADIO_WORK done " + id)
  314. wpas.request("RADIO_WORK done " + w_id)
  315. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  316. if ev is not None:
  317. raise Exception("Unexpected connection")
  318. dev[0].remove_network(nid)
  319. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  320. if ev is not None:
  321. raise Exception("Unexpected connection")
  322. wpas.remove_network(w_nid)
  323. time.sleep(0.5)
  324. dev[0].request("BSS_FLUSH 0")
  325. wpas.request("BSS_FLUSH 0")
  326. # Add a BSS entry
  327. dev[0].scan_for_bss(bssid, freq="2412")
  328. wpas.scan_for_bss(bssid, freq="2412")
  329. # Start a connect radio work with a blocking entry preventing this from
  330. # proceeding; this stores a pointer to the selected BSS entry.
  331. id = dev[0].request("RADIO_WORK add block-work")
  332. w_id = wpas.request("RADIO_WORK add block-work")
  333. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  334. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  335. # Schedule a connection based on the current BSS entry.
  336. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  337. wait_connect=False)
  338. wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  339. wait_connect=False)
  340. # Update scan results with results that have longer set of IEs so that new
  341. # memory needs to be allocated for the BSS entry.
  342. hapd.request("WPS_PBC")
  343. time.sleep(0.1)
  344. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger', 'freq', '2412'])
  345. subprocess.call(['iw', wpas.ifname, 'scan', 'trigger', 'freq', '2412'])
  346. time.sleep(0.1)
  347. # Allow the connect radio work to continue. The bss entry stored in the
  348. # pending connect work becomes stale during the scan and it must have been
  349. # updated for the connection to work.
  350. dev[0].request("RADIO_WORK done " + id)
  351. wpas.request("RADIO_WORK done " + w_id)
  352. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  353. if ev is None:
  354. raise Exception("No connection (sme-connect)")
  355. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  356. if ev is None:
  357. raise Exception("No connection (connect)")