xref: /freebsd/sys/net80211/ieee80211.c (revision 67f4aa3878efa3ab6b639e65830d52b30e303f0d)
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>
388ec07310SGleb Smirnoff #include <sys/malloc.h>
398a1b9b6aSSam Leffler #include <sys/socket.h>
407a79cebfSGleb Smirnoff #include <sys/sbuf.h>
411a1e1d21SSam Leffler 
42c8f5794eSGleb Smirnoff #include <machine/stdarg.h>
43c8f5794eSGleb Smirnoff 
441a1e1d21SSam Leffler #include <net/if.h>
4576039bc8SGleb Smirnoff #include <net/if_var.h>
46b032f27cSSam Leffler #include <net/if_dl.h>
471a1e1d21SSam Leffler #include <net/if_media.h>
48b032f27cSSam Leffler #include <net/if_types.h>
491a1e1d21SSam Leffler #include <net/ethernet.h>
501a1e1d21SSam Leffler 
511a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
52b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
53616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
54616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
55616190d0SSam Leffler #endif
56b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
57*67f4aa38SAdrian Chadd #include <net80211/ieee80211_vht.h>
581a1e1d21SSam Leffler 
591a1e1d21SSam Leffler #include <net/bpf.h>
601a1e1d21SSam Leffler 
61bb77492fSSam Leffler const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
62bb77492fSSam Leffler 	[IEEE80211_MODE_AUTO]	  = "auto",
63bb77492fSSam Leffler 	[IEEE80211_MODE_11A]	  = "11a",
64bb77492fSSam Leffler 	[IEEE80211_MODE_11B]	  = "11b",
65bb77492fSSam Leffler 	[IEEE80211_MODE_11G]	  = "11g",
66bb77492fSSam Leffler 	[IEEE80211_MODE_FH]	  = "FH",
67bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_A]  = "turboA",
68bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_G]  = "turboG",
69bb77492fSSam Leffler 	[IEEE80211_MODE_STURBO_A] = "sturboA",
706a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	  = "half",
716a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]  = "quarter",
72bb77492fSSam Leffler 	[IEEE80211_MODE_11NA]	  = "11na",
73bb77492fSSam Leffler 	[IEEE80211_MODE_11NG]	  = "11ng",
740c67d389SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	  = "11acg",
750c67d389SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	  = "11ac",
761a1e1d21SSam Leffler };
77c43feedeSSam Leffler /* map ieee80211_opmode to the corresponding capability bit */
78c43feedeSSam Leffler const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
79c43feedeSSam Leffler 	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
80c43feedeSSam Leffler 	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
81c43feedeSSam Leffler 	[IEEE80211_M_STA]	= IEEE80211_C_STA,
82c43feedeSSam Leffler 	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
83c43feedeSSam Leffler 	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
84c43feedeSSam Leffler 	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
8559aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
8659aa14a9SRui Paulo 	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
8759aa14a9SRui Paulo #endif
88c43feedeSSam Leffler };
89c43feedeSSam Leffler 
9092002144SGleb Smirnoff const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
91b032f27cSSam Leffler 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
92b032f27cSSam Leffler 
93b032f27cSSam Leffler static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
942bfc8a91SSam Leffler static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
95b032f27cSSam Leffler static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
968e71a4aaSAdrian Chadd static	void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
97b032f27cSSam Leffler static	int ieee80211_media_setup(struct ieee80211com *ic,
98b032f27cSSam Leffler 		struct ifmedia *media, int caps, int addsta,
99b032f27cSSam Leffler 		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
100b032f27cSSam Leffler static	int media_status(enum ieee80211_opmode,
101b032f27cSSam Leffler 		const struct ieee80211_channel *);
10228da1b56SGleb Smirnoff static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
103b032f27cSSam Leffler 
104b032f27cSSam Leffler MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
1051a1e1d21SSam Leffler 
106aadecb1aSSam Leffler /*
107aadecb1aSSam Leffler  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
108aadecb1aSSam Leffler  */
109aadecb1aSSam Leffler #define	B(r)	((r) | IEEE80211_RATE_BASIC)
110aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11a =
111aadecb1aSSam Leffler 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
11241b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_half =
11341b3c790SSam Leffler 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
11441b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_quarter =
11541b3c790SSam Leffler 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
116aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11b =
117aadecb1aSSam Leffler 	{ 4, { B(2), B(4), B(11), B(22) } };
118aadecb1aSSam Leffler /* NB: OFDM rates are handled specially based on mode */
119aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11g =
120aadecb1aSSam Leffler 	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
121aadecb1aSSam Leffler #undef B
122aadecb1aSSam Leffler 
123*67f4aa38SAdrian Chadd static int set_vht_extchan(struct ieee80211_channel *c);
124*67f4aa38SAdrian Chadd 
1251a1e1d21SSam Leffler /*
1261a1e1d21SSam Leffler  * Fill in 802.11 available channel set, mark
1271a1e1d21SSam Leffler  * all available channels as active, and pick
1281a1e1d21SSam Leffler  * a default channel if not already specified.
1291a1e1d21SSam Leffler  */
1307a79cebfSGleb Smirnoff void
13141b3c790SSam Leffler ieee80211_chan_init(struct ieee80211com *ic)
13241b3c790SSam Leffler {
13341b3c790SSam Leffler #define	DEFAULTRATES(m, def) do { \
1346a76ae21SSam Leffler 	if (ic->ic_sup_rates[m].rs_nrates == 0) \
13545fa8b0eSSam Leffler 		ic->ic_sup_rates[m] = def; \
13641b3c790SSam Leffler } while (0)
13741b3c790SSam Leffler 	struct ieee80211_channel *c;
13841b3c790SSam Leffler 	int i;
13941b3c790SSam Leffler 
14031378b1cSSam Leffler 	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
14168e8e04eSSam Leffler 		("invalid number of channels specified: %u", ic->ic_nchans));
1421a1e1d21SSam Leffler 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
143b032f27cSSam Leffler 	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
1446dbd16f1SSam Leffler 	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
14568e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1461a1e1d21SSam Leffler 		c = &ic->ic_channels[i];
14768e8e04eSSam Leffler 		KASSERT(c->ic_flags != 0, ("channel with no flags"));
1489c2c544dSSam Leffler 		/*
1499c2c544dSSam Leffler 		 * Help drivers that work only with frequencies by filling
1509c2c544dSSam Leffler 		 * in IEEE channel #'s if not already calculated.  Note this
1519c2c544dSSam Leffler 		 * mimics similar work done in ieee80211_setregdomain when
1529c2c544dSSam Leffler 		 * changing regulatory state.
1539c2c544dSSam Leffler 		 */
1549c2c544dSSam Leffler 		if (c->ic_ieee == 0)
1559c2c544dSSam Leffler 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
156*67f4aa38SAdrian Chadd 
157*67f4aa38SAdrian Chadd 		/*
158*67f4aa38SAdrian Chadd 		 * Setup the HT40/VHT40 upper/lower bits.
159*67f4aa38SAdrian Chadd 		 * The VHT80 math is done elsewhere.
160*67f4aa38SAdrian Chadd 		 */
1619c2c544dSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
1629c2c544dSSam Leffler 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
1639c2c544dSSam Leffler 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
1649c2c544dSSam Leffler 			    c->ic_flags);
165*67f4aa38SAdrian Chadd 
166*67f4aa38SAdrian Chadd 		/* Update VHT math */
167*67f4aa38SAdrian Chadd 		/*
168*67f4aa38SAdrian Chadd 		 * XXX VHT again, note that this assumes VHT80 channels
169*67f4aa38SAdrian Chadd 		 * are legit already
170*67f4aa38SAdrian Chadd 		 */
171*67f4aa38SAdrian Chadd 		set_vht_extchan(c);
172*67f4aa38SAdrian Chadd 
1739c2c544dSSam Leffler 		/* default max tx power to max regulatory */
1749c2c544dSSam Leffler 		if (c->ic_maxpower == 0)
1759c2c544dSSam Leffler 			c->ic_maxpower = 2*c->ic_maxregpower;
17668e8e04eSSam Leffler 		setbit(ic->ic_chan_avail, c->ic_ieee);
1771a1e1d21SSam Leffler 		/*
1781a1e1d21SSam Leffler 		 * Identify mode capabilities.
1791a1e1d21SSam Leffler 		 */
1801a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_A(c))
1816dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
1821a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_B(c))
1836dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
18445fa8b0eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(c))
1856dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
1864844aa7dSAtsushi Onoe 		if (IEEE80211_IS_CHAN_FHSS(c))
1876dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
18868e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_108A(c))
1896dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
1908a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_108G(c))
1916dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
19268e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
19368e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
1946a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_HALF(c))
1956a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
1966a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_QUARTER(c))
1976a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
19868e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTA(c))
19968e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
20068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTG(c))
20168e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
2020c67d389SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHTA(c))
2030c67d389SAdrian Chadd 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
2040c67d389SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHTG(c))
2050c67d389SAdrian Chadd 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
20668e8e04eSSam Leffler 	}
20768e8e04eSSam Leffler 	/* initialize candidate channels to all available */
20868e8e04eSSam Leffler 	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
20968e8e04eSSam Leffler 		sizeof(ic->ic_chan_avail));
21068e8e04eSSam Leffler 
211b032f27cSSam Leffler 	/* sort channel table to allow lookup optimizations */
212b032f27cSSam Leffler 	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
213b032f27cSSam Leffler 
214b032f27cSSam Leffler 	/* invalidate any previous state */
21568e8e04eSSam Leffler 	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
216ab562eefSSam Leffler 	ic->ic_prevchan = NULL;
217b032f27cSSam Leffler 	ic->ic_csa_newchan = NULL;
218b5c99415SSam Leffler 	/* arbitrarily pick the first channel */
21968e8e04eSSam Leffler 	ic->ic_curchan = &ic->ic_channels[0];
22026d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
221aadecb1aSSam Leffler 
222aadecb1aSSam Leffler 	/* fillin well-known rate sets if driver has not specified */
22341b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
22441b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
22541b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
22641b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
22741b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
2288500d65dSSam Leffler 	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
2296a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
2306a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
23140432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
23240432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
2330c67d389SAdrian Chadd 	DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ,	 ieee80211_rateset_11g);
2340c67d389SAdrian Chadd 	DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ,	 ieee80211_rateset_11a);
23541b3c790SSam Leffler 
23641b3c790SSam Leffler 	/*
237fbbe47a9SBernhard Schmidt 	 * Setup required information to fill the mcsset field, if driver did
238fbbe47a9SBernhard Schmidt 	 * not. Assume a 2T2R setup for historic reasons.
239fbbe47a9SBernhard Schmidt 	 */
240fbbe47a9SBernhard Schmidt 	if (ic->ic_rxstream == 0)
241fbbe47a9SBernhard Schmidt 		ic->ic_rxstream = 2;
242fbbe47a9SBernhard Schmidt 	if (ic->ic_txstream == 0)
243fbbe47a9SBernhard Schmidt 		ic->ic_txstream = 2;
244fbbe47a9SBernhard Schmidt 
245fbbe47a9SBernhard Schmidt 	/*
24641b3c790SSam Leffler 	 * Set auto mode to reset active channel state and any desired channel.
24741b3c790SSam Leffler 	 */
24841b3c790SSam Leffler 	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
24941b3c790SSam Leffler #undef DEFAULTRATES
25041b3c790SSam Leffler }
25141b3c790SSam Leffler 
252b032f27cSSam Leffler static void
253272f6adeSGleb Smirnoff null_update_mcast(struct ieee80211com *ic)
254b032f27cSSam Leffler {
255272f6adeSGleb Smirnoff 
256272f6adeSGleb Smirnoff 	ic_printf(ic, "need multicast update callback\n");
257b032f27cSSam Leffler }
258b032f27cSSam Leffler 
259b032f27cSSam Leffler static void
260272f6adeSGleb Smirnoff null_update_promisc(struct ieee80211com *ic)
261b032f27cSSam Leffler {
262272f6adeSGleb Smirnoff 
263272f6adeSGleb Smirnoff 	ic_printf(ic, "need promiscuous mode update callback\n");
264b032f27cSSam Leffler }
265b032f27cSSam Leffler 
266b94299c4SAdrian Chadd static void
267b94299c4SAdrian Chadd null_update_chw(struct ieee80211com *ic)
268b94299c4SAdrian Chadd {
269b94299c4SAdrian Chadd 
270c8f5794eSGleb Smirnoff 	ic_printf(ic, "%s: need callback\n", __func__);
271c8f5794eSGleb Smirnoff }
272c8f5794eSGleb Smirnoff 
273c8f5794eSGleb Smirnoff int
274c8f5794eSGleb Smirnoff ic_printf(struct ieee80211com *ic, const char * fmt, ...)
275c8f5794eSGleb Smirnoff {
276c8f5794eSGleb Smirnoff 	va_list ap;
277c8f5794eSGleb Smirnoff 	int retval;
278c8f5794eSGleb Smirnoff 
279c8f5794eSGleb Smirnoff 	retval = printf("%s: ", ic->ic_name);
280c8f5794eSGleb Smirnoff 	va_start(ap, fmt);
281c8f5794eSGleb Smirnoff 	retval += vprintf(fmt, ap);
282c8f5794eSGleb Smirnoff 	va_end(ap);
283c8f5794eSGleb Smirnoff 	return (retval);
284b94299c4SAdrian Chadd }
285b94299c4SAdrian Chadd 
2867a79cebfSGleb Smirnoff static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
2877a79cebfSGleb Smirnoff static struct mtx ic_list_mtx;
2887a79cebfSGleb Smirnoff MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
2897a79cebfSGleb Smirnoff 
2907a79cebfSGleb Smirnoff static int
2917a79cebfSGleb Smirnoff sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
2927a79cebfSGleb Smirnoff {
2937a79cebfSGleb Smirnoff 	struct ieee80211com *ic;
294f09a089eSAndriy Voskoboinyk 	struct sbuf sb;
2957a79cebfSGleb Smirnoff 	char *sp;
2967a79cebfSGleb Smirnoff 	int error;
2977a79cebfSGleb Smirnoff 
298f09a089eSAndriy Voskoboinyk 	error = sysctl_wire_old_buffer(req, 0);
299f09a089eSAndriy Voskoboinyk 	if (error)
300f09a089eSAndriy Voskoboinyk 		return (error);
301f09a089eSAndriy Voskoboinyk 	sbuf_new_for_sysctl(&sb, NULL, 8, req);
302f09a089eSAndriy Voskoboinyk 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
3037a79cebfSGleb Smirnoff 	sp = "";
3047a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3057a79cebfSGleb Smirnoff 	LIST_FOREACH(ic, &ic_head, ic_next) {
306f09a089eSAndriy Voskoboinyk 		sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
3077a79cebfSGleb Smirnoff 		sp = " ";
3087a79cebfSGleb Smirnoff 	}
3097a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
310f09a089eSAndriy Voskoboinyk 	error = sbuf_finish(&sb);
311f09a089eSAndriy Voskoboinyk 	sbuf_delete(&sb);
3127a79cebfSGleb Smirnoff 	return (error);
3137a79cebfSGleb Smirnoff }
3147a79cebfSGleb Smirnoff 
3157a79cebfSGleb Smirnoff SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
3167a79cebfSGleb Smirnoff     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3177a79cebfSGleb Smirnoff     sysctl_ieee80211coms, "A", "names of available 802.11 devices");
3187a79cebfSGleb Smirnoff 
319b032f27cSSam Leffler /*
320b032f27cSSam Leffler  * Attach/setup the common net80211 state.  Called by
321b032f27cSSam Leffler  * the driver on attach to prior to creating any vap's.
322b032f27cSSam Leffler  */
32341b3c790SSam Leffler void
3247a79cebfSGleb Smirnoff ieee80211_ifattach(struct ieee80211com *ic)
32541b3c790SSam Leffler {
32641b3c790SSam Leffler 
327c8f5794eSGleb Smirnoff 	IEEE80211_LOCK_INIT(ic, ic->ic_name);
328c8f5794eSGleb Smirnoff 	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
329b032f27cSSam Leffler 	TAILQ_INIT(&ic->ic_vaps);
3305efea30fSAndrew Thompson 
3315efea30fSAndrew Thompson 	/* Create a taskqueue for all state changes */
3325efea30fSAndrew Thompson 	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
3335efea30fSAndrew Thompson 	    taskqueue_thread_enqueue, &ic->ic_tq);
3347b2b15ebSAdrian Chadd 	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
3357fc10b6bSGleb Smirnoff 	    ic->ic_name);
33628da1b56SGleb Smirnoff 	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
33728da1b56SGleb Smirnoff 	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
33841b3c790SSam Leffler 	/*
33941b3c790SSam Leffler 	 * Fill in 802.11 available channel set, mark all
34041b3c790SSam Leffler 	 * available channels as active, and pick a default
34141b3c790SSam Leffler 	 * channel if not already specified.
34241b3c790SSam Leffler 	 */
3437a79cebfSGleb Smirnoff 	ieee80211_chan_init(ic);
34468e8e04eSSam Leffler 
345b032f27cSSam Leffler 	ic->ic_update_mcast = null_update_mcast;
346b032f27cSSam Leffler 	ic->ic_update_promisc = null_update_promisc;
347b94299c4SAdrian Chadd 	ic->ic_update_chw = null_update_chw;
3481a1e1d21SSam Leffler 
3495b16c28cSSam Leffler 	ic->ic_hash_key = arc4random();
350d365f9c7SSam Leffler 	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
351d365f9c7SSam Leffler 	ic->ic_lintval = ic->ic_bintval;
3528a1b9b6aSSam Leffler 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
3538a1b9b6aSSam Leffler 
35468e8e04eSSam Leffler 	ieee80211_crypto_attach(ic);
3558a1b9b6aSSam Leffler 	ieee80211_node_attach(ic);
35668e8e04eSSam Leffler 	ieee80211_power_attach(ic);
3578a1b9b6aSSam Leffler 	ieee80211_proto_attach(ic);
358616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
359616190d0SSam Leffler 	ieee80211_superg_attach(ic);
360616190d0SSam Leffler #endif
36168e8e04eSSam Leffler 	ieee80211_ht_attach(ic);
362*67f4aa38SAdrian Chadd 	ieee80211_vht_attach(ic);
36368e8e04eSSam Leffler 	ieee80211_scan_attach(ic);
364b032f27cSSam Leffler 	ieee80211_regdomain_attach(ic);
365e95e0edbSSam Leffler 	ieee80211_dfs_attach(ic);
3668a1b9b6aSSam Leffler 
367b032f27cSSam Leffler 	ieee80211_sysctl_attach(ic);
3688a1b9b6aSSam Leffler 
3697a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3707a79cebfSGleb Smirnoff 	LIST_INSERT_HEAD(&ic_head, ic, ic_next);
3717a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
3721a1e1d21SSam Leffler }
3731a1e1d21SSam Leffler 
374b032f27cSSam Leffler /*
375b032f27cSSam Leffler  * Detach net80211 state on device detach.  Tear down
376b032f27cSSam Leffler  * all vap's and reclaim all common state prior to the
377b032f27cSSam Leffler  * device state going away.  Note we may call back into
378b032f27cSSam Leffler  * driver; it must be prepared for this.
379b032f27cSSam Leffler  */
3801a1e1d21SSam Leffler void
3818a1b9b6aSSam Leffler ieee80211_ifdetach(struct ieee80211com *ic)
3821a1e1d21SSam Leffler {
383b032f27cSSam Leffler 	struct ieee80211vap *vap;
3841a1e1d21SSam Leffler 
3857a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3867a79cebfSGleb Smirnoff 	LIST_REMOVE(ic, ic_next);
3877a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
3885c600a90SSam Leffler 
3894061c639SAndriy Voskoboinyk 	taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
3904061c639SAndriy Voskoboinyk 
39130e4856aSAdrian Chadd 	/*
39230e4856aSAdrian Chadd 	 * The VAP is responsible for setting and clearing
39330e4856aSAdrian Chadd 	 * the VIMAGE context.
39430e4856aSAdrian Chadd 	 */
395b032f27cSSam Leffler 	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
396b032f27cSSam Leffler 		ieee80211_vap_destroy(vap);
397ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
3988a1b9b6aSSam Leffler 
3998a1b9b6aSSam Leffler 	ieee80211_sysctl_detach(ic);
400e95e0edbSSam Leffler 	ieee80211_dfs_detach(ic);
401b032f27cSSam Leffler 	ieee80211_regdomain_detach(ic);
40268e8e04eSSam Leffler 	ieee80211_scan_detach(ic);
403616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
404616190d0SSam Leffler 	ieee80211_superg_detach(ic);
405616190d0SSam Leffler #endif
406*67f4aa38SAdrian Chadd 	ieee80211_vht_detach(ic);
40768e8e04eSSam Leffler 	ieee80211_ht_detach(ic);
408ca4ac7aeSSam Leffler 	/* NB: must be called before ieee80211_node_detach */
4098a1b9b6aSSam Leffler 	ieee80211_proto_detach(ic);
4108a1b9b6aSSam Leffler 	ieee80211_crypto_detach(ic);
41168e8e04eSSam Leffler 	ieee80211_power_detach(ic);
4128a1b9b6aSSam Leffler 	ieee80211_node_detach(ic);
4138a1b9b6aSSam Leffler 
41428da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_ierrors);
41528da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_oerrors);
41630e4856aSAdrian Chadd 
4175efea30fSAndrew Thompson 	taskqueue_free(ic->ic_tq);
4185cda6006SAdrian Chadd 	IEEE80211_TX_LOCK_DESTROY(ic);
41968e8e04eSSam Leffler 	IEEE80211_LOCK_DESTROY(ic);
420b032f27cSSam Leffler }
4218a1b9b6aSSam Leffler 
4227a79cebfSGleb Smirnoff struct ieee80211com *
4237a79cebfSGleb Smirnoff ieee80211_find_com(const char *name)
4247a79cebfSGleb Smirnoff {
4257a79cebfSGleb Smirnoff 	struct ieee80211com *ic;
4267a79cebfSGleb Smirnoff 
4277a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
4287a79cebfSGleb Smirnoff 	LIST_FOREACH(ic, &ic_head, ic_next)
4297a79cebfSGleb Smirnoff 		if (strcmp(ic->ic_name, name) == 0)
4307a79cebfSGleb Smirnoff 			break;
4317a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
4327a79cebfSGleb Smirnoff 
4337a79cebfSGleb Smirnoff 	return (ic);
4347a79cebfSGleb Smirnoff }
4357a79cebfSGleb Smirnoff 
4367cde0202SAndriy Voskoboinyk void
4377cde0202SAndriy Voskoboinyk ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
4387cde0202SAndriy Voskoboinyk {
4397cde0202SAndriy Voskoboinyk 	struct ieee80211com *ic;
4407cde0202SAndriy Voskoboinyk 
4417cde0202SAndriy Voskoboinyk 	mtx_lock(&ic_list_mtx);
4427cde0202SAndriy Voskoboinyk 	LIST_FOREACH(ic, &ic_head, ic_next)
4437cde0202SAndriy Voskoboinyk 		(*f)(arg, ic);
4447cde0202SAndriy Voskoboinyk 	mtx_unlock(&ic_list_mtx);
4457cde0202SAndriy Voskoboinyk }
4467cde0202SAndriy Voskoboinyk 
447b032f27cSSam Leffler /*
448b032f27cSSam Leffler  * Default reset method for use with the ioctl support.  This
449b032f27cSSam Leffler  * method is invoked after any state change in the 802.11
450b032f27cSSam Leffler  * layer that should be propagated to the hardware but not
451b032f27cSSam Leffler  * require re-initialization of the 802.11 state machine (e.g
452b032f27cSSam Leffler  * rescanning for an ap).  We always return ENETRESET which
453b032f27cSSam Leffler  * should cause the driver to re-initialize the device. Drivers
454b032f27cSSam Leffler  * can override this method to implement more optimized support.
455b032f27cSSam Leffler  */
456b032f27cSSam Leffler static int
457b032f27cSSam Leffler default_reset(struct ieee80211vap *vap, u_long cmd)
458b032f27cSSam Leffler {
459b032f27cSSam Leffler 	return ENETRESET;
460b032f27cSSam Leffler }
461b032f27cSSam Leffler 
462b032f27cSSam Leffler /*
463781487cfSAdrian Chadd  * Default for updating the VAP default TX key index.
464781487cfSAdrian Chadd  *
465781487cfSAdrian Chadd  * Drivers that support TX offload as well as hardware encryption offload
466781487cfSAdrian Chadd  * may need to be informed of key index changes separate from the key
467781487cfSAdrian Chadd  * update.
468781487cfSAdrian Chadd  */
469781487cfSAdrian Chadd static void
470781487cfSAdrian Chadd default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
471781487cfSAdrian Chadd {
472781487cfSAdrian Chadd 
473781487cfSAdrian Chadd 	/* XXX assert validity */
474781487cfSAdrian Chadd 	/* XXX assert we're in a key update block */
475781487cfSAdrian Chadd 	vap->iv_def_txkey = kid;
476781487cfSAdrian Chadd }
477781487cfSAdrian Chadd 
478781487cfSAdrian Chadd /*
47928da1b56SGleb Smirnoff  * Add underlying device errors to vap errors.
48028da1b56SGleb Smirnoff  */
48128da1b56SGleb Smirnoff static uint64_t
48228da1b56SGleb Smirnoff ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
48328da1b56SGleb Smirnoff {
48428da1b56SGleb Smirnoff 	struct ieee80211vap *vap = ifp->if_softc;
48528da1b56SGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
48628da1b56SGleb Smirnoff 	uint64_t rv;
48728da1b56SGleb Smirnoff 
48828da1b56SGleb Smirnoff 	rv = if_get_counter_default(ifp, cnt);
48928da1b56SGleb Smirnoff 	switch (cnt) {
49028da1b56SGleb Smirnoff 	case IFCOUNTER_OERRORS:
49128da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_oerrors);
49228da1b56SGleb Smirnoff 		break;
49328da1b56SGleb Smirnoff 	case IFCOUNTER_IERRORS:
49428da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_ierrors);
49528da1b56SGleb Smirnoff 		break;
49628da1b56SGleb Smirnoff 	default:
49728da1b56SGleb Smirnoff 		break;
49828da1b56SGleb Smirnoff 	}
49928da1b56SGleb Smirnoff 
50028da1b56SGleb Smirnoff 	return (rv);
50128da1b56SGleb Smirnoff }
50228da1b56SGleb Smirnoff 
50328da1b56SGleb Smirnoff /*
504b032f27cSSam Leffler  * Prepare a vap for use.  Drivers use this call to
505b032f27cSSam Leffler  * setup net80211 state in new vap's prior attaching
506b032f27cSSam Leffler  * them with ieee80211_vap_attach (below).
507b032f27cSSam Leffler  */
508b032f27cSSam Leffler int
509b032f27cSSam Leffler ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
510fcd9500fSBernhard Schmidt     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
5117a79cebfSGleb Smirnoff     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
512b032f27cSSam Leffler {
513b032f27cSSam Leffler 	struct ifnet *ifp;
514b032f27cSSam Leffler 
515b032f27cSSam Leffler 	ifp = if_alloc(IFT_ETHER);
516b032f27cSSam Leffler 	if (ifp == NULL) {
517c8f5794eSGleb Smirnoff 		ic_printf(ic, "%s: unable to allocate ifnet\n",
518b032f27cSSam Leffler 		    __func__);
519b032f27cSSam Leffler 		return ENOMEM;
520b032f27cSSam Leffler 	}
521b032f27cSSam Leffler 	if_initname(ifp, name, unit);
522b032f27cSSam Leffler 	ifp->if_softc = vap;			/* back pointer */
523b032f27cSSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
524e7495198SAdrian Chadd 	ifp->if_transmit = ieee80211_vap_transmit;
525e7495198SAdrian Chadd 	ifp->if_qflush = ieee80211_vap_qflush;
526b032f27cSSam Leffler 	ifp->if_ioctl = ieee80211_ioctl;
527b032f27cSSam Leffler 	ifp->if_init = ieee80211_init;
52828da1b56SGleb Smirnoff 	ifp->if_get_counter = ieee80211_get_counter;
529b032f27cSSam Leffler 
530b032f27cSSam Leffler 	vap->iv_ifp = ifp;
531b032f27cSSam Leffler 	vap->iv_ic = ic;
532b032f27cSSam Leffler 	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
533b032f27cSSam Leffler 	vap->iv_flags_ext = ic->ic_flags_ext;
534b032f27cSSam Leffler 	vap->iv_flags_ven = ic->ic_flags_ven;
535b032f27cSSam Leffler 	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
536*67f4aa38SAdrian Chadd 
537*67f4aa38SAdrian Chadd 	/* 11n capabilities - XXX methodize */
538b032f27cSSam Leffler 	vap->iv_htcaps = ic->ic_htcaps;
539e1d36f83SRui Paulo 	vap->iv_htextcaps = ic->ic_htextcaps;
540*67f4aa38SAdrian Chadd 
541*67f4aa38SAdrian Chadd 	/* 11ac capabilities - XXX methodize */
542*67f4aa38SAdrian Chadd 	vap->iv_vhtcaps = ic->ic_vhtcaps;
543*67f4aa38SAdrian Chadd 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
544*67f4aa38SAdrian Chadd 
545b032f27cSSam Leffler 	vap->iv_opmode = opmode;
546c43feedeSSam Leffler 	vap->iv_caps |= ieee80211_opcap[opmode];
5471d47c76cSAndriy Voskoboinyk 	IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
548b032f27cSSam Leffler 	switch (opmode) {
549b032f27cSSam Leffler 	case IEEE80211_M_WDS:
550b032f27cSSam Leffler 		/*
551b032f27cSSam Leffler 		 * WDS links must specify the bssid of the far end.
552b032f27cSSam Leffler 		 * For legacy operation this is a static relationship.
553b032f27cSSam Leffler 		 * For non-legacy operation the station must associate
554b032f27cSSam Leffler 		 * and be authorized to pass traffic.  Plumbing the
555b032f27cSSam Leffler 		 * vap to the proper node happens when the vap
556b032f27cSSam Leffler 		 * transitions to RUN state.
557b032f27cSSam Leffler 		 */
558b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
559b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_DESBSSID;
560b032f27cSSam Leffler 		if (flags & IEEE80211_CLONE_WDSLEGACY)
561b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
562b032f27cSSam Leffler 		break;
56310ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
56410ad9a77SSam Leffler 	case IEEE80211_M_AHDEMO:
56510ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
56610ad9a77SSam Leffler 			/* NB: checked before clone operation allowed */
56710ad9a77SSam Leffler 			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
56810ad9a77SSam Leffler 			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
56910ad9a77SSam Leffler 			/*
57010ad9a77SSam Leffler 			 * Propagate TDMA capability to mark vap; this
57110ad9a77SSam Leffler 			 * cannot be removed and is used to distinguish
57210ad9a77SSam Leffler 			 * regular ahdemo operation from ahdemo+tdma.
57310ad9a77SSam Leffler 			 */
57410ad9a77SSam Leffler 			vap->iv_caps |= IEEE80211_C_TDMA;
57510ad9a77SSam Leffler 		}
57610ad9a77SSam Leffler 		break;
57710ad9a77SSam Leffler #endif
578fcd9500fSBernhard Schmidt 	default:
579fcd9500fSBernhard Schmidt 		break;
580b032f27cSSam Leffler 	}
581ae3f00bbSSam Leffler 	/* auto-enable s/w beacon miss support */
582ae3f00bbSSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS)
583ae3f00bbSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
58483fcb812SAndrew Thompson 	/* auto-generated or user supplied MAC address */
58583fcb812SAndrew Thompson 	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
58683fcb812SAndrew Thompson 		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
587b032f27cSSam Leffler 	/*
588b032f27cSSam Leffler 	 * Enable various functionality by default if we're
589b032f27cSSam Leffler 	 * capable; the driver can override us if it knows better.
590b032f27cSSam Leffler 	 */
591b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_WME)
592b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_WME;
593b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_BURST)
594b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BURST;
595b032f27cSSam Leffler 	/* NB: bg scanning only makes sense for station mode right now */
596b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA &&
597b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_BGSCAN))
598b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BGSCAN;
599c43feedeSSam Leffler 	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
60082fd2577SSam Leffler 	/* NB: DFS support only makes sense for ap mode right now */
60182fd2577SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
60282fd2577SSam Leffler 	    (vap->iv_caps & IEEE80211_C_DFS))
603b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
604b032f27cSSam Leffler 
605b032f27cSSam Leffler 	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
606b032f27cSSam Leffler 	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
607b032f27cSSam Leffler 	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
608b032f27cSSam Leffler 	/*
609b032f27cSSam Leffler 	 * Install a default reset method for the ioctl support;
610b032f27cSSam Leffler 	 * the driver can override this.
611b032f27cSSam Leffler 	 */
612b032f27cSSam Leffler 	vap->iv_reset = default_reset;
613b032f27cSSam Leffler 
614781487cfSAdrian Chadd 	/*
615781487cfSAdrian Chadd 	 * Install a default crypto key update method, the driver
616781487cfSAdrian Chadd 	 * can override this.
617781487cfSAdrian Chadd 	 */
618781487cfSAdrian Chadd 	vap->iv_update_deftxkey = default_update_deftxkey;
619781487cfSAdrian Chadd 
620b032f27cSSam Leffler 	ieee80211_sysctl_vattach(vap);
621b032f27cSSam Leffler 	ieee80211_crypto_vattach(vap);
622b032f27cSSam Leffler 	ieee80211_node_vattach(vap);
623b032f27cSSam Leffler 	ieee80211_power_vattach(vap);
624b032f27cSSam Leffler 	ieee80211_proto_vattach(vap);
625616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
626616190d0SSam Leffler 	ieee80211_superg_vattach(vap);
627616190d0SSam Leffler #endif
628b032f27cSSam Leffler 	ieee80211_ht_vattach(vap);
629*67f4aa38SAdrian Chadd 	ieee80211_vht_vattach(vap);
630b032f27cSSam Leffler 	ieee80211_scan_vattach(vap);
631b032f27cSSam Leffler 	ieee80211_regdomain_vattach(vap);
6325463c4a4SSam Leffler 	ieee80211_radiotap_vattach(vap);
633a7c6aabdSBernhard Schmidt 	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
634b6108616SRui Paulo 
635b032f27cSSam Leffler 	return 0;
636b032f27cSSam Leffler }
637b032f27cSSam Leffler 
638b032f27cSSam Leffler /*
639b032f27cSSam Leffler  * Activate a vap.  State should have been prepared with a
640b032f27cSSam Leffler  * call to ieee80211_vap_setup and by the driver.  On return
641b032f27cSSam Leffler  * from this call the vap is ready for use.
642b032f27cSSam Leffler  */
643b032f27cSSam Leffler int
6447a79cebfSGleb Smirnoff ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
6457a79cebfSGleb Smirnoff     ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
646b032f27cSSam Leffler {
647b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
648b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
649b032f27cSSam Leffler 	struct ifmediareq imr;
650b032f27cSSam Leffler 	int maxrate;
651b032f27cSSam Leffler 
652b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
653b032f27cSSam Leffler 	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
654b032f27cSSam Leffler 	    __func__, ieee80211_opmode_name[vap->iv_opmode],
655c8f5794eSGleb Smirnoff 	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
656b032f27cSSam Leffler 
657b032f27cSSam Leffler 	/*
658b032f27cSSam Leffler 	 * Do late attach work that cannot happen until after
659b032f27cSSam Leffler 	 * the driver has had a chance to override defaults.
660b032f27cSSam Leffler 	 */
661b032f27cSSam Leffler 	ieee80211_node_latevattach(vap);
662b032f27cSSam Leffler 	ieee80211_power_latevattach(vap);
663b032f27cSSam Leffler 
664b032f27cSSam Leffler 	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
665b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
666b032f27cSSam Leffler 	ieee80211_media_status(ifp, &imr);
667b032f27cSSam Leffler 	/* NB: strip explicit mode; we're actually in autoselect */
668c3f10abdSSam Leffler 	ifmedia_set(&vap->iv_media,
669c3f10abdSSam Leffler 	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
670b032f27cSSam Leffler 	if (maxrate)
671b032f27cSSam Leffler 		ifp->if_baudrate = IF_Mbps(maxrate);
672b032f27cSSam Leffler 
6737a79cebfSGleb Smirnoff 	ether_ifattach(ifp, macaddr);
6741d47c76cSAndriy Voskoboinyk 	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
675b032f27cSSam Leffler 	/* hook output method setup by ether_ifattach */
676b032f27cSSam Leffler 	vap->iv_output = ifp->if_output;
677b032f27cSSam Leffler 	ifp->if_output = ieee80211_output;
678b032f27cSSam Leffler 	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
679b032f27cSSam Leffler 
680b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
681b032f27cSSam Leffler 	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
682b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
683616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
684b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
685616190d0SSam Leffler #endif
686b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
687b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
6882bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
6892bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
6908e71a4aaSAdrian Chadd 
6918e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
6928e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
6938e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
6948e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
6958e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
696b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
697b032f27cSSam Leffler 
698b032f27cSSam Leffler 	return 1;
699b032f27cSSam Leffler }
700b032f27cSSam Leffler 
701b032f27cSSam Leffler /*
702b032f27cSSam Leffler  * Tear down vap state and reclaim the ifnet.
703b032f27cSSam Leffler  * The driver is assumed to have prepared for
704b032f27cSSam Leffler  * this; e.g. by turning off interrupts for the
705b032f27cSSam Leffler  * underlying device.
706b032f27cSSam Leffler  */
707b032f27cSSam Leffler void
708b032f27cSSam Leffler ieee80211_vap_detach(struct ieee80211vap *vap)
709b032f27cSSam Leffler {
710b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
711b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
712b032f27cSSam Leffler 
71330e4856aSAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
71430e4856aSAdrian Chadd 
715b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
7167fc10b6bSGleb Smirnoff 	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
717b032f27cSSam Leffler 
7181da89db5SSam Leffler 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
7191da89db5SSam Leffler 	ether_ifdetach(ifp);
7201da89db5SSam Leffler 
7211da89db5SSam Leffler 	ieee80211_stop(vap);
722b032f27cSSam Leffler 
7235efea30fSAndrew Thompson 	/*
7245efea30fSAndrew Thompson 	 * Flush any deferred vap tasks.
7255efea30fSAndrew Thompson 	 */
7265efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_nstate_task);
7275efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
7285efea30fSAndrew Thompson 
729ab501dd6SSam Leffler 	/* XXX band-aid until ifnet handles this for us */
730ab501dd6SSam Leffler 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
731ab501dd6SSam Leffler 
7325efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
7335efea30fSAndrew Thompson 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
734b032f27cSSam Leffler 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
735b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
736616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
737b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
738616190d0SSam Leffler #endif
739b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
740b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
7412bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
7422bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
7438e71a4aaSAdrian Chadd 
7448e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
7458e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
7468e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
7478e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
7488e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
7498e71a4aaSAdrian Chadd 
7505463c4a4SSam Leffler 	/* NB: this handles the bpfdetach done below */
7515463c4a4SSam Leffler 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
7527a79cebfSGleb Smirnoff 	if (vap->iv_ifflags & IFF_PROMISC)
7537a79cebfSGleb Smirnoff 		ieee80211_promisc(vap, false);
7547a79cebfSGleb Smirnoff 	if (vap->iv_ifflags & IFF_ALLMULTI)
7557a79cebfSGleb Smirnoff 		ieee80211_allmulti(vap, false);
756b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
757b032f27cSSam Leffler 
758b032f27cSSam Leffler 	ifmedia_removeall(&vap->iv_media);
759b032f27cSSam Leffler 
7605463c4a4SSam Leffler 	ieee80211_radiotap_vdetach(vap);
761b032f27cSSam Leffler 	ieee80211_regdomain_vdetach(vap);
762b032f27cSSam Leffler 	ieee80211_scan_vdetach(vap);
763616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
764616190d0SSam Leffler 	ieee80211_superg_vdetach(vap);
765616190d0SSam Leffler #endif
766*67f4aa38SAdrian Chadd 	ieee80211_vht_vdetach(vap);
767b032f27cSSam Leffler 	ieee80211_ht_vdetach(vap);
768b032f27cSSam Leffler 	/* NB: must be before ieee80211_node_vdetach */
769b032f27cSSam Leffler 	ieee80211_proto_vdetach(vap);
770b032f27cSSam Leffler 	ieee80211_crypto_vdetach(vap);
771b032f27cSSam Leffler 	ieee80211_power_vdetach(vap);
772b032f27cSSam Leffler 	ieee80211_node_vdetach(vap);
773b032f27cSSam Leffler 	ieee80211_sysctl_vdetach(vap);
774b20f0ed1SWeongyo Jeong 
775b20f0ed1SWeongyo Jeong 	if_free(ifp);
77630e4856aSAdrian Chadd 
77730e4856aSAdrian Chadd 	CURVNET_RESTORE();
778b032f27cSSam Leffler }
779b032f27cSSam Leffler 
780b032f27cSSam Leffler /*
7817a79cebfSGleb Smirnoff  * Count number of vaps in promisc, and issue promisc on
7827a79cebfSGleb Smirnoff  * parent respectively.
783b032f27cSSam Leffler  */
784b032f27cSSam Leffler void
7857a79cebfSGleb Smirnoff ieee80211_promisc(struct ieee80211vap *vap, bool on)
786b032f27cSSam Leffler {
7877a79cebfSGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
788b032f27cSSam Leffler 
789c6427be9SAndriy Voskoboinyk 	IEEE80211_LOCK_ASSERT(ic);
790c6427be9SAndriy Voskoboinyk 
7917a79cebfSGleb Smirnoff 	if (on) {
7927a79cebfSGleb Smirnoff 		if (++ic->ic_promisc == 1)
793ba2c1fbcSAdrian Chadd 			ieee80211_runtask(ic, &ic->ic_promisc_task);
7947a79cebfSGleb Smirnoff 	} else {
7957a79cebfSGleb Smirnoff 		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
7967a79cebfSGleb Smirnoff 		    __func__, ic));
7977a79cebfSGleb Smirnoff 		if (--ic->ic_promisc == 0)
7987a79cebfSGleb Smirnoff 			ieee80211_runtask(ic, &ic->ic_promisc_task);
7997a79cebfSGleb Smirnoff 	}
8007a79cebfSGleb Smirnoff }
8017a79cebfSGleb Smirnoff 
8027a79cebfSGleb Smirnoff /*
8037a79cebfSGleb Smirnoff  * Count number of vaps in allmulti, and issue allmulti on
8047a79cebfSGleb Smirnoff  * parent respectively.
8057a79cebfSGleb Smirnoff  */
8067a79cebfSGleb Smirnoff void
8077a79cebfSGleb Smirnoff ieee80211_allmulti(struct ieee80211vap *vap, bool on)
8087a79cebfSGleb Smirnoff {
8097a79cebfSGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
8107a79cebfSGleb Smirnoff 
811c6427be9SAndriy Voskoboinyk 	IEEE80211_LOCK_ASSERT(ic);
812c6427be9SAndriy Voskoboinyk 
8137a79cebfSGleb Smirnoff 	if (on) {
8147a79cebfSGleb Smirnoff 		if (++ic->ic_allmulti == 1)
8157a79cebfSGleb Smirnoff 			ieee80211_runtask(ic, &ic->ic_mcast_task);
8167a79cebfSGleb Smirnoff 	} else {
8177a79cebfSGleb Smirnoff 		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
8187a79cebfSGleb Smirnoff 		    __func__, ic));
8197a79cebfSGleb Smirnoff 		if (--ic->ic_allmulti == 0)
8205efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_mcast_task);
821b032f27cSSam Leffler 	}
822b032f27cSSam Leffler }
823b032f27cSSam Leffler 
824b032f27cSSam Leffler /*
825b032f27cSSam Leffler  * Synchronize flag bit state in the com structure
826b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
827b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
828b032f27cSSam Leffler  */
829b032f27cSSam Leffler static void
830b032f27cSSam Leffler ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
831b032f27cSSam Leffler {
832b032f27cSSam Leffler 	struct ieee80211vap *vap;
833b032f27cSSam Leffler 	int bit;
834b032f27cSSam Leffler 
835b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
836b032f27cSSam Leffler 
837b032f27cSSam Leffler 	bit = 0;
838b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
839b032f27cSSam Leffler 		if (vap->iv_flags & flag) {
840b032f27cSSam Leffler 			bit = 1;
841b032f27cSSam Leffler 			break;
842b032f27cSSam Leffler 		}
843b032f27cSSam Leffler 	if (bit)
844b032f27cSSam Leffler 		ic->ic_flags |= flag;
845b032f27cSSam Leffler 	else
846b032f27cSSam Leffler 		ic->ic_flags &= ~flag;
847b032f27cSSam Leffler }
848b032f27cSSam Leffler 
849b032f27cSSam Leffler void
850b032f27cSSam Leffler ieee80211_syncflag(struct ieee80211vap *vap, int flag)
851b032f27cSSam Leffler {
852b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
853b032f27cSSam Leffler 
854b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
855b032f27cSSam Leffler 	if (flag < 0) {
856b032f27cSSam Leffler 		flag = -flag;
857b032f27cSSam Leffler 		vap->iv_flags &= ~flag;
858b032f27cSSam Leffler 	} else
859b032f27cSSam Leffler 		vap->iv_flags |= flag;
860b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, flag);
861b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
862b032f27cSSam Leffler }
863b032f27cSSam Leffler 
864b032f27cSSam Leffler /*
8652bfc8a91SSam Leffler  * Synchronize flags_ht bit state in the com structure
8662bfc8a91SSam Leffler  * according to the state of all vap's.  This is used,
8672bfc8a91SSam Leffler  * for example, to handle state changes via ioctls.
8682bfc8a91SSam Leffler  */
8692bfc8a91SSam Leffler static void
8702bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
8712bfc8a91SSam Leffler {
8722bfc8a91SSam Leffler 	struct ieee80211vap *vap;
8732bfc8a91SSam Leffler 	int bit;
8742bfc8a91SSam Leffler 
8752bfc8a91SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
8762bfc8a91SSam Leffler 
8772bfc8a91SSam Leffler 	bit = 0;
8782bfc8a91SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8792bfc8a91SSam Leffler 		if (vap->iv_flags_ht & flag) {
8802bfc8a91SSam Leffler 			bit = 1;
8812bfc8a91SSam Leffler 			break;
8822bfc8a91SSam Leffler 		}
8832bfc8a91SSam Leffler 	if (bit)
8842bfc8a91SSam Leffler 		ic->ic_flags_ht |= flag;
8852bfc8a91SSam Leffler 	else
8862bfc8a91SSam Leffler 		ic->ic_flags_ht &= ~flag;
8872bfc8a91SSam Leffler }
8882bfc8a91SSam Leffler 
8892bfc8a91SSam Leffler void
8902bfc8a91SSam Leffler ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
8912bfc8a91SSam Leffler {
8922bfc8a91SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
8932bfc8a91SSam Leffler 
8942bfc8a91SSam Leffler 	IEEE80211_LOCK(ic);
8952bfc8a91SSam Leffler 	if (flag < 0) {
8962bfc8a91SSam Leffler 		flag = -flag;
8972bfc8a91SSam Leffler 		vap->iv_flags_ht &= ~flag;
8982bfc8a91SSam Leffler 	} else
8992bfc8a91SSam Leffler 		vap->iv_flags_ht |= flag;
9002bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, flag);
9012bfc8a91SSam Leffler 	IEEE80211_UNLOCK(ic);
9022bfc8a91SSam Leffler }
9032bfc8a91SSam Leffler 
9042bfc8a91SSam Leffler /*
9058e71a4aaSAdrian Chadd  * Synchronize flags_vht bit state in the com structure
9068e71a4aaSAdrian Chadd  * according to the state of all vap's.  This is used,
9078e71a4aaSAdrian Chadd  * for example, to handle state changes via ioctls.
9088e71a4aaSAdrian Chadd  */
9098e71a4aaSAdrian Chadd static void
9108e71a4aaSAdrian Chadd ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
9118e71a4aaSAdrian Chadd {
9128e71a4aaSAdrian Chadd 	struct ieee80211vap *vap;
9138e71a4aaSAdrian Chadd 	int bit;
9148e71a4aaSAdrian Chadd 
9158e71a4aaSAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
9168e71a4aaSAdrian Chadd 
9178e71a4aaSAdrian Chadd 	bit = 0;
9188e71a4aaSAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
9198e71a4aaSAdrian Chadd 		if (vap->iv_flags_vht & flag) {
9208e71a4aaSAdrian Chadd 			bit = 1;
9218e71a4aaSAdrian Chadd 			break;
9228e71a4aaSAdrian Chadd 		}
9238e71a4aaSAdrian Chadd 	if (bit)
9248e71a4aaSAdrian Chadd 		ic->ic_flags_vht |= flag;
9258e71a4aaSAdrian Chadd 	else
9268e71a4aaSAdrian Chadd 		ic->ic_flags_vht &= ~flag;
9278e71a4aaSAdrian Chadd }
9288e71a4aaSAdrian Chadd 
9298e71a4aaSAdrian Chadd void
9308e71a4aaSAdrian Chadd ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
9318e71a4aaSAdrian Chadd {
9328e71a4aaSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
9338e71a4aaSAdrian Chadd 
9348e71a4aaSAdrian Chadd 	IEEE80211_LOCK(ic);
9358e71a4aaSAdrian Chadd 	if (flag < 0) {
9368e71a4aaSAdrian Chadd 		flag = -flag;
9378e71a4aaSAdrian Chadd 		vap->iv_flags_vht &= ~flag;
9388e71a4aaSAdrian Chadd 	} else
9398e71a4aaSAdrian Chadd 		vap->iv_flags_vht |= flag;
9408e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, flag);
9418e71a4aaSAdrian Chadd 	IEEE80211_UNLOCK(ic);
9428e71a4aaSAdrian Chadd }
9438e71a4aaSAdrian Chadd 
9448e71a4aaSAdrian Chadd /*
9452bfc8a91SSam Leffler  * Synchronize flags_ext bit state in the com structure
946b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
947b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
948b032f27cSSam Leffler  */
949b032f27cSSam Leffler static void
950b032f27cSSam Leffler ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
951b032f27cSSam Leffler {
952b032f27cSSam Leffler 	struct ieee80211vap *vap;
953b032f27cSSam Leffler 	int bit;
954b032f27cSSam Leffler 
955b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
956b032f27cSSam Leffler 
957b032f27cSSam Leffler 	bit = 0;
958b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
959b032f27cSSam Leffler 		if (vap->iv_flags_ext & flag) {
960b032f27cSSam Leffler 			bit = 1;
961b032f27cSSam Leffler 			break;
962b032f27cSSam Leffler 		}
963b032f27cSSam Leffler 	if (bit)
964b032f27cSSam Leffler 		ic->ic_flags_ext |= flag;
965b032f27cSSam Leffler 	else
966b032f27cSSam Leffler 		ic->ic_flags_ext &= ~flag;
967b032f27cSSam Leffler }
968b032f27cSSam Leffler 
969b032f27cSSam Leffler void
970b032f27cSSam Leffler ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
971b032f27cSSam Leffler {
972b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
973b032f27cSSam Leffler 
974b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
975b032f27cSSam Leffler 	if (flag < 0) {
976b032f27cSSam Leffler 		flag = -flag;
977b032f27cSSam Leffler 		vap->iv_flags_ext &= ~flag;
978b032f27cSSam Leffler 	} else
979b032f27cSSam Leffler 		vap->iv_flags_ext |= flag;
980b032f27cSSam Leffler 	ieee80211_syncflag_ext_locked(ic, flag);
981b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
9821a1e1d21SSam Leffler }
9831a1e1d21SSam Leffler 
984ca4ac7aeSSam Leffler static __inline int
985ca4ac7aeSSam Leffler mapgsm(u_int freq, u_int flags)
986ca4ac7aeSSam Leffler {
987ca4ac7aeSSam Leffler 	freq *= 10;
988ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_QUARTER)
989ca4ac7aeSSam Leffler 		freq += 5;
990ca4ac7aeSSam Leffler 	else if (flags & IEEE80211_CHAN_HALF)
991ca4ac7aeSSam Leffler 		freq += 10;
992ca4ac7aeSSam Leffler 	else
993ca4ac7aeSSam Leffler 		freq += 20;
994ca4ac7aeSSam Leffler 	/* NB: there is no 907/20 wide but leave room */
995ca4ac7aeSSam Leffler 	return (freq - 906*10) / 5;
996ca4ac7aeSSam Leffler }
997ca4ac7aeSSam Leffler 
998ca4ac7aeSSam Leffler static __inline int
999ca4ac7aeSSam Leffler mappsb(u_int freq, u_int flags)
1000ca4ac7aeSSam Leffler {
1001ca4ac7aeSSam Leffler 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1002ca4ac7aeSSam Leffler }
1003ca4ac7aeSSam Leffler 
10041a1e1d21SSam Leffler /*
10051a1e1d21SSam Leffler  * Convert MHz frequency to IEEE channel number.
10061a1e1d21SSam Leffler  */
10076f322b78SSam Leffler int
10081a1e1d21SSam Leffler ieee80211_mhz2ieee(u_int freq, u_int flags)
10091a1e1d21SSam Leffler {
101011df4239SSam Leffler #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1011ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
1012ca4ac7aeSSam Leffler 		return mapgsm(freq, flags);
10131a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
10141a1e1d21SSam Leffler 		if (freq == 2484)
10151a1e1d21SSam Leffler 			return 14;
10161a1e1d21SSam Leffler 		if (freq < 2484)
10176f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
10181a1e1d21SSam Leffler 		else
10191a1e1d21SSam Leffler 			return 15 + ((freq - 2512) / 20);
1020c032abb5SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
102141b3c790SSam Leffler 		if (freq <= 5000) {
102268e8e04eSSam Leffler 			/* XXX check regdomain? */
102311df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
1024ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
10256f322b78SSam Leffler 			return (freq - 4000) / 5;
102641b3c790SSam Leffler 		} else
10271a1e1d21SSam Leffler 			return (freq - 5000) / 5;
10281a1e1d21SSam Leffler 	} else {				/* either, guess */
10291a1e1d21SSam Leffler 		if (freq == 2484)
10301a1e1d21SSam Leffler 			return 14;
1031ca4ac7aeSSam Leffler 		if (freq < 2484) {
1032ca4ac7aeSSam Leffler 			if (907 <= freq && freq <= 922)
1033ca4ac7aeSSam Leffler 				return mapgsm(freq, flags);
10346f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
1035ca4ac7aeSSam Leffler 		}
10366f322b78SSam Leffler 		if (freq < 5000) {
103711df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
1038ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
103941b3c790SSam Leffler 			else if (freq > 4900)
10406f322b78SSam Leffler 				return (freq - 4000) / 5;
10416f322b78SSam Leffler 			else
10421a1e1d21SSam Leffler 				return 15 + ((freq - 2512) / 20);
10436f322b78SSam Leffler 		}
10441a1e1d21SSam Leffler 		return (freq - 5000) / 5;
10451a1e1d21SSam Leffler 	}
104611df4239SSam Leffler #undef IS_FREQ_IN_PSB
10471a1e1d21SSam Leffler }
10481a1e1d21SSam Leffler 
10491a1e1d21SSam Leffler /*
10501a1e1d21SSam Leffler  * Convert channel to IEEE channel number.
10511a1e1d21SSam Leffler  */
10526f322b78SSam Leffler int
105338da1496SMatt Jacob ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
10541a1e1d21SSam Leffler {
105568e8e04eSSam Leffler 	if (c == NULL) {
1056c8f5794eSGleb Smirnoff 		ic_printf(ic, "invalid channel (NULL)\n");
10578be0d570SSam Leffler 		return 0;		/* XXX */
10581a1e1d21SSam Leffler 	}
105968e8e04eSSam Leffler 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
10601a1e1d21SSam Leffler }
10611a1e1d21SSam Leffler 
10621a1e1d21SSam Leffler /*
10631a1e1d21SSam Leffler  * Convert IEEE channel number to MHz frequency.
10641a1e1d21SSam Leffler  */
10651a1e1d21SSam Leffler u_int
10661a1e1d21SSam Leffler ieee80211_ieee2mhz(u_int chan, u_int flags)
10671a1e1d21SSam Leffler {
1068ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
1069ca4ac7aeSSam Leffler 		return 907 + 5 * (chan / 10);
10701a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
10711a1e1d21SSam Leffler 		if (chan == 14)
10721a1e1d21SSam Leffler 			return 2484;
10731a1e1d21SSam Leffler 		if (chan < 14)
10741a1e1d21SSam Leffler 			return 2407 + chan*5;
10751a1e1d21SSam Leffler 		else
10761a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
10771a1e1d21SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
107841b3c790SSam Leffler 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
107941b3c790SSam Leffler 			chan -= 37;
108041b3c790SSam Leffler 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
108141b3c790SSam Leffler 		}
10821a1e1d21SSam Leffler 		return 5000 + (chan*5);
10831a1e1d21SSam Leffler 	} else {				/* either, guess */
1084ca4ac7aeSSam Leffler 		/* XXX can't distinguish PSB+GSM channels */
10851a1e1d21SSam Leffler 		if (chan == 14)
10861a1e1d21SSam Leffler 			return 2484;
10871a1e1d21SSam Leffler 		if (chan < 14)			/* 0-13 */
10881a1e1d21SSam Leffler 			return 2407 + chan*5;
10891a1e1d21SSam Leffler 		if (chan < 27)			/* 15-26 */
10901a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
10911a1e1d21SSam Leffler 		return 5000 + (chan*5);
10921a1e1d21SSam Leffler 	}
10931a1e1d21SSam Leffler }
10941a1e1d21SSam Leffler 
1095355fec48SAndriy Voskoboinyk static __inline void
1096355fec48SAndriy Voskoboinyk set_extchan(struct ieee80211_channel *c)
1097355fec48SAndriy Voskoboinyk {
1098355fec48SAndriy Voskoboinyk 
1099355fec48SAndriy Voskoboinyk 	/*
1100355fec48SAndriy Voskoboinyk 	 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1101355fec48SAndriy Voskoboinyk 	 * "the secondary channel number shall be 'N + [1,-1] * 4'
1102355fec48SAndriy Voskoboinyk 	 */
1103355fec48SAndriy Voskoboinyk 	if (c->ic_flags & IEEE80211_CHAN_HT40U)
1104355fec48SAndriy Voskoboinyk 		c->ic_extieee = c->ic_ieee + 4;
1105355fec48SAndriy Voskoboinyk 	else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1106355fec48SAndriy Voskoboinyk 		c->ic_extieee = c->ic_ieee - 4;
1107355fec48SAndriy Voskoboinyk 	else
1108355fec48SAndriy Voskoboinyk 		c->ic_extieee = 0;
1109355fec48SAndriy Voskoboinyk }
1110355fec48SAndriy Voskoboinyk 
1111*67f4aa38SAdrian Chadd /*
1112*67f4aa38SAdrian Chadd  * Populate the freq1/freq2 fields as appropriate for VHT channels.
1113*67f4aa38SAdrian Chadd  *
1114*67f4aa38SAdrian Chadd  * This for now uses a hard-coded list of 80MHz wide channels.
1115*67f4aa38SAdrian Chadd  *
1116*67f4aa38SAdrian Chadd  * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
1117*67f4aa38SAdrian Chadd  * wide channel we've already decided upon.
1118*67f4aa38SAdrian Chadd  *
1119*67f4aa38SAdrian Chadd  * For VHT80 and VHT160, there are only a small number of fixed
1120*67f4aa38SAdrian Chadd  * 80/160MHz wide channels, so we just use those.
1121*67f4aa38SAdrian Chadd  *
1122*67f4aa38SAdrian Chadd  * This is all likely very very wrong - both the regulatory code
1123*67f4aa38SAdrian Chadd  * and this code needs to ensure that all four channels are
1124*67f4aa38SAdrian Chadd  * available and valid before the VHT80 (and eight for VHT160) channel
1125*67f4aa38SAdrian Chadd  * is created.
1126*67f4aa38SAdrian Chadd  */
1127*67f4aa38SAdrian Chadd 
1128*67f4aa38SAdrian Chadd struct vht_chan_range {
1129*67f4aa38SAdrian Chadd 	uint16_t freq_start;
1130*67f4aa38SAdrian Chadd 	uint16_t freq_end;
1131*67f4aa38SAdrian Chadd };
1132*67f4aa38SAdrian Chadd 
1133*67f4aa38SAdrian Chadd struct vht_chan_range vht80_chan_ranges[] = {
1134*67f4aa38SAdrian Chadd 	{ 5170, 5250 },
1135*67f4aa38SAdrian Chadd 	{ 5250, 5330 },
1136*67f4aa38SAdrian Chadd 	{ 5490, 5570 },
1137*67f4aa38SAdrian Chadd 	{ 5570, 5650 },
1138*67f4aa38SAdrian Chadd 	{ 5650, 5730 },
1139*67f4aa38SAdrian Chadd 	{ 5735, 5815 },
1140*67f4aa38SAdrian Chadd 	{ 0, 0, }
1141*67f4aa38SAdrian Chadd };
1142*67f4aa38SAdrian Chadd 
1143*67f4aa38SAdrian Chadd static int
1144*67f4aa38SAdrian Chadd set_vht_extchan(struct ieee80211_channel *c)
1145*67f4aa38SAdrian Chadd {
1146*67f4aa38SAdrian Chadd 	int i;
1147*67f4aa38SAdrian Chadd 
1148*67f4aa38SAdrian Chadd 	if (! IEEE80211_IS_CHAN_VHT(c)) {
1149*67f4aa38SAdrian Chadd 		return (0);
1150*67f4aa38SAdrian Chadd 	}
1151*67f4aa38SAdrian Chadd 
1152*67f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT20(c)) {
1153*67f4aa38SAdrian Chadd 		c->ic_vht_ch_freq1 = c->ic_ieee;
1154*67f4aa38SAdrian Chadd 		return (1);
1155*67f4aa38SAdrian Chadd 	}
1156*67f4aa38SAdrian Chadd 
1157*67f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT40(c)) {
1158*67f4aa38SAdrian Chadd 		if (IEEE80211_IS_CHAN_HT40U(c))
1159*67f4aa38SAdrian Chadd 			c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1160*67f4aa38SAdrian Chadd 		else if (IEEE80211_IS_CHAN_HT40D(c))
1161*67f4aa38SAdrian Chadd 			c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1162*67f4aa38SAdrian Chadd 		else
1163*67f4aa38SAdrian Chadd 			return (0);
1164*67f4aa38SAdrian Chadd 		return (1);
1165*67f4aa38SAdrian Chadd 	}
1166*67f4aa38SAdrian Chadd 
1167*67f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT80(c)) {
1168*67f4aa38SAdrian Chadd 		for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1169*67f4aa38SAdrian Chadd 			if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
1170*67f4aa38SAdrian Chadd 			    c->ic_freq < vht80_chan_ranges[i].freq_end) {
1171*67f4aa38SAdrian Chadd 				int midpoint;
1172*67f4aa38SAdrian Chadd 
1173*67f4aa38SAdrian Chadd 				midpoint = vht80_chan_ranges[i].freq_start + 40;
1174*67f4aa38SAdrian Chadd 				c->ic_vht_ch_freq1 =
1175*67f4aa38SAdrian Chadd 				    ieee80211_mhz2ieee(midpoint, c->ic_flags);
1176*67f4aa38SAdrian Chadd 				c->ic_vht_ch_freq2 = 0;
1177*67f4aa38SAdrian Chadd #if 0
1178*67f4aa38SAdrian Chadd 				printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1179*67f4aa38SAdrian Chadd 				    __func__, c->ic_ieee, c->ic_freq, midpoint,
1180*67f4aa38SAdrian Chadd 				    c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1181*67f4aa38SAdrian Chadd #endif
1182*67f4aa38SAdrian Chadd 				return (1);
1183*67f4aa38SAdrian Chadd 			}
1184*67f4aa38SAdrian Chadd 		}
1185*67f4aa38SAdrian Chadd 		return (0);
1186*67f4aa38SAdrian Chadd 	}
1187*67f4aa38SAdrian Chadd 
1188*67f4aa38SAdrian Chadd 	printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1189*67f4aa38SAdrian Chadd 	    __func__,
1190*67f4aa38SAdrian Chadd 	    c->ic_ieee,
1191*67f4aa38SAdrian Chadd 	    c->ic_flags);
1192*67f4aa38SAdrian Chadd 
1193*67f4aa38SAdrian Chadd 	return (0);
1194*67f4aa38SAdrian Chadd }
1195*67f4aa38SAdrian Chadd 
1196*67f4aa38SAdrian Chadd /*
1197*67f4aa38SAdrian Chadd  * Return whether the current channel could possibly be a part of
1198*67f4aa38SAdrian Chadd  * a VHT80 channel.
1199*67f4aa38SAdrian Chadd  *
1200*67f4aa38SAdrian Chadd  * This doesn't check that the whole range is in the allowed list
1201*67f4aa38SAdrian Chadd  * according to regulatory.
1202*67f4aa38SAdrian Chadd  */
1203*67f4aa38SAdrian Chadd static int
1204*67f4aa38SAdrian Chadd is_vht80_valid_freq(uint16_t freq)
1205*67f4aa38SAdrian Chadd {
1206*67f4aa38SAdrian Chadd 	int i;
1207*67f4aa38SAdrian Chadd 	for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1208*67f4aa38SAdrian Chadd 		if (freq >= vht80_chan_ranges[i].freq_start &&
1209*67f4aa38SAdrian Chadd 		    freq < vht80_chan_ranges[i].freq_end)
1210*67f4aa38SAdrian Chadd 			return (1);
1211*67f4aa38SAdrian Chadd 	}
1212*67f4aa38SAdrian Chadd 	return (0);
1213*67f4aa38SAdrian Chadd }
1214*67f4aa38SAdrian Chadd 
1215355fec48SAndriy Voskoboinyk static int
1216355fec48SAndriy Voskoboinyk addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1217355fec48SAndriy Voskoboinyk     uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1218355fec48SAndriy Voskoboinyk {
1219355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1220355fec48SAndriy Voskoboinyk 
1221355fec48SAndriy Voskoboinyk 	if (*nchans >= maxchans)
1222355fec48SAndriy Voskoboinyk 		return (ENOBUFS);
1223355fec48SAndriy Voskoboinyk 
1224*67f4aa38SAdrian Chadd #if 0
1225*67f4aa38SAdrian Chadd 	printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
1226*67f4aa38SAdrian Chadd 	    __func__,
1227*67f4aa38SAdrian Chadd 	    *nchans,
1228*67f4aa38SAdrian Chadd 	    ieee,
1229*67f4aa38SAdrian Chadd 	    freq,
1230*67f4aa38SAdrian Chadd 	    flags);
1231*67f4aa38SAdrian Chadd #endif
1232*67f4aa38SAdrian Chadd 
1233355fec48SAndriy Voskoboinyk 	c = &chans[(*nchans)++];
1234355fec48SAndriy Voskoboinyk 	c->ic_ieee = ieee;
1235355fec48SAndriy Voskoboinyk 	c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1236355fec48SAndriy Voskoboinyk 	c->ic_maxregpower = maxregpower;
1237355fec48SAndriy Voskoboinyk 	c->ic_maxpower = 2 * maxregpower;
1238355fec48SAndriy Voskoboinyk 	c->ic_flags = flags;
1239*67f4aa38SAdrian Chadd 	c->ic_vht_ch_freq1 = 0;
1240*67f4aa38SAdrian Chadd 	c->ic_vht_ch_freq2 = 0;
1241355fec48SAndriy Voskoboinyk 	set_extchan(c);
1242*67f4aa38SAdrian Chadd 	set_vht_extchan(c);
1243355fec48SAndriy Voskoboinyk 
1244355fec48SAndriy Voskoboinyk 	return (0);
1245355fec48SAndriy Voskoboinyk }
1246355fec48SAndriy Voskoboinyk 
1247355fec48SAndriy Voskoboinyk static int
1248355fec48SAndriy Voskoboinyk copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1249355fec48SAndriy Voskoboinyk     uint32_t flags)
1250355fec48SAndriy Voskoboinyk {
1251355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1252355fec48SAndriy Voskoboinyk 
1253355fec48SAndriy Voskoboinyk 	KASSERT(*nchans > 0, ("channel list is empty\n"));
1254355fec48SAndriy Voskoboinyk 
1255355fec48SAndriy Voskoboinyk 	if (*nchans >= maxchans)
1256355fec48SAndriy Voskoboinyk 		return (ENOBUFS);
1257355fec48SAndriy Voskoboinyk 
1258*67f4aa38SAdrian Chadd #if 0
1259*67f4aa38SAdrian Chadd 	printf("%s: %d: flags=0x%08x\n",
1260*67f4aa38SAdrian Chadd 	    __func__,
1261*67f4aa38SAdrian Chadd 	    *nchans,
1262*67f4aa38SAdrian Chadd 	    flags);
1263*67f4aa38SAdrian Chadd #endif
1264*67f4aa38SAdrian Chadd 
1265355fec48SAndriy Voskoboinyk 	c = &chans[(*nchans)++];
1266355fec48SAndriy Voskoboinyk 	c[0] = c[-1];
1267355fec48SAndriy Voskoboinyk 	c->ic_flags = flags;
1268*67f4aa38SAdrian Chadd 	c->ic_vht_ch_freq1 = 0;
1269*67f4aa38SAdrian Chadd 	c->ic_vht_ch_freq2 = 0;
1270355fec48SAndriy Voskoboinyk 	set_extchan(c);
1271*67f4aa38SAdrian Chadd 	set_vht_extchan(c);
1272355fec48SAndriy Voskoboinyk 
1273355fec48SAndriy Voskoboinyk 	return (0);
1274355fec48SAndriy Voskoboinyk }
1275355fec48SAndriy Voskoboinyk 
1276*67f4aa38SAdrian Chadd /*
1277*67f4aa38SAdrian Chadd  * XXX VHT-2GHz
1278*67f4aa38SAdrian Chadd  */
1279355fec48SAndriy Voskoboinyk static void
1280355fec48SAndriy Voskoboinyk getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1281355fec48SAndriy Voskoboinyk {
1282355fec48SAndriy Voskoboinyk 	int nmodes;
1283355fec48SAndriy Voskoboinyk 
1284355fec48SAndriy Voskoboinyk 	nmodes = 0;
1285355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11B))
1286355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_B;
1287355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11G))
1288355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G;
1289355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11NG))
1290355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1291355fec48SAndriy Voskoboinyk 	if (ht40) {
1292355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1293355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1294355fec48SAndriy Voskoboinyk 	}
1295355fec48SAndriy Voskoboinyk 	flags[nmodes] = 0;
1296355fec48SAndriy Voskoboinyk }
1297355fec48SAndriy Voskoboinyk 
1298355fec48SAndriy Voskoboinyk static void
1299*67f4aa38SAdrian Chadd getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1300355fec48SAndriy Voskoboinyk {
1301355fec48SAndriy Voskoboinyk 	int nmodes;
1302355fec48SAndriy Voskoboinyk 
1303*67f4aa38SAdrian Chadd 	/*
1304*67f4aa38SAdrian Chadd 	 * the addchan_list function seems to expect the flags array to
1305*67f4aa38SAdrian Chadd 	 * be in channel width order, so the VHT bits are interspersed
1306*67f4aa38SAdrian Chadd 	 * as appropriate to maintain said order.
1307*67f4aa38SAdrian Chadd 	 *
1308*67f4aa38SAdrian Chadd 	 * It also assumes HT40U is before HT40D.
1309*67f4aa38SAdrian Chadd 	 */
1310355fec48SAndriy Voskoboinyk 	nmodes = 0;
1311*67f4aa38SAdrian Chadd 
1312*67f4aa38SAdrian Chadd 	/* 20MHz */
1313355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11A))
1314355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A;
1315355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11NA))
1316355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1317*67f4aa38SAdrian Chadd 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1318*67f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
1319*67f4aa38SAdrian Chadd 		    IEEE80211_CHAN_VHT20;
1320*67f4aa38SAdrian Chadd 
1321*67f4aa38SAdrian Chadd 	/* 40MHz */
1322355fec48SAndriy Voskoboinyk 	if (ht40) {
1323355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1324*67f4aa38SAdrian Chadd 	}
1325*67f4aa38SAdrian Chadd 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1326*67f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
1327*67f4aa38SAdrian Chadd 		    | IEEE80211_CHAN_VHT40U;
1328*67f4aa38SAdrian Chadd 	}
1329*67f4aa38SAdrian Chadd 	if (ht40) {
1330355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1331355fec48SAndriy Voskoboinyk 	}
1332*67f4aa38SAdrian Chadd 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1333*67f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
1334*67f4aa38SAdrian Chadd 		    | IEEE80211_CHAN_VHT40D;
1335*67f4aa38SAdrian Chadd 	}
1336*67f4aa38SAdrian Chadd 
1337*67f4aa38SAdrian Chadd 	/* 80MHz */
1338*67f4aa38SAdrian Chadd 	if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1339*67f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A |
1340*67f4aa38SAdrian Chadd 		    IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80;
1341*67f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A |
1342*67f4aa38SAdrian Chadd 		    IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80;
1343*67f4aa38SAdrian Chadd 		}
1344*67f4aa38SAdrian Chadd 	}
1345*67f4aa38SAdrian Chadd 
1346*67f4aa38SAdrian Chadd 	/* XXX VHT80+80 */
1347*67f4aa38SAdrian Chadd 	/* XXX VHT160 */
1348355fec48SAndriy Voskoboinyk 	flags[nmodes] = 0;
1349355fec48SAndriy Voskoboinyk }
1350355fec48SAndriy Voskoboinyk 
1351355fec48SAndriy Voskoboinyk static void
1352*67f4aa38SAdrian Chadd getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1353355fec48SAndriy Voskoboinyk {
1354355fec48SAndriy Voskoboinyk 
1355355fec48SAndriy Voskoboinyk 	flags[0] = 0;
1356355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11A) ||
1357*67f4aa38SAdrian Chadd 	    isset(bands, IEEE80211_MODE_11NA) ||
1358*67f4aa38SAdrian Chadd 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1359355fec48SAndriy Voskoboinyk 		if (isset(bands, IEEE80211_MODE_11B) ||
1360355fec48SAndriy Voskoboinyk 		    isset(bands, IEEE80211_MODE_11G) ||
1361*67f4aa38SAdrian Chadd 		    isset(bands, IEEE80211_MODE_11NG) ||
1362*67f4aa38SAdrian Chadd 		    isset(bands, IEEE80211_MODE_VHT_2GHZ))
1363355fec48SAndriy Voskoboinyk 			return;
1364355fec48SAndriy Voskoboinyk 
1365*67f4aa38SAdrian Chadd 		getflags_5ghz(bands, flags, ht40, vht80);
1366355fec48SAndriy Voskoboinyk 	} else
1367355fec48SAndriy Voskoboinyk 		getflags_2ghz(bands, flags, ht40);
1368355fec48SAndriy Voskoboinyk }
1369355fec48SAndriy Voskoboinyk 
1370355fec48SAndriy Voskoboinyk /*
1371355fec48SAndriy Voskoboinyk  * Add one 20 MHz channel into specified channel list.
1372355fec48SAndriy Voskoboinyk  */
1373*67f4aa38SAdrian Chadd /* XXX VHT */
1374355fec48SAndriy Voskoboinyk int
1375355fec48SAndriy Voskoboinyk ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1376355fec48SAndriy Voskoboinyk     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1377355fec48SAndriy Voskoboinyk     uint32_t chan_flags, const uint8_t bands[])
1378355fec48SAndriy Voskoboinyk {
1379355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
1380355fec48SAndriy Voskoboinyk 	int i, error;
1381355fec48SAndriy Voskoboinyk 
1382*67f4aa38SAdrian Chadd 	getflags(bands, flags, 0, 0);
1383355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1384355fec48SAndriy Voskoboinyk 
1385355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1386355fec48SAndriy Voskoboinyk 	    flags[0] | chan_flags);
1387355fec48SAndriy Voskoboinyk 	for (i = 1; flags[i] != 0 && error == 0; i++) {
1388355fec48SAndriy Voskoboinyk 		error = copychan_prev(chans, maxchans, nchans,
1389355fec48SAndriy Voskoboinyk 		    flags[i] | chan_flags);
1390355fec48SAndriy Voskoboinyk 	}
1391355fec48SAndriy Voskoboinyk 
1392355fec48SAndriy Voskoboinyk 	return (error);
1393355fec48SAndriy Voskoboinyk }
1394355fec48SAndriy Voskoboinyk 
1395355fec48SAndriy Voskoboinyk static struct ieee80211_channel *
1396355fec48SAndriy Voskoboinyk findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1397355fec48SAndriy Voskoboinyk     uint32_t flags)
1398355fec48SAndriy Voskoboinyk {
1399355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1400355fec48SAndriy Voskoboinyk 	int i;
1401355fec48SAndriy Voskoboinyk 
1402355fec48SAndriy Voskoboinyk 	flags &= IEEE80211_CHAN_ALLTURBO;
1403355fec48SAndriy Voskoboinyk 	/* brute force search */
1404355fec48SAndriy Voskoboinyk 	for (i = 0; i < nchans; i++) {
1405355fec48SAndriy Voskoboinyk 		c = &chans[i];
1406355fec48SAndriy Voskoboinyk 		if (c->ic_freq == freq &&
1407355fec48SAndriy Voskoboinyk 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1408355fec48SAndriy Voskoboinyk 			return c;
1409355fec48SAndriy Voskoboinyk 	}
1410355fec48SAndriy Voskoboinyk 	return NULL;
1411355fec48SAndriy Voskoboinyk }
1412355fec48SAndriy Voskoboinyk 
1413355fec48SAndriy Voskoboinyk /*
1414355fec48SAndriy Voskoboinyk  * Add 40 MHz channel pair into specified channel list.
1415355fec48SAndriy Voskoboinyk  */
1416*67f4aa38SAdrian Chadd /* XXX VHT */
1417355fec48SAndriy Voskoboinyk int
1418355fec48SAndriy Voskoboinyk ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1419355fec48SAndriy Voskoboinyk     int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1420355fec48SAndriy Voskoboinyk {
1421355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *cent, *extc;
1422355fec48SAndriy Voskoboinyk 	uint16_t freq;
1423355fec48SAndriy Voskoboinyk 	int error;
1424355fec48SAndriy Voskoboinyk 
1425355fec48SAndriy Voskoboinyk 	freq = ieee80211_ieee2mhz(ieee, flags);
1426355fec48SAndriy Voskoboinyk 
1427355fec48SAndriy Voskoboinyk 	/*
1428355fec48SAndriy Voskoboinyk 	 * Each entry defines an HT40 channel pair; find the
1429355fec48SAndriy Voskoboinyk 	 * center channel, then the extension channel above.
1430355fec48SAndriy Voskoboinyk 	 */
1431355fec48SAndriy Voskoboinyk 	flags |= IEEE80211_CHAN_HT20;
1432355fec48SAndriy Voskoboinyk 	cent = findchannel(chans, *nchans, freq, flags);
1433355fec48SAndriy Voskoboinyk 	if (cent == NULL)
1434355fec48SAndriy Voskoboinyk 		return (EINVAL);
1435355fec48SAndriy Voskoboinyk 
1436355fec48SAndriy Voskoboinyk 	extc = findchannel(chans, *nchans, freq + 20, flags);
1437355fec48SAndriy Voskoboinyk 	if (extc == NULL)
1438355fec48SAndriy Voskoboinyk 		return (ENOENT);
1439355fec48SAndriy Voskoboinyk 
1440355fec48SAndriy Voskoboinyk 	flags &= ~IEEE80211_CHAN_HT;
1441355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1442355fec48SAndriy Voskoboinyk 	    maxregpower, flags | IEEE80211_CHAN_HT40U);
1443355fec48SAndriy Voskoboinyk 	if (error != 0)
1444355fec48SAndriy Voskoboinyk 		return (error);
1445355fec48SAndriy Voskoboinyk 
1446355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1447355fec48SAndriy Voskoboinyk 	    maxregpower, flags | IEEE80211_CHAN_HT40D);
1448355fec48SAndriy Voskoboinyk 
1449355fec48SAndriy Voskoboinyk 	return (error);
1450355fec48SAndriy Voskoboinyk }
1451355fec48SAndriy Voskoboinyk 
1452355fec48SAndriy Voskoboinyk /*
14534774b999SAdrian Chadd  * Fetch the center frequency for the primary channel.
14544774b999SAdrian Chadd  */
14554774b999SAdrian Chadd uint32_t
14564774b999SAdrian Chadd ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
14574774b999SAdrian Chadd {
14584774b999SAdrian Chadd 
14594774b999SAdrian Chadd 	return (c->ic_freq);
14604774b999SAdrian Chadd }
14614774b999SAdrian Chadd 
14624774b999SAdrian Chadd /*
14634774b999SAdrian Chadd  * Fetch the center frequency for the primary BAND channel.
14644774b999SAdrian Chadd  *
14654774b999SAdrian Chadd  * For 5, 10, 20MHz channels it'll be the normally configured channel
14664774b999SAdrian Chadd  * frequency.
14674774b999SAdrian Chadd  *
14684774b999SAdrian Chadd  * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
14694774b999SAdrian Chadd  * wide channel, not the centre of the primary channel (that's ic_freq).
14704774b999SAdrian Chadd  *
14714774b999SAdrian Chadd  * For 80+80MHz channels this will be the centre of the primary
14724774b999SAdrian Chadd  * 80MHz channel; the secondary 80MHz channel will be center_freq2().
14734774b999SAdrian Chadd  */
14744774b999SAdrian Chadd uint32_t
14754774b999SAdrian Chadd ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
14764774b999SAdrian Chadd {
14774774b999SAdrian Chadd 
1478*67f4aa38SAdrian Chadd 	/*
1479*67f4aa38SAdrian Chadd 	 * VHT - use the pre-calculated centre frequency
1480*67f4aa38SAdrian Chadd 	 * of the given channel.
1481*67f4aa38SAdrian Chadd 	 */
1482*67f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c))
1483*67f4aa38SAdrian Chadd 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1484*67f4aa38SAdrian Chadd 
14854774b999SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT40U(c)) {
14864774b999SAdrian Chadd 		return (c->ic_freq + 10);
14874774b999SAdrian Chadd 	}
14884774b999SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT40D(c)) {
14894774b999SAdrian Chadd 		return (c->ic_freq - 10);
14904774b999SAdrian Chadd 	}
14914774b999SAdrian Chadd 
14924774b999SAdrian Chadd 	return (c->ic_freq);
14934774b999SAdrian Chadd }
14944774b999SAdrian Chadd 
14954774b999SAdrian Chadd /*
1496*67f4aa38SAdrian Chadd  * For now, no 80+80 support; it will likely always return 0.
14974774b999SAdrian Chadd  */
14984774b999SAdrian Chadd uint32_t
14994774b999SAdrian Chadd ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
15004774b999SAdrian Chadd {
15014774b999SAdrian Chadd 
1502*67f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
1503*67f4aa38SAdrian Chadd 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
1504*67f4aa38SAdrian Chadd 
15054774b999SAdrian Chadd 	return (0);
15064774b999SAdrian Chadd }
15074774b999SAdrian Chadd 
15084774b999SAdrian Chadd /*
1509355fec48SAndriy Voskoboinyk  * Adds channels into specified channel list (ieee[] array must be sorted).
1510355fec48SAndriy Voskoboinyk  * Channels are already sorted.
1511355fec48SAndriy Voskoboinyk  */
1512355fec48SAndriy Voskoboinyk static int
1513355fec48SAndriy Voskoboinyk add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1514355fec48SAndriy Voskoboinyk     const uint8_t ieee[], int nieee, uint32_t flags[])
1515355fec48SAndriy Voskoboinyk {
1516355fec48SAndriy Voskoboinyk 	uint16_t freq;
1517355fec48SAndriy Voskoboinyk 	int i, j, error;
1518*67f4aa38SAdrian Chadd 	int is_vht;
1519355fec48SAndriy Voskoboinyk 
1520355fec48SAndriy Voskoboinyk 	for (i = 0; i < nieee; i++) {
1521355fec48SAndriy Voskoboinyk 		freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1522355fec48SAndriy Voskoboinyk 		for (j = 0; flags[j] != 0; j++) {
1523*67f4aa38SAdrian Chadd 			/*
1524*67f4aa38SAdrian Chadd 			 * Notes:
1525*67f4aa38SAdrian Chadd 			 * + HT40 and VHT40 channels occur together, so
1526*67f4aa38SAdrian Chadd 			 *   we need to be careful that we actually allow that.
1527*67f4aa38SAdrian Chadd 			 * + VHT80, VHT160 will coexist with HT40/VHT40, so
1528*67f4aa38SAdrian Chadd 			 *   make sure it's not skipped because of the overlap
1529*67f4aa38SAdrian Chadd 			 *   check used for (V)HT40.
1530*67f4aa38SAdrian Chadd 			 */
1531*67f4aa38SAdrian Chadd 			is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1532*67f4aa38SAdrian Chadd 
1533*67f4aa38SAdrian Chadd 			/*
1534*67f4aa38SAdrian Chadd 			 * Test for VHT80.
1535*67f4aa38SAdrian Chadd 			 * XXX This is all very broken right now.
1536*67f4aa38SAdrian Chadd 			 * What we /should/ do is:
1537*67f4aa38SAdrian Chadd 			 *
1538*67f4aa38SAdrian Chadd 			 * + check that the frequency is in the list of
1539*67f4aa38SAdrian Chadd 			 *   allowed VHT80 ranges; and
1540*67f4aa38SAdrian Chadd 			 * + the other 3 channels in the list are actually
1541*67f4aa38SAdrian Chadd 			 *   also available.
1542*67f4aa38SAdrian Chadd 			 */
1543*67f4aa38SAdrian Chadd 			if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1544*67f4aa38SAdrian Chadd 				if (! is_vht80_valid_freq(freq))
1545*67f4aa38SAdrian Chadd 					continue;
1546*67f4aa38SAdrian Chadd 
1547*67f4aa38SAdrian Chadd 			/*
1548*67f4aa38SAdrian Chadd 			 * Test for (V)HT40.
1549*67f4aa38SAdrian Chadd 			 *
1550*67f4aa38SAdrian Chadd 			 * This is also a fall through from VHT80; as we only
1551*67f4aa38SAdrian Chadd 			 * allow a VHT80 channel if the VHT40 combination is
1552*67f4aa38SAdrian Chadd 			 * also valid.  If the VHT40 form is not valid then
1553*67f4aa38SAdrian Chadd 			 * we certainly can't do VHT80..
1554*67f4aa38SAdrian Chadd 			 */
1555355fec48SAndriy Voskoboinyk 			if (flags[j] & IEEE80211_CHAN_HT40D)
1556*67f4aa38SAdrian Chadd 				/*
1557*67f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if we are the
1558*67f4aa38SAdrian Chadd 				 * first channel.
1559*67f4aa38SAdrian Chadd 				 *
1560*67f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if it's below/
1561*67f4aa38SAdrian Chadd 				 * within 20MHz of the first channel.
1562*67f4aa38SAdrian Chadd 				 *
1563*67f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if the channel
1564*67f4aa38SAdrian Chadd 				 * below it is not 20MHz away.
1565*67f4aa38SAdrian Chadd 				 */
1566355fec48SAndriy Voskoboinyk 				if (i == 0 || ieee[i] < ieee[0] + 4 ||
1567355fec48SAndriy Voskoboinyk 				    freq - 20 !=
1568355fec48SAndriy Voskoboinyk 				    ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1569355fec48SAndriy Voskoboinyk 					continue;
1570355fec48SAndriy Voskoboinyk 			if (flags[j] & IEEE80211_CHAN_HT40U)
1571*67f4aa38SAdrian Chadd 				/*
1572*67f4aa38SAdrian Chadd 				 * Can't have an "upper" channel if we are
1573*67f4aa38SAdrian Chadd 				 * the last channel.
1574*67f4aa38SAdrian Chadd 				 *
1575*67f4aa38SAdrian Chadd 				 * Can't have an "upper" channel be above the
1576*67f4aa38SAdrian Chadd 				 * last channel in the list.
1577*67f4aa38SAdrian Chadd 				 *
1578*67f4aa38SAdrian Chadd 				 * Can't have an "upper" channel if the next
1579*67f4aa38SAdrian Chadd 				 * channel according to the math isn't 20MHz
1580*67f4aa38SAdrian Chadd 				 * away.  (Likely for channel 13/14.)
1581*67f4aa38SAdrian Chadd 				 */
1582355fec48SAndriy Voskoboinyk 				if (i == nieee - 1 ||
1583355fec48SAndriy Voskoboinyk 				    ieee[i] + 4 > ieee[nieee - 1] ||
1584355fec48SAndriy Voskoboinyk 				    freq + 20 !=
1585355fec48SAndriy Voskoboinyk 				    ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1586355fec48SAndriy Voskoboinyk 					continue;
1587355fec48SAndriy Voskoboinyk 
1588355fec48SAndriy Voskoboinyk 			if (j == 0) {
1589355fec48SAndriy Voskoboinyk 				error = addchan(chans, maxchans, nchans,
1590355fec48SAndriy Voskoboinyk 				    ieee[i], freq, 0, flags[j]);
1591355fec48SAndriy Voskoboinyk 			} else {
1592355fec48SAndriy Voskoboinyk 				error = copychan_prev(chans, maxchans, nchans,
1593355fec48SAndriy Voskoboinyk 				    flags[j]);
1594355fec48SAndriy Voskoboinyk 			}
1595355fec48SAndriy Voskoboinyk 			if (error != 0)
1596355fec48SAndriy Voskoboinyk 				return (error);
1597355fec48SAndriy Voskoboinyk 		}
1598355fec48SAndriy Voskoboinyk 	}
1599355fec48SAndriy Voskoboinyk 
16006dbbec93SAndriy Voskoboinyk 	return (0);
1601355fec48SAndriy Voskoboinyk }
1602355fec48SAndriy Voskoboinyk 
1603355fec48SAndriy Voskoboinyk int
1604355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1605355fec48SAndriy Voskoboinyk     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1606355fec48SAndriy Voskoboinyk     int ht40)
1607355fec48SAndriy Voskoboinyk {
1608355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
1609355fec48SAndriy Voskoboinyk 
1610*67f4aa38SAdrian Chadd 	/* XXX no VHT for now */
1611355fec48SAndriy Voskoboinyk 	getflags_2ghz(bands, flags, ht40);
1612355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1613355fec48SAndriy Voskoboinyk 
1614355fec48SAndriy Voskoboinyk 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1615355fec48SAndriy Voskoboinyk }
1616355fec48SAndriy Voskoboinyk 
1617355fec48SAndriy Voskoboinyk int
1618355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1619355fec48SAndriy Voskoboinyk     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1620355fec48SAndriy Voskoboinyk     int ht40)
1621355fec48SAndriy Voskoboinyk {
1622355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
1623*67f4aa38SAdrian Chadd 	int vht80 = 0;
1624355fec48SAndriy Voskoboinyk 
1625*67f4aa38SAdrian Chadd 	/*
1626*67f4aa38SAdrian Chadd 	 * For now, assume VHT == VHT80 support as a minimum.
1627*67f4aa38SAdrian Chadd 	 */
1628*67f4aa38SAdrian Chadd 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
1629*67f4aa38SAdrian Chadd 		vht80 = 1;
1630*67f4aa38SAdrian Chadd 
1631*67f4aa38SAdrian Chadd 	getflags_5ghz(bands, flags, ht40, vht80);
1632355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1633355fec48SAndriy Voskoboinyk 
1634355fec48SAndriy Voskoboinyk 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1635355fec48SAndriy Voskoboinyk }
1636355fec48SAndriy Voskoboinyk 
16371a1e1d21SSam Leffler /*
163868e8e04eSSam Leffler  * Locate a channel given a frequency+flags.  We cache
1639b032f27cSSam Leffler  * the previous lookup to optimize switching between two
164068e8e04eSSam Leffler  * channels--as happens with dynamic turbo.
164168e8e04eSSam Leffler  */
164268e8e04eSSam Leffler struct ieee80211_channel *
164368e8e04eSSam Leffler ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
164468e8e04eSSam Leffler {
164568e8e04eSSam Leffler 	struct ieee80211_channel *c;
164668e8e04eSSam Leffler 
164768e8e04eSSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
164868e8e04eSSam Leffler 	c = ic->ic_prevchan;
164968e8e04eSSam Leffler 	if (c != NULL && c->ic_freq == freq &&
165068e8e04eSSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
165168e8e04eSSam Leffler 		return c;
165268e8e04eSSam Leffler 	/* brute force search */
1653355fec48SAndriy Voskoboinyk 	return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
165468e8e04eSSam Leffler }
165568e8e04eSSam Leffler 
1656a557c018SSam Leffler /*
1657a557c018SSam Leffler  * Locate a channel given a channel number+flags.  We cache
1658a557c018SSam Leffler  * the previous lookup to optimize switching between two
1659a557c018SSam Leffler  * channels--as happens with dynamic turbo.
1660a557c018SSam Leffler  */
1661a557c018SSam Leffler struct ieee80211_channel *
1662a557c018SSam Leffler ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1663a557c018SSam Leffler {
1664a557c018SSam Leffler 	struct ieee80211_channel *c;
1665a557c018SSam Leffler 	int i;
1666a557c018SSam Leffler 
1667a557c018SSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
1668a557c018SSam Leffler 	c = ic->ic_prevchan;
1669a557c018SSam Leffler 	if (c != NULL && c->ic_ieee == ieee &&
1670a557c018SSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1671a557c018SSam Leffler 		return c;
1672a557c018SSam Leffler 	/* brute force search */
1673a557c018SSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1674a557c018SSam Leffler 		c = &ic->ic_channels[i];
1675a557c018SSam Leffler 		if (c->ic_ieee == ieee &&
1676a557c018SSam Leffler 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1677a557c018SSam Leffler 			return c;
1678a557c018SSam Leffler 	}
1679a557c018SSam Leffler 	return NULL;
1680a557c018SSam Leffler }
1681a557c018SSam Leffler 
1682c79f192cSAdrian Chadd /*
1683c79f192cSAdrian Chadd  * Lookup a channel suitable for the given rx status.
1684c79f192cSAdrian Chadd  *
1685c79f192cSAdrian Chadd  * This is used to find a channel for a frame (eg beacon, probe
1686c79f192cSAdrian Chadd  * response) based purely on the received PHY information.
1687c79f192cSAdrian Chadd  *
1688c79f192cSAdrian Chadd  * For now it tries to do it based on R_FREQ / R_IEEE.
1689c79f192cSAdrian Chadd  * This is enough for 11bg and 11a (and thus 11ng/11na)
1690c79f192cSAdrian Chadd  * but it will not be enough for GSM, PSB channels and the
1691c79f192cSAdrian Chadd  * like.  It also doesn't know about legacy-turbog and
1692c79f192cSAdrian Chadd  * legacy-turbo modes, which some offload NICs actually
1693c79f192cSAdrian Chadd  * support in weird ways.
1694c79f192cSAdrian Chadd  *
1695c79f192cSAdrian Chadd  * Takes the ic and rxstatus; returns the channel or NULL
1696c79f192cSAdrian Chadd  * if not found.
1697c79f192cSAdrian Chadd  *
1698c79f192cSAdrian Chadd  * XXX TODO: Add support for that when the need arises.
1699c79f192cSAdrian Chadd  */
1700c79f192cSAdrian Chadd struct ieee80211_channel *
1701c79f192cSAdrian Chadd ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1702c79f192cSAdrian Chadd     const struct ieee80211_rx_stats *rxs)
1703c79f192cSAdrian Chadd {
1704c79f192cSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1705c79f192cSAdrian Chadd 	uint32_t flags;
1706c79f192cSAdrian Chadd 	struct ieee80211_channel *c;
1707c79f192cSAdrian Chadd 
1708c79f192cSAdrian Chadd 	if (rxs == NULL)
1709c79f192cSAdrian Chadd 		return (NULL);
1710c79f192cSAdrian Chadd 
1711c79f192cSAdrian Chadd 	/*
1712c79f192cSAdrian Chadd 	 * Strictly speaking we only use freq for now,
1713c79f192cSAdrian Chadd 	 * however later on we may wish to just store
1714c79f192cSAdrian Chadd 	 * the ieee for verification.
1715c79f192cSAdrian Chadd 	 */
1716c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1717c79f192cSAdrian Chadd 		return (NULL);
1718c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1719c79f192cSAdrian Chadd 		return (NULL);
1720c79f192cSAdrian Chadd 
1721c79f192cSAdrian Chadd 	/*
1722c79f192cSAdrian Chadd 	 * If the rx status contains a valid ieee/freq, then
1723c79f192cSAdrian Chadd 	 * ensure we populate the correct channel information
1724c79f192cSAdrian Chadd 	 * in rxchan before passing it up to the scan infrastructure.
1725c79f192cSAdrian Chadd 	 * Offload NICs will pass up beacons from all channels
1726c79f192cSAdrian Chadd 	 * during background scans.
1727c79f192cSAdrian Chadd 	 */
1728c79f192cSAdrian Chadd 
1729c79f192cSAdrian Chadd 	/* Determine a band */
1730c79f192cSAdrian Chadd 	/* XXX should be done by the driver? */
1731c79f192cSAdrian Chadd 	if (rxs->c_freq < 3000) {
17322108f2a8SAdrian Chadd 		flags = IEEE80211_CHAN_G;
1733c79f192cSAdrian Chadd 	} else {
1734c79f192cSAdrian Chadd 		flags = IEEE80211_CHAN_A;
1735c79f192cSAdrian Chadd 	}
1736c79f192cSAdrian Chadd 
1737c79f192cSAdrian Chadd 	/* Channel lookup */
1738c79f192cSAdrian Chadd 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1739c79f192cSAdrian Chadd 
1740c79f192cSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1741c79f192cSAdrian Chadd 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1742c79f192cSAdrian Chadd 	    __func__,
1743c79f192cSAdrian Chadd 	    (int) rxs->c_freq,
1744c79f192cSAdrian Chadd 	    (int) rxs->c_ieee,
1745c79f192cSAdrian Chadd 	    flags,
1746c79f192cSAdrian Chadd 	    c);
1747c79f192cSAdrian Chadd 
1748c79f192cSAdrian Chadd 	return (c);
1749c79f192cSAdrian Chadd }
1750c79f192cSAdrian Chadd 
175168e8e04eSSam Leffler static void
1752b032f27cSSam Leffler addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
175368e8e04eSSam Leffler {
175468e8e04eSSam Leffler #define	ADD(_ic, _s, _o) \
1755b032f27cSSam Leffler 	ifmedia_add(media, \
175668e8e04eSSam Leffler 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
175768e8e04eSSam Leffler 	static const u_int mopts[IEEE80211_MODE_MAX] = {
1758c3f10abdSSam Leffler 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1759c3f10abdSSam Leffler 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1760c3f10abdSSam Leffler 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1761c3f10abdSSam Leffler 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1762c3f10abdSSam Leffler 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1763c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1764c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1765c3f10abdSSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
17666a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
17676a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1768c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1769c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
17700c67d389SAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= IFM_IEEE80211_VHT2G,
17710c67d389SAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= IFM_IEEE80211_VHT5G,
177268e8e04eSSam Leffler 	};
177368e8e04eSSam Leffler 	u_int mopt;
177468e8e04eSSam Leffler 
177568e8e04eSSam Leffler 	mopt = mopts[mode];
1776b032f27cSSam Leffler 	if (addsta)
1777b032f27cSSam Leffler 		ADD(ic, mword, mopt);	/* STA mode has no cap */
1778b032f27cSSam Leffler 	if (caps & IEEE80211_C_IBSS)
1779b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1780b032f27cSSam Leffler 	if (caps & IEEE80211_C_HOSTAP)
1781b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1782b032f27cSSam Leffler 	if (caps & IEEE80211_C_AHDEMO)
1783b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1784b032f27cSSam Leffler 	if (caps & IEEE80211_C_MONITOR)
1785b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1786b032f27cSSam Leffler 	if (caps & IEEE80211_C_WDS)
1787b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
178859aa14a9SRui Paulo 	if (caps & IEEE80211_C_MBSS)
178959aa14a9SRui Paulo 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
179068e8e04eSSam Leffler #undef ADD
179168e8e04eSSam Leffler }
179268e8e04eSSam Leffler 
179368e8e04eSSam Leffler /*
17941a1e1d21SSam Leffler  * Setup the media data structures according to the channel and
1795b032f27cSSam Leffler  * rate tables.
17961a1e1d21SSam Leffler  */
1797b032f27cSSam Leffler static int
1798b032f27cSSam Leffler ieee80211_media_setup(struct ieee80211com *ic,
1799b032f27cSSam Leffler 	struct ifmedia *media, int caps, int addsta,
18001a1e1d21SSam Leffler 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
18011a1e1d21SSam Leffler {
1802fcd9500fSBernhard Schmidt 	int i, j, rate, maxrate, mword, r;
1803fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
180468e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
18051a1e1d21SSam Leffler 	struct ieee80211_rateset allrates;
18061a1e1d21SSam Leffler 
18072692bb26SSam Leffler 	/*
18081a1e1d21SSam Leffler 	 * Fill in media characteristics.
18091a1e1d21SSam Leffler 	 */
1810b032f27cSSam Leffler 	ifmedia_init(media, 0, media_change, media_stat);
18111a1e1d21SSam Leffler 	maxrate = 0;
181268e8e04eSSam Leffler 	/*
181368e8e04eSSam Leffler 	 * Add media for legacy operating modes.
181468e8e04eSSam Leffler 	 */
18151a1e1d21SSam Leffler 	memset(&allrates, 0, sizeof(allrates));
181668e8e04eSSam Leffler 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
18176dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
18181a1e1d21SSam Leffler 			continue;
1819b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
18201a1e1d21SSam Leffler 		if (mode == IEEE80211_MODE_AUTO)
18211a1e1d21SSam Leffler 			continue;
18221a1e1d21SSam Leffler 		rs = &ic->ic_sup_rates[mode];
18231a1e1d21SSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
18241a1e1d21SSam Leffler 			rate = rs->rs_rates[i];
18251a1e1d21SSam Leffler 			mword = ieee80211_rate2media(ic, rate, mode);
18261a1e1d21SSam Leffler 			if (mword == 0)
18271a1e1d21SSam Leffler 				continue;
1828b032f27cSSam Leffler 			addmedia(media, caps, addsta, mode, mword);
18291a1e1d21SSam Leffler 			/*
183068e8e04eSSam Leffler 			 * Add legacy rate to the collection of all rates.
18311a1e1d21SSam Leffler 			 */
18321a1e1d21SSam Leffler 			r = rate & IEEE80211_RATE_VAL;
18331a1e1d21SSam Leffler 			for (j = 0; j < allrates.rs_nrates; j++)
18341a1e1d21SSam Leffler 				if (allrates.rs_rates[j] == r)
18351a1e1d21SSam Leffler 					break;
18361a1e1d21SSam Leffler 			if (j == allrates.rs_nrates) {
18371a1e1d21SSam Leffler 				/* unique, add to the set */
18381a1e1d21SSam Leffler 				allrates.rs_rates[j] = r;
18391a1e1d21SSam Leffler 				allrates.rs_nrates++;
18401a1e1d21SSam Leffler 			}
18411a1e1d21SSam Leffler 			rate = (rate & IEEE80211_RATE_VAL) / 2;
18421a1e1d21SSam Leffler 			if (rate > maxrate)
18431a1e1d21SSam Leffler 				maxrate = rate;
18441a1e1d21SSam Leffler 		}
18451a1e1d21SSam Leffler 	}
18461a1e1d21SSam Leffler 	for (i = 0; i < allrates.rs_nrates; i++) {
18471a1e1d21SSam Leffler 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
18481a1e1d21SSam Leffler 				IEEE80211_MODE_AUTO);
18491a1e1d21SSam Leffler 		if (mword == 0)
18501a1e1d21SSam Leffler 			continue;
185168e8e04eSSam Leffler 		/* NB: remove media options from mword */
1852b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1853b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
18541a1e1d21SSam Leffler 	}
185568e8e04eSSam Leffler 	/*
185668e8e04eSSam Leffler 	 * Add HT/11n media.  Note that we do not have enough
185768e8e04eSSam Leffler 	 * bits in the media subtype to express the MCS so we
185868e8e04eSSam Leffler 	 * use a "placeholder" media subtype and any fixed MCS
185968e8e04eSSam Leffler 	 * must be specified with a different mechanism.
186068e8e04eSSam Leffler 	 */
18616a76ae21SSam Leffler 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
186268e8e04eSSam Leffler 		if (isclr(ic->ic_modecaps, mode))
186368e8e04eSSam Leffler 			continue;
1864b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1865b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
186668e8e04eSSam Leffler 	}
186768e8e04eSSam Leffler 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
186868e8e04eSSam Leffler 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1869b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1870b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
18716f897ba9SBernhard Schmidt 		i = ic->ic_txstream * 8 - 1;
18726f897ba9SBernhard Schmidt 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
18736f897ba9SBernhard Schmidt 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
18746f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_400ns;
18756f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
18766f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_800ns;
18776f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
18786f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_400ns;
18796f897ba9SBernhard Schmidt 		else
18806f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_800ns;
18816f897ba9SBernhard Schmidt 		if (rate > maxrate)
18826f897ba9SBernhard Schmidt 			maxrate = rate;
1883b032f27cSSam Leffler 	}
18840c67d389SAdrian Chadd 
18850c67d389SAdrian Chadd 	/*
18860c67d389SAdrian Chadd 	 * Add VHT media.
18870c67d389SAdrian Chadd 	 */
18880c67d389SAdrian Chadd 	for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
18890c67d389SAdrian Chadd 		if (isclr(ic->ic_modecaps, mode))
18900c67d389SAdrian Chadd 			continue;
18910c67d389SAdrian Chadd 		addmedia(media, caps, addsta, mode, IFM_AUTO);
18920c67d389SAdrian Chadd 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
18930c67d389SAdrian Chadd 
18940c67d389SAdrian Chadd 		/* XXX TODO: VHT maxrate */
18950c67d389SAdrian Chadd 	}
18960c67d389SAdrian Chadd 
1897b032f27cSSam Leffler 	return maxrate;
189868e8e04eSSam Leffler }
189968e8e04eSSam Leffler 
19006a76ae21SSam Leffler /* XXX inline or eliminate? */
190141b3c790SSam Leffler const struct ieee80211_rateset *
190241b3c790SSam Leffler ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
190341b3c790SSam Leffler {
190440432d36SSam Leffler 	/* XXX does this work for 11ng basic rates? */
190568e8e04eSSam Leffler 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
190641b3c790SSam Leffler }
190741b3c790SSam Leffler 
19088a1b9b6aSSam Leffler void
19098a1b9b6aSSam Leffler ieee80211_announce(struct ieee80211com *ic)
19108a1b9b6aSSam Leffler {
1911fcd9500fSBernhard Schmidt 	int i, rate, mword;
1912fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
191368e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
19148a1b9b6aSSam Leffler 
19157edb9e0aSSam Leffler 	/* NB: skip AUTO since it has no rates */
19167edb9e0aSSam Leffler 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
19176dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
19188a1b9b6aSSam Leffler 			continue;
1919c8f5794eSGleb Smirnoff 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
19208a1b9b6aSSam Leffler 		rs = &ic->ic_sup_rates[mode];
19218a1b9b6aSSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
192268e8e04eSSam Leffler 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
19238a1b9b6aSSam Leffler 			if (mword == 0)
19248a1b9b6aSSam Leffler 				continue;
192568e8e04eSSam Leffler 			rate = ieee80211_media2rate(mword);
19268a1b9b6aSSam Leffler 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
192768e8e04eSSam Leffler 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
19288a1b9b6aSSam Leffler 		}
19298a1b9b6aSSam Leffler 		printf("\n");
19308a1b9b6aSSam Leffler 	}
193168e8e04eSSam Leffler 	ieee80211_ht_announce(ic);
1932*67f4aa38SAdrian Chadd 	ieee80211_vht_announce(ic);
19338a1b9b6aSSam Leffler }
19348a1b9b6aSSam Leffler 
193568e8e04eSSam Leffler void
193668e8e04eSSam Leffler ieee80211_announce_channels(struct ieee80211com *ic)
19371a1e1d21SSam Leffler {
193868e8e04eSSam Leffler 	const struct ieee80211_channel *c;
193968e8e04eSSam Leffler 	char type;
194068e8e04eSSam Leffler 	int i, cw;
194168e8e04eSSam Leffler 
194268e8e04eSSam Leffler 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
194368e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
194468e8e04eSSam Leffler 		c = &ic->ic_channels[i];
194568e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
194668e8e04eSSam Leffler 			type = 'S';
194768e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108A(c))
194868e8e04eSSam Leffler 			type = 'T';
194968e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108G(c))
195068e8e04eSSam Leffler 			type = 'G';
195168e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HT(c))
195268e8e04eSSam Leffler 			type = 'n';
195368e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_A(c))
195468e8e04eSSam Leffler 			type = 'a';
195568e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_ANYG(c))
195668e8e04eSSam Leffler 			type = 'g';
195768e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_B(c))
195868e8e04eSSam Leffler 			type = 'b';
195968e8e04eSSam Leffler 		else
196068e8e04eSSam Leffler 			type = 'f';
196168e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
196268e8e04eSSam Leffler 			cw = 40;
196368e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(c))
196468e8e04eSSam Leffler 			cw = 10;
196568e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(c))
196668e8e04eSSam Leffler 			cw = 5;
196768e8e04eSSam Leffler 		else
196868e8e04eSSam Leffler 			cw = 20;
196968e8e04eSSam Leffler 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
197068e8e04eSSam Leffler 			, c->ic_ieee, c->ic_freq, type
197168e8e04eSSam Leffler 			, cw
197268e8e04eSSam Leffler 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
197368e8e04eSSam Leffler 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
197468e8e04eSSam Leffler 			, c->ic_maxregpower
197568e8e04eSSam Leffler 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
197668e8e04eSSam Leffler 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
197768e8e04eSSam Leffler 		);
197868e8e04eSSam Leffler 	}
19791a1e1d21SSam Leffler }
19801a1e1d21SSam Leffler 
198168e8e04eSSam Leffler static int
1982f945bd7aSSam Leffler media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
198368e8e04eSSam Leffler {
19841a1e1d21SSam Leffler 	switch (IFM_MODE(ime->ifm_media)) {
19851a1e1d21SSam Leffler 	case IFM_IEEE80211_11A:
1986b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11A;
19871a1e1d21SSam Leffler 		break;
19881a1e1d21SSam Leffler 	case IFM_IEEE80211_11B:
1989b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11B;
19901a1e1d21SSam Leffler 		break;
19911a1e1d21SSam Leffler 	case IFM_IEEE80211_11G:
1992b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11G;
19931a1e1d21SSam Leffler 		break;
19944844aa7dSAtsushi Onoe 	case IFM_IEEE80211_FH:
1995b032f27cSSam Leffler 		*mode = IEEE80211_MODE_FH;
19964844aa7dSAtsushi Onoe 		break;
199768e8e04eSSam Leffler 	case IFM_IEEE80211_11NA:
1998b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NA;
199968e8e04eSSam Leffler 		break;
200068e8e04eSSam Leffler 	case IFM_IEEE80211_11NG:
2001b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NG;
200268e8e04eSSam Leffler 		break;
20031a1e1d21SSam Leffler 	case IFM_AUTO:
2004b032f27cSSam Leffler 		*mode = IEEE80211_MODE_AUTO;
20051a1e1d21SSam Leffler 		break;
20061a1e1d21SSam Leffler 	default:
2007b032f27cSSam Leffler 		return 0;
20081a1e1d21SSam Leffler 	}
20091a1e1d21SSam Leffler 	/*
20108a1b9b6aSSam Leffler 	 * Turbo mode is an ``option''.
20118a1b9b6aSSam Leffler 	 * XXX does not apply to AUTO
20121a1e1d21SSam Leffler 	 */
20131a1e1d21SSam Leffler 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2014b032f27cSSam Leffler 		if (*mode == IEEE80211_MODE_11A) {
2015f945bd7aSSam Leffler 			if (flags & IEEE80211_F_TURBOP)
2016b032f27cSSam Leffler 				*mode = IEEE80211_MODE_TURBO_A;
201768e8e04eSSam Leffler 			else
2018b032f27cSSam Leffler 				*mode = IEEE80211_MODE_STURBO_A;
2019b032f27cSSam Leffler 		} else if (*mode == IEEE80211_MODE_11G)
2020b032f27cSSam Leffler 			*mode = IEEE80211_MODE_TURBO_G;
20218a1b9b6aSSam Leffler 		else
2022b032f27cSSam Leffler 			return 0;
20231a1e1d21SSam Leffler 	}
202468e8e04eSSam Leffler 	/* XXX HT40 +/- */
2025b032f27cSSam Leffler 	return 1;
2026b032f27cSSam Leffler }
20271a1e1d21SSam Leffler 
20281a1e1d21SSam Leffler /*
2029b032f27cSSam Leffler  * Handle a media change request on the vap interface.
2030b032f27cSSam Leffler  */
2031b032f27cSSam Leffler int
2032b032f27cSSam Leffler ieee80211_media_change(struct ifnet *ifp)
2033b032f27cSSam Leffler {
2034b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
2035b032f27cSSam Leffler 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2036f945bd7aSSam Leffler 	uint16_t newmode;
2037b032f27cSSam Leffler 
2038f945bd7aSSam Leffler 	if (!media2mode(ime, vap->iv_flags, &newmode))
2039b032f27cSSam Leffler 		return EINVAL;
2040f945bd7aSSam Leffler 	if (vap->iv_des_mode != newmode) {
2041f945bd7aSSam Leffler 		vap->iv_des_mode = newmode;
20420a310468SSam Leffler 		/* XXX kick state machine if up+running */
2043b032f27cSSam Leffler 	}
2044b032f27cSSam Leffler 	return 0;
2045b032f27cSSam Leffler }
2046b032f27cSSam Leffler 
204768e8e04eSSam Leffler /*
204868e8e04eSSam Leffler  * Common code to calculate the media status word
204968e8e04eSSam Leffler  * from the operating mode and channel state.
205068e8e04eSSam Leffler  */
205168e8e04eSSam Leffler static int
205268e8e04eSSam Leffler media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
205368e8e04eSSam Leffler {
205468e8e04eSSam Leffler 	int status;
205568e8e04eSSam Leffler 
205668e8e04eSSam Leffler 	status = IFM_IEEE80211;
205768e8e04eSSam Leffler 	switch (opmode) {
205868e8e04eSSam Leffler 	case IEEE80211_M_STA:
205968e8e04eSSam Leffler 		break;
206068e8e04eSSam Leffler 	case IEEE80211_M_IBSS:
206168e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC;
206268e8e04eSSam Leffler 		break;
206368e8e04eSSam Leffler 	case IEEE80211_M_HOSTAP:
206468e8e04eSSam Leffler 		status |= IFM_IEEE80211_HOSTAP;
206568e8e04eSSam Leffler 		break;
206668e8e04eSSam Leffler 	case IEEE80211_M_MONITOR:
206768e8e04eSSam Leffler 		status |= IFM_IEEE80211_MONITOR;
206868e8e04eSSam Leffler 		break;
206968e8e04eSSam Leffler 	case IEEE80211_M_AHDEMO:
207068e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
207168e8e04eSSam Leffler 		break;
207268e8e04eSSam Leffler 	case IEEE80211_M_WDS:
2073b032f27cSSam Leffler 		status |= IFM_IEEE80211_WDS;
207468e8e04eSSam Leffler 		break;
207559aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
207659aa14a9SRui Paulo 		status |= IFM_IEEE80211_MBSS;
207759aa14a9SRui Paulo 		break;
207868e8e04eSSam Leffler 	}
207968e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HTA(chan)) {
208068e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NA;
208168e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
208268e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NG;
208368e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_A(chan)) {
208468e8e04eSSam Leffler 		status |= IFM_IEEE80211_11A;
208568e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_B(chan)) {
208668e8e04eSSam Leffler 		status |= IFM_IEEE80211_11B;
208768e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
208868e8e04eSSam Leffler 		status |= IFM_IEEE80211_11G;
208968e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
209068e8e04eSSam Leffler 		status |= IFM_IEEE80211_FH;
209168e8e04eSSam Leffler 	}
209268e8e04eSSam Leffler 	/* XXX else complain? */
209368e8e04eSSam Leffler 
209468e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_TURBO(chan))
209568e8e04eSSam Leffler 		status |= IFM_IEEE80211_TURBO;
2096b032f27cSSam Leffler #if 0
2097b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT20(chan))
2098b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT20;
2099b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT40(chan))
2100b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT40;
2101b032f27cSSam Leffler #endif
210268e8e04eSSam Leffler 	return status;
210368e8e04eSSam Leffler }
210468e8e04eSSam Leffler 
21051a1e1d21SSam Leffler void
21061a1e1d21SSam Leffler ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
21071a1e1d21SSam Leffler {
2108b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
2109b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
211068e8e04eSSam Leffler 	enum ieee80211_phymode mode;
21111a1e1d21SSam Leffler 
21121a1e1d21SSam Leffler 	imr->ifm_status = IFM_AVALID;
211368e8e04eSSam Leffler 	/*
211468e8e04eSSam Leffler 	 * NB: use the current channel's mode to lock down a xmit
211568e8e04eSSam Leffler 	 * rate only when running; otherwise we may have a mismatch
211668e8e04eSSam Leffler 	 * in which case the rate will not be convertible.
211768e8e04eSSam Leffler 	 */
21189f098ac7SAdrian Chadd 	if (vap->iv_state == IEEE80211_S_RUN ||
21199f098ac7SAdrian Chadd 	    vap->iv_state == IEEE80211_S_SLEEP) {
21201a1e1d21SSam Leffler 		imr->ifm_status |= IFM_ACTIVE;
212168e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_curchan);
212268e8e04eSSam Leffler 	} else
212368e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
2124b032f27cSSam Leffler 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
21258a1b9b6aSSam Leffler 	/*
21268a1b9b6aSSam Leffler 	 * Calculate a current rate if possible.
21278a1b9b6aSSam Leffler 	 */
2128b032f27cSSam Leffler 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
21298a1b9b6aSSam Leffler 		/*
21308a1b9b6aSSam Leffler 		 * A fixed rate is set, report that.
21318a1b9b6aSSam Leffler 		 */
21328a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
2133b032f27cSSam Leffler 			vap->iv_txparms[mode].ucastrate, mode);
2134b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
21358a1b9b6aSSam Leffler 		/*
21368a1b9b6aSSam Leffler 		 * In station mode report the current transmit rate.
21378a1b9b6aSSam Leffler 		 */
21388a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
2139b032f27cSSam Leffler 			vap->iv_bss->ni_txrate, mode);
2140ba99a9b1SAndre Oppermann 	} else
21411a1e1d21SSam Leffler 		imr->ifm_active |= IFM_AUTO;
2142b032f27cSSam Leffler 	if (imr->ifm_status & IFM_ACTIVE)
2143b032f27cSSam Leffler 		imr->ifm_current = imr->ifm_active;
21441a1e1d21SSam Leffler }
21451a1e1d21SSam Leffler 
21461a1e1d21SSam Leffler /*
21471a1e1d21SSam Leffler  * Set the current phy mode and recalculate the active channel
21481a1e1d21SSam Leffler  * set based on the available channels for this mode.  Also
21491a1e1d21SSam Leffler  * select a new default/current channel if the current one is
21501a1e1d21SSam Leffler  * inappropriate for this mode.
21511a1e1d21SSam Leffler  */
21521a1e1d21SSam Leffler int
21531a1e1d21SSam Leffler ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
21541a1e1d21SSam Leffler {
21551a1e1d21SSam Leffler 	/*
2156ca4ac7aeSSam Leffler 	 * Adjust basic rates in 11b/11g supported rate set.
2157ca4ac7aeSSam Leffler 	 * Note that if operating on a hal/quarter rate channel
2158ca4ac7aeSSam Leffler 	 * this is a noop as those rates sets are different
2159ca4ac7aeSSam Leffler 	 * and used instead.
21601a1e1d21SSam Leffler 	 */
2161ca4ac7aeSSam Leffler 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2162b032f27cSSam Leffler 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2163ca4ac7aeSSam Leffler 
21641a1e1d21SSam Leffler 	ic->ic_curmode = mode;
21658a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);	/* reset ERP state */
21668a1b9b6aSSam Leffler 
21671a1e1d21SSam Leffler 	return 0;
21681a1e1d21SSam Leffler }
21691a1e1d21SSam Leffler 
21701a1e1d21SSam Leffler /*
217168e8e04eSSam Leffler  * Return the phy mode for with the specified channel.
21721a1e1d21SSam Leffler  */
21731a1e1d21SSam Leffler enum ieee80211_phymode
217468e8e04eSSam Leffler ieee80211_chan2mode(const struct ieee80211_channel *chan)
21751a1e1d21SSam Leffler {
217668e8e04eSSam Leffler 
21770c67d389SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
21780c67d389SAdrian Chadd 		return IEEE80211_MODE_VHT_2GHZ;
21790c67d389SAdrian Chadd 	else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
21800c67d389SAdrian Chadd 		return IEEE80211_MODE_VHT_5GHZ;
21810c67d389SAdrian Chadd 	else if (IEEE80211_IS_CHAN_HTA(chan))
218268e8e04eSSam Leffler 		return IEEE80211_MODE_11NA;
218368e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_HTG(chan))
218468e8e04eSSam Leffler 		return IEEE80211_MODE_11NG;
218568e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_108G(chan))
21868a1b9b6aSSam Leffler 		return IEEE80211_MODE_TURBO_G;
218768e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ST(chan))
218868e8e04eSSam Leffler 		return IEEE80211_MODE_STURBO_A;
218968e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_TURBO(chan))
219068e8e04eSSam Leffler 		return IEEE80211_MODE_TURBO_A;
21916a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_HALF(chan))
21926a76ae21SSam Leffler 		return IEEE80211_MODE_HALF;
21936a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
21946a76ae21SSam Leffler 		return IEEE80211_MODE_QUARTER;
219568e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_A(chan))
219668e8e04eSSam Leffler 		return IEEE80211_MODE_11A;
219768e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(chan))
21981a1e1d21SSam Leffler 		return IEEE80211_MODE_11G;
219968e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_B(chan))
220068e8e04eSSam Leffler 		return IEEE80211_MODE_11B;
220168e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_FHSS(chan))
220268e8e04eSSam Leffler 		return IEEE80211_MODE_FH;
220368e8e04eSSam Leffler 
220468e8e04eSSam Leffler 	/* NB: should not get here */
220568e8e04eSSam Leffler 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
220668e8e04eSSam Leffler 		__func__, chan->ic_freq, chan->ic_flags);
22071a1e1d21SSam Leffler 	return IEEE80211_MODE_11B;
22081a1e1d21SSam Leffler }
22091a1e1d21SSam Leffler 
221068e8e04eSSam Leffler struct ratemedia {
221168e8e04eSSam Leffler 	u_int	match;	/* rate + mode */
221268e8e04eSSam Leffler 	u_int	media;	/* if_media rate */
221368e8e04eSSam Leffler };
221468e8e04eSSam Leffler 
221568e8e04eSSam Leffler static int
221668e8e04eSSam Leffler findmedia(const struct ratemedia rates[], int n, u_int match)
221768e8e04eSSam Leffler {
221868e8e04eSSam Leffler 	int i;
221968e8e04eSSam Leffler 
222068e8e04eSSam Leffler 	for (i = 0; i < n; i++)
222168e8e04eSSam Leffler 		if (rates[i].match == match)
222268e8e04eSSam Leffler 			return rates[i].media;
222368e8e04eSSam Leffler 	return IFM_AUTO;
222468e8e04eSSam Leffler }
222568e8e04eSSam Leffler 
22261a1e1d21SSam Leffler /*
222768e8e04eSSam Leffler  * Convert IEEE80211 rate value to ifmedia subtype.
222868e8e04eSSam Leffler  * Rate is either a legacy rate in units of 0.5Mbps
222968e8e04eSSam Leffler  * or an MCS index.
22301a1e1d21SSam Leffler  */
22311a1e1d21SSam Leffler int
22321a1e1d21SSam Leffler ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
22331a1e1d21SSam Leffler {
223468e8e04eSSam Leffler 	static const struct ratemedia rates[] = {
22354844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
22364844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
22374844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
22384844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
22394844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
22404844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
22414844aa7dSAtsushi Onoe 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
22424844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
22434844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
22444844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
22454844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
22464844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
22474844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
22484844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
22494844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
22504844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
22514844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
22524844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
22534844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
22544844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
22554844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
22564844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
22574844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
22584844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
22594844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
22604844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
22614844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
226241b3c790SSam Leffler 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
226341b3c790SSam Leffler 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
226441b3c790SSam Leffler 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2265a4641f4eSPedro F. Giffuni 		/* NB: OFDM72 doesn't really exist so we don't handle it */
22661a1e1d21SSam Leffler 	};
226768e8e04eSSam Leffler 	static const struct ratemedia htrates[] = {
226868e8e04eSSam Leffler 		{   0, IFM_IEEE80211_MCS },
226968e8e04eSSam Leffler 		{   1, IFM_IEEE80211_MCS },
227068e8e04eSSam Leffler 		{   2, IFM_IEEE80211_MCS },
227168e8e04eSSam Leffler 		{   3, IFM_IEEE80211_MCS },
227268e8e04eSSam Leffler 		{   4, IFM_IEEE80211_MCS },
227368e8e04eSSam Leffler 		{   5, IFM_IEEE80211_MCS },
227468e8e04eSSam Leffler 		{   6, IFM_IEEE80211_MCS },
227568e8e04eSSam Leffler 		{   7, IFM_IEEE80211_MCS },
227668e8e04eSSam Leffler 		{   8, IFM_IEEE80211_MCS },
227768e8e04eSSam Leffler 		{   9, IFM_IEEE80211_MCS },
227868e8e04eSSam Leffler 		{  10, IFM_IEEE80211_MCS },
227968e8e04eSSam Leffler 		{  11, IFM_IEEE80211_MCS },
228068e8e04eSSam Leffler 		{  12, IFM_IEEE80211_MCS },
228168e8e04eSSam Leffler 		{  13, IFM_IEEE80211_MCS },
228268e8e04eSSam Leffler 		{  14, IFM_IEEE80211_MCS },
228368e8e04eSSam Leffler 		{  15, IFM_IEEE80211_MCS },
2284f136f45fSBernhard Schmidt 		{  16, IFM_IEEE80211_MCS },
2285f136f45fSBernhard Schmidt 		{  17, IFM_IEEE80211_MCS },
2286f136f45fSBernhard Schmidt 		{  18, IFM_IEEE80211_MCS },
2287f136f45fSBernhard Schmidt 		{  19, IFM_IEEE80211_MCS },
2288f136f45fSBernhard Schmidt 		{  20, IFM_IEEE80211_MCS },
2289f136f45fSBernhard Schmidt 		{  21, IFM_IEEE80211_MCS },
2290f136f45fSBernhard Schmidt 		{  22, IFM_IEEE80211_MCS },
2291f136f45fSBernhard Schmidt 		{  23, IFM_IEEE80211_MCS },
2292f136f45fSBernhard Schmidt 		{  24, IFM_IEEE80211_MCS },
2293f136f45fSBernhard Schmidt 		{  25, IFM_IEEE80211_MCS },
2294f136f45fSBernhard Schmidt 		{  26, IFM_IEEE80211_MCS },
2295f136f45fSBernhard Schmidt 		{  27, IFM_IEEE80211_MCS },
2296f136f45fSBernhard Schmidt 		{  28, IFM_IEEE80211_MCS },
2297f136f45fSBernhard Schmidt 		{  29, IFM_IEEE80211_MCS },
2298f136f45fSBernhard Schmidt 		{  30, IFM_IEEE80211_MCS },
2299f136f45fSBernhard Schmidt 		{  31, IFM_IEEE80211_MCS },
2300f136f45fSBernhard Schmidt 		{  32, IFM_IEEE80211_MCS },
2301f136f45fSBernhard Schmidt 		{  33, IFM_IEEE80211_MCS },
2302f136f45fSBernhard Schmidt 		{  34, IFM_IEEE80211_MCS },
2303f136f45fSBernhard Schmidt 		{  35, IFM_IEEE80211_MCS },
2304f136f45fSBernhard Schmidt 		{  36, IFM_IEEE80211_MCS },
2305f136f45fSBernhard Schmidt 		{  37, IFM_IEEE80211_MCS },
2306f136f45fSBernhard Schmidt 		{  38, IFM_IEEE80211_MCS },
2307f136f45fSBernhard Schmidt 		{  39, IFM_IEEE80211_MCS },
2308f136f45fSBernhard Schmidt 		{  40, IFM_IEEE80211_MCS },
2309f136f45fSBernhard Schmidt 		{  41, IFM_IEEE80211_MCS },
2310f136f45fSBernhard Schmidt 		{  42, IFM_IEEE80211_MCS },
2311f136f45fSBernhard Schmidt 		{  43, IFM_IEEE80211_MCS },
2312f136f45fSBernhard Schmidt 		{  44, IFM_IEEE80211_MCS },
2313f136f45fSBernhard Schmidt 		{  45, IFM_IEEE80211_MCS },
2314f136f45fSBernhard Schmidt 		{  46, IFM_IEEE80211_MCS },
2315f136f45fSBernhard Schmidt 		{  47, IFM_IEEE80211_MCS },
2316f136f45fSBernhard Schmidt 		{  48, IFM_IEEE80211_MCS },
2317f136f45fSBernhard Schmidt 		{  49, IFM_IEEE80211_MCS },
2318f136f45fSBernhard Schmidt 		{  50, IFM_IEEE80211_MCS },
2319f136f45fSBernhard Schmidt 		{  51, IFM_IEEE80211_MCS },
2320f136f45fSBernhard Schmidt 		{  52, IFM_IEEE80211_MCS },
2321f136f45fSBernhard Schmidt 		{  53, IFM_IEEE80211_MCS },
2322f136f45fSBernhard Schmidt 		{  54, IFM_IEEE80211_MCS },
2323f136f45fSBernhard Schmidt 		{  55, IFM_IEEE80211_MCS },
2324f136f45fSBernhard Schmidt 		{  56, IFM_IEEE80211_MCS },
2325f136f45fSBernhard Schmidt 		{  57, IFM_IEEE80211_MCS },
2326f136f45fSBernhard Schmidt 		{  58, IFM_IEEE80211_MCS },
2327f136f45fSBernhard Schmidt 		{  59, IFM_IEEE80211_MCS },
2328f136f45fSBernhard Schmidt 		{  60, IFM_IEEE80211_MCS },
2329f136f45fSBernhard Schmidt 		{  61, IFM_IEEE80211_MCS },
2330f136f45fSBernhard Schmidt 		{  62, IFM_IEEE80211_MCS },
2331f136f45fSBernhard Schmidt 		{  63, IFM_IEEE80211_MCS },
2332f136f45fSBernhard Schmidt 		{  64, IFM_IEEE80211_MCS },
2333f136f45fSBernhard Schmidt 		{  65, IFM_IEEE80211_MCS },
2334f136f45fSBernhard Schmidt 		{  66, IFM_IEEE80211_MCS },
2335f136f45fSBernhard Schmidt 		{  67, IFM_IEEE80211_MCS },
2336f136f45fSBernhard Schmidt 		{  68, IFM_IEEE80211_MCS },
2337f136f45fSBernhard Schmidt 		{  69, IFM_IEEE80211_MCS },
2338f136f45fSBernhard Schmidt 		{  70, IFM_IEEE80211_MCS },
2339f136f45fSBernhard Schmidt 		{  71, IFM_IEEE80211_MCS },
2340f136f45fSBernhard Schmidt 		{  72, IFM_IEEE80211_MCS },
2341f136f45fSBernhard Schmidt 		{  73, IFM_IEEE80211_MCS },
2342f136f45fSBernhard Schmidt 		{  74, IFM_IEEE80211_MCS },
2343f136f45fSBernhard Schmidt 		{  75, IFM_IEEE80211_MCS },
2344f136f45fSBernhard Schmidt 		{  76, IFM_IEEE80211_MCS },
234568e8e04eSSam Leffler 	};
234668e8e04eSSam Leffler 	int m;
23471a1e1d21SSam Leffler 
234868e8e04eSSam Leffler 	/*
234968e8e04eSSam Leffler 	 * Check 11n rates first for match as an MCS.
235068e8e04eSSam Leffler 	 */
235168e8e04eSSam Leffler 	if (mode == IEEE80211_MODE_11NA) {
2352f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
2353f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
2354a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
235568e8e04eSSam Leffler 			if (m != IFM_AUTO)
235668e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NA;
235768e8e04eSSam Leffler 		}
235868e8e04eSSam Leffler 	} else if (mode == IEEE80211_MODE_11NG) {
235968e8e04eSSam Leffler 		/* NB: 12 is ambiguous, it will be treated as an MCS */
2360f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
2361f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
2362a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
236368e8e04eSSam Leffler 			if (m != IFM_AUTO)
236468e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NG;
236568e8e04eSSam Leffler 		}
236668e8e04eSSam Leffler 	}
236768e8e04eSSam Leffler 	rate &= IEEE80211_RATE_VAL;
23681a1e1d21SSam Leffler 	switch (mode) {
23691a1e1d21SSam Leffler 	case IEEE80211_MODE_11A:
23706a76ae21SSam Leffler 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
23716a76ae21SSam Leffler 	case IEEE80211_MODE_QUARTER:
237268e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
23738a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_A:
237468e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
2375a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2376a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11A);
23771a1e1d21SSam Leffler 	case IEEE80211_MODE_11B:
2378a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2379a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11B);
23804844aa7dSAtsushi Onoe 	case IEEE80211_MODE_FH:
2381a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2382a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_FH);
23831a1e1d21SSam Leffler 	case IEEE80211_MODE_AUTO:
23841a1e1d21SSam Leffler 		/* NB: ic may be NULL for some drivers */
2385566d825bSSam Leffler 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2386a3e08d6fSRui Paulo 			return findmedia(rates, nitems(rates),
238768e8e04eSSam Leffler 			    rate | IFM_IEEE80211_FH);
23881a1e1d21SSam Leffler 		/* NB: hack, 11g matches both 11b+11a rates */
23891a1e1d21SSam Leffler 		/* fall thru... */
23901a1e1d21SSam Leffler 	case IEEE80211_MODE_11G:
239168e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
23928a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_G:
2393a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
23947aebd3e5SAdrian Chadd 	case IEEE80211_MODE_VHT_2GHZ:
23957aebd3e5SAdrian Chadd 	case IEEE80211_MODE_VHT_5GHZ:
23967aebd3e5SAdrian Chadd 		/* XXX TODO: need to figure out mapping for VHT rates */
23977aebd3e5SAdrian Chadd 		return IFM_AUTO;
23981a1e1d21SSam Leffler 	}
23991a1e1d21SSam Leffler 	return IFM_AUTO;
24001a1e1d21SSam Leffler }
24011a1e1d21SSam Leffler 
24021a1e1d21SSam Leffler int
24031a1e1d21SSam Leffler ieee80211_media2rate(int mword)
24041a1e1d21SSam Leffler {
24051a1e1d21SSam Leffler 	static const int ieeerates[] = {
24061a1e1d21SSam Leffler 		-1,		/* IFM_AUTO */
24071a1e1d21SSam Leffler 		0,		/* IFM_MANUAL */
24081a1e1d21SSam Leffler 		0,		/* IFM_NONE */
24091a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_FH1 */
24101a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_FH2 */
24111a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_DS1 */
24121a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_DS2 */
24131a1e1d21SSam Leffler 		11,		/* IFM_IEEE80211_DS5 */
24141a1e1d21SSam Leffler 		22,		/* IFM_IEEE80211_DS11 */
24151a1e1d21SSam Leffler 		44,		/* IFM_IEEE80211_DS22 */
24161a1e1d21SSam Leffler 		12,		/* IFM_IEEE80211_OFDM6 */
24171a1e1d21SSam Leffler 		18,		/* IFM_IEEE80211_OFDM9 */
24181a1e1d21SSam Leffler 		24,		/* IFM_IEEE80211_OFDM12 */
24191a1e1d21SSam Leffler 		36,		/* IFM_IEEE80211_OFDM18 */
24201a1e1d21SSam Leffler 		48,		/* IFM_IEEE80211_OFDM24 */
24211a1e1d21SSam Leffler 		72,		/* IFM_IEEE80211_OFDM36 */
24221a1e1d21SSam Leffler 		96,		/* IFM_IEEE80211_OFDM48 */
24231a1e1d21SSam Leffler 		108,		/* IFM_IEEE80211_OFDM54 */
24241a1e1d21SSam Leffler 		144,		/* IFM_IEEE80211_OFDM72 */
242541b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS354k */
242641b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS512k */
242741b3c790SSam Leffler 		6,		/* IFM_IEEE80211_OFDM3 */
242841b3c790SSam Leffler 		9,		/* IFM_IEEE80211_OFDM4 */
242941b3c790SSam Leffler 		54,		/* IFM_IEEE80211_OFDM27 */
243068e8e04eSSam Leffler 		-1,		/* IFM_IEEE80211_MCS */
24317aebd3e5SAdrian Chadd 		-1,		/* IFM_IEEE80211_VHT */
24321a1e1d21SSam Leffler 	};
2433a3e08d6fSRui Paulo 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
24341a1e1d21SSam Leffler 		ieeerates[IFM_SUBTYPE(mword)] : 0;
24351a1e1d21SSam Leffler }
24365b16c28cSSam Leffler 
24375b16c28cSSam Leffler /*
24385b16c28cSSam Leffler  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
24395b16c28cSSam Leffler  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
24405b16c28cSSam Leffler  */
24415b16c28cSSam Leffler #define	mix(a, b, c)							\
24425b16c28cSSam Leffler do {									\
24435b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 13);					\
24445b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 8);					\
24455b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 13);					\
24465b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 12);					\
24475b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 16);					\
24485b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 5);					\
24495b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 3);					\
24505b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 10);					\
24515b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 15);					\
24525b16c28cSSam Leffler } while (/*CONSTCOND*/0)
24535b16c28cSSam Leffler 
24545b16c28cSSam Leffler uint32_t
24555b16c28cSSam Leffler ieee80211_mac_hash(const struct ieee80211com *ic,
24565b16c28cSSam Leffler 	const uint8_t addr[IEEE80211_ADDR_LEN])
24575b16c28cSSam Leffler {
24585b16c28cSSam Leffler 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
24595b16c28cSSam Leffler 
24605b16c28cSSam Leffler 	b += addr[5] << 8;
24615b16c28cSSam Leffler 	b += addr[4];
24625b16c28cSSam Leffler 	a += addr[3] << 24;
24635b16c28cSSam Leffler 	a += addr[2] << 16;
24645b16c28cSSam Leffler 	a += addr[1] << 8;
24655b16c28cSSam Leffler 	a += addr[0];
24665b16c28cSSam Leffler 
24675b16c28cSSam Leffler 	mix(a, b, c);
24685b16c28cSSam Leffler 
24695b16c28cSSam Leffler 	return c;
24705b16c28cSSam Leffler }
24715b16c28cSSam Leffler #undef mix
2472a1cbd043SAdrian Chadd 
2473a1cbd043SAdrian Chadd char
2474a1cbd043SAdrian Chadd ieee80211_channel_type_char(const struct ieee80211_channel *c)
2475a1cbd043SAdrian Chadd {
2476a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ST(c))
2477a1cbd043SAdrian Chadd 		return 'S';
2478a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108A(c))
2479a1cbd043SAdrian Chadd 		return 'T';
2480a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108G(c))
2481a1cbd043SAdrian Chadd 		return 'G';
24827aebd3e5SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c))
24837aebd3e5SAdrian Chadd 		return 'v';
2484a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(c))
2485a1cbd043SAdrian Chadd 		return 'n';
2486a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_A(c))
2487a1cbd043SAdrian Chadd 		return 'a';
2488a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ANYG(c))
2489a1cbd043SAdrian Chadd 		return 'g';
2490a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_B(c))
2491a1cbd043SAdrian Chadd 		return 'b';
2492a1cbd043SAdrian Chadd 	return 'f';
2493a1cbd043SAdrian Chadd }
2494