xref: /freebsd/sys/net80211/ieee80211.c (revision ef0cb5db0af0d5d5b75b74f8e534fe601b7176d7)
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$");
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 
58 const 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 */
73 const 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 
85 static const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
86 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
87 
88 static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
89 static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
90 static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
91 static	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);
94 static	void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
95 static	int ieee80211com_media_change(struct ifnet *);
96 static	int media_status(enum ieee80211_opmode,
97 		const struct ieee80211_channel *);
98 
99 MALLOC_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)
105 static const struct ieee80211_rateset ieee80211_rateset_11a =
106 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
107 static const struct ieee80211_rateset ieee80211_rateset_half =
108 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
109 static const struct ieee80211_rateset ieee80211_rateset_quarter =
110 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
111 static 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 */
114 static 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  */
123 static void
124 ieee80211_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 
226 static void
227 null_update_mcast(struct ieee80211com *ic)
228 {
229 
230 	ic_printf(ic, "need multicast update callback\n");
231 }
232 
233 static void
234 null_update_promisc(struct ieee80211com *ic)
235 {
236 
237 	ic_printf(ic, "need promiscuous mode update callback\n");
238 }
239 
240 static int
241 null_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 
248 static int
249 null_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 
256 static void
257 null_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 
263 static void
264 null_update_chw(struct ieee80211com *ic)
265 {
266 
267 	ic_printf(ic, "%s: need callback\n", __func__);
268 }
269 
270 int
271 ic_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  */
287 void
288 ieee80211_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 	    ifp->if_xname);
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  */
366 void
367 ieee80211_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  */
419 static int
420 default_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  */
430 int
431 ieee80211_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  */
552 int
553 ieee80211_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  */
611 void
612 ieee80211_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],
621 	    ic->ic_name);
622 
623 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
624 	ether_ifdetach(ifp);
625 
626 	ieee80211_stop(vap);
627 
628 	/*
629 	 * Flush any deferred vap tasks.
630 	 */
631 	ieee80211_draintask(ic, &vap->iv_nstate_task);
632 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
633 
634 	/* XXX band-aid until ifnet handles this for us */
635 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
636 
637 	IEEE80211_LOCK(ic);
638 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
639 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
640 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
641 #ifdef IEEE80211_SUPPORT_SUPERG
642 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
643 #endif
644 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
645 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
646 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
647 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
648 	/* NB: this handles the bpfdetach done below */
649 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
650 	ieee80211_syncifflag_locked(ic, IFF_PROMISC);
651 	ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
652 	IEEE80211_UNLOCK(ic);
653 
654 	ifmedia_removeall(&vap->iv_media);
655 
656 	ieee80211_radiotap_vdetach(vap);
657 	ieee80211_regdomain_vdetach(vap);
658 	ieee80211_scan_vdetach(vap);
659 #ifdef IEEE80211_SUPPORT_SUPERG
660 	ieee80211_superg_vdetach(vap);
661 #endif
662 	ieee80211_ht_vdetach(vap);
663 	/* NB: must be before ieee80211_node_vdetach */
664 	ieee80211_proto_vdetach(vap);
665 	ieee80211_crypto_vdetach(vap);
666 	ieee80211_power_vdetach(vap);
667 	ieee80211_node_vdetach(vap);
668 	ieee80211_sysctl_vdetach(vap);
669 
670 	if_free(ifp);
671 
672 	CURVNET_RESTORE();
673 }
674 
675 /*
676  * Synchronize flag bit state in the parent ifnet structure
677  * according to the state of all vap ifnet's.  This is used,
678  * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
679  */
680 void
681 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
682 {
683 	struct ifnet *ifp = ic->ic_ifp;
684 	struct ieee80211vap *vap;
685 	int bit, oflags;
686 
687 	IEEE80211_LOCK_ASSERT(ic);
688 
689 	bit = 0;
690 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
691 		if (vap->iv_ifp->if_flags & flag) {
692 			/*
693 			 * XXX the bridge sets PROMISC but we don't want to
694 			 * enable it on the device, discard here so all the
695 			 * drivers don't need to special-case it
696 			 */
697 			if (flag == IFF_PROMISC &&
698 			    !(vap->iv_opmode == IEEE80211_M_MONITOR ||
699 			      (vap->iv_opmode == IEEE80211_M_AHDEMO &&
700 			       (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
701 				continue;
702 			bit = 1;
703 			break;
704 		}
705 	oflags = ifp->if_flags;
706 	if (bit)
707 		ifp->if_flags |= flag;
708 	else
709 		ifp->if_flags &= ~flag;
710 	if ((ifp->if_flags ^ oflags) & flag) {
711 		/* XXX should we return 1/0 and let caller do this? */
712 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
713 			if (flag == IFF_PROMISC)
714 				ieee80211_runtask(ic, &ic->ic_promisc_task);
715 			else if (flag == IFF_ALLMULTI)
716 				ieee80211_runtask(ic, &ic->ic_mcast_task);
717 		}
718 	}
719 }
720 
721 /*
722  * Synchronize flag bit state in the com structure
723  * according to the state of all vap's.  This is used,
724  * for example, to handle state changes via ioctls.
725  */
726 static void
727 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
728 {
729 	struct ieee80211vap *vap;
730 	int bit;
731 
732 	IEEE80211_LOCK_ASSERT(ic);
733 
734 	bit = 0;
735 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
736 		if (vap->iv_flags & flag) {
737 			bit = 1;
738 			break;
739 		}
740 	if (bit)
741 		ic->ic_flags |= flag;
742 	else
743 		ic->ic_flags &= ~flag;
744 }
745 
746 void
747 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
748 {
749 	struct ieee80211com *ic = vap->iv_ic;
750 
751 	IEEE80211_LOCK(ic);
752 	if (flag < 0) {
753 		flag = -flag;
754 		vap->iv_flags &= ~flag;
755 	} else
756 		vap->iv_flags |= flag;
757 	ieee80211_syncflag_locked(ic, flag);
758 	IEEE80211_UNLOCK(ic);
759 }
760 
761 /*
762  * Synchronize flags_ht bit state in the com structure
763  * according to the state of all vap's.  This is used,
764  * for example, to handle state changes via ioctls.
765  */
766 static void
767 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
768 {
769 	struct ieee80211vap *vap;
770 	int bit;
771 
772 	IEEE80211_LOCK_ASSERT(ic);
773 
774 	bit = 0;
775 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
776 		if (vap->iv_flags_ht & flag) {
777 			bit = 1;
778 			break;
779 		}
780 	if (bit)
781 		ic->ic_flags_ht |= flag;
782 	else
783 		ic->ic_flags_ht &= ~flag;
784 }
785 
786 void
787 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
788 {
789 	struct ieee80211com *ic = vap->iv_ic;
790 
791 	IEEE80211_LOCK(ic);
792 	if (flag < 0) {
793 		flag = -flag;
794 		vap->iv_flags_ht &= ~flag;
795 	} else
796 		vap->iv_flags_ht |= flag;
797 	ieee80211_syncflag_ht_locked(ic, flag);
798 	IEEE80211_UNLOCK(ic);
799 }
800 
801 /*
802  * Synchronize flags_ext bit state in the com structure
803  * according to the state of all vap's.  This is used,
804  * for example, to handle state changes via ioctls.
805  */
806 static void
807 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
808 {
809 	struct ieee80211vap *vap;
810 	int bit;
811 
812 	IEEE80211_LOCK_ASSERT(ic);
813 
814 	bit = 0;
815 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
816 		if (vap->iv_flags_ext & flag) {
817 			bit = 1;
818 			break;
819 		}
820 	if (bit)
821 		ic->ic_flags_ext |= flag;
822 	else
823 		ic->ic_flags_ext &= ~flag;
824 }
825 
826 void
827 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
828 {
829 	struct ieee80211com *ic = vap->iv_ic;
830 
831 	IEEE80211_LOCK(ic);
832 	if (flag < 0) {
833 		flag = -flag;
834 		vap->iv_flags_ext &= ~flag;
835 	} else
836 		vap->iv_flags_ext |= flag;
837 	ieee80211_syncflag_ext_locked(ic, flag);
838 	IEEE80211_UNLOCK(ic);
839 }
840 
841 static __inline int
842 mapgsm(u_int freq, u_int flags)
843 {
844 	freq *= 10;
845 	if (flags & IEEE80211_CHAN_QUARTER)
846 		freq += 5;
847 	else if (flags & IEEE80211_CHAN_HALF)
848 		freq += 10;
849 	else
850 		freq += 20;
851 	/* NB: there is no 907/20 wide but leave room */
852 	return (freq - 906*10) / 5;
853 }
854 
855 static __inline int
856 mappsb(u_int freq, u_int flags)
857 {
858 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
859 }
860 
861 /*
862  * Convert MHz frequency to IEEE channel number.
863  */
864 int
865 ieee80211_mhz2ieee(u_int freq, u_int flags)
866 {
867 #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
868 	if (flags & IEEE80211_CHAN_GSM)
869 		return mapgsm(freq, flags);
870 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
871 		if (freq == 2484)
872 			return 14;
873 		if (freq < 2484)
874 			return ((int) freq - 2407) / 5;
875 		else
876 			return 15 + ((freq - 2512) / 20);
877 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
878 		if (freq <= 5000) {
879 			/* XXX check regdomain? */
880 			if (IS_FREQ_IN_PSB(freq))
881 				return mappsb(freq, flags);
882 			return (freq - 4000) / 5;
883 		} else
884 			return (freq - 5000) / 5;
885 	} else {				/* either, guess */
886 		if (freq == 2484)
887 			return 14;
888 		if (freq < 2484) {
889 			if (907 <= freq && freq <= 922)
890 				return mapgsm(freq, flags);
891 			return ((int) freq - 2407) / 5;
892 		}
893 		if (freq < 5000) {
894 			if (IS_FREQ_IN_PSB(freq))
895 				return mappsb(freq, flags);
896 			else if (freq > 4900)
897 				return (freq - 4000) / 5;
898 			else
899 				return 15 + ((freq - 2512) / 20);
900 		}
901 		return (freq - 5000) / 5;
902 	}
903 #undef IS_FREQ_IN_PSB
904 }
905 
906 /*
907  * Convert channel to IEEE channel number.
908  */
909 int
910 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
911 {
912 	if (c == NULL) {
913 		ic_printf(ic, "invalid channel (NULL)\n");
914 		return 0;		/* XXX */
915 	}
916 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
917 }
918 
919 /*
920  * Convert IEEE channel number to MHz frequency.
921  */
922 u_int
923 ieee80211_ieee2mhz(u_int chan, u_int flags)
924 {
925 	if (flags & IEEE80211_CHAN_GSM)
926 		return 907 + 5 * (chan / 10);
927 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
928 		if (chan == 14)
929 			return 2484;
930 		if (chan < 14)
931 			return 2407 + chan*5;
932 		else
933 			return 2512 + ((chan-15)*20);
934 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
935 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
936 			chan -= 37;
937 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
938 		}
939 		return 5000 + (chan*5);
940 	} else {				/* either, guess */
941 		/* XXX can't distinguish PSB+GSM channels */
942 		if (chan == 14)
943 			return 2484;
944 		if (chan < 14)			/* 0-13 */
945 			return 2407 + chan*5;
946 		if (chan < 27)			/* 15-26 */
947 			return 2512 + ((chan-15)*20);
948 		return 5000 + (chan*5);
949 	}
950 }
951 
952 /*
953  * Locate a channel given a frequency+flags.  We cache
954  * the previous lookup to optimize switching between two
955  * channels--as happens with dynamic turbo.
956  */
957 struct ieee80211_channel *
958 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
959 {
960 	struct ieee80211_channel *c;
961 	int i;
962 
963 	flags &= IEEE80211_CHAN_ALLTURBO;
964 	c = ic->ic_prevchan;
965 	if (c != NULL && c->ic_freq == freq &&
966 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
967 		return c;
968 	/* brute force search */
969 	for (i = 0; i < ic->ic_nchans; i++) {
970 		c = &ic->ic_channels[i];
971 		if (c->ic_freq == freq &&
972 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
973 			return c;
974 	}
975 	return NULL;
976 }
977 
978 /*
979  * Locate a channel given a channel number+flags.  We cache
980  * the previous lookup to optimize switching between two
981  * channels--as happens with dynamic turbo.
982  */
983 struct ieee80211_channel *
984 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
985 {
986 	struct ieee80211_channel *c;
987 	int i;
988 
989 	flags &= IEEE80211_CHAN_ALLTURBO;
990 	c = ic->ic_prevchan;
991 	if (c != NULL && c->ic_ieee == ieee &&
992 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
993 		return c;
994 	/* brute force search */
995 	for (i = 0; i < ic->ic_nchans; i++) {
996 		c = &ic->ic_channels[i];
997 		if (c->ic_ieee == ieee &&
998 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
999 			return c;
1000 	}
1001 	return NULL;
1002 }
1003 
1004 /*
1005  * Lookup a channel suitable for the given rx status.
1006  *
1007  * This is used to find a channel for a frame (eg beacon, probe
1008  * response) based purely on the received PHY information.
1009  *
1010  * For now it tries to do it based on R_FREQ / R_IEEE.
1011  * This is enough for 11bg and 11a (and thus 11ng/11na)
1012  * but it will not be enough for GSM, PSB channels and the
1013  * like.  It also doesn't know about legacy-turbog and
1014  * legacy-turbo modes, which some offload NICs actually
1015  * support in weird ways.
1016  *
1017  * Takes the ic and rxstatus; returns the channel or NULL
1018  * if not found.
1019  *
1020  * XXX TODO: Add support for that when the need arises.
1021  */
1022 struct ieee80211_channel *
1023 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1024     const struct ieee80211_rx_stats *rxs)
1025 {
1026 	struct ieee80211com *ic = vap->iv_ic;
1027 	uint32_t flags;
1028 	struct ieee80211_channel *c;
1029 
1030 	if (rxs == NULL)
1031 		return (NULL);
1032 
1033 	/*
1034 	 * Strictly speaking we only use freq for now,
1035 	 * however later on we may wish to just store
1036 	 * the ieee for verification.
1037 	 */
1038 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1039 		return (NULL);
1040 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1041 		return (NULL);
1042 
1043 	/*
1044 	 * If the rx status contains a valid ieee/freq, then
1045 	 * ensure we populate the correct channel information
1046 	 * in rxchan before passing it up to the scan infrastructure.
1047 	 * Offload NICs will pass up beacons from all channels
1048 	 * during background scans.
1049 	 */
1050 
1051 	/* Determine a band */
1052 	/* XXX should be done by the driver? */
1053 	if (rxs->c_freq < 3000) {
1054 		flags = IEEE80211_CHAN_B;
1055 	} else {
1056 		flags = IEEE80211_CHAN_A;
1057 	}
1058 
1059 	/* Channel lookup */
1060 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1061 
1062 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1063 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1064 	    __func__,
1065 	    (int) rxs->c_freq,
1066 	    (int) rxs->c_ieee,
1067 	    flags,
1068 	    c);
1069 
1070 	return (c);
1071 }
1072 
1073 static void
1074 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1075 {
1076 #define	ADD(_ic, _s, _o) \
1077 	ifmedia_add(media, \
1078 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1079 	static const u_int mopts[IEEE80211_MODE_MAX] = {
1080 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1081 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1082 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1083 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1084 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1085 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1086 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1087 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1088 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1089 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1090 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1091 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1092 	};
1093 	u_int mopt;
1094 
1095 	mopt = mopts[mode];
1096 	if (addsta)
1097 		ADD(ic, mword, mopt);	/* STA mode has no cap */
1098 	if (caps & IEEE80211_C_IBSS)
1099 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1100 	if (caps & IEEE80211_C_HOSTAP)
1101 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1102 	if (caps & IEEE80211_C_AHDEMO)
1103 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1104 	if (caps & IEEE80211_C_MONITOR)
1105 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1106 	if (caps & IEEE80211_C_WDS)
1107 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1108 	if (caps & IEEE80211_C_MBSS)
1109 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1110 #undef ADD
1111 }
1112 
1113 /*
1114  * Setup the media data structures according to the channel and
1115  * rate tables.
1116  */
1117 static int
1118 ieee80211_media_setup(struct ieee80211com *ic,
1119 	struct ifmedia *media, int caps, int addsta,
1120 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1121 {
1122 	int i, j, rate, maxrate, mword, r;
1123 	enum ieee80211_phymode mode;
1124 	const struct ieee80211_rateset *rs;
1125 	struct ieee80211_rateset allrates;
1126 
1127 	/*
1128 	 * Fill in media characteristics.
1129 	 */
1130 	ifmedia_init(media, 0, media_change, media_stat);
1131 	maxrate = 0;
1132 	/*
1133 	 * Add media for legacy operating modes.
1134 	 */
1135 	memset(&allrates, 0, sizeof(allrates));
1136 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1137 		if (isclr(ic->ic_modecaps, mode))
1138 			continue;
1139 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1140 		if (mode == IEEE80211_MODE_AUTO)
1141 			continue;
1142 		rs = &ic->ic_sup_rates[mode];
1143 		for (i = 0; i < rs->rs_nrates; i++) {
1144 			rate = rs->rs_rates[i];
1145 			mword = ieee80211_rate2media(ic, rate, mode);
1146 			if (mword == 0)
1147 				continue;
1148 			addmedia(media, caps, addsta, mode, mword);
1149 			/*
1150 			 * Add legacy rate to the collection of all rates.
1151 			 */
1152 			r = rate & IEEE80211_RATE_VAL;
1153 			for (j = 0; j < allrates.rs_nrates; j++)
1154 				if (allrates.rs_rates[j] == r)
1155 					break;
1156 			if (j == allrates.rs_nrates) {
1157 				/* unique, add to the set */
1158 				allrates.rs_rates[j] = r;
1159 				allrates.rs_nrates++;
1160 			}
1161 			rate = (rate & IEEE80211_RATE_VAL) / 2;
1162 			if (rate > maxrate)
1163 				maxrate = rate;
1164 		}
1165 	}
1166 	for (i = 0; i < allrates.rs_nrates; i++) {
1167 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1168 				IEEE80211_MODE_AUTO);
1169 		if (mword == 0)
1170 			continue;
1171 		/* NB: remove media options from mword */
1172 		addmedia(media, caps, addsta,
1173 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1174 	}
1175 	/*
1176 	 * Add HT/11n media.  Note that we do not have enough
1177 	 * bits in the media subtype to express the MCS so we
1178 	 * use a "placeholder" media subtype and any fixed MCS
1179 	 * must be specified with a different mechanism.
1180 	 */
1181 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1182 		if (isclr(ic->ic_modecaps, mode))
1183 			continue;
1184 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1185 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1186 	}
1187 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1188 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1189 		addmedia(media, caps, addsta,
1190 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1191 		i = ic->ic_txstream * 8 - 1;
1192 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1193 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1194 			rate = ieee80211_htrates[i].ht40_rate_400ns;
1195 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1196 			rate = ieee80211_htrates[i].ht40_rate_800ns;
1197 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1198 			rate = ieee80211_htrates[i].ht20_rate_400ns;
1199 		else
1200 			rate = ieee80211_htrates[i].ht20_rate_800ns;
1201 		if (rate > maxrate)
1202 			maxrate = rate;
1203 	}
1204 	return maxrate;
1205 }
1206 
1207 void
1208 ieee80211_media_init(struct ieee80211com *ic)
1209 {
1210 	struct ifnet *ifp = ic->ic_ifp;
1211 	int maxrate;
1212 
1213 	/* NB: this works because the structure is initialized to zero */
1214 	if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1215 		/*
1216 		 * We are re-initializing the channel list; clear
1217 		 * the existing media state as the media routines
1218 		 * don't suppress duplicates.
1219 		 */
1220 		ifmedia_removeall(&ic->ic_media);
1221 	}
1222 	ieee80211_chan_init(ic);
1223 
1224 	/*
1225 	 * Recalculate media settings in case new channel list changes
1226 	 * the set of available modes.
1227 	 */
1228 	maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1229 		ieee80211com_media_change, ieee80211com_media_status);
1230 	/* NB: strip explicit mode; we're actually in autoselect */
1231 	ifmedia_set(&ic->ic_media,
1232 	    media_status(ic->ic_opmode, ic->ic_curchan) &~
1233 		(IFM_MMASK | IFM_IEEE80211_TURBO));
1234 	if (maxrate)
1235 		ifp->if_baudrate = IF_Mbps(maxrate);
1236 
1237 	/* XXX need to propagate new media settings to vap's */
1238 }
1239 
1240 /* XXX inline or eliminate? */
1241 const struct ieee80211_rateset *
1242 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1243 {
1244 	/* XXX does this work for 11ng basic rates? */
1245 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1246 }
1247 
1248 void
1249 ieee80211_announce(struct ieee80211com *ic)
1250 {
1251 	int i, rate, mword;
1252 	enum ieee80211_phymode mode;
1253 	const struct ieee80211_rateset *rs;
1254 
1255 	/* NB: skip AUTO since it has no rates */
1256 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1257 		if (isclr(ic->ic_modecaps, mode))
1258 			continue;
1259 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1260 		rs = &ic->ic_sup_rates[mode];
1261 		for (i = 0; i < rs->rs_nrates; i++) {
1262 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1263 			if (mword == 0)
1264 				continue;
1265 			rate = ieee80211_media2rate(mword);
1266 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1267 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1268 		}
1269 		printf("\n");
1270 	}
1271 	ieee80211_ht_announce(ic);
1272 }
1273 
1274 void
1275 ieee80211_announce_channels(struct ieee80211com *ic)
1276 {
1277 	const struct ieee80211_channel *c;
1278 	char type;
1279 	int i, cw;
1280 
1281 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1282 	for (i = 0; i < ic->ic_nchans; i++) {
1283 		c = &ic->ic_channels[i];
1284 		if (IEEE80211_IS_CHAN_ST(c))
1285 			type = 'S';
1286 		else if (IEEE80211_IS_CHAN_108A(c))
1287 			type = 'T';
1288 		else if (IEEE80211_IS_CHAN_108G(c))
1289 			type = 'G';
1290 		else if (IEEE80211_IS_CHAN_HT(c))
1291 			type = 'n';
1292 		else if (IEEE80211_IS_CHAN_A(c))
1293 			type = 'a';
1294 		else if (IEEE80211_IS_CHAN_ANYG(c))
1295 			type = 'g';
1296 		else if (IEEE80211_IS_CHAN_B(c))
1297 			type = 'b';
1298 		else
1299 			type = 'f';
1300 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1301 			cw = 40;
1302 		else if (IEEE80211_IS_CHAN_HALF(c))
1303 			cw = 10;
1304 		else if (IEEE80211_IS_CHAN_QUARTER(c))
1305 			cw = 5;
1306 		else
1307 			cw = 20;
1308 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1309 			, c->ic_ieee, c->ic_freq, type
1310 			, cw
1311 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1312 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1313 			, c->ic_maxregpower
1314 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1315 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1316 		);
1317 	}
1318 }
1319 
1320 static int
1321 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1322 {
1323 	switch (IFM_MODE(ime->ifm_media)) {
1324 	case IFM_IEEE80211_11A:
1325 		*mode = IEEE80211_MODE_11A;
1326 		break;
1327 	case IFM_IEEE80211_11B:
1328 		*mode = IEEE80211_MODE_11B;
1329 		break;
1330 	case IFM_IEEE80211_11G:
1331 		*mode = IEEE80211_MODE_11G;
1332 		break;
1333 	case IFM_IEEE80211_FH:
1334 		*mode = IEEE80211_MODE_FH;
1335 		break;
1336 	case IFM_IEEE80211_11NA:
1337 		*mode = IEEE80211_MODE_11NA;
1338 		break;
1339 	case IFM_IEEE80211_11NG:
1340 		*mode = IEEE80211_MODE_11NG;
1341 		break;
1342 	case IFM_AUTO:
1343 		*mode = IEEE80211_MODE_AUTO;
1344 		break;
1345 	default:
1346 		return 0;
1347 	}
1348 	/*
1349 	 * Turbo mode is an ``option''.
1350 	 * XXX does not apply to AUTO
1351 	 */
1352 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1353 		if (*mode == IEEE80211_MODE_11A) {
1354 			if (flags & IEEE80211_F_TURBOP)
1355 				*mode = IEEE80211_MODE_TURBO_A;
1356 			else
1357 				*mode = IEEE80211_MODE_STURBO_A;
1358 		} else if (*mode == IEEE80211_MODE_11G)
1359 			*mode = IEEE80211_MODE_TURBO_G;
1360 		else
1361 			return 0;
1362 	}
1363 	/* XXX HT40 +/- */
1364 	return 1;
1365 }
1366 
1367 /*
1368  * Handle a media change request on the underlying interface.
1369  */
1370 int
1371 ieee80211com_media_change(struct ifnet *ifp)
1372 {
1373 	return EINVAL;
1374 }
1375 
1376 /*
1377  * Handle a media change request on the vap interface.
1378  */
1379 int
1380 ieee80211_media_change(struct ifnet *ifp)
1381 {
1382 	struct ieee80211vap *vap = ifp->if_softc;
1383 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1384 	uint16_t newmode;
1385 
1386 	if (!media2mode(ime, vap->iv_flags, &newmode))
1387 		return EINVAL;
1388 	if (vap->iv_des_mode != newmode) {
1389 		vap->iv_des_mode = newmode;
1390 		/* XXX kick state machine if up+running */
1391 	}
1392 	return 0;
1393 }
1394 
1395 /*
1396  * Common code to calculate the media status word
1397  * from the operating mode and channel state.
1398  */
1399 static int
1400 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1401 {
1402 	int status;
1403 
1404 	status = IFM_IEEE80211;
1405 	switch (opmode) {
1406 	case IEEE80211_M_STA:
1407 		break;
1408 	case IEEE80211_M_IBSS:
1409 		status |= IFM_IEEE80211_ADHOC;
1410 		break;
1411 	case IEEE80211_M_HOSTAP:
1412 		status |= IFM_IEEE80211_HOSTAP;
1413 		break;
1414 	case IEEE80211_M_MONITOR:
1415 		status |= IFM_IEEE80211_MONITOR;
1416 		break;
1417 	case IEEE80211_M_AHDEMO:
1418 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1419 		break;
1420 	case IEEE80211_M_WDS:
1421 		status |= IFM_IEEE80211_WDS;
1422 		break;
1423 	case IEEE80211_M_MBSS:
1424 		status |= IFM_IEEE80211_MBSS;
1425 		break;
1426 	}
1427 	if (IEEE80211_IS_CHAN_HTA(chan)) {
1428 		status |= IFM_IEEE80211_11NA;
1429 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
1430 		status |= IFM_IEEE80211_11NG;
1431 	} else if (IEEE80211_IS_CHAN_A(chan)) {
1432 		status |= IFM_IEEE80211_11A;
1433 	} else if (IEEE80211_IS_CHAN_B(chan)) {
1434 		status |= IFM_IEEE80211_11B;
1435 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1436 		status |= IFM_IEEE80211_11G;
1437 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1438 		status |= IFM_IEEE80211_FH;
1439 	}
1440 	/* XXX else complain? */
1441 
1442 	if (IEEE80211_IS_CHAN_TURBO(chan))
1443 		status |= IFM_IEEE80211_TURBO;
1444 #if 0
1445 	if (IEEE80211_IS_CHAN_HT20(chan))
1446 		status |= IFM_IEEE80211_HT20;
1447 	if (IEEE80211_IS_CHAN_HT40(chan))
1448 		status |= IFM_IEEE80211_HT40;
1449 #endif
1450 	return status;
1451 }
1452 
1453 static void
1454 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1455 {
1456 	struct ieee80211com *ic = ifp->if_l2com;
1457 	struct ieee80211vap *vap;
1458 
1459 	imr->ifm_status = IFM_AVALID;
1460 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1461 		if (vap->iv_ifp->if_flags & IFF_UP) {
1462 			imr->ifm_status |= IFM_ACTIVE;
1463 			break;
1464 		}
1465 	imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1466 	if (imr->ifm_status & IFM_ACTIVE)
1467 		imr->ifm_current = imr->ifm_active;
1468 }
1469 
1470 void
1471 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1472 {
1473 	struct ieee80211vap *vap = ifp->if_softc;
1474 	struct ieee80211com *ic = vap->iv_ic;
1475 	enum ieee80211_phymode mode;
1476 
1477 	imr->ifm_status = IFM_AVALID;
1478 	/*
1479 	 * NB: use the current channel's mode to lock down a xmit
1480 	 * rate only when running; otherwise we may have a mismatch
1481 	 * in which case the rate will not be convertible.
1482 	 */
1483 	if (vap->iv_state == IEEE80211_S_RUN ||
1484 	    vap->iv_state == IEEE80211_S_SLEEP) {
1485 		imr->ifm_status |= IFM_ACTIVE;
1486 		mode = ieee80211_chan2mode(ic->ic_curchan);
1487 	} else
1488 		mode = IEEE80211_MODE_AUTO;
1489 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1490 	/*
1491 	 * Calculate a current rate if possible.
1492 	 */
1493 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1494 		/*
1495 		 * A fixed rate is set, report that.
1496 		 */
1497 		imr->ifm_active |= ieee80211_rate2media(ic,
1498 			vap->iv_txparms[mode].ucastrate, mode);
1499 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
1500 		/*
1501 		 * In station mode report the current transmit rate.
1502 		 */
1503 		imr->ifm_active |= ieee80211_rate2media(ic,
1504 			vap->iv_bss->ni_txrate, mode);
1505 	} else
1506 		imr->ifm_active |= IFM_AUTO;
1507 	if (imr->ifm_status & IFM_ACTIVE)
1508 		imr->ifm_current = imr->ifm_active;
1509 }
1510 
1511 /*
1512  * Set the current phy mode and recalculate the active channel
1513  * set based on the available channels for this mode.  Also
1514  * select a new default/current channel if the current one is
1515  * inappropriate for this mode.
1516  */
1517 int
1518 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1519 {
1520 	/*
1521 	 * Adjust basic rates in 11b/11g supported rate set.
1522 	 * Note that if operating on a hal/quarter rate channel
1523 	 * this is a noop as those rates sets are different
1524 	 * and used instead.
1525 	 */
1526 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1527 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1528 
1529 	ic->ic_curmode = mode;
1530 	ieee80211_reset_erp(ic);	/* reset ERP state */
1531 
1532 	return 0;
1533 }
1534 
1535 /*
1536  * Return the phy mode for with the specified channel.
1537  */
1538 enum ieee80211_phymode
1539 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1540 {
1541 
1542 	if (IEEE80211_IS_CHAN_HTA(chan))
1543 		return IEEE80211_MODE_11NA;
1544 	else if (IEEE80211_IS_CHAN_HTG(chan))
1545 		return IEEE80211_MODE_11NG;
1546 	else if (IEEE80211_IS_CHAN_108G(chan))
1547 		return IEEE80211_MODE_TURBO_G;
1548 	else if (IEEE80211_IS_CHAN_ST(chan))
1549 		return IEEE80211_MODE_STURBO_A;
1550 	else if (IEEE80211_IS_CHAN_TURBO(chan))
1551 		return IEEE80211_MODE_TURBO_A;
1552 	else if (IEEE80211_IS_CHAN_HALF(chan))
1553 		return IEEE80211_MODE_HALF;
1554 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
1555 		return IEEE80211_MODE_QUARTER;
1556 	else if (IEEE80211_IS_CHAN_A(chan))
1557 		return IEEE80211_MODE_11A;
1558 	else if (IEEE80211_IS_CHAN_ANYG(chan))
1559 		return IEEE80211_MODE_11G;
1560 	else if (IEEE80211_IS_CHAN_B(chan))
1561 		return IEEE80211_MODE_11B;
1562 	else if (IEEE80211_IS_CHAN_FHSS(chan))
1563 		return IEEE80211_MODE_FH;
1564 
1565 	/* NB: should not get here */
1566 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1567 		__func__, chan->ic_freq, chan->ic_flags);
1568 	return IEEE80211_MODE_11B;
1569 }
1570 
1571 struct ratemedia {
1572 	u_int	match;	/* rate + mode */
1573 	u_int	media;	/* if_media rate */
1574 };
1575 
1576 static int
1577 findmedia(const struct ratemedia rates[], int n, u_int match)
1578 {
1579 	int i;
1580 
1581 	for (i = 0; i < n; i++)
1582 		if (rates[i].match == match)
1583 			return rates[i].media;
1584 	return IFM_AUTO;
1585 }
1586 
1587 /*
1588  * Convert IEEE80211 rate value to ifmedia subtype.
1589  * Rate is either a legacy rate in units of 0.5Mbps
1590  * or an MCS index.
1591  */
1592 int
1593 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1594 {
1595 	static const struct ratemedia rates[] = {
1596 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1597 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1598 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1599 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1600 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1601 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1602 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1603 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1604 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1605 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1606 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1607 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1608 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1609 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1610 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1611 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1612 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1613 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1614 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1615 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1616 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1617 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1618 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1619 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1620 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1621 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1622 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1623 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1624 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1625 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1626 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
1627 	};
1628 	static const struct ratemedia htrates[] = {
1629 		{   0, IFM_IEEE80211_MCS },
1630 		{   1, IFM_IEEE80211_MCS },
1631 		{   2, IFM_IEEE80211_MCS },
1632 		{   3, IFM_IEEE80211_MCS },
1633 		{   4, IFM_IEEE80211_MCS },
1634 		{   5, IFM_IEEE80211_MCS },
1635 		{   6, IFM_IEEE80211_MCS },
1636 		{   7, IFM_IEEE80211_MCS },
1637 		{   8, IFM_IEEE80211_MCS },
1638 		{   9, IFM_IEEE80211_MCS },
1639 		{  10, IFM_IEEE80211_MCS },
1640 		{  11, IFM_IEEE80211_MCS },
1641 		{  12, IFM_IEEE80211_MCS },
1642 		{  13, IFM_IEEE80211_MCS },
1643 		{  14, IFM_IEEE80211_MCS },
1644 		{  15, IFM_IEEE80211_MCS },
1645 		{  16, IFM_IEEE80211_MCS },
1646 		{  17, IFM_IEEE80211_MCS },
1647 		{  18, IFM_IEEE80211_MCS },
1648 		{  19, IFM_IEEE80211_MCS },
1649 		{  20, IFM_IEEE80211_MCS },
1650 		{  21, IFM_IEEE80211_MCS },
1651 		{  22, IFM_IEEE80211_MCS },
1652 		{  23, IFM_IEEE80211_MCS },
1653 		{  24, IFM_IEEE80211_MCS },
1654 		{  25, IFM_IEEE80211_MCS },
1655 		{  26, IFM_IEEE80211_MCS },
1656 		{  27, IFM_IEEE80211_MCS },
1657 		{  28, IFM_IEEE80211_MCS },
1658 		{  29, IFM_IEEE80211_MCS },
1659 		{  30, IFM_IEEE80211_MCS },
1660 		{  31, IFM_IEEE80211_MCS },
1661 		{  32, IFM_IEEE80211_MCS },
1662 		{  33, IFM_IEEE80211_MCS },
1663 		{  34, IFM_IEEE80211_MCS },
1664 		{  35, IFM_IEEE80211_MCS },
1665 		{  36, IFM_IEEE80211_MCS },
1666 		{  37, IFM_IEEE80211_MCS },
1667 		{  38, IFM_IEEE80211_MCS },
1668 		{  39, IFM_IEEE80211_MCS },
1669 		{  40, IFM_IEEE80211_MCS },
1670 		{  41, IFM_IEEE80211_MCS },
1671 		{  42, IFM_IEEE80211_MCS },
1672 		{  43, IFM_IEEE80211_MCS },
1673 		{  44, IFM_IEEE80211_MCS },
1674 		{  45, IFM_IEEE80211_MCS },
1675 		{  46, IFM_IEEE80211_MCS },
1676 		{  47, IFM_IEEE80211_MCS },
1677 		{  48, IFM_IEEE80211_MCS },
1678 		{  49, IFM_IEEE80211_MCS },
1679 		{  50, IFM_IEEE80211_MCS },
1680 		{  51, IFM_IEEE80211_MCS },
1681 		{  52, IFM_IEEE80211_MCS },
1682 		{  53, IFM_IEEE80211_MCS },
1683 		{  54, IFM_IEEE80211_MCS },
1684 		{  55, IFM_IEEE80211_MCS },
1685 		{  56, IFM_IEEE80211_MCS },
1686 		{  57, IFM_IEEE80211_MCS },
1687 		{  58, IFM_IEEE80211_MCS },
1688 		{  59, IFM_IEEE80211_MCS },
1689 		{  60, IFM_IEEE80211_MCS },
1690 		{  61, IFM_IEEE80211_MCS },
1691 		{  62, IFM_IEEE80211_MCS },
1692 		{  63, IFM_IEEE80211_MCS },
1693 		{  64, IFM_IEEE80211_MCS },
1694 		{  65, IFM_IEEE80211_MCS },
1695 		{  66, IFM_IEEE80211_MCS },
1696 		{  67, IFM_IEEE80211_MCS },
1697 		{  68, IFM_IEEE80211_MCS },
1698 		{  69, IFM_IEEE80211_MCS },
1699 		{  70, IFM_IEEE80211_MCS },
1700 		{  71, IFM_IEEE80211_MCS },
1701 		{  72, IFM_IEEE80211_MCS },
1702 		{  73, IFM_IEEE80211_MCS },
1703 		{  74, IFM_IEEE80211_MCS },
1704 		{  75, IFM_IEEE80211_MCS },
1705 		{  76, IFM_IEEE80211_MCS },
1706 	};
1707 	int m;
1708 
1709 	/*
1710 	 * Check 11n rates first for match as an MCS.
1711 	 */
1712 	if (mode == IEEE80211_MODE_11NA) {
1713 		if (rate & IEEE80211_RATE_MCS) {
1714 			rate &= ~IEEE80211_RATE_MCS;
1715 			m = findmedia(htrates, nitems(htrates), rate);
1716 			if (m != IFM_AUTO)
1717 				return m | IFM_IEEE80211_11NA;
1718 		}
1719 	} else if (mode == IEEE80211_MODE_11NG) {
1720 		/* NB: 12 is ambiguous, it will be treated as an MCS */
1721 		if (rate & IEEE80211_RATE_MCS) {
1722 			rate &= ~IEEE80211_RATE_MCS;
1723 			m = findmedia(htrates, nitems(htrates), rate);
1724 			if (m != IFM_AUTO)
1725 				return m | IFM_IEEE80211_11NG;
1726 		}
1727 	}
1728 	rate &= IEEE80211_RATE_VAL;
1729 	switch (mode) {
1730 	case IEEE80211_MODE_11A:
1731 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
1732 	case IEEE80211_MODE_QUARTER:
1733 	case IEEE80211_MODE_11NA:
1734 	case IEEE80211_MODE_TURBO_A:
1735 	case IEEE80211_MODE_STURBO_A:
1736 		return findmedia(rates, nitems(rates),
1737 		    rate | IFM_IEEE80211_11A);
1738 	case IEEE80211_MODE_11B:
1739 		return findmedia(rates, nitems(rates),
1740 		    rate | IFM_IEEE80211_11B);
1741 	case IEEE80211_MODE_FH:
1742 		return findmedia(rates, nitems(rates),
1743 		    rate | IFM_IEEE80211_FH);
1744 	case IEEE80211_MODE_AUTO:
1745 		/* NB: ic may be NULL for some drivers */
1746 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1747 			return findmedia(rates, nitems(rates),
1748 			    rate | IFM_IEEE80211_FH);
1749 		/* NB: hack, 11g matches both 11b+11a rates */
1750 		/* fall thru... */
1751 	case IEEE80211_MODE_11G:
1752 	case IEEE80211_MODE_11NG:
1753 	case IEEE80211_MODE_TURBO_G:
1754 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1755 	}
1756 	return IFM_AUTO;
1757 }
1758 
1759 int
1760 ieee80211_media2rate(int mword)
1761 {
1762 	static const int ieeerates[] = {
1763 		-1,		/* IFM_AUTO */
1764 		0,		/* IFM_MANUAL */
1765 		0,		/* IFM_NONE */
1766 		2,		/* IFM_IEEE80211_FH1 */
1767 		4,		/* IFM_IEEE80211_FH2 */
1768 		2,		/* IFM_IEEE80211_DS1 */
1769 		4,		/* IFM_IEEE80211_DS2 */
1770 		11,		/* IFM_IEEE80211_DS5 */
1771 		22,		/* IFM_IEEE80211_DS11 */
1772 		44,		/* IFM_IEEE80211_DS22 */
1773 		12,		/* IFM_IEEE80211_OFDM6 */
1774 		18,		/* IFM_IEEE80211_OFDM9 */
1775 		24,		/* IFM_IEEE80211_OFDM12 */
1776 		36,		/* IFM_IEEE80211_OFDM18 */
1777 		48,		/* IFM_IEEE80211_OFDM24 */
1778 		72,		/* IFM_IEEE80211_OFDM36 */
1779 		96,		/* IFM_IEEE80211_OFDM48 */
1780 		108,		/* IFM_IEEE80211_OFDM54 */
1781 		144,		/* IFM_IEEE80211_OFDM72 */
1782 		0,		/* IFM_IEEE80211_DS354k */
1783 		0,		/* IFM_IEEE80211_DS512k */
1784 		6,		/* IFM_IEEE80211_OFDM3 */
1785 		9,		/* IFM_IEEE80211_OFDM4 */
1786 		54,		/* IFM_IEEE80211_OFDM27 */
1787 		-1,		/* IFM_IEEE80211_MCS */
1788 	};
1789 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1790 		ieeerates[IFM_SUBTYPE(mword)] : 0;
1791 }
1792 
1793 /*
1794  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1795  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1796  */
1797 #define	mix(a, b, c)							\
1798 do {									\
1799 	a -= b; a -= c; a ^= (c >> 13);					\
1800 	b -= c; b -= a; b ^= (a << 8);					\
1801 	c -= a; c -= b; c ^= (b >> 13);					\
1802 	a -= b; a -= c; a ^= (c >> 12);					\
1803 	b -= c; b -= a; b ^= (a << 16);					\
1804 	c -= a; c -= b; c ^= (b >> 5);					\
1805 	a -= b; a -= c; a ^= (c >> 3);					\
1806 	b -= c; b -= a; b ^= (a << 10);					\
1807 	c -= a; c -= b; c ^= (b >> 15);					\
1808 } while (/*CONSTCOND*/0)
1809 
1810 uint32_t
1811 ieee80211_mac_hash(const struct ieee80211com *ic,
1812 	const uint8_t addr[IEEE80211_ADDR_LEN])
1813 {
1814 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1815 
1816 	b += addr[5] << 8;
1817 	b += addr[4];
1818 	a += addr[3] << 24;
1819 	a += addr[2] << 16;
1820 	a += addr[1] << 8;
1821 	a += addr[0];
1822 
1823 	mix(a, b, c);
1824 
1825 	return c;
1826 }
1827 #undef mix
1828 
1829 char
1830 ieee80211_channel_type_char(const struct ieee80211_channel *c)
1831 {
1832 	if (IEEE80211_IS_CHAN_ST(c))
1833 		return 'S';
1834 	if (IEEE80211_IS_CHAN_108A(c))
1835 		return 'T';
1836 	if (IEEE80211_IS_CHAN_108G(c))
1837 		return 'G';
1838 	if (IEEE80211_IS_CHAN_HT(c))
1839 		return 'n';
1840 	if (IEEE80211_IS_CHAN_A(c))
1841 		return 'a';
1842 	if (IEEE80211_IS_CHAN_ANYG(c))
1843 		return 'g';
1844 	if (IEEE80211_IS_CHAN_B(c))
1845 		return 'b';
1846 	return 'f';
1847 }
1848