test_scan.py 46 KB

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