xref: /freebsd/sys/net80211/ieee80211_proto.c (revision b94299c4377dee4d13001cb74134b26137c10a7c)
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>
451a1e1d21SSam Leffler #include <net/if_media.h>
468a1b9b6aSSam Leffler #include <net/ethernet.h>		/* XXX for ether_sprintf */
471a1e1d21SSam Leffler 
481a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
49b032f27cSSam Leffler #include <net80211/ieee80211_adhoc.h>
50b032f27cSSam Leffler #include <net80211/ieee80211_sta.h>
51b032f27cSSam Leffler #include <net80211/ieee80211_hostap.h>
52b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
5459aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
5559aa14a9SRui Paulo #endif
56b032f27cSSam Leffler #include <net80211/ieee80211_monitor.h>
57b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
581a1e1d21SSam Leffler 
598a1b9b6aSSam Leffler /* XXX tunables */
608a1b9b6aSSam Leffler #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
618a1b9b6aSSam Leffler #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
621a1e1d21SSam Leffler 
631a1e1d21SSam Leffler const char *ieee80211_mgt_subtype_name[] = {
641a1e1d21SSam Leffler 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
651a1e1d21SSam Leffler 	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
661a1e1d21SSam Leffler 	"beacon",	"atim",		"disassoc",	"auth",
6796283082SBernhard Schmidt 	"deauth",	"action",	"action_noack",	"reserved#15"
681a1e1d21SSam Leffler };
698a1b9b6aSSam Leffler const char *ieee80211_ctl_subtype_name[] = {
708a1b9b6aSSam Leffler 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
718a1b9b6aSSam Leffler 	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
728a1b9b6aSSam Leffler 	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
738a1b9b6aSSam Leffler 	"cts",		"ack",		"cf_end",	"cf_end_ack"
748a1b9b6aSSam Leffler };
7549aa47d6SSam Leffler const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
7649aa47d6SSam Leffler 	"IBSS",		/* IEEE80211_M_IBSS */
7749aa47d6SSam Leffler 	"STA",		/* IEEE80211_M_STA */
78b032f27cSSam Leffler 	"WDS",		/* IEEE80211_M_WDS */
7949aa47d6SSam Leffler 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
8049aa47d6SSam Leffler 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
8159aa14a9SRui Paulo 	"MONITOR",	/* IEEE80211_M_MONITOR */
8259aa14a9SRui Paulo 	"MBSS"		/* IEEE80211_M_MBSS */
8349aa47d6SSam Leffler };
84a11c9a5cSSam Leffler const char *ieee80211_state_name[IEEE80211_S_MAX] = {
85a11c9a5cSSam Leffler 	"INIT",		/* IEEE80211_S_INIT */
86a11c9a5cSSam Leffler 	"SCAN",		/* IEEE80211_S_SCAN */
87a11c9a5cSSam Leffler 	"AUTH",		/* IEEE80211_S_AUTH */
88a11c9a5cSSam Leffler 	"ASSOC",	/* IEEE80211_S_ASSOC */
8914fb6b8fSSam Leffler 	"CAC",		/* IEEE80211_S_CAC */
9014fb6b8fSSam Leffler 	"RUN",		/* IEEE80211_S_RUN */
9114fb6b8fSSam Leffler 	"CSA",		/* IEEE80211_S_CSA */
9214fb6b8fSSam Leffler 	"SLEEP",	/* IEEE80211_S_SLEEP */
93a11c9a5cSSam Leffler };
948a1b9b6aSSam Leffler const char *ieee80211_wme_acnames[] = {
958a1b9b6aSSam Leffler 	"WME_AC_BE",
968a1b9b6aSSam Leffler 	"WME_AC_BK",
978a1b9b6aSSam Leffler 	"WME_AC_VI",
988a1b9b6aSSam Leffler 	"WME_AC_VO",
998a1b9b6aSSam Leffler 	"WME_UPSD",
1008a1b9b6aSSam Leffler };
101a11c9a5cSSam Leffler 
1025efea30fSAndrew Thompson static void beacon_miss(void *, int);
1035efea30fSAndrew Thompson static void beacon_swmiss(void *, int);
104b032f27cSSam Leffler static void parent_updown(void *, int);
1055efea30fSAndrew Thompson static void update_mcast(void *, int);
1065efea30fSAndrew Thompson static void update_promisc(void *, int);
1075efea30fSAndrew Thompson static void update_channel(void *, int);
108*b94299c4SAdrian Chadd static void update_chw(void *, int);
1095efea30fSAndrew Thompson static void ieee80211_newstate_cb(void *, int);
110b032f27cSSam Leffler static int ieee80211_new_state_locked(struct ieee80211vap *,
111b032f27cSSam Leffler 	enum ieee80211_state, int);
1121a1e1d21SSam Leffler 
113b032f27cSSam Leffler static int
114b032f27cSSam Leffler null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
115b032f27cSSam Leffler 	const struct ieee80211_bpf_params *params)
116b105a069SSam Leffler {
117b032f27cSSam Leffler 	struct ifnet *ifp = ni->ni_ic->ic_ifp;
118b032f27cSSam Leffler 
119b032f27cSSam Leffler 	if_printf(ifp, "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 {
1278a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1281a1e1d21SSam Leffler 
129b032f27cSSam Leffler 	/* override the 802.3 setting */
130b032f27cSSam Leffler 	ifp->if_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 */
135b032f27cSSam Leffler 	if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
136b032f27cSSam Leffler 		/* XXX sanity check... */
137b032f27cSSam Leffler 		max_linkhdr = ALIGN(ifp->if_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 
143b032f27cSSam Leffler 	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ifp);
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);
148*b94299c4SAdrian Chadd 	TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
1498a1b9b6aSSam Leffler 
1508a1b9b6aSSam Leffler 	ic->ic_wme.wme_hipri_switch_hysteresis =
1518a1b9b6aSSam Leffler 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
1521a1e1d21SSam Leffler 
1531a1e1d21SSam Leffler 	/* initialize management frame handlers */
1541a1e1d21SSam Leffler 	ic->ic_send_mgmt = ieee80211_send_mgmt;
155b032f27cSSam Leffler 	ic->ic_raw_xmit = null_raw_xmit;
156b032f27cSSam Leffler 
157b032f27cSSam Leffler 	ieee80211_adhoc_attach(ic);
158b032f27cSSam Leffler 	ieee80211_sta_attach(ic);
159b032f27cSSam Leffler 	ieee80211_wds_attach(ic);
160b032f27cSSam Leffler 	ieee80211_hostap_attach(ic);
16159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
16259aa14a9SRui Paulo 	ieee80211_mesh_attach(ic);
16359aa14a9SRui Paulo #endif
164b032f27cSSam Leffler 	ieee80211_monitor_attach(ic);
1651a1e1d21SSam Leffler }
1661a1e1d21SSam Leffler 
1671a1e1d21SSam Leffler void
1688a1b9b6aSSam Leffler ieee80211_proto_detach(struct ieee80211com *ic)
1691a1e1d21SSam Leffler {
170b032f27cSSam Leffler 	ieee80211_monitor_detach(ic);
17159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
17259aa14a9SRui Paulo 	ieee80211_mesh_detach(ic);
17359aa14a9SRui Paulo #endif
174b032f27cSSam Leffler 	ieee80211_hostap_detach(ic);
175b032f27cSSam Leffler 	ieee80211_wds_detach(ic);
176b032f27cSSam Leffler 	ieee80211_adhoc_detach(ic);
177b032f27cSSam Leffler 	ieee80211_sta_detach(ic);
178b032f27cSSam Leffler }
1798a1b9b6aSSam Leffler 
180b032f27cSSam Leffler static void
181b032f27cSSam Leffler null_update_beacon(struct ieee80211vap *vap, int item)
182b032f27cSSam Leffler {
183b032f27cSSam Leffler }
184b032f27cSSam Leffler 
185b032f27cSSam Leffler void
186b032f27cSSam Leffler ieee80211_proto_vattach(struct ieee80211vap *vap)
187b032f27cSSam Leffler {
188b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
189b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
190b032f27cSSam Leffler 	int i;
191b032f27cSSam Leffler 
192b032f27cSSam Leffler 	/* override the 802.3 setting */
193b032f27cSSam Leffler 	ifp->if_hdrlen = ic->ic_ifp->if_hdrlen;
194b032f27cSSam Leffler 
195b032f27cSSam Leffler 	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
196b032f27cSSam Leffler 	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
197b032f27cSSam Leffler 	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
19823401900SAdrian Chadd 	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
199b032f27cSSam Leffler 	callout_init(&vap->iv_mgtsend, CALLOUT_MPSAFE);
2005efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
2015efea30fSAndrew Thompson 	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
202b032f27cSSam Leffler 	/*
203b032f27cSSam Leffler 	 * Install default tx rate handling: no fixed rate, lowest
204b032f27cSSam Leffler 	 * supported rate for mgmt and multicast frames.  Default
205b032f27cSSam Leffler 	 * max retry count.  These settings can be changed by the
206b032f27cSSam Leffler 	 * driver and/or user applications.
207b032f27cSSam Leffler 	 */
208047db6b3SSam Leffler 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
209b032f27cSSam Leffler 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
210b032f27cSSam Leffler 
211b032f27cSSam Leffler 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
212338452c9SAdrian Chadd 
213338452c9SAdrian Chadd 		/*
214338452c9SAdrian Chadd 		 * Setting the management rate to MCS 0 assumes that the
215338452c9SAdrian Chadd 		 * BSS Basic rate set is empty and the BSS Basic MCS set
216338452c9SAdrian Chadd 		 * is not.
217338452c9SAdrian Chadd 		 *
218338452c9SAdrian Chadd 		 * Since we're not checking this, default to the lowest
219338452c9SAdrian Chadd 		 * defined rate for this mode.
220338452c9SAdrian Chadd 		 *
221338452c9SAdrian Chadd 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
222338452c9SAdrian Chadd 		 * some MCS management traffic (eg BA response frames.)
223338452c9SAdrian Chadd 		 *
224338452c9SAdrian Chadd 		 * See also: 9.6.0 of the 802.11n-2009 specification.
225338452c9SAdrian Chadd 		 */
226338452c9SAdrian Chadd #ifdef	NOTYET
227047db6b3SSam Leffler 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
228047db6b3SSam Leffler 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
229047db6b3SSam Leffler 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
230047db6b3SSam Leffler 		} else {
231b032f27cSSam Leffler 			vap->iv_txparms[i].mgmtrate =
232b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
233b032f27cSSam Leffler 			vap->iv_txparms[i].mcastrate =
234b032f27cSSam Leffler 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
235b032f27cSSam Leffler 		}
236338452c9SAdrian Chadd #endif
237338452c9SAdrian Chadd 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
238338452c9SAdrian Chadd 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
239b032f27cSSam Leffler 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
240b032f27cSSam Leffler 	}
241b032f27cSSam Leffler 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
242b032f27cSSam Leffler 
243b032f27cSSam Leffler 	vap->iv_update_beacon = null_update_beacon;
244b032f27cSSam Leffler 	vap->iv_deliver_data = ieee80211_deliver_data;
245b032f27cSSam Leffler 
246b032f27cSSam Leffler 	/* attach support for operating mode */
247b032f27cSSam Leffler 	ic->ic_vattach[vap->iv_opmode](vap);
248b032f27cSSam Leffler }
249b032f27cSSam Leffler 
250b032f27cSSam Leffler void
251b032f27cSSam Leffler ieee80211_proto_vdetach(struct ieee80211vap *vap)
252b032f27cSSam Leffler {
253b032f27cSSam Leffler #define	FREEAPPIE(ie) do { \
254b032f27cSSam Leffler 	if (ie != NULL) \
255e2126decSSam Leffler 		free(ie, M_80211_NODE_IE); \
256b032f27cSSam Leffler } while (0)
257b032f27cSSam Leffler 	/*
258b032f27cSSam Leffler 	 * Detach operating mode module.
259b032f27cSSam Leffler 	 */
260b032f27cSSam Leffler 	if (vap->iv_opdetach != NULL)
261b032f27cSSam Leffler 		vap->iv_opdetach(vap);
2628a1b9b6aSSam Leffler 	/*
2638a1b9b6aSSam Leffler 	 * This should not be needed as we detach when reseting
2648a1b9b6aSSam Leffler 	 * the state but be conservative here since the
2658a1b9b6aSSam Leffler 	 * authenticator may do things like spawn kernel threads.
2668a1b9b6aSSam Leffler 	 */
267b032f27cSSam Leffler 	if (vap->iv_auth->ia_detach != NULL)
268b032f27cSSam Leffler 		vap->iv_auth->ia_detach(vap);
2698a1b9b6aSSam Leffler 	/*
2708a1b9b6aSSam Leffler 	 * Detach any ACL'ator.
2718a1b9b6aSSam Leffler 	 */
272b032f27cSSam Leffler 	if (vap->iv_acl != NULL)
273b032f27cSSam Leffler 		vap->iv_acl->iac_detach(vap);
274b032f27cSSam Leffler 
275b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_beacon);
276b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_probereq);
277b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_proberesp);
278b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocreq);
279b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_assocresp);
280b032f27cSSam Leffler 	FREEAPPIE(vap->iv_appie_wpa);
281b032f27cSSam Leffler #undef FREEAPPIE
2828a1b9b6aSSam Leffler }
2838a1b9b6aSSam Leffler 
2848a1b9b6aSSam Leffler /*
2858a1b9b6aSSam Leffler  * Simple-minded authenticator module support.
2868a1b9b6aSSam Leffler  */
2878a1b9b6aSSam Leffler 
2888a1b9b6aSSam Leffler #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
2898a1b9b6aSSam Leffler /* XXX well-known names */
2908a1b9b6aSSam Leffler static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
2918a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
2928a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
2938a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
2948a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
2958a1b9b6aSSam Leffler 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
2968a1b9b6aSSam Leffler 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
2978a1b9b6aSSam Leffler };
2988a1b9b6aSSam Leffler static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
2998a1b9b6aSSam Leffler 
3008a1b9b6aSSam Leffler static const struct ieee80211_authenticator auth_internal = {
3018a1b9b6aSSam Leffler 	.ia_name		= "wlan_internal",
3028a1b9b6aSSam Leffler 	.ia_attach		= NULL,
3038a1b9b6aSSam Leffler 	.ia_detach		= NULL,
3048a1b9b6aSSam Leffler 	.ia_node_join		= NULL,
3058a1b9b6aSSam Leffler 	.ia_node_leave		= NULL,
3068a1b9b6aSSam Leffler };
3078a1b9b6aSSam Leffler 
3088a1b9b6aSSam Leffler /*
3098a1b9b6aSSam Leffler  * Setup internal authenticators once; they are never unregistered.
3108a1b9b6aSSam Leffler  */
3118a1b9b6aSSam Leffler static void
3128a1b9b6aSSam Leffler ieee80211_auth_setup(void)
3138a1b9b6aSSam Leffler {
3148a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
3158a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
3168a1b9b6aSSam Leffler 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
3178a1b9b6aSSam Leffler }
3188a1b9b6aSSam Leffler SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
3198a1b9b6aSSam Leffler 
3208a1b9b6aSSam Leffler const struct ieee80211_authenticator *
3218a1b9b6aSSam Leffler ieee80211_authenticator_get(int auth)
3228a1b9b6aSSam Leffler {
3238a1b9b6aSSam Leffler 	if (auth >= IEEE80211_AUTH_MAX)
3248a1b9b6aSSam Leffler 		return NULL;
3258a1b9b6aSSam Leffler 	if (authenticators[auth] == NULL)
3268a1b9b6aSSam Leffler 		ieee80211_load_module(auth_modnames[auth]);
3278a1b9b6aSSam Leffler 	return authenticators[auth];
3281a1e1d21SSam Leffler }
3291a1e1d21SSam Leffler 
3301a1e1d21SSam Leffler void
3318a1b9b6aSSam Leffler ieee80211_authenticator_register(int type,
3328a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth)
3331a1e1d21SSam Leffler {
3348a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
3358a1b9b6aSSam Leffler 		return;
3368a1b9b6aSSam Leffler 	authenticators[type] = auth;
3378a1b9b6aSSam Leffler }
3388a1b9b6aSSam Leffler 
3398a1b9b6aSSam Leffler void
3408a1b9b6aSSam Leffler ieee80211_authenticator_unregister(int type)
3418a1b9b6aSSam Leffler {
3428a1b9b6aSSam Leffler 
3438a1b9b6aSSam Leffler 	if (type >= IEEE80211_AUTH_MAX)
3448a1b9b6aSSam Leffler 		return;
3458a1b9b6aSSam Leffler 	authenticators[type] = NULL;
3468a1b9b6aSSam Leffler }
3478a1b9b6aSSam Leffler 
3488a1b9b6aSSam Leffler /*
3498a1b9b6aSSam Leffler  * Very simple-minded ACL module support.
3508a1b9b6aSSam Leffler  */
3518a1b9b6aSSam Leffler /* XXX just one for now */
3528a1b9b6aSSam Leffler static	const struct ieee80211_aclator *acl = NULL;
3538a1b9b6aSSam Leffler 
3548a1b9b6aSSam Leffler void
3558a1b9b6aSSam Leffler ieee80211_aclator_register(const struct ieee80211_aclator *iac)
3568a1b9b6aSSam Leffler {
3578a1b9b6aSSam Leffler 	printf("wlan: %s acl policy registered\n", iac->iac_name);
3588a1b9b6aSSam Leffler 	acl = iac;
3598a1b9b6aSSam Leffler }
3608a1b9b6aSSam Leffler 
3618a1b9b6aSSam Leffler void
3628a1b9b6aSSam Leffler ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
3638a1b9b6aSSam Leffler {
3648a1b9b6aSSam Leffler 	if (acl == iac)
3658a1b9b6aSSam Leffler 		acl = NULL;
3668a1b9b6aSSam Leffler 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
3678a1b9b6aSSam Leffler }
3688a1b9b6aSSam Leffler 
3698a1b9b6aSSam Leffler const struct ieee80211_aclator *
3708a1b9b6aSSam Leffler ieee80211_aclator_get(const char *name)
3718a1b9b6aSSam Leffler {
3728a1b9b6aSSam Leffler 	if (acl == NULL)
3738a1b9b6aSSam Leffler 		ieee80211_load_module("wlan_acl");
3748a1b9b6aSSam Leffler 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
3758a1b9b6aSSam Leffler }
3768a1b9b6aSSam Leffler 
3778a1b9b6aSSam Leffler void
37868e8e04eSSam Leffler ieee80211_print_essid(const uint8_t *essid, int len)
3798a1b9b6aSSam Leffler {
38068e8e04eSSam Leffler 	const uint8_t *p;
3811a1e1d21SSam Leffler 	int i;
3821a1e1d21SSam Leffler 
3831a1e1d21SSam Leffler 	if (len > IEEE80211_NWID_LEN)
3841a1e1d21SSam Leffler 		len = IEEE80211_NWID_LEN;
3851a1e1d21SSam Leffler 	/* determine printable or not */
3861a1e1d21SSam Leffler 	for (i = 0, p = essid; i < len; i++, p++) {
3871a1e1d21SSam Leffler 		if (*p < ' ' || *p > 0x7e)
3881a1e1d21SSam Leffler 			break;
3891a1e1d21SSam Leffler 	}
3901a1e1d21SSam Leffler 	if (i == len) {
3911a1e1d21SSam Leffler 		printf("\"");
3921a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
3931a1e1d21SSam Leffler 			printf("%c", *p);
3941a1e1d21SSam Leffler 		printf("\"");
3951a1e1d21SSam Leffler 	} else {
3961a1e1d21SSam Leffler 		printf("0x");
3971a1e1d21SSam Leffler 		for (i = 0, p = essid; i < len; i++, p++)
3981a1e1d21SSam Leffler 			printf("%02x", *p);
3991a1e1d21SSam Leffler 	}
4001a1e1d21SSam Leffler }
4011a1e1d21SSam Leffler 
4021a1e1d21SSam Leffler void
40368e8e04eSSam Leffler ieee80211_dump_pkt(struct ieee80211com *ic,
40468e8e04eSSam Leffler 	const uint8_t *buf, int len, int rate, int rssi)
4051a1e1d21SSam Leffler {
4068a1b9b6aSSam Leffler 	const struct ieee80211_frame *wh;
4071a1e1d21SSam Leffler 	int i;
4081a1e1d21SSam Leffler 
4098a1b9b6aSSam Leffler 	wh = (const struct ieee80211_frame *)buf;
4101a1e1d21SSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
4111a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_NODS:
4121a1e1d21SSam Leffler 		printf("NODS %s", ether_sprintf(wh->i_addr2));
4131a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
4141a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr3));
4151a1e1d21SSam Leffler 		break;
4161a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_TODS:
4171a1e1d21SSam Leffler 		printf("TODS %s", ether_sprintf(wh->i_addr2));
4181a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
4191a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr1));
4201a1e1d21SSam Leffler 		break;
4211a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
4221a1e1d21SSam Leffler 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
4231a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr1));
4241a1e1d21SSam Leffler 		printf("(%s)", ether_sprintf(wh->i_addr2));
4251a1e1d21SSam Leffler 		break;
4261a1e1d21SSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
42768e8e04eSSam Leffler 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
4281a1e1d21SSam Leffler 		printf("->%s", ether_sprintf(wh->i_addr3));
4291a1e1d21SSam Leffler 		printf("(%s", ether_sprintf(wh->i_addr2));
4301a1e1d21SSam Leffler 		printf("->%s)", ether_sprintf(wh->i_addr1));
4311a1e1d21SSam Leffler 		break;
4321a1e1d21SSam Leffler 	}
4331a1e1d21SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
4341a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
4351a1e1d21SSam Leffler 		printf(" data");
4361a1e1d21SSam Leffler 		break;
4371a1e1d21SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
4381a1e1d21SSam Leffler 		printf(" %s", ieee80211_mgt_subtype_name[
4391a1e1d21SSam Leffler 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
4401a1e1d21SSam Leffler 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
4411a1e1d21SSam Leffler 		break;
4421a1e1d21SSam Leffler 	default:
4431a1e1d21SSam Leffler 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
4441a1e1d21SSam Leffler 		break;
4451a1e1d21SSam Leffler 	}
44668e8e04eSSam Leffler 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
44768e8e04eSSam Leffler 		const struct ieee80211_qosframe *qwh =
44868e8e04eSSam Leffler 			(const struct ieee80211_qosframe *)buf;
44968e8e04eSSam Leffler 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
45068e8e04eSSam Leffler 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
45168e8e04eSSam Leffler 	}
4528a1b9b6aSSam Leffler 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
45368e8e04eSSam Leffler 		int off;
45468e8e04eSSam Leffler 
45568e8e04eSSam Leffler 		off = ieee80211_anyhdrspace(ic, wh);
45668e8e04eSSam Leffler 		printf(" WEP [IV %.02x %.02x %.02x",
45768e8e04eSSam Leffler 			buf[off+0], buf[off+1], buf[off+2]);
45868e8e04eSSam Leffler 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
45968e8e04eSSam Leffler 			printf(" %.02x %.02x %.02x",
46068e8e04eSSam Leffler 				buf[off+4], buf[off+5], buf[off+6]);
46168e8e04eSSam Leffler 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
4628a1b9b6aSSam Leffler 	}
4631a1e1d21SSam Leffler 	if (rate >= 0)
4641a1e1d21SSam Leffler 		printf(" %dM", rate / 2);
4651a1e1d21SSam Leffler 	if (rssi >= 0)
4661a1e1d21SSam Leffler 		printf(" +%d", rssi);
4671a1e1d21SSam Leffler 	printf("\n");
4681a1e1d21SSam Leffler 	if (len > 0) {
4691a1e1d21SSam Leffler 		for (i = 0; i < len; i++) {
4701a1e1d21SSam Leffler 			if ((i & 1) == 0)
4711a1e1d21SSam Leffler 				printf(" ");
4721a1e1d21SSam Leffler 			printf("%02x", buf[i]);
4731a1e1d21SSam Leffler 		}
4741a1e1d21SSam Leffler 		printf("\n");
4751a1e1d21SSam Leffler 	}
4761a1e1d21SSam Leffler }
4771a1e1d21SSam Leffler 
47879edaebfSSam Leffler static __inline int
47979edaebfSSam Leffler findrix(const struct ieee80211_rateset *rs, int r)
48079edaebfSSam Leffler {
48179edaebfSSam Leffler 	int i;
48279edaebfSSam Leffler 
48379edaebfSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++)
48479edaebfSSam Leffler 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
48579edaebfSSam Leffler 			return i;
48679edaebfSSam Leffler 	return -1;
48779edaebfSSam Leffler }
48879edaebfSSam Leffler 
4891a1e1d21SSam Leffler int
49070e28b9aSSam Leffler ieee80211_fix_rate(struct ieee80211_node *ni,
49170e28b9aSSam Leffler 	struct ieee80211_rateset *nrs, int flags)
4921a1e1d21SSam Leffler {
4931a1e1d21SSam Leffler #define	RV(v)	((v) & IEEE80211_RATE_VAL)
494b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
4957d77cd53SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
49679edaebfSSam Leffler 	int i, j, rix, error;
497b032f27cSSam Leffler 	int okrate, badrate, fixedrate, ucastrate;
49841b3c790SSam Leffler 	const struct ieee80211_rateset *srs;
49968e8e04eSSam Leffler 	uint8_t r;
5001a1e1d21SSam Leffler 
5011a1e1d21SSam Leffler 	error = 0;
50268e8e04eSSam Leffler 	okrate = badrate = 0;
503b032f27cSSam Leffler 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
504b032f27cSSam Leffler 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
505b032f27cSSam Leffler 		/*
506b032f27cSSam Leffler 		 * Workaround awkwardness with fixed rate.  We are called
507b032f27cSSam Leffler 		 * to check both the legacy rate set and the HT rate set
508b032f27cSSam Leffler 		 * but we must apply any legacy fixed rate check only to the
509b032f27cSSam Leffler 		 * legacy rate set and vice versa.  We cannot tell what type
510b032f27cSSam Leffler 		 * of rate set we've been given (legacy or HT) but we can
511b032f27cSSam Leffler 		 * distinguish the fixed rate type (MCS have 0x80 set).
512b032f27cSSam Leffler 		 * So to deal with this the caller communicates whether to
513b032f27cSSam Leffler 		 * check MCS or legacy rate using the flags and we use the
514b032f27cSSam Leffler 		 * type of any fixed rate to avoid applying an MCS to a
515b032f27cSSam Leffler 		 * legacy rate and vice versa.
516b032f27cSSam Leffler 		 */
517b032f27cSSam Leffler 		if (ucastrate & 0x80) {
518b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFRATE)
519b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFRATE;
520b032f27cSSam Leffler 		} else if ((ucastrate & 0x80) == 0) {
521b032f27cSSam Leffler 			if (flags & IEEE80211_F_DOFMCS)
522b032f27cSSam Leffler 				flags &= ~IEEE80211_F_DOFMCS;
523b032f27cSSam Leffler 		}
524b032f27cSSam Leffler 		/* NB: required to make MCS match below work */
525b032f27cSSam Leffler 		ucastrate &= IEEE80211_RATE_VAL;
526b032f27cSSam Leffler 	}
52768e8e04eSSam Leffler 	fixedrate = IEEE80211_FIXED_RATE_NONE;
528b032f27cSSam Leffler 	/*
529b032f27cSSam Leffler 	 * XXX we are called to process both MCS and legacy rates;
530b032f27cSSam Leffler 	 * we must use the appropriate basic rate set or chaos will
531b032f27cSSam Leffler 	 * ensue; for now callers that want MCS must supply
532b032f27cSSam Leffler 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
533b032f27cSSam Leffler 	 * function so there are two variants, one for MCS and one
534b032f27cSSam Leffler 	 * for legacy rates.
535b032f27cSSam Leffler 	 */
536b032f27cSSam Leffler 	if (flags & IEEE80211_F_DOBRS)
537b032f27cSSam Leffler 		srs = (const struct ieee80211_rateset *)
538b032f27cSSam Leffler 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
539b032f27cSSam Leffler 	else
54041b3c790SSam Leffler 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
541ef39d4beSSam Leffler 	for (i = 0; i < nrs->rs_nrates; ) {
5421a1e1d21SSam Leffler 		if (flags & IEEE80211_F_DOSORT) {
5431a1e1d21SSam Leffler 			/*
5441a1e1d21SSam Leffler 			 * Sort rates.
5451a1e1d21SSam Leffler 			 */
5461a1e1d21SSam Leffler 			for (j = i + 1; j < nrs->rs_nrates; j++) {
5471a1e1d21SSam Leffler 				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
5481a1e1d21SSam Leffler 					r = nrs->rs_rates[i];
5491a1e1d21SSam Leffler 					nrs->rs_rates[i] = nrs->rs_rates[j];
5501a1e1d21SSam Leffler 					nrs->rs_rates[j] = r;
5511a1e1d21SSam Leffler 				}
5521a1e1d21SSam Leffler 			}
5531a1e1d21SSam Leffler 		}
5541a1e1d21SSam Leffler 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
5551a1e1d21SSam Leffler 		badrate = r;
5561a1e1d21SSam Leffler 		/*
55768e8e04eSSam Leffler 		 * Check for fixed rate.
5581a1e1d21SSam Leffler 		 */
559b032f27cSSam Leffler 		if (r == ucastrate)
5608a1b9b6aSSam Leffler 			fixedrate = r;
5611a1e1d21SSam Leffler 		/*
5621a1e1d21SSam Leffler 		 * Check against supported rates.
5631a1e1d21SSam Leffler 		 */
56479edaebfSSam Leffler 		rix = findrix(srs, r);
56579edaebfSSam Leffler 		if (flags & IEEE80211_F_DONEGO) {
56679edaebfSSam Leffler 			if (rix < 0) {
567ef39d4beSSam Leffler 				/*
568ef39d4beSSam Leffler 				 * A rate in the node's rate set is not
569ef39d4beSSam Leffler 				 * supported.  If this is a basic rate and we
57079edaebfSSam Leffler 				 * are operating as a STA then this is an error.
571ef39d4beSSam Leffler 				 * Otherwise we just discard/ignore the rate.
572ef39d4beSSam Leffler 				 */
57379edaebfSSam Leffler 				if ((flags & IEEE80211_F_JOIN) &&
574ef39d4beSSam Leffler 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
5751a1e1d21SSam Leffler 					error++;
57679edaebfSSam Leffler 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
57779edaebfSSam Leffler 				/*
57879edaebfSSam Leffler 				 * Overwrite with the supported rate
57979edaebfSSam Leffler 				 * value so any basic rate bit is set.
58079edaebfSSam Leffler 				 */
58179edaebfSSam Leffler 				nrs->rs_rates[i] = srs->rs_rates[rix];
5821a1e1d21SSam Leffler 			}
5831a1e1d21SSam Leffler 		}
58479edaebfSSam Leffler 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
5851a1e1d21SSam Leffler 			/*
5861a1e1d21SSam Leffler 			 * Delete unacceptable rates.
5871a1e1d21SSam Leffler 			 */
5881a1e1d21SSam Leffler 			nrs->rs_nrates--;
5891a1e1d21SSam Leffler 			for (j = i; j < nrs->rs_nrates; j++)
5901a1e1d21SSam Leffler 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
5911a1e1d21SSam Leffler 			nrs->rs_rates[j] = 0;
5921a1e1d21SSam Leffler 			continue;
5931a1e1d21SSam Leffler 		}
59479edaebfSSam Leffler 		if (rix >= 0)
5951a1e1d21SSam Leffler 			okrate = nrs->rs_rates[i];
5961a1e1d21SSam Leffler 		i++;
5971a1e1d21SSam Leffler 	}
5988a1b9b6aSSam Leffler 	if (okrate == 0 || error != 0 ||
599b032f27cSSam Leffler 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
600b032f27cSSam Leffler 	     fixedrate != ucastrate)) {
601b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
602b032f27cSSam Leffler 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
603b032f27cSSam Leffler 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
6041a1e1d21SSam Leffler 		return badrate | IEEE80211_RATE_BASIC;
605b032f27cSSam Leffler 	} else
6061a1e1d21SSam Leffler 		return RV(okrate);
6071a1e1d21SSam Leffler #undef RV
6081a1e1d21SSam Leffler }
6091a1e1d21SSam Leffler 
6108a1b9b6aSSam Leffler /*
6118a1b9b6aSSam Leffler  * Reset 11g-related state.
6128a1b9b6aSSam Leffler  */
6138a1b9b6aSSam Leffler void
6148a1b9b6aSSam Leffler ieee80211_reset_erp(struct ieee80211com *ic)
6151a1e1d21SSam Leffler {
6168a1b9b6aSSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
6178a1b9b6aSSam Leffler 	ic->ic_nonerpsta = 0;
6188a1b9b6aSSam Leffler 	ic->ic_longslotsta = 0;
6198a1b9b6aSSam Leffler 	/*
6208a1b9b6aSSam Leffler 	 * Short slot time is enabled only when operating in 11g
6218a1b9b6aSSam Leffler 	 * and not in an IBSS.  We must also honor whether or not
6228a1b9b6aSSam Leffler 	 * the driver is capable of doing it.
6238a1b9b6aSSam Leffler 	 */
6248a1b9b6aSSam Leffler 	ieee80211_set_shortslottime(ic,
62568e8e04eSSam Leffler 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
62668e8e04eSSam Leffler 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
62768e8e04eSSam Leffler 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
6288a1b9b6aSSam Leffler 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
6298a1b9b6aSSam Leffler 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
6308a1b9b6aSSam Leffler 	/*
6318a1b9b6aSSam Leffler 	 * Set short preamble and ERP barker-preamble flags.
6328a1b9b6aSSam Leffler 	 */
63368e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
6348a1b9b6aSSam Leffler 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
6358a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
6368a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
6378a1b9b6aSSam Leffler 	} else {
6388a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
6398a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_USEBARKER;
6408a1b9b6aSSam Leffler 	}
6418a1b9b6aSSam Leffler }
6428a1b9b6aSSam Leffler 
6438a1b9b6aSSam Leffler /*
6448a1b9b6aSSam Leffler  * Set the short slot time state and notify the driver.
6458a1b9b6aSSam Leffler  */
6468a1b9b6aSSam Leffler void
6478a1b9b6aSSam Leffler ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
6488a1b9b6aSSam Leffler {
6498a1b9b6aSSam Leffler 	if (onoff)
6508a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SHSLOT;
6518a1b9b6aSSam Leffler 	else
6528a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
6538a1b9b6aSSam Leffler 	/* notify driver */
6548a1b9b6aSSam Leffler 	if (ic->ic_updateslot != NULL)
6558a1b9b6aSSam Leffler 		ic->ic_updateslot(ic->ic_ifp);
6568a1b9b6aSSam Leffler }
6578a1b9b6aSSam Leffler 
6588a1b9b6aSSam Leffler /*
6598a1b9b6aSSam Leffler  * Check if the specified rate set supports ERP.
6608a1b9b6aSSam Leffler  * NB: the rate set is assumed to be sorted.
6618a1b9b6aSSam Leffler  */
6628a1b9b6aSSam Leffler int
663b032f27cSSam Leffler ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
6648a1b9b6aSSam Leffler {
6658a1b9b6aSSam Leffler #define N(a)	(sizeof(a) / sizeof(a[0]))
6668a1b9b6aSSam Leffler 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
6678a1b9b6aSSam Leffler 	int i, j;
6688a1b9b6aSSam Leffler 
6698a1b9b6aSSam Leffler 	if (rs->rs_nrates < N(rates))
6708a1b9b6aSSam Leffler 		return 0;
6718a1b9b6aSSam Leffler 	for (i = 0; i < N(rates); i++) {
6728a1b9b6aSSam Leffler 		for (j = 0; j < rs->rs_nrates; j++) {
6738a1b9b6aSSam Leffler 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
6748a1b9b6aSSam Leffler 			if (rates[i] == r)
6758a1b9b6aSSam Leffler 				goto next;
6768a1b9b6aSSam Leffler 			if (r > rates[i])
6778a1b9b6aSSam Leffler 				return 0;
6788a1b9b6aSSam Leffler 		}
6798a1b9b6aSSam Leffler 		return 0;
6808a1b9b6aSSam Leffler 	next:
6818a1b9b6aSSam Leffler 		;
6828a1b9b6aSSam Leffler 	}
6838a1b9b6aSSam Leffler 	return 1;
6848a1b9b6aSSam Leffler #undef N
6858a1b9b6aSSam Leffler }
6868a1b9b6aSSam Leffler 
6878a1b9b6aSSam Leffler /*
688b032f27cSSam Leffler  * Mark the basic rates for the rate table based on the
6898a1b9b6aSSam Leffler  * operating mode.  For real 11g we mark all the 11b rates
6908a1b9b6aSSam Leffler  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
6918a1b9b6aSSam Leffler  * 11b rates.  There's also a pseudo 11a-mode used to mark only
6928a1b9b6aSSam Leffler  * the basic OFDM rates.
6938a1b9b6aSSam Leffler  */
694b032f27cSSam Leffler static void
695b032f27cSSam Leffler setbasicrates(struct ieee80211_rateset *rs,
696b032f27cSSam Leffler     enum ieee80211_phymode mode, int add)
6978a1b9b6aSSam Leffler {
69868e8e04eSSam Leffler 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
699be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
700be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
701be0df3e7SSam Leffler 					    /* NB: mixed b/g */
702be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
703be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
704be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
705be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
7066a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
7076a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
708be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
709be0df3e7SSam Leffler 					    /* NB: mixed b/g */
710be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
7118a1b9b6aSSam Leffler 	};
7128a1b9b6aSSam Leffler 	int i, j;
7138a1b9b6aSSam Leffler 
7148a1b9b6aSSam Leffler 	for (i = 0; i < rs->rs_nrates; i++) {
715b032f27cSSam Leffler 		if (!add)
7168a1b9b6aSSam Leffler 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
7178a1b9b6aSSam Leffler 		for (j = 0; j < basic[mode].rs_nrates; j++)
7188a1b9b6aSSam Leffler 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
7198a1b9b6aSSam Leffler 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
7208a1b9b6aSSam Leffler 				break;
7218a1b9b6aSSam Leffler 			}
7228a1b9b6aSSam Leffler 	}
7238a1b9b6aSSam Leffler }
7248a1b9b6aSSam Leffler 
7258a1b9b6aSSam Leffler /*
726b032f27cSSam Leffler  * Set the basic rates in a rate set.
727b032f27cSSam Leffler  */
728b032f27cSSam Leffler void
729b032f27cSSam Leffler ieee80211_setbasicrates(struct ieee80211_rateset *rs,
730b032f27cSSam Leffler     enum ieee80211_phymode mode)
731b032f27cSSam Leffler {
732b032f27cSSam Leffler 	setbasicrates(rs, mode, 0);
733b032f27cSSam Leffler }
734b032f27cSSam Leffler 
735b032f27cSSam Leffler /*
736b032f27cSSam Leffler  * Add basic rates to a rate set.
737b032f27cSSam Leffler  */
738b032f27cSSam Leffler void
739b032f27cSSam Leffler ieee80211_addbasicrates(struct ieee80211_rateset *rs,
740b032f27cSSam Leffler     enum ieee80211_phymode mode)
741b032f27cSSam Leffler {
742b032f27cSSam Leffler 	setbasicrates(rs, mode, 1);
743b032f27cSSam Leffler }
744b032f27cSSam Leffler 
745b032f27cSSam Leffler /*
746b032f27cSSam Leffler  * WME protocol support.
747b032f27cSSam Leffler  *
748b032f27cSSam Leffler  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
749b032f27cSSam Leffler  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
750b032f27cSSam Leffler  * Draft 2.0 Test Plan (Appendix D).
751b032f27cSSam Leffler  *
752b032f27cSSam Leffler  * Static/Dynamic Turbo mode settings come from Atheros.
7538a1b9b6aSSam Leffler  */
7548a1b9b6aSSam Leffler typedef struct phyParamType {
75568e8e04eSSam Leffler 	uint8_t		aifsn;
75668e8e04eSSam Leffler 	uint8_t		logcwmin;
75768e8e04eSSam Leffler 	uint8_t		logcwmax;
75868e8e04eSSam Leffler 	uint16_t	txopLimit;
75968e8e04eSSam Leffler 	uint8_t 	acm;
7608a1b9b6aSSam Leffler } paramType;
7618a1b9b6aSSam Leffler 
7628a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
763be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
764be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
765be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
766be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
767be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
768be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
769be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
770be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
7716a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
7726a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
773be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
774be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
7758a1b9b6aSSam Leffler };
7768a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
777be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
778be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
779be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
780be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
781be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
782be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
783be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
784be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
7856a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
7866a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
787be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
788be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
7898a1b9b6aSSam Leffler };
7908a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
791be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
792be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
793be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
794be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
795be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
796be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
797be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
798be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
7996a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
8006a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
801be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
802be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
8038a1b9b6aSSam Leffler };
8048a1b9b6aSSam Leffler static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
805be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
806be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
807be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
808be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
809be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
810be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
811be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
812be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
8136a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
8146a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
815be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
816be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
8178a1b9b6aSSam Leffler };
8188a1b9b6aSSam Leffler 
8198a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
820be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
821be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
822be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
823be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
824be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
825be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
826be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
827be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
8286a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
8296a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
830be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
831be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
8328a1b9b6aSSam Leffler };
8338a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
834be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
835be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
836be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
837be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
838be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
839be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
840be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
841be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
8426a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
8436a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
844be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
845be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
8468a1b9b6aSSam Leffler };
8478a1b9b6aSSam Leffler static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
848be0df3e7SSam Leffler 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
849be0df3e7SSam Leffler 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
850be0df3e7SSam Leffler 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
851be0df3e7SSam Leffler 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
852be0df3e7SSam Leffler 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
853be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
854be0df3e7SSam Leffler 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
855be0df3e7SSam Leffler 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
8566a76ae21SSam Leffler 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
8576a76ae21SSam Leffler 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
858be0df3e7SSam Leffler 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
859be0df3e7SSam Leffler 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
8608a1b9b6aSSam Leffler };
8618a1b9b6aSSam Leffler 
862b032f27cSSam Leffler static void
86367ce310aSSam Leffler _setifsparams(struct wmeParams *wmep, const paramType *phy)
86467ce310aSSam Leffler {
86567ce310aSSam Leffler 	wmep->wmep_aifsn = phy->aifsn;
86667ce310aSSam Leffler 	wmep->wmep_logcwmin = phy->logcwmin;
86767ce310aSSam Leffler 	wmep->wmep_logcwmax = phy->logcwmax;
86867ce310aSSam Leffler 	wmep->wmep_txopLimit = phy->txopLimit;
86967ce310aSSam Leffler }
87067ce310aSSam Leffler 
87167ce310aSSam Leffler static void
87267ce310aSSam Leffler setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
87367ce310aSSam Leffler 	struct wmeParams *wmep, const paramType *phy)
87467ce310aSSam Leffler {
87567ce310aSSam Leffler 	wmep->wmep_acm = phy->acm;
87667ce310aSSam Leffler 	_setifsparams(wmep, phy);
87767ce310aSSam Leffler 
87867ce310aSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
87967ce310aSSam Leffler 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
88067ce310aSSam Leffler 	    ieee80211_wme_acnames[ac], type,
88167ce310aSSam Leffler 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
88267ce310aSSam Leffler 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
88367ce310aSSam Leffler }
88467ce310aSSam Leffler 
88567ce310aSSam Leffler static void
886b032f27cSSam Leffler ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
8878a1b9b6aSSam Leffler {
888b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
8898a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
8908a1b9b6aSSam Leffler 	const paramType *pPhyParam, *pBssPhyParam;
8918a1b9b6aSSam Leffler 	struct wmeParams *wmep;
89268e8e04eSSam Leffler 	enum ieee80211_phymode mode;
8938a1b9b6aSSam Leffler 	int i;
8948a1b9b6aSSam Leffler 
895b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
896b032f27cSSam Leffler 
897a4b3c7a5SSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
8988a1b9b6aSSam Leffler 		return;
8998a1b9b6aSSam Leffler 
90068e8e04eSSam Leffler 	/*
9010d4e4e5eSAdrian Chadd 	 * Clear the wme cap_info field so a qoscount from a previous
9020d4e4e5eSAdrian Chadd 	 * vap doesn't confuse later code which only parses the beacon
9030d4e4e5eSAdrian Chadd 	 * field and updates hardware when said field changes.
9040d4e4e5eSAdrian Chadd 	 * Otherwise the hardware is programmed with defaults, not what
9050d4e4e5eSAdrian Chadd 	 * the beacon actually announces.
9060d4e4e5eSAdrian Chadd 	 */
9070d4e4e5eSAdrian Chadd 	wme->wme_wmeChanParams.cap_info = 0;
9080d4e4e5eSAdrian Chadd 
9090d4e4e5eSAdrian Chadd 	/*
91068e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
91168e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
91268e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
91368e8e04eSSam Leffler 	 * so state will eventually get set correctly
91468e8e04eSSam Leffler 	 */
91568e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
91668e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
91768e8e04eSSam Leffler 	else
91868e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
9198a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
9208a1b9b6aSSam Leffler 		switch (i) {
9218a1b9b6aSSam Leffler 		case WME_AC_BK:
92268e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BK[mode];
92368e8e04eSSam Leffler 			pBssPhyParam = &phyParamForAC_BK[mode];
9248a1b9b6aSSam Leffler 			break;
9258a1b9b6aSSam Leffler 		case WME_AC_VI:
92668e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VI[mode];
92768e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
9288a1b9b6aSSam Leffler 			break;
9298a1b9b6aSSam Leffler 		case WME_AC_VO:
93068e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_VO[mode];
93168e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
9328a1b9b6aSSam Leffler 			break;
9338a1b9b6aSSam Leffler 		case WME_AC_BE:
9348a1b9b6aSSam Leffler 		default:
93568e8e04eSSam Leffler 			pPhyParam = &phyParamForAC_BE[mode];
93668e8e04eSSam Leffler 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
9378a1b9b6aSSam Leffler 			break;
9388a1b9b6aSSam Leffler 		}
9398a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
9408a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
94167ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
9428a1b9b6aSSam Leffler 		} else {
94367ce310aSSam Leffler 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
9448a1b9b6aSSam Leffler 		}
9458a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
94667ce310aSSam Leffler 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
9478a1b9b6aSSam Leffler 	}
9488a1b9b6aSSam Leffler 	/* NB: check ic_bss to avoid NULL deref on initial attach */
949b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
9508a1b9b6aSSam Leffler 		/*
9518a1b9b6aSSam Leffler 		 * Calculate agressive mode switching threshold based
9528a1b9b6aSSam Leffler 		 * on beacon interval.  This doesn't need locking since
9538a1b9b6aSSam Leffler 		 * we're only called before entering the RUN state at
9548a1b9b6aSSam Leffler 		 * which point we start sending beacon frames.
9558a1b9b6aSSam Leffler 		 */
9568a1b9b6aSSam Leffler 		wme->wme_hipri_switch_thresh =
957b032f27cSSam Leffler 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
958a4b3c7a5SSam Leffler 		wme->wme_flags &= ~WME_F_AGGRMODE;
959b032f27cSSam Leffler 		ieee80211_wme_updateparams(vap);
9608a1b9b6aSSam Leffler 	}
9618a1b9b6aSSam Leffler }
9628a1b9b6aSSam Leffler 
963b032f27cSSam Leffler void
964b032f27cSSam Leffler ieee80211_wme_initparams(struct ieee80211vap *vap)
965b032f27cSSam Leffler {
966b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
967b032f27cSSam Leffler 
968b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
969b032f27cSSam Leffler 	ieee80211_wme_initparams_locked(vap);
970b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
971b032f27cSSam Leffler }
972b032f27cSSam Leffler 
9738a1b9b6aSSam Leffler /*
9748a1b9b6aSSam Leffler  * Update WME parameters for ourself and the BSS.
9758a1b9b6aSSam Leffler  */
9768a1b9b6aSSam Leffler void
977b032f27cSSam Leffler ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
9788a1b9b6aSSam Leffler {
97967ce310aSSam Leffler 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
980be0df3e7SSam Leffler 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
981be0df3e7SSam Leffler 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
982be0df3e7SSam Leffler 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
983be0df3e7SSam Leffler 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
984be0df3e7SSam Leffler 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
985be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
986be0df3e7SSam Leffler 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
987be0df3e7SSam Leffler 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
9886a76ae21SSam Leffler 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
9896a76ae21SSam Leffler 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
990be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
991be0df3e7SSam Leffler 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
9928a1b9b6aSSam Leffler 	};
993b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
9948a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
9958a1b9b6aSSam Leffler 	const struct wmeParams *wmep;
9968a1b9b6aSSam Leffler 	struct wmeParams *chanp, *bssp;
99768e8e04eSSam Leffler 	enum ieee80211_phymode mode;
9988a1b9b6aSSam Leffler 	int i;
9998a1b9b6aSSam Leffler 
100067ce310aSSam Leffler        	/*
100167ce310aSSam Leffler 	 * Set up the channel access parameters for the physical
100267ce310aSSam Leffler 	 * device.  First populate the configured settings.
100367ce310aSSam Leffler 	 */
10048a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
10058a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
10068a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
10078a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
10088a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
10098a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
10108a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
10118a1b9b6aSSam Leffler 
10128a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
10138a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
10148a1b9b6aSSam Leffler 		chanp->wmep_aifsn = wmep->wmep_aifsn;
10158a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
10168a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
10178a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
10188a1b9b6aSSam Leffler 	}
10198a1b9b6aSSam Leffler 
10208a1b9b6aSSam Leffler 	/*
102168e8e04eSSam Leffler 	 * Select mode; we can be called early in which case we
102268e8e04eSSam Leffler 	 * always use auto mode.  We know we'll be called when
102368e8e04eSSam Leffler 	 * entering the RUN state with bsschan setup properly
102468e8e04eSSam Leffler 	 * so state will eventually get set correctly
102568e8e04eSSam Leffler 	 */
102668e8e04eSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
102768e8e04eSSam Leffler 		mode = ieee80211_chan2mode(ic->ic_bsschan);
102868e8e04eSSam Leffler 	else
102968e8e04eSSam Leffler 		mode = IEEE80211_MODE_AUTO;
103068e8e04eSSam Leffler 
103168e8e04eSSam Leffler 	/*
10328a1b9b6aSSam Leffler 	 * This implements agressive mode as found in certain
10338a1b9b6aSSam Leffler 	 * vendors' AP's.  When there is significant high
10348a1b9b6aSSam Leffler 	 * priority (VI/VO) traffic in the BSS throttle back BE
10358a1b9b6aSSam Leffler 	 * traffic by using conservative parameters.  Otherwise
10368a1b9b6aSSam Leffler 	 * BE uses agressive params to optimize performance of
10378a1b9b6aSSam Leffler 	 * legacy/non-QoS traffic.
10388a1b9b6aSSam Leffler 	 */
1039b032f27cSSam Leffler         if ((vap->iv_opmode == IEEE80211_M_HOSTAP &&
1040ad262427SSam Leffler 	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
1041b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_STA &&
1042b032f27cSSam Leffler 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
1043b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_WME) == 0) {
10448a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
10458a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
10468a1b9b6aSSam Leffler 
104767ce310aSSam Leffler 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
10488a1b9b6aSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
104967ce310aSSam Leffler 		    aggrParam[mode].logcwmin;
10508a1b9b6aSSam Leffler 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
105167ce310aSSam Leffler 		    aggrParam[mode].logcwmax;
10528a1b9b6aSSam Leffler 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1053b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_BURST) ?
105467ce310aSSam Leffler 			aggrParam[mode].txopLimit : 0;
1055b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
105667ce310aSSam Leffler 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
105767ce310aSSam Leffler 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
105867ce310aSSam Leffler 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
105967ce310aSSam Leffler 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
10608a1b9b6aSSam Leffler 	}
10618a1b9b6aSSam Leffler 
1062b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1063ad262427SSam Leffler 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
106468e8e04eSSam Leffler 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1065be0df3e7SSam Leffler 		    [IEEE80211_MODE_AUTO]	= 3,
1066be0df3e7SSam Leffler 		    [IEEE80211_MODE_11A]	= 3,
1067be0df3e7SSam Leffler 		    [IEEE80211_MODE_11B]	= 4,
1068be0df3e7SSam Leffler 		    [IEEE80211_MODE_11G]	= 3,
1069be0df3e7SSam Leffler 		    [IEEE80211_MODE_FH]		= 4,
1070be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_A]	= 3,
1071be0df3e7SSam Leffler 		    [IEEE80211_MODE_TURBO_G]	= 3,
1072be0df3e7SSam Leffler 		    [IEEE80211_MODE_STURBO_A]	= 3,
10736a76ae21SSam Leffler 		    [IEEE80211_MODE_HALF]	= 3,
10746a76ae21SSam Leffler 		    [IEEE80211_MODE_QUARTER]	= 3,
1075be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NA]	= 3,
1076be0df3e7SSam Leffler 		    [IEEE80211_MODE_11NG]	= 3,
10778a1b9b6aSSam Leffler 		};
10788a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
10798a1b9b6aSSam Leffler 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
10808a1b9b6aSSam Leffler 
108168e8e04eSSam Leffler 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1082b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
108367ce310aSSam Leffler 		    "update %s (chan+bss) logcwmin %u\n",
108467ce310aSSam Leffler 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
10858a1b9b6aSSam Leffler     	}
1086b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
10878a1b9b6aSSam Leffler 		/*
10888a1b9b6aSSam Leffler 		 * Arrange for a beacon update and bump the parameter
10898a1b9b6aSSam Leffler 		 * set number so associated stations load the new values.
10908a1b9b6aSSam Leffler 		 */
10918a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info =
10928a1b9b6aSSam Leffler 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1093b032f27cSSam Leffler 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
10948a1b9b6aSSam Leffler 	}
10958a1b9b6aSSam Leffler 
10968a1b9b6aSSam Leffler 	wme->wme_update(ic);
10978a1b9b6aSSam Leffler 
1098b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
10998a1b9b6aSSam Leffler 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1100b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_STA ?
11018a1b9b6aSSam Leffler 		wme->wme_wmeChanParams.cap_info :
11028a1b9b6aSSam Leffler 		wme->wme_bssChanParams.cap_info);
11038a1b9b6aSSam Leffler }
11048a1b9b6aSSam Leffler 
11058a1b9b6aSSam Leffler void
1106b032f27cSSam Leffler ieee80211_wme_updateparams(struct ieee80211vap *vap)
11078a1b9b6aSSam Leffler {
1108b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
11098a1b9b6aSSam Leffler 
11108a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_WME) {
1111b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
1112b032f27cSSam Leffler 		ieee80211_wme_updateparams_locked(vap);
1113b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
11148a1b9b6aSSam Leffler 	}
11158a1b9b6aSSam Leffler }
11168a1b9b6aSSam Leffler 
1117b032f27cSSam Leffler static void
1118b032f27cSSam Leffler parent_updown(void *arg, int npending)
111968e8e04eSSam Leffler {
1120b032f27cSSam Leffler 	struct ifnet *parent = arg;
112168e8e04eSSam Leffler 
1122b032f27cSSam Leffler 	parent->if_ioctl(parent, SIOCSIFFLAGS, NULL);
1123b032f27cSSam Leffler }
112468e8e04eSSam Leffler 
11255efea30fSAndrew Thompson static void
11265efea30fSAndrew Thompson update_mcast(void *arg, int npending)
11275efea30fSAndrew Thompson {
11285efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
11295efea30fSAndrew Thompson 	struct ifnet *parent = ic->ic_ifp;
11305efea30fSAndrew Thompson 
11315efea30fSAndrew Thompson 	ic->ic_update_mcast(parent);
11325efea30fSAndrew Thompson }
11335efea30fSAndrew Thompson 
11345efea30fSAndrew Thompson static void
11355efea30fSAndrew Thompson update_promisc(void *arg, int npending)
11365efea30fSAndrew Thompson {
11375efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
11385efea30fSAndrew Thompson 	struct ifnet *parent = ic->ic_ifp;
11395efea30fSAndrew Thompson 
11405efea30fSAndrew Thompson 	ic->ic_update_promisc(parent);
11415efea30fSAndrew Thompson }
11425efea30fSAndrew Thompson 
11435efea30fSAndrew Thompson static void
11445efea30fSAndrew Thompson update_channel(void *arg, int npending)
11455efea30fSAndrew Thompson {
11465efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
11475efea30fSAndrew Thompson 
11485efea30fSAndrew Thompson 	ic->ic_set_channel(ic);
11495463c4a4SSam Leffler 	ieee80211_radiotap_chan_change(ic);
11505efea30fSAndrew Thompson }
11515efea30fSAndrew Thompson 
1152*b94299c4SAdrian Chadd static void
1153*b94299c4SAdrian Chadd update_chw(void *arg, int npending)
1154*b94299c4SAdrian Chadd {
1155*b94299c4SAdrian Chadd 	struct ieee80211com *ic = arg;
1156*b94299c4SAdrian Chadd 
1157*b94299c4SAdrian Chadd 	/*
1158*b94299c4SAdrian Chadd 	 * XXX should we defer the channel width _config_ update until now?
1159*b94299c4SAdrian Chadd 	 */
1160*b94299c4SAdrian Chadd 	ic->ic_update_chw(ic);
1161*b94299c4SAdrian Chadd }
1162*b94299c4SAdrian Chadd 
116368e8e04eSSam Leffler /*
1164ae55932eSAndrew Thompson  * Block until the parent is in a known state.  This is
1165ae55932eSAndrew Thompson  * used after any operations that dispatch a task (e.g.
1166ae55932eSAndrew Thompson  * to auto-configure the parent device up/down).
1167ae55932eSAndrew Thompson  */
1168ae55932eSAndrew Thompson void
1169ae55932eSAndrew Thompson ieee80211_waitfor_parent(struct ieee80211com *ic)
1170ae55932eSAndrew Thompson {
11715efea30fSAndrew Thompson 	taskqueue_block(ic->ic_tq);
11725efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_parent_task);
11735efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_mcast_task);
11745efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_promisc_task);
11755efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_chan_task);
11765efea30fSAndrew Thompson 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1177*b94299c4SAdrian Chadd 	ieee80211_draintask(ic, &ic->ic_chw_task);
11785efea30fSAndrew Thompson 	taskqueue_unblock(ic->ic_tq);
1179ae55932eSAndrew Thompson }
1180ae55932eSAndrew Thompson 
1181ae55932eSAndrew Thompson /*
1182b032f27cSSam Leffler  * Start a vap running.  If this is the first vap to be
1183b032f27cSSam Leffler  * set running on the underlying device then we
1184b032f27cSSam Leffler  * automatically bring the device up.
118568e8e04eSSam Leffler  */
1186b032f27cSSam Leffler void
1187b032f27cSSam Leffler ieee80211_start_locked(struct ieee80211vap *vap)
1188b032f27cSSam Leffler {
1189b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1190b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1191b032f27cSSam Leffler 	struct ifnet *parent = ic->ic_ifp;
1192b032f27cSSam Leffler 
1193b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1194b032f27cSSam Leffler 
1195b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap,
1196b032f27cSSam Leffler 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1197b032f27cSSam Leffler 		"start running, %d vaps running\n", ic->ic_nrunning);
1198b032f27cSSam Leffler 
1199b032f27cSSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1200b032f27cSSam Leffler 		/*
1201b032f27cSSam Leffler 		 * Mark us running.  Note that it's ok to do this first;
1202b032f27cSSam Leffler 		 * if we need to bring the parent device up we defer that
1203b032f27cSSam Leffler 		 * to avoid dropping the com lock.  We expect the device
1204b032f27cSSam Leffler 		 * to respond to being marked up by calling back into us
1205b032f27cSSam Leffler 		 * through ieee80211_start_all at which point we'll come
1206b032f27cSSam Leffler 		 * back in here and complete the work.
1207b032f27cSSam Leffler 		 */
1208b032f27cSSam Leffler 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1209b032f27cSSam Leffler 		/*
1210b032f27cSSam Leffler 		 * We are not running; if this we are the first vap
1211b032f27cSSam Leffler 		 * to be brought up auto-up the parent if necessary.
1212b032f27cSSam Leffler 		 */
1213b032f27cSSam Leffler 		if (ic->ic_nrunning++ == 0 &&
1214b032f27cSSam Leffler 		    (parent->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1215b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1216b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1217b032f27cSSam Leffler 			    "%s: up parent %s\n", __func__, parent->if_xname);
1218b032f27cSSam Leffler 			parent->if_flags |= IFF_UP;
12195efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1220b032f27cSSam Leffler 			return;
1221b032f27cSSam Leffler 		}
1222b032f27cSSam Leffler 	}
1223b032f27cSSam Leffler 	/*
1224b032f27cSSam Leffler 	 * If the parent is up and running, then kick the
1225b032f27cSSam Leffler 	 * 802.11 state machine as appropriate.
1226b032f27cSSam Leffler 	 */
1227b032f27cSSam Leffler 	if ((parent->if_drv_flags & IFF_DRV_RUNNING) &&
1228b032f27cSSam Leffler 	    vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1229b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA) {
1230b032f27cSSam Leffler #if 0
1231b032f27cSSam Leffler 			/* XXX bypasses scan too easily; disable for now */
1232b032f27cSSam Leffler 			/*
1233b032f27cSSam Leffler 			 * Try to be intelligent about clocking the state
1234b032f27cSSam Leffler 			 * machine.  If we're currently in RUN state then
1235b032f27cSSam Leffler 			 * we should be able to apply any new state/parameters
1236b032f27cSSam Leffler 			 * simply by re-associating.  Otherwise we need to
1237b032f27cSSam Leffler 			 * re-scan to select an appropriate ap.
1238b032f27cSSam Leffler 			 */
1239b032f27cSSam Leffler 			if (vap->iv_state >= IEEE80211_S_RUN)
1240b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1241b032f27cSSam Leffler 				    IEEE80211_S_ASSOC, 1);
1242b032f27cSSam Leffler 			else
1243b032f27cSSam Leffler #endif
1244b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1245b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
124668e8e04eSSam Leffler 		} else {
124768e8e04eSSam Leffler 			/*
1248b032f27cSSam Leffler 			 * For monitor+wds mode there's nothing to do but
1249b032f27cSSam Leffler 			 * start running.  Otherwise if this is the first
125068e8e04eSSam Leffler 			 * vap to be brought up, start a scan which may be
125168e8e04eSSam Leffler 			 * preempted if the station is locked to a particular
125268e8e04eSSam Leffler 			 * channel.
125368e8e04eSSam Leffler 			 */
12545efea30fSAndrew Thompson 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1255b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1256b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_WDS)
1257b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1258b032f27cSSam Leffler 				    IEEE80211_S_RUN, -1);
1259b032f27cSSam Leffler 			else
1260b032f27cSSam Leffler 				ieee80211_new_state_locked(vap,
1261b032f27cSSam Leffler 				    IEEE80211_S_SCAN, 0);
126268e8e04eSSam Leffler 		}
126368e8e04eSSam Leffler 	}
1264b032f27cSSam Leffler }
1265b032f27cSSam Leffler 
1266b032f27cSSam Leffler /*
1267b032f27cSSam Leffler  * Start a single vap.
1268b032f27cSSam Leffler  */
1269b032f27cSSam Leffler void
1270b032f27cSSam Leffler ieee80211_init(void *arg)
1271b032f27cSSam Leffler {
1272b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1273b032f27cSSam Leffler 
127435f434b2SSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1275b032f27cSSam Leffler 	    "%s\n", __func__);
1276b032f27cSSam Leffler 
1277b032f27cSSam Leffler 	IEEE80211_LOCK(vap->iv_ic);
1278b032f27cSSam Leffler 	ieee80211_start_locked(vap);
1279b032f27cSSam Leffler 	IEEE80211_UNLOCK(vap->iv_ic);
1280b032f27cSSam Leffler }
1281b032f27cSSam Leffler 
1282b032f27cSSam Leffler /*
1283b032f27cSSam Leffler  * Start all runnable vap's on a device.
1284b032f27cSSam Leffler  */
1285b032f27cSSam Leffler void
1286b032f27cSSam Leffler ieee80211_start_all(struct ieee80211com *ic)
1287b032f27cSSam Leffler {
1288b032f27cSSam Leffler 	struct ieee80211vap *vap;
1289b032f27cSSam Leffler 
1290b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1291b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1292b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1293b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1294b032f27cSSam Leffler 			ieee80211_start_locked(vap);
1295b032f27cSSam Leffler 	}
1296b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1297b032f27cSSam Leffler }
1298b032f27cSSam Leffler 
1299b032f27cSSam Leffler /*
1300b032f27cSSam Leffler  * Stop a vap.  We force it down using the state machine
1301b032f27cSSam Leffler  * then mark it's ifnet not running.  If this is the last
1302b032f27cSSam Leffler  * vap running on the underlying device then we close it
1303b032f27cSSam Leffler  * too to insure it will be properly initialized when the
1304b032f27cSSam Leffler  * next vap is brought up.
1305b032f27cSSam Leffler  */
1306b032f27cSSam Leffler void
1307b032f27cSSam Leffler ieee80211_stop_locked(struct ieee80211vap *vap)
1308b032f27cSSam Leffler {
1309b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1310b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1311b032f27cSSam Leffler 	struct ifnet *parent = ic->ic_ifp;
1312b032f27cSSam Leffler 
1313b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1314b032f27cSSam Leffler 
1315b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1316b032f27cSSam Leffler 	    "stop running, %d vaps running\n", ic->ic_nrunning);
1317b032f27cSSam Leffler 
1318b032f27cSSam Leffler 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1319b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1320b032f27cSSam Leffler 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1321b032f27cSSam Leffler 		if (--ic->ic_nrunning == 0 &&
1322b032f27cSSam Leffler 		    (parent->if_drv_flags & IFF_DRV_RUNNING)) {
1323b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap,
1324b032f27cSSam Leffler 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1325b032f27cSSam Leffler 			    "down parent %s\n", parent->if_xname);
1326b032f27cSSam Leffler 			parent->if_flags &= ~IFF_UP;
13275efea30fSAndrew Thompson 			ieee80211_runtask(ic, &ic->ic_parent_task);
1328b032f27cSSam Leffler 		}
1329b032f27cSSam Leffler 	}
1330b032f27cSSam Leffler }
1331b032f27cSSam Leffler 
1332b032f27cSSam Leffler void
1333b032f27cSSam Leffler ieee80211_stop(struct ieee80211vap *vap)
1334b032f27cSSam Leffler {
1335b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1336b032f27cSSam Leffler 
1337b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1338b032f27cSSam Leffler 	ieee80211_stop_locked(vap);
1339b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1340b032f27cSSam Leffler }
1341b032f27cSSam Leffler 
1342b032f27cSSam Leffler /*
1343b032f27cSSam Leffler  * Stop all vap's running on a device.
1344b032f27cSSam Leffler  */
1345b032f27cSSam Leffler void
1346b032f27cSSam Leffler ieee80211_stop_all(struct ieee80211com *ic)
1347b032f27cSSam Leffler {
1348b032f27cSSam Leffler 	struct ieee80211vap *vap;
1349b032f27cSSam Leffler 
1350b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1351b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1352b032f27cSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
1353b032f27cSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1354b032f27cSSam Leffler 			ieee80211_stop_locked(vap);
1355b032f27cSSam Leffler 	}
1356b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1357ae55932eSAndrew Thompson 
1358ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
135968e8e04eSSam Leffler }
136068e8e04eSSam Leffler 
136168e8e04eSSam Leffler /*
13626076cbacSSam Leffler  * Stop all vap's running on a device and arrange
13636076cbacSSam Leffler  * for those that were running to be resumed.
13646076cbacSSam Leffler  */
13656076cbacSSam Leffler void
13666076cbacSSam Leffler ieee80211_suspend_all(struct ieee80211com *ic)
13676076cbacSSam Leffler {
13686076cbacSSam Leffler 	struct ieee80211vap *vap;
13696076cbacSSam Leffler 
13706076cbacSSam Leffler 	IEEE80211_LOCK(ic);
13716076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
13726076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
13736076cbacSSam Leffler 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
13746076cbacSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
13756076cbacSSam Leffler 			ieee80211_stop_locked(vap);
13766076cbacSSam Leffler 		}
13776076cbacSSam Leffler 	}
13786076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
1379ae55932eSAndrew Thompson 
1380ae55932eSAndrew Thompson 	ieee80211_waitfor_parent(ic);
13816076cbacSSam Leffler }
13826076cbacSSam Leffler 
13836076cbacSSam Leffler /*
13846076cbacSSam Leffler  * Start all vap's marked for resume.
13856076cbacSSam Leffler  */
13866076cbacSSam Leffler void
13876076cbacSSam Leffler ieee80211_resume_all(struct ieee80211com *ic)
13886076cbacSSam Leffler {
13896076cbacSSam Leffler 	struct ieee80211vap *vap;
13906076cbacSSam Leffler 
13916076cbacSSam Leffler 	IEEE80211_LOCK(ic);
13926076cbacSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
13936076cbacSSam Leffler 		struct ifnet *ifp = vap->iv_ifp;
13946076cbacSSam Leffler 		if (!IFNET_IS_UP_RUNNING(ifp) &&
13956076cbacSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
13966076cbacSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
13976076cbacSSam Leffler 			ieee80211_start_locked(vap);
13986076cbacSSam Leffler 		}
13996076cbacSSam Leffler 	}
14006076cbacSSam Leffler 	IEEE80211_UNLOCK(ic);
14016076cbacSSam Leffler }
14026076cbacSSam Leffler 
1403e701e041SSam Leffler void
1404e701e041SSam Leffler ieee80211_beacon_miss(struct ieee80211com *ic)
1405e701e041SSam Leffler {
14065efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
14075efea30fSAndrew Thompson 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
14085efea30fSAndrew Thompson 		/* Process in a taskq, the handler may reenter the driver */
14095efea30fSAndrew Thompson 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
14105efea30fSAndrew Thompson 	}
14115efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
14125efea30fSAndrew Thompson }
14135efea30fSAndrew Thompson 
14145efea30fSAndrew Thompson static void
14155efea30fSAndrew Thompson beacon_miss(void *arg, int npending)
14165efea30fSAndrew Thompson {
14175efea30fSAndrew Thompson 	struct ieee80211com *ic = arg;
1418b032f27cSSam Leffler 	struct ieee80211vap *vap;
1419e701e041SSam Leffler 
142023401900SAdrian Chadd 	IEEE80211_LOCK(ic);
1421b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1422e701e041SSam Leffler 		/*
1423b032f27cSSam Leffler 		 * We only pass events through for sta vap's in RUN state;
1424b032f27cSSam Leffler 		 * may be too restrictive but for now this saves all the
1425b032f27cSSam Leffler 		 * handlers duplicating these checks.
1426e701e041SSam Leffler 		 */
1427b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA &&
1428c70761e6SSam Leffler 		    vap->iv_state >= IEEE80211_S_RUN &&
1429b032f27cSSam Leffler 		    vap->iv_bmiss != NULL)
1430b032f27cSSam Leffler 			vap->iv_bmiss(vap);
1431e701e041SSam Leffler 	}
143223401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
143368e8e04eSSam Leffler }
1434e701e041SSam Leffler 
14355efea30fSAndrew Thompson static void
14365efea30fSAndrew Thompson beacon_swmiss(void *arg, int npending)
14375efea30fSAndrew Thompson {
14385efea30fSAndrew Thompson 	struct ieee80211vap *vap = arg;
143923401900SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
14405efea30fSAndrew Thompson 
144123401900SAdrian Chadd 	IEEE80211_LOCK(ic);
144223401900SAdrian Chadd 	if (vap->iv_state == IEEE80211_S_RUN) {
14435efea30fSAndrew Thompson 		/* XXX Call multiple times if npending > zero? */
14445efea30fSAndrew Thompson 		vap->iv_bmiss(vap);
14455efea30fSAndrew Thompson 	}
144623401900SAdrian Chadd 	IEEE80211_UNLOCK(ic);
144723401900SAdrian Chadd }
14485efea30fSAndrew Thompson 
1449e99662a6SSam Leffler /*
1450e99662a6SSam Leffler  * Software beacon miss handling.  Check if any beacons
1451e99662a6SSam Leffler  * were received in the last period.  If not post a
1452e99662a6SSam Leffler  * beacon miss; otherwise reset the counter.
1453e99662a6SSam Leffler  */
1454b032f27cSSam Leffler void
1455e99662a6SSam Leffler ieee80211_swbmiss(void *arg)
1456e99662a6SSam Leffler {
1457b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
1458c448998dSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1459e99662a6SSam Leffler 
146023401900SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
146123401900SAdrian Chadd 
1462c448998dSSam Leffler 	/* XXX sleep state? */
1463c448998dSSam Leffler 	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1464c448998dSSam Leffler 	    ("wrong state %d", vap->iv_state));
1465c448998dSSam Leffler 
1466c448998dSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN) {
1467c448998dSSam Leffler 		/*
1468c448998dSSam Leffler 		 * If scanning just ignore and reset state.  If we get a
1469c448998dSSam Leffler 		 * bmiss after coming out of scan because we haven't had
1470c448998dSSam Leffler 		 * time to receive a beacon then we should probe the AP
1471c448998dSSam Leffler 		 * before posting a real bmiss (unless iv_bmiss_max has
1472c448998dSSam Leffler 		 * been artifiically lowered).  A cleaner solution might
1473c448998dSSam Leffler 		 * be to disable the timer on scan start/end but to handle
1474c448998dSSam Leffler 		 * case of multiple sta vap's we'd need to disable the
1475c448998dSSam Leffler 		 * timers of all affected vap's.
1476c448998dSSam Leffler 		 */
1477c448998dSSam Leffler 		vap->iv_swbmiss_count = 0;
1478c448998dSSam Leffler 	} else if (vap->iv_swbmiss_count == 0) {
1479b032f27cSSam Leffler 		if (vap->iv_bmiss != NULL)
14805efea30fSAndrew Thompson 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1481e99662a6SSam Leffler 	} else
1482b032f27cSSam Leffler 		vap->iv_swbmiss_count = 0;
1483b032f27cSSam Leffler 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1484b032f27cSSam Leffler 		ieee80211_swbmiss, vap);
14857edb8cf9SSam Leffler }
14867edb8cf9SSam Leffler 
148768e8e04eSSam Leffler /*
1488b032f27cSSam Leffler  * Start an 802.11h channel switch.  We record the parameters,
1489b032f27cSSam Leffler  * mark the operation pending, notify each vap through the
1490b032f27cSSam Leffler  * beacon update mechanism so it can update the beacon frame
1491b032f27cSSam Leffler  * contents, and then switch vap's to CSA state to block outbound
1492b032f27cSSam Leffler  * traffic.  Devices that handle CSA directly can use the state
1493b032f27cSSam Leffler  * switch to do the right thing so long as they call
1494b032f27cSSam Leffler  * ieee80211_csa_completeswitch when it's time to complete the
1495b032f27cSSam Leffler  * channel change.  Devices that depend on the net80211 layer can
1496b032f27cSSam Leffler  * use ieee80211_beacon_update to handle the countdown and the
1497b032f27cSSam Leffler  * channel switch.
1498b032f27cSSam Leffler  */
1499b032f27cSSam Leffler void
1500b032f27cSSam Leffler ieee80211_csa_startswitch(struct ieee80211com *ic,
1501b032f27cSSam Leffler 	struct ieee80211_channel *c, int mode, int count)
1502b032f27cSSam Leffler {
1503b032f27cSSam Leffler 	struct ieee80211vap *vap;
1504b032f27cSSam Leffler 
1505b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1506b032f27cSSam Leffler 
1507b032f27cSSam Leffler 	ic->ic_csa_newchan = c;
1508c70761e6SSam Leffler 	ic->ic_csa_mode = mode;
1509b032f27cSSam Leffler 	ic->ic_csa_count = count;
1510b032f27cSSam Leffler 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1511b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1512b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
151359aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_IBSS ||
151459aa14a9SRui Paulo 		    vap->iv_opmode == IEEE80211_M_MBSS)
1515b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1516b032f27cSSam Leffler 		/* switch to CSA state to block outbound traffic */
1517b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN)
1518b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1519b032f27cSSam Leffler 	}
1520b032f27cSSam Leffler 	ieee80211_notify_csa(ic, c, mode, count);
1521b032f27cSSam Leffler }
1522b032f27cSSam Leffler 
1523886bbec1SAdrian Chadd /*
1524886bbec1SAdrian Chadd  * Complete the channel switch by transitioning all CSA VAPs to RUN.
1525886bbec1SAdrian Chadd  * This is called by both the completion and cancellation functions
1526886bbec1SAdrian Chadd  * so each VAP is placed back in the RUN state and can thus transmit.
1527886bbec1SAdrian Chadd  */
1528c70761e6SSam Leffler static void
1529c70761e6SSam Leffler csa_completeswitch(struct ieee80211com *ic)
1530c70761e6SSam Leffler {
1531c70761e6SSam Leffler 	struct ieee80211vap *vap;
1532c70761e6SSam Leffler 
1533c70761e6SSam Leffler 	ic->ic_csa_newchan = NULL;
1534c70761e6SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1535c70761e6SSam Leffler 
1536c70761e6SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1537c70761e6SSam Leffler 		if (vap->iv_state == IEEE80211_S_CSA)
1538c70761e6SSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1539c70761e6SSam Leffler }
1540c70761e6SSam Leffler 
1541b032f27cSSam Leffler /*
1542b032f27cSSam Leffler  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1543b032f27cSSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1544b032f27cSSam Leffler  * so they can again transmit.
1545886bbec1SAdrian Chadd  *
1546886bbec1SAdrian Chadd  * Although this may not be completely correct, update the BSS channel
1547886bbec1SAdrian Chadd  * for each VAP to the newly configured channel. The setcurchan sets
1548886bbec1SAdrian Chadd  * the current operating channel for the interface (so the radio does
1549886bbec1SAdrian Chadd  * switch over) but the VAP BSS isn't updated, leading to incorrectly
1550886bbec1SAdrian Chadd  * reported information via ioctl.
1551b032f27cSSam Leffler  */
1552b032f27cSSam Leffler void
1553b032f27cSSam Leffler ieee80211_csa_completeswitch(struct ieee80211com *ic)
1554b032f27cSSam Leffler {
15556f16ec31SAdrian Chadd 	struct ieee80211vap *vap;
15566f16ec31SAdrian Chadd 
1557b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1558b032f27cSSam Leffler 
1559b032f27cSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1560b032f27cSSam Leffler 
1561b032f27cSSam Leffler 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1562886bbec1SAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1563886bbec1SAdrian Chadd 		if (vap->iv_state == IEEE80211_S_CSA)
1564886bbec1SAdrian Chadd 			vap->iv_bss->ni_chan = ic->ic_curchan;
1565886bbec1SAdrian Chadd 
1566c70761e6SSam Leffler 	csa_completeswitch(ic);
1567c70761e6SSam Leffler }
1568b032f27cSSam Leffler 
1569c70761e6SSam Leffler /*
1570c70761e6SSam Leffler  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1571c70761e6SSam Leffler  * We clear state and move all vap's in CSA state to RUN state
1572c70761e6SSam Leffler  * so they can again transmit.
1573c70761e6SSam Leffler  */
1574c70761e6SSam Leffler void
1575c70761e6SSam Leffler ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1576c70761e6SSam Leffler {
1577c70761e6SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1578c70761e6SSam Leffler 
1579c70761e6SSam Leffler 	csa_completeswitch(ic);
1580b032f27cSSam Leffler }
1581b032f27cSSam Leffler 
1582b032f27cSSam Leffler /*
1583b032f27cSSam Leffler  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1584b032f27cSSam Leffler  * We clear state and move all vap's in CAC state to RUN state.
1585b032f27cSSam Leffler  */
1586b032f27cSSam Leffler void
1587b032f27cSSam Leffler ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1588b032f27cSSam Leffler {
1589b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1590b032f27cSSam Leffler 	struct ieee80211vap *vap;
1591b032f27cSSam Leffler 
1592b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1593b032f27cSSam Leffler 	/*
1594b032f27cSSam Leffler 	 * Complete CAC state change for lead vap first; then
1595b032f27cSSam Leffler 	 * clock all the other vap's waiting.
1596b032f27cSSam Leffler 	 */
1597b032f27cSSam Leffler 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1598b032f27cSSam Leffler 	    ("wrong state %d", vap0->iv_state));
1599b032f27cSSam Leffler 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1600b032f27cSSam Leffler 
1601b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1602b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_CAC)
1603b032f27cSSam Leffler 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1604b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1605b032f27cSSam Leffler }
1606b032f27cSSam Leffler 
1607b032f27cSSam Leffler /*
1608b032f27cSSam Leffler  * Force all vap's other than the specified vap to the INIT state
1609b032f27cSSam Leffler  * and mark them as waiting for a scan to complete.  These vaps
1610b032f27cSSam Leffler  * will be brought up when the scan completes and the scanning vap
1611b032f27cSSam Leffler  * reaches RUN state by wakeupwaiting.
161268e8e04eSSam Leffler  */
161368e8e04eSSam Leffler static void
1614b032f27cSSam Leffler markwaiting(struct ieee80211vap *vap0)
161568e8e04eSSam Leffler {
1616b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1617b032f27cSSam Leffler 	struct ieee80211vap *vap;
1618b032f27cSSam Leffler 
1619b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1620b032f27cSSam Leffler 
16215efea30fSAndrew Thompson 	/*
16225efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
16235efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
16245efea30fSAndrew Thompson 	 * change task.
16255efea30fSAndrew Thompson 	 */
1626b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1627b032f27cSSam Leffler 		if (vap == vap0)
1628b032f27cSSam Leffler 			continue;
1629b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_INIT) {
16305efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1631b032f27cSSam Leffler 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1632dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1633b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1634b032f27cSSam Leffler 		}
163568e8e04eSSam Leffler 	}
163668e8e04eSSam Leffler }
163768e8e04eSSam Leffler 
1638b032f27cSSam Leffler /*
1639b032f27cSSam Leffler  * Wakeup all vap's waiting for a scan to complete.  This is the
1640b032f27cSSam Leffler  * companion to markwaiting (above) and is used to coordinate
1641b032f27cSSam Leffler  * multiple vaps scanning.
16425efea30fSAndrew Thompson  * This is called from the state taskqueue.
1643b032f27cSSam Leffler  */
1644b032f27cSSam Leffler static void
1645b032f27cSSam Leffler wakeupwaiting(struct ieee80211vap *vap0)
1646b032f27cSSam Leffler {
1647b032f27cSSam Leffler 	struct ieee80211com *ic = vap0->iv_ic;
1648b032f27cSSam Leffler 	struct ieee80211vap *vap;
1649b032f27cSSam Leffler 
1650b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1651b032f27cSSam Leffler 
16525efea30fSAndrew Thompson 	/*
16535efea30fSAndrew Thompson 	 * A vap list entry can not disappear since we are running on the
16545efea30fSAndrew Thompson 	 * taskqueue and a vap destroy will queue and drain another state
16555efea30fSAndrew Thompson 	 * change task.
16565efea30fSAndrew Thompson 	 */
1657b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1658b032f27cSSam Leffler 		if (vap == vap0)
1659b032f27cSSam Leffler 			continue;
1660b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1661b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1662b032f27cSSam Leffler 			/* NB: sta's cannot go INIT->RUN */
16635efea30fSAndrew Thompson 			/* NB: iv_newstate may drop the lock */
1664b032f27cSSam Leffler 			vap->iv_newstate(vap,
1665b032f27cSSam Leffler 			    vap->iv_opmode == IEEE80211_M_STA ?
1666b032f27cSSam Leffler 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1667dcc56af0SAdrian Chadd 			IEEE80211_LOCK_ASSERT(ic);
1668b032f27cSSam Leffler 		}
1669b032f27cSSam Leffler 	}
1670b032f27cSSam Leffler }
1671b032f27cSSam Leffler 
1672b032f27cSSam Leffler /*
1673b032f27cSSam Leffler  * Handle post state change work common to all operating modes.
1674b032f27cSSam Leffler  */
1675b032f27cSSam Leffler static void
16765efea30fSAndrew Thompson ieee80211_newstate_cb(void *xvap, int npending)
1677b032f27cSSam Leffler {
16785efea30fSAndrew Thompson 	struct ieee80211vap *vap = xvap;
1679b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
16805efea30fSAndrew Thompson 	enum ieee80211_state nstate, ostate;
16815efea30fSAndrew Thompson 	int arg, rc;
1682b032f27cSSam Leffler 
16835efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
16845efea30fSAndrew Thompson 	nstate = vap->iv_nstate;
16855efea30fSAndrew Thompson 	arg = vap->iv_nstate_arg;
1686b032f27cSSam Leffler 
16875efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
16885efea30fSAndrew Thompson 		/*
16895efea30fSAndrew Thompson 		 * We have been requested to drop back to the INIT before
16905efea30fSAndrew Thompson 		 * proceeding to the new state.
16915efea30fSAndrew Thompson 		 */
1692b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
16935efea30fSAndrew Thompson 		    "%s: %s -> %s arg %d\n", __func__,
16945efea30fSAndrew Thompson 		    ieee80211_state_name[vap->iv_state],
16955efea30fSAndrew Thompson 		    ieee80211_state_name[IEEE80211_S_INIT], arg);
16965efea30fSAndrew Thompson 		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1697dcc56af0SAdrian Chadd 		IEEE80211_LOCK_ASSERT(ic);
16985efea30fSAndrew Thompson 		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
16995efea30fSAndrew Thompson 	}
17005efea30fSAndrew Thompson 
17015efea30fSAndrew Thompson 	ostate = vap->iv_state;
17025efea30fSAndrew Thompson 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
17035efea30fSAndrew Thompson 		/*
17045efea30fSAndrew Thompson 		 * SCAN was forced; e.g. on beacon miss.  Force other running
17055efea30fSAndrew Thompson 		 * vap's to INIT state and mark them as waiting for the scan to
17065efea30fSAndrew Thompson 		 * complete.  This insures they don't interfere with our
17075efea30fSAndrew Thompson 		 * scanning.  Since we are single threaded the vaps can not
17085efea30fSAndrew Thompson 		 * transition again while we are executing.
17095efea30fSAndrew Thompson 		 *
17105efea30fSAndrew Thompson 		 * XXX not always right, assumes ap follows sta
17115efea30fSAndrew Thompson 		 */
17125efea30fSAndrew Thompson 		markwaiting(vap);
17135efea30fSAndrew Thompson 	}
17145efea30fSAndrew Thompson 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
17155efea30fSAndrew Thompson 	    "%s: %s -> %s arg %d\n", __func__,
17165efea30fSAndrew Thompson 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
17175efea30fSAndrew Thompson 
17185efea30fSAndrew Thompson 	rc = vap->iv_newstate(vap, nstate, arg);
1719dcc56af0SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
17205efea30fSAndrew Thompson 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
17215efea30fSAndrew Thompson 	if (rc != 0) {
17225efea30fSAndrew Thompson 		/* State transition failed */
17235efea30fSAndrew Thompson 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
17245efea30fSAndrew Thompson 		KASSERT(nstate != IEEE80211_S_INIT,
17255efea30fSAndrew Thompson 		    ("INIT state change failed"));
17265efea30fSAndrew Thompson 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
17275efea30fSAndrew Thompson 		    "%s: %s returned error %d\n", __func__,
17285efea30fSAndrew Thompson 		    ieee80211_state_name[nstate], rc);
17295efea30fSAndrew Thompson 		goto done;
17305efea30fSAndrew Thompson 	}
17315efea30fSAndrew Thompson 
17325efea30fSAndrew Thompson 	/* No actual transition, skip post processing */
17335efea30fSAndrew Thompson 	if (ostate == nstate)
17345efea30fSAndrew Thompson 		goto done;
1735b032f27cSSam Leffler 
1736b032f27cSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
1737b032f27cSSam Leffler 		/*
1738b032f27cSSam Leffler 		 * OACTIVE may be set on the vap if the upper layer
1739b032f27cSSam Leffler 		 * tried to transmit (e.g. IPv6 NDP) before we reach
1740b032f27cSSam Leffler 		 * RUN state.  Clear it and restart xmit.
1741b032f27cSSam Leffler 		 *
1742b032f27cSSam Leffler 		 * Note this can also happen as a result of SLEEP->RUN
1743b032f27cSSam Leffler 		 * (i.e. coming out of power save mode).
1744b032f27cSSam Leffler 		 */
1745dcc56af0SAdrian Chadd 		IF_LOCK(&vap->iv_ifp->if_snd);
1746b032f27cSSam Leffler 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1747dcc56af0SAdrian Chadd 		IF_UNLOCK(&vap->iv_ifp->if_snd);
1748b032f27cSSam Leffler 		if_start(vap->iv_ifp);
1749b032f27cSSam Leffler 
1750b032f27cSSam Leffler 		/* bring up any vaps waiting on us */
1751b032f27cSSam Leffler 		wakeupwaiting(vap);
1752b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
1753b032f27cSSam Leffler 		/*
1754b032f27cSSam Leffler 		 * Flush the scan cache if we did the last scan (XXX?)
1755b032f27cSSam Leffler 		 * and flush any frames on send queues from this vap.
1756b032f27cSSam Leffler 		 * Note the mgt q is used only for legacy drivers and
1757b032f27cSSam Leffler 		 * will go away shortly.
1758b032f27cSSam Leffler 		 */
1759b032f27cSSam Leffler 		ieee80211_scan_flush(vap);
1760b032f27cSSam Leffler 
1761b032f27cSSam Leffler 		/* XXX NB: cast for altq */
1762b032f27cSSam Leffler 		ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1763b032f27cSSam Leffler 	}
17645efea30fSAndrew Thompson done:
17655efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
1766b032f27cSSam Leffler }
1767b032f27cSSam Leffler 
1768b032f27cSSam Leffler /*
1769b032f27cSSam Leffler  * Public interface for initiating a state machine change.
1770b032f27cSSam Leffler  * This routine single-threads the request and coordinates
1771b032f27cSSam Leffler  * the scheduling of multiple vaps for the purpose of selecting
1772b032f27cSSam Leffler  * an operating channel.  Specifically the following scenarios
1773b032f27cSSam Leffler  * are handled:
1774b032f27cSSam Leffler  * o only one vap can be selecting a channel so on transition to
1775b032f27cSSam Leffler  *   SCAN state if another vap is already scanning then
1776b032f27cSSam Leffler  *   mark the caller for later processing and return without
1777b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
1778b032f27cSSam Leffler  * o only one vap can be doing CAC of a channel so on transition to
1779b032f27cSSam Leffler  *   CAC state if another vap is already scanning for radar then
1780b032f27cSSam Leffler  *   mark the caller for later processing and return without
1781b032f27cSSam Leffler  *   doing anything (XXX? expectations by caller of synchronous operation)
1782b032f27cSSam Leffler  * o if another vap is already running when a request is made
1783b032f27cSSam Leffler  *   to SCAN then an operating channel has been chosen; bypass
1784b032f27cSSam Leffler  *   the scan and just join the channel
1785b032f27cSSam Leffler  *
1786b032f27cSSam Leffler  * Note that the state change call is done through the iv_newstate
1787b032f27cSSam Leffler  * method pointer so any driver routine gets invoked.  The driver
1788b032f27cSSam Leffler  * will normally call back into operating mode-specific
1789b032f27cSSam Leffler  * ieee80211_newstate routines (below) unless it needs to completely
1790b032f27cSSam Leffler  * bypass the state machine (e.g. because the firmware has it's
1791b032f27cSSam Leffler  * own idea how things should work).  Bypassing the net80211 layer
1792b032f27cSSam Leffler  * is usually a mistake and indicates lack of proper integration
1793b032f27cSSam Leffler  * with the net80211 layer.
1794b032f27cSSam Leffler  */
17958a1b9b6aSSam Leffler static int
1796b032f27cSSam Leffler ieee80211_new_state_locked(struct ieee80211vap *vap,
1797b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
17988a1b9b6aSSam Leffler {
1799b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1800b032f27cSSam Leffler 	struct ieee80211vap *vp;
1801a11c9a5cSSam Leffler 	enum ieee80211_state ostate;
18025efea30fSAndrew Thompson 	int nrunning, nscanning;
18031a1e1d21SSam Leffler 
1804b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1805b032f27cSSam Leffler 
18065efea30fSAndrew Thompson 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
18075efea30fSAndrew Thompson 		if (vap->iv_nstate == IEEE80211_S_INIT) {
18085efea30fSAndrew Thompson 			/*
18095efea30fSAndrew Thompson 			 * XXX The vap is being stopped, do no allow any other
18105efea30fSAndrew Thompson 			 * state changes until this is completed.
18115efea30fSAndrew Thompson 			 */
18125efea30fSAndrew Thompson 			return -1;
18138ee6f90aSAndrew Thompson 		} else if (vap->iv_state != vap->iv_nstate) {
18145efea30fSAndrew Thompson #if 0
18155efea30fSAndrew Thompson 			/* Warn if the previous state hasn't completed. */
18165efea30fSAndrew Thompson 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
18175efea30fSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n", __func__,
18185efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_state],
18195efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
18205efea30fSAndrew Thompson #else
18215efea30fSAndrew Thompson 			/* XXX temporarily enable to identify issues */
18228ee6f90aSAndrew Thompson 			if_printf(vap->iv_ifp,
18238ee6f90aSAndrew Thompson 			    "%s: pending %s -> %s transition lost\n",
18245efea30fSAndrew Thompson 			    __func__, ieee80211_state_name[vap->iv_state],
18255efea30fSAndrew Thompson 			    ieee80211_state_name[vap->iv_nstate]);
18265efea30fSAndrew Thompson #endif
18275efea30fSAndrew Thompson 		}
18288ee6f90aSAndrew Thompson 	}
18295efea30fSAndrew Thompson 
1830b032f27cSSam Leffler 	nrunning = nscanning = 0;
1831b032f27cSSam Leffler 	/* XXX can track this state instead of calculating */
1832b032f27cSSam Leffler 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1833b032f27cSSam Leffler 		if (vp != vap) {
1834b032f27cSSam Leffler 			if (vp->iv_state >= IEEE80211_S_RUN)
1835b032f27cSSam Leffler 				nrunning++;
1836b032f27cSSam Leffler 			/* XXX doesn't handle bg scan */
1837b032f27cSSam Leffler 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1838b032f27cSSam Leffler 			else if (vp->iv_state > IEEE80211_S_INIT)
1839b032f27cSSam Leffler 				nscanning++;
1840b032f27cSSam Leffler 		}
1841b032f27cSSam Leffler 	}
1842b032f27cSSam Leffler 	ostate = vap->iv_state;
1843b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1844b032f27cSSam Leffler 	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1845b032f27cSSam Leffler 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1846b032f27cSSam Leffler 	    nrunning, nscanning);
18471a1e1d21SSam Leffler 	switch (nstate) {
18481a1e1d21SSam Leffler 	case IEEE80211_S_SCAN:
1849b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT) {
18501a1e1d21SSam Leffler 			/*
1851b032f27cSSam Leffler 			 * INIT -> SCAN happens on initial bringup.
18521a1e1d21SSam Leffler 			 */
1853b032f27cSSam Leffler 			KASSERT(!(nscanning && nrunning),
1854b032f27cSSam Leffler 			    ("%d scanning and %d running", nscanning, nrunning));
1855b032f27cSSam Leffler 			if (nscanning) {
185668e8e04eSSam Leffler 				/*
1857b032f27cSSam Leffler 				 * Someone is scanning, defer our state
1858b032f27cSSam Leffler 				 * change until the work has completed.
185968e8e04eSSam Leffler 				 */
1860b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1861b032f27cSSam Leffler 				    "%s: defer %s -> %s\n",
1862b032f27cSSam Leffler 				    __func__, ieee80211_state_name[ostate],
1863b032f27cSSam Leffler 				    ieee80211_state_name[nstate]);
1864b032f27cSSam Leffler 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
18655efea30fSAndrew Thompson 				return 0;
186668e8e04eSSam Leffler 			}
1867b032f27cSSam Leffler 			if (nrunning) {
186868e8e04eSSam Leffler 				/*
1869b032f27cSSam Leffler 				 * Someone is operating; just join the channel
1870b032f27cSSam Leffler 				 * they have chosen.
187168e8e04eSSam Leffler 				 */
1872b032f27cSSam Leffler 				/* XXX kill arg? */
1873b032f27cSSam Leffler 				/* XXX check each opmode, adhoc? */
1874b032f27cSSam Leffler 				if (vap->iv_opmode == IEEE80211_M_STA)
1875b032f27cSSam Leffler 					nstate = IEEE80211_S_SCAN;
18761a1e1d21SSam Leffler 				else
1877b032f27cSSam Leffler 					nstate = IEEE80211_S_RUN;
1878b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
1879b032f27cSSam Leffler 				if (nstate != IEEE80211_S_SCAN) {
1880b032f27cSSam Leffler 					IEEE80211_DPRINTF(vap,
1881b032f27cSSam Leffler 					    IEEE80211_MSG_STATE,
1882b032f27cSSam Leffler 					    "%s: override, now %s -> %s\n",
1883b032f27cSSam Leffler 					    __func__,
1884b032f27cSSam Leffler 					    ieee80211_state_name[ostate],
1885b032f27cSSam Leffler 					    ieee80211_state_name[nstate]);
18861a1e1d21SSam Leffler 				}
18878a1b9b6aSSam Leffler #endif
188868e8e04eSSam Leffler 			}
1889b032f27cSSam Leffler 		}
18901a1e1d21SSam Leffler 		break;
1891b032f27cSSam Leffler 	case IEEE80211_S_RUN:
1892b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS &&
1893b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1894b032f27cSSam Leffler 		    nscanning) {
1895b032f27cSSam Leffler 			/*
1896b032f27cSSam Leffler 			 * Legacy WDS with someone else scanning; don't
1897b032f27cSSam Leffler 			 * go online until that completes as we should
1898b032f27cSSam Leffler 			 * follow the other vap to the channel they choose.
1899b032f27cSSam Leffler 			 */
1900b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1901b032f27cSSam Leffler 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
1902b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
1903b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
1904b032f27cSSam Leffler 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
19055efea30fSAndrew Thompson 			return 0;
1906b032f27cSSam Leffler 		}
1907b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1908b032f27cSSam Leffler 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1909b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1910b032f27cSSam Leffler 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1911b032f27cSSam Leffler 			/*
1912b032f27cSSam Leffler 			 * This is a DFS channel, transition to CAC state
1913b032f27cSSam Leffler 			 * instead of RUN.  This allows us to initiate
1914b032f27cSSam Leffler 			 * Channel Availability Check (CAC) as specified
1915b032f27cSSam Leffler 			 * by 11h/DFS.
1916b032f27cSSam Leffler 			 */
1917b032f27cSSam Leffler 			nstate = IEEE80211_S_CAC;
1918b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1919b032f27cSSam Leffler 			     "%s: override %s -> %s (DFS)\n", __func__,
1920b032f27cSSam Leffler 			     ieee80211_state_name[ostate],
1921b032f27cSSam Leffler 			     ieee80211_state_name[nstate]);
1922b032f27cSSam Leffler 		}
1923b032f27cSSam Leffler 		break;
1924b032f27cSSam Leffler 	case IEEE80211_S_INIT:
1925b016f58cSAndrew Thompson 		/* cancel any scan in progress */
1926b016f58cSAndrew Thompson 		ieee80211_cancel_scan(vap);
1927b032f27cSSam Leffler 		if (ostate == IEEE80211_S_INIT ) {
1928b032f27cSSam Leffler 			/* XXX don't believe this */
1929b032f27cSSam Leffler 			/* INIT -> INIT. nothing to do */
1930b032f27cSSam Leffler 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1931b032f27cSSam Leffler 		}
1932b032f27cSSam Leffler 		/* fall thru... */
193314fb6b8fSSam Leffler 	default:
193414fb6b8fSSam Leffler 		break;
19351a1e1d21SSam Leffler 	}
19365efea30fSAndrew Thompson 	/* defer the state change to a thread */
19375efea30fSAndrew Thompson 	vap->iv_nstate = nstate;
19385efea30fSAndrew Thompson 	vap->iv_nstate_arg = arg;
19395efea30fSAndrew Thompson 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
19405efea30fSAndrew Thompson 	ieee80211_runtask(ic, &vap->iv_nstate_task);
19415efea30fSAndrew Thompson 	return EINPROGRESS;
19428a1b9b6aSSam Leffler }
1943b032f27cSSam Leffler 
1944b032f27cSSam Leffler int
1945b032f27cSSam Leffler ieee80211_new_state(struct ieee80211vap *vap,
1946b032f27cSSam Leffler 	enum ieee80211_state nstate, int arg)
1947b032f27cSSam Leffler {
1948b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1949b032f27cSSam Leffler 	int rc;
1950b032f27cSSam Leffler 
1951b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
1952b032f27cSSam Leffler 	rc = ieee80211_new_state_locked(vap, nstate, arg);
1953b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
1954b032f27cSSam Leffler 	return rc;
19551a1e1d21SSam Leffler }
1956