xref: /freebsd/sys/net80211/ieee80211_dfs.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1b032f27cSSam Leffler /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4b032f27cSSam Leffler  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5b032f27cSSam Leffler  * All rights reserved.
6b032f27cSSam Leffler  *
7b032f27cSSam Leffler  * Redistribution and use in source and binary forms, with or without
8b032f27cSSam Leffler  * modification, are permitted provided that the following conditions
9b032f27cSSam Leffler  * are met:
10b032f27cSSam Leffler  * 1. Redistributions of source code must retain the above copyright
11b032f27cSSam Leffler  *    notice, this list of conditions and the following disclaimer.
12b032f27cSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
13b032f27cSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
14b032f27cSSam Leffler  *    documentation and/or other materials provided with the distribution.
15b032f27cSSam Leffler  *
16b032f27cSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17b032f27cSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18b032f27cSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19b032f27cSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20b032f27cSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21b032f27cSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22b032f27cSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23b032f27cSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24b032f27cSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25b032f27cSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b032f27cSSam Leffler  */
27b032f27cSSam Leffler 
28b032f27cSSam Leffler /*
29b032f27cSSam Leffler  * IEEE 802.11 DFS/Radar support.
30b032f27cSSam Leffler  */
31b032f27cSSam Leffler #include "opt_inet.h"
32b032f27cSSam Leffler #include "opt_wlan.h"
33b032f27cSSam Leffler 
34b032f27cSSam Leffler #include <sys/param.h>
35b032f27cSSam Leffler #include <sys/systm.h>
36b032f27cSSam Leffler #include <sys/mbuf.h>
37b032f27cSSam Leffler #include <sys/malloc.h>
38b032f27cSSam Leffler #include <sys/kernel.h>
39b032f27cSSam Leffler 
40b032f27cSSam Leffler #include <sys/socket.h>
41b032f27cSSam Leffler #include <sys/sockio.h>
42b032f27cSSam Leffler #include <sys/endian.h>
43b032f27cSSam Leffler #include <sys/errno.h>
44b032f27cSSam Leffler #include <sys/proc.h>
45b032f27cSSam Leffler #include <sys/sysctl.h>
46b032f27cSSam Leffler 
47b032f27cSSam Leffler #include <net/if.h>
4876039bc8SGleb Smirnoff #include <net/if_var.h>
49b032f27cSSam Leffler #include <net/if_media.h>
50c3322cb9SGleb Smirnoff #include <net/ethernet.h>
51b032f27cSSam Leffler 
52b032f27cSSam Leffler #include <net80211/ieee80211_var.h>
53b032f27cSSam Leffler 
54d745c852SEd Schouten static MALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state");
55b032f27cSSam Leffler 
56a77b10b3SSam Leffler static	int ieee80211_nol_timeout = 30*60;		/* 30 minutes */
57a77b10b3SSam Leffler SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW,
58a77b10b3SSam Leffler 	&ieee80211_nol_timeout, 0, "NOL timeout (secs)");
59b032f27cSSam Leffler #define	NOL_TIMEOUT	msecs_to_ticks(ieee80211_nol_timeout*1000)
60a77b10b3SSam Leffler 
61a77b10b3SSam Leffler static	int ieee80211_cac_timeout = 60;		/* 60 seconds */
62a77b10b3SSam Leffler SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW,
63a77b10b3SSam Leffler 	&ieee80211_cac_timeout, 0, "CAC timeout (secs)");
64b032f27cSSam Leffler #define	CAC_TIMEOUT	msecs_to_ticks(ieee80211_cac_timeout*1000)
65b032f27cSSam Leffler 
6662f8a13aSAdrian Chadd /*
6762f8a13aSAdrian Chadd  DFS* In order to facilitate  debugging, a couple of operating
6862f8a13aSAdrian Chadd  * modes aside from the default are needed.
6962f8a13aSAdrian Chadd  *
7062f8a13aSAdrian Chadd  * 0 - default CAC/NOL behaviour - ie, start CAC, place
7162f8a13aSAdrian Chadd  *     channel on NOL list.
7262f8a13aSAdrian Chadd  * 1 - send CAC, but don't change channel or add the channel
7362f8a13aSAdrian Chadd  *     to the NOL list.
7462f8a13aSAdrian Chadd  * 2 - just match on radar, don't send CAC or place channel in
7562f8a13aSAdrian Chadd  *     the NOL list.
7662f8a13aSAdrian Chadd  */
7762f8a13aSAdrian Chadd static	int ieee80211_dfs_debug = DFS_DBG_NONE;
7862f8a13aSAdrian Chadd 
7962f8a13aSAdrian Chadd /*
8062f8a13aSAdrian Chadd  * This option must not be included in the default kernel
8162f8a13aSAdrian Chadd  * as it allows users to plainly disable CAC/NOL handling.
8262f8a13aSAdrian Chadd  */
8362f8a13aSAdrian Chadd #ifdef	IEEE80211_DFS_DEBUG
8462f8a13aSAdrian Chadd SYSCTL_INT(_net_wlan, OID_AUTO, dfs_debug, CTLFLAG_RW,
8562f8a13aSAdrian Chadd 	&ieee80211_dfs_debug, 0, "DFS debug behaviour");
8662f8a13aSAdrian Chadd #endif
8762f8a13aSAdrian Chadd 
8832b0e64bSAdrian Chadd static int
null_set_quiet(struct ieee80211_node * ni,u_int8_t * quiet_elm)8932b0e64bSAdrian Chadd null_set_quiet(struct ieee80211_node *ni, u_int8_t *quiet_elm)
9032b0e64bSAdrian Chadd {
9132b0e64bSAdrian Chadd 	return ENOSYS;
9232b0e64bSAdrian Chadd }
9332b0e64bSAdrian Chadd 
94b032f27cSSam Leffler void
ieee80211_dfs_attach(struct ieee80211com * ic)95b032f27cSSam Leffler ieee80211_dfs_attach(struct ieee80211com *ic)
96b032f27cSSam Leffler {
97b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
98b032f27cSSam Leffler 
9992c4a81cSSam Leffler 	callout_init_mtx(&dfs->nol_timer, IEEE80211_LOCK_OBJ(ic), 0);
10092c4a81cSSam Leffler 	callout_init_mtx(&dfs->cac_timer, IEEE80211_LOCK_OBJ(ic), 0);
10132b0e64bSAdrian Chadd 
10232b0e64bSAdrian Chadd 	ic->ic_set_quiet = null_set_quiet;
103b032f27cSSam Leffler }
104b032f27cSSam Leffler 
105b032f27cSSam Leffler void
ieee80211_dfs_detach(struct ieee80211com * ic)106b032f27cSSam Leffler ieee80211_dfs_detach(struct ieee80211com *ic)
107b032f27cSSam Leffler {
108b032f27cSSam Leffler 	/* NB: we assume no locking is needed */
109b032f27cSSam Leffler 	ieee80211_dfs_reset(ic);
110b032f27cSSam Leffler }
111b032f27cSSam Leffler 
112b032f27cSSam Leffler void
ieee80211_dfs_reset(struct ieee80211com * ic)113b032f27cSSam Leffler ieee80211_dfs_reset(struct ieee80211com *ic)
114b032f27cSSam Leffler {
115b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
116b032f27cSSam Leffler 	int i;
117b032f27cSSam Leffler 
118b032f27cSSam Leffler 	/* NB: we assume no locking is needed */
119b032f27cSSam Leffler 	/* NB: cac_timer should be cleared by the state machine */
120b032f27cSSam Leffler 	callout_drain(&dfs->nol_timer);
121b032f27cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++)
122b032f27cSSam Leffler 		ic->ic_channels[i].ic_state = 0;
123b032f27cSSam Leffler 	dfs->lastchan = NULL;
124b032f27cSSam Leffler }
125b032f27cSSam Leffler 
126b032f27cSSam Leffler static void
cac_timeout(void * arg)127b032f27cSSam Leffler cac_timeout(void *arg)
128b032f27cSSam Leffler {
129b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
130b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
131b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
132b032f27cSSam Leffler 	int i;
133b032f27cSSam Leffler 
13492c4a81cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
13592c4a81cSSam Leffler 
136b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_CAC)	/* NB: just in case */
137b032f27cSSam Leffler 		return;
138b032f27cSSam Leffler 	/*
139b032f27cSSam Leffler 	 * When radar is detected during a CAC we are woken
140b032f27cSSam Leffler 	 * up prematurely to switch to a new channel.
141b032f27cSSam Leffler 	 * Check the channel to decide how to act.
142b032f27cSSam Leffler 	 */
143b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
144b032f27cSSam Leffler 		ieee80211_notify_cac(ic, ic->ic_curchan,
145b032f27cSSam Leffler 		    IEEE80211_NOTIFY_CAC_RADAR);
146b032f27cSSam Leffler 
147b032f27cSSam Leffler 		if_printf(vap->iv_ifp,
148b032f27cSSam Leffler 		    "CAC timer on channel %u (%u MHz) stopped due to radar\n",
149b032f27cSSam Leffler 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
150b032f27cSSam Leffler 
151b032f27cSSam Leffler 		/* XXX clobbers any existing desired channel */
152b032f27cSSam Leffler 		/* NB: dfs->newchan may be NULL, that's ok */
153b032f27cSSam Leffler 		vap->iv_des_chan = dfs->newchan;
1544ab4d681SAndriy Voskoboinyk 		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN, 0);
155b032f27cSSam Leffler 	} else {
156b032f27cSSam Leffler 		if_printf(vap->iv_ifp,
157b032f27cSSam Leffler 		    "CAC timer on channel %u (%u MHz) expired; "
158b032f27cSSam Leffler 		    "no radar detected\n",
159b032f27cSSam Leffler 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
160b032f27cSSam Leffler 		/*
161b032f27cSSam Leffler 		 * Mark all channels with the current frequency
162b032f27cSSam Leffler 		 * as having completed CAC; this keeps us from
163b032f27cSSam Leffler 		 * doing it again until we change channels.
164b032f27cSSam Leffler 		 */
165b032f27cSSam Leffler 		for (i = 0; i < ic->ic_nchans; i++) {
166b032f27cSSam Leffler 			struct ieee80211_channel *c = &ic->ic_channels[i];
167b032f27cSSam Leffler 			if (c->ic_freq == ic->ic_curchan->ic_freq)
168b032f27cSSam Leffler 				c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
169b032f27cSSam Leffler 		}
170b032f27cSSam Leffler 		ieee80211_notify_cac(ic, ic->ic_curchan,
171b032f27cSSam Leffler 		    IEEE80211_NOTIFY_CAC_EXPIRE);
172b032f27cSSam Leffler 		ieee80211_cac_completeswitch(vap);
173b032f27cSSam Leffler 	}
174b032f27cSSam Leffler }
175b032f27cSSam Leffler 
176b032f27cSSam Leffler /*
177b032f27cSSam Leffler  * Initiate the CAC timer.  The driver is responsible
178b032f27cSSam Leffler  * for setting up the hardware to scan for radar on the
179b032f27cSSam Leffler  * channnel, we just handle timing things out.
180b032f27cSSam Leffler  */
181b032f27cSSam Leffler void
ieee80211_dfs_cac_start(struct ieee80211vap * vap)182b032f27cSSam Leffler ieee80211_dfs_cac_start(struct ieee80211vap *vap)
183b032f27cSSam Leffler {
184b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
185b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
186b032f27cSSam Leffler 
187b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
188b032f27cSSam Leffler 
189b032f27cSSam Leffler 	callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap);
190b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
191b032f27cSSam Leffler 	    ticks_to_secs(CAC_TIMEOUT),
192b032f27cSSam Leffler 	    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
193b032f27cSSam Leffler 	ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
194b032f27cSSam Leffler }
195b032f27cSSam Leffler 
196b032f27cSSam Leffler /*
197b032f27cSSam Leffler  * Clear the CAC timer.
198b032f27cSSam Leffler  */
199b032f27cSSam Leffler void
ieee80211_dfs_cac_stop(struct ieee80211vap * vap)200b032f27cSSam Leffler ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
201b032f27cSSam Leffler {
202b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
203b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
204b032f27cSSam Leffler 
205b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
206b032f27cSSam Leffler 
207b032f27cSSam Leffler 	/* NB: racey but not important */
208b032f27cSSam Leffler 	if (callout_pending(&dfs->cac_timer)) {
209b032f27cSSam Leffler 		if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
210b032f27cSSam Leffler 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
211b032f27cSSam Leffler 		ieee80211_notify_cac(ic, ic->ic_curchan,
212b032f27cSSam Leffler 		    IEEE80211_NOTIFY_CAC_STOP);
213b032f27cSSam Leffler 	}
214b032f27cSSam Leffler 	callout_stop(&dfs->cac_timer);
215b032f27cSSam Leffler }
216b032f27cSSam Leffler 
217b032f27cSSam Leffler void
ieee80211_dfs_cac_clear(struct ieee80211com * ic,const struct ieee80211_channel * chan)218b032f27cSSam Leffler ieee80211_dfs_cac_clear(struct ieee80211com *ic,
219b032f27cSSam Leffler 	const struct ieee80211_channel *chan)
220b032f27cSSam Leffler {
221b032f27cSSam Leffler 	int i;
222b032f27cSSam Leffler 
223b032f27cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
224b032f27cSSam Leffler 		struct ieee80211_channel *c = &ic->ic_channels[i];
225b032f27cSSam Leffler 		if (c->ic_freq == chan->ic_freq)
226b032f27cSSam Leffler 			c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
227b032f27cSSam Leffler 	}
228b032f27cSSam Leffler }
229b032f27cSSam Leffler 
230b032f27cSSam Leffler static void
dfs_timeout(void * arg)231b032f27cSSam Leffler dfs_timeout(void *arg)
232b032f27cSSam Leffler {
233b032f27cSSam Leffler 	struct ieee80211com *ic = arg;
234b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
235b032f27cSSam Leffler 	struct ieee80211_channel *c;
236b032f27cSSam Leffler 	int i, oldest, now;
237b032f27cSSam Leffler 
23892c4a81cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
23992c4a81cSSam Leffler 
240b032f27cSSam Leffler 	now = oldest = ticks;
241b032f27cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
242b032f27cSSam Leffler 		c = &ic->ic_channels[i];
243b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c)) {
244b8e29e06SAdrian Chadd 			if (ieee80211_time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
245b032f27cSSam Leffler 				c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
246b032f27cSSam Leffler 				if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
247b032f27cSSam Leffler 					/*
248b032f27cSSam Leffler 					 * NB: do this here so we get only one
249b032f27cSSam Leffler 					 * msg instead of one for every channel
250b032f27cSSam Leffler 					 * table entry.
251b032f27cSSam Leffler 					 */
252c8f5794eSGleb Smirnoff 					ic_printf(ic, "radar on channel %u "
253c8f5794eSGleb Smirnoff 					    "(%u MHz) cleared after timeout\n",
254b032f27cSSam Leffler 					    c->ic_ieee, c->ic_freq);
255b032f27cSSam Leffler 					/* notify user space */
256b032f27cSSam Leffler 					c->ic_state &=
257b032f27cSSam Leffler 					    ~IEEE80211_CHANSTATE_NORADAR;
258b032f27cSSam Leffler 					ieee80211_notify_radar(ic, c);
259b032f27cSSam Leffler 				}
260b032f27cSSam Leffler 			} else if (dfs->nol_event[i] < oldest)
261b032f27cSSam Leffler 				oldest = dfs->nol_event[i];
262b032f27cSSam Leffler 		}
263b032f27cSSam Leffler 	}
264b032f27cSSam Leffler 	if (oldest != now) {
265b032f27cSSam Leffler 		/* arrange to process next channel up for a status change */
2668460188cSSam Leffler 		callout_schedule(&dfs->nol_timer, oldest + NOL_TIMEOUT - now);
267b032f27cSSam Leffler 	}
268b032f27cSSam Leffler }
269b032f27cSSam Leffler 
270b032f27cSSam Leffler static void
announce_radar(struct ieee80211com * ic,const struct ieee80211_channel * curchan,const struct ieee80211_channel * newchan)271c8f5794eSGleb Smirnoff announce_radar(struct ieee80211com *ic, const struct ieee80211_channel *curchan,
272b032f27cSSam Leffler 	const struct ieee80211_channel *newchan)
273b032f27cSSam Leffler {
274b032f27cSSam Leffler 	if (newchan == NULL)
275c8f5794eSGleb Smirnoff 		ic_printf(ic, "radar detected on channel %u (%u MHz)\n",
276b032f27cSSam Leffler 		    curchan->ic_ieee, curchan->ic_freq);
277b032f27cSSam Leffler 	else
278c8f5794eSGleb Smirnoff 		ic_printf(ic, "radar detected on channel %u (%u MHz), "
279b032f27cSSam Leffler 		    "moving to channel %u (%u MHz)\n",
280b032f27cSSam Leffler 		    curchan->ic_ieee, curchan->ic_freq,
281b032f27cSSam Leffler 		    newchan->ic_ieee, newchan->ic_freq);
282b032f27cSSam Leffler }
283b032f27cSSam Leffler 
284b032f27cSSam Leffler /*
285b032f27cSSam Leffler  * Handle a radar detection event on a channel. The channel is
286b032f27cSSam Leffler  * added to the NOL list and we record the time of the event.
287b032f27cSSam Leffler  * Entries are aged out after NOL_TIMEOUT.  If radar was
288b032f27cSSam Leffler  * detected while doing CAC we force a state/channel change.
289b032f27cSSam Leffler  * Otherwise radar triggers a channel switch using the CSA
290b032f27cSSam Leffler  * mechanism (when the channel is the bss channel).
291b032f27cSSam Leffler  */
292b032f27cSSam Leffler void
ieee80211_dfs_notify_radar(struct ieee80211com * ic,struct ieee80211_channel * chan)293b032f27cSSam Leffler ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
294b032f27cSSam Leffler {
295b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
296b032f27cSSam Leffler 	int i, now;
297b032f27cSSam Leffler 
298b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
299b032f27cSSam Leffler 
300b032f27cSSam Leffler 	/*
30162f8a13aSAdrian Chadd 	 * If doing DFS debugging (mode 2), don't bother
30262f8a13aSAdrian Chadd 	 * running the rest of this function.
30362f8a13aSAdrian Chadd 	 *
30462f8a13aSAdrian Chadd 	 * Simply announce the presence of the radar and continue
30562f8a13aSAdrian Chadd 	 * along merrily.
30662f8a13aSAdrian Chadd 	 */
30762f8a13aSAdrian Chadd 	if (ieee80211_dfs_debug == DFS_DBG_NOCSANOL) {
308c8f5794eSGleb Smirnoff 		announce_radar(ic, chan, chan);
30962f8a13aSAdrian Chadd 		ieee80211_notify_radar(ic, chan);
31062f8a13aSAdrian Chadd 		return;
31162f8a13aSAdrian Chadd 	}
31262f8a13aSAdrian Chadd 
31362f8a13aSAdrian Chadd 	/*
31462f8a13aSAdrian Chadd 	 * Don't mark the channel and don't put it into NOL
31562f8a13aSAdrian Chadd 	 * if we're doing DFS debugging.
31662f8a13aSAdrian Chadd 	 */
31762f8a13aSAdrian Chadd 	if (ieee80211_dfs_debug == DFS_DBG_NONE) {
31862f8a13aSAdrian Chadd 		/*
319b032f27cSSam Leffler 		 * Mark all entries with this frequency.  Notify user
320b032f27cSSam Leffler 		 * space and arrange for notification when the radar
321b032f27cSSam Leffler 		 * indication is cleared.  Then kick the NOL processing
322b032f27cSSam Leffler 		 * thread if not already running.
323b032f27cSSam Leffler 		 */
324b032f27cSSam Leffler 		now = ticks;
325b032f27cSSam Leffler 		for (i = 0; i < ic->ic_nchans; i++) {
326b032f27cSSam Leffler 			struct ieee80211_channel *c = &ic->ic_channels[i];
327b032f27cSSam Leffler 			if (c->ic_freq == chan->ic_freq) {
328b032f27cSSam Leffler 				c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
329b032f27cSSam Leffler 				c->ic_state |= IEEE80211_CHANSTATE_RADAR;
330b032f27cSSam Leffler 				dfs->nol_event[i] = now;
331b032f27cSSam Leffler 			}
332b032f27cSSam Leffler 		}
333b032f27cSSam Leffler 		ieee80211_notify_radar(ic, chan);
334b032f27cSSam Leffler 		chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
335b032f27cSSam Leffler 		if (!callout_pending(&dfs->nol_timer))
33662f8a13aSAdrian Chadd 			callout_reset(&dfs->nol_timer, NOL_TIMEOUT,
33762f8a13aSAdrian Chadd 			    dfs_timeout, ic);
33862f8a13aSAdrian Chadd 	}
339b032f27cSSam Leffler 
340b032f27cSSam Leffler 	/*
341b032f27cSSam Leffler 	 * If radar is detected on the bss channel while
342b032f27cSSam Leffler 	 * doing CAC; force a state change by scheduling the
343b032f27cSSam Leffler 	 * callout to be dispatched asap.  Otherwise, if this
344b032f27cSSam Leffler 	 * event is for the bss channel then we must quiet
345b032f27cSSam Leffler 	 * traffic and schedule a channel switch.
346b032f27cSSam Leffler 	 *
347b032f27cSSam Leffler 	 * Note this allows us to receive notification about
348b032f27cSSam Leffler 	 * channels other than the bss channel; not sure
349b032f27cSSam Leffler 	 * that can/will happen but it's simple to support.
350b032f27cSSam Leffler 	 */
351b032f27cSSam Leffler 	if (chan == ic->ic_bsschan) {
352b032f27cSSam Leffler 		/* XXX need a way to defer to user app */
35362f8a13aSAdrian Chadd 
35462f8a13aSAdrian Chadd 		/*
35562f8a13aSAdrian Chadd 		 * Don't flip over to a new channel if
35662f8a13aSAdrian Chadd 		 * we are currently doing DFS debugging.
35762f8a13aSAdrian Chadd 		 */
35862f8a13aSAdrian Chadd 		if (ieee80211_dfs_debug == DFS_DBG_NONE)
359b032f27cSSam Leffler 			dfs->newchan = ieee80211_dfs_pickchannel(ic);
36062f8a13aSAdrian Chadd 		else
36162f8a13aSAdrian Chadd 			dfs->newchan = chan;
362b032f27cSSam Leffler 
363c8f5794eSGleb Smirnoff 		announce_radar(ic, chan, dfs->newchan);
364b032f27cSSam Leffler 
365b032f27cSSam Leffler 		if (callout_pending(&dfs->cac_timer))
3665c9b0f1dSSam Leffler 			callout_schedule(&dfs->cac_timer, 0);
367b032f27cSSam Leffler 		else if (dfs->newchan != NULL) {
368b032f27cSSam Leffler 			/* XXX mode 1, switch count 2 */
369b032f27cSSam Leffler 			/* XXX calculate switch count based on max
370b032f27cSSam Leffler 			  switch time and beacon interval? */
371b032f27cSSam Leffler 			ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
372b032f27cSSam Leffler 		} else {
373b032f27cSSam Leffler 			/*
374b032f27cSSam Leffler 			 * Spec says to stop all transmissions and
375b032f27cSSam Leffler 			 * wait on the current channel for an entry
376b032f27cSSam Leffler 			 * on the NOL to expire.
377b032f27cSSam Leffler 			 */
378b032f27cSSam Leffler 			/*XXX*/
379c8f5794eSGleb Smirnoff 			ic_printf(ic, "%s: No free channels; waiting for entry "
380e1ab183cSAdrian Chadd 			    "on NOL to expire\n", __func__);
381b032f27cSSam Leffler 		}
382b032f27cSSam Leffler 	} else {
383b032f27cSSam Leffler 		/*
384b032f27cSSam Leffler 		 * Issue rate-limited console msgs.
385b032f27cSSam Leffler 		 */
386b032f27cSSam Leffler 		if (dfs->lastchan != chan) {
387b032f27cSSam Leffler 			dfs->lastchan = chan;
388b032f27cSSam Leffler 			dfs->cureps = 0;
389c8f5794eSGleb Smirnoff 			announce_radar(ic, chan, NULL);
390b032f27cSSam Leffler 		} else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
391c8f5794eSGleb Smirnoff 			announce_radar(ic, chan, NULL);
392b032f27cSSam Leffler 		}
393b032f27cSSam Leffler 	}
394b032f27cSSam Leffler }
395b032f27cSSam Leffler 
396b032f27cSSam Leffler struct ieee80211_channel *
ieee80211_dfs_pickchannel(struct ieee80211com * ic)397b032f27cSSam Leffler ieee80211_dfs_pickchannel(struct ieee80211com *ic)
398b032f27cSSam Leffler {
399b032f27cSSam Leffler 	struct ieee80211_channel *c;
400b032f27cSSam Leffler 	int i, flags;
401b032f27cSSam Leffler 	uint16_t v;
402b032f27cSSam Leffler 
403b032f27cSSam Leffler 	/*
404b032f27cSSam Leffler 	 * Consult the scan cache first.
405b032f27cSSam Leffler 	 */
406b032f27cSSam Leffler 	flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
407b032f27cSSam Leffler 	/*
408b032f27cSSam Leffler 	 * XXX if curchan is HT this will never find a channel
409b032f27cSSam Leffler 	 * XXX 'cuz we scan only legacy channels
410b032f27cSSam Leffler 	 */
411b032f27cSSam Leffler 	c = ieee80211_scan_pickchannel(ic, flags);
412b032f27cSSam Leffler 	if (c != NULL)
413b032f27cSSam Leffler 		return c;
414b032f27cSSam Leffler 	/*
415b032f27cSSam Leffler 	 * No channel found in scan cache; select a compatible
416b032f27cSSam Leffler 	 * one at random (skipping channels where radar has
417b032f27cSSam Leffler 	 * been detected).
418b032f27cSSam Leffler 	 */
419af7d9f8eSBjoern A. Zeeb 	net80211_get_random_bytes(&v, sizeof(v));
420b032f27cSSam Leffler 	v %= ic->ic_nchans;
421b032f27cSSam Leffler 	for (i = v; i < ic->ic_nchans; i++) {
422b032f27cSSam Leffler 		c = &ic->ic_channels[i];
423b032f27cSSam Leffler 		if (!IEEE80211_IS_CHAN_RADAR(c) &&
424b032f27cSSam Leffler 		   (c->ic_flags & flags) == flags)
425b032f27cSSam Leffler 			return c;
426b032f27cSSam Leffler 	}
427b032f27cSSam Leffler 	for (i = 0; i < v; i++) {
428b032f27cSSam Leffler 		c = &ic->ic_channels[i];
429b032f27cSSam Leffler 		if (!IEEE80211_IS_CHAN_RADAR(c) &&
430b032f27cSSam Leffler 		   (c->ic_flags & flags) == flags)
431b032f27cSSam Leffler 			return c;
432b032f27cSSam Leffler 	}
433c8f5794eSGleb Smirnoff 	ic_printf(ic, "HELP, no channel located to switch to!\n");
434b032f27cSSam Leffler 	return NULL;
435b032f27cSSam Leffler }
436