1 /*- 2 * Copyright (c) 2002-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 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 /* 30 * IEEE 802.11 scanning support. 31 */ 32 #include "opt_wlan.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/proc.h> 37 #include <sys/kernel.h> 38 #include <sys/condvar.h> 39 40 #include <sys/socket.h> 41 42 #include <net/if.h> 43 #include <net/if_var.h> 44 #include <net/if_media.h> 45 #include <net/ethernet.h> 46 47 #include <net80211/ieee80211_var.h> 48 49 #include <net80211/ieee80211_scan_sw.h> 50 51 #include <net/bpf.h> 52 53 struct scan_state { 54 struct ieee80211_scan_state base; /* public state */ 55 56 u_int ss_iflags; /* flags used internally */ 57 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */ 58 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */ 59 #define ISCAN_CANCEL 0x0004 /* cancel current scan */ 60 #define ISCAN_ABORT 0x0008 /* end the scan immediately */ 61 unsigned long ss_chanmindwell; /* min dwell on curchan */ 62 unsigned long ss_scanend; /* time scan must stop */ 63 u_int ss_duration; /* duration for next scan */ 64 struct task ss_scan_task; /* scan execution */ 65 struct cv ss_scan_cv; /* scan signal */ 66 struct callout ss_scan_timer; /* scan timer */ 67 }; 68 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss) 69 70 /* 71 * Amount of time to go off-channel during a background 72 * scan. This value should be large enough to catch most 73 * ap's but short enough that we can return on-channel 74 * before our listen interval expires. 75 * 76 * XXX tunable 77 * XXX check against configured listen interval 78 */ 79 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150) 80 81 /* 82 * Roaming-related defaults. RSSI thresholds are as returned by the 83 * driver (.5dBm). Transmit rate thresholds are IEEE rate codes (i.e 84 * .5M units) or MCS. 85 */ 86 /* rssi thresholds */ 87 #define ROAM_RSSI_11A_DEFAULT 14 /* 11a bss */ 88 #define ROAM_RSSI_11B_DEFAULT 14 /* 11b bss */ 89 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* 11b-only bss */ 90 /* transmit rate thresholds */ 91 #define ROAM_RATE_11A_DEFAULT 2*12 /* 11a bss */ 92 #define ROAM_RATE_11B_DEFAULT 2*5 /* 11b bss */ 93 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* 11b-only bss */ 94 #define ROAM_RATE_HALF_DEFAULT 2*6 /* half-width 11a/g bss */ 95 #define ROAM_RATE_QUARTER_DEFAULT 2*3 /* quarter-width 11a/g bss */ 96 #define ROAM_MCS_11N_DEFAULT (1 | IEEE80211_RATE_MCS) /* 11n bss */ 97 98 static void scan_curchan(struct ieee80211_scan_state *, unsigned long); 99 static void scan_mindwell(struct ieee80211_scan_state *); 100 static void scan_signal(void *); 101 static void scan_task(void *, int); 102 103 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state"); 104 105 static void 106 ieee80211_swscan_detach(struct ieee80211com *ic) 107 { 108 struct ieee80211_scan_state *ss = ic->ic_scan; 109 110 if (ss != NULL) { 111 IEEE80211_LOCK(ic); 112 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT; 113 scan_signal(ss); 114 IEEE80211_UNLOCK(ic); 115 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 116 callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer); 117 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, 118 ("scan still running")); 119 120 /* 121 * For now, do the ss_ops detach here rather 122 * than ieee80211_scan_detach(). 123 * 124 * I'll figure out how to cleanly split things up 125 * at a later date. 126 */ 127 if (ss->ss_ops != NULL) { 128 ss->ss_ops->scan_detach(ss); 129 ss->ss_ops = NULL; 130 } 131 ic->ic_scan = NULL; 132 IEEE80211_FREE(SCAN_PRIVATE(ss), M_80211_SCAN); 133 } 134 } 135 136 static void 137 ieee80211_swscan_vattach(struct ieee80211vap *vap) 138 { 139 /* nothing to do for now */ 140 /* 141 * TODO: all of the vap scan calls should be methods! 142 */ 143 144 } 145 146 static void 147 ieee80211_swscan_vdetach(struct ieee80211vap *vap) 148 { 149 struct ieee80211com *ic = vap->iv_ic; 150 struct ieee80211_scan_state *ss; 151 152 IEEE80211_LOCK_ASSERT(ic); 153 ss = ic->ic_scan; 154 if (ss != NULL && ss->ss_vap == vap) { 155 if (ic->ic_flags & IEEE80211_F_SCAN) { 156 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT; 157 scan_signal(ss); 158 } 159 } 160 } 161 162 static void 163 ieee80211_swscan_set_scan_duration(struct ieee80211vap *vap, u_int duration) 164 { 165 struct ieee80211com *ic = vap->iv_ic; 166 struct ieee80211_scan_state *ss = ic->ic_scan; 167 168 IEEE80211_LOCK_ASSERT(ic); 169 170 /* NB: flush frames rx'd before 1st channel change */ 171 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD; 172 SCAN_PRIVATE(ss)->ss_duration = duration; 173 } 174 175 /* 176 * Start a scan unless one is already going. 177 */ 178 static int 179 ieee80211_swscan_start_scan_locked(const struct ieee80211_scanner *scan, 180 struct ieee80211vap *vap, int flags, u_int duration, 181 u_int mindwell, u_int maxdwell, 182 u_int nssid, const struct ieee80211_scan_ssid ssids[]) 183 { 184 struct ieee80211com *ic = vap->iv_ic; 185 struct ieee80211_scan_state *ss = ic->ic_scan; 186 187 IEEE80211_LOCK_ASSERT(ic); 188 189 if (ic->ic_flags & IEEE80211_F_CSAPENDING) { 190 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 191 "%s: scan inhibited by pending channel change\n", __func__); 192 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 193 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 194 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n" 195 , __func__ 196 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive" 197 , duration, mindwell, maxdwell 198 , ieee80211_phymode_name[vap->iv_des_mode] 199 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append" 200 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : "" 201 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : "" 202 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : "" 203 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : "" 204 , flags & IEEE80211_SCAN_ONCE ? ", once" : "" 205 ); 206 207 ieee80211_scan_update_locked(vap, scan); 208 if (ss->ss_ops != NULL) { 209 if ((flags & IEEE80211_SCAN_NOSSID) == 0) 210 ieee80211_scan_copy_ssid(vap, ss, nssid, ssids); 211 212 /* NB: top 4 bits for internal use */ 213 ss->ss_flags = flags & 0xfff; 214 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 215 vap->iv_stats.is_scan_active++; 216 else 217 vap->iv_stats.is_scan_passive++; 218 if (flags & IEEE80211_SCAN_FLUSH) 219 ss->ss_ops->scan_flush(ss); 220 if (flags & IEEE80211_SCAN_BGSCAN) 221 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN; 222 223 /* Set duration for this particular scan */ 224 ieee80211_swscan_set_scan_duration(vap, duration); 225 226 ss->ss_next = 0; 227 ss->ss_mindwell = mindwell; 228 ss->ss_maxdwell = maxdwell; 229 /* NB: scan_start must be before the scan runtask */ 230 ss->ss_ops->scan_start(ss, vap); 231 #ifdef IEEE80211_DEBUG 232 if (ieee80211_msg_scan(vap)) 233 ieee80211_scan_dump(ss); 234 #endif /* IEEE80211_DEBUG */ 235 ic->ic_flags |= IEEE80211_F_SCAN; 236 237 /* Start scan task */ 238 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 239 } 240 return 1; 241 } else { 242 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 243 "%s: %s scan already in progress\n", __func__, 244 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"); 245 } 246 return 0; 247 } 248 249 250 /* 251 * Start a scan unless one is already going. 252 * 253 * Called without the comlock held; grab the comlock as appropriate. 254 */ 255 static int 256 ieee80211_swscan_start_scan(const struct ieee80211_scanner *scan, 257 struct ieee80211vap *vap, int flags, 258 u_int duration, u_int mindwell, u_int maxdwell, 259 u_int nssid, const struct ieee80211_scan_ssid ssids[]) 260 { 261 struct ieee80211com *ic = vap->iv_ic; 262 int result; 263 264 IEEE80211_UNLOCK_ASSERT(ic); 265 266 IEEE80211_LOCK(ic); 267 result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration, 268 mindwell, maxdwell, nssid, ssids); 269 IEEE80211_UNLOCK(ic); 270 271 return result; 272 } 273 274 /* 275 * Check the scan cache for an ap/channel to use; if that 276 * fails then kick off a new scan. 277 * 278 * Called with the comlock held. 279 * 280 * XXX TODO: split out! 281 */ 282 static int 283 ieee80211_swscan_check_scan(const struct ieee80211_scanner *scan, 284 struct ieee80211vap *vap, int flags, 285 u_int duration, u_int mindwell, u_int maxdwell, 286 u_int nssid, const struct ieee80211_scan_ssid ssids[]) 287 { 288 struct ieee80211com *ic = vap->iv_ic; 289 struct ieee80211_scan_state *ss = ic->ic_scan; 290 int result; 291 292 IEEE80211_LOCK_ASSERT(ic); 293 294 if (ss->ss_ops != NULL) { 295 /* XXX verify ss_ops matches vap->iv_opmode */ 296 if ((flags & IEEE80211_SCAN_NOSSID) == 0) { 297 /* 298 * Update the ssid list and mark flags so if 299 * we call start_scan it doesn't duplicate work. 300 */ 301 ieee80211_scan_copy_ssid(vap, ss, nssid, ssids); 302 flags |= IEEE80211_SCAN_NOSSID; 303 } 304 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 && 305 (flags & IEEE80211_SCAN_FLUSH) == 0 && 306 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) { 307 /* 308 * We're not currently scanning and the cache is 309 * deemed hot enough to consult. Lock out others 310 * by marking IEEE80211_F_SCAN while we decide if 311 * something is already in the scan cache we can 312 * use. Also discard any frames that might come 313 * in while temporarily marked as scanning. 314 */ 315 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD; 316 ic->ic_flags |= IEEE80211_F_SCAN; 317 318 /* NB: need to use supplied flags in check */ 319 ss->ss_flags = flags & 0xff; 320 result = ss->ss_ops->scan_end(ss, vap); 321 322 ic->ic_flags &= ~IEEE80211_F_SCAN; 323 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD; 324 if (result) { 325 ieee80211_notify_scan_done(vap); 326 return 1; 327 } 328 } 329 } 330 result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration, 331 mindwell, maxdwell, nssid, ssids); 332 333 return result; 334 } 335 336 /* 337 * Restart a previous scan. If the previous scan completed 338 * then we start again using the existing channel list. 339 */ 340 static int 341 ieee80211_swscan_bg_scan(const struct ieee80211_scanner *scan, 342 struct ieee80211vap *vap, int flags) 343 { 344 struct ieee80211com *ic = vap->iv_ic; 345 struct ieee80211_scan_state *ss = ic->ic_scan; 346 347 /* XXX assert unlocked? */ 348 // IEEE80211_UNLOCK_ASSERT(ic); 349 350 IEEE80211_LOCK(ic); 351 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 352 u_int duration; 353 /* 354 * Go off-channel for a fixed interval that is large 355 * enough to catch most ap's but short enough that 356 * we can return on-channel before our listen interval 357 * expires. 358 */ 359 duration = IEEE80211_SCAN_OFFCHANNEL; 360 361 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 362 "%s: %s scan, ticks %u duration %lu\n", __func__, 363 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive", 364 ticks, duration); 365 366 ieee80211_scan_update_locked(vap, scan); 367 if (ss->ss_ops != NULL) { 368 ss->ss_vap = vap; 369 /* 370 * A background scan does not select a new sta; it 371 * just refreshes the scan cache. Also, indicate 372 * the scan logic should follow the beacon schedule: 373 * we go off-channel and scan for a while, then 374 * return to the bss channel to receive a beacon, 375 * then go off-channel again. All during this time 376 * we notify the ap we're in power save mode. When 377 * the scan is complete we leave power save mode. 378 * If any beacon indicates there are frames pending 379 * for us then we drop out of power save mode 380 * (and background scan) automatically by way of the 381 * usual sta power save logic. 382 */ 383 ss->ss_flags |= IEEE80211_SCAN_NOPICK 384 | IEEE80211_SCAN_BGSCAN 385 | flags 386 ; 387 /* if previous scan completed, restart */ 388 if (ss->ss_next >= ss->ss_last) { 389 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 390 vap->iv_stats.is_scan_active++; 391 else 392 vap->iv_stats.is_scan_passive++; 393 /* 394 * NB: beware of the scan cache being flushed; 395 * if the channel list is empty use the 396 * scan_start method to populate it. 397 */ 398 ss->ss_next = 0; 399 if (ss->ss_last != 0) 400 ss->ss_ops->scan_restart(ss, vap); 401 else { 402 ss->ss_ops->scan_start(ss, vap); 403 #ifdef IEEE80211_DEBUG 404 if (ieee80211_msg_scan(vap)) 405 ieee80211_scan_dump(ss); 406 #endif /* IEEE80211_DEBUG */ 407 } 408 } 409 ieee80211_swscan_set_scan_duration(vap, duration); 410 ss->ss_maxdwell = duration; 411 ic->ic_flags |= IEEE80211_F_SCAN; 412 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN; 413 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 414 } else { 415 /* XXX msg+stat */ 416 } 417 } else { 418 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 419 "%s: %s scan already in progress\n", __func__, 420 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"); 421 } 422 IEEE80211_UNLOCK(ic); 423 424 /* NB: racey, does it matter? */ 425 return (ic->ic_flags & IEEE80211_F_SCAN); 426 } 427 428 /* 429 * Cancel any scan currently going on for the specified vap. 430 */ 431 static void 432 ieee80211_swscan_cancel_scan(struct ieee80211vap *vap) 433 { 434 struct ieee80211com *ic = vap->iv_ic; 435 struct ieee80211_scan_state *ss = ic->ic_scan; 436 437 IEEE80211_LOCK(ic); 438 if ((ic->ic_flags & IEEE80211_F_SCAN) && 439 ss->ss_vap == vap && 440 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) { 441 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 442 "%s: cancel %s scan\n", __func__, 443 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? 444 "active" : "passive"); 445 446 /* clear bg scan NOPICK and mark cancel request */ 447 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 448 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL; 449 /* wake up the scan task */ 450 scan_signal(ss); 451 } else { 452 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 453 "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n", 454 __func__, 455 !! (ic->ic_flags & IEEE80211_F_SCAN), 456 (ss->ss_vap == vap ? "match" : "nomatch"), 457 !! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL)); 458 } 459 IEEE80211_UNLOCK(ic); 460 } 461 462 /* 463 * Cancel any scan currently going on. 464 */ 465 static void 466 ieee80211_swscan_cancel_anyscan(struct ieee80211vap *vap) 467 { 468 struct ieee80211com *ic = vap->iv_ic; 469 struct ieee80211_scan_state *ss = ic->ic_scan; 470 471 IEEE80211_LOCK(ic); 472 if ((ic->ic_flags & IEEE80211_F_SCAN) && 473 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) { 474 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 475 "%s: cancel %s scan\n", __func__, 476 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? 477 "active" : "passive"); 478 479 /* clear bg scan NOPICK and mark cancel request */ 480 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 481 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL; 482 /* wake up the scan task */ 483 scan_signal(ss); 484 } else { 485 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 486 "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n", 487 __func__, 488 !! (ic->ic_flags & IEEE80211_F_SCAN), 489 (ss->ss_vap == vap ? "match" : "nomatch"), 490 !! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL)); 491 } 492 IEEE80211_UNLOCK(ic); 493 } 494 495 /* 496 * Public access to scan_next for drivers that manage 497 * scanning themselves (e.g. for firmware-based devices). 498 */ 499 static void 500 ieee80211_swscan_scan_next(struct ieee80211vap *vap) 501 { 502 struct ieee80211com *ic = vap->iv_ic; 503 struct ieee80211_scan_state *ss = ic->ic_scan; 504 505 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__); 506 507 /* wake up the scan task */ 508 IEEE80211_LOCK(ic); 509 scan_signal(ss); 510 IEEE80211_UNLOCK(ic); 511 } 512 513 /* 514 * Public access to scan_next for drivers that are not able to scan single 515 * channels (e.g. for firmware-based devices). 516 */ 517 static void 518 ieee80211_swscan_scan_done(struct ieee80211vap *vap) 519 { 520 struct ieee80211com *ic = vap->iv_ic; 521 struct ieee80211_scan_state *ss; 522 523 IEEE80211_LOCK_ASSERT(ic); 524 525 ss = ic->ic_scan; 526 scan_signal(ss); 527 } 528 529 /* 530 * Probe the curent channel, if allowed, while scanning. 531 * If the channel is not marked passive-only then send 532 * a probe request immediately. Otherwise mark state and 533 * listen for beacons on the channel; if we receive something 534 * then we'll transmit a probe request. 535 */ 536 static void 537 ieee80211_swscan_probe_curchan(struct ieee80211vap *vap, int force) 538 { 539 struct ieee80211com *ic = vap->iv_ic; 540 struct ieee80211_scan_state *ss = ic->ic_scan; 541 struct ifnet *ifp = vap->iv_ifp; 542 int i; 543 544 /* 545 * Send directed probe requests followed by any 546 * broadcast probe request. 547 * XXX remove dependence on ic/vap->iv_bss 548 */ 549 for (i = 0; i < ss->ss_nssid; i++) 550 ieee80211_send_probereq(vap->iv_bss, 551 vap->iv_myaddr, ifp->if_broadcastaddr, 552 ifp->if_broadcastaddr, 553 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len); 554 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0) 555 ieee80211_send_probereq(vap->iv_bss, 556 vap->iv_myaddr, ifp->if_broadcastaddr, 557 ifp->if_broadcastaddr, 558 "", 0); 559 } 560 561 /* 562 * Scan curchan. If this is an active scan and the channel 563 * is not marked passive then send probe request frame(s). 564 * Arrange for the channel change after maxdwell ticks. 565 */ 566 static void 567 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 568 { 569 struct ieee80211vap *vap = ss->ss_vap; 570 571 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 572 "%s: calling; maxdwell=%lu\n", 573 __func__, 574 maxdwell); 575 IEEE80211_LOCK(vap->iv_ic); 576 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 577 ieee80211_probe_curchan(vap, 0); 578 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 579 maxdwell, scan_signal, ss); 580 IEEE80211_UNLOCK(vap->iv_ic); 581 } 582 583 static void 584 scan_signal(void *arg) 585 { 586 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg; 587 588 IEEE80211_LOCK_ASSERT(ss->ss_ic); 589 cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv); 590 } 591 592 /* 593 * Handle mindwell requirements completed; initiate a channel 594 * change to the next channel asap. 595 */ 596 static void 597 scan_mindwell(struct ieee80211_scan_state *ss) 598 { 599 struct ieee80211com *ic = ss->ss_ic; 600 601 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__); 602 603 IEEE80211_LOCK(ic); 604 scan_signal(ss); 605 IEEE80211_UNLOCK(ic); 606 } 607 608 static void 609 scan_task(void *arg, int pending) 610 { 611 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD) 612 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg; 613 struct ieee80211vap *vap = ss->ss_vap; 614 struct ieee80211com *ic = ss->ss_ic; 615 struct ieee80211_channel *chan; 616 unsigned long maxdwell, scanend; 617 int scandone = 0; 618 619 IEEE80211_LOCK(ic); 620 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 || 621 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) { 622 /* Cancelled before we started */ 623 goto done; 624 } 625 626 if (ss->ss_next == ss->ss_last) { 627 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 628 "%s: no channels to scan\n", __func__); 629 scandone = 1; 630 goto done; 631 } 632 633 if (vap->iv_opmode == IEEE80211_M_STA && 634 vap->iv_state == IEEE80211_S_RUN) { 635 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) { 636 /* Enable station power save mode */ 637 vap->iv_sta_ps(vap, 1); 638 /* 639 * Use an 1ms delay so the null data frame has a chance 640 * to go out. 641 * XXX Should use M_TXCB mechanism to eliminate this. 642 */ 643 cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv, 644 IEEE80211_LOCK_OBJ(ic), hz / 1000); 645 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) 646 goto done; 647 } 648 } 649 650 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration; 651 652 /* XXX scan state can change! Re-validate scan state! */ 653 654 IEEE80211_UNLOCK(ic); 655 ic->ic_scan_start(ic); /* notify driver */ 656 IEEE80211_LOCK(ic); 657 658 for (;;) { 659 660 scandone = (ss->ss_next >= ss->ss_last) || 661 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0; 662 663 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 664 "%s: loop start; scandone=%d\n", 665 __func__, 666 scandone); 667 668 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) || 669 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) || 670 time_after(ticks + ss->ss_mindwell, scanend)) 671 break; 672 673 chan = ss->ss_chans[ss->ss_next++]; 674 675 /* 676 * Watch for truncation due to the scan end time. 677 */ 678 if (time_after(ticks + ss->ss_maxdwell, scanend)) 679 maxdwell = scanend - ticks; 680 else 681 maxdwell = ss->ss_maxdwell; 682 683 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 684 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n", 685 __func__, 686 ieee80211_chan2ieee(ic, ic->ic_curchan), 687 ieee80211_channel_type_char(ic->ic_curchan), 688 ieee80211_chan2ieee(ic, chan), 689 ieee80211_channel_type_char(chan), 690 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) && 691 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ? 692 "active" : "passive", 693 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell)); 694 695 /* 696 * Potentially change channel and phy mode. 697 */ 698 ic->ic_curchan = chan; 699 ic->ic_rt = ieee80211_get_ratetable(chan); 700 IEEE80211_UNLOCK(ic); 701 /* 702 * Perform the channel change and scan unlocked so the driver 703 * may sleep. Once set_channel returns the hardware has 704 * completed the channel change. 705 */ 706 ic->ic_set_channel(ic); 707 ieee80211_radiotap_chan_change(ic); 708 709 /* 710 * Scan curchan. Drivers for "intelligent hardware" 711 * override ic_scan_curchan to tell the device to do 712 * the work. Otherwise we manage the work outselves; 713 * sending a probe request (as needed), and arming the 714 * timeout to switch channels after maxdwell ticks. 715 * 716 * scan_curchan should only pause for the time required to 717 * prepare/initiate the hardware for the scan (if at all), the 718 * below condvar is used to sleep for the channels dwell time 719 * and allows it to be signalled for abort. 720 */ 721 ic->ic_scan_curchan(ss, maxdwell); 722 IEEE80211_LOCK(ic); 723 724 /* XXX scan state can change! Re-validate scan state! */ 725 726 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell; 727 /* clear mindwell lock and initial channel change flush */ 728 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP; 729 730 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT))) 731 continue; 732 733 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: waiting\n", __func__); 734 /* Wait to be signalled to scan the next channel */ 735 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic)); 736 } 737 738 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: out\n", __func__); 739 740 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) 741 goto done; 742 743 IEEE80211_UNLOCK(ic); 744 ic->ic_scan_end(ic); /* notify driver */ 745 IEEE80211_LOCK(ic); 746 /* XXX scan state can change! Re-validate scan state! */ 747 748 /* 749 * Since a cancellation may have occured during one of the 750 * driver calls (whilst unlocked), update scandone. 751 */ 752 if (scandone == 0 && 753 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) { 754 /* XXX printf? */ 755 if_printf(vap->iv_ifp, 756 "%s: OOPS! scan cancelled during driver call (1)!\n", 757 __func__); 758 scandone = 1; 759 } 760 761 /* 762 * Record scan complete time. Note that we also do 763 * this when canceled so any background scan will 764 * not be restarted for a while. 765 */ 766 if (scandone) 767 ic->ic_lastscan = ticks; 768 /* return to the bss channel */ 769 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 770 ic->ic_curchan != ic->ic_bsschan) { 771 ieee80211_setupcurchan(ic, ic->ic_bsschan); 772 IEEE80211_UNLOCK(ic); 773 ic->ic_set_channel(ic); 774 ieee80211_radiotap_chan_change(ic); 775 IEEE80211_LOCK(ic); 776 } 777 /* clear internal flags and any indication of a pick */ 778 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP; 779 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK; 780 781 /* 782 * If not canceled and scan completed, do post-processing. 783 * If the callback function returns 0, then it wants to 784 * continue/restart scanning. Unfortunately we needed to 785 * notify the driver to end the scan above to avoid having 786 * rx frames alter the scan candidate list. 787 */ 788 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 && 789 !ss->ss_ops->scan_end(ss, vap) && 790 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 && 791 time_before(ticks + ss->ss_mindwell, scanend)) { 792 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 793 "%s: done, restart " 794 "[ticks %u, dwell min %lu scanend %lu]\n", 795 __func__, 796 ticks, ss->ss_mindwell, scanend); 797 ss->ss_next = 0; /* reset to begining */ 798 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 799 vap->iv_stats.is_scan_active++; 800 else 801 vap->iv_stats.is_scan_passive++; 802 803 ss->ss_ops->scan_restart(ss, vap); /* XXX? */ 804 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 805 IEEE80211_UNLOCK(ic); 806 return; 807 } 808 809 /* past here, scandone is ``true'' if not in bg mode */ 810 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0) 811 scandone = 1; 812 813 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 814 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n", 815 __func__, scandone ? "done" : "stopped", 816 ticks, ss->ss_mindwell, scanend); 817 818 /* 819 * Since a cancellation may have occured during one of the 820 * driver calls (whilst unlocked), update scandone. 821 */ 822 if (scandone == 0 && 823 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) { 824 /* XXX printf? */ 825 if_printf(vap->iv_ifp, 826 "%s: OOPS! scan cancelled during driver call (2)!\n", 827 __func__); 828 scandone = 1; 829 } 830 831 /* 832 * Clear the SCAN bit first in case frames are 833 * pending on the station power save queue. If 834 * we defer this then the dispatch of the frames 835 * may generate a request to cancel scanning. 836 */ 837 done: 838 ic->ic_flags &= ~IEEE80211_F_SCAN; 839 /* 840 * Drop out of power save mode when a scan has 841 * completed. If this scan was prematurely terminated 842 * because it is a background scan then don't notify 843 * the ap; we'll either return to scanning after we 844 * receive the beacon frame or we'll drop out of power 845 * save mode because the beacon indicates we have frames 846 * waiting for us. 847 */ 848 if (scandone) { 849 vap->iv_sta_ps(vap, 0); 850 if (ss->ss_next >= ss->ss_last) { 851 ieee80211_notify_scan_done(vap); 852 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN; 853 } 854 } 855 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT); 856 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST); 857 IEEE80211_UNLOCK(ic); 858 #undef ISCAN_REP 859 } 860 861 /* 862 * Process a beacon or probe response frame. 863 */ 864 static void 865 ieee80211_swscan_add_scan(struct ieee80211vap *vap, 866 struct ieee80211_channel *curchan, 867 const struct ieee80211_scanparams *sp, 868 const struct ieee80211_frame *wh, 869 int subtype, int rssi, int noise) 870 { 871 struct ieee80211com *ic = vap->iv_ic; 872 struct ieee80211_scan_state *ss = ic->ic_scan; 873 874 /* XXX locking */ 875 /* 876 * Frames received during startup are discarded to avoid 877 * using scan state setup on the initial entry to the timer 878 * callback. This can occur because the device may enable 879 * rx prior to our doing the initial channel change in the 880 * timer routine. 881 */ 882 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD) 883 return; 884 #ifdef IEEE80211_DEBUG 885 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN)) 886 ieee80211_scan_dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi); 887 #endif 888 if (ss->ss_ops != NULL && 889 ss->ss_ops->scan_add(ss, curchan, sp, wh, subtype, rssi, noise)) { 890 /* 891 * If we've reached the min dwell time terminate 892 * the timer so we'll switch to the next channel. 893 */ 894 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 && 895 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) { 896 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 897 "%s: chan %3d%c min dwell met (%u > %lu)\n", 898 __func__, 899 ieee80211_chan2ieee(ic, ic->ic_curchan), 900 ieee80211_channel_type_char(ic->ic_curchan), 901 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell); 902 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL; 903 /* 904 * NB: trigger at next clock tick or wait for the 905 * hardware. 906 */ 907 ic->ic_scan_mindwell(ss); 908 } 909 } 910 } 911 912 static struct ieee80211_scan_methods swscan_methods = { 913 .sc_attach = ieee80211_swscan_attach, 914 .sc_detach = ieee80211_swscan_detach, 915 .sc_vattach = ieee80211_swscan_vattach, 916 .sc_vdetach = ieee80211_swscan_vdetach, 917 .sc_set_scan_duration = ieee80211_swscan_set_scan_duration, 918 .sc_start_scan = ieee80211_swscan_start_scan, 919 .sc_check_scan = ieee80211_swscan_check_scan, 920 .sc_bg_scan = ieee80211_swscan_bg_scan, 921 .sc_cancel_scan = ieee80211_swscan_cancel_scan, 922 .sc_cancel_anyscan = ieee80211_swscan_cancel_anyscan, 923 .sc_scan_next = ieee80211_swscan_scan_next, 924 .sc_scan_done = ieee80211_swscan_scan_done, 925 .sc_scan_probe_curchan = ieee80211_swscan_probe_curchan, 926 .sc_add_scan = ieee80211_swscan_add_scan 927 }; 928 929 /* 930 * Default scan attach method. 931 */ 932 void 933 ieee80211_swscan_attach(struct ieee80211com *ic) 934 { 935 struct scan_state *ss; 936 937 /* 938 * Setup the default methods 939 */ 940 ic->ic_scan_methods = &swscan_methods; 941 942 /* Allocate initial scan state */ 943 ss = (struct scan_state *) IEEE80211_MALLOC(sizeof(struct scan_state), 944 M_80211_SCAN, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 945 if (ss == NULL) { 946 ic->ic_scan = NULL; 947 return; 948 } 949 callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0); 950 cv_init(&ss->ss_scan_cv, "scan"); 951 TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss); 952 953 ic->ic_scan = &ss->base; 954 ss->base.ss_ic = ic; 955 956 ic->ic_scan_curchan = scan_curchan; 957 ic->ic_scan_mindwell = scan_mindwell; 958 } 959