ieee80211.c revision 254076
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 254076 2013-08-07 22:01:43Z adrian $");
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
244254076Sadrian#if __FreeBSD_version >= 1000031
245195846Ssamstatic int
246178521Ssamnull_output(struct ifnet *ifp, struct mbuf *m,
247249925Sglebius	const struct sockaddr *dst, struct route *ro)
248254076Sadrian#else
249254076Sadrianstatic int
250254076Sadriannull_output(struct ifnet *ifp, struct mbuf *m,
251254076Sadrian	struct sockaddr *dst, struct route *ro)
252254076Sadrian#endif
253178521Ssam{
254178521Ssam	if_printf(ifp, "discard raw packet\n");
255195846Ssam	return null_transmit(ifp, m);
256178521Ssam}
257178521Ssam
258178521Ssamstatic void
259178521Ssamnull_input(struct ifnet *ifp, struct mbuf *m)
260178521Ssam{
261178521Ssam	if_printf(ifp, "if_input should not be called\n");
262178521Ssam	m_freem(m);
263178521Ssam}
264178521Ssam
265233452Sadrianstatic void
266233452Sadriannull_update_chw(struct ieee80211com *ic)
267233452Sadrian{
268233452Sadrian
269233452Sadrian	if_printf(ic->ic_ifp, "%s: need callback\n", __func__);
270233452Sadrian}
271233452Sadrian
272178354Ssam/*
273178354Ssam * Attach/setup the common net80211 state.  Called by
274178354Ssam * the driver on attach to prior to creating any vap's.
275178354Ssam */
276165569Ssamvoid
277190526Ssamieee80211_ifattach(struct ieee80211com *ic,
278190526Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
279165569Ssam{
280165569Ssam	struct ifnet *ifp = ic->ic_ifp;
281178354Ssam	struct sockaddr_dl *sdl;
282178354Ssam	struct ifaddr *ifa;
283165569Ssam
284178354Ssam	KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
285165569Ssam
286179388Ssam	IEEE80211_LOCK_INIT(ic, ifp->if_xname);
287248069Sadrian	IEEE80211_TX_LOCK_INIT(ic, ifp->if_xname);
288178354Ssam	TAILQ_INIT(&ic->ic_vaps);
289191746Sthompsa
290191746Sthompsa	/* Create a taskqueue for all state changes */
291191746Sthompsa	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
292191746Sthompsa	    taskqueue_thread_enqueue, &ic->ic_tq);
293230447Sadrian	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
294191746Sthompsa	    ifp->if_xname);
295165569Ssam	/*
296165569Ssam	 * Fill in 802.11 available channel set, mark all
297165569Ssam	 * available channels as active, and pick a default
298165569Ssam	 * channel if not already specified.
299165569Ssam	 */
300178354Ssam	ieee80211_media_init(ic);
301170530Ssam
302178354Ssam	ic->ic_update_mcast = null_update_mcast;
303178354Ssam	ic->ic_update_promisc = null_update_promisc;
304233452Sadrian	ic->ic_update_chw = null_update_chw;
305116742Ssam
306195379Ssam	ic->ic_hash_key = arc4random();
307155688Ssam	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
308155688Ssam	ic->ic_lintval = ic->ic_bintval;
309138568Ssam	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
310138568Ssam
311170530Ssam	ieee80211_crypto_attach(ic);
312138568Ssam	ieee80211_node_attach(ic);
313170530Ssam	ieee80211_power_attach(ic);
314138568Ssam	ieee80211_proto_attach(ic);
315190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
316190391Ssam	ieee80211_superg_attach(ic);
317190391Ssam#endif
318170530Ssam	ieee80211_ht_attach(ic);
319170530Ssam	ieee80211_scan_attach(ic);
320178354Ssam	ieee80211_regdomain_attach(ic);
321193843Ssam	ieee80211_dfs_attach(ic);
322138568Ssam
323178354Ssam	ieee80211_sysctl_attach(ic);
324138568Ssam
325178354Ssam	ifp->if_addrlen = IEEE80211_ADDR_LEN;
326178354Ssam	ifp->if_hdrlen = 0;
327242149Sadrian
328242149Sadrian	CURVNET_SET(vnet0);
329242149Sadrian
330178354Ssam	if_attach(ifp);
331242149Sadrian
332178354Ssam	ifp->if_mtu = IEEE80211_MTU_MAX;
333178354Ssam	ifp->if_broadcastaddr = ieee80211broadcastaddr;
334178521Ssam	ifp->if_output = null_output;
335178521Ssam	ifp->if_input = null_input;	/* just in case */
336178521Ssam	ifp->if_resolvemulti = NULL;	/* NB: callers check */
337140915Ssam
338178354Ssam	ifa = ifaddr_byindex(ifp->if_index);
339178354Ssam	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
340178354Ssam	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
341178354Ssam	sdl->sdl_type = IFT_ETHER;		/* XXX IFT_IEEE80211? */
342178354Ssam	sdl->sdl_alen = IEEE80211_ADDR_LEN;
343190526Ssam	IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
344194760Srwatson	ifa_free(ifa);
345242149Sadrian
346242149Sadrian	CURVNET_RESTORE();
347116742Ssam}
348116742Ssam
349178354Ssam/*
350178354Ssam * Detach net80211 state on device detach.  Tear down
351178354Ssam * all vap's and reclaim all common state prior to the
352178354Ssam * device state going away.  Note we may call back into
353178354Ssam * driver; it must be prepared for this.
354178354Ssam */
355116742Ssamvoid
356138568Ssamieee80211_ifdetach(struct ieee80211com *ic)
357116742Ssam{
358138568Ssam	struct ifnet *ifp = ic->ic_ifp;
359178354Ssam	struct ieee80211vap *vap;
360116742Ssam
361242149Sadrian	/*
362242149Sadrian	 * This detaches the main interface, but not the vaps.
363242149Sadrian	 * Each VAP may be in a separate VIMAGE.
364242149Sadrian	 */
365242149Sadrian	CURVNET_SET(ifp->if_vnet);
366193337Ssam	if_detach(ifp);
367242149Sadrian	CURVNET_RESTORE();
368193337Ssam
369242149Sadrian	/*
370242149Sadrian	 * The VAP is responsible for setting and clearing
371242149Sadrian	 * the VIMAGE context.
372242149Sadrian	 */
373178354Ssam	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
374178354Ssam		ieee80211_vap_destroy(vap);
375188533Sthompsa	ieee80211_waitfor_parent(ic);
376138568Ssam
377138568Ssam	ieee80211_sysctl_detach(ic);
378193843Ssam	ieee80211_dfs_detach(ic);
379178354Ssam	ieee80211_regdomain_detach(ic);
380170530Ssam	ieee80211_scan_detach(ic);
381190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
382190391Ssam	ieee80211_superg_detach(ic);
383190391Ssam#endif
384170530Ssam	ieee80211_ht_detach(ic);
385166012Ssam	/* NB: must be called before ieee80211_node_detach */
386138568Ssam	ieee80211_proto_detach(ic);
387138568Ssam	ieee80211_crypto_detach(ic);
388170530Ssam	ieee80211_power_detach(ic);
389138568Ssam	ieee80211_node_detach(ic);
390193337Ssam
391242149Sadrian	/* XXX VNET needed? */
392116742Ssam	ifmedia_removeall(&ic->ic_media);
393242149Sadrian
394191746Sthompsa	taskqueue_free(ic->ic_tq);
395248069Sadrian	IEEE80211_TX_LOCK_DESTROY(ic);
396170530Ssam	IEEE80211_LOCK_DESTROY(ic);
397178354Ssam}
398138568Ssam
399178354Ssam/*
400178354Ssam * Default reset method for use with the ioctl support.  This
401178354Ssam * method is invoked after any state change in the 802.11
402178354Ssam * layer that should be propagated to the hardware but not
403178354Ssam * require re-initialization of the 802.11 state machine (e.g
404178354Ssam * rescanning for an ap).  We always return ENETRESET which
405178354Ssam * should cause the driver to re-initialize the device. Drivers
406178354Ssam * can override this method to implement more optimized support.
407178354Ssam */
408178354Ssamstatic int
409178354Ssamdefault_reset(struct ieee80211vap *vap, u_long cmd)
410178354Ssam{
411178354Ssam	return ENETRESET;
412178354Ssam}
413178354Ssam
414178354Ssam/*
415178354Ssam * Prepare a vap for use.  Drivers use this call to
416178354Ssam * setup net80211 state in new vap's prior attaching
417178354Ssam * them with ieee80211_vap_attach (below).
418178354Ssam */
419178354Ssamint
420178354Ssamieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
421228621Sbschmidt    const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
422228621Sbschmidt    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
423228621Sbschmidt    const uint8_t macaddr[IEEE80211_ADDR_LEN])
424178354Ssam{
425178354Ssam	struct ifnet *ifp;
426178354Ssam
427178354Ssam	ifp = if_alloc(IFT_ETHER);
428178354Ssam	if (ifp == NULL) {
429178354Ssam		if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
430178354Ssam		    __func__);
431178354Ssam		return ENOMEM;
432178354Ssam	}
433178354Ssam	if_initname(ifp, name, unit);
434178354Ssam	ifp->if_softc = vap;			/* back pointer */
435178354Ssam	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
436178354Ssam	ifp->if_start = ieee80211_start;
437178354Ssam	ifp->if_ioctl = ieee80211_ioctl;
438178354Ssam	ifp->if_init = ieee80211_init;
439178354Ssam	/* NB: input+output filled in by ether_ifattach */
440207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
441207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
442178354Ssam	IFQ_SET_READY(&ifp->if_snd);
443178354Ssam
444178354Ssam	vap->iv_ifp = ifp;
445178354Ssam	vap->iv_ic = ic;
446178354Ssam	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
447178354Ssam	vap->iv_flags_ext = ic->ic_flags_ext;
448178354Ssam	vap->iv_flags_ven = ic->ic_flags_ven;
449178354Ssam	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
450178354Ssam	vap->iv_htcaps = ic->ic_htcaps;
451205513Srpaulo	vap->iv_htextcaps = ic->ic_htextcaps;
452178354Ssam	vap->iv_opmode = opmode;
453178957Ssam	vap->iv_caps |= ieee80211_opcap[opmode];
454178354Ssam	switch (opmode) {
455178354Ssam	case IEEE80211_M_WDS:
456178354Ssam		/*
457178354Ssam		 * WDS links must specify the bssid of the far end.
458178354Ssam		 * For legacy operation this is a static relationship.
459178354Ssam		 * For non-legacy operation the station must associate
460178354Ssam		 * and be authorized to pass traffic.  Plumbing the
461178354Ssam		 * vap to the proper node happens when the vap
462178354Ssam		 * transitions to RUN state.
463178354Ssam		 */
464178354Ssam		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
465178354Ssam		vap->iv_flags |= IEEE80211_F_DESBSSID;
466178354Ssam		if (flags & IEEE80211_CLONE_WDSLEGACY)
467178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
468178354Ssam		break;
469186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
470186904Ssam	case IEEE80211_M_AHDEMO:
471186904Ssam		if (flags & IEEE80211_CLONE_TDMA) {
472186904Ssam			/* NB: checked before clone operation allowed */
473186904Ssam			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
474186904Ssam			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
475186904Ssam			/*
476186904Ssam			 * Propagate TDMA capability to mark vap; this
477186904Ssam			 * cannot be removed and is used to distinguish
478186904Ssam			 * regular ahdemo operation from ahdemo+tdma.
479186904Ssam			 */
480186904Ssam			vap->iv_caps |= IEEE80211_C_TDMA;
481186904Ssam		}
482186904Ssam		break;
483186904Ssam#endif
484228621Sbschmidt	default:
485228621Sbschmidt		break;
486178354Ssam	}
487184278Ssam	/* auto-enable s/w beacon miss support */
488184278Ssam	if (flags & IEEE80211_CLONE_NOBEACONS)
489184278Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
490202612Sthompsa	/* auto-generated or user supplied MAC address */
491202612Sthompsa	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
492202612Sthompsa		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
493178354Ssam	/*
494178354Ssam	 * Enable various functionality by default if we're
495178354Ssam	 * capable; the driver can override us if it knows better.
496178354Ssam	 */
497178354Ssam	if (vap->iv_caps & IEEE80211_C_WME)
498178354Ssam		vap->iv_flags |= IEEE80211_F_WME;
499178354Ssam	if (vap->iv_caps & IEEE80211_C_BURST)
500178354Ssam		vap->iv_flags |= IEEE80211_F_BURST;
501178354Ssam	/* NB: bg scanning only makes sense for station mode right now */
502178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
503178354Ssam	    (vap->iv_caps & IEEE80211_C_BGSCAN))
504178354Ssam		vap->iv_flags |= IEEE80211_F_BGSCAN;
505178957Ssam	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
506178954Ssam	/* NB: DFS support only makes sense for ap mode right now */
507178954Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
508178954Ssam	    (vap->iv_caps & IEEE80211_C_DFS))
509178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
510178354Ssam
511178354Ssam	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
512178354Ssam	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
513178354Ssam	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
514178354Ssam	/*
515178354Ssam	 * Install a default reset method for the ioctl support;
516178354Ssam	 * the driver can override this.
517178354Ssam	 */
518178354Ssam	vap->iv_reset = default_reset;
519178354Ssam
520178354Ssam	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
521178354Ssam
522178354Ssam	ieee80211_sysctl_vattach(vap);
523178354Ssam	ieee80211_crypto_vattach(vap);
524178354Ssam	ieee80211_node_vattach(vap);
525178354Ssam	ieee80211_power_vattach(vap);
526178354Ssam	ieee80211_proto_vattach(vap);
527190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
528190391Ssam	ieee80211_superg_vattach(vap);
529190391Ssam#endif
530178354Ssam	ieee80211_ht_vattach(vap);
531178354Ssam	ieee80211_scan_vattach(vap);
532178354Ssam	ieee80211_regdomain_vattach(vap);
533192468Ssam	ieee80211_radiotap_vattach(vap);
534214894Sbschmidt	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
535178354Ssam
536178354Ssam	return 0;
537178354Ssam}
538178354Ssam
539178354Ssam/*
540178354Ssam * Activate a vap.  State should have been prepared with a
541178354Ssam * call to ieee80211_vap_setup and by the driver.  On return
542178354Ssam * from this call the vap is ready for use.
543178354Ssam */
544178354Ssamint
545178354Ssamieee80211_vap_attach(struct ieee80211vap *vap,
546178354Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
547178354Ssam{
548178354Ssam	struct ifnet *ifp = vap->iv_ifp;
549178354Ssam	struct ieee80211com *ic = vap->iv_ic;
550178354Ssam	struct ifmediareq imr;
551178354Ssam	int maxrate;
552178354Ssam
553178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
554178354Ssam	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
555178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
556178354Ssam	    ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
557178354Ssam
558178354Ssam	/*
559178354Ssam	 * Do late attach work that cannot happen until after
560178354Ssam	 * the driver has had a chance to override defaults.
561178354Ssam	 */
562178354Ssam	ieee80211_node_latevattach(vap);
563178354Ssam	ieee80211_power_latevattach(vap);
564178354Ssam
565178354Ssam	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
566178354Ssam	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
567178354Ssam	ieee80211_media_status(ifp, &imr);
568178354Ssam	/* NB: strip explicit mode; we're actually in autoselect */
569188106Ssam	ifmedia_set(&vap->iv_media,
570188106Ssam	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
571178354Ssam	if (maxrate)
572178354Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
573178354Ssam
574178354Ssam	ether_ifattach(ifp, vap->iv_myaddr);
575195846Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR) {
576195846Ssam		/* NB: disallow transmit */
577195846Ssam		ifp->if_transmit = null_transmit;
578195846Ssam		ifp->if_output = null_output;
579195846Ssam	} else {
580195846Ssam		/* hook output method setup by ether_ifattach */
581195846Ssam		vap->iv_output = ifp->if_output;
582195846Ssam		ifp->if_output = ieee80211_output;
583195846Ssam	}
584178354Ssam	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
585178354Ssam
586178354Ssam	IEEE80211_LOCK(ic);
587178354Ssam	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
588178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
589190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
590178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
591190391Ssam#endif
592178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
593178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
594193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
595193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
596178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
597178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
598178354Ssam	IEEE80211_UNLOCK(ic);
599178354Ssam
600178354Ssam	return 1;
601178354Ssam}
602178354Ssam
603178354Ssam/*
604178354Ssam * Tear down vap state and reclaim the ifnet.
605178354Ssam * The driver is assumed to have prepared for
606178354Ssam * this; e.g. by turning off interrupts for the
607178354Ssam * underlying device.
608178354Ssam */
609178354Ssamvoid
610178354Ssamieee80211_vap_detach(struct ieee80211vap *vap)
611178354Ssam{
612178354Ssam	struct ieee80211com *ic = vap->iv_ic;
613178354Ssam	struct ifnet *ifp = vap->iv_ifp;
614178354Ssam
615242149Sadrian	CURVNET_SET(ifp->if_vnet);
616242149Sadrian
617178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
618178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
619178354Ssam	    ic->ic_ifp->if_xname);
620178354Ssam
621193312Ssam	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
622193312Ssam	ether_ifdetach(ifp);
623178354Ssam
624193312Ssam	ieee80211_stop(vap);
625193312Ssam
626191746Sthompsa	/*
627191746Sthompsa	 * Flush any deferred vap tasks.
628191746Sthompsa	 */
629191746Sthompsa	ieee80211_draintask(ic, &vap->iv_nstate_task);
630191746Sthompsa	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
631191746Sthompsa
632196159Ssam	/* XXX band-aid until ifnet handles this for us */
633196159Ssam	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
634196159Ssam
635191746Sthompsa	IEEE80211_LOCK(ic);
636191746Sthompsa	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
637178354Ssam	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
638178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
639190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
640178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
641190391Ssam#endif
642178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
643178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
644193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
645193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
646192468Ssam	/* NB: this handles the bpfdetach done below */
647192468Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
648178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
649178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
650178354Ssam	IEEE80211_UNLOCK(ic);
651178354Ssam
652178354Ssam	ifmedia_removeall(&vap->iv_media);
653178354Ssam
654192468Ssam	ieee80211_radiotap_vdetach(vap);
655178354Ssam	ieee80211_regdomain_vdetach(vap);
656178354Ssam	ieee80211_scan_vdetach(vap);
657190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
658190391Ssam	ieee80211_superg_vdetach(vap);
659190391Ssam#endif
660178354Ssam	ieee80211_ht_vdetach(vap);
661178354Ssam	/* NB: must be before ieee80211_node_vdetach */
662178354Ssam	ieee80211_proto_vdetach(vap);
663178354Ssam	ieee80211_crypto_vdetach(vap);
664178354Ssam	ieee80211_power_vdetach(vap);
665178354Ssam	ieee80211_node_vdetach(vap);
666178354Ssam	ieee80211_sysctl_vdetach(vap);
667182674Sweongyo
668182674Sweongyo	if_free(ifp);
669242149Sadrian
670242149Sadrian	CURVNET_RESTORE();
671116742Ssam}
672116742Ssam
673178354Ssam/*
674178354Ssam * Synchronize flag bit state in the parent ifnet structure
675178354Ssam * according to the state of all vap ifnet's.  This is used,
676178354Ssam * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
677178354Ssam */
678178354Ssamvoid
679178354Ssamieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
680178354Ssam{
681178354Ssam	struct ifnet *ifp = ic->ic_ifp;
682178354Ssam	struct ieee80211vap *vap;
683178354Ssam	int bit, oflags;
684178354Ssam
685178354Ssam	IEEE80211_LOCK_ASSERT(ic);
686178354Ssam
687178354Ssam	bit = 0;
688178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
689178354Ssam		if (vap->iv_ifp->if_flags & flag) {
690178354Ssam			/*
691178354Ssam			 * XXX the bridge sets PROMISC but we don't want to
692178354Ssam			 * enable it on the device, discard here so all the
693178354Ssam			 * drivers don't need to special-case it
694178354Ssam			 */
695178354Ssam			if (flag == IFF_PROMISC &&
696195847Ssam			    !(vap->iv_opmode == IEEE80211_M_MONITOR ||
697196004Ssam			      (vap->iv_opmode == IEEE80211_M_AHDEMO &&
698196004Ssam			       (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
699178354Ssam				continue;
700178354Ssam			bit = 1;
701178354Ssam			break;
702178354Ssam		}
703178354Ssam	oflags = ifp->if_flags;
704178354Ssam	if (bit)
705178354Ssam		ifp->if_flags |= flag;
706178354Ssam	else
707178354Ssam		ifp->if_flags &= ~flag;
708178354Ssam	if ((ifp->if_flags ^ oflags) & flag) {
709178354Ssam		/* XXX should we return 1/0 and let caller do this? */
710178354Ssam		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
711178354Ssam			if (flag == IFF_PROMISC)
712191746Sthompsa				ieee80211_runtask(ic, &ic->ic_promisc_task);
713178354Ssam			else if (flag == IFF_ALLMULTI)
714191746Sthompsa				ieee80211_runtask(ic, &ic->ic_mcast_task);
715178354Ssam		}
716178354Ssam	}
717178354Ssam}
718178354Ssam
719178354Ssam/*
720178354Ssam * Synchronize flag 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
725178354Ssamieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
726178354Ssam{
727178354Ssam	struct ieee80211vap *vap;
728178354Ssam	int bit;
729178354Ssam
730178354Ssam	IEEE80211_LOCK_ASSERT(ic);
731178354Ssam
732178354Ssam	bit = 0;
733178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
734178354Ssam		if (vap->iv_flags & flag) {
735178354Ssam			bit = 1;
736178354Ssam			break;
737178354Ssam		}
738178354Ssam	if (bit)
739178354Ssam		ic->ic_flags |= flag;
740178354Ssam	else
741178354Ssam		ic->ic_flags &= ~flag;
742178354Ssam}
743178354Ssam
744178354Ssamvoid
745178354Ssamieee80211_syncflag(struct ieee80211vap *vap, int flag)
746178354Ssam{
747178354Ssam	struct ieee80211com *ic = vap->iv_ic;
748178354Ssam
749178354Ssam	IEEE80211_LOCK(ic);
750178354Ssam	if (flag < 0) {
751178354Ssam		flag = -flag;
752178354Ssam		vap->iv_flags &= ~flag;
753178354Ssam	} else
754178354Ssam		vap->iv_flags |= flag;
755178354Ssam	ieee80211_syncflag_locked(ic, flag);
756178354Ssam	IEEE80211_UNLOCK(ic);
757178354Ssam}
758178354Ssam
759178354Ssam/*
760193655Ssam * Synchronize flags_ht bit state in the com structure
761178354Ssam * according to the state of all vap's.  This is used,
762178354Ssam * for example, to handle state changes via ioctls.
763178354Ssam */
764178354Ssamstatic void
765193655Ssamieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
766193655Ssam{
767193655Ssam	struct ieee80211vap *vap;
768193655Ssam	int bit;
769193655Ssam
770193655Ssam	IEEE80211_LOCK_ASSERT(ic);
771193655Ssam
772193655Ssam	bit = 0;
773193655Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
774193655Ssam		if (vap->iv_flags_ht & flag) {
775193655Ssam			bit = 1;
776193655Ssam			break;
777193655Ssam		}
778193655Ssam	if (bit)
779193655Ssam		ic->ic_flags_ht |= flag;
780193655Ssam	else
781193655Ssam		ic->ic_flags_ht &= ~flag;
782193655Ssam}
783193655Ssam
784193655Ssamvoid
785193655Ssamieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
786193655Ssam{
787193655Ssam	struct ieee80211com *ic = vap->iv_ic;
788193655Ssam
789193655Ssam	IEEE80211_LOCK(ic);
790193655Ssam	if (flag < 0) {
791193655Ssam		flag = -flag;
792193655Ssam		vap->iv_flags_ht &= ~flag;
793193655Ssam	} else
794193655Ssam		vap->iv_flags_ht |= flag;
795193655Ssam	ieee80211_syncflag_ht_locked(ic, flag);
796193655Ssam	IEEE80211_UNLOCK(ic);
797193655Ssam}
798193655Ssam
799193655Ssam/*
800193655Ssam * Synchronize flags_ext bit state in the com structure
801193655Ssam * according to the state of all vap's.  This is used,
802193655Ssam * for example, to handle state changes via ioctls.
803193655Ssam */
804193655Ssamstatic void
805178354Ssamieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
806178354Ssam{
807178354Ssam	struct ieee80211vap *vap;
808178354Ssam	int bit;
809178354Ssam
810178354Ssam	IEEE80211_LOCK_ASSERT(ic);
811178354Ssam
812178354Ssam	bit = 0;
813178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
814178354Ssam		if (vap->iv_flags_ext & flag) {
815178354Ssam			bit = 1;
816178354Ssam			break;
817178354Ssam		}
818178354Ssam	if (bit)
819178354Ssam		ic->ic_flags_ext |= flag;
820178354Ssam	else
821178354Ssam		ic->ic_flags_ext &= ~flag;
822178354Ssam}
823178354Ssam
824178354Ssamvoid
825178354Ssamieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
826178354Ssam{
827178354Ssam	struct ieee80211com *ic = vap->iv_ic;
828178354Ssam
829178354Ssam	IEEE80211_LOCK(ic);
830178354Ssam	if (flag < 0) {
831178354Ssam		flag = -flag;
832178354Ssam		vap->iv_flags_ext &= ~flag;
833178354Ssam	} else
834178354Ssam		vap->iv_flags_ext |= flag;
835178354Ssam	ieee80211_syncflag_ext_locked(ic, flag);
836178354Ssam	IEEE80211_UNLOCK(ic);
837178354Ssam}
838178354Ssam
839166012Ssamstatic __inline int
840166012Ssammapgsm(u_int freq, u_int flags)
841166012Ssam{
842166012Ssam	freq *= 10;
843166012Ssam	if (flags & IEEE80211_CHAN_QUARTER)
844166012Ssam		freq += 5;
845166012Ssam	else if (flags & IEEE80211_CHAN_HALF)
846166012Ssam		freq += 10;
847166012Ssam	else
848166012Ssam		freq += 20;
849166012Ssam	/* NB: there is no 907/20 wide but leave room */
850166012Ssam	return (freq - 906*10) / 5;
851166012Ssam}
852166012Ssam
853166012Ssamstatic __inline int
854166012Ssammappsb(u_int freq, u_int flags)
855166012Ssam{
856166012Ssam	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
857166012Ssam}
858166012Ssam
859116742Ssam/*
860116742Ssam * Convert MHz frequency to IEEE channel number.
861116742Ssam */
862152450Ssamint
863116742Ssamieee80211_mhz2ieee(u_int freq, u_int flags)
864116742Ssam{
865167430Ssam#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
866166012Ssam	if (flags & IEEE80211_CHAN_GSM)
867166012Ssam		return mapgsm(freq, flags);
868116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
869116742Ssam		if (freq == 2484)
870116742Ssam			return 14;
871116742Ssam		if (freq < 2484)
872152450Ssam			return ((int) freq - 2407) / 5;
873116742Ssam		else
874116742Ssam			return 15 + ((freq - 2512) / 20);
875116899Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
876165569Ssam		if (freq <= 5000) {
877170530Ssam			/* XXX check regdomain? */
878167430Ssam			if (IS_FREQ_IN_PSB(freq))
879166012Ssam				return mappsb(freq, flags);
880152450Ssam			return (freq - 4000) / 5;
881165569Ssam		} else
882152450Ssam			return (freq - 5000) / 5;
883116742Ssam	} else {				/* either, guess */
884116742Ssam		if (freq == 2484)
885116742Ssam			return 14;
886166012Ssam		if (freq < 2484) {
887166012Ssam			if (907 <= freq && freq <= 922)
888166012Ssam				return mapgsm(freq, flags);
889152450Ssam			return ((int) freq - 2407) / 5;
890166012Ssam		}
891152450Ssam		if (freq < 5000) {
892167430Ssam			if (IS_FREQ_IN_PSB(freq))
893166012Ssam				return mappsb(freq, flags);
894165569Ssam			else if (freq > 4900)
895152450Ssam				return (freq - 4000) / 5;
896152450Ssam			else
897152450Ssam				return 15 + ((freq - 2512) / 20);
898152450Ssam		}
899116742Ssam		return (freq - 5000) / 5;
900116742Ssam	}
901167430Ssam#undef IS_FREQ_IN_PSB
902116742Ssam}
903116742Ssam
904116742Ssam/*
905116742Ssam * Convert channel to IEEE channel number.
906116742Ssam */
907152450Ssamint
908165825Smjacobieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
909116742Ssam{
910170530Ssam	if (c == NULL) {
911138568Ssam		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
912117039Ssam		return 0;		/* XXX */
913116742Ssam	}
914170530Ssam	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
915116742Ssam}
916116742Ssam
917116742Ssam/*
918116742Ssam * Convert IEEE channel number to MHz frequency.
919116742Ssam */
920116742Ssamu_int
921116742Ssamieee80211_ieee2mhz(u_int chan, u_int flags)
922116742Ssam{
923166012Ssam	if (flags & IEEE80211_CHAN_GSM)
924166012Ssam		return 907 + 5 * (chan / 10);
925116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
926116742Ssam		if (chan == 14)
927116742Ssam			return 2484;
928116742Ssam		if (chan < 14)
929116742Ssam			return 2407 + chan*5;
930116742Ssam		else
931116742Ssam			return 2512 + ((chan-15)*20);
932116742Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
933165569Ssam		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
934165569Ssam			chan -= 37;
935165569Ssam			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
936165569Ssam		}
937116742Ssam		return 5000 + (chan*5);
938116742Ssam	} else {				/* either, guess */
939166012Ssam		/* XXX can't distinguish PSB+GSM channels */
940116742Ssam		if (chan == 14)
941116742Ssam			return 2484;
942116742Ssam		if (chan < 14)			/* 0-13 */
943116742Ssam			return 2407 + chan*5;
944116742Ssam		if (chan < 27)			/* 15-26 */
945116742Ssam			return 2512 + ((chan-15)*20);
946116742Ssam		return 5000 + (chan*5);
947116742Ssam	}
948116742Ssam}
949116742Ssam
950116742Ssam/*
951170530Ssam * Locate a channel given a frequency+flags.  We cache
952178354Ssam * the previous lookup to optimize switching between two
953170530Ssam * channels--as happens with dynamic turbo.
954170530Ssam */
955170530Ssamstruct ieee80211_channel *
956170530Ssamieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
957170530Ssam{
958170530Ssam	struct ieee80211_channel *c;
959170530Ssam	int i;
960170530Ssam
961170530Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
962170530Ssam	c = ic->ic_prevchan;
963170530Ssam	if (c != NULL && c->ic_freq == freq &&
964170530Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
965170530Ssam		return c;
966170530Ssam	/* brute force search */
967170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
968170530Ssam		c = &ic->ic_channels[i];
969170530Ssam		if (c->ic_freq == freq &&
970170530Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
971170530Ssam			return c;
972170530Ssam	}
973170530Ssam	return NULL;
974170530Ssam}
975170530Ssam
976173861Ssam/*
977173861Ssam * Locate a channel given a channel number+flags.  We cache
978173861Ssam * the previous lookup to optimize switching between two
979173861Ssam * channels--as happens with dynamic turbo.
980173861Ssam */
981173861Ssamstruct ieee80211_channel *
982173861Ssamieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
983173861Ssam{
984173861Ssam	struct ieee80211_channel *c;
985173861Ssam	int i;
986173861Ssam
987173861Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
988173861Ssam	c = ic->ic_prevchan;
989173861Ssam	if (c != NULL && c->ic_ieee == ieee &&
990173861Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
991173861Ssam		return c;
992173861Ssam	/* brute force search */
993173861Ssam	for (i = 0; i < ic->ic_nchans; i++) {
994173861Ssam		c = &ic->ic_channels[i];
995173861Ssam		if (c->ic_ieee == ieee &&
996173861Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
997173861Ssam			return c;
998173861Ssam	}
999173861Ssam	return NULL;
1000173861Ssam}
1001173861Ssam
1002170530Ssamstatic void
1003178354Ssamaddmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1004170530Ssam{
1005170530Ssam#define	ADD(_ic, _s, _o) \
1006178354Ssam	ifmedia_add(media, \
1007170530Ssam		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1008170530Ssam	static const u_int mopts[IEEE80211_MODE_MAX] = {
1009188106Ssam	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1010188106Ssam	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1011188106Ssam	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1012188106Ssam	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1013188106Ssam	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1014188106Ssam	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1015188106Ssam	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1016188106Ssam	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1017188782Ssam	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1018188782Ssam	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1019188106Ssam	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1020188106Ssam	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1021170530Ssam	};
1022170530Ssam	u_int mopt;
1023170530Ssam
1024170530Ssam	mopt = mopts[mode];
1025178354Ssam	if (addsta)
1026178354Ssam		ADD(ic, mword, mopt);	/* STA mode has no cap */
1027178354Ssam	if (caps & IEEE80211_C_IBSS)
1028178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1029178354Ssam	if (caps & IEEE80211_C_HOSTAP)
1030178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1031178354Ssam	if (caps & IEEE80211_C_AHDEMO)
1032178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1033178354Ssam	if (caps & IEEE80211_C_MONITOR)
1034178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1035178354Ssam	if (caps & IEEE80211_C_WDS)
1036178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1037195618Srpaulo	if (caps & IEEE80211_C_MBSS)
1038195618Srpaulo		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1039170530Ssam#undef ADD
1040170530Ssam}
1041170530Ssam
1042170530Ssam/*
1043116742Ssam * Setup the media data structures according to the channel and
1044178354Ssam * rate tables.
1045116742Ssam */
1046178354Ssamstatic int
1047178354Ssamieee80211_media_setup(struct ieee80211com *ic,
1048178354Ssam	struct ifmedia *media, int caps, int addsta,
1049116742Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1050116742Ssam{
1051228621Sbschmidt	int i, j, rate, maxrate, mword, r;
1052228621Sbschmidt	enum ieee80211_phymode mode;
1053170530Ssam	const struct ieee80211_rateset *rs;
1054116742Ssam	struct ieee80211_rateset allrates;
1055116742Ssam
1056118887Ssam	/*
1057116742Ssam	 * Fill in media characteristics.
1058116742Ssam	 */
1059178354Ssam	ifmedia_init(media, 0, media_change, media_stat);
1060116742Ssam	maxrate = 0;
1061170530Ssam	/*
1062170530Ssam	 * Add media for legacy operating modes.
1063170530Ssam	 */
1064116742Ssam	memset(&allrates, 0, sizeof(allrates));
1065170530Ssam	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1066167468Ssam		if (isclr(ic->ic_modecaps, mode))
1067116742Ssam			continue;
1068178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1069116742Ssam		if (mode == IEEE80211_MODE_AUTO)
1070116742Ssam			continue;
1071116742Ssam		rs = &ic->ic_sup_rates[mode];
1072116742Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1073116742Ssam			rate = rs->rs_rates[i];
1074116742Ssam			mword = ieee80211_rate2media(ic, rate, mode);
1075116742Ssam			if (mword == 0)
1076116742Ssam				continue;
1077178354Ssam			addmedia(media, caps, addsta, mode, mword);
1078116742Ssam			/*
1079170530Ssam			 * Add legacy rate to the collection of all rates.
1080116742Ssam			 */
1081116742Ssam			r = rate & IEEE80211_RATE_VAL;
1082116742Ssam			for (j = 0; j < allrates.rs_nrates; j++)
1083116742Ssam				if (allrates.rs_rates[j] == r)
1084116742Ssam					break;
1085116742Ssam			if (j == allrates.rs_nrates) {
1086116742Ssam				/* unique, add to the set */
1087116742Ssam				allrates.rs_rates[j] = r;
1088116742Ssam				allrates.rs_nrates++;
1089116742Ssam			}
1090116742Ssam			rate = (rate & IEEE80211_RATE_VAL) / 2;
1091116742Ssam			if (rate > maxrate)
1092116742Ssam				maxrate = rate;
1093116742Ssam		}
1094116742Ssam	}
1095116742Ssam	for (i = 0; i < allrates.rs_nrates; i++) {
1096116742Ssam		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1097116742Ssam				IEEE80211_MODE_AUTO);
1098116742Ssam		if (mword == 0)
1099116742Ssam			continue;
1100170530Ssam		/* NB: remove media options from mword */
1101178354Ssam		addmedia(media, caps, addsta,
1102178354Ssam		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1103116742Ssam	}
1104170530Ssam	/*
1105170530Ssam	 * Add HT/11n media.  Note that we do not have enough
1106170530Ssam	 * bits in the media subtype to express the MCS so we
1107170530Ssam	 * use a "placeholder" media subtype and any fixed MCS
1108170530Ssam	 * must be specified with a different mechanism.
1109170530Ssam	 */
1110188782Ssam	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1111170530Ssam		if (isclr(ic->ic_modecaps, mode))
1112170530Ssam			continue;
1113178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1114178354Ssam		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1115170530Ssam	}
1116170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1117170530Ssam	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1118178354Ssam		addmedia(media, caps, addsta,
1119178354Ssam		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1120219599Sbschmidt		i = ic->ic_txstream * 8 - 1;
1121219599Sbschmidt		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1122219599Sbschmidt		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1123219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_400ns;
1124219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1125219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_800ns;
1126219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1127219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_400ns;
1128219599Sbschmidt		else
1129219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_800ns;
1130219599Sbschmidt		if (rate > maxrate)
1131219599Sbschmidt			maxrate = rate;
1132170530Ssam	}
1133178354Ssam	return maxrate;
1134178354Ssam}
1135116742Ssam
1136178354Ssamvoid
1137178354Ssamieee80211_media_init(struct ieee80211com *ic)
1138178354Ssam{
1139178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1140178354Ssam	int maxrate;
1141178354Ssam
1142178354Ssam	/* NB: this works because the structure is initialized to zero */
1143178354Ssam	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1144178354Ssam		/*
1145178354Ssam		 * We are re-initializing the channel list; clear
1146178354Ssam		 * the existing media state as the media routines
1147178354Ssam		 * don't suppress duplicates.
1148178354Ssam		 */
1149178354Ssam		ifmedia_removeall(&ic->ic_media);
1150178354Ssam	}
1151178354Ssam	ieee80211_chan_init(ic);
1152178354Ssam
1153178354Ssam	/*
1154178354Ssam	 * Recalculate media settings in case new channel list changes
1155178354Ssam	 * the set of available modes.
1156178354Ssam	 */
1157178354Ssam	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1158178354Ssam		ieee80211com_media_change, ieee80211com_media_status);
1159170530Ssam	/* NB: strip explicit mode; we're actually in autoselect */
1160170530Ssam	ifmedia_set(&ic->ic_media,
1161188106Ssam	    media_status(ic->ic_opmode, ic->ic_curchan) &~
1162188106Ssam		(IFM_MMASK | IFM_IEEE80211_TURBO));
1163116742Ssam	if (maxrate)
1164116742Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
1165178354Ssam
1166178354Ssam	/* XXX need to propagate new media settings to vap's */
1167116742Ssam}
1168116742Ssam
1169188782Ssam/* XXX inline or eliminate? */
1170165569Ssamconst struct ieee80211_rateset *
1171165569Ssamieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1172165569Ssam{
1173188774Ssam	/* XXX does this work for 11ng basic rates? */
1174170530Ssam	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1175165569Ssam}
1176165569Ssam
1177138568Ssamvoid
1178138568Ssamieee80211_announce(struct ieee80211com *ic)
1179138568Ssam{
1180138568Ssam	struct ifnet *ifp = ic->ic_ifp;
1181228621Sbschmidt	int i, rate, mword;
1182228621Sbschmidt	enum ieee80211_phymode mode;
1183170530Ssam	const struct ieee80211_rateset *rs;
1184138568Ssam
1185172227Ssam	/* NB: skip AUTO since it has no rates */
1186172227Ssam	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1187167468Ssam		if (isclr(ic->ic_modecaps, mode))
1188138568Ssam			continue;
1189138568Ssam		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1190138568Ssam		rs = &ic->ic_sup_rates[mode];
1191138568Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1192170530Ssam			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1193138568Ssam			if (mword == 0)
1194138568Ssam				continue;
1195170530Ssam			rate = ieee80211_media2rate(mword);
1196138568Ssam			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1197170530Ssam			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1198138568Ssam		}
1199138568Ssam		printf("\n");
1200138568Ssam	}
1201170530Ssam	ieee80211_ht_announce(ic);
1202138568Ssam}
1203138568Ssam
1204170530Ssamvoid
1205170530Ssamieee80211_announce_channels(struct ieee80211com *ic)
1206116742Ssam{
1207170530Ssam	const struct ieee80211_channel *c;
1208170530Ssam	char type;
1209170530Ssam	int i, cw;
1210170530Ssam
1211170530Ssam	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1212170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1213170530Ssam		c = &ic->ic_channels[i];
1214170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
1215170530Ssam			type = 'S';
1216170530Ssam		else if (IEEE80211_IS_CHAN_108A(c))
1217170530Ssam			type = 'T';
1218170530Ssam		else if (IEEE80211_IS_CHAN_108G(c))
1219170530Ssam			type = 'G';
1220170530Ssam		else if (IEEE80211_IS_CHAN_HT(c))
1221170530Ssam			type = 'n';
1222170530Ssam		else if (IEEE80211_IS_CHAN_A(c))
1223170530Ssam			type = 'a';
1224170530Ssam		else if (IEEE80211_IS_CHAN_ANYG(c))
1225170530Ssam			type = 'g';
1226170530Ssam		else if (IEEE80211_IS_CHAN_B(c))
1227170530Ssam			type = 'b';
1228170530Ssam		else
1229170530Ssam			type = 'f';
1230170530Ssam		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1231170530Ssam			cw = 40;
1232170530Ssam		else if (IEEE80211_IS_CHAN_HALF(c))
1233170530Ssam			cw = 10;
1234170530Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
1235170530Ssam			cw = 5;
1236170530Ssam		else
1237170530Ssam			cw = 20;
1238170530Ssam		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1239170530Ssam			, c->ic_ieee, c->ic_freq, type
1240170530Ssam			, cw
1241170530Ssam			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1242170530Ssam			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1243170530Ssam			, c->ic_maxregpower
1244170530Ssam			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1245170530Ssam			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1246170530Ssam		);
1247170530Ssam	}
1248116742Ssam}
1249116742Ssam
1250170530Ssamstatic int
1251184273Ssammedia2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1252170530Ssam{
1253116742Ssam	switch (IFM_MODE(ime->ifm_media)) {
1254116742Ssam	case IFM_IEEE80211_11A:
1255178354Ssam		*mode = IEEE80211_MODE_11A;
1256116742Ssam		break;
1257116742Ssam	case IFM_IEEE80211_11B:
1258178354Ssam		*mode = IEEE80211_MODE_11B;
1259116742Ssam		break;
1260116742Ssam	case IFM_IEEE80211_11G:
1261178354Ssam		*mode = IEEE80211_MODE_11G;
1262116742Ssam		break;
1263124543Sonoe	case IFM_IEEE80211_FH:
1264178354Ssam		*mode = IEEE80211_MODE_FH;
1265124543Sonoe		break;
1266170530Ssam	case IFM_IEEE80211_11NA:
1267178354Ssam		*mode = IEEE80211_MODE_11NA;
1268170530Ssam		break;
1269170530Ssam	case IFM_IEEE80211_11NG:
1270178354Ssam		*mode = IEEE80211_MODE_11NG;
1271170530Ssam		break;
1272116742Ssam	case IFM_AUTO:
1273178354Ssam		*mode = IEEE80211_MODE_AUTO;
1274116742Ssam		break;
1275116742Ssam	default:
1276178354Ssam		return 0;
1277116742Ssam	}
1278116742Ssam	/*
1279138568Ssam	 * Turbo mode is an ``option''.
1280138568Ssam	 * XXX does not apply to AUTO
1281116742Ssam	 */
1282116742Ssam	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1283178354Ssam		if (*mode == IEEE80211_MODE_11A) {
1284184273Ssam			if (flags & IEEE80211_F_TURBOP)
1285178354Ssam				*mode = IEEE80211_MODE_TURBO_A;
1286170530Ssam			else
1287178354Ssam				*mode = IEEE80211_MODE_STURBO_A;
1288178354Ssam		} else if (*mode == IEEE80211_MODE_11G)
1289178354Ssam			*mode = IEEE80211_MODE_TURBO_G;
1290138568Ssam		else
1291178354Ssam			return 0;
1292116742Ssam	}
1293170530Ssam	/* XXX HT40 +/- */
1294178354Ssam	return 1;
1295178354Ssam}
1296116742Ssam
1297178354Ssam/*
1298184273Ssam * Handle a media change request on the underlying interface.
1299178354Ssam */
1300178354Ssamint
1301178354Ssamieee80211com_media_change(struct ifnet *ifp)
1302178354Ssam{
1303184273Ssam	return EINVAL;
1304178354Ssam}
1305116742Ssam
1306178354Ssam/*
1307178354Ssam * Handle a media change request on the vap interface.
1308178354Ssam */
1309178354Ssamint
1310178354Ssamieee80211_media_change(struct ifnet *ifp)
1311178354Ssam{
1312178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1313178354Ssam	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1314184273Ssam	uint16_t newmode;
1315178354Ssam
1316184273Ssam	if (!media2mode(ime, vap->iv_flags, &newmode))
1317178354Ssam		return EINVAL;
1318184273Ssam	if (vap->iv_des_mode != newmode) {
1319184273Ssam		vap->iv_des_mode = newmode;
1320193340Ssam		/* XXX kick state machine if up+running */
1321178354Ssam	}
1322178354Ssam	return 0;
1323116742Ssam}
1324116742Ssam
1325170530Ssam/*
1326170530Ssam * Common code to calculate the media status word
1327170530Ssam * from the operating mode and channel state.
1328170530Ssam */
1329170530Ssamstatic int
1330170530Ssammedia_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1331170530Ssam{
1332170530Ssam	int status;
1333170530Ssam
1334170530Ssam	status = IFM_IEEE80211;
1335170530Ssam	switch (opmode) {
1336170530Ssam	case IEEE80211_M_STA:
1337170530Ssam		break;
1338170530Ssam	case IEEE80211_M_IBSS:
1339170530Ssam		status |= IFM_IEEE80211_ADHOC;
1340170530Ssam		break;
1341170530Ssam	case IEEE80211_M_HOSTAP:
1342170530Ssam		status |= IFM_IEEE80211_HOSTAP;
1343170530Ssam		break;
1344170530Ssam	case IEEE80211_M_MONITOR:
1345170530Ssam		status |= IFM_IEEE80211_MONITOR;
1346170530Ssam		break;
1347170530Ssam	case IEEE80211_M_AHDEMO:
1348170530Ssam		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1349170530Ssam		break;
1350170530Ssam	case IEEE80211_M_WDS:
1351178354Ssam		status |= IFM_IEEE80211_WDS;
1352170530Ssam		break;
1353195618Srpaulo	case IEEE80211_M_MBSS:
1354195618Srpaulo		status |= IFM_IEEE80211_MBSS;
1355195618Srpaulo		break;
1356170530Ssam	}
1357170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan)) {
1358170530Ssam		status |= IFM_IEEE80211_11NA;
1359170530Ssam	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1360170530Ssam		status |= IFM_IEEE80211_11NG;
1361170530Ssam	} else if (IEEE80211_IS_CHAN_A(chan)) {
1362170530Ssam		status |= IFM_IEEE80211_11A;
1363170530Ssam	} else if (IEEE80211_IS_CHAN_B(chan)) {
1364170530Ssam		status |= IFM_IEEE80211_11B;
1365170530Ssam	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1366170530Ssam		status |= IFM_IEEE80211_11G;
1367170530Ssam	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1368170530Ssam		status |= IFM_IEEE80211_FH;
1369170530Ssam	}
1370170530Ssam	/* XXX else complain? */
1371170530Ssam
1372170530Ssam	if (IEEE80211_IS_CHAN_TURBO(chan))
1373170530Ssam		status |= IFM_IEEE80211_TURBO;
1374178354Ssam#if 0
1375178354Ssam	if (IEEE80211_IS_CHAN_HT20(chan))
1376178354Ssam		status |= IFM_IEEE80211_HT20;
1377178354Ssam	if (IEEE80211_IS_CHAN_HT40(chan))
1378178354Ssam		status |= IFM_IEEE80211_HT40;
1379178354Ssam#endif
1380170530Ssam	return status;
1381170530Ssam}
1382170530Ssam
1383178354Ssamstatic void
1384178354Ssamieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1385178354Ssam{
1386178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1387178354Ssam	struct ieee80211vap *vap;
1388178354Ssam
1389178354Ssam	imr->ifm_status = IFM_AVALID;
1390178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1391178354Ssam		if (vap->iv_ifp->if_flags & IFF_UP) {
1392178354Ssam			imr->ifm_status |= IFM_ACTIVE;
1393178354Ssam			break;
1394178354Ssam		}
1395178354Ssam	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1396178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1397178354Ssam		imr->ifm_current = imr->ifm_active;
1398178354Ssam}
1399178354Ssam
1400116742Ssamvoid
1401116742Ssamieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1402116742Ssam{
1403178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1404178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1405170530Ssam	enum ieee80211_phymode mode;
1406116742Ssam
1407116742Ssam	imr->ifm_status = IFM_AVALID;
1408170530Ssam	/*
1409170530Ssam	 * NB: use the current channel's mode to lock down a xmit
1410170530Ssam	 * rate only when running; otherwise we may have a mismatch
1411170530Ssam	 * in which case the rate will not be convertible.
1412170530Ssam	 */
1413178354Ssam	if (vap->iv_state == IEEE80211_S_RUN) {
1414116742Ssam		imr->ifm_status |= IFM_ACTIVE;
1415170530Ssam		mode = ieee80211_chan2mode(ic->ic_curchan);
1416170530Ssam	} else
1417170530Ssam		mode = IEEE80211_MODE_AUTO;
1418178354Ssam	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1419138568Ssam	/*
1420138568Ssam	 * Calculate a current rate if possible.
1421138568Ssam	 */
1422178354Ssam	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1423138568Ssam		/*
1424138568Ssam		 * A fixed rate is set, report that.
1425138568Ssam		 */
1426138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1427178354Ssam			vap->iv_txparms[mode].ucastrate, mode);
1428178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1429138568Ssam		/*
1430138568Ssam		 * In station mode report the current transmit rate.
1431138568Ssam		 */
1432138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1433178354Ssam			vap->iv_bss->ni_txrate, mode);
1434128966Sandre	} else
1435138568Ssam		imr->ifm_active |= IFM_AUTO;
1436178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1437178354Ssam		imr->ifm_current = imr->ifm_active;
1438116742Ssam}
1439116742Ssam
1440116742Ssam/*
1441116742Ssam * Set the current phy mode and recalculate the active channel
1442116742Ssam * set based on the available channels for this mode.  Also
1443116742Ssam * select a new default/current channel if the current one is
1444116742Ssam * inappropriate for this mode.
1445116742Ssam */
1446116742Ssamint
1447116742Ssamieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1448116742Ssam{
1449116742Ssam	/*
1450166012Ssam	 * Adjust basic rates in 11b/11g supported rate set.
1451166012Ssam	 * Note that if operating on a hal/quarter rate channel
1452166012Ssam	 * this is a noop as those rates sets are different
1453166012Ssam	 * and used instead.
1454116742Ssam	 */
1455166012Ssam	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1456178354Ssam		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1457166012Ssam
1458116742Ssam	ic->ic_curmode = mode;
1459138568Ssam	ieee80211_reset_erp(ic);	/* reset ERP state */
1460138568Ssam
1461116742Ssam	return 0;
1462116742Ssam}
1463116742Ssam
1464116742Ssam/*
1465170530Ssam * Return the phy mode for with the specified channel.
1466116742Ssam */
1467116742Ssamenum ieee80211_phymode
1468170530Ssamieee80211_chan2mode(const struct ieee80211_channel *chan)
1469116742Ssam{
1470170530Ssam
1471170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan))
1472170530Ssam		return IEEE80211_MODE_11NA;
1473170530Ssam	else if (IEEE80211_IS_CHAN_HTG(chan))
1474170530Ssam		return IEEE80211_MODE_11NG;
1475170530Ssam	else if (IEEE80211_IS_CHAN_108G(chan))
1476170530Ssam		return IEEE80211_MODE_TURBO_G;
1477170530Ssam	else if (IEEE80211_IS_CHAN_ST(chan))
1478170530Ssam		return IEEE80211_MODE_STURBO_A;
1479170530Ssam	else if (IEEE80211_IS_CHAN_TURBO(chan))
1480153350Ssam		return IEEE80211_MODE_TURBO_A;
1481188782Ssam	else if (IEEE80211_IS_CHAN_HALF(chan))
1482188782Ssam		return IEEE80211_MODE_HALF;
1483188782Ssam	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1484188782Ssam		return IEEE80211_MODE_QUARTER;
1485170530Ssam	else if (IEEE80211_IS_CHAN_A(chan))
1486116742Ssam		return IEEE80211_MODE_11A;
1487170530Ssam	else if (IEEE80211_IS_CHAN_ANYG(chan))
1488116742Ssam		return IEEE80211_MODE_11G;
1489170530Ssam	else if (IEEE80211_IS_CHAN_B(chan))
1490116742Ssam		return IEEE80211_MODE_11B;
1491170530Ssam	else if (IEEE80211_IS_CHAN_FHSS(chan))
1492170530Ssam		return IEEE80211_MODE_FH;
1493170530Ssam
1494170530Ssam	/* NB: should not get here */
1495170530Ssam	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1496170530Ssam		__func__, chan->ic_freq, chan->ic_flags);
1497170530Ssam	return IEEE80211_MODE_11B;
1498116742Ssam}
1499116742Ssam
1500170530Ssamstruct ratemedia {
1501170530Ssam	u_int	match;	/* rate + mode */
1502170530Ssam	u_int	media;	/* if_media rate */
1503170530Ssam};
1504170530Ssam
1505170530Ssamstatic int
1506170530Ssamfindmedia(const struct ratemedia rates[], int n, u_int match)
1507170530Ssam{
1508170530Ssam	int i;
1509170530Ssam
1510170530Ssam	for (i = 0; i < n; i++)
1511170530Ssam		if (rates[i].match == match)
1512170530Ssam			return rates[i].media;
1513170530Ssam	return IFM_AUTO;
1514170530Ssam}
1515170530Ssam
1516116742Ssam/*
1517170530Ssam * Convert IEEE80211 rate value to ifmedia subtype.
1518170530Ssam * Rate is either a legacy rate in units of 0.5Mbps
1519170530Ssam * or an MCS index.
1520116742Ssam */
1521116742Ssamint
1522116742Ssamieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1523116742Ssam{
1524116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1525170530Ssam	static const struct ratemedia rates[] = {
1526124543Sonoe		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1527124543Sonoe		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1528124543Sonoe		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1529124543Sonoe		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1530124543Sonoe		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1531124543Sonoe		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1532124543Sonoe		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1533124543Sonoe		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1534124543Sonoe		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1535124543Sonoe		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1536124543Sonoe		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1537124543Sonoe		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1538124543Sonoe		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1539124543Sonoe		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1540124543Sonoe		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1541124543Sonoe		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1542124543Sonoe		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1543124543Sonoe		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1544124543Sonoe		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1545124543Sonoe		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1546124543Sonoe		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1547124543Sonoe		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1548124543Sonoe		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1549124543Sonoe		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1550124543Sonoe		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1551124543Sonoe		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1552124543Sonoe		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1553165569Ssam		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1554165569Ssam		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1555165569Ssam		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1556116742Ssam		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1557116742Ssam	};
1558170530Ssam	static const struct ratemedia htrates[] = {
1559170530Ssam		{   0, IFM_IEEE80211_MCS },
1560170530Ssam		{   1, IFM_IEEE80211_MCS },
1561170530Ssam		{   2, IFM_IEEE80211_MCS },
1562170530Ssam		{   3, IFM_IEEE80211_MCS },
1563170530Ssam		{   4, IFM_IEEE80211_MCS },
1564170530Ssam		{   5, IFM_IEEE80211_MCS },
1565170530Ssam		{   6, IFM_IEEE80211_MCS },
1566170530Ssam		{   7, IFM_IEEE80211_MCS },
1567170530Ssam		{   8, IFM_IEEE80211_MCS },
1568170530Ssam		{   9, IFM_IEEE80211_MCS },
1569170530Ssam		{  10, IFM_IEEE80211_MCS },
1570170530Ssam		{  11, IFM_IEEE80211_MCS },
1571170530Ssam		{  12, IFM_IEEE80211_MCS },
1572170530Ssam		{  13, IFM_IEEE80211_MCS },
1573170530Ssam		{  14, IFM_IEEE80211_MCS },
1574170530Ssam		{  15, IFM_IEEE80211_MCS },
1575219456Sbschmidt		{  16, IFM_IEEE80211_MCS },
1576219456Sbschmidt		{  17, IFM_IEEE80211_MCS },
1577219456Sbschmidt		{  18, IFM_IEEE80211_MCS },
1578219456Sbschmidt		{  19, IFM_IEEE80211_MCS },
1579219456Sbschmidt		{  20, IFM_IEEE80211_MCS },
1580219456Sbschmidt		{  21, IFM_IEEE80211_MCS },
1581219456Sbschmidt		{  22, IFM_IEEE80211_MCS },
1582219456Sbschmidt		{  23, IFM_IEEE80211_MCS },
1583219456Sbschmidt		{  24, IFM_IEEE80211_MCS },
1584219456Sbschmidt		{  25, IFM_IEEE80211_MCS },
1585219456Sbschmidt		{  26, IFM_IEEE80211_MCS },
1586219456Sbschmidt		{  27, IFM_IEEE80211_MCS },
1587219456Sbschmidt		{  28, IFM_IEEE80211_MCS },
1588219456Sbschmidt		{  29, IFM_IEEE80211_MCS },
1589219456Sbschmidt		{  30, IFM_IEEE80211_MCS },
1590219456Sbschmidt		{  31, IFM_IEEE80211_MCS },
1591219456Sbschmidt		{  32, IFM_IEEE80211_MCS },
1592219456Sbschmidt		{  33, IFM_IEEE80211_MCS },
1593219456Sbschmidt		{  34, IFM_IEEE80211_MCS },
1594219456Sbschmidt		{  35, IFM_IEEE80211_MCS },
1595219456Sbschmidt		{  36, IFM_IEEE80211_MCS },
1596219456Sbschmidt		{  37, IFM_IEEE80211_MCS },
1597219456Sbschmidt		{  38, IFM_IEEE80211_MCS },
1598219456Sbschmidt		{  39, IFM_IEEE80211_MCS },
1599219456Sbschmidt		{  40, IFM_IEEE80211_MCS },
1600219456Sbschmidt		{  41, IFM_IEEE80211_MCS },
1601219456Sbschmidt		{  42, IFM_IEEE80211_MCS },
1602219456Sbschmidt		{  43, IFM_IEEE80211_MCS },
1603219456Sbschmidt		{  44, IFM_IEEE80211_MCS },
1604219456Sbschmidt		{  45, IFM_IEEE80211_MCS },
1605219456Sbschmidt		{  46, IFM_IEEE80211_MCS },
1606219456Sbschmidt		{  47, IFM_IEEE80211_MCS },
1607219456Sbschmidt		{  48, IFM_IEEE80211_MCS },
1608219456Sbschmidt		{  49, IFM_IEEE80211_MCS },
1609219456Sbschmidt		{  50, IFM_IEEE80211_MCS },
1610219456Sbschmidt		{  51, IFM_IEEE80211_MCS },
1611219456Sbschmidt		{  52, IFM_IEEE80211_MCS },
1612219456Sbschmidt		{  53, IFM_IEEE80211_MCS },
1613219456Sbschmidt		{  54, IFM_IEEE80211_MCS },
1614219456Sbschmidt		{  55, IFM_IEEE80211_MCS },
1615219456Sbschmidt		{  56, IFM_IEEE80211_MCS },
1616219456Sbschmidt		{  57, IFM_IEEE80211_MCS },
1617219456Sbschmidt		{  58, IFM_IEEE80211_MCS },
1618219456Sbschmidt		{  59, IFM_IEEE80211_MCS },
1619219456Sbschmidt		{  60, IFM_IEEE80211_MCS },
1620219456Sbschmidt		{  61, IFM_IEEE80211_MCS },
1621219456Sbschmidt		{  62, IFM_IEEE80211_MCS },
1622219456Sbschmidt		{  63, IFM_IEEE80211_MCS },
1623219456Sbschmidt		{  64, IFM_IEEE80211_MCS },
1624219456Sbschmidt		{  65, IFM_IEEE80211_MCS },
1625219456Sbschmidt		{  66, IFM_IEEE80211_MCS },
1626219456Sbschmidt		{  67, IFM_IEEE80211_MCS },
1627219456Sbschmidt		{  68, IFM_IEEE80211_MCS },
1628219456Sbschmidt		{  69, IFM_IEEE80211_MCS },
1629219456Sbschmidt		{  70, IFM_IEEE80211_MCS },
1630219456Sbschmidt		{  71, IFM_IEEE80211_MCS },
1631219456Sbschmidt		{  72, IFM_IEEE80211_MCS },
1632219456Sbschmidt		{  73, IFM_IEEE80211_MCS },
1633219456Sbschmidt		{  74, IFM_IEEE80211_MCS },
1634219456Sbschmidt		{  75, IFM_IEEE80211_MCS },
1635219456Sbschmidt		{  76, IFM_IEEE80211_MCS },
1636170530Ssam	};
1637170530Ssam	int m;
1638116742Ssam
1639170530Ssam	/*
1640170530Ssam	 * Check 11n rates first for match as an MCS.
1641170530Ssam	 */
1642170530Ssam	if (mode == IEEE80211_MODE_11NA) {
1643172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1644172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1645170530Ssam			m = findmedia(htrates, N(htrates), rate);
1646170530Ssam			if (m != IFM_AUTO)
1647170530Ssam				return m | IFM_IEEE80211_11NA;
1648170530Ssam		}
1649170530Ssam	} else if (mode == IEEE80211_MODE_11NG) {
1650170530Ssam		/* NB: 12 is ambiguous, it will be treated as an MCS */
1651172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1652172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1653170530Ssam			m = findmedia(htrates, N(htrates), rate);
1654170530Ssam			if (m != IFM_AUTO)
1655170530Ssam				return m | IFM_IEEE80211_11NG;
1656170530Ssam		}
1657170530Ssam	}
1658170530Ssam	rate &= IEEE80211_RATE_VAL;
1659116742Ssam	switch (mode) {
1660116742Ssam	case IEEE80211_MODE_11A:
1661188782Ssam	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
1662188782Ssam	case IEEE80211_MODE_QUARTER:
1663170530Ssam	case IEEE80211_MODE_11NA:
1664138568Ssam	case IEEE80211_MODE_TURBO_A:
1665170530Ssam	case IEEE80211_MODE_STURBO_A:
1666170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1667116742Ssam	case IEEE80211_MODE_11B:
1668170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1669124543Sonoe	case IEEE80211_MODE_FH:
1670170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1671116742Ssam	case IEEE80211_MODE_AUTO:
1672116742Ssam		/* NB: ic may be NULL for some drivers */
1673188775Ssam		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1674170530Ssam			return findmedia(rates, N(rates),
1675170530Ssam			    rate | IFM_IEEE80211_FH);
1676116742Ssam		/* NB: hack, 11g matches both 11b+11a rates */
1677116742Ssam		/* fall thru... */
1678116742Ssam	case IEEE80211_MODE_11G:
1679170530Ssam	case IEEE80211_MODE_11NG:
1680138568Ssam	case IEEE80211_MODE_TURBO_G:
1681170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1682116742Ssam	}
1683116742Ssam	return IFM_AUTO;
1684116742Ssam#undef N
1685116742Ssam}
1686116742Ssam
1687116742Ssamint
1688116742Ssamieee80211_media2rate(int mword)
1689116742Ssam{
1690116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1691116742Ssam	static const int ieeerates[] = {
1692116742Ssam		-1,		/* IFM_AUTO */
1693116742Ssam		0,		/* IFM_MANUAL */
1694116742Ssam		0,		/* IFM_NONE */
1695116742Ssam		2,		/* IFM_IEEE80211_FH1 */
1696116742Ssam		4,		/* IFM_IEEE80211_FH2 */
1697116742Ssam		2,		/* IFM_IEEE80211_DS1 */
1698116742Ssam		4,		/* IFM_IEEE80211_DS2 */
1699116742Ssam		11,		/* IFM_IEEE80211_DS5 */
1700116742Ssam		22,		/* IFM_IEEE80211_DS11 */
1701116742Ssam		44,		/* IFM_IEEE80211_DS22 */
1702116742Ssam		12,		/* IFM_IEEE80211_OFDM6 */
1703116742Ssam		18,		/* IFM_IEEE80211_OFDM9 */
1704116742Ssam		24,		/* IFM_IEEE80211_OFDM12 */
1705116742Ssam		36,		/* IFM_IEEE80211_OFDM18 */
1706116742Ssam		48,		/* IFM_IEEE80211_OFDM24 */
1707116742Ssam		72,		/* IFM_IEEE80211_OFDM36 */
1708116742Ssam		96,		/* IFM_IEEE80211_OFDM48 */
1709116742Ssam		108,		/* IFM_IEEE80211_OFDM54 */
1710116742Ssam		144,		/* IFM_IEEE80211_OFDM72 */
1711165569Ssam		0,		/* IFM_IEEE80211_DS354k */
1712165569Ssam		0,		/* IFM_IEEE80211_DS512k */
1713165569Ssam		6,		/* IFM_IEEE80211_OFDM3 */
1714165569Ssam		9,		/* IFM_IEEE80211_OFDM4 */
1715165569Ssam		54,		/* IFM_IEEE80211_OFDM27 */
1716170530Ssam		-1,		/* IFM_IEEE80211_MCS */
1717116742Ssam	};
1718116742Ssam	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1719116742Ssam		ieeerates[IFM_SUBTYPE(mword)] : 0;
1720116742Ssam#undef N
1721116742Ssam}
1722195379Ssam
1723195379Ssam/*
1724195379Ssam * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1725195379Ssam * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1726195379Ssam */
1727195379Ssam#define	mix(a, b, c)							\
1728195379Ssamdo {									\
1729195379Ssam	a -= b; a -= c; a ^= (c >> 13);					\
1730195379Ssam	b -= c; b -= a; b ^= (a << 8);					\
1731195379Ssam	c -= a; c -= b; c ^= (b >> 13);					\
1732195379Ssam	a -= b; a -= c; a ^= (c >> 12);					\
1733195379Ssam	b -= c; b -= a; b ^= (a << 16);					\
1734195379Ssam	c -= a; c -= b; c ^= (b >> 5);					\
1735195379Ssam	a -= b; a -= c; a ^= (c >> 3);					\
1736195379Ssam	b -= c; b -= a; b ^= (a << 10);					\
1737195379Ssam	c -= a; c -= b; c ^= (b >> 15);					\
1738195379Ssam} while (/*CONSTCOND*/0)
1739195379Ssam
1740195379Ssamuint32_t
1741195379Ssamieee80211_mac_hash(const struct ieee80211com *ic,
1742195379Ssam	const uint8_t addr[IEEE80211_ADDR_LEN])
1743195379Ssam{
1744195379Ssam	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1745195379Ssam
1746195379Ssam	b += addr[5] << 8;
1747195379Ssam	b += addr[4];
1748195379Ssam	a += addr[3] << 24;
1749195379Ssam	a += addr[2] << 16;
1750195379Ssam	a += addr[1] << 8;
1751195379Ssam	a += addr[0];
1752195379Ssam
1753195379Ssam	mix(a, b, c);
1754195379Ssam
1755195379Ssam	return c;
1756195379Ssam}
1757195379Ssam#undef mix
1758