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