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