ieee80211_scan_sta.c revision 189377
1251652Sgjb/*-
2251652Sgjb * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3251652Sgjb * All rights reserved.
4251652Sgjb *
5251652Sgjb * Redistribution and use in source and binary forms, with or without
6251652Sgjb * modification, are permitted provided that the following conditions
7251652Sgjb * are met:
8251652Sgjb * 1. Redistributions of source code must retain the above copyright
9251652Sgjb *    notice, this list of conditions and the following disclaimer.
10262762Sgjb * 2. Redistributions in binary form must reproduce the above copyright
11251652Sgjb *    notice, this list of conditions and the following disclaimer in the
12251652Sgjb *    documentation and/or other materials provided with the distribution.
13260072Sgjb *
14260072Sgjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15260072Sgjb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16251652Sgjb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17253019Sgjb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18253019Sgjb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19253019Sgjb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20262762Sgjb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21262762Sgjb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22262762Sgjb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23262762Sgjb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24262762Sgjb */
25262762Sgjb
26262762Sgjb#include <sys/cdefs.h>
27251652Sgjb__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_scan_sta.c 189377 2009-03-04 22:05:25Z sam $");
28251652Sgjb
29251652Sgjb/*
30251652Sgjb * IEEE 802.11 station scanning support.
31253019Sgjb */
32253019Sgjb#include "opt_wlan.h"
33251652Sgjb
34251652Sgjb#include <sys/param.h>
35251652Sgjb#include <sys/systm.h>
36251652Sgjb#include <sys/kernel.h>
37251652Sgjb#include <sys/module.h>
38251652Sgjb
39251652Sgjb#include <sys/socket.h>
40251652Sgjb
41251652Sgjb#include <net/if.h>
42251652Sgjb#include <net/if_media.h>
43262794Sgjb#include <net/ethernet.h>
44251652Sgjb
45251652Sgjb#include <net80211/ieee80211_var.h>
46251652Sgjb#include <net80211/ieee80211_input.h>
47251652Sgjb#include <net80211/ieee80211_regdomain.h>
48259530Sgjb#ifdef IEEE80211_SUPPORT_TDMA
49264246Sgjb#include <net80211/ieee80211_tdma.h>
50264141Sgjb#endif
51264141Sgjb
52264141Sgjb#include <net/bpf.h>
53264141Sgjb
54264141Sgjb/*
55264141Sgjb * Parameters for managing cache entries:
56264141Sgjb *
57264141Sgjb * o a station with STA_FAILS_MAX failures is not considered
58264141Sgjb *   when picking a candidate
59264441Sgjb * o a station that hasn't had an update in STA_PURGE_SCANS
60264441Sgjb *   (background) scans is discarded
61264441Sgjb * o after STA_FAILS_AGE seconds we clear the failure count
62264441Sgjb */
63264441Sgjb#define	STA_FAILS_MAX	2		/* assoc failures before ignored */
64264441Sgjb#define	STA_FAILS_AGE	(2*60)		/* time before clearing fails (secs) */
65264441Sgjb#define	STA_PURGE_SCANS	2		/* age for purging entries (scans) */
66264441Sgjb
67264441Sgjb/* XXX tunable */
68264441Sgjb#define	STA_RSSI_MIN	8		/* min acceptable rssi */
69264441Sgjb#define	STA_RSSI_MAX	40		/* max rssi for comparison */
70264441Sgjb
71264141Sgjbstruct sta_entry {
72264141Sgjb	struct ieee80211_scan_entry base;
73264141Sgjb	TAILQ_ENTRY(sta_entry) se_list;
74264141Sgjb	LIST_ENTRY(sta_entry) se_hash;
75	uint8_t		se_fails;		/* failure to associate count */
76	uint8_t		se_seen;		/* seen during current scan */
77	uint8_t		se_notseen;		/* not seen in previous scans */
78	uint8_t		se_flags;
79#define	STA_DEMOTE11B	0x01			/* match w/ demoted 11b chan */
80	uint32_t	se_avgrssi;		/* LPF rssi state */
81	unsigned long	se_lastupdate;		/* time of last update */
82	unsigned long	se_lastfail;		/* time of last failure */
83	unsigned long	se_lastassoc;		/* time of last association */
84	u_int		se_scangen;		/* iterator scan gen# */
85	u_int		se_countrygen;		/* gen# of last cc notify */
86};
87
88#define	STA_HASHSIZE	32
89/* simple hash is enough for variation of macaddr */
90#define	STA_HASH(addr)	\
91	(((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE)
92
93#define	MAX_IEEE_CHAN	256			/* max acceptable IEEE chan # */
94CTASSERT(MAX_IEEE_CHAN >= 256);
95
96struct sta_table {
97	struct mtx	st_lock;		/* on scan table */
98	TAILQ_HEAD(, sta_entry) st_entry;	/* all entries */
99	LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE];
100	struct mtx	st_scanlock;		/* on st_scaniter */
101	u_int		st_scaniter;		/* gen# for iterator */
102	u_int		st_scangen;		/* scan generation # */
103	int		st_newscan;
104	/* ap-related state */
105	int		st_maxrssi[MAX_IEEE_CHAN];
106};
107
108static void sta_flush_table(struct sta_table *);
109/*
110 * match_bss returns a bitmask describing if an entry is suitable
111 * for use.  If non-zero the entry was deemed not suitable and it's
112 * contents explains why.  The following flags are or'd to to this
113 * mask and can be used to figure out why the entry was rejected.
114 */
115#define	MATCH_CHANNEL		0x0001	/* channel mismatch */
116#define	MATCH_CAPINFO		0x0002	/* capabilities mismatch, e.g. no ess */
117#define	MATCH_PRIVACY		0x0004	/* privacy mismatch */
118#define	MATCH_RATE		0x0008	/* rate set mismatch */
119#define	MATCH_SSID		0x0010	/* ssid mismatch */
120#define	MATCH_BSSID		0x0020	/* bssid mismatch */
121#define	MATCH_FAILS		0x0040	/* too many failed auth attempts */
122#define	MATCH_NOTSEEN		0x0080	/* not seen in recent scans */
123#define	MATCH_RSSI		0x0100	/* rssi deemed too low to use */
124#define	MATCH_CC		0x0200	/* country code mismatch */
125#define	MATCH_TDMA_NOIE		0x0400	/* no TDMA ie */
126#define	MATCH_TDMA_NOTMASTER	0x0800	/* not TDMA master */
127#define	MATCH_TDMA_NOSLOT	0x1000	/* all TDMA slots occupied */
128#define	MATCH_TDMA_LOCAL	0x2000	/* local address */
129static int match_bss(struct ieee80211vap *,
130	const struct ieee80211_scan_state *, struct sta_entry *, int);
131static void adhoc_age(struct ieee80211_scan_state *);
132
133static __inline int
134isocmp(const uint8_t cc1[], const uint8_t cc2[])
135{
136     return (cc1[0] == cc2[0] && cc1[1] == cc2[1]);
137}
138
139/* number of references from net80211 layer */
140static	int nrefs = 0;
141
142/*
143 * Attach prior to any scanning work.
144 */
145static int
146sta_attach(struct ieee80211_scan_state *ss)
147{
148	struct sta_table *st;
149
150	st = (struct sta_table *) malloc(sizeof(struct sta_table),
151		M_80211_SCAN, M_NOWAIT | M_ZERO);
152	if (st == NULL)
153		return 0;
154	mtx_init(&st->st_lock, "scantable", "802.11 scan table", MTX_DEF);
155	mtx_init(&st->st_scanlock, "scangen", "802.11 scangen", MTX_DEF);
156	TAILQ_INIT(&st->st_entry);
157	ss->ss_priv = st;
158	nrefs++;			/* NB: we assume caller locking */
159	return 1;
160}
161
162/*
163 * Cleanup any private state.
164 */
165static int
166sta_detach(struct ieee80211_scan_state *ss)
167{
168	struct sta_table *st = ss->ss_priv;
169
170	if (st != NULL) {
171		sta_flush_table(st);
172		mtx_destroy(&st->st_lock);
173		mtx_destroy(&st->st_scanlock);
174		free(st, M_80211_SCAN);
175		KASSERT(nrefs > 0, ("imbalanced attach/detach"));
176		nrefs--;		/* NB: we assume caller locking */
177	}
178	return 1;
179}
180
181/*
182 * Flush all per-scan state.
183 */
184static int
185sta_flush(struct ieee80211_scan_state *ss)
186{
187	struct sta_table *st = ss->ss_priv;
188
189	mtx_lock(&st->st_lock);
190	sta_flush_table(st);
191	mtx_unlock(&st->st_lock);
192	ss->ss_last = 0;
193	return 0;
194}
195
196/*
197 * Flush all entries in the scan cache.
198 */
199static void
200sta_flush_table(struct sta_table *st)
201{
202	struct sta_entry *se, *next;
203
204	TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
205		TAILQ_REMOVE(&st->st_entry, se, se_list);
206		LIST_REMOVE(se, se_hash);
207		ieee80211_ies_cleanup(&se->base.se_ies);
208		free(se, M_80211_SCAN);
209	}
210	memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi));
211}
212
213/*
214 * Process a beacon or probe response frame; create an
215 * entry in the scan cache or update any previous entry.
216 */
217static int
218sta_add(struct ieee80211_scan_state *ss,
219	const struct ieee80211_scanparams *sp,
220	const struct ieee80211_frame *wh,
221	int subtype, int rssi, int noise, int rstamp)
222{
223#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
224#define	PICK1ST(_ss) \
225	((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
226	IEEE80211_SCAN_PICK1ST)
227	struct sta_table *st = ss->ss_priv;
228	const uint8_t *macaddr = wh->i_addr2;
229	struct ieee80211vap *vap = ss->ss_vap;
230	struct ieee80211com *ic = vap->iv_ic;
231	struct sta_entry *se;
232	struct ieee80211_scan_entry *ise;
233	int hash;
234
235	hash = STA_HASH(macaddr);
236
237	mtx_lock(&st->st_lock);
238	LIST_FOREACH(se, &st->st_hash[hash], se_hash)
239		if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
240			goto found;
241	se = (struct sta_entry *) malloc(sizeof(struct sta_entry),
242		M_80211_SCAN, M_NOWAIT | M_ZERO);
243	if (se == NULL) {
244		mtx_unlock(&st->st_lock);
245		return 0;
246	}
247	se->se_scangen = st->st_scaniter-1;
248	se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
249	IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
250	TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
251	LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
252found:
253	ise = &se->base;
254	/* XXX ap beaconing multiple ssid w/ same bssid */
255	if (sp->ssid[1] != 0 &&
256	    (ISPROBE(subtype) || ise->se_ssid[1] == 0))
257		memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]);
258	KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE,
259		("rate set too large: %u", sp->rates[1]));
260	memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]);
261	if (sp->xrates != NULL) {
262		/* XXX validate xrates[1] */
263		KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE,
264			("xrate set too large: %u", sp->xrates[1]));
265		memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]);
266	} else
267		ise->se_xrates[1] = 0;
268	IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
269	if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) {
270		/*
271		 * Record rssi data using extended precision LPF filter.
272		 *
273		 * NB: use only on-channel data to insure we get a good
274		 *     estimate of the signal we'll see when associated.
275		 */
276		IEEE80211_RSSI_LPF(se->se_avgrssi, rssi);
277		ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi);
278		ise->se_noise = noise;
279	}
280	ise->se_rstamp = rstamp;
281	memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
282	ise->se_intval = sp->bintval;
283	ise->se_capinfo = sp->capinfo;
284	/*
285	 * Beware of overriding se_chan for frames seen
286	 * off-channel; this can cause us to attempt an
287	 * association on the wrong channel.
288	 */
289	if (sp->status & IEEE80211_BPARSE_OFFCHAN) {
290		struct ieee80211_channel *c;
291		/*
292		 * Off-channel, locate the home/bss channel for the sta
293		 * using the value broadcast in the DSPARMS ie.  We know
294		 * sp->chan has this value because it's used to calculate
295		 * IEEE80211_BPARSE_OFFCHAN.
296		 */
297		c = ieee80211_find_channel_byieee(ic, sp->chan,
298		    ic->ic_curchan->ic_flags);
299		if (c != NULL) {
300			ise->se_chan = c;
301		} else if (ise->se_chan == NULL) {
302			/* should not happen, pick something */
303			ise->se_chan = ic->ic_curchan;
304		}
305	} else
306		ise->se_chan = ic->ic_curchan;
307	ise->se_fhdwell = sp->fhdwell;
308	ise->se_fhindex = sp->fhindex;
309	ise->se_erp = sp->erp;
310	ise->se_timoff = sp->timoff;
311	if (sp->tim != NULL) {
312		const struct ieee80211_tim_ie *tim =
313		    (const struct ieee80211_tim_ie *) sp->tim;
314		ise->se_dtimperiod = tim->tim_period;
315	}
316	if (sp->country != NULL) {
317		const struct ieee80211_country_ie *cie =
318		    (const struct ieee80211_country_ie *) sp->country;
319		/*
320		 * If 11d is enabled and we're attempting to join a bss
321		 * that advertises it's country code then compare our
322		 * current settings to what we fetched from the country ie.
323		 * If our country code is unspecified or different then
324		 * dispatch an event to user space that identifies the
325		 * country code so our regdomain config can be changed.
326		 */
327		/* XXX only for STA mode? */
328		if ((IEEE80211_IS_CHAN_11D(ise->se_chan) ||
329		    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
330		    (ic->ic_regdomain.country == CTRY_DEFAULT ||
331		     !isocmp(cie->cc, ic->ic_regdomain.isocc))) {
332			/* only issue one notify event per scan */
333			if (se->se_countrygen != st->st_scangen) {
334				ieee80211_notify_country(vap, ise->se_bssid,
335				    cie->cc);
336				se->se_countrygen = st->st_scangen;
337			}
338		}
339		ise->se_cc[0] = cie->cc[0];
340		ise->se_cc[1] = cie->cc[1];
341	}
342	/* NB: no need to setup ie ptrs; they are not (currently) used */
343	(void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len);
344
345	/* clear failure count after STA_FAIL_AGE passes */
346	if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) {
347		se->se_fails = 0;
348		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
349		    "%s: fails %u", __func__, se->se_fails);
350	}
351
352	se->se_lastupdate = ticks;		/* update time */
353	se->se_seen = 1;
354	se->se_notseen = 0;
355
356	KASSERT(sizeof(sp->bchan) == 1, ("bchan size"));
357	if (rssi > st->st_maxrssi[sp->bchan])
358		st->st_maxrssi[sp->bchan] = rssi;
359
360	mtx_unlock(&st->st_lock);
361
362	/*
363	 * If looking for a quick choice and nothing's
364	 * been found check here.
365	 */
366	if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0)
367		ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
368
369	return 1;
370#undef PICK1ST
371#undef ISPROBE
372}
373
374/*
375 * Check if a channel is excluded by user request.
376 */
377static int
378isexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c)
379{
380	return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) ||
381	    (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
382	     c->ic_freq != vap->iv_des_chan->ic_freq));
383}
384
385static struct ieee80211_channel *
386find11gchannel(struct ieee80211com *ic, int i, int freq)
387{
388	struct ieee80211_channel *c;
389	int j;
390
391	/*
392	 * The normal ordering in the channel list is b channel
393	 * immediately followed by g so optimize the search for
394	 * this.  We'll still do a full search just in case.
395	 */
396	for (j = i+1; j < ic->ic_nchans; j++) {
397		c = &ic->ic_channels[j];
398		if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
399			return c;
400	}
401	for (j = 0; j < i; j++) {
402		c = &ic->ic_channels[j];
403		if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
404			return c;
405	}
406	return NULL;
407}
408
409static const u_int chanflags[IEEE80211_MODE_MAX] = {
410	[IEEE80211_MODE_AUTO]	  = IEEE80211_CHAN_B,
411	[IEEE80211_MODE_11A]	  = IEEE80211_CHAN_A,
412	[IEEE80211_MODE_11B]	  = IEEE80211_CHAN_B,
413	[IEEE80211_MODE_11G]	  = IEEE80211_CHAN_G,
414	[IEEE80211_MODE_FH]	  = IEEE80211_CHAN_FHSS,
415	/* check base channel */
416	[IEEE80211_MODE_TURBO_A]  = IEEE80211_CHAN_A,
417	[IEEE80211_MODE_TURBO_G]  = IEEE80211_CHAN_G,
418	[IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_ST,
419	[IEEE80211_MODE_HALF]	  = IEEE80211_CHAN_HALF,
420	[IEEE80211_MODE_QUARTER]  = IEEE80211_CHAN_QUARTER,
421	/* check legacy */
422	[IEEE80211_MODE_11NA]	  = IEEE80211_CHAN_A,
423	[IEEE80211_MODE_11NG]	  = IEEE80211_CHAN_G,
424};
425
426static void
427add_channels(struct ieee80211vap *vap,
428	struct ieee80211_scan_state *ss,
429	enum ieee80211_phymode mode, const uint16_t freq[], int nfreq)
430{
431#define	N(a)	(sizeof(a) / sizeof(a[0]))
432	struct ieee80211com *ic = vap->iv_ic;
433	struct ieee80211_channel *c, *cg;
434	u_int modeflags;
435	int i;
436
437	KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
438	modeflags = chanflags[mode];
439	for (i = 0; i < nfreq; i++) {
440		if (ss->ss_last >= IEEE80211_SCAN_MAX)
441			break;
442
443		c = ieee80211_find_channel(ic, freq[i], modeflags);
444		if (c == NULL || isexcluded(vap, c))
445			continue;
446		if (mode == IEEE80211_MODE_AUTO) {
447			/*
448			 * XXX special-case 11b/g channels so we select
449			 *     the g channel if both are present.
450			 */
451			if (IEEE80211_IS_CHAN_B(c) &&
452			    (cg = find11gchannel(ic, i, c->ic_freq)) != NULL)
453				c = cg;
454		}
455		ss->ss_chans[ss->ss_last++] = c;
456	}
457#undef N
458}
459
460struct scanlist {
461	uint16_t	mode;
462	uint16_t	count;
463	const uint16_t	*list;
464};
465
466static int
467checktable(const struct scanlist *scan, const struct ieee80211_channel *c)
468{
469	int i;
470
471	for (; scan->list != NULL; scan++) {
472		for (i = 0; i < scan->count; i++)
473			if (scan->list[i] == c->ic_freq)
474				return 1;
475	}
476	return 0;
477}
478
479static int
480onscanlist(const struct ieee80211_scan_state *ss,
481	const struct ieee80211_channel *c)
482{
483	int i;
484
485	for (i = 0; i < ss->ss_last; i++)
486		if (ss->ss_chans[i] == c)
487			return 1;
488	return 0;
489}
490
491static void
492sweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
493	const struct scanlist table[])
494{
495	struct ieee80211com *ic = vap->iv_ic;
496	struct ieee80211_channel *c;
497	int i;
498
499	for (i = 0; i < ic->ic_nchans; i++) {
500		if (ss->ss_last >= IEEE80211_SCAN_MAX)
501			break;
502
503		c = &ic->ic_channels[i];
504		/*
505		 * Ignore dynamic turbo channels; we scan them
506		 * in normal mode (i.e. not boosted).  Likewise
507		 * for HT channels, they get scanned using
508		 * legacy rates.
509		 */
510		if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c))
511			continue;
512
513		/*
514		 * If a desired mode was specified, scan only
515		 * channels that satisfy that constraint.
516		 */
517		if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
518		    vap->iv_des_mode != ieee80211_chan2mode(c))
519			continue;
520
521		/*
522		 * Skip channels excluded by user request.
523		 */
524		if (isexcluded(vap, c))
525			continue;
526
527		/*
528		 * Add the channel unless it is listed in the
529		 * fixed scan order tables.  This insures we
530		 * don't sweep back in channels we filtered out
531		 * above.
532		 */
533		if (checktable(table, c))
534			continue;
535
536		/* Add channel to scanning list. */
537		ss->ss_chans[ss->ss_last++] = c;
538	}
539	/*
540	 * Explicitly add any desired channel if:
541	 * - not already on the scan list
542	 * - allowed by any desired mode constraint
543	 * - there is space in the scan list
544	 * This allows the channel to be used when the filtering
545	 * mechanisms would otherwise elide it (e.g HT, turbo).
546	 */
547	c = vap->iv_des_chan;
548	if (c != IEEE80211_CHAN_ANYC &&
549	    !onscanlist(ss, c) &&
550	    (vap->iv_des_mode == IEEE80211_MODE_AUTO ||
551	     vap->iv_des_mode == ieee80211_chan2mode(c)) &&
552	    ss->ss_last < IEEE80211_SCAN_MAX)
553		ss->ss_chans[ss->ss_last++] = c;
554}
555
556static void
557makescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
558	const struct scanlist table[])
559{
560	const struct scanlist *scan;
561	enum ieee80211_phymode mode;
562
563	ss->ss_last = 0;
564	/*
565	 * Use the table of ordered channels to construct the list
566	 * of channels for scanning.  Any channels in the ordered
567	 * list not in the master list will be discarded.
568	 */
569	for (scan = table; scan->list != NULL; scan++) {
570		mode = scan->mode;
571		if (vap->iv_des_mode != IEEE80211_MODE_AUTO) {
572			/*
573			 * If a desired mode was specified, scan only
574			 * channels that satisfy that constraint.
575			 */
576			if (vap->iv_des_mode != mode) {
577				/*
578				 * The scan table marks 2.4Ghz channels as b
579				 * so if the desired mode is 11g, then use
580				 * the 11b channel list but upgrade the mode.
581				 */
582				if (vap->iv_des_mode != IEEE80211_MODE_11G ||
583				    mode != IEEE80211_MODE_11B)
584					continue;
585				mode = IEEE80211_MODE_11G;	/* upgrade */
586			}
587		} else {
588			/*
589			 * This lets add_channels upgrade an 11b channel
590			 * to 11g if available.
591			 */
592			if (mode == IEEE80211_MODE_11B)
593				mode = IEEE80211_MODE_AUTO;
594		}
595#ifdef IEEE80211_F_XR
596		/* XR does not operate on turbo channels */
597		if ((vap->iv_flags & IEEE80211_F_XR) &&
598		    (mode == IEEE80211_MODE_TURBO_A ||
599		     mode == IEEE80211_MODE_TURBO_G ||
600		     mode == IEEE80211_MODE_STURBO_A))
601			continue;
602#endif
603		/*
604		 * Add the list of the channels; any that are not
605		 * in the master channel list will be discarded.
606		 */
607		add_channels(vap, ss, mode, scan->list, scan->count);
608	}
609
610	/*
611	 * Add the channels from the ic that are not present
612	 * in the table.
613	 */
614	sweepchannels(ss, vap, table);
615}
616
617static const uint16_t rcl1[] =		/* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
618{ 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
619static const uint16_t rcl2[] =		/* 4 MKK channels: 34, 38, 42, 46 */
620{ 5170, 5190, 5210, 5230 };
621static const uint16_t rcl3[] =		/* 2.4Ghz ch: 1,6,11,7,13 */
622{ 2412, 2437, 2462, 2442, 2472 };
623static const uint16_t rcl4[] =		/* 5 FCC channel: 149, 153, 161, 165 */
624{ 5745, 5765, 5785, 5805, 5825 };
625static const uint16_t rcl7[] =		/* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
626{ 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
627static const uint16_t rcl8[] =		/* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
628{ 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
629static const uint16_t rcl9[] =		/* 2.4Ghz ch: 14 */
630{ 2484 };
631static const uint16_t rcl10[] =	/* Added Korean channels 2312-2372 */
632{ 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
633static const uint16_t rcl11[] =	/* Added Japan channels in 4.9/5.0 spectrum */
634{ 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
635#ifdef ATH_TURBO_SCAN
636static const uint16_t rcl5[] =		/* 3 static turbo channels */
637{ 5210, 5250, 5290 };
638static const uint16_t rcl6[] =		/* 2 static turbo channels */
639{ 5760, 5800 };
640static const uint16_t rcl6x[] =	/* 4 FCC3 turbo channels */
641{ 5540, 5580, 5620, 5660 };
642static const uint16_t rcl12[] =	/* 2.4Ghz Turbo channel 6 */
643{ 2437 };
644static const uint16_t rcl13[] =	/* dynamic Turbo channels */
645{ 5200, 5240, 5280, 5765, 5805 };
646#endif /* ATH_TURBO_SCAN */
647
648#define	X(a)	.count = sizeof(a)/sizeof(a[0]), .list = a
649
650static const struct scanlist staScanTable[] = {
651	{ IEEE80211_MODE_11B,   	X(rcl3) },
652	{ IEEE80211_MODE_11A,   	X(rcl1) },
653	{ IEEE80211_MODE_11A,   	X(rcl2) },
654	{ IEEE80211_MODE_11B,   	X(rcl8) },
655	{ IEEE80211_MODE_11B,   	X(rcl9) },
656	{ IEEE80211_MODE_11A,   	X(rcl4) },
657#ifdef ATH_TURBO_SCAN
658	{ IEEE80211_MODE_STURBO_A,	X(rcl5) },
659	{ IEEE80211_MODE_STURBO_A,	X(rcl6) },
660	{ IEEE80211_MODE_TURBO_A,	X(rcl6x) },
661	{ IEEE80211_MODE_TURBO_A,	X(rcl13) },
662#endif /* ATH_TURBO_SCAN */
663	{ IEEE80211_MODE_11A,		X(rcl7) },
664	{ IEEE80211_MODE_11B,		X(rcl10) },
665	{ IEEE80211_MODE_11A,		X(rcl11) },
666#ifdef ATH_TURBO_SCAN
667	{ IEEE80211_MODE_TURBO_G,	X(rcl12) },
668#endif /* ATH_TURBO_SCAN */
669	{ .list = NULL }
670};
671
672/*
673 * Start a station-mode scan by populating the channel list.
674 */
675static int
676sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
677{
678	struct sta_table *st = ss->ss_priv;
679
680	makescanlist(ss, vap, staScanTable);
681
682	if (ss->ss_mindwell == 0)
683		ss->ss_mindwell = msecs_to_ticks(20);	/* 20ms */
684	if (ss->ss_maxdwell == 0)
685		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
686
687	st->st_scangen++;
688	st->st_newscan = 1;
689
690	return 0;
691}
692
693/*
694 * Restart a scan, typically a bg scan but can
695 * also be a fg scan that came up empty.
696 */
697static int
698sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
699{
700	struct sta_table *st = ss->ss_priv;
701
702	st->st_newscan = 1;
703	return 0;
704}
705
706/*
707 * Cancel an ongoing scan.
708 */
709static int
710sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
711{
712	return 0;
713}
714
715/* unalligned little endian access */
716#define LE_READ_2(p)					\
717	((uint16_t)					\
718	 ((((const uint8_t *)(p))[0]      ) |		\
719	  (((const uint8_t *)(p))[1] <<  8)))
720
721/*
722 * Demote any supplied 11g channel to 11b.  There should
723 * always be an 11b channel but we check anyway...
724 */
725static struct ieee80211_channel *
726demote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan)
727{
728	struct ieee80211_channel *c;
729
730	if (IEEE80211_IS_CHAN_ANYG(chan) &&
731	    vap->iv_des_mode == IEEE80211_MODE_AUTO) {
732		c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq,
733		    (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) |
734		    IEEE80211_CHAN_B);
735		if (c != NULL)
736			chan = c;
737	}
738	return chan;
739}
740
741static int
742maxrate(const struct ieee80211_scan_entry *se)
743{
744	const struct ieee80211_ie_htcap *htcap =
745	    (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie;
746	int rmax, r, i;
747	uint16_t caps;
748
749	rmax = 0;
750	if (htcap != NULL) {
751		/*
752		 * HT station; inspect supported MCS and then adjust
753		 * rate by channel width.  Could also include short GI
754		 * in this if we want to be extra accurate.
755		 */
756		/* XXX assumes MCS15 is max */
757		for (i = 15; i >= 0 && isclr(htcap->hc_mcsset, i); i--)
758			;
759		if (i >= 0) {
760			caps = LE_READ_2(&htcap->hc_cap);
761			/* XXX short/long GI */
762			if (caps & IEEE80211_HTCAP_CHWIDTH40)
763				rmax = ieee80211_htrates[i].ht40_rate_400ns;
764			else
765				rmax = ieee80211_htrates[i].ht40_rate_800ns;
766		}
767	}
768	for (i = 0; i < se->se_rates[1]; i++) {
769		r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
770		if (r > rmax)
771			rmax = r;
772	}
773	for (i = 0; i < se->se_xrates[1]; i++) {
774		r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
775		if (r > rmax)
776			rmax = r;
777	}
778	return rmax;
779}
780
781/*
782 * Compare the capabilities of two entries and decide which is
783 * more desirable (return >0 if a is considered better).  Note
784 * that we assume compatibility/usability has already been checked
785 * so we don't need to (e.g. validate whether privacy is supported).
786 * Used to select the best scan candidate for association in a BSS.
787 */
788static int
789sta_compare(const struct sta_entry *a, const struct sta_entry *b)
790{
791#define	PREFER(_a,_b,_what) do {			\
792	if (((_a) ^ (_b)) & (_what))			\
793		return ((_a) & (_what)) ? 1 : -1;	\
794} while (0)
795	int maxa, maxb;
796	int8_t rssia, rssib;
797	int weight;
798
799	/* privacy support */
800	PREFER(a->base.se_capinfo, b->base.se_capinfo,
801		IEEE80211_CAPINFO_PRIVACY);
802
803	/* compare count of previous failures */
804	weight = b->se_fails - a->se_fails;
805	if (abs(weight) > 1)
806		return weight;
807
808	/*
809	 * Compare rssi.  If the two are considered equivalent
810	 * then fallback to other criteria.  We threshold the
811	 * comparisons to avoid selecting an ap purely by rssi
812	 * when both values may be good but one ap is otherwise
813	 * more desirable (e.g. an 11b-only ap with stronger
814	 * signal than an 11g ap).
815	 */
816	rssia = MIN(a->base.se_rssi, STA_RSSI_MAX);
817	rssib = MIN(b->base.se_rssi, STA_RSSI_MAX);
818	if (abs(rssib - rssia) < 5) {
819		/* best/max rate preferred if signal level close enough XXX */
820		maxa = maxrate(&a->base);
821		maxb = maxrate(&b->base);
822		if (maxa != maxb)
823			return maxa - maxb;
824		/* XXX use freq for channel preference */
825		/* for now just prefer 5Ghz band to all other bands */
826		PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan),
827		       IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1);
828	}
829	/* all things being equal, use signal level */
830	return a->base.se_rssi - b->base.se_rssi;
831#undef PREFER
832}
833
834/*
835 * Check rate set suitability and return the best supported rate.
836 * XXX inspect MCS for HT
837 */
838static int
839check_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan,
840    const struct ieee80211_scan_entry *se)
841{
842#define	RV(v)	((v) & IEEE80211_RATE_VAL)
843	const struct ieee80211_rateset *srs;
844	int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate;
845	const uint8_t *rs;
846
847	okrate = badrate = 0;
848
849	srs = ieee80211_get_suprates(vap->iv_ic, chan);
850	nrs = se->se_rates[1];
851	rs = se->se_rates+2;
852	/* XXX MCS */
853	ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate;
854	fixedrate = IEEE80211_FIXED_RATE_NONE;
855again:
856	for (i = 0; i < nrs; i++) {
857		r = RV(rs[i]);
858		badrate = r;
859		/*
860		 * Check any fixed rate is included.
861		 */
862		if (r == ucastrate)
863			fixedrate = r;
864		/*
865		 * Check against our supported rates.
866		 */
867		for (j = 0; j < srs->rs_nrates; j++)
868			if (r == RV(srs->rs_rates[j])) {
869				if (r > okrate)		/* NB: track max */
870					okrate = r;
871				break;
872			}
873
874		if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) {
875			/*
876			 * Don't try joining a BSS, if we don't support
877			 * one of its basic rates.
878			 */
879			okrate = 0;
880			goto back;
881		}
882	}
883	if (rs == se->se_rates+2) {
884		/* scan xrates too; sort of an algol68-style for loop */
885		nrs = se->se_xrates[1];
886		rs = se->se_xrates+2;
887		goto again;
888	}
889
890back:
891	if (okrate == 0 || ucastrate != fixedrate)
892		return badrate | IEEE80211_RATE_BASIC;
893	else
894		return RV(okrate);
895#undef RV
896}
897
898static int
899match_ssid(const uint8_t *ie,
900	int nssid, const struct ieee80211_scan_ssid ssids[])
901{
902	int i;
903
904	for (i = 0; i < nssid; i++) {
905		if (ie[1] == ssids[i].len &&
906		     memcmp(ie+2, ssids[i].ssid, ie[1]) == 0)
907			return 1;
908	}
909	return 0;
910}
911
912#ifdef IEEE80211_SUPPORT_TDMA
913static int
914tdma_isfull(const struct ieee80211_tdma_param *tdma)
915{
916	int slot, slotcnt;
917
918	slotcnt = tdma->tdma_slotcnt;
919	for (slot = slotcnt-1; slot >= 0; slot--)
920		if (isclr(tdma->tdma_inuse, slot))
921			return 0;
922	return 1;
923}
924#endif /* IEEE80211_SUPPORT_TDMA */
925
926/*
927 * Test a scan candidate for suitability/compatibility.
928 */
929static int
930match_bss(struct ieee80211vap *vap,
931	const struct ieee80211_scan_state *ss, struct sta_entry *se0,
932	int debug)
933{
934	struct ieee80211com *ic = vap->iv_ic;
935	struct ieee80211_scan_entry *se = &se0->base;
936        uint8_t rate;
937        int fail;
938
939	fail = 0;
940	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
941		fail |= MATCH_CHANNEL;
942	/*
943	 * NB: normally the desired mode is used to construct
944	 * the channel list, but it's possible for the scan
945	 * cache to include entries for stations outside this
946	 * list so we check the desired mode here to weed them
947	 * out.
948	 */
949	if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
950	    (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
951	    chanflags[vap->iv_des_mode])
952		fail |= MATCH_CHANNEL;
953	if (vap->iv_opmode == IEEE80211_M_IBSS) {
954		if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
955			fail |= MATCH_CAPINFO;
956#ifdef IEEE80211_SUPPORT_TDMA
957	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
958		/*
959		 * Adhoc demo network setup shouldn't really be scanning
960		 * but just in case skip stations operating in IBSS or
961		 * BSS mode.
962		 */
963		if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
964			fail |= MATCH_CAPINFO;
965		/*
966		 * TDMA operation cannot coexist with a normal 802.11 network;
967		 * skip if IBSS or ESS capabilities are marked and require
968		 * the beacon have a TDMA ie present.
969		 */
970		if (vap->iv_caps & IEEE80211_C_TDMA) {
971			const struct ieee80211_tdma_param *tdma =
972			    (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie;
973
974			if (tdma == NULL)
975				fail |= MATCH_TDMA_NOIE;
976			else if (tdma->tdma_slot != 0)
977				fail |= MATCH_TDMA_NOTMASTER;
978			else if (tdma_isfull(tdma))
979				fail |= MATCH_TDMA_NOSLOT;
980#if 0
981			else if (ieee80211_local_address(se->se_macaddr))
982				fail |= MATCH_TDMA_LOCAL;
983#endif
984		}
985#endif /* IEEE80211_SUPPORT_TDMA */
986	} else {
987		if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
988			fail |= MATCH_CAPINFO;
989		/*
990		 * If 11d is enabled and we're attempting to join a bss
991		 * that advertises it's country code then compare our
992		 * current settings to what we fetched from the country ie.
993		 * If our country code is unspecified or different then do
994		 * not attempt to join the bss.  We should have already
995		 * dispatched an event to user space that identifies the
996		 * new country code so our regdomain config should match.
997		 */
998		if ((IEEE80211_IS_CHAN_11D(se->se_chan) ||
999		    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
1000		    se->se_cc[0] != 0 &&
1001		    (ic->ic_regdomain.country == CTRY_DEFAULT ||
1002		     !isocmp(se->se_cc, ic->ic_regdomain.isocc)))
1003			fail |= MATCH_CC;
1004	}
1005	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1006		if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
1007			fail |= MATCH_PRIVACY;
1008	} else {
1009		/* XXX does this mean privacy is supported or required? */
1010		if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
1011			fail |= MATCH_PRIVACY;
1012	}
1013	se0->se_flags &= ~STA_DEMOTE11B;
1014	rate = check_rate(vap, se->se_chan, se);
1015	if (rate & IEEE80211_RATE_BASIC) {
1016		fail |= MATCH_RATE;
1017		/*
1018		 * An 11b-only ap will give a rate mismatch if there is an
1019		 * OFDM fixed tx rate for 11g.  Try downgrading the channel
1020		 * in the scan list to 11b and retry the rate check.
1021		 */
1022		if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) {
1023			rate = check_rate(vap, demote11b(vap, se->se_chan), se);
1024			if ((rate & IEEE80211_RATE_BASIC) == 0) {
1025				fail &= ~MATCH_RATE;
1026				se0->se_flags |= STA_DEMOTE11B;
1027			}
1028		}
1029	} else if (rate < 2*24) {
1030		/*
1031		 * This is an 11b-only ap.  Check the desired mode in
1032		 * case that needs to be honored (mode 11g filters out
1033		 * 11b-only ap's).  Otherwise force any 11g channel used
1034		 * in scanning to be demoted.
1035		 *
1036		 * NB: we cheat a bit here by looking at the max rate;
1037		 *     we could/should check the rates.
1038		 */
1039		if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO ||
1040		      vap->iv_des_mode == IEEE80211_MODE_11B))
1041			fail |= MATCH_RATE;
1042		else
1043			se0->se_flags |= STA_DEMOTE11B;
1044	}
1045	if (ss->ss_nssid != 0 &&
1046	    !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
1047		fail |= MATCH_SSID;
1048	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
1049	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
1050		fail |= MATCH_BSSID;
1051	if (se0->se_fails >= STA_FAILS_MAX)
1052		fail |= MATCH_FAILS;
1053	if (se0->se_notseen >= STA_PURGE_SCANS)
1054		fail |= MATCH_NOTSEEN;
1055	if (se->se_rssi < STA_RSSI_MIN)
1056		fail |= MATCH_RSSI;
1057#ifdef IEEE80211_DEBUG
1058	if (ieee80211_msg(vap, debug)) {
1059		printf(" %c %s",
1060		    fail & MATCH_FAILS ? '=' :
1061		    fail & MATCH_NOTSEEN ? '^' :
1062		    fail & MATCH_CC ? '$' :
1063#ifdef IEEE80211_SUPPORT_TDMA
1064		    fail & MATCH_TDMA_NOIE ? '&' :
1065		    fail & MATCH_TDMA_NOTMASTER ? ':' :
1066		    fail & MATCH_TDMA_NOSLOT ? '@' :
1067		    fail & MATCH_TDMA_LOCAL ? '#' :
1068#endif
1069		    fail ? '-' : '+', ether_sprintf(se->se_macaddr));
1070		printf(" %s%c", ether_sprintf(se->se_bssid),
1071		    fail & MATCH_BSSID ? '!' : ' ');
1072		printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
1073			fail & MATCH_CHANNEL ? '!' : ' ');
1074		printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' ');
1075		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
1076		    fail & MATCH_RATE ? '!' : ' ');
1077		printf(" %4s%c",
1078		    (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
1079		    (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
1080		    "????",
1081		    fail & MATCH_CAPINFO ? '!' : ' ');
1082		printf(" %3s%c ",
1083		    (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
1084		    "wep" : "no",
1085		    fail & MATCH_PRIVACY ? '!' : ' ');
1086		ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]);
1087		printf("%s\n", fail & MATCH_SSID ? "!" : "");
1088	}
1089#endif
1090	return fail;
1091}
1092
1093static void
1094sta_update_notseen(struct sta_table *st)
1095{
1096	struct sta_entry *se;
1097
1098	mtx_lock(&st->st_lock);
1099	TAILQ_FOREACH(se, &st->st_entry, se_list) {
1100		/*
1101		 * If seen the reset and don't bump the count;
1102		 * otherwise bump the ``not seen'' count.  Note
1103		 * that this insures that stations for which we
1104		 * see frames while not scanning but not during
1105		 * this scan will not be penalized.
1106		 */
1107		if (se->se_seen)
1108			se->se_seen = 0;
1109		else
1110			se->se_notseen++;
1111	}
1112	mtx_unlock(&st->st_lock);
1113}
1114
1115static void
1116sta_dec_fails(struct sta_table *st)
1117{
1118	struct sta_entry *se;
1119
1120	mtx_lock(&st->st_lock);
1121	TAILQ_FOREACH(se, &st->st_entry, se_list)
1122		if (se->se_fails)
1123			se->se_fails--;
1124	mtx_unlock(&st->st_lock);
1125}
1126
1127static struct sta_entry *
1128select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug)
1129{
1130	struct sta_table *st = ss->ss_priv;
1131	struct sta_entry *se, *selbs = NULL;
1132
1133	IEEE80211_DPRINTF(vap, debug, " %s\n",
1134	    "macaddr          bssid         chan  rssi  rate flag  wep  essid");
1135	mtx_lock(&st->st_lock);
1136	TAILQ_FOREACH(se, &st->st_entry, se_list) {
1137		ieee80211_ies_expand(&se->base.se_ies);
1138		if (match_bss(vap, ss, se, debug) == 0) {
1139			if (selbs == NULL)
1140				selbs = se;
1141			else if (sta_compare(se, selbs) > 0)
1142				selbs = se;
1143		}
1144	}
1145	mtx_unlock(&st->st_lock);
1146
1147	return selbs;
1148}
1149
1150/*
1151 * Pick an ap or ibss network to join or find a channel
1152 * to use to start an ibss network.
1153 */
1154static int
1155sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1156{
1157	struct sta_table *st = ss->ss_priv;
1158	struct sta_entry *selbs;
1159	struct ieee80211_channel *chan;
1160
1161	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1162		("wrong mode %u", vap->iv_opmode));
1163
1164	if (st->st_newscan) {
1165		sta_update_notseen(st);
1166		st->st_newscan = 0;
1167	}
1168	if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1169		/*
1170		 * Manual/background scan, don't select+join the
1171		 * bss, just return.  The scanning framework will
1172		 * handle notification that this has completed.
1173		 */
1174		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1175		return 1;
1176	}
1177	/*
1178	 * Automatic sequencing; look for a candidate and
1179	 * if found join the network.
1180	 */
1181	/* NB: unlocked read should be ok */
1182	if (TAILQ_FIRST(&st->st_entry) == NULL) {
1183		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1184			"%s: no scan candidate\n", __func__);
1185		if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1186			return 0;
1187notfound:
1188		/*
1189		 * If nothing suitable was found decrement
1190		 * the failure counts so entries will be
1191		 * reconsidered the next time around.  We
1192		 * really want to do this only for sta's
1193		 * where we've previously had some success.
1194		 */
1195		sta_dec_fails(st);
1196		st->st_newscan = 1;
1197		return 0;			/* restart scan */
1198	}
1199	selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1200	if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1201		return (selbs != NULL);
1202	if (selbs == NULL)
1203		goto notfound;
1204	chan = selbs->base.se_chan;
1205	if (selbs->se_flags & STA_DEMOTE11B)
1206		chan = demote11b(vap, chan);
1207	if (!ieee80211_sta_join(vap, chan, &selbs->base))
1208		goto notfound;
1209	return 1;				/* terminate scan */
1210}
1211
1212/*
1213 * Lookup an entry in the scan cache.  We assume we're
1214 * called from the bottom half or such that we don't need
1215 * to block the bottom half so that it's safe to return
1216 * a reference to an entry w/o holding the lock on the table.
1217 */
1218static struct sta_entry *
1219sta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1220{
1221	struct sta_entry *se;
1222	int hash = STA_HASH(macaddr);
1223
1224	mtx_lock(&st->st_lock);
1225	LIST_FOREACH(se, &st->st_hash[hash], se_hash)
1226		if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
1227			break;
1228	mtx_unlock(&st->st_lock);
1229
1230	return se;		/* NB: unlocked */
1231}
1232
1233static void
1234sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1235{
1236	struct ieee80211com *ic = vap->iv_ic;
1237	struct ieee80211_node *ni = vap->iv_bss;
1238	struct sta_table *st = ss->ss_priv;
1239	enum ieee80211_phymode mode;
1240	struct sta_entry *se, *selbs;
1241	uint8_t roamRate, curRate, ucastRate;
1242	int8_t roamRssi, curRssi;
1243
1244	se = sta_lookup(st, ni->ni_macaddr);
1245	if (se == NULL) {
1246		/* XXX something is wrong */
1247		return;
1248	}
1249
1250	mode = ieee80211_chan2mode(ic->ic_bsschan);
1251	roamRate = vap->iv_roamparms[mode].rate;
1252	roamRssi = vap->iv_roamparms[mode].rssi;
1253	ucastRate = vap->iv_txparms[mode].ucastrate;
1254	/* NB: the most up to date rssi is in the node, not the scan cache */
1255	curRssi = ic->ic_node_getrssi(ni);
1256	if (ucastRate == IEEE80211_FIXED_RATE_NONE) {
1257		curRate = ni->ni_txrate;
1258		roamRate &= IEEE80211_RATE_VAL;
1259		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1260		    "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
1261		    __func__, curRssi, curRate, roamRssi, roamRate);
1262	} else {
1263		curRate = roamRate;	/* NB: insure compare below fails */
1264		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
1265		    "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi);
1266	}
1267	/*
1268	 * Check if a new ap should be used and switch.
1269	 * XXX deauth current ap
1270	 */
1271	if (curRate < roamRate || curRssi < roamRssi) {
1272		if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
1273			/*
1274			 * Scan cache contents are too old; force a scan now
1275			 * if possible so we have current state to make a
1276			 * decision with.  We don't kick off a bg scan if
1277			 * we're using dynamic turbo and boosted or if the
1278			 * channel is busy.
1279			 * XXX force immediate switch on scan complete
1280			 */
1281			if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1282			    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle))
1283				ieee80211_bg_scan(vap, 0);
1284			return;
1285		}
1286		se->base.se_rssi = curRssi;
1287		selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM);
1288		if (selbs != NULL && selbs != se) {
1289			struct ieee80211_channel *chan;
1290
1291			IEEE80211_DPRINTF(vap,
1292			    IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG,
1293			    "%s: ROAM: curRate %u, roamRate %u, "
1294			    "curRssi %d, roamRssi %d\n", __func__,
1295			    curRate, roamRate, curRssi, roamRssi);
1296
1297			chan = selbs->base.se_chan;
1298			if (selbs->se_flags & STA_DEMOTE11B)
1299				chan = demote11b(vap, chan);
1300			(void) ieee80211_sta_join(vap, chan, &selbs->base);
1301		}
1302	}
1303}
1304
1305/*
1306 * Age entries in the scan cache.
1307 * XXX also do roaming since it's convenient
1308 */
1309static void
1310sta_age(struct ieee80211_scan_state *ss)
1311{
1312	struct ieee80211vap *vap = ss->ss_vap;
1313
1314	adhoc_age(ss);
1315	/*
1316	 * If rate control is enabled check periodically to see if
1317	 * we should roam from our current connection to one that
1318	 * might be better.  This only applies when we're operating
1319	 * in sta mode and automatic roaming is set.
1320	 * XXX defer if busy
1321	 * XXX repeater station
1322	 * XXX do when !bgscan?
1323	 */
1324	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1325		("wrong mode %u", vap->iv_opmode));
1326	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO &&
1327	    (vap->iv_ic->ic_flags & IEEE80211_F_BGSCAN) &&
1328	    vap->iv_state >= IEEE80211_S_RUN)
1329		/* XXX vap is implicit */
1330		sta_roam_check(ss, vap);
1331}
1332
1333/*
1334 * Iterate over the entries in the scan cache, invoking
1335 * the callback function on each one.
1336 */
1337static void
1338sta_iterate(struct ieee80211_scan_state *ss,
1339	ieee80211_scan_iter_func *f, void *arg)
1340{
1341	struct sta_table *st = ss->ss_priv;
1342	struct sta_entry *se;
1343	u_int gen;
1344
1345	mtx_lock(&st->st_scanlock);
1346	gen = st->st_scaniter++;
1347restart:
1348	mtx_lock(&st->st_lock);
1349	TAILQ_FOREACH(se, &st->st_entry, se_list) {
1350		if (se->se_scangen != gen) {
1351			se->se_scangen = gen;
1352			/* update public state */
1353			se->base.se_age = ticks - se->se_lastupdate;
1354			mtx_unlock(&st->st_lock);
1355			(*f)(arg, &se->base);
1356			goto restart;
1357		}
1358	}
1359	mtx_unlock(&st->st_lock);
1360
1361	mtx_unlock(&st->st_scanlock);
1362}
1363
1364static void
1365sta_assoc_fail(struct ieee80211_scan_state *ss,
1366	const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason)
1367{
1368	struct sta_table *st = ss->ss_priv;
1369	struct sta_entry *se;
1370
1371	se = sta_lookup(st, macaddr);
1372	if (se != NULL) {
1373		se->se_fails++;
1374		se->se_lastfail = ticks;
1375		IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1376		    macaddr, "%s: reason %u fails %u",
1377		    __func__, reason, se->se_fails);
1378	}
1379}
1380
1381static void
1382sta_assoc_success(struct ieee80211_scan_state *ss,
1383	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1384{
1385	struct sta_table *st = ss->ss_priv;
1386	struct sta_entry *se;
1387
1388	se = sta_lookup(st, macaddr);
1389	if (se != NULL) {
1390#if 0
1391		se->se_fails = 0;
1392		IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
1393		    macaddr, "%s: fails %u",
1394		    __func__, se->se_fails);
1395#endif
1396		se->se_lastassoc = ticks;
1397	}
1398}
1399
1400static const struct ieee80211_scanner sta_default = {
1401	.scan_name		= "default",
1402	.scan_attach		= sta_attach,
1403	.scan_detach		= sta_detach,
1404	.scan_start		= sta_start,
1405	.scan_restart		= sta_restart,
1406	.scan_cancel		= sta_cancel,
1407	.scan_end		= sta_pick_bss,
1408	.scan_flush		= sta_flush,
1409	.scan_add		= sta_add,
1410	.scan_age		= sta_age,
1411	.scan_iterate		= sta_iterate,
1412	.scan_assoc_fail	= sta_assoc_fail,
1413	.scan_assoc_success	= sta_assoc_success,
1414};
1415
1416/*
1417 * Adhoc mode-specific support.
1418 */
1419
1420static const uint16_t adhocWorld[] =		/* 36, 40, 44, 48 */
1421{ 5180, 5200, 5220, 5240 };
1422static const uint16_t adhocFcc3[] =		/* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */
1423{ 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 };
1424static const uint16_t adhocMkk[] =		/* 34, 38, 42, 46 */
1425{ 5170, 5190, 5210, 5230 };
1426static const uint16_t adhoc11b[] =		/* 10, 11 */
1427{ 2457, 2462 };
1428
1429static const struct scanlist adhocScanTable[] = {
1430	{ IEEE80211_MODE_11B,   	X(adhoc11b) },
1431	{ IEEE80211_MODE_11A,   	X(adhocWorld) },
1432	{ IEEE80211_MODE_11A,   	X(adhocFcc3) },
1433	{ IEEE80211_MODE_11B,   	X(adhocMkk) },
1434	{ .list = NULL }
1435};
1436#undef X
1437
1438/*
1439 * Start an adhoc-mode scan by populating the channel list.
1440 */
1441static int
1442adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1443{
1444	struct sta_table *st = ss->ss_priv;
1445
1446	makescanlist(ss, vap, adhocScanTable);
1447
1448	if (ss->ss_mindwell == 0)
1449		ss->ss_mindwell = msecs_to_ticks(200);	/* 200ms */
1450	if (ss->ss_maxdwell == 0)
1451		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
1452
1453	st->st_scangen++;
1454	st->st_newscan = 1;
1455
1456	return 0;
1457}
1458
1459/*
1460 * Select a channel to start an adhoc network on.
1461 * The channel list was populated with appropriate
1462 * channels so select one that looks least occupied.
1463 */
1464static struct ieee80211_channel *
1465adhoc_pick_channel(struct ieee80211_scan_state *ss, int flags)
1466{
1467	struct sta_table *st = ss->ss_priv;
1468	struct sta_entry *se;
1469	struct ieee80211_channel *c, *bestchan;
1470	int i, bestrssi, maxrssi;
1471
1472	bestchan = NULL;
1473	bestrssi = -1;
1474
1475	mtx_lock(&st->st_lock);
1476	for (i = 0; i < ss->ss_last; i++) {
1477		c = ss->ss_chans[i];
1478		/* never consider a channel with radar */
1479		if (IEEE80211_IS_CHAN_RADAR(c))
1480			continue;
1481		/* skip channels disallowed by regulatory settings */
1482		if (IEEE80211_IS_CHAN_NOADHOC(c))
1483			continue;
1484		/* check channel attributes for band compatibility */
1485		if (flags != 0 && (c->ic_flags & flags) != flags)
1486			continue;
1487		maxrssi = 0;
1488		TAILQ_FOREACH(se, &st->st_entry, se_list) {
1489			if (se->base.se_chan != c)
1490				continue;
1491			if (se->base.se_rssi > maxrssi)
1492				maxrssi = se->base.se_rssi;
1493		}
1494		if (bestchan == NULL || maxrssi < bestrssi)
1495			bestchan = c;
1496	}
1497	mtx_unlock(&st->st_lock);
1498
1499	return bestchan;
1500}
1501
1502/*
1503 * Pick an ibss network to join or find a channel
1504 * to use to start an ibss network.
1505 */
1506static int
1507adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1508{
1509	struct sta_table *st = ss->ss_priv;
1510	struct sta_entry *selbs;
1511	struct ieee80211_channel *chan;
1512
1513	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
1514		vap->iv_opmode == IEEE80211_M_AHDEMO,
1515		("wrong opmode %u", vap->iv_opmode));
1516
1517	if (st->st_newscan) {
1518		sta_update_notseen(st);
1519		st->st_newscan = 0;
1520	}
1521	if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
1522		/*
1523		 * Manual/background scan, don't select+join the
1524		 * bss, just return.  The scanning framework will
1525		 * handle notification that this has completed.
1526		 */
1527		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1528		return 1;
1529	}
1530	/*
1531	 * Automatic sequencing; look for a candidate and
1532	 * if found join the network.
1533	 */
1534	/* NB: unlocked read should be ok */
1535	if (TAILQ_FIRST(&st->st_entry) == NULL) {
1536		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1537			"%s: no scan candidate\n", __func__);
1538		if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1539			return 0;
1540notfound:
1541		/* NB: never auto-start a tdma network for slot !0 */
1542#ifdef IEEE80211_SUPPORT_TDMA
1543		if (vap->iv_des_nssid &&
1544		    ((vap->iv_caps & IEEE80211_C_TDMA) == 0 ||
1545		     ieee80211_tdma_getslot(vap) == 0)) {
1546#else
1547		if (vap->iv_des_nssid) {
1548#endif
1549			/*
1550			 * No existing adhoc network to join and we have
1551			 * an ssid; start one up.  If no channel was
1552			 * specified, try to select a channel.
1553			 */
1554			if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1555			    IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
1556				struct ieee80211com *ic = vap->iv_ic;
1557
1558				chan = adhoc_pick_channel(ss, 0);
1559				if (chan != NULL)
1560					chan = ieee80211_ht_adjust_channel(ic,
1561					    chan, vap->iv_flags_ext);
1562			} else
1563				chan = vap->iv_des_chan;
1564			if (chan != NULL) {
1565				ieee80211_create_ibss(vap, chan);
1566				return 1;
1567			}
1568		}
1569		/*
1570		 * If nothing suitable was found decrement
1571		 * the failure counts so entries will be
1572		 * reconsidered the next time around.  We
1573		 * really want to do this only for sta's
1574		 * where we've previously had some success.
1575		 */
1576		sta_dec_fails(st);
1577		st->st_newscan = 1;
1578		return 0;			/* restart scan */
1579	}
1580	selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1581	if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1582		return (selbs != NULL);
1583	if (selbs == NULL)
1584		goto notfound;
1585	chan = selbs->base.se_chan;
1586	if (selbs->se_flags & STA_DEMOTE11B)
1587		chan = demote11b(vap, chan);
1588	if (!ieee80211_sta_join(vap, chan, &selbs->base))
1589		goto notfound;
1590	return 1;				/* terminate scan */
1591}
1592
1593/*
1594 * Age entries in the scan cache.
1595 */
1596static void
1597adhoc_age(struct ieee80211_scan_state *ss)
1598{
1599	struct sta_table *st = ss->ss_priv;
1600	struct sta_entry *se, *next;
1601
1602	mtx_lock(&st->st_lock);
1603	TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
1604		if (se->se_notseen > STA_PURGE_SCANS) {
1605			TAILQ_REMOVE(&st->st_entry, se, se_list);
1606			LIST_REMOVE(se, se_hash);
1607			ieee80211_ies_cleanup(&se->base.se_ies);
1608			free(se, M_80211_SCAN);
1609		}
1610	}
1611	mtx_unlock(&st->st_lock);
1612}
1613
1614static const struct ieee80211_scanner adhoc_default = {
1615	.scan_name		= "default",
1616	.scan_attach		= sta_attach,
1617	.scan_detach		= sta_detach,
1618	.scan_start		= adhoc_start,
1619	.scan_restart		= sta_restart,
1620	.scan_cancel		= sta_cancel,
1621	.scan_end		= adhoc_pick_bss,
1622	.scan_flush		= sta_flush,
1623	.scan_pickchan		= adhoc_pick_channel,
1624	.scan_add		= sta_add,
1625	.scan_age		= adhoc_age,
1626	.scan_iterate		= sta_iterate,
1627	.scan_assoc_fail	= sta_assoc_fail,
1628	.scan_assoc_success	= sta_assoc_success,
1629};
1630
1631static void
1632ap_force_promisc(struct ieee80211com *ic)
1633{
1634	struct ifnet *ifp = ic->ic_ifp;
1635
1636	IEEE80211_LOCK(ic);
1637	/* set interface into promiscuous mode */
1638	ifp->if_flags |= IFF_PROMISC;
1639	ic->ic_update_promisc(ifp);
1640	IEEE80211_UNLOCK(ic);
1641}
1642
1643static void
1644ap_reset_promisc(struct ieee80211com *ic)
1645{
1646	IEEE80211_LOCK(ic);
1647	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
1648	IEEE80211_UNLOCK(ic);
1649}
1650
1651static int
1652ap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1653{
1654	struct sta_table *st = ss->ss_priv;
1655
1656	makescanlist(ss, vap, staScanTable);
1657
1658	if (ss->ss_mindwell == 0)
1659		ss->ss_mindwell = msecs_to_ticks(200);	/* 200ms */
1660	if (ss->ss_maxdwell == 0)
1661		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
1662
1663	st->st_scangen++;
1664	st->st_newscan = 1;
1665
1666	ap_force_promisc(vap->iv_ic);
1667	return 0;
1668}
1669
1670/*
1671 * Cancel an ongoing scan.
1672 */
1673static int
1674ap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1675{
1676	ap_reset_promisc(vap->iv_ic);
1677	return 0;
1678}
1679
1680/*
1681 * Pick a quiet channel to use for ap operation.
1682 */
1683static struct ieee80211_channel *
1684ap_pick_channel(struct ieee80211_scan_state *ss, int flags)
1685{
1686	struct sta_table *st = ss->ss_priv;
1687	struct ieee80211_channel *bestchan = NULL;
1688	int i;
1689
1690	/* XXX select channel more intelligently, e.g. channel spread, power */
1691	/* NB: use scan list order to preserve channel preference */
1692	for (i = 0; i < ss->ss_last; i++) {
1693		struct ieee80211_channel *chan = ss->ss_chans[i];
1694		/*
1695		 * If the channel is unoccupied the max rssi
1696		 * should be zero; just take it.  Otherwise
1697		 * track the channel with the lowest rssi and
1698		 * use that when all channels appear occupied.
1699		 */
1700		if (IEEE80211_IS_CHAN_RADAR(chan))
1701			continue;
1702		if (IEEE80211_IS_CHAN_NOHOSTAP(chan))
1703			continue;
1704		/* check channel attributes for band compatibility */
1705		if (flags != 0 && (chan->ic_flags & flags) != flags)
1706			continue;
1707		KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size"));
1708		/* XXX channel have interference */
1709		if (st->st_maxrssi[chan->ic_ieee] == 0) {
1710			/* XXX use other considerations */
1711			return chan;
1712		}
1713		if (bestchan == NULL ||
1714		    st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee])
1715			bestchan = chan;
1716	}
1717	return bestchan;
1718}
1719
1720/*
1721 * Pick a quiet channel to use for ap operation.
1722 */
1723static int
1724ap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1725{
1726	struct ieee80211com *ic = vap->iv_ic;
1727	struct ieee80211_channel *bestchan;
1728
1729	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
1730		("wrong opmode %u", vap->iv_opmode));
1731	bestchan = ap_pick_channel(ss, 0);
1732	if (bestchan == NULL) {
1733		/* no suitable channel, should not happen */
1734		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1735		    "%s: no suitable channel! (should not happen)\n", __func__);
1736		/* XXX print something? */
1737		return 0;			/* restart scan */
1738	}
1739	/*
1740	 * If this is a dynamic turbo channel, start with the unboosted one.
1741	 */
1742	if (IEEE80211_IS_CHAN_TURBO(bestchan)) {
1743		bestchan = ieee80211_find_channel(ic, bestchan->ic_freq,
1744			bestchan->ic_flags & ~IEEE80211_CHAN_TURBO);
1745		if (bestchan == NULL) {
1746			/* should never happen ?? */
1747			return 0;
1748		}
1749	}
1750	ap_reset_promisc(ic);
1751	if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) {
1752		/*
1753		 * Manual/background scan, don't select+join the
1754		 * bss, just return.  The scanning framework will
1755		 * handle notification that this has completed.
1756		 */
1757		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1758		return 1;
1759	}
1760	ieee80211_create_ibss(vap,
1761	    ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ext));
1762	return 1;
1763}
1764
1765static const struct ieee80211_scanner ap_default = {
1766	.scan_name		= "default",
1767	.scan_attach		= sta_attach,
1768	.scan_detach		= sta_detach,
1769	.scan_start		= ap_start,
1770	.scan_restart		= sta_restart,
1771	.scan_cancel		= ap_cancel,
1772	.scan_end		= ap_end,
1773	.scan_flush		= sta_flush,
1774	.scan_pickchan		= ap_pick_channel,
1775	.scan_add		= sta_add,
1776	.scan_age		= adhoc_age,
1777	.scan_iterate		= sta_iterate,
1778	.scan_assoc_success	= sta_assoc_success,
1779	.scan_assoc_fail	= sta_assoc_fail,
1780};
1781
1782/*
1783 * Module glue.
1784 */
1785IEEE80211_SCANNER_MODULE(sta, 1);
1786IEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default);
1787IEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default);
1788IEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default);
1789IEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
1790