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