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 ieee80211_scan_iter_lock_t 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 *) IEEE80211_MALLOC(sizeof(struct sta_table),
164 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
167 IEEE80211_SCAN_TABLE_LOCK_INIT(st, "scantable");
168 IEEE80211_SCAN_ITER_LOCK_INIT(st, "scangen");
169 TAILQ_INIT(&st->st_entry);
171 nrefs++; /* NB: we assume caller locking */
176 * Cleanup any private state.
179 sta_detach(struct ieee80211_scan_state *ss)
181 struct sta_table *st = ss->ss_priv;
185 IEEE80211_SCAN_TABLE_LOCK_DESTROY(st);
186 IEEE80211_SCAN_ITER_LOCK_DESTROY(st);
187 IEEE80211_FREE(st, M_80211_SCAN);
188 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
189 nrefs--; /* NB: we assume caller locking */
195 * Flush all per-scan state.
198 sta_flush(struct ieee80211_scan_state *ss)
200 struct sta_table *st = ss->ss_priv;
202 IEEE80211_SCAN_TABLE_LOCK(st);
204 IEEE80211_SCAN_TABLE_UNLOCK(st);
210 * Flush all entries in the scan cache.
213 sta_flush_table(struct sta_table *st)
215 struct sta_entry *se, *next;
217 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
218 TAILQ_REMOVE(&st->st_entry, se, se_list);
219 LIST_REMOVE(se, se_hash);
220 ieee80211_ies_cleanup(&se->base.se_ies);
221 IEEE80211_FREE(se, M_80211_SCAN);
223 memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi));
227 * Process a beacon or probe response frame; create an
228 * entry in the scan cache or update any previous entry.
231 sta_add(struct ieee80211_scan_state *ss,
232 struct ieee80211_channel *curchan,
233 const struct ieee80211_scanparams *sp,
234 const struct ieee80211_frame *wh,
235 int subtype, int rssi, int noise)
237 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
238 #define PICK1ST(_ss) \
239 ((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
240 IEEE80211_SCAN_PICK1ST)
241 struct sta_table *st = ss->ss_priv;
242 const uint8_t *macaddr = wh->i_addr2;
243 struct ieee80211vap *vap = ss->ss_vap;
244 struct ieee80211com *ic = vap->iv_ic;
245 struct ieee80211_channel *c;
246 struct sta_entry *se;
247 struct ieee80211_scan_entry *ise;
250 hash = STA_HASH(macaddr);
252 IEEE80211_SCAN_TABLE_LOCK(st);
253 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
254 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
256 se = (struct sta_entry *) IEEE80211_MALLOC(sizeof(struct sta_entry),
257 M_80211_SCAN, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
259 IEEE80211_SCAN_TABLE_UNLOCK(st);
262 se->se_scangen = st->st_scaniter-1;
263 se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
264 IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
265 TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
266 LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
269 /* XXX ap beaconing multiple ssid w/ same bssid */
270 if (sp->ssid[1] != 0 &&
271 (ISPROBE(subtype) || ise->se_ssid[1] == 0))
272 memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]);
273 KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE,
274 ("rate set too large: %u", sp->rates[1]));
275 memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]);
276 if (sp->xrates != NULL) {
277 /* XXX validate xrates[1] */
278 KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE,
279 ("xrate set too large: %u", sp->xrates[1]));
280 memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]);
282 ise->se_xrates[1] = 0;
283 IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
284 if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) {
286 * Record rssi data using extended precision LPF filter.
288 * NB: use only on-channel data to insure we get a good
289 * estimate of the signal we'll see when associated.
291 IEEE80211_RSSI_LPF(se->se_avgrssi, rssi);
292 ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi);
293 ise->se_noise = noise;
295 memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
296 ise->se_intval = sp->bintval;
297 ise->se_capinfo = sp->capinfo;
298 #ifdef IEEE80211_SUPPORT_MESH
299 if (sp->meshid != NULL && sp->meshid[1] != 0)
300 memcpy(ise->se_meshid, sp->meshid, 2+sp->meshid[1]);
303 * Beware of overriding se_chan for frames seen
304 * off-channel; this can cause us to attempt an
305 * association on the wrong channel.
307 if (sp->status & IEEE80211_BPARSE_OFFCHAN) {
309 * Off-channel, locate the home/bss channel for the sta
310 * using the value broadcast in the DSPARMS ie. We know
311 * sp->chan has this value because it's used to calculate
312 * IEEE80211_BPARSE_OFFCHAN.
314 c = ieee80211_find_channel_byieee(ic, sp->chan,
318 } else if (ise->se_chan == NULL) {
319 /* should not happen, pick something */
320 ise->se_chan = curchan;
323 ise->se_chan = curchan;
324 if (IEEE80211_IS_CHAN_HT(ise->se_chan) && sp->htcap == NULL) {
325 /* Demote legacy networks to a non-HT channel. */
326 c = ieee80211_find_channel(ic, ise->se_chan->ic_freq,
327 ise->se_chan->ic_flags & ~IEEE80211_CHAN_HT);
329 ("no legacy channel %u", ise->se_chan->ic_ieee));
332 ise->se_fhdwell = sp->fhdwell;
333 ise->se_fhindex = sp->fhindex;
334 ise->se_erp = sp->erp;
335 ise->se_timoff = sp->timoff;
336 if (sp->tim != NULL) {
337 const struct ieee80211_tim_ie *tim =
338 (const struct ieee80211_tim_ie *) sp->tim;
339 ise->se_dtimperiod = tim->tim_period;
341 if (sp->country != NULL) {
342 const struct ieee80211_country_ie *cie =
343 (const struct ieee80211_country_ie *) sp->country;
345 * If 11d is enabled and we're attempting to join a bss
346 * that advertises it's country code then compare our
347 * current settings to what we fetched from the country ie.
348 * If our country code is unspecified or different then
349 * dispatch an event to user space that identifies the
350 * country code so our regdomain config can be changed.
352 /* XXX only for STA mode? */
353 if ((IEEE80211_IS_CHAN_11D(ise->se_chan) ||
354 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
355 (ic->ic_regdomain.country == CTRY_DEFAULT ||
356 !isocmp(cie->cc, ic->ic_regdomain.isocc))) {
357 /* only issue one notify event per scan */
358 if (se->se_countrygen != st->st_scangen) {
359 ieee80211_notify_country(vap, ise->se_bssid,
361 se->se_countrygen = st->st_scangen;
364 ise->se_cc[0] = cie->cc[0];
365 ise->se_cc[1] = cie->cc[1];
367 /* NB: no need to setup ie ptrs; they are not (currently) used */
368 (void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len);
370 /* clear failure count after STA_FAIL_AGE passes */
371 if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) {
373 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
374 "%s: fails %u", __func__, se->se_fails);
377 se->se_lastupdate = ticks; /* update time */
381 KASSERT(sizeof(sp->bchan) == 1, ("bchan size"));
382 if (rssi > st->st_maxrssi[sp->bchan])
383 st->st_maxrssi[sp->bchan] = rssi;
385 IEEE80211_SCAN_TABLE_UNLOCK(st);
388 * If looking for a quick choice and nothing's
389 * been found check here.
391 if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0)
392 ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
400 * Check if a channel is excluded by user request.
403 isexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c)
405 return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) ||
406 (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
407 c->ic_freq != vap->iv_des_chan->ic_freq));
410 static struct ieee80211_channel *
411 find11gchannel(struct ieee80211com *ic, int i, int freq)
413 struct ieee80211_channel *c;
417 * The normal ordering in the channel list is b channel
418 * immediately followed by g so optimize the search for
419 * this. We'll still do a full search just in case.
421 for (j = i+1; j < ic->ic_nchans; j++) {
422 c = &ic->ic_channels[j];
423 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
426 for (j = 0; j < i; j++) {
427 c = &ic->ic_channels[j];
428 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
434 static const u_int chanflags[IEEE80211_MODE_MAX] = {
435 [IEEE80211_MODE_AUTO] = IEEE80211_CHAN_B,
436 [IEEE80211_MODE_11A] = IEEE80211_CHAN_A,
437 [IEEE80211_MODE_11B] = IEEE80211_CHAN_B,
438 [IEEE80211_MODE_11G] = IEEE80211_CHAN_G,
439 [IEEE80211_MODE_FH] = IEEE80211_CHAN_FHSS,
440 /* check base channel */
441 [IEEE80211_MODE_TURBO_A] = IEEE80211_CHAN_A,
442 [IEEE80211_MODE_TURBO_G] = IEEE80211_CHAN_G,
443 [IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_ST,
444 [IEEE80211_MODE_HALF] = IEEE80211_CHAN_HALF,
445 [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_QUARTER,
447 [IEEE80211_MODE_11NA] = IEEE80211_CHAN_A,
448 [IEEE80211_MODE_11NG] = IEEE80211_CHAN_G,
452 add_channels(struct ieee80211vap *vap,
453 struct ieee80211_scan_state *ss,
454 enum ieee80211_phymode mode, const uint16_t freq[], int nfreq)
456 struct ieee80211com *ic = vap->iv_ic;
457 struct ieee80211_channel *c, *cg;
461 KASSERT(mode < nitems(chanflags), ("Unexpected mode %u", mode));
462 modeflags = chanflags[mode];
463 for (i = 0; i < nfreq; i++) {
464 if (ss->ss_last >= IEEE80211_SCAN_MAX)
467 c = ieee80211_find_channel(ic, freq[i], modeflags);
468 if (c == NULL || isexcluded(vap, c))
470 if (mode == IEEE80211_MODE_AUTO) {
472 * XXX special-case 11b/g channels so we select
473 * the g channel if both are present.
475 if (IEEE80211_IS_CHAN_B(c) &&
476 (cg = find11gchannel(ic, i, c->ic_freq)) != NULL)
479 ss->ss_chans[ss->ss_last++] = c;
486 const uint16_t *list;
490 checktable(const struct scanlist *scan, const struct ieee80211_channel *c)
494 for (; scan->list != NULL; scan++) {
495 for (i = 0; i < scan->count; i++)
496 if (scan->list[i] == c->ic_freq)
503 onscanlist(const struct ieee80211_scan_state *ss,
504 const struct ieee80211_channel *c)
508 for (i = 0; i < ss->ss_last; i++)
509 if (ss->ss_chans[i] == c)
515 sweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
516 const struct scanlist table[])
518 struct ieee80211com *ic = vap->iv_ic;
519 struct ieee80211_channel *c;
522 for (i = 0; i < ic->ic_nchans; i++) {
523 if (ss->ss_last >= IEEE80211_SCAN_MAX)
526 c = &ic->ic_channels[i];
528 * Ignore dynamic turbo channels; we scan them
529 * in normal mode (i.e. not boosted). Likewise
530 * for HT channels, they get scanned using
533 if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c))
537 * If a desired mode was specified, scan only
538 * channels that satisfy that constraint.
540 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
541 vap->iv_des_mode != ieee80211_chan2mode(c))
545 * Skip channels excluded by user request.
547 if (isexcluded(vap, c))
551 * Add the channel unless it is listed in the
552 * fixed scan order tables. This insures we
553 * don't sweep back in channels we filtered out
556 if (checktable(table, c))
559 /* Add channel to scanning list. */
560 ss->ss_chans[ss->ss_last++] = c;
563 * Explicitly add any desired channel if:
564 * - not already on the scan list
565 * - allowed by any desired mode constraint
566 * - there is space in the scan list
567 * This allows the channel to be used when the filtering
568 * mechanisms would otherwise elide it (e.g HT, turbo).
570 c = vap->iv_des_chan;
571 if (c != IEEE80211_CHAN_ANYC &&
572 !onscanlist(ss, c) &&
573 (vap->iv_des_mode == IEEE80211_MODE_AUTO ||
574 vap->iv_des_mode == ieee80211_chan2mode(c)) &&
575 ss->ss_last < IEEE80211_SCAN_MAX)
576 ss->ss_chans[ss->ss_last++] = c;
580 makescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
581 const struct scanlist table[])
583 const struct scanlist *scan;
584 enum ieee80211_phymode mode;
588 * Use the table of ordered channels to construct the list
589 * of channels for scanning. Any channels in the ordered
590 * list not in the master list will be discarded.
592 for (scan = table; scan->list != NULL; scan++) {
594 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
596 * If a desired mode was specified, scan only
597 * channels that satisfy that constraint.
599 if (vap->iv_des_mode != mode) {
601 * The scan table marks 2.4Ghz channels as b
602 * so if the desired mode is 11g, then use
603 * the 11b channel list but upgrade the mode.
605 if (vap->iv_des_mode == IEEE80211_MODE_11G) {
606 if (mode == IEEE80211_MODE_11G) /* Skip the G check */
608 else if (mode == IEEE80211_MODE_11B)
609 mode = IEEE80211_MODE_11G; /* upgrade */
614 * This lets add_channels upgrade an 11b channel
615 * to 11g if available.
617 if (mode == IEEE80211_MODE_11B)
618 mode = IEEE80211_MODE_AUTO;
620 #ifdef IEEE80211_F_XR
621 /* XR does not operate on turbo channels */
622 if ((vap->iv_flags & IEEE80211_F_XR) &&
623 (mode == IEEE80211_MODE_TURBO_A ||
624 mode == IEEE80211_MODE_TURBO_G ||
625 mode == IEEE80211_MODE_STURBO_A))
629 * Add the list of the channels; any that are not
630 * in the master channel list will be discarded.
632 add_channels(vap, ss, mode, scan->list, scan->count);
636 * Add the channels from the ic that are not present
639 sweepchannels(ss, vap, table);
642 static const uint16_t rcl1[] = /* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
643 { 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
644 static const uint16_t rcl2[] = /* 4 MKK channels: 34, 38, 42, 46 */
645 { 5170, 5190, 5210, 5230 };
646 static const uint16_t rcl3[] = /* 2.4Ghz ch: 1,6,11,7,13 */
647 { 2412, 2437, 2462, 2442, 2472 };
648 static const uint16_t rcl4[] = /* 5 FCC channel: 149, 153, 161, 165 */
649 { 5745, 5765, 5785, 5805, 5825 };
650 static const uint16_t rcl7[] = /* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
651 { 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
652 static const uint16_t rcl8[] = /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
653 { 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
654 static const uint16_t rcl9[] = /* 2.4Ghz ch: 14 */
656 static const uint16_t rcl10[] = /* Added Korean channels 2312-2372 */
657 { 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
658 static const uint16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */
659 { 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
660 #ifdef ATH_TURBO_SCAN
661 static const uint16_t rcl5[] = /* 3 static turbo channels */
662 { 5210, 5250, 5290 };
663 static const uint16_t rcl6[] = /* 2 static turbo channels */
665 static const uint16_t rcl6x[] = /* 4 FCC3 turbo channels */
666 { 5540, 5580, 5620, 5660 };
667 static const uint16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */
669 static const uint16_t rcl13[] = /* dynamic Turbo channels */
670 { 5200, 5240, 5280, 5765, 5805 };
671 #endif /* ATH_TURBO_SCAN */
673 #define X(a) .count = sizeof(a)/sizeof(a[0]), .list = a
675 static const struct scanlist staScanTable[] = {
676 { IEEE80211_MODE_11B, X(rcl3) },
677 { IEEE80211_MODE_11A, X(rcl1) },
678 { IEEE80211_MODE_11A, X(rcl2) },
679 { IEEE80211_MODE_11B, X(rcl8) },
680 { IEEE80211_MODE_11B, X(rcl9) },
681 { IEEE80211_MODE_11A, X(rcl4) },
682 #ifdef ATH_TURBO_SCAN
683 { IEEE80211_MODE_STURBO_A, X(rcl5) },
684 { IEEE80211_MODE_STURBO_A, X(rcl6) },
685 { IEEE80211_MODE_TURBO_A, X(rcl6x) },
686 { IEEE80211_MODE_TURBO_A, X(rcl13) },
687 #endif /* ATH_TURBO_SCAN */
688 { IEEE80211_MODE_11A, X(rcl7) },
689 { IEEE80211_MODE_11B, X(rcl10) },
690 { IEEE80211_MODE_11A, X(rcl11) },
691 #ifdef ATH_TURBO_SCAN
692 { IEEE80211_MODE_TURBO_G, X(rcl12) },
693 #endif /* ATH_TURBO_SCAN */
698 * Start a station-mode scan by populating the channel list.
701 sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
703 struct sta_table *st = ss->ss_priv;
705 makescanlist(ss, vap, staScanTable);
707 if (ss->ss_mindwell == 0)
708 ss->ss_mindwell = msecs_to_ticks(20); /* 20ms */
709 if (ss->ss_maxdwell == 0)
710 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
719 * Restart a scan, typically a bg scan but can
720 * also be a fg scan that came up empty.
723 sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
725 struct sta_table *st = ss->ss_priv;
732 * Cancel an ongoing scan.
735 sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
741 * Demote any supplied 11g channel to 11b. There should
742 * always be an 11b channel but we check anyway...
744 static struct ieee80211_channel *
745 demote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan)
747 struct ieee80211_channel *c;
749 if (IEEE80211_IS_CHAN_ANYG(chan) &&
750 vap->iv_des_mode == IEEE80211_MODE_AUTO) {
751 c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq,
752 (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) |
761 maxrate(const struct ieee80211_scan_entry *se)
763 const struct ieee80211_ie_htcap *htcap =
764 (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie;
765 int rmax, r, i, txstream;
772 * HT station; inspect supported MCS and then adjust
773 * rate by channel width.
775 txparams = htcap->hc_mcsset[12];
776 if (txparams & 0x3) {
778 * TX MCS parameters defined and not equal to RX,
779 * extract the number of spartial streams and
780 * map it to the highest MCS rate.
782 txstream = ((txparams & 0xc) >> 2) + 1;
783 i = txstream * 8 - 1;
785 for (i = 31; i >= 0 && isclr(htcap->hc_mcsset, i); i--);
787 caps = LE_READ_2(&htcap->hc_cap);
788 if ((caps & IEEE80211_HTCAP_CHWIDTH40) &&
789 (caps & IEEE80211_HTCAP_SHORTGI40))
790 rmax = ieee80211_htrates[i].ht40_rate_400ns;
791 else if (caps & IEEE80211_HTCAP_CHWIDTH40)
792 rmax = ieee80211_htrates[i].ht40_rate_800ns;
793 else if (caps & IEEE80211_HTCAP_SHORTGI20)
794 rmax = ieee80211_htrates[i].ht20_rate_400ns;
796 rmax = ieee80211_htrates[i].ht20_rate_800ns;
799 for (i = 0; i < se->se_rates[1]; i++) {
800 r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
804 for (i = 0; i < se->se_xrates[1]; i++) {
805 r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
813 * Compare the capabilities of two entries and decide which is
814 * more desirable (return >0 if a is considered better). Note
815 * that we assume compatibility/usability has already been checked
816 * so we don't need to (e.g. validate whether privacy is supported).
817 * Used to select the best scan candidate for association in a BSS.
820 sta_compare(const struct sta_entry *a, const struct sta_entry *b)
822 #define PREFER(_a,_b,_what) do { \
823 if (((_a) ^ (_b)) & (_what)) \
824 return ((_a) & (_what)) ? 1 : -1; \
830 /* privacy support */
831 PREFER(a->base.se_capinfo, b->base.se_capinfo,
832 IEEE80211_CAPINFO_PRIVACY);
834 /* compare count of previous failures */
835 weight = b->se_fails - a->se_fails;
840 * Compare rssi. If the two are considered equivalent
841 * then fallback to other criteria. We threshold the
842 * comparisons to avoid selecting an ap purely by rssi
843 * when both values may be good but one ap is otherwise
844 * more desirable (e.g. an 11b-only ap with stronger
845 * signal than an 11g ap).
847 rssia = MIN(a->base.se_rssi, STA_RSSI_MAX);
848 rssib = MIN(b->base.se_rssi, STA_RSSI_MAX);
849 if (abs(rssib - rssia) < 5) {
850 /* best/max rate preferred if signal level close enough XXX */
851 maxa = maxrate(&a->base);
852 maxb = maxrate(&b->base);
855 /* XXX use freq for channel preference */
856 /* for now just prefer 5Ghz band to all other bands */
857 PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan),
858 IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1);
860 /* all things being equal, use signal level */
861 return a->base.se_rssi - b->base.se_rssi;
866 * Check rate set suitability and return the best supported rate.
867 * XXX inspect MCS for HT
870 check_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan,
871 const struct ieee80211_scan_entry *se)
873 const struct ieee80211_rateset *srs;
874 int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate;
877 okrate = badrate = 0;
879 srs = ieee80211_get_suprates(vap->iv_ic, chan);
880 nrs = se->se_rates[1];
883 ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate;
884 fixedrate = IEEE80211_FIXED_RATE_NONE;
886 for (i = 0; i < nrs; i++) {
887 r = IEEE80211_RV(rs[i]);
890 * Check any fixed rate is included.
895 * Check against our supported rates.
897 for (j = 0; j < srs->rs_nrates; j++)
898 if (r == IEEE80211_RV(srs->rs_rates[j])) {
899 if (r > okrate) /* NB: track max */
904 if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) {
906 * Don't try joining a BSS, if we don't support
907 * one of its basic rates.
913 if (rs == se->se_rates+2) {
914 /* scan xrates too; sort of an algol68-style for loop */
915 nrs = se->se_xrates[1];
916 rs = se->se_xrates+2;
921 if (okrate == 0 || ucastrate != fixedrate)
922 return badrate | IEEE80211_RATE_BASIC;
924 return IEEE80211_RV(okrate);
928 match_id(const uint8_t *ie, const uint8_t *val, int len)
930 return (ie[1] == len && memcmp(ie+2, val, len) == 0);
934 match_ssid(const uint8_t *ie,
935 int nssid, const struct ieee80211_scan_ssid ssids[])
939 for (i = 0; i < nssid; i++) {
940 if (match_id(ie, ssids[i].ssid, ssids[i].len))
946 #ifdef IEEE80211_SUPPORT_TDMA
948 tdma_isfull(const struct ieee80211_tdma_param *tdma)
952 slotcnt = tdma->tdma_slotcnt;
953 for (slot = slotcnt-1; slot >= 0; slot--)
954 if (isclr(tdma->tdma_inuse, slot))
958 #endif /* IEEE80211_SUPPORT_TDMA */
961 * Test a scan candidate for suitability/compatibility.
964 match_bss(struct ieee80211vap *vap,
965 const struct ieee80211_scan_state *ss, struct sta_entry *se0,
968 struct ieee80211com *ic = vap->iv_ic;
969 struct ieee80211_scan_entry *se = &se0->base;
974 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
975 fail |= MATCH_CHANNEL;
977 * NB: normally the desired mode is used to construct
978 * the channel list, but it's possible for the scan
979 * cache to include entries for stations outside this
980 * list so we check the desired mode here to weed them
983 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
984 (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
985 chanflags[vap->iv_des_mode])
986 fail |= MATCH_CHANNEL;
987 if (vap->iv_opmode == IEEE80211_M_IBSS) {
988 if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
989 fail |= MATCH_CAPINFO;
990 #ifdef IEEE80211_SUPPORT_TDMA
991 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
993 * Adhoc demo network setup shouldn't really be scanning
994 * but just in case skip stations operating in IBSS or
997 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
998 fail |= MATCH_CAPINFO;
1000 * TDMA operation cannot coexist with a normal 802.11 network;
1001 * skip if IBSS or ESS capabilities are marked and require
1002 * the beacon have a TDMA ie present.
1004 if (vap->iv_caps & IEEE80211_C_TDMA) {
1005 const struct ieee80211_tdma_param *tdma =
1006 (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie;
1007 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
1010 fail |= MATCH_TDMA_NOIE;
1011 else if (tdma->tdma_version != ts->tdma_version)
1012 fail |= MATCH_TDMA_VERSION;
1013 else if (tdma->tdma_slot != 0)
1014 fail |= MATCH_TDMA_NOTMASTER;
1015 else if (tdma_isfull(tdma))
1016 fail |= MATCH_TDMA_NOSLOT;
1018 else if (ieee80211_local_address(se->se_macaddr))
1019 fail |= MATCH_TDMA_LOCAL;
1022 #endif /* IEEE80211_SUPPORT_TDMA */
1023 #ifdef IEEE80211_SUPPORT_MESH
1024 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
1025 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
1027 * Mesh nodes have IBSS & ESS bits in capinfo turned off
1028 * and two special ie's that must be present.
1030 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
1031 fail |= MATCH_CAPINFO;
1032 else if (se->se_meshid[0] != IEEE80211_ELEMID_MESHID)
1033 fail |= MATCH_MESH_NOID;
1034 else if (ms->ms_idlen != 0 &&
1035 match_id(se->se_meshid, ms->ms_id, ms->ms_idlen))
1036 fail |= MATCH_MESHID;
1039 if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
1040 fail |= MATCH_CAPINFO;
1042 * If 11d is enabled and we're attempting to join a bss
1043 * that advertises it's country code then compare our
1044 * current settings to what we fetched from the country ie.
1045 * If our country code is unspecified or different then do
1046 * not attempt to join the bss. We should have already
1047 * dispatched an event to user space that identifies the
1048 * new country code so our regdomain config should match.
1050 if ((IEEE80211_IS_CHAN_11D(se->se_chan) ||
1051 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
1052 se->se_cc[0] != 0 &&
1053 (ic->ic_regdomain.country == CTRY_DEFAULT ||
1054 !isocmp(se->se_cc, ic->ic_regdomain.isocc)))
1057 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1058 if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
1059 fail |= MATCH_PRIVACY;
1061 /* XXX does this mean privacy is supported or required? */
1062 if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
1063 fail |= MATCH_PRIVACY;
1065 se0->se_flags &= ~STA_DEMOTE11B;
1066 rate = check_rate(vap, se->se_chan, se);
1067 if (rate & IEEE80211_RATE_BASIC) {
1070 * An 11b-only ap will give a rate mismatch if there is an
1071 * OFDM fixed tx rate for 11g. Try downgrading the channel
1072 * in the scan list to 11b and retry the rate check.
1074 if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) {
1075 rate = check_rate(vap, demote11b(vap, se->se_chan), se);
1076 if ((rate & IEEE80211_RATE_BASIC) == 0) {
1077 fail &= ~MATCH_RATE;
1078 se0->se_flags |= STA_DEMOTE11B;
1081 } else if (rate < 2*24) {
1083 * This is an 11b-only ap. Check the desired mode in
1084 * case that needs to be honored (mode 11g filters out
1085 * 11b-only ap's). Otherwise force any 11g channel used
1086 * in scanning to be demoted.
1088 * NB: we cheat a bit here by looking at the max rate;
1089 * we could/should check the rates.
1091 if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO ||
1092 vap->iv_des_mode == IEEE80211_MODE_11B))
1095 se0->se_flags |= STA_DEMOTE11B;
1097 if (ss->ss_nssid != 0 &&
1098 !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
1100 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
1101 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
1102 fail |= MATCH_BSSID;
1103 if (se0->se_fails >= STA_FAILS_MAX)
1104 fail |= MATCH_FAILS;
1105 if (se0->se_notseen >= STA_PURGE_SCANS)
1106 fail |= MATCH_NOTSEEN;
1107 if (se->se_rssi < STA_RSSI_MIN)
1109 #ifdef IEEE80211_DEBUG
1110 if (ieee80211_msg(vap, debug)) {
1112 fail & MATCH_FAILS ? '=' :
1113 fail & MATCH_NOTSEEN ? '^' :
1114 fail & MATCH_CC ? '$' :
1115 #ifdef IEEE80211_SUPPORT_TDMA
1116 fail & MATCH_TDMA_NOIE ? '&' :
1117 fail & MATCH_TDMA_VERSION ? 'v' :
1118 fail & MATCH_TDMA_NOTMASTER ? 's' :
1119 fail & MATCH_TDMA_NOSLOT ? 'f' :
1120 fail & MATCH_TDMA_LOCAL ? 'l' :
1122 fail & MATCH_MESH_NOID ? 'm' :
1123 fail ? '-' : '+', ether_sprintf(se->se_macaddr));
1124 printf(" %s%c", ether_sprintf(se->se_bssid),
1125 fail & MATCH_BSSID ? '!' : ' ');
1126 printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
1127 fail & MATCH_CHANNEL ? '!' : ' ');
1128 printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' ');
1129 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
1130 fail & MATCH_RATE ? '!' : ' ');
1132 (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
1133 (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : "",
1134 fail & MATCH_CAPINFO ? '!' : ' ');
1136 (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
1138 fail & MATCH_PRIVACY ? '!' : ' ');
1139 ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]);
1140 printf("%s\n", fail & (MATCH_SSID | MATCH_MESHID) ? "!" : "");
1147 sta_update_notseen(struct sta_table *st)
1149 struct sta_entry *se;
1151 IEEE80211_SCAN_TABLE_LOCK(st);
1152 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1154 * If seen the reset and don't bump the count;
1155 * otherwise bump the ``not seen'' count. Note
1156 * that this insures that stations for which we
1157 * see frames while not scanning but not during
1158 * this scan will not be penalized.
1165 IEEE80211_SCAN_TABLE_UNLOCK(st);
1169 sta_dec_fails(struct sta_table *st)
1171 struct sta_entry *se;
1173 IEEE80211_SCAN_TABLE_LOCK(st);
1174 TAILQ_FOREACH(se, &st->st_entry, se_list)
1177 IEEE80211_SCAN_TABLE_UNLOCK(st);
1180 static struct sta_entry *
1181 select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug)
1183 struct sta_table *st = ss->ss_priv;
1184 struct sta_entry *se, *selbs = NULL;
1186 IEEE80211_DPRINTF(vap, debug, " %s\n",
1187 "macaddr bssid chan rssi rate flag wep essid");
1188 IEEE80211_SCAN_TABLE_LOCK(st);
1189 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1190 ieee80211_ies_expand(&se->base.se_ies);
1191 if (match_bss(vap, ss, se, debug) == 0) {
1194 else if (sta_compare(se, selbs) > 0)
1198 IEEE80211_SCAN_TABLE_UNLOCK(st);
1204 * Pick an ap or ibss network to join or find a channel
1205 * to use to start an ibss network.
1208 sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1210 struct sta_table *st = ss->ss_priv;
1211 struct sta_entry *selbs;
1212 struct ieee80211_channel *chan;
1214 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1215 ("wrong mode %u", vap->iv_opmode));
1217 if (st->st_newscan) {
1218 sta_update_notseen(st);
1221 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1223 * Manual/background scan, don't select+join the
1224 * bss, just return. The scanning framework will
1225 * handle notification that this has completed.
1227 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1231 * Automatic sequencing; look for a candidate and
1232 * if found join the network.
1234 /* NB: unlocked read should be ok */
1235 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1237 "%s: no scan candidate\n", __func__);
1238 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1242 * If nothing suitable was found decrement
1243 * the failure counts so entries will be
1244 * reconsidered the next time around. We
1245 * really want to do this only for sta's
1246 * where we've previously had some success.
1250 return 0; /* restart scan */
1252 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1253 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1254 return (selbs != NULL);
1257 chan = selbs->base.se_chan;
1258 if (selbs->se_flags & STA_DEMOTE11B)
1259 chan = demote11b(vap, chan);
1260 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1262 return 1; /* terminate scan */
1266 * Lookup an entry in the scan cache. We assume we're
1267 * called from the bottom half or such that we don't need
1268 * to block the bottom half so that it's safe to return
1269 * a reference to an entry w/o holding the lock on the table.
1271 static struct sta_entry *
1272 sta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1274 struct sta_entry *se;
1275 int hash = STA_HASH(macaddr);
1277 IEEE80211_SCAN_TABLE_LOCK(st);
1278 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
1279 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
1281 IEEE80211_SCAN_TABLE_UNLOCK(st);
1283 return se; /* NB: unlocked */
1287 sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1289 struct ieee80211com *ic = vap->iv_ic;
1290 struct ieee80211_node *ni = vap->iv_bss;
1291 struct sta_table *st = ss->ss_priv;
1292 enum ieee80211_phymode mode;
1293 struct sta_entry *se, *selbs;
1294 uint8_t roamRate, curRate, ucastRate;
1295 int8_t roamRssi, curRssi;
1297 se = sta_lookup(st, ni->ni_macaddr);
1299 /* XXX something is wrong */
1303 mode = ieee80211_chan2mode(ic->ic_bsschan);
1304 roamRate = vap->iv_roamparms[mode].rate;
1305 roamRssi = vap->iv_roamparms[mode].rssi;
1306 ucastRate = vap->iv_txparms[mode].ucastrate;
1307 /* NB: the most up to date rssi is in the node, not the scan cache */
1308 curRssi = ic->ic_node_getrssi(ni);
1309 if (ucastRate == IEEE80211_FIXED_RATE_NONE) {
1310 curRate = ni->ni_txrate;
1311 roamRate &= IEEE80211_RATE_VAL;
1312 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1313 "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
1314 __func__, curRssi, curRate, roamRssi, roamRate);
1316 curRate = roamRate; /* NB: insure compare below fails */
1317 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1318 "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi);
1321 * Check if a new ap should be used and switch.
1322 * XXX deauth current ap
1324 if (curRate < roamRate || curRssi < roamRssi) {
1325 if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
1327 * Scan cache contents are too old; force a scan now
1328 * if possible so we have current state to make a
1329 * decision with. We don't kick off a bg scan if
1330 * we're using dynamic turbo and boosted or if the
1332 * XXX force immediate switch on scan complete
1334 if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1335 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle))
1336 ieee80211_bg_scan(vap, 0);
1339 se->base.se_rssi = curRssi;
1340 selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM);
1341 if (selbs != NULL && selbs != se) {
1342 struct ieee80211_channel *chan;
1344 IEEE80211_DPRINTF(vap,
1345 IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG,
1346 "%s: ROAM: curRate %u, roamRate %u, "
1347 "curRssi %d, roamRssi %d\n", __func__,
1348 curRate, roamRate, curRssi, roamRssi);
1350 chan = selbs->base.se_chan;
1351 if (selbs->se_flags & STA_DEMOTE11B)
1352 chan = demote11b(vap, chan);
1353 (void) ieee80211_sta_join(vap, chan, &selbs->base);
1359 * Age entries in the scan cache.
1360 * XXX also do roaming since it's convenient
1363 sta_age(struct ieee80211_scan_state *ss)
1365 struct ieee80211vap *vap = ss->ss_vap;
1369 * If rate control is enabled check periodically to see if
1370 * we should roam from our current connection to one that
1371 * might be better. This only applies when we're operating
1372 * in sta mode and automatic roaming is set.
1374 * XXX repeater station
1375 * XXX do when !bgscan?
1377 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1378 ("wrong mode %u", vap->iv_opmode));
1379 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO &&
1380 (vap->iv_flags & IEEE80211_F_BGSCAN) &&
1381 vap->iv_state >= IEEE80211_S_RUN)
1382 /* XXX vap is implicit */
1383 sta_roam_check(ss, vap);
1387 * Iterate over the entries in the scan cache, invoking
1388 * the callback function on each one.
1391 sta_iterate(struct ieee80211_scan_state *ss,
1392 ieee80211_scan_iter_func *f, void *arg)
1394 struct sta_table *st = ss->ss_priv;
1395 struct sta_entry *se;
1398 IEEE80211_SCAN_ITER_LOCK(st);
1399 gen = st->st_scaniter++;
1401 IEEE80211_SCAN_TABLE_LOCK(st);
1402 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1403 if (se->se_scangen != gen) {
1404 se->se_scangen = gen;
1405 /* update public state */
1406 se->base.se_age = ticks - se->se_lastupdate;
1407 IEEE80211_SCAN_TABLE_UNLOCK(st);
1408 (*f)(arg, &se->base);
1412 IEEE80211_SCAN_TABLE_UNLOCK(st);
1414 IEEE80211_SCAN_ITER_UNLOCK(st);
1418 sta_assoc_fail(struct ieee80211_scan_state *ss,
1419 const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason)
1421 struct sta_table *st = ss->ss_priv;
1422 struct sta_entry *se;
1424 se = sta_lookup(st, macaddr);
1427 se->se_lastfail = ticks;
1428 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1429 macaddr, "%s: reason %u fails %u",
1430 __func__, reason, se->se_fails);
1435 sta_assoc_success(struct ieee80211_scan_state *ss,
1436 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1438 struct sta_table *st = ss->ss_priv;
1439 struct sta_entry *se;
1441 se = sta_lookup(st, macaddr);
1445 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1446 macaddr, "%s: fails %u",
1447 __func__, se->se_fails);
1449 se->se_lastassoc = ticks;
1453 static const struct ieee80211_scanner sta_default = {
1454 .scan_name = "default",
1455 .scan_attach = sta_attach,
1456 .scan_detach = sta_detach,
1457 .scan_start = sta_start,
1458 .scan_restart = sta_restart,
1459 .scan_cancel = sta_cancel,
1460 .scan_end = sta_pick_bss,
1461 .scan_flush = sta_flush,
1462 .scan_add = sta_add,
1463 .scan_age = sta_age,
1464 .scan_iterate = sta_iterate,
1465 .scan_assoc_fail = sta_assoc_fail,
1466 .scan_assoc_success = sta_assoc_success,
1468 IEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default);
1471 * Adhoc mode-specific support.
1474 static const uint16_t adhocWorld[] = /* 36, 40, 44, 48 */
1475 { 5180, 5200, 5220, 5240 };
1476 static const uint16_t adhocFcc3[] = /* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */
1477 { 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 };
1478 static const uint16_t adhocMkk[] = /* 34, 38, 42, 46 */
1479 { 5170, 5190, 5210, 5230 };
1480 static const uint16_t adhoc11b[] = /* 10, 11 */
1483 static const struct scanlist adhocScanTable[] = {
1484 { IEEE80211_MODE_11B, X(adhoc11b) },
1485 { IEEE80211_MODE_11A, X(adhocWorld) },
1486 { IEEE80211_MODE_11A, X(adhocFcc3) },
1487 { IEEE80211_MODE_11B, X(adhocMkk) },
1493 * Start an adhoc-mode scan by populating the channel list.
1496 adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1498 struct sta_table *st = ss->ss_priv;
1500 makescanlist(ss, vap, adhocScanTable);
1502 if (ss->ss_mindwell == 0)
1503 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */
1504 if (ss->ss_maxdwell == 0)
1505 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
1514 * Select a channel to start an adhoc network on.
1515 * The channel list was populated with appropriate
1516 * channels so select one that looks least occupied.
1518 static struct ieee80211_channel *
1519 adhoc_pick_channel(struct ieee80211_scan_state *ss, int flags)
1521 struct sta_table *st = ss->ss_priv;
1522 struct sta_entry *se;
1523 struct ieee80211_channel *c, *bestchan;
1524 int i, bestrssi, maxrssi;
1529 IEEE80211_SCAN_TABLE_LOCK(st);
1530 for (i = 0; i < ss->ss_last; i++) {
1531 c = ss->ss_chans[i];
1532 /* never consider a channel with radar */
1533 if (IEEE80211_IS_CHAN_RADAR(c))
1535 /* skip channels disallowed by regulatory settings */
1536 if (IEEE80211_IS_CHAN_NOADHOC(c))
1538 /* check channel attributes for band compatibility */
1539 if (flags != 0 && (c->ic_flags & flags) != flags)
1542 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1543 if (se->base.se_chan != c)
1545 if (se->base.se_rssi > maxrssi)
1546 maxrssi = se->base.se_rssi;
1548 if (bestchan == NULL || maxrssi < bestrssi)
1551 IEEE80211_SCAN_TABLE_UNLOCK(st);
1557 * Pick an ibss network to join or find a channel
1558 * to use to start an ibss network.
1561 adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1563 struct sta_table *st = ss->ss_priv;
1564 struct sta_entry *selbs;
1565 struct ieee80211_channel *chan;
1566 struct ieee80211com *ic = vap->iv_ic;
1568 KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1569 vap->iv_opmode == IEEE80211_M_AHDEMO ||
1570 vap->iv_opmode == IEEE80211_M_MBSS,
1571 ("wrong opmode %u", vap->iv_opmode));
1573 if (st->st_newscan) {
1574 sta_update_notseen(st);
1577 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1579 * Manual/background scan, don't select+join the
1580 * bss, just return. The scanning framework will
1581 * handle notification that this has completed.
1583 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1587 * Automatic sequencing; look for a candidate and
1588 * if found join the network.
1590 /* NB: unlocked read should be ok */
1591 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1592 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1593 "%s: no scan candidate\n", __func__);
1594 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1597 /* NB: never auto-start a tdma network for slot !0 */
1598 #ifdef IEEE80211_SUPPORT_TDMA
1599 if (vap->iv_des_nssid &&
1600 ((vap->iv_caps & IEEE80211_C_TDMA) == 0 ||
1601 ieee80211_tdma_getslot(vap) == 0)) {
1603 if (vap->iv_des_nssid) {
1606 * No existing adhoc network to join and we have
1607 * an ssid; start one up. If no channel was
1608 * specified, try to select a channel.
1610 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1611 IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1612 chan = adhoc_pick_channel(ss, 0);
1614 chan = vap->iv_des_chan;
1616 struct ieee80211com *ic = vap->iv_ic;
1618 * Create a HT capable IBSS; the per-node
1619 * probe request/response will result in
1620 * "correct" rate control capabilities being
1623 chan = ieee80211_ht_adjust_channel(ic,
1624 chan, vap->iv_flags_ht);
1625 ieee80211_create_ibss(vap, chan);
1630 * If nothing suitable was found decrement
1631 * the failure counts so entries will be
1632 * reconsidered the next time around. We
1633 * really want to do this only for sta's
1634 * where we've previously had some success.
1638 return 0; /* restart scan */
1640 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1641 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1642 return (selbs != NULL);
1645 chan = selbs->base.se_chan;
1646 if (selbs->se_flags & STA_DEMOTE11B)
1647 chan = demote11b(vap, chan);
1649 * If HT is available, make it a possibility here.
1650 * The intent is to enable HT20/HT40 when joining a non-HT
1651 * IBSS node; we can then advertise HT IEs and speak HT
1652 * to any subsequent nodes that support it.
1654 chan = ieee80211_ht_adjust_channel(ic,
1655 chan, vap->iv_flags_ht);
1656 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1658 return 1; /* terminate scan */
1662 * Age entries in the scan cache.
1665 adhoc_age(struct ieee80211_scan_state *ss)
1667 struct sta_table *st = ss->ss_priv;
1668 struct sta_entry *se, *next;
1670 IEEE80211_SCAN_TABLE_LOCK(st);
1671 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
1672 if (se->se_notseen > STA_PURGE_SCANS) {
1673 TAILQ_REMOVE(&st->st_entry, se, se_list);
1674 LIST_REMOVE(se, se_hash);
1675 ieee80211_ies_cleanup(&se->base.se_ies);
1676 IEEE80211_FREE(se, M_80211_SCAN);
1679 IEEE80211_SCAN_TABLE_UNLOCK(st);
1682 static const struct ieee80211_scanner adhoc_default = {
1683 .scan_name = "default",
1684 .scan_attach = sta_attach,
1685 .scan_detach = sta_detach,
1686 .scan_start = adhoc_start,
1687 .scan_restart = sta_restart,
1688 .scan_cancel = sta_cancel,
1689 .scan_end = adhoc_pick_bss,
1690 .scan_flush = sta_flush,
1691 .scan_pickchan = adhoc_pick_channel,
1692 .scan_add = sta_add,
1693 .scan_age = adhoc_age,
1694 .scan_iterate = sta_iterate,
1695 .scan_assoc_fail = sta_assoc_fail,
1696 .scan_assoc_success = sta_assoc_success,
1698 IEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default);
1699 IEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default);
1702 ap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1704 struct sta_table *st = ss->ss_priv;
1706 makescanlist(ss, vap, staScanTable);
1708 if (ss->ss_mindwell == 0)
1709 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */
1710 if (ss->ss_maxdwell == 0)
1711 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
1716 ieee80211_promisc(vap, true);
1721 * Cancel an ongoing scan.
1724 ap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1726 ieee80211_promisc(vap, false);
1731 * Pick a quiet channel to use for ap operation.
1733 static struct ieee80211_channel *
1734 ap_pick_channel(struct ieee80211_scan_state *ss, int flags)
1736 struct sta_table *st = ss->ss_priv;
1737 struct ieee80211_channel *bestchan = NULL;
1740 /* XXX select channel more intelligently, e.g. channel spread, power */
1741 /* NB: use scan list order to preserve channel preference */
1742 for (i = 0; i < ss->ss_last; i++) {
1743 struct ieee80211_channel *chan = ss->ss_chans[i];
1745 * If the channel is unoccupied the max rssi
1746 * should be zero; just take it. Otherwise
1747 * track the channel with the lowest rssi and
1748 * use that when all channels appear occupied.
1750 if (IEEE80211_IS_CHAN_RADAR(chan))
1752 if (IEEE80211_IS_CHAN_NOHOSTAP(chan))
1754 /* check channel attributes for band compatibility */
1755 if (flags != 0 && (chan->ic_flags & flags) != flags)
1757 KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size"));
1758 /* XXX channel have interference */
1759 if (st->st_maxrssi[chan->ic_ieee] == 0) {
1760 /* XXX use other considerations */
1763 if (bestchan == NULL ||
1764 st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee])
1771 * Pick a quiet channel to use for ap operation.
1774 ap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1776 struct ieee80211com *ic = vap->iv_ic;
1777 struct ieee80211_channel *bestchan;
1779 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
1780 ("wrong opmode %u", vap->iv_opmode));
1781 bestchan = ap_pick_channel(ss, 0);
1782 if (bestchan == NULL) {
1783 /* no suitable channel, should not happen */
1784 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1785 "%s: no suitable channel! (should not happen)\n", __func__);
1786 /* XXX print something? */
1787 return 0; /* restart scan */
1790 * If this is a dynamic turbo channel, start with the unboosted one.
1792 if (IEEE80211_IS_CHAN_TURBO(bestchan)) {
1793 bestchan = ieee80211_find_channel(ic, bestchan->ic_freq,
1794 bestchan->ic_flags & ~IEEE80211_CHAN_TURBO);
1795 if (bestchan == NULL) {
1796 /* should never happen ?? */
1800 ieee80211_promisc(vap, false);
1801 if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) {
1803 * Manual/background scan, don't select+join the
1804 * bss, just return. The scanning framework will
1805 * handle notification that this has completed.
1807 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1810 ieee80211_create_ibss(vap,
1811 ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ht));
1815 static const struct ieee80211_scanner ap_default = {
1816 .scan_name = "default",
1817 .scan_attach = sta_attach,
1818 .scan_detach = sta_detach,
1819 .scan_start = ap_start,
1820 .scan_restart = sta_restart,
1821 .scan_cancel = ap_cancel,
1823 .scan_flush = sta_flush,
1824 .scan_pickchan = ap_pick_channel,
1825 .scan_add = sta_add,
1826 .scan_age = adhoc_age,
1827 .scan_iterate = sta_iterate,
1828 .scan_assoc_success = sta_assoc_success,
1829 .scan_assoc_fail = sta_assoc_fail,
1831 IEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
1833 #ifdef IEEE80211_SUPPORT_MESH
1835 * Pick an mbss network to join or find a channel
1836 * to use to start an mbss network.
1839 mesh_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1841 struct sta_table *st = ss->ss_priv;
1842 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1843 struct sta_entry *selbs;
1844 struct ieee80211_channel *chan;
1846 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
1847 ("wrong opmode %u", vap->iv_opmode));
1849 if (st->st_newscan) {
1850 sta_update_notseen(st);
1853 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1855 * Manual/background scan, don't select+join the
1856 * bss, just return. The scanning framework will
1857 * handle notification that this has completed.
1859 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1863 * Automatic sequencing; look for a candidate and
1864 * if found join the network.
1866 /* NB: unlocked read should be ok */
1867 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1868 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1869 "%s: no scan candidate\n", __func__);
1870 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1873 if (ms->ms_idlen != 0) {
1875 * No existing mbss network to join and we have
1876 * a meshid; start one up. If no channel was
1877 * specified, try to select a channel.
1879 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1880 IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1881 struct ieee80211com *ic = vap->iv_ic;
1883 chan = adhoc_pick_channel(ss, 0);
1885 chan = ieee80211_ht_adjust_channel(ic,
1886 chan, vap->iv_flags_ht);
1888 chan = vap->iv_des_chan;
1890 ieee80211_create_ibss(vap, chan);
1895 * If nothing suitable was found decrement
1896 * the failure counts so entries will be
1897 * reconsidered the next time around. We
1898 * really want to do this only for sta's
1899 * where we've previously had some success.
1903 return 0; /* restart scan */
1905 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1906 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1907 return (selbs != NULL);
1910 chan = selbs->base.se_chan;
1911 if (selbs->se_flags & STA_DEMOTE11B)
1912 chan = demote11b(vap, chan);
1913 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1915 return 1; /* terminate scan */
1918 static const struct ieee80211_scanner mesh_default = {
1919 .scan_name = "default",
1920 .scan_attach = sta_attach,
1921 .scan_detach = sta_detach,
1922 .scan_start = adhoc_start,
1923 .scan_restart = sta_restart,
1924 .scan_cancel = sta_cancel,
1925 .scan_end = mesh_pick_bss,
1926 .scan_flush = sta_flush,
1927 .scan_pickchan = adhoc_pick_channel,
1928 .scan_add = sta_add,
1929 .scan_age = adhoc_age,
1930 .scan_iterate = sta_iterate,
1931 .scan_assoc_fail = sta_assoc_fail,
1932 .scan_assoc_success = sta_assoc_success,
1934 IEEE80211_SCANNER_ALG(mesh, IEEE80211_M_MBSS, mesh_default);
1935 #endif /* IEEE80211_SUPPORT_MESH */