test_scan.py 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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, parse_ie
  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. raise Exception("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. if dev[0].get_status_field('wpa_state') == "SCANNING":
  721. raise Exception("wpa_state was already SCANNING")
  722. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  723. bssid = apdev[0]['bssid']
  724. if "OK" not in dev[0].request("SET test_failure 1"):
  725. raise Exception("Failed to set test_failure")
  726. if "OK" not in dev[0].request("SCAN"):
  727. raise Exception("SCAN command failed")
  728. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  729. if ev is None:
  730. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  731. if "retry=1" in ev:
  732. raise Exception("Unexpected scan retry indicated")
  733. if dev[0].get_status_field('wpa_state') == "SCANNING":
  734. raise Exception("wpa_state SCANNING not cleared")
  735. id = dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412",
  736. only_add_network=True)
  737. dev[0].select_network(id)
  738. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  739. if ev is None:
  740. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  741. if "retry=1" not in ev:
  742. raise Exception("No scan retry indicated for connection")
  743. if dev[0].get_status_field('wpa_state') == "SCANNING":
  744. raise Exception("wpa_state SCANNING not cleared")
  745. dev[0].request("SET test_failure 0")
  746. dev[0].wait_connected()
  747. dev[0].request("SET test_failure 1")
  748. if "OK" not in dev[0].request("SCAN"):
  749. raise Exception("SCAN command failed")
  750. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  751. if ev is None:
  752. raise Exception("Did not receive CTRL-EVENT-SCAN-FAILED event")
  753. if "retry=1" in ev:
  754. raise Exception("Unexpected scan retry indicated")
  755. if dev[0].get_status_field('wpa_state') != "COMPLETED":
  756. raise Exception("wpa_state COMPLETED not restored")
  757. dev[0].request("SET test_failure 0")
  758. @remote_compatible
  759. def test_scan_specify_ssid(dev, apdev):
  760. """Control interface behavior on scan SSID parameter"""
  761. dev[0].flush_scan_cache()
  762. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-hidden",
  763. "ignore_broadcast_ssid": "1" })
  764. bssid = apdev[0]['bssid']
  765. check_scan(dev[0], "freq=2412 use_id=1 ssid 414243")
  766. bss = dev[0].get_bss(bssid)
  767. if bss is not None and bss['ssid'] == 'test-hidden':
  768. raise Exception("BSS entry for hidden AP present unexpectedly")
  769. # Allow couple more retries to avoid reporting errors during heavy load
  770. for i in range(5):
  771. check_scan(dev[0], "freq=2412 ssid 414243 ssid 746573742d68696464656e ssid 616263313233 use_id=1")
  772. bss = dev[0].get_bss(bssid)
  773. if bss and 'test-hidden' in dev[0].request("SCAN_RESULTS"):
  774. break
  775. if bss is None:
  776. raise Exception("BSS entry for hidden AP not found")
  777. if 'test-hidden' not in dev[0].request("SCAN_RESULTS"):
  778. raise Exception("Expected SSID not included in the scan results")
  779. hapd.disable()
  780. dev[0].flush_scan_cache(freq=2432)
  781. dev[0].flush_scan_cache()
  782. if "FAIL" not in dev[0].request("SCAN ssid foo"):
  783. raise Exception("Invalid SCAN command accepted")
  784. @remote_compatible
  785. def test_scan_ap_scan_2_ap_mode(dev, apdev):
  786. """AP_SCAN 2 AP mode and scan()"""
  787. try:
  788. _test_scan_ap_scan_2_ap_mode(dev, apdev)
  789. finally:
  790. dev[0].request("AP_SCAN 1")
  791. def _test_scan_ap_scan_2_ap_mode(dev, apdev):
  792. if "OK" not in dev[0].request("AP_SCAN 2"):
  793. raise Exception("Failed to set AP_SCAN 2")
  794. id = dev[0].add_network()
  795. dev[0].set_network(id, "mode", "2")
  796. dev[0].set_network_quoted(id, "ssid", "wpas-ap-open")
  797. dev[0].set_network(id, "key_mgmt", "NONE")
  798. dev[0].set_network(id, "frequency", "2412")
  799. dev[0].set_network(id, "scan_freq", "2412")
  800. dev[0].set_network(id, "disabled", "0")
  801. dev[0].select_network(id)
  802. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=5)
  803. if ev is None:
  804. raise Exception("AP failed to start")
  805. with fail_test(dev[0], 1, "wpa_driver_nl80211_scan"):
  806. if "OK" not in dev[0].request("SCAN freq=2412"):
  807. raise Exception("SCAN command failed unexpectedly")
  808. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED",
  809. "AP-DISABLED"], timeout=5)
  810. if ev is None:
  811. raise Exception("CTRL-EVENT-SCAN-FAILED not seen")
  812. if "AP-DISABLED" in ev:
  813. raise Exception("Unexpected AP-DISABLED event")
  814. if "retry=1" in ev:
  815. # Wait for the retry to scan happen
  816. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED",
  817. "AP-DISABLED"], timeout=5)
  818. if ev is None:
  819. raise Exception("CTRL-EVENT-SCAN-FAILED not seen - retry")
  820. if "AP-DISABLED" in ev:
  821. raise Exception("Unexpected AP-DISABLED event - retry")
  822. dev[1].connect("wpas-ap-open", key_mgmt="NONE", scan_freq="2412")
  823. dev[1].request("DISCONNECT")
  824. dev[1].wait_disconnected()
  825. dev[0].request("DISCONNECT")
  826. dev[0].wait_disconnected()
  827. def test_scan_bss_expiration_on_ssid_change(dev, apdev):
  828. """BSS entry expiration when AP changes SSID"""
  829. dev[0].flush_scan_cache()
  830. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  831. bssid = apdev[0]['bssid']
  832. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  833. hapd.request("DISABLE")
  834. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  835. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 3"):
  836. raise Exception("BSS_EXPIRE_COUNT failed")
  837. dev[0].scan(freq="2412")
  838. dev[0].scan(freq="2412")
  839. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  840. raise Exception("BSS_EXPIRE_COUNT failed")
  841. res = dev[0].request("SCAN_RESULTS")
  842. if "test-scan" not in res:
  843. raise Exception("The first SSID not in scan results")
  844. if "open" not in res:
  845. raise Exception("The second SSID not in scan results")
  846. dev[0].connect("open", key_mgmt="NONE")
  847. dev[0].request("BSS_FLUSH 0")
  848. res = dev[0].request("SCAN_RESULTS")
  849. if "test-scan" in res:
  850. raise Exception("The BSS entry with the old SSID was not removed")
  851. dev[0].request("DISCONNECT")
  852. dev[0].wait_disconnected()
  853. def test_scan_dfs(dev, apdev, params):
  854. """Scan on DFS channels"""
  855. try:
  856. _test_scan_dfs(dev, apdev, params)
  857. finally:
  858. subprocess.call(['iw', 'reg', 'set', '00'])
  859. def _test_scan_dfs(dev, apdev, params):
  860. subprocess.call(['iw', 'reg', 'set', 'US'])
  861. for i in range(2):
  862. for j in range(5):
  863. ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
  864. if ev is None:
  865. raise Exception("No regdom change event")
  866. if "alpha2=US" in ev:
  867. break
  868. dev[i].dump_monitor()
  869. if "OK" not in dev[0].request("SCAN"):
  870. raise Exception("SCAN command failed")
  871. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  872. if ev is None:
  873. raise Exception("Scan did not complete")
  874. if "OK" not in dev[0].request("SCAN freq=2412,5180,5260,5500,5600,5745"):
  875. raise Exception("SCAN command failed")
  876. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  877. if ev is None:
  878. raise Exception("Scan did not complete")
  879. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  880. "wlan.fc.type_subtype == 4", [ "radiotap.channel.freq" ])
  881. if out is not None:
  882. freq = out.splitlines()
  883. freq = [int(f) for f in freq]
  884. freq = list(set(freq))
  885. freq.sort()
  886. logger.info("Active scan seen on channels: " + str(freq))
  887. for f in freq:
  888. if (f >= 5260 and f <= 5320) or (f >= 5500 and f <= 5700):
  889. raise Exception("Active scan on DFS channel: %d" % f)
  890. if f in [ 2467, 2472 ]:
  891. raise Exception("Active scan on US-disallowed channel: %d" % f)
  892. @remote_compatible
  893. def test_scan_abort(dev, apdev):
  894. """Aborting a full scan"""
  895. dev[0].request("SCAN")
  896. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  897. if ev is None:
  898. raise Exception("Scan did not start")
  899. if "OK" not in dev[0].request("ABORT_SCAN"):
  900. raise Exception("ABORT_SCAN command failed")
  901. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=2)
  902. if ev is None:
  903. raise Exception("Scan did not terminate")
  904. @remote_compatible
  905. def test_scan_abort_on_connect(dev, apdev):
  906. """Aborting a full scan on connection request"""
  907. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  908. bssid = apdev[0]['bssid']
  909. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  910. dev[0].dump_monitor()
  911. dev[0].request("SCAN")
  912. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  913. if ev is None:
  914. raise Exception("Scan did not start")
  915. dev[0].connect("test-scan", key_mgmt="NONE")
  916. @remote_compatible
  917. def test_scan_ext(dev, apdev):
  918. """Custom IE in Probe Request frame"""
  919. hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  920. bssid = apdev[0]['bssid']
  921. try:
  922. if "OK" not in dev[0].request("VENDOR_ELEM_ADD 14 dd050011223300"):
  923. raise Exception("VENDOR_ELEM_ADD failed")
  924. check_scan(dev[0], "freq=2412 use_id=1")
  925. finally:
  926. dev[0].request("VENDOR_ELEM_REMOVE 14 *")
  927. def test_scan_fail(dev, apdev):
  928. """Scan failures"""
  929. with fail_test(dev[0], 1, "wpa_driver_nl80211_scan"):
  930. dev[0].request("DISCONNECT")
  931. if "OK" not in dev[0].request("SCAN freq=2412"):
  932. raise Exception("SCAN failed")
  933. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  934. if ev is None:
  935. raise Exception("Did not see scan failure event")
  936. dev[0].dump_monitor()
  937. for i in range(1, 5):
  938. with alloc_fail(dev[0], i,
  939. "wpa_scan_clone_params;wpa_supplicant_trigger_scan"):
  940. if "OK" not in dev[0].request("SCAN ssid 112233 freq=2412"):
  941. raise Exception("SCAN failed")
  942. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  943. if ev is None:
  944. raise Exception("Did not see scan failure event")
  945. dev[0].dump_monitor()
  946. with alloc_fail(dev[0], 1, "radio_add_work;wpa_supplicant_trigger_scan"):
  947. if "OK" not in dev[0].request("SCAN freq=2412"):
  948. raise Exception("SCAN failed")
  949. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  950. if ev is None:
  951. raise Exception("Did not see scan failure event")
  952. dev[0].dump_monitor()
  953. try:
  954. if "OK" not in dev[0].request("SET filter_ssids 1"):
  955. raise Exception("SET failed")
  956. id = dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  957. with alloc_fail(dev[0], 1, "wpa_supplicant_build_filter_ssids"):
  958. # While the filter list cannot be created due to memory allocation
  959. # failure, this scan is expected to be completed without SSID
  960. # filtering.
  961. if "OK" not in dev[0].request("SCAN freq=2412"):
  962. raise Exception("SCAN failed")
  963. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  964. if ev is None:
  965. raise Exception("Scan did not complete")
  966. dev[0].remove_network(id)
  967. finally:
  968. dev[0].request("SET filter_ssids 0")
  969. dev[0].dump_monitor()
  970. with alloc_fail(dev[0], 1, "nl80211_get_scan_results"):
  971. if "OK" not in dev[0].request("SCAN freq=2412"):
  972. raise Exception("SCAN failed")
  973. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  974. if ev is None:
  975. raise Exception("Did not see scan started event")
  976. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  977. dev[0].dump_monitor()
  978. try:
  979. if "OK" not in dev[0].request("SET setband 2G"):
  980. raise Exception("SET setband failed")
  981. with alloc_fail(dev[0], 1, "=wpa_setband_scan_freqs_list"):
  982. # While the frequency list cannot be created due to memory
  983. # allocation failure, this scan is expected to be completed without
  984. # frequency filtering.
  985. if "OK" not in dev[0].request("SCAN"):
  986. raise Exception("SCAN failed")
  987. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  988. dev[0].request("ABORT_SCAN")
  989. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  990. if ev is None:
  991. raise Exception("Scan did not complete")
  992. finally:
  993. dev[0].request("SET setband AUTO")
  994. dev[0].dump_monitor()
  995. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  996. wpas.interface_add("wlan5")
  997. wpas.request("SET preassoc_mac_addr 1")
  998. with fail_test(wpas, 1, "nl80211_set_mac_addr;wpas_trigger_scan_cb"):
  999. if "OK" not in wpas.request("SCAN freq=2412"):
  1000. raise Exception("SCAN failed")
  1001. ev = wpas.wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=5)
  1002. if ev is None:
  1003. raise Exception("Did not see scan failure event")
  1004. wpas.request("SET preassoc_mac_addr 0")
  1005. wpas.dump_monitor()
  1006. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  1007. with alloc_fail(dev[0], 1, "wpa_bss_add"):
  1008. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1009. @remote_compatible
  1010. def test_scan_freq_list(dev, apdev):
  1011. """Scan with SET freq_list and scan_cur_freq"""
  1012. try:
  1013. if "OK" not in dev[0].request("SET freq_list 2412 2417"):
  1014. raise Exception("SET freq_list failed")
  1015. check_scan(dev[0], "use_id=1")
  1016. finally:
  1017. dev[0].request("SET freq_list ")
  1018. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1019. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412")
  1020. try:
  1021. if "OK" not in dev[0].request("SET scan_cur_freq 1"):
  1022. raise Exception("SET scan_cur_freq failed")
  1023. check_scan(dev[0], "use_id=1")
  1024. finally:
  1025. dev[0].request("SET scan_cur_freq 0")
  1026. dev[0].request("REMOVE_NETWORK all")
  1027. dev[0].wait_disconnected()
  1028. def test_scan_bss_limit(dev, apdev):
  1029. """Scan and wpa_supplicant BSS entry limit"""
  1030. try:
  1031. _test_scan_bss_limit(dev, apdev)
  1032. finally:
  1033. dev[0].request("SET bss_max_count 200")
  1034. pass
  1035. def _test_scan_bss_limit(dev, apdev):
  1036. # Trigger 'Increasing the MAX BSS count to 2 because all BSSes are in use.
  1037. # We should normally not get here!' message by limiting the maximum BSS
  1038. # count to one so that the second AP would not fit in the BSS list and the
  1039. # first AP cannot be removed from the list since it is still in use.
  1040. dev[0].request("SET bss_max_count 1")
  1041. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1042. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412")
  1043. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "test-scan-2",
  1044. "channel": "6" })
  1045. dev[0].scan_for_bss(apdev[1]['bssid'], freq=2437, force_scan=True)
  1046. def run_scan(dev, bssid, exp_freq):
  1047. for i in range(5):
  1048. dev.request("SCAN freq=2412,2437,2462")
  1049. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  1050. if ev is None:
  1051. raise Exception("Scan did not complete")
  1052. bss = dev.get_bss(bssid)
  1053. freq = int(bss['freq']) if bss else 0
  1054. if freq == exp_freq:
  1055. break
  1056. if freq != exp_freq:
  1057. raise Exception("BSS entry shows incorrect frequency: %d != %d" % (freq, exp_freq))
  1058. def test_scan_chan_switch(dev, apdev):
  1059. """Scanning and AP changing channels"""
  1060. # This test verifies that wpa_supplicant updates its local BSS table based
  1061. # on the correct cfg80211 scan entry in cases where the cfg80211 BSS table
  1062. # has multiple (one for each frequency) BSS entries for the same BSS.
  1063. csa_supported(dev[0])
  1064. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan", "channel": "1" })
  1065. csa_supported(hapd)
  1066. bssid = hapd.own_addr()
  1067. logger.info("AP channel switch while not connected")
  1068. run_scan(dev[0], bssid, 2412)
  1069. dev[0].dump_monitor()
  1070. switch_channel(hapd, 1, 2437)
  1071. run_scan(dev[0], bssid, 2437)
  1072. dev[0].dump_monitor()
  1073. switch_channel(hapd, 1, 2462)
  1074. run_scan(dev[0], bssid, 2462)
  1075. dev[0].dump_monitor()
  1076. logger.info("AP channel switch while connected")
  1077. dev[0].connect("test-scan", key_mgmt="NONE", scan_freq="2412 2437 2462")
  1078. run_scan(dev[0], bssid, 2462)
  1079. dev[0].dump_monitor()
  1080. switch_channel(hapd, 2, 2437)
  1081. wait_channel_switch(dev[0], 2437)
  1082. dev[0].dump_monitor()
  1083. run_scan(dev[0], bssid, 2437)
  1084. dev[0].dump_monitor()
  1085. switch_channel(hapd, 2, 2412)
  1086. wait_channel_switch(dev[0], 2412)
  1087. dev[0].dump_monitor()
  1088. run_scan(dev[0], bssid, 2412)
  1089. dev[0].dump_monitor()
  1090. def test_scan_new_only(dev, apdev):
  1091. """Scan and only_new=1 multiple times"""
  1092. try:
  1093. _test_scan_new_only(dev, apdev)
  1094. finally:
  1095. dev[0].set("ignore_old_scan_res", "0")
  1096. def _test_scan_new_only(dev, apdev):
  1097. dev[0].flush_scan_cache()
  1098. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1099. dev[0].set("ignore_old_scan_res", "1")
  1100. # Get the BSS added to cfg80211 BSS list
  1101. bssid = hapd.own_addr()
  1102. dev[0].scan_for_bss(bssid, freq=2412)
  1103. bss = dev[0].get_bss(bssid)
  1104. idx1 = bss['update_idx']
  1105. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  1106. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  1107. bss = dev[0].get_bss(bssid)
  1108. idx2 = bss['update_idx']
  1109. if int(idx2) <= int(idx1):
  1110. raise Exception("Scan result update_idx did not increase")
  1111. # Disable AP to ensure there are no new scan results after this.
  1112. hapd.disable()
  1113. # Try to scan multiple times to verify that old scan results do not get
  1114. # accepted as new.
  1115. for i in range(10):
  1116. dev[0].scan(freq=2412)
  1117. bss = dev[0].get_bss(bssid)
  1118. if bss:
  1119. idx = bss['update_idx']
  1120. if int(idx) > int(idx2):
  1121. raise Exception("Unexpected update_idx increase")
  1122. def test_scan_flush(dev, apdev):
  1123. """Ongoing scan and FLUSH"""
  1124. dev[0].flush_scan_cache()
  1125. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan" })
  1126. dev[0].dump_monitor()
  1127. dev[0].request("SCAN TYPE=ONLY freq=2412-2472 passive=1")
  1128. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=10)
  1129. if ev is None:
  1130. raise Exception("Scan did not start")
  1131. time.sleep(0.1)
  1132. dev[0].request("FLUSH")
  1133. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS",
  1134. "CTRL-EVENT-SCAN-FAILED",
  1135. "CTRL-EVENT-BSS-ADDED"], timeout=10)
  1136. if ev is None:
  1137. raise Exception("Scan did not complete")
  1138. if "CTRL-EVENT-BSS-ADDED" in ev:
  1139. raise Exception("Unexpected BSS entry addition after FLUSH")
  1140. def test_scan_ies(dev, apdev):
  1141. """Scan and both Beacon and Probe Response frame IEs"""
  1142. dev[0].flush_scan_cache()
  1143. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan",
  1144. "beacon_int": "20" })
  1145. bssid = hapd.own_addr()
  1146. dev[0].dump_monitor()
  1147. for i in range(10):
  1148. dev[0].request("SCAN TYPE=ONLY freq=2412 passive=1")
  1149. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
  1150. if ev is None:
  1151. raise Exception("Scan did not complete")
  1152. if dev[0].get_bss(bssid):
  1153. break
  1154. for i in range(10):
  1155. dev[0].scan_for_bss(bssid, freq=2412, force_scan=True)
  1156. bss = dev[0].get_bss(bssid)
  1157. if 'beacon_ie' in bss:
  1158. if bss['ie'] != bss['beacon_ie']:
  1159. break
  1160. if not bss or 'beacon_ie' not in bss:
  1161. raise Exception("beacon_ie not present")
  1162. ie = parse_ie(bss['ie'])
  1163. logger.info("ie: " + str(ie.keys()))
  1164. beacon_ie = parse_ie(bss['beacon_ie'])
  1165. logger.info("beacon_ie: " + str(ie.keys()))
  1166. if bss['ie'] == bss['beacon_ie']:
  1167. raise Exception("Both ie and beacon_ie show same data")