xref: /freebsd/sys/net80211/ieee80211_dfs.c (revision af7d9f8e31c4c185f277b27059e470ec8a5627a7)
1b032f27cSSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 #include <sys/cdefs.h>
29b032f27cSSam Leffler #ifdef __FreeBSD__
30b032f27cSSam Leffler __FBSDID("$FreeBSD$");
31b032f27cSSam Leffler #endif
32b032f27cSSam Leffler 
33b032f27cSSam Leffler /*
34b032f27cSSam Leffler  * IEEE 802.11 DFS/Radar support.
35b032f27cSSam Leffler  */
36b032f27cSSam Leffler #include "opt_inet.h"
37b032f27cSSam Leffler #include "opt_wlan.h"
38b032f27cSSam Leffler 
39b032f27cSSam Leffler #include <sys/param.h>
40b032f27cSSam Leffler #include <sys/systm.h>
41b032f27cSSam Leffler #include <sys/mbuf.h>
42b032f27cSSam Leffler #include <sys/malloc.h>
43b032f27cSSam Leffler #include <sys/kernel.h>
44b032f27cSSam Leffler 
45b032f27cSSam Leffler #include <sys/socket.h>
46b032f27cSSam Leffler #include <sys/sockio.h>
47b032f27cSSam Leffler #include <sys/endian.h>
48b032f27cSSam Leffler #include <sys/errno.h>
49b032f27cSSam Leffler #include <sys/proc.h>
50b032f27cSSam Leffler #include <sys/sysctl.h>
51b032f27cSSam Leffler 
52b032f27cSSam Leffler #include <net/if.h>
5376039bc8SGleb Smirnoff #include <net/if_var.h>
54b032f27cSSam Leffler #include <net/if_media.h>
55c3322cb9SGleb Smirnoff #include <net/ethernet.h>
56b032f27cSSam Leffler 
57b032f27cSSam Leffler #include <net80211/ieee80211_var.h>
58b032f27cSSam Leffler 
59d745c852SEd Schouten static MALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state");
60b032f27cSSam Leffler 
61a77b10b3SSam Leffler static	int ieee80211_nol_timeout = 30*60;		/* 30 minutes */
62a77b10b3SSam Leffler SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW,
63a77b10b3SSam Leffler 	&ieee80211_nol_timeout, 0, "NOL timeout (secs)");
64b032f27cSSam Leffler #define	NOL_TIMEOUT	msecs_to_ticks(ieee80211_nol_timeout*1000)
65a77b10b3SSam Leffler 
66a77b10b3SSam Leffler static	int ieee80211_cac_timeout = 60;		/* 60 seconds */
67a77b10b3SSam Leffler SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW,
68a77b10b3SSam Leffler 	&ieee80211_cac_timeout, 0, "CAC timeout (secs)");
69b032f27cSSam Leffler #define	CAC_TIMEOUT	msecs_to_ticks(ieee80211_cac_timeout*1000)
70b032f27cSSam Leffler 
7162f8a13aSAdrian Chadd /*
7262f8a13aSAdrian Chadd  DFS* In order to facilitate  debugging, a couple of operating
7362f8a13aSAdrian Chadd  * modes aside from the default are needed.
7462f8a13aSAdrian Chadd  *
7562f8a13aSAdrian Chadd  * 0 - default CAC/NOL behaviour - ie, start CAC, place
7662f8a13aSAdrian Chadd  *     channel on NOL list.
7762f8a13aSAdrian Chadd  * 1 - send CAC, but don't change channel or add the channel
7862f8a13aSAdrian Chadd  *     to the NOL list.
7962f8a13aSAdrian Chadd  * 2 - just match on radar, don't send CAC or place channel in
8062f8a13aSAdrian Chadd  *     the NOL list.
8162f8a13aSAdrian Chadd  */
8262f8a13aSAdrian Chadd static	int ieee80211_dfs_debug = DFS_DBG_NONE;
8362f8a13aSAdrian Chadd 
8462f8a13aSAdrian Chadd /*
8562f8a13aSAdrian Chadd  * This option must not be included in the default kernel
8662f8a13aSAdrian Chadd  * as it allows users to plainly disable CAC/NOL handling.
8762f8a13aSAdrian Chadd  */
8862f8a13aSAdrian Chadd #ifdef	IEEE80211_DFS_DEBUG
8962f8a13aSAdrian Chadd SYSCTL_INT(_net_wlan, OID_AUTO, dfs_debug, CTLFLAG_RW,
9062f8a13aSAdrian Chadd 	&ieee80211_dfs_debug, 0, "DFS debug behaviour");
9162f8a13aSAdrian Chadd #endif
9262f8a13aSAdrian Chadd 
9332b0e64bSAdrian Chadd static int
9432b0e64bSAdrian Chadd null_set_quiet(struct ieee80211_node *ni, u_int8_t *quiet_elm)
9532b0e64bSAdrian Chadd {
9632b0e64bSAdrian Chadd 	return ENOSYS;
9732b0e64bSAdrian Chadd }
9832b0e64bSAdrian Chadd 
99b032f27cSSam Leffler void
100b032f27cSSam Leffler ieee80211_dfs_attach(struct ieee80211com *ic)
101b032f27cSSam Leffler {
102b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
103b032f27cSSam Leffler 
10492c4a81cSSam Leffler 	callout_init_mtx(&dfs->nol_timer, IEEE80211_LOCK_OBJ(ic), 0);
10592c4a81cSSam Leffler 	callout_init_mtx(&dfs->cac_timer, IEEE80211_LOCK_OBJ(ic), 0);
10632b0e64bSAdrian Chadd 
10732b0e64bSAdrian Chadd 	ic->ic_set_quiet = null_set_quiet;
108b032f27cSSam Leffler }
109b032f27cSSam Leffler 
110b032f27cSSam Leffler void
111b032f27cSSam Leffler ieee80211_dfs_detach(struct ieee80211com *ic)
112b032f27cSSam Leffler {
113b032f27cSSam Leffler 	/* NB: we assume no locking is needed */
114b032f27cSSam Leffler 	ieee80211_dfs_reset(ic);
115b032f27cSSam Leffler }
116b032f27cSSam Leffler 
117b032f27cSSam Leffler void
118b032f27cSSam Leffler ieee80211_dfs_reset(struct ieee80211com *ic)
119b032f27cSSam Leffler {
120b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
121b032f27cSSam Leffler 	int i;
122b032f27cSSam Leffler 
123b032f27cSSam Leffler 	/* NB: we assume no locking is needed */
124b032f27cSSam Leffler 	/* NB: cac_timer should be cleared by the state machine */
125b032f27cSSam Leffler 	callout_drain(&dfs->nol_timer);
126b032f27cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++)
127b032f27cSSam Leffler 		ic->ic_channels[i].ic_state = 0;
128b032f27cSSam Leffler 	dfs->lastchan = NULL;
129b032f27cSSam Leffler }
130b032f27cSSam Leffler 
131b032f27cSSam Leffler static void
132b032f27cSSam Leffler cac_timeout(void *arg)
133b032f27cSSam Leffler {
134b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
135b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
136b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
137b032f27cSSam Leffler 	int i;
138b032f27cSSam Leffler 
13992c4a81cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
14092c4a81cSSam Leffler 
141b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_CAC)	/* NB: just in case */
142b032f27cSSam Leffler 		return;
143b032f27cSSam Leffler 	/*
144b032f27cSSam Leffler 	 * When radar is detected during a CAC we are woken
145b032f27cSSam Leffler 	 * up prematurely to switch to a new channel.
146b032f27cSSam Leffler 	 * Check the channel to decide how to act.
147b032f27cSSam Leffler 	 */
148b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
149b032f27cSSam Leffler 		ieee80211_notify_cac(ic, ic->ic_curchan,
150b032f27cSSam Leffler 		    IEEE80211_NOTIFY_CAC_RADAR);
151b032f27cSSam Leffler 
152b032f27cSSam Leffler 		if_printf(vap->iv_ifp,
153b032f27cSSam Leffler 		    "CAC timer on channel %u (%u MHz) stopped due to radar\n",
154b032f27cSSam Leffler 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
155b032f27cSSam Leffler 
156b032f27cSSam Leffler 		/* XXX clobbers any existing desired channel */
157b032f27cSSam Leffler 		/* NB: dfs->newchan may be NULL, that's ok */
158b032f27cSSam Leffler 		vap->iv_des_chan = dfs->newchan;
1594ab4d681SAndriy Voskoboinyk 		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN, 0);
160b032f27cSSam Leffler 	} else {
161b032f27cSSam Leffler 		if_printf(vap->iv_ifp,
162b032f27cSSam Leffler 		    "CAC timer on channel %u (%u MHz) expired; "
163b032f27cSSam Leffler 		    "no radar detected\n",
164b032f27cSSam Leffler 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
165b032f27cSSam Leffler 		/*
166b032f27cSSam Leffler 		 * Mark all channels with the current frequency
167b032f27cSSam Leffler 		 * as having completed CAC; this keeps us from
168b032f27cSSam Leffler 		 * doing it again until we change channels.
169b032f27cSSam Leffler 		 */
170b032f27cSSam Leffler 		for (i = 0; i < ic->ic_nchans; i++) {
171b032f27cSSam Leffler 			struct ieee80211_channel *c = &ic->ic_channels[i];
172b032f27cSSam Leffler 			if (c->ic_freq == ic->ic_curchan->ic_freq)
173b032f27cSSam Leffler 				c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
174b032f27cSSam Leffler 		}
175b032f27cSSam Leffler 		ieee80211_notify_cac(ic, ic->ic_curchan,
176b032f27cSSam Leffler 		    IEEE80211_NOTIFY_CAC_EXPIRE);
177b032f27cSSam Leffler 		ieee80211_cac_completeswitch(vap);
178b032f27cSSam Leffler 	}
179b032f27cSSam Leffler }
180b032f27cSSam Leffler 
181b032f27cSSam Leffler /*
182b032f27cSSam Leffler  * Initiate the CAC timer.  The driver is responsible
183b032f27cSSam Leffler  * for setting up the hardware to scan for radar on the
184b032f27cSSam Leffler  * channnel, we just handle timing things out.
185b032f27cSSam Leffler  */
186b032f27cSSam Leffler void
187b032f27cSSam Leffler ieee80211_dfs_cac_start(struct ieee80211vap *vap)
188b032f27cSSam Leffler {
189b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
190b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
191b032f27cSSam Leffler 
192b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
193b032f27cSSam Leffler 
194b032f27cSSam Leffler 	callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap);
195b032f27cSSam Leffler 	if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
196b032f27cSSam Leffler 	    ticks_to_secs(CAC_TIMEOUT),
197b032f27cSSam Leffler 	    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
198b032f27cSSam Leffler 	ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
199b032f27cSSam Leffler }
200b032f27cSSam Leffler 
201b032f27cSSam Leffler /*
202b032f27cSSam Leffler  * Clear the CAC timer.
203b032f27cSSam Leffler  */
204b032f27cSSam Leffler void
205b032f27cSSam Leffler ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
206b032f27cSSam Leffler {
207b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
208b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
209b032f27cSSam Leffler 
210b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
211b032f27cSSam Leffler 
212b032f27cSSam Leffler 	/* NB: racey but not important */
213b032f27cSSam Leffler 	if (callout_pending(&dfs->cac_timer)) {
214b032f27cSSam Leffler 		if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
215b032f27cSSam Leffler 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
216b032f27cSSam Leffler 		ieee80211_notify_cac(ic, ic->ic_curchan,
217b032f27cSSam Leffler 		    IEEE80211_NOTIFY_CAC_STOP);
218b032f27cSSam Leffler 	}
219b032f27cSSam Leffler 	callout_stop(&dfs->cac_timer);
220b032f27cSSam Leffler }
221b032f27cSSam Leffler 
222b032f27cSSam Leffler void
223b032f27cSSam Leffler ieee80211_dfs_cac_clear(struct ieee80211com *ic,
224b032f27cSSam Leffler 	const struct ieee80211_channel *chan)
225b032f27cSSam Leffler {
226b032f27cSSam Leffler 	int i;
227b032f27cSSam Leffler 
228b032f27cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
229b032f27cSSam Leffler 		struct ieee80211_channel *c = &ic->ic_channels[i];
230b032f27cSSam Leffler 		if (c->ic_freq == chan->ic_freq)
231b032f27cSSam Leffler 			c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
232b032f27cSSam Leffler 	}
233b032f27cSSam Leffler }
234b032f27cSSam Leffler 
235b032f27cSSam Leffler static void
236b032f27cSSam Leffler dfs_timeout(void *arg)
237b032f27cSSam Leffler {
238b032f27cSSam Leffler 	struct ieee80211com *ic = arg;
239b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
240b032f27cSSam Leffler 	struct ieee80211_channel *c;
241b032f27cSSam Leffler 	int i, oldest, now;
242b032f27cSSam Leffler 
24392c4a81cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
24492c4a81cSSam Leffler 
245b032f27cSSam Leffler 	now = oldest = ticks;
246b032f27cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
247b032f27cSSam Leffler 		c = &ic->ic_channels[i];
248b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c)) {
249b8e29e06SAdrian Chadd 			if (ieee80211_time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
250b032f27cSSam Leffler 				c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
251b032f27cSSam Leffler 				if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
252b032f27cSSam Leffler 					/*
253b032f27cSSam Leffler 					 * NB: do this here so we get only one
254b032f27cSSam Leffler 					 * msg instead of one for every channel
255b032f27cSSam Leffler 					 * table entry.
256b032f27cSSam Leffler 					 */
257c8f5794eSGleb Smirnoff 					ic_printf(ic, "radar on channel %u "
258c8f5794eSGleb Smirnoff 					    "(%u MHz) cleared after timeout\n",
259b032f27cSSam Leffler 					    c->ic_ieee, c->ic_freq);
260b032f27cSSam Leffler 					/* notify user space */
261b032f27cSSam Leffler 					c->ic_state &=
262b032f27cSSam Leffler 					    ~IEEE80211_CHANSTATE_NORADAR;
263b032f27cSSam Leffler 					ieee80211_notify_radar(ic, c);
264b032f27cSSam Leffler 				}
265b032f27cSSam Leffler 			} else if (dfs->nol_event[i] < oldest)
266b032f27cSSam Leffler 				oldest = dfs->nol_event[i];
267b032f27cSSam Leffler 		}
268b032f27cSSam Leffler 	}
269b032f27cSSam Leffler 	if (oldest != now) {
270b032f27cSSam Leffler 		/* arrange to process next channel up for a status change */
2718460188cSSam Leffler 		callout_schedule(&dfs->nol_timer, oldest + NOL_TIMEOUT - now);
272b032f27cSSam Leffler 	}
273b032f27cSSam Leffler }
274b032f27cSSam Leffler 
275b032f27cSSam Leffler static void
276c8f5794eSGleb Smirnoff announce_radar(struct ieee80211com *ic, const struct ieee80211_channel *curchan,
277b032f27cSSam Leffler 	const struct ieee80211_channel *newchan)
278b032f27cSSam Leffler {
279b032f27cSSam Leffler 	if (newchan == NULL)
280c8f5794eSGleb Smirnoff 		ic_printf(ic, "radar detected on channel %u (%u MHz)\n",
281b032f27cSSam Leffler 		    curchan->ic_ieee, curchan->ic_freq);
282b032f27cSSam Leffler 	else
283c8f5794eSGleb Smirnoff 		ic_printf(ic, "radar detected on channel %u (%u MHz), "
284b032f27cSSam Leffler 		    "moving to channel %u (%u MHz)\n",
285b032f27cSSam Leffler 		    curchan->ic_ieee, curchan->ic_freq,
286b032f27cSSam Leffler 		    newchan->ic_ieee, newchan->ic_freq);
287b032f27cSSam Leffler }
288b032f27cSSam Leffler 
289b032f27cSSam Leffler /*
290b032f27cSSam Leffler  * Handle a radar detection event on a channel. The channel is
291b032f27cSSam Leffler  * added to the NOL list and we record the time of the event.
292b032f27cSSam Leffler  * Entries are aged out after NOL_TIMEOUT.  If radar was
293b032f27cSSam Leffler  * detected while doing CAC we force a state/channel change.
294b032f27cSSam Leffler  * Otherwise radar triggers a channel switch using the CSA
295b032f27cSSam Leffler  * mechanism (when the channel is the bss channel).
296b032f27cSSam Leffler  */
297b032f27cSSam Leffler void
298b032f27cSSam Leffler ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
299b032f27cSSam Leffler {
300b032f27cSSam Leffler 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
301b032f27cSSam Leffler 	int i, now;
302b032f27cSSam Leffler 
303b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
304b032f27cSSam Leffler 
305b032f27cSSam Leffler 	/*
30662f8a13aSAdrian Chadd 	 * If doing DFS debugging (mode 2), don't bother
30762f8a13aSAdrian Chadd 	 * running the rest of this function.
30862f8a13aSAdrian Chadd 	 *
30962f8a13aSAdrian Chadd 	 * Simply announce the presence of the radar and continue
31062f8a13aSAdrian Chadd 	 * along merrily.
31162f8a13aSAdrian Chadd 	 */
31262f8a13aSAdrian Chadd 	if (ieee80211_dfs_debug == DFS_DBG_NOCSANOL) {
313c8f5794eSGleb Smirnoff 		announce_radar(ic, chan, chan);
31462f8a13aSAdrian Chadd 		ieee80211_notify_radar(ic, chan);
31562f8a13aSAdrian Chadd 		return;
31662f8a13aSAdrian Chadd 	}
31762f8a13aSAdrian Chadd 
31862f8a13aSAdrian Chadd 	/*
31962f8a13aSAdrian Chadd 	 * Don't mark the channel and don't put it into NOL
32062f8a13aSAdrian Chadd 	 * if we're doing DFS debugging.
32162f8a13aSAdrian Chadd 	 */
32262f8a13aSAdrian Chadd 	if (ieee80211_dfs_debug == DFS_DBG_NONE) {
32362f8a13aSAdrian Chadd 		/*
324b032f27cSSam Leffler 		 * Mark all entries with this frequency.  Notify user
325b032f27cSSam Leffler 		 * space and arrange for notification when the radar
326b032f27cSSam Leffler 		 * indication is cleared.  Then kick the NOL processing
327b032f27cSSam Leffler 		 * thread if not already running.
328b032f27cSSam Leffler 		 */
329b032f27cSSam Leffler 		now = ticks;
330b032f27cSSam Leffler 		for (i = 0; i < ic->ic_nchans; i++) {
331b032f27cSSam Leffler 			struct ieee80211_channel *c = &ic->ic_channels[i];
332b032f27cSSam Leffler 			if (c->ic_freq == chan->ic_freq) {
333b032f27cSSam Leffler 				c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
334b032f27cSSam Leffler 				c->ic_state |= IEEE80211_CHANSTATE_RADAR;
335b032f27cSSam Leffler 				dfs->nol_event[i] = now;
336b032f27cSSam Leffler 			}
337b032f27cSSam Leffler 		}
338b032f27cSSam Leffler 		ieee80211_notify_radar(ic, chan);
339b032f27cSSam Leffler 		chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
340b032f27cSSam Leffler 		if (!callout_pending(&dfs->nol_timer))
34162f8a13aSAdrian Chadd 			callout_reset(&dfs->nol_timer, NOL_TIMEOUT,
34262f8a13aSAdrian Chadd 			    dfs_timeout, ic);
34362f8a13aSAdrian Chadd 	}
344b032f27cSSam Leffler 
345b032f27cSSam Leffler 	/*
346b032f27cSSam Leffler 	 * If radar is detected on the bss channel while
347b032f27cSSam Leffler 	 * doing CAC; force a state change by scheduling the
348b032f27cSSam Leffler 	 * callout to be dispatched asap.  Otherwise, if this
349b032f27cSSam Leffler 	 * event is for the bss channel then we must quiet
350b032f27cSSam Leffler 	 * traffic and schedule a channel switch.
351b032f27cSSam Leffler 	 *
352b032f27cSSam Leffler 	 * Note this allows us to receive notification about
353b032f27cSSam Leffler 	 * channels other than the bss channel; not sure
354b032f27cSSam Leffler 	 * that can/will happen but it's simple to support.
355b032f27cSSam Leffler 	 */
356b032f27cSSam Leffler 	if (chan == ic->ic_bsschan) {
357b032f27cSSam Leffler 		/* XXX need a way to defer to user app */
35862f8a13aSAdrian Chadd 
35962f8a13aSAdrian Chadd 		/*
36062f8a13aSAdrian Chadd 		 * Don't flip over to a new channel if
36162f8a13aSAdrian Chadd 		 * we are currently doing DFS debugging.
36262f8a13aSAdrian Chadd 		 */
36362f8a13aSAdrian Chadd 		if (ieee80211_dfs_debug == DFS_DBG_NONE)
364b032f27cSSam Leffler 			dfs->newchan = ieee80211_dfs_pickchannel(ic);
36562f8a13aSAdrian Chadd 		else
36662f8a13aSAdrian Chadd 			dfs->newchan = chan;
367b032f27cSSam Leffler 
368c8f5794eSGleb Smirnoff 		announce_radar(ic, chan, dfs->newchan);
369b032f27cSSam Leffler 
370b032f27cSSam Leffler 		if (callout_pending(&dfs->cac_timer))
3715c9b0f1dSSam Leffler 			callout_schedule(&dfs->cac_timer, 0);
372b032f27cSSam Leffler 		else if (dfs->newchan != NULL) {
373b032f27cSSam Leffler 			/* XXX mode 1, switch count 2 */
374b032f27cSSam Leffler 			/* XXX calculate switch count based on max
375b032f27cSSam Leffler 			  switch time and beacon interval? */
376b032f27cSSam Leffler 			ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
377b032f27cSSam Leffler 		} else {
378b032f27cSSam Leffler 			/*
379b032f27cSSam Leffler 			 * Spec says to stop all transmissions and
380b032f27cSSam Leffler 			 * wait on the current channel for an entry
381b032f27cSSam Leffler 			 * on the NOL to expire.
382b032f27cSSam Leffler 			 */
383b032f27cSSam Leffler 			/*XXX*/
384c8f5794eSGleb Smirnoff 			ic_printf(ic, "%s: No free channels; waiting for entry "
385e1ab183cSAdrian Chadd 			    "on NOL to expire\n", __func__);
386b032f27cSSam Leffler 		}
387b032f27cSSam Leffler 	} else {
388b032f27cSSam Leffler 		/*
389b032f27cSSam Leffler 		 * Issue rate-limited console msgs.
390b032f27cSSam Leffler 		 */
391b032f27cSSam Leffler 		if (dfs->lastchan != chan) {
392b032f27cSSam Leffler 			dfs->lastchan = chan;
393b032f27cSSam Leffler 			dfs->cureps = 0;
394c8f5794eSGleb Smirnoff 			announce_radar(ic, chan, NULL);
395b032f27cSSam Leffler 		} else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
396c8f5794eSGleb Smirnoff 			announce_radar(ic, chan, NULL);
397b032f27cSSam Leffler 		}
398b032f27cSSam Leffler 	}
399b032f27cSSam Leffler }
400b032f27cSSam Leffler 
401b032f27cSSam Leffler struct ieee80211_channel *
402b032f27cSSam Leffler ieee80211_dfs_pickchannel(struct ieee80211com *ic)
403b032f27cSSam Leffler {
404b032f27cSSam Leffler 	struct ieee80211_channel *c;
405b032f27cSSam Leffler 	int i, flags;
406b032f27cSSam Leffler 	uint16_t v;
407b032f27cSSam Leffler 
408b032f27cSSam Leffler 	/*
409b032f27cSSam Leffler 	 * Consult the scan cache first.
410b032f27cSSam Leffler 	 */
411b032f27cSSam Leffler 	flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
412b032f27cSSam Leffler 	/*
413b032f27cSSam Leffler 	 * XXX if curchan is HT this will never find a channel
414b032f27cSSam Leffler 	 * XXX 'cuz we scan only legacy channels
415b032f27cSSam Leffler 	 */
416b032f27cSSam Leffler 	c = ieee80211_scan_pickchannel(ic, flags);
417b032f27cSSam Leffler 	if (c != NULL)
418b032f27cSSam Leffler 		return c;
419b032f27cSSam Leffler 	/*
420b032f27cSSam Leffler 	 * No channel found in scan cache; select a compatible
421b032f27cSSam Leffler 	 * one at random (skipping channels where radar has
422b032f27cSSam Leffler 	 * been detected).
423b032f27cSSam Leffler 	 */
424*af7d9f8eSBjoern A. Zeeb 	net80211_get_random_bytes(&v, sizeof(v));
425b032f27cSSam Leffler 	v %= ic->ic_nchans;
426b032f27cSSam Leffler 	for (i = v; i < ic->ic_nchans; i++) {
427b032f27cSSam Leffler 		c = &ic->ic_channels[i];
428b032f27cSSam Leffler 		if (!IEEE80211_IS_CHAN_RADAR(c) &&
429b032f27cSSam Leffler 		   (c->ic_flags & flags) == flags)
430b032f27cSSam Leffler 			return c;
431b032f27cSSam Leffler 	}
432b032f27cSSam Leffler 	for (i = 0; i < v; i++) {
433b032f27cSSam Leffler 		c = &ic->ic_channels[i];
434b032f27cSSam Leffler 		if (!IEEE80211_IS_CHAN_RADAR(c) &&
435b032f27cSSam Leffler 		   (c->ic_flags & flags) == flags)
436b032f27cSSam Leffler 			return c;
437b032f27cSSam Leffler 	}
438c8f5794eSGleb Smirnoff 	ic_printf(ic, "HELP, no channel located to switch to!\n");
439b032f27cSSam Leffler 	return NULL;
440b032f27cSSam Leffler }
441