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