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_media.h> 44 #include <net/ethernet.h> 45 46 #include <net80211/ieee80211_var.h> 47 48 #include <net/bpf.h> 49 50 struct scan_state { 51 struct ieee80211_scan_state base; /* public state */ 52 53 u_int ss_iflags; /* flags used internally */ 54 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */ 55 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */ 56 #define ISCAN_CANCEL 0x0004 /* cancel current scan */ 57 #define ISCAN_ABORT 0x0008 /* end the scan immediately */ 58 unsigned long ss_chanmindwell; /* min dwell on curchan */ 59 unsigned long ss_scanend; /* time scan must stop */ 60 u_int ss_duration; /* duration for next scan */ 61 struct task ss_scan_task; /* scan execution */ 62 struct cv ss_scan_cv; /* scan signal */ 63 struct callout ss_scan_timer; /* scan timer */ 64 }; 65 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss) 66 67 /* 68 * Amount of time to go off-channel during a background 69 * scan. This value should be large enough to catch most 70 * ap's but short enough that we can return on-channel 71 * before our listen interval expires. 72 * 73 * XXX tunable 74 * XXX check against configured listen interval 75 */ 76 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150) 77 78 /* 79 * Roaming-related defaults. RSSI thresholds are as returned by the 80 * driver (.5dBm). Transmit rate thresholds are IEEE rate codes (i.e 81 * .5M units) or MCS. 82 */ 83 /* rssi thresholds */ 84 #define ROAM_RSSI_11A_DEFAULT 14 /* 11a bss */ 85 #define ROAM_RSSI_11B_DEFAULT 14 /* 11b bss */ 86 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* 11b-only bss */ 87 /* transmit rate thresholds */ 88 #define ROAM_RATE_11A_DEFAULT 2*12 /* 11a bss */ 89 #define ROAM_RATE_11B_DEFAULT 2*5 /* 11b bss */ 90 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* 11b-only bss */ 91 #define ROAM_RATE_HALF_DEFAULT 2*6 /* half-width 11a/g bss */ 92 #define ROAM_RATE_QUARTER_DEFAULT 2*3 /* quarter-width 11a/g bss */ 93 #define ROAM_MCS_11N_DEFAULT (1 | IEEE80211_RATE_MCS) /* 11n bss */ 94 95 static void scan_curchan(struct ieee80211_scan_state *, unsigned long); 96 static void scan_mindwell(struct ieee80211_scan_state *); 97 static void scan_signal(void *); 98 static void scan_task(void *, int); 99 100 void 101 ieee80211_scan_attach(struct ieee80211com *ic) 102 { 103 struct scan_state *ss; 104 105 ss = (struct scan_state *) malloc(sizeof(struct scan_state), 106 M_80211_SCAN, M_NOWAIT | M_ZERO); 107 if (ss == NULL) { 108 ic->ic_scan = NULL; 109 return; 110 } 111 callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0); 112 cv_init(&ss->ss_scan_cv, "scan"); 113 TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss); 114 ic->ic_scan = &ss->base; 115 ss->base.ss_ic = ic; 116 117 ic->ic_scan_curchan = scan_curchan; 118 ic->ic_scan_mindwell = scan_mindwell; 119 120 #if defined(__HAIKU__) 121 ieee80211_scan_sta_init(); 122 #endif 123 } 124 125 void 126 ieee80211_scan_detach(struct ieee80211com *ic) 127 { 128 struct ieee80211_scan_state *ss = ic->ic_scan; 129 130 if (ss != NULL) { 131 IEEE80211_LOCK(ic); 132 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT; 133 scan_signal(ss); 134 IEEE80211_UNLOCK(ic); 135 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 136 callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer); 137 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, 138 ("scan still running")); 139 if (ss->ss_ops != NULL) { 140 ss->ss_ops->scan_detach(ss); 141 ss->ss_ops = NULL; 142 } 143 ic->ic_scan = NULL; 144 free(SCAN_PRIVATE(ss), M_80211_SCAN); 145 } 146 147 148 #if defined(__HAIKU__) 149 ieee80211_scan_sta_uninit(); 150 #endif 151 } 152 153 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = { 154 [IEEE80211_MODE_11A] = { .rssi = ROAM_RSSI_11A_DEFAULT, 155 .rate = ROAM_RATE_11A_DEFAULT }, 156 [IEEE80211_MODE_11G] = { .rssi = ROAM_RSSI_11B_DEFAULT, 157 .rate = ROAM_RATE_11B_DEFAULT }, 158 [IEEE80211_MODE_11B] = { .rssi = ROAM_RSSI_11BONLY_DEFAULT, 159 .rate = ROAM_RATE_11BONLY_DEFAULT }, 160 [IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT, 161 .rate = ROAM_RATE_11A_DEFAULT }, 162 [IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT, 163 .rate = ROAM_RATE_11A_DEFAULT }, 164 [IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT, 165 .rate = ROAM_RATE_11A_DEFAULT }, 166 [IEEE80211_MODE_HALF] = { .rssi = ROAM_RSSI_11A_DEFAULT, 167 .rate = ROAM_RATE_HALF_DEFAULT }, 168 [IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT, 169 .rate = ROAM_RATE_QUARTER_DEFAULT }, 170 [IEEE80211_MODE_11NA] = { .rssi = ROAM_RSSI_11A_DEFAULT, 171 .rate = ROAM_MCS_11N_DEFAULT }, 172 [IEEE80211_MODE_11NG] = { .rssi = ROAM_RSSI_11B_DEFAULT, 173 .rate = ROAM_MCS_11N_DEFAULT }, 174 }; 175 176 void 177 ieee80211_scan_vattach(struct ieee80211vap *vap) 178 { 179 vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz; 180 vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz; 181 vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz; 182 183 vap->iv_roaming = IEEE80211_ROAMING_AUTO; 184 memcpy(vap->iv_roamparms, defroam, sizeof(defroam)); 185 186 } 187 188 void 189 ieee80211_scan_vdetach(struct ieee80211vap *vap) 190 { 191 struct ieee80211com *ic = vap->iv_ic; 192 struct ieee80211_scan_state *ss; 193 194 IEEE80211_LOCK(ic); 195 ss = ic->ic_scan; 196 if (ss != NULL && ss->ss_vap == vap) { 197 if (ic->ic_flags & IEEE80211_F_SCAN) { 198 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT; 199 scan_signal(ss); 200 } 201 if (ss->ss_ops != NULL) { 202 ss->ss_ops->scan_detach(ss); 203 ss->ss_ops = NULL; 204 } 205 ss->ss_vap = NULL; 206 } 207 IEEE80211_UNLOCK(ic); 208 } 209 210 /* 211 * Simple-minded scanner module support. 212 */ 213 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = { 214 "wlan_scan_sta", /* IEEE80211_M_IBSS */ 215 "wlan_scan_sta", /* IEEE80211_M_STA */ 216 "wlan_scan_wds", /* IEEE80211_M_WDS */ 217 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */ 218 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */ 219 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */ 220 "wlan_scan_sta", /* IEEE80211_M_MBSS */ 221 }; 222 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX]; 223 224 const struct ieee80211_scanner * 225 ieee80211_scanner_get(enum ieee80211_opmode mode) 226 { 227 if (mode >= IEEE80211_OPMODE_MAX) 228 return NULL; 229 if (scanners[mode] == NULL) 230 ieee80211_load_module(scan_modnames[mode]); 231 return scanners[mode]; 232 } 233 234 void 235 ieee80211_scanner_register(enum ieee80211_opmode mode, 236 const struct ieee80211_scanner *scan) 237 { 238 if (mode >= IEEE80211_OPMODE_MAX) 239 return; 240 scanners[mode] = scan; 241 } 242 243 void 244 ieee80211_scanner_unregister(enum ieee80211_opmode mode, 245 const struct ieee80211_scanner *scan) 246 { 247 if (mode >= IEEE80211_OPMODE_MAX) 248 return; 249 if (scanners[mode] == scan) 250 scanners[mode] = NULL; 251 } 252 253 void 254 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan) 255 { 256 int m; 257 258 for (m = 0; m < IEEE80211_OPMODE_MAX; m++) 259 if (scanners[m] == scan) 260 scanners[m] = NULL; 261 } 262 263 /* 264 * Update common scanner state to reflect the current 265 * operating mode. This is called when the state machine 266 * is transitioned to RUN state w/o scanning--e.g. when 267 * operating in monitor mode. The purpose of this is to 268 * ensure later callbacks find ss_ops set to properly 269 * reflect current operating mode. 270 */ 271 static void 272 scan_update_locked(struct ieee80211vap *vap, 273 const struct ieee80211_scanner *scan) 274 { 275 struct ieee80211com *ic = vap->iv_ic; 276 struct ieee80211_scan_state *ss = ic->ic_scan; 277 278 IEEE80211_LOCK_ASSERT(ic); 279 280 #ifdef IEEE80211_DEBUG 281 if (ss->ss_vap != vap || ss->ss_ops != scan) { 282 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 283 "%s: current scanner is <%s:%s>, switch to <%s:%s>\n", 284 __func__, 285 ss->ss_vap != NULL ? 286 ss->ss_vap->iv_ifp->if_xname : "none", 287 ss->ss_vap != NULL ? 288 ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none", 289 vap->iv_ifp->if_xname, 290 ieee80211_opmode_name[vap->iv_opmode]); 291 } 292 #endif 293 ss->ss_vap = vap; 294 if (ss->ss_ops != scan) { 295 /* 296 * Switch scanners; detach old, attach new. Special 297 * case where a single scan module implements multiple 298 * policies by using different scan ops but a common 299 * core. We assume if the old and new attach methods 300 * are identical then it's ok to just change ss_ops 301 * and not flush the internal state of the module. 302 */ 303 if (scan == NULL || ss->ss_ops == NULL || 304 ss->ss_ops->scan_attach != scan->scan_attach) { 305 if (ss->ss_ops != NULL) 306 ss->ss_ops->scan_detach(ss); 307 if (scan != NULL && !scan->scan_attach(ss)) { 308 /* XXX attach failure */ 309 /* XXX stat+msg */ 310 scan = NULL; 311 } 312 } 313 ss->ss_ops = scan; 314 } 315 } 316 317 static char 318 channel_type(const struct ieee80211_channel *c) 319 { 320 if (IEEE80211_IS_CHAN_ST(c)) 321 return 'S'; 322 if (IEEE80211_IS_CHAN_108A(c)) 323 return 'T'; 324 if (IEEE80211_IS_CHAN_108G(c)) 325 return 'G'; 326 if (IEEE80211_IS_CHAN_HT(c)) 327 return 'n'; 328 if (IEEE80211_IS_CHAN_A(c)) 329 return 'a'; 330 if (IEEE80211_IS_CHAN_ANYG(c)) 331 return 'g'; 332 if (IEEE80211_IS_CHAN_B(c)) 333 return 'b'; 334 return 'f'; 335 } 336 337 void 338 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss) 339 { 340 struct ieee80211com *ic = ss->ss_ic; 341 const char *sep; 342 int i; 343 344 sep = ""; 345 for (i = ss->ss_next; i < ss->ss_last; i++) { 346 const struct ieee80211_channel *c = ss->ss_chans[i]; 347 348 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c), 349 channel_type(c)); 350 sep = ", "; 351 } 352 } 353 354 #ifdef IEEE80211_DEBUG 355 static void 356 scan_dump(struct ieee80211_scan_state *ss) 357 { 358 struct ieee80211vap *vap = ss->ss_vap; 359 360 if_printf(vap->iv_ifp, "scan set "); 361 ieee80211_scan_dump_channels(ss); 362 printf(" dwell min %lums max %lums\n", 363 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell)); 364 } 365 #endif /* IEEE80211_DEBUG */ 366 367 static void 368 copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss, 369 int nssid, const struct ieee80211_scan_ssid ssids[]) 370 { 371 if (nssid > IEEE80211_SCAN_MAX_SSID) { 372 /* XXX printf */ 373 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 374 "%s: too many ssid %d, ignoring all of them\n", 375 __func__, nssid); 376 return; 377 } 378 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0])); 379 ss->ss_nssid = nssid; 380 } 381 382 /* 383 * Start a scan unless one is already going. 384 */ 385 static int 386 start_scan_locked(const struct ieee80211_scanner *scan, 387 struct ieee80211vap *vap, int flags, u_int duration, 388 u_int mindwell, u_int maxdwell, 389 u_int nssid, const struct ieee80211_scan_ssid ssids[]) 390 { 391 struct ieee80211com *ic = vap->iv_ic; 392 struct ieee80211_scan_state *ss = ic->ic_scan; 393 394 IEEE80211_LOCK_ASSERT(ic); 395 396 if (ic->ic_flags & IEEE80211_F_CSAPENDING) { 397 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 398 "%s: scan inhibited by pending channel change\n", __func__); 399 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 400 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 401 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n" 402 , __func__ 403 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive" 404 , duration, mindwell, maxdwell 405 , ieee80211_phymode_name[vap->iv_des_mode] 406 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append" 407 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : "" 408 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : "" 409 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : "" 410 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : "" 411 , flags & IEEE80211_SCAN_ONCE ? ", once" : "" 412 ); 413 414 #ifdef __HAIKU__ 415 /* We never want to join if not explicitly looking for an SSID */ 416 if (nssid == 0 && (flags & IEEE80211_SCAN_NOJOIN) == 0) { 417 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 418 "%s: setting nojoin due to no configured ssid\n", __func__); 419 flags |= IEEE80211_SCAN_NOJOIN; 420 } 421 #endif 422 423 scan_update_locked(vap, scan); 424 if (ss->ss_ops != NULL) { 425 if ((flags & IEEE80211_SCAN_NOSSID) == 0) 426 copy_ssid(vap, ss, nssid, ssids); 427 428 /* NB: top 4 bits for internal use */ 429 ss->ss_flags = flags & 0xfff; 430 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 431 vap->iv_stats.is_scan_active++; 432 else 433 vap->iv_stats.is_scan_passive++; 434 if (flags & IEEE80211_SCAN_FLUSH) 435 ss->ss_ops->scan_flush(ss); 436 if (flags & IEEE80211_SCAN_BGSCAN) 437 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN; 438 439 /* NB: flush frames rx'd before 1st channel change */ 440 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD; 441 SCAN_PRIVATE(ss)->ss_duration = duration; 442 ss->ss_next = 0; 443 ss->ss_mindwell = mindwell; 444 ss->ss_maxdwell = maxdwell; 445 /* NB: scan_start must be before the scan runtask */ 446 ss->ss_ops->scan_start(ss, vap); 447 #ifdef IEEE80211_DEBUG 448 if (ieee80211_msg_scan(vap)) 449 scan_dump(ss); 450 #endif /* IEEE80211_DEBUG */ 451 ic->ic_flags |= IEEE80211_F_SCAN; 452 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 453 } 454 return 1; 455 } else { 456 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 457 "%s: %s scan already in progress\n", __func__, 458 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"); 459 } 460 return 0; 461 } 462 463 /* 464 * Start a scan unless one is already going. 465 */ 466 int 467 ieee80211_start_scan(struct ieee80211vap *vap, int flags, 468 u_int duration, u_int mindwell, u_int maxdwell, 469 u_int nssid, const struct ieee80211_scan_ssid ssids[]) 470 { 471 struct ieee80211com *ic = vap->iv_ic; 472 const struct ieee80211_scanner *scan; 473 int result; 474 475 scan = ieee80211_scanner_get(vap->iv_opmode); 476 if (scan == NULL) { 477 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 478 "%s: no scanner support for %s mode\n", 479 __func__, ieee80211_opmode_name[vap->iv_opmode]); 480 /* XXX stat */ 481 return 0; 482 } 483 484 IEEE80211_LOCK(ic); 485 result = start_scan_locked(scan, vap, flags, duration, 486 mindwell, maxdwell, nssid, ssids); 487 IEEE80211_UNLOCK(ic); 488 489 return result; 490 } 491 492 /* 493 * Check the scan cache for an ap/channel to use; if that 494 * fails then kick off a new scan. 495 */ 496 int 497 ieee80211_check_scan(struct ieee80211vap *vap, int flags, 498 u_int duration, u_int mindwell, u_int maxdwell, 499 u_int nssid, const struct ieee80211_scan_ssid ssids[]) 500 { 501 struct ieee80211com *ic = vap->iv_ic; 502 struct ieee80211_scan_state *ss = ic->ic_scan; 503 const struct ieee80211_scanner *scan; 504 int result; 505 506 scan = ieee80211_scanner_get(vap->iv_opmode); 507 if (scan == NULL) { 508 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 509 "%s: no scanner support for %s mode\n", 510 __func__, vap->iv_opmode); 511 /* XXX stat */ 512 return 0; 513 } 514 515 /* 516 * Check if there's a list of scan candidates already. 517 * XXX want more than the ap we're currently associated with 518 */ 519 520 IEEE80211_LOCK(ic); 521 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 522 "%s: %s scan, %s%s%s%s%s\n" 523 , __func__ 524 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive" 525 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append" 526 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : "" 527 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : "" 528 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : "" 529 , flags & IEEE80211_SCAN_ONCE ? ", once" : "" 530 ); 531 532 if (ss->ss_ops != scan) { 533 /* XXX re-use cache contents? e.g. adhoc<->sta */ 534 flags |= IEEE80211_SCAN_FLUSH; 535 } 536 scan_update_locked(vap, scan); 537 if (ss->ss_ops != NULL) { 538 /* XXX verify ss_ops matches vap->iv_opmode */ 539 if ((flags & IEEE80211_SCAN_NOSSID) == 0) { 540 /* 541 * Update the ssid list and mark flags so if 542 * we call start_scan it doesn't duplicate work. 543 */ 544 copy_ssid(vap, ss, nssid, ssids); 545 flags |= IEEE80211_SCAN_NOSSID; 546 } 547 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 && 548 (flags & IEEE80211_SCAN_FLUSH) == 0 && 549 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) { 550 /* 551 * We're not currently scanning and the cache is 552 * deemed hot enough to consult. Lock out others 553 * by marking IEEE80211_F_SCAN while we decide if 554 * something is already in the scan cache we can 555 * use. Also discard any frames that might come 556 * in while temporarily marked as scanning. 557 */ 558 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD; 559 ic->ic_flags |= IEEE80211_F_SCAN; 560 561 /* NB: need to use supplied flags in check */ 562 ss->ss_flags = flags & 0xff; 563 result = ss->ss_ops->scan_end(ss, vap); 564 565 ic->ic_flags &= ~IEEE80211_F_SCAN; 566 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD; 567 if (result) { 568 ieee80211_notify_scan_done(vap); 569 IEEE80211_UNLOCK(ic); 570 return 1; 571 } 572 } 573 } 574 result = start_scan_locked(scan, vap, flags, duration, 575 mindwell, maxdwell, nssid, ssids); 576 IEEE80211_UNLOCK(ic); 577 578 return result; 579 } 580 581 /* 582 * Check the scan cache for an ap/channel to use; if that fails 583 * then kick off a scan using the current settings. 584 */ 585 int 586 ieee80211_check_scan_current(struct ieee80211vap *vap) 587 { 588 return ieee80211_check_scan(vap, 589 IEEE80211_SCAN_ACTIVE, 590 IEEE80211_SCAN_FOREVER, 0, 0, 591 vap->iv_des_nssid, vap->iv_des_ssid); 592 } 593 594 /* 595 * Restart a previous scan. If the previous scan completed 596 * then we start again using the existing channel list. 597 */ 598 int 599 ieee80211_bg_scan(struct ieee80211vap *vap, int flags) 600 { 601 struct ieee80211com *ic = vap->iv_ic; 602 struct ieee80211_scan_state *ss = ic->ic_scan; 603 const struct ieee80211_scanner *scan; 604 605 scan = ieee80211_scanner_get(vap->iv_opmode); 606 if (scan == NULL) { 607 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 608 "%s: no scanner support for %s mode\n", 609 __func__, vap->iv_opmode); 610 /* XXX stat */ 611 return 0; 612 } 613 614 IEEE80211_LOCK(ic); 615 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 616 u_int duration; 617 /* 618 * Go off-channel for a fixed interval that is large 619 * enough to catch most ap's but short enough that 620 * we can return on-channel before our listen interval 621 * expires. 622 */ 623 duration = IEEE80211_SCAN_OFFCHANNEL; 624 625 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 626 "%s: %s scan, ticks %u duration %lu\n", __func__, 627 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive", 628 ticks, duration); 629 630 scan_update_locked(vap, scan); 631 if (ss->ss_ops != NULL) { 632 ss->ss_vap = vap; 633 /* 634 * A background scan does not select a new sta; it 635 * just refreshes the scan cache. Also, indicate 636 * the scan logic should follow the beacon schedule: 637 * we go off-channel and scan for a while, then 638 * return to the bss channel to receive a beacon, 639 * then go off-channel again. All during this time 640 * we notify the ap we're in power save mode. When 641 * the scan is complete we leave power save mode. 642 * If any beacon indicates there are frames pending 643 * for us then we drop out of power save mode 644 * (and background scan) automatically by way of the 645 * usual sta power save logic. 646 */ 647 ss->ss_flags |= IEEE80211_SCAN_NOPICK 648 | IEEE80211_SCAN_BGSCAN 649 | flags 650 ; 651 /* if previous scan completed, restart */ 652 if (ss->ss_next >= ss->ss_last) { 653 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 654 vap->iv_stats.is_scan_active++; 655 else 656 vap->iv_stats.is_scan_passive++; 657 /* 658 * NB: beware of the scan cache being flushed; 659 * if the channel list is empty use the 660 * scan_start method to populate it. 661 */ 662 ss->ss_next = 0; 663 if (ss->ss_last != 0) 664 ss->ss_ops->scan_restart(ss, vap); 665 else { 666 ss->ss_ops->scan_start(ss, vap); 667 #ifdef IEEE80211_DEBUG 668 if (ieee80211_msg_scan(vap)) 669 scan_dump(ss); 670 #endif /* IEEE80211_DEBUG */ 671 } 672 } 673 /* NB: flush frames rx'd before 1st channel change */ 674 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD; 675 SCAN_PRIVATE(ss)->ss_duration = duration; 676 ss->ss_maxdwell = duration; 677 ic->ic_flags |= IEEE80211_F_SCAN; 678 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN; 679 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 680 } else { 681 /* XXX msg+stat */ 682 } 683 } else { 684 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 685 "%s: %s scan already in progress\n", __func__, 686 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"); 687 } 688 IEEE80211_UNLOCK(ic); 689 690 /* NB: racey, does it matter? */ 691 return (ic->ic_flags & IEEE80211_F_SCAN); 692 } 693 694 /* 695 * Cancel any scan currently going on for the specified vap. 696 */ 697 void 698 ieee80211_cancel_scan(struct ieee80211vap *vap) 699 { 700 struct ieee80211com *ic = vap->iv_ic; 701 struct ieee80211_scan_state *ss = ic->ic_scan; 702 703 IEEE80211_LOCK(ic); 704 if ((ic->ic_flags & IEEE80211_F_SCAN) && 705 ss->ss_vap == vap && 706 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) { 707 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 708 "%s: cancel %s scan\n", __func__, 709 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? 710 "active" : "passive"); 711 712 /* clear bg scan NOPICK and mark cancel request */ 713 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 714 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL; 715 /* wake up the scan task */ 716 scan_signal(ss); 717 } 718 IEEE80211_UNLOCK(ic); 719 } 720 721 /* 722 * Cancel any scan currently going on. 723 */ 724 void 725 ieee80211_cancel_anyscan(struct ieee80211vap *vap) 726 { 727 struct ieee80211com *ic = vap->iv_ic; 728 struct ieee80211_scan_state *ss = ic->ic_scan; 729 730 IEEE80211_LOCK(ic); 731 if ((ic->ic_flags & IEEE80211_F_SCAN) && 732 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) { 733 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 734 "%s: cancel %s scan\n", __func__, 735 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? 736 "active" : "passive"); 737 738 /* clear bg scan NOPICK and mark cancel request */ 739 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 740 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL; 741 /* wake up the scan task */ 742 scan_signal(ss); 743 } 744 IEEE80211_UNLOCK(ic); 745 } 746 747 /* 748 * Public access to scan_next for drivers that manage 749 * scanning themselves (e.g. for firmware-based devices). 750 */ 751 void 752 ieee80211_scan_next(struct ieee80211vap *vap) 753 { 754 struct ieee80211com *ic = vap->iv_ic; 755 struct ieee80211_scan_state *ss = ic->ic_scan; 756 757 /* wake up the scan task */ 758 IEEE80211_LOCK(ic); 759 scan_signal(ss); 760 IEEE80211_UNLOCK(ic); 761 } 762 763 /* 764 * Public access to scan_next for drivers that are not able to scan single 765 * channels (e.g. for firmware-based devices). 766 */ 767 void 768 ieee80211_scan_done(struct ieee80211vap *vap) 769 { 770 struct ieee80211com *ic = vap->iv_ic; 771 struct ieee80211_scan_state *ss; 772 773 IEEE80211_LOCK(ic); 774 ss = ic->ic_scan; 775 ss->ss_next = ss->ss_last; /* all channels are complete */ 776 scan_signal(ss); 777 IEEE80211_UNLOCK(ic); 778 } 779 780 /* 781 * Probe the curent channel, if allowed, while scanning. 782 * If the channel is not marked passive-only then send 783 * a probe request immediately. Otherwise mark state and 784 * listen for beacons on the channel; if we receive something 785 * then we'll transmit a probe request. 786 */ 787 void 788 ieee80211_probe_curchan(struct ieee80211vap *vap, int force) 789 { 790 struct ieee80211com *ic = vap->iv_ic; 791 struct ieee80211_scan_state *ss = ic->ic_scan; 792 struct ifnet *ifp = vap->iv_ifp; 793 int i; 794 795 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) { 796 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN; 797 return; 798 } 799 /* 800 * Send directed probe requests followed by any 801 * broadcast probe request. 802 * XXX remove dependence on ic/vap->iv_bss 803 */ 804 for (i = 0; i < ss->ss_nssid; i++) 805 ieee80211_send_probereq(vap->iv_bss, 806 vap->iv_myaddr, ifp->if_broadcastaddr, 807 ifp->if_broadcastaddr, 808 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len); 809 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0) 810 ieee80211_send_probereq(vap->iv_bss, 811 vap->iv_myaddr, ifp->if_broadcastaddr, 812 ifp->if_broadcastaddr, 813 "", 0); 814 } 815 816 /* 817 * Scan curchan. If this is an active scan and the channel 818 * is not marked passive then send probe request frame(s). 819 * Arrange for the channel change after maxdwell ticks. 820 */ 821 static void 822 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 823 { 824 struct ieee80211vap *vap = ss->ss_vap; 825 826 IEEE80211_LOCK(vap->iv_ic); 827 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 828 ieee80211_probe_curchan(vap, 0); 829 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 830 maxdwell, scan_signal, ss); 831 IEEE80211_UNLOCK(vap->iv_ic); 832 } 833 834 static void 835 scan_signal(void *arg) 836 { 837 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg; 838 839 IEEE80211_LOCK_ASSERT(ss->ss_ic); 840 841 cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv); 842 } 843 844 /* 845 * Handle mindwell requirements completed; initiate a channel 846 * change to the next channel asap. 847 */ 848 static void 849 scan_mindwell(struct ieee80211_scan_state *ss) 850 { 851 struct ieee80211com *ic = ss->ss_ic; 852 853 IEEE80211_LOCK(ic); 854 scan_signal(ss); 855 IEEE80211_UNLOCK(ic); 856 } 857 858 static void 859 scan_task(void *arg, int pending) 860 { 861 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD) 862 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg; 863 struct ieee80211vap *vap = ss->ss_vap; 864 struct ieee80211com *ic = ss->ss_ic; 865 struct ieee80211_channel *chan; 866 unsigned long maxdwell, scanend; 867 int scandone = 0; 868 869 IEEE80211_LOCK(ic); 870 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 || 871 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) { 872 /* Cancelled before we started */ 873 goto done; 874 } 875 876 if (ss->ss_next == ss->ss_last) { 877 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 878 "%s: no channels to scan\n", __func__); 879 goto done; 880 } 881 882 if (vap->iv_opmode == IEEE80211_M_STA && 883 vap->iv_state == IEEE80211_S_RUN) { 884 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) { 885 /* Enable station power save mode */ 886 ieee80211_sta_pwrsave(vap, 1); 887 /* 888 * Use an 1ms delay so the null data frame has a chance 889 * to go out. 890 * XXX Should use M_TXCB mechanism to eliminate this. 891 */ 892 cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv, 893 IEEE80211_LOCK_OBJ(ic), hz / 1000); 894 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) 895 goto done; 896 } 897 } 898 899 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration; 900 IEEE80211_UNLOCK(ic); 901 ic->ic_scan_start(ic); /* notify driver */ 902 IEEE80211_LOCK(ic); 903 904 for (;;) { 905 scandone = (ss->ss_next >= ss->ss_last) || 906 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0; 907 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) || 908 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) || 909 time_after(ticks + ss->ss_mindwell, scanend)) 910 break; 911 912 chan = ss->ss_chans[ss->ss_next++]; 913 914 /* 915 * Watch for truncation due to the scan end time. 916 */ 917 if (time_after(ticks + ss->ss_maxdwell, scanend)) 918 maxdwell = scanend - ticks; 919 else 920 maxdwell = ss->ss_maxdwell; 921 922 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 923 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n", 924 __func__, 925 ieee80211_chan2ieee(ic, ic->ic_curchan), 926 channel_type(ic->ic_curchan), 927 ieee80211_chan2ieee(ic, chan), channel_type(chan), 928 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) && 929 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ? 930 "active" : "passive", 931 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell)); 932 933 /* 934 * Potentially change channel and phy mode. 935 */ 936 ic->ic_curchan = chan; 937 ic->ic_rt = ieee80211_get_ratetable(chan); 938 IEEE80211_UNLOCK(ic); 939 /* 940 * Perform the channel change and scan unlocked so the driver 941 * may sleep. Once set_channel returns the hardware has 942 * completed the channel change. 943 */ 944 ic->ic_set_channel(ic); 945 ieee80211_radiotap_chan_change(ic); 946 947 /* 948 * Scan curchan. Drivers for "intelligent hardware" 949 * override ic_scan_curchan to tell the device to do 950 * the work. Otherwise we manage the work outselves; 951 * sending a probe request (as needed), and arming the 952 * timeout to switch channels after maxdwell ticks. 953 * 954 * scan_curchan should only pause for the time required to 955 * prepare/initiate the hardware for the scan (if at all), the 956 * below condvar is used to sleep for the channels dwell time 957 * and allows it to be signalled for abort. 958 */ 959 ic->ic_scan_curchan(ss, maxdwell); 960 IEEE80211_LOCK(ic); 961 962 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell; 963 /* clear mindwell lock and initial channel change flush */ 964 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP; 965 966 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT))) 967 continue; 968 969 /* Wait to be signalled to scan the next channel */ 970 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic)); 971 } 972 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) 973 goto done; 974 975 IEEE80211_UNLOCK(ic); 976 ic->ic_scan_end(ic); /* notify driver */ 977 IEEE80211_LOCK(ic); 978 979 /* 980 * Record scan complete time. Note that we also do 981 * this when canceled so any background scan will 982 * not be restarted for a while. 983 */ 984 if (scandone) 985 ic->ic_lastscan = ticks; 986 /* return to the bss channel */ 987 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 988 ic->ic_curchan != ic->ic_bsschan) { 989 ieee80211_setupcurchan(ic, ic->ic_bsschan); 990 IEEE80211_UNLOCK(ic); 991 ic->ic_set_channel(ic); 992 ieee80211_radiotap_chan_change(ic); 993 IEEE80211_LOCK(ic); 994 } 995 /* clear internal flags and any indication of a pick */ 996 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP; 997 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK; 998 999 /* 1000 * If not canceled and scan completed, do post-processing. 1001 * If the callback function returns 0, then it wants to 1002 * continue/restart scanning. Unfortunately we needed to 1003 * notify the driver to end the scan above to avoid having 1004 * rx frames alter the scan candidate list. 1005 */ 1006 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 && 1007 !ss->ss_ops->scan_end(ss, vap) && 1008 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 && 1009 time_before(ticks + ss->ss_mindwell, scanend)) { 1010 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1011 "%s: done, restart " 1012 "[ticks %u, dwell min %lu scanend %lu]\n", 1013 __func__, 1014 ticks, ss->ss_mindwell, scanend); 1015 ss->ss_next = 0; /* reset to begining */ 1016 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 1017 vap->iv_stats.is_scan_active++; 1018 else 1019 vap->iv_stats.is_scan_passive++; 1020 1021 ss->ss_ops->scan_restart(ss, vap); /* XXX? */ 1022 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task); 1023 IEEE80211_UNLOCK(ic); 1024 return; 1025 } 1026 1027 /* past here, scandone is ``true'' if not in bg mode */ 1028 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0) 1029 scandone = 1; 1030 1031 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1032 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n", 1033 __func__, scandone ? "done" : "stopped", 1034 ticks, ss->ss_mindwell, scanend); 1035 1036 /* 1037 * Clear the SCAN bit first in case frames are 1038 * pending on the station power save queue. If 1039 * we defer this then the dispatch of the frames 1040 * may generate a request to cancel scanning. 1041 */ 1042 done: 1043 ic->ic_flags &= ~IEEE80211_F_SCAN; 1044 /* 1045 * Drop out of power save mode when a scan has 1046 * completed. If this scan was prematurely terminated 1047 * because it is a background scan then don't notify 1048 * the ap; we'll either return to scanning after we 1049 * receive the beacon frame or we'll drop out of power 1050 * save mode because the beacon indicates we have frames 1051 * waiting for us. 1052 */ 1053 if (scandone) { 1054 ieee80211_sta_pwrsave(vap, 0); 1055 if (ss->ss_next >= ss->ss_last) { 1056 ieee80211_notify_scan_done(vap); 1057 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN; 1058 } 1059 } 1060 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT); 1061 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST); 1062 IEEE80211_UNLOCK(ic); 1063 #undef ISCAN_REP 1064 } 1065 1066 #ifdef IEEE80211_DEBUG 1067 static void 1068 dump_country(const uint8_t *ie) 1069 { 1070 const struct ieee80211_country_ie *cie = 1071 (const struct ieee80211_country_ie *) ie; 1072 int i, nbands, schan, nchan; 1073 1074 if (cie->len < 3) { 1075 printf(" <bogus country ie, len %d>", cie->len); 1076 return; 1077 } 1078 printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]); 1079 nbands = (cie->len - 3) / sizeof(cie->band[0]); 1080 for (i = 0; i < nbands; i++) { 1081 schan = cie->band[i].schan; 1082 nchan = cie->band[i].nchan; 1083 if (nchan != 1) 1084 printf(" %u-%u,%u", schan, schan + nchan-1, 1085 cie->band[i].maxtxpwr); 1086 else 1087 printf(" %u,%u", schan, cie->band[i].maxtxpwr); 1088 } 1089 printf("]"); 1090 } 1091 1092 static void 1093 dump_probe_beacon(uint8_t subtype, int isnew, 1094 const uint8_t mac[IEEE80211_ADDR_LEN], 1095 const struct ieee80211_scanparams *sp, int rssi) 1096 { 1097 1098 printf("[%s] %s%s on chan %u (bss chan %u) ", 1099 ether_sprintf(mac), isnew ? "new " : "", 1100 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT], 1101 sp->chan, sp->bchan); 1102 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]); 1103 printf(" rssi %d\n", rssi); 1104 1105 if (isnew) { 1106 printf("[%s] caps 0x%x bintval %u erp 0x%x", 1107 ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp); 1108 if (sp->country != NULL) 1109 dump_country(sp->country); 1110 printf("\n"); 1111 } 1112 } 1113 #endif /* IEEE80211_DEBUG */ 1114 1115 /* 1116 * Process a beacon or probe response frame. 1117 */ 1118 void 1119 ieee80211_add_scan(struct ieee80211vap *vap, 1120 const struct ieee80211_scanparams *sp, 1121 const struct ieee80211_frame *wh, 1122 int subtype, int rssi, int noise) 1123 { 1124 struct ieee80211com *ic = vap->iv_ic; 1125 struct ieee80211_scan_state *ss = ic->ic_scan; 1126 1127 /* XXX locking */ 1128 /* 1129 * Frames received during startup are discarded to avoid 1130 * using scan state setup on the initial entry to the timer 1131 * callback. This can occur because the device may enable 1132 * rx prior to our doing the initial channel change in the 1133 * timer routine. 1134 */ 1135 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD) 1136 return; 1137 #ifdef IEEE80211_DEBUG 1138 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN)) 1139 dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi); 1140 #endif 1141 if (ss->ss_ops != NULL && 1142 ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise)) { 1143 /* 1144 * If we've reached the min dwell time terminate 1145 * the timer so we'll switch to the next channel. 1146 */ 1147 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 && 1148 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) { 1149 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1150 "%s: chan %3d%c min dwell met (%u > %lu)\n", 1151 __func__, 1152 ieee80211_chan2ieee(ic, ic->ic_curchan), 1153 channel_type(ic->ic_curchan), 1154 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell); 1155 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL; 1156 /* 1157 * NB: trigger at next clock tick or wait for the 1158 * hardware. 1159 */ 1160 ic->ic_scan_mindwell(ss); 1161 } 1162 } 1163 } 1164 1165 /* 1166 * Timeout/age scan cache entries; called from sta timeout 1167 * timer (XXX should be self-contained). 1168 */ 1169 void 1170 ieee80211_scan_timeout(struct ieee80211com *ic) 1171 { 1172 struct ieee80211_scan_state *ss = ic->ic_scan; 1173 1174 if (ss->ss_ops != NULL) 1175 ss->ss_ops->scan_age(ss); 1176 } 1177 1178 /* 1179 * Mark a scan cache entry after a successful associate. 1180 */ 1181 void 1182 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[]) 1183 { 1184 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan; 1185 1186 if (ss->ss_ops != NULL) { 1187 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, 1188 mac, "%s", __func__); 1189 ss->ss_ops->scan_assoc_success(ss, mac); 1190 } 1191 } 1192 1193 /* 1194 * Demerit a scan cache entry after failing to associate. 1195 */ 1196 void 1197 ieee80211_scan_assoc_fail(struct ieee80211vap *vap, 1198 const uint8_t mac[], int reason) 1199 { 1200 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan; 1201 1202 if (ss->ss_ops != NULL) { 1203 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac, 1204 "%s: reason %u", __func__, reason); 1205 ss->ss_ops->scan_assoc_fail(ss, mac, reason); 1206 } 1207 } 1208 1209 /* 1210 * Iterate over the contents of the scan cache. 1211 */ 1212 void 1213 ieee80211_scan_iterate(struct ieee80211vap *vap, 1214 ieee80211_scan_iter_func *f, void *arg) 1215 { 1216 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan; 1217 1218 if (ss->ss_ops != NULL) 1219 ss->ss_ops->scan_iterate(ss, f, arg); 1220 } 1221 1222 /* 1223 * Flush the contents of the scan cache. 1224 */ 1225 void 1226 ieee80211_scan_flush(struct ieee80211vap *vap) 1227 { 1228 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan; 1229 1230 if (ss->ss_ops != NULL && ss->ss_vap == vap) { 1231 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", __func__); 1232 ss->ss_ops->scan_flush(ss); 1233 } 1234 } 1235 1236 /* 1237 * Check the scan cache for an ap/channel to use; if that 1238 * fails then kick off a new scan. 1239 */ 1240 struct ieee80211_channel * 1241 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags) 1242 { 1243 struct ieee80211_scan_state *ss = ic->ic_scan; 1244 1245 IEEE80211_LOCK_ASSERT(ic); 1246 1247 if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) { 1248 /* XXX printf? */ 1249 return NULL; 1250 } 1251 if (ss->ss_ops->scan_pickchan == NULL) { 1252 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, 1253 "%s: scan module does not support picking a channel, " 1254 "opmode %s\n", __func__, ss->ss_vap->iv_opmode); 1255 return NULL; 1256 } 1257 return ss->ss_ops->scan_pickchan(ss, flags); 1258 } 1259