ieee80211.c revision 254082
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 254082 2013-08-08 05:09:35Z 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;
436254082Sadrian	ifp->if_transmit = ieee80211_vap_transmit;
437254082Sadrian	ifp->if_qflush = ieee80211_vap_qflush;
438178354Ssam	ifp->if_ioctl = ieee80211_ioctl;
439178354Ssam	ifp->if_init = ieee80211_init;
440178354Ssam
441178354Ssam	vap->iv_ifp = ifp;
442178354Ssam	vap->iv_ic = ic;
443178354Ssam	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
444178354Ssam	vap->iv_flags_ext = ic->ic_flags_ext;
445178354Ssam	vap->iv_flags_ven = ic->ic_flags_ven;
446178354Ssam	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
447178354Ssam	vap->iv_htcaps = ic->ic_htcaps;
448205513Srpaulo	vap->iv_htextcaps = ic->ic_htextcaps;
449178354Ssam	vap->iv_opmode = opmode;
450178957Ssam	vap->iv_caps |= ieee80211_opcap[opmode];
451178354Ssam	switch (opmode) {
452178354Ssam	case IEEE80211_M_WDS:
453178354Ssam		/*
454178354Ssam		 * WDS links must specify the bssid of the far end.
455178354Ssam		 * For legacy operation this is a static relationship.
456178354Ssam		 * For non-legacy operation the station must associate
457178354Ssam		 * and be authorized to pass traffic.  Plumbing the
458178354Ssam		 * vap to the proper node happens when the vap
459178354Ssam		 * transitions to RUN state.
460178354Ssam		 */
461178354Ssam		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
462178354Ssam		vap->iv_flags |= IEEE80211_F_DESBSSID;
463178354Ssam		if (flags & IEEE80211_CLONE_WDSLEGACY)
464178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
465178354Ssam		break;
466186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
467186904Ssam	case IEEE80211_M_AHDEMO:
468186904Ssam		if (flags & IEEE80211_CLONE_TDMA) {
469186904Ssam			/* NB: checked before clone operation allowed */
470186904Ssam			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
471186904Ssam			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
472186904Ssam			/*
473186904Ssam			 * Propagate TDMA capability to mark vap; this
474186904Ssam			 * cannot be removed and is used to distinguish
475186904Ssam			 * regular ahdemo operation from ahdemo+tdma.
476186904Ssam			 */
477186904Ssam			vap->iv_caps |= IEEE80211_C_TDMA;
478186904Ssam		}
479186904Ssam		break;
480186904Ssam#endif
481228621Sbschmidt	default:
482228621Sbschmidt		break;
483178354Ssam	}
484184278Ssam	/* auto-enable s/w beacon miss support */
485184278Ssam	if (flags & IEEE80211_CLONE_NOBEACONS)
486184278Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
487202612Sthompsa	/* auto-generated or user supplied MAC address */
488202612Sthompsa	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
489202612Sthompsa		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
490178354Ssam	/*
491178354Ssam	 * Enable various functionality by default if we're
492178354Ssam	 * capable; the driver can override us if it knows better.
493178354Ssam	 */
494178354Ssam	if (vap->iv_caps & IEEE80211_C_WME)
495178354Ssam		vap->iv_flags |= IEEE80211_F_WME;
496178354Ssam	if (vap->iv_caps & IEEE80211_C_BURST)
497178354Ssam		vap->iv_flags |= IEEE80211_F_BURST;
498178354Ssam	/* NB: bg scanning only makes sense for station mode right now */
499178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
500178354Ssam	    (vap->iv_caps & IEEE80211_C_BGSCAN))
501178354Ssam		vap->iv_flags |= IEEE80211_F_BGSCAN;
502178957Ssam	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
503178954Ssam	/* NB: DFS support only makes sense for ap mode right now */
504178954Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
505178954Ssam	    (vap->iv_caps & IEEE80211_C_DFS))
506178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
507178354Ssam
508178354Ssam	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
509178354Ssam	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
510178354Ssam	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
511178354Ssam	/*
512178354Ssam	 * Install a default reset method for the ioctl support;
513178354Ssam	 * the driver can override this.
514178354Ssam	 */
515178354Ssam	vap->iv_reset = default_reset;
516178354Ssam
517178354Ssam	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
518178354Ssam
519178354Ssam	ieee80211_sysctl_vattach(vap);
520178354Ssam	ieee80211_crypto_vattach(vap);
521178354Ssam	ieee80211_node_vattach(vap);
522178354Ssam	ieee80211_power_vattach(vap);
523178354Ssam	ieee80211_proto_vattach(vap);
524190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
525190391Ssam	ieee80211_superg_vattach(vap);
526190391Ssam#endif
527178354Ssam	ieee80211_ht_vattach(vap);
528178354Ssam	ieee80211_scan_vattach(vap);
529178354Ssam	ieee80211_regdomain_vattach(vap);
530192468Ssam	ieee80211_radiotap_vattach(vap);
531214894Sbschmidt	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
532178354Ssam
533178354Ssam	return 0;
534178354Ssam}
535178354Ssam
536178354Ssam/*
537178354Ssam * Activate a vap.  State should have been prepared with a
538178354Ssam * call to ieee80211_vap_setup and by the driver.  On return
539178354Ssam * from this call the vap is ready for use.
540178354Ssam */
541178354Ssamint
542178354Ssamieee80211_vap_attach(struct ieee80211vap *vap,
543178354Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
544178354Ssam{
545178354Ssam	struct ifnet *ifp = vap->iv_ifp;
546178354Ssam	struct ieee80211com *ic = vap->iv_ic;
547178354Ssam	struct ifmediareq imr;
548178354Ssam	int maxrate;
549178354Ssam
550178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
551178354Ssam	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
552178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
553178354Ssam	    ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
554178354Ssam
555178354Ssam	/*
556178354Ssam	 * Do late attach work that cannot happen until after
557178354Ssam	 * the driver has had a chance to override defaults.
558178354Ssam	 */
559178354Ssam	ieee80211_node_latevattach(vap);
560178354Ssam	ieee80211_power_latevattach(vap);
561178354Ssam
562178354Ssam	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
563178354Ssam	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
564178354Ssam	ieee80211_media_status(ifp, &imr);
565178354Ssam	/* NB: strip explicit mode; we're actually in autoselect */
566188106Ssam	ifmedia_set(&vap->iv_media,
567188106Ssam	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
568178354Ssam	if (maxrate)
569178354Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
570178354Ssam
571178354Ssam	ether_ifattach(ifp, vap->iv_myaddr);
572195846Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR) {
573195846Ssam		/* NB: disallow transmit */
574195846Ssam		ifp->if_transmit = null_transmit;
575195846Ssam		ifp->if_output = null_output;
576195846Ssam	} else {
577195846Ssam		/* hook output method setup by ether_ifattach */
578195846Ssam		vap->iv_output = ifp->if_output;
579195846Ssam		ifp->if_output = ieee80211_output;
580195846Ssam	}
581178354Ssam	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
582178354Ssam
583178354Ssam	IEEE80211_LOCK(ic);
584178354Ssam	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
585178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
586190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
587178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
588190391Ssam#endif
589178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
590178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
591193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
592193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
593178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
594178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
595178354Ssam	IEEE80211_UNLOCK(ic);
596178354Ssam
597178354Ssam	return 1;
598178354Ssam}
599178354Ssam
600178354Ssam/*
601178354Ssam * Tear down vap state and reclaim the ifnet.
602178354Ssam * The driver is assumed to have prepared for
603178354Ssam * this; e.g. by turning off interrupts for the
604178354Ssam * underlying device.
605178354Ssam */
606178354Ssamvoid
607178354Ssamieee80211_vap_detach(struct ieee80211vap *vap)
608178354Ssam{
609178354Ssam	struct ieee80211com *ic = vap->iv_ic;
610178354Ssam	struct ifnet *ifp = vap->iv_ifp;
611178354Ssam
612242149Sadrian	CURVNET_SET(ifp->if_vnet);
613242149Sadrian
614178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
615178354Ssam	    __func__, ieee80211_opmode_name[vap->iv_opmode],
616178354Ssam	    ic->ic_ifp->if_xname);
617178354Ssam
618193312Ssam	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
619193312Ssam	ether_ifdetach(ifp);
620178354Ssam
621193312Ssam	ieee80211_stop(vap);
622193312Ssam
623191746Sthompsa	/*
624191746Sthompsa	 * Flush any deferred vap tasks.
625191746Sthompsa	 */
626191746Sthompsa	ieee80211_draintask(ic, &vap->iv_nstate_task);
627191746Sthompsa	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
628191746Sthompsa
629196159Ssam	/* XXX band-aid until ifnet handles this for us */
630196159Ssam	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
631196159Ssam
632191746Sthompsa	IEEE80211_LOCK(ic);
633191746Sthompsa	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
634178354Ssam	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
635178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
636190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
637178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
638190391Ssam#endif
639178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
640178354Ssam	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
641193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
642193655Ssam	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
643192468Ssam	/* NB: this handles the bpfdetach done below */
644192468Ssam	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
645178354Ssam	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
646178354Ssam	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
647178354Ssam	IEEE80211_UNLOCK(ic);
648178354Ssam
649178354Ssam	ifmedia_removeall(&vap->iv_media);
650178354Ssam
651192468Ssam	ieee80211_radiotap_vdetach(vap);
652178354Ssam	ieee80211_regdomain_vdetach(vap);
653178354Ssam	ieee80211_scan_vdetach(vap);
654190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
655190391Ssam	ieee80211_superg_vdetach(vap);
656190391Ssam#endif
657178354Ssam	ieee80211_ht_vdetach(vap);
658178354Ssam	/* NB: must be before ieee80211_node_vdetach */
659178354Ssam	ieee80211_proto_vdetach(vap);
660178354Ssam	ieee80211_crypto_vdetach(vap);
661178354Ssam	ieee80211_power_vdetach(vap);
662178354Ssam	ieee80211_node_vdetach(vap);
663178354Ssam	ieee80211_sysctl_vdetach(vap);
664182674Sweongyo
665182674Sweongyo	if_free(ifp);
666242149Sadrian
667242149Sadrian	CURVNET_RESTORE();
668116742Ssam}
669116742Ssam
670178354Ssam/*
671178354Ssam * Synchronize flag bit state in the parent ifnet structure
672178354Ssam * according to the state of all vap ifnet's.  This is used,
673178354Ssam * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
674178354Ssam */
675178354Ssamvoid
676178354Ssamieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
677178354Ssam{
678178354Ssam	struct ifnet *ifp = ic->ic_ifp;
679178354Ssam	struct ieee80211vap *vap;
680178354Ssam	int bit, oflags;
681178354Ssam
682178354Ssam	IEEE80211_LOCK_ASSERT(ic);
683178354Ssam
684178354Ssam	bit = 0;
685178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
686178354Ssam		if (vap->iv_ifp->if_flags & flag) {
687178354Ssam			/*
688178354Ssam			 * XXX the bridge sets PROMISC but we don't want to
689178354Ssam			 * enable it on the device, discard here so all the
690178354Ssam			 * drivers don't need to special-case it
691178354Ssam			 */
692178354Ssam			if (flag == IFF_PROMISC &&
693195847Ssam			    !(vap->iv_opmode == IEEE80211_M_MONITOR ||
694196004Ssam			      (vap->iv_opmode == IEEE80211_M_AHDEMO &&
695196004Ssam			       (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
696178354Ssam				continue;
697178354Ssam			bit = 1;
698178354Ssam			break;
699178354Ssam		}
700178354Ssam	oflags = ifp->if_flags;
701178354Ssam	if (bit)
702178354Ssam		ifp->if_flags |= flag;
703178354Ssam	else
704178354Ssam		ifp->if_flags &= ~flag;
705178354Ssam	if ((ifp->if_flags ^ oflags) & flag) {
706178354Ssam		/* XXX should we return 1/0 and let caller do this? */
707178354Ssam		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
708178354Ssam			if (flag == IFF_PROMISC)
709191746Sthompsa				ieee80211_runtask(ic, &ic->ic_promisc_task);
710178354Ssam			else if (flag == IFF_ALLMULTI)
711191746Sthompsa				ieee80211_runtask(ic, &ic->ic_mcast_task);
712178354Ssam		}
713178354Ssam	}
714178354Ssam}
715178354Ssam
716178354Ssam/*
717178354Ssam * Synchronize flag bit state in the com structure
718178354Ssam * according to the state of all vap's.  This is used,
719178354Ssam * for example, to handle state changes via ioctls.
720178354Ssam */
721178354Ssamstatic void
722178354Ssamieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
723178354Ssam{
724178354Ssam	struct ieee80211vap *vap;
725178354Ssam	int bit;
726178354Ssam
727178354Ssam	IEEE80211_LOCK_ASSERT(ic);
728178354Ssam
729178354Ssam	bit = 0;
730178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
731178354Ssam		if (vap->iv_flags & flag) {
732178354Ssam			bit = 1;
733178354Ssam			break;
734178354Ssam		}
735178354Ssam	if (bit)
736178354Ssam		ic->ic_flags |= flag;
737178354Ssam	else
738178354Ssam		ic->ic_flags &= ~flag;
739178354Ssam}
740178354Ssam
741178354Ssamvoid
742178354Ssamieee80211_syncflag(struct ieee80211vap *vap, int flag)
743178354Ssam{
744178354Ssam	struct ieee80211com *ic = vap->iv_ic;
745178354Ssam
746178354Ssam	IEEE80211_LOCK(ic);
747178354Ssam	if (flag < 0) {
748178354Ssam		flag = -flag;
749178354Ssam		vap->iv_flags &= ~flag;
750178354Ssam	} else
751178354Ssam		vap->iv_flags |= flag;
752178354Ssam	ieee80211_syncflag_locked(ic, flag);
753178354Ssam	IEEE80211_UNLOCK(ic);
754178354Ssam}
755178354Ssam
756178354Ssam/*
757193655Ssam * Synchronize flags_ht bit state in the com structure
758178354Ssam * according to the state of all vap's.  This is used,
759178354Ssam * for example, to handle state changes via ioctls.
760178354Ssam */
761178354Ssamstatic void
762193655Ssamieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
763193655Ssam{
764193655Ssam	struct ieee80211vap *vap;
765193655Ssam	int bit;
766193655Ssam
767193655Ssam	IEEE80211_LOCK_ASSERT(ic);
768193655Ssam
769193655Ssam	bit = 0;
770193655Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
771193655Ssam		if (vap->iv_flags_ht & flag) {
772193655Ssam			bit = 1;
773193655Ssam			break;
774193655Ssam		}
775193655Ssam	if (bit)
776193655Ssam		ic->ic_flags_ht |= flag;
777193655Ssam	else
778193655Ssam		ic->ic_flags_ht &= ~flag;
779193655Ssam}
780193655Ssam
781193655Ssamvoid
782193655Ssamieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
783193655Ssam{
784193655Ssam	struct ieee80211com *ic = vap->iv_ic;
785193655Ssam
786193655Ssam	IEEE80211_LOCK(ic);
787193655Ssam	if (flag < 0) {
788193655Ssam		flag = -flag;
789193655Ssam		vap->iv_flags_ht &= ~flag;
790193655Ssam	} else
791193655Ssam		vap->iv_flags_ht |= flag;
792193655Ssam	ieee80211_syncflag_ht_locked(ic, flag);
793193655Ssam	IEEE80211_UNLOCK(ic);
794193655Ssam}
795193655Ssam
796193655Ssam/*
797193655Ssam * Synchronize flags_ext bit state in the com structure
798193655Ssam * according to the state of all vap's.  This is used,
799193655Ssam * for example, to handle state changes via ioctls.
800193655Ssam */
801193655Ssamstatic void
802178354Ssamieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
803178354Ssam{
804178354Ssam	struct ieee80211vap *vap;
805178354Ssam	int bit;
806178354Ssam
807178354Ssam	IEEE80211_LOCK_ASSERT(ic);
808178354Ssam
809178354Ssam	bit = 0;
810178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
811178354Ssam		if (vap->iv_flags_ext & flag) {
812178354Ssam			bit = 1;
813178354Ssam			break;
814178354Ssam		}
815178354Ssam	if (bit)
816178354Ssam		ic->ic_flags_ext |= flag;
817178354Ssam	else
818178354Ssam		ic->ic_flags_ext &= ~flag;
819178354Ssam}
820178354Ssam
821178354Ssamvoid
822178354Ssamieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
823178354Ssam{
824178354Ssam	struct ieee80211com *ic = vap->iv_ic;
825178354Ssam
826178354Ssam	IEEE80211_LOCK(ic);
827178354Ssam	if (flag < 0) {
828178354Ssam		flag = -flag;
829178354Ssam		vap->iv_flags_ext &= ~flag;
830178354Ssam	} else
831178354Ssam		vap->iv_flags_ext |= flag;
832178354Ssam	ieee80211_syncflag_ext_locked(ic, flag);
833178354Ssam	IEEE80211_UNLOCK(ic);
834178354Ssam}
835178354Ssam
836166012Ssamstatic __inline int
837166012Ssammapgsm(u_int freq, u_int flags)
838166012Ssam{
839166012Ssam	freq *= 10;
840166012Ssam	if (flags & IEEE80211_CHAN_QUARTER)
841166012Ssam		freq += 5;
842166012Ssam	else if (flags & IEEE80211_CHAN_HALF)
843166012Ssam		freq += 10;
844166012Ssam	else
845166012Ssam		freq += 20;
846166012Ssam	/* NB: there is no 907/20 wide but leave room */
847166012Ssam	return (freq - 906*10) / 5;
848166012Ssam}
849166012Ssam
850166012Ssamstatic __inline int
851166012Ssammappsb(u_int freq, u_int flags)
852166012Ssam{
853166012Ssam	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
854166012Ssam}
855166012Ssam
856116742Ssam/*
857116742Ssam * Convert MHz frequency to IEEE channel number.
858116742Ssam */
859152450Ssamint
860116742Ssamieee80211_mhz2ieee(u_int freq, u_int flags)
861116742Ssam{
862167430Ssam#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
863166012Ssam	if (flags & IEEE80211_CHAN_GSM)
864166012Ssam		return mapgsm(freq, flags);
865116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
866116742Ssam		if (freq == 2484)
867116742Ssam			return 14;
868116742Ssam		if (freq < 2484)
869152450Ssam			return ((int) freq - 2407) / 5;
870116742Ssam		else
871116742Ssam			return 15 + ((freq - 2512) / 20);
872116899Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
873165569Ssam		if (freq <= 5000) {
874170530Ssam			/* XXX check regdomain? */
875167430Ssam			if (IS_FREQ_IN_PSB(freq))
876166012Ssam				return mappsb(freq, flags);
877152450Ssam			return (freq - 4000) / 5;
878165569Ssam		} else
879152450Ssam			return (freq - 5000) / 5;
880116742Ssam	} else {				/* either, guess */
881116742Ssam		if (freq == 2484)
882116742Ssam			return 14;
883166012Ssam		if (freq < 2484) {
884166012Ssam			if (907 <= freq && freq <= 922)
885166012Ssam				return mapgsm(freq, flags);
886152450Ssam			return ((int) freq - 2407) / 5;
887166012Ssam		}
888152450Ssam		if (freq < 5000) {
889167430Ssam			if (IS_FREQ_IN_PSB(freq))
890166012Ssam				return mappsb(freq, flags);
891165569Ssam			else if (freq > 4900)
892152450Ssam				return (freq - 4000) / 5;
893152450Ssam			else
894152450Ssam				return 15 + ((freq - 2512) / 20);
895152450Ssam		}
896116742Ssam		return (freq - 5000) / 5;
897116742Ssam	}
898167430Ssam#undef IS_FREQ_IN_PSB
899116742Ssam}
900116742Ssam
901116742Ssam/*
902116742Ssam * Convert channel to IEEE channel number.
903116742Ssam */
904152450Ssamint
905165825Smjacobieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
906116742Ssam{
907170530Ssam	if (c == NULL) {
908138568Ssam		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
909117039Ssam		return 0;		/* XXX */
910116742Ssam	}
911170530Ssam	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
912116742Ssam}
913116742Ssam
914116742Ssam/*
915116742Ssam * Convert IEEE channel number to MHz frequency.
916116742Ssam */
917116742Ssamu_int
918116742Ssamieee80211_ieee2mhz(u_int chan, u_int flags)
919116742Ssam{
920166012Ssam	if (flags & IEEE80211_CHAN_GSM)
921166012Ssam		return 907 + 5 * (chan / 10);
922116742Ssam	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
923116742Ssam		if (chan == 14)
924116742Ssam			return 2484;
925116742Ssam		if (chan < 14)
926116742Ssam			return 2407 + chan*5;
927116742Ssam		else
928116742Ssam			return 2512 + ((chan-15)*20);
929116742Ssam	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
930165569Ssam		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
931165569Ssam			chan -= 37;
932165569Ssam			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
933165569Ssam		}
934116742Ssam		return 5000 + (chan*5);
935116742Ssam	} else {				/* either, guess */
936166012Ssam		/* XXX can't distinguish PSB+GSM channels */
937116742Ssam		if (chan == 14)
938116742Ssam			return 2484;
939116742Ssam		if (chan < 14)			/* 0-13 */
940116742Ssam			return 2407 + chan*5;
941116742Ssam		if (chan < 27)			/* 15-26 */
942116742Ssam			return 2512 + ((chan-15)*20);
943116742Ssam		return 5000 + (chan*5);
944116742Ssam	}
945116742Ssam}
946116742Ssam
947116742Ssam/*
948170530Ssam * Locate a channel given a frequency+flags.  We cache
949178354Ssam * the previous lookup to optimize switching between two
950170530Ssam * channels--as happens with dynamic turbo.
951170530Ssam */
952170530Ssamstruct ieee80211_channel *
953170530Ssamieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
954170530Ssam{
955170530Ssam	struct ieee80211_channel *c;
956170530Ssam	int i;
957170530Ssam
958170530Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
959170530Ssam	c = ic->ic_prevchan;
960170530Ssam	if (c != NULL && c->ic_freq == freq &&
961170530Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
962170530Ssam		return c;
963170530Ssam	/* brute force search */
964170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
965170530Ssam		c = &ic->ic_channels[i];
966170530Ssam		if (c->ic_freq == freq &&
967170530Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
968170530Ssam			return c;
969170530Ssam	}
970170530Ssam	return NULL;
971170530Ssam}
972170530Ssam
973173861Ssam/*
974173861Ssam * Locate a channel given a channel number+flags.  We cache
975173861Ssam * the previous lookup to optimize switching between two
976173861Ssam * channels--as happens with dynamic turbo.
977173861Ssam */
978173861Ssamstruct ieee80211_channel *
979173861Ssamieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
980173861Ssam{
981173861Ssam	struct ieee80211_channel *c;
982173861Ssam	int i;
983173861Ssam
984173861Ssam	flags &= IEEE80211_CHAN_ALLTURBO;
985173861Ssam	c = ic->ic_prevchan;
986173861Ssam	if (c != NULL && c->ic_ieee == ieee &&
987173861Ssam	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
988173861Ssam		return c;
989173861Ssam	/* brute force search */
990173861Ssam	for (i = 0; i < ic->ic_nchans; i++) {
991173861Ssam		c = &ic->ic_channels[i];
992173861Ssam		if (c->ic_ieee == ieee &&
993173861Ssam		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
994173861Ssam			return c;
995173861Ssam	}
996173861Ssam	return NULL;
997173861Ssam}
998173861Ssam
999170530Ssamstatic void
1000178354Ssamaddmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1001170530Ssam{
1002170530Ssam#define	ADD(_ic, _s, _o) \
1003178354Ssam	ifmedia_add(media, \
1004170530Ssam		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1005170530Ssam	static const u_int mopts[IEEE80211_MODE_MAX] = {
1006188106Ssam	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1007188106Ssam	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1008188106Ssam	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1009188106Ssam	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1010188106Ssam	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1011188106Ssam	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1012188106Ssam	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1013188106Ssam	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1014188782Ssam	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1015188782Ssam	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1016188106Ssam	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1017188106Ssam	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1018170530Ssam	};
1019170530Ssam	u_int mopt;
1020170530Ssam
1021170530Ssam	mopt = mopts[mode];
1022178354Ssam	if (addsta)
1023178354Ssam		ADD(ic, mword, mopt);	/* STA mode has no cap */
1024178354Ssam	if (caps & IEEE80211_C_IBSS)
1025178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1026178354Ssam	if (caps & IEEE80211_C_HOSTAP)
1027178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1028178354Ssam	if (caps & IEEE80211_C_AHDEMO)
1029178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1030178354Ssam	if (caps & IEEE80211_C_MONITOR)
1031178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1032178354Ssam	if (caps & IEEE80211_C_WDS)
1033178354Ssam		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1034195618Srpaulo	if (caps & IEEE80211_C_MBSS)
1035195618Srpaulo		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1036170530Ssam#undef ADD
1037170530Ssam}
1038170530Ssam
1039170530Ssam/*
1040116742Ssam * Setup the media data structures according to the channel and
1041178354Ssam * rate tables.
1042116742Ssam */
1043178354Ssamstatic int
1044178354Ssamieee80211_media_setup(struct ieee80211com *ic,
1045178354Ssam	struct ifmedia *media, int caps, int addsta,
1046116742Ssam	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1047116742Ssam{
1048228621Sbschmidt	int i, j, rate, maxrate, mword, r;
1049228621Sbschmidt	enum ieee80211_phymode mode;
1050170530Ssam	const struct ieee80211_rateset *rs;
1051116742Ssam	struct ieee80211_rateset allrates;
1052116742Ssam
1053118887Ssam	/*
1054116742Ssam	 * Fill in media characteristics.
1055116742Ssam	 */
1056178354Ssam	ifmedia_init(media, 0, media_change, media_stat);
1057116742Ssam	maxrate = 0;
1058170530Ssam	/*
1059170530Ssam	 * Add media for legacy operating modes.
1060170530Ssam	 */
1061116742Ssam	memset(&allrates, 0, sizeof(allrates));
1062170530Ssam	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1063167468Ssam		if (isclr(ic->ic_modecaps, mode))
1064116742Ssam			continue;
1065178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1066116742Ssam		if (mode == IEEE80211_MODE_AUTO)
1067116742Ssam			continue;
1068116742Ssam		rs = &ic->ic_sup_rates[mode];
1069116742Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1070116742Ssam			rate = rs->rs_rates[i];
1071116742Ssam			mword = ieee80211_rate2media(ic, rate, mode);
1072116742Ssam			if (mword == 0)
1073116742Ssam				continue;
1074178354Ssam			addmedia(media, caps, addsta, mode, mword);
1075116742Ssam			/*
1076170530Ssam			 * Add legacy rate to the collection of all rates.
1077116742Ssam			 */
1078116742Ssam			r = rate & IEEE80211_RATE_VAL;
1079116742Ssam			for (j = 0; j < allrates.rs_nrates; j++)
1080116742Ssam				if (allrates.rs_rates[j] == r)
1081116742Ssam					break;
1082116742Ssam			if (j == allrates.rs_nrates) {
1083116742Ssam				/* unique, add to the set */
1084116742Ssam				allrates.rs_rates[j] = r;
1085116742Ssam				allrates.rs_nrates++;
1086116742Ssam			}
1087116742Ssam			rate = (rate & IEEE80211_RATE_VAL) / 2;
1088116742Ssam			if (rate > maxrate)
1089116742Ssam				maxrate = rate;
1090116742Ssam		}
1091116742Ssam	}
1092116742Ssam	for (i = 0; i < allrates.rs_nrates; i++) {
1093116742Ssam		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1094116742Ssam				IEEE80211_MODE_AUTO);
1095116742Ssam		if (mword == 0)
1096116742Ssam			continue;
1097170530Ssam		/* NB: remove media options from mword */
1098178354Ssam		addmedia(media, caps, addsta,
1099178354Ssam		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1100116742Ssam	}
1101170530Ssam	/*
1102170530Ssam	 * Add HT/11n media.  Note that we do not have enough
1103170530Ssam	 * bits in the media subtype to express the MCS so we
1104170530Ssam	 * use a "placeholder" media subtype and any fixed MCS
1105170530Ssam	 * must be specified with a different mechanism.
1106170530Ssam	 */
1107188782Ssam	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1108170530Ssam		if (isclr(ic->ic_modecaps, mode))
1109170530Ssam			continue;
1110178354Ssam		addmedia(media, caps, addsta, mode, IFM_AUTO);
1111178354Ssam		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1112170530Ssam	}
1113170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1114170530Ssam	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1115178354Ssam		addmedia(media, caps, addsta,
1116178354Ssam		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1117219599Sbschmidt		i = ic->ic_txstream * 8 - 1;
1118219599Sbschmidt		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1119219599Sbschmidt		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1120219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_400ns;
1121219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1122219599Sbschmidt			rate = ieee80211_htrates[i].ht40_rate_800ns;
1123219599Sbschmidt		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1124219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_400ns;
1125219599Sbschmidt		else
1126219599Sbschmidt			rate = ieee80211_htrates[i].ht20_rate_800ns;
1127219599Sbschmidt		if (rate > maxrate)
1128219599Sbschmidt			maxrate = rate;
1129170530Ssam	}
1130178354Ssam	return maxrate;
1131178354Ssam}
1132116742Ssam
1133178354Ssamvoid
1134178354Ssamieee80211_media_init(struct ieee80211com *ic)
1135178354Ssam{
1136178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1137178354Ssam	int maxrate;
1138178354Ssam
1139178354Ssam	/* NB: this works because the structure is initialized to zero */
1140178354Ssam	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1141178354Ssam		/*
1142178354Ssam		 * We are re-initializing the channel list; clear
1143178354Ssam		 * the existing media state as the media routines
1144178354Ssam		 * don't suppress duplicates.
1145178354Ssam		 */
1146178354Ssam		ifmedia_removeall(&ic->ic_media);
1147178354Ssam	}
1148178354Ssam	ieee80211_chan_init(ic);
1149178354Ssam
1150178354Ssam	/*
1151178354Ssam	 * Recalculate media settings in case new channel list changes
1152178354Ssam	 * the set of available modes.
1153178354Ssam	 */
1154178354Ssam	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1155178354Ssam		ieee80211com_media_change, ieee80211com_media_status);
1156170530Ssam	/* NB: strip explicit mode; we're actually in autoselect */
1157170530Ssam	ifmedia_set(&ic->ic_media,
1158188106Ssam	    media_status(ic->ic_opmode, ic->ic_curchan) &~
1159188106Ssam		(IFM_MMASK | IFM_IEEE80211_TURBO));
1160116742Ssam	if (maxrate)
1161116742Ssam		ifp->if_baudrate = IF_Mbps(maxrate);
1162178354Ssam
1163178354Ssam	/* XXX need to propagate new media settings to vap's */
1164116742Ssam}
1165116742Ssam
1166188782Ssam/* XXX inline or eliminate? */
1167165569Ssamconst struct ieee80211_rateset *
1168165569Ssamieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1169165569Ssam{
1170188774Ssam	/* XXX does this work for 11ng basic rates? */
1171170530Ssam	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1172165569Ssam}
1173165569Ssam
1174138568Ssamvoid
1175138568Ssamieee80211_announce(struct ieee80211com *ic)
1176138568Ssam{
1177138568Ssam	struct ifnet *ifp = ic->ic_ifp;
1178228621Sbschmidt	int i, rate, mword;
1179228621Sbschmidt	enum ieee80211_phymode mode;
1180170530Ssam	const struct ieee80211_rateset *rs;
1181138568Ssam
1182172227Ssam	/* NB: skip AUTO since it has no rates */
1183172227Ssam	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1184167468Ssam		if (isclr(ic->ic_modecaps, mode))
1185138568Ssam			continue;
1186138568Ssam		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1187138568Ssam		rs = &ic->ic_sup_rates[mode];
1188138568Ssam		for (i = 0; i < rs->rs_nrates; i++) {
1189170530Ssam			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1190138568Ssam			if (mword == 0)
1191138568Ssam				continue;
1192170530Ssam			rate = ieee80211_media2rate(mword);
1193138568Ssam			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1194170530Ssam			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1195138568Ssam		}
1196138568Ssam		printf("\n");
1197138568Ssam	}
1198170530Ssam	ieee80211_ht_announce(ic);
1199138568Ssam}
1200138568Ssam
1201170530Ssamvoid
1202170530Ssamieee80211_announce_channels(struct ieee80211com *ic)
1203116742Ssam{
1204170530Ssam	const struct ieee80211_channel *c;
1205170530Ssam	char type;
1206170530Ssam	int i, cw;
1207170530Ssam
1208170530Ssam	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1209170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
1210170530Ssam		c = &ic->ic_channels[i];
1211170530Ssam		if (IEEE80211_IS_CHAN_ST(c))
1212170530Ssam			type = 'S';
1213170530Ssam		else if (IEEE80211_IS_CHAN_108A(c))
1214170530Ssam			type = 'T';
1215170530Ssam		else if (IEEE80211_IS_CHAN_108G(c))
1216170530Ssam			type = 'G';
1217170530Ssam		else if (IEEE80211_IS_CHAN_HT(c))
1218170530Ssam			type = 'n';
1219170530Ssam		else if (IEEE80211_IS_CHAN_A(c))
1220170530Ssam			type = 'a';
1221170530Ssam		else if (IEEE80211_IS_CHAN_ANYG(c))
1222170530Ssam			type = 'g';
1223170530Ssam		else if (IEEE80211_IS_CHAN_B(c))
1224170530Ssam			type = 'b';
1225170530Ssam		else
1226170530Ssam			type = 'f';
1227170530Ssam		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1228170530Ssam			cw = 40;
1229170530Ssam		else if (IEEE80211_IS_CHAN_HALF(c))
1230170530Ssam			cw = 10;
1231170530Ssam		else if (IEEE80211_IS_CHAN_QUARTER(c))
1232170530Ssam			cw = 5;
1233170530Ssam		else
1234170530Ssam			cw = 20;
1235170530Ssam		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1236170530Ssam			, c->ic_ieee, c->ic_freq, type
1237170530Ssam			, cw
1238170530Ssam			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1239170530Ssam			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1240170530Ssam			, c->ic_maxregpower
1241170530Ssam			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1242170530Ssam			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1243170530Ssam		);
1244170530Ssam	}
1245116742Ssam}
1246116742Ssam
1247170530Ssamstatic int
1248184273Ssammedia2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1249170530Ssam{
1250116742Ssam	switch (IFM_MODE(ime->ifm_media)) {
1251116742Ssam	case IFM_IEEE80211_11A:
1252178354Ssam		*mode = IEEE80211_MODE_11A;
1253116742Ssam		break;
1254116742Ssam	case IFM_IEEE80211_11B:
1255178354Ssam		*mode = IEEE80211_MODE_11B;
1256116742Ssam		break;
1257116742Ssam	case IFM_IEEE80211_11G:
1258178354Ssam		*mode = IEEE80211_MODE_11G;
1259116742Ssam		break;
1260124543Sonoe	case IFM_IEEE80211_FH:
1261178354Ssam		*mode = IEEE80211_MODE_FH;
1262124543Sonoe		break;
1263170530Ssam	case IFM_IEEE80211_11NA:
1264178354Ssam		*mode = IEEE80211_MODE_11NA;
1265170530Ssam		break;
1266170530Ssam	case IFM_IEEE80211_11NG:
1267178354Ssam		*mode = IEEE80211_MODE_11NG;
1268170530Ssam		break;
1269116742Ssam	case IFM_AUTO:
1270178354Ssam		*mode = IEEE80211_MODE_AUTO;
1271116742Ssam		break;
1272116742Ssam	default:
1273178354Ssam		return 0;
1274116742Ssam	}
1275116742Ssam	/*
1276138568Ssam	 * Turbo mode is an ``option''.
1277138568Ssam	 * XXX does not apply to AUTO
1278116742Ssam	 */
1279116742Ssam	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1280178354Ssam		if (*mode == IEEE80211_MODE_11A) {
1281184273Ssam			if (flags & IEEE80211_F_TURBOP)
1282178354Ssam				*mode = IEEE80211_MODE_TURBO_A;
1283170530Ssam			else
1284178354Ssam				*mode = IEEE80211_MODE_STURBO_A;
1285178354Ssam		} else if (*mode == IEEE80211_MODE_11G)
1286178354Ssam			*mode = IEEE80211_MODE_TURBO_G;
1287138568Ssam		else
1288178354Ssam			return 0;
1289116742Ssam	}
1290170530Ssam	/* XXX HT40 +/- */
1291178354Ssam	return 1;
1292178354Ssam}
1293116742Ssam
1294178354Ssam/*
1295184273Ssam * Handle a media change request on the underlying interface.
1296178354Ssam */
1297178354Ssamint
1298178354Ssamieee80211com_media_change(struct ifnet *ifp)
1299178354Ssam{
1300184273Ssam	return EINVAL;
1301178354Ssam}
1302116742Ssam
1303178354Ssam/*
1304178354Ssam * Handle a media change request on the vap interface.
1305178354Ssam */
1306178354Ssamint
1307178354Ssamieee80211_media_change(struct ifnet *ifp)
1308178354Ssam{
1309178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1310178354Ssam	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1311184273Ssam	uint16_t newmode;
1312178354Ssam
1313184273Ssam	if (!media2mode(ime, vap->iv_flags, &newmode))
1314178354Ssam		return EINVAL;
1315184273Ssam	if (vap->iv_des_mode != newmode) {
1316184273Ssam		vap->iv_des_mode = newmode;
1317193340Ssam		/* XXX kick state machine if up+running */
1318178354Ssam	}
1319178354Ssam	return 0;
1320116742Ssam}
1321116742Ssam
1322170530Ssam/*
1323170530Ssam * Common code to calculate the media status word
1324170530Ssam * from the operating mode and channel state.
1325170530Ssam */
1326170530Ssamstatic int
1327170530Ssammedia_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1328170530Ssam{
1329170530Ssam	int status;
1330170530Ssam
1331170530Ssam	status = IFM_IEEE80211;
1332170530Ssam	switch (opmode) {
1333170530Ssam	case IEEE80211_M_STA:
1334170530Ssam		break;
1335170530Ssam	case IEEE80211_M_IBSS:
1336170530Ssam		status |= IFM_IEEE80211_ADHOC;
1337170530Ssam		break;
1338170530Ssam	case IEEE80211_M_HOSTAP:
1339170530Ssam		status |= IFM_IEEE80211_HOSTAP;
1340170530Ssam		break;
1341170530Ssam	case IEEE80211_M_MONITOR:
1342170530Ssam		status |= IFM_IEEE80211_MONITOR;
1343170530Ssam		break;
1344170530Ssam	case IEEE80211_M_AHDEMO:
1345170530Ssam		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1346170530Ssam		break;
1347170530Ssam	case IEEE80211_M_WDS:
1348178354Ssam		status |= IFM_IEEE80211_WDS;
1349170530Ssam		break;
1350195618Srpaulo	case IEEE80211_M_MBSS:
1351195618Srpaulo		status |= IFM_IEEE80211_MBSS;
1352195618Srpaulo		break;
1353170530Ssam	}
1354170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan)) {
1355170530Ssam		status |= IFM_IEEE80211_11NA;
1356170530Ssam	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1357170530Ssam		status |= IFM_IEEE80211_11NG;
1358170530Ssam	} else if (IEEE80211_IS_CHAN_A(chan)) {
1359170530Ssam		status |= IFM_IEEE80211_11A;
1360170530Ssam	} else if (IEEE80211_IS_CHAN_B(chan)) {
1361170530Ssam		status |= IFM_IEEE80211_11B;
1362170530Ssam	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1363170530Ssam		status |= IFM_IEEE80211_11G;
1364170530Ssam	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1365170530Ssam		status |= IFM_IEEE80211_FH;
1366170530Ssam	}
1367170530Ssam	/* XXX else complain? */
1368170530Ssam
1369170530Ssam	if (IEEE80211_IS_CHAN_TURBO(chan))
1370170530Ssam		status |= IFM_IEEE80211_TURBO;
1371178354Ssam#if 0
1372178354Ssam	if (IEEE80211_IS_CHAN_HT20(chan))
1373178354Ssam		status |= IFM_IEEE80211_HT20;
1374178354Ssam	if (IEEE80211_IS_CHAN_HT40(chan))
1375178354Ssam		status |= IFM_IEEE80211_HT40;
1376178354Ssam#endif
1377170530Ssam	return status;
1378170530Ssam}
1379170530Ssam
1380178354Ssamstatic void
1381178354Ssamieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1382178354Ssam{
1383178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1384178354Ssam	struct ieee80211vap *vap;
1385178354Ssam
1386178354Ssam	imr->ifm_status = IFM_AVALID;
1387178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1388178354Ssam		if (vap->iv_ifp->if_flags & IFF_UP) {
1389178354Ssam			imr->ifm_status |= IFM_ACTIVE;
1390178354Ssam			break;
1391178354Ssam		}
1392178354Ssam	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1393178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1394178354Ssam		imr->ifm_current = imr->ifm_active;
1395178354Ssam}
1396178354Ssam
1397116742Ssamvoid
1398116742Ssamieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1399116742Ssam{
1400178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1401178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1402170530Ssam	enum ieee80211_phymode mode;
1403116742Ssam
1404116742Ssam	imr->ifm_status = IFM_AVALID;
1405170530Ssam	/*
1406170530Ssam	 * NB: use the current channel's mode to lock down a xmit
1407170530Ssam	 * rate only when running; otherwise we may have a mismatch
1408170530Ssam	 * in which case the rate will not be convertible.
1409170530Ssam	 */
1410178354Ssam	if (vap->iv_state == IEEE80211_S_RUN) {
1411116742Ssam		imr->ifm_status |= IFM_ACTIVE;
1412170530Ssam		mode = ieee80211_chan2mode(ic->ic_curchan);
1413170530Ssam	} else
1414170530Ssam		mode = IEEE80211_MODE_AUTO;
1415178354Ssam	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1416138568Ssam	/*
1417138568Ssam	 * Calculate a current rate if possible.
1418138568Ssam	 */
1419178354Ssam	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1420138568Ssam		/*
1421138568Ssam		 * A fixed rate is set, report that.
1422138568Ssam		 */
1423138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1424178354Ssam			vap->iv_txparms[mode].ucastrate, mode);
1425178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1426138568Ssam		/*
1427138568Ssam		 * In station mode report the current transmit rate.
1428138568Ssam		 */
1429138568Ssam		imr->ifm_active |= ieee80211_rate2media(ic,
1430178354Ssam			vap->iv_bss->ni_txrate, mode);
1431128966Sandre	} else
1432138568Ssam		imr->ifm_active |= IFM_AUTO;
1433178354Ssam	if (imr->ifm_status & IFM_ACTIVE)
1434178354Ssam		imr->ifm_current = imr->ifm_active;
1435116742Ssam}
1436116742Ssam
1437116742Ssam/*
1438116742Ssam * Set the current phy mode and recalculate the active channel
1439116742Ssam * set based on the available channels for this mode.  Also
1440116742Ssam * select a new default/current channel if the current one is
1441116742Ssam * inappropriate for this mode.
1442116742Ssam */
1443116742Ssamint
1444116742Ssamieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1445116742Ssam{
1446116742Ssam	/*
1447166012Ssam	 * Adjust basic rates in 11b/11g supported rate set.
1448166012Ssam	 * Note that if operating on a hal/quarter rate channel
1449166012Ssam	 * this is a noop as those rates sets are different
1450166012Ssam	 * and used instead.
1451116742Ssam	 */
1452166012Ssam	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1453178354Ssam		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1454166012Ssam
1455116742Ssam	ic->ic_curmode = mode;
1456138568Ssam	ieee80211_reset_erp(ic);	/* reset ERP state */
1457138568Ssam
1458116742Ssam	return 0;
1459116742Ssam}
1460116742Ssam
1461116742Ssam/*
1462170530Ssam * Return the phy mode for with the specified channel.
1463116742Ssam */
1464116742Ssamenum ieee80211_phymode
1465170530Ssamieee80211_chan2mode(const struct ieee80211_channel *chan)
1466116742Ssam{
1467170530Ssam
1468170530Ssam	if (IEEE80211_IS_CHAN_HTA(chan))
1469170530Ssam		return IEEE80211_MODE_11NA;
1470170530Ssam	else if (IEEE80211_IS_CHAN_HTG(chan))
1471170530Ssam		return IEEE80211_MODE_11NG;
1472170530Ssam	else if (IEEE80211_IS_CHAN_108G(chan))
1473170530Ssam		return IEEE80211_MODE_TURBO_G;
1474170530Ssam	else if (IEEE80211_IS_CHAN_ST(chan))
1475170530Ssam		return IEEE80211_MODE_STURBO_A;
1476170530Ssam	else if (IEEE80211_IS_CHAN_TURBO(chan))
1477153350Ssam		return IEEE80211_MODE_TURBO_A;
1478188782Ssam	else if (IEEE80211_IS_CHAN_HALF(chan))
1479188782Ssam		return IEEE80211_MODE_HALF;
1480188782Ssam	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1481188782Ssam		return IEEE80211_MODE_QUARTER;
1482170530Ssam	else if (IEEE80211_IS_CHAN_A(chan))
1483116742Ssam		return IEEE80211_MODE_11A;
1484170530Ssam	else if (IEEE80211_IS_CHAN_ANYG(chan))
1485116742Ssam		return IEEE80211_MODE_11G;
1486170530Ssam	else if (IEEE80211_IS_CHAN_B(chan))
1487116742Ssam		return IEEE80211_MODE_11B;
1488170530Ssam	else if (IEEE80211_IS_CHAN_FHSS(chan))
1489170530Ssam		return IEEE80211_MODE_FH;
1490170530Ssam
1491170530Ssam	/* NB: should not get here */
1492170530Ssam	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1493170530Ssam		__func__, chan->ic_freq, chan->ic_flags);
1494170530Ssam	return IEEE80211_MODE_11B;
1495116742Ssam}
1496116742Ssam
1497170530Ssamstruct ratemedia {
1498170530Ssam	u_int	match;	/* rate + mode */
1499170530Ssam	u_int	media;	/* if_media rate */
1500170530Ssam};
1501170530Ssam
1502170530Ssamstatic int
1503170530Ssamfindmedia(const struct ratemedia rates[], int n, u_int match)
1504170530Ssam{
1505170530Ssam	int i;
1506170530Ssam
1507170530Ssam	for (i = 0; i < n; i++)
1508170530Ssam		if (rates[i].match == match)
1509170530Ssam			return rates[i].media;
1510170530Ssam	return IFM_AUTO;
1511170530Ssam}
1512170530Ssam
1513116742Ssam/*
1514170530Ssam * Convert IEEE80211 rate value to ifmedia subtype.
1515170530Ssam * Rate is either a legacy rate in units of 0.5Mbps
1516170530Ssam * or an MCS index.
1517116742Ssam */
1518116742Ssamint
1519116742Ssamieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1520116742Ssam{
1521116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1522170530Ssam	static const struct ratemedia rates[] = {
1523124543Sonoe		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1524124543Sonoe		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1525124543Sonoe		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1526124543Sonoe		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1527124543Sonoe		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1528124543Sonoe		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1529124543Sonoe		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1530124543Sonoe		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1531124543Sonoe		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1532124543Sonoe		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1533124543Sonoe		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1534124543Sonoe		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1535124543Sonoe		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1536124543Sonoe		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1537124543Sonoe		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1538124543Sonoe		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1539124543Sonoe		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1540124543Sonoe		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1541124543Sonoe		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1542124543Sonoe		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1543124543Sonoe		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1544124543Sonoe		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1545124543Sonoe		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1546124543Sonoe		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1547124543Sonoe		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1548124543Sonoe		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1549124543Sonoe		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1550165569Ssam		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1551165569Ssam		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1552165569Ssam		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1553116742Ssam		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1554116742Ssam	};
1555170530Ssam	static const struct ratemedia htrates[] = {
1556170530Ssam		{   0, IFM_IEEE80211_MCS },
1557170530Ssam		{   1, IFM_IEEE80211_MCS },
1558170530Ssam		{   2, IFM_IEEE80211_MCS },
1559170530Ssam		{   3, IFM_IEEE80211_MCS },
1560170530Ssam		{   4, IFM_IEEE80211_MCS },
1561170530Ssam		{   5, IFM_IEEE80211_MCS },
1562170530Ssam		{   6, IFM_IEEE80211_MCS },
1563170530Ssam		{   7, IFM_IEEE80211_MCS },
1564170530Ssam		{   8, IFM_IEEE80211_MCS },
1565170530Ssam		{   9, IFM_IEEE80211_MCS },
1566170530Ssam		{  10, IFM_IEEE80211_MCS },
1567170530Ssam		{  11, IFM_IEEE80211_MCS },
1568170530Ssam		{  12, IFM_IEEE80211_MCS },
1569170530Ssam		{  13, IFM_IEEE80211_MCS },
1570170530Ssam		{  14, IFM_IEEE80211_MCS },
1571170530Ssam		{  15, IFM_IEEE80211_MCS },
1572219456Sbschmidt		{  16, IFM_IEEE80211_MCS },
1573219456Sbschmidt		{  17, IFM_IEEE80211_MCS },
1574219456Sbschmidt		{  18, IFM_IEEE80211_MCS },
1575219456Sbschmidt		{  19, IFM_IEEE80211_MCS },
1576219456Sbschmidt		{  20, IFM_IEEE80211_MCS },
1577219456Sbschmidt		{  21, IFM_IEEE80211_MCS },
1578219456Sbschmidt		{  22, IFM_IEEE80211_MCS },
1579219456Sbschmidt		{  23, IFM_IEEE80211_MCS },
1580219456Sbschmidt		{  24, IFM_IEEE80211_MCS },
1581219456Sbschmidt		{  25, IFM_IEEE80211_MCS },
1582219456Sbschmidt		{  26, IFM_IEEE80211_MCS },
1583219456Sbschmidt		{  27, IFM_IEEE80211_MCS },
1584219456Sbschmidt		{  28, IFM_IEEE80211_MCS },
1585219456Sbschmidt		{  29, IFM_IEEE80211_MCS },
1586219456Sbschmidt		{  30, IFM_IEEE80211_MCS },
1587219456Sbschmidt		{  31, IFM_IEEE80211_MCS },
1588219456Sbschmidt		{  32, IFM_IEEE80211_MCS },
1589219456Sbschmidt		{  33, IFM_IEEE80211_MCS },
1590219456Sbschmidt		{  34, IFM_IEEE80211_MCS },
1591219456Sbschmidt		{  35, IFM_IEEE80211_MCS },
1592219456Sbschmidt		{  36, IFM_IEEE80211_MCS },
1593219456Sbschmidt		{  37, IFM_IEEE80211_MCS },
1594219456Sbschmidt		{  38, IFM_IEEE80211_MCS },
1595219456Sbschmidt		{  39, IFM_IEEE80211_MCS },
1596219456Sbschmidt		{  40, IFM_IEEE80211_MCS },
1597219456Sbschmidt		{  41, IFM_IEEE80211_MCS },
1598219456Sbschmidt		{  42, IFM_IEEE80211_MCS },
1599219456Sbschmidt		{  43, IFM_IEEE80211_MCS },
1600219456Sbschmidt		{  44, IFM_IEEE80211_MCS },
1601219456Sbschmidt		{  45, IFM_IEEE80211_MCS },
1602219456Sbschmidt		{  46, IFM_IEEE80211_MCS },
1603219456Sbschmidt		{  47, IFM_IEEE80211_MCS },
1604219456Sbschmidt		{  48, IFM_IEEE80211_MCS },
1605219456Sbschmidt		{  49, IFM_IEEE80211_MCS },
1606219456Sbschmidt		{  50, IFM_IEEE80211_MCS },
1607219456Sbschmidt		{  51, IFM_IEEE80211_MCS },
1608219456Sbschmidt		{  52, IFM_IEEE80211_MCS },
1609219456Sbschmidt		{  53, IFM_IEEE80211_MCS },
1610219456Sbschmidt		{  54, IFM_IEEE80211_MCS },
1611219456Sbschmidt		{  55, IFM_IEEE80211_MCS },
1612219456Sbschmidt		{  56, IFM_IEEE80211_MCS },
1613219456Sbschmidt		{  57, IFM_IEEE80211_MCS },
1614219456Sbschmidt		{  58, IFM_IEEE80211_MCS },
1615219456Sbschmidt		{  59, IFM_IEEE80211_MCS },
1616219456Sbschmidt		{  60, IFM_IEEE80211_MCS },
1617219456Sbschmidt		{  61, IFM_IEEE80211_MCS },
1618219456Sbschmidt		{  62, IFM_IEEE80211_MCS },
1619219456Sbschmidt		{  63, IFM_IEEE80211_MCS },
1620219456Sbschmidt		{  64, IFM_IEEE80211_MCS },
1621219456Sbschmidt		{  65, IFM_IEEE80211_MCS },
1622219456Sbschmidt		{  66, IFM_IEEE80211_MCS },
1623219456Sbschmidt		{  67, IFM_IEEE80211_MCS },
1624219456Sbschmidt		{  68, IFM_IEEE80211_MCS },
1625219456Sbschmidt		{  69, IFM_IEEE80211_MCS },
1626219456Sbschmidt		{  70, IFM_IEEE80211_MCS },
1627219456Sbschmidt		{  71, IFM_IEEE80211_MCS },
1628219456Sbschmidt		{  72, IFM_IEEE80211_MCS },
1629219456Sbschmidt		{  73, IFM_IEEE80211_MCS },
1630219456Sbschmidt		{  74, IFM_IEEE80211_MCS },
1631219456Sbschmidt		{  75, IFM_IEEE80211_MCS },
1632219456Sbschmidt		{  76, IFM_IEEE80211_MCS },
1633170530Ssam	};
1634170530Ssam	int m;
1635116742Ssam
1636170530Ssam	/*
1637170530Ssam	 * Check 11n rates first for match as an MCS.
1638170530Ssam	 */
1639170530Ssam	if (mode == IEEE80211_MODE_11NA) {
1640172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1641172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1642170530Ssam			m = findmedia(htrates, N(htrates), rate);
1643170530Ssam			if (m != IFM_AUTO)
1644170530Ssam				return m | IFM_IEEE80211_11NA;
1645170530Ssam		}
1646170530Ssam	} else if (mode == IEEE80211_MODE_11NG) {
1647170530Ssam		/* NB: 12 is ambiguous, it will be treated as an MCS */
1648172226Ssam		if (rate & IEEE80211_RATE_MCS) {
1649172226Ssam			rate &= ~IEEE80211_RATE_MCS;
1650170530Ssam			m = findmedia(htrates, N(htrates), rate);
1651170530Ssam			if (m != IFM_AUTO)
1652170530Ssam				return m | IFM_IEEE80211_11NG;
1653170530Ssam		}
1654170530Ssam	}
1655170530Ssam	rate &= IEEE80211_RATE_VAL;
1656116742Ssam	switch (mode) {
1657116742Ssam	case IEEE80211_MODE_11A:
1658188782Ssam	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
1659188782Ssam	case IEEE80211_MODE_QUARTER:
1660170530Ssam	case IEEE80211_MODE_11NA:
1661138568Ssam	case IEEE80211_MODE_TURBO_A:
1662170530Ssam	case IEEE80211_MODE_STURBO_A:
1663170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1664116742Ssam	case IEEE80211_MODE_11B:
1665170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1666124543Sonoe	case IEEE80211_MODE_FH:
1667170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1668116742Ssam	case IEEE80211_MODE_AUTO:
1669116742Ssam		/* NB: ic may be NULL for some drivers */
1670188775Ssam		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1671170530Ssam			return findmedia(rates, N(rates),
1672170530Ssam			    rate | IFM_IEEE80211_FH);
1673116742Ssam		/* NB: hack, 11g matches both 11b+11a rates */
1674116742Ssam		/* fall thru... */
1675116742Ssam	case IEEE80211_MODE_11G:
1676170530Ssam	case IEEE80211_MODE_11NG:
1677138568Ssam	case IEEE80211_MODE_TURBO_G:
1678170530Ssam		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1679116742Ssam	}
1680116742Ssam	return IFM_AUTO;
1681116742Ssam#undef N
1682116742Ssam}
1683116742Ssam
1684116742Ssamint
1685116742Ssamieee80211_media2rate(int mword)
1686116742Ssam{
1687116742Ssam#define	N(a)	(sizeof(a) / sizeof(a[0]))
1688116742Ssam	static const int ieeerates[] = {
1689116742Ssam		-1,		/* IFM_AUTO */
1690116742Ssam		0,		/* IFM_MANUAL */
1691116742Ssam		0,		/* IFM_NONE */
1692116742Ssam		2,		/* IFM_IEEE80211_FH1 */
1693116742Ssam		4,		/* IFM_IEEE80211_FH2 */
1694116742Ssam		2,		/* IFM_IEEE80211_DS1 */
1695116742Ssam		4,		/* IFM_IEEE80211_DS2 */
1696116742Ssam		11,		/* IFM_IEEE80211_DS5 */
1697116742Ssam		22,		/* IFM_IEEE80211_DS11 */
1698116742Ssam		44,		/* IFM_IEEE80211_DS22 */
1699116742Ssam		12,		/* IFM_IEEE80211_OFDM6 */
1700116742Ssam		18,		/* IFM_IEEE80211_OFDM9 */
1701116742Ssam		24,		/* IFM_IEEE80211_OFDM12 */
1702116742Ssam		36,		/* IFM_IEEE80211_OFDM18 */
1703116742Ssam		48,		/* IFM_IEEE80211_OFDM24 */
1704116742Ssam		72,		/* IFM_IEEE80211_OFDM36 */
1705116742Ssam		96,		/* IFM_IEEE80211_OFDM48 */
1706116742Ssam		108,		/* IFM_IEEE80211_OFDM54 */
1707116742Ssam		144,		/* IFM_IEEE80211_OFDM72 */
1708165569Ssam		0,		/* IFM_IEEE80211_DS354k */
1709165569Ssam		0,		/* IFM_IEEE80211_DS512k */
1710165569Ssam		6,		/* IFM_IEEE80211_OFDM3 */
1711165569Ssam		9,		/* IFM_IEEE80211_OFDM4 */
1712165569Ssam		54,		/* IFM_IEEE80211_OFDM27 */
1713170530Ssam		-1,		/* IFM_IEEE80211_MCS */
1714116742Ssam	};
1715116742Ssam	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1716116742Ssam		ieeerates[IFM_SUBTYPE(mword)] : 0;
1717116742Ssam#undef N
1718116742Ssam}
1719195379Ssam
1720195379Ssam/*
1721195379Ssam * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1722195379Ssam * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1723195379Ssam */
1724195379Ssam#define	mix(a, b, c)							\
1725195379Ssamdo {									\
1726195379Ssam	a -= b; a -= c; a ^= (c >> 13);					\
1727195379Ssam	b -= c; b -= a; b ^= (a << 8);					\
1728195379Ssam	c -= a; c -= b; c ^= (b >> 13);					\
1729195379Ssam	a -= b; a -= c; a ^= (c >> 12);					\
1730195379Ssam	b -= c; b -= a; b ^= (a << 16);					\
1731195379Ssam	c -= a; c -= b; c ^= (b >> 5);					\
1732195379Ssam	a -= b; a -= c; a ^= (c >> 3);					\
1733195379Ssam	b -= c; b -= a; b ^= (a << 10);					\
1734195379Ssam	c -= a; c -= b; c ^= (b >> 15);					\
1735195379Ssam} while (/*CONSTCOND*/0)
1736195379Ssam
1737195379Ssamuint32_t
1738195379Ssamieee80211_mac_hash(const struct ieee80211com *ic,
1739195379Ssam	const uint8_t addr[IEEE80211_ADDR_LEN])
1740195379Ssam{
1741195379Ssam	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1742195379Ssam
1743195379Ssam	b += addr[5] << 8;
1744195379Ssam	b += addr[4];
1745195379Ssam	a += addr[3] << 24;
1746195379Ssam	a += addr[2] << 16;
1747195379Ssam	a += addr[1] << 8;
1748195379Ssam	a += addr[0];
1749195379Ssam
1750195379Ssam	mix(a, b, c);
1751195379Ssam
1752195379Ssam	return c;
1753195379Ssam}
1754195379Ssam#undef mix
1755