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