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