xref: /dflybsd-src/sys/netproto/802_11/wlan/ieee80211_dfs.c (revision eb67213abec698ffb555ee926f2761bcb7e95f55)
132176cfdSRui Paulo /*-
232176cfdSRui Paulo  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
332176cfdSRui Paulo  * All rights reserved.
432176cfdSRui Paulo  *
532176cfdSRui Paulo  * Redistribution and use in source and binary forms, with or without
632176cfdSRui Paulo  * modification, are permitted provided that the following conditions
732176cfdSRui Paulo  * are met:
832176cfdSRui Paulo  * 1. Redistributions of source code must retain the above copyright
932176cfdSRui Paulo  *    notice, this list of conditions and the following disclaimer.
1032176cfdSRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
1132176cfdSRui Paulo  *    notice, this list of conditions and the following disclaimer in the
1232176cfdSRui Paulo  *    documentation and/or other materials provided with the distribution.
1332176cfdSRui Paulo  *
1432176cfdSRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1532176cfdSRui Paulo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1632176cfdSRui Paulo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1732176cfdSRui Paulo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1832176cfdSRui Paulo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1932176cfdSRui Paulo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2032176cfdSRui Paulo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2132176cfdSRui Paulo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2232176cfdSRui Paulo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2332176cfdSRui Paulo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2432176cfdSRui Paulo  */
2532176cfdSRui Paulo 
26085ff963SMatthew Dillon #include <sys/cdefs.h>
27085ff963SMatthew Dillon #ifdef __FreeBSD__
28085ff963SMatthew Dillon __FBSDID("$FreeBSD$");
29085ff963SMatthew Dillon #endif
30085ff963SMatthew Dillon 
3132176cfdSRui Paulo /*
3232176cfdSRui Paulo  * IEEE 802.11 DFS/Radar support.
3332176cfdSRui Paulo  */
3432176cfdSRui Paulo #include "opt_inet.h"
3532176cfdSRui Paulo #include "opt_wlan.h"
3632176cfdSRui Paulo 
3732176cfdSRui Paulo #include <sys/param.h>
3832176cfdSRui Paulo #include <sys/systm.h>
3932176cfdSRui Paulo #include <sys/mbuf.h>
40085ff963SMatthew Dillon #include <sys/malloc.h>
4132176cfdSRui Paulo #include <sys/kernel.h>
4232176cfdSRui Paulo 
4332176cfdSRui Paulo #include <sys/socket.h>
4432176cfdSRui Paulo #include <sys/sockio.h>
4532176cfdSRui Paulo #include <sys/endian.h>
4632176cfdSRui Paulo #include <sys/errno.h>
4732176cfdSRui Paulo #include <sys/proc.h>
4832176cfdSRui Paulo #include <sys/sysctl.h>
4932176cfdSRui Paulo 
5032176cfdSRui Paulo #include <net/if.h>
51085ff963SMatthew Dillon #include <net/if_var.h>
5232176cfdSRui Paulo #include <net/if_media.h>
53085ff963SMatthew Dillon #include <net/ethernet.h>
5432176cfdSRui Paulo 
5532176cfdSRui Paulo #include <netproto/802_11/ieee80211_var.h>
5632176cfdSRui Paulo 
5732176cfdSRui Paulo static	int ieee80211_nol_timeout = 30*60;		/* 30 minutes */
5832176cfdSRui Paulo SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW,
5932176cfdSRui Paulo 	&ieee80211_nol_timeout, 0, "NOL timeout (secs)");
6032176cfdSRui Paulo #define	NOL_TIMEOUT	msecs_to_ticks(ieee80211_nol_timeout*1000)
6132176cfdSRui Paulo 
6232176cfdSRui Paulo static	int ieee80211_cac_timeout = 60;		/* 60 seconds */
6332176cfdSRui Paulo SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW,
6432176cfdSRui Paulo 	&ieee80211_cac_timeout, 0, "CAC timeout (secs)");
6532176cfdSRui Paulo #define	CAC_TIMEOUT	msecs_to_ticks(ieee80211_cac_timeout*1000)
6632176cfdSRui Paulo 
67085ff963SMatthew Dillon /*
68085ff963SMatthew Dillon  DFS* In order to facilitate  debugging, a couple of operating
69085ff963SMatthew Dillon  * modes aside from the default are needed.
70085ff963SMatthew Dillon  *
71085ff963SMatthew Dillon  * 0 - default CAC/NOL behaviour - ie, start CAC, place
72085ff963SMatthew Dillon  *     channel on NOL list.
73085ff963SMatthew Dillon  * 1 - send CAC, but don't change channel or add the channel
74085ff963SMatthew Dillon  *     to the NOL list.
75085ff963SMatthew Dillon  * 2 - just match on radar, don't send CAC or place channel in
76085ff963SMatthew Dillon  *     the NOL list.
77085ff963SMatthew Dillon  */
78085ff963SMatthew Dillon static	int ieee80211_dfs_debug = DFS_DBG_NONE;
79085ff963SMatthew Dillon 
80085ff963SMatthew Dillon /*
81085ff963SMatthew Dillon  * This option must not be included in the default kernel
82085ff963SMatthew Dillon  * as it allows users to plainly disable CAC/NOL handling.
83085ff963SMatthew Dillon  */
84085ff963SMatthew Dillon #ifdef	IEEE80211_DFS_DEBUG
85085ff963SMatthew Dillon SYSCTL_INT(_net_wlan, OID_AUTO, dfs_debug, CTLFLAG_RW,
86085ff963SMatthew Dillon 	&ieee80211_dfs_debug, 0, "DFS debug behaviour");
87085ff963SMatthew Dillon #endif
88085ff963SMatthew Dillon 
89085ff963SMatthew Dillon static int
null_set_quiet(struct ieee80211_node * ni,u_int8_t * quiet_elm)90085ff963SMatthew Dillon null_set_quiet(struct ieee80211_node *ni, u_int8_t *quiet_elm)
91085ff963SMatthew Dillon {
92085ff963SMatthew Dillon 	return ENOSYS;
93085ff963SMatthew Dillon }
94085ff963SMatthew Dillon 
9532176cfdSRui Paulo void
ieee80211_dfs_attach(struct ieee80211com * ic)9632176cfdSRui Paulo ieee80211_dfs_attach(struct ieee80211com *ic)
9732176cfdSRui Paulo {
9832176cfdSRui Paulo 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
9932176cfdSRui Paulo 
100085ff963SMatthew Dillon 	callout_init_mtx(&dfs->nol_timer, IEEE80211_LOCK_OBJ(ic), 0);
101085ff963SMatthew Dillon 	callout_init_mtx(&dfs->cac_timer, IEEE80211_LOCK_OBJ(ic), 0);
102085ff963SMatthew Dillon 
103085ff963SMatthew Dillon 	ic->ic_set_quiet = null_set_quiet;
10432176cfdSRui Paulo }
10532176cfdSRui Paulo 
10632176cfdSRui Paulo void
ieee80211_dfs_detach(struct ieee80211com * ic)10732176cfdSRui Paulo ieee80211_dfs_detach(struct ieee80211com *ic)
10832176cfdSRui Paulo {
10932176cfdSRui Paulo 	/* NB: we assume no locking is needed */
11032176cfdSRui Paulo 	ieee80211_dfs_reset(ic);
11132176cfdSRui Paulo }
11232176cfdSRui Paulo 
11332176cfdSRui Paulo void
ieee80211_dfs_reset(struct ieee80211com * ic)11432176cfdSRui Paulo ieee80211_dfs_reset(struct ieee80211com *ic)
11532176cfdSRui Paulo {
11632176cfdSRui Paulo 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
11732176cfdSRui Paulo 	int i;
11832176cfdSRui Paulo 
11932176cfdSRui Paulo 	/* NB: we assume no locking is needed */
12032176cfdSRui Paulo 	/* NB: cac_timer should be cleared by the state machine */
121085ff963SMatthew Dillon 	callout_drain(&dfs->nol_timer);
12232176cfdSRui Paulo 	for (i = 0; i < ic->ic_nchans; i++)
12332176cfdSRui Paulo 		ic->ic_channels[i].ic_state = 0;
12432176cfdSRui Paulo 	dfs->lastchan = NULL;
12532176cfdSRui Paulo }
12632176cfdSRui Paulo 
12732176cfdSRui Paulo static void
cac_timeout(void * arg)128085ff963SMatthew Dillon cac_timeout(void *arg)
12932176cfdSRui Paulo {
13032176cfdSRui Paulo 	struct ieee80211vap *vap = arg;
131085ff963SMatthew Dillon 	struct ieee80211com *ic = vap->iv_ic;
132085ff963SMatthew Dillon 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
13332176cfdSRui Paulo 	int i;
13432176cfdSRui Paulo 
135085ff963SMatthew Dillon 	IEEE80211_LOCK_ASSERT(ic);
136085ff963SMatthew Dillon 
137085ff963SMatthew Dillon 	if (vap->iv_state != IEEE80211_S_CAC)	/* NB: just in case */
13832176cfdSRui Paulo 		return;
13932176cfdSRui Paulo 	/*
14032176cfdSRui Paulo 	 * When radar is detected during a CAC we are woken
14132176cfdSRui Paulo 	 * up prematurely to switch to a new channel.
14232176cfdSRui Paulo 	 * Check the channel to decide how to act.
14332176cfdSRui Paulo 	 */
14432176cfdSRui Paulo 	if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
14532176cfdSRui Paulo 		ieee80211_notify_cac(ic, ic->ic_curchan,
14632176cfdSRui Paulo 		    IEEE80211_NOTIFY_CAC_RADAR);
14732176cfdSRui Paulo 
14832176cfdSRui Paulo 		if_printf(vap->iv_ifp,
14932176cfdSRui Paulo 		    "CAC timer on channel %u (%u MHz) stopped due to radar\n",
15032176cfdSRui Paulo 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
15132176cfdSRui Paulo 
15232176cfdSRui Paulo 		/* XXX clobbers any existing desired channel */
15332176cfdSRui Paulo 		/* NB: dfs->newchan may be NULL, that's ok */
15432176cfdSRui Paulo 		vap->iv_des_chan = dfs->newchan;
15532176cfdSRui Paulo 		/* XXX recursive lock need ieee80211_new_state_locked */
15632176cfdSRui Paulo 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
15732176cfdSRui Paulo 	} else {
15832176cfdSRui Paulo 		if_printf(vap->iv_ifp,
15932176cfdSRui Paulo 		    "CAC timer on channel %u (%u MHz) expired; "
16032176cfdSRui Paulo 		    "no radar detected\n",
16132176cfdSRui Paulo 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
16232176cfdSRui Paulo 		/*
16332176cfdSRui Paulo 		 * Mark all channels with the current frequency
16432176cfdSRui Paulo 		 * as having completed CAC; this keeps us from
16532176cfdSRui Paulo 		 * doing it again until we change channels.
16632176cfdSRui Paulo 		 */
16732176cfdSRui Paulo 		for (i = 0; i < ic->ic_nchans; i++) {
16832176cfdSRui Paulo 			struct ieee80211_channel *c = &ic->ic_channels[i];
16932176cfdSRui Paulo 			if (c->ic_freq == ic->ic_curchan->ic_freq)
17032176cfdSRui Paulo 				c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
17132176cfdSRui Paulo 		}
17232176cfdSRui Paulo 		ieee80211_notify_cac(ic, ic->ic_curchan,
17332176cfdSRui Paulo 		    IEEE80211_NOTIFY_CAC_EXPIRE);
17432176cfdSRui Paulo 		ieee80211_cac_completeswitch(vap);
17532176cfdSRui Paulo 	}
17632176cfdSRui Paulo }
17732176cfdSRui Paulo 
17832176cfdSRui Paulo /*
17932176cfdSRui Paulo  * Initiate the CAC timer.  The driver is responsible
18032176cfdSRui Paulo  * for setting up the hardware to scan for radar on the
18132176cfdSRui Paulo  * channnel, we just handle timing things out.
18232176cfdSRui Paulo  */
18332176cfdSRui Paulo void
ieee80211_dfs_cac_start(struct ieee80211vap * vap)18432176cfdSRui Paulo ieee80211_dfs_cac_start(struct ieee80211vap *vap)
18532176cfdSRui Paulo {
18632176cfdSRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
18732176cfdSRui Paulo 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
18832176cfdSRui Paulo 
189085ff963SMatthew Dillon 	IEEE80211_LOCK_ASSERT(ic);
190085ff963SMatthew Dillon 
191085ff963SMatthew Dillon 	callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap);
19232176cfdSRui Paulo 	if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
19332176cfdSRui Paulo 	    ticks_to_secs(CAC_TIMEOUT),
19432176cfdSRui Paulo 	    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
19532176cfdSRui Paulo 	ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
19632176cfdSRui Paulo }
19732176cfdSRui Paulo 
19832176cfdSRui Paulo /*
19932176cfdSRui Paulo  * Clear the CAC timer.
20032176cfdSRui Paulo  */
20132176cfdSRui Paulo void
ieee80211_dfs_cac_stop(struct ieee80211vap * vap)20232176cfdSRui Paulo ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
20332176cfdSRui Paulo {
20432176cfdSRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
20532176cfdSRui Paulo 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
20632176cfdSRui Paulo 
207085ff963SMatthew Dillon 	IEEE80211_LOCK_ASSERT(ic);
208085ff963SMatthew Dillon 
20932176cfdSRui Paulo 	/* NB: racey but not important */
21032176cfdSRui Paulo 	if (callout_pending(&dfs->cac_timer)) {
21132176cfdSRui Paulo 		if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
21232176cfdSRui Paulo 		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
21332176cfdSRui Paulo 		ieee80211_notify_cac(ic, ic->ic_curchan,
21432176cfdSRui Paulo 		    IEEE80211_NOTIFY_CAC_STOP);
21532176cfdSRui Paulo 	}
21632176cfdSRui Paulo 	callout_stop(&dfs->cac_timer);
21732176cfdSRui Paulo }
21832176cfdSRui Paulo 
21932176cfdSRui Paulo void
ieee80211_dfs_cac_clear(struct ieee80211com * ic,const struct ieee80211_channel * chan)22032176cfdSRui Paulo ieee80211_dfs_cac_clear(struct ieee80211com *ic,
22132176cfdSRui Paulo 	const struct ieee80211_channel *chan)
22232176cfdSRui Paulo {
22332176cfdSRui Paulo 	int i;
22432176cfdSRui Paulo 
22532176cfdSRui Paulo 	for (i = 0; i < ic->ic_nchans; i++) {
22632176cfdSRui Paulo 		struct ieee80211_channel *c = &ic->ic_channels[i];
22732176cfdSRui Paulo 		if (c->ic_freq == chan->ic_freq)
22832176cfdSRui Paulo 			c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
22932176cfdSRui Paulo 	}
23032176cfdSRui Paulo }
23132176cfdSRui Paulo 
23232176cfdSRui Paulo static void
dfs_timeout(void * arg)233085ff963SMatthew Dillon dfs_timeout(void *arg)
23432176cfdSRui Paulo {
23532176cfdSRui Paulo 	struct ieee80211com *ic = arg;
23632176cfdSRui Paulo 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
23732176cfdSRui Paulo 	struct ieee80211_channel *c;
23832176cfdSRui Paulo 	int i, oldest, now;
23932176cfdSRui Paulo 
240085ff963SMatthew Dillon 	IEEE80211_LOCK_ASSERT(ic);
241085ff963SMatthew Dillon 
24232176cfdSRui Paulo 	now = oldest = ticks;
24332176cfdSRui Paulo 	for (i = 0; i < ic->ic_nchans; i++) {
24432176cfdSRui Paulo 		c = &ic->ic_channels[i];
24532176cfdSRui Paulo 		if (IEEE80211_IS_CHAN_RADAR(c)) {
2464f655ef5SMatthew Dillon 			if (ieee80211_time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
24732176cfdSRui Paulo 				c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
24832176cfdSRui Paulo 				if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
24932176cfdSRui Paulo 					/*
25032176cfdSRui Paulo 					 * NB: do this here so we get only one
25132176cfdSRui Paulo 					 * msg instead of one for every channel
25232176cfdSRui Paulo 					 * table entry.
25332176cfdSRui Paulo 					 */
2544f898719SImre Vadász 					ic_printf(ic, "radar on channel %u "
2554f898719SImre Vadász 					    "(%u MHz) cleared after timeout\n",
25632176cfdSRui Paulo 					    c->ic_ieee, c->ic_freq);
25732176cfdSRui Paulo 					/* notify user space */
25832176cfdSRui Paulo 					c->ic_state &=
25932176cfdSRui Paulo 					    ~IEEE80211_CHANSTATE_NORADAR;
26032176cfdSRui Paulo 					ieee80211_notify_radar(ic, c);
26132176cfdSRui Paulo 				}
26232176cfdSRui Paulo 			} else if (dfs->nol_event[i] < oldest)
26332176cfdSRui Paulo 				oldest = dfs->nol_event[i];
26432176cfdSRui Paulo 		}
26532176cfdSRui Paulo 	}
26632176cfdSRui Paulo 	if (oldest != now) {
26732176cfdSRui Paulo 		/* arrange to process next channel up for a status change */
2684f655ef5SMatthew Dillon #if defined(__DragonFly__)
269085ff963SMatthew Dillon 		callout_schedule_dfly(&dfs->nol_timer, oldest + NOL_TIMEOUT - now,
270085ff963SMatthew Dillon 				dfs_timeout, ic);
2714f655ef5SMatthew Dillon #else
2724f655ef5SMatthew Dillon 		callout_schedule(&dfs->nol_timer, oldest + NOL_TIMEOUT - now);
2734f655ef5SMatthew Dillon #endif
27432176cfdSRui Paulo 	}
27532176cfdSRui Paulo }
27632176cfdSRui Paulo 
27732176cfdSRui Paulo static void
announce_radar(struct ieee80211com * ic,const struct ieee80211_channel * curchan,const struct ieee80211_channel * newchan)2784f898719SImre Vadász announce_radar(struct ieee80211com *ic, const struct ieee80211_channel *curchan,
27932176cfdSRui Paulo 	const struct ieee80211_channel *newchan)
28032176cfdSRui Paulo {
28132176cfdSRui Paulo 	if (newchan == NULL)
2824f898719SImre Vadász 		ic_printf(ic, "radar detected on channel %u (%u MHz)\n",
28332176cfdSRui Paulo 		    curchan->ic_ieee, curchan->ic_freq);
28432176cfdSRui Paulo 	else
2854f898719SImre Vadász 		ic_printf(ic, "radar detected on channel %u (%u MHz), "
28632176cfdSRui Paulo 		    "moving to channel %u (%u MHz)\n",
28732176cfdSRui Paulo 		    curchan->ic_ieee, curchan->ic_freq,
28832176cfdSRui Paulo 		    newchan->ic_ieee, newchan->ic_freq);
28932176cfdSRui Paulo }
29032176cfdSRui Paulo 
29132176cfdSRui Paulo /*
29232176cfdSRui Paulo  * Handle a radar detection event on a channel. The channel is
29332176cfdSRui Paulo  * added to the NOL list and we record the time of the event.
29432176cfdSRui Paulo  * Entries are aged out after NOL_TIMEOUT.  If radar was
29532176cfdSRui Paulo  * detected while doing CAC we force a state/channel change.
29632176cfdSRui Paulo  * Otherwise radar triggers a channel switch using the CSA
29732176cfdSRui Paulo  * mechanism (when the channel is the bss channel).
29832176cfdSRui Paulo  */
29932176cfdSRui Paulo void
ieee80211_dfs_notify_radar(struct ieee80211com * ic,struct ieee80211_channel * chan)30032176cfdSRui Paulo ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
30132176cfdSRui Paulo {
30232176cfdSRui Paulo 	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
30332176cfdSRui Paulo 	int i, now;
30432176cfdSRui Paulo 
305085ff963SMatthew Dillon 	IEEE80211_LOCK_ASSERT(ic);
306085ff963SMatthew Dillon 
307085ff963SMatthew Dillon 	/*
308085ff963SMatthew Dillon 	 * If doing DFS debugging (mode 2), don't bother
309085ff963SMatthew Dillon 	 * running the rest of this function.
310085ff963SMatthew Dillon 	 *
311085ff963SMatthew Dillon 	 * Simply announce the presence of the radar and continue
312085ff963SMatthew Dillon 	 * along merrily.
313085ff963SMatthew Dillon 	 */
314085ff963SMatthew Dillon 	if (ieee80211_dfs_debug == DFS_DBG_NOCSANOL) {
3154f898719SImre Vadász 		announce_radar(ic, chan, chan);
316085ff963SMatthew Dillon 		ieee80211_notify_radar(ic, chan);
317085ff963SMatthew Dillon 		return;
318085ff963SMatthew Dillon 	}
319085ff963SMatthew Dillon 
320085ff963SMatthew Dillon 	/*
321085ff963SMatthew Dillon 	 * Don't mark the channel and don't put it into NOL
322085ff963SMatthew Dillon 	 * if we're doing DFS debugging.
323085ff963SMatthew Dillon 	 */
324085ff963SMatthew Dillon 	if (ieee80211_dfs_debug == DFS_DBG_NONE) {
32532176cfdSRui Paulo 		/*
32632176cfdSRui Paulo 		 * Mark all entries with this frequency.  Notify user
32732176cfdSRui Paulo 		 * space and arrange for notification when the radar
32832176cfdSRui Paulo 		 * indication is cleared.  Then kick the NOL processing
32932176cfdSRui Paulo 		 * thread if not already running.
33032176cfdSRui Paulo 		 */
33132176cfdSRui Paulo 		now = ticks;
33232176cfdSRui Paulo 		for (i = 0; i < ic->ic_nchans; i++) {
33332176cfdSRui Paulo 			struct ieee80211_channel *c = &ic->ic_channels[i];
33432176cfdSRui Paulo 			if (c->ic_freq == chan->ic_freq) {
33532176cfdSRui Paulo 				c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
33632176cfdSRui Paulo 				c->ic_state |= IEEE80211_CHANSTATE_RADAR;
33732176cfdSRui Paulo 				dfs->nol_event[i] = now;
33832176cfdSRui Paulo 			}
33932176cfdSRui Paulo 		}
34032176cfdSRui Paulo 		ieee80211_notify_radar(ic, chan);
34132176cfdSRui Paulo 		chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
342085ff963SMatthew Dillon 		if (!callout_pending(&dfs->nol_timer))
34347156d48SMatthew Dillon 			callout_reset(&dfs->nol_timer, NOL_TIMEOUT,
344085ff963SMatthew Dillon 			    dfs_timeout, ic);
34547156d48SMatthew Dillon 	}
34632176cfdSRui Paulo 
34732176cfdSRui Paulo 	/*
34832176cfdSRui Paulo 	 * If radar is detected on the bss channel while
34932176cfdSRui Paulo 	 * doing CAC; force a state change by scheduling the
35032176cfdSRui Paulo 	 * callout to be dispatched asap.  Otherwise, if this
35132176cfdSRui Paulo 	 * event is for the bss channel then we must quiet
35232176cfdSRui Paulo 	 * traffic and schedule a channel switch.
35332176cfdSRui Paulo 	 *
35432176cfdSRui Paulo 	 * Note this allows us to receive notification about
35532176cfdSRui Paulo 	 * channels other than the bss channel; not sure
35632176cfdSRui Paulo 	 * that can/will happen but it's simple to support.
35732176cfdSRui Paulo 	 */
35832176cfdSRui Paulo 	if (chan == ic->ic_bsschan) {
35932176cfdSRui Paulo 		/* XXX need a way to defer to user app */
360085ff963SMatthew Dillon 
361085ff963SMatthew Dillon 		/*
362085ff963SMatthew Dillon 		 * Don't flip over to a new channel if
363085ff963SMatthew Dillon 		 * we are currently doing DFS debugging.
364085ff963SMatthew Dillon 		 */
365085ff963SMatthew Dillon 		if (ieee80211_dfs_debug == DFS_DBG_NONE)
36632176cfdSRui Paulo 			dfs->newchan = ieee80211_dfs_pickchannel(ic);
367085ff963SMatthew Dillon 		else
368085ff963SMatthew Dillon 			dfs->newchan = chan;
36932176cfdSRui Paulo 
3704f898719SImre Vadász 		announce_radar(ic, chan, dfs->newchan);
37132176cfdSRui Paulo 
372085ff963SMatthew Dillon 		if (callout_pending(&dfs->cac_timer))
3734f655ef5SMatthew Dillon #if defined(__DragonFly__)
374*eb67213aSMatthew Dillon 			callout_schedule_dfly(&dfs->cac_timer, 0,
375*eb67213aSMatthew Dillon 					      cac_timeout,
376*eb67213aSMatthew Dillon 					      callout_arg(&dfs->cac_timer));
3774f655ef5SMatthew Dillon #else
3784f655ef5SMatthew Dillon 			callout_schedule(&dfs->cac_timer, 0);
3794f655ef5SMatthew Dillon #endif
38032176cfdSRui Paulo 		else if (dfs->newchan != NULL) {
38132176cfdSRui Paulo 			/* XXX mode 1, switch count 2 */
38232176cfdSRui Paulo 			/* XXX calculate switch count based on max
38332176cfdSRui Paulo 			  switch time and beacon interval? */
38432176cfdSRui Paulo 			ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
38532176cfdSRui Paulo 		} else {
38632176cfdSRui Paulo 			/*
38732176cfdSRui Paulo 			 * Spec says to stop all transmissions and
38832176cfdSRui Paulo 			 * wait on the current channel for an entry
38932176cfdSRui Paulo 			 * on the NOL to expire.
39032176cfdSRui Paulo 			 */
39132176cfdSRui Paulo 			/*XXX*/
3924f898719SImre Vadász 			ic_printf(ic, "%s: No free channels; waiting for entry "
393085ff963SMatthew Dillon 			    "on NOL to expire\n", __func__);
39432176cfdSRui Paulo 		}
39532176cfdSRui Paulo 	} else {
39632176cfdSRui Paulo 		/*
39732176cfdSRui Paulo 		 * Issue rate-limited console msgs.
39832176cfdSRui Paulo 		 */
39932176cfdSRui Paulo 		if (dfs->lastchan != chan) {
40032176cfdSRui Paulo 			dfs->lastchan = chan;
40132176cfdSRui Paulo 			dfs->cureps = 0;
4024f898719SImre Vadász 			announce_radar(ic, chan, NULL);
40332176cfdSRui Paulo 		} else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
4044f898719SImre Vadász 			announce_radar(ic, chan, NULL);
40532176cfdSRui Paulo 		}
40632176cfdSRui Paulo 	}
40732176cfdSRui Paulo }
40832176cfdSRui Paulo 
40932176cfdSRui Paulo struct ieee80211_channel *
ieee80211_dfs_pickchannel(struct ieee80211com * ic)41032176cfdSRui Paulo ieee80211_dfs_pickchannel(struct ieee80211com *ic)
41132176cfdSRui Paulo {
41232176cfdSRui Paulo 	struct ieee80211_channel *c;
41332176cfdSRui Paulo 	int i, flags;
41432176cfdSRui Paulo 	uint16_t v;
41532176cfdSRui Paulo 
41632176cfdSRui Paulo 	/*
41732176cfdSRui Paulo 	 * Consult the scan cache first.
41832176cfdSRui Paulo 	 */
41932176cfdSRui Paulo 	flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
42032176cfdSRui Paulo 	/*
42132176cfdSRui Paulo 	 * XXX if curchan is HT this will never find a channel
42232176cfdSRui Paulo 	 * XXX 'cuz we scan only legacy channels
42332176cfdSRui Paulo 	 */
42432176cfdSRui Paulo 	c = ieee80211_scan_pickchannel(ic, flags);
42532176cfdSRui Paulo 	if (c != NULL)
42632176cfdSRui Paulo 		return c;
42732176cfdSRui Paulo 	/*
42832176cfdSRui Paulo 	 * No channel found in scan cache; select a compatible
42932176cfdSRui Paulo 	 * one at random (skipping channels where radar has
43032176cfdSRui Paulo 	 * been detected).
43132176cfdSRui Paulo 	 */
43232176cfdSRui Paulo 	get_random_bytes(&v, sizeof(v));
43332176cfdSRui Paulo 	v %= ic->ic_nchans;
43432176cfdSRui Paulo 	for (i = v; i < ic->ic_nchans; i++) {
43532176cfdSRui Paulo 		c = &ic->ic_channels[i];
43632176cfdSRui Paulo 		if (!IEEE80211_IS_CHAN_RADAR(c) &&
43732176cfdSRui Paulo 		   (c->ic_flags & flags) == flags)
43832176cfdSRui Paulo 			return c;
43932176cfdSRui Paulo 	}
44032176cfdSRui Paulo 	for (i = 0; i < v; i++) {
44132176cfdSRui Paulo 		c = &ic->ic_channels[i];
44232176cfdSRui Paulo 		if (!IEEE80211_IS_CHAN_RADAR(c) &&
44332176cfdSRui Paulo 		   (c->ic_flags & flags) == flags)
44432176cfdSRui Paulo 			return c;
44532176cfdSRui Paulo 	}
4464f898719SImre Vadász 	ic_printf(ic, "HELP, no channel located to switch to!\n");
44732176cfdSRui Paulo 	return NULL;
44832176cfdSRui Paulo }
449