2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * $FreeBSD: head/sys/net80211/ieee80211_dfs.c 196785 2009-09-03 16:29:02Z sam $
30 * IEEE 802.11 DFS/Radar support.
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/endian.h>
44 #include <sys/errno.h>
46 #include <sys/sysctl.h>
49 #include <net/if_media.h>
50 #include <net/route.h>
52 #include <netproto/802_11/ieee80211_var.h>
54 MALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state");
56 static int ieee80211_nol_timeout = 30*60; /* 30 minutes */
57 SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW,
58 &ieee80211_nol_timeout, 0, "NOL timeout (secs)");
59 #define NOL_TIMEOUT msecs_to_ticks(ieee80211_nol_timeout*1000)
61 static int ieee80211_cac_timeout = 60; /* 60 seconds */
62 SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW,
63 &ieee80211_cac_timeout, 0, "CAC timeout (secs)");
64 #define CAC_TIMEOUT msecs_to_ticks(ieee80211_cac_timeout*1000)
67 ieee80211_dfs_attach(struct ieee80211com *ic)
69 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
71 callout_init(&dfs->nol_timer);
72 callout_init(&dfs->cac_timer);
76 ieee80211_dfs_detach(struct ieee80211com *ic)
78 /* NB: we assume no locking is needed */
79 ieee80211_dfs_reset(ic);
83 ieee80211_dfs_reset(struct ieee80211com *ic)
85 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
88 /* NB: we assume no locking is needed */
89 /* NB: cac_timer should be cleared by the state machine */
90 callout_stop(&dfs->nol_timer);
91 for (i = 0; i < ic->ic_nchans; i++)
92 ic->ic_channels[i].ic_state = 0;
97 cac_timeout_callout(void *arg)
99 struct ieee80211vap *vap = arg;
100 struct ieee80211com *ic;
101 struct ieee80211_dfs_state *dfs;
104 wlan_serialize_enter();
107 if (vap->iv_state != IEEE80211_S_CAC) { /* NB: just in case */
108 wlan_serialize_exit();
112 * When radar is detected during a CAC we are woken
113 * up prematurely to switch to a new channel.
114 * Check the channel to decide how to act.
116 if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
117 ieee80211_notify_cac(ic, ic->ic_curchan,
118 IEEE80211_NOTIFY_CAC_RADAR);
120 if_printf(vap->iv_ifp,
121 "CAC timer on channel %u (%u MHz) stopped due to radar\n",
122 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
124 /* XXX clobbers any existing desired channel */
125 /* NB: dfs->newchan may be NULL, that's ok */
126 vap->iv_des_chan = dfs->newchan;
127 /* XXX recursive lock need ieee80211_new_state_locked */
128 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
130 if_printf(vap->iv_ifp,
131 "CAC timer on channel %u (%u MHz) expired; "
132 "no radar detected\n",
133 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
135 * Mark all channels with the current frequency
136 * as having completed CAC; this keeps us from
137 * doing it again until we change channels.
139 for (i = 0; i < ic->ic_nchans; i++) {
140 struct ieee80211_channel *c = &ic->ic_channels[i];
141 if (c->ic_freq == ic->ic_curchan->ic_freq)
142 c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
144 ieee80211_notify_cac(ic, ic->ic_curchan,
145 IEEE80211_NOTIFY_CAC_EXPIRE);
146 ieee80211_cac_completeswitch(vap);
148 wlan_serialize_exit();
152 * Initiate the CAC timer. The driver is responsible
153 * for setting up the hardware to scan for radar on the
154 * channnel, we just handle timing things out.
157 ieee80211_dfs_cac_start(struct ieee80211vap *vap)
159 struct ieee80211com *ic = vap->iv_ic;
160 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
162 callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout_callout, vap);
163 if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
164 ticks_to_secs(CAC_TIMEOUT),
165 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
166 ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
170 * Clear the CAC timer.
173 ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
175 struct ieee80211com *ic = vap->iv_ic;
176 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
178 /* NB: racey but not important */
179 if (callout_pending(&dfs->cac_timer)) {
180 if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
181 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
182 ieee80211_notify_cac(ic, ic->ic_curchan,
183 IEEE80211_NOTIFY_CAC_STOP);
185 callout_stop(&dfs->cac_timer);
189 ieee80211_dfs_cac_clear(struct ieee80211com *ic,
190 const struct ieee80211_channel *chan)
194 for (i = 0; i < ic->ic_nchans; i++) {
195 struct ieee80211_channel *c = &ic->ic_channels[i];
196 if (c->ic_freq == chan->ic_freq)
197 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
202 dfs_timeout_callout(void *arg)
204 struct ieee80211com *ic = arg;
205 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
206 struct ieee80211_channel *c;
209 wlan_serialize_enter();
210 now = oldest = ticks;
211 for (i = 0; i < ic->ic_nchans; i++) {
212 c = &ic->ic_channels[i];
213 if (IEEE80211_IS_CHAN_RADAR(c)) {
214 if (time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
215 c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
216 if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
218 * NB: do this here so we get only one
219 * msg instead of one for every channel
222 if_printf(ic->ic_ifp, "radar on channel"
223 " %u (%u MHz) cleared after timeout\n",
224 c->ic_ieee, c->ic_freq);
225 /* notify user space */
227 ~IEEE80211_CHANSTATE_NORADAR;
228 ieee80211_notify_radar(ic, c);
230 } else if (dfs->nol_event[i] < oldest)
231 oldest = dfs->nol_event[i];
235 /* arrange to process next channel up for a status change */
236 callout_reset(&dfs->nol_timer, oldest + NOL_TIMEOUT - now,
237 dfs_timeout_callout, ic);
239 wlan_serialize_exit();
243 announce_radar(struct ifnet *ifp, const struct ieee80211_channel *curchan,
244 const struct ieee80211_channel *newchan)
247 if_printf(ifp, "radar detected on channel %u (%u MHz)\n",
248 curchan->ic_ieee, curchan->ic_freq);
250 if_printf(ifp, "radar detected on channel %u (%u MHz), "
251 "moving to channel %u (%u MHz)\n",
252 curchan->ic_ieee, curchan->ic_freq,
253 newchan->ic_ieee, newchan->ic_freq);
257 * Handle a radar detection event on a channel. The channel is
258 * added to the NOL list and we record the time of the event.
259 * Entries are aged out after NOL_TIMEOUT. If radar was
260 * detected while doing CAC we force a state/channel change.
261 * Otherwise radar triggers a channel switch using the CSA
262 * mechanism (when the channel is the bss channel).
265 ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
267 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
271 * Mark all entries with this frequency. Notify user
272 * space and arrange for notification when the radar
273 * indication is cleared. Then kick the NOL processing
274 * thread if not already running.
277 for (i = 0; i < ic->ic_nchans; i++) {
278 struct ieee80211_channel *c = &ic->ic_channels[i];
279 if (c->ic_freq == chan->ic_freq) {
280 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
281 c->ic_state |= IEEE80211_CHANSTATE_RADAR;
282 dfs->nol_event[i] = now;
285 ieee80211_notify_radar(ic, chan);
286 chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
287 if (!callout_pending(&dfs->nol_timer)) {
288 callout_reset(&dfs->nol_timer, NOL_TIMEOUT,
289 dfs_timeout_callout, ic);
293 * If radar is detected on the bss channel while
294 * doing CAC; force a state change by scheduling the
295 * callout to be dispatched asap. Otherwise, if this
296 * event is for the bss channel then we must quiet
297 * traffic and schedule a channel switch.
299 * Note this allows us to receive notification about
300 * channels other than the bss channel; not sure
301 * that can/will happen but it's simple to support.
303 if (chan == ic->ic_bsschan) {
304 /* XXX need a way to defer to user app */
305 dfs->newchan = ieee80211_dfs_pickchannel(ic);
307 announce_radar(ic->ic_ifp, chan, dfs->newchan);
310 if (callout_pending(&dfs->cac_timer)) {
311 callout_reset(&dfs->cac_timer, 0,
312 cac_timeout_callout, vap);
314 else if (dfs->newchan != NULL) {
315 /* XXX mode 1, switch count 2 */
316 /* XXX calculate switch count based on max
317 switch time and beacon interval? */
318 ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
321 * Spec says to stop all transmissions and
322 * wait on the current channel for an entry
323 * on the NOL to expire.
330 * Issue rate-limited console msgs.
332 if (dfs->lastchan != chan) {
333 dfs->lastchan = chan;
335 announce_radar(ic->ic_ifp, chan, NULL);
336 } else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
337 announce_radar(ic->ic_ifp, chan, NULL);
342 struct ieee80211_channel *
343 ieee80211_dfs_pickchannel(struct ieee80211com *ic)
345 struct ieee80211_channel *c;
350 * Consult the scan cache first.
352 flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
354 * XXX if curchan is HT this will never find a channel
355 * XXX 'cuz we scan only legacy channels
357 c = ieee80211_scan_pickchannel(ic, flags);
361 * No channel found in scan cache; select a compatible
362 * one at random (skipping channels where radar has
365 get_random_bytes(&v, sizeof(v));
367 for (i = v; i < ic->ic_nchans; i++) {
368 c = &ic->ic_channels[i];
369 if (!IEEE80211_IS_CHAN_RADAR(c) &&
370 (c->ic_flags & flags) == flags)
373 for (i = 0; i < v; i++) {
374 c = &ic->ic_channels[i];
375 if (!IEEE80211_IS_CHAN_RADAR(c) &&
376 (c->ic_flags & flags) == flags)
379 if_printf(ic->ic_ifp, "HELP, no channel located to switch to!\n");