]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_dfs.c
Upgrade to OpenSSH 5.1p1.
[FreeBSD/FreeBSD.git] / sys / net80211 / 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_media.h>
52
53 #include <net80211/ieee80211_var.h>
54
55 MALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state");
56
57 /* XXX public for sysctl hookup */
58 int     ieee80211_nol_timeout = 30*60;          /* 30 minutes */
59 #define NOL_TIMEOUT     msecs_to_ticks(ieee80211_nol_timeout*1000)
60 int     ieee80211_cac_timeout = 60;             /* 60 seconds */
61 #define CAC_TIMEOUT     msecs_to_ticks(ieee80211_cac_timeout*1000)
62
63 void
64 ieee80211_dfs_attach(struct ieee80211com *ic)
65 {
66         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
67
68         callout_init(&dfs->nol_timer, CALLOUT_MPSAFE);
69         callout_init(&dfs->cac_timer, CALLOUT_MPSAFE);
70 }
71
72 void
73 ieee80211_dfs_detach(struct ieee80211com *ic)
74 {
75         /* NB: we assume no locking is needed */
76         ieee80211_dfs_reset(ic);
77 }
78
79 void
80 ieee80211_dfs_reset(struct ieee80211com *ic)
81 {
82         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
83         int i;
84
85         /* NB: we assume no locking is needed */
86         /* NB: cac_timer should be cleared by the state machine */
87         callout_drain(&dfs->nol_timer);
88         for (i = 0; i < ic->ic_nchans; i++)
89                 ic->ic_channels[i].ic_state = 0;
90         dfs->lastchan = NULL;
91 }
92
93 static void
94 cac_timeout(void *arg)
95 {
96         struct ieee80211vap *vap = arg;
97         struct ieee80211com *ic = vap->iv_ic;
98         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
99         int i;
100
101         if (vap->iv_state != IEEE80211_S_CAC)   /* NB: just in case */
102                 return;
103         /*
104          * When radar is detected during a CAC we are woken
105          * up prematurely to switch to a new channel.
106          * Check the channel to decide how to act.
107          */
108         if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) {
109                 ieee80211_notify_cac(ic, ic->ic_curchan,
110                     IEEE80211_NOTIFY_CAC_RADAR);
111
112                 if_printf(vap->iv_ifp,
113                     "CAC timer on channel %u (%u MHz) stopped due to radar\n",
114                     ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
115
116                 /* XXX clobbers any existing desired channel */
117                 /* NB: dfs->newchan may be NULL, that's ok */
118                 vap->iv_des_chan = dfs->newchan;
119                 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
120         } else {
121                 if_printf(vap->iv_ifp,
122                     "CAC timer on channel %u (%u MHz) expired; "
123                     "no radar detected\n",
124                     ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
125                 /*
126                  * Mark all channels with the current frequency
127                  * as having completed CAC; this keeps us from
128                  * doing it again until we change channels.
129                  */
130                 for (i = 0; i < ic->ic_nchans; i++) {
131                         struct ieee80211_channel *c = &ic->ic_channels[i];
132                         if (c->ic_freq == ic->ic_curchan->ic_freq)
133                                 c->ic_state |= IEEE80211_CHANSTATE_CACDONE;
134                 }
135                 ieee80211_notify_cac(ic, ic->ic_curchan,
136                     IEEE80211_NOTIFY_CAC_EXPIRE);
137                 ieee80211_cac_completeswitch(vap);
138         }
139 }
140
141 /*
142  * Initiate the CAC timer.  The driver is responsible
143  * for setting up the hardware to scan for radar on the
144  * channnel, we just handle timing things out.
145  */
146 void
147 ieee80211_dfs_cac_start(struct ieee80211vap *vap)
148 {
149         struct ieee80211com *ic = vap->iv_ic;
150         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
151
152         IEEE80211_LOCK_ASSERT(ic);
153
154         callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap);
155         if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n",
156             ticks_to_secs(CAC_TIMEOUT),
157             ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
158         ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START);
159 }
160
161 /*
162  * Clear the CAC timer.
163  */
164 void
165 ieee80211_dfs_cac_stop(struct ieee80211vap *vap)
166 {
167         struct ieee80211com *ic = vap->iv_ic;
168         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
169
170         IEEE80211_LOCK_ASSERT(ic);
171
172         /* NB: racey but not important */
173         if (callout_pending(&dfs->cac_timer)) {
174                 if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n",
175                     ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq);
176                 ieee80211_notify_cac(ic, ic->ic_curchan,
177                     IEEE80211_NOTIFY_CAC_STOP);
178         }
179         /* XXX cannot use drain 'cuz holding a lock */
180         callout_stop(&dfs->cac_timer);
181 }
182
183 void
184 ieee80211_dfs_cac_clear(struct ieee80211com *ic,
185         const struct ieee80211_channel *chan)
186 {
187         int i;
188
189         for (i = 0; i < ic->ic_nchans; i++) {
190                 struct ieee80211_channel *c = &ic->ic_channels[i];
191                 if (c->ic_freq == chan->ic_freq)
192                         c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
193         }
194 }
195
196 static void
197 dfs_timeout(void *arg)
198 {
199         struct ieee80211com *ic = arg;
200         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
201         struct ieee80211_channel *c;
202         int i, oldest, now;
203
204         IEEE80211_LOCK(ic);
205         now = oldest = ticks;
206         for (i = 0; i < ic->ic_nchans; i++) {
207                 c = &ic->ic_channels[i];
208                 if (IEEE80211_IS_CHAN_RADAR(c)) {
209                         if (time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) {
210                                 c->ic_state &= ~IEEE80211_CHANSTATE_RADAR;
211                                 if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) {
212                                         /*
213                                          * NB: do this here so we get only one
214                                          * msg instead of one for every channel
215                                          * table entry.
216                                          */
217                                         if_printf(ic->ic_ifp, "radar on channel"
218                                             " %u (%u MHz) cleared after timeout\n",
219                                             c->ic_ieee, c->ic_freq);
220                                         /* notify user space */
221                                         c->ic_state &=
222                                             ~IEEE80211_CHANSTATE_NORADAR;
223                                         ieee80211_notify_radar(ic, c);
224                                 }
225                         } else if (dfs->nol_event[i] < oldest)
226                                 oldest = dfs->nol_event[i];
227                 }
228         }
229         if (oldest != now) {
230                 /* arrange to process next channel up for a status change */
231                 callout_reset(&dfs->nol_timer, oldest + NOL_TIMEOUT,
232                     dfs_timeout, ic);
233         }
234         IEEE80211_UNLOCK(ic);
235 }
236
237 static void
238 announce_radar(struct ifnet *ifp, const struct ieee80211_channel *curchan,
239         const struct ieee80211_channel *newchan)
240 {
241         if (newchan == NULL)
242                 if_printf(ifp, "radar detected on channel %u (%u MHz)\n",
243                     curchan->ic_ieee, curchan->ic_freq);
244         else
245                 if_printf(ifp, "radar detected on channel %u (%u MHz), "
246                     "moving to channel %u (%u MHz)\n",
247                     curchan->ic_ieee, curchan->ic_freq,
248                     newchan->ic_ieee, newchan->ic_freq);
249 }
250
251 /*
252  * Handle a radar detection event on a channel. The channel is
253  * added to the NOL list and we record the time of the event.
254  * Entries are aged out after NOL_TIMEOUT.  If radar was
255  * detected while doing CAC we force a state/channel change.
256  * Otherwise radar triggers a channel switch using the CSA
257  * mechanism (when the channel is the bss channel).
258  */
259 void
260 ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan)
261 {
262         struct ieee80211_dfs_state *dfs = &ic->ic_dfs;
263         int i, now;
264
265         IEEE80211_LOCK_ASSERT(ic);
266
267         /*
268          * Mark all entries with this frequency.  Notify user
269          * space and arrange for notification when the radar
270          * indication is cleared.  Then kick the NOL processing
271          * thread if not already running.
272          */
273         now = ticks;
274         for (i = 0; i < ic->ic_nchans; i++) {
275                 struct ieee80211_channel *c = &ic->ic_channels[i];
276                 if (c->ic_freq == chan->ic_freq) {
277                         c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE;
278                         c->ic_state |= IEEE80211_CHANSTATE_RADAR;
279                         dfs->nol_event[i] = now;
280                 }
281         }
282         ieee80211_notify_radar(ic, chan);
283         chan->ic_state |= IEEE80211_CHANSTATE_NORADAR;
284         if (!callout_pending(&dfs->nol_timer))
285                 callout_reset(&dfs->nol_timer, NOL_TIMEOUT, dfs_timeout, ic);
286
287         /*
288          * If radar is detected on the bss channel while
289          * doing CAC; force a state change by scheduling the
290          * callout to be dispatched asap.  Otherwise, if this
291          * event is for the bss channel then we must quiet
292          * traffic and schedule a channel switch.
293          *
294          * Note this allows us to receive notification about
295          * channels other than the bss channel; not sure
296          * that can/will happen but it's simple to support.
297          */
298         if (chan == ic->ic_bsschan) {
299                 /* XXX need a way to defer to user app */
300                 dfs->newchan = ieee80211_dfs_pickchannel(ic);
301
302                 announce_radar(ic->ic_ifp, chan, dfs->newchan);
303
304                 if (callout_pending(&dfs->cac_timer))
305                         callout_reset(&dfs->nol_timer, 0, dfs_timeout, ic);
306                 else if (dfs->newchan != NULL) {
307                         /* XXX mode 1, switch count 2 */
308                         /* XXX calculate switch count based on max
309                           switch time and beacon interval? */
310                         ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2);
311                 } else {
312                         /*
313                          * Spec says to stop all transmissions and
314                          * wait on the current channel for an entry
315                          * on the NOL to expire.
316                          */
317                         /*XXX*/
318                 }
319         } else {
320                 /*
321                  * Issue rate-limited console msgs.
322                  */
323                 if (dfs->lastchan != chan) {
324                         dfs->lastchan = chan;
325                         dfs->cureps = 0;
326                         announce_radar(ic->ic_ifp, chan, NULL);
327                 } else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) {
328                         announce_radar(ic->ic_ifp, chan, NULL);
329                 }
330         }
331 }
332
333 struct ieee80211_channel *
334 ieee80211_dfs_pickchannel(struct ieee80211com *ic)
335 {
336         struct ieee80211_channel *c;
337         int i, flags;
338         uint16_t v;
339
340         /*
341          * Consult the scan cache first.
342          */
343         flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL;
344         /*
345          * XXX if curchan is HT this will never find a channel
346          * XXX 'cuz we scan only legacy channels
347          */
348         c = ieee80211_scan_pickchannel(ic, flags);
349         if (c != NULL)
350                 return c;
351         /*
352          * No channel found in scan cache; select a compatible
353          * one at random (skipping channels where radar has
354          * been detected).
355          */
356         get_random_bytes(&v, sizeof(v));
357         v %= ic->ic_nchans;
358         for (i = v; i < ic->ic_nchans; i++) {
359                 c = &ic->ic_channels[i];
360                 if (!IEEE80211_IS_CHAN_RADAR(c) &&
361                    (c->ic_flags & flags) == flags)
362                         return c;
363         }
364         for (i = 0; i < v; i++) {
365                 c = &ic->ic_channels[i];
366                 if (!IEEE80211_IS_CHAN_RADAR(c) &&
367                    (c->ic_flags & flags) == flags)
368                         return c;
369         }
370         if_printf(ic->ic_ifp, "HELP, no channel located to switch to!\n");
371         return NULL;
372 }