2 * Copyright (c) 2002-2009 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 station scanning support.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/socket.h>
42 #include <net/if_var.h>
43 #include <net/if_media.h>
44 #include <net/ethernet.h>
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_input.h>
48 #include <net80211/ieee80211_regdomain.h>
49 #ifdef IEEE80211_SUPPORT_TDMA
50 #include <net80211/ieee80211_tdma.h>
52 #ifdef IEEE80211_SUPPORT_MESH
53 #include <net80211/ieee80211_mesh.h>
55 #include <net80211/ieee80211_ratectl.h>
60 * Parameters for managing cache entries:
62 * o a station with STA_FAILS_MAX failures is not considered
63 * when picking a candidate
64 * o a station that hasn't had an update in STA_PURGE_SCANS
65 * (background) scans is discarded
66 * o after STA_FAILS_AGE seconds we clear the failure count
68 #define STA_FAILS_MAX 2 /* assoc failures before ignored */
69 #define STA_FAILS_AGE (2*60) /* time before clearing fails (secs) */
70 #define STA_PURGE_SCANS 2 /* age for purging entries (scans) */
73 #define STA_RSSI_MIN 8 /* min acceptable rssi */
74 #define STA_RSSI_MAX 40 /* max rssi for comparison */
77 struct ieee80211_scan_entry base;
78 TAILQ_ENTRY(sta_entry) se_list;
79 LIST_ENTRY(sta_entry) se_hash;
80 uint8_t se_fails; /* failure to associate count */
81 uint8_t se_seen; /* seen during current scan */
82 uint8_t se_notseen; /* not seen in previous scans */
84 #define STA_DEMOTE11B 0x01 /* match w/ demoted 11b chan */
85 uint32_t se_avgrssi; /* LPF rssi state */
86 unsigned long se_lastupdate; /* time of last update */
87 unsigned long se_lastfail; /* time of last failure */
88 unsigned long se_lastassoc; /* time of last association */
89 u_int se_scangen; /* iterator scan gen# */
90 u_int se_countrygen; /* gen# of last cc notify */
93 #define STA_HASHSIZE 32
94 /* simple hash is enough for variation of macaddr */
95 #define STA_HASH(addr) \
96 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE)
98 #define MAX_IEEE_CHAN 256 /* max acceptable IEEE chan # */
99 CTASSERT(MAX_IEEE_CHAN >= 256);
102 ieee80211_scan_table_lock_t st_lock; /* on scan table */
103 TAILQ_HEAD(, sta_entry) st_entry; /* all entries */
104 LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE];
105 struct mtx st_scanlock; /* on st_scaniter */
106 u_int st_scaniter; /* gen# for iterator */
107 u_int st_scangen; /* scan generation # */
109 /* ap-related state */
110 int st_maxrssi[MAX_IEEE_CHAN];
113 static void sta_flush_table(struct sta_table *);
115 * match_bss returns a bitmask describing if an entry is suitable
116 * for use. If non-zero the entry was deemed not suitable and it's
117 * contents explains why. The following flags are or'd to to this
118 * mask and can be used to figure out why the entry was rejected.
120 #define MATCH_CHANNEL 0x00001 /* channel mismatch */
121 #define MATCH_CAPINFO 0x00002 /* capabilities mismatch, e.g. no ess */
122 #define MATCH_PRIVACY 0x00004 /* privacy mismatch */
123 #define MATCH_RATE 0x00008 /* rate set mismatch */
124 #define MATCH_SSID 0x00010 /* ssid mismatch */
125 #define MATCH_BSSID 0x00020 /* bssid mismatch */
126 #define MATCH_FAILS 0x00040 /* too many failed auth attempts */
127 #define MATCH_NOTSEEN 0x00080 /* not seen in recent scans */
128 #define MATCH_RSSI 0x00100 /* rssi deemed too low to use */
129 #define MATCH_CC 0x00200 /* country code mismatch */
130 #define MATCH_TDMA_NOIE 0x00400 /* no TDMA ie */
131 #define MATCH_TDMA_NOTMASTER 0x00800 /* not TDMA master */
132 #define MATCH_TDMA_NOSLOT 0x01000 /* all TDMA slots occupied */
133 #define MATCH_TDMA_LOCAL 0x02000 /* local address */
134 #define MATCH_TDMA_VERSION 0x04000 /* protocol version mismatch */
135 #define MATCH_MESH_NOID 0x10000 /* no MESHID ie */
136 #define MATCH_MESHID 0x20000 /* meshid mismatch */
137 static int match_bss(struct ieee80211vap *,
138 const struct ieee80211_scan_state *, struct sta_entry *, int);
139 static void adhoc_age(struct ieee80211_scan_state *);
142 isocmp(const uint8_t cc1[], const uint8_t cc2[])
144 return (cc1[0] == cc2[0] && cc1[1] == cc2[1]);
147 /* number of references from net80211 layer */
148 static int nrefs = 0;
152 IEEE80211_SCANNER_MODULE(sta, 1);
155 * Attach prior to any scanning work.
158 sta_attach(struct ieee80211_scan_state *ss)
160 struct sta_table *st;
162 st = (struct sta_table *) malloc(sizeof(struct sta_table),
163 M_80211_SCAN, M_NOWAIT | M_ZERO);
166 IEEE80211_SCAN_TABLE_LOCK_INIT(st, "scantable");
167 mtx_init(&st->st_scanlock, "scangen", "802.11 scangen", MTX_DEF);
168 TAILQ_INIT(&st->st_entry);
170 nrefs++; /* NB: we assume caller locking */
175 * Cleanup any private state.
178 sta_detach(struct ieee80211_scan_state *ss)
180 struct sta_table *st = ss->ss_priv;
184 IEEE80211_SCAN_TABLE_LOCK_DESTROY(st);
185 mtx_destroy(&st->st_scanlock);
186 free(st, M_80211_SCAN);
187 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
188 nrefs--; /* NB: we assume caller locking */
194 * Flush all per-scan state.
197 sta_flush(struct ieee80211_scan_state *ss)
199 struct sta_table *st = ss->ss_priv;
201 IEEE80211_SCAN_TABLE_LOCK(st);
203 IEEE80211_SCAN_TABLE_UNLOCK(st);
209 * Flush all entries in the scan cache.
212 sta_flush_table(struct sta_table *st)
214 struct sta_entry *se, *next;
216 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
217 TAILQ_REMOVE(&st->st_entry, se, se_list);
218 LIST_REMOVE(se, se_hash);
219 ieee80211_ies_cleanup(&se->base.se_ies);
220 free(se, M_80211_SCAN);
222 memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi));
226 * Process a beacon or probe response frame; create an
227 * entry in the scan cache or update any previous entry.
230 sta_add(struct ieee80211_scan_state *ss,
231 const struct ieee80211_scanparams *sp,
232 const struct ieee80211_frame *wh,
233 int subtype, int rssi, int noise)
235 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
236 #define PICK1ST(_ss) \
237 ((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
238 IEEE80211_SCAN_PICK1ST)
239 struct sta_table *st = ss->ss_priv;
240 const uint8_t *macaddr = wh->i_addr2;
241 struct ieee80211vap *vap = ss->ss_vap;
242 struct ieee80211com *ic = vap->iv_ic;
243 struct ieee80211_channel *c;
244 struct sta_entry *se;
245 struct ieee80211_scan_entry *ise;
248 hash = STA_HASH(macaddr);
250 IEEE80211_SCAN_TABLE_LOCK(st);
251 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
252 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
254 se = (struct sta_entry *) malloc(sizeof(struct sta_entry),
255 M_80211_SCAN, M_NOWAIT | M_ZERO);
257 IEEE80211_SCAN_TABLE_UNLOCK(st);
260 se->se_scangen = st->st_scaniter-1;
261 se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
262 IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
263 TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
264 LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
267 /* XXX ap beaconing multiple ssid w/ same bssid */
268 if (sp->ssid[1] != 0 &&
269 (ISPROBE(subtype) || ise->se_ssid[1] == 0))
270 memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]);
271 KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE,
272 ("rate set too large: %u", sp->rates[1]));
273 memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]);
274 if (sp->xrates != NULL) {
275 /* XXX validate xrates[1] */
276 KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE,
277 ("xrate set too large: %u", sp->xrates[1]));
278 memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]);
280 ise->se_xrates[1] = 0;
281 IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
282 if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) {
284 * Record rssi data using extended precision LPF filter.
286 * NB: use only on-channel data to insure we get a good
287 * estimate of the signal we'll see when associated.
289 IEEE80211_RSSI_LPF(se->se_avgrssi, rssi);
290 ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi);
291 ise->se_noise = noise;
293 memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
294 ise->se_intval = sp->bintval;
295 ise->se_capinfo = sp->capinfo;
296 #ifdef IEEE80211_SUPPORT_MESH
297 if (sp->meshid != NULL && sp->meshid[1] != 0)
298 memcpy(ise->se_meshid, sp->meshid, 2+sp->meshid[1]);
301 * Beware of overriding se_chan for frames seen
302 * off-channel; this can cause us to attempt an
303 * association on the wrong channel.
305 if (sp->status & IEEE80211_BPARSE_OFFCHAN) {
307 * Off-channel, locate the home/bss channel for the sta
308 * using the value broadcast in the DSPARMS ie. We know
309 * sp->chan has this value because it's used to calculate
310 * IEEE80211_BPARSE_OFFCHAN.
312 c = ieee80211_find_channel_byieee(ic, sp->chan,
313 ic->ic_curchan->ic_flags);
316 } else if (ise->se_chan == NULL) {
317 /* should not happen, pick something */
318 ise->se_chan = ic->ic_curchan;
321 ise->se_chan = ic->ic_curchan;
322 if (IEEE80211_IS_CHAN_HT(ise->se_chan) && sp->htcap == NULL) {
323 /* Demote legacy networks to a non-HT channel. */
324 c = ieee80211_find_channel(ic, ise->se_chan->ic_freq,
325 ise->se_chan->ic_flags & ~IEEE80211_CHAN_HT);
327 ("no legacy channel %u", ise->se_chan->ic_ieee));
330 ise->se_fhdwell = sp->fhdwell;
331 ise->se_fhindex = sp->fhindex;
332 ise->se_erp = sp->erp;
333 ise->se_timoff = sp->timoff;
334 if (sp->tim != NULL) {
335 const struct ieee80211_tim_ie *tim =
336 (const struct ieee80211_tim_ie *) sp->tim;
337 ise->se_dtimperiod = tim->tim_period;
339 if (sp->country != NULL) {
340 const struct ieee80211_country_ie *cie =
341 (const struct ieee80211_country_ie *) sp->country;
343 * If 11d is enabled and we're attempting to join a bss
344 * that advertises it's country code then compare our
345 * current settings to what we fetched from the country ie.
346 * If our country code is unspecified or different then
347 * dispatch an event to user space that identifies the
348 * country code so our regdomain config can be changed.
350 /* XXX only for STA mode? */
351 if ((IEEE80211_IS_CHAN_11D(ise->se_chan) ||
352 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
353 (ic->ic_regdomain.country == CTRY_DEFAULT ||
354 !isocmp(cie->cc, ic->ic_regdomain.isocc))) {
355 /* only issue one notify event per scan */
356 if (se->se_countrygen != st->st_scangen) {
357 ieee80211_notify_country(vap, ise->se_bssid,
359 se->se_countrygen = st->st_scangen;
362 ise->se_cc[0] = cie->cc[0];
363 ise->se_cc[1] = cie->cc[1];
365 /* NB: no need to setup ie ptrs; they are not (currently) used */
366 (void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len);
368 /* clear failure count after STA_FAIL_AGE passes */
369 if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) {
371 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
372 "%s: fails %u", __func__, se->se_fails);
375 se->se_lastupdate = ticks; /* update time */
379 KASSERT(sizeof(sp->bchan) == 1, ("bchan size"));
380 if (rssi > st->st_maxrssi[sp->bchan])
381 st->st_maxrssi[sp->bchan] = rssi;
383 IEEE80211_SCAN_TABLE_UNLOCK(st);
386 * If looking for a quick choice and nothing's
387 * been found check here.
389 if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0)
390 ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
398 * Check if a channel is excluded by user request.
401 isexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c)
403 return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) ||
404 (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
405 c->ic_freq != vap->iv_des_chan->ic_freq));
408 static struct ieee80211_channel *
409 find11gchannel(struct ieee80211com *ic, int i, int freq)
411 struct ieee80211_channel *c;
415 * The normal ordering in the channel list is b channel
416 * immediately followed by g so optimize the search for
417 * this. We'll still do a full search just in case.
419 for (j = i+1; j < ic->ic_nchans; j++) {
420 c = &ic->ic_channels[j];
421 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
424 for (j = 0; j < i; j++) {
425 c = &ic->ic_channels[j];
426 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
432 static const u_int chanflags[IEEE80211_MODE_MAX] = {
433 [IEEE80211_MODE_AUTO] = IEEE80211_CHAN_B,
434 [IEEE80211_MODE_11A] = IEEE80211_CHAN_A,
435 [IEEE80211_MODE_11B] = IEEE80211_CHAN_B,
436 [IEEE80211_MODE_11G] = IEEE80211_CHAN_G,
437 [IEEE80211_MODE_FH] = IEEE80211_CHAN_FHSS,
438 /* check base channel */
439 [IEEE80211_MODE_TURBO_A] = IEEE80211_CHAN_A,
440 [IEEE80211_MODE_TURBO_G] = IEEE80211_CHAN_G,
441 [IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_ST,
442 [IEEE80211_MODE_HALF] = IEEE80211_CHAN_HALF,
443 [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_QUARTER,
445 [IEEE80211_MODE_11NA] = IEEE80211_CHAN_A,
446 [IEEE80211_MODE_11NG] = IEEE80211_CHAN_G,
450 add_channels(struct ieee80211vap *vap,
451 struct ieee80211_scan_state *ss,
452 enum ieee80211_phymode mode, const uint16_t freq[], int nfreq)
454 struct ieee80211com *ic = vap->iv_ic;
455 struct ieee80211_channel *c, *cg;
459 KASSERT(mode < nitems(chanflags), ("Unexpected mode %u", mode));
460 modeflags = chanflags[mode];
461 for (i = 0; i < nfreq; i++) {
462 if (ss->ss_last >= IEEE80211_SCAN_MAX)
465 c = ieee80211_find_channel(ic, freq[i], modeflags);
466 if (c == NULL || isexcluded(vap, c))
468 if (mode == IEEE80211_MODE_AUTO) {
470 * XXX special-case 11b/g channels so we select
471 * the g channel if both are present.
473 if (IEEE80211_IS_CHAN_B(c) &&
474 (cg = find11gchannel(ic, i, c->ic_freq)) != NULL)
477 ss->ss_chans[ss->ss_last++] = c;
484 const uint16_t *list;
488 checktable(const struct scanlist *scan, const struct ieee80211_channel *c)
492 for (; scan->list != NULL; scan++) {
493 for (i = 0; i < scan->count; i++)
494 if (scan->list[i] == c->ic_freq)
501 onscanlist(const struct ieee80211_scan_state *ss,
502 const struct ieee80211_channel *c)
506 for (i = 0; i < ss->ss_last; i++)
507 if (ss->ss_chans[i] == c)
513 sweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
514 const struct scanlist table[])
516 struct ieee80211com *ic = vap->iv_ic;
517 struct ieee80211_channel *c;
520 for (i = 0; i < ic->ic_nchans; i++) {
521 if (ss->ss_last >= IEEE80211_SCAN_MAX)
524 c = &ic->ic_channels[i];
526 * Ignore dynamic turbo channels; we scan them
527 * in normal mode (i.e. not boosted). Likewise
528 * for HT channels, they get scanned using
531 if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c))
535 * If a desired mode was specified, scan only
536 * channels that satisfy that constraint.
538 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
539 vap->iv_des_mode != ieee80211_chan2mode(c))
543 * Skip channels excluded by user request.
545 if (isexcluded(vap, c))
549 * Add the channel unless it is listed in the
550 * fixed scan order tables. This insures we
551 * don't sweep back in channels we filtered out
554 if (checktable(table, c))
557 /* Add channel to scanning list. */
558 ss->ss_chans[ss->ss_last++] = c;
561 * Explicitly add any desired channel if:
562 * - not already on the scan list
563 * - allowed by any desired mode constraint
564 * - there is space in the scan list
565 * This allows the channel to be used when the filtering
566 * mechanisms would otherwise elide it (e.g HT, turbo).
568 c = vap->iv_des_chan;
569 if (c != IEEE80211_CHAN_ANYC &&
570 !onscanlist(ss, c) &&
571 (vap->iv_des_mode == IEEE80211_MODE_AUTO ||
572 vap->iv_des_mode == ieee80211_chan2mode(c)) &&
573 ss->ss_last < IEEE80211_SCAN_MAX)
574 ss->ss_chans[ss->ss_last++] = c;
578 makescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
579 const struct scanlist table[])
581 const struct scanlist *scan;
582 enum ieee80211_phymode mode;
586 * Use the table of ordered channels to construct the list
587 * of channels for scanning. Any channels in the ordered
588 * list not in the master list will be discarded.
590 for (scan = table; scan->list != NULL; scan++) {
592 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
594 * If a desired mode was specified, scan only
595 * channels that satisfy that constraint.
597 if (vap->iv_des_mode != mode) {
599 * The scan table marks 2.4Ghz channels as b
600 * so if the desired mode is 11g, then use
601 * the 11b channel list but upgrade the mode.
603 if (vap->iv_des_mode != IEEE80211_MODE_11G ||
604 mode != IEEE80211_MODE_11B)
606 mode = IEEE80211_MODE_11G; /* upgrade */
610 * This lets add_channels upgrade an 11b channel
611 * to 11g if available.
613 if (mode == IEEE80211_MODE_11B)
614 mode = IEEE80211_MODE_AUTO;
616 #ifdef IEEE80211_F_XR
617 /* XR does not operate on turbo channels */
618 if ((vap->iv_flags & IEEE80211_F_XR) &&
619 (mode == IEEE80211_MODE_TURBO_A ||
620 mode == IEEE80211_MODE_TURBO_G ||
621 mode == IEEE80211_MODE_STURBO_A))
625 * Add the list of the channels; any that are not
626 * in the master channel list will be discarded.
628 add_channels(vap, ss, mode, scan->list, scan->count);
632 * Add the channels from the ic that are not present
635 sweepchannels(ss, vap, table);
638 static const uint16_t rcl1[] = /* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
639 { 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
640 static const uint16_t rcl2[] = /* 4 MKK channels: 34, 38, 42, 46 */
641 { 5170, 5190, 5210, 5230 };
642 static const uint16_t rcl3[] = /* 2.4Ghz ch: 1,6,11,7,13 */
643 { 2412, 2437, 2462, 2442, 2472 };
644 static const uint16_t rcl4[] = /* 5 FCC channel: 149, 153, 161, 165 */
645 { 5745, 5765, 5785, 5805, 5825 };
646 static const uint16_t rcl7[] = /* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
647 { 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
648 static const uint16_t rcl8[] = /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
649 { 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
650 static const uint16_t rcl9[] = /* 2.4Ghz ch: 14 */
652 static const uint16_t rcl10[] = /* Added Korean channels 2312-2372 */
653 { 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
654 static const uint16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */
655 { 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
656 #ifdef ATH_TURBO_SCAN
657 static const uint16_t rcl5[] = /* 3 static turbo channels */
658 { 5210, 5250, 5290 };
659 static const uint16_t rcl6[] = /* 2 static turbo channels */
661 static const uint16_t rcl6x[] = /* 4 FCC3 turbo channels */
662 { 5540, 5580, 5620, 5660 };
663 static const uint16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */
665 static const uint16_t rcl13[] = /* dynamic Turbo channels */
666 { 5200, 5240, 5280, 5765, 5805 };
667 #endif /* ATH_TURBO_SCAN */
669 #define X(a) .count = sizeof(a)/sizeof(a[0]), .list = a
671 static const struct scanlist staScanTable[] = {
672 { IEEE80211_MODE_11B, X(rcl3) },
673 { IEEE80211_MODE_11A, X(rcl1) },
674 { IEEE80211_MODE_11A, X(rcl2) },
675 { IEEE80211_MODE_11B, X(rcl8) },
676 { IEEE80211_MODE_11B, X(rcl9) },
677 { IEEE80211_MODE_11A, X(rcl4) },
678 #ifdef ATH_TURBO_SCAN
679 { IEEE80211_MODE_STURBO_A, X(rcl5) },
680 { IEEE80211_MODE_STURBO_A, X(rcl6) },
681 { IEEE80211_MODE_TURBO_A, X(rcl6x) },
682 { IEEE80211_MODE_TURBO_A, X(rcl13) },
683 #endif /* ATH_TURBO_SCAN */
684 { IEEE80211_MODE_11A, X(rcl7) },
685 { IEEE80211_MODE_11B, X(rcl10) },
686 { IEEE80211_MODE_11A, X(rcl11) },
687 #ifdef ATH_TURBO_SCAN
688 { IEEE80211_MODE_TURBO_G, X(rcl12) },
689 #endif /* ATH_TURBO_SCAN */
694 * Start a station-mode scan by populating the channel list.
697 sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
699 struct sta_table *st = ss->ss_priv;
701 makescanlist(ss, vap, staScanTable);
703 if (ss->ss_mindwell == 0)
704 ss->ss_mindwell = msecs_to_ticks(20); /* 20ms */
705 if (ss->ss_maxdwell == 0)
706 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
715 * Restart a scan, typically a bg scan but can
716 * also be a fg scan that came up empty.
719 sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
721 struct sta_table *st = ss->ss_priv;
728 * Cancel an ongoing scan.
731 sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
736 /* unaligned little endian access */
737 #define LE_READ_2(p) \
739 ((((const uint8_t *)(p))[0] ) | \
740 (((const uint8_t *)(p))[1] << 8)))
743 * Demote any supplied 11g channel to 11b. There should
744 * always be an 11b channel but we check anyway...
746 static struct ieee80211_channel *
747 demote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan)
749 struct ieee80211_channel *c;
751 if (IEEE80211_IS_CHAN_ANYG(chan) &&
752 vap->iv_des_mode == IEEE80211_MODE_AUTO) {
753 c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq,
754 (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) |
763 maxrate(const struct ieee80211_scan_entry *se)
765 const struct ieee80211_ie_htcap *htcap =
766 (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie;
767 int rmax, r, i, txstream;
774 * HT station; inspect supported MCS and then adjust
775 * rate by channel width.
777 txparams = htcap->hc_mcsset[12];
778 if (txparams & 0x3) {
780 * TX MCS parameters defined and not equal to RX,
781 * extract the number of spartial streams and
782 * map it to the highest MCS rate.
784 txstream = ((txparams & 0xc) >> 2) + 1;
785 i = txstream * 8 - 1;
787 for (i = 31; i >= 0 && isclr(htcap->hc_mcsset, i); i--);
789 caps = LE_READ_2(&htcap->hc_cap);
790 if ((caps & IEEE80211_HTCAP_CHWIDTH40) &&
791 (caps & IEEE80211_HTCAP_SHORTGI40))
792 rmax = ieee80211_htrates[i].ht40_rate_400ns;
793 else if (caps & IEEE80211_HTCAP_CHWIDTH40)
794 rmax = ieee80211_htrates[i].ht40_rate_800ns;
795 else if (caps & IEEE80211_HTCAP_SHORTGI20)
796 rmax = ieee80211_htrates[i].ht20_rate_400ns;
798 rmax = ieee80211_htrates[i].ht20_rate_800ns;
801 for (i = 0; i < se->se_rates[1]; i++) {
802 r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
806 for (i = 0; i < se->se_xrates[1]; i++) {
807 r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
815 * Compare the capabilities of two entries and decide which is
816 * more desirable (return >0 if a is considered better). Note
817 * that we assume compatibility/usability has already been checked
818 * so we don't need to (e.g. validate whether privacy is supported).
819 * Used to select the best scan candidate for association in a BSS.
822 sta_compare(const struct sta_entry *a, const struct sta_entry *b)
824 #define PREFER(_a,_b,_what) do { \
825 if (((_a) ^ (_b)) & (_what)) \
826 return ((_a) & (_what)) ? 1 : -1; \
832 /* privacy support */
833 PREFER(a->base.se_capinfo, b->base.se_capinfo,
834 IEEE80211_CAPINFO_PRIVACY);
836 /* compare count of previous failures */
837 weight = b->se_fails - a->se_fails;
842 * Compare rssi. If the two are considered equivalent
843 * then fallback to other criteria. We threshold the
844 * comparisons to avoid selecting an ap purely by rssi
845 * when both values may be good but one ap is otherwise
846 * more desirable (e.g. an 11b-only ap with stronger
847 * signal than an 11g ap).
849 rssia = MIN(a->base.se_rssi, STA_RSSI_MAX);
850 rssib = MIN(b->base.se_rssi, STA_RSSI_MAX);
851 if (abs(rssib - rssia) < 5) {
852 /* best/max rate preferred if signal level close enough XXX */
853 maxa = maxrate(&a->base);
854 maxb = maxrate(&b->base);
857 /* XXX use freq for channel preference */
858 /* for now just prefer 5Ghz band to all other bands */
859 PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan),
860 IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1);
862 /* all things being equal, use signal level */
863 return a->base.se_rssi - b->base.se_rssi;
868 * Check rate set suitability and return the best supported rate.
869 * XXX inspect MCS for HT
872 check_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan,
873 const struct ieee80211_scan_entry *se)
875 #define RV(v) ((v) & IEEE80211_RATE_VAL)
876 const struct ieee80211_rateset *srs;
877 int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate;
880 okrate = badrate = 0;
882 srs = ieee80211_get_suprates(vap->iv_ic, chan);
883 nrs = se->se_rates[1];
886 ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate;
887 fixedrate = IEEE80211_FIXED_RATE_NONE;
889 for (i = 0; i < nrs; i++) {
893 * Check any fixed rate is included.
898 * Check against our supported rates.
900 for (j = 0; j < srs->rs_nrates; j++)
901 if (r == RV(srs->rs_rates[j])) {
902 if (r > okrate) /* NB: track max */
907 if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) {
909 * Don't try joining a BSS, if we don't support
910 * one of its basic rates.
916 if (rs == se->se_rates+2) {
917 /* scan xrates too; sort of an algol68-style for loop */
918 nrs = se->se_xrates[1];
919 rs = se->se_xrates+2;
924 if (okrate == 0 || ucastrate != fixedrate)
925 return badrate | IEEE80211_RATE_BASIC;
932 match_id(const uint8_t *ie, const uint8_t *val, int len)
934 return (ie[1] == len && memcmp(ie+2, val, len) == 0);
938 match_ssid(const uint8_t *ie,
939 int nssid, const struct ieee80211_scan_ssid ssids[])
943 for (i = 0; i < nssid; i++) {
944 if (match_id(ie, ssids[i].ssid, ssids[i].len))
950 #ifdef IEEE80211_SUPPORT_TDMA
952 tdma_isfull(const struct ieee80211_tdma_param *tdma)
956 slotcnt = tdma->tdma_slotcnt;
957 for (slot = slotcnt-1; slot >= 0; slot--)
958 if (isclr(tdma->tdma_inuse, slot))
962 #endif /* IEEE80211_SUPPORT_TDMA */
965 * Test a scan candidate for suitability/compatibility.
968 match_bss(struct ieee80211vap *vap,
969 const struct ieee80211_scan_state *ss, struct sta_entry *se0,
972 struct ieee80211com *ic = vap->iv_ic;
973 struct ieee80211_scan_entry *se = &se0->base;
978 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
979 fail |= MATCH_CHANNEL;
981 * NB: normally the desired mode is used to construct
982 * the channel list, but it's possible for the scan
983 * cache to include entries for stations outside this
984 * list so we check the desired mode here to weed them
987 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
988 (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
989 chanflags[vap->iv_des_mode])
990 fail |= MATCH_CHANNEL;
991 if (vap->iv_opmode == IEEE80211_M_IBSS) {
992 if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
993 fail |= MATCH_CAPINFO;
994 #ifdef IEEE80211_SUPPORT_TDMA
995 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
997 * Adhoc demo network setup shouldn't really be scanning
998 * but just in case skip stations operating in IBSS or
1001 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
1002 fail |= MATCH_CAPINFO;
1004 * TDMA operation cannot coexist with a normal 802.11 network;
1005 * skip if IBSS or ESS capabilities are marked and require
1006 * the beacon have a TDMA ie present.
1008 if (vap->iv_caps & IEEE80211_C_TDMA) {
1009 const struct ieee80211_tdma_param *tdma =
1010 (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie;
1011 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
1014 fail |= MATCH_TDMA_NOIE;
1015 else if (tdma->tdma_version != ts->tdma_version)
1016 fail |= MATCH_TDMA_VERSION;
1017 else if (tdma->tdma_slot != 0)
1018 fail |= MATCH_TDMA_NOTMASTER;
1019 else if (tdma_isfull(tdma))
1020 fail |= MATCH_TDMA_NOSLOT;
1022 else if (ieee80211_local_address(se->se_macaddr))
1023 fail |= MATCH_TDMA_LOCAL;
1026 #endif /* IEEE80211_SUPPORT_TDMA */
1027 #ifdef IEEE80211_SUPPORT_MESH
1028 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
1029 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
1031 * Mesh nodes have IBSS & ESS bits in capinfo turned off
1032 * and two special ie's that must be present.
1034 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
1035 fail |= MATCH_CAPINFO;
1036 else if (se->se_meshid[0] != IEEE80211_ELEMID_MESHID)
1037 fail |= MATCH_MESH_NOID;
1038 else if (ms->ms_idlen != 0 &&
1039 match_id(se->se_meshid, ms->ms_id, ms->ms_idlen))
1040 fail |= MATCH_MESHID;
1043 if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
1044 fail |= MATCH_CAPINFO;
1046 * If 11d is enabled and we're attempting to join a bss
1047 * that advertises it's country code then compare our
1048 * current settings to what we fetched from the country ie.
1049 * If our country code is unspecified or different then do
1050 * not attempt to join the bss. We should have already
1051 * dispatched an event to user space that identifies the
1052 * new country code so our regdomain config should match.
1054 if ((IEEE80211_IS_CHAN_11D(se->se_chan) ||
1055 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
1056 se->se_cc[0] != 0 &&
1057 (ic->ic_regdomain.country == CTRY_DEFAULT ||
1058 !isocmp(se->se_cc, ic->ic_regdomain.isocc)))
1061 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1062 if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
1063 fail |= MATCH_PRIVACY;
1065 /* XXX does this mean privacy is supported or required? */
1066 if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
1067 fail |= MATCH_PRIVACY;
1069 se0->se_flags &= ~STA_DEMOTE11B;
1070 rate = check_rate(vap, se->se_chan, se);
1071 if (rate & IEEE80211_RATE_BASIC) {
1074 * An 11b-only ap will give a rate mismatch if there is an
1075 * OFDM fixed tx rate for 11g. Try downgrading the channel
1076 * in the scan list to 11b and retry the rate check.
1078 if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) {
1079 rate = check_rate(vap, demote11b(vap, se->se_chan), se);
1080 if ((rate & IEEE80211_RATE_BASIC) == 0) {
1081 fail &= ~MATCH_RATE;
1082 se0->se_flags |= STA_DEMOTE11B;
1085 } else if (rate < 2*24) {
1087 * This is an 11b-only ap. Check the desired mode in
1088 * case that needs to be honored (mode 11g filters out
1089 * 11b-only ap's). Otherwise force any 11g channel used
1090 * in scanning to be demoted.
1092 * NB: we cheat a bit here by looking at the max rate;
1093 * we could/should check the rates.
1095 if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO ||
1096 vap->iv_des_mode == IEEE80211_MODE_11B))
1099 se0->se_flags |= STA_DEMOTE11B;
1101 if (ss->ss_nssid != 0 &&
1102 !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
1104 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
1105 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
1106 fail |= MATCH_BSSID;
1107 if (se0->se_fails >= STA_FAILS_MAX)
1108 fail |= MATCH_FAILS;
1109 if (se0->se_notseen >= STA_PURGE_SCANS)
1110 fail |= MATCH_NOTSEEN;
1111 if (se->se_rssi < STA_RSSI_MIN)
1113 #ifdef IEEE80211_DEBUG
1114 if (ieee80211_msg(vap, debug)) {
1116 fail & MATCH_FAILS ? '=' :
1117 fail & MATCH_NOTSEEN ? '^' :
1118 fail & MATCH_CC ? '$' :
1119 #ifdef IEEE80211_SUPPORT_TDMA
1120 fail & MATCH_TDMA_NOIE ? '&' :
1121 fail & MATCH_TDMA_VERSION ? 'v' :
1122 fail & MATCH_TDMA_NOTMASTER ? 's' :
1123 fail & MATCH_TDMA_NOSLOT ? 'f' :
1124 fail & MATCH_TDMA_LOCAL ? 'l' :
1126 fail & MATCH_MESH_NOID ? 'm' :
1127 fail ? '-' : '+', ether_sprintf(se->se_macaddr));
1128 printf(" %s%c", ether_sprintf(se->se_bssid),
1129 fail & MATCH_BSSID ? '!' : ' ');
1130 printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
1131 fail & MATCH_CHANNEL ? '!' : ' ');
1132 printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' ');
1133 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
1134 fail & MATCH_RATE ? '!' : ' ');
1136 (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
1137 (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : "",
1138 fail & MATCH_CAPINFO ? '!' : ' ');
1140 (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
1142 fail & MATCH_PRIVACY ? '!' : ' ');
1143 ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]);
1144 printf("%s\n", fail & (MATCH_SSID | MATCH_MESHID) ? "!" : "");
1151 sta_update_notseen(struct sta_table *st)
1153 struct sta_entry *se;
1155 IEEE80211_SCAN_TABLE_LOCK(st);
1156 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1158 * If seen the reset and don't bump the count;
1159 * otherwise bump the ``not seen'' count. Note
1160 * that this insures that stations for which we
1161 * see frames while not scanning but not during
1162 * this scan will not be penalized.
1169 IEEE80211_SCAN_TABLE_UNLOCK(st);
1173 sta_dec_fails(struct sta_table *st)
1175 struct sta_entry *se;
1177 IEEE80211_SCAN_TABLE_LOCK(st);
1178 TAILQ_FOREACH(se, &st->st_entry, se_list)
1181 IEEE80211_SCAN_TABLE_UNLOCK(st);
1184 static struct sta_entry *
1185 select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug)
1187 struct sta_table *st = ss->ss_priv;
1188 struct sta_entry *se, *selbs = NULL;
1190 IEEE80211_DPRINTF(vap, debug, " %s\n",
1191 "macaddr bssid chan rssi rate flag wep essid");
1192 IEEE80211_SCAN_TABLE_LOCK(st);
1193 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1194 ieee80211_ies_expand(&se->base.se_ies);
1195 if (match_bss(vap, ss, se, debug) == 0) {
1198 else if (sta_compare(se, selbs) > 0)
1202 IEEE80211_SCAN_TABLE_UNLOCK(st);
1208 * Pick an ap or ibss network to join or find a channel
1209 * to use to start an ibss network.
1212 sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1214 struct sta_table *st = ss->ss_priv;
1215 struct sta_entry *selbs;
1216 struct ieee80211_channel *chan;
1218 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1219 ("wrong mode %u", vap->iv_opmode));
1221 if (st->st_newscan) {
1222 sta_update_notseen(st);
1225 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1227 * Manual/background scan, don't select+join the
1228 * bss, just return. The scanning framework will
1229 * handle notification that this has completed.
1231 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1235 * Automatic sequencing; look for a candidate and
1236 * if found join the network.
1238 /* NB: unlocked read should be ok */
1239 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1240 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1241 "%s: no scan candidate\n", __func__);
1242 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1246 * If nothing suitable was found decrement
1247 * the failure counts so entries will be
1248 * reconsidered the next time around. We
1249 * really want to do this only for sta's
1250 * where we've previously had some success.
1254 return 0; /* restart scan */
1256 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1257 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1258 return (selbs != NULL);
1261 chan = selbs->base.se_chan;
1262 if (selbs->se_flags & STA_DEMOTE11B)
1263 chan = demote11b(vap, chan);
1264 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1266 return 1; /* terminate scan */
1270 * Lookup an entry in the scan cache. We assume we're
1271 * called from the bottom half or such that we don't need
1272 * to block the bottom half so that it's safe to return
1273 * a reference to an entry w/o holding the lock on the table.
1275 static struct sta_entry *
1276 sta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1278 struct sta_entry *se;
1279 int hash = STA_HASH(macaddr);
1281 IEEE80211_SCAN_TABLE_LOCK(st);
1282 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
1283 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
1285 IEEE80211_SCAN_TABLE_UNLOCK(st);
1287 return se; /* NB: unlocked */
1291 sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1293 struct ieee80211com *ic = vap->iv_ic;
1294 struct ieee80211_node *ni = vap->iv_bss;
1295 struct sta_table *st = ss->ss_priv;
1296 enum ieee80211_phymode mode;
1297 struct sta_entry *se, *selbs;
1298 uint8_t roamRate, curRate, ucastRate;
1299 int8_t roamRssi, curRssi;
1301 se = sta_lookup(st, ni->ni_macaddr);
1303 /* XXX something is wrong */
1307 mode = ieee80211_chan2mode(ic->ic_bsschan);
1308 roamRate = vap->iv_roamparms[mode].rate;
1309 roamRssi = vap->iv_roamparms[mode].rssi;
1310 ucastRate = vap->iv_txparms[mode].ucastrate;
1311 /* NB: the most up to date rssi is in the node, not the scan cache */
1312 curRssi = ic->ic_node_getrssi(ni);
1313 if (ucastRate == IEEE80211_FIXED_RATE_NONE) {
1314 curRate = ni->ni_txrate;
1315 roamRate &= IEEE80211_RATE_VAL;
1316 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1317 "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
1318 __func__, curRssi, curRate, roamRssi, roamRate);
1320 curRate = roamRate; /* NB: insure compare below fails */
1321 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1322 "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi);
1325 * Check if a new ap should be used and switch.
1326 * XXX deauth current ap
1328 if (curRate < roamRate || curRssi < roamRssi) {
1329 if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
1331 * Scan cache contents are too old; force a scan now
1332 * if possible so we have current state to make a
1333 * decision with. We don't kick off a bg scan if
1334 * we're using dynamic turbo and boosted or if the
1336 * XXX force immediate switch on scan complete
1338 if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1339 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle))
1340 ieee80211_bg_scan(vap, 0);
1343 se->base.se_rssi = curRssi;
1344 selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM);
1345 if (selbs != NULL && selbs != se) {
1346 struct ieee80211_channel *chan;
1348 IEEE80211_DPRINTF(vap,
1349 IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG,
1350 "%s: ROAM: curRate %u, roamRate %u, "
1351 "curRssi %d, roamRssi %d\n", __func__,
1352 curRate, roamRate, curRssi, roamRssi);
1354 chan = selbs->base.se_chan;
1355 if (selbs->se_flags & STA_DEMOTE11B)
1356 chan = demote11b(vap, chan);
1357 (void) ieee80211_sta_join(vap, chan, &selbs->base);
1363 * Age entries in the scan cache.
1364 * XXX also do roaming since it's convenient
1367 sta_age(struct ieee80211_scan_state *ss)
1369 struct ieee80211vap *vap = ss->ss_vap;
1373 * If rate control is enabled check periodically to see if
1374 * we should roam from our current connection to one that
1375 * might be better. This only applies when we're operating
1376 * in sta mode and automatic roaming is set.
1378 * XXX repeater station
1379 * XXX do when !bgscan?
1381 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1382 ("wrong mode %u", vap->iv_opmode));
1383 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO &&
1384 (vap->iv_flags & IEEE80211_F_BGSCAN) &&
1385 vap->iv_state >= IEEE80211_S_RUN)
1386 /* XXX vap is implicit */
1387 sta_roam_check(ss, vap);
1391 * Iterate over the entries in the scan cache, invoking
1392 * the callback function on each one.
1395 sta_iterate(struct ieee80211_scan_state *ss,
1396 ieee80211_scan_iter_func *f, void *arg)
1398 struct sta_table *st = ss->ss_priv;
1399 struct sta_entry *se;
1402 mtx_lock(&st->st_scanlock);
1403 gen = st->st_scaniter++;
1405 IEEE80211_SCAN_TABLE_LOCK(st);
1406 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1407 if (se->se_scangen != gen) {
1408 se->se_scangen = gen;
1409 /* update public state */
1410 se->base.se_age = ticks - se->se_lastupdate;
1411 IEEE80211_SCAN_TABLE_UNLOCK(st);
1412 (*f)(arg, &se->base);
1416 IEEE80211_SCAN_TABLE_UNLOCK(st);
1418 mtx_unlock(&st->st_scanlock);
1422 sta_assoc_fail(struct ieee80211_scan_state *ss,
1423 const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason)
1425 struct sta_table *st = ss->ss_priv;
1426 struct sta_entry *se;
1428 se = sta_lookup(st, macaddr);
1431 se->se_lastfail = ticks;
1432 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1433 macaddr, "%s: reason %u fails %u",
1434 __func__, reason, se->se_fails);
1439 sta_assoc_success(struct ieee80211_scan_state *ss,
1440 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1442 struct sta_table *st = ss->ss_priv;
1443 struct sta_entry *se;
1445 se = sta_lookup(st, macaddr);
1449 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1450 macaddr, "%s: fails %u",
1451 __func__, se->se_fails);
1453 se->se_lastassoc = ticks;
1457 static const struct ieee80211_scanner sta_default = {
1458 .scan_name = "default",
1459 .scan_attach = sta_attach,
1460 .scan_detach = sta_detach,
1461 .scan_start = sta_start,
1462 .scan_restart = sta_restart,
1463 .scan_cancel = sta_cancel,
1464 .scan_end = sta_pick_bss,
1465 .scan_flush = sta_flush,
1466 .scan_add = sta_add,
1467 .scan_age = sta_age,
1468 .scan_iterate = sta_iterate,
1469 .scan_assoc_fail = sta_assoc_fail,
1470 .scan_assoc_success = sta_assoc_success,
1472 IEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default);
1475 * Adhoc mode-specific support.
1478 static const uint16_t adhocWorld[] = /* 36, 40, 44, 48 */
1479 { 5180, 5200, 5220, 5240 };
1480 static const uint16_t adhocFcc3[] = /* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */
1481 { 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 };
1482 static const uint16_t adhocMkk[] = /* 34, 38, 42, 46 */
1483 { 5170, 5190, 5210, 5230 };
1484 static const uint16_t adhoc11b[] = /* 10, 11 */
1487 static const struct scanlist adhocScanTable[] = {
1488 { IEEE80211_MODE_11B, X(adhoc11b) },
1489 { IEEE80211_MODE_11A, X(adhocWorld) },
1490 { IEEE80211_MODE_11A, X(adhocFcc3) },
1491 { IEEE80211_MODE_11B, X(adhocMkk) },
1497 * Start an adhoc-mode scan by populating the channel list.
1500 adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1502 struct sta_table *st = ss->ss_priv;
1504 makescanlist(ss, vap, adhocScanTable);
1506 if (ss->ss_mindwell == 0)
1507 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */
1508 if (ss->ss_maxdwell == 0)
1509 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
1518 * Select a channel to start an adhoc network on.
1519 * The channel list was populated with appropriate
1520 * channels so select one that looks least occupied.
1522 static struct ieee80211_channel *
1523 adhoc_pick_channel(struct ieee80211_scan_state *ss, int flags)
1525 struct sta_table *st = ss->ss_priv;
1526 struct sta_entry *se;
1527 struct ieee80211_channel *c, *bestchan;
1528 int i, bestrssi, maxrssi;
1533 IEEE80211_SCAN_TABLE_LOCK(st);
1534 for (i = 0; i < ss->ss_last; i++) {
1535 c = ss->ss_chans[i];
1536 /* never consider a channel with radar */
1537 if (IEEE80211_IS_CHAN_RADAR(c))
1539 /* skip channels disallowed by regulatory settings */
1540 if (IEEE80211_IS_CHAN_NOADHOC(c))
1542 /* check channel attributes for band compatibility */
1543 if (flags != 0 && (c->ic_flags & flags) != flags)
1546 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1547 if (se->base.se_chan != c)
1549 if (se->base.se_rssi > maxrssi)
1550 maxrssi = se->base.se_rssi;
1552 if (bestchan == NULL || maxrssi < bestrssi)
1555 IEEE80211_SCAN_TABLE_UNLOCK(st);
1561 * Pick an ibss network to join or find a channel
1562 * to use to start an ibss network.
1565 adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1567 struct sta_table *st = ss->ss_priv;
1568 struct sta_entry *selbs;
1569 struct ieee80211_channel *chan;
1570 struct ieee80211com *ic = vap->iv_ic;
1572 KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1573 vap->iv_opmode == IEEE80211_M_AHDEMO ||
1574 vap->iv_opmode == IEEE80211_M_MBSS,
1575 ("wrong opmode %u", vap->iv_opmode));
1577 if (st->st_newscan) {
1578 sta_update_notseen(st);
1581 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1583 * Manual/background scan, don't select+join the
1584 * bss, just return. The scanning framework will
1585 * handle notification that this has completed.
1587 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1591 * Automatic sequencing; look for a candidate and
1592 * if found join the network.
1594 /* NB: unlocked read should be ok */
1595 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1596 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1597 "%s: no scan candidate\n", __func__);
1598 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1601 /* NB: never auto-start a tdma network for slot !0 */
1602 #ifdef IEEE80211_SUPPORT_TDMA
1603 if (vap->iv_des_nssid &&
1604 ((vap->iv_caps & IEEE80211_C_TDMA) == 0 ||
1605 ieee80211_tdma_getslot(vap) == 0)) {
1607 if (vap->iv_des_nssid) {
1610 * No existing adhoc network to join and we have
1611 * an ssid; start one up. If no channel was
1612 * specified, try to select a channel.
1614 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1615 IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1616 chan = adhoc_pick_channel(ss, 0);
1618 chan = vap->iv_des_chan;
1620 struct ieee80211com *ic = vap->iv_ic;
1622 * Create a HT capable IBSS; the per-node
1623 * probe request/response will result in
1624 * "correct" rate control capabilities being
1627 chan = ieee80211_ht_adjust_channel(ic,
1628 chan, vap->iv_flags_ht);
1629 ieee80211_create_ibss(vap, chan);
1634 * If nothing suitable was found decrement
1635 * the failure counts so entries will be
1636 * reconsidered the next time around. We
1637 * really want to do this only for sta's
1638 * where we've previously had some success.
1642 return 0; /* restart scan */
1644 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1645 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1646 return (selbs != NULL);
1649 chan = selbs->base.se_chan;
1650 if (selbs->se_flags & STA_DEMOTE11B)
1651 chan = demote11b(vap, chan);
1653 * If HT is available, make it a possibility here.
1654 * The intent is to enable HT20/HT40 when joining a non-HT
1655 * IBSS node; we can then advertise HT IEs and speak HT
1656 * to any subsequent nodes that support it.
1658 chan = ieee80211_ht_adjust_channel(ic,
1659 chan, vap->iv_flags_ht);
1660 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1662 return 1; /* terminate scan */
1666 * Age entries in the scan cache.
1669 adhoc_age(struct ieee80211_scan_state *ss)
1671 struct sta_table *st = ss->ss_priv;
1672 struct sta_entry *se, *next;
1674 IEEE80211_SCAN_TABLE_LOCK(st);
1675 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
1676 if (se->se_notseen > STA_PURGE_SCANS) {
1677 TAILQ_REMOVE(&st->st_entry, se, se_list);
1678 LIST_REMOVE(se, se_hash);
1679 ieee80211_ies_cleanup(&se->base.se_ies);
1680 free(se, M_80211_SCAN);
1683 IEEE80211_SCAN_TABLE_UNLOCK(st);
1686 static const struct ieee80211_scanner adhoc_default = {
1687 .scan_name = "default",
1688 .scan_attach = sta_attach,
1689 .scan_detach = sta_detach,
1690 .scan_start = adhoc_start,
1691 .scan_restart = sta_restart,
1692 .scan_cancel = sta_cancel,
1693 .scan_end = adhoc_pick_bss,
1694 .scan_flush = sta_flush,
1695 .scan_pickchan = adhoc_pick_channel,
1696 .scan_add = sta_add,
1697 .scan_age = adhoc_age,
1698 .scan_iterate = sta_iterate,
1699 .scan_assoc_fail = sta_assoc_fail,
1700 .scan_assoc_success = sta_assoc_success,
1702 IEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default);
1703 IEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default);
1706 ap_force_promisc(struct ieee80211com *ic)
1708 struct ifnet *ifp = ic->ic_ifp;
1711 /* set interface into promiscuous mode */
1712 ifp->if_flags |= IFF_PROMISC;
1713 ieee80211_runtask(ic, &ic->ic_promisc_task);
1714 IEEE80211_UNLOCK(ic);
1718 ap_reset_promisc(struct ieee80211com *ic)
1721 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
1722 IEEE80211_UNLOCK(ic);
1726 ap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1728 struct sta_table *st = ss->ss_priv;
1730 makescanlist(ss, vap, staScanTable);
1732 if (ss->ss_mindwell == 0)
1733 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */
1734 if (ss->ss_maxdwell == 0)
1735 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
1740 ap_force_promisc(vap->iv_ic);
1745 * Cancel an ongoing scan.
1748 ap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1750 ap_reset_promisc(vap->iv_ic);
1755 * Pick a quiet channel to use for ap operation.
1757 static struct ieee80211_channel *
1758 ap_pick_channel(struct ieee80211_scan_state *ss, int flags)
1760 struct sta_table *st = ss->ss_priv;
1761 struct ieee80211_channel *bestchan = NULL;
1764 /* XXX select channel more intelligently, e.g. channel spread, power */
1765 /* NB: use scan list order to preserve channel preference */
1766 for (i = 0; i < ss->ss_last; i++) {
1767 struct ieee80211_channel *chan = ss->ss_chans[i];
1769 * If the channel is unoccupied the max rssi
1770 * should be zero; just take it. Otherwise
1771 * track the channel with the lowest rssi and
1772 * use that when all channels appear occupied.
1774 if (IEEE80211_IS_CHAN_RADAR(chan))
1776 if (IEEE80211_IS_CHAN_NOHOSTAP(chan))
1778 /* check channel attributes for band compatibility */
1779 if (flags != 0 && (chan->ic_flags & flags) != flags)
1781 KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size"));
1782 /* XXX channel have interference */
1783 if (st->st_maxrssi[chan->ic_ieee] == 0) {
1784 /* XXX use other considerations */
1787 if (bestchan == NULL ||
1788 st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee])
1795 * Pick a quiet channel to use for ap operation.
1798 ap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1800 struct ieee80211com *ic = vap->iv_ic;
1801 struct ieee80211_channel *bestchan;
1803 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
1804 ("wrong opmode %u", vap->iv_opmode));
1805 bestchan = ap_pick_channel(ss, 0);
1806 if (bestchan == NULL) {
1807 /* no suitable channel, should not happen */
1808 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1809 "%s: no suitable channel! (should not happen)\n", __func__);
1810 /* XXX print something? */
1811 return 0; /* restart scan */
1814 * If this is a dynamic turbo channel, start with the unboosted one.
1816 if (IEEE80211_IS_CHAN_TURBO(bestchan)) {
1817 bestchan = ieee80211_find_channel(ic, bestchan->ic_freq,
1818 bestchan->ic_flags & ~IEEE80211_CHAN_TURBO);
1819 if (bestchan == NULL) {
1820 /* should never happen ?? */
1824 ap_reset_promisc(ic);
1825 if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) {
1827 * Manual/background scan, don't select+join the
1828 * bss, just return. The scanning framework will
1829 * handle notification that this has completed.
1831 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1834 ieee80211_create_ibss(vap,
1835 ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ht));
1839 static const struct ieee80211_scanner ap_default = {
1840 .scan_name = "default",
1841 .scan_attach = sta_attach,
1842 .scan_detach = sta_detach,
1843 .scan_start = ap_start,
1844 .scan_restart = sta_restart,
1845 .scan_cancel = ap_cancel,
1847 .scan_flush = sta_flush,
1848 .scan_pickchan = ap_pick_channel,
1849 .scan_add = sta_add,
1850 .scan_age = adhoc_age,
1851 .scan_iterate = sta_iterate,
1852 .scan_assoc_success = sta_assoc_success,
1853 .scan_assoc_fail = sta_assoc_fail,
1855 IEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
1857 #ifdef IEEE80211_SUPPORT_MESH
1859 * Pick an mbss network to join or find a channel
1860 * to use to start an mbss network.
1863 mesh_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1865 struct sta_table *st = ss->ss_priv;
1866 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1867 struct sta_entry *selbs;
1868 struct ieee80211_channel *chan;
1870 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
1871 ("wrong opmode %u", vap->iv_opmode));
1873 if (st->st_newscan) {
1874 sta_update_notseen(st);
1877 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1879 * Manual/background scan, don't select+join the
1880 * bss, just return. The scanning framework will
1881 * handle notification that this has completed.
1883 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1887 * Automatic sequencing; look for a candidate and
1888 * if found join the network.
1890 /* NB: unlocked read should be ok */
1891 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1892 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1893 "%s: no scan candidate\n", __func__);
1894 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1897 if (ms->ms_idlen != 0) {
1899 * No existing mbss network to join and we have
1900 * a meshid; start one up. If no channel was
1901 * specified, try to select a channel.
1903 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1904 IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1905 struct ieee80211com *ic = vap->iv_ic;
1907 chan = adhoc_pick_channel(ss, 0);
1909 chan = ieee80211_ht_adjust_channel(ic,
1910 chan, vap->iv_flags_ht);
1912 chan = vap->iv_des_chan;
1914 ieee80211_create_ibss(vap, chan);
1919 * If nothing suitable was found decrement
1920 * the failure counts so entries will be
1921 * reconsidered the next time around. We
1922 * really want to do this only for sta's
1923 * where we've previously had some success.
1927 return 0; /* restart scan */
1929 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1930 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1931 return (selbs != NULL);
1934 chan = selbs->base.se_chan;
1935 if (selbs->se_flags & STA_DEMOTE11B)
1936 chan = demote11b(vap, chan);
1937 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1939 return 1; /* terminate scan */
1942 static const struct ieee80211_scanner mesh_default = {
1943 .scan_name = "default",
1944 .scan_attach = sta_attach,
1945 .scan_detach = sta_detach,
1946 .scan_start = adhoc_start,
1947 .scan_restart = sta_restart,
1948 .scan_cancel = sta_cancel,
1949 .scan_end = mesh_pick_bss,
1950 .scan_flush = sta_flush,
1951 .scan_pickchan = adhoc_pick_channel,
1952 .scan_add = sta_add,
1953 .scan_age = adhoc_age,
1954 .scan_iterate = sta_iterate,
1955 .scan_assoc_fail = sta_assoc_fail,
1956 .scan_assoc_success = sta_assoc_success,
1958 IEEE80211_SCANNER_ALG(mesh, IEEE80211_M_MBSS, mesh_default);
1959 #endif /* IEEE80211_SUPPORT_MESH */