xref: /freebsd/sys/net80211/ieee80211_proto.c (revision 4061c639f0ec759bf9a8a4a974507d2b0a8d5bd9)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3b032f27cSSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler /*
311a1e1d21SSam Leffler  * IEEE 802.11 protocol support.
321a1e1d21SSam Leffler  */
331a1e1d21SSam Leffler 
341a1e1d21SSam Leffler #include "opt_inet.h"
35b032f27cSSam Leffler #include "opt_wlan.h"
361a1e1d21SSam Leffler 
371a1e1d21SSam Leffler #include <sys/param.h>
381a1e1d21SSam Leffler #include <sys/kernel.h>
398a1b9b6aSSam Leffler #include <sys/systm.h>
401a1e1d21SSam Leffler 
418a1b9b6aSSam Leffler #include <sys/socket.h>
42b032f27cSSam Leffler #include <sys/sockio.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/if.h>
4576039bc8SGleb Smirnoff #include <net/if_var.h>
461a1e1d21SSam Leffler #include <net/if_media.h>
478a1b9b6aSSam Leffler #include <net/ethernet.h>		/* XXX for ether_sprintf */
481a1e1d21SSam Leffler 
491a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
50b032f27cSSam Leffler #include <net80211/ieee80211_adhoc.h>
51b032f27cSSam Leffler #include <net80211/ieee80211_sta.h>
52b032f27cSSam Leffler #include <net80211/ieee80211_hostap.h>
53b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5459aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
5559aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
5659aa14a9SRui Paulo #endif
57b032f27cSSam Leffler #include <net80211/ieee80211_monitor.h>
58b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
591a1e1d21SSam Leffler 
608a1b9b6aSSam Leffler /* XXX tunables */
618a1b9b6aSSam Leffler #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
628a1b9b6aSSam Leffler #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
631a1e1d21SSam Leffler 
641a1e1d21SSam Leffler const char *ieee80211_mgt_subtype_name[] = {
651a1e1d21SSam Leffler 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
661a1e1d21SSam Leffler 	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
671a1e1d21SSam Leffler 	"beacon",	"atim",		"disassoc",	"auth",
6896283082SBernhard Schmidt 	"deauth",	"action",	"action_noack",	"reserved#15"
691a1e1d21SSam Leffler };
708a1b9b6aSSam Leffler const char *ieee80211_ctl_subtype_name[] = {
718a1b9b6aSSam Leffler 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
728a1b9b6aSSam Leffler 	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
738a1b9b6aSSam Leffler 	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
748a1b9b6aSSam Leffler 	"cts",		"ack",		"cf_end",	"cf_end_ack"
758a1b9b6aSSam Leffler };
7649aa47d6SSam Leffler const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
7749aa47d6SSam Leffler 	"IBSS",		/* IEEE80211_M_IBSS */
7849aa47d6SSam Leffler 	"STA",		/* IEEE80211_M_STA */
79b032f27cSSam Leffler 	"WDS",		/* IEEE80211_M_WDS */
8049aa47d6SSam Leffler 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
8149aa47d6SSam Leffler 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
8259aa14a9SRui Paulo 	"MONITOR",	/* IEEE80211_M_MONITOR */
8359aa14a9SRui Paulo 	"MBSS"		/* IEEE80211_M_MBSS */
8449aa47d6SSam Leffler };
85a11c9a5cSSam Leffler const char *ieee80211_state_name[IEEE80211_S_MAX] = {
86a11c9a5cSSam Leffler 	"INIT",		/* IEEE80211_S_INIT */
87a11c9a5cSSam Leffler 	"SCAN",		/* IEEE80211_S_SCAN */
88a11c9a5cSSam Leffler 	"AUTH",		/* IEEE80211_S_AUTH */
89a11c9a5cSSam Leffler 	"ASSOC",	/* IEEE80211_S_ASSOC */
9014fb6b8fSSam Leffler 	"CAC",		/* IEEE80211_S_CAC */
9114fb6b8fSSam Leffler 	"RUN",		/* IEEE80211_S_RUN */
9214fb6b8fSSam Leffler 	"CSA",		/* IEEE80211_S_CSA */
9314fb6b8fSSam Leffler 	"SLEEP",	/* IEEE80211_S_SLEEP */
94a11c9a5cSSam Leffler };
958a1b9b6aSSam Leffler const char *ieee80211_wme_acnames[] = {
968a1b9b6aSSam Leffler 	"WME_AC_BE",
978a1b9b6aSSam Leffler 	"WME_AC_BK",
988a1b9b6aSSam Leffler 	"WME_AC_VI",
998a1b9b6aSSam Leffler 	"WME_AC_VO",
1008a1b9b6aSSam Leffler 	"WME_UPSD",
1018a1b9b6aSSam Leffler };
102a11c9a5cSSam Leffler 
1035efea30fSAndrew Thompson static void beacon_miss(void *, int);
1045efea30fSAndrew Thompson static void beacon_swmiss(void *, int);
105b032f27cSSam Leffler static void parent_updown(void *, int);
1065efea30fSAndrew Thompson static void update_mcast(void *, int);
1075efea30fSAndrew Thompson static void update_promisc(void *, int);
1085efea30fSAndrew Thompson static void update_channel(void *, int);
109b94299c4SAdrian Chadd static void update_chw(void *, int);
110dd2fb488SAdrian Chadd static void update_wme(void *, int);
111*4061c639SAndriy Voskoboinyk static void restart_vaps(void *, int);
1125efea30fSAndrew Thompson static void ieee80211_newstate_cb(void *, int);
1131a1e1d21SSam Leffler 
114b032f27cSSam Leffler static int
115b032f27cSSam Leffler null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
116b032f27cSSam Leffler 	const struct ieee80211_bpf_params *params)
117b105a069SSam Leffler {
118b032f27cSSam Leffler 
119c8f5794eSGleb Smirnoff 	ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
120b032f27cSSam Leffler 	m_freem(m);
121b032f27cSSam Leffler 	return ENETDOWN;
122b105a069SSam Leffler }
123b105a069SSam Leffler 
1241a1e1d21SSam Leffler void
1258a1b9b6aSSam Leffler ieee80211_proto_attach(struct ieee80211com *ic)
1261a1e1d21SSam Leffler {
1277a79cebfSGleb Smirnoff 	uint8_t hdrlen;
1281a1e1d21SSam Leffler 
129b032f27cSSam Leffler 	/* override the 802.3 setting */
1307a79cebfSGleb Smirnoff 	hdrlen = ic->ic_headroom
131b032f27cSSam Leffler 		+ sizeof(struct ieee80211_qosframe_addr4)
132b032f27cSSam Leffler 		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
133b032f27cSSam Leffler 		+ IEEE80211_WEP_EXTIVLEN;
134b032f27cSSam Leffler 	/* XXX no way to recalculate on ifdetach */
1357a79cebfSGleb Smirnoff 	if (ALIGN(hdrlen) > max_linkhdr) {
136b032f27cSSam Leffler 		/* XXX sanity check... */
1377a79cebfSGleb Smirnoff 		max_linkhdr = ALIGN(hdrlen);
138b032f27cSSam Leffler 		max_hdr = max_linkhdr + max_protohdr;
139b032f27cSSam Leffler 		max_datalen = MHLEN - max_hdr;
140b032f27cSSam Leffler 	}
1412e79ca97SSam Leffler 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
142b032f27cSSam Leffler 
1437a79cebfSGleb Smirnoff 	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
1445efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
1455efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
1465efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
1475efea30fSAndrew Thompson 	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
148b94299c4SAdrian Chadd 	TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
149dd2fb488SAdrian Chadd 	TASK_INIT(&ic->ic_wme_task, 0, update_wme, ic);
150*4061c639SAndriy Voskoboinyk 	TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
1518a1b9b6aSSam Leffler 
1528a1b9b6aSSam Leffler 	ic->ic_wme.wme_hipri_switch_hysteresis =
1538a1b9b6aSSam Leffler 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
1541a1e1d21SSam Leffler 
1551a1e1d21SSam Leffler 	/* initialize management frame handlers */
1561a1e1d21SSam Leffler 	ic->ic_send_mgmt = ieee80211_send_mgmt;
157b032f27cSSam Leffler 	ic->ic_raw_xmit = null_raw_xmit;
158b032f27cSSam Leffler 
159b032f27cSSam Leffler 	ieee80211_adhoc_attach(ic);
160b032f27cSSam Leffler 	ieee80211_sta_attach(ic);
161b032f27cSSam Leffler 	ieee80211_wds_attach(ic);
162b032f27cSSam Leffler 	ieee80211_hostap_attach(ic);
16359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
16459aa14a9SRui Paulo 	ieee80211_mesh_attach(ic);
16559aa14a9SRui Paulo #endif
166b032f27cSSam Leffler 	ieee80211_monitor_attach(ic);
1671a1e1d21SSam Leffler }
1681a1e1d21SSam Leffler 
1691a1e1d21SSam Leffler void
1708a1b9b6aSSam Leffler ieee80211_proto_detach(struct ieee80211com *ic)
1711a1e1d21SSam Leffler {
172b032f27cSSam Leffler 	ieee80211_monitor_detach(ic);
17359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
17459aa14a9SRui Paulo 	ieee80211_mesh_detach(ic);
17559aa14a9SRui Paulo #endif
176b032f27cSSam Leffler 	ieee80211_hostap_detach(ic);
177b032f27cSSam Leffler 	ieee80211_wds_detach(ic);
178b032f27cSSam Leffler 	ieee80211_adhoc_detach(ic);
179b032f27cSSam Leffler 	ieee80211_sta_detach(ic);
180b032f27cSSam Leffler }
1818a1b9b6aSSam Leffler 
182b032f27cSSam Leffler static void
183b032f27cSSam Leffler null_update_beacon(struct ieee80211vap *vap, int item)
184b032f27cSSam Leffler {
185b032f27cSSam Leffler }
186b032f27cSSam Leffler 
187b032f27cSSam Leffler void
188b032f27cSSam Leffler ieee80211_proto_vattach(struct ieee80211vap *vap)
189b032f27cSSam Leffler {
190b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
191b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
192b032f27cSSam Leffler 	int i;
193b032f27cSSam Leffler 
194b032f27cSSam Leffler 	/* override the 802.3 setting */
1957a79cebfSGleb Smirnoff 	ifp->if_hdrlen = ic->ic_headroom
1967a79cebfSGleb Smirnoff                 + sizeof(struct ieee80211_qosframe_addr4)
1977a79cebfSGleb Smirnoff                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
1987a79cebfSGleb Smirnoff                 + IEEE80211_WEP_EXTIVLEN;
199b032f27cSSam Leffler 
200b032f27cSSam Leffler 	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
201b032f27cSSam Leffler 	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
202b032f27cSSam Leffler 	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
20323401900SAdrian Chadd 	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
204fd90e2edSJung-uk Kim 	callout_init(&vap->iv_mgtsend, 1);
2055efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
2065efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
207b032f27cSSam Leffler 	/*
208b032f27cSSam Leffler 	 * Install default tx rate handling: no fixed rate, lowest
209b032f27cSSam Leffler 	 * supported rate for mgmt and multicast frames.  Default
210b032f27cSSam Leffler 	 * max retry count.  These settings can be changed by the
211b032f27cSSam Leffler 	 * driver and/or user applications.
212b032f27cSSam Leffler 	 */
213047db6b3SSam Leffler 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
214b032f27cSSam Leffler 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
215b032f27cSSam Leffler 
216b032f27cSSam Leffler 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
217338452c9SAdrian Chadd 
218338452c9SAdrian Chadd 		/*
219338452c9SAdrian Chadd 		 * Setting the management rate to MCS 0 assumes that the
220338452c9SAdrian Chadd 		 * BSS Basic rate set is empty and the BSS Basic MCS set
221338452c9SAdrian Chadd 		 * is not.
222338452c9SAdrian Chadd 		 *
223338452c9SAdrian Chadd 		 * Since we're not checking this, default to the lowest
224338452c9SAdrian Chadd 		 * defined rate for this mode.
225338452c9SAdrian Chadd 		 *
226338452c9SAdrian Chadd 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
227338452c9SAdrian Chadd 		 * some MCS management traffic (eg BA response frames.)
228338452c9SAdrian Chadd 		 *
229338452c9SAdrian Chadd 		 * See also: 9.6.0 of the 802.11n-2009 specification.
230338452c9SAdrian Chadd 		 */
231338452c9SAdrian Chadd #ifdef	NOTYET
232047db6b3SSam Leffler 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
233047db6b3SSam Leffler 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
234047db6b3SSam Leffler 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
235047db6b3SSam Leffler 		} else {
236b032f27cSSam Leffler 			vap->iv_txparms[i].mgmtrate =
237b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
238b032f27cSSam Leffler 			vap->iv_txparms[i].mcastrate =
239b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
240b032f27cSSam Leffler 		}
241338452c9SAdrian Chadd #endif
242338452c9SAdrian Chadd 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
243338452c9SAdrian Chadd 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
244b032f27cSSam Leffler 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
245b032f27cSSam Leffler 	}
246b032f27cSSam Leffler 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
247b032f27cSSam Leffler 
248b032f27cSSam Leffler 	vap->iv_update_beacon = null_update_beacon;
249b032f27cSSam Leffler 	vap->iv_deliver_data = ieee80211_deliver_data;
250b032f27cSSam Leffler 
251b032f27cSSam Leffler 	/* attach support for operating mode */
252b032f27cSSam Leffler 	ic->ic_vattach[vap->iv_opmode](vap);
253b032f27cSSam Leffler }
254b032f27cSSam Leffler 
255b032f27cSSam Leffler void
256b032f27cSSam Leffler ieee80211_proto_vdetach(struct ieee80211vap *vap)
257b032f27cSSam Leffler {
258b032f27cSSam Leffler #define	FREEAPPIE(ie) do { \
259b032f27cSSam Leffler 	if (ie != NULL) \
260b9b53389SAdrian Chadd 		IEEE80211_FREE(ie, M_80211_NODE_IE); \
261b032f27cSSam Leffler } while (0)
262b032f27cSSam Leffler 	/*
263b032f27cSSam Leffler 	 * Detach operating mode module.
264b032f27cSSam Leffler 	 */
265b032f27cSSam Leffler 	if (vap->iv_opdetach != NULL)
266b032f27cSSam Leffler 		vap->iv_opdetach(vap);
2678a1b9b6aSSam Leffler 	/*
2688a1b9b6aSSam Leffler 	 * This should not be needed as we detach when reseting
2698a1b9b6aSSam Leffler 	 * the state but be conservative here since the
2708a1b9b6aSSam Leffler 	 * authenticator may do things like spawn kernel threads.
2718a1b9b6aSSam Leffler 	 */
272b032f27cSSam Leffler 	if (vap->iv_auth->ia_detach != NULL)
273b032f27cSSam Leffler 		vap->iv_auth->ia_detach(vap);
2748a1b9b6aSSam Leffler 	/*
2758a1b9b6aSSam Leffler 	 * Detach any ACL'ator.
2768a1b9b6aSSam Leffler 	 */
277b032f27cSSam Leffler 	if (vap->iv_acl != NULL)
278b032f27cSSam Leffler 		vap->iv_acl->iac_detach(vap);
279b032f27cSSam Leffler 
280b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_beacon);
281b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_probereq);
282b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_proberesp);
283b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocreq);
284b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocresp);
285b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_wpa);
286b032f27cSSam Leffler #undef FREEAPPIE
2878a1b9b6aSSam Leffler }
2888a1b9b6aSSam Leffler 
2898a1b9b6aSSam Leffler /*
2908a1b9b6aSSam Leffler  * Simple-minded authenticator module support.
2918a1b9b6aSSam Leffler  */
2928a1b9b6aSSam Leffler 
2938a1b9b6aSSam Leffler #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
2948a1b9b6aSSam Leffler /* XXX well-known names */
2958a1b9b6aSSam Leffler static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
2968a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
2978a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
2988a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
2998a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
3008a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
3018a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
3028a1b9b6aSSam Leffler };
3038a1b9b6aSSam Leffler static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
3048a1b9b6aSSam Leffler 
3058a1b9b6aSSam Leffler static const struct ieee80211_authenticator auth_internal = {
3068a1b9b6aSSam Leffler 	.ia_name		= "wlan_internal",
3078a1b9b6aSSam Leffler 	.ia_attach		= NULL,
3088a1b9b6aSSam Leffler 	.ia_detach		= NULL,
3098a1b9b6aSSam Leffler 	.ia_node_join		= NULL,
3108a1b9b6aSSam Leffler 	.ia_node_leave		= NULL,
3118a1b9b6aSSam Leffler };
3128a1b9b6aSSam Leffler 
3138a1b9b6aSSam Leffler /*
3148a1b9b6aSSam Leffler  * Setup internal authenticators once; they are never unregistered.
3158a1b9b6aSSam Leffler  */
3168a1b9b6aSSam Leffler static void
3178a1b9b6aSSam Leffler ieee80211_auth_setup(void)
3188a1b9b6aSSam Leffler {
3198a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
3208a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
3218a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
3228a1b9b6aSSam Leffler }
3238a1b9b6aSSam Leffler SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
3248a1b9b6aSSam Leffler 
3258a1b9b6aSSam Leffler const struct ieee80211_authenticator *
3268a1b9b6aSSam Leffler ieee80211_authenticator_get(int auth)
3278a1b9b6aSSam Leffler {
3288a1b9b6aSSam Leffler 	if (auth >= IEEE80211_AUTH_MAX)
3298a1b9b6aSSam Leffler 		return NULL;
3308a1b9b6aSSam Leffler 	if (authenticators[auth] == NULL)
3318a1b9b6aSSam Leffler 		ieee80211_load_module(auth_modnames[auth]);
3328a1b9b6aSSam Leffler 	return authenticators[auth];
3331a1e1d21SSam Leffler }
3341a1e1d21SSam Leffler 
3351a1e1d21SSam Leffler void
3368a1b9b6aSSam Leffler ieee80211_authenticator_register(int type,
3378a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth)
3381a1e1d21SSam Leffler {
3398a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
3408a1b9b6aSSam Leffler 		return;
3418a1b9b6aSSam Leffler 	authenticators[type] = auth;
3428a1b9b6aSSam Leffler }
3438a1b9b6aSSam Leffler 
3448a1b9b6aSSam Leffler void
3458a1b9b6aSSam Leffler ieee80211_authenticator_unregister(int type)
3468a1b9b6aSSam Leffler {
3478a1b9b6aSSam Leffler 
3488a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
3498a1b9b6aSSam Leffler 		return;
3508a1b9b6aSSam Leffler 	authenticators[type] = NULL;
3518a1b9b6aSSam Leffler }
3528a1b9b6aSSam Leffler 
3538a1b9b6aSSam Leffler /*
3548a1b9b6aSSam Leffler  * Very simple-minded ACL module support.
3558a1b9b6aSSam Leffler  */
3568a1b9b6aSSam Leffler /* XXX just one for now */
3578a1b9b6aSSam Leffler static	const struct ieee80211_aclator *acl = NULL;
3588a1b9b6aSSam Leffler 
3598a1b9b6aSSam Leffler void
3608a1b9b6aSSam Leffler ieee80211_aclator_register(const struct ieee80211_aclator *iac)
3618a1b9b6aSSam Leffler {
3628a1b9b6aSSam Leffler 	printf("wlan: %s acl policy registered\n", iac->iac_name);
3638a1b9b6aSSam Leffler 	acl = iac;
3648a1b9b6aSSam Leffler }
3658a1b9b6aSSam Leffler 
3668a1b9b6aSSam Leffler void
3678a1b9b6aSSam Leffler ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
3688a1b9b6aSSam Leffler {
3698a1b9b6aSSam Leffler 	if (acl == iac)
3708a1b9b6aSSam Leffler 		acl = NULL;
3718a1b9b6aSSam Leffler 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
3728a1b9b6aSSam Leffler }
3738a1b9b6aSSam Leffler 
3748a1b9b6aSSam Leffler const struct ieee80211_aclator *
3758a1b9b6aSSam Leffler ieee80211_aclator_get(const char *name)
3768a1b9b6aSSam Leffler {
3778a1b9b6aSSam Leffler 	if (acl == NULL)
3788a1b9b6aSSam Leffler 		ieee80211_load_module("wlan_acl");
3798a1b9b6aSSam Leffler 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
3808a1b9b6aSSam Leffler }
3818a1b9b6aSSam Leffler 
3828a1b9b6aSSam Leffler void
38368e8e04eSSam Leffler ieee80211_print_essid(const uint8_t *essid, int len)
3848a1b9b6aSSam Leffler {
38568e8e04eSSam Leffler 	const uint8_t *p;
3861a1e1d21SSam Leffler 	int i;
3871a1e1d21SSam Leffler 
3881a1e1d21SSam Leffler 	if (len > IEEE80211_NWID_LEN)
3891a1e1d21SSam Leffler 		len = IEEE80211_NWID_LEN;
3901a1e1d21SSam Leffler 	/* determine printable or not */
3911a1e1d21SSam Leffler 	for (i = 0, p = essid; i < len; i++, p++) {
3921a1e1d21SSam Leffler 		if (*p < ' ' || *p > 0x7e)
3931a1e1d21SSam Leffler 			break;
3941a1e1d21SSam Leffler 	}
3951a1e1d21SSam Leffler 	if (i == len) {
3961a1e1d21SSam Leffler 		printf("\"");
3971a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
3981a1e1d21SSam Leffler 			printf("%c", *p);
3991a1e1d21SSam Leffler 		printf("\"");
4001a1e1d21SSam Leffler 	} else {
4011a1e1d21SSam Leffler 		printf("0x");
4021a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
4031a1e1d21SSam Leffler 			printf("%02x", *p);
4041a1e1d21SSam Leffler 	}
4051a1e1d21SSam Leffler }
4061a1e1d21SSam Leffler 
4071a1e1d21SSam Leffler void
40868e8e04eSSam Leffler ieee80211_dump_pkt(struct ieee80211com *ic,
40968e8e04eSSam Leffler 	const uint8_t *buf, int len, int rate, int rssi)
4101a1e1d21SSam Leffler {
4118a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh;
4121a1e1d21SSam Leffler 	int i;
4131a1e1d21SSam Leffler 
4148a1b9b6aSSam Leffler 	wh = (const struct ieee80211_frame *)buf;
4151a1e1d21SSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
4161a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
4171a1e1d21SSam Leffler 		printf("NODS %s", ether_sprintf(wh->i_addr2));
4181a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
4191a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr3));
4201a1e1d21SSam Leffler 		break;
4211a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
4221a1e1d21SSam Leffler 		printf("TODS %s", ether_sprintf(wh->i_addr2));
4231a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
4241a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr1));
4251a1e1d21SSam Leffler 		break;
4261a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
4271a1e1d21SSam Leffler 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
4281a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
4291a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr2));
4301a1e1d21SSam Leffler 		break;
4311a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
43268e8e04eSSam Leffler 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
4331a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
4341a1e1d21SSam Leffler 		printf("(%s", ether_sprintf(wh->i_addr2));
4351a1e1d21SSam Leffler 		printf("->%s)", ether_sprintf(wh->i_addr1));
4361a1e1d21SSam Leffler 		break;
4371a1e1d21SSam Leffler 	}
4381a1e1d21SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
4391a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
4401a1e1d21SSam Leffler 		printf(" data");
4411a1e1d21SSam Leffler 		break;
4421a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
4431a1e1d21SSam Leffler 		printf(" %s", ieee80211_mgt_subtype_name[
4441a1e1d21SSam Leffler 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
4451a1e1d21SSam Leffler 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
4461a1e1d21SSam Leffler 		break;
4471a1e1d21SSam Leffler 	default:
4481a1e1d21SSam Leffler 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
4491a1e1d21SSam Leffler 		break;
4501a1e1d21SSam Leffler 	}
45168e8e04eSSam Leffler 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
45268e8e04eSSam Leffler 		const struct ieee80211_qosframe *qwh =
45368e8e04eSSam Leffler 			(const struct ieee80211_qosframe *)buf;
45468e8e04eSSam Leffler 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
45568e8e04eSSam Leffler 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
45668e8e04eSSam Leffler 	}
4575945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
45868e8e04eSSam Leffler 		int off;
45968e8e04eSSam Leffler 
46068e8e04eSSam Leffler 		off = ieee80211_anyhdrspace(ic, wh);
46168e8e04eSSam Leffler 		printf(" WEP [IV %.02x %.02x %.02x",
46268e8e04eSSam Leffler 			buf[off+0], buf[off+1], buf[off+2]);
46368e8e04eSSam Leffler 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
46468e8e04eSSam Leffler 			printf(" %.02x %.02x %.02x",
46568e8e04eSSam Leffler 				buf[off+4], buf[off+5], buf[off+6]);
46668e8e04eSSam Leffler 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
4678a1b9b6aSSam Leffler 	}
4681a1e1d21SSam Leffler 	if (rate >= 0)
4691a1e1d21SSam Leffler 		printf(" %dM", rate / 2);
4701a1e1d21SSam Leffler 	if (rssi >= 0)
4711a1e1d21SSam Leffler 		printf(" +%d", rssi);
4721a1e1d21SSam Leffler 	printf("\n");
4731a1e1d21SSam Leffler 	if (len > 0) {
4741a1e1d21SSam Leffler 		for (i = 0; i < len; i++) {
4751a1e1d21SSam Leffler 			if ((i & 1) == 0)
4761a1e1d21SSam Leffler 				printf(" ");
4771a1e1d21SSam Leffler 			printf("%02x", buf[i]);
4781a1e1d21SSam Leffler 		}
4791a1e1d21SSam Leffler 		printf("\n");
4801a1e1d21SSam Leffler 	}
4811a1e1d21SSam Leffler }
4821a1e1d21SSam Leffler 
48379edaebfSSam Leffler static __inline int
48479edaebfSSam Leffler findrix(const struct ieee80211_rateset *rs, int r)
48579edaebfSSam Leffler {
48679edaebfSSam Leffler 	int i;
48779edaebfSSam Leffler 
48879edaebfSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
48979edaebfSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
49079edaebfSSam Leffler 			return i;
49179edaebfSSam Leffler 	return -1;
49279edaebfSSam Leffler }
49379edaebfSSam Leffler 
4941a1e1d21SSam Leffler int
49570e28b9aSSam Leffler ieee80211_fix_rate(struct ieee80211_node *ni,
49670e28b9aSSam Leffler 	struct ieee80211_rateset *nrs, int flags)
4971a1e1d21SSam Leffler {
498b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
4997d77cd53SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
50079edaebfSSam Leffler 	int i, j, rix, error;
501b032f27cSSam Leffler 	int okrate, badrate, fixedrate, ucastrate;
50241b3c790SSam Leffler 	const struct ieee80211_rateset *srs;
50368e8e04eSSam Leffler 	uint8_t r;
5041a1e1d21SSam Leffler 
5051a1e1d21SSam Leffler 	error = 0;
50668e8e04eSSam Leffler 	okrate = badrate = 0;
507b032f27cSSam Leffler 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
508b032f27cSSam Leffler 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
509b032f27cSSam Leffler 		/*
510b032f27cSSam Leffler 		 * Workaround awkwardness with fixed rate.  We are called
511b032f27cSSam Leffler 		 * to check both the legacy rate set and the HT rate set
512b032f27cSSam Leffler 		 * but we must apply any legacy fixed rate check only to the
513b032f27cSSam Leffler 		 * legacy rate set and vice versa.  We cannot tell what type
514b032f27cSSam Leffler 		 * of rate set we've been given (legacy or HT) but we can
515b032f27cSSam Leffler 		 * distinguish the fixed rate type (MCS have 0x80 set).
516b032f27cSSam Leffler 		 * So to deal with this the caller communicates whether to
517b032f27cSSam Leffler 		 * check MCS or legacy rate using the flags and we use the
518b032f27cSSam Leffler 		 * type of any fixed rate to avoid applying an MCS to a
519b032f27cSSam Leffler 		 * legacy rate and vice versa.
520b032f27cSSam Leffler 		 */
521b032f27cSSam Leffler 		if (ucastrate & 0x80) {
522b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFRATE)
523b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFRATE;
524b032f27cSSam Leffler 		} else if ((ucastrate & 0x80) == 0) {
525b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFMCS)
526b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFMCS;
527b032f27cSSam Leffler 		}
528b032f27cSSam Leffler 		/* NB: required to make MCS match below work */
529b032f27cSSam Leffler 		ucastrate &= IEEE80211_RATE_VAL;
530b032f27cSSam Leffler 	}
53168e8e04eSSam Leffler 	fixedrate = IEEE80211_FIXED_RATE_NONE;
532b032f27cSSam Leffler 	/*
533b032f27cSSam Leffler 	 * XXX we are called to process both MCS and legacy rates;
534b032f27cSSam Leffler 	 * we must use the appropriate basic rate set or chaos will
535b032f27cSSam Leffler 	 * ensue; for now callers that want MCS must supply
536b032f27cSSam Leffler 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
537b032f27cSSam Leffler 	 * function so there are two variants, one for MCS and one
538b032f27cSSam Leffler 	 * for legacy rates.
539b032f27cSSam Leffler 	 */
540b032f27cSSam Leffler 	if (flags & IEEE80211_F_DOBRS)
541b032f27cSSam Leffler 		srs = (const struct ieee80211_rateset *)
542b032f27cSSam Leffler 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
543b032f27cSSam Leffler 	else
54441b3c790SSam Leffler 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
545ef39d4beSSam Leffler 	for (i = 0; i < nrs->rs_nrates; ) {
5461a1e1d21SSam Leffler 		if (flags & IEEE80211_F_DOSORT) {
5471a1e1d21SSam Leffler 			/*
5481a1e1d21SSam Leffler 			 * Sort rates.
5491a1e1d21SSam Leffler 			 */
5501a1e1d21SSam Leffler 			for (j = i + 1; j < nrs->rs_nrates; j++) {
5510ebe104fSAdrian Chadd 				if (IEEE80211_RV(nrs->rs_rates[i]) >
5520ebe104fSAdrian Chadd 				    IEEE80211_RV(nrs->rs_rates[j])) {
5531a1e1d21SSam Leffler 					r = nrs->rs_rates[i];
5541a1e1d21SSam Leffler 					nrs->rs_rates[i] = nrs->rs_rates[j];
5551a1e1d21SSam Leffler 					nrs->rs_rates[j] = r;
5561a1e1d21SSam Leffler 				}
5571a1e1d21SSam Leffler 			}
5581a1e1d21SSam Leffler 		}
5591a1e1d21SSam Leffler 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
5601a1e1d21SSam Leffler 		badrate = r;
5611a1e1d21SSam Leffler 		/*
56268e8e04eSSam Leffler 		 * Check for fixed rate.
5631a1e1d21SSam Leffler 		 */
564b032f27cSSam Leffler 		if (r == ucastrate)
5658a1b9b6aSSam Leffler 			fixedrate = r;
5661a1e1d21SSam Leffler 		/*
5671a1e1d21SSam Leffler 		 * Check against supported rates.
5681a1e1d21SSam Leffler 		 */
56979edaebfSSam Leffler 		rix = findrix(srs, r);
57079edaebfSSam Leffler 		if (flags & IEEE80211_F_DONEGO) {
57179edaebfSSam Leffler 			if (rix < 0) {
572ef39d4beSSam Leffler 				/*
573ef39d4beSSam Leffler 				 * A rate in the node's rate set is not
574ef39d4beSSam Leffler 				 * supported.  If this is a basic rate and we
57579edaebfSSam Leffler 				 * are operating as a STA then this is an error.
576ef39d4beSSam Leffler 				 * Otherwise we just discard/ignore the rate.
577ef39d4beSSam Leffler 				 */
57879edaebfSSam Leffler 				if ((flags & IEEE80211_F_JOIN) &&
579ef39d4beSSam Leffler 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
5801a1e1d21SSam Leffler 					error++;
58179edaebfSSam Leffler 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
58279edaebfSSam Leffler 				/*
58379edaebfSSam Leffler 				 * Overwrite with the supported rate
58479edaebfSSam Leffler 				 * value so any basic rate bit is set.
58579edaebfSSam Leffler 				 */
58679edaebfSSam Leffler 				nrs->rs_rates[i] = srs->rs_rates[rix];
5871a1e1d21SSam Leffler 			}
5881a1e1d21SSam Leffler 		}
58979edaebfSSam Leffler 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
5901a1e1d21SSam Leffler 			/*
5911a1e1d21SSam Leffler 			 * Delete unacceptable rates.
5921a1e1d21SSam Leffler 			 */
5931a1e1d21SSam Leffler 			nrs->rs_nrates--;
5941a1e1d21SSam Leffler 			for (j = i; j < nrs->rs_nrates; j++)
5951a1e1d21SSam Leffler 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
5961a1e1d21SSam Leffler 			nrs->rs_rates[j] = 0;
5971a1e1d21SSam Leffler 			continue;
5981a1e1d21SSam Leffler 		}
59979edaebfSSam Leffler 		if (rix >= 0)
6001a1e1d21SSam Leffler 			okrate = nrs->rs_rates[i];
6011a1e1d21SSam Leffler 		i++;
6021a1e1d21SSam Leffler 	}
6038a1b9b6aSSam Leffler 	if (okrate == 0 || error != 0 ||
604b032f27cSSam Leffler 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
605b032f27cSSam Leffler 	     fixedrate != ucastrate)) {
606b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
607b032f27cSSam Leffler 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
608b032f27cSSam Leffler 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
6091a1e1d21SSam Leffler 		return badrate | IEEE80211_RATE_BASIC;
610b032f27cSSam Leffler 	} else
6110ebe104fSAdrian Chadd 		return IEEE80211_RV(okrate);
6121a1e1d21SSam Leffler }
6131a1e1d21SSam Leffler 
6148a1b9b6aSSam Leffler /*
6158a1b9b6aSSam Leffler  * Reset 11g-related state.
6168a1b9b6aSSam Leffler  */
6178a1b9b6aSSam Leffler void
6188a1b9b6aSSam Leffler ieee80211_reset_erp(struct ieee80211com *ic)
6191a1e1d21SSam Leffler {
6208a1b9b6aSSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
6218a1b9b6aSSam Leffler 	ic->ic_nonerpsta = 0;
6228a1b9b6aSSam Leffler 	ic->ic_longslotsta = 0;
6238a1b9b6aSSam Leffler 	/*
6248a1b9b6aSSam Leffler 	 * Short slot time is enabled only when operating in 11g
6258a1b9b6aSSam Leffler 	 * and not in an IBSS.  We must also honor whether or not
6268a1b9b6aSSam Leffler 	 * the driver is capable of doing it.
6278a1b9b6aSSam Leffler 	 */
6288a1b9b6aSSam Leffler 	ieee80211_set_shortslottime(ic,
62968e8e04eSSam Leffler 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
63068e8e04eSSam Leffler 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
63168e8e04eSSam Leffler 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
6328a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
6338a1b9b6aSSam Leffler 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
6348a1b9b6aSSam Leffler 	/*
6358a1b9b6aSSam Leffler 	 * Set short preamble and ERP barker-preamble flags.
6368a1b9b6aSSam Leffler 	 */
63768e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
6388a1b9b6aSSam Leffler 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
6398a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
6408a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
6418a1b9b6aSSam Leffler 	} else {
6428a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
6438a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_USEBARKER;
6448a1b9b6aSSam Leffler 	}
6458a1b9b6aSSam Leffler }
6468a1b9b6aSSam Leffler 
6478a1b9b6aSSam Leffler /*
6488a1b9b6aSSam Leffler  * Set the short slot time state and notify the driver.
6498a1b9b6aSSam Leffler  */
6508a1b9b6aSSam Leffler void
6518a1b9b6aSSam Leffler ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
6528a1b9b6aSSam Leffler {
6538a1b9b6aSSam Leffler 	if (onoff)
6548a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHSLOT;
6558a1b9b6aSSam Leffler 	else
6568a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
6578a1b9b6aSSam Leffler 	/* notify driver */
6588a1b9b6aSSam Leffler 	if (ic->ic_updateslot != NULL)
659272f6adeSGleb Smirnoff 		ic->ic_updateslot(ic);
6608a1b9b6aSSam Leffler }
6618a1b9b6aSSam Leffler 
6628a1b9b6aSSam Leffler /*
6638a1b9b6aSSam Leffler  * Check if the specified rate set supports ERP.
6648a1b9b6aSSam Leffler  * NB: the rate set is assumed to be sorted.
6658a1b9b6aSSam Leffler  */
6668a1b9b6aSSam Leffler int
667b032f27cSSam Leffler ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
6688a1b9b6aSSam Leffler {
6698a1b9b6aSSam Leffler 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
6708a1b9b6aSSam Leffler 	int i, j;
6718a1b9b6aSSam Leffler 
672a3e08d6fSRui Paulo 	if (rs->rs_nrates < nitems(rates))
6738a1b9b6aSSam Leffler 		return 0;
674a3e08d6fSRui Paulo 	for (i = 0; i < nitems(rates); i++) {
6758a1b9b6aSSam Leffler 		for (j = 0; j < rs->rs_nrates; j++) {
6768a1b9b6aSSam Leffler 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
6778a1b9b6aSSam Leffler 			if (rates[i] == r)
6788a1b9b6aSSam Leffler 				goto next;
6798a1b9b6aSSam Leffler 			if (r > rates[i])
6808a1b9b6aSSam Leffler 				return 0;
6818a1b9b6aSSam Leffler 		}
6828a1b9b6aSSam Leffler 		return 0;
6838a1b9b6aSSam Leffler 	next:
6848a1b9b6aSSam Leffler 		;
6858a1b9b6aSSam Leffler 	}
6868a1b9b6aSSam Leffler 	return 1;
6878a1b9b6aSSam Leffler }
6888a1b9b6aSSam Leffler 
6898a1b9b6aSSam Leffler /*
690b032f27cSSam Leffler  * Mark the basic rates for the rate table based on the
6918a1b9b6aSSam Leffler  * operating mode.  For real 11g we mark all the 11b rates
6928a1b9b6aSSam Leffler  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
6938a1b9b6aSSam Leffler  * 11b rates.  There's also a pseudo 11a-mode used to mark only
6948a1b9b6aSSam Leffler  * the basic OFDM rates.
6958a1b9b6aSSam Leffler  */
696b032f27cSSam Leffler static void
697b032f27cSSam Leffler setbasicrates(struct ieee80211_rateset *rs,
698b032f27cSSam Leffler     enum ieee80211_phymode mode, int add)
6998a1b9b6aSSam Leffler {
70068e8e04eSSam Leffler 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
701be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
702be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
703be0df3e7SSam Leffler 					    /* NB: mixed b/g */
704be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
705be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
706be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
707be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
7086a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
7096a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
710be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
711be0df3e7SSam Leffler 					    /* NB: mixed b/g */
712be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
7138a1b9b6aSSam Leffler 	};
7148a1b9b6aSSam Leffler 	int i, j;
7158a1b9b6aSSam Leffler 
7168a1b9b6aSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++) {
717b032f27cSSam Leffler 		if (!add)
7188a1b9b6aSSam Leffler 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
7198a1b9b6aSSam Leffler 		for (j = 0; j < basic[mode].rs_nrates; j++)
7208a1b9b6aSSam Leffler 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
7218a1b9b6aSSam Leffler 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
7228a1b9b6aSSam Leffler 				break;
7238a1b9b6aSSam Leffler 			}
7248a1b9b6aSSam Leffler 	}
7258a1b9b6aSSam Leffler }
7268a1b9b6aSSam Leffler 
7278a1b9b6aSSam Leffler /*
728b032f27cSSam Leffler  * Set the basic rates in a rate set.
729b032f27cSSam Leffler  */
730b032f27cSSam Leffler void
731b032f27cSSam Leffler ieee80211_setbasicrates(struct ieee80211_rateset *rs,
732b032f27cSSam Leffler     enum ieee80211_phymode mode)
733b032f27cSSam Leffler {
734b032f27cSSam Leffler 	setbasicrates(rs, mode, 0);
735b032f27cSSam Leffler }
736b032f27cSSam Leffler 
737b032f27cSSam Leffler /*
738b032f27cSSam Leffler  * Add basic rates to a rate set.
739b032f27cSSam Leffler  */
740b032f27cSSam Leffler void
741b032f27cSSam Leffler ieee80211_addbasicrates(struct ieee80211_rateset *rs,
742b032f27cSSam Leffler     enum ieee80211_phymode mode)
743b032f27cSSam Leffler {
744b032f27cSSam Leffler 	setbasicrates(rs, mode, 1);
745b032f27cSSam Leffler }
746b032f27cSSam Leffler 
747b032f27cSSam Leffler /*
748b032f27cSSam Leffler  * WME protocol support.
749b032f27cSSam Leffler  *
750b032f27cSSam Leffler  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
751b032f27cSSam Leffler  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
752b032f27cSSam Leffler  * Draft 2.0 Test Plan (Appendix D).
753b032f27cSSam Leffler  *
754b032f27cSSam Leffler  * Static/Dynamic Turbo mode settings come from Atheros.
7558a1b9b6aSSam Leffler  */
7568a1b9b6aSSam Leffler typedef struct phyParamType {
75768e8e04eSSam Leffler 	uint8_t		aifsn;
75868e8e04eSSam Leffler 	uint8_t		logcwmin;
75968e8e04eSSam Leffler 	uint8_t		logcwmax;
76068e8e04eSSam Leffler 	uint16_t	txopLimit;
76168e8e04eSSam Leffler 	uint8_t 	acm;
7628a1b9b6aSSam Leffler } paramType;
7638a1b9b6aSSam Leffler 
7648a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
765be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
766be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
767be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
768be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
769be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
770be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
771be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
772be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
7736a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
7746a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
775be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
776be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
7778a1b9b6aSSam Leffler };
7788a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
779be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
780be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
781be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
782be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
783be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
784be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
785be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
786be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
7876a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
7886a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
789be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
790be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
7918a1b9b6aSSam Leffler };
7928a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
793be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
794be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
795be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
796be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
797be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
798be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
799be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
800be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
8016a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
8026a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
803be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
804be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
8058a1b9b6aSSam Leffler };
8068a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
807be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
808be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
809be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
810be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
811be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
812be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
813be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
814be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
8156a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
8166a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
817be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
818be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
8198a1b9b6aSSam Leffler };
8208a1b9b6aSSam Leffler 
8218a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
822be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
823be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
824be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
825be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
826be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
827be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
828be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
829be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
8306a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
8316a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
832be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
833be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
8348a1b9b6aSSam Leffler };
8358a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
836be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
837be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
838be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
839be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
840be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
841be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
842be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
843be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
8446a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
8456a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
846be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
847be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
8488a1b9b6aSSam Leffler };
8498a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
850be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
851be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
852be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
853be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
854be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
855be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
856be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
857be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
8586a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
8596a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
860be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
861be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
8628a1b9b6aSSam Leffler };
8638a1b9b6aSSam Leffler 
864b032f27cSSam Leffler static void
86567ce310aSSam Leffler _setifsparams(struct wmeParams *wmep, const paramType *phy)
86667ce310aSSam Leffler {
86767ce310aSSam Leffler 	wmep->wmep_aifsn = phy->aifsn;
86867ce310aSSam Leffler 	wmep->wmep_logcwmin = phy->logcwmin;
86967ce310aSSam Leffler 	wmep->wmep_logcwmax = phy->logcwmax;
87067ce310aSSam Leffler 	wmep->wmep_txopLimit = phy->txopLimit;
87167ce310aSSam Leffler }
87267ce310aSSam Leffler 
87367ce310aSSam Leffler static void
87467ce310aSSam Leffler setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
87567ce310aSSam Leffler 	struct wmeParams *wmep, const paramType *phy)
87667ce310aSSam Leffler {
87767ce310aSSam Leffler 	wmep->wmep_acm = phy->acm;
87867ce310aSSam Leffler 	_setifsparams(wmep, phy);
87967ce310aSSam Leffler 
88067ce310aSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
88167ce310aSSam Leffler 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
88267ce310aSSam Leffler 	    ieee80211_wme_acnames[ac], type,
88367ce310aSSam Leffler 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
88467ce310aSSam Leffler 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
88567ce310aSSam Leffler }
88667ce310aSSam Leffler 
88767ce310aSSam Leffler static void
888b032f27cSSam Leffler ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
8898a1b9b6aSSam Leffler {
890b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
8918a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
8928a1b9b6aSSam Leffler 	const paramType *pPhyParam, *pBssPhyParam;
8938a1b9b6aSSam Leffler 	struct wmeParams *wmep;
89468e8e04eSSam Leffler 	enum ieee80211_phymode mode;
8958a1b9b6aSSam Leffler 	int i;
8968a1b9b6aSSam Leffler 
897b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
898b032f27cSSam Leffler 
899a4b3c7a5SSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
9008a1b9b6aSSam Leffler 		return;
9018a1b9b6aSSam Leffler 
90268e8e04eSSam Leffler 	/*
9030d4e4e5eSAdrian Chadd 	 * Clear the wme cap_info field so a qoscount from a previous
9040d4e4e5eSAdrian Chadd 	 * vap doesn't confuse later code which only parses the beacon
9050d4e4e5eSAdrian Chadd 	 * field and updates hardware when said field changes.
9060d4e4e5eSAdrian Chadd 	 * Otherwise the hardware is programmed with defaults, not what
9070d4e4e5eSAdrian Chadd 	 * the beacon actually announces.
9080d4e4e5eSAdrian Chadd 	 */
9090d4e4e5eSAdrian Chadd 	wme->wme_wmeChanParams.cap_info = 0;
9100d4e4e5eSAdrian Chadd 
9110d4e4e5eSAdrian Chadd 	/*
91268e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
91368e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
91468e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
91568e8e04eSSam Leffler 	 * so state will eventually get set correctly
91668e8e04eSSam Leffler 	 */
91768e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
91868e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
91968e8e04eSSam Leffler 	else
92068e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
9218a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
9228a1b9b6aSSam Leffler 		switch (i) {
9238a1b9b6aSSam Leffler 		case WME_AC_BK:
92468e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BK[mode];
92568e8e04eSSam Leffler 			pBssPhyParam = &phyParamForAC_BK[mode];
9268a1b9b6aSSam Leffler 			break;
9278a1b9b6aSSam Leffler 		case WME_AC_VI:
92868e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VI[mode];
92968e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
9308a1b9b6aSSam Leffler 			break;
9318a1b9b6aSSam Leffler 		case WME_AC_VO:
93268e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VO[mode];
93368e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
9348a1b9b6aSSam Leffler 			break;
9358a1b9b6aSSam Leffler 		case WME_AC_BE:
9368a1b9b6aSSam Leffler 		default:
93768e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BE[mode];
93868e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
9398a1b9b6aSSam Leffler 			break;
9408a1b9b6aSSam Leffler 		}
9418a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
9428a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
94367ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
9448a1b9b6aSSam Leffler 		} else {
94567ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
9468a1b9b6aSSam Leffler 		}
9478a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
94867ce310aSSam Leffler 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
9498a1b9b6aSSam Leffler 	}
9508a1b9b6aSSam Leffler 	/* NB: check ic_bss to avoid NULL deref on initial attach */
951b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
9528a1b9b6aSSam Leffler 		/*
9538a1b9b6aSSam Leffler 		 * Calculate agressive mode switching threshold based
9548a1b9b6aSSam Leffler 		 * on beacon interval.  This doesn't need locking since
9558a1b9b6aSSam Leffler 		 * we're only called before entering the RUN state at
9568a1b9b6aSSam Leffler 		 * which point we start sending beacon frames.
9578a1b9b6aSSam Leffler 		 */
9588a1b9b6aSSam Leffler 		wme->wme_hipri_switch_thresh =
959b032f27cSSam Leffler 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
960a4b3c7a5SSam Leffler 		wme->wme_flags &= ~WME_F_AGGRMODE;
961b032f27cSSam Leffler 		ieee80211_wme_updateparams(vap);
9628a1b9b6aSSam Leffler 	}
9638a1b9b6aSSam Leffler }
9648a1b9b6aSSam Leffler 
965b032f27cSSam Leffler void
966b032f27cSSam Leffler ieee80211_wme_initparams(struct ieee80211vap *vap)
967b032f27cSSam Leffler {
968b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
969b032f27cSSam Leffler 
970b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
971b032f27cSSam Leffler 	ieee80211_wme_initparams_locked(vap);
972b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
973b032f27cSSam Leffler }
974b032f27cSSam Leffler 
9758a1b9b6aSSam Leffler /*
9768a1b9b6aSSam Leffler  * Update WME parameters for ourself and the BSS.
9778a1b9b6aSSam Leffler  */
9788a1b9b6aSSam Leffler void
979b032f27cSSam Leffler ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
9808a1b9b6aSSam Leffler {
98167ce310aSSam Leffler 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
982be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
983be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
984be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
985be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
986be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
987be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
988be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
989be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
9906a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
9916a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
992be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
993be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
9948a1b9b6aSSam Leffler 	};
995b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
9968a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
9978a1b9b6aSSam Leffler 	const struct wmeParams *wmep;
9988a1b9b6aSSam Leffler 	struct wmeParams *chanp, *bssp;
99968e8e04eSSam Leffler 	enum ieee80211_phymode mode;
10008a1b9b6aSSam Leffler 	int i;
1001a48a8ad7SAdrian Chadd 	int do_aggrmode = 0;
10028a1b9b6aSSam Leffler 
100367ce310aSSam Leffler        	/*
100467ce310aSSam Leffler 	 * Set up the channel access parameters for the physical
100567ce310aSSam Leffler 	 * device.  First populate the configured settings.
100667ce310aSSam Leffler 	 */
10078a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
10088a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
10098a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
10108a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
10118a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
10128a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
10138a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
10148a1b9b6aSSam Leffler 
10158a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
10168a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
10178a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
10188a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
10198a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
10208a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
10218a1b9b6aSSam Leffler 	}
10228a1b9b6aSSam Leffler 
10238a1b9b6aSSam Leffler 	/*
102468e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
102568e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
102668e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
102768e8e04eSSam Leffler 	 * so state will eventually get set correctly
102868e8e04eSSam Leffler 	 */
102968e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
103068e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
103168e8e04eSSam Leffler 	else
103268e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
103368e8e04eSSam Leffler 
103468e8e04eSSam Leffler 	/*
10358a1b9b6aSSam Leffler 	 * This implements agressive mode as found in certain
10368a1b9b6aSSam Leffler 	 * vendors' AP's.  When there is significant high
10378a1b9b6aSSam Leffler 	 * priority (VI/VO) traffic in the BSS throttle back BE
10388a1b9b6aSSam Leffler 	 * traffic by using conservative parameters.  Otherwise
10398a1b9b6aSSam Leffler 	 * BE uses agressive params to optimize performance of
10408a1b9b6aSSam Leffler 	 * legacy/non-QoS traffic.
10418a1b9b6aSSam Leffler 	 */
1042a48a8ad7SAdrian Chadd 
1043a48a8ad7SAdrian Chadd 	/* Hostap? Only if aggressive mode is enabled */
1044a48a8ad7SAdrian Chadd         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1045a48a8ad7SAdrian Chadd 	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
1046a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1047a48a8ad7SAdrian Chadd 
1048a48a8ad7SAdrian Chadd 	/*
1049a48a8ad7SAdrian Chadd 	 * Station? Only if we're in a non-QoS BSS.
1050a48a8ad7SAdrian Chadd 	 */
1051a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_STA &&
1052a48a8ad7SAdrian Chadd 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1053a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1054a48a8ad7SAdrian Chadd 
1055a48a8ad7SAdrian Chadd 	/*
1056a48a8ad7SAdrian Chadd 	 * IBSS? Only if we we have WME enabled.
1057a48a8ad7SAdrian Chadd 	 */
1058a48a8ad7SAdrian Chadd 	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1059a48a8ad7SAdrian Chadd 	    (vap->iv_flags & IEEE80211_F_WME))
1060a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1061a48a8ad7SAdrian Chadd 
1062a48a8ad7SAdrian Chadd 	/*
1063a48a8ad7SAdrian Chadd 	 * If WME is disabled on this VAP, default to aggressive mode
1064a48a8ad7SAdrian Chadd 	 * regardless of the configuration.
1065a48a8ad7SAdrian Chadd 	 */
1066a48a8ad7SAdrian Chadd 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1067a48a8ad7SAdrian Chadd 		do_aggrmode = 1;
1068a48a8ad7SAdrian Chadd 
1069a48a8ad7SAdrian Chadd 	/* XXX WDS? */
1070a48a8ad7SAdrian Chadd 
1071a48a8ad7SAdrian Chadd 	/* XXX MBSS? */
1072a48a8ad7SAdrian Chadd 
1073a48a8ad7SAdrian Chadd 	if (do_aggrmode) {
10748a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
10758a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
10768a1b9b6aSSam Leffler 
107767ce310aSSam Leffler 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
10788a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
107967ce310aSSam Leffler 		    aggrParam[mode].logcwmin;
10808a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
108167ce310aSSam Leffler 		    aggrParam[mode].logcwmax;
10828a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1083b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_BURST) ?
108467ce310aSSam Leffler 			aggrParam[mode].txopLimit : 0;
1085b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
108667ce310aSSam Leffler 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
108767ce310aSSam Leffler 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
108867ce310aSSam Leffler 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
108967ce310aSSam Leffler 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
10908a1b9b6aSSam Leffler 	}
10918a1b9b6aSSam Leffler 
1092a48a8ad7SAdrian Chadd 
1093a48a8ad7SAdrian Chadd 	/*
1094a48a8ad7SAdrian Chadd 	 * Change the contention window based on the number of associated
1095a48a8ad7SAdrian Chadd 	 * stations.  If the number of associated stations is 1 and
1096a48a8ad7SAdrian Chadd 	 * aggressive mode is enabled, lower the contention window even
1097a48a8ad7SAdrian Chadd 	 * further.
1098a48a8ad7SAdrian Chadd 	 */
1099b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1100ad262427SSam Leffler 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
110168e8e04eSSam Leffler 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1102be0df3e7SSam Leffler 		    [IEEE80211_MODE_AUTO]	= 3,
1103be0df3e7SSam Leffler 		    [IEEE80211_MODE_11A]	= 3,
1104be0df3e7SSam Leffler 		    [IEEE80211_MODE_11B]	= 4,
1105be0df3e7SSam Leffler 		    [IEEE80211_MODE_11G]	= 3,
1106be0df3e7SSam Leffler 		    [IEEE80211_MODE_FH]		= 4,
1107be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_A]	= 3,
1108be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_G]	= 3,
1109be0df3e7SSam Leffler 		    [IEEE80211_MODE_STURBO_A]	= 3,
11106a76ae21SSam Leffler 		    [IEEE80211_MODE_HALF]	= 3,
11116a76ae21SSam Leffler 		    [IEEE80211_MODE_QUARTER]	= 3,
1112be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NA]	= 3,
1113be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NG]	= 3,
11148a1b9b6aSSam Leffler 		};
11158a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
11168a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
11178a1b9b6aSSam Leffler 
111868e8e04eSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1119b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
112067ce310aSSam Leffler 		    "update %s (chan+bss) logcwmin %u\n",
112167ce310aSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
11228a1b9b6aSSam Leffler 	}
1123a48a8ad7SAdrian Chadd 
1124a48a8ad7SAdrian Chadd 	/*
1125a48a8ad7SAdrian Chadd 	 * Arrange for the beacon update.
1126a48a8ad7SAdrian Chadd 	 *
1127a48a8ad7SAdrian Chadd 	 * XXX what about MBSS, WDS?
1128a48a8ad7SAdrian Chadd 	 */
1129a48a8ad7SAdrian Chadd 	if (vap->iv_opmode == IEEE80211_M_HOSTAP
1130a48a8ad7SAdrian Chadd 	    || vap->iv_opmode == IEEE80211_M_IBSS) {
11318a1b9b6aSSam Leffler 		/*
11328a1b9b6aSSam Leffler 		 * Arrange for a beacon update and bump the parameter
11338a1b9b6aSSam Leffler 		 * set number so associated stations load the new values.
11348a1b9b6aSSam Leffler 		 */
11358a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info =
11368a1b9b6aSSam Leffler 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1137b032f27cSSam Leffler 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
11388a1b9b6aSSam Leffler 	}
11398a1b9b6aSSam Leffler 
1140dd2fb488SAdrian Chadd 	/* schedule the deferred WME update */
1141dd2fb488SAdrian Chadd 	ieee80211_runtask(ic, &ic->ic_wme_task);
11428a1b9b6aSSam Leffler 
1143b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
11448a1b9b6aSSam Leffler 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1145b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA ?
11468a1b9b6aSSam Leffler 		wme->wme_wmeChanParams.cap_info :
11478a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info);
11488a1b9b6aSSam Leffler }
11498a1b9b6aSSam Leffler 
11508a1b9b6aSSam Leffler void
1151b032f27cSSam Leffler ieee80211_wme_updateparams(struct ieee80211vap *vap)
11528a1b9b6aSSam Leffler {
1153b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
11548a1b9b6aSSam Leffler 
11558a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_WME) {
1156b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
1157b032f27cSSam Leffler 		ieee80211_wme_updateparams_locked(vap);
1158b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
11598a1b9b6aSSam Leffler 	}
11608a1b9b6aSSam Leffler }
11618a1b9b6aSSam Leffler 
1162b032f27cSSam Leffler static void
1163b032f27cSSam Leffler parent_updown(void *arg, int npending)
116468e8e04eSSam Leffler {
11657a79cebfSGleb Smirnoff 	struct ieee80211com *ic = arg;
116668e8e04eSSam Leffler 
11677a79cebfSGleb Smirnoff 	ic->ic_parent(ic);
1168b032f27cSSam Leffler }
116968e8e04eSSam Leffler 
11705efea30fSAndrew Thompson static void
11715efea30fSAndrew Thompson update_mcast(void *arg, int npending)
11725efea30fSAndrew Thompson {
11735efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
11745efea30fSAndrew Thompson 
1175272f6adeSGleb Smirnoff 	ic->ic_update_mcast(ic);
11765efea30fSAndrew Thompson }
11775efea30fSAndrew Thompson 
11785efea30fSAndrew Thompson static void
11795efea30fSAndrew Thompson update_promisc(void *arg, int npending)
11805efea30fSAndrew Thompson {
11815efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
11825efea30fSAndrew Thompson 
1183272f6adeSGleb Smirnoff 	ic->ic_update_promisc(ic);
11845efea30fSAndrew Thompson }
11855efea30fSAndrew Thompson 
11865efea30fSAndrew Thompson static void
11875efea30fSAndrew Thompson update_channel(void *arg, int npending)
11885efea30fSAndrew Thompson {
11895efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
11905efea30fSAndrew Thompson 
11915efea30fSAndrew Thompson 	ic->ic_set_channel(ic);
11925463c4a4SSam Leffler 	ieee80211_radiotap_chan_change(ic);
11935efea30fSAndrew Thompson }
11945efea30fSAndrew Thompson 
1195b94299c4SAdrian Chadd static void
1196b94299c4SAdrian Chadd update_chw(void *arg, int npending)
1197b94299c4SAdrian Chadd {
1198b94299c4SAdrian Chadd 	struct ieee80211com *ic = arg;
1199b94299c4SAdrian Chadd 
1200b94299c4SAdrian Chadd 	/*
1201b94299c4SAdrian Chadd 	 * XXX should we defer the channel width _config_ update until now?
1202b94299c4SAdrian Chadd 	 */
1203b94299c4SAdrian Chadd 	ic->ic_update_chw(ic);
1204b94299c4SAdrian Chadd }
1205b94299c4SAdrian Chadd 
1206dd2fb488SAdrian Chadd static void
1207dd2fb488SAdrian Chadd update_wme(void *arg, int npending)
1208dd2fb488SAdrian Chadd {
1209dd2fb488SAdrian Chadd 	struct ieee80211com *ic = arg;
1210dd2fb488SAdrian Chadd 
1211dd2fb488SAdrian Chadd 	/*
1212dd2fb488SAdrian Chadd 	 * XXX should we defer the WME configuration update until now?
1213dd2fb488SAdrian Chadd 	 */
1214dd2fb488SAdrian Chadd 	ic->ic_wme.wme_update(ic);
1215dd2fb488SAdrian Chadd }
1216dd2fb488SAdrian Chadd 
1217*4061c639SAndriy Voskoboinyk static void
1218*4061c639SAndriy Voskoboinyk restart_vaps(void *arg, int npending)
1219*4061c639SAndriy Voskoboinyk {
1220*4061c639SAndriy Voskoboinyk 	struct ieee80211com *ic = arg;
1221*4061c639SAndriy Voskoboinyk 
1222*4061c639SAndriy Voskoboinyk 	ieee80211_suspend_all(ic);
1223*4061c639SAndriy Voskoboinyk 	ieee80211_resume_all(ic);
1224*4061c639SAndriy Voskoboinyk }
1225*4061c639SAndriy Voskoboinyk 
122668e8e04eSSam Leffler /*
1227ae55932eSAndrew Thompson  * Block until the parent is in a known state.  This is
1228ae55932eSAndrew Thompson  * used after any operations that dispatch a task (e.g.
1229ae55932eSAndrew Thompson  * to auto-configure the parent device up/down).
1230ae55932eSAndrew Thompson  */
1231ae55932eSAndrew Thompson void
1232ae55932eSAndrew Thompson ieee80211_waitfor_parent(struct ieee80211com *ic)
1233ae55932eSAndrew Thompson {
12345efea30fSAndrew Thompson 	taskqueue_block(ic->ic_tq);
12355efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_parent_task);
12365efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_mcast_task);
12375efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_promisc_task);
12385efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_chan_task);
12395efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1240b94299c4SAdrian Chadd 	ieee80211_draintask(ic, &ic->ic_chw_task);
1241dd2fb488SAdrian Chadd 	ieee80211_draintask(ic, &ic->ic_wme_task);
12425efea30fSAndrew Thompson 	taskqueue_unblock(ic->ic_tq);
1243ae55932eSAndrew Thompson }
1244ae55932eSAndrew Thompson 
1245ae55932eSAndrew Thompson /*
124624034ddbSAdrian Chadd  * Check to see whether the current channel needs reset.
124724034ddbSAdrian Chadd  *
124824034ddbSAdrian Chadd  * Some devices don't handle being given an invalid channel
124924034ddbSAdrian Chadd  * in their operating mode very well (eg wpi(4) will throw a
125024034ddbSAdrian Chadd  * firmware exception.)
125124034ddbSAdrian Chadd  *
125224034ddbSAdrian Chadd  * Return 0 if we're ok, 1 if the channel needs to be reset.
125324034ddbSAdrian Chadd  *
125424034ddbSAdrian Chadd  * See PR kern/202502.
125524034ddbSAdrian Chadd  */
125624034ddbSAdrian Chadd static int
125724034ddbSAdrian Chadd ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
125824034ddbSAdrian Chadd {
125924034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
126024034ddbSAdrian Chadd 
126124034ddbSAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_IBSS &&
126224034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
126324034ddbSAdrian Chadd 	    (vap->iv_opmode == IEEE80211_M_HOSTAP &&
126424034ddbSAdrian Chadd 	     IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
126524034ddbSAdrian Chadd 		return (1);
126624034ddbSAdrian Chadd 	return (0);
126724034ddbSAdrian Chadd }
126824034ddbSAdrian Chadd 
126924034ddbSAdrian Chadd /*
127024034ddbSAdrian Chadd  * Reset the curchan to a known good state.
127124034ddbSAdrian Chadd  */
127224034ddbSAdrian Chadd static void
127324034ddbSAdrian Chadd ieee80211_start_reset_chan(struct ieee80211vap *vap)
127424034ddbSAdrian Chadd {
127524034ddbSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
127624034ddbSAdrian Chadd 
127724034ddbSAdrian Chadd 	ic->ic_curchan = &ic->ic_channels[0];
127824034ddbSAdrian Chadd }
127924034ddbSAdrian Chadd 
128024034ddbSAdrian Chadd /*
1281b032f27cSSam Leffler  * Start a vap running.  If this is the first vap to be
1282b032f27cSSam Leffler  * set running on the underlying device then we
1283b032f27cSSam Leffler  * automatically bring the device up.
128468e8e04eSSam Leffler  */
1285b032f27cSSam Leffler void
1286b032f27cSSam Leffler ieee80211_start_locked(struct ieee80211vap *vap)
1287b032f27cSSam Leffler {
1288b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1289b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1290b032f27cSSam Leffler 
1291b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1292b032f27cSSam Leffler 
1293b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap,
1294b032f27cSSam Leffler 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1295b032f27cSSam Leffler 		"start running, %d vaps running\n", ic->ic_nrunning);
1296b032f27cSSam Leffler 
1297b032f27cSSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1298b032f27cSSam Leffler 		/*
1299b032f27cSSam Leffler 		 * Mark us running.  Note that it's ok to do this first;
1300b032f27cSSam Leffler 		 * if we need to bring the parent device up we defer that
1301b032f27cSSam Leffler 		 * to avoid dropping the com lock.  We expect the device
1302b032f27cSSam Leffler 		 * to respond to being marked up by calling back into us
1303b032f27cSSam Leffler 		 * through ieee80211_start_all at which point we'll come
1304b032f27cSSam Leffler 		 * back in here and complete the work.
1305b032f27cSSam Leffler 		 */
1306b032f27cSSam Leffler 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1307b032f27cSSam Leffler 		/*
1308b032f27cSSam Leffler 		 * We are not running; if this we are the first vap
1309b032f27cSSam Leffler 		 * to be brought up auto-up the parent if necessary.
1310b032f27cSSam Leffler 		 */
13117a79cebfSGleb Smirnoff 		if (ic->ic_nrunning++ == 0) {
131224034ddbSAdrian Chadd 
131324034ddbSAdrian Chadd 			/* reset the channel to a known good channel */
131424034ddbSAdrian Chadd 			if (ieee80211_start_check_reset_chan(vap))
131524034ddbSAdrian Chadd 				ieee80211_start_reset_chan(vap);
131624034ddbSAdrian Chadd 
1317b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1318b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
13197a79cebfSGleb Smirnoff 			    "%s: up parent %s\n", __func__, ic->ic_name);
13205efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1321b032f27cSSam Leffler 			return;
1322b032f27cSSam Leffler 		}
1323b032f27cSSam Leffler 	}
1324b032f27cSSam Leffler 	/*
1325b032f27cSSam Leffler 	 * If the parent is up and running, then kick the
1326b032f27cSSam Leffler 	 * 802.11 state machine as appropriate.
1327b032f27cSSam Leffler 	 */
13287a79cebfSGleb Smirnoff 	if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1329b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1330b032f27cSSam Leffler #if 0
1331b032f27cSSam Leffler 			/* XXX bypasses scan too easily; disable for now */
1332b032f27cSSam Leffler 			/*
1333b032f27cSSam Leffler 			 * Try to be intelligent about clocking the state
1334b032f27cSSam Leffler 			 * machine.  If we're currently in RUN state then
1335b032f27cSSam Leffler 			 * we should be able to apply any new state/parameters
1336b032f27cSSam Leffler 			 * simply by re-associating.  Otherwise we need to
1337b032f27cSSam Leffler 			 * re-scan to select an appropriate ap.
1338b032f27cSSam Leffler 			 */
1339b032f27cSSam Leffler 			if (vap->iv_state >= IEEE80211_S_RUN)
1340b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1341b032f27cSSam Leffler 				    IEEE80211_S_ASSOC, 1);
1342b032f27cSSam Leffler 			else
1343b032f27cSSam Leffler #endif
1344b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1345b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
134668e8e04eSSam Leffler 		} else {
134768e8e04eSSam Leffler 			/*
1348b032f27cSSam Leffler 			 * For monitor+wds mode there's nothing to do but
1349b032f27cSSam Leffler 			 * start running.  Otherwise if this is the first
135068e8e04eSSam Leffler 			 * vap to be brought up, start a scan which may be
135168e8e04eSSam Leffler 			 * preempted if the station is locked to a particular
135268e8e04eSSam Leffler 			 * channel.
135368e8e04eSSam Leffler 			 */
13545efea30fSAndrew Thompson 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1355b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1356b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_WDS)
1357b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1358b032f27cSSam Leffler 				    IEEE80211_S_RUN, -1);
1359b032f27cSSam Leffler 			else
1360b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1361b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
136268e8e04eSSam Leffler 		}
136368e8e04eSSam Leffler 	}
1364b032f27cSSam Leffler }
1365b032f27cSSam Leffler 
1366b032f27cSSam Leffler /*
1367b032f27cSSam Leffler  * Start a single vap.
1368b032f27cSSam Leffler  */
1369b032f27cSSam Leffler void
1370b032f27cSSam Leffler ieee80211_init(void *arg)
1371b032f27cSSam Leffler {
1372b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1373b032f27cSSam Leffler 
137435f434b2SSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1375b032f27cSSam Leffler 	    "%s\n", __func__);
1376b032f27cSSam Leffler 
1377b032f27cSSam Leffler 	IEEE80211_LOCK(vap->iv_ic);
1378b032f27cSSam Leffler 	ieee80211_start_locked(vap);
1379b032f27cSSam Leffler 	IEEE80211_UNLOCK(vap->iv_ic);
1380b032f27cSSam Leffler }
1381b032f27cSSam Leffler 
1382b032f27cSSam Leffler /*
1383b032f27cSSam Leffler  * Start all runnable vap's on a device.
1384b032f27cSSam Leffler  */
1385b032f27cSSam Leffler void
1386b032f27cSSam Leffler ieee80211_start_all(struct ieee80211com *ic)
1387b032f27cSSam Leffler {
1388b032f27cSSam Leffler 	struct ieee80211vap *vap;
1389b032f27cSSam Leffler 
1390b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1391b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1392b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1393b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1394b032f27cSSam Leffler 			ieee80211_start_locked(vap);
1395b032f27cSSam Leffler 	}
1396b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1397b032f27cSSam Leffler }
1398b032f27cSSam Leffler 
1399b032f27cSSam Leffler /*
1400b032f27cSSam Leffler  * Stop a vap.  We force it down using the state machine
1401b032f27cSSam Leffler  * then mark it's ifnet not running.  If this is the last
1402b032f27cSSam Leffler  * vap running on the underlying device then we close it
1403b032f27cSSam Leffler  * too to insure it will be properly initialized when the
1404b032f27cSSam Leffler  * next vap is brought up.
1405b032f27cSSam Leffler  */
1406b032f27cSSam Leffler void
1407b032f27cSSam Leffler ieee80211_stop_locked(struct ieee80211vap *vap)
1408b032f27cSSam Leffler {
1409b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1410b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1411b032f27cSSam Leffler 
1412b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1413b032f27cSSam Leffler 
1414b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1415b032f27cSSam Leffler 	    "stop running, %d vaps running\n", ic->ic_nrunning);
1416b032f27cSSam Leffler 
1417b032f27cSSam Leffler 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1418b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1419b032f27cSSam Leffler 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
14207a79cebfSGleb Smirnoff 		if (--ic->ic_nrunning == 0) {
1421b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1422b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
14237a79cebfSGleb Smirnoff 			    "down parent %s\n", ic->ic_name);
14245efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1425b032f27cSSam Leffler 		}
1426b032f27cSSam Leffler 	}
1427b032f27cSSam Leffler }
1428b032f27cSSam Leffler 
1429b032f27cSSam Leffler void
1430b032f27cSSam Leffler ieee80211_stop(struct ieee80211vap *vap)
1431b032f27cSSam Leffler {
1432b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1433b032f27cSSam Leffler 
1434b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1435b032f27cSSam Leffler 	ieee80211_stop_locked(vap);
1436b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1437b032f27cSSam Leffler }
1438b032f27cSSam Leffler 
1439b032f27cSSam Leffler /*
1440b032f27cSSam Leffler  * Stop all vap's running on a device.
1441b032f27cSSam Leffler  */
1442b032f27cSSam Leffler void
1443b032f27cSSam Leffler ieee80211_stop_all(struct ieee80211com *ic)
1444b032f27cSSam Leffler {
1445b032f27cSSam Leffler 	struct ieee80211vap *vap;
1446b032f27cSSam Leffler 
1447b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1448b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1449b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1450b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1451b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
1452b032f27cSSam Leffler 	}
1453b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1454ae55932eSAndrew Thompson 
1455ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
145668e8e04eSSam Leffler }
145768e8e04eSSam Leffler 
145868e8e04eSSam Leffler /*
14596076cbacSSam Leffler  * Stop all vap's running on a device and arrange
14606076cbacSSam Leffler  * for those that were running to be resumed.
14616076cbacSSam Leffler  */
14626076cbacSSam Leffler void
14636076cbacSSam Leffler ieee80211_suspend_all(struct ieee80211com *ic)
14646076cbacSSam Leffler {
14656076cbacSSam Leffler 	struct ieee80211vap *vap;
14666076cbacSSam Leffler 
14676076cbacSSam Leffler 	IEEE80211_LOCK(ic);
14686076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
14696076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
14706076cbacSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
14716076cbacSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
14726076cbacSSam Leffler 			ieee80211_stop_locked(vap);
14736076cbacSSam Leffler 		}
14746076cbacSSam Leffler 	}
14756076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
1476ae55932eSAndrew Thompson 
1477ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
14786076cbacSSam Leffler }
14796076cbacSSam Leffler 
14806076cbacSSam Leffler /*
14816076cbacSSam Leffler  * Start all vap's marked for resume.
14826076cbacSSam Leffler  */
14836076cbacSSam Leffler void
14846076cbacSSam Leffler ieee80211_resume_all(struct ieee80211com *ic)
14856076cbacSSam Leffler {
14866076cbacSSam Leffler 	struct ieee80211vap *vap;
14876076cbacSSam Leffler 
14886076cbacSSam Leffler 	IEEE80211_LOCK(ic);
14896076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
14906076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
14916076cbacSSam Leffler 		if (!IFNET_IS_UP_RUNNING(ifp) &&
14926076cbacSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
14936076cbacSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
14946076cbacSSam Leffler 			ieee80211_start_locked(vap);
14956076cbacSSam Leffler 		}
14966076cbacSSam Leffler 	}
14976076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
14986076cbacSSam Leffler }
14996076cbacSSam Leffler 
1500*4061c639SAndriy Voskoboinyk /*
1501*4061c639SAndriy Voskoboinyk  * Restart all vap's running on a device.
1502*4061c639SAndriy Voskoboinyk  */
1503*4061c639SAndriy Voskoboinyk void
1504*4061c639SAndriy Voskoboinyk ieee80211_restart_all(struct ieee80211com *ic)
1505*4061c639SAndriy Voskoboinyk {
1506*4061c639SAndriy Voskoboinyk 	/*
1507*4061c639SAndriy Voskoboinyk 	 * NB: do not use ieee80211_runtask here, we will
1508*4061c639SAndriy Voskoboinyk 	 * block & drain net80211 taskqueue.
1509*4061c639SAndriy Voskoboinyk 	 */
1510*4061c639SAndriy Voskoboinyk 	taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
1511*4061c639SAndriy Voskoboinyk }
1512*4061c639SAndriy Voskoboinyk 
1513e701e041SSam Leffler void
1514e701e041SSam Leffler ieee80211_beacon_miss(struct ieee80211com *ic)
1515e701e041SSam Leffler {
15165efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
15175efea30fSAndrew Thompson 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
15185efea30fSAndrew Thompson 		/* Process in a taskq, the handler may reenter the driver */
15195efea30fSAndrew Thompson 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
15205efea30fSAndrew Thompson 	}
15215efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
15225efea30fSAndrew Thompson }
15235efea30fSAndrew Thompson 
15245efea30fSAndrew Thompson static void
15255efea30fSAndrew Thompson beacon_miss(void *arg, int npending)
15265efea30fSAndrew Thompson {
15275efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
1528b032f27cSSam Leffler 	struct ieee80211vap *vap;
1529e701e041SSam Leffler 
153023401900SAdrian Chadd 	IEEE80211_LOCK(ic);
1531b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1532e701e041SSam Leffler 		/*
1533b032f27cSSam Leffler 		 * We only pass events through for sta vap's in RUN state;
1534b032f27cSSam Leffler 		 * may be too restrictive but for now this saves all the
1535b032f27cSSam Leffler 		 * handlers duplicating these checks.
1536e701e041SSam Leffler 		 */
1537b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
1538c70761e6SSam Leffler 		    vap->iv_state >= IEEE80211_S_RUN &&
1539b032f27cSSam Leffler 		    vap->iv_bmiss != NULL)
1540b032f27cSSam Leffler 			vap->iv_bmiss(vap);
1541e701e041SSam Leffler 	}
154223401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
154368e8e04eSSam Leffler }
1544e701e041SSam Leffler 
15455efea30fSAndrew Thompson static void
15465efea30fSAndrew Thompson beacon_swmiss(void *arg, int npending)
15475efea30fSAndrew Thompson {
15485efea30fSAndrew Thompson 	struct ieee80211vap *vap = arg;
154923401900SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
15505efea30fSAndrew Thompson 
155123401900SAdrian Chadd 	IEEE80211_LOCK(ic);
155223401900SAdrian Chadd 	if (vap->iv_state == IEEE80211_S_RUN) {
15535efea30fSAndrew Thompson 		/* XXX Call multiple times if npending > zero? */
15545efea30fSAndrew Thompson 		vap->iv_bmiss(vap);
15555efea30fSAndrew Thompson 	}
155623401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
155723401900SAdrian Chadd }
15585efea30fSAndrew Thompson 
1559e99662a6SSam Leffler /*
1560e99662a6SSam Leffler  * Software beacon miss handling.  Check if any beacons
1561e99662a6SSam Leffler  * were received in the last period.  If not post a
1562e99662a6SSam Leffler  * beacon miss; otherwise reset the counter.
1563e99662a6SSam Leffler  */
1564b032f27cSSam Leffler void
1565e99662a6SSam Leffler ieee80211_swbmiss(void *arg)
1566e99662a6SSam Leffler {
1567b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1568c448998dSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1569e99662a6SSam Leffler 
157023401900SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
157123401900SAdrian Chadd 
1572c448998dSSam Leffler 	/* XXX sleep state? */
1573c448998dSSam Leffler 	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1574c448998dSSam Leffler 	    ("wrong state %d", vap->iv_state));
1575c448998dSSam Leffler 
1576c448998dSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN) {
1577c448998dSSam Leffler 		/*
1578c448998dSSam Leffler 		 * If scanning just ignore and reset state.  If we get a
1579c448998dSSam Leffler 		 * bmiss after coming out of scan because we haven't had
1580c448998dSSam Leffler 		 * time to receive a beacon then we should probe the AP
1581c448998dSSam Leffler 		 * before posting a real bmiss (unless iv_bmiss_max has
1582c448998dSSam Leffler 		 * been artifiically lowered).  A cleaner solution might
1583c448998dSSam Leffler 		 * be to disable the timer on scan start/end but to handle
1584c448998dSSam Leffler 		 * case of multiple sta vap's we'd need to disable the
1585c448998dSSam Leffler 		 * timers of all affected vap's.
1586c448998dSSam Leffler 		 */
1587c448998dSSam Leffler 		vap->iv_swbmiss_count = 0;
1588c448998dSSam Leffler 	} else if (vap->iv_swbmiss_count == 0) {
1589b032f27cSSam Leffler 		if (vap->iv_bmiss != NULL)
15905efea30fSAndrew Thompson 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1591e99662a6SSam Leffler 	} else
1592b032f27cSSam Leffler 		vap->iv_swbmiss_count = 0;
1593b032f27cSSam Leffler 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1594b032f27cSSam Leffler 		ieee80211_swbmiss, vap);
15957edb8cf9SSam Leffler }
15967edb8cf9SSam Leffler 
159768e8e04eSSam Leffler /*
1598b032f27cSSam Leffler  * Start an 802.11h channel switch.  We record the parameters,
1599b032f27cSSam Leffler  * mark the operation pending, notify each vap through the
1600b032f27cSSam Leffler  * beacon update mechanism so it can update the beacon frame
1601b032f27cSSam Leffler  * contents, and then switch vap's to CSA state to block outbound
1602b032f27cSSam Leffler  * traffic.  Devices that handle CSA directly can use the state
1603b032f27cSSam Leffler  * switch to do the right thing so long as they call
1604b032f27cSSam Leffler  * ieee80211_csa_completeswitch when it's time to complete the
1605b032f27cSSam Leffler  * channel change.  Devices that depend on the net80211 layer can
1606b032f27cSSam Leffler  * use ieee80211_beacon_update to handle the countdown and the
1607b032f27cSSam Leffler  * channel switch.
1608b032f27cSSam Leffler  */
1609b032f27cSSam Leffler void
1610b032f27cSSam Leffler ieee80211_csa_startswitch(struct ieee80211com *ic,
1611b032f27cSSam Leffler 	struct ieee80211_channel *c, int mode, int count)
1612b032f27cSSam Leffler {
1613b032f27cSSam Leffler 	struct ieee80211vap *vap;
1614b032f27cSSam Leffler 
1615b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1616b032f27cSSam Leffler 
1617b032f27cSSam Leffler 	ic->ic_csa_newchan = c;
1618c70761e6SSam Leffler 	ic->ic_csa_mode = mode;
1619b032f27cSSam Leffler 	ic->ic_csa_count = count;
1620b032f27cSSam Leffler 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1621b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1622b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
162359aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_IBSS ||
162459aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_MBSS)
1625b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1626b032f27cSSam Leffler 		/* switch to CSA state to block outbound traffic */
1627b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN)
1628b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1629b032f27cSSam Leffler 	}
1630b032f27cSSam Leffler 	ieee80211_notify_csa(ic, c, mode, count);
1631b032f27cSSam Leffler }
1632b032f27cSSam Leffler 
1633886bbec1SAdrian Chadd /*
1634886bbec1SAdrian Chadd  * Complete the channel switch by transitioning all CSA VAPs to RUN.
1635886bbec1SAdrian Chadd  * This is called by both the completion and cancellation functions
1636886bbec1SAdrian Chadd  * so each VAP is placed back in the RUN state and can thus transmit.
1637886bbec1SAdrian Chadd  */
1638c70761e6SSam Leffler static void
1639c70761e6SSam Leffler csa_completeswitch(struct ieee80211com *ic)
1640c70761e6SSam Leffler {
1641c70761e6SSam Leffler 	struct ieee80211vap *vap;
1642c70761e6SSam Leffler 
1643c70761e6SSam Leffler 	ic->ic_csa_newchan = NULL;
1644c70761e6SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1645c70761e6SSam Leffler 
1646c70761e6SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1647c70761e6SSam Leffler 		if (vap->iv_state == IEEE80211_S_CSA)
1648c70761e6SSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1649c70761e6SSam Leffler }
1650c70761e6SSam Leffler 
1651b032f27cSSam Leffler /*
1652b032f27cSSam Leffler  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1653b032f27cSSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1654b032f27cSSam Leffler  * so they can again transmit.
1655886bbec1SAdrian Chadd  *
1656886bbec1SAdrian Chadd  * Although this may not be completely correct, update the BSS channel
1657886bbec1SAdrian Chadd  * for each VAP to the newly configured channel. The setcurchan sets
1658886bbec1SAdrian Chadd  * the current operating channel for the interface (so the radio does
1659886bbec1SAdrian Chadd  * switch over) but the VAP BSS isn't updated, leading to incorrectly
1660886bbec1SAdrian Chadd  * reported information via ioctl.
1661b032f27cSSam Leffler  */
1662b032f27cSSam Leffler void
1663b032f27cSSam Leffler ieee80211_csa_completeswitch(struct ieee80211com *ic)
1664b032f27cSSam Leffler {
16656f16ec31SAdrian Chadd 	struct ieee80211vap *vap;
16666f16ec31SAdrian Chadd 
1667b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1668b032f27cSSam Leffler 
1669b032f27cSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1670b032f27cSSam Leffler 
1671b032f27cSSam Leffler 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1672886bbec1SAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1673886bbec1SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_CSA)
1674886bbec1SAdrian Chadd 			vap->iv_bss->ni_chan = ic->ic_curchan;
1675886bbec1SAdrian Chadd 
1676c70761e6SSam Leffler 	csa_completeswitch(ic);
1677c70761e6SSam Leffler }
1678b032f27cSSam Leffler 
1679c70761e6SSam Leffler /*
1680c70761e6SSam Leffler  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1681c70761e6SSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1682c70761e6SSam Leffler  * so they can again transmit.
1683c70761e6SSam Leffler  */
1684c70761e6SSam Leffler void
1685c70761e6SSam Leffler ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1686c70761e6SSam Leffler {
1687c70761e6SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1688c70761e6SSam Leffler 
1689c70761e6SSam Leffler 	csa_completeswitch(ic);
1690b032f27cSSam Leffler }
1691b032f27cSSam Leffler 
1692b032f27cSSam Leffler /*
1693b032f27cSSam Leffler  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1694b032f27cSSam Leffler  * We clear state and move all vap's in CAC state to RUN state.
1695b032f27cSSam Leffler  */
1696b032f27cSSam Leffler void
1697b032f27cSSam Leffler ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1698b032f27cSSam Leffler {
1699b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1700b032f27cSSam Leffler 	struct ieee80211vap *vap;
1701b032f27cSSam Leffler 
1702b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1703b032f27cSSam Leffler 	/*
1704b032f27cSSam Leffler 	 * Complete CAC state change for lead vap first; then
1705b032f27cSSam Leffler 	 * clock all the other vap's waiting.
1706b032f27cSSam Leffler 	 */
1707b032f27cSSam Leffler 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1708b032f27cSSam Leffler 	    ("wrong state %d", vap0->iv_state));
1709b032f27cSSam Leffler 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1710b032f27cSSam Leffler 
1711b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1712b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_CAC)
1713b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1714b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1715b032f27cSSam Leffler }
1716b032f27cSSam Leffler 
1717b032f27cSSam Leffler /*
1718b032f27cSSam Leffler  * Force all vap's other than the specified vap to the INIT state
1719b032f27cSSam Leffler  * and mark them as waiting for a scan to complete.  These vaps
1720b032f27cSSam Leffler  * will be brought up when the scan completes and the scanning vap
1721b032f27cSSam Leffler  * reaches RUN state by wakeupwaiting.
172268e8e04eSSam Leffler  */
172368e8e04eSSam Leffler static void
1724b032f27cSSam Leffler markwaiting(struct ieee80211vap *vap0)
172568e8e04eSSam Leffler {
1726b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1727b032f27cSSam Leffler 	struct ieee80211vap *vap;
1728b032f27cSSam Leffler 
1729b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1730b032f27cSSam Leffler 
17315efea30fSAndrew Thompson 	/*
17325efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
17335efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
17345efea30fSAndrew Thompson 	 * change task.
17355efea30fSAndrew Thompson 	 */
1736b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1737b032f27cSSam Leffler 		if (vap == vap0)
1738b032f27cSSam Leffler 			continue;
1739b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_INIT) {
17405efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1741b032f27cSSam Leffler 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1742dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1743b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1744b032f27cSSam Leffler 		}
174568e8e04eSSam Leffler 	}
174668e8e04eSSam Leffler }
174768e8e04eSSam Leffler 
1748b032f27cSSam Leffler /*
1749b032f27cSSam Leffler  * Wakeup all vap's waiting for a scan to complete.  This is the
1750b032f27cSSam Leffler  * companion to markwaiting (above) and is used to coordinate
1751b032f27cSSam Leffler  * multiple vaps scanning.
17525efea30fSAndrew Thompson  * This is called from the state taskqueue.
1753b032f27cSSam Leffler  */
1754b032f27cSSam Leffler static void
1755b032f27cSSam Leffler wakeupwaiting(struct ieee80211vap *vap0)
1756b032f27cSSam Leffler {
1757b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1758b032f27cSSam Leffler 	struct ieee80211vap *vap;
1759b032f27cSSam Leffler 
1760b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1761b032f27cSSam Leffler 
17625efea30fSAndrew Thompson 	/*
17635efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
17645efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
17655efea30fSAndrew Thompson 	 * change task.
17665efea30fSAndrew Thompson 	 */
1767b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1768b032f27cSSam Leffler 		if (vap == vap0)
1769b032f27cSSam Leffler 			continue;
1770b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1771b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1772b032f27cSSam Leffler 			/* NB: sta's cannot go INIT->RUN */
17735efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1774b032f27cSSam Leffler 			vap->iv_newstate(vap,
1775b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_STA ?
1776b032f27cSSam Leffler 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1777dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1778b032f27cSSam Leffler 		}
1779b032f27cSSam Leffler 	}
1780b032f27cSSam Leffler }
1781b032f27cSSam Leffler 
1782b032f27cSSam Leffler /*
1783b032f27cSSam Leffler  * Handle post state change work common to all operating modes.
1784b032f27cSSam Leffler  */
1785b032f27cSSam Leffler static void
17865efea30fSAndrew Thompson ieee80211_newstate_cb(void *xvap, int npending)
1787b032f27cSSam Leffler {
17885efea30fSAndrew Thompson 	struct ieee80211vap *vap = xvap;
1789b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17905efea30fSAndrew Thompson 	enum ieee80211_state nstate, ostate;
17915efea30fSAndrew Thompson 	int arg, rc;
1792b032f27cSSam Leffler 
17935efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
17945efea30fSAndrew Thompson 	nstate = vap->iv_nstate;
17955efea30fSAndrew Thompson 	arg = vap->iv_nstate_arg;
1796b032f27cSSam Leffler 
17975efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
17985efea30fSAndrew Thompson 		/*
17995efea30fSAndrew Thompson 		 * We have been requested to drop back to the INIT before
18005efea30fSAndrew Thompson 		 * proceeding to the new state.
18015efea30fSAndrew Thompson 		 */
1802b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
18035efea30fSAndrew Thompson 		    "%s: %s -> %s arg %d\n", __func__,
18045efea30fSAndrew Thompson 		    ieee80211_state_name[vap->iv_state],
18055efea30fSAndrew Thompson 		    ieee80211_state_name[IEEE80211_S_INIT], arg);
18065efea30fSAndrew Thompson 		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1807dcc56af0SAdrian Chadd 		IEEE80211_LOCK_ASSERT(ic);
18085efea30fSAndrew Thompson 		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
18095efea30fSAndrew Thompson 	}
18105efea30fSAndrew Thompson 
18115efea30fSAndrew Thompson 	ostate = vap->iv_state;
18125efea30fSAndrew Thompson 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
18135efea30fSAndrew Thompson 		/*
18145efea30fSAndrew Thompson 		 * SCAN was forced; e.g. on beacon miss.  Force other running
18155efea30fSAndrew Thompson 		 * vap's to INIT state and mark them as waiting for the scan to
18165efea30fSAndrew Thompson 		 * complete.  This insures they don't interfere with our
18175efea30fSAndrew Thompson 		 * scanning.  Since we are single threaded the vaps can not
18185efea30fSAndrew Thompson 		 * transition again while we are executing.
18195efea30fSAndrew Thompson 		 *
18205efea30fSAndrew Thompson 		 * XXX not always right, assumes ap follows sta
18215efea30fSAndrew Thompson 		 */
18225efea30fSAndrew Thompson 		markwaiting(vap);
18235efea30fSAndrew Thompson 	}
18245efea30fSAndrew Thompson 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
18255efea30fSAndrew Thompson 	    "%s: %s -> %s arg %d\n", __func__,
18265efea30fSAndrew Thompson 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
18275efea30fSAndrew Thompson 
18285efea30fSAndrew Thompson 	rc = vap->iv_newstate(vap, nstate, arg);
1829dcc56af0SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
18305efea30fSAndrew Thompson 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
18315efea30fSAndrew Thompson 	if (rc != 0) {
18325efea30fSAndrew Thompson 		/* State transition failed */
18335efea30fSAndrew Thompson 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
18345efea30fSAndrew Thompson 		KASSERT(nstate != IEEE80211_S_INIT,
18355efea30fSAndrew Thompson 		    ("INIT state change failed"));
18365efea30fSAndrew Thompson 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
18375efea30fSAndrew Thompson 		    "%s: %s returned error %d\n", __func__,
18385efea30fSAndrew Thompson 		    ieee80211_state_name[nstate], rc);
18395efea30fSAndrew Thompson 		goto done;
18405efea30fSAndrew Thompson 	}
18415efea30fSAndrew Thompson 
18425efea30fSAndrew Thompson 	/* No actual transition, skip post processing */
18435efea30fSAndrew Thompson 	if (ostate == nstate)
18445efea30fSAndrew Thompson 		goto done;
1845b032f27cSSam Leffler 
1846b032f27cSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
1847b032f27cSSam Leffler 		/*
1848b032f27cSSam Leffler 		 * OACTIVE may be set on the vap if the upper layer
1849b032f27cSSam Leffler 		 * tried to transmit (e.g. IPv6 NDP) before we reach
1850b032f27cSSam Leffler 		 * RUN state.  Clear it and restart xmit.
1851b032f27cSSam Leffler 		 *
1852b032f27cSSam Leffler 		 * Note this can also happen as a result of SLEEP->RUN
1853b032f27cSSam Leffler 		 * (i.e. coming out of power save mode).
1854b032f27cSSam Leffler 		 */
1855b032f27cSSam Leffler 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1856a7f31a36SAdrian Chadd 
1857a7f31a36SAdrian Chadd 		/*
1858e7495198SAdrian Chadd 		 * XXX TODO Kick-start a VAP queue - this should be a method!
1859a7f31a36SAdrian Chadd 		 */
1860b032f27cSSam Leffler 
1861b032f27cSSam Leffler 		/* bring up any vaps waiting on us */
1862b032f27cSSam Leffler 		wakeupwaiting(vap);
1863b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
1864b032f27cSSam Leffler 		/*
1865b032f27cSSam Leffler 		 * Flush the scan cache if we did the last scan (XXX?)
1866b032f27cSSam Leffler 		 * and flush any frames on send queues from this vap.
1867b032f27cSSam Leffler 		 * Note the mgt q is used only for legacy drivers and
1868b032f27cSSam Leffler 		 * will go away shortly.
1869b032f27cSSam Leffler 		 */
1870b032f27cSSam Leffler 		ieee80211_scan_flush(vap);
1871b032f27cSSam Leffler 
1872e7495198SAdrian Chadd 		/*
1873e7495198SAdrian Chadd 		 * XXX TODO: ic/vap queue flush
1874e7495198SAdrian Chadd 		 */
1875b032f27cSSam Leffler 	}
18765efea30fSAndrew Thompson done:
18775efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
1878b032f27cSSam Leffler }
1879b032f27cSSam Leffler 
1880b032f27cSSam Leffler /*
1881b032f27cSSam Leffler  * Public interface for initiating a state machine change.
1882b032f27cSSam Leffler  * This routine single-threads the request and coordinates
1883b032f27cSSam Leffler  * the scheduling of multiple vaps for the purpose of selecting
1884b032f27cSSam Leffler  * an operating channel.  Specifically the following scenarios
1885b032f27cSSam Leffler  * are handled:
1886b032f27cSSam Leffler  * o only one vap can be selecting a channel so on transition to
1887b032f27cSSam Leffler  *   SCAN state if another vap is already scanning then
1888b032f27cSSam Leffler  *   mark the caller for later processing and return without
1889b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
1890b032f27cSSam Leffler  * o only one vap can be doing CAC of a channel so on transition to
1891b032f27cSSam Leffler  *   CAC state if another vap is already scanning for radar then
1892b032f27cSSam Leffler  *   mark the caller for later processing and return without
1893b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
1894b032f27cSSam Leffler  * o if another vap is already running when a request is made
1895b032f27cSSam Leffler  *   to SCAN then an operating channel has been chosen; bypass
1896b032f27cSSam Leffler  *   the scan and just join the channel
1897b032f27cSSam Leffler  *
1898b032f27cSSam Leffler  * Note that the state change call is done through the iv_newstate
1899b032f27cSSam Leffler  * method pointer so any driver routine gets invoked.  The driver
1900b032f27cSSam Leffler  * will normally call back into operating mode-specific
1901b032f27cSSam Leffler  * ieee80211_newstate routines (below) unless it needs to completely
1902b032f27cSSam Leffler  * bypass the state machine (e.g. because the firmware has it's
1903b032f27cSSam Leffler  * own idea how things should work).  Bypassing the net80211 layer
1904b032f27cSSam Leffler  * is usually a mistake and indicates lack of proper integration
1905b032f27cSSam Leffler  * with the net80211 layer.
1906b032f27cSSam Leffler  */
1907e94527beSAdrian Chadd int
1908b032f27cSSam Leffler ieee80211_new_state_locked(struct ieee80211vap *vap,
1909b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
19108a1b9b6aSSam Leffler {
1911b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1912b032f27cSSam Leffler 	struct ieee80211vap *vp;
1913a11c9a5cSSam Leffler 	enum ieee80211_state ostate;
19145efea30fSAndrew Thompson 	int nrunning, nscanning;
19151a1e1d21SSam Leffler 
1916b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1917b032f27cSSam Leffler 
19185efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
19195efea30fSAndrew Thompson 		if (vap->iv_nstate == IEEE80211_S_INIT) {
19205efea30fSAndrew Thompson 			/*
19215efea30fSAndrew Thompson 			 * XXX The vap is being stopped, do no allow any other
19225efea30fSAndrew Thompson 			 * state changes until this is completed.
19235efea30fSAndrew Thompson 			 */
19245efea30fSAndrew Thompson 			return -1;
19258ee6f90aSAndrew Thompson 		} else if (vap->iv_state != vap->iv_nstate) {
19265efea30fSAndrew Thompson #if 0
19275efea30fSAndrew Thompson 			/* Warn if the previous state hasn't completed. */
19285efea30fSAndrew Thompson 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
19295efea30fSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n", __func__,
19305efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_state],
19315efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
19325efea30fSAndrew Thompson #else
19335efea30fSAndrew Thompson 			/* XXX temporarily enable to identify issues */
19348ee6f90aSAndrew Thompson 			if_printf(vap->iv_ifp,
19358ee6f90aSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n",
19365efea30fSAndrew Thompson 			    __func__, ieee80211_state_name[vap->iv_state],
19375efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
19385efea30fSAndrew Thompson #endif
19395efea30fSAndrew Thompson 		}
19408ee6f90aSAndrew Thompson 	}
19415efea30fSAndrew Thompson 
1942b032f27cSSam Leffler 	nrunning = nscanning = 0;
1943b032f27cSSam Leffler 	/* XXX can track this state instead of calculating */
1944b032f27cSSam Leffler 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1945b032f27cSSam Leffler 		if (vp != vap) {
1946b032f27cSSam Leffler 			if (vp->iv_state >= IEEE80211_S_RUN)
1947b032f27cSSam Leffler 				nrunning++;
1948b032f27cSSam Leffler 			/* XXX doesn't handle bg scan */
1949b032f27cSSam Leffler 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1950b032f27cSSam Leffler 			else if (vp->iv_state > IEEE80211_S_INIT)
1951b032f27cSSam Leffler 				nscanning++;
1952b032f27cSSam Leffler 		}
1953b032f27cSSam Leffler 	}
1954b032f27cSSam Leffler 	ostate = vap->iv_state;
1955b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1956b032f27cSSam Leffler 	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1957b032f27cSSam Leffler 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1958b032f27cSSam Leffler 	    nrunning, nscanning);
19591a1e1d21SSam Leffler 	switch (nstate) {
19601a1e1d21SSam Leffler 	case IEEE80211_S_SCAN:
1961b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT) {
19621a1e1d21SSam Leffler 			/*
1963b032f27cSSam Leffler 			 * INIT -> SCAN happens on initial bringup.
19641a1e1d21SSam Leffler 			 */
1965b032f27cSSam Leffler 			KASSERT(!(nscanning && nrunning),
1966b032f27cSSam Leffler 			    ("%d scanning and %d running", nscanning, nrunning));
1967b032f27cSSam Leffler 			if (nscanning) {
196868e8e04eSSam Leffler 				/*
1969b032f27cSSam Leffler 				 * Someone is scanning, defer our state
1970b032f27cSSam Leffler 				 * change until the work has completed.
197168e8e04eSSam Leffler 				 */
1972b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1973b032f27cSSam Leffler 				    "%s: defer %s -> %s\n",
1974b032f27cSSam Leffler 				    __func__, ieee80211_state_name[ostate],
1975b032f27cSSam Leffler 				    ieee80211_state_name[nstate]);
1976b032f27cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
19775efea30fSAndrew Thompson 				return 0;
197868e8e04eSSam Leffler 			}
1979b032f27cSSam Leffler 			if (nrunning) {
198068e8e04eSSam Leffler 				/*
1981b032f27cSSam Leffler 				 * Someone is operating; just join the channel
1982b032f27cSSam Leffler 				 * they have chosen.
198368e8e04eSSam Leffler 				 */
1984b032f27cSSam Leffler 				/* XXX kill arg? */
1985b032f27cSSam Leffler 				/* XXX check each opmode, adhoc? */
1986b032f27cSSam Leffler 				if (vap->iv_opmode == IEEE80211_M_STA)
1987b032f27cSSam Leffler 					nstate = IEEE80211_S_SCAN;
19881a1e1d21SSam Leffler 				else
1989b032f27cSSam Leffler 					nstate = IEEE80211_S_RUN;
1990b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
1991b032f27cSSam Leffler 				if (nstate != IEEE80211_S_SCAN) {
1992b032f27cSSam Leffler 					IEEE80211_DPRINTF(vap,
1993b032f27cSSam Leffler 					    IEEE80211_MSG_STATE,
1994b032f27cSSam Leffler 					    "%s: override, now %s -> %s\n",
1995b032f27cSSam Leffler 					    __func__,
1996b032f27cSSam Leffler 					    ieee80211_state_name[ostate],
1997b032f27cSSam Leffler 					    ieee80211_state_name[nstate]);
19981a1e1d21SSam Leffler 				}
19998a1b9b6aSSam Leffler #endif
200068e8e04eSSam Leffler 			}
2001b032f27cSSam Leffler 		}
20021a1e1d21SSam Leffler 		break;
2003b032f27cSSam Leffler 	case IEEE80211_S_RUN:
2004b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS &&
2005b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2006b032f27cSSam Leffler 		    nscanning) {
2007b032f27cSSam Leffler 			/*
2008b032f27cSSam Leffler 			 * Legacy WDS with someone else scanning; don't
2009b032f27cSSam Leffler 			 * go online until that completes as we should
2010b032f27cSSam Leffler 			 * follow the other vap to the channel they choose.
2011b032f27cSSam Leffler 			 */
2012b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2013b032f27cSSam Leffler 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
2014b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2015b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2016b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
20175efea30fSAndrew Thompson 			return 0;
2018b032f27cSSam Leffler 		}
2019b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2020b032f27cSSam Leffler 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2021b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2022b032f27cSSam Leffler 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2023b032f27cSSam Leffler 			/*
2024b032f27cSSam Leffler 			 * This is a DFS channel, transition to CAC state
2025b032f27cSSam Leffler 			 * instead of RUN.  This allows us to initiate
2026b032f27cSSam Leffler 			 * Channel Availability Check (CAC) as specified
2027b032f27cSSam Leffler 			 * by 11h/DFS.
2028b032f27cSSam Leffler 			 */
2029b032f27cSSam Leffler 			nstate = IEEE80211_S_CAC;
2030b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2031b032f27cSSam Leffler 			     "%s: override %s -> %s (DFS)\n", __func__,
2032b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
2033b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
2034b032f27cSSam Leffler 		}
2035b032f27cSSam Leffler 		break;
2036b032f27cSSam Leffler 	case IEEE80211_S_INIT:
2037b016f58cSAndrew Thompson 		/* cancel any scan in progress */
2038b016f58cSAndrew Thompson 		ieee80211_cancel_scan(vap);
2039b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT ) {
2040b032f27cSSam Leffler 			/* XXX don't believe this */
2041b032f27cSSam Leffler 			/* INIT -> INIT. nothing to do */
2042b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2043b032f27cSSam Leffler 		}
2044b032f27cSSam Leffler 		/* fall thru... */
204514fb6b8fSSam Leffler 	default:
204614fb6b8fSSam Leffler 		break;
20471a1e1d21SSam Leffler 	}
20485efea30fSAndrew Thompson 	/* defer the state change to a thread */
20495efea30fSAndrew Thompson 	vap->iv_nstate = nstate;
20505efea30fSAndrew Thompson 	vap->iv_nstate_arg = arg;
20515efea30fSAndrew Thompson 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
20525efea30fSAndrew Thompson 	ieee80211_runtask(ic, &vap->iv_nstate_task);
20535efea30fSAndrew Thompson 	return EINPROGRESS;
20548a1b9b6aSSam Leffler }
2055b032f27cSSam Leffler 
2056b032f27cSSam Leffler int
2057b032f27cSSam Leffler ieee80211_new_state(struct ieee80211vap *vap,
2058b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
2059b032f27cSSam Leffler {
2060b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2061b032f27cSSam Leffler 	int rc;
2062b032f27cSSam Leffler 
2063b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2064b032f27cSSam Leffler 	rc = ieee80211_new_state_locked(vap, nstate, arg);
2065b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
2066b032f27cSSam Leffler 	return rc;
20671a1e1d21SSam Leffler }
2068