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