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