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