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_media.h>
44 #include <net/ethernet.h>
46 #include <net80211/ieee80211_var.h>
51 struct ieee80211_scan_state base; /* public state */
53 u_int ss_iflags; /* flags used internally */
54 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */
55 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */
56 #define ISCAN_CANCEL 0x0004 /* cancel current scan */
57 #define ISCAN_ABORT 0x0008 /* end the scan immediately */
58 unsigned long ss_chanmindwell; /* min dwell on curchan */
59 unsigned long ss_scanend; /* time scan must stop */
60 u_int ss_duration; /* duration for next scan */
61 struct task ss_scan_task; /* scan execution */
62 struct cv ss_scan_cv; /* scan signal */
63 struct callout ss_scan_timer; /* scan timer */
65 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss)
68 * Amount of time to go off-channel during a background
69 * scan. This value should be large enough to catch most
70 * ap's but short enough that we can return on-channel
71 * before our listen interval expires.
74 * XXX check against configured listen interval
76 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150)
79 * Roaming-related defaults. RSSI thresholds are as returned by the
80 * driver (.5dBm). Transmit rate thresholds are IEEE rate codes (i.e
84 #define ROAM_RSSI_11A_DEFAULT 14 /* 11a bss */
85 #define ROAM_RSSI_11B_DEFAULT 14 /* 11b bss */
86 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* 11b-only bss */
87 /* transmit rate thresholds */
88 #define ROAM_RATE_11A_DEFAULT 2*12 /* 11a bss */
89 #define ROAM_RATE_11B_DEFAULT 2*5 /* 11b bss */
90 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* 11b-only bss */
91 #define ROAM_RATE_HALF_DEFAULT 2*6 /* half-width 11a/g bss */
92 #define ROAM_RATE_QUARTER_DEFAULT 2*3 /* quarter-width 11a/g bss */
93 #define ROAM_MCS_11N_DEFAULT (1 | IEEE80211_RATE_MCS) /* 11n bss */
95 static void scan_curchan(struct ieee80211_scan_state *, unsigned long);
96 static void scan_mindwell(struct ieee80211_scan_state *);
97 static void scan_signal(void *);
98 static void scan_task(void *, int);
100 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
103 ieee80211_scan_attach(struct ieee80211com *ic)
105 struct scan_state *ss;
107 ss = (struct scan_state *) malloc(sizeof(struct scan_state),
108 M_80211_SCAN, M_NOWAIT | M_ZERO);
113 callout_init_mtx(&ss->ss_scan_timer, IEEE80211_LOCK_OBJ(ic), 0);
114 cv_init(&ss->ss_scan_cv, "scan");
115 TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss);
116 ic->ic_scan = &ss->base;
119 ic->ic_scan_curchan = scan_curchan;
120 ic->ic_scan_mindwell = scan_mindwell;
124 ieee80211_scan_detach(struct ieee80211com *ic)
126 struct ieee80211_scan_state *ss = ic->ic_scan;
130 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
132 IEEE80211_UNLOCK(ic);
133 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
134 callout_drain(&SCAN_PRIVATE(ss)->ss_scan_timer);
135 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0,
136 ("scan still running"));
137 if (ss->ss_ops != NULL) {
138 ss->ss_ops->scan_detach(ss);
142 free(SCAN_PRIVATE(ss), M_80211_SCAN);
146 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
147 [IEEE80211_MODE_11A] = { .rssi = ROAM_RSSI_11A_DEFAULT,
148 .rate = ROAM_RATE_11A_DEFAULT },
149 [IEEE80211_MODE_11G] = { .rssi = ROAM_RSSI_11B_DEFAULT,
150 .rate = ROAM_RATE_11B_DEFAULT },
151 [IEEE80211_MODE_11B] = { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
152 .rate = ROAM_RATE_11BONLY_DEFAULT },
153 [IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
154 .rate = ROAM_RATE_11A_DEFAULT },
155 [IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
156 .rate = ROAM_RATE_11A_DEFAULT },
157 [IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
158 .rate = ROAM_RATE_11A_DEFAULT },
159 [IEEE80211_MODE_HALF] = { .rssi = ROAM_RSSI_11A_DEFAULT,
160 .rate = ROAM_RATE_HALF_DEFAULT },
161 [IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
162 .rate = ROAM_RATE_QUARTER_DEFAULT },
163 [IEEE80211_MODE_11NA] = { .rssi = ROAM_RSSI_11A_DEFAULT,
164 .rate = ROAM_MCS_11N_DEFAULT },
165 [IEEE80211_MODE_11NG] = { .rssi = ROAM_RSSI_11B_DEFAULT,
166 .rate = ROAM_MCS_11N_DEFAULT },
170 ieee80211_scan_vattach(struct ieee80211vap *vap)
172 vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
173 vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
174 vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
176 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
177 memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
181 ieee80211_scan_vdetach(struct ieee80211vap *vap)
183 struct ieee80211com *ic = vap->iv_ic;
184 struct ieee80211_scan_state *ss;
188 if (ss != NULL && ss->ss_vap == vap) {
189 if (ic->ic_flags & IEEE80211_F_SCAN) {
190 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
193 if (ss->ss_ops != NULL) {
194 ss->ss_ops->scan_detach(ss);
199 IEEE80211_UNLOCK(ic);
203 * Simple-minded scanner module support.
205 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
206 "wlan_scan_sta", /* IEEE80211_M_IBSS */
207 "wlan_scan_sta", /* IEEE80211_M_STA */
208 "wlan_scan_wds", /* IEEE80211_M_WDS */
209 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */
210 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */
211 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */
212 "wlan_scan_sta", /* IEEE80211_M_MBSS */
214 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
216 const struct ieee80211_scanner *
217 ieee80211_scanner_get(enum ieee80211_opmode mode)
219 if (mode >= IEEE80211_OPMODE_MAX)
221 if (scanners[mode] == NULL)
222 ieee80211_load_module(scan_modnames[mode]);
223 return scanners[mode];
227 ieee80211_scanner_register(enum ieee80211_opmode mode,
228 const struct ieee80211_scanner *scan)
230 if (mode >= IEEE80211_OPMODE_MAX)
232 scanners[mode] = scan;
236 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
237 const struct ieee80211_scanner *scan)
239 if (mode >= IEEE80211_OPMODE_MAX)
241 if (scanners[mode] == scan)
242 scanners[mode] = NULL;
246 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
250 for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
251 if (scanners[m] == scan)
256 * Update common scanner state to reflect the current
257 * operating mode. This is called when the state machine
258 * is transitioned to RUN state w/o scanning--e.g. when
259 * operating in monitor mode. The purpose of this is to
260 * ensure later callbacks find ss_ops set to properly
261 * reflect current operating mode.
264 scan_update_locked(struct ieee80211vap *vap,
265 const struct ieee80211_scanner *scan)
267 struct ieee80211com *ic = vap->iv_ic;
268 struct ieee80211_scan_state *ss = ic->ic_scan;
270 IEEE80211_LOCK_ASSERT(ic);
272 #ifdef IEEE80211_DEBUG
273 if (ss->ss_vap != vap || ss->ss_ops != scan) {
274 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
275 "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
278 ss->ss_vap->iv_ifp->if_xname : "none",
280 ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
281 vap->iv_ifp->if_xname,
282 ieee80211_opmode_name[vap->iv_opmode]);
286 if (ss->ss_ops != scan) {
288 * Switch scanners; detach old, attach new. Special
289 * case where a single scan module implements multiple
290 * policies by using different scan ops but a common
291 * core. We assume if the old and new attach methods
292 * are identical then it's ok to just change ss_ops
293 * and not flush the internal state of the module.
295 if (scan == NULL || ss->ss_ops == NULL ||
296 ss->ss_ops->scan_attach != scan->scan_attach) {
297 if (ss->ss_ops != NULL)
298 ss->ss_ops->scan_detach(ss);
299 if (scan != NULL && !scan->scan_attach(ss)) {
300 /* XXX attach failure */
310 channel_type(const struct ieee80211_channel *c)
312 if (IEEE80211_IS_CHAN_ST(c))
314 if (IEEE80211_IS_CHAN_108A(c))
316 if (IEEE80211_IS_CHAN_108G(c))
318 if (IEEE80211_IS_CHAN_HT(c))
320 if (IEEE80211_IS_CHAN_A(c))
322 if (IEEE80211_IS_CHAN_ANYG(c))
324 if (IEEE80211_IS_CHAN_B(c))
330 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
332 struct ieee80211com *ic = ss->ss_ic;
337 for (i = ss->ss_next; i < ss->ss_last; i++) {
338 const struct ieee80211_channel *c = ss->ss_chans[i];
340 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
346 #ifdef IEEE80211_DEBUG
348 scan_dump(struct ieee80211_scan_state *ss)
350 struct ieee80211vap *vap = ss->ss_vap;
352 if_printf(vap->iv_ifp, "scan set ");
353 ieee80211_scan_dump_channels(ss);
354 printf(" dwell min %lums max %lums\n",
355 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
357 #endif /* IEEE80211_DEBUG */
360 copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
361 int nssid, const struct ieee80211_scan_ssid ssids[])
363 if (nssid > IEEE80211_SCAN_MAX_SSID) {
365 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
366 "%s: too many ssid %d, ignoring all of them\n",
370 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
371 ss->ss_nssid = nssid;
375 * Start a scan unless one is already going.
378 start_scan_locked(const struct ieee80211_scanner *scan,
379 struct ieee80211vap *vap, int flags, u_int duration,
380 u_int mindwell, u_int maxdwell,
381 u_int nssid, const struct ieee80211_scan_ssid ssids[])
383 struct ieee80211com *ic = vap->iv_ic;
384 struct ieee80211_scan_state *ss = ic->ic_scan;
386 IEEE80211_LOCK_ASSERT(ic);
388 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
389 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
390 "%s: scan inhibited by pending channel change\n", __func__);
391 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
392 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
393 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
395 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
396 , duration, mindwell, maxdwell
397 , ieee80211_phymode_name[vap->iv_des_mode]
398 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
399 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
400 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
401 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
402 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
403 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
406 scan_update_locked(vap, scan);
407 if (ss->ss_ops != NULL) {
408 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
409 copy_ssid(vap, ss, nssid, ssids);
411 /* NB: top 4 bits for internal use */
412 ss->ss_flags = flags & 0xfff;
413 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
414 vap->iv_stats.is_scan_active++;
416 vap->iv_stats.is_scan_passive++;
417 if (flags & IEEE80211_SCAN_FLUSH)
418 ss->ss_ops->scan_flush(ss);
419 if (flags & IEEE80211_SCAN_BGSCAN)
420 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
422 /* NB: flush frames rx'd before 1st channel change */
423 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
424 SCAN_PRIVATE(ss)->ss_duration = duration;
426 ss->ss_mindwell = mindwell;
427 ss->ss_maxdwell = maxdwell;
428 /* NB: scan_start must be before the scan runtask */
429 ss->ss_ops->scan_start(ss, vap);
430 #ifdef IEEE80211_DEBUG
431 if (ieee80211_msg_scan(vap))
433 #endif /* IEEE80211_DEBUG */
434 ic->ic_flags |= IEEE80211_F_SCAN;
435 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
439 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
440 "%s: %s scan already in progress\n", __func__,
441 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
447 * Start a scan unless one is already going.
450 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
451 u_int duration, u_int mindwell, u_int maxdwell,
452 u_int nssid, const struct ieee80211_scan_ssid ssids[])
454 struct ieee80211com *ic = vap->iv_ic;
455 const struct ieee80211_scanner *scan;
458 scan = ieee80211_scanner_get(vap->iv_opmode);
460 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
461 "%s: no scanner support for %s mode\n",
462 __func__, ieee80211_opmode_name[vap->iv_opmode]);
468 result = start_scan_locked(scan, vap, flags, duration,
469 mindwell, maxdwell, nssid, ssids);
470 IEEE80211_UNLOCK(ic);
476 * Check the scan cache for an ap/channel to use; if that
477 * fails then kick off a new scan.
480 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
481 u_int duration, u_int mindwell, u_int maxdwell,
482 u_int nssid, const struct ieee80211_scan_ssid ssids[])
484 struct ieee80211com *ic = vap->iv_ic;
485 struct ieee80211_scan_state *ss = ic->ic_scan;
486 const struct ieee80211_scanner *scan;
489 scan = ieee80211_scanner_get(vap->iv_opmode);
491 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
492 "%s: no scanner support for %s mode\n",
493 __func__, vap->iv_opmode);
499 * Check if there's a list of scan candidates already.
500 * XXX want more than the ap we're currently associated with
504 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
505 "%s: %s scan, %s%s%s%s%s\n"
507 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
508 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
509 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
510 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
511 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
512 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
515 if (ss->ss_ops != scan) {
516 /* XXX re-use cache contents? e.g. adhoc<->sta */
517 flags |= IEEE80211_SCAN_FLUSH;
519 scan_update_locked(vap, scan);
520 if (ss->ss_ops != NULL) {
521 /* XXX verify ss_ops matches vap->iv_opmode */
522 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
524 * Update the ssid list and mark flags so if
525 * we call start_scan it doesn't duplicate work.
527 copy_ssid(vap, ss, nssid, ssids);
528 flags |= IEEE80211_SCAN_NOSSID;
530 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
531 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
532 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
534 * We're not currently scanning and the cache is
535 * deemed hot enough to consult. Lock out others
536 * by marking IEEE80211_F_SCAN while we decide if
537 * something is already in the scan cache we can
538 * use. Also discard any frames that might come
539 * in while temporarily marked as scanning.
541 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
542 ic->ic_flags |= IEEE80211_F_SCAN;
544 /* NB: need to use supplied flags in check */
545 ss->ss_flags = flags & 0xff;
546 result = ss->ss_ops->scan_end(ss, vap);
548 ic->ic_flags &= ~IEEE80211_F_SCAN;
549 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
551 ieee80211_notify_scan_done(vap);
552 IEEE80211_UNLOCK(ic);
557 result = start_scan_locked(scan, vap, flags, duration,
558 mindwell, maxdwell, nssid, ssids);
559 IEEE80211_UNLOCK(ic);
565 * Check the scan cache for an ap/channel to use; if that fails
566 * then kick off a scan using the current settings.
569 ieee80211_check_scan_current(struct ieee80211vap *vap)
571 return ieee80211_check_scan(vap,
572 IEEE80211_SCAN_ACTIVE,
573 IEEE80211_SCAN_FOREVER, 0, 0,
574 vap->iv_des_nssid, vap->iv_des_ssid);
578 * Restart a previous scan. If the previous scan completed
579 * then we start again using the existing channel list.
582 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
584 struct ieee80211com *ic = vap->iv_ic;
585 struct ieee80211_scan_state *ss = ic->ic_scan;
586 const struct ieee80211_scanner *scan;
588 scan = ieee80211_scanner_get(vap->iv_opmode);
590 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
591 "%s: no scanner support for %s mode\n",
592 __func__, vap->iv_opmode);
598 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
601 * Go off-channel for a fixed interval that is large
602 * enough to catch most ap's but short enough that
603 * we can return on-channel before our listen interval
606 duration = IEEE80211_SCAN_OFFCHANNEL;
608 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
609 "%s: %s scan, ticks %u duration %lu\n", __func__,
610 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
613 scan_update_locked(vap, scan);
614 if (ss->ss_ops != NULL) {
617 * A background scan does not select a new sta; it
618 * just refreshes the scan cache. Also, indicate
619 * the scan logic should follow the beacon schedule:
620 * we go off-channel and scan for a while, then
621 * return to the bss channel to receive a beacon,
622 * then go off-channel again. All during this time
623 * we notify the ap we're in power save mode. When
624 * the scan is complete we leave power save mode.
625 * If any beacon indicates there are frames pending
626 * for us then we drop out of power save mode
627 * (and background scan) automatically by way of the
628 * usual sta power save logic.
630 ss->ss_flags |= IEEE80211_SCAN_NOPICK
631 | IEEE80211_SCAN_BGSCAN
634 /* if previous scan completed, restart */
635 if (ss->ss_next >= ss->ss_last) {
636 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
637 vap->iv_stats.is_scan_active++;
639 vap->iv_stats.is_scan_passive++;
641 * NB: beware of the scan cache being flushed;
642 * if the channel list is empty use the
643 * scan_start method to populate it.
646 if (ss->ss_last != 0)
647 ss->ss_ops->scan_restart(ss, vap);
649 ss->ss_ops->scan_start(ss, vap);
650 #ifdef IEEE80211_DEBUG
651 if (ieee80211_msg_scan(vap))
653 #endif /* IEEE80211_DEBUG */
656 /* NB: flush frames rx'd before 1st channel change */
657 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
658 SCAN_PRIVATE(ss)->ss_duration = duration;
659 ss->ss_maxdwell = duration;
660 ic->ic_flags |= IEEE80211_F_SCAN;
661 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
662 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
667 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
668 "%s: %s scan already in progress\n", __func__,
669 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
671 IEEE80211_UNLOCK(ic);
673 /* NB: racey, does it matter? */
674 return (ic->ic_flags & IEEE80211_F_SCAN);
678 * Cancel any scan currently going on for the specified vap.
681 ieee80211_cancel_scan(struct ieee80211vap *vap)
683 struct ieee80211com *ic = vap->iv_ic;
684 struct ieee80211_scan_state *ss = ic->ic_scan;
687 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
689 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
690 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
691 "%s: cancel %s scan\n", __func__,
692 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
693 "active" : "passive");
695 /* clear bg scan NOPICK and mark cancel request */
696 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
697 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
698 /* wake up the scan task */
701 IEEE80211_UNLOCK(ic);
705 * Cancel any scan currently going on.
708 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
710 struct ieee80211com *ic = vap->iv_ic;
711 struct ieee80211_scan_state *ss = ic->ic_scan;
714 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
715 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
716 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
717 "%s: cancel %s scan\n", __func__,
718 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
719 "active" : "passive");
721 /* clear bg scan NOPICK and mark cancel request */
722 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
723 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
724 /* wake up the scan task */
727 IEEE80211_UNLOCK(ic);
731 * Public access to scan_next for drivers that manage
732 * scanning themselves (e.g. for firmware-based devices).
735 ieee80211_scan_next(struct ieee80211vap *vap)
737 struct ieee80211com *ic = vap->iv_ic;
738 struct ieee80211_scan_state *ss = ic->ic_scan;
740 /* wake up the scan task */
743 IEEE80211_UNLOCK(ic);
747 * Public access to scan_next for drivers that are not able to scan single
748 * channels (e.g. for firmware-based devices).
751 ieee80211_scan_done(struct ieee80211vap *vap)
753 struct ieee80211com *ic = vap->iv_ic;
754 struct ieee80211_scan_state *ss;
758 ss->ss_next = ss->ss_last; /* all channels are complete */
760 IEEE80211_UNLOCK(ic);
764 * Probe the curent channel, if allowed, while scanning.
765 * If the channel is not marked passive-only then send
766 * a probe request immediately. Otherwise mark state and
767 * listen for beacons on the channel; if we receive something
768 * then we'll transmit a probe request.
771 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
773 struct ieee80211com *ic = vap->iv_ic;
774 struct ieee80211_scan_state *ss = ic->ic_scan;
775 struct ifnet *ifp = vap->iv_ifp;
778 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
779 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
783 * Send directed probe requests followed by any
784 * broadcast probe request.
785 * XXX remove dependence on ic/vap->iv_bss
787 for (i = 0; i < ss->ss_nssid; i++)
788 ieee80211_send_probereq(vap->iv_bss,
789 vap->iv_myaddr, ifp->if_broadcastaddr,
790 ifp->if_broadcastaddr,
791 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
792 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
793 ieee80211_send_probereq(vap->iv_bss,
794 vap->iv_myaddr, ifp->if_broadcastaddr,
795 ifp->if_broadcastaddr,
800 * Scan curchan. If this is an active scan and the channel
801 * is not marked passive then send probe request frame(s).
802 * Arrange for the channel change after maxdwell ticks.
805 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
807 struct ieee80211vap *vap = ss->ss_vap;
809 IEEE80211_LOCK(vap->iv_ic);
810 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
811 ieee80211_probe_curchan(vap, 0);
812 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
813 maxdwell, scan_signal, ss);
814 IEEE80211_UNLOCK(vap->iv_ic);
818 scan_signal(void *arg)
820 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
822 IEEE80211_LOCK_ASSERT(ss->ss_ic);
824 cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv);
828 * Handle mindwell requirements completed; initiate a channel
829 * change to the next channel asap.
832 scan_mindwell(struct ieee80211_scan_state *ss)
834 struct ieee80211com *ic = ss->ss_ic;
838 IEEE80211_UNLOCK(ic);
842 scan_task(void *arg, int pending)
844 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD)
845 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
846 struct ieee80211vap *vap = ss->ss_vap;
847 struct ieee80211com *ic = ss->ss_ic;
848 struct ieee80211_channel *chan;
849 unsigned long maxdwell, scanend;
853 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
854 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) {
855 /* Cancelled before we started */
859 if (ss->ss_next == ss->ss_last) {
860 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
861 "%s: no channels to scan\n", __func__);
866 if (vap->iv_opmode == IEEE80211_M_STA &&
867 vap->iv_state == IEEE80211_S_RUN) {
868 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
869 /* Enable station power save mode */
870 vap->iv_sta_ps(vap, 1);
872 * Use an 1ms delay so the null data frame has a chance
874 * XXX Should use M_TXCB mechanism to eliminate this.
876 cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
877 IEEE80211_LOCK_OBJ(ic), hz / 1000);
878 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
883 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
884 IEEE80211_UNLOCK(ic);
885 ic->ic_scan_start(ic); /* notify driver */
889 scandone = (ss->ss_next >= ss->ss_last) ||
890 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
891 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
892 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
893 time_after(ticks + ss->ss_mindwell, scanend))
896 chan = ss->ss_chans[ss->ss_next++];
899 * Watch for truncation due to the scan end time.
901 if (time_after(ticks + ss->ss_maxdwell, scanend))
902 maxdwell = scanend - ticks;
904 maxdwell = ss->ss_maxdwell;
906 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
907 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
909 ieee80211_chan2ieee(ic, ic->ic_curchan),
910 channel_type(ic->ic_curchan),
911 ieee80211_chan2ieee(ic, chan), channel_type(chan),
912 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
913 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
914 "active" : "passive",
915 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
918 * Potentially change channel and phy mode.
920 ic->ic_curchan = chan;
921 ic->ic_rt = ieee80211_get_ratetable(chan);
922 IEEE80211_UNLOCK(ic);
924 * Perform the channel change and scan unlocked so the driver
925 * may sleep. Once set_channel returns the hardware has
926 * completed the channel change.
928 ic->ic_set_channel(ic);
929 ieee80211_radiotap_chan_change(ic);
932 * Scan curchan. Drivers for "intelligent hardware"
933 * override ic_scan_curchan to tell the device to do
934 * the work. Otherwise we manage the work outselves;
935 * sending a probe request (as needed), and arming the
936 * timeout to switch channels after maxdwell ticks.
938 * scan_curchan should only pause for the time required to
939 * prepare/initiate the hardware for the scan (if at all), the
940 * below condvar is used to sleep for the channels dwell time
941 * and allows it to be signalled for abort.
943 ic->ic_scan_curchan(ss, maxdwell);
946 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
947 /* clear mindwell lock and initial channel change flush */
948 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
950 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
953 /* Wait to be signalled to scan the next channel */
954 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic));
956 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
959 IEEE80211_UNLOCK(ic);
960 ic->ic_scan_end(ic); /* notify driver */
964 * Since a cancellation may have occured during one of the
965 * driver calls (whilst unlocked), update scandone.
968 ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0)) {
970 if_printf(vap->iv_ifp,
971 "%s: OOPS! scan cancelled during driver call!\n",
974 scandone |= ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0);
977 * Record scan complete time. Note that we also do
978 * this when canceled so any background scan will
979 * not be restarted for a while.
982 ic->ic_lastscan = ticks;
983 /* return to the bss channel */
984 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
985 ic->ic_curchan != ic->ic_bsschan) {
986 ieee80211_setupcurchan(ic, ic->ic_bsschan);
987 IEEE80211_UNLOCK(ic);
988 ic->ic_set_channel(ic);
989 ieee80211_radiotap_chan_change(ic);
992 /* clear internal flags and any indication of a pick */
993 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
994 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
997 * If not canceled and scan completed, do post-processing.
998 * If the callback function returns 0, then it wants to
999 * continue/restart scanning. Unfortunately we needed to
1000 * notify the driver to end the scan above to avoid having
1001 * rx frames alter the scan candidate list.
1003 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
1004 !ss->ss_ops->scan_end(ss, vap) &&
1005 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
1006 time_before(ticks + ss->ss_mindwell, scanend)) {
1007 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1008 "%s: done, restart "
1009 "[ticks %u, dwell min %lu scanend %lu]\n",
1011 ticks, ss->ss_mindwell, scanend);
1012 ss->ss_next = 0; /* reset to begining */
1013 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
1014 vap->iv_stats.is_scan_active++;
1016 vap->iv_stats.is_scan_passive++;
1018 ss->ss_ops->scan_restart(ss, vap); /* XXX? */
1019 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
1020 IEEE80211_UNLOCK(ic);
1024 /* past here, scandone is ``true'' if not in bg mode */
1025 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
1028 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1029 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
1030 __func__, scandone ? "done" : "stopped",
1031 ticks, ss->ss_mindwell, scanend);
1034 * Clear the SCAN bit first in case frames are
1035 * pending on the station power save queue. If
1036 * we defer this then the dispatch of the frames
1037 * may generate a request to cancel scanning.
1040 ic->ic_flags &= ~IEEE80211_F_SCAN;
1042 * Drop out of power save mode when a scan has
1043 * completed. If this scan was prematurely terminated
1044 * because it is a background scan then don't notify
1045 * the ap; we'll either return to scanning after we
1046 * receive the beacon frame or we'll drop out of power
1047 * save mode because the beacon indicates we have frames
1051 vap->iv_sta_ps(vap, 0);
1052 if (ss->ss_next >= ss->ss_last) {
1053 ieee80211_notify_scan_done(vap);
1054 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
1057 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
1058 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
1059 IEEE80211_UNLOCK(ic);
1063 #ifdef IEEE80211_DEBUG
1065 dump_country(const uint8_t *ie)
1067 const struct ieee80211_country_ie *cie =
1068 (const struct ieee80211_country_ie *) ie;
1069 int i, nbands, schan, nchan;
1072 printf(" <bogus country ie, len %d>", cie->len);
1075 printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
1076 nbands = (cie->len - 3) / sizeof(cie->band[0]);
1077 for (i = 0; i < nbands; i++) {
1078 schan = cie->band[i].schan;
1079 nchan = cie->band[i].nchan;
1081 printf(" %u-%u,%u", schan, schan + nchan-1,
1082 cie->band[i].maxtxpwr);
1084 printf(" %u,%u", schan, cie->band[i].maxtxpwr);
1090 dump_probe_beacon(uint8_t subtype, int isnew,
1091 const uint8_t mac[IEEE80211_ADDR_LEN],
1092 const struct ieee80211_scanparams *sp, int rssi)
1095 printf("[%s] %s%s on chan %u (bss chan %u) ",
1096 ether_sprintf(mac), isnew ? "new " : "",
1097 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1098 sp->chan, sp->bchan);
1099 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1100 printf(" rssi %d\n", rssi);
1103 printf("[%s] caps 0x%x bintval %u erp 0x%x",
1104 ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1105 if (sp->country != NULL)
1106 dump_country(sp->country);
1110 #endif /* IEEE80211_DEBUG */
1113 * Process a beacon or probe response frame.
1116 ieee80211_add_scan(struct ieee80211vap *vap,
1117 const struct ieee80211_scanparams *sp,
1118 const struct ieee80211_frame *wh,
1119 int subtype, int rssi, int noise)
1121 struct ieee80211com *ic = vap->iv_ic;
1122 struct ieee80211_scan_state *ss = ic->ic_scan;
1126 * Frames received during startup are discarded to avoid
1127 * using scan state setup on the initial entry to the timer
1128 * callback. This can occur because the device may enable
1129 * rx prior to our doing the initial channel change in the
1132 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
1134 #ifdef IEEE80211_DEBUG
1135 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
1136 dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
1138 if (ss->ss_ops != NULL &&
1139 ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise)) {
1141 * If we've reached the min dwell time terminate
1142 * the timer so we'll switch to the next channel.
1144 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
1145 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
1146 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1147 "%s: chan %3d%c min dwell met (%u > %lu)\n",
1149 ieee80211_chan2ieee(ic, ic->ic_curchan),
1150 channel_type(ic->ic_curchan),
1151 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
1152 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
1154 * NB: trigger at next clock tick or wait for the
1157 ic->ic_scan_mindwell(ss);
1163 * Timeout/age scan cache entries; called from sta timeout
1164 * timer (XXX should be self-contained).
1167 ieee80211_scan_timeout(struct ieee80211com *ic)
1169 struct ieee80211_scan_state *ss = ic->ic_scan;
1171 if (ss->ss_ops != NULL)
1172 ss->ss_ops->scan_age(ss);
1176 * Mark a scan cache entry after a successful associate.
1179 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
1181 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1183 if (ss->ss_ops != NULL) {
1184 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
1185 mac, "%s", __func__);
1186 ss->ss_ops->scan_assoc_success(ss, mac);
1191 * Demerit a scan cache entry after failing to associate.
1194 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
1195 const uint8_t mac[], int reason)
1197 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1199 if (ss->ss_ops != NULL) {
1200 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
1201 "%s: reason %u", __func__, reason);
1202 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
1207 * Iterate over the contents of the scan cache.
1210 ieee80211_scan_iterate(struct ieee80211vap *vap,
1211 ieee80211_scan_iter_func *f, void *arg)
1213 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1215 if (ss->ss_ops != NULL)
1216 ss->ss_ops->scan_iterate(ss, f, arg);
1220 * Flush the contents of the scan cache.
1223 ieee80211_scan_flush(struct ieee80211vap *vap)
1225 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1227 if (ss->ss_ops != NULL && ss->ss_vap == vap) {
1228 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", __func__);
1229 ss->ss_ops->scan_flush(ss);
1234 * Check the scan cache for an ap/channel to use; if that
1235 * fails then kick off a new scan.
1237 struct ieee80211_channel *
1238 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
1240 struct ieee80211_scan_state *ss = ic->ic_scan;
1242 IEEE80211_LOCK_ASSERT(ic);
1244 if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
1248 if (ss->ss_ops->scan_pickchan == NULL) {
1249 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
1250 "%s: scan module does not support picking a channel, "
1251 "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
1254 return ss->ss_ops->scan_pickchan(ss, flags);