ieee80211_scan_sta.c revision 184274
1170530Ssam/*-
2178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3170530Ssam * All rights reserved.
4170530Ssam *
5170530Ssam * Redistribution and use in source and binary forms, with or without
6170530Ssam * modification, are permitted provided that the following conditions
7170530Ssam * are met:
8170530Ssam * 1. Redistributions of source code must retain the above copyright
9170530Ssam *    notice, this list of conditions and the following disclaimer.
10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright
11170530Ssam *    notice, this list of conditions and the following disclaimer in the
12170530Ssam *    documentation and/or other materials provided with the distribution.
13170530Ssam *
14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170530Ssam */
25170530Ssam
26170530Ssam#include <sys/cdefs.h>
27170530Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_scan_sta.c 184274 2008-10-25 23:32:24Z sam $");
28170530Ssam
29170530Ssam/*
30170530Ssam * IEEE 802.11 station scanning support.
31170530Ssam */
32178354Ssam#include "opt_wlan.h"
33178354Ssam
34170530Ssam#include <sys/param.h>
35170530Ssam#include <sys/systm.h>
36170530Ssam#include <sys/kernel.h>
37170530Ssam#include <sys/module.h>
38170530Ssam
39170530Ssam#include <sys/socket.h>
40170530Ssam
41170530Ssam#include <net/if.h>
42170530Ssam#include <net/if_media.h>
43170530Ssam#include <net/ethernet.h>
44170530Ssam
45170530Ssam#include <net80211/ieee80211_var.h>
46178354Ssam#include <net80211/ieee80211_input.h>
47178354Ssam#include <net80211/ieee80211_regdomain.h>
48170530Ssam
49170530Ssam#include <net/bpf.h>
50170530Ssam
51170530Ssam/*
52170530Ssam * Parameters for managing cache entries:
53170530Ssam *
54170530Ssam * o a station with STA_FAILS_MAX failures is not considered
55170530Ssam *   when picking a candidate
56170530Ssam * o a station that hasn't had an update in STA_PURGE_SCANS
57170530Ssam *   (background) scans is discarded
58170530Ssam * o after STA_FAILS_AGE seconds we clear the failure count
59170530Ssam */
60170530Ssam#define	STA_FAILS_MAX	2		/* assoc failures before ignored */
61170530Ssam#define	STA_FAILS_AGE	(2*60)		/* time before clearing fails (secs) */
62170530Ssam#define	STA_PURGE_SCANS	2		/* age for purging entries (scans) */
63170530Ssam
64170530Ssam/* XXX tunable */
65170530Ssam#define	STA_RSSI_MIN	8		/* min acceptable rssi */
66170530Ssam#define	STA_RSSI_MAX	40		/* max rssi for comparison */
67170530Ssam
68170530Ssamstruct sta_entry {
69170530Ssam	struct ieee80211_scan_entry base;
70170530Ssam	TAILQ_ENTRY(sta_entry) se_list;
71170530Ssam	LIST_ENTRY(sta_entry) se_hash;
72170530Ssam	uint8_t		se_fails;		/* failure to associate count */
73170530Ssam	uint8_t		se_seen;		/* seen during current scan */
74170530Ssam	uint8_t		se_notseen;		/* not seen in previous scans */
75170530Ssam	uint8_t		se_flags;
76178354Ssam#define	STA_SSID_MATCH	0x01
77178354Ssam#define	STA_BSSID_MATCH	0x02
78170530Ssam	uint32_t	se_avgrssi;		/* LPF rssi state */
79170530Ssam	unsigned long	se_lastupdate;		/* time of last update */
80170530Ssam	unsigned long	se_lastfail;		/* time of last failure */
81170530Ssam	unsigned long	se_lastassoc;		/* time of last association */
82170530Ssam	u_int		se_scangen;		/* iterator scan gen# */
83178354Ssam	u_int		se_countrygen;		/* gen# of last cc notify */
84170530Ssam};
85170530Ssam
86170530Ssam#define	STA_HASHSIZE	32
87170530Ssam/* simple hash is enough for variation of macaddr */
88170530Ssam#define	STA_HASH(addr)	\
89170530Ssam	(((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE)
90170530Ssam
91170530Ssamstruct sta_table {
92170530Ssam	struct mtx	st_lock;		/* on scan table */
93170530Ssam	TAILQ_HEAD(, sta_entry) st_entry;	/* all entries */
94170530Ssam	LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE];
95178354Ssam	struct mtx	st_scanlock;		/* on st_scaniter */
96178354Ssam	u_int		st_scaniter;		/* gen# for iterator */
97178354Ssam	u_int		st_scangen;		/* scan generation # */
98170530Ssam	int		st_newscan;
99178354Ssam	/* ap-related state */
100178354Ssam	int		st_maxrssi[IEEE80211_CHAN_MAX];
101170530Ssam};
102170530Ssam
103170530Ssamstatic void sta_flush_table(struct sta_table *);
104171409Ssam/*
105171409Ssam * match_bss returns a bitmask describing if an entry is suitable
106171409Ssam * for use.  If non-zero the entry was deemed not suitable and it's
107171409Ssam * contents explains why.  The following flags are or'd to to this
108171409Ssam * mask and can be used to figure out why the entry was rejected.
109171409Ssam */
110171409Ssam#define	MATCH_CHANNEL	0x001	/* channel mismatch */
111171409Ssam#define	MATCH_CAPINFO	0x002	/* capabilities mismatch, e.g. no ess */
112171409Ssam#define	MATCH_PRIVACY	0x004	/* privacy mismatch */
113171409Ssam#define	MATCH_RATE	0x008	/* rate set mismatch */
114171409Ssam#define	MATCH_SSID	0x010	/* ssid mismatch */
115171409Ssam#define	MATCH_BSSID	0x020	/* bssid mismatch */
116171409Ssam#define	MATCH_FAILS	0x040	/* too many failed auth attempts */
117171409Ssam#define	MATCH_NOTSEEN	0x080	/* not seen in recent scans */
118171409Ssam#define	MATCH_RSSI	0x100	/* rssi deemed too low to use */
119178354Ssam#define	MATCH_CC	0x200	/* country code mismatch */
120178354Ssamstatic int match_bss(struct ieee80211vap *,
121170530Ssam	const struct ieee80211_scan_state *, struct sta_entry *, int);
122178354Ssamstatic void adhoc_age(struct ieee80211_scan_state *);
123170530Ssam
124178354Ssamstatic __inline int
125178354Ssamisocmp(const uint8_t cc1[], const uint8_t cc2[])
126178354Ssam{
127178354Ssam     return (cc1[0] == cc2[0] && cc1[1] == cc2[1]);
128178354Ssam}
129178354Ssam
130170530Ssam/* number of references from net80211 layer */
131170530Ssamstatic	int nrefs = 0;
132170530Ssam
133170530Ssam/*
134170530Ssam * Attach prior to any scanning work.
135170530Ssam */
136170530Ssamstatic int
137170530Ssamsta_attach(struct ieee80211_scan_state *ss)
138170530Ssam{
139170530Ssam	struct sta_table *st;
140170530Ssam
141184210Sdes	MALLOC(st, struct sta_table *, sizeof(struct sta_table),
142170530Ssam		M_80211_SCAN, M_NOWAIT | M_ZERO);
143170530Ssam	if (st == NULL)
144170530Ssam		return 0;
145170530Ssam	mtx_init(&st->st_lock, "scantable", "802.11 scan table", MTX_DEF);
146170530Ssam	mtx_init(&st->st_scanlock, "scangen", "802.11 scangen", MTX_DEF);
147170530Ssam	TAILQ_INIT(&st->st_entry);
148170530Ssam	ss->ss_priv = st;
149170530Ssam	nrefs++;			/* NB: we assume caller locking */
150170530Ssam	return 1;
151170530Ssam}
152170530Ssam
153170530Ssam/*
154170530Ssam * Cleanup any private state.
155170530Ssam */
156170530Ssamstatic int
157170530Ssamsta_detach(struct ieee80211_scan_state *ss)
158170530Ssam{
159170530Ssam	struct sta_table *st = ss->ss_priv;
160170530Ssam
161170530Ssam	if (st != NULL) {
162170530Ssam		sta_flush_table(st);
163170530Ssam		mtx_destroy(&st->st_lock);
164170530Ssam		mtx_destroy(&st->st_scanlock);
165184210Sdes		FREE(st, M_80211_SCAN);
166170530Ssam		KASSERT(nrefs > 0, ("imbalanced attach/detach"));
167170530Ssam		nrefs--;		/* NB: we assume caller locking */
168170530Ssam	}
169170530Ssam	return 1;
170170530Ssam}
171170530Ssam
172170530Ssam/*
173170530Ssam * Flush all per-scan state.
174170530Ssam */
175170530Ssamstatic int
176170530Ssamsta_flush(struct ieee80211_scan_state *ss)
177170530Ssam{
178170530Ssam	struct sta_table *st = ss->ss_priv;
179170530Ssam
180170530Ssam	mtx_lock(&st->st_lock);
181170530Ssam	sta_flush_table(st);
182170530Ssam	mtx_unlock(&st->st_lock);
183170530Ssam	ss->ss_last = 0;
184170530Ssam	return 0;
185170530Ssam}
186170530Ssam
187170530Ssam/*
188170530Ssam * Flush all entries in the scan cache.
189170530Ssam */
190170530Ssamstatic void
191170530Ssamsta_flush_table(struct sta_table *st)
192170530Ssam{
193170530Ssam	struct sta_entry *se, *next;
194170530Ssam
195170530Ssam	TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
196170530Ssam		TAILQ_REMOVE(&st->st_entry, se, se_list);
197170530Ssam		LIST_REMOVE(se, se_hash);
198178354Ssam		ieee80211_ies_cleanup(&se->base.se_ies);
199184210Sdes		FREE(se, M_80211_SCAN);
200170530Ssam	}
201178354Ssam	memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi));
202170530Ssam}
203170530Ssam
204170530Ssam/*
205170530Ssam * Process a beacon or probe response frame; create an
206170530Ssam * entry in the scan cache or update any previous entry.
207170530Ssam */
208170530Ssamstatic int
209170530Ssamsta_add(struct ieee80211_scan_state *ss,
210170530Ssam	const struct ieee80211_scanparams *sp,
211170530Ssam	const struct ieee80211_frame *wh,
212170530Ssam	int subtype, int rssi, int noise, int rstamp)
213170530Ssam{
214170530Ssam#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
215170530Ssam#define	PICK1ST(_ss) \
216170530Ssam	((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
217170530Ssam	IEEE80211_SCAN_PICK1ST)
218170530Ssam	struct sta_table *st = ss->ss_priv;
219170530Ssam	const uint8_t *macaddr = wh->i_addr2;
220178354Ssam	struct ieee80211vap *vap = ss->ss_vap;
221178354Ssam	struct ieee80211com *ic = vap->iv_ic;
222170530Ssam	struct sta_entry *se;
223170530Ssam	struct ieee80211_scan_entry *ise;
224178354Ssam	int hash;
225170530Ssam
226170530Ssam	hash = STA_HASH(macaddr);
227170530Ssam
228170530Ssam	mtx_lock(&st->st_lock);
229170530Ssam	LIST_FOREACH(se, &st->st_hash[hash], se_hash)
230170530Ssam		if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
231170530Ssam			goto found;
232184210Sdes	MALLOC(se, struct sta_entry *, sizeof(struct sta_entry),
233170530Ssam		M_80211_SCAN, M_NOWAIT | M_ZERO);
234170530Ssam	if (se == NULL) {
235170530Ssam		mtx_unlock(&st->st_lock);
236170530Ssam		return 0;
237170530Ssam	}
238178354Ssam	se->se_scangen = st->st_scaniter-1;
239178354Ssam	se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
240170530Ssam	IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
241170530Ssam	TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
242170530Ssam	LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
243170530Ssamfound:
244170530Ssam	ise = &se->base;
245170530Ssam	/* XXX ap beaconing multiple ssid w/ same bssid */
246170530Ssam	if (sp->ssid[1] != 0 &&
247170530Ssam	    (ISPROBE(subtype) || ise->se_ssid[1] == 0))
248170530Ssam		memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]);
249170530Ssam	KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE,
250170530Ssam		("rate set too large: %u", sp->rates[1]));
251170530Ssam	memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]);
252170530Ssam	if (sp->xrates != NULL) {
253170530Ssam		/* XXX validate xrates[1] */
254178354Ssam		KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE,
255170530Ssam			("xrate set too large: %u", sp->xrates[1]));
256170530Ssam		memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]);
257170530Ssam	} else
258170530Ssam		ise->se_xrates[1] = 0;
259170530Ssam	IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
260178354Ssam	if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) {
261173368Ssam		/*
262173368Ssam		 * Record rssi data using extended precision LPF filter.
263173368Ssam		 *
264173368Ssam		 * NB: use only on-channel data to insure we get a good
265173368Ssam		 *     estimate of the signal we'll see when associated.
266173368Ssam		 */
267178354Ssam		IEEE80211_RSSI_LPF(se->se_avgrssi, rssi);
268178354Ssam		ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi);
269173368Ssam		ise->se_noise = noise;
270173368Ssam	}
271170530Ssam	ise->se_rstamp = rstamp;
272170530Ssam	memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
273170530Ssam	ise->se_intval = sp->bintval;
274170530Ssam	ise->se_capinfo = sp->capinfo;
275173368Ssam	/*
276173368Ssam	 * Beware of overriding se_chan for frames seen
277173368Ssam	 * off-channel; this can cause us to attempt an
278178354Ssam	 * association on the wrong channel.
279173368Ssam	 */
280178354Ssam	if (sp->status & IEEE80211_BPARSE_OFFCHAN) {
281173862Ssam		struct ieee80211_channel *c;
282173368Ssam		/*
283173862Ssam		 * Off-channel, locate the home/bss channel for the sta
284178354Ssam		 * using the value broadcast in the DSPARMS ie.  We know
285178354Ssam		 * sp->chan has this value because it's used to calculate
286178354Ssam		 * IEEE80211_BPARSE_OFFCHAN.
287173368Ssam		 */
288178354Ssam		c = ieee80211_find_channel_byieee(ic, sp->chan,
289178354Ssam		    ic->ic_curchan->ic_flags);
290173956Ssam		if (c != NULL) {
291173956Ssam			ise->se_chan = c;
292173956Ssam		} else if (ise->se_chan == NULL) {
293173862Ssam			/* should not happen, pick something */
294178354Ssam			ise->se_chan = ic->ic_curchan;
295173862Ssam		}
296173862Ssam	} else
297178354Ssam		ise->se_chan = ic->ic_curchan;
298170530Ssam	ise->se_fhdwell = sp->fhdwell;
299170530Ssam	ise->se_fhindex = sp->fhindex;
300170530Ssam	ise->se_erp = sp->erp;
301170530Ssam	ise->se_timoff = sp->timoff;
302170530Ssam	if (sp->tim != NULL) {
303170530Ssam		const struct ieee80211_tim_ie *tim =
304170530Ssam		    (const struct ieee80211_tim_ie *) sp->tim;
305170530Ssam		ise->se_dtimperiod = tim->tim_period;
306170530Ssam	}
307178354Ssam	if (sp->country != NULL) {
308178354Ssam		const struct ieee80211_country_ie *cie =
309178354Ssam		    (const struct ieee80211_country_ie *) sp->country;
310178354Ssam		/*
311178354Ssam		 * If 11d is enabled and we're attempting to join a bss
312178354Ssam		 * that advertises it's country code then compare our
313178354Ssam		 * current settings to what we fetched from the country ie.
314178354Ssam		 * If our country code is unspecified or different then
315178354Ssam		 * dispatch an event to user space that identifies the
316178354Ssam		 * country code so our regdomain config can be changed.
317178354Ssam		 */
318178354Ssam		/* XXX only for STA mode? */
319178354Ssam		if ((IEEE80211_IS_CHAN_11D(ise->se_chan) ||
320178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
321178354Ssam		    (ic->ic_regdomain.country == CTRY_DEFAULT ||
322178354Ssam		     !isocmp(cie->cc, ic->ic_regdomain.isocc))) {
323178354Ssam			/* only issue one notify event per scan */
324178354Ssam			if (se->se_countrygen != st->st_scangen) {
325178354Ssam				ieee80211_notify_country(vap, ise->se_bssid,
326178354Ssam				    cie->cc);
327178354Ssam				se->se_countrygen = st->st_scangen;
328178354Ssam			}
329178354Ssam		}
330178354Ssam		ise->se_cc[0] = cie->cc[0];
331178354Ssam		ise->se_cc[1] = cie->cc[1];
332178354Ssam	}
333178354Ssam	/* NB: no need to setup ie ptrs; they are not (currently) used */
334178354Ssam	(void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len);
335170530Ssam
336170530Ssam	/* clear failure count after STA_FAIL_AGE passes */
337170530Ssam	if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) {
338170530Ssam		se->se_fails = 0;
339178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
340170530Ssam		    "%s: fails %u", __func__, se->se_fails);
341170530Ssam	}
342170530Ssam
343170530Ssam	se->se_lastupdate = ticks;		/* update time */
344170530Ssam	se->se_seen = 1;
345170530Ssam	se->se_notseen = 0;
346170530Ssam
347178354Ssam	if (rssi > st->st_maxrssi[sp->bchan])
348178354Ssam		st->st_maxrssi[sp->bchan] = rssi;
349178354Ssam
350170530Ssam	mtx_unlock(&st->st_lock);
351170530Ssam
352170530Ssam	/*
353170530Ssam	 * If looking for a quick choice and nothing's
354170530Ssam	 * been found check here.
355170530Ssam	 */
356178354Ssam	if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0)
357170530Ssam		ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
358170530Ssam
359170530Ssam	return 1;
360170530Ssam#undef PICK1ST
361170530Ssam#undef ISPROBE
362170530Ssam}
363170530Ssam
364170530Ssam/*
365170530Ssam * Check if a channel is excluded by user request.
366170530Ssam */
367170530Ssamstatic int
368178354Ssamisexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c)
369170530Ssam{
370178354Ssam	return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) ||
371178354Ssam	    (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
372178354Ssam	     c->ic_freq != vap->iv_des_chan->ic_freq));
373170530Ssam}
374170530Ssam
375170530Ssamstatic struct ieee80211_channel *
376170530Ssamfind11gchannel(struct ieee80211com *ic, int i, int freq)
377170530Ssam{
378170530Ssam	struct ieee80211_channel *c;
379170530Ssam	int j;
380170530Ssam
381170530Ssam	/*
382170530Ssam	 * The normal ordering in the channel list is b channel
383170530Ssam	 * immediately followed by g so optimize the search for
384170530Ssam	 * this.  We'll still do a full search just in case.
385170530Ssam	 */
386170530Ssam	for (j = i+1; j < ic->ic_nchans; j++) {
387170530Ssam		c = &ic->ic_channels[j];
388170530Ssam		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
389170530Ssam			return c;
390170530Ssam	}
391170530Ssam	for (j = 0; j < i; j++) {
392170530Ssam		c = &ic->ic_channels[j];
393170530Ssam		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
394170530Ssam			return c;
395170530Ssam	}
396170530Ssam	return NULL;
397170530Ssam}
398170530Ssamstatic const u_int chanflags[IEEE80211_MODE_MAX] = {
399170530Ssam	IEEE80211_CHAN_B,	/* IEEE80211_MODE_AUTO */
400170530Ssam	IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
401170530Ssam	IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
402170530Ssam	IEEE80211_CHAN_G,	/* IEEE80211_MODE_11G */
403170530Ssam	IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
404170530Ssam	IEEE80211_CHAN_A,	/* IEEE80211_MODE_TURBO_A (check base channel)*/
405170530Ssam	IEEE80211_CHAN_G,	/* IEEE80211_MODE_TURBO_G */
406170530Ssam	IEEE80211_CHAN_ST,	/* IEEE80211_MODE_STURBO_A */
407170530Ssam	IEEE80211_CHAN_A,	/* IEEE80211_MODE_11NA (check legacy) */
408170530Ssam	IEEE80211_CHAN_G,	/* IEEE80211_MODE_11NG (check legacy) */
409170530Ssam};
410170530Ssam
411170530Ssamstatic void
412178354Ssamadd_channels(struct ieee80211vap *vap,
413170530Ssam	struct ieee80211_scan_state *ss,
414170530Ssam	enum ieee80211_phymode mode, const uint16_t freq[], int nfreq)
415170530Ssam{
416170530Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
417178354Ssam	struct ieee80211com *ic = vap->iv_ic;
418170530Ssam	struct ieee80211_channel *c, *cg;
419170530Ssam	u_int modeflags;
420170530Ssam	int i;
421170530Ssam
422170530Ssam	KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
423170530Ssam	modeflags = chanflags[mode];
424170530Ssam	for (i = 0; i < nfreq; i++) {
425170530Ssam		if (ss->ss_last >= IEEE80211_SCAN_MAX)
426170530Ssam			break;
427170530Ssam
428170530Ssam		c = ieee80211_find_channel(ic, freq[i], modeflags);
429178354Ssam		if (c == NULL || isexcluded(vap, c))
430170530Ssam			continue;
431170530Ssam		if (mode == IEEE80211_MODE_AUTO) {
432170530Ssam			/*
433170530Ssam			 * XXX special-case 11b/g channels so we select
434178354Ssam			 *     the g channel if both are present.
435170530Ssam			 */
436178354Ssam			if (IEEE80211_IS_CHAN_B(c) &&
437178354Ssam			    (cg = find11gchannel(ic, i, c->ic_freq)) != NULL)
438178354Ssam				c = cg;
439170530Ssam		}
440170530Ssam		ss->ss_chans[ss->ss_last++] = c;
441170530Ssam	}
442170530Ssam#undef N
443170530Ssam}
444170530Ssam
445170530Ssamstruct scanlist {
446170530Ssam	uint16_t	mode;
447170530Ssam	uint16_t	count;
448170530Ssam	const uint16_t	*list;
449170530Ssam};
450170530Ssam
451170530Ssamstatic int
452170530Ssamchecktable(const struct scanlist *scan, const struct ieee80211_channel *c)
453170530Ssam{
454170530Ssam	int i;
455170530Ssam
456170530Ssam	for (; scan->list != NULL; scan++) {
457170530Ssam		for (i = 0; i < scan->count; i++)
458170530Ssam			if (scan->list[i] == c->ic_freq)
459170530Ssam				return 1;
460170530Ssam	}
461170530Ssam	return 0;
462170530Ssam}
463170530Ssam
464176653Ssamstatic void
465178354Ssamsweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
466176653Ssam	const struct scanlist table[])
467176653Ssam{
468178354Ssam	struct ieee80211com *ic = vap->iv_ic;
469176653Ssam	struct ieee80211_channel *c;
470176653Ssam	int i;
471176653Ssam
472176653Ssam	for (i = 0; i < ic->ic_nchans; i++) {
473176653Ssam		if (ss->ss_last >= IEEE80211_SCAN_MAX)
474176653Ssam			break;
475176653Ssam
476176653Ssam		c = &ic->ic_channels[i];
477176653Ssam		/*
478176653Ssam		 * Ignore dynamic turbo channels; we scan them
479176653Ssam		 * in normal mode (i.e. not boosted).  Likewise
480176653Ssam		 * for HT channels, they get scanned using
481176653Ssam		 * legacy rates.
482176653Ssam		 */
483176653Ssam		if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c))
484176653Ssam			continue;
485176653Ssam
486176653Ssam		/*
487176653Ssam		 * If a desired mode was specified, scan only
488176653Ssam		 * channels that satisfy that constraint.
489176653Ssam		 */
490178354Ssam		if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
491178354Ssam		    vap->iv_des_mode != ieee80211_chan2mode(c))
492176653Ssam			continue;
493176653Ssam
494176653Ssam		/*
495176653Ssam		 * Skip channels excluded by user request.
496176653Ssam		 */
497178354Ssam		if (isexcluded(vap, c))
498176653Ssam			continue;
499176653Ssam
500176653Ssam		/*
501176653Ssam		 * Add the channel unless it is listed in the
502176653Ssam		 * fixed scan order tables.  This insures we
503176653Ssam		 * don't sweep back in channels we filtered out
504176653Ssam		 * above.
505176653Ssam		 */
506176653Ssam		if (checktable(table, c))
507176653Ssam			continue;
508176653Ssam
509176653Ssam		/* Add channel to scanning list. */
510176653Ssam		ss->ss_chans[ss->ss_last++] = c;
511176653Ssam	}
512176653Ssam}
513176653Ssam
514178354Ssamstatic void
515178354Ssammakescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
516178354Ssam	const struct scanlist table[])
517170530Ssam{
518170530Ssam	const struct scanlist *scan;
519170530Ssam	enum ieee80211_phymode mode;
520170530Ssam
521170530Ssam	ss->ss_last = 0;
522170530Ssam	/*
523170530Ssam	 * Use the table of ordered channels to construct the list
524170530Ssam	 * of channels for scanning.  Any channels in the ordered
525170530Ssam	 * list not in the master list will be discarded.
526170530Ssam	 */
527178354Ssam	for (scan = table; scan->list != NULL; scan++) {
528170530Ssam		mode = scan->mode;
529178354Ssam		if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
530170530Ssam			/*
531170530Ssam			 * If a desired mode was specified, scan only
532170530Ssam			 * channels that satisfy that constraint.
533170530Ssam			 */
534178354Ssam			if (vap->iv_des_mode != mode) {
535170530Ssam				/*
536170530Ssam				 * The scan table marks 2.4Ghz channels as b
537170530Ssam				 * so if the desired mode is 11g, then use
538170530Ssam				 * the 11b channel list but upgrade the mode.
539170530Ssam				 */
540178354Ssam				if (vap->iv_des_mode != IEEE80211_MODE_11G ||
541170530Ssam				    mode != IEEE80211_MODE_11B)
542170530Ssam					continue;
543170530Ssam				mode = IEEE80211_MODE_11G;	/* upgrade */
544170530Ssam			}
545170530Ssam		} else {
546170530Ssam			/*
547170530Ssam			 * This lets add_channels upgrade an 11b channel
548170530Ssam			 * to 11g if available.
549170530Ssam			 */
550170530Ssam			if (mode == IEEE80211_MODE_11B)
551170530Ssam				mode = IEEE80211_MODE_AUTO;
552170530Ssam		}
553170530Ssam#ifdef IEEE80211_F_XR
554170530Ssam		/* XR does not operate on turbo channels */
555178354Ssam		if ((vap->iv_flags & IEEE80211_F_XR) &&
556170530Ssam		    (mode == IEEE80211_MODE_TURBO_A ||
557170530Ssam		     mode == IEEE80211_MODE_TURBO_G ||
558170530Ssam		     mode == IEEE80211_MODE_STURBO_A))
559170530Ssam			continue;
560170530Ssam#endif
561170530Ssam		/*
562170530Ssam		 * Add the list of the channels; any that are not
563170530Ssam		 * in the master channel list will be discarded.
564170530Ssam		 */
565178354Ssam		add_channels(vap, ss, mode, scan->list, scan->count);
566170530Ssam	}
567170530Ssam
568170530Ssam	/*
569178354Ssam	 * Add the channels from the ic that are not present
570178354Ssam	 * in the table.
571170530Ssam	 */
572178354Ssam	sweepchannels(ss, vap, table);
573178354Ssam}
574170530Ssam
575178354Ssamstatic const uint16_t rcl1[] =		/* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
576178354Ssam{ 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
577178354Ssamstatic const uint16_t rcl2[] =		/* 4 MKK channels: 34, 38, 42, 46 */
578178354Ssam{ 5170, 5190, 5210, 5230 };
579178354Ssamstatic const uint16_t rcl3[] =		/* 2.4Ghz ch: 1,6,11,7,13 */
580178354Ssam{ 2412, 2437, 2462, 2442, 2472 };
581178354Ssamstatic const uint16_t rcl4[] =		/* 5 FCC channel: 149, 153, 161, 165 */
582178354Ssam{ 5745, 5765, 5785, 5805, 5825 };
583178354Ssamstatic const uint16_t rcl7[] =		/* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
584178354Ssam{ 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
585178354Ssamstatic const uint16_t rcl8[] =		/* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
586178354Ssam{ 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
587178354Ssamstatic const uint16_t rcl9[] =		/* 2.4Ghz ch: 14 */
588178354Ssam{ 2484 };
589178354Ssamstatic const uint16_t rcl10[] =	/* Added Korean channels 2312-2372 */
590178354Ssam{ 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
591178354Ssamstatic const uint16_t rcl11[] =	/* Added Japan channels in 4.9/5.0 spectrum */
592178354Ssam{ 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
593178354Ssam#ifdef ATH_TURBO_SCAN
594178354Ssamstatic const uint16_t rcl5[] =		/* 3 static turbo channels */
595178354Ssam{ 5210, 5250, 5290 };
596178354Ssamstatic const uint16_t rcl6[] =		/* 2 static turbo channels */
597178354Ssam{ 5760, 5800 };
598178354Ssamstatic const uint16_t rcl6x[] =	/* 4 FCC3 turbo channels */
599178354Ssam{ 5540, 5580, 5620, 5660 };
600178354Ssamstatic const uint16_t rcl12[] =	/* 2.4Ghz Turbo channel 6 */
601178354Ssam{ 2437 };
602178354Ssamstatic const uint16_t rcl13[] =	/* dynamic Turbo channels */
603178354Ssam{ 5200, 5240, 5280, 5765, 5805 };
604178354Ssam#endif /* ATH_TURBO_SCAN */
605170530Ssam
606178354Ssam#define	X(a)	.count = sizeof(a)/sizeof(a[0]), .list = a
607170530Ssam
608178354Ssamstatic const struct scanlist staScanTable[] = {
609178354Ssam	{ IEEE80211_MODE_11B,   	X(rcl3) },
610178354Ssam	{ IEEE80211_MODE_11A,   	X(rcl1) },
611178354Ssam	{ IEEE80211_MODE_11A,   	X(rcl2) },
612178354Ssam	{ IEEE80211_MODE_11B,   	X(rcl8) },
613178354Ssam	{ IEEE80211_MODE_11B,   	X(rcl9) },
614178354Ssam	{ IEEE80211_MODE_11A,   	X(rcl4) },
615178354Ssam#ifdef ATH_TURBO_SCAN
616178354Ssam	{ IEEE80211_MODE_STURBO_A,	X(rcl5) },
617178354Ssam	{ IEEE80211_MODE_STURBO_A,	X(rcl6) },
618178354Ssam	{ IEEE80211_MODE_TURBO_A,	X(rcl6x) },
619178354Ssam	{ IEEE80211_MODE_TURBO_A,	X(rcl13) },
620178354Ssam#endif /* ATH_TURBO_SCAN */
621178354Ssam	{ IEEE80211_MODE_11A,		X(rcl7) },
622178354Ssam	{ IEEE80211_MODE_11B,		X(rcl10) },
623178354Ssam	{ IEEE80211_MODE_11A,		X(rcl11) },
624178354Ssam#ifdef ATH_TURBO_SCAN
625178354Ssam	{ IEEE80211_MODE_TURBO_G,	X(rcl12) },
626178354Ssam#endif /* ATH_TURBO_SCAN */
627178354Ssam	{ .list = NULL }
628178354Ssam};
629178354Ssam
630178354Ssam/*
631178354Ssam * Start a station-mode scan by populating the channel list.
632178354Ssam */
633178354Ssamstatic int
634178354Ssamsta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
635178354Ssam{
636178354Ssam	struct sta_table *st = ss->ss_priv;
637178354Ssam
638178354Ssam	makescanlist(ss, vap, staScanTable);
639178354Ssam
640178354Ssam	if (ss->ss_mindwell == 0)
641178354Ssam		ss->ss_mindwell = msecs_to_ticks(20);	/* 20ms */
642178354Ssam	if (ss->ss_maxdwell == 0)
643178354Ssam		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
644178354Ssam
645178354Ssam	st->st_scangen++;
646170530Ssam	st->st_newscan = 1;
647170530Ssam
648170530Ssam	return 0;
649170530Ssam}
650170530Ssam
651170530Ssam/*
652178354Ssam * Restart a scan, typically a bg scan but can
653178354Ssam * also be a fg scan that came up empty.
654170530Ssam */
655170530Ssamstatic int
656178354Ssamsta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
657170530Ssam{
658170530Ssam	struct sta_table *st = ss->ss_priv;
659170530Ssam
660170530Ssam	st->st_newscan = 1;
661170530Ssam	return 0;
662170530Ssam}
663170530Ssam
664170530Ssam/*
665170530Ssam * Cancel an ongoing scan.
666170530Ssam */
667170530Ssamstatic int
668178354Ssamsta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
669170530Ssam{
670170530Ssam	return 0;
671170530Ssam}
672170530Ssam
673178354Ssam/* unalligned little endian access */
674178354Ssam#define LE_READ_2(p)					\
675178354Ssam	((uint16_t)					\
676178354Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
677178354Ssam	  (((const uint8_t *)(p))[1] <<  8)))
678178354Ssam
679178354Ssamstatic int
680170530Ssammaxrate(const struct ieee80211_scan_entry *se)
681170530Ssam{
682178354Ssam	const struct ieee80211_ie_htcap *htcap =
683178354Ssam	    (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie;
684178354Ssam	int rmax, r, i;
685178354Ssam	uint16_t caps;
686170530Ssam
687170530Ssam	rmax = 0;
688178354Ssam	if (htcap != NULL) {
689178354Ssam		/*
690178354Ssam		 * HT station; inspect supported MCS and then adjust
691178354Ssam		 * rate by channel width.  Could also include short GI
692178354Ssam		 * in this if we want to be extra accurate.
693178354Ssam		 */
694178354Ssam		/* XXX assumes MCS15 is max */
695178354Ssam		for (i = 15; i >= 0 && isclr(htcap->hc_mcsset, i); i--)
696178354Ssam			;
697178354Ssam		if (i >= 0) {
698178354Ssam			caps = LE_READ_2(&htcap->hc_cap);
699178354Ssam			/* XXX short/long GI */
700178354Ssam			if (caps & IEEE80211_HTCAP_CHWIDTH40)
701178354Ssam				rmax = ieee80211_htrates[i].ht40_rate_400ns;
702178354Ssam			else
703178354Ssam				rmax = ieee80211_htrates[i].ht40_rate_800ns;
704178354Ssam		}
705178354Ssam	}
706170530Ssam	for (i = 0; i < se->se_rates[1]; i++) {
707170530Ssam		r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
708170530Ssam		if (r > rmax)
709170530Ssam			rmax = r;
710170530Ssam	}
711170530Ssam	for (i = 0; i < se->se_xrates[1]; i++) {
712170530Ssam		r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
713170530Ssam		if (r > rmax)
714170530Ssam			rmax = r;
715170530Ssam	}
716170530Ssam	return rmax;
717170530Ssam}
718170530Ssam
719170530Ssam/*
720170530Ssam * Compare the capabilities of two entries and decide which is
721170530Ssam * more desirable (return >0 if a is considered better).  Note
722170530Ssam * that we assume compatibility/usability has already been checked
723170530Ssam * so we don't need to (e.g. validate whether privacy is supported).
724170530Ssam * Used to select the best scan candidate for association in a BSS.
725170530Ssam */
726170530Ssamstatic int
727170530Ssamsta_compare(const struct sta_entry *a, const struct sta_entry *b)
728170530Ssam{
729170530Ssam#define	PREFER(_a,_b,_what) do {			\
730170530Ssam	if (((_a) ^ (_b)) & (_what))			\
731170530Ssam		return ((_a) & (_what)) ? 1 : -1;	\
732170530Ssam} while (0)
733178354Ssam	int maxa, maxb;
734170530Ssam	int8_t rssia, rssib;
735170530Ssam	int weight;
736170530Ssam
737170530Ssam	/* privacy support */
738170530Ssam	PREFER(a->base.se_capinfo, b->base.se_capinfo,
739170530Ssam		IEEE80211_CAPINFO_PRIVACY);
740170530Ssam
741170530Ssam	/* compare count of previous failures */
742170530Ssam	weight = b->se_fails - a->se_fails;
743170530Ssam	if (abs(weight) > 1)
744170530Ssam		return weight;
745170530Ssam
746170530Ssam	/*
747170530Ssam	 * Compare rssi.  If the two are considered equivalent
748170530Ssam	 * then fallback to other criteria.  We threshold the
749170530Ssam	 * comparisons to avoid selecting an ap purely by rssi
750170530Ssam	 * when both values may be good but one ap is otherwise
751170530Ssam	 * more desirable (e.g. an 11b-only ap with stronger
752170530Ssam	 * signal than an 11g ap).
753170530Ssam	 */
754170530Ssam	rssia = MIN(a->base.se_rssi, STA_RSSI_MAX);
755170530Ssam	rssib = MIN(b->base.se_rssi, STA_RSSI_MAX);
756170530Ssam	if (abs(rssib - rssia) < 5) {
757170530Ssam		/* best/max rate preferred if signal level close enough XXX */
758170530Ssam		maxa = maxrate(&a->base);
759170530Ssam		maxb = maxrate(&b->base);
760170530Ssam		if (maxa != maxb)
761170530Ssam			return maxa - maxb;
762170530Ssam		/* XXX use freq for channel preference */
763170530Ssam		/* for now just prefer 5Ghz band to all other bands */
764178354Ssam		PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan),
765178354Ssam		       IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1);
766170530Ssam	}
767170530Ssam	/* all things being equal, use signal level */
768170530Ssam	return a->base.se_rssi - b->base.se_rssi;
769170530Ssam#undef PREFER
770170530Ssam}
771170530Ssam
772170530Ssam/*
773170530Ssam * Check rate set suitability and return the best supported rate.
774178354Ssam * XXX inspect MCS for HT
775170530Ssam */
776170530Ssamstatic int
777178354Ssamcheck_rate(struct ieee80211vap *vap, const struct ieee80211_scan_entry *se)
778170530Ssam{
779170530Ssam#define	RV(v)	((v) & IEEE80211_RATE_VAL)
780170530Ssam	const struct ieee80211_rateset *srs;
781178354Ssam	int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate;
782170530Ssam	const uint8_t *rs;
783170530Ssam
784178354Ssam	okrate = badrate = 0;
785170530Ssam
786178354Ssam	srs = ieee80211_get_suprates(vap->iv_ic, se->se_chan);
787170530Ssam	nrs = se->se_rates[1];
788170530Ssam	rs = se->se_rates+2;
789178354Ssam	/* XXX MCS */
790178354Ssam	ucastrate = vap->iv_txparms[ieee80211_chan2mode(se->se_chan)].ucastrate;
791170530Ssam	fixedrate = IEEE80211_FIXED_RATE_NONE;
792170530Ssamagain:
793170530Ssam	for (i = 0; i < nrs; i++) {
794170530Ssam		r = RV(rs[i]);
795170530Ssam		badrate = r;
796170530Ssam		/*
797170530Ssam		 * Check any fixed rate is included.
798170530Ssam		 */
799178354Ssam		if (r == ucastrate)
800170530Ssam			fixedrate = r;
801170530Ssam		/*
802170530Ssam		 * Check against our supported rates.
803170530Ssam		 */
804170530Ssam		for (j = 0; j < srs->rs_nrates; j++)
805170530Ssam			if (r == RV(srs->rs_rates[j])) {
806170530Ssam				if (r > okrate)		/* NB: track max */
807170530Ssam					okrate = r;
808170530Ssam				break;
809170530Ssam			}
810170530Ssam
811170530Ssam		if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) {
812170530Ssam			/*
813170530Ssam			 * Don't try joining a BSS, if we don't support
814170530Ssam			 * one of its basic rates.
815170530Ssam			 */
816170530Ssam			okrate = 0;
817170530Ssam			goto back;
818170530Ssam		}
819170530Ssam	}
820170530Ssam	if (rs == se->se_rates+2) {
821170530Ssam		/* scan xrates too; sort of an algol68-style for loop */
822170530Ssam		nrs = se->se_xrates[1];
823170530Ssam		rs = se->se_xrates+2;
824170530Ssam		goto again;
825170530Ssam	}
826170530Ssam
827170530Ssamback:
828178354Ssam	if (okrate == 0 || ucastrate != fixedrate)
829170530Ssam		return badrate | IEEE80211_RATE_BASIC;
830170530Ssam	else
831170530Ssam		return RV(okrate);
832170530Ssam#undef RV
833170530Ssam}
834170530Ssam
835170530Ssamstatic int
836170530Ssammatch_ssid(const uint8_t *ie,
837170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
838170530Ssam{
839170530Ssam	int i;
840170530Ssam
841170530Ssam	for (i = 0; i < nssid; i++) {
842170530Ssam		if (ie[1] == ssids[i].len &&
843170530Ssam		     memcmp(ie+2, ssids[i].ssid, ie[1]) == 0)
844170530Ssam			return 1;
845170530Ssam	}
846170530Ssam	return 0;
847170530Ssam}
848170530Ssam
849170530Ssam/*
850170530Ssam * Test a scan candidate for suitability/compatibility.
851170530Ssam */
852170530Ssamstatic int
853178354Ssammatch_bss(struct ieee80211vap *vap,
854170530Ssam	const struct ieee80211_scan_state *ss, struct sta_entry *se0,
855170530Ssam	int debug)
856170530Ssam{
857178354Ssam	struct ieee80211com *ic = vap->iv_ic;
858170530Ssam	struct ieee80211_scan_entry *se = &se0->base;
859178354Ssam        uint8_t rate;
860178354Ssam        int fail;
861170530Ssam
862170530Ssam	fail = 0;
863170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
864171409Ssam		fail |= MATCH_CHANNEL;
865170530Ssam	/*
866170530Ssam	 * NB: normally the desired mode is used to construct
867170530Ssam	 * the channel list, but it's possible for the scan
868170530Ssam	 * cache to include entries for stations outside this
869170530Ssam	 * list so we check the desired mode here to weed them
870170530Ssam	 * out.
871170530Ssam	 */
872178354Ssam	if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
873170530Ssam	    (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
874178354Ssam	    chanflags[vap->iv_des_mode])
875171409Ssam		fail |= MATCH_CHANNEL;
876178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
877170530Ssam		if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
878171409Ssam			fail |= MATCH_CAPINFO;
879170530Ssam	} else {
880170530Ssam		if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
881171409Ssam			fail |= MATCH_CAPINFO;
882178354Ssam		/*
883178354Ssam		 * If 11d is enabled and we're attempting to join a bss
884178354Ssam		 * that advertises it's country code then compare our
885178354Ssam		 * current settings to what we fetched from the country ie.
886178354Ssam		 * If our country code is unspecified or different then do
887178354Ssam		 * not attempt to join the bss.  We should have already
888178354Ssam		 * dispatched an event to user space that identifies the
889178354Ssam		 * new country code so our regdomain config should match.
890178354Ssam		 */
891178354Ssam		if ((IEEE80211_IS_CHAN_11D(se->se_chan) ||
892178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
893178354Ssam		    se->se_cc[0] != 0 &&
894178354Ssam		    (ic->ic_regdomain.country == CTRY_DEFAULT ||
895178354Ssam		     !isocmp(se->se_cc, ic->ic_regdomain.isocc)))
896178354Ssam			fail |= MATCH_CC;
897170530Ssam	}
898178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
899170530Ssam		if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
900171409Ssam			fail |= MATCH_PRIVACY;
901170530Ssam	} else {
902170530Ssam		/* XXX does this mean privacy is supported or required? */
903170530Ssam		if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
904171409Ssam			fail |= MATCH_PRIVACY;
905170530Ssam	}
906178354Ssam	rate = check_rate(vap, se);
907170530Ssam	if (rate & IEEE80211_RATE_BASIC)
908171409Ssam		fail |= MATCH_RATE;
909170530Ssam	if (ss->ss_nssid != 0 &&
910171409Ssam	    !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
911171409Ssam		fail |= MATCH_SSID;
912178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
913178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
914178354Ssam		fail |= MATCH_BSSID;
915170530Ssam	if (se0->se_fails >= STA_FAILS_MAX)
916171409Ssam		fail |= MATCH_FAILS;
917170530Ssam	if (se0->se_notseen >= STA_PURGE_SCANS)
918171409Ssam		fail |= MATCH_NOTSEEN;
919170530Ssam	if (se->se_rssi < STA_RSSI_MIN)
920171409Ssam		fail |= MATCH_RSSI;
921170530Ssam#ifdef IEEE80211_DEBUG
922178354Ssam	if (ieee80211_msg(vap, debug)) {
923170530Ssam		printf(" %c %s",
924171409Ssam		    fail & MATCH_FAILS ? '=' :
925171409Ssam		    fail & MATCH_NOTSEEN ? '^' :
926178354Ssam		    fail & MATCH_CC ? '$' :
927171409Ssam		    fail ? '-' : '+', ether_sprintf(se->se_macaddr));
928170530Ssam		printf(" %s%c", ether_sprintf(se->se_bssid),
929171409Ssam		    fail & MATCH_BSSID ? '!' : ' ');
930170530Ssam		printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
931171409Ssam			fail & MATCH_CHANNEL ? '!' : ' ');
932171409Ssam		printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' ');
933170530Ssam		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
934171409Ssam		    fail & MATCH_RATE ? '!' : ' ');
935170530Ssam		printf(" %4s%c",
936170530Ssam		    (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
937170530Ssam		    (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
938170530Ssam		    "????",
939171409Ssam		    fail & MATCH_CAPINFO ? '!' : ' ');
940170530Ssam		printf(" %3s%c ",
941170530Ssam		    (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
942170530Ssam		    "wep" : "no",
943171409Ssam		    fail & MATCH_PRIVACY ? '!' : ' ');
944170530Ssam		ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]);
945171409Ssam		printf("%s\n", fail & MATCH_SSID ? "!" : "");
946170530Ssam	}
947170530Ssam#endif
948170530Ssam	return fail;
949170530Ssam}
950170530Ssam
951170530Ssamstatic void
952170530Ssamsta_update_notseen(struct sta_table *st)
953170530Ssam{
954170530Ssam	struct sta_entry *se;
955170530Ssam
956170530Ssam	mtx_lock(&st->st_lock);
957170530Ssam	TAILQ_FOREACH(se, &st->st_entry, se_list) {
958170530Ssam		/*
959170530Ssam		 * If seen the reset and don't bump the count;
960170530Ssam		 * otherwise bump the ``not seen'' count.  Note
961170530Ssam		 * that this insures that stations for which we
962170530Ssam		 * see frames while not scanning but not during
963170530Ssam		 * this scan will not be penalized.
964170530Ssam		 */
965170530Ssam		if (se->se_seen)
966170530Ssam			se->se_seen = 0;
967170530Ssam		else
968170530Ssam			se->se_notseen++;
969170530Ssam	}
970170530Ssam	mtx_unlock(&st->st_lock);
971170530Ssam}
972170530Ssam
973170530Ssamstatic void
974170530Ssamsta_dec_fails(struct sta_table *st)
975170530Ssam{
976170530Ssam	struct sta_entry *se;
977170530Ssam
978170530Ssam	mtx_lock(&st->st_lock);
979170530Ssam	TAILQ_FOREACH(se, &st->st_entry, se_list)
980170530Ssam		if (se->se_fails)
981170530Ssam			se->se_fails--;
982170530Ssam	mtx_unlock(&st->st_lock);
983170530Ssam}
984170530Ssam
985170530Ssamstatic struct sta_entry *
986178354Ssamselect_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug)
987170530Ssam{
988170530Ssam	struct sta_table *st = ss->ss_priv;
989170530Ssam	struct sta_entry *se, *selbs = NULL;
990170530Ssam
991178354Ssam	IEEE80211_DPRINTF(vap, debug, " %s\n",
992170530Ssam	    "macaddr          bssid         chan  rssi  rate flag  wep  essid");
993170530Ssam	mtx_lock(&st->st_lock);
994170530Ssam	TAILQ_FOREACH(se, &st->st_entry, se_list) {
995178354Ssam		if (match_bss(vap, ss, se, debug) == 0) {
996178354Ssam			ieee80211_ies_expand(&se->base.se_ies);
997170530Ssam			if (selbs == NULL)
998170530Ssam				selbs = se;
999170530Ssam			else if (sta_compare(se, selbs) > 0)
1000170530Ssam				selbs = se;
1001170530Ssam		}
1002170530Ssam	}
1003170530Ssam	mtx_unlock(&st->st_lock);
1004170530Ssam
1005170530Ssam	return selbs;
1006170530Ssam}
1007170530Ssam
1008170530Ssam/*
1009170530Ssam * Pick an ap or ibss network to join or find a channel
1010170530Ssam * to use to start an ibss network.
1011170530Ssam */
1012170530Ssamstatic int
1013178354Ssamsta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1014170530Ssam{
1015170530Ssam	struct sta_table *st = ss->ss_priv;
1016170530Ssam	struct sta_entry *selbs;
1017184274Ssam	struct ieee80211_channel *chan;
1018170530Ssam
1019178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1020178354Ssam		("wrong mode %u", vap->iv_opmode));
1021170530Ssam
1022170530Ssam	if (st->st_newscan) {
1023170530Ssam		sta_update_notseen(st);
1024170530Ssam		st->st_newscan = 0;
1025170530Ssam	}
1026170530Ssam	if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1027170530Ssam		/*
1028170530Ssam		 * Manual/background scan, don't select+join the
1029170530Ssam		 * bss, just return.  The scanning framework will
1030170530Ssam		 * handle notification that this has completed.
1031170530Ssam		 */
1032170530Ssam		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1033170530Ssam		return 1;
1034170530Ssam	}
1035170530Ssam	/*
1036170530Ssam	 * Automatic sequencing; look for a candidate and
1037170530Ssam	 * if found join the network.
1038170530Ssam	 */
1039170530Ssam	/* NB: unlocked read should be ok */
1040170530Ssam	if (TAILQ_FIRST(&st->st_entry) == NULL) {
1041178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1042170530Ssam			"%s: no scan candidate\n", __func__);
1043178354Ssam		if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1044178354Ssam			return 0;
1045170530Ssamnotfound:
1046170530Ssam		/*
1047170530Ssam		 * If nothing suitable was found decrement
1048170530Ssam		 * the failure counts so entries will be
1049170530Ssam		 * reconsidered the next time around.  We
1050170530Ssam		 * really want to do this only for sta's
1051170530Ssam		 * where we've previously had some success.
1052170530Ssam		 */
1053170530Ssam		sta_dec_fails(st);
1054170530Ssam		st->st_newscan = 1;
1055170530Ssam		return 0;			/* restart scan */
1056170530Ssam	}
1057178354Ssam	selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1058178354Ssam	if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1059178354Ssam		return (selbs != NULL);
1060184274Ssam	if (selbs == NULL)
1061170530Ssam		goto notfound;
1062184274Ssam	chan = selbs->base.se_chan;
1063184274Ssam	if (!ieee80211_sta_join(vap, chan, &selbs->base))
1064184274Ssam		goto notfound;
1065170530Ssam	return 1;				/* terminate scan */
1066170530Ssam}
1067170530Ssam
1068170530Ssam/*
1069170530Ssam * Lookup an entry in the scan cache.  We assume we're
1070170530Ssam * called from the bottom half or such that we don't need
1071170530Ssam * to block the bottom half so that it's safe to return
1072170530Ssam * a reference to an entry w/o holding the lock on the table.
1073170530Ssam */
1074170530Ssamstatic struct sta_entry *
1075170530Ssamsta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1076170530Ssam{
1077170530Ssam	struct sta_entry *se;
1078170530Ssam	int hash = STA_HASH(macaddr);
1079170530Ssam
1080170530Ssam	mtx_lock(&st->st_lock);
1081170530Ssam	LIST_FOREACH(se, &st->st_hash[hash], se_hash)
1082170530Ssam		if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
1083170530Ssam			break;
1084170530Ssam	mtx_unlock(&st->st_lock);
1085170530Ssam
1086170530Ssam	return se;		/* NB: unlocked */
1087170530Ssam}
1088170530Ssam
1089170530Ssamstatic void
1090178354Ssamsta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1091170530Ssam{
1092178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1093178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
1094170530Ssam	struct sta_table *st = ss->ss_priv;
1095178354Ssam	enum ieee80211_phymode mode;
1096170530Ssam	struct sta_entry *se, *selbs;
1097178354Ssam	uint8_t roamRate, curRate, ucastRate;
1098170530Ssam	int8_t roamRssi, curRssi;
1099170530Ssam
1100170530Ssam	se = sta_lookup(st, ni->ni_macaddr);
1101170530Ssam	if (se == NULL) {
1102170530Ssam		/* XXX something is wrong */
1103170530Ssam		return;
1104170530Ssam	}
1105170530Ssam
1106178354Ssam	mode = ieee80211_chan2mode(ic->ic_bsschan);
1107178354Ssam	roamRate = vap->iv_roamparms[mode].rate;
1108178354Ssam	roamRssi = vap->iv_roamparms[mode].rssi;
1109178354Ssam	ucastRate = vap->iv_txparms[mode].ucastrate;
1110170530Ssam	/* NB: the most up to date rssi is in the node, not the scan cache */
1111170530Ssam	curRssi = ic->ic_node_getrssi(ni);
1112178354Ssam	if (ucastRate == IEEE80211_FIXED_RATE_NONE) {
1113178354Ssam		curRate = ni->ni_txrate;
1114178354Ssam		roamRate &= IEEE80211_RATE_VAL;
1115178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1116170530Ssam		    "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
1117170530Ssam		    __func__, curRssi, curRate, roamRssi, roamRate);
1118170530Ssam	} else {
1119170530Ssam		curRate = roamRate;	/* NB: insure compare below fails */
1120178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1121170530Ssam		    "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi);
1122170530Ssam	}
1123170530Ssam	/*
1124170530Ssam	 * Check if a new ap should be used and switch.
1125170530Ssam	 * XXX deauth current ap
1126170530Ssam	 */
1127170530Ssam	if (curRate < roamRate || curRssi < roamRssi) {
1128178354Ssam		if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
1129170530Ssam			/*
1130170530Ssam			 * Scan cache contents are too old; force a scan now
1131170530Ssam			 * if possible so we have current state to make a
1132170530Ssam			 * decision with.  We don't kick off a bg scan if
1133170530Ssam			 * we're using dynamic turbo and boosted or if the
1134170530Ssam			 * channel is busy.
1135170530Ssam			 * XXX force immediate switch on scan complete
1136170530Ssam			 */
1137170530Ssam			if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1138178354Ssam			    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle))
1139178354Ssam				ieee80211_bg_scan(vap, 0);
1140170530Ssam			return;
1141170530Ssam		}
1142170530Ssam		se->base.se_rssi = curRssi;
1143178354Ssam		selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM);
1144170530Ssam		if (selbs != NULL && selbs != se) {
1145184274Ssam			struct ieee80211_channel *chan;
1146184274Ssam
1147178354Ssam			IEEE80211_DPRINTF(vap,
1148170530Ssam			    IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG,
1149170530Ssam			    "%s: ROAM: curRate %u, roamRate %u, "
1150170530Ssam			    "curRssi %d, roamRssi %d\n", __func__,
1151170530Ssam			    curRate, roamRate, curRssi, roamRssi);
1152184274Ssam
1153184274Ssam			chan = selbs->base.se_chan;
1154184274Ssam			(void) ieee80211_sta_join(vap, chan, &selbs->base);
1155170530Ssam		}
1156170530Ssam	}
1157170530Ssam}
1158170530Ssam
1159170530Ssam/*
1160170530Ssam * Age entries in the scan cache.
1161170530Ssam * XXX also do roaming since it's convenient
1162170530Ssam */
1163170530Ssamstatic void
1164170530Ssamsta_age(struct ieee80211_scan_state *ss)
1165170530Ssam{
1166178354Ssam	struct ieee80211vap *vap = ss->ss_vap;
1167170530Ssam
1168178354Ssam	adhoc_age(ss);
1169170530Ssam	/*
1170170530Ssam	 * If rate control is enabled check periodically to see if
1171170530Ssam	 * we should roam from our current connection to one that
1172170530Ssam	 * might be better.  This only applies when we're operating
1173170530Ssam	 * in sta mode and automatic roaming is set.
1174170530Ssam	 * XXX defer if busy
1175170530Ssam	 * XXX repeater station
1176170530Ssam	 * XXX do when !bgscan?
1177170530Ssam	 */
1178178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1179178354Ssam		("wrong mode %u", vap->iv_opmode));
1180178354Ssam	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO &&
1181178354Ssam	    (vap->iv_ic->ic_flags & IEEE80211_F_BGSCAN) &&
1182178354Ssam	    vap->iv_state >= IEEE80211_S_RUN)
1183170530Ssam		/* XXX vap is implicit */
1184178354Ssam		sta_roam_check(ss, vap);
1185170530Ssam}
1186170530Ssam
1187170530Ssam/*
1188170530Ssam * Iterate over the entries in the scan cache, invoking
1189170530Ssam * the callback function on each one.
1190170530Ssam */
1191170530Ssamstatic void
1192170530Ssamsta_iterate(struct ieee80211_scan_state *ss,
1193170530Ssam	ieee80211_scan_iter_func *f, void *arg)
1194170530Ssam{
1195170530Ssam	struct sta_table *st = ss->ss_priv;
1196170530Ssam	struct sta_entry *se;
1197170530Ssam	u_int gen;
1198170530Ssam
1199170530Ssam	mtx_lock(&st->st_scanlock);
1200178354Ssam	gen = st->st_scaniter++;
1201170530Ssamrestart:
1202170530Ssam	mtx_lock(&st->st_lock);
1203170530Ssam	TAILQ_FOREACH(se, &st->st_entry, se_list) {
1204170530Ssam		if (se->se_scangen != gen) {
1205170530Ssam			se->se_scangen = gen;
1206170530Ssam			/* update public state */
1207170530Ssam			se->base.se_age = ticks - se->se_lastupdate;
1208170530Ssam			mtx_unlock(&st->st_lock);
1209170530Ssam			(*f)(arg, &se->base);
1210170530Ssam			goto restart;
1211170530Ssam		}
1212170530Ssam	}
1213170530Ssam	mtx_unlock(&st->st_lock);
1214170530Ssam
1215170530Ssam	mtx_unlock(&st->st_scanlock);
1216170530Ssam}
1217170530Ssam
1218170530Ssamstatic void
1219170530Ssamsta_assoc_fail(struct ieee80211_scan_state *ss,
1220170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason)
1221170530Ssam{
1222170530Ssam	struct sta_table *st = ss->ss_priv;
1223170530Ssam	struct sta_entry *se;
1224170530Ssam
1225170530Ssam	se = sta_lookup(st, macaddr);
1226170530Ssam	if (se != NULL) {
1227170530Ssam		se->se_fails++;
1228170530Ssam		se->se_lastfail = ticks;
1229178354Ssam		IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1230170530Ssam		    macaddr, "%s: reason %u fails %u",
1231170530Ssam		    __func__, reason, se->se_fails);
1232170530Ssam	}
1233170530Ssam}
1234170530Ssam
1235170530Ssamstatic void
1236170530Ssamsta_assoc_success(struct ieee80211_scan_state *ss,
1237170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1238170530Ssam{
1239170530Ssam	struct sta_table *st = ss->ss_priv;
1240170530Ssam	struct sta_entry *se;
1241170530Ssam
1242170530Ssam	se = sta_lookup(st, macaddr);
1243170530Ssam	if (se != NULL) {
1244170530Ssam#if 0
1245170530Ssam		se->se_fails = 0;
1246178354Ssam		IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1247170530Ssam		    macaddr, "%s: fails %u",
1248170530Ssam		    __func__, se->se_fails);
1249170530Ssam#endif
1250170530Ssam		se->se_lastassoc = ticks;
1251170530Ssam	}
1252170530Ssam}
1253170530Ssam
1254170530Ssamstatic const struct ieee80211_scanner sta_default = {
1255170530Ssam	.scan_name		= "default",
1256170530Ssam	.scan_attach		= sta_attach,
1257170530Ssam	.scan_detach		= sta_detach,
1258170530Ssam	.scan_start		= sta_start,
1259170530Ssam	.scan_restart		= sta_restart,
1260170530Ssam	.scan_cancel		= sta_cancel,
1261170530Ssam	.scan_end		= sta_pick_bss,
1262170530Ssam	.scan_flush		= sta_flush,
1263170530Ssam	.scan_add		= sta_add,
1264170530Ssam	.scan_age		= sta_age,
1265170530Ssam	.scan_iterate		= sta_iterate,
1266170530Ssam	.scan_assoc_fail	= sta_assoc_fail,
1267170530Ssam	.scan_assoc_success	= sta_assoc_success,
1268170530Ssam};
1269170530Ssam
1270170530Ssam/*
1271170530Ssam * Adhoc mode-specific support.
1272170530Ssam */
1273170530Ssam
1274170530Ssamstatic const uint16_t adhocWorld[] =		/* 36, 40, 44, 48 */
1275170530Ssam{ 5180, 5200, 5220, 5240 };
1276170530Ssamstatic const uint16_t adhocFcc3[] =		/* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */
1277170530Ssam{ 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 };
1278170530Ssamstatic const uint16_t adhocMkk[] =		/* 34, 38, 42, 46 */
1279170530Ssam{ 5170, 5190, 5210, 5230 };
1280170530Ssamstatic const uint16_t adhoc11b[] =		/* 10, 11 */
1281170530Ssam{ 2457, 2462 };
1282170530Ssam
1283170530Ssamstatic const struct scanlist adhocScanTable[] = {
1284170530Ssam	{ IEEE80211_MODE_11B,   	X(adhoc11b) },
1285170530Ssam	{ IEEE80211_MODE_11A,   	X(adhocWorld) },
1286170530Ssam	{ IEEE80211_MODE_11A,   	X(adhocFcc3) },
1287170530Ssam	{ IEEE80211_MODE_11B,   	X(adhocMkk) },
1288170530Ssam	{ .list = NULL }
1289170530Ssam};
1290170530Ssam#undef X
1291170530Ssam
1292170530Ssam/*
1293170530Ssam * Start an adhoc-mode scan by populating the channel list.
1294170530Ssam */
1295170530Ssamstatic int
1296178354Ssamadhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1297170530Ssam{
1298170530Ssam	struct sta_table *st = ss->ss_priv;
1299170530Ssam
1300178354Ssam	makescanlist(ss, vap, adhocScanTable);
1301176653Ssam
1302178354Ssam	if (ss->ss_mindwell == 0)
1303178354Ssam		ss->ss_mindwell = msecs_to_ticks(200);	/* 200ms */
1304178354Ssam	if (ss->ss_maxdwell == 0)
1305178354Ssam		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
1306176653Ssam
1307178354Ssam	st->st_scangen++;
1308170530Ssam	st->st_newscan = 1;
1309170530Ssam
1310170530Ssam	return 0;
1311170530Ssam}
1312170530Ssam
1313170530Ssam/*
1314170530Ssam * Select a channel to start an adhoc network on.
1315170530Ssam * The channel list was populated with appropriate
1316170530Ssam * channels so select one that looks least occupied.
1317170530Ssam */
1318170530Ssamstatic struct ieee80211_channel *
1319178354Ssamadhoc_pick_channel(struct ieee80211_scan_state *ss, int flags)
1320170530Ssam{
1321170530Ssam	struct sta_table *st = ss->ss_priv;
1322170530Ssam	struct sta_entry *se;
1323170530Ssam	struct ieee80211_channel *c, *bestchan;
1324170530Ssam	int i, bestrssi, maxrssi;
1325170530Ssam
1326170530Ssam	bestchan = NULL;
1327170530Ssam	bestrssi = -1;
1328170530Ssam
1329170530Ssam	mtx_lock(&st->st_lock);
1330170530Ssam	for (i = 0; i < ss->ss_last; i++) {
1331170530Ssam		c = ss->ss_chans[i];
1332178354Ssam		/* never consider a channel with radar */
1333178354Ssam		if (IEEE80211_IS_CHAN_RADAR(c))
1334176653Ssam			continue;
1335178354Ssam		/* skip channels disallowed by regulatory settings */
1336178354Ssam		if (IEEE80211_IS_CHAN_NOADHOC(c))
1337178354Ssam			continue;
1338178354Ssam		/* check channel attributes for band compatibility */
1339178354Ssam		if (flags != 0 && (c->ic_flags & flags) != flags)
1340178354Ssam			continue;
1341170530Ssam		maxrssi = 0;
1342170530Ssam		TAILQ_FOREACH(se, &st->st_entry, se_list) {
1343170530Ssam			if (se->base.se_chan != c)
1344170530Ssam				continue;
1345170530Ssam			if (se->base.se_rssi > maxrssi)
1346170530Ssam				maxrssi = se->base.se_rssi;
1347170530Ssam		}
1348170530Ssam		if (bestchan == NULL || maxrssi < bestrssi)
1349170530Ssam			bestchan = c;
1350170530Ssam	}
1351170530Ssam	mtx_unlock(&st->st_lock);
1352170530Ssam
1353170530Ssam	return bestchan;
1354170530Ssam}
1355170530Ssam
1356170530Ssam/*
1357170530Ssam * Pick an ibss network to join or find a channel
1358170530Ssam * to use to start an ibss network.
1359170530Ssam */
1360170530Ssamstatic int
1361178354Ssamadhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1362170530Ssam{
1363170530Ssam	struct sta_table *st = ss->ss_priv;
1364170530Ssam	struct sta_entry *selbs;
1365170530Ssam	struct ieee80211_channel *chan;
1366170530Ssam
1367178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1368178354Ssam		vap->iv_opmode == IEEE80211_M_AHDEMO,
1369178354Ssam		("wrong opmode %u", vap->iv_opmode));
1370170530Ssam
1371170530Ssam	if (st->st_newscan) {
1372170530Ssam		sta_update_notseen(st);
1373170530Ssam		st->st_newscan = 0;
1374170530Ssam	}
1375170530Ssam	if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1376170530Ssam		/*
1377170530Ssam		 * Manual/background scan, don't select+join the
1378170530Ssam		 * bss, just return.  The scanning framework will
1379170530Ssam		 * handle notification that this has completed.
1380170530Ssam		 */
1381170530Ssam		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1382170530Ssam		return 1;
1383170530Ssam	}
1384170530Ssam	/*
1385170530Ssam	 * Automatic sequencing; look for a candidate and
1386170530Ssam	 * if found join the network.
1387170530Ssam	 */
1388170530Ssam	/* NB: unlocked read should be ok */
1389170530Ssam	if (TAILQ_FIRST(&st->st_entry) == NULL) {
1390178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1391170530Ssam			"%s: no scan candidate\n", __func__);
1392178354Ssam		if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1393178354Ssam			return 0;
1394170530Ssamnotfound:
1395178354Ssam		if (vap->iv_des_nssid) {
1396170530Ssam			/*
1397170530Ssam			 * No existing adhoc network to join and we have
1398170530Ssam			 * an ssid; start one up.  If no channel was
1399170530Ssam			 * specified, try to select a channel.
1400170530Ssam			 */
1401178354Ssam			if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1402178354Ssam			    IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1403183421Ssam				struct ieee80211com *ic = vap->iv_ic;
1404183421Ssam
1405183421Ssam				chan = adhoc_pick_channel(ss, 0);
1406183421Ssam				if (chan != NULL)
1407183421Ssam					chan = ieee80211_ht_adjust_channel(ic,
1408183421Ssam					    chan, vap->iv_flags_ext);
1409178354Ssam			} else
1410178354Ssam				chan = vap->iv_des_chan;
1411170530Ssam			if (chan != NULL) {
1412178354Ssam				ieee80211_create_ibss(vap, chan);
1413170530Ssam				return 1;
1414170530Ssam			}
1415170530Ssam		}
1416170530Ssam		/*
1417170530Ssam		 * If nothing suitable was found decrement
1418170530Ssam		 * the failure counts so entries will be
1419170530Ssam		 * reconsidered the next time around.  We
1420170530Ssam		 * really want to do this only for sta's
1421170530Ssam		 * where we've previously had some success.
1422170530Ssam		 */
1423170530Ssam		sta_dec_fails(st);
1424170530Ssam		st->st_newscan = 1;
1425170530Ssam		return 0;			/* restart scan */
1426170530Ssam	}
1427178354Ssam	selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1428178354Ssam	if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1429178354Ssam		return (selbs != NULL);
1430184274Ssam	if (selbs == NULL)
1431170530Ssam		goto notfound;
1432184274Ssam	chan = selbs->base.se_chan;
1433184274Ssam	if (!ieee80211_sta_join(vap, chan, &selbs->base))
1434184274Ssam		goto notfound;
1435170530Ssam	return 1;				/* terminate scan */
1436170530Ssam}
1437170530Ssam
1438170530Ssam/*
1439170530Ssam * Age entries in the scan cache.
1440170530Ssam */
1441170530Ssamstatic void
1442170530Ssamadhoc_age(struct ieee80211_scan_state *ss)
1443170530Ssam{
1444170530Ssam	struct sta_table *st = ss->ss_priv;
1445170530Ssam	struct sta_entry *se, *next;
1446170530Ssam
1447170530Ssam	mtx_lock(&st->st_lock);
1448170530Ssam	TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
1449170530Ssam		if (se->se_notseen > STA_PURGE_SCANS) {
1450170530Ssam			TAILQ_REMOVE(&st->st_entry, se, se_list);
1451170530Ssam			LIST_REMOVE(se, se_hash);
1452178354Ssam			ieee80211_ies_cleanup(&se->base.se_ies);
1453184210Sdes			FREE(se, M_80211_SCAN);
1454170530Ssam		}
1455170530Ssam	}
1456170530Ssam	mtx_unlock(&st->st_lock);
1457170530Ssam}
1458170530Ssam
1459170530Ssamstatic const struct ieee80211_scanner adhoc_default = {
1460170530Ssam	.scan_name		= "default",
1461170530Ssam	.scan_attach		= sta_attach,
1462170530Ssam	.scan_detach		= sta_detach,
1463170530Ssam	.scan_start		= adhoc_start,
1464170530Ssam	.scan_restart		= sta_restart,
1465170530Ssam	.scan_cancel		= sta_cancel,
1466170530Ssam	.scan_end		= adhoc_pick_bss,
1467170530Ssam	.scan_flush		= sta_flush,
1468178354Ssam	.scan_pickchan		= adhoc_pick_channel,
1469170530Ssam	.scan_add		= sta_add,
1470170530Ssam	.scan_age		= adhoc_age,
1471170530Ssam	.scan_iterate		= sta_iterate,
1472170530Ssam	.scan_assoc_fail	= sta_assoc_fail,
1473170530Ssam	.scan_assoc_success	= sta_assoc_success,
1474170530Ssam};
1475170530Ssam
1476178354Ssamstatic void
1477178354Ssamap_force_promisc(struct ieee80211com *ic)
1478178354Ssam{
1479178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1480178354Ssam
1481178354Ssam	IEEE80211_LOCK(ic);
1482178354Ssam	/* set interface into promiscuous mode */
1483178354Ssam	ifp->if_flags |= IFF_PROMISC;
1484178354Ssam	ic->ic_update_promisc(ifp);
1485178354Ssam	IEEE80211_UNLOCK(ic);
1486178354Ssam}
1487178354Ssam
1488178354Ssamstatic void
1489178354Ssamap_reset_promisc(struct ieee80211com *ic)
1490178354Ssam{
1491178354Ssam	IEEE80211_LOCK(ic);
1492178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
1493178354Ssam	IEEE80211_UNLOCK(ic);
1494178354Ssam}
1495178354Ssam
1496178354Ssamstatic int
1497178354Ssamap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1498178354Ssam{
1499178354Ssam	struct sta_table *st = ss->ss_priv;
1500178354Ssam
1501178354Ssam	makescanlist(ss, vap, staScanTable);
1502178354Ssam
1503178354Ssam	if (ss->ss_mindwell == 0)
1504178354Ssam		ss->ss_mindwell = msecs_to_ticks(200);	/* 200ms */
1505178354Ssam	if (ss->ss_maxdwell == 0)
1506178354Ssam		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
1507178354Ssam
1508178354Ssam	st->st_scangen++;
1509178354Ssam	st->st_newscan = 1;
1510178354Ssam
1511178354Ssam	ap_force_promisc(vap->iv_ic);
1512178354Ssam	return 0;
1513178354Ssam}
1514178354Ssam
1515170530Ssam/*
1516178354Ssam * Cancel an ongoing scan.
1517170530Ssam */
1518170530Ssamstatic int
1519178354Ssamap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1520170530Ssam{
1521178354Ssam	ap_reset_promisc(vap->iv_ic);
1522178354Ssam	return 0;
1523178354Ssam}
1524178354Ssam
1525178354Ssam/*
1526178354Ssam * Pick a quiet channel to use for ap operation.
1527178354Ssam */
1528178354Ssamstatic struct ieee80211_channel *
1529178354Ssamap_pick_channel(struct ieee80211_scan_state *ss, int flags)
1530178354Ssam{
1531178354Ssam	struct sta_table *st = ss->ss_priv;
1532178354Ssam	struct ieee80211_channel *bestchan = NULL;
1533178354Ssam	int i;
1534178354Ssam
1535178354Ssam	/* XXX select channel more intelligently, e.g. channel spread, power */
1536178354Ssam	/* NB: use scan list order to preserve channel preference */
1537178354Ssam	for (i = 0; i < ss->ss_last; i++) {
1538178354Ssam		struct ieee80211_channel *chan = ss->ss_chans[i];
1539178354Ssam		/*
1540178354Ssam		 * If the channel is unoccupied the max rssi
1541178354Ssam		 * should be zero; just take it.  Otherwise
1542178354Ssam		 * track the channel with the lowest rssi and
1543178354Ssam		 * use that when all channels appear occupied.
1544178354Ssam		 */
1545178354Ssam		if (IEEE80211_IS_CHAN_RADAR(chan))
1546178354Ssam			continue;
1547178354Ssam		if (IEEE80211_IS_CHAN_NOHOSTAP(chan))
1548178354Ssam			continue;
1549178354Ssam		/* check channel attributes for band compatibility */
1550178354Ssam		if (flags != 0 && (chan->ic_flags & flags) != flags)
1551178354Ssam			continue;
1552178354Ssam		/* XXX channel have interference */
1553178354Ssam		if (st->st_maxrssi[chan->ic_ieee] == 0) {
1554178354Ssam			/* XXX use other considerations */
1555178354Ssam			return chan;
1556170530Ssam		}
1557178354Ssam		if (bestchan == NULL ||
1558178354Ssam		    st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee])
1559178354Ssam			bestchan = chan;
1560178354Ssam	}
1561178354Ssam	return bestchan;
1562178354Ssam}
1563178354Ssam
1564178354Ssam/*
1565178354Ssam * Pick a quiet channel to use for ap operation.
1566178354Ssam */
1567178354Ssamstatic int
1568178354Ssamap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1569178354Ssam{
1570178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1571178354Ssam	struct ieee80211_channel *bestchan;
1572178354Ssam
1573178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
1574178354Ssam		("wrong opmode %u", vap->iv_opmode));
1575178354Ssam	bestchan = ap_pick_channel(ss, 0);
1576178354Ssam	if (bestchan == NULL) {
1577178354Ssam		/* no suitable channel, should not happen */
1578178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1579178354Ssam		    "%s: no suitable channel! (should not happen)\n", __func__);
1580178354Ssam		/* XXX print something? */
1581178354Ssam		return 0;			/* restart scan */
1582178354Ssam	}
1583178354Ssam	/*
1584178354Ssam	 * If this is a dynamic turbo channel, start with the unboosted one.
1585178354Ssam	 */
1586178354Ssam	if (IEEE80211_IS_CHAN_TURBO(bestchan)) {
1587178354Ssam		bestchan = ieee80211_find_channel(ic, bestchan->ic_freq,
1588178354Ssam			bestchan->ic_flags & ~IEEE80211_CHAN_TURBO);
1589178354Ssam		if (bestchan == NULL) {
1590178354Ssam			/* should never happen ?? */
1591178354Ssam			return 0;
1592170530Ssam		}
1593170530Ssam	}
1594178354Ssam	ap_reset_promisc(ic);
1595178354Ssam	if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) {
1596178354Ssam		/*
1597178354Ssam		 * Manual/background scan, don't select+join the
1598178354Ssam		 * bss, just return.  The scanning framework will
1599178354Ssam		 * handle notification that this has completed.
1600178354Ssam		 */
1601178354Ssam		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1602178354Ssam		return 1;
1603178354Ssam	}
1604178354Ssam	ieee80211_create_ibss(vap,
1605178354Ssam	    ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ext));
1606178354Ssam	return 1;
1607170530Ssam}
1608170530Ssam
1609178354Ssamstatic const struct ieee80211_scanner ap_default = {
1610178354Ssam	.scan_name		= "default",
1611178354Ssam	.scan_attach		= sta_attach,
1612178354Ssam	.scan_detach		= sta_detach,
1613178354Ssam	.scan_start		= ap_start,
1614178354Ssam	.scan_restart		= sta_restart,
1615178354Ssam	.scan_cancel		= ap_cancel,
1616178354Ssam	.scan_end		= ap_end,
1617178354Ssam	.scan_flush		= sta_flush,
1618178354Ssam	.scan_pickchan		= ap_pick_channel,
1619178354Ssam	.scan_add		= sta_add,
1620178354Ssam	.scan_age		= adhoc_age,
1621178354Ssam	.scan_iterate		= sta_iterate,
1622178354Ssam	.scan_assoc_success	= sta_assoc_success,
1623178354Ssam	.scan_assoc_fail	= sta_assoc_fail,
1624170530Ssam};
1625178354Ssam
1626178354Ssam/*
1627178354Ssam * Module glue.
1628178354Ssam */
1629178354SsamIEEE80211_SCANNER_MODULE(sta, 1);
1630178354SsamIEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default);
1631178354SsamIEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default);
1632178354SsamIEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default);
1633178354SsamIEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
1634