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