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.
25 * $FreeBSD: head/sys/net80211/ieee80211_scan.c 195618 2009-07-11 15:02:45Z rpaulo $
30 * IEEE 802.11 scanning support.
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/condvar.h>
40 #include <sys/socket.h>
43 #include <net/if_media.h>
44 #include <net/ethernet.h>
45 #include <net/route.h>
47 #include <netproto/802_11/ieee80211_var.h>
52 struct ieee80211_scan_state base; /* public state */
54 u_int ss_iflags; /* flags used internally */
55 #define ISCAN_MINDWELL 0x0001 /* min dwell time reached */
56 #define ISCAN_DISCARD 0x0002 /* discard rx'd frames */
57 #define ISCAN_CANCEL 0x0004 /* cancel current scan */
58 #define ISCAN_ABORT 0x0008 /* end the scan immediately */
59 unsigned long ss_chanmindwell; /* min dwell on curchan */
60 unsigned long ss_scanend; /* time scan must stop */
61 u_int ss_duration; /* duration for next scan */
62 struct task ss_scan_task; /* scan execution */
63 struct cv ss_scan_cv; /* scan signal */
64 struct callout ss_scan_timer; /* scan timer */
66 #define SCAN_PRIVATE(ss) ((struct scan_state *) ss)
69 * Amount of time to go off-channel during a background
70 * scan. This value should be large enough to catch most
71 * ap's but short enough that we can return on-channel
72 * before our listen interval expires.
75 * XXX check against configured listen interval
77 #define IEEE80211_SCAN_OFFCHANNEL msecs_to_ticks(150)
80 * Roaming-related defaults. RSSI thresholds are as returned by the
81 * driver (.5dBm). Transmit rate thresholds are IEEE rate codes (i.e
85 #define ROAM_RSSI_11A_DEFAULT 14 /* 11a bss */
86 #define ROAM_RSSI_11B_DEFAULT 14 /* 11b bss */
87 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* 11b-only bss */
88 /* transmit rate thresholds */
89 #define ROAM_RATE_11A_DEFAULT 2*12 /* 11a bss */
90 #define ROAM_RATE_11B_DEFAULT 2*5 /* 11b bss */
91 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* 11b-only bss */
92 #define ROAM_RATE_HALF_DEFAULT 2*6 /* half-width 11a/g bss */
93 #define ROAM_RATE_QUARTER_DEFAULT 2*3 /* quarter-width 11a/g bss */
94 #define ROAM_MCS_11N_DEFAULT (1 | IEEE80211_RATE_MCS) /* 11n bss */
96 static void scan_curchan(struct ieee80211_scan_state *, unsigned long);
97 static void scan_mindwell(struct ieee80211_scan_state *);
98 static void scan_signal(void *);
99 static void scan_task(void *, int);
101 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
104 ieee80211_scan_attach(struct ieee80211com *ic)
106 struct scan_state *ss;
108 ss = (struct scan_state *) kmalloc(sizeof(struct scan_state),
109 M_80211_SCAN, M_INTWAIT | M_ZERO);
114 cv_init(&ss->ss_scan_cv, "scan");
115 callout_init(&ss->ss_scan_timer);
116 TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss);
117 ic->ic_scan = &ss->base;
120 ic->ic_scan_curchan = scan_curchan;
121 ic->ic_scan_mindwell = scan_mindwell;
125 ieee80211_scan_detach(struct ieee80211com *ic)
127 struct ieee80211_scan_state *ss = ic->ic_scan;
131 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
133 IEEE80211_UNLOCK(ic);
134 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
135 callout_stop(&SCAN_PRIVATE(ss)->ss_scan_timer);
136 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0,
137 ("scan still running"));
138 if (ss->ss_ops != NULL) {
139 ss->ss_ops->scan_detach(ss);
143 kfree(SCAN_PRIVATE(ss), M_80211_SCAN);
147 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
148 [IEEE80211_MODE_11A] = { .rssi = ROAM_RSSI_11A_DEFAULT,
149 .rate = ROAM_RATE_11A_DEFAULT },
150 [IEEE80211_MODE_11G] = { .rssi = ROAM_RSSI_11B_DEFAULT,
151 .rate = ROAM_RATE_11B_DEFAULT },
152 [IEEE80211_MODE_11B] = { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
153 .rate = ROAM_RATE_11BONLY_DEFAULT },
154 [IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
155 .rate = ROAM_RATE_11A_DEFAULT },
156 [IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
157 .rate = ROAM_RATE_11A_DEFAULT },
158 [IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
159 .rate = ROAM_RATE_11A_DEFAULT },
160 [IEEE80211_MODE_HALF] = { .rssi = ROAM_RSSI_11A_DEFAULT,
161 .rate = ROAM_RATE_HALF_DEFAULT },
162 [IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
163 .rate = ROAM_RATE_QUARTER_DEFAULT },
164 [IEEE80211_MODE_11NA] = { .rssi = ROAM_RSSI_11A_DEFAULT,
165 .rate = ROAM_MCS_11N_DEFAULT },
166 [IEEE80211_MODE_11NG] = { .rssi = ROAM_RSSI_11B_DEFAULT,
167 .rate = ROAM_MCS_11N_DEFAULT },
171 ieee80211_scan_vattach(struct ieee80211vap *vap)
173 vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
174 vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
175 vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
177 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
178 memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
182 ieee80211_scan_vdetach(struct ieee80211vap *vap)
184 struct ieee80211com *ic = vap->iv_ic;
185 struct ieee80211_scan_state *ss;
189 if (ss != NULL && ss->ss_vap == vap) {
190 if (ic->ic_flags & IEEE80211_F_SCAN) {
191 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
194 if (ss->ss_ops != NULL) {
195 ss->ss_ops->scan_detach(ss);
200 IEEE80211_UNLOCK(ic);
204 * Simple-minded scanner module support.
206 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
207 "wlan_scan_sta", /* IEEE80211_M_IBSS */
208 "wlan_scan_sta", /* IEEE80211_M_STA */
209 "wlan_scan_wds", /* IEEE80211_M_WDS */
210 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */
211 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */
212 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */
213 "wlan_scan_sta", /* IEEE80211_M_MBSS */
215 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
217 const struct ieee80211_scanner *
218 ieee80211_scanner_get(enum ieee80211_opmode mode)
220 if (mode >= IEEE80211_OPMODE_MAX)
222 if (scanners[mode] == NULL)
223 ieee80211_load_module(scan_modnames[mode]);
224 return scanners[mode];
228 ieee80211_scanner_register(enum ieee80211_opmode mode,
229 const struct ieee80211_scanner *scan)
231 if (mode >= IEEE80211_OPMODE_MAX)
233 scanners[mode] = scan;
237 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
238 const struct ieee80211_scanner *scan)
240 if (mode >= IEEE80211_OPMODE_MAX)
242 if (scanners[mode] == scan)
243 scanners[mode] = NULL;
247 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
251 for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
252 if (scanners[m] == scan)
257 * Update common scanner state to reflect the current
258 * operating mode. This is called when the state machine
259 * is transitioned to RUN state w/o scanning--e.g. when
260 * operating in monitor mode. The purpose of this is to
261 * ensure later callbacks find ss_ops set to properly
262 * reflect current operating mode.
265 scan_update_locked(struct ieee80211vap *vap,
266 const struct ieee80211_scanner *scan)
268 struct ieee80211com *ic = vap->iv_ic;
269 struct ieee80211_scan_state *ss = ic->ic_scan;
271 IEEE80211_LOCK_ASSERT(ic);
273 #ifdef IEEE80211_DEBUG
274 if (ss->ss_vap != vap || ss->ss_ops != scan) {
275 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
276 "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
279 ss->ss_vap->iv_ifp->if_xname : "none",
281 ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
282 vap->iv_ifp->if_xname,
283 ieee80211_opmode_name[vap->iv_opmode]);
287 if (ss->ss_ops != scan) {
289 * Switch scanners; detach old, attach new. Special
290 * case where a single scan module implements multiple
291 * policies by using different scan ops but a common
292 * core. We assume if the old and new attach methods
293 * are identical then it's ok to just change ss_ops
294 * and not flush the internal state of the module.
296 if (scan == NULL || ss->ss_ops == NULL ||
297 ss->ss_ops->scan_attach != scan->scan_attach) {
298 if (ss->ss_ops != NULL)
299 ss->ss_ops->scan_detach(ss);
300 if (scan != NULL && !scan->scan_attach(ss)) {
301 /* XXX attach failure */
311 channel_type(const struct ieee80211_channel *c)
313 if (IEEE80211_IS_CHAN_ST(c))
315 if (IEEE80211_IS_CHAN_108A(c))
317 if (IEEE80211_IS_CHAN_108G(c))
319 if (IEEE80211_IS_CHAN_HT(c))
321 if (IEEE80211_IS_CHAN_A(c))
323 if (IEEE80211_IS_CHAN_ANYG(c))
325 if (IEEE80211_IS_CHAN_B(c))
331 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
333 struct ieee80211com *ic = ss->ss_ic;
338 for (i = ss->ss_next; i < ss->ss_last; i++) {
339 const struct ieee80211_channel *c = ss->ss_chans[i];
341 kprintf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
347 #ifdef IEEE80211_DEBUG
349 scan_dump(struct ieee80211_scan_state *ss)
351 struct ieee80211vap *vap = ss->ss_vap;
353 if_printf(vap->iv_ifp, "scan set ");
354 ieee80211_scan_dump_channels(ss);
355 kprintf(" dwell min %lums max %lums\n",
356 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
358 #endif /* IEEE80211_DEBUG */
361 copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
362 int nssid, const struct ieee80211_scan_ssid ssids[])
364 if (nssid > IEEE80211_SCAN_MAX_SSID) {
366 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
367 "%s: too many ssid %d, ignoring all of them\n",
371 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
372 ss->ss_nssid = nssid;
376 * Start a scan unless one is already going.
379 start_scan_locked(const struct ieee80211_scanner *scan,
380 struct ieee80211vap *vap, int flags, u_int duration,
381 u_int mindwell, u_int maxdwell,
382 u_int nssid, const struct ieee80211_scan_ssid ssids[])
384 struct ieee80211com *ic = vap->iv_ic;
385 struct ieee80211_scan_state *ss = ic->ic_scan;
387 IEEE80211_LOCK_ASSERT(ic);
389 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
390 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
391 "%s: scan inhibited by pending channel change\n", __func__);
392 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
393 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
394 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
396 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
397 , duration, mindwell, maxdwell
398 , ieee80211_phymode_name[vap->iv_des_mode]
399 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
400 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
401 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
402 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
403 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
404 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
407 scan_update_locked(vap, scan);
408 if (ss->ss_ops != NULL) {
409 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
410 copy_ssid(vap, 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 vap->iv_stats.is_scan_active++;
417 vap->iv_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 SCAN_PRIVATE(ss)->ss_duration = duration;
425 ss->ss_mindwell = mindwell;
426 ss->ss_maxdwell = maxdwell;
427 /* NB: scan_start must be before the scan runtask */
428 ss->ss_ops->scan_start(ss, vap);
429 #ifdef IEEE80211_DEBUG
430 if (ieee80211_msg_scan(vap))
432 #endif /* IEEE80211_DEBUG */
433 ic->ic_flags |= IEEE80211_F_SCAN;
434 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
437 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
438 "%s: %s scan already in progress\n", __func__,
439 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
441 return (ic->ic_flags & IEEE80211_F_SCAN);
445 * Start a scan unless one is already going.
448 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
449 u_int duration, u_int mindwell, u_int maxdwell,
450 u_int nssid, const struct ieee80211_scan_ssid ssids[])
452 struct ieee80211com *ic = vap->iv_ic;
453 const struct ieee80211_scanner *scan;
457 scan = ieee80211_scanner_get(vap->iv_opmode);
459 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
460 "%s: no scanner support for %s mode\n",
461 __func__, ieee80211_opmode_name[vap->iv_opmode]);
467 result = start_scan_locked(scan, vap, flags, duration,
468 mindwell, maxdwell, nssid, ssids);
469 IEEE80211_UNLOCK(ic);
475 * Check the scan cache for an ap/channel to use; if that
476 * fails then kick off a new scan.
479 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
480 u_int duration, u_int mindwell, u_int maxdwell,
481 u_int nssid, const struct ieee80211_scan_ssid ssids[])
483 struct ieee80211com *ic = vap->iv_ic;
484 struct ieee80211_scan_state *ss = ic->ic_scan;
485 const struct ieee80211_scanner *scan;
488 scan = ieee80211_scanner_get(vap->iv_opmode);
490 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
491 "%s: no scanner support for %s mode\n",
492 __func__, vap->iv_opmode);
498 * Check if there's a list of scan candidates already.
499 * XXX want more than the ap we're currently associated with
503 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
504 "%s: %s scan, %s%s%s%s%s\n"
506 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
507 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
508 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
509 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
510 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
511 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
514 if (ss->ss_ops != scan) {
515 /* XXX re-use cache contents? e.g. adhoc<->sta */
516 flags |= IEEE80211_SCAN_FLUSH;
518 scan_update_locked(vap, scan);
519 if (ss->ss_ops != NULL) {
520 /* XXX verify ss_ops matches vap->iv_opmode */
521 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
523 * Update the ssid list and mark flags so if
524 * we call start_scan it doesn't duplicate work.
526 copy_ssid(vap, ss, nssid, ssids);
527 flags |= IEEE80211_SCAN_NOSSID;
529 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
530 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
531 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
533 * We're not currently scanning and the cache is
534 * deemed hot enough to consult. Lock out others
535 * by marking IEEE80211_F_SCAN while we decide if
536 * something is already in the scan cache we can
537 * use. Also discard any frames that might come
538 * in while temporarily marked as scanning.
540 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
541 ic->ic_flags |= IEEE80211_F_SCAN;
543 /* NB: need to use supplied flags in check */
544 ss->ss_flags = flags & 0xff;
545 result = ss->ss_ops->scan_end(ss, vap);
547 ic->ic_flags &= ~IEEE80211_F_SCAN;
548 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
550 ieee80211_notify_scan_done(vap);
551 IEEE80211_UNLOCK(ic);
556 result = start_scan_locked(scan, vap, flags, duration,
557 mindwell, maxdwell, nssid, ssids);
558 IEEE80211_UNLOCK(ic);
564 * Check the scan cache for an ap/channel to use; if that fails
565 * then kick off a scan using the current settings.
568 ieee80211_check_scan_current(struct ieee80211vap *vap)
570 return ieee80211_check_scan(vap,
571 IEEE80211_SCAN_ACTIVE,
572 IEEE80211_SCAN_FOREVER, 0, 0,
573 vap->iv_des_nssid, vap->iv_des_ssid);
577 * Restart a previous scan. If the previous scan completed
578 * then we start again using the existing channel list.
581 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
583 struct ieee80211com *ic = vap->iv_ic;
584 struct ieee80211_scan_state *ss = ic->ic_scan;
585 const struct ieee80211_scanner *scan;
587 scan = ieee80211_scanner_get(vap->iv_opmode);
589 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
590 "%s: no scanner support for %s mode\n",
591 __func__, vap->iv_opmode);
597 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
600 * Go off-channel for a fixed interval that is large
601 * enough to catch most ap's but short enough that
602 * we can return on-channel before our listen interval
605 duration = IEEE80211_SCAN_OFFCHANNEL;
607 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
608 "%s: %s scan, ticks %u duration %lu\n", __func__,
609 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
612 scan_update_locked(vap, scan);
613 if (ss->ss_ops != NULL) {
616 * A background scan does not select a new sta; it
617 * just refreshes the scan cache. Also, indicate
618 * the scan logic should follow the beacon schedule:
619 * we go off-channel and scan for a while, then
620 * return to the bss channel to receive a beacon,
621 * then go off-channel again. All during this time
622 * we notify the ap we're in power save mode. When
623 * the scan is complete we leave power save mode.
624 * If any beacon indicates there are frames pending
625 * for us then we drop out of power save mode
626 * (and background scan) automatically by way of the
627 * usual sta power save logic.
629 ss->ss_flags |= IEEE80211_SCAN_NOPICK
630 | IEEE80211_SCAN_BGSCAN
633 /* if previous scan completed, restart */
634 if (ss->ss_next >= ss->ss_last) {
635 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
636 vap->iv_stats.is_scan_active++;
638 vap->iv_stats.is_scan_passive++;
640 * NB: beware of the scan cache being flushed;
641 * if the channel list is empty use the
642 * scan_start method to populate it.
645 if (ss->ss_last != 0)
646 ss->ss_ops->scan_restart(ss, vap);
648 ss->ss_ops->scan_start(ss, vap);
649 #ifdef IEEE80211_DEBUG
650 if (ieee80211_msg_scan(vap))
652 #endif /* IEEE80211_DEBUG */
655 /* NB: flush frames rx'd before 1st channel change */
656 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
657 SCAN_PRIVATE(ss)->ss_duration = duration;
658 ss->ss_maxdwell = duration;
659 ic->ic_flags |= IEEE80211_F_SCAN;
660 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
661 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
666 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
667 "%s: %s scan already in progress\n", __func__,
668 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
670 IEEE80211_UNLOCK(ic);
672 /* NB: racey, does it matter? */
673 return (ic->ic_flags & IEEE80211_F_SCAN);
677 * Cancel any scan currently going on for the specified vap.
680 ieee80211_cancel_scan(struct ieee80211vap *vap)
682 struct ieee80211com *ic = vap->iv_ic;
683 struct ieee80211_scan_state *ss = ic->ic_scan;
686 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
688 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
689 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
690 "%s: cancel %s scan\n", __func__,
691 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
692 "active" : "passive");
694 /* clear bg scan NOPICK and mark cancel request */
695 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
696 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
697 /* wake up the scan task */
700 IEEE80211_UNLOCK(ic);
704 * Cancel any scan currently going on.
707 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
709 struct ieee80211com *ic = vap->iv_ic;
710 struct ieee80211_scan_state *ss = ic->ic_scan;
713 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
714 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
715 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
716 "%s: cancel %s scan\n", __func__,
717 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
718 "active" : "passive");
720 /* clear bg scan NOPICK and mark cancel request */
721 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
722 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
723 /* wake up the scan task */
726 IEEE80211_UNLOCK(ic);
730 * Public access to scan_next for drivers that manage
731 * scanning themselves (e.g. for firmware-based devices).
734 ieee80211_scan_next(struct ieee80211vap *vap)
736 struct ieee80211com *ic = vap->iv_ic;
737 struct ieee80211_scan_state *ss = ic->ic_scan;
739 /* wake up the scan task */
742 IEEE80211_UNLOCK(ic);
746 * Public access to scan_next for drivers that are not able to scan single
747 * channels (e.g. for firmware-based devices).
750 ieee80211_scan_done(struct ieee80211vap *vap)
752 struct ieee80211com *ic = vap->iv_ic;
753 struct ieee80211_scan_state *ss;
757 ss->ss_next = ss->ss_last; /* all channels are complete */
759 IEEE80211_UNLOCK(ic);
763 * Probe the curent channel, if allowed, while scanning.
764 * If the channel is not marked passive-only then send
765 * a probe request immediately. Otherwise mark state and
766 * listen for beacons on the channel; if we receive something
767 * then we'll transmit a probe request.
770 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
772 struct ieee80211com *ic = vap->iv_ic;
773 struct ieee80211_scan_state *ss = ic->ic_scan;
774 struct ifnet *ifp = vap->iv_ifp;
777 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
778 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
782 * Send directed probe requests followed by any
783 * broadcast probe request.
784 * XXX remove dependence on ic/vap->iv_bss
786 for (i = 0; i < ss->ss_nssid; i++)
787 ieee80211_send_probereq(vap->iv_bss,
788 vap->iv_myaddr, ifp->if_broadcastaddr,
789 ifp->if_broadcastaddr,
790 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
791 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
792 ieee80211_send_probereq(vap->iv_bss,
793 vap->iv_myaddr, ifp->if_broadcastaddr,
794 ifp->if_broadcastaddr,
799 * Scan curchan. If this is an active scan and the channel
800 * is not marked passive then send probe request frame(s).
801 * Arrange for the channel change after maxdwell ticks.
804 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
806 struct ieee80211vap *vap = ss->ss_vap;
808 IEEE80211_LOCK(vap->iv_ic);
809 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
810 ieee80211_probe_curchan(vap, 0);
811 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
812 maxdwell, scan_signal, ss);
813 IEEE80211_UNLOCK(vap->iv_ic);
817 scan_signal(void *arg)
819 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
821 IEEE80211_LOCK(ss->ss_ic);
822 cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv);
823 IEEE80211_UNLOCK(ss->ss_ic);
827 * Handle mindwell requirements completed; initiate a channel
828 * change to the next channel asap.
831 scan_mindwell(struct ieee80211_scan_state *ss)
833 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__);
865 if (vap->iv_opmode == IEEE80211_M_STA &&
866 vap->iv_state == IEEE80211_S_RUN) {
867 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
868 /* Enable station power save mode */
869 ieee80211_sta_pwrsave(vap, 1);
871 * Use an 1ms delay so the null data frame has a chance
873 * XXX Should use M_TXCB mechanism to eliminate this.
875 cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
876 IEEE80211_LOCK_OBJ(ic), hz / 1000);
877 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
882 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
883 IEEE80211_UNLOCK(ic);
884 ic->ic_scan_start(ic); /* notify driver */
888 scandone = (ss->ss_next >= ss->ss_last) ||
889 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
890 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
891 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
892 time_after(ticks + ss->ss_mindwell, scanend))
895 chan = ss->ss_chans[ss->ss_next++];
898 * Watch for truncation due to the scan end time.
900 if (time_after(ticks + ss->ss_maxdwell, scanend))
901 maxdwell = scanend - ticks;
903 maxdwell = ss->ss_maxdwell;
905 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
906 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
908 ieee80211_chan2ieee(ic, ic->ic_curchan),
909 channel_type(ic->ic_curchan),
910 ieee80211_chan2ieee(ic, chan), channel_type(chan),
911 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
912 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
913 "active" : "passive",
914 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
917 * Potentially change channel and phy mode.
919 ic->ic_curchan = chan;
920 ic->ic_rt = ieee80211_get_ratetable(chan);
921 IEEE80211_UNLOCK(ic);
923 * Perform the channel change and scan unlocked so the driver
924 * may sleep. Once set_channel returns the hardware has
925 * completed the channel change.
927 ic->ic_set_channel(ic);
928 ieee80211_radiotap_chan_change(ic);
931 * Scan curchan. Drivers for "intelligent hardware"
932 * override ic_scan_curchan to tell the device to do
933 * the work. Otherwise we manage the work outselves;
934 * sending a probe request (as needed), and arming the
935 * timeout to switch channels after maxdwell ticks.
937 * scan_curchan should only pause for the time required to
938 * prepare/initiate the hardware for the scan (if at all), the
939 * below condvar is used to sleep for the channels dwell time
940 * and allows it to be signalled for abort.
942 ic->ic_scan_curchan(ss, maxdwell);
945 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
946 /* clear mindwell lock and initial channel change flush */
947 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
949 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
952 /* Wait to be signalled to scan the next channel */
953 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic));
955 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
958 IEEE80211_UNLOCK(ic);
959 ic->ic_scan_end(ic); /* notify driver */
963 * Record scan complete time. Note that we also do
964 * this when canceled so any background scan will
965 * not be restarted for a while.
968 ic->ic_lastscan = ticks;
969 /* return to the bss channel */
970 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
971 ic->ic_curchan != ic->ic_bsschan) {
972 ieee80211_setupcurchan(ic, ic->ic_bsschan);
973 IEEE80211_UNLOCK(ic);
974 ic->ic_set_channel(ic);
975 ieee80211_radiotap_chan_change(ic);
978 /* clear internal flags and any indication of a pick */
979 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
980 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
983 * If not canceled and scan completed, do post-processing.
984 * If the callback function returns 0, then it wants to
985 * continue/restart scanning. Unfortunately we needed to
986 * notify the driver to end the scan above to avoid having
987 * rx frames alter the scan candidate list.
989 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
990 !ss->ss_ops->scan_end(ss, vap) &&
991 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
992 time_before(ticks + ss->ss_mindwell, scanend)) {
993 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
995 "[ticks %u, dwell min %lu scanend %lu]\n",
997 ticks, ss->ss_mindwell, scanend);
998 ss->ss_next = 0; /* reset to begining */
999 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
1000 vap->iv_stats.is_scan_active++;
1002 vap->iv_stats.is_scan_passive++;
1004 ss->ss_ops->scan_restart(ss, vap); /* XXX? */
1005 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
1006 IEEE80211_UNLOCK(ic);
1010 /* past here, scandone is ``true'' if not in bg mode */
1011 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
1014 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1015 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
1016 __func__, scandone ? "done" : "stopped",
1017 ticks, ss->ss_mindwell, scanend);
1020 * Clear the SCAN bit first in case frames are
1021 * pending on the station power save queue. If
1022 * we defer this then the dispatch of the frames
1023 * may generate a request to cancel scanning.
1026 ic->ic_flags &= ~IEEE80211_F_SCAN;
1028 * Drop out of power save mode when a scan has
1029 * completed. If this scan was prematurely terminated
1030 * because it is a background scan then don't notify
1031 * the ap; we'll either return to scanning after we
1032 * receive the beacon frame or we'll drop out of power
1033 * save mode because the beacon indicates we have frames
1037 ieee80211_sta_pwrsave(vap, 0);
1038 if (ss->ss_next >= ss->ss_last) {
1039 ieee80211_notify_scan_done(vap);
1040 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
1043 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
1044 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
1045 IEEE80211_UNLOCK(ic);
1049 #ifdef IEEE80211_DEBUG
1051 dump_country(const uint8_t *ie)
1053 const struct ieee80211_country_ie *cie =
1054 (const struct ieee80211_country_ie *) ie;
1055 int i, nbands, schan, nchan;
1058 kprintf(" <bogus country ie, len %d>", cie->len);
1061 kprintf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
1062 nbands = (cie->len - 3) / sizeof(cie->band[0]);
1063 for (i = 0; i < nbands; i++) {
1064 schan = cie->band[i].schan;
1065 nchan = cie->band[i].nchan;
1067 kprintf(" %u-%u,%u", schan, schan + nchan-1,
1068 cie->band[i].maxtxpwr);
1070 kprintf(" %u,%u", schan, cie->band[i].maxtxpwr);
1076 dump_probe_beacon(uint8_t subtype, int isnew,
1077 const uint8_t mac[IEEE80211_ADDR_LEN],
1078 const struct ieee80211_scanparams *sp, int rssi)
1081 kprintf("[%6D] %s%s on chan %u (bss chan %u) ",
1082 mac, ":", isnew ? "new " : "",
1083 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1084 sp->chan, sp->bchan);
1085 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1086 kprintf(" rssi %d\n", rssi);
1089 kprintf("[%6D] caps 0x%x bintval %u erp 0x%x",
1090 mac, ":", sp->capinfo, sp->bintval, sp->erp);
1091 if (sp->country != NULL)
1092 dump_country(sp->country);
1096 #endif /* IEEE80211_DEBUG */
1099 * Process a beacon or probe response frame.
1102 ieee80211_add_scan(struct ieee80211vap *vap,
1103 const struct ieee80211_scanparams *sp,
1104 const struct ieee80211_frame *wh,
1105 int subtype, int rssi, int noise)
1107 struct ieee80211com *ic = vap->iv_ic;
1108 struct ieee80211_scan_state *ss = ic->ic_scan;
1112 * Frames received during startup are discarded to avoid
1113 * using scan state setup on the initial entry to the timer
1114 * callback. This can occur because the device may enable
1115 * rx prior to our doing the initial channel change in the
1118 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
1120 #ifdef IEEE80211_DEBUG
1121 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
1122 dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
1124 if (ss->ss_ops != NULL &&
1125 ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise)) {
1127 * If we've reached the min dwell time terminate
1128 * the timer so we'll switch to the next channel.
1130 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
1131 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
1132 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1133 "%s: chan %3d%c min dwell met (%u > %lu)\n",
1135 ieee80211_chan2ieee(ic, ic->ic_curchan),
1136 channel_type(ic->ic_curchan),
1137 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
1138 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
1140 * NB: trigger at next clock tick or wait for the
1143 ic->ic_scan_mindwell(ss);
1149 * Timeout/age scan cache entries; called from sta timeout
1150 * timer (XXX should be self-contained).
1153 ieee80211_scan_timeout(struct ieee80211com *ic)
1155 struct ieee80211_scan_state *ss = ic->ic_scan;
1157 if (ss->ss_ops != NULL)
1158 ss->ss_ops->scan_age(ss);
1162 * Mark a scan cache entry after a successful associate.
1165 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
1167 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1169 if (ss->ss_ops != NULL) {
1170 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
1171 mac, "%s", __func__);
1172 ss->ss_ops->scan_assoc_success(ss, mac);
1177 * Demerit a scan cache entry after failing to associate.
1180 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
1181 const uint8_t mac[], int reason)
1183 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1185 if (ss->ss_ops != NULL) {
1186 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
1187 "%s: reason %u", __func__, reason);
1188 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
1193 * Iterate over the contents of the scan cache.
1196 ieee80211_scan_iterate(struct ieee80211vap *vap,
1197 ieee80211_scan_iter_func *f, void *arg)
1199 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1201 if (ss->ss_ops != NULL)
1202 ss->ss_ops->scan_iterate(ss, f, arg);
1206 * Flush the contents of the scan cache.
1209 ieee80211_scan_flush(struct ieee80211vap *vap)
1211 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1213 if (ss->ss_ops != NULL && ss->ss_vap == vap) {
1214 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", __func__);
1215 ss->ss_ops->scan_flush(ss);
1220 * Check the scan cache for an ap/channel to use; if that
1221 * fails then kick off a new scan.
1223 struct ieee80211_channel *
1224 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
1226 struct ieee80211_scan_state *ss = ic->ic_scan;
1228 IEEE80211_LOCK_ASSERT(ic);
1230 if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
1234 if (ss->ss_ops->scan_pickchan == NULL) {
1235 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
1236 "%s: scan module does not support picking a channel, "
1237 "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
1240 return ss->ss_ops->scan_pickchan(ss, flags);