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 */ 27 28 /* 29 * IEEE 802.11 DFS/Radar support. 30 */ 31 #include "opt_inet.h" 32 #include "opt_wlan.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/mbuf.h> 37 #include <sys/malloc.h> 38 #include <sys/kernel.h> 39 40 #include <sys/socket.h> 41 #include <sys/sockio.h> 42 #include <sys/endian.h> 43 #include <sys/errno.h> 44 #include <sys/proc.h> 45 #include <sys/sysctl.h> 46 47 #include <net/if.h> 48 #include <net/if_media.h> 49 #include <net/route.h> 50 51 #include <netproto/802_11/ieee80211_var.h> 52 53 MALLOC_DEFINE(M_80211_DFS, "80211dfs", "802.11 DFS state"); 54 55 static int ieee80211_nol_timeout = 30*60; /* 30 minutes */ 56 SYSCTL_INT(_net_wlan, OID_AUTO, nol_timeout, CTLFLAG_RW, 57 &ieee80211_nol_timeout, 0, "NOL timeout (secs)"); 58 #define NOL_TIMEOUT msecs_to_ticks(ieee80211_nol_timeout*1000) 59 60 static int ieee80211_cac_timeout = 60; /* 60 seconds */ 61 SYSCTL_INT(_net_wlan, OID_AUTO, cac_timeout, CTLFLAG_RW, 62 &ieee80211_cac_timeout, 0, "CAC timeout (secs)"); 63 #define CAC_TIMEOUT msecs_to_ticks(ieee80211_cac_timeout*1000) 64 65 void 66 ieee80211_dfs_attach(struct ieee80211com *ic) 67 { 68 struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 69 70 callout_init(&dfs->nol_timer); 71 callout_init(&dfs->cac_timer); 72 } 73 74 void 75 ieee80211_dfs_detach(struct ieee80211com *ic) 76 { 77 /* NB: we assume no locking is needed */ 78 ieee80211_dfs_reset(ic); 79 } 80 81 void 82 ieee80211_dfs_reset(struct ieee80211com *ic) 83 { 84 struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 85 int i; 86 87 /* NB: we assume no locking is needed */ 88 /* NB: cac_timer should be cleared by the state machine */ 89 callout_stop(&dfs->nol_timer); 90 for (i = 0; i < ic->ic_nchans; i++) 91 ic->ic_channels[i].ic_state = 0; 92 dfs->lastchan = NULL; 93 } 94 95 static void 96 cac_timeout_callout(void *arg) 97 { 98 struct ieee80211vap *vap = arg; 99 struct ieee80211com *ic; 100 struct ieee80211_dfs_state *dfs; 101 int i; 102 103 wlan_serialize_enter(); 104 ic = vap->iv_ic; 105 dfs = &ic->ic_dfs; 106 if (vap->iv_state != IEEE80211_S_CAC) { /* NB: just in case */ 107 wlan_serialize_exit(); 108 return; 109 } 110 /* 111 * When radar is detected during a CAC we are woken 112 * up prematurely to switch to a new channel. 113 * Check the channel to decide how to act. 114 */ 115 if (IEEE80211_IS_CHAN_RADAR(ic->ic_curchan)) { 116 ieee80211_notify_cac(ic, ic->ic_curchan, 117 IEEE80211_NOTIFY_CAC_RADAR); 118 119 if_printf(vap->iv_ifp, 120 "CAC timer on channel %u (%u MHz) stopped due to radar\n", 121 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 122 123 /* XXX clobbers any existing desired channel */ 124 /* NB: dfs->newchan may be NULL, that's ok */ 125 vap->iv_des_chan = dfs->newchan; 126 /* XXX recursive lock need ieee80211_new_state_locked */ 127 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 128 } else { 129 if_printf(vap->iv_ifp, 130 "CAC timer on channel %u (%u MHz) expired; " 131 "no radar detected\n", 132 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 133 /* 134 * Mark all channels with the current frequency 135 * as having completed CAC; this keeps us from 136 * doing it again until we change channels. 137 */ 138 for (i = 0; i < ic->ic_nchans; i++) { 139 struct ieee80211_channel *c = &ic->ic_channels[i]; 140 if (c->ic_freq == ic->ic_curchan->ic_freq) 141 c->ic_state |= IEEE80211_CHANSTATE_CACDONE; 142 } 143 ieee80211_notify_cac(ic, ic->ic_curchan, 144 IEEE80211_NOTIFY_CAC_EXPIRE); 145 ieee80211_cac_completeswitch(vap); 146 } 147 wlan_serialize_exit(); 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 */ 155 void 156 ieee80211_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 callout_reset(&dfs->cac_timer, CAC_TIMEOUT, cac_timeout_callout, vap); 162 if_printf(vap->iv_ifp, "start %d second CAC timer on channel %u (%u MHz)\n", 163 ticks_to_secs(CAC_TIMEOUT), 164 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 165 ieee80211_notify_cac(ic, ic->ic_curchan, IEEE80211_NOTIFY_CAC_START); 166 } 167 168 /* 169 * Clear the CAC timer. 170 */ 171 void 172 ieee80211_dfs_cac_stop(struct ieee80211vap *vap) 173 { 174 struct ieee80211com *ic = vap->iv_ic; 175 struct ieee80211_dfs_state *dfs = &ic->ic_dfs; 176 177 /* NB: racey but not important */ 178 if (callout_pending(&dfs->cac_timer)) { 179 if_printf(vap->iv_ifp, "stop CAC timer on channel %u (%u MHz)\n", 180 ic->ic_curchan->ic_ieee, ic->ic_curchan->ic_freq); 181 ieee80211_notify_cac(ic, ic->ic_curchan, 182 IEEE80211_NOTIFY_CAC_STOP); 183 } 184 callout_stop(&dfs->cac_timer); 185 } 186 187 void 188 ieee80211_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 200 static void 201 dfs_timeout_callout(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 wlan_serialize_enter(); 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 - now, 236 dfs_timeout_callout, ic); 237 } 238 wlan_serialize_exit(); 239 } 240 241 static void 242 announce_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 */ 263 void 264 ieee80211_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 /* 270 * Mark all entries with this frequency. Notify user 271 * space and arrange for notification when the radar 272 * indication is cleared. Then kick the NOL processing 273 * thread if not already running. 274 */ 275 now = ticks; 276 for (i = 0; i < ic->ic_nchans; i++) { 277 struct ieee80211_channel *c = &ic->ic_channels[i]; 278 if (c->ic_freq == chan->ic_freq) { 279 c->ic_state &= ~IEEE80211_CHANSTATE_CACDONE; 280 c->ic_state |= IEEE80211_CHANSTATE_RADAR; 281 dfs->nol_event[i] = now; 282 } 283 } 284 ieee80211_notify_radar(ic, chan); 285 chan->ic_state |= IEEE80211_CHANSTATE_NORADAR; 286 if (!callout_pending(&dfs->nol_timer)) { 287 callout_reset(&dfs->nol_timer, NOL_TIMEOUT, 288 dfs_timeout_callout, ic); 289 } 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 #ifdef notyet 309 if (callout_pending(&dfs->cac_timer)) { 310 callout_reset(&dfs->cac_timer, 0, 311 cac_timeout_callout, vap); 312 } 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 } 326 #endif 327 } else { 328 /* 329 * Issue rate-limited console msgs. 330 */ 331 if (dfs->lastchan != chan) { 332 dfs->lastchan = chan; 333 dfs->cureps = 0; 334 announce_radar(ic->ic_ifp, chan, NULL); 335 } else if (ppsratecheck(&dfs->lastevent, &dfs->cureps, 1)) { 336 announce_radar(ic->ic_ifp, chan, NULL); 337 } 338 } 339 } 340 341 struct ieee80211_channel * 342 ieee80211_dfs_pickchannel(struct ieee80211com *ic) 343 { 344 struct ieee80211_channel *c; 345 int i, flags; 346 uint16_t v; 347 348 /* 349 * Consult the scan cache first. 350 */ 351 flags = ic->ic_curchan->ic_flags & IEEE80211_CHAN_ALL; 352 /* 353 * XXX if curchan is HT this will never find a channel 354 * XXX 'cuz we scan only legacy channels 355 */ 356 c = ieee80211_scan_pickchannel(ic, flags); 357 if (c != NULL) 358 return c; 359 /* 360 * No channel found in scan cache; select a compatible 361 * one at random (skipping channels where radar has 362 * been detected). 363 */ 364 get_random_bytes(&v, sizeof(v)); 365 v %= ic->ic_nchans; 366 for (i = v; i < ic->ic_nchans; i++) { 367 c = &ic->ic_channels[i]; 368 if (!IEEE80211_IS_CHAN_RADAR(c) && 369 (c->ic_flags & flags) == flags) 370 return c; 371 } 372 for (i = 0; i < v; i++) { 373 c = &ic->ic_channels[i]; 374 if (!IEEE80211_IS_CHAN_RADAR(c) && 375 (c->ic_flags & flags) == flags) 376 return c; 377 } 378 if_printf(ic->ic_ifp, "HELP, no channel located to switch to!\n"); 379 return NULL; 380 } 381