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