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