xref: /freebsd/sys/net80211/ieee80211_scan_sta.c (revision ef48d4fa)
168e8e04eSSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
410ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
568e8e04eSSam Leffler  * All rights reserved.
668e8e04eSSam Leffler  *
768e8e04eSSam Leffler  * Redistribution and use in source and binary forms, with or without
868e8e04eSSam Leffler  * modification, are permitted provided that the following conditions
968e8e04eSSam Leffler  * are met:
1068e8e04eSSam Leffler  * 1. Redistributions of source code must retain the above copyright
1168e8e04eSSam Leffler  *    notice, this list of conditions and the following disclaimer.
1268e8e04eSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
1368e8e04eSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
1468e8e04eSSam Leffler  *    documentation and/or other materials provided with the distribution.
1568e8e04eSSam Leffler  *
1668e8e04eSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1768e8e04eSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1868e8e04eSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1968e8e04eSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2068e8e04eSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2168e8e04eSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2268e8e04eSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2368e8e04eSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2468e8e04eSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2568e8e04eSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2668e8e04eSSam Leffler  */
2768e8e04eSSam Leffler 
2868e8e04eSSam Leffler #include <sys/cdefs.h>
2968e8e04eSSam Leffler /*
3068e8e04eSSam Leffler  * IEEE 802.11 station scanning support.
3168e8e04eSSam Leffler  */
32b032f27cSSam Leffler #include "opt_wlan.h"
33b032f27cSSam Leffler 
3468e8e04eSSam Leffler #include <sys/param.h>
3568e8e04eSSam Leffler #include <sys/systm.h>
3668e8e04eSSam Leffler #include <sys/kernel.h>
3731021a2bSAndriy Voskoboinyk #include <sys/endian.h>
388ec07310SGleb Smirnoff #include <sys/malloc.h>
3968e8e04eSSam Leffler #include <sys/module.h>
4068e8e04eSSam Leffler 
4168e8e04eSSam Leffler #include <sys/socket.h>
4268e8e04eSSam Leffler 
4368e8e04eSSam Leffler #include <net/if.h>
4476039bc8SGleb Smirnoff #include <net/if_var.h>
4568e8e04eSSam Leffler #include <net/if_media.h>
4668e8e04eSSam Leffler #include <net/ethernet.h>
4768e8e04eSSam Leffler 
4868e8e04eSSam Leffler #include <net80211/ieee80211_var.h>
49b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
50b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
5110ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5210ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5310ad9a77SSam Leffler #endif
5459aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
5559aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
5659aa14a9SRui Paulo #endif
57d71a1f7aSAdrian Chadd #include <net80211/ieee80211_ratectl.h>
5851172f62SAdrian Chadd #include <net80211/ieee80211_vht.h>
5968e8e04eSSam Leffler 
6068e8e04eSSam Leffler #include <net/bpf.h>
6168e8e04eSSam Leffler 
6268e8e04eSSam Leffler /*
6368e8e04eSSam Leffler  * Parameters for managing cache entries:
6468e8e04eSSam Leffler  *
6568e8e04eSSam Leffler  * o a station with STA_FAILS_MAX failures is not considered
6668e8e04eSSam Leffler  *   when picking a candidate
6768e8e04eSSam Leffler  * o a station that hasn't had an update in STA_PURGE_SCANS
6868e8e04eSSam Leffler  *   (background) scans is discarded
6968e8e04eSSam Leffler  * o after STA_FAILS_AGE seconds we clear the failure count
7068e8e04eSSam Leffler  */
7168e8e04eSSam Leffler #define	STA_FAILS_MAX	2		/* assoc failures before ignored */
7268e8e04eSSam Leffler #define	STA_FAILS_AGE	(2*60)		/* time before clearing fails (secs) */
7368e8e04eSSam Leffler #define	STA_PURGE_SCANS	2		/* age for purging entries (scans) */
7468e8e04eSSam Leffler 
7568e8e04eSSam Leffler /* XXX tunable */
7668e8e04eSSam Leffler #define	STA_RSSI_MIN	8		/* min acceptable rssi */
7768e8e04eSSam Leffler #define	STA_RSSI_MAX	40		/* max rssi for comparison */
7868e8e04eSSam Leffler 
7968e8e04eSSam Leffler struct sta_entry {
8068e8e04eSSam Leffler 	struct ieee80211_scan_entry base;
8168e8e04eSSam Leffler 	TAILQ_ENTRY(sta_entry) se_list;
8268e8e04eSSam Leffler 	LIST_ENTRY(sta_entry) se_hash;
8368e8e04eSSam Leffler 	uint8_t		se_fails;		/* failure to associate count */
8468e8e04eSSam Leffler 	uint8_t		se_seen;		/* seen during current scan */
8568e8e04eSSam Leffler 	uint8_t		se_notseen;		/* not seen in previous scans */
8668e8e04eSSam Leffler 	uint8_t		se_flags;
878f4addc0SSam Leffler #define	STA_DEMOTE11B	0x01			/* match w/ demoted 11b chan */
8868e8e04eSSam Leffler 	uint32_t	se_avgrssi;		/* LPF rssi state */
8968e8e04eSSam Leffler 	unsigned long	se_lastupdate;		/* time of last update */
9068e8e04eSSam Leffler 	unsigned long	se_lastfail;		/* time of last failure */
9168e8e04eSSam Leffler 	unsigned long	se_lastassoc;		/* time of last association */
9268e8e04eSSam Leffler 	u_int		se_scangen;		/* iterator scan gen# */
93b032f27cSSam Leffler 	u_int		se_countrygen;		/* gen# of last cc notify */
9468e8e04eSSam Leffler };
9568e8e04eSSam Leffler 
9668e8e04eSSam Leffler #define	STA_HASHSIZE	32
9768e8e04eSSam Leffler /* simple hash is enough for variation of macaddr */
9868e8e04eSSam Leffler #define	STA_HASH(addr)	\
9968e8e04eSSam Leffler 	(((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % STA_HASHSIZE)
10068e8e04eSSam Leffler 
10131378b1cSSam Leffler #define	MAX_IEEE_CHAN	256			/* max acceptable IEEE chan # */
10231378b1cSSam Leffler CTASSERT(MAX_IEEE_CHAN >= 256);
10331378b1cSSam Leffler 
10468e8e04eSSam Leffler struct sta_table {
10520c3b3faSRui Paulo 	ieee80211_scan_table_lock_t st_lock;	/* on scan table */
10668e8e04eSSam Leffler 	TAILQ_HEAD(, sta_entry) st_entry;	/* all entries */
10768e8e04eSSam Leffler 	LIST_HEAD(, sta_entry) st_hash[STA_HASHSIZE];
108b6b2fb59SAdrian Chadd 	ieee80211_scan_iter_lock_t st_scanlock;		/* on st_scaniter */
109b032f27cSSam Leffler 	u_int		st_scaniter;		/* gen# for iterator */
110b032f27cSSam Leffler 	u_int		st_scangen;		/* scan generation # */
11168e8e04eSSam Leffler 	int		st_newscan;
112b032f27cSSam Leffler 	/* ap-related state */
11331378b1cSSam Leffler 	int		st_maxrssi[MAX_IEEE_CHAN];
11468e8e04eSSam Leffler };
11568e8e04eSSam Leffler 
11668e8e04eSSam Leffler static void sta_flush_table(struct sta_table *);
117c4ed2c08SSam Leffler /*
118c4ed2c08SSam Leffler  * match_bss returns a bitmask describing if an entry is suitable
119c4ed2c08SSam Leffler  * for use.  If non-zero the entry was deemed not suitable and it's
120caa7e52fSEitan Adler  * contents explains why.  The following flags are or'd to this
121c4ed2c08SSam Leffler  * mask and can be used to figure out why the entry was rejected.
122c4ed2c08SSam Leffler  */
12359aa14a9SRui Paulo #define	MATCH_CHANNEL		0x00001	/* channel mismatch */
12459aa14a9SRui Paulo #define	MATCH_CAPINFO		0x00002	/* capabilities mismatch, e.g. no ess */
12559aa14a9SRui Paulo #define	MATCH_PRIVACY		0x00004	/* privacy mismatch */
12659aa14a9SRui Paulo #define	MATCH_RATE		0x00008	/* rate set mismatch */
12759aa14a9SRui Paulo #define	MATCH_SSID		0x00010	/* ssid mismatch */
12859aa14a9SRui Paulo #define	MATCH_BSSID		0x00020	/* bssid mismatch */
12959aa14a9SRui Paulo #define	MATCH_FAILS		0x00040	/* too many failed auth attempts */
13059aa14a9SRui Paulo #define	MATCH_NOTSEEN		0x00080	/* not seen in recent scans */
13159aa14a9SRui Paulo #define	MATCH_RSSI		0x00100	/* rssi deemed too low to use */
13259aa14a9SRui Paulo #define	MATCH_CC		0x00200	/* country code mismatch */
1336dbbec93SAndriy Voskoboinyk #ifdef IEEE80211_SUPPORT_TDMA
13459aa14a9SRui Paulo #define	MATCH_TDMA_NOIE		0x00400	/* no TDMA ie */
13559aa14a9SRui Paulo #define	MATCH_TDMA_NOTMASTER	0x00800	/* not TDMA master */
13659aa14a9SRui Paulo #define	MATCH_TDMA_NOSLOT	0x01000	/* all TDMA slots occupied */
13759aa14a9SRui Paulo #define	MATCH_TDMA_LOCAL	0x02000	/* local address */
13859aa14a9SRui Paulo #define	MATCH_TDMA_VERSION	0x04000	/* protocol version mismatch */
1396dbbec93SAndriy Voskoboinyk #endif
14059aa14a9SRui Paulo #define	MATCH_MESH_NOID		0x10000	/* no MESHID ie */
14159aa14a9SRui Paulo #define	MATCH_MESHID		0x20000	/* meshid mismatch */
142b032f27cSSam Leffler static int match_bss(struct ieee80211vap *,
14368e8e04eSSam Leffler 	const struct ieee80211_scan_state *, struct sta_entry *, int);
144b032f27cSSam Leffler static void adhoc_age(struct ieee80211_scan_state *);
145b032f27cSSam Leffler 
146b032f27cSSam Leffler static __inline int
isocmp(const uint8_t cc1[],const uint8_t cc2[])147b032f27cSSam Leffler isocmp(const uint8_t cc1[], const uint8_t cc2[])
148b032f27cSSam Leffler {
149b032f27cSSam Leffler      return (cc1[0] == cc2[0] && cc1[1] == cc2[1]);
150b032f27cSSam Leffler }
15168e8e04eSSam Leffler 
15268e8e04eSSam Leffler /* number of references from net80211 layer */
15368e8e04eSSam Leffler static	int nrefs = 0;
15459aa14a9SRui Paulo /*
15559aa14a9SRui Paulo  * Module glue.
15659aa14a9SRui Paulo  */
15759aa14a9SRui Paulo IEEE80211_SCANNER_MODULE(sta, 1);
15868e8e04eSSam Leffler 
15968e8e04eSSam Leffler /*
16068e8e04eSSam Leffler  * Attach prior to any scanning work.
16168e8e04eSSam Leffler  */
16268e8e04eSSam Leffler static int
sta_attach(struct ieee80211_scan_state * ss)16368e8e04eSSam Leffler sta_attach(struct ieee80211_scan_state *ss)
16468e8e04eSSam Leffler {
16568e8e04eSSam Leffler 	struct sta_table *st;
16668e8e04eSSam Leffler 
167b9b53389SAdrian Chadd 	st = (struct sta_table *) IEEE80211_MALLOC(sizeof(struct sta_table),
168b9b53389SAdrian Chadd 		M_80211_SCAN,
169b9b53389SAdrian Chadd 		IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
17068e8e04eSSam Leffler 	if (st == NULL)
17168e8e04eSSam Leffler 		return 0;
17220c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK_INIT(st, "scantable");
173b6b2fb59SAdrian Chadd 	IEEE80211_SCAN_ITER_LOCK_INIT(st, "scangen");
17468e8e04eSSam Leffler 	TAILQ_INIT(&st->st_entry);
17568e8e04eSSam Leffler 	ss->ss_priv = st;
17668e8e04eSSam Leffler 	nrefs++;			/* NB: we assume caller locking */
17768e8e04eSSam Leffler 	return 1;
17868e8e04eSSam Leffler }
17968e8e04eSSam Leffler 
18068e8e04eSSam Leffler /*
18168e8e04eSSam Leffler  * Cleanup any private state.
18268e8e04eSSam Leffler  */
18368e8e04eSSam Leffler static int
sta_detach(struct ieee80211_scan_state * ss)18468e8e04eSSam Leffler sta_detach(struct ieee80211_scan_state *ss)
18568e8e04eSSam Leffler {
18668e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
18768e8e04eSSam Leffler 
18868e8e04eSSam Leffler 	if (st != NULL) {
18968e8e04eSSam Leffler 		sta_flush_table(st);
19020c3b3faSRui Paulo 		IEEE80211_SCAN_TABLE_LOCK_DESTROY(st);
191b6b2fb59SAdrian Chadd 		IEEE80211_SCAN_ITER_LOCK_DESTROY(st);
192b9b53389SAdrian Chadd 		IEEE80211_FREE(st, M_80211_SCAN);
19368e8e04eSSam Leffler 		KASSERT(nrefs > 0, ("imbalanced attach/detach"));
19468e8e04eSSam Leffler 		nrefs--;		/* NB: we assume caller locking */
19568e8e04eSSam Leffler 	}
19668e8e04eSSam Leffler 	return 1;
19768e8e04eSSam Leffler }
19868e8e04eSSam Leffler 
19968e8e04eSSam Leffler /*
20068e8e04eSSam Leffler  * Flush all per-scan state.
20168e8e04eSSam Leffler  */
20268e8e04eSSam Leffler static int
sta_flush(struct ieee80211_scan_state * ss)20368e8e04eSSam Leffler sta_flush(struct ieee80211_scan_state *ss)
20468e8e04eSSam Leffler {
20568e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
20668e8e04eSSam Leffler 
20720c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
20868e8e04eSSam Leffler 	sta_flush_table(st);
20920c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
21068e8e04eSSam Leffler 	ss->ss_last = 0;
21168e8e04eSSam Leffler 	return 0;
21268e8e04eSSam Leffler }
21368e8e04eSSam Leffler 
21468e8e04eSSam Leffler /*
21568e8e04eSSam Leffler  * Flush all entries in the scan cache.
21668e8e04eSSam Leffler  */
21768e8e04eSSam Leffler static void
sta_flush_table(struct sta_table * st)21868e8e04eSSam Leffler sta_flush_table(struct sta_table *st)
21968e8e04eSSam Leffler {
22068e8e04eSSam Leffler 	struct sta_entry *se, *next;
22168e8e04eSSam Leffler 
22268e8e04eSSam Leffler 	TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
22368e8e04eSSam Leffler 		TAILQ_REMOVE(&st->st_entry, se, se_list);
22468e8e04eSSam Leffler 		LIST_REMOVE(se, se_hash);
225b032f27cSSam Leffler 		ieee80211_ies_cleanup(&se->base.se_ies);
226b9b53389SAdrian Chadd 		IEEE80211_FREE(se, M_80211_SCAN);
22768e8e04eSSam Leffler 	}
228b032f27cSSam Leffler 	memset(st->st_maxrssi, 0, sizeof(st->st_maxrssi));
22968e8e04eSSam Leffler }
23068e8e04eSSam Leffler 
23168e8e04eSSam Leffler /*
23268e8e04eSSam Leffler  * Process a beacon or probe response frame; create an
23368e8e04eSSam Leffler  * entry in the scan cache or update any previous entry.
23468e8e04eSSam Leffler  */
23568e8e04eSSam Leffler static int
sta_add(struct ieee80211_scan_state * ss,struct ieee80211_channel * curchan,const struct ieee80211_scanparams * sp,const struct ieee80211_frame * wh,int subtype,int rssi,int noise)23668e8e04eSSam Leffler sta_add(struct ieee80211_scan_state *ss,
2372808a02bSAdrian Chadd 	struct ieee80211_channel *curchan,
23868e8e04eSSam Leffler 	const struct ieee80211_scanparams *sp,
23968e8e04eSSam Leffler 	const struct ieee80211_frame *wh,
2405463c4a4SSam Leffler 	int subtype, int rssi, int noise)
24168e8e04eSSam Leffler {
24268e8e04eSSam Leffler #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
24368e8e04eSSam Leffler #define	PICK1ST(_ss) \
24468e8e04eSSam Leffler 	((ss->ss_flags & (IEEE80211_SCAN_PICK1ST | IEEE80211_SCAN_GOTPICK)) == \
24568e8e04eSSam Leffler 	IEEE80211_SCAN_PICK1ST)
24668e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
24768e8e04eSSam Leffler 	const uint8_t *macaddr = wh->i_addr2;
248b032f27cSSam Leffler 	struct ieee80211vap *vap = ss->ss_vap;
249b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
250d4b82f4dSBernhard Schmidt 	struct ieee80211_channel *c;
25168e8e04eSSam Leffler 	struct sta_entry *se;
25268e8e04eSSam Leffler 	struct ieee80211_scan_entry *ise;
253b032f27cSSam Leffler 	int hash;
25468e8e04eSSam Leffler 
25568e8e04eSSam Leffler 	hash = STA_HASH(macaddr);
25668e8e04eSSam Leffler 
25720c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
25868e8e04eSSam Leffler 	LIST_FOREACH(se, &st->st_hash[hash], se_hash)
25968e8e04eSSam Leffler 		if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
26068e8e04eSSam Leffler 			goto found;
261b9b53389SAdrian Chadd 	se = (struct sta_entry *) IEEE80211_MALLOC(sizeof(struct sta_entry),
262b9b53389SAdrian Chadd 		M_80211_SCAN, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
26368e8e04eSSam Leffler 	if (se == NULL) {
26420c3b3faSRui Paulo 		IEEE80211_SCAN_TABLE_UNLOCK(st);
26568e8e04eSSam Leffler 		return 0;
26668e8e04eSSam Leffler 	}
267b032f27cSSam Leffler 	se->se_scangen = st->st_scaniter-1;
268b032f27cSSam Leffler 	se->se_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
26968e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(se->base.se_macaddr, macaddr);
27068e8e04eSSam Leffler 	TAILQ_INSERT_TAIL(&st->st_entry, se, se_list);
27168e8e04eSSam Leffler 	LIST_INSERT_HEAD(&st->st_hash[hash], se, se_hash);
27268e8e04eSSam Leffler found:
27368e8e04eSSam Leffler 	ise = &se->base;
27468e8e04eSSam Leffler 	/* XXX ap beaconing multiple ssid w/ same bssid */
27568e8e04eSSam Leffler 	if (sp->ssid[1] != 0 &&
27668e8e04eSSam Leffler 	    (ISPROBE(subtype) || ise->se_ssid[1] == 0))
27768e8e04eSSam Leffler 		memcpy(ise->se_ssid, sp->ssid, 2+sp->ssid[1]);
27868e8e04eSSam Leffler 	KASSERT(sp->rates[1] <= IEEE80211_RATE_MAXSIZE,
27968e8e04eSSam Leffler 		("rate set too large: %u", sp->rates[1]));
28068e8e04eSSam Leffler 	memcpy(ise->se_rates, sp->rates, 2+sp->rates[1]);
28168e8e04eSSam Leffler 	if (sp->xrates != NULL) {
28268e8e04eSSam Leffler 		/* XXX validate xrates[1] */
283b032f27cSSam Leffler 		KASSERT(sp->xrates[1] <= IEEE80211_RATE_MAXSIZE,
28468e8e04eSSam Leffler 			("xrate set too large: %u", sp->xrates[1]));
28568e8e04eSSam Leffler 		memcpy(ise->se_xrates, sp->xrates, 2+sp->xrates[1]);
28668e8e04eSSam Leffler 	} else
28768e8e04eSSam Leffler 		ise->se_xrates[1] = 0;
28868e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ise->se_bssid, wh->i_addr3);
289b032f27cSSam Leffler 	if ((sp->status & IEEE80211_BPARSE_OFFCHAN) == 0) {
29068e8e04eSSam Leffler 		/*
29168e8e04eSSam Leffler 		 * Record rssi data using extended precision LPF filter.
2925773d80eSSam Leffler 		 *
2935773d80eSSam Leffler 		 * NB: use only on-channel data to insure we get a good
2945773d80eSSam Leffler 		 *     estimate of the signal we'll see when associated.
29568e8e04eSSam Leffler 		 */
296b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(se->se_avgrssi, rssi);
297b032f27cSSam Leffler 		ise->se_rssi = IEEE80211_RSSI_GET(se->se_avgrssi);
2985773d80eSSam Leffler 		ise->se_noise = noise;
2995773d80eSSam Leffler 	}
30068e8e04eSSam Leffler 	memcpy(ise->se_tstamp.data, sp->tstamp, sizeof(ise->se_tstamp));
30168e8e04eSSam Leffler 	ise->se_intval = sp->bintval;
30268e8e04eSSam Leffler 	ise->se_capinfo = sp->capinfo;
30359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
30459aa14a9SRui Paulo 	if (sp->meshid != NULL && sp->meshid[1] != 0)
30559aa14a9SRui Paulo 		memcpy(ise->se_meshid, sp->meshid, 2+sp->meshid[1]);
30659aa14a9SRui Paulo #endif
3075773d80eSSam Leffler 	/*
3085773d80eSSam Leffler 	 * Beware of overriding se_chan for frames seen
3095773d80eSSam Leffler 	 * off-channel; this can cause us to attempt an
310b032f27cSSam Leffler 	 * association on the wrong channel.
3115773d80eSSam Leffler 	 */
312b032f27cSSam Leffler 	if (sp->status & IEEE80211_BPARSE_OFFCHAN) {
3135773d80eSSam Leffler 		/*
3148039c240SSam Leffler 		 * Off-channel, locate the home/bss channel for the sta
315b032f27cSSam Leffler 		 * using the value broadcast in the DSPARMS ie.  We know
316b032f27cSSam Leffler 		 * sp->chan has this value because it's used to calculate
317b032f27cSSam Leffler 		 * IEEE80211_BPARSE_OFFCHAN.
3185773d80eSSam Leffler 		 */
319b032f27cSSam Leffler 		c = ieee80211_find_channel_byieee(ic, sp->chan,
3202808a02bSAdrian Chadd 		    curchan->ic_flags);
3211a4ae5c4SSam Leffler 		if (c != NULL) {
3228039c240SSam Leffler 			ise->se_chan = c;
3231a4ae5c4SSam Leffler 		} else if (ise->se_chan == NULL) {
3241a4ae5c4SSam Leffler 			/* should not happen, pick something */
3252808a02bSAdrian Chadd 			ise->se_chan = curchan;
3261a4ae5c4SSam Leffler 		}
3278039c240SSam Leffler 	} else
3282808a02bSAdrian Chadd 		ise->se_chan = curchan;
32951172f62SAdrian Chadd 
33051172f62SAdrian Chadd 	/* VHT demotion */
33151172f62SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(ise->se_chan) && sp->vhtcap == NULL) {
33251172f62SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
33351172f62SAdrian Chadd 		    "%s: demoting VHT->HT %d/0x%08x\n",
33451172f62SAdrian Chadd 		    __func__, ise->se_chan->ic_freq, ise->se_chan->ic_flags);
33551172f62SAdrian Chadd 		/* Demote legacy networks to a non-VHT channel. */
33651172f62SAdrian Chadd 		c = ieee80211_find_channel(ic, ise->se_chan->ic_freq,
33751172f62SAdrian Chadd 		    ise->se_chan->ic_flags & ~IEEE80211_CHAN_VHT);
33851172f62SAdrian Chadd 		KASSERT(c != NULL,
33951172f62SAdrian Chadd 		    ("no non-VHT channel %u", ise->se_chan->ic_ieee));
34051172f62SAdrian Chadd 		ise->se_chan = c;
34151172f62SAdrian Chadd 	}
34251172f62SAdrian Chadd 
34351172f62SAdrian Chadd 	/* HT demotion */
344d4b82f4dSBernhard Schmidt 	if (IEEE80211_IS_CHAN_HT(ise->se_chan) && sp->htcap == NULL) {
345d4b82f4dSBernhard Schmidt 		/* Demote legacy networks to a non-HT channel. */
34651172f62SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
34751172f62SAdrian Chadd 		    "%s: demoting HT->legacy %d/0x%08x\n",
34851172f62SAdrian Chadd 		    __func__, ise->se_chan->ic_freq, ise->se_chan->ic_flags);
349d4b82f4dSBernhard Schmidt 		c = ieee80211_find_channel(ic, ise->se_chan->ic_freq,
350d4b82f4dSBernhard Schmidt 		    ise->se_chan->ic_flags & ~IEEE80211_CHAN_HT);
351d4b82f4dSBernhard Schmidt 		KASSERT(c != NULL,
352d4b82f4dSBernhard Schmidt 		    ("no legacy channel %u", ise->se_chan->ic_ieee));
353d4b82f4dSBernhard Schmidt 		ise->se_chan = c;
354d4b82f4dSBernhard Schmidt 	}
35551172f62SAdrian Chadd 
35668e8e04eSSam Leffler 	ise->se_fhdwell = sp->fhdwell;
35768e8e04eSSam Leffler 	ise->se_fhindex = sp->fhindex;
35868e8e04eSSam Leffler 	ise->se_erp = sp->erp;
35968e8e04eSSam Leffler 	ise->se_timoff = sp->timoff;
36068e8e04eSSam Leffler 	if (sp->tim != NULL) {
36168e8e04eSSam Leffler 		const struct ieee80211_tim_ie *tim =
36268e8e04eSSam Leffler 		    (const struct ieee80211_tim_ie *) sp->tim;
36368e8e04eSSam Leffler 		ise->se_dtimperiod = tim->tim_period;
36468e8e04eSSam Leffler 	}
365b032f27cSSam Leffler 	if (sp->country != NULL) {
366b032f27cSSam Leffler 		const struct ieee80211_country_ie *cie =
367b032f27cSSam Leffler 		    (const struct ieee80211_country_ie *) sp->country;
368b032f27cSSam Leffler 		/*
369b032f27cSSam Leffler 		 * If 11d is enabled and we're attempting to join a bss
370b032f27cSSam Leffler 		 * that advertises it's country code then compare our
371b032f27cSSam Leffler 		 * current settings to what we fetched from the country ie.
372b032f27cSSam Leffler 		 * If our country code is unspecified or different then
373b032f27cSSam Leffler 		 * dispatch an event to user space that identifies the
374b032f27cSSam Leffler 		 * country code so our regdomain config can be changed.
375b032f27cSSam Leffler 		 */
376b032f27cSSam Leffler 		/* XXX only for STA mode? */
377b032f27cSSam Leffler 		if ((IEEE80211_IS_CHAN_11D(ise->se_chan) ||
378b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
379b032f27cSSam Leffler 		    (ic->ic_regdomain.country == CTRY_DEFAULT ||
380b032f27cSSam Leffler 		     !isocmp(cie->cc, ic->ic_regdomain.isocc))) {
381b032f27cSSam Leffler 			/* only issue one notify event per scan */
382b032f27cSSam Leffler 			if (se->se_countrygen != st->st_scangen) {
383b032f27cSSam Leffler 				ieee80211_notify_country(vap, ise->se_bssid,
384b032f27cSSam Leffler 				    cie->cc);
385b032f27cSSam Leffler 				se->se_countrygen = st->st_scangen;
386b032f27cSSam Leffler 			}
387b032f27cSSam Leffler 		}
388b032f27cSSam Leffler 		ise->se_cc[0] = cie->cc[0];
389b032f27cSSam Leffler 		ise->se_cc[1] = cie->cc[1];
390b032f27cSSam Leffler 	}
391b032f27cSSam Leffler 	/* NB: no need to setup ie ptrs; they are not (currently) used */
392b032f27cSSam Leffler 	(void) ieee80211_ies_init(&ise->se_ies, sp->ies, sp->ies_len);
39368e8e04eSSam Leffler 
39468e8e04eSSam Leffler 	/* clear failure count after STA_FAIL_AGE passes */
39568e8e04eSSam Leffler 	if (se->se_fails && (ticks - se->se_lastfail) > STA_FAILS_AGE*hz) {
39668e8e04eSSam Leffler 		se->se_fails = 0;
397b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_SCAN, macaddr,
39868e8e04eSSam Leffler 		    "%s: fails %u", __func__, se->se_fails);
39968e8e04eSSam Leffler 	}
40068e8e04eSSam Leffler 
40168e8e04eSSam Leffler 	se->se_lastupdate = ticks;		/* update time */
40268e8e04eSSam Leffler 	se->se_seen = 1;
40368e8e04eSSam Leffler 	se->se_notseen = 0;
40468e8e04eSSam Leffler 
40531378b1cSSam Leffler 	KASSERT(sizeof(sp->bchan) == 1, ("bchan size"));
406b032f27cSSam Leffler 	if (rssi > st->st_maxrssi[sp->bchan])
407b032f27cSSam Leffler 		st->st_maxrssi[sp->bchan] = rssi;
408b032f27cSSam Leffler 
40920c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
41068e8e04eSSam Leffler 
41168e8e04eSSam Leffler 	/*
41268e8e04eSSam Leffler 	 * If looking for a quick choice and nothing's
41368e8e04eSSam Leffler 	 * been found check here.
41468e8e04eSSam Leffler 	 */
415b032f27cSSam Leffler 	if (PICK1ST(ss) && match_bss(vap, ss, se, IEEE80211_MSG_SCAN) == 0)
41668e8e04eSSam Leffler 		ss->ss_flags |= IEEE80211_SCAN_GOTPICK;
41768e8e04eSSam Leffler 
41868e8e04eSSam Leffler 	return 1;
41968e8e04eSSam Leffler #undef PICK1ST
42068e8e04eSSam Leffler #undef ISPROBE
42168e8e04eSSam Leffler }
42268e8e04eSSam Leffler 
42368e8e04eSSam Leffler /*
42468e8e04eSSam Leffler  * Check if a channel is excluded by user request.
42568e8e04eSSam Leffler  */
42668e8e04eSSam Leffler static int
isexcluded(struct ieee80211vap * vap,const struct ieee80211_channel * c)427b032f27cSSam Leffler isexcluded(struct ieee80211vap *vap, const struct ieee80211_channel *c)
42868e8e04eSSam Leffler {
429b032f27cSSam Leffler 	return (isclr(vap->iv_ic->ic_chan_active, c->ic_ieee) ||
430b032f27cSSam Leffler 	    (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
431b032f27cSSam Leffler 	     c->ic_freq != vap->iv_des_chan->ic_freq));
43268e8e04eSSam Leffler }
43368e8e04eSSam Leffler 
43468e8e04eSSam Leffler static struct ieee80211_channel *
find11gchannel(struct ieee80211com * ic,int i,int freq)43568e8e04eSSam Leffler find11gchannel(struct ieee80211com *ic, int i, int freq)
43668e8e04eSSam Leffler {
43768e8e04eSSam Leffler 	struct ieee80211_channel *c;
43868e8e04eSSam Leffler 	int j;
43968e8e04eSSam Leffler 
44068e8e04eSSam Leffler 	/*
44168e8e04eSSam Leffler 	 * The normal ordering in the channel list is b channel
44268e8e04eSSam Leffler 	 * immediately followed by g so optimize the search for
44368e8e04eSSam Leffler 	 * this.  We'll still do a full search just in case.
44468e8e04eSSam Leffler 	 */
44568e8e04eSSam Leffler 	for (j = i+1; j < ic->ic_nchans; j++) {
44668e8e04eSSam Leffler 		c = &ic->ic_channels[j];
447a3dfb736SSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
44868e8e04eSSam Leffler 			return c;
44968e8e04eSSam Leffler 	}
45068e8e04eSSam Leffler 	for (j = 0; j < i; j++) {
45168e8e04eSSam Leffler 		c = &ic->ic_channels[j];
452a3dfb736SSam Leffler 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_G(c))
45368e8e04eSSam Leffler 			return c;
45468e8e04eSSam Leffler 	}
45568e8e04eSSam Leffler 	return NULL;
45668e8e04eSSam Leffler }
45759a40225SSam Leffler 
45868e8e04eSSam Leffler static const u_int chanflags[IEEE80211_MODE_MAX] = {
45959a40225SSam Leffler 	[IEEE80211_MODE_AUTO]	  = IEEE80211_CHAN_B,
46059a40225SSam Leffler 	[IEEE80211_MODE_11A]	  = IEEE80211_CHAN_A,
46159a40225SSam Leffler 	[IEEE80211_MODE_11B]	  = IEEE80211_CHAN_B,
46259a40225SSam Leffler 	[IEEE80211_MODE_11G]	  = IEEE80211_CHAN_G,
46359a40225SSam Leffler 	[IEEE80211_MODE_FH]	  = IEEE80211_CHAN_FHSS,
46459a40225SSam Leffler 	/* check base channel */
46559a40225SSam Leffler 	[IEEE80211_MODE_TURBO_A]  = IEEE80211_CHAN_A,
46659a40225SSam Leffler 	[IEEE80211_MODE_TURBO_G]  = IEEE80211_CHAN_G,
46759a40225SSam Leffler 	[IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_ST,
4686a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	  = IEEE80211_CHAN_HALF,
4696a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]  = IEEE80211_CHAN_QUARTER,
47059a40225SSam Leffler 	/* check legacy */
47159a40225SSam Leffler 	[IEEE80211_MODE_11NA]	  = IEEE80211_CHAN_A,
47259a40225SSam Leffler 	[IEEE80211_MODE_11NG]	  = IEEE80211_CHAN_G,
473d514ab89SAndriy Voskoboinyk 	[IEEE80211_MODE_VHT_5GHZ] = IEEE80211_CHAN_A,
474d514ab89SAndriy Voskoboinyk 	[IEEE80211_MODE_VHT_2GHZ] = IEEE80211_CHAN_G,
47568e8e04eSSam Leffler };
47668e8e04eSSam Leffler 
47768e8e04eSSam Leffler static void
add_channels(struct ieee80211vap * vap,struct ieee80211_scan_state * ss,enum ieee80211_phymode mode,const uint16_t freq[],int nfreq)478b032f27cSSam Leffler add_channels(struct ieee80211vap *vap,
47968e8e04eSSam Leffler 	struct ieee80211_scan_state *ss,
48068e8e04eSSam Leffler 	enum ieee80211_phymode mode, const uint16_t freq[], int nfreq)
48168e8e04eSSam Leffler {
482b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
48368e8e04eSSam Leffler 	struct ieee80211_channel *c, *cg;
48468e8e04eSSam Leffler 	u_int modeflags;
48568e8e04eSSam Leffler 	int i;
48668e8e04eSSam Leffler 
487a3e08d6fSRui Paulo 	KASSERT(mode < nitems(chanflags), ("Unexpected mode %u", mode));
48868e8e04eSSam Leffler 	modeflags = chanflags[mode];
48968e8e04eSSam Leffler 	for (i = 0; i < nfreq; i++) {
49068e8e04eSSam Leffler 		if (ss->ss_last >= IEEE80211_SCAN_MAX)
49168e8e04eSSam Leffler 			break;
49268e8e04eSSam Leffler 
49368e8e04eSSam Leffler 		c = ieee80211_find_channel(ic, freq[i], modeflags);
494b032f27cSSam Leffler 		if (c == NULL || isexcluded(vap, c))
49568e8e04eSSam Leffler 			continue;
49668e8e04eSSam Leffler 		if (mode == IEEE80211_MODE_AUTO) {
4977e2bcba4SAndriy Voskoboinyk 			KASSERT(IEEE80211_IS_CHAN_B(c),
4987e2bcba4SAndriy Voskoboinyk 			    ("%s: wrong channel for 'auto' mode %u / %u\n",
4997e2bcba4SAndriy Voskoboinyk 			    __func__, c->ic_freq, c->ic_flags));
5007e2bcba4SAndriy Voskoboinyk 
50168e8e04eSSam Leffler 			/*
50268e8e04eSSam Leffler 			 * XXX special-case 11b/g channels so we select
503b032f27cSSam Leffler 			 *     the g channel if both are present.
50468e8e04eSSam Leffler 			 */
5057e2bcba4SAndriy Voskoboinyk 			if ((cg = find11gchannel(ic, i, c->ic_freq)) != NULL)
50668e8e04eSSam Leffler 				c = cg;
50768e8e04eSSam Leffler 		}
50868e8e04eSSam Leffler 		ss->ss_chans[ss->ss_last++] = c;
50968e8e04eSSam Leffler 	}
51068e8e04eSSam Leffler }
51168e8e04eSSam Leffler 
512b032f27cSSam Leffler struct scanlist {
513b032f27cSSam Leffler 	uint16_t	mode;
514b032f27cSSam Leffler 	uint16_t	count;
515b032f27cSSam Leffler 	const uint16_t	*list;
516b032f27cSSam Leffler };
517b032f27cSSam Leffler 
518b032f27cSSam Leffler static int
checktable(const struct scanlist * scan,const struct ieee80211_channel * c)519b032f27cSSam Leffler checktable(const struct scanlist *scan, const struct ieee80211_channel *c)
520b032f27cSSam Leffler {
521b032f27cSSam Leffler 	int i;
522b032f27cSSam Leffler 
523b032f27cSSam Leffler 	for (; scan->list != NULL; scan++) {
524b032f27cSSam Leffler 		for (i = 0; i < scan->count; i++)
525b032f27cSSam Leffler 			if (scan->list[i] == c->ic_freq)
526b032f27cSSam Leffler 				return 1;
527b032f27cSSam Leffler 	}
528b032f27cSSam Leffler 	return 0;
529b032f27cSSam Leffler }
530b032f27cSSam Leffler 
531645aa556SSam Leffler static int
onscanlist(const struct ieee80211_scan_state * ss,const struct ieee80211_channel * c)532645aa556SSam Leffler onscanlist(const struct ieee80211_scan_state *ss,
533645aa556SSam Leffler 	const struct ieee80211_channel *c)
534645aa556SSam Leffler {
535645aa556SSam Leffler 	int i;
536645aa556SSam Leffler 
537645aa556SSam Leffler 	for (i = 0; i < ss->ss_last; i++)
538645aa556SSam Leffler 		if (ss->ss_chans[i] == c)
539645aa556SSam Leffler 			return 1;
540645aa556SSam Leffler 	return 0;
541645aa556SSam Leffler }
542645aa556SSam Leffler 
543b032f27cSSam Leffler static void
sweepchannels(struct ieee80211_scan_state * ss,struct ieee80211vap * vap,const struct scanlist table[])544b032f27cSSam Leffler sweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
545b032f27cSSam Leffler 	const struct scanlist table[])
546b032f27cSSam Leffler {
547b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
548b032f27cSSam Leffler 	struct ieee80211_channel *c;
549b032f27cSSam Leffler 	int i;
550b032f27cSSam Leffler 
551b032f27cSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
552b032f27cSSam Leffler 		if (ss->ss_last >= IEEE80211_SCAN_MAX)
553b032f27cSSam Leffler 			break;
554b032f27cSSam Leffler 
555b032f27cSSam Leffler 		c = &ic->ic_channels[i];
556b032f27cSSam Leffler 		/*
557b032f27cSSam Leffler 		 * Ignore dynamic turbo channels; we scan them
558b032f27cSSam Leffler 		 * in normal mode (i.e. not boosted).  Likewise
55951172f62SAdrian Chadd 		 * for HT/VHT channels, they get scanned using
560b032f27cSSam Leffler 		 * legacy rates.
561b032f27cSSam Leffler 		 */
56251172f62SAdrian Chadd 		if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c) ||
56351172f62SAdrian Chadd 		    IEEE80211_IS_CHAN_VHT(c))
564b032f27cSSam Leffler 			continue;
565b032f27cSSam Leffler 
566b032f27cSSam Leffler 		/*
567b032f27cSSam Leffler 		 * If a desired mode was specified, scan only
568b032f27cSSam Leffler 		 * channels that satisfy that constraint.
569b032f27cSSam Leffler 		 */
570b032f27cSSam Leffler 		if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
571b032f27cSSam Leffler 		    vap->iv_des_mode != ieee80211_chan2mode(c))
572b032f27cSSam Leffler 			continue;
573b032f27cSSam Leffler 
574b032f27cSSam Leffler 		/*
575b032f27cSSam Leffler 		 * Skip channels excluded by user request.
576b032f27cSSam Leffler 		 */
577b032f27cSSam Leffler 		if (isexcluded(vap, c))
578b032f27cSSam Leffler 			continue;
579b032f27cSSam Leffler 
580b032f27cSSam Leffler 		/*
581b032f27cSSam Leffler 		 * Add the channel unless it is listed in the
582b032f27cSSam Leffler 		 * fixed scan order tables.  This insures we
583b032f27cSSam Leffler 		 * don't sweep back in channels we filtered out
584b032f27cSSam Leffler 		 * above.
585b032f27cSSam Leffler 		 */
586b032f27cSSam Leffler 		if (checktable(table, c))
587b032f27cSSam Leffler 			continue;
588b032f27cSSam Leffler 
589b032f27cSSam Leffler 		/* Add channel to scanning list. */
590b032f27cSSam Leffler 		ss->ss_chans[ss->ss_last++] = c;
591b032f27cSSam Leffler 	}
592645aa556SSam Leffler 	/*
593645aa556SSam Leffler 	 * Explicitly add any desired channel if:
594645aa556SSam Leffler 	 * - not already on the scan list
595645aa556SSam Leffler 	 * - allowed by any desired mode constraint
596645aa556SSam Leffler 	 * - there is space in the scan list
597645aa556SSam Leffler 	 * This allows the channel to be used when the filtering
598645aa556SSam Leffler 	 * mechanisms would otherwise elide it (e.g HT, turbo).
599645aa556SSam Leffler 	 */
600645aa556SSam Leffler 	c = vap->iv_des_chan;
601645aa556SSam Leffler 	if (c != IEEE80211_CHAN_ANYC &&
602645aa556SSam Leffler 	    !onscanlist(ss, c) &&
603645aa556SSam Leffler 	    (vap->iv_des_mode == IEEE80211_MODE_AUTO ||
604645aa556SSam Leffler 	     vap->iv_des_mode == ieee80211_chan2mode(c)) &&
605645aa556SSam Leffler 	    ss->ss_last < IEEE80211_SCAN_MAX)
606645aa556SSam Leffler 		ss->ss_chans[ss->ss_last++] = c;
607b032f27cSSam Leffler }
608b032f27cSSam Leffler 
609b032f27cSSam Leffler static void
makescanlist(struct ieee80211_scan_state * ss,struct ieee80211vap * vap,const struct scanlist table[])610b032f27cSSam Leffler makescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap,
611b032f27cSSam Leffler 	const struct scanlist table[])
612b032f27cSSam Leffler {
613b032f27cSSam Leffler 	const struct scanlist *scan;
614b032f27cSSam Leffler 	enum ieee80211_phymode mode;
615b032f27cSSam Leffler 
616b032f27cSSam Leffler 	ss->ss_last = 0;
617b032f27cSSam Leffler 	/*
618b032f27cSSam Leffler 	 * Use the table of ordered channels to construct the list
619b032f27cSSam Leffler 	 * of channels for scanning.  Any channels in the ordered
620b032f27cSSam Leffler 	 * list not in the master list will be discarded.
621b032f27cSSam Leffler 	 */
622b032f27cSSam Leffler 	for (scan = table; scan->list != NULL; scan++) {
623b032f27cSSam Leffler 		mode = scan->mode;
624d514ab89SAndriy Voskoboinyk 
625d514ab89SAndriy Voskoboinyk 		switch (mode) {
626d514ab89SAndriy Voskoboinyk 		case IEEE80211_MODE_11B:
627d514ab89SAndriy Voskoboinyk 			if (vap->iv_des_mode == IEEE80211_MODE_11B)
628d514ab89SAndriy Voskoboinyk 				break;
629d514ab89SAndriy Voskoboinyk 
630d514ab89SAndriy Voskoboinyk 			/*
631d514ab89SAndriy Voskoboinyk 			 * The scan table marks 2.4Ghz channels as b
632d514ab89SAndriy Voskoboinyk 			 * so if the desired mode is 11g / 11ng / 11acg,
633d514ab89SAndriy Voskoboinyk 			 * then use the 11b channel list but upgrade the mode.
634d514ab89SAndriy Voskoboinyk 			 *
635d514ab89SAndriy Voskoboinyk 			 * NB: 11b -> AUTO lets add_channels upgrade an
636d514ab89SAndriy Voskoboinyk 			 * 11b channel to 11g if available.
637d514ab89SAndriy Voskoboinyk 			 */
638d514ab89SAndriy Voskoboinyk 			if (vap->iv_des_mode == IEEE80211_MODE_AUTO ||
639d514ab89SAndriy Voskoboinyk 			    vap->iv_des_mode == IEEE80211_MODE_11G ||
640d514ab89SAndriy Voskoboinyk 			    vap->iv_des_mode == IEEE80211_MODE_11NG ||
641d514ab89SAndriy Voskoboinyk 			    vap->iv_des_mode == IEEE80211_MODE_VHT_2GHZ) {
642d514ab89SAndriy Voskoboinyk 				mode = vap->iv_des_mode;
643d514ab89SAndriy Voskoboinyk 				break;
644d514ab89SAndriy Voskoboinyk 			}
645d514ab89SAndriy Voskoboinyk 
646d514ab89SAndriy Voskoboinyk 			continue;
647d514ab89SAndriy Voskoboinyk 		case IEEE80211_MODE_11A:
648d514ab89SAndriy Voskoboinyk 			/* Use 11a channel list for 11na / 11ac modes */
649d514ab89SAndriy Voskoboinyk 			if (vap->iv_des_mode == IEEE80211_MODE_11NA ||
650d514ab89SAndriy Voskoboinyk 			    vap->iv_des_mode == IEEE80211_MODE_VHT_5GHZ) {
651d514ab89SAndriy Voskoboinyk 				mode = vap->iv_des_mode;
652d514ab89SAndriy Voskoboinyk 				break;
653d514ab89SAndriy Voskoboinyk 			}
654d514ab89SAndriy Voskoboinyk 
655d514ab89SAndriy Voskoboinyk 			/* FALLTHROUGH */
656d514ab89SAndriy Voskoboinyk 		default:
657b032f27cSSam Leffler 			/*
658b032f27cSSam Leffler 			 * If a desired mode was specified, scan only
659b032f27cSSam Leffler 			 * channels that satisfy that constraint.
660b032f27cSSam Leffler 			 */
661d514ab89SAndriy Voskoboinyk 			if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
662d514ab89SAndriy Voskoboinyk 			    vap->iv_des_mode != mode)
663b032f27cSSam Leffler 				continue;
664b032f27cSSam Leffler 		}
665d514ab89SAndriy Voskoboinyk 
666b032f27cSSam Leffler #ifdef IEEE80211_F_XR
667b032f27cSSam Leffler 		/* XR does not operate on turbo channels */
668b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_XR) &&
669b032f27cSSam Leffler 		    (mode == IEEE80211_MODE_TURBO_A ||
670b032f27cSSam Leffler 		     mode == IEEE80211_MODE_TURBO_G ||
671b032f27cSSam Leffler 		     mode == IEEE80211_MODE_STURBO_A))
672b032f27cSSam Leffler 			continue;
673b032f27cSSam Leffler #endif
674b032f27cSSam Leffler 		/*
675b032f27cSSam Leffler 		 * Add the list of the channels; any that are not
676b032f27cSSam Leffler 		 * in the master channel list will be discarded.
677b032f27cSSam Leffler 		 */
678b032f27cSSam Leffler 		add_channels(vap, ss, mode, scan->list, scan->count);
679b032f27cSSam Leffler 	}
680b032f27cSSam Leffler 
681b032f27cSSam Leffler 	/*
682b032f27cSSam Leffler 	 * Add the channels from the ic that are not present
683b032f27cSSam Leffler 	 * in the table.
684b032f27cSSam Leffler 	 */
685b032f27cSSam Leffler 	sweepchannels(ss, vap, table);
686b032f27cSSam Leffler }
687b032f27cSSam Leffler 
68868e8e04eSSam Leffler static const uint16_t rcl1[] =		/* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */
68968e8e04eSSam Leffler { 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };
69068e8e04eSSam Leffler static const uint16_t rcl2[] =		/* 4 MKK channels: 34, 38, 42, 46 */
69168e8e04eSSam Leffler { 5170, 5190, 5210, 5230 };
69268e8e04eSSam Leffler static const uint16_t rcl3[] =		/* 2.4Ghz ch: 1,6,11,7,13 */
69368e8e04eSSam Leffler { 2412, 2437, 2462, 2442, 2472 };
69468e8e04eSSam Leffler static const uint16_t rcl4[] =		/* 5 FCC channel: 149, 153, 161, 165 */
69568e8e04eSSam Leffler { 5745, 5765, 5785, 5805, 5825 };
69668e8e04eSSam Leffler static const uint16_t rcl7[] =		/* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */
69768e8e04eSSam Leffler { 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };
69868e8e04eSSam Leffler static const uint16_t rcl8[] =		/* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */
69968e8e04eSSam Leffler { 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };
70068e8e04eSSam Leffler static const uint16_t rcl9[] =		/* 2.4Ghz ch: 14 */
70168e8e04eSSam Leffler { 2484 };
70268e8e04eSSam Leffler static const uint16_t rcl10[] =	/* Added Korean channels 2312-2372 */
70368e8e04eSSam Leffler { 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };
70468e8e04eSSam Leffler static const uint16_t rcl11[] =	/* Added Japan channels in 4.9/5.0 spectrum */
70568e8e04eSSam Leffler { 5040, 5060, 5080, 4920, 4940, 4960, 4980 };
70668e8e04eSSam Leffler #ifdef ATH_TURBO_SCAN
70768e8e04eSSam Leffler static const uint16_t rcl5[] =		/* 3 static turbo channels */
70868e8e04eSSam Leffler { 5210, 5250, 5290 };
70968e8e04eSSam Leffler static const uint16_t rcl6[] =		/* 2 static turbo channels */
71068e8e04eSSam Leffler { 5760, 5800 };
71168e8e04eSSam Leffler static const uint16_t rcl6x[] =	/* 4 FCC3 turbo channels */
71268e8e04eSSam Leffler { 5540, 5580, 5620, 5660 };
71368e8e04eSSam Leffler static const uint16_t rcl12[] =	/* 2.4Ghz Turbo channel 6 */
71468e8e04eSSam Leffler { 2437 };
71568e8e04eSSam Leffler static const uint16_t rcl13[] =	/* dynamic Turbo channels */
71668e8e04eSSam Leffler { 5200, 5240, 5280, 5765, 5805 };
71768e8e04eSSam Leffler #endif /* ATH_TURBO_SCAN */
71868e8e04eSSam Leffler 
71968e8e04eSSam Leffler #define	X(a)	.count = sizeof(a)/sizeof(a[0]), .list = a
72068e8e04eSSam Leffler 
72168e8e04eSSam Leffler static const struct scanlist staScanTable[] = {
72268e8e04eSSam Leffler 	{ IEEE80211_MODE_11B,   	X(rcl3) },
72368e8e04eSSam Leffler 	{ IEEE80211_MODE_11A,   	X(rcl1) },
72468e8e04eSSam Leffler 	{ IEEE80211_MODE_11A,   	X(rcl2) },
72568e8e04eSSam Leffler 	{ IEEE80211_MODE_11B,   	X(rcl8) },
72668e8e04eSSam Leffler 	{ IEEE80211_MODE_11B,   	X(rcl9) },
72768e8e04eSSam Leffler 	{ IEEE80211_MODE_11A,   	X(rcl4) },
72868e8e04eSSam Leffler #ifdef ATH_TURBO_SCAN
72968e8e04eSSam Leffler 	{ IEEE80211_MODE_STURBO_A,	X(rcl5) },
73068e8e04eSSam Leffler 	{ IEEE80211_MODE_STURBO_A,	X(rcl6) },
73168e8e04eSSam Leffler 	{ IEEE80211_MODE_TURBO_A,	X(rcl6x) },
73268e8e04eSSam Leffler 	{ IEEE80211_MODE_TURBO_A,	X(rcl13) },
73368e8e04eSSam Leffler #endif /* ATH_TURBO_SCAN */
73468e8e04eSSam Leffler 	{ IEEE80211_MODE_11A,		X(rcl7) },
73568e8e04eSSam Leffler 	{ IEEE80211_MODE_11B,		X(rcl10) },
73668e8e04eSSam Leffler 	{ IEEE80211_MODE_11A,		X(rcl11) },
73768e8e04eSSam Leffler #ifdef ATH_TURBO_SCAN
73868e8e04eSSam Leffler 	{ IEEE80211_MODE_TURBO_G,	X(rcl12) },
73968e8e04eSSam Leffler #endif /* ATH_TURBO_SCAN */
74068e8e04eSSam Leffler 	{ .list = NULL }
74168e8e04eSSam Leffler };
74268e8e04eSSam Leffler 
74368e8e04eSSam Leffler /*
74468e8e04eSSam Leffler  * Start a station-mode scan by populating the channel list.
74568e8e04eSSam Leffler  */
74668e8e04eSSam Leffler static int
sta_start(struct ieee80211_scan_state * ss,struct ieee80211vap * vap)747b032f27cSSam Leffler sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
74868e8e04eSSam Leffler {
74968e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
75068e8e04eSSam Leffler 
751b032f27cSSam Leffler 	makescanlist(ss, vap, staScanTable);
75268e8e04eSSam Leffler 
753b032f27cSSam Leffler 	if (ss->ss_mindwell == 0)
75468e8e04eSSam Leffler 		ss->ss_mindwell = msecs_to_ticks(20);	/* 20ms */
755b032f27cSSam Leffler 	if (ss->ss_maxdwell == 0)
75668e8e04eSSam Leffler 		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
75768e8e04eSSam Leffler 
758b032f27cSSam Leffler 	st->st_scangen++;
75968e8e04eSSam Leffler 	st->st_newscan = 1;
76068e8e04eSSam Leffler 
76168e8e04eSSam Leffler 	return 0;
76268e8e04eSSam Leffler }
76368e8e04eSSam Leffler 
76468e8e04eSSam Leffler /*
765b032f27cSSam Leffler  * Restart a scan, typically a bg scan but can
766b032f27cSSam Leffler  * also be a fg scan that came up empty.
76768e8e04eSSam Leffler  */
76868e8e04eSSam Leffler static int
sta_restart(struct ieee80211_scan_state * ss,struct ieee80211vap * vap)769b032f27cSSam Leffler sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
77068e8e04eSSam Leffler {
77168e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
77268e8e04eSSam Leffler 
77368e8e04eSSam Leffler 	st->st_newscan = 1;
77468e8e04eSSam Leffler 	return 0;
77568e8e04eSSam Leffler }
77668e8e04eSSam Leffler 
77768e8e04eSSam Leffler /*
77868e8e04eSSam Leffler  * Cancel an ongoing scan.
77968e8e04eSSam Leffler  */
78068e8e04eSSam Leffler static int
sta_cancel(struct ieee80211_scan_state * ss,struct ieee80211vap * vap)781b032f27cSSam Leffler sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
78268e8e04eSSam Leffler {
78368e8e04eSSam Leffler 	return 0;
78468e8e04eSSam Leffler }
78568e8e04eSSam Leffler 
7868f4addc0SSam Leffler /*
7878f4addc0SSam Leffler  * Demote any supplied 11g channel to 11b.  There should
7888f4addc0SSam Leffler  * always be an 11b channel but we check anyway...
7898f4addc0SSam Leffler  */
7908f4addc0SSam Leffler static struct ieee80211_channel *
demote11b(struct ieee80211vap * vap,struct ieee80211_channel * chan)7918f4addc0SSam Leffler demote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan)
7928f4addc0SSam Leffler {
7938f4addc0SSam Leffler 	struct ieee80211_channel *c;
7948f4addc0SSam Leffler 
7958f4addc0SSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(chan) &&
7968f4addc0SSam Leffler 	    vap->iv_des_mode == IEEE80211_MODE_AUTO) {
7978f4addc0SSam Leffler 		c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq,
7988f4addc0SSam Leffler 		    (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) |
7998f4addc0SSam Leffler 		    IEEE80211_CHAN_B);
8008f4addc0SSam Leffler 		if (c != NULL)
8018f4addc0SSam Leffler 			chan = c;
8028f4addc0SSam Leffler 	}
8038f4addc0SSam Leffler 	return chan;
8048f4addc0SSam Leffler }
8058f4addc0SSam Leffler 
806b032f27cSSam Leffler static int
maxrate(const struct ieee80211_scan_entry * se)80768e8e04eSSam Leffler maxrate(const struct ieee80211_scan_entry *se)
80868e8e04eSSam Leffler {
809b032f27cSSam Leffler 	const struct ieee80211_ie_htcap *htcap =
810b032f27cSSam Leffler 	    (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie;
811759c594dSBernhard Schmidt 	int rmax, r, i, txstream;
812b032f27cSSam Leffler 	uint16_t caps;
813759c594dSBernhard Schmidt 	uint8_t txparams;
81468e8e04eSSam Leffler 
81568e8e04eSSam Leffler 	rmax = 0;
816b032f27cSSam Leffler 	if (htcap != NULL) {
817b032f27cSSam Leffler 		/*
818b032f27cSSam Leffler 		 * HT station; inspect supported MCS and then adjust
819759c594dSBernhard Schmidt 		 * rate by channel width.
820b032f27cSSam Leffler 		 */
821759c594dSBernhard Schmidt 		txparams = htcap->hc_mcsset[12];
822759c594dSBernhard Schmidt 		if (txparams & 0x3) {
823759c594dSBernhard Schmidt 			/*
824759c594dSBernhard Schmidt 			 * TX MCS parameters defined and not equal to RX,
825759c594dSBernhard Schmidt 			 * extract the number of spartial streams and
826759c594dSBernhard Schmidt 			 * map it to the highest MCS rate.
827759c594dSBernhard Schmidt 			 */
828759c594dSBernhard Schmidt 			txstream = ((txparams & 0xc) >> 2) + 1;
829759c594dSBernhard Schmidt 			i = txstream * 8 - 1;
830759c594dSBernhard Schmidt 		} else
831759c594dSBernhard Schmidt 			for (i = 31; i >= 0 && isclr(htcap->hc_mcsset, i); i--);
832b032f27cSSam Leffler 		if (i >= 0) {
83331021a2bSAndriy Voskoboinyk 			caps = le16dec(&htcap->hc_cap);
834759c594dSBernhard Schmidt 			if ((caps & IEEE80211_HTCAP_CHWIDTH40) &&
835759c594dSBernhard Schmidt 			    (caps & IEEE80211_HTCAP_SHORTGI40))
836b032f27cSSam Leffler 				rmax = ieee80211_htrates[i].ht40_rate_400ns;
837759c594dSBernhard Schmidt 			else if (caps & IEEE80211_HTCAP_CHWIDTH40)
838b032f27cSSam Leffler 				rmax = ieee80211_htrates[i].ht40_rate_800ns;
839759c594dSBernhard Schmidt 			else if (caps & IEEE80211_HTCAP_SHORTGI20)
840759c594dSBernhard Schmidt 				rmax = ieee80211_htrates[i].ht20_rate_400ns;
841759c594dSBernhard Schmidt 			else
842759c594dSBernhard Schmidt 				rmax = ieee80211_htrates[i].ht20_rate_800ns;
843b032f27cSSam Leffler 		}
844b032f27cSSam Leffler 	}
84568e8e04eSSam Leffler 	for (i = 0; i < se->se_rates[1]; i++) {
84668e8e04eSSam Leffler 		r = se->se_rates[2+i] & IEEE80211_RATE_VAL;
84768e8e04eSSam Leffler 		if (r > rmax)
84868e8e04eSSam Leffler 			rmax = r;
84968e8e04eSSam Leffler 	}
85068e8e04eSSam Leffler 	for (i = 0; i < se->se_xrates[1]; i++) {
85168e8e04eSSam Leffler 		r = se->se_xrates[2+i] & IEEE80211_RATE_VAL;
85268e8e04eSSam Leffler 		if (r > rmax)
85368e8e04eSSam Leffler 			rmax = r;
85468e8e04eSSam Leffler 	}
85568e8e04eSSam Leffler 	return rmax;
85668e8e04eSSam Leffler }
85768e8e04eSSam Leffler 
85868e8e04eSSam Leffler /*
85968e8e04eSSam Leffler  * Compare the capabilities of two entries and decide which is
86068e8e04eSSam Leffler  * more desirable (return >0 if a is considered better).  Note
86168e8e04eSSam Leffler  * that we assume compatibility/usability has already been checked
86268e8e04eSSam Leffler  * so we don't need to (e.g. validate whether privacy is supported).
86368e8e04eSSam Leffler  * Used to select the best scan candidate for association in a BSS.
86451172f62SAdrian Chadd  *
86551172f62SAdrian Chadd  * TODO: should we take 11n, 11ac into account when selecting the
86651172f62SAdrian Chadd  * best?  Right now it just compares frequency band and RSSI.
86768e8e04eSSam Leffler  */
86868e8e04eSSam Leffler static int
sta_compare(const struct sta_entry * a,const struct sta_entry * b)86968e8e04eSSam Leffler sta_compare(const struct sta_entry *a, const struct sta_entry *b)
87068e8e04eSSam Leffler {
87168e8e04eSSam Leffler #define	PREFER(_a,_b,_what) do {			\
87268e8e04eSSam Leffler 	if (((_a) ^ (_b)) & (_what))			\
87368e8e04eSSam Leffler 		return ((_a) & (_what)) ? 1 : -1;	\
87468e8e04eSSam Leffler } while (0)
875b032f27cSSam Leffler 	int maxa, maxb;
87668e8e04eSSam Leffler 	int8_t rssia, rssib;
87768e8e04eSSam Leffler 	int weight;
87868e8e04eSSam Leffler 
87968e8e04eSSam Leffler 	/* privacy support */
88068e8e04eSSam Leffler 	PREFER(a->base.se_capinfo, b->base.se_capinfo,
88168e8e04eSSam Leffler 		IEEE80211_CAPINFO_PRIVACY);
88268e8e04eSSam Leffler 
88368e8e04eSSam Leffler 	/* compare count of previous failures */
88468e8e04eSSam Leffler 	weight = b->se_fails - a->se_fails;
88568e8e04eSSam Leffler 	if (abs(weight) > 1)
88668e8e04eSSam Leffler 		return weight;
88768e8e04eSSam Leffler 
88868e8e04eSSam Leffler 	/*
88968e8e04eSSam Leffler 	 * Compare rssi.  If the two are considered equivalent
89068e8e04eSSam Leffler 	 * then fallback to other criteria.  We threshold the
89168e8e04eSSam Leffler 	 * comparisons to avoid selecting an ap purely by rssi
89268e8e04eSSam Leffler 	 * when both values may be good but one ap is otherwise
89368e8e04eSSam Leffler 	 * more desirable (e.g. an 11b-only ap with stronger
89468e8e04eSSam Leffler 	 * signal than an 11g ap).
89568e8e04eSSam Leffler 	 */
89668e8e04eSSam Leffler 	rssia = MIN(a->base.se_rssi, STA_RSSI_MAX);
89768e8e04eSSam Leffler 	rssib = MIN(b->base.se_rssi, STA_RSSI_MAX);
89868e8e04eSSam Leffler 	if (abs(rssib - rssia) < 5) {
89968e8e04eSSam Leffler 		/* best/max rate preferred if signal level close enough XXX */
90068e8e04eSSam Leffler 		maxa = maxrate(&a->base);
90168e8e04eSSam Leffler 		maxb = maxrate(&b->base);
90268e8e04eSSam Leffler 		if (maxa != maxb)
90368e8e04eSSam Leffler 			return maxa - maxb;
90468e8e04eSSam Leffler 		/* XXX use freq for channel preference */
90568e8e04eSSam Leffler 		/* for now just prefer 5Ghz band to all other bands */
906b032f27cSSam Leffler 		PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan),
907b032f27cSSam Leffler 		       IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1);
90868e8e04eSSam Leffler 	}
90968e8e04eSSam Leffler 	/* all things being equal, use signal level */
91068e8e04eSSam Leffler 	return a->base.se_rssi - b->base.se_rssi;
91168e8e04eSSam Leffler #undef PREFER
91268e8e04eSSam Leffler }
91368e8e04eSSam Leffler 
91468e8e04eSSam Leffler /*
91568e8e04eSSam Leffler  * Check rate set suitability and return the best supported rate.
916b032f27cSSam Leffler  * XXX inspect MCS for HT
91768e8e04eSSam Leffler  */
91868e8e04eSSam Leffler static int
check_rate(struct ieee80211vap * vap,const struct ieee80211_channel * chan,const struct ieee80211_scan_entry * se)9198f4addc0SSam Leffler check_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan,
9208f4addc0SSam Leffler     const struct ieee80211_scan_entry *se)
92168e8e04eSSam Leffler {
92268e8e04eSSam Leffler 	const struct ieee80211_rateset *srs;
923b032f27cSSam Leffler 	int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate;
92468e8e04eSSam Leffler 	const uint8_t *rs;
92568e8e04eSSam Leffler 
926b032f27cSSam Leffler 	okrate = badrate = 0;
92768e8e04eSSam Leffler 
9288f4addc0SSam Leffler 	srs = ieee80211_get_suprates(vap->iv_ic, chan);
92968e8e04eSSam Leffler 	nrs = se->se_rates[1];
93068e8e04eSSam Leffler 	rs = se->se_rates+2;
931b032f27cSSam Leffler 	/* XXX MCS */
9328f4addc0SSam Leffler 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate;
93368e8e04eSSam Leffler 	fixedrate = IEEE80211_FIXED_RATE_NONE;
93468e8e04eSSam Leffler again:
93568e8e04eSSam Leffler 	for (i = 0; i < nrs; i++) {
9360ebe104fSAdrian Chadd 		r = IEEE80211_RV(rs[i]);
93768e8e04eSSam Leffler 		badrate = r;
93868e8e04eSSam Leffler 		/*
93968e8e04eSSam Leffler 		 * Check any fixed rate is included.
94068e8e04eSSam Leffler 		 */
941b032f27cSSam Leffler 		if (r == ucastrate)
94268e8e04eSSam Leffler 			fixedrate = r;
94368e8e04eSSam Leffler 		/*
94468e8e04eSSam Leffler 		 * Check against our supported rates.
94568e8e04eSSam Leffler 		 */
94668e8e04eSSam Leffler 		for (j = 0; j < srs->rs_nrates; j++)
9470ebe104fSAdrian Chadd 			if (r == IEEE80211_RV(srs->rs_rates[j])) {
94868e8e04eSSam Leffler 				if (r > okrate)		/* NB: track max */
94968e8e04eSSam Leffler 					okrate = r;
95068e8e04eSSam Leffler 				break;
95168e8e04eSSam Leffler 			}
95268e8e04eSSam Leffler 
95368e8e04eSSam Leffler 		if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) {
95468e8e04eSSam Leffler 			/*
95568e8e04eSSam Leffler 			 * Don't try joining a BSS, if we don't support
95668e8e04eSSam Leffler 			 * one of its basic rates.
95768e8e04eSSam Leffler 			 */
95868e8e04eSSam Leffler 			okrate = 0;
95968e8e04eSSam Leffler 			goto back;
96068e8e04eSSam Leffler 		}
96168e8e04eSSam Leffler 	}
96268e8e04eSSam Leffler 	if (rs == se->se_rates+2) {
96368e8e04eSSam Leffler 		/* scan xrates too; sort of an algol68-style for loop */
96468e8e04eSSam Leffler 		nrs = se->se_xrates[1];
96568e8e04eSSam Leffler 		rs = se->se_xrates+2;
96668e8e04eSSam Leffler 		goto again;
96768e8e04eSSam Leffler 	}
96868e8e04eSSam Leffler 
96968e8e04eSSam Leffler back:
970b032f27cSSam Leffler 	if (okrate == 0 || ucastrate != fixedrate)
97168e8e04eSSam Leffler 		return badrate | IEEE80211_RATE_BASIC;
97268e8e04eSSam Leffler 	else
9730ebe104fSAdrian Chadd 		return IEEE80211_RV(okrate);
97468e8e04eSSam Leffler }
97568e8e04eSSam Leffler 
97659aa14a9SRui Paulo static __inline int
match_id(const uint8_t * ie,const uint8_t * val,int len)97759aa14a9SRui Paulo match_id(const uint8_t *ie, const uint8_t *val, int len)
97859aa14a9SRui Paulo {
97959aa14a9SRui Paulo 	return (ie[1] == len && memcmp(ie+2, val, len) == 0);
98059aa14a9SRui Paulo }
98159aa14a9SRui Paulo 
98268e8e04eSSam Leffler static int
match_ssid(const uint8_t * ie,int nssid,const struct ieee80211_scan_ssid ssids[])98368e8e04eSSam Leffler match_ssid(const uint8_t *ie,
98468e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
98568e8e04eSSam Leffler {
98668e8e04eSSam Leffler 	int i;
98768e8e04eSSam Leffler 
98868e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
98959aa14a9SRui Paulo 		if (match_id(ie, ssids[i].ssid, ssids[i].len))
99068e8e04eSSam Leffler 			return 1;
99168e8e04eSSam Leffler 	}
99268e8e04eSSam Leffler 	return 0;
99368e8e04eSSam Leffler }
99468e8e04eSSam Leffler 
99510ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
99610ad9a77SSam Leffler static int
tdma_isfull(const struct ieee80211_tdma_param * tdma)99710ad9a77SSam Leffler tdma_isfull(const struct ieee80211_tdma_param *tdma)
99810ad9a77SSam Leffler {
99910ad9a77SSam Leffler 	int slot, slotcnt;
100010ad9a77SSam Leffler 
100110ad9a77SSam Leffler 	slotcnt = tdma->tdma_slotcnt;
100210ad9a77SSam Leffler 	for (slot = slotcnt-1; slot >= 0; slot--)
100310ad9a77SSam Leffler 		if (isclr(tdma->tdma_inuse, slot))
100410ad9a77SSam Leffler 			return 0;
100510ad9a77SSam Leffler 	return 1;
100610ad9a77SSam Leffler }
100710ad9a77SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
100810ad9a77SSam Leffler 
100968e8e04eSSam Leffler /*
101068e8e04eSSam Leffler  * Test a scan candidate for suitability/compatibility.
101168e8e04eSSam Leffler  */
101268e8e04eSSam Leffler static int
match_bss(struct ieee80211vap * vap,const struct ieee80211_scan_state * ss,struct sta_entry * se0,int debug)1013b032f27cSSam Leffler match_bss(struct ieee80211vap *vap,
101468e8e04eSSam Leffler 	const struct ieee80211_scan_state *ss, struct sta_entry *se0,
101568e8e04eSSam Leffler 	int debug)
101668e8e04eSSam Leffler {
1017b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
101868e8e04eSSam Leffler 	struct ieee80211_scan_entry *se = &se0->base;
101968e8e04eSSam Leffler         uint8_t rate;
102068e8e04eSSam Leffler         int fail;
102168e8e04eSSam Leffler 
102268e8e04eSSam Leffler 	fail = 0;
102368e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan)))
1024c4ed2c08SSam Leffler 		fail |= MATCH_CHANNEL;
102568e8e04eSSam Leffler 	/*
102668e8e04eSSam Leffler 	 * NB: normally the desired mode is used to construct
102768e8e04eSSam Leffler 	 * the channel list, but it's possible for the scan
102868e8e04eSSam Leffler 	 * cache to include entries for stations outside this
102968e8e04eSSam Leffler 	 * list so we check the desired mode here to weed them
103068e8e04eSSam Leffler 	 * out.
103168e8e04eSSam Leffler 	 */
1032b032f27cSSam Leffler 	if (vap->iv_des_mode != IEEE80211_MODE_AUTO &&
103368e8e04eSSam Leffler 	    (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) !=
1034b032f27cSSam Leffler 	    chanflags[vap->iv_des_mode])
1035c4ed2c08SSam Leffler 		fail |= MATCH_CHANNEL;
1036b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
103768e8e04eSSam Leffler 		if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
1038c4ed2c08SSam Leffler 			fail |= MATCH_CAPINFO;
103910ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
104010ad9a77SSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
104110ad9a77SSam Leffler 		/*
104210ad9a77SSam Leffler 		 * Adhoc demo network setup shouldn't really be scanning
104310ad9a77SSam Leffler 		 * but just in case skip stations operating in IBSS or
104410ad9a77SSam Leffler 		 * BSS mode.
104510ad9a77SSam Leffler 		 */
104610ad9a77SSam Leffler 		if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
104710ad9a77SSam Leffler 			fail |= MATCH_CAPINFO;
104810ad9a77SSam Leffler 		/*
104910ad9a77SSam Leffler 		 * TDMA operation cannot coexist with a normal 802.11 network;
105010ad9a77SSam Leffler 		 * skip if IBSS or ESS capabilities are marked and require
105110ad9a77SSam Leffler 		 * the beacon have a TDMA ie present.
105210ad9a77SSam Leffler 		 */
105310ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
105410ad9a77SSam Leffler 			const struct ieee80211_tdma_param *tdma =
105510ad9a77SSam Leffler 			    (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie;
10562bc3ce77SSam Leffler 			const struct ieee80211_tdma_state *ts = vap->iv_tdma;
105710ad9a77SSam Leffler 
105810ad9a77SSam Leffler 			if (tdma == NULL)
105910ad9a77SSam Leffler 				fail |= MATCH_TDMA_NOIE;
10602bc3ce77SSam Leffler 			else if (tdma->tdma_version != ts->tdma_version)
10612bc3ce77SSam Leffler 				fail |= MATCH_TDMA_VERSION;
106210ad9a77SSam Leffler 			else if (tdma->tdma_slot != 0)
106310ad9a77SSam Leffler 				fail |= MATCH_TDMA_NOTMASTER;
106410ad9a77SSam Leffler 			else if (tdma_isfull(tdma))
106510ad9a77SSam Leffler 				fail |= MATCH_TDMA_NOSLOT;
106610ad9a77SSam Leffler #if 0
106710ad9a77SSam Leffler 			else if (ieee80211_local_address(se->se_macaddr))
106810ad9a77SSam Leffler 				fail |= MATCH_TDMA_LOCAL;
106910ad9a77SSam Leffler #endif
107010ad9a77SSam Leffler 		}
107110ad9a77SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
107259aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
107359aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
107459aa14a9SRui Paulo 		const struct ieee80211_mesh_state *ms = vap->iv_mesh;
107559aa14a9SRui Paulo 		/*
107659aa14a9SRui Paulo 		 * Mesh nodes have IBSS & ESS bits in capinfo turned off
107759aa14a9SRui Paulo 		 * and two special ie's that must be present.
107859aa14a9SRui Paulo 		 */
107959aa14a9SRui Paulo 		if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS))
108059aa14a9SRui Paulo 			fail |= MATCH_CAPINFO;
108142298a2eSBernhard Schmidt 		else if (se->se_meshid[0] != IEEE80211_ELEMID_MESHID)
108259aa14a9SRui Paulo 			fail |= MATCH_MESH_NOID;
108359aa14a9SRui Paulo 		else if (ms->ms_idlen != 0 &&
108459aa14a9SRui Paulo 		    match_id(se->se_meshid, ms->ms_id, ms->ms_idlen))
108559aa14a9SRui Paulo 			fail |= MATCH_MESHID;
108659aa14a9SRui Paulo #endif
1087faf666aaSSam Leffler 	} else {
108868e8e04eSSam Leffler 		if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0)
1089c4ed2c08SSam Leffler 			fail |= MATCH_CAPINFO;
1090b032f27cSSam Leffler 		/*
1091b032f27cSSam Leffler 		 * If 11d is enabled and we're attempting to join a bss
1092b032f27cSSam Leffler 		 * that advertises it's country code then compare our
1093b032f27cSSam Leffler 		 * current settings to what we fetched from the country ie.
1094b032f27cSSam Leffler 		 * If our country code is unspecified or different then do
1095b032f27cSSam Leffler 		 * not attempt to join the bss.  We should have already
1096b032f27cSSam Leffler 		 * dispatched an event to user space that identifies the
1097b032f27cSSam Leffler 		 * new country code so our regdomain config should match.
1098b032f27cSSam Leffler 		 */
1099b032f27cSSam Leffler 		if ((IEEE80211_IS_CHAN_11D(se->se_chan) ||
1100b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) &&
1101b032f27cSSam Leffler 		    se->se_cc[0] != 0 &&
1102b032f27cSSam Leffler 		    (ic->ic_regdomain.country == CTRY_DEFAULT ||
1103b032f27cSSam Leffler 		     !isocmp(se->se_cc, ic->ic_regdomain.isocc)))
1104b032f27cSSam Leffler 			fail |= MATCH_CC;
110568e8e04eSSam Leffler 	}
1106b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
110768e8e04eSSam Leffler 		if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
1108c4ed2c08SSam Leffler 			fail |= MATCH_PRIVACY;
110968e8e04eSSam Leffler 	} else {
111068e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
111168e8e04eSSam Leffler 		if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY)
1112c4ed2c08SSam Leffler 			fail |= MATCH_PRIVACY;
111368e8e04eSSam Leffler 	}
11148f4addc0SSam Leffler 	se0->se_flags &= ~STA_DEMOTE11B;
11158f4addc0SSam Leffler 	rate = check_rate(vap, se->se_chan, se);
11168f4addc0SSam Leffler 	if (rate & IEEE80211_RATE_BASIC) {
1117c4ed2c08SSam Leffler 		fail |= MATCH_RATE;
11188f4addc0SSam Leffler 		/*
11198f4addc0SSam Leffler 		 * An 11b-only ap will give a rate mismatch if there is an
11208f4addc0SSam Leffler 		 * OFDM fixed tx rate for 11g.  Try downgrading the channel
11218f4addc0SSam Leffler 		 * in the scan list to 11b and retry the rate check.
11228f4addc0SSam Leffler 		 */
11238f4addc0SSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) {
11248f4addc0SSam Leffler 			rate = check_rate(vap, demote11b(vap, se->se_chan), se);
11258f4addc0SSam Leffler 			if ((rate & IEEE80211_RATE_BASIC) == 0) {
11268f4addc0SSam Leffler 				fail &= ~MATCH_RATE;
11278f4addc0SSam Leffler 				se0->se_flags |= STA_DEMOTE11B;
11288f4addc0SSam Leffler 			}
11298f4addc0SSam Leffler 		}
11308f4addc0SSam Leffler 	} else if (rate < 2*24) {
11318f4addc0SSam Leffler 		/*
11328f4addc0SSam Leffler 		 * This is an 11b-only ap.  Check the desired mode in
11338f4addc0SSam Leffler 		 * case that needs to be honored (mode 11g filters out
11348f4addc0SSam Leffler 		 * 11b-only ap's).  Otherwise force any 11g channel used
11358f4addc0SSam Leffler 		 * in scanning to be demoted.
11368f4addc0SSam Leffler 		 *
11378f4addc0SSam Leffler 		 * NB: we cheat a bit here by looking at the max rate;
11388f4addc0SSam Leffler 		 *     we could/should check the rates.
11398f4addc0SSam Leffler 		 */
11408f4addc0SSam Leffler 		if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO ||
11418f4addc0SSam Leffler 		      vap->iv_des_mode == IEEE80211_MODE_11B))
11428f4addc0SSam Leffler 			fail |= MATCH_RATE;
11438f4addc0SSam Leffler 		else
11448f4addc0SSam Leffler 			se0->se_flags |= STA_DEMOTE11B;
11458f4addc0SSam Leffler 	}
114668e8e04eSSam Leffler 	if (ss->ss_nssid != 0 &&
1147c4ed2c08SSam Leffler 	    !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid))
1148c4ed2c08SSam Leffler 		fail |= MATCH_SSID;
1149b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
1150b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid))
1151c4ed2c08SSam Leffler 		fail |= MATCH_BSSID;
115268e8e04eSSam Leffler 	if (se0->se_fails >= STA_FAILS_MAX)
1153c4ed2c08SSam Leffler 		fail |= MATCH_FAILS;
115468e8e04eSSam Leffler 	if (se0->se_notseen >= STA_PURGE_SCANS)
1155c4ed2c08SSam Leffler 		fail |= MATCH_NOTSEEN;
115668e8e04eSSam Leffler 	if (se->se_rssi < STA_RSSI_MIN)
1157c4ed2c08SSam Leffler 		fail |= MATCH_RSSI;
115868e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
1159b032f27cSSam Leffler 	if (ieee80211_msg(vap, debug)) {
116068e8e04eSSam Leffler 		printf(" %c %s",
1161c4ed2c08SSam Leffler 		    fail & MATCH_FAILS ? '=' :
1162c4ed2c08SSam Leffler 		    fail & MATCH_NOTSEEN ? '^' :
1163b032f27cSSam Leffler 		    fail & MATCH_CC ? '$' :
116410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
116510ad9a77SSam Leffler 		    fail & MATCH_TDMA_NOIE ? '&' :
11662bc3ce77SSam Leffler 		    fail & MATCH_TDMA_VERSION ? 'v' :
11672bc3ce77SSam Leffler 		    fail & MATCH_TDMA_NOTMASTER ? 's' :
11682bc3ce77SSam Leffler 		    fail & MATCH_TDMA_NOSLOT ? 'f' :
11692bc3ce77SSam Leffler 		    fail & MATCH_TDMA_LOCAL ? 'l' :
117010ad9a77SSam Leffler #endif
117159aa14a9SRui Paulo 		    fail & MATCH_MESH_NOID ? 'm' :
1172c4ed2c08SSam Leffler 		    fail ? '-' : '+', ether_sprintf(se->se_macaddr));
117368e8e04eSSam Leffler 		printf(" %s%c", ether_sprintf(se->se_bssid),
1174c4ed2c08SSam Leffler 		    fail & MATCH_BSSID ? '!' : ' ');
117568e8e04eSSam Leffler 		printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan),
1176c4ed2c08SSam Leffler 			fail & MATCH_CHANNEL ? '!' : ' ');
1177c4ed2c08SSam Leffler 		printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' ');
117868e8e04eSSam Leffler 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
1179c4ed2c08SSam Leffler 		    fail & MATCH_RATE ? '!' : ' ');
118068e8e04eSSam Leffler 		printf(" %4s%c",
118168e8e04eSSam Leffler 		    (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
11822bc3ce77SSam Leffler 		    (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : "",
1183c4ed2c08SSam Leffler 		    fail & MATCH_CAPINFO ? '!' : ' ');
118468e8e04eSSam Leffler 		printf(" %3s%c ",
118568e8e04eSSam Leffler 		    (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
118668e8e04eSSam Leffler 		    "wep" : "no",
1187c4ed2c08SSam Leffler 		    fail & MATCH_PRIVACY ? '!' : ' ');
118868e8e04eSSam Leffler 		ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]);
118959aa14a9SRui Paulo 		printf("%s\n", fail & (MATCH_SSID | MATCH_MESHID) ? "!" : "");
119068e8e04eSSam Leffler 	}
119168e8e04eSSam Leffler #endif
119268e8e04eSSam Leffler 	return fail;
119368e8e04eSSam Leffler }
119468e8e04eSSam Leffler 
119568e8e04eSSam Leffler static void
sta_update_notseen(struct sta_table * st)119668e8e04eSSam Leffler sta_update_notseen(struct sta_table *st)
119768e8e04eSSam Leffler {
119868e8e04eSSam Leffler 	struct sta_entry *se;
119968e8e04eSSam Leffler 
120020c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
120168e8e04eSSam Leffler 	TAILQ_FOREACH(se, &st->st_entry, se_list) {
120268e8e04eSSam Leffler 		/*
120368e8e04eSSam Leffler 		 * If seen the reset and don't bump the count;
120468e8e04eSSam Leffler 		 * otherwise bump the ``not seen'' count.  Note
120568e8e04eSSam Leffler 		 * that this insures that stations for which we
120668e8e04eSSam Leffler 		 * see frames while not scanning but not during
120768e8e04eSSam Leffler 		 * this scan will not be penalized.
120868e8e04eSSam Leffler 		 */
120968e8e04eSSam Leffler 		if (se->se_seen)
121068e8e04eSSam Leffler 			se->se_seen = 0;
121168e8e04eSSam Leffler 		else
121268e8e04eSSam Leffler 			se->se_notseen++;
121368e8e04eSSam Leffler 	}
121420c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
121568e8e04eSSam Leffler }
121668e8e04eSSam Leffler 
121768e8e04eSSam Leffler static void
sta_dec_fails(struct sta_table * st)121868e8e04eSSam Leffler sta_dec_fails(struct sta_table *st)
121968e8e04eSSam Leffler {
122068e8e04eSSam Leffler 	struct sta_entry *se;
122168e8e04eSSam Leffler 
122220c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
122368e8e04eSSam Leffler 	TAILQ_FOREACH(se, &st->st_entry, se_list)
122468e8e04eSSam Leffler 		if (se->se_fails)
122568e8e04eSSam Leffler 			se->se_fails--;
122620c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
122768e8e04eSSam Leffler }
122868e8e04eSSam Leffler 
122968e8e04eSSam Leffler static struct sta_entry *
select_bss(struct ieee80211_scan_state * ss,struct ieee80211vap * vap,int debug)1230b032f27cSSam Leffler select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug)
123168e8e04eSSam Leffler {
123268e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
123368e8e04eSSam Leffler 	struct sta_entry *se, *selbs = NULL;
123468e8e04eSSam Leffler 
1235b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, debug, " %s\n",
123668e8e04eSSam Leffler 	    "macaddr          bssid         chan  rssi  rate flag  wep  essid");
123720c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
123868e8e04eSSam Leffler 	TAILQ_FOREACH(se, &st->st_entry, se_list) {
1239b032f27cSSam Leffler 		ieee80211_ies_expand(&se->base.se_ies);
1240887acaadSSam Leffler 		if (match_bss(vap, ss, se, debug) == 0) {
124168e8e04eSSam Leffler 			if (selbs == NULL)
124268e8e04eSSam Leffler 				selbs = se;
124368e8e04eSSam Leffler 			else if (sta_compare(se, selbs) > 0)
124468e8e04eSSam Leffler 				selbs = se;
124568e8e04eSSam Leffler 		}
124668e8e04eSSam Leffler 	}
124720c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
124868e8e04eSSam Leffler 
124968e8e04eSSam Leffler 	return selbs;
125068e8e04eSSam Leffler }
125168e8e04eSSam Leffler 
125268e8e04eSSam Leffler /*
125368e8e04eSSam Leffler  * Pick an ap or ibss network to join or find a channel
125468e8e04eSSam Leffler  * to use to start an ibss network.
125568e8e04eSSam Leffler  */
125668e8e04eSSam Leffler static int
sta_pick_bss(struct ieee80211_scan_state * ss,struct ieee80211vap * vap)1257b032f27cSSam Leffler sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
125868e8e04eSSam Leffler {
125968e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
126068e8e04eSSam Leffler 	struct sta_entry *selbs;
126110959256SSam Leffler 	struct ieee80211_channel *chan;
126268e8e04eSSam Leffler 
1263b032f27cSSam Leffler 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1264b032f27cSSam Leffler 		("wrong mode %u", vap->iv_opmode));
126568e8e04eSSam Leffler 
126668e8e04eSSam Leffler 	if (st->st_newscan) {
126768e8e04eSSam Leffler 		sta_update_notseen(st);
126868e8e04eSSam Leffler 		st->st_newscan = 0;
126968e8e04eSSam Leffler 	}
127068e8e04eSSam Leffler 	if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
127168e8e04eSSam Leffler 		/*
127268e8e04eSSam Leffler 		 * Manual/background scan, don't select+join the
127368e8e04eSSam Leffler 		 * bss, just return.  The scanning framework will
127468e8e04eSSam Leffler 		 * handle notification that this has completed.
127568e8e04eSSam Leffler 		 */
127668e8e04eSSam Leffler 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1277a6ca7ce3SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1278a6ca7ce3SAdrian Chadd 		    "%s: nopick; return 1\n", __func__);
127968e8e04eSSam Leffler 		return 1;
128068e8e04eSSam Leffler 	}
128168e8e04eSSam Leffler 	/*
128268e8e04eSSam Leffler 	 * Automatic sequencing; look for a candidate and
128368e8e04eSSam Leffler 	 * if found join the network.
128468e8e04eSSam Leffler 	 */
128568e8e04eSSam Leffler 	/* NB: unlocked read should be ok */
128668e8e04eSSam Leffler 	if (TAILQ_FIRST(&st->st_entry) == NULL) {
1287b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1288a6ca7ce3SAdrian Chadd 			"%s: no scan candidate, join=%d, return 0\n",
1289a6ca7ce3SAdrian Chadd 			__func__,
1290a6ca7ce3SAdrian Chadd 			!! (ss->ss_flags & IEEE80211_SCAN_NOJOIN));
1291b032f27cSSam Leffler 		if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1292b032f27cSSam Leffler 			return 0;
129368e8e04eSSam Leffler notfound:
129468e8e04eSSam Leffler 		/*
129568e8e04eSSam Leffler 		 * If nothing suitable was found decrement
129668e8e04eSSam Leffler 		 * the failure counts so entries will be
129768e8e04eSSam Leffler 		 * reconsidered the next time around.  We
129868e8e04eSSam Leffler 		 * really want to do this only for sta's
129968e8e04eSSam Leffler 		 * where we've previously had some success.
130068e8e04eSSam Leffler 		 */
130168e8e04eSSam Leffler 		sta_dec_fails(st);
130268e8e04eSSam Leffler 		st->st_newscan = 1;
130368e8e04eSSam Leffler 		return 0;			/* restart scan */
130468e8e04eSSam Leffler 	}
1305b032f27cSSam Leffler 	selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1306b032f27cSSam Leffler 	if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1307b032f27cSSam Leffler 		return (selbs != NULL);
130810959256SSam Leffler 	if (selbs == NULL)
130910959256SSam Leffler 		goto notfound;
131010959256SSam Leffler 	chan = selbs->base.se_chan;
13118f4addc0SSam Leffler 	if (selbs->se_flags & STA_DEMOTE11B)
13128f4addc0SSam Leffler 		chan = demote11b(vap, chan);
131310959256SSam Leffler 	if (!ieee80211_sta_join(vap, chan, &selbs->base))
131468e8e04eSSam Leffler 		goto notfound;
1315a6ca7ce3SAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1316a6ca7ce3SAdrian Chadd 	    "%s: terminate scan; return 1\n", __func__);
131768e8e04eSSam Leffler 	return 1;				/* terminate scan */
131868e8e04eSSam Leffler }
131968e8e04eSSam Leffler 
132068e8e04eSSam Leffler /*
132168e8e04eSSam Leffler  * Lookup an entry in the scan cache.  We assume we're
132268e8e04eSSam Leffler  * called from the bottom half or such that we don't need
132368e8e04eSSam Leffler  * to block the bottom half so that it's safe to return
132468e8e04eSSam Leffler  * a reference to an entry w/o holding the lock on the table.
132568e8e04eSSam Leffler  */
132668e8e04eSSam Leffler static struct sta_entry *
sta_lookup(struct sta_table * st,const uint8_t macaddr[IEEE80211_ADDR_LEN])132768e8e04eSSam Leffler sta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN])
132868e8e04eSSam Leffler {
132968e8e04eSSam Leffler 	struct sta_entry *se;
133068e8e04eSSam Leffler 	int hash = STA_HASH(macaddr);
133168e8e04eSSam Leffler 
133220c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
133368e8e04eSSam Leffler 	LIST_FOREACH(se, &st->st_hash[hash], se_hash)
133468e8e04eSSam Leffler 		if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr))
133568e8e04eSSam Leffler 			break;
133620c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
133768e8e04eSSam Leffler 
133868e8e04eSSam Leffler 	return se;		/* NB: unlocked */
133968e8e04eSSam Leffler }
134068e8e04eSSam Leffler 
134168e8e04eSSam Leffler static void
sta_roam_check(struct ieee80211_scan_state * ss,struct ieee80211vap * vap)1342b032f27cSSam Leffler sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
134368e8e04eSSam Leffler {
1344b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1345b032f27cSSam Leffler 	struct ieee80211_node *ni = vap->iv_bss;
134668e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
1347b032f27cSSam Leffler 	enum ieee80211_phymode mode;
134868e8e04eSSam Leffler 	struct sta_entry *se, *selbs;
1349b032f27cSSam Leffler 	uint8_t roamRate, curRate, ucastRate;
135068e8e04eSSam Leffler 	int8_t roamRssi, curRssi;
135168e8e04eSSam Leffler 
135268e8e04eSSam Leffler 	se = sta_lookup(st, ni->ni_macaddr);
135368e8e04eSSam Leffler 	if (se == NULL) {
135468e8e04eSSam Leffler 		/* XXX something is wrong */
135568e8e04eSSam Leffler 		return;
135668e8e04eSSam Leffler 	}
135768e8e04eSSam Leffler 
1358b032f27cSSam Leffler 	mode = ieee80211_chan2mode(ic->ic_bsschan);
1359b032f27cSSam Leffler 	roamRate = vap->iv_roamparms[mode].rate;
1360b032f27cSSam Leffler 	roamRssi = vap->iv_roamparms[mode].rssi;
1361511e2766SAndriy Voskoboinyk 	KASSERT(roamRate != 0 && roamRssi != 0, ("iv_roamparms are not"
1362511e2766SAndriy Voskoboinyk 	    "initialized for %s mode!", ieee80211_phymode_name[mode]));
1363511e2766SAndriy Voskoboinyk 
1364b032f27cSSam Leffler 	ucastRate = vap->iv_txparms[mode].ucastrate;
136568e8e04eSSam Leffler 	/* NB: the most up to date rssi is in the node, not the scan cache */
136668e8e04eSSam Leffler 	curRssi = ic->ic_node_getrssi(ni);
1367b032f27cSSam Leffler 	if (ucastRate == IEEE80211_FIXED_RATE_NONE) {
1368b032f27cSSam Leffler 		curRate = ni->ni_txrate;
1369b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
137068e8e04eSSam Leffler 		    "%s: currssi %d currate %u roamrssi %d roamrate %u\n",
137168e8e04eSSam Leffler 		    __func__, curRssi, curRate, roamRssi, roamRate);
137268e8e04eSSam Leffler 	} else {
137368e8e04eSSam Leffler 		curRate = roamRate;	/* NB: insure compare below fails */
1374b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM,
137568e8e04eSSam Leffler 		    "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi);
137668e8e04eSSam Leffler 	}
137768e8e04eSSam Leffler 	/*
137868e8e04eSSam Leffler 	 * Check if a new ap should be used and switch.
137968e8e04eSSam Leffler 	 * XXX deauth current ap
138068e8e04eSSam Leffler 	 */
138168e8e04eSSam Leffler 	if (curRate < roamRate || curRssi < roamRssi) {
1382b8e29e06SAdrian Chadd 		if (ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) {
138368e8e04eSSam Leffler 			/*
138468e8e04eSSam Leffler 			 * Scan cache contents are too old; force a scan now
138568e8e04eSSam Leffler 			 * if possible so we have current state to make a
138668e8e04eSSam Leffler 			 * decision with.  We don't kick off a bg scan if
138768e8e04eSSam Leffler 			 * we're using dynamic turbo and boosted or if the
138868e8e04eSSam Leffler 			 * channel is busy.
138968e8e04eSSam Leffler 			 * XXX force immediate switch on scan complete
139068e8e04eSSam Leffler 			 */
139168e8e04eSSam Leffler 			if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1392260b8f08SImre Vadász 			    ((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) ||
1393260b8f08SImre Vadász 			     ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)))
1394b032f27cSSam Leffler 				ieee80211_bg_scan(vap, 0);
139568e8e04eSSam Leffler 			return;
139668e8e04eSSam Leffler 		}
139768e8e04eSSam Leffler 		se->base.se_rssi = curRssi;
1398b032f27cSSam Leffler 		selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM);
139968e8e04eSSam Leffler 		if (selbs != NULL && selbs != se) {
140010959256SSam Leffler 			struct ieee80211_channel *chan;
140110959256SSam Leffler 
1402b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
140368e8e04eSSam Leffler 			    IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG,
140468e8e04eSSam Leffler 			    "%s: ROAM: curRate %u, roamRate %u, "
140568e8e04eSSam Leffler 			    "curRssi %d, roamRssi %d\n", __func__,
140668e8e04eSSam Leffler 			    curRate, roamRate, curRssi, roamRssi);
140710959256SSam Leffler 
140810959256SSam Leffler 			chan = selbs->base.se_chan;
14098f4addc0SSam Leffler 			if (selbs->se_flags & STA_DEMOTE11B)
14108f4addc0SSam Leffler 				chan = demote11b(vap, chan);
141110959256SSam Leffler 			(void) ieee80211_sta_join(vap, chan, &selbs->base);
141268e8e04eSSam Leffler 		}
141368e8e04eSSam Leffler 	}
141468e8e04eSSam Leffler }
141568e8e04eSSam Leffler 
141668e8e04eSSam Leffler /*
141768e8e04eSSam Leffler  * Age entries in the scan cache.
141868e8e04eSSam Leffler  * XXX also do roaming since it's convenient
141968e8e04eSSam Leffler  */
142068e8e04eSSam Leffler static void
sta_age(struct ieee80211_scan_state * ss)142168e8e04eSSam Leffler sta_age(struct ieee80211_scan_state *ss)
142268e8e04eSSam Leffler {
1423b032f27cSSam Leffler 	struct ieee80211vap *vap = ss->ss_vap;
142468e8e04eSSam Leffler 
1425b032f27cSSam Leffler 	adhoc_age(ss);
142668e8e04eSSam Leffler 	/*
142768e8e04eSSam Leffler 	 * If rate control is enabled check periodically to see if
142868e8e04eSSam Leffler 	 * we should roam from our current connection to one that
142968e8e04eSSam Leffler 	 * might be better.  This only applies when we're operating
143068e8e04eSSam Leffler 	 * in sta mode and automatic roaming is set.
143168e8e04eSSam Leffler 	 * XXX defer if busy
143268e8e04eSSam Leffler 	 * XXX repeater station
143368e8e04eSSam Leffler 	 * XXX do when !bgscan?
143468e8e04eSSam Leffler 	 */
1435b032f27cSSam Leffler 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1436b032f27cSSam Leffler 		("wrong mode %u", vap->iv_opmode));
1437b032f27cSSam Leffler 	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO &&
14384e62eb6cSAdrian Chadd 	    (vap->iv_flags & IEEE80211_F_BGSCAN) &&
1439b032f27cSSam Leffler 	    vap->iv_state >= IEEE80211_S_RUN)
144068e8e04eSSam Leffler 		/* XXX vap is implicit */
1441b032f27cSSam Leffler 		sta_roam_check(ss, vap);
144268e8e04eSSam Leffler }
144368e8e04eSSam Leffler 
144468e8e04eSSam Leffler /*
144568e8e04eSSam Leffler  * Iterate over the entries in the scan cache, invoking
144668e8e04eSSam Leffler  * the callback function on each one.
144768e8e04eSSam Leffler  */
144868e8e04eSSam Leffler static void
sta_iterate(struct ieee80211_scan_state * ss,ieee80211_scan_iter_func * f,void * arg)144968e8e04eSSam Leffler sta_iterate(struct ieee80211_scan_state *ss,
145068e8e04eSSam Leffler 	ieee80211_scan_iter_func *f, void *arg)
145168e8e04eSSam Leffler {
145268e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
145368e8e04eSSam Leffler 	struct sta_entry *se;
145468e8e04eSSam Leffler 	u_int gen;
145568e8e04eSSam Leffler 
1456b6b2fb59SAdrian Chadd 	IEEE80211_SCAN_ITER_LOCK(st);
1457b032f27cSSam Leffler 	gen = st->st_scaniter++;
145868e8e04eSSam Leffler restart:
145920c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
146068e8e04eSSam Leffler 	TAILQ_FOREACH(se, &st->st_entry, se_list) {
146168e8e04eSSam Leffler 		if (se->se_scangen != gen) {
146268e8e04eSSam Leffler 			se->se_scangen = gen;
146368e8e04eSSam Leffler 			/* update public state */
146468e8e04eSSam Leffler 			se->base.se_age = ticks - se->se_lastupdate;
146520c3b3faSRui Paulo 			IEEE80211_SCAN_TABLE_UNLOCK(st);
146668e8e04eSSam Leffler 			(*f)(arg, &se->base);
146768e8e04eSSam Leffler 			goto restart;
146868e8e04eSSam Leffler 		}
146968e8e04eSSam Leffler 	}
147020c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
147168e8e04eSSam Leffler 
1472b6b2fb59SAdrian Chadd 	IEEE80211_SCAN_ITER_UNLOCK(st);
147368e8e04eSSam Leffler }
147468e8e04eSSam Leffler 
147568e8e04eSSam Leffler static void
sta_assoc_fail(struct ieee80211_scan_state * ss,const uint8_t macaddr[IEEE80211_ADDR_LEN],int reason)147668e8e04eSSam Leffler sta_assoc_fail(struct ieee80211_scan_state *ss,
147768e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason)
147868e8e04eSSam Leffler {
147968e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
148068e8e04eSSam Leffler 	struct sta_entry *se;
148168e8e04eSSam Leffler 
148268e8e04eSSam Leffler 	se = sta_lookup(st, macaddr);
148368e8e04eSSam Leffler 	if (se != NULL) {
148468e8e04eSSam Leffler 		se->se_fails++;
148568e8e04eSSam Leffler 		se->se_lastfail = ticks;
1486b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
148768e8e04eSSam Leffler 		    macaddr, "%s: reason %u fails %u",
148868e8e04eSSam Leffler 		    __func__, reason, se->se_fails);
148968e8e04eSSam Leffler 	}
149068e8e04eSSam Leffler }
149168e8e04eSSam Leffler 
149268e8e04eSSam Leffler static void
sta_assoc_success(struct ieee80211_scan_state * ss,const uint8_t macaddr[IEEE80211_ADDR_LEN])149368e8e04eSSam Leffler sta_assoc_success(struct ieee80211_scan_state *ss,
149468e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
149568e8e04eSSam Leffler {
149668e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
149768e8e04eSSam Leffler 	struct sta_entry *se;
149868e8e04eSSam Leffler 
149968e8e04eSSam Leffler 	se = sta_lookup(st, macaddr);
150068e8e04eSSam Leffler 	if (se != NULL) {
150168e8e04eSSam Leffler #if 0
150268e8e04eSSam Leffler 		se->se_fails = 0;
1503b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN,
150468e8e04eSSam Leffler 		    macaddr, "%s: fails %u",
150568e8e04eSSam Leffler 		    __func__, se->se_fails);
150668e8e04eSSam Leffler #endif
150768e8e04eSSam Leffler 		se->se_lastassoc = ticks;
150868e8e04eSSam Leffler 	}
150968e8e04eSSam Leffler }
151068e8e04eSSam Leffler 
151168e8e04eSSam Leffler static const struct ieee80211_scanner sta_default = {
151268e8e04eSSam Leffler 	.scan_name		= "default",
151368e8e04eSSam Leffler 	.scan_attach		= sta_attach,
151468e8e04eSSam Leffler 	.scan_detach		= sta_detach,
151568e8e04eSSam Leffler 	.scan_start		= sta_start,
151668e8e04eSSam Leffler 	.scan_restart		= sta_restart,
151768e8e04eSSam Leffler 	.scan_cancel		= sta_cancel,
151868e8e04eSSam Leffler 	.scan_end		= sta_pick_bss,
151968e8e04eSSam Leffler 	.scan_flush		= sta_flush,
152068e8e04eSSam Leffler 	.scan_add		= sta_add,
152168e8e04eSSam Leffler 	.scan_age		= sta_age,
152268e8e04eSSam Leffler 	.scan_iterate		= sta_iterate,
152368e8e04eSSam Leffler 	.scan_assoc_fail	= sta_assoc_fail,
152468e8e04eSSam Leffler 	.scan_assoc_success	= sta_assoc_success,
152568e8e04eSSam Leffler };
152659aa14a9SRui Paulo IEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default);
152768e8e04eSSam Leffler 
152868e8e04eSSam Leffler /*
152968e8e04eSSam Leffler  * Adhoc mode-specific support.
153068e8e04eSSam Leffler  */
153168e8e04eSSam Leffler 
153268e8e04eSSam Leffler static const uint16_t adhocWorld[] =		/* 36, 40, 44, 48 */
153368e8e04eSSam Leffler { 5180, 5200, 5220, 5240 };
153468e8e04eSSam Leffler static const uint16_t adhocFcc3[] =		/* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */
153568e8e04eSSam Leffler { 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 };
153668e8e04eSSam Leffler static const uint16_t adhocMkk[] =		/* 34, 38, 42, 46 */
153768e8e04eSSam Leffler { 5170, 5190, 5210, 5230 };
153868e8e04eSSam Leffler static const uint16_t adhoc11b[] =		/* 10, 11 */
153968e8e04eSSam Leffler { 2457, 2462 };
154068e8e04eSSam Leffler 
154168e8e04eSSam Leffler static const struct scanlist adhocScanTable[] = {
154268e8e04eSSam Leffler 	{ IEEE80211_MODE_11B,   	X(adhoc11b) },
154368e8e04eSSam Leffler 	{ IEEE80211_MODE_11A,   	X(adhocWorld) },
154468e8e04eSSam Leffler 	{ IEEE80211_MODE_11A,   	X(adhocFcc3) },
154568e8e04eSSam Leffler 	{ IEEE80211_MODE_11B,   	X(adhocMkk) },
154668e8e04eSSam Leffler 	{ .list = NULL }
154768e8e04eSSam Leffler };
154868e8e04eSSam Leffler #undef X
154968e8e04eSSam Leffler 
155068e8e04eSSam Leffler /*
155168e8e04eSSam Leffler  * Start an adhoc-mode scan by populating the channel list.
155268e8e04eSSam Leffler  */
155368e8e04eSSam Leffler static int
adhoc_start(struct ieee80211_scan_state * ss,struct ieee80211vap * vap)1554b032f27cSSam Leffler adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
155568e8e04eSSam Leffler {
155668e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
155768e8e04eSSam Leffler 
1558b032f27cSSam Leffler 	makescanlist(ss, vap, adhocScanTable);
155998fed7a4SSam Leffler 
1560b032f27cSSam Leffler 	if (ss->ss_mindwell == 0)
156168e8e04eSSam Leffler 		ss->ss_mindwell = msecs_to_ticks(200);	/* 200ms */
1562b032f27cSSam Leffler 	if (ss->ss_maxdwell == 0)
156368e8e04eSSam Leffler 		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
156468e8e04eSSam Leffler 
1565b032f27cSSam Leffler 	st->st_scangen++;
156668e8e04eSSam Leffler 	st->st_newscan = 1;
156768e8e04eSSam Leffler 
156868e8e04eSSam Leffler 	return 0;
156968e8e04eSSam Leffler }
157068e8e04eSSam Leffler 
157168e8e04eSSam Leffler /*
157268e8e04eSSam Leffler  * Select a channel to start an adhoc network on.
157368e8e04eSSam Leffler  * The channel list was populated with appropriate
157468e8e04eSSam Leffler  * channels so select one that looks least occupied.
157568e8e04eSSam Leffler  */
157668e8e04eSSam Leffler static struct ieee80211_channel *
adhoc_pick_channel(struct ieee80211_scan_state * ss,int flags)1577b032f27cSSam Leffler adhoc_pick_channel(struct ieee80211_scan_state *ss, int flags)
157868e8e04eSSam Leffler {
157968e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
158068e8e04eSSam Leffler 	struct sta_entry *se;
158168e8e04eSSam Leffler 	struct ieee80211_channel *c, *bestchan;
158268e8e04eSSam Leffler 	int i, bestrssi, maxrssi;
158368e8e04eSSam Leffler 
158468e8e04eSSam Leffler 	bestchan = NULL;
158568e8e04eSSam Leffler 	bestrssi = -1;
158668e8e04eSSam Leffler 
158720c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
158868e8e04eSSam Leffler 	for (i = 0; i < ss->ss_last; i++) {
158968e8e04eSSam Leffler 		c = ss->ss_chans[i];
1590b032f27cSSam Leffler 		/* never consider a channel with radar */
1591b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(c))
1592b032f27cSSam Leffler 			continue;
1593b032f27cSSam Leffler 		/* skip channels disallowed by regulatory settings */
1594b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_NOADHOC(c))
1595b032f27cSSam Leffler 			continue;
1596b032f27cSSam Leffler 		/* check channel attributes for band compatibility */
1597b032f27cSSam Leffler 		if (flags != 0 && (c->ic_flags & flags) != flags)
159898fed7a4SSam Leffler 			continue;
159968e8e04eSSam Leffler 		maxrssi = 0;
160068e8e04eSSam Leffler 		TAILQ_FOREACH(se, &st->st_entry, se_list) {
160168e8e04eSSam Leffler 			if (se->base.se_chan != c)
160268e8e04eSSam Leffler 				continue;
160368e8e04eSSam Leffler 			if (se->base.se_rssi > maxrssi)
160468e8e04eSSam Leffler 				maxrssi = se->base.se_rssi;
160568e8e04eSSam Leffler 		}
160668e8e04eSSam Leffler 		if (bestchan == NULL || maxrssi < bestrssi)
160768e8e04eSSam Leffler 			bestchan = c;
160868e8e04eSSam Leffler 	}
160920c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
161068e8e04eSSam Leffler 
161168e8e04eSSam Leffler 	return bestchan;
161268e8e04eSSam Leffler }
161368e8e04eSSam Leffler 
161468e8e04eSSam Leffler /*
161568e8e04eSSam Leffler  * Pick an ibss network to join or find a channel
161668e8e04eSSam Leffler  * to use to start an ibss network.
161768e8e04eSSam Leffler  */
161868e8e04eSSam Leffler static int
adhoc_pick_bss(struct ieee80211_scan_state * ss,struct ieee80211vap * vap)1619b032f27cSSam Leffler adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
162068e8e04eSSam Leffler {
162168e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
162268e8e04eSSam Leffler 	struct sta_entry *selbs;
162368e8e04eSSam Leffler 	struct ieee80211_channel *chan;
1624d71a1f7aSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
162568e8e04eSSam Leffler 
1626b032f27cSSam Leffler 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
162759aa14a9SRui Paulo 		vap->iv_opmode == IEEE80211_M_AHDEMO ||
162859aa14a9SRui Paulo 		vap->iv_opmode == IEEE80211_M_MBSS,
1629b032f27cSSam Leffler 		("wrong opmode %u", vap->iv_opmode));
163068e8e04eSSam Leffler 
163168e8e04eSSam Leffler 	if (st->st_newscan) {
163268e8e04eSSam Leffler 		sta_update_notseen(st);
163368e8e04eSSam Leffler 		st->st_newscan = 0;
163468e8e04eSSam Leffler 	}
163568e8e04eSSam Leffler 	if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
163668e8e04eSSam Leffler 		/*
163768e8e04eSSam Leffler 		 * Manual/background scan, don't select+join the
163868e8e04eSSam Leffler 		 * bss, just return.  The scanning framework will
163968e8e04eSSam Leffler 		 * handle notification that this has completed.
164068e8e04eSSam Leffler 		 */
164168e8e04eSSam Leffler 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
164268e8e04eSSam Leffler 		return 1;
164368e8e04eSSam Leffler 	}
164468e8e04eSSam Leffler 	/*
164568e8e04eSSam Leffler 	 * Automatic sequencing; look for a candidate and
164668e8e04eSSam Leffler 	 * if found join the network.
164768e8e04eSSam Leffler 	 */
164868e8e04eSSam Leffler 	/* NB: unlocked read should be ok */
164968e8e04eSSam Leffler 	if (TAILQ_FIRST(&st->st_entry) == NULL) {
1650b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
165168e8e04eSSam Leffler 			"%s: no scan candidate\n", __func__);
1652b032f27cSSam Leffler 		if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1653b032f27cSSam Leffler 			return 0;
165468e8e04eSSam Leffler notfound:
165510ad9a77SSam Leffler 		/* NB: never auto-start a tdma network for slot !0 */
165610ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
165710ad9a77SSam Leffler 		if (vap->iv_des_nssid &&
165810ad9a77SSam Leffler 		    ((vap->iv_caps & IEEE80211_C_TDMA) == 0 ||
165910ad9a77SSam Leffler 		     ieee80211_tdma_getslot(vap) == 0)) {
166010ad9a77SSam Leffler #else
1661b032f27cSSam Leffler 		if (vap->iv_des_nssid) {
166210ad9a77SSam Leffler #endif
166368e8e04eSSam Leffler 			/*
166468e8e04eSSam Leffler 			 * No existing adhoc network to join and we have
166568e8e04eSSam Leffler 			 * an ssid; start one up.  If no channel was
166668e8e04eSSam Leffler 			 * specified, try to select a channel.
166768e8e04eSSam Leffler 			 */
1668b032f27cSSam Leffler 			if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
1669b032f27cSSam Leffler 			    IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
167063c61408SSam Leffler 				chan = adhoc_pick_channel(ss, 0);
1671b032f27cSSam Leffler 			} else
1672b032f27cSSam Leffler 				chan = vap->iv_des_chan;
167368e8e04eSSam Leffler 			if (chan != NULL) {
1674d71a1f7aSAdrian Chadd 				/*
1675d71a1f7aSAdrian Chadd 				 * Create a HT capable IBSS; the per-node
1676d71a1f7aSAdrian Chadd 				 * probe request/response will result in
1677d71a1f7aSAdrian Chadd 				 * "correct" rate control capabilities being
1678d71a1f7aSAdrian Chadd 				 * negotiated.
1679d71a1f7aSAdrian Chadd 				 */
1680d71a1f7aSAdrian Chadd 				chan = ieee80211_ht_adjust_channel(ic,
1681d71a1f7aSAdrian Chadd 				    chan, vap->iv_flags_ht);
168251172f62SAdrian Chadd 				chan = ieee80211_vht_adjust_channel(ic,
1683ef48d4faSBjoern A. Zeeb 				    chan, vap->iv_vht_flags);
1684b032f27cSSam Leffler 				ieee80211_create_ibss(vap, chan);
168568e8e04eSSam Leffler 				return 1;
168668e8e04eSSam Leffler 			}
168768e8e04eSSam Leffler 		}
168868e8e04eSSam Leffler 		/*
168968e8e04eSSam Leffler 		 * If nothing suitable was found decrement
169068e8e04eSSam Leffler 		 * the failure counts so entries will be
169168e8e04eSSam Leffler 		 * reconsidered the next time around.  We
169268e8e04eSSam Leffler 		 * really want to do this only for sta's
169368e8e04eSSam Leffler 		 * where we've previously had some success.
169468e8e04eSSam Leffler 		 */
169568e8e04eSSam Leffler 		sta_dec_fails(st);
169668e8e04eSSam Leffler 		st->st_newscan = 1;
169768e8e04eSSam Leffler 		return 0;			/* restart scan */
169868e8e04eSSam Leffler 	}
1699b032f27cSSam Leffler 	selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
1700b032f27cSSam Leffler 	if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
1701b032f27cSSam Leffler 		return (selbs != NULL);
170210959256SSam Leffler 	if (selbs == NULL)
170310959256SSam Leffler 		goto notfound;
170410959256SSam Leffler 	chan = selbs->base.se_chan;
17058f4addc0SSam Leffler 	if (selbs->se_flags & STA_DEMOTE11B)
17068f4addc0SSam Leffler 		chan = demote11b(vap, chan);
1707d71a1f7aSAdrian Chadd 	/*
1708d71a1f7aSAdrian Chadd 	 * If HT is available, make it a possibility here.
1709d71a1f7aSAdrian Chadd 	 * The intent is to enable HT20/HT40 when joining a non-HT
1710d71a1f7aSAdrian Chadd 	 * IBSS node; we can then advertise HT IEs and speak HT
1711d71a1f7aSAdrian Chadd 	 * to any subsequent nodes that support it.
1712d71a1f7aSAdrian Chadd 	 */
1713d71a1f7aSAdrian Chadd 	chan = ieee80211_ht_adjust_channel(ic,
1714d71a1f7aSAdrian Chadd 	    chan, vap->iv_flags_ht);
171551172f62SAdrian Chadd 	chan = ieee80211_vht_adjust_channel(ic,
1716ef48d4faSBjoern A. Zeeb 	    chan, vap->iv_vht_flags);
171710959256SSam Leffler 	if (!ieee80211_sta_join(vap, chan, &selbs->base))
171868e8e04eSSam Leffler 		goto notfound;
171968e8e04eSSam Leffler 	return 1;				/* terminate scan */
172068e8e04eSSam Leffler }
172168e8e04eSSam Leffler 
172268e8e04eSSam Leffler /*
172368e8e04eSSam Leffler  * Age entries in the scan cache.
172468e8e04eSSam Leffler  */
172568e8e04eSSam Leffler static void
172668e8e04eSSam Leffler adhoc_age(struct ieee80211_scan_state *ss)
172768e8e04eSSam Leffler {
172868e8e04eSSam Leffler 	struct sta_table *st = ss->ss_priv;
172968e8e04eSSam Leffler 	struct sta_entry *se, *next;
173068e8e04eSSam Leffler 
173120c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_LOCK(st);
173268e8e04eSSam Leffler 	TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) {
173368e8e04eSSam Leffler 		if (se->se_notseen > STA_PURGE_SCANS) {
173468e8e04eSSam Leffler 			TAILQ_REMOVE(&st->st_entry, se, se_list);
173568e8e04eSSam Leffler 			LIST_REMOVE(se, se_hash);
1736b032f27cSSam Leffler 			ieee80211_ies_cleanup(&se->base.se_ies);
1737b9b53389SAdrian Chadd 			IEEE80211_FREE(se, M_80211_SCAN);
173868e8e04eSSam Leffler 		}
173968e8e04eSSam Leffler 	}
174020c3b3faSRui Paulo 	IEEE80211_SCAN_TABLE_UNLOCK(st);
174168e8e04eSSam Leffler }
174268e8e04eSSam Leffler 
174368e8e04eSSam Leffler static const struct ieee80211_scanner adhoc_default = {
174468e8e04eSSam Leffler 	.scan_name		= "default",
174568e8e04eSSam Leffler 	.scan_attach		= sta_attach,
174668e8e04eSSam Leffler 	.scan_detach		= sta_detach,
174768e8e04eSSam Leffler 	.scan_start		= adhoc_start,
174868e8e04eSSam Leffler 	.scan_restart		= sta_restart,
174968e8e04eSSam Leffler 	.scan_cancel		= sta_cancel,
175068e8e04eSSam Leffler 	.scan_end		= adhoc_pick_bss,
175168e8e04eSSam Leffler 	.scan_flush		= sta_flush,
1752b032f27cSSam Leffler 	.scan_pickchan		= adhoc_pick_channel,
175368e8e04eSSam Leffler 	.scan_add		= sta_add,
175468e8e04eSSam Leffler 	.scan_age		= adhoc_age,
175568e8e04eSSam Leffler 	.scan_iterate		= sta_iterate,
175668e8e04eSSam Leffler 	.scan_assoc_fail	= sta_assoc_fail,
175768e8e04eSSam Leffler 	.scan_assoc_success	= sta_assoc_success,
175868e8e04eSSam Leffler };
175959aa14a9SRui Paulo IEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default);
176059aa14a9SRui Paulo IEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default);
176168e8e04eSSam Leffler 
1762b032f27cSSam Leffler static int
1763b032f27cSSam Leffler ap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1764b032f27cSSam Leffler {
1765b032f27cSSam Leffler 	struct sta_table *st = ss->ss_priv;
1766b032f27cSSam Leffler 
1767b032f27cSSam Leffler 	makescanlist(ss, vap, staScanTable);
1768b032f27cSSam Leffler 
1769b032f27cSSam Leffler 	if (ss->ss_mindwell == 0)
1770b032f27cSSam Leffler 		ss->ss_mindwell = msecs_to_ticks(200);	/* 200ms */
1771b032f27cSSam Leffler 	if (ss->ss_maxdwell == 0)
1772b032f27cSSam Leffler 		ss->ss_maxdwell = msecs_to_ticks(200);	/* 200ms */
1773b032f27cSSam Leffler 
1774b032f27cSSam Leffler 	st->st_scangen++;
1775b032f27cSSam Leffler 	st->st_newscan = 1;
1776b032f27cSSam Leffler 
1777b032f27cSSam Leffler 	return 0;
1778b032f27cSSam Leffler }
1779b032f27cSSam Leffler 
1780b032f27cSSam Leffler /*
1781b032f27cSSam Leffler  * Cancel an ongoing scan.
1782b032f27cSSam Leffler  */
1783b032f27cSSam Leffler static int
1784b032f27cSSam Leffler ap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1785b032f27cSSam Leffler {
1786b032f27cSSam Leffler 	return 0;
1787b032f27cSSam Leffler }
1788b032f27cSSam Leffler 
1789b032f27cSSam Leffler /*
1790b032f27cSSam Leffler  * Pick a quiet channel to use for ap operation.
1791b032f27cSSam Leffler  */
1792b032f27cSSam Leffler static struct ieee80211_channel *
1793b032f27cSSam Leffler ap_pick_channel(struct ieee80211_scan_state *ss, int flags)
1794b032f27cSSam Leffler {
1795b032f27cSSam Leffler 	struct sta_table *st = ss->ss_priv;
1796b032f27cSSam Leffler 	struct ieee80211_channel *bestchan = NULL;
1797b032f27cSSam Leffler 	int i;
1798b032f27cSSam Leffler 
1799b032f27cSSam Leffler 	/* XXX select channel more intelligently, e.g. channel spread, power */
1800b032f27cSSam Leffler 	/* NB: use scan list order to preserve channel preference */
1801b032f27cSSam Leffler 	for (i = 0; i < ss->ss_last; i++) {
1802b032f27cSSam Leffler 		struct ieee80211_channel *chan = ss->ss_chans[i];
1803b032f27cSSam Leffler 		/*
1804b032f27cSSam Leffler 		 * If the channel is unoccupied the max rssi
1805b032f27cSSam Leffler 		 * should be zero; just take it.  Otherwise
1806b032f27cSSam Leffler 		 * track the channel with the lowest rssi and
1807b032f27cSSam Leffler 		 * use that when all channels appear occupied.
1808b032f27cSSam Leffler 		 */
1809b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_RADAR(chan))
1810b032f27cSSam Leffler 			continue;
1811b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_NOHOSTAP(chan))
1812b032f27cSSam Leffler 			continue;
1813b032f27cSSam Leffler 		/* check channel attributes for band compatibility */
1814b032f27cSSam Leffler 		if (flags != 0 && (chan->ic_flags & flags) != flags)
1815b032f27cSSam Leffler 			continue;
181631378b1cSSam Leffler 		KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size"));
1817b032f27cSSam Leffler 		/* XXX channel have interference */
1818b032f27cSSam Leffler 		if (st->st_maxrssi[chan->ic_ieee] == 0) {
1819b032f27cSSam Leffler 			/* XXX use other considerations */
1820b032f27cSSam Leffler 			return chan;
1821b032f27cSSam Leffler 		}
1822b032f27cSSam Leffler 		if (bestchan == NULL ||
1823b032f27cSSam Leffler 		    st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee])
1824b032f27cSSam Leffler 			bestchan = chan;
1825b032f27cSSam Leffler 	}
1826b032f27cSSam Leffler 	return bestchan;
1827b032f27cSSam Leffler }
1828b032f27cSSam Leffler 
1829b032f27cSSam Leffler /*
1830b032f27cSSam Leffler  * Pick a quiet channel to use for ap operation.
1831b032f27cSSam Leffler  */
1832b032f27cSSam Leffler static int
1833b032f27cSSam Leffler ap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
1834b032f27cSSam Leffler {
1835b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
183651172f62SAdrian Chadd 	struct ieee80211_channel *bestchan, *chan;
1837b032f27cSSam Leffler 
1838b032f27cSSam Leffler 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
1839b032f27cSSam Leffler 		("wrong opmode %u", vap->iv_opmode));
1840b032f27cSSam Leffler 	bestchan = ap_pick_channel(ss, 0);
1841b032f27cSSam Leffler 	if (bestchan == NULL) {
1842b032f27cSSam Leffler 		/* no suitable channel, should not happen */
1843b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
1844b032f27cSSam Leffler 		    "%s: no suitable channel! (should not happen)\n", __func__);
1845b032f27cSSam Leffler 		/* XXX print something? */
1846b032f27cSSam Leffler 		return 0;			/* restart scan */
1847b032f27cSSam Leffler 	}
1848b032f27cSSam Leffler 	/*
1849b032f27cSSam Leffler 	 * If this is a dynamic turbo channel, start with the unboosted one.
1850b032f27cSSam Leffler 	 */
1851b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_TURBO(bestchan)) {
1852b032f27cSSam Leffler 		bestchan = ieee80211_find_channel(ic, bestchan->ic_freq,
1853b032f27cSSam Leffler 			bestchan->ic_flags & ~IEEE80211_CHAN_TURBO);
1854b032f27cSSam Leffler 		if (bestchan == NULL) {
1855b032f27cSSam Leffler 			/* should never happen ?? */
1856b032f27cSSam Leffler 			return 0;
1857b032f27cSSam Leffler 		}
1858b032f27cSSam Leffler 	}
1859b032f27cSSam Leffler 	if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) {
1860b032f27cSSam Leffler 		/*
1861b032f27cSSam Leffler 		 * Manual/background scan, don't select+join the
1862b032f27cSSam Leffler 		 * bss, just return.  The scanning framework will
1863b032f27cSSam Leffler 		 * handle notification that this has completed.
1864b032f27cSSam Leffler 		 */
1865b032f27cSSam Leffler 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
1866b032f27cSSam Leffler 		return 1;
1867b032f27cSSam Leffler 	}
186851172f62SAdrian Chadd 	chan = ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ht);
1869ef48d4faSBjoern A. Zeeb 	chan = ieee80211_vht_adjust_channel(ic, chan, vap->iv_vht_flags);
187051172f62SAdrian Chadd 	ieee80211_create_ibss(vap, chan);
187151172f62SAdrian Chadd 
1872b032f27cSSam Leffler 	return 1;
1873b032f27cSSam Leffler }
1874b032f27cSSam Leffler 
1875b032f27cSSam Leffler static const struct ieee80211_scanner ap_default = {
1876b032f27cSSam Leffler 	.scan_name		= "default",
1877b032f27cSSam Leffler 	.scan_attach		= sta_attach,
1878b032f27cSSam Leffler 	.scan_detach		= sta_detach,
1879b032f27cSSam Leffler 	.scan_start		= ap_start,
1880b032f27cSSam Leffler 	.scan_restart		= sta_restart,
1881b032f27cSSam Leffler 	.scan_cancel		= ap_cancel,
1882b032f27cSSam Leffler 	.scan_end		= ap_end,
1883b032f27cSSam Leffler 	.scan_flush		= sta_flush,
1884b032f27cSSam Leffler 	.scan_pickchan		= ap_pick_channel,
1885b032f27cSSam Leffler 	.scan_add		= sta_add,
1886b032f27cSSam Leffler 	.scan_age		= adhoc_age,
1887b032f27cSSam Leffler 	.scan_iterate		= sta_iterate,
1888b032f27cSSam Leffler 	.scan_assoc_success	= sta_assoc_success,
1889b032f27cSSam Leffler 	.scan_assoc_fail	= sta_assoc_fail,
1890b032f27cSSam Leffler };
1891b032f27cSSam Leffler IEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
189259aa14a9SRui Paulo 
189359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
189459aa14a9SRui Paulo /*
189559aa14a9SRui Paulo  * Pick an mbss network to join or find a channel
189659aa14a9SRui Paulo  * to use to start an mbss network.
189759aa14a9SRui Paulo  */
189859aa14a9SRui Paulo static int
189959aa14a9SRui Paulo mesh_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap)
190059aa14a9SRui Paulo {
190159aa14a9SRui Paulo 	struct sta_table *st = ss->ss_priv;
190259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
190359aa14a9SRui Paulo 	struct sta_entry *selbs;
190459aa14a9SRui Paulo 	struct ieee80211_channel *chan;
190559aa14a9SRui Paulo 
190659aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
190759aa14a9SRui Paulo 		("wrong opmode %u", vap->iv_opmode));
190859aa14a9SRui Paulo 
190959aa14a9SRui Paulo 	if (st->st_newscan) {
191059aa14a9SRui Paulo 		sta_update_notseen(st);
191159aa14a9SRui Paulo 		st->st_newscan = 0;
191259aa14a9SRui Paulo 	}
191359aa14a9SRui Paulo 	if (ss->ss_flags & IEEE80211_SCAN_NOPICK) {
191459aa14a9SRui Paulo 		/*
191559aa14a9SRui Paulo 		 * Manual/background scan, don't select+join the
191659aa14a9SRui Paulo 		 * bss, just return.  The scanning framework will
191759aa14a9SRui Paulo 		 * handle notification that this has completed.
191859aa14a9SRui Paulo 		 */
191959aa14a9SRui Paulo 		ss->ss_flags &= ~IEEE80211_SCAN_NOPICK;
192059aa14a9SRui Paulo 		return 1;
192159aa14a9SRui Paulo 	}
192259aa14a9SRui Paulo 	/*
192359aa14a9SRui Paulo 	 * Automatic sequencing; look for a candidate and
192459aa14a9SRui Paulo 	 * if found join the network.
192559aa14a9SRui Paulo 	 */
192659aa14a9SRui Paulo 	/* NB: unlocked read should be ok */
192759aa14a9SRui Paulo 	if (TAILQ_FIRST(&st->st_entry) == NULL) {
192859aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
192959aa14a9SRui Paulo 			"%s: no scan candidate\n", __func__);
193059aa14a9SRui Paulo 		if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
193159aa14a9SRui Paulo 			return 0;
193259aa14a9SRui Paulo notfound:
193359aa14a9SRui Paulo 		if (ms->ms_idlen != 0) {
193459aa14a9SRui Paulo 			/*
193559aa14a9SRui Paulo 			 * No existing mbss network to join and we have
193659aa14a9SRui Paulo 			 * a meshid; start one up.  If no channel was
193759aa14a9SRui Paulo 			 * specified, try to select a channel.
193859aa14a9SRui Paulo 			 */
193959aa14a9SRui Paulo 			if (vap->iv_des_chan == IEEE80211_CHAN_ANYC ||
194059aa14a9SRui Paulo 			    IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
194159aa14a9SRui Paulo 				struct ieee80211com *ic = vap->iv_ic;
194259aa14a9SRui Paulo 
194351172f62SAdrian Chadd 				/* XXX VHT */
194459aa14a9SRui Paulo 				chan = adhoc_pick_channel(ss, 0);
194551172f62SAdrian Chadd 				if (chan != NULL) {
194659aa14a9SRui Paulo 					chan = ieee80211_ht_adjust_channel(ic,
194759aa14a9SRui Paulo 					    chan, vap->iv_flags_ht);
194851172f62SAdrian Chadd 					chan = ieee80211_vht_adjust_channel(ic,
1949ef48d4faSBjoern A. Zeeb 					    chan, vap->iv_vht_flags);
195051172f62SAdrian Chadd 					}
195159aa14a9SRui Paulo 			} else
195259aa14a9SRui Paulo 				chan = vap->iv_des_chan;
195359aa14a9SRui Paulo 			if (chan != NULL) {
195459aa14a9SRui Paulo 				ieee80211_create_ibss(vap, chan);
195559aa14a9SRui Paulo 				return 1;
195659aa14a9SRui Paulo 			}
195759aa14a9SRui Paulo 		}
195859aa14a9SRui Paulo 		/*
195959aa14a9SRui Paulo 		 * If nothing suitable was found decrement
196059aa14a9SRui Paulo 		 * the failure counts so entries will be
196159aa14a9SRui Paulo 		 * reconsidered the next time around.  We
196259aa14a9SRui Paulo 		 * really want to do this only for sta's
196359aa14a9SRui Paulo 		 * where we've previously had some success.
196459aa14a9SRui Paulo 		 */
196559aa14a9SRui Paulo 		sta_dec_fails(st);
196659aa14a9SRui Paulo 		st->st_newscan = 1;
196759aa14a9SRui Paulo 		return 0;			/* restart scan */
196859aa14a9SRui Paulo 	}
196959aa14a9SRui Paulo 	selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN);
197059aa14a9SRui Paulo 	if (ss->ss_flags & IEEE80211_SCAN_NOJOIN)
197159aa14a9SRui Paulo 		return (selbs != NULL);
197259aa14a9SRui Paulo 	if (selbs == NULL)
197359aa14a9SRui Paulo 		goto notfound;
197459aa14a9SRui Paulo 	chan = selbs->base.se_chan;
197559aa14a9SRui Paulo 	if (selbs->se_flags & STA_DEMOTE11B)
197659aa14a9SRui Paulo 		chan = demote11b(vap, chan);
197759aa14a9SRui Paulo 	if (!ieee80211_sta_join(vap, chan, &selbs->base))
197859aa14a9SRui Paulo 		goto notfound;
197959aa14a9SRui Paulo 	return 1;				/* terminate scan */
198059aa14a9SRui Paulo }
198159aa14a9SRui Paulo 
198259aa14a9SRui Paulo static const struct ieee80211_scanner mesh_default = {
198359aa14a9SRui Paulo 	.scan_name		= "default",
198459aa14a9SRui Paulo 	.scan_attach		= sta_attach,
198559aa14a9SRui Paulo 	.scan_detach		= sta_detach,
198659aa14a9SRui Paulo 	.scan_start		= adhoc_start,
198759aa14a9SRui Paulo 	.scan_restart		= sta_restart,
198859aa14a9SRui Paulo 	.scan_cancel		= sta_cancel,
198959aa14a9SRui Paulo 	.scan_end		= mesh_pick_bss,
199059aa14a9SRui Paulo 	.scan_flush		= sta_flush,
199159aa14a9SRui Paulo 	.scan_pickchan		= adhoc_pick_channel,
199259aa14a9SRui Paulo 	.scan_add		= sta_add,
199359aa14a9SRui Paulo 	.scan_age		= adhoc_age,
199459aa14a9SRui Paulo 	.scan_iterate		= sta_iterate,
199559aa14a9SRui Paulo 	.scan_assoc_fail	= sta_assoc_fail,
199659aa14a9SRui Paulo 	.scan_assoc_success	= sta_assoc_success,
199759aa14a9SRui Paulo };
199859aa14a9SRui Paulo IEEE80211_SCANNER_ALG(mesh, IEEE80211_M_MBSS, mesh_default);
199959aa14a9SRui Paulo #endif /* IEEE80211_SUPPORT_MESH */
2000