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