wlan - Update wlan from Adrian / FreeBSD
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_dfs.c
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_var.h>
52 #include <net/if_media.h>
53 #include <net/ethernet.h>
54
55 #include <netproto/802_11/ieee80211_var.h>
56
57 static MALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state");
58
59 static  int ieee80211_nol_timeout = 30*60;              /* 30 minutes */
60 SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW,
61         &ieee80211_nol_timeout, 0, "NOL timeout (secs)");
62 #define NOL_TIMEOUT     msecs_to_ticks(ieee80211_nol_timeout*1000)
63
64 static  int ieee80211_cac_timeout = 60;         /* 60 seconds */
65 SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW,
66         &ieee80211_cac_timeout, 0, "CAC timeout (secs)");
67 #define CAC_TIMEOUT     msecs_to_ticks(ieee80211_cac_timeout*1000)
68
69 /*
70  DFS* In order to facilitate  debugging, a couple of operating
71  * modes aside from the default are needed.
72  *
73  * 0 - default CAC/NOL behaviour - ie, start CAC, place
74  *     channel on NOL list.
75  * 1 - send CAC, but don't change channel or add the channel
76  *     to the NOL list.
77  * 2 - just match on radar, don't send CAC or place channel in
78  *     the NOL list.
79  */
80 static  int ieee80211_dfs_debug = DFS_DBG_NONE;
81
82 /*
83  * This option must not be included in the default kernel
84  * as it allows users to plainly disable CAC/NOL handling.
85  */
86 #ifdef  IEEE80211_DFS_DEBUG
87 SYSCTL_INT(_net_wlan, OID_AUTO, dfs_debug, CTLFLAG_RW,
88         &ieee80211_dfs_debug, 0, "DFS debug behaviour");
89 #endif
90
91 static int
92 null_set_quiet(struct ieee80211_node *ni, u_int8_t *quiet_elm)
93 {
94         return ENOSYS;
95 }
96
97 void
98 ieee80211_dfs_attach(struct ieee80211com *ic)
99 {
100         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
101
102         callout_init_mtx(&dfs->nol_timer, IEEE80211_LOCK_OBJ(ic), 0);
103         callout_init_mtx(&dfs->cac_timer, IEEE80211_LOCK_OBJ(ic), 0);
104
105         ic->ic_set_quiet = null_set_quiet;
106 }
107
108 void
109 ieee80211_dfs_detach(struct ieee80211com *ic)
110 {
111         /* NB: we assume no locking is needed */
112         ieee80211_dfs_reset(ic);
113 }
114
115 void
116 ieee80211_dfs_reset(struct ieee80211com *ic)
117 {
118         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
119         int i;
120
121         /* NB: we assume no locking is needed */
122         /* NB: cac_timer should be cleared by the state machine */
123         callout_drain(&dfs->nol_timer);
124         for (i = 0; i < ic->ic_nchans; i++)
125                 ic->ic_channels[i].ic_state = 0;
126         dfs->lastchan = NULL;
127 }
128
129 static void
130 cac_timeout(void *arg)
131 {
132         struct ieee80211vap *vap = arg;
133         struct ieee80211com *ic = vap->iv_ic;
134         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
135         int i;
136
137         IEEE80211_LOCK_ASSERT(ic);
138
139         if (vap->iv_state != IEEE80211_S_CAC)   /* NB: just in case */
140                 return;
141         /*
142          * When radar is detected during a CAC we are woken
143          * up prematurely to switch to a new channel.
144          * Check the channel to decide how to act.
145          */
146         if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
147                 ieee80211_notify_cac(ic, ic->ic_curchan,
148                     IEEE80211_NOTIFY_CAC_RADAR);
149
150                 if_printf(vap->iv_ifp,
151                     "CAC timer on channel %u (%u MHz) stopped due to radar\n",
152                     ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
153
154                 /* XXX clobbers any existing desired channel */
155                 /* NB: dfs->newchan may be NULL, that's ok */
156                 vap->iv_des_chan = dfs->newchan;
157                 /* XXX recursive lock need ieee80211_new_state_locked */
158                 ieee80211_new_state(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 (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                                         if_printf(ic->ic_ifp, "radar on channel"
257                                             " %u (%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_dfly(&dfs->nol_timer, oldest + NOL_TIMEOUT - now,
271                                 dfs_timeout, ic);
272         }
273 }
274
275 static void
276 announce_radar(struct ifnet *ifp, const struct ieee80211_channel *curchan,
277         const struct ieee80211_channel *newchan)
278 {
279         if (newchan == NULL)
280                 if_printf(ifp, "radar detected on channel %u (%u MHz)\n",
281                     curchan->ic_ieee, curchan->ic_freq);
282         else
283                 if_printf(ifp, "radar detected on channel %u (%u MHz), "
284                     "moving to channel %u (%u MHz)\n",
285                     curchan->ic_ieee, curchan->ic_freq,
286                     newchan->ic_ieee, newchan->ic_freq);
287 }
288
289 /*
290  * Handle a radar detection event on a channel. The channel is
291  * added to the NOL list and we record the time of the event.
292  * Entries are aged out after NOL_TIMEOUT.  If radar was
293  * detected while doing CAC we force a state/channel change.
294  * Otherwise radar triggers a channel switch using the CSA
295  * mechanism (when the channel is the bss channel).
296  */
297 void
298 ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
299 {
300         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
301         int i, now;
302
303         IEEE80211_LOCK_ASSERT(ic);
304
305         /*
306          * If doing DFS debugging (mode 2), don't bother
307          * running the rest of this function.
308          *
309          * Simply announce the presence of the radar and continue
310          * along merrily.
311          */
312         if (ieee80211_dfs_debug == DFS_DBG_NOCSANOL) {
313                 announce_radar(ic->ic_ifp, chan, chan);
314                 ieee80211_notify_radar(ic, chan);
315                 return;
316         }
317
318         /*
319          * Don't mark the channel and don't put it into NOL
320          * if we're doing DFS debugging.
321          */
322         if (ieee80211_dfs_debug == DFS_DBG_NONE) {
323                 /*
324                  * Mark all entries with this frequency.  Notify user
325                  * space and arrange for notification when the radar
326                  * indication is cleared.  Then kick the NOL processing
327                  * thread if not already running.
328                  */
329                 now = ticks;
330                 for (i = 0; i < ic->ic_nchans; i++) {
331                         struct ieee80211_channel *c = &ic->ic_channels[i];
332                         if (c->ic_freq == chan->ic_freq) {
333                                 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
334                                 c->ic_state |= IEEE80211_CHANSTATE_RADAR;
335                                 dfs->nol_event[i] = now;
336                         }
337                 }
338                 ieee80211_notify_radar(ic, chan);
339                 chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
340                 if (!callout_pending(&dfs->nol_timer))
341                         callout_reset(&dfs->nol_timer, NOL_TIMEOUT,
342                             dfs_timeout, ic);
343         }
344
345         /*
346          * If radar is detected on the bss channel while
347          * doing CAC; force a state change by scheduling the
348          * callout to be dispatched asap.  Otherwise, if this
349          * event is for the bss channel then we must quiet
350          * traffic and schedule a channel switch.
351          *
352          * Note this allows us to receive notification about
353          * channels other than the bss channel; not sure
354          * that can/will happen but it's simple to support.
355          */
356         if (chan == ic->ic_bsschan) {
357                 /* XXX need a way to defer to user app */
358
359                 /*
360                  * Don't flip over to a new channel if
361                  * we are currently doing DFS debugging.
362                  */
363                 if (ieee80211_dfs_debug == DFS_DBG_NONE)
364                         dfs->newchan = ieee80211_dfs_pickchannel(ic);
365                 else
366                         dfs->newchan = chan;
367
368                 announce_radar(ic->ic_ifp, chan, dfs->newchan);
369
370                 if (callout_pending(&dfs->cac_timer))
371                         callout_schedule_dfly(&dfs->cac_timer, 0, cac_timeout, dfs->cac_timer.c_arg);
372                 else if (dfs->newchan != NULL) {
373                         /* XXX mode 1, switch count 2 */
374                         /* XXX calculate switch count based on max
375                           switch time and beacon interval? */
376                         ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
377                 } else {
378                         /*
379                          * Spec says to stop all transmissions and
380                          * wait on the current channel for an entry
381                          * on the NOL to expire.
382                          */
383                         /*XXX*/
384                         if_printf(ic->ic_ifp, "%s: No free channels; waiting for entry "
385                             "on NOL to expire\n", __func__);
386                 }
387         } else {
388                 /*
389                  * Issue rate-limited console msgs.
390                  */
391                 if (dfs->lastchan != chan) {
392                         dfs->lastchan = chan;
393                         dfs->cureps = 0;
394                         announce_radar(ic->ic_ifp, chan, NULL);
395                 } else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
396                         announce_radar(ic->ic_ifp, chan, NULL);
397                 }
398         }
399 }
400
401 struct ieee80211_channel *
402 ieee80211_dfs_pickchannel(struct ieee80211com *ic)
403 {
404         struct ieee80211_channel *c;
405         int i, flags;
406         uint16_t v;
407
408         /*
409          * Consult the scan cache first.
410          */
411         flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
412         /*
413          * XXX if curchan is HT this will never find a channel
414          * XXX 'cuz we scan only legacy channels
415          */
416         c = ieee80211_scan_pickchannel(ic, flags);
417         if (c != NULL)
418                 return c;
419         /*
420          * No channel found in scan cache; select a compatible
421          * one at random (skipping channels where radar has
422          * been detected).
423          */
424         get_random_bytes(&v, sizeof(v));
425         v %= ic->ic_nchans;
426         for (i = v; i < ic->ic_nchans; i++) {
427                 c = &ic->ic_channels[i];
428                 if (!IEEE80211_IS_CHAN_RADAR(c) &&
429                    (c->ic_flags & flags) == flags)
430                         return c;
431         }
432         for (i = 0; i < v; i++) {
433                 c = &ic->ic_channels[i];
434                 if (!IEEE80211_IS_CHAN_RADAR(c) &&
435                    (c->ic_flags & flags) == flags)
436                         return c;
437         }
438         if_printf(ic->ic_ifp, "HELP, no channel located to switch to!\n");
439         return NULL;
440 }