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