2 * Copyright (c) 2002-2007 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.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/socket.h>
39 #include <net/if_media.h>
40 #include <net/ethernet.h>
42 #include <net80211/ieee80211_var.h>
47 struct ieee80211_scan_state base; /* public state */
49 u_int ss_iflags; /* flags used internally */
50 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */
51 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */
52 #define ISCAN_CANCEL 0x0004 /* cancel current scan */
53 #define ISCAN_START 0x0008 /* 1st time through next_scan */
54 unsigned long ss_chanmindwell; /* min dwell on curchan */
55 unsigned long ss_scanend; /* time scan must stop */
56 u_int ss_duration; /* duration for next scan */
57 struct callout ss_scan_timer; /* scan timer */
59 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss)
62 * Amount of time to go off-channel during a background
63 * scan. This value should be large enough to catch most
64 * ap's but short enough that we can return on-channel
65 * before our listen interval expires.
68 * XXX check against configured listen interval
70 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150)
73 * Roaming-related defaults. RSSI thresholds are as returned by the
74 * driver (dBm). Transmit rate thresholds are IEEE rate codes (i.e
77 #define ROAM_RSSI_11A_DEFAULT 14 /* rssi threshold for 11a bss */
78 #define ROAM_RSSI_11B_DEFAULT 14 /* rssi threshold for 11b bss */
79 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* rssi threshold for 11b-only bss */
80 #define ROAM_RATE_11A_DEFAULT 2*12 /* tx rate thresh for 11a bss */
81 #define ROAM_RATE_11B_DEFAULT 2*5 /* tx rate thresh for 11b bss */
82 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* tx rate thresh for 11b-only bss */
84 static void scan_restart_pwrsav(void *);
85 static void scan_curchan(struct ieee80211com *, unsigned long);
86 static void scan_mindwell(struct ieee80211com *);
87 static void scan_next(void *);
89 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
92 ieee80211_scan_attach(struct ieee80211com *ic)
94 struct scan_state *ss;
96 ic->ic_roaming = IEEE80211_ROAMING_AUTO;
98 MALLOC(ss, struct scan_state *, sizeof(struct scan_state),
99 M_80211_SCAN, M_NOWAIT | M_ZERO);
104 callout_init(&ss->ss_scan_timer, CALLOUT_MPSAFE);
105 ic->ic_scan = &ss->base;
107 ic->ic_scan_curchan = scan_curchan;
108 ic->ic_scan_mindwell = scan_mindwell;
110 ic->ic_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
111 ic->ic_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
112 ic->ic_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
113 ic->ic_roam.rssi11a = ROAM_RSSI_11A_DEFAULT;
114 ic->ic_roam.rssi11b = ROAM_RSSI_11B_DEFAULT;
115 ic->ic_roam.rssi11bOnly = ROAM_RSSI_11BONLY_DEFAULT;
116 ic->ic_roam.rate11a = ROAM_RATE_11A_DEFAULT;
117 ic->ic_roam.rate11b = ROAM_RATE_11B_DEFAULT;
118 ic->ic_roam.rate11bOnly = ROAM_RATE_11BONLY_DEFAULT;
122 ieee80211_scan_detach(struct ieee80211com *ic)
124 struct ieee80211_scan_state *ss = ic->ic_scan;
127 callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer);
128 if (ss->ss_ops != NULL) {
129 ss->ss_ops->scan_detach(ss);
132 ic->ic_flags &= ~IEEE80211_F_SCAN;
134 FREE(SCAN_PRIVATE(ss), M_80211_SCAN);
139 * Simple-minded scanner module support.
141 #define IEEE80211_SCANNER_MAX (IEEE80211_M_MONITOR+1)
143 static const char *scan_modnames[IEEE80211_SCANNER_MAX] = {
144 "wlan_scan_sta", /* IEEE80211_M_IBSS */
145 "wlan_scan_sta", /* IEEE80211_M_STA */
146 "wlan_scan_wds", /* IEEE80211_M_WDS */
147 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */
148 "wlan_scan_4", /* n/a */
149 "wlan_scan_5", /* n/a */
150 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */
151 "wlan_scan_7", /* n/a */
152 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */
154 static const struct ieee80211_scanner *scanners[IEEE80211_SCANNER_MAX];
156 const struct ieee80211_scanner *
157 ieee80211_scanner_get(enum ieee80211_opmode mode)
159 if (mode >= IEEE80211_SCANNER_MAX)
161 /* NB: avoid monitor mode; there is no scan support */
162 if (mode != IEEE80211_M_MONITOR && scanners[mode] == NULL)
163 ieee80211_load_module(scan_modnames[mode]);
164 return scanners[mode];
168 ieee80211_scanner_register(enum ieee80211_opmode mode,
169 const struct ieee80211_scanner *scan)
171 if (mode >= IEEE80211_SCANNER_MAX)
173 scanners[mode] = scan;
177 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
178 const struct ieee80211_scanner *scan)
180 if (mode >= IEEE80211_SCANNER_MAX)
182 if (scanners[mode] == scan)
183 scanners[mode] = NULL;
187 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
191 for (m = 0; m < IEEE80211_SCANNER_MAX; m++)
192 if (scanners[m] == scan)
197 * Update common scanner state to reflect the current
198 * operating mode. This is called when the state machine
199 * is transitioned to RUN state w/o scanning--e.g. when
200 * operating in monitor mode. The purpose of this is to
201 * ensure later callbacks find ss_ops set to properly
202 * reflect current operating mode.
205 ieee80211_scan_update(struct ieee80211com *ic)
207 struct ieee80211_scan_state *ss = ic->ic_scan;
208 const struct ieee80211_scanner *scan;
210 scan = ieee80211_scanner_get(ic->ic_opmode);
213 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
214 "%s: no scanner support for mode %u\n",
215 __func__, ic->ic_opmode);
219 if (ss->ss_ops != scan) {
220 /* switch scanners; detach old, attach new */
221 if (ss->ss_ops != NULL)
222 ss->ss_ops->scan_detach(ss);
223 if (scan != NULL && !scan->scan_attach(ss)) {
224 /* XXX attach failure */
230 IEEE80211_UNLOCK(ic);
232 return (scan != NULL);
236 change_channel(struct ieee80211com *ic,
237 struct ieee80211_channel *chan)
239 ic->ic_curchan = chan;
240 ic->ic_set_channel(ic);
244 channel_type(const struct ieee80211_channel *c)
246 if (IEEE80211_IS_CHAN_ST(c))
248 if (IEEE80211_IS_CHAN_108A(c))
250 if (IEEE80211_IS_CHAN_108G(c))
252 if (IEEE80211_IS_CHAN_HT(c))
254 if (IEEE80211_IS_CHAN_A(c))
256 if (IEEE80211_IS_CHAN_ANYG(c))
258 if (IEEE80211_IS_CHAN_B(c))
264 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
266 struct ieee80211com *ic = ss->ss_ic;
271 for (i = ss->ss_next; i < ss->ss_last; i++) {
272 const struct ieee80211_channel *c = ss->ss_chans[i];
274 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
281 * Enable station power save mode and start/restart the scanning thread.
284 scan_restart_pwrsav(void *arg)
286 struct scan_state *ss = (struct scan_state *) arg;
287 struct ieee80211com *ic = ss->base.ss_ic;
290 ieee80211_sta_pwrsave(ic, 1);
292 * Use an initial 1ms delay to insure the null
293 * data frame has a chance to go out.
294 * XXX 1ms is a lot, better to trigger scan
300 ic->ic_scan_start(ic); /* notify driver */
301 ss->ss_scanend = ticks + delay + ss->ss_duration;
302 ss->ss_iflags |= ISCAN_START;
303 callout_reset(&ss->ss_scan_timer, delay, scan_next, ss);
307 * Start/restart scanning. If we're operating in station mode
308 * and associated notify the ap we're going into power save mode
309 * and schedule a callback to initiate the work (where there's a
310 * better context for doing the work). Otherwise, start the scan
314 scan_restart(struct scan_state *ss, u_int duration)
316 struct ieee80211com *ic = ss->base.ss_ic;
319 if (ss->base.ss_next == ss->base.ss_last) {
320 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
321 "%s: no channels to scan\n", __func__);
324 if (ic->ic_opmode == IEEE80211_M_STA &&
325 ic->ic_state == IEEE80211_S_RUN) {
326 if ((ic->ic_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
328 * Initiate power save before going off-channel.
329 * Note that we cannot do this directly because
330 * of locking issues; instead we defer it to a
333 ss->ss_duration = duration;
339 ic->ic_scan_start(ic); /* notify driver */
340 ss->ss_scanend = ticks + duration;
341 ss->ss_iflags |= ISCAN_START;
342 callout_reset(&ss->ss_scan_timer, 0, scan_next, ss);
344 scan_restart_pwrsav(ss);
349 copy_ssid(struct ieee80211com *ic, struct ieee80211_scan_state *ss,
350 int nssid, const struct ieee80211_scan_ssid ssids[])
352 if (nssid > IEEE80211_SCAN_MAX_SSID) {
354 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
355 "%s: too many ssid %d, ignoring all of them\n",
359 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
360 ss->ss_nssid = nssid;
364 * Start a scan unless one is already going.
367 ieee80211_start_scan(struct ieee80211com *ic, int flags, u_int duration,
368 u_int nssid, const struct ieee80211_scan_ssid ssids[])
370 const struct ieee80211_scanner *scan;
371 struct ieee80211_scan_state *ss = ic->ic_scan;
373 scan = ieee80211_scanner_get(ic->ic_opmode);
375 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
376 "%s: no scanner support for mode %u\n",
377 __func__, ic->ic_opmode);
383 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
384 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
385 "%s: %s scan, duration %u, desired mode %s, %s%s%s%s\n"
387 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
389 , ieee80211_phymode_name[ic->ic_des_mode]
390 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
391 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
392 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
393 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
397 if (ss->ss_ops != scan) {
398 /* switch scanners; detach old, attach new */
399 if (ss->ss_ops != NULL)
400 ss->ss_ops->scan_detach(ss);
401 if (!scan->scan_attach(ss)) {
402 /* XXX attach failure */
408 if (ss->ss_ops != NULL) {
409 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
410 copy_ssid(ic, ss, nssid, ssids);
412 /* NB: top 4 bits for internal use */
413 ss->ss_flags = flags & 0xfff;
414 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
415 ic->ic_stats.is_scan_active++;
417 ic->ic_stats.is_scan_passive++;
418 if (flags & IEEE80211_SCAN_FLUSH)
419 ss->ss_ops->scan_flush(ss);
421 /* NB: flush frames rx'd before 1st channel change */
422 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
423 ss->ss_ops->scan_start(ss, ic);
424 if (scan_restart(SCAN_PRIVATE(ss), duration))
425 ic->ic_flags |= IEEE80211_F_SCAN;
428 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
429 "%s: %s scan already in progress\n", __func__,
430 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
432 IEEE80211_UNLOCK(ic);
434 /* NB: racey, does it matter? */
435 return (ic->ic_flags & IEEE80211_F_SCAN);
439 * Check the scan cache for an ap/channel to use; if that
440 * fails then kick off a new scan.
443 ieee80211_check_scan(struct ieee80211com *ic, int flags, u_int duration,
444 u_int nssid, const struct ieee80211_scan_ssid ssids[])
446 struct ieee80211_scan_state *ss = ic->ic_scan;
447 int checkscanlist = 0;
450 * Check if there's a list of scan candidates already.
451 * XXX want more than the ap we're currently associated with
455 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
456 "%s: %s scan, duration %u, desired mode %s, %s%s%s%s\n"
458 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
460 , ieee80211_phymode_name[ic->ic_des_mode]
461 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
462 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
463 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
464 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
467 if (ss->ss_ops != NULL) {
468 /* XXX verify ss_ops matches ic->ic_opmode */
469 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
471 * Update the ssid list and mark flags so if
472 * we call start_scan it doesn't duplicate work.
474 copy_ssid(ic, ss, nssid, ssids);
475 flags |= IEEE80211_SCAN_NOSSID;
477 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
478 time_before(ticks, ic->ic_lastscan + ic->ic_scanvalid)) {
480 * We're not currently scanning and the cache is
481 * deemed hot enough to consult. Lock out others
482 * by marking IEEE80211_F_SCAN while we decide if
483 * something is already in the scan cache we can
484 * use. Also discard any frames that might come
485 * in while temporarily marked as scanning.
487 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
488 ic->ic_flags |= IEEE80211_F_SCAN;
492 IEEE80211_UNLOCK(ic);
494 const struct ieee80211_scanner *scan;
496 scan = ieee80211_scanner_get(ic->ic_opmode);
498 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
499 "%s: no scanner support for mode %u\n",
500 __func__, ic->ic_opmode);
504 if (scan == ss->ss_ops && ss->ss_ops->scan_end(ss, ic)) {
505 /* found an ap, just clear the flag */
506 ic->ic_flags &= ~IEEE80211_F_SCAN;
509 /* no ap, clear the flag before starting a scan */
510 ic->ic_flags &= ~IEEE80211_F_SCAN;
512 return ieee80211_start_scan(ic, flags, duration, nssid, ssids);
516 * Restart a previous scan. If the previous scan completed
517 * then we start again using the existing channel list.
520 ieee80211_bg_scan(struct ieee80211com *ic)
522 struct ieee80211_scan_state *ss = ic->ic_scan;
525 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
528 * Go off-channel for a fixed interval that is large
529 * enough to catch most ap's but short enough that
530 * we can return on-channel before our listen interval
533 duration = IEEE80211_SCAN_OFFCHANNEL;
535 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
536 "%s: %s scan, ticks %u duration %lu\n", __func__,
537 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
540 if (ss->ss_ops != NULL) {
543 * A background scan does not select a new sta; it
544 * just refreshes the scan cache. Also, indicate
545 * the scan logic should follow the beacon schedule:
546 * we go off-channel and scan for a while, then
547 * return to the bss channel to receive a beacon,
548 * then go off-channel again. All during this time
549 * we notify the ap we're in power save mode. When
550 * the scan is complete we leave power save mode.
551 * If any beacon indicates there are frames pending
552 * for us then we drop out of power save mode
553 * (and background scan) automatically by way of the
554 * usual sta power save logic.
556 ss->ss_flags |= IEEE80211_SCAN_NOPICK
557 | IEEE80211_SCAN_BGSCAN;
558 /* if previous scan completed, restart */
559 if (ss->ss_next >= ss->ss_last) {
561 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
562 ic->ic_stats.is_scan_active++;
564 ic->ic_stats.is_scan_passive++;
565 ss->ss_ops->scan_restart(ss, ic);
567 /* NB: flush frames rx'd before 1st channel change */
568 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
569 ss->ss_maxdwell = duration;
570 if (scan_restart(SCAN_PRIVATE(ss), duration)) {
571 ic->ic_flags |= IEEE80211_F_SCAN;
572 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
578 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
579 "%s: %s scan already in progress\n", __func__,
580 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
582 IEEE80211_UNLOCK(ic);
584 /* NB: racey, does it matter? */
585 return (ic->ic_flags & IEEE80211_F_SCAN);
589 * Cancel any scan currently going on.
592 ieee80211_cancel_scan(struct ieee80211com *ic)
594 struct ieee80211_scan_state *ss = ic->ic_scan;
597 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
598 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
599 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
600 "%s: cancel %s scan\n", __func__,
601 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
602 "active" : "passive");
604 /* clear bg scan NOPICK and mark cancel request */
605 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
606 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
607 /* force it to fire asap */
608 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
611 IEEE80211_UNLOCK(ic);
615 * Public access to scan_next for drivers that manage
616 * scanning themselves (e.g. for firmware-based devices).
619 ieee80211_scan_next(struct ieee80211com *ic)
622 * XXX: We might need/want to decouple context here by either:
623 * callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 0, scan_next, ss);
624 * or using a taskqueue. Let's see what kind of problems direct
625 * dispatch has for now.
627 scan_next(ic->ic_scan);
631 * Public access to scan_next for drivers that are not able to scan single
632 * channels (e.g. for firmware-based devices).
635 ieee80211_scan_done(struct ieee80211com *ic)
637 struct ieee80211_scan_state *ss = ic->ic_scan;
639 ss->ss_next = ss->ss_last; /* all channels are complete */
644 * Scan curchan. If this is an active scan and the channel
645 * is not marked passive then send probe request frame(s).
646 * Arrange for the channel change after maxdwell ticks.
649 scan_curchan(struct ieee80211com *ic, unsigned long maxdwell)
651 struct ieee80211_scan_state *ss = ic->ic_scan;
653 if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
654 (ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
655 struct ifnet *ifp = ic->ic_ifp;
659 * Send a broadcast probe request followed by
660 * any specified directed probe requests.
661 * XXX suppress broadcast probe req?
662 * XXX remove dependence on ic/ic->ic_bss
663 * XXX move to policy code?
665 ieee80211_send_probereq(ic->ic_bss,
666 ic->ic_myaddr, ifp->if_broadcastaddr,
667 ifp->if_broadcastaddr,
669 ic->ic_opt_ie, ic->ic_opt_ie_len);
670 for (i = 0; i < ss->ss_nssid; i++)
671 ieee80211_send_probereq(ic->ic_bss,
672 ic->ic_myaddr, ifp->if_broadcastaddr,
673 ifp->if_broadcastaddr,
676 ic->ic_opt_ie, ic->ic_opt_ie_len);
678 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
679 maxdwell, scan_next, ss);
683 * Handle mindwell requirements completed; initiate a channel
684 * change to the next channel asap.
687 scan_mindwell(struct ieee80211com *ic)
689 struct ieee80211_scan_state *ss = ic->ic_scan;
691 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer, 0, scan_next, ss);
695 * Switch to the next channel marked for scanning.
700 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_START | ISCAN_DISCARD)
701 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
702 struct ieee80211com *ic = ss->ss_ic;
703 struct ieee80211_channel *chan;
704 unsigned long maxdwell, scanend;
705 int scanning, scandone;
708 scanning = (ic->ic_flags & IEEE80211_F_SCAN) != 0;
709 IEEE80211_UNLOCK(ic);
710 if (!scanning) /* canceled */
714 scandone = (ss->ss_next >= ss->ss_last) ||
715 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
716 scanend = SCAN_PRIVATE(ss)->ss_scanend;
718 (ss->ss_flags & IEEE80211_SCAN_GOTPICK) == 0 &&
719 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_START) ||
720 time_before(ticks + ss->ss_mindwell, scanend))) {
721 chan = ss->ss_chans[ss->ss_next++];
724 * Watch for truncation due to the scan end time.
726 if (time_after(ticks + ss->ss_maxdwell, scanend))
727 maxdwell = scanend - ticks;
729 maxdwell = ss->ss_maxdwell;
731 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
732 "%s: chan %3d%c -> %3d%c [%s, dwell min %lu max %lu]\n",
734 ieee80211_chan2ieee(ic, ic->ic_curchan),
735 channel_type(ic->ic_curchan),
736 ieee80211_chan2ieee(ic, chan), channel_type(chan),
737 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
738 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
739 "active" : "passive",
740 ss->ss_mindwell, maxdwell);
743 * Potentially change channel and phy mode.
745 change_channel(ic, chan);
748 * Scan curchan. Drivers for "intelligent hardware"
749 * override ic_scan_curchan to tell the device to do
750 * the work. Otherwise we manage the work outselves;
751 * sending a probe request (as needed), and arming the
752 * timeout to switch channels after maxdwell ticks.
754 ic->ic_scan_curchan(ic, maxdwell);
756 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
757 /* clear mindwell lock and initial channel change flush */
758 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
760 ic->ic_scan_end(ic); /* notify driver */
762 * Record scan complete time. Note that we also do
763 * this when canceled so any background scan will
764 * not be restarted for a while.
767 ic->ic_lastscan = ticks;
768 /* return to the bss channel */
769 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
770 ic->ic_curchan != ic->ic_bsschan)
771 change_channel(ic, ic->ic_bsschan);
772 /* clear internal flags and any indication of a pick */
773 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
774 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
777 * If not canceled and scan completed, do post-processing.
778 * If the callback function returns 0, then it wants to
779 * continue/restart scanning. Unfortunately we needed to
780 * notify the driver to end the scan above to avoid having
781 * rx frames alter the scan candidate list.
783 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
784 !ss->ss_ops->scan_end(ss, ic) &&
785 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
786 time_before(ticks + ss->ss_mindwell, scanend)) {
787 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
789 "[ticks %u, dwell min %lu scanend %lu]\n",
791 ticks, ss->ss_mindwell, scanend);
792 ss->ss_next = 0; /* reset to begining */
793 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
794 ic->ic_stats.is_scan_active++;
796 ic->ic_stats.is_scan_passive++;
798 ic->ic_scan_start(ic); /* notify driver */
801 /* past here, scandone is ``true'' if not in bg mode */
802 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
805 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
807 "[ticks %u, dwell min %lu scanend %lu]\n",
808 __func__, scandone ? "done" : "stopped",
809 ticks, ss->ss_mindwell, scanend);
812 * Clear the SCAN bit first in case frames are
813 * pending on the station power save queue. If
814 * we defer this then the dispatch of the frames
815 * may generate a request to cancel scanning.
817 ic->ic_flags &= ~IEEE80211_F_SCAN;
819 * Drop out of power save mode when a scan has
820 * completed. If this scan was prematurely terminated
821 * because it is a background scan then don't notify
822 * the ap; we'll either return to scanning after we
823 * receive the beacon frame or we'll drop out of power
824 * save mode because the beacon indicates we have frames
828 ieee80211_sta_pwrsave(ic, 0);
829 if (ss->ss_next >= ss->ss_last) {
830 ieee80211_notify_scan_done(ic);
831 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
834 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_CANCEL;
836 ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
842 #ifdef IEEE80211_DEBUG
844 dump_probe_beacon(uint8_t subtype, int isnew,
845 const uint8_t mac[IEEE80211_ADDR_LEN],
846 const struct ieee80211_scanparams *sp)
849 printf("[%s] %s%s on chan %u (bss chan %u) ",
850 ether_sprintf(mac), isnew ? "new " : "",
851 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
852 IEEE80211_CHAN2IEEE(sp->curchan), sp->bchan);
853 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
857 printf("[%s] caps 0x%x bintval %u erp 0x%x",
858 ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
859 if (sp->country != NULL) {
861 printf(" country info %*D",
862 sp->country[1], sp->country+2, " ");
865 printf(" country info");
866 for (i = 0; i < sp->country[1]; i++)
867 printf(" %02x", sp->country[i+2]);
873 #endif /* IEEE80211_DEBUG */
876 * Process a beacon or probe response frame.
879 ieee80211_add_scan(struct ieee80211com *ic,
880 const struct ieee80211_scanparams *sp,
881 const struct ieee80211_frame *wh,
882 int subtype, int rssi, int noise, int rstamp)
884 struct ieee80211_scan_state *ss = ic->ic_scan;
887 * Frames received during startup are discarded to avoid
888 * using scan state setup on the initial entry to the timer
889 * callback. This can occur because the device may enable
890 * rx prior to our doing the initial channel change in the
891 * timer routine (we defer the channel change to the timer
892 * code to simplify locking on linux).
894 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
896 #ifdef IEEE80211_DEBUG
897 if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
898 dump_probe_beacon(subtype, 1, wh->i_addr2, sp);
900 if (ss->ss_ops != NULL &&
901 ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise, rstamp)) {
903 * If we've reached the min dwell time terminate
904 * the timer so we'll switch to the next channel.
906 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
907 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
908 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
909 "%s: chan %3d%c min dwell met (%u > %lu)\n",
911 ieee80211_chan2ieee(ic, ic->ic_curchan),
912 channel_type(ic->ic_curchan),
913 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
914 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
916 * NB: trigger at next clock tick or wait for the
919 ic->ic_scan_mindwell(ic);
925 * Timeout/age scan cache entries; called from sta timeout
926 * timer (XXX should be self-contained).
929 ieee80211_scan_timeout(struct ieee80211com *ic)
931 struct ieee80211_scan_state *ss = ic->ic_scan;
933 if (ss->ss_ops != NULL)
934 ss->ss_ops->scan_age(ss);
938 * Mark a scan cache entry after a successful associate.
941 ieee80211_scan_assoc_success(struct ieee80211com *ic, const uint8_t mac[])
943 struct ieee80211_scan_state *ss = ic->ic_scan;
945 if (ss->ss_ops != NULL) {
946 IEEE80211_NOTE_MAC(ic, IEEE80211_MSG_SCAN,
947 mac, "%s", __func__);
948 ss->ss_ops->scan_assoc_success(ss, mac);
953 * Demerit a scan cache entry after failing to associate.
956 ieee80211_scan_assoc_fail(struct ieee80211com *ic,
957 const uint8_t mac[], int reason)
959 struct ieee80211_scan_state *ss = ic->ic_scan;
961 if (ss->ss_ops != NULL) {
962 IEEE80211_NOTE_MAC(ic, IEEE80211_MSG_SCAN, mac,
963 "%s: reason %u", __func__, reason);
964 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
969 * Iterate over the contents of the scan cache.
972 ieee80211_scan_iterate(struct ieee80211com *ic,
973 ieee80211_scan_iter_func *f, void *arg)
975 struct ieee80211_scan_state *ss = ic->ic_scan;
977 if (ss->ss_ops != NULL)
978 ss->ss_ops->scan_iterate(ss, f, arg);
982 * Flush the contents of the scan cache.
985 ieee80211_scan_flush(struct ieee80211com *ic)
987 struct ieee80211_scan_state *ss = ic->ic_scan;
989 if (ss->ss_ops != NULL) {
990 IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
992 ss->ss_ops->scan_flush(ss);