xref: /freebsd/sys/net80211/ieee80211.c (revision ba2c1fbc03f312b978f76f7b6c67eec6afa80bf8)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler /*
311a1e1d21SSam Leffler  * IEEE 802.11 generic handler
321a1e1d21SSam Leffler  */
33b032f27cSSam Leffler #include "opt_wlan.h"
341a1e1d21SSam Leffler 
351a1e1d21SSam Leffler #include <sys/param.h>
361a1e1d21SSam Leffler #include <sys/systm.h>
371a1e1d21SSam Leffler #include <sys/kernel.h>
388a1b9b6aSSam Leffler #include <sys/socket.h>
391a1e1d21SSam Leffler 
40c8f5794eSGleb Smirnoff #include <machine/stdarg.h>
41c8f5794eSGleb Smirnoff 
421a1e1d21SSam Leffler #include <net/if.h>
4376039bc8SGleb Smirnoff #include <net/if_var.h>
44b032f27cSSam Leffler #include <net/if_dl.h>
451a1e1d21SSam Leffler #include <net/if_media.h>
46b032f27cSSam Leffler #include <net/if_types.h>
471a1e1d21SSam Leffler #include <net/ethernet.h>
481a1e1d21SSam Leffler 
491a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
50b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
51616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
52616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
53616190d0SSam Leffler #endif
54b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
551a1e1d21SSam Leffler 
561a1e1d21SSam Leffler #include <net/bpf.h>
571a1e1d21SSam Leffler 
58bb77492fSSam Leffler const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
59bb77492fSSam Leffler 	[IEEE80211_MODE_AUTO]	  = "auto",
60bb77492fSSam Leffler 	[IEEE80211_MODE_11A]	  = "11a",
61bb77492fSSam Leffler 	[IEEE80211_MODE_11B]	  = "11b",
62bb77492fSSam Leffler 	[IEEE80211_MODE_11G]	  = "11g",
63bb77492fSSam Leffler 	[IEEE80211_MODE_FH]	  = "FH",
64bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_A]  = "turboA",
65bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_G]  = "turboG",
66bb77492fSSam Leffler 	[IEEE80211_MODE_STURBO_A] = "sturboA",
676a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	  = "half",
686a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]  = "quarter",
69bb77492fSSam Leffler 	[IEEE80211_MODE_11NA]	  = "11na",
70bb77492fSSam Leffler 	[IEEE80211_MODE_11NG]	  = "11ng",
711a1e1d21SSam Leffler };
72c43feedeSSam Leffler /* map ieee80211_opmode to the corresponding capability bit */
73c43feedeSSam Leffler const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
74c43feedeSSam Leffler 	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
75c43feedeSSam Leffler 	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
76c43feedeSSam Leffler 	[IEEE80211_M_STA]	= IEEE80211_C_STA,
77c43feedeSSam Leffler 	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
78c43feedeSSam Leffler 	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
79c43feedeSSam Leffler 	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
8059aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
8159aa14a9SRui Paulo 	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
8259aa14a9SRui Paulo #endif
83c43feedeSSam Leffler };
84c43feedeSSam Leffler 
8592002144SGleb Smirnoff const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
86b032f27cSSam Leffler 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
87b032f27cSSam Leffler 
88b032f27cSSam Leffler static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
892bfc8a91SSam Leffler static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
90b032f27cSSam Leffler static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
91b032f27cSSam Leffler static	int ieee80211_media_setup(struct ieee80211com *ic,
92b032f27cSSam Leffler 		struct ifmedia *media, int caps, int addsta,
93b032f27cSSam Leffler 		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
94*ba2c1fbcSAdrian Chadd static	void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
95*ba2c1fbcSAdrian Chadd static	int ieee80211com_media_change(struct ifnet *);
96b032f27cSSam Leffler static	int media_status(enum ieee80211_opmode,
97b032f27cSSam Leffler 		const struct ieee80211_channel *);
9828da1b56SGleb Smirnoff static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
99b032f27cSSam Leffler 
100b032f27cSSam Leffler MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
1011a1e1d21SSam Leffler 
102aadecb1aSSam Leffler /*
103aadecb1aSSam Leffler  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
104aadecb1aSSam Leffler  */
105aadecb1aSSam Leffler #define	B(r)	((r) | IEEE80211_RATE_BASIC)
106aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11a =
107aadecb1aSSam Leffler 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
10841b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_half =
10941b3c790SSam Leffler 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
11041b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_quarter =
11141b3c790SSam Leffler 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11b =
113aadecb1aSSam Leffler 	{ 4, { B(2), B(4), B(11), B(22) } };
114aadecb1aSSam Leffler /* NB: OFDM rates are handled specially based on mode */
115aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11g =
116aadecb1aSSam Leffler 	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
117aadecb1aSSam Leffler #undef B
118aadecb1aSSam Leffler 
1191a1e1d21SSam Leffler /*
1201a1e1d21SSam Leffler  * Fill in 802.11 available channel set, mark
1211a1e1d21SSam Leffler  * all available channels as active, and pick
1221a1e1d21SSam Leffler  * a default channel if not already specified.
1231a1e1d21SSam Leffler  */
124*ba2c1fbcSAdrian Chadd static void
12541b3c790SSam Leffler ieee80211_chan_init(struct ieee80211com *ic)
12641b3c790SSam Leffler {
12741b3c790SSam Leffler #define	DEFAULTRATES(m, def) do { \
1286a76ae21SSam Leffler 	if (ic->ic_sup_rates[m].rs_nrates == 0) \
12945fa8b0eSSam Leffler 		ic->ic_sup_rates[m] = def; \
13041b3c790SSam Leffler } while (0)
13141b3c790SSam Leffler 	struct ieee80211_channel *c;
13241b3c790SSam Leffler 	int i;
13341b3c790SSam Leffler 
13431378b1cSSam Leffler 	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
13568e8e04eSSam Leffler 		("invalid number of channels specified: %u", ic->ic_nchans));
1361a1e1d21SSam Leffler 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
137b032f27cSSam Leffler 	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
1386dbd16f1SSam Leffler 	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
13968e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1401a1e1d21SSam Leffler 		c = &ic->ic_channels[i];
14168e8e04eSSam Leffler 		KASSERT(c->ic_flags != 0, ("channel with no flags"));
1429c2c544dSSam Leffler 		/*
1439c2c544dSSam Leffler 		 * Help drivers that work only with frequencies by filling
1449c2c544dSSam Leffler 		 * in IEEE channel #'s if not already calculated.  Note this
1459c2c544dSSam Leffler 		 * mimics similar work done in ieee80211_setregdomain when
1469c2c544dSSam Leffler 		 * changing regulatory state.
1479c2c544dSSam Leffler 		 */
1489c2c544dSSam Leffler 		if (c->ic_ieee == 0)
1499c2c544dSSam Leffler 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
1509c2c544dSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
1519c2c544dSSam Leffler 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
1529c2c544dSSam Leffler 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
1539c2c544dSSam Leffler 			    c->ic_flags);
1549c2c544dSSam Leffler 		/* default max tx power to max regulatory */
1559c2c544dSSam Leffler 		if (c->ic_maxpower == 0)
1569c2c544dSSam Leffler 			c->ic_maxpower = 2*c->ic_maxregpower;
15768e8e04eSSam Leffler 		setbit(ic->ic_chan_avail, c->ic_ieee);
1581a1e1d21SSam Leffler 		/*
1591a1e1d21SSam Leffler 		 * Identify mode capabilities.
1601a1e1d21SSam Leffler 		 */
1611a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_A(c))
1626dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
1631a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_B(c))
1646dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
16545fa8b0eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(c))
1666dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
1674844aa7dSAtsushi Onoe 		if (IEEE80211_IS_CHAN_FHSS(c))
1686dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
16968e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_108A(c))
1706dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
1718a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_108G(c))
1726dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
17368e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
17468e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
1756a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_HALF(c))
1766a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
1776a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_QUARTER(c))
1786a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
17968e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTA(c))
18068e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
18168e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTG(c))
18268e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
18368e8e04eSSam Leffler 	}
18468e8e04eSSam Leffler 	/* initialize candidate channels to all available */
18568e8e04eSSam Leffler 	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
18668e8e04eSSam Leffler 		sizeof(ic->ic_chan_avail));
18768e8e04eSSam Leffler 
188b032f27cSSam Leffler 	/* sort channel table to allow lookup optimizations */
189b032f27cSSam Leffler 	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
190b032f27cSSam Leffler 
191b032f27cSSam Leffler 	/* invalidate any previous state */
19268e8e04eSSam Leffler 	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
193ab562eefSSam Leffler 	ic->ic_prevchan = NULL;
194b032f27cSSam Leffler 	ic->ic_csa_newchan = NULL;
195b5c99415SSam Leffler 	/* arbitrarily pick the first channel */
19668e8e04eSSam Leffler 	ic->ic_curchan = &ic->ic_channels[0];
19726d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
198aadecb1aSSam Leffler 
199aadecb1aSSam Leffler 	/* fillin well-known rate sets if driver has not specified */
20041b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
20141b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
20241b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
20341b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
20441b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
2058500d65dSSam Leffler 	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
2066a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
2076a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
20840432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
20940432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
21041b3c790SSam Leffler 
21141b3c790SSam Leffler 	/*
212fbbe47a9SBernhard Schmidt 	 * Setup required information to fill the mcsset field, if driver did
213fbbe47a9SBernhard Schmidt 	 * not. Assume a 2T2R setup for historic reasons.
214fbbe47a9SBernhard Schmidt 	 */
215fbbe47a9SBernhard Schmidt 	if (ic->ic_rxstream == 0)
216fbbe47a9SBernhard Schmidt 		ic->ic_rxstream = 2;
217fbbe47a9SBernhard Schmidt 	if (ic->ic_txstream == 0)
218fbbe47a9SBernhard Schmidt 		ic->ic_txstream = 2;
219fbbe47a9SBernhard Schmidt 
220fbbe47a9SBernhard Schmidt 	/*
22141b3c790SSam Leffler 	 * Set auto mode to reset active channel state and any desired channel.
22241b3c790SSam Leffler 	 */
22341b3c790SSam Leffler 	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
22441b3c790SSam Leffler #undef DEFAULTRATES
22541b3c790SSam Leffler }
22641b3c790SSam Leffler 
227b032f27cSSam Leffler static void
228272f6adeSGleb Smirnoff null_update_mcast(struct ieee80211com *ic)
229b032f27cSSam Leffler {
230272f6adeSGleb Smirnoff 
231272f6adeSGleb Smirnoff 	ic_printf(ic, "need multicast update callback\n");
232b032f27cSSam Leffler }
233b032f27cSSam Leffler 
234b032f27cSSam Leffler static void
235272f6adeSGleb Smirnoff null_update_promisc(struct ieee80211com *ic)
236b032f27cSSam Leffler {
237272f6adeSGleb Smirnoff 
238272f6adeSGleb Smirnoff 	ic_printf(ic, "need promiscuous mode update callback\n");
239b032f27cSSam Leffler }
240b032f27cSSam Leffler 
241*ba2c1fbcSAdrian Chadd static int
242*ba2c1fbcSAdrian Chadd null_transmit(struct ifnet *ifp, struct mbuf *m)
243*ba2c1fbcSAdrian Chadd {
244*ba2c1fbcSAdrian Chadd 	m_freem(m);
245*ba2c1fbcSAdrian Chadd 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
246*ba2c1fbcSAdrian Chadd 	return EACCES;		/* XXX EIO/EPERM? */
247*ba2c1fbcSAdrian Chadd }
248*ba2c1fbcSAdrian Chadd 
249*ba2c1fbcSAdrian Chadd static int
250*ba2c1fbcSAdrian Chadd null_output(struct ifnet *ifp, struct mbuf *m,
251*ba2c1fbcSAdrian Chadd 	const struct sockaddr *dst, struct route *ro)
252*ba2c1fbcSAdrian Chadd {
253*ba2c1fbcSAdrian Chadd 	if_printf(ifp, "discard raw packet\n");
254*ba2c1fbcSAdrian Chadd 	return null_transmit(ifp, m);
255*ba2c1fbcSAdrian Chadd }
256*ba2c1fbcSAdrian Chadd 
257*ba2c1fbcSAdrian Chadd static void
258*ba2c1fbcSAdrian Chadd null_input(struct ifnet *ifp, struct mbuf *m)
259*ba2c1fbcSAdrian Chadd {
260*ba2c1fbcSAdrian Chadd 	if_printf(ifp, "if_input should not be called\n");
261*ba2c1fbcSAdrian Chadd 	m_freem(m);
262*ba2c1fbcSAdrian Chadd }
263*ba2c1fbcSAdrian Chadd 
264b94299c4SAdrian Chadd static void
265b94299c4SAdrian Chadd null_update_chw(struct ieee80211com *ic)
266b94299c4SAdrian Chadd {
267b94299c4SAdrian Chadd 
268c8f5794eSGleb Smirnoff 	ic_printf(ic, "%s: need callback\n", __func__);
269c8f5794eSGleb Smirnoff }
270c8f5794eSGleb Smirnoff 
271c8f5794eSGleb Smirnoff int
272c8f5794eSGleb Smirnoff ic_printf(struct ieee80211com *ic, const char * fmt, ...)
273c8f5794eSGleb Smirnoff {
274c8f5794eSGleb Smirnoff 	va_list ap;
275c8f5794eSGleb Smirnoff 	int retval;
276c8f5794eSGleb Smirnoff 
277c8f5794eSGleb Smirnoff 	retval = printf("%s: ", ic->ic_name);
278c8f5794eSGleb Smirnoff 	va_start(ap, fmt);
279c8f5794eSGleb Smirnoff 	retval += vprintf(fmt, ap);
280c8f5794eSGleb Smirnoff 	va_end(ap);
281c8f5794eSGleb Smirnoff 	return (retval);
282b94299c4SAdrian Chadd }
283b94299c4SAdrian Chadd 
284b032f27cSSam Leffler /*
285b032f27cSSam Leffler  * Attach/setup the common net80211 state.  Called by
286b032f27cSSam Leffler  * the driver on attach to prior to creating any vap's.
287b032f27cSSam Leffler  */
28841b3c790SSam Leffler void
289*ba2c1fbcSAdrian Chadd ieee80211_ifattach(struct ieee80211com *ic,
290*ba2c1fbcSAdrian Chadd 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
29141b3c790SSam Leffler {
292*ba2c1fbcSAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
293*ba2c1fbcSAdrian Chadd 	struct sockaddr_dl *sdl;
294*ba2c1fbcSAdrian Chadd 	struct ifaddr *ifa;
295*ba2c1fbcSAdrian Chadd 
296*ba2c1fbcSAdrian Chadd 	KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
29741b3c790SSam Leffler 
298c8f5794eSGleb Smirnoff 	IEEE80211_LOCK_INIT(ic, ic->ic_name);
299c8f5794eSGleb Smirnoff 	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
300b032f27cSSam Leffler 	TAILQ_INIT(&ic->ic_vaps);
3015efea30fSAndrew Thompson 
3025efea30fSAndrew Thompson 	/* Create a taskqueue for all state changes */
3035efea30fSAndrew Thompson 	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
3045efea30fSAndrew Thompson 	    taskqueue_thread_enqueue, &ic->ic_tq);
3057b2b15ebSAdrian Chadd 	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
3067fc10b6bSGleb Smirnoff 	    ic->ic_name);
30728da1b56SGleb Smirnoff 	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
30828da1b56SGleb Smirnoff 	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
30941b3c790SSam Leffler 	/*
31041b3c790SSam Leffler 	 * Fill in 802.11 available channel set, mark all
31141b3c790SSam Leffler 	 * available channels as active, and pick a default
31241b3c790SSam Leffler 	 * channel if not already specified.
31341b3c790SSam Leffler 	 */
314*ba2c1fbcSAdrian Chadd 	ieee80211_media_init(ic);
31568e8e04eSSam Leffler 
316b032f27cSSam Leffler 	ic->ic_update_mcast = null_update_mcast;
317b032f27cSSam Leffler 	ic->ic_update_promisc = null_update_promisc;
318b94299c4SAdrian Chadd 	ic->ic_update_chw = null_update_chw;
3191a1e1d21SSam Leffler 
3205b16c28cSSam Leffler 	ic->ic_hash_key = arc4random();
321d365f9c7SSam Leffler 	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
322d365f9c7SSam Leffler 	ic->ic_lintval = ic->ic_bintval;
3238a1b9b6aSSam Leffler 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
3248a1b9b6aSSam Leffler 
32568e8e04eSSam Leffler 	ieee80211_crypto_attach(ic);
3268a1b9b6aSSam Leffler 	ieee80211_node_attach(ic);
32768e8e04eSSam Leffler 	ieee80211_power_attach(ic);
3288a1b9b6aSSam Leffler 	ieee80211_proto_attach(ic);
329616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
330616190d0SSam Leffler 	ieee80211_superg_attach(ic);
331616190d0SSam Leffler #endif
33268e8e04eSSam Leffler 	ieee80211_ht_attach(ic);
33368e8e04eSSam Leffler 	ieee80211_scan_attach(ic);
334b032f27cSSam Leffler 	ieee80211_regdomain_attach(ic);
335e95e0edbSSam Leffler 	ieee80211_dfs_attach(ic);
3368a1b9b6aSSam Leffler 
337b032f27cSSam Leffler 	ieee80211_sysctl_attach(ic);
3388a1b9b6aSSam Leffler 
339*ba2c1fbcSAdrian Chadd 	ifp->if_addrlen = IEEE80211_ADDR_LEN;
340*ba2c1fbcSAdrian Chadd 	ifp->if_hdrlen = 0;
341*ba2c1fbcSAdrian Chadd 
342*ba2c1fbcSAdrian Chadd 	CURVNET_SET(vnet0);
343*ba2c1fbcSAdrian Chadd 
344*ba2c1fbcSAdrian Chadd 	if_attach(ifp);
345*ba2c1fbcSAdrian Chadd 
346*ba2c1fbcSAdrian Chadd 	ifp->if_mtu = IEEE80211_MTU_MAX;
347*ba2c1fbcSAdrian Chadd 	ifp->if_broadcastaddr = ieee80211broadcastaddr;
348*ba2c1fbcSAdrian Chadd 	ifp->if_output = null_output;
349*ba2c1fbcSAdrian Chadd 	ifp->if_input = null_input;	/* just in case */
350*ba2c1fbcSAdrian Chadd 	ifp->if_resolvemulti = NULL;	/* NB: callers check */
351*ba2c1fbcSAdrian Chadd 
352*ba2c1fbcSAdrian Chadd 	ifa = ifaddr_byindex(ifp->if_index);
353*ba2c1fbcSAdrian Chadd 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
354*ba2c1fbcSAdrian Chadd 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
355*ba2c1fbcSAdrian Chadd 	sdl->sdl_type = IFT_ETHER;		/* XXX IFT_IEEE80211? */
356*ba2c1fbcSAdrian Chadd 	sdl->sdl_alen = IEEE80211_ADDR_LEN;
357*ba2c1fbcSAdrian Chadd 	IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
358*ba2c1fbcSAdrian Chadd 	ifa_free(ifa);
359*ba2c1fbcSAdrian Chadd 
360*ba2c1fbcSAdrian Chadd 	CURVNET_RESTORE();
3611a1e1d21SSam Leffler }
3621a1e1d21SSam Leffler 
363b032f27cSSam Leffler /*
364b032f27cSSam Leffler  * Detach net80211 state on device detach.  Tear down
365b032f27cSSam Leffler  * all vap's and reclaim all common state prior to the
366b032f27cSSam Leffler  * device state going away.  Note we may call back into
367b032f27cSSam Leffler  * driver; it must be prepared for this.
368b032f27cSSam Leffler  */
3691a1e1d21SSam Leffler void
3708a1b9b6aSSam Leffler ieee80211_ifdetach(struct ieee80211com *ic)
3711a1e1d21SSam Leffler {
372*ba2c1fbcSAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
373b032f27cSSam Leffler 	struct ieee80211vap *vap;
3741a1e1d21SSam Leffler 
375*ba2c1fbcSAdrian Chadd 	/*
376*ba2c1fbcSAdrian Chadd 	 * This detaches the main interface, but not the vaps.
377*ba2c1fbcSAdrian Chadd 	 * Each VAP may be in a separate VIMAGE.
378*ba2c1fbcSAdrian Chadd 	 */
379*ba2c1fbcSAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
380*ba2c1fbcSAdrian Chadd 	if_detach(ifp);
381*ba2c1fbcSAdrian Chadd 	CURVNET_RESTORE();
3825c600a90SSam Leffler 
38330e4856aSAdrian Chadd 	/*
38430e4856aSAdrian Chadd 	 * The VAP is responsible for setting and clearing
38530e4856aSAdrian Chadd 	 * the VIMAGE context.
38630e4856aSAdrian Chadd 	 */
387b032f27cSSam Leffler 	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
388b032f27cSSam Leffler 		ieee80211_vap_destroy(vap);
389ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
3908a1b9b6aSSam Leffler 
3918a1b9b6aSSam Leffler 	ieee80211_sysctl_detach(ic);
392e95e0edbSSam Leffler 	ieee80211_dfs_detach(ic);
393b032f27cSSam Leffler 	ieee80211_regdomain_detach(ic);
39468e8e04eSSam Leffler 	ieee80211_scan_detach(ic);
395616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
396616190d0SSam Leffler 	ieee80211_superg_detach(ic);
397616190d0SSam Leffler #endif
39868e8e04eSSam Leffler 	ieee80211_ht_detach(ic);
399ca4ac7aeSSam Leffler 	/* NB: must be called before ieee80211_node_detach */
4008a1b9b6aSSam Leffler 	ieee80211_proto_detach(ic);
4018a1b9b6aSSam Leffler 	ieee80211_crypto_detach(ic);
40268e8e04eSSam Leffler 	ieee80211_power_detach(ic);
4038a1b9b6aSSam Leffler 	ieee80211_node_detach(ic);
4048a1b9b6aSSam Leffler 
405*ba2c1fbcSAdrian Chadd 	/* XXX VNET needed? */
406*ba2c1fbcSAdrian Chadd 	ifmedia_removeall(&ic->ic_media);
40728da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_ierrors);
40828da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_oerrors);
40930e4856aSAdrian Chadd 
4105efea30fSAndrew Thompson 	taskqueue_free(ic->ic_tq);
4115cda6006SAdrian Chadd 	IEEE80211_TX_LOCK_DESTROY(ic);
41268e8e04eSSam Leffler 	IEEE80211_LOCK_DESTROY(ic);
413b032f27cSSam Leffler }
4148a1b9b6aSSam Leffler 
415b032f27cSSam Leffler /*
416b032f27cSSam Leffler  * Default reset method for use with the ioctl support.  This
417b032f27cSSam Leffler  * method is invoked after any state change in the 802.11
418b032f27cSSam Leffler  * layer that should be propagated to the hardware but not
419b032f27cSSam Leffler  * require re-initialization of the 802.11 state machine (e.g
420b032f27cSSam Leffler  * rescanning for an ap).  We always return ENETRESET which
421b032f27cSSam Leffler  * should cause the driver to re-initialize the device. Drivers
422b032f27cSSam Leffler  * can override this method to implement more optimized support.
423b032f27cSSam Leffler  */
424b032f27cSSam Leffler static int
425b032f27cSSam Leffler default_reset(struct ieee80211vap *vap, u_long cmd)
426b032f27cSSam Leffler {
427b032f27cSSam Leffler 	return ENETRESET;
428b032f27cSSam Leffler }
429b032f27cSSam Leffler 
430b032f27cSSam Leffler /*
43128da1b56SGleb Smirnoff  * Add underlying device errors to vap errors.
43228da1b56SGleb Smirnoff  */
43328da1b56SGleb Smirnoff static uint64_t
43428da1b56SGleb Smirnoff ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
43528da1b56SGleb Smirnoff {
43628da1b56SGleb Smirnoff 	struct ieee80211vap *vap = ifp->if_softc;
43728da1b56SGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
43828da1b56SGleb Smirnoff 	uint64_t rv;
43928da1b56SGleb Smirnoff 
44028da1b56SGleb Smirnoff 	rv = if_get_counter_default(ifp, cnt);
44128da1b56SGleb Smirnoff 	switch (cnt) {
44228da1b56SGleb Smirnoff 	case IFCOUNTER_OERRORS:
44328da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_oerrors);
44428da1b56SGleb Smirnoff 		break;
44528da1b56SGleb Smirnoff 	case IFCOUNTER_IERRORS:
44628da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_ierrors);
44728da1b56SGleb Smirnoff 		break;
44828da1b56SGleb Smirnoff 	default:
44928da1b56SGleb Smirnoff 		break;
45028da1b56SGleb Smirnoff 	}
45128da1b56SGleb Smirnoff 
45228da1b56SGleb Smirnoff 	return (rv);
45328da1b56SGleb Smirnoff }
45428da1b56SGleb Smirnoff 
45528da1b56SGleb Smirnoff /*
456b032f27cSSam Leffler  * Prepare a vap for use.  Drivers use this call to
457b032f27cSSam Leffler  * setup net80211 state in new vap's prior attaching
458b032f27cSSam Leffler  * them with ieee80211_vap_attach (below).
459b032f27cSSam Leffler  */
460b032f27cSSam Leffler int
461b032f27cSSam Leffler ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
462fcd9500fSBernhard Schmidt     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
463*ba2c1fbcSAdrian Chadd     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
464*ba2c1fbcSAdrian Chadd     const uint8_t macaddr[IEEE80211_ADDR_LEN])
465b032f27cSSam Leffler {
466b032f27cSSam Leffler 	struct ifnet *ifp;
467b032f27cSSam Leffler 
468b032f27cSSam Leffler 	ifp = if_alloc(IFT_ETHER);
469b032f27cSSam Leffler 	if (ifp == NULL) {
470c8f5794eSGleb Smirnoff 		ic_printf(ic, "%s: unable to allocate ifnet\n",
471b032f27cSSam Leffler 		    __func__);
472b032f27cSSam Leffler 		return ENOMEM;
473b032f27cSSam Leffler 	}
474b032f27cSSam Leffler 	if_initname(ifp, name, unit);
475b032f27cSSam Leffler 	ifp->if_softc = vap;			/* back pointer */
476b032f27cSSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
477e7495198SAdrian Chadd 	ifp->if_transmit = ieee80211_vap_transmit;
478e7495198SAdrian Chadd 	ifp->if_qflush = ieee80211_vap_qflush;
479b032f27cSSam Leffler 	ifp->if_ioctl = ieee80211_ioctl;
480b032f27cSSam Leffler 	ifp->if_init = ieee80211_init;
48128da1b56SGleb Smirnoff 	ifp->if_get_counter = ieee80211_get_counter;
482b032f27cSSam Leffler 
483b032f27cSSam Leffler 	vap->iv_ifp = ifp;
484b032f27cSSam Leffler 	vap->iv_ic = ic;
485b032f27cSSam Leffler 	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
486b032f27cSSam Leffler 	vap->iv_flags_ext = ic->ic_flags_ext;
487b032f27cSSam Leffler 	vap->iv_flags_ven = ic->ic_flags_ven;
488b032f27cSSam Leffler 	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
489b032f27cSSam Leffler 	vap->iv_htcaps = ic->ic_htcaps;
490e1d36f83SRui Paulo 	vap->iv_htextcaps = ic->ic_htextcaps;
491b032f27cSSam Leffler 	vap->iv_opmode = opmode;
492c43feedeSSam Leffler 	vap->iv_caps |= ieee80211_opcap[opmode];
493b032f27cSSam Leffler 	switch (opmode) {
494b032f27cSSam Leffler 	case IEEE80211_M_WDS:
495b032f27cSSam Leffler 		/*
496b032f27cSSam Leffler 		 * WDS links must specify the bssid of the far end.
497b032f27cSSam Leffler 		 * For legacy operation this is a static relationship.
498b032f27cSSam Leffler 		 * For non-legacy operation the station must associate
499b032f27cSSam Leffler 		 * and be authorized to pass traffic.  Plumbing the
500b032f27cSSam Leffler 		 * vap to the proper node happens when the vap
501b032f27cSSam Leffler 		 * transitions to RUN state.
502b032f27cSSam Leffler 		 */
503b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
504b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_DESBSSID;
505b032f27cSSam Leffler 		if (flags & IEEE80211_CLONE_WDSLEGACY)
506b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
507b032f27cSSam Leffler 		break;
50810ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
50910ad9a77SSam Leffler 	case IEEE80211_M_AHDEMO:
51010ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
51110ad9a77SSam Leffler 			/* NB: checked before clone operation allowed */
51210ad9a77SSam Leffler 			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
51310ad9a77SSam Leffler 			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
51410ad9a77SSam Leffler 			/*
51510ad9a77SSam Leffler 			 * Propagate TDMA capability to mark vap; this
51610ad9a77SSam Leffler 			 * cannot be removed and is used to distinguish
51710ad9a77SSam Leffler 			 * regular ahdemo operation from ahdemo+tdma.
51810ad9a77SSam Leffler 			 */
51910ad9a77SSam Leffler 			vap->iv_caps |= IEEE80211_C_TDMA;
52010ad9a77SSam Leffler 		}
52110ad9a77SSam Leffler 		break;
52210ad9a77SSam Leffler #endif
523fcd9500fSBernhard Schmidt 	default:
524fcd9500fSBernhard Schmidt 		break;
525b032f27cSSam Leffler 	}
526ae3f00bbSSam Leffler 	/* auto-enable s/w beacon miss support */
527ae3f00bbSSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS)
528ae3f00bbSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
52983fcb812SAndrew Thompson 	/* auto-generated or user supplied MAC address */
53083fcb812SAndrew Thompson 	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
53183fcb812SAndrew Thompson 		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
532b032f27cSSam Leffler 	/*
533b032f27cSSam Leffler 	 * Enable various functionality by default if we're
534b032f27cSSam Leffler 	 * capable; the driver can override us if it knows better.
535b032f27cSSam Leffler 	 */
536b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_WME)
537b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_WME;
538b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_BURST)
539b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BURST;
540b032f27cSSam Leffler 	/* NB: bg scanning only makes sense for station mode right now */
541b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA &&
542b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_BGSCAN))
543b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BGSCAN;
544c43feedeSSam Leffler 	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
54582fd2577SSam Leffler 	/* NB: DFS support only makes sense for ap mode right now */
54682fd2577SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
54782fd2577SSam Leffler 	    (vap->iv_caps & IEEE80211_C_DFS))
548b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
549b032f27cSSam Leffler 
550b032f27cSSam Leffler 	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
551b032f27cSSam Leffler 	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
552b032f27cSSam Leffler 	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
553b032f27cSSam Leffler 	/*
554b032f27cSSam Leffler 	 * Install a default reset method for the ioctl support;
555b032f27cSSam Leffler 	 * the driver can override this.
556b032f27cSSam Leffler 	 */
557b032f27cSSam Leffler 	vap->iv_reset = default_reset;
558b032f27cSSam Leffler 
559*ba2c1fbcSAdrian Chadd 	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
560*ba2c1fbcSAdrian Chadd 
561b032f27cSSam Leffler 	ieee80211_sysctl_vattach(vap);
562b032f27cSSam Leffler 	ieee80211_crypto_vattach(vap);
563b032f27cSSam Leffler 	ieee80211_node_vattach(vap);
564b032f27cSSam Leffler 	ieee80211_power_vattach(vap);
565b032f27cSSam Leffler 	ieee80211_proto_vattach(vap);
566616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
567616190d0SSam Leffler 	ieee80211_superg_vattach(vap);
568616190d0SSam Leffler #endif
569b032f27cSSam Leffler 	ieee80211_ht_vattach(vap);
570b032f27cSSam Leffler 	ieee80211_scan_vattach(vap);
571b032f27cSSam Leffler 	ieee80211_regdomain_vattach(vap);
5725463c4a4SSam Leffler 	ieee80211_radiotap_vattach(vap);
573a7c6aabdSBernhard Schmidt 	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
574b6108616SRui Paulo 
575b032f27cSSam Leffler 	return 0;
576b032f27cSSam Leffler }
577b032f27cSSam Leffler 
578b032f27cSSam Leffler /*
579b032f27cSSam Leffler  * Activate a vap.  State should have been prepared with a
580b032f27cSSam Leffler  * call to ieee80211_vap_setup and by the driver.  On return
581b032f27cSSam Leffler  * from this call the vap is ready for use.
582b032f27cSSam Leffler  */
583b032f27cSSam Leffler int
584*ba2c1fbcSAdrian Chadd ieee80211_vap_attach(struct ieee80211vap *vap,
585*ba2c1fbcSAdrian Chadd 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
586b032f27cSSam Leffler {
587b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
588b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
589b032f27cSSam Leffler 	struct ifmediareq imr;
590b032f27cSSam Leffler 	int maxrate;
591b032f27cSSam Leffler 
592b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
593b032f27cSSam Leffler 	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
594b032f27cSSam Leffler 	    __func__, ieee80211_opmode_name[vap->iv_opmode],
595c8f5794eSGleb Smirnoff 	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
596b032f27cSSam Leffler 
597b032f27cSSam Leffler 	/*
598b032f27cSSam Leffler 	 * Do late attach work that cannot happen until after
599b032f27cSSam Leffler 	 * the driver has had a chance to override defaults.
600b032f27cSSam Leffler 	 */
601b032f27cSSam Leffler 	ieee80211_node_latevattach(vap);
602b032f27cSSam Leffler 	ieee80211_power_latevattach(vap);
603b032f27cSSam Leffler 
604b032f27cSSam Leffler 	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
605b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
606b032f27cSSam Leffler 	ieee80211_media_status(ifp, &imr);
607b032f27cSSam Leffler 	/* NB: strip explicit mode; we're actually in autoselect */
608c3f10abdSSam Leffler 	ifmedia_set(&vap->iv_media,
609c3f10abdSSam Leffler 	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
610b032f27cSSam Leffler 	if (maxrate)
611b032f27cSSam Leffler 		ifp->if_baudrate = IF_Mbps(maxrate);
612b032f27cSSam Leffler 
613*ba2c1fbcSAdrian Chadd 	ether_ifattach(ifp, vap->iv_myaddr);
614b032f27cSSam Leffler 	/* hook output method setup by ether_ifattach */
615b032f27cSSam Leffler 	vap->iv_output = ifp->if_output;
616b032f27cSSam Leffler 	ifp->if_output = ieee80211_output;
617b032f27cSSam Leffler 	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
618b032f27cSSam Leffler 
619b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
620b032f27cSSam Leffler 	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
621b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
622616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
623b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
624616190d0SSam Leffler #endif
625b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
626b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
6272bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
6282bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
629*ba2c1fbcSAdrian Chadd 	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
630*ba2c1fbcSAdrian Chadd 	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
631b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
632b032f27cSSam Leffler 
633b032f27cSSam Leffler 	return 1;
634b032f27cSSam Leffler }
635b032f27cSSam Leffler 
636b032f27cSSam Leffler /*
637b032f27cSSam Leffler  * Tear down vap state and reclaim the ifnet.
638b032f27cSSam Leffler  * The driver is assumed to have prepared for
639b032f27cSSam Leffler  * this; e.g. by turning off interrupts for the
640b032f27cSSam Leffler  * underlying device.
641b032f27cSSam Leffler  */
642b032f27cSSam Leffler void
643b032f27cSSam Leffler ieee80211_vap_detach(struct ieee80211vap *vap)
644b032f27cSSam Leffler {
645b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
646b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
647b032f27cSSam Leffler 
64830e4856aSAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
64930e4856aSAdrian Chadd 
650b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
6517fc10b6bSGleb Smirnoff 	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
652b032f27cSSam Leffler 
6531da89db5SSam Leffler 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
6541da89db5SSam Leffler 	ether_ifdetach(ifp);
6551da89db5SSam Leffler 
6561da89db5SSam Leffler 	ieee80211_stop(vap);
657b032f27cSSam Leffler 
6585efea30fSAndrew Thompson 	/*
6595efea30fSAndrew Thompson 	 * Flush any deferred vap tasks.
6605efea30fSAndrew Thompson 	 */
6615efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_nstate_task);
6625efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
6635efea30fSAndrew Thompson 
664ab501dd6SSam Leffler 	/* XXX band-aid until ifnet handles this for us */
665ab501dd6SSam Leffler 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
666ab501dd6SSam Leffler 
6675efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
6685efea30fSAndrew Thompson 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
669b032f27cSSam Leffler 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
670b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
671616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
672b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
673616190d0SSam Leffler #endif
674b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
675b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
6762bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
6772bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
6785463c4a4SSam Leffler 	/* NB: this handles the bpfdetach done below */
6795463c4a4SSam Leffler 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
680*ba2c1fbcSAdrian Chadd 	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
681*ba2c1fbcSAdrian Chadd 	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
682b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
683b032f27cSSam Leffler 
684b032f27cSSam Leffler 	ifmedia_removeall(&vap->iv_media);
685b032f27cSSam Leffler 
6865463c4a4SSam Leffler 	ieee80211_radiotap_vdetach(vap);
687b032f27cSSam Leffler 	ieee80211_regdomain_vdetach(vap);
688b032f27cSSam Leffler 	ieee80211_scan_vdetach(vap);
689616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
690616190d0SSam Leffler 	ieee80211_superg_vdetach(vap);
691616190d0SSam Leffler #endif
692b032f27cSSam Leffler 	ieee80211_ht_vdetach(vap);
693b032f27cSSam Leffler 	/* NB: must be before ieee80211_node_vdetach */
694b032f27cSSam Leffler 	ieee80211_proto_vdetach(vap);
695b032f27cSSam Leffler 	ieee80211_crypto_vdetach(vap);
696b032f27cSSam Leffler 	ieee80211_power_vdetach(vap);
697b032f27cSSam Leffler 	ieee80211_node_vdetach(vap);
698b032f27cSSam Leffler 	ieee80211_sysctl_vdetach(vap);
699b20f0ed1SWeongyo Jeong 
700b20f0ed1SWeongyo Jeong 	if_free(ifp);
70130e4856aSAdrian Chadd 
70230e4856aSAdrian Chadd 	CURVNET_RESTORE();
703b032f27cSSam Leffler }
704b032f27cSSam Leffler 
705b032f27cSSam Leffler /*
706*ba2c1fbcSAdrian Chadd  * Synchronize flag bit state in the parent ifnet structure
707*ba2c1fbcSAdrian Chadd  * according to the state of all vap ifnet's.  This is used,
708*ba2c1fbcSAdrian Chadd  * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
709b032f27cSSam Leffler  */
710b032f27cSSam Leffler void
711*ba2c1fbcSAdrian Chadd ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
712b032f27cSSam Leffler {
713*ba2c1fbcSAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
714*ba2c1fbcSAdrian Chadd 	struct ieee80211vap *vap;
715*ba2c1fbcSAdrian Chadd 	int bit, oflags;
716b032f27cSSam Leffler 
717*ba2c1fbcSAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
718*ba2c1fbcSAdrian Chadd 
719*ba2c1fbcSAdrian Chadd 	bit = 0;
720*ba2c1fbcSAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
721*ba2c1fbcSAdrian Chadd 		if (vap->iv_ifp->if_flags & flag) {
722b032f27cSSam Leffler 			/*
723b032f27cSSam Leffler 			 * XXX the bridge sets PROMISC but we don't want to
724b032f27cSSam Leffler 			 * enable it on the device, discard here so all the
725b032f27cSSam Leffler 			 * drivers don't need to special-case it
726b032f27cSSam Leffler 			 */
727*ba2c1fbcSAdrian Chadd 			if (flag == IFF_PROMISC &&
728*ba2c1fbcSAdrian Chadd 			    !(vap->iv_opmode == IEEE80211_M_MONITOR ||
7292dfcbb0eSSam Leffler 			      (vap->iv_opmode == IEEE80211_M_AHDEMO &&
7302dfcbb0eSSam Leffler 			       (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
731*ba2c1fbcSAdrian Chadd 				continue;
732*ba2c1fbcSAdrian Chadd 			bit = 1;
733*ba2c1fbcSAdrian Chadd 			break;
73479d2c5e8SGleb Smirnoff 		}
735*ba2c1fbcSAdrian Chadd 	oflags = ifp->if_flags;
736*ba2c1fbcSAdrian Chadd 	if (bit)
737*ba2c1fbcSAdrian Chadd 		ifp->if_flags |= flag;
738*ba2c1fbcSAdrian Chadd 	else
739*ba2c1fbcSAdrian Chadd 		ifp->if_flags &= ~flag;
740*ba2c1fbcSAdrian Chadd 	if ((ifp->if_flags ^ oflags) & flag) {
741*ba2c1fbcSAdrian Chadd 		/* XXX should we return 1/0 and let caller do this? */
742*ba2c1fbcSAdrian Chadd 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
743*ba2c1fbcSAdrian Chadd 			if (flag == IFF_PROMISC)
744*ba2c1fbcSAdrian Chadd 				ieee80211_runtask(ic, &ic->ic_promisc_task);
745*ba2c1fbcSAdrian Chadd 			else if (flag == IFF_ALLMULTI)
7465efea30fSAndrew Thompson 				ieee80211_runtask(ic, &ic->ic_mcast_task);
747b032f27cSSam Leffler 		}
748*ba2c1fbcSAdrian Chadd 	}
749b032f27cSSam Leffler }
750b032f27cSSam Leffler 
751b032f27cSSam Leffler /*
752b032f27cSSam Leffler  * Synchronize flag bit state in the com structure
753b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
754b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
755b032f27cSSam Leffler  */
756b032f27cSSam Leffler static void
757b032f27cSSam Leffler ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
758b032f27cSSam Leffler {
759b032f27cSSam Leffler 	struct ieee80211vap *vap;
760b032f27cSSam Leffler 	int bit;
761b032f27cSSam Leffler 
762b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
763b032f27cSSam Leffler 
764b032f27cSSam Leffler 	bit = 0;
765b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
766b032f27cSSam Leffler 		if (vap->iv_flags & flag) {
767b032f27cSSam Leffler 			bit = 1;
768b032f27cSSam Leffler 			break;
769b032f27cSSam Leffler 		}
770b032f27cSSam Leffler 	if (bit)
771b032f27cSSam Leffler 		ic->ic_flags |= flag;
772b032f27cSSam Leffler 	else
773b032f27cSSam Leffler 		ic->ic_flags &= ~flag;
774b032f27cSSam Leffler }
775b032f27cSSam Leffler 
776b032f27cSSam Leffler void
777b032f27cSSam Leffler ieee80211_syncflag(struct ieee80211vap *vap, int flag)
778b032f27cSSam Leffler {
779b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
780b032f27cSSam Leffler 
781b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
782b032f27cSSam Leffler 	if (flag < 0) {
783b032f27cSSam Leffler 		flag = -flag;
784b032f27cSSam Leffler 		vap->iv_flags &= ~flag;
785b032f27cSSam Leffler 	} else
786b032f27cSSam Leffler 		vap->iv_flags |= flag;
787b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, flag);
788b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
789b032f27cSSam Leffler }
790b032f27cSSam Leffler 
791b032f27cSSam Leffler /*
7922bfc8a91SSam Leffler  * Synchronize flags_ht bit state in the com structure
7932bfc8a91SSam Leffler  * according to the state of all vap's.  This is used,
7942bfc8a91SSam Leffler  * for example, to handle state changes via ioctls.
7952bfc8a91SSam Leffler  */
7962bfc8a91SSam Leffler static void
7972bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
7982bfc8a91SSam Leffler {
7992bfc8a91SSam Leffler 	struct ieee80211vap *vap;
8002bfc8a91SSam Leffler 	int bit;
8012bfc8a91SSam Leffler 
8022bfc8a91SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
8032bfc8a91SSam Leffler 
8042bfc8a91SSam Leffler 	bit = 0;
8052bfc8a91SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8062bfc8a91SSam Leffler 		if (vap->iv_flags_ht & flag) {
8072bfc8a91SSam Leffler 			bit = 1;
8082bfc8a91SSam Leffler 			break;
8092bfc8a91SSam Leffler 		}
8102bfc8a91SSam Leffler 	if (bit)
8112bfc8a91SSam Leffler 		ic->ic_flags_ht |= flag;
8122bfc8a91SSam Leffler 	else
8132bfc8a91SSam Leffler 		ic->ic_flags_ht &= ~flag;
8142bfc8a91SSam Leffler }
8152bfc8a91SSam Leffler 
8162bfc8a91SSam Leffler void
8172bfc8a91SSam Leffler ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
8182bfc8a91SSam Leffler {
8192bfc8a91SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
8202bfc8a91SSam Leffler 
8212bfc8a91SSam Leffler 	IEEE80211_LOCK(ic);
8222bfc8a91SSam Leffler 	if (flag < 0) {
8232bfc8a91SSam Leffler 		flag = -flag;
8242bfc8a91SSam Leffler 		vap->iv_flags_ht &= ~flag;
8252bfc8a91SSam Leffler 	} else
8262bfc8a91SSam Leffler 		vap->iv_flags_ht |= flag;
8272bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, flag);
8282bfc8a91SSam Leffler 	IEEE80211_UNLOCK(ic);
8292bfc8a91SSam Leffler }
8302bfc8a91SSam Leffler 
8312bfc8a91SSam Leffler /*
8322bfc8a91SSam Leffler  * Synchronize flags_ext bit state in the com structure
833b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
834b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
835b032f27cSSam Leffler  */
836b032f27cSSam Leffler static void
837b032f27cSSam Leffler ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
838b032f27cSSam Leffler {
839b032f27cSSam Leffler 	struct ieee80211vap *vap;
840b032f27cSSam Leffler 	int bit;
841b032f27cSSam Leffler 
842b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
843b032f27cSSam Leffler 
844b032f27cSSam Leffler 	bit = 0;
845b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
846b032f27cSSam Leffler 		if (vap->iv_flags_ext & flag) {
847b032f27cSSam Leffler 			bit = 1;
848b032f27cSSam Leffler 			break;
849b032f27cSSam Leffler 		}
850b032f27cSSam Leffler 	if (bit)
851b032f27cSSam Leffler 		ic->ic_flags_ext |= flag;
852b032f27cSSam Leffler 	else
853b032f27cSSam Leffler 		ic->ic_flags_ext &= ~flag;
854b032f27cSSam Leffler }
855b032f27cSSam Leffler 
856b032f27cSSam Leffler void
857b032f27cSSam Leffler ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
858b032f27cSSam Leffler {
859b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
860b032f27cSSam Leffler 
861b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
862b032f27cSSam Leffler 	if (flag < 0) {
863b032f27cSSam Leffler 		flag = -flag;
864b032f27cSSam Leffler 		vap->iv_flags_ext &= ~flag;
865b032f27cSSam Leffler 	} else
866b032f27cSSam Leffler 		vap->iv_flags_ext |= flag;
867b032f27cSSam Leffler 	ieee80211_syncflag_ext_locked(ic, flag);
868b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
8691a1e1d21SSam Leffler }
8701a1e1d21SSam Leffler 
871ca4ac7aeSSam Leffler static __inline int
872ca4ac7aeSSam Leffler mapgsm(u_int freq, u_int flags)
873ca4ac7aeSSam Leffler {
874ca4ac7aeSSam Leffler 	freq *= 10;
875ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_QUARTER)
876ca4ac7aeSSam Leffler 		freq += 5;
877ca4ac7aeSSam Leffler 	else if (flags & IEEE80211_CHAN_HALF)
878ca4ac7aeSSam Leffler 		freq += 10;
879ca4ac7aeSSam Leffler 	else
880ca4ac7aeSSam Leffler 		freq += 20;
881ca4ac7aeSSam Leffler 	/* NB: there is no 907/20 wide but leave room */
882ca4ac7aeSSam Leffler 	return (freq - 906*10) / 5;
883ca4ac7aeSSam Leffler }
884ca4ac7aeSSam Leffler 
885ca4ac7aeSSam Leffler static __inline int
886ca4ac7aeSSam Leffler mappsb(u_int freq, u_int flags)
887ca4ac7aeSSam Leffler {
888ca4ac7aeSSam Leffler 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
889ca4ac7aeSSam Leffler }
890ca4ac7aeSSam Leffler 
8911a1e1d21SSam Leffler /*
8921a1e1d21SSam Leffler  * Convert MHz frequency to IEEE channel number.
8931a1e1d21SSam Leffler  */
8946f322b78SSam Leffler int
8951a1e1d21SSam Leffler ieee80211_mhz2ieee(u_int freq, u_int flags)
8961a1e1d21SSam Leffler {
89711df4239SSam Leffler #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
898ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
899ca4ac7aeSSam Leffler 		return mapgsm(freq, flags);
9001a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
9011a1e1d21SSam Leffler 		if (freq == 2484)
9021a1e1d21SSam Leffler 			return 14;
9031a1e1d21SSam Leffler 		if (freq < 2484)
9046f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
9051a1e1d21SSam Leffler 		else
9061a1e1d21SSam Leffler 			return 15 + ((freq - 2512) / 20);
907c032abb5SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
90841b3c790SSam Leffler 		if (freq <= 5000) {
90968e8e04eSSam Leffler 			/* XXX check regdomain? */
91011df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
911ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
9126f322b78SSam Leffler 			return (freq - 4000) / 5;
91341b3c790SSam Leffler 		} else
9141a1e1d21SSam Leffler 			return (freq - 5000) / 5;
9151a1e1d21SSam Leffler 	} else {				/* either, guess */
9161a1e1d21SSam Leffler 		if (freq == 2484)
9171a1e1d21SSam Leffler 			return 14;
918ca4ac7aeSSam Leffler 		if (freq < 2484) {
919ca4ac7aeSSam Leffler 			if (907 <= freq && freq <= 922)
920ca4ac7aeSSam Leffler 				return mapgsm(freq, flags);
9216f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
922ca4ac7aeSSam Leffler 		}
9236f322b78SSam Leffler 		if (freq < 5000) {
92411df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
925ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
92641b3c790SSam Leffler 			else if (freq > 4900)
9276f322b78SSam Leffler 				return (freq - 4000) / 5;
9286f322b78SSam Leffler 			else
9291a1e1d21SSam Leffler 				return 15 + ((freq - 2512) / 20);
9306f322b78SSam Leffler 		}
9311a1e1d21SSam Leffler 		return (freq - 5000) / 5;
9321a1e1d21SSam Leffler 	}
93311df4239SSam Leffler #undef IS_FREQ_IN_PSB
9341a1e1d21SSam Leffler }
9351a1e1d21SSam Leffler 
9361a1e1d21SSam Leffler /*
9371a1e1d21SSam Leffler  * Convert channel to IEEE channel number.
9381a1e1d21SSam Leffler  */
9396f322b78SSam Leffler int
94038da1496SMatt Jacob ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
9411a1e1d21SSam Leffler {
94268e8e04eSSam Leffler 	if (c == NULL) {
943c8f5794eSGleb Smirnoff 		ic_printf(ic, "invalid channel (NULL)\n");
9448be0d570SSam Leffler 		return 0;		/* XXX */
9451a1e1d21SSam Leffler 	}
94668e8e04eSSam Leffler 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
9471a1e1d21SSam Leffler }
9481a1e1d21SSam Leffler 
9491a1e1d21SSam Leffler /*
9501a1e1d21SSam Leffler  * Convert IEEE channel number to MHz frequency.
9511a1e1d21SSam Leffler  */
9521a1e1d21SSam Leffler u_int
9531a1e1d21SSam Leffler ieee80211_ieee2mhz(u_int chan, u_int flags)
9541a1e1d21SSam Leffler {
955ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
956ca4ac7aeSSam Leffler 		return 907 + 5 * (chan / 10);
9571a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
9581a1e1d21SSam Leffler 		if (chan == 14)
9591a1e1d21SSam Leffler 			return 2484;
9601a1e1d21SSam Leffler 		if (chan < 14)
9611a1e1d21SSam Leffler 			return 2407 + chan*5;
9621a1e1d21SSam Leffler 		else
9631a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
9641a1e1d21SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
96541b3c790SSam Leffler 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
96641b3c790SSam Leffler 			chan -= 37;
96741b3c790SSam Leffler 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
96841b3c790SSam Leffler 		}
9691a1e1d21SSam Leffler 		return 5000 + (chan*5);
9701a1e1d21SSam Leffler 	} else {				/* either, guess */
971ca4ac7aeSSam Leffler 		/* XXX can't distinguish PSB+GSM channels */
9721a1e1d21SSam Leffler 		if (chan == 14)
9731a1e1d21SSam Leffler 			return 2484;
9741a1e1d21SSam Leffler 		if (chan < 14)			/* 0-13 */
9751a1e1d21SSam Leffler 			return 2407 + chan*5;
9761a1e1d21SSam Leffler 		if (chan < 27)			/* 15-26 */
9771a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
9781a1e1d21SSam Leffler 		return 5000 + (chan*5);
9791a1e1d21SSam Leffler 	}
9801a1e1d21SSam Leffler }
9811a1e1d21SSam Leffler 
9821a1e1d21SSam Leffler /*
98368e8e04eSSam Leffler  * Locate a channel given a frequency+flags.  We cache
984b032f27cSSam Leffler  * the previous lookup to optimize switching between two
98568e8e04eSSam Leffler  * channels--as happens with dynamic turbo.
98668e8e04eSSam Leffler  */
98768e8e04eSSam Leffler struct ieee80211_channel *
98868e8e04eSSam Leffler ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
98968e8e04eSSam Leffler {
99068e8e04eSSam Leffler 	struct ieee80211_channel *c;
99168e8e04eSSam Leffler 	int i;
99268e8e04eSSam Leffler 
99368e8e04eSSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
99468e8e04eSSam Leffler 	c = ic->ic_prevchan;
99568e8e04eSSam Leffler 	if (c != NULL && c->ic_freq == freq &&
99668e8e04eSSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
99768e8e04eSSam Leffler 		return c;
99868e8e04eSSam Leffler 	/* brute force search */
99968e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
100068e8e04eSSam Leffler 		c = &ic->ic_channels[i];
100168e8e04eSSam Leffler 		if (c->ic_freq == freq &&
100268e8e04eSSam Leffler 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
100368e8e04eSSam Leffler 			return c;
100468e8e04eSSam Leffler 	}
100568e8e04eSSam Leffler 	return NULL;
100668e8e04eSSam Leffler }
100768e8e04eSSam Leffler 
1008a557c018SSam Leffler /*
1009a557c018SSam Leffler  * Locate a channel given a channel number+flags.  We cache
1010a557c018SSam Leffler  * the previous lookup to optimize switching between two
1011a557c018SSam Leffler  * channels--as happens with dynamic turbo.
1012a557c018SSam Leffler  */
1013a557c018SSam Leffler struct ieee80211_channel *
1014a557c018SSam Leffler ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1015a557c018SSam Leffler {
1016a557c018SSam Leffler 	struct ieee80211_channel *c;
1017a557c018SSam Leffler 	int i;
1018a557c018SSam Leffler 
1019a557c018SSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
1020a557c018SSam Leffler 	c = ic->ic_prevchan;
1021a557c018SSam Leffler 	if (c != NULL && c->ic_ieee == ieee &&
1022a557c018SSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1023a557c018SSam Leffler 		return c;
1024a557c018SSam Leffler 	/* brute force search */
1025a557c018SSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1026a557c018SSam Leffler 		c = &ic->ic_channels[i];
1027a557c018SSam Leffler 		if (c->ic_ieee == ieee &&
1028a557c018SSam Leffler 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1029a557c018SSam Leffler 			return c;
1030a557c018SSam Leffler 	}
1031a557c018SSam Leffler 	return NULL;
1032a557c018SSam Leffler }
1033a557c018SSam Leffler 
1034c79f192cSAdrian Chadd /*
1035c79f192cSAdrian Chadd  * Lookup a channel suitable for the given rx status.
1036c79f192cSAdrian Chadd  *
1037c79f192cSAdrian Chadd  * This is used to find a channel for a frame (eg beacon, probe
1038c79f192cSAdrian Chadd  * response) based purely on the received PHY information.
1039c79f192cSAdrian Chadd  *
1040c79f192cSAdrian Chadd  * For now it tries to do it based on R_FREQ / R_IEEE.
1041c79f192cSAdrian Chadd  * This is enough for 11bg and 11a (and thus 11ng/11na)
1042c79f192cSAdrian Chadd  * but it will not be enough for GSM, PSB channels and the
1043c79f192cSAdrian Chadd  * like.  It also doesn't know about legacy-turbog and
1044c79f192cSAdrian Chadd  * legacy-turbo modes, which some offload NICs actually
1045c79f192cSAdrian Chadd  * support in weird ways.
1046c79f192cSAdrian Chadd  *
1047c79f192cSAdrian Chadd  * Takes the ic and rxstatus; returns the channel or NULL
1048c79f192cSAdrian Chadd  * if not found.
1049c79f192cSAdrian Chadd  *
1050c79f192cSAdrian Chadd  * XXX TODO: Add support for that when the need arises.
1051c79f192cSAdrian Chadd  */
1052c79f192cSAdrian Chadd struct ieee80211_channel *
1053c79f192cSAdrian Chadd ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1054c79f192cSAdrian Chadd     const struct ieee80211_rx_stats *rxs)
1055c79f192cSAdrian Chadd {
1056c79f192cSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1057c79f192cSAdrian Chadd 	uint32_t flags;
1058c79f192cSAdrian Chadd 	struct ieee80211_channel *c;
1059c79f192cSAdrian Chadd 
1060c79f192cSAdrian Chadd 	if (rxs == NULL)
1061c79f192cSAdrian Chadd 		return (NULL);
1062c79f192cSAdrian Chadd 
1063c79f192cSAdrian Chadd 	/*
1064c79f192cSAdrian Chadd 	 * Strictly speaking we only use freq for now,
1065c79f192cSAdrian Chadd 	 * however later on we may wish to just store
1066c79f192cSAdrian Chadd 	 * the ieee for verification.
1067c79f192cSAdrian Chadd 	 */
1068c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1069c79f192cSAdrian Chadd 		return (NULL);
1070c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1071c79f192cSAdrian Chadd 		return (NULL);
1072c79f192cSAdrian Chadd 
1073c79f192cSAdrian Chadd 	/*
1074c79f192cSAdrian Chadd 	 * If the rx status contains a valid ieee/freq, then
1075c79f192cSAdrian Chadd 	 * ensure we populate the correct channel information
1076c79f192cSAdrian Chadd 	 * in rxchan before passing it up to the scan infrastructure.
1077c79f192cSAdrian Chadd 	 * Offload NICs will pass up beacons from all channels
1078c79f192cSAdrian Chadd 	 * during background scans.
1079c79f192cSAdrian Chadd 	 */
1080c79f192cSAdrian Chadd 
1081c79f192cSAdrian Chadd 	/* Determine a band */
1082c79f192cSAdrian Chadd 	/* XXX should be done by the driver? */
1083c79f192cSAdrian Chadd 	if (rxs->c_freq < 3000) {
10842108f2a8SAdrian Chadd 		flags = IEEE80211_CHAN_G;
1085c79f192cSAdrian Chadd 	} else {
1086c79f192cSAdrian Chadd 		flags = IEEE80211_CHAN_A;
1087c79f192cSAdrian Chadd 	}
1088c79f192cSAdrian Chadd 
1089c79f192cSAdrian Chadd 	/* Channel lookup */
1090c79f192cSAdrian Chadd 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1091c79f192cSAdrian Chadd 
1092c79f192cSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1093c79f192cSAdrian Chadd 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1094c79f192cSAdrian Chadd 	    __func__,
1095c79f192cSAdrian Chadd 	    (int) rxs->c_freq,
1096c79f192cSAdrian Chadd 	    (int) rxs->c_ieee,
1097c79f192cSAdrian Chadd 	    flags,
1098c79f192cSAdrian Chadd 	    c);
1099c79f192cSAdrian Chadd 
1100c79f192cSAdrian Chadd 	return (c);
1101c79f192cSAdrian Chadd }
1102c79f192cSAdrian Chadd 
110368e8e04eSSam Leffler static void
1104b032f27cSSam Leffler addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
110568e8e04eSSam Leffler {
110668e8e04eSSam Leffler #define	ADD(_ic, _s, _o) \
1107b032f27cSSam Leffler 	ifmedia_add(media, \
110868e8e04eSSam Leffler 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
110968e8e04eSSam Leffler 	static const u_int mopts[IEEE80211_MODE_MAX] = {
1110c3f10abdSSam Leffler 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1111c3f10abdSSam Leffler 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1112c3f10abdSSam Leffler 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1113c3f10abdSSam Leffler 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1114c3f10abdSSam Leffler 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1115c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1116c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1117c3f10abdSSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
11186a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
11196a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1120c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1121c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
112268e8e04eSSam Leffler 	};
112368e8e04eSSam Leffler 	u_int mopt;
112468e8e04eSSam Leffler 
112568e8e04eSSam Leffler 	mopt = mopts[mode];
1126b032f27cSSam Leffler 	if (addsta)
1127b032f27cSSam Leffler 		ADD(ic, mword, mopt);	/* STA mode has no cap */
1128b032f27cSSam Leffler 	if (caps & IEEE80211_C_IBSS)
1129b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1130b032f27cSSam Leffler 	if (caps & IEEE80211_C_HOSTAP)
1131b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1132b032f27cSSam Leffler 	if (caps & IEEE80211_C_AHDEMO)
1133b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1134b032f27cSSam Leffler 	if (caps & IEEE80211_C_MONITOR)
1135b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1136b032f27cSSam Leffler 	if (caps & IEEE80211_C_WDS)
1137b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
113859aa14a9SRui Paulo 	if (caps & IEEE80211_C_MBSS)
113959aa14a9SRui Paulo 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
114068e8e04eSSam Leffler #undef ADD
114168e8e04eSSam Leffler }
114268e8e04eSSam Leffler 
114368e8e04eSSam Leffler /*
11441a1e1d21SSam Leffler  * Setup the media data structures according to the channel and
1145b032f27cSSam Leffler  * rate tables.
11461a1e1d21SSam Leffler  */
1147b032f27cSSam Leffler static int
1148b032f27cSSam Leffler ieee80211_media_setup(struct ieee80211com *ic,
1149b032f27cSSam Leffler 	struct ifmedia *media, int caps, int addsta,
11501a1e1d21SSam Leffler 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
11511a1e1d21SSam Leffler {
1152fcd9500fSBernhard Schmidt 	int i, j, rate, maxrate, mword, r;
1153fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
115468e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
11551a1e1d21SSam Leffler 	struct ieee80211_rateset allrates;
11561a1e1d21SSam Leffler 
11572692bb26SSam Leffler 	/*
11581a1e1d21SSam Leffler 	 * Fill in media characteristics.
11591a1e1d21SSam Leffler 	 */
1160b032f27cSSam Leffler 	ifmedia_init(media, 0, media_change, media_stat);
11611a1e1d21SSam Leffler 	maxrate = 0;
116268e8e04eSSam Leffler 	/*
116368e8e04eSSam Leffler 	 * Add media for legacy operating modes.
116468e8e04eSSam Leffler 	 */
11651a1e1d21SSam Leffler 	memset(&allrates, 0, sizeof(allrates));
116668e8e04eSSam Leffler 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
11676dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
11681a1e1d21SSam Leffler 			continue;
1169b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
11701a1e1d21SSam Leffler 		if (mode == IEEE80211_MODE_AUTO)
11711a1e1d21SSam Leffler 			continue;
11721a1e1d21SSam Leffler 		rs = &ic->ic_sup_rates[mode];
11731a1e1d21SSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
11741a1e1d21SSam Leffler 			rate = rs->rs_rates[i];
11751a1e1d21SSam Leffler 			mword = ieee80211_rate2media(ic, rate, mode);
11761a1e1d21SSam Leffler 			if (mword == 0)
11771a1e1d21SSam Leffler 				continue;
1178b032f27cSSam Leffler 			addmedia(media, caps, addsta, mode, mword);
11791a1e1d21SSam Leffler 			/*
118068e8e04eSSam Leffler 			 * Add legacy rate to the collection of all rates.
11811a1e1d21SSam Leffler 			 */
11821a1e1d21SSam Leffler 			r = rate & IEEE80211_RATE_VAL;
11831a1e1d21SSam Leffler 			for (j = 0; j < allrates.rs_nrates; j++)
11841a1e1d21SSam Leffler 				if (allrates.rs_rates[j] == r)
11851a1e1d21SSam Leffler 					break;
11861a1e1d21SSam Leffler 			if (j == allrates.rs_nrates) {
11871a1e1d21SSam Leffler 				/* unique, add to the set */
11881a1e1d21SSam Leffler 				allrates.rs_rates[j] = r;
11891a1e1d21SSam Leffler 				allrates.rs_nrates++;
11901a1e1d21SSam Leffler 			}
11911a1e1d21SSam Leffler 			rate = (rate & IEEE80211_RATE_VAL) / 2;
11921a1e1d21SSam Leffler 			if (rate > maxrate)
11931a1e1d21SSam Leffler 				maxrate = rate;
11941a1e1d21SSam Leffler 		}
11951a1e1d21SSam Leffler 	}
11961a1e1d21SSam Leffler 	for (i = 0; i < allrates.rs_nrates; i++) {
11971a1e1d21SSam Leffler 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
11981a1e1d21SSam Leffler 				IEEE80211_MODE_AUTO);
11991a1e1d21SSam Leffler 		if (mword == 0)
12001a1e1d21SSam Leffler 			continue;
120168e8e04eSSam Leffler 		/* NB: remove media options from mword */
1202b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1203b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
12041a1e1d21SSam Leffler 	}
120568e8e04eSSam Leffler 	/*
120668e8e04eSSam Leffler 	 * Add HT/11n media.  Note that we do not have enough
120768e8e04eSSam Leffler 	 * bits in the media subtype to express the MCS so we
120868e8e04eSSam Leffler 	 * use a "placeholder" media subtype and any fixed MCS
120968e8e04eSSam Leffler 	 * must be specified with a different mechanism.
121068e8e04eSSam Leffler 	 */
12116a76ae21SSam Leffler 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
121268e8e04eSSam Leffler 		if (isclr(ic->ic_modecaps, mode))
121368e8e04eSSam Leffler 			continue;
1214b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1215b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
121668e8e04eSSam Leffler 	}
121768e8e04eSSam Leffler 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
121868e8e04eSSam Leffler 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1219b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1220b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
12216f897ba9SBernhard Schmidt 		i = ic->ic_txstream * 8 - 1;
12226f897ba9SBernhard Schmidt 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
12236f897ba9SBernhard Schmidt 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
12246f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_400ns;
12256f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
12266f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_800ns;
12276f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
12286f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_400ns;
12296f897ba9SBernhard Schmidt 		else
12306f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_800ns;
12316f897ba9SBernhard Schmidt 		if (rate > maxrate)
12326f897ba9SBernhard Schmidt 			maxrate = rate;
1233b032f27cSSam Leffler 	}
1234b032f27cSSam Leffler 	return maxrate;
123568e8e04eSSam Leffler }
123668e8e04eSSam Leffler 
1237*ba2c1fbcSAdrian Chadd void
1238*ba2c1fbcSAdrian Chadd ieee80211_media_init(struct ieee80211com *ic)
1239*ba2c1fbcSAdrian Chadd {
1240*ba2c1fbcSAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
1241*ba2c1fbcSAdrian Chadd 	int maxrate;
1242*ba2c1fbcSAdrian Chadd 
1243*ba2c1fbcSAdrian Chadd 	/* NB: this works because the structure is initialized to zero */
1244*ba2c1fbcSAdrian Chadd 	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1245*ba2c1fbcSAdrian Chadd 		/*
1246*ba2c1fbcSAdrian Chadd 		 * We are re-initializing the channel list; clear
1247*ba2c1fbcSAdrian Chadd 		 * the existing media state as the media routines
1248*ba2c1fbcSAdrian Chadd 		 * don't suppress duplicates.
1249*ba2c1fbcSAdrian Chadd 		 */
1250*ba2c1fbcSAdrian Chadd 		ifmedia_removeall(&ic->ic_media);
1251*ba2c1fbcSAdrian Chadd 	}
1252*ba2c1fbcSAdrian Chadd 	ieee80211_chan_init(ic);
1253*ba2c1fbcSAdrian Chadd 
1254*ba2c1fbcSAdrian Chadd 	/*
1255*ba2c1fbcSAdrian Chadd 	 * Recalculate media settings in case new channel list changes
1256*ba2c1fbcSAdrian Chadd 	 * the set of available modes.
1257*ba2c1fbcSAdrian Chadd 	 */
1258*ba2c1fbcSAdrian Chadd 	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1259*ba2c1fbcSAdrian Chadd 		ieee80211com_media_change, ieee80211com_media_status);
1260*ba2c1fbcSAdrian Chadd 	/* NB: strip explicit mode; we're actually in autoselect */
1261*ba2c1fbcSAdrian Chadd 	ifmedia_set(&ic->ic_media,
1262*ba2c1fbcSAdrian Chadd 	    media_status(ic->ic_opmode, ic->ic_curchan) &~
1263*ba2c1fbcSAdrian Chadd 		(IFM_MMASK | IFM_IEEE80211_TURBO));
1264*ba2c1fbcSAdrian Chadd 	if (maxrate)
1265*ba2c1fbcSAdrian Chadd 		ifp->if_baudrate = IF_Mbps(maxrate);
1266*ba2c1fbcSAdrian Chadd 
1267*ba2c1fbcSAdrian Chadd 	/* XXX need to propagate new media settings to vap's */
1268*ba2c1fbcSAdrian Chadd }
1269*ba2c1fbcSAdrian Chadd 
12706a76ae21SSam Leffler /* XXX inline or eliminate? */
127141b3c790SSam Leffler const struct ieee80211_rateset *
127241b3c790SSam Leffler ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
127341b3c790SSam Leffler {
127440432d36SSam Leffler 	/* XXX does this work for 11ng basic rates? */
127568e8e04eSSam Leffler 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
127641b3c790SSam Leffler }
127741b3c790SSam Leffler 
12788a1b9b6aSSam Leffler void
12798a1b9b6aSSam Leffler ieee80211_announce(struct ieee80211com *ic)
12808a1b9b6aSSam Leffler {
1281fcd9500fSBernhard Schmidt 	int i, rate, mword;
1282fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
128368e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
12848a1b9b6aSSam Leffler 
12857edb9e0aSSam Leffler 	/* NB: skip AUTO since it has no rates */
12867edb9e0aSSam Leffler 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
12876dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
12888a1b9b6aSSam Leffler 			continue;
1289c8f5794eSGleb Smirnoff 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
12908a1b9b6aSSam Leffler 		rs = &ic->ic_sup_rates[mode];
12918a1b9b6aSSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
129268e8e04eSSam Leffler 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
12938a1b9b6aSSam Leffler 			if (mword == 0)
12948a1b9b6aSSam Leffler 				continue;
129568e8e04eSSam Leffler 			rate = ieee80211_media2rate(mword);
12968a1b9b6aSSam Leffler 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
129768e8e04eSSam Leffler 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
12988a1b9b6aSSam Leffler 		}
12998a1b9b6aSSam Leffler 		printf("\n");
13008a1b9b6aSSam Leffler 	}
130168e8e04eSSam Leffler 	ieee80211_ht_announce(ic);
13028a1b9b6aSSam Leffler }
13038a1b9b6aSSam Leffler 
130468e8e04eSSam Leffler void
130568e8e04eSSam Leffler ieee80211_announce_channels(struct ieee80211com *ic)
13061a1e1d21SSam Leffler {
130768e8e04eSSam Leffler 	const struct ieee80211_channel *c;
130868e8e04eSSam Leffler 	char type;
130968e8e04eSSam Leffler 	int i, cw;
131068e8e04eSSam Leffler 
131168e8e04eSSam Leffler 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
131268e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
131368e8e04eSSam Leffler 		c = &ic->ic_channels[i];
131468e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
131568e8e04eSSam Leffler 			type = 'S';
131668e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108A(c))
131768e8e04eSSam Leffler 			type = 'T';
131868e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108G(c))
131968e8e04eSSam Leffler 			type = 'G';
132068e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HT(c))
132168e8e04eSSam Leffler 			type = 'n';
132268e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_A(c))
132368e8e04eSSam Leffler 			type = 'a';
132468e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_ANYG(c))
132568e8e04eSSam Leffler 			type = 'g';
132668e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_B(c))
132768e8e04eSSam Leffler 			type = 'b';
132868e8e04eSSam Leffler 		else
132968e8e04eSSam Leffler 			type = 'f';
133068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
133168e8e04eSSam Leffler 			cw = 40;
133268e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(c))
133368e8e04eSSam Leffler 			cw = 10;
133468e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(c))
133568e8e04eSSam Leffler 			cw = 5;
133668e8e04eSSam Leffler 		else
133768e8e04eSSam Leffler 			cw = 20;
133868e8e04eSSam Leffler 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
133968e8e04eSSam Leffler 			, c->ic_ieee, c->ic_freq, type
134068e8e04eSSam Leffler 			, cw
134168e8e04eSSam Leffler 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
134268e8e04eSSam Leffler 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
134368e8e04eSSam Leffler 			, c->ic_maxregpower
134468e8e04eSSam Leffler 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
134568e8e04eSSam Leffler 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
134668e8e04eSSam Leffler 		);
134768e8e04eSSam Leffler 	}
13481a1e1d21SSam Leffler }
13491a1e1d21SSam Leffler 
135068e8e04eSSam Leffler static int
1351f945bd7aSSam Leffler media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
135268e8e04eSSam Leffler {
13531a1e1d21SSam Leffler 	switch (IFM_MODE(ime->ifm_media)) {
13541a1e1d21SSam Leffler 	case IFM_IEEE80211_11A:
1355b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11A;
13561a1e1d21SSam Leffler 		break;
13571a1e1d21SSam Leffler 	case IFM_IEEE80211_11B:
1358b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11B;
13591a1e1d21SSam Leffler 		break;
13601a1e1d21SSam Leffler 	case IFM_IEEE80211_11G:
1361b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11G;
13621a1e1d21SSam Leffler 		break;
13634844aa7dSAtsushi Onoe 	case IFM_IEEE80211_FH:
1364b032f27cSSam Leffler 		*mode = IEEE80211_MODE_FH;
13654844aa7dSAtsushi Onoe 		break;
136668e8e04eSSam Leffler 	case IFM_IEEE80211_11NA:
1367b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NA;
136868e8e04eSSam Leffler 		break;
136968e8e04eSSam Leffler 	case IFM_IEEE80211_11NG:
1370b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NG;
137168e8e04eSSam Leffler 		break;
13721a1e1d21SSam Leffler 	case IFM_AUTO:
1373b032f27cSSam Leffler 		*mode = IEEE80211_MODE_AUTO;
13741a1e1d21SSam Leffler 		break;
13751a1e1d21SSam Leffler 	default:
1376b032f27cSSam Leffler 		return 0;
13771a1e1d21SSam Leffler 	}
13781a1e1d21SSam Leffler 	/*
13798a1b9b6aSSam Leffler 	 * Turbo mode is an ``option''.
13808a1b9b6aSSam Leffler 	 * XXX does not apply to AUTO
13811a1e1d21SSam Leffler 	 */
13821a1e1d21SSam Leffler 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1383b032f27cSSam Leffler 		if (*mode == IEEE80211_MODE_11A) {
1384f945bd7aSSam Leffler 			if (flags & IEEE80211_F_TURBOP)
1385b032f27cSSam Leffler 				*mode = IEEE80211_MODE_TURBO_A;
138668e8e04eSSam Leffler 			else
1387b032f27cSSam Leffler 				*mode = IEEE80211_MODE_STURBO_A;
1388b032f27cSSam Leffler 		} else if (*mode == IEEE80211_MODE_11G)
1389b032f27cSSam Leffler 			*mode = IEEE80211_MODE_TURBO_G;
13908a1b9b6aSSam Leffler 		else
1391b032f27cSSam Leffler 			return 0;
13921a1e1d21SSam Leffler 	}
139368e8e04eSSam Leffler 	/* XXX HT40 +/- */
1394b032f27cSSam Leffler 	return 1;
1395b032f27cSSam Leffler }
13961a1e1d21SSam Leffler 
13971a1e1d21SSam Leffler /*
1398*ba2c1fbcSAdrian Chadd  * Handle a media change request on the underlying interface.
1399*ba2c1fbcSAdrian Chadd  */
1400*ba2c1fbcSAdrian Chadd int
1401*ba2c1fbcSAdrian Chadd ieee80211com_media_change(struct ifnet *ifp)
1402*ba2c1fbcSAdrian Chadd {
1403*ba2c1fbcSAdrian Chadd 	return EINVAL;
1404*ba2c1fbcSAdrian Chadd }
1405*ba2c1fbcSAdrian Chadd 
1406*ba2c1fbcSAdrian Chadd /*
1407b032f27cSSam Leffler  * Handle a media change request on the vap interface.
1408b032f27cSSam Leffler  */
1409b032f27cSSam Leffler int
1410b032f27cSSam Leffler ieee80211_media_change(struct ifnet *ifp)
1411b032f27cSSam Leffler {
1412b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
1413b032f27cSSam Leffler 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1414f945bd7aSSam Leffler 	uint16_t newmode;
1415b032f27cSSam Leffler 
1416f945bd7aSSam Leffler 	if (!media2mode(ime, vap->iv_flags, &newmode))
1417b032f27cSSam Leffler 		return EINVAL;
1418f945bd7aSSam Leffler 	if (vap->iv_des_mode != newmode) {
1419f945bd7aSSam Leffler 		vap->iv_des_mode = newmode;
14200a310468SSam Leffler 		/* XXX kick state machine if up+running */
1421b032f27cSSam Leffler 	}
1422b032f27cSSam Leffler 	return 0;
1423b032f27cSSam Leffler }
1424b032f27cSSam Leffler 
142568e8e04eSSam Leffler /*
142668e8e04eSSam Leffler  * Common code to calculate the media status word
142768e8e04eSSam Leffler  * from the operating mode and channel state.
142868e8e04eSSam Leffler  */
142968e8e04eSSam Leffler static int
143068e8e04eSSam Leffler media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
143168e8e04eSSam Leffler {
143268e8e04eSSam Leffler 	int status;
143368e8e04eSSam Leffler 
143468e8e04eSSam Leffler 	status = IFM_IEEE80211;
143568e8e04eSSam Leffler 	switch (opmode) {
143668e8e04eSSam Leffler 	case IEEE80211_M_STA:
143768e8e04eSSam Leffler 		break;
143868e8e04eSSam Leffler 	case IEEE80211_M_IBSS:
143968e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC;
144068e8e04eSSam Leffler 		break;
144168e8e04eSSam Leffler 	case IEEE80211_M_HOSTAP:
144268e8e04eSSam Leffler 		status |= IFM_IEEE80211_HOSTAP;
144368e8e04eSSam Leffler 		break;
144468e8e04eSSam Leffler 	case IEEE80211_M_MONITOR:
144568e8e04eSSam Leffler 		status |= IFM_IEEE80211_MONITOR;
144668e8e04eSSam Leffler 		break;
144768e8e04eSSam Leffler 	case IEEE80211_M_AHDEMO:
144868e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
144968e8e04eSSam Leffler 		break;
145068e8e04eSSam Leffler 	case IEEE80211_M_WDS:
1451b032f27cSSam Leffler 		status |= IFM_IEEE80211_WDS;
145268e8e04eSSam Leffler 		break;
145359aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
145459aa14a9SRui Paulo 		status |= IFM_IEEE80211_MBSS;
145559aa14a9SRui Paulo 		break;
145668e8e04eSSam Leffler 	}
145768e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HTA(chan)) {
145868e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NA;
145968e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
146068e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NG;
146168e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_A(chan)) {
146268e8e04eSSam Leffler 		status |= IFM_IEEE80211_11A;
146368e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_B(chan)) {
146468e8e04eSSam Leffler 		status |= IFM_IEEE80211_11B;
146568e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
146668e8e04eSSam Leffler 		status |= IFM_IEEE80211_11G;
146768e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
146868e8e04eSSam Leffler 		status |= IFM_IEEE80211_FH;
146968e8e04eSSam Leffler 	}
147068e8e04eSSam Leffler 	/* XXX else complain? */
147168e8e04eSSam Leffler 
147268e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_TURBO(chan))
147368e8e04eSSam Leffler 		status |= IFM_IEEE80211_TURBO;
1474b032f27cSSam Leffler #if 0
1475b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT20(chan))
1476b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT20;
1477b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT40(chan))
1478b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT40;
1479b032f27cSSam Leffler #endif
148068e8e04eSSam Leffler 	return status;
148168e8e04eSSam Leffler }
148268e8e04eSSam Leffler 
1483*ba2c1fbcSAdrian Chadd static void
1484*ba2c1fbcSAdrian Chadd ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1485*ba2c1fbcSAdrian Chadd {
1486*ba2c1fbcSAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1487*ba2c1fbcSAdrian Chadd 	struct ieee80211vap *vap;
1488*ba2c1fbcSAdrian Chadd 
1489*ba2c1fbcSAdrian Chadd 	imr->ifm_status = IFM_AVALID;
1490*ba2c1fbcSAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1491*ba2c1fbcSAdrian Chadd 		if (vap->iv_ifp->if_flags & IFF_UP) {
1492*ba2c1fbcSAdrian Chadd 			imr->ifm_status |= IFM_ACTIVE;
1493*ba2c1fbcSAdrian Chadd 			break;
1494*ba2c1fbcSAdrian Chadd 		}
1495*ba2c1fbcSAdrian Chadd 	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1496*ba2c1fbcSAdrian Chadd 	if (imr->ifm_status & IFM_ACTIVE)
1497*ba2c1fbcSAdrian Chadd 		imr->ifm_current = imr->ifm_active;
1498*ba2c1fbcSAdrian Chadd }
1499*ba2c1fbcSAdrian Chadd 
15001a1e1d21SSam Leffler void
15011a1e1d21SSam Leffler ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
15021a1e1d21SSam Leffler {
1503b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
1504b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
150568e8e04eSSam Leffler 	enum ieee80211_phymode mode;
15061a1e1d21SSam Leffler 
15071a1e1d21SSam Leffler 	imr->ifm_status = IFM_AVALID;
150868e8e04eSSam Leffler 	/*
150968e8e04eSSam Leffler 	 * NB: use the current channel's mode to lock down a xmit
151068e8e04eSSam Leffler 	 * rate only when running; otherwise we may have a mismatch
151168e8e04eSSam Leffler 	 * in which case the rate will not be convertible.
151268e8e04eSSam Leffler 	 */
15139f098ac7SAdrian Chadd 	if (vap->iv_state == IEEE80211_S_RUN ||
15149f098ac7SAdrian Chadd 	    vap->iv_state == IEEE80211_S_SLEEP) {
15151a1e1d21SSam Leffler 		imr->ifm_status |= IFM_ACTIVE;
151668e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_curchan);
151768e8e04eSSam Leffler 	} else
151868e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
1519b032f27cSSam Leffler 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
15208a1b9b6aSSam Leffler 	/*
15218a1b9b6aSSam Leffler 	 * Calculate a current rate if possible.
15228a1b9b6aSSam Leffler 	 */
1523b032f27cSSam Leffler 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
15248a1b9b6aSSam Leffler 		/*
15258a1b9b6aSSam Leffler 		 * A fixed rate is set, report that.
15268a1b9b6aSSam Leffler 		 */
15278a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
1528b032f27cSSam Leffler 			vap->iv_txparms[mode].ucastrate, mode);
1529b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
15308a1b9b6aSSam Leffler 		/*
15318a1b9b6aSSam Leffler 		 * In station mode report the current transmit rate.
15328a1b9b6aSSam Leffler 		 */
15338a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
1534b032f27cSSam Leffler 			vap->iv_bss->ni_txrate, mode);
1535ba99a9b1SAndre Oppermann 	} else
15361a1e1d21SSam Leffler 		imr->ifm_active |= IFM_AUTO;
1537b032f27cSSam Leffler 	if (imr->ifm_status & IFM_ACTIVE)
1538b032f27cSSam Leffler 		imr->ifm_current = imr->ifm_active;
15391a1e1d21SSam Leffler }
15401a1e1d21SSam Leffler 
15411a1e1d21SSam Leffler /*
15421a1e1d21SSam Leffler  * Set the current phy mode and recalculate the active channel
15431a1e1d21SSam Leffler  * set based on the available channels for this mode.  Also
15441a1e1d21SSam Leffler  * select a new default/current channel if the current one is
15451a1e1d21SSam Leffler  * inappropriate for this mode.
15461a1e1d21SSam Leffler  */
15471a1e1d21SSam Leffler int
15481a1e1d21SSam Leffler ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
15491a1e1d21SSam Leffler {
15501a1e1d21SSam Leffler 	/*
1551ca4ac7aeSSam Leffler 	 * Adjust basic rates in 11b/11g supported rate set.
1552ca4ac7aeSSam Leffler 	 * Note that if operating on a hal/quarter rate channel
1553ca4ac7aeSSam Leffler 	 * this is a noop as those rates sets are different
1554ca4ac7aeSSam Leffler 	 * and used instead.
15551a1e1d21SSam Leffler 	 */
1556ca4ac7aeSSam Leffler 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1557b032f27cSSam Leffler 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1558ca4ac7aeSSam Leffler 
15591a1e1d21SSam Leffler 	ic->ic_curmode = mode;
15608a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);	/* reset ERP state */
15618a1b9b6aSSam Leffler 
15621a1e1d21SSam Leffler 	return 0;
15631a1e1d21SSam Leffler }
15641a1e1d21SSam Leffler 
15651a1e1d21SSam Leffler /*
156668e8e04eSSam Leffler  * Return the phy mode for with the specified channel.
15671a1e1d21SSam Leffler  */
15681a1e1d21SSam Leffler enum ieee80211_phymode
156968e8e04eSSam Leffler ieee80211_chan2mode(const struct ieee80211_channel *chan)
15701a1e1d21SSam Leffler {
157168e8e04eSSam Leffler 
157268e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HTA(chan))
157368e8e04eSSam Leffler 		return IEEE80211_MODE_11NA;
157468e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_HTG(chan))
157568e8e04eSSam Leffler 		return IEEE80211_MODE_11NG;
157668e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_108G(chan))
15778a1b9b6aSSam Leffler 		return IEEE80211_MODE_TURBO_G;
157868e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ST(chan))
157968e8e04eSSam Leffler 		return IEEE80211_MODE_STURBO_A;
158068e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_TURBO(chan))
158168e8e04eSSam Leffler 		return IEEE80211_MODE_TURBO_A;
15826a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_HALF(chan))
15836a76ae21SSam Leffler 		return IEEE80211_MODE_HALF;
15846a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
15856a76ae21SSam Leffler 		return IEEE80211_MODE_QUARTER;
158668e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_A(chan))
158768e8e04eSSam Leffler 		return IEEE80211_MODE_11A;
158868e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(chan))
15891a1e1d21SSam Leffler 		return IEEE80211_MODE_11G;
159068e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_B(chan))
159168e8e04eSSam Leffler 		return IEEE80211_MODE_11B;
159268e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_FHSS(chan))
159368e8e04eSSam Leffler 		return IEEE80211_MODE_FH;
159468e8e04eSSam Leffler 
159568e8e04eSSam Leffler 	/* NB: should not get here */
159668e8e04eSSam Leffler 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
159768e8e04eSSam Leffler 		__func__, chan->ic_freq, chan->ic_flags);
15981a1e1d21SSam Leffler 	return IEEE80211_MODE_11B;
15991a1e1d21SSam Leffler }
16001a1e1d21SSam Leffler 
160168e8e04eSSam Leffler struct ratemedia {
160268e8e04eSSam Leffler 	u_int	match;	/* rate + mode */
160368e8e04eSSam Leffler 	u_int	media;	/* if_media rate */
160468e8e04eSSam Leffler };
160568e8e04eSSam Leffler 
160668e8e04eSSam Leffler static int
160768e8e04eSSam Leffler findmedia(const struct ratemedia rates[], int n, u_int match)
160868e8e04eSSam Leffler {
160968e8e04eSSam Leffler 	int i;
161068e8e04eSSam Leffler 
161168e8e04eSSam Leffler 	for (i = 0; i < n; i++)
161268e8e04eSSam Leffler 		if (rates[i].match == match)
161368e8e04eSSam Leffler 			return rates[i].media;
161468e8e04eSSam Leffler 	return IFM_AUTO;
161568e8e04eSSam Leffler }
161668e8e04eSSam Leffler 
16171a1e1d21SSam Leffler /*
161868e8e04eSSam Leffler  * Convert IEEE80211 rate value to ifmedia subtype.
161968e8e04eSSam Leffler  * Rate is either a legacy rate in units of 0.5Mbps
162068e8e04eSSam Leffler  * or an MCS index.
16211a1e1d21SSam Leffler  */
16221a1e1d21SSam Leffler int
16231a1e1d21SSam Leffler ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
16241a1e1d21SSam Leffler {
162568e8e04eSSam Leffler 	static const struct ratemedia rates[] = {
16264844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
16274844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
16284844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
16294844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
16304844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
16314844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
16324844aa7dSAtsushi Onoe 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
16334844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
16344844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
16354844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
16364844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
16374844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
16384844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
16394844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
16404844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
16414844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
16424844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
16434844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
16444844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
16454844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
16464844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
16474844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
16484844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
16494844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
16504844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
16514844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
16524844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
165341b3c790SSam Leffler 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
165441b3c790SSam Leffler 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
165541b3c790SSam Leffler 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
16561a1e1d21SSam Leffler 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
16571a1e1d21SSam Leffler 	};
165868e8e04eSSam Leffler 	static const struct ratemedia htrates[] = {
165968e8e04eSSam Leffler 		{   0, IFM_IEEE80211_MCS },
166068e8e04eSSam Leffler 		{   1, IFM_IEEE80211_MCS },
166168e8e04eSSam Leffler 		{   2, IFM_IEEE80211_MCS },
166268e8e04eSSam Leffler 		{   3, IFM_IEEE80211_MCS },
166368e8e04eSSam Leffler 		{   4, IFM_IEEE80211_MCS },
166468e8e04eSSam Leffler 		{   5, IFM_IEEE80211_MCS },
166568e8e04eSSam Leffler 		{   6, IFM_IEEE80211_MCS },
166668e8e04eSSam Leffler 		{   7, IFM_IEEE80211_MCS },
166768e8e04eSSam Leffler 		{   8, IFM_IEEE80211_MCS },
166868e8e04eSSam Leffler 		{   9, IFM_IEEE80211_MCS },
166968e8e04eSSam Leffler 		{  10, IFM_IEEE80211_MCS },
167068e8e04eSSam Leffler 		{  11, IFM_IEEE80211_MCS },
167168e8e04eSSam Leffler 		{  12, IFM_IEEE80211_MCS },
167268e8e04eSSam Leffler 		{  13, IFM_IEEE80211_MCS },
167368e8e04eSSam Leffler 		{  14, IFM_IEEE80211_MCS },
167468e8e04eSSam Leffler 		{  15, IFM_IEEE80211_MCS },
1675f136f45fSBernhard Schmidt 		{  16, IFM_IEEE80211_MCS },
1676f136f45fSBernhard Schmidt 		{  17, IFM_IEEE80211_MCS },
1677f136f45fSBernhard Schmidt 		{  18, IFM_IEEE80211_MCS },
1678f136f45fSBernhard Schmidt 		{  19, IFM_IEEE80211_MCS },
1679f136f45fSBernhard Schmidt 		{  20, IFM_IEEE80211_MCS },
1680f136f45fSBernhard Schmidt 		{  21, IFM_IEEE80211_MCS },
1681f136f45fSBernhard Schmidt 		{  22, IFM_IEEE80211_MCS },
1682f136f45fSBernhard Schmidt 		{  23, IFM_IEEE80211_MCS },
1683f136f45fSBernhard Schmidt 		{  24, IFM_IEEE80211_MCS },
1684f136f45fSBernhard Schmidt 		{  25, IFM_IEEE80211_MCS },
1685f136f45fSBernhard Schmidt 		{  26, IFM_IEEE80211_MCS },
1686f136f45fSBernhard Schmidt 		{  27, IFM_IEEE80211_MCS },
1687f136f45fSBernhard Schmidt 		{  28, IFM_IEEE80211_MCS },
1688f136f45fSBernhard Schmidt 		{  29, IFM_IEEE80211_MCS },
1689f136f45fSBernhard Schmidt 		{  30, IFM_IEEE80211_MCS },
1690f136f45fSBernhard Schmidt 		{  31, IFM_IEEE80211_MCS },
1691f136f45fSBernhard Schmidt 		{  32, IFM_IEEE80211_MCS },
1692f136f45fSBernhard Schmidt 		{  33, IFM_IEEE80211_MCS },
1693f136f45fSBernhard Schmidt 		{  34, IFM_IEEE80211_MCS },
1694f136f45fSBernhard Schmidt 		{  35, IFM_IEEE80211_MCS },
1695f136f45fSBernhard Schmidt 		{  36, IFM_IEEE80211_MCS },
1696f136f45fSBernhard Schmidt 		{  37, IFM_IEEE80211_MCS },
1697f136f45fSBernhard Schmidt 		{  38, IFM_IEEE80211_MCS },
1698f136f45fSBernhard Schmidt 		{  39, IFM_IEEE80211_MCS },
1699f136f45fSBernhard Schmidt 		{  40, IFM_IEEE80211_MCS },
1700f136f45fSBernhard Schmidt 		{  41, IFM_IEEE80211_MCS },
1701f136f45fSBernhard Schmidt 		{  42, IFM_IEEE80211_MCS },
1702f136f45fSBernhard Schmidt 		{  43, IFM_IEEE80211_MCS },
1703f136f45fSBernhard Schmidt 		{  44, IFM_IEEE80211_MCS },
1704f136f45fSBernhard Schmidt 		{  45, IFM_IEEE80211_MCS },
1705f136f45fSBernhard Schmidt 		{  46, IFM_IEEE80211_MCS },
1706f136f45fSBernhard Schmidt 		{  47, IFM_IEEE80211_MCS },
1707f136f45fSBernhard Schmidt 		{  48, IFM_IEEE80211_MCS },
1708f136f45fSBernhard Schmidt 		{  49, IFM_IEEE80211_MCS },
1709f136f45fSBernhard Schmidt 		{  50, IFM_IEEE80211_MCS },
1710f136f45fSBernhard Schmidt 		{  51, IFM_IEEE80211_MCS },
1711f136f45fSBernhard Schmidt 		{  52, IFM_IEEE80211_MCS },
1712f136f45fSBernhard Schmidt 		{  53, IFM_IEEE80211_MCS },
1713f136f45fSBernhard Schmidt 		{  54, IFM_IEEE80211_MCS },
1714f136f45fSBernhard Schmidt 		{  55, IFM_IEEE80211_MCS },
1715f136f45fSBernhard Schmidt 		{  56, IFM_IEEE80211_MCS },
1716f136f45fSBernhard Schmidt 		{  57, IFM_IEEE80211_MCS },
1717f136f45fSBernhard Schmidt 		{  58, IFM_IEEE80211_MCS },
1718f136f45fSBernhard Schmidt 		{  59, IFM_IEEE80211_MCS },
1719f136f45fSBernhard Schmidt 		{  60, IFM_IEEE80211_MCS },
1720f136f45fSBernhard Schmidt 		{  61, IFM_IEEE80211_MCS },
1721f136f45fSBernhard Schmidt 		{  62, IFM_IEEE80211_MCS },
1722f136f45fSBernhard Schmidt 		{  63, IFM_IEEE80211_MCS },
1723f136f45fSBernhard Schmidt 		{  64, IFM_IEEE80211_MCS },
1724f136f45fSBernhard Schmidt 		{  65, IFM_IEEE80211_MCS },
1725f136f45fSBernhard Schmidt 		{  66, IFM_IEEE80211_MCS },
1726f136f45fSBernhard Schmidt 		{  67, IFM_IEEE80211_MCS },
1727f136f45fSBernhard Schmidt 		{  68, IFM_IEEE80211_MCS },
1728f136f45fSBernhard Schmidt 		{  69, IFM_IEEE80211_MCS },
1729f136f45fSBernhard Schmidt 		{  70, IFM_IEEE80211_MCS },
1730f136f45fSBernhard Schmidt 		{  71, IFM_IEEE80211_MCS },
1731f136f45fSBernhard Schmidt 		{  72, IFM_IEEE80211_MCS },
1732f136f45fSBernhard Schmidt 		{  73, IFM_IEEE80211_MCS },
1733f136f45fSBernhard Schmidt 		{  74, IFM_IEEE80211_MCS },
1734f136f45fSBernhard Schmidt 		{  75, IFM_IEEE80211_MCS },
1735f136f45fSBernhard Schmidt 		{  76, IFM_IEEE80211_MCS },
173668e8e04eSSam Leffler 	};
173768e8e04eSSam Leffler 	int m;
17381a1e1d21SSam Leffler 
173968e8e04eSSam Leffler 	/*
174068e8e04eSSam Leffler 	 * Check 11n rates first for match as an MCS.
174168e8e04eSSam Leffler 	 */
174268e8e04eSSam Leffler 	if (mode == IEEE80211_MODE_11NA) {
1743f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
1744f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
1745a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
174668e8e04eSSam Leffler 			if (m != IFM_AUTO)
174768e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NA;
174868e8e04eSSam Leffler 		}
174968e8e04eSSam Leffler 	} else if (mode == IEEE80211_MODE_11NG) {
175068e8e04eSSam Leffler 		/* NB: 12 is ambiguous, it will be treated as an MCS */
1751f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
1752f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
1753a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
175468e8e04eSSam Leffler 			if (m != IFM_AUTO)
175568e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NG;
175668e8e04eSSam Leffler 		}
175768e8e04eSSam Leffler 	}
175868e8e04eSSam Leffler 	rate &= IEEE80211_RATE_VAL;
17591a1e1d21SSam Leffler 	switch (mode) {
17601a1e1d21SSam Leffler 	case IEEE80211_MODE_11A:
17616a76ae21SSam Leffler 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
17626a76ae21SSam Leffler 	case IEEE80211_MODE_QUARTER:
176368e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
17648a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_A:
176568e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
1766a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
1767a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11A);
17681a1e1d21SSam Leffler 	case IEEE80211_MODE_11B:
1769a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
1770a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11B);
17714844aa7dSAtsushi Onoe 	case IEEE80211_MODE_FH:
1772a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
1773a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_FH);
17741a1e1d21SSam Leffler 	case IEEE80211_MODE_AUTO:
17751a1e1d21SSam Leffler 		/* NB: ic may be NULL for some drivers */
1776566d825bSSam Leffler 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1777a3e08d6fSRui Paulo 			return findmedia(rates, nitems(rates),
177868e8e04eSSam Leffler 			    rate | IFM_IEEE80211_FH);
17791a1e1d21SSam Leffler 		/* NB: hack, 11g matches both 11b+11a rates */
17801a1e1d21SSam Leffler 		/* fall thru... */
17811a1e1d21SSam Leffler 	case IEEE80211_MODE_11G:
178268e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
17838a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_G:
1784a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
17851a1e1d21SSam Leffler 	}
17861a1e1d21SSam Leffler 	return IFM_AUTO;
17871a1e1d21SSam Leffler }
17881a1e1d21SSam Leffler 
17891a1e1d21SSam Leffler int
17901a1e1d21SSam Leffler ieee80211_media2rate(int mword)
17911a1e1d21SSam Leffler {
17921a1e1d21SSam Leffler 	static const int ieeerates[] = {
17931a1e1d21SSam Leffler 		-1,		/* IFM_AUTO */
17941a1e1d21SSam Leffler 		0,		/* IFM_MANUAL */
17951a1e1d21SSam Leffler 		0,		/* IFM_NONE */
17961a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_FH1 */
17971a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_FH2 */
17981a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_DS1 */
17991a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_DS2 */
18001a1e1d21SSam Leffler 		11,		/* IFM_IEEE80211_DS5 */
18011a1e1d21SSam Leffler 		22,		/* IFM_IEEE80211_DS11 */
18021a1e1d21SSam Leffler 		44,		/* IFM_IEEE80211_DS22 */
18031a1e1d21SSam Leffler 		12,		/* IFM_IEEE80211_OFDM6 */
18041a1e1d21SSam Leffler 		18,		/* IFM_IEEE80211_OFDM9 */
18051a1e1d21SSam Leffler 		24,		/* IFM_IEEE80211_OFDM12 */
18061a1e1d21SSam Leffler 		36,		/* IFM_IEEE80211_OFDM18 */
18071a1e1d21SSam Leffler 		48,		/* IFM_IEEE80211_OFDM24 */
18081a1e1d21SSam Leffler 		72,		/* IFM_IEEE80211_OFDM36 */
18091a1e1d21SSam Leffler 		96,		/* IFM_IEEE80211_OFDM48 */
18101a1e1d21SSam Leffler 		108,		/* IFM_IEEE80211_OFDM54 */
18111a1e1d21SSam Leffler 		144,		/* IFM_IEEE80211_OFDM72 */
181241b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS354k */
181341b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS512k */
181441b3c790SSam Leffler 		6,		/* IFM_IEEE80211_OFDM3 */
181541b3c790SSam Leffler 		9,		/* IFM_IEEE80211_OFDM4 */
181641b3c790SSam Leffler 		54,		/* IFM_IEEE80211_OFDM27 */
181768e8e04eSSam Leffler 		-1,		/* IFM_IEEE80211_MCS */
18181a1e1d21SSam Leffler 	};
1819a3e08d6fSRui Paulo 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
18201a1e1d21SSam Leffler 		ieeerates[IFM_SUBTYPE(mword)] : 0;
18211a1e1d21SSam Leffler }
18225b16c28cSSam Leffler 
18235b16c28cSSam Leffler /*
18245b16c28cSSam Leffler  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
18255b16c28cSSam Leffler  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
18265b16c28cSSam Leffler  */
18275b16c28cSSam Leffler #define	mix(a, b, c)							\
18285b16c28cSSam Leffler do {									\
18295b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 13);					\
18305b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 8);					\
18315b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 13);					\
18325b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 12);					\
18335b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 16);					\
18345b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 5);					\
18355b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 3);					\
18365b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 10);					\
18375b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 15);					\
18385b16c28cSSam Leffler } while (/*CONSTCOND*/0)
18395b16c28cSSam Leffler 
18405b16c28cSSam Leffler uint32_t
18415b16c28cSSam Leffler ieee80211_mac_hash(const struct ieee80211com *ic,
18425b16c28cSSam Leffler 	const uint8_t addr[IEEE80211_ADDR_LEN])
18435b16c28cSSam Leffler {
18445b16c28cSSam Leffler 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
18455b16c28cSSam Leffler 
18465b16c28cSSam Leffler 	b += addr[5] << 8;
18475b16c28cSSam Leffler 	b += addr[4];
18485b16c28cSSam Leffler 	a += addr[3] << 24;
18495b16c28cSSam Leffler 	a += addr[2] << 16;
18505b16c28cSSam Leffler 	a += addr[1] << 8;
18515b16c28cSSam Leffler 	a += addr[0];
18525b16c28cSSam Leffler 
18535b16c28cSSam Leffler 	mix(a, b, c);
18545b16c28cSSam Leffler 
18555b16c28cSSam Leffler 	return c;
18565b16c28cSSam Leffler }
18575b16c28cSSam Leffler #undef mix
1858a1cbd043SAdrian Chadd 
1859a1cbd043SAdrian Chadd char
1860a1cbd043SAdrian Chadd ieee80211_channel_type_char(const struct ieee80211_channel *c)
1861a1cbd043SAdrian Chadd {
1862a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ST(c))
1863a1cbd043SAdrian Chadd 		return 'S';
1864a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108A(c))
1865a1cbd043SAdrian Chadd 		return 'T';
1866a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108G(c))
1867a1cbd043SAdrian Chadd 		return 'G';
1868a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(c))
1869a1cbd043SAdrian Chadd 		return 'n';
1870a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_A(c))
1871a1cbd043SAdrian Chadd 		return 'a';
1872a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ANYG(c))
1873a1cbd043SAdrian Chadd 		return 'g';
1874a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_B(c))
1875a1cbd043SAdrian Chadd 		return 'b';
1876a1cbd043SAdrian Chadd 	return 'f';
1877a1cbd043SAdrian Chadd }
1878