test_dfs.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. # Test cases for DFS
  2. # Copyright (c) 2013, 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 os
  8. import subprocess
  9. import time
  10. import logging
  11. logger = logging.getLogger()
  12. import hwsim_utils
  13. import hostapd
  14. from utils import HwsimSkip
  15. def wait_dfs_event(hapd, event, timeout):
  16. dfs_events = [ "DFS-RADAR-DETECTED", "DFS-NEW-CHANNEL",
  17. "DFS-CAC-START", "DFS-CAC-COMPLETED",
  18. "DFS-NOP-FINISHED", "AP-ENABLED", "AP-CSA-FINISHED" ]
  19. ev = hapd.wait_event(dfs_events, timeout=timeout)
  20. if not ev:
  21. raise Exception("DFS event timed out")
  22. if event and event not in ev:
  23. raise Exception("Unexpected DFS event: " + ev + " (expected: %s)" % event)
  24. return ev
  25. def start_dfs_ap(ap, allow_failure=False, ssid="dfs", ht=True, ht40=False,
  26. ht40minus=False, vht80=False, vht20=False, chanlist=None,
  27. channel=None, country="FI"):
  28. ifname = ap['ifname']
  29. logger.info("Starting AP " + ifname + " on DFS channel")
  30. hapd = hostapd.add_ap(ap, {}, no_enable=True)
  31. hapd.set("ssid", ssid)
  32. hapd.set("country_code", country)
  33. hapd.set("ieee80211d", "1")
  34. hapd.set("ieee80211h", "1")
  35. hapd.set("hw_mode", "a")
  36. hapd.set("channel", "52")
  37. if not ht:
  38. hapd.set("ieee80211n", "0")
  39. if ht40:
  40. hapd.set("ht_capab", "[HT40+]")
  41. elif ht40minus:
  42. hapd.set("ht_capab", "[HT40-]")
  43. hapd.set("channel", "56")
  44. if vht80:
  45. hapd.set("ieee80211ac", "1")
  46. hapd.set("vht_oper_chwidth", "1")
  47. hapd.set("vht_oper_centr_freq_seg0_idx", "58")
  48. if vht20:
  49. hapd.set("ieee80211ac", "1")
  50. hapd.set("vht_oper_chwidth", "0")
  51. hapd.set("vht_oper_centr_freq_seg0_idx", "0")
  52. if chanlist:
  53. hapd.set("chanlist", chanlist)
  54. if channel:
  55. hapd.set("channel", str(channel))
  56. hapd.enable()
  57. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  58. if "DFS-CAC-START" not in ev:
  59. raise Exception("Unexpected DFS event: " + ev)
  60. state = hapd.get_status_field("state")
  61. if state != "DFS":
  62. if allow_failure:
  63. logger.info("Interface state not DFS: " + state)
  64. if not os.path.exists("dfs"):
  65. raise HwsimSkip("Assume DFS testing not supported")
  66. raise Exception("Failed to start DFS AP")
  67. raise Exception("Unexpected interface state: " + state)
  68. return hapd
  69. def dfs_simulate_radar(hapd):
  70. logger.info("Trigger a simulated radar event")
  71. phyname = hapd.get_driver_status_field("phyname")
  72. radar_file = '/sys/kernel/debug/ieee80211/' + phyname + '/hwsim/dfs_simulate_radar'
  73. with open(radar_file, 'w') as f:
  74. f.write('1')
  75. def test_dfs(dev, apdev):
  76. """DFS CAC functionality on clear channel"""
  77. try:
  78. hapd = None
  79. hapd = start_dfs_ap(apdev[0], allow_failure=True, country="US")
  80. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  81. if "success=1" not in ev:
  82. raise Exception("CAC failed")
  83. if "freq=5260" not in ev:
  84. raise Exception("Unexpected DFS freq result")
  85. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  86. if not ev:
  87. raise Exception("AP setup timed out")
  88. state = hapd.get_status_field("state")
  89. if state != "ENABLED":
  90. raise Exception("Unexpected interface state")
  91. freq = hapd.get_status_field("freq")
  92. if freq != "5260":
  93. raise Exception("Unexpected frequency")
  94. dev[0].connect("dfs", key_mgmt="NONE")
  95. hwsim_utils.test_connectivity(dev[0], hapd)
  96. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  97. ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=10)
  98. if ev is None:
  99. raise Exception("DFS-RADAR-DETECTED event not reported")
  100. if "freq=5260" not in ev:
  101. raise Exception("Incorrect frequency in radar detected event: " + ev)
  102. ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=70)
  103. if ev is None:
  104. raise Exception("DFS-NEW-CHANNEL event not reported")
  105. if "freq=5260" in ev:
  106. raise Exception("Channel did not change after radar was detected")
  107. ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=70)
  108. if ev is None:
  109. raise Exception("AP-CSA-FINISHED event not reported")
  110. if "freq=5260" in ev:
  111. raise Exception("Channel did not change after radar was detected(2)")
  112. time.sleep(1)
  113. hwsim_utils.test_connectivity(dev[0], hapd)
  114. finally:
  115. dev[0].request("DISCONNECT")
  116. if hapd:
  117. hapd.request("DISABLE")
  118. subprocess.call(['iw', 'reg', 'set', '00'])
  119. dev[0].flush_scan_cache()
  120. def test_dfs_etsi(dev, apdev, params):
  121. """DFS and uniform spreading requirement for ETSI [long]"""
  122. if not params['long']:
  123. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  124. try:
  125. hapd = None
  126. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  127. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  128. if "success=1" not in ev:
  129. raise Exception("CAC failed")
  130. if "freq=5260" not in ev:
  131. raise Exception("Unexpected DFS freq result")
  132. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  133. if not ev:
  134. raise Exception("AP setup timed out")
  135. state = hapd.get_status_field("state")
  136. if state != "ENABLED":
  137. raise Exception("Unexpected interface state")
  138. freq = hapd.get_status_field("freq")
  139. if freq != "5260":
  140. raise Exception("Unexpected frequency")
  141. dev[0].connect("dfs", key_mgmt="NONE")
  142. hwsim_utils.test_connectivity(dev[0], hapd)
  143. hapd.request("RADAR DETECTED freq=%s ht_enabled=1 chan_width=1" % freq)
  144. ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=5)
  145. if ev is None:
  146. raise Exception("DFS-RADAR-DETECTED event not reported")
  147. if "freq=%s" % freq not in ev:
  148. raise Exception("Incorrect frequency in radar detected event: " + ev)
  149. ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=5)
  150. if ev is None:
  151. raise Exception("DFS-NEW-CHANNEL event not reported")
  152. if "freq=%s" % freq in ev:
  153. raise Exception("Channel did not change after radar was detected")
  154. ev = hapd.wait_event(["AP-CSA-FINISHED", "DFS-CAC-START"], timeout=10)
  155. if ev is None:
  156. raise Exception("AP-CSA-FINISHED or DFS-CAC-START event not reported")
  157. if "DFS-CAC-START" in ev:
  158. # The selected new channel requires CAC
  159. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  160. if "success=1" not in ev:
  161. raise Exception("CAC failed")
  162. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  163. if not ev:
  164. raise Exception("AP setup timed out")
  165. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=30)
  166. if not ev:
  167. raise Exception("STA did not reconnect on new DFS channel")
  168. else:
  169. # The new channel did not require CAC - try again
  170. if "freq=%s" % freq in ev:
  171. raise Exception("Channel did not change after radar was detected(2)")
  172. time.sleep(1)
  173. hwsim_utils.test_connectivity(dev[0], hapd)
  174. finally:
  175. dev[0].request("DISCONNECT")
  176. if hapd:
  177. hapd.request("DISABLE")
  178. subprocess.call(['iw', 'reg', 'set', '00'])
  179. dev[0].flush_scan_cache()
  180. def test_dfs_radar(dev, apdev):
  181. """DFS CAC functionality with radar detected"""
  182. try:
  183. hapd = None
  184. hapd2 = None
  185. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  186. time.sleep(1)
  187. dfs_simulate_radar(hapd)
  188. hapd2 = start_dfs_ap(apdev[1], ssid="dfs2", ht40=True)
  189. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  190. if ev is None:
  191. raise Exception("Timeout on DFS aborted event")
  192. if "success=0 freq=5260" not in ev:
  193. raise Exception("Unexpected DFS aborted event contents: " + ev)
  194. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  195. if "freq=5260" not in ev:
  196. raise Exception("Unexpected DFS radar detection freq")
  197. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  198. if "freq=5260" in ev:
  199. raise Exception("Unexpected DFS new freq")
  200. ev = wait_dfs_event(hapd, None, 5)
  201. if "AP-ENABLED" in ev:
  202. logger.info("Started AP on non-DFS channel")
  203. else:
  204. logger.info("Trying to start AP on another DFS channel")
  205. if "DFS-CAC-START" not in ev:
  206. raise Exception("Unexpected DFS event: " + ev)
  207. if "freq=5260" in ev:
  208. raise Exception("Unexpected DFS CAC freq")
  209. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  210. if "success=1" not in ev:
  211. raise Exception("CAC failed")
  212. if "freq=5260" in ev:
  213. raise Exception("Unexpected DFS freq result - radar channel")
  214. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  215. if not ev:
  216. raise Exception("AP setup timed out")
  217. state = hapd.get_status_field("state")
  218. if state != "ENABLED":
  219. raise Exception("Unexpected interface state")
  220. freq = hapd.get_status_field("freq")
  221. if freq == "5260":
  222. raise Exception("Unexpected frequency: " + freq)
  223. dev[0].connect("dfs", key_mgmt="NONE")
  224. ev = hapd2.wait_event(["AP-ENABLED"], timeout=70)
  225. if not ev:
  226. raise Exception("AP2 setup timed out")
  227. dfs_simulate_radar(hapd2)
  228. ev = wait_dfs_event(hapd2, "DFS-RADAR-DETECTED", 5)
  229. if "freq=5260 ht_enabled=1 chan_offset=1 chan_width=2" not in ev:
  230. raise Exception("Unexpected DFS radar detection freq from AP2")
  231. ev = wait_dfs_event(hapd2, "DFS-NEW-CHANNEL", 5)
  232. if "freq=5260" in ev:
  233. raise Exception("Unexpected DFS new freq for AP2")
  234. wait_dfs_event(hapd2, None, 5)
  235. finally:
  236. dev[0].request("DISCONNECT")
  237. if hapd:
  238. hapd.request("DISABLE")
  239. if hapd2:
  240. hapd2.request("DISABLE")
  241. subprocess.call(['iw', 'reg', 'set', '00'])
  242. dev[0].flush_scan_cache()
  243. @remote_compatible
  244. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  245. """DFS radar detection test code on non-DFS channel"""
  246. params = { "ssid": "radar" }
  247. hapd = hostapd.add_ap(apdev[0], params)
  248. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  249. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")
  250. def test_dfs_radar_chanlist(dev, apdev):
  251. """DFS chanlist when radar is detected"""
  252. try:
  253. hapd = None
  254. hapd = start_dfs_ap(apdev[0], chanlist="40 44", allow_failure=True)
  255. time.sleep(1)
  256. dfs_simulate_radar(hapd)
  257. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  258. if ev is None:
  259. raise Exception("Timeout on DFS aborted event")
  260. if "success=0 freq=5260" not in ev:
  261. raise Exception("Unexpected DFS aborted event contents: " + ev)
  262. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  263. if "freq=5260" not in ev:
  264. raise Exception("Unexpected DFS radar detection freq")
  265. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  266. if "freq=5200 chan=40" not in ev and "freq=5220 chan=44" not in ev:
  267. raise Exception("Unexpected DFS new freq: " + ev)
  268. ev = wait_dfs_event(hapd, None, 5)
  269. if "AP-ENABLED" not in ev:
  270. raise Exception("Unexpected DFS event: " + ev)
  271. dev[0].connect("dfs", key_mgmt="NONE")
  272. finally:
  273. dev[0].request("DISCONNECT")
  274. if hapd:
  275. hapd.request("DISABLE")
  276. subprocess.call(['iw', 'reg', 'set', '00'])
  277. dev[0].flush_scan_cache()
  278. def test_dfs_radar_chanlist_vht80(dev, apdev):
  279. """DFS chanlist when radar is detected and VHT80 configured"""
  280. try:
  281. hapd = None
  282. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40=True, vht80=True,
  283. allow_failure=True)
  284. time.sleep(1)
  285. dfs_simulate_radar(hapd)
  286. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  287. if ev is None:
  288. raise Exception("Timeout on DFS aborted event")
  289. if "success=0 freq=5260" not in ev:
  290. raise Exception("Unexpected DFS aborted event contents: " + ev)
  291. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  292. if "freq=5260" not in ev:
  293. raise Exception("Unexpected DFS radar detection freq")
  294. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  295. if "freq=5180 chan=36 sec_chan=1" not in ev:
  296. raise Exception("Unexpected DFS new freq: " + ev)
  297. ev = wait_dfs_event(hapd, None, 5)
  298. if "AP-ENABLED" not in ev:
  299. raise Exception("Unexpected DFS event: " + ev)
  300. dev[0].connect("dfs", key_mgmt="NONE")
  301. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  302. raise Exception("Unexpected seg0 idx")
  303. finally:
  304. dev[0].request("DISCONNECT")
  305. if hapd:
  306. hapd.request("DISABLE")
  307. subprocess.call(['iw', 'reg', 'set', '00'])
  308. dev[0].flush_scan_cache()
  309. def test_dfs_radar_chanlist_vht20(dev, apdev):
  310. """DFS chanlist when radar is detected and VHT40 configured"""
  311. try:
  312. hapd = None
  313. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True,
  314. allow_failure=True)
  315. time.sleep(1)
  316. dfs_simulate_radar(hapd)
  317. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  318. if ev is None:
  319. raise Exception("Timeout on DFS aborted event")
  320. if "success=0 freq=5260" not in ev:
  321. raise Exception("Unexpected DFS aborted event contents: " + ev)
  322. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  323. if "freq=5260" not in ev:
  324. raise Exception("Unexpected DFS radar detection freq")
  325. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  326. if "freq=5180 chan=36 sec_chan=0" not in ev:
  327. raise Exception("Unexpected DFS new freq: " + ev)
  328. ev = wait_dfs_event(hapd, None, 5)
  329. if "AP-ENABLED" not in ev:
  330. raise Exception("Unexpected DFS event: " + ev)
  331. dev[0].connect("dfs", key_mgmt="NONE")
  332. finally:
  333. dev[0].request("DISCONNECT")
  334. if hapd:
  335. hapd.request("DISABLE")
  336. subprocess.call(['iw', 'reg', 'set', '00'])
  337. dev[0].flush_scan_cache()
  338. def test_dfs_radar_no_ht(dev, apdev):
  339. """DFS chanlist when radar is detected and no HT configured"""
  340. try:
  341. hapd = None
  342. hapd = start_dfs_ap(apdev[0], chanlist="36", ht=False,
  343. allow_failure=True)
  344. time.sleep(1)
  345. dfs_simulate_radar(hapd)
  346. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  347. if ev is None:
  348. raise Exception("Timeout on DFS aborted event")
  349. if "success=0 freq=5260" not in ev:
  350. raise Exception("Unexpected DFS aborted event contents: " + ev)
  351. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  352. if "freq=5260 ht_enabled=0" not in ev:
  353. raise Exception("Unexpected DFS radar detection freq: " + ev)
  354. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  355. if "freq=5180 chan=36 sec_chan=0" not in ev:
  356. raise Exception("Unexpected DFS new freq: " + ev)
  357. ev = wait_dfs_event(hapd, None, 5)
  358. if "AP-ENABLED" not in ev:
  359. raise Exception("Unexpected DFS event: " + ev)
  360. dev[0].connect("dfs", key_mgmt="NONE")
  361. finally:
  362. dev[0].request("DISCONNECT")
  363. if hapd:
  364. hapd.request("DISABLE")
  365. subprocess.call(['iw', 'reg', 'set', '00'])
  366. dev[0].flush_scan_cache()
  367. def test_dfs_radar_ht40minus(dev, apdev):
  368. """DFS chanlist when radar is detected and HT40- configured"""
  369. try:
  370. hapd = None
  371. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40minus=True,
  372. allow_failure=True)
  373. time.sleep(1)
  374. dfs_simulate_radar(hapd)
  375. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  376. if ev is None:
  377. raise Exception("Timeout on DFS aborted event")
  378. if "success=0 freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  379. raise Exception("Unexpected DFS aborted event contents: " + ev)
  380. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  381. if "freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  382. raise Exception("Unexpected DFS radar detection freq: " + ev)
  383. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  384. if "freq=5180 chan=36 sec_chan=1" not in ev:
  385. raise Exception("Unexpected DFS new freq: " + ev)
  386. ev = wait_dfs_event(hapd, None, 5)
  387. if "AP-ENABLED" not in ev:
  388. raise Exception("Unexpected DFS event: " + ev)
  389. dev[0].connect("dfs", key_mgmt="NONE")
  390. finally:
  391. dev[0].request("DISCONNECT")
  392. if hapd:
  393. hapd.request("DISABLE")
  394. subprocess.call(['iw', 'reg', 'set', '00'])
  395. dev[0].flush_scan_cache()
  396. def test_dfs_ht40_minus(dev, apdev, params):
  397. """DFS CAC functionality on channel 104 HT40- [long]"""
  398. if not params['long']:
  399. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  400. try:
  401. hapd = None
  402. hapd = start_dfs_ap(apdev[0], allow_failure=True, ht40minus=True,
  403. channel=104)
  404. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  405. if "success=1" not in ev:
  406. raise Exception("CAC failed")
  407. if "freq=5520" not in ev:
  408. raise Exception("Unexpected DFS freq result")
  409. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  410. if not ev:
  411. raise Exception("AP setup timed out")
  412. state = hapd.get_status_field("state")
  413. if state != "ENABLED":
  414. raise Exception("Unexpected interface state")
  415. freq = hapd.get_status_field("freq")
  416. if freq != "5520":
  417. raise Exception("Unexpected frequency")
  418. dev[0].connect("dfs", key_mgmt="NONE", scan_freq="5520")
  419. hwsim_utils.test_connectivity(dev[0], hapd)
  420. finally:
  421. dev[0].request("DISCONNECT")
  422. if hapd:
  423. hapd.request("DISABLE")
  424. subprocess.call(['iw', 'reg', 'set', '00'])
  425. dev[0].flush_scan_cache()