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