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