inet6: require RTF_ANNOUNCE to proxy NS
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_dfs.c
blob20202ba4d614348731477c02e07b1ea3a7264751
1 /*-
2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3 * All rights reserved.
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.
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.
26 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
32 * IEEE 802.11 DFS/Radar support.
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
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>
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>
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_media.h>
53 #include <net/ethernet.h>
55 #include <netproto/802_11/ieee80211_var.h>
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)
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)
68 DFS* In order to facilitate debugging, a couple of operating
69 * modes aside from the default are needed.
71 * 0 - default CAC/NOL behaviour - ie, start CAC, place
72 * channel on NOL list.
73 * 1 - send CAC, but don't change channel or add the channel
74 * to the NOL list.
75 * 2 - just match on radar, don't send CAC or place channel in
76 * the NOL list.
78 static int ieee80211_dfs_debug = DFS_DBG_NONE;
81 * This option must not be included in the default kernel
82 * as it allows users to plainly disable CAC/NOL handling.
84 #ifdef IEEE80211_DFS_DEBUG
85 SYSCTL_INT(_net_wlan, OID_AUTO, dfs_debug, CTLFLAG_RW,
86 &ieee80211_dfs_debug, 0, "DFS debug behaviour");
87 #endif
89 static int
90 null_set_quiet(struct ieee80211_node *ni, u_int8_t *quiet_elm)
92 return ENOSYS;
95 void
96 ieee80211_dfs_attach(struct ieee80211com *ic)
98 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
100 callout_init_mtx(&dfs->nol_timer, IEEE80211_LOCK_OBJ(ic), 0);
101 callout_init_mtx(&dfs->cac_timer, IEEE80211_LOCK_OBJ(ic), 0);
103 ic->ic_set_quiet = null_set_quiet;
106 void
107 ieee80211_dfs_detach(struct ieee80211com *ic)
109 /* NB: we assume no locking is needed */
110 ieee80211_dfs_reset(ic);
113 void
114 ieee80211_dfs_reset(struct ieee80211com *ic)
116 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
117 int i;
119 /* NB: we assume no locking is needed */
120 /* NB: cac_timer should be cleared by the state machine */
121 callout_drain(&dfs->nol_timer);
122 for (i = 0; i < ic->ic_nchans; i++)
123 ic->ic_channels[i].ic_state = 0;
124 dfs->lastchan = NULL;
127 static void
128 cac_timeout(void *arg)
130 struct ieee80211vap *vap = arg;
131 struct ieee80211com *ic = vap->iv_ic;
132 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
133 int i;
135 IEEE80211_LOCK_ASSERT(ic);
137 if (vap->iv_state != IEEE80211_S_CAC) /* NB: just in case */
138 return;
140 * When radar is detected during a CAC we are woken
141 * up prematurely to switch to a new channel.
142 * Check the channel to decide how to act.
144 if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
145 ieee80211_notify_cac(ic, ic->ic_curchan,
146 IEEE80211_NOTIFY_CAC_RADAR);
148 if_printf(vap->iv_ifp,
149 "CAC timer on channel %u (%u MHz) stopped due to radar\n",
150 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
152 /* XXX clobbers any existing desired channel */
153 /* NB: dfs->newchan may be NULL, that's ok */
154 vap->iv_des_chan = dfs->newchan;
155 /* XXX recursive lock need ieee80211_new_state_locked */
156 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
157 } else {
158 if_printf(vap->iv_ifp,
159 "CAC timer on channel %u (%u MHz) expired; "
160 "no radar detected\n",
161 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
163 * Mark all channels with the current frequency
164 * as having completed CAC; this keeps us from
165 * doing it again until we change channels.
167 for (i = 0; i < ic->ic_nchans; i++) {
168 struct ieee80211_channel *c = &ic->ic_channels[i];
169 if (c->ic_freq == ic->ic_curchan->ic_freq)
170 c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
172 ieee80211_notify_cac(ic, ic->ic_curchan,
173 IEEE80211_NOTIFY_CAC_EXPIRE);
174 ieee80211_cac_completeswitch(vap);
179 * Initiate the CAC timer. The driver is responsible
180 * for setting up the hardware to scan for radar on the
181 * channnel, we just handle timing things out.
183 void
184 ieee80211_dfs_cac_start(struct ieee80211vap *vap)
186 struct ieee80211com *ic = vap->iv_ic;
187 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
189 IEEE80211_LOCK_ASSERT(ic);
191 callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap);
192 if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
193 ticks_to_secs(CAC_TIMEOUT),
194 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
195 ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
199 * Clear the CAC timer.
201 void
202 ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
204 struct ieee80211com *ic = vap->iv_ic;
205 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
207 IEEE80211_LOCK_ASSERT(ic);
209 /* NB: racey but not important */
210 if (callout_pending(&dfs->cac_timer)) {
211 if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
212 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
213 ieee80211_notify_cac(ic, ic->ic_curchan,
214 IEEE80211_NOTIFY_CAC_STOP);
216 callout_stop(&dfs->cac_timer);
219 void
220 ieee80211_dfs_cac_clear(struct ieee80211com *ic,
221 const struct ieee80211_channel *chan)
223 int i;
225 for (i = 0; i < ic->ic_nchans; i++) {
226 struct ieee80211_channel *c = &ic->ic_channels[i];
227 if (c->ic_freq == chan->ic_freq)
228 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
232 static void
233 dfs_timeout(void *arg)
235 struct ieee80211com *ic = arg;
236 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
237 struct ieee80211_channel *c;
238 int i, oldest, now;
240 IEEE80211_LOCK_ASSERT(ic);
242 now = oldest = ticks;
243 for (i = 0; i < ic->ic_nchans; i++) {
244 c = &ic->ic_channels[i];
245 if (IEEE80211_IS_CHAN_RADAR(c)) {
246 if (ieee80211_time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
247 c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
248 if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
250 * NB: do this here so we get only one
251 * msg instead of one for every channel
252 * table entry.
254 ic_printf(ic, "radar on channel %u "
255 "(%u MHz) cleared after timeout\n",
256 c->ic_ieee, c->ic_freq);
257 /* notify user space */
258 c->ic_state &=
259 ~IEEE80211_CHANSTATE_NORADAR;
260 ieee80211_notify_radar(ic, c);
262 } else if (dfs->nol_event[i] < oldest)
263 oldest = dfs->nol_event[i];
266 if (oldest != now) {
267 /* arrange to process next channel up for a status change */
268 #if defined(__DragonFly__)
269 callout_schedule_dfly(&dfs->nol_timer, oldest + NOL_TIMEOUT - now,
270 dfs_timeout, ic);
271 #else
272 callout_schedule(&dfs->nol_timer, oldest + NOL_TIMEOUT - now);
273 #endif
277 static void
278 announce_radar(struct ieee80211com *ic, const struct ieee80211_channel *curchan,
279 const struct ieee80211_channel *newchan)
281 if (newchan == NULL)
282 ic_printf(ic, "radar detected on channel %u (%u MHz)\n",
283 curchan->ic_ieee, curchan->ic_freq);
284 else
285 ic_printf(ic, "radar detected on channel %u (%u MHz), "
286 "moving to channel %u (%u MHz)\n",
287 curchan->ic_ieee, curchan->ic_freq,
288 newchan->ic_ieee, newchan->ic_freq);
292 * Handle a radar detection event on a channel. The channel is
293 * added to the NOL list and we record the time of the event.
294 * Entries are aged out after NOL_TIMEOUT. If radar was
295 * detected while doing CAC we force a state/channel change.
296 * Otherwise radar triggers a channel switch using the CSA
297 * mechanism (when the channel is the bss channel).
299 void
300 ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
302 struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
303 int i, now;
305 IEEE80211_LOCK_ASSERT(ic);
308 * If doing DFS debugging (mode 2), don't bother
309 * running the rest of this function.
311 * Simply announce the presence of the radar and continue
312 * along merrily.
314 if (ieee80211_dfs_debug == DFS_DBG_NOCSANOL) {
315 announce_radar(ic, chan, chan);
316 ieee80211_notify_radar(ic, chan);
317 return;
321 * Don't mark the channel and don't put it into NOL
322 * if we're doing DFS debugging.
324 if (ieee80211_dfs_debug == DFS_DBG_NONE) {
326 * Mark all entries with this frequency. Notify user
327 * space and arrange for notification when the radar
328 * indication is cleared. Then kick the NOL processing
329 * thread if not already running.
331 now = ticks;
332 for (i = 0; i < ic->ic_nchans; i++) {
333 struct ieee80211_channel *c = &ic->ic_channels[i];
334 if (c->ic_freq == chan->ic_freq) {
335 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
336 c->ic_state |= IEEE80211_CHANSTATE_RADAR;
337 dfs->nol_event[i] = now;
340 ieee80211_notify_radar(ic, chan);
341 chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
342 if (!callout_pending(&dfs->nol_timer))
343 callout_reset(&dfs->nol_timer, NOL_TIMEOUT,
344 dfs_timeout, ic);
348 * If radar is detected on the bss channel while
349 * doing CAC; force a state change by scheduling the
350 * callout to be dispatched asap. Otherwise, if this
351 * event is for the bss channel then we must quiet
352 * traffic and schedule a channel switch.
354 * Note this allows us to receive notification about
355 * channels other than the bss channel; not sure
356 * that can/will happen but it's simple to support.
358 if (chan == ic->ic_bsschan) {
359 /* XXX need a way to defer to user app */
362 * Don't flip over to a new channel if
363 * we are currently doing DFS debugging.
365 if (ieee80211_dfs_debug == DFS_DBG_NONE)
366 dfs->newchan = ieee80211_dfs_pickchannel(ic);
367 else
368 dfs->newchan = chan;
370 announce_radar(ic, chan, dfs->newchan);
372 if (callout_pending(&dfs->cac_timer))
373 #if defined(__DragonFly__)
374 callout_schedule_dfly(&dfs->cac_timer, 0,
375 cac_timeout,
376 callout_arg(&dfs->cac_timer));
377 #else
378 callout_schedule(&dfs->cac_timer, 0);
379 #endif
380 else if (dfs->newchan != NULL) {
381 /* XXX mode 1, switch count 2 */
382 /* XXX calculate switch count based on max
383 switch time and beacon interval? */
384 ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
385 } else {
387 * Spec says to stop all transmissions and
388 * wait on the current channel for an entry
389 * on the NOL to expire.
391 /*XXX*/
392 ic_printf(ic, "%s: No free channels; waiting for entry "
393 "on NOL to expire\n", __func__);
395 } else {
397 * Issue rate-limited console msgs.
399 if (dfs->lastchan != chan) {
400 dfs->lastchan = chan;
401 dfs->cureps = 0;
402 announce_radar(ic, chan, NULL);
403 } else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
404 announce_radar(ic, chan, NULL);
409 struct ieee80211_channel *
410 ieee80211_dfs_pickchannel(struct ieee80211com *ic)
412 struct ieee80211_channel *c;
413 int i, flags;
414 uint16_t v;
417 * Consult the scan cache first.
419 flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
421 * XXX if curchan is HT this will never find a channel
422 * XXX 'cuz we scan only legacy channels
424 c = ieee80211_scan_pickchannel(ic, flags);
425 if (c != NULL)
426 return c;
428 * No channel found in scan cache; select a compatible
429 * one at random (skipping channels where radar has
430 * been detected).
432 get_random_bytes(&v, sizeof(v));
433 v %= ic->ic_nchans;
434 for (i = v; i < ic->ic_nchans; i++) {
435 c = &ic->ic_channels[i];
436 if (!IEEE80211_IS_CHAN_RADAR(c) &&
437 (c->ic_flags & flags) == flags)
438 return c;
440 for (i = 0; i < v; i++) {
441 c = &ic->ic_channels[i];
442 if (!IEEE80211_IS_CHAN_RADAR(c) &&
443 (c->ic_flags & flags) == flags)
444 return c;
446 ic_printf(ic, "HELP, no channel located to switch to!\n");
447 return NULL;