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_callout(void *);
99 static void scan_signal(struct ieee80211_scan_state *ss);
100 static void scan_task(void *, int);
102 MALLOC_DEFINE(M_80211_SCAN, "80211scan", "802.11 scan state");
105 ieee80211_scan_attach(struct ieee80211com *ic)
107 struct scan_state *ss;
109 ss = (struct scan_state *) kmalloc(sizeof(struct scan_state),
110 M_80211_SCAN, M_INTWAIT | M_ZERO);
115 wlan_cv_init(&ss->ss_scan_cv, "scan");
116 callout_init(&ss->ss_scan_timer);
117 TASK_INIT(&ss->ss_scan_task, 0, scan_task, ss);
118 ic->ic_scan = &ss->base;
121 ic->ic_scan_curchan = scan_curchan;
122 ic->ic_scan_mindwell = scan_mindwell;
126 ieee80211_scan_detach(struct ieee80211com *ic)
128 struct ieee80211_scan_state *ss = ic->ic_scan;
131 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
133 ieee80211_draintask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
134 callout_stop(&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 kfree(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;
187 if (ss != NULL && ss->ss_vap == vap) {
188 if (ic->ic_flags & IEEE80211_F_SCAN) {
189 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_ABORT;
192 if (ss->ss_ops != NULL) {
193 ss->ss_ops->scan_detach(ss);
201 * Simple-minded scanner module support.
203 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
204 "wlan_scan_sta", /* IEEE80211_M_IBSS */
205 "wlan_scan_sta", /* IEEE80211_M_STA */
206 "wlan_scan_wds", /* IEEE80211_M_WDS */
207 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */
208 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */
209 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */
210 "wlan_scan_sta", /* IEEE80211_M_MBSS */
212 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
214 const struct ieee80211_scanner *
215 ieee80211_scanner_get(enum ieee80211_opmode mode)
217 if (mode >= IEEE80211_OPMODE_MAX)
219 if (scanners[mode] == NULL)
220 ieee80211_load_module(scan_modnames[mode]);
221 return scanners[mode];
225 ieee80211_scanner_register(enum ieee80211_opmode mode,
226 const struct ieee80211_scanner *scan)
228 if (mode >= IEEE80211_OPMODE_MAX)
230 scanners[mode] = scan;
234 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
235 const struct ieee80211_scanner *scan)
237 if (mode >= IEEE80211_OPMODE_MAX)
239 if (scanners[mode] == scan)
240 scanners[mode] = NULL;
244 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
248 for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
249 if (scanners[m] == scan)
254 * Update common scanner state to reflect the current
255 * operating mode. This is called when the state machine
256 * is transitioned to RUN state w/o scanning--e.g. when
257 * operating in monitor mode. The purpose of this is to
258 * ensure later callbacks find ss_ops set to properly
259 * reflect current operating mode.
262 scan_update_locked(struct ieee80211vap *vap,
263 const struct ieee80211_scanner *scan)
265 struct ieee80211com *ic = vap->iv_ic;
266 struct ieee80211_scan_state *ss = ic->ic_scan;
268 #ifdef IEEE80211_DEBUG
269 if (ss->ss_vap != vap || ss->ss_ops != scan) {
270 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
271 "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
274 ss->ss_vap->iv_ifp->if_xname : "none",
276 ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
277 vap->iv_ifp->if_xname,
278 ieee80211_opmode_name[vap->iv_opmode]);
282 if (ss->ss_ops != scan) {
284 * Switch scanners; detach old, attach new. Special
285 * case where a single scan module implements multiple
286 * policies by using different scan ops but a common
287 * core. We assume if the old and new attach methods
288 * are identical then it's ok to just change ss_ops
289 * and not flush the internal state of the module.
291 if (scan == NULL || ss->ss_ops == NULL ||
292 ss->ss_ops->scan_attach != scan->scan_attach) {
293 if (ss->ss_ops != NULL)
294 ss->ss_ops->scan_detach(ss);
295 if (scan != NULL && !scan->scan_attach(ss)) {
296 /* XXX attach failure */
306 channel_type(const struct ieee80211_channel *c)
308 if (IEEE80211_IS_CHAN_ST(c))
310 if (IEEE80211_IS_CHAN_108A(c))
312 if (IEEE80211_IS_CHAN_108G(c))
314 if (IEEE80211_IS_CHAN_HT(c))
316 if (IEEE80211_IS_CHAN_A(c))
318 if (IEEE80211_IS_CHAN_ANYG(c))
320 if (IEEE80211_IS_CHAN_B(c))
326 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
328 struct ieee80211com *ic = ss->ss_ic;
333 for (i = ss->ss_next; i < ss->ss_last; i++) {
334 const struct ieee80211_channel *c = ss->ss_chans[i];
336 kprintf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
342 #ifdef IEEE80211_DEBUG
344 scan_dump(struct ieee80211_scan_state *ss)
346 struct ieee80211vap *vap = ss->ss_vap;
348 if_printf(vap->iv_ifp, "scan set ");
349 ieee80211_scan_dump_channels(ss);
350 kprintf(" dwell min %lums max %lums\n",
351 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
353 #endif /* IEEE80211_DEBUG */
356 copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
357 int nssid, const struct ieee80211_scan_ssid ssids[])
359 if (nssid > IEEE80211_SCAN_MAX_SSID) {
361 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
362 "%s: too many ssid %d, ignoring all of them\n",
366 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
367 ss->ss_nssid = nssid;
371 * Start a scan unless one is already going.
374 start_scan_locked(const struct ieee80211_scanner *scan,
375 struct ieee80211vap *vap, int flags, u_int duration,
376 u_int mindwell, u_int maxdwell,
377 u_int nssid, const struct ieee80211_scan_ssid ssids[])
379 struct ieee80211com *ic = vap->iv_ic;
380 struct ieee80211_scan_state *ss = ic->ic_scan;
382 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
383 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
384 "%s: scan inhibited by pending channel change\n", __func__);
385 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
386 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
387 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
389 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
390 , duration, mindwell, maxdwell
391 , ieee80211_phymode_name[vap->iv_des_mode]
392 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
393 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
394 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
395 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
396 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
397 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
400 scan_update_locked(vap, scan);
401 if (ss->ss_ops != NULL) {
402 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
403 copy_ssid(vap, ss, nssid, ssids);
405 /* NB: top 4 bits for internal use */
406 ss->ss_flags = flags & 0xfff;
407 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
408 vap->iv_stats.is_scan_active++;
410 vap->iv_stats.is_scan_passive++;
411 if (flags & IEEE80211_SCAN_FLUSH)
412 ss->ss_ops->scan_flush(ss);
414 /* NB: flush frames rx'd before 1st channel change */
415 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
416 SCAN_PRIVATE(ss)->ss_duration = duration;
418 ss->ss_mindwell = mindwell;
419 ss->ss_maxdwell = maxdwell;
420 /* NB: scan_start must be before the scan runtask */
421 ss->ss_ops->scan_start(ss, vap);
422 #ifdef IEEE80211_DEBUG
423 if (ieee80211_msg_scan(vap))
425 #endif /* IEEE80211_DEBUG */
426 ic->ic_flags |= IEEE80211_F_SCAN;
427 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
430 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
431 "%s: %s scan already in progress\n", __func__,
432 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
434 return (ic->ic_flags & IEEE80211_F_SCAN);
438 * Start a scan unless one is already going.
441 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
442 u_int duration, u_int mindwell, u_int maxdwell,
443 u_int nssid, const struct ieee80211_scan_ssid ssids[])
445 struct ieee80211com *ic = vap->iv_ic;
446 const struct ieee80211_scanner *scan;
450 scan = ieee80211_scanner_get(vap->iv_opmode);
452 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
453 "%s: no scanner support for %s mode\n",
454 __func__, ieee80211_opmode_name[vap->iv_opmode]);
459 result = start_scan_locked(scan, vap, flags, duration,
460 mindwell, maxdwell, nssid, ssids);
466 * Check the scan cache for an ap/channel to use; if that
467 * fails then kick off a new scan.
470 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
471 u_int duration, u_int mindwell, u_int maxdwell,
472 u_int nssid, const struct ieee80211_scan_ssid ssids[])
474 struct ieee80211com *ic = vap->iv_ic;
475 struct ieee80211_scan_state *ss = ic->ic_scan;
476 const struct ieee80211_scanner *scan;
479 scan = ieee80211_scanner_get(vap->iv_opmode);
481 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
482 "%s: no scanner support for %s mode\n",
483 __func__, vap->iv_opmode);
489 * Check if there's a list of scan candidates already.
490 * XXX want more than the ap we're currently associated with
493 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
494 "%s: %s scan, %s%s%s%s%s\n"
496 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
497 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
498 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
499 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
500 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
501 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
504 if (ss->ss_ops != scan) {
505 /* XXX re-use cache contents? e.g. adhoc<->sta */
506 flags |= IEEE80211_SCAN_FLUSH;
508 scan_update_locked(vap, scan);
509 if (ss->ss_ops != NULL) {
510 /* XXX verify ss_ops matches vap->iv_opmode */
511 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
513 * Update the ssid list and mark flags so if
514 * we call start_scan it doesn't duplicate work.
516 copy_ssid(vap, ss, nssid, ssids);
517 flags |= IEEE80211_SCAN_NOSSID;
519 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
520 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
521 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
523 * We're not currently scanning and the cache is
524 * deemed hot enough to consult. Lock out others
525 * by marking IEEE80211_F_SCAN while we decide if
526 * something is already in the scan cache we can
527 * use. Also discard any frames that might come
528 * in while temporarily marked as scanning.
530 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
531 ic->ic_flags |= IEEE80211_F_SCAN;
533 /* NB: need to use supplied flags in check */
534 ss->ss_flags = flags & 0xff;
535 result = ss->ss_ops->scan_end(ss, vap);
537 ic->ic_flags &= ~IEEE80211_F_SCAN;
538 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
540 ieee80211_notify_scan_done(vap);
545 result = start_scan_locked(scan, vap, flags, duration,
546 mindwell, maxdwell, nssid, ssids);
552 * Check the scan cache for an ap/channel to use; if that fails
553 * then kick off a scan using the current settings.
556 ieee80211_check_scan_current(struct ieee80211vap *vap)
558 return ieee80211_check_scan(vap,
559 IEEE80211_SCAN_ACTIVE,
560 IEEE80211_SCAN_FOREVER, 0, 0,
561 vap->iv_des_nssid, vap->iv_des_ssid);
565 * Restart a previous scan. If the previous scan completed
566 * then we start again using the existing channel list.
569 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
571 struct ieee80211com *ic = vap->iv_ic;
572 struct ieee80211_scan_state *ss = ic->ic_scan;
573 const struct ieee80211_scanner *scan;
575 scan = ieee80211_scanner_get(vap->iv_opmode);
577 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
578 "%s: no scanner support for %s mode\n",
579 __func__, vap->iv_opmode);
584 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
587 * Go off-channel for a fixed interval that is large
588 * enough to catch most ap's but short enough that
589 * we can return on-channel before our listen interval
592 duration = IEEE80211_SCAN_OFFCHANNEL;
594 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
595 "%s: %s scan, ticks %u duration %lu\n", __func__,
596 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
599 scan_update_locked(vap, scan);
600 if (ss->ss_ops != NULL) {
603 * A background scan does not select a new sta; it
604 * just refreshes the scan cache. Also, indicate
605 * the scan logic should follow the beacon schedule:
606 * we go off-channel and scan for a while, then
607 * return to the bss channel to receive a beacon,
608 * then go off-channel again. All during this time
609 * we notify the ap we're in power save mode. When
610 * the scan is complete we leave power save mode.
611 * If any beacon indicates there are frames pending
612 * for us then we drop out of power save mode
613 * (and background scan) automatically by way of the
614 * usual sta power save logic.
616 ss->ss_flags |= IEEE80211_SCAN_NOPICK
617 | IEEE80211_SCAN_BGSCAN
620 /* if previous scan completed, restart */
621 if (ss->ss_next >= ss->ss_last) {
622 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
623 vap->iv_stats.is_scan_active++;
625 vap->iv_stats.is_scan_passive++;
627 * NB: beware of the scan cache being flushed;
628 * if the channel list is empty use the
629 * scan_start method to populate it.
632 if (ss->ss_last != 0)
633 ss->ss_ops->scan_restart(ss, vap);
635 ss->ss_ops->scan_start(ss, vap);
636 #ifdef IEEE80211_DEBUG
637 if (ieee80211_msg_scan(vap))
639 #endif /* IEEE80211_DEBUG */
642 /* NB: flush frames rx'd before 1st channel change */
643 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
644 SCAN_PRIVATE(ss)->ss_duration = duration;
645 ss->ss_maxdwell = duration;
646 ic->ic_flags |= IEEE80211_F_SCAN;
647 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
648 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
653 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
654 "%s: %s scan already in progress\n", __func__,
655 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
658 /* NB: racey, does it matter? */
659 return (ic->ic_flags & IEEE80211_F_SCAN);
663 * Cancel any scan currently going on for the specified vap.
666 ieee80211_cancel_scan(struct ieee80211vap *vap)
668 struct ieee80211com *ic = vap->iv_ic;
669 struct ieee80211_scan_state *ss = ic->ic_scan;
671 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
673 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
674 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
675 "%s: cancel %s scan\n", __func__,
676 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
677 "active" : "passive");
679 /* clear bg scan NOPICK and mark cancel request */
680 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
681 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
682 /* wake up the scan task */
688 * Cancel any scan currently going on.
691 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
693 struct ieee80211com *ic = vap->iv_ic;
694 struct ieee80211_scan_state *ss = ic->ic_scan;
696 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
697 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
698 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
699 "%s: cancel %s scan\n", __func__,
700 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
701 "active" : "passive");
703 /* clear bg scan NOPICK and mark cancel request */
704 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
705 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
706 /* wake up the scan task */
712 * Public access to scan_next for drivers that manage
713 * scanning themselves (e.g. for firmware-based devices).
716 ieee80211_scan_next(struct ieee80211vap *vap)
718 struct ieee80211com *ic = vap->iv_ic;
719 struct ieee80211_scan_state *ss = ic->ic_scan;
721 /* wake up the scan task */
726 * Public access to scan_next for drivers that are not able to scan single
727 * channels (e.g. for firmware-based devices).
730 ieee80211_scan_done(struct ieee80211vap *vap)
732 struct ieee80211com *ic = vap->iv_ic;
733 struct ieee80211_scan_state *ss;
736 ss->ss_next = ss->ss_last; /* all channels are complete */
741 * Probe the curent channel, if allowed, while scanning.
742 * If the channel is not marked passive-only then send
743 * a probe request immediately. Otherwise mark state and
744 * listen for beacons on the channel; if we receive something
745 * then we'll transmit a probe request.
748 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
750 struct ieee80211com *ic = vap->iv_ic;
751 struct ieee80211_scan_state *ss = ic->ic_scan;
752 struct ifnet *ifp = vap->iv_ifp;
755 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
756 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
760 * Send directed probe requests followed by any
761 * broadcast probe request.
762 * XXX remove dependence on ic/vap->iv_bss
764 for (i = 0; i < ss->ss_nssid; i++)
765 ieee80211_send_probereq(vap->iv_bss,
766 vap->iv_myaddr, ifp->if_broadcastaddr,
767 ifp->if_broadcastaddr,
768 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
769 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
770 ieee80211_send_probereq(vap->iv_bss,
771 vap->iv_myaddr, ifp->if_broadcastaddr,
772 ifp->if_broadcastaddr,
777 * Scan curchan. If this is an active scan and the channel
778 * is not marked passive then send probe request frame(s).
779 * Arrange for the channel change after maxdwell ticks.
782 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
784 struct ieee80211vap *vap = ss->ss_vap;
786 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
787 ieee80211_probe_curchan(vap, 0);
788 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
789 maxdwell, scan_signal_callout, ss);
793 scan_signal_callout(void *arg)
795 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
797 wlan_serialize_enter();
799 wlan_serialize_exit();
803 scan_signal(struct ieee80211_scan_state *ss)
805 wlan_cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv, 0);
809 * Handle mindwell requirements completed; initiate a channel
810 * change to the next channel asap.
813 scan_mindwell(struct ieee80211_scan_state *ss)
815 struct ieee80211com *ic = ss->ss_ic;
822 scan_task(void *arg, int pending)
824 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD)
825 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
826 struct ieee80211vap *vap;
827 struct ieee80211com *ic;
828 struct ieee80211_channel *chan;
829 unsigned long maxdwell, scanend;
832 wlan_serialize_enter();
836 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
837 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) {
838 /* Cancelled before we started */
842 if (ss->ss_next == ss->ss_last) {
843 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
844 "%s: no channels to scan\n", __func__);
848 if (vap->iv_opmode == IEEE80211_M_STA &&
849 vap->iv_state == IEEE80211_S_RUN) {
850 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
851 /* Enable station power save mode */
852 ieee80211_sta_pwrsave(vap, 1);
854 * Use an 1ms delay so the null data frame has a chance
855 * to go out. Minimum one tick.
856 * XXX Should use M_TXCB mechanism to eliminate this.
858 wlan_cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
860 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
865 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
866 ic->ic_scan_start(ic); /* notify driver */
869 scandone = (ss->ss_next >= ss->ss_last) ||
870 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
871 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
872 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
873 time_after(ticks + ss->ss_mindwell, scanend))
876 chan = ss->ss_chans[ss->ss_next++];
879 * Watch for truncation due to the scan end time.
881 if (time_after(ticks + ss->ss_maxdwell, scanend))
882 maxdwell = scanend - ticks;
884 maxdwell = ss->ss_maxdwell;
886 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
887 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
889 ieee80211_chan2ieee(ic, ic->ic_curchan),
890 channel_type(ic->ic_curchan),
891 ieee80211_chan2ieee(ic, chan), channel_type(chan),
892 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
893 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
894 "active" : "passive",
895 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
898 * Potentially change channel and phy mode.
900 ic->ic_curchan = chan;
901 ic->ic_rt = ieee80211_get_ratetable(chan);
903 * Perform the channel change and scan unlocked so the driver
904 * may sleep. Once set_channel returns the hardware has
905 * completed the channel change.
907 ic->ic_set_channel(ic);
908 ieee80211_radiotap_chan_change(ic);
911 * Scan curchan. Drivers for "intelligent hardware"
912 * override ic_scan_curchan to tell the device to do
913 * the work. Otherwise we manage the work outselves;
914 * sending a probe request (as needed), and arming the
915 * timeout to switch channels after maxdwell ticks.
917 * scan_curchan should only pause for the time required to
918 * prepare/initiate the hardware for the scan (if at all), the
919 * below condvar is used to sleep for the channels dwell time
920 * and allows it to be signalled for abort.
922 ic->ic_scan_curchan(ss, maxdwell);
924 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
925 /* clear mindwell lock and initial channel change flush */
926 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
928 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
931 /* Wait to be signalled to scan the next channel */
932 wlan_cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv);
934 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
937 ic->ic_scan_end(ic); /* notify driver */
940 * Record scan complete time. Note that we also do
941 * this when canceled so any background scan will
942 * not be restarted for a while.
945 ic->ic_lastscan = ticks;
946 /* return to the bss channel */
947 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
948 ic->ic_curchan != ic->ic_bsschan) {
949 ieee80211_setupcurchan(ic, ic->ic_bsschan);
950 ic->ic_set_channel(ic);
951 ieee80211_radiotap_chan_change(ic);
953 /* clear internal flags and any indication of a pick */
954 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
955 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
958 * If not canceled and scan completed, do post-processing.
959 * If the callback function returns 0, then it wants to
960 * continue/restart scanning. Unfortunately we needed to
961 * notify the driver to end the scan above to avoid having
962 * rx frames alter the scan candidate list.
964 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
965 !ss->ss_ops->scan_end(ss, vap) &&
966 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
967 time_before(ticks + ss->ss_mindwell, scanend)) {
968 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
970 "[ticks %u, dwell min %lu scanend %lu]\n",
972 ticks, ss->ss_mindwell, scanend);
973 ss->ss_next = 0; /* reset to begining */
974 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
975 vap->iv_stats.is_scan_active++;
977 vap->iv_stats.is_scan_passive++;
979 ss->ss_ops->scan_restart(ss, vap); /* XXX? */
980 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
984 /* past here, scandone is ``true'' if not in bg mode */
985 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
988 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
989 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
990 __func__, scandone ? "done" : "stopped",
991 ticks, ss->ss_mindwell, scanend);
994 * Clear the SCAN bit first in case frames are
995 * pending on the station power save queue. If
996 * we defer this then the dispatch of the frames
997 * may generate a request to cancel scanning.
1000 ic->ic_flags &= ~IEEE80211_F_SCAN;
1002 * Drop out of power save mode when a scan has
1003 * completed. If this scan was prematurely terminated
1004 * because it is a background scan then don't notify
1005 * the ap; we'll either return to scanning after we
1006 * receive the beacon frame or we'll drop out of power
1007 * save mode because the beacon indicates we have frames
1011 ieee80211_sta_pwrsave(vap, 0);
1012 if (ss->ss_next >= ss->ss_last) {
1013 ieee80211_notify_scan_done(vap);
1014 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
1017 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
1018 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
1020 wlan_serialize_exit();
1024 #ifdef IEEE80211_DEBUG
1026 dump_country(const uint8_t *ie)
1028 const struct ieee80211_country_ie *cie =
1029 (const struct ieee80211_country_ie *) ie;
1030 int i, nbands, schan, nchan;
1033 kprintf(" <bogus country ie, len %d>", cie->len);
1036 kprintf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
1037 nbands = (cie->len - 3) / sizeof(cie->band[0]);
1038 for (i = 0; i < nbands; i++) {
1039 schan = cie->band[i].schan;
1040 nchan = cie->band[i].nchan;
1042 kprintf(" %u-%u,%u", schan, schan + nchan-1,
1043 cie->band[i].maxtxpwr);
1045 kprintf(" %u,%u", schan, cie->band[i].maxtxpwr);
1051 dump_probe_beacon(uint8_t subtype, int isnew,
1052 const uint8_t mac[IEEE80211_ADDR_LEN],
1053 const struct ieee80211_scanparams *sp, int rssi)
1056 kprintf("[%6D] %s%s on chan %u (bss chan %u) ",
1057 mac, ":", isnew ? "new " : "",
1058 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1059 sp->chan, sp->bchan);
1060 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1061 kprintf(" rssi %d\n", rssi);
1064 kprintf("[%6D] caps 0x%x bintval %u erp 0x%x",
1065 mac, ":", sp->capinfo, sp->bintval, sp->erp);
1066 if (sp->country != NULL)
1067 dump_country(sp->country);
1071 #endif /* IEEE80211_DEBUG */
1074 * Process a beacon or probe response frame.
1077 ieee80211_add_scan(struct ieee80211vap *vap,
1078 const struct ieee80211_scanparams *sp,
1079 const struct ieee80211_frame *wh,
1080 int subtype, int rssi, int noise)
1082 struct ieee80211com *ic = vap->iv_ic;
1083 struct ieee80211_scan_state *ss = ic->ic_scan;
1087 * Frames received during startup are discarded to avoid
1088 * using scan state setup on the initial entry to the timer
1089 * callback. This can occur because the device may enable
1090 * rx prior to our doing the initial channel change in the
1093 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
1095 #ifdef IEEE80211_DEBUG
1096 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
1097 dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
1099 if (ss->ss_ops != NULL &&
1100 ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise)) {
1102 * If we've reached the min dwell time terminate
1103 * the timer so we'll switch to the next channel.
1105 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
1106 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
1107 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1108 "%s: chan %3d%c min dwell met (%u > %lu)\n",
1110 ieee80211_chan2ieee(ic, ic->ic_curchan),
1111 channel_type(ic->ic_curchan),
1112 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
1113 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
1115 * NB: trigger at next clock tick or wait for the
1118 ic->ic_scan_mindwell(ss);
1124 * Timeout/age scan cache entries; called from sta timeout
1125 * timer (XXX should be self-contained).
1128 ieee80211_scan_timeout(struct ieee80211com *ic)
1130 struct ieee80211_scan_state *ss = ic->ic_scan;
1132 if (ss->ss_ops != NULL)
1133 ss->ss_ops->scan_age(ss);
1137 * Mark a scan cache entry after a successful associate.
1140 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
1142 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1144 if (ss->ss_ops != NULL) {
1145 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
1146 mac, "%s", __func__);
1147 ss->ss_ops->scan_assoc_success(ss, mac);
1152 * Demerit a scan cache entry after failing to associate.
1155 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
1156 const uint8_t mac[], int reason)
1158 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1160 if (ss->ss_ops != NULL) {
1161 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
1162 "%s: reason %u", __func__, reason);
1163 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
1168 * Iterate over the contents of the scan cache.
1171 ieee80211_scan_iterate(struct ieee80211vap *vap,
1172 ieee80211_scan_iter_func *f, void *arg)
1174 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1176 if (ss->ss_ops != NULL)
1177 ss->ss_ops->scan_iterate(ss, f, arg);
1181 * Flush the contents of the scan cache.
1184 ieee80211_scan_flush(struct ieee80211vap *vap)
1186 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1188 if (ss->ss_ops != NULL && ss->ss_vap == vap) {
1189 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", __func__);
1190 ss->ss_ops->scan_flush(ss);
1195 * Check the scan cache for an ap/channel to use; if that
1196 * fails then kick off a new scan.
1198 struct ieee80211_channel *
1199 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
1201 struct ieee80211_scan_state *ss = ic->ic_scan;
1203 if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
1207 if (ss->ss_ops->scan_pickchan == NULL) {
1208 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
1209 "%s: scan module does not support picking a channel, "
1210 "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
1213 return ss->ss_ops->scan_pickchan(ss, flags);