test_dfs.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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_radar1(dev, apdev):
  181. """DFS CAC functionality with radar detected during initial CAC"""
  182. try:
  183. hapd = None
  184. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  185. time.sleep(1)
  186. dfs_simulate_radar(hapd)
  187. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  188. if ev is None:
  189. raise Exception("Timeout on DFS aborted event")
  190. if "success=0 freq=5260" not in ev:
  191. raise Exception("Unexpected DFS aborted event contents: " + ev)
  192. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  193. if "freq=5260" not in ev:
  194. raise Exception("Unexpected DFS radar detection freq")
  195. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  196. if "freq=5260" in ev:
  197. raise Exception("Unexpected DFS new freq")
  198. ev = wait_dfs_event(hapd, None, 5)
  199. if "AP-ENABLED" in ev:
  200. logger.info("Started AP on non-DFS channel")
  201. else:
  202. logger.info("Trying to start AP on another DFS channel")
  203. if "DFS-CAC-START" not in ev:
  204. raise Exception("Unexpected DFS event: " + ev)
  205. if "freq=5260" in ev:
  206. raise Exception("Unexpected DFS CAC freq")
  207. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  208. if "success=1" not in ev:
  209. raise Exception("CAC failed")
  210. if "freq=5260" in ev:
  211. raise Exception("Unexpected DFS freq result - radar channel")
  212. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  213. if not ev:
  214. raise Exception("AP setup timed out")
  215. state = hapd.get_status_field("state")
  216. if state != "ENABLED":
  217. raise Exception("Unexpected interface state")
  218. freq = hapd.get_status_field("freq")
  219. if freq == "5260":
  220. raise Exception("Unexpected frequency: " + freq)
  221. dev[0].connect("dfs", key_mgmt="NONE")
  222. finally:
  223. dev[0].request("DISCONNECT")
  224. if hapd:
  225. hapd.request("DISABLE")
  226. subprocess.call(['iw', 'reg', 'set', '00'])
  227. dev[0].flush_scan_cache()
  228. def test_dfs_radar2(dev, apdev):
  229. """DFS CAC functionality with radar detected after initial CAC"""
  230. try:
  231. hapd = None
  232. hapd = start_dfs_ap(apdev[0], ssid="dfs2", ht40=True)
  233. ev = hapd.wait_event(["AP-ENABLED"], timeout=70)
  234. if not ev:
  235. raise Exception("AP2 setup timed out")
  236. dfs_simulate_radar(hapd)
  237. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  238. if "freq=5260 ht_enabled=1 chan_offset=1 chan_width=2" not in ev:
  239. raise Exception("Unexpected DFS radar detection freq from AP2")
  240. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  241. if "freq=5260" in ev:
  242. raise Exception("Unexpected DFS new freq for AP2")
  243. wait_dfs_event(hapd, None, 5)
  244. finally:
  245. if hapd:
  246. hapd.request("DISABLE")
  247. subprocess.call(['iw', 'reg', 'set', '00'])
  248. @remote_compatible
  249. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  250. """DFS radar detection test code on non-DFS channel"""
  251. params = { "ssid": "radar" }
  252. hapd = hostapd.add_ap(apdev[0], params)
  253. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  254. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")
  255. def test_dfs_radar_chanlist(dev, apdev):
  256. """DFS chanlist when radar is detected"""
  257. try:
  258. hapd = None
  259. hapd = start_dfs_ap(apdev[0], chanlist="40 44", allow_failure=True)
  260. time.sleep(1)
  261. dfs_simulate_radar(hapd)
  262. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  263. if ev is None:
  264. raise Exception("Timeout on DFS aborted event")
  265. if "success=0 freq=5260" not in ev:
  266. raise Exception("Unexpected DFS aborted event contents: " + ev)
  267. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  268. if "freq=5260" not in ev:
  269. raise Exception("Unexpected DFS radar detection freq")
  270. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  271. if "freq=5200 chan=40" not in ev and "freq=5220 chan=44" not in ev:
  272. raise Exception("Unexpected DFS new freq: " + ev)
  273. ev = wait_dfs_event(hapd, None, 5)
  274. if "AP-ENABLED" not in ev:
  275. raise Exception("Unexpected DFS event: " + ev)
  276. dev[0].connect("dfs", key_mgmt="NONE")
  277. finally:
  278. dev[0].request("DISCONNECT")
  279. if hapd:
  280. hapd.request("DISABLE")
  281. subprocess.call(['iw', 'reg', 'set', '00'])
  282. dev[0].flush_scan_cache()
  283. def test_dfs_radar_chanlist_vht80(dev, apdev):
  284. """DFS chanlist when radar is detected and VHT80 configured"""
  285. try:
  286. hapd = None
  287. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40=True, vht80=True,
  288. allow_failure=True)
  289. time.sleep(1)
  290. dfs_simulate_radar(hapd)
  291. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  292. if ev is None:
  293. raise Exception("Timeout on DFS aborted event")
  294. if "success=0 freq=5260" not in ev:
  295. raise Exception("Unexpected DFS aborted event contents: " + ev)
  296. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  297. if "freq=5260" not in ev:
  298. raise Exception("Unexpected DFS radar detection freq")
  299. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  300. if "freq=5180 chan=36 sec_chan=1" not in ev:
  301. raise Exception("Unexpected DFS new freq: " + ev)
  302. ev = wait_dfs_event(hapd, None, 5)
  303. if "AP-ENABLED" not in ev:
  304. raise Exception("Unexpected DFS event: " + ev)
  305. dev[0].connect("dfs", key_mgmt="NONE")
  306. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  307. raise Exception("Unexpected seg0 idx")
  308. finally:
  309. dev[0].request("DISCONNECT")
  310. if hapd:
  311. hapd.request("DISABLE")
  312. subprocess.call(['iw', 'reg', 'set', '00'])
  313. dev[0].flush_scan_cache()
  314. def test_dfs_radar_chanlist_vht20(dev, apdev):
  315. """DFS chanlist when radar is detected and VHT40 configured"""
  316. try:
  317. hapd = None
  318. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True,
  319. allow_failure=True)
  320. time.sleep(1)
  321. dfs_simulate_radar(hapd)
  322. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  323. if ev is None:
  324. raise Exception("Timeout on DFS aborted event")
  325. if "success=0 freq=5260" not in ev:
  326. raise Exception("Unexpected DFS aborted event contents: " + ev)
  327. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  328. if "freq=5260" not in ev:
  329. raise Exception("Unexpected DFS radar detection freq")
  330. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  331. if "freq=5180 chan=36 sec_chan=0" not in ev:
  332. raise Exception("Unexpected DFS new freq: " + ev)
  333. ev = wait_dfs_event(hapd, None, 5)
  334. if "AP-ENABLED" not in ev:
  335. raise Exception("Unexpected DFS event: " + ev)
  336. dev[0].connect("dfs", key_mgmt="NONE")
  337. finally:
  338. dev[0].request("DISCONNECT")
  339. if hapd:
  340. hapd.request("DISABLE")
  341. subprocess.call(['iw', 'reg', 'set', '00'])
  342. dev[0].flush_scan_cache()
  343. def test_dfs_radar_no_ht(dev, apdev):
  344. """DFS chanlist when radar is detected and no HT configured"""
  345. try:
  346. hapd = None
  347. hapd = start_dfs_ap(apdev[0], chanlist="36", ht=False,
  348. allow_failure=True)
  349. time.sleep(1)
  350. dfs_simulate_radar(hapd)
  351. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  352. if ev is None:
  353. raise Exception("Timeout on DFS aborted event")
  354. if "success=0 freq=5260" not in ev:
  355. raise Exception("Unexpected DFS aborted event contents: " + ev)
  356. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  357. if "freq=5260 ht_enabled=0" not in ev:
  358. raise Exception("Unexpected DFS radar detection freq: " + ev)
  359. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  360. if "freq=5180 chan=36 sec_chan=0" not in ev:
  361. raise Exception("Unexpected DFS new freq: " + ev)
  362. ev = wait_dfs_event(hapd, None, 5)
  363. if "AP-ENABLED" not in ev:
  364. raise Exception("Unexpected DFS event: " + ev)
  365. dev[0].connect("dfs", key_mgmt="NONE")
  366. finally:
  367. dev[0].request("DISCONNECT")
  368. if hapd:
  369. hapd.request("DISABLE")
  370. subprocess.call(['iw', 'reg', 'set', '00'])
  371. dev[0].flush_scan_cache()
  372. def test_dfs_radar_ht40minus(dev, apdev):
  373. """DFS chanlist when radar is detected and HT40- configured"""
  374. try:
  375. hapd = None
  376. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40minus=True,
  377. allow_failure=True)
  378. time.sleep(1)
  379. dfs_simulate_radar(hapd)
  380. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  381. if ev is None:
  382. raise Exception("Timeout on DFS aborted event")
  383. if "success=0 freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  384. raise Exception("Unexpected DFS aborted event contents: " + ev)
  385. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  386. if "freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  387. raise Exception("Unexpected DFS radar detection freq: " + ev)
  388. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  389. if "freq=5180 chan=36 sec_chan=1" not in ev:
  390. raise Exception("Unexpected DFS new freq: " + ev)
  391. ev = wait_dfs_event(hapd, None, 5)
  392. if "AP-ENABLED" not in ev:
  393. raise Exception("Unexpected DFS event: " + ev)
  394. dev[0].connect("dfs", key_mgmt="NONE")
  395. finally:
  396. dev[0].request("DISCONNECT")
  397. if hapd:
  398. hapd.request("DISABLE")
  399. subprocess.call(['iw', 'reg', 'set', '00'])
  400. dev[0].flush_scan_cache()
  401. def test_dfs_ht40_minus(dev, apdev, params):
  402. """DFS CAC functionality on channel 104 HT40- [long]"""
  403. if not params['long']:
  404. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  405. try:
  406. hapd = None
  407. hapd = start_dfs_ap(apdev[0], allow_failure=True, ht40minus=True,
  408. channel=104)
  409. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  410. if "success=1" not in ev:
  411. raise Exception("CAC failed")
  412. if "freq=5520" not in ev:
  413. raise Exception("Unexpected DFS freq result")
  414. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  415. if not ev:
  416. raise Exception("AP setup timed out")
  417. state = hapd.get_status_field("state")
  418. if state != "ENABLED":
  419. raise Exception("Unexpected interface state")
  420. freq = hapd.get_status_field("freq")
  421. if freq != "5520":
  422. raise Exception("Unexpected frequency")
  423. dev[0].connect("dfs", key_mgmt="NONE", scan_freq="5520")
  424. hwsim_utils.test_connectivity(dev[0], hapd)
  425. finally:
  426. dev[0].request("DISCONNECT")
  427. if hapd:
  428. hapd.request("DISABLE")
  429. subprocess.call(['iw', 'reg', 'set', '00'])
  430. dev[0].flush_scan_cache()