ieee80211_dfs.c revision 227331
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 227331 2011-11-08 04:00:24Z adrian $");
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
55static MALLOC_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
67static int
68null_set_quiet(struct ieee80211_node *ni, u_int8_t *quiet_elm)
69{
70	return ENOSYS;
71}
72
73void
74ieee80211_dfs_attach(struct ieee80211com *ic)
75{
76	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
77
78	callout_init_mtx(&dfs->nol_timer, IEEE80211_LOCK_OBJ(ic), 0);
79	callout_init_mtx(&dfs->cac_timer, IEEE80211_LOCK_OBJ(ic), 0);
80
81	ic->ic_set_quiet = null_set_quiet;
82}
83
84void
85ieee80211_dfs_detach(struct ieee80211com *ic)
86{
87	/* NB: we assume no locking is needed */
88	ieee80211_dfs_reset(ic);
89}
90
91void
92ieee80211_dfs_reset(struct ieee80211com *ic)
93{
94	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
95	int i;
96
97	/* NB: we assume no locking is needed */
98	/* NB: cac_timer should be cleared by the state machine */
99	callout_drain(&dfs->nol_timer);
100	for (i = 0; i < ic->ic_nchans; i++)
101		ic->ic_channels[i].ic_state = 0;
102	dfs->lastchan = NULL;
103}
104
105static void
106cac_timeout(void *arg)
107{
108	struct ieee80211vap *vap = arg;
109	struct ieee80211com *ic = vap->iv_ic;
110	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
111	int i;
112
113	IEEE80211_LOCK_ASSERT(ic);
114
115	if (vap->iv_state != IEEE80211_S_CAC)	/* NB: just in case */
116		return;
117	/*
118	 * When radar is detected during a CAC we are woken
119	 * up prematurely to switch to a new channel.
120	 * Check the channel to decide how to act.
121	 */
122	if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
123		ieee80211_notify_cac(ic, ic->ic_curchan,
124		    IEEE80211_NOTIFY_CAC_RADAR);
125
126		if_printf(vap->iv_ifp,
127		    "CAC timer on channel %u (%u MHz) stopped due to radar\n",
128		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
129
130		/* XXX clobbers any existing desired channel */
131		/* NB: dfs->newchan may be NULL, that's ok */
132		vap->iv_des_chan = dfs->newchan;
133		/* XXX recursive lock need ieee80211_new_state_locked */
134		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
135	} else {
136		if_printf(vap->iv_ifp,
137		    "CAC timer on channel %u (%u MHz) expired; "
138		    "no radar detected\n",
139		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
140		/*
141		 * Mark all channels with the current frequency
142		 * as having completed CAC; this keeps us from
143		 * doing it again until we change channels.
144		 */
145		for (i = 0; i < ic->ic_nchans; i++) {
146			struct ieee80211_channel *c = &ic->ic_channels[i];
147			if (c->ic_freq == ic->ic_curchan->ic_freq)
148				c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
149		}
150		ieee80211_notify_cac(ic, ic->ic_curchan,
151		    IEEE80211_NOTIFY_CAC_EXPIRE);
152		ieee80211_cac_completeswitch(vap);
153	}
154}
155
156/*
157 * Initiate the CAC timer.  The driver is responsible
158 * for setting up the hardware to scan for radar on the
159 * channnel, we just handle timing things out.
160 */
161void
162ieee80211_dfs_cac_start(struct ieee80211vap *vap)
163{
164	struct ieee80211com *ic = vap->iv_ic;
165	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
166
167	IEEE80211_LOCK_ASSERT(ic);
168
169	callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap);
170	if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
171	    ticks_to_secs(CAC_TIMEOUT),
172	    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
173	ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
174}
175
176/*
177 * Clear the CAC timer.
178 */
179void
180ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
181{
182	struct ieee80211com *ic = vap->iv_ic;
183	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
184
185	IEEE80211_LOCK_ASSERT(ic);
186
187	/* NB: racey but not important */
188	if (callout_pending(&dfs->cac_timer)) {
189		if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
190		    ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
191		ieee80211_notify_cac(ic, ic->ic_curchan,
192		    IEEE80211_NOTIFY_CAC_STOP);
193	}
194	callout_stop(&dfs->cac_timer);
195}
196
197void
198ieee80211_dfs_cac_clear(struct ieee80211com *ic,
199	const struct ieee80211_channel *chan)
200{
201	int i;
202
203	for (i = 0; i < ic->ic_nchans; i++) {
204		struct ieee80211_channel *c = &ic->ic_channels[i];
205		if (c->ic_freq == chan->ic_freq)
206			c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
207	}
208}
209
210static void
211dfs_timeout(void *arg)
212{
213	struct ieee80211com *ic = arg;
214	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
215	struct ieee80211_channel *c;
216	int i, oldest, now;
217
218	IEEE80211_LOCK_ASSERT(ic);
219
220	now = oldest = ticks;
221	for (i = 0; i < ic->ic_nchans; i++) {
222		c = &ic->ic_channels[i];
223		if (IEEE80211_IS_CHAN_RADAR(c)) {
224			if (time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
225				c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
226				if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
227					/*
228					 * NB: do this here so we get only one
229					 * msg instead of one for every channel
230					 * table entry.
231					 */
232					if_printf(ic->ic_ifp, "radar on channel"
233					    " %u (%u MHz) cleared after timeout\n",
234					    c->ic_ieee, c->ic_freq);
235					/* notify user space */
236					c->ic_state &=
237					    ~IEEE80211_CHANSTATE_NORADAR;
238					ieee80211_notify_radar(ic, c);
239				}
240			} else if (dfs->nol_event[i] < oldest)
241				oldest = dfs->nol_event[i];
242		}
243	}
244	if (oldest != now) {
245		/* arrange to process next channel up for a status change */
246		callout_schedule(&dfs->nol_timer, oldest + NOL_TIMEOUT - now);
247	}
248}
249
250static void
251announce_radar(struct ifnet *ifp, const struct ieee80211_channel *curchan,
252	const struct ieee80211_channel *newchan)
253{
254	if (newchan == NULL)
255		if_printf(ifp, "radar detected on channel %u (%u MHz)\n",
256		    curchan->ic_ieee, curchan->ic_freq);
257	else
258		if_printf(ifp, "radar detected on channel %u (%u MHz), "
259		    "moving to channel %u (%u MHz)\n",
260		    curchan->ic_ieee, curchan->ic_freq,
261		    newchan->ic_ieee, newchan->ic_freq);
262}
263
264/*
265 * Handle a radar detection event on a channel. The channel is
266 * added to the NOL list and we record the time of the event.
267 * Entries are aged out after NOL_TIMEOUT.  If radar was
268 * detected while doing CAC we force a state/channel change.
269 * Otherwise radar triggers a channel switch using the CSA
270 * mechanism (when the channel is the bss channel).
271 */
272void
273ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
274{
275	struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
276	int i, now;
277
278	IEEE80211_LOCK_ASSERT(ic);
279
280	/*
281	 * Mark all entries with this frequency.  Notify user
282	 * space and arrange for notification when the radar
283	 * indication is cleared.  Then kick the NOL processing
284	 * thread if not already running.
285	 */
286	now = ticks;
287	for (i = 0; i < ic->ic_nchans; i++) {
288		struct ieee80211_channel *c = &ic->ic_channels[i];
289		if (c->ic_freq == chan->ic_freq) {
290			c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
291			c->ic_state |= IEEE80211_CHANSTATE_RADAR;
292			dfs->nol_event[i] = now;
293		}
294	}
295	ieee80211_notify_radar(ic, chan);
296	chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
297	if (!callout_pending(&dfs->nol_timer))
298		callout_reset(&dfs->nol_timer, NOL_TIMEOUT, dfs_timeout, ic);
299
300	/*
301	 * If radar is detected on the bss channel while
302	 * doing CAC; force a state change by scheduling the
303	 * callout to be dispatched asap.  Otherwise, if this
304	 * event is for the bss channel then we must quiet
305	 * traffic and schedule a channel switch.
306	 *
307	 * Note this allows us to receive notification about
308	 * channels other than the bss channel; not sure
309	 * that can/will happen but it's simple to support.
310	 */
311	if (chan == ic->ic_bsschan) {
312		/* XXX need a way to defer to user app */
313		dfs->newchan = ieee80211_dfs_pickchannel(ic);
314
315		announce_radar(ic->ic_ifp, chan, dfs->newchan);
316
317		if (callout_pending(&dfs->cac_timer))
318			callout_schedule(&dfs->cac_timer, 0);
319		else if (dfs->newchan != NULL) {
320			/* XXX mode 1, switch count 2 */
321			/* XXX calculate switch count based on max
322			  switch time and beacon interval? */
323			ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
324		} else {
325			/*
326			 * Spec says to stop all transmissions and
327			 * wait on the current channel for an entry
328			 * on the NOL to expire.
329			 */
330			/*XXX*/
331			if_printf(ic->ic_ifp, "%s: No free channels; waiting for entry "
332			    "on NOL to expire\n", __func__);
333		}
334	} else {
335		/*
336		 * Issue rate-limited console msgs.
337		 */
338		if (dfs->lastchan != chan) {
339			dfs->lastchan = chan;
340			dfs->cureps = 0;
341			announce_radar(ic->ic_ifp, chan, NULL);
342		} else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
343			announce_radar(ic->ic_ifp, chan, NULL);
344		}
345	}
346}
347
348struct ieee80211_channel *
349ieee80211_dfs_pickchannel(struct ieee80211com *ic)
350{
351	struct ieee80211_channel *c;
352	int i, flags;
353	uint16_t v;
354
355	/*
356	 * Consult the scan cache first.
357	 */
358	flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
359	/*
360	 * XXX if curchan is HT this will never find a channel
361	 * XXX 'cuz we scan only legacy channels
362	 */
363	c = ieee80211_scan_pickchannel(ic, flags);
364	if (c != NULL)
365		return c;
366	/*
367	 * No channel found in scan cache; select a compatible
368	 * one at random (skipping channels where radar has
369	 * been detected).
370	 */
371	get_random_bytes(&v, sizeof(v));
372	v %= ic->ic_nchans;
373	for (i = v; i < ic->ic_nchans; i++) {
374		c = &ic->ic_channels[i];
375		if (!IEEE80211_IS_CHAN_RADAR(c) &&
376		   (c->ic_flags & flags) == flags)
377			return c;
378	}
379	for (i = 0; i < v; i++) {
380		c = &ic->ic_channels[i];
381		if (!IEEE80211_IS_CHAN_RADAR(c) &&
382		   (c->ic_flags & flags) == flags)
383			return c;
384	}
385	if_printf(ic->ic_ifp, "HELP, no channel located to switch to!\n");
386	return NULL;
387}
388