2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 scanning support.
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/condvar.h>
40 #include <sys/socket.h>
43 #include <net/if_var.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
47 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_scan_sw.h>
54 struct ieee80211_scan_state base; /* public state */
56 u_int ss_iflags; /* flags used internally */
57 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */
58 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */
59 #define ISCAN_CANCEL 0x0004 /* cancel current scan */
60 #define ISCAN_ABORT 0x0008 /* end the scan immediately */
61 unsigned long ss_chanmindwell; /* min dwell on curchan */
62 unsigned long ss_scanend; /* time scan must stop */
63 u_int ss_duration; /* duration for next scan */
64 struct task ss_scan_task; /* scan execution */
65 struct cv ss_scan_cv; /* scan signal */
66 struct callout ss_scan_timer; /* scan timer */
68 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss)
71 * Amount of time to go off-channel during a background
72 * scan. This value should be large enough to catch most
73 * ap's but short enough that we can return on-channel
74 * before our listen interval expires.
77 * XXX check against configured listen interval
79 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150)
82 * Roaming-related defaults. RSSI thresholds are as returned by the
83 * driver (.5dBm). Transmit rate thresholds are IEEE rate codes (i.e
87 #define ROAM_RSSI_11A_DEFAULT 14 /* 11a bss */
88 #define ROAM_RSSI_11B_DEFAULT 14 /* 11b bss */
89 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* 11b-only bss */
90 /* transmit rate thresholds */
91 #define ROAM_RATE_11A_DEFAULT 2*12 /* 11a bss */
92 #define ROAM_RATE_11B_DEFAULT 2*5 /* 11b bss */
93 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* 11b-only bss */
94 #define ROAM_RATE_HALF_DEFAULT 2*6 /* half-width 11a/g bss */
95 #define ROAM_RATE_QUARTER_DEFAULT 2*3 /* quarter-width 11a/g bss */
96 #define ROAM_MCS_11N_DEFAULT (1 | IEEE80211_RATE_MCS) /* 11n bss */
98 static void scan_curchan(struct ieee80211_scan_state *, unsigned long);
99 static void scan_mindwell(struct ieee80211_scan_state *);
100 static void scan_signal(void *);
101 static void scan_task(void *, int);
103 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
106 ieee80211_swscan_attach(struct ieee80211com *ic)
108 struct scan_state *ss;
110 ss = (struct scan_state *) malloc(sizeof(struct scan_state),
111 M_80211_SCAN, M_NOWAIT | M_ZERO);
116 callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0);
117 cv_init(&ss->ss_scan_cv, "scan");
118 TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss);
120 ic->ic_scan = &ss->base;
123 ic->ic_scan_curchan = scan_curchan;
124 ic->ic_scan_mindwell = scan_mindwell;
127 * TODO: all of the non-vap scan calls should be methods!
132 ieee80211_swscan_detach(struct ieee80211com *ic)
134 struct ieee80211_scan_state *ss = ic->ic_scan;
138 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
140 IEEE80211_UNLOCK(ic);
141 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
142 callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer);
143 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0,
144 ("scan still running"));
147 * For now, do the ss_ops detach here rather
148 * than ieee80211_scan_detach().
150 * I'll figure out how to cleanly split things up
153 if (ss->ss_ops != NULL) {
154 ss->ss_ops->scan_detach(ss);
158 free(SCAN_PRIVATE(ss), M_80211_SCAN);
163 ieee80211_swscan_vattach(struct ieee80211vap *vap)
165 /* nothing to do for now */
167 * TODO: all of the vap scan calls should be methods!
173 ieee80211_swscan_vdetach(struct ieee80211vap *vap)
175 struct ieee80211com *ic = vap->iv_ic;
176 struct ieee80211_scan_state *ss;
178 IEEE80211_LOCK_ASSERT(ic);
180 if (ss != NULL && ss->ss_vap == vap) {
181 if (ic->ic_flags & IEEE80211_F_SCAN) {
182 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
189 ieee80211_swscan_set_scan_duration(struct ieee80211vap *vap, u_int duration)
191 struct ieee80211com *ic = vap->iv_ic;
192 struct ieee80211_scan_state *ss = ic->ic_scan;
194 IEEE80211_LOCK_ASSERT(ic);
196 /* NB: flush frames rx'd before 1st channel change */
197 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
198 SCAN_PRIVATE(ss)->ss_duration = duration;
202 ieee80211_swscan_run_scan_task(struct ieee80211vap *vap)
204 struct ieee80211com *ic = vap->iv_ic;
205 struct ieee80211_scan_state *ss = ic->ic_scan;
207 IEEE80211_LOCK_ASSERT(ic);
209 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
213 * Start a scan unless one is already going.
215 * Called without the comlock held; grab the comlock as appropriate.
218 ieee80211_swscan_start_scan(const struct ieee80211_scanner *scan,
219 struct ieee80211vap *vap, int flags,
220 u_int duration, u_int mindwell, u_int maxdwell,
221 u_int nssid, const struct ieee80211_scan_ssid ssids[])
223 struct ieee80211com *ic = vap->iv_ic;
226 IEEE80211_UNLOCK_ASSERT(ic);
229 result = ieee80211_start_scan_locked(scan, vap, flags, duration,
230 mindwell, maxdwell, nssid, ssids);
231 IEEE80211_UNLOCK(ic);
237 * Check the scan cache for an ap/channel to use; if that
238 * fails then kick off a new scan.
240 * Called with the comlock held.
242 * XXX TODO: split out!
245 ieee80211_swscan_check_scan(const struct ieee80211_scanner *scan,
246 struct ieee80211vap *vap, int flags,
247 u_int duration, u_int mindwell, u_int maxdwell,
248 u_int nssid, const struct ieee80211_scan_ssid ssids[])
250 struct ieee80211com *ic = vap->iv_ic;
251 struct ieee80211_scan_state *ss = ic->ic_scan;
254 IEEE80211_LOCK_ASSERT(ic);
256 if (ss->ss_ops != NULL) {
257 /* XXX verify ss_ops matches vap->iv_opmode */
258 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
260 * Update the ssid list and mark flags so if
261 * we call start_scan it doesn't duplicate work.
263 ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
264 flags |= IEEE80211_SCAN_NOSSID;
266 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
267 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
268 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
270 * We're not currently scanning and the cache is
271 * deemed hot enough to consult. Lock out others
272 * by marking IEEE80211_F_SCAN while we decide if
273 * something is already in the scan cache we can
274 * use. Also discard any frames that might come
275 * in while temporarily marked as scanning.
277 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
278 ic->ic_flags |= IEEE80211_F_SCAN;
280 /* NB: need to use supplied flags in check */
281 ss->ss_flags = flags & 0xff;
282 result = ss->ss_ops->scan_end(ss, vap);
284 ic->ic_flags &= ~IEEE80211_F_SCAN;
285 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
287 ieee80211_notify_scan_done(vap);
292 result = ieee80211_start_scan_locked(scan, vap, flags, duration,
293 mindwell, maxdwell, nssid, ssids);
299 * Restart a previous scan. If the previous scan completed
300 * then we start again using the existing channel list.
303 ieee80211_swscan_bg_scan(const struct ieee80211_scanner *scan,
304 struct ieee80211vap *vap, int flags)
306 struct ieee80211com *ic = vap->iv_ic;
307 struct ieee80211_scan_state *ss = ic->ic_scan;
309 /* XXX assert unlocked? */
310 // IEEE80211_UNLOCK_ASSERT(ic);
313 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
316 * Go off-channel for a fixed interval that is large
317 * enough to catch most ap's but short enough that
318 * we can return on-channel before our listen interval
321 duration = IEEE80211_SCAN_OFFCHANNEL;
323 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
324 "%s: %s scan, ticks %u duration %lu\n", __func__,
325 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
328 ieee80211_scan_update_locked(vap, scan);
329 if (ss->ss_ops != NULL) {
332 * A background scan does not select a new sta; it
333 * just refreshes the scan cache. Also, indicate
334 * the scan logic should follow the beacon schedule:
335 * we go off-channel and scan for a while, then
336 * return to the bss channel to receive a beacon,
337 * then go off-channel again. All during this time
338 * we notify the ap we're in power save mode. When
339 * the scan is complete we leave power save mode.
340 * If any beacon indicates there are frames pending
341 * for us then we drop out of power save mode
342 * (and background scan) automatically by way of the
343 * usual sta power save logic.
345 ss->ss_flags |= IEEE80211_SCAN_NOPICK
346 | IEEE80211_SCAN_BGSCAN
349 /* if previous scan completed, restart */
350 if (ss->ss_next >= ss->ss_last) {
351 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
352 vap->iv_stats.is_scan_active++;
354 vap->iv_stats.is_scan_passive++;
356 * NB: beware of the scan cache being flushed;
357 * if the channel list is empty use the
358 * scan_start method to populate it.
361 if (ss->ss_last != 0)
362 ss->ss_ops->scan_restart(ss, vap);
364 ss->ss_ops->scan_start(ss, vap);
365 #ifdef IEEE80211_DEBUG
366 if (ieee80211_msg_scan(vap))
367 ieee80211_scan_dump(ss);
368 #endif /* IEEE80211_DEBUG */
371 ieee80211_swscan_set_scan_duration(vap, duration);
372 ss->ss_maxdwell = duration;
373 ic->ic_flags |= IEEE80211_F_SCAN;
374 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
375 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
380 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
381 "%s: %s scan already in progress\n", __func__,
382 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
384 IEEE80211_UNLOCK(ic);
386 /* NB: racey, does it matter? */
387 return (ic->ic_flags & IEEE80211_F_SCAN);
391 * Cancel any scan currently going on for the specified vap.
394 ieee80211_swscan_cancel_scan(struct ieee80211vap *vap)
396 struct ieee80211com *ic = vap->iv_ic;
397 struct ieee80211_scan_state *ss = ic->ic_scan;
400 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
402 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
403 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
404 "%s: cancel %s scan\n", __func__,
405 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
406 "active" : "passive");
408 /* clear bg scan NOPICK and mark cancel request */
409 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
410 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
411 /* wake up the scan task */
414 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
415 "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n",
417 !! (ic->ic_flags & IEEE80211_F_SCAN),
418 (ss->ss_vap == vap ? "match" : "nomatch"),
419 !! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL));
421 IEEE80211_UNLOCK(ic);
425 * Cancel any scan currently going on.
428 ieee80211_swscan_cancel_anyscan(struct ieee80211vap *vap)
430 struct ieee80211com *ic = vap->iv_ic;
431 struct ieee80211_scan_state *ss = ic->ic_scan;
434 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
435 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
436 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
437 "%s: cancel %s scan\n", __func__,
438 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
439 "active" : "passive");
441 /* clear bg scan NOPICK and mark cancel request */
442 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
443 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
444 /* wake up the scan task */
447 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
448 "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n",
450 !! (ic->ic_flags & IEEE80211_F_SCAN),
451 (ss->ss_vap == vap ? "match" : "nomatch"),
452 !! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL));
454 IEEE80211_UNLOCK(ic);
458 * Public access to scan_next for drivers that manage
459 * scanning themselves (e.g. for firmware-based devices).
462 ieee80211_swscan_scan_next(struct ieee80211vap *vap)
464 struct ieee80211com *ic = vap->iv_ic;
465 struct ieee80211_scan_state *ss = ic->ic_scan;
467 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
469 /* wake up the scan task */
472 IEEE80211_UNLOCK(ic);
476 * Public access to scan_next for drivers that are not able to scan single
477 * channels (e.g. for firmware-based devices).
480 ieee80211_swscan_scan_done(struct ieee80211vap *vap)
482 struct ieee80211com *ic = vap->iv_ic;
483 struct ieee80211_scan_state *ss;
485 IEEE80211_LOCK_ASSERT(ic);
492 * Probe the curent channel, if allowed, while scanning.
493 * If the channel is not marked passive-only then send
494 * a probe request immediately. Otherwise mark state and
495 * listen for beacons on the channel; if we receive something
496 * then we'll transmit a probe request.
499 ieee80211_swscan_probe_curchan(struct ieee80211vap *vap, int force)
501 struct ieee80211com *ic = vap->iv_ic;
502 struct ieee80211_scan_state *ss = ic->ic_scan;
503 struct ifnet *ifp = vap->iv_ifp;
507 * Send directed probe requests followed by any
508 * broadcast probe request.
509 * XXX remove dependence on ic/vap->iv_bss
511 for (i = 0; i < ss->ss_nssid; i++)
512 ieee80211_send_probereq(vap->iv_bss,
513 vap->iv_myaddr, ifp->if_broadcastaddr,
514 ifp->if_broadcastaddr,
515 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
516 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
517 ieee80211_send_probereq(vap->iv_bss,
518 vap->iv_myaddr, ifp->if_broadcastaddr,
519 ifp->if_broadcastaddr,
524 * Scan curchan. If this is an active scan and the channel
525 * is not marked passive then send probe request frame(s).
526 * Arrange for the channel change after maxdwell ticks.
529 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
531 struct ieee80211vap *vap = ss->ss_vap;
533 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
534 "%s: calling; maxdwell=%lu\n",
537 IEEE80211_LOCK(vap->iv_ic);
538 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
539 ieee80211_probe_curchan(vap, 0);
540 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
541 maxdwell, scan_signal, ss);
542 IEEE80211_UNLOCK(vap->iv_ic);
546 scan_signal(void *arg)
548 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
550 IEEE80211_LOCK_ASSERT(ss->ss_ic);
551 cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv);
555 * Handle mindwell requirements completed; initiate a channel
556 * change to the next channel asap.
559 scan_mindwell(struct ieee80211_scan_state *ss)
561 struct ieee80211com *ic = ss->ss_ic;
563 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
567 IEEE80211_UNLOCK(ic);
571 scan_task(void *arg, int pending)
573 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD)
574 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
575 struct ieee80211vap *vap = ss->ss_vap;
576 struct ieee80211com *ic = ss->ss_ic;
577 struct ieee80211_channel *chan;
578 unsigned long maxdwell, scanend;
582 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
583 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) {
584 /* Cancelled before we started */
588 if (ss->ss_next == ss->ss_last) {
589 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
590 "%s: no channels to scan\n", __func__);
595 if (vap->iv_opmode == IEEE80211_M_STA &&
596 vap->iv_state == IEEE80211_S_RUN) {
597 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
598 /* Enable station power save mode */
599 vap->iv_sta_ps(vap, 1);
601 * Use an 1ms delay so the null data frame has a chance
603 * XXX Should use M_TXCB mechanism to eliminate this.
605 cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
606 IEEE80211_LOCK_OBJ(ic), hz / 1000);
607 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
612 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
614 /* XXX scan state can change! Re-validate scan state! */
616 IEEE80211_UNLOCK(ic);
617 ic->ic_scan_start(ic); /* notify driver */
622 scandone = (ss->ss_next >= ss->ss_last) ||
623 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
625 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
626 "%s: loop start; scandone=%d\n",
630 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
631 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
632 time_after(ticks + ss->ss_mindwell, scanend))
635 chan = ss->ss_chans[ss->ss_next++];
638 * Watch for truncation due to the scan end time.
640 if (time_after(ticks + ss->ss_maxdwell, scanend))
641 maxdwell = scanend - ticks;
643 maxdwell = ss->ss_maxdwell;
645 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
646 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
648 ieee80211_chan2ieee(ic, ic->ic_curchan),
649 ieee80211_channel_type_char(ic->ic_curchan),
650 ieee80211_chan2ieee(ic, chan),
651 ieee80211_channel_type_char(chan),
652 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
653 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
654 "active" : "passive",
655 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
658 * Potentially change channel and phy mode.
660 ic->ic_curchan = chan;
661 ic->ic_rt = ieee80211_get_ratetable(chan);
662 IEEE80211_UNLOCK(ic);
664 * Perform the channel change and scan unlocked so the driver
665 * may sleep. Once set_channel returns the hardware has
666 * completed the channel change.
668 ic->ic_set_channel(ic);
669 ieee80211_radiotap_chan_change(ic);
672 * Scan curchan. Drivers for "intelligent hardware"
673 * override ic_scan_curchan to tell the device to do
674 * the work. Otherwise we manage the work outselves;
675 * sending a probe request (as needed), and arming the
676 * timeout to switch channels after maxdwell ticks.
678 * scan_curchan should only pause for the time required to
679 * prepare/initiate the hardware for the scan (if at all), the
680 * below condvar is used to sleep for the channels dwell time
681 * and allows it to be signalled for abort.
683 ic->ic_scan_curchan(ss, maxdwell);
686 /* XXX scan state can change! Re-validate scan state! */
688 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
689 /* clear mindwell lock and initial channel change flush */
690 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
692 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
695 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: waiting\n", __func__);
696 /* Wait to be signalled to scan the next channel */
697 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic));
700 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: out\n", __func__);
702 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
705 IEEE80211_UNLOCK(ic);
706 ic->ic_scan_end(ic); /* notify driver */
708 /* XXX scan state can change! Re-validate scan state! */
711 * Since a cancellation may have occured during one of the
712 * driver calls (whilst unlocked), update scandone.
715 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) {
717 if_printf(vap->iv_ifp,
718 "%s: OOPS! scan cancelled during driver call (1)!\n",
724 * Record scan complete time. Note that we also do
725 * this when canceled so any background scan will
726 * not be restarted for a while.
729 ic->ic_lastscan = ticks;
730 /* return to the bss channel */
731 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
732 ic->ic_curchan != ic->ic_bsschan) {
733 ieee80211_setupcurchan(ic, ic->ic_bsschan);
734 IEEE80211_UNLOCK(ic);
735 ic->ic_set_channel(ic);
736 ieee80211_radiotap_chan_change(ic);
739 /* clear internal flags and any indication of a pick */
740 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
741 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
744 * If not canceled and scan completed, do post-processing.
745 * If the callback function returns 0, then it wants to
746 * continue/restart scanning. Unfortunately we needed to
747 * notify the driver to end the scan above to avoid having
748 * rx frames alter the scan candidate list.
750 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
751 !ss->ss_ops->scan_end(ss, vap) &&
752 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
753 time_before(ticks + ss->ss_mindwell, scanend)) {
754 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
756 "[ticks %u, dwell min %lu scanend %lu]\n",
758 ticks, ss->ss_mindwell, scanend);
759 ss->ss_next = 0; /* reset to begining */
760 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
761 vap->iv_stats.is_scan_active++;
763 vap->iv_stats.is_scan_passive++;
765 ss->ss_ops->scan_restart(ss, vap); /* XXX? */
766 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
767 IEEE80211_UNLOCK(ic);
771 /* past here, scandone is ``true'' if not in bg mode */
772 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
775 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
776 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
777 __func__, scandone ? "done" : "stopped",
778 ticks, ss->ss_mindwell, scanend);
781 * Since a cancellation may have occured during one of the
782 * driver calls (whilst unlocked), update scandone.
785 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) {
787 if_printf(vap->iv_ifp,
788 "%s: OOPS! scan cancelled during driver call (2)!\n",
794 * Clear the SCAN bit first in case frames are
795 * pending on the station power save queue. If
796 * we defer this then the dispatch of the frames
797 * may generate a request to cancel scanning.
800 ic->ic_flags &= ~IEEE80211_F_SCAN;
802 * Drop out of power save mode when a scan has
803 * completed. If this scan was prematurely terminated
804 * because it is a background scan then don't notify
805 * the ap; we'll either return to scanning after we
806 * receive the beacon frame or we'll drop out of power
807 * save mode because the beacon indicates we have frames
811 vap->iv_sta_ps(vap, 0);
812 if (ss->ss_next >= ss->ss_last) {
813 ieee80211_notify_scan_done(vap);
814 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
817 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
818 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
819 IEEE80211_UNLOCK(ic);
824 * Process a beacon or probe response frame.
827 ieee80211_swscan_add_scan(struct ieee80211vap *vap,
828 const struct ieee80211_scanparams *sp,
829 const struct ieee80211_frame *wh,
830 int subtype, int rssi, int noise)
832 struct ieee80211com *ic = vap->iv_ic;
833 struct ieee80211_scan_state *ss = ic->ic_scan;
837 * Frames received during startup are discarded to avoid
838 * using scan state setup on the initial entry to the timer
839 * callback. This can occur because the device may enable
840 * rx prior to our doing the initial channel change in the
843 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
845 #ifdef IEEE80211_DEBUG
846 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
847 ieee80211_scan_dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
849 if (ss->ss_ops != NULL &&
850 ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise)) {
852 * If we've reached the min dwell time terminate
853 * the timer so we'll switch to the next channel.
855 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
856 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
857 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
858 "%s: chan %3d%c min dwell met (%u > %lu)\n",
860 ieee80211_chan2ieee(ic, ic->ic_curchan),
861 ieee80211_channel_type_char(ic->ic_curchan),
862 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
863 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
865 * NB: trigger at next clock tick or wait for the
868 ic->ic_scan_mindwell(ss);