xref: /freebsd/sys/net80211/ieee80211.c (revision f4f33ea0c752ff0f9bfad34991d5bbb54e71133d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Atsushi Onoe
5  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * IEEE 802.11 generic handler
34  */
35 #include "opt_wlan.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/sbuf.h>
43 
44 #include <machine/stdarg.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/ethernet.h>
52 
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_regdomain.h>
55 #ifdef IEEE80211_SUPPORT_SUPERG
56 #include <net80211/ieee80211_superg.h>
57 #endif
58 #include <net80211/ieee80211_ratectl.h>
59 #include <net80211/ieee80211_vht.h>
60 
61 #include <net/bpf.h>
62 
63 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
64 	[IEEE80211_MODE_AUTO]	  = "auto",
65 	[IEEE80211_MODE_11A]	  = "11a",
66 	[IEEE80211_MODE_11B]	  = "11b",
67 	[IEEE80211_MODE_11G]	  = "11g",
68 	[IEEE80211_MODE_FH]	  = "FH",
69 	[IEEE80211_MODE_TURBO_A]  = "turboA",
70 	[IEEE80211_MODE_TURBO_G]  = "turboG",
71 	[IEEE80211_MODE_STURBO_A] = "sturboA",
72 	[IEEE80211_MODE_HALF]	  = "half",
73 	[IEEE80211_MODE_QUARTER]  = "quarter",
74 	[IEEE80211_MODE_11NA]	  = "11na",
75 	[IEEE80211_MODE_11NG]	  = "11ng",
76 	[IEEE80211_MODE_VHT_2GHZ]	  = "11acg",
77 	[IEEE80211_MODE_VHT_5GHZ]	  = "11ac",
78 };
79 /* map ieee80211_opmode to the corresponding capability bit */
80 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
81 	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
82 	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
83 	[IEEE80211_M_STA]	= IEEE80211_C_STA,
84 	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
85 	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
86 	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
87 #ifdef IEEE80211_SUPPORT_MESH
88 	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
89 #endif
90 };
91 
92 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
93 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
94 
95 static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
96 static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
97 static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
98 static	void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
99 static	int ieee80211_media_setup(struct ieee80211com *ic,
100 		struct ifmedia *media, int caps, int addsta,
101 		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
102 static	int media_status(enum ieee80211_opmode,
103 		const struct ieee80211_channel *);
104 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
105 
106 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
107 
108 /*
109  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
110  */
111 #define	B(r)	((r) | IEEE80211_RATE_BASIC)
112 static const struct ieee80211_rateset ieee80211_rateset_11a =
113 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
114 static const struct ieee80211_rateset ieee80211_rateset_half =
115 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
116 static const struct ieee80211_rateset ieee80211_rateset_quarter =
117 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
118 static const struct ieee80211_rateset ieee80211_rateset_11b =
119 	{ 4, { B(2), B(4), B(11), B(22) } };
120 /* NB: OFDM rates are handled specially based on mode */
121 static const struct ieee80211_rateset ieee80211_rateset_11g =
122 	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
123 #undef B
124 
125 static int set_vht_extchan(struct ieee80211_channel *c);
126 
127 /*
128  * Fill in 802.11 available channel set, mark
129  * all available channels as active, and pick
130  * a default channel if not already specified.
131  */
132 void
133 ieee80211_chan_init(struct ieee80211com *ic)
134 {
135 #define	DEFAULTRATES(m, def) do { \
136 	if (ic->ic_sup_rates[m].rs_nrates == 0) \
137 		ic->ic_sup_rates[m] = def; \
138 } while (0)
139 	struct ieee80211_channel *c;
140 	int i;
141 
142 	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
143 		("invalid number of channels specified: %u", ic->ic_nchans));
144 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
145 	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
146 	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
147 	for (i = 0; i < ic->ic_nchans; i++) {
148 		c = &ic->ic_channels[i];
149 		KASSERT(c->ic_flags != 0, ("channel with no flags"));
150 		/*
151 		 * Help drivers that work only with frequencies by filling
152 		 * in IEEE channel #'s if not already calculated.  Note this
153 		 * mimics similar work done in ieee80211_setregdomain when
154 		 * changing regulatory state.
155 		 */
156 		if (c->ic_ieee == 0)
157 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
158 
159 		/*
160 		 * Setup the HT40/VHT40 upper/lower bits.
161 		 * The VHT80 math is done elsewhere.
162 		 */
163 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
164 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
165 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
166 			    c->ic_flags);
167 
168 		/* Update VHT math */
169 		/*
170 		 * XXX VHT again, note that this assumes VHT80 channels
171 		 * are legit already
172 		 */
173 		set_vht_extchan(c);
174 
175 		/* default max tx power to max regulatory */
176 		if (c->ic_maxpower == 0)
177 			c->ic_maxpower = 2*c->ic_maxregpower;
178 		setbit(ic->ic_chan_avail, c->ic_ieee);
179 		/*
180 		 * Identify mode capabilities.
181 		 */
182 		if (IEEE80211_IS_CHAN_A(c))
183 			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
184 		if (IEEE80211_IS_CHAN_B(c))
185 			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
186 		if (IEEE80211_IS_CHAN_ANYG(c))
187 			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
188 		if (IEEE80211_IS_CHAN_FHSS(c))
189 			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
190 		if (IEEE80211_IS_CHAN_108A(c))
191 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
192 		if (IEEE80211_IS_CHAN_108G(c))
193 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
194 		if (IEEE80211_IS_CHAN_ST(c))
195 			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
196 		if (IEEE80211_IS_CHAN_HALF(c))
197 			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
198 		if (IEEE80211_IS_CHAN_QUARTER(c))
199 			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
200 		if (IEEE80211_IS_CHAN_HTA(c))
201 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
202 		if (IEEE80211_IS_CHAN_HTG(c))
203 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
204 		if (IEEE80211_IS_CHAN_VHTA(c))
205 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
206 		if (IEEE80211_IS_CHAN_VHTG(c))
207 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
208 	}
209 	/* initialize candidate channels to all available */
210 	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
211 		sizeof(ic->ic_chan_avail));
212 
213 	/* sort channel table to allow lookup optimizations */
214 	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
215 
216 	/* invalidate any previous state */
217 	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
218 	ic->ic_prevchan = NULL;
219 	ic->ic_csa_newchan = NULL;
220 	/* arbitrarily pick the first channel */
221 	ic->ic_curchan = &ic->ic_channels[0];
222 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
223 
224 	/* fillin well-known rate sets if driver has not specified */
225 	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
226 	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
227 	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
228 	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
229 	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
230 	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
231 	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
232 	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
233 	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
234 	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
235 	DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ,	 ieee80211_rateset_11g);
236 	DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ,	 ieee80211_rateset_11a);
237 
238 	/*
239 	 * Setup required information to fill the mcsset field, if driver did
240 	 * not. Assume a 2T2R setup for historic reasons.
241 	 */
242 	if (ic->ic_rxstream == 0)
243 		ic->ic_rxstream = 2;
244 	if (ic->ic_txstream == 0)
245 		ic->ic_txstream = 2;
246 
247 	ieee80211_init_suphtrates(ic);
248 
249 	/*
250 	 * Set auto mode to reset active channel state and any desired channel.
251 	 */
252 	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
253 #undef DEFAULTRATES
254 }
255 
256 static void
257 null_update_mcast(struct ieee80211com *ic)
258 {
259 
260 	ic_printf(ic, "need multicast update callback\n");
261 }
262 
263 static void
264 null_update_promisc(struct ieee80211com *ic)
265 {
266 
267 	ic_printf(ic, "need promiscuous mode update callback\n");
268 }
269 
270 static void
271 null_update_chw(struct ieee80211com *ic)
272 {
273 
274 	ic_printf(ic, "%s: need callback\n", __func__);
275 }
276 
277 int
278 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
279 {
280 	va_list ap;
281 	int retval;
282 
283 	retval = printf("%s: ", ic->ic_name);
284 	va_start(ap, fmt);
285 	retval += vprintf(fmt, ap);
286 	va_end(ap);
287 	return (retval);
288 }
289 
290 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
291 static struct mtx ic_list_mtx;
292 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
293 
294 static int
295 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
296 {
297 	struct ieee80211com *ic;
298 	struct sbuf sb;
299 	char *sp;
300 	int error;
301 
302 	error = sysctl_wire_old_buffer(req, 0);
303 	if (error)
304 		return (error);
305 	sbuf_new_for_sysctl(&sb, NULL, 8, req);
306 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
307 	sp = "";
308 	mtx_lock(&ic_list_mtx);
309 	LIST_FOREACH(ic, &ic_head, ic_next) {
310 		sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
311 		sp = " ";
312 	}
313 	mtx_unlock(&ic_list_mtx);
314 	error = sbuf_finish(&sb);
315 	sbuf_delete(&sb);
316 	return (error);
317 }
318 
319 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
320     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
321     sysctl_ieee80211coms, "A", "names of available 802.11 devices");
322 
323 /*
324  * Attach/setup the common net80211 state.  Called by
325  * the driver on attach to prior to creating any vap's.
326  */
327 void
328 ieee80211_ifattach(struct ieee80211com *ic)
329 {
330 
331 	IEEE80211_LOCK_INIT(ic, ic->ic_name);
332 	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
333 	TAILQ_INIT(&ic->ic_vaps);
334 
335 	/* Create a taskqueue for all state changes */
336 	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
337 	    taskqueue_thread_enqueue, &ic->ic_tq);
338 	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
339 	    ic->ic_name);
340 	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
341 	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
342 	/*
343 	 * Fill in 802.11 available channel set, mark all
344 	 * available channels as active, and pick a default
345 	 * channel if not already specified.
346 	 */
347 	ieee80211_chan_init(ic);
348 
349 	ic->ic_update_mcast = null_update_mcast;
350 	ic->ic_update_promisc = null_update_promisc;
351 	ic->ic_update_chw = null_update_chw;
352 
353 	ic->ic_hash_key = arc4random();
354 	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
355 	ic->ic_lintval = ic->ic_bintval;
356 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
357 
358 	ieee80211_crypto_attach(ic);
359 	ieee80211_node_attach(ic);
360 	ieee80211_power_attach(ic);
361 	ieee80211_proto_attach(ic);
362 #ifdef IEEE80211_SUPPORT_SUPERG
363 	ieee80211_superg_attach(ic);
364 #endif
365 	ieee80211_ht_attach(ic);
366 	ieee80211_vht_attach(ic);
367 	ieee80211_scan_attach(ic);
368 	ieee80211_regdomain_attach(ic);
369 	ieee80211_dfs_attach(ic);
370 
371 	ieee80211_sysctl_attach(ic);
372 
373 	mtx_lock(&ic_list_mtx);
374 	LIST_INSERT_HEAD(&ic_head, ic, ic_next);
375 	mtx_unlock(&ic_list_mtx);
376 }
377 
378 /*
379  * Detach net80211 state on device detach.  Tear down
380  * all vap's and reclaim all common state prior to the
381  * device state going away.  Note we may call back into
382  * driver; it must be prepared for this.
383  */
384 void
385 ieee80211_ifdetach(struct ieee80211com *ic)
386 {
387 	struct ieee80211vap *vap;
388 
389 	/*
390 	 * We use this as an indicator that ifattach never had a chance to be
391 	 * called, e.g. early driver attach failed and ifdetach was called
392 	 * during subsequent detach.  Never fear, for we have nothing to do
393 	 * here.
394 	 */
395 	if (ic->ic_tq == NULL)
396 		return;
397 
398 	mtx_lock(&ic_list_mtx);
399 	LIST_REMOVE(ic, ic_next);
400 	mtx_unlock(&ic_list_mtx);
401 
402 	taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
403 
404 	/*
405 	 * The VAP is responsible for setting and clearing
406 	 * the VIMAGE context.
407 	 */
408 	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
409 		ieee80211_vap_destroy(vap);
410 	ieee80211_waitfor_parent(ic);
411 
412 	ieee80211_sysctl_detach(ic);
413 	ieee80211_dfs_detach(ic);
414 	ieee80211_regdomain_detach(ic);
415 	ieee80211_scan_detach(ic);
416 #ifdef IEEE80211_SUPPORT_SUPERG
417 	ieee80211_superg_detach(ic);
418 #endif
419 	ieee80211_vht_detach(ic);
420 	ieee80211_ht_detach(ic);
421 	/* NB: must be called before ieee80211_node_detach */
422 	ieee80211_proto_detach(ic);
423 	ieee80211_crypto_detach(ic);
424 	ieee80211_power_detach(ic);
425 	ieee80211_node_detach(ic);
426 
427 	counter_u64_free(ic->ic_ierrors);
428 	counter_u64_free(ic->ic_oerrors);
429 
430 	taskqueue_free(ic->ic_tq);
431 	IEEE80211_TX_LOCK_DESTROY(ic);
432 	IEEE80211_LOCK_DESTROY(ic);
433 }
434 
435 struct ieee80211com *
436 ieee80211_find_com(const char *name)
437 {
438 	struct ieee80211com *ic;
439 
440 	mtx_lock(&ic_list_mtx);
441 	LIST_FOREACH(ic, &ic_head, ic_next)
442 		if (strcmp(ic->ic_name, name) == 0)
443 			break;
444 	mtx_unlock(&ic_list_mtx);
445 
446 	return (ic);
447 }
448 
449 void
450 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
451 {
452 	struct ieee80211com *ic;
453 
454 	mtx_lock(&ic_list_mtx);
455 	LIST_FOREACH(ic, &ic_head, ic_next)
456 		(*f)(arg, ic);
457 	mtx_unlock(&ic_list_mtx);
458 }
459 
460 /*
461  * Default reset method for use with the ioctl support.  This
462  * method is invoked after any state change in the 802.11
463  * layer that should be propagated to the hardware but not
464  * require re-initialization of the 802.11 state machine (e.g
465  * rescanning for an ap).  We always return ENETRESET which
466  * should cause the driver to re-initialize the device. Drivers
467  * can override this method to implement more optimized support.
468  */
469 static int
470 default_reset(struct ieee80211vap *vap, u_long cmd)
471 {
472 	return ENETRESET;
473 }
474 
475 /*
476  * Default for updating the VAP default TX key index.
477  *
478  * Drivers that support TX offload as well as hardware encryption offload
479  * may need to be informed of key index changes separate from the key
480  * update.
481  */
482 static void
483 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
484 {
485 
486 	/* XXX assert validity */
487 	/* XXX assert we're in a key update block */
488 	vap->iv_def_txkey = kid;
489 }
490 
491 /*
492  * Add underlying device errors to vap errors.
493  */
494 static uint64_t
495 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
496 {
497 	struct ieee80211vap *vap = ifp->if_softc;
498 	struct ieee80211com *ic = vap->iv_ic;
499 	uint64_t rv;
500 
501 	rv = if_get_counter_default(ifp, cnt);
502 	switch (cnt) {
503 	case IFCOUNTER_OERRORS:
504 		rv += counter_u64_fetch(ic->ic_oerrors);
505 		break;
506 	case IFCOUNTER_IERRORS:
507 		rv += counter_u64_fetch(ic->ic_ierrors);
508 		break;
509 	default:
510 		break;
511 	}
512 
513 	return (rv);
514 }
515 
516 /*
517  * Prepare a vap for use.  Drivers use this call to
518  * setup net80211 state in new vap's prior attaching
519  * them with ieee80211_vap_attach (below).
520  */
521 int
522 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
523     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
524     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
525 {
526 	struct ifnet *ifp;
527 
528 	ifp = if_alloc(IFT_ETHER);
529 	if (ifp == NULL) {
530 		ic_printf(ic, "%s: unable to allocate ifnet\n",
531 		    __func__);
532 		return ENOMEM;
533 	}
534 	if_initname(ifp, name, unit);
535 	ifp->if_softc = vap;			/* back pointer */
536 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
537 	ifp->if_transmit = ieee80211_vap_transmit;
538 	ifp->if_qflush = ieee80211_vap_qflush;
539 	ifp->if_ioctl = ieee80211_ioctl;
540 	ifp->if_init = ieee80211_init;
541 	ifp->if_get_counter = ieee80211_get_counter;
542 
543 	vap->iv_ifp = ifp;
544 	vap->iv_ic = ic;
545 	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
546 	vap->iv_flags_ext = ic->ic_flags_ext;
547 	vap->iv_flags_ven = ic->ic_flags_ven;
548 	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
549 
550 	/* 11n capabilities - XXX methodize */
551 	vap->iv_htcaps = ic->ic_htcaps;
552 	vap->iv_htextcaps = ic->ic_htextcaps;
553 
554 	/* 11ac capabilities - XXX methodize */
555 	vap->iv_vhtcaps = ic->ic_vhtcaps;
556 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
557 
558 	vap->iv_opmode = opmode;
559 	vap->iv_caps |= ieee80211_opcap[opmode];
560 	IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
561 	switch (opmode) {
562 	case IEEE80211_M_WDS:
563 		/*
564 		 * WDS links must specify the bssid of the far end.
565 		 * For legacy operation this is a static relationship.
566 		 * For non-legacy operation the station must associate
567 		 * and be authorized to pass traffic.  Plumbing the
568 		 * vap to the proper node happens when the vap
569 		 * transitions to RUN state.
570 		 */
571 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
572 		vap->iv_flags |= IEEE80211_F_DESBSSID;
573 		if (flags & IEEE80211_CLONE_WDSLEGACY)
574 			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
575 		break;
576 #ifdef IEEE80211_SUPPORT_TDMA
577 	case IEEE80211_M_AHDEMO:
578 		if (flags & IEEE80211_CLONE_TDMA) {
579 			/* NB: checked before clone operation allowed */
580 			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
581 			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
582 			/*
583 			 * Propagate TDMA capability to mark vap; this
584 			 * cannot be removed and is used to distinguish
585 			 * regular ahdemo operation from ahdemo+tdma.
586 			 */
587 			vap->iv_caps |= IEEE80211_C_TDMA;
588 		}
589 		break;
590 #endif
591 	default:
592 		break;
593 	}
594 	/* auto-enable s/w beacon miss support */
595 	if (flags & IEEE80211_CLONE_NOBEACONS)
596 		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
597 	/* auto-generated or user supplied MAC address */
598 	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
599 		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
600 	/*
601 	 * Enable various functionality by default if we're
602 	 * capable; the driver can override us if it knows better.
603 	 */
604 	if (vap->iv_caps & IEEE80211_C_WME)
605 		vap->iv_flags |= IEEE80211_F_WME;
606 	if (vap->iv_caps & IEEE80211_C_BURST)
607 		vap->iv_flags |= IEEE80211_F_BURST;
608 	/* NB: bg scanning only makes sense for station mode right now */
609 	if (vap->iv_opmode == IEEE80211_M_STA &&
610 	    (vap->iv_caps & IEEE80211_C_BGSCAN))
611 		vap->iv_flags |= IEEE80211_F_BGSCAN;
612 	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
613 	/* NB: DFS support only makes sense for ap mode right now */
614 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
615 	    (vap->iv_caps & IEEE80211_C_DFS))
616 		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
617 
618 	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
619 	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
620 	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
621 	/*
622 	 * Install a default reset method for the ioctl support;
623 	 * the driver can override this.
624 	 */
625 	vap->iv_reset = default_reset;
626 
627 	/*
628 	 * Install a default crypto key update method, the driver
629 	 * can override this.
630 	 */
631 	vap->iv_update_deftxkey = default_update_deftxkey;
632 
633 	ieee80211_sysctl_vattach(vap);
634 	ieee80211_crypto_vattach(vap);
635 	ieee80211_node_vattach(vap);
636 	ieee80211_power_vattach(vap);
637 	ieee80211_proto_vattach(vap);
638 #ifdef IEEE80211_SUPPORT_SUPERG
639 	ieee80211_superg_vattach(vap);
640 #endif
641 	ieee80211_ht_vattach(vap);
642 	ieee80211_vht_vattach(vap);
643 	ieee80211_scan_vattach(vap);
644 	ieee80211_regdomain_vattach(vap);
645 	ieee80211_radiotap_vattach(vap);
646 	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
647 
648 	return 0;
649 }
650 
651 /*
652  * Activate a vap.  State should have been prepared with a
653  * call to ieee80211_vap_setup and by the driver.  On return
654  * from this call the vap is ready for use.
655  */
656 int
657 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
658     ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
659 {
660 	struct ifnet *ifp = vap->iv_ifp;
661 	struct ieee80211com *ic = vap->iv_ic;
662 	struct ifmediareq imr;
663 	int maxrate;
664 
665 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
666 	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
667 	    __func__, ieee80211_opmode_name[vap->iv_opmode],
668 	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
669 
670 	/*
671 	 * Do late attach work that cannot happen until after
672 	 * the driver has had a chance to override defaults.
673 	 */
674 	ieee80211_node_latevattach(vap);
675 	ieee80211_power_latevattach(vap);
676 
677 	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
678 	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
679 	ieee80211_media_status(ifp, &imr);
680 	/* NB: strip explicit mode; we're actually in autoselect */
681 	ifmedia_set(&vap->iv_media,
682 	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
683 	if (maxrate)
684 		ifp->if_baudrate = IF_Mbps(maxrate);
685 
686 	ether_ifattach(ifp, macaddr);
687 	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
688 	/* hook output method setup by ether_ifattach */
689 	vap->iv_output = ifp->if_output;
690 	ifp->if_output = ieee80211_output;
691 	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
692 
693 	IEEE80211_LOCK(ic);
694 	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
695 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
696 #ifdef IEEE80211_SUPPORT_SUPERG
697 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
698 #endif
699 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
700 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
701 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
702 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
703 
704 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
705 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
706 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
707 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
708 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
709 	IEEE80211_UNLOCK(ic);
710 
711 	return 1;
712 }
713 
714 /*
715  * Tear down vap state and reclaim the ifnet.
716  * The driver is assumed to have prepared for
717  * this; e.g. by turning off interrupts for the
718  * underlying device.
719  */
720 void
721 ieee80211_vap_detach(struct ieee80211vap *vap)
722 {
723 	struct ieee80211com *ic = vap->iv_ic;
724 	struct ifnet *ifp = vap->iv_ifp;
725 
726 	CURVNET_SET(ifp->if_vnet);
727 
728 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
729 	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
730 
731 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
732 	ether_ifdetach(ifp);
733 
734 	ieee80211_stop(vap);
735 
736 	/*
737 	 * Flush any deferred vap tasks.
738 	 */
739 	ieee80211_draintask(ic, &vap->iv_nstate_task);
740 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
741 	ieee80211_draintask(ic, &vap->iv_wme_task);
742 	ieee80211_draintask(ic, &ic->ic_parent_task);
743 
744 	/* XXX band-aid until ifnet handles this for us */
745 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
746 
747 	IEEE80211_LOCK(ic);
748 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
749 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
750 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
751 #ifdef IEEE80211_SUPPORT_SUPERG
752 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
753 #endif
754 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
755 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
756 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
757 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
758 
759 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
760 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
761 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
762 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
763 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
764 
765 	/* NB: this handles the bpfdetach done below */
766 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
767 	if (vap->iv_ifflags & IFF_PROMISC)
768 		ieee80211_promisc(vap, false);
769 	if (vap->iv_ifflags & IFF_ALLMULTI)
770 		ieee80211_allmulti(vap, false);
771 	IEEE80211_UNLOCK(ic);
772 
773 	ifmedia_removeall(&vap->iv_media);
774 
775 	ieee80211_radiotap_vdetach(vap);
776 	ieee80211_regdomain_vdetach(vap);
777 	ieee80211_scan_vdetach(vap);
778 #ifdef IEEE80211_SUPPORT_SUPERG
779 	ieee80211_superg_vdetach(vap);
780 #endif
781 	ieee80211_vht_vdetach(vap);
782 	ieee80211_ht_vdetach(vap);
783 	/* NB: must be before ieee80211_node_vdetach */
784 	ieee80211_proto_vdetach(vap);
785 	ieee80211_crypto_vdetach(vap);
786 	ieee80211_power_vdetach(vap);
787 	ieee80211_node_vdetach(vap);
788 	ieee80211_sysctl_vdetach(vap);
789 
790 	if_free(ifp);
791 
792 	CURVNET_RESTORE();
793 }
794 
795 /*
796  * Count number of vaps in promisc, and issue promisc on
797  * parent respectively.
798  */
799 void
800 ieee80211_promisc(struct ieee80211vap *vap, bool on)
801 {
802 	struct ieee80211com *ic = vap->iv_ic;
803 
804 	IEEE80211_LOCK_ASSERT(ic);
805 
806 	if (on) {
807 		if (++ic->ic_promisc == 1)
808 			ieee80211_runtask(ic, &ic->ic_promisc_task);
809 	} else {
810 		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
811 		    __func__, ic));
812 		if (--ic->ic_promisc == 0)
813 			ieee80211_runtask(ic, &ic->ic_promisc_task);
814 	}
815 }
816 
817 /*
818  * Count number of vaps in allmulti, and issue allmulti on
819  * parent respectively.
820  */
821 void
822 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
823 {
824 	struct ieee80211com *ic = vap->iv_ic;
825 
826 	IEEE80211_LOCK_ASSERT(ic);
827 
828 	if (on) {
829 		if (++ic->ic_allmulti == 1)
830 			ieee80211_runtask(ic, &ic->ic_mcast_task);
831 	} else {
832 		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
833 		    __func__, ic));
834 		if (--ic->ic_allmulti == 0)
835 			ieee80211_runtask(ic, &ic->ic_mcast_task);
836 	}
837 }
838 
839 /*
840  * Synchronize flag bit state in the com structure
841  * according to the state of all vap's.  This is used,
842  * for example, to handle state changes via ioctls.
843  */
844 static void
845 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
846 {
847 	struct ieee80211vap *vap;
848 	int bit;
849 
850 	IEEE80211_LOCK_ASSERT(ic);
851 
852 	bit = 0;
853 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
854 		if (vap->iv_flags & flag) {
855 			bit = 1;
856 			break;
857 		}
858 	if (bit)
859 		ic->ic_flags |= flag;
860 	else
861 		ic->ic_flags &= ~flag;
862 }
863 
864 void
865 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
866 {
867 	struct ieee80211com *ic = vap->iv_ic;
868 
869 	IEEE80211_LOCK(ic);
870 	if (flag < 0) {
871 		flag = -flag;
872 		vap->iv_flags &= ~flag;
873 	} else
874 		vap->iv_flags |= flag;
875 	ieee80211_syncflag_locked(ic, flag);
876 	IEEE80211_UNLOCK(ic);
877 }
878 
879 /*
880  * Synchronize flags_ht bit state in the com structure
881  * according to the state of all vap's.  This is used,
882  * for example, to handle state changes via ioctls.
883  */
884 static void
885 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
886 {
887 	struct ieee80211vap *vap;
888 	int bit;
889 
890 	IEEE80211_LOCK_ASSERT(ic);
891 
892 	bit = 0;
893 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
894 		if (vap->iv_flags_ht & flag) {
895 			bit = 1;
896 			break;
897 		}
898 	if (bit)
899 		ic->ic_flags_ht |= flag;
900 	else
901 		ic->ic_flags_ht &= ~flag;
902 }
903 
904 void
905 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
906 {
907 	struct ieee80211com *ic = vap->iv_ic;
908 
909 	IEEE80211_LOCK(ic);
910 	if (flag < 0) {
911 		flag = -flag;
912 		vap->iv_flags_ht &= ~flag;
913 	} else
914 		vap->iv_flags_ht |= flag;
915 	ieee80211_syncflag_ht_locked(ic, flag);
916 	IEEE80211_UNLOCK(ic);
917 }
918 
919 /*
920  * Synchronize flags_vht bit state in the com structure
921  * according to the state of all vap's.  This is used,
922  * for example, to handle state changes via ioctls.
923  */
924 static void
925 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
926 {
927 	struct ieee80211vap *vap;
928 	int bit;
929 
930 	IEEE80211_LOCK_ASSERT(ic);
931 
932 	bit = 0;
933 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
934 		if (vap->iv_flags_vht & flag) {
935 			bit = 1;
936 			break;
937 		}
938 	if (bit)
939 		ic->ic_flags_vht |= flag;
940 	else
941 		ic->ic_flags_vht &= ~flag;
942 }
943 
944 void
945 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
946 {
947 	struct ieee80211com *ic = vap->iv_ic;
948 
949 	IEEE80211_LOCK(ic);
950 	if (flag < 0) {
951 		flag = -flag;
952 		vap->iv_flags_vht &= ~flag;
953 	} else
954 		vap->iv_flags_vht |= flag;
955 	ieee80211_syncflag_vht_locked(ic, flag);
956 	IEEE80211_UNLOCK(ic);
957 }
958 
959 /*
960  * Synchronize flags_ext bit state in the com structure
961  * according to the state of all vap's.  This is used,
962  * for example, to handle state changes via ioctls.
963  */
964 static void
965 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
966 {
967 	struct ieee80211vap *vap;
968 	int bit;
969 
970 	IEEE80211_LOCK_ASSERT(ic);
971 
972 	bit = 0;
973 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
974 		if (vap->iv_flags_ext & flag) {
975 			bit = 1;
976 			break;
977 		}
978 	if (bit)
979 		ic->ic_flags_ext |= flag;
980 	else
981 		ic->ic_flags_ext &= ~flag;
982 }
983 
984 void
985 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
986 {
987 	struct ieee80211com *ic = vap->iv_ic;
988 
989 	IEEE80211_LOCK(ic);
990 	if (flag < 0) {
991 		flag = -flag;
992 		vap->iv_flags_ext &= ~flag;
993 	} else
994 		vap->iv_flags_ext |= flag;
995 	ieee80211_syncflag_ext_locked(ic, flag);
996 	IEEE80211_UNLOCK(ic);
997 }
998 
999 static __inline int
1000 mapgsm(u_int freq, u_int flags)
1001 {
1002 	freq *= 10;
1003 	if (flags & IEEE80211_CHAN_QUARTER)
1004 		freq += 5;
1005 	else if (flags & IEEE80211_CHAN_HALF)
1006 		freq += 10;
1007 	else
1008 		freq += 20;
1009 	/* NB: there is no 907/20 wide but leave room */
1010 	return (freq - 906*10) / 5;
1011 }
1012 
1013 static __inline int
1014 mappsb(u_int freq, u_int flags)
1015 {
1016 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1017 }
1018 
1019 /*
1020  * Convert MHz frequency to IEEE channel number.
1021  */
1022 int
1023 ieee80211_mhz2ieee(u_int freq, u_int flags)
1024 {
1025 #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1026 	if (flags & IEEE80211_CHAN_GSM)
1027 		return mapgsm(freq, flags);
1028 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
1029 		if (freq == 2484)
1030 			return 14;
1031 		if (freq < 2484)
1032 			return ((int) freq - 2407) / 5;
1033 		else
1034 			return 15 + ((freq - 2512) / 20);
1035 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
1036 		if (freq <= 5000) {
1037 			/* XXX check regdomain? */
1038 			if (IS_FREQ_IN_PSB(freq))
1039 				return mappsb(freq, flags);
1040 			return (freq - 4000) / 5;
1041 		} else
1042 			return (freq - 5000) / 5;
1043 	} else {				/* either, guess */
1044 		if (freq == 2484)
1045 			return 14;
1046 		if (freq < 2484) {
1047 			if (907 <= freq && freq <= 922)
1048 				return mapgsm(freq, flags);
1049 			return ((int) freq - 2407) / 5;
1050 		}
1051 		if (freq < 5000) {
1052 			if (IS_FREQ_IN_PSB(freq))
1053 				return mappsb(freq, flags);
1054 			else if (freq > 4900)
1055 				return (freq - 4000) / 5;
1056 			else
1057 				return 15 + ((freq - 2512) / 20);
1058 		}
1059 		return (freq - 5000) / 5;
1060 	}
1061 #undef IS_FREQ_IN_PSB
1062 }
1063 
1064 /*
1065  * Convert channel to IEEE channel number.
1066  */
1067 int
1068 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1069 {
1070 	if (c == NULL) {
1071 		ic_printf(ic, "invalid channel (NULL)\n");
1072 		return 0;		/* XXX */
1073 	}
1074 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
1075 }
1076 
1077 /*
1078  * Convert IEEE channel number to MHz frequency.
1079  */
1080 u_int
1081 ieee80211_ieee2mhz(u_int chan, u_int flags)
1082 {
1083 	if (flags & IEEE80211_CHAN_GSM)
1084 		return 907 + 5 * (chan / 10);
1085 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
1086 		if (chan == 14)
1087 			return 2484;
1088 		if (chan < 14)
1089 			return 2407 + chan*5;
1090 		else
1091 			return 2512 + ((chan-15)*20);
1092 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1093 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1094 			chan -= 37;
1095 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1096 		}
1097 		return 5000 + (chan*5);
1098 	} else {				/* either, guess */
1099 		/* XXX can't distinguish PSB+GSM channels */
1100 		if (chan == 14)
1101 			return 2484;
1102 		if (chan < 14)			/* 0-13 */
1103 			return 2407 + chan*5;
1104 		if (chan < 27)			/* 15-26 */
1105 			return 2512 + ((chan-15)*20);
1106 		return 5000 + (chan*5);
1107 	}
1108 }
1109 
1110 static __inline void
1111 set_extchan(struct ieee80211_channel *c)
1112 {
1113 
1114 	/*
1115 	 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1116 	 * "the secondary channel number shall be 'N + [1,-1] * 4'
1117 	 */
1118 	if (c->ic_flags & IEEE80211_CHAN_HT40U)
1119 		c->ic_extieee = c->ic_ieee + 4;
1120 	else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1121 		c->ic_extieee = c->ic_ieee - 4;
1122 	else
1123 		c->ic_extieee = 0;
1124 }
1125 
1126 /*
1127  * Populate the freq1/freq2 fields as appropriate for VHT channels.
1128  *
1129  * This for now uses a hard-coded list of 80MHz wide channels.
1130  *
1131  * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
1132  * wide channel we've already decided upon.
1133  *
1134  * For VHT80 and VHT160, there are only a small number of fixed
1135  * 80/160MHz wide channels, so we just use those.
1136  *
1137  * This is all likely very very wrong - both the regulatory code
1138  * and this code needs to ensure that all four channels are
1139  * available and valid before the VHT80 (and eight for VHT160) channel
1140  * is created.
1141  */
1142 
1143 struct vht_chan_range {
1144 	uint16_t freq_start;
1145 	uint16_t freq_end;
1146 };
1147 
1148 struct vht_chan_range vht80_chan_ranges[] = {
1149 	{ 5170, 5250 },
1150 	{ 5250, 5330 },
1151 	{ 5490, 5570 },
1152 	{ 5570, 5650 },
1153 	{ 5650, 5730 },
1154 	{ 5735, 5815 },
1155 	{ 0, 0, }
1156 };
1157 
1158 static int
1159 set_vht_extchan(struct ieee80211_channel *c)
1160 {
1161 	int i;
1162 
1163 	if (! IEEE80211_IS_CHAN_VHT(c)) {
1164 		return (0);
1165 	}
1166 
1167 	if (IEEE80211_IS_CHAN_VHT20(c)) {
1168 		c->ic_vht_ch_freq1 = c->ic_ieee;
1169 		return (1);
1170 	}
1171 
1172 	if (IEEE80211_IS_CHAN_VHT40(c)) {
1173 		if (IEEE80211_IS_CHAN_HT40U(c))
1174 			c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1175 		else if (IEEE80211_IS_CHAN_HT40D(c))
1176 			c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1177 		else
1178 			return (0);
1179 		return (1);
1180 	}
1181 
1182 	if (IEEE80211_IS_CHAN_VHT80(c)) {
1183 		for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1184 			if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
1185 			    c->ic_freq < vht80_chan_ranges[i].freq_end) {
1186 				int midpoint;
1187 
1188 				midpoint = vht80_chan_ranges[i].freq_start + 40;
1189 				c->ic_vht_ch_freq1 =
1190 				    ieee80211_mhz2ieee(midpoint, c->ic_flags);
1191 				c->ic_vht_ch_freq2 = 0;
1192 #if 0
1193 				printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1194 				    __func__, c->ic_ieee, c->ic_freq, midpoint,
1195 				    c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1196 #endif
1197 				return (1);
1198 			}
1199 		}
1200 		return (0);
1201 	}
1202 
1203 	printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1204 	    __func__,
1205 	    c->ic_ieee,
1206 	    c->ic_flags);
1207 
1208 	return (0);
1209 }
1210 
1211 /*
1212  * Return whether the current channel could possibly be a part of
1213  * a VHT80 channel.
1214  *
1215  * This doesn't check that the whole range is in the allowed list
1216  * according to regulatory.
1217  */
1218 static int
1219 is_vht80_valid_freq(uint16_t freq)
1220 {
1221 	int i;
1222 	for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1223 		if (freq >= vht80_chan_ranges[i].freq_start &&
1224 		    freq < vht80_chan_ranges[i].freq_end)
1225 			return (1);
1226 	}
1227 	return (0);
1228 }
1229 
1230 static int
1231 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1232     uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1233 {
1234 	struct ieee80211_channel *c;
1235 
1236 	if (*nchans >= maxchans)
1237 		return (ENOBUFS);
1238 
1239 #if 0
1240 	printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
1241 	    __func__,
1242 	    *nchans,
1243 	    ieee,
1244 	    freq,
1245 	    flags);
1246 #endif
1247 
1248 	c = &chans[(*nchans)++];
1249 	c->ic_ieee = ieee;
1250 	c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1251 	c->ic_maxregpower = maxregpower;
1252 	c->ic_maxpower = 2 * maxregpower;
1253 	c->ic_flags = flags;
1254 	c->ic_vht_ch_freq1 = 0;
1255 	c->ic_vht_ch_freq2 = 0;
1256 	set_extchan(c);
1257 	set_vht_extchan(c);
1258 
1259 	return (0);
1260 }
1261 
1262 static int
1263 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1264     uint32_t flags)
1265 {
1266 	struct ieee80211_channel *c;
1267 
1268 	KASSERT(*nchans > 0, ("channel list is empty\n"));
1269 
1270 	if (*nchans >= maxchans)
1271 		return (ENOBUFS);
1272 
1273 #if 0
1274 	printf("%s: %d: flags=0x%08x\n",
1275 	    __func__,
1276 	    *nchans,
1277 	    flags);
1278 #endif
1279 
1280 	c = &chans[(*nchans)++];
1281 	c[0] = c[-1];
1282 	c->ic_flags = flags;
1283 	c->ic_vht_ch_freq1 = 0;
1284 	c->ic_vht_ch_freq2 = 0;
1285 	set_extchan(c);
1286 	set_vht_extchan(c);
1287 
1288 	return (0);
1289 }
1290 
1291 /*
1292  * XXX VHT-2GHz
1293  */
1294 static void
1295 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1296 {
1297 	int nmodes;
1298 
1299 	nmodes = 0;
1300 	if (isset(bands, IEEE80211_MODE_11B))
1301 		flags[nmodes++] = IEEE80211_CHAN_B;
1302 	if (isset(bands, IEEE80211_MODE_11G))
1303 		flags[nmodes++] = IEEE80211_CHAN_G;
1304 	if (isset(bands, IEEE80211_MODE_11NG))
1305 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1306 	if (ht40) {
1307 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1308 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1309 	}
1310 	flags[nmodes] = 0;
1311 }
1312 
1313 static void
1314 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1315 {
1316 	int nmodes;
1317 
1318 	/*
1319 	 * the addchan_list function seems to expect the flags array to
1320 	 * be in channel width order, so the VHT bits are interspersed
1321 	 * as appropriate to maintain said order.
1322 	 *
1323 	 * It also assumes HT40U is before HT40D.
1324 	 */
1325 	nmodes = 0;
1326 
1327 	/* 20MHz */
1328 	if (isset(bands, IEEE80211_MODE_11A))
1329 		flags[nmodes++] = IEEE80211_CHAN_A;
1330 	if (isset(bands, IEEE80211_MODE_11NA))
1331 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1332 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1333 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
1334 		    IEEE80211_CHAN_VHT20;
1335 	}
1336 
1337 	/* 40MHz */
1338 	if (ht40) {
1339 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1340 	}
1341 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1342 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
1343 		    | IEEE80211_CHAN_VHT40U;
1344 	}
1345 	if (ht40) {
1346 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1347 	}
1348 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1349 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
1350 		    | IEEE80211_CHAN_VHT40D;
1351 	}
1352 
1353 	/* 80MHz */
1354 	if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1355 		flags[nmodes++] = IEEE80211_CHAN_A |
1356 		    IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80;
1357 		flags[nmodes++] = IEEE80211_CHAN_A |
1358 		    IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80;
1359 	}
1360 
1361 	/* XXX VHT80+80 */
1362 	/* XXX VHT160 */
1363 	flags[nmodes] = 0;
1364 }
1365 
1366 static void
1367 getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1368 {
1369 
1370 	flags[0] = 0;
1371 	if (isset(bands, IEEE80211_MODE_11A) ||
1372 	    isset(bands, IEEE80211_MODE_11NA) ||
1373 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1374 		if (isset(bands, IEEE80211_MODE_11B) ||
1375 		    isset(bands, IEEE80211_MODE_11G) ||
1376 		    isset(bands, IEEE80211_MODE_11NG) ||
1377 		    isset(bands, IEEE80211_MODE_VHT_2GHZ))
1378 			return;
1379 
1380 		getflags_5ghz(bands, flags, ht40, vht80);
1381 	} else
1382 		getflags_2ghz(bands, flags, ht40);
1383 }
1384 
1385 /*
1386  * Add one 20 MHz channel into specified channel list.
1387  */
1388 /* XXX VHT */
1389 int
1390 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1391     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1392     uint32_t chan_flags, const uint8_t bands[])
1393 {
1394 	uint32_t flags[IEEE80211_MODE_MAX];
1395 	int i, error;
1396 
1397 	getflags(bands, flags, 0, 0);
1398 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1399 
1400 	error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1401 	    flags[0] | chan_flags);
1402 	for (i = 1; flags[i] != 0 && error == 0; i++) {
1403 		error = copychan_prev(chans, maxchans, nchans,
1404 		    flags[i] | chan_flags);
1405 	}
1406 
1407 	return (error);
1408 }
1409 
1410 static struct ieee80211_channel *
1411 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1412     uint32_t flags)
1413 {
1414 	struct ieee80211_channel *c;
1415 	int i;
1416 
1417 	flags &= IEEE80211_CHAN_ALLTURBO;
1418 	/* brute force search */
1419 	for (i = 0; i < nchans; i++) {
1420 		c = &chans[i];
1421 		if (c->ic_freq == freq &&
1422 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1423 			return c;
1424 	}
1425 	return NULL;
1426 }
1427 
1428 /*
1429  * Add 40 MHz channel pair into specified channel list.
1430  */
1431 /* XXX VHT */
1432 int
1433 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1434     int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1435 {
1436 	struct ieee80211_channel *cent, *extc;
1437 	uint16_t freq;
1438 	int error;
1439 
1440 	freq = ieee80211_ieee2mhz(ieee, flags);
1441 
1442 	/*
1443 	 * Each entry defines an HT40 channel pair; find the
1444 	 * center channel, then the extension channel above.
1445 	 */
1446 	flags |= IEEE80211_CHAN_HT20;
1447 	cent = findchannel(chans, *nchans, freq, flags);
1448 	if (cent == NULL)
1449 		return (EINVAL);
1450 
1451 	extc = findchannel(chans, *nchans, freq + 20, flags);
1452 	if (extc == NULL)
1453 		return (ENOENT);
1454 
1455 	flags &= ~IEEE80211_CHAN_HT;
1456 	error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1457 	    maxregpower, flags | IEEE80211_CHAN_HT40U);
1458 	if (error != 0)
1459 		return (error);
1460 
1461 	error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1462 	    maxregpower, flags | IEEE80211_CHAN_HT40D);
1463 
1464 	return (error);
1465 }
1466 
1467 /*
1468  * Fetch the center frequency for the primary channel.
1469  */
1470 uint32_t
1471 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1472 {
1473 
1474 	return (c->ic_freq);
1475 }
1476 
1477 /*
1478  * Fetch the center frequency for the primary BAND channel.
1479  *
1480  * For 5, 10, 20MHz channels it'll be the normally configured channel
1481  * frequency.
1482  *
1483  * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
1484  * wide channel, not the centre of the primary channel (that's ic_freq).
1485  *
1486  * For 80+80MHz channels this will be the centre of the primary
1487  * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1488  */
1489 uint32_t
1490 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1491 {
1492 
1493 	/*
1494 	 * VHT - use the pre-calculated centre frequency
1495 	 * of the given channel.
1496 	 */
1497 	if (IEEE80211_IS_CHAN_VHT(c))
1498 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1499 
1500 	if (IEEE80211_IS_CHAN_HT40U(c)) {
1501 		return (c->ic_freq + 10);
1502 	}
1503 	if (IEEE80211_IS_CHAN_HT40D(c)) {
1504 		return (c->ic_freq - 10);
1505 	}
1506 
1507 	return (c->ic_freq);
1508 }
1509 
1510 /*
1511  * For now, no 80+80 support; it will likely always return 0.
1512  */
1513 uint32_t
1514 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1515 {
1516 
1517 	if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
1518 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
1519 
1520 	return (0);
1521 }
1522 
1523 /*
1524  * Adds channels into specified channel list (ieee[] array must be sorted).
1525  * Channels are already sorted.
1526  */
1527 static int
1528 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1529     const uint8_t ieee[], int nieee, uint32_t flags[])
1530 {
1531 	uint16_t freq;
1532 	int i, j, error;
1533 	int is_vht;
1534 
1535 	for (i = 0; i < nieee; i++) {
1536 		freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1537 		for (j = 0; flags[j] != 0; j++) {
1538 			/*
1539 			 * Notes:
1540 			 * + HT40 and VHT40 channels occur together, so
1541 			 *   we need to be careful that we actually allow that.
1542 			 * + VHT80, VHT160 will coexist with HT40/VHT40, so
1543 			 *   make sure it's not skipped because of the overlap
1544 			 *   check used for (V)HT40.
1545 			 */
1546 			is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1547 
1548 			/*
1549 			 * Test for VHT80.
1550 			 * XXX This is all very broken right now.
1551 			 * What we /should/ do is:
1552 			 *
1553 			 * + check that the frequency is in the list of
1554 			 *   allowed VHT80 ranges; and
1555 			 * + the other 3 channels in the list are actually
1556 			 *   also available.
1557 			 */
1558 			if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1559 				if (! is_vht80_valid_freq(freq))
1560 					continue;
1561 
1562 			/*
1563 			 * Test for (V)HT40.
1564 			 *
1565 			 * This is also a fall through from VHT80; as we only
1566 			 * allow a VHT80 channel if the VHT40 combination is
1567 			 * also valid.  If the VHT40 form is not valid then
1568 			 * we certainly can't do VHT80..
1569 			 */
1570 			if (flags[j] & IEEE80211_CHAN_HT40D)
1571 				/*
1572 				 * Can't have a "lower" channel if we are the
1573 				 * first channel.
1574 				 *
1575 				 * Can't have a "lower" channel if it's below/
1576 				 * within 20MHz of the first channel.
1577 				 *
1578 				 * Can't have a "lower" channel if the channel
1579 				 * below it is not 20MHz away.
1580 				 */
1581 				if (i == 0 || ieee[i] < ieee[0] + 4 ||
1582 				    freq - 20 !=
1583 				    ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1584 					continue;
1585 			if (flags[j] & IEEE80211_CHAN_HT40U)
1586 				/*
1587 				 * Can't have an "upper" channel if we are
1588 				 * the last channel.
1589 				 *
1590 				 * Can't have an "upper" channel be above the
1591 				 * last channel in the list.
1592 				 *
1593 				 * Can't have an "upper" channel if the next
1594 				 * channel according to the math isn't 20MHz
1595 				 * away.  (Likely for channel 13/14.)
1596 				 */
1597 				if (i == nieee - 1 ||
1598 				    ieee[i] + 4 > ieee[nieee - 1] ||
1599 				    freq + 20 !=
1600 				    ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1601 					continue;
1602 
1603 			if (j == 0) {
1604 				error = addchan(chans, maxchans, nchans,
1605 				    ieee[i], freq, 0, flags[j]);
1606 			} else {
1607 				error = copychan_prev(chans, maxchans, nchans,
1608 				    flags[j]);
1609 			}
1610 			if (error != 0)
1611 				return (error);
1612 		}
1613 	}
1614 
1615 	return (0);
1616 }
1617 
1618 int
1619 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1620     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1621     int ht40)
1622 {
1623 	uint32_t flags[IEEE80211_MODE_MAX];
1624 
1625 	/* XXX no VHT for now */
1626 	getflags_2ghz(bands, flags, ht40);
1627 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1628 
1629 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1630 }
1631 
1632 int
1633 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1634     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1635     int ht40)
1636 {
1637 	uint32_t flags[IEEE80211_MODE_MAX];
1638 	int vht80 = 0;
1639 
1640 	/*
1641 	 * For now, assume VHT == VHT80 support as a minimum.
1642 	 */
1643 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
1644 		vht80 = 1;
1645 
1646 	getflags_5ghz(bands, flags, ht40, vht80);
1647 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1648 
1649 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1650 }
1651 
1652 /*
1653  * Locate a channel given a frequency+flags.  We cache
1654  * the previous lookup to optimize switching between two
1655  * channels--as happens with dynamic turbo.
1656  */
1657 struct ieee80211_channel *
1658 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1659 {
1660 	struct ieee80211_channel *c;
1661 
1662 	flags &= IEEE80211_CHAN_ALLTURBO;
1663 	c = ic->ic_prevchan;
1664 	if (c != NULL && c->ic_freq == freq &&
1665 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1666 		return c;
1667 	/* brute force search */
1668 	return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1669 }
1670 
1671 /*
1672  * Locate a channel given a channel number+flags.  We cache
1673  * the previous lookup to optimize switching between two
1674  * channels--as happens with dynamic turbo.
1675  */
1676 struct ieee80211_channel *
1677 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1678 {
1679 	struct ieee80211_channel *c;
1680 	int i;
1681 
1682 	flags &= IEEE80211_CHAN_ALLTURBO;
1683 	c = ic->ic_prevchan;
1684 	if (c != NULL && c->ic_ieee == ieee &&
1685 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1686 		return c;
1687 	/* brute force search */
1688 	for (i = 0; i < ic->ic_nchans; i++) {
1689 		c = &ic->ic_channels[i];
1690 		if (c->ic_ieee == ieee &&
1691 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1692 			return c;
1693 	}
1694 	return NULL;
1695 }
1696 
1697 /*
1698  * Lookup a channel suitable for the given rx status.
1699  *
1700  * This is used to find a channel for a frame (eg beacon, probe
1701  * response) based purely on the received PHY information.
1702  *
1703  * For now it tries to do it based on R_FREQ / R_IEEE.
1704  * This is enough for 11bg and 11a (and thus 11ng/11na)
1705  * but it will not be enough for GSM, PSB channels and the
1706  * like.  It also doesn't know about legacy-turbog and
1707  * legacy-turbo modes, which some offload NICs actually
1708  * support in weird ways.
1709  *
1710  * Takes the ic and rxstatus; returns the channel or NULL
1711  * if not found.
1712  *
1713  * XXX TODO: Add support for that when the need arises.
1714  */
1715 struct ieee80211_channel *
1716 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1717     const struct ieee80211_rx_stats *rxs)
1718 {
1719 	struct ieee80211com *ic = vap->iv_ic;
1720 	uint32_t flags;
1721 	struct ieee80211_channel *c;
1722 
1723 	if (rxs == NULL)
1724 		return (NULL);
1725 
1726 	/*
1727 	 * Strictly speaking we only use freq for now,
1728 	 * however later on we may wish to just store
1729 	 * the ieee for verification.
1730 	 */
1731 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1732 		return (NULL);
1733 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1734 		return (NULL);
1735 
1736 	/*
1737 	 * If the rx status contains a valid ieee/freq, then
1738 	 * ensure we populate the correct channel information
1739 	 * in rxchan before passing it up to the scan infrastructure.
1740 	 * Offload NICs will pass up beacons from all channels
1741 	 * during background scans.
1742 	 */
1743 
1744 	/* Determine a band */
1745 	/* XXX should be done by the driver? */
1746 	if (rxs->c_freq < 3000) {
1747 		flags = IEEE80211_CHAN_G;
1748 	} else {
1749 		flags = IEEE80211_CHAN_A;
1750 	}
1751 
1752 	/* Channel lookup */
1753 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1754 
1755 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1756 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1757 	    __func__,
1758 	    (int) rxs->c_freq,
1759 	    (int) rxs->c_ieee,
1760 	    flags,
1761 	    c);
1762 
1763 	return (c);
1764 }
1765 
1766 static void
1767 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1768 {
1769 #define	ADD(_ic, _s, _o) \
1770 	ifmedia_add(media, \
1771 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1772 	static const u_int mopts[IEEE80211_MODE_MAX] = {
1773 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
1774 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
1775 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
1776 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
1777 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
1778 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1779 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1780 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1781 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
1782 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
1783 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
1784 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
1785 	    [IEEE80211_MODE_VHT_2GHZ]	= IFM_IEEE80211_VHT2G,
1786 	    [IEEE80211_MODE_VHT_5GHZ]	= IFM_IEEE80211_VHT5G,
1787 	};
1788 	u_int mopt;
1789 
1790 	mopt = mopts[mode];
1791 	if (addsta)
1792 		ADD(ic, mword, mopt);	/* STA mode has no cap */
1793 	if (caps & IEEE80211_C_IBSS)
1794 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1795 	if (caps & IEEE80211_C_HOSTAP)
1796 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1797 	if (caps & IEEE80211_C_AHDEMO)
1798 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1799 	if (caps & IEEE80211_C_MONITOR)
1800 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1801 	if (caps & IEEE80211_C_WDS)
1802 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1803 	if (caps & IEEE80211_C_MBSS)
1804 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1805 #undef ADD
1806 }
1807 
1808 /*
1809  * Setup the media data structures according to the channel and
1810  * rate tables.
1811  */
1812 static int
1813 ieee80211_media_setup(struct ieee80211com *ic,
1814 	struct ifmedia *media, int caps, int addsta,
1815 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1816 {
1817 	int i, j, rate, maxrate, mword, r;
1818 	enum ieee80211_phymode mode;
1819 	const struct ieee80211_rateset *rs;
1820 	struct ieee80211_rateset allrates;
1821 
1822 	/*
1823 	 * Fill in media characteristics.
1824 	 */
1825 	ifmedia_init(media, 0, media_change, media_stat);
1826 	maxrate = 0;
1827 	/*
1828 	 * Add media for legacy operating modes.
1829 	 */
1830 	memset(&allrates, 0, sizeof(allrates));
1831 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1832 		if (isclr(ic->ic_modecaps, mode))
1833 			continue;
1834 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1835 		if (mode == IEEE80211_MODE_AUTO)
1836 			continue;
1837 		rs = &ic->ic_sup_rates[mode];
1838 		for (i = 0; i < rs->rs_nrates; i++) {
1839 			rate = rs->rs_rates[i];
1840 			mword = ieee80211_rate2media(ic, rate, mode);
1841 			if (mword == 0)
1842 				continue;
1843 			addmedia(media, caps, addsta, mode, mword);
1844 			/*
1845 			 * Add legacy rate to the collection of all rates.
1846 			 */
1847 			r = rate & IEEE80211_RATE_VAL;
1848 			for (j = 0; j < allrates.rs_nrates; j++)
1849 				if (allrates.rs_rates[j] == r)
1850 					break;
1851 			if (j == allrates.rs_nrates) {
1852 				/* unique, add to the set */
1853 				allrates.rs_rates[j] = r;
1854 				allrates.rs_nrates++;
1855 			}
1856 			rate = (rate & IEEE80211_RATE_VAL) / 2;
1857 			if (rate > maxrate)
1858 				maxrate = rate;
1859 		}
1860 	}
1861 	for (i = 0; i < allrates.rs_nrates; i++) {
1862 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1863 				IEEE80211_MODE_AUTO);
1864 		if (mword == 0)
1865 			continue;
1866 		/* NB: remove media options from mword */
1867 		addmedia(media, caps, addsta,
1868 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1869 	}
1870 	/*
1871 	 * Add HT/11n media.  Note that we do not have enough
1872 	 * bits in the media subtype to express the MCS so we
1873 	 * use a "placeholder" media subtype and any fixed MCS
1874 	 * must be specified with a different mechanism.
1875 	 */
1876 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
1877 		if (isclr(ic->ic_modecaps, mode))
1878 			continue;
1879 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1880 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1881 	}
1882 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1883 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1884 		addmedia(media, caps, addsta,
1885 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1886 		i = ic->ic_txstream * 8 - 1;
1887 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1888 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1889 			rate = ieee80211_htrates[i].ht40_rate_400ns;
1890 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1891 			rate = ieee80211_htrates[i].ht40_rate_800ns;
1892 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1893 			rate = ieee80211_htrates[i].ht20_rate_400ns;
1894 		else
1895 			rate = ieee80211_htrates[i].ht20_rate_800ns;
1896 		if (rate > maxrate)
1897 			maxrate = rate;
1898 	}
1899 
1900 	/*
1901 	 * Add VHT media.
1902 	 */
1903 	for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
1904 		if (isclr(ic->ic_modecaps, mode))
1905 			continue;
1906 		addmedia(media, caps, addsta, mode, IFM_AUTO);
1907 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
1908 
1909 		/* XXX TODO: VHT maxrate */
1910 	}
1911 
1912 	return maxrate;
1913 }
1914 
1915 /* XXX inline or eliminate? */
1916 const struct ieee80211_rateset *
1917 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1918 {
1919 	/* XXX does this work for 11ng basic rates? */
1920 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1921 }
1922 
1923 /* XXX inline or eliminate? */
1924 const struct ieee80211_htrateset *
1925 ieee80211_get_suphtrates(struct ieee80211com *ic,
1926     const struct ieee80211_channel *c)
1927 {
1928 	return &ic->ic_sup_htrates;
1929 }
1930 
1931 void
1932 ieee80211_announce(struct ieee80211com *ic)
1933 {
1934 	int i, rate, mword;
1935 	enum ieee80211_phymode mode;
1936 	const struct ieee80211_rateset *rs;
1937 
1938 	/* NB: skip AUTO since it has no rates */
1939 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1940 		if (isclr(ic->ic_modecaps, mode))
1941 			continue;
1942 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1943 		rs = &ic->ic_sup_rates[mode];
1944 		for (i = 0; i < rs->rs_nrates; i++) {
1945 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1946 			if (mword == 0)
1947 				continue;
1948 			rate = ieee80211_media2rate(mword);
1949 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
1950 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1951 		}
1952 		printf("\n");
1953 	}
1954 	ieee80211_ht_announce(ic);
1955 	ieee80211_vht_announce(ic);
1956 }
1957 
1958 void
1959 ieee80211_announce_channels(struct ieee80211com *ic)
1960 {
1961 	const struct ieee80211_channel *c;
1962 	char type;
1963 	int i, cw;
1964 
1965 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1966 	for (i = 0; i < ic->ic_nchans; i++) {
1967 		c = &ic->ic_channels[i];
1968 		if (IEEE80211_IS_CHAN_ST(c))
1969 			type = 'S';
1970 		else if (IEEE80211_IS_CHAN_108A(c))
1971 			type = 'T';
1972 		else if (IEEE80211_IS_CHAN_108G(c))
1973 			type = 'G';
1974 		else if (IEEE80211_IS_CHAN_HT(c))
1975 			type = 'n';
1976 		else if (IEEE80211_IS_CHAN_A(c))
1977 			type = 'a';
1978 		else if (IEEE80211_IS_CHAN_ANYG(c))
1979 			type = 'g';
1980 		else if (IEEE80211_IS_CHAN_B(c))
1981 			type = 'b';
1982 		else
1983 			type = 'f';
1984 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1985 			cw = 40;
1986 		else if (IEEE80211_IS_CHAN_HALF(c))
1987 			cw = 10;
1988 		else if (IEEE80211_IS_CHAN_QUARTER(c))
1989 			cw = 5;
1990 		else
1991 			cw = 20;
1992 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1993 			, c->ic_ieee, c->ic_freq, type
1994 			, cw
1995 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1996 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1997 			, c->ic_maxregpower
1998 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1999 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
2000 		);
2001 	}
2002 }
2003 
2004 static int
2005 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
2006 {
2007 	switch (IFM_MODE(ime->ifm_media)) {
2008 	case IFM_IEEE80211_11A:
2009 		*mode = IEEE80211_MODE_11A;
2010 		break;
2011 	case IFM_IEEE80211_11B:
2012 		*mode = IEEE80211_MODE_11B;
2013 		break;
2014 	case IFM_IEEE80211_11G:
2015 		*mode = IEEE80211_MODE_11G;
2016 		break;
2017 	case IFM_IEEE80211_FH:
2018 		*mode = IEEE80211_MODE_FH;
2019 		break;
2020 	case IFM_IEEE80211_11NA:
2021 		*mode = IEEE80211_MODE_11NA;
2022 		break;
2023 	case IFM_IEEE80211_11NG:
2024 		*mode = IEEE80211_MODE_11NG;
2025 		break;
2026 	case IFM_AUTO:
2027 		*mode = IEEE80211_MODE_AUTO;
2028 		break;
2029 	default:
2030 		return 0;
2031 	}
2032 	/*
2033 	 * Turbo mode is an ``option''.
2034 	 * XXX does not apply to AUTO
2035 	 */
2036 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2037 		if (*mode == IEEE80211_MODE_11A) {
2038 			if (flags & IEEE80211_F_TURBOP)
2039 				*mode = IEEE80211_MODE_TURBO_A;
2040 			else
2041 				*mode = IEEE80211_MODE_STURBO_A;
2042 		} else if (*mode == IEEE80211_MODE_11G)
2043 			*mode = IEEE80211_MODE_TURBO_G;
2044 		else
2045 			return 0;
2046 	}
2047 	/* XXX HT40 +/- */
2048 	return 1;
2049 }
2050 
2051 /*
2052  * Handle a media change request on the vap interface.
2053  */
2054 int
2055 ieee80211_media_change(struct ifnet *ifp)
2056 {
2057 	struct ieee80211vap *vap = ifp->if_softc;
2058 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2059 	uint16_t newmode;
2060 
2061 	if (!media2mode(ime, vap->iv_flags, &newmode))
2062 		return EINVAL;
2063 	if (vap->iv_des_mode != newmode) {
2064 		vap->iv_des_mode = newmode;
2065 		/* XXX kick state machine if up+running */
2066 	}
2067 	return 0;
2068 }
2069 
2070 /*
2071  * Common code to calculate the media status word
2072  * from the operating mode and channel state.
2073  */
2074 static int
2075 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
2076 {
2077 	int status;
2078 
2079 	status = IFM_IEEE80211;
2080 	switch (opmode) {
2081 	case IEEE80211_M_STA:
2082 		break;
2083 	case IEEE80211_M_IBSS:
2084 		status |= IFM_IEEE80211_ADHOC;
2085 		break;
2086 	case IEEE80211_M_HOSTAP:
2087 		status |= IFM_IEEE80211_HOSTAP;
2088 		break;
2089 	case IEEE80211_M_MONITOR:
2090 		status |= IFM_IEEE80211_MONITOR;
2091 		break;
2092 	case IEEE80211_M_AHDEMO:
2093 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
2094 		break;
2095 	case IEEE80211_M_WDS:
2096 		status |= IFM_IEEE80211_WDS;
2097 		break;
2098 	case IEEE80211_M_MBSS:
2099 		status |= IFM_IEEE80211_MBSS;
2100 		break;
2101 	}
2102 	if (IEEE80211_IS_CHAN_HTA(chan)) {
2103 		status |= IFM_IEEE80211_11NA;
2104 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
2105 		status |= IFM_IEEE80211_11NG;
2106 	} else if (IEEE80211_IS_CHAN_A(chan)) {
2107 		status |= IFM_IEEE80211_11A;
2108 	} else if (IEEE80211_IS_CHAN_B(chan)) {
2109 		status |= IFM_IEEE80211_11B;
2110 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
2111 		status |= IFM_IEEE80211_11G;
2112 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
2113 		status |= IFM_IEEE80211_FH;
2114 	}
2115 	/* XXX else complain? */
2116 
2117 	if (IEEE80211_IS_CHAN_TURBO(chan))
2118 		status |= IFM_IEEE80211_TURBO;
2119 #if 0
2120 	if (IEEE80211_IS_CHAN_HT20(chan))
2121 		status |= IFM_IEEE80211_HT20;
2122 	if (IEEE80211_IS_CHAN_HT40(chan))
2123 		status |= IFM_IEEE80211_HT40;
2124 #endif
2125 	return status;
2126 }
2127 
2128 void
2129 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2130 {
2131 	struct ieee80211vap *vap = ifp->if_softc;
2132 	struct ieee80211com *ic = vap->iv_ic;
2133 	enum ieee80211_phymode mode;
2134 
2135 	imr->ifm_status = IFM_AVALID;
2136 	/*
2137 	 * NB: use the current channel's mode to lock down a xmit
2138 	 * rate only when running; otherwise we may have a mismatch
2139 	 * in which case the rate will not be convertible.
2140 	 */
2141 	if (vap->iv_state == IEEE80211_S_RUN ||
2142 	    vap->iv_state == IEEE80211_S_SLEEP) {
2143 		imr->ifm_status |= IFM_ACTIVE;
2144 		mode = ieee80211_chan2mode(ic->ic_curchan);
2145 	} else
2146 		mode = IEEE80211_MODE_AUTO;
2147 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
2148 	/*
2149 	 * Calculate a current rate if possible.
2150 	 */
2151 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
2152 		/*
2153 		 * A fixed rate is set, report that.
2154 		 */
2155 		imr->ifm_active |= ieee80211_rate2media(ic,
2156 			vap->iv_txparms[mode].ucastrate, mode);
2157 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
2158 		/*
2159 		 * In station mode report the current transmit rate.
2160 		 */
2161 		imr->ifm_active |= ieee80211_rate2media(ic,
2162 			vap->iv_bss->ni_txrate, mode);
2163 	} else
2164 		imr->ifm_active |= IFM_AUTO;
2165 	if (imr->ifm_status & IFM_ACTIVE)
2166 		imr->ifm_current = imr->ifm_active;
2167 }
2168 
2169 /*
2170  * Set the current phy mode and recalculate the active channel
2171  * set based on the available channels for this mode.  Also
2172  * select a new default/current channel if the current one is
2173  * inappropriate for this mode.
2174  */
2175 int
2176 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
2177 {
2178 	/*
2179 	 * Adjust basic rates in 11b/11g supported rate set.
2180 	 * Note that if operating on a hal/quarter rate channel
2181 	 * this is a noop as those rates sets are different
2182 	 * and used instead.
2183 	 */
2184 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2185 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2186 
2187 	ic->ic_curmode = mode;
2188 	ieee80211_reset_erp(ic);	/* reset ERP state */
2189 
2190 	return 0;
2191 }
2192 
2193 /*
2194  * Return the phy mode for with the specified channel.
2195  */
2196 enum ieee80211_phymode
2197 ieee80211_chan2mode(const struct ieee80211_channel *chan)
2198 {
2199 
2200 	if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
2201 		return IEEE80211_MODE_VHT_2GHZ;
2202 	else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
2203 		return IEEE80211_MODE_VHT_5GHZ;
2204 	else if (IEEE80211_IS_CHAN_HTA(chan))
2205 		return IEEE80211_MODE_11NA;
2206 	else if (IEEE80211_IS_CHAN_HTG(chan))
2207 		return IEEE80211_MODE_11NG;
2208 	else if (IEEE80211_IS_CHAN_108G(chan))
2209 		return IEEE80211_MODE_TURBO_G;
2210 	else if (IEEE80211_IS_CHAN_ST(chan))
2211 		return IEEE80211_MODE_STURBO_A;
2212 	else if (IEEE80211_IS_CHAN_TURBO(chan))
2213 		return IEEE80211_MODE_TURBO_A;
2214 	else if (IEEE80211_IS_CHAN_HALF(chan))
2215 		return IEEE80211_MODE_HALF;
2216 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
2217 		return IEEE80211_MODE_QUARTER;
2218 	else if (IEEE80211_IS_CHAN_A(chan))
2219 		return IEEE80211_MODE_11A;
2220 	else if (IEEE80211_IS_CHAN_ANYG(chan))
2221 		return IEEE80211_MODE_11G;
2222 	else if (IEEE80211_IS_CHAN_B(chan))
2223 		return IEEE80211_MODE_11B;
2224 	else if (IEEE80211_IS_CHAN_FHSS(chan))
2225 		return IEEE80211_MODE_FH;
2226 
2227 	/* NB: should not get here */
2228 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
2229 		__func__, chan->ic_freq, chan->ic_flags);
2230 	return IEEE80211_MODE_11B;
2231 }
2232 
2233 struct ratemedia {
2234 	u_int	match;	/* rate + mode */
2235 	u_int	media;	/* if_media rate */
2236 };
2237 
2238 static int
2239 findmedia(const struct ratemedia rates[], int n, u_int match)
2240 {
2241 	int i;
2242 
2243 	for (i = 0; i < n; i++)
2244 		if (rates[i].match == match)
2245 			return rates[i].media;
2246 	return IFM_AUTO;
2247 }
2248 
2249 /*
2250  * Convert IEEE80211 rate value to ifmedia subtype.
2251  * Rate is either a legacy rate in units of 0.5Mbps
2252  * or an MCS index.
2253  */
2254 int
2255 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
2256 {
2257 	static const struct ratemedia rates[] = {
2258 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
2259 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
2260 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
2261 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
2262 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
2263 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
2264 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
2265 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
2266 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
2267 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
2268 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
2269 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
2270 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
2271 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
2272 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
2273 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
2274 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
2275 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
2276 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
2277 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
2278 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
2279 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
2280 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
2281 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
2282 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
2283 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
2284 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
2285 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
2286 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
2287 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2288 		/* NB: OFDM72 doesn't really exist so we don't handle it */
2289 	};
2290 	static const struct ratemedia htrates[] = {
2291 		{   0, IFM_IEEE80211_MCS },
2292 		{   1, IFM_IEEE80211_MCS },
2293 		{   2, IFM_IEEE80211_MCS },
2294 		{   3, IFM_IEEE80211_MCS },
2295 		{   4, IFM_IEEE80211_MCS },
2296 		{   5, IFM_IEEE80211_MCS },
2297 		{   6, IFM_IEEE80211_MCS },
2298 		{   7, IFM_IEEE80211_MCS },
2299 		{   8, IFM_IEEE80211_MCS },
2300 		{   9, IFM_IEEE80211_MCS },
2301 		{  10, IFM_IEEE80211_MCS },
2302 		{  11, IFM_IEEE80211_MCS },
2303 		{  12, IFM_IEEE80211_MCS },
2304 		{  13, IFM_IEEE80211_MCS },
2305 		{  14, IFM_IEEE80211_MCS },
2306 		{  15, IFM_IEEE80211_MCS },
2307 		{  16, IFM_IEEE80211_MCS },
2308 		{  17, IFM_IEEE80211_MCS },
2309 		{  18, IFM_IEEE80211_MCS },
2310 		{  19, IFM_IEEE80211_MCS },
2311 		{  20, IFM_IEEE80211_MCS },
2312 		{  21, IFM_IEEE80211_MCS },
2313 		{  22, IFM_IEEE80211_MCS },
2314 		{  23, IFM_IEEE80211_MCS },
2315 		{  24, IFM_IEEE80211_MCS },
2316 		{  25, IFM_IEEE80211_MCS },
2317 		{  26, IFM_IEEE80211_MCS },
2318 		{  27, IFM_IEEE80211_MCS },
2319 		{  28, IFM_IEEE80211_MCS },
2320 		{  29, IFM_IEEE80211_MCS },
2321 		{  30, IFM_IEEE80211_MCS },
2322 		{  31, IFM_IEEE80211_MCS },
2323 		{  32, IFM_IEEE80211_MCS },
2324 		{  33, IFM_IEEE80211_MCS },
2325 		{  34, IFM_IEEE80211_MCS },
2326 		{  35, IFM_IEEE80211_MCS },
2327 		{  36, IFM_IEEE80211_MCS },
2328 		{  37, IFM_IEEE80211_MCS },
2329 		{  38, IFM_IEEE80211_MCS },
2330 		{  39, IFM_IEEE80211_MCS },
2331 		{  40, IFM_IEEE80211_MCS },
2332 		{  41, IFM_IEEE80211_MCS },
2333 		{  42, IFM_IEEE80211_MCS },
2334 		{  43, IFM_IEEE80211_MCS },
2335 		{  44, IFM_IEEE80211_MCS },
2336 		{  45, IFM_IEEE80211_MCS },
2337 		{  46, IFM_IEEE80211_MCS },
2338 		{  47, IFM_IEEE80211_MCS },
2339 		{  48, IFM_IEEE80211_MCS },
2340 		{  49, IFM_IEEE80211_MCS },
2341 		{  50, IFM_IEEE80211_MCS },
2342 		{  51, IFM_IEEE80211_MCS },
2343 		{  52, IFM_IEEE80211_MCS },
2344 		{  53, IFM_IEEE80211_MCS },
2345 		{  54, IFM_IEEE80211_MCS },
2346 		{  55, IFM_IEEE80211_MCS },
2347 		{  56, IFM_IEEE80211_MCS },
2348 		{  57, IFM_IEEE80211_MCS },
2349 		{  58, IFM_IEEE80211_MCS },
2350 		{  59, IFM_IEEE80211_MCS },
2351 		{  60, IFM_IEEE80211_MCS },
2352 		{  61, IFM_IEEE80211_MCS },
2353 		{  62, IFM_IEEE80211_MCS },
2354 		{  63, IFM_IEEE80211_MCS },
2355 		{  64, IFM_IEEE80211_MCS },
2356 		{  65, IFM_IEEE80211_MCS },
2357 		{  66, IFM_IEEE80211_MCS },
2358 		{  67, IFM_IEEE80211_MCS },
2359 		{  68, IFM_IEEE80211_MCS },
2360 		{  69, IFM_IEEE80211_MCS },
2361 		{  70, IFM_IEEE80211_MCS },
2362 		{  71, IFM_IEEE80211_MCS },
2363 		{  72, IFM_IEEE80211_MCS },
2364 		{  73, IFM_IEEE80211_MCS },
2365 		{  74, IFM_IEEE80211_MCS },
2366 		{  75, IFM_IEEE80211_MCS },
2367 		{  76, IFM_IEEE80211_MCS },
2368 	};
2369 	int m;
2370 
2371 	/*
2372 	 * Check 11n rates first for match as an MCS.
2373 	 */
2374 	if (mode == IEEE80211_MODE_11NA) {
2375 		if (rate & IEEE80211_RATE_MCS) {
2376 			rate &= ~IEEE80211_RATE_MCS;
2377 			m = findmedia(htrates, nitems(htrates), rate);
2378 			if (m != IFM_AUTO)
2379 				return m | IFM_IEEE80211_11NA;
2380 		}
2381 	} else if (mode == IEEE80211_MODE_11NG) {
2382 		/* NB: 12 is ambiguous, it will be treated as an MCS */
2383 		if (rate & IEEE80211_RATE_MCS) {
2384 			rate &= ~IEEE80211_RATE_MCS;
2385 			m = findmedia(htrates, nitems(htrates), rate);
2386 			if (m != IFM_AUTO)
2387 				return m | IFM_IEEE80211_11NG;
2388 		}
2389 	}
2390 	rate &= IEEE80211_RATE_VAL;
2391 	switch (mode) {
2392 	case IEEE80211_MODE_11A:
2393 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
2394 	case IEEE80211_MODE_QUARTER:
2395 	case IEEE80211_MODE_11NA:
2396 	case IEEE80211_MODE_TURBO_A:
2397 	case IEEE80211_MODE_STURBO_A:
2398 		return findmedia(rates, nitems(rates),
2399 		    rate | IFM_IEEE80211_11A);
2400 	case IEEE80211_MODE_11B:
2401 		return findmedia(rates, nitems(rates),
2402 		    rate | IFM_IEEE80211_11B);
2403 	case IEEE80211_MODE_FH:
2404 		return findmedia(rates, nitems(rates),
2405 		    rate | IFM_IEEE80211_FH);
2406 	case IEEE80211_MODE_AUTO:
2407 		/* NB: ic may be NULL for some drivers */
2408 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2409 			return findmedia(rates, nitems(rates),
2410 			    rate | IFM_IEEE80211_FH);
2411 		/* NB: hack, 11g matches both 11b+11a rates */
2412 		/* fall thru... */
2413 	case IEEE80211_MODE_11G:
2414 	case IEEE80211_MODE_11NG:
2415 	case IEEE80211_MODE_TURBO_G:
2416 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2417 	case IEEE80211_MODE_VHT_2GHZ:
2418 	case IEEE80211_MODE_VHT_5GHZ:
2419 		/* XXX TODO: need to figure out mapping for VHT rates */
2420 		return IFM_AUTO;
2421 	}
2422 	return IFM_AUTO;
2423 }
2424 
2425 int
2426 ieee80211_media2rate(int mword)
2427 {
2428 	static const int ieeerates[] = {
2429 		-1,		/* IFM_AUTO */
2430 		0,		/* IFM_MANUAL */
2431 		0,		/* IFM_NONE */
2432 		2,		/* IFM_IEEE80211_FH1 */
2433 		4,		/* IFM_IEEE80211_FH2 */
2434 		2,		/* IFM_IEEE80211_DS1 */
2435 		4,		/* IFM_IEEE80211_DS2 */
2436 		11,		/* IFM_IEEE80211_DS5 */
2437 		22,		/* IFM_IEEE80211_DS11 */
2438 		44,		/* IFM_IEEE80211_DS22 */
2439 		12,		/* IFM_IEEE80211_OFDM6 */
2440 		18,		/* IFM_IEEE80211_OFDM9 */
2441 		24,		/* IFM_IEEE80211_OFDM12 */
2442 		36,		/* IFM_IEEE80211_OFDM18 */
2443 		48,		/* IFM_IEEE80211_OFDM24 */
2444 		72,		/* IFM_IEEE80211_OFDM36 */
2445 		96,		/* IFM_IEEE80211_OFDM48 */
2446 		108,		/* IFM_IEEE80211_OFDM54 */
2447 		144,		/* IFM_IEEE80211_OFDM72 */
2448 		0,		/* IFM_IEEE80211_DS354k */
2449 		0,		/* IFM_IEEE80211_DS512k */
2450 		6,		/* IFM_IEEE80211_OFDM3 */
2451 		9,		/* IFM_IEEE80211_OFDM4 */
2452 		54,		/* IFM_IEEE80211_OFDM27 */
2453 		-1,		/* IFM_IEEE80211_MCS */
2454 		-1,		/* IFM_IEEE80211_VHT */
2455 	};
2456 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2457 		ieeerates[IFM_SUBTYPE(mword)] : 0;
2458 }
2459 
2460 /*
2461  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2462  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2463  */
2464 #define	mix(a, b, c)							\
2465 do {									\
2466 	a -= b; a -= c; a ^= (c >> 13);					\
2467 	b -= c; b -= a; b ^= (a << 8);					\
2468 	c -= a; c -= b; c ^= (b >> 13);					\
2469 	a -= b; a -= c; a ^= (c >> 12);					\
2470 	b -= c; b -= a; b ^= (a << 16);					\
2471 	c -= a; c -= b; c ^= (b >> 5);					\
2472 	a -= b; a -= c; a ^= (c >> 3);					\
2473 	b -= c; b -= a; b ^= (a << 10);					\
2474 	c -= a; c -= b; c ^= (b >> 15);					\
2475 } while (/*CONSTCOND*/0)
2476 
2477 uint32_t
2478 ieee80211_mac_hash(const struct ieee80211com *ic,
2479 	const uint8_t addr[IEEE80211_ADDR_LEN])
2480 {
2481 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2482 
2483 	b += addr[5] << 8;
2484 	b += addr[4];
2485 	a += addr[3] << 24;
2486 	a += addr[2] << 16;
2487 	a += addr[1] << 8;
2488 	a += addr[0];
2489 
2490 	mix(a, b, c);
2491 
2492 	return c;
2493 }
2494 #undef mix
2495 
2496 char
2497 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2498 {
2499 	if (IEEE80211_IS_CHAN_ST(c))
2500 		return 'S';
2501 	if (IEEE80211_IS_CHAN_108A(c))
2502 		return 'T';
2503 	if (IEEE80211_IS_CHAN_108G(c))
2504 		return 'G';
2505 	if (IEEE80211_IS_CHAN_VHT(c))
2506 		return 'v';
2507 	if (IEEE80211_IS_CHAN_HT(c))
2508 		return 'n';
2509 	if (IEEE80211_IS_CHAN_A(c))
2510 		return 'a';
2511 	if (IEEE80211_IS_CHAN_ANYG(c))
2512 		return 'g';
2513 	if (IEEE80211_IS_CHAN_B(c))
2514 		return 'b';
2515 	return 'f';
2516 }
2517