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_var.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
47 #include <net80211/ieee80211_var.h>
49 /* XXX until it's implemented as attach ops */
50 #include <net80211/ieee80211_scan_sw.h>
55 * Roaming-related defaults. RSSI thresholds are as returned by the
56 * driver (.5dBm). Transmit rate thresholds are IEEE rate codes (i.e
60 #define ROAM_RSSI_11A_DEFAULT 14 /* 11a bss */
61 #define ROAM_RSSI_11B_DEFAULT 14 /* 11b bss */
62 #define ROAM_RSSI_11BONLY_DEFAULT 14 /* 11b-only bss */
63 /* transmit rate thresholds */
64 #define ROAM_RATE_11A_DEFAULT 2*12 /* 11a bss */
65 #define ROAM_RATE_11B_DEFAULT 2*5 /* 11b bss */
66 #define ROAM_RATE_11BONLY_DEFAULT 2*1 /* 11b-only bss */
67 #define ROAM_RATE_HALF_DEFAULT 2*6 /* half-width 11a/g bss */
68 #define ROAM_RATE_QUARTER_DEFAULT 2*3 /* quarter-width 11a/g bss */
69 #define ROAM_MCS_11N_DEFAULT (1 | IEEE80211_RATE_MCS) /* 11n bss */
72 ieee80211_scan_attach(struct ieee80211com *ic)
75 * If there's no scan method pointer, attach the
76 * swscan set as a default.
78 if (ic->ic_scan_methods == NULL)
79 ieee80211_swscan_attach(ic);
81 ic->ic_scan_methods->sc_attach(ic);
85 ieee80211_scan_detach(struct ieee80211com *ic)
89 * Ideally we'd do the ss_ops detach call here;
90 * but then sc_detach() would need to be split in two.
94 ic->ic_scan_methods->sc_detach(ic);
97 static const struct ieee80211_roamparam defroam[IEEE80211_MODE_MAX] = {
98 [IEEE80211_MODE_11A] = { .rssi = ROAM_RSSI_11A_DEFAULT,
99 .rate = ROAM_RATE_11A_DEFAULT },
100 [IEEE80211_MODE_11G] = { .rssi = ROAM_RSSI_11B_DEFAULT,
101 .rate = ROAM_RATE_11B_DEFAULT },
102 [IEEE80211_MODE_11B] = { .rssi = ROAM_RSSI_11BONLY_DEFAULT,
103 .rate = ROAM_RATE_11BONLY_DEFAULT },
104 [IEEE80211_MODE_TURBO_A]= { .rssi = ROAM_RSSI_11A_DEFAULT,
105 .rate = ROAM_RATE_11A_DEFAULT },
106 [IEEE80211_MODE_TURBO_G]= { .rssi = ROAM_RSSI_11A_DEFAULT,
107 .rate = ROAM_RATE_11A_DEFAULT },
108 [IEEE80211_MODE_STURBO_A]={ .rssi = ROAM_RSSI_11A_DEFAULT,
109 .rate = ROAM_RATE_11A_DEFAULT },
110 [IEEE80211_MODE_HALF] = { .rssi = ROAM_RSSI_11A_DEFAULT,
111 .rate = ROAM_RATE_HALF_DEFAULT },
112 [IEEE80211_MODE_QUARTER]= { .rssi = ROAM_RSSI_11A_DEFAULT,
113 .rate = ROAM_RATE_QUARTER_DEFAULT },
114 [IEEE80211_MODE_11NA] = { .rssi = ROAM_RSSI_11A_DEFAULT,
115 .rate = ROAM_MCS_11N_DEFAULT },
116 [IEEE80211_MODE_11NG] = { .rssi = ROAM_RSSI_11B_DEFAULT,
117 .rate = ROAM_MCS_11N_DEFAULT },
121 ieee80211_scan_vattach(struct ieee80211vap *vap)
123 struct ieee80211com *ic = vap->iv_ic;
125 vap->iv_bgscanidle = (IEEE80211_BGSCAN_IDLE_DEFAULT*1000)/hz;
126 vap->iv_bgscanintvl = IEEE80211_BGSCAN_INTVAL_DEFAULT*hz;
127 vap->iv_scanvalid = IEEE80211_SCAN_VALID_DEFAULT*hz;
129 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
130 memcpy(vap->iv_roamparms, defroam, sizeof(defroam));
132 ic->ic_scan_methods->sc_vattach(vap);
136 ieee80211_scan_vdetach(struct ieee80211vap *vap)
138 struct ieee80211com *ic = vap->iv_ic;
139 struct ieee80211_scan_state *ss;
144 ic->ic_scan_methods->sc_vdetach(vap);
146 if (ss != NULL && ss->ss_vap == vap) {
147 if (ss->ss_ops != NULL) {
148 ss->ss_ops->scan_detach(ss);
153 IEEE80211_UNLOCK(ic);
157 * Simple-minded scanner module support.
159 static const char *scan_modnames[IEEE80211_OPMODE_MAX] = {
160 "wlan_scan_sta", /* IEEE80211_M_IBSS */
161 "wlan_scan_sta", /* IEEE80211_M_STA */
162 "wlan_scan_wds", /* IEEE80211_M_WDS */
163 "wlan_scan_sta", /* IEEE80211_M_AHDEMO */
164 "wlan_scan_ap", /* IEEE80211_M_HOSTAP */
165 "wlan_scan_monitor", /* IEEE80211_M_MONITOR */
166 "wlan_scan_sta", /* IEEE80211_M_MBSS */
168 static const struct ieee80211_scanner *scanners[IEEE80211_OPMODE_MAX];
170 const struct ieee80211_scanner *
171 ieee80211_scanner_get(enum ieee80211_opmode mode)
173 if (mode >= IEEE80211_OPMODE_MAX)
175 if (scanners[mode] == NULL)
176 ieee80211_load_module(scan_modnames[mode]);
177 return scanners[mode];
181 ieee80211_scanner_register(enum ieee80211_opmode mode,
182 const struct ieee80211_scanner *scan)
184 if (mode >= IEEE80211_OPMODE_MAX)
186 scanners[mode] = scan;
190 ieee80211_scanner_unregister(enum ieee80211_opmode mode,
191 const struct ieee80211_scanner *scan)
193 if (mode >= IEEE80211_OPMODE_MAX)
195 if (scanners[mode] == scan)
196 scanners[mode] = NULL;
200 ieee80211_scanner_unregister_all(const struct ieee80211_scanner *scan)
204 for (m = 0; m < IEEE80211_OPMODE_MAX; m++)
205 if (scanners[m] == scan)
210 * Update common scanner state to reflect the current
211 * operating mode. This is called when the state machine
212 * is transitioned to RUN state w/o scanning--e.g. when
213 * operating in monitor mode. The purpose of this is to
214 * ensure later callbacks find ss_ops set to properly
215 * reflect current operating mode.
218 ieee80211_scan_update_locked(struct ieee80211vap *vap,
219 const struct ieee80211_scanner *scan)
221 struct ieee80211com *ic = vap->iv_ic;
222 struct ieee80211_scan_state *ss = ic->ic_scan;
224 IEEE80211_LOCK_ASSERT(ic);
226 #ifdef IEEE80211_DEBUG
227 if (ss->ss_vap != vap || ss->ss_ops != scan) {
228 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
229 "%s: current scanner is <%s:%s>, switch to <%s:%s>\n",
232 ss->ss_vap->iv_ifp->if_xname : "none",
234 ieee80211_opmode_name[ss->ss_vap->iv_opmode] : "none",
235 vap->iv_ifp->if_xname,
236 ieee80211_opmode_name[vap->iv_opmode]);
240 if (ss->ss_ops != scan) {
242 * Switch scanners; detach old, attach new. Special
243 * case where a single scan module implements multiple
244 * policies by using different scan ops but a common
245 * core. We assume if the old and new attach methods
246 * are identical then it's ok to just change ss_ops
247 * and not flush the internal state of the module.
249 if (scan == NULL || ss->ss_ops == NULL ||
250 ss->ss_ops->scan_attach != scan->scan_attach) {
251 if (ss->ss_ops != NULL)
252 ss->ss_ops->scan_detach(ss);
253 if (scan != NULL && !scan->scan_attach(ss)) {
254 /* XXX attach failure */
264 ieee80211_scan_dump_channels(const struct ieee80211_scan_state *ss)
266 struct ieee80211com *ic = ss->ss_ic;
271 for (i = ss->ss_next; i < ss->ss_last; i++) {
272 const struct ieee80211_channel *c = ss->ss_chans[i];
274 printf("%s%u%c", sep, ieee80211_chan2ieee(ic, c),
275 ieee80211_channel_type_char(c));
280 #ifdef IEEE80211_DEBUG
282 ieee80211_scan_dump(struct ieee80211_scan_state *ss)
284 struct ieee80211vap *vap = ss->ss_vap;
286 if_printf(vap->iv_ifp, "scan set ");
287 ieee80211_scan_dump_channels(ss);
288 printf(" dwell min %lums max %lums\n",
289 ticks_to_msecs(ss->ss_mindwell), ticks_to_msecs(ss->ss_maxdwell));
291 #endif /* IEEE80211_DEBUG */
294 ieee80211_scan_copy_ssid(struct ieee80211vap *vap, struct ieee80211_scan_state *ss,
295 int nssid, const struct ieee80211_scan_ssid ssids[])
297 if (nssid > IEEE80211_SCAN_MAX_SSID) {
299 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
300 "%s: too many ssid %d, ignoring all of them\n",
304 memcpy(ss->ss_ssid, ssids, nssid * sizeof(ssids[0]));
305 ss->ss_nssid = nssid;
309 * Start a scan unless one is already going.
312 ieee80211_start_scan(struct ieee80211vap *vap, int flags,
313 u_int duration, u_int mindwell, u_int maxdwell,
314 u_int nssid, const struct ieee80211_scan_ssid ssids[])
316 const struct ieee80211_scanner *scan;
317 struct ieee80211com *ic = vap->iv_ic;
319 scan = ieee80211_scanner_get(vap->iv_opmode);
321 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
322 "%s: no scanner support for %s mode\n",
323 __func__, ieee80211_opmode_name[vap->iv_opmode]);
328 return ic->ic_scan_methods->sc_start_scan(scan, vap, flags, duration,
329 mindwell, maxdwell, nssid, ssids);
333 * Check the scan cache for an ap/channel to use; if that
334 * fails then kick off a new scan.
337 ieee80211_check_scan(struct ieee80211vap *vap, int flags,
338 u_int duration, u_int mindwell, u_int maxdwell,
339 u_int nssid, const struct ieee80211_scan_ssid ssids[])
341 struct ieee80211com *ic = vap->iv_ic;
342 struct ieee80211_scan_state *ss = ic->ic_scan;
343 const struct ieee80211_scanner *scan;
346 scan = ieee80211_scanner_get(vap->iv_opmode);
348 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
349 "%s: no scanner support for %s mode\n",
350 __func__, vap->iv_opmode);
356 * Check if there's a list of scan candidates already.
357 * XXX want more than the ap we're currently associated with
361 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
362 "%s: %s scan, %s%s%s%s%s\n"
364 , flags & IEEE80211_SCAN_ACTIVE ? "active" : "passive"
365 , flags & IEEE80211_SCAN_FLUSH ? "flush" : "append"
366 , flags & IEEE80211_SCAN_NOPICK ? ", nopick" : ""
367 , flags & IEEE80211_SCAN_NOJOIN ? ", nojoin" : ""
368 , flags & IEEE80211_SCAN_PICK1ST ? ", pick1st" : ""
369 , flags & IEEE80211_SCAN_ONCE ? ", once" : ""
372 if (ss->ss_ops != scan) {
373 /* XXX re-use cache contents? e.g. adhoc<->sta */
374 flags |= IEEE80211_SCAN_FLUSH;
378 * XXX TODO: separate things out a bit better.
380 ieee80211_scan_update_locked(vap, scan);
382 result = ic->ic_scan_methods->sc_check_scan(scan, vap, flags, duration,
383 mindwell, maxdwell, nssid, ssids);
385 IEEE80211_UNLOCK(ic);
391 * Check the scan cache for an ap/channel to use; if that fails
392 * then kick off a scan using the current settings.
395 ieee80211_check_scan_current(struct ieee80211vap *vap)
397 return ieee80211_check_scan(vap,
398 IEEE80211_SCAN_ACTIVE,
399 IEEE80211_SCAN_FOREVER, 0, 0,
400 vap->iv_des_nssid, vap->iv_des_ssid);
404 * Restart a previous scan. If the previous scan completed
405 * then we start again using the existing channel list.
408 ieee80211_bg_scan(struct ieee80211vap *vap, int flags)
410 struct ieee80211com *ic = vap->iv_ic;
411 const struct ieee80211_scanner *scan;
413 // IEEE80211_UNLOCK_ASSERT(sc);
415 scan = ieee80211_scanner_get(vap->iv_opmode);
417 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
418 "%s: no scanner support for %s mode\n",
419 __func__, vap->iv_opmode);
425 * XXX TODO: pull apart the bgscan logic into whatever
426 * belongs here and whatever belongs in the software
429 return (ic->ic_scan_methods->sc_bg_scan(scan, vap, flags));
433 * Cancel any scan currently going on for the specified vap.
436 ieee80211_cancel_scan(struct ieee80211vap *vap)
438 struct ieee80211com *ic = vap->iv_ic;
440 ic->ic_scan_methods->sc_cancel_scan(vap);
444 * Cancel any scan currently going on.
447 ieee80211_cancel_anyscan(struct ieee80211vap *vap)
449 struct ieee80211com *ic = vap->iv_ic;
451 ic->ic_scan_methods->sc_cancel_anyscan(vap);
455 * Public access to scan_next for drivers that manage
456 * scanning themselves (e.g. for firmware-based devices).
459 ieee80211_scan_next(struct ieee80211vap *vap)
461 struct ieee80211com *ic = vap->iv_ic;
463 ic->ic_scan_methods->sc_scan_next(vap);
467 * Public access to scan_next for drivers that are not able to scan single
468 * channels (e.g. for firmware-based devices).
471 ieee80211_scan_done(struct ieee80211vap *vap)
473 struct ieee80211com *ic = vap->iv_ic;
474 struct ieee80211_scan_state *ss;
476 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s: called\n", __func__);
480 ss->ss_next = ss->ss_last; /* all channels are complete */
482 ic->ic_scan_methods->sc_scan_done(vap);
484 IEEE80211_UNLOCK(ic);
488 * Probe the curent channel, if allowed, while scanning.
489 * If the channel is not marked passive-only then send
490 * a probe request immediately. Otherwise mark state and
491 * listen for beacons on the channel; if we receive something
492 * then we'll transmit a probe request.
495 ieee80211_probe_curchan(struct ieee80211vap *vap, int force)
497 struct ieee80211com *ic = vap->iv_ic;
499 if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) && !force) {
500 ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
504 ic->ic_scan_methods->sc_scan_probe_curchan(vap, force);
507 #ifdef IEEE80211_DEBUG
509 dump_country(const uint8_t *ie)
511 const struct ieee80211_country_ie *cie =
512 (const struct ieee80211_country_ie *) ie;
513 int i, nbands, schan, nchan;
516 printf(" <bogus country ie, len %d>", cie->len);
519 printf(" country [%c%c%c", cie->cc[0], cie->cc[1], cie->cc[2]);
520 nbands = (cie->len - 3) / sizeof(cie->band[0]);
521 for (i = 0; i < nbands; i++) {
522 schan = cie->band[i].schan;
523 nchan = cie->band[i].nchan;
525 printf(" %u-%u,%u", schan, schan + nchan-1,
526 cie->band[i].maxtxpwr);
528 printf(" %u,%u", schan, cie->band[i].maxtxpwr);
534 ieee80211_scan_dump_probe_beacon(uint8_t subtype, int isnew,
535 const uint8_t mac[IEEE80211_ADDR_LEN],
536 const struct ieee80211_scanparams *sp, int rssi)
539 printf("[%s] %s%s on chan %u (bss chan %u) ",
540 ether_sprintf(mac), isnew ? "new " : "",
541 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
542 sp->chan, sp->bchan);
543 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
544 printf(" rssi %d\n", rssi);
547 printf("[%s] caps 0x%x bintval %u erp 0x%x",
548 ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
549 if (sp->country != NULL)
550 dump_country(sp->country);
554 #endif /* IEEE80211_DEBUG */
557 * Process a beacon or probe response frame.
560 ieee80211_add_scan(struct ieee80211vap *vap,
561 struct ieee80211_channel *curchan,
562 const struct ieee80211_scanparams *sp,
563 const struct ieee80211_frame *wh,
564 int subtype, int rssi, int noise)
566 struct ieee80211com *ic = vap->iv_ic;
568 return (ic->ic_scan_methods->sc_add_scan(vap, curchan, sp, wh, subtype,
573 * Timeout/age scan cache entries; called from sta timeout
574 * timer (XXX should be self-contained).
577 ieee80211_scan_timeout(struct ieee80211com *ic)
579 struct ieee80211_scan_state *ss = ic->ic_scan;
581 if (ss->ss_ops != NULL)
582 ss->ss_ops->scan_age(ss);
586 * Mark a scan cache entry after a successful associate.
589 ieee80211_scan_assoc_success(struct ieee80211vap *vap, const uint8_t mac[])
591 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
593 if (ss->ss_ops != NULL) {
594 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN,
595 mac, "%s", __func__);
596 ss->ss_ops->scan_assoc_success(ss, mac);
601 * Demerit a scan cache entry after failing to associate.
604 ieee80211_scan_assoc_fail(struct ieee80211vap *vap,
605 const uint8_t mac[], int reason)
607 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
609 if (ss->ss_ops != NULL) {
610 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, mac,
611 "%s: reason %u", __func__, reason);
612 ss->ss_ops->scan_assoc_fail(ss, mac, reason);
617 * Iterate over the contents of the scan cache.
620 ieee80211_scan_iterate(struct ieee80211vap *vap,
621 ieee80211_scan_iter_func *f, void *arg)
623 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
625 if (ss->ss_ops != NULL)
626 ss->ss_ops->scan_iterate(ss, f, arg);
630 * Flush the contents of the scan cache.
633 ieee80211_scan_flush(struct ieee80211vap *vap)
635 struct ieee80211_scan_state *ss = vap->iv_ic->ic_scan;
637 if (ss->ss_ops != NULL && ss->ss_vap == vap) {
638 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", __func__);
639 ss->ss_ops->scan_flush(ss);
644 * Check the scan cache for an ap/channel to use; if that
645 * fails then kick off a new scan.
647 struct ieee80211_channel *
648 ieee80211_scan_pickchannel(struct ieee80211com *ic, int flags)
650 struct ieee80211_scan_state *ss = ic->ic_scan;
652 IEEE80211_LOCK_ASSERT(ic);
654 if (ss == NULL || ss->ss_ops == NULL || ss->ss_vap == NULL) {
658 if (ss->ss_ops->scan_pickchan == NULL) {
659 IEEE80211_DPRINTF(ss->ss_vap, IEEE80211_MSG_SCAN,
660 "%s: scan module does not support picking a channel, "
661 "opmode %s\n", __func__, ss->ss_vap->iv_opmode);
664 return ss->ss_ops->scan_pickchan(ss, flags);