ieee80211.c revision 172223
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211.c 172223 2007-09-18 20:30:40Z sam $");
29
30/*
31 * IEEE 802.11 generic handler
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37
38#include <sys/socket.h>
39
40#include <net/if.h>
41#include <net/if_media.h>
42#include <net/ethernet.h>
43
44#include <net80211/ieee80211_var.h>
45
46#include <net/bpf.h>
47
48const char *ieee80211_phymode_name[] = {
49	"auto",		/* IEEE80211_MODE_AUTO */
50	"11a",		/* IEEE80211_MODE_11A */
51	"11b",		/* IEEE80211_MODE_11B */
52	"11g",		/* IEEE80211_MODE_11G */
53	"FH",		/* IEEE80211_MODE_FH */
54	"turboA",	/* IEEE80211_MODE_TURBO_A */
55	"turboG",	/* IEEE80211_MODE_TURBO_G */
56	"sturboA",	/* IEEE80211_MODE_STURBO_A */
57	"11na",		/* IEEE80211_MODE_11NA */
58	"11ng",		/* IEEE80211_MODE_11NG */
59};
60
61/*
62 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
63 */
64#define	B(r)	((r) | IEEE80211_RATE_BASIC)
65static const struct ieee80211_rateset ieee80211_rateset_11a =
66	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
67static const struct ieee80211_rateset ieee80211_rateset_half =
68	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
69static const struct ieee80211_rateset ieee80211_rateset_quarter =
70	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
71static const struct ieee80211_rateset ieee80211_rateset_11b =
72	{ 4, { B(2), B(4), B(11), B(22) } };
73/* NB: OFDM rates are handled specially based on mode */
74static const struct ieee80211_rateset ieee80211_rateset_11g =
75	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
76#undef B
77
78static	int media_status(enum ieee80211_opmode ,
79		const struct ieee80211_channel *);
80
81/* list of all instances */
82SLIST_HEAD(ieee80211_list, ieee80211com);
83static struct ieee80211_list ieee80211_list =
84	SLIST_HEAD_INITIALIZER(ieee80211_list);
85static uint8_t ieee80211_vapmap[32];		/* enough for 256 */
86static struct mtx ieee80211_vap_mtx;
87MTX_SYSINIT(ieee80211, &ieee80211_vap_mtx, "net80211 instances", MTX_DEF);
88
89static void
90ieee80211_add_vap(struct ieee80211com *ic)
91{
92#define	N(a)	(sizeof(a)/sizeof(a[0]))
93	int i;
94	uint8_t b;
95
96	mtx_lock(&ieee80211_vap_mtx);
97	ic->ic_vap = 0;
98	for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
99		ic->ic_vap += NBBY;
100	if (i == N(ieee80211_vapmap))
101		panic("vap table full");
102	for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
103		ic->ic_vap++;
104	setbit(ieee80211_vapmap, ic->ic_vap);
105	SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
106	mtx_unlock(&ieee80211_vap_mtx);
107#undef N
108}
109
110static void
111ieee80211_remove_vap(struct ieee80211com *ic)
112{
113	mtx_lock(&ieee80211_vap_mtx);
114	SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
115	KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
116		("invalid vap id %d", ic->ic_vap));
117	KASSERT(isset(ieee80211_vapmap, ic->ic_vap),
118		("vap id %d not allocated", ic->ic_vap));
119	clrbit(ieee80211_vapmap, ic->ic_vap);
120	mtx_unlock(&ieee80211_vap_mtx);
121}
122
123/*
124 * Default reset method for use with the ioctl support.  This
125 * method is invoked after any state change in the 802.11
126 * layer that should be propagated to the hardware but not
127 * require re-initialization of the 802.11 state machine (e.g
128 * rescanning for an ap).  We always return ENETRESET which
129 * should cause the driver to re-initialize the device. Drivers
130 * can override this method to implement more optimized support.
131 */
132static int
133ieee80211_default_reset(struct ifnet *ifp)
134{
135	return ENETRESET;
136}
137
138/*
139 * Fill in 802.11 available channel set, mark
140 * all available channels as active, and pick
141 * a default channel if not already specified.
142 */
143static void
144ieee80211_chan_init(struct ieee80211com *ic)
145{
146#define	DEFAULTRATES(m, def) do { \
147	if (isset(ic->ic_modecaps, m) && ic->ic_sup_rates[m].rs_nrates == 0) \
148		ic->ic_sup_rates[m] = def; \
149} while (0)
150	struct ieee80211_channel *c;
151	int i;
152
153	KASSERT(0 < ic->ic_nchans && ic->ic_nchans < IEEE80211_CHAN_MAX,
154		("invalid number of channels specified: %u", ic->ic_nchans));
155	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
156	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
157	for (i = 0; i < ic->ic_nchans; i++) {
158		c = &ic->ic_channels[i];
159		KASSERT(c->ic_flags != 0, ("channel with no flags"));
160		KASSERT(c->ic_ieee < IEEE80211_CHAN_MAX,
161			("channel with bogus ieee number %u", c->ic_ieee));
162		setbit(ic->ic_chan_avail, c->ic_ieee);
163		/*
164		 * Identify mode capabilities.
165		 */
166		if (IEEE80211_IS_CHAN_A(c))
167			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
168		if (IEEE80211_IS_CHAN_B(c))
169			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
170		if (IEEE80211_IS_CHAN_ANYG(c))
171			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
172		if (IEEE80211_IS_CHAN_FHSS(c))
173			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
174		if (IEEE80211_IS_CHAN_108A(c))
175			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
176		if (IEEE80211_IS_CHAN_108G(c))
177			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
178		if (IEEE80211_IS_CHAN_ST(c))
179			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
180		if (IEEE80211_IS_CHAN_HTA(c))
181			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
182		if (IEEE80211_IS_CHAN_HTG(c))
183			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
184	}
185	/* initialize candidate channels to all available */
186	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
187		sizeof(ic->ic_chan_avail));
188
189	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
190	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
191	/* arbitrarily pick the first channel */
192	ic->ic_curchan = &ic->ic_channels[0];
193
194	/* fillin well-known rate sets if driver has not specified */
195	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
196	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
197	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
198	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
199	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
200
201	/*
202	 * Set auto mode to reset active channel state and any desired channel.
203	 */
204	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
205#undef DEFAULTRATES
206}
207
208void
209ieee80211_ifattach(struct ieee80211com *ic)
210{
211	struct ifnet *ifp = ic->ic_ifp;
212
213	ether_ifattach(ifp, ic->ic_myaddr);
214	ifp->if_output = ieee80211_output;
215
216	bpfattach2(ifp, DLT_IEEE802_11,
217	    sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
218
219	/* override the 802.3 setting */
220	ifp->if_hdrlen = ic->ic_headroom
221		+ sizeof(struct ieee80211_qosframe_addr4)
222		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
223		+ IEEE80211_WEP_EXTIVLEN;
224	/* XXX no way to recalculate on ifdetach */
225	if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
226		/* XXX sanity check... */
227		max_linkhdr = ALIGN(ifp->if_hdrlen);
228		max_hdr = max_linkhdr + max_protohdr;
229		max_datalen = MHLEN - max_hdr;
230	}
231
232	/*
233	 * Fill in 802.11 available channel set, mark all
234	 * available channels as active, and pick a default
235	 * channel if not already specified.
236	 */
237	ieee80211_chan_init(ic);
238
239	if (ic->ic_caps & IEEE80211_C_BGSCAN)	/* enable if capable */
240		ic->ic_flags |= IEEE80211_F_BGSCAN;
241#if 0
242	/* XXX not until WME+WPA issues resolved */
243	if (ic->ic_caps & IEEE80211_C_WME)	/* enable if capable */
244		ic->ic_flags |= IEEE80211_F_WME;
245#endif
246	if (ic->ic_caps & IEEE80211_C_BURST)
247		ic->ic_flags |= IEEE80211_F_BURST;
248	ic->ic_flags |= IEEE80211_F_DOTH;	/* XXX out of caps, just ena */
249
250	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
251	ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
252	ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
253	IEEE80211_LOCK_INIT(ic, "ieee80211com");
254	IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
255
256	ic->ic_lintval = ic->ic_bintval;
257	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
258
259	ieee80211_crypto_attach(ic);
260	ieee80211_node_attach(ic);
261	ieee80211_power_attach(ic);
262	ieee80211_proto_attach(ic);
263	ieee80211_ht_attach(ic);
264	ieee80211_scan_attach(ic);
265
266	ieee80211_add_vap(ic);
267
268	ieee80211_sysctl_attach(ic);		/* NB: requires ic_vap */
269
270	/*
271	 * Install a default reset method for the ioctl support.
272	 * The driver is expected to fill this in before calling us.
273	 */
274	if (ic->ic_reset == NULL)
275		ic->ic_reset = ieee80211_default_reset;
276
277	KASSERT(ifp->if_spare2 == NULL, ("oops, hosed"));
278	ifp->if_spare2 = ic;			/* XXX temp backpointer */
279}
280
281void
282ieee80211_ifdetach(struct ieee80211com *ic)
283{
284	struct ifnet *ifp = ic->ic_ifp;
285
286	ieee80211_remove_vap(ic);
287
288	ieee80211_sysctl_detach(ic);
289	ieee80211_scan_detach(ic);
290	ieee80211_ht_detach(ic);
291	/* NB: must be called before ieee80211_node_detach */
292	ieee80211_proto_detach(ic);
293	ieee80211_crypto_detach(ic);
294	ieee80211_power_detach(ic);
295	ieee80211_node_detach(ic);
296	ifmedia_removeall(&ic->ic_media);
297
298	IEEE80211_LOCK_DESTROY(ic);
299	IEEE80211_BEACON_LOCK_DESTROY(ic);
300
301	bpfdetach(ifp);
302	ether_ifdetach(ifp);
303}
304
305static __inline int
306mapgsm(u_int freq, u_int flags)
307{
308	freq *= 10;
309	if (flags & IEEE80211_CHAN_QUARTER)
310		freq += 5;
311	else if (flags & IEEE80211_CHAN_HALF)
312		freq += 10;
313	else
314		freq += 20;
315	/* NB: there is no 907/20 wide but leave room */
316	return (freq - 906*10) / 5;
317}
318
319static __inline int
320mappsb(u_int freq, u_int flags)
321{
322	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
323}
324
325/*
326 * Convert MHz frequency to IEEE channel number.
327 */
328int
329ieee80211_mhz2ieee(u_int freq, u_int flags)
330{
331#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
332	if (flags & IEEE80211_CHAN_GSM)
333		return mapgsm(freq, flags);
334	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
335		if (freq == 2484)
336			return 14;
337		if (freq < 2484)
338			return ((int) freq - 2407) / 5;
339		else
340			return 15 + ((freq - 2512) / 20);
341	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
342		if (freq <= 5000) {
343			/* XXX check regdomain? */
344			if (IS_FREQ_IN_PSB(freq))
345				return mappsb(freq, flags);
346			return (freq - 4000) / 5;
347		} else
348			return (freq - 5000) / 5;
349	} else {				/* either, guess */
350		if (freq == 2484)
351			return 14;
352		if (freq < 2484) {
353			if (907 <= freq && freq <= 922)
354				return mapgsm(freq, flags);
355			return ((int) freq - 2407) / 5;
356		}
357		if (freq < 5000) {
358			if (IS_FREQ_IN_PSB(freq))
359				return mappsb(freq, flags);
360			else if (freq > 4900)
361				return (freq - 4000) / 5;
362			else
363				return 15 + ((freq - 2512) / 20);
364		}
365		return (freq - 5000) / 5;
366	}
367#undef IS_FREQ_IN_PSB
368}
369
370/*
371 * Convert channel to IEEE channel number.
372 */
373int
374ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
375{
376	if (c == NULL) {
377		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
378		return 0;		/* XXX */
379	}
380	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
381}
382
383/*
384 * Convert IEEE channel number to MHz frequency.
385 */
386u_int
387ieee80211_ieee2mhz(u_int chan, u_int flags)
388{
389	if (flags & IEEE80211_CHAN_GSM)
390		return 907 + 5 * (chan / 10);
391	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
392		if (chan == 14)
393			return 2484;
394		if (chan < 14)
395			return 2407 + chan*5;
396		else
397			return 2512 + ((chan-15)*20);
398	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
399		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
400			chan -= 37;
401			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
402		}
403		return 5000 + (chan*5);
404	} else {				/* either, guess */
405		/* XXX can't distinguish PSB+GSM channels */
406		if (chan == 14)
407			return 2484;
408		if (chan < 14)			/* 0-13 */
409			return 2407 + chan*5;
410		if (chan < 27)			/* 15-26 */
411			return 2512 + ((chan-15)*20);
412		return 5000 + (chan*5);
413	}
414}
415
416/*
417 * Locate a channel given a frequency+flags.  We cache
418 * the previous lookup to optimize swithing between two
419 * channels--as happens with dynamic turbo.
420 */
421struct ieee80211_channel *
422ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
423{
424	struct ieee80211_channel *c;
425	int i;
426
427	flags &= IEEE80211_CHAN_ALLTURBO;
428	c = ic->ic_prevchan;
429	if (c != NULL && c->ic_freq == freq &&
430	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
431		return c;
432	/* brute force search */
433	for (i = 0; i < ic->ic_nchans; i++) {
434		c = &ic->ic_channels[i];
435		if (c->ic_freq == freq &&
436		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
437			return c;
438	}
439	return NULL;
440}
441
442static void
443addmedia(struct ieee80211com *ic, int mode, int mword)
444{
445#define	TURBO(m)	((m) | IFM_IEEE80211_TURBO)
446#define	ADD(_ic, _s, _o) \
447	ifmedia_add(&(_ic)->ic_media, \
448		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
449	static const u_int mopts[IEEE80211_MODE_MAX] = {
450		IFM_AUTO,			/* IEEE80211_MODE_AUTO */
451		IFM_IEEE80211_11A,		/* IEEE80211_MODE_11A */
452		IFM_IEEE80211_11B,		/* IEEE80211_MODE_11B */
453		IFM_IEEE80211_11G,		/* IEEE80211_MODE_11G */
454		IFM_IEEE80211_FH,		/* IEEE80211_MODE_FH */
455		TURBO(IFM_IEEE80211_11A),	/* IEEE80211_MODE_TURBO_A */
456		TURBO(IFM_IEEE80211_11G),	/* IEEE80211_MODE_TURBO_G */
457		TURBO(IFM_IEEE80211_11A),	/* IEEE80211_MODE_STURBO_A */
458		IFM_IEEE80211_11NA,		/* IEEE80211_MODE_11NA */
459		IFM_IEEE80211_11NG,		/* IEEE80211_MODE_11NG */
460	};
461	u_int mopt;
462
463	KASSERT(mode < IEEE80211_MODE_MAX, ("bad mode %u", mode));
464	mopt = mopts[mode];
465	KASSERT(mopt != 0 || mode == IEEE80211_MODE_AUTO,
466	    ("no media mapping for mode %u", mode));
467
468	ADD(ic, mword, mopt);	/* e.g. 11a auto */
469	if (ic->ic_caps & IEEE80211_C_IBSS)
470		ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
471	if (ic->ic_caps & IEEE80211_C_HOSTAP)
472		ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
473	if (ic->ic_caps & IEEE80211_C_AHDEMO)
474		ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
475	if (ic->ic_caps & IEEE80211_C_MONITOR)
476		ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
477#undef ADD
478#undef TURBO
479}
480
481/*
482 * Setup the media data structures according to the channel and
483 * rate tables.  This must be called by the driver after
484 * ieee80211_attach and before most anything else.
485 */
486void
487ieee80211_media_init(struct ieee80211com *ic,
488	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
489{
490	struct ifnet *ifp = ic->ic_ifp;
491	int i, j, mode, rate, maxrate, mword, r;
492	const struct ieee80211_rateset *rs;
493	struct ieee80211_rateset allrates;
494
495	/* NB: this works because the structure is initialized to zero */
496	if (LIST_EMPTY(&ic->ic_media.ifm_list)) {
497		/*
498		 * Do late attach work that must wait for any subclass
499		 * (i.e. driver) work such as overriding methods.
500		 */
501		ieee80211_node_lateattach(ic);
502	} else {
503		/*
504		 * We are re-initializing the channel list; clear
505		 * the existing media state as the media routines
506		 * don't suppress duplicates.
507		 */
508		ifmedia_removeall(&ic->ic_media);
509		ieee80211_chan_init(ic);
510	}
511	ieee80211_power_lateattach(ic);
512
513	/*
514	 * Fill in media characteristics.
515	 */
516	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
517	maxrate = 0;
518	/*
519	 * Add media for legacy operating modes.
520	 */
521	memset(&allrates, 0, sizeof(allrates));
522	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
523		if (isclr(ic->ic_modecaps, mode))
524			continue;
525		addmedia(ic, mode, IFM_AUTO);
526		if (mode == IEEE80211_MODE_AUTO)
527			continue;
528		rs = &ic->ic_sup_rates[mode];
529		for (i = 0; i < rs->rs_nrates; i++) {
530			rate = rs->rs_rates[i];
531			mword = ieee80211_rate2media(ic, rate, mode);
532			if (mword == 0)
533				continue;
534			addmedia(ic, mode, mword);
535			/*
536			 * Add legacy rate to the collection of all rates.
537			 */
538			r = rate & IEEE80211_RATE_VAL;
539			for (j = 0; j < allrates.rs_nrates; j++)
540				if (allrates.rs_rates[j] == r)
541					break;
542			if (j == allrates.rs_nrates) {
543				/* unique, add to the set */
544				allrates.rs_rates[j] = r;
545				allrates.rs_nrates++;
546			}
547			rate = (rate & IEEE80211_RATE_VAL) / 2;
548			if (rate > maxrate)
549				maxrate = rate;
550		}
551	}
552	for (i = 0; i < allrates.rs_nrates; i++) {
553		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
554				IEEE80211_MODE_AUTO);
555		if (mword == 0)
556			continue;
557		/* NB: remove media options from mword */
558		addmedia(ic, IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
559	}
560	/*
561	 * Add HT/11n media.  Note that we do not have enough
562	 * bits in the media subtype to express the MCS so we
563	 * use a "placeholder" media subtype and any fixed MCS
564	 * must be specified with a different mechanism.
565	 */
566	for (; mode < IEEE80211_MODE_MAX; mode++) {
567		if (isclr(ic->ic_modecaps, mode))
568			continue;
569		addmedia(ic, mode, IFM_AUTO);
570		addmedia(ic, mode, IFM_IEEE80211_MCS);
571	}
572	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
573	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
574		addmedia(ic, IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
575		/* XXX could walk htrates */
576		/* XXX known array size */
577		if (ieee80211_htrates[15] > maxrate)
578			maxrate = ieee80211_htrates[15];
579	}
580
581	/* NB: strip explicit mode; we're actually in autoselect */
582	ifmedia_set(&ic->ic_media,
583		media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK);
584
585	if (maxrate)
586		ifp->if_baudrate = IF_Mbps(maxrate);
587}
588
589const struct ieee80211_rateset *
590ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
591{
592	if (IEEE80211_IS_CHAN_HALF(c))
593		return &ieee80211_rateset_half;
594	if (IEEE80211_IS_CHAN_QUARTER(c))
595		return &ieee80211_rateset_quarter;
596	if (IEEE80211_IS_CHAN_HTA(c))
597		return &ic->ic_sup_rates[IEEE80211_MODE_11A];
598	if (IEEE80211_IS_CHAN_HTG(c)) {
599		/* XXX does this work for basic rates? */
600		return &ic->ic_sup_rates[IEEE80211_MODE_11G];
601	}
602	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
603}
604
605void
606ieee80211_announce(struct ieee80211com *ic)
607{
608	struct ifnet *ifp = ic->ic_ifp;
609	int i, mode, rate, mword;
610	const struct ieee80211_rateset *rs;
611
612	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
613		if (isclr(ic->ic_modecaps, mode))
614			continue;
615		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
616		rs = &ic->ic_sup_rates[mode];
617		for (i = 0; i < rs->rs_nrates; i++) {
618			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
619			if (mword == 0)
620				continue;
621			rate = ieee80211_media2rate(mword);
622			printf("%s%d%sMbps", (i != 0 ? " " : ""),
623			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
624		}
625		printf("\n");
626	}
627	ieee80211_ht_announce(ic);
628}
629
630void
631ieee80211_announce_channels(struct ieee80211com *ic)
632{
633	const struct ieee80211_channel *c;
634	char type;
635	int i, cw;
636
637	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
638	for (i = 0; i < ic->ic_nchans; i++) {
639		c = &ic->ic_channels[i];
640		if (IEEE80211_IS_CHAN_ST(c))
641			type = 'S';
642		else if (IEEE80211_IS_CHAN_108A(c))
643			type = 'T';
644		else if (IEEE80211_IS_CHAN_108G(c))
645			type = 'G';
646		else if (IEEE80211_IS_CHAN_HT(c))
647			type = 'n';
648		else if (IEEE80211_IS_CHAN_A(c))
649			type = 'a';
650		else if (IEEE80211_IS_CHAN_ANYG(c))
651			type = 'g';
652		else if (IEEE80211_IS_CHAN_B(c))
653			type = 'b';
654		else
655			type = 'f';
656		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
657			cw = 40;
658		else if (IEEE80211_IS_CHAN_HALF(c))
659			cw = 10;
660		else if (IEEE80211_IS_CHAN_QUARTER(c))
661			cw = 5;
662		else
663			cw = 20;
664		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
665			, c->ic_ieee, c->ic_freq, type
666			, cw
667			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
668			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
669			, c->ic_maxregpower
670			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
671			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
672		);
673	}
674}
675
676/*
677 * Find an instance by it's mac address.
678 */
679struct ieee80211com *
680ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN])
681{
682	struct ieee80211com *ic;
683
684	/* XXX lock */
685	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
686		if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
687			return ic;
688	return NULL;
689}
690
691static struct ieee80211com *
692ieee80211_find_instance(struct ifnet *ifp)
693{
694	struct ieee80211com *ic;
695
696	/* XXX lock */
697	/* XXX not right for multiple instances but works for now */
698	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
699		if (ic->ic_ifp == ifp)
700			return ic;
701	return NULL;
702}
703
704static int
705findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
706{
707#define	IEEERATE(_ic,_m,_i) \
708	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
709	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
710	for (i = 0; i < nrates; i++)
711		if (IEEERATE(ic, mode, i) == rate)
712			return i;
713	return -1;
714#undef IEEERATE
715}
716
717/*
718 * Convert a media specification to a rate index and possibly a mode
719 * (if the rate is fixed and the mode is specified as ``auto'' then
720 * we need to lock down the mode so the index is meanginful).
721 */
722static int
723checkrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
724{
725
726	/*
727	 * Check the rate table for the specified/current phy.
728	 */
729	if (mode == IEEE80211_MODE_AUTO) {
730		int i;
731		/*
732		 * In autoselect mode search for the rate.
733		 */
734		for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
735			if (isset(ic->ic_modecaps, i) &&
736			    findrate(ic, i, rate) != -1)
737				return 1;
738		}
739		return 0;
740	} else {
741		/*
742		 * Mode is fixed, check for rate.
743		 */
744		return (findrate(ic, mode, rate) != -1);
745	}
746}
747
748/*
749 * Handle a media change request.
750 */
751int
752ieee80211_media_change(struct ifnet *ifp)
753{
754	struct ieee80211com *ic;
755	struct ifmedia_entry *ime;
756	enum ieee80211_opmode newopmode;
757	enum ieee80211_phymode newphymode;
758	int newrate, error = 0;
759
760	ic = ieee80211_find_instance(ifp);
761	if (!ic) {
762		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
763		return EINVAL;
764	}
765	ime = ic->ic_media.ifm_cur;
766	/*
767	 * First, identify the phy mode.
768	 */
769	switch (IFM_MODE(ime->ifm_media)) {
770	case IFM_IEEE80211_11A:
771		newphymode = IEEE80211_MODE_11A;
772		break;
773	case IFM_IEEE80211_11B:
774		newphymode = IEEE80211_MODE_11B;
775		break;
776	case IFM_IEEE80211_11G:
777		newphymode = IEEE80211_MODE_11G;
778		break;
779	case IFM_IEEE80211_FH:
780		newphymode = IEEE80211_MODE_FH;
781		break;
782	case IFM_IEEE80211_11NA:
783		newphymode = IEEE80211_MODE_11NA;
784		break;
785	case IFM_IEEE80211_11NG:
786		newphymode = IEEE80211_MODE_11NG;
787		break;
788	case IFM_AUTO:
789		newphymode = IEEE80211_MODE_AUTO;
790		break;
791	default:
792		return EINVAL;
793	}
794	/*
795	 * Turbo mode is an ``option''.
796	 * XXX does not apply to AUTO
797	 */
798	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
799		if (newphymode == IEEE80211_MODE_11A) {
800			if (ic->ic_flags & IEEE80211_F_TURBOP)
801				newphymode = IEEE80211_MODE_TURBO_A;
802			else
803				newphymode = IEEE80211_MODE_STURBO_A;
804		} else if (newphymode == IEEE80211_MODE_11G)
805			newphymode = IEEE80211_MODE_TURBO_G;
806		else
807			return EINVAL;
808	}
809	/* XXX HT40 +/- */
810	/*
811	 * Next, the fixed/variable rate.
812	 */
813	newrate = ic->ic_fixed_rate;
814	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
815		/*
816		 * Convert media subtype to rate.
817		 */
818		newrate = ieee80211_media2rate(ime->ifm_media);
819		if (newrate == 0 || !checkrate(ic, newphymode, newrate))
820			return EINVAL;
821	} else
822		newrate = IEEE80211_FIXED_RATE_NONE;
823
824	/*
825	 * Deduce new operating mode but don't install it just yet.
826	 */
827	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
828	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
829		newopmode = IEEE80211_M_AHDEMO;
830	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
831		newopmode = IEEE80211_M_HOSTAP;
832	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
833		newopmode = IEEE80211_M_IBSS;
834	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
835		newopmode = IEEE80211_M_MONITOR;
836	else
837		newopmode = IEEE80211_M_STA;
838
839	/*
840	 * Handle phy mode change.
841	 */
842	if (ic->ic_des_mode != newphymode) {		/* change phy mode */
843		ic->ic_des_mode = newphymode;
844		error = ENETRESET;
845	}
846
847	/*
848	 * Committed to changes, install the rate setting.
849	 */
850	if (ic->ic_fixed_rate != newrate) {
851		ic->ic_fixed_rate = newrate;		/* set fixed tx rate */
852		error = ENETRESET;
853	}
854
855	/*
856	 * Handle operating mode change.
857	 */
858	if (ic->ic_opmode != newopmode) {
859		ic->ic_opmode = newopmode;
860		switch (newopmode) {
861		case IEEE80211_M_AHDEMO:
862		case IEEE80211_M_HOSTAP:
863		case IEEE80211_M_STA:
864		case IEEE80211_M_MONITOR:
865		case IEEE80211_M_WDS:
866			ic->ic_flags &= ~IEEE80211_F_IBSSON;
867			break;
868		case IEEE80211_M_IBSS:
869			ic->ic_flags |= IEEE80211_F_IBSSON;
870			break;
871		}
872		/*
873		 * Yech, slot time may change depending on the
874		 * operating mode so reset it to be sure everything
875		 * is setup appropriately.
876		 */
877		ieee80211_reset_erp(ic);
878		ieee80211_wme_initparams(ic);	/* after opmode change */
879		error = ENETRESET;
880	}
881#ifdef notdef
882	if (error == 0)
883		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
884#endif
885	return error;
886}
887
888/*
889 * Common code to calculate the media status word
890 * from the operating mode and channel state.
891 */
892static int
893media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
894{
895	int status;
896
897	status = IFM_IEEE80211;
898	switch (opmode) {
899	case IEEE80211_M_STA:
900		break;
901	case IEEE80211_M_IBSS:
902		status |= IFM_IEEE80211_ADHOC;
903		break;
904	case IEEE80211_M_HOSTAP:
905		status |= IFM_IEEE80211_HOSTAP;
906		break;
907	case IEEE80211_M_MONITOR:
908		status |= IFM_IEEE80211_MONITOR;
909		break;
910	case IEEE80211_M_AHDEMO:
911		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
912		break;
913	case IEEE80211_M_WDS:
914		/* should not come here */
915		break;
916	}
917	if (IEEE80211_IS_CHAN_HTA(chan)) {
918		status |= IFM_IEEE80211_11NA;
919	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
920		status |= IFM_IEEE80211_11NG;
921	} else if (IEEE80211_IS_CHAN_A(chan)) {
922		status |= IFM_IEEE80211_11A;
923	} else if (IEEE80211_IS_CHAN_B(chan)) {
924		status |= IFM_IEEE80211_11B;
925	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
926		status |= IFM_IEEE80211_11G;
927	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
928		status |= IFM_IEEE80211_FH;
929	}
930	/* XXX else complain? */
931
932	if (IEEE80211_IS_CHAN_TURBO(chan))
933		status |= IFM_IEEE80211_TURBO;
934
935	return status;
936}
937
938void
939ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
940{
941	struct ieee80211com *ic;
942	enum ieee80211_phymode mode;
943	const struct ieee80211_rateset *rs;
944
945	ic = ieee80211_find_instance(ifp);
946	if (!ic) {
947		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
948		return;
949	}
950	imr->ifm_status = IFM_AVALID;
951	/*
952	 * NB: use the current channel's mode to lock down a xmit
953	 * rate only when running; otherwise we may have a mismatch
954	 * in which case the rate will not be convertible.
955	 */
956	if (ic->ic_state == IEEE80211_S_RUN) {
957		imr->ifm_status |= IFM_ACTIVE;
958		mode = ieee80211_chan2mode(ic->ic_curchan);
959	} else
960		mode = IEEE80211_MODE_AUTO;
961	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
962	/*
963	 * Calculate a current rate if possible.
964	 */
965	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
966		/*
967		 * A fixed rate is set, report that.
968		 */
969		imr->ifm_active |= ieee80211_rate2media(ic,
970			ic->ic_fixed_rate, mode);
971	} else if (ic->ic_opmode == IEEE80211_M_STA) {
972		/*
973		 * In station mode report the current transmit rate.
974		 * XXX HT rate
975		 */
976		rs = &ic->ic_bss->ni_rates;
977		imr->ifm_active |= ieee80211_rate2media(ic,
978			rs->rs_rates[ic->ic_bss->ni_txrate], mode);
979	} else
980		imr->ifm_active |= IFM_AUTO;
981}
982
983/*
984 * Set the current phy mode and recalculate the active channel
985 * set based on the available channels for this mode.  Also
986 * select a new default/current channel if the current one is
987 * inappropriate for this mode.
988 */
989int
990ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
991{
992	/*
993	 * Adjust basic rates in 11b/11g supported rate set.
994	 * Note that if operating on a hal/quarter rate channel
995	 * this is a noop as those rates sets are different
996	 * and used instead.
997	 */
998	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
999		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], mode);
1000
1001	ic->ic_curmode = mode;
1002	ieee80211_reset_erp(ic);	/* reset ERP state */
1003	ieee80211_wme_initparams(ic);	/* reset WME stat */
1004
1005	return 0;
1006}
1007
1008/*
1009 * Return the phy mode for with the specified channel.
1010 */
1011enum ieee80211_phymode
1012ieee80211_chan2mode(const struct ieee80211_channel *chan)
1013{
1014
1015	if (IEEE80211_IS_CHAN_HTA(chan))
1016		return IEEE80211_MODE_11NA;
1017	else if (IEEE80211_IS_CHAN_HTG(chan))
1018		return IEEE80211_MODE_11NG;
1019	else if (IEEE80211_IS_CHAN_108G(chan))
1020		return IEEE80211_MODE_TURBO_G;
1021	else if (IEEE80211_IS_CHAN_ST(chan))
1022		return IEEE80211_MODE_STURBO_A;
1023	else if (IEEE80211_IS_CHAN_TURBO(chan))
1024		return IEEE80211_MODE_TURBO_A;
1025	else if (IEEE80211_IS_CHAN_A(chan))
1026		return IEEE80211_MODE_11A;
1027	else if (IEEE80211_IS_CHAN_ANYG(chan))
1028		return IEEE80211_MODE_11G;
1029	else if (IEEE80211_IS_CHAN_B(chan))
1030		return IEEE80211_MODE_11B;
1031	else if (IEEE80211_IS_CHAN_FHSS(chan))
1032		return IEEE80211_MODE_FH;
1033
1034	/* NB: should not get here */
1035	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1036		__func__, chan->ic_freq, chan->ic_flags);
1037	return IEEE80211_MODE_11B;
1038}
1039
1040struct ratemedia {
1041	u_int	match;	/* rate + mode */
1042	u_int	media;	/* if_media rate */
1043};
1044
1045static int
1046findmedia(const struct ratemedia rates[], int n, u_int match)
1047{
1048	int i;
1049
1050	for (i = 0; i < n; i++)
1051		if (rates[i].match == match)
1052			return rates[i].media;
1053	return IFM_AUTO;
1054}
1055
1056/*
1057 * Convert IEEE80211 rate value to ifmedia subtype.
1058 * Rate is either a legacy rate in units of 0.5Mbps
1059 * or an MCS index.
1060 */
1061int
1062ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1063{
1064#define	N(a)	(sizeof(a) / sizeof(a[0]))
1065	static const struct ratemedia rates[] = {
1066		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1067		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1068		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1069		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1070		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1071		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1072		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1073		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1074		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1075		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1076		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1077		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1078		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1079		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1080		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1081		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1082		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1083		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1084		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1085		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1086		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1087		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1088		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1089		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1090		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1091		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1092		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1093		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1094		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1095		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1096		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1097	};
1098	static const struct ratemedia htrates[] = {
1099		{   0, IFM_IEEE80211_MCS },
1100		{   1, IFM_IEEE80211_MCS },
1101		{   2, IFM_IEEE80211_MCS },
1102		{   3, IFM_IEEE80211_MCS },
1103		{   4, IFM_IEEE80211_MCS },
1104		{   5, IFM_IEEE80211_MCS },
1105		{   6, IFM_IEEE80211_MCS },
1106		{   7, IFM_IEEE80211_MCS },
1107		{   8, IFM_IEEE80211_MCS },
1108		{   9, IFM_IEEE80211_MCS },
1109		{  10, IFM_IEEE80211_MCS },
1110		{  11, IFM_IEEE80211_MCS },
1111		{  12, IFM_IEEE80211_MCS },
1112		{  13, IFM_IEEE80211_MCS },
1113		{  14, IFM_IEEE80211_MCS },
1114		{  15, IFM_IEEE80211_MCS },
1115	};
1116	int m;
1117
1118	/*
1119	 * Check 11n rates first for match as an MCS.
1120	 */
1121	if (mode == IEEE80211_MODE_11NA) {
1122		if ((rate & IEEE80211_RATE_BASIC) == 0) {
1123			m = findmedia(htrates, N(htrates), rate);
1124			if (m != IFM_AUTO)
1125				return m | IFM_IEEE80211_11NA;
1126		}
1127	} else if (mode == IEEE80211_MODE_11NG) {
1128		/* NB: 12 is ambiguous, it will be treated as an MCS */
1129		if ((rate & IEEE80211_RATE_BASIC) == 0) {
1130			m = findmedia(htrates, N(htrates), rate);
1131			if (m != IFM_AUTO)
1132				return m | IFM_IEEE80211_11NG;
1133		}
1134	}
1135	rate &= IEEE80211_RATE_VAL;
1136	switch (mode) {
1137	case IEEE80211_MODE_11A:
1138	case IEEE80211_MODE_11NA:
1139	case IEEE80211_MODE_TURBO_A:
1140	case IEEE80211_MODE_STURBO_A:
1141		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1142	case IEEE80211_MODE_11B:
1143		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1144	case IEEE80211_MODE_FH:
1145		return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1146	case IEEE80211_MODE_AUTO:
1147		/* NB: ic may be NULL for some drivers */
1148		if (ic && ic->ic_phytype == IEEE80211_T_FH)
1149			return findmedia(rates, N(rates),
1150			    rate | IFM_IEEE80211_FH);
1151		/* NB: hack, 11g matches both 11b+11a rates */
1152		/* fall thru... */
1153	case IEEE80211_MODE_11G:
1154	case IEEE80211_MODE_11NG:
1155	case IEEE80211_MODE_TURBO_G:
1156		return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1157	}
1158	return IFM_AUTO;
1159#undef N
1160}
1161
1162int
1163ieee80211_media2rate(int mword)
1164{
1165#define	N(a)	(sizeof(a) / sizeof(a[0]))
1166	static const int ieeerates[] = {
1167		-1,		/* IFM_AUTO */
1168		0,		/* IFM_MANUAL */
1169		0,		/* IFM_NONE */
1170		2,		/* IFM_IEEE80211_FH1 */
1171		4,		/* IFM_IEEE80211_FH2 */
1172		2,		/* IFM_IEEE80211_DS1 */
1173		4,		/* IFM_IEEE80211_DS2 */
1174		11,		/* IFM_IEEE80211_DS5 */
1175		22,		/* IFM_IEEE80211_DS11 */
1176		44,		/* IFM_IEEE80211_DS22 */
1177		12,		/* IFM_IEEE80211_OFDM6 */
1178		18,		/* IFM_IEEE80211_OFDM9 */
1179		24,		/* IFM_IEEE80211_OFDM12 */
1180		36,		/* IFM_IEEE80211_OFDM18 */
1181		48,		/* IFM_IEEE80211_OFDM24 */
1182		72,		/* IFM_IEEE80211_OFDM36 */
1183		96,		/* IFM_IEEE80211_OFDM48 */
1184		108,		/* IFM_IEEE80211_OFDM54 */
1185		144,		/* IFM_IEEE80211_OFDM72 */
1186		0,		/* IFM_IEEE80211_DS354k */
1187		0,		/* IFM_IEEE80211_DS512k */
1188		6,		/* IFM_IEEE80211_OFDM3 */
1189		9,		/* IFM_IEEE80211_OFDM4 */
1190		54,		/* IFM_IEEE80211_OFDM27 */
1191		-1,		/* IFM_IEEE80211_MCS */
1192	};
1193	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1194		ieeerates[IFM_SUBTYPE(mword)] : 0;
1195#undef N
1196}
1197