ieee80211_dfs.c revision 193115
122347Spst/*- 222347Spst * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting 322347Spst * All rights reserved. 429964Sache * 592906Smarkm * Redistribution and use in source and binary forms, with or without 622347Spst * modification, are permitted provided that the following conditions 722347Spst * are met: 822347Spst * 1. Redistributions of source code must retain the above copyright 922347Spst * notice, this list of conditions and the following disclaimer. 1022347Spst * 2. Redistributions in binary form must reproduce the above copyright 1122347Spst * notice, this list of conditions and the following disclaimer in the 1222347Spst * documentation and/or other materials provided with the distribution. 1322347Spst * 1422347Spst * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1522347Spst * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1622347Spst * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1722347Spst * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1829964Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1922347Spst * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2022347Spst * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2122347Spst * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2222347Spst * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2322347Spst * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2422347Spst */ 2522347Spst 2622347Spst#include <sys/cdefs.h> 2722347Spst#ifdef __FreeBSD__ 2822347Spst__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_dfs.c 193115 2009-05-30 20:11:23Z sam $"); 2922347Spst#endif 3022347Spst 3122347Spst/* 3222347Spst * IEEE 802.11 DFS/Radar support. 3322347Spst */ 3429964Sache#include "opt_inet.h" 3529964Sache#include "opt_wlan.h" 3629964Sache 3722347Spst#include <sys/param.h> 3822347Spst#include <sys/systm.h> 3922347Spst#include <sys/mbuf.h> 4022347Spst#include <sys/malloc.h> 4122347Spst#include <sys/kernel.h> 4222347Spst 4322347Spst#include <sys/socket.h> 4422347Spst#include <sys/sockio.h> 4522347Spst#include <sys/endian.h> 4622347Spst#include <sys/errno.h> 4722347Spst#include <sys/proc.h> 4822347Spst#include <sys/sysctl.h> 4922347Spst 5022347Spst#include <net/if.h> 5192906Smarkm#include <net/if_media.h> 5222347Spst 5322347Spst#include <net80211/ieee80211_var.h> 5422347Spst 5522347SpstMALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state"); 5622347Spst 5722347Spststatic int ieee80211_nol_timeout = 30*60; /* 30 minutes */ 5822347SpstSYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW, 5922347Spst &ieee80211_nol_timeout, 0, "NOL timeout (secs)"); 6022347Spst#define NOL_TIMEOUT msecs_to_ticks(ieee80211_nol_timeout*1000) 6122347Spst 6222347Spststatic int ieee80211_cac_timeout = 60; /* 60 seconds */ 6322347SpstSYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW, 6422347Spst &ieee80211_cac_timeout, 0, "CAC timeout (secs)"); 6522347Spst#define CAC_TIMEOUT msecs_to_ticks(ieee80211_cac_timeout*1000) 6622347Spst 6722347Spstvoid 6822347Spstieee80211_dfs_attach(struct ieee80211com *ic) 6922347Spst{ 7022347Spst struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 7122347Spst 7222347Spst callout_init(&dfs->nol_timer, CALLOUT_MPSAFE); 7322347Spst callout_init(&dfs->cac_timer, CALLOUT_MPSAFE); 7422347Spst} 7522347Spst 7622347Spstvoid 7722347Spstieee80211_dfs_detach(struct ieee80211com *ic) 7822347Spst{ 7922347Spst /* NB: we assume no locking is needed */ 8022347Spst ieee80211_dfs_reset(ic); 8122347Spst} 8222347Spst 8322347Spstvoid 8422347Spstieee80211_dfs_reset(struct ieee80211com *ic) 8522347Spst{ 8622347Spst struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 8722347Spst int i; 8822347Spst 8922347Spst /* NB: we assume no locking is needed */ 9022347Spst /* NB: cac_timer should be cleared by the state machine */ 9122347Spst callout_drain(&dfs->nol_timer); 9222347Spst for (i = 0; i < ic->ic_nchans; i++) 9322347Spst ic->ic_channels[i].ic_state = 0; 9422347Spst dfs->lastchan = NULL; 9522347Spst} 9622347Spst 9722347Spststatic void 9822347Spstcac_timeout(void *arg) 9922347Spst{ 10022347Spst struct ieee80211vap *vap = arg; 10122347Spst struct ieee80211com *ic = vap->iv_ic; 10222347Spst struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 10322347Spst int i; 10422347Spst 10522347Spst if (vap->iv_state != IEEE80211_S_CAC) /* NB: just in case */ 10622347Spst return; 10722347Spst /* 10822347Spst * When radar is detected during a CAC we are woken 10922347Spst * up prematurely to switch to a new channel. 11022347Spst * Check the channel to decide how to act. 11122347Spst */ 11222347Spst if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) { 11322347Spst ieee80211_notify_cac(ic, ic->ic_curchan, 11422347Spst IEEE80211_NOTIFY_CAC_RADAR); 11522347Spst 11622347Spst if_printf(vap->iv_ifp, 11722347Spst "CAC timer on channel %u (%u MHz) stopped due to radar\n", 11822347Spst ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 11922347Spst 12022347Spst /* XXX clobbers any existing desired channel */ 12122347Spst /* NB: dfs->newchan may be NULL, that's ok */ 12222347Spst vap->iv_des_chan = dfs->newchan; 12322347Spst ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 12422347Spst } else { 12522347Spst if_printf(vap->iv_ifp, 12622347Spst "CAC timer on channel %u (%u MHz) expired; " 12722347Spst "no radar detected\n", 12822347Spst ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 12922347Spst /* 13022347Spst * Mark all channels with the current frequency 13122347Spst * as having completed CAC; this keeps us from 13222347Spst * doing it again until we change channels. 13322347Spst */ 13422347Spst for (i = 0; i < ic->ic_nchans; i++) { 13522347Spst struct ieee80211_channel *c = &ic->ic_channels[i]; 13622347Spst if (c->ic_freq == ic->ic_curchan->ic_freq) 13722347Spst c->ic_state |= IEEE80211_CHANSTATE_CACDONE; 13822347Spst } 13922347Spst ieee80211_notify_cac(ic, ic->ic_curchan, 14022347Spst IEEE80211_NOTIFY_CAC_EXPIRE); 14122347Spst ieee80211_cac_completeswitch(vap); 14222347Spst } 14322347Spst} 14422347Spst 14522347Spst/* 14622347Spst * Initiate the CAC timer. The driver is responsible 14722347Spst * for setting up the hardware to scan for radar on the 14822347Spst * channnel, we just handle timing things out. 14922347Spst */ 15022347Spstvoid 15122347Spstieee80211_dfs_cac_start(struct ieee80211vap *vap) 15222347Spst{ 15322347Spst struct ieee80211com *ic = vap->iv_ic; 15422347Spst struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 15522347Spst 15622347Spst IEEE80211_LOCK_ASSERT(ic); 15722347Spst 15822347Spst callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout, vap); 15922347Spst if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n", 16022347Spst ticks_to_secs(CAC_TIMEOUT), 16122347Spst ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 16222347Spst ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START); 16322347Spst} 16422347Spst 16522347Spst/* 16622347Spst * Clear the CAC timer. 16722347Spst */ 168void 169ieee80211_dfs_cac_stop(struct ieee80211vap *vap) 170{ 171 struct ieee80211com *ic = vap->iv_ic; 172 struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 173 174 IEEE80211_LOCK_ASSERT(ic); 175 176 /* NB: racey but not important */ 177 if (callout_pending(&dfs->cac_timer)) { 178 if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n", 179 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 180 ieee80211_notify_cac(ic, ic->ic_curchan, 181 IEEE80211_NOTIFY_CAC_STOP); 182 } 183 /* XXX cannot use drain 'cuz holding a lock */ 184 callout_stop(&dfs->cac_timer); 185} 186 187void 188ieee80211_dfs_cac_clear(struct ieee80211com *ic, 189 const struct ieee80211_channel *chan) 190{ 191 int i; 192 193 for (i = 0; i < ic->ic_nchans; i++) { 194 struct ieee80211_channel *c = &ic->ic_channels[i]; 195 if (c->ic_freq == chan->ic_freq) 196 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE; 197 } 198} 199 200static void 201dfs_timeout(void *arg) 202{ 203 struct ieee80211com *ic = arg; 204 struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 205 struct ieee80211_channel *c; 206 int i, oldest, now; 207 208 IEEE80211_LOCK(ic); 209 now = oldest = ticks; 210 for (i = 0; i < ic->ic_nchans; i++) { 211 c = &ic->ic_channels[i]; 212 if (IEEE80211_IS_CHAN_RADAR(c)) { 213 if (time_after_eq(now, dfs->nol_event[i]+NOL_TIMEOUT)) { 214 c->ic_state &= ~IEEE80211_CHANSTATE_RADAR; 215 if (c->ic_state & IEEE80211_CHANSTATE_NORADAR) { 216 /* 217 * NB: do this here so we get only one 218 * msg instead of one for every channel 219 * table entry. 220 */ 221 if_printf(ic->ic_ifp, "radar on channel" 222 " %u (%u MHz) cleared after timeout\n", 223 c->ic_ieee, c->ic_freq); 224 /* notify user space */ 225 c->ic_state &= 226 ~IEEE80211_CHANSTATE_NORADAR; 227 ieee80211_notify_radar(ic, c); 228 } 229 } else if (dfs->nol_event[i] < oldest) 230 oldest = dfs->nol_event[i]; 231 } 232 } 233 if (oldest != now) { 234 /* arrange to process next channel up for a status change */ 235 callout_reset(&dfs->nol_timer, oldest + NOL_TIMEOUT, 236 dfs_timeout, ic); 237 } 238 IEEE80211_UNLOCK(ic); 239} 240 241static void 242announce_radar(struct ifnet *ifp, const struct ieee80211_channel *curchan, 243 const struct ieee80211_channel *newchan) 244{ 245 if (newchan == NULL) 246 if_printf(ifp, "radar detected on channel %u (%u MHz)\n", 247 curchan->ic_ieee, curchan->ic_freq); 248 else 249 if_printf(ifp, "radar detected on channel %u (%u MHz), " 250 "moving to channel %u (%u MHz)\n", 251 curchan->ic_ieee, curchan->ic_freq, 252 newchan->ic_ieee, newchan->ic_freq); 253} 254 255/* 256 * Handle a radar detection event on a channel. The channel is 257 * added to the NOL list and we record the time of the event. 258 * Entries are aged out after NOL_TIMEOUT. If radar was 259 * detected while doing CAC we force a state/channel change. 260 * Otherwise radar triggers a channel switch using the CSA 261 * mechanism (when the channel is the bss channel). 262 */ 263void 264ieee80211_dfs_notify_radar(struct ieee80211com *ic, struct ieee80211_channel *chan) 265{ 266 struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 267 int i, now; 268 269 IEEE80211_LOCK_ASSERT(ic); 270 271 /* 272 * Mark all entries with this frequency. Notify user 273 * space and arrange for notification when the radar 274 * indication is cleared. Then kick the NOL processing 275 * thread if not already running. 276 */ 277 now = ticks; 278 for (i = 0; i < ic->ic_nchans; i++) { 279 struct ieee80211_channel *c = &ic->ic_channels[i]; 280 if (c->ic_freq == chan->ic_freq) { 281 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE; 282 c->ic_state |= IEEE80211_CHANSTATE_RADAR; 283 dfs->nol_event[i] = now; 284 } 285 } 286 ieee80211_notify_radar(ic, chan); 287 chan->ic_state |= IEEE80211_CHANSTATE_NORADAR; 288 if (!callout_pending(&dfs->nol_timer)) 289 callout_reset(&dfs->nol_timer, NOL_TIMEOUT, dfs_timeout, ic); 290 291 /* 292 * If radar is detected on the bss channel while 293 * doing CAC; force a state change by scheduling the 294 * callout to be dispatched asap. Otherwise, if this 295 * event is for the bss channel then we must quiet 296 * traffic and schedule a channel switch. 297 * 298 * Note this allows us to receive notification about 299 * channels other than the bss channel; not sure 300 * that can/will happen but it's simple to support. 301 */ 302 if (chan == ic->ic_bsschan) { 303 /* XXX need a way to defer to user app */ 304 dfs->newchan = ieee80211_dfs_pickchannel(ic); 305 306 announce_radar(ic->ic_ifp, chan, dfs->newchan); 307 308 if (callout_pending(&dfs->cac_timer)) 309 callout_schedule(&dfs->cac_timer, 0); 310 else if (dfs->newchan != NULL) { 311 /* XXX mode 1, switch count 2 */ 312 /* XXX calculate switch count based on max 313 switch time and beacon interval? */ 314 ieee80211_csa_startswitch(ic, dfs->newchan, 1, 2); 315 } else { 316 /* 317 * Spec says to stop all transmissions and 318 * wait on the current channel for an entry 319 * on the NOL to expire. 320 */ 321 /*XXX*/ 322 } 323 } else { 324 /* 325 * Issue rate-limited console msgs. 326 */ 327 if (dfs->lastchan != chan) { 328 dfs->lastchan = chan; 329 dfs->cureps = 0; 330 announce_radar(ic->ic_ifp, chan, NULL); 331 } else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) { 332 announce_radar(ic->ic_ifp, chan, NULL); 333 } 334 } 335} 336 337struct ieee80211_channel * 338ieee80211_dfs_pickchannel(struct ieee80211com *ic) 339{ 340 struct ieee80211_channel *c; 341 int i, flags; 342 uint16_t v; 343 344 /* 345 * Consult the scan cache first. 346 */ 347 flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL; 348 /* 349 * XXX if curchan is HT this will never find a channel 350 * XXX 'cuz we scan only legacy channels 351 */ 352 c = ieee80211_scan_pickchannel(ic, flags); 353 if (c != NULL) 354 return c; 355 /* 356 * No channel found in scan cache; select a compatible 357 * one at random (skipping channels where radar has 358 * been detected). 359 */ 360 get_random_bytes(&v, sizeof(v)); 361 v %= ic->ic_nchans; 362 for (i = v; i < ic->ic_nchans; i++) { 363 c = &ic->ic_channels[i]; 364 if (!IEEE80211_IS_CHAN_RADAR(c) && 365 (c->ic_flags & flags) == flags) 366 return c; 367 } 368 for (i = 0; i < v; i++) { 369 c = &ic->ic_channels[i]; 370 if (!IEEE80211_IS_CHAN_RADAR(c) && 371 (c->ic_flags & flags) == flags) 372 return c; 373 } 374 if_printf(ic->ic_ifp, "HELP, no channel located to switch to!\n"); 375 return NULL; 376} 377