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 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0,
135 ("scan still running"));
136 if (ss->ss_ops != NULL) {
137 ss->ss_ops->scan_detach(ss);
141 free(SCAN_PRIVATE(ss), M_80211_SCAN);
145 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
146 [IEEE80211_MODE_11A] = { .rssi = ROAM_RSSI_11A_DEFAULT,
147 .rate = ROAM_RATE_11A_DEFAULT },
148 [IEEE80211_MODE_11G] = { .rssi = ROAM_RSSI_11B_DEFAULT,
149 .rate = ROAM_RATE_11B_DEFAULT },
150 [IEEE80211_MODE_11B] = { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
151 .rate = ROAM_RATE_11BONLY_DEFAULT },
152 [IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
153 .rate = ROAM_RATE_11A_DEFAULT },
154 [IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
155 .rate = ROAM_RATE_11A_DEFAULT },
156 [IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
157 .rate = ROAM_RATE_11A_DEFAULT },
158 [IEEE80211_MODE_HALF] = { .rssi = ROAM_RSSI_11A_DEFAULT,
159 .rate = ROAM_RATE_HALF_DEFAULT },
160 [IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
161 .rate = ROAM_RATE_QUARTER_DEFAULT },
162 [IEEE80211_MODE_11NA] = { .rssi = ROAM_RSSI_11A_DEFAULT,
163 .rate = ROAM_MCS_11N_DEFAULT },
164 [IEEE80211_MODE_11NG] = { .rssi = ROAM_RSSI_11B_DEFAULT,
165 .rate = ROAM_MCS_11N_DEFAULT },
169 ieee80211_scan_vattach(struct ieee80211vap *vap)
171 vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
172 vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
173 vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
175 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
176 memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
180 ieee80211_scan_vdetach(struct ieee80211vap *vap)
182 struct ieee80211com *ic = vap->iv_ic;
183 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);
198 IEEE80211_UNLOCK(ic);
202 * Simple-minded scanner module support.
204 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
205 "wlan_scan_sta", /* IEEE80211_M_IBSS */
206 "wlan_scan_sta", /* IEEE80211_M_STA */
207 "wlan_scan_wds", /* IEEE80211_M_WDS */
208 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */
209 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */
210 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */
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 IEEE80211_LOCK_ASSERT(ic);
270 #ifdef IEEE80211_DEBUG
271 if (ss->ss_vap != vap || ss->ss_ops != scan) {
272 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
273 "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
276 ss->ss_vap->iv_ifp->if_xname : "none",
278 ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
279 vap->iv_ifp->if_xname,
280 ieee80211_opmode_name[vap->iv_opmode]);
284 if (ss->ss_ops != scan) {
286 * Switch scanners; detach old, attach new. Special
287 * case where a single scan module implements multiple
288 * policies by using different scan ops but a common
289 * core. We assume if the old and new attach methods
290 * are identical then it's ok to just change ss_ops
291 * and not flush the internal state of the module.
293 if (scan == NULL || ss->ss_ops == NULL ||
294 ss->ss_ops->scan_attach != scan->scan_attach) {
295 if (ss->ss_ops != NULL)
296 ss->ss_ops->scan_detach(ss);
297 if (scan != NULL && !scan->scan_attach(ss)) {
298 /* XXX attach failure */
308 channel_type(const struct ieee80211_channel *c)
310 if (IEEE80211_IS_CHAN_ST(c))
312 if (IEEE80211_IS_CHAN_108A(c))
314 if (IEEE80211_IS_CHAN_108G(c))
316 if (IEEE80211_IS_CHAN_HT(c))
318 if (IEEE80211_IS_CHAN_A(c))
320 if (IEEE80211_IS_CHAN_ANYG(c))
322 if (IEEE80211_IS_CHAN_B(c))
328 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
330 struct ieee80211com *ic = ss->ss_ic;
335 for (i = ss->ss_next; i < ss->ss_last; i++) {
336 const struct ieee80211_channel *c = ss->ss_chans[i];
338 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
344 #ifdef IEEE80211_DEBUG
346 scan_dump(struct ieee80211_scan_state *ss)
348 struct ieee80211vap *vap = ss->ss_vap;
350 if_printf(vap->iv_ifp, "scan set ");
351 ieee80211_scan_dump_channels(ss);
352 printf(" dwell min %lums max %lums\n",
353 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
355 #endif /* IEEE80211_DEBUG */
358 copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
359 int nssid, const struct ieee80211_scan_ssid ssids[])
361 if (nssid > IEEE80211_SCAN_MAX_SSID) {
363 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
364 "%s: too many ssid %d, ignoring all of them\n",
368 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
369 ss->ss_nssid = nssid;
373 * Start a scan unless one is already going.
376 start_scan_locked(const struct ieee80211_scanner *scan,
377 struct ieee80211vap *vap, int flags, u_int duration,
378 u_int mindwell, u_int maxdwell,
379 u_int nssid, const struct ieee80211_scan_ssid ssids[])
381 struct ieee80211com *ic = vap->iv_ic;
382 struct ieee80211_scan_state *ss = ic->ic_scan;
384 IEEE80211_LOCK_ASSERT(ic);
386 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
387 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
388 "%s: scan inhibited by pending channel change\n", __func__);
389 } else if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
390 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
391 "%s: %s scan, duration %u mindwell %u maxdwell %u, desired mode %s, %s%s%s%s%s%s\n"
393 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
394 , duration, mindwell, maxdwell
395 , ieee80211_phymode_name[vap->iv_des_mode]
396 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
397 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
398 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
399 , flags & IEEE80211_SCAN_NOBCAST ? ", nobcast" : ""
400 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
401 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
404 scan_update_locked(vap, scan);
405 if (ss->ss_ops != NULL) {
406 if ((flags & IEEE80211_SCAN_NOSSID) == 0)
407 copy_ssid(vap, ss, nssid, ssids);
409 /* NB: top 4 bits for internal use */
410 ss->ss_flags = flags & 0xfff;
411 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
412 vap->iv_stats.is_scan_active++;
414 vap->iv_stats.is_scan_passive++;
415 if (flags & IEEE80211_SCAN_FLUSH)
416 ss->ss_ops->scan_flush(ss);
418 /* NB: flush frames rx'd before 1st channel change */
419 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
420 SCAN_PRIVATE(ss)->ss_duration = duration;
422 ss->ss_mindwell = mindwell;
423 ss->ss_maxdwell = maxdwell;
424 /* NB: scan_start must be before the scan runtask */
425 ss->ss_ops->scan_start(ss, vap);
426 #ifdef IEEE80211_DEBUG
427 if (ieee80211_msg_scan(vap))
429 #endif /* IEEE80211_DEBUG */
430 ic->ic_flags |= IEEE80211_F_SCAN;
431 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
434 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
435 "%s: %s scan already in progress\n", __func__,
436 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
438 return (ic->ic_flags & IEEE80211_F_SCAN);
442 * Start a scan unless one is already going.
445 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
446 u_int duration, u_int mindwell, u_int maxdwell,
447 u_int nssid, const struct ieee80211_scan_ssid ssids[])
449 struct ieee80211com *ic = vap->iv_ic;
450 const struct ieee80211_scanner *scan;
453 scan = ieee80211_scanner_get(vap->iv_opmode);
455 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
456 "%s: no scanner support for %s mode\n",
457 __func__, ieee80211_opmode_name[vap->iv_opmode]);
463 result = start_scan_locked(scan, vap, flags, duration,
464 mindwell, maxdwell, nssid, ssids);
465 IEEE80211_UNLOCK(ic);
471 * Check the scan cache for an ap/channel to use; if that
472 * fails then kick off a new scan.
475 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
476 u_int duration, u_int mindwell, u_int maxdwell,
477 u_int nssid, const struct ieee80211_scan_ssid ssids[])
479 struct ieee80211com *ic = vap->iv_ic;
480 struct ieee80211_scan_state *ss = ic->ic_scan;
481 const struct ieee80211_scanner *scan;
484 scan = ieee80211_scanner_get(vap->iv_opmode);
486 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
487 "%s: no scanner support for %s mode\n",
488 __func__, vap->iv_opmode);
494 * Check if there's a list of scan candidates already.
495 * XXX want more than the ap we're currently associated with
499 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
500 "%s: %s scan, %s%s%s%s%s\n"
502 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
503 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
504 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
505 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
506 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
507 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
510 if (ss->ss_ops != scan) {
511 /* XXX re-use cache contents? e.g. adhoc<->sta */
512 flags |= IEEE80211_SCAN_FLUSH;
514 scan_update_locked(vap, scan);
515 if (ss->ss_ops != NULL) {
516 /* XXX verify ss_ops matches vap->iv_opmode */
517 if ((flags & IEEE80211_SCAN_NOSSID) == 0) {
519 * Update the ssid list and mark flags so if
520 * we call start_scan it doesn't duplicate work.
522 copy_ssid(vap, ss, nssid, ssids);
523 flags |= IEEE80211_SCAN_NOSSID;
525 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
526 (flags & IEEE80211_SCAN_FLUSH) == 0 &&
527 time_before(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
529 * We're not currently scanning and the cache is
530 * deemed hot enough to consult. Lock out others
531 * by marking IEEE80211_F_SCAN while we decide if
532 * something is already in the scan cache we can
533 * use. Also discard any frames that might come
534 * in while temporarily marked as scanning.
536 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
537 ic->ic_flags |= IEEE80211_F_SCAN;
539 /* NB: need to use supplied flags in check */
540 ss->ss_flags = flags & 0xff;
541 result = ss->ss_ops->scan_end(ss, vap);
543 ic->ic_flags &= ~IEEE80211_F_SCAN;
544 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_DISCARD;
546 ieee80211_notify_scan_done(vap);
547 IEEE80211_UNLOCK(ic);
552 result = start_scan_locked(scan, vap, flags, duration,
553 mindwell, maxdwell, nssid, ssids);
554 IEEE80211_UNLOCK(ic);
560 * Check the scan cache for an ap/channel to use; if that fails
561 * then kick off a scan using the current settings.
564 ieee80211_check_scan_current(struct ieee80211vap *vap)
566 return ieee80211_check_scan(vap,
567 IEEE80211_SCAN_ACTIVE,
568 IEEE80211_SCAN_FOREVER, 0, 0,
569 vap->iv_des_nssid, vap->iv_des_ssid);
573 * Restart a previous scan. If the previous scan completed
574 * then we start again using the existing channel list.
577 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
579 struct ieee80211com *ic = vap->iv_ic;
580 struct ieee80211_scan_state *ss = ic->ic_scan;
581 const struct ieee80211_scanner *scan;
583 scan = ieee80211_scanner_get(vap->iv_opmode);
585 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
586 "%s: no scanner support for %s mode\n",
587 __func__, vap->iv_opmode);
593 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
596 * Go off-channel for a fixed interval that is large
597 * enough to catch most ap's but short enough that
598 * we can return on-channel before our listen interval
601 duration = IEEE80211_SCAN_OFFCHANNEL;
603 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
604 "%s: %s scan, ticks %u duration %lu\n", __func__,
605 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive",
608 scan_update_locked(vap, scan);
609 if (ss->ss_ops != NULL) {
612 * A background scan does not select a new sta; it
613 * just refreshes the scan cache. Also, indicate
614 * the scan logic should follow the beacon schedule:
615 * we go off-channel and scan for a while, then
616 * return to the bss channel to receive a beacon,
617 * then go off-channel again. All during this time
618 * we notify the ap we're in power save mode. When
619 * the scan is complete we leave power save mode.
620 * If any beacon indicates there are frames pending
621 * for us then we drop out of power save mode
622 * (and background scan) automatically by way of the
623 * usual sta power save logic.
625 ss->ss_flags |= IEEE80211_SCAN_NOPICK
626 | IEEE80211_SCAN_BGSCAN
629 /* if previous scan completed, restart */
630 if (ss->ss_next >= ss->ss_last) {
631 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
632 vap->iv_stats.is_scan_active++;
634 vap->iv_stats.is_scan_passive++;
636 * NB: beware of the scan cache being flushed;
637 * if the channel list is empty use the
638 * scan_start method to populate it.
641 if (ss->ss_last != 0)
642 ss->ss_ops->scan_restart(ss, vap);
644 ss->ss_ops->scan_start(ss, vap);
645 #ifdef IEEE80211_DEBUG
646 if (ieee80211_msg_scan(vap))
648 #endif /* IEEE80211_DEBUG */
651 /* NB: flush frames rx'd before 1st channel change */
652 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_DISCARD;
653 SCAN_PRIVATE(ss)->ss_duration = duration;
654 ss->ss_maxdwell = duration;
655 ic->ic_flags |= IEEE80211_F_SCAN;
656 ic->ic_flags_ext |= IEEE80211_FEXT_BGSCAN;
657 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
662 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
663 "%s: %s scan already in progress\n", __func__,
664 ss->ss_flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive");
666 IEEE80211_UNLOCK(ic);
668 /* NB: racey, does it matter? */
669 return (ic->ic_flags & IEEE80211_F_SCAN);
673 * Cancel any scan currently going on for the specified vap.
676 ieee80211_cancel_scan(struct ieee80211vap *vap)
678 struct ieee80211com *ic = vap->iv_ic;
679 struct ieee80211_scan_state *ss = ic->ic_scan;
682 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
684 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
685 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
686 "%s: cancel %s scan\n", __func__,
687 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
688 "active" : "passive");
690 /* clear bg scan NOPICK and mark cancel request */
691 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
692 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
693 /* wake up the scan task */
696 IEEE80211_UNLOCK(ic);
700 * Cancel any scan currently going on.
703 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
705 struct ieee80211com *ic = vap->iv_ic;
706 struct ieee80211_scan_state *ss = ic->ic_scan;
709 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
710 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0) {
711 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
712 "%s: cancel %s scan\n", __func__,
713 ss->ss_flags & IEEE80211_SCAN_ACTIVE ?
714 "active" : "passive");
716 /* clear bg scan NOPICK and mark cancel request */
717 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
718 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_CANCEL;
719 /* wake up the scan task */
722 IEEE80211_UNLOCK(ic);
726 * Public access to scan_next for drivers that manage
727 * scanning themselves (e.g. for firmware-based devices).
730 ieee80211_scan_next(struct ieee80211vap *vap)
732 struct ieee80211com *ic = vap->iv_ic;
733 struct ieee80211_scan_state *ss = ic->ic_scan;
735 /* wake up the scan task */
738 IEEE80211_UNLOCK(ic);
742 * Public access to scan_next for drivers that are not able to scan single
743 * channels (e.g. for firmware-based devices).
746 ieee80211_scan_done(struct ieee80211vap *vap)
748 struct ieee80211com *ic = vap->iv_ic;
749 struct ieee80211_scan_state *ss;
753 ss->ss_next = ss->ss_last; /* all channels are complete */
755 IEEE80211_UNLOCK(ic);
759 * Probe the curent channel, if allowed, while scanning.
760 * If the channel is not marked passive-only then send
761 * a probe request immediately. Otherwise mark state and
762 * listen for beacons on the channel; if we receive something
763 * then we'll transmit a probe request.
766 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
768 struct ieee80211com *ic = vap->iv_ic;
769 struct ieee80211_scan_state *ss = ic->ic_scan;
770 struct ifnet *ifp = vap->iv_ifp;
773 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
774 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
778 * Send directed probe requests followed by any
779 * broadcast probe request.
780 * XXX remove dependence on ic/vap->iv_bss
782 for (i = 0; i < ss->ss_nssid; i++)
783 ieee80211_send_probereq(vap->iv_bss,
784 vap->iv_myaddr, ifp->if_broadcastaddr,
785 ifp->if_broadcastaddr,
786 ss->ss_ssid[i].ssid, ss->ss_ssid[i].len);
787 if ((ss->ss_flags & IEEE80211_SCAN_NOBCAST) == 0)
788 ieee80211_send_probereq(vap->iv_bss,
789 vap->iv_myaddr, ifp->if_broadcastaddr,
790 ifp->if_broadcastaddr,
795 * Scan curchan. If this is an active scan and the channel
796 * is not marked passive then send probe request frame(s).
797 * Arrange for the channel change after maxdwell ticks.
800 scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
802 struct ieee80211vap *vap = ss->ss_vap;
804 IEEE80211_LOCK(vap->iv_ic);
805 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
806 ieee80211_probe_curchan(vap, 0);
807 callout_reset(&SCAN_PRIVATE(ss)->ss_scan_timer,
808 maxdwell, scan_signal, ss);
809 IEEE80211_UNLOCK(vap->iv_ic);
813 scan_signal(void *arg)
815 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
817 IEEE80211_LOCK_ASSERT(ss->ss_ic);
819 cv_signal(&SCAN_PRIVATE(ss)->ss_scan_cv);
823 * Handle mindwell requirements completed; initiate a channel
824 * change to the next channel asap.
827 scan_mindwell(struct ieee80211_scan_state *ss)
829 struct ieee80211com *ic = ss->ss_ic;
833 IEEE80211_UNLOCK(ic);
837 scan_task(void *arg, int pending)
839 #define ISCAN_REP (ISCAN_MINDWELL | ISCAN_DISCARD)
840 struct ieee80211_scan_state *ss = (struct ieee80211_scan_state *) arg;
841 struct ieee80211vap *vap = ss->ss_vap;
842 struct ieee80211com *ic = ss->ss_ic;
843 struct ieee80211_channel *chan;
844 unsigned long maxdwell, scanend;
848 if (vap == NULL || (ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
849 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)) {
850 /* Cancelled before we started */
854 if (ss->ss_next == ss->ss_last) {
855 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
856 "%s: no channels to scan\n", __func__);
860 if (vap->iv_opmode == IEEE80211_M_STA &&
861 vap->iv_state == IEEE80211_S_RUN) {
862 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
863 /* Enable station power save mode */
864 ieee80211_sta_pwrsave(vap, 1);
866 * Use an 1ms delay so the null data frame has a chance
868 * XXX Should use M_TXCB mechanism to eliminate this.
870 cv_timedwait(&SCAN_PRIVATE(ss)->ss_scan_cv,
871 IEEE80211_LOCK_OBJ(ic), hz / 1000);
872 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
877 scanend = ticks + SCAN_PRIVATE(ss)->ss_duration;
878 IEEE80211_UNLOCK(ic);
879 ic->ic_scan_start(ic); /* notify driver */
883 scandone = (ss->ss_next >= ss->ss_last) ||
884 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) != 0;
885 if (scandone || (ss->ss_flags & IEEE80211_SCAN_GOTPICK) ||
886 (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT) ||
887 time_after(ticks + ss->ss_mindwell, scanend))
890 chan = ss->ss_chans[ss->ss_next++];
893 * Watch for truncation due to the scan end time.
895 if (time_after(ticks + ss->ss_maxdwell, scanend))
896 maxdwell = scanend - ticks;
898 maxdwell = ss->ss_maxdwell;
900 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
901 "%s: chan %3d%c -> %3d%c [%s, dwell min %lums max %lums]\n",
903 ieee80211_chan2ieee(ic, ic->ic_curchan),
904 channel_type(ic->ic_curchan),
905 ieee80211_chan2ieee(ic, chan), channel_type(chan),
906 (ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
907 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 ?
908 "active" : "passive",
909 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(maxdwell));
912 * Potentially change channel and phy mode.
914 ic->ic_curchan = chan;
915 ic->ic_rt = ieee80211_get_ratetable(chan);
916 IEEE80211_UNLOCK(ic);
918 * Perform the channel change and scan unlocked so the driver
919 * may sleep. Once set_channel returns the hardware has
920 * completed the channel change.
922 ic->ic_set_channel(ic);
923 ieee80211_radiotap_chan_change(ic);
926 * Scan curchan. Drivers for "intelligent hardware"
927 * override ic_scan_curchan to tell the device to do
928 * the work. Otherwise we manage the work outselves;
929 * sending a probe request (as needed), and arming the
930 * timeout to switch channels after maxdwell ticks.
932 * scan_curchan should only pause for the time required to
933 * prepare/initiate the hardware for the scan (if at all), the
934 * below condvar is used to sleep for the channels dwell time
935 * and allows it to be signalled for abort.
937 ic->ic_scan_curchan(ss, maxdwell);
940 SCAN_PRIVATE(ss)->ss_chanmindwell = ticks + ss->ss_mindwell;
941 /* clear mindwell lock and initial channel change flush */
942 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
944 if ((SCAN_PRIVATE(ss)->ss_iflags & (ISCAN_CANCEL|ISCAN_ABORT)))
947 /* Wait to be signalled to scan the next channel */
948 cv_wait(&SCAN_PRIVATE(ss)->ss_scan_cv, IEEE80211_LOCK_OBJ(ic));
950 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_ABORT)
953 IEEE80211_UNLOCK(ic);
954 ic->ic_scan_end(ic); /* notify driver */
958 * Record scan complete time. Note that we also do
959 * this when canceled so any background scan will
960 * not be restarted for a while.
963 ic->ic_lastscan = ticks;
964 /* return to the bss channel */
965 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
966 ic->ic_curchan != ic->ic_bsschan) {
967 ieee80211_setupcurchan(ic, ic->ic_bsschan);
968 IEEE80211_UNLOCK(ic);
969 ic->ic_set_channel(ic);
970 ieee80211_radiotap_chan_change(ic);
973 /* clear internal flags and any indication of a pick */
974 SCAN_PRIVATE(ss)->ss_iflags &= ~ISCAN_REP;
975 ss->ss_flags &= ~IEEE80211_SCAN_GOTPICK;
978 * If not canceled and scan completed, do post-processing.
979 * If the callback function returns 0, then it wants to
980 * continue/restart scanning. Unfortunately we needed to
981 * notify the driver to end the scan above to avoid having
982 * rx frames alter the scan candidate list.
984 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_CANCEL) == 0 &&
985 !ss->ss_ops->scan_end(ss, vap) &&
986 (ss->ss_flags & IEEE80211_SCAN_ONCE) == 0 &&
987 time_before(ticks + ss->ss_mindwell, scanend)) {
988 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
990 "[ticks %u, dwell min %lu scanend %lu]\n",
992 ticks, ss->ss_mindwell, scanend);
993 ss->ss_next = 0; /* reset to begining */
994 if (ss->ss_flags & IEEE80211_SCAN_ACTIVE)
995 vap->iv_stats.is_scan_active++;
997 vap->iv_stats.is_scan_passive++;
999 ss->ss_ops->scan_restart(ss, vap); /* XXX? */
1000 ieee80211_runtask(ic, &SCAN_PRIVATE(ss)->ss_scan_task);
1001 IEEE80211_UNLOCK(ic);
1005 /* past here, scandone is ``true'' if not in bg mode */
1006 if ((ss->ss_flags & IEEE80211_SCAN_BGSCAN) == 0)
1009 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1010 "%s: %s, [ticks %u, dwell min %lu scanend %lu]\n",
1011 __func__, scandone ? "done" : "stopped",
1012 ticks, ss->ss_mindwell, scanend);
1015 * Clear the SCAN bit first in case frames are
1016 * pending on the station power save queue. If
1017 * we defer this then the dispatch of the frames
1018 * may generate a request to cancel scanning.
1021 ic->ic_flags &= ~IEEE80211_F_SCAN;
1023 * Drop out of power save mode when a scan has
1024 * completed. If this scan was prematurely terminated
1025 * because it is a background scan then don't notify
1026 * the ap; we'll either return to scanning after we
1027 * receive the beacon frame or we'll drop out of power
1028 * save mode because the beacon indicates we have frames
1032 ieee80211_sta_pwrsave(vap, 0);
1033 if (ss->ss_next >= ss->ss_last) {
1034 ieee80211_notify_scan_done(vap);
1035 ic->ic_flags_ext &= ~IEEE80211_FEXT_BGSCAN;
1038 SCAN_PRIVATE(ss)->ss_iflags &= ~(ISCAN_CANCEL|ISCAN_ABORT);
1039 ss->ss_flags &= ~(IEEE80211_SCAN_ONCE | IEEE80211_SCAN_PICK1ST);
1040 IEEE80211_UNLOCK(ic);
1044 #ifdef IEEE80211_DEBUG
1046 dump_country(const uint8_t *ie)
1048 const struct ieee80211_country_ie *cie =
1049 (const struct ieee80211_country_ie *) ie;
1050 int i, nbands, schan, nchan;
1053 printf(" <bogus country ie, len %d>", cie->len);
1056 printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
1057 nbands = (cie->len - 3) / sizeof(cie->band[0]);
1058 for (i = 0; i < nbands; i++) {
1059 schan = cie->band[i].schan;
1060 nchan = cie->band[i].nchan;
1062 printf(" %u-%u,%u", schan, schan + nchan-1,
1063 cie->band[i].maxtxpwr);
1065 printf(" %u,%u", schan, cie->band[i].maxtxpwr);
1071 dump_probe_beacon(uint8_t subtype, int isnew,
1072 const uint8_t mac[IEEE80211_ADDR_LEN],
1073 const struct ieee80211_scanparams *sp, int rssi)
1076 printf("[%s] %s%s on chan %u (bss chan %u) ",
1077 ether_sprintf(mac), isnew ? "new " : "",
1078 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1079 sp->chan, sp->bchan);
1080 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1081 printf(" rssi %d\n", rssi);
1084 printf("[%s] caps 0x%x bintval %u erp 0x%x",
1085 ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1086 if (sp->country != NULL)
1087 dump_country(sp->country);
1091 #endif /* IEEE80211_DEBUG */
1094 * Process a beacon or probe response frame.
1097 ieee80211_add_scan(struct ieee80211vap *vap,
1098 const struct ieee80211_scanparams *sp,
1099 const struct ieee80211_frame *wh,
1100 int subtype, int rssi, int noise)
1102 struct ieee80211com *ic = vap->iv_ic;
1103 struct ieee80211_scan_state *ss = ic->ic_scan;
1107 * Frames received during startup are discarded to avoid
1108 * using scan state setup on the initial entry to the timer
1109 * callback. This can occur because the device may enable
1110 * rx prior to our doing the initial channel change in the
1113 if (SCAN_PRIVATE(ss)->ss_iflags & ISCAN_DISCARD)
1115 #ifdef IEEE80211_DEBUG
1116 if (ieee80211_msg_scan(vap) && (ic->ic_flags & IEEE80211_F_SCAN))
1117 dump_probe_beacon(subtype, 1, wh->i_addr2, sp, rssi);
1119 if (ss->ss_ops != NULL &&
1120 ss->ss_ops->scan_add(ss, sp, wh, subtype, rssi, noise)) {
1122 * If we've reached the min dwell time terminate
1123 * the timer so we'll switch to the next channel.
1125 if ((SCAN_PRIVATE(ss)->ss_iflags & ISCAN_MINDWELL) == 0 &&
1126 time_after_eq(ticks, SCAN_PRIVATE(ss)->ss_chanmindwell)) {
1127 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1128 "%s: chan %3d%c min dwell met (%u > %lu)\n",
1130 ieee80211_chan2ieee(ic, ic->ic_curchan),
1131 channel_type(ic->ic_curchan),
1132 ticks, SCAN_PRIVATE(ss)->ss_chanmindwell);
1133 SCAN_PRIVATE(ss)->ss_iflags |= ISCAN_MINDWELL;
1135 * NB: trigger at next clock tick or wait for the
1138 ic->ic_scan_mindwell(ss);
1144 * Timeout/age scan cache entries; called from sta timeout
1145 * timer (XXX should be self-contained).
1148 ieee80211_scan_timeout(struct ieee80211com *ic)
1150 struct ieee80211_scan_state *ss = ic->ic_scan;
1152 if (ss->ss_ops != NULL)
1153 ss->ss_ops->scan_age(ss);
1157 * Mark a scan cache entry after a successful associate.
1160 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
1162 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1164 if (ss->ss_ops != NULL) {
1165 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
1166 mac, "%s", __func__);
1167 ss->ss_ops->scan_assoc_success(ss, mac);
1172 * Demerit a scan cache entry after failing to associate.
1175 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
1176 const uint8_t mac[], int reason)
1178 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1180 if (ss->ss_ops != NULL) {
1181 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
1182 "%s: reason %u", __func__, reason);
1183 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
1188 * Iterate over the contents of the scan cache.
1191 ieee80211_scan_iterate(struct ieee80211vap *vap,
1192 ieee80211_scan_iter_func *f, void *arg)
1194 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1196 if (ss->ss_ops != NULL)
1197 ss->ss_ops->scan_iterate(ss, f, arg);
1201 * Flush the contents of the scan cache.
1204 ieee80211_scan_flush(struct ieee80211vap *vap)
1206 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
1208 if (ss->ss_ops != NULL && ss->ss_vap == vap) {
1209 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", __func__);
1210 ss->ss_ops->scan_flush(ss);
1215 * Check the scan cache for an ap/channel to use; if that
1216 * fails then kick off a new scan.
1218 struct ieee80211_channel *
1219 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
1221 struct ieee80211_scan_state *ss = ic->ic_scan;
1223 IEEE80211_LOCK_ASSERT(ic);
1225 if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
1229 if (ss->ss_ops->scan_pickchan == NULL) {
1230 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
1231 "%s: scan module does not support picking a channel, "
1232 "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
1235 return ss->ss_ops->scan_pickchan(ss, flags);