Home | History | Annotate | Line # | Download | only in net80211
ieee80211_proto.c revision 1.34.14.2
      1 /*	$NetBSD: ieee80211_proto.c,v 1.34.14.2 2018/07/12 16:35:34 phil Exp $ */
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  *
      6  * Copyright (c) 2001 Atsushi Onoe
      7  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
      8  * Copyright (c) 2012 IEEE
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if __FreeBSD__
     34 __FBSDID("$FreeBSD$");
     35 #endif
     36 
     37 /*
     38  * IEEE 802.11 protocol support.
     39  */
     40 
     41 #include "opt_inet.h"
     42 #include "opt_wlan.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/malloc.h>
     48 #ifdef __NetBSD__
     49 #include <sys/mbuf.h>
     50 #endif
     51 
     52 #include <sys/socket.h>
     53 #include <sys/sockio.h>
     54 
     55 #include <net/if.h>
     56 #if __FreeBSD__
     57 #include <net/if_var.h>
     58 #endif
     59 #include <net/if_media.h>
     60 #if __FreeBSD__
     61 #include <net/ethernet.h>		/* XXX for ether_sprintf */
     62 #endif
     63 #ifdef __NetBSD__
     64 #include <net/if_ether.h>
     65 #include <net/route.h>
     66 #endif
     67 
     68 #include <net80211/ieee80211_var.h>
     69 #include <net80211/ieee80211_adhoc.h>
     70 #include <net80211/ieee80211_sta.h>
     71 #include <net80211/ieee80211_hostap.h>
     72 #include <net80211/ieee80211_wds.h>
     73 #ifdef IEEE80211_SUPPORT_MESH
     74 #include <net80211/ieee80211_mesh.h>
     75 #endif
     76 #include <net80211/ieee80211_monitor.h>
     77 #include <net80211/ieee80211_input.h>
     78 
     79 #ifdef __NetBSD__
     80 #undef  KASSERT
     81 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     82 #endif
     83 
     84 /* XXX tunables */
     85 #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
     86 #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
     87 
     88 const char *mgt_subtype_name[] = {
     89 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
     90 	"probe_req",	"probe_resp",	"timing_adv",	"reserved#7",
     91 	"beacon",	"atim",		"disassoc",	"auth",
     92 	"deauth",	"action",	"action_noack",	"reserved#15"
     93 };
     94 const char *ctl_subtype_name[] = {
     95 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
     96 	"reserved#4",	"reserved#5",	"reserved#6",	"control_wrap",
     97 	"bar",		"ba",		"ps_poll",	"rts",
     98 	"cts",		"ack",		"cf_end",	"cf_end_ack"
     99 };
    100 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
    101 	"IBSS",		/* IEEE80211_M_IBSS */
    102 	"STA",		/* IEEE80211_M_STA */
    103 	"WDS",		/* IEEE80211_M_WDS */
    104 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
    105 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
    106 	"MONITOR",	/* IEEE80211_M_MONITOR */
    107 	"MBSS"		/* IEEE80211_M_MBSS */
    108 };
    109 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
    110 	"INIT",		/* IEEE80211_S_INIT */
    111 	"SCAN",		/* IEEE80211_S_SCAN */
    112 	"AUTH",		/* IEEE80211_S_AUTH */
    113 	"ASSOC",	/* IEEE80211_S_ASSOC */
    114 	"CAC",		/* IEEE80211_S_CAC */
    115 	"RUN",		/* IEEE80211_S_RUN */
    116 	"CSA",		/* IEEE80211_S_CSA */
    117 	"SLEEP",	/* IEEE80211_S_SLEEP */
    118 };
    119 const char *ieee80211_wme_acnames[] = {
    120 	"WME_AC_BE",
    121 	"WME_AC_BK",
    122 	"WME_AC_VI",
    123 	"WME_AC_VO",
    124 	"WME_UPSD",
    125 };
    126 
    127 
    128 /*
    129  * Reason code descriptions were (mostly) obtained from
    130  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
    131  */
    132 const char *
    133 ieee80211_reason_to_string(uint16_t reason)
    134 {
    135 	switch (reason) {
    136 	case IEEE80211_REASON_UNSPECIFIED:
    137 		return ("unspecified");
    138 	case IEEE80211_REASON_AUTH_EXPIRE:
    139 		return ("previous authentication is expired");
    140 	case IEEE80211_REASON_AUTH_LEAVE:
    141 		return ("sending STA is leaving/has left IBSS or ESS");
    142 	case IEEE80211_REASON_ASSOC_EXPIRE:
    143 		return ("disassociated due to inactivity");
    144 	case IEEE80211_REASON_ASSOC_TOOMANY:
    145 		return ("too many associated STAs");
    146 	case IEEE80211_REASON_NOT_AUTHED:
    147 		return ("class 2 frame received from nonauthenticated STA");
    148 	case IEEE80211_REASON_NOT_ASSOCED:
    149 		return ("class 3 frame received from nonassociated STA");
    150 	case IEEE80211_REASON_ASSOC_LEAVE:
    151 		return ("sending STA is leaving/has left BSS");
    152 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
    153 		return ("STA requesting (re)association is not authenticated");
    154 	case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
    155 		return ("information in the Power Capability element is "
    156 			"unacceptable");
    157 	case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
    158 		return ("information in the Supported Channels element is "
    159 			"unacceptable");
    160 	case IEEE80211_REASON_IE_INVALID:
    161 		return ("invalid element");
    162 	case IEEE80211_REASON_MIC_FAILURE:
    163 		return ("MIC failure");
    164 	case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
    165 		return ("4-Way handshake timeout");
    166 	case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
    167 		return ("group key update timeout");
    168 	case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
    169 		return ("element in 4-Way handshake different from "
    170 			"(re)association request/probe response/beacon frame");
    171 	case IEEE80211_REASON_GROUP_CIPHER_INVALID:
    172 		return ("invalid group cipher");
    173 	case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
    174 		return ("invalid pairwise cipher");
    175 	case IEEE80211_REASON_AKMP_INVALID:
    176 		return ("invalid AKMP");
    177 	case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
    178 		return ("unsupported version in RSN IE");
    179 	case IEEE80211_REASON_INVALID_RSN_IE_CAP:
    180 		return ("invalid capabilities in RSN IE");
    181 	case IEEE80211_REASON_802_1X_AUTH_FAILED:
    182 		return ("IEEE 802.1X authentication failed");
    183 	case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
    184 		return ("cipher suite rejected because of the security "
    185 			"policy");
    186 	case IEEE80211_REASON_UNSPECIFIED_QOS:
    187 		return ("unspecified (QoS-related)");
    188 	case IEEE80211_REASON_INSUFFICIENT_BW:
    189 		return ("QoS AP lacks sufficient bandwidth for this QoS STA");
    190 	case IEEE80211_REASON_TOOMANY_FRAMES:
    191 		return ("too many frames need to be acknowledged");
    192 	case IEEE80211_REASON_OUTSIDE_TXOP:
    193 		return ("STA is transmitting outside the limits of its TXOPs");
    194 	case IEEE80211_REASON_LEAVING_QBSS:
    195 		return ("requested from peer STA (the STA is "
    196 			"resetting/leaving the BSS)");
    197 	case IEEE80211_REASON_BAD_MECHANISM:
    198 		return ("requested from peer STA (it does not want to use "
    199 			"the mechanism)");
    200 	case IEEE80211_REASON_SETUP_NEEDED:
    201 		return ("requested from peer STA (setup is required for the "
    202 			"used mechanism)");
    203 	case IEEE80211_REASON_TIMEOUT:
    204 		return ("requested from peer STA (timeout)");
    205 	case IEEE80211_REASON_PEER_LINK_CANCELED:
    206 		return ("SME cancels the mesh peering instance (not related "
    207 			"to the maximum number of peer mesh STAs)");
    208 	case IEEE80211_REASON_MESH_MAX_PEERS:
    209 		return ("maximum number of peer mesh STAs was reached");
    210 	case IEEE80211_REASON_MESH_CPVIOLATION:
    211 		return ("the received information violates the Mesh "
    212 			"Configuration policy configured in the mesh STA "
    213 			"profile");
    214 	case IEEE80211_REASON_MESH_CLOSE_RCVD:
    215 		return ("the mesh STA has received a Mesh Peering Close "
    216 			"message requesting to close the mesh peering");
    217 	case IEEE80211_REASON_MESH_MAX_RETRIES:
    218 		return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
    219 			"Peering Open messages, without receiving a Mesh "
    220 			"Peering Confirm message");
    221 	case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
    222 		return ("the confirmTimer for the mesh peering instance times "
    223 			"out");
    224 	case IEEE80211_REASON_MESH_INVALID_GTK:
    225 		return ("the mesh STA fails to unwrap the GTK or the values "
    226 			"in the wrapped contents do not match");
    227 	case IEEE80211_REASON_MESH_INCONS_PARAMS:
    228 		return ("the mesh STA receives inconsistent information about "
    229 			"the mesh parameters between Mesh Peering Management "
    230 			"frames");
    231 	case IEEE80211_REASON_MESH_INVALID_SECURITY:
    232 		return ("the mesh STA fails the authenticated mesh peering "
    233 			"exchange because due to failure in selecting "
    234 			"pairwise/group ciphersuite");
    235 	case IEEE80211_REASON_MESH_PERR_NO_PROXY:
    236 		return ("the mesh STA does not have proxy information for "
    237 			"this external destination");
    238 	case IEEE80211_REASON_MESH_PERR_NO_FI:
    239 		return ("the mesh STA does not have forwarding information "
    240 			"for this destination");
    241 	case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
    242 		return ("the mesh STA determines that the link to the next "
    243 			"hop of an active path in its forwarding information "
    244 			"is no longer usable");
    245 	case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
    246 		return ("the MAC address of the STA already exists in the "
    247 			"mesh BSS");
    248 	case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
    249 		return ("the mesh STA performs channel switch to meet "
    250 			"regulatory requirements");
    251 	case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
    252 		return ("the mesh STA performs channel switch with "
    253 			"unspecified reason");
    254 	default:
    255 		return ("reserved/unknown");
    256 	}
    257 }
    258 
    259 static void beacon_miss(void *, int);
    260 static void beacon_swmiss(void *, int);
    261 static void parent_updown(void *, int);
    262 static void update_mcast(void *, int);
    263 static void update_promisc(void *, int);
    264 static void update_channel(void *, int);
    265 static void update_chw(void *, int);
    266 static void vap_update_wme(void *, int);
    267 static void restart_vaps(void *, int);
    268 static void ieee80211_newstate_cb(void *, int);
    269 
    270 static int
    271 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
    272 	const struct ieee80211_bpf_params *params)
    273 {
    274 
    275 	ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
    276 	m_freem(m);
    277 	return ENETDOWN;
    278 }
    279 
    280 void
    281 ieee80211_proto_attach(struct ieee80211com *ic)
    282 {
    283 	uint8_t hdrlen;
    284 
    285 	/* override the 802.3 setting */
    286 	hdrlen = ic->ic_headroom
    287 		+ sizeof(struct ieee80211_qosframe_addr4)
    288 		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
    289 		+ IEEE80211_WEP_EXTIVLEN;
    290 	/* XXX no way to recalculate on ifdetach */
    291 	if (ALIGN(hdrlen) > max_linkhdr) {
    292 		/* XXX sanity check... */
    293 		max_linkhdr = ALIGN(hdrlen);
    294 		max_hdr = max_linkhdr + max_protohdr;
    295 		max_datalen = MHLEN - max_hdr;
    296 	}
    297 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
    298 
    299 	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
    300 	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
    301 	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
    302 	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
    303 	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
    304 	TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
    305 	TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
    306 
    307 	ic->ic_wme.wme_hipri_switch_hysteresis =
    308 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
    309 
    310 	/* initialize management frame handlers */
    311 	ic->ic_send_mgmt = ieee80211_send_mgmt;
    312 	ic->ic_raw_xmit = null_raw_xmit;
    313 
    314 	ieee80211_adhoc_attach(ic);
    315 	ieee80211_sta_attach(ic);
    316 	ieee80211_wds_attach(ic);
    317 	ieee80211_hostap_attach(ic);
    318 #ifdef IEEE80211_SUPPORT_MESH
    319 	ieee80211_mesh_attach(ic);
    320 #endif
    321 	ieee80211_monitor_attach(ic);
    322 }
    323 
    324 void
    325 ieee80211_proto_detach(struct ieee80211com *ic)
    326 {
    327 	ieee80211_monitor_detach(ic);
    328 #ifdef IEEE80211_SUPPORT_MESH
    329 	ieee80211_mesh_detach(ic);
    330 #endif
    331 	ieee80211_hostap_detach(ic);
    332 	ieee80211_wds_detach(ic);
    333 	ieee80211_adhoc_detach(ic);
    334 	ieee80211_sta_detach(ic);
    335 }
    336 
    337 static void
    338 null_update_beacon(struct ieee80211vap *vap, int item)
    339 {
    340 }
    341 
    342 void
    343 ieee80211_proto_vattach(struct ieee80211vap *vap)
    344 {
    345 	struct ieee80211com *ic = vap->iv_ic;
    346 	struct ifnet *ifp = vap->iv_ifp;
    347 	int i;
    348 
    349 	/* override the 802.3 setting */
    350 	ifp->if_hdrlen = ic->ic_headroom
    351                 + sizeof(struct ieee80211_qosframe_addr4)
    352                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
    353                 + IEEE80211_WEP_EXTIVLEN;
    354 
    355 	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
    356 	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
    357 	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
    358 #if __FreeBSD__
    359 	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
    360 #endif
    361 	callout_init(&vap->iv_mgtsend, 1);
    362 	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
    363 	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
    364 	TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
    365 	/*
    366 	 * Install default tx rate handling: no fixed rate, lowest
    367 	 * supported rate for mgmt and multicast frames.  Default
    368 	 * max retry count.  These settings can be changed by the
    369 	 * driver and/or user applications.
    370 	 */
    371 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
    372 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
    373 
    374 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
    375 
    376 		/*
    377 		 * Setting the management rate to MCS 0 assumes that the
    378 		 * BSS Basic rate set is empty and the BSS Basic MCS set
    379 		 * is not.
    380 		 *
    381 		 * Since we're not checking this, default to the lowest
    382 		 * defined rate for this mode.
    383 		 *
    384 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
    385 		 * some MCS management traffic (eg BA response frames.)
    386 		 *
    387 		 * See also: 9.6.0 of the 802.11n-2009 specification.
    388 		 */
    389 #ifdef	NOTYET
    390 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
    391 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
    392 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
    393 		} else {
    394 			vap->iv_txparms[i].mgmtrate =
    395 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
    396 			vap->iv_txparms[i].mcastrate =
    397 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
    398 		}
    399 #endif
    400 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
    401 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
    402 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
    403 	}
    404 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
    405 
    406 	vap->iv_update_beacon = null_update_beacon;
    407 	vap->iv_deliver_data = ieee80211_deliver_data;
    408 
    409 	/* attach support for operating mode */
    410 	ic->ic_vattach[vap->iv_opmode](vap);
    411 }
    412 
    413 void
    414 ieee80211_proto_vdetach(struct ieee80211vap *vap)
    415 {
    416 #define	FREEAPPIE(ie) do { \
    417 	if (ie != NULL) \
    418 		IEEE80211_FREE(ie, M_80211_NODE_IE); \
    419 } while (0)
    420 	/*
    421 	 * Detach operating mode module.
    422 	 */
    423 	if (vap->iv_opdetach != NULL)
    424 		vap->iv_opdetach(vap);
    425 	/*
    426 	 * This should not be needed as we detach when reseting
    427 	 * the state but be conservative here since the
    428 	 * authenticator may do things like spawn kernel threads.
    429 	 */
    430 	if (vap->iv_auth->ia_detach != NULL)
    431 		vap->iv_auth->ia_detach(vap);
    432 	/*
    433 	 * Detach any ACL'ator.
    434 	 */
    435 	if (vap->iv_acl != NULL)
    436 		vap->iv_acl->iac_detach(vap);
    437 
    438 	FREEAPPIE(vap->iv_appie_beacon);
    439 	FREEAPPIE(vap->iv_appie_probereq);
    440 	FREEAPPIE(vap->iv_appie_proberesp);
    441 	FREEAPPIE(vap->iv_appie_assocreq);
    442 	FREEAPPIE(vap->iv_appie_assocresp);
    443 	FREEAPPIE(vap->iv_appie_wpa);
    444 #undef FREEAPPIE
    445 }
    446 
    447 /*
    448  * Simple-minded authenticator module support.
    449  */
    450 
    451 #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
    452 /* XXX well-known names */
    453 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
    454 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
    455 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
    456 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
    457 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
    458 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
    459 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
    460 };
    461 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
    462 
    463 static const struct ieee80211_authenticator auth_internal = {
    464 	.ia_name		= "wlan_internal",
    465 	.ia_attach		= NULL,
    466 	.ia_detach		= NULL,
    467 	.ia_node_join		= NULL,
    468 	.ia_node_leave		= NULL,
    469 };
    470 
    471 /*
    472  * Setup internal authenticators once; they are never unregistered.
    473  */
    474 static __unused void
    475 ieee80211_auth_setup(void)
    476 {
    477 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
    478 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
    479 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
    480 }
    481 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
    482 
    483 const struct ieee80211_authenticator *
    484 ieee80211_authenticator_get(int auth)
    485 {
    486 	if (auth >= IEEE80211_AUTH_MAX)
    487 		return NULL;
    488 	if (authenticators[auth] == NULL)
    489 		ieee80211_load_module(auth_modnames[auth]);
    490 	return authenticators[auth];
    491 }
    492 
    493 void
    494 ieee80211_authenticator_register(int type,
    495 	const struct ieee80211_authenticator *auth)
    496 {
    497 	if (type >= IEEE80211_AUTH_MAX)
    498 		return;
    499 	authenticators[type] = auth;
    500 }
    501 
    502 void
    503 ieee80211_authenticator_unregister(int type)
    504 {
    505 
    506 	if (type >= IEEE80211_AUTH_MAX)
    507 		return;
    508 	authenticators[type] = NULL;
    509 }
    510 
    511 /*
    512  * Very simple-minded ACL module support.
    513  */
    514 /* XXX just one for now */
    515 static	const struct ieee80211_aclator *acl = NULL;
    516 
    517 void
    518 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
    519 {
    520 	printf("wlan: %s acl policy registered\n", iac->iac_name);
    521 	acl = iac;
    522 }
    523 
    524 void
    525 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
    526 {
    527 	if (acl == iac)
    528 		acl = NULL;
    529 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
    530 }
    531 
    532 const struct ieee80211_aclator *
    533 ieee80211_aclator_get(const char *name)
    534 {
    535 	if (acl == NULL)
    536 		ieee80211_load_module("wlan_acl");
    537 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
    538 }
    539 
    540 void
    541 ieee80211_print_essid(const uint8_t *essid, int len)
    542 {
    543 	const uint8_t *p;
    544 	int i;
    545 
    546 	if (len > IEEE80211_NWID_LEN)
    547 		len = IEEE80211_NWID_LEN;
    548 	/* determine printable or not */
    549 	for (i = 0, p = essid; i < len; i++, p++) {
    550 		if (*p < ' ' || *p > 0x7e)
    551 			break;
    552 	}
    553 	if (i == len) {
    554 		printf("\"");
    555 		for (i = 0, p = essid; i < len; i++, p++)
    556 			printf("%c", *p);
    557 		printf("\"");
    558 	} else {
    559 		printf("0x");
    560 		for (i = 0, p = essid; i < len; i++, p++)
    561 			printf("%02x", *p);
    562 	}
    563 }
    564 
    565 void
    566 ieee80211_dump_pkt(struct ieee80211com *ic,
    567 	const uint8_t *buf, int len, int rate, int rssi)
    568 {
    569 	const struct ieee80211_frame *wh;
    570 	int i;
    571 
    572 	wh = (const struct ieee80211_frame *)buf;
    573 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    574 	case IEEE80211_FC1_DIR_NODS:
    575 		printf("NODS %s", ether_sprintf(wh->i_addr2));
    576 		printf("->%s", ether_sprintf(wh->i_addr1));
    577 		printf("(%s)", ether_sprintf(wh->i_addr3));
    578 		break;
    579 	case IEEE80211_FC1_DIR_TODS:
    580 		printf("TODS %s", ether_sprintf(wh->i_addr2));
    581 		printf("->%s", ether_sprintf(wh->i_addr3));
    582 		printf("(%s)", ether_sprintf(wh->i_addr1));
    583 		break;
    584 	case IEEE80211_FC1_DIR_FROMDS:
    585 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
    586 		printf("->%s", ether_sprintf(wh->i_addr1));
    587 		printf("(%s)", ether_sprintf(wh->i_addr2));
    588 		break;
    589 	case IEEE80211_FC1_DIR_DSTODS:
    590 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
    591 		printf("->%s", ether_sprintf(wh->i_addr3));
    592 		printf("(%s", ether_sprintf(wh->i_addr2));
    593 		printf("->%s)", ether_sprintf(wh->i_addr1));
    594 		break;
    595 	}
    596 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
    597 	case IEEE80211_FC0_TYPE_DATA:
    598 		printf(" data");
    599 		break;
    600 	case IEEE80211_FC0_TYPE_MGT:
    601 		printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
    602 		break;
    603 	default:
    604 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
    605 		break;
    606 	}
    607 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
    608 		const struct ieee80211_qosframe *qwh =
    609 			(const struct ieee80211_qosframe *)buf;
    610 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
    611 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
    612 	}
    613 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
    614 		int off;
    615 
    616 		off = ieee80211_anyhdrspace(ic, wh);
    617 		printf(" WEP [IV %.02x %.02x %.02x",
    618 			buf[off+0], buf[off+1], buf[off+2]);
    619 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
    620 			printf(" %.02x %.02x %.02x",
    621 				buf[off+4], buf[off+5], buf[off+6]);
    622 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
    623 	}
    624 	if (rate >= 0)
    625 		printf(" %dM", rate / 2);
    626 	if (rssi >= 0)
    627 		printf(" +%d", rssi);
    628 	printf("\n");
    629 	if (len > 0) {
    630 		for (i = 0; i < len; i++) {
    631 			if ((i & 1) == 0)
    632 				printf(" ");
    633 			printf("%02x", buf[i]);
    634 		}
    635 		printf("\n");
    636 	}
    637 }
    638 
    639 static __inline int
    640 findrix(const struct ieee80211_rateset *rs, int r)
    641 {
    642 	int i;
    643 
    644 	for (i = 0; i < rs->rs_nrates; i++)
    645 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
    646 			return i;
    647 	return -1;
    648 }
    649 
    650 int
    651 ieee80211_fix_rate(struct ieee80211_node *ni,
    652 	struct ieee80211_rateset *nrs, int flags)
    653 {
    654 	struct ieee80211vap *vap = ni->ni_vap;
    655 	struct ieee80211com *ic = ni->ni_ic;
    656 	int i, j, rix, error;
    657 	int okrate, badrate, fixedrate, ucastrate;
    658 	const struct ieee80211_rateset *srs;
    659 	uint8_t r;
    660 
    661 	error = 0;
    662 	okrate = badrate = 0;
    663 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
    664 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
    665 		/*
    666 		 * Workaround awkwardness with fixed rate.  We are called
    667 		 * to check both the legacy rate set and the HT rate set
    668 		 * but we must apply any legacy fixed rate check only to the
    669 		 * legacy rate set and vice versa.  We cannot tell what type
    670 		 * of rate set we've been given (legacy or HT) but we can
    671 		 * distinguish the fixed rate type (MCS have 0x80 set).
    672 		 * So to deal with this the caller communicates whether to
    673 		 * check MCS or legacy rate using the flags and we use the
    674 		 * type of any fixed rate to avoid applying an MCS to a
    675 		 * legacy rate and vice versa.
    676 		 */
    677 		if (ucastrate & 0x80) {
    678 			if (flags & IEEE80211_F_DOFRATE)
    679 				flags &= ~IEEE80211_F_DOFRATE;
    680 		} else if ((ucastrate & 0x80) == 0) {
    681 			if (flags & IEEE80211_F_DOFMCS)
    682 				flags &= ~IEEE80211_F_DOFMCS;
    683 		}
    684 		/* NB: required to make MCS match below work */
    685 		ucastrate &= IEEE80211_RATE_VAL;
    686 	}
    687 	fixedrate = IEEE80211_FIXED_RATE_NONE;
    688 	/*
    689 	 * XXX we are called to process both MCS and legacy rates;
    690 	 * we must use the appropriate basic rate set or chaos will
    691 	 * ensue; for now callers that want MCS must supply
    692 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
    693 	 * function so there are two variants, one for MCS and one
    694 	 * for legacy rates.
    695 	 */
    696 	if (flags & IEEE80211_F_DOBRS)
    697 		srs = (const struct ieee80211_rateset *)
    698 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
    699 	else
    700 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
    701 	for (i = 0; i < nrs->rs_nrates; ) {
    702 		if (flags & IEEE80211_F_DOSORT) {
    703 			/*
    704 			 * Sort rates.
    705 			 */
    706 			for (j = i + 1; j < nrs->rs_nrates; j++) {
    707 				if (IEEE80211_RV(nrs->rs_rates[i]) >
    708 				    IEEE80211_RV(nrs->rs_rates[j])) {
    709 					r = nrs->rs_rates[i];
    710 					nrs->rs_rates[i] = nrs->rs_rates[j];
    711 					nrs->rs_rates[j] = r;
    712 				}
    713 			}
    714 		}
    715 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
    716 		badrate = r;
    717 		/*
    718 		 * Check for fixed rate.
    719 		 */
    720 		if (r == ucastrate)
    721 			fixedrate = r;
    722 		/*
    723 		 * Check against supported rates.
    724 		 */
    725 		rix = findrix(srs, r);
    726 		if (flags & IEEE80211_F_DONEGO) {
    727 			if (rix < 0) {
    728 				/*
    729 				 * A rate in the node's rate set is not
    730 				 * supported.  If this is a basic rate and we
    731 				 * are operating as a STA then this is an error.
    732 				 * Otherwise we just discard/ignore the rate.
    733 				 */
    734 				if ((flags & IEEE80211_F_JOIN) &&
    735 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
    736 					error++;
    737 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
    738 				/*
    739 				 * Overwrite with the supported rate
    740 				 * value so any basic rate bit is set.
    741 				 */
    742 				nrs->rs_rates[i] = srs->rs_rates[rix];
    743 			}
    744 		}
    745 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
    746 			/*
    747 			 * Delete unacceptable rates.
    748 			 */
    749 			nrs->rs_nrates--;
    750 			for (j = i; j < nrs->rs_nrates; j++)
    751 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
    752 			nrs->rs_rates[j] = 0;
    753 			continue;
    754 		}
    755 		if (rix >= 0)
    756 			okrate = nrs->rs_rates[i];
    757 		i++;
    758 	}
    759 	if (okrate == 0 || error != 0 ||
    760 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
    761 	     fixedrate != ucastrate)) {
    762 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
    763 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
    764 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
    765 		return badrate | IEEE80211_RATE_BASIC;
    766 	} else
    767 		return IEEE80211_RV(okrate);
    768 }
    769 
    770 /*
    771  * Reset 11g-related state.
    772  */
    773 void
    774 ieee80211_reset_erp(struct ieee80211com *ic)
    775 {
    776 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
    777 	ic->ic_nonerpsta = 0;
    778 	ic->ic_longslotsta = 0;
    779 	/*
    780 	 * Short slot time is enabled only when operating in 11g
    781 	 * and not in an IBSS.  We must also honor whether or not
    782 	 * the driver is capable of doing it.
    783 	 */
    784 	ieee80211_set_shortslottime(ic,
    785 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
    786 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
    787 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
    788 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
    789 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
    790 	/*
    791 	 * Set short preamble and ERP barker-preamble flags.
    792 	 */
    793 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
    794 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
    795 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    796 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
    797 	} else {
    798 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
    799 		ic->ic_flags |= IEEE80211_F_USEBARKER;
    800 	}
    801 }
    802 
    803 /*
    804  * Set the short slot time state and notify the driver.
    805  */
    806 void
    807 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
    808 {
    809 	if (onoff)
    810 		ic->ic_flags |= IEEE80211_F_SHSLOT;
    811 	else
    812 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
    813 	/* notify driver */
    814 	if (ic->ic_updateslot != NULL)
    815 		ic->ic_updateslot(ic);
    816 }
    817 
    818 /*
    819  * Check if the specified rate set supports ERP.
    820  * NB: the rate set is assumed to be sorted.
    821  */
    822 int
    823 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
    824 {
    825 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
    826 	int i, j;
    827 
    828 	if (rs->rs_nrates < nitems(rates))
    829 		return 0;
    830 	for (i = 0; i < nitems(rates); i++) {
    831 		for (j = 0; j < rs->rs_nrates; j++) {
    832 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
    833 			if (rates[i] == r)
    834 				goto next;
    835 			if (r > rates[i])
    836 				return 0;
    837 		}
    838 		return 0;
    839 	next:
    840 		;
    841 	}
    842 	return 1;
    843 }
    844 
    845 /*
    846  * Mark the basic rates for the rate table based on the
    847  * operating mode.  For real 11g we mark all the 11b rates
    848  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    849  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    850  * the basic OFDM rates.
    851  */
    852 static void
    853 setbasicrates(struct ieee80211_rateset *rs,
    854     enum ieee80211_phymode mode, int add)
    855 {
    856 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
    857 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
    858 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
    859 					    /* NB: mixed b/g */
    860 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
    861 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
    862 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
    863 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
    864 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
    865 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
    866 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
    867 					    /* NB: mixed b/g */
    868 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
    869 					    /* NB: mixed b/g */
    870 	    [IEEE80211_MODE_VHT_2GHZ]	= { 4, { 2, 4, 11, 22 } },
    871 	    [IEEE80211_MODE_VHT_5GHZ]	= { 3, { 12, 24, 48 } },
    872 	};
    873 	int i, j;
    874 
    875 	for (i = 0; i < rs->rs_nrates; i++) {
    876 		if (!add)
    877 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    878 		for (j = 0; j < basic[mode].rs_nrates; j++)
    879 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    880 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    881 				break;
    882 			}
    883 	}
    884 }
    885 
    886 /*
    887  * Set the basic rates in a rate set.
    888  */
    889 void
    890 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
    891     enum ieee80211_phymode mode)
    892 {
    893 	setbasicrates(rs, mode, 0);
    894 }
    895 
    896 /*
    897  * Add basic rates to a rate set.
    898  */
    899 void
    900 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
    901     enum ieee80211_phymode mode)
    902 {
    903 	setbasicrates(rs, mode, 1);
    904 }
    905 
    906 /*
    907  * WME protocol support.
    908  *
    909  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
    910  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
    911  * Draft 2.0 Test Plan (Appendix D).
    912  *
    913  * Static/Dynamic Turbo mode settings come from Atheros.
    914  */
    915 typedef struct phyParamType {
    916 	uint8_t		aifsn;
    917 	uint8_t		logcwmin;
    918 	uint8_t		logcwmax;
    919 	uint16_t	txopLimit;
    920 	uint8_t 	acm;
    921 } paramType;
    922 
    923 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
    924 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
    925 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
    926 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
    927 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
    928 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
    929 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
    930 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
    931 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
    932 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
    933 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
    934 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
    935 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
    936 	[IEEE80211_MODE_VHT_2GHZ]	= { 3, 4,  6,  0, 0 },
    937 	[IEEE80211_MODE_VHT_5GHZ]	= { 3, 4,  6,  0, 0 },
    938 };
    939 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
    940 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
    941 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
    942 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
    943 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
    944 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
    945 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
    946 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
    947 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
    948 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
    949 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
    950 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
    951 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
    952 	[IEEE80211_MODE_VHT_2GHZ]	= { 7, 4, 10,  0, 0 },
    953 	[IEEE80211_MODE_VHT_5GHZ]	= { 7, 4, 10,  0, 0 },
    954 };
    955 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
    956 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
    957 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
    958 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
    959 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
    960 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
    961 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
    962 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
    963 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
    964 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
    965 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
    966 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
    967 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
    968 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 3, 4,  94, 0 },
    969 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 3, 4,  94, 0 },
    970 };
    971 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
    972 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
    973 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
    974 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
    975 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
    976 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
    977 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
    978 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
    979 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
    980 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
    981 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
    982 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
    983 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
    984 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 2, 3,  47, 0 },
    985 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 2, 3,  47, 0 },
    986 };
    987 
    988 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
    989 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
    990 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
    991 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
    992 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
    993 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
    994 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
    995 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
    996 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
    997 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
    998 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
    999 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
   1000 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
   1001 };
   1002 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
   1003 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
   1004 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
   1005 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
   1006 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
   1007 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
   1008 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
   1009 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
   1010 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
   1011 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
   1012 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
   1013 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
   1014 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
   1015 };
   1016 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
   1017 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
   1018 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
   1019 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
   1020 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
   1021 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
   1022 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
   1023 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
   1024 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
   1025 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
   1026 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
   1027 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
   1028 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
   1029 };
   1030 
   1031 static void
   1032 _setifsparams(struct wmeParams *wmep, const paramType *phy)
   1033 {
   1034 	wmep->wmep_aifsn = phy->aifsn;
   1035 	wmep->wmep_logcwmin = phy->logcwmin;
   1036 	wmep->wmep_logcwmax = phy->logcwmax;
   1037 	wmep->wmep_txopLimit = phy->txopLimit;
   1038 }
   1039 
   1040 static void
   1041 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
   1042 	struct wmeParams *wmep, const paramType *phy)
   1043 {
   1044 	wmep->wmep_acm = phy->acm;
   1045 	_setifsparams(wmep, phy);
   1046 
   1047 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1048 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
   1049 	    ieee80211_wme_acnames[ac], type,
   1050 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
   1051 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
   1052 }
   1053 
   1054 static void
   1055 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
   1056 {
   1057 	struct ieee80211com *ic = vap->iv_ic;
   1058 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1059 	const paramType *pPhyParam, *pBssPhyParam;
   1060 	struct wmeParams *wmep;
   1061 	enum ieee80211_phymode mode;
   1062 	int i;
   1063 
   1064 	IEEE80211_LOCK_ASSERT(ic);
   1065 
   1066 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
   1067 		return;
   1068 
   1069 	/*
   1070 	 * Clear the wme cap_info field so a qoscount from a previous
   1071 	 * vap doesn't confuse later code which only parses the beacon
   1072 	 * field and updates hardware when said field changes.
   1073 	 * Otherwise the hardware is programmed with defaults, not what
   1074 	 * the beacon actually announces.
   1075 	 */
   1076 	wme->wme_wmeChanParams.cap_info = 0;
   1077 
   1078 	/*
   1079 	 * Select mode; we can be called early in which case we
   1080 	 * always use auto mode.  We know we'll be called when
   1081 	 * entering the RUN state with bsschan setup properly
   1082 	 * so state will eventually get set correctly
   1083 	 */
   1084 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
   1085 		mode = ieee80211_chan2mode(ic->ic_bsschan);
   1086 	else
   1087 		mode = IEEE80211_MODE_AUTO;
   1088 	for (i = 0; i < WME_NUM_AC; i++) {
   1089 		switch (i) {
   1090 		case WME_AC_BK:
   1091 			pPhyParam = &phyParamForAC_BK[mode];
   1092 			pBssPhyParam = &phyParamForAC_BK[mode];
   1093 			break;
   1094 		case WME_AC_VI:
   1095 			pPhyParam = &phyParamForAC_VI[mode];
   1096 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
   1097 			break;
   1098 		case WME_AC_VO:
   1099 			pPhyParam = &phyParamForAC_VO[mode];
   1100 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
   1101 			break;
   1102 		case WME_AC_BE:
   1103 		default:
   1104 			pPhyParam = &phyParamForAC_BE[mode];
   1105 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
   1106 			break;
   1107 		}
   1108 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
   1109 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   1110 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
   1111 		} else {
   1112 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
   1113 		}
   1114 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
   1115 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
   1116 	}
   1117 	/* NB: check ic_bss to avoid NULL deref on initial attach */
   1118 	if (vap->iv_bss != NULL) {
   1119 		/*
   1120 		 * Calculate aggressive mode switching threshold based
   1121 		 * on beacon interval.  This doesn't need locking since
   1122 		 * we're only called before entering the RUN state at
   1123 		 * which point we start sending beacon frames.
   1124 		 */
   1125 		wme->wme_hipri_switch_thresh =
   1126 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
   1127 		wme->wme_flags &= ~WME_F_AGGRMODE;
   1128 		ieee80211_wme_updateparams(vap);
   1129 	}
   1130 }
   1131 
   1132 void
   1133 ieee80211_wme_initparams(struct ieee80211vap *vap)
   1134 {
   1135 	struct ieee80211com *ic = vap->iv_ic;
   1136 
   1137 	IEEE80211_LOCK(ic);
   1138 	ieee80211_wme_initparams_locked(vap);
   1139 	IEEE80211_UNLOCK(ic);
   1140 }
   1141 
   1142 /*
   1143  * Update WME parameters for ourself and the BSS.
   1144  */
   1145 void
   1146 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
   1147 {
   1148 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
   1149 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
   1150 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
   1151 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
   1152 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
   1153 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
   1154 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
   1155 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
   1156 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
   1157 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
   1158 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
   1159 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1160 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1161 	    [IEEE80211_MODE_VHT_2GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1162 	    [IEEE80211_MODE_VHT_5GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1163 	};
   1164 	struct ieee80211com *ic = vap->iv_ic;
   1165 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1166 	const struct wmeParams *wmep;
   1167 	struct wmeParams *chanp, *bssp;
   1168 	enum ieee80211_phymode mode;
   1169 	int i;
   1170 	int do_aggrmode = 0;
   1171 
   1172        	/*
   1173 	 * Set up the channel access parameters for the physical
   1174 	 * device.  First populate the configured settings.
   1175 	 */
   1176 	for (i = 0; i < WME_NUM_AC; i++) {
   1177 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
   1178 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
   1179 		chanp->wmep_aifsn = wmep->wmep_aifsn;
   1180 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
   1181 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
   1182 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
   1183 
   1184 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
   1185 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
   1186 		chanp->wmep_aifsn = wmep->wmep_aifsn;
   1187 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
   1188 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
   1189 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
   1190 	}
   1191 
   1192 	/*
   1193 	 * Select mode; we can be called early in which case we
   1194 	 * always use auto mode.  We know we'll be called when
   1195 	 * entering the RUN state with bsschan setup properly
   1196 	 * so state will eventually get set correctly
   1197 	 */
   1198 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
   1199 		mode = ieee80211_chan2mode(ic->ic_bsschan);
   1200 	else
   1201 		mode = IEEE80211_MODE_AUTO;
   1202 
   1203 	/*
   1204 	 * This implements aggressive mode as found in certain
   1205 	 * vendors' AP's.  When there is significant high
   1206 	 * priority (VI/VO) traffic in the BSS throttle back BE
   1207 	 * traffic by using conservative parameters.  Otherwise
   1208 	 * BE uses aggressive params to optimize performance of
   1209 	 * legacy/non-QoS traffic.
   1210 	 */
   1211 
   1212 	/* Hostap? Only if aggressive mode is enabled */
   1213         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   1214 	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
   1215 		do_aggrmode = 1;
   1216 
   1217 	/*
   1218 	 * Station? Only if we're in a non-QoS BSS.
   1219 	 */
   1220 	else if ((vap->iv_opmode == IEEE80211_M_STA &&
   1221 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
   1222 		do_aggrmode = 1;
   1223 
   1224 	/*
   1225 	 * IBSS? Only if we we have WME enabled.
   1226 	 */
   1227 	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
   1228 	    (vap->iv_flags & IEEE80211_F_WME))
   1229 		do_aggrmode = 1;
   1230 
   1231 	/*
   1232 	 * If WME is disabled on this VAP, default to aggressive mode
   1233 	 * regardless of the configuration.
   1234 	 */
   1235 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
   1236 		do_aggrmode = 1;
   1237 
   1238 	/* XXX WDS? */
   1239 
   1240 	/* XXX MBSS? */
   1241 
   1242 	if (do_aggrmode) {
   1243 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
   1244 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
   1245 
   1246 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
   1247 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
   1248 		    aggrParam[mode].logcwmin;
   1249 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
   1250 		    aggrParam[mode].logcwmax;
   1251 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
   1252 		    (vap->iv_flags & IEEE80211_F_BURST) ?
   1253 			aggrParam[mode].txopLimit : 0;
   1254 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1255 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
   1256 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
   1257 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
   1258 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
   1259 	}
   1260 
   1261 
   1262 	/*
   1263 	 * Change the contention window based on the number of associated
   1264 	 * stations.  If the number of associated stations is 1 and
   1265 	 * aggressive mode is enabled, lower the contention window even
   1266 	 * further.
   1267 	 */
   1268 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   1269 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
   1270 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
   1271 		    [IEEE80211_MODE_AUTO]	= 3,
   1272 		    [IEEE80211_MODE_11A]	= 3,
   1273 		    [IEEE80211_MODE_11B]	= 4,
   1274 		    [IEEE80211_MODE_11G]	= 3,
   1275 		    [IEEE80211_MODE_FH]		= 4,
   1276 		    [IEEE80211_MODE_TURBO_A]	= 3,
   1277 		    [IEEE80211_MODE_TURBO_G]	= 3,
   1278 		    [IEEE80211_MODE_STURBO_A]	= 3,
   1279 		    [IEEE80211_MODE_HALF]	= 3,
   1280 		    [IEEE80211_MODE_QUARTER]	= 3,
   1281 		    [IEEE80211_MODE_11NA]	= 3,
   1282 		    [IEEE80211_MODE_11NG]	= 3,
   1283 		    [IEEE80211_MODE_VHT_2GHZ]	= 3,
   1284 		    [IEEE80211_MODE_VHT_5GHZ]	= 3,
   1285 		};
   1286 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
   1287 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
   1288 
   1289 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
   1290 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1291 		    "update %s (chan+bss) logcwmin %u\n",
   1292 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
   1293 	}
   1294 
   1295 	/*
   1296 	 * Arrange for the beacon update.
   1297 	 *
   1298 	 * XXX what about MBSS, WDS?
   1299 	 */
   1300 	if (vap->iv_opmode == IEEE80211_M_HOSTAP
   1301 	    || vap->iv_opmode == IEEE80211_M_IBSS) {
   1302 		/*
   1303 		 * Arrange for a beacon update and bump the parameter
   1304 		 * set number so associated stations load the new values.
   1305 		 */
   1306 		wme->wme_bssChanParams.cap_info =
   1307 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
   1308 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
   1309 	}
   1310 
   1311 	/* schedule the deferred WME update */
   1312 	ieee80211_runtask(ic, &vap->iv_wme_task);
   1313 
   1314 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1315 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
   1316 	    vap->iv_opmode == IEEE80211_M_STA ?
   1317 		wme->wme_wmeChanParams.cap_info :
   1318 		wme->wme_bssChanParams.cap_info);
   1319 }
   1320 
   1321 void
   1322 ieee80211_wme_updateparams(struct ieee80211vap *vap)
   1323 {
   1324 	struct ieee80211com *ic = vap->iv_ic;
   1325 
   1326 	if (ic->ic_caps & IEEE80211_C_WME) {
   1327 		IEEE80211_LOCK(ic);
   1328 		ieee80211_wme_updateparams_locked(vap);
   1329 		IEEE80211_UNLOCK(ic);
   1330 	}
   1331 }
   1332 
   1333 /*
   1334  * Fetch the WME parameters for the given VAP.
   1335  *
   1336  * When net80211 grows p2p, etc support, this may return different
   1337  * parameters for each VAP.
   1338  */
   1339 void
   1340 ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
   1341 {
   1342 
   1343 	memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
   1344 }
   1345 
   1346 /*
   1347  * For NICs which only support one set of WME paramaters (ie, softmac NICs)
   1348  * there may be different VAP WME parameters but only one is "active".
   1349  * This returns the "NIC" WME parameters for the currently active
   1350  * context.
   1351  */
   1352 void
   1353 ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
   1354 {
   1355 
   1356 	memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
   1357 }
   1358 
   1359 /*
   1360  * Return whether to use QoS on a given WME queue.
   1361  *
   1362  * This is intended to be called from the transmit path of softmac drivers
   1363  * which are setting NoAck bits in transmit descriptors.
   1364  *
   1365  * Ideally this would be set in some transmit field before the packet is
   1366  * queued to the driver but net80211 isn't quite there yet.
   1367  */
   1368 int
   1369 ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
   1370 {
   1371 	/* Bounds/sanity check */
   1372 	if (ac < 0 || ac >= WME_NUM_AC)
   1373 		return (0);
   1374 
   1375 	/* Again, there's only one global context for now */
   1376 	return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
   1377 }
   1378 
   1379 static void
   1380 parent_updown(void *arg, int npending)
   1381 {
   1382 	struct ieee80211com *ic = arg;
   1383 
   1384 	ic->ic_parent(ic);
   1385 }
   1386 
   1387 static void
   1388 update_mcast(void *arg, int npending)
   1389 {
   1390 	struct ieee80211com *ic = arg;
   1391 
   1392 	ic->ic_update_mcast(ic);
   1393 }
   1394 
   1395 static void
   1396 update_promisc(void *arg, int npending)
   1397 {
   1398 	struct ieee80211com *ic = arg;
   1399 
   1400 	ic->ic_update_promisc(ic);
   1401 }
   1402 
   1403 static void
   1404 update_channel(void *arg, int npending)
   1405 {
   1406 	struct ieee80211com *ic = arg;
   1407 
   1408 	ic->ic_set_channel(ic);
   1409 	ieee80211_radiotap_chan_change(ic);
   1410 }
   1411 
   1412 static void
   1413 update_chw(void *arg, int npending)
   1414 {
   1415 	struct ieee80211com *ic = arg;
   1416 
   1417 	/*
   1418 	 * XXX should we defer the channel width _config_ update until now?
   1419 	 */
   1420 	ic->ic_update_chw(ic);
   1421 }
   1422 
   1423 /*
   1424  * Deferred WME update.
   1425  *
   1426  * In preparation for per-VAP WME configuration, call the VAP
   1427  * method if the VAP requires it.  Otherwise, just call the
   1428  * older global method.  There isn't a per-VAP WME configuration
   1429  * just yet so for now just use the global configuration.
   1430  */
   1431 static void
   1432 vap_update_wme(void *arg, int npending)
   1433 {
   1434 	struct ieee80211vap *vap = arg;
   1435 	struct ieee80211com *ic = vap->iv_ic;
   1436 
   1437 	if (vap->iv_wme_update != NULL)
   1438 		vap->iv_wme_update(vap,
   1439 		    ic->ic_wme.wme_chanParams.cap_wmeParams);
   1440 	else
   1441 		ic->ic_wme.wme_update(ic);
   1442 }
   1443 
   1444 static void
   1445 restart_vaps(void *arg, int npending)
   1446 {
   1447 	struct ieee80211com *ic = arg;
   1448 
   1449 	ieee80211_suspend_all(ic);
   1450 	ieee80211_resume_all(ic);
   1451 }
   1452 
   1453 /*
   1454  * Block until the parent is in a known state.  This is
   1455  * used after any operations that dispatch a task (e.g.
   1456  * to auto-configure the parent device up/down).
   1457  */
   1458 void
   1459 ieee80211_waitfor_parent(struct ieee80211com *ic)
   1460 {
   1461 	taskqueue_block(ic->ic_tq);
   1462 	ieee80211_draintask(ic, &ic->ic_parent_task);
   1463 	ieee80211_draintask(ic, &ic->ic_mcast_task);
   1464 	ieee80211_draintask(ic, &ic->ic_promisc_task);
   1465 	ieee80211_draintask(ic, &ic->ic_chan_task);
   1466 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
   1467 	ieee80211_draintask(ic, &ic->ic_chw_task);
   1468 	taskqueue_unblock(ic->ic_tq);
   1469 }
   1470 
   1471 /*
   1472  * Check to see whether the current channel needs reset.
   1473  *
   1474  * Some devices don't handle being given an invalid channel
   1475  * in their operating mode very well (eg wpi(4) will throw a
   1476  * firmware exception.)
   1477  *
   1478  * Return 0 if we're ok, 1 if the channel needs to be reset.
   1479  *
   1480  * See PR kern/202502.
   1481  */
   1482 static int
   1483 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
   1484 {
   1485 	struct ieee80211com *ic = vap->iv_ic;
   1486 
   1487 	if ((vap->iv_opmode == IEEE80211_M_IBSS &&
   1488 	     IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
   1489 	    (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   1490 	     IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
   1491 		return (1);
   1492 	return (0);
   1493 }
   1494 
   1495 /*
   1496  * Reset the curchan to a known good state.
   1497  */
   1498 static void
   1499 ieee80211_start_reset_chan(struct ieee80211vap *vap)
   1500 {
   1501 	struct ieee80211com *ic = vap->iv_ic;
   1502 
   1503 	ic->ic_curchan = &ic->ic_channels[0];
   1504 }
   1505 
   1506 /*
   1507  * Start a vap running.  If this is the first vap to be
   1508  * set running on the underlying device then we
   1509  * automatically bring the device up.
   1510  */
   1511 void
   1512 ieee80211_start_locked(struct ieee80211vap *vap)
   1513 {
   1514 	struct ifnet *ifp = vap->iv_ifp;
   1515 	struct ieee80211com *ic = vap->iv_ic;
   1516 
   1517 	IEEE80211_LOCK_ASSERT(ic);
   1518 
   1519 	IEEE80211_DPRINTF(vap,
   1520 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1521 		"start running, %d vaps running\n", ic->ic_nrunning);
   1522 
   1523 #if __FreeBSD__
   1524 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
   1525 #elif __NetBSD__
   1526 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1527 #endif
   1528 		/*
   1529 		 * Mark us running.  Note that it's ok to do this first;
   1530 		 * if we need to bring the parent device up we defer that
   1531 		 * to avoid dropping the com lock.  We expect the device
   1532 		 * to respond to being marked up by calling back into us
   1533 		 * through ieee80211_start_all at which point we'll come
   1534 		 * back in here and complete the work.
   1535 		 */
   1536 #if __FreeBSD__
   1537 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
   1538 #elif __NetBSD__
   1539 		ifp->if_flags |= IFF_RUNNING;
   1540 #endif
   1541 		/*
   1542 		 * We are not running; if this we are the first vap
   1543 		 * to be brought up auto-up the parent if necessary.
   1544 		 */
   1545 		if (ic->ic_nrunning++ == 0) {
   1546 
   1547 			/* reset the channel to a known good channel */
   1548 			if (ieee80211_start_check_reset_chan(vap))
   1549 				ieee80211_start_reset_chan(vap);
   1550 
   1551 			IEEE80211_DPRINTF(vap,
   1552 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1553 			    "%s: up parent %s\n", __func__, ic->ic_name);
   1554 			ieee80211_runtask(ic, &ic->ic_parent_task);
   1555 			return;
   1556 		}
   1557 	}
   1558 	/*
   1559 	 * If the parent is up and running, then kick the
   1560 	 * 802.11 state machine as appropriate.
   1561 	 */
   1562 	if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
   1563 		if (vap->iv_opmode == IEEE80211_M_STA) {
   1564 #if 0
   1565 			/* XXX bypasses scan too easily; disable for now */
   1566 			/*
   1567 			 * Try to be intelligent about clocking the state
   1568 			 * machine.  If we're currently in RUN state then
   1569 			 * we should be able to apply any new state/parameters
   1570 			 * simply by re-associating.  Otherwise we need to
   1571 			 * re-scan to select an appropriate ap.
   1572 			 */
   1573 			if (vap->iv_state >= IEEE80211_S_RUN)
   1574 				ieee80211_new_state_locked(vap,
   1575 				    IEEE80211_S_ASSOC, 1);
   1576 			else
   1577 #endif
   1578 				ieee80211_new_state_locked(vap,
   1579 				    IEEE80211_S_SCAN, 0);
   1580 		} else {
   1581 			/*
   1582 			 * For monitor+wds mode there's nothing to do but
   1583 			 * start running.  Otherwise if this is the first
   1584 			 * vap to be brought up, start a scan which may be
   1585 			 * preempted if the station is locked to a particular
   1586 			 * channel.
   1587 			 */
   1588 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
   1589 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
   1590 			    vap->iv_opmode == IEEE80211_M_WDS)
   1591 				ieee80211_new_state_locked(vap,
   1592 				    IEEE80211_S_RUN, -1);
   1593 			else
   1594 				ieee80211_new_state_locked(vap,
   1595 				    IEEE80211_S_SCAN, 0);
   1596 		}
   1597 	}
   1598 }
   1599 
   1600 /*
   1601  * Start a single vap.
   1602  */
   1603 void
   1604 ieee80211_init(void *arg)
   1605 {
   1606 	struct ieee80211vap *vap = arg;
   1607 
   1608 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1609 	    "%s\n", __func__);
   1610 
   1611 	IEEE80211_LOCK(vap->iv_ic);
   1612 	ieee80211_start_locked(vap);
   1613 	IEEE80211_UNLOCK(vap->iv_ic);
   1614 }
   1615 
   1616 /*
   1617  * Start all runnable vap's on a device.
   1618  */
   1619 void
   1620 ieee80211_start_all(struct ieee80211com *ic)
   1621 {
   1622 	struct ieee80211vap *vap;
   1623 
   1624 	IEEE80211_LOCK(ic);
   1625 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1626 		struct ifnet *ifp = vap->iv_ifp;
   1627 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
   1628 			ieee80211_start_locked(vap);
   1629 	}
   1630 	IEEE80211_UNLOCK(ic);
   1631 }
   1632 
   1633 /*
   1634  * Stop a vap.  We force it down using the state machine
   1635  * then mark it's ifnet not running.  If this is the last
   1636  * vap running on the underlying device then we close it
   1637  * too to insure it will be properly initialized when the
   1638  * next vap is brought up.
   1639  */
   1640 void
   1641 ieee80211_stop_locked(struct ieee80211vap *vap)
   1642 {
   1643 	struct ieee80211com *ic = vap->iv_ic;
   1644 	struct ifnet *ifp = vap->iv_ifp;
   1645 
   1646 	IEEE80211_LOCK_ASSERT(ic);
   1647 
   1648 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1649 	    "stop running, %d vaps running\n", ic->ic_nrunning);
   1650 
   1651 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
   1652 #if __FreeBSD__
   1653 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
   1654 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
   1655 #elif __NetBSD__
   1656 	if (ifp->if_flags & IFF_RUNNING) {
   1657 		ifp->if_flags &= ~IFF_RUNNING;	/* mark us stopped */
   1658 #endif
   1659 		if (--ic->ic_nrunning == 0) {
   1660 			IEEE80211_DPRINTF(vap,
   1661 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1662 			    "down parent %s\n", ic->ic_name);
   1663 			ieee80211_runtask(ic, &ic->ic_parent_task);
   1664 		}
   1665 	}
   1666 }
   1667 
   1668 void
   1669 ieee80211_stop(struct ieee80211vap *vap)
   1670 {
   1671 	struct ieee80211com *ic = vap->iv_ic;
   1672 
   1673 	IEEE80211_LOCK(ic);
   1674 	ieee80211_stop_locked(vap);
   1675 	IEEE80211_UNLOCK(ic);
   1676 }
   1677 
   1678 /*
   1679  * Stop all vap's running on a device.
   1680  */
   1681 void
   1682 ieee80211_stop_all(struct ieee80211com *ic)
   1683 {
   1684 	struct ieee80211vap *vap;
   1685 
   1686 	IEEE80211_LOCK(ic);
   1687 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1688 		struct ifnet *ifp = vap->iv_ifp;
   1689 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
   1690 			ieee80211_stop_locked(vap);
   1691 	}
   1692 	IEEE80211_UNLOCK(ic);
   1693 
   1694 	ieee80211_waitfor_parent(ic);
   1695 }
   1696 
   1697 /*
   1698  * Stop all vap's running on a device and arrange
   1699  * for those that were running to be resumed.
   1700  */
   1701 void
   1702 ieee80211_suspend_all(struct ieee80211com *ic)
   1703 {
   1704 	struct ieee80211vap *vap;
   1705 
   1706 	IEEE80211_LOCK(ic);
   1707 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1708 		struct ifnet *ifp = vap->iv_ifp;
   1709 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
   1710 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
   1711 			ieee80211_stop_locked(vap);
   1712 		}
   1713 	}
   1714 	IEEE80211_UNLOCK(ic);
   1715 
   1716 	ieee80211_waitfor_parent(ic);
   1717 }
   1718 
   1719 /*
   1720  * Start all vap's marked for resume.
   1721  */
   1722 void
   1723 ieee80211_resume_all(struct ieee80211com *ic)
   1724 {
   1725 	struct ieee80211vap *vap;
   1726 
   1727 	IEEE80211_LOCK(ic);
   1728 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1729 		struct ifnet *ifp = vap->iv_ifp;
   1730 		if (!IFNET_IS_UP_RUNNING(ifp) &&
   1731 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
   1732 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
   1733 			ieee80211_start_locked(vap);
   1734 		}
   1735 	}
   1736 	IEEE80211_UNLOCK(ic);
   1737 }
   1738 
   1739 /*
   1740  * Restart all vap's running on a device.
   1741  */
   1742 void
   1743 ieee80211_restart_all(struct ieee80211com *ic)
   1744 {
   1745 	/*
   1746 	 * NB: do not use ieee80211_runtask here, we will
   1747 	 * block & drain net80211 taskqueue.
   1748 	 */
   1749 	taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
   1750 }
   1751 
   1752 void
   1753 ieee80211_beacon_miss(struct ieee80211com *ic)
   1754 {
   1755 	IEEE80211_LOCK(ic);
   1756 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
   1757 		/* Process in a taskq, the handler may reenter the driver */
   1758 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
   1759 	}
   1760 	IEEE80211_UNLOCK(ic);
   1761 }
   1762 
   1763 static void
   1764 beacon_miss(void *arg, int npending)
   1765 {
   1766 	struct ieee80211com *ic = arg;
   1767 	struct ieee80211vap *vap;
   1768 
   1769 	IEEE80211_LOCK(ic);
   1770 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1771 		/*
   1772 		 * We only pass events through for sta vap's in RUN+ state;
   1773 		 * may be too restrictive but for now this saves all the
   1774 		 * handlers duplicating these checks.
   1775 		 */
   1776 		if (vap->iv_opmode == IEEE80211_M_STA &&
   1777 		    vap->iv_state >= IEEE80211_S_RUN &&
   1778 		    vap->iv_bmiss != NULL)
   1779 			vap->iv_bmiss(vap);
   1780 	}
   1781 	IEEE80211_UNLOCK(ic);
   1782 }
   1783 
   1784 static void
   1785 beacon_swmiss(void *arg, int npending)
   1786 {
   1787 	struct ieee80211vap *vap = arg;
   1788 	struct ieee80211com *ic = vap->iv_ic;
   1789 
   1790 	IEEE80211_LOCK(ic);
   1791 	if (vap->iv_state >= IEEE80211_S_RUN) {
   1792 		/* XXX Call multiple times if npending > zero? */
   1793 		vap->iv_bmiss(vap);
   1794 	}
   1795 	IEEE80211_UNLOCK(ic);
   1796 }
   1797 
   1798 /*
   1799  * Software beacon miss handling.  Check if any beacons
   1800  * were received in the last period.  If not post a
   1801  * beacon miss; otherwise reset the counter.
   1802  */
   1803 void
   1804 ieee80211_swbmiss(void *arg)
   1805 {
   1806 	struct ieee80211vap *vap = arg;
   1807 	struct ieee80211com *ic = vap->iv_ic;
   1808 
   1809 	IEEE80211_LOCK_ASSERT(ic);
   1810 
   1811 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
   1812 	    ("wrong state %d", vap->iv_state));
   1813 
   1814 	if (ic->ic_flags & IEEE80211_F_SCAN) {
   1815 		/*
   1816 		 * If scanning just ignore and reset state.  If we get a
   1817 		 * bmiss after coming out of scan because we haven't had
   1818 		 * time to receive a beacon then we should probe the AP
   1819 		 * before posting a real bmiss (unless iv_bmiss_max has
   1820 		 * been artifiically lowered).  A cleaner solution might
   1821 		 * be to disable the timer on scan start/end but to handle
   1822 		 * case of multiple sta vap's we'd need to disable the
   1823 		 * timers of all affected vap's.
   1824 		 */
   1825 		vap->iv_swbmiss_count = 0;
   1826 	} else if (vap->iv_swbmiss_count == 0) {
   1827 		if (vap->iv_bmiss != NULL)
   1828 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
   1829 	} else
   1830 		vap->iv_swbmiss_count = 0;
   1831 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
   1832 		ieee80211_swbmiss, vap);
   1833 }
   1834 
   1835 /*
   1836  * Start an 802.11h channel switch.  We record the parameters,
   1837  * mark the operation pending, notify each vap through the
   1838  * beacon update mechanism so it can update the beacon frame
   1839  * contents, and then switch vap's to CSA state to block outbound
   1840  * traffic.  Devices that handle CSA directly can use the state
   1841  * switch to do the right thing so long as they call
   1842  * ieee80211_csa_completeswitch when it's time to complete the
   1843  * channel change.  Devices that depend on the net80211 layer can
   1844  * use ieee80211_beacon_update to handle the countdown and the
   1845  * channel switch.
   1846  */
   1847 void
   1848 ieee80211_csa_startswitch(struct ieee80211com *ic,
   1849 	struct ieee80211_channel *c, int mode, int count)
   1850 {
   1851 	struct ieee80211vap *vap;
   1852 
   1853 	IEEE80211_LOCK_ASSERT(ic);
   1854 
   1855 	ic->ic_csa_newchan = c;
   1856 	ic->ic_csa_mode = mode;
   1857 	ic->ic_csa_count = count;
   1858 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
   1859 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1860 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
   1861 		    vap->iv_opmode == IEEE80211_M_IBSS ||
   1862 		    vap->iv_opmode == IEEE80211_M_MBSS)
   1863 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
   1864 		/* switch to CSA state to block outbound traffic */
   1865 		if (vap->iv_state == IEEE80211_S_RUN)
   1866 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
   1867 	}
   1868 	ieee80211_notify_csa(ic, c, mode, count);
   1869 }
   1870 
   1871 /*
   1872  * Complete the channel switch by transitioning all CSA VAPs to RUN.
   1873  * This is called by both the completion and cancellation functions
   1874  * so each VAP is placed back in the RUN state and can thus transmit.
   1875  */
   1876 static void
   1877 csa_completeswitch(struct ieee80211com *ic)
   1878 {
   1879 	struct ieee80211vap *vap;
   1880 
   1881 	ic->ic_csa_newchan = NULL;
   1882 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
   1883 
   1884 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   1885 		if (vap->iv_state == IEEE80211_S_CSA)
   1886 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
   1887 }
   1888 
   1889 /*
   1890  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
   1891  * We clear state and move all vap's in CSA state to RUN state
   1892  * so they can again transmit.
   1893  *
   1894  * Although this may not be completely correct, update the BSS channel
   1895  * for each VAP to the newly configured channel. The setcurchan sets
   1896  * the current operating channel for the interface (so the radio does
   1897  * switch over) but the VAP BSS isn't updated, leading to incorrectly
   1898  * reported information via ioctl.
   1899  */
   1900 void
   1901 ieee80211_csa_completeswitch(struct ieee80211com *ic)
   1902 {
   1903 	struct ieee80211vap *vap;
   1904 
   1905 	IEEE80211_LOCK_ASSERT(ic);
   1906 
   1907 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
   1908 
   1909 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
   1910 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   1911 		if (vap->iv_state == IEEE80211_S_CSA)
   1912 			vap->iv_bss->ni_chan = ic->ic_curchan;
   1913 
   1914 	csa_completeswitch(ic);
   1915 }
   1916 
   1917 /*
   1918  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
   1919  * We clear state and move all vap's in CSA state to RUN state
   1920  * so they can again transmit.
   1921  */
   1922 void
   1923 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
   1924 {
   1925 	IEEE80211_LOCK_ASSERT(ic);
   1926 
   1927 	csa_completeswitch(ic);
   1928 }
   1929 
   1930 /*
   1931  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
   1932  * We clear state and move all vap's in CAC state to RUN state.
   1933  */
   1934 void
   1935 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
   1936 {
   1937 	struct ieee80211com *ic = vap0->iv_ic;
   1938 	struct ieee80211vap *vap;
   1939 
   1940 	IEEE80211_LOCK(ic);
   1941 	/*
   1942 	 * Complete CAC state change for lead vap first; then
   1943 	 * clock all the other vap's waiting.
   1944 	 */
   1945 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
   1946 	    ("wrong state %d", vap0->iv_state));
   1947 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
   1948 
   1949 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   1950 		if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
   1951 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
   1952 	IEEE80211_UNLOCK(ic);
   1953 }
   1954 
   1955 /*
   1956  * Force all vap's other than the specified vap to the INIT state
   1957  * and mark them as waiting for a scan to complete.  These vaps
   1958  * will be brought up when the scan completes and the scanning vap
   1959  * reaches RUN state by wakeupwaiting.
   1960  */
   1961 static void
   1962 markwaiting(struct ieee80211vap *vap0)
   1963 {
   1964 	struct ieee80211com *ic = vap0->iv_ic;
   1965 	struct ieee80211vap *vap;
   1966 
   1967 	IEEE80211_LOCK_ASSERT(ic);
   1968 
   1969 	/*
   1970 	 * A vap list entry can not disappear since we are running on the
   1971 	 * taskqueue and a vap destroy will queue and drain another state
   1972 	 * change task.
   1973 	 */
   1974 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1975 		if (vap == vap0)
   1976 			continue;
   1977 		if (vap->iv_state != IEEE80211_S_INIT) {
   1978 			/* NB: iv_newstate may drop the lock */
   1979 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
   1980 			IEEE80211_LOCK_ASSERT(ic);
   1981 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
   1982 		}
   1983 	}
   1984 }
   1985 
   1986 /*
   1987  * Wakeup all vap's waiting for a scan to complete.  This is the
   1988  * companion to markwaiting (above) and is used to coordinate
   1989  * multiple vaps scanning.
   1990  * This is called from the state taskqueue.
   1991  */
   1992 static void
   1993 wakeupwaiting(struct ieee80211vap *vap0)
   1994 {
   1995 	struct ieee80211com *ic = vap0->iv_ic;
   1996 	struct ieee80211vap *vap;
   1997 
   1998 	IEEE80211_LOCK_ASSERT(ic);
   1999 
   2000 	/*
   2001 	 * A vap list entry can not disappear since we are running on the
   2002 	 * taskqueue and a vap destroy will queue and drain another state
   2003 	 * change task.
   2004 	 */
   2005 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   2006 		if (vap == vap0)
   2007 			continue;
   2008 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
   2009 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
   2010 			/* NB: sta's cannot go INIT->RUN */
   2011 			/* NB: iv_newstate may drop the lock */
   2012 			vap->iv_newstate(vap,
   2013 			    vap->iv_opmode == IEEE80211_M_STA ?
   2014 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
   2015 			IEEE80211_LOCK_ASSERT(ic);
   2016 		}
   2017 	}
   2018 }
   2019 
   2020 /*
   2021  * Handle post state change work common to all operating modes.
   2022  */
   2023 static void
   2024 ieee80211_newstate_cb(void *xvap, int npending)
   2025 {
   2026 	struct ieee80211vap *vap = xvap;
   2027 	struct ieee80211com *ic = vap->iv_ic;
   2028 	enum ieee80211_state nstate, ostate;
   2029 	int arg, rc;
   2030 
   2031 	IEEE80211_LOCK(ic);
   2032 	nstate = vap->iv_nstate;
   2033 	arg = vap->iv_nstate_arg;
   2034 
   2035 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
   2036 		/*
   2037 		 * We have been requested to drop back to the INIT before
   2038 		 * proceeding to the new state.
   2039 		 */
   2040 		/* Deny any state changes while we are here. */
   2041 		vap->iv_nstate = IEEE80211_S_INIT;
   2042 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2043 		    "%s: %s -> %s arg %d\n", __func__,
   2044 		    ieee80211_state_name[vap->iv_state],
   2045 		    ieee80211_state_name[vap->iv_nstate], arg);
   2046 		vap->iv_newstate(vap, vap->iv_nstate, 0);
   2047 		IEEE80211_LOCK_ASSERT(ic);
   2048 		vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
   2049 		    IEEE80211_FEXT_STATEWAIT);
   2050 		/* enqueue new state transition after cancel_scan() task */
   2051 		ieee80211_new_state_locked(vap, nstate, arg);
   2052 		goto done;
   2053 	}
   2054 
   2055 	ostate = vap->iv_state;
   2056 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
   2057 		/*
   2058 		 * SCAN was forced; e.g. on beacon miss.  Force other running
   2059 		 * vap's to INIT state and mark them as waiting for the scan to
   2060 		 * complete.  This insures they don't interfere with our
   2061 		 * scanning.  Since we are single threaded the vaps can not
   2062 		 * transition again while we are executing.
   2063 		 *
   2064 		 * XXX not always right, assumes ap follows sta
   2065 		 */
   2066 		markwaiting(vap);
   2067 	}
   2068 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2069 	    "%s: %s -> %s arg %d\n", __func__,
   2070 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
   2071 
   2072 	rc = vap->iv_newstate(vap, nstate, arg);
   2073 	IEEE80211_LOCK_ASSERT(ic);
   2074 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
   2075 	if (rc != 0) {
   2076 		/* State transition failed */
   2077 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
   2078 		KASSERT(nstate != IEEE80211_S_INIT,
   2079 		    ("INIT state change failed"));
   2080 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2081 		    "%s: %s returned error %d\n", __func__,
   2082 		    ieee80211_state_name[nstate], rc);
   2083 		goto done;
   2084 	}
   2085 
   2086 	/* No actual transition, skip post processing */
   2087 	if (ostate == nstate)
   2088 		goto done;
   2089 
   2090 	if (nstate == IEEE80211_S_RUN) {
   2091 		/*
   2092 		 * OACTIVE may be set on the vap if the upper layer
   2093 		 * tried to transmit (e.g. IPv6 NDP) before we reach
   2094 		 * RUN state.  Clear it and restart xmit.
   2095 		 *
   2096 		 * Note this can also happen as a result of SLEEP->RUN
   2097 		 * (i.e. coming out of power save mode).
   2098 		 */
   2099 #if __FreeBSD__
   2100 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
   2101 #elif __NetBSD__
   2102 		vap->iv_ifp->if_flags &= ~IFF_OACTIVE;
   2103 #endif
   2104 
   2105 		/*
   2106 		 * XXX TODO Kick-start a VAP queue - this should be a method!
   2107 		 */
   2108 
   2109 		/* bring up any vaps waiting on us */
   2110 		wakeupwaiting(vap);
   2111 	} else if (nstate == IEEE80211_S_INIT) {
   2112 		/*
   2113 		 * Flush the scan cache if we did the last scan (XXX?)
   2114 		 * and flush any frames on send queues from this vap.
   2115 		 * Note the mgt q is used only for legacy drivers and
   2116 		 * will go away shortly.
   2117 		 */
   2118 		ieee80211_scan_flush(vap);
   2119 
   2120 		/*
   2121 		 * XXX TODO: ic/vap queue flush
   2122 		 */
   2123 	}
   2124 done:
   2125 	IEEE80211_UNLOCK(ic);
   2126 }
   2127 
   2128 /*
   2129  * Public interface for initiating a state machine change.
   2130  * This routine single-threads the request and coordinates
   2131  * the scheduling of multiple vaps for the purpose of selecting
   2132  * an operating channel.  Specifically the following scenarios
   2133  * are handled:
   2134  * o only one vap can be selecting a channel so on transition to
   2135  *   SCAN state if another vap is already scanning then
   2136  *   mark the caller for later processing and return without
   2137  *   doing anything (XXX? expectations by caller of synchronous operation)
   2138  * o only one vap can be doing CAC of a channel so on transition to
   2139  *   CAC state if another vap is already scanning for radar then
   2140  *   mark the caller for later processing and return without
   2141  *   doing anything (XXX? expectations by caller of synchronous operation)
   2142  * o if another vap is already running when a request is made
   2143  *   to SCAN then an operating channel has been chosen; bypass
   2144  *   the scan and just join the channel
   2145  *
   2146  * Note that the state change call is done through the iv_newstate
   2147  * method pointer so any driver routine gets invoked.  The driver
   2148  * will normally call back into operating mode-specific
   2149  * ieee80211_newstate routines (below) unless it needs to completely
   2150  * bypass the state machine (e.g. because the firmware has it's
   2151  * own idea how things should work).  Bypassing the net80211 layer
   2152  * is usually a mistake and indicates lack of proper integration
   2153  * with the net80211 layer.
   2154  */
   2155 int
   2156 ieee80211_new_state_locked(struct ieee80211vap *vap,
   2157 	enum ieee80211_state nstate, int arg)
   2158 {
   2159 	struct ieee80211com *ic = vap->iv_ic;
   2160 	struct ieee80211vap *vp;
   2161 	enum ieee80211_state ostate;
   2162 	int nrunning, nscanning;
   2163 
   2164 	IEEE80211_LOCK_ASSERT(ic);
   2165 
   2166 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
   2167 		if (vap->iv_nstate == IEEE80211_S_INIT ||
   2168 		    ((vap->iv_state == IEEE80211_S_INIT ||
   2169 		    (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
   2170 		    vap->iv_nstate == IEEE80211_S_SCAN &&
   2171 		    nstate > IEEE80211_S_SCAN)) {
   2172 			/*
   2173 			 * XXX The vap is being stopped/started,
   2174 			 * do not allow any other state changes
   2175 			 * until this is completed.
   2176 			 */
   2177 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2178 			    "%s: %s -> %s (%s) transition discarded\n",
   2179 			    __func__,
   2180 			    ieee80211_state_name[vap->iv_state],
   2181 			    ieee80211_state_name[nstate],
   2182 			    ieee80211_state_name[vap->iv_nstate]);
   2183 			return -1;
   2184 		} else if (vap->iv_state != vap->iv_nstate) {
   2185 #if 0
   2186 			/* Warn if the previous state hasn't completed. */
   2187 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2188 			    "%s: pending %s -> %s transition lost\n", __func__,
   2189 			    ieee80211_state_name[vap->iv_state],
   2190 			    ieee80211_state_name[vap->iv_nstate]);
   2191 #else
   2192 			/* XXX temporarily enable to identify issues */
   2193 			if_printf(vap->iv_ifp,
   2194 			    "%s: pending %s -> %s transition lost\n",
   2195 			    __func__, ieee80211_state_name[vap->iv_state],
   2196 			    ieee80211_state_name[vap->iv_nstate]);
   2197 #endif
   2198 		}
   2199 	}
   2200 
   2201 	nrunning = nscanning = 0;
   2202 	/* XXX can track this state instead of calculating */
   2203 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
   2204 		if (vp != vap) {
   2205 			if (vp->iv_state >= IEEE80211_S_RUN)
   2206 				nrunning++;
   2207 			/* XXX doesn't handle bg scan */
   2208 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
   2209 			else if (vp->iv_state > IEEE80211_S_INIT)
   2210 				nscanning++;
   2211 		}
   2212 	}
   2213 	ostate = vap->iv_state;
   2214 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2215 	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
   2216 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
   2217 	    nrunning, nscanning);
   2218 	switch (nstate) {
   2219 	case IEEE80211_S_SCAN:
   2220 		if (ostate == IEEE80211_S_INIT) {
   2221 			/*
   2222 			 * INIT -> SCAN happens on initial bringup.
   2223 			 */
   2224 			KASSERT(!(nscanning && nrunning),
   2225 			    ("%d scanning and %d running", nscanning, nrunning));
   2226 			if (nscanning) {
   2227 				/*
   2228 				 * Someone is scanning, defer our state
   2229 				 * change until the work has completed.
   2230 				 */
   2231 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2232 				    "%s: defer %s -> %s\n",
   2233 				    __func__, ieee80211_state_name[ostate],
   2234 				    ieee80211_state_name[nstate]);
   2235 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
   2236 				return 0;
   2237 			}
   2238 			if (nrunning) {
   2239 				/*
   2240 				 * Someone is operating; just join the channel
   2241 				 * they have chosen.
   2242 				 */
   2243 				/* XXX kill arg? */
   2244 				/* XXX check each opmode, adhoc? */
   2245 				if (vap->iv_opmode == IEEE80211_M_STA)
   2246 					nstate = IEEE80211_S_SCAN;
   2247 				else
   2248 					nstate = IEEE80211_S_RUN;
   2249 #ifdef IEEE80211_DEBUG
   2250 				if (nstate != IEEE80211_S_SCAN) {
   2251 					IEEE80211_DPRINTF(vap,
   2252 					    IEEE80211_MSG_STATE,
   2253 					    "%s: override, now %s -> %s\n",
   2254 					    __func__,
   2255 					    ieee80211_state_name[ostate],
   2256 					    ieee80211_state_name[nstate]);
   2257 				}
   2258 #endif
   2259 			}
   2260 		}
   2261 		break;
   2262 	case IEEE80211_S_RUN:
   2263 		if (vap->iv_opmode == IEEE80211_M_WDS &&
   2264 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
   2265 		    nscanning) {
   2266 			/*
   2267 			 * Legacy WDS with someone else scanning; don't
   2268 			 * go online until that completes as we should
   2269 			 * follow the other vap to the channel they choose.
   2270 			 */
   2271 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2272 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
   2273 			     ieee80211_state_name[ostate],
   2274 			     ieee80211_state_name[nstate]);
   2275 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
   2276 			return 0;
   2277 		}
   2278 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   2279 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
   2280 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
   2281 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
   2282 			/*
   2283 			 * This is a DFS channel, transition to CAC state
   2284 			 * instead of RUN.  This allows us to initiate
   2285 			 * Channel Availability Check (CAC) as specified
   2286 			 * by 11h/DFS.
   2287 			 */
   2288 			nstate = IEEE80211_S_CAC;
   2289 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2290 			     "%s: override %s -> %s (DFS)\n", __func__,
   2291 			     ieee80211_state_name[ostate],
   2292 			     ieee80211_state_name[nstate]);
   2293 		}
   2294 		break;
   2295 	case IEEE80211_S_INIT:
   2296 		/* cancel any scan in progress */
   2297 		ieee80211_cancel_scan(vap);
   2298 		if (ostate == IEEE80211_S_INIT ) {
   2299 			/* XXX don't believe this */
   2300 			/* INIT -> INIT. nothing to do */
   2301 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
   2302 		}
   2303 		/* fall thru... */
   2304 	default:
   2305 		break;
   2306 	}
   2307 	/* defer the state change to a thread */
   2308 	vap->iv_nstate = nstate;
   2309 	vap->iv_nstate_arg = arg;
   2310 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
   2311 	ieee80211_runtask(ic, &vap->iv_nstate_task);
   2312 	return EINPROGRESS;
   2313 }
   2314 
   2315 int
   2316 ieee80211_new_state(struct ieee80211vap *vap,
   2317 	enum ieee80211_state nstate, int arg)
   2318 {
   2319 	struct ieee80211com *ic = vap->iv_ic;
   2320 	int rc;
   2321 
   2322 	IEEE80211_LOCK(ic);
   2323 	rc = ieee80211_new_state_locked(vap, nstate, arg);
   2324 	IEEE80211_UNLOCK(ic);
   2325 	return rc;
   2326 }
   2327