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