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_media.h>
43 #include <net/ethernet.h>
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_input.h>
47 #include <net80211/ieee80211_regdomain.h>
48 #ifdef IEEE80211_SUPPORT_TDMA
49 #include <net80211/ieee80211_tdma.h>
51 #ifdef IEEE80211_SUPPORT_MESH
52 #include <net80211/ieee80211_mesh.h>
54 #include <net80211/ieee80211_ratectl.h>
59 * Parameters for managing cache entries:
61 * o a station with STA_FAILS_MAX failures is not considered
62 * when picking a candidate
63 * o a station that hasn't had an update in STA_PURGE_SCANS
64 * (background) scans is discarded
65 * o after STA_FAILS_AGE seconds we clear the failure count
67 #define STA_FAILS_MAX 2 /* assoc failures before ignored */
68 #define STA_FAILS_AGE (2*60) /* time before clearing fails (secs) */
69 #define STA_PURGE_SCANS 2 /* age for purging entries (scans) */
72 #define STA_RSSI_MIN 8 /* min acceptable rssi */
73 #define STA_RSSI_MAX 40 /* max rssi for comparison */
76 struct ieee80211_scan_entry base;
77 TAILQ_ENTRY(sta_entry) se_list;
78 LIST_ENTRY(sta_entry) se_hash;
79 uint8_t se_fails; /* failure to associate count */
80 uint8_t se_seen; /* seen during current scan */
81 uint8_t se_notseen; /* not seen in previous scans */
83 #define STA_DEMOTE11B 0x01 /* match w/ demoted 11b chan */
84 uint32_t se_avgrssi; /* LPF rssi state */
85 unsigned long se_lastupdate; /* time of last update */
86 unsigned long se_lastfail; /* time of last failure */
87 unsigned long se_lastassoc; /* time of last association */
88 u_int se_scangen; /* iterator scan gen# */
89 u_int se_countrygen; /* gen# of last cc notify */
92 #define STA_HASHSIZE 32
93 /* simple hash is enough for variation of macaddr */
94 #define STA_HASH(addr) \
95 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE)
97 #define MAX_IEEE_CHAN 256 /* max acceptable IEEE chan # */
98 CTASSERT(MAX_IEEE_CHAN >= 256);
101 ieee80211_scan_table_lock_t st_lock; /* on scan table */
102 TAILQ_HEAD(, sta_entry) st_entry; /* all entries */
103 LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE];
104 struct mtx st_scanlock; /* on st_scaniter */
105 u_int st_scaniter; /* gen# for iterator */
106 u_int st_scangen; /* scan generation # */
108 /* ap-related state */
109 int st_maxrssi[MAX_IEEE_CHAN];
112 static void sta_flush_table(struct sta_table *);
114 * match_bss returns a bitmask describing if an entry is suitable
115 * for use. If non-zero the entry was deemed not suitable and it's
116 * contents explains why. The following flags are or'd to to this
117 * mask and can be used to figure out why the entry was rejected.
119 #define MATCH_CHANNEL 0x00001 /* channel mismatch */
120 #define MATCH_CAPINFO 0x00002 /* capabilities mismatch, e.g. no ess */
121 #define MATCH_PRIVACY 0x00004 /* privacy mismatch */
122 #define MATCH_RATE 0x00008 /* rate set mismatch */
123 #define MATCH_SSID 0x00010 /* ssid mismatch */
124 #define MATCH_BSSID 0x00020 /* bssid mismatch */
125 #define MATCH_FAILS 0x00040 /* too many failed auth attempts */
126 #define MATCH_NOTSEEN 0x00080 /* not seen in recent scans */
127 #define MATCH_RSSI 0x00100 /* rssi deemed too low to use */
128 #define MATCH_CC 0x00200 /* country code mismatch */
129 #define MATCH_TDMA_NOIE 0x00400 /* no TDMA ie */
130 #define MATCH_TDMA_NOTMASTER 0x00800 /* not TDMA master */
131 #define MATCH_TDMA_NOSLOT 0x01000 /* all TDMA slots occupied */
132 #define MATCH_TDMA_LOCAL 0x02000 /* local address */
133 #define MATCH_TDMA_VERSION 0x04000 /* protocol version mismatch */
134 #define MATCH_MESH_NOID 0x10000 /* no MESHID ie */
135 #define MATCH_MESHID 0x20000 /* meshid mismatch */
136 static int match_bss(struct ieee80211vap *,
137 const struct ieee80211_scan_state *, struct sta_entry *, int);
138 static void adhoc_age(struct ieee80211_scan_state *);
141 isocmp(const uint8_t cc1[], const uint8_t cc2[])
143 return (cc1[0] == cc2[0] && cc1[1] == cc2[1]);
146 /* number of references from net80211 layer */
147 static int nrefs = 0;
151 IEEE80211_SCANNER_MODULE(sta, 1);
154 * Attach prior to any scanning work.
157 sta_attach(struct ieee80211_scan_state *ss)
159 struct sta_table *st;
161 st = (struct sta_table *) malloc(sizeof(struct sta_table),
162 M_80211_SCAN, M_NOWAIT | M_ZERO);
165 IEEE80211_SCAN_TABLE_LOCK_INIT(st, "scantable");
166 mtx_init(&st->st_scanlock, "scangen", "802.11 scangen", MTX_DEF);
167 TAILQ_INIT(&st->st_entry);
169 nrefs++; /* NB: we assume caller locking */
174 * Cleanup any private state.
177 sta_detach(struct ieee80211_scan_state *ss)
179 struct sta_table *st = ss->ss_priv;
183 IEEE80211_SCAN_TABLE_LOCK_DESTROY(st);
184 mtx_destroy(&st->st_scanlock);
185 free(st, M_80211_SCAN);
186 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
187 nrefs--; /* NB: we assume caller locking */
193 * Flush all per-scan state.
196 sta_flush(struct ieee80211_scan_state *ss)
198 struct sta_table *st = ss->ss_priv;
200 IEEE80211_SCAN_TABLE_LOCK(st);
202 IEEE80211_SCAN_TABLE_UNLOCK(st);
208 * Flush all entries in the scan cache.
211 sta_flush_table(struct sta_table *st)
213 struct sta_entry *se, *next;
215 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
216 TAILQ_REMOVE(&st->st_entry, se, se_list);
217 LIST_REMOVE(se, se_hash);
218 ieee80211_ies_cleanup(&se->base.se_ies);
219 free(se, M_80211_SCAN);
221 memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi));
225 * Process a beacon or probe response frame; create an
226 * entry in the scan cache or update any previous entry.
229 sta_add(struct ieee80211_scan_state *ss,
230 const struct ieee80211_scanparams *sp,
231 const struct ieee80211_frame *wh,
232 int subtype, int rssi, int noise)
234 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
235 #define PICK1ST(_ss) \
236 ((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
237 IEEE80211_SCAN_PICK1ST)
238 struct sta_table *st = ss->ss_priv;
239 const uint8_t *macaddr = wh->i_addr2;
240 struct ieee80211vap *vap = ss->ss_vap;
241 struct ieee80211com *ic = vap->iv_ic;
242 struct ieee80211_channel *c;
243 struct sta_entry *se;
244 struct ieee80211_scan_entry *ise;
247 hash = STA_HASH(macaddr);
249 IEEE80211_SCAN_TABLE_LOCK(st);
250 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
251 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
253 se = (struct sta_entry *) malloc(sizeof(struct sta_entry),
254 M_80211_SCAN, M_NOWAIT | M_ZERO);
256 IEEE80211_SCAN_TABLE_UNLOCK(st);
259 se->se_scangen = st->st_scaniter-1;
260 se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
261 IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
262 TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
263 LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
266 /* XXX ap beaconing multiple ssid w/ same bssid */
267 if (sp->ssid[1] != 0 &&
268 (ISPROBE(subtype) || ise->se_ssid[1] == 0))
269 memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]);
270 KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE,
271 ("rate set too large: %u", sp->rates[1]));
272 memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]);
273 if (sp->xrates != NULL) {
274 /* XXX validate xrates[1] */
275 KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE,
276 ("xrate set too large: %u", sp->xrates[1]));
277 memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]);
279 ise->se_xrates[1] = 0;
280 IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
281 if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) {
283 * Record rssi data using extended precision LPF filter.
285 * NB: use only on-channel data to insure we get a good
286 * estimate of the signal we'll see when associated.
288 IEEE80211_RSSI_LPF(se->se_avgrssi, rssi);
289 ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi);
290 ise->se_noise = noise;
292 memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
293 ise->se_intval = sp->bintval;
294 ise->se_capinfo = sp->capinfo;
295 #ifdef IEEE80211_SUPPORT_MESH
296 if (sp->meshid != NULL && sp->meshid[1] != 0)
297 memcpy(ise->se_meshid, sp->meshid, 2+sp->meshid[1]);
300 * Beware of overriding se_chan for frames seen
301 * off-channel; this can cause us to attempt an
302 * association on the wrong channel.
304 if (sp->status & IEEE80211_BPARSE_OFFCHAN) {
306 * Off-channel, locate the home/bss channel for the sta
307 * using the value broadcast in the DSPARMS ie. We know
308 * sp->chan has this value because it's used to calculate
309 * IEEE80211_BPARSE_OFFCHAN.
311 c = ieee80211_find_channel_byieee(ic, sp->chan,
312 ic->ic_curchan->ic_flags);
315 } else if (ise->se_chan == NULL) {
316 /* should not happen, pick something */
317 ise->se_chan = ic->ic_curchan;
320 ise->se_chan = ic->ic_curchan;
321 if (IEEE80211_IS_CHAN_HT(ise->se_chan) && sp->htcap == NULL) {
322 /* Demote legacy networks to a non-HT channel. */
323 c = ieee80211_find_channel(ic, ise->se_chan->ic_freq,
324 ise->se_chan->ic_flags & ~IEEE80211_CHAN_HT);
326 ("no legacy channel %u", ise->se_chan->ic_ieee));
329 ise->se_fhdwell = sp->fhdwell;
330 ise->se_fhindex = sp->fhindex;
331 ise->se_erp = sp->erp;
332 ise->se_timoff = sp->timoff;
333 if (sp->tim != NULL) {
334 const struct ieee80211_tim_ie *tim =
335 (const struct ieee80211_tim_ie *) sp->tim;
336 ise->se_dtimperiod = tim->tim_period;
338 if (sp->country != NULL) {
339 const struct ieee80211_country_ie *cie =
340 (const struct ieee80211_country_ie *) sp->country;
342 * If 11d is enabled and we're attempting to join a bss
343 * that advertises it's country code then compare our
344 * current settings to what we fetched from the country ie.
345 * If our country code is unspecified or different then
346 * dispatch an event to user space that identifies the
347 * country code so our regdomain config can be changed.
349 /* XXX only for STA mode? */
350 if ((IEEE80211_IS_CHAN_11D(ise->se_chan) ||
351 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
352 (ic->ic_regdomain.country == CTRY_DEFAULT ||
353 !isocmp(cie->cc, ic->ic_regdomain.isocc))) {
354 /* only issue one notify event per scan */
355 if (se->se_countrygen != st->st_scangen) {
356 ieee80211_notify_country(vap, ise->se_bssid,
358 se->se_countrygen = st->st_scangen;
361 ise->se_cc[0] = cie->cc[0];
362 ise->se_cc[1] = cie->cc[1];
364 /* NB: no need to setup ie ptrs; they are not (currently) used */
365 (void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len);
367 /* clear failure count after STA_FAIL_AGE passes */
368 if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) {
370 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
371 "%s: fails %u", __func__, se->se_fails);
374 se->se_lastupdate = ticks; /* update time */
378 KASSERT(sizeof(sp->bchan) == 1, ("bchan size"));
379 if (rssi > st->st_maxrssi[sp->bchan])
380 st->st_maxrssi[sp->bchan] = rssi;
382 IEEE80211_SCAN_TABLE_UNLOCK(st);
385 * If looking for a quick choice and nothing's
386 * been found check here.
388 if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0)
389 ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
397 * Check if a channel is excluded by user request.
400 isexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c)
402 return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) ||
403 (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
404 c->ic_freq != vap->iv_des_chan->ic_freq));
407 static struct ieee80211_channel *
408 find11gchannel(struct ieee80211com *ic, int i, int freq)
410 struct ieee80211_channel *c;
414 * The normal ordering in the channel list is b channel
415 * immediately followed by g so optimize the search for
416 * this. We'll still do a full search just in case.
418 for (j = i+1; j < ic->ic_nchans; j++) {
419 c = &ic->ic_channels[j];
420 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
423 for (j = 0; j < i; j++) {
424 c = &ic->ic_channels[j];
425 if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
431 static const u_int chanflags[IEEE80211_MODE_MAX] = {
432 [IEEE80211_MODE_AUTO] = IEEE80211_CHAN_B,
433 [IEEE80211_MODE_11A] = IEEE80211_CHAN_A,
434 [IEEE80211_MODE_11B] = IEEE80211_CHAN_B,
435 [IEEE80211_MODE_11G] = IEEE80211_CHAN_G,
436 [IEEE80211_MODE_FH] = IEEE80211_CHAN_FHSS,
437 /* check base channel */
438 [IEEE80211_MODE_TURBO_A] = IEEE80211_CHAN_A,
439 [IEEE80211_MODE_TURBO_G] = IEEE80211_CHAN_G,
440 [IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_ST,
441 [IEEE80211_MODE_HALF] = IEEE80211_CHAN_HALF,
442 [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_QUARTER,
444 [IEEE80211_MODE_11NA] = IEEE80211_CHAN_A,
445 [IEEE80211_MODE_11NG] = IEEE80211_CHAN_G,
449 add_channels(struct ieee80211vap *vap,
450 struct ieee80211_scan_state *ss,
451 enum ieee80211_phymode mode, const uint16_t freq[], int nfreq)
453 #define N(a) (sizeof(a) / sizeof(a[0]))
454 struct ieee80211com *ic = vap->iv_ic;
455 struct ieee80211_channel *c, *cg;
459 KASSERT(mode < N(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;
485 const uint16_t *list;
489 checktable(const struct scanlist *scan, const struct ieee80211_channel *c)
493 for (; scan->list != NULL; scan++) {
494 for (i = 0; i < scan->count; i++)
495 if (scan->list[i] == c->ic_freq)
502 onscanlist(const struct ieee80211_scan_state *ss,
503 const struct ieee80211_channel *c)
507 for (i = 0; i < ss->ss_last; i++)
508 if (ss->ss_chans[i] == c)
514 sweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
515 const struct scanlist table[])
517 struct ieee80211com *ic = vap->iv_ic;
518 struct ieee80211_channel *c;
521 for (i = 0; i < ic->ic_nchans; i++) {
522 if (ss->ss_last >= IEEE80211_SCAN_MAX)
525 c = &ic->ic_channels[i];
527 * Ignore dynamic turbo channels; we scan them
528 * in normal mode (i.e. not boosted). Likewise
529 * for HT channels, they get scanned using
532 if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c))
536 * If a desired mode was specified, scan only
537 * channels that satisfy that constraint.
539 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
540 vap->iv_des_mode != ieee80211_chan2mode(c))
544 * Skip channels excluded by user request.
546 if (isexcluded(vap, c))
550 * Add the channel unless it is listed in the
551 * fixed scan order tables. This insures we
552 * don't sweep back in channels we filtered out
555 if (checktable(table, c))
558 /* Add channel to scanning list. */
559 ss->ss_chans[ss->ss_last++] = c;
562 * Explicitly add any desired channel if:
563 * - not already on the scan list
564 * - allowed by any desired mode constraint
565 * - there is space in the scan list
566 * This allows the channel to be used when the filtering
567 * mechanisms would otherwise elide it (e.g HT, turbo).
569 c = vap->iv_des_chan;
570 if (c != IEEE80211_CHAN_ANYC &&
571 !onscanlist(ss, c) &&
572 (vap->iv_des_mode == IEEE80211_MODE_AUTO ||
573 vap->iv_des_mode == ieee80211_chan2mode(c)) &&
574 ss->ss_last < IEEE80211_SCAN_MAX)
575 ss->ss_chans[ss->ss_last++] = c;
579 makescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
580 const struct scanlist table[])
582 const struct scanlist *scan;
583 enum ieee80211_phymode mode;
587 * Use the table of ordered channels to construct the list
588 * of channels for scanning. Any channels in the ordered
589 * list not in the master list will be discarded.
591 for (scan = table; scan->list != NULL; scan++) {
593 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
595 * If a desired mode was specified, scan only
596 * channels that satisfy that constraint.
598 if (vap->iv_des_mode != mode) {
600 * The scan table marks 2.4Ghz channels as b
601 * so if the desired mode is 11g, then use
602 * the 11b channel list but upgrade the mode.
604 if (vap->iv_des_mode != IEEE80211_MODE_11G ||
605 mode != IEEE80211_MODE_11B)
607 mode = IEEE80211_MODE_11G; /* upgrade */
611 * This lets add_channels upgrade an 11b channel
612 * to 11g if available.
614 if (mode == IEEE80211_MODE_11B)
615 mode = IEEE80211_MODE_AUTO;
617 #ifdef IEEE80211_F_XR
618 /* XR does not operate on turbo channels */
619 if ((vap->iv_flags & IEEE80211_F_XR) &&
620 (mode == IEEE80211_MODE_TURBO_A ||
621 mode == IEEE80211_MODE_TURBO_G ||
622 mode == IEEE80211_MODE_STURBO_A))
626 * Add the list of the channels; any that are not
627 * in the master channel list will be discarded.
629 add_channels(vap, ss, mode, scan->list, scan->count);
633 * Add the channels from the ic that are not present
636 sweepchannels(ss, vap, table);
639 static const uint16_t rcl1[] = /* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
640 { 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
641 static const uint16_t rcl2[] = /* 4 MKK channels: 34, 38, 42, 46 */
642 { 5170, 5190, 5210, 5230 };
643 static const uint16_t rcl3[] = /* 2.4Ghz ch: 1,6,11,7,13 */
644 { 2412, 2437, 2462, 2442, 2472 };
645 static const uint16_t rcl4[] = /* 5 FCC channel: 149, 153, 161, 165 */
646 { 5745, 5765, 5785, 5805, 5825 };
647 static const uint16_t rcl7[] = /* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
648 { 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
649 static const uint16_t rcl8[] = /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
650 { 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
651 static const uint16_t rcl9[] = /* 2.4Ghz ch: 14 */
653 static const uint16_t rcl10[] = /* Added Korean channels 2312-2372 */
654 { 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
655 static const uint16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */
656 { 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
657 #ifdef ATH_TURBO_SCAN
658 static const uint16_t rcl5[] = /* 3 static turbo channels */
659 { 5210, 5250, 5290 };
660 static const uint16_t rcl6[] = /* 2 static turbo channels */
662 static const uint16_t rcl6x[] = /* 4 FCC3 turbo channels */
663 { 5540, 5580, 5620, 5660 };
664 static const uint16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */
666 static const uint16_t rcl13[] = /* dynamic Turbo channels */
667 { 5200, 5240, 5280, 5765, 5805 };
668 #endif /* ATH_TURBO_SCAN */
670 #define X(a) .count = sizeof(a)/sizeof(a[0]), .list = a
672 static const struct scanlist staScanTable[] = {
673 { IEEE80211_MODE_11B, X(rcl3) },
674 { IEEE80211_MODE_11A, X(rcl1) },
675 { IEEE80211_MODE_11A, X(rcl2) },
676 { IEEE80211_MODE_11B, X(rcl8) },
677 { IEEE80211_MODE_11B, X(rcl9) },
678 { IEEE80211_MODE_11A, X(rcl4) },
679 #ifdef ATH_TURBO_SCAN
680 { IEEE80211_MODE_STURBO_A, X(rcl5) },
681 { IEEE80211_MODE_STURBO_A, X(rcl6) },
682 { IEEE80211_MODE_TURBO_A, X(rcl6x) },
683 { IEEE80211_MODE_TURBO_A, X(rcl13) },
684 #endif /* ATH_TURBO_SCAN */
685 { IEEE80211_MODE_11A, X(rcl7) },
686 { IEEE80211_MODE_11B, X(rcl10) },
687 { IEEE80211_MODE_11A, X(rcl11) },
688 #ifdef ATH_TURBO_SCAN
689 { IEEE80211_MODE_TURBO_G, X(rcl12) },
690 #endif /* ATH_TURBO_SCAN */
695 * Start a station-mode scan by populating the channel list.
698 sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
700 struct sta_table *st = ss->ss_priv;
702 makescanlist(ss, vap, staScanTable);
704 if (ss->ss_mindwell == 0)
705 ss->ss_mindwell = msecs_to_ticks(20); /* 20ms */
706 if (ss->ss_maxdwell == 0)
707 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
716 * Restart a scan, typically a bg scan but can
717 * also be a fg scan that came up empty.
720 sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
722 struct sta_table *st = ss->ss_priv;
729 * Cancel an ongoing scan.
732 sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
737 /* unalligned little endian access */
738 #define LE_READ_2(p) \
740 ((((const uint8_t *)(p))[0] ) | \
741 (((const uint8_t *)(p))[1] << 8)))
744 * Demote any supplied 11g channel to 11b. There should
745 * always be an 11b channel but we check anyway...
747 static struct ieee80211_channel *
748 demote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan)
750 struct ieee80211_channel *c;
752 if (IEEE80211_IS_CHAN_ANYG(chan) &&
753 vap->iv_des_mode == IEEE80211_MODE_AUTO) {
754 c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq,
755 (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) |
764 maxrate(const struct ieee80211_scan_entry *se)
766 const struct ieee80211_ie_htcap *htcap =
767 (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie;
768 int rmax, r, i, txstream;
775 * HT station; inspect supported MCS and then adjust
776 * rate by channel width.
778 txparams = htcap->hc_mcsset[12];
779 if (txparams & 0x3) {
781 * TX MCS parameters defined and not equal to RX,
782 * extract the number of spartial streams and
783 * map it to the highest MCS rate.
785 txstream = ((txparams & 0xc) >> 2) + 1;
786 i = txstream * 8 - 1;
788 for (i = 31; i >= 0 && isclr(htcap->hc_mcsset, i); i--);
790 caps = LE_READ_2(&htcap->hc_cap);
791 if ((caps & IEEE80211_HTCAP_CHWIDTH40) &&
792 (caps & IEEE80211_HTCAP_SHORTGI40))
793 rmax = ieee80211_htrates[i].ht40_rate_400ns;
794 else if (caps & IEEE80211_HTCAP_CHWIDTH40)
795 rmax = ieee80211_htrates[i].ht40_rate_800ns;
796 else if (caps & IEEE80211_HTCAP_SHORTGI20)
797 rmax = ieee80211_htrates[i].ht20_rate_400ns;
799 rmax = ieee80211_htrates[i].ht20_rate_800ns;
802 for (i = 0; i < se->se_rates[1]; i++) {
803 r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
807 for (i = 0; i < se->se_xrates[1]; i++) {
808 r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
816 * Compare the capabilities of two entries and decide which is
817 * more desirable (return >0 if a is considered better). Note
818 * that we assume compatibility/usability has already been checked
819 * so we don't need to (e.g. validate whether privacy is supported).
820 * Used to select the best scan candidate for association in a BSS.
823 sta_compare(const struct sta_entry *a, const struct sta_entry *b)
825 #define PREFER(_a,_b,_what) do { \
826 if (((_a) ^ (_b)) & (_what)) \
827 return ((_a) & (_what)) ? 1 : -1; \
833 /* privacy support */
834 PREFER(a->base.se_capinfo, b->base.se_capinfo,
835 IEEE80211_CAPINFO_PRIVACY);
837 /* compare count of previous failures */
838 weight = b->se_fails - a->se_fails;
843 * Compare rssi. If the two are considered equivalent
844 * then fallback to other criteria. We threshold the
845 * comparisons to avoid selecting an ap purely by rssi
846 * when both values may be good but one ap is otherwise
847 * more desirable (e.g. an 11b-only ap with stronger
848 * signal than an 11g ap).
850 rssia = MIN(a->base.se_rssi, STA_RSSI_MAX);
851 rssib = MIN(b->base.se_rssi, STA_RSSI_MAX);
852 if (abs(rssib - rssia) < 5) {
853 /* best/max rate preferred if signal level close enough XXX */
854 maxa = maxrate(&a->base);
855 maxb = maxrate(&b->base);
858 /* XXX use freq for channel preference */
859 /* for now just prefer 5Ghz band to all other bands */
860 PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan),
861 IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1);
863 /* all things being equal, use signal level */
864 return a->base.se_rssi - b->base.se_rssi;
869 * Check rate set suitability and return the best supported rate.
870 * XXX inspect MCS for HT
873 check_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan,
874 const struct ieee80211_scan_entry *se)
876 #define RV(v) ((v) & IEEE80211_RATE_VAL)
877 const struct ieee80211_rateset *srs;
878 int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate;
881 okrate = badrate = 0;
883 srs = ieee80211_get_suprates(vap->iv_ic, chan);
884 nrs = se->se_rates[1];
887 ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate;
888 fixedrate = IEEE80211_FIXED_RATE_NONE;
890 for (i = 0; i < nrs; i++) {
894 * Check any fixed rate is included.
899 * Check against our supported rates.
901 for (j = 0; j < srs->rs_nrates; j++)
902 if (r == RV(srs->rs_rates[j])) {
903 if (r > okrate) /* NB: track max */
908 if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) {
910 * Don't try joining a BSS, if we don't support
911 * one of its basic rates.
917 if (rs == se->se_rates+2) {
918 /* scan xrates too; sort of an algol68-style for loop */
919 nrs = se->se_xrates[1];
920 rs = se->se_xrates+2;
925 if (okrate == 0 || ucastrate != fixedrate)
926 return badrate | IEEE80211_RATE_BASIC;
933 match_id(const uint8_t *ie, const uint8_t *val, int len)
935 return (ie[1] == len && memcmp(ie+2, val, len) == 0);
939 match_ssid(const uint8_t *ie,
940 int nssid, const struct ieee80211_scan_ssid ssids[])
944 for (i = 0; i < nssid; i++) {
945 if (match_id(ie, ssids[i].ssid, ssids[i].len))
951 #ifdef IEEE80211_SUPPORT_TDMA
953 tdma_isfull(const struct ieee80211_tdma_param *tdma)
957 slotcnt = tdma->tdma_slotcnt;
958 for (slot = slotcnt-1; slot >= 0; slot--)
959 if (isclr(tdma->tdma_inuse, slot))
963 #endif /* IEEE80211_SUPPORT_TDMA */
966 * Test a scan candidate for suitability/compatibility.
969 match_bss(struct ieee80211vap *vap,
970 const struct ieee80211_scan_state *ss, struct sta_entry *se0,
973 struct ieee80211com *ic = vap->iv_ic;
974 struct ieee80211_scan_entry *se = &se0->base;
979 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
980 fail |= MATCH_CHANNEL;
982 * NB: normally the desired mode is used to construct
983 * the channel list, but it's possible for the scan
984 * cache to include entries for stations outside this
985 * list so we check the desired mode here to weed them
988 if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
989 (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
990 chanflags[vap->iv_des_mode])
991 fail |= MATCH_CHANNEL;
992 if (vap->iv_opmode == IEEE80211_M_IBSS) {
993 if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
994 fail |= MATCH_CAPINFO;
995 #ifdef IEEE80211_SUPPORT_TDMA
996 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
998 * Adhoc demo network setup shouldn't really be scanning
999 * but just in case skip stations operating in IBSS or
1002 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
1003 fail |= MATCH_CAPINFO;
1005 * TDMA operation cannot coexist with a normal 802.11 network;
1006 * skip if IBSS or ESS capabilities are marked and require
1007 * the beacon have a TDMA ie present.
1009 if (vap->iv_caps & IEEE80211_C_TDMA) {
1010 const struct ieee80211_tdma_param *tdma =
1011 (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie;
1012 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
1015 fail |= MATCH_TDMA_NOIE;
1016 else if (tdma->tdma_version != ts->tdma_version)
1017 fail |= MATCH_TDMA_VERSION;
1018 else if (tdma->tdma_slot != 0)
1019 fail |= MATCH_TDMA_NOTMASTER;
1020 else if (tdma_isfull(tdma))
1021 fail |= MATCH_TDMA_NOSLOT;
1023 else if (ieee80211_local_address(se->se_macaddr))
1024 fail |= MATCH_TDMA_LOCAL;
1027 #endif /* IEEE80211_SUPPORT_TDMA */
1028 #ifdef IEEE80211_SUPPORT_MESH
1029 } else if (vap->iv_opmode == IEEE80211_M_MBSS) {
1030 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
1032 * Mesh nodes have IBSS & ESS bits in capinfo turned off
1033 * and two special ie's that must be present.
1035 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
1036 fail |= MATCH_CAPINFO;
1037 else if (se->se_meshid[0] != IEEE80211_ELEMID_MESHID)
1038 fail |= MATCH_MESH_NOID;
1039 else if (ms->ms_idlen != 0 &&
1040 match_id(se->se_meshid, ms->ms_id, ms->ms_idlen))
1041 fail |= MATCH_MESHID;
1044 if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
1045 fail |= MATCH_CAPINFO;
1047 * If 11d is enabled and we're attempting to join a bss
1048 * that advertises it's country code then compare our
1049 * current settings to what we fetched from the country ie.
1050 * If our country code is unspecified or different then do
1051 * not attempt to join the bss. We should have already
1052 * dispatched an event to user space that identifies the
1053 * new country code so our regdomain config should match.
1055 if ((IEEE80211_IS_CHAN_11D(se->se_chan) ||
1056 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
1057 se->se_cc[0] != 0 &&
1058 (ic->ic_regdomain.country == CTRY_DEFAULT ||
1059 !isocmp(se->se_cc, ic->ic_regdomain.isocc)))
1062 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1063 if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
1064 fail |= MATCH_PRIVACY;
1066 /* XXX does this mean privacy is supported or required? */
1067 if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
1068 fail |= MATCH_PRIVACY;
1070 se0->se_flags &= ~STA_DEMOTE11B;
1071 rate = check_rate(vap, se->se_chan, se);
1072 if (rate & IEEE80211_RATE_BASIC) {
1075 * An 11b-only ap will give a rate mismatch if there is an
1076 * OFDM fixed tx rate for 11g. Try downgrading the channel
1077 * in the scan list to 11b and retry the rate check.
1079 if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) {
1080 rate = check_rate(vap, demote11b(vap, se->se_chan), se);
1081 if ((rate & IEEE80211_RATE_BASIC) == 0) {
1082 fail &= ~MATCH_RATE;
1083 se0->se_flags |= STA_DEMOTE11B;
1086 } else if (rate < 2*24) {
1088 * This is an 11b-only ap. Check the desired mode in
1089 * case that needs to be honored (mode 11g filters out
1090 * 11b-only ap's). Otherwise force any 11g channel used
1091 * in scanning to be demoted.
1093 * NB: we cheat a bit here by looking at the max rate;
1094 * we could/should check the rates.
1096 if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO ||
1097 vap->iv_des_mode == IEEE80211_MODE_11B))
1100 se0->se_flags |= STA_DEMOTE11B;
1102 if (ss->ss_nssid != 0 &&
1103 !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
1105 if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
1106 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
1107 fail |= MATCH_BSSID;
1108 if (se0->se_fails >= STA_FAILS_MAX)
1109 fail |= MATCH_FAILS;
1110 if (se0->se_notseen >= STA_PURGE_SCANS)
1111 fail |= MATCH_NOTSEEN;
1112 if (se->se_rssi < STA_RSSI_MIN)
1114 #ifdef IEEE80211_DEBUG
1115 if (ieee80211_msg(vap, debug)) {
1117 fail & MATCH_FAILS ? '=' :
1118 fail & MATCH_NOTSEEN ? '^' :
1119 fail & MATCH_CC ? '$' :
1120 #ifdef IEEE80211_SUPPORT_TDMA
1121 fail & MATCH_TDMA_NOIE ? '&' :
1122 fail & MATCH_TDMA_VERSION ? 'v' :
1123 fail & MATCH_TDMA_NOTMASTER ? 's' :
1124 fail & MATCH_TDMA_NOSLOT ? 'f' :
1125 fail & MATCH_TDMA_LOCAL ? 'l' :
1127 fail & MATCH_MESH_NOID ? 'm' :
1128 fail ? '-' : '+', ether_sprintf(se->se_macaddr));
1129 printf(" %s%c", ether_sprintf(se->se_bssid),
1130 fail & MATCH_BSSID ? '!' : ' ');
1131 printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
1132 fail & MATCH_CHANNEL ? '!' : ' ');
1133 printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' ');
1134 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
1135 fail & MATCH_RATE ? '!' : ' ');
1137 (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
1138 (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : "",
1139 fail & MATCH_CAPINFO ? '!' : ' ');
1141 (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
1143 fail & MATCH_PRIVACY ? '!' : ' ');
1144 ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]);
1145 printf("%s\n", fail & (MATCH_SSID | MATCH_MESHID) ? "!" : "");
1152 sta_update_notseen(struct sta_table *st)
1154 struct sta_entry *se;
1156 IEEE80211_SCAN_TABLE_LOCK(st);
1157 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1159 * If seen the reset and don't bump the count;
1160 * otherwise bump the ``not seen'' count. Note
1161 * that this insures that stations for which we
1162 * see frames while not scanning but not during
1163 * this scan will not be penalized.
1170 IEEE80211_SCAN_TABLE_UNLOCK(st);
1174 sta_dec_fails(struct sta_table *st)
1176 struct sta_entry *se;
1178 IEEE80211_SCAN_TABLE_LOCK(st);
1179 TAILQ_FOREACH(se, &st->st_entry, se_list)
1182 IEEE80211_SCAN_TABLE_UNLOCK(st);
1185 static struct sta_entry *
1186 select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug)
1188 struct sta_table *st = ss->ss_priv;
1189 struct sta_entry *se, *selbs = NULL;
1191 IEEE80211_DPRINTF(vap, debug, " %s\n",
1192 "macaddr bssid chan rssi rate flag wep essid");
1193 IEEE80211_SCAN_TABLE_LOCK(st);
1194 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1195 ieee80211_ies_expand(&se->base.se_ies);
1196 if (match_bss(vap, ss, se, debug) == 0) {
1199 else if (sta_compare(se, selbs) > 0)
1203 IEEE80211_SCAN_TABLE_UNLOCK(st);
1209 * Pick an ap or ibss network to join or find a channel
1210 * to use to start an ibss network.
1213 sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1215 struct sta_table *st = ss->ss_priv;
1216 struct sta_entry *selbs;
1217 struct ieee80211_channel *chan;
1219 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1220 ("wrong mode %u", vap->iv_opmode));
1222 if (st->st_newscan) {
1223 sta_update_notseen(st);
1226 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1228 * Manual/background scan, don't select+join the
1229 * bss, just return. The scanning framework will
1230 * handle notification that this has completed.
1232 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1236 * Automatic sequencing; look for a candidate and
1237 * if found join the network.
1239 /* NB: unlocked read should be ok */
1240 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1241 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1242 "%s: no scan candidate\n", __func__);
1243 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1247 * If nothing suitable was found decrement
1248 * the failure counts so entries will be
1249 * reconsidered the next time around. We
1250 * really want to do this only for sta's
1251 * where we've previously had some success.
1255 return 0; /* restart scan */
1257 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1258 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1259 return (selbs != NULL);
1262 chan = selbs->base.se_chan;
1263 if (selbs->se_flags & STA_DEMOTE11B)
1264 chan = demote11b(vap, chan);
1265 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1267 return 1; /* terminate scan */
1271 * Lookup an entry in the scan cache. We assume we're
1272 * called from the bottom half or such that we don't need
1273 * to block the bottom half so that it's safe to return
1274 * a reference to an entry w/o holding the lock on the table.
1276 static struct sta_entry *
1277 sta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1279 struct sta_entry *se;
1280 int hash = STA_HASH(macaddr);
1282 IEEE80211_SCAN_TABLE_LOCK(st);
1283 LIST_FOREACH(se, &st->st_hash[hash], se_hash)
1284 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
1286 IEEE80211_SCAN_TABLE_UNLOCK(st);
1288 return se; /* NB: unlocked */
1292 sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1294 struct ieee80211com *ic = vap->iv_ic;
1295 struct ieee80211_node *ni = vap->iv_bss;
1296 struct sta_table *st = ss->ss_priv;
1297 enum ieee80211_phymode mode;
1298 struct sta_entry *se, *selbs;
1299 uint8_t roamRate, curRate, ucastRate;
1300 int8_t roamRssi, curRssi;
1302 se = sta_lookup(st, ni->ni_macaddr);
1304 /* XXX something is wrong */
1308 mode = ieee80211_chan2mode(ic->ic_bsschan);
1309 roamRate = vap->iv_roamparms[mode].rate;
1310 roamRssi = vap->iv_roamparms[mode].rssi;
1311 ucastRate = vap->iv_txparms[mode].ucastrate;
1312 /* NB: the most up to date rssi is in the node, not the scan cache */
1313 curRssi = ic->ic_node_getrssi(ni);
1314 if (ucastRate == IEEE80211_FIXED_RATE_NONE) {
1315 curRate = ni->ni_txrate;
1316 roamRate &= IEEE80211_RATE_VAL;
1317 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1318 "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
1319 __func__, curRssi, curRate, roamRssi, roamRate);
1321 curRate = roamRate; /* NB: insure compare below fails */
1322 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1323 "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi);
1326 * Check if a new ap should be used and switch.
1327 * XXX deauth current ap
1329 if (curRate < roamRate || curRssi < roamRssi) {
1330 if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
1332 * Scan cache contents are too old; force a scan now
1333 * if possible so we have current state to make a
1334 * decision with. We don't kick off a bg scan if
1335 * we're using dynamic turbo and boosted or if the
1337 * XXX force immediate switch on scan complete
1339 if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1340 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle))
1341 ieee80211_bg_scan(vap, 0);
1344 se->base.se_rssi = curRssi;
1345 selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM);
1346 if (selbs != NULL && selbs != se) {
1347 struct ieee80211_channel *chan;
1349 IEEE80211_DPRINTF(vap,
1350 IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG,
1351 "%s: ROAM: curRate %u, roamRate %u, "
1352 "curRssi %d, roamRssi %d\n", __func__,
1353 curRate, roamRate, curRssi, roamRssi);
1355 chan = selbs->base.se_chan;
1356 if (selbs->se_flags & STA_DEMOTE11B)
1357 chan = demote11b(vap, chan);
1358 (void) ieee80211_sta_join(vap, chan, &selbs->base);
1364 * Age entries in the scan cache.
1365 * XXX also do roaming since it's convenient
1368 sta_age(struct ieee80211_scan_state *ss)
1370 struct ieee80211vap *vap = ss->ss_vap;
1374 * If rate control is enabled check periodically to see if
1375 * we should roam from our current connection to one that
1376 * might be better. This only applies when we're operating
1377 * in sta mode and automatic roaming is set.
1379 * XXX repeater station
1380 * XXX do when !bgscan?
1382 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1383 ("wrong mode %u", vap->iv_opmode));
1384 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO &&
1385 (vap->iv_flags & IEEE80211_F_BGSCAN) &&
1386 vap->iv_state >= IEEE80211_S_RUN)
1387 /* XXX vap is implicit */
1388 sta_roam_check(ss, vap);
1392 * Iterate over the entries in the scan cache, invoking
1393 * the callback function on each one.
1396 sta_iterate(struct ieee80211_scan_state *ss,
1397 ieee80211_scan_iter_func *f, void *arg)
1399 struct sta_table *st = ss->ss_priv;
1400 struct sta_entry *se;
1403 mtx_lock(&st->st_scanlock);
1404 gen = st->st_scaniter++;
1406 IEEE80211_SCAN_TABLE_LOCK(st);
1407 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1408 if (se->se_scangen != gen) {
1409 se->se_scangen = gen;
1410 /* update public state */
1411 se->base.se_age = ticks - se->se_lastupdate;
1412 IEEE80211_SCAN_TABLE_UNLOCK(st);
1413 (*f)(arg, &se->base);
1417 IEEE80211_SCAN_TABLE_UNLOCK(st);
1419 mtx_unlock(&st->st_scanlock);
1423 sta_assoc_fail(struct ieee80211_scan_state *ss,
1424 const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason)
1426 struct sta_table *st = ss->ss_priv;
1427 struct sta_entry *se;
1429 se = sta_lookup(st, macaddr);
1432 se->se_lastfail = ticks;
1433 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1434 macaddr, "%s: reason %u fails %u",
1435 __func__, reason, se->se_fails);
1440 sta_assoc_success(struct ieee80211_scan_state *ss,
1441 const uint8_t macaddr[IEEE80211_ADDR_LEN])
1443 struct sta_table *st = ss->ss_priv;
1444 struct sta_entry *se;
1446 se = sta_lookup(st, macaddr);
1450 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1451 macaddr, "%s: fails %u",
1452 __func__, se->se_fails);
1454 se->se_lastassoc = ticks;
1458 static const struct ieee80211_scanner sta_default = {
1459 .scan_name = "default",
1460 .scan_attach = sta_attach,
1461 .scan_detach = sta_detach,
1462 .scan_start = sta_start,
1463 .scan_restart = sta_restart,
1464 .scan_cancel = sta_cancel,
1465 .scan_end = sta_pick_bss,
1466 .scan_flush = sta_flush,
1467 .scan_add = sta_add,
1468 .scan_age = sta_age,
1469 .scan_iterate = sta_iterate,
1470 .scan_assoc_fail = sta_assoc_fail,
1471 .scan_assoc_success = sta_assoc_success,
1473 IEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default);
1476 * Adhoc mode-specific support.
1479 static const uint16_t adhocWorld[] = /* 36, 40, 44, 48 */
1480 { 5180, 5200, 5220, 5240 };
1481 static const uint16_t adhocFcc3[] = /* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */
1482 { 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 };
1483 static const uint16_t adhocMkk[] = /* 34, 38, 42, 46 */
1484 { 5170, 5190, 5210, 5230 };
1485 static const uint16_t adhoc11b[] = /* 10, 11 */
1488 static const struct scanlist adhocScanTable[] = {
1489 { IEEE80211_MODE_11B, X(adhoc11b) },
1490 { IEEE80211_MODE_11A, X(adhocWorld) },
1491 { IEEE80211_MODE_11A, X(adhocFcc3) },
1492 { IEEE80211_MODE_11B, X(adhocMkk) },
1498 * Start an adhoc-mode scan by populating the channel list.
1501 adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1503 struct sta_table *st = ss->ss_priv;
1505 makescanlist(ss, vap, adhocScanTable);
1507 if (ss->ss_mindwell == 0)
1508 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */
1509 if (ss->ss_maxdwell == 0)
1510 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
1519 * Select a channel to start an adhoc network on.
1520 * The channel list was populated with appropriate
1521 * channels so select one that looks least occupied.
1523 static struct ieee80211_channel *
1524 adhoc_pick_channel(struct ieee80211_scan_state *ss, int flags)
1526 struct sta_table *st = ss->ss_priv;
1527 struct sta_entry *se;
1528 struct ieee80211_channel *c, *bestchan;
1529 int i, bestrssi, maxrssi;
1534 IEEE80211_SCAN_TABLE_LOCK(st);
1535 for (i = 0; i < ss->ss_last; i++) {
1536 c = ss->ss_chans[i];
1537 /* never consider a channel with radar */
1538 if (IEEE80211_IS_CHAN_RADAR(c))
1540 /* skip channels disallowed by regulatory settings */
1541 if (IEEE80211_IS_CHAN_NOADHOC(c))
1543 /* check channel attributes for band compatibility */
1544 if (flags != 0 && (c->ic_flags & flags) != flags)
1547 TAILQ_FOREACH(se, &st->st_entry, se_list) {
1548 if (se->base.se_chan != c)
1550 if (se->base.se_rssi > maxrssi)
1551 maxrssi = se->base.se_rssi;
1553 if (bestchan == NULL || maxrssi < bestrssi)
1556 IEEE80211_SCAN_TABLE_UNLOCK(st);
1562 * Pick an ibss network to join or find a channel
1563 * to use to start an ibss network.
1566 adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1568 struct sta_table *st = ss->ss_priv;
1569 struct sta_entry *selbs;
1570 struct ieee80211_channel *chan;
1571 struct ieee80211com *ic = vap->iv_ic;
1573 KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1574 vap->iv_opmode == IEEE80211_M_AHDEMO ||
1575 vap->iv_opmode == IEEE80211_M_MBSS,
1576 ("wrong opmode %u", vap->iv_opmode));
1578 if (st->st_newscan) {
1579 sta_update_notseen(st);
1582 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1584 * Manual/background scan, don't select+join the
1585 * bss, just return. The scanning framework will
1586 * handle notification that this has completed.
1588 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1592 * Automatic sequencing; look for a candidate and
1593 * if found join the network.
1595 /* NB: unlocked read should be ok */
1596 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1597 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1598 "%s: no scan candidate\n", __func__);
1599 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1602 /* NB: never auto-start a tdma network for slot !0 */
1603 #ifdef IEEE80211_SUPPORT_TDMA
1604 if (vap->iv_des_nssid &&
1605 ((vap->iv_caps & IEEE80211_C_TDMA) == 0 ||
1606 ieee80211_tdma_getslot(vap) == 0)) {
1608 if (vap->iv_des_nssid) {
1611 * No existing adhoc network to join and we have
1612 * an ssid; start one up. If no channel was
1613 * specified, try to select a channel.
1615 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1616 IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1617 chan = adhoc_pick_channel(ss, 0);
1619 chan = vap->iv_des_chan;
1621 struct ieee80211com *ic = vap->iv_ic;
1623 * Create a HT capable IBSS; the per-node
1624 * probe request/response will result in
1625 * "correct" rate control capabilities being
1628 chan = ieee80211_ht_adjust_channel(ic,
1629 chan, vap->iv_flags_ht);
1630 ieee80211_create_ibss(vap, chan);
1635 * If nothing suitable was found decrement
1636 * the failure counts so entries will be
1637 * reconsidered the next time around. We
1638 * really want to do this only for sta's
1639 * where we've previously had some success.
1643 return 0; /* restart scan */
1645 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1646 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1647 return (selbs != NULL);
1650 chan = selbs->base.se_chan;
1651 if (selbs->se_flags & STA_DEMOTE11B)
1652 chan = demote11b(vap, chan);
1654 * If HT is available, make it a possibility here.
1655 * The intent is to enable HT20/HT40 when joining a non-HT
1656 * IBSS node; we can then advertise HT IEs and speak HT
1657 * to any subsequent nodes that support it.
1659 chan = ieee80211_ht_adjust_channel(ic,
1660 chan, vap->iv_flags_ht);
1661 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1663 return 1; /* terminate scan */
1667 * Age entries in the scan cache.
1670 adhoc_age(struct ieee80211_scan_state *ss)
1672 struct sta_table *st = ss->ss_priv;
1673 struct sta_entry *se, *next;
1675 IEEE80211_SCAN_TABLE_LOCK(st);
1676 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
1677 if (se->se_notseen > STA_PURGE_SCANS) {
1678 TAILQ_REMOVE(&st->st_entry, se, se_list);
1679 LIST_REMOVE(se, se_hash);
1680 ieee80211_ies_cleanup(&se->base.se_ies);
1681 free(se, M_80211_SCAN);
1684 IEEE80211_SCAN_TABLE_UNLOCK(st);
1687 static const struct ieee80211_scanner adhoc_default = {
1688 .scan_name = "default",
1689 .scan_attach = sta_attach,
1690 .scan_detach = sta_detach,
1691 .scan_start = adhoc_start,
1692 .scan_restart = sta_restart,
1693 .scan_cancel = sta_cancel,
1694 .scan_end = adhoc_pick_bss,
1695 .scan_flush = sta_flush,
1696 .scan_pickchan = adhoc_pick_channel,
1697 .scan_add = sta_add,
1698 .scan_age = adhoc_age,
1699 .scan_iterate = sta_iterate,
1700 .scan_assoc_fail = sta_assoc_fail,
1701 .scan_assoc_success = sta_assoc_success,
1703 IEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default);
1704 IEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default);
1707 ap_force_promisc(struct ieee80211com *ic)
1709 struct ifnet *ifp = ic->ic_ifp;
1712 /* set interface into promiscuous mode */
1713 ifp->if_flags |= IFF_PROMISC;
1714 ieee80211_runtask(ic, &ic->ic_promisc_task);
1715 IEEE80211_UNLOCK(ic);
1719 ap_reset_promisc(struct ieee80211com *ic)
1722 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
1723 IEEE80211_UNLOCK(ic);
1727 ap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1729 struct sta_table *st = ss->ss_priv;
1731 makescanlist(ss, vap, staScanTable);
1733 if (ss->ss_mindwell == 0)
1734 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */
1735 if (ss->ss_maxdwell == 0)
1736 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */
1741 ap_force_promisc(vap->iv_ic);
1746 * Cancel an ongoing scan.
1749 ap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1751 ap_reset_promisc(vap->iv_ic);
1756 * Pick a quiet channel to use for ap operation.
1758 static struct ieee80211_channel *
1759 ap_pick_channel(struct ieee80211_scan_state *ss, int flags)
1761 struct sta_table *st = ss->ss_priv;
1762 struct ieee80211_channel *bestchan = NULL;
1765 /* XXX select channel more intelligently, e.g. channel spread, power */
1766 /* NB: use scan list order to preserve channel preference */
1767 for (i = 0; i < ss->ss_last; i++) {
1768 struct ieee80211_channel *chan = ss->ss_chans[i];
1770 * If the channel is unoccupied the max rssi
1771 * should be zero; just take it. Otherwise
1772 * track the channel with the lowest rssi and
1773 * use that when all channels appear occupied.
1775 if (IEEE80211_IS_CHAN_RADAR(chan))
1777 if (IEEE80211_IS_CHAN_NOHOSTAP(chan))
1779 /* check channel attributes for band compatibility */
1780 if (flags != 0 && (chan->ic_flags & flags) != flags)
1782 KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size"));
1783 /* XXX channel have interference */
1784 if (st->st_maxrssi[chan->ic_ieee] == 0) {
1785 /* XXX use other considerations */
1788 if (bestchan == NULL ||
1789 st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee])
1796 * Pick a quiet channel to use for ap operation.
1799 ap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1801 struct ieee80211com *ic = vap->iv_ic;
1802 struct ieee80211_channel *bestchan;
1804 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
1805 ("wrong opmode %u", vap->iv_opmode));
1806 bestchan = ap_pick_channel(ss, 0);
1807 if (bestchan == NULL) {
1808 /* no suitable channel, should not happen */
1809 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1810 "%s: no suitable channel! (should not happen)\n", __func__);
1811 /* XXX print something? */
1812 return 0; /* restart scan */
1815 * If this is a dynamic turbo channel, start with the unboosted one.
1817 if (IEEE80211_IS_CHAN_TURBO(bestchan)) {
1818 bestchan = ieee80211_find_channel(ic, bestchan->ic_freq,
1819 bestchan->ic_flags & ~IEEE80211_CHAN_TURBO);
1820 if (bestchan == NULL) {
1821 /* should never happen ?? */
1825 ap_reset_promisc(ic);
1826 if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) {
1828 * Manual/background scan, don't select+join the
1829 * bss, just return. The scanning framework will
1830 * handle notification that this has completed.
1832 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1835 ieee80211_create_ibss(vap,
1836 ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ht));
1840 static const struct ieee80211_scanner ap_default = {
1841 .scan_name = "default",
1842 .scan_attach = sta_attach,
1843 .scan_detach = sta_detach,
1844 .scan_start = ap_start,
1845 .scan_restart = sta_restart,
1846 .scan_cancel = ap_cancel,
1848 .scan_flush = sta_flush,
1849 .scan_pickchan = ap_pick_channel,
1850 .scan_add = sta_add,
1851 .scan_age = adhoc_age,
1852 .scan_iterate = sta_iterate,
1853 .scan_assoc_success = sta_assoc_success,
1854 .scan_assoc_fail = sta_assoc_fail,
1856 IEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
1858 #ifdef IEEE80211_SUPPORT_MESH
1860 * Pick an mbss network to join or find a channel
1861 * to use to start an mbss network.
1864 mesh_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1866 struct sta_table *st = ss->ss_priv;
1867 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1868 struct sta_entry *selbs;
1869 struct ieee80211_channel *chan;
1871 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
1872 ("wrong opmode %u", vap->iv_opmode));
1874 if (st->st_newscan) {
1875 sta_update_notseen(st);
1878 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1880 * Manual/background scan, don't select+join the
1881 * bss, just return. The scanning framework will
1882 * handle notification that this has completed.
1884 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1888 * Automatic sequencing; look for a candidate and
1889 * if found join the network.
1891 /* NB: unlocked read should be ok */
1892 if (TAILQ_FIRST(&st->st_entry) == NULL) {
1893 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1894 "%s: no scan candidate\n", __func__);
1895 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1898 if (ms->ms_idlen != 0) {
1900 * No existing mbss network to join and we have
1901 * a meshid; start one up. If no channel was
1902 * specified, try to select a channel.
1904 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1905 IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1906 struct ieee80211com *ic = vap->iv_ic;
1908 chan = adhoc_pick_channel(ss, 0);
1910 chan = ieee80211_ht_adjust_channel(ic,
1911 chan, vap->iv_flags_ht);
1913 chan = vap->iv_des_chan;
1915 ieee80211_create_ibss(vap, chan);
1920 * If nothing suitable was found decrement
1921 * the failure counts so entries will be
1922 * reconsidered the next time around. We
1923 * really want to do this only for sta's
1924 * where we've previously had some success.
1928 return 0; /* restart scan */
1930 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1931 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1932 return (selbs != NULL);
1935 chan = selbs->base.se_chan;
1936 if (selbs->se_flags & STA_DEMOTE11B)
1937 chan = demote11b(vap, chan);
1938 if (!ieee80211_sta_join(vap, chan, &selbs->base))
1940 return 1; /* terminate scan */
1943 static const struct ieee80211_scanner mesh_default = {
1944 .scan_name = "default",
1945 .scan_attach = sta_attach,
1946 .scan_detach = sta_detach,
1947 .scan_start = adhoc_start,
1948 .scan_restart = sta_restart,
1949 .scan_cancel = sta_cancel,
1950 .scan_end = mesh_pick_bss,
1951 .scan_flush = sta_flush,
1952 .scan_pickchan = adhoc_pick_channel,
1953 .scan_add = sta_add,
1954 .scan_age = adhoc_age,
1955 .scan_iterate = sta_iterate,
1956 .scan_assoc_fail = sta_assoc_fail,
1957 .scan_assoc_success = sta_assoc_success,
1959 IEEE80211_SCANNER_ALG(mesh, IEEE80211_M_MBSS, mesh_default);
1960 #endif /* IEEE80211_SUPPORT_MESH */