ieee80211_dfs.c revision 193666
1/*-
2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26#include <sys/cdefs.h>
27#ifdef __FreeBSD__
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_dfs.c 193666 2009-06-07 23:16:59Z sam $");
29#endif
30
31/*
32 * IEEE 802.11 DFS/Radar support.
33 */
34#include "opt_inet.h"
35#include "opt_wlan.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/mbuf.h>
40#include <sys/malloc.h>
41#include <sys/kernel.h>
42
43#include <sys/socket.h>
44#include <sys/sockio.h>
45#include <sys/endian.h>
46#include <sys/errno.h>
47#include <sys/proc.h>
48#include <sys/sysctl.h>
49
50#include <net/if.h>
51#include <net/if_media.h>
52
53#include <net80211/ieee80211_var.h>
54
55MALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state");
56
57static	int ieee80211_nol_timeout = 30*60;		/* 30 minutes */
58SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW,
59	&ieee80211_nol_timeout, 0, "NOL timeout (secs)");
60#define	NOL_TIMEOUT	msecs_to_ticks(ieee80211_nol_timeout*1000)
61
62static	int ieee80211_cac_timeout = 60;		/* 60 seconds */
63SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW,
64	&ieee80211_cac_timeout, 0, "CAC timeout (secs)");
65#define	CAC_TIMEOUT	msecs_to_ticks(ieee80211_cac_timeout*1000)
66
67void
68ieee80211_dfs_attach(struct ieee80211com *ic)
69{
70	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
71
72	callout_init(&dfs->nol_timer, CALLOUT_MPSAFE);
73	callout_init(&dfs->cac_timer, CALLOUT_MPSAFE);
74}
75
76void
77ieee80211_dfs_detach(struct ieee80211com *ic)
78{
79	/* NB: we assume no locking is needed */
80	ieee80211_dfs_reset(ic);
81}
82
83void
84ieee80211_dfs_reset(struct ieee80211com *ic)
85{
86	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
87	int i;
88
89	/* NB: we assume no locking is needed */
90	/* NB: cac_timer should be cleared by the state machine */
91	callout_drain(&dfs->nol_timer);
92	for (i = 0; i < ic->ic_nchans; i++)
93		ic->ic_channels[i].ic_state = 0;
94	dfs->lastchan = NULL;
95}
96
97static void
98cac_timeout(void *arg)
99{
100	struct ieee80211vap *vap = arg;
101	struct ieee80211com *ic = vap->iv_ic;
102	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
103	int i;
104
105	if (vap->iv_state != IEEE80211_S_CAC)	/* NB: just in case */
106		return;
107	/*
108	 * When radar is detected during a CAC we are woken
109	 * up prematurely to switch to a new channel.
110	 * Check the channel to decide how to act.
111	 */
112	if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
113		ieee80211_notify_cac(ic, ic->ic_curchan,
114		    IEEE80211_NOTIFY_CAC_RADAR);
115
116		if_printf(vap->iv_ifp,
117		    "CAC timer on channel %u (%u MHz) stopped due to radar\n",
118		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
119
120		/* XXX clobbers any existing desired channel */
121		/* NB: dfs->newchan may be NULL, that's ok */
122		vap->iv_des_chan = dfs->newchan;
123		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
124	} else {
125		if_printf(vap->iv_ifp,
126		    "CAC timer on channel %u (%u MHz) expired; "
127		    "no radar detected\n",
128		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
129		/*
130		 * Mark all channels with the current frequency
131		 * as having completed CAC; this keeps us from
132		 * doing it again until we change channels.
133		 */
134		for (i = 0; i < ic->ic_nchans; i++) {
135			struct ieee80211_channel *c = &ic->ic_channels[i];
136			if (c->ic_freq == ic->ic_curchan->ic_freq)
137				c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
138		}
139		ieee80211_notify_cac(ic, ic->ic_curchan,
140		    IEEE80211_NOTIFY_CAC_EXPIRE);
141		ieee80211_cac_completeswitch(vap);
142	}
143}
144
145/*
146 * Initiate the CAC timer.  The driver is responsible
147 * for setting up the hardware to scan for radar on the
148 * channnel, we just handle timing things out.
149 */
150void
151ieee80211_dfs_cac_start(struct ieee80211vap *vap)
152{
153	struct ieee80211com *ic = vap->iv_ic;
154	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
155
156	IEEE80211_LOCK_ASSERT(ic);
157
158	callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap);
159	if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
160	    ticks_to_secs(CAC_TIMEOUT),
161	    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
162	ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
163}
164
165/*
166 * Clear the CAC timer.
167 */
168void
169ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
170{
171	struct ieee80211com *ic = vap->iv_ic;
172	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
173
174	IEEE80211_LOCK_ASSERT(ic);
175
176	/* NB: racey but not important */
177	if (callout_pending(&dfs->cac_timer)) {
178		if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
179		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
180		ieee80211_notify_cac(ic, ic->ic_curchan,
181		    IEEE80211_NOTIFY_CAC_STOP);
182	}
183	/* XXX cannot use drain 'cuz holding a lock */
184	callout_stop(&dfs->cac_timer);
185}
186
187void
188ieee80211_dfs_cac_clear(struct ieee80211com *ic,
189	const struct ieee80211_channel *chan)
190{
191	int i;
192
193	for (i = 0; i < ic->ic_nchans; i++) {
194		struct ieee80211_channel *c = &ic->ic_channels[i];
195		if (c->ic_freq == chan->ic_freq)
196			c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
197	}
198}
199
200static void
201dfs_timeout(void *arg)
202{
203	struct ieee80211com *ic = arg;
204	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
205	struct ieee80211_channel *c;
206	int i, oldest, now;
207
208	IEEE80211_LOCK(ic);
209	now = oldest = ticks;
210	for (i = 0; i < ic->ic_nchans; i++) {
211		c = &ic->ic_channels[i];
212		if (IEEE80211_IS_CHAN_RADAR(c)) {
213			if (time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
214				c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
215				if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
216					/*
217					 * NB: do this here so we get only one
218					 * msg instead of one for every channel
219					 * table entry.
220					 */
221					if_printf(ic->ic_ifp, "radar on channel"
222					    " %u (%u MHz) cleared after timeout\n",
223					    c->ic_ieee, c->ic_freq);
224					/* notify user space */
225					c->ic_state &=
226					    ~IEEE80211_CHANSTATE_NORADAR;
227					ieee80211_notify_radar(ic, c);
228				}
229			} else if (dfs->nol_event[i] < oldest)
230				oldest = dfs->nol_event[i];
231		}
232	}
233	if (oldest != now) {
234		/* arrange to process next channel up for a status change */
235		callout_schedule(&dfs->nol_timer, oldest + NOL_TIMEOUT);
236	}
237	IEEE80211_UNLOCK(ic);
238}
239
240static void
241announce_radar(struct ifnet *ifp, const struct ieee80211_channel *curchan,
242	const struct ieee80211_channel *newchan)
243{
244	if (newchan == NULL)
245		if_printf(ifp, "radar detected on channel %u (%u MHz)\n",
246		    curchan->ic_ieee, curchan->ic_freq);
247	else
248		if_printf(ifp, "radar detected on channel %u (%u MHz), "
249		    "moving to channel %u (%u MHz)\n",
250		    curchan->ic_ieee, curchan->ic_freq,
251		    newchan->ic_ieee, newchan->ic_freq);
252}
253
254/*
255 * Handle a radar detection event on a channel. The channel is
256 * added to the NOL list and we record the time of the event.
257 * Entries are aged out after NOL_TIMEOUT.  If radar was
258 * detected while doing CAC we force a state/channel change.
259 * Otherwise radar triggers a channel switch using the CSA
260 * mechanism (when the channel is the bss channel).
261 */
262void
263ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
264{
265	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
266	int i, now;
267
268	IEEE80211_LOCK_ASSERT(ic);
269
270	/*
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.
275	 */
276	now = ticks;
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;
283		}
284	}
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, dfs_timeout, ic);
289
290	/*
291	 * If radar is detected on the bss channel while
292	 * doing CAC; force a state change by scheduling the
293	 * callout to be dispatched asap.  Otherwise, if this
294	 * event is for the bss channel then we must quiet
295	 * traffic and schedule a channel switch.
296	 *
297	 * Note this allows us to receive notification about
298	 * channels other than the bss channel; not sure
299	 * that can/will happen but it's simple to support.
300	 */
301	if (chan == ic->ic_bsschan) {
302		/* XXX need a way to defer to user app */
303		dfs->newchan = ieee80211_dfs_pickchannel(ic);
304
305		announce_radar(ic->ic_ifp, chan, dfs->newchan);
306
307		if (callout_pending(&dfs->cac_timer))
308			callout_schedule(&dfs->cac_timer, 0);
309		else if (dfs->newchan != NULL) {
310			/* XXX mode 1, switch count 2 */
311			/* XXX calculate switch count based on max
312			  switch time and beacon interval? */
313			ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
314		} else {
315			/*
316			 * Spec says to stop all transmissions and
317			 * wait on the current channel for an entry
318			 * on the NOL to expire.
319			 */
320			/*XXX*/
321		}
322	} else {
323		/*
324		 * Issue rate-limited console msgs.
325		 */
326		if (dfs->lastchan != chan) {
327			dfs->lastchan = chan;
328			dfs->cureps = 0;
329			announce_radar(ic->ic_ifp, chan, NULL);
330		} else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
331			announce_radar(ic->ic_ifp, chan, NULL);
332		}
333	}
334}
335
336struct ieee80211_channel *
337ieee80211_dfs_pickchannel(struct ieee80211com *ic)
338{
339	struct ieee80211_channel *c;
340	int i, flags;
341	uint16_t v;
342
343	/*
344	 * Consult the scan cache first.
345	 */
346	flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
347	/*
348	 * XXX if curchan is HT this will never find a channel
349	 * XXX 'cuz we scan only legacy channels
350	 */
351	c = ieee80211_scan_pickchannel(ic, flags);
352	if (c != NULL)
353		return c;
354	/*
355	 * No channel found in scan cache; select a compatible
356	 * one at random (skipping channels where radar has
357	 * been detected).
358	 */
359	get_random_bytes(&v, sizeof(v));
360	v %= ic->ic_nchans;
361	for (i = v; i < ic->ic_nchans; i++) {
362		c = &ic->ic_channels[i];
363		if (!IEEE80211_IS_CHAN_RADAR(c) &&
364		   (c->ic_flags & flags) == flags)
365			return c;
366	}
367	for (i = 0; i < v; i++) {
368		c = &ic->ic_channels[i];
369		if (!IEEE80211_IS_CHAN_RADAR(c) &&
370		   (c->ic_flags & flags) == flags)
371			return c;
372	}
373	if_printf(ic->ic_ifp, "HELP, no channel located to switch to!\n");
374	return NULL;
375}
376