ieee80211.c revision 219599
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211.c 219599 2011-03-13 11:47:43Z bschmidt $");
29116742Ssam
30116742Ssam/*
31116742Ssam * IEEE 802.11 generic handler
32116742Ssam */
33178354Ssam#include "opt_wlan.h"
34116742Ssam
35116742Ssam#include <sys/param.h>
36191746Sthompsa#include <sys/systm.h>
37116742Ssam#include <sys/kernel.h>
38182742Sbrooks
39116742Ssam#include <sys/socket.h>
40116742Ssam
41116742Ssam#include <net/if.h>
42178354Ssam#include <net/if_dl.h>
43116742Ssam#include <net/if_media.h>
44178354Ssam#include <net/if_types.h>
45116742Ssam#include <net/ethernet.h>
46116742Ssam
47116742Ssam#include <net80211/ieee80211_var.h>
48178354Ssam#include <net80211/ieee80211_regdomain.h>
49190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
50190391Ssam#include <net80211/ieee80211_superg.h>
51190391Ssam#endif
52206358Srpaulo#include <net80211/ieee80211_ratectl.h>
53116742Ssam
54116742Ssam#include <net/bpf.h>
55116742Ssam
56178955Ssamconst char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
57178955Ssam	[IEEE80211_MODE_AUTO]	  = "auto",
58178955Ssam	[IEEE80211_MODE_11A]	  = "11a",
59178955Ssam	[IEEE80211_MODE_11B]	  = "11b",
60178955Ssam	[IEEE80211_MODE_11G]	  = "11g",
61178955Ssam	[IEEE80211_MODE_FH]	  = "FH",
62178955Ssam	[IEEE80211_MODE_TURBO_A]  = "turboA",
63178955Ssam	[IEEE80211_MODE_TURBO_G]  = "turboG",
64178955Ssam	[IEEE80211_MODE_STURBO_A] = "sturboA",
65188782Ssam	[IEEE80211_MODE_HALF]	  = "half",
66188782Ssam	[IEEE80211_MODE_QUARTER]  = "quarter",
67178955Ssam	[IEEE80211_MODE_11NA]	  = "11na",
68178955Ssam	[IEEE80211_MODE_11NG]	  = "11ng",
69116742Ssam};
70178957Ssam/* map ieee80211_opmode to the corresponding capability bit */
71178957Ssamconst int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
72178957Ssam	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
73178957Ssam	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
74178957Ssam	[IEEE80211_M_STA]	= IEEE80211_C_STA,
75178957Ssam	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
76178957Ssam	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
77178957Ssam	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
78195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
79195618Srpaulo	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
80195618Srpaulo#endif
81178957Ssam};
82178957Ssam
83178354Ssamstatic const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
84178354Ssam	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
85116742Ssam
86178354Ssamstatic	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
87193655Ssamstatic	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
88178354Ssamstatic	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
89178354Ssamstatic	int ieee80211_media_setup(struct ieee80211com *ic,
90178354Ssam		struct ifmedia *media, int caps, int addsta,
91178354Ssam		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
92178354Ssamstatic	void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
93178354Ssamstatic	int ieee80211com_media_change(struct ifnet *);
94178354Ssamstatic	int media_status(enum ieee80211_opmode,
95178354Ssam		const struct ieee80211_channel *);
96178354Ssam
97178354SsamMALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
98178354Ssam
99164645Ssam/*
100164645Ssam * Default supported rates for 802.11 operation (in IEEE .5Mb units).
101164645Ssam */
102164645Ssam#define	B(r)	((r) | IEEE80211_RATE_BASIC)
103164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11a =
104164645Ssam	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
105165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_half =
106165569Ssam	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
107165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_quarter =
108165569Ssam	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
109164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11b =
110164645Ssam	{ 4, { B(2), B(4), B(11), B(22) } };
111164645Ssam/* NB: OFDM rates are handled specially based on mode */
112164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11g =
113164645Ssam	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
114164645Ssam#undef B
115164645Ssam
116140915Ssam/*
117165569Ssam * Fill in 802.11 available channel set, mark
118165569Ssam * all available channels as active, and pick
119165569Ssam * a default channel if not already specified.
120165569Ssam */
121165569Ssamstatic void
122165569Ssamieee80211_chan_init(struct ieee80211com *ic)
123116742Ssam{
124165569Ssam#define	DEFAULTRATES(m, def) do { \
125188782Ssam	if (ic->ic_sup_rates[m].rs_nrates == 0) \
126165574Ssam		ic->ic_sup_rates[m] = def; \
127165569Ssam} while (0)
128116742Ssam	struct ieee80211_channel *c;
129116742Ssam	int i;
130116742Ssam
131186107Ssam	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
132170530Ssam		("invalid number of channels specified: %u", ic->ic_nchans));
133116742Ssam	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
134178354Ssam	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
135167468Ssam	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
136170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
137116742Ssam		c = &ic->ic_channels[i];
138170530Ssam		KASSERT(c->ic_flags != 0, ("channel with no flags"));
139187796Ssam		/*
140187796Ssam		 * Help drivers that work only with frequencies by filling
141187796Ssam		 * in IEEE channel #'s if not already calculated.  Note this
142187796Ssam		 * mimics similar work done in ieee80211_setregdomain when
143187796Ssam		 * changing regulatory state.
144187796Ssam		 */
145187796Ssam		if (c->ic_ieee == 0)
146187796Ssam			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
147187796Ssam		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
148187796Ssam			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
149187796Ssam			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
150187796Ssam			    c->ic_flags);
151187796Ssam		/* default max tx power to max regulatory */
152187796Ssam		if (c->ic_maxpower == 0)
153187796Ssam			c->ic_maxpower = 2*c->ic_maxregpower;
154170530Ssam		setbit(ic->ic_chan_avail, c->ic_ieee);
155170530Ssam		/*
156170530Ssam		 * Identify mode capabilities.
157170530Ssam		 */
158170530Ssam		if (IEEE80211_IS_CHAN_A(c))
159170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
160170530Ssam		if (IEEE80211_IS_CHAN_B(c))
161170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
162170530Ssam		if (IEEE80211_IS_CHAN_ANYG(c))
163170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
164170530Ssam		if (IEEE80211_IS_CHAN_FHSS(c))
165170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
166170530Ssam		if (IEEE80211_IS_CHAN_108A(c))
167170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
168170530Ssam		if (IEEE80211_IS_CHAN_108G(c))
169170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
170170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
171170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
172188782Ssam		if (IEEE80211_IS_CHAN_HALF(c))
173188782Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
174188782Ssam		if (IEEE80211_IS_CHAN_QUARTER(c))
175188782Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
176170530Ssam		if (IEEE80211_IS_CHAN_HTA(c))
177170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
178170530Ssam		if (IEEE80211_IS_CHAN_HTG(c))
179170530Ssam			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
180116742Ssam	}
181170530Ssam	/* initialize candidate channels to all available */
182170530Ssam	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
183170530Ssam		sizeof(ic->ic_chan_avail));
184164645Ssam
185178354Ssam	/* sort channel table to allow lookup optimizations */
186178354Ssam	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
187178354Ssam
188178354Ssam	/* invalidate any previous state */
189170530Ssam	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
190172233Ssam	ic->ic_prevchan = NULL;
191178354Ssam	ic->ic_csa_newchan = NULL;
192170530Ssam	/* arbitrarily pick the first channel */
193170530Ssam	ic->ic_curchan = &ic->ic_channels[0];
194190532Ssam	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
195170530Ssam
196164645Ssam	/* fillin well-known rate sets if driver has not specified */
197165569Ssam	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
198165569Ssam	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
199165569Ssam	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
200165569Ssam	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
201165569Ssam	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
202187897Ssam	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
203188782Ssam	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
204188782Ssam	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
205188774Ssam	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
206188774Ssam	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
207165569Ssam
208165569Ssam	/*
209219596Sbschmidt	 * Setup required information to fill the mcsset field, if driver did
210219596Sbschmidt	 * not. Assume a 2T2R setup for historic reasons.
211219596Sbschmidt	 */
212219596Sbschmidt	if (ic->ic_rxstream == 0)
213219596Sbschmidt		ic->ic_rxstream = 2;
214219596Sbschmidt	if (ic->ic_txstream == 0)
215219596Sbschmidt		ic->ic_txstream = 2;
216219596Sbschmidt
217219596Sbschmidt	/*
218165569Ssam	 * Set auto mode to reset active channel state and any desired channel.
219165569Ssam	 */
220165569Ssam	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
221165569Ssam#undef DEFAULTRATES
222165569Ssam}
223165569Ssam
224178354Ssamstatic void
225178354Ssamnull_update_mcast(struct ifnet *ifp)
226178354Ssam{
227178354Ssam	if_printf(ifp, "need multicast update callback\n");
228178354Ssam}
229178354Ssam
230178354Ssamstatic void
231178354Ssamnull_update_promisc(struct ifnet *ifp)
232178354Ssam{
233178354Ssam	if_printf(ifp, "need promiscuous mode update callback\n");
234178354Ssam}
235178354Ssam
236178521Ssamstatic int
237195846Ssamnull_transmit(struct ifnet *ifp, struct mbuf *m)
238195846Ssam{
239195846Ssam	m_freem(m);
240195846Ssam	ifp->if_oerrors++;
241195846Ssam	return EACCES;		/* XXX EIO/EPERM? */
242195846Ssam}
243195846Ssam
244195846Ssamstatic int
245178521Ssamnull_output(struct ifnet *ifp, struct mbuf *m,
246191148Skmacy	struct sockaddr *dst, struct route *ro)
247178521Ssam{
248178521Ssam	if_printf(ifp, "discard raw packet\n");
249195846Ssam	return null_transmit(ifp, m);
250178521Ssam}
251178521Ssam
252178521Ssamstatic void
253178521Ssamnull_input(struct ifnet *ifp, struct mbuf *m)
254178521Ssam{
255178521Ssam	if_printf(ifp, "if_input should not be called\n");
256178521Ssam	m_freem(m);
257178521Ssam}
258178521Ssam
259178354Ssam/*
260178354Ssam * Attach/setup the common net80211 state.  Called by
261178354Ssam * the driver on attach to prior to creating any vap's.
262178354Ssam */
263165569Ssamvoid
264190526Ssamieee80211_ifattach(struct ieee80211com *ic,
265190526Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
266165569Ssam{
267165569Ssam	struct ifnet *ifp = ic->ic_ifp;
268178354Ssam	struct sockaddr_dl *sdl;
269178354Ssam	struct ifaddr *ifa;
270165569Ssam
271178354Ssam	KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
272165569Ssam
273179388Ssam	IEEE80211_LOCK_INIT(ic, ifp->if_xname);
274178354Ssam	TAILQ_INIT(&ic->ic_vaps);
275191746Sthompsa
276191746Sthompsa	/* Create a taskqueue for all state changes */
277191746Sthompsa	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
278191746Sthompsa	    taskqueue_thread_enqueue, &ic->ic_tq);
279191746Sthompsa	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s taskq",
280191746Sthompsa	    ifp->if_xname);
281165569Ssam	/*
282165569Ssam	 * Fill in 802.11 available channel set, mark all
283165569Ssam	 * available channels as active, and pick a default
284165569Ssam	 * channel if not already specified.
285165569Ssam	 */
286178354Ssam	ieee80211_media_init(ic);
287170530Ssam
288178354Ssam	ic->ic_update_mcast = null_update_mcast;
289178354Ssam	ic->ic_update_promisc = null_update_promisc;
290116742Ssam
291195379Ssam	ic->ic_hash_key = arc4random();
292155688Ssam	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
293155688Ssam	ic->ic_lintval = ic->ic_bintval;
294138568Ssam	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
295138568Ssam
296170530Ssam	ieee80211_crypto_attach(ic);
297138568Ssam	ieee80211_node_attach(ic);
298170530Ssam	ieee80211_power_attach(ic);
299138568Ssam	ieee80211_proto_attach(ic);
300190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
301190391Ssam	ieee80211_superg_attach(ic);
302190391Ssam#endif
303170530Ssam	ieee80211_ht_attach(ic);
304170530Ssam	ieee80211_scan_attach(ic);
305178354Ssam	ieee80211_regdomain_attach(ic);
306193843Ssam	ieee80211_dfs_attach(ic);
307138568Ssam
308178354Ssam	ieee80211_sysctl_attach(ic);
309138568Ssam
310178354Ssam	ifp->if_addrlen = IEEE80211_ADDR_LEN;
311178354Ssam	ifp->if_hdrlen = 0;
312178354Ssam	if_attach(ifp);
313178354Ssam	ifp->if_mtu = IEEE80211_MTU_MAX;
314178354Ssam	ifp->if_broadcastaddr = ieee80211broadcastaddr;
315178521Ssam	ifp->if_output = null_output;
316178521Ssam	ifp->if_input = null_input;	/* just in case */
317178521Ssam	ifp->if_resolvemulti = NULL;	/* NB: callers check */
318140915Ssam
319178354Ssam	ifa = ifaddr_byindex(ifp->if_index);
320178354Ssam	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
321178354Ssam	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
322178354Ssam	sdl->sdl_type = IFT_ETHER;		/* XXX IFT_IEEE80211? */
323178354Ssam	sdl->sdl_alen = IEEE80211_ADDR_LEN;
324190526Ssam	IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
325194760Srwatson	ifa_free(ifa);
326116742Ssam}
327116742Ssam
328178354Ssam/*
329178354Ssam * Detach net80211 state on device detach.  Tear down
330178354Ssam * all vap's and reclaim all common state prior to the
331178354Ssam * device state going away.  Note we may call back into
332178354Ssam * driver; it must be prepared for this.
333178354Ssam */
334116742Ssamvoid
335138568Ssamieee80211_ifdetach(struct ieee80211com *ic)
336116742Ssam{
337138568Ssam	struct ifnet *ifp = ic->ic_ifp;
338178354Ssam	struct ieee80211vap *vap;
339116742Ssam
340193337Ssam	if_detach(ifp);
341193337Ssam
342178354Ssam	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
343178354Ssam		ieee80211_vap_destroy(vap);
344188533Sthompsa	ieee80211_waitfor_parent(ic);
345138568Ssam
346138568Ssam	ieee80211_sysctl_detach(ic);
347193843Ssam	ieee80211_dfs_detach(ic);
348178354Ssam	ieee80211_regdomain_detach(ic);
349170530Ssam	ieee80211_scan_detach(ic);
350190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
351190391Ssam	ieee80211_superg_detach(ic);
352190391Ssam#endif
353170530Ssam	ieee80211_ht_detach(ic);
354166012Ssam	/* NB: must be called before ieee80211_node_detach */
355138568Ssam	ieee80211_proto_detach(ic);
356138568Ssam	ieee80211_crypto_detach(ic);
357170530Ssam	ieee80211_power_detach(ic);
358138568Ssam	ieee80211_node_detach(ic);
359193337Ssam
360116742Ssam	ifmedia_removeall(&ic->ic_media);
361191746Sthompsa	taskqueue_free(ic->ic_tq);
362170530Ssam	IEEE80211_LOCK_DESTROY(ic);
363178354Ssam}
364138568Ssam
365178354Ssam/*
366178354Ssam * Default reset method for use with the ioctl support.  This
367178354Ssam * method is invoked after any state change in the 802.11
368178354Ssam * layer that should be propagated to the hardware but not
369178354Ssam * require re-initialization of the 802.11 state machine (e.g
370178354Ssam * rescanning for an ap).  We always return ENETRESET which
371178354Ssam * should cause the driver to re-initialize the device. Drivers
372178354Ssam * can override this method to implement more optimized support.
373178354Ssam */
374178354Ssamstatic int
375178354Ssamdefault_reset(struct ieee80211vap *vap, u_long cmd)
376178354Ssam{
377178354Ssam	return ENETRESET;
378178354Ssam}
379178354Ssam
380178354Ssam/*
381178354Ssam * Prepare a vap for use.  Drivers use this call to
382178354Ssam * setup net80211 state in new vap's prior attaching
383178354Ssam * them with ieee80211_vap_attach (below).
384178354Ssam */
385178354Ssamint
386178354Ssamieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
387178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
388178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
389178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
390178354Ssam{
391178354Ssam	struct ifnet *ifp;
392178354Ssam
393178354Ssam	ifp = if_alloc(IFT_ETHER);
394178354Ssam	if (ifp == NULL) {
395178354Ssam		if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
396178354Ssam		    __func__);
397178354Ssam		return ENOMEM;
398178354Ssam	}
399178354Ssam	if_initname(ifp, name, unit);
400178354Ssam	ifp->if_softc = vap;			/* back pointer */
401178354Ssam	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
402178354Ssam	ifp->if_start = ieee80211_start;
403178354Ssam	ifp->if_ioctl = ieee80211_ioctl;
404178354Ssam	ifp->if_init = ieee80211_init;
405178354Ssam	/* NB: input+output filled in by ether_ifattach */
406207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
407207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
408178354Ssam	IFQ_SET_READY(&ifp->if_snd);
409178354Ssam
410178354Ssam	vap->iv_ifp = ifp;
411178354Ssam	vap->iv_ic = ic;
412178354Ssam	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
413178354Ssam	vap->iv_flags_ext = ic->ic_flags_ext;
414178354Ssam	vap->iv_flags_ven = ic->ic_flags_ven;
415178354Ssam	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
416178354Ssam	vap->iv_htcaps = ic->ic_htcaps;
417205513Srpaulo	vap->iv_htextcaps = ic->ic_htextcaps;
418178354Ssam	vap->iv_opmode = opmode;
419178957Ssam	vap->iv_caps |= ieee80211_opcap[opmode];
420178354Ssam	switch (opmode) {
421178354Ssam	case IEEE80211_M_WDS:
422178354Ssam		/*
423178354Ssam		 * WDS links must specify the bssid of the far end.
424178354Ssam		 * For legacy operation this is a static relationship.
425178354Ssam		 * For non-legacy operation the station must associate
426178354Ssam		 * and be authorized to pass traffic.  Plumbing the
427178354Ssam		 * vap to the proper node happens when the vap
428178354Ssam		 * transitions to RUN state.
429178354Ssam		 */
430178354Ssam		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
431178354Ssam		vap->iv_flags |= IEEE80211_F_DESBSSID;
432178354Ssam		if (flags & IEEE80211_CLONE_WDSLEGACY)
433178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
434178354Ssam		break;
435186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
436186904Ssam	case IEEE80211_M_AHDEMO:
437186904Ssam		if (flags & IEEE80211_CLONE_TDMA) {
438186904Ssam			/* NB: checked before clone operation allowed */
439186904Ssam			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
440186904Ssam			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
441186904Ssam			/*
442186904Ssam			 * Propagate TDMA capability to mark vap; this
443186904Ssam			 * cannot be removed and is used to distinguish
444186904Ssam			 * regular ahdemo operation from ahdemo+tdma.
445186904Ssam			 */
446186904Ssam			vap->iv_caps |= IEEE80211_C_TDMA;
447186904Ssam		}
448186904Ssam		break;
449186904Ssam#endif
450178354Ssam	}
451184278Ssam	/* auto-enable s/w beacon miss support */
452184278Ssam	if (flags & IEEE80211_CLONE_NOBEACONS)
453184278Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
454202612Sthompsa	/* auto-generated or user supplied MAC address */
455202612Sthompsa	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
456202612Sthompsa		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
457178354Ssam	/*
458178354Ssam	 * Enable various functionality by default if we're
459178354Ssam	 * capable; the driver can override us if it knows better.
460178354Ssam	 */
461178354Ssam	if (vap->iv_caps & IEEE80211_C_WME)
462178354Ssam		vap->iv_flags |= IEEE80211_F_WME;
463178354Ssam	if (vap->iv_caps & IEEE80211_C_BURST)
464178354Ssam		vap->iv_flags |= IEEE80211_F_BURST;
465178354Ssam	/* NB: bg scanning only makes sense for station mode right now */
466178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
467178354Ssam	    (vap->iv_caps & IEEE80211_C_BGSCAN))
468178354Ssam		vap->iv_flags |= IEEE80211_F_BGSCAN;
469178957Ssam	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
470178954Ssam	/* NB: DFS support only makes sense for ap mode right now */
471178954Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
472178954Ssam	    (vap->iv_caps & IEEE80211_C_DFS))
473178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
474178354Ssam
475178354Ssam	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
476178354Ssam	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
477178354Ssam	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
478178354Ssam	/*
479178354Ssam	 * Install a default reset method for the ioctl support;
480178354Ssam	 * the driver can override this.
481178354Ssam	 */
482178354Ssam	vap->iv_reset = default_reset;
483178354Ssam
484178354Ssam	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
485178354Ssam
486178354Ssam	ieee80211_sysctl_vattach(vap);
487178354Ssam	ieee80211_crypto_vattach(vap);
488178354Ssam	ieee80211_node_vattach(vap);
489178354Ssam	ieee80211_power_vattach(vap);
490178354Ssam	ieee80211_proto_vattach(vap);
491190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
492190391Ssam	ieee80211_superg_vattach(vap);
493190391Ssam#endif
494178354Ssam	ieee80211_ht_vattach(vap);
495178354Ssam	ieee80211_scan_vattach(vap);
496178354Ssam	ieee80211_regdomain_vattach(vap);
497192468Ssam	ieee80211_radiotap_vattach(vap);
498214894Sbschmidt	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
499178354Ssam
500178354Ssam	return 0;
501178354Ssam}
502178354Ssam
503178354Ssam/*
504178354Ssam * Activate a vap.  State should have been prepared with a
505178354Ssam * call to ieee80211_vap_setup and by the driver.  On return
506178354Ssam * from this call the vap is ready for use.
507178354Ssam */
508178354Ssamint
509178354Ssamieee80211_vap_attach(struct ieee80211vap *vap,
510178354Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
511178354Ssam{
512178354Ssam	struct ifnet *ifp = vap->iv_ifp;
513178354Ssam	struct ieee80211com *ic = vap->iv_ic;
514178354Ssam	struct ifmediareq imr;
515178354Ssam	int maxrate;
516178354Ssam
517178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
518178354Ssam	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
519178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
520178354Ssam	    ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
521178354Ssam
522178354Ssam	/*
523178354Ssam	 * Do late attach work that cannot happen until after
524178354Ssam	 * the driver has had a chance to override defaults.
525178354Ssam	 */
526178354Ssam	ieee80211_node_latevattach(vap);
527178354Ssam	ieee80211_power_latevattach(vap);
528178354Ssam
529178354Ssam	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
530178354Ssam	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
531178354Ssam	ieee80211_media_status(ifp, &imr);
532178354Ssam	/* NB: strip explicit mode; we're actually in autoselect */
533188106Ssam	ifmedia_set(&vap->iv_media,
534188106Ssam	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
535178354Ssam	if (maxrate)
536178354Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
537178354Ssam
538178354Ssam	ether_ifattach(ifp, vap->iv_myaddr);
539195846Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR) {
540195846Ssam		/* NB: disallow transmit */
541195846Ssam		ifp->if_transmit = null_transmit;
542195846Ssam		ifp->if_output = null_output;
543195846Ssam	} else {
544195846Ssam		/* hook output method setup by ether_ifattach */
545195846Ssam		vap->iv_output = ifp->if_output;
546195846Ssam		ifp->if_output = ieee80211_output;
547195846Ssam	}
548178354Ssam	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
549178354Ssam
550178354Ssam	IEEE80211_LOCK(ic);
551178354Ssam	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
552178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
553190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
554178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
555190391Ssam#endif
556178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
557178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
558193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
559193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
560178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
561178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
562178354Ssam	IEEE80211_UNLOCK(ic);
563178354Ssam
564178354Ssam	return 1;
565178354Ssam}
566178354Ssam
567178354Ssam/*
568178354Ssam * Tear down vap state and reclaim the ifnet.
569178354Ssam * The driver is assumed to have prepared for
570178354Ssam * this; e.g. by turning off interrupts for the
571178354Ssam * underlying device.
572178354Ssam */
573178354Ssamvoid
574178354Ssamieee80211_vap_detach(struct ieee80211vap *vap)
575178354Ssam{
576178354Ssam	struct ieee80211com *ic = vap->iv_ic;
577178354Ssam	struct ifnet *ifp = vap->iv_ifp;
578178354Ssam
579178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
580178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
581178354Ssam	    ic->ic_ifp->if_xname);
582178354Ssam
583193312Ssam	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
584193312Ssam	ether_ifdetach(ifp);
585178354Ssam
586193312Ssam	ieee80211_stop(vap);
587193312Ssam
588191746Sthompsa	/*
589191746Sthompsa	 * Flush any deferred vap tasks.
590191746Sthompsa	 */
591191746Sthompsa	ieee80211_draintask(ic, &vap->iv_nstate_task);
592191746Sthompsa	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
593191746Sthompsa
594196159Ssam	/* XXX band-aid until ifnet handles this for us */
595196159Ssam	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
596196159Ssam
597191746Sthompsa	IEEE80211_LOCK(ic);
598191746Sthompsa	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
599178354Ssam	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
600178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
601190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
602178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
603190391Ssam#endif
604178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
605178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
606193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
607193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
608192468Ssam	/* NB: this handles the bpfdetach done below */
609192468Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
610178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
611178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
612178354Ssam	IEEE80211_UNLOCK(ic);
613178354Ssam
614178354Ssam	ifmedia_removeall(&vap->iv_media);
615178354Ssam
616192468Ssam	ieee80211_radiotap_vdetach(vap);
617178354Ssam	ieee80211_regdomain_vdetach(vap);
618178354Ssam	ieee80211_scan_vdetach(vap);
619190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
620190391Ssam	ieee80211_superg_vdetach(vap);
621190391Ssam#endif
622178354Ssam	ieee80211_ht_vdetach(vap);
623178354Ssam	/* NB: must be before ieee80211_node_vdetach */
624178354Ssam	ieee80211_proto_vdetach(vap);
625178354Ssam	ieee80211_crypto_vdetach(vap);
626178354Ssam	ieee80211_power_vdetach(vap);
627178354Ssam	ieee80211_node_vdetach(vap);
628178354Ssam	ieee80211_sysctl_vdetach(vap);
629182674Sweongyo
630182674Sweongyo	if_free(ifp);
631116742Ssam}
632116742Ssam
633178354Ssam/*
634178354Ssam * Synchronize flag bit state in the parent ifnet structure
635178354Ssam * according to the state of all vap ifnet's.  This is used,
636178354Ssam * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
637178354Ssam */
638178354Ssamvoid
639178354Ssamieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
640178354Ssam{
641178354Ssam	struct ifnet *ifp = ic->ic_ifp;
642178354Ssam	struct ieee80211vap *vap;
643178354Ssam	int bit, oflags;
644178354Ssam
645178354Ssam	IEEE80211_LOCK_ASSERT(ic);
646178354Ssam
647178354Ssam	bit = 0;
648178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
649178354Ssam		if (vap->iv_ifp->if_flags & flag) {
650178354Ssam			/*
651178354Ssam			 * XXX the bridge sets PROMISC but we don't want to
652178354Ssam			 * enable it on the device, discard here so all the
653178354Ssam			 * drivers don't need to special-case it
654178354Ssam			 */
655178354Ssam			if (flag == IFF_PROMISC &&
656195847Ssam			    !(vap->iv_opmode == IEEE80211_M_MONITOR ||
657196004Ssam			      (vap->iv_opmode == IEEE80211_M_AHDEMO &&
658196004Ssam			       (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
659178354Ssam				continue;
660178354Ssam			bit = 1;
661178354Ssam			break;
662178354Ssam		}
663178354Ssam	oflags = ifp->if_flags;
664178354Ssam	if (bit)
665178354Ssam		ifp->if_flags |= flag;
666178354Ssam	else
667178354Ssam		ifp->if_flags &= ~flag;
668178354Ssam	if ((ifp->if_flags ^ oflags) & flag) {
669178354Ssam		/* XXX should we return 1/0 and let caller do this? */
670178354Ssam		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
671178354Ssam			if (flag == IFF_PROMISC)
672191746Sthompsa				ieee80211_runtask(ic, &ic->ic_promisc_task);
673178354Ssam			else if (flag == IFF_ALLMULTI)
674191746Sthompsa				ieee80211_runtask(ic, &ic->ic_mcast_task);
675178354Ssam		}
676178354Ssam	}
677178354Ssam}
678178354Ssam
679178354Ssam/*
680178354Ssam * Synchronize flag bit state in the com structure
681178354Ssam * according to the state of all vap's.  This is used,
682178354Ssam * for example, to handle state changes via ioctls.
683178354Ssam */
684178354Ssamstatic void
685178354Ssamieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
686178354Ssam{
687178354Ssam	struct ieee80211vap *vap;
688178354Ssam	int bit;
689178354Ssam
690178354Ssam	IEEE80211_LOCK_ASSERT(ic);
691178354Ssam
692178354Ssam	bit = 0;
693178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
694178354Ssam		if (vap->iv_flags & flag) {
695178354Ssam			bit = 1;
696178354Ssam			break;
697178354Ssam		}
698178354Ssam	if (bit)
699178354Ssam		ic->ic_flags |= flag;
700178354Ssam	else
701178354Ssam		ic->ic_flags &= ~flag;
702178354Ssam}
703178354Ssam
704178354Ssamvoid
705178354Ssamieee80211_syncflag(struct ieee80211vap *vap, int flag)
706178354Ssam{
707178354Ssam	struct ieee80211com *ic = vap->iv_ic;
708178354Ssam
709178354Ssam	IEEE80211_LOCK(ic);
710178354Ssam	if (flag < 0) {
711178354Ssam		flag = -flag;
712178354Ssam		vap->iv_flags &= ~flag;
713178354Ssam	} else
714178354Ssam		vap->iv_flags |= flag;
715178354Ssam	ieee80211_syncflag_locked(ic, flag);
716178354Ssam	IEEE80211_UNLOCK(ic);
717178354Ssam}
718178354Ssam
719178354Ssam/*
720193655Ssam * Synchronize flags_ht bit state in the com structure
721178354Ssam * according to the state of all vap's.  This is used,
722178354Ssam * for example, to handle state changes via ioctls.
723178354Ssam */
724178354Ssamstatic void
725193655Ssamieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
726193655Ssam{
727193655Ssam	struct ieee80211vap *vap;
728193655Ssam	int bit;
729193655Ssam
730193655Ssam	IEEE80211_LOCK_ASSERT(ic);
731193655Ssam
732193655Ssam	bit = 0;
733193655Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
734193655Ssam		if (vap->iv_flags_ht & flag) {
735193655Ssam			bit = 1;
736193655Ssam			break;
737193655Ssam		}
738193655Ssam	if (bit)
739193655Ssam		ic->ic_flags_ht |= flag;
740193655Ssam	else
741193655Ssam		ic->ic_flags_ht &= ~flag;
742193655Ssam}
743193655Ssam
744193655Ssamvoid
745193655Ssamieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
746193655Ssam{
747193655Ssam	struct ieee80211com *ic = vap->iv_ic;
748193655Ssam
749193655Ssam	IEEE80211_LOCK(ic);
750193655Ssam	if (flag < 0) {
751193655Ssam		flag = -flag;
752193655Ssam		vap->iv_flags_ht &= ~flag;
753193655Ssam	} else
754193655Ssam		vap->iv_flags_ht |= flag;
755193655Ssam	ieee80211_syncflag_ht_locked(ic, flag);
756193655Ssam	IEEE80211_UNLOCK(ic);
757193655Ssam}
758193655Ssam
759193655Ssam/*
760193655Ssam * Synchronize flags_ext bit state in the com structure
761193655Ssam * according to the state of all vap's.  This is used,
762193655Ssam * for example, to handle state changes via ioctls.
763193655Ssam */
764193655Ssamstatic void
765178354Ssamieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
766178354Ssam{
767178354Ssam	struct ieee80211vap *vap;
768178354Ssam	int bit;
769178354Ssam
770178354Ssam	IEEE80211_LOCK_ASSERT(ic);
771178354Ssam
772178354Ssam	bit = 0;
773178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
774178354Ssam		if (vap->iv_flags_ext & flag) {
775178354Ssam			bit = 1;
776178354Ssam			break;
777178354Ssam		}
778178354Ssam	if (bit)
779178354Ssam		ic->ic_flags_ext |= flag;
780178354Ssam	else
781178354Ssam		ic->ic_flags_ext &= ~flag;
782178354Ssam}
783178354Ssam
784178354Ssamvoid
785178354Ssamieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
786178354Ssam{
787178354Ssam	struct ieee80211com *ic = vap->iv_ic;
788178354Ssam
789178354Ssam	IEEE80211_LOCK(ic);
790178354Ssam	if (flag < 0) {
791178354Ssam		flag = -flag;
792178354Ssam		vap->iv_flags_ext &= ~flag;
793178354Ssam	} else
794178354Ssam		vap->iv_flags_ext |= flag;
795178354Ssam	ieee80211_syncflag_ext_locked(ic, flag);
796178354Ssam	IEEE80211_UNLOCK(ic);
797178354Ssam}
798178354Ssam
799166012Ssamstatic __inline int
800166012Ssammapgsm(u_int freq, u_int flags)
801166012Ssam{
802166012Ssam	freq *= 10;
803166012Ssam	if (flags & IEEE80211_CHAN_QUARTER)
804166012Ssam		freq += 5;
805166012Ssam	else if (flags & IEEE80211_CHAN_HALF)
806166012Ssam		freq += 10;
807166012Ssam	else
808166012Ssam		freq += 20;
809166012Ssam	/* NB: there is no 907/20 wide but leave room */
810166012Ssam	return (freq - 906*10) / 5;
811166012Ssam}
812166012Ssam
813166012Ssamstatic __inline int
814166012Ssammappsb(u_int freq, u_int flags)
815166012Ssam{
816166012Ssam	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
817166012Ssam}
818166012Ssam
819116742Ssam/*
820116742Ssam * Convert MHz frequency to IEEE channel number.
821116742Ssam */
822152450Ssamint
823116742Ssamieee80211_mhz2ieee(u_int freq, u_int flags)
824116742Ssam{
825167430Ssam#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
826166012Ssam	if (flags & IEEE80211_CHAN_GSM)
827166012Ssam		return mapgsm(freq, flags);
828116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
829116742Ssam		if (freq == 2484)
830116742Ssam			return 14;
831116742Ssam		if (freq < 2484)
832152450Ssam			return ((int) freq - 2407) / 5;
833116742Ssam		else
834116742Ssam			return 15 + ((freq - 2512) / 20);
835116899Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
836165569Ssam		if (freq <= 5000) {
837170530Ssam			/* XXX check regdomain? */
838167430Ssam			if (IS_FREQ_IN_PSB(freq))
839166012Ssam				return mappsb(freq, flags);
840152450Ssam			return (freq - 4000) / 5;
841165569Ssam		} else
842152450Ssam			return (freq - 5000) / 5;
843116742Ssam	} else {				/* either, guess */
844116742Ssam		if (freq == 2484)
845116742Ssam			return 14;
846166012Ssam		if (freq < 2484) {
847166012Ssam			if (907 <= freq && freq <= 922)
848166012Ssam				return mapgsm(freq, flags);
849152450Ssam			return ((int) freq - 2407) / 5;
850166012Ssam		}
851152450Ssam		if (freq < 5000) {
852167430Ssam			if (IS_FREQ_IN_PSB(freq))
853166012Ssam				return mappsb(freq, flags);
854165569Ssam			else if (freq > 4900)
855152450Ssam				return (freq - 4000) / 5;
856152450Ssam			else
857152450Ssam				return 15 + ((freq - 2512) / 20);
858152450Ssam		}
859116742Ssam		return (freq - 5000) / 5;
860116742Ssam	}
861167430Ssam#undef IS_FREQ_IN_PSB
862116742Ssam}
863116742Ssam
864116742Ssam/*
865116742Ssam * Convert channel to IEEE channel number.
866116742Ssam */
867152450Ssamint
868165825Smjacobieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
869116742Ssam{
870170530Ssam	if (c == NULL) {
871138568Ssam		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
872117039Ssam		return 0;		/* XXX */
873116742Ssam	}
874170530Ssam	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
875116742Ssam}
876116742Ssam
877116742Ssam/*
878116742Ssam * Convert IEEE channel number to MHz frequency.
879116742Ssam */
880116742Ssamu_int
881116742Ssamieee80211_ieee2mhz(u_int chan, u_int flags)
882116742Ssam{
883166012Ssam	if (flags & IEEE80211_CHAN_GSM)
884166012Ssam		return 907 + 5 * (chan / 10);
885116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
886116742Ssam		if (chan == 14)
887116742Ssam			return 2484;
888116742Ssam		if (chan < 14)
889116742Ssam			return 2407 + chan*5;
890116742Ssam		else
891116742Ssam			return 2512 + ((chan-15)*20);
892116742Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
893165569Ssam		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
894165569Ssam			chan -= 37;
895165569Ssam			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
896165569Ssam		}
897116742Ssam		return 5000 + (chan*5);
898116742Ssam	} else {				/* either, guess */
899166012Ssam		/* XXX can't distinguish PSB+GSM channels */
900116742Ssam		if (chan == 14)
901116742Ssam			return 2484;
902116742Ssam		if (chan < 14)			/* 0-13 */
903116742Ssam			return 2407 + chan*5;
904116742Ssam		if (chan < 27)			/* 15-26 */
905116742Ssam			return 2512 + ((chan-15)*20);
906116742Ssam		return 5000 + (chan*5);
907116742Ssam	}
908116742Ssam}
909116742Ssam
910116742Ssam/*
911170530Ssam * Locate a channel given a frequency+flags.  We cache
912178354Ssam * the previous lookup to optimize switching between two
913170530Ssam * channels--as happens with dynamic turbo.
914170530Ssam */
915170530Ssamstruct ieee80211_channel *
916170530Ssamieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
917170530Ssam{
918170530Ssam	struct ieee80211_channel *c;
919170530Ssam	int i;
920170530Ssam
921170530Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
922170530Ssam	c = ic->ic_prevchan;
923170530Ssam	if (c != NULL && c->ic_freq == freq &&
924170530Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
925170530Ssam		return c;
926170530Ssam	/* brute force search */
927170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
928170530Ssam		c = &ic->ic_channels[i];
929170530Ssam		if (c->ic_freq == freq &&
930170530Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
931170530Ssam			return c;
932170530Ssam	}
933170530Ssam	return NULL;
934170530Ssam}
935170530Ssam
936173861Ssam/*
937173861Ssam * Locate a channel given a channel number+flags.  We cache
938173861Ssam * the previous lookup to optimize switching between two
939173861Ssam * channels--as happens with dynamic turbo.
940173861Ssam */
941173861Ssamstruct ieee80211_channel *
942173861Ssamieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
943173861Ssam{
944173861Ssam	struct ieee80211_channel *c;
945173861Ssam	int i;
946173861Ssam
947173861Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
948173861Ssam	c = ic->ic_prevchan;
949173861Ssam	if (c != NULL && c->ic_ieee == ieee &&
950173861Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
951173861Ssam		return c;
952173861Ssam	/* brute force search */
953173861Ssam	for (i = 0; i < ic->ic_nchans; i++) {
954173861Ssam		c = &ic->ic_channels[i];
955173861Ssam		if (c->ic_ieee == ieee &&
956173861Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
957173861Ssam			return c;
958173861Ssam	}
959173861Ssam	return NULL;
960173861Ssam}
961173861Ssam
962170530Ssamstatic void
963178354Ssamaddmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
964170530Ssam{
965170530Ssam#define	ADD(_ic, _s, _o) \
966178354Ssam	ifmedia_add(media, \
967170530Ssam		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
968170530Ssam	static const u_int mopts[IEEE80211_MODE_MAX] = {
969188106Ssam	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
970188106Ssam	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
971188106Ssam	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
972188106Ssam	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
973188106Ssam	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
974188106Ssam	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
975188106Ssam	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
976188106Ssam	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
977188782Ssam	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
978188782Ssam	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
979188106Ssam	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
980188106Ssam	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
981170530Ssam	};
982170530Ssam	u_int mopt;
983170530Ssam
984170530Ssam	mopt = mopts[mode];
985178354Ssam	if (addsta)
986178354Ssam		ADD(ic, mword, mopt);	/* STA mode has no cap */
987178354Ssam	if (caps & IEEE80211_C_IBSS)
988178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
989178354Ssam	if (caps & IEEE80211_C_HOSTAP)
990178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
991178354Ssam	if (caps & IEEE80211_C_AHDEMO)
992178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
993178354Ssam	if (caps & IEEE80211_C_MONITOR)
994178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
995178354Ssam	if (caps & IEEE80211_C_WDS)
996178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
997195618Srpaulo	if (caps & IEEE80211_C_MBSS)
998195618Srpaulo		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
999170530Ssam#undef ADD
1000170530Ssam}
1001170530Ssam
1002170530Ssam/*
1003116742Ssam * Setup the media data structures according to the channel and
1004178354Ssam * rate tables.
1005116742Ssam */
1006178354Ssamstatic int
1007178354Ssamieee80211_media_setup(struct ieee80211com *ic,
1008178354Ssam	struct ifmedia *media, int caps, int addsta,
1009116742Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1010116742Ssam{
1011170530Ssam	int i, j, mode, rate, maxrate, mword, r;
1012170530Ssam	const struct ieee80211_rateset *rs;
1013116742Ssam	struct ieee80211_rateset allrates;
1014116742Ssam
1015118887Ssam	/*
1016116742Ssam	 * Fill in media characteristics.
1017116742Ssam	 */
1018178354Ssam	ifmedia_init(media, 0, media_change, media_stat);
1019116742Ssam	maxrate = 0;
1020170530Ssam	/*
1021170530Ssam	 * Add media for legacy operating modes.
1022170530Ssam	 */
1023116742Ssam	memset(&allrates, 0, sizeof(allrates));
1024170530Ssam	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1025167468Ssam		if (isclr(ic->ic_modecaps, mode))
1026116742Ssam			continue;
1027178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1028116742Ssam		if (mode == IEEE80211_MODE_AUTO)
1029116742Ssam			continue;
1030116742Ssam		rs = &ic->ic_sup_rates[mode];
1031116742Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1032116742Ssam			rate = rs->rs_rates[i];
1033116742Ssam			mword = ieee80211_rate2media(ic, rate, mode);
1034116742Ssam			if (mword == 0)
1035116742Ssam				continue;
1036178354Ssam			addmedia(media, caps, addsta, mode, mword);
1037116742Ssam			/*
1038170530Ssam			 * Add legacy rate to the collection of all rates.
1039116742Ssam			 */
1040116742Ssam			r = rate & IEEE80211_RATE_VAL;
1041116742Ssam			for (j = 0; j < allrates.rs_nrates; j++)
1042116742Ssam				if (allrates.rs_rates[j] == r)
1043116742Ssam					break;
1044116742Ssam			if (j == allrates.rs_nrates) {
1045116742Ssam				/* unique, add to the set */
1046116742Ssam				allrates.rs_rates[j] = r;
1047116742Ssam				allrates.rs_nrates++;
1048116742Ssam			}
1049116742Ssam			rate = (rate & IEEE80211_RATE_VAL) / 2;
1050116742Ssam			if (rate > maxrate)
1051116742Ssam				maxrate = rate;
1052116742Ssam		}
1053116742Ssam	}
1054116742Ssam	for (i = 0; i < allrates.rs_nrates; i++) {
1055116742Ssam		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1056116742Ssam				IEEE80211_MODE_AUTO);
1057116742Ssam		if (mword == 0)
1058116742Ssam			continue;
1059170530Ssam		/* NB: remove media options from mword */
1060178354Ssam		addmedia(media, caps, addsta,
1061178354Ssam		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1062116742Ssam	}
1063170530Ssam	/*
1064170530Ssam	 * Add HT/11n media.  Note that we do not have enough
1065170530Ssam	 * bits in the media subtype to express the MCS so we
1066170530Ssam	 * use a "placeholder" media subtype and any fixed MCS
1067170530Ssam	 * must be specified with a different mechanism.
1068170530Ssam	 */
1069188782Ssam	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1070170530Ssam		if (isclr(ic->ic_modecaps, mode))
1071170530Ssam			continue;
1072178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1073178354Ssam		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1074170530Ssam	}
1075170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1076170530Ssam	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1077178354Ssam		addmedia(media, caps, addsta,
1078178354Ssam		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1079219599Sbschmidt		i = ic->ic_txstream * 8 - 1;
1080219599Sbschmidt		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1081219599Sbschmidt		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1082219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_400ns;
1083219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1084219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_800ns;
1085219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1086219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_400ns;
1087219599Sbschmidt		else
1088219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_800ns;
1089219599Sbschmidt		if (rate > maxrate)
1090219599Sbschmidt			maxrate = rate;
1091170530Ssam	}
1092178354Ssam	return maxrate;
1093178354Ssam}
1094116742Ssam
1095178354Ssamvoid
1096178354Ssamieee80211_media_init(struct ieee80211com *ic)
1097178354Ssam{
1098178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1099178354Ssam	int maxrate;
1100178354Ssam
1101178354Ssam	/* NB: this works because the structure is initialized to zero */
1102178354Ssam	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1103178354Ssam		/*
1104178354Ssam		 * We are re-initializing the channel list; clear
1105178354Ssam		 * the existing media state as the media routines
1106178354Ssam		 * don't suppress duplicates.
1107178354Ssam		 */
1108178354Ssam		ifmedia_removeall(&ic->ic_media);
1109178354Ssam	}
1110178354Ssam	ieee80211_chan_init(ic);
1111178354Ssam
1112178354Ssam	/*
1113178354Ssam	 * Recalculate media settings in case new channel list changes
1114178354Ssam	 * the set of available modes.
1115178354Ssam	 */
1116178354Ssam	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1117178354Ssam		ieee80211com_media_change, ieee80211com_media_status);
1118170530Ssam	/* NB: strip explicit mode; we're actually in autoselect */
1119170530Ssam	ifmedia_set(&ic->ic_media,
1120188106Ssam	    media_status(ic->ic_opmode, ic->ic_curchan) &~
1121188106Ssam		(IFM_MMASK | IFM_IEEE80211_TURBO));
1122116742Ssam	if (maxrate)
1123116742Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
1124178354Ssam
1125178354Ssam	/* XXX need to propagate new media settings to vap's */
1126116742Ssam}
1127116742Ssam
1128188782Ssam/* XXX inline or eliminate? */
1129165569Ssamconst struct ieee80211_rateset *
1130165569Ssamieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1131165569Ssam{
1132188774Ssam	/* XXX does this work for 11ng basic rates? */
1133170530Ssam	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1134165569Ssam}
1135165569Ssam
1136138568Ssamvoid
1137138568Ssamieee80211_announce(struct ieee80211com *ic)
1138138568Ssam{
1139138568Ssam	struct ifnet *ifp = ic->ic_ifp;
1140138568Ssam	int i, mode, rate, mword;
1141170530Ssam	const struct ieee80211_rateset *rs;
1142138568Ssam
1143172227Ssam	/* NB: skip AUTO since it has no rates */
1144172227Ssam	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1145167468Ssam		if (isclr(ic->ic_modecaps, mode))
1146138568Ssam			continue;
1147138568Ssam		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1148138568Ssam		rs = &ic->ic_sup_rates[mode];
1149138568Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1150170530Ssam			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1151138568Ssam			if (mword == 0)
1152138568Ssam				continue;
1153170530Ssam			rate = ieee80211_media2rate(mword);
1154138568Ssam			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1155170530Ssam			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1156138568Ssam		}
1157138568Ssam		printf("\n");
1158138568Ssam	}
1159170530Ssam	ieee80211_ht_announce(ic);
1160138568Ssam}
1161138568Ssam
1162170530Ssamvoid
1163170530Ssamieee80211_announce_channels(struct ieee80211com *ic)
1164116742Ssam{
1165170530Ssam	const struct ieee80211_channel *c;
1166170530Ssam	char type;
1167170530Ssam	int i, cw;
1168170530Ssam
1169170530Ssam	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1170170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1171170530Ssam		c = &ic->ic_channels[i];
1172170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
1173170530Ssam			type = 'S';
1174170530Ssam		else if (IEEE80211_IS_CHAN_108A(c))
1175170530Ssam			type = 'T';
1176170530Ssam		else if (IEEE80211_IS_CHAN_108G(c))
1177170530Ssam			type = 'G';
1178170530Ssam		else if (IEEE80211_IS_CHAN_HT(c))
1179170530Ssam			type = 'n';
1180170530Ssam		else if (IEEE80211_IS_CHAN_A(c))
1181170530Ssam			type = 'a';
1182170530Ssam		else if (IEEE80211_IS_CHAN_ANYG(c))
1183170530Ssam			type = 'g';
1184170530Ssam		else if (IEEE80211_IS_CHAN_B(c))
1185170530Ssam			type = 'b';
1186170530Ssam		else
1187170530Ssam			type = 'f';
1188170530Ssam		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1189170530Ssam			cw = 40;
1190170530Ssam		else if (IEEE80211_IS_CHAN_HALF(c))
1191170530Ssam			cw = 10;
1192170530Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
1193170530Ssam			cw = 5;
1194170530Ssam		else
1195170530Ssam			cw = 20;
1196170530Ssam		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1197170530Ssam			, c->ic_ieee, c->ic_freq, type
1198170530Ssam			, cw
1199170530Ssam			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1200170530Ssam			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1201170530Ssam			, c->ic_maxregpower
1202170530Ssam			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1203170530Ssam			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1204170530Ssam		);
1205170530Ssam	}
1206116742Ssam}
1207116742Ssam
1208170530Ssamstatic int
1209184273Ssammedia2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1210170530Ssam{
1211116742Ssam	switch (IFM_MODE(ime->ifm_media)) {
1212116742Ssam	case IFM_IEEE80211_11A:
1213178354Ssam		*mode = IEEE80211_MODE_11A;
1214116742Ssam		break;
1215116742Ssam	case IFM_IEEE80211_11B:
1216178354Ssam		*mode = IEEE80211_MODE_11B;
1217116742Ssam		break;
1218116742Ssam	case IFM_IEEE80211_11G:
1219178354Ssam		*mode = IEEE80211_MODE_11G;
1220116742Ssam		break;
1221124543Sonoe	case IFM_IEEE80211_FH:
1222178354Ssam		*mode = IEEE80211_MODE_FH;
1223124543Sonoe		break;
1224170530Ssam	case IFM_IEEE80211_11NA:
1225178354Ssam		*mode = IEEE80211_MODE_11NA;
1226170530Ssam		break;
1227170530Ssam	case IFM_IEEE80211_11NG:
1228178354Ssam		*mode = IEEE80211_MODE_11NG;
1229170530Ssam		break;
1230116742Ssam	case IFM_AUTO:
1231178354Ssam		*mode = IEEE80211_MODE_AUTO;
1232116742Ssam		break;
1233116742Ssam	default:
1234178354Ssam		return 0;
1235116742Ssam	}
1236116742Ssam	/*
1237138568Ssam	 * Turbo mode is an ``option''.
1238138568Ssam	 * XXX does not apply to AUTO
1239116742Ssam	 */
1240116742Ssam	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1241178354Ssam		if (*mode == IEEE80211_MODE_11A) {
1242184273Ssam			if (flags & IEEE80211_F_TURBOP)
1243178354Ssam				*mode = IEEE80211_MODE_TURBO_A;
1244170530Ssam			else
1245178354Ssam				*mode = IEEE80211_MODE_STURBO_A;
1246178354Ssam		} else if (*mode == IEEE80211_MODE_11G)
1247178354Ssam			*mode = IEEE80211_MODE_TURBO_G;
1248138568Ssam		else
1249178354Ssam			return 0;
1250116742Ssam	}
1251170530Ssam	/* XXX HT40 +/- */
1252178354Ssam	return 1;
1253178354Ssam}
1254116742Ssam
1255178354Ssam/*
1256184273Ssam * Handle a media change request on the underlying interface.
1257178354Ssam */
1258178354Ssamint
1259178354Ssamieee80211com_media_change(struct ifnet *ifp)
1260178354Ssam{
1261184273Ssam	return EINVAL;
1262178354Ssam}
1263116742Ssam
1264178354Ssam/*
1265178354Ssam * Handle a media change request on the vap interface.
1266178354Ssam */
1267178354Ssamint
1268178354Ssamieee80211_media_change(struct ifnet *ifp)
1269178354Ssam{
1270178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1271178354Ssam	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1272184273Ssam	uint16_t newmode;
1273178354Ssam
1274184273Ssam	if (!media2mode(ime, vap->iv_flags, &newmode))
1275178354Ssam		return EINVAL;
1276184273Ssam	if (vap->iv_des_mode != newmode) {
1277184273Ssam		vap->iv_des_mode = newmode;
1278193340Ssam		/* XXX kick state machine if up+running */
1279178354Ssam	}
1280178354Ssam	return 0;
1281116742Ssam}
1282116742Ssam
1283170530Ssam/*
1284170530Ssam * Common code to calculate the media status word
1285170530Ssam * from the operating mode and channel state.
1286170530Ssam */
1287170530Ssamstatic int
1288170530Ssammedia_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1289170530Ssam{
1290170530Ssam	int status;
1291170530Ssam
1292170530Ssam	status = IFM_IEEE80211;
1293170530Ssam	switch (opmode) {
1294170530Ssam	case IEEE80211_M_STA:
1295170530Ssam		break;
1296170530Ssam	case IEEE80211_M_IBSS:
1297170530Ssam		status |= IFM_IEEE80211_ADHOC;
1298170530Ssam		break;
1299170530Ssam	case IEEE80211_M_HOSTAP:
1300170530Ssam		status |= IFM_IEEE80211_HOSTAP;
1301170530Ssam		break;
1302170530Ssam	case IEEE80211_M_MONITOR:
1303170530Ssam		status |= IFM_IEEE80211_MONITOR;
1304170530Ssam		break;
1305170530Ssam	case IEEE80211_M_AHDEMO:
1306170530Ssam		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1307170530Ssam		break;
1308170530Ssam	case IEEE80211_M_WDS:
1309178354Ssam		status |= IFM_IEEE80211_WDS;
1310170530Ssam		break;
1311195618Srpaulo	case IEEE80211_M_MBSS:
1312195618Srpaulo		status |= IFM_IEEE80211_MBSS;
1313195618Srpaulo		break;
1314170530Ssam	}
1315170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan)) {
1316170530Ssam		status |= IFM_IEEE80211_11NA;
1317170530Ssam	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1318170530Ssam		status |= IFM_IEEE80211_11NG;
1319170530Ssam	} else if (IEEE80211_IS_CHAN_A(chan)) {
1320170530Ssam		status |= IFM_IEEE80211_11A;
1321170530Ssam	} else if (IEEE80211_IS_CHAN_B(chan)) {
1322170530Ssam		status |= IFM_IEEE80211_11B;
1323170530Ssam	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1324170530Ssam		status |= IFM_IEEE80211_11G;
1325170530Ssam	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1326170530Ssam		status |= IFM_IEEE80211_FH;
1327170530Ssam	}
1328170530Ssam	/* XXX else complain? */
1329170530Ssam
1330170530Ssam	if (IEEE80211_IS_CHAN_TURBO(chan))
1331170530Ssam		status |= IFM_IEEE80211_TURBO;
1332178354Ssam#if 0
1333178354Ssam	if (IEEE80211_IS_CHAN_HT20(chan))
1334178354Ssam		status |= IFM_IEEE80211_HT20;
1335178354Ssam	if (IEEE80211_IS_CHAN_HT40(chan))
1336178354Ssam		status |= IFM_IEEE80211_HT40;
1337178354Ssam#endif
1338170530Ssam	return status;
1339170530Ssam}
1340170530Ssam
1341178354Ssamstatic void
1342178354Ssamieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1343178354Ssam{
1344178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1345178354Ssam	struct ieee80211vap *vap;
1346178354Ssam
1347178354Ssam	imr->ifm_status = IFM_AVALID;
1348178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1349178354Ssam		if (vap->iv_ifp->if_flags & IFF_UP) {
1350178354Ssam			imr->ifm_status |= IFM_ACTIVE;
1351178354Ssam			break;
1352178354Ssam		}
1353178354Ssam	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1354178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1355178354Ssam		imr->ifm_current = imr->ifm_active;
1356178354Ssam}
1357178354Ssam
1358116742Ssamvoid
1359116742Ssamieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1360116742Ssam{
1361178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1362178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1363170530Ssam	enum ieee80211_phymode mode;
1364116742Ssam
1365116742Ssam	imr->ifm_status = IFM_AVALID;
1366170530Ssam	/*
1367170530Ssam	 * NB: use the current channel's mode to lock down a xmit
1368170530Ssam	 * rate only when running; otherwise we may have a mismatch
1369170530Ssam	 * in which case the rate will not be convertible.
1370170530Ssam	 */
1371178354Ssam	if (vap->iv_state == IEEE80211_S_RUN) {
1372116742Ssam		imr->ifm_status |= IFM_ACTIVE;
1373170530Ssam		mode = ieee80211_chan2mode(ic->ic_curchan);
1374170530Ssam	} else
1375170530Ssam		mode = IEEE80211_MODE_AUTO;
1376178354Ssam	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1377138568Ssam	/*
1378138568Ssam	 * Calculate a current rate if possible.
1379138568Ssam	 */
1380178354Ssam	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1381138568Ssam		/*
1382138568Ssam		 * A fixed rate is set, report that.
1383138568Ssam		 */
1384138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1385178354Ssam			vap->iv_txparms[mode].ucastrate, mode);
1386178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1387138568Ssam		/*
1388138568Ssam		 * In station mode report the current transmit rate.
1389138568Ssam		 */
1390138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1391178354Ssam			vap->iv_bss->ni_txrate, mode);
1392128966Sandre	} else
1393138568Ssam		imr->ifm_active |= IFM_AUTO;
1394178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1395178354Ssam		imr->ifm_current = imr->ifm_active;
1396116742Ssam}
1397116742Ssam
1398116742Ssam/*
1399116742Ssam * Set the current phy mode and recalculate the active channel
1400116742Ssam * set based on the available channels for this mode.  Also
1401116742Ssam * select a new default/current channel if the current one is
1402116742Ssam * inappropriate for this mode.
1403116742Ssam */
1404116742Ssamint
1405116742Ssamieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1406116742Ssam{
1407116742Ssam	/*
1408166012Ssam	 * Adjust basic rates in 11b/11g supported rate set.
1409166012Ssam	 * Note that if operating on a hal/quarter rate channel
1410166012Ssam	 * this is a noop as those rates sets are different
1411166012Ssam	 * and used instead.
1412116742Ssam	 */
1413166012Ssam	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1414178354Ssam		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1415166012Ssam
1416116742Ssam	ic->ic_curmode = mode;
1417138568Ssam	ieee80211_reset_erp(ic);	/* reset ERP state */
1418138568Ssam
1419116742Ssam	return 0;
1420116742Ssam}
1421116742Ssam
1422116742Ssam/*
1423170530Ssam * Return the phy mode for with the specified channel.
1424116742Ssam */
1425116742Ssamenum ieee80211_phymode
1426170530Ssamieee80211_chan2mode(const struct ieee80211_channel *chan)
1427116742Ssam{
1428170530Ssam
1429170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan))
1430170530Ssam		return IEEE80211_MODE_11NA;
1431170530Ssam	else if (IEEE80211_IS_CHAN_HTG(chan))
1432170530Ssam		return IEEE80211_MODE_11NG;
1433170530Ssam	else if (IEEE80211_IS_CHAN_108G(chan))
1434170530Ssam		return IEEE80211_MODE_TURBO_G;
1435170530Ssam	else if (IEEE80211_IS_CHAN_ST(chan))
1436170530Ssam		return IEEE80211_MODE_STURBO_A;
1437170530Ssam	else if (IEEE80211_IS_CHAN_TURBO(chan))
1438153350Ssam		return IEEE80211_MODE_TURBO_A;
1439188782Ssam	else if (IEEE80211_IS_CHAN_HALF(chan))
1440188782Ssam		return IEEE80211_MODE_HALF;
1441188782Ssam	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1442188782Ssam		return IEEE80211_MODE_QUARTER;
1443170530Ssam	else if (IEEE80211_IS_CHAN_A(chan))
1444116742Ssam		return IEEE80211_MODE_11A;
1445170530Ssam	else if (IEEE80211_IS_CHAN_ANYG(chan))
1446116742Ssam		return IEEE80211_MODE_11G;
1447170530Ssam	else if (IEEE80211_IS_CHAN_B(chan))
1448116742Ssam		return IEEE80211_MODE_11B;
1449170530Ssam	else if (IEEE80211_IS_CHAN_FHSS(chan))
1450170530Ssam		return IEEE80211_MODE_FH;
1451170530Ssam
1452170530Ssam	/* NB: should not get here */
1453170530Ssam	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1454170530Ssam		__func__, chan->ic_freq, chan->ic_flags);
1455170530Ssam	return IEEE80211_MODE_11B;
1456116742Ssam}
1457116742Ssam
1458170530Ssamstruct ratemedia {
1459170530Ssam	u_int	match;	/* rate + mode */
1460170530Ssam	u_int	media;	/* if_media rate */
1461170530Ssam};
1462170530Ssam
1463170530Ssamstatic int
1464170530Ssamfindmedia(const struct ratemedia rates[], int n, u_int match)
1465170530Ssam{
1466170530Ssam	int i;
1467170530Ssam
1468170530Ssam	for (i = 0; i < n; i++)
1469170530Ssam		if (rates[i].match == match)
1470170530Ssam			return rates[i].media;
1471170530Ssam	return IFM_AUTO;
1472170530Ssam}
1473170530Ssam
1474116742Ssam/*
1475170530Ssam * Convert IEEE80211 rate value to ifmedia subtype.
1476170530Ssam * Rate is either a legacy rate in units of 0.5Mbps
1477170530Ssam * or an MCS index.
1478116742Ssam */
1479116742Ssamint
1480116742Ssamieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1481116742Ssam{
1482116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1483170530Ssam	static const struct ratemedia rates[] = {
1484124543Sonoe		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1485124543Sonoe		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1486124543Sonoe		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1487124543Sonoe		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1488124543Sonoe		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1489124543Sonoe		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1490124543Sonoe		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1491124543Sonoe		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1492124543Sonoe		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1493124543Sonoe		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1494124543Sonoe		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1495124543Sonoe		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1496124543Sonoe		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1497124543Sonoe		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1498124543Sonoe		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1499124543Sonoe		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1500124543Sonoe		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1501124543Sonoe		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1502124543Sonoe		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1503124543Sonoe		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1504124543Sonoe		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1505124543Sonoe		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1506124543Sonoe		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1507124543Sonoe		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1508124543Sonoe		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1509124543Sonoe		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1510124543Sonoe		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1511165569Ssam		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1512165569Ssam		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1513165569Ssam		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1514116742Ssam		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1515116742Ssam	};
1516170530Ssam	static const struct ratemedia htrates[] = {
1517170530Ssam		{   0, IFM_IEEE80211_MCS },
1518170530Ssam		{   1, IFM_IEEE80211_MCS },
1519170530Ssam		{   2, IFM_IEEE80211_MCS },
1520170530Ssam		{   3, IFM_IEEE80211_MCS },
1521170530Ssam		{   4, IFM_IEEE80211_MCS },
1522170530Ssam		{   5, IFM_IEEE80211_MCS },
1523170530Ssam		{   6, IFM_IEEE80211_MCS },
1524170530Ssam		{   7, IFM_IEEE80211_MCS },
1525170530Ssam		{   8, IFM_IEEE80211_MCS },
1526170530Ssam		{   9, IFM_IEEE80211_MCS },
1527170530Ssam		{  10, IFM_IEEE80211_MCS },
1528170530Ssam		{  11, IFM_IEEE80211_MCS },
1529170530Ssam		{  12, IFM_IEEE80211_MCS },
1530170530Ssam		{  13, IFM_IEEE80211_MCS },
1531170530Ssam		{  14, IFM_IEEE80211_MCS },
1532170530Ssam		{  15, IFM_IEEE80211_MCS },
1533219456Sbschmidt		{  16, IFM_IEEE80211_MCS },
1534219456Sbschmidt		{  17, IFM_IEEE80211_MCS },
1535219456Sbschmidt		{  18, IFM_IEEE80211_MCS },
1536219456Sbschmidt		{  19, IFM_IEEE80211_MCS },
1537219456Sbschmidt		{  20, IFM_IEEE80211_MCS },
1538219456Sbschmidt		{  21, IFM_IEEE80211_MCS },
1539219456Sbschmidt		{  22, IFM_IEEE80211_MCS },
1540219456Sbschmidt		{  23, IFM_IEEE80211_MCS },
1541219456Sbschmidt		{  24, IFM_IEEE80211_MCS },
1542219456Sbschmidt		{  25, IFM_IEEE80211_MCS },
1543219456Sbschmidt		{  26, IFM_IEEE80211_MCS },
1544219456Sbschmidt		{  27, IFM_IEEE80211_MCS },
1545219456Sbschmidt		{  28, IFM_IEEE80211_MCS },
1546219456Sbschmidt		{  29, IFM_IEEE80211_MCS },
1547219456Sbschmidt		{  30, IFM_IEEE80211_MCS },
1548219456Sbschmidt		{  31, IFM_IEEE80211_MCS },
1549219456Sbschmidt		{  32, IFM_IEEE80211_MCS },
1550219456Sbschmidt		{  33, IFM_IEEE80211_MCS },
1551219456Sbschmidt		{  34, IFM_IEEE80211_MCS },
1552219456Sbschmidt		{  35, IFM_IEEE80211_MCS },
1553219456Sbschmidt		{  36, IFM_IEEE80211_MCS },
1554219456Sbschmidt		{  37, IFM_IEEE80211_MCS },
1555219456Sbschmidt		{  38, IFM_IEEE80211_MCS },
1556219456Sbschmidt		{  39, IFM_IEEE80211_MCS },
1557219456Sbschmidt		{  40, IFM_IEEE80211_MCS },
1558219456Sbschmidt		{  41, IFM_IEEE80211_MCS },
1559219456Sbschmidt		{  42, IFM_IEEE80211_MCS },
1560219456Sbschmidt		{  43, IFM_IEEE80211_MCS },
1561219456Sbschmidt		{  44, IFM_IEEE80211_MCS },
1562219456Sbschmidt		{  45, IFM_IEEE80211_MCS },
1563219456Sbschmidt		{  46, IFM_IEEE80211_MCS },
1564219456Sbschmidt		{  47, IFM_IEEE80211_MCS },
1565219456Sbschmidt		{  48, IFM_IEEE80211_MCS },
1566219456Sbschmidt		{  49, IFM_IEEE80211_MCS },
1567219456Sbschmidt		{  50, IFM_IEEE80211_MCS },
1568219456Sbschmidt		{  51, IFM_IEEE80211_MCS },
1569219456Sbschmidt		{  52, IFM_IEEE80211_MCS },
1570219456Sbschmidt		{  53, IFM_IEEE80211_MCS },
1571219456Sbschmidt		{  54, IFM_IEEE80211_MCS },
1572219456Sbschmidt		{  55, IFM_IEEE80211_MCS },
1573219456Sbschmidt		{  56, IFM_IEEE80211_MCS },
1574219456Sbschmidt		{  57, IFM_IEEE80211_MCS },
1575219456Sbschmidt		{  58, IFM_IEEE80211_MCS },
1576219456Sbschmidt		{  59, IFM_IEEE80211_MCS },
1577219456Sbschmidt		{  60, IFM_IEEE80211_MCS },
1578219456Sbschmidt		{  61, IFM_IEEE80211_MCS },
1579219456Sbschmidt		{  62, IFM_IEEE80211_MCS },
1580219456Sbschmidt		{  63, IFM_IEEE80211_MCS },
1581219456Sbschmidt		{  64, IFM_IEEE80211_MCS },
1582219456Sbschmidt		{  65, IFM_IEEE80211_MCS },
1583219456Sbschmidt		{  66, IFM_IEEE80211_MCS },
1584219456Sbschmidt		{  67, IFM_IEEE80211_MCS },
1585219456Sbschmidt		{  68, IFM_IEEE80211_MCS },
1586219456Sbschmidt		{  69, IFM_IEEE80211_MCS },
1587219456Sbschmidt		{  70, IFM_IEEE80211_MCS },
1588219456Sbschmidt		{  71, IFM_IEEE80211_MCS },
1589219456Sbschmidt		{  72, IFM_IEEE80211_MCS },
1590219456Sbschmidt		{  73, IFM_IEEE80211_MCS },
1591219456Sbschmidt		{  74, IFM_IEEE80211_MCS },
1592219456Sbschmidt		{  75, IFM_IEEE80211_MCS },
1593219456Sbschmidt		{  76, IFM_IEEE80211_MCS },
1594170530Ssam	};
1595170530Ssam	int m;
1596116742Ssam
1597170530Ssam	/*
1598170530Ssam	 * Check 11n rates first for match as an MCS.
1599170530Ssam	 */
1600170530Ssam	if (mode == IEEE80211_MODE_11NA) {
1601172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1602172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1603170530Ssam			m = findmedia(htrates, N(htrates), rate);
1604170530Ssam			if (m != IFM_AUTO)
1605170530Ssam				return m | IFM_IEEE80211_11NA;
1606170530Ssam		}
1607170530Ssam	} else if (mode == IEEE80211_MODE_11NG) {
1608170530Ssam		/* NB: 12 is ambiguous, it will be treated as an MCS */
1609172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1610172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1611170530Ssam			m = findmedia(htrates, N(htrates), rate);
1612170530Ssam			if (m != IFM_AUTO)
1613170530Ssam				return m | IFM_IEEE80211_11NG;
1614170530Ssam		}
1615170530Ssam	}
1616170530Ssam	rate &= IEEE80211_RATE_VAL;
1617116742Ssam	switch (mode) {
1618116742Ssam	case IEEE80211_MODE_11A:
1619188782Ssam	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
1620188782Ssam	case IEEE80211_MODE_QUARTER:
1621170530Ssam	case IEEE80211_MODE_11NA:
1622138568Ssam	case IEEE80211_MODE_TURBO_A:
1623170530Ssam	case IEEE80211_MODE_STURBO_A:
1624170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1625116742Ssam	case IEEE80211_MODE_11B:
1626170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1627124543Sonoe	case IEEE80211_MODE_FH:
1628170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1629116742Ssam	case IEEE80211_MODE_AUTO:
1630116742Ssam		/* NB: ic may be NULL for some drivers */
1631188775Ssam		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1632170530Ssam			return findmedia(rates, N(rates),
1633170530Ssam			    rate | IFM_IEEE80211_FH);
1634116742Ssam		/* NB: hack, 11g matches both 11b+11a rates */
1635116742Ssam		/* fall thru... */
1636116742Ssam	case IEEE80211_MODE_11G:
1637170530Ssam	case IEEE80211_MODE_11NG:
1638138568Ssam	case IEEE80211_MODE_TURBO_G:
1639170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1640116742Ssam	}
1641116742Ssam	return IFM_AUTO;
1642116742Ssam#undef N
1643116742Ssam}
1644116742Ssam
1645116742Ssamint
1646116742Ssamieee80211_media2rate(int mword)
1647116742Ssam{
1648116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1649116742Ssam	static const int ieeerates[] = {
1650116742Ssam		-1,		/* IFM_AUTO */
1651116742Ssam		0,		/* IFM_MANUAL */
1652116742Ssam		0,		/* IFM_NONE */
1653116742Ssam		2,		/* IFM_IEEE80211_FH1 */
1654116742Ssam		4,		/* IFM_IEEE80211_FH2 */
1655116742Ssam		2,		/* IFM_IEEE80211_DS1 */
1656116742Ssam		4,		/* IFM_IEEE80211_DS2 */
1657116742Ssam		11,		/* IFM_IEEE80211_DS5 */
1658116742Ssam		22,		/* IFM_IEEE80211_DS11 */
1659116742Ssam		44,		/* IFM_IEEE80211_DS22 */
1660116742Ssam		12,		/* IFM_IEEE80211_OFDM6 */
1661116742Ssam		18,		/* IFM_IEEE80211_OFDM9 */
1662116742Ssam		24,		/* IFM_IEEE80211_OFDM12 */
1663116742Ssam		36,		/* IFM_IEEE80211_OFDM18 */
1664116742Ssam		48,		/* IFM_IEEE80211_OFDM24 */
1665116742Ssam		72,		/* IFM_IEEE80211_OFDM36 */
1666116742Ssam		96,		/* IFM_IEEE80211_OFDM48 */
1667116742Ssam		108,		/* IFM_IEEE80211_OFDM54 */
1668116742Ssam		144,		/* IFM_IEEE80211_OFDM72 */
1669165569Ssam		0,		/* IFM_IEEE80211_DS354k */
1670165569Ssam		0,		/* IFM_IEEE80211_DS512k */
1671165569Ssam		6,		/* IFM_IEEE80211_OFDM3 */
1672165569Ssam		9,		/* IFM_IEEE80211_OFDM4 */
1673165569Ssam		54,		/* IFM_IEEE80211_OFDM27 */
1674170530Ssam		-1,		/* IFM_IEEE80211_MCS */
1675116742Ssam	};
1676116742Ssam	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1677116742Ssam		ieeerates[IFM_SUBTYPE(mword)] : 0;
1678116742Ssam#undef N
1679116742Ssam}
1680195379Ssam
1681195379Ssam/*
1682195379Ssam * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1683195379Ssam * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1684195379Ssam */
1685195379Ssam#define	mix(a, b, c)							\
1686195379Ssamdo {									\
1687195379Ssam	a -= b; a -= c; a ^= (c >> 13);					\
1688195379Ssam	b -= c; b -= a; b ^= (a << 8);					\
1689195379Ssam	c -= a; c -= b; c ^= (b >> 13);					\
1690195379Ssam	a -= b; a -= c; a ^= (c >> 12);					\
1691195379Ssam	b -= c; b -= a; b ^= (a << 16);					\
1692195379Ssam	c -= a; c -= b; c ^= (b >> 5);					\
1693195379Ssam	a -= b; a -= c; a ^= (c >> 3);					\
1694195379Ssam	b -= c; b -= a; b ^= (a << 10);					\
1695195379Ssam	c -= a; c -= b; c ^= (b >> 15);					\
1696195379Ssam} while (/*CONSTCOND*/0)
1697195379Ssam
1698195379Ssamuint32_t
1699195379Ssamieee80211_mac_hash(const struct ieee80211com *ic,
1700195379Ssam	const uint8_t addr[IEEE80211_ADDR_LEN])
1701195379Ssam{
1702195379Ssam	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1703195379Ssam
1704195379Ssam	b += addr[5] << 8;
1705195379Ssam	b += addr[4];
1706195379Ssam	a += addr[3] << 24;
1707195379Ssam	a += addr[2] << 16;
1708195379Ssam	a += addr[1] << 8;
1709195379Ssam	a += addr[0];
1710195379Ssam
1711195379Ssam	mix(a, b, c);
1712195379Ssam
1713195379Ssam	return c;
1714195379Ssam}
1715195379Ssam#undef mix
1716