xref: /freebsd/sys/net80211/ieee80211_proto.c (revision 2c13efdf1c3a8906a6b882923a071d9100ec1d32)
11a1e1d21SSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
47535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
5b032f27cSSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
6d72d72d3SAndriy Voskoboinyk  * Copyright (c) 2012 IEEE
71a1e1d21SSam Leffler  * All rights reserved.
81a1e1d21SSam Leffler  *
91a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
101a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
111a1e1d21SSam Leffler  * are met:
121a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
137535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
147535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
157535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
167535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
171a1e1d21SSam Leffler  *
187535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
197535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
207535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
217535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
227535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
237535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
257535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
267535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
277535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281a1e1d21SSam Leffler  */
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler #include <sys/cdefs.h>
311a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler /*
341a1e1d21SSam Leffler  * IEEE 802.11 protocol support.
351a1e1d21SSam Leffler  */
361a1e1d21SSam Leffler 
371a1e1d21SSam Leffler #include "opt_inet.h"
38b032f27cSSam Leffler #include "opt_wlan.h"
391a1e1d21SSam Leffler 
401a1e1d21SSam Leffler #include <sys/param.h>
418a1b9b6aSSam Leffler #include <sys/systm.h>
428ec07310SGleb Smirnoff #include <sys/kernel.h>
438ec07310SGleb Smirnoff #include <sys/malloc.h>
441a1e1d21SSam Leffler 
458a1b9b6aSSam Leffler #include <sys/socket.h>
46b032f27cSSam Leffler #include <sys/sockio.h>
471a1e1d21SSam Leffler 
481a1e1d21SSam Leffler #include <net/if.h>
4976039bc8SGleb Smirnoff #include <net/if_var.h>
501a1e1d21SSam Leffler #include <net/if_media.h>
518a1b9b6aSSam Leffler #include <net/ethernet.h>		/* XXX for ether_sprintf */
521a1e1d21SSam Leffler 
531a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
54b032f27cSSam Leffler #include <net80211/ieee80211_adhoc.h>
55b032f27cSSam Leffler #include <net80211/ieee80211_sta.h>
56b032f27cSSam Leffler #include <net80211/ieee80211_hostap.h>
57b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
5959aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
6059aa14a9SRui Paulo #endif
61b032f27cSSam Leffler #include <net80211/ieee80211_monitor.h>
62b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
631a1e1d21SSam Leffler 
648a1b9b6aSSam Leffler /* XXX tunables */
658a1b9b6aSSam Leffler #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
668a1b9b6aSSam Leffler #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
671a1e1d21SSam Leffler 
684357a5d1SAndriy Voskoboinyk const char *mgt_subtype_name[] = {
691a1e1d21SSam Leffler 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
70665d5ae9SAndriy Voskoboinyk 	"probe_req",	"probe_resp",	"timing_adv",	"reserved#7",
711a1e1d21SSam Leffler 	"beacon",	"atim",		"disassoc",	"auth",
7296283082SBernhard Schmidt 	"deauth",	"action",	"action_noack",	"reserved#15"
731a1e1d21SSam Leffler };
744357a5d1SAndriy Voskoboinyk const char *ctl_subtype_name[] = {
758a1b9b6aSSam Leffler 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
76665d5ae9SAndriy Voskoboinyk 	"reserved#4",	"reserved#5",	"reserved#6",	"control_wrap",
77665d5ae9SAndriy Voskoboinyk 	"bar",		"ba",		"ps_poll",	"rts",
788a1b9b6aSSam Leffler 	"cts",		"ack",		"cf_end",	"cf_end_ack"
798a1b9b6aSSam Leffler };
8049aa47d6SSam Leffler const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
8149aa47d6SSam Leffler 	"IBSS",		/* IEEE80211_M_IBSS */
8249aa47d6SSam Leffler 	"STA",		/* IEEE80211_M_STA */
83b032f27cSSam Leffler 	"WDS",		/* IEEE80211_M_WDS */
8449aa47d6SSam Leffler 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
8549aa47d6SSam Leffler 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
8659aa14a9SRui Paulo 	"MONITOR",	/* IEEE80211_M_MONITOR */
8759aa14a9SRui Paulo 	"MBSS"		/* IEEE80211_M_MBSS */
8849aa47d6SSam Leffler };
89a11c9a5cSSam Leffler const char *ieee80211_state_name[IEEE80211_S_MAX] = {
90a11c9a5cSSam Leffler 	"INIT",		/* IEEE80211_S_INIT */
91a11c9a5cSSam Leffler 	"SCAN",		/* IEEE80211_S_SCAN */
92a11c9a5cSSam Leffler 	"AUTH",		/* IEEE80211_S_AUTH */
93a11c9a5cSSam Leffler 	"ASSOC",	/* IEEE80211_S_ASSOC */
9414fb6b8fSSam Leffler 	"CAC",		/* IEEE80211_S_CAC */
9514fb6b8fSSam Leffler 	"RUN",		/* IEEE80211_S_RUN */
9614fb6b8fSSam Leffler 	"CSA",		/* IEEE80211_S_CSA */
9714fb6b8fSSam Leffler 	"SLEEP",	/* IEEE80211_S_SLEEP */
98a11c9a5cSSam Leffler };
998a1b9b6aSSam Leffler const char *ieee80211_wme_acnames[] = {
1008a1b9b6aSSam Leffler 	"WME_AC_BE",
1018a1b9b6aSSam Leffler 	"WME_AC_BK",
1028a1b9b6aSSam Leffler 	"WME_AC_VI",
1038a1b9b6aSSam Leffler 	"WME_AC_VO",
1048a1b9b6aSSam Leffler 	"WME_UPSD",
1058a1b9b6aSSam Leffler };
106a11c9a5cSSam Leffler 
107d72d72d3SAndriy Voskoboinyk 
108d72d72d3SAndriy Voskoboinyk /*
109d72d72d3SAndriy Voskoboinyk  * Reason code descriptions were (mostly) obtained from
110d72d72d3SAndriy Voskoboinyk  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
111d72d72d3SAndriy Voskoboinyk  */
112d72d72d3SAndriy Voskoboinyk const char *
113d72d72d3SAndriy Voskoboinyk ieee80211_reason_to_string(uint16_t reason)
114d72d72d3SAndriy Voskoboinyk {
115d72d72d3SAndriy Voskoboinyk 	switch (reason) {
116d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSPECIFIED:
117d72d72d3SAndriy Voskoboinyk 		return ("unspecified");
118d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AUTH_EXPIRE:
119d72d72d3SAndriy Voskoboinyk 		return ("previous authentication is expired");
120d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AUTH_LEAVE:
121d72d72d3SAndriy Voskoboinyk 		return ("sending STA is leaving/has left IBSS or ESS");
122d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_EXPIRE:
123d72d72d3SAndriy Voskoboinyk 		return ("disassociated due to inactivity");
124d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_TOOMANY:
125d72d72d3SAndriy Voskoboinyk 		return ("too many associated STAs");
126d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_NOT_AUTHED:
127d72d72d3SAndriy Voskoboinyk 		return ("class 2 frame received from nonauthenticated STA");
128d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_NOT_ASSOCED:
129d72d72d3SAndriy Voskoboinyk 		return ("class 3 frame received from nonassociated STA");
130d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_LEAVE:
131d72d72d3SAndriy Voskoboinyk 		return ("sending STA is leaving/has left BSS");
132d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
133d72d72d3SAndriy Voskoboinyk 		return ("STA requesting (re)association is not authenticated");
134d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
135d72d72d3SAndriy Voskoboinyk 		return ("information in the Power Capability element is "
136d72d72d3SAndriy Voskoboinyk 			"unacceptable");
137d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
138d72d72d3SAndriy Voskoboinyk 		return ("information in the Supported Channels element is "
139d72d72d3SAndriy Voskoboinyk 			"unacceptable");
140d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_IE_INVALID:
141d72d72d3SAndriy Voskoboinyk 		return ("invalid element");
142d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MIC_FAILURE:
143d72d72d3SAndriy Voskoboinyk 		return ("MIC failure");
144d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
145d72d72d3SAndriy Voskoboinyk 		return ("4-Way handshake timeout");
146d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
147d72d72d3SAndriy Voskoboinyk 		return ("group key update timeout");
148d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
149d72d72d3SAndriy Voskoboinyk 		return ("element in 4-Way handshake different from "
150d72d72d3SAndriy Voskoboinyk 			"(re)association request/probe response/beacon frame");
151d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_GROUP_CIPHER_INVALID:
152d72d72d3SAndriy Voskoboinyk 		return ("invalid group cipher");
153d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
154d72d72d3SAndriy Voskoboinyk 		return ("invalid pairwise cipher");
155d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AKMP_INVALID:
156d72d72d3SAndriy Voskoboinyk 		return ("invalid AKMP");
157d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
158d72d72d3SAndriy Voskoboinyk 		return ("unsupported version in RSN IE");
159d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_INVALID_RSN_IE_CAP:
160d72d72d3SAndriy Voskoboinyk 		return ("invalid capabilities in RSN IE");
161d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_802_1X_AUTH_FAILED:
162d72d72d3SAndriy Voskoboinyk 		return ("IEEE 802.1X authentication failed");
163d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
164d72d72d3SAndriy Voskoboinyk 		return ("cipher suite rejected because of the security "
165d72d72d3SAndriy Voskoboinyk 			"policy");
166d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSPECIFIED_QOS:
167d72d72d3SAndriy Voskoboinyk 		return ("unspecified (QoS-related)");
168d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_INSUFFICIENT_BW:
169d72d72d3SAndriy Voskoboinyk 		return ("QoS AP lacks sufficient bandwidth for this QoS STA");
170d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_TOOMANY_FRAMES:
171d72d72d3SAndriy Voskoboinyk 		return ("too many frames need to be acknowledged");
172d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_OUTSIDE_TXOP:
173d72d72d3SAndriy Voskoboinyk 		return ("STA is transmitting outside the limits of its TXOPs");
174d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_LEAVING_QBSS:
175d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (the STA is "
176d72d72d3SAndriy Voskoboinyk 			"resetting/leaving the BSS)");
177d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_BAD_MECHANISM:
178d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (it does not want to use "
179d72d72d3SAndriy Voskoboinyk 			"the mechanism)");
180d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_SETUP_NEEDED:
181d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (setup is required for the "
182d72d72d3SAndriy Voskoboinyk 			"used mechanism)");
183d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_TIMEOUT:
184d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (timeout)");
185d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_PEER_LINK_CANCELED:
186d72d72d3SAndriy Voskoboinyk 		return ("SME cancels the mesh peering instance (not related "
187d72d72d3SAndriy Voskoboinyk 			"to the maximum number of peer mesh STAs)");
188d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAX_PEERS:
189d72d72d3SAndriy Voskoboinyk 		return ("maximum number of peer mesh STAs was reached");
190d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CPVIOLATION:
191d72d72d3SAndriy Voskoboinyk 		return ("the received information violates the Mesh "
192d72d72d3SAndriy Voskoboinyk 			"Configuration policy configured in the mesh STA "
193d72d72d3SAndriy Voskoboinyk 			"profile");
194d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CLOSE_RCVD:
195d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA has received a Mesh Peering Close "
196d72d72d3SAndriy Voskoboinyk 			"message requesting to close the mesh peering");
197d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAX_RETRIES:
198d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
199d72d72d3SAndriy Voskoboinyk 			"Peering Open messages, without receiving a Mesh "
200d72d72d3SAndriy Voskoboinyk 			"Peering Confirm message");
201d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
202d72d72d3SAndriy Voskoboinyk 		return ("the confirmTimer for the mesh peering instance times "
203d72d72d3SAndriy Voskoboinyk 			"out");
204d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INVALID_GTK:
205d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA fails to unwrap the GTK or the values "
206d72d72d3SAndriy Voskoboinyk 			"in the wrapped contents do not match");
207d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INCONS_PARAMS:
208d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA receives inconsistent information about "
209d72d72d3SAndriy Voskoboinyk 			"the mesh parameters between Mesh Peering Management "
210d72d72d3SAndriy Voskoboinyk 			"frames");
211d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INVALID_SECURITY:
212d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA fails the authenticated mesh peering "
213d72d72d3SAndriy Voskoboinyk 			"exchange because due to failure in selecting "
214d72d72d3SAndriy Voskoboinyk 			"pairwise/group ciphersuite");
215d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_NO_PROXY:
216d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA does not have proxy information for "
217d72d72d3SAndriy Voskoboinyk 			"this external destination");
218d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_NO_FI:
219d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA does not have forwarding information "
220d72d72d3SAndriy Voskoboinyk 			"for this destination");
221d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
222d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA determines that the link to the next "
223d72d72d3SAndriy Voskoboinyk 			"hop of an active path in its forwarding information "
224d72d72d3SAndriy Voskoboinyk 			"is no longer usable");
225d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
226d72d72d3SAndriy Voskoboinyk 		return ("the MAC address of the STA already exists in the "
227d72d72d3SAndriy Voskoboinyk 			"mesh BSS");
228d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
229d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA performs channel switch to meet "
230d72d72d3SAndriy Voskoboinyk 			"regulatory requirements");
231d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
232d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA performs channel switch with "
233d72d72d3SAndriy Voskoboinyk 			"unspecified reason");
234d72d72d3SAndriy Voskoboinyk 	default:
235d72d72d3SAndriy Voskoboinyk 		return ("reserved/unknown");
236d72d72d3SAndriy Voskoboinyk 	}
237d72d72d3SAndriy Voskoboinyk }
238d72d72d3SAndriy Voskoboinyk 
2395efea30fSAndrew Thompson static void beacon_miss(void *, int);
2405efea30fSAndrew Thompson static void beacon_swmiss(void *, int);
241b032f27cSSam Leffler static void parent_updown(void *, int);
2425efea30fSAndrew Thompson static void update_mcast(void *, int);
2435efea30fSAndrew Thompson static void update_promisc(void *, int);
2445efea30fSAndrew Thompson static void update_channel(void *, int);
245b94299c4SAdrian Chadd static void update_chw(void *, int);
246e3e94c96SAdrian Chadd static void vap_update_wme(void *, int);
2474061c639SAndriy Voskoboinyk static void restart_vaps(void *, int);
2485efea30fSAndrew Thompson static void ieee80211_newstate_cb(void *, int);
2491a1e1d21SSam Leffler 
250b032f27cSSam Leffler static int
251b032f27cSSam Leffler null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
252b032f27cSSam Leffler 	const struct ieee80211_bpf_params *params)
253b105a069SSam Leffler {
254b032f27cSSam Leffler 
255c8f5794eSGleb Smirnoff 	ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
256b032f27cSSam Leffler 	m_freem(m);
257b032f27cSSam Leffler 	return ENETDOWN;
258b105a069SSam Leffler }
259b105a069SSam Leffler 
2601a1e1d21SSam Leffler void
2618a1b9b6aSSam Leffler ieee80211_proto_attach(struct ieee80211com *ic)
2621a1e1d21SSam Leffler {
2637a79cebfSGleb Smirnoff 	uint8_t hdrlen;
2641a1e1d21SSam Leffler 
265b032f27cSSam Leffler 	/* override the 802.3 setting */
2667a79cebfSGleb Smirnoff 	hdrlen = ic->ic_headroom
267b032f27cSSam Leffler 		+ sizeof(struct ieee80211_qosframe_addr4)
268b032f27cSSam Leffler 		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
269b032f27cSSam Leffler 		+ IEEE80211_WEP_EXTIVLEN;
270b032f27cSSam Leffler 	/* XXX no way to recalculate on ifdetach */
2717a79cebfSGleb Smirnoff 	if (ALIGN(hdrlen) > max_linkhdr) {
272b032f27cSSam Leffler 		/* XXX sanity check... */
2737a79cebfSGleb Smirnoff 		max_linkhdr = ALIGN(hdrlen);
274b032f27cSSam Leffler 		max_hdr = max_linkhdr + max_protohdr;
275b032f27cSSam Leffler 		max_datalen = MHLEN - max_hdr;
276b032f27cSSam Leffler 	}
2772e79ca97SSam Leffler 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
278b032f27cSSam Leffler 
2797a79cebfSGleb Smirnoff 	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
2805efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
2815efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
2825efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
2835efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
284b94299c4SAdrian Chadd 	TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
2854061c639SAndriy Voskoboinyk 	TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
2868a1b9b6aSSam Leffler 
2878a1b9b6aSSam Leffler 	ic->ic_wme.wme_hipri_switch_hysteresis =
2888a1b9b6aSSam Leffler 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
2891a1e1d21SSam Leffler 
2901a1e1d21SSam Leffler 	/* initialize management frame handlers */
2911a1e1d21SSam Leffler 	ic->ic_send_mgmt = ieee80211_send_mgmt;
292b032f27cSSam Leffler 	ic->ic_raw_xmit = null_raw_xmit;
293b032f27cSSam Leffler 
294b032f27cSSam Leffler 	ieee80211_adhoc_attach(ic);
295b032f27cSSam Leffler 	ieee80211_sta_attach(ic);
296b032f27cSSam Leffler 	ieee80211_wds_attach(ic);
297b032f27cSSam Leffler 	ieee80211_hostap_attach(ic);
29859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
29959aa14a9SRui Paulo 	ieee80211_mesh_attach(ic);
30059aa14a9SRui Paulo #endif
301b032f27cSSam Leffler 	ieee80211_monitor_attach(ic);
3021a1e1d21SSam Leffler }
3031a1e1d21SSam Leffler 
3041a1e1d21SSam Leffler void
3058a1b9b6aSSam Leffler ieee80211_proto_detach(struct ieee80211com *ic)
3061a1e1d21SSam Leffler {
307b032f27cSSam Leffler 	ieee80211_monitor_detach(ic);
30859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
30959aa14a9SRui Paulo 	ieee80211_mesh_detach(ic);
31059aa14a9SRui Paulo #endif
311b032f27cSSam Leffler 	ieee80211_hostap_detach(ic);
312b032f27cSSam Leffler 	ieee80211_wds_detach(ic);
313b032f27cSSam Leffler 	ieee80211_adhoc_detach(ic);
314b032f27cSSam Leffler 	ieee80211_sta_detach(ic);
315b032f27cSSam Leffler }
3168a1b9b6aSSam Leffler 
317b032f27cSSam Leffler static void
318b032f27cSSam Leffler null_update_beacon(struct ieee80211vap *vap, int item)
319b032f27cSSam Leffler {
320b032f27cSSam Leffler }
321b032f27cSSam Leffler 
322b032f27cSSam Leffler void
323b032f27cSSam Leffler ieee80211_proto_vattach(struct ieee80211vap *vap)
324b032f27cSSam Leffler {
325b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
326b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
327b032f27cSSam Leffler 	int i;
328b032f27cSSam Leffler 
329b032f27cSSam Leffler 	/* override the 802.3 setting */
3307a79cebfSGleb Smirnoff 	ifp->if_hdrlen = ic->ic_headroom
3317a79cebfSGleb Smirnoff                 + sizeof(struct ieee80211_qosframe_addr4)
3327a79cebfSGleb Smirnoff                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
3337a79cebfSGleb Smirnoff                 + IEEE80211_WEP_EXTIVLEN;
334b032f27cSSam Leffler 
335b032f27cSSam Leffler 	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
336b032f27cSSam Leffler 	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
337b032f27cSSam Leffler 	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
33823401900SAdrian Chadd 	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
339fd90e2edSJung-uk Kim 	callout_init(&vap->iv_mgtsend, 1);
3405efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
3415efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
342e3e94c96SAdrian Chadd 	TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
343b032f27cSSam Leffler 	/*
344b032f27cSSam Leffler 	 * Install default tx rate handling: no fixed rate, lowest
345b032f27cSSam Leffler 	 * supported rate for mgmt and multicast frames.  Default
346b032f27cSSam Leffler 	 * max retry count.  These settings can be changed by the
347b032f27cSSam Leffler 	 * driver and/or user applications.
348b032f27cSSam Leffler 	 */
349047db6b3SSam Leffler 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
3501c4cb651SAndriy Voskoboinyk 		if (isclr(ic->ic_modecaps, i))
3511c4cb651SAndriy Voskoboinyk 			continue;
3521c4cb651SAndriy Voskoboinyk 
353b032f27cSSam Leffler 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
354b032f27cSSam Leffler 
355b032f27cSSam Leffler 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
356338452c9SAdrian Chadd 
357338452c9SAdrian Chadd 		/*
358338452c9SAdrian Chadd 		 * Setting the management rate to MCS 0 assumes that the
359338452c9SAdrian Chadd 		 * BSS Basic rate set is empty and the BSS Basic MCS set
360338452c9SAdrian Chadd 		 * is not.
361338452c9SAdrian Chadd 		 *
362338452c9SAdrian Chadd 		 * Since we're not checking this, default to the lowest
363338452c9SAdrian Chadd 		 * defined rate for this mode.
364338452c9SAdrian Chadd 		 *
365338452c9SAdrian Chadd 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
366338452c9SAdrian Chadd 		 * some MCS management traffic (eg BA response frames.)
367338452c9SAdrian Chadd 		 *
368338452c9SAdrian Chadd 		 * See also: 9.6.0 of the 802.11n-2009 specification.
369338452c9SAdrian Chadd 		 */
370338452c9SAdrian Chadd #ifdef	NOTYET
371047db6b3SSam Leffler 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
372047db6b3SSam Leffler 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
373047db6b3SSam Leffler 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
374047db6b3SSam Leffler 		} else {
375b032f27cSSam Leffler 			vap->iv_txparms[i].mgmtrate =
376b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
377b032f27cSSam Leffler 			vap->iv_txparms[i].mcastrate =
378b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
379b032f27cSSam Leffler 		}
380338452c9SAdrian Chadd #endif
381338452c9SAdrian Chadd 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
382338452c9SAdrian Chadd 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
383b032f27cSSam Leffler 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
384b032f27cSSam Leffler 	}
385b032f27cSSam Leffler 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
386b032f27cSSam Leffler 
387b032f27cSSam Leffler 	vap->iv_update_beacon = null_update_beacon;
388b032f27cSSam Leffler 	vap->iv_deliver_data = ieee80211_deliver_data;
389b032f27cSSam Leffler 
390b032f27cSSam Leffler 	/* attach support for operating mode */
391b032f27cSSam Leffler 	ic->ic_vattach[vap->iv_opmode](vap);
392b032f27cSSam Leffler }
393b032f27cSSam Leffler 
394b032f27cSSam Leffler void
395b032f27cSSam Leffler ieee80211_proto_vdetach(struct ieee80211vap *vap)
396b032f27cSSam Leffler {
397b032f27cSSam Leffler #define	FREEAPPIE(ie) do { \
398b032f27cSSam Leffler 	if (ie != NULL) \
399b9b53389SAdrian Chadd 		IEEE80211_FREE(ie, M_80211_NODE_IE); \
400b032f27cSSam Leffler } while (0)
401b032f27cSSam Leffler 	/*
402b032f27cSSam Leffler 	 * Detach operating mode module.
403b032f27cSSam Leffler 	 */
404b032f27cSSam Leffler 	if (vap->iv_opdetach != NULL)
405b032f27cSSam Leffler 		vap->iv_opdetach(vap);
4068a1b9b6aSSam Leffler 	/*
4078a1b9b6aSSam Leffler 	 * This should not be needed as we detach when reseting
4088a1b9b6aSSam Leffler 	 * the state but be conservative here since the
4098a1b9b6aSSam Leffler 	 * authenticator may do things like spawn kernel threads.
4108a1b9b6aSSam Leffler 	 */
411b032f27cSSam Leffler 	if (vap->iv_auth->ia_detach != NULL)
412b032f27cSSam Leffler 		vap->iv_auth->ia_detach(vap);
4138a1b9b6aSSam Leffler 	/*
4148a1b9b6aSSam Leffler 	 * Detach any ACL'ator.
4158a1b9b6aSSam Leffler 	 */
416b032f27cSSam Leffler 	if (vap->iv_acl != NULL)
417b032f27cSSam Leffler 		vap->iv_acl->iac_detach(vap);
418b032f27cSSam Leffler 
419b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_beacon);
420b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_probereq);
421b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_proberesp);
422b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocreq);
423b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocresp);
424b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_wpa);
425b032f27cSSam Leffler #undef FREEAPPIE
4268a1b9b6aSSam Leffler }
4278a1b9b6aSSam Leffler 
4288a1b9b6aSSam Leffler /*
4298a1b9b6aSSam Leffler  * Simple-minded authenticator module support.
4308a1b9b6aSSam Leffler  */
4318a1b9b6aSSam Leffler 
4328a1b9b6aSSam Leffler #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
4338a1b9b6aSSam Leffler /* XXX well-known names */
4348a1b9b6aSSam Leffler static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
4358a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
4368a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
4378a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
4388a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
4398a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
4408a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
4418a1b9b6aSSam Leffler };
4428a1b9b6aSSam Leffler static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
4438a1b9b6aSSam Leffler 
4448a1b9b6aSSam Leffler static const struct ieee80211_authenticator auth_internal = {
4458a1b9b6aSSam Leffler 	.ia_name		= "wlan_internal",
4468a1b9b6aSSam Leffler 	.ia_attach		= NULL,
4478a1b9b6aSSam Leffler 	.ia_detach		= NULL,
4488a1b9b6aSSam Leffler 	.ia_node_join		= NULL,
4498a1b9b6aSSam Leffler 	.ia_node_leave		= NULL,
4508a1b9b6aSSam Leffler };
4518a1b9b6aSSam Leffler 
4528a1b9b6aSSam Leffler /*
4538a1b9b6aSSam Leffler  * Setup internal authenticators once; they are never unregistered.
4548a1b9b6aSSam Leffler  */
4558a1b9b6aSSam Leffler static void
4568a1b9b6aSSam Leffler ieee80211_auth_setup(void)
4578a1b9b6aSSam Leffler {
4588a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
4598a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
4608a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
4618a1b9b6aSSam Leffler }
4628a1b9b6aSSam Leffler SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
4638a1b9b6aSSam Leffler 
4648a1b9b6aSSam Leffler const struct ieee80211_authenticator *
4658a1b9b6aSSam Leffler ieee80211_authenticator_get(int auth)
4668a1b9b6aSSam Leffler {
4678a1b9b6aSSam Leffler 	if (auth >= IEEE80211_AUTH_MAX)
4688a1b9b6aSSam Leffler 		return NULL;
4698a1b9b6aSSam Leffler 	if (authenticators[auth] == NULL)
4708a1b9b6aSSam Leffler 		ieee80211_load_module(auth_modnames[auth]);
4718a1b9b6aSSam Leffler 	return authenticators[auth];
4721a1e1d21SSam Leffler }
4731a1e1d21SSam Leffler 
4741a1e1d21SSam Leffler void
4758a1b9b6aSSam Leffler ieee80211_authenticator_register(int type,
4768a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth)
4771a1e1d21SSam Leffler {
4788a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
4798a1b9b6aSSam Leffler 		return;
4808a1b9b6aSSam Leffler 	authenticators[type] = auth;
4818a1b9b6aSSam Leffler }
4828a1b9b6aSSam Leffler 
4838a1b9b6aSSam Leffler void
4848a1b9b6aSSam Leffler ieee80211_authenticator_unregister(int type)
4858a1b9b6aSSam Leffler {
4868a1b9b6aSSam Leffler 
4878a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
4888a1b9b6aSSam Leffler 		return;
4898a1b9b6aSSam Leffler 	authenticators[type] = NULL;
4908a1b9b6aSSam Leffler }
4918a1b9b6aSSam Leffler 
4928a1b9b6aSSam Leffler /*
4938a1b9b6aSSam Leffler  * Very simple-minded ACL module support.
4948a1b9b6aSSam Leffler  */
4958a1b9b6aSSam Leffler /* XXX just one for now */
4968a1b9b6aSSam Leffler static	const struct ieee80211_aclator *acl = NULL;
4978a1b9b6aSSam Leffler 
4988a1b9b6aSSam Leffler void
4998a1b9b6aSSam Leffler ieee80211_aclator_register(const struct ieee80211_aclator *iac)
5008a1b9b6aSSam Leffler {
5018a1b9b6aSSam Leffler 	printf("wlan: %s acl policy registered\n", iac->iac_name);
5028a1b9b6aSSam Leffler 	acl = iac;
5038a1b9b6aSSam Leffler }
5048a1b9b6aSSam Leffler 
5058a1b9b6aSSam Leffler void
5068a1b9b6aSSam Leffler ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
5078a1b9b6aSSam Leffler {
5088a1b9b6aSSam Leffler 	if (acl == iac)
5098a1b9b6aSSam Leffler 		acl = NULL;
5108a1b9b6aSSam Leffler 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
5118a1b9b6aSSam Leffler }
5128a1b9b6aSSam Leffler 
5138a1b9b6aSSam Leffler const struct ieee80211_aclator *
5148a1b9b6aSSam Leffler ieee80211_aclator_get(const char *name)
5158a1b9b6aSSam Leffler {
5168a1b9b6aSSam Leffler 	if (acl == NULL)
5178a1b9b6aSSam Leffler 		ieee80211_load_module("wlan_acl");
5188a1b9b6aSSam Leffler 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
5198a1b9b6aSSam Leffler }
5208a1b9b6aSSam Leffler 
5218a1b9b6aSSam Leffler void
52268e8e04eSSam Leffler ieee80211_print_essid(const uint8_t *essid, int len)
5238a1b9b6aSSam Leffler {
52468e8e04eSSam Leffler 	const uint8_t *p;
5251a1e1d21SSam Leffler 	int i;
5261a1e1d21SSam Leffler 
5271a1e1d21SSam Leffler 	if (len > IEEE80211_NWID_LEN)
5281a1e1d21SSam Leffler 		len = IEEE80211_NWID_LEN;
5291a1e1d21SSam Leffler 	/* determine printable or not */
5301a1e1d21SSam Leffler 	for (i = 0, p = essid; i < len; i++, p++) {
5311a1e1d21SSam Leffler 		if (*p < ' ' || *p > 0x7e)
5321a1e1d21SSam Leffler 			break;
5331a1e1d21SSam Leffler 	}
5341a1e1d21SSam Leffler 	if (i == len) {
5351a1e1d21SSam Leffler 		printf("\"");
5361a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
5371a1e1d21SSam Leffler 			printf("%c", *p);
5381a1e1d21SSam Leffler 		printf("\"");
5391a1e1d21SSam Leffler 	} else {
5401a1e1d21SSam Leffler 		printf("0x");
5411a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
5421a1e1d21SSam Leffler 			printf("%02x", *p);
5431a1e1d21SSam Leffler 	}
5441a1e1d21SSam Leffler }
5451a1e1d21SSam Leffler 
5461a1e1d21SSam Leffler void
54768e8e04eSSam Leffler ieee80211_dump_pkt(struct ieee80211com *ic,
54868e8e04eSSam Leffler 	const uint8_t *buf, int len, int rate, int rssi)
5491a1e1d21SSam Leffler {
5508a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh;
5511a1e1d21SSam Leffler 	int i;
5521a1e1d21SSam Leffler 
5538a1b9b6aSSam Leffler 	wh = (const struct ieee80211_frame *)buf;
5541a1e1d21SSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
5551a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
5561a1e1d21SSam Leffler 		printf("NODS %s", ether_sprintf(wh->i_addr2));
5571a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
5581a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr3));
5591a1e1d21SSam Leffler 		break;
5601a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
5611a1e1d21SSam Leffler 		printf("TODS %s", ether_sprintf(wh->i_addr2));
5621a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
5631a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr1));
5641a1e1d21SSam Leffler 		break;
5651a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
5661a1e1d21SSam Leffler 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
5671a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
5681a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr2));
5691a1e1d21SSam Leffler 		break;
5701a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
57168e8e04eSSam Leffler 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
5721a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
5731a1e1d21SSam Leffler 		printf("(%s", ether_sprintf(wh->i_addr2));
5741a1e1d21SSam Leffler 		printf("->%s)", ether_sprintf(wh->i_addr1));
5751a1e1d21SSam Leffler 		break;
5761a1e1d21SSam Leffler 	}
5771a1e1d21SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
5781a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
5791a1e1d21SSam Leffler 		printf(" data");
5801a1e1d21SSam Leffler 		break;
5811a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
5824357a5d1SAndriy Voskoboinyk 		printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
5831a1e1d21SSam Leffler 		break;
5841a1e1d21SSam Leffler 	default:
5851a1e1d21SSam Leffler 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
5861a1e1d21SSam Leffler 		break;
5871a1e1d21SSam Leffler 	}
58868e8e04eSSam Leffler 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
58968e8e04eSSam Leffler 		const struct ieee80211_qosframe *qwh =
59068e8e04eSSam Leffler 			(const struct ieee80211_qosframe *)buf;
59168e8e04eSSam Leffler 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
59268e8e04eSSam Leffler 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
59368e8e04eSSam Leffler 	}
5945945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
59568e8e04eSSam Leffler 		int off;
59668e8e04eSSam Leffler 
59768e8e04eSSam Leffler 		off = ieee80211_anyhdrspace(ic, wh);
59868e8e04eSSam Leffler 		printf(" WEP [IV %.02x %.02x %.02x",
59968e8e04eSSam Leffler 			buf[off+0], buf[off+1], buf[off+2]);
60068e8e04eSSam Leffler 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
60168e8e04eSSam Leffler 			printf(" %.02x %.02x %.02x",
60268e8e04eSSam Leffler 				buf[off+4], buf[off+5], buf[off+6]);
60368e8e04eSSam Leffler 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
6048a1b9b6aSSam Leffler 	}
6051a1e1d21SSam Leffler 	if (rate >= 0)
6061a1e1d21SSam Leffler 		printf(" %dM", rate / 2);
6071a1e1d21SSam Leffler 	if (rssi >= 0)
6081a1e1d21SSam Leffler 		printf(" +%d", rssi);
6091a1e1d21SSam Leffler 	printf("\n");
6101a1e1d21SSam Leffler 	if (len > 0) {
6111a1e1d21SSam Leffler 		for (i = 0; i < len; i++) {
6121a1e1d21SSam Leffler 			if ((i & 1) == 0)
6131a1e1d21SSam Leffler 				printf(" ");
6141a1e1d21SSam Leffler 			printf("%02x", buf[i]);
6151a1e1d21SSam Leffler 		}
6161a1e1d21SSam Leffler 		printf("\n");
6171a1e1d21SSam Leffler 	}
6181a1e1d21SSam Leffler }
6191a1e1d21SSam Leffler 
62079edaebfSSam Leffler static __inline int
62179edaebfSSam Leffler findrix(const struct ieee80211_rateset *rs, int r)
62279edaebfSSam Leffler {
62379edaebfSSam Leffler 	int i;
62479edaebfSSam Leffler 
62579edaebfSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
62679edaebfSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
62779edaebfSSam Leffler 			return i;
62879edaebfSSam Leffler 	return -1;
62979edaebfSSam Leffler }
63079edaebfSSam Leffler 
6311a1e1d21SSam Leffler int
63270e28b9aSSam Leffler ieee80211_fix_rate(struct ieee80211_node *ni,
63370e28b9aSSam Leffler 	struct ieee80211_rateset *nrs, int flags)
6341a1e1d21SSam Leffler {
635b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6367d77cd53SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
63779edaebfSSam Leffler 	int i, j, rix, error;
638b032f27cSSam Leffler 	int okrate, badrate, fixedrate, ucastrate;
63941b3c790SSam Leffler 	const struct ieee80211_rateset *srs;
64068e8e04eSSam Leffler 	uint8_t r;
6411a1e1d21SSam Leffler 
6421a1e1d21SSam Leffler 	error = 0;
64368e8e04eSSam Leffler 	okrate = badrate = 0;
644b032f27cSSam Leffler 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
645b032f27cSSam Leffler 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
646b032f27cSSam Leffler 		/*
647b032f27cSSam Leffler 		 * Workaround awkwardness with fixed rate.  We are called
648b032f27cSSam Leffler 		 * to check both the legacy rate set and the HT rate set
649b032f27cSSam Leffler 		 * but we must apply any legacy fixed rate check only to the
650b032f27cSSam Leffler 		 * legacy rate set and vice versa.  We cannot tell what type
651b032f27cSSam Leffler 		 * of rate set we've been given (legacy or HT) but we can
652b032f27cSSam Leffler 		 * distinguish the fixed rate type (MCS have 0x80 set).
653b032f27cSSam Leffler 		 * So to deal with this the caller communicates whether to
654b032f27cSSam Leffler 		 * check MCS or legacy rate using the flags and we use the
655b032f27cSSam Leffler 		 * type of any fixed rate to avoid applying an MCS to a
656b032f27cSSam Leffler 		 * legacy rate and vice versa.
657b032f27cSSam Leffler 		 */
658b032f27cSSam Leffler 		if (ucastrate & 0x80) {
659b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFRATE)
660b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFRATE;
661b032f27cSSam Leffler 		} else if ((ucastrate & 0x80) == 0) {
662b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFMCS)
663b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFMCS;
664b032f27cSSam Leffler 		}
665b032f27cSSam Leffler 		/* NB: required to make MCS match below work */
666b032f27cSSam Leffler 		ucastrate &= IEEE80211_RATE_VAL;
667b032f27cSSam Leffler 	}
66868e8e04eSSam Leffler 	fixedrate = IEEE80211_FIXED_RATE_NONE;
669b032f27cSSam Leffler 	/*
670b032f27cSSam Leffler 	 * XXX we are called to process both MCS and legacy rates;
671b032f27cSSam Leffler 	 * we must use the appropriate basic rate set or chaos will
672b032f27cSSam Leffler 	 * ensue; for now callers that want MCS must supply
673b032f27cSSam Leffler 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
674b032f27cSSam Leffler 	 * function so there are two variants, one for MCS and one
675b032f27cSSam Leffler 	 * for legacy rates.
676b032f27cSSam Leffler 	 */
677b032f27cSSam Leffler 	if (flags & IEEE80211_F_DOBRS)
678b032f27cSSam Leffler 		srs = (const struct ieee80211_rateset *)
679b032f27cSSam Leffler 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
680b032f27cSSam Leffler 	else
68141b3c790SSam Leffler 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
682ef39d4beSSam Leffler 	for (i = 0; i < nrs->rs_nrates; ) {
6831a1e1d21SSam Leffler 		if (flags & IEEE80211_F_DOSORT) {
6841a1e1d21SSam Leffler 			/*
6851a1e1d21SSam Leffler 			 * Sort rates.
6861a1e1d21SSam Leffler 			 */
6871a1e1d21SSam Leffler 			for (j = i + 1; j < nrs->rs_nrates; j++) {
6880ebe104fSAdrian Chadd 				if (IEEE80211_RV(nrs->rs_rates[i]) >
6890ebe104fSAdrian Chadd 				    IEEE80211_RV(nrs->rs_rates[j])) {
6901a1e1d21SSam Leffler 					r = nrs->rs_rates[i];
6911a1e1d21SSam Leffler 					nrs->rs_rates[i] = nrs->rs_rates[j];
6921a1e1d21SSam Leffler 					nrs->rs_rates[j] = r;
6931a1e1d21SSam Leffler 				}
6941a1e1d21SSam Leffler 			}
6951a1e1d21SSam Leffler 		}
6961a1e1d21SSam Leffler 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
6971a1e1d21SSam Leffler 		badrate = r;
6981a1e1d21SSam Leffler 		/*
69968e8e04eSSam Leffler 		 * Check for fixed rate.
7001a1e1d21SSam Leffler 		 */
701b032f27cSSam Leffler 		if (r == ucastrate)
7028a1b9b6aSSam Leffler 			fixedrate = r;
7031a1e1d21SSam Leffler 		/*
7041a1e1d21SSam Leffler 		 * Check against supported rates.
7051a1e1d21SSam Leffler 		 */
70679edaebfSSam Leffler 		rix = findrix(srs, r);
70779edaebfSSam Leffler 		if (flags & IEEE80211_F_DONEGO) {
70879edaebfSSam Leffler 			if (rix < 0) {
709ef39d4beSSam Leffler 				/*
710ef39d4beSSam Leffler 				 * A rate in the node's rate set is not
711ef39d4beSSam Leffler 				 * supported.  If this is a basic rate and we
71279edaebfSSam Leffler 				 * are operating as a STA then this is an error.
713ef39d4beSSam Leffler 				 * Otherwise we just discard/ignore the rate.
714ef39d4beSSam Leffler 				 */
71579edaebfSSam Leffler 				if ((flags & IEEE80211_F_JOIN) &&
716ef39d4beSSam Leffler 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
7171a1e1d21SSam Leffler 					error++;
71879edaebfSSam Leffler 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
71979edaebfSSam Leffler 				/*
72079edaebfSSam Leffler 				 * Overwrite with the supported rate
72179edaebfSSam Leffler 				 * value so any basic rate bit is set.
72279edaebfSSam Leffler 				 */
72379edaebfSSam Leffler 				nrs->rs_rates[i] = srs->rs_rates[rix];
7241a1e1d21SSam Leffler 			}
7251a1e1d21SSam Leffler 		}
72679edaebfSSam Leffler 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
7271a1e1d21SSam Leffler 			/*
7281a1e1d21SSam Leffler 			 * Delete unacceptable rates.
7291a1e1d21SSam Leffler 			 */
7301a1e1d21SSam Leffler 			nrs->rs_nrates--;
7311a1e1d21SSam Leffler 			for (j = i; j < nrs->rs_nrates; j++)
7321a1e1d21SSam Leffler 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
7331a1e1d21SSam Leffler 			nrs->rs_rates[j] = 0;
7341a1e1d21SSam Leffler 			continue;
7351a1e1d21SSam Leffler 		}
73679edaebfSSam Leffler 		if (rix >= 0)
7371a1e1d21SSam Leffler 			okrate = nrs->rs_rates[i];
7381a1e1d21SSam Leffler 		i++;
7391a1e1d21SSam Leffler 	}
7408a1b9b6aSSam Leffler 	if (okrate == 0 || error != 0 ||
741b032f27cSSam Leffler 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
742b032f27cSSam Leffler 	     fixedrate != ucastrate)) {
743b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
744b032f27cSSam Leffler 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
745b032f27cSSam Leffler 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
7461a1e1d21SSam Leffler 		return badrate | IEEE80211_RATE_BASIC;
747b032f27cSSam Leffler 	} else
7480ebe104fSAdrian Chadd 		return IEEE80211_RV(okrate);
7491a1e1d21SSam Leffler }
7501a1e1d21SSam Leffler 
7518a1b9b6aSSam Leffler /*
7528a1b9b6aSSam Leffler  * Reset 11g-related state.
7538a1b9b6aSSam Leffler  */
7548a1b9b6aSSam Leffler void
7558a1b9b6aSSam Leffler ieee80211_reset_erp(struct ieee80211com *ic)
7561a1e1d21SSam Leffler {
7578a1b9b6aSSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
7588a1b9b6aSSam Leffler 	ic->ic_nonerpsta = 0;
7598a1b9b6aSSam Leffler 	ic->ic_longslotsta = 0;
7608a1b9b6aSSam Leffler 	/*
7618a1b9b6aSSam Leffler 	 * Short slot time is enabled only when operating in 11g
7628a1b9b6aSSam Leffler 	 * and not in an IBSS.  We must also honor whether or not
7638a1b9b6aSSam Leffler 	 * the driver is capable of doing it.
7648a1b9b6aSSam Leffler 	 */
7658a1b9b6aSSam Leffler 	ieee80211_set_shortslottime(ic,
76668e8e04eSSam Leffler 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
76768e8e04eSSam Leffler 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
76868e8e04eSSam Leffler 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
7698a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
7708a1b9b6aSSam Leffler 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
7718a1b9b6aSSam Leffler 	/*
7728a1b9b6aSSam Leffler 	 * Set short preamble and ERP barker-preamble flags.
7738a1b9b6aSSam Leffler 	 */
77468e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
7758a1b9b6aSSam Leffler 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
7768a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
7778a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
7788a1b9b6aSSam Leffler 	} else {
7798a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
7808a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_USEBARKER;
7818a1b9b6aSSam Leffler 	}
7828a1b9b6aSSam Leffler }
7838a1b9b6aSSam Leffler 
7848a1b9b6aSSam Leffler /*
7858a1b9b6aSSam Leffler  * Set the short slot time state and notify the driver.
7868a1b9b6aSSam Leffler  */
7878a1b9b6aSSam Leffler void
7888a1b9b6aSSam Leffler ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
7898a1b9b6aSSam Leffler {
7908a1b9b6aSSam Leffler 	if (onoff)
7918a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHSLOT;
7928a1b9b6aSSam Leffler 	else
7938a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
7948a1b9b6aSSam Leffler 	/* notify driver */
7958a1b9b6aSSam Leffler 	if (ic->ic_updateslot != NULL)
796272f6adeSGleb Smirnoff 		ic->ic_updateslot(ic);
7978a1b9b6aSSam Leffler }
7988a1b9b6aSSam Leffler 
7998a1b9b6aSSam Leffler /*
8008a1b9b6aSSam Leffler  * Check if the specified rate set supports ERP.
8018a1b9b6aSSam Leffler  * NB: the rate set is assumed to be sorted.
8028a1b9b6aSSam Leffler  */
8038a1b9b6aSSam Leffler int
804b032f27cSSam Leffler ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
8058a1b9b6aSSam Leffler {
8068a1b9b6aSSam Leffler 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
8078a1b9b6aSSam Leffler 	int i, j;
8088a1b9b6aSSam Leffler 
809a3e08d6fSRui Paulo 	if (rs->rs_nrates < nitems(rates))
8108a1b9b6aSSam Leffler 		return 0;
811a3e08d6fSRui Paulo 	for (i = 0; i < nitems(rates); i++) {
8128a1b9b6aSSam Leffler 		for (j = 0; j < rs->rs_nrates; j++) {
8138a1b9b6aSSam Leffler 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
8148a1b9b6aSSam Leffler 			if (rates[i] == r)
8158a1b9b6aSSam Leffler 				goto next;
8168a1b9b6aSSam Leffler 			if (r > rates[i])
8178a1b9b6aSSam Leffler 				return 0;
8188a1b9b6aSSam Leffler 		}
8198a1b9b6aSSam Leffler 		return 0;
8208a1b9b6aSSam Leffler 	next:
8218a1b9b6aSSam Leffler 		;
8228a1b9b6aSSam Leffler 	}
8238a1b9b6aSSam Leffler 	return 1;
8248a1b9b6aSSam Leffler }
8258a1b9b6aSSam Leffler 
8268a1b9b6aSSam Leffler /*
827b032f27cSSam Leffler  * Mark the basic rates for the rate table based on the
8288a1b9b6aSSam Leffler  * operating mode.  For real 11g we mark all the 11b rates
8298a1b9b6aSSam Leffler  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
8308a1b9b6aSSam Leffler  * 11b rates.  There's also a pseudo 11a-mode used to mark only
8318a1b9b6aSSam Leffler  * the basic OFDM rates.
8328a1b9b6aSSam Leffler  */
833b032f27cSSam Leffler static void
834b032f27cSSam Leffler setbasicrates(struct ieee80211_rateset *rs,
835b032f27cSSam Leffler     enum ieee80211_phymode mode, int add)
8368a1b9b6aSSam Leffler {
83768e8e04eSSam Leffler 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
838be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
839be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
840be0df3e7SSam Leffler 					    /* NB: mixed b/g */
841be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
842be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
843be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
844be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
8456a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
8466a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
847be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
848be0df3e7SSam Leffler 					    /* NB: mixed b/g */
849be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
8508fde59a7SAdrian Chadd 					    /* NB: mixed b/g */
8518fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= { 4, { 2, 4, 11, 22 } },
8528fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= { 3, { 12, 24, 48 } },
8538a1b9b6aSSam Leffler 	};
8548a1b9b6aSSam Leffler 	int i, j;
8558a1b9b6aSSam Leffler 
8568a1b9b6aSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++) {
857b032f27cSSam Leffler 		if (!add)
8588a1b9b6aSSam Leffler 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
8598a1b9b6aSSam Leffler 		for (j = 0; j < basic[mode].rs_nrates; j++)
8608a1b9b6aSSam Leffler 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
8618a1b9b6aSSam Leffler 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
8628a1b9b6aSSam Leffler 				break;
8638a1b9b6aSSam Leffler 			}
8648a1b9b6aSSam Leffler 	}
8658a1b9b6aSSam Leffler }
8668a1b9b6aSSam Leffler 
8678a1b9b6aSSam Leffler /*
868b032f27cSSam Leffler  * Set the basic rates in a rate set.
869b032f27cSSam Leffler  */
870b032f27cSSam Leffler void
871b032f27cSSam Leffler ieee80211_setbasicrates(struct ieee80211_rateset *rs,
872b032f27cSSam Leffler     enum ieee80211_phymode mode)
873b032f27cSSam Leffler {
874b032f27cSSam Leffler 	setbasicrates(rs, mode, 0);
875b032f27cSSam Leffler }
876b032f27cSSam Leffler 
877b032f27cSSam Leffler /*
878b032f27cSSam Leffler  * Add basic rates to a rate set.
879b032f27cSSam Leffler  */
880b032f27cSSam Leffler void
881b032f27cSSam Leffler ieee80211_addbasicrates(struct ieee80211_rateset *rs,
882b032f27cSSam Leffler     enum ieee80211_phymode mode)
883b032f27cSSam Leffler {
884b032f27cSSam Leffler 	setbasicrates(rs, mode, 1);
885b032f27cSSam Leffler }
886b032f27cSSam Leffler 
887b032f27cSSam Leffler /*
888b032f27cSSam Leffler  * WME protocol support.
889b032f27cSSam Leffler  *
890b032f27cSSam Leffler  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
891b032f27cSSam Leffler  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
892b032f27cSSam Leffler  * Draft 2.0 Test Plan (Appendix D).
893b032f27cSSam Leffler  *
894b032f27cSSam Leffler  * Static/Dynamic Turbo mode settings come from Atheros.
8958a1b9b6aSSam Leffler  */
8968a1b9b6aSSam Leffler typedef struct phyParamType {
89768e8e04eSSam Leffler 	uint8_t		aifsn;
89868e8e04eSSam Leffler 	uint8_t		logcwmin;
89968e8e04eSSam Leffler 	uint8_t		logcwmax;
90068e8e04eSSam Leffler 	uint16_t	txopLimit;
90168e8e04eSSam Leffler 	uint8_t 	acm;
9028a1b9b6aSSam Leffler } paramType;
9038a1b9b6aSSam Leffler 
9048a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
905be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
906be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
907be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
908be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
909be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
910be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
911be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
912be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
9136a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
9146a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
915be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
916be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
9178fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 3, 4,  6,  0, 0 },
9188fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 3, 4,  6,  0, 0 },
9198a1b9b6aSSam Leffler };
9208a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
921be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
922be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
923be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
924be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
925be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
926be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
927be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
928be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
9296a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
9306a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
931be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
932be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
9338fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 7, 4, 10,  0, 0 },
9348fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 7, 4, 10,  0, 0 },
9358a1b9b6aSSam Leffler };
9368a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
937be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
938be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
939be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
940be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
941be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
942be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
943be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
944be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
9456a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
9466a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
947be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
948be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
9498fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 3, 4,  94, 0 },
9508fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 3, 4,  94, 0 },
9518a1b9b6aSSam Leffler };
9528a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
953be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
954be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
955be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
956be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
957be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
958be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
959be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
960be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
9616a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
9626a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
963be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
964be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
9658fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 2, 3,  47, 0 },
9668fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 2, 3,  47, 0 },
9678a1b9b6aSSam Leffler };
9688a1b9b6aSSam Leffler 
9698a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
970be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
971be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
972be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
973be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
974be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
975be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
976be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
977be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
9786a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
9796a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
980be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
981be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
9828a1b9b6aSSam Leffler };
9838a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
984be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
985be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
986be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
987be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
988be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
989be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
990be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
991be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
9926a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
9936a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
994be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
995be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
9968a1b9b6aSSam Leffler };
9978a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
998be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
999be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
1000be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
1001be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
1002be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
1003be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
1004be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
1005be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
10066a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
10076a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
1008be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
1009be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
10108a1b9b6aSSam Leffler };
10118a1b9b6aSSam Leffler 
1012b032f27cSSam Leffler static void
101367ce310aSSam Leffler _setifsparams(struct wmeParams *wmep, const paramType *phy)
101467ce310aSSam Leffler {
101567ce310aSSam Leffler 	wmep->wmep_aifsn = phy->aifsn;
101667ce310aSSam Leffler 	wmep->wmep_logcwmin = phy->logcwmin;
101767ce310aSSam Leffler 	wmep->wmep_logcwmax = phy->logcwmax;
101867ce310aSSam Leffler 	wmep->wmep_txopLimit = phy->txopLimit;
101967ce310aSSam Leffler }
102067ce310aSSam Leffler 
102167ce310aSSam Leffler static void
102267ce310aSSam Leffler setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
102367ce310aSSam Leffler 	struct wmeParams *wmep, const paramType *phy)
102467ce310aSSam Leffler {
102567ce310aSSam Leffler 	wmep->wmep_acm = phy->acm;
102667ce310aSSam Leffler 	_setifsparams(wmep, phy);
102767ce310aSSam Leffler 
102867ce310aSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
102967ce310aSSam Leffler 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
103067ce310aSSam Leffler 	    ieee80211_wme_acnames[ac], type,
103167ce310aSSam Leffler 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
103267ce310aSSam Leffler 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
103367ce310aSSam Leffler }
103467ce310aSSam Leffler 
103567ce310aSSam Leffler static void
1036b032f27cSSam Leffler ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
10378a1b9b6aSSam Leffler {
1038b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
10398a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
10408a1b9b6aSSam Leffler 	const paramType *pPhyParam, *pBssPhyParam;
10418a1b9b6aSSam Leffler 	struct wmeParams *wmep;
104268e8e04eSSam Leffler 	enum ieee80211_phymode mode;
10438a1b9b6aSSam Leffler 	int i;
10448a1b9b6aSSam Leffler 
1045b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1046b032f27cSSam Leffler 
1047a4b3c7a5SSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
10488a1b9b6aSSam Leffler 		return;
10498a1b9b6aSSam Leffler 
105068e8e04eSSam Leffler 	/*
10510d4e4e5eSAdrian Chadd 	 * Clear the wme cap_info field so a qoscount from a previous
10520d4e4e5eSAdrian Chadd 	 * vap doesn't confuse later code which only parses the beacon
10530d4e4e5eSAdrian Chadd 	 * field and updates hardware when said field changes.
10540d4e4e5eSAdrian Chadd 	 * Otherwise the hardware is programmed with defaults, not what
10550d4e4e5eSAdrian Chadd 	 * the beacon actually announces.
10560d4e4e5eSAdrian Chadd 	 */
10570d4e4e5eSAdrian Chadd 	wme->wme_wmeChanParams.cap_info = 0;
10580d4e4e5eSAdrian Chadd 
10590d4e4e5eSAdrian Chadd 	/*
106068e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
106168e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
106268e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
106368e8e04eSSam Leffler 	 * so state will eventually get set correctly
106468e8e04eSSam Leffler 	 */
106568e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
106668e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
106768e8e04eSSam Leffler 	else
106868e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
10698a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
10708a1b9b6aSSam Leffler 		switch (i) {
10718a1b9b6aSSam Leffler 		case WME_AC_BK:
107268e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BK[mode];
107368e8e04eSSam Leffler 			pBssPhyParam = &phyParamForAC_BK[mode];
10748a1b9b6aSSam Leffler 			break;
10758a1b9b6aSSam Leffler 		case WME_AC_VI:
107668e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VI[mode];
107768e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
10788a1b9b6aSSam Leffler 			break;
10798a1b9b6aSSam Leffler 		case WME_AC_VO:
108068e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VO[mode];
108168e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
10828a1b9b6aSSam Leffler 			break;
10838a1b9b6aSSam Leffler 		case WME_AC_BE:
10848a1b9b6aSSam Leffler 		default:
108568e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BE[mode];
108668e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
10878a1b9b6aSSam Leffler 			break;
10888a1b9b6aSSam Leffler 		}
10898a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
10908a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
109167ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
10928a1b9b6aSSam Leffler 		} else {
109367ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
10948a1b9b6aSSam Leffler 		}
10958a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
109667ce310aSSam Leffler 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
10978a1b9b6aSSam Leffler 	}
10988a1b9b6aSSam Leffler 	/* NB: check ic_bss to avoid NULL deref on initial attach */
1099b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
11008a1b9b6aSSam Leffler 		/*
1101a4641f4eSPedro F. Giffuni 		 * Calculate aggressive mode switching threshold based
11028a1b9b6aSSam Leffler 		 * on beacon interval.  This doesn't need locking since
11038a1b9b6aSSam Leffler 		 * we're only called before entering the RUN state at
11048a1b9b6aSSam Leffler 		 * which point we start sending beacon frames.
11058a1b9b6aSSam Leffler 		 */
11068a1b9b6aSSam Leffler 		wme->wme_hipri_switch_thresh =
1107b032f27cSSam Leffler 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1108a4b3c7a5SSam Leffler 		wme->wme_flags &= ~WME_F_AGGRMODE;
1109b032f27cSSam Leffler 		ieee80211_wme_updateparams(vap);
11108a1b9b6aSSam Leffler 	}
11118a1b9b6aSSam Leffler }
11128a1b9b6aSSam Leffler 
1113b032f27cSSam Leffler void
1114b032f27cSSam Leffler ieee80211_wme_initparams(struct ieee80211vap *vap)
1115b032f27cSSam Leffler {
1116b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1117b032f27cSSam Leffler 
1118b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1119b032f27cSSam Leffler 	ieee80211_wme_initparams_locked(vap);
1120b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1121b032f27cSSam Leffler }
1122b032f27cSSam Leffler 
11238a1b9b6aSSam Leffler /*
11248a1b9b6aSSam Leffler  * Update WME parameters for ourself and the BSS.
11258a1b9b6aSSam Leffler  */
11268a1b9b6aSSam Leffler void
1127b032f27cSSam Leffler ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
11288a1b9b6aSSam Leffler {
112967ce310aSSam Leffler 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1130be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
1131be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
1132be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
1133be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
1134be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
1135be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
1136be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
1137be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
11386a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
11396a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
1140be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
1141be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
11428fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
11438fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
11448a1b9b6aSSam Leffler 	};
1145b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
11468a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
11478a1b9b6aSSam Leffler 	const struct wmeParams *wmep;
11488a1b9b6aSSam Leffler 	struct wmeParams *chanp, *bssp;
114968e8e04eSSam Leffler 	enum ieee80211_phymode mode;
11508a1b9b6aSSam Leffler 	int i;
1151a48a8ad7SAdrian Chadd 	int do_aggrmode = 0;
11528a1b9b6aSSam Leffler 
115367ce310aSSam Leffler        	/*
115467ce310aSSam Leffler 	 * Set up the channel access parameters for the physical
115567ce310aSSam Leffler 	 * device.  First populate the configured settings.
115667ce310aSSam Leffler 	 */
11578a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
11588a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
11598a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
11608a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
11618a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
11628a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
11638a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
11648a1b9b6aSSam Leffler 
11658a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
11668a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
11678a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
11688a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
11698a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
11708a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
11718a1b9b6aSSam Leffler 	}
11728a1b9b6aSSam Leffler 
11738a1b9b6aSSam Leffler 	/*
117468e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
117568e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
117668e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
117768e8e04eSSam Leffler 	 * so state will eventually get set correctly
117868e8e04eSSam Leffler 	 */
117968e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
118068e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
118168e8e04eSSam Leffler 	else
118268e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
118368e8e04eSSam Leffler 
118468e8e04eSSam Leffler 	/*
1185a4641f4eSPedro F. Giffuni 	 * This implements aggressive mode as found in certain
11868a1b9b6aSSam Leffler 	 * vendors' AP's.  When there is significant high
11878a1b9b6aSSam Leffler 	 * priority (VI/VO) traffic in the BSS throttle back BE
11888a1b9b6aSSam Leffler 	 * traffic by using conservative parameters.  Otherwise
1189a4641f4eSPedro F. Giffuni 	 * BE uses aggressive params to optimize performance of
11908a1b9b6aSSam Leffler 	 * legacy/non-QoS traffic.
11918a1b9b6aSSam Leffler 	 */
1192a48a8ad7SAdrian Chadd 
1193a48a8ad7SAdrian Chadd 	/* Hostap? Only if aggressive mode is enabled */
1194a48a8ad7SAdrian Chadd         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1195a48a8ad7SAdrian Chadd 	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
1196a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1197a48a8ad7SAdrian Chadd 
1198a48a8ad7SAdrian Chadd 	/*
1199a48a8ad7SAdrian Chadd 	 * Station? Only if we're in a non-QoS BSS.
1200a48a8ad7SAdrian Chadd 	 */
1201a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_STA &&
1202a48a8ad7SAdrian Chadd 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1203a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1204a48a8ad7SAdrian Chadd 
1205a48a8ad7SAdrian Chadd 	/*
1206a48a8ad7SAdrian Chadd 	 * IBSS? Only if we we have WME enabled.
1207a48a8ad7SAdrian Chadd 	 */
1208a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1209a48a8ad7SAdrian Chadd 	    (vap->iv_flags & IEEE80211_F_WME))
1210a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1211a48a8ad7SAdrian Chadd 
1212a48a8ad7SAdrian Chadd 	/*
1213a48a8ad7SAdrian Chadd 	 * If WME is disabled on this VAP, default to aggressive mode
1214a48a8ad7SAdrian Chadd 	 * regardless of the configuration.
1215a48a8ad7SAdrian Chadd 	 */
1216a48a8ad7SAdrian Chadd 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1217a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1218a48a8ad7SAdrian Chadd 
1219a48a8ad7SAdrian Chadd 	/* XXX WDS? */
1220a48a8ad7SAdrian Chadd 
1221a48a8ad7SAdrian Chadd 	/* XXX MBSS? */
1222a48a8ad7SAdrian Chadd 
1223a48a8ad7SAdrian Chadd 	if (do_aggrmode) {
12248a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
12258a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
12268a1b9b6aSSam Leffler 
122767ce310aSSam Leffler 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
12288a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
122967ce310aSSam Leffler 		    aggrParam[mode].logcwmin;
12308a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
123167ce310aSSam Leffler 		    aggrParam[mode].logcwmax;
12328a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1233b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_BURST) ?
123467ce310aSSam Leffler 			aggrParam[mode].txopLimit : 0;
1235b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
123667ce310aSSam Leffler 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
123767ce310aSSam Leffler 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
123867ce310aSSam Leffler 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
123967ce310aSSam Leffler 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
12408a1b9b6aSSam Leffler 	}
12418a1b9b6aSSam Leffler 
1242a48a8ad7SAdrian Chadd 
1243a48a8ad7SAdrian Chadd 	/*
1244a48a8ad7SAdrian Chadd 	 * Change the contention window based on the number of associated
1245a48a8ad7SAdrian Chadd 	 * stations.  If the number of associated stations is 1 and
1246a48a8ad7SAdrian Chadd 	 * aggressive mode is enabled, lower the contention window even
1247a48a8ad7SAdrian Chadd 	 * further.
1248a48a8ad7SAdrian Chadd 	 */
1249b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1250ad262427SSam Leffler 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
125168e8e04eSSam Leffler 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1252be0df3e7SSam Leffler 		    [IEEE80211_MODE_AUTO]	= 3,
1253be0df3e7SSam Leffler 		    [IEEE80211_MODE_11A]	= 3,
1254be0df3e7SSam Leffler 		    [IEEE80211_MODE_11B]	= 4,
1255be0df3e7SSam Leffler 		    [IEEE80211_MODE_11G]	= 3,
1256be0df3e7SSam Leffler 		    [IEEE80211_MODE_FH]		= 4,
1257be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_A]	= 3,
1258be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_G]	= 3,
1259be0df3e7SSam Leffler 		    [IEEE80211_MODE_STURBO_A]	= 3,
12606a76ae21SSam Leffler 		    [IEEE80211_MODE_HALF]	= 3,
12616a76ae21SSam Leffler 		    [IEEE80211_MODE_QUARTER]	= 3,
1262be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NA]	= 3,
1263be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NG]	= 3,
12648fde59a7SAdrian Chadd 		    [IEEE80211_MODE_VHT_2GHZ]	= 3,
12658fde59a7SAdrian Chadd 		    [IEEE80211_MODE_VHT_5GHZ]	= 3,
12668a1b9b6aSSam Leffler 		};
12678a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
12688a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
12698a1b9b6aSSam Leffler 
127068e8e04eSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1271b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
127267ce310aSSam Leffler 		    "update %s (chan+bss) logcwmin %u\n",
127367ce310aSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
12748a1b9b6aSSam Leffler 	}
1275a48a8ad7SAdrian Chadd 
1276a48a8ad7SAdrian Chadd 	/*
1277a48a8ad7SAdrian Chadd 	 * Arrange for the beacon update.
1278a48a8ad7SAdrian Chadd 	 *
1279a48a8ad7SAdrian Chadd 	 * XXX what about MBSS, WDS?
1280a48a8ad7SAdrian Chadd 	 */
1281a48a8ad7SAdrian Chadd 	if (vap->iv_opmode == IEEE80211_M_HOSTAP
1282a48a8ad7SAdrian Chadd 	    || vap->iv_opmode == IEEE80211_M_IBSS) {
12838a1b9b6aSSam Leffler 		/*
12848a1b9b6aSSam Leffler 		 * Arrange for a beacon update and bump the parameter
12858a1b9b6aSSam Leffler 		 * set number so associated stations load the new values.
12868a1b9b6aSSam Leffler 		 */
12878a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info =
12888a1b9b6aSSam Leffler 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1289b032f27cSSam Leffler 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
12908a1b9b6aSSam Leffler 	}
12918a1b9b6aSSam Leffler 
1292dd2fb488SAdrian Chadd 	/* schedule the deferred WME update */
1293e3e94c96SAdrian Chadd 	ieee80211_runtask(ic, &vap->iv_wme_task);
12948a1b9b6aSSam Leffler 
1295b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
12968a1b9b6aSSam Leffler 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1297b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA ?
12988a1b9b6aSSam Leffler 		wme->wme_wmeChanParams.cap_info :
12998a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info);
13008a1b9b6aSSam Leffler }
13018a1b9b6aSSam Leffler 
13028a1b9b6aSSam Leffler void
1303b032f27cSSam Leffler ieee80211_wme_updateparams(struct ieee80211vap *vap)
13048a1b9b6aSSam Leffler {
1305b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
13068a1b9b6aSSam Leffler 
13078a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_WME) {
1308b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
1309b032f27cSSam Leffler 		ieee80211_wme_updateparams_locked(vap);
1310b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
13118a1b9b6aSSam Leffler 	}
13128a1b9b6aSSam Leffler }
13138a1b9b6aSSam Leffler 
13140c696036SAdrian Chadd /*
13150c696036SAdrian Chadd  * Fetch the WME parameters for the given VAP.
13160c696036SAdrian Chadd  *
13170c696036SAdrian Chadd  * When net80211 grows p2p, etc support, this may return different
13180c696036SAdrian Chadd  * parameters for each VAP.
13190c696036SAdrian Chadd  */
1320d03baf35SAdrian Chadd void
1321d03baf35SAdrian Chadd ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
1322d03baf35SAdrian Chadd {
1323d03baf35SAdrian Chadd 
1324d03baf35SAdrian Chadd 	memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
1325d03baf35SAdrian Chadd }
1326d03baf35SAdrian Chadd 
13270c696036SAdrian Chadd /*
13280c696036SAdrian Chadd  * For NICs which only support one set of WME paramaters (ie, softmac NICs)
13290c696036SAdrian Chadd  * there may be different VAP WME parameters but only one is "active".
13300c696036SAdrian Chadd  * This returns the "NIC" WME parameters for the currently active
13310c696036SAdrian Chadd  * context.
13320c696036SAdrian Chadd  */
1333d03baf35SAdrian Chadd void
1334d03baf35SAdrian Chadd ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
1335d03baf35SAdrian Chadd {
1336d03baf35SAdrian Chadd 
1337d03baf35SAdrian Chadd 	memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
1338d03baf35SAdrian Chadd }
1339d03baf35SAdrian Chadd 
13400c696036SAdrian Chadd /*
13410c696036SAdrian Chadd  * Return whether to use QoS on a given WME queue.
13420c696036SAdrian Chadd  *
13430c696036SAdrian Chadd  * This is intended to be called from the transmit path of softmac drivers
13440c696036SAdrian Chadd  * which are setting NoAck bits in transmit descriptors.
13450c696036SAdrian Chadd  *
13460c696036SAdrian Chadd  * Ideally this would be set in some transmit field before the packet is
13470c696036SAdrian Chadd  * queued to the driver but net80211 isn't quite there yet.
13480c696036SAdrian Chadd  */
13490c696036SAdrian Chadd int
13500c696036SAdrian Chadd ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
13510c696036SAdrian Chadd {
13520c696036SAdrian Chadd 	/* Bounds/sanity check */
13530c696036SAdrian Chadd 	if (ac < 0 || ac >= WME_NUM_AC)
13540c696036SAdrian Chadd 		return (0);
13550c696036SAdrian Chadd 
13560c696036SAdrian Chadd 	/* Again, there's only one global context for now */
13570c696036SAdrian Chadd 	return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
13580c696036SAdrian Chadd }
13590c696036SAdrian Chadd 
1360b032f27cSSam Leffler static void
1361b032f27cSSam Leffler parent_updown(void *arg, int npending)
136268e8e04eSSam Leffler {
13637a79cebfSGleb Smirnoff 	struct ieee80211com *ic = arg;
136468e8e04eSSam Leffler 
13657a79cebfSGleb Smirnoff 	ic->ic_parent(ic);
1366b032f27cSSam Leffler }
136768e8e04eSSam Leffler 
13685efea30fSAndrew Thompson static void
13695efea30fSAndrew Thompson update_mcast(void *arg, int npending)
13705efea30fSAndrew Thompson {
13715efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
13725efea30fSAndrew Thompson 
1373272f6adeSGleb Smirnoff 	ic->ic_update_mcast(ic);
13745efea30fSAndrew Thompson }
13755efea30fSAndrew Thompson 
13765efea30fSAndrew Thompson static void
13775efea30fSAndrew Thompson update_promisc(void *arg, int npending)
13785efea30fSAndrew Thompson {
13795efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
13805efea30fSAndrew Thompson 
1381272f6adeSGleb Smirnoff 	ic->ic_update_promisc(ic);
13825efea30fSAndrew Thompson }
13835efea30fSAndrew Thompson 
13845efea30fSAndrew Thompson static void
13855efea30fSAndrew Thompson update_channel(void *arg, int npending)
13865efea30fSAndrew Thompson {
13875efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
13885efea30fSAndrew Thompson 
13895efea30fSAndrew Thompson 	ic->ic_set_channel(ic);
13905463c4a4SSam Leffler 	ieee80211_radiotap_chan_change(ic);
13915efea30fSAndrew Thompson }
13925efea30fSAndrew Thompson 
1393b94299c4SAdrian Chadd static void
1394b94299c4SAdrian Chadd update_chw(void *arg, int npending)
1395b94299c4SAdrian Chadd {
1396b94299c4SAdrian Chadd 	struct ieee80211com *ic = arg;
1397b94299c4SAdrian Chadd 
1398b94299c4SAdrian Chadd 	/*
1399b94299c4SAdrian Chadd 	 * XXX should we defer the channel width _config_ update until now?
1400b94299c4SAdrian Chadd 	 */
1401b94299c4SAdrian Chadd 	ic->ic_update_chw(ic);
1402b94299c4SAdrian Chadd }
1403b94299c4SAdrian Chadd 
1404dd2fb488SAdrian Chadd /*
1405e3e94c96SAdrian Chadd  * Deferred WME update.
1406e3e94c96SAdrian Chadd  *
1407e3e94c96SAdrian Chadd  * In preparation for per-VAP WME configuration, call the VAP
1408e3e94c96SAdrian Chadd  * method if the VAP requires it.  Otherwise, just call the
1409e3e94c96SAdrian Chadd  * older global method.  There isn't a per-VAP WME configuration
1410e3e94c96SAdrian Chadd  * just yet so for now just use the global configuration.
1411dd2fb488SAdrian Chadd  */
1412e3e94c96SAdrian Chadd static void
1413e3e94c96SAdrian Chadd vap_update_wme(void *arg, int npending)
1414e3e94c96SAdrian Chadd {
1415e3e94c96SAdrian Chadd 	struct ieee80211vap *vap = arg;
1416e3e94c96SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1417e3e94c96SAdrian Chadd 
1418e3e94c96SAdrian Chadd 	if (vap->iv_wme_update != NULL)
1419e3e94c96SAdrian Chadd 		vap->iv_wme_update(vap,
1420e3e94c96SAdrian Chadd 		    ic->ic_wme.wme_chanParams.cap_wmeParams);
1421e3e94c96SAdrian Chadd 	else
1422dd2fb488SAdrian Chadd 		ic->ic_wme.wme_update(ic);
1423dd2fb488SAdrian Chadd }
1424dd2fb488SAdrian Chadd 
14254061c639SAndriy Voskoboinyk static void
14264061c639SAndriy Voskoboinyk restart_vaps(void *arg, int npending)
14274061c639SAndriy Voskoboinyk {
14284061c639SAndriy Voskoboinyk 	struct ieee80211com *ic = arg;
14294061c639SAndriy Voskoboinyk 
14304061c639SAndriy Voskoboinyk 	ieee80211_suspend_all(ic);
14314061c639SAndriy Voskoboinyk 	ieee80211_resume_all(ic);
14324061c639SAndriy Voskoboinyk }
14334061c639SAndriy Voskoboinyk 
143468e8e04eSSam Leffler /*
1435ae55932eSAndrew Thompson  * Block until the parent is in a known state.  This is
1436ae55932eSAndrew Thompson  * used after any operations that dispatch a task (e.g.
1437ae55932eSAndrew Thompson  * to auto-configure the parent device up/down).
1438ae55932eSAndrew Thompson  */
1439ae55932eSAndrew Thompson void
1440ae55932eSAndrew Thompson ieee80211_waitfor_parent(struct ieee80211com *ic)
1441ae55932eSAndrew Thompson {
14425efea30fSAndrew Thompson 	taskqueue_block(ic->ic_tq);
14435efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_parent_task);
14445efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_mcast_task);
14455efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_promisc_task);
14465efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_chan_task);
14475efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1448b94299c4SAdrian Chadd 	ieee80211_draintask(ic, &ic->ic_chw_task);
14495efea30fSAndrew Thompson 	taskqueue_unblock(ic->ic_tq);
1450ae55932eSAndrew Thompson }
1451ae55932eSAndrew Thompson 
1452ae55932eSAndrew Thompson /*
145324034ddbSAdrian Chadd  * Check to see whether the current channel needs reset.
145424034ddbSAdrian Chadd  *
145524034ddbSAdrian Chadd  * Some devices don't handle being given an invalid channel
145624034ddbSAdrian Chadd  * in their operating mode very well (eg wpi(4) will throw a
145724034ddbSAdrian Chadd  * firmware exception.)
145824034ddbSAdrian Chadd  *
145924034ddbSAdrian Chadd  * Return 0 if we're ok, 1 if the channel needs to be reset.
146024034ddbSAdrian Chadd  *
146124034ddbSAdrian Chadd  * See PR kern/202502.
146224034ddbSAdrian Chadd  */
146324034ddbSAdrian Chadd static int
146424034ddbSAdrian Chadd ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
146524034ddbSAdrian Chadd {
146624034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
146724034ddbSAdrian Chadd 
146824034ddbSAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_IBSS &&
146924034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
147024034ddbSAdrian Chadd 	    (vap->iv_opmode == IEEE80211_M_HOSTAP &&
147124034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
147224034ddbSAdrian Chadd 		return (1);
147324034ddbSAdrian Chadd 	return (0);
147424034ddbSAdrian Chadd }
147524034ddbSAdrian Chadd 
147624034ddbSAdrian Chadd /*
147724034ddbSAdrian Chadd  * Reset the curchan to a known good state.
147824034ddbSAdrian Chadd  */
147924034ddbSAdrian Chadd static void
148024034ddbSAdrian Chadd ieee80211_start_reset_chan(struct ieee80211vap *vap)
148124034ddbSAdrian Chadd {
148224034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
148324034ddbSAdrian Chadd 
148424034ddbSAdrian Chadd 	ic->ic_curchan = &ic->ic_channels[0];
148524034ddbSAdrian Chadd }
148624034ddbSAdrian Chadd 
148724034ddbSAdrian Chadd /*
1488b032f27cSSam Leffler  * Start a vap running.  If this is the first vap to be
1489b032f27cSSam Leffler  * set running on the underlying device then we
1490b032f27cSSam Leffler  * automatically bring the device up.
149168e8e04eSSam Leffler  */
1492b032f27cSSam Leffler void
1493b032f27cSSam Leffler ieee80211_start_locked(struct ieee80211vap *vap)
1494b032f27cSSam Leffler {
1495b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1496b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1497b032f27cSSam Leffler 
1498b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1499b032f27cSSam Leffler 
1500b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap,
1501b032f27cSSam Leffler 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1502b032f27cSSam Leffler 		"start running, %d vaps running\n", ic->ic_nrunning);
1503b032f27cSSam Leffler 
1504b032f27cSSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1505b032f27cSSam Leffler 		/*
1506b032f27cSSam Leffler 		 * Mark us running.  Note that it's ok to do this first;
1507b032f27cSSam Leffler 		 * if we need to bring the parent device up we defer that
1508b032f27cSSam Leffler 		 * to avoid dropping the com lock.  We expect the device
1509b032f27cSSam Leffler 		 * to respond to being marked up by calling back into us
1510b032f27cSSam Leffler 		 * through ieee80211_start_all at which point we'll come
1511b032f27cSSam Leffler 		 * back in here and complete the work.
1512b032f27cSSam Leffler 		 */
1513b032f27cSSam Leffler 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1514*2c13efdfSAndriy Gapon 		ieee80211_notify_ifnet_change(vap);
1515*2c13efdfSAndriy Gapon 
1516b032f27cSSam Leffler 		/*
1517b032f27cSSam Leffler 		 * We are not running; if this we are the first vap
1518b032f27cSSam Leffler 		 * to be brought up auto-up the parent if necessary.
1519b032f27cSSam Leffler 		 */
15207a79cebfSGleb Smirnoff 		if (ic->ic_nrunning++ == 0) {
152124034ddbSAdrian Chadd 
152224034ddbSAdrian Chadd 			/* reset the channel to a known good channel */
152324034ddbSAdrian Chadd 			if (ieee80211_start_check_reset_chan(vap))
152424034ddbSAdrian Chadd 				ieee80211_start_reset_chan(vap);
152524034ddbSAdrian Chadd 
1526b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1527b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
15287a79cebfSGleb Smirnoff 			    "%s: up parent %s\n", __func__, ic->ic_name);
15295efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1530b032f27cSSam Leffler 			return;
1531b032f27cSSam Leffler 		}
1532b032f27cSSam Leffler 	}
1533b032f27cSSam Leffler 	/*
1534b032f27cSSam Leffler 	 * If the parent is up and running, then kick the
1535b032f27cSSam Leffler 	 * 802.11 state machine as appropriate.
1536b032f27cSSam Leffler 	 */
15377a79cebfSGleb Smirnoff 	if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1538b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1539b032f27cSSam Leffler #if 0
1540b032f27cSSam Leffler 			/* XXX bypasses scan too easily; disable for now */
1541b032f27cSSam Leffler 			/*
1542b032f27cSSam Leffler 			 * Try to be intelligent about clocking the state
1543b032f27cSSam Leffler 			 * machine.  If we're currently in RUN state then
1544b032f27cSSam Leffler 			 * we should be able to apply any new state/parameters
1545b032f27cSSam Leffler 			 * simply by re-associating.  Otherwise we need to
1546b032f27cSSam Leffler 			 * re-scan to select an appropriate ap.
1547b032f27cSSam Leffler 			 */
1548b032f27cSSam Leffler 			if (vap->iv_state >= IEEE80211_S_RUN)
1549b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1550b032f27cSSam Leffler 				    IEEE80211_S_ASSOC, 1);
1551b032f27cSSam Leffler 			else
1552b032f27cSSam Leffler #endif
1553b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1554b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
155568e8e04eSSam Leffler 		} else {
155668e8e04eSSam Leffler 			/*
1557b032f27cSSam Leffler 			 * For monitor+wds mode there's nothing to do but
1558b032f27cSSam Leffler 			 * start running.  Otherwise if this is the first
155968e8e04eSSam Leffler 			 * vap to be brought up, start a scan which may be
156068e8e04eSSam Leffler 			 * preempted if the station is locked to a particular
156168e8e04eSSam Leffler 			 * channel.
156268e8e04eSSam Leffler 			 */
15635efea30fSAndrew Thompson 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1564b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1565b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_WDS)
1566b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1567b032f27cSSam Leffler 				    IEEE80211_S_RUN, -1);
1568b032f27cSSam Leffler 			else
1569b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1570b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
157168e8e04eSSam Leffler 		}
157268e8e04eSSam Leffler 	}
1573b032f27cSSam Leffler }
1574b032f27cSSam Leffler 
1575b032f27cSSam Leffler /*
1576b032f27cSSam Leffler  * Start a single vap.
1577b032f27cSSam Leffler  */
1578b032f27cSSam Leffler void
1579b032f27cSSam Leffler ieee80211_init(void *arg)
1580b032f27cSSam Leffler {
1581b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1582b032f27cSSam Leffler 
158335f434b2SSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1584b032f27cSSam Leffler 	    "%s\n", __func__);
1585b032f27cSSam Leffler 
1586b032f27cSSam Leffler 	IEEE80211_LOCK(vap->iv_ic);
1587b032f27cSSam Leffler 	ieee80211_start_locked(vap);
1588b032f27cSSam Leffler 	IEEE80211_UNLOCK(vap->iv_ic);
1589b032f27cSSam Leffler }
1590b032f27cSSam Leffler 
1591b032f27cSSam Leffler /*
1592b032f27cSSam Leffler  * Start all runnable vap's on a device.
1593b032f27cSSam Leffler  */
1594b032f27cSSam Leffler void
1595b032f27cSSam Leffler ieee80211_start_all(struct ieee80211com *ic)
1596b032f27cSSam Leffler {
1597b032f27cSSam Leffler 	struct ieee80211vap *vap;
1598b032f27cSSam Leffler 
1599b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1600b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1601b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1602b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1603b032f27cSSam Leffler 			ieee80211_start_locked(vap);
1604b032f27cSSam Leffler 	}
1605b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1606b032f27cSSam Leffler }
1607b032f27cSSam Leffler 
1608b032f27cSSam Leffler /*
1609b032f27cSSam Leffler  * Stop a vap.  We force it down using the state machine
1610b032f27cSSam Leffler  * then mark it's ifnet not running.  If this is the last
1611b032f27cSSam Leffler  * vap running on the underlying device then we close it
1612b032f27cSSam Leffler  * too to insure it will be properly initialized when the
1613b032f27cSSam Leffler  * next vap is brought up.
1614b032f27cSSam Leffler  */
1615b032f27cSSam Leffler void
1616b032f27cSSam Leffler ieee80211_stop_locked(struct ieee80211vap *vap)
1617b032f27cSSam Leffler {
1618b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1619b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1620b032f27cSSam Leffler 
1621b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1622b032f27cSSam Leffler 
1623b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1624b032f27cSSam Leffler 	    "stop running, %d vaps running\n", ic->ic_nrunning);
1625b032f27cSSam Leffler 
1626b032f27cSSam Leffler 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1627b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1628b032f27cSSam Leffler 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1629*2c13efdfSAndriy Gapon 		ieee80211_notify_ifnet_change(vap);
16307a79cebfSGleb Smirnoff 		if (--ic->ic_nrunning == 0) {
1631b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1632b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
16337a79cebfSGleb Smirnoff 			    "down parent %s\n", ic->ic_name);
16345efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1635b032f27cSSam Leffler 		}
1636b032f27cSSam Leffler 	}
1637b032f27cSSam Leffler }
1638b032f27cSSam Leffler 
1639b032f27cSSam Leffler void
1640b032f27cSSam Leffler ieee80211_stop(struct ieee80211vap *vap)
1641b032f27cSSam Leffler {
1642b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1643b032f27cSSam Leffler 
1644b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1645b032f27cSSam Leffler 	ieee80211_stop_locked(vap);
1646b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1647b032f27cSSam Leffler }
1648b032f27cSSam Leffler 
1649b032f27cSSam Leffler /*
1650b032f27cSSam Leffler  * Stop all vap's running on a device.
1651b032f27cSSam Leffler  */
1652b032f27cSSam Leffler void
1653b032f27cSSam Leffler ieee80211_stop_all(struct ieee80211com *ic)
1654b032f27cSSam Leffler {
1655b032f27cSSam Leffler 	struct ieee80211vap *vap;
1656b032f27cSSam Leffler 
1657b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1658b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1659b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1660b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1661b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
1662b032f27cSSam Leffler 	}
1663b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1664ae55932eSAndrew Thompson 
1665ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
166668e8e04eSSam Leffler }
166768e8e04eSSam Leffler 
166868e8e04eSSam Leffler /*
16696076cbacSSam Leffler  * Stop all vap's running on a device and arrange
16706076cbacSSam Leffler  * for those that were running to be resumed.
16716076cbacSSam Leffler  */
16726076cbacSSam Leffler void
16736076cbacSSam Leffler ieee80211_suspend_all(struct ieee80211com *ic)
16746076cbacSSam Leffler {
16756076cbacSSam Leffler 	struct ieee80211vap *vap;
16766076cbacSSam Leffler 
16776076cbacSSam Leffler 	IEEE80211_LOCK(ic);
16786076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
16796076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
16806076cbacSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
16816076cbacSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
16826076cbacSSam Leffler 			ieee80211_stop_locked(vap);
16836076cbacSSam Leffler 		}
16846076cbacSSam Leffler 	}
16856076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
1686ae55932eSAndrew Thompson 
1687ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
16886076cbacSSam Leffler }
16896076cbacSSam Leffler 
16906076cbacSSam Leffler /*
16916076cbacSSam Leffler  * Start all vap's marked for resume.
16926076cbacSSam Leffler  */
16936076cbacSSam Leffler void
16946076cbacSSam Leffler ieee80211_resume_all(struct ieee80211com *ic)
16956076cbacSSam Leffler {
16966076cbacSSam Leffler 	struct ieee80211vap *vap;
16976076cbacSSam Leffler 
16986076cbacSSam Leffler 	IEEE80211_LOCK(ic);
16996076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
17006076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
17016076cbacSSam Leffler 		if (!IFNET_IS_UP_RUNNING(ifp) &&
17026076cbacSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
17036076cbacSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
17046076cbacSSam Leffler 			ieee80211_start_locked(vap);
17056076cbacSSam Leffler 		}
17066076cbacSSam Leffler 	}
17076076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
17086076cbacSSam Leffler }
17096076cbacSSam Leffler 
17104061c639SAndriy Voskoboinyk /*
17114061c639SAndriy Voskoboinyk  * Restart all vap's running on a device.
17124061c639SAndriy Voskoboinyk  */
17134061c639SAndriy Voskoboinyk void
17144061c639SAndriy Voskoboinyk ieee80211_restart_all(struct ieee80211com *ic)
17154061c639SAndriy Voskoboinyk {
17164061c639SAndriy Voskoboinyk 	/*
17174061c639SAndriy Voskoboinyk 	 * NB: do not use ieee80211_runtask here, we will
17184061c639SAndriy Voskoboinyk 	 * block & drain net80211 taskqueue.
17194061c639SAndriy Voskoboinyk 	 */
17204061c639SAndriy Voskoboinyk 	taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
17214061c639SAndriy Voskoboinyk }
17224061c639SAndriy Voskoboinyk 
1723e701e041SSam Leffler void
1724e701e041SSam Leffler ieee80211_beacon_miss(struct ieee80211com *ic)
1725e701e041SSam Leffler {
17265efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
17275efea30fSAndrew Thompson 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
17285efea30fSAndrew Thompson 		/* Process in a taskq, the handler may reenter the driver */
17295efea30fSAndrew Thompson 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
17305efea30fSAndrew Thompson 	}
17315efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
17325efea30fSAndrew Thompson }
17335efea30fSAndrew Thompson 
17345efea30fSAndrew Thompson static void
17355efea30fSAndrew Thompson beacon_miss(void *arg, int npending)
17365efea30fSAndrew Thompson {
17375efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
1738b032f27cSSam Leffler 	struct ieee80211vap *vap;
1739e701e041SSam Leffler 
174023401900SAdrian Chadd 	IEEE80211_LOCK(ic);
1741b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1742e701e041SSam Leffler 		/*
1743d8c364fbSAndriy Voskoboinyk 		 * We only pass events through for sta vap's in RUN+ state;
1744b032f27cSSam Leffler 		 * may be too restrictive but for now this saves all the
1745b032f27cSSam Leffler 		 * handlers duplicating these checks.
1746e701e041SSam Leffler 		 */
1747b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
1748c70761e6SSam Leffler 		    vap->iv_state >= IEEE80211_S_RUN &&
1749b032f27cSSam Leffler 		    vap->iv_bmiss != NULL)
1750b032f27cSSam Leffler 			vap->iv_bmiss(vap);
1751e701e041SSam Leffler 	}
175223401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
175368e8e04eSSam Leffler }
1754e701e041SSam Leffler 
17555efea30fSAndrew Thompson static void
17565efea30fSAndrew Thompson beacon_swmiss(void *arg, int npending)
17575efea30fSAndrew Thompson {
17585efea30fSAndrew Thompson 	struct ieee80211vap *vap = arg;
175923401900SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
17605efea30fSAndrew Thompson 
176123401900SAdrian Chadd 	IEEE80211_LOCK(ic);
1762d8c364fbSAndriy Voskoboinyk 	if (vap->iv_state >= IEEE80211_S_RUN) {
17635efea30fSAndrew Thompson 		/* XXX Call multiple times if npending > zero? */
17645efea30fSAndrew Thompson 		vap->iv_bmiss(vap);
17655efea30fSAndrew Thompson 	}
176623401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
176723401900SAdrian Chadd }
17685efea30fSAndrew Thompson 
1769e99662a6SSam Leffler /*
1770e99662a6SSam Leffler  * Software beacon miss handling.  Check if any beacons
1771e99662a6SSam Leffler  * were received in the last period.  If not post a
1772e99662a6SSam Leffler  * beacon miss; otherwise reset the counter.
1773e99662a6SSam Leffler  */
1774b032f27cSSam Leffler void
1775e99662a6SSam Leffler ieee80211_swbmiss(void *arg)
1776e99662a6SSam Leffler {
1777b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1778c448998dSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1779e99662a6SSam Leffler 
178023401900SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
178123401900SAdrian Chadd 
1782d8c364fbSAndriy Voskoboinyk 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1783c448998dSSam Leffler 	    ("wrong state %d", vap->iv_state));
1784c448998dSSam Leffler 
1785c448998dSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN) {
1786c448998dSSam Leffler 		/*
1787c448998dSSam Leffler 		 * If scanning just ignore and reset state.  If we get a
1788c448998dSSam Leffler 		 * bmiss after coming out of scan because we haven't had
1789c448998dSSam Leffler 		 * time to receive a beacon then we should probe the AP
1790c448998dSSam Leffler 		 * before posting a real bmiss (unless iv_bmiss_max has
1791c448998dSSam Leffler 		 * been artifiically lowered).  A cleaner solution might
1792c448998dSSam Leffler 		 * be to disable the timer on scan start/end but to handle
1793c448998dSSam Leffler 		 * case of multiple sta vap's we'd need to disable the
1794c448998dSSam Leffler 		 * timers of all affected vap's.
1795c448998dSSam Leffler 		 */
1796c448998dSSam Leffler 		vap->iv_swbmiss_count = 0;
1797c448998dSSam Leffler 	} else if (vap->iv_swbmiss_count == 0) {
1798b032f27cSSam Leffler 		if (vap->iv_bmiss != NULL)
17995efea30fSAndrew Thompson 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1800e99662a6SSam Leffler 	} else
1801b032f27cSSam Leffler 		vap->iv_swbmiss_count = 0;
1802b032f27cSSam Leffler 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1803b032f27cSSam Leffler 		ieee80211_swbmiss, vap);
18047edb8cf9SSam Leffler }
18057edb8cf9SSam Leffler 
180668e8e04eSSam Leffler /*
1807b032f27cSSam Leffler  * Start an 802.11h channel switch.  We record the parameters,
1808b032f27cSSam Leffler  * mark the operation pending, notify each vap through the
1809b032f27cSSam Leffler  * beacon update mechanism so it can update the beacon frame
1810b032f27cSSam Leffler  * contents, and then switch vap's to CSA state to block outbound
1811b032f27cSSam Leffler  * traffic.  Devices that handle CSA directly can use the state
1812b032f27cSSam Leffler  * switch to do the right thing so long as they call
1813b032f27cSSam Leffler  * ieee80211_csa_completeswitch when it's time to complete the
1814b032f27cSSam Leffler  * channel change.  Devices that depend on the net80211 layer can
1815b032f27cSSam Leffler  * use ieee80211_beacon_update to handle the countdown and the
1816b032f27cSSam Leffler  * channel switch.
1817b032f27cSSam Leffler  */
1818b032f27cSSam Leffler void
1819b032f27cSSam Leffler ieee80211_csa_startswitch(struct ieee80211com *ic,
1820b032f27cSSam Leffler 	struct ieee80211_channel *c, int mode, int count)
1821b032f27cSSam Leffler {
1822b032f27cSSam Leffler 	struct ieee80211vap *vap;
1823b032f27cSSam Leffler 
1824b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1825b032f27cSSam Leffler 
1826b032f27cSSam Leffler 	ic->ic_csa_newchan = c;
1827c70761e6SSam Leffler 	ic->ic_csa_mode = mode;
1828b032f27cSSam Leffler 	ic->ic_csa_count = count;
1829b032f27cSSam Leffler 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1830b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1831b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
183259aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_IBSS ||
183359aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_MBSS)
1834b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1835b032f27cSSam Leffler 		/* switch to CSA state to block outbound traffic */
1836b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN)
1837b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1838b032f27cSSam Leffler 	}
1839b032f27cSSam Leffler 	ieee80211_notify_csa(ic, c, mode, count);
1840b032f27cSSam Leffler }
1841b032f27cSSam Leffler 
1842886bbec1SAdrian Chadd /*
1843886bbec1SAdrian Chadd  * Complete the channel switch by transitioning all CSA VAPs to RUN.
1844886bbec1SAdrian Chadd  * This is called by both the completion and cancellation functions
1845886bbec1SAdrian Chadd  * so each VAP is placed back in the RUN state and can thus transmit.
1846886bbec1SAdrian Chadd  */
1847c70761e6SSam Leffler static void
1848c70761e6SSam Leffler csa_completeswitch(struct ieee80211com *ic)
1849c70761e6SSam Leffler {
1850c70761e6SSam Leffler 	struct ieee80211vap *vap;
1851c70761e6SSam Leffler 
1852c70761e6SSam Leffler 	ic->ic_csa_newchan = NULL;
1853c70761e6SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1854c70761e6SSam Leffler 
1855c70761e6SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1856c70761e6SSam Leffler 		if (vap->iv_state == IEEE80211_S_CSA)
1857c70761e6SSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1858c70761e6SSam Leffler }
1859c70761e6SSam Leffler 
1860b032f27cSSam Leffler /*
1861b032f27cSSam Leffler  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1862b032f27cSSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1863b032f27cSSam Leffler  * so they can again transmit.
1864886bbec1SAdrian Chadd  *
1865886bbec1SAdrian Chadd  * Although this may not be completely correct, update the BSS channel
1866886bbec1SAdrian Chadd  * for each VAP to the newly configured channel. The setcurchan sets
1867886bbec1SAdrian Chadd  * the current operating channel for the interface (so the radio does
1868886bbec1SAdrian Chadd  * switch over) but the VAP BSS isn't updated, leading to incorrectly
1869886bbec1SAdrian Chadd  * reported information via ioctl.
1870b032f27cSSam Leffler  */
1871b032f27cSSam Leffler void
1872b032f27cSSam Leffler ieee80211_csa_completeswitch(struct ieee80211com *ic)
1873b032f27cSSam Leffler {
18746f16ec31SAdrian Chadd 	struct ieee80211vap *vap;
18756f16ec31SAdrian Chadd 
1876b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1877b032f27cSSam Leffler 
1878b032f27cSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1879b032f27cSSam Leffler 
1880b032f27cSSam Leffler 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1881886bbec1SAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1882886bbec1SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_CSA)
1883886bbec1SAdrian Chadd 			vap->iv_bss->ni_chan = ic->ic_curchan;
1884886bbec1SAdrian Chadd 
1885c70761e6SSam Leffler 	csa_completeswitch(ic);
1886c70761e6SSam Leffler }
1887b032f27cSSam Leffler 
1888c70761e6SSam Leffler /*
1889c70761e6SSam Leffler  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1890c70761e6SSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1891c70761e6SSam Leffler  * so they can again transmit.
1892c70761e6SSam Leffler  */
1893c70761e6SSam Leffler void
1894c70761e6SSam Leffler ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1895c70761e6SSam Leffler {
1896c70761e6SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1897c70761e6SSam Leffler 
1898c70761e6SSam Leffler 	csa_completeswitch(ic);
1899b032f27cSSam Leffler }
1900b032f27cSSam Leffler 
1901b032f27cSSam Leffler /*
1902b032f27cSSam Leffler  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1903b032f27cSSam Leffler  * We clear state and move all vap's in CAC state to RUN state.
1904b032f27cSSam Leffler  */
1905b032f27cSSam Leffler void
1906b032f27cSSam Leffler ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1907b032f27cSSam Leffler {
1908b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1909b032f27cSSam Leffler 	struct ieee80211vap *vap;
1910b032f27cSSam Leffler 
1911b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1912b032f27cSSam Leffler 	/*
1913b032f27cSSam Leffler 	 * Complete CAC state change for lead vap first; then
1914b032f27cSSam Leffler 	 * clock all the other vap's waiting.
1915b032f27cSSam Leffler 	 */
1916b032f27cSSam Leffler 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1917b032f27cSSam Leffler 	    ("wrong state %d", vap0->iv_state));
1918b032f27cSSam Leffler 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1919b032f27cSSam Leffler 
1920b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1921e0625c4cSAndriy Voskoboinyk 		if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
1922b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1923b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1924b032f27cSSam Leffler }
1925b032f27cSSam Leffler 
1926b032f27cSSam Leffler /*
1927b032f27cSSam Leffler  * Force all vap's other than the specified vap to the INIT state
1928b032f27cSSam Leffler  * and mark them as waiting for a scan to complete.  These vaps
1929b032f27cSSam Leffler  * will be brought up when the scan completes and the scanning vap
1930b032f27cSSam Leffler  * reaches RUN state by wakeupwaiting.
193168e8e04eSSam Leffler  */
193268e8e04eSSam Leffler static void
1933b032f27cSSam Leffler markwaiting(struct ieee80211vap *vap0)
193468e8e04eSSam Leffler {
1935b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1936b032f27cSSam Leffler 	struct ieee80211vap *vap;
1937b032f27cSSam Leffler 
1938b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1939b032f27cSSam Leffler 
19405efea30fSAndrew Thompson 	/*
19415efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
19425efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
19435efea30fSAndrew Thompson 	 * change task.
19445efea30fSAndrew Thompson 	 */
1945b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1946b032f27cSSam Leffler 		if (vap == vap0)
1947b032f27cSSam Leffler 			continue;
1948b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_INIT) {
19495efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1950b032f27cSSam Leffler 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1951dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1952b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1953b032f27cSSam Leffler 		}
195468e8e04eSSam Leffler 	}
195568e8e04eSSam Leffler }
195668e8e04eSSam Leffler 
1957b032f27cSSam Leffler /*
1958b032f27cSSam Leffler  * Wakeup all vap's waiting for a scan to complete.  This is the
1959b032f27cSSam Leffler  * companion to markwaiting (above) and is used to coordinate
1960b032f27cSSam Leffler  * multiple vaps scanning.
19615efea30fSAndrew Thompson  * This is called from the state taskqueue.
1962b032f27cSSam Leffler  */
1963b032f27cSSam Leffler static void
1964b032f27cSSam Leffler wakeupwaiting(struct ieee80211vap *vap0)
1965b032f27cSSam Leffler {
1966b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1967b032f27cSSam Leffler 	struct ieee80211vap *vap;
1968b032f27cSSam Leffler 
1969b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1970b032f27cSSam Leffler 
19715efea30fSAndrew Thompson 	/*
19725efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
19735efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
19745efea30fSAndrew Thompson 	 * change task.
19755efea30fSAndrew Thompson 	 */
1976b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1977b032f27cSSam Leffler 		if (vap == vap0)
1978b032f27cSSam Leffler 			continue;
1979b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1980b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1981b032f27cSSam Leffler 			/* NB: sta's cannot go INIT->RUN */
19825efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1983b032f27cSSam Leffler 			vap->iv_newstate(vap,
1984b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_STA ?
1985b032f27cSSam Leffler 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1986dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1987b032f27cSSam Leffler 		}
1988b032f27cSSam Leffler 	}
1989b032f27cSSam Leffler }
1990b032f27cSSam Leffler 
1991b032f27cSSam Leffler /*
1992b032f27cSSam Leffler  * Handle post state change work common to all operating modes.
1993b032f27cSSam Leffler  */
1994b032f27cSSam Leffler static void
19955efea30fSAndrew Thompson ieee80211_newstate_cb(void *xvap, int npending)
1996b032f27cSSam Leffler {
19975efea30fSAndrew Thompson 	struct ieee80211vap *vap = xvap;
1998b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
19995efea30fSAndrew Thompson 	enum ieee80211_state nstate, ostate;
20005efea30fSAndrew Thompson 	int arg, rc;
2001b032f27cSSam Leffler 
20025efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
20035efea30fSAndrew Thompson 	nstate = vap->iv_nstate;
20045efea30fSAndrew Thompson 	arg = vap->iv_nstate_arg;
2005b032f27cSSam Leffler 
20065efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
20075efea30fSAndrew Thompson 		/*
20085efea30fSAndrew Thompson 		 * We have been requested to drop back to the INIT before
20095efea30fSAndrew Thompson 		 * proceeding to the new state.
20105efea30fSAndrew Thompson 		 */
2011d13806f4SAndriy Voskoboinyk 		/* Deny any state changes while we are here. */
2012d13806f4SAndriy Voskoboinyk 		vap->iv_nstate = IEEE80211_S_INIT;
2013b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
20145efea30fSAndrew Thompson 		    "%s: %s -> %s arg %d\n", __func__,
20155efea30fSAndrew Thompson 		    ieee80211_state_name[vap->iv_state],
2016d13806f4SAndriy Voskoboinyk 		    ieee80211_state_name[vap->iv_nstate], arg);
2017d13806f4SAndriy Voskoboinyk 		vap->iv_newstate(vap, vap->iv_nstate, 0);
2018dcc56af0SAdrian Chadd 		IEEE80211_LOCK_ASSERT(ic);
2019d13806f4SAndriy Voskoboinyk 		vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
2020d13806f4SAndriy Voskoboinyk 		    IEEE80211_FEXT_STATEWAIT);
2021d13806f4SAndriy Voskoboinyk 		/* enqueue new state transition after cancel_scan() task */
2022d13806f4SAndriy Voskoboinyk 		ieee80211_new_state_locked(vap, nstate, arg);
2023d13806f4SAndriy Voskoboinyk 		goto done;
20245efea30fSAndrew Thompson 	}
20255efea30fSAndrew Thompson 
20265efea30fSAndrew Thompson 	ostate = vap->iv_state;
20275efea30fSAndrew Thompson 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
20285efea30fSAndrew Thompson 		/*
20295efea30fSAndrew Thompson 		 * SCAN was forced; e.g. on beacon miss.  Force other running
20305efea30fSAndrew Thompson 		 * vap's to INIT state and mark them as waiting for the scan to
20315efea30fSAndrew Thompson 		 * complete.  This insures they don't interfere with our
20325efea30fSAndrew Thompson 		 * scanning.  Since we are single threaded the vaps can not
20335efea30fSAndrew Thompson 		 * transition again while we are executing.
20345efea30fSAndrew Thompson 		 *
20355efea30fSAndrew Thompson 		 * XXX not always right, assumes ap follows sta
20365efea30fSAndrew Thompson 		 */
20375efea30fSAndrew Thompson 		markwaiting(vap);
20385efea30fSAndrew Thompson 	}
20395efea30fSAndrew Thompson 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
20405efea30fSAndrew Thompson 	    "%s: %s -> %s arg %d\n", __func__,
20415efea30fSAndrew Thompson 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
20425efea30fSAndrew Thompson 
20435efea30fSAndrew Thompson 	rc = vap->iv_newstate(vap, nstate, arg);
2044dcc56af0SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
20455efea30fSAndrew Thompson 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
20465efea30fSAndrew Thompson 	if (rc != 0) {
20475efea30fSAndrew Thompson 		/* State transition failed */
20485efea30fSAndrew Thompson 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
20495efea30fSAndrew Thompson 		KASSERT(nstate != IEEE80211_S_INIT,
20505efea30fSAndrew Thompson 		    ("INIT state change failed"));
20515efea30fSAndrew Thompson 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
20525efea30fSAndrew Thompson 		    "%s: %s returned error %d\n", __func__,
20535efea30fSAndrew Thompson 		    ieee80211_state_name[nstate], rc);
20545efea30fSAndrew Thompson 		goto done;
20555efea30fSAndrew Thompson 	}
20565efea30fSAndrew Thompson 
20575efea30fSAndrew Thompson 	/* No actual transition, skip post processing */
20585efea30fSAndrew Thompson 	if (ostate == nstate)
20595efea30fSAndrew Thompson 		goto done;
2060b032f27cSSam Leffler 
2061b032f27cSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
2062b032f27cSSam Leffler 		/*
2063b032f27cSSam Leffler 		 * OACTIVE may be set on the vap if the upper layer
2064b032f27cSSam Leffler 		 * tried to transmit (e.g. IPv6 NDP) before we reach
2065b032f27cSSam Leffler 		 * RUN state.  Clear it and restart xmit.
2066b032f27cSSam Leffler 		 *
2067b032f27cSSam Leffler 		 * Note this can also happen as a result of SLEEP->RUN
2068b032f27cSSam Leffler 		 * (i.e. coming out of power save mode).
2069b032f27cSSam Leffler 		 */
2070b032f27cSSam Leffler 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2071a7f31a36SAdrian Chadd 
2072a7f31a36SAdrian Chadd 		/*
2073e7495198SAdrian Chadd 		 * XXX TODO Kick-start a VAP queue - this should be a method!
2074a7f31a36SAdrian Chadd 		 */
2075b032f27cSSam Leffler 
2076b032f27cSSam Leffler 		/* bring up any vaps waiting on us */
2077b032f27cSSam Leffler 		wakeupwaiting(vap);
2078b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
2079b032f27cSSam Leffler 		/*
2080b032f27cSSam Leffler 		 * Flush the scan cache if we did the last scan (XXX?)
2081b032f27cSSam Leffler 		 * and flush any frames on send queues from this vap.
2082b032f27cSSam Leffler 		 * Note the mgt q is used only for legacy drivers and
2083b032f27cSSam Leffler 		 * will go away shortly.
2084b032f27cSSam Leffler 		 */
2085b032f27cSSam Leffler 		ieee80211_scan_flush(vap);
2086b032f27cSSam Leffler 
2087e7495198SAdrian Chadd 		/*
2088e7495198SAdrian Chadd 		 * XXX TODO: ic/vap queue flush
2089e7495198SAdrian Chadd 		 */
2090b032f27cSSam Leffler 	}
20915efea30fSAndrew Thompson done:
20925efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
2093b032f27cSSam Leffler }
2094b032f27cSSam Leffler 
2095b032f27cSSam Leffler /*
2096b032f27cSSam Leffler  * Public interface for initiating a state machine change.
2097b032f27cSSam Leffler  * This routine single-threads the request and coordinates
2098b032f27cSSam Leffler  * the scheduling of multiple vaps for the purpose of selecting
2099b032f27cSSam Leffler  * an operating channel.  Specifically the following scenarios
2100b032f27cSSam Leffler  * are handled:
2101b032f27cSSam Leffler  * o only one vap can be selecting a channel so on transition to
2102b032f27cSSam Leffler  *   SCAN state if another vap is already scanning then
2103b032f27cSSam Leffler  *   mark the caller for later processing and return without
2104b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
2105b032f27cSSam Leffler  * o only one vap can be doing CAC of a channel so on transition to
2106b032f27cSSam Leffler  *   CAC state if another vap is already scanning for radar then
2107b032f27cSSam Leffler  *   mark the caller for later processing and return without
2108b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
2109b032f27cSSam Leffler  * o if another vap is already running when a request is made
2110b032f27cSSam Leffler  *   to SCAN then an operating channel has been chosen; bypass
2111b032f27cSSam Leffler  *   the scan and just join the channel
2112b032f27cSSam Leffler  *
2113b032f27cSSam Leffler  * Note that the state change call is done through the iv_newstate
2114b032f27cSSam Leffler  * method pointer so any driver routine gets invoked.  The driver
2115b032f27cSSam Leffler  * will normally call back into operating mode-specific
2116b032f27cSSam Leffler  * ieee80211_newstate routines (below) unless it needs to completely
2117b032f27cSSam Leffler  * bypass the state machine (e.g. because the firmware has it's
2118b032f27cSSam Leffler  * own idea how things should work).  Bypassing the net80211 layer
2119b032f27cSSam Leffler  * is usually a mistake and indicates lack of proper integration
2120b032f27cSSam Leffler  * with the net80211 layer.
2121b032f27cSSam Leffler  */
2122e94527beSAdrian Chadd int
2123b032f27cSSam Leffler ieee80211_new_state_locked(struct ieee80211vap *vap,
2124b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
21258a1b9b6aSSam Leffler {
2126b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2127b032f27cSSam Leffler 	struct ieee80211vap *vp;
2128a11c9a5cSSam Leffler 	enum ieee80211_state ostate;
21295efea30fSAndrew Thompson 	int nrunning, nscanning;
21301a1e1d21SSam Leffler 
2131b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2132b032f27cSSam Leffler 
21335efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2134d13806f4SAndriy Voskoboinyk 		if (vap->iv_nstate == IEEE80211_S_INIT ||
2135d13806f4SAndriy Voskoboinyk 		    ((vap->iv_state == IEEE80211_S_INIT ||
2136d13806f4SAndriy Voskoboinyk 		    (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2137d13806f4SAndriy Voskoboinyk 		    vap->iv_nstate == IEEE80211_S_SCAN &&
2138d13806f4SAndriy Voskoboinyk 		    nstate > IEEE80211_S_SCAN)) {
21395efea30fSAndrew Thompson 			/*
2140d13806f4SAndriy Voskoboinyk 			 * XXX The vap is being stopped/started,
2141d13806f4SAndriy Voskoboinyk 			 * do not allow any other state changes
2142d13806f4SAndriy Voskoboinyk 			 * until this is completed.
21435efea30fSAndrew Thompson 			 */
2144d13806f4SAndriy Voskoboinyk 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2145d13806f4SAndriy Voskoboinyk 			    "%s: %s -> %s (%s) transition discarded\n",
2146d13806f4SAndriy Voskoboinyk 			    __func__,
2147d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[vap->iv_state],
2148d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[nstate],
2149d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[vap->iv_nstate]);
21505efea30fSAndrew Thompson 			return -1;
21518ee6f90aSAndrew Thompson 		} else if (vap->iv_state != vap->iv_nstate) {
21525efea30fSAndrew Thompson #if 0
21535efea30fSAndrew Thompson 			/* Warn if the previous state hasn't completed. */
21545efea30fSAndrew Thompson 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
21555efea30fSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n", __func__,
21565efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_state],
21575efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
21585efea30fSAndrew Thompson #else
21595efea30fSAndrew Thompson 			/* XXX temporarily enable to identify issues */
21608ee6f90aSAndrew Thompson 			if_printf(vap->iv_ifp,
21618ee6f90aSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n",
21625efea30fSAndrew Thompson 			    __func__, ieee80211_state_name[vap->iv_state],
21635efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
21645efea30fSAndrew Thompson #endif
21655efea30fSAndrew Thompson 		}
21668ee6f90aSAndrew Thompson 	}
21675efea30fSAndrew Thompson 
2168b032f27cSSam Leffler 	nrunning = nscanning = 0;
2169b032f27cSSam Leffler 	/* XXX can track this state instead of calculating */
2170b032f27cSSam Leffler 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2171b032f27cSSam Leffler 		if (vp != vap) {
2172b032f27cSSam Leffler 			if (vp->iv_state >= IEEE80211_S_RUN)
2173b032f27cSSam Leffler 				nrunning++;
2174b032f27cSSam Leffler 			/* XXX doesn't handle bg scan */
2175b032f27cSSam Leffler 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
2176b032f27cSSam Leffler 			else if (vp->iv_state > IEEE80211_S_INIT)
2177b032f27cSSam Leffler 				nscanning++;
2178b032f27cSSam Leffler 		}
2179b032f27cSSam Leffler 	}
2180b032f27cSSam Leffler 	ostate = vap->iv_state;
2181b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2182b032f27cSSam Leffler 	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
2183b032f27cSSam Leffler 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
2184b032f27cSSam Leffler 	    nrunning, nscanning);
21851a1e1d21SSam Leffler 	switch (nstate) {
21861a1e1d21SSam Leffler 	case IEEE80211_S_SCAN:
2187b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT) {
21881a1e1d21SSam Leffler 			/*
2189b032f27cSSam Leffler 			 * INIT -> SCAN happens on initial bringup.
21901a1e1d21SSam Leffler 			 */
2191b032f27cSSam Leffler 			KASSERT(!(nscanning && nrunning),
2192b032f27cSSam Leffler 			    ("%d scanning and %d running", nscanning, nrunning));
2193b032f27cSSam Leffler 			if (nscanning) {
219468e8e04eSSam Leffler 				/*
2195b032f27cSSam Leffler 				 * Someone is scanning, defer our state
2196b032f27cSSam Leffler 				 * change until the work has completed.
219768e8e04eSSam Leffler 				 */
2198b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2199b032f27cSSam Leffler 				    "%s: defer %s -> %s\n",
2200b032f27cSSam Leffler 				    __func__, ieee80211_state_name[ostate],
2201b032f27cSSam Leffler 				    ieee80211_state_name[nstate]);
2202b032f27cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
22035efea30fSAndrew Thompson 				return 0;
220468e8e04eSSam Leffler 			}
2205b032f27cSSam Leffler 			if (nrunning) {
220668e8e04eSSam Leffler 				/*
2207b032f27cSSam Leffler 				 * Someone is operating; just join the channel
2208b032f27cSSam Leffler 				 * they have chosen.
220968e8e04eSSam Leffler 				 */
2210b032f27cSSam Leffler 				/* XXX kill arg? */
2211b032f27cSSam Leffler 				/* XXX check each opmode, adhoc? */
2212b032f27cSSam Leffler 				if (vap->iv_opmode == IEEE80211_M_STA)
2213b032f27cSSam Leffler 					nstate = IEEE80211_S_SCAN;
22141a1e1d21SSam Leffler 				else
2215b032f27cSSam Leffler 					nstate = IEEE80211_S_RUN;
2216b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
2217b032f27cSSam Leffler 				if (nstate != IEEE80211_S_SCAN) {
2218b032f27cSSam Leffler 					IEEE80211_DPRINTF(vap,
2219b032f27cSSam Leffler 					    IEEE80211_MSG_STATE,
2220b032f27cSSam Leffler 					    "%s: override, now %s -> %s\n",
2221b032f27cSSam Leffler 					    __func__,
2222b032f27cSSam Leffler 					    ieee80211_state_name[ostate],
2223b032f27cSSam Leffler 					    ieee80211_state_name[nstate]);
22241a1e1d21SSam Leffler 				}
22258a1b9b6aSSam Leffler #endif
222668e8e04eSSam Leffler 			}
2227b032f27cSSam Leffler 		}
22281a1e1d21SSam Leffler 		break;
2229b032f27cSSam Leffler 	case IEEE80211_S_RUN:
2230b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS &&
2231b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2232b032f27cSSam Leffler 		    nscanning) {
2233b032f27cSSam Leffler 			/*
2234b032f27cSSam Leffler 			 * Legacy WDS with someone else scanning; don't
2235b032f27cSSam Leffler 			 * go online until that completes as we should
2236b032f27cSSam Leffler 			 * follow the other vap to the channel they choose.
2237b032f27cSSam Leffler 			 */
2238b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2239b032f27cSSam Leffler 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
2240b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2241b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2242b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
22435efea30fSAndrew Thompson 			return 0;
2244b032f27cSSam Leffler 		}
2245b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2246b032f27cSSam Leffler 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2247b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2248b032f27cSSam Leffler 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2249b032f27cSSam Leffler 			/*
2250b032f27cSSam Leffler 			 * This is a DFS channel, transition to CAC state
2251b032f27cSSam Leffler 			 * instead of RUN.  This allows us to initiate
2252b032f27cSSam Leffler 			 * Channel Availability Check (CAC) as specified
2253b032f27cSSam Leffler 			 * by 11h/DFS.
2254b032f27cSSam Leffler 			 */
2255b032f27cSSam Leffler 			nstate = IEEE80211_S_CAC;
2256b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2257b032f27cSSam Leffler 			     "%s: override %s -> %s (DFS)\n", __func__,
2258b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2259b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2260b032f27cSSam Leffler 		}
2261b032f27cSSam Leffler 		break;
2262b032f27cSSam Leffler 	case IEEE80211_S_INIT:
2263b016f58cSAndrew Thompson 		/* cancel any scan in progress */
2264b016f58cSAndrew Thompson 		ieee80211_cancel_scan(vap);
2265b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT ) {
2266b032f27cSSam Leffler 			/* XXX don't believe this */
2267b032f27cSSam Leffler 			/* INIT -> INIT. nothing to do */
2268b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2269b032f27cSSam Leffler 		}
2270b032f27cSSam Leffler 		/* fall thru... */
227114fb6b8fSSam Leffler 	default:
227214fb6b8fSSam Leffler 		break;
22731a1e1d21SSam Leffler 	}
22745efea30fSAndrew Thompson 	/* defer the state change to a thread */
22755efea30fSAndrew Thompson 	vap->iv_nstate = nstate;
22765efea30fSAndrew Thompson 	vap->iv_nstate_arg = arg;
22775efea30fSAndrew Thompson 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
22785efea30fSAndrew Thompson 	ieee80211_runtask(ic, &vap->iv_nstate_task);
22795efea30fSAndrew Thompson 	return EINPROGRESS;
22808a1b9b6aSSam Leffler }
2281b032f27cSSam Leffler 
2282b032f27cSSam Leffler int
2283b032f27cSSam Leffler ieee80211_new_state(struct ieee80211vap *vap,
2284b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
2285b032f27cSSam Leffler {
2286b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2287b032f27cSSam Leffler 	int rc;
2288b032f27cSSam Leffler 
2289b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2290b032f27cSSam Leffler 	rc = ieee80211_new_state_locked(vap, nstate, arg);
2291b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2292b032f27cSSam Leffler 	return rc;
22931a1e1d21SSam Leffler }
2294