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