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