xref: /freebsd/sys/net80211/ieee80211_proto.c (revision d72d72d38110a7f7dae1703fdffdbdf5e9b32fb3)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3b032f27cSSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4*d72d72d3SAndriy Voskoboinyk  * Copyright (c) 2012 IEEE
51a1e1d21SSam Leffler  * All rights reserved.
61a1e1d21SSam Leffler  *
71a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
81a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
91a1e1d21SSam Leffler  * are met:
101a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
117535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
127535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
137535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
147535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
151a1e1d21SSam Leffler  *
167535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
207535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
217535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
237535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
257535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261a1e1d21SSam Leffler  */
271a1e1d21SSam Leffler 
281a1e1d21SSam Leffler #include <sys/cdefs.h>
291a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
301a1e1d21SSam Leffler 
311a1e1d21SSam Leffler /*
321a1e1d21SSam Leffler  * IEEE 802.11 protocol support.
331a1e1d21SSam Leffler  */
341a1e1d21SSam Leffler 
351a1e1d21SSam Leffler #include "opt_inet.h"
36b032f27cSSam Leffler #include "opt_wlan.h"
371a1e1d21SSam Leffler 
381a1e1d21SSam Leffler #include <sys/param.h>
398a1b9b6aSSam Leffler #include <sys/systm.h>
408ec07310SGleb Smirnoff #include <sys/kernel.h>
418ec07310SGleb Smirnoff #include <sys/malloc.h>
421a1e1d21SSam Leffler 
438a1b9b6aSSam Leffler #include <sys/socket.h>
44b032f27cSSam Leffler #include <sys/sockio.h>
451a1e1d21SSam Leffler 
461a1e1d21SSam Leffler #include <net/if.h>
4776039bc8SGleb Smirnoff #include <net/if_var.h>
481a1e1d21SSam Leffler #include <net/if_media.h>
498a1b9b6aSSam Leffler #include <net/ethernet.h>		/* XXX for ether_sprintf */
501a1e1d21SSam Leffler 
511a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
52b032f27cSSam Leffler #include <net80211/ieee80211_adhoc.h>
53b032f27cSSam Leffler #include <net80211/ieee80211_sta.h>
54b032f27cSSam Leffler #include <net80211/ieee80211_hostap.h>
55b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5659aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
5759aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
5859aa14a9SRui Paulo #endif
59b032f27cSSam Leffler #include <net80211/ieee80211_monitor.h>
60b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
611a1e1d21SSam Leffler 
628a1b9b6aSSam Leffler /* XXX tunables */
638a1b9b6aSSam Leffler #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
648a1b9b6aSSam Leffler #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
651a1e1d21SSam Leffler 
661a1e1d21SSam Leffler const char *ieee80211_mgt_subtype_name[] = {
671a1e1d21SSam Leffler 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
68665d5ae9SAndriy Voskoboinyk 	"probe_req",	"probe_resp",	"timing_adv",	"reserved#7",
691a1e1d21SSam Leffler 	"beacon",	"atim",		"disassoc",	"auth",
7096283082SBernhard Schmidt 	"deauth",	"action",	"action_noack",	"reserved#15"
711a1e1d21SSam Leffler };
728a1b9b6aSSam Leffler const char *ieee80211_ctl_subtype_name[] = {
738a1b9b6aSSam Leffler 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
74665d5ae9SAndriy Voskoboinyk 	"reserved#4",	"reserved#5",	"reserved#6",	"control_wrap",
75665d5ae9SAndriy Voskoboinyk 	"bar",		"ba",		"ps_poll",	"rts",
768a1b9b6aSSam Leffler 	"cts",		"ack",		"cf_end",	"cf_end_ack"
778a1b9b6aSSam Leffler };
7849aa47d6SSam Leffler const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
7949aa47d6SSam Leffler 	"IBSS",		/* IEEE80211_M_IBSS */
8049aa47d6SSam Leffler 	"STA",		/* IEEE80211_M_STA */
81b032f27cSSam Leffler 	"WDS",		/* IEEE80211_M_WDS */
8249aa47d6SSam Leffler 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
8349aa47d6SSam Leffler 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
8459aa14a9SRui Paulo 	"MONITOR",	/* IEEE80211_M_MONITOR */
8559aa14a9SRui Paulo 	"MBSS"		/* IEEE80211_M_MBSS */
8649aa47d6SSam Leffler };
87a11c9a5cSSam Leffler const char *ieee80211_state_name[IEEE80211_S_MAX] = {
88a11c9a5cSSam Leffler 	"INIT",		/* IEEE80211_S_INIT */
89a11c9a5cSSam Leffler 	"SCAN",		/* IEEE80211_S_SCAN */
90a11c9a5cSSam Leffler 	"AUTH",		/* IEEE80211_S_AUTH */
91a11c9a5cSSam Leffler 	"ASSOC",	/* IEEE80211_S_ASSOC */
9214fb6b8fSSam Leffler 	"CAC",		/* IEEE80211_S_CAC */
9314fb6b8fSSam Leffler 	"RUN",		/* IEEE80211_S_RUN */
9414fb6b8fSSam Leffler 	"CSA",		/* IEEE80211_S_CSA */
9514fb6b8fSSam Leffler 	"SLEEP",	/* IEEE80211_S_SLEEP */
96a11c9a5cSSam Leffler };
978a1b9b6aSSam Leffler const char *ieee80211_wme_acnames[] = {
988a1b9b6aSSam Leffler 	"WME_AC_BE",
998a1b9b6aSSam Leffler 	"WME_AC_BK",
1008a1b9b6aSSam Leffler 	"WME_AC_VI",
1018a1b9b6aSSam Leffler 	"WME_AC_VO",
1028a1b9b6aSSam Leffler 	"WME_UPSD",
1038a1b9b6aSSam Leffler };
104a11c9a5cSSam Leffler 
105*d72d72d3SAndriy Voskoboinyk 
106*d72d72d3SAndriy Voskoboinyk /*
107*d72d72d3SAndriy Voskoboinyk  * Reason code descriptions were (mostly) obtained from
108*d72d72d3SAndriy Voskoboinyk  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
109*d72d72d3SAndriy Voskoboinyk  */
110*d72d72d3SAndriy Voskoboinyk const char *
111*d72d72d3SAndriy Voskoboinyk ieee80211_reason_to_string(uint16_t reason)
112*d72d72d3SAndriy Voskoboinyk {
113*d72d72d3SAndriy Voskoboinyk 	switch (reason) {
114*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSPECIFIED:
115*d72d72d3SAndriy Voskoboinyk 		return ("unspecified");
116*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AUTH_EXPIRE:
117*d72d72d3SAndriy Voskoboinyk 		return ("previous authentication is expired");
118*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AUTH_LEAVE:
119*d72d72d3SAndriy Voskoboinyk 		return ("sending STA is leaving/has left IBSS or ESS");
120*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_EXPIRE:
121*d72d72d3SAndriy Voskoboinyk 		return ("disassociated due to inactivity");
122*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_TOOMANY:
123*d72d72d3SAndriy Voskoboinyk 		return ("too many associated STAs");
124*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_NOT_AUTHED:
125*d72d72d3SAndriy Voskoboinyk 		return ("class 2 frame received from nonauthenticated STA");
126*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_NOT_ASSOCED:
127*d72d72d3SAndriy Voskoboinyk 		return ("class 3 frame received from nonassociated STA");
128*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_LEAVE:
129*d72d72d3SAndriy Voskoboinyk 		return ("sending STA is leaving/has left BSS");
130*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
131*d72d72d3SAndriy Voskoboinyk 		return ("STA requesting (re)association is not authenticated");
132*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
133*d72d72d3SAndriy Voskoboinyk 		return ("information in the Power Capability element is "
134*d72d72d3SAndriy Voskoboinyk 			"unacceptable");
135*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
136*d72d72d3SAndriy Voskoboinyk 		return ("information in the Supported Channels element is "
137*d72d72d3SAndriy Voskoboinyk 			"unacceptable");
138*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_IE_INVALID:
139*d72d72d3SAndriy Voskoboinyk 		return ("invalid element");
140*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MIC_FAILURE:
141*d72d72d3SAndriy Voskoboinyk 		return ("MIC failure");
142*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
143*d72d72d3SAndriy Voskoboinyk 		return ("4-Way handshake timeout");
144*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
145*d72d72d3SAndriy Voskoboinyk 		return ("group key update timeout");
146*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
147*d72d72d3SAndriy Voskoboinyk 		return ("element in 4-Way handshake different from "
148*d72d72d3SAndriy Voskoboinyk 			"(re)association request/probe response/beacon frame");
149*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_GROUP_CIPHER_INVALID:
150*d72d72d3SAndriy Voskoboinyk 		return ("invalid group cipher");
151*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
152*d72d72d3SAndriy Voskoboinyk 		return ("invalid pairwise cipher");
153*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AKMP_INVALID:
154*d72d72d3SAndriy Voskoboinyk 		return ("invalid AKMP");
155*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
156*d72d72d3SAndriy Voskoboinyk 		return ("unsupported version in RSN IE");
157*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_INVALID_RSN_IE_CAP:
158*d72d72d3SAndriy Voskoboinyk 		return ("invalid capabilities in RSN IE");
159*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_802_1X_AUTH_FAILED:
160*d72d72d3SAndriy Voskoboinyk 		return ("IEEE 802.1X authentication failed");
161*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
162*d72d72d3SAndriy Voskoboinyk 		return ("cipher suite rejected because of the security "
163*d72d72d3SAndriy Voskoboinyk 			"policy");
164*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSPECIFIED_QOS:
165*d72d72d3SAndriy Voskoboinyk 		return ("unspecified (QoS-related)");
166*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_INSUFFICIENT_BW:
167*d72d72d3SAndriy Voskoboinyk 		return ("QoS AP lacks sufficient bandwidth for this QoS STA");
168*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_TOOMANY_FRAMES:
169*d72d72d3SAndriy Voskoboinyk 		return ("too many frames need to be acknowledged");
170*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_OUTSIDE_TXOP:
171*d72d72d3SAndriy Voskoboinyk 		return ("STA is transmitting outside the limits of its TXOPs");
172*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_LEAVING_QBSS:
173*d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (the STA is "
174*d72d72d3SAndriy Voskoboinyk 			"resetting/leaving the BSS)");
175*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_BAD_MECHANISM:
176*d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (it does not want to use "
177*d72d72d3SAndriy Voskoboinyk 			"the mechanism)");
178*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_SETUP_NEEDED:
179*d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (setup is required for the "
180*d72d72d3SAndriy Voskoboinyk 			"used mechanism)");
181*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_TIMEOUT:
182*d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (timeout)");
183*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_PEER_LINK_CANCELED:
184*d72d72d3SAndriy Voskoboinyk 		return ("SME cancels the mesh peering instance (not related "
185*d72d72d3SAndriy Voskoboinyk 			"to the maximum number of peer mesh STAs)");
186*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAX_PEERS:
187*d72d72d3SAndriy Voskoboinyk 		return ("maximum number of peer mesh STAs was reached");
188*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CPVIOLATION:
189*d72d72d3SAndriy Voskoboinyk 		return ("the received information violates the Mesh "
190*d72d72d3SAndriy Voskoboinyk 			"Configuration policy configured in the mesh STA "
191*d72d72d3SAndriy Voskoboinyk 			"profile");
192*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CLOSE_RCVD:
193*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA has received a Mesh Peering Close "
194*d72d72d3SAndriy Voskoboinyk 			"message requesting to close the mesh peering");
195*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAX_RETRIES:
196*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
197*d72d72d3SAndriy Voskoboinyk 			"Peering Open messages, without receiving a Mesh "
198*d72d72d3SAndriy Voskoboinyk 			"Peering Confirm message");
199*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
200*d72d72d3SAndriy Voskoboinyk 		return ("the confirmTimer for the mesh peering instance times "
201*d72d72d3SAndriy Voskoboinyk 			"out");
202*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INVALID_GTK:
203*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA fails to unwrap the GTK or the values "
204*d72d72d3SAndriy Voskoboinyk 			"in the wrapped contents do not match");
205*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INCONS_PARAMS:
206*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA receives inconsistent information about "
207*d72d72d3SAndriy Voskoboinyk 			"the mesh parameters between Mesh Peering Management "
208*d72d72d3SAndriy Voskoboinyk 			"frames");
209*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INVALID_SECURITY:
210*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA fails the authenticated mesh peering "
211*d72d72d3SAndriy Voskoboinyk 			"exchange because due to failure in selecting "
212*d72d72d3SAndriy Voskoboinyk 			"pairwise/group ciphersuite");
213*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_NO_PROXY:
214*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA does not have proxy information for "
215*d72d72d3SAndriy Voskoboinyk 			"this external destination");
216*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_NO_FI:
217*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA does not have forwarding information "
218*d72d72d3SAndriy Voskoboinyk 			"for this destination");
219*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
220*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA determines that the link to the next "
221*d72d72d3SAndriy Voskoboinyk 			"hop of an active path in its forwarding information "
222*d72d72d3SAndriy Voskoboinyk 			"is no longer usable");
223*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
224*d72d72d3SAndriy Voskoboinyk 		return ("the MAC address of the STA already exists in the "
225*d72d72d3SAndriy Voskoboinyk 			"mesh BSS");
226*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
227*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA performs channel switch to meet "
228*d72d72d3SAndriy Voskoboinyk 			"regulatory requirements");
229*d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
230*d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA performs channel switch with "
231*d72d72d3SAndriy Voskoboinyk 			"unspecified reason");
232*d72d72d3SAndriy Voskoboinyk 	default:
233*d72d72d3SAndriy Voskoboinyk 		return ("reserved/unknown");
234*d72d72d3SAndriy Voskoboinyk 	}
235*d72d72d3SAndriy Voskoboinyk }
236*d72d72d3SAndriy Voskoboinyk 
2375efea30fSAndrew Thompson static void beacon_miss(void *, int);
2385efea30fSAndrew Thompson static void beacon_swmiss(void *, int);
239b032f27cSSam Leffler static void parent_updown(void *, int);
2405efea30fSAndrew Thompson static void update_mcast(void *, int);
2415efea30fSAndrew Thompson static void update_promisc(void *, int);
2425efea30fSAndrew Thompson static void update_channel(void *, int);
243b94299c4SAdrian Chadd static void update_chw(void *, int);
244dd2fb488SAdrian Chadd static void update_wme(void *, int);
2454061c639SAndriy Voskoboinyk static void restart_vaps(void *, int);
2465efea30fSAndrew Thompson static void ieee80211_newstate_cb(void *, int);
2471a1e1d21SSam Leffler 
248b032f27cSSam Leffler static int
249b032f27cSSam Leffler null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
250b032f27cSSam Leffler 	const struct ieee80211_bpf_params *params)
251b105a069SSam Leffler {
252b032f27cSSam Leffler 
253c8f5794eSGleb Smirnoff 	ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
254b032f27cSSam Leffler 	m_freem(m);
255b032f27cSSam Leffler 	return ENETDOWN;
256b105a069SSam Leffler }
257b105a069SSam Leffler 
2581a1e1d21SSam Leffler void
2598a1b9b6aSSam Leffler ieee80211_proto_attach(struct ieee80211com *ic)
2601a1e1d21SSam Leffler {
2617a79cebfSGleb Smirnoff 	uint8_t hdrlen;
2621a1e1d21SSam Leffler 
263b032f27cSSam Leffler 	/* override the 802.3 setting */
2647a79cebfSGleb Smirnoff 	hdrlen = ic->ic_headroom
265b032f27cSSam Leffler 		+ sizeof(struct ieee80211_qosframe_addr4)
266b032f27cSSam Leffler 		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
267b032f27cSSam Leffler 		+ IEEE80211_WEP_EXTIVLEN;
268b032f27cSSam Leffler 	/* XXX no way to recalculate on ifdetach */
2697a79cebfSGleb Smirnoff 	if (ALIGN(hdrlen) > max_linkhdr) {
270b032f27cSSam Leffler 		/* XXX sanity check... */
2717a79cebfSGleb Smirnoff 		max_linkhdr = ALIGN(hdrlen);
272b032f27cSSam Leffler 		max_hdr = max_linkhdr + max_protohdr;
273b032f27cSSam Leffler 		max_datalen = MHLEN - max_hdr;
274b032f27cSSam Leffler 	}
2752e79ca97SSam Leffler 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
276b032f27cSSam Leffler 
2777a79cebfSGleb Smirnoff 	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
2785efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
2795efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
2805efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
2815efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
282b94299c4SAdrian Chadd 	TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
283dd2fb488SAdrian Chadd 	TASK_INIT(&ic->ic_wme_task, 0, update_wme, ic);
2844061c639SAndriy Voskoboinyk 	TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
2858a1b9b6aSSam Leffler 
2868a1b9b6aSSam Leffler 	ic->ic_wme.wme_hipri_switch_hysteresis =
2878a1b9b6aSSam Leffler 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
2881a1e1d21SSam Leffler 
2891a1e1d21SSam Leffler 	/* initialize management frame handlers */
2901a1e1d21SSam Leffler 	ic->ic_send_mgmt = ieee80211_send_mgmt;
291b032f27cSSam Leffler 	ic->ic_raw_xmit = null_raw_xmit;
292b032f27cSSam Leffler 
293b032f27cSSam Leffler 	ieee80211_adhoc_attach(ic);
294b032f27cSSam Leffler 	ieee80211_sta_attach(ic);
295b032f27cSSam Leffler 	ieee80211_wds_attach(ic);
296b032f27cSSam Leffler 	ieee80211_hostap_attach(ic);
29759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
29859aa14a9SRui Paulo 	ieee80211_mesh_attach(ic);
29959aa14a9SRui Paulo #endif
300b032f27cSSam Leffler 	ieee80211_monitor_attach(ic);
3011a1e1d21SSam Leffler }
3021a1e1d21SSam Leffler 
3031a1e1d21SSam Leffler void
3048a1b9b6aSSam Leffler ieee80211_proto_detach(struct ieee80211com *ic)
3051a1e1d21SSam Leffler {
306b032f27cSSam Leffler 	ieee80211_monitor_detach(ic);
30759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
30859aa14a9SRui Paulo 	ieee80211_mesh_detach(ic);
30959aa14a9SRui Paulo #endif
310b032f27cSSam Leffler 	ieee80211_hostap_detach(ic);
311b032f27cSSam Leffler 	ieee80211_wds_detach(ic);
312b032f27cSSam Leffler 	ieee80211_adhoc_detach(ic);
313b032f27cSSam Leffler 	ieee80211_sta_detach(ic);
314b032f27cSSam Leffler }
3158a1b9b6aSSam Leffler 
316b032f27cSSam Leffler static void
317b032f27cSSam Leffler null_update_beacon(struct ieee80211vap *vap, int item)
318b032f27cSSam Leffler {
319b032f27cSSam Leffler }
320b032f27cSSam Leffler 
321b032f27cSSam Leffler void
322b032f27cSSam Leffler ieee80211_proto_vattach(struct ieee80211vap *vap)
323b032f27cSSam Leffler {
324b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
325b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
326b032f27cSSam Leffler 	int i;
327b032f27cSSam Leffler 
328b032f27cSSam Leffler 	/* override the 802.3 setting */
3297a79cebfSGleb Smirnoff 	ifp->if_hdrlen = ic->ic_headroom
3307a79cebfSGleb Smirnoff                 + sizeof(struct ieee80211_qosframe_addr4)
3317a79cebfSGleb Smirnoff                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
3327a79cebfSGleb Smirnoff                 + IEEE80211_WEP_EXTIVLEN;
333b032f27cSSam Leffler 
334b032f27cSSam Leffler 	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
335b032f27cSSam Leffler 	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
336b032f27cSSam Leffler 	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
33723401900SAdrian Chadd 	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
338fd90e2edSJung-uk Kim 	callout_init(&vap->iv_mgtsend, 1);
3395efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
3405efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
341b032f27cSSam Leffler 	/*
342b032f27cSSam Leffler 	 * Install default tx rate handling: no fixed rate, lowest
343b032f27cSSam Leffler 	 * supported rate for mgmt and multicast frames.  Default
344b032f27cSSam Leffler 	 * max retry count.  These settings can be changed by the
345b032f27cSSam Leffler 	 * driver and/or user applications.
346b032f27cSSam Leffler 	 */
347047db6b3SSam Leffler 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
348b032f27cSSam Leffler 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
349b032f27cSSam Leffler 
350b032f27cSSam Leffler 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
351338452c9SAdrian Chadd 
352338452c9SAdrian Chadd 		/*
353338452c9SAdrian Chadd 		 * Setting the management rate to MCS 0 assumes that the
354338452c9SAdrian Chadd 		 * BSS Basic rate set is empty and the BSS Basic MCS set
355338452c9SAdrian Chadd 		 * is not.
356338452c9SAdrian Chadd 		 *
357338452c9SAdrian Chadd 		 * Since we're not checking this, default to the lowest
358338452c9SAdrian Chadd 		 * defined rate for this mode.
359338452c9SAdrian Chadd 		 *
360338452c9SAdrian Chadd 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
361338452c9SAdrian Chadd 		 * some MCS management traffic (eg BA response frames.)
362338452c9SAdrian Chadd 		 *
363338452c9SAdrian Chadd 		 * See also: 9.6.0 of the 802.11n-2009 specification.
364338452c9SAdrian Chadd 		 */
365338452c9SAdrian Chadd #ifdef	NOTYET
366047db6b3SSam Leffler 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
367047db6b3SSam Leffler 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
368047db6b3SSam Leffler 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
369047db6b3SSam Leffler 		} else {
370b032f27cSSam Leffler 			vap->iv_txparms[i].mgmtrate =
371b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
372b032f27cSSam Leffler 			vap->iv_txparms[i].mcastrate =
373b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
374b032f27cSSam Leffler 		}
375338452c9SAdrian Chadd #endif
376338452c9SAdrian Chadd 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
377338452c9SAdrian Chadd 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
378b032f27cSSam Leffler 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
379b032f27cSSam Leffler 	}
380b032f27cSSam Leffler 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
381b032f27cSSam Leffler 
382b032f27cSSam Leffler 	vap->iv_update_beacon = null_update_beacon;
383b032f27cSSam Leffler 	vap->iv_deliver_data = ieee80211_deliver_data;
384b032f27cSSam Leffler 
385b032f27cSSam Leffler 	/* attach support for operating mode */
386b032f27cSSam Leffler 	ic->ic_vattach[vap->iv_opmode](vap);
387b032f27cSSam Leffler }
388b032f27cSSam Leffler 
389b032f27cSSam Leffler void
390b032f27cSSam Leffler ieee80211_proto_vdetach(struct ieee80211vap *vap)
391b032f27cSSam Leffler {
392b032f27cSSam Leffler #define	FREEAPPIE(ie) do { \
393b032f27cSSam Leffler 	if (ie != NULL) \
394b9b53389SAdrian Chadd 		IEEE80211_FREE(ie, M_80211_NODE_IE); \
395b032f27cSSam Leffler } while (0)
396b032f27cSSam Leffler 	/*
397b032f27cSSam Leffler 	 * Detach operating mode module.
398b032f27cSSam Leffler 	 */
399b032f27cSSam Leffler 	if (vap->iv_opdetach != NULL)
400b032f27cSSam Leffler 		vap->iv_opdetach(vap);
4018a1b9b6aSSam Leffler 	/*
4028a1b9b6aSSam Leffler 	 * This should not be needed as we detach when reseting
4038a1b9b6aSSam Leffler 	 * the state but be conservative here since the
4048a1b9b6aSSam Leffler 	 * authenticator may do things like spawn kernel threads.
4058a1b9b6aSSam Leffler 	 */
406b032f27cSSam Leffler 	if (vap->iv_auth->ia_detach != NULL)
407b032f27cSSam Leffler 		vap->iv_auth->ia_detach(vap);
4088a1b9b6aSSam Leffler 	/*
4098a1b9b6aSSam Leffler 	 * Detach any ACL'ator.
4108a1b9b6aSSam Leffler 	 */
411b032f27cSSam Leffler 	if (vap->iv_acl != NULL)
412b032f27cSSam Leffler 		vap->iv_acl->iac_detach(vap);
413b032f27cSSam Leffler 
414b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_beacon);
415b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_probereq);
416b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_proberesp);
417b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocreq);
418b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocresp);
419b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_wpa);
420b032f27cSSam Leffler #undef FREEAPPIE
4218a1b9b6aSSam Leffler }
4228a1b9b6aSSam Leffler 
4238a1b9b6aSSam Leffler /*
4248a1b9b6aSSam Leffler  * Simple-minded authenticator module support.
4258a1b9b6aSSam Leffler  */
4268a1b9b6aSSam Leffler 
4278a1b9b6aSSam Leffler #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
4288a1b9b6aSSam Leffler /* XXX well-known names */
4298a1b9b6aSSam Leffler static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
4308a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
4318a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
4328a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
4338a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
4348a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
4358a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
4368a1b9b6aSSam Leffler };
4378a1b9b6aSSam Leffler static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
4388a1b9b6aSSam Leffler 
4398a1b9b6aSSam Leffler static const struct ieee80211_authenticator auth_internal = {
4408a1b9b6aSSam Leffler 	.ia_name		= "wlan_internal",
4418a1b9b6aSSam Leffler 	.ia_attach		= NULL,
4428a1b9b6aSSam Leffler 	.ia_detach		= NULL,
4438a1b9b6aSSam Leffler 	.ia_node_join		= NULL,
4448a1b9b6aSSam Leffler 	.ia_node_leave		= NULL,
4458a1b9b6aSSam Leffler };
4468a1b9b6aSSam Leffler 
4478a1b9b6aSSam Leffler /*
4488a1b9b6aSSam Leffler  * Setup internal authenticators once; they are never unregistered.
4498a1b9b6aSSam Leffler  */
4508a1b9b6aSSam Leffler static void
4518a1b9b6aSSam Leffler ieee80211_auth_setup(void)
4528a1b9b6aSSam Leffler {
4538a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
4548a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
4558a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
4568a1b9b6aSSam Leffler }
4578a1b9b6aSSam Leffler SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
4588a1b9b6aSSam Leffler 
4598a1b9b6aSSam Leffler const struct ieee80211_authenticator *
4608a1b9b6aSSam Leffler ieee80211_authenticator_get(int auth)
4618a1b9b6aSSam Leffler {
4628a1b9b6aSSam Leffler 	if (auth >= IEEE80211_AUTH_MAX)
4638a1b9b6aSSam Leffler 		return NULL;
4648a1b9b6aSSam Leffler 	if (authenticators[auth] == NULL)
4658a1b9b6aSSam Leffler 		ieee80211_load_module(auth_modnames[auth]);
4668a1b9b6aSSam Leffler 	return authenticators[auth];
4671a1e1d21SSam Leffler }
4681a1e1d21SSam Leffler 
4691a1e1d21SSam Leffler void
4708a1b9b6aSSam Leffler ieee80211_authenticator_register(int type,
4718a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth)
4721a1e1d21SSam Leffler {
4738a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
4748a1b9b6aSSam Leffler 		return;
4758a1b9b6aSSam Leffler 	authenticators[type] = auth;
4768a1b9b6aSSam Leffler }
4778a1b9b6aSSam Leffler 
4788a1b9b6aSSam Leffler void
4798a1b9b6aSSam Leffler ieee80211_authenticator_unregister(int type)
4808a1b9b6aSSam Leffler {
4818a1b9b6aSSam Leffler 
4828a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
4838a1b9b6aSSam Leffler 		return;
4848a1b9b6aSSam Leffler 	authenticators[type] = NULL;
4858a1b9b6aSSam Leffler }
4868a1b9b6aSSam Leffler 
4878a1b9b6aSSam Leffler /*
4888a1b9b6aSSam Leffler  * Very simple-minded ACL module support.
4898a1b9b6aSSam Leffler  */
4908a1b9b6aSSam Leffler /* XXX just one for now */
4918a1b9b6aSSam Leffler static	const struct ieee80211_aclator *acl = NULL;
4928a1b9b6aSSam Leffler 
4938a1b9b6aSSam Leffler void
4948a1b9b6aSSam Leffler ieee80211_aclator_register(const struct ieee80211_aclator *iac)
4958a1b9b6aSSam Leffler {
4968a1b9b6aSSam Leffler 	printf("wlan: %s acl policy registered\n", iac->iac_name);
4978a1b9b6aSSam Leffler 	acl = iac;
4988a1b9b6aSSam Leffler }
4998a1b9b6aSSam Leffler 
5008a1b9b6aSSam Leffler void
5018a1b9b6aSSam Leffler ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
5028a1b9b6aSSam Leffler {
5038a1b9b6aSSam Leffler 	if (acl == iac)
5048a1b9b6aSSam Leffler 		acl = NULL;
5058a1b9b6aSSam Leffler 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
5068a1b9b6aSSam Leffler }
5078a1b9b6aSSam Leffler 
5088a1b9b6aSSam Leffler const struct ieee80211_aclator *
5098a1b9b6aSSam Leffler ieee80211_aclator_get(const char *name)
5108a1b9b6aSSam Leffler {
5118a1b9b6aSSam Leffler 	if (acl == NULL)
5128a1b9b6aSSam Leffler 		ieee80211_load_module("wlan_acl");
5138a1b9b6aSSam Leffler 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
5148a1b9b6aSSam Leffler }
5158a1b9b6aSSam Leffler 
5168a1b9b6aSSam Leffler void
51768e8e04eSSam Leffler ieee80211_print_essid(const uint8_t *essid, int len)
5188a1b9b6aSSam Leffler {
51968e8e04eSSam Leffler 	const uint8_t *p;
5201a1e1d21SSam Leffler 	int i;
5211a1e1d21SSam Leffler 
5221a1e1d21SSam Leffler 	if (len > IEEE80211_NWID_LEN)
5231a1e1d21SSam Leffler 		len = IEEE80211_NWID_LEN;
5241a1e1d21SSam Leffler 	/* determine printable or not */
5251a1e1d21SSam Leffler 	for (i = 0, p = essid; i < len; i++, p++) {
5261a1e1d21SSam Leffler 		if (*p < ' ' || *p > 0x7e)
5271a1e1d21SSam Leffler 			break;
5281a1e1d21SSam Leffler 	}
5291a1e1d21SSam Leffler 	if (i == len) {
5301a1e1d21SSam Leffler 		printf("\"");
5311a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
5321a1e1d21SSam Leffler 			printf("%c", *p);
5331a1e1d21SSam Leffler 		printf("\"");
5341a1e1d21SSam Leffler 	} else {
5351a1e1d21SSam Leffler 		printf("0x");
5361a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
5371a1e1d21SSam Leffler 			printf("%02x", *p);
5381a1e1d21SSam Leffler 	}
5391a1e1d21SSam Leffler }
5401a1e1d21SSam Leffler 
5411a1e1d21SSam Leffler void
54268e8e04eSSam Leffler ieee80211_dump_pkt(struct ieee80211com *ic,
54368e8e04eSSam Leffler 	const uint8_t *buf, int len, int rate, int rssi)
5441a1e1d21SSam Leffler {
5458a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh;
5461a1e1d21SSam Leffler 	int i;
5471a1e1d21SSam Leffler 
5488a1b9b6aSSam Leffler 	wh = (const struct ieee80211_frame *)buf;
5491a1e1d21SSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
5501a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
5511a1e1d21SSam Leffler 		printf("NODS %s", ether_sprintf(wh->i_addr2));
5521a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
5531a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr3));
5541a1e1d21SSam Leffler 		break;
5551a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
5561a1e1d21SSam Leffler 		printf("TODS %s", ether_sprintf(wh->i_addr2));
5571a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
5581a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr1));
5591a1e1d21SSam Leffler 		break;
5601a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
5611a1e1d21SSam Leffler 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
5621a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
5631a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr2));
5641a1e1d21SSam Leffler 		break;
5651a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
56668e8e04eSSam Leffler 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
5671a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
5681a1e1d21SSam Leffler 		printf("(%s", ether_sprintf(wh->i_addr2));
5691a1e1d21SSam Leffler 		printf("->%s)", ether_sprintf(wh->i_addr1));
5701a1e1d21SSam Leffler 		break;
5711a1e1d21SSam Leffler 	}
5721a1e1d21SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
5731a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
5741a1e1d21SSam Leffler 		printf(" data");
5751a1e1d21SSam Leffler 		break;
5761a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
5771a1e1d21SSam Leffler 		printf(" %s", ieee80211_mgt_subtype_name[
5781a1e1d21SSam Leffler 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
5791a1e1d21SSam Leffler 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
5801a1e1d21SSam Leffler 		break;
5811a1e1d21SSam Leffler 	default:
5821a1e1d21SSam Leffler 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
5831a1e1d21SSam Leffler 		break;
5841a1e1d21SSam Leffler 	}
58568e8e04eSSam Leffler 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
58668e8e04eSSam Leffler 		const struct ieee80211_qosframe *qwh =
58768e8e04eSSam Leffler 			(const struct ieee80211_qosframe *)buf;
58868e8e04eSSam Leffler 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
58968e8e04eSSam Leffler 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
59068e8e04eSSam Leffler 	}
5915945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
59268e8e04eSSam Leffler 		int off;
59368e8e04eSSam Leffler 
59468e8e04eSSam Leffler 		off = ieee80211_anyhdrspace(ic, wh);
59568e8e04eSSam Leffler 		printf(" WEP [IV %.02x %.02x %.02x",
59668e8e04eSSam Leffler 			buf[off+0], buf[off+1], buf[off+2]);
59768e8e04eSSam Leffler 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
59868e8e04eSSam Leffler 			printf(" %.02x %.02x %.02x",
59968e8e04eSSam Leffler 				buf[off+4], buf[off+5], buf[off+6]);
60068e8e04eSSam Leffler 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
6018a1b9b6aSSam Leffler 	}
6021a1e1d21SSam Leffler 	if (rate >= 0)
6031a1e1d21SSam Leffler 		printf(" %dM", rate / 2);
6041a1e1d21SSam Leffler 	if (rssi >= 0)
6051a1e1d21SSam Leffler 		printf(" +%d", rssi);
6061a1e1d21SSam Leffler 	printf("\n");
6071a1e1d21SSam Leffler 	if (len > 0) {
6081a1e1d21SSam Leffler 		for (i = 0; i < len; i++) {
6091a1e1d21SSam Leffler 			if ((i & 1) == 0)
6101a1e1d21SSam Leffler 				printf(" ");
6111a1e1d21SSam Leffler 			printf("%02x", buf[i]);
6121a1e1d21SSam Leffler 		}
6131a1e1d21SSam Leffler 		printf("\n");
6141a1e1d21SSam Leffler 	}
6151a1e1d21SSam Leffler }
6161a1e1d21SSam Leffler 
61779edaebfSSam Leffler static __inline int
61879edaebfSSam Leffler findrix(const struct ieee80211_rateset *rs, int r)
61979edaebfSSam Leffler {
62079edaebfSSam Leffler 	int i;
62179edaebfSSam Leffler 
62279edaebfSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
62379edaebfSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
62479edaebfSSam Leffler 			return i;
62579edaebfSSam Leffler 	return -1;
62679edaebfSSam Leffler }
62779edaebfSSam Leffler 
6281a1e1d21SSam Leffler int
62970e28b9aSSam Leffler ieee80211_fix_rate(struct ieee80211_node *ni,
63070e28b9aSSam Leffler 	struct ieee80211_rateset *nrs, int flags)
6311a1e1d21SSam Leffler {
632b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6337d77cd53SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
63479edaebfSSam Leffler 	int i, j, rix, error;
635b032f27cSSam Leffler 	int okrate, badrate, fixedrate, ucastrate;
63641b3c790SSam Leffler 	const struct ieee80211_rateset *srs;
63768e8e04eSSam Leffler 	uint8_t r;
6381a1e1d21SSam Leffler 
6391a1e1d21SSam Leffler 	error = 0;
64068e8e04eSSam Leffler 	okrate = badrate = 0;
641b032f27cSSam Leffler 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
642b032f27cSSam Leffler 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
643b032f27cSSam Leffler 		/*
644b032f27cSSam Leffler 		 * Workaround awkwardness with fixed rate.  We are called
645b032f27cSSam Leffler 		 * to check both the legacy rate set and the HT rate set
646b032f27cSSam Leffler 		 * but we must apply any legacy fixed rate check only to the
647b032f27cSSam Leffler 		 * legacy rate set and vice versa.  We cannot tell what type
648b032f27cSSam Leffler 		 * of rate set we've been given (legacy or HT) but we can
649b032f27cSSam Leffler 		 * distinguish the fixed rate type (MCS have 0x80 set).
650b032f27cSSam Leffler 		 * So to deal with this the caller communicates whether to
651b032f27cSSam Leffler 		 * check MCS or legacy rate using the flags and we use the
652b032f27cSSam Leffler 		 * type of any fixed rate to avoid applying an MCS to a
653b032f27cSSam Leffler 		 * legacy rate and vice versa.
654b032f27cSSam Leffler 		 */
655b032f27cSSam Leffler 		if (ucastrate & 0x80) {
656b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFRATE)
657b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFRATE;
658b032f27cSSam Leffler 		} else if ((ucastrate & 0x80) == 0) {
659b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFMCS)
660b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFMCS;
661b032f27cSSam Leffler 		}
662b032f27cSSam Leffler 		/* NB: required to make MCS match below work */
663b032f27cSSam Leffler 		ucastrate &= IEEE80211_RATE_VAL;
664b032f27cSSam Leffler 	}
66568e8e04eSSam Leffler 	fixedrate = IEEE80211_FIXED_RATE_NONE;
666b032f27cSSam Leffler 	/*
667b032f27cSSam Leffler 	 * XXX we are called to process both MCS and legacy rates;
668b032f27cSSam Leffler 	 * we must use the appropriate basic rate set or chaos will
669b032f27cSSam Leffler 	 * ensue; for now callers that want MCS must supply
670b032f27cSSam Leffler 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
671b032f27cSSam Leffler 	 * function so there are two variants, one for MCS and one
672b032f27cSSam Leffler 	 * for legacy rates.
673b032f27cSSam Leffler 	 */
674b032f27cSSam Leffler 	if (flags & IEEE80211_F_DOBRS)
675b032f27cSSam Leffler 		srs = (const struct ieee80211_rateset *)
676b032f27cSSam Leffler 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
677b032f27cSSam Leffler 	else
67841b3c790SSam Leffler 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
679ef39d4beSSam Leffler 	for (i = 0; i < nrs->rs_nrates; ) {
6801a1e1d21SSam Leffler 		if (flags & IEEE80211_F_DOSORT) {
6811a1e1d21SSam Leffler 			/*
6821a1e1d21SSam Leffler 			 * Sort rates.
6831a1e1d21SSam Leffler 			 */
6841a1e1d21SSam Leffler 			for (j = i + 1; j < nrs->rs_nrates; j++) {
6850ebe104fSAdrian Chadd 				if (IEEE80211_RV(nrs->rs_rates[i]) >
6860ebe104fSAdrian Chadd 				    IEEE80211_RV(nrs->rs_rates[j])) {
6871a1e1d21SSam Leffler 					r = nrs->rs_rates[i];
6881a1e1d21SSam Leffler 					nrs->rs_rates[i] = nrs->rs_rates[j];
6891a1e1d21SSam Leffler 					nrs->rs_rates[j] = r;
6901a1e1d21SSam Leffler 				}
6911a1e1d21SSam Leffler 			}
6921a1e1d21SSam Leffler 		}
6931a1e1d21SSam Leffler 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
6941a1e1d21SSam Leffler 		badrate = r;
6951a1e1d21SSam Leffler 		/*
69668e8e04eSSam Leffler 		 * Check for fixed rate.
6971a1e1d21SSam Leffler 		 */
698b032f27cSSam Leffler 		if (r == ucastrate)
6998a1b9b6aSSam Leffler 			fixedrate = r;
7001a1e1d21SSam Leffler 		/*
7011a1e1d21SSam Leffler 		 * Check against supported rates.
7021a1e1d21SSam Leffler 		 */
70379edaebfSSam Leffler 		rix = findrix(srs, r);
70479edaebfSSam Leffler 		if (flags & IEEE80211_F_DONEGO) {
70579edaebfSSam Leffler 			if (rix < 0) {
706ef39d4beSSam Leffler 				/*
707ef39d4beSSam Leffler 				 * A rate in the node's rate set is not
708ef39d4beSSam Leffler 				 * supported.  If this is a basic rate and we
70979edaebfSSam Leffler 				 * are operating as a STA then this is an error.
710ef39d4beSSam Leffler 				 * Otherwise we just discard/ignore the rate.
711ef39d4beSSam Leffler 				 */
71279edaebfSSam Leffler 				if ((flags & IEEE80211_F_JOIN) &&
713ef39d4beSSam Leffler 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
7141a1e1d21SSam Leffler 					error++;
71579edaebfSSam Leffler 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
71679edaebfSSam Leffler 				/*
71779edaebfSSam Leffler 				 * Overwrite with the supported rate
71879edaebfSSam Leffler 				 * value so any basic rate bit is set.
71979edaebfSSam Leffler 				 */
72079edaebfSSam Leffler 				nrs->rs_rates[i] = srs->rs_rates[rix];
7211a1e1d21SSam Leffler 			}
7221a1e1d21SSam Leffler 		}
72379edaebfSSam Leffler 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
7241a1e1d21SSam Leffler 			/*
7251a1e1d21SSam Leffler 			 * Delete unacceptable rates.
7261a1e1d21SSam Leffler 			 */
7271a1e1d21SSam Leffler 			nrs->rs_nrates--;
7281a1e1d21SSam Leffler 			for (j = i; j < nrs->rs_nrates; j++)
7291a1e1d21SSam Leffler 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
7301a1e1d21SSam Leffler 			nrs->rs_rates[j] = 0;
7311a1e1d21SSam Leffler 			continue;
7321a1e1d21SSam Leffler 		}
73379edaebfSSam Leffler 		if (rix >= 0)
7341a1e1d21SSam Leffler 			okrate = nrs->rs_rates[i];
7351a1e1d21SSam Leffler 		i++;
7361a1e1d21SSam Leffler 	}
7378a1b9b6aSSam Leffler 	if (okrate == 0 || error != 0 ||
738b032f27cSSam Leffler 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
739b032f27cSSam Leffler 	     fixedrate != ucastrate)) {
740b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
741b032f27cSSam Leffler 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
742b032f27cSSam Leffler 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
7431a1e1d21SSam Leffler 		return badrate | IEEE80211_RATE_BASIC;
744b032f27cSSam Leffler 	} else
7450ebe104fSAdrian Chadd 		return IEEE80211_RV(okrate);
7461a1e1d21SSam Leffler }
7471a1e1d21SSam Leffler 
7488a1b9b6aSSam Leffler /*
7498a1b9b6aSSam Leffler  * Reset 11g-related state.
7508a1b9b6aSSam Leffler  */
7518a1b9b6aSSam Leffler void
7528a1b9b6aSSam Leffler ieee80211_reset_erp(struct ieee80211com *ic)
7531a1e1d21SSam Leffler {
7548a1b9b6aSSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
7558a1b9b6aSSam Leffler 	ic->ic_nonerpsta = 0;
7568a1b9b6aSSam Leffler 	ic->ic_longslotsta = 0;
7578a1b9b6aSSam Leffler 	/*
7588a1b9b6aSSam Leffler 	 * Short slot time is enabled only when operating in 11g
7598a1b9b6aSSam Leffler 	 * and not in an IBSS.  We must also honor whether or not
7608a1b9b6aSSam Leffler 	 * the driver is capable of doing it.
7618a1b9b6aSSam Leffler 	 */
7628a1b9b6aSSam Leffler 	ieee80211_set_shortslottime(ic,
76368e8e04eSSam Leffler 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
76468e8e04eSSam Leffler 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
76568e8e04eSSam Leffler 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
7668a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
7678a1b9b6aSSam Leffler 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
7688a1b9b6aSSam Leffler 	/*
7698a1b9b6aSSam Leffler 	 * Set short preamble and ERP barker-preamble flags.
7708a1b9b6aSSam Leffler 	 */
77168e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
7728a1b9b6aSSam Leffler 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
7738a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
7748a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
7758a1b9b6aSSam Leffler 	} else {
7768a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
7778a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_USEBARKER;
7788a1b9b6aSSam Leffler 	}
7798a1b9b6aSSam Leffler }
7808a1b9b6aSSam Leffler 
7818a1b9b6aSSam Leffler /*
7828a1b9b6aSSam Leffler  * Set the short slot time state and notify the driver.
7838a1b9b6aSSam Leffler  */
7848a1b9b6aSSam Leffler void
7858a1b9b6aSSam Leffler ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
7868a1b9b6aSSam Leffler {
7878a1b9b6aSSam Leffler 	if (onoff)
7888a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHSLOT;
7898a1b9b6aSSam Leffler 	else
7908a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
7918a1b9b6aSSam Leffler 	/* notify driver */
7928a1b9b6aSSam Leffler 	if (ic->ic_updateslot != NULL)
793272f6adeSGleb Smirnoff 		ic->ic_updateslot(ic);
7948a1b9b6aSSam Leffler }
7958a1b9b6aSSam Leffler 
7968a1b9b6aSSam Leffler /*
7978a1b9b6aSSam Leffler  * Check if the specified rate set supports ERP.
7988a1b9b6aSSam Leffler  * NB: the rate set is assumed to be sorted.
7998a1b9b6aSSam Leffler  */
8008a1b9b6aSSam Leffler int
801b032f27cSSam Leffler ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
8028a1b9b6aSSam Leffler {
8038a1b9b6aSSam Leffler 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
8048a1b9b6aSSam Leffler 	int i, j;
8058a1b9b6aSSam Leffler 
806a3e08d6fSRui Paulo 	if (rs->rs_nrates < nitems(rates))
8078a1b9b6aSSam Leffler 		return 0;
808a3e08d6fSRui Paulo 	for (i = 0; i < nitems(rates); i++) {
8098a1b9b6aSSam Leffler 		for (j = 0; j < rs->rs_nrates; j++) {
8108a1b9b6aSSam Leffler 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
8118a1b9b6aSSam Leffler 			if (rates[i] == r)
8128a1b9b6aSSam Leffler 				goto next;
8138a1b9b6aSSam Leffler 			if (r > rates[i])
8148a1b9b6aSSam Leffler 				return 0;
8158a1b9b6aSSam Leffler 		}
8168a1b9b6aSSam Leffler 		return 0;
8178a1b9b6aSSam Leffler 	next:
8188a1b9b6aSSam Leffler 		;
8198a1b9b6aSSam Leffler 	}
8208a1b9b6aSSam Leffler 	return 1;
8218a1b9b6aSSam Leffler }
8228a1b9b6aSSam Leffler 
8238a1b9b6aSSam Leffler /*
824b032f27cSSam Leffler  * Mark the basic rates for the rate table based on the
8258a1b9b6aSSam Leffler  * operating mode.  For real 11g we mark all the 11b rates
8268a1b9b6aSSam Leffler  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
8278a1b9b6aSSam Leffler  * 11b rates.  There's also a pseudo 11a-mode used to mark only
8288a1b9b6aSSam Leffler  * the basic OFDM rates.
8298a1b9b6aSSam Leffler  */
830b032f27cSSam Leffler static void
831b032f27cSSam Leffler setbasicrates(struct ieee80211_rateset *rs,
832b032f27cSSam Leffler     enum ieee80211_phymode mode, int add)
8338a1b9b6aSSam Leffler {
83468e8e04eSSam Leffler 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
835be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
836be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
837be0df3e7SSam Leffler 					    /* NB: mixed b/g */
838be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
839be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
840be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
841be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
8426a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
8436a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
844be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
845be0df3e7SSam Leffler 					    /* NB: mixed b/g */
846be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
8478a1b9b6aSSam Leffler 	};
8488a1b9b6aSSam Leffler 	int i, j;
8498a1b9b6aSSam Leffler 
8508a1b9b6aSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++) {
851b032f27cSSam Leffler 		if (!add)
8528a1b9b6aSSam Leffler 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
8538a1b9b6aSSam Leffler 		for (j = 0; j < basic[mode].rs_nrates; j++)
8548a1b9b6aSSam Leffler 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
8558a1b9b6aSSam Leffler 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
8568a1b9b6aSSam Leffler 				break;
8578a1b9b6aSSam Leffler 			}
8588a1b9b6aSSam Leffler 	}
8598a1b9b6aSSam Leffler }
8608a1b9b6aSSam Leffler 
8618a1b9b6aSSam Leffler /*
862b032f27cSSam Leffler  * Set the basic rates in a rate set.
863b032f27cSSam Leffler  */
864b032f27cSSam Leffler void
865b032f27cSSam Leffler ieee80211_setbasicrates(struct ieee80211_rateset *rs,
866b032f27cSSam Leffler     enum ieee80211_phymode mode)
867b032f27cSSam Leffler {
868b032f27cSSam Leffler 	setbasicrates(rs, mode, 0);
869b032f27cSSam Leffler }
870b032f27cSSam Leffler 
871b032f27cSSam Leffler /*
872b032f27cSSam Leffler  * Add basic rates to a rate set.
873b032f27cSSam Leffler  */
874b032f27cSSam Leffler void
875b032f27cSSam Leffler ieee80211_addbasicrates(struct ieee80211_rateset *rs,
876b032f27cSSam Leffler     enum ieee80211_phymode mode)
877b032f27cSSam Leffler {
878b032f27cSSam Leffler 	setbasicrates(rs, mode, 1);
879b032f27cSSam Leffler }
880b032f27cSSam Leffler 
881b032f27cSSam Leffler /*
882b032f27cSSam Leffler  * WME protocol support.
883b032f27cSSam Leffler  *
884b032f27cSSam Leffler  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
885b032f27cSSam Leffler  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
886b032f27cSSam Leffler  * Draft 2.0 Test Plan (Appendix D).
887b032f27cSSam Leffler  *
888b032f27cSSam Leffler  * Static/Dynamic Turbo mode settings come from Atheros.
8898a1b9b6aSSam Leffler  */
8908a1b9b6aSSam Leffler typedef struct phyParamType {
89168e8e04eSSam Leffler 	uint8_t		aifsn;
89268e8e04eSSam Leffler 	uint8_t		logcwmin;
89368e8e04eSSam Leffler 	uint8_t		logcwmax;
89468e8e04eSSam Leffler 	uint16_t	txopLimit;
89568e8e04eSSam Leffler 	uint8_t 	acm;
8968a1b9b6aSSam Leffler } paramType;
8978a1b9b6aSSam Leffler 
8988a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
899be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
900be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
901be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
902be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
903be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
904be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
905be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
906be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
9076a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
9086a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
909be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
910be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
9118a1b9b6aSSam Leffler };
9128a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
913be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
914be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
915be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
916be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
917be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
918be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
919be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
920be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
9216a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
9226a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
923be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
924be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
9258a1b9b6aSSam Leffler };
9268a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
927be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
928be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
929be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
930be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
931be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
932be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
933be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
934be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
9356a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
9366a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
937be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
938be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
9398a1b9b6aSSam Leffler };
9408a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
941be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
942be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
943be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
944be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
945be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
946be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
947be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
948be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
9496a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
9506a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
951be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
952be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
9538a1b9b6aSSam Leffler };
9548a1b9b6aSSam Leffler 
9558a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
956be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
957be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
958be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
959be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
960be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
961be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
962be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
963be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
9646a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
9656a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
966be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
967be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
9688a1b9b6aSSam Leffler };
9698a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
970be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
971be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
972be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
973be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
974be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
975be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
976be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
977be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
9786a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
9796a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
980be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
981be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
9828a1b9b6aSSam Leffler };
9838a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
984be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
985be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
986be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
987be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
988be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
989be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
990be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
991be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
9926a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
9936a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
994be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
995be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
9968a1b9b6aSSam Leffler };
9978a1b9b6aSSam Leffler 
998b032f27cSSam Leffler static void
99967ce310aSSam Leffler _setifsparams(struct wmeParams *wmep, const paramType *phy)
100067ce310aSSam Leffler {
100167ce310aSSam Leffler 	wmep->wmep_aifsn = phy->aifsn;
100267ce310aSSam Leffler 	wmep->wmep_logcwmin = phy->logcwmin;
100367ce310aSSam Leffler 	wmep->wmep_logcwmax = phy->logcwmax;
100467ce310aSSam Leffler 	wmep->wmep_txopLimit = phy->txopLimit;
100567ce310aSSam Leffler }
100667ce310aSSam Leffler 
100767ce310aSSam Leffler static void
100867ce310aSSam Leffler setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
100967ce310aSSam Leffler 	struct wmeParams *wmep, const paramType *phy)
101067ce310aSSam Leffler {
101167ce310aSSam Leffler 	wmep->wmep_acm = phy->acm;
101267ce310aSSam Leffler 	_setifsparams(wmep, phy);
101367ce310aSSam Leffler 
101467ce310aSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
101567ce310aSSam Leffler 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
101667ce310aSSam Leffler 	    ieee80211_wme_acnames[ac], type,
101767ce310aSSam Leffler 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
101867ce310aSSam Leffler 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
101967ce310aSSam Leffler }
102067ce310aSSam Leffler 
102167ce310aSSam Leffler static void
1022b032f27cSSam Leffler ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
10238a1b9b6aSSam Leffler {
1024b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
10258a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
10268a1b9b6aSSam Leffler 	const paramType *pPhyParam, *pBssPhyParam;
10278a1b9b6aSSam Leffler 	struct wmeParams *wmep;
102868e8e04eSSam Leffler 	enum ieee80211_phymode mode;
10298a1b9b6aSSam Leffler 	int i;
10308a1b9b6aSSam Leffler 
1031b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1032b032f27cSSam Leffler 
1033a4b3c7a5SSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
10348a1b9b6aSSam Leffler 		return;
10358a1b9b6aSSam Leffler 
103668e8e04eSSam Leffler 	/*
10370d4e4e5eSAdrian Chadd 	 * Clear the wme cap_info field so a qoscount from a previous
10380d4e4e5eSAdrian Chadd 	 * vap doesn't confuse later code which only parses the beacon
10390d4e4e5eSAdrian Chadd 	 * field and updates hardware when said field changes.
10400d4e4e5eSAdrian Chadd 	 * Otherwise the hardware is programmed with defaults, not what
10410d4e4e5eSAdrian Chadd 	 * the beacon actually announces.
10420d4e4e5eSAdrian Chadd 	 */
10430d4e4e5eSAdrian Chadd 	wme->wme_wmeChanParams.cap_info = 0;
10440d4e4e5eSAdrian Chadd 
10450d4e4e5eSAdrian Chadd 	/*
104668e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
104768e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
104868e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
104968e8e04eSSam Leffler 	 * so state will eventually get set correctly
105068e8e04eSSam Leffler 	 */
105168e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
105268e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
105368e8e04eSSam Leffler 	else
105468e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
10558a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
10568a1b9b6aSSam Leffler 		switch (i) {
10578a1b9b6aSSam Leffler 		case WME_AC_BK:
105868e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BK[mode];
105968e8e04eSSam Leffler 			pBssPhyParam = &phyParamForAC_BK[mode];
10608a1b9b6aSSam Leffler 			break;
10618a1b9b6aSSam Leffler 		case WME_AC_VI:
106268e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VI[mode];
106368e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
10648a1b9b6aSSam Leffler 			break;
10658a1b9b6aSSam Leffler 		case WME_AC_VO:
106668e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VO[mode];
106768e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
10688a1b9b6aSSam Leffler 			break;
10698a1b9b6aSSam Leffler 		case WME_AC_BE:
10708a1b9b6aSSam Leffler 		default:
107168e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BE[mode];
107268e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
10738a1b9b6aSSam Leffler 			break;
10748a1b9b6aSSam Leffler 		}
10758a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
10768a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
107767ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
10788a1b9b6aSSam Leffler 		} else {
107967ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
10808a1b9b6aSSam Leffler 		}
10818a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
108267ce310aSSam Leffler 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
10838a1b9b6aSSam Leffler 	}
10848a1b9b6aSSam Leffler 	/* NB: check ic_bss to avoid NULL deref on initial attach */
1085b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
10868a1b9b6aSSam Leffler 		/*
10878a1b9b6aSSam Leffler 		 * Calculate agressive mode switching threshold based
10888a1b9b6aSSam Leffler 		 * on beacon interval.  This doesn't need locking since
10898a1b9b6aSSam Leffler 		 * we're only called before entering the RUN state at
10908a1b9b6aSSam Leffler 		 * which point we start sending beacon frames.
10918a1b9b6aSSam Leffler 		 */
10928a1b9b6aSSam Leffler 		wme->wme_hipri_switch_thresh =
1093b032f27cSSam Leffler 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1094a4b3c7a5SSam Leffler 		wme->wme_flags &= ~WME_F_AGGRMODE;
1095b032f27cSSam Leffler 		ieee80211_wme_updateparams(vap);
10968a1b9b6aSSam Leffler 	}
10978a1b9b6aSSam Leffler }
10988a1b9b6aSSam Leffler 
1099b032f27cSSam Leffler void
1100b032f27cSSam Leffler ieee80211_wme_initparams(struct ieee80211vap *vap)
1101b032f27cSSam Leffler {
1102b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1103b032f27cSSam Leffler 
1104b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1105b032f27cSSam Leffler 	ieee80211_wme_initparams_locked(vap);
1106b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1107b032f27cSSam Leffler }
1108b032f27cSSam Leffler 
11098a1b9b6aSSam Leffler /*
11108a1b9b6aSSam Leffler  * Update WME parameters for ourself and the BSS.
11118a1b9b6aSSam Leffler  */
11128a1b9b6aSSam Leffler void
1113b032f27cSSam Leffler ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
11148a1b9b6aSSam Leffler {
111567ce310aSSam Leffler 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1116be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
1117be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
1118be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
1119be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
1120be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
1121be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
1122be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
1123be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
11246a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
11256a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
1126be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
1127be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
11288a1b9b6aSSam Leffler 	};
1129b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
11308a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
11318a1b9b6aSSam Leffler 	const struct wmeParams *wmep;
11328a1b9b6aSSam Leffler 	struct wmeParams *chanp, *bssp;
113368e8e04eSSam Leffler 	enum ieee80211_phymode mode;
11348a1b9b6aSSam Leffler 	int i;
1135a48a8ad7SAdrian Chadd 	int do_aggrmode = 0;
11368a1b9b6aSSam Leffler 
113767ce310aSSam Leffler        	/*
113867ce310aSSam Leffler 	 * Set up the channel access parameters for the physical
113967ce310aSSam Leffler 	 * device.  First populate the configured settings.
114067ce310aSSam Leffler 	 */
11418a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
11428a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
11438a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
11448a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
11458a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
11468a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
11478a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
11488a1b9b6aSSam Leffler 
11498a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
11508a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
11518a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
11528a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
11538a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
11548a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
11558a1b9b6aSSam Leffler 	}
11568a1b9b6aSSam Leffler 
11578a1b9b6aSSam Leffler 	/*
115868e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
115968e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
116068e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
116168e8e04eSSam Leffler 	 * so state will eventually get set correctly
116268e8e04eSSam Leffler 	 */
116368e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
116468e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
116568e8e04eSSam Leffler 	else
116668e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
116768e8e04eSSam Leffler 
116868e8e04eSSam Leffler 	/*
11698a1b9b6aSSam Leffler 	 * This implements agressive mode as found in certain
11708a1b9b6aSSam Leffler 	 * vendors' AP's.  When there is significant high
11718a1b9b6aSSam Leffler 	 * priority (VI/VO) traffic in the BSS throttle back BE
11728a1b9b6aSSam Leffler 	 * traffic by using conservative parameters.  Otherwise
11738a1b9b6aSSam Leffler 	 * BE uses agressive params to optimize performance of
11748a1b9b6aSSam Leffler 	 * legacy/non-QoS traffic.
11758a1b9b6aSSam Leffler 	 */
1176a48a8ad7SAdrian Chadd 
1177a48a8ad7SAdrian Chadd 	/* Hostap? Only if aggressive mode is enabled */
1178a48a8ad7SAdrian Chadd         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1179a48a8ad7SAdrian Chadd 	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
1180a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1181a48a8ad7SAdrian Chadd 
1182a48a8ad7SAdrian Chadd 	/*
1183a48a8ad7SAdrian Chadd 	 * Station? Only if we're in a non-QoS BSS.
1184a48a8ad7SAdrian Chadd 	 */
1185a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_STA &&
1186a48a8ad7SAdrian Chadd 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1187a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1188a48a8ad7SAdrian Chadd 
1189a48a8ad7SAdrian Chadd 	/*
1190a48a8ad7SAdrian Chadd 	 * IBSS? Only if we we have WME enabled.
1191a48a8ad7SAdrian Chadd 	 */
1192a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1193a48a8ad7SAdrian Chadd 	    (vap->iv_flags & IEEE80211_F_WME))
1194a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1195a48a8ad7SAdrian Chadd 
1196a48a8ad7SAdrian Chadd 	/*
1197a48a8ad7SAdrian Chadd 	 * If WME is disabled on this VAP, default to aggressive mode
1198a48a8ad7SAdrian Chadd 	 * regardless of the configuration.
1199a48a8ad7SAdrian Chadd 	 */
1200a48a8ad7SAdrian Chadd 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1201a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1202a48a8ad7SAdrian Chadd 
1203a48a8ad7SAdrian Chadd 	/* XXX WDS? */
1204a48a8ad7SAdrian Chadd 
1205a48a8ad7SAdrian Chadd 	/* XXX MBSS? */
1206a48a8ad7SAdrian Chadd 
1207a48a8ad7SAdrian Chadd 	if (do_aggrmode) {
12088a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
12098a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
12108a1b9b6aSSam Leffler 
121167ce310aSSam Leffler 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
12128a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
121367ce310aSSam Leffler 		    aggrParam[mode].logcwmin;
12148a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
121567ce310aSSam Leffler 		    aggrParam[mode].logcwmax;
12168a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1217b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_BURST) ?
121867ce310aSSam Leffler 			aggrParam[mode].txopLimit : 0;
1219b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
122067ce310aSSam Leffler 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
122167ce310aSSam Leffler 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
122267ce310aSSam Leffler 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
122367ce310aSSam Leffler 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
12248a1b9b6aSSam Leffler 	}
12258a1b9b6aSSam Leffler 
1226a48a8ad7SAdrian Chadd 
1227a48a8ad7SAdrian Chadd 	/*
1228a48a8ad7SAdrian Chadd 	 * Change the contention window based on the number of associated
1229a48a8ad7SAdrian Chadd 	 * stations.  If the number of associated stations is 1 and
1230a48a8ad7SAdrian Chadd 	 * aggressive mode is enabled, lower the contention window even
1231a48a8ad7SAdrian Chadd 	 * further.
1232a48a8ad7SAdrian Chadd 	 */
1233b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1234ad262427SSam Leffler 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
123568e8e04eSSam Leffler 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1236be0df3e7SSam Leffler 		    [IEEE80211_MODE_AUTO]	= 3,
1237be0df3e7SSam Leffler 		    [IEEE80211_MODE_11A]	= 3,
1238be0df3e7SSam Leffler 		    [IEEE80211_MODE_11B]	= 4,
1239be0df3e7SSam Leffler 		    [IEEE80211_MODE_11G]	= 3,
1240be0df3e7SSam Leffler 		    [IEEE80211_MODE_FH]		= 4,
1241be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_A]	= 3,
1242be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_G]	= 3,
1243be0df3e7SSam Leffler 		    [IEEE80211_MODE_STURBO_A]	= 3,
12446a76ae21SSam Leffler 		    [IEEE80211_MODE_HALF]	= 3,
12456a76ae21SSam Leffler 		    [IEEE80211_MODE_QUARTER]	= 3,
1246be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NA]	= 3,
1247be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NG]	= 3,
12488a1b9b6aSSam Leffler 		};
12498a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
12508a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
12518a1b9b6aSSam Leffler 
125268e8e04eSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1253b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
125467ce310aSSam Leffler 		    "update %s (chan+bss) logcwmin %u\n",
125567ce310aSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
12568a1b9b6aSSam Leffler 	}
1257a48a8ad7SAdrian Chadd 
1258a48a8ad7SAdrian Chadd 	/*
1259a48a8ad7SAdrian Chadd 	 * Arrange for the beacon update.
1260a48a8ad7SAdrian Chadd 	 *
1261a48a8ad7SAdrian Chadd 	 * XXX what about MBSS, WDS?
1262a48a8ad7SAdrian Chadd 	 */
1263a48a8ad7SAdrian Chadd 	if (vap->iv_opmode == IEEE80211_M_HOSTAP
1264a48a8ad7SAdrian Chadd 	    || vap->iv_opmode == IEEE80211_M_IBSS) {
12658a1b9b6aSSam Leffler 		/*
12668a1b9b6aSSam Leffler 		 * Arrange for a beacon update and bump the parameter
12678a1b9b6aSSam Leffler 		 * set number so associated stations load the new values.
12688a1b9b6aSSam Leffler 		 */
12698a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info =
12708a1b9b6aSSam Leffler 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1271b032f27cSSam Leffler 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
12728a1b9b6aSSam Leffler 	}
12738a1b9b6aSSam Leffler 
1274dd2fb488SAdrian Chadd 	/* schedule the deferred WME update */
1275dd2fb488SAdrian Chadd 	ieee80211_runtask(ic, &ic->ic_wme_task);
12768a1b9b6aSSam Leffler 
1277b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
12788a1b9b6aSSam Leffler 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1279b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA ?
12808a1b9b6aSSam Leffler 		wme->wme_wmeChanParams.cap_info :
12818a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info);
12828a1b9b6aSSam Leffler }
12838a1b9b6aSSam Leffler 
12848a1b9b6aSSam Leffler void
1285b032f27cSSam Leffler ieee80211_wme_updateparams(struct ieee80211vap *vap)
12868a1b9b6aSSam Leffler {
1287b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
12888a1b9b6aSSam Leffler 
12898a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_WME) {
1290b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
1291b032f27cSSam Leffler 		ieee80211_wme_updateparams_locked(vap);
1292b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
12938a1b9b6aSSam Leffler 	}
12948a1b9b6aSSam Leffler }
12958a1b9b6aSSam Leffler 
1296b032f27cSSam Leffler static void
1297b032f27cSSam Leffler parent_updown(void *arg, int npending)
129868e8e04eSSam Leffler {
12997a79cebfSGleb Smirnoff 	struct ieee80211com *ic = arg;
130068e8e04eSSam Leffler 
13017a79cebfSGleb Smirnoff 	ic->ic_parent(ic);
1302b032f27cSSam Leffler }
130368e8e04eSSam Leffler 
13045efea30fSAndrew Thompson static void
13055efea30fSAndrew Thompson update_mcast(void *arg, int npending)
13065efea30fSAndrew Thompson {
13075efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
13085efea30fSAndrew Thompson 
1309272f6adeSGleb Smirnoff 	ic->ic_update_mcast(ic);
13105efea30fSAndrew Thompson }
13115efea30fSAndrew Thompson 
13125efea30fSAndrew Thompson static void
13135efea30fSAndrew Thompson update_promisc(void *arg, int npending)
13145efea30fSAndrew Thompson {
13155efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
13165efea30fSAndrew Thompson 
1317272f6adeSGleb Smirnoff 	ic->ic_update_promisc(ic);
13185efea30fSAndrew Thompson }
13195efea30fSAndrew Thompson 
13205efea30fSAndrew Thompson static void
13215efea30fSAndrew Thompson update_channel(void *arg, int npending)
13225efea30fSAndrew Thompson {
13235efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
13245efea30fSAndrew Thompson 
13255efea30fSAndrew Thompson 	ic->ic_set_channel(ic);
13265463c4a4SSam Leffler 	ieee80211_radiotap_chan_change(ic);
13275efea30fSAndrew Thompson }
13285efea30fSAndrew Thompson 
1329b94299c4SAdrian Chadd static void
1330b94299c4SAdrian Chadd update_chw(void *arg, int npending)
1331b94299c4SAdrian Chadd {
1332b94299c4SAdrian Chadd 	struct ieee80211com *ic = arg;
1333b94299c4SAdrian Chadd 
1334b94299c4SAdrian Chadd 	/*
1335b94299c4SAdrian Chadd 	 * XXX should we defer the channel width _config_ update until now?
1336b94299c4SAdrian Chadd 	 */
1337b94299c4SAdrian Chadd 	ic->ic_update_chw(ic);
1338b94299c4SAdrian Chadd }
1339b94299c4SAdrian Chadd 
1340dd2fb488SAdrian Chadd static void
1341dd2fb488SAdrian Chadd update_wme(void *arg, int npending)
1342dd2fb488SAdrian Chadd {
1343dd2fb488SAdrian Chadd 	struct ieee80211com *ic = arg;
1344dd2fb488SAdrian Chadd 
1345dd2fb488SAdrian Chadd 	/*
1346dd2fb488SAdrian Chadd 	 * XXX should we defer the WME configuration update until now?
1347dd2fb488SAdrian Chadd 	 */
1348dd2fb488SAdrian Chadd 	ic->ic_wme.wme_update(ic);
1349dd2fb488SAdrian Chadd }
1350dd2fb488SAdrian Chadd 
13514061c639SAndriy Voskoboinyk static void
13524061c639SAndriy Voskoboinyk restart_vaps(void *arg, int npending)
13534061c639SAndriy Voskoboinyk {
13544061c639SAndriy Voskoboinyk 	struct ieee80211com *ic = arg;
13554061c639SAndriy Voskoboinyk 
13564061c639SAndriy Voskoboinyk 	ieee80211_suspend_all(ic);
13574061c639SAndriy Voskoboinyk 	ieee80211_resume_all(ic);
13584061c639SAndriy Voskoboinyk }
13594061c639SAndriy Voskoboinyk 
136068e8e04eSSam Leffler /*
1361ae55932eSAndrew Thompson  * Block until the parent is in a known state.  This is
1362ae55932eSAndrew Thompson  * used after any operations that dispatch a task (e.g.
1363ae55932eSAndrew Thompson  * to auto-configure the parent device up/down).
1364ae55932eSAndrew Thompson  */
1365ae55932eSAndrew Thompson void
1366ae55932eSAndrew Thompson ieee80211_waitfor_parent(struct ieee80211com *ic)
1367ae55932eSAndrew Thompson {
13685efea30fSAndrew Thompson 	taskqueue_block(ic->ic_tq);
13695efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_parent_task);
13705efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_mcast_task);
13715efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_promisc_task);
13725efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_chan_task);
13735efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1374b94299c4SAdrian Chadd 	ieee80211_draintask(ic, &ic->ic_chw_task);
1375dd2fb488SAdrian Chadd 	ieee80211_draintask(ic, &ic->ic_wme_task);
13765efea30fSAndrew Thompson 	taskqueue_unblock(ic->ic_tq);
1377ae55932eSAndrew Thompson }
1378ae55932eSAndrew Thompson 
1379ae55932eSAndrew Thompson /*
138024034ddbSAdrian Chadd  * Check to see whether the current channel needs reset.
138124034ddbSAdrian Chadd  *
138224034ddbSAdrian Chadd  * Some devices don't handle being given an invalid channel
138324034ddbSAdrian Chadd  * in their operating mode very well (eg wpi(4) will throw a
138424034ddbSAdrian Chadd  * firmware exception.)
138524034ddbSAdrian Chadd  *
138624034ddbSAdrian Chadd  * Return 0 if we're ok, 1 if the channel needs to be reset.
138724034ddbSAdrian Chadd  *
138824034ddbSAdrian Chadd  * See PR kern/202502.
138924034ddbSAdrian Chadd  */
139024034ddbSAdrian Chadd static int
139124034ddbSAdrian Chadd ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
139224034ddbSAdrian Chadd {
139324034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
139424034ddbSAdrian Chadd 
139524034ddbSAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_IBSS &&
139624034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
139724034ddbSAdrian Chadd 	    (vap->iv_opmode == IEEE80211_M_HOSTAP &&
139824034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
139924034ddbSAdrian Chadd 		return (1);
140024034ddbSAdrian Chadd 	return (0);
140124034ddbSAdrian Chadd }
140224034ddbSAdrian Chadd 
140324034ddbSAdrian Chadd /*
140424034ddbSAdrian Chadd  * Reset the curchan to a known good state.
140524034ddbSAdrian Chadd  */
140624034ddbSAdrian Chadd static void
140724034ddbSAdrian Chadd ieee80211_start_reset_chan(struct ieee80211vap *vap)
140824034ddbSAdrian Chadd {
140924034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
141024034ddbSAdrian Chadd 
141124034ddbSAdrian Chadd 	ic->ic_curchan = &ic->ic_channels[0];
141224034ddbSAdrian Chadd }
141324034ddbSAdrian Chadd 
141424034ddbSAdrian Chadd /*
1415b032f27cSSam Leffler  * Start a vap running.  If this is the first vap to be
1416b032f27cSSam Leffler  * set running on the underlying device then we
1417b032f27cSSam Leffler  * automatically bring the device up.
141868e8e04eSSam Leffler  */
1419b032f27cSSam Leffler void
1420b032f27cSSam Leffler ieee80211_start_locked(struct ieee80211vap *vap)
1421b032f27cSSam Leffler {
1422b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1423b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1424b032f27cSSam Leffler 
1425b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1426b032f27cSSam Leffler 
1427b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap,
1428b032f27cSSam Leffler 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1429b032f27cSSam Leffler 		"start running, %d vaps running\n", ic->ic_nrunning);
1430b032f27cSSam Leffler 
1431b032f27cSSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1432b032f27cSSam Leffler 		/*
1433b032f27cSSam Leffler 		 * Mark us running.  Note that it's ok to do this first;
1434b032f27cSSam Leffler 		 * if we need to bring the parent device up we defer that
1435b032f27cSSam Leffler 		 * to avoid dropping the com lock.  We expect the device
1436b032f27cSSam Leffler 		 * to respond to being marked up by calling back into us
1437b032f27cSSam Leffler 		 * through ieee80211_start_all at which point we'll come
1438b032f27cSSam Leffler 		 * back in here and complete the work.
1439b032f27cSSam Leffler 		 */
1440b032f27cSSam Leffler 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1441b032f27cSSam Leffler 		/*
1442b032f27cSSam Leffler 		 * We are not running; if this we are the first vap
1443b032f27cSSam Leffler 		 * to be brought up auto-up the parent if necessary.
1444b032f27cSSam Leffler 		 */
14457a79cebfSGleb Smirnoff 		if (ic->ic_nrunning++ == 0) {
144624034ddbSAdrian Chadd 
144724034ddbSAdrian Chadd 			/* reset the channel to a known good channel */
144824034ddbSAdrian Chadd 			if (ieee80211_start_check_reset_chan(vap))
144924034ddbSAdrian Chadd 				ieee80211_start_reset_chan(vap);
145024034ddbSAdrian Chadd 
1451b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1452b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
14537a79cebfSGleb Smirnoff 			    "%s: up parent %s\n", __func__, ic->ic_name);
14545efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1455b032f27cSSam Leffler 			return;
1456b032f27cSSam Leffler 		}
1457b032f27cSSam Leffler 	}
1458b032f27cSSam Leffler 	/*
1459b032f27cSSam Leffler 	 * If the parent is up and running, then kick the
1460b032f27cSSam Leffler 	 * 802.11 state machine as appropriate.
1461b032f27cSSam Leffler 	 */
14627a79cebfSGleb Smirnoff 	if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1463b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1464b032f27cSSam Leffler #if 0
1465b032f27cSSam Leffler 			/* XXX bypasses scan too easily; disable for now */
1466b032f27cSSam Leffler 			/*
1467b032f27cSSam Leffler 			 * Try to be intelligent about clocking the state
1468b032f27cSSam Leffler 			 * machine.  If we're currently in RUN state then
1469b032f27cSSam Leffler 			 * we should be able to apply any new state/parameters
1470b032f27cSSam Leffler 			 * simply by re-associating.  Otherwise we need to
1471b032f27cSSam Leffler 			 * re-scan to select an appropriate ap.
1472b032f27cSSam Leffler 			 */
1473b032f27cSSam Leffler 			if (vap->iv_state >= IEEE80211_S_RUN)
1474b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1475b032f27cSSam Leffler 				    IEEE80211_S_ASSOC, 1);
1476b032f27cSSam Leffler 			else
1477b032f27cSSam Leffler #endif
1478b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1479b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
148068e8e04eSSam Leffler 		} else {
148168e8e04eSSam Leffler 			/*
1482b032f27cSSam Leffler 			 * For monitor+wds mode there's nothing to do but
1483b032f27cSSam Leffler 			 * start running.  Otherwise if this is the first
148468e8e04eSSam Leffler 			 * vap to be brought up, start a scan which may be
148568e8e04eSSam Leffler 			 * preempted if the station is locked to a particular
148668e8e04eSSam Leffler 			 * channel.
148768e8e04eSSam Leffler 			 */
14885efea30fSAndrew Thompson 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1489b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1490b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_WDS)
1491b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1492b032f27cSSam Leffler 				    IEEE80211_S_RUN, -1);
1493b032f27cSSam Leffler 			else
1494b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1495b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
149668e8e04eSSam Leffler 		}
149768e8e04eSSam Leffler 	}
1498b032f27cSSam Leffler }
1499b032f27cSSam Leffler 
1500b032f27cSSam Leffler /*
1501b032f27cSSam Leffler  * Start a single vap.
1502b032f27cSSam Leffler  */
1503b032f27cSSam Leffler void
1504b032f27cSSam Leffler ieee80211_init(void *arg)
1505b032f27cSSam Leffler {
1506b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1507b032f27cSSam Leffler 
150835f434b2SSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1509b032f27cSSam Leffler 	    "%s\n", __func__);
1510b032f27cSSam Leffler 
1511b032f27cSSam Leffler 	IEEE80211_LOCK(vap->iv_ic);
1512b032f27cSSam Leffler 	ieee80211_start_locked(vap);
1513b032f27cSSam Leffler 	IEEE80211_UNLOCK(vap->iv_ic);
1514b032f27cSSam Leffler }
1515b032f27cSSam Leffler 
1516b032f27cSSam Leffler /*
1517b032f27cSSam Leffler  * Start all runnable vap's on a device.
1518b032f27cSSam Leffler  */
1519b032f27cSSam Leffler void
1520b032f27cSSam Leffler ieee80211_start_all(struct ieee80211com *ic)
1521b032f27cSSam Leffler {
1522b032f27cSSam Leffler 	struct ieee80211vap *vap;
1523b032f27cSSam Leffler 
1524b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1525b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1526b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1527b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1528b032f27cSSam Leffler 			ieee80211_start_locked(vap);
1529b032f27cSSam Leffler 	}
1530b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1531b032f27cSSam Leffler }
1532b032f27cSSam Leffler 
1533b032f27cSSam Leffler /*
1534b032f27cSSam Leffler  * Stop a vap.  We force it down using the state machine
1535b032f27cSSam Leffler  * then mark it's ifnet not running.  If this is the last
1536b032f27cSSam Leffler  * vap running on the underlying device then we close it
1537b032f27cSSam Leffler  * too to insure it will be properly initialized when the
1538b032f27cSSam Leffler  * next vap is brought up.
1539b032f27cSSam Leffler  */
1540b032f27cSSam Leffler void
1541b032f27cSSam Leffler ieee80211_stop_locked(struct ieee80211vap *vap)
1542b032f27cSSam Leffler {
1543b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1544b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1545b032f27cSSam Leffler 
1546b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1547b032f27cSSam Leffler 
1548b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1549b032f27cSSam Leffler 	    "stop running, %d vaps running\n", ic->ic_nrunning);
1550b032f27cSSam Leffler 
1551b032f27cSSam Leffler 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1552b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1553b032f27cSSam Leffler 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
15547a79cebfSGleb Smirnoff 		if (--ic->ic_nrunning == 0) {
1555b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1556b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
15577a79cebfSGleb Smirnoff 			    "down parent %s\n", ic->ic_name);
15585efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1559b032f27cSSam Leffler 		}
1560b032f27cSSam Leffler 	}
1561b032f27cSSam Leffler }
1562b032f27cSSam Leffler 
1563b032f27cSSam Leffler void
1564b032f27cSSam Leffler ieee80211_stop(struct ieee80211vap *vap)
1565b032f27cSSam Leffler {
1566b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1567b032f27cSSam Leffler 
1568b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1569b032f27cSSam Leffler 	ieee80211_stop_locked(vap);
1570b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1571b032f27cSSam Leffler }
1572b032f27cSSam Leffler 
1573b032f27cSSam Leffler /*
1574b032f27cSSam Leffler  * Stop all vap's running on a device.
1575b032f27cSSam Leffler  */
1576b032f27cSSam Leffler void
1577b032f27cSSam Leffler ieee80211_stop_all(struct ieee80211com *ic)
1578b032f27cSSam Leffler {
1579b032f27cSSam Leffler 	struct ieee80211vap *vap;
1580b032f27cSSam Leffler 
1581b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1582b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1583b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1584b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1585b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
1586b032f27cSSam Leffler 	}
1587b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1588ae55932eSAndrew Thompson 
1589ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
159068e8e04eSSam Leffler }
159168e8e04eSSam Leffler 
159268e8e04eSSam Leffler /*
15936076cbacSSam Leffler  * Stop all vap's running on a device and arrange
15946076cbacSSam Leffler  * for those that were running to be resumed.
15956076cbacSSam Leffler  */
15966076cbacSSam Leffler void
15976076cbacSSam Leffler ieee80211_suspend_all(struct ieee80211com *ic)
15986076cbacSSam Leffler {
15996076cbacSSam Leffler 	struct ieee80211vap *vap;
16006076cbacSSam Leffler 
16016076cbacSSam Leffler 	IEEE80211_LOCK(ic);
16026076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
16036076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
16046076cbacSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
16056076cbacSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
16066076cbacSSam Leffler 			ieee80211_stop_locked(vap);
16076076cbacSSam Leffler 		}
16086076cbacSSam Leffler 	}
16096076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
1610ae55932eSAndrew Thompson 
1611ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
16126076cbacSSam Leffler }
16136076cbacSSam Leffler 
16146076cbacSSam Leffler /*
16156076cbacSSam Leffler  * Start all vap's marked for resume.
16166076cbacSSam Leffler  */
16176076cbacSSam Leffler void
16186076cbacSSam Leffler ieee80211_resume_all(struct ieee80211com *ic)
16196076cbacSSam Leffler {
16206076cbacSSam Leffler 	struct ieee80211vap *vap;
16216076cbacSSam Leffler 
16226076cbacSSam Leffler 	IEEE80211_LOCK(ic);
16236076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
16246076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
16256076cbacSSam Leffler 		if (!IFNET_IS_UP_RUNNING(ifp) &&
16266076cbacSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
16276076cbacSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
16286076cbacSSam Leffler 			ieee80211_start_locked(vap);
16296076cbacSSam Leffler 		}
16306076cbacSSam Leffler 	}
16316076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
16326076cbacSSam Leffler }
16336076cbacSSam Leffler 
16344061c639SAndriy Voskoboinyk /*
16354061c639SAndriy Voskoboinyk  * Restart all vap's running on a device.
16364061c639SAndriy Voskoboinyk  */
16374061c639SAndriy Voskoboinyk void
16384061c639SAndriy Voskoboinyk ieee80211_restart_all(struct ieee80211com *ic)
16394061c639SAndriy Voskoboinyk {
16404061c639SAndriy Voskoboinyk 	/*
16414061c639SAndriy Voskoboinyk 	 * NB: do not use ieee80211_runtask here, we will
16424061c639SAndriy Voskoboinyk 	 * block & drain net80211 taskqueue.
16434061c639SAndriy Voskoboinyk 	 */
16444061c639SAndriy Voskoboinyk 	taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
16454061c639SAndriy Voskoboinyk }
16464061c639SAndriy Voskoboinyk 
1647e701e041SSam Leffler void
1648e701e041SSam Leffler ieee80211_beacon_miss(struct ieee80211com *ic)
1649e701e041SSam Leffler {
16505efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
16515efea30fSAndrew Thompson 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
16525efea30fSAndrew Thompson 		/* Process in a taskq, the handler may reenter the driver */
16535efea30fSAndrew Thompson 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
16545efea30fSAndrew Thompson 	}
16555efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
16565efea30fSAndrew Thompson }
16575efea30fSAndrew Thompson 
16585efea30fSAndrew Thompson static void
16595efea30fSAndrew Thompson beacon_miss(void *arg, int npending)
16605efea30fSAndrew Thompson {
16615efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
1662b032f27cSSam Leffler 	struct ieee80211vap *vap;
1663e701e041SSam Leffler 
166423401900SAdrian Chadd 	IEEE80211_LOCK(ic);
1665b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1666e701e041SSam Leffler 		/*
1667d8c364fbSAndriy Voskoboinyk 		 * We only pass events through for sta vap's in RUN+ state;
1668b032f27cSSam Leffler 		 * may be too restrictive but for now this saves all the
1669b032f27cSSam Leffler 		 * handlers duplicating these checks.
1670e701e041SSam Leffler 		 */
1671b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
1672c70761e6SSam Leffler 		    vap->iv_state >= IEEE80211_S_RUN &&
1673b032f27cSSam Leffler 		    vap->iv_bmiss != NULL)
1674b032f27cSSam Leffler 			vap->iv_bmiss(vap);
1675e701e041SSam Leffler 	}
167623401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
167768e8e04eSSam Leffler }
1678e701e041SSam Leffler 
16795efea30fSAndrew Thompson static void
16805efea30fSAndrew Thompson beacon_swmiss(void *arg, int npending)
16815efea30fSAndrew Thompson {
16825efea30fSAndrew Thompson 	struct ieee80211vap *vap = arg;
168323401900SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
16845efea30fSAndrew Thompson 
168523401900SAdrian Chadd 	IEEE80211_LOCK(ic);
1686d8c364fbSAndriy Voskoboinyk 	if (vap->iv_state >= IEEE80211_S_RUN) {
16875efea30fSAndrew Thompson 		/* XXX Call multiple times if npending > zero? */
16885efea30fSAndrew Thompson 		vap->iv_bmiss(vap);
16895efea30fSAndrew Thompson 	}
169023401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
169123401900SAdrian Chadd }
16925efea30fSAndrew Thompson 
1693e99662a6SSam Leffler /*
1694e99662a6SSam Leffler  * Software beacon miss handling.  Check if any beacons
1695e99662a6SSam Leffler  * were received in the last period.  If not post a
1696e99662a6SSam Leffler  * beacon miss; otherwise reset the counter.
1697e99662a6SSam Leffler  */
1698b032f27cSSam Leffler void
1699e99662a6SSam Leffler ieee80211_swbmiss(void *arg)
1700e99662a6SSam Leffler {
1701b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1702c448998dSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1703e99662a6SSam Leffler 
170423401900SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
170523401900SAdrian Chadd 
1706d8c364fbSAndriy Voskoboinyk 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1707c448998dSSam Leffler 	    ("wrong state %d", vap->iv_state));
1708c448998dSSam Leffler 
1709c448998dSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN) {
1710c448998dSSam Leffler 		/*
1711c448998dSSam Leffler 		 * If scanning just ignore and reset state.  If we get a
1712c448998dSSam Leffler 		 * bmiss after coming out of scan because we haven't had
1713c448998dSSam Leffler 		 * time to receive a beacon then we should probe the AP
1714c448998dSSam Leffler 		 * before posting a real bmiss (unless iv_bmiss_max has
1715c448998dSSam Leffler 		 * been artifiically lowered).  A cleaner solution might
1716c448998dSSam Leffler 		 * be to disable the timer on scan start/end but to handle
1717c448998dSSam Leffler 		 * case of multiple sta vap's we'd need to disable the
1718c448998dSSam Leffler 		 * timers of all affected vap's.
1719c448998dSSam Leffler 		 */
1720c448998dSSam Leffler 		vap->iv_swbmiss_count = 0;
1721c448998dSSam Leffler 	} else if (vap->iv_swbmiss_count == 0) {
1722b032f27cSSam Leffler 		if (vap->iv_bmiss != NULL)
17235efea30fSAndrew Thompson 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1724e99662a6SSam Leffler 	} else
1725b032f27cSSam Leffler 		vap->iv_swbmiss_count = 0;
1726b032f27cSSam Leffler 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1727b032f27cSSam Leffler 		ieee80211_swbmiss, vap);
17287edb8cf9SSam Leffler }
17297edb8cf9SSam Leffler 
173068e8e04eSSam Leffler /*
1731b032f27cSSam Leffler  * Start an 802.11h channel switch.  We record the parameters,
1732b032f27cSSam Leffler  * mark the operation pending, notify each vap through the
1733b032f27cSSam Leffler  * beacon update mechanism so it can update the beacon frame
1734b032f27cSSam Leffler  * contents, and then switch vap's to CSA state to block outbound
1735b032f27cSSam Leffler  * traffic.  Devices that handle CSA directly can use the state
1736b032f27cSSam Leffler  * switch to do the right thing so long as they call
1737b032f27cSSam Leffler  * ieee80211_csa_completeswitch when it's time to complete the
1738b032f27cSSam Leffler  * channel change.  Devices that depend on the net80211 layer can
1739b032f27cSSam Leffler  * use ieee80211_beacon_update to handle the countdown and the
1740b032f27cSSam Leffler  * channel switch.
1741b032f27cSSam Leffler  */
1742b032f27cSSam Leffler void
1743b032f27cSSam Leffler ieee80211_csa_startswitch(struct ieee80211com *ic,
1744b032f27cSSam Leffler 	struct ieee80211_channel *c, int mode, int count)
1745b032f27cSSam Leffler {
1746b032f27cSSam Leffler 	struct ieee80211vap *vap;
1747b032f27cSSam Leffler 
1748b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1749b032f27cSSam Leffler 
1750b032f27cSSam Leffler 	ic->ic_csa_newchan = c;
1751c70761e6SSam Leffler 	ic->ic_csa_mode = mode;
1752b032f27cSSam Leffler 	ic->ic_csa_count = count;
1753b032f27cSSam Leffler 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1754b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1755b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
175659aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_IBSS ||
175759aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_MBSS)
1758b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1759b032f27cSSam Leffler 		/* switch to CSA state to block outbound traffic */
1760b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN)
1761b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1762b032f27cSSam Leffler 	}
1763b032f27cSSam Leffler 	ieee80211_notify_csa(ic, c, mode, count);
1764b032f27cSSam Leffler }
1765b032f27cSSam Leffler 
1766886bbec1SAdrian Chadd /*
1767886bbec1SAdrian Chadd  * Complete the channel switch by transitioning all CSA VAPs to RUN.
1768886bbec1SAdrian Chadd  * This is called by both the completion and cancellation functions
1769886bbec1SAdrian Chadd  * so each VAP is placed back in the RUN state and can thus transmit.
1770886bbec1SAdrian Chadd  */
1771c70761e6SSam Leffler static void
1772c70761e6SSam Leffler csa_completeswitch(struct ieee80211com *ic)
1773c70761e6SSam Leffler {
1774c70761e6SSam Leffler 	struct ieee80211vap *vap;
1775c70761e6SSam Leffler 
1776c70761e6SSam Leffler 	ic->ic_csa_newchan = NULL;
1777c70761e6SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1778c70761e6SSam Leffler 
1779c70761e6SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1780c70761e6SSam Leffler 		if (vap->iv_state == IEEE80211_S_CSA)
1781c70761e6SSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1782c70761e6SSam Leffler }
1783c70761e6SSam Leffler 
1784b032f27cSSam Leffler /*
1785b032f27cSSam Leffler  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1786b032f27cSSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1787b032f27cSSam Leffler  * so they can again transmit.
1788886bbec1SAdrian Chadd  *
1789886bbec1SAdrian Chadd  * Although this may not be completely correct, update the BSS channel
1790886bbec1SAdrian Chadd  * for each VAP to the newly configured channel. The setcurchan sets
1791886bbec1SAdrian Chadd  * the current operating channel for the interface (so the radio does
1792886bbec1SAdrian Chadd  * switch over) but the VAP BSS isn't updated, leading to incorrectly
1793886bbec1SAdrian Chadd  * reported information via ioctl.
1794b032f27cSSam Leffler  */
1795b032f27cSSam Leffler void
1796b032f27cSSam Leffler ieee80211_csa_completeswitch(struct ieee80211com *ic)
1797b032f27cSSam Leffler {
17986f16ec31SAdrian Chadd 	struct ieee80211vap *vap;
17996f16ec31SAdrian Chadd 
1800b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1801b032f27cSSam Leffler 
1802b032f27cSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1803b032f27cSSam Leffler 
1804b032f27cSSam Leffler 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1805886bbec1SAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1806886bbec1SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_CSA)
1807886bbec1SAdrian Chadd 			vap->iv_bss->ni_chan = ic->ic_curchan;
1808886bbec1SAdrian Chadd 
1809c70761e6SSam Leffler 	csa_completeswitch(ic);
1810c70761e6SSam Leffler }
1811b032f27cSSam Leffler 
1812c70761e6SSam Leffler /*
1813c70761e6SSam Leffler  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1814c70761e6SSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1815c70761e6SSam Leffler  * so they can again transmit.
1816c70761e6SSam Leffler  */
1817c70761e6SSam Leffler void
1818c70761e6SSam Leffler ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1819c70761e6SSam Leffler {
1820c70761e6SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1821c70761e6SSam Leffler 
1822c70761e6SSam Leffler 	csa_completeswitch(ic);
1823b032f27cSSam Leffler }
1824b032f27cSSam Leffler 
1825b032f27cSSam Leffler /*
1826b032f27cSSam Leffler  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1827b032f27cSSam Leffler  * We clear state and move all vap's in CAC state to RUN state.
1828b032f27cSSam Leffler  */
1829b032f27cSSam Leffler void
1830b032f27cSSam Leffler ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1831b032f27cSSam Leffler {
1832b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1833b032f27cSSam Leffler 	struct ieee80211vap *vap;
1834b032f27cSSam Leffler 
1835b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1836b032f27cSSam Leffler 	/*
1837b032f27cSSam Leffler 	 * Complete CAC state change for lead vap first; then
1838b032f27cSSam Leffler 	 * clock all the other vap's waiting.
1839b032f27cSSam Leffler 	 */
1840b032f27cSSam Leffler 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1841b032f27cSSam Leffler 	    ("wrong state %d", vap0->iv_state));
1842b032f27cSSam Leffler 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1843b032f27cSSam Leffler 
1844b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1845b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_CAC)
1846b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1847b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1848b032f27cSSam Leffler }
1849b032f27cSSam Leffler 
1850b032f27cSSam Leffler /*
1851b032f27cSSam Leffler  * Force all vap's other than the specified vap to the INIT state
1852b032f27cSSam Leffler  * and mark them as waiting for a scan to complete.  These vaps
1853b032f27cSSam Leffler  * will be brought up when the scan completes and the scanning vap
1854b032f27cSSam Leffler  * reaches RUN state by wakeupwaiting.
185568e8e04eSSam Leffler  */
185668e8e04eSSam Leffler static void
1857b032f27cSSam Leffler markwaiting(struct ieee80211vap *vap0)
185868e8e04eSSam Leffler {
1859b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1860b032f27cSSam Leffler 	struct ieee80211vap *vap;
1861b032f27cSSam Leffler 
1862b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1863b032f27cSSam Leffler 
18645efea30fSAndrew Thompson 	/*
18655efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
18665efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
18675efea30fSAndrew Thompson 	 * change task.
18685efea30fSAndrew Thompson 	 */
1869b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1870b032f27cSSam Leffler 		if (vap == vap0)
1871b032f27cSSam Leffler 			continue;
1872b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_INIT) {
18735efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1874b032f27cSSam Leffler 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1875dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1876b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1877b032f27cSSam Leffler 		}
187868e8e04eSSam Leffler 	}
187968e8e04eSSam Leffler }
188068e8e04eSSam Leffler 
1881b032f27cSSam Leffler /*
1882b032f27cSSam Leffler  * Wakeup all vap's waiting for a scan to complete.  This is the
1883b032f27cSSam Leffler  * companion to markwaiting (above) and is used to coordinate
1884b032f27cSSam Leffler  * multiple vaps scanning.
18855efea30fSAndrew Thompson  * This is called from the state taskqueue.
1886b032f27cSSam Leffler  */
1887b032f27cSSam Leffler static void
1888b032f27cSSam Leffler wakeupwaiting(struct ieee80211vap *vap0)
1889b032f27cSSam Leffler {
1890b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1891b032f27cSSam Leffler 	struct ieee80211vap *vap;
1892b032f27cSSam Leffler 
1893b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1894b032f27cSSam Leffler 
18955efea30fSAndrew Thompson 	/*
18965efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
18975efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
18985efea30fSAndrew Thompson 	 * change task.
18995efea30fSAndrew Thompson 	 */
1900b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1901b032f27cSSam Leffler 		if (vap == vap0)
1902b032f27cSSam Leffler 			continue;
1903b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1904b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1905b032f27cSSam Leffler 			/* NB: sta's cannot go INIT->RUN */
19065efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1907b032f27cSSam Leffler 			vap->iv_newstate(vap,
1908b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_STA ?
1909b032f27cSSam Leffler 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1910dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1911b032f27cSSam Leffler 		}
1912b032f27cSSam Leffler 	}
1913b032f27cSSam Leffler }
1914b032f27cSSam Leffler 
1915b032f27cSSam Leffler /*
1916b032f27cSSam Leffler  * Handle post state change work common to all operating modes.
1917b032f27cSSam Leffler  */
1918b032f27cSSam Leffler static void
19195efea30fSAndrew Thompson ieee80211_newstate_cb(void *xvap, int npending)
1920b032f27cSSam Leffler {
19215efea30fSAndrew Thompson 	struct ieee80211vap *vap = xvap;
1922b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
19235efea30fSAndrew Thompson 	enum ieee80211_state nstate, ostate;
19245efea30fSAndrew Thompson 	int arg, rc;
1925b032f27cSSam Leffler 
19265efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
19275efea30fSAndrew Thompson 	nstate = vap->iv_nstate;
19285efea30fSAndrew Thompson 	arg = vap->iv_nstate_arg;
1929b032f27cSSam Leffler 
19305efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
19315efea30fSAndrew Thompson 		/*
19325efea30fSAndrew Thompson 		 * We have been requested to drop back to the INIT before
19335efea30fSAndrew Thompson 		 * proceeding to the new state.
19345efea30fSAndrew Thompson 		 */
1935d13806f4SAndriy Voskoboinyk 		/* Deny any state changes while we are here. */
1936d13806f4SAndriy Voskoboinyk 		vap->iv_nstate = IEEE80211_S_INIT;
1937b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
19385efea30fSAndrew Thompson 		    "%s: %s -> %s arg %d\n", __func__,
19395efea30fSAndrew Thompson 		    ieee80211_state_name[vap->iv_state],
1940d13806f4SAndriy Voskoboinyk 		    ieee80211_state_name[vap->iv_nstate], arg);
1941d13806f4SAndriy Voskoboinyk 		vap->iv_newstate(vap, vap->iv_nstate, 0);
1942dcc56af0SAdrian Chadd 		IEEE80211_LOCK_ASSERT(ic);
1943d13806f4SAndriy Voskoboinyk 		vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
1944d13806f4SAndriy Voskoboinyk 		    IEEE80211_FEXT_STATEWAIT);
1945d13806f4SAndriy Voskoboinyk 		/* enqueue new state transition after cancel_scan() task */
1946d13806f4SAndriy Voskoboinyk 		ieee80211_new_state_locked(vap, nstate, arg);
1947d13806f4SAndriy Voskoboinyk 		goto done;
19485efea30fSAndrew Thompson 	}
19495efea30fSAndrew Thompson 
19505efea30fSAndrew Thompson 	ostate = vap->iv_state;
19515efea30fSAndrew Thompson 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
19525efea30fSAndrew Thompson 		/*
19535efea30fSAndrew Thompson 		 * SCAN was forced; e.g. on beacon miss.  Force other running
19545efea30fSAndrew Thompson 		 * vap's to INIT state and mark them as waiting for the scan to
19555efea30fSAndrew Thompson 		 * complete.  This insures they don't interfere with our
19565efea30fSAndrew Thompson 		 * scanning.  Since we are single threaded the vaps can not
19575efea30fSAndrew Thompson 		 * transition again while we are executing.
19585efea30fSAndrew Thompson 		 *
19595efea30fSAndrew Thompson 		 * XXX not always right, assumes ap follows sta
19605efea30fSAndrew Thompson 		 */
19615efea30fSAndrew Thompson 		markwaiting(vap);
19625efea30fSAndrew Thompson 	}
19635efea30fSAndrew Thompson 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
19645efea30fSAndrew Thompson 	    "%s: %s -> %s arg %d\n", __func__,
19655efea30fSAndrew Thompson 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
19665efea30fSAndrew Thompson 
19675efea30fSAndrew Thompson 	rc = vap->iv_newstate(vap, nstate, arg);
1968dcc56af0SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
19695efea30fSAndrew Thompson 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
19705efea30fSAndrew Thompson 	if (rc != 0) {
19715efea30fSAndrew Thompson 		/* State transition failed */
19725efea30fSAndrew Thompson 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
19735efea30fSAndrew Thompson 		KASSERT(nstate != IEEE80211_S_INIT,
19745efea30fSAndrew Thompson 		    ("INIT state change failed"));
19755efea30fSAndrew Thompson 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
19765efea30fSAndrew Thompson 		    "%s: %s returned error %d\n", __func__,
19775efea30fSAndrew Thompson 		    ieee80211_state_name[nstate], rc);
19785efea30fSAndrew Thompson 		goto done;
19795efea30fSAndrew Thompson 	}
19805efea30fSAndrew Thompson 
19815efea30fSAndrew Thompson 	/* No actual transition, skip post processing */
19825efea30fSAndrew Thompson 	if (ostate == nstate)
19835efea30fSAndrew Thompson 		goto done;
1984b032f27cSSam Leffler 
1985b032f27cSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
1986b032f27cSSam Leffler 		/*
1987b032f27cSSam Leffler 		 * OACTIVE may be set on the vap if the upper layer
1988b032f27cSSam Leffler 		 * tried to transmit (e.g. IPv6 NDP) before we reach
1989b032f27cSSam Leffler 		 * RUN state.  Clear it and restart xmit.
1990b032f27cSSam Leffler 		 *
1991b032f27cSSam Leffler 		 * Note this can also happen as a result of SLEEP->RUN
1992b032f27cSSam Leffler 		 * (i.e. coming out of power save mode).
1993b032f27cSSam Leffler 		 */
1994b032f27cSSam Leffler 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1995a7f31a36SAdrian Chadd 
1996a7f31a36SAdrian Chadd 		/*
1997e7495198SAdrian Chadd 		 * XXX TODO Kick-start a VAP queue - this should be a method!
1998a7f31a36SAdrian Chadd 		 */
1999b032f27cSSam Leffler 
2000b032f27cSSam Leffler 		/* bring up any vaps waiting on us */
2001b032f27cSSam Leffler 		wakeupwaiting(vap);
2002b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
2003b032f27cSSam Leffler 		/*
2004b032f27cSSam Leffler 		 * Flush the scan cache if we did the last scan (XXX?)
2005b032f27cSSam Leffler 		 * and flush any frames on send queues from this vap.
2006b032f27cSSam Leffler 		 * Note the mgt q is used only for legacy drivers and
2007b032f27cSSam Leffler 		 * will go away shortly.
2008b032f27cSSam Leffler 		 */
2009b032f27cSSam Leffler 		ieee80211_scan_flush(vap);
2010b032f27cSSam Leffler 
2011e7495198SAdrian Chadd 		/*
2012e7495198SAdrian Chadd 		 * XXX TODO: ic/vap queue flush
2013e7495198SAdrian Chadd 		 */
2014b032f27cSSam Leffler 	}
20155efea30fSAndrew Thompson done:
20165efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
2017b032f27cSSam Leffler }
2018b032f27cSSam Leffler 
2019b032f27cSSam Leffler /*
2020b032f27cSSam Leffler  * Public interface for initiating a state machine change.
2021b032f27cSSam Leffler  * This routine single-threads the request and coordinates
2022b032f27cSSam Leffler  * the scheduling of multiple vaps for the purpose of selecting
2023b032f27cSSam Leffler  * an operating channel.  Specifically the following scenarios
2024b032f27cSSam Leffler  * are handled:
2025b032f27cSSam Leffler  * o only one vap can be selecting a channel so on transition to
2026b032f27cSSam Leffler  *   SCAN state if another vap is already scanning then
2027b032f27cSSam Leffler  *   mark the caller for later processing and return without
2028b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
2029b032f27cSSam Leffler  * o only one vap can be doing CAC of a channel so on transition to
2030b032f27cSSam Leffler  *   CAC state if another vap is already scanning for radar then
2031b032f27cSSam Leffler  *   mark the caller for later processing and return without
2032b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
2033b032f27cSSam Leffler  * o if another vap is already running when a request is made
2034b032f27cSSam Leffler  *   to SCAN then an operating channel has been chosen; bypass
2035b032f27cSSam Leffler  *   the scan and just join the channel
2036b032f27cSSam Leffler  *
2037b032f27cSSam Leffler  * Note that the state change call is done through the iv_newstate
2038b032f27cSSam Leffler  * method pointer so any driver routine gets invoked.  The driver
2039b032f27cSSam Leffler  * will normally call back into operating mode-specific
2040b032f27cSSam Leffler  * ieee80211_newstate routines (below) unless it needs to completely
2041b032f27cSSam Leffler  * bypass the state machine (e.g. because the firmware has it's
2042b032f27cSSam Leffler  * own idea how things should work).  Bypassing the net80211 layer
2043b032f27cSSam Leffler  * is usually a mistake and indicates lack of proper integration
2044b032f27cSSam Leffler  * with the net80211 layer.
2045b032f27cSSam Leffler  */
2046e94527beSAdrian Chadd int
2047b032f27cSSam Leffler ieee80211_new_state_locked(struct ieee80211vap *vap,
2048b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
20498a1b9b6aSSam Leffler {
2050b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2051b032f27cSSam Leffler 	struct ieee80211vap *vp;
2052a11c9a5cSSam Leffler 	enum ieee80211_state ostate;
20535efea30fSAndrew Thompson 	int nrunning, nscanning;
20541a1e1d21SSam Leffler 
2055b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2056b032f27cSSam Leffler 
20575efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2058d13806f4SAndriy Voskoboinyk 		if (vap->iv_nstate == IEEE80211_S_INIT ||
2059d13806f4SAndriy Voskoboinyk 		    ((vap->iv_state == IEEE80211_S_INIT ||
2060d13806f4SAndriy Voskoboinyk 		    (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2061d13806f4SAndriy Voskoboinyk 		    vap->iv_nstate == IEEE80211_S_SCAN &&
2062d13806f4SAndriy Voskoboinyk 		    nstate > IEEE80211_S_SCAN)) {
20635efea30fSAndrew Thompson 			/*
2064d13806f4SAndriy Voskoboinyk 			 * XXX The vap is being stopped/started,
2065d13806f4SAndriy Voskoboinyk 			 * do not allow any other state changes
2066d13806f4SAndriy Voskoboinyk 			 * until this is completed.
20675efea30fSAndrew Thompson 			 */
2068d13806f4SAndriy Voskoboinyk 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2069d13806f4SAndriy Voskoboinyk 			    "%s: %s -> %s (%s) transition discarded\n",
2070d13806f4SAndriy Voskoboinyk 			    __func__,
2071d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[vap->iv_state],
2072d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[nstate],
2073d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[vap->iv_nstate]);
20745efea30fSAndrew Thompson 			return -1;
20758ee6f90aSAndrew Thompson 		} else if (vap->iv_state != vap->iv_nstate) {
20765efea30fSAndrew Thompson #if 0
20775efea30fSAndrew Thompson 			/* Warn if the previous state hasn't completed. */
20785efea30fSAndrew Thompson 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
20795efea30fSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n", __func__,
20805efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_state],
20815efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
20825efea30fSAndrew Thompson #else
20835efea30fSAndrew Thompson 			/* XXX temporarily enable to identify issues */
20848ee6f90aSAndrew Thompson 			if_printf(vap->iv_ifp,
20858ee6f90aSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n",
20865efea30fSAndrew Thompson 			    __func__, ieee80211_state_name[vap->iv_state],
20875efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
20885efea30fSAndrew Thompson #endif
20895efea30fSAndrew Thompson 		}
20908ee6f90aSAndrew Thompson 	}
20915efea30fSAndrew Thompson 
2092b032f27cSSam Leffler 	nrunning = nscanning = 0;
2093b032f27cSSam Leffler 	/* XXX can track this state instead of calculating */
2094b032f27cSSam Leffler 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2095b032f27cSSam Leffler 		if (vp != vap) {
2096b032f27cSSam Leffler 			if (vp->iv_state >= IEEE80211_S_RUN)
2097b032f27cSSam Leffler 				nrunning++;
2098b032f27cSSam Leffler 			/* XXX doesn't handle bg scan */
2099b032f27cSSam Leffler 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
2100b032f27cSSam Leffler 			else if (vp->iv_state > IEEE80211_S_INIT)
2101b032f27cSSam Leffler 				nscanning++;
2102b032f27cSSam Leffler 		}
2103b032f27cSSam Leffler 	}
2104b032f27cSSam Leffler 	ostate = vap->iv_state;
2105b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2106b032f27cSSam Leffler 	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
2107b032f27cSSam Leffler 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
2108b032f27cSSam Leffler 	    nrunning, nscanning);
21091a1e1d21SSam Leffler 	switch (nstate) {
21101a1e1d21SSam Leffler 	case IEEE80211_S_SCAN:
2111b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT) {
21121a1e1d21SSam Leffler 			/*
2113b032f27cSSam Leffler 			 * INIT -> SCAN happens on initial bringup.
21141a1e1d21SSam Leffler 			 */
2115b032f27cSSam Leffler 			KASSERT(!(nscanning && nrunning),
2116b032f27cSSam Leffler 			    ("%d scanning and %d running", nscanning, nrunning));
2117b032f27cSSam Leffler 			if (nscanning) {
211868e8e04eSSam Leffler 				/*
2119b032f27cSSam Leffler 				 * Someone is scanning, defer our state
2120b032f27cSSam Leffler 				 * change until the work has completed.
212168e8e04eSSam Leffler 				 */
2122b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2123b032f27cSSam Leffler 				    "%s: defer %s -> %s\n",
2124b032f27cSSam Leffler 				    __func__, ieee80211_state_name[ostate],
2125b032f27cSSam Leffler 				    ieee80211_state_name[nstate]);
2126b032f27cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
21275efea30fSAndrew Thompson 				return 0;
212868e8e04eSSam Leffler 			}
2129b032f27cSSam Leffler 			if (nrunning) {
213068e8e04eSSam Leffler 				/*
2131b032f27cSSam Leffler 				 * Someone is operating; just join the channel
2132b032f27cSSam Leffler 				 * they have chosen.
213368e8e04eSSam Leffler 				 */
2134b032f27cSSam Leffler 				/* XXX kill arg? */
2135b032f27cSSam Leffler 				/* XXX check each opmode, adhoc? */
2136b032f27cSSam Leffler 				if (vap->iv_opmode == IEEE80211_M_STA)
2137b032f27cSSam Leffler 					nstate = IEEE80211_S_SCAN;
21381a1e1d21SSam Leffler 				else
2139b032f27cSSam Leffler 					nstate = IEEE80211_S_RUN;
2140b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
2141b032f27cSSam Leffler 				if (nstate != IEEE80211_S_SCAN) {
2142b032f27cSSam Leffler 					IEEE80211_DPRINTF(vap,
2143b032f27cSSam Leffler 					    IEEE80211_MSG_STATE,
2144b032f27cSSam Leffler 					    "%s: override, now %s -> %s\n",
2145b032f27cSSam Leffler 					    __func__,
2146b032f27cSSam Leffler 					    ieee80211_state_name[ostate],
2147b032f27cSSam Leffler 					    ieee80211_state_name[nstate]);
21481a1e1d21SSam Leffler 				}
21498a1b9b6aSSam Leffler #endif
215068e8e04eSSam Leffler 			}
2151b032f27cSSam Leffler 		}
21521a1e1d21SSam Leffler 		break;
2153b032f27cSSam Leffler 	case IEEE80211_S_RUN:
2154b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS &&
2155b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2156b032f27cSSam Leffler 		    nscanning) {
2157b032f27cSSam Leffler 			/*
2158b032f27cSSam Leffler 			 * Legacy WDS with someone else scanning; don't
2159b032f27cSSam Leffler 			 * go online until that completes as we should
2160b032f27cSSam Leffler 			 * follow the other vap to the channel they choose.
2161b032f27cSSam Leffler 			 */
2162b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2163b032f27cSSam Leffler 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
2164b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2165b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2166b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
21675efea30fSAndrew Thompson 			return 0;
2168b032f27cSSam Leffler 		}
2169b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2170b032f27cSSam Leffler 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2171b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2172b032f27cSSam Leffler 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2173b032f27cSSam Leffler 			/*
2174b032f27cSSam Leffler 			 * This is a DFS channel, transition to CAC state
2175b032f27cSSam Leffler 			 * instead of RUN.  This allows us to initiate
2176b032f27cSSam Leffler 			 * Channel Availability Check (CAC) as specified
2177b032f27cSSam Leffler 			 * by 11h/DFS.
2178b032f27cSSam Leffler 			 */
2179b032f27cSSam Leffler 			nstate = IEEE80211_S_CAC;
2180b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2181b032f27cSSam Leffler 			     "%s: override %s -> %s (DFS)\n", __func__,
2182b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2183b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2184b032f27cSSam Leffler 		}
2185b032f27cSSam Leffler 		break;
2186b032f27cSSam Leffler 	case IEEE80211_S_INIT:
2187b016f58cSAndrew Thompson 		/* cancel any scan in progress */
2188b016f58cSAndrew Thompson 		ieee80211_cancel_scan(vap);
2189b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT ) {
2190b032f27cSSam Leffler 			/* XXX don't believe this */
2191b032f27cSSam Leffler 			/* INIT -> INIT. nothing to do */
2192b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2193b032f27cSSam Leffler 		}
2194b032f27cSSam Leffler 		/* fall thru... */
219514fb6b8fSSam Leffler 	default:
219614fb6b8fSSam Leffler 		break;
21971a1e1d21SSam Leffler 	}
21985efea30fSAndrew Thompson 	/* defer the state change to a thread */
21995efea30fSAndrew Thompson 	vap->iv_nstate = nstate;
22005efea30fSAndrew Thompson 	vap->iv_nstate_arg = arg;
22015efea30fSAndrew Thompson 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
22025efea30fSAndrew Thompson 	ieee80211_runtask(ic, &vap->iv_nstate_task);
22035efea30fSAndrew Thompson 	return EINPROGRESS;
22048a1b9b6aSSam Leffler }
2205b032f27cSSam Leffler 
2206b032f27cSSam Leffler int
2207b032f27cSSam Leffler ieee80211_new_state(struct ieee80211vap *vap,
2208b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
2209b032f27cSSam Leffler {
2210b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2211b032f27cSSam Leffler 	int rc;
2212b032f27cSSam Leffler 
2213b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2214b032f27cSSam Leffler 	rc = ieee80211_new_state_locked(vap, nstate, arg);
2215b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2216b032f27cSSam Leffler 	return rc;
22171a1e1d21SSam Leffler }
2218