xref: /freebsd/sys/net80211/ieee80211.c (revision 30fdd33ca35dceb87c8ba051b9957d9766333582)
11a1e1d21SSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
47535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
510ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
61a1e1d21SSam Leffler  * All rights reserved.
71a1e1d21SSam Leffler  *
81a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
91a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
101a1e1d21SSam Leffler  * are met:
111a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
137535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
147535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
157535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
161a1e1d21SSam Leffler  *
177535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271a1e1d21SSam Leffler  */
281a1e1d21SSam Leffler 
291a1e1d21SSam Leffler #include <sys/cdefs.h>
301a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
311a1e1d21SSam Leffler 
321a1e1d21SSam Leffler /*
331a1e1d21SSam Leffler  * IEEE 802.11 generic handler
341a1e1d21SSam Leffler  */
35b032f27cSSam Leffler #include "opt_wlan.h"
361a1e1d21SSam Leffler 
371a1e1d21SSam Leffler #include <sys/param.h>
381a1e1d21SSam Leffler #include <sys/systm.h>
391a1e1d21SSam Leffler #include <sys/kernel.h>
408ec07310SGleb Smirnoff #include <sys/malloc.h>
418a1b9b6aSSam Leffler #include <sys/socket.h>
427a79cebfSGleb Smirnoff #include <sys/sbuf.h>
431a1e1d21SSam Leffler 
44c8f5794eSGleb Smirnoff #include <machine/stdarg.h>
45c8f5794eSGleb Smirnoff 
461a1e1d21SSam Leffler #include <net/if.h>
4776039bc8SGleb Smirnoff #include <net/if_var.h>
48b032f27cSSam Leffler #include <net/if_dl.h>
491a1e1d21SSam Leffler #include <net/if_media.h>
50b032f27cSSam Leffler #include <net/if_types.h>
511a1e1d21SSam Leffler #include <net/ethernet.h>
521a1e1d21SSam Leffler 
531a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
54b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h>
55616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
56616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
57616190d0SSam Leffler #endif
58b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
5967f4aa38SAdrian Chadd #include <net80211/ieee80211_vht.h>
601a1e1d21SSam Leffler 
611a1e1d21SSam Leffler #include <net/bpf.h>
621a1e1d21SSam Leffler 
63bb77492fSSam Leffler const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
64bb77492fSSam Leffler 	[IEEE80211_MODE_AUTO]	  = "auto",
65bb77492fSSam Leffler 	[IEEE80211_MODE_11A]	  = "11a",
66bb77492fSSam Leffler 	[IEEE80211_MODE_11B]	  = "11b",
67bb77492fSSam Leffler 	[IEEE80211_MODE_11G]	  = "11g",
68bb77492fSSam Leffler 	[IEEE80211_MODE_FH]	  = "FH",
69bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_A]  = "turboA",
70bb77492fSSam Leffler 	[IEEE80211_MODE_TURBO_G]  = "turboG",
71bb77492fSSam Leffler 	[IEEE80211_MODE_STURBO_A] = "sturboA",
726a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	  = "half",
736a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]  = "quarter",
74bb77492fSSam Leffler 	[IEEE80211_MODE_11NA]	  = "11na",
75bb77492fSSam Leffler 	[IEEE80211_MODE_11NG]	  = "11ng",
760c67d389SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	  = "11acg",
770c67d389SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	  = "11ac",
781a1e1d21SSam Leffler };
79c43feedeSSam Leffler /* map ieee80211_opmode to the corresponding capability bit */
80c43feedeSSam Leffler const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
81c43feedeSSam Leffler 	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
82c43feedeSSam Leffler 	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
83c43feedeSSam Leffler 	[IEEE80211_M_STA]	= IEEE80211_C_STA,
84c43feedeSSam Leffler 	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
85c43feedeSSam Leffler 	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
86c43feedeSSam Leffler 	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
8759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
8859aa14a9SRui Paulo 	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
8959aa14a9SRui Paulo #endif
90c43feedeSSam Leffler };
91c43feedeSSam Leffler 
9292002144SGleb Smirnoff const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
93b032f27cSSam Leffler 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
94b032f27cSSam Leffler 
95b032f27cSSam Leffler static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
962bfc8a91SSam Leffler static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
97b032f27cSSam Leffler static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
988e71a4aaSAdrian Chadd static	void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
99b032f27cSSam Leffler static	int ieee80211_media_setup(struct ieee80211com *ic,
100b032f27cSSam Leffler 		struct ifmedia *media, int caps, int addsta,
101b032f27cSSam Leffler 		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
102b032f27cSSam Leffler static	int media_status(enum ieee80211_opmode,
103b032f27cSSam Leffler 		const struct ieee80211_channel *);
10428da1b56SGleb Smirnoff static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
105b032f27cSSam Leffler 
106b032f27cSSam Leffler MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
1071a1e1d21SSam Leffler 
108aadecb1aSSam Leffler /*
109aadecb1aSSam Leffler  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
110aadecb1aSSam Leffler  */
111aadecb1aSSam Leffler #define	B(r)	((r) | IEEE80211_RATE_BASIC)
112aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11a =
113aadecb1aSSam Leffler 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
11441b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_half =
11541b3c790SSam Leffler 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
11641b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_quarter =
11741b3c790SSam Leffler 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
118aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11b =
119aadecb1aSSam Leffler 	{ 4, { B(2), B(4), B(11), B(22) } };
120aadecb1aSSam Leffler /* NB: OFDM rates are handled specially based on mode */
121aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11g =
122aadecb1aSSam Leffler 	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
123aadecb1aSSam Leffler #undef B
124aadecb1aSSam Leffler 
12567f4aa38SAdrian Chadd static int set_vht_extchan(struct ieee80211_channel *c);
12667f4aa38SAdrian Chadd 
1271a1e1d21SSam Leffler /*
1281a1e1d21SSam Leffler  * Fill in 802.11 available channel set, mark
1291a1e1d21SSam Leffler  * all available channels as active, and pick
1301a1e1d21SSam Leffler  * a default channel if not already specified.
1311a1e1d21SSam Leffler  */
1327a79cebfSGleb Smirnoff void
13341b3c790SSam Leffler ieee80211_chan_init(struct ieee80211com *ic)
13441b3c790SSam Leffler {
13541b3c790SSam Leffler #define	DEFAULTRATES(m, def) do { \
1366a76ae21SSam Leffler 	if (ic->ic_sup_rates[m].rs_nrates == 0) \
13745fa8b0eSSam Leffler 		ic->ic_sup_rates[m] = def; \
13841b3c790SSam Leffler } while (0)
13941b3c790SSam Leffler 	struct ieee80211_channel *c;
14041b3c790SSam Leffler 	int i;
14141b3c790SSam Leffler 
14231378b1cSSam Leffler 	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
14368e8e04eSSam Leffler 		("invalid number of channels specified: %u", ic->ic_nchans));
1441a1e1d21SSam Leffler 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
145b032f27cSSam Leffler 	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
1466dbd16f1SSam Leffler 	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
14768e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1481a1e1d21SSam Leffler 		c = &ic->ic_channels[i];
14968e8e04eSSam Leffler 		KASSERT(c->ic_flags != 0, ("channel with no flags"));
1509c2c544dSSam Leffler 		/*
1519c2c544dSSam Leffler 		 * Help drivers that work only with frequencies by filling
1529c2c544dSSam Leffler 		 * in IEEE channel #'s if not already calculated.  Note this
1539c2c544dSSam Leffler 		 * mimics similar work done in ieee80211_setregdomain when
1549c2c544dSSam Leffler 		 * changing regulatory state.
1559c2c544dSSam Leffler 		 */
1569c2c544dSSam Leffler 		if (c->ic_ieee == 0)
1579c2c544dSSam Leffler 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
15867f4aa38SAdrian Chadd 
15967f4aa38SAdrian Chadd 		/*
16067f4aa38SAdrian Chadd 		 * Setup the HT40/VHT40 upper/lower bits.
16167f4aa38SAdrian Chadd 		 * The VHT80 math is done elsewhere.
16267f4aa38SAdrian Chadd 		 */
1639c2c544dSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
1649c2c544dSSam Leffler 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
1659c2c544dSSam Leffler 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
1669c2c544dSSam Leffler 			    c->ic_flags);
16767f4aa38SAdrian Chadd 
16867f4aa38SAdrian Chadd 		/* Update VHT math */
16967f4aa38SAdrian Chadd 		/*
17067f4aa38SAdrian Chadd 		 * XXX VHT again, note that this assumes VHT80 channels
17167f4aa38SAdrian Chadd 		 * are legit already
17267f4aa38SAdrian Chadd 		 */
17367f4aa38SAdrian Chadd 		set_vht_extchan(c);
17467f4aa38SAdrian Chadd 
1759c2c544dSSam Leffler 		/* default max tx power to max regulatory */
1769c2c544dSSam Leffler 		if (c->ic_maxpower == 0)
1779c2c544dSSam Leffler 			c->ic_maxpower = 2*c->ic_maxregpower;
17868e8e04eSSam Leffler 		setbit(ic->ic_chan_avail, c->ic_ieee);
1791a1e1d21SSam Leffler 		/*
1801a1e1d21SSam Leffler 		 * Identify mode capabilities.
1811a1e1d21SSam Leffler 		 */
1821a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_A(c))
1836dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
1841a1e1d21SSam Leffler 		if (IEEE80211_IS_CHAN_B(c))
1856dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
18645fa8b0eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(c))
1876dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
1884844aa7dSAtsushi Onoe 		if (IEEE80211_IS_CHAN_FHSS(c))
1896dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
19068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_108A(c))
1916dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
1928a1b9b6aSSam Leffler 		if (IEEE80211_IS_CHAN_108G(c))
1936dbd16f1SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
19468e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
19568e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
1966a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_HALF(c))
1976a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
1986a76ae21SSam Leffler 		if (IEEE80211_IS_CHAN_QUARTER(c))
1996a76ae21SSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
20068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTA(c))
20168e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
20268e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HTG(c))
20368e8e04eSSam Leffler 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
2040c67d389SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHTA(c))
2050c67d389SAdrian Chadd 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
2060c67d389SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHTG(c))
2070c67d389SAdrian Chadd 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
20868e8e04eSSam Leffler 	}
20968e8e04eSSam Leffler 	/* initialize candidate channels to all available */
21068e8e04eSSam Leffler 	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
21168e8e04eSSam Leffler 		sizeof(ic->ic_chan_avail));
21268e8e04eSSam Leffler 
213b032f27cSSam Leffler 	/* sort channel table to allow lookup optimizations */
214b032f27cSSam Leffler 	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
215b032f27cSSam Leffler 
216b032f27cSSam Leffler 	/* invalidate any previous state */
21768e8e04eSSam Leffler 	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
218ab562eefSSam Leffler 	ic->ic_prevchan = NULL;
219b032f27cSSam Leffler 	ic->ic_csa_newchan = NULL;
220b5c99415SSam Leffler 	/* arbitrarily pick the first channel */
22168e8e04eSSam Leffler 	ic->ic_curchan = &ic->ic_channels[0];
22226d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
223aadecb1aSSam Leffler 
224aadecb1aSSam Leffler 	/* fillin well-known rate sets if driver has not specified */
22541b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
22641b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
22741b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
22841b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
22941b3c790SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
2308500d65dSSam Leffler 	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
2316a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
2326a76ae21SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
23340432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
23440432d36SSam Leffler 	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
2350c67d389SAdrian Chadd 	DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ,	 ieee80211_rateset_11g);
2360c67d389SAdrian Chadd 	DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ,	 ieee80211_rateset_11a);
23741b3c790SSam Leffler 
23841b3c790SSam Leffler 	/*
239fbbe47a9SBernhard Schmidt 	 * Setup required information to fill the mcsset field, if driver did
240fbbe47a9SBernhard Schmidt 	 * not. Assume a 2T2R setup for historic reasons.
241fbbe47a9SBernhard Schmidt 	 */
242fbbe47a9SBernhard Schmidt 	if (ic->ic_rxstream == 0)
243fbbe47a9SBernhard Schmidt 		ic->ic_rxstream = 2;
244fbbe47a9SBernhard Schmidt 	if (ic->ic_txstream == 0)
245fbbe47a9SBernhard Schmidt 		ic->ic_txstream = 2;
246fbbe47a9SBernhard Schmidt 
247dfabbaa0SAndriy Voskoboinyk 	ieee80211_init_suphtrates(ic);
248dfabbaa0SAndriy Voskoboinyk 
249fbbe47a9SBernhard Schmidt 	/*
25041b3c790SSam Leffler 	 * Set auto mode to reset active channel state and any desired channel.
25141b3c790SSam Leffler 	 */
25241b3c790SSam Leffler 	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
25341b3c790SSam Leffler #undef DEFAULTRATES
25441b3c790SSam Leffler }
25541b3c790SSam Leffler 
256b032f27cSSam Leffler static void
257272f6adeSGleb Smirnoff null_update_mcast(struct ieee80211com *ic)
258b032f27cSSam Leffler {
259272f6adeSGleb Smirnoff 
260272f6adeSGleb Smirnoff 	ic_printf(ic, "need multicast update callback\n");
261b032f27cSSam Leffler }
262b032f27cSSam Leffler 
263b032f27cSSam Leffler static void
264272f6adeSGleb Smirnoff null_update_promisc(struct ieee80211com *ic)
265b032f27cSSam Leffler {
266272f6adeSGleb Smirnoff 
267272f6adeSGleb Smirnoff 	ic_printf(ic, "need promiscuous mode update callback\n");
268b032f27cSSam Leffler }
269b032f27cSSam Leffler 
270b94299c4SAdrian Chadd static void
271b94299c4SAdrian Chadd null_update_chw(struct ieee80211com *ic)
272b94299c4SAdrian Chadd {
273b94299c4SAdrian Chadd 
274c8f5794eSGleb Smirnoff 	ic_printf(ic, "%s: need callback\n", __func__);
275c8f5794eSGleb Smirnoff }
276c8f5794eSGleb Smirnoff 
277c8f5794eSGleb Smirnoff int
278c8f5794eSGleb Smirnoff ic_printf(struct ieee80211com *ic, const char * fmt, ...)
279c8f5794eSGleb Smirnoff {
280c8f5794eSGleb Smirnoff 	va_list ap;
281c8f5794eSGleb Smirnoff 	int retval;
282c8f5794eSGleb Smirnoff 
283c8f5794eSGleb Smirnoff 	retval = printf("%s: ", ic->ic_name);
284c8f5794eSGleb Smirnoff 	va_start(ap, fmt);
285c8f5794eSGleb Smirnoff 	retval += vprintf(fmt, ap);
286c8f5794eSGleb Smirnoff 	va_end(ap);
287c8f5794eSGleb Smirnoff 	return (retval);
288b94299c4SAdrian Chadd }
289b94299c4SAdrian Chadd 
2907a79cebfSGleb Smirnoff static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
2917a79cebfSGleb Smirnoff static struct mtx ic_list_mtx;
2927a79cebfSGleb Smirnoff MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
2937a79cebfSGleb Smirnoff 
2947a79cebfSGleb Smirnoff static int
2957a79cebfSGleb Smirnoff sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
2967a79cebfSGleb Smirnoff {
2977a79cebfSGleb Smirnoff 	struct ieee80211com *ic;
298f09a089eSAndriy Voskoboinyk 	struct sbuf sb;
2997a79cebfSGleb Smirnoff 	char *sp;
3007a79cebfSGleb Smirnoff 	int error;
3017a79cebfSGleb Smirnoff 
302f09a089eSAndriy Voskoboinyk 	error = sysctl_wire_old_buffer(req, 0);
303f09a089eSAndriy Voskoboinyk 	if (error)
304f09a089eSAndriy Voskoboinyk 		return (error);
305f09a089eSAndriy Voskoboinyk 	sbuf_new_for_sysctl(&sb, NULL, 8, req);
306f09a089eSAndriy Voskoboinyk 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
3077a79cebfSGleb Smirnoff 	sp = "";
3087a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3097a79cebfSGleb Smirnoff 	LIST_FOREACH(ic, &ic_head, ic_next) {
310f09a089eSAndriy Voskoboinyk 		sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
3117a79cebfSGleb Smirnoff 		sp = " ";
3127a79cebfSGleb Smirnoff 	}
3137a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
314f09a089eSAndriy Voskoboinyk 	error = sbuf_finish(&sb);
315f09a089eSAndriy Voskoboinyk 	sbuf_delete(&sb);
3167a79cebfSGleb Smirnoff 	return (error);
3177a79cebfSGleb Smirnoff }
3187a79cebfSGleb Smirnoff 
3197a79cebfSGleb Smirnoff SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
3207a79cebfSGleb Smirnoff     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3217a79cebfSGleb Smirnoff     sysctl_ieee80211coms, "A", "names of available 802.11 devices");
3227a79cebfSGleb Smirnoff 
323b032f27cSSam Leffler /*
324b032f27cSSam Leffler  * Attach/setup the common net80211 state.  Called by
325b032f27cSSam Leffler  * the driver on attach to prior to creating any vap's.
326b032f27cSSam Leffler  */
32741b3c790SSam Leffler void
3287a79cebfSGleb Smirnoff ieee80211_ifattach(struct ieee80211com *ic)
32941b3c790SSam Leffler {
33041b3c790SSam Leffler 
331c8f5794eSGleb Smirnoff 	IEEE80211_LOCK_INIT(ic, ic->ic_name);
332c8f5794eSGleb Smirnoff 	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
333b032f27cSSam Leffler 	TAILQ_INIT(&ic->ic_vaps);
3345efea30fSAndrew Thompson 
3355efea30fSAndrew Thompson 	/* Create a taskqueue for all state changes */
3365efea30fSAndrew Thompson 	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
3375efea30fSAndrew Thompson 	    taskqueue_thread_enqueue, &ic->ic_tq);
3387b2b15ebSAdrian Chadd 	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
3397fc10b6bSGleb Smirnoff 	    ic->ic_name);
34028da1b56SGleb Smirnoff 	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
34128da1b56SGleb Smirnoff 	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
34241b3c790SSam Leffler 	/*
34341b3c790SSam Leffler 	 * Fill in 802.11 available channel set, mark all
34441b3c790SSam Leffler 	 * available channels as active, and pick a default
34541b3c790SSam Leffler 	 * channel if not already specified.
34641b3c790SSam Leffler 	 */
3477a79cebfSGleb Smirnoff 	ieee80211_chan_init(ic);
34868e8e04eSSam Leffler 
349b032f27cSSam Leffler 	ic->ic_update_mcast = null_update_mcast;
350b032f27cSSam Leffler 	ic->ic_update_promisc = null_update_promisc;
351b94299c4SAdrian Chadd 	ic->ic_update_chw = null_update_chw;
3521a1e1d21SSam Leffler 
3535b16c28cSSam Leffler 	ic->ic_hash_key = arc4random();
354d365f9c7SSam Leffler 	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
355d365f9c7SSam Leffler 	ic->ic_lintval = ic->ic_bintval;
3568a1b9b6aSSam Leffler 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
3578a1b9b6aSSam Leffler 
35868e8e04eSSam Leffler 	ieee80211_crypto_attach(ic);
3598a1b9b6aSSam Leffler 	ieee80211_node_attach(ic);
36068e8e04eSSam Leffler 	ieee80211_power_attach(ic);
3618a1b9b6aSSam Leffler 	ieee80211_proto_attach(ic);
362616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
363616190d0SSam Leffler 	ieee80211_superg_attach(ic);
364616190d0SSam Leffler #endif
36568e8e04eSSam Leffler 	ieee80211_ht_attach(ic);
36667f4aa38SAdrian Chadd 	ieee80211_vht_attach(ic);
36768e8e04eSSam Leffler 	ieee80211_scan_attach(ic);
368b032f27cSSam Leffler 	ieee80211_regdomain_attach(ic);
369e95e0edbSSam Leffler 	ieee80211_dfs_attach(ic);
3708a1b9b6aSSam Leffler 
371b032f27cSSam Leffler 	ieee80211_sysctl_attach(ic);
3728a1b9b6aSSam Leffler 
3737a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3747a79cebfSGleb Smirnoff 	LIST_INSERT_HEAD(&ic_head, ic, ic_next);
3757a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
3761a1e1d21SSam Leffler }
3771a1e1d21SSam Leffler 
378b032f27cSSam Leffler /*
379b032f27cSSam Leffler  * Detach net80211 state on device detach.  Tear down
380b032f27cSSam Leffler  * all vap's and reclaim all common state prior to the
381b032f27cSSam Leffler  * device state going away.  Note we may call back into
382b032f27cSSam Leffler  * driver; it must be prepared for this.
383b032f27cSSam Leffler  */
3841a1e1d21SSam Leffler void
3858a1b9b6aSSam Leffler ieee80211_ifdetach(struct ieee80211com *ic)
3861a1e1d21SSam Leffler {
387b032f27cSSam Leffler 	struct ieee80211vap *vap;
3881a1e1d21SSam Leffler 
389a84a458cSKyle Evans 	/*
390a84a458cSKyle Evans 	 * We use this as an indicator that ifattach never had a chance to be
391a84a458cSKyle Evans 	 * called, e.g. early driver attach failed and ifdetach was called
392a84a458cSKyle Evans 	 * during subsequent detach.  Never fear, for we have nothing to do
393a84a458cSKyle Evans 	 * here.
394a84a458cSKyle Evans 	 */
395a84a458cSKyle Evans 	if (ic->ic_tq == NULL)
396a84a458cSKyle Evans 		return;
397a84a458cSKyle Evans 
3987a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
3997a79cebfSGleb Smirnoff 	LIST_REMOVE(ic, ic_next);
4007a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
4015c600a90SSam Leffler 
4024061c639SAndriy Voskoboinyk 	taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
4034061c639SAndriy Voskoboinyk 
40430e4856aSAdrian Chadd 	/*
40530e4856aSAdrian Chadd 	 * The VAP is responsible for setting and clearing
40630e4856aSAdrian Chadd 	 * the VIMAGE context.
40730e4856aSAdrian Chadd 	 */
408dab61567SAndriy Voskoboinyk 	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) {
409dab61567SAndriy Voskoboinyk 		ieee80211_com_vdetach(vap);
410b032f27cSSam Leffler 		ieee80211_vap_destroy(vap);
411dab61567SAndriy Voskoboinyk 	}
412ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
4138a1b9b6aSSam Leffler 
4148a1b9b6aSSam Leffler 	ieee80211_sysctl_detach(ic);
415e95e0edbSSam Leffler 	ieee80211_dfs_detach(ic);
416b032f27cSSam Leffler 	ieee80211_regdomain_detach(ic);
41768e8e04eSSam Leffler 	ieee80211_scan_detach(ic);
418616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
419616190d0SSam Leffler 	ieee80211_superg_detach(ic);
420616190d0SSam Leffler #endif
42167f4aa38SAdrian Chadd 	ieee80211_vht_detach(ic);
42268e8e04eSSam Leffler 	ieee80211_ht_detach(ic);
423ca4ac7aeSSam Leffler 	/* NB: must be called before ieee80211_node_detach */
4248a1b9b6aSSam Leffler 	ieee80211_proto_detach(ic);
4258a1b9b6aSSam Leffler 	ieee80211_crypto_detach(ic);
42668e8e04eSSam Leffler 	ieee80211_power_detach(ic);
4278a1b9b6aSSam Leffler 	ieee80211_node_detach(ic);
4288a1b9b6aSSam Leffler 
42928da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_ierrors);
43028da1b56SGleb Smirnoff 	counter_u64_free(ic->ic_oerrors);
43130e4856aSAdrian Chadd 
4325efea30fSAndrew Thompson 	taskqueue_free(ic->ic_tq);
4335cda6006SAdrian Chadd 	IEEE80211_TX_LOCK_DESTROY(ic);
43468e8e04eSSam Leffler 	IEEE80211_LOCK_DESTROY(ic);
435b032f27cSSam Leffler }
4368a1b9b6aSSam Leffler 
4377a79cebfSGleb Smirnoff struct ieee80211com *
4387a79cebfSGleb Smirnoff ieee80211_find_com(const char *name)
4397a79cebfSGleb Smirnoff {
4407a79cebfSGleb Smirnoff 	struct ieee80211com *ic;
4417a79cebfSGleb Smirnoff 
4427a79cebfSGleb Smirnoff 	mtx_lock(&ic_list_mtx);
4437a79cebfSGleb Smirnoff 	LIST_FOREACH(ic, &ic_head, ic_next)
4447a79cebfSGleb Smirnoff 		if (strcmp(ic->ic_name, name) == 0)
4457a79cebfSGleb Smirnoff 			break;
4467a79cebfSGleb Smirnoff 	mtx_unlock(&ic_list_mtx);
4477a79cebfSGleb Smirnoff 
4487a79cebfSGleb Smirnoff 	return (ic);
4497a79cebfSGleb Smirnoff }
4507a79cebfSGleb Smirnoff 
4517cde0202SAndriy Voskoboinyk void
4527cde0202SAndriy Voskoboinyk ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
4537cde0202SAndriy Voskoboinyk {
4547cde0202SAndriy Voskoboinyk 	struct ieee80211com *ic;
4557cde0202SAndriy Voskoboinyk 
4567cde0202SAndriy Voskoboinyk 	mtx_lock(&ic_list_mtx);
4577cde0202SAndriy Voskoboinyk 	LIST_FOREACH(ic, &ic_head, ic_next)
4587cde0202SAndriy Voskoboinyk 		(*f)(arg, ic);
4597cde0202SAndriy Voskoboinyk 	mtx_unlock(&ic_list_mtx);
4607cde0202SAndriy Voskoboinyk }
4617cde0202SAndriy Voskoboinyk 
462b032f27cSSam Leffler /*
463b032f27cSSam Leffler  * Default reset method for use with the ioctl support.  This
464b032f27cSSam Leffler  * method is invoked after any state change in the 802.11
465b032f27cSSam Leffler  * layer that should be propagated to the hardware but not
466b032f27cSSam Leffler  * require re-initialization of the 802.11 state machine (e.g
467b032f27cSSam Leffler  * rescanning for an ap).  We always return ENETRESET which
468b032f27cSSam Leffler  * should cause the driver to re-initialize the device. Drivers
469b032f27cSSam Leffler  * can override this method to implement more optimized support.
470b032f27cSSam Leffler  */
471b032f27cSSam Leffler static int
472b032f27cSSam Leffler default_reset(struct ieee80211vap *vap, u_long cmd)
473b032f27cSSam Leffler {
474b032f27cSSam Leffler 	return ENETRESET;
475b032f27cSSam Leffler }
476b032f27cSSam Leffler 
477b032f27cSSam Leffler /*
478781487cfSAdrian Chadd  * Default for updating the VAP default TX key index.
479781487cfSAdrian Chadd  *
480781487cfSAdrian Chadd  * Drivers that support TX offload as well as hardware encryption offload
481781487cfSAdrian Chadd  * may need to be informed of key index changes separate from the key
482781487cfSAdrian Chadd  * update.
483781487cfSAdrian Chadd  */
484781487cfSAdrian Chadd static void
485781487cfSAdrian Chadd default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
486781487cfSAdrian Chadd {
487781487cfSAdrian Chadd 
488781487cfSAdrian Chadd 	/* XXX assert validity */
489781487cfSAdrian Chadd 	/* XXX assert we're in a key update block */
490781487cfSAdrian Chadd 	vap->iv_def_txkey = kid;
491781487cfSAdrian Chadd }
492781487cfSAdrian Chadd 
493781487cfSAdrian Chadd /*
49428da1b56SGleb Smirnoff  * Add underlying device errors to vap errors.
49528da1b56SGleb Smirnoff  */
49628da1b56SGleb Smirnoff static uint64_t
49728da1b56SGleb Smirnoff ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
49828da1b56SGleb Smirnoff {
49928da1b56SGleb Smirnoff 	struct ieee80211vap *vap = ifp->if_softc;
50028da1b56SGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
50128da1b56SGleb Smirnoff 	uint64_t rv;
50228da1b56SGleb Smirnoff 
50328da1b56SGleb Smirnoff 	rv = if_get_counter_default(ifp, cnt);
50428da1b56SGleb Smirnoff 	switch (cnt) {
50528da1b56SGleb Smirnoff 	case IFCOUNTER_OERRORS:
50628da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_oerrors);
50728da1b56SGleb Smirnoff 		break;
50828da1b56SGleb Smirnoff 	case IFCOUNTER_IERRORS:
50928da1b56SGleb Smirnoff 		rv += counter_u64_fetch(ic->ic_ierrors);
51028da1b56SGleb Smirnoff 		break;
51128da1b56SGleb Smirnoff 	default:
51228da1b56SGleb Smirnoff 		break;
51328da1b56SGleb Smirnoff 	}
51428da1b56SGleb Smirnoff 
51528da1b56SGleb Smirnoff 	return (rv);
51628da1b56SGleb Smirnoff }
51728da1b56SGleb Smirnoff 
51828da1b56SGleb Smirnoff /*
519b032f27cSSam Leffler  * Prepare a vap for use.  Drivers use this call to
520b032f27cSSam Leffler  * setup net80211 state in new vap's prior attaching
521b032f27cSSam Leffler  * them with ieee80211_vap_attach (below).
522b032f27cSSam Leffler  */
523b032f27cSSam Leffler int
524b032f27cSSam Leffler ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
525fcd9500fSBernhard Schmidt     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
5267a79cebfSGleb Smirnoff     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
527b032f27cSSam Leffler {
528b032f27cSSam Leffler 	struct ifnet *ifp;
529b032f27cSSam Leffler 
530b032f27cSSam Leffler 	ifp = if_alloc(IFT_ETHER);
531b032f27cSSam Leffler 	if (ifp == NULL) {
532372c7b95SBjoern A. Zeeb 		ic_printf(ic, "%s: unable to allocate ifnet\n", __func__);
533b032f27cSSam Leffler 		return ENOMEM;
534b032f27cSSam Leffler 	}
535b032f27cSSam Leffler 	if_initname(ifp, name, unit);
536b032f27cSSam Leffler 	ifp->if_softc = vap;			/* back pointer */
537b032f27cSSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
538e7495198SAdrian Chadd 	ifp->if_transmit = ieee80211_vap_transmit;
539e7495198SAdrian Chadd 	ifp->if_qflush = ieee80211_vap_qflush;
540b032f27cSSam Leffler 	ifp->if_ioctl = ieee80211_ioctl;
541b032f27cSSam Leffler 	ifp->if_init = ieee80211_init;
54228da1b56SGleb Smirnoff 	ifp->if_get_counter = ieee80211_get_counter;
543b032f27cSSam Leffler 
544b032f27cSSam Leffler 	vap->iv_ifp = ifp;
545b032f27cSSam Leffler 	vap->iv_ic = ic;
546b032f27cSSam Leffler 	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
547b032f27cSSam Leffler 	vap->iv_flags_ext = ic->ic_flags_ext;
548b032f27cSSam Leffler 	vap->iv_flags_ven = ic->ic_flags_ven;
549b032f27cSSam Leffler 	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
55067f4aa38SAdrian Chadd 
55167f4aa38SAdrian Chadd 	/* 11n capabilities - XXX methodize */
552b032f27cSSam Leffler 	vap->iv_htcaps = ic->ic_htcaps;
553e1d36f83SRui Paulo 	vap->iv_htextcaps = ic->ic_htextcaps;
55467f4aa38SAdrian Chadd 
55567f4aa38SAdrian Chadd 	/* 11ac capabilities - XXX methodize */
55667f4aa38SAdrian Chadd 	vap->iv_vhtcaps = ic->ic_vhtcaps;
55767f4aa38SAdrian Chadd 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
55867f4aa38SAdrian Chadd 
559b032f27cSSam Leffler 	vap->iv_opmode = opmode;
560c43feedeSSam Leffler 	vap->iv_caps |= ieee80211_opcap[opmode];
5611d47c76cSAndriy Voskoboinyk 	IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
562b032f27cSSam Leffler 	switch (opmode) {
563b032f27cSSam Leffler 	case IEEE80211_M_WDS:
564b032f27cSSam Leffler 		/*
565b032f27cSSam Leffler 		 * WDS links must specify the bssid of the far end.
566b032f27cSSam Leffler 		 * For legacy operation this is a static relationship.
567b032f27cSSam Leffler 		 * For non-legacy operation the station must associate
568b032f27cSSam Leffler 		 * and be authorized to pass traffic.  Plumbing the
569b032f27cSSam Leffler 		 * vap to the proper node happens when the vap
570b032f27cSSam Leffler 		 * transitions to RUN state.
571b032f27cSSam Leffler 		 */
572b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
573b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_DESBSSID;
574b032f27cSSam Leffler 		if (flags & IEEE80211_CLONE_WDSLEGACY)
575b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
576b032f27cSSam Leffler 		break;
57710ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
57810ad9a77SSam Leffler 	case IEEE80211_M_AHDEMO:
57910ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
58010ad9a77SSam Leffler 			/* NB: checked before clone operation allowed */
58110ad9a77SSam Leffler 			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
58210ad9a77SSam Leffler 			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
58310ad9a77SSam Leffler 			/*
58410ad9a77SSam Leffler 			 * Propagate TDMA capability to mark vap; this
58510ad9a77SSam Leffler 			 * cannot be removed and is used to distinguish
58610ad9a77SSam Leffler 			 * regular ahdemo operation from ahdemo+tdma.
58710ad9a77SSam Leffler 			 */
58810ad9a77SSam Leffler 			vap->iv_caps |= IEEE80211_C_TDMA;
58910ad9a77SSam Leffler 		}
59010ad9a77SSam Leffler 		break;
59110ad9a77SSam Leffler #endif
592fcd9500fSBernhard Schmidt 	default:
593fcd9500fSBernhard Schmidt 		break;
594b032f27cSSam Leffler 	}
595ae3f00bbSSam Leffler 	/* auto-enable s/w beacon miss support */
596ae3f00bbSSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS)
597ae3f00bbSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
59883fcb812SAndrew Thompson 	/* auto-generated or user supplied MAC address */
59983fcb812SAndrew Thompson 	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
60083fcb812SAndrew Thompson 		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
601b032f27cSSam Leffler 	/*
602b032f27cSSam Leffler 	 * Enable various functionality by default if we're
603b032f27cSSam Leffler 	 * capable; the driver can override us if it knows better.
604b032f27cSSam Leffler 	 */
605b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_WME)
606b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_WME;
607b032f27cSSam Leffler 	if (vap->iv_caps & IEEE80211_C_BURST)
608b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BURST;
609b032f27cSSam Leffler 	/* NB: bg scanning only makes sense for station mode right now */
610b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA &&
611b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_BGSCAN))
612b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_BGSCAN;
613c43feedeSSam Leffler 	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
61482fd2577SSam Leffler 	/* NB: DFS support only makes sense for ap mode right now */
61582fd2577SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
61682fd2577SSam Leffler 	    (vap->iv_caps & IEEE80211_C_DFS))
617b032f27cSSam Leffler 		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
6188379e8dbSAdrian Chadd 	/* NB: only flip on U-APSD for hostap/sta for now */
6198379e8dbSAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_STA)
6208379e8dbSAdrian Chadd 	    || (vap->iv_opmode == IEEE80211_M_HOSTAP)) {
6218379e8dbSAdrian Chadd 		if (vap->iv_caps & IEEE80211_C_UAPSD)
6228379e8dbSAdrian Chadd 			vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD;
6238379e8dbSAdrian Chadd 	}
624b032f27cSSam Leffler 
625b032f27cSSam Leffler 	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
626b032f27cSSam Leffler 	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
627b032f27cSSam Leffler 	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
628b032f27cSSam Leffler 	/*
629b032f27cSSam Leffler 	 * Install a default reset method for the ioctl support;
630b032f27cSSam Leffler 	 * the driver can override this.
631b032f27cSSam Leffler 	 */
632b032f27cSSam Leffler 	vap->iv_reset = default_reset;
633b032f27cSSam Leffler 
634781487cfSAdrian Chadd 	/*
635781487cfSAdrian Chadd 	 * Install a default crypto key update method, the driver
636781487cfSAdrian Chadd 	 * can override this.
637781487cfSAdrian Chadd 	 */
638781487cfSAdrian Chadd 	vap->iv_update_deftxkey = default_update_deftxkey;
639781487cfSAdrian Chadd 
640b032f27cSSam Leffler 	ieee80211_sysctl_vattach(vap);
641b032f27cSSam Leffler 	ieee80211_crypto_vattach(vap);
642b032f27cSSam Leffler 	ieee80211_node_vattach(vap);
643b032f27cSSam Leffler 	ieee80211_power_vattach(vap);
644b032f27cSSam Leffler 	ieee80211_proto_vattach(vap);
645616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
646616190d0SSam Leffler 	ieee80211_superg_vattach(vap);
647616190d0SSam Leffler #endif
648b032f27cSSam Leffler 	ieee80211_ht_vattach(vap);
64967f4aa38SAdrian Chadd 	ieee80211_vht_vattach(vap);
650b032f27cSSam Leffler 	ieee80211_scan_vattach(vap);
651b032f27cSSam Leffler 	ieee80211_regdomain_vattach(vap);
6525463c4a4SSam Leffler 	ieee80211_radiotap_vattach(vap);
653d20ff6e6SAdrian Chadd 	ieee80211_vap_reset_erp(vap);
654a7c6aabdSBernhard Schmidt 	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
655b6108616SRui Paulo 
656b032f27cSSam Leffler 	return 0;
657b032f27cSSam Leffler }
658b032f27cSSam Leffler 
659b032f27cSSam Leffler /*
660b032f27cSSam Leffler  * Activate a vap.  State should have been prepared with a
661b032f27cSSam Leffler  * call to ieee80211_vap_setup and by the driver.  On return
662b032f27cSSam Leffler  * from this call the vap is ready for use.
663b032f27cSSam Leffler  */
664b032f27cSSam Leffler int
6657a79cebfSGleb Smirnoff ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
6667a79cebfSGleb Smirnoff     ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
667b032f27cSSam Leffler {
668b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
669b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
670b032f27cSSam Leffler 	struct ifmediareq imr;
671b032f27cSSam Leffler 	int maxrate;
672b032f27cSSam Leffler 
673b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
674b032f27cSSam Leffler 	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
675b032f27cSSam Leffler 	    __func__, ieee80211_opmode_name[vap->iv_opmode],
676c8f5794eSGleb Smirnoff 	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
677b032f27cSSam Leffler 
678b032f27cSSam Leffler 	/*
679b032f27cSSam Leffler 	 * Do late attach work that cannot happen until after
680b032f27cSSam Leffler 	 * the driver has had a chance to override defaults.
681b032f27cSSam Leffler 	 */
682b032f27cSSam Leffler 	ieee80211_node_latevattach(vap);
683b032f27cSSam Leffler 	ieee80211_power_latevattach(vap);
684b032f27cSSam Leffler 
685b032f27cSSam Leffler 	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
686b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
687b032f27cSSam Leffler 	ieee80211_media_status(ifp, &imr);
688b032f27cSSam Leffler 	/* NB: strip explicit mode; we're actually in autoselect */
689c3f10abdSSam Leffler 	ifmedia_set(&vap->iv_media,
690c3f10abdSSam Leffler 	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
691b032f27cSSam Leffler 	if (maxrate)
692b032f27cSSam Leffler 		ifp->if_baudrate = IF_Mbps(maxrate);
693b032f27cSSam Leffler 
6947a79cebfSGleb Smirnoff 	ether_ifattach(ifp, macaddr);
6951d47c76cSAndriy Voskoboinyk 	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
696b032f27cSSam Leffler 	/* hook output method setup by ether_ifattach */
697b032f27cSSam Leffler 	vap->iv_output = ifp->if_output;
698b032f27cSSam Leffler 	ifp->if_output = ieee80211_output;
699b032f27cSSam Leffler 	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
700b032f27cSSam Leffler 
701b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
702b032f27cSSam Leffler 	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
703b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
704616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
705b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
706616190d0SSam Leffler #endif
707b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
708b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
7092bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
7102bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
7118e71a4aaSAdrian Chadd 
7128e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
7138e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
7148e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
7158e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
7168e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
717b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
718b032f27cSSam Leffler 
719b032f27cSSam Leffler 	return 1;
720b032f27cSSam Leffler }
721b032f27cSSam Leffler 
722b032f27cSSam Leffler /*
723b032f27cSSam Leffler  * Tear down vap state and reclaim the ifnet.
724b032f27cSSam Leffler  * The driver is assumed to have prepared for
725b032f27cSSam Leffler  * this; e.g. by turning off interrupts for the
726b032f27cSSam Leffler  * underlying device.
727b032f27cSSam Leffler  */
728b032f27cSSam Leffler void
729b032f27cSSam Leffler ieee80211_vap_detach(struct ieee80211vap *vap)
730b032f27cSSam Leffler {
731b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
732b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
733b032f27cSSam Leffler 
73430e4856aSAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
73530e4856aSAdrian Chadd 
736b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
7377fc10b6bSGleb Smirnoff 	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
738b032f27cSSam Leffler 
7391da89db5SSam Leffler 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
7401da89db5SSam Leffler 	ether_ifdetach(ifp);
7411da89db5SSam Leffler 
7421da89db5SSam Leffler 	ieee80211_stop(vap);
743b032f27cSSam Leffler 
7445efea30fSAndrew Thompson 	/*
7455efea30fSAndrew Thompson 	 * Flush any deferred vap tasks.
7465efea30fSAndrew Thompson 	 */
7475efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_nstate_task);
7485efea30fSAndrew Thompson 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
749e3e94c96SAdrian Chadd 	ieee80211_draintask(ic, &vap->iv_wme_task);
750e2db307eSAndriy Voskoboinyk 	ieee80211_draintask(ic, &ic->ic_parent_task);
7515efea30fSAndrew Thompson 
752ab501dd6SSam Leffler 	/* XXX band-aid until ifnet handles this for us */
753ab501dd6SSam Leffler 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
754ab501dd6SSam Leffler 
7555efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
7565efea30fSAndrew Thompson 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
757b032f27cSSam Leffler 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
758b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
759616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
760b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
761616190d0SSam Leffler #endif
762b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
763b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
7642bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
7652bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
7668e71a4aaSAdrian Chadd 
7678e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
7688e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
7698e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
7708e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
7718e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
7728e71a4aaSAdrian Chadd 
7735463c4a4SSam Leffler 	/* NB: this handles the bpfdetach done below */
7745463c4a4SSam Leffler 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
7757a79cebfSGleb Smirnoff 	if (vap->iv_ifflags & IFF_PROMISC)
7767a79cebfSGleb Smirnoff 		ieee80211_promisc(vap, false);
7777a79cebfSGleb Smirnoff 	if (vap->iv_ifflags & IFF_ALLMULTI)
7787a79cebfSGleb Smirnoff 		ieee80211_allmulti(vap, false);
779b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
780b032f27cSSam Leffler 
781b032f27cSSam Leffler 	ifmedia_removeall(&vap->iv_media);
782b032f27cSSam Leffler 
7835463c4a4SSam Leffler 	ieee80211_radiotap_vdetach(vap);
784b032f27cSSam Leffler 	ieee80211_regdomain_vdetach(vap);
785b032f27cSSam Leffler 	ieee80211_scan_vdetach(vap);
786616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
787616190d0SSam Leffler 	ieee80211_superg_vdetach(vap);
788616190d0SSam Leffler #endif
78967f4aa38SAdrian Chadd 	ieee80211_vht_vdetach(vap);
790b032f27cSSam Leffler 	ieee80211_ht_vdetach(vap);
791b032f27cSSam Leffler 	/* NB: must be before ieee80211_node_vdetach */
792b032f27cSSam Leffler 	ieee80211_proto_vdetach(vap);
793b032f27cSSam Leffler 	ieee80211_crypto_vdetach(vap);
794b032f27cSSam Leffler 	ieee80211_power_vdetach(vap);
795b032f27cSSam Leffler 	ieee80211_node_vdetach(vap);
796b032f27cSSam Leffler 	ieee80211_sysctl_vdetach(vap);
797b20f0ed1SWeongyo Jeong 
798b20f0ed1SWeongyo Jeong 	if_free(ifp);
79930e4856aSAdrian Chadd 
80030e4856aSAdrian Chadd 	CURVNET_RESTORE();
801b032f27cSSam Leffler }
802b032f27cSSam Leffler 
803b032f27cSSam Leffler /*
8047a79cebfSGleb Smirnoff  * Count number of vaps in promisc, and issue promisc on
8057a79cebfSGleb Smirnoff  * parent respectively.
806b032f27cSSam Leffler  */
807b032f27cSSam Leffler void
8087a79cebfSGleb Smirnoff ieee80211_promisc(struct ieee80211vap *vap, bool on)
809b032f27cSSam Leffler {
8107a79cebfSGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
811b032f27cSSam Leffler 
812c6427be9SAndriy Voskoboinyk 	IEEE80211_LOCK_ASSERT(ic);
813c6427be9SAndriy Voskoboinyk 
8147a79cebfSGleb Smirnoff 	if (on) {
8157a79cebfSGleb Smirnoff 		if (++ic->ic_promisc == 1)
816ba2c1fbcSAdrian Chadd 			ieee80211_runtask(ic, &ic->ic_promisc_task);
8177a79cebfSGleb Smirnoff 	} else {
8187a79cebfSGleb Smirnoff 		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
8197a79cebfSGleb Smirnoff 		    __func__, ic));
8207a79cebfSGleb Smirnoff 		if (--ic->ic_promisc == 0)
8217a79cebfSGleb Smirnoff 			ieee80211_runtask(ic, &ic->ic_promisc_task);
8227a79cebfSGleb Smirnoff 	}
8237a79cebfSGleb Smirnoff }
8247a79cebfSGleb Smirnoff 
8257a79cebfSGleb Smirnoff /*
8267a79cebfSGleb Smirnoff  * Count number of vaps in allmulti, and issue allmulti on
8277a79cebfSGleb Smirnoff  * parent respectively.
8287a79cebfSGleb Smirnoff  */
8297a79cebfSGleb Smirnoff void
8307a79cebfSGleb Smirnoff ieee80211_allmulti(struct ieee80211vap *vap, bool on)
8317a79cebfSGleb Smirnoff {
8327a79cebfSGleb Smirnoff 	struct ieee80211com *ic = vap->iv_ic;
8337a79cebfSGleb Smirnoff 
834c6427be9SAndriy Voskoboinyk 	IEEE80211_LOCK_ASSERT(ic);
835c6427be9SAndriy Voskoboinyk 
8367a79cebfSGleb Smirnoff 	if (on) {
8377a79cebfSGleb Smirnoff 		if (++ic->ic_allmulti == 1)
8387a79cebfSGleb Smirnoff 			ieee80211_runtask(ic, &ic->ic_mcast_task);
8397a79cebfSGleb Smirnoff 	} else {
8407a79cebfSGleb Smirnoff 		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
8417a79cebfSGleb Smirnoff 		    __func__, ic));
8427a79cebfSGleb Smirnoff 		if (--ic->ic_allmulti == 0)
8435efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_mcast_task);
844b032f27cSSam Leffler 	}
845b032f27cSSam Leffler }
846b032f27cSSam Leffler 
847b032f27cSSam Leffler /*
848b032f27cSSam Leffler  * Synchronize flag bit state in the com structure
849b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
850b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
851b032f27cSSam Leffler  */
852b032f27cSSam Leffler static void
853b032f27cSSam Leffler ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
854b032f27cSSam Leffler {
855b032f27cSSam Leffler 	struct ieee80211vap *vap;
856b032f27cSSam Leffler 	int bit;
857b032f27cSSam Leffler 
858b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
859b032f27cSSam Leffler 
860b032f27cSSam Leffler 	bit = 0;
861b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
862b032f27cSSam Leffler 		if (vap->iv_flags & flag) {
863b032f27cSSam Leffler 			bit = 1;
864b032f27cSSam Leffler 			break;
865b032f27cSSam Leffler 		}
866b032f27cSSam Leffler 	if (bit)
867b032f27cSSam Leffler 		ic->ic_flags |= flag;
868b032f27cSSam Leffler 	else
869b032f27cSSam Leffler 		ic->ic_flags &= ~flag;
870b032f27cSSam Leffler }
871b032f27cSSam Leffler 
872b032f27cSSam Leffler void
873b032f27cSSam Leffler ieee80211_syncflag(struct ieee80211vap *vap, int flag)
874b032f27cSSam Leffler {
875b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
876b032f27cSSam Leffler 
877b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
878b032f27cSSam Leffler 	if (flag < 0) {
879b032f27cSSam Leffler 		flag = -flag;
880b032f27cSSam Leffler 		vap->iv_flags &= ~flag;
881b032f27cSSam Leffler 	} else
882b032f27cSSam Leffler 		vap->iv_flags |= flag;
883b032f27cSSam Leffler 	ieee80211_syncflag_locked(ic, flag);
884b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
885b032f27cSSam Leffler }
886b032f27cSSam Leffler 
887b032f27cSSam Leffler /*
8882bfc8a91SSam Leffler  * Synchronize flags_ht bit state in the com structure
8892bfc8a91SSam Leffler  * according to the state of all vap's.  This is used,
8902bfc8a91SSam Leffler  * for example, to handle state changes via ioctls.
8912bfc8a91SSam Leffler  */
8922bfc8a91SSam Leffler static void
8932bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
8942bfc8a91SSam Leffler {
8952bfc8a91SSam Leffler 	struct ieee80211vap *vap;
8962bfc8a91SSam Leffler 	int bit;
8972bfc8a91SSam Leffler 
8982bfc8a91SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
8992bfc8a91SSam Leffler 
9002bfc8a91SSam Leffler 	bit = 0;
9012bfc8a91SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
9022bfc8a91SSam Leffler 		if (vap->iv_flags_ht & flag) {
9032bfc8a91SSam Leffler 			bit = 1;
9042bfc8a91SSam Leffler 			break;
9052bfc8a91SSam Leffler 		}
9062bfc8a91SSam Leffler 	if (bit)
9072bfc8a91SSam Leffler 		ic->ic_flags_ht |= flag;
9082bfc8a91SSam Leffler 	else
9092bfc8a91SSam Leffler 		ic->ic_flags_ht &= ~flag;
9102bfc8a91SSam Leffler }
9112bfc8a91SSam Leffler 
9122bfc8a91SSam Leffler void
9132bfc8a91SSam Leffler ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
9142bfc8a91SSam Leffler {
9152bfc8a91SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
9162bfc8a91SSam Leffler 
9172bfc8a91SSam Leffler 	IEEE80211_LOCK(ic);
9182bfc8a91SSam Leffler 	if (flag < 0) {
9192bfc8a91SSam Leffler 		flag = -flag;
9202bfc8a91SSam Leffler 		vap->iv_flags_ht &= ~flag;
9212bfc8a91SSam Leffler 	} else
9222bfc8a91SSam Leffler 		vap->iv_flags_ht |= flag;
9232bfc8a91SSam Leffler 	ieee80211_syncflag_ht_locked(ic, flag);
9242bfc8a91SSam Leffler 	IEEE80211_UNLOCK(ic);
9252bfc8a91SSam Leffler }
9262bfc8a91SSam Leffler 
9272bfc8a91SSam Leffler /*
9288e71a4aaSAdrian Chadd  * Synchronize flags_vht bit state in the com structure
9298e71a4aaSAdrian Chadd  * according to the state of all vap's.  This is used,
9308e71a4aaSAdrian Chadd  * for example, to handle state changes via ioctls.
9318e71a4aaSAdrian Chadd  */
9328e71a4aaSAdrian Chadd static void
9338e71a4aaSAdrian Chadd ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
9348e71a4aaSAdrian Chadd {
9358e71a4aaSAdrian Chadd 	struct ieee80211vap *vap;
9368e71a4aaSAdrian Chadd 	int bit;
9378e71a4aaSAdrian Chadd 
9388e71a4aaSAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
9398e71a4aaSAdrian Chadd 
9408e71a4aaSAdrian Chadd 	bit = 0;
9418e71a4aaSAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
9428e71a4aaSAdrian Chadd 		if (vap->iv_flags_vht & flag) {
9438e71a4aaSAdrian Chadd 			bit = 1;
9448e71a4aaSAdrian Chadd 			break;
9458e71a4aaSAdrian Chadd 		}
9468e71a4aaSAdrian Chadd 	if (bit)
9478e71a4aaSAdrian Chadd 		ic->ic_flags_vht |= flag;
9488e71a4aaSAdrian Chadd 	else
9498e71a4aaSAdrian Chadd 		ic->ic_flags_vht &= ~flag;
9508e71a4aaSAdrian Chadd }
9518e71a4aaSAdrian Chadd 
9528e71a4aaSAdrian Chadd void
9538e71a4aaSAdrian Chadd ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
9548e71a4aaSAdrian Chadd {
9558e71a4aaSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
9568e71a4aaSAdrian Chadd 
9578e71a4aaSAdrian Chadd 	IEEE80211_LOCK(ic);
9588e71a4aaSAdrian Chadd 	if (flag < 0) {
9598e71a4aaSAdrian Chadd 		flag = -flag;
9608e71a4aaSAdrian Chadd 		vap->iv_flags_vht &= ~flag;
9618e71a4aaSAdrian Chadd 	} else
9628e71a4aaSAdrian Chadd 		vap->iv_flags_vht |= flag;
9638e71a4aaSAdrian Chadd 	ieee80211_syncflag_vht_locked(ic, flag);
9648e71a4aaSAdrian Chadd 	IEEE80211_UNLOCK(ic);
9658e71a4aaSAdrian Chadd }
9668e71a4aaSAdrian Chadd 
9678e71a4aaSAdrian Chadd /*
9682bfc8a91SSam Leffler  * Synchronize flags_ext bit state in the com structure
969b032f27cSSam Leffler  * according to the state of all vap's.  This is used,
970b032f27cSSam Leffler  * for example, to handle state changes via ioctls.
971b032f27cSSam Leffler  */
972b032f27cSSam Leffler static void
973b032f27cSSam Leffler ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
974b032f27cSSam Leffler {
975b032f27cSSam Leffler 	struct ieee80211vap *vap;
976b032f27cSSam Leffler 	int bit;
977b032f27cSSam Leffler 
978b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
979b032f27cSSam Leffler 
980b032f27cSSam Leffler 	bit = 0;
981b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
982b032f27cSSam Leffler 		if (vap->iv_flags_ext & flag) {
983b032f27cSSam Leffler 			bit = 1;
984b032f27cSSam Leffler 			break;
985b032f27cSSam Leffler 		}
986b032f27cSSam Leffler 	if (bit)
987b032f27cSSam Leffler 		ic->ic_flags_ext |= flag;
988b032f27cSSam Leffler 	else
989b032f27cSSam Leffler 		ic->ic_flags_ext &= ~flag;
990b032f27cSSam Leffler }
991b032f27cSSam Leffler 
992b032f27cSSam Leffler void
993b032f27cSSam Leffler ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
994b032f27cSSam Leffler {
995b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
996b032f27cSSam Leffler 
997b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
998b032f27cSSam Leffler 	if (flag < 0) {
999b032f27cSSam Leffler 		flag = -flag;
1000b032f27cSSam Leffler 		vap->iv_flags_ext &= ~flag;
1001b032f27cSSam Leffler 	} else
1002b032f27cSSam Leffler 		vap->iv_flags_ext |= flag;
1003b032f27cSSam Leffler 	ieee80211_syncflag_ext_locked(ic, flag);
1004b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
10051a1e1d21SSam Leffler }
10061a1e1d21SSam Leffler 
1007ca4ac7aeSSam Leffler static __inline int
1008ca4ac7aeSSam Leffler mapgsm(u_int freq, u_int flags)
1009ca4ac7aeSSam Leffler {
1010ca4ac7aeSSam Leffler 	freq *= 10;
1011ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_QUARTER)
1012ca4ac7aeSSam Leffler 		freq += 5;
1013ca4ac7aeSSam Leffler 	else if (flags & IEEE80211_CHAN_HALF)
1014ca4ac7aeSSam Leffler 		freq += 10;
1015ca4ac7aeSSam Leffler 	else
1016ca4ac7aeSSam Leffler 		freq += 20;
1017ca4ac7aeSSam Leffler 	/* NB: there is no 907/20 wide but leave room */
1018ca4ac7aeSSam Leffler 	return (freq - 906*10) / 5;
1019ca4ac7aeSSam Leffler }
1020ca4ac7aeSSam Leffler 
1021ca4ac7aeSSam Leffler static __inline int
1022ca4ac7aeSSam Leffler mappsb(u_int freq, u_int flags)
1023ca4ac7aeSSam Leffler {
1024ca4ac7aeSSam Leffler 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1025ca4ac7aeSSam Leffler }
1026ca4ac7aeSSam Leffler 
10271a1e1d21SSam Leffler /*
10281a1e1d21SSam Leffler  * Convert MHz frequency to IEEE channel number.
10291a1e1d21SSam Leffler  */
10306f322b78SSam Leffler int
10311a1e1d21SSam Leffler ieee80211_mhz2ieee(u_int freq, u_int flags)
10321a1e1d21SSam Leffler {
103311df4239SSam Leffler #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1034ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
1035ca4ac7aeSSam Leffler 		return mapgsm(freq, flags);
10361a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
10371a1e1d21SSam Leffler 		if (freq == 2484)
10381a1e1d21SSam Leffler 			return 14;
10391a1e1d21SSam Leffler 		if (freq < 2484)
10406f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
10411a1e1d21SSam Leffler 		else
10421a1e1d21SSam Leffler 			return 15 + ((freq - 2512) / 20);
1043c032abb5SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
104441b3c790SSam Leffler 		if (freq <= 5000) {
104568e8e04eSSam Leffler 			/* XXX check regdomain? */
104611df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
1047ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
10486f322b78SSam Leffler 			return (freq - 4000) / 5;
104941b3c790SSam Leffler 		} else
10501a1e1d21SSam Leffler 			return (freq - 5000) / 5;
10511a1e1d21SSam Leffler 	} else {				/* either, guess */
10521a1e1d21SSam Leffler 		if (freq == 2484)
10531a1e1d21SSam Leffler 			return 14;
1054ca4ac7aeSSam Leffler 		if (freq < 2484) {
1055ca4ac7aeSSam Leffler 			if (907 <= freq && freq <= 922)
1056ca4ac7aeSSam Leffler 				return mapgsm(freq, flags);
10576f322b78SSam Leffler 			return ((int) freq - 2407) / 5;
1058ca4ac7aeSSam Leffler 		}
10596f322b78SSam Leffler 		if (freq < 5000) {
106011df4239SSam Leffler 			if (IS_FREQ_IN_PSB(freq))
1061ca4ac7aeSSam Leffler 				return mappsb(freq, flags);
106241b3c790SSam Leffler 			else if (freq > 4900)
10636f322b78SSam Leffler 				return (freq - 4000) / 5;
10646f322b78SSam Leffler 			else
10651a1e1d21SSam Leffler 				return 15 + ((freq - 2512) / 20);
10666f322b78SSam Leffler 		}
10671a1e1d21SSam Leffler 		return (freq - 5000) / 5;
10681a1e1d21SSam Leffler 	}
106911df4239SSam Leffler #undef IS_FREQ_IN_PSB
10701a1e1d21SSam Leffler }
10711a1e1d21SSam Leffler 
10721a1e1d21SSam Leffler /*
10731a1e1d21SSam Leffler  * Convert channel to IEEE channel number.
10741a1e1d21SSam Leffler  */
10756f322b78SSam Leffler int
107638da1496SMatt Jacob ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
10771a1e1d21SSam Leffler {
107868e8e04eSSam Leffler 	if (c == NULL) {
1079c8f5794eSGleb Smirnoff 		ic_printf(ic, "invalid channel (NULL)\n");
10808be0d570SSam Leffler 		return 0;		/* XXX */
10811a1e1d21SSam Leffler 	}
108268e8e04eSSam Leffler 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
10831a1e1d21SSam Leffler }
10841a1e1d21SSam Leffler 
10851a1e1d21SSam Leffler /*
10861a1e1d21SSam Leffler  * Convert IEEE channel number to MHz frequency.
10871a1e1d21SSam Leffler  */
10881a1e1d21SSam Leffler u_int
10891a1e1d21SSam Leffler ieee80211_ieee2mhz(u_int chan, u_int flags)
10901a1e1d21SSam Leffler {
1091ca4ac7aeSSam Leffler 	if (flags & IEEE80211_CHAN_GSM)
1092ca4ac7aeSSam Leffler 		return 907 + 5 * (chan / 10);
10931a1e1d21SSam Leffler 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
10941a1e1d21SSam Leffler 		if (chan == 14)
10951a1e1d21SSam Leffler 			return 2484;
10961a1e1d21SSam Leffler 		if (chan < 14)
10971a1e1d21SSam Leffler 			return 2407 + chan*5;
10981a1e1d21SSam Leffler 		else
10991a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
11001a1e1d21SSam Leffler 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
110141b3c790SSam Leffler 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
110241b3c790SSam Leffler 			chan -= 37;
110341b3c790SSam Leffler 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
110441b3c790SSam Leffler 		}
11051a1e1d21SSam Leffler 		return 5000 + (chan*5);
11061a1e1d21SSam Leffler 	} else {				/* either, guess */
1107ca4ac7aeSSam Leffler 		/* XXX can't distinguish PSB+GSM channels */
11081a1e1d21SSam Leffler 		if (chan == 14)
11091a1e1d21SSam Leffler 			return 2484;
11101a1e1d21SSam Leffler 		if (chan < 14)			/* 0-13 */
11111a1e1d21SSam Leffler 			return 2407 + chan*5;
11121a1e1d21SSam Leffler 		if (chan < 27)			/* 15-26 */
11131a1e1d21SSam Leffler 			return 2512 + ((chan-15)*20);
11141a1e1d21SSam Leffler 		return 5000 + (chan*5);
11151a1e1d21SSam Leffler 	}
11161a1e1d21SSam Leffler }
11171a1e1d21SSam Leffler 
1118355fec48SAndriy Voskoboinyk static __inline void
1119355fec48SAndriy Voskoboinyk set_extchan(struct ieee80211_channel *c)
1120355fec48SAndriy Voskoboinyk {
1121355fec48SAndriy Voskoboinyk 
1122355fec48SAndriy Voskoboinyk 	/*
1123355fec48SAndriy Voskoboinyk 	 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1124355fec48SAndriy Voskoboinyk 	 * "the secondary channel number shall be 'N + [1,-1] * 4'
1125355fec48SAndriy Voskoboinyk 	 */
1126355fec48SAndriy Voskoboinyk 	if (c->ic_flags & IEEE80211_CHAN_HT40U)
1127355fec48SAndriy Voskoboinyk 		c->ic_extieee = c->ic_ieee + 4;
1128355fec48SAndriy Voskoboinyk 	else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1129355fec48SAndriy Voskoboinyk 		c->ic_extieee = c->ic_ieee - 4;
1130355fec48SAndriy Voskoboinyk 	else
1131355fec48SAndriy Voskoboinyk 		c->ic_extieee = 0;
1132355fec48SAndriy Voskoboinyk }
1133355fec48SAndriy Voskoboinyk 
113467f4aa38SAdrian Chadd /*
113567f4aa38SAdrian Chadd  * Populate the freq1/freq2 fields as appropriate for VHT channels.
113667f4aa38SAdrian Chadd  *
113767f4aa38SAdrian Chadd  * This for now uses a hard-coded list of 80MHz wide channels.
113867f4aa38SAdrian Chadd  *
113967f4aa38SAdrian Chadd  * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
114067f4aa38SAdrian Chadd  * wide channel we've already decided upon.
114167f4aa38SAdrian Chadd  *
114267f4aa38SAdrian Chadd  * For VHT80 and VHT160, there are only a small number of fixed
114367f4aa38SAdrian Chadd  * 80/160MHz wide channels, so we just use those.
114467f4aa38SAdrian Chadd  *
114567f4aa38SAdrian Chadd  * This is all likely very very wrong - both the regulatory code
114667f4aa38SAdrian Chadd  * and this code needs to ensure that all four channels are
114767f4aa38SAdrian Chadd  * available and valid before the VHT80 (and eight for VHT160) channel
114867f4aa38SAdrian Chadd  * is created.
114967f4aa38SAdrian Chadd  */
115067f4aa38SAdrian Chadd 
115167f4aa38SAdrian Chadd struct vht_chan_range {
115267f4aa38SAdrian Chadd 	uint16_t freq_start;
115367f4aa38SAdrian Chadd 	uint16_t freq_end;
115467f4aa38SAdrian Chadd };
115567f4aa38SAdrian Chadd 
115667f4aa38SAdrian Chadd struct vht_chan_range vht80_chan_ranges[] = {
115767f4aa38SAdrian Chadd 	{ 5170, 5250 },
115867f4aa38SAdrian Chadd 	{ 5250, 5330 },
115967f4aa38SAdrian Chadd 	{ 5490, 5570 },
116067f4aa38SAdrian Chadd 	{ 5570, 5650 },
116167f4aa38SAdrian Chadd 	{ 5650, 5730 },
116267f4aa38SAdrian Chadd 	{ 5735, 5815 },
116367f4aa38SAdrian Chadd 	{ 0, 0, }
116467f4aa38SAdrian Chadd };
116567f4aa38SAdrian Chadd 
116667f4aa38SAdrian Chadd static int
116767f4aa38SAdrian Chadd set_vht_extchan(struct ieee80211_channel *c)
116867f4aa38SAdrian Chadd {
116967f4aa38SAdrian Chadd 	int i;
117067f4aa38SAdrian Chadd 
1171*30fdd33cSBjoern A. Zeeb 	if (! IEEE80211_IS_CHAN_VHT(c))
117267f4aa38SAdrian Chadd 		return (0);
1173*30fdd33cSBjoern A. Zeeb 
1174*30fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT80P80(c)) {
1175*30fdd33cSBjoern A. Zeeb 		printf("%s: TODO VHT80+80 channel (ieee=%d, flags=0x%08x)\n",
1176*30fdd33cSBjoern A. Zeeb 		    __func__, c->ic_ieee, c->ic_flags);
117767f4aa38SAdrian Chadd 	}
117867f4aa38SAdrian Chadd 
1179*30fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT160(c)) {
1180*30fdd33cSBjoern A. Zeeb 		printf("%s: TODO VHT160 channel (ieee=%d, flags=0x%08x)\n",
1181*30fdd33cSBjoern A. Zeeb 		    __func__, c->ic_ieee, c->ic_flags);
118267f4aa38SAdrian Chadd 	}
118367f4aa38SAdrian Chadd 
118467f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT80(c)) {
118567f4aa38SAdrian Chadd 		for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
118667f4aa38SAdrian Chadd 			if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
118767f4aa38SAdrian Chadd 			    c->ic_freq < vht80_chan_ranges[i].freq_end) {
118867f4aa38SAdrian Chadd 				int midpoint;
118967f4aa38SAdrian Chadd 
119067f4aa38SAdrian Chadd 				midpoint = vht80_chan_ranges[i].freq_start + 40;
119167f4aa38SAdrian Chadd 				c->ic_vht_ch_freq1 =
119267f4aa38SAdrian Chadd 				    ieee80211_mhz2ieee(midpoint, c->ic_flags);
119367f4aa38SAdrian Chadd 				c->ic_vht_ch_freq2 = 0;
119467f4aa38SAdrian Chadd #if 0
119567f4aa38SAdrian Chadd 				printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
119667f4aa38SAdrian Chadd 				    __func__, c->ic_ieee, c->ic_freq, midpoint,
119767f4aa38SAdrian Chadd 				    c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
119867f4aa38SAdrian Chadd #endif
119967f4aa38SAdrian Chadd 				return (1);
120067f4aa38SAdrian Chadd 			}
120167f4aa38SAdrian Chadd 		}
120267f4aa38SAdrian Chadd 		return (0);
120367f4aa38SAdrian Chadd 	}
120467f4aa38SAdrian Chadd 
1205*30fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT40(c)) {
1206*30fdd33cSBjoern A. Zeeb 		if (IEEE80211_IS_CHAN_HT40U(c))
1207*30fdd33cSBjoern A. Zeeb 			c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1208*30fdd33cSBjoern A. Zeeb 		else if (IEEE80211_IS_CHAN_HT40D(c))
1209*30fdd33cSBjoern A. Zeeb 			c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1210*30fdd33cSBjoern A. Zeeb 		else
1211*30fdd33cSBjoern A. Zeeb 			return (0);
1212*30fdd33cSBjoern A. Zeeb 		return (1);
1213*30fdd33cSBjoern A. Zeeb 	}
1214*30fdd33cSBjoern A. Zeeb 
1215*30fdd33cSBjoern A. Zeeb 	if (IEEE80211_IS_CHAN_VHT20(c)) {
1216*30fdd33cSBjoern A. Zeeb 		c->ic_vht_ch_freq1 = c->ic_ieee;
1217*30fdd33cSBjoern A. Zeeb 		return (1);
1218*30fdd33cSBjoern A. Zeeb 	}
1219*30fdd33cSBjoern A. Zeeb 
122067f4aa38SAdrian Chadd 	printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1221372c7b95SBjoern A. Zeeb 	    __func__, c->ic_ieee, c->ic_flags);
122267f4aa38SAdrian Chadd 
122367f4aa38SAdrian Chadd 	return (0);
122467f4aa38SAdrian Chadd }
122567f4aa38SAdrian Chadd 
122667f4aa38SAdrian Chadd /*
122767f4aa38SAdrian Chadd  * Return whether the current channel could possibly be a part of
122867f4aa38SAdrian Chadd  * a VHT80 channel.
122967f4aa38SAdrian Chadd  *
123067f4aa38SAdrian Chadd  * This doesn't check that the whole range is in the allowed list
123167f4aa38SAdrian Chadd  * according to regulatory.
123267f4aa38SAdrian Chadd  */
123367f4aa38SAdrian Chadd static int
123467f4aa38SAdrian Chadd is_vht80_valid_freq(uint16_t freq)
123567f4aa38SAdrian Chadd {
123667f4aa38SAdrian Chadd 	int i;
123767f4aa38SAdrian Chadd 	for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
123867f4aa38SAdrian Chadd 		if (freq >= vht80_chan_ranges[i].freq_start &&
123967f4aa38SAdrian Chadd 		    freq < vht80_chan_ranges[i].freq_end)
124067f4aa38SAdrian Chadd 			return (1);
124167f4aa38SAdrian Chadd 	}
124267f4aa38SAdrian Chadd 	return (0);
124367f4aa38SAdrian Chadd }
124467f4aa38SAdrian Chadd 
1245355fec48SAndriy Voskoboinyk static int
1246355fec48SAndriy Voskoboinyk addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1247355fec48SAndriy Voskoboinyk     uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1248355fec48SAndriy Voskoboinyk {
1249355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1250355fec48SAndriy Voskoboinyk 
1251355fec48SAndriy Voskoboinyk 	if (*nchans >= maxchans)
1252355fec48SAndriy Voskoboinyk 		return (ENOBUFS);
1253355fec48SAndriy Voskoboinyk 
125467f4aa38SAdrian Chadd #if 0
125567f4aa38SAdrian Chadd 	printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
1256372c7b95SBjoern A. Zeeb 	    __func__, *nchans, ieee, freq, flags);
125767f4aa38SAdrian Chadd #endif
125867f4aa38SAdrian Chadd 
1259355fec48SAndriy Voskoboinyk 	c = &chans[(*nchans)++];
1260355fec48SAndriy Voskoboinyk 	c->ic_ieee = ieee;
1261355fec48SAndriy Voskoboinyk 	c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1262355fec48SAndriy Voskoboinyk 	c->ic_maxregpower = maxregpower;
1263355fec48SAndriy Voskoboinyk 	c->ic_maxpower = 2 * maxregpower;
1264355fec48SAndriy Voskoboinyk 	c->ic_flags = flags;
126567f4aa38SAdrian Chadd 	c->ic_vht_ch_freq1 = 0;
126667f4aa38SAdrian Chadd 	c->ic_vht_ch_freq2 = 0;
1267355fec48SAndriy Voskoboinyk 	set_extchan(c);
126867f4aa38SAdrian Chadd 	set_vht_extchan(c);
1269355fec48SAndriy Voskoboinyk 
1270355fec48SAndriy Voskoboinyk 	return (0);
1271355fec48SAndriy Voskoboinyk }
1272355fec48SAndriy Voskoboinyk 
1273355fec48SAndriy Voskoboinyk static int
1274355fec48SAndriy Voskoboinyk copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1275355fec48SAndriy Voskoboinyk     uint32_t flags)
1276355fec48SAndriy Voskoboinyk {
1277355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1278355fec48SAndriy Voskoboinyk 
1279355fec48SAndriy Voskoboinyk 	KASSERT(*nchans > 0, ("channel list is empty\n"));
1280355fec48SAndriy Voskoboinyk 
1281355fec48SAndriy Voskoboinyk 	if (*nchans >= maxchans)
1282355fec48SAndriy Voskoboinyk 		return (ENOBUFS);
1283355fec48SAndriy Voskoboinyk 
128467f4aa38SAdrian Chadd #if 0
128567f4aa38SAdrian Chadd 	printf("%s: %d: flags=0x%08x\n",
1286372c7b95SBjoern A. Zeeb 	    __func__, *nchans, flags);
128767f4aa38SAdrian Chadd #endif
128867f4aa38SAdrian Chadd 
1289355fec48SAndriy Voskoboinyk 	c = &chans[(*nchans)++];
1290355fec48SAndriy Voskoboinyk 	c[0] = c[-1];
1291355fec48SAndriy Voskoboinyk 	c->ic_flags = flags;
129267f4aa38SAdrian Chadd 	c->ic_vht_ch_freq1 = 0;
129367f4aa38SAdrian Chadd 	c->ic_vht_ch_freq2 = 0;
1294355fec48SAndriy Voskoboinyk 	set_extchan(c);
129567f4aa38SAdrian Chadd 	set_vht_extchan(c);
1296355fec48SAndriy Voskoboinyk 
1297355fec48SAndriy Voskoboinyk 	return (0);
1298355fec48SAndriy Voskoboinyk }
1299355fec48SAndriy Voskoboinyk 
130067f4aa38SAdrian Chadd /*
130167f4aa38SAdrian Chadd  * XXX VHT-2GHz
130267f4aa38SAdrian Chadd  */
1303355fec48SAndriy Voskoboinyk static void
1304355fec48SAndriy Voskoboinyk getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1305355fec48SAndriy Voskoboinyk {
1306355fec48SAndriy Voskoboinyk 	int nmodes;
1307355fec48SAndriy Voskoboinyk 
1308355fec48SAndriy Voskoboinyk 	nmodes = 0;
1309355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11B))
1310355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_B;
1311355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11G))
1312355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G;
1313355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11NG))
1314355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1315355fec48SAndriy Voskoboinyk 	if (ht40) {
1316355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1317355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1318355fec48SAndriy Voskoboinyk 	}
1319355fec48SAndriy Voskoboinyk 	flags[nmodes] = 0;
1320355fec48SAndriy Voskoboinyk }
1321355fec48SAndriy Voskoboinyk 
1322355fec48SAndriy Voskoboinyk static void
132367f4aa38SAdrian Chadd getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1324355fec48SAndriy Voskoboinyk {
1325355fec48SAndriy Voskoboinyk 	int nmodes;
1326355fec48SAndriy Voskoboinyk 
132767f4aa38SAdrian Chadd 	/*
132867f4aa38SAdrian Chadd 	 * the addchan_list function seems to expect the flags array to
132967f4aa38SAdrian Chadd 	 * be in channel width order, so the VHT bits are interspersed
133067f4aa38SAdrian Chadd 	 * as appropriate to maintain said order.
133167f4aa38SAdrian Chadd 	 *
133267f4aa38SAdrian Chadd 	 * It also assumes HT40U is before HT40D.
133367f4aa38SAdrian Chadd 	 */
1334355fec48SAndriy Voskoboinyk 	nmodes = 0;
133567f4aa38SAdrian Chadd 
133667f4aa38SAdrian Chadd 	/* 20MHz */
1337355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11A))
1338355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A;
1339355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11NA))
1340355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
134167f4aa38SAdrian Chadd 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
134267f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
134367f4aa38SAdrian Chadd 		    IEEE80211_CHAN_VHT20;
134498ff1f7cSAndriy Voskoboinyk 	}
134567f4aa38SAdrian Chadd 
134667f4aa38SAdrian Chadd 	/* 40MHz */
1347355fec48SAndriy Voskoboinyk 	if (ht40) {
1348355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
134967f4aa38SAdrian Chadd 	}
135067f4aa38SAdrian Chadd 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
135167f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
135267f4aa38SAdrian Chadd 		    | IEEE80211_CHAN_VHT40U;
135367f4aa38SAdrian Chadd 	}
135467f4aa38SAdrian Chadd 	if (ht40) {
1355355fec48SAndriy Voskoboinyk 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1356355fec48SAndriy Voskoboinyk 	}
135767f4aa38SAdrian Chadd 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
135867f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
135967f4aa38SAdrian Chadd 		    | IEEE80211_CHAN_VHT40D;
136067f4aa38SAdrian Chadd 	}
136167f4aa38SAdrian Chadd 
136267f4aa38SAdrian Chadd 	/* 80MHz */
136367f4aa38SAdrian Chadd 	if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
136467f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A |
136567f4aa38SAdrian Chadd 		    IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80;
136667f4aa38SAdrian Chadd 		flags[nmodes++] = IEEE80211_CHAN_A |
136767f4aa38SAdrian Chadd 		    IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80;
136867f4aa38SAdrian Chadd 	}
136967f4aa38SAdrian Chadd 
137067f4aa38SAdrian Chadd 	/* XXX VHT160 */
13714b1c2487SBjoern A. Zeeb 	/* XXX VHT80+80 */
1372355fec48SAndriy Voskoboinyk 	flags[nmodes] = 0;
1373355fec48SAndriy Voskoboinyk }
1374355fec48SAndriy Voskoboinyk 
1375355fec48SAndriy Voskoboinyk static void
137667f4aa38SAdrian Chadd getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1377355fec48SAndriy Voskoboinyk {
1378355fec48SAndriy Voskoboinyk 
1379355fec48SAndriy Voskoboinyk 	flags[0] = 0;
1380355fec48SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11A) ||
138167f4aa38SAdrian Chadd 	    isset(bands, IEEE80211_MODE_11NA) ||
138267f4aa38SAdrian Chadd 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1383355fec48SAndriy Voskoboinyk 		if (isset(bands, IEEE80211_MODE_11B) ||
1384355fec48SAndriy Voskoboinyk 		    isset(bands, IEEE80211_MODE_11G) ||
138567f4aa38SAdrian Chadd 		    isset(bands, IEEE80211_MODE_11NG) ||
138667f4aa38SAdrian Chadd 		    isset(bands, IEEE80211_MODE_VHT_2GHZ))
1387355fec48SAndriy Voskoboinyk 			return;
1388355fec48SAndriy Voskoboinyk 
138967f4aa38SAdrian Chadd 		getflags_5ghz(bands, flags, ht40, vht80);
1390355fec48SAndriy Voskoboinyk 	} else
1391355fec48SAndriy Voskoboinyk 		getflags_2ghz(bands, flags, ht40);
1392355fec48SAndriy Voskoboinyk }
1393355fec48SAndriy Voskoboinyk 
1394355fec48SAndriy Voskoboinyk /*
1395355fec48SAndriy Voskoboinyk  * Add one 20 MHz channel into specified channel list.
1396cd02c6b1SBjoern A. Zeeb  * You MUST NOT mix bands when calling this.  It will not add 5ghz
1397cd02c6b1SBjoern A. Zeeb  * channels if you have any B/G/N band bit set.
1398355fec48SAndriy Voskoboinyk  */
139967f4aa38SAdrian Chadd /* XXX VHT */
1400355fec48SAndriy Voskoboinyk int
1401355fec48SAndriy Voskoboinyk ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1402355fec48SAndriy Voskoboinyk     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1403355fec48SAndriy Voskoboinyk     uint32_t chan_flags, const uint8_t bands[])
1404355fec48SAndriy Voskoboinyk {
1405355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
1406355fec48SAndriy Voskoboinyk 	int i, error;
1407355fec48SAndriy Voskoboinyk 
140867f4aa38SAdrian Chadd 	getflags(bands, flags, 0, 0);
1409355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1410355fec48SAndriy Voskoboinyk 
1411355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1412355fec48SAndriy Voskoboinyk 	    flags[0] | chan_flags);
1413355fec48SAndriy Voskoboinyk 	for (i = 1; flags[i] != 0 && error == 0; i++) {
1414355fec48SAndriy Voskoboinyk 		error = copychan_prev(chans, maxchans, nchans,
1415355fec48SAndriy Voskoboinyk 		    flags[i] | chan_flags);
1416355fec48SAndriy Voskoboinyk 	}
1417355fec48SAndriy Voskoboinyk 
1418355fec48SAndriy Voskoboinyk 	return (error);
1419355fec48SAndriy Voskoboinyk }
1420355fec48SAndriy Voskoboinyk 
1421355fec48SAndriy Voskoboinyk static struct ieee80211_channel *
1422355fec48SAndriy Voskoboinyk findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1423355fec48SAndriy Voskoboinyk     uint32_t flags)
1424355fec48SAndriy Voskoboinyk {
1425355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *c;
1426355fec48SAndriy Voskoboinyk 	int i;
1427355fec48SAndriy Voskoboinyk 
1428355fec48SAndriy Voskoboinyk 	flags &= IEEE80211_CHAN_ALLTURBO;
1429355fec48SAndriy Voskoboinyk 	/* brute force search */
1430355fec48SAndriy Voskoboinyk 	for (i = 0; i < nchans; i++) {
1431355fec48SAndriy Voskoboinyk 		c = &chans[i];
1432355fec48SAndriy Voskoboinyk 		if (c->ic_freq == freq &&
1433355fec48SAndriy Voskoboinyk 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1434355fec48SAndriy Voskoboinyk 			return c;
1435355fec48SAndriy Voskoboinyk 	}
1436355fec48SAndriy Voskoboinyk 	return NULL;
1437355fec48SAndriy Voskoboinyk }
1438355fec48SAndriy Voskoboinyk 
1439355fec48SAndriy Voskoboinyk /*
1440355fec48SAndriy Voskoboinyk  * Add 40 MHz channel pair into specified channel list.
1441355fec48SAndriy Voskoboinyk  */
144267f4aa38SAdrian Chadd /* XXX VHT */
1443355fec48SAndriy Voskoboinyk int
1444355fec48SAndriy Voskoboinyk ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1445355fec48SAndriy Voskoboinyk     int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1446355fec48SAndriy Voskoboinyk {
1447355fec48SAndriy Voskoboinyk 	struct ieee80211_channel *cent, *extc;
1448355fec48SAndriy Voskoboinyk 	uint16_t freq;
1449355fec48SAndriy Voskoboinyk 	int error;
1450355fec48SAndriy Voskoboinyk 
1451355fec48SAndriy Voskoboinyk 	freq = ieee80211_ieee2mhz(ieee, flags);
1452355fec48SAndriy Voskoboinyk 
1453355fec48SAndriy Voskoboinyk 	/*
1454355fec48SAndriy Voskoboinyk 	 * Each entry defines an HT40 channel pair; find the
1455355fec48SAndriy Voskoboinyk 	 * center channel, then the extension channel above.
1456355fec48SAndriy Voskoboinyk 	 */
1457355fec48SAndriy Voskoboinyk 	flags |= IEEE80211_CHAN_HT20;
1458355fec48SAndriy Voskoboinyk 	cent = findchannel(chans, *nchans, freq, flags);
1459355fec48SAndriy Voskoboinyk 	if (cent == NULL)
1460355fec48SAndriy Voskoboinyk 		return (EINVAL);
1461355fec48SAndriy Voskoboinyk 
1462355fec48SAndriy Voskoboinyk 	extc = findchannel(chans, *nchans, freq + 20, flags);
1463355fec48SAndriy Voskoboinyk 	if (extc == NULL)
1464355fec48SAndriy Voskoboinyk 		return (ENOENT);
1465355fec48SAndriy Voskoboinyk 
1466355fec48SAndriy Voskoboinyk 	flags &= ~IEEE80211_CHAN_HT;
1467355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1468355fec48SAndriy Voskoboinyk 	    maxregpower, flags | IEEE80211_CHAN_HT40U);
1469355fec48SAndriy Voskoboinyk 	if (error != 0)
1470355fec48SAndriy Voskoboinyk 		return (error);
1471355fec48SAndriy Voskoboinyk 
1472355fec48SAndriy Voskoboinyk 	error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1473355fec48SAndriy Voskoboinyk 	    maxregpower, flags | IEEE80211_CHAN_HT40D);
1474355fec48SAndriy Voskoboinyk 
1475355fec48SAndriy Voskoboinyk 	return (error);
1476355fec48SAndriy Voskoboinyk }
1477355fec48SAndriy Voskoboinyk 
1478355fec48SAndriy Voskoboinyk /*
14794774b999SAdrian Chadd  * Fetch the center frequency for the primary channel.
14804774b999SAdrian Chadd  */
14814774b999SAdrian Chadd uint32_t
14824774b999SAdrian Chadd ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
14834774b999SAdrian Chadd {
14844774b999SAdrian Chadd 
14854774b999SAdrian Chadd 	return (c->ic_freq);
14864774b999SAdrian Chadd }
14874774b999SAdrian Chadd 
14884774b999SAdrian Chadd /*
14894774b999SAdrian Chadd  * Fetch the center frequency for the primary BAND channel.
14904774b999SAdrian Chadd  *
14914774b999SAdrian Chadd  * For 5, 10, 20MHz channels it'll be the normally configured channel
14924774b999SAdrian Chadd  * frequency.
14934774b999SAdrian Chadd  *
14944774b999SAdrian Chadd  * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
14954774b999SAdrian Chadd  * wide channel, not the centre of the primary channel (that's ic_freq).
14964774b999SAdrian Chadd  *
14974774b999SAdrian Chadd  * For 80+80MHz channels this will be the centre of the primary
14984774b999SAdrian Chadd  * 80MHz channel; the secondary 80MHz channel will be center_freq2().
14994774b999SAdrian Chadd  */
15004774b999SAdrian Chadd uint32_t
15014774b999SAdrian Chadd ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
15024774b999SAdrian Chadd {
15034774b999SAdrian Chadd 
150467f4aa38SAdrian Chadd 	/*
150567f4aa38SAdrian Chadd 	 * VHT - use the pre-calculated centre frequency
150667f4aa38SAdrian Chadd 	 * of the given channel.
150767f4aa38SAdrian Chadd 	 */
150867f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c))
150967f4aa38SAdrian Chadd 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
151067f4aa38SAdrian Chadd 
15114774b999SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT40U(c)) {
15124774b999SAdrian Chadd 		return (c->ic_freq + 10);
15134774b999SAdrian Chadd 	}
15144774b999SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT40D(c)) {
15154774b999SAdrian Chadd 		return (c->ic_freq - 10);
15164774b999SAdrian Chadd 	}
15174774b999SAdrian Chadd 
15184774b999SAdrian Chadd 	return (c->ic_freq);
15194774b999SAdrian Chadd }
15204774b999SAdrian Chadd 
15214774b999SAdrian Chadd /*
152267f4aa38SAdrian Chadd  * For now, no 80+80 support; it will likely always return 0.
15234774b999SAdrian Chadd  */
15244774b999SAdrian Chadd uint32_t
15254774b999SAdrian Chadd ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
15264774b999SAdrian Chadd {
15274774b999SAdrian Chadd 
152867f4aa38SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
152967f4aa38SAdrian Chadd 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
153067f4aa38SAdrian Chadd 
15314774b999SAdrian Chadd 	return (0);
15324774b999SAdrian Chadd }
15334774b999SAdrian Chadd 
15344774b999SAdrian Chadd /*
1535355fec48SAndriy Voskoboinyk  * Adds channels into specified channel list (ieee[] array must be sorted).
1536355fec48SAndriy Voskoboinyk  * Channels are already sorted.
1537355fec48SAndriy Voskoboinyk  */
1538355fec48SAndriy Voskoboinyk static int
1539355fec48SAndriy Voskoboinyk add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1540355fec48SAndriy Voskoboinyk     const uint8_t ieee[], int nieee, uint32_t flags[])
1541355fec48SAndriy Voskoboinyk {
1542355fec48SAndriy Voskoboinyk 	uint16_t freq;
1543355fec48SAndriy Voskoboinyk 	int i, j, error;
154467f4aa38SAdrian Chadd 	int is_vht;
1545355fec48SAndriy Voskoboinyk 
1546355fec48SAndriy Voskoboinyk 	for (i = 0; i < nieee; i++) {
1547355fec48SAndriy Voskoboinyk 		freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1548355fec48SAndriy Voskoboinyk 		for (j = 0; flags[j] != 0; j++) {
154967f4aa38SAdrian Chadd 			/*
155067f4aa38SAdrian Chadd 			 * Notes:
155167f4aa38SAdrian Chadd 			 * + HT40 and VHT40 channels occur together, so
155267f4aa38SAdrian Chadd 			 *   we need to be careful that we actually allow that.
155367f4aa38SAdrian Chadd 			 * + VHT80, VHT160 will coexist with HT40/VHT40, so
155467f4aa38SAdrian Chadd 			 *   make sure it's not skipped because of the overlap
155567f4aa38SAdrian Chadd 			 *   check used for (V)HT40.
155667f4aa38SAdrian Chadd 			 */
155767f4aa38SAdrian Chadd 			is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
155867f4aa38SAdrian Chadd 
15594b1c2487SBjoern A. Zeeb 			/* XXX TODO FIXME VHT80P80. */
15604b1c2487SBjoern A. Zeeb 			/* XXX TODO FIXME VHT160. */
15614b1c2487SBjoern A. Zeeb 
156267f4aa38SAdrian Chadd 			/*
156367f4aa38SAdrian Chadd 			 * Test for VHT80.
156467f4aa38SAdrian Chadd 			 * XXX This is all very broken right now.
156567f4aa38SAdrian Chadd 			 * What we /should/ do is:
156667f4aa38SAdrian Chadd 			 *
156767f4aa38SAdrian Chadd 			 * + check that the frequency is in the list of
156867f4aa38SAdrian Chadd 			 *   allowed VHT80 ranges; and
156967f4aa38SAdrian Chadd 			 * + the other 3 channels in the list are actually
157067f4aa38SAdrian Chadd 			 *   also available.
157167f4aa38SAdrian Chadd 			 */
157267f4aa38SAdrian Chadd 			if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
157367f4aa38SAdrian Chadd 				if (! is_vht80_valid_freq(freq))
157467f4aa38SAdrian Chadd 					continue;
157567f4aa38SAdrian Chadd 
157667f4aa38SAdrian Chadd 			/*
157767f4aa38SAdrian Chadd 			 * Test for (V)HT40.
157867f4aa38SAdrian Chadd 			 *
157967f4aa38SAdrian Chadd 			 * This is also a fall through from VHT80; as we only
158067f4aa38SAdrian Chadd 			 * allow a VHT80 channel if the VHT40 combination is
158167f4aa38SAdrian Chadd 			 * also valid.  If the VHT40 form is not valid then
158267f4aa38SAdrian Chadd 			 * we certainly can't do VHT80..
158367f4aa38SAdrian Chadd 			 */
1584355fec48SAndriy Voskoboinyk 			if (flags[j] & IEEE80211_CHAN_HT40D)
158567f4aa38SAdrian Chadd 				/*
158667f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if we are the
158767f4aa38SAdrian Chadd 				 * first channel.
158867f4aa38SAdrian Chadd 				 *
158967f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if it's below/
159067f4aa38SAdrian Chadd 				 * within 20MHz of the first channel.
159167f4aa38SAdrian Chadd 				 *
159267f4aa38SAdrian Chadd 				 * Can't have a "lower" channel if the channel
159367f4aa38SAdrian Chadd 				 * below it is not 20MHz away.
159467f4aa38SAdrian Chadd 				 */
1595355fec48SAndriy Voskoboinyk 				if (i == 0 || ieee[i] < ieee[0] + 4 ||
1596355fec48SAndriy Voskoboinyk 				    freq - 20 !=
1597355fec48SAndriy Voskoboinyk 				    ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1598355fec48SAndriy Voskoboinyk 					continue;
1599355fec48SAndriy Voskoboinyk 			if (flags[j] & IEEE80211_CHAN_HT40U)
160067f4aa38SAdrian Chadd 				/*
160167f4aa38SAdrian Chadd 				 * Can't have an "upper" channel if we are
160267f4aa38SAdrian Chadd 				 * the last channel.
160367f4aa38SAdrian Chadd 				 *
160467f4aa38SAdrian Chadd 				 * Can't have an "upper" channel be above the
160567f4aa38SAdrian Chadd 				 * last channel in the list.
160667f4aa38SAdrian Chadd 				 *
160767f4aa38SAdrian Chadd 				 * Can't have an "upper" channel if the next
160867f4aa38SAdrian Chadd 				 * channel according to the math isn't 20MHz
160967f4aa38SAdrian Chadd 				 * away.  (Likely for channel 13/14.)
161067f4aa38SAdrian Chadd 				 */
1611355fec48SAndriy Voskoboinyk 				if (i == nieee - 1 ||
1612355fec48SAndriy Voskoboinyk 				    ieee[i] + 4 > ieee[nieee - 1] ||
1613355fec48SAndriy Voskoboinyk 				    freq + 20 !=
1614355fec48SAndriy Voskoboinyk 				    ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1615355fec48SAndriy Voskoboinyk 					continue;
1616355fec48SAndriy Voskoboinyk 
1617355fec48SAndriy Voskoboinyk 			if (j == 0) {
1618355fec48SAndriy Voskoboinyk 				error = addchan(chans, maxchans, nchans,
1619355fec48SAndriy Voskoboinyk 				    ieee[i], freq, 0, flags[j]);
1620355fec48SAndriy Voskoboinyk 			} else {
1621355fec48SAndriy Voskoboinyk 				error = copychan_prev(chans, maxchans, nchans,
1622355fec48SAndriy Voskoboinyk 				    flags[j]);
1623355fec48SAndriy Voskoboinyk 			}
1624355fec48SAndriy Voskoboinyk 			if (error != 0)
1625355fec48SAndriy Voskoboinyk 				return (error);
1626355fec48SAndriy Voskoboinyk 		}
1627355fec48SAndriy Voskoboinyk 	}
1628355fec48SAndriy Voskoboinyk 
16296dbbec93SAndriy Voskoboinyk 	return (0);
1630355fec48SAndriy Voskoboinyk }
1631355fec48SAndriy Voskoboinyk 
1632355fec48SAndriy Voskoboinyk int
1633355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1634355fec48SAndriy Voskoboinyk     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1635355fec48SAndriy Voskoboinyk     int ht40)
1636355fec48SAndriy Voskoboinyk {
1637355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
1638355fec48SAndriy Voskoboinyk 
163967f4aa38SAdrian Chadd 	/* XXX no VHT for now */
1640355fec48SAndriy Voskoboinyk 	getflags_2ghz(bands, flags, ht40);
1641355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1642355fec48SAndriy Voskoboinyk 
1643355fec48SAndriy Voskoboinyk 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1644355fec48SAndriy Voskoboinyk }
1645355fec48SAndriy Voskoboinyk 
1646355fec48SAndriy Voskoboinyk int
1647b84b3638SAndriy Voskoboinyk ieee80211_add_channels_default_2ghz(struct ieee80211_channel chans[],
1648b84b3638SAndriy Voskoboinyk     int maxchans, int *nchans, const uint8_t bands[], int ht40)
1649b84b3638SAndriy Voskoboinyk {
1650b84b3638SAndriy Voskoboinyk 	const uint8_t default_chan_list[] =
1651b84b3638SAndriy Voskoboinyk 	    { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
1652b84b3638SAndriy Voskoboinyk 
1653b84b3638SAndriy Voskoboinyk 	return (ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
1654b84b3638SAndriy Voskoboinyk 	    default_chan_list, nitems(default_chan_list), bands, ht40));
1655b84b3638SAndriy Voskoboinyk }
1656b84b3638SAndriy Voskoboinyk 
1657b84b3638SAndriy Voskoboinyk int
1658355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1659355fec48SAndriy Voskoboinyk     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1660355fec48SAndriy Voskoboinyk     int ht40)
1661355fec48SAndriy Voskoboinyk {
1662355fec48SAndriy Voskoboinyk 	uint32_t flags[IEEE80211_MODE_MAX];
166367f4aa38SAdrian Chadd 	int vht80 = 0;
1664355fec48SAndriy Voskoboinyk 
166567f4aa38SAdrian Chadd 	/*
166667f4aa38SAdrian Chadd 	 * For now, assume VHT == VHT80 support as a minimum.
166767f4aa38SAdrian Chadd 	 */
166867f4aa38SAdrian Chadd 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
166967f4aa38SAdrian Chadd 		vht80 = 1;
167067f4aa38SAdrian Chadd 
167167f4aa38SAdrian Chadd 	getflags_5ghz(bands, flags, ht40, vht80);
1672355fec48SAndriy Voskoboinyk 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1673355fec48SAndriy Voskoboinyk 
1674355fec48SAndriy Voskoboinyk 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1675355fec48SAndriy Voskoboinyk }
1676355fec48SAndriy Voskoboinyk 
16771a1e1d21SSam Leffler /*
167868e8e04eSSam Leffler  * Locate a channel given a frequency+flags.  We cache
1679b032f27cSSam Leffler  * the previous lookup to optimize switching between two
168068e8e04eSSam Leffler  * channels--as happens with dynamic turbo.
168168e8e04eSSam Leffler  */
168268e8e04eSSam Leffler struct ieee80211_channel *
168368e8e04eSSam Leffler ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
168468e8e04eSSam Leffler {
168568e8e04eSSam Leffler 	struct ieee80211_channel *c;
168668e8e04eSSam Leffler 
168768e8e04eSSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
168868e8e04eSSam Leffler 	c = ic->ic_prevchan;
168968e8e04eSSam Leffler 	if (c != NULL && c->ic_freq == freq &&
169068e8e04eSSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
169168e8e04eSSam Leffler 		return c;
169268e8e04eSSam Leffler 	/* brute force search */
1693355fec48SAndriy Voskoboinyk 	return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
169468e8e04eSSam Leffler }
169568e8e04eSSam Leffler 
1696a557c018SSam Leffler /*
1697a557c018SSam Leffler  * Locate a channel given a channel number+flags.  We cache
1698a557c018SSam Leffler  * the previous lookup to optimize switching between two
1699a557c018SSam Leffler  * channels--as happens with dynamic turbo.
1700a557c018SSam Leffler  */
1701a557c018SSam Leffler struct ieee80211_channel *
1702a557c018SSam Leffler ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1703a557c018SSam Leffler {
1704a557c018SSam Leffler 	struct ieee80211_channel *c;
1705a557c018SSam Leffler 	int i;
1706a557c018SSam Leffler 
1707a557c018SSam Leffler 	flags &= IEEE80211_CHAN_ALLTURBO;
1708a557c018SSam Leffler 	c = ic->ic_prevchan;
1709a557c018SSam Leffler 	if (c != NULL && c->ic_ieee == ieee &&
1710a557c018SSam Leffler 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1711a557c018SSam Leffler 		return c;
1712a557c018SSam Leffler 	/* brute force search */
1713a557c018SSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
1714a557c018SSam Leffler 		c = &ic->ic_channels[i];
1715a557c018SSam Leffler 		if (c->ic_ieee == ieee &&
1716a557c018SSam Leffler 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1717a557c018SSam Leffler 			return c;
1718a557c018SSam Leffler 	}
1719a557c018SSam Leffler 	return NULL;
1720a557c018SSam Leffler }
1721a557c018SSam Leffler 
1722c79f192cSAdrian Chadd /*
1723c79f192cSAdrian Chadd  * Lookup a channel suitable for the given rx status.
1724c79f192cSAdrian Chadd  *
1725c79f192cSAdrian Chadd  * This is used to find a channel for a frame (eg beacon, probe
1726c79f192cSAdrian Chadd  * response) based purely on the received PHY information.
1727c79f192cSAdrian Chadd  *
1728c79f192cSAdrian Chadd  * For now it tries to do it based on R_FREQ / R_IEEE.
1729c79f192cSAdrian Chadd  * This is enough for 11bg and 11a (and thus 11ng/11na)
1730c79f192cSAdrian Chadd  * but it will not be enough for GSM, PSB channels and the
1731c79f192cSAdrian Chadd  * like.  It also doesn't know about legacy-turbog and
1732c79f192cSAdrian Chadd  * legacy-turbo modes, which some offload NICs actually
1733c79f192cSAdrian Chadd  * support in weird ways.
1734c79f192cSAdrian Chadd  *
1735c79f192cSAdrian Chadd  * Takes the ic and rxstatus; returns the channel or NULL
1736c79f192cSAdrian Chadd  * if not found.
1737c79f192cSAdrian Chadd  *
1738c79f192cSAdrian Chadd  * XXX TODO: Add support for that when the need arises.
1739c79f192cSAdrian Chadd  */
1740c79f192cSAdrian Chadd struct ieee80211_channel *
1741c79f192cSAdrian Chadd ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1742c79f192cSAdrian Chadd     const struct ieee80211_rx_stats *rxs)
1743c79f192cSAdrian Chadd {
1744c79f192cSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1745c79f192cSAdrian Chadd 	uint32_t flags;
1746c79f192cSAdrian Chadd 	struct ieee80211_channel *c;
1747c79f192cSAdrian Chadd 
1748c79f192cSAdrian Chadd 	if (rxs == NULL)
1749c79f192cSAdrian Chadd 		return (NULL);
1750c79f192cSAdrian Chadd 
1751c79f192cSAdrian Chadd 	/*
1752c79f192cSAdrian Chadd 	 * Strictly speaking we only use freq for now,
1753c79f192cSAdrian Chadd 	 * however later on we may wish to just store
1754c79f192cSAdrian Chadd 	 * the ieee for verification.
1755c79f192cSAdrian Chadd 	 */
1756c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1757c79f192cSAdrian Chadd 		return (NULL);
1758c79f192cSAdrian Chadd 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1759c79f192cSAdrian Chadd 		return (NULL);
1760c79f192cSAdrian Chadd 
1761c79f192cSAdrian Chadd 	/*
1762c79f192cSAdrian Chadd 	 * If the rx status contains a valid ieee/freq, then
1763c79f192cSAdrian Chadd 	 * ensure we populate the correct channel information
1764c79f192cSAdrian Chadd 	 * in rxchan before passing it up to the scan infrastructure.
1765c79f192cSAdrian Chadd 	 * Offload NICs will pass up beacons from all channels
1766c79f192cSAdrian Chadd 	 * during background scans.
1767c79f192cSAdrian Chadd 	 */
1768c79f192cSAdrian Chadd 
1769c79f192cSAdrian Chadd 	/* Determine a band */
1770c79f192cSAdrian Chadd 	/* XXX should be done by the driver? */
1771c79f192cSAdrian Chadd 	if (rxs->c_freq < 3000) {
17722108f2a8SAdrian Chadd 		flags = IEEE80211_CHAN_G;
1773c79f192cSAdrian Chadd 	} else {
1774c79f192cSAdrian Chadd 		flags = IEEE80211_CHAN_A;
1775c79f192cSAdrian Chadd 	}
1776c79f192cSAdrian Chadd 
1777c79f192cSAdrian Chadd 	/* Channel lookup */
1778c79f192cSAdrian Chadd 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1779c79f192cSAdrian Chadd 
1780c79f192cSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1781c79f192cSAdrian Chadd 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1782372c7b95SBjoern A. Zeeb 	    __func__, (int) rxs->c_freq, (int) rxs->c_ieee, flags, c);
1783c79f192cSAdrian Chadd 
1784c79f192cSAdrian Chadd 	return (c);
1785c79f192cSAdrian Chadd }
1786c79f192cSAdrian Chadd 
178768e8e04eSSam Leffler static void
1788b032f27cSSam Leffler addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
178968e8e04eSSam Leffler {
179068e8e04eSSam Leffler #define	ADD(_ic, _s, _o) \
1791b032f27cSSam Leffler 	ifmedia_add(media, \
179268e8e04eSSam Leffler 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
179368e8e04eSSam Leffler 	static const u_int mopts[IEEE80211_MODE_MAX] = {
1794c3f10abdSSam Leffler 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1795c3f10abdSSam Leffler 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1796c3f10abdSSam Leffler 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1797c3f10abdSSam Leffler 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1798c3f10abdSSam Leffler 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1799c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1800c3f10abdSSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1801c3f10abdSSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
18026a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
18036a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1804c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1805c3f10abdSSam Leffler 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
18060c67d389SAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= IFM_IEEE80211_VHT2G,
18070c67d389SAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= IFM_IEEE80211_VHT5G,
180868e8e04eSSam Leffler 	};
180968e8e04eSSam Leffler 	u_int mopt;
181068e8e04eSSam Leffler 
181168e8e04eSSam Leffler 	mopt = mopts[mode];
1812b032f27cSSam Leffler 	if (addsta)
1813b032f27cSSam Leffler 		ADD(ic, mword, mopt);	/* STA mode has no cap */
1814b032f27cSSam Leffler 	if (caps & IEEE80211_C_IBSS)
1815b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1816b032f27cSSam Leffler 	if (caps & IEEE80211_C_HOSTAP)
1817b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1818b032f27cSSam Leffler 	if (caps & IEEE80211_C_AHDEMO)
1819b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1820b032f27cSSam Leffler 	if (caps & IEEE80211_C_MONITOR)
1821b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1822b032f27cSSam Leffler 	if (caps & IEEE80211_C_WDS)
1823b032f27cSSam Leffler 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
182459aa14a9SRui Paulo 	if (caps & IEEE80211_C_MBSS)
182559aa14a9SRui Paulo 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
182668e8e04eSSam Leffler #undef ADD
182768e8e04eSSam Leffler }
182868e8e04eSSam Leffler 
182968e8e04eSSam Leffler /*
18301a1e1d21SSam Leffler  * Setup the media data structures according to the channel and
1831b032f27cSSam Leffler  * rate tables.
18321a1e1d21SSam Leffler  */
1833b032f27cSSam Leffler static int
1834b032f27cSSam Leffler ieee80211_media_setup(struct ieee80211com *ic,
1835b032f27cSSam Leffler 	struct ifmedia *media, int caps, int addsta,
18361a1e1d21SSam Leffler 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
18371a1e1d21SSam Leffler {
1838fcd9500fSBernhard Schmidt 	int i, j, rate, maxrate, mword, r;
1839fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
184068e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
18411a1e1d21SSam Leffler 	struct ieee80211_rateset allrates;
18421a1e1d21SSam Leffler 
18432692bb26SSam Leffler 	/*
18441a1e1d21SSam Leffler 	 * Fill in media characteristics.
18451a1e1d21SSam Leffler 	 */
1846b032f27cSSam Leffler 	ifmedia_init(media, 0, media_change, media_stat);
18471a1e1d21SSam Leffler 	maxrate = 0;
184868e8e04eSSam Leffler 	/*
184968e8e04eSSam Leffler 	 * Add media for legacy operating modes.
185068e8e04eSSam Leffler 	 */
18511a1e1d21SSam Leffler 	memset(&allrates, 0, sizeof(allrates));
185268e8e04eSSam Leffler 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
18536dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
18541a1e1d21SSam Leffler 			continue;
1855b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
18561a1e1d21SSam Leffler 		if (mode == IEEE80211_MODE_AUTO)
18571a1e1d21SSam Leffler 			continue;
18581a1e1d21SSam Leffler 		rs = &ic->ic_sup_rates[mode];
18591a1e1d21SSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
18601a1e1d21SSam Leffler 			rate = rs->rs_rates[i];
18611a1e1d21SSam Leffler 			mword = ieee80211_rate2media(ic, rate, mode);
18621a1e1d21SSam Leffler 			if (mword == 0)
18631a1e1d21SSam Leffler 				continue;
1864b032f27cSSam Leffler 			addmedia(media, caps, addsta, mode, mword);
18651a1e1d21SSam Leffler 			/*
186668e8e04eSSam Leffler 			 * Add legacy rate to the collection of all rates.
18671a1e1d21SSam Leffler 			 */
18681a1e1d21SSam Leffler 			r = rate & IEEE80211_RATE_VAL;
18691a1e1d21SSam Leffler 			for (j = 0; j < allrates.rs_nrates; j++)
18701a1e1d21SSam Leffler 				if (allrates.rs_rates[j] == r)
18711a1e1d21SSam Leffler 					break;
18721a1e1d21SSam Leffler 			if (j == allrates.rs_nrates) {
18731a1e1d21SSam Leffler 				/* unique, add to the set */
18741a1e1d21SSam Leffler 				allrates.rs_rates[j] = r;
18751a1e1d21SSam Leffler 				allrates.rs_nrates++;
18761a1e1d21SSam Leffler 			}
18771a1e1d21SSam Leffler 			rate = (rate & IEEE80211_RATE_VAL) / 2;
18781a1e1d21SSam Leffler 			if (rate > maxrate)
18791a1e1d21SSam Leffler 				maxrate = rate;
18801a1e1d21SSam Leffler 		}
18811a1e1d21SSam Leffler 	}
18821a1e1d21SSam Leffler 	for (i = 0; i < allrates.rs_nrates; i++) {
18831a1e1d21SSam Leffler 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
18841a1e1d21SSam Leffler 				IEEE80211_MODE_AUTO);
18851a1e1d21SSam Leffler 		if (mword == 0)
18861a1e1d21SSam Leffler 			continue;
188768e8e04eSSam Leffler 		/* NB: remove media options from mword */
1888b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1889b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
18901a1e1d21SSam Leffler 	}
189168e8e04eSSam Leffler 	/*
189268e8e04eSSam Leffler 	 * Add HT/11n media.  Note that we do not have enough
189368e8e04eSSam Leffler 	 * bits in the media subtype to express the MCS so we
189468e8e04eSSam Leffler 	 * use a "placeholder" media subtype and any fixed MCS
189568e8e04eSSam Leffler 	 * must be specified with a different mechanism.
189668e8e04eSSam Leffler 	 */
18976a76ae21SSam Leffler 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
189868e8e04eSSam Leffler 		if (isclr(ic->ic_modecaps, mode))
189968e8e04eSSam Leffler 			continue;
1900b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1901b032f27cSSam Leffler 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
190268e8e04eSSam Leffler 	}
190368e8e04eSSam Leffler 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
190468e8e04eSSam Leffler 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1905b032f27cSSam Leffler 		addmedia(media, caps, addsta,
1906b032f27cSSam Leffler 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
19076f897ba9SBernhard Schmidt 		i = ic->ic_txstream * 8 - 1;
19086f897ba9SBernhard Schmidt 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
19096f897ba9SBernhard Schmidt 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
19106f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_400ns;
19116f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
19126f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht40_rate_800ns;
19136f897ba9SBernhard Schmidt 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
19146f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_400ns;
19156f897ba9SBernhard Schmidt 		else
19166f897ba9SBernhard Schmidt 			rate = ieee80211_htrates[i].ht20_rate_800ns;
19176f897ba9SBernhard Schmidt 		if (rate > maxrate)
19186f897ba9SBernhard Schmidt 			maxrate = rate;
1919b032f27cSSam Leffler 	}
19200c67d389SAdrian Chadd 
19210c67d389SAdrian Chadd 	/*
19220c67d389SAdrian Chadd 	 * Add VHT media.
19230c67d389SAdrian Chadd 	 */
19240c67d389SAdrian Chadd 	for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
19250c67d389SAdrian Chadd 		if (isclr(ic->ic_modecaps, mode))
19260c67d389SAdrian Chadd 			continue;
19270c67d389SAdrian Chadd 		addmedia(media, caps, addsta, mode, IFM_AUTO);
19280c67d389SAdrian Chadd 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
19290c67d389SAdrian Chadd 
19300c67d389SAdrian Chadd 		/* XXX TODO: VHT maxrate */
19310c67d389SAdrian Chadd 	}
19320c67d389SAdrian Chadd 
1933b032f27cSSam Leffler 	return maxrate;
193468e8e04eSSam Leffler }
193568e8e04eSSam Leffler 
19366a76ae21SSam Leffler /* XXX inline or eliminate? */
193741b3c790SSam Leffler const struct ieee80211_rateset *
193841b3c790SSam Leffler ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
193941b3c790SSam Leffler {
194040432d36SSam Leffler 	/* XXX does this work for 11ng basic rates? */
194168e8e04eSSam Leffler 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
194241b3c790SSam Leffler }
194341b3c790SSam Leffler 
1944dfabbaa0SAndriy Voskoboinyk /* XXX inline or eliminate? */
1945dfabbaa0SAndriy Voskoboinyk const struct ieee80211_htrateset *
1946dfabbaa0SAndriy Voskoboinyk ieee80211_get_suphtrates(struct ieee80211com *ic,
1947dfabbaa0SAndriy Voskoboinyk     const struct ieee80211_channel *c)
1948dfabbaa0SAndriy Voskoboinyk {
1949dfabbaa0SAndriy Voskoboinyk 	return &ic->ic_sup_htrates;
1950dfabbaa0SAndriy Voskoboinyk }
1951dfabbaa0SAndriy Voskoboinyk 
19528a1b9b6aSSam Leffler void
19538a1b9b6aSSam Leffler ieee80211_announce(struct ieee80211com *ic)
19548a1b9b6aSSam Leffler {
1955fcd9500fSBernhard Schmidt 	int i, rate, mword;
1956fcd9500fSBernhard Schmidt 	enum ieee80211_phymode mode;
195768e8e04eSSam Leffler 	const struct ieee80211_rateset *rs;
19588a1b9b6aSSam Leffler 
19597edb9e0aSSam Leffler 	/* NB: skip AUTO since it has no rates */
19607edb9e0aSSam Leffler 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
19616dbd16f1SSam Leffler 		if (isclr(ic->ic_modecaps, mode))
19628a1b9b6aSSam Leffler 			continue;
1963c8f5794eSGleb Smirnoff 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
19648a1b9b6aSSam Leffler 		rs = &ic->ic_sup_rates[mode];
19658a1b9b6aSSam Leffler 		for (i = 0; i < rs->rs_nrates; i++) {
196668e8e04eSSam Leffler 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
19678a1b9b6aSSam Leffler 			if (mword == 0)
19688a1b9b6aSSam Leffler 				continue;
196968e8e04eSSam Leffler 			rate = ieee80211_media2rate(mword);
19708a1b9b6aSSam Leffler 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
197168e8e04eSSam Leffler 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
19728a1b9b6aSSam Leffler 		}
19738a1b9b6aSSam Leffler 		printf("\n");
19748a1b9b6aSSam Leffler 	}
197568e8e04eSSam Leffler 	ieee80211_ht_announce(ic);
197667f4aa38SAdrian Chadd 	ieee80211_vht_announce(ic);
19778a1b9b6aSSam Leffler }
19788a1b9b6aSSam Leffler 
197968e8e04eSSam Leffler void
198068e8e04eSSam Leffler ieee80211_announce_channels(struct ieee80211com *ic)
19811a1e1d21SSam Leffler {
198268e8e04eSSam Leffler 	const struct ieee80211_channel *c;
198368e8e04eSSam Leffler 	char type;
198468e8e04eSSam Leffler 	int i, cw;
198568e8e04eSSam Leffler 
198668e8e04eSSam Leffler 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
198768e8e04eSSam Leffler 	for (i = 0; i < ic->ic_nchans; i++) {
198868e8e04eSSam Leffler 		c = &ic->ic_channels[i];
198968e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ST(c))
199068e8e04eSSam Leffler 			type = 'S';
199168e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108A(c))
199268e8e04eSSam Leffler 			type = 'T';
199368e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_108G(c))
199468e8e04eSSam Leffler 			type = 'G';
199568e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HT(c))
199668e8e04eSSam Leffler 			type = 'n';
199768e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_A(c))
199868e8e04eSSam Leffler 			type = 'a';
199968e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_ANYG(c))
200068e8e04eSSam Leffler 			type = 'g';
200168e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_B(c))
200268e8e04eSSam Leffler 			type = 'b';
200368e8e04eSSam Leffler 		else
200468e8e04eSSam Leffler 			type = 'f';
200568e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
200668e8e04eSSam Leffler 			cw = 40;
200768e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(c))
200868e8e04eSSam Leffler 			cw = 10;
200968e8e04eSSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(c))
201068e8e04eSSam Leffler 			cw = 5;
201168e8e04eSSam Leffler 		else
201268e8e04eSSam Leffler 			cw = 20;
201368e8e04eSSam Leffler 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
201468e8e04eSSam Leffler 			, c->ic_ieee, c->ic_freq, type
201568e8e04eSSam Leffler 			, cw
201668e8e04eSSam Leffler 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
201768e8e04eSSam Leffler 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
201868e8e04eSSam Leffler 			, c->ic_maxregpower
201968e8e04eSSam Leffler 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
202068e8e04eSSam Leffler 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
202168e8e04eSSam Leffler 		);
202268e8e04eSSam Leffler 	}
20231a1e1d21SSam Leffler }
20241a1e1d21SSam Leffler 
202568e8e04eSSam Leffler static int
2026f945bd7aSSam Leffler media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
202768e8e04eSSam Leffler {
20281a1e1d21SSam Leffler 	switch (IFM_MODE(ime->ifm_media)) {
20291a1e1d21SSam Leffler 	case IFM_IEEE80211_11A:
2030b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11A;
20311a1e1d21SSam Leffler 		break;
20321a1e1d21SSam Leffler 	case IFM_IEEE80211_11B:
2033b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11B;
20341a1e1d21SSam Leffler 		break;
20351a1e1d21SSam Leffler 	case IFM_IEEE80211_11G:
2036b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11G;
20371a1e1d21SSam Leffler 		break;
20384844aa7dSAtsushi Onoe 	case IFM_IEEE80211_FH:
2039b032f27cSSam Leffler 		*mode = IEEE80211_MODE_FH;
20404844aa7dSAtsushi Onoe 		break;
204168e8e04eSSam Leffler 	case IFM_IEEE80211_11NA:
2042b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NA;
204368e8e04eSSam Leffler 		break;
204468e8e04eSSam Leffler 	case IFM_IEEE80211_11NG:
2045b032f27cSSam Leffler 		*mode = IEEE80211_MODE_11NG;
204668e8e04eSSam Leffler 		break;
20471a1e1d21SSam Leffler 	case IFM_AUTO:
2048b032f27cSSam Leffler 		*mode = IEEE80211_MODE_AUTO;
20491a1e1d21SSam Leffler 		break;
20501a1e1d21SSam Leffler 	default:
2051b032f27cSSam Leffler 		return 0;
20521a1e1d21SSam Leffler 	}
20531a1e1d21SSam Leffler 	/*
20548a1b9b6aSSam Leffler 	 * Turbo mode is an ``option''.
20558a1b9b6aSSam Leffler 	 * XXX does not apply to AUTO
20561a1e1d21SSam Leffler 	 */
20571a1e1d21SSam Leffler 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2058b032f27cSSam Leffler 		if (*mode == IEEE80211_MODE_11A) {
2059f945bd7aSSam Leffler 			if (flags & IEEE80211_F_TURBOP)
2060b032f27cSSam Leffler 				*mode = IEEE80211_MODE_TURBO_A;
206168e8e04eSSam Leffler 			else
2062b032f27cSSam Leffler 				*mode = IEEE80211_MODE_STURBO_A;
2063b032f27cSSam Leffler 		} else if (*mode == IEEE80211_MODE_11G)
2064b032f27cSSam Leffler 			*mode = IEEE80211_MODE_TURBO_G;
20658a1b9b6aSSam Leffler 		else
2066b032f27cSSam Leffler 			return 0;
20671a1e1d21SSam Leffler 	}
206868e8e04eSSam Leffler 	/* XXX HT40 +/- */
2069b032f27cSSam Leffler 	return 1;
2070b032f27cSSam Leffler }
20711a1e1d21SSam Leffler 
20721a1e1d21SSam Leffler /*
2073b032f27cSSam Leffler  * Handle a media change request on the vap interface.
2074b032f27cSSam Leffler  */
2075b032f27cSSam Leffler int
2076b032f27cSSam Leffler ieee80211_media_change(struct ifnet *ifp)
2077b032f27cSSam Leffler {
2078b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
2079b032f27cSSam Leffler 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2080f945bd7aSSam Leffler 	uint16_t newmode;
2081b032f27cSSam Leffler 
2082f945bd7aSSam Leffler 	if (!media2mode(ime, vap->iv_flags, &newmode))
2083b032f27cSSam Leffler 		return EINVAL;
2084f945bd7aSSam Leffler 	if (vap->iv_des_mode != newmode) {
2085f945bd7aSSam Leffler 		vap->iv_des_mode = newmode;
20860a310468SSam Leffler 		/* XXX kick state machine if up+running */
2087b032f27cSSam Leffler 	}
2088b032f27cSSam Leffler 	return 0;
2089b032f27cSSam Leffler }
2090b032f27cSSam Leffler 
209168e8e04eSSam Leffler /*
209268e8e04eSSam Leffler  * Common code to calculate the media status word
209368e8e04eSSam Leffler  * from the operating mode and channel state.
209468e8e04eSSam Leffler  */
209568e8e04eSSam Leffler static int
209668e8e04eSSam Leffler media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
209768e8e04eSSam Leffler {
209868e8e04eSSam Leffler 	int status;
209968e8e04eSSam Leffler 
210068e8e04eSSam Leffler 	status = IFM_IEEE80211;
210168e8e04eSSam Leffler 	switch (opmode) {
210268e8e04eSSam Leffler 	case IEEE80211_M_STA:
210368e8e04eSSam Leffler 		break;
210468e8e04eSSam Leffler 	case IEEE80211_M_IBSS:
210568e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC;
210668e8e04eSSam Leffler 		break;
210768e8e04eSSam Leffler 	case IEEE80211_M_HOSTAP:
210868e8e04eSSam Leffler 		status |= IFM_IEEE80211_HOSTAP;
210968e8e04eSSam Leffler 		break;
211068e8e04eSSam Leffler 	case IEEE80211_M_MONITOR:
211168e8e04eSSam Leffler 		status |= IFM_IEEE80211_MONITOR;
211268e8e04eSSam Leffler 		break;
211368e8e04eSSam Leffler 	case IEEE80211_M_AHDEMO:
211468e8e04eSSam Leffler 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
211568e8e04eSSam Leffler 		break;
211668e8e04eSSam Leffler 	case IEEE80211_M_WDS:
2117b032f27cSSam Leffler 		status |= IFM_IEEE80211_WDS;
211868e8e04eSSam Leffler 		break;
211959aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
212059aa14a9SRui Paulo 		status |= IFM_IEEE80211_MBSS;
212159aa14a9SRui Paulo 		break;
212268e8e04eSSam Leffler 	}
212368e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HTA(chan)) {
212468e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NA;
212568e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
212668e8e04eSSam Leffler 		status |= IFM_IEEE80211_11NG;
212768e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_A(chan)) {
212868e8e04eSSam Leffler 		status |= IFM_IEEE80211_11A;
212968e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_B(chan)) {
213068e8e04eSSam Leffler 		status |= IFM_IEEE80211_11B;
213168e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
213268e8e04eSSam Leffler 		status |= IFM_IEEE80211_11G;
213368e8e04eSSam Leffler 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
213468e8e04eSSam Leffler 		status |= IFM_IEEE80211_FH;
213568e8e04eSSam Leffler 	}
213668e8e04eSSam Leffler 	/* XXX else complain? */
213768e8e04eSSam Leffler 
213868e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_TURBO(chan))
213968e8e04eSSam Leffler 		status |= IFM_IEEE80211_TURBO;
2140b032f27cSSam Leffler #if 0
2141b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT20(chan))
2142b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT20;
2143b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT40(chan))
2144b032f27cSSam Leffler 		status |= IFM_IEEE80211_HT40;
2145b032f27cSSam Leffler #endif
214668e8e04eSSam Leffler 	return status;
214768e8e04eSSam Leffler }
214868e8e04eSSam Leffler 
21491a1e1d21SSam Leffler void
21501a1e1d21SSam Leffler ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
21511a1e1d21SSam Leffler {
2152b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
2153b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
215468e8e04eSSam Leffler 	enum ieee80211_phymode mode;
21551a1e1d21SSam Leffler 
21561a1e1d21SSam Leffler 	imr->ifm_status = IFM_AVALID;
215768e8e04eSSam Leffler 	/*
215868e8e04eSSam Leffler 	 * NB: use the current channel's mode to lock down a xmit
215968e8e04eSSam Leffler 	 * rate only when running; otherwise we may have a mismatch
216068e8e04eSSam Leffler 	 * in which case the rate will not be convertible.
216168e8e04eSSam Leffler 	 */
21629f098ac7SAdrian Chadd 	if (vap->iv_state == IEEE80211_S_RUN ||
21639f098ac7SAdrian Chadd 	    vap->iv_state == IEEE80211_S_SLEEP) {
21641a1e1d21SSam Leffler 		imr->ifm_status |= IFM_ACTIVE;
216568e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_curchan);
216668e8e04eSSam Leffler 	} else
216768e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
2168b032f27cSSam Leffler 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
21698a1b9b6aSSam Leffler 	/*
21708a1b9b6aSSam Leffler 	 * Calculate a current rate if possible.
21718a1b9b6aSSam Leffler 	 */
2172b032f27cSSam Leffler 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
21738a1b9b6aSSam Leffler 		/*
21748a1b9b6aSSam Leffler 		 * A fixed rate is set, report that.
21758a1b9b6aSSam Leffler 		 */
21768a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
2177b032f27cSSam Leffler 			vap->iv_txparms[mode].ucastrate, mode);
2178b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
21798a1b9b6aSSam Leffler 		/*
21808a1b9b6aSSam Leffler 		 * In station mode report the current transmit rate.
21818a1b9b6aSSam Leffler 		 */
21828a1b9b6aSSam Leffler 		imr->ifm_active |= ieee80211_rate2media(ic,
2183b032f27cSSam Leffler 			vap->iv_bss->ni_txrate, mode);
2184ba99a9b1SAndre Oppermann 	} else
21851a1e1d21SSam Leffler 		imr->ifm_active |= IFM_AUTO;
2186b032f27cSSam Leffler 	if (imr->ifm_status & IFM_ACTIVE)
2187b032f27cSSam Leffler 		imr->ifm_current = imr->ifm_active;
21881a1e1d21SSam Leffler }
21891a1e1d21SSam Leffler 
21901a1e1d21SSam Leffler /*
21911a1e1d21SSam Leffler  * Set the current phy mode and recalculate the active channel
21921a1e1d21SSam Leffler  * set based on the available channels for this mode.  Also
21931a1e1d21SSam Leffler  * select a new default/current channel if the current one is
21941a1e1d21SSam Leffler  * inappropriate for this mode.
21951a1e1d21SSam Leffler  */
21961a1e1d21SSam Leffler int
21971a1e1d21SSam Leffler ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
21981a1e1d21SSam Leffler {
21991a1e1d21SSam Leffler 	/*
2200ca4ac7aeSSam Leffler 	 * Adjust basic rates in 11b/11g supported rate set.
2201ca4ac7aeSSam Leffler 	 * Note that if operating on a hal/quarter rate channel
2202ca4ac7aeSSam Leffler 	 * this is a noop as those rates sets are different
2203ca4ac7aeSSam Leffler 	 * and used instead.
22041a1e1d21SSam Leffler 	 */
2205ca4ac7aeSSam Leffler 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2206b032f27cSSam Leffler 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2207ca4ac7aeSSam Leffler 
22081a1e1d21SSam Leffler 	ic->ic_curmode = mode;
2209d20ff6e6SAdrian Chadd 	ieee80211_reset_erp(ic);	/* reset global ERP state */
22108a1b9b6aSSam Leffler 
22111a1e1d21SSam Leffler 	return 0;
22121a1e1d21SSam Leffler }
22131a1e1d21SSam Leffler 
22141a1e1d21SSam Leffler /*
221568e8e04eSSam Leffler  * Return the phy mode for with the specified channel.
22161a1e1d21SSam Leffler  */
22171a1e1d21SSam Leffler enum ieee80211_phymode
221868e8e04eSSam Leffler ieee80211_chan2mode(const struct ieee80211_channel *chan)
22191a1e1d21SSam Leffler {
222068e8e04eSSam Leffler 
22210c67d389SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
22220c67d389SAdrian Chadd 		return IEEE80211_MODE_VHT_2GHZ;
22230c67d389SAdrian Chadd 	else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
22240c67d389SAdrian Chadd 		return IEEE80211_MODE_VHT_5GHZ;
22250c67d389SAdrian Chadd 	else if (IEEE80211_IS_CHAN_HTA(chan))
222668e8e04eSSam Leffler 		return IEEE80211_MODE_11NA;
222768e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_HTG(chan))
222868e8e04eSSam Leffler 		return IEEE80211_MODE_11NG;
222968e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_108G(chan))
22308a1b9b6aSSam Leffler 		return IEEE80211_MODE_TURBO_G;
223168e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ST(chan))
223268e8e04eSSam Leffler 		return IEEE80211_MODE_STURBO_A;
223368e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_TURBO(chan))
223468e8e04eSSam Leffler 		return IEEE80211_MODE_TURBO_A;
22356a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_HALF(chan))
22366a76ae21SSam Leffler 		return IEEE80211_MODE_HALF;
22376a76ae21SSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
22386a76ae21SSam Leffler 		return IEEE80211_MODE_QUARTER;
223968e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_A(chan))
224068e8e04eSSam Leffler 		return IEEE80211_MODE_11A;
224168e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(chan))
22421a1e1d21SSam Leffler 		return IEEE80211_MODE_11G;
224368e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_B(chan))
224468e8e04eSSam Leffler 		return IEEE80211_MODE_11B;
224568e8e04eSSam Leffler 	else if (IEEE80211_IS_CHAN_FHSS(chan))
224668e8e04eSSam Leffler 		return IEEE80211_MODE_FH;
224768e8e04eSSam Leffler 
224868e8e04eSSam Leffler 	/* NB: should not get here */
224968e8e04eSSam Leffler 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
225068e8e04eSSam Leffler 		__func__, chan->ic_freq, chan->ic_flags);
22511a1e1d21SSam Leffler 	return IEEE80211_MODE_11B;
22521a1e1d21SSam Leffler }
22531a1e1d21SSam Leffler 
225468e8e04eSSam Leffler struct ratemedia {
225568e8e04eSSam Leffler 	u_int	match;	/* rate + mode */
225668e8e04eSSam Leffler 	u_int	media;	/* if_media rate */
225768e8e04eSSam Leffler };
225868e8e04eSSam Leffler 
225968e8e04eSSam Leffler static int
226068e8e04eSSam Leffler findmedia(const struct ratemedia rates[], int n, u_int match)
226168e8e04eSSam Leffler {
226268e8e04eSSam Leffler 	int i;
226368e8e04eSSam Leffler 
226468e8e04eSSam Leffler 	for (i = 0; i < n; i++)
226568e8e04eSSam Leffler 		if (rates[i].match == match)
226668e8e04eSSam Leffler 			return rates[i].media;
226768e8e04eSSam Leffler 	return IFM_AUTO;
226868e8e04eSSam Leffler }
226968e8e04eSSam Leffler 
22701a1e1d21SSam Leffler /*
227168e8e04eSSam Leffler  * Convert IEEE80211 rate value to ifmedia subtype.
227268e8e04eSSam Leffler  * Rate is either a legacy rate in units of 0.5Mbps
227368e8e04eSSam Leffler  * or an MCS index.
22741a1e1d21SSam Leffler  */
22751a1e1d21SSam Leffler int
22761a1e1d21SSam Leffler ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
22771a1e1d21SSam Leffler {
227868e8e04eSSam Leffler 	static const struct ratemedia rates[] = {
22794844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
22804844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
22814844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
22824844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
22834844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
22844844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
22854844aa7dSAtsushi Onoe 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
22864844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
22874844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
22884844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
22894844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
22904844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
22914844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
22924844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
22934844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
22944844aa7dSAtsushi Onoe 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
22954844aa7dSAtsushi Onoe 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
22964844aa7dSAtsushi Onoe 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
22974844aa7dSAtsushi Onoe 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
22984844aa7dSAtsushi Onoe 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
22994844aa7dSAtsushi Onoe 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
23004844aa7dSAtsushi Onoe 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
23014844aa7dSAtsushi Onoe 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
23024844aa7dSAtsushi Onoe 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
23034844aa7dSAtsushi Onoe 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
23044844aa7dSAtsushi Onoe 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
23054844aa7dSAtsushi Onoe 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
230641b3c790SSam Leffler 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
230741b3c790SSam Leffler 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
230841b3c790SSam Leffler 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2309a4641f4eSPedro F. Giffuni 		/* NB: OFDM72 doesn't really exist so we don't handle it */
23101a1e1d21SSam Leffler 	};
231168e8e04eSSam Leffler 	static const struct ratemedia htrates[] = {
231268e8e04eSSam Leffler 		{   0, IFM_IEEE80211_MCS },
231368e8e04eSSam Leffler 		{   1, IFM_IEEE80211_MCS },
231468e8e04eSSam Leffler 		{   2, IFM_IEEE80211_MCS },
231568e8e04eSSam Leffler 		{   3, IFM_IEEE80211_MCS },
231668e8e04eSSam Leffler 		{   4, IFM_IEEE80211_MCS },
231768e8e04eSSam Leffler 		{   5, IFM_IEEE80211_MCS },
231868e8e04eSSam Leffler 		{   6, IFM_IEEE80211_MCS },
231968e8e04eSSam Leffler 		{   7, IFM_IEEE80211_MCS },
232068e8e04eSSam Leffler 		{   8, IFM_IEEE80211_MCS },
232168e8e04eSSam Leffler 		{   9, IFM_IEEE80211_MCS },
232268e8e04eSSam Leffler 		{  10, IFM_IEEE80211_MCS },
232368e8e04eSSam Leffler 		{  11, IFM_IEEE80211_MCS },
232468e8e04eSSam Leffler 		{  12, IFM_IEEE80211_MCS },
232568e8e04eSSam Leffler 		{  13, IFM_IEEE80211_MCS },
232668e8e04eSSam Leffler 		{  14, IFM_IEEE80211_MCS },
232768e8e04eSSam Leffler 		{  15, IFM_IEEE80211_MCS },
2328f136f45fSBernhard Schmidt 		{  16, IFM_IEEE80211_MCS },
2329f136f45fSBernhard Schmidt 		{  17, IFM_IEEE80211_MCS },
2330f136f45fSBernhard Schmidt 		{  18, IFM_IEEE80211_MCS },
2331f136f45fSBernhard Schmidt 		{  19, IFM_IEEE80211_MCS },
2332f136f45fSBernhard Schmidt 		{  20, IFM_IEEE80211_MCS },
2333f136f45fSBernhard Schmidt 		{  21, IFM_IEEE80211_MCS },
2334f136f45fSBernhard Schmidt 		{  22, IFM_IEEE80211_MCS },
2335f136f45fSBernhard Schmidt 		{  23, IFM_IEEE80211_MCS },
2336f136f45fSBernhard Schmidt 		{  24, IFM_IEEE80211_MCS },
2337f136f45fSBernhard Schmidt 		{  25, IFM_IEEE80211_MCS },
2338f136f45fSBernhard Schmidt 		{  26, IFM_IEEE80211_MCS },
2339f136f45fSBernhard Schmidt 		{  27, IFM_IEEE80211_MCS },
2340f136f45fSBernhard Schmidt 		{  28, IFM_IEEE80211_MCS },
2341f136f45fSBernhard Schmidt 		{  29, IFM_IEEE80211_MCS },
2342f136f45fSBernhard Schmidt 		{  30, IFM_IEEE80211_MCS },
2343f136f45fSBernhard Schmidt 		{  31, IFM_IEEE80211_MCS },
2344f136f45fSBernhard Schmidt 		{  32, IFM_IEEE80211_MCS },
2345f136f45fSBernhard Schmidt 		{  33, IFM_IEEE80211_MCS },
2346f136f45fSBernhard Schmidt 		{  34, IFM_IEEE80211_MCS },
2347f136f45fSBernhard Schmidt 		{  35, IFM_IEEE80211_MCS },
2348f136f45fSBernhard Schmidt 		{  36, IFM_IEEE80211_MCS },
2349f136f45fSBernhard Schmidt 		{  37, IFM_IEEE80211_MCS },
2350f136f45fSBernhard Schmidt 		{  38, IFM_IEEE80211_MCS },
2351f136f45fSBernhard Schmidt 		{  39, IFM_IEEE80211_MCS },
2352f136f45fSBernhard Schmidt 		{  40, IFM_IEEE80211_MCS },
2353f136f45fSBernhard Schmidt 		{  41, IFM_IEEE80211_MCS },
2354f136f45fSBernhard Schmidt 		{  42, IFM_IEEE80211_MCS },
2355f136f45fSBernhard Schmidt 		{  43, IFM_IEEE80211_MCS },
2356f136f45fSBernhard Schmidt 		{  44, IFM_IEEE80211_MCS },
2357f136f45fSBernhard Schmidt 		{  45, IFM_IEEE80211_MCS },
2358f136f45fSBernhard Schmidt 		{  46, IFM_IEEE80211_MCS },
2359f136f45fSBernhard Schmidt 		{  47, IFM_IEEE80211_MCS },
2360f136f45fSBernhard Schmidt 		{  48, IFM_IEEE80211_MCS },
2361f136f45fSBernhard Schmidt 		{  49, IFM_IEEE80211_MCS },
2362f136f45fSBernhard Schmidt 		{  50, IFM_IEEE80211_MCS },
2363f136f45fSBernhard Schmidt 		{  51, IFM_IEEE80211_MCS },
2364f136f45fSBernhard Schmidt 		{  52, IFM_IEEE80211_MCS },
2365f136f45fSBernhard Schmidt 		{  53, IFM_IEEE80211_MCS },
2366f136f45fSBernhard Schmidt 		{  54, IFM_IEEE80211_MCS },
2367f136f45fSBernhard Schmidt 		{  55, IFM_IEEE80211_MCS },
2368f136f45fSBernhard Schmidt 		{  56, IFM_IEEE80211_MCS },
2369f136f45fSBernhard Schmidt 		{  57, IFM_IEEE80211_MCS },
2370f136f45fSBernhard Schmidt 		{  58, IFM_IEEE80211_MCS },
2371f136f45fSBernhard Schmidt 		{  59, IFM_IEEE80211_MCS },
2372f136f45fSBernhard Schmidt 		{  60, IFM_IEEE80211_MCS },
2373f136f45fSBernhard Schmidt 		{  61, IFM_IEEE80211_MCS },
2374f136f45fSBernhard Schmidt 		{  62, IFM_IEEE80211_MCS },
2375f136f45fSBernhard Schmidt 		{  63, IFM_IEEE80211_MCS },
2376f136f45fSBernhard Schmidt 		{  64, IFM_IEEE80211_MCS },
2377f136f45fSBernhard Schmidt 		{  65, IFM_IEEE80211_MCS },
2378f136f45fSBernhard Schmidt 		{  66, IFM_IEEE80211_MCS },
2379f136f45fSBernhard Schmidt 		{  67, IFM_IEEE80211_MCS },
2380f136f45fSBernhard Schmidt 		{  68, IFM_IEEE80211_MCS },
2381f136f45fSBernhard Schmidt 		{  69, IFM_IEEE80211_MCS },
2382f136f45fSBernhard Schmidt 		{  70, IFM_IEEE80211_MCS },
2383f136f45fSBernhard Schmidt 		{  71, IFM_IEEE80211_MCS },
2384f136f45fSBernhard Schmidt 		{  72, IFM_IEEE80211_MCS },
2385f136f45fSBernhard Schmidt 		{  73, IFM_IEEE80211_MCS },
2386f136f45fSBernhard Schmidt 		{  74, IFM_IEEE80211_MCS },
2387f136f45fSBernhard Schmidt 		{  75, IFM_IEEE80211_MCS },
2388f136f45fSBernhard Schmidt 		{  76, IFM_IEEE80211_MCS },
238968e8e04eSSam Leffler 	};
239068e8e04eSSam Leffler 	int m;
23911a1e1d21SSam Leffler 
239268e8e04eSSam Leffler 	/*
239368e8e04eSSam Leffler 	 * Check 11n rates first for match as an MCS.
239468e8e04eSSam Leffler 	 */
239568e8e04eSSam Leffler 	if (mode == IEEE80211_MODE_11NA) {
2396f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
2397f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
2398a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
239968e8e04eSSam Leffler 			if (m != IFM_AUTO)
240068e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NA;
240168e8e04eSSam Leffler 		}
240268e8e04eSSam Leffler 	} else if (mode == IEEE80211_MODE_11NG) {
240368e8e04eSSam Leffler 		/* NB: 12 is ambiguous, it will be treated as an MCS */
2404f0ee92d5SSam Leffler 		if (rate & IEEE80211_RATE_MCS) {
2405f0ee92d5SSam Leffler 			rate &= ~IEEE80211_RATE_MCS;
2406a3e08d6fSRui Paulo 			m = findmedia(htrates, nitems(htrates), rate);
240768e8e04eSSam Leffler 			if (m != IFM_AUTO)
240868e8e04eSSam Leffler 				return m | IFM_IEEE80211_11NG;
240968e8e04eSSam Leffler 		}
241068e8e04eSSam Leffler 	}
241168e8e04eSSam Leffler 	rate &= IEEE80211_RATE_VAL;
24121a1e1d21SSam Leffler 	switch (mode) {
24131a1e1d21SSam Leffler 	case IEEE80211_MODE_11A:
24146a76ae21SSam Leffler 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
24156a76ae21SSam Leffler 	case IEEE80211_MODE_QUARTER:
241668e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
24178a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_A:
241868e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
2419a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2420a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11A);
24211a1e1d21SSam Leffler 	case IEEE80211_MODE_11B:
2422a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2423a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_11B);
24244844aa7dSAtsushi Onoe 	case IEEE80211_MODE_FH:
2425a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates),
2426a3e08d6fSRui Paulo 		    rate | IFM_IEEE80211_FH);
24271a1e1d21SSam Leffler 	case IEEE80211_MODE_AUTO:
24281a1e1d21SSam Leffler 		/* NB: ic may be NULL for some drivers */
2429566d825bSSam Leffler 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2430a3e08d6fSRui Paulo 			return findmedia(rates, nitems(rates),
243168e8e04eSSam Leffler 			    rate | IFM_IEEE80211_FH);
24321a1e1d21SSam Leffler 		/* NB: hack, 11g matches both 11b+11a rates */
24331a1e1d21SSam Leffler 		/* fall thru... */
24341a1e1d21SSam Leffler 	case IEEE80211_MODE_11G:
243568e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
24368a1b9b6aSSam Leffler 	case IEEE80211_MODE_TURBO_G:
2437a3e08d6fSRui Paulo 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
24387aebd3e5SAdrian Chadd 	case IEEE80211_MODE_VHT_2GHZ:
24397aebd3e5SAdrian Chadd 	case IEEE80211_MODE_VHT_5GHZ:
24407aebd3e5SAdrian Chadd 		/* XXX TODO: need to figure out mapping for VHT rates */
24417aebd3e5SAdrian Chadd 		return IFM_AUTO;
24421a1e1d21SSam Leffler 	}
24431a1e1d21SSam Leffler 	return IFM_AUTO;
24441a1e1d21SSam Leffler }
24451a1e1d21SSam Leffler 
24461a1e1d21SSam Leffler int
24471a1e1d21SSam Leffler ieee80211_media2rate(int mword)
24481a1e1d21SSam Leffler {
24491a1e1d21SSam Leffler 	static const int ieeerates[] = {
24501a1e1d21SSam Leffler 		-1,		/* IFM_AUTO */
24511a1e1d21SSam Leffler 		0,		/* IFM_MANUAL */
24521a1e1d21SSam Leffler 		0,		/* IFM_NONE */
24531a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_FH1 */
24541a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_FH2 */
24551a1e1d21SSam Leffler 		2,		/* IFM_IEEE80211_DS1 */
24561a1e1d21SSam Leffler 		4,		/* IFM_IEEE80211_DS2 */
24571a1e1d21SSam Leffler 		11,		/* IFM_IEEE80211_DS5 */
24581a1e1d21SSam Leffler 		22,		/* IFM_IEEE80211_DS11 */
24591a1e1d21SSam Leffler 		44,		/* IFM_IEEE80211_DS22 */
24601a1e1d21SSam Leffler 		12,		/* IFM_IEEE80211_OFDM6 */
24611a1e1d21SSam Leffler 		18,		/* IFM_IEEE80211_OFDM9 */
24621a1e1d21SSam Leffler 		24,		/* IFM_IEEE80211_OFDM12 */
24631a1e1d21SSam Leffler 		36,		/* IFM_IEEE80211_OFDM18 */
24641a1e1d21SSam Leffler 		48,		/* IFM_IEEE80211_OFDM24 */
24651a1e1d21SSam Leffler 		72,		/* IFM_IEEE80211_OFDM36 */
24661a1e1d21SSam Leffler 		96,		/* IFM_IEEE80211_OFDM48 */
24671a1e1d21SSam Leffler 		108,		/* IFM_IEEE80211_OFDM54 */
24681a1e1d21SSam Leffler 		144,		/* IFM_IEEE80211_OFDM72 */
246941b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS354k */
247041b3c790SSam Leffler 		0,		/* IFM_IEEE80211_DS512k */
247141b3c790SSam Leffler 		6,		/* IFM_IEEE80211_OFDM3 */
247241b3c790SSam Leffler 		9,		/* IFM_IEEE80211_OFDM4 */
247341b3c790SSam Leffler 		54,		/* IFM_IEEE80211_OFDM27 */
247468e8e04eSSam Leffler 		-1,		/* IFM_IEEE80211_MCS */
24757aebd3e5SAdrian Chadd 		-1,		/* IFM_IEEE80211_VHT */
24761a1e1d21SSam Leffler 	};
2477a3e08d6fSRui Paulo 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
24781a1e1d21SSam Leffler 		ieeerates[IFM_SUBTYPE(mword)] : 0;
24791a1e1d21SSam Leffler }
24805b16c28cSSam Leffler 
24815b16c28cSSam Leffler /*
24825b16c28cSSam Leffler  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
24835b16c28cSSam Leffler  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
24845b16c28cSSam Leffler  */
24855b16c28cSSam Leffler #define	mix(a, b, c)							\
24865b16c28cSSam Leffler do {									\
24875b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 13);					\
24885b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 8);					\
24895b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 13);					\
24905b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 12);					\
24915b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 16);					\
24925b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 5);					\
24935b16c28cSSam Leffler 	a -= b; a -= c; a ^= (c >> 3);					\
24945b16c28cSSam Leffler 	b -= c; b -= a; b ^= (a << 10);					\
24955b16c28cSSam Leffler 	c -= a; c -= b; c ^= (b >> 15);					\
24965b16c28cSSam Leffler } while (/*CONSTCOND*/0)
24975b16c28cSSam Leffler 
24985b16c28cSSam Leffler uint32_t
24995b16c28cSSam Leffler ieee80211_mac_hash(const struct ieee80211com *ic,
25005b16c28cSSam Leffler 	const uint8_t addr[IEEE80211_ADDR_LEN])
25015b16c28cSSam Leffler {
25025b16c28cSSam Leffler 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
25035b16c28cSSam Leffler 
25045b16c28cSSam Leffler 	b += addr[5] << 8;
25055b16c28cSSam Leffler 	b += addr[4];
25065b16c28cSSam Leffler 	a += addr[3] << 24;
25075b16c28cSSam Leffler 	a += addr[2] << 16;
25085b16c28cSSam Leffler 	a += addr[1] << 8;
25095b16c28cSSam Leffler 	a += addr[0];
25105b16c28cSSam Leffler 
25115b16c28cSSam Leffler 	mix(a, b, c);
25125b16c28cSSam Leffler 
25135b16c28cSSam Leffler 	return c;
25145b16c28cSSam Leffler }
25155b16c28cSSam Leffler #undef mix
2516a1cbd043SAdrian Chadd 
2517a1cbd043SAdrian Chadd char
2518a1cbd043SAdrian Chadd ieee80211_channel_type_char(const struct ieee80211_channel *c)
2519a1cbd043SAdrian Chadd {
2520a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ST(c))
2521a1cbd043SAdrian Chadd 		return 'S';
2522a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108A(c))
2523a1cbd043SAdrian Chadd 		return 'T';
2524a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_108G(c))
2525a1cbd043SAdrian Chadd 		return 'G';
25267aebd3e5SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(c))
25277aebd3e5SAdrian Chadd 		return 'v';
2528a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(c))
2529a1cbd043SAdrian Chadd 		return 'n';
2530a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_A(c))
2531a1cbd043SAdrian Chadd 		return 'a';
2532a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_ANYG(c))
2533a1cbd043SAdrian Chadd 		return 'g';
2534a1cbd043SAdrian Chadd 	if (IEEE80211_IS_CHAN_B(c))
2535a1cbd043SAdrian Chadd 		return 'b';
2536a1cbd043SAdrian Chadd 	return 'f';
2537a1cbd043SAdrian Chadd }
2538