test_scan.py 30 KB

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