test_scan.py 47 KB

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