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.
216 ieee80211_swscan_start_scan_locked(const struct ieee80211_scanner *scan,
217 struct ieee80211vap *vap, int flags, u_int duration,
218 u_int mindwell, u_int maxdwell,
219 u_int nssid, const struct ieee80211_scan_ssid ssids[])
221 struct ieee80211com *ic = vap->iv_ic;
222 struct ieee80211_scan_state *ss = ic->ic_scan;
224 IEEE80211_LOCK_ASSERT(ic);
226 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
227 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
228 "%s: scan inhibited by pending channel change\n", __func__);
229 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
230 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
231 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
233 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
234 , duration, mindwell, maxdwell
235 , ieee80211_phymode_name[vap->iv_des_mode]
236 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
237 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
238 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
239 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
240 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
241 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
244 ieee80211_scan_update_locked(vap, scan);
245 if (ss->ss_ops != NULL) {
246 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
247 ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
249 /* NB: top 4 bits for internal use */
250 ss->ss_flags = flags & 0xfff;
251 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
252 vap->iv_stats.is_scan_active++;
254 vap->iv_stats.is_scan_passive++;
255 if (flags & IEEE80211_SCAN_FLUSH)
256 ss->ss_ops->scan_flush(ss);
257 if (flags & IEEE80211_SCAN_BGSCAN)
258 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
260 /* Set duration for this particular scan */
261 ieee80211_swscan_set_scan_duration(vap, duration);
264 ss->ss_mindwell = mindwell;
265 ss->ss_maxdwell = maxdwell;
266 /* NB: scan_start must be before the scan runtask */
267 ss->ss_ops->scan_start(ss, vap);
268 #ifdef IEEE80211_DEBUG
269 if (ieee80211_msg_scan(vap))
270 ieee80211_scan_dump(ss);
271 #endif /* IEEE80211_DEBUG */
272 ic->ic_flags |= IEEE80211_F_SCAN;
274 /* Start scan task */
275 ieee80211_swscan_run_scan_task(vap);
279 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
280 "%s: %s scan already in progress\n", __func__,
281 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
288 * Start a scan unless one is already going.
290 * Called without the comlock held; grab the comlock as appropriate.
293 ieee80211_swscan_start_scan(const struct ieee80211_scanner *scan,
294 struct ieee80211vap *vap, int flags,
295 u_int duration, u_int mindwell, u_int maxdwell,
296 u_int nssid, const struct ieee80211_scan_ssid ssids[])
298 struct ieee80211com *ic = vap->iv_ic;
301 IEEE80211_UNLOCK_ASSERT(ic);
304 result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration,
305 mindwell, maxdwell, nssid, ssids);
306 IEEE80211_UNLOCK(ic);
312 * Check the scan cache for an ap/channel to use; if that
313 * fails then kick off a new scan.
315 * Called with the comlock held.
317 * XXX TODO: split out!
320 ieee80211_swscan_check_scan(const struct ieee80211_scanner *scan,
321 struct ieee80211vap *vap, int flags,
322 u_int duration, u_int mindwell, u_int maxdwell,
323 u_int nssid, const struct ieee80211_scan_ssid ssids[])
325 struct ieee80211com *ic = vap->iv_ic;
326 struct ieee80211_scan_state *ss = ic->ic_scan;
329 IEEE80211_LOCK_ASSERT(ic);
331 if (ss->ss_ops != NULL) {
332 /* XXX verify ss_ops matches vap->iv_opmode */
333 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
335 * Update the ssid list and mark flags so if
336 * we call start_scan it doesn't duplicate work.
338 ieee80211_scan_copy_ssid(vap, ss, nssid, ssids);
339 flags |= IEEE80211_SCAN_NOSSID;
341 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
342 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
343 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
345 * We're not currently scanning and the cache is
346 * deemed hot enough to consult. Lock out others
347 * by marking IEEE80211_F_SCAN while we decide if
348 * something is already in the scan cache we can
349 * use. Also discard any frames that might come
350 * in while temporarily marked as scanning.
352 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
353 ic->ic_flags |= IEEE80211_F_SCAN;
355 /* NB: need to use supplied flags in check */
356 ss->ss_flags = flags & 0xff;
357 result = ss->ss_ops->scan_end(ss, vap);
359 ic->ic_flags &= ~IEEE80211_F_SCAN;
360 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
362 ieee80211_notify_scan_done(vap);
367 result = ieee80211_swscan_start_scan_locked(scan, vap, flags, duration,
368 mindwell, maxdwell, nssid, ssids);
374 * Restart a previous scan. If the previous scan completed
375 * then we start again using the existing channel list.
378 ieee80211_swscan_bg_scan(const struct ieee80211_scanner *scan,
379 struct ieee80211vap *vap, int flags)
381 struct ieee80211com *ic = vap->iv_ic;
382 struct ieee80211_scan_state *ss = ic->ic_scan;
384 /* XXX assert unlocked? */
385 // IEEE80211_UNLOCK_ASSERT(ic);
388 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
391 * Go off-channel for a fixed interval that is large
392 * enough to catch most ap's but short enough that
393 * we can return on-channel before our listen interval
396 duration = IEEE80211_SCAN_OFFCHANNEL;
398 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
399 "%s: %s scan, ticks %u duration %lu\n", __func__,
400 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
403 ieee80211_scan_update_locked(vap, scan);
404 if (ss->ss_ops != NULL) {
407 * A background scan does not select a new sta; it
408 * just refreshes the scan cache. Also, indicate
409 * the scan logic should follow the beacon schedule:
410 * we go off-channel and scan for a while, then
411 * return to the bss channel to receive a beacon,
412 * then go off-channel again. All during this time
413 * we notify the ap we're in power save mode. When
414 * the scan is complete we leave power save mode.
415 * If any beacon indicates there are frames pending
416 * for us then we drop out of power save mode
417 * (and background scan) automatically by way of the
418 * usual sta power save logic.
420 ss->ss_flags |= IEEE80211_SCAN_NOPICK
421 | IEEE80211_SCAN_BGSCAN
424 /* if previous scan completed, restart */
425 if (ss->ss_next >= ss->ss_last) {
426 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
427 vap->iv_stats.is_scan_active++;
429 vap->iv_stats.is_scan_passive++;
431 * NB: beware of the scan cache being flushed;
432 * if the channel list is empty use the
433 * scan_start method to populate it.
436 if (ss->ss_last != 0)
437 ss->ss_ops->scan_restart(ss, vap);
439 ss->ss_ops->scan_start(ss, vap);
440 #ifdef IEEE80211_DEBUG
441 if (ieee80211_msg_scan(vap))
442 ieee80211_scan_dump(ss);
443 #endif /* IEEE80211_DEBUG */
446 ieee80211_swscan_set_scan_duration(vap, duration);
447 ss->ss_maxdwell = duration;
448 ic->ic_flags |= IEEE80211_F_SCAN;
449 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
450 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
455 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
456 "%s: %s scan already in progress\n", __func__,
457 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
459 IEEE80211_UNLOCK(ic);
461 /* NB: racey, does it matter? */
462 return (ic->ic_flags & IEEE80211_F_SCAN);
466 * Cancel any scan currently going on for the specified vap.
469 ieee80211_swscan_cancel_scan(struct ieee80211vap *vap)
471 struct ieee80211com *ic = vap->iv_ic;
472 struct ieee80211_scan_state *ss = ic->ic_scan;
475 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
477 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
478 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
479 "%s: cancel %s scan\n", __func__,
480 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
481 "active" : "passive");
483 /* clear bg scan NOPICK and mark cancel request */
484 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
485 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
486 /* wake up the scan task */
489 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
490 "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n",
492 !! (ic->ic_flags & IEEE80211_F_SCAN),
493 (ss->ss_vap == vap ? "match" : "nomatch"),
494 !! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL));
496 IEEE80211_UNLOCK(ic);
500 * Cancel any scan currently going on.
503 ieee80211_swscan_cancel_anyscan(struct ieee80211vap *vap)
505 struct ieee80211com *ic = vap->iv_ic;
506 struct ieee80211_scan_state *ss = ic->ic_scan;
509 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
510 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
511 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
512 "%s: cancel %s scan\n", __func__,
513 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
514 "active" : "passive");
516 /* clear bg scan NOPICK and mark cancel request */
517 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
518 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
519 /* wake up the scan task */
522 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
523 "%s: called; F_SCAN=%d, vap=%s, CANCEL=%d\n",
525 !! (ic->ic_flags & IEEE80211_F_SCAN),
526 (ss->ss_vap == vap ? "match" : "nomatch"),
527 !! (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL));
529 IEEE80211_UNLOCK(ic);
533 * Public access to scan_next for drivers that manage
534 * scanning themselves (e.g. for firmware-based devices).
537 ieee80211_swscan_scan_next(struct ieee80211vap *vap)
539 struct ieee80211com *ic = vap->iv_ic;
540 struct ieee80211_scan_state *ss = ic->ic_scan;
542 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
544 /* wake up the scan task */
547 IEEE80211_UNLOCK(ic);
551 * Public access to scan_next for drivers that are not able to scan single
552 * channels (e.g. for firmware-based devices).
555 ieee80211_swscan_scan_done(struct ieee80211vap *vap)
557 struct ieee80211com *ic = vap->iv_ic;
558 struct ieee80211_scan_state *ss;
560 IEEE80211_LOCK_ASSERT(ic);
567 * Probe the curent channel, if allowed, while scanning.
568 * If the channel is not marked passive-only then send
569 * a probe request immediately. Otherwise mark state and
570 * listen for beacons on the channel; if we receive something
571 * then we'll transmit a probe request.
574 ieee80211_swscan_probe_curchan(struct ieee80211vap *vap, int force)
576 struct ieee80211com *ic = vap->iv_ic;
577 struct ieee80211_scan_state *ss = ic->ic_scan;
578 struct ifnet *ifp = vap->iv_ifp;
582 * Send directed probe requests followed by any
583 * broadcast probe request.
584 * XXX remove dependence on ic/vap->iv_bss
586 for (i = 0; i < ss->ss_nssid; i++)
587 ieee80211_send_probereq(vap->iv_bss,
588 vap->iv_myaddr, ifp->if_broadcastaddr,
589 ifp->if_broadcastaddr,
590 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
591 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
592 ieee80211_send_probereq(vap->iv_bss,
593 vap->iv_myaddr, ifp->if_broadcastaddr,
594 ifp->if_broadcastaddr,
599 * Scan curchan. If this is an active scan and the channel
600 * is not marked passive then send probe request frame(s).
601 * Arrange for the channel change after maxdwell ticks.
604 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
606 struct ieee80211vap *vap = ss->ss_vap;
608 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
609 "%s: calling; maxdwell=%lu\n",
612 IEEE80211_LOCK(vap->iv_ic);
613 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
614 ieee80211_probe_curchan(vap, 0);
615 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
616 maxdwell, scan_signal, ss);
617 IEEE80211_UNLOCK(vap->iv_ic);
621 scan_signal(void *arg)
623 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
625 IEEE80211_LOCK_ASSERT(ss->ss_ic);
626 cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv);
630 * Handle mindwell requirements completed; initiate a channel
631 * change to the next channel asap.
634 scan_mindwell(struct ieee80211_scan_state *ss)
636 struct ieee80211com *ic = ss->ss_ic;
638 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
642 IEEE80211_UNLOCK(ic);
646 scan_task(void *arg, int pending)
648 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD)
649 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
650 struct ieee80211vap *vap = ss->ss_vap;
651 struct ieee80211com *ic = ss->ss_ic;
652 struct ieee80211_channel *chan;
653 unsigned long maxdwell, scanend;
657 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
658 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) {
659 /* Cancelled before we started */
663 if (ss->ss_next == ss->ss_last) {
664 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
665 "%s: no channels to scan\n", __func__);
670 if (vap->iv_opmode == IEEE80211_M_STA &&
671 vap->iv_state == IEEE80211_S_RUN) {
672 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
673 /* Enable station power save mode */
674 vap->iv_sta_ps(vap, 1);
676 * Use an 1ms delay so the null data frame has a chance
678 * XXX Should use M_TXCB mechanism to eliminate this.
680 cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
681 IEEE80211_LOCK_OBJ(ic), hz / 1000);
682 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
687 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
689 /* XXX scan state can change! Re-validate scan state! */
691 IEEE80211_UNLOCK(ic);
692 ic->ic_scan_start(ic); /* notify driver */
697 scandone = (ss->ss_next >= ss->ss_last) ||
698 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
700 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
701 "%s: loop start; scandone=%d\n",
705 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
706 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
707 time_after(ticks + ss->ss_mindwell, scanend))
710 chan = ss->ss_chans[ss->ss_next++];
713 * Watch for truncation due to the scan end time.
715 if (time_after(ticks + ss->ss_maxdwell, scanend))
716 maxdwell = scanend - ticks;
718 maxdwell = ss->ss_maxdwell;
720 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
721 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
723 ieee80211_chan2ieee(ic, ic->ic_curchan),
724 ieee80211_channel_type_char(ic->ic_curchan),
725 ieee80211_chan2ieee(ic, chan),
726 ieee80211_channel_type_char(chan),
727 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
728 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
729 "active" : "passive",
730 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
733 * Potentially change channel and phy mode.
735 ic->ic_curchan = chan;
736 ic->ic_rt = ieee80211_get_ratetable(chan);
737 IEEE80211_UNLOCK(ic);
739 * Perform the channel change and scan unlocked so the driver
740 * may sleep. Once set_channel returns the hardware has
741 * completed the channel change.
743 ic->ic_set_channel(ic);
744 ieee80211_radiotap_chan_change(ic);
747 * Scan curchan. Drivers for "intelligent hardware"
748 * override ic_scan_curchan to tell the device to do
749 * the work. Otherwise we manage the work outselves;
750 * sending a probe request (as needed), and arming the
751 * timeout to switch channels after maxdwell ticks.
753 * scan_curchan should only pause for the time required to
754 * prepare/initiate the hardware for the scan (if at all), the
755 * below condvar is used to sleep for the channels dwell time
756 * and allows it to be signalled for abort.
758 ic->ic_scan_curchan(ss, maxdwell);
761 /* XXX scan state can change! Re-validate scan state! */
763 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
764 /* clear mindwell lock and initial channel change flush */
765 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
767 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
770 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: waiting\n", __func__);
771 /* Wait to be signalled to scan the next channel */
772 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic));
775 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: out\n", __func__);
777 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
780 IEEE80211_UNLOCK(ic);
781 ic->ic_scan_end(ic); /* notify driver */
783 /* XXX scan state can change! Re-validate scan state! */
786 * Since a cancellation may have occured during one of the
787 * driver calls (whilst unlocked), update scandone.
790 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) {
792 if_printf(vap->iv_ifp,
793 "%s: OOPS! scan cancelled during driver call (1)!\n",
799 * Record scan complete time. Note that we also do
800 * this when canceled so any background scan will
801 * not be restarted for a while.
804 ic->ic_lastscan = ticks;
805 /* return to the bss channel */
806 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
807 ic->ic_curchan != ic->ic_bsschan) {
808 ieee80211_setupcurchan(ic, ic->ic_bsschan);
809 IEEE80211_UNLOCK(ic);
810 ic->ic_set_channel(ic);
811 ieee80211_radiotap_chan_change(ic);
814 /* clear internal flags and any indication of a pick */
815 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
816 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
819 * If not canceled and scan completed, do post-processing.
820 * If the callback function returns 0, then it wants to
821 * continue/restart scanning. Unfortunately we needed to
822 * notify the driver to end the scan above to avoid having
823 * rx frames alter the scan candidate list.
825 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
826 !ss->ss_ops->scan_end(ss, vap) &&
827 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
828 time_before(ticks + ss->ss_mindwell, scanend)) {
829 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
831 "[ticks %u, dwell min %lu scanend %lu]\n",
833 ticks, ss->ss_mindwell, scanend);
834 ss->ss_next = 0; /* reset to begining */
835 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
836 vap->iv_stats.is_scan_active++;
838 vap->iv_stats.is_scan_passive++;
840 ss->ss_ops->scan_restart(ss, vap); /* XXX? */
841 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
842 IEEE80211_UNLOCK(ic);
846 /* past here, scandone is ``true'' if not in bg mode */
847 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
850 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
851 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
852 __func__, scandone ? "done" : "stopped",
853 ticks, ss->ss_mindwell, scanend);
856 * Since a cancellation may have occured during one of the
857 * driver calls (whilst unlocked), update scandone.
860 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) {
862 if_printf(vap->iv_ifp,
863 "%s: OOPS! scan cancelled during driver call (2)!\n",
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.
875 ic->ic_flags &= ~IEEE80211_F_SCAN;
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
886 vap->iv_sta_ps(vap, 0);
887 if (ss->ss_next >= ss->ss_last) {
888 ieee80211_notify_scan_done(vap);
889 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
892 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
893 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
894 IEEE80211_UNLOCK(ic);
899 * Process a beacon or probe response frame.
902 ieee80211_swscan_add_scan(struct ieee80211vap *vap,
903 struct ieee80211_channel *curchan,
904 const struct ieee80211_scanparams *sp,
905 const struct ieee80211_frame *wh,
906 int subtype, int rssi, int noise)
908 struct ieee80211com *ic = vap->iv_ic;
909 struct ieee80211_scan_state *ss = ic->ic_scan;
913 * Frames received during startup are discarded to avoid
914 * using scan state setup on the initial entry to the timer
915 * callback. This can occur because the device may enable
916 * rx prior to our doing the initial channel change in the
919 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
921 #ifdef IEEE80211_DEBUG
922 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
923 ieee80211_scan_dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
925 if (ss->ss_ops != NULL &&
926 ss->ss_ops->scan_add(ss, curchan, sp, wh, subtype, rssi, noise)) {
928 * If we've reached the min dwell time terminate
929 * the timer so we'll switch to the next channel.
931 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
932 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
933 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
934 "%s: chan %3d%c min dwell met (%u > %lu)\n",
936 ieee80211_chan2ieee(ic, ic->ic_curchan),
937 ieee80211_channel_type_char(ic->ic_curchan),
938 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
939 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
941 * NB: trigger at next clock tick or wait for the
944 ic->ic_scan_mindwell(ss);