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 330460 2018-03-05 08:22:24Z 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 465 /* XXX for now - just don't do this per packet. */ 466 if (vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) 467 return; 468 469 cancel_scan(vap, 1, __func__); 470} 471 472/* 473 * Manually switch to the next channel in the channel list. 474 * Provided for drivers that manage scanning themselves 475 * (e.g. for firmware-based devices). 476 */ 477static void 478ieee80211_swscan_scan_next(struct ieee80211vap *vap) 479{ 480 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan; 481 482 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__); 483 484 /* wake up the scan task */ 485 scan_signal(ss, 0); 486} 487 488/* 489 * Manually stop a scan that is currently running. 490 * Provided for drivers that are not able to scan single channels 491 * (e.g. for firmware-based devices). 492 */ 493static void 494ieee80211_swscan_scan_done(struct ieee80211vap *vap) 495{ 496 struct ieee80211com *ic = vap->iv_ic; 497 struct ieee80211_scan_state *ss = ic->ic_scan; 498 499 IEEE80211_LOCK_ASSERT(ic); 500 501 scan_signal_locked(ss, 0); 502} 503 504/* 505 * Probe the current channel, if allowed, while scanning. 506 * If the channel is not marked passive-only then send 507 * a probe request immediately. Otherwise mark state and 508 * listen for beacons on the channel; if we receive something 509 * then we'll transmit a probe request. 510 */ 511static void 512ieee80211_swscan_probe_curchan(struct ieee80211vap *vap, int force) 513{ 514 struct ieee80211com *ic = vap->iv_ic; 515 struct ieee80211_scan_state *ss = ic->ic_scan; 516 struct ifnet *ifp = vap->iv_ifp; 517 int i; 518 519 /* 520 * Full-offload scan devices don't require this. 521 */ 522 if (vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) 523 return; 524 525 /* 526 * Send directed probe requests followed by any 527 * broadcast probe request. 528 * XXX remove dependence on ic/vap->iv_bss 529 */ 530 for (i = 0; i < ss->ss_nssid; i++) 531 ieee80211_send_probereq(vap->iv_bss, 532 vap->iv_myaddr, ifp->if_broadcastaddr, 533 ifp->if_broadcastaddr, 534 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len); 535 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0) 536 ieee80211_send_probereq(vap->iv_bss, 537 vap->iv_myaddr, ifp->if_broadcastaddr, 538 ifp->if_broadcastaddr, 539 "", 0); 540} 541 542/* 543 * Scan curchan. If this is an active scan and the channel 544 * is not marked passive then send probe request frame(s). 545 * Arrange for the channel change after maxdwell ticks. 546 */ 547static void 548scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 549{ 550 struct ieee80211vap *vap = ss->ss_vap; 551 struct ieee80211com *ic = ss->ss_ic; 552 553 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 554 "%s: calling; maxdwell=%lu\n", 555 __func__, 556 maxdwell); 557 IEEE80211_LOCK(ic); 558 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 559 ieee80211_probe_curchan(vap, 0); 560 taskqueue_enqueue_timeout(ic->ic_tq, 561 &SCAN_PRIVATE(ss)->ss_scan_curchan, maxdwell); 562 IEEE80211_UNLOCK(ic); 563} 564 565static void 566scan_signal(struct ieee80211_scan_state *ss, int iflags) 567{ 568 struct ieee80211com *ic = ss->ss_ic; 569 570 IEEE80211_UNLOCK_ASSERT(ic); 571 572 IEEE80211_LOCK(ic); 573 scan_signal_locked(ss, iflags); 574 IEEE80211_UNLOCK(ic); 575} 576 577static void 578scan_signal_locked(struct ieee80211_scan_state *ss, int iflags) 579{ 580 struct scan_state *ss_priv = SCAN_PRIVATE(ss); 581 struct timeout_task *scan_task = &ss_priv->ss_scan_curchan; 582 struct ieee80211com *ic = ss->ss_ic; 583 584 IEEE80211_LOCK_ASSERT(ic); 585 586 ss_priv->ss_iflags |= iflags; 587 if (ss_priv->ss_iflags & ISCAN_RUNNING) { 588 if (taskqueue_cancel_timeout(ic->ic_tq, scan_task, NULL) == 0) 589 taskqueue_enqueue_timeout(ic->ic_tq, scan_task, 0); 590 } 591} 592 593/* 594 * Handle mindwell requirements completed; initiate a channel 595 * change to the next channel asap. 596 */ 597static void 598scan_mindwell(struct ieee80211_scan_state *ss) 599{ 600 601 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: called\n", 602 __func__); 603 604 scan_signal(ss, 0); 605} 606 607static void 608scan_start(void *arg, int pending) 609{ 610#define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD) 611 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg; 612 struct scan_state *ss_priv = SCAN_PRIVATE(ss); 613 struct ieee80211vap *vap = ss->ss_vap; 614 struct ieee80211com *ic = ss->ss_ic; 615 616 IEEE80211_LOCK(ic); 617 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 || 618 (ss_priv->ss_iflags & ISCAN_ABORT)) { 619 /* Cancelled before we started */ 620 scan_done(ss, 0); 621 return; 622 } 623 624 if (ss->ss_next == ss->ss_last) { 625 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 626 "%s: no channels to scan\n", __func__); 627 scan_done(ss, 1); 628 return; 629 } 630 631 /* 632 * Put the station into power save mode. 633 * 634 * This is only required if we're not a full-offload devices; 635 * those devices manage scan/traffic differently. 636 */ 637 if (((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) && 638 vap->iv_opmode == IEEE80211_M_STA && 639 vap->iv_state == IEEE80211_S_RUN) { 640 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) { 641 /* Enable station power save mode */ 642 vap->iv_sta_ps(vap, 1); 643 /* Wait until null data frame will be ACK'ed */ 644 mtx_sleep(vap, IEEE80211_LOCK_OBJ(ic), PCATCH, 645 "sta_ps", msecs_to_ticks(10)); 646 if (ss_priv->ss_iflags & ISCAN_ABORT) { 647 scan_done(ss, 0); 648 return; 649 } 650 } 651 } 652 653 ss_priv->ss_scanend = ticks + ss_priv->ss_duration; 654 655 /* XXX scan state can change! Re-validate scan state! */ 656 657 IEEE80211_UNLOCK(ic); 658 659 ic->ic_scan_start(ic); /* notify driver */ 660 661 scan_curchan_task(ss, 0); 662} 663 664static void 665scan_curchan_task(void *arg, int pending) 666{ 667 struct ieee80211_scan_state *ss = arg; 668 struct scan_state *ss_priv = SCAN_PRIVATE(ss); 669 struct ieee80211com *ic = ss->ss_ic; 670 struct ieee80211_channel *chan; 671 unsigned long maxdwell; 672 int scandone; 673 674 IEEE80211_LOCK(ic); 675end: 676 scandone = (ss->ss_next >= ss->ss_last) || 677 (ss_priv->ss_iflags & ISCAN_CANCEL) != 0; 678 679 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, 680 "%s: loop start; scandone=%d\n", 681 __func__, 682 scandone); 683 684 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) || 685 (ss_priv->ss_iflags & ISCAN_ABORT) || 686 ieee80211_time_after(ticks + ss->ss_mindwell, ss_priv->ss_scanend)) { 687 ss_priv->ss_iflags &= ~ISCAN_RUNNING; 688 scan_end(ss, scandone); 689 return; 690 } else 691 ss_priv->ss_iflags |= ISCAN_RUNNING; 692 693 chan = ss->ss_chans[ss->ss_next++]; 694 695 /* 696 * Watch for truncation due to the scan end time. 697 */ 698 if (ieee80211_time_after(ticks + ss->ss_maxdwell, ss_priv->ss_scanend)) 699 maxdwell = ss_priv->ss_scanend - ticks; 700 else 701 maxdwell = ss->ss_maxdwell; 702 703 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, 704 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n", 705 __func__, 706 ieee80211_chan2ieee(ic, ic->ic_curchan), 707 ieee80211_channel_type_char(ic->ic_curchan), 708 ieee80211_chan2ieee(ic, chan), 709 ieee80211_channel_type_char(chan), 710 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) && 711 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ? 712 "active" : "passive", 713 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell)); 714 715 /* 716 * Potentially change channel and phy mode. 717 */ 718 ic->ic_curchan = chan; 719 ic->ic_rt = ieee80211_get_ratetable(chan); 720 IEEE80211_UNLOCK(ic); 721 /* 722 * Perform the channel change and scan unlocked so the driver 723 * may sleep. Once set_channel returns the hardware has 724 * completed the channel change. 725 */ 726 ic->ic_set_channel(ic); 727 ieee80211_radiotap_chan_change(ic); 728 729 /* 730 * Scan curchan. Drivers for "intelligent hardware" 731 * override ic_scan_curchan to tell the device to do 732 * the work. Otherwise we manage the work ourselves; 733 * sending a probe request (as needed), and arming the 734 * timeout to switch channels after maxdwell ticks. 735 * 736 * scan_curchan should only pause for the time required to 737 * prepare/initiate the hardware for the scan (if at all). 738 */ 739 ic->ic_scan_curchan(ss, maxdwell); 740 IEEE80211_LOCK(ic); 741 742 /* XXX scan state can change! Re-validate scan state! */ 743 744 ss_priv->ss_chanmindwell = ticks + ss->ss_mindwell; 745 /* clear mindwell lock and initial channel change flush */ 746 ss_priv->ss_iflags &= ~ISCAN_REP; 747 748 if (ss_priv->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)) { 749 taskqueue_cancel_timeout(ic->ic_tq, &ss_priv->ss_scan_curchan, 750 NULL); 751 goto end; 752 } 753 754 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: waiting\n", 755 __func__); 756 IEEE80211_UNLOCK(ic); 757} 758 759static void 760scan_end(struct ieee80211_scan_state *ss, int scandone) 761{ 762 struct scan_state *ss_priv = SCAN_PRIVATE(ss); 763 struct ieee80211vap *vap = ss->ss_vap; 764 struct ieee80211com *ic = ss->ss_ic; 765 766 IEEE80211_LOCK_ASSERT(ic); 767 768 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: out\n", __func__); 769 770 if (ss_priv->ss_iflags & ISCAN_ABORT) { 771 scan_done(ss, scandone); 772 return; 773 } 774 775 IEEE80211_UNLOCK(ic); 776 ic->ic_scan_end(ic); /* notify driver */ 777 IEEE80211_LOCK(ic); 778 /* XXX scan state can change! Re-validate scan state! */ 779 780 /* 781 * Since a cancellation may have occurred during one of the 782 * driver calls (whilst unlocked), update scandone. 783 */ 784 if (scandone == 0 && (ss_priv->ss_iflags & ISCAN_CANCEL) != 0) { 785 /* XXX printf? */ 786 if_printf(vap->iv_ifp, 787 "%s: OOPS! scan cancelled during driver call (1)!\n", 788 __func__); 789 scandone = 1; 790 } 791 792 /* 793 * Record scan complete time. Note that we also do 794 * this when canceled so any background scan will 795 * not be restarted for a while. 796 */ 797 if (scandone) 798 ic->ic_lastscan = ticks; 799 /* return to the bss channel */ 800 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 801 ic->ic_curchan != ic->ic_bsschan) { 802 ieee80211_setupcurchan(ic, ic->ic_bsschan); 803 IEEE80211_UNLOCK(ic); 804 ic->ic_set_channel(ic); 805 ieee80211_radiotap_chan_change(ic); 806 IEEE80211_LOCK(ic); 807 } 808 /* clear internal flags and any indication of a pick */ 809 ss_priv->ss_iflags &= ~ISCAN_REP; 810 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK; 811 812 /* 813 * If not canceled and scan completed, do post-processing. 814 * If the callback function returns 0, then it wants to 815 * continue/restart scanning. Unfortunately we needed to 816 * notify the driver to end the scan above to avoid having 817 * rx frames alter the scan candidate list. 818 */ 819 if ((ss_priv->ss_iflags & ISCAN_CANCEL) == 0 && 820 !ss->ss_ops->scan_end(ss, vap) && 821 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 && 822 ieee80211_time_before(ticks + ss->ss_mindwell, ss_priv->ss_scanend)) { 823 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 824 "%s: done, restart " 825 "[ticks %u, dwell min %lu scanend %lu]\n", 826 __func__, 827 ticks, ss->ss_mindwell, ss_priv->ss_scanend); 828 ss->ss_next = 0; /* reset to beginning */ 829 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE) 830 vap->iv_stats.is_scan_active++; 831 else 832 vap->iv_stats.is_scan_passive++; 833 834 ss->ss_ops->scan_restart(ss, vap); /* XXX? */ 835 ieee80211_runtask(ic, &ss_priv->ss_scan_start); 836 IEEE80211_UNLOCK(ic); 837 return; 838 } 839 840 /* past here, scandone is ``true'' if not in bg mode */ 841 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0) 842 scandone = 1; 843 844 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 845 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n", 846 __func__, scandone ? "done" : "stopped", 847 ticks, ss->ss_mindwell, ss_priv->ss_scanend); 848 849 /* 850 * Since a cancellation may have occurred during one of the 851 * driver calls (whilst unlocked), update scandone. 852 */ 853 if (scandone == 0 && (ss_priv->ss_iflags & ISCAN_CANCEL) != 0) { 854 /* XXX printf? */ 855 if_printf(vap->iv_ifp, 856 "%s: OOPS! scan cancelled during driver call (2)!\n", 857 __func__); 858 scandone = 1; 859 } 860 861 scan_done(ss, scandone); 862} 863 864static void 865scan_done(struct ieee80211_scan_state *ss, int scandone) 866{ 867 struct scan_state *ss_priv = SCAN_PRIVATE(ss); 868 struct ieee80211com *ic = ss->ss_ic; 869 struct ieee80211vap *vap = ss->ss_vap; 870 871 IEEE80211_LOCK_ASSERT(ic); 872 873 /* 874 * Clear the SCAN bit first in case frames are 875 * pending on the station power save queue. If 876 * we defer this then the dispatch of the frames 877 * may generate a request to cancel scanning. 878 */ 879 ic->ic_flags &= ~IEEE80211_F_SCAN; 880 881 /* 882 * Drop out of power save mode when a scan has 883 * completed. If this scan was prematurely terminated 884 * because it is a background scan then don't notify 885 * the ap; we'll either return to scanning after we 886 * receive the beacon frame or we'll drop out of power 887 * save mode because the beacon indicates we have frames 888 * waiting for us. 889 */ 890 if (scandone) { 891 /* 892 * If we're not a scan offload device, come back out of 893 * station powersave. Offload devices handle this themselves. 894 */ 895 if ((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) 896 vap->iv_sta_ps(vap, 0); 897 if (ss->ss_next >= ss->ss_last) 898 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN; 899 900 ieee80211_notify_scan_done(vap); 901 } 902 ss_priv->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT); 903 ss_priv->ss_scanend = 0; 904 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST); 905 IEEE80211_UNLOCK(ic); 906#undef ISCAN_REP 907} 908 909/* 910 * Process a beacon or probe response frame. 911 */ 912static void 913ieee80211_swscan_add_scan(struct ieee80211vap *vap, 914 struct ieee80211_channel *curchan, 915 const struct ieee80211_scanparams *sp, 916 const struct ieee80211_frame *wh, 917 int subtype, int rssi, int noise) 918{ 919 struct ieee80211com *ic = vap->iv_ic; 920 struct ieee80211_scan_state *ss = ic->ic_scan; 921 922 /* XXX locking */ 923 /* 924 * Frames received during startup are discarded to avoid 925 * using scan state setup on the initial entry to the timer 926 * callback. This can occur because the device may enable 927 * rx prior to our doing the initial channel change in the 928 * timer routine. 929 */ 930 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD) 931 return; 932#ifdef IEEE80211_DEBUG 933 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN)) 934 ieee80211_scan_dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi); 935#endif 936 if (ss->ss_ops != NULL && 937 ss->ss_ops->scan_add(ss, curchan, sp, wh, subtype, rssi, noise)) { 938 /* 939 * If we've reached the min dwell time terminate 940 * the timer so we'll switch to the next channel. 941 */ 942 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 && 943 ieee80211_time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) { 944 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 945 "%s: chan %3d%c min dwell met (%u > %lu)\n", 946 __func__, 947 ieee80211_chan2ieee(ic, ic->ic_curchan), 948 ieee80211_channel_type_char(ic->ic_curchan), 949 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell); 950 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL; 951 /* 952 * NB: trigger at next clock tick or wait for the 953 * hardware. 954 */ 955 ic->ic_scan_mindwell(ss); 956 } 957 } 958} 959 960static struct ieee80211_scan_methods swscan_methods = { 961 .sc_attach = ieee80211_swscan_attach, 962 .sc_detach = ieee80211_swscan_detach, 963 .sc_vattach = ieee80211_swscan_vattach, 964 .sc_vdetach = ieee80211_swscan_vdetach, 965 .sc_set_scan_duration = ieee80211_swscan_set_scan_duration, 966 .sc_start_scan = ieee80211_swscan_start_scan, 967 .sc_check_scan = ieee80211_swscan_check_scan, 968 .sc_bg_scan = ieee80211_swscan_bg_scan, 969 .sc_cancel_scan = ieee80211_swscan_cancel_scan, 970 .sc_cancel_anyscan = ieee80211_swscan_cancel_anyscan, 971 .sc_scan_next = ieee80211_swscan_scan_next, 972 .sc_scan_done = ieee80211_swscan_scan_done, 973 .sc_scan_probe_curchan = ieee80211_swscan_probe_curchan, 974 .sc_add_scan = ieee80211_swscan_add_scan 975}; 976 977/* 978 * Default scan attach method. 979 */ 980void 981ieee80211_swscan_attach(struct ieee80211com *ic) 982{ 983 struct scan_state *ss; 984 985 /* 986 * Setup the default methods 987 */ 988 ic->ic_scan_methods = &swscan_methods; 989 990 /* Allocate initial scan state */ 991 ss = (struct scan_state *) IEEE80211_MALLOC(sizeof(struct scan_state), 992 M_80211_SCAN, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 993 if (ss == NULL) { 994 ic->ic_scan = NULL; 995 return; 996 } 997 TASK_INIT(&ss->ss_scan_start, 0, scan_start, ss); 998 TIMEOUT_TASK_INIT(ic->ic_tq, &ss->ss_scan_curchan, 0, 999 scan_curchan_task, ss); 1000 1001 ic->ic_scan = &ss->base; 1002 ss->base.ss_ic = ic; 1003 1004 ic->ic_scan_curchan = scan_curchan; 1005 ic->ic_scan_mindwell = scan_mindwell; 1006} 1007