test_scan.py 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. # Scanning tests
  2. # Copyright (c) 2013-2016, 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. from remotehost import remote_compatible
  7. import time
  8. import logging
  9. logger = logging.getLogger()
  10. import os
  11. import subprocess
  12. import hostapd
  13. from wpasupplicant import WpaSupplicant
  14. from utils import HwsimSkip, fail_test, alloc_fail, wait_fail_trigger
  15. from tshark import run_tshark
  16. from test_ap_csa import switch_channel, wait_channel_switch, csa_supported
  17. def check_scan(dev, params, other_started=False, test_busy=False):
  18. if not other_started:
  19. dev.dump_monitor()
  20. id = dev.request("SCAN " + params)
  21. if "FAIL" in id:
  22. raise Exception("Failed to start scan")
  23. id = int(id)
  24. if test_busy:
  25. if "FAIL-BUSY" not in dev.request("SCAN"):
  26. raise Exception("SCAN command while already scanning not rejected")
  27. if other_started:
  28. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  29. if ev is None:
  30. raise Exception("Other scan did not start")
  31. if "id=" + str(id) in ev:
  32. raise Exception("Own scan id unexpectedly included in start event")
  33. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  34. if ev is None:
  35. raise Exception("Other scan did not complete")
  36. if "id=" + str(id) in ev:
  37. raise Exception("Own scan id unexpectedly included in completed event")
  38. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  39. if ev is None:
  40. raise Exception("Scan did not start")
  41. if "id=" + str(id) not in ev:
  42. raise Exception("Scan id not included in start event")
  43. if test_busy:
  44. if "FAIL-BUSY" not in dev.request("SCAN"):
  45. raise Exception("SCAN command while already scanning not rejected")
  46. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  47. if ev is None:
  48. raise Exception("Scan did not complete")
  49. if "id=" + str(id) not in ev:
  50. raise Exception("Scan id not included in completed event")
  51. def check_scan_retry(dev, params, bssid):
  52. for i in range(0, 5):
  53. check_scan(dev, "freq=2412-2462,5180 use_id=1")
  54. if int(dev.get_bss(bssid)['age']) <= 1:
  55. return
  56. raise Exception("Unexpectedly old BSS entry")
  57. @remote_compatible
  58. def test_scan(dev, apdev):
  59. """Control interface behavior on scan parameters"""
  60. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  61. bssid = apdev[0]['bssid']
  62. logger.info("Full scan")
  63. check_scan(dev[0], "use_id=1", test_busy=True)
  64. logger.info("Limited channel scan")
  65. check_scan_retry(dev[0], "freq=2412-2462,5180 use_id=1", bssid)
  66. # wait long enough to allow next scans to be verified not to find the AP
  67. time.sleep(2)
  68. logger.info("Passive single-channel scan")
  69. check_scan(dev[0], "freq=2457 passive=1 use_id=1")
  70. logger.info("Active single-channel scan")
  71. check_scan(dev[0], "freq=2452 passive=0 use_id=1")
  72. if int(dev[0].get_bss(bssid)['age']) < 2:
  73. raise Exception("Unexpectedly updated BSS entry")
  74. logger.info("Active single-channel scan on AP's operating channel")
  75. check_scan_retry(dev[0], "freq=2412 passive=0 use_id=1", bssid)
  76. @remote_compatible
  77. def test_scan_tsf(dev, apdev):
  78. """Scan and TSF updates from Beacon/Probe Response frames"""
  79. hostapd.add_ap(apdev[0], { "ssid": "test-scan",
  80. 'beacon_int': "100" })
  81. bssid = apdev[0]['bssid']
  82. tsf = []
  83. for passive in [ 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1 ]:
  84. check_scan(dev[0], "freq=2412 passive=%d use_id=1" % passive)
  85. bss = dev[0].get_bss(bssid)
  86. if bss:
  87. tsf.append(int(bss['tsf']))
  88. logger.info("TSF: " + bss['tsf'])
  89. if tsf[-3] <= tsf[-4]:
  90. # For now, only write this in the log without failing the test case
  91. # since mac80211_hwsim does not yet update the Timestamp field in
  92. # Probe Response frames.
  93. logger.info("Probe Response did not update TSF")
  94. #raise Exception("Probe Response did not update TSF")
  95. if tsf[-1] <= tsf[-3]:
  96. raise Exception("Beacon did not update TSF")
  97. if 0 in tsf:
  98. raise Exception("0 TSF reported")
  99. @remote_compatible
  100. def test_scan_only(dev, apdev):
  101. """Control interface behavior on scan parameters with type=only"""
  102. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  103. bssid = apdev[0]['bssid']
  104. logger.info("Full scan")
  105. check_scan(dev[0], "type=only use_id=1")
  106. logger.info("Limited channel scan")
  107. check_scan_retry(dev[0], "type=only freq=2412-2462,5180 use_id=1", bssid)
  108. # wait long enough to allow next scans to be verified not to find the AP
  109. time.sleep(2)
  110. logger.info("Passive single-channel scan")
  111. check_scan(dev[0], "type=only freq=2457 passive=1 use_id=1")
  112. logger.info("Active single-channel scan")
  113. check_scan(dev[0], "type=only freq=2452 passive=0 use_id=1")
  114. if int(dev[0].get_bss(bssid)['age']) < 2:
  115. raise Exception("Unexpectedly updated BSS entry")
  116. logger.info("Active single-channel scan on AP's operating channel")
  117. check_scan_retry(dev[0], "type=only freq=2412 passive=0 use_id=1", bssid)
  118. @remote_compatible
  119. def test_scan_external_trigger(dev, apdev):
  120. """Avoid operations during externally triggered scan"""
  121. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  122. bssid = apdev[0]['bssid']
  123. dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger'])
  124. check_scan(dev[0], "use_id=1", other_started=True)
  125. def test_scan_bss_expiration_count(dev, apdev):
  126. """BSS entry expiration based on scan results without match"""
  127. if "FAIL" not in dev[0].request("BSS_EXPIRE_COUNT 0"):
  128. raise Exception("Invalid BSS_EXPIRE_COUNT accepted")
  129. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  130. raise Exception("BSS_EXPIRE_COUNT failed")
  131. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  132. bssid = apdev[0]['bssid']
  133. dev[0].scan(freq="2412", only_new=True)
  134. if bssid not in dev[0].request("SCAN_RESULTS"):
  135. raise Exception("BSS not found in initial scan")
  136. hapd.request("DISABLE")
  137. dev[0].scan(freq="2412", only_new=True)
  138. if bssid not in dev[0].request("SCAN_RESULTS"):
  139. raise Exception("BSS not found in first scan without match")
  140. dev[0].scan(freq="2412", only_new=True)
  141. if bssid in dev[0].request("SCAN_RESULTS"):
  142. raise Exception("BSS found after two scans without match")
  143. @remote_compatible
  144. def test_scan_bss_expiration_age(dev, apdev):
  145. """BSS entry expiration based on age"""
  146. try:
  147. if "FAIL" not in dev[0].request("BSS_EXPIRE_AGE COUNT 9"):
  148. raise Exception("Invalid BSS_EXPIRE_AGE accepted")
  149. if "OK" not in dev[0].request("BSS_EXPIRE_AGE 10"):
  150. raise Exception("BSS_EXPIRE_AGE failed")
  151. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  152. bssid = apdev[0]['bssid']
  153. # Allow couple more retries to avoid reporting errors during heavy load
  154. for i in range(5):
  155. dev[0].scan(freq="2412")
  156. if bssid in dev[0].request("SCAN_RESULTS"):
  157. break
  158. if bssid not in dev[0].request("SCAN_RESULTS"):
  159. raise Exception("BSS not found in initial scan")
  160. hapd.request("DISABLE")
  161. logger.info("Waiting for BSS entry to expire")
  162. time.sleep(7)
  163. if bssid not in dev[0].request("SCAN_RESULTS"):
  164. raise Exception("BSS expired too quickly")
  165. ev = dev[0].wait_event(["CTRL-EVENT-BSS-REMOVED"], timeout=15)
  166. if ev is None:
  167. raise Exception("BSS entry expiration timed out")
  168. if bssid in dev[0].request("SCAN_RESULTS"):
  169. raise Exception("BSS not removed after expiration time")
  170. finally:
  171. dev[0].request("BSS_EXPIRE_AGE 180")
  172. @remote_compatible
  173. def test_scan_filter(dev, apdev):
  174. """Filter scan results based on SSID"""
  175. try:
  176. if "OK" not in dev[0].request("SET filter_ssids 1"):
  177. raise Exception("SET failed")
  178. id = dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  179. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  180. bssid = apdev[0]['bssid']
  181. hostapd.add_ap(apdev[1], { "ssid": "test-scan2" })
  182. bssid2 = apdev[1]['bssid']
  183. dev[0].scan(freq="2412", only_new=True)
  184. if bssid not in dev[0].request("SCAN_RESULTS"):
  185. raise Exception("BSS not found in scan results")
  186. if bssid2 in dev[0].request("SCAN_RESULTS"):
  187. raise Exception("Unexpected BSS found in scan results")
  188. dev[0].set_network_quoted(id, "ssid", "")
  189. dev[0].scan(freq="2412")
  190. id2 = dev[0].connect("test", key_mgmt="NONE", only_add_network=True)
  191. dev[0].scan(freq="2412")
  192. finally:
  193. dev[0].request("SET filter_ssids 0")
  194. @remote_compatible
  195. def test_scan_int(dev, apdev):
  196. """scan interval configuration"""
  197. try:
  198. if "FAIL" not in dev[0].request("SCAN_INTERVAL -1"):
  199. raise Exception("Accepted invalid scan interval")
  200. if "OK" not in dev[0].request("SCAN_INTERVAL 1"):
  201. raise Exception("Failed to set scan interval")
  202. dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
  203. wait_connect=False)
  204. times = {}
  205. for i in range(0, 3):
  206. logger.info("Waiting for scan to start")
  207. start = os.times()[4]
  208. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  209. if ev is None:
  210. raise Exception("did not start a scan")
  211. stop = os.times()[4]
  212. times[i] = stop - start
  213. logger.info("Waiting for scan to complete")
  214. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  215. if ev is None:
  216. raise Exception("did not complete a scan")
  217. logger.info("times=" + str(times))
  218. if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
  219. raise Exception("Unexpected scan timing: " + str(times))
  220. finally:
  221. dev[0].request("SCAN_INTERVAL 5")
  222. def test_scan_bss_operations(dev, apdev):
  223. """Control interface behavior on BSS parameters"""
  224. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  225. bssid = apdev[0]['bssid']
  226. hostapd.add_ap(apdev[1], { "ssid": "test2-scan" })
  227. bssid2 = apdev[1]['bssid']
  228. dev[0].scan(freq="2412")
  229. dev[0].scan(freq="2412")
  230. dev[0].scan(freq="2412")
  231. id1 = dev[0].request("BSS FIRST MASK=0x1").splitlines()[0].split('=')[1]
  232. id2 = dev[0].request("BSS LAST MASK=0x1").splitlines()[0].split('=')[1]
  233. res = dev[0].request("BSS RANGE=ALL MASK=0x20001")
  234. if "id=" + id1 not in res:
  235. raise Exception("Missing BSS " + id1)
  236. if "id=" + id2 not in res:
  237. raise Exception("Missing BSS " + id2)
  238. if "====" not in res:
  239. raise Exception("Missing delim")
  240. if "####" not in res:
  241. raise Exception("Missing end")
  242. res = dev[0].request("BSS RANGE=ALL MASK=0")
  243. if "id=" + id1 not in res:
  244. raise Exception("Missing BSS " + id1)
  245. if "id=" + id2 not in res:
  246. raise Exception("Missing BSS " + id2)
  247. if "====" in res:
  248. raise Exception("Unexpected delim")
  249. if "####" in res:
  250. raise Exception("Unexpected end delim")
  251. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  252. if len(res) != 2:
  253. raise Exception("Unexpected result: " + str(res))
  254. res = dev[0].request("BSS FIRST MASK=0x1")
  255. if "id=" + id1 not in res:
  256. raise Exception("Unexpected result: " + res)
  257. res = dev[0].request("BSS LAST MASK=0x1")
  258. if "id=" + id2 not in res:
  259. raise Exception("Unexpected result: " + res)
  260. res = dev[0].request("BSS ID-" + id1 + " MASK=0x1")
  261. if "id=" + id1 not in res:
  262. raise Exception("Unexpected result: " + res)
  263. res = dev[0].request("BSS NEXT-" + id1 + " MASK=0x1")
  264. if "id=" + id2 not in res:
  265. raise Exception("Unexpected result: " + res)
  266. res = dev[0].request("BSS NEXT-" + id2 + " MASK=0x1")
  267. if "id=" in res:
  268. raise Exception("Unexpected result: " + res)
  269. if len(dev[0].request("BSS RANGE=" + id2 + " MASK=0x1").splitlines()) != 0:
  270. raise Exception("Unexpected RANGE=1 result")
  271. if len(dev[0].request("BSS RANGE=" + id1 + "- MASK=0x1").splitlines()) != 2:
  272. raise Exception("Unexpected RANGE=0- result")
  273. if len(dev[0].request("BSS RANGE=-" + id2 + " MASK=0x1").splitlines()) != 2:
  274. raise Exception("Unexpected RANGE=-1 result")
  275. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id2 + " MASK=0x1").splitlines()) != 2:
  276. raise Exception("Unexpected RANGE=0-1 result")
  277. if len(dev[0].request("BSS RANGE=" + id2 + "-" + id2 + " MASK=0x1").splitlines()) != 1:
  278. raise Exception("Unexpected RANGE=1-1 result")
  279. if len(dev[0].request("BSS RANGE=" + str(int(id2) + 1) + "-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 0:
  280. raise Exception("Unexpected RANGE=2-10 result")
  281. if len(dev[0].request("BSS RANGE=0-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 2:
  282. raise Exception("Unexpected RANGE=0-10 result")
  283. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id1 + " MASK=0x1").splitlines()) != 1:
  284. raise Exception("Unexpected RANGE=0-0 result")
  285. res = dev[0].request("BSS p2p_dev_addr=FOO")
  286. if "FAIL" in res or "id=" in res:
  287. raise Exception("Unexpected result: " + res)
  288. res = dev[0].request("BSS p2p_dev_addr=00:11:22:33:44:55")
  289. if "FAIL" in res or "id=" in res:
  290. raise Exception("Unexpected result: " + res)
  291. dev[0].request("BSS_FLUSH 1000")
  292. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  293. if len(res) != 2:
  294. raise Exception("Unexpected result after BSS_FLUSH 1000")
  295. dev[0].request("BSS_FLUSH 0")
  296. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  297. if len(res) != 0:
  298. raise Exception("Unexpected result after BSS_FLUSH 0")
  299. @remote_compatible
  300. def test_scan_and_interface_disabled(dev, apdev):
  301. """Scan operation when interface gets disabled"""
  302. try:
  303. dev[0].request("SCAN")
  304. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  305. if ev is None:
  306. raise Exception("Scan did not start")
  307. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  308. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=7)
  309. if ev is not None:
  310. raise Exception("Scan completed unexpectedly")
  311. # verify that scan is rejected
  312. if "FAIL" not in dev[0].request("SCAN"):
  313. raise Exception("New scan request was accepted unexpectedly")
  314. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  315. dev[0].scan(freq="2412")
  316. finally:
  317. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  318. @remote_compatible
  319. def test_scan_for_auth(dev, apdev):
  320. """cfg80211 workaround with scan-for-auth"""
  321. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  322. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  323. # Block sme-connect radio work with an external radio work item, so that
  324. # SELECT_NETWORK can decide to use fast associate without a new scan while
  325. # cfg80211 still has the matching BSS entry, but the actual connection is
  326. # not yet started.
  327. id = dev[0].request("RADIO_WORK add block-work")
  328. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  329. if ev is None:
  330. raise Exception("Timeout while waiting radio work to start")
  331. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  332. wait_connect=False)
  333. dev[0].dump_monitor()
  334. # Clear cfg80211 BSS table.
  335. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger',
  336. 'freq', '2457', 'flush'])
  337. if res != 0:
  338. raise HwsimSkip("iw scan trigger flush not supported")
  339. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  340. if ev is None:
  341. raise Exception("External flush scan timed out")
  342. # Release blocking radio work to allow connection to go through with the
  343. # cfg80211 BSS entry missing.
  344. dev[0].request("RADIO_WORK done " + id)
  345. dev[0].wait_connected(timeout=15)
  346. @remote_compatible
  347. def test_scan_for_auth_fail(dev, apdev):
  348. """cfg80211 workaround with scan-for-auth failing"""
  349. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  350. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  351. # Block sme-connect radio work with an external radio work item, so that
  352. # SELECT_NETWORK can decide to use fast associate without a new scan while
  353. # cfg80211 still has the matching BSS entry, but the actual connection is
  354. # not yet started.
  355. id = dev[0].request("RADIO_WORK add block-work")
  356. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  357. if ev is None:
  358. raise Exception("Timeout while waiting radio work to start")
  359. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  360. wait_connect=False)
  361. dev[0].dump_monitor()
  362. hapd.disable()
  363. # Clear cfg80211 BSS table.
  364. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger',
  365. 'freq', '2457', 'flush'])
  366. if res != 0:
  367. raise HwsimSkip("iw scan trigger flush not supported")
  368. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  369. if ev is None:
  370. raise Exception("External flush scan timed out")
  371. # Release blocking radio work to allow connection to go through with the
  372. # cfg80211 BSS entry missing.
  373. dev[0].request("RADIO_WORK done " + id)
  374. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS",
  375. "CTRL-EVENT-CONNECTED"], 15)
  376. if ev is None:
  377. raise Exception("Scan event missing")
  378. if "CTRL-EVENT-CONNECTED" in ev:
  379. raise Exception("Unexpected connection")
  380. dev[0].request("DISCONNECT")
  381. @remote_compatible
  382. def test_scan_for_auth_wep(dev, apdev):
  383. """cfg80211 scan-for-auth workaround with WEP keys"""
  384. dev[0].flush_scan_cache()
  385. hapd = hostapd.add_ap(apdev[0],
  386. { "ssid": "wep", "wep_key0": '"abcde"',
  387. "auth_algs": "2" })
  388. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  389. # Block sme-connect radio work with an external radio work item, so that
  390. # SELECT_NETWORK can decide to use fast associate without a new scan while
  391. # cfg80211 still has the matching BSS entry, but the actual connection is
  392. # not yet started.
  393. id = dev[0].request("RADIO_WORK add block-work")
  394. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  395. if ev is None:
  396. raise Exception("Timeout while waiting radio work to start")
  397. dev[0].connect("wep", key_mgmt="NONE", wep_key0='"abcde"',
  398. auth_alg="SHARED", scan_freq="2412", wait_connect=False)
  399. dev[0].dump_monitor()
  400. # Clear cfg80211 BSS table.
  401. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'scan', 'trigger',
  402. 'freq', '2457', 'flush'])
  403. if res != 0:
  404. raise HwsimSkip("iw scan trigger flush not supported")
  405. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  406. if ev is None:
  407. raise Exception("External flush scan timed out")
  408. # Release blocking radio work to allow connection to go through with the
  409. # cfg80211 BSS entry missing.
  410. dev[0].request("RADIO_WORK done " + id)
  411. dev[0].wait_connected(timeout=15)
  412. @remote_compatible
  413. def test_scan_hidden(dev, apdev):
  414. """Control interface behavior on scan parameters"""
  415. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan",
  416. "ignore_broadcast_ssid": "1" })
  417. bssid = apdev[0]['bssid']
  418. check_scan(dev[0], "freq=2412 use_id=1")
  419. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  420. raise Exception("BSS unexpectedly found in initial scan")
  421. id1 = dev[0].connect("foo", key_mgmt="NONE", scan_ssid="1",
  422. only_add_network=True)
  423. id2 = dev[0].connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  424. only_add_network=True)
  425. id3 = dev[0].connect("bar", key_mgmt="NONE", only_add_network=True)
  426. check_scan(dev[0], "freq=2412 use_id=1")
  427. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  428. raise Exception("BSS unexpectedly found in scan")
  429. # Allow multiple attempts to be more robust under heavy CPU load that can
  430. # result in Probe Response frames getting sent only after the station has
  431. # already stopped waiting for the response on the channel.
  432. found = False
  433. for i in range(10):
  434. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id2, id3))
  435. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  436. found = True
  437. break
  438. if not found:
  439. raise Exception("BSS not found in scan")
  440. 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"):
  441. raise Exception("Too many scan_id values accepted")
  442. # Duplicate SSID removal
  443. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id1, id2))
  444. dev[0].request("REMOVE_NETWORK all")
  445. hapd.disable()
  446. dev[0].flush_scan_cache(freq=2432)
  447. dev[0].flush_scan_cache()
  448. def test_scan_and_bss_entry_removed(dev, apdev):
  449. """Last scan result and connect work processing on BSS entry update"""
  450. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  451. "eap_server": "1",
  452. "wps_state": "2" })
  453. bssid = apdev[0]['bssid']
  454. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  455. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  456. # Add a BSS entry
  457. dev[0].scan_for_bss(bssid, freq="2412")
  458. wpas.scan_for_bss(bssid, freq="2412")
  459. # Start a connect radio work with a blocking entry preventing this from
  460. # proceeding; this stores a pointer to the selected BSS entry.
  461. id = dev[0].request("RADIO_WORK add block-work")
  462. w_id = wpas.request("RADIO_WORK add block-work")
  463. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  464. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  465. nid = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  466. wait_connect=False)
  467. w_nid = wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  468. wait_connect=False)
  469. time.sleep(0.1)
  470. # Remove the BSS entry
  471. dev[0].request("BSS_FLUSH 0")
  472. wpas.request("BSS_FLUSH 0")
  473. # Allow the connect radio work to continue. The bss entry stored in the
  474. # pending connect work is now stale. This will result in the connection
  475. # attempt failing since the BSS entry does not exist.
  476. dev[0].request("RADIO_WORK done " + id)
  477. wpas.request("RADIO_WORK done " + w_id)
  478. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  479. if ev is not None:
  480. raise Exception("Unexpected connection")
  481. dev[0].remove_network(nid)
  482. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  483. if ev is not None:
  484. raise Exception("Unexpected connection")
  485. wpas.remove_network(w_nid)
  486. time.sleep(0.5)
  487. dev[0].request("BSS_FLUSH 0")
  488. wpas.request("BSS_FLUSH 0")
  489. # Add a BSS entry
  490. dev[0].scan_for_bss(bssid, freq="2412")
  491. wpas.scan_for_bss(bssid, freq="2412")
  492. # Start a connect radio work with a blocking entry preventing this from
  493. # proceeding; this stores a pointer to the selected BSS entry.
  494. id = dev[0].request("RADIO_WORK add block-work")
  495. w_id = wpas.request("RADIO_WORK add block-work")
  496. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  497. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  498. # Schedule a connection based on the current BSS entry.
  499. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  500. wait_connect=False)
  501. wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  502. wait_connect=False)
  503. # Update scan results with results that have longer set of IEs so that new
  504. # memory needs to be allocated for the BSS entry.
  505. hapd.request("WPS_PBC")
  506. time.sleep(0.1)
  507. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger', 'freq', '2412'])
  508. subprocess.call(['iw', wpas.ifname, 'scan', 'trigger', 'freq', '2412'])
  509. time.sleep(0.1)
  510. # Allow the connect radio work to continue. The bss entry stored in the
  511. # pending connect work becomes stale during the scan and it must have been
  512. # updated for the connection to work.
  513. dev[0].request("RADIO_WORK done " + id)
  514. wpas.request("RADIO_WORK done " + w_id)
  515. dev[0].wait_connected(timeout=15, error="No connection (sme-connect)")
  516. wpas.wait_connected(timeout=15, error="No connection (connect)")
  517. dev[0].request("DISCONNECT")
  518. wpas.request("DISCONNECT")
  519. dev[0].flush_scan_cache()
  520. wpas.flush_scan_cache()
  521. @remote_compatible
  522. def test_scan_reqs_with_non_scan_radio_work(dev, apdev):
  523. """SCAN commands while non-scan radio_work is in progress"""
  524. id = dev[0].request("RADIO_WORK add test-work-a")
  525. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  526. if ev is None:
  527. raise Exception("Timeout while waiting radio work to start")
  528. if "OK" not in dev[0].request("SCAN"):
  529. raise Exception("SCAN failed")
  530. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  531. raise Exception("SCAN accepted while one is already pending")
  532. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  533. raise Exception("SCAN accepted while one is already pending")
  534. res = dev[0].request("RADIO_WORK show").splitlines()
  535. count = 0
  536. for l in res:
  537. if "scan" in l:
  538. count += 1
  539. if count != 1:
  540. logger.info(res)
  541. raise Exception("Unexpected number of scan radio work items")
  542. dev[0].dump_monitor()
  543. dev[0].request("RADIO_WORK done " + id)
  544. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  545. if ev is None:
  546. raise Exception("Scan did not start")
  547. if "FAIL-BUSY" not in dev[0].request("SCAN"):
  548. raise Exception("SCAN accepted while one is already in progress")
  549. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  550. if ev is None:
  551. print "Scan did not complete"
  552. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.2)
  553. if ev is not None:
  554. raise Exception("Unexpected scan started")
  555. def test_scan_setband(dev, apdev):
  556. """Band selection for scan operations"""
  557. try:
  558. hapd = None
  559. hapd2 = None
  560. params = { "ssid": "test-setband",
  561. "hw_mode": "a",
  562. "channel": "36",
  563. "country_code": "US" }
  564. hapd = hostapd.add_ap(apdev[0], params)
  565. bssid = apdev[0]['bssid']
  566. params = { "ssid": "test-setband",
  567. "hw_mode": "g",
  568. "channel": "1" }
  569. hapd2 = hostapd.add_ap(apdev[1], params)
  570. bssid2 = apdev[1]['bssid']
  571. if "FAIL" not in dev[0].request("SET setband FOO"):
  572. raise Exception("Invalid set setband accepted")
  573. if "OK" not in dev[0].request("SET setband AUTO"):
  574. raise Exception("Failed to set setband")
  575. if "OK" not in dev[1].request("SET setband 5G"):
  576. raise Exception("Failed to set setband")
  577. if "OK" not in dev[2].request("SET setband 2G"):
  578. raise Exception("Failed to set setband")
  579. # Allow a retry to avoid reporting errors during heavy load
  580. for j in range(5):
  581. for i in range(3):
  582. dev[i].request("SCAN only_new=1")
  583. for i in range(3):
  584. ev = dev[i].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
  585. if ev is None:
  586. raise Exception("Scan timed out")
  587. res0 = dev[0].request("SCAN_RESULTS")
  588. res1 = dev[1].request("SCAN_RESULTS")
  589. res2 = dev[2].request("SCAN_RESULTS")
  590. if bssid in res0 and bssid2 in res0 and bssid in res1 and bssid2 in res2:
  591. break
  592. res = dev[0].request("SCAN_RESULTS")
  593. if bssid not in res or bssid2 not in res:
  594. raise Exception("Missing scan result(0)")
  595. res = dev[1].request("SCAN_RESULTS")
  596. if bssid not in res:
  597. raise Exception("Missing scan result(1)")
  598. if bssid2 in res:
  599. raise Exception("Unexpected scan result(1)")
  600. res = dev[2].request("SCAN_RESULTS")
  601. if bssid2 not in res:
  602. raise Exception("Missing scan result(2)")
  603. if bssid in res:
  604. raise Exception("Unexpected scan result(2)")
  605. finally:
  606. if hapd:
  607. hapd.request("DISABLE")
  608. if hapd2:
  609. hapd2.request("DISABLE")
  610. subprocess.call(['iw', 'reg', 'set', '00'])
  611. for i in range(3):
  612. dev[i].request("SET setband AUTO")
  613. dev[i].flush_scan_cache()
  614. @remote_compatible
  615. def test_scan_hidden_many(dev, apdev):
  616. """scan_ssid=1 with large number of profile with hidden SSID"""
  617. try:
  618. _test_scan_hidden_many(dev, apdev)
  619. finally:
  620. dev[0].flush_scan_cache(freq=2432)
  621. dev[0].flush_scan_cache()
  622. dev[0].request("SCAN_INTERVAL 5")
  623. def _test_scan_hidden_many(dev, apdev):
  624. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan-ssid",
  625. "ignore_broadcast_ssid": "1" })
  626. bssid = apdev[0]['bssid']
  627. dev[0].request("SCAN_INTERVAL 1")
  628. for i in range(5):
  629. id = dev[0].add_network()
  630. dev[0].set_network_quoted(id, "ssid", "foo")
  631. dev[0].set_network(id, "key_mgmt", "NONE")
  632. dev[0].set_network(id, "disabled", "0")
  633. dev[0].set_network(id, "scan_freq", "2412")
  634. dev[0].set_network(id, "scan_ssid", "1")
  635. dev[0].set_network_quoted(id, "ssid", "test-scan-ssid")
  636. dev[0].set_network(id, "key_mgmt", "NONE")
  637. dev[0].set_network(id, "disabled", "0")
  638. dev[0].set_network(id, "scan_freq", "2412")
  639. dev[0].set_network(id, "scan_ssid", "1")
  640. for i in range(5):
  641. id = dev[0].add_network()
  642. dev[0].set_network_quoted(id, "ssid", "foo")
  643. dev[0].set_network(id, "key_mgmt", "NONE")
  644. dev[0].set_network(id, "disabled", "0")
  645. dev[0].set_network(id, "scan_freq", "2412")
  646. dev[0].set_network(id, "scan_ssid", "1")
  647. dev[0].request("REASSOCIATE")
  648. dev[0].wait_connected(timeout=30)
  649. dev[0].request("REMOVE_NETWORK all")
  650. hapd.disable()
  651. def test_scan_random_mac(dev, apdev, params):
  652. """Random MAC address in scans"""
  653. try:
  654. _test_scan_random_mac(dev, apdev, params)
  655. finally:
  656. dev[0].request("MAC_RAND_SCAN all enable=0")
  657. def _test_scan_random_mac(dev, apdev, params):
  658. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  659. bssid = apdev[0]['bssid']
  660. tests = [ "",
  661. "addr=foo",
  662. "mask=foo",
  663. "enable=1",
  664. "all enable=1 mask=00:11:22:33:44:55",
  665. "all enable=1 addr=00:11:22:33:44:55",
  666. "all enable=1 addr=01:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  667. "all enable=1 addr=00:11:22:33:44:55 mask=fe:ff:ff:ff:ff:ff",
  668. "enable=2 scan sched pno all",
  669. "pno enable=1",
  670. "all enable=2",
  671. "foo" ]
  672. for args in tests:
  673. if "FAIL" not in dev[0].request("MAC_RAND_SCAN " + args):
  674. raise Exception("Invalid MAC_RAND_SCAN accepted: " + args)
  675. if dev[0].get_driver_status_field('capa.mac_addr_rand_scan_supported') != '1':
  676. raise HwsimSkip("Driver does not support random MAC address for scanning")
  677. tests = [ "all enable=1",
  678. "all enable=1 addr=f2:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
  679. "all enable=1 addr=f2:11:33:00:00:00 mask=ff:ff:ff:00:00:00" ]
  680. for args in tests:
  681. dev[0].request("MAC_RAND_SCAN " + args)
  682. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  683. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  684. "wlan.fc.type_subtype == 4", ["wlan.ta" ])
  685. if out is not None:
  686. addr = out.splitlines()
  687. logger.info("Probe Request frames seen from: " + str(addr))
  688. if dev[0].own_addr() in addr:
  689. raise Exception("Real address used to transmit Probe Request frame")
  690. if "f2:11:22:33:44:55" not in addr:
  691. raise Exception("Fully configured random address not seen")
  692. found = False
  693. for a in addr:
  694. if a.startswith('f2:11:33'):
  695. found = True
  696. break
  697. if not found:
  698. raise Exception("Fixed OUI random address not seen")
  699. def test_scan_random_mac_connected(dev, apdev, params):
  700. """Random MAC address in scans while connected"""
  701. try:
  702. _test_scan_random_mac_connected(dev, apdev, params)
  703. finally:
  704. dev[0].request("MAC_RAND_SCAN all enable=0")
  705. def _test_scan_random_mac_connected(dev, apdev, params):
  706. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  707. bssid = apdev[0]['bssid']
  708. if dev[0].get_driver_status_field('capa.mac_addr_rand_scan_supported') != '1':
  709. raise HwsimSkip("Driver does not support random MAC address for scanning")
  710. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412")
  711. hostapd.add_ap(apdev[1], { "ssid": "test-scan-2", "channel": "11" })
  712. bssid1 = apdev[1]['bssid']
  713. # Verify that scanning can be completed while connected even if that means
  714. # disabling use of random MAC address.
  715. dev[0].request("MAC_RAND_SCAN all enable=1")
  716. dev[0].scan_for_bss(bssid1, freq=2462, force_scan=True)
  717. @remote_compatible
  718. def test_scan_trigger_failure(dev, apdev):
  719. """Scan trigger to the driver failing"""
  720. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  721. bssid = apdev[0]['bssid']
  722. if "OK" not in dev[0].request("SET test_failure 1"):
  723. raise Exception("Failed to set test_failure")
  724. if "OK" not in dev[0].request("SCAN"):
  725. raise Exception("SCAN command failed")
  726. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  727. if ev is None:
  728. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  729. if "retry=1" in ev:
  730. raise Exception("Unexpected scan retry indicated")
  731. if dev[0].get_status_field('wpa_state') == "SCANNING":
  732. raise Exception("wpa_state SCANNING not cleared")
  733. id = dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412",
  734. only_add_network=True)
  735. dev[0].select_network(id)
  736. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  737. if ev is None:
  738. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  739. if "retry=1" not in ev:
  740. raise Exception("No scan retry indicated for connection")
  741. if dev[0].get_status_field('wpa_state') == "SCANNING":
  742. raise Exception("wpa_state SCANNING not cleared")
  743. dev[0].request("SET test_failure 0")
  744. dev[0].wait_connected()
  745. dev[0].request("SET test_failure 1")
  746. if "OK" not in dev[0].request("SCAN"):
  747. raise Exception("SCAN command failed")
  748. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  749. if ev is None:
  750. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  751. if "retry=1" in ev:
  752. raise Exception("Unexpected scan retry indicated")
  753. if dev[0].get_status_field('wpa_state') != "COMPLETED":
  754. raise Exception("wpa_state COMPLETED not restored")
  755. dev[0].request("SET test_failure 0")
  756. @remote_compatible
  757. def test_scan_specify_ssid(dev, apdev):
  758. """Control interface behavior on scan SSID parameter"""
  759. dev[0].flush_scan_cache()
  760. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-hidden",
  761. "ignore_broadcast_ssid": "1" })
  762. bssid = apdev[0]['bssid']
  763. check_scan(dev[0], "freq=2412 use_id=1 ssid 414243")
  764. bss = dev[0].get_bss(bssid)
  765. if bss is not None and bss['ssid'] == 'test-hidden':
  766. raise Exception("BSS entry for hidden AP present unexpectedly")
  767. # Allow couple more retries to avoid reporting errors during heavy load
  768. for i in range(5):
  769. check_scan(dev[0], "freq=2412 ssid 414243 ssid 746573742d68696464656e ssid 616263313233 use_id=1")
  770. bss = dev[0].get_bss(bssid)
  771. if bss and 'test-hidden' in dev[0].request("SCAN_RESULTS"):
  772. break
  773. if bss is None:
  774. raise Exception("BSS entry for hidden AP not found")
  775. if 'test-hidden' not in dev[0].request("SCAN_RESULTS"):
  776. raise Exception("Expected SSID not included in the scan results")
  777. hapd.disable()
  778. dev[0].flush_scan_cache(freq=2432)
  779. dev[0].flush_scan_cache()
  780. if "FAIL" not in dev[0].request("SCAN ssid foo"):
  781. raise Exception("Invalid SCAN command accepted")
  782. @remote_compatible
  783. def test_scan_ap_scan_2_ap_mode(dev, apdev):
  784. """AP_SCAN 2 AP mode and scan()"""
  785. try:
  786. _test_scan_ap_scan_2_ap_mode(dev, apdev)
  787. finally:
  788. dev[0].request("AP_SCAN 1")
  789. def _test_scan_ap_scan_2_ap_mode(dev, apdev):
  790. if "OK" not in dev[0].request("AP_SCAN 2"):
  791. raise Exception("Failed to set AP_SCAN 2")
  792. id = dev[0].add_network()
  793. dev[0].set_network(id, "mode", "2")
  794. dev[0].set_network_quoted(id, "ssid", "wpas-ap-open")
  795. dev[0].set_network(id, "key_mgmt", "NONE")
  796. dev[0].set_network(id, "frequency", "2412")
  797. dev[0].set_network(id, "scan_freq", "2412")
  798. dev[0].set_network(id, "disabled", "0")
  799. dev[0].select_network(id)
  800. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=5)
  801. if ev is None:
  802. raise Exception("AP failed to start")
  803. with fail_test(dev[0], 1, "wpa_driver_nl80211_scan"):
  804. if "OK" not in dev[0].request("SCAN freq=2412"):
  805. raise Exception("SCAN command failed unexpectedly")
  806. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED",
  807. "AP-DISABLED"], timeout=5)
  808. if ev is None:
  809. raise Exception("CTRL-EVENT-SCAN-FAILED not seen")
  810. if "AP-DISABLED" in ev:
  811. raise Exception("Unexpected AP-DISABLED event")
  812. if "retry=1" in ev:
  813. # Wait for the retry to scan happen
  814. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED",
  815. "AP-DISABLED"], timeout=5)
  816. if ev is None:
  817. raise Exception("CTRL-EVENT-SCAN-FAILED not seen - retry")
  818. if "AP-DISABLED" in ev:
  819. raise Exception("Unexpected AP-DISABLED event - retry")
  820. dev[1].connect("wpas-ap-open", key_mgmt="NONE", scan_freq="2412")
  821. dev[1].request("DISCONNECT")
  822. dev[1].wait_disconnected()
  823. dev[0].request("DISCONNECT")
  824. dev[0].wait_disconnected()
  825. def test_scan_bss_expiration_on_ssid_change(dev, apdev):
  826. """BSS entry expiration when AP changes SSID"""
  827. dev[0].flush_scan_cache()
  828. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  829. bssid = apdev[0]['bssid']
  830. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  831. hapd.request("DISABLE")
  832. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  833. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 3"):
  834. raise Exception("BSS_EXPIRE_COUNT failed")
  835. dev[0].scan(freq="2412")
  836. dev[0].scan(freq="2412")
  837. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  838. raise Exception("BSS_EXPIRE_COUNT failed")
  839. res = dev[0].request("SCAN_RESULTS")
  840. if "test-scan" not in res:
  841. raise Exception("The first SSID not in scan results")
  842. if "open" not in res:
  843. raise Exception("The second SSID not in scan results")
  844. dev[0].connect("open", key_mgmt="NONE")
  845. dev[0].request("BSS_FLUSH 0")
  846. res = dev[0].request("SCAN_RESULTS")
  847. if "test-scan" in res:
  848. raise Exception("The BSS entry with the old SSID was not removed")
  849. dev[0].request("DISCONNECT")
  850. dev[0].wait_disconnected()
  851. def test_scan_dfs(dev, apdev, params):
  852. """Scan on DFS channels"""
  853. try:
  854. _test_scan_dfs(dev, apdev, params)
  855. finally:
  856. subprocess.call(['iw', 'reg', 'set', '00'])
  857. def _test_scan_dfs(dev, apdev, params):
  858. subprocess.call(['iw', 'reg', 'set', 'US'])
  859. for i in range(2):
  860. for j in range(5):
  861. ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
  862. if ev is None:
  863. raise Exception("No regdom change event")
  864. if "alpha2=US" in ev:
  865. break
  866. dev[i].dump_monitor()
  867. if "OK" not in dev[0].request("SCAN"):
  868. raise Exception("SCAN command failed")
  869. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  870. if ev is None:
  871. raise Exception("Scan did not complete")
  872. if "OK" not in dev[0].request("SCAN freq=2412,5180,5260,5500,5600,5745"):
  873. raise Exception("SCAN command failed")
  874. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  875. if ev is None:
  876. raise Exception("Scan did not complete")
  877. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  878. "wlan.fc.type_subtype == 4", [ "radiotap.channel.freq" ])
  879. if out is not None:
  880. freq = out.splitlines()
  881. freq = [int(f) for f in freq]
  882. freq = list(set(freq))
  883. freq.sort()
  884. logger.info("Active scan seen on channels: " + str(freq))
  885. for f in freq:
  886. if (f >= 5260 and f <= 5320) or (f >= 5500 and f <= 5700):
  887. raise Exception("Active scan on DFS channel: %d" % f)
  888. if f in [ 2467, 2472 ]:
  889. raise Exception("Active scan on US-disallowed channel: %d" % f)
  890. @remote_compatible
  891. def test_scan_abort(dev, apdev):
  892. """Aborting a full scan"""
  893. dev[0].request("SCAN")
  894. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  895. if ev is None:
  896. raise Exception("Scan did not start")
  897. if "OK" not in dev[0].request("ABORT_SCAN"):
  898. raise Exception("ABORT_SCAN command failed")
  899. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=2)
  900. if ev is None:
  901. raise Exception("Scan did not terminate")
  902. @remote_compatible
  903. def test_scan_abort_on_connect(dev, apdev):
  904. """Aborting a full scan on connection request"""
  905. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  906. bssid = apdev[0]['bssid']
  907. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  908. dev[0].dump_monitor()
  909. dev[0].request("SCAN")
  910. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  911. if ev is None:
  912. raise Exception("Scan did not start")
  913. dev[0].connect("test-scan", key_mgmt="NONE")
  914. @remote_compatible
  915. def test_scan_ext(dev, apdev):
  916. """Custom IE in Probe Request frame"""
  917. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  918. bssid = apdev[0]['bssid']
  919. try:
  920. if "OK" not in dev[0].request("VENDOR_ELEM_ADD 14 dd050011223300"):
  921. raise Exception("VENDOR_ELEM_ADD failed")
  922. check_scan(dev[0], "freq=2412 use_id=1")
  923. finally:
  924. dev[0].request("VENDOR_ELEM_REMOVE 14 *")
  925. def test_scan_fail(dev, apdev):
  926. """Scan failures"""
  927. with fail_test(dev[0], 1, "wpa_driver_nl80211_scan"):
  928. dev[0].request("DISCONNECT")
  929. if "OK" not in dev[0].request("SCAN freq=2412"):
  930. raise Exception("SCAN failed")
  931. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  932. if ev is None:
  933. raise Exception("Did not see scan failure event")
  934. dev[0].dump_monitor()
  935. for i in range(1, 5):
  936. with alloc_fail(dev[0], i,
  937. "wpa_scan_clone_params;wpa_supplicant_trigger_scan"):
  938. if "OK" not in dev[0].request("SCAN ssid 112233 freq=2412"):
  939. raise Exception("SCAN failed")
  940. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  941. if ev is None:
  942. raise Exception("Did not see scan failure event")
  943. dev[0].dump_monitor()
  944. with alloc_fail(dev[0], 1, "radio_add_work;wpa_supplicant_trigger_scan"):
  945. if "OK" not in dev[0].request("SCAN freq=2412"):
  946. raise Exception("SCAN failed")
  947. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  948. if ev is None:
  949. raise Exception("Did not see scan failure event")
  950. dev[0].dump_monitor()
  951. try:
  952. if "OK" not in dev[0].request("SET filter_ssids 1"):
  953. raise Exception("SET failed")
  954. id = dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  955. with alloc_fail(dev[0], 1, "wpa_supplicant_build_filter_ssids"):
  956. # While the filter list cannot be created due to memory allocation
  957. # failure, this scan is expected to be completed without SSID
  958. # filtering.
  959. if "OK" not in dev[0].request("SCAN freq=2412"):
  960. raise Exception("SCAN failed")
  961. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  962. if ev is None:
  963. raise Exception("Scan did not complete")
  964. dev[0].remove_network(id)
  965. finally:
  966. dev[0].request("SET filter_ssids 0")
  967. dev[0].dump_monitor()
  968. with alloc_fail(dev[0], 1, "nl80211_get_scan_results"):
  969. if "OK" not in dev[0].request("SCAN freq=2412"):
  970. raise Exception("SCAN failed")
  971. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  972. if ev is None:
  973. raise Exception("Did not see scan started event")
  974. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  975. dev[0].dump_monitor()
  976. try:
  977. if "OK" not in dev[0].request("SET setband 2G"):
  978. raise Exception("SET setband failed")
  979. with alloc_fail(dev[0], 1, "=wpa_setband_scan_freqs_list"):
  980. # While the frequency list cannot be created due to memory
  981. # allocation failure, this scan is expected to be completed without
  982. # frequency filtering.
  983. if "OK" not in dev[0].request("SCAN"):
  984. raise Exception("SCAN failed")
  985. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  986. dev[0].request("ABORT_SCAN")
  987. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  988. if ev is None:
  989. raise Exception("Scan did not complete")
  990. finally:
  991. dev[0].request("SET setband AUTO")
  992. dev[0].dump_monitor()
  993. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  994. wpas.interface_add("wlan5")
  995. wpas.request("SET preassoc_mac_addr 1")
  996. with fail_test(wpas, 1, "nl80211_set_mac_addr;wpas_trigger_scan_cb"):
  997. if "OK" not in wpas.request("SCAN freq=2412"):
  998. raise Exception("SCAN failed")
  999. ev = wpas.wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  1000. if ev is None:
  1001. raise Exception("Did not see scan failure event")
  1002. wpas.request("SET preassoc_mac_addr 0")
  1003. wpas.dump_monitor()
  1004. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  1005. with alloc_fail(dev[0], 1, "wpa_bss_add"):
  1006. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1007. @remote_compatible
  1008. def test_scan_freq_list(dev, apdev):
  1009. """Scan with SET freq_list and scan_cur_freq"""
  1010. try:
  1011. if "OK" not in dev[0].request("SET freq_list 2412 2417"):
  1012. raise Exception("SET freq_list failed")
  1013. check_scan(dev[0], "use_id=1")
  1014. finally:
  1015. dev[0].request("SET freq_list ")
  1016. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1017. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412")
  1018. try:
  1019. if "OK" not in dev[0].request("SET scan_cur_freq 1"):
  1020. raise Exception("SET scan_cur_freq failed")
  1021. check_scan(dev[0], "use_id=1")
  1022. finally:
  1023. dev[0].request("SET scan_cur_freq 0")
  1024. dev[0].request("REMOVE_NETWORK all")
  1025. dev[0].wait_disconnected()
  1026. def test_scan_bss_limit(dev, apdev):
  1027. """Scan and wpa_supplicant BSS entry limit"""
  1028. try:
  1029. _test_scan_bss_limit(dev, apdev)
  1030. finally:
  1031. dev[0].request("SET bss_max_count 200")
  1032. pass
  1033. def _test_scan_bss_limit(dev, apdev):
  1034. # Trigger 'Increasing the MAX BSS count to 2 because all BSSes are in use.
  1035. # We should normally not get here!' message by limiting the maximum BSS
  1036. # count to one so that the second AP would not fit in the BSS list and the
  1037. # first AP cannot be removed from the list since it is still in use.
  1038. dev[0].request("SET bss_max_count 1")
  1039. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1040. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412")
  1041. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "test-scan-2",
  1042. "channel": "6" })
  1043. dev[0].scan_for_bss(apdev[1]['bssid'], freq=2437, force_scan=True)
  1044. def run_scan(dev, bssid, exp_freq):
  1045. for i in range(5):
  1046. dev.request("SCAN freq=2412,2437,2462")
  1047. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  1048. if ev is None:
  1049. raise Exception("Scan did not complete")
  1050. bss = dev.get_bss(bssid)
  1051. freq = int(bss['freq']) if bss else 0
  1052. if freq == exp_freq:
  1053. break
  1054. if freq != exp_freq:
  1055. raise Exception("BSS entry shows incorrect frequency: %d != %d" % (freq, exp_freq))
  1056. def test_scan_chan_switch(dev, apdev):
  1057. """Scanning and AP changing channels"""
  1058. # This test verifies that wpa_supplicant updates its local BSS table based
  1059. # on the correct cfg80211 scan entry in cases where the cfg80211 BSS table
  1060. # has multiple (one for each frequency) BSS entries for the same BSS.
  1061. csa_supported(dev[0])
  1062. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan", "channel": "1" })
  1063. csa_supported(hapd)
  1064. bssid = hapd.own_addr()
  1065. logger.info("AP channel switch while not connected")
  1066. run_scan(dev[0], bssid, 2412)
  1067. dev[0].dump_monitor()
  1068. switch_channel(hapd, 1, 2437)
  1069. run_scan(dev[0], bssid, 2437)
  1070. dev[0].dump_monitor()
  1071. switch_channel(hapd, 1, 2462)
  1072. run_scan(dev[0], bssid, 2462)
  1073. dev[0].dump_monitor()
  1074. logger.info("AP channel switch while connected")
  1075. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412 2437 2462")
  1076. run_scan(dev[0], bssid, 2462)
  1077. dev[0].dump_monitor()
  1078. switch_channel(hapd, 2, 2437)
  1079. wait_channel_switch(dev[0], 2437)
  1080. dev[0].dump_monitor()
  1081. run_scan(dev[0], bssid, 2437)
  1082. dev[0].dump_monitor()
  1083. switch_channel(hapd, 2, 2412)
  1084. wait_channel_switch(dev[0], 2412)
  1085. dev[0].dump_monitor()
  1086. run_scan(dev[0], bssid, 2412)
  1087. dev[0].dump_monitor()
  1088. def test_scan_new_only(dev, apdev):
  1089. """Scan and only_new=1 multiple times"""
  1090. try:
  1091. _test_scan_new_only(dev, apdev)
  1092. finally:
  1093. dev[0].set("ignore_old_scan_res", "0")
  1094. def _test_scan_new_only(dev, apdev):
  1095. dev[0].flush_scan_cache()
  1096. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1097. dev[0].set("ignore_old_scan_res", "1")
  1098. # Get the BSS added to cfg80211 BSS list
  1099. bssid = hapd.own_addr()
  1100. dev[0].scan_for_bss(bssid, freq=2412)
  1101. bss = dev[0].get_bss(bssid)
  1102. idx1 = bss['update_idx']
  1103. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  1104. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  1105. bss = dev[0].get_bss(bssid)
  1106. idx2 = bss['update_idx']
  1107. if int(idx2) <= int(idx1):
  1108. raise Exception("Scan result update_idx did not increase")
  1109. # Disable AP to ensure there are no new scan results after this.
  1110. hapd.disable()
  1111. # Try to scan multiple times to verify that old scan results do not get
  1112. # accepted as new.
  1113. for i in range(10):
  1114. dev[0].scan(freq=2412)
  1115. bss = dev[0].get_bss(bssid)
  1116. if bss:
  1117. idx = bss['update_idx']
  1118. if int(idx) > int(idx2):
  1119. raise Exception("Unexpected update_idx increase")