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