test_ap_acs.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. # Test cases for automatic channel selection with hostapd
  2. # Copyright (c) 2013-2017, 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 logging
  7. logger = logging.getLogger()
  8. import time
  9. import hostapd
  10. from utils import skip_with_fips, alloc_fail, fail_test
  11. from test_ap_ht import clear_scan_cache
  12. def force_prev_ap_on_24g(ap):
  13. # For now, make sure the last operating channel was on 2.4 GHz band to get
  14. # sufficient survey data from mac80211_hwsim.
  15. hostapd.add_ap(ap, { "ssid": "open" })
  16. time.sleep(0.1)
  17. hostapd.remove_bss(ap)
  18. def force_prev_ap_on_5g(ap):
  19. # For now, make sure the last operating channel was on 5 GHz band to get
  20. # sufficient survey data from mac80211_hwsim.
  21. hostapd.add_ap(ap, { "ssid": "open", "hw_mode": "a",
  22. "channel": "36", "country_code": "US" })
  23. time.sleep(0.1)
  24. hostapd.remove_bss(ap)
  25. def wait_acs(hapd):
  26. ev = hapd.wait_event(["ACS-STARTED", "ACS-COMPLETED", "ACS-FAILED",
  27. "AP-ENABLED", "AP-DISABLED"], timeout=5)
  28. if not ev:
  29. raise Exception("ACS start timed out")
  30. if "ACS-STARTED" not in ev:
  31. raise Exception("Unexpected ACS event: " + ev)
  32. state = hapd.get_status_field("state")
  33. if state != "ACS":
  34. raise Exception("Unexpected interface state")
  35. ev = hapd.wait_event(["ACS-COMPLETED", "ACS-FAILED", "AP-ENABLED",
  36. "AP-DISABLED"], timeout=20)
  37. if not ev:
  38. raise Exception("ACS timed out")
  39. if "ACS-COMPLETED" not in ev:
  40. raise Exception("Unexpected ACS event: " + ev)
  41. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=5)
  42. if not ev:
  43. raise Exception("AP setup timed out")
  44. if "AP-ENABLED" not in ev:
  45. raise Exception("Unexpected ACS event: " + ev)
  46. state = hapd.get_status_field("state")
  47. if state != "ENABLED":
  48. raise Exception("Unexpected interface state")
  49. def test_ap_acs(dev, apdev):
  50. """Automatic channel selection"""
  51. force_prev_ap_on_24g(apdev[0])
  52. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  53. params['channel'] = '0'
  54. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  55. wait_acs(hapd)
  56. freq = hapd.get_status_field("freq")
  57. if int(freq) < 2400:
  58. raise Exception("Unexpected frequency")
  59. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  60. def test_ap_acs_chanlist(dev, apdev):
  61. """Automatic channel selection with chanlist set"""
  62. force_prev_ap_on_24g(apdev[0])
  63. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  64. params['channel'] = '0'
  65. params['chanlist'] = '1 6 11'
  66. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  67. wait_acs(hapd)
  68. freq = hapd.get_status_field("freq")
  69. if int(freq) < 2400:
  70. raise Exception("Unexpected frequency")
  71. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  72. def test_ap_multi_bss_acs(dev, apdev):
  73. """hostapd start with a multi-BSS configuration file using ACS"""
  74. skip_with_fips(dev[0])
  75. force_prev_ap_on_24g(apdev[0])
  76. # start the actual test
  77. hapd = hostapd.add_iface(apdev[0], 'multi-bss-acs.conf')
  78. hapd.enable()
  79. wait_acs(hapd)
  80. freq = hapd.get_status_field("freq")
  81. if int(freq) < 2400:
  82. raise Exception("Unexpected frequency")
  83. dev[0].connect("bss-1", key_mgmt="NONE", scan_freq=freq)
  84. dev[1].connect("bss-2", psk="12345678", scan_freq=freq)
  85. dev[2].connect("bss-3", psk="qwertyuiop", scan_freq=freq)
  86. def test_ap_acs_40mhz(dev, apdev):
  87. """Automatic channel selection for 40 MHz channel"""
  88. clear_scan_cache(apdev[0])
  89. force_prev_ap_on_24g(apdev[0])
  90. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  91. params['channel'] = '0'
  92. params['ht_capab'] = '[HT40+]'
  93. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  94. wait_acs(hapd)
  95. freq = hapd.get_status_field("freq")
  96. if int(freq) < 2400:
  97. raise Exception("Unexpected frequency")
  98. sec = hapd.get_status_field("secondary_channel")
  99. if int(sec) == 0:
  100. raise Exception("Secondary channel not set")
  101. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  102. def test_ap_acs_40mhz_minus(dev, apdev):
  103. """Automatic channel selection for HT40- channel"""
  104. clear_scan_cache(apdev[0])
  105. force_prev_ap_on_24g(apdev[0])
  106. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  107. params['channel'] = '0'
  108. params['ht_capab'] = '[HT40-]'
  109. params['acs_num_scans'] = '1'
  110. params['chanlist'] = '1 11'
  111. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  112. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=10)
  113. if not ev:
  114. raise Exception("ACS start timed out")
  115. # HT40- is not currently supported in hostapd ACS, so do not try to connect
  116. # or verify that this operation succeeded.
  117. def test_ap_acs_5ghz(dev, apdev):
  118. """Automatic channel selection on 5 GHz"""
  119. try:
  120. hapd = None
  121. force_prev_ap_on_5g(apdev[0])
  122. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  123. params['hw_mode'] = 'a'
  124. params['channel'] = '0'
  125. params['country_code'] = 'US'
  126. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  127. wait_acs(hapd)
  128. freq = hapd.get_status_field("freq")
  129. if int(freq) < 5000:
  130. raise Exception("Unexpected frequency")
  131. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  132. finally:
  133. dev[0].request("DISCONNECT")
  134. if hapd:
  135. hapd.request("DISABLE")
  136. hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
  137. dev[0].flush_scan_cache()
  138. def test_ap_acs_5ghz_40mhz(dev, apdev):
  139. """Automatic channel selection on 5 GHz for 40 MHz channel"""
  140. try:
  141. hapd = None
  142. force_prev_ap_on_5g(apdev[0])
  143. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  144. params['hw_mode'] = 'a'
  145. params['channel'] = '0'
  146. params['ht_capab'] = '[HT40+]'
  147. params['country_code'] = 'US'
  148. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  149. wait_acs(hapd)
  150. freq = hapd.get_status_field("freq")
  151. if int(freq) < 5000:
  152. raise Exception("Unexpected frequency")
  153. sec = hapd.get_status_field("secondary_channel")
  154. if int(sec) == 0:
  155. raise Exception("Secondary channel not set")
  156. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  157. finally:
  158. dev[0].request("DISCONNECT")
  159. if hapd:
  160. hapd.request("DISABLE")
  161. hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
  162. dev[0].flush_scan_cache()
  163. def test_ap_acs_vht(dev, apdev):
  164. """Automatic channel selection for VHT"""
  165. try:
  166. hapd = None
  167. force_prev_ap_on_5g(apdev[0])
  168. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  169. params['hw_mode'] = 'a'
  170. params['channel'] = '0'
  171. params['ht_capab'] = '[HT40+]'
  172. params['country_code'] = 'US'
  173. params['ieee80211ac'] = '1'
  174. params['vht_oper_chwidth'] = '1'
  175. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  176. wait_acs(hapd)
  177. freq = hapd.get_status_field("freq")
  178. if int(freq) < 5000:
  179. raise Exception("Unexpected frequency")
  180. sec = hapd.get_status_field("secondary_channel")
  181. if int(sec) == 0:
  182. raise Exception("Secondary channel not set")
  183. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  184. finally:
  185. dev[0].request("DISCONNECT")
  186. if hapd:
  187. hapd.request("DISABLE")
  188. hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
  189. dev[0].flush_scan_cache()
  190. def test_ap_acs_vht40(dev, apdev):
  191. """Automatic channel selection for VHT40"""
  192. try:
  193. hapd = None
  194. force_prev_ap_on_5g(apdev[0])
  195. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  196. params['hw_mode'] = 'a'
  197. params['channel'] = '0'
  198. params['ht_capab'] = '[HT40+]'
  199. params['country_code'] = 'US'
  200. params['ieee80211ac'] = '1'
  201. params['vht_oper_chwidth'] = '0'
  202. params['acs_num_scans'] = '1'
  203. params['chanlist'] = '36 149'
  204. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  205. wait_acs(hapd)
  206. freq = hapd.get_status_field("freq")
  207. if int(freq) < 5000:
  208. raise Exception("Unexpected frequency")
  209. sec = hapd.get_status_field("secondary_channel")
  210. if int(sec) == 0:
  211. raise Exception("Secondary channel not set")
  212. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  213. finally:
  214. dev[0].request("DISCONNECT")
  215. if hapd:
  216. hapd.request("DISABLE")
  217. hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
  218. dev[0].flush_scan_cache()
  219. def test_ap_acs_vht160(dev, apdev):
  220. """Automatic channel selection for VHT160"""
  221. try:
  222. hapd = None
  223. force_prev_ap_on_5g(apdev[0])
  224. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  225. params['hw_mode'] = 'a'
  226. params['channel'] = '0'
  227. params['ht_capab'] = '[HT40+]'
  228. params['country_code'] = 'ZA'
  229. params['ieee80211ac'] = '1'
  230. params['vht_oper_chwidth'] = '2'
  231. params["vht_oper_centr_freq_seg0_idx"] = "114"
  232. params['ieee80211d'] = '1'
  233. params['ieee80211h'] = '1'
  234. params['chanlist'] = '100'
  235. params['acs_num_scans'] = '1'
  236. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  237. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=10)
  238. if not ev:
  239. raise Exception("ACS start timed out")
  240. # VHT160 is not currently supported in hostapd ACS, so do not try to
  241. # enforce successful AP start.
  242. if "AP-ENABLED" in ev:
  243. freq = hapd.get_status_field("freq")
  244. if int(freq) < 5000:
  245. raise Exception("Unexpected frequency")
  246. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  247. finally:
  248. dev[0].request("DISCONNECT")
  249. if hapd:
  250. hapd.request("DISABLE")
  251. hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
  252. def test_ap_acs_vht160_scan_disable(dev, apdev):
  253. """Automatic channel selection for VHT160 and DISABLE during scan"""
  254. force_prev_ap_on_5g(apdev[0])
  255. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  256. params['hw_mode'] = 'a'
  257. params['channel'] = '0'
  258. params['ht_capab'] = '[HT40+]'
  259. params['country_code'] = 'ZA'
  260. params['ieee80211ac'] = '1'
  261. params['vht_oper_chwidth'] = '2'
  262. params["vht_oper_centr_freq_seg0_idx"] = "114"
  263. params['ieee80211d'] = '1'
  264. params['ieee80211h'] = '1'
  265. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  266. time.sleep(3)
  267. hapd.request("DISABLE")
  268. hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
  269. def test_ap_acs_bias(dev, apdev):
  270. """Automatic channel selection with bias values"""
  271. force_prev_ap_on_24g(apdev[0])
  272. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  273. params['channel'] = '0'
  274. params['acs_chan_bias'] = '1:0.8 3:1.2 6:0.7 11:0.8'
  275. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  276. wait_acs(hapd)
  277. freq = hapd.get_status_field("freq")
  278. if int(freq) < 2400:
  279. raise Exception("Unexpected frequency")
  280. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  281. def test_ap_acs_survey(dev, apdev):
  282. """Automatic channel selection using acs_survey parameter"""
  283. force_prev_ap_on_24g(apdev[0])
  284. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  285. params['channel'] = 'acs_survey'
  286. params['acs_num_scans'] = '1'
  287. hapd = hostapd.add_ap(apdev[0], params, wait_enabled=False)
  288. wait_acs(hapd)
  289. freq = hapd.get_status_field("freq")
  290. if int(freq) < 2400:
  291. raise Exception("Unexpected frequency")
  292. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  293. def test_ap_acs_errors(dev, apdev):
  294. """Automatic channel selection failures"""
  295. clear_scan_cache(apdev[0])
  296. force_prev_ap_on_24g(apdev[0])
  297. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  298. params['channel'] = '0'
  299. params['acs_num_scans'] = '2'
  300. params['chanlist'] = '1'
  301. hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
  302. with alloc_fail(hapd, 1, "acs_request_scan"):
  303. if "FAIL" not in hapd.request("ENABLE"):
  304. raise Exception("Unexpected success for ENABLE")
  305. hapd.dump_monitor()
  306. with fail_test(hapd, 1, "acs_request_scan"):
  307. if "FAIL" not in hapd.request("ENABLE"):
  308. raise Exception("Unexpected success for ENABLE")
  309. hapd.dump_monitor()
  310. with fail_test(hapd, 1, "acs_scan_complete"):
  311. hapd.enable()
  312. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=10)
  313. if not ev:
  314. raise Exception("ACS start timed out")
  315. hapd.dump_monitor()
  316. with fail_test(hapd, 1, "acs_request_scan;acs_scan_complete"):
  317. hapd.enable()
  318. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=10)
  319. if not ev:
  320. raise Exception("ACS start timed out")