ieee80211.c revision 284014
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 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 284014 2015-06-05 06:49:08Z adrian $");
29
30/*
31 * IEEE 802.11 generic handler
32 */
33#include "opt_wlan.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/socket.h>
39
40#include <machine/stdarg.h>
41
42#include <net/if.h>
43#include <net/if_var.h>
44#include <net/if_dl.h>
45#include <net/if_media.h>
46#include <net/if_types.h>
47#include <net/ethernet.h>
48
49#include <net80211/ieee80211_var.h>
50#include <net80211/ieee80211_regdomain.h>
51#ifdef IEEE80211_SUPPORT_SUPERG
52#include <net80211/ieee80211_superg.h>
53#endif
54#include <net80211/ieee80211_ratectl.h>
55
56#include <net/bpf.h>
57
58const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
59	[IEEE80211_MODE_AUTO]	  = "auto",
60	[IEEE80211_MODE_11A]	  = "11a",
61	[IEEE80211_MODE_11B]	  = "11b",
62	[IEEE80211_MODE_11G]	  = "11g",
63	[IEEE80211_MODE_FH]	  = "FH",
64	[IEEE80211_MODE_TURBO_A]  = "turboA",
65	[IEEE80211_MODE_TURBO_G]  = "turboG",
66	[IEEE80211_MODE_STURBO_A] = "sturboA",
67	[IEEE80211_MODE_HALF]	  = "half",
68	[IEEE80211_MODE_QUARTER]  = "quarter",
69	[IEEE80211_MODE_11NA]	  = "11na",
70	[IEEE80211_MODE_11NG]	  = "11ng",
71};
72/* map ieee80211_opmode to the corresponding capability bit */
73const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
74	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
75	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
76	[IEEE80211_M_STA]	= IEEE80211_C_STA,
77	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
78	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
79	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
80#ifdef IEEE80211_SUPPORT_MESH
81	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
82#endif
83};
84
85const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
86	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
87
88static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
89static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
90static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
91static	int ieee80211_media_setup(struct ieee80211com *ic,
92		struct ifmedia *media, int caps, int addsta,
93		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
94static	void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
95static	int ieee80211com_media_change(struct ifnet *);
96static	int media_status(enum ieee80211_opmode,
97		const struct ieee80211_channel *);
98static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
99
100MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
101
102/*
103 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
104 */
105#define	B(r)	((r) | IEEE80211_RATE_BASIC)
106static const struct ieee80211_rateset ieee80211_rateset_11a =
107	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
108static const struct ieee80211_rateset ieee80211_rateset_half =
109	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
110static const struct ieee80211_rateset ieee80211_rateset_quarter =
111	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112static const struct ieee80211_rateset ieee80211_rateset_11b =
113	{ 4, { B(2), B(4), B(11), B(22) } };
114/* NB: OFDM rates are handled specially based on mode */
115static const struct ieee80211_rateset ieee80211_rateset_11g =
116	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
117#undef B
118
119/*
120 * Fill in 802.11 available channel set, mark
121 * all available channels as active, and pick
122 * a default channel if not already specified.
123 */
124static void
125ieee80211_chan_init(struct ieee80211com *ic)
126{
127#define	DEFAULTRATES(m, def) do { \
128	if (ic->ic_sup_rates[m].rs_nrates == 0) \
129		ic->ic_sup_rates[m] = def; \
130} while (0)
131	struct ieee80211_channel *c;
132	int i;
133
134	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
135		("invalid number of channels specified: %u", ic->ic_nchans));
136	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
137	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
138	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
139	for (i = 0; i < ic->ic_nchans; i++) {
140		c = &ic->ic_channels[i];
141		KASSERT(c->ic_flags != 0, ("channel with no flags"));
142		/*
143		 * Help drivers that work only with frequencies by filling
144		 * in IEEE channel #'s if not already calculated.  Note this
145		 * mimics similar work done in ieee80211_setregdomain when
146		 * changing regulatory state.
147		 */
148		if (c->ic_ieee == 0)
149			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
150		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
151			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
152			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
153			    c->ic_flags);
154		/* default max tx power to max regulatory */
155		if (c->ic_maxpower == 0)
156			c->ic_maxpower = 2*c->ic_maxregpower;
157		setbit(ic->ic_chan_avail, c->ic_ieee);
158		/*
159		 * Identify mode capabilities.
160		 */
161		if (IEEE80211_IS_CHAN_A(c))
162			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
163		if (IEEE80211_IS_CHAN_B(c))
164			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
165		if (IEEE80211_IS_CHAN_ANYG(c))
166			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
167		if (IEEE80211_IS_CHAN_FHSS(c))
168			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
169		if (IEEE80211_IS_CHAN_108A(c))
170			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
171		if (IEEE80211_IS_CHAN_108G(c))
172			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
173		if (IEEE80211_IS_CHAN_ST(c))
174			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
175		if (IEEE80211_IS_CHAN_HALF(c))
176			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
177		if (IEEE80211_IS_CHAN_QUARTER(c))
178			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
179		if (IEEE80211_IS_CHAN_HTA(c))
180			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
181		if (IEEE80211_IS_CHAN_HTG(c))
182			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
183	}
184	/* initialize candidate channels to all available */
185	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
186		sizeof(ic->ic_chan_avail));
187
188	/* sort channel table to allow lookup optimizations */
189	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
190
191	/* invalidate any previous state */
192	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
193	ic->ic_prevchan = NULL;
194	ic->ic_csa_newchan = NULL;
195	/* arbitrarily pick the first channel */
196	ic->ic_curchan = &ic->ic_channels[0];
197	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
198
199	/* fillin well-known rate sets if driver has not specified */
200	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
201	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
202	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
203	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
204	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
205	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
206	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
207	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
208	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
209	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
210
211	/*
212	 * Setup required information to fill the mcsset field, if driver did
213	 * not. Assume a 2T2R setup for historic reasons.
214	 */
215	if (ic->ic_rxstream == 0)
216		ic->ic_rxstream = 2;
217	if (ic->ic_txstream == 0)
218		ic->ic_txstream = 2;
219
220	/*
221	 * Set auto mode to reset active channel state and any desired channel.
222	 */
223	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
224#undef DEFAULTRATES
225}
226
227static void
228null_update_mcast(struct ieee80211com *ic)
229{
230
231	ic_printf(ic, "need multicast update callback\n");
232}
233
234static void
235null_update_promisc(struct ieee80211com *ic)
236{
237
238	ic_printf(ic, "need promiscuous mode update callback\n");
239}
240
241static int
242null_transmit(struct ifnet *ifp, struct mbuf *m)
243{
244	m_freem(m);
245	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
246	return EACCES;		/* XXX EIO/EPERM? */
247}
248
249static int
250null_output(struct ifnet *ifp, struct mbuf *m,
251	const struct sockaddr *dst, struct route *ro)
252{
253	if_printf(ifp, "discard raw packet\n");
254	return null_transmit(ifp, m);
255}
256
257static void
258null_input(struct ifnet *ifp, struct mbuf *m)
259{
260	if_printf(ifp, "if_input should not be called\n");
261	m_freem(m);
262}
263
264static void
265null_update_chw(struct ieee80211com *ic)
266{
267
268	ic_printf(ic, "%s: need callback\n", __func__);
269}
270
271int
272ic_printf(struct ieee80211com *ic, const char * fmt, ...)
273{
274	va_list ap;
275	int retval;
276
277	retval = printf("%s: ", ic->ic_name);
278	va_start(ap, fmt);
279	retval += vprintf(fmt, ap);
280	va_end(ap);
281	return (retval);
282}
283
284/*
285 * Attach/setup the common net80211 state.  Called by
286 * the driver on attach to prior to creating any vap's.
287 */
288void
289ieee80211_ifattach(struct ieee80211com *ic,
290	const uint8_t macaddr[IEEE80211_ADDR_LEN])
291{
292	struct ifnet *ifp = ic->ic_ifp;
293	struct sockaddr_dl *sdl;
294	struct ifaddr *ifa;
295
296	KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
297
298	IEEE80211_LOCK_INIT(ic, ic->ic_name);
299	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
300	TAILQ_INIT(&ic->ic_vaps);
301
302	/* Create a taskqueue for all state changes */
303	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
304	    taskqueue_thread_enqueue, &ic->ic_tq);
305	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
306	    ic->ic_name);
307	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
308	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
309	/*
310	 * Fill in 802.11 available channel set, mark all
311	 * available channels as active, and pick a default
312	 * channel if not already specified.
313	 */
314	ieee80211_media_init(ic);
315
316	ic->ic_update_mcast = null_update_mcast;
317	ic->ic_update_promisc = null_update_promisc;
318	ic->ic_update_chw = null_update_chw;
319
320	ic->ic_hash_key = arc4random();
321	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
322	ic->ic_lintval = ic->ic_bintval;
323	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
324
325	ieee80211_crypto_attach(ic);
326	ieee80211_node_attach(ic);
327	ieee80211_power_attach(ic);
328	ieee80211_proto_attach(ic);
329#ifdef IEEE80211_SUPPORT_SUPERG
330	ieee80211_superg_attach(ic);
331#endif
332	ieee80211_ht_attach(ic);
333	ieee80211_scan_attach(ic);
334	ieee80211_regdomain_attach(ic);
335	ieee80211_dfs_attach(ic);
336
337	ieee80211_sysctl_attach(ic);
338
339	ifp->if_addrlen = IEEE80211_ADDR_LEN;
340	ifp->if_hdrlen = 0;
341
342	CURVNET_SET(vnet0);
343
344	if_attach(ifp);
345
346	ifp->if_mtu = IEEE80211_MTU_MAX;
347	ifp->if_broadcastaddr = ieee80211broadcastaddr;
348	ifp->if_output = null_output;
349	ifp->if_input = null_input;	/* just in case */
350	ifp->if_resolvemulti = NULL;	/* NB: callers check */
351
352	ifa = ifaddr_byindex(ifp->if_index);
353	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
354	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
355	sdl->sdl_type = IFT_ETHER;		/* XXX IFT_IEEE80211? */
356	sdl->sdl_alen = IEEE80211_ADDR_LEN;
357	IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
358	ifa_free(ifa);
359
360	CURVNET_RESTORE();
361}
362
363/*
364 * Detach net80211 state on device detach.  Tear down
365 * all vap's and reclaim all common state prior to the
366 * device state going away.  Note we may call back into
367 * driver; it must be prepared for this.
368 */
369void
370ieee80211_ifdetach(struct ieee80211com *ic)
371{
372	struct ifnet *ifp = ic->ic_ifp;
373	struct ieee80211vap *vap;
374
375	/*
376	 * This detaches the main interface, but not the vaps.
377	 * Each VAP may be in a separate VIMAGE.
378	 */
379	CURVNET_SET(ifp->if_vnet);
380	if_detach(ifp);
381	CURVNET_RESTORE();
382
383	/*
384	 * The VAP is responsible for setting and clearing
385	 * the VIMAGE context.
386	 */
387	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
388		ieee80211_vap_destroy(vap);
389	ieee80211_waitfor_parent(ic);
390
391	ieee80211_sysctl_detach(ic);
392	ieee80211_dfs_detach(ic);
393	ieee80211_regdomain_detach(ic);
394	ieee80211_scan_detach(ic);
395#ifdef IEEE80211_SUPPORT_SUPERG
396	ieee80211_superg_detach(ic);
397#endif
398	ieee80211_ht_detach(ic);
399	/* NB: must be called before ieee80211_node_detach */
400	ieee80211_proto_detach(ic);
401	ieee80211_crypto_detach(ic);
402	ieee80211_power_detach(ic);
403	ieee80211_node_detach(ic);
404
405	/* XXX VNET needed? */
406	ifmedia_removeall(&ic->ic_media);
407	counter_u64_free(ic->ic_ierrors);
408	counter_u64_free(ic->ic_oerrors);
409
410	taskqueue_free(ic->ic_tq);
411	IEEE80211_TX_LOCK_DESTROY(ic);
412	IEEE80211_LOCK_DESTROY(ic);
413}
414
415/*
416 * Default reset method for use with the ioctl support.  This
417 * method is invoked after any state change in the 802.11
418 * layer that should be propagated to the hardware but not
419 * require re-initialization of the 802.11 state machine (e.g
420 * rescanning for an ap).  We always return ENETRESET which
421 * should cause the driver to re-initialize the device. Drivers
422 * can override this method to implement more optimized support.
423 */
424static int
425default_reset(struct ieee80211vap *vap, u_long cmd)
426{
427	return ENETRESET;
428}
429
430/*
431 * Add underlying device errors to vap errors.
432 */
433static uint64_t
434ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
435{
436	struct ieee80211vap *vap = ifp->if_softc;
437	struct ieee80211com *ic = vap->iv_ic;
438	uint64_t rv;
439
440	rv = if_get_counter_default(ifp, cnt);
441	switch (cnt) {
442	case IFCOUNTER_OERRORS:
443		rv += counter_u64_fetch(ic->ic_oerrors);
444		break;
445	case IFCOUNTER_IERRORS:
446		rv += counter_u64_fetch(ic->ic_ierrors);
447		break;
448	default:
449		break;
450	}
451
452	return (rv);
453}
454
455/*
456 * Prepare a vap for use.  Drivers use this call to
457 * setup net80211 state in new vap's prior attaching
458 * them with ieee80211_vap_attach (below).
459 */
460int
461ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
462    const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
463    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
464    const uint8_t macaddr[IEEE80211_ADDR_LEN])
465{
466	struct ifnet *ifp;
467
468	ifp = if_alloc(IFT_ETHER);
469	if (ifp == NULL) {
470		ic_printf(ic, "%s: unable to allocate ifnet\n",
471		    __func__);
472		return ENOMEM;
473	}
474	if_initname(ifp, name, unit);
475	ifp->if_softc = vap;			/* back pointer */
476	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
477	ifp->if_transmit = ieee80211_vap_transmit;
478	ifp->if_qflush = ieee80211_vap_qflush;
479	ifp->if_ioctl = ieee80211_ioctl;
480	ifp->if_init = ieee80211_init;
481	ifp->if_get_counter = ieee80211_get_counter;
482
483	vap->iv_ifp = ifp;
484	vap->iv_ic = ic;
485	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
486	vap->iv_flags_ext = ic->ic_flags_ext;
487	vap->iv_flags_ven = ic->ic_flags_ven;
488	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
489	vap->iv_htcaps = ic->ic_htcaps;
490	vap->iv_htextcaps = ic->ic_htextcaps;
491	vap->iv_opmode = opmode;
492	vap->iv_caps |= ieee80211_opcap[opmode];
493	switch (opmode) {
494	case IEEE80211_M_WDS:
495		/*
496		 * WDS links must specify the bssid of the far end.
497		 * For legacy operation this is a static relationship.
498		 * For non-legacy operation the station must associate
499		 * and be authorized to pass traffic.  Plumbing the
500		 * vap to the proper node happens when the vap
501		 * transitions to RUN state.
502		 */
503		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
504		vap->iv_flags |= IEEE80211_F_DESBSSID;
505		if (flags & IEEE80211_CLONE_WDSLEGACY)
506			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
507		break;
508#ifdef IEEE80211_SUPPORT_TDMA
509	case IEEE80211_M_AHDEMO:
510		if (flags & IEEE80211_CLONE_TDMA) {
511			/* NB: checked before clone operation allowed */
512			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
513			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
514			/*
515			 * Propagate TDMA capability to mark vap; this
516			 * cannot be removed and is used to distinguish
517			 * regular ahdemo operation from ahdemo+tdma.
518			 */
519			vap->iv_caps |= IEEE80211_C_TDMA;
520		}
521		break;
522#endif
523	default:
524		break;
525	}
526	/* auto-enable s/w beacon miss support */
527	if (flags & IEEE80211_CLONE_NOBEACONS)
528		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
529	/* auto-generated or user supplied MAC address */
530	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
531		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
532	/*
533	 * Enable various functionality by default if we're
534	 * capable; the driver can override us if it knows better.
535	 */
536	if (vap->iv_caps & IEEE80211_C_WME)
537		vap->iv_flags |= IEEE80211_F_WME;
538	if (vap->iv_caps & IEEE80211_C_BURST)
539		vap->iv_flags |= IEEE80211_F_BURST;
540	/* NB: bg scanning only makes sense for station mode right now */
541	if (vap->iv_opmode == IEEE80211_M_STA &&
542	    (vap->iv_caps & IEEE80211_C_BGSCAN))
543		vap->iv_flags |= IEEE80211_F_BGSCAN;
544	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
545	/* NB: DFS support only makes sense for ap mode right now */
546	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
547	    (vap->iv_caps & IEEE80211_C_DFS))
548		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
549
550	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
551	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
552	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
553	/*
554	 * Install a default reset method for the ioctl support;
555	 * the driver can override this.
556	 */
557	vap->iv_reset = default_reset;
558
559	IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
560
561	ieee80211_sysctl_vattach(vap);
562	ieee80211_crypto_vattach(vap);
563	ieee80211_node_vattach(vap);
564	ieee80211_power_vattach(vap);
565	ieee80211_proto_vattach(vap);
566#ifdef IEEE80211_SUPPORT_SUPERG
567	ieee80211_superg_vattach(vap);
568#endif
569	ieee80211_ht_vattach(vap);
570	ieee80211_scan_vattach(vap);
571	ieee80211_regdomain_vattach(vap);
572	ieee80211_radiotap_vattach(vap);
573	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
574
575	return 0;
576}
577
578/*
579 * Activate a vap.  State should have been prepared with a
580 * call to ieee80211_vap_setup and by the driver.  On return
581 * from this call the vap is ready for use.
582 */
583int
584ieee80211_vap_attach(struct ieee80211vap *vap,
585	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
586{
587	struct ifnet *ifp = vap->iv_ifp;
588	struct ieee80211com *ic = vap->iv_ic;
589	struct ifmediareq imr;
590	int maxrate;
591
592	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
593	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
594	    __func__, ieee80211_opmode_name[vap->iv_opmode],
595	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
596
597	/*
598	 * Do late attach work that cannot happen until after
599	 * the driver has had a chance to override defaults.
600	 */
601	ieee80211_node_latevattach(vap);
602	ieee80211_power_latevattach(vap);
603
604	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
605	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
606	ieee80211_media_status(ifp, &imr);
607	/* NB: strip explicit mode; we're actually in autoselect */
608	ifmedia_set(&vap->iv_media,
609	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
610	if (maxrate)
611		ifp->if_baudrate = IF_Mbps(maxrate);
612
613	ether_ifattach(ifp, vap->iv_myaddr);
614	/* hook output method setup by ether_ifattach */
615	vap->iv_output = ifp->if_output;
616	ifp->if_output = ieee80211_output;
617	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
618
619	IEEE80211_LOCK(ic);
620	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
621	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
622#ifdef IEEE80211_SUPPORT_SUPERG
623	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
624#endif
625	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
626	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
627	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
628	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
629	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
630	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
631	IEEE80211_UNLOCK(ic);
632
633	return 1;
634}
635
636/*
637 * Tear down vap state and reclaim the ifnet.
638 * The driver is assumed to have prepared for
639 * this; e.g. by turning off interrupts for the
640 * underlying device.
641 */
642void
643ieee80211_vap_detach(struct ieee80211vap *vap)
644{
645	struct ieee80211com *ic = vap->iv_ic;
646	struct ifnet *ifp = vap->iv_ifp;
647
648	CURVNET_SET(ifp->if_vnet);
649
650	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
651	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
652
653	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
654	ether_ifdetach(ifp);
655
656	ieee80211_stop(vap);
657
658	/*
659	 * Flush any deferred vap tasks.
660	 */
661	ieee80211_draintask(ic, &vap->iv_nstate_task);
662	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
663
664	/* XXX band-aid until ifnet handles this for us */
665	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
666
667	IEEE80211_LOCK(ic);
668	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
669	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
670	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
671#ifdef IEEE80211_SUPPORT_SUPERG
672	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
673#endif
674	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
675	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
676	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
677	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
678	/* NB: this handles the bpfdetach done below */
679	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
680	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
681	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
682	IEEE80211_UNLOCK(ic);
683
684	ifmedia_removeall(&vap->iv_media);
685
686	ieee80211_radiotap_vdetach(vap);
687	ieee80211_regdomain_vdetach(vap);
688	ieee80211_scan_vdetach(vap);
689#ifdef IEEE80211_SUPPORT_SUPERG
690	ieee80211_superg_vdetach(vap);
691#endif
692	ieee80211_ht_vdetach(vap);
693	/* NB: must be before ieee80211_node_vdetach */
694	ieee80211_proto_vdetach(vap);
695	ieee80211_crypto_vdetach(vap);
696	ieee80211_power_vdetach(vap);
697	ieee80211_node_vdetach(vap);
698	ieee80211_sysctl_vdetach(vap);
699
700	if_free(ifp);
701
702	CURVNET_RESTORE();
703}
704
705/*
706 * Synchronize flag bit state in the parent ifnet structure
707 * according to the state of all vap ifnet's.  This is used,
708 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
709 */
710void
711ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
712{
713	struct ifnet *ifp = ic->ic_ifp;
714	struct ieee80211vap *vap;
715	int bit, oflags;
716
717	IEEE80211_LOCK_ASSERT(ic);
718
719	bit = 0;
720	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
721		if (vap->iv_ifp->if_flags & flag) {
722			/*
723			 * XXX the bridge sets PROMISC but we don't want to
724			 * enable it on the device, discard here so all the
725			 * drivers don't need to special-case it
726			 */
727			if (flag == IFF_PROMISC &&
728			    !(vap->iv_opmode == IEEE80211_M_MONITOR ||
729			      (vap->iv_opmode == IEEE80211_M_AHDEMO &&
730			       (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
731				continue;
732			bit = 1;
733			break;
734		}
735	oflags = ifp->if_flags;
736	if (bit)
737		ifp->if_flags |= flag;
738	else
739		ifp->if_flags &= ~flag;
740	if ((ifp->if_flags ^ oflags) & flag) {
741		/* XXX should we return 1/0 and let caller do this? */
742		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
743			if (flag == IFF_PROMISC)
744				ieee80211_runtask(ic, &ic->ic_promisc_task);
745			else if (flag == IFF_ALLMULTI)
746				ieee80211_runtask(ic, &ic->ic_mcast_task);
747		}
748	}
749}
750
751/*
752 * Synchronize flag bit state in the com structure
753 * according to the state of all vap's.  This is used,
754 * for example, to handle state changes via ioctls.
755 */
756static void
757ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
758{
759	struct ieee80211vap *vap;
760	int bit;
761
762	IEEE80211_LOCK_ASSERT(ic);
763
764	bit = 0;
765	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
766		if (vap->iv_flags & flag) {
767			bit = 1;
768			break;
769		}
770	if (bit)
771		ic->ic_flags |= flag;
772	else
773		ic->ic_flags &= ~flag;
774}
775
776void
777ieee80211_syncflag(struct ieee80211vap *vap, int flag)
778{
779	struct ieee80211com *ic = vap->iv_ic;
780
781	IEEE80211_LOCK(ic);
782	if (flag < 0) {
783		flag = -flag;
784		vap->iv_flags &= ~flag;
785	} else
786		vap->iv_flags |= flag;
787	ieee80211_syncflag_locked(ic, flag);
788	IEEE80211_UNLOCK(ic);
789}
790
791/*
792 * Synchronize flags_ht bit state in the com structure
793 * according to the state of all vap's.  This is used,
794 * for example, to handle state changes via ioctls.
795 */
796static void
797ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
798{
799	struct ieee80211vap *vap;
800	int bit;
801
802	IEEE80211_LOCK_ASSERT(ic);
803
804	bit = 0;
805	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
806		if (vap->iv_flags_ht & flag) {
807			bit = 1;
808			break;
809		}
810	if (bit)
811		ic->ic_flags_ht |= flag;
812	else
813		ic->ic_flags_ht &= ~flag;
814}
815
816void
817ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
818{
819	struct ieee80211com *ic = vap->iv_ic;
820
821	IEEE80211_LOCK(ic);
822	if (flag < 0) {
823		flag = -flag;
824		vap->iv_flags_ht &= ~flag;
825	} else
826		vap->iv_flags_ht |= flag;
827	ieee80211_syncflag_ht_locked(ic, flag);
828	IEEE80211_UNLOCK(ic);
829}
830
831/*
832 * Synchronize flags_ext bit state in the com structure
833 * according to the state of all vap's.  This is used,
834 * for example, to handle state changes via ioctls.
835 */
836static void
837ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
838{
839	struct ieee80211vap *vap;
840	int bit;
841
842	IEEE80211_LOCK_ASSERT(ic);
843
844	bit = 0;
845	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
846		if (vap->iv_flags_ext & flag) {
847			bit = 1;
848			break;
849		}
850	if (bit)
851		ic->ic_flags_ext |= flag;
852	else
853		ic->ic_flags_ext &= ~flag;
854}
855
856void
857ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
858{
859	struct ieee80211com *ic = vap->iv_ic;
860
861	IEEE80211_LOCK(ic);
862	if (flag < 0) {
863		flag = -flag;
864		vap->iv_flags_ext &= ~flag;
865	} else
866		vap->iv_flags_ext |= flag;
867	ieee80211_syncflag_ext_locked(ic, flag);
868	IEEE80211_UNLOCK(ic);
869}
870
871static __inline int
872mapgsm(u_int freq, u_int flags)
873{
874	freq *= 10;
875	if (flags & IEEE80211_CHAN_QUARTER)
876		freq += 5;
877	else if (flags & IEEE80211_CHAN_HALF)
878		freq += 10;
879	else
880		freq += 20;
881	/* NB: there is no 907/20 wide but leave room */
882	return (freq - 906*10) / 5;
883}
884
885static __inline int
886mappsb(u_int freq, u_int flags)
887{
888	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
889}
890
891/*
892 * Convert MHz frequency to IEEE channel number.
893 */
894int
895ieee80211_mhz2ieee(u_int freq, u_int flags)
896{
897#define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
898	if (flags & IEEE80211_CHAN_GSM)
899		return mapgsm(freq, flags);
900	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
901		if (freq == 2484)
902			return 14;
903		if (freq < 2484)
904			return ((int) freq - 2407) / 5;
905		else
906			return 15 + ((freq - 2512) / 20);
907	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
908		if (freq <= 5000) {
909			/* XXX check regdomain? */
910			if (IS_FREQ_IN_PSB(freq))
911				return mappsb(freq, flags);
912			return (freq - 4000) / 5;
913		} else
914			return (freq - 5000) / 5;
915	} else {				/* either, guess */
916		if (freq == 2484)
917			return 14;
918		if (freq < 2484) {
919			if (907 <= freq && freq <= 922)
920				return mapgsm(freq, flags);
921			return ((int) freq - 2407) / 5;
922		}
923		if (freq < 5000) {
924			if (IS_FREQ_IN_PSB(freq))
925				return mappsb(freq, flags);
926			else if (freq > 4900)
927				return (freq - 4000) / 5;
928			else
929				return 15 + ((freq - 2512) / 20);
930		}
931		return (freq - 5000) / 5;
932	}
933#undef IS_FREQ_IN_PSB
934}
935
936/*
937 * Convert channel to IEEE channel number.
938 */
939int
940ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
941{
942	if (c == NULL) {
943		ic_printf(ic, "invalid channel (NULL)\n");
944		return 0;		/* XXX */
945	}
946	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
947}
948
949/*
950 * Convert IEEE channel number to MHz frequency.
951 */
952u_int
953ieee80211_ieee2mhz(u_int chan, u_int flags)
954{
955	if (flags & IEEE80211_CHAN_GSM)
956		return 907 + 5 * (chan / 10);
957	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
958		if (chan == 14)
959			return 2484;
960		if (chan < 14)
961			return 2407 + chan*5;
962		else
963			return 2512 + ((chan-15)*20);
964	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
965		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
966			chan -= 37;
967			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
968		}
969		return 5000 + (chan*5);
970	} else {				/* either, guess */
971		/* XXX can't distinguish PSB+GSM channels */
972		if (chan == 14)
973			return 2484;
974		if (chan < 14)			/* 0-13 */
975			return 2407 + chan*5;
976		if (chan < 27)			/* 15-26 */
977			return 2512 + ((chan-15)*20);
978		return 5000 + (chan*5);
979	}
980}
981
982/*
983 * Locate a channel given a frequency+flags.  We cache
984 * the previous lookup to optimize switching between two
985 * channels--as happens with dynamic turbo.
986 */
987struct ieee80211_channel *
988ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
989{
990	struct ieee80211_channel *c;
991	int i;
992
993	flags &= IEEE80211_CHAN_ALLTURBO;
994	c = ic->ic_prevchan;
995	if (c != NULL && c->ic_freq == freq &&
996	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
997		return c;
998	/* brute force search */
999	for (i = 0; i < ic->ic_nchans; i++) {
1000		c = &ic->ic_channels[i];
1001		if (c->ic_freq == freq &&
1002		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1003			return c;
1004	}
1005	return NULL;
1006}
1007
1008/*
1009 * Locate a channel given a channel number+flags.  We cache
1010 * the previous lookup to optimize switching between two
1011 * channels--as happens with dynamic turbo.
1012 */
1013struct ieee80211_channel *
1014ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1015{
1016	struct ieee80211_channel *c;
1017	int i;
1018
1019	flags &= IEEE80211_CHAN_ALLTURBO;
1020	c = ic->ic_prevchan;
1021	if (c != NULL && c->ic_ieee == ieee &&
1022	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1023		return c;
1024	/* brute force search */
1025	for (i = 0; i < ic->ic_nchans; i++) {
1026		c = &ic->ic_channels[i];
1027		if (c->ic_ieee == ieee &&
1028		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1029			return c;
1030	}
1031	return NULL;
1032}
1033
1034/*
1035 * Lookup a channel suitable for the given rx status.
1036 *
1037 * This is used to find a channel for a frame (eg beacon, probe
1038 * response) based purely on the received PHY information.
1039 *
1040 * For now it tries to do it based on R_FREQ / R_IEEE.
1041 * This is enough for 11bg and 11a (and thus 11ng/11na)
1042 * but it will not be enough for GSM, PSB channels and the
1043 * like.  It also doesn't know about legacy-turbog and
1044 * legacy-turbo modes, which some offload NICs actually
1045 * support in weird ways.
1046 *
1047 * Takes the ic and rxstatus; returns the channel or NULL
1048 * if not found.
1049 *
1050 * XXX TODO: Add support for that when the need arises.
1051 */
1052struct ieee80211_channel *
1053ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1054    const struct ieee80211_rx_stats *rxs)
1055{
1056	struct ieee80211com *ic = vap->iv_ic;
1057	uint32_t flags;
1058	struct ieee80211_channel *c;
1059
1060	if (rxs == NULL)
1061		return (NULL);
1062
1063	/*
1064	 * Strictly speaking we only use freq for now,
1065	 * however later on we may wish to just store
1066	 * the ieee for verification.
1067	 */
1068	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1069		return (NULL);
1070	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1071		return (NULL);
1072
1073	/*
1074	 * If the rx status contains a valid ieee/freq, then
1075	 * ensure we populate the correct channel information
1076	 * in rxchan before passing it up to the scan infrastructure.
1077	 * Offload NICs will pass up beacons from all channels
1078	 * during background scans.
1079	 */
1080
1081	/* Determine a band */
1082	/* XXX should be done by the driver? */
1083	if (rxs->c_freq < 3000) {
1084		flags = IEEE80211_CHAN_G;
1085	} else {
1086		flags = IEEE80211_CHAN_A;
1087	}
1088
1089	/* Channel lookup */
1090	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1091
1092	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1093	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1094	    __func__,
1095	    (int) rxs->c_freq,
1096	    (int) rxs->c_ieee,
1097	    flags,
1098	    c);
1099
1100	return (c);
1101}
1102
1103static void
1104addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1105{
1106#define	ADD(_ic, _s, _o) \
1107	ifmedia_add(media, \
1108		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1109	static const u_int mopts[IEEE80211_MODE_MAX] = {
1110	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1111	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1112	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1113	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1114	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1115	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1116	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1117	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1118	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1119	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1120	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1121	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1122	};
1123	u_int mopt;
1124
1125	mopt = mopts[mode];
1126	if (addsta)
1127		ADD(ic, mword, mopt);	/* STA mode has no cap */
1128	if (caps & IEEE80211_C_IBSS)
1129		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1130	if (caps & IEEE80211_C_HOSTAP)
1131		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1132	if (caps & IEEE80211_C_AHDEMO)
1133		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1134	if (caps & IEEE80211_C_MONITOR)
1135		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1136	if (caps & IEEE80211_C_WDS)
1137		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1138	if (caps & IEEE80211_C_MBSS)
1139		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1140#undef ADD
1141}
1142
1143/*
1144 * Setup the media data structures according to the channel and
1145 * rate tables.
1146 */
1147static int
1148ieee80211_media_setup(struct ieee80211com *ic,
1149	struct ifmedia *media, int caps, int addsta,
1150	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1151{
1152	int i, j, rate, maxrate, mword, r;
1153	enum ieee80211_phymode mode;
1154	const struct ieee80211_rateset *rs;
1155	struct ieee80211_rateset allrates;
1156
1157	/*
1158	 * Fill in media characteristics.
1159	 */
1160	ifmedia_init(media, 0, media_change, media_stat);
1161	maxrate = 0;
1162	/*
1163	 * Add media for legacy operating modes.
1164	 */
1165	memset(&allrates, 0, sizeof(allrates));
1166	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1167		if (isclr(ic->ic_modecaps, mode))
1168			continue;
1169		addmedia(media, caps, addsta, mode, IFM_AUTO);
1170		if (mode == IEEE80211_MODE_AUTO)
1171			continue;
1172		rs = &ic->ic_sup_rates[mode];
1173		for (i = 0; i < rs->rs_nrates; i++) {
1174			rate = rs->rs_rates[i];
1175			mword = ieee80211_rate2media(ic, rate, mode);
1176			if (mword == 0)
1177				continue;
1178			addmedia(media, caps, addsta, mode, mword);
1179			/*
1180			 * Add legacy rate to the collection of all rates.
1181			 */
1182			r = rate & IEEE80211_RATE_VAL;
1183			for (j = 0; j < allrates.rs_nrates; j++)
1184				if (allrates.rs_rates[j] == r)
1185					break;
1186			if (j == allrates.rs_nrates) {
1187				/* unique, add to the set */
1188				allrates.rs_rates[j] = r;
1189				allrates.rs_nrates++;
1190			}
1191			rate = (rate & IEEE80211_RATE_VAL) / 2;
1192			if (rate > maxrate)
1193				maxrate = rate;
1194		}
1195	}
1196	for (i = 0; i < allrates.rs_nrates; i++) {
1197		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1198				IEEE80211_MODE_AUTO);
1199		if (mword == 0)
1200			continue;
1201		/* NB: remove media options from mword */
1202		addmedia(media, caps, addsta,
1203		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1204	}
1205	/*
1206	 * Add HT/11n media.  Note that we do not have enough
1207	 * bits in the media subtype to express the MCS so we
1208	 * use a "placeholder" media subtype and any fixed MCS
1209	 * must be specified with a different mechanism.
1210	 */
1211	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1212		if (isclr(ic->ic_modecaps, mode))
1213			continue;
1214		addmedia(media, caps, addsta, mode, IFM_AUTO);
1215		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1216	}
1217	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1218	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1219		addmedia(media, caps, addsta,
1220		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1221		i = ic->ic_txstream * 8 - 1;
1222		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1223		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1224			rate = ieee80211_htrates[i].ht40_rate_400ns;
1225		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1226			rate = ieee80211_htrates[i].ht40_rate_800ns;
1227		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1228			rate = ieee80211_htrates[i].ht20_rate_400ns;
1229		else
1230			rate = ieee80211_htrates[i].ht20_rate_800ns;
1231		if (rate > maxrate)
1232			maxrate = rate;
1233	}
1234	return maxrate;
1235}
1236
1237void
1238ieee80211_media_init(struct ieee80211com *ic)
1239{
1240	struct ifnet *ifp = ic->ic_ifp;
1241	int maxrate;
1242
1243	/* NB: this works because the structure is initialized to zero */
1244	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1245		/*
1246		 * We are re-initializing the channel list; clear
1247		 * the existing media state as the media routines
1248		 * don't suppress duplicates.
1249		 */
1250		ifmedia_removeall(&ic->ic_media);
1251	}
1252	ieee80211_chan_init(ic);
1253
1254	/*
1255	 * Recalculate media settings in case new channel list changes
1256	 * the set of available modes.
1257	 */
1258	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1259		ieee80211com_media_change, ieee80211com_media_status);
1260	/* NB: strip explicit mode; we're actually in autoselect */
1261	ifmedia_set(&ic->ic_media,
1262	    media_status(ic->ic_opmode, ic->ic_curchan) &~
1263		(IFM_MMASK | IFM_IEEE80211_TURBO));
1264	if (maxrate)
1265		ifp->if_baudrate = IF_Mbps(maxrate);
1266
1267	/* XXX need to propagate new media settings to vap's */
1268}
1269
1270/* XXX inline or eliminate? */
1271const struct ieee80211_rateset *
1272ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1273{
1274	/* XXX does this work for 11ng basic rates? */
1275	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1276}
1277
1278void
1279ieee80211_announce(struct ieee80211com *ic)
1280{
1281	int i, rate, mword;
1282	enum ieee80211_phymode mode;
1283	const struct ieee80211_rateset *rs;
1284
1285	/* NB: skip AUTO since it has no rates */
1286	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1287		if (isclr(ic->ic_modecaps, mode))
1288			continue;
1289		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1290		rs = &ic->ic_sup_rates[mode];
1291		for (i = 0; i < rs->rs_nrates; i++) {
1292			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1293			if (mword == 0)
1294				continue;
1295			rate = ieee80211_media2rate(mword);
1296			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1297			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1298		}
1299		printf("\n");
1300	}
1301	ieee80211_ht_announce(ic);
1302}
1303
1304void
1305ieee80211_announce_channels(struct ieee80211com *ic)
1306{
1307	const struct ieee80211_channel *c;
1308	char type;
1309	int i, cw;
1310
1311	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1312	for (i = 0; i < ic->ic_nchans; i++) {
1313		c = &ic->ic_channels[i];
1314		if (IEEE80211_IS_CHAN_ST(c))
1315			type = 'S';
1316		else if (IEEE80211_IS_CHAN_108A(c))
1317			type = 'T';
1318		else if (IEEE80211_IS_CHAN_108G(c))
1319			type = 'G';
1320		else if (IEEE80211_IS_CHAN_HT(c))
1321			type = 'n';
1322		else if (IEEE80211_IS_CHAN_A(c))
1323			type = 'a';
1324		else if (IEEE80211_IS_CHAN_ANYG(c))
1325			type = 'g';
1326		else if (IEEE80211_IS_CHAN_B(c))
1327			type = 'b';
1328		else
1329			type = 'f';
1330		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1331			cw = 40;
1332		else if (IEEE80211_IS_CHAN_HALF(c))
1333			cw = 10;
1334		else if (IEEE80211_IS_CHAN_QUARTER(c))
1335			cw = 5;
1336		else
1337			cw = 20;
1338		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1339			, c->ic_ieee, c->ic_freq, type
1340			, cw
1341			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1342			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1343			, c->ic_maxregpower
1344			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1345			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1346		);
1347	}
1348}
1349
1350static int
1351media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1352{
1353	switch (IFM_MODE(ime->ifm_media)) {
1354	case IFM_IEEE80211_11A:
1355		*mode = IEEE80211_MODE_11A;
1356		break;
1357	case IFM_IEEE80211_11B:
1358		*mode = IEEE80211_MODE_11B;
1359		break;
1360	case IFM_IEEE80211_11G:
1361		*mode = IEEE80211_MODE_11G;
1362		break;
1363	case IFM_IEEE80211_FH:
1364		*mode = IEEE80211_MODE_FH;
1365		break;
1366	case IFM_IEEE80211_11NA:
1367		*mode = IEEE80211_MODE_11NA;
1368		break;
1369	case IFM_IEEE80211_11NG:
1370		*mode = IEEE80211_MODE_11NG;
1371		break;
1372	case IFM_AUTO:
1373		*mode = IEEE80211_MODE_AUTO;
1374		break;
1375	default:
1376		return 0;
1377	}
1378	/*
1379	 * Turbo mode is an ``option''.
1380	 * XXX does not apply to AUTO
1381	 */
1382	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1383		if (*mode == IEEE80211_MODE_11A) {
1384			if (flags & IEEE80211_F_TURBOP)
1385				*mode = IEEE80211_MODE_TURBO_A;
1386			else
1387				*mode = IEEE80211_MODE_STURBO_A;
1388		} else if (*mode == IEEE80211_MODE_11G)
1389			*mode = IEEE80211_MODE_TURBO_G;
1390		else
1391			return 0;
1392	}
1393	/* XXX HT40 +/- */
1394	return 1;
1395}
1396
1397/*
1398 * Handle a media change request on the underlying interface.
1399 */
1400int
1401ieee80211com_media_change(struct ifnet *ifp)
1402{
1403	return EINVAL;
1404}
1405
1406/*
1407 * Handle a media change request on the vap interface.
1408 */
1409int
1410ieee80211_media_change(struct ifnet *ifp)
1411{
1412	struct ieee80211vap *vap = ifp->if_softc;
1413	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1414	uint16_t newmode;
1415
1416	if (!media2mode(ime, vap->iv_flags, &newmode))
1417		return EINVAL;
1418	if (vap->iv_des_mode != newmode) {
1419		vap->iv_des_mode = newmode;
1420		/* XXX kick state machine if up+running */
1421	}
1422	return 0;
1423}
1424
1425/*
1426 * Common code to calculate the media status word
1427 * from the operating mode and channel state.
1428 */
1429static int
1430media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1431{
1432	int status;
1433
1434	status = IFM_IEEE80211;
1435	switch (opmode) {
1436	case IEEE80211_M_STA:
1437		break;
1438	case IEEE80211_M_IBSS:
1439		status |= IFM_IEEE80211_ADHOC;
1440		break;
1441	case IEEE80211_M_HOSTAP:
1442		status |= IFM_IEEE80211_HOSTAP;
1443		break;
1444	case IEEE80211_M_MONITOR:
1445		status |= IFM_IEEE80211_MONITOR;
1446		break;
1447	case IEEE80211_M_AHDEMO:
1448		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1449		break;
1450	case IEEE80211_M_WDS:
1451		status |= IFM_IEEE80211_WDS;
1452		break;
1453	case IEEE80211_M_MBSS:
1454		status |= IFM_IEEE80211_MBSS;
1455		break;
1456	}
1457	if (IEEE80211_IS_CHAN_HTA(chan)) {
1458		status |= IFM_IEEE80211_11NA;
1459	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1460		status |= IFM_IEEE80211_11NG;
1461	} else if (IEEE80211_IS_CHAN_A(chan)) {
1462		status |= IFM_IEEE80211_11A;
1463	} else if (IEEE80211_IS_CHAN_B(chan)) {
1464		status |= IFM_IEEE80211_11B;
1465	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1466		status |= IFM_IEEE80211_11G;
1467	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1468		status |= IFM_IEEE80211_FH;
1469	}
1470	/* XXX else complain? */
1471
1472	if (IEEE80211_IS_CHAN_TURBO(chan))
1473		status |= IFM_IEEE80211_TURBO;
1474#if 0
1475	if (IEEE80211_IS_CHAN_HT20(chan))
1476		status |= IFM_IEEE80211_HT20;
1477	if (IEEE80211_IS_CHAN_HT40(chan))
1478		status |= IFM_IEEE80211_HT40;
1479#endif
1480	return status;
1481}
1482
1483static void
1484ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1485{
1486	struct ieee80211com *ic = ifp->if_l2com;
1487	struct ieee80211vap *vap;
1488
1489	imr->ifm_status = IFM_AVALID;
1490	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1491		if (vap->iv_ifp->if_flags & IFF_UP) {
1492			imr->ifm_status |= IFM_ACTIVE;
1493			break;
1494		}
1495	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1496	if (imr->ifm_status & IFM_ACTIVE)
1497		imr->ifm_current = imr->ifm_active;
1498}
1499
1500void
1501ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1502{
1503	struct ieee80211vap *vap = ifp->if_softc;
1504	struct ieee80211com *ic = vap->iv_ic;
1505	enum ieee80211_phymode mode;
1506
1507	imr->ifm_status = IFM_AVALID;
1508	/*
1509	 * NB: use the current channel's mode to lock down a xmit
1510	 * rate only when running; otherwise we may have a mismatch
1511	 * in which case the rate will not be convertible.
1512	 */
1513	if (vap->iv_state == IEEE80211_S_RUN ||
1514	    vap->iv_state == IEEE80211_S_SLEEP) {
1515		imr->ifm_status |= IFM_ACTIVE;
1516		mode = ieee80211_chan2mode(ic->ic_curchan);
1517	} else
1518		mode = IEEE80211_MODE_AUTO;
1519	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1520	/*
1521	 * Calculate a current rate if possible.
1522	 */
1523	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1524		/*
1525		 * A fixed rate is set, report that.
1526		 */
1527		imr->ifm_active |= ieee80211_rate2media(ic,
1528			vap->iv_txparms[mode].ucastrate, mode);
1529	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1530		/*
1531		 * In station mode report the current transmit rate.
1532		 */
1533		imr->ifm_active |= ieee80211_rate2media(ic,
1534			vap->iv_bss->ni_txrate, mode);
1535	} else
1536		imr->ifm_active |= IFM_AUTO;
1537	if (imr->ifm_status & IFM_ACTIVE)
1538		imr->ifm_current = imr->ifm_active;
1539}
1540
1541/*
1542 * Set the current phy mode and recalculate the active channel
1543 * set based on the available channels for this mode.  Also
1544 * select a new default/current channel if the current one is
1545 * inappropriate for this mode.
1546 */
1547int
1548ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1549{
1550	/*
1551	 * Adjust basic rates in 11b/11g supported rate set.
1552	 * Note that if operating on a hal/quarter rate channel
1553	 * this is a noop as those rates sets are different
1554	 * and used instead.
1555	 */
1556	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1557		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1558
1559	ic->ic_curmode = mode;
1560	ieee80211_reset_erp(ic);	/* reset ERP state */
1561
1562	return 0;
1563}
1564
1565/*
1566 * Return the phy mode for with the specified channel.
1567 */
1568enum ieee80211_phymode
1569ieee80211_chan2mode(const struct ieee80211_channel *chan)
1570{
1571
1572	if (IEEE80211_IS_CHAN_HTA(chan))
1573		return IEEE80211_MODE_11NA;
1574	else if (IEEE80211_IS_CHAN_HTG(chan))
1575		return IEEE80211_MODE_11NG;
1576	else if (IEEE80211_IS_CHAN_108G(chan))
1577		return IEEE80211_MODE_TURBO_G;
1578	else if (IEEE80211_IS_CHAN_ST(chan))
1579		return IEEE80211_MODE_STURBO_A;
1580	else if (IEEE80211_IS_CHAN_TURBO(chan))
1581		return IEEE80211_MODE_TURBO_A;
1582	else if (IEEE80211_IS_CHAN_HALF(chan))
1583		return IEEE80211_MODE_HALF;
1584	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1585		return IEEE80211_MODE_QUARTER;
1586	else if (IEEE80211_IS_CHAN_A(chan))
1587		return IEEE80211_MODE_11A;
1588	else if (IEEE80211_IS_CHAN_ANYG(chan))
1589		return IEEE80211_MODE_11G;
1590	else if (IEEE80211_IS_CHAN_B(chan))
1591		return IEEE80211_MODE_11B;
1592	else if (IEEE80211_IS_CHAN_FHSS(chan))
1593		return IEEE80211_MODE_FH;
1594
1595	/* NB: should not get here */
1596	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1597		__func__, chan->ic_freq, chan->ic_flags);
1598	return IEEE80211_MODE_11B;
1599}
1600
1601struct ratemedia {
1602	u_int	match;	/* rate + mode */
1603	u_int	media;	/* if_media rate */
1604};
1605
1606static int
1607findmedia(const struct ratemedia rates[], int n, u_int match)
1608{
1609	int i;
1610
1611	for (i = 0; i < n; i++)
1612		if (rates[i].match == match)
1613			return rates[i].media;
1614	return IFM_AUTO;
1615}
1616
1617/*
1618 * Convert IEEE80211 rate value to ifmedia subtype.
1619 * Rate is either a legacy rate in units of 0.5Mbps
1620 * or an MCS index.
1621 */
1622int
1623ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1624{
1625	static const struct ratemedia rates[] = {
1626		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1627		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1628		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1629		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1630		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1631		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1632		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1633		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1634		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1635		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1636		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1637		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1638		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1639		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1640		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1641		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1642		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1643		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1644		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1645		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1646		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1647		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1648		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1649		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1650		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1651		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1652		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1653		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1654		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1655		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1656		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1657	};
1658	static const struct ratemedia htrates[] = {
1659		{   0, IFM_IEEE80211_MCS },
1660		{   1, IFM_IEEE80211_MCS },
1661		{   2, IFM_IEEE80211_MCS },
1662		{   3, IFM_IEEE80211_MCS },
1663		{   4, IFM_IEEE80211_MCS },
1664		{   5, IFM_IEEE80211_MCS },
1665		{   6, IFM_IEEE80211_MCS },
1666		{   7, IFM_IEEE80211_MCS },
1667		{   8, IFM_IEEE80211_MCS },
1668		{   9, IFM_IEEE80211_MCS },
1669		{  10, IFM_IEEE80211_MCS },
1670		{  11, IFM_IEEE80211_MCS },
1671		{  12, IFM_IEEE80211_MCS },
1672		{  13, IFM_IEEE80211_MCS },
1673		{  14, IFM_IEEE80211_MCS },
1674		{  15, IFM_IEEE80211_MCS },
1675		{  16, IFM_IEEE80211_MCS },
1676		{  17, IFM_IEEE80211_MCS },
1677		{  18, IFM_IEEE80211_MCS },
1678		{  19, IFM_IEEE80211_MCS },
1679		{  20, IFM_IEEE80211_MCS },
1680		{  21, IFM_IEEE80211_MCS },
1681		{  22, IFM_IEEE80211_MCS },
1682		{  23, IFM_IEEE80211_MCS },
1683		{  24, IFM_IEEE80211_MCS },
1684		{  25, IFM_IEEE80211_MCS },
1685		{  26, IFM_IEEE80211_MCS },
1686		{  27, IFM_IEEE80211_MCS },
1687		{  28, IFM_IEEE80211_MCS },
1688		{  29, IFM_IEEE80211_MCS },
1689		{  30, IFM_IEEE80211_MCS },
1690		{  31, IFM_IEEE80211_MCS },
1691		{  32, IFM_IEEE80211_MCS },
1692		{  33, IFM_IEEE80211_MCS },
1693		{  34, IFM_IEEE80211_MCS },
1694		{  35, IFM_IEEE80211_MCS },
1695		{  36, IFM_IEEE80211_MCS },
1696		{  37, IFM_IEEE80211_MCS },
1697		{  38, IFM_IEEE80211_MCS },
1698		{  39, IFM_IEEE80211_MCS },
1699		{  40, IFM_IEEE80211_MCS },
1700		{  41, IFM_IEEE80211_MCS },
1701		{  42, IFM_IEEE80211_MCS },
1702		{  43, IFM_IEEE80211_MCS },
1703		{  44, IFM_IEEE80211_MCS },
1704		{  45, IFM_IEEE80211_MCS },
1705		{  46, IFM_IEEE80211_MCS },
1706		{  47, IFM_IEEE80211_MCS },
1707		{  48, IFM_IEEE80211_MCS },
1708		{  49, IFM_IEEE80211_MCS },
1709		{  50, IFM_IEEE80211_MCS },
1710		{  51, IFM_IEEE80211_MCS },
1711		{  52, IFM_IEEE80211_MCS },
1712		{  53, IFM_IEEE80211_MCS },
1713		{  54, IFM_IEEE80211_MCS },
1714		{  55, IFM_IEEE80211_MCS },
1715		{  56, IFM_IEEE80211_MCS },
1716		{  57, IFM_IEEE80211_MCS },
1717		{  58, IFM_IEEE80211_MCS },
1718		{  59, IFM_IEEE80211_MCS },
1719		{  60, IFM_IEEE80211_MCS },
1720		{  61, IFM_IEEE80211_MCS },
1721		{  62, IFM_IEEE80211_MCS },
1722		{  63, IFM_IEEE80211_MCS },
1723		{  64, IFM_IEEE80211_MCS },
1724		{  65, IFM_IEEE80211_MCS },
1725		{  66, IFM_IEEE80211_MCS },
1726		{  67, IFM_IEEE80211_MCS },
1727		{  68, IFM_IEEE80211_MCS },
1728		{  69, IFM_IEEE80211_MCS },
1729		{  70, IFM_IEEE80211_MCS },
1730		{  71, IFM_IEEE80211_MCS },
1731		{  72, IFM_IEEE80211_MCS },
1732		{  73, IFM_IEEE80211_MCS },
1733		{  74, IFM_IEEE80211_MCS },
1734		{  75, IFM_IEEE80211_MCS },
1735		{  76, IFM_IEEE80211_MCS },
1736	};
1737	int m;
1738
1739	/*
1740	 * Check 11n rates first for match as an MCS.
1741	 */
1742	if (mode == IEEE80211_MODE_11NA) {
1743		if (rate & IEEE80211_RATE_MCS) {
1744			rate &= ~IEEE80211_RATE_MCS;
1745			m = findmedia(htrates, nitems(htrates), rate);
1746			if (m != IFM_AUTO)
1747				return m | IFM_IEEE80211_11NA;
1748		}
1749	} else if (mode == IEEE80211_MODE_11NG) {
1750		/* NB: 12 is ambiguous, it will be treated as an MCS */
1751		if (rate & IEEE80211_RATE_MCS) {
1752			rate &= ~IEEE80211_RATE_MCS;
1753			m = findmedia(htrates, nitems(htrates), rate);
1754			if (m != IFM_AUTO)
1755				return m | IFM_IEEE80211_11NG;
1756		}
1757	}
1758	rate &= IEEE80211_RATE_VAL;
1759	switch (mode) {
1760	case IEEE80211_MODE_11A:
1761	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
1762	case IEEE80211_MODE_QUARTER:
1763	case IEEE80211_MODE_11NA:
1764	case IEEE80211_MODE_TURBO_A:
1765	case IEEE80211_MODE_STURBO_A:
1766		return findmedia(rates, nitems(rates),
1767		    rate | IFM_IEEE80211_11A);
1768	case IEEE80211_MODE_11B:
1769		return findmedia(rates, nitems(rates),
1770		    rate | IFM_IEEE80211_11B);
1771	case IEEE80211_MODE_FH:
1772		return findmedia(rates, nitems(rates),
1773		    rate | IFM_IEEE80211_FH);
1774	case IEEE80211_MODE_AUTO:
1775		/* NB: ic may be NULL for some drivers */
1776		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1777			return findmedia(rates, nitems(rates),
1778			    rate | IFM_IEEE80211_FH);
1779		/* NB: hack, 11g matches both 11b+11a rates */
1780		/* fall thru... */
1781	case IEEE80211_MODE_11G:
1782	case IEEE80211_MODE_11NG:
1783	case IEEE80211_MODE_TURBO_G:
1784		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1785	}
1786	return IFM_AUTO;
1787}
1788
1789int
1790ieee80211_media2rate(int mword)
1791{
1792	static const int ieeerates[] = {
1793		-1,		/* IFM_AUTO */
1794		0,		/* IFM_MANUAL */
1795		0,		/* IFM_NONE */
1796		2,		/* IFM_IEEE80211_FH1 */
1797		4,		/* IFM_IEEE80211_FH2 */
1798		2,		/* IFM_IEEE80211_DS1 */
1799		4,		/* IFM_IEEE80211_DS2 */
1800		11,		/* IFM_IEEE80211_DS5 */
1801		22,		/* IFM_IEEE80211_DS11 */
1802		44,		/* IFM_IEEE80211_DS22 */
1803		12,		/* IFM_IEEE80211_OFDM6 */
1804		18,		/* IFM_IEEE80211_OFDM9 */
1805		24,		/* IFM_IEEE80211_OFDM12 */
1806		36,		/* IFM_IEEE80211_OFDM18 */
1807		48,		/* IFM_IEEE80211_OFDM24 */
1808		72,		/* IFM_IEEE80211_OFDM36 */
1809		96,		/* IFM_IEEE80211_OFDM48 */
1810		108,		/* IFM_IEEE80211_OFDM54 */
1811		144,		/* IFM_IEEE80211_OFDM72 */
1812		0,		/* IFM_IEEE80211_DS354k */
1813		0,		/* IFM_IEEE80211_DS512k */
1814		6,		/* IFM_IEEE80211_OFDM3 */
1815		9,		/* IFM_IEEE80211_OFDM4 */
1816		54,		/* IFM_IEEE80211_OFDM27 */
1817		-1,		/* IFM_IEEE80211_MCS */
1818	};
1819	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1820		ieeerates[IFM_SUBTYPE(mword)] : 0;
1821}
1822
1823/*
1824 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1825 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1826 */
1827#define	mix(a, b, c)							\
1828do {									\
1829	a -= b; a -= c; a ^= (c >> 13);					\
1830	b -= c; b -= a; b ^= (a << 8);					\
1831	c -= a; c -= b; c ^= (b >> 13);					\
1832	a -= b; a -= c; a ^= (c >> 12);					\
1833	b -= c; b -= a; b ^= (a << 16);					\
1834	c -= a; c -= b; c ^= (b >> 5);					\
1835	a -= b; a -= c; a ^= (c >> 3);					\
1836	b -= c; b -= a; b ^= (a << 10);					\
1837	c -= a; c -= b; c ^= (b >> 15);					\
1838} while (/*CONSTCOND*/0)
1839
1840uint32_t
1841ieee80211_mac_hash(const struct ieee80211com *ic,
1842	const uint8_t addr[IEEE80211_ADDR_LEN])
1843{
1844	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1845
1846	b += addr[5] << 8;
1847	b += addr[4];
1848	a += addr[3] << 24;
1849	a += addr[2] << 16;
1850	a += addr[1] << 8;
1851	a += addr[0];
1852
1853	mix(a, b, c);
1854
1855	return c;
1856}
1857#undef mix
1858
1859char
1860ieee80211_channel_type_char(const struct ieee80211_channel *c)
1861{
1862	if (IEEE80211_IS_CHAN_ST(c))
1863		return 'S';
1864	if (IEEE80211_IS_CHAN_108A(c))
1865		return 'T';
1866	if (IEEE80211_IS_CHAN_108G(c))
1867		return 'G';
1868	if (IEEE80211_IS_CHAN_HT(c))
1869		return 'n';
1870	if (IEEE80211_IS_CHAN_A(c))
1871		return 'a';
1872	if (IEEE80211_IS_CHAN_ANYG(c))
1873		return 'g';
1874	if (IEEE80211_IS_CHAN_B(c))
1875		return 'b';
1876	return 'f';
1877}
1878