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