xref: /freebsd/sys/net80211/ieee80211_proto.c (revision 713db49d06deee90dd358b2e4b9ca05368a5eaf6)
11a1e1d21SSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 /*
321a1e1d21SSam Leffler  * IEEE 802.11 protocol support.
331a1e1d21SSam Leffler  */
341a1e1d21SSam Leffler 
351a1e1d21SSam Leffler #include "opt_inet.h"
36b032f27cSSam Leffler #include "opt_wlan.h"
371a1e1d21SSam Leffler 
381a1e1d21SSam Leffler #include <sys/param.h>
398a1b9b6aSSam Leffler #include <sys/systm.h>
408ec07310SGleb Smirnoff #include <sys/kernel.h>
418ec07310SGleb Smirnoff #include <sys/malloc.h>
421a1e1d21SSam Leffler 
438a1b9b6aSSam Leffler #include <sys/socket.h>
44b032f27cSSam Leffler #include <sys/sockio.h>
451a1e1d21SSam Leffler 
461a1e1d21SSam Leffler #include <net/if.h>
4776039bc8SGleb Smirnoff #include <net/if_var.h>
481a1e1d21SSam Leffler #include <net/if_media.h>
493d0d5b21SJustin Hibbits #include <net/if_private.h>
508a1b9b6aSSam Leffler #include <net/ethernet.h>		/* XXX for ether_sprintf */
511a1e1d21SSam Leffler 
521a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
53b032f27cSSam Leffler #include <net80211/ieee80211_adhoc.h>
54b032f27cSSam Leffler #include <net80211/ieee80211_sta.h>
55b032f27cSSam Leffler #include <net80211/ieee80211_hostap.h>
56b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
5859aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
5959aa14a9SRui Paulo #endif
60b032f27cSSam Leffler #include <net80211/ieee80211_monitor.h>
61b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
621a1e1d21SSam Leffler 
638a1b9b6aSSam Leffler /* XXX tunables */
648a1b9b6aSSam Leffler #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
658a1b9b6aSSam Leffler #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
661a1e1d21SSam Leffler 
674357a5d1SAndriy Voskoboinyk const char *mgt_subtype_name[] = {
681a1e1d21SSam Leffler 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
69665d5ae9SAndriy Voskoboinyk 	"probe_req",	"probe_resp",	"timing_adv",	"reserved#7",
701a1e1d21SSam Leffler 	"beacon",	"atim",		"disassoc",	"auth",
7196283082SBernhard Schmidt 	"deauth",	"action",	"action_noack",	"reserved#15"
721a1e1d21SSam Leffler };
734357a5d1SAndriy Voskoboinyk const char *ctl_subtype_name[] = {
748a1b9b6aSSam Leffler 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
75665d5ae9SAndriy Voskoboinyk 	"reserved#4",	"reserved#5",	"reserved#6",	"control_wrap",
76665d5ae9SAndriy Voskoboinyk 	"bar",		"ba",		"ps_poll",	"rts",
778a1b9b6aSSam Leffler 	"cts",		"ack",		"cf_end",	"cf_end_ack"
788a1b9b6aSSam Leffler };
7949aa47d6SSam Leffler const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
8049aa47d6SSam Leffler 	"IBSS",		/* IEEE80211_M_IBSS */
8149aa47d6SSam Leffler 	"STA",		/* IEEE80211_M_STA */
82b032f27cSSam Leffler 	"WDS",		/* IEEE80211_M_WDS */
8349aa47d6SSam Leffler 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
8449aa47d6SSam Leffler 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
8559aa14a9SRui Paulo 	"MONITOR",	/* IEEE80211_M_MONITOR */
8659aa14a9SRui Paulo 	"MBSS"		/* IEEE80211_M_MBSS */
8749aa47d6SSam Leffler };
88a11c9a5cSSam Leffler const char *ieee80211_state_name[IEEE80211_S_MAX] = {
89a11c9a5cSSam Leffler 	"INIT",		/* IEEE80211_S_INIT */
90a11c9a5cSSam Leffler 	"SCAN",		/* IEEE80211_S_SCAN */
91a11c9a5cSSam Leffler 	"AUTH",		/* IEEE80211_S_AUTH */
92a11c9a5cSSam Leffler 	"ASSOC",	/* IEEE80211_S_ASSOC */
9314fb6b8fSSam Leffler 	"CAC",		/* IEEE80211_S_CAC */
9414fb6b8fSSam Leffler 	"RUN",		/* IEEE80211_S_RUN */
9514fb6b8fSSam Leffler 	"CSA",		/* IEEE80211_S_CSA */
9614fb6b8fSSam Leffler 	"SLEEP",	/* IEEE80211_S_SLEEP */
97a11c9a5cSSam Leffler };
988a1b9b6aSSam Leffler const char *ieee80211_wme_acnames[] = {
998a1b9b6aSSam Leffler 	"WME_AC_BE",
1008a1b9b6aSSam Leffler 	"WME_AC_BK",
1018a1b9b6aSSam Leffler 	"WME_AC_VI",
1028a1b9b6aSSam Leffler 	"WME_AC_VO",
1038a1b9b6aSSam Leffler 	"WME_UPSD",
1048a1b9b6aSSam Leffler };
105a11c9a5cSSam Leffler 
106d72d72d3SAndriy Voskoboinyk /*
107d72d72d3SAndriy Voskoboinyk  * Reason code descriptions were (mostly) obtained from
108d72d72d3SAndriy Voskoboinyk  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
109d72d72d3SAndriy Voskoboinyk  */
110d72d72d3SAndriy Voskoboinyk const char *
ieee80211_reason_to_string(uint16_t reason)111d72d72d3SAndriy Voskoboinyk ieee80211_reason_to_string(uint16_t reason)
112d72d72d3SAndriy Voskoboinyk {
113d72d72d3SAndriy Voskoboinyk 	switch (reason) {
114d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSPECIFIED:
115d72d72d3SAndriy Voskoboinyk 		return ("unspecified");
116d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AUTH_EXPIRE:
117d72d72d3SAndriy Voskoboinyk 		return ("previous authentication is expired");
118d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AUTH_LEAVE:
119d72d72d3SAndriy Voskoboinyk 		return ("sending STA is leaving/has left IBSS or ESS");
120d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_EXPIRE:
121d72d72d3SAndriy Voskoboinyk 		return ("disassociated due to inactivity");
122d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_TOOMANY:
123d72d72d3SAndriy Voskoboinyk 		return ("too many associated STAs");
124d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_NOT_AUTHED:
125d72d72d3SAndriy Voskoboinyk 		return ("class 2 frame received from nonauthenticated STA");
126d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_NOT_ASSOCED:
127d72d72d3SAndriy Voskoboinyk 		return ("class 3 frame received from nonassociated STA");
128d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_LEAVE:
129d72d72d3SAndriy Voskoboinyk 		return ("sending STA is leaving/has left BSS");
130d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
131d72d72d3SAndriy Voskoboinyk 		return ("STA requesting (re)association is not authenticated");
132d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
133d72d72d3SAndriy Voskoboinyk 		return ("information in the Power Capability element is "
134d72d72d3SAndriy Voskoboinyk 			"unacceptable");
135d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
136d72d72d3SAndriy Voskoboinyk 		return ("information in the Supported Channels element is "
137d72d72d3SAndriy Voskoboinyk 			"unacceptable");
138d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_IE_INVALID:
139d72d72d3SAndriy Voskoboinyk 		return ("invalid element");
140d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MIC_FAILURE:
141d72d72d3SAndriy Voskoboinyk 		return ("MIC failure");
142d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
143d72d72d3SAndriy Voskoboinyk 		return ("4-Way handshake timeout");
144d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
145d72d72d3SAndriy Voskoboinyk 		return ("group key update timeout");
146d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
147d72d72d3SAndriy Voskoboinyk 		return ("element in 4-Way handshake different from "
148d72d72d3SAndriy Voskoboinyk 			"(re)association request/probe response/beacon frame");
149d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_GROUP_CIPHER_INVALID:
150d72d72d3SAndriy Voskoboinyk 		return ("invalid group cipher");
151d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
152d72d72d3SAndriy Voskoboinyk 		return ("invalid pairwise cipher");
153d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_AKMP_INVALID:
154d72d72d3SAndriy Voskoboinyk 		return ("invalid AKMP");
155d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
156d72d72d3SAndriy Voskoboinyk 		return ("unsupported version in RSN IE");
157d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_INVALID_RSN_IE_CAP:
158d72d72d3SAndriy Voskoboinyk 		return ("invalid capabilities in RSN IE");
159d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_802_1X_AUTH_FAILED:
160d72d72d3SAndriy Voskoboinyk 		return ("IEEE 802.1X authentication failed");
161d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
162d72d72d3SAndriy Voskoboinyk 		return ("cipher suite rejected because of the security "
163d72d72d3SAndriy Voskoboinyk 			"policy");
164d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_UNSPECIFIED_QOS:
165d72d72d3SAndriy Voskoboinyk 		return ("unspecified (QoS-related)");
166d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_INSUFFICIENT_BW:
167d72d72d3SAndriy Voskoboinyk 		return ("QoS AP lacks sufficient bandwidth for this QoS STA");
168d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_TOOMANY_FRAMES:
169d72d72d3SAndriy Voskoboinyk 		return ("too many frames need to be acknowledged");
170d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_OUTSIDE_TXOP:
171d72d72d3SAndriy Voskoboinyk 		return ("STA is transmitting outside the limits of its TXOPs");
172d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_LEAVING_QBSS:
173d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (the STA is "
174d72d72d3SAndriy Voskoboinyk 			"resetting/leaving the BSS)");
175d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_BAD_MECHANISM:
176d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (it does not want to use "
177d72d72d3SAndriy Voskoboinyk 			"the mechanism)");
178d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_SETUP_NEEDED:
179d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (setup is required for the "
180d72d72d3SAndriy Voskoboinyk 			"used mechanism)");
181d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_TIMEOUT:
182d72d72d3SAndriy Voskoboinyk 		return ("requested from peer STA (timeout)");
183d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_PEER_LINK_CANCELED:
184d72d72d3SAndriy Voskoboinyk 		return ("SME cancels the mesh peering instance (not related "
185d72d72d3SAndriy Voskoboinyk 			"to the maximum number of peer mesh STAs)");
186d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAX_PEERS:
187d72d72d3SAndriy Voskoboinyk 		return ("maximum number of peer mesh STAs was reached");
188d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CPVIOLATION:
189d72d72d3SAndriy Voskoboinyk 		return ("the received information violates the Mesh "
190d72d72d3SAndriy Voskoboinyk 			"Configuration policy configured in the mesh STA "
191d72d72d3SAndriy Voskoboinyk 			"profile");
192d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CLOSE_RCVD:
193d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA has received a Mesh Peering Close "
194d72d72d3SAndriy Voskoboinyk 			"message requesting to close the mesh peering");
195d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAX_RETRIES:
196d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
197d72d72d3SAndriy Voskoboinyk 			"Peering Open messages, without receiving a Mesh "
198d72d72d3SAndriy Voskoboinyk 			"Peering Confirm message");
199d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
200d72d72d3SAndriy Voskoboinyk 		return ("the confirmTimer for the mesh peering instance times "
201d72d72d3SAndriy Voskoboinyk 			"out");
202d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INVALID_GTK:
203d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA fails to unwrap the GTK or the values "
204d72d72d3SAndriy Voskoboinyk 			"in the wrapped contents do not match");
205d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INCONS_PARAMS:
206d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA receives inconsistent information about "
207d72d72d3SAndriy Voskoboinyk 			"the mesh parameters between Mesh Peering Management "
208d72d72d3SAndriy Voskoboinyk 			"frames");
209d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_INVALID_SECURITY:
210d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA fails the authenticated mesh peering "
211d72d72d3SAndriy Voskoboinyk 			"exchange because due to failure in selecting "
212d72d72d3SAndriy Voskoboinyk 			"pairwise/group ciphersuite");
213d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_NO_PROXY:
214d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA does not have proxy information for "
215d72d72d3SAndriy Voskoboinyk 			"this external destination");
216d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_NO_FI:
217d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA does not have forwarding information "
218d72d72d3SAndriy Voskoboinyk 			"for this destination");
219d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
220d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA determines that the link to the next "
221d72d72d3SAndriy Voskoboinyk 			"hop of an active path in its forwarding information "
222d72d72d3SAndriy Voskoboinyk 			"is no longer usable");
223d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
224d72d72d3SAndriy Voskoboinyk 		return ("the MAC address of the STA already exists in the "
225d72d72d3SAndriy Voskoboinyk 			"mesh BSS");
226d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
227d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA performs channel switch to meet "
228d72d72d3SAndriy Voskoboinyk 			"regulatory requirements");
229d72d72d3SAndriy Voskoboinyk 	case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
230d72d72d3SAndriy Voskoboinyk 		return ("the mesh STA performs channel switch with "
231d72d72d3SAndriy Voskoboinyk 			"unspecified reason");
232d72d72d3SAndriy Voskoboinyk 	default:
233d72d72d3SAndriy Voskoboinyk 		return ("reserved/unknown");
234d72d72d3SAndriy Voskoboinyk 	}
235d72d72d3SAndriy Voskoboinyk }
236d72d72d3SAndriy Voskoboinyk 
2375efea30fSAndrew Thompson static void beacon_miss(void *, int);
2385efea30fSAndrew Thompson static void beacon_swmiss(void *, int);
239b032f27cSSam Leffler static void parent_updown(void *, int);
2405efea30fSAndrew Thompson static void update_mcast(void *, int);
2415efea30fSAndrew Thompson static void update_promisc(void *, int);
2425efea30fSAndrew Thompson static void update_channel(void *, int);
243b94299c4SAdrian Chadd static void update_chw(void *, int);
244e3e94c96SAdrian Chadd static void vap_update_wme(void *, int);
245d20ff6e6SAdrian Chadd static void vap_update_slot(void *, int);
2464061c639SAndriy Voskoboinyk static void restart_vaps(void *, int);
247f1481c8dSAdrian Chadd static void vap_update_erp_protmode(void *, int);
248f1481c8dSAdrian Chadd static void vap_update_preamble(void *, int);
249f1481c8dSAdrian Chadd static void vap_update_ht_protmode(void *, int);
2505efea30fSAndrew Thompson static void ieee80211_newstate_cb(void *, int);
25191b4225aSBjoern A. Zeeb static struct ieee80211_node *vap_update_bss(struct ieee80211vap *,
25291b4225aSBjoern A. Zeeb     struct ieee80211_node *);
2531a1e1d21SSam Leffler 
254b032f27cSSam Leffler static int
null_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)255b032f27cSSam Leffler null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
256b032f27cSSam Leffler 	const struct ieee80211_bpf_params *params)
257b105a069SSam Leffler {
258b032f27cSSam Leffler 
259c8f5794eSGleb Smirnoff 	ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
260b032f27cSSam Leffler 	m_freem(m);
261b032f27cSSam Leffler 	return ENETDOWN;
262b105a069SSam Leffler }
263b105a069SSam Leffler 
2641a1e1d21SSam Leffler void
ieee80211_proto_attach(struct ieee80211com * ic)2658a1b9b6aSSam Leffler ieee80211_proto_attach(struct ieee80211com *ic)
2661a1e1d21SSam Leffler {
2677a79cebfSGleb Smirnoff 	uint8_t hdrlen;
2681a1e1d21SSam Leffler 
269b032f27cSSam Leffler 	/* override the 802.3 setting */
2707a79cebfSGleb Smirnoff 	hdrlen = ic->ic_headroom
271b032f27cSSam Leffler 		+ sizeof(struct ieee80211_qosframe_addr4)
272b032f27cSSam Leffler 		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
273b032f27cSSam Leffler 		+ IEEE80211_WEP_EXTIVLEN;
274b032f27cSSam Leffler 	/* XXX no way to recalculate on ifdetach */
275c414347bSGleb Smirnoff 	max_linkhdr_grow(ALIGN(hdrlen));
276f1481c8dSAdrian Chadd 	//ic->ic_protmode = IEEE80211_PROT_CTSONLY;
277b032f27cSSam Leffler 
2787a79cebfSGleb Smirnoff 	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
2795efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
2805efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
2815efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
2825efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
283b94299c4SAdrian Chadd 	TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
2844061c639SAndriy Voskoboinyk 	TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
2858a1b9b6aSSam Leffler 
2868a1b9b6aSSam Leffler 	ic->ic_wme.wme_hipri_switch_hysteresis =
2878a1b9b6aSSam Leffler 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
2881a1e1d21SSam Leffler 
2891a1e1d21SSam Leffler 	/* initialize management frame handlers */
2901a1e1d21SSam Leffler 	ic->ic_send_mgmt = ieee80211_send_mgmt;
291b032f27cSSam Leffler 	ic->ic_raw_xmit = null_raw_xmit;
292b032f27cSSam Leffler 
293b032f27cSSam Leffler 	ieee80211_adhoc_attach(ic);
294b032f27cSSam Leffler 	ieee80211_sta_attach(ic);
295b032f27cSSam Leffler 	ieee80211_wds_attach(ic);
296b032f27cSSam Leffler 	ieee80211_hostap_attach(ic);
29759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
29859aa14a9SRui Paulo 	ieee80211_mesh_attach(ic);
29959aa14a9SRui Paulo #endif
300b032f27cSSam Leffler 	ieee80211_monitor_attach(ic);
3011a1e1d21SSam Leffler }
3021a1e1d21SSam Leffler 
3031a1e1d21SSam Leffler void
ieee80211_proto_detach(struct ieee80211com * ic)3048a1b9b6aSSam Leffler ieee80211_proto_detach(struct ieee80211com *ic)
3051a1e1d21SSam Leffler {
306b032f27cSSam Leffler 	ieee80211_monitor_detach(ic);
30759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
30859aa14a9SRui Paulo 	ieee80211_mesh_detach(ic);
30959aa14a9SRui Paulo #endif
310b032f27cSSam Leffler 	ieee80211_hostap_detach(ic);
311b032f27cSSam Leffler 	ieee80211_wds_detach(ic);
312b032f27cSSam Leffler 	ieee80211_adhoc_detach(ic);
313b032f27cSSam Leffler 	ieee80211_sta_detach(ic);
314b032f27cSSam Leffler }
3158a1b9b6aSSam Leffler 
316b032f27cSSam Leffler static void
null_update_beacon(struct ieee80211vap * vap,int item)317b032f27cSSam Leffler null_update_beacon(struct ieee80211vap *vap, int item)
318b032f27cSSam Leffler {
319b032f27cSSam Leffler }
320b032f27cSSam Leffler 
321b032f27cSSam Leffler void
ieee80211_proto_vattach(struct ieee80211vap * vap)322b032f27cSSam Leffler ieee80211_proto_vattach(struct ieee80211vap *vap)
323b032f27cSSam Leffler {
324b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
325b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
326b032f27cSSam Leffler 	int i;
327b032f27cSSam Leffler 
328b032f27cSSam Leffler 	/* override the 802.3 setting */
3297a79cebfSGleb Smirnoff 	ifp->if_hdrlen = ic->ic_headroom
3307a79cebfSGleb Smirnoff                 + sizeof(struct ieee80211_qosframe_addr4)
3317a79cebfSGleb Smirnoff                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
3327a79cebfSGleb Smirnoff                 + IEEE80211_WEP_EXTIVLEN;
333b032f27cSSam Leffler 
334b032f27cSSam Leffler 	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
335b032f27cSSam Leffler 	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
336b032f27cSSam Leffler 	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
33723401900SAdrian Chadd 	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
338fd90e2edSJung-uk Kim 	callout_init(&vap->iv_mgtsend, 1);
339*713db49dSBjoern A. Zeeb 	for (i = 0; i < NET80211_IV_NSTATE_NUM; i++)
340*713db49dSBjoern A. Zeeb 		TASK_INIT(&vap->iv_nstate_task[i], 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);
343d20ff6e6SAdrian Chadd 	TASK_INIT(&vap->iv_slot_task, 0, vap_update_slot, vap);
344f1481c8dSAdrian Chadd 	TASK_INIT(&vap->iv_erp_protmode_task, 0, vap_update_erp_protmode, vap);
345f1481c8dSAdrian Chadd 	TASK_INIT(&vap->iv_ht_protmode_task, 0, vap_update_ht_protmode, vap);
346f1481c8dSAdrian Chadd 	TASK_INIT(&vap->iv_preamble_task, 0, vap_update_preamble, vap);
347b032f27cSSam Leffler 	/*
348b032f27cSSam Leffler 	 * Install default tx rate handling: no fixed rate, lowest
349b032f27cSSam Leffler 	 * supported rate for mgmt and multicast frames.  Default
350b032f27cSSam Leffler 	 * max retry count.  These settings can be changed by the
351b032f27cSSam Leffler 	 * driver and/or user applications.
352b032f27cSSam Leffler 	 */
353047db6b3SSam Leffler 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
3541c4cb651SAndriy Voskoboinyk 		if (isclr(ic->ic_modecaps, i))
3551c4cb651SAndriy Voskoboinyk 			continue;
3561c4cb651SAndriy Voskoboinyk 
357b032f27cSSam Leffler 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
358b032f27cSSam Leffler 
359b032f27cSSam Leffler 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
360338452c9SAdrian Chadd 
361338452c9SAdrian Chadd 		/*
362338452c9SAdrian Chadd 		 * Setting the management rate to MCS 0 assumes that the
363338452c9SAdrian Chadd 		 * BSS Basic rate set is empty and the BSS Basic MCS set
364338452c9SAdrian Chadd 		 * is not.
365338452c9SAdrian Chadd 		 *
366338452c9SAdrian Chadd 		 * Since we're not checking this, default to the lowest
367338452c9SAdrian Chadd 		 * defined rate for this mode.
368338452c9SAdrian Chadd 		 *
369338452c9SAdrian Chadd 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
370338452c9SAdrian Chadd 		 * some MCS management traffic (eg BA response frames.)
371338452c9SAdrian Chadd 		 *
372338452c9SAdrian Chadd 		 * See also: 9.6.0 of the 802.11n-2009 specification.
373338452c9SAdrian Chadd 		 */
374338452c9SAdrian Chadd #ifdef	NOTYET
375047db6b3SSam Leffler 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
376047db6b3SSam Leffler 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
377047db6b3SSam Leffler 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
378047db6b3SSam Leffler 		} else {
379b032f27cSSam Leffler 			vap->iv_txparms[i].mgmtrate =
380b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
381b032f27cSSam Leffler 			vap->iv_txparms[i].mcastrate =
382b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
383b032f27cSSam Leffler 		}
384338452c9SAdrian Chadd #endif
385338452c9SAdrian Chadd 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
386338452c9SAdrian Chadd 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
387b032f27cSSam Leffler 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
388b032f27cSSam Leffler 	}
389b032f27cSSam Leffler 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
390b032f27cSSam Leffler 
391b032f27cSSam Leffler 	vap->iv_update_beacon = null_update_beacon;
392b032f27cSSam Leffler 	vap->iv_deliver_data = ieee80211_deliver_data;
393f1481c8dSAdrian Chadd 	vap->iv_protmode = IEEE80211_PROT_CTSONLY;
39491b4225aSBjoern A. Zeeb 	vap->iv_update_bss = vap_update_bss;
395b032f27cSSam Leffler 
396b032f27cSSam Leffler 	/* attach support for operating mode */
397b032f27cSSam Leffler 	ic->ic_vattach[vap->iv_opmode](vap);
398b032f27cSSam Leffler }
399b032f27cSSam Leffler 
400b032f27cSSam Leffler void
ieee80211_proto_vdetach(struct ieee80211vap * vap)401b032f27cSSam Leffler ieee80211_proto_vdetach(struct ieee80211vap *vap)
402b032f27cSSam Leffler {
403b032f27cSSam Leffler #define	FREEAPPIE(ie) do { \
404b032f27cSSam Leffler 	if (ie != NULL) \
405b9b53389SAdrian Chadd 		IEEE80211_FREE(ie, M_80211_NODE_IE); \
406b032f27cSSam Leffler } while (0)
407b032f27cSSam Leffler 	/*
408b032f27cSSam Leffler 	 * Detach operating mode module.
409b032f27cSSam Leffler 	 */
410b032f27cSSam Leffler 	if (vap->iv_opdetach != NULL)
411b032f27cSSam Leffler 		vap->iv_opdetach(vap);
4128a1b9b6aSSam Leffler 	/*
4138a1b9b6aSSam Leffler 	 * This should not be needed as we detach when reseting
4148a1b9b6aSSam Leffler 	 * the state but be conservative here since the
4158a1b9b6aSSam Leffler 	 * authenticator may do things like spawn kernel threads.
4168a1b9b6aSSam Leffler 	 */
417b032f27cSSam Leffler 	if (vap->iv_auth->ia_detach != NULL)
418b032f27cSSam Leffler 		vap->iv_auth->ia_detach(vap);
4198a1b9b6aSSam Leffler 	/*
4208a1b9b6aSSam Leffler 	 * Detach any ACL'ator.
4218a1b9b6aSSam Leffler 	 */
422b032f27cSSam Leffler 	if (vap->iv_acl != NULL)
423b032f27cSSam Leffler 		vap->iv_acl->iac_detach(vap);
424b032f27cSSam Leffler 
425b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_beacon);
426b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_probereq);
427b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_proberesp);
428b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocreq);
429b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocresp);
430b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_wpa);
431b032f27cSSam Leffler #undef FREEAPPIE
4328a1b9b6aSSam Leffler }
4338a1b9b6aSSam Leffler 
4348a1b9b6aSSam Leffler /*
4358a1b9b6aSSam Leffler  * Simple-minded authenticator module support.
4368a1b9b6aSSam Leffler  */
4378a1b9b6aSSam Leffler 
4388a1b9b6aSSam Leffler #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
4398a1b9b6aSSam Leffler /* XXX well-known names */
4408a1b9b6aSSam Leffler static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
4418a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
4428a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
4438a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
4448a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
4458a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
4468a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
4478a1b9b6aSSam Leffler };
4488a1b9b6aSSam Leffler static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
4498a1b9b6aSSam Leffler 
4508a1b9b6aSSam Leffler static const struct ieee80211_authenticator auth_internal = {
4518a1b9b6aSSam Leffler 	.ia_name		= "wlan_internal",
4528a1b9b6aSSam Leffler 	.ia_attach		= NULL,
4538a1b9b6aSSam Leffler 	.ia_detach		= NULL,
4548a1b9b6aSSam Leffler 	.ia_node_join		= NULL,
4558a1b9b6aSSam Leffler 	.ia_node_leave		= NULL,
4568a1b9b6aSSam Leffler };
4578a1b9b6aSSam Leffler 
4588a1b9b6aSSam Leffler /*
4598a1b9b6aSSam Leffler  * Setup internal authenticators once; they are never unregistered.
4608a1b9b6aSSam Leffler  */
4618a1b9b6aSSam Leffler static void
ieee80211_auth_setup(void)4628a1b9b6aSSam Leffler ieee80211_auth_setup(void)
4638a1b9b6aSSam Leffler {
4648a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
4658a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
4668a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
4678a1b9b6aSSam Leffler }
4688a1b9b6aSSam Leffler SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
4698a1b9b6aSSam Leffler 
4708a1b9b6aSSam Leffler const struct ieee80211_authenticator *
ieee80211_authenticator_get(int auth)4718a1b9b6aSSam Leffler ieee80211_authenticator_get(int auth)
4728a1b9b6aSSam Leffler {
4738a1b9b6aSSam Leffler 	if (auth >= IEEE80211_AUTH_MAX)
4748a1b9b6aSSam Leffler 		return NULL;
4758a1b9b6aSSam Leffler 	if (authenticators[auth] == NULL)
4768a1b9b6aSSam Leffler 		ieee80211_load_module(auth_modnames[auth]);
4778a1b9b6aSSam Leffler 	return authenticators[auth];
4781a1e1d21SSam Leffler }
4791a1e1d21SSam Leffler 
4801a1e1d21SSam Leffler void
ieee80211_authenticator_register(int type,const struct ieee80211_authenticator * auth)4818a1b9b6aSSam Leffler ieee80211_authenticator_register(int type,
4828a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth)
4831a1e1d21SSam Leffler {
4848a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
4858a1b9b6aSSam Leffler 		return;
4868a1b9b6aSSam Leffler 	authenticators[type] = auth;
4878a1b9b6aSSam Leffler }
4888a1b9b6aSSam Leffler 
4898a1b9b6aSSam Leffler void
ieee80211_authenticator_unregister(int type)4908a1b9b6aSSam Leffler ieee80211_authenticator_unregister(int type)
4918a1b9b6aSSam Leffler {
4928a1b9b6aSSam Leffler 
4938a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
4948a1b9b6aSSam Leffler 		return;
4958a1b9b6aSSam Leffler 	authenticators[type] = NULL;
4968a1b9b6aSSam Leffler }
4978a1b9b6aSSam Leffler 
4988a1b9b6aSSam Leffler /*
4998a1b9b6aSSam Leffler  * Very simple-minded ACL module support.
5008a1b9b6aSSam Leffler  */
5018a1b9b6aSSam Leffler /* XXX just one for now */
5028a1b9b6aSSam Leffler static	const struct ieee80211_aclator *acl = NULL;
5038a1b9b6aSSam Leffler 
5048a1b9b6aSSam Leffler void
ieee80211_aclator_register(const struct ieee80211_aclator * iac)5058a1b9b6aSSam Leffler ieee80211_aclator_register(const struct ieee80211_aclator *iac)
5068a1b9b6aSSam Leffler {
5078a1b9b6aSSam Leffler 	printf("wlan: %s acl policy registered\n", iac->iac_name);
5088a1b9b6aSSam Leffler 	acl = iac;
5098a1b9b6aSSam Leffler }
5108a1b9b6aSSam Leffler 
5118a1b9b6aSSam Leffler void
ieee80211_aclator_unregister(const struct ieee80211_aclator * iac)5128a1b9b6aSSam Leffler ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
5138a1b9b6aSSam Leffler {
5148a1b9b6aSSam Leffler 	if (acl == iac)
5158a1b9b6aSSam Leffler 		acl = NULL;
5168a1b9b6aSSam Leffler 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
5178a1b9b6aSSam Leffler }
5188a1b9b6aSSam Leffler 
5198a1b9b6aSSam Leffler const struct ieee80211_aclator *
ieee80211_aclator_get(const char * name)5208a1b9b6aSSam Leffler ieee80211_aclator_get(const char *name)
5218a1b9b6aSSam Leffler {
5228a1b9b6aSSam Leffler 	if (acl == NULL)
5238a1b9b6aSSam Leffler 		ieee80211_load_module("wlan_acl");
5248a1b9b6aSSam Leffler 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
5258a1b9b6aSSam Leffler }
5268a1b9b6aSSam Leffler 
5278a1b9b6aSSam Leffler void
ieee80211_print_essid(const uint8_t * essid,int len)52868e8e04eSSam Leffler ieee80211_print_essid(const uint8_t *essid, int len)
5298a1b9b6aSSam Leffler {
53068e8e04eSSam Leffler 	const uint8_t *p;
5311a1e1d21SSam Leffler 	int i;
5321a1e1d21SSam Leffler 
5331a1e1d21SSam Leffler 	if (len > IEEE80211_NWID_LEN)
5341a1e1d21SSam Leffler 		len = IEEE80211_NWID_LEN;
5351a1e1d21SSam Leffler 	/* determine printable or not */
5361a1e1d21SSam Leffler 	for (i = 0, p = essid; i < len; i++, p++) {
5371a1e1d21SSam Leffler 		if (*p < ' ' || *p > 0x7e)
5381a1e1d21SSam Leffler 			break;
5391a1e1d21SSam Leffler 	}
5401a1e1d21SSam Leffler 	if (i == len) {
5411a1e1d21SSam Leffler 		printf("\"");
5421a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
5431a1e1d21SSam Leffler 			printf("%c", *p);
5441a1e1d21SSam Leffler 		printf("\"");
5451a1e1d21SSam Leffler 	} else {
5461a1e1d21SSam Leffler 		printf("0x");
5471a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
5481a1e1d21SSam Leffler 			printf("%02x", *p);
5491a1e1d21SSam Leffler 	}
5501a1e1d21SSam Leffler }
5511a1e1d21SSam Leffler 
5521a1e1d21SSam Leffler void
ieee80211_dump_pkt(struct ieee80211com * ic,const uint8_t * buf,int len,int rate,int rssi)55368e8e04eSSam Leffler ieee80211_dump_pkt(struct ieee80211com *ic,
55468e8e04eSSam Leffler 	const uint8_t *buf, int len, int rate, int rssi)
5551a1e1d21SSam Leffler {
5568a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh;
5571a1e1d21SSam Leffler 	int i;
5581a1e1d21SSam Leffler 
5598a1b9b6aSSam Leffler 	wh = (const struct ieee80211_frame *)buf;
5601a1e1d21SSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
5611a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
5621a1e1d21SSam Leffler 		printf("NODS %s", ether_sprintf(wh->i_addr2));
5631a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
5641a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr3));
5651a1e1d21SSam Leffler 		break;
5661a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
5671a1e1d21SSam Leffler 		printf("TODS %s", ether_sprintf(wh->i_addr2));
5681a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
5691a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr1));
5701a1e1d21SSam Leffler 		break;
5711a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
5721a1e1d21SSam Leffler 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
5731a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
5741a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr2));
5751a1e1d21SSam Leffler 		break;
5761a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
57768e8e04eSSam Leffler 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
5781a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
5791a1e1d21SSam Leffler 		printf("(%s", ether_sprintf(wh->i_addr2));
5801a1e1d21SSam Leffler 		printf("->%s)", ether_sprintf(wh->i_addr1));
5811a1e1d21SSam Leffler 		break;
5821a1e1d21SSam Leffler 	}
5831a1e1d21SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
5841a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
5851a1e1d21SSam Leffler 		printf(" data");
5861a1e1d21SSam Leffler 		break;
5871a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
5884357a5d1SAndriy Voskoboinyk 		printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
5891a1e1d21SSam Leffler 		break;
5901a1e1d21SSam Leffler 	default:
5911a1e1d21SSam Leffler 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
5921a1e1d21SSam Leffler 		break;
5931a1e1d21SSam Leffler 	}
59468e8e04eSSam Leffler 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
59568e8e04eSSam Leffler 		const struct ieee80211_qosframe *qwh =
59668e8e04eSSam Leffler 			(const struct ieee80211_qosframe *)buf;
59768e8e04eSSam Leffler 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
59868e8e04eSSam Leffler 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
59968e8e04eSSam Leffler 	}
6002889cbe2SAdrian Chadd 	if (IEEE80211_IS_PROTECTED(wh)) {
60168e8e04eSSam Leffler 		int off;
60268e8e04eSSam Leffler 
60368e8e04eSSam Leffler 		off = ieee80211_anyhdrspace(ic, wh);
60468e8e04eSSam Leffler 		printf(" WEP [IV %.02x %.02x %.02x",
60568e8e04eSSam Leffler 			buf[off+0], buf[off+1], buf[off+2]);
60668e8e04eSSam Leffler 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
60768e8e04eSSam Leffler 			printf(" %.02x %.02x %.02x",
60868e8e04eSSam Leffler 				buf[off+4], buf[off+5], buf[off+6]);
60968e8e04eSSam Leffler 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
6108a1b9b6aSSam Leffler 	}
6111a1e1d21SSam Leffler 	if (rate >= 0)
6121a1e1d21SSam Leffler 		printf(" %dM", rate / 2);
6131a1e1d21SSam Leffler 	if (rssi >= 0)
6141a1e1d21SSam Leffler 		printf(" +%d", rssi);
6151a1e1d21SSam Leffler 	printf("\n");
6161a1e1d21SSam Leffler 	if (len > 0) {
6171a1e1d21SSam Leffler 		for (i = 0; i < len; i++) {
6181a1e1d21SSam Leffler 			if ((i & 1) == 0)
6191a1e1d21SSam Leffler 				printf(" ");
6201a1e1d21SSam Leffler 			printf("%02x", buf[i]);
6211a1e1d21SSam Leffler 		}
6221a1e1d21SSam Leffler 		printf("\n");
6231a1e1d21SSam Leffler 	}
6241a1e1d21SSam Leffler }
6251a1e1d21SSam Leffler 
62679edaebfSSam Leffler static __inline int
findrix(const struct ieee80211_rateset * rs,int r)62779edaebfSSam Leffler findrix(const struct ieee80211_rateset *rs, int r)
62879edaebfSSam Leffler {
62979edaebfSSam Leffler 	int i;
63079edaebfSSam Leffler 
63179edaebfSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
63279edaebfSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
63379edaebfSSam Leffler 			return i;
63479edaebfSSam Leffler 	return -1;
63579edaebfSSam Leffler }
63679edaebfSSam Leffler 
6371a1e1d21SSam Leffler int
ieee80211_fix_rate(struct ieee80211_node * ni,struct ieee80211_rateset * nrs,int flags)63870e28b9aSSam Leffler ieee80211_fix_rate(struct ieee80211_node *ni,
63970e28b9aSSam Leffler 	struct ieee80211_rateset *nrs, int flags)
6401a1e1d21SSam Leffler {
641b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6427d77cd53SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
64379edaebfSSam Leffler 	int i, j, rix, error;
644b032f27cSSam Leffler 	int okrate, badrate, fixedrate, ucastrate;
64541b3c790SSam Leffler 	const struct ieee80211_rateset *srs;
64668e8e04eSSam Leffler 	uint8_t r;
6471a1e1d21SSam Leffler 
6481a1e1d21SSam Leffler 	error = 0;
64968e8e04eSSam Leffler 	okrate = badrate = 0;
650b032f27cSSam Leffler 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
651b032f27cSSam Leffler 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
652b032f27cSSam Leffler 		/*
653b032f27cSSam Leffler 		 * Workaround awkwardness with fixed rate.  We are called
654b032f27cSSam Leffler 		 * to check both the legacy rate set and the HT rate set
655b032f27cSSam Leffler 		 * but we must apply any legacy fixed rate check only to the
656b032f27cSSam Leffler 		 * legacy rate set and vice versa.  We cannot tell what type
657b032f27cSSam Leffler 		 * of rate set we've been given (legacy or HT) but we can
658b032f27cSSam Leffler 		 * distinguish the fixed rate type (MCS have 0x80 set).
659b032f27cSSam Leffler 		 * So to deal with this the caller communicates whether to
660b032f27cSSam Leffler 		 * check MCS or legacy rate using the flags and we use the
661b032f27cSSam Leffler 		 * type of any fixed rate to avoid applying an MCS to a
662b032f27cSSam Leffler 		 * legacy rate and vice versa.
663b032f27cSSam Leffler 		 */
664b032f27cSSam Leffler 		if (ucastrate & 0x80) {
665b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFRATE)
666b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFRATE;
667b032f27cSSam Leffler 		} else if ((ucastrate & 0x80) == 0) {
668b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFMCS)
669b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFMCS;
670b032f27cSSam Leffler 		}
671b032f27cSSam Leffler 		/* NB: required to make MCS match below work */
672b032f27cSSam Leffler 		ucastrate &= IEEE80211_RATE_VAL;
673b032f27cSSam Leffler 	}
67468e8e04eSSam Leffler 	fixedrate = IEEE80211_FIXED_RATE_NONE;
675b032f27cSSam Leffler 	/*
676b032f27cSSam Leffler 	 * XXX we are called to process both MCS and legacy rates;
677b032f27cSSam Leffler 	 * we must use the appropriate basic rate set or chaos will
678b032f27cSSam Leffler 	 * ensue; for now callers that want MCS must supply
679b032f27cSSam Leffler 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
680b032f27cSSam Leffler 	 * function so there are two variants, one for MCS and one
681b032f27cSSam Leffler 	 * for legacy rates.
682b032f27cSSam Leffler 	 */
683b032f27cSSam Leffler 	if (flags & IEEE80211_F_DOBRS)
684b032f27cSSam Leffler 		srs = (const struct ieee80211_rateset *)
685b032f27cSSam Leffler 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
686b032f27cSSam Leffler 	else
68741b3c790SSam Leffler 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
688ef39d4beSSam Leffler 	for (i = 0; i < nrs->rs_nrates; ) {
6891a1e1d21SSam Leffler 		if (flags & IEEE80211_F_DOSORT) {
6901a1e1d21SSam Leffler 			/*
6911a1e1d21SSam Leffler 			 * Sort rates.
6921a1e1d21SSam Leffler 			 */
6931a1e1d21SSam Leffler 			for (j = i + 1; j < nrs->rs_nrates; j++) {
6940ebe104fSAdrian Chadd 				if (IEEE80211_RV(nrs->rs_rates[i]) >
6950ebe104fSAdrian Chadd 				    IEEE80211_RV(nrs->rs_rates[j])) {
6961a1e1d21SSam Leffler 					r = nrs->rs_rates[i];
6971a1e1d21SSam Leffler 					nrs->rs_rates[i] = nrs->rs_rates[j];
6981a1e1d21SSam Leffler 					nrs->rs_rates[j] = r;
6991a1e1d21SSam Leffler 				}
7001a1e1d21SSam Leffler 			}
7011a1e1d21SSam Leffler 		}
7021a1e1d21SSam Leffler 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
7031a1e1d21SSam Leffler 		badrate = r;
7041a1e1d21SSam Leffler 		/*
70568e8e04eSSam Leffler 		 * Check for fixed rate.
7061a1e1d21SSam Leffler 		 */
707b032f27cSSam Leffler 		if (r == ucastrate)
7088a1b9b6aSSam Leffler 			fixedrate = r;
7091a1e1d21SSam Leffler 		/*
7101a1e1d21SSam Leffler 		 * Check against supported rates.
7111a1e1d21SSam Leffler 		 */
71279edaebfSSam Leffler 		rix = findrix(srs, r);
71379edaebfSSam Leffler 		if (flags & IEEE80211_F_DONEGO) {
71479edaebfSSam Leffler 			if (rix < 0) {
715ef39d4beSSam Leffler 				/*
716ef39d4beSSam Leffler 				 * A rate in the node's rate set is not
717ef39d4beSSam Leffler 				 * supported.  If this is a basic rate and we
71879edaebfSSam Leffler 				 * are operating as a STA then this is an error.
719ef39d4beSSam Leffler 				 * Otherwise we just discard/ignore the rate.
720ef39d4beSSam Leffler 				 */
72179edaebfSSam Leffler 				if ((flags & IEEE80211_F_JOIN) &&
722ef39d4beSSam Leffler 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
7231a1e1d21SSam Leffler 					error++;
72479edaebfSSam Leffler 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
72579edaebfSSam Leffler 				/*
72679edaebfSSam Leffler 				 * Overwrite with the supported rate
72779edaebfSSam Leffler 				 * value so any basic rate bit is set.
72879edaebfSSam Leffler 				 */
72979edaebfSSam Leffler 				nrs->rs_rates[i] = srs->rs_rates[rix];
7301a1e1d21SSam Leffler 			}
7311a1e1d21SSam Leffler 		}
73279edaebfSSam Leffler 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
7331a1e1d21SSam Leffler 			/*
7341a1e1d21SSam Leffler 			 * Delete unacceptable rates.
7351a1e1d21SSam Leffler 			 */
7361a1e1d21SSam Leffler 			nrs->rs_nrates--;
7371a1e1d21SSam Leffler 			for (j = i; j < nrs->rs_nrates; j++)
7381a1e1d21SSam Leffler 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
7391a1e1d21SSam Leffler 			nrs->rs_rates[j] = 0;
7401a1e1d21SSam Leffler 			continue;
7411a1e1d21SSam Leffler 		}
74279edaebfSSam Leffler 		if (rix >= 0)
7431a1e1d21SSam Leffler 			okrate = nrs->rs_rates[i];
7441a1e1d21SSam Leffler 		i++;
7451a1e1d21SSam Leffler 	}
7468a1b9b6aSSam Leffler 	if (okrate == 0 || error != 0 ||
747b032f27cSSam Leffler 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
748b032f27cSSam Leffler 	     fixedrate != ucastrate)) {
749b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
750b032f27cSSam Leffler 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
751b032f27cSSam Leffler 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
7521a1e1d21SSam Leffler 		return badrate | IEEE80211_RATE_BASIC;
753b032f27cSSam Leffler 	} else
7540ebe104fSAdrian Chadd 		return IEEE80211_RV(okrate);
7551a1e1d21SSam Leffler }
7561a1e1d21SSam Leffler 
7578a1b9b6aSSam Leffler /*
7588a1b9b6aSSam Leffler  * Reset 11g-related state.
759d20ff6e6SAdrian Chadd  *
760d20ff6e6SAdrian Chadd  * This is for per-VAP ERP/11g state.
761d20ff6e6SAdrian Chadd  *
762d20ff6e6SAdrian Chadd  * Eventually everything in ieee80211_reset_erp() will be
763d20ff6e6SAdrian Chadd  * per-VAP and in here.
764d20ff6e6SAdrian Chadd  */
765d20ff6e6SAdrian Chadd void
ieee80211_vap_reset_erp(struct ieee80211vap * vap)766d20ff6e6SAdrian Chadd ieee80211_vap_reset_erp(struct ieee80211vap *vap)
767d20ff6e6SAdrian Chadd {
768d20ff6e6SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
769d20ff6e6SAdrian Chadd 
770f1481c8dSAdrian Chadd 	vap->iv_nonerpsta = 0;
771f1481c8dSAdrian Chadd 	vap->iv_longslotsta = 0;
772f1481c8dSAdrian Chadd 
773f1481c8dSAdrian Chadd 	vap->iv_flags &= ~IEEE80211_F_USEPROT;
774f1481c8dSAdrian Chadd 	/*
775f1481c8dSAdrian Chadd 	 * Set short preamble and ERP barker-preamble flags.
776f1481c8dSAdrian Chadd 	 */
777f1481c8dSAdrian Chadd 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
778f1481c8dSAdrian Chadd 	    (vap->iv_caps & IEEE80211_C_SHPREAMBLE)) {
779f1481c8dSAdrian Chadd 		vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
780f1481c8dSAdrian Chadd 		vap->iv_flags &= ~IEEE80211_F_USEBARKER;
781f1481c8dSAdrian Chadd 	} else {
782f1481c8dSAdrian Chadd 		vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
783f1481c8dSAdrian Chadd 		vap->iv_flags |= IEEE80211_F_USEBARKER;
784f1481c8dSAdrian Chadd 	}
785f1481c8dSAdrian Chadd 
786d20ff6e6SAdrian Chadd 	/*
787d20ff6e6SAdrian Chadd 	 * Short slot time is enabled only when operating in 11g
788d20ff6e6SAdrian Chadd 	 * and not in an IBSS.  We must also honor whether or not
789d20ff6e6SAdrian Chadd 	 * the driver is capable of doing it.
790d20ff6e6SAdrian Chadd 	 */
791d20ff6e6SAdrian Chadd 	ieee80211_vap_set_shortslottime(vap,
792d20ff6e6SAdrian Chadd 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
793d20ff6e6SAdrian Chadd 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
794d20ff6e6SAdrian Chadd 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
795d20ff6e6SAdrian Chadd 		vap->iv_opmode == IEEE80211_M_HOSTAP &&
796d20ff6e6SAdrian Chadd 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
797d20ff6e6SAdrian Chadd }
798d20ff6e6SAdrian Chadd 
799d20ff6e6SAdrian Chadd /*
800d20ff6e6SAdrian Chadd  * Reset 11g-related state.
801f1481c8dSAdrian Chadd  *
802f1481c8dSAdrian Chadd  * Note this resets the global state and a caller should schedule
803f1481c8dSAdrian Chadd  * a re-check of all the VAPs after setup to update said state.
8048a1b9b6aSSam Leffler  */
8058a1b9b6aSSam Leffler void
ieee80211_reset_erp(struct ieee80211com * ic)8068a1b9b6aSSam Leffler ieee80211_reset_erp(struct ieee80211com *ic)
8071a1e1d21SSam Leffler {
808f1481c8dSAdrian Chadd #if 0
8098a1b9b6aSSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
8108a1b9b6aSSam Leffler 	/*
8118a1b9b6aSSam Leffler 	 * Set short preamble and ERP barker-preamble flags.
8128a1b9b6aSSam Leffler 	 */
81368e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
8148a1b9b6aSSam Leffler 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
8158a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
8168a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
8178a1b9b6aSSam Leffler 	} else {
8188a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
8198a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_USEBARKER;
8208a1b9b6aSSam Leffler 	}
821f1481c8dSAdrian Chadd #endif
822f1481c8dSAdrian Chadd 	/* XXX TODO: schedule a new per-VAP ERP calculation */
8238a1b9b6aSSam Leffler }
8248a1b9b6aSSam Leffler 
82591b4225aSBjoern A. Zeeb static struct ieee80211_node *
vap_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)82691b4225aSBjoern A. Zeeb vap_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
82791b4225aSBjoern A. Zeeb {
82891b4225aSBjoern A. Zeeb 	struct ieee80211_node *obss;
82991b4225aSBjoern A. Zeeb 
83049619f73SBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
83149619f73SBjoern A. Zeeb 
83291b4225aSBjoern A. Zeeb 	obss = vap->iv_bss;
83391b4225aSBjoern A. Zeeb 	vap->iv_bss = ni;
83491b4225aSBjoern A. Zeeb 
83591b4225aSBjoern A. Zeeb 	return (obss);
83691b4225aSBjoern A. Zeeb }
83791b4225aSBjoern A. Zeeb 
8388a1b9b6aSSam Leffler /*
839d20ff6e6SAdrian Chadd  * Deferred slot time update.
840d20ff6e6SAdrian Chadd  *
841d20ff6e6SAdrian Chadd  * For per-VAP slot time configuration, call the VAP
842d20ff6e6SAdrian Chadd  * method if the VAP requires it.  Otherwise, just call the
843d20ff6e6SAdrian Chadd  * older global method.
844d20ff6e6SAdrian Chadd  *
845d20ff6e6SAdrian Chadd  * If the per-VAP method is called then it's expected that
846d20ff6e6SAdrian Chadd  * the driver/firmware will take care of turning the per-VAP
847d20ff6e6SAdrian Chadd  * flags into slot time configuration.
848d20ff6e6SAdrian Chadd  *
849d20ff6e6SAdrian Chadd  * If the per-VAP method is not called then the global flags will be
850d20ff6e6SAdrian Chadd  * flipped into sync with the VAPs; ic_flags IEEE80211_F_SHSLOT will
851d20ff6e6SAdrian Chadd  * be set only if all of the vaps will have it set.
852f1481c8dSAdrian Chadd  *
853f1481c8dSAdrian Chadd  * Look at the comments for vap_update_erp_protmode() for more
854f1481c8dSAdrian Chadd  * background; this assumes all VAPs are on the same channel.
855d20ff6e6SAdrian Chadd  */
856d20ff6e6SAdrian Chadd static void
vap_update_slot(void * arg,int npending)857d20ff6e6SAdrian Chadd vap_update_slot(void *arg, int npending)
858d20ff6e6SAdrian Chadd {
859d20ff6e6SAdrian Chadd 	struct ieee80211vap *vap = arg;
860d20ff6e6SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
861d20ff6e6SAdrian Chadd 	struct ieee80211vap *iv;
862d20ff6e6SAdrian Chadd 	int num_shslot = 0, num_lgslot = 0;
863d20ff6e6SAdrian Chadd 
864d20ff6e6SAdrian Chadd 	/*
865d20ff6e6SAdrian Chadd 	 * Per-VAP path - we've already had the flags updated;
866d20ff6e6SAdrian Chadd 	 * so just notify the driver and move on.
867d20ff6e6SAdrian Chadd 	 */
868d20ff6e6SAdrian Chadd 	if (vap->iv_updateslot != NULL) {
869d20ff6e6SAdrian Chadd 		vap->iv_updateslot(vap);
870d20ff6e6SAdrian Chadd 		return;
871d20ff6e6SAdrian Chadd 	}
872d20ff6e6SAdrian Chadd 
873d20ff6e6SAdrian Chadd 	/*
874d20ff6e6SAdrian Chadd 	 * Iterate over all of the VAP flags to update the
875d20ff6e6SAdrian Chadd 	 * global flag.
876d20ff6e6SAdrian Chadd 	 *
877d20ff6e6SAdrian Chadd 	 * If all vaps have short slot enabled then flip on
878d20ff6e6SAdrian Chadd 	 * short slot.  If any vap has it disabled then
879d20ff6e6SAdrian Chadd 	 * we leave it globally disabled.  This should provide
880d20ff6e6SAdrian Chadd 	 * correct behaviour in a multi-BSS scenario where
881d20ff6e6SAdrian Chadd 	 * at least one VAP has short slot disabled for some
882d20ff6e6SAdrian Chadd 	 * reason.
883d20ff6e6SAdrian Chadd 	 */
884d20ff6e6SAdrian Chadd 	IEEE80211_LOCK(ic);
885d20ff6e6SAdrian Chadd 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
886d20ff6e6SAdrian Chadd 		if (iv->iv_flags & IEEE80211_F_SHSLOT)
887d20ff6e6SAdrian Chadd 			num_shslot++;
888d20ff6e6SAdrian Chadd 		else
889d20ff6e6SAdrian Chadd 			num_lgslot++;
890d20ff6e6SAdrian Chadd 	}
891d20ff6e6SAdrian Chadd 
892d20ff6e6SAdrian Chadd 	/*
893d20ff6e6SAdrian Chadd 	 * It looks backwards but - if the number of short slot VAPs
894d20ff6e6SAdrian Chadd 	 * is zero then we're not short slot.  Else, we have one
895d20ff6e6SAdrian Chadd 	 * or more short slot VAPs and we're checking to see if ANY
896d20ff6e6SAdrian Chadd 	 * of them have short slot disabled.
897d20ff6e6SAdrian Chadd 	 */
898d20ff6e6SAdrian Chadd 	if (num_shslot == 0)
899d20ff6e6SAdrian Chadd 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
900d20ff6e6SAdrian Chadd 	else if (num_lgslot == 0)
901d20ff6e6SAdrian Chadd 		ic->ic_flags |= IEEE80211_F_SHSLOT;
902f1481c8dSAdrian Chadd 	IEEE80211_UNLOCK(ic);
903d20ff6e6SAdrian Chadd 
904d20ff6e6SAdrian Chadd 	/*
905d20ff6e6SAdrian Chadd 	 * Call the driver with our new global slot time flags.
906d20ff6e6SAdrian Chadd 	 */
907c3739eb6SAdrian Chadd 	if (ic->ic_updateslot != NULL)
908d20ff6e6SAdrian Chadd 		ic->ic_updateslot(ic);
909d20ff6e6SAdrian Chadd }
910d20ff6e6SAdrian Chadd 
911d20ff6e6SAdrian Chadd /*
912f1481c8dSAdrian Chadd  * Deferred ERP protmode update.
913f1481c8dSAdrian Chadd  *
914f1481c8dSAdrian Chadd  * This currently calculates the global ERP protection mode flag
915f1481c8dSAdrian Chadd  * based on each of the VAPs.  Any VAP with it enabled is enough
916f1481c8dSAdrian Chadd  * for the global flag to be enabled.  All VAPs with it disabled
917f1481c8dSAdrian Chadd  * is enough for it to be disabled.
918f1481c8dSAdrian Chadd  *
919f1481c8dSAdrian Chadd  * This may make sense right now for the supported hardware where
920f1481c8dSAdrian Chadd  * net80211 is controlling the single channel configuration, but
921f1481c8dSAdrian Chadd  * offload firmware that's doing channel changes (eg off-channel
922f1481c8dSAdrian Chadd  * TDLS, off-channel STA, off-channel P2P STA/AP) may get some
923f1481c8dSAdrian Chadd  * silly looking flag updates.
924f1481c8dSAdrian Chadd  *
925f1481c8dSAdrian Chadd  * Ideally the protection mode calculation is done based on the
926f1481c8dSAdrian Chadd  * channel, and all VAPs using that channel will inherit it.
927f1481c8dSAdrian Chadd  * But until that's what net80211 does, this wil have to do.
928f1481c8dSAdrian Chadd  */
929f1481c8dSAdrian Chadd static void
vap_update_erp_protmode(void * arg,int npending)930f1481c8dSAdrian Chadd vap_update_erp_protmode(void *arg, int npending)
931f1481c8dSAdrian Chadd {
932f1481c8dSAdrian Chadd 	struct ieee80211vap *vap = arg;
933f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
934f1481c8dSAdrian Chadd 	struct ieee80211vap *iv;
935f1481c8dSAdrian Chadd 	int enable_protmode = 0;
936f1481c8dSAdrian Chadd 	int non_erp_present = 0;
937f1481c8dSAdrian Chadd 
938f1481c8dSAdrian Chadd 	/*
939f1481c8dSAdrian Chadd 	 * Iterate over all of the VAPs to calculate the overlapping
940f1481c8dSAdrian Chadd 	 * ERP protection mode configuration and ERP present math.
941f1481c8dSAdrian Chadd 	 *
942f1481c8dSAdrian Chadd 	 * For now we assume that if a driver can handle this per-VAP
943f1481c8dSAdrian Chadd 	 * then it'll ignore the ic->ic_protmode variant and instead
944f1481c8dSAdrian Chadd 	 * will look at the vap related flags.
945f1481c8dSAdrian Chadd 	 */
946f1481c8dSAdrian Chadd 	IEEE80211_LOCK(ic);
947f1481c8dSAdrian Chadd 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
948f1481c8dSAdrian Chadd 		if (iv->iv_flags & IEEE80211_F_USEPROT)
949f1481c8dSAdrian Chadd 			enable_protmode = 1;
950f1481c8dSAdrian Chadd 		if (iv->iv_flags_ext & IEEE80211_FEXT_NONERP_PR)
951f1481c8dSAdrian Chadd 			non_erp_present = 1;
952f1481c8dSAdrian Chadd 	}
953f1481c8dSAdrian Chadd 
954f1481c8dSAdrian Chadd 	if (enable_protmode)
955f1481c8dSAdrian Chadd 		ic->ic_flags |= IEEE80211_F_USEPROT;
956f1481c8dSAdrian Chadd 	else
957f1481c8dSAdrian Chadd 		ic->ic_flags &= ~IEEE80211_F_USEPROT;
958f1481c8dSAdrian Chadd 
959f1481c8dSAdrian Chadd 	if (non_erp_present)
960f1481c8dSAdrian Chadd 		ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
961f1481c8dSAdrian Chadd 	else
962f1481c8dSAdrian Chadd 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
963f1481c8dSAdrian Chadd 
964f1481c8dSAdrian Chadd 	/* Beacon update on all VAPs */
965f1481c8dSAdrian Chadd 	ieee80211_notify_erp_locked(ic);
966f1481c8dSAdrian Chadd 
967f1481c8dSAdrian Chadd 	IEEE80211_UNLOCK(ic);
968f1481c8dSAdrian Chadd 
969f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
970f1481c8dSAdrian Chadd 	    "%s: called; enable_protmode=%d, non_erp_present=%d\n",
971f1481c8dSAdrian Chadd 	    __func__, enable_protmode, non_erp_present);
972f1481c8dSAdrian Chadd 
973f1481c8dSAdrian Chadd 	/*
974f1481c8dSAdrian Chadd 	 * Now that the global configuration flags are calculated,
975f1481c8dSAdrian Chadd 	 * notify the VAP about its configuration.
976f1481c8dSAdrian Chadd 	 *
977f1481c8dSAdrian Chadd 	 * The global flags will be used when assembling ERP IEs
978f1481c8dSAdrian Chadd 	 * for multi-VAP operation, even if it's on a different
979f1481c8dSAdrian Chadd 	 * channel.  Yes, that's going to need fixing in the
980f1481c8dSAdrian Chadd 	 * future.
981f1481c8dSAdrian Chadd 	 */
982f1481c8dSAdrian Chadd 	if (vap->iv_erp_protmode_update != NULL)
983f1481c8dSAdrian Chadd 		vap->iv_erp_protmode_update(vap);
984f1481c8dSAdrian Chadd }
985f1481c8dSAdrian Chadd 
986f1481c8dSAdrian Chadd /*
987f1481c8dSAdrian Chadd  * Deferred ERP short preamble/barker update.
988f1481c8dSAdrian Chadd  *
989f1481c8dSAdrian Chadd  * All VAPs need to use short preamble for it to be globally
990f1481c8dSAdrian Chadd  * enabled or not.
991f1481c8dSAdrian Chadd  *
992f1481c8dSAdrian Chadd  * Look at the comments for vap_update_erp_protmode() for more
993f1481c8dSAdrian Chadd  * background; this assumes all VAPs are on the same channel.
994f1481c8dSAdrian Chadd  */
995f1481c8dSAdrian Chadd static void
vap_update_preamble(void * arg,int npending)996f1481c8dSAdrian Chadd vap_update_preamble(void *arg, int npending)
997f1481c8dSAdrian Chadd {
998f1481c8dSAdrian Chadd 	struct ieee80211vap *vap = arg;
999f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1000f1481c8dSAdrian Chadd 	struct ieee80211vap *iv;
1001f1481c8dSAdrian Chadd 	int barker_count = 0, short_preamble_count = 0, count = 0;
1002f1481c8dSAdrian Chadd 
1003f1481c8dSAdrian Chadd 	/*
1004f1481c8dSAdrian Chadd 	 * Iterate over all of the VAPs to calculate the overlapping
1005f1481c8dSAdrian Chadd 	 * short or long preamble configuration.
1006f1481c8dSAdrian Chadd 	 *
1007f1481c8dSAdrian Chadd 	 * For now we assume that if a driver can handle this per-VAP
1008f1481c8dSAdrian Chadd 	 * then it'll ignore the ic->ic_flags variant and instead
1009f1481c8dSAdrian Chadd 	 * will look at the vap related flags.
1010f1481c8dSAdrian Chadd 	 */
1011f1481c8dSAdrian Chadd 	IEEE80211_LOCK(ic);
1012f1481c8dSAdrian Chadd 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1013f1481c8dSAdrian Chadd 		if (iv->iv_flags & IEEE80211_F_USEBARKER)
1014f1481c8dSAdrian Chadd 			barker_count++;
1015f1481c8dSAdrian Chadd 		if (iv->iv_flags & IEEE80211_F_SHPREAMBLE)
1016f1481c8dSAdrian Chadd 			short_preamble_count++;
1017f1481c8dSAdrian Chadd 		count++;
1018f1481c8dSAdrian Chadd 	}
1019f1481c8dSAdrian Chadd 
1020f1481c8dSAdrian Chadd 	/*
1021f1481c8dSAdrian Chadd 	 * As with vap_update_erp_protmode(), the global flags are
1022f1481c8dSAdrian Chadd 	 * currently used for beacon IEs.
1023f1481c8dSAdrian Chadd 	 */
1024f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1025f1481c8dSAdrian Chadd 	    "%s: called; barker_count=%d, short_preamble_count=%d\n",
1026f1481c8dSAdrian Chadd 	    __func__, barker_count, short_preamble_count);
1027f1481c8dSAdrian Chadd 
1028f1481c8dSAdrian Chadd 	/*
1029f1481c8dSAdrian Chadd 	 * Only flip on short preamble if all of the VAPs support
1030f1481c8dSAdrian Chadd 	 * it.
1031f1481c8dSAdrian Chadd 	 */
1032f1481c8dSAdrian Chadd 	if (barker_count == 0 && short_preamble_count == count) {
1033f1481c8dSAdrian Chadd 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1034f1481c8dSAdrian Chadd 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1035f1481c8dSAdrian Chadd 	} else {
1036f1481c8dSAdrian Chadd 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1037f1481c8dSAdrian Chadd 		ic->ic_flags |= IEEE80211_F_USEBARKER;
1038f1481c8dSAdrian Chadd 	}
1039f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1040f1481c8dSAdrian Chadd 	  "%s: global barker=%d preamble=%d\n",
1041f1481c8dSAdrian Chadd 	  __func__,
1042f1481c8dSAdrian Chadd 	  !! (ic->ic_flags & IEEE80211_F_USEBARKER),
1043f1481c8dSAdrian Chadd 	  !! (ic->ic_flags & IEEE80211_F_SHPREAMBLE));
1044f1481c8dSAdrian Chadd 
1045f1481c8dSAdrian Chadd 	/* Beacon update on all VAPs */
1046f1481c8dSAdrian Chadd 	ieee80211_notify_erp_locked(ic);
1047f1481c8dSAdrian Chadd 
1048f1481c8dSAdrian Chadd 	IEEE80211_UNLOCK(ic);
1049f1481c8dSAdrian Chadd 
1050f1481c8dSAdrian Chadd 	/* Driver notification */
105148d689d6SBjoern A. Zeeb 	if (vap->iv_preamble_update != NULL)
1052f1481c8dSAdrian Chadd 		vap->iv_preamble_update(vap);
1053f1481c8dSAdrian Chadd }
1054f1481c8dSAdrian Chadd 
1055f1481c8dSAdrian Chadd /*
1056f1481c8dSAdrian Chadd  * Deferred HT protmode update and beacon update.
1057f1481c8dSAdrian Chadd  *
1058f1481c8dSAdrian Chadd  * Look at the comments for vap_update_erp_protmode() for more
1059f1481c8dSAdrian Chadd  * background; this assumes all VAPs are on the same channel.
1060f1481c8dSAdrian Chadd  */
1061f1481c8dSAdrian Chadd static void
vap_update_ht_protmode(void * arg,int npending)1062f1481c8dSAdrian Chadd vap_update_ht_protmode(void *arg, int npending)
1063f1481c8dSAdrian Chadd {
1064f1481c8dSAdrian Chadd 	struct ieee80211vap *vap = arg;
1065f1481c8dSAdrian Chadd 	struct ieee80211vap *iv;
1066f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
10679319211fSDimitry Andric 	int num_vaps = 0, num_pure = 0;
1068f1481c8dSAdrian Chadd 	int num_optional = 0, num_ht2040 = 0, num_nonht = 0;
1069f1481c8dSAdrian Chadd 	int num_ht_sta = 0, num_ht40_sta = 0, num_sta = 0;
1070f1481c8dSAdrian Chadd 	int num_nonhtpr = 0;
1071f1481c8dSAdrian Chadd 
1072f1481c8dSAdrian Chadd 	/*
1073f1481c8dSAdrian Chadd 	 * Iterate over all of the VAPs to calculate everything.
1074f1481c8dSAdrian Chadd 	 *
1075f1481c8dSAdrian Chadd 	 * There are a few different flags to calculate:
1076f1481c8dSAdrian Chadd 	 *
1077f1481c8dSAdrian Chadd 	 * + whether there's HT only or HT+legacy stations;
1078f1481c8dSAdrian Chadd 	 * + whether there's HT20, HT40, or HT20+HT40 stations;
1079f1481c8dSAdrian Chadd 	 * + whether the desired protection mode is mixed, pure or
1080f1481c8dSAdrian Chadd 	 *   one of the two above.
1081f1481c8dSAdrian Chadd 	 *
1082f1481c8dSAdrian Chadd 	 * For now we assume that if a driver can handle this per-VAP
1083f1481c8dSAdrian Chadd 	 * then it'll ignore the ic->ic_htprotmode / ic->ic_curhtprotmode
1084f1481c8dSAdrian Chadd 	 * variant and instead will look at the vap related variables.
1085f1481c8dSAdrian Chadd 	 *
1086f1481c8dSAdrian Chadd 	 * XXX TODO: non-greenfield STAs present (IEEE80211_HTINFO_NONGF_PRESENT) !
1087f1481c8dSAdrian Chadd 	 */
1088f1481c8dSAdrian Chadd 
1089f1481c8dSAdrian Chadd 	IEEE80211_LOCK(ic);
1090f1481c8dSAdrian Chadd 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1091f1481c8dSAdrian Chadd 		num_vaps++;
1092f1481c8dSAdrian Chadd 		/* overlapping BSSes advertising non-HT status present */
1093f1481c8dSAdrian Chadd 		if (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR)
1094f1481c8dSAdrian Chadd 			num_nonht++;
1095f1481c8dSAdrian Chadd 		/* Operating mode flags */
1096f1481c8dSAdrian Chadd 		if (iv->iv_curhtprotmode & IEEE80211_HTINFO_NONHT_PRESENT)
1097f1481c8dSAdrian Chadd 			num_nonhtpr++;
1098f1481c8dSAdrian Chadd 		switch (iv->iv_curhtprotmode & IEEE80211_HTINFO_OPMODE) {
1099f1481c8dSAdrian Chadd 		case IEEE80211_HTINFO_OPMODE_PURE:
1100f1481c8dSAdrian Chadd 			num_pure++;
1101f1481c8dSAdrian Chadd 			break;
1102f1481c8dSAdrian Chadd 		case IEEE80211_HTINFO_OPMODE_PROTOPT:
1103f1481c8dSAdrian Chadd 			num_optional++;
1104f1481c8dSAdrian Chadd 			break;
1105f1481c8dSAdrian Chadd 		case IEEE80211_HTINFO_OPMODE_HT20PR:
1106f1481c8dSAdrian Chadd 			num_ht2040++;
1107f1481c8dSAdrian Chadd 			break;
1108f1481c8dSAdrian Chadd 		}
1109f1481c8dSAdrian Chadd 
1110f1481c8dSAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1111f1481c8dSAdrian Chadd 		    "%s: vap %s: nonht_pr=%d, curhtprotmode=0x%02x\n",
1112f1481c8dSAdrian Chadd 		    __func__,
1113f1481c8dSAdrian Chadd 		    ieee80211_get_vap_ifname(iv),
1114f1481c8dSAdrian Chadd 		    !! (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR),
1115f1481c8dSAdrian Chadd 		    iv->iv_curhtprotmode);
1116f1481c8dSAdrian Chadd 
1117f1481c8dSAdrian Chadd 		num_ht_sta += iv->iv_ht_sta_assoc;
1118f1481c8dSAdrian Chadd 		num_ht40_sta += iv->iv_ht40_sta_assoc;
1119f1481c8dSAdrian Chadd 		num_sta += iv->iv_sta_assoc;
1120f1481c8dSAdrian Chadd 	}
1121f1481c8dSAdrian Chadd 
1122f1481c8dSAdrian Chadd 	/*
1123f1481c8dSAdrian Chadd 	 * Step 1 - if any VAPs indicate NONHT_PR set (overlapping BSS
1124f1481c8dSAdrian Chadd 	 * non-HT present), set it here.  This shouldn't be used by
1125f1481c8dSAdrian Chadd 	 * anything but the old overlapping BSS logic so if any drivers
1126f1481c8dSAdrian Chadd 	 * consume it, it's up to date.
1127f1481c8dSAdrian Chadd 	 */
1128f1481c8dSAdrian Chadd 	if (num_nonht > 0)
1129f1481c8dSAdrian Chadd 		ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1130f1481c8dSAdrian Chadd 	else
1131f1481c8dSAdrian Chadd 		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1132f1481c8dSAdrian Chadd 
1133f1481c8dSAdrian Chadd 	/*
1134f1481c8dSAdrian Chadd 	 * Step 2 - default HT protection mode to MIXED (802.11-2016 10.26.3.1.)
1135f1481c8dSAdrian Chadd 	 *
1136f1481c8dSAdrian Chadd 	 * + If all VAPs are PURE, we can stay PURE.
1137f1481c8dSAdrian Chadd 	 * + If all VAPs are PROTOPT, we can go to PROTOPT.
1138f1481c8dSAdrian Chadd 	 * + If any VAP has HT20PR then it sees at least a HT40+HT20 station.
1139f1481c8dSAdrian Chadd 	 *   Note that we may have a VAP with one HT20 and a VAP with one HT40;
1140f1481c8dSAdrian Chadd 	 *   So we look at the sum ht and sum ht40 sta counts; if we have a
1141f1481c8dSAdrian Chadd 	 *   HT station and the HT20 != HT40 count, we have to do HT20PR here.
1142f1481c8dSAdrian Chadd 	 *   Note all stations need to be HT for this to be an option.
1143f1481c8dSAdrian Chadd 	 * + The fall-through is MIXED, because it means we have some odd
1144f1481c8dSAdrian Chadd 	 *   non HT40-involved combination of opmode and this is the most
1145f1481c8dSAdrian Chadd 	 *   sensible default.
1146f1481c8dSAdrian Chadd 	 */
1147f1481c8dSAdrian Chadd 	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1148f1481c8dSAdrian Chadd 
1149f1481c8dSAdrian Chadd 	if (num_pure == num_vaps)
1150f1481c8dSAdrian Chadd 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
1151f1481c8dSAdrian Chadd 
1152f1481c8dSAdrian Chadd 	if (num_optional == num_vaps)
1153f1481c8dSAdrian Chadd 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PROTOPT;
1154f1481c8dSAdrian Chadd 
1155f1481c8dSAdrian Chadd 	/*
1156f1481c8dSAdrian Chadd 	 * Note: we need /a/ HT40 station somewhere for this to
1157f1481c8dSAdrian Chadd 	 * be a possibility.
1158f1481c8dSAdrian Chadd 	 */
1159f1481c8dSAdrian Chadd 	if ((num_ht2040 > 0) ||
1160f1481c8dSAdrian Chadd 	    ((num_ht_sta > 0) && (num_ht40_sta > 0) &&
1161f1481c8dSAdrian Chadd 	     (num_ht_sta != num_ht40_sta)))
1162f1481c8dSAdrian Chadd 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1163f1481c8dSAdrian Chadd 
1164f1481c8dSAdrian Chadd 	/*
1165f1481c8dSAdrian Chadd 	 * Step 3 - if any of the stations across the VAPs are
1166f1481c8dSAdrian Chadd 	 * non-HT then this needs to be flipped back to MIXED.
1167f1481c8dSAdrian Chadd 	 */
1168f1481c8dSAdrian Chadd 	if (num_ht_sta != num_sta)
1169f1481c8dSAdrian Chadd 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1170f1481c8dSAdrian Chadd 
1171f1481c8dSAdrian Chadd 	/*
1172f1481c8dSAdrian Chadd 	 * Step 4 - If we see any overlapping BSS non-HT stations
1173f1481c8dSAdrian Chadd 	 * via beacons then flip on NONHT_PRESENT.
1174f1481c8dSAdrian Chadd 	 */
1175f1481c8dSAdrian Chadd 	if (num_nonhtpr > 0)
1176f1481c8dSAdrian Chadd 		ic->ic_curhtprotmode |= IEEE80211_HTINFO_NONHT_PRESENT;
1177f1481c8dSAdrian Chadd 
1178f1481c8dSAdrian Chadd 	/* Notify all VAPs to potentially update their beacons */
1179f1481c8dSAdrian Chadd 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next)
1180f1481c8dSAdrian Chadd 		ieee80211_htinfo_notify(iv);
1181f1481c8dSAdrian Chadd 
1182f1481c8dSAdrian Chadd 	IEEE80211_UNLOCK(ic);
1183f1481c8dSAdrian Chadd 
1184f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1185f1481c8dSAdrian Chadd 	  "%s: global: nonht_pr=%d ht_opmode=0x%02x\n",
1186f1481c8dSAdrian Chadd 	  __func__,
1187f1481c8dSAdrian Chadd 	  !! (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR),
1188f1481c8dSAdrian Chadd 	  ic->ic_curhtprotmode);
1189f1481c8dSAdrian Chadd 
1190f1481c8dSAdrian Chadd 	/* Driver update */
119148d689d6SBjoern A. Zeeb 	if (vap->iv_ht_protmode_update != NULL)
1192f1481c8dSAdrian Chadd 		vap->iv_ht_protmode_update(vap);
1193f1481c8dSAdrian Chadd }
1194f1481c8dSAdrian Chadd 
1195f1481c8dSAdrian Chadd /*
11968a1b9b6aSSam Leffler  * Set the short slot time state and notify the driver.
1197d20ff6e6SAdrian Chadd  *
1198d20ff6e6SAdrian Chadd  * This is the per-VAP slot time state.
11998a1b9b6aSSam Leffler  */
12008a1b9b6aSSam Leffler void
ieee80211_vap_set_shortslottime(struct ieee80211vap * vap,int onoff)1201d20ff6e6SAdrian Chadd ieee80211_vap_set_shortslottime(struct ieee80211vap *vap, int onoff)
12028a1b9b6aSSam Leffler {
1203d20ff6e6SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1204d20ff6e6SAdrian Chadd 
1205f1481c8dSAdrian Chadd 	/* XXX lock? */
1206f1481c8dSAdrian Chadd 
1207d20ff6e6SAdrian Chadd 	/*
1208d20ff6e6SAdrian Chadd 	 * Only modify the per-VAP slot time.
1209d20ff6e6SAdrian Chadd 	 */
12108a1b9b6aSSam Leffler 	if (onoff)
1211d20ff6e6SAdrian Chadd 		vap->iv_flags |= IEEE80211_F_SHSLOT;
12128a1b9b6aSSam Leffler 	else
1213d20ff6e6SAdrian Chadd 		vap->iv_flags &= ~IEEE80211_F_SHSLOT;
1214d20ff6e6SAdrian Chadd 
1215f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1216f1481c8dSAdrian Chadd 	    "%s: called; onoff=%d\n", __func__, onoff);
1217d20ff6e6SAdrian Chadd 	/* schedule the deferred slot flag update and update */
1218d20ff6e6SAdrian Chadd 	ieee80211_runtask(ic, &vap->iv_slot_task);
12198a1b9b6aSSam Leffler }
12208a1b9b6aSSam Leffler 
12218a1b9b6aSSam Leffler /*
1222f1481c8dSAdrian Chadd  * Update the VAP short /long / barker preamble state and
1223f1481c8dSAdrian Chadd  * update beacon state if needed.
1224f1481c8dSAdrian Chadd  *
1225f1481c8dSAdrian Chadd  * For now it simply copies the global flags into the per-vap
1226f1481c8dSAdrian Chadd  * flags and schedules the callback.  Later this will support
1227f1481c8dSAdrian Chadd  * both global and per-VAP flags, especially useful for
1228f1481c8dSAdrian Chadd  * and STA+STA multi-channel operation (eg p2p).
1229f1481c8dSAdrian Chadd  */
1230f1481c8dSAdrian Chadd void
ieee80211_vap_update_preamble(struct ieee80211vap * vap)1231f1481c8dSAdrian Chadd ieee80211_vap_update_preamble(struct ieee80211vap *vap)
1232f1481c8dSAdrian Chadd {
1233f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1234f1481c8dSAdrian Chadd 
1235f1481c8dSAdrian Chadd 	/* XXX lock? */
1236f1481c8dSAdrian Chadd 
1237f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1238f1481c8dSAdrian Chadd 	    "%s: called\n", __func__);
1239f1481c8dSAdrian Chadd 	/* schedule the deferred slot flag update and update */
1240f1481c8dSAdrian Chadd 	ieee80211_runtask(ic, &vap->iv_preamble_task);
1241f1481c8dSAdrian Chadd }
1242f1481c8dSAdrian Chadd 
1243f1481c8dSAdrian Chadd /*
1244f1481c8dSAdrian Chadd  * Update the VAP 11g protection mode and update beacon state
1245f1481c8dSAdrian Chadd  * if needed.
1246f1481c8dSAdrian Chadd  */
1247f1481c8dSAdrian Chadd void
ieee80211_vap_update_erp_protmode(struct ieee80211vap * vap)1248f1481c8dSAdrian Chadd ieee80211_vap_update_erp_protmode(struct ieee80211vap *vap)
1249f1481c8dSAdrian Chadd {
1250f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1251f1481c8dSAdrian Chadd 
1252f1481c8dSAdrian Chadd 	/* XXX lock? */
1253f1481c8dSAdrian Chadd 
1254f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1255f1481c8dSAdrian Chadd 	    "%s: called\n", __func__);
1256f1481c8dSAdrian Chadd 	/* schedule the deferred slot flag update and update */
1257f1481c8dSAdrian Chadd 	ieee80211_runtask(ic, &vap->iv_erp_protmode_task);
1258f1481c8dSAdrian Chadd }
1259f1481c8dSAdrian Chadd 
1260f1481c8dSAdrian Chadd /*
1261f1481c8dSAdrian Chadd  * Update the VAP 11n protection mode and update beacon state
1262f1481c8dSAdrian Chadd  * if needed.
1263f1481c8dSAdrian Chadd  */
1264f1481c8dSAdrian Chadd void
ieee80211_vap_update_ht_protmode(struct ieee80211vap * vap)1265f1481c8dSAdrian Chadd ieee80211_vap_update_ht_protmode(struct ieee80211vap *vap)
1266f1481c8dSAdrian Chadd {
1267f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1268f1481c8dSAdrian Chadd 
1269f1481c8dSAdrian Chadd 	/* XXX lock? */
1270f1481c8dSAdrian Chadd 
1271f1481c8dSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1272f1481c8dSAdrian Chadd 	    "%s: called\n", __func__);
1273f1481c8dSAdrian Chadd 	/* schedule the deferred protmode update */
1274f1481c8dSAdrian Chadd 	ieee80211_runtask(ic, &vap->iv_ht_protmode_task);
1275f1481c8dSAdrian Chadd }
1276f1481c8dSAdrian Chadd 
1277f1481c8dSAdrian Chadd /*
12788a1b9b6aSSam Leffler  * Check if the specified rate set supports ERP.
12798a1b9b6aSSam Leffler  * NB: the rate set is assumed to be sorted.
12808a1b9b6aSSam Leffler  */
12818a1b9b6aSSam Leffler int
ieee80211_iserp_rateset(const struct ieee80211_rateset * rs)1282b032f27cSSam Leffler ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
12838a1b9b6aSSam Leffler {
12848a1b9b6aSSam Leffler 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
12858a1b9b6aSSam Leffler 	int i, j;
12868a1b9b6aSSam Leffler 
1287a3e08d6fSRui Paulo 	if (rs->rs_nrates < nitems(rates))
12888a1b9b6aSSam Leffler 		return 0;
1289a3e08d6fSRui Paulo 	for (i = 0; i < nitems(rates); i++) {
12908a1b9b6aSSam Leffler 		for (j = 0; j < rs->rs_nrates; j++) {
12918a1b9b6aSSam Leffler 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
12928a1b9b6aSSam Leffler 			if (rates[i] == r)
12938a1b9b6aSSam Leffler 				goto next;
12948a1b9b6aSSam Leffler 			if (r > rates[i])
12958a1b9b6aSSam Leffler 				return 0;
12968a1b9b6aSSam Leffler 		}
12978a1b9b6aSSam Leffler 		return 0;
12988a1b9b6aSSam Leffler 	next:
12998a1b9b6aSSam Leffler 		;
13008a1b9b6aSSam Leffler 	}
13018a1b9b6aSSam Leffler 	return 1;
13028a1b9b6aSSam Leffler }
13038a1b9b6aSSam Leffler 
13048a1b9b6aSSam Leffler /*
1305b032f27cSSam Leffler  * Mark the basic rates for the rate table based on the
13068a1b9b6aSSam Leffler  * operating mode.  For real 11g we mark all the 11b rates
13078a1b9b6aSSam Leffler  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
13088a1b9b6aSSam Leffler  * 11b rates.  There's also a pseudo 11a-mode used to mark only
13098a1b9b6aSSam Leffler  * the basic OFDM rates.
13108a1b9b6aSSam Leffler  */
1311b032f27cSSam Leffler static void
setbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode,int add)1312b032f27cSSam Leffler setbasicrates(struct ieee80211_rateset *rs,
1313b032f27cSSam Leffler     enum ieee80211_phymode mode, int add)
13148a1b9b6aSSam Leffler {
131568e8e04eSSam Leffler 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
1316be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
1317be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
1318be0df3e7SSam Leffler 					    /* NB: mixed b/g */
1319be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
1320be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
1321be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
1322be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
13236a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
13246a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
1325be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
1326be0df3e7SSam Leffler 					    /* NB: mixed b/g */
1327be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
13288fde59a7SAdrian Chadd 					    /* NB: mixed b/g */
13298fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= { 4, { 2, 4, 11, 22 } },
13308fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= { 3, { 12, 24, 48 } },
13318a1b9b6aSSam Leffler 	};
13328a1b9b6aSSam Leffler 	int i, j;
13338a1b9b6aSSam Leffler 
13348a1b9b6aSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++) {
1335b032f27cSSam Leffler 		if (!add)
13368a1b9b6aSSam Leffler 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
13378a1b9b6aSSam Leffler 		for (j = 0; j < basic[mode].rs_nrates; j++)
13388a1b9b6aSSam Leffler 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
13398a1b9b6aSSam Leffler 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
13408a1b9b6aSSam Leffler 				break;
13418a1b9b6aSSam Leffler 			}
13428a1b9b6aSSam Leffler 	}
13438a1b9b6aSSam Leffler }
13448a1b9b6aSSam Leffler 
13458a1b9b6aSSam Leffler /*
1346b032f27cSSam Leffler  * Set the basic rates in a rate set.
1347b032f27cSSam Leffler  */
1348b032f27cSSam Leffler void
ieee80211_setbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode)1349b032f27cSSam Leffler ieee80211_setbasicrates(struct ieee80211_rateset *rs,
1350b032f27cSSam Leffler     enum ieee80211_phymode mode)
1351b032f27cSSam Leffler {
1352b032f27cSSam Leffler 	setbasicrates(rs, mode, 0);
1353b032f27cSSam Leffler }
1354b032f27cSSam Leffler 
1355b032f27cSSam Leffler /*
1356b032f27cSSam Leffler  * Add basic rates to a rate set.
1357b032f27cSSam Leffler  */
1358b032f27cSSam Leffler void
ieee80211_addbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode)1359b032f27cSSam Leffler ieee80211_addbasicrates(struct ieee80211_rateset *rs,
1360b032f27cSSam Leffler     enum ieee80211_phymode mode)
1361b032f27cSSam Leffler {
1362b032f27cSSam Leffler 	setbasicrates(rs, mode, 1);
1363b032f27cSSam Leffler }
1364b032f27cSSam Leffler 
1365b032f27cSSam Leffler /*
1366b032f27cSSam Leffler  * WME protocol support.
1367b032f27cSSam Leffler  *
1368b032f27cSSam Leffler  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
1369b032f27cSSam Leffler  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
1370b032f27cSSam Leffler  * Draft 2.0 Test Plan (Appendix D).
1371b032f27cSSam Leffler  *
1372b032f27cSSam Leffler  * Static/Dynamic Turbo mode settings come from Atheros.
13738a1b9b6aSSam Leffler  */
13748a1b9b6aSSam Leffler typedef struct phyParamType {
137568e8e04eSSam Leffler 	uint8_t		aifsn;
137668e8e04eSSam Leffler 	uint8_t		logcwmin;
137768e8e04eSSam Leffler 	uint8_t		logcwmax;
137868e8e04eSSam Leffler 	uint16_t	txopLimit;
137968e8e04eSSam Leffler 	uint8_t 	acm;
13808a1b9b6aSSam Leffler } paramType;
13818a1b9b6aSSam Leffler 
13828a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
1383be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
1384be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
1385be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
1386be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
1387be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
1388be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
1389be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
1390be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
13916a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
13926a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
1393be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
1394be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
13958fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 3, 4,  6,  0, 0 },
13968fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 3, 4,  6,  0, 0 },
13978a1b9b6aSSam Leffler };
13988a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
1399be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
1400be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
1401be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
1402be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
1403be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
1404be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
1405be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
1406be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
14076a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
14086a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
1409be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
1410be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
14118fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 7, 4, 10,  0, 0 },
14128fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 7, 4, 10,  0, 0 },
14138a1b9b6aSSam Leffler };
14148a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
1415be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
1416be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
1417be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
1418be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
1419be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
1420be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
1421be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
1422be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
14236a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
14246a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
1425be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
1426be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
14278fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 3, 4,  94, 0 },
14288fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 3, 4,  94, 0 },
14298a1b9b6aSSam Leffler };
14308a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
1431be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
1432be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
1433be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
1434be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
1435be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
1436be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
1437be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
1438be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
14396a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
14406a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
1441be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
1442be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
14438fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 2, 3,  47, 0 },
14448fde59a7SAdrian Chadd 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 2, 3,  47, 0 },
14458a1b9b6aSSam Leffler };
14468a1b9b6aSSam Leffler 
14478a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
1448be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
1449be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
1450be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
1451be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
1452be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
1453be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
1454be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
1455be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
14566a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
14576a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
1458be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
1459be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
14608a1b9b6aSSam Leffler };
14618a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
1462be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
1463be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
1464be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
1465be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
1466be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
1467be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
1468be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
1469be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
14706a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
14716a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
1472be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
1473be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
14748a1b9b6aSSam Leffler };
14758a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
1476be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
1477be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
1478be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
1479be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
1480be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
1481be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
1482be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
1483be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
14846a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
14856a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
1486be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
1487be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
14888a1b9b6aSSam Leffler };
14898a1b9b6aSSam Leffler 
1490b032f27cSSam Leffler static void
_setifsparams(struct wmeParams * wmep,const paramType * phy)149167ce310aSSam Leffler _setifsparams(struct wmeParams *wmep, const paramType *phy)
149267ce310aSSam Leffler {
149367ce310aSSam Leffler 	wmep->wmep_aifsn = phy->aifsn;
149467ce310aSSam Leffler 	wmep->wmep_logcwmin = phy->logcwmin;
149567ce310aSSam Leffler 	wmep->wmep_logcwmax = phy->logcwmax;
149667ce310aSSam Leffler 	wmep->wmep_txopLimit = phy->txopLimit;
149767ce310aSSam Leffler }
149867ce310aSSam Leffler 
149967ce310aSSam Leffler static void
setwmeparams(struct ieee80211vap * vap,const char * type,int ac,struct wmeParams * wmep,const paramType * phy)150067ce310aSSam Leffler setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
150167ce310aSSam Leffler 	struct wmeParams *wmep, const paramType *phy)
150267ce310aSSam Leffler {
150367ce310aSSam Leffler 	wmep->wmep_acm = phy->acm;
150467ce310aSSam Leffler 	_setifsparams(wmep, phy);
150567ce310aSSam Leffler 
150667ce310aSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
150767ce310aSSam Leffler 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
150867ce310aSSam Leffler 	    ieee80211_wme_acnames[ac], type,
150967ce310aSSam Leffler 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
151067ce310aSSam Leffler 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
151167ce310aSSam Leffler }
151267ce310aSSam Leffler 
151367ce310aSSam Leffler static void
ieee80211_wme_initparams_locked(struct ieee80211vap * vap)1514b032f27cSSam Leffler ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
15158a1b9b6aSSam Leffler {
1516b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
15178a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
15188a1b9b6aSSam Leffler 	const paramType *pPhyParam, *pBssPhyParam;
15198a1b9b6aSSam Leffler 	struct wmeParams *wmep;
152068e8e04eSSam Leffler 	enum ieee80211_phymode mode;
15218a1b9b6aSSam Leffler 	int i;
15228a1b9b6aSSam Leffler 
1523b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1524b032f27cSSam Leffler 
1525a4b3c7a5SSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
15268a1b9b6aSSam Leffler 		return;
15278a1b9b6aSSam Leffler 
152868e8e04eSSam Leffler 	/*
15290d4e4e5eSAdrian Chadd 	 * Clear the wme cap_info field so a qoscount from a previous
15300d4e4e5eSAdrian Chadd 	 * vap doesn't confuse later code which only parses the beacon
15310d4e4e5eSAdrian Chadd 	 * field and updates hardware when said field changes.
15320d4e4e5eSAdrian Chadd 	 * Otherwise the hardware is programmed with defaults, not what
15330d4e4e5eSAdrian Chadd 	 * the beacon actually announces.
15348379e8dbSAdrian Chadd 	 *
15358379e8dbSAdrian Chadd 	 * Note that we can't ever have 0xff as an actual value;
15368379e8dbSAdrian Chadd 	 * the only valid values are 0..15.
15370d4e4e5eSAdrian Chadd 	 */
15388379e8dbSAdrian Chadd 	wme->wme_wmeChanParams.cap_info = 0xfe;
15390d4e4e5eSAdrian Chadd 
15400d4e4e5eSAdrian Chadd 	/*
154168e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
154268e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
154368e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
154468e8e04eSSam Leffler 	 * so state will eventually get set correctly
154568e8e04eSSam Leffler 	 */
154668e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
154768e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
154868e8e04eSSam Leffler 	else
154968e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
15508a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
15518a1b9b6aSSam Leffler 		switch (i) {
15528a1b9b6aSSam Leffler 		case WME_AC_BK:
155368e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BK[mode];
155468e8e04eSSam Leffler 			pBssPhyParam = &phyParamForAC_BK[mode];
15558a1b9b6aSSam Leffler 			break;
15568a1b9b6aSSam Leffler 		case WME_AC_VI:
155768e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VI[mode];
155868e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
15598a1b9b6aSSam Leffler 			break;
15608a1b9b6aSSam Leffler 		case WME_AC_VO:
156168e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VO[mode];
156268e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
15638a1b9b6aSSam Leffler 			break;
15648a1b9b6aSSam Leffler 		case WME_AC_BE:
15658a1b9b6aSSam Leffler 		default:
156668e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BE[mode];
156768e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
15688a1b9b6aSSam Leffler 			break;
15698a1b9b6aSSam Leffler 		}
15708a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
15718a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
157267ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
15738a1b9b6aSSam Leffler 		} else {
157467ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
15758a1b9b6aSSam Leffler 		}
15768a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
157767ce310aSSam Leffler 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
15788a1b9b6aSSam Leffler 	}
15798a1b9b6aSSam Leffler 	/* NB: check ic_bss to avoid NULL deref on initial attach */
1580b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
15818a1b9b6aSSam Leffler 		/*
1582a4641f4eSPedro F. Giffuni 		 * Calculate aggressive mode switching threshold based
15838a1b9b6aSSam Leffler 		 * on beacon interval.  This doesn't need locking since
15848a1b9b6aSSam Leffler 		 * we're only called before entering the RUN state at
15858a1b9b6aSSam Leffler 		 * which point we start sending beacon frames.
15868a1b9b6aSSam Leffler 		 */
15878a1b9b6aSSam Leffler 		wme->wme_hipri_switch_thresh =
1588b032f27cSSam Leffler 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1589a4b3c7a5SSam Leffler 		wme->wme_flags &= ~WME_F_AGGRMODE;
1590b032f27cSSam Leffler 		ieee80211_wme_updateparams(vap);
15918a1b9b6aSSam Leffler 	}
15928a1b9b6aSSam Leffler }
15938a1b9b6aSSam Leffler 
1594b032f27cSSam Leffler void
ieee80211_wme_initparams(struct ieee80211vap * vap)1595b032f27cSSam Leffler ieee80211_wme_initparams(struct ieee80211vap *vap)
1596b032f27cSSam Leffler {
1597b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1598b032f27cSSam Leffler 
1599b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1600b032f27cSSam Leffler 	ieee80211_wme_initparams_locked(vap);
1601b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1602b032f27cSSam Leffler }
1603b032f27cSSam Leffler 
16048a1b9b6aSSam Leffler /*
16058a1b9b6aSSam Leffler  * Update WME parameters for ourself and the BSS.
16068a1b9b6aSSam Leffler  */
16078a1b9b6aSSam Leffler void
ieee80211_wme_updateparams_locked(struct ieee80211vap * vap)1608b032f27cSSam Leffler ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
16098a1b9b6aSSam Leffler {
161067ce310aSSam Leffler 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1611be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
1612be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
1613be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
1614be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
1615be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
1616be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
1617be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
1618be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
16196a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
16206a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
1621be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
1622be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
16238fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_2GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
16248fde59a7SAdrian Chadd 	    [IEEE80211_MODE_VHT_5GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
16258a1b9b6aSSam Leffler 	};
1626b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
16278a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
16288a1b9b6aSSam Leffler 	const struct wmeParams *wmep;
16298a1b9b6aSSam Leffler 	struct wmeParams *chanp, *bssp;
163068e8e04eSSam Leffler 	enum ieee80211_phymode mode;
16318a1b9b6aSSam Leffler 	int i;
1632a48a8ad7SAdrian Chadd 	int do_aggrmode = 0;
16338a1b9b6aSSam Leffler 
163467ce310aSSam Leffler        	/*
163567ce310aSSam Leffler 	 * Set up the channel access parameters for the physical
163667ce310aSSam Leffler 	 * device.  First populate the configured settings.
163767ce310aSSam Leffler 	 */
16388a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
16398a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
16408a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
16418a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
16428a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
16438a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
16448a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
16458a1b9b6aSSam Leffler 
16468a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
16478a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
16488a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
16498a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
16508a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
16518a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
16528a1b9b6aSSam Leffler 	}
16538a1b9b6aSSam Leffler 
16548a1b9b6aSSam Leffler 	/*
165568e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
165668e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
165768e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
165868e8e04eSSam Leffler 	 * so state will eventually get set correctly
165968e8e04eSSam Leffler 	 */
166068e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
166168e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
166268e8e04eSSam Leffler 	else
166368e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
166468e8e04eSSam Leffler 
166568e8e04eSSam Leffler 	/*
1666a4641f4eSPedro F. Giffuni 	 * This implements aggressive mode as found in certain
16678a1b9b6aSSam Leffler 	 * vendors' AP's.  When there is significant high
16688a1b9b6aSSam Leffler 	 * priority (VI/VO) traffic in the BSS throttle back BE
16698a1b9b6aSSam Leffler 	 * traffic by using conservative parameters.  Otherwise
1670a4641f4eSPedro F. Giffuni 	 * BE uses aggressive params to optimize performance of
16718a1b9b6aSSam Leffler 	 * legacy/non-QoS traffic.
16728a1b9b6aSSam Leffler 	 */
1673a48a8ad7SAdrian Chadd 
1674a48a8ad7SAdrian Chadd 	/* Hostap? Only if aggressive mode is enabled */
1675a48a8ad7SAdrian Chadd         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1676a48a8ad7SAdrian Chadd 	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
1677a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1678a48a8ad7SAdrian Chadd 
1679a48a8ad7SAdrian Chadd 	/*
1680a48a8ad7SAdrian Chadd 	 * Station? Only if we're in a non-QoS BSS.
1681a48a8ad7SAdrian Chadd 	 */
1682a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_STA &&
1683a48a8ad7SAdrian Chadd 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1684a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1685a48a8ad7SAdrian Chadd 
1686a48a8ad7SAdrian Chadd 	/*
168793e49148SGordon Bergling 	 * IBSS? Only if we have WME enabled.
1688a48a8ad7SAdrian Chadd 	 */
1689a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1690a48a8ad7SAdrian Chadd 	    (vap->iv_flags & IEEE80211_F_WME))
1691a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1692a48a8ad7SAdrian Chadd 
1693a48a8ad7SAdrian Chadd 	/*
1694a48a8ad7SAdrian Chadd 	 * If WME is disabled on this VAP, default to aggressive mode
1695a48a8ad7SAdrian Chadd 	 * regardless of the configuration.
1696a48a8ad7SAdrian Chadd 	 */
1697a48a8ad7SAdrian Chadd 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1698a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1699a48a8ad7SAdrian Chadd 
1700a48a8ad7SAdrian Chadd 	/* XXX WDS? */
1701a48a8ad7SAdrian Chadd 
1702a48a8ad7SAdrian Chadd 	/* XXX MBSS? */
1703a48a8ad7SAdrian Chadd 
1704a48a8ad7SAdrian Chadd 	if (do_aggrmode) {
17058a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
17068a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
17078a1b9b6aSSam Leffler 
170867ce310aSSam Leffler 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
17098a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
171067ce310aSSam Leffler 		    aggrParam[mode].logcwmin;
17118a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
171267ce310aSSam Leffler 		    aggrParam[mode].logcwmax;
17138a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1714b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_BURST) ?
171567ce310aSSam Leffler 			aggrParam[mode].txopLimit : 0;
1716b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
171767ce310aSSam Leffler 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
171867ce310aSSam Leffler 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
171967ce310aSSam Leffler 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
172067ce310aSSam Leffler 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
17218a1b9b6aSSam Leffler 	}
17228a1b9b6aSSam Leffler 
1723a48a8ad7SAdrian Chadd 	/*
1724a48a8ad7SAdrian Chadd 	 * Change the contention window based on the number of associated
1725a48a8ad7SAdrian Chadd 	 * stations.  If the number of associated stations is 1 and
1726a48a8ad7SAdrian Chadd 	 * aggressive mode is enabled, lower the contention window even
1727a48a8ad7SAdrian Chadd 	 * further.
1728a48a8ad7SAdrian Chadd 	 */
1729b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1730f1481c8dSAdrian Chadd 	    vap->iv_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
173168e8e04eSSam Leffler 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1732be0df3e7SSam Leffler 		    [IEEE80211_MODE_AUTO]	= 3,
1733be0df3e7SSam Leffler 		    [IEEE80211_MODE_11A]	= 3,
1734be0df3e7SSam Leffler 		    [IEEE80211_MODE_11B]	= 4,
1735be0df3e7SSam Leffler 		    [IEEE80211_MODE_11G]	= 3,
1736be0df3e7SSam Leffler 		    [IEEE80211_MODE_FH]		= 4,
1737be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_A]	= 3,
1738be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_G]	= 3,
1739be0df3e7SSam Leffler 		    [IEEE80211_MODE_STURBO_A]	= 3,
17406a76ae21SSam Leffler 		    [IEEE80211_MODE_HALF]	= 3,
17416a76ae21SSam Leffler 		    [IEEE80211_MODE_QUARTER]	= 3,
1742be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NA]	= 3,
1743be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NG]	= 3,
17448fde59a7SAdrian Chadd 		    [IEEE80211_MODE_VHT_2GHZ]	= 3,
17458fde59a7SAdrian Chadd 		    [IEEE80211_MODE_VHT_5GHZ]	= 3,
17468a1b9b6aSSam Leffler 		};
17478a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
17488a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
17498a1b9b6aSSam Leffler 
175068e8e04eSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1751b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
175267ce310aSSam Leffler 		    "update %s (chan+bss) logcwmin %u\n",
175367ce310aSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
17548a1b9b6aSSam Leffler 	}
1755a48a8ad7SAdrian Chadd 
1756dd2fb488SAdrian Chadd 	/* schedule the deferred WME update */
1757e3e94c96SAdrian Chadd 	ieee80211_runtask(ic, &vap->iv_wme_task);
17588a1b9b6aSSam Leffler 
1759b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
17608a1b9b6aSSam Leffler 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1761b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA ?
17628a1b9b6aSSam Leffler 		wme->wme_wmeChanParams.cap_info :
17638a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info);
17648a1b9b6aSSam Leffler }
17658a1b9b6aSSam Leffler 
17668a1b9b6aSSam Leffler void
ieee80211_wme_updateparams(struct ieee80211vap * vap)1767b032f27cSSam Leffler ieee80211_wme_updateparams(struct ieee80211vap *vap)
17688a1b9b6aSSam Leffler {
1769b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17708a1b9b6aSSam Leffler 
17718a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_WME) {
1772b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
1773b032f27cSSam Leffler 		ieee80211_wme_updateparams_locked(vap);
1774b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
17758a1b9b6aSSam Leffler 	}
17768a1b9b6aSSam Leffler }
17778a1b9b6aSSam Leffler 
17780c696036SAdrian Chadd /*
17790c696036SAdrian Chadd  * Fetch the WME parameters for the given VAP.
17800c696036SAdrian Chadd  *
17810c696036SAdrian Chadd  * When net80211 grows p2p, etc support, this may return different
17820c696036SAdrian Chadd  * parameters for each VAP.
17830c696036SAdrian Chadd  */
1784d03baf35SAdrian Chadd void
ieee80211_wme_vap_getparams(struct ieee80211vap * vap,struct chanAccParams * wp)1785d03baf35SAdrian Chadd ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
1786d03baf35SAdrian Chadd {
1787d03baf35SAdrian Chadd 
1788d03baf35SAdrian Chadd 	memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
1789d03baf35SAdrian Chadd }
1790d03baf35SAdrian Chadd 
17910c696036SAdrian Chadd /*
1792044169efSGordon Bergling  * For NICs which only support one set of WME parameters (ie, softmac NICs)
17930c696036SAdrian Chadd  * there may be different VAP WME parameters but only one is "active".
17940c696036SAdrian Chadd  * This returns the "NIC" WME parameters for the currently active
17950c696036SAdrian Chadd  * context.
17960c696036SAdrian Chadd  */
1797d03baf35SAdrian Chadd void
ieee80211_wme_ic_getparams(struct ieee80211com * ic,struct chanAccParams * wp)1798d03baf35SAdrian Chadd ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
1799d03baf35SAdrian Chadd {
1800d03baf35SAdrian Chadd 
1801d03baf35SAdrian Chadd 	memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
1802d03baf35SAdrian Chadd }
1803d03baf35SAdrian Chadd 
18040c696036SAdrian Chadd /*
18050c696036SAdrian Chadd  * Return whether to use QoS on a given WME queue.
18060c696036SAdrian Chadd  *
18070c696036SAdrian Chadd  * This is intended to be called from the transmit path of softmac drivers
18080c696036SAdrian Chadd  * which are setting NoAck bits in transmit descriptors.
18090c696036SAdrian Chadd  *
18100c696036SAdrian Chadd  * Ideally this would be set in some transmit field before the packet is
18110c696036SAdrian Chadd  * queued to the driver but net80211 isn't quite there yet.
18120c696036SAdrian Chadd  */
18130c696036SAdrian Chadd int
ieee80211_wme_vap_ac_is_noack(struct ieee80211vap * vap,int ac)18140c696036SAdrian Chadd ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
18150c696036SAdrian Chadd {
18160c696036SAdrian Chadd 	/* Bounds/sanity check */
18170c696036SAdrian Chadd 	if (ac < 0 || ac >= WME_NUM_AC)
18180c696036SAdrian Chadd 		return (0);
18190c696036SAdrian Chadd 
18200c696036SAdrian Chadd 	/* Again, there's only one global context for now */
18210c696036SAdrian Chadd 	return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
18220c696036SAdrian Chadd }
18230c696036SAdrian Chadd 
1824b032f27cSSam Leffler static void
parent_updown(void * arg,int npending)1825b032f27cSSam Leffler parent_updown(void *arg, int npending)
182668e8e04eSSam Leffler {
18277a79cebfSGleb Smirnoff 	struct ieee80211com *ic = arg;
182868e8e04eSSam Leffler 
18297a79cebfSGleb Smirnoff 	ic->ic_parent(ic);
1830b032f27cSSam Leffler }
183168e8e04eSSam Leffler 
18325efea30fSAndrew Thompson static void
update_mcast(void * arg,int npending)18335efea30fSAndrew Thompson update_mcast(void *arg, int npending)
18345efea30fSAndrew Thompson {
18355efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
18365efea30fSAndrew Thompson 
1837272f6adeSGleb Smirnoff 	ic->ic_update_mcast(ic);
18385efea30fSAndrew Thompson }
18395efea30fSAndrew Thompson 
18405efea30fSAndrew Thompson static void
update_promisc(void * arg,int npending)18415efea30fSAndrew Thompson update_promisc(void *arg, int npending)
18425efea30fSAndrew Thompson {
18435efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
18445efea30fSAndrew Thompson 
1845272f6adeSGleb Smirnoff 	ic->ic_update_promisc(ic);
18465efea30fSAndrew Thompson }
18475efea30fSAndrew Thompson 
18485efea30fSAndrew Thompson static void
update_channel(void * arg,int npending)18495efea30fSAndrew Thompson update_channel(void *arg, int npending)
18505efea30fSAndrew Thompson {
18515efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
18525efea30fSAndrew Thompson 
18535efea30fSAndrew Thompson 	ic->ic_set_channel(ic);
18545463c4a4SSam Leffler 	ieee80211_radiotap_chan_change(ic);
18555efea30fSAndrew Thompson }
18565efea30fSAndrew Thompson 
1857b94299c4SAdrian Chadd static void
update_chw(void * arg,int npending)1858b94299c4SAdrian Chadd update_chw(void *arg, int npending)
1859b94299c4SAdrian Chadd {
1860b94299c4SAdrian Chadd 	struct ieee80211com *ic = arg;
1861b94299c4SAdrian Chadd 
1862b94299c4SAdrian Chadd 	/*
1863b94299c4SAdrian Chadd 	 * XXX should we defer the channel width _config_ update until now?
1864b94299c4SAdrian Chadd 	 */
1865b94299c4SAdrian Chadd 	ic->ic_update_chw(ic);
1866b94299c4SAdrian Chadd }
1867b94299c4SAdrian Chadd 
1868dd2fb488SAdrian Chadd /*
1869f1481c8dSAdrian Chadd  * Deferred WME parameter and beacon update.
1870e3e94c96SAdrian Chadd  *
1871e3e94c96SAdrian Chadd  * In preparation for per-VAP WME configuration, call the VAP
1872e3e94c96SAdrian Chadd  * method if the VAP requires it.  Otherwise, just call the
1873e3e94c96SAdrian Chadd  * older global method.  There isn't a per-VAP WME configuration
1874e3e94c96SAdrian Chadd  * just yet so for now just use the global configuration.
1875dd2fb488SAdrian Chadd  */
1876e3e94c96SAdrian Chadd static void
vap_update_wme(void * arg,int npending)1877e3e94c96SAdrian Chadd vap_update_wme(void *arg, int npending)
1878e3e94c96SAdrian Chadd {
1879e3e94c96SAdrian Chadd 	struct ieee80211vap *vap = arg;
1880e3e94c96SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
1881f1481c8dSAdrian Chadd 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1882e3e94c96SAdrian Chadd 
1883f1481c8dSAdrian Chadd 	/* Driver update */
1884e3e94c96SAdrian Chadd 	if (vap->iv_wme_update != NULL)
1885e3e94c96SAdrian Chadd 		vap->iv_wme_update(vap,
1886e3e94c96SAdrian Chadd 		    ic->ic_wme.wme_chanParams.cap_wmeParams);
1887e3e94c96SAdrian Chadd 	else
1888dd2fb488SAdrian Chadd 		ic->ic_wme.wme_update(ic);
1889f1481c8dSAdrian Chadd 
1890f1481c8dSAdrian Chadd 	IEEE80211_LOCK(ic);
1891f1481c8dSAdrian Chadd 	/*
1892f1481c8dSAdrian Chadd 	 * Arrange for the beacon update.
1893f1481c8dSAdrian Chadd 	 *
1894f1481c8dSAdrian Chadd 	 * XXX what about MBSS, WDS?
1895f1481c8dSAdrian Chadd 	 */
1896f1481c8dSAdrian Chadd 	if (vap->iv_opmode == IEEE80211_M_HOSTAP
1897f1481c8dSAdrian Chadd 	    || vap->iv_opmode == IEEE80211_M_IBSS) {
1898f1481c8dSAdrian Chadd 		/*
1899f1481c8dSAdrian Chadd 		 * Arrange for a beacon update and bump the parameter
1900f1481c8dSAdrian Chadd 		 * set number so associated stations load the new values.
1901f1481c8dSAdrian Chadd 		 */
1902f1481c8dSAdrian Chadd 		wme->wme_bssChanParams.cap_info =
1903f1481c8dSAdrian Chadd 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1904f1481c8dSAdrian Chadd 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1905f1481c8dSAdrian Chadd 	}
1906f1481c8dSAdrian Chadd 	IEEE80211_UNLOCK(ic);
1907dd2fb488SAdrian Chadd }
1908dd2fb488SAdrian Chadd 
19094061c639SAndriy Voskoboinyk static void
restart_vaps(void * arg,int npending)19104061c639SAndriy Voskoboinyk restart_vaps(void *arg, int npending)
19114061c639SAndriy Voskoboinyk {
19124061c639SAndriy Voskoboinyk 	struct ieee80211com *ic = arg;
19134061c639SAndriy Voskoboinyk 
19144061c639SAndriy Voskoboinyk 	ieee80211_suspend_all(ic);
19154061c639SAndriy Voskoboinyk 	ieee80211_resume_all(ic);
19164061c639SAndriy Voskoboinyk }
19174061c639SAndriy Voskoboinyk 
191868e8e04eSSam Leffler /*
1919ae55932eSAndrew Thompson  * Block until the parent is in a known state.  This is
1920ae55932eSAndrew Thompson  * used after any operations that dispatch a task (e.g.
1921ae55932eSAndrew Thompson  * to auto-configure the parent device up/down).
1922ae55932eSAndrew Thompson  */
1923ae55932eSAndrew Thompson void
ieee80211_waitfor_parent(struct ieee80211com * ic)1924ae55932eSAndrew Thompson ieee80211_waitfor_parent(struct ieee80211com *ic)
1925ae55932eSAndrew Thompson {
19265efea30fSAndrew Thompson 	taskqueue_block(ic->ic_tq);
19275efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_parent_task);
19285efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_mcast_task);
19295efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_promisc_task);
19305efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_chan_task);
19315efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1932b94299c4SAdrian Chadd 	ieee80211_draintask(ic, &ic->ic_chw_task);
19335efea30fSAndrew Thompson 	taskqueue_unblock(ic->ic_tq);
1934ae55932eSAndrew Thompson }
1935ae55932eSAndrew Thompson 
1936ae55932eSAndrew Thompson /*
193724034ddbSAdrian Chadd  * Check to see whether the current channel needs reset.
193824034ddbSAdrian Chadd  *
193924034ddbSAdrian Chadd  * Some devices don't handle being given an invalid channel
194024034ddbSAdrian Chadd  * in their operating mode very well (eg wpi(4) will throw a
194124034ddbSAdrian Chadd  * firmware exception.)
194224034ddbSAdrian Chadd  *
194324034ddbSAdrian Chadd  * Return 0 if we're ok, 1 if the channel needs to be reset.
194424034ddbSAdrian Chadd  *
194524034ddbSAdrian Chadd  * See PR kern/202502.
194624034ddbSAdrian Chadd  */
194724034ddbSAdrian Chadd static int
ieee80211_start_check_reset_chan(struct ieee80211vap * vap)194824034ddbSAdrian Chadd ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
194924034ddbSAdrian Chadd {
195024034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
195124034ddbSAdrian Chadd 
195224034ddbSAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_IBSS &&
195324034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
195424034ddbSAdrian Chadd 	    (vap->iv_opmode == IEEE80211_M_HOSTAP &&
195524034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
195624034ddbSAdrian Chadd 		return (1);
195724034ddbSAdrian Chadd 	return (0);
195824034ddbSAdrian Chadd }
195924034ddbSAdrian Chadd 
196024034ddbSAdrian Chadd /*
196124034ddbSAdrian Chadd  * Reset the curchan to a known good state.
196224034ddbSAdrian Chadd  */
196324034ddbSAdrian Chadd static void
ieee80211_start_reset_chan(struct ieee80211vap * vap)196424034ddbSAdrian Chadd ieee80211_start_reset_chan(struct ieee80211vap *vap)
196524034ddbSAdrian Chadd {
196624034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
196724034ddbSAdrian Chadd 
196824034ddbSAdrian Chadd 	ic->ic_curchan = &ic->ic_channels[0];
196924034ddbSAdrian Chadd }
197024034ddbSAdrian Chadd 
197124034ddbSAdrian Chadd /*
1972b032f27cSSam Leffler  * Start a vap running.  If this is the first vap to be
1973b032f27cSSam Leffler  * set running on the underlying device then we
1974b032f27cSSam Leffler  * automatically bring the device up.
197568e8e04eSSam Leffler  */
1976b032f27cSSam Leffler void
ieee80211_start_locked(struct ieee80211vap * vap)1977b032f27cSSam Leffler ieee80211_start_locked(struct ieee80211vap *vap)
1978b032f27cSSam Leffler {
1979b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1980b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1981b032f27cSSam Leffler 
1982b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1983b032f27cSSam Leffler 
1984b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap,
1985b032f27cSSam Leffler 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1986b032f27cSSam Leffler 		"start running, %d vaps running\n", ic->ic_nrunning);
1987b032f27cSSam Leffler 
1988b032f27cSSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1989b032f27cSSam Leffler 		/*
1990b032f27cSSam Leffler 		 * Mark us running.  Note that it's ok to do this first;
1991b032f27cSSam Leffler 		 * if we need to bring the parent device up we defer that
1992b032f27cSSam Leffler 		 * to avoid dropping the com lock.  We expect the device
1993b032f27cSSam Leffler 		 * to respond to being marked up by calling back into us
1994b032f27cSSam Leffler 		 * through ieee80211_start_all at which point we'll come
1995b032f27cSSam Leffler 		 * back in here and complete the work.
1996b032f27cSSam Leffler 		 */
1997b032f27cSSam Leffler 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
19981bcd230fSAlexander V. Chernikov 		ieee80211_notify_ifnet_change(vap, IFF_DRV_RUNNING);
19992c13efdfSAndriy Gapon 
2000b032f27cSSam Leffler 		/*
2001b032f27cSSam Leffler 		 * We are not running; if this we are the first vap
2002b032f27cSSam Leffler 		 * to be brought up auto-up the parent if necessary.
2003b032f27cSSam Leffler 		 */
20047a79cebfSGleb Smirnoff 		if (ic->ic_nrunning++ == 0) {
200524034ddbSAdrian Chadd 			/* reset the channel to a known good channel */
200624034ddbSAdrian Chadd 			if (ieee80211_start_check_reset_chan(vap))
200724034ddbSAdrian Chadd 				ieee80211_start_reset_chan(vap);
200824034ddbSAdrian Chadd 
2009b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
2010b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
20117a79cebfSGleb Smirnoff 			    "%s: up parent %s\n", __func__, ic->ic_name);
20125efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
2013b032f27cSSam Leffler 			return;
2014b032f27cSSam Leffler 		}
2015b032f27cSSam Leffler 	}
2016b032f27cSSam Leffler 	/*
2017b032f27cSSam Leffler 	 * If the parent is up and running, then kick the
2018b032f27cSSam Leffler 	 * 802.11 state machine as appropriate.
2019b032f27cSSam Leffler 	 */
20207a79cebfSGleb Smirnoff 	if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
2021b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
2022b032f27cSSam Leffler #if 0
2023b032f27cSSam Leffler 			/* XXX bypasses scan too easily; disable for now */
2024b032f27cSSam Leffler 			/*
2025b032f27cSSam Leffler 			 * Try to be intelligent about clocking the state
2026b032f27cSSam Leffler 			 * machine.  If we're currently in RUN state then
2027b032f27cSSam Leffler 			 * we should be able to apply any new state/parameters
2028b032f27cSSam Leffler 			 * simply by re-associating.  Otherwise we need to
2029b032f27cSSam Leffler 			 * re-scan to select an appropriate ap.
2030b032f27cSSam Leffler 			 */
2031b032f27cSSam Leffler 			if (vap->iv_state >= IEEE80211_S_RUN)
2032b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
2033b032f27cSSam Leffler 				    IEEE80211_S_ASSOC, 1);
2034b032f27cSSam Leffler 			else
2035b032f27cSSam Leffler #endif
2036b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
2037b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
203868e8e04eSSam Leffler 		} else {
203968e8e04eSSam Leffler 			/*
2040b032f27cSSam Leffler 			 * For monitor+wds mode there's nothing to do but
2041b032f27cSSam Leffler 			 * start running.  Otherwise if this is the first
204268e8e04eSSam Leffler 			 * vap to be brought up, start a scan which may be
204368e8e04eSSam Leffler 			 * preempted if the station is locked to a particular
204468e8e04eSSam Leffler 			 * channel.
204568e8e04eSSam Leffler 			 */
20465efea30fSAndrew Thompson 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
2047b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
2048b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_WDS)
2049b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
2050b032f27cSSam Leffler 				    IEEE80211_S_RUN, -1);
2051b032f27cSSam Leffler 			else
2052b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
2053b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
205468e8e04eSSam Leffler 		}
205568e8e04eSSam Leffler 	}
2056b032f27cSSam Leffler }
2057b032f27cSSam Leffler 
2058b032f27cSSam Leffler /*
2059b032f27cSSam Leffler  * Start a single vap.
2060b032f27cSSam Leffler  */
2061b032f27cSSam Leffler void
ieee80211_init(void * arg)2062b032f27cSSam Leffler ieee80211_init(void *arg)
2063b032f27cSSam Leffler {
2064b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
2065b032f27cSSam Leffler 
206635f434b2SSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2067b032f27cSSam Leffler 	    "%s\n", __func__);
2068b032f27cSSam Leffler 
2069b032f27cSSam Leffler 	IEEE80211_LOCK(vap->iv_ic);
2070b032f27cSSam Leffler 	ieee80211_start_locked(vap);
2071b032f27cSSam Leffler 	IEEE80211_UNLOCK(vap->iv_ic);
2072b032f27cSSam Leffler }
2073b032f27cSSam Leffler 
2074b032f27cSSam Leffler /*
2075b032f27cSSam Leffler  * Start all runnable vap's on a device.
2076b032f27cSSam Leffler  */
2077b032f27cSSam Leffler void
ieee80211_start_all(struct ieee80211com * ic)2078b032f27cSSam Leffler ieee80211_start_all(struct ieee80211com *ic)
2079b032f27cSSam Leffler {
2080b032f27cSSam Leffler 	struct ieee80211vap *vap;
2081b032f27cSSam Leffler 
2082b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2083b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2084b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
2085b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
2086b032f27cSSam Leffler 			ieee80211_start_locked(vap);
2087b032f27cSSam Leffler 	}
2088b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2089b032f27cSSam Leffler }
2090b032f27cSSam Leffler 
2091b032f27cSSam Leffler /*
2092b032f27cSSam Leffler  * Stop a vap.  We force it down using the state machine
2093b032f27cSSam Leffler  * then mark it's ifnet not running.  If this is the last
2094b032f27cSSam Leffler  * vap running on the underlying device then we close it
2095b032f27cSSam Leffler  * too to insure it will be properly initialized when the
2096b032f27cSSam Leffler  * next vap is brought up.
2097b032f27cSSam Leffler  */
2098b032f27cSSam Leffler void
ieee80211_stop_locked(struct ieee80211vap * vap)2099b032f27cSSam Leffler ieee80211_stop_locked(struct ieee80211vap *vap)
2100b032f27cSSam Leffler {
2101b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2102b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2103b032f27cSSam Leffler 
2104b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2105b032f27cSSam Leffler 
2106b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2107b032f27cSSam Leffler 	    "stop running, %d vaps running\n", ic->ic_nrunning);
2108b032f27cSSam Leffler 
2109b032f27cSSam Leffler 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
2110b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2111b032f27cSSam Leffler 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
21121bcd230fSAlexander V. Chernikov 		ieee80211_notify_ifnet_change(vap, IFF_DRV_RUNNING);
21137a79cebfSGleb Smirnoff 		if (--ic->ic_nrunning == 0) {
2114b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
2115b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
21167a79cebfSGleb Smirnoff 			    "down parent %s\n", ic->ic_name);
21175efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
2118b032f27cSSam Leffler 		}
2119b032f27cSSam Leffler 	}
2120b032f27cSSam Leffler }
2121b032f27cSSam Leffler 
2122b032f27cSSam Leffler void
ieee80211_stop(struct ieee80211vap * vap)2123b032f27cSSam Leffler ieee80211_stop(struct ieee80211vap *vap)
2124b032f27cSSam Leffler {
2125b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2126b032f27cSSam Leffler 
2127b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2128b032f27cSSam Leffler 	ieee80211_stop_locked(vap);
2129b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2130b032f27cSSam Leffler }
2131b032f27cSSam Leffler 
2132b032f27cSSam Leffler /*
2133b032f27cSSam Leffler  * Stop all vap's running on a device.
2134b032f27cSSam Leffler  */
2135b032f27cSSam Leffler void
ieee80211_stop_all(struct ieee80211com * ic)2136b032f27cSSam Leffler ieee80211_stop_all(struct ieee80211com *ic)
2137b032f27cSSam Leffler {
2138b032f27cSSam Leffler 	struct ieee80211vap *vap;
2139b032f27cSSam Leffler 
2140b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2141b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2142b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
2143b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
2144b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
2145b032f27cSSam Leffler 	}
2146b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2147ae55932eSAndrew Thompson 
2148ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
214968e8e04eSSam Leffler }
215068e8e04eSSam Leffler 
215168e8e04eSSam Leffler /*
21526076cbacSSam Leffler  * Stop all vap's running on a device and arrange
21536076cbacSSam Leffler  * for those that were running to be resumed.
21546076cbacSSam Leffler  */
21556076cbacSSam Leffler void
ieee80211_suspend_all(struct ieee80211com * ic)21566076cbacSSam Leffler ieee80211_suspend_all(struct ieee80211com *ic)
21576076cbacSSam Leffler {
21586076cbacSSam Leffler 	struct ieee80211vap *vap;
21596076cbacSSam Leffler 
21606076cbacSSam Leffler 	IEEE80211_LOCK(ic);
21616076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
21626076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
21636076cbacSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
21646076cbacSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
21656076cbacSSam Leffler 			ieee80211_stop_locked(vap);
21666076cbacSSam Leffler 		}
21676076cbacSSam Leffler 	}
21686076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
2169ae55932eSAndrew Thompson 
2170ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
21716076cbacSSam Leffler }
21726076cbacSSam Leffler 
21736076cbacSSam Leffler /*
21746076cbacSSam Leffler  * Start all vap's marked for resume.
21756076cbacSSam Leffler  */
21766076cbacSSam Leffler void
ieee80211_resume_all(struct ieee80211com * ic)21776076cbacSSam Leffler ieee80211_resume_all(struct ieee80211com *ic)
21786076cbacSSam Leffler {
21796076cbacSSam Leffler 	struct ieee80211vap *vap;
21806076cbacSSam Leffler 
21816076cbacSSam Leffler 	IEEE80211_LOCK(ic);
21826076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
21836076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
21846076cbacSSam Leffler 		if (!IFNET_IS_UP_RUNNING(ifp) &&
21856076cbacSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
21866076cbacSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
21876076cbacSSam Leffler 			ieee80211_start_locked(vap);
21886076cbacSSam Leffler 		}
21896076cbacSSam Leffler 	}
21906076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
21916076cbacSSam Leffler }
21926076cbacSSam Leffler 
21934061c639SAndriy Voskoboinyk /*
21944061c639SAndriy Voskoboinyk  * Restart all vap's running on a device.
21954061c639SAndriy Voskoboinyk  */
21964061c639SAndriy Voskoboinyk void
ieee80211_restart_all(struct ieee80211com * ic)21974061c639SAndriy Voskoboinyk ieee80211_restart_all(struct ieee80211com *ic)
21984061c639SAndriy Voskoboinyk {
21994061c639SAndriy Voskoboinyk 	/*
22004061c639SAndriy Voskoboinyk 	 * NB: do not use ieee80211_runtask here, we will
22014061c639SAndriy Voskoboinyk 	 * block & drain net80211 taskqueue.
22024061c639SAndriy Voskoboinyk 	 */
22034061c639SAndriy Voskoboinyk 	taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
22044061c639SAndriy Voskoboinyk }
22054061c639SAndriy Voskoboinyk 
2206e701e041SSam Leffler void
ieee80211_beacon_miss(struct ieee80211com * ic)2207e701e041SSam Leffler ieee80211_beacon_miss(struct ieee80211com *ic)
2208e701e041SSam Leffler {
22095efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
22105efea30fSAndrew Thompson 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
22115efea30fSAndrew Thompson 		/* Process in a taskq, the handler may reenter the driver */
22125efea30fSAndrew Thompson 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
22135efea30fSAndrew Thompson 	}
22145efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
22155efea30fSAndrew Thompson }
22165efea30fSAndrew Thompson 
22175efea30fSAndrew Thompson static void
beacon_miss(void * arg,int npending)22185efea30fSAndrew Thompson beacon_miss(void *arg, int npending)
22195efea30fSAndrew Thompson {
22205efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
2221b032f27cSSam Leffler 	struct ieee80211vap *vap;
2222e701e041SSam Leffler 
222323401900SAdrian Chadd 	IEEE80211_LOCK(ic);
2224b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2225e701e041SSam Leffler 		/*
2226d8c364fbSAndriy Voskoboinyk 		 * We only pass events through for sta vap's in RUN+ state;
2227b032f27cSSam Leffler 		 * may be too restrictive but for now this saves all the
2228b032f27cSSam Leffler 		 * handlers duplicating these checks.
2229e701e041SSam Leffler 		 */
2230b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
2231c70761e6SSam Leffler 		    vap->iv_state >= IEEE80211_S_RUN &&
2232b032f27cSSam Leffler 		    vap->iv_bmiss != NULL)
2233b032f27cSSam Leffler 			vap->iv_bmiss(vap);
2234e701e041SSam Leffler 	}
223523401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
223668e8e04eSSam Leffler }
2237e701e041SSam Leffler 
22385efea30fSAndrew Thompson static void
beacon_swmiss(void * arg,int npending)22395efea30fSAndrew Thompson beacon_swmiss(void *arg, int npending)
22405efea30fSAndrew Thompson {
22415efea30fSAndrew Thompson 	struct ieee80211vap *vap = arg;
224223401900SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
22435efea30fSAndrew Thompson 
224423401900SAdrian Chadd 	IEEE80211_LOCK(ic);
2245d8c364fbSAndriy Voskoboinyk 	if (vap->iv_state >= IEEE80211_S_RUN) {
22465efea30fSAndrew Thompson 		/* XXX Call multiple times if npending > zero? */
22475efea30fSAndrew Thompson 		vap->iv_bmiss(vap);
22485efea30fSAndrew Thompson 	}
224923401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
225023401900SAdrian Chadd }
22515efea30fSAndrew Thompson 
2252e99662a6SSam Leffler /*
2253e99662a6SSam Leffler  * Software beacon miss handling.  Check if any beacons
2254e99662a6SSam Leffler  * were received in the last period.  If not post a
2255e99662a6SSam Leffler  * beacon miss; otherwise reset the counter.
2256e99662a6SSam Leffler  */
2257b032f27cSSam Leffler void
ieee80211_swbmiss(void * arg)2258e99662a6SSam Leffler ieee80211_swbmiss(void *arg)
2259e99662a6SSam Leffler {
2260b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
2261c448998dSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2262e99662a6SSam Leffler 
226323401900SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
226423401900SAdrian Chadd 
2265d8c364fbSAndriy Voskoboinyk 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2266c448998dSSam Leffler 	    ("wrong state %d", vap->iv_state));
2267c448998dSSam Leffler 
2268c448998dSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN) {
2269c448998dSSam Leffler 		/*
2270c448998dSSam Leffler 		 * If scanning just ignore and reset state.  If we get a
2271c448998dSSam Leffler 		 * bmiss after coming out of scan because we haven't had
2272c448998dSSam Leffler 		 * time to receive a beacon then we should probe the AP
2273c448998dSSam Leffler 		 * before posting a real bmiss (unless iv_bmiss_max has
2274c448998dSSam Leffler 		 * been artifiically lowered).  A cleaner solution might
2275c448998dSSam Leffler 		 * be to disable the timer on scan start/end but to handle
2276c448998dSSam Leffler 		 * case of multiple sta vap's we'd need to disable the
2277c448998dSSam Leffler 		 * timers of all affected vap's.
2278c448998dSSam Leffler 		 */
2279c448998dSSam Leffler 		vap->iv_swbmiss_count = 0;
2280c448998dSSam Leffler 	} else if (vap->iv_swbmiss_count == 0) {
2281b032f27cSSam Leffler 		if (vap->iv_bmiss != NULL)
22825efea30fSAndrew Thompson 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
2283e99662a6SSam Leffler 	} else
2284b032f27cSSam Leffler 		vap->iv_swbmiss_count = 0;
2285b032f27cSSam Leffler 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
2286b032f27cSSam Leffler 		ieee80211_swbmiss, vap);
22877edb8cf9SSam Leffler }
22887edb8cf9SSam Leffler 
228968e8e04eSSam Leffler /*
2290b032f27cSSam Leffler  * Start an 802.11h channel switch.  We record the parameters,
2291b032f27cSSam Leffler  * mark the operation pending, notify each vap through the
2292b032f27cSSam Leffler  * beacon update mechanism so it can update the beacon frame
2293b032f27cSSam Leffler  * contents, and then switch vap's to CSA state to block outbound
2294b032f27cSSam Leffler  * traffic.  Devices that handle CSA directly can use the state
2295b032f27cSSam Leffler  * switch to do the right thing so long as they call
2296b032f27cSSam Leffler  * ieee80211_csa_completeswitch when it's time to complete the
2297b032f27cSSam Leffler  * channel change.  Devices that depend on the net80211 layer can
2298b032f27cSSam Leffler  * use ieee80211_beacon_update to handle the countdown and the
2299b032f27cSSam Leffler  * channel switch.
2300b032f27cSSam Leffler  */
2301b032f27cSSam Leffler void
ieee80211_csa_startswitch(struct ieee80211com * ic,struct ieee80211_channel * c,int mode,int count)2302b032f27cSSam Leffler ieee80211_csa_startswitch(struct ieee80211com *ic,
2303b032f27cSSam Leffler 	struct ieee80211_channel *c, int mode, int count)
2304b032f27cSSam Leffler {
2305b032f27cSSam Leffler 	struct ieee80211vap *vap;
2306b032f27cSSam Leffler 
2307b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2308b032f27cSSam Leffler 
2309b032f27cSSam Leffler 	ic->ic_csa_newchan = c;
2310c70761e6SSam Leffler 	ic->ic_csa_mode = mode;
2311b032f27cSSam Leffler 	ic->ic_csa_count = count;
2312b032f27cSSam Leffler 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
2313b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2314b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
231559aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_IBSS ||
231659aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_MBSS)
2317b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
2318b032f27cSSam Leffler 		/* switch to CSA state to block outbound traffic */
2319b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN)
2320b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
2321b032f27cSSam Leffler 	}
2322b032f27cSSam Leffler 	ieee80211_notify_csa(ic, c, mode, count);
2323b032f27cSSam Leffler }
2324b032f27cSSam Leffler 
2325886bbec1SAdrian Chadd /*
2326886bbec1SAdrian Chadd  * Complete the channel switch by transitioning all CSA VAPs to RUN.
2327886bbec1SAdrian Chadd  * This is called by both the completion and cancellation functions
2328886bbec1SAdrian Chadd  * so each VAP is placed back in the RUN state and can thus transmit.
2329886bbec1SAdrian Chadd  */
2330c70761e6SSam Leffler static void
csa_completeswitch(struct ieee80211com * ic)2331c70761e6SSam Leffler csa_completeswitch(struct ieee80211com *ic)
2332c70761e6SSam Leffler {
2333c70761e6SSam Leffler 	struct ieee80211vap *vap;
2334c70761e6SSam Leffler 
2335c70761e6SSam Leffler 	ic->ic_csa_newchan = NULL;
2336c70761e6SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
2337c70761e6SSam Leffler 
2338c70761e6SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2339c70761e6SSam Leffler 		if (vap->iv_state == IEEE80211_S_CSA)
2340c70761e6SSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2341c70761e6SSam Leffler }
2342c70761e6SSam Leffler 
2343b032f27cSSam Leffler /*
2344b032f27cSSam Leffler  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
2345b032f27cSSam Leffler  * We clear state and move all vap's in CSA state to RUN state
2346b032f27cSSam Leffler  * so they can again transmit.
2347886bbec1SAdrian Chadd  *
2348886bbec1SAdrian Chadd  * Although this may not be completely correct, update the BSS channel
2349886bbec1SAdrian Chadd  * for each VAP to the newly configured channel. The setcurchan sets
2350886bbec1SAdrian Chadd  * the current operating channel for the interface (so the radio does
2351886bbec1SAdrian Chadd  * switch over) but the VAP BSS isn't updated, leading to incorrectly
2352886bbec1SAdrian Chadd  * reported information via ioctl.
2353b032f27cSSam Leffler  */
2354b032f27cSSam Leffler void
ieee80211_csa_completeswitch(struct ieee80211com * ic)2355b032f27cSSam Leffler ieee80211_csa_completeswitch(struct ieee80211com *ic)
2356b032f27cSSam Leffler {
23576f16ec31SAdrian Chadd 	struct ieee80211vap *vap;
23586f16ec31SAdrian Chadd 
2359b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2360b032f27cSSam Leffler 
2361b032f27cSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
2362b032f27cSSam Leffler 
2363b032f27cSSam Leffler 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
2364886bbec1SAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2365886bbec1SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_CSA)
2366886bbec1SAdrian Chadd 			vap->iv_bss->ni_chan = ic->ic_curchan;
2367886bbec1SAdrian Chadd 
2368c70761e6SSam Leffler 	csa_completeswitch(ic);
2369c70761e6SSam Leffler }
2370b032f27cSSam Leffler 
2371c70761e6SSam Leffler /*
2372c70761e6SSam Leffler  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
2373c70761e6SSam Leffler  * We clear state and move all vap's in CSA state to RUN state
2374c70761e6SSam Leffler  * so they can again transmit.
2375c70761e6SSam Leffler  */
2376c70761e6SSam Leffler void
ieee80211_csa_cancelswitch(struct ieee80211com * ic)2377c70761e6SSam Leffler ieee80211_csa_cancelswitch(struct ieee80211com *ic)
2378c70761e6SSam Leffler {
2379c70761e6SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2380c70761e6SSam Leffler 
2381c70761e6SSam Leffler 	csa_completeswitch(ic);
2382b032f27cSSam Leffler }
2383b032f27cSSam Leffler 
2384b032f27cSSam Leffler /*
2385b032f27cSSam Leffler  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
2386b032f27cSSam Leffler  * We clear state and move all vap's in CAC state to RUN state.
2387b032f27cSSam Leffler  */
2388b032f27cSSam Leffler void
ieee80211_cac_completeswitch(struct ieee80211vap * vap0)2389b032f27cSSam Leffler ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
2390b032f27cSSam Leffler {
2391b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
2392b032f27cSSam Leffler 	struct ieee80211vap *vap;
2393b032f27cSSam Leffler 
2394b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2395b032f27cSSam Leffler 	/*
2396b032f27cSSam Leffler 	 * Complete CAC state change for lead vap first; then
2397b032f27cSSam Leffler 	 * clock all the other vap's waiting.
2398b032f27cSSam Leffler 	 */
2399b032f27cSSam Leffler 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
2400b032f27cSSam Leffler 	    ("wrong state %d", vap0->iv_state));
2401b032f27cSSam Leffler 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
2402b032f27cSSam Leffler 
2403b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2404e0625c4cSAndriy Voskoboinyk 		if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
2405b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2406b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2407b032f27cSSam Leffler }
2408b032f27cSSam Leffler 
2409b032f27cSSam Leffler /*
2410b032f27cSSam Leffler  * Force all vap's other than the specified vap to the INIT state
2411b032f27cSSam Leffler  * and mark them as waiting for a scan to complete.  These vaps
2412b032f27cSSam Leffler  * will be brought up when the scan completes and the scanning vap
2413b032f27cSSam Leffler  * reaches RUN state by wakeupwaiting.
241468e8e04eSSam Leffler  */
241568e8e04eSSam Leffler static void
markwaiting(struct ieee80211vap * vap0)2416b032f27cSSam Leffler markwaiting(struct ieee80211vap *vap0)
241768e8e04eSSam Leffler {
2418b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
2419b032f27cSSam Leffler 	struct ieee80211vap *vap;
2420b032f27cSSam Leffler 
2421b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2422b032f27cSSam Leffler 
24235efea30fSAndrew Thompson 	/*
24245efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
24255efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
24265efea30fSAndrew Thompson 	 * change task.
24275efea30fSAndrew Thompson 	 */
2428b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2429b032f27cSSam Leffler 		if (vap == vap0)
2430b032f27cSSam Leffler 			continue;
2431b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_INIT) {
24325efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
2433b032f27cSSam Leffler 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
2434dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
2435b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2436b032f27cSSam Leffler 		}
243768e8e04eSSam Leffler 	}
243868e8e04eSSam Leffler }
243968e8e04eSSam Leffler 
2440b032f27cSSam Leffler /*
2441b032f27cSSam Leffler  * Wakeup all vap's waiting for a scan to complete.  This is the
2442b032f27cSSam Leffler  * companion to markwaiting (above) and is used to coordinate
2443b032f27cSSam Leffler  * multiple vaps scanning.
24445efea30fSAndrew Thompson  * This is called from the state taskqueue.
2445b032f27cSSam Leffler  */
2446b032f27cSSam Leffler static void
wakeupwaiting(struct ieee80211vap * vap0)2447b032f27cSSam Leffler wakeupwaiting(struct ieee80211vap *vap0)
2448b032f27cSSam Leffler {
2449b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
2450b032f27cSSam Leffler 	struct ieee80211vap *vap;
2451b032f27cSSam Leffler 
2452b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2453b032f27cSSam Leffler 
24545efea30fSAndrew Thompson 	/*
24555efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
24565efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
24575efea30fSAndrew Thompson 	 * change task.
24585efea30fSAndrew Thompson 	 */
2459b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2460b032f27cSSam Leffler 		if (vap == vap0)
2461b032f27cSSam Leffler 			continue;
2462b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
2463b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2464b032f27cSSam Leffler 			/* NB: sta's cannot go INIT->RUN */
24655efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
2466e8de31caSAdrian Chadd 
2467e8de31caSAdrian Chadd 			/*
2468e8de31caSAdrian Chadd 			 * This is problematic if the interface has OACTIVE
2469e8de31caSAdrian Chadd 			 * set.  Only the deferred ieee80211_newstate_cb()
2470e8de31caSAdrian Chadd 			 * will end up actually /clearing/ the OACTIVE
2471e8de31caSAdrian Chadd 			 * flag on a state transition to RUN from a non-RUN
2472e8de31caSAdrian Chadd 			 * state.
2473e8de31caSAdrian Chadd 			 *
2474e8de31caSAdrian Chadd 			 * But, we're not actually deferring this callback;
2475e8de31caSAdrian Chadd 			 * and when the deferred call occurs it shows up as
2476e8de31caSAdrian Chadd 			 * a RUN->RUN transition!  So the flag isn't/wasn't
2477e8de31caSAdrian Chadd 			 * cleared!
2478e8de31caSAdrian Chadd 			 *
2479e8de31caSAdrian Chadd 			 * I'm also not sure if it's correct to actually
2480e8de31caSAdrian Chadd 			 * do the transitions here fully through the deferred
2481e8de31caSAdrian Chadd 			 * paths either as other things can be invoked as
2482e8de31caSAdrian Chadd 			 * part of that state machine.
2483e8de31caSAdrian Chadd 			 *
2484e8de31caSAdrian Chadd 			 * So just keep this in mind when looking at what
2485e8de31caSAdrian Chadd 			 * the markwaiting/wakeupwaiting routines are doing
2486e8de31caSAdrian Chadd 			 * and how they invoke vap state changes.
2487e8de31caSAdrian Chadd 			 */
2488e8de31caSAdrian Chadd 
2489b032f27cSSam Leffler 			vap->iv_newstate(vap,
2490b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_STA ?
2491b032f27cSSam Leffler 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
2492dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
2493b032f27cSSam Leffler 		}
2494b032f27cSSam Leffler 	}
2495b032f27cSSam Leffler }
2496b032f27cSSam Leffler 
2497*713db49dSBjoern A. Zeeb static int
_ieee80211_newstate_get_next_empty_slot(struct ieee80211vap * vap)2498*713db49dSBjoern A. Zeeb _ieee80211_newstate_get_next_empty_slot(struct ieee80211vap *vap)
2499*713db49dSBjoern A. Zeeb {
2500*713db49dSBjoern A. Zeeb 	int nstate_num;
2501*713db49dSBjoern A. Zeeb 
2502*713db49dSBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2503*713db49dSBjoern A. Zeeb 
2504*713db49dSBjoern A. Zeeb 	if (vap->iv_nstate_n >= NET80211_IV_NSTATE_NUM)
2505*713db49dSBjoern A. Zeeb 		return (-1);
2506*713db49dSBjoern A. Zeeb 
2507*713db49dSBjoern A. Zeeb 	nstate_num = vap->iv_nstate_b + vap->iv_nstate_n;
2508*713db49dSBjoern A. Zeeb 	nstate_num %= NET80211_IV_NSTATE_NUM;
2509*713db49dSBjoern A. Zeeb 	vap->iv_nstate_n++;
2510*713db49dSBjoern A. Zeeb 
2511*713db49dSBjoern A. Zeeb 	return (nstate_num);
2512*713db49dSBjoern A. Zeeb }
2513*713db49dSBjoern A. Zeeb 
2514*713db49dSBjoern A. Zeeb static int
_ieee80211_newstate_get_next_pending_slot(struct ieee80211vap * vap)2515*713db49dSBjoern A. Zeeb _ieee80211_newstate_get_next_pending_slot(struct ieee80211vap *vap)
2516*713db49dSBjoern A. Zeeb {
2517*713db49dSBjoern A. Zeeb 	int nstate_num;
2518*713db49dSBjoern A. Zeeb 
2519*713db49dSBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2520*713db49dSBjoern A. Zeeb 
2521*713db49dSBjoern A. Zeeb 	KASSERT(vap->iv_nstate_n > 0, ("%s: vap %p iv_nstate_n %d\n",
2522*713db49dSBjoern A. Zeeb 	    __func__, vap, vap->iv_nstate_n));
2523*713db49dSBjoern A. Zeeb 
2524*713db49dSBjoern A. Zeeb 	nstate_num = vap->iv_nstate_b;
2525*713db49dSBjoern A. Zeeb 	vap->iv_nstate_b++;
2526*713db49dSBjoern A. Zeeb 	if (vap->iv_nstate_b >= NET80211_IV_NSTATE_NUM)
2527*713db49dSBjoern A. Zeeb 		vap->iv_nstate_b = 0;
2528*713db49dSBjoern A. Zeeb 	vap->iv_nstate_n--;
2529*713db49dSBjoern A. Zeeb 
2530*713db49dSBjoern A. Zeeb 	return (nstate_num);
2531*713db49dSBjoern A. Zeeb }
2532*713db49dSBjoern A. Zeeb 
2533*713db49dSBjoern A. Zeeb static int
_ieee80211_newstate_get_npending(struct ieee80211vap * vap)2534*713db49dSBjoern A. Zeeb _ieee80211_newstate_get_npending(struct ieee80211vap *vap)
2535*713db49dSBjoern A. Zeeb {
2536*713db49dSBjoern A. Zeeb 
2537*713db49dSBjoern A. Zeeb 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2538*713db49dSBjoern A. Zeeb 
2539*713db49dSBjoern A. Zeeb 	return (vap->iv_nstate_n);
2540*713db49dSBjoern A. Zeeb }
2541*713db49dSBjoern A. Zeeb 
2542b032f27cSSam Leffler /*
2543b032f27cSSam Leffler  * Handle post state change work common to all operating modes.
2544b032f27cSSam Leffler  */
2545b032f27cSSam Leffler static void
ieee80211_newstate_cb(void * xvap,int npending)25465efea30fSAndrew Thompson ieee80211_newstate_cb(void *xvap, int npending)
2547b032f27cSSam Leffler {
25485efea30fSAndrew Thompson 	struct ieee80211vap *vap = xvap;
2549b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
25505efea30fSAndrew Thompson 	enum ieee80211_state nstate, ostate;
2551*713db49dSBjoern A. Zeeb 	int arg, rc, nstate_num;
2552b032f27cSSam Leffler 
2553*713db49dSBjoern A. Zeeb 	KASSERT(npending == 1, ("%s: vap %p with npending %d != 1\n",
2554*713db49dSBjoern A. Zeeb 	    __func__, vap, npending));
25555efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
2556*713db49dSBjoern A. Zeeb 	nstate_num = _ieee80211_newstate_get_next_pending_slot(vap);
2557*713db49dSBjoern A. Zeeb 
2558*713db49dSBjoern A. Zeeb 	/*
2559*713db49dSBjoern A. Zeeb 	 * Update the historic fields for now as they are used in some
2560*713db49dSBjoern A. Zeeb 	 * drivers and reduce code changes for now.
2561*713db49dSBjoern A. Zeeb 	 */
2562*713db49dSBjoern A. Zeeb 	vap->iv_nstate = nstate = vap->iv_nstates[nstate_num];
2563*713db49dSBjoern A. Zeeb 	arg = vap->iv_nstate_args[nstate_num];
2564b032f27cSSam Leffler 
256572bb33a3SBjoern A. Zeeb 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
256672bb33a3SBjoern A. Zeeb 	    "%s:%d: running state update %s -> %s (%d)\n",
256772bb33a3SBjoern A. Zeeb 	    __func__, __LINE__,
256872bb33a3SBjoern A. Zeeb 	    ieee80211_state_name[vap->iv_state],
2569*713db49dSBjoern A. Zeeb 	    ieee80211_state_name[nstate],
257072bb33a3SBjoern A. Zeeb 	    npending);
257172bb33a3SBjoern A. Zeeb 
25725efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
25735efea30fSAndrew Thompson 		/*
25745efea30fSAndrew Thompson 		 * We have been requested to drop back to the INIT before
25755efea30fSAndrew Thompson 		 * proceeding to the new state.
25765efea30fSAndrew Thompson 		 */
2577d13806f4SAndriy Voskoboinyk 		/* Deny any state changes while we are here. */
2578d13806f4SAndriy Voskoboinyk 		vap->iv_nstate = IEEE80211_S_INIT;
2579b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2580*713db49dSBjoern A. Zeeb 		    "%s: %s -> %s arg %d -> %s arg %d\n", __func__,
25815efea30fSAndrew Thompson 		    ieee80211_state_name[vap->iv_state],
2582*713db49dSBjoern A. Zeeb 		    ieee80211_state_name[vap->iv_nstate], 0,
2583*713db49dSBjoern A. Zeeb 		    ieee80211_state_name[nstate], arg);
2584d13806f4SAndriy Voskoboinyk 		vap->iv_newstate(vap, vap->iv_nstate, 0);
2585dcc56af0SAdrian Chadd 		IEEE80211_LOCK_ASSERT(ic);
2586d13806f4SAndriy Voskoboinyk 		vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
2587d13806f4SAndriy Voskoboinyk 		    IEEE80211_FEXT_STATEWAIT);
2588d13806f4SAndriy Voskoboinyk 		/* enqueue new state transition after cancel_scan() task */
2589d13806f4SAndriy Voskoboinyk 		ieee80211_new_state_locked(vap, nstate, arg);
2590d13806f4SAndriy Voskoboinyk 		goto done;
25915efea30fSAndrew Thompson 	}
25925efea30fSAndrew Thompson 
25935efea30fSAndrew Thompson 	ostate = vap->iv_state;
25945efea30fSAndrew Thompson 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
25955efea30fSAndrew Thompson 		/*
25965efea30fSAndrew Thompson 		 * SCAN was forced; e.g. on beacon miss.  Force other running
25975efea30fSAndrew Thompson 		 * vap's to INIT state and mark them as waiting for the scan to
25985efea30fSAndrew Thompson 		 * complete.  This insures they don't interfere with our
25995efea30fSAndrew Thompson 		 * scanning.  Since we are single threaded the vaps can not
26005efea30fSAndrew Thompson 		 * transition again while we are executing.
26015efea30fSAndrew Thompson 		 *
26025efea30fSAndrew Thompson 		 * XXX not always right, assumes ap follows sta
26035efea30fSAndrew Thompson 		 */
26045efea30fSAndrew Thompson 		markwaiting(vap);
26055efea30fSAndrew Thompson 	}
26065efea30fSAndrew Thompson 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
26075efea30fSAndrew Thompson 	    "%s: %s -> %s arg %d\n", __func__,
26085efea30fSAndrew Thompson 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
26095efea30fSAndrew Thompson 
26105efea30fSAndrew Thompson 	rc = vap->iv_newstate(vap, nstate, arg);
2611dcc56af0SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
26125efea30fSAndrew Thompson 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
26135efea30fSAndrew Thompson 	if (rc != 0) {
26145efea30fSAndrew Thompson 		/* State transition failed */
26155efea30fSAndrew Thompson 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
26165efea30fSAndrew Thompson 		KASSERT(nstate != IEEE80211_S_INIT,
26175efea30fSAndrew Thompson 		    ("INIT state change failed"));
26185efea30fSAndrew Thompson 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
26195efea30fSAndrew Thompson 		    "%s: %s returned error %d\n", __func__,
26205efea30fSAndrew Thompson 		    ieee80211_state_name[nstate], rc);
26215efea30fSAndrew Thompson 		goto done;
26225efea30fSAndrew Thompson 	}
26235efea30fSAndrew Thompson 
2624e8de31caSAdrian Chadd 	/*
2625e8de31caSAdrian Chadd 	 * Handle the case of a RUN->RUN transition occuring when STA + AP
2626e8de31caSAdrian Chadd 	 * VAPs occur on the same radio.
2627e8de31caSAdrian Chadd 	 *
2628e8de31caSAdrian Chadd 	 * The mark and wakeup waiting routines call iv_newstate() directly,
2629e8de31caSAdrian Chadd 	 * but they do not end up deferring state changes here.
2630e8de31caSAdrian Chadd 	 * Thus, although the VAP newstate method sees a transition
2631e8de31caSAdrian Chadd 	 * of RUN->INIT->RUN, the deferred path here only sees a RUN->RUN
2632e8de31caSAdrian Chadd 	 * transition.  If OACTIVE is set then it is never cleared.
2633e8de31caSAdrian Chadd 	 *
2634e8de31caSAdrian Chadd 	 * So, if we're here and the state is RUN, just clear OACTIVE.
2635e8de31caSAdrian Chadd 	 * At some point if the markwaiting/wakeupwaiting paths end up
2636e8de31caSAdrian Chadd 	 * also invoking the deferred state updates then this will
2637e8de31caSAdrian Chadd 	 * be no-op code - and also if OACTIVE is finally retired, it'll
2638e8de31caSAdrian Chadd 	 * also be no-op code.
2639e8de31caSAdrian Chadd 	 */
2640e8de31caSAdrian Chadd 	if (nstate == IEEE80211_S_RUN) {
2641e8de31caSAdrian Chadd 		/*
2642464907ceSBjoern A. Zeeb 		 * OACTIVE may be set on the vap if the upper layer
2643464907ceSBjoern A. Zeeb 		 * tried to transmit (e.g. IPv6 NDP) before we reach
2644464907ceSBjoern A. Zeeb 		 * RUN state.  Clear it and restart xmit.
2645464907ceSBjoern A. Zeeb 		 *
2646464907ceSBjoern A. Zeeb 		 * Note this can also happen as a result of SLEEP->RUN
2647464907ceSBjoern A. Zeeb 		 * (i.e. coming out of power save mode).
2648464907ceSBjoern A. Zeeb 		 *
2649464907ceSBjoern A. Zeeb 		 * Historically this was done only for a state change
2650464907ceSBjoern A. Zeeb 		 * but is needed earlier; see next comment.  The 2nd half
2651464907ceSBjoern A. Zeeb 		 * of the work is still only done in case of an actual
2652464907ceSBjoern A. Zeeb 		 * state change below.
2653464907ceSBjoern A. Zeeb 		 */
2654464907ceSBjoern A. Zeeb 		/*
2655e8de31caSAdrian Chadd 		 * Unblock the VAP queue; a RUN->RUN state can happen
2656e8de31caSAdrian Chadd 		 * on a STA+AP setup on the AP vap.  See wakeupwaiting().
2657e8de31caSAdrian Chadd 		 */
2658e8de31caSAdrian Chadd 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2659464907ceSBjoern A. Zeeb 
2660464907ceSBjoern A. Zeeb 		/*
2661464907ceSBjoern A. Zeeb 		 * XXX TODO Kick-start a VAP queue - this should be a method!
2662464907ceSBjoern A. Zeeb 		 */
2663e8de31caSAdrian Chadd 	}
2664e8de31caSAdrian Chadd 
26655efea30fSAndrew Thompson 	/* No actual transition, skip post processing */
26665efea30fSAndrew Thompson 	if (ostate == nstate)
26675efea30fSAndrew Thompson 		goto done;
2668b032f27cSSam Leffler 
2669b032f27cSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
2670b032f27cSSam Leffler 
2671b032f27cSSam Leffler 		/* bring up any vaps waiting on us */
2672b032f27cSSam Leffler 		wakeupwaiting(vap);
2673b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
2674b032f27cSSam Leffler 		/*
2675b032f27cSSam Leffler 		 * Flush the scan cache if we did the last scan (XXX?)
2676b032f27cSSam Leffler 		 * and flush any frames on send queues from this vap.
2677b032f27cSSam Leffler 		 * Note the mgt q is used only for legacy drivers and
2678b032f27cSSam Leffler 		 * will go away shortly.
2679b032f27cSSam Leffler 		 */
2680b032f27cSSam Leffler 		ieee80211_scan_flush(vap);
2681b032f27cSSam Leffler 
2682e7495198SAdrian Chadd 		/*
2683e7495198SAdrian Chadd 		 * XXX TODO: ic/vap queue flush
2684e7495198SAdrian Chadd 		 */
2685b032f27cSSam Leffler 	}
26865efea30fSAndrew Thompson done:
26875efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
2688b032f27cSSam Leffler }
2689b032f27cSSam Leffler 
2690b032f27cSSam Leffler /*
2691b032f27cSSam Leffler  * Public interface for initiating a state machine change.
2692b032f27cSSam Leffler  * This routine single-threads the request and coordinates
2693b032f27cSSam Leffler  * the scheduling of multiple vaps for the purpose of selecting
2694b032f27cSSam Leffler  * an operating channel.  Specifically the following scenarios
2695b032f27cSSam Leffler  * are handled:
2696b032f27cSSam Leffler  * o only one vap can be selecting a channel so on transition to
2697b032f27cSSam Leffler  *   SCAN state if another vap is already scanning then
2698b032f27cSSam Leffler  *   mark the caller for later processing and return without
2699b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
2700b032f27cSSam Leffler  * o only one vap can be doing CAC of a channel so on transition to
2701b032f27cSSam Leffler  *   CAC state if another vap is already scanning for radar then
2702b032f27cSSam Leffler  *   mark the caller for later processing and return without
2703b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
2704b032f27cSSam Leffler  * o if another vap is already running when a request is made
2705b032f27cSSam Leffler  *   to SCAN then an operating channel has been chosen; bypass
2706b032f27cSSam Leffler  *   the scan and just join the channel
2707b032f27cSSam Leffler  *
2708b032f27cSSam Leffler  * Note that the state change call is done through the iv_newstate
2709b032f27cSSam Leffler  * method pointer so any driver routine gets invoked.  The driver
2710b032f27cSSam Leffler  * will normally call back into operating mode-specific
2711b032f27cSSam Leffler  * ieee80211_newstate routines (below) unless it needs to completely
2712b032f27cSSam Leffler  * bypass the state machine (e.g. because the firmware has it's
2713b032f27cSSam Leffler  * own idea how things should work).  Bypassing the net80211 layer
2714b032f27cSSam Leffler  * is usually a mistake and indicates lack of proper integration
2715b032f27cSSam Leffler  * with the net80211 layer.
2716b032f27cSSam Leffler  */
2717e94527beSAdrian Chadd int
ieee80211_new_state_locked(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2718b032f27cSSam Leffler ieee80211_new_state_locked(struct ieee80211vap *vap,
2719b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
27208a1b9b6aSSam Leffler {
2721b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2722b032f27cSSam Leffler 	struct ieee80211vap *vp;
2723a11c9a5cSSam Leffler 	enum ieee80211_state ostate;
2724*713db49dSBjoern A. Zeeb 	int nrunning, nscanning, nstate_num;
27251a1e1d21SSam Leffler 
2726b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2727b032f27cSSam Leffler 
27285efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2729d13806f4SAndriy Voskoboinyk 		if (vap->iv_nstate == IEEE80211_S_INIT ||
2730d13806f4SAndriy Voskoboinyk 		    ((vap->iv_state == IEEE80211_S_INIT ||
2731d13806f4SAndriy Voskoboinyk 		    (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2732d13806f4SAndriy Voskoboinyk 		    vap->iv_nstate == IEEE80211_S_SCAN &&
2733d13806f4SAndriy Voskoboinyk 		    nstate > IEEE80211_S_SCAN)) {
27345efea30fSAndrew Thompson 			/*
2735d13806f4SAndriy Voskoboinyk 			 * XXX The vap is being stopped/started,
2736d13806f4SAndriy Voskoboinyk 			 * do not allow any other state changes
2737d13806f4SAndriy Voskoboinyk 			 * until this is completed.
27385efea30fSAndrew Thompson 			 */
2739d13806f4SAndriy Voskoboinyk 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
274072bb33a3SBjoern A. Zeeb 			    "%s:%d: %s -> %s (%s) transition discarded\n",
274172bb33a3SBjoern A. Zeeb 			    __func__, __LINE__,
2742d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[vap->iv_state],
2743d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[nstate],
2744d13806f4SAndriy Voskoboinyk 			    ieee80211_state_name[vap->iv_nstate]);
27455efea30fSAndrew Thompson 			return -1;
27465efea30fSAndrew Thompson 		}
27478ee6f90aSAndrew Thompson 	}
27485efea30fSAndrew Thompson 
274972bb33a3SBjoern A. Zeeb 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
275072bb33a3SBjoern A. Zeeb 	    "%s:%d: starting state update %s -> %s (%s)\n",
275172bb33a3SBjoern A. Zeeb 	    __func__, __LINE__,
275272bb33a3SBjoern A. Zeeb 	    ieee80211_state_name[vap->iv_state],
275372bb33a3SBjoern A. Zeeb 	    ieee80211_state_name[vap->iv_nstate],
275472bb33a3SBjoern A. Zeeb 	    ieee80211_state_name[nstate]);
275572bb33a3SBjoern A. Zeeb 
2756b032f27cSSam Leffler 	nrunning = nscanning = 0;
2757b032f27cSSam Leffler 	/* XXX can track this state instead of calculating */
2758b032f27cSSam Leffler 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2759b032f27cSSam Leffler 		if (vp != vap) {
2760b032f27cSSam Leffler 			if (vp->iv_state >= IEEE80211_S_RUN)
2761b032f27cSSam Leffler 				nrunning++;
2762b032f27cSSam Leffler 			/* XXX doesn't handle bg scan */
2763b032f27cSSam Leffler 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
2764b032f27cSSam Leffler 			else if (vp->iv_state > IEEE80211_S_INIT)
2765b032f27cSSam Leffler 				nscanning++;
2766b032f27cSSam Leffler 		}
2767b032f27cSSam Leffler 	}
2768*713db49dSBjoern A. Zeeb 	/*
2769*713db49dSBjoern A. Zeeb 	 * Look ahead for the "old state" at that point when the last queued
2770*713db49dSBjoern A. Zeeb 	 * state transition is run.
2771*713db49dSBjoern A. Zeeb 	 */
2772*713db49dSBjoern A. Zeeb 	if (vap->iv_nstate_n == 0) {
2773b032f27cSSam Leffler 		ostate = vap->iv_state;
2774*713db49dSBjoern A. Zeeb 	} else {
2775*713db49dSBjoern A. Zeeb 		nstate_num = (vap->iv_nstate_b + vap->iv_nstate_n - 1) % NET80211_IV_NSTATE_NUM;
2776*713db49dSBjoern A. Zeeb 		ostate = vap->iv_nstates[nstate_num];
2777*713db49dSBjoern A. Zeeb 	}
2778b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
277904efa18fSBjoern A. Zeeb 	    "%s: %s -> %s (arg %d) (nrunning %d nscanning %d)\n", __func__,
278004efa18fSBjoern A. Zeeb 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg,
2781b032f27cSSam Leffler 	    nrunning, nscanning);
27821a1e1d21SSam Leffler 	switch (nstate) {
27831a1e1d21SSam Leffler 	case IEEE80211_S_SCAN:
2784b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT) {
27851a1e1d21SSam Leffler 			/*
2786b032f27cSSam Leffler 			 * INIT -> SCAN happens on initial bringup.
27871a1e1d21SSam Leffler 			 */
2788b032f27cSSam Leffler 			KASSERT(!(nscanning && nrunning),
2789b032f27cSSam Leffler 			    ("%d scanning and %d running", nscanning, nrunning));
2790b032f27cSSam Leffler 			if (nscanning) {
279168e8e04eSSam Leffler 				/*
2792b032f27cSSam Leffler 				 * Someone is scanning, defer our state
2793b032f27cSSam Leffler 				 * change until the work has completed.
279468e8e04eSSam Leffler 				 */
2795b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2796b032f27cSSam Leffler 				    "%s: defer %s -> %s\n",
2797b032f27cSSam Leffler 				    __func__, ieee80211_state_name[ostate],
2798b032f27cSSam Leffler 				    ieee80211_state_name[nstate]);
2799b032f27cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
28005efea30fSAndrew Thompson 				return 0;
280168e8e04eSSam Leffler 			}
2802b032f27cSSam Leffler 			if (nrunning) {
280368e8e04eSSam Leffler 				/*
2804b032f27cSSam Leffler 				 * Someone is operating; just join the channel
2805b032f27cSSam Leffler 				 * they have chosen.
280668e8e04eSSam Leffler 				 */
2807b032f27cSSam Leffler 				/* XXX kill arg? */
2808b032f27cSSam Leffler 				/* XXX check each opmode, adhoc? */
2809b032f27cSSam Leffler 				if (vap->iv_opmode == IEEE80211_M_STA)
2810b032f27cSSam Leffler 					nstate = IEEE80211_S_SCAN;
28111a1e1d21SSam Leffler 				else
2812b032f27cSSam Leffler 					nstate = IEEE80211_S_RUN;
2813b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
2814b032f27cSSam Leffler 				if (nstate != IEEE80211_S_SCAN) {
2815b032f27cSSam Leffler 					IEEE80211_DPRINTF(vap,
2816b032f27cSSam Leffler 					    IEEE80211_MSG_STATE,
2817b032f27cSSam Leffler 					    "%s: override, now %s -> %s\n",
2818b032f27cSSam Leffler 					    __func__,
2819b032f27cSSam Leffler 					    ieee80211_state_name[ostate],
2820b032f27cSSam Leffler 					    ieee80211_state_name[nstate]);
28211a1e1d21SSam Leffler 				}
28228a1b9b6aSSam Leffler #endif
282368e8e04eSSam Leffler 			}
2824b032f27cSSam Leffler 		}
28251a1e1d21SSam Leffler 		break;
2826b032f27cSSam Leffler 	case IEEE80211_S_RUN:
2827b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS &&
2828b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2829b032f27cSSam Leffler 		    nscanning) {
2830b032f27cSSam Leffler 			/*
2831b032f27cSSam Leffler 			 * Legacy WDS with someone else scanning; don't
2832b032f27cSSam Leffler 			 * go online until that completes as we should
2833b032f27cSSam Leffler 			 * follow the other vap to the channel they choose.
2834b032f27cSSam Leffler 			 */
2835b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2836b032f27cSSam Leffler 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
2837b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2838b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2839b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
28405efea30fSAndrew Thompson 			return 0;
2841b032f27cSSam Leffler 		}
2842b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2843b032f27cSSam Leffler 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2844b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2845b032f27cSSam Leffler 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2846b032f27cSSam Leffler 			/*
2847b032f27cSSam Leffler 			 * This is a DFS channel, transition to CAC state
2848b032f27cSSam Leffler 			 * instead of RUN.  This allows us to initiate
2849b032f27cSSam Leffler 			 * Channel Availability Check (CAC) as specified
2850b032f27cSSam Leffler 			 * by 11h/DFS.
2851b032f27cSSam Leffler 			 */
2852b032f27cSSam Leffler 			nstate = IEEE80211_S_CAC;
2853b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2854b032f27cSSam Leffler 			     "%s: override %s -> %s (DFS)\n", __func__,
2855b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2856b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2857b032f27cSSam Leffler 		}
2858b032f27cSSam Leffler 		break;
2859b032f27cSSam Leffler 	case IEEE80211_S_INIT:
2860b016f58cSAndrew Thompson 		/* cancel any scan in progress */
2861b016f58cSAndrew Thompson 		ieee80211_cancel_scan(vap);
2862b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT ) {
2863b032f27cSSam Leffler 			/* XXX don't believe this */
2864b032f27cSSam Leffler 			/* INIT -> INIT. nothing to do */
2865b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2866b032f27cSSam Leffler 		}
2867b032f27cSSam Leffler 		/* fall thru... */
286814fb6b8fSSam Leffler 	default:
286914fb6b8fSSam Leffler 		break;
28701a1e1d21SSam Leffler 	}
2871*713db49dSBjoern A. Zeeb 	/*
2872*713db49dSBjoern A. Zeeb 	 * Defer the state change to a thread.
2873*713db49dSBjoern A. Zeeb 	 * We support up-to NET80211_IV_NSTATE_NUM pending state changes
2874*713db49dSBjoern A. Zeeb 	 * using a separate task for each. Otherwise, if we enqueue
2875*713db49dSBjoern A. Zeeb 	 * more than one state change they will be folded together,
2876*713db49dSBjoern A. Zeeb 	 * npedning will be > 1 and we may run then out of sequence with
2877*713db49dSBjoern A. Zeeb 	 * other events.
2878*713db49dSBjoern A. Zeeb 	 * This is kind-of a hack after 10 years but we know how to provoke
2879*713db49dSBjoern A. Zeeb 	 * these cases now (and seen them in the wild).
2880*713db49dSBjoern A. Zeeb 	 */
2881*713db49dSBjoern A. Zeeb 	nstate_num = _ieee80211_newstate_get_next_empty_slot(vap);
2882*713db49dSBjoern A. Zeeb 	if (nstate_num == -1) {
2883*713db49dSBjoern A. Zeeb 		/*
2884*713db49dSBjoern A. Zeeb 		 * This is really bad and we should just go kaboom.
2885*713db49dSBjoern A. Zeeb 		 * Instead drop it.  No one checks the return code anyway.
2886*713db49dSBjoern A. Zeeb 		 */
2887*713db49dSBjoern A. Zeeb 		ic_printf(ic, "%s:%d: pending %s -> %s (now to %s) "
2888*713db49dSBjoern A. Zeeb 		    "transition lost. %d/%d pending state changes:\n",
2889*713db49dSBjoern A. Zeeb 		    __func__, __LINE__,
2890*713db49dSBjoern A. Zeeb 		    ieee80211_state_name[vap->iv_state],
2891*713db49dSBjoern A. Zeeb 		    ieee80211_state_name[vap->iv_nstate],
2892*713db49dSBjoern A. Zeeb 		    ieee80211_state_name[nstate],
2893*713db49dSBjoern A. Zeeb 		    _ieee80211_newstate_get_npending(vap),
2894*713db49dSBjoern A. Zeeb 		    NET80211_IV_NSTATE_NUM);
2895*713db49dSBjoern A. Zeeb 
2896*713db49dSBjoern A. Zeeb 		return (EAGAIN);
2897*713db49dSBjoern A. Zeeb 	}
2898*713db49dSBjoern A. Zeeb 	vap->iv_nstates[nstate_num] = nstate;
2899*713db49dSBjoern A. Zeeb 	vap->iv_nstate_args[nstate_num] = arg;
29005efea30fSAndrew Thompson 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2901*713db49dSBjoern A. Zeeb 	ieee80211_runtask(ic, &vap->iv_nstate_task[nstate_num]);
29025efea30fSAndrew Thompson 	return EINPROGRESS;
29038a1b9b6aSSam Leffler }
2904b032f27cSSam Leffler 
2905b032f27cSSam Leffler int
ieee80211_new_state(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2906b032f27cSSam Leffler ieee80211_new_state(struct ieee80211vap *vap,
2907b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
2908b032f27cSSam Leffler {
2909b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2910b032f27cSSam Leffler 	int rc;
2911b032f27cSSam Leffler 
2912b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2913b032f27cSSam Leffler 	rc = ieee80211_new_state_locked(vap, nstate, arg);
2914b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2915b032f27cSSam Leffler 	return rc;
29161a1e1d21SSam Leffler }
2917