source: rtems-libbsd/freebsd/sys/net80211/ieee80211_dfs.c @ 66659ff

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 66659ff was 66659ff, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 15:20:21

Update to FreeBSD 9.2

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