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