ieee80211_proto.c revision 218916
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_proto.c 218916 2011-02-21 15:49:59Z adrian $");
29116742Ssam
30116742Ssam/*
31116742Ssam * IEEE 802.11 protocol support.
32116742Ssam */
33116742Ssam
34116742Ssam#include "opt_inet.h"
35178354Ssam#include "opt_wlan.h"
36116742Ssam
37116742Ssam#include <sys/param.h>
38138568Ssam#include <sys/kernel.h>
39170530Ssam#include <sys/systm.h>
40170530Ssam
41116742Ssam#include <sys/socket.h>
42178354Ssam#include <sys/sockio.h>
43116742Ssam
44116742Ssam#include <net/if.h>
45116742Ssam#include <net/if_media.h>
46138568Ssam#include <net/ethernet.h>		/* XXX for ether_sprintf */
47116742Ssam
48116742Ssam#include <net80211/ieee80211_var.h>
49178354Ssam#include <net80211/ieee80211_adhoc.h>
50178354Ssam#include <net80211/ieee80211_sta.h>
51178354Ssam#include <net80211/ieee80211_hostap.h>
52178354Ssam#include <net80211/ieee80211_wds.h>
53195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
54195618Srpaulo#include <net80211/ieee80211_mesh.h>
55195618Srpaulo#endif
56178354Ssam#include <net80211/ieee80211_monitor.h>
57178354Ssam#include <net80211/ieee80211_input.h>
58116742Ssam
59138568Ssam/* XXX tunables */
60138568Ssam#define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
61138568Ssam#define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
62116742Ssam
63116742Ssamconst char *ieee80211_mgt_subtype_name[] = {
64116742Ssam	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
65116742Ssam	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
66116742Ssam	"beacon",	"atim",		"disassoc",	"auth",
67172230Ssam	"deauth",	"action",	"reserved#14",	"reserved#15"
68116742Ssam};
69138568Ssamconst char *ieee80211_ctl_subtype_name[] = {
70138568Ssam	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
71138568Ssam	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
72138568Ssam	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
73138568Ssam	"cts",		"ack",		"cf_end",	"cf_end_ack"
74138568Ssam};
75167283Ssamconst char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
76167283Ssam	"IBSS",		/* IEEE80211_M_IBSS */
77167283Ssam	"STA",		/* IEEE80211_M_STA */
78178354Ssam	"WDS",		/* IEEE80211_M_WDS */
79167283Ssam	"AHDEMO",	/* IEEE80211_M_AHDEMO */
80167283Ssam	"HOSTAP",	/* IEEE80211_M_HOSTAP */
81195618Srpaulo	"MONITOR",	/* IEEE80211_M_MONITOR */
82195618Srpaulo	"MBSS"		/* IEEE80211_M_MBSS */
83167283Ssam};
84117811Ssamconst char *ieee80211_state_name[IEEE80211_S_MAX] = {
85117811Ssam	"INIT",		/* IEEE80211_S_INIT */
86117811Ssam	"SCAN",		/* IEEE80211_S_SCAN */
87117811Ssam	"AUTH",		/* IEEE80211_S_AUTH */
88117811Ssam	"ASSOC",	/* IEEE80211_S_ASSOC */
89172058Ssam	"CAC",		/* IEEE80211_S_CAC */
90172058Ssam	"RUN",		/* IEEE80211_S_RUN */
91172058Ssam	"CSA",		/* IEEE80211_S_CSA */
92172058Ssam	"SLEEP",	/* IEEE80211_S_SLEEP */
93117811Ssam};
94138568Ssamconst char *ieee80211_wme_acnames[] = {
95138568Ssam	"WME_AC_BE",
96138568Ssam	"WME_AC_BK",
97138568Ssam	"WME_AC_VI",
98138568Ssam	"WME_AC_VO",
99138568Ssam	"WME_UPSD",
100138568Ssam};
101116742Ssam
102191746Sthompsastatic void beacon_miss(void *, int);
103191746Sthompsastatic void beacon_swmiss(void *, int);
104178354Ssamstatic void parent_updown(void *, int);
105191746Sthompsastatic void update_mcast(void *, int);
106191746Sthompsastatic void update_promisc(void *, int);
107191746Sthompsastatic void update_channel(void *, int);
108191746Sthompsastatic void ieee80211_newstate_cb(void *, int);
109178354Ssamstatic int ieee80211_new_state_locked(struct ieee80211vap *,
110178354Ssam	enum ieee80211_state, int);
111117811Ssam
112178354Ssamstatic int
113178354Ssamnull_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
114178354Ssam	const struct ieee80211_bpf_params *params)
115172211Ssam{
116178354Ssam	struct ifnet *ifp = ni->ni_ic->ic_ifp;
117178354Ssam
118178354Ssam	if_printf(ifp, "missing ic_raw_xmit callback, drop frame\n");
119178354Ssam	m_freem(m);
120178354Ssam	return ENETDOWN;
121172211Ssam}
122172211Ssam
123116742Ssamvoid
124138568Ssamieee80211_proto_attach(struct ieee80211com *ic)
125116742Ssam{
126138568Ssam	struct ifnet *ifp = ic->ic_ifp;
127116742Ssam
128178354Ssam	/* override the 802.3 setting */
129178354Ssam	ifp->if_hdrlen = ic->ic_headroom
130178354Ssam		+ sizeof(struct ieee80211_qosframe_addr4)
131178354Ssam		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
132178354Ssam		+ IEEE80211_WEP_EXTIVLEN;
133178354Ssam	/* XXX no way to recalculate on ifdetach */
134178354Ssam	if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
135178354Ssam		/* XXX sanity check... */
136178354Ssam		max_linkhdr = ALIGN(ifp->if_hdrlen);
137178354Ssam		max_hdr = max_linkhdr + max_protohdr;
138178354Ssam		max_datalen = MHLEN - max_hdr;
139178354Ssam	}
140127648Ssam	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
141116742Ssam
142178354Ssam	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ifp);
143191746Sthompsa	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
144191746Sthompsa	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
145191746Sthompsa	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
146191746Sthompsa	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
147178354Ssam
148138568Ssam	ic->ic_wme.wme_hipri_switch_hysteresis =
149138568Ssam		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
150138568Ssam
151116742Ssam	/* initialize management frame handlers */
152116742Ssam	ic->ic_send_mgmt = ieee80211_send_mgmt;
153178354Ssam	ic->ic_raw_xmit = null_raw_xmit;
154178354Ssam
155178354Ssam	ieee80211_adhoc_attach(ic);
156178354Ssam	ieee80211_sta_attach(ic);
157178354Ssam	ieee80211_wds_attach(ic);
158178354Ssam	ieee80211_hostap_attach(ic);
159195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
160195618Srpaulo	ieee80211_mesh_attach(ic);
161195618Srpaulo#endif
162178354Ssam	ieee80211_monitor_attach(ic);
163116742Ssam}
164116742Ssam
165116742Ssamvoid
166138568Ssamieee80211_proto_detach(struct ieee80211com *ic)
167116742Ssam{
168178354Ssam	ieee80211_monitor_detach(ic);
169195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
170195618Srpaulo	ieee80211_mesh_detach(ic);
171195618Srpaulo#endif
172178354Ssam	ieee80211_hostap_detach(ic);
173178354Ssam	ieee80211_wds_detach(ic);
174178354Ssam	ieee80211_adhoc_detach(ic);
175178354Ssam	ieee80211_sta_detach(ic);
176178354Ssam}
177116742Ssam
178178354Ssamstatic void
179178354Ssamnull_update_beacon(struct ieee80211vap *vap, int item)
180178354Ssam{
181178354Ssam}
182178354Ssam
183178354Ssamvoid
184178354Ssamieee80211_proto_vattach(struct ieee80211vap *vap)
185178354Ssam{
186178354Ssam	struct ieee80211com *ic = vap->iv_ic;
187178354Ssam	struct ifnet *ifp = vap->iv_ifp;
188178354Ssam	int i;
189178354Ssam
190178354Ssam	/* override the 802.3 setting */
191178354Ssam	ifp->if_hdrlen = ic->ic_ifp->if_hdrlen;
192178354Ssam
193178354Ssam	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
194178354Ssam	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
195178354Ssam	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
196178354Ssam	callout_init(&vap->iv_swbmiss, CALLOUT_MPSAFE);
197178354Ssam	callout_init(&vap->iv_mgtsend, CALLOUT_MPSAFE);
198191746Sthompsa	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
199191746Sthompsa	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
200138568Ssam	/*
201178354Ssam	 * Install default tx rate handling: no fixed rate, lowest
202178354Ssam	 * supported rate for mgmt and multicast frames.  Default
203178354Ssam	 * max retry count.  These settings can be changed by the
204178354Ssam	 * driver and/or user applications.
205178354Ssam	 */
206188779Ssam	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
207178354Ssam		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
208178354Ssam
209178354Ssam		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
210218916Sadrian
211218916Sadrian		/*
212218916Sadrian		 * Setting the management rate to MCS 0 assumes that the
213218916Sadrian		 * BSS Basic rate set is empty and the BSS Basic MCS set
214218916Sadrian		 * is not.
215218916Sadrian		 *
216218916Sadrian		 * Since we're not checking this, default to the lowest
217218916Sadrian		 * defined rate for this mode.
218218916Sadrian		 *
219218916Sadrian		 * At least one 11n AP (DLINK DIR-825) is reported to drop
220218916Sadrian		 * some MCS management traffic (eg BA response frames.)
221218916Sadrian		 *
222218916Sadrian		 * See also: 9.6.0 of the 802.11n-2009 specification.
223218916Sadrian		 */
224218916Sadrian#ifdef	NOTYET
225188779Ssam		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
226188779Ssam			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
227188779Ssam			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
228188779Ssam		} else {
229188779Ssam			vap->iv_txparms[i].mgmtrate =
230188779Ssam			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
231188779Ssam			vap->iv_txparms[i].mcastrate =
232188779Ssam			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
233188779Ssam		}
234218916Sadrian#endif
235218916Sadrian		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
236218916Sadrian		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
237178354Ssam		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
238178354Ssam	}
239178354Ssam	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
240178354Ssam
241178354Ssam	vap->iv_update_beacon = null_update_beacon;
242178354Ssam	vap->iv_deliver_data = ieee80211_deliver_data;
243178354Ssam
244178354Ssam	/* attach support for operating mode */
245178354Ssam	ic->ic_vattach[vap->iv_opmode](vap);
246178354Ssam}
247178354Ssam
248178354Ssamvoid
249178354Ssamieee80211_proto_vdetach(struct ieee80211vap *vap)
250178354Ssam{
251178354Ssam#define	FREEAPPIE(ie) do { \
252178354Ssam	if (ie != NULL) \
253186302Ssam		free(ie, M_80211_NODE_IE); \
254178354Ssam} while (0)
255178354Ssam	/*
256178354Ssam	 * Detach operating mode module.
257178354Ssam	 */
258178354Ssam	if (vap->iv_opdetach != NULL)
259178354Ssam		vap->iv_opdetach(vap);
260178354Ssam	/*
261138568Ssam	 * This should not be needed as we detach when reseting
262138568Ssam	 * the state but be conservative here since the
263138568Ssam	 * authenticator may do things like spawn kernel threads.
264138568Ssam	 */
265178354Ssam	if (vap->iv_auth->ia_detach != NULL)
266178354Ssam		vap->iv_auth->ia_detach(vap);
267138568Ssam	/*
268138568Ssam	 * Detach any ACL'ator.
269138568Ssam	 */
270178354Ssam	if (vap->iv_acl != NULL)
271178354Ssam		vap->iv_acl->iac_detach(vap);
272178354Ssam
273178354Ssam	FREEAPPIE(vap->iv_appie_beacon);
274178354Ssam	FREEAPPIE(vap->iv_appie_probereq);
275178354Ssam	FREEAPPIE(vap->iv_appie_proberesp);
276178354Ssam	FREEAPPIE(vap->iv_appie_assocreq);
277178354Ssam	FREEAPPIE(vap->iv_appie_assocresp);
278178354Ssam	FREEAPPIE(vap->iv_appie_wpa);
279178354Ssam#undef FREEAPPIE
280116742Ssam}
281116742Ssam
282138568Ssam/*
283138568Ssam * Simple-minded authenticator module support.
284138568Ssam */
285138568Ssam
286138568Ssam#define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
287138568Ssam/* XXX well-known names */
288138568Ssamstatic const char *auth_modnames[IEEE80211_AUTH_MAX] = {
289138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_NONE */
290138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
291138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
292138568Ssam	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
293138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
294138568Ssam	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
295138568Ssam};
296138568Ssamstatic const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
297138568Ssam
298138568Ssamstatic const struct ieee80211_authenticator auth_internal = {
299138568Ssam	.ia_name		= "wlan_internal",
300138568Ssam	.ia_attach		= NULL,
301138568Ssam	.ia_detach		= NULL,
302138568Ssam	.ia_node_join		= NULL,
303138568Ssam	.ia_node_leave		= NULL,
304138568Ssam};
305138568Ssam
306138568Ssam/*
307138568Ssam * Setup internal authenticators once; they are never unregistered.
308138568Ssam */
309138568Ssamstatic void
310138568Ssamieee80211_auth_setup(void)
311138568Ssam{
312138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
313138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
314138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
315138568Ssam}
316138568SsamSYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
317138568Ssam
318138568Ssamconst struct ieee80211_authenticator *
319138568Ssamieee80211_authenticator_get(int auth)
320138568Ssam{
321138568Ssam	if (auth >= IEEE80211_AUTH_MAX)
322138568Ssam		return NULL;
323138568Ssam	if (authenticators[auth] == NULL)
324138568Ssam		ieee80211_load_module(auth_modnames[auth]);
325138568Ssam	return authenticators[auth];
326138568Ssam}
327138568Ssam
328116742Ssamvoid
329138568Ssamieee80211_authenticator_register(int type,
330138568Ssam	const struct ieee80211_authenticator *auth)
331116742Ssam{
332138568Ssam	if (type >= IEEE80211_AUTH_MAX)
333138568Ssam		return;
334138568Ssam	authenticators[type] = auth;
335138568Ssam}
336138568Ssam
337138568Ssamvoid
338138568Ssamieee80211_authenticator_unregister(int type)
339138568Ssam{
340138568Ssam
341138568Ssam	if (type >= IEEE80211_AUTH_MAX)
342138568Ssam		return;
343138568Ssam	authenticators[type] = NULL;
344138568Ssam}
345138568Ssam
346138568Ssam/*
347138568Ssam * Very simple-minded ACL module support.
348138568Ssam */
349138568Ssam/* XXX just one for now */
350138568Ssamstatic	const struct ieee80211_aclator *acl = NULL;
351138568Ssam
352138568Ssamvoid
353138568Ssamieee80211_aclator_register(const struct ieee80211_aclator *iac)
354138568Ssam{
355138568Ssam	printf("wlan: %s acl policy registered\n", iac->iac_name);
356138568Ssam	acl = iac;
357138568Ssam}
358138568Ssam
359138568Ssamvoid
360138568Ssamieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
361138568Ssam{
362138568Ssam	if (acl == iac)
363138568Ssam		acl = NULL;
364138568Ssam	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
365138568Ssam}
366138568Ssam
367138568Ssamconst struct ieee80211_aclator *
368138568Ssamieee80211_aclator_get(const char *name)
369138568Ssam{
370138568Ssam	if (acl == NULL)
371138568Ssam		ieee80211_load_module("wlan_acl");
372138568Ssam	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
373138568Ssam}
374138568Ssam
375138568Ssamvoid
376170530Ssamieee80211_print_essid(const uint8_t *essid, int len)
377138568Ssam{
378170530Ssam	const uint8_t *p;
379116742Ssam	int i;
380116742Ssam
381116742Ssam	if (len > IEEE80211_NWID_LEN)
382116742Ssam		len = IEEE80211_NWID_LEN;
383116742Ssam	/* determine printable or not */
384116742Ssam	for (i = 0, p = essid; i < len; i++, p++) {
385116742Ssam		if (*p < ' ' || *p > 0x7e)
386116742Ssam			break;
387116742Ssam	}
388116742Ssam	if (i == len) {
389116742Ssam		printf("\"");
390116742Ssam		for (i = 0, p = essid; i < len; i++, p++)
391116742Ssam			printf("%c", *p);
392116742Ssam		printf("\"");
393116742Ssam	} else {
394116742Ssam		printf("0x");
395116742Ssam		for (i = 0, p = essid; i < len; i++, p++)
396116742Ssam			printf("%02x", *p);
397116742Ssam	}
398116742Ssam}
399116742Ssam
400116742Ssamvoid
401170530Ssamieee80211_dump_pkt(struct ieee80211com *ic,
402170530Ssam	const uint8_t *buf, int len, int rate, int rssi)
403116742Ssam{
404138568Ssam	const struct ieee80211_frame *wh;
405116742Ssam	int i;
406116742Ssam
407138568Ssam	wh = (const struct ieee80211_frame *)buf;
408116742Ssam	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
409116742Ssam	case IEEE80211_FC1_DIR_NODS:
410116742Ssam		printf("NODS %s", ether_sprintf(wh->i_addr2));
411116742Ssam		printf("->%s", ether_sprintf(wh->i_addr1));
412116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr3));
413116742Ssam		break;
414116742Ssam	case IEEE80211_FC1_DIR_TODS:
415116742Ssam		printf("TODS %s", ether_sprintf(wh->i_addr2));
416116742Ssam		printf("->%s", ether_sprintf(wh->i_addr3));
417116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr1));
418116742Ssam		break;
419116742Ssam	case IEEE80211_FC1_DIR_FROMDS:
420116742Ssam		printf("FRDS %s", ether_sprintf(wh->i_addr3));
421116742Ssam		printf("->%s", ether_sprintf(wh->i_addr1));
422116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr2));
423116742Ssam		break;
424116742Ssam	case IEEE80211_FC1_DIR_DSTODS:
425170530Ssam		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
426116742Ssam		printf("->%s", ether_sprintf(wh->i_addr3));
427116742Ssam		printf("(%s", ether_sprintf(wh->i_addr2));
428116742Ssam		printf("->%s)", ether_sprintf(wh->i_addr1));
429116742Ssam		break;
430116742Ssam	}
431116742Ssam	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
432116742Ssam	case IEEE80211_FC0_TYPE_DATA:
433116742Ssam		printf(" data");
434116742Ssam		break;
435116742Ssam	case IEEE80211_FC0_TYPE_MGT:
436116742Ssam		printf(" %s", ieee80211_mgt_subtype_name[
437116742Ssam		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
438116742Ssam		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
439116742Ssam		break;
440116742Ssam	default:
441116742Ssam		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
442116742Ssam		break;
443116742Ssam	}
444170530Ssam	if (IEEE80211_QOS_HAS_SEQ(wh)) {
445170530Ssam		const struct ieee80211_qosframe *qwh =
446170530Ssam			(const struct ieee80211_qosframe *)buf;
447170530Ssam		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
448170530Ssam			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
449170530Ssam	}
450138568Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
451170530Ssam		int off;
452170530Ssam
453170530Ssam		off = ieee80211_anyhdrspace(ic, wh);
454170530Ssam		printf(" WEP [IV %.02x %.02x %.02x",
455170530Ssam			buf[off+0], buf[off+1], buf[off+2]);
456170530Ssam		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
457170530Ssam			printf(" %.02x %.02x %.02x",
458170530Ssam				buf[off+4], buf[off+5], buf[off+6]);
459170530Ssam		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
460138568Ssam	}
461116742Ssam	if (rate >= 0)
462116742Ssam		printf(" %dM", rate / 2);
463116742Ssam	if (rssi >= 0)
464116742Ssam		printf(" +%d", rssi);
465116742Ssam	printf("\n");
466116742Ssam	if (len > 0) {
467116742Ssam		for (i = 0; i < len; i++) {
468116742Ssam			if ((i & 1) == 0)
469116742Ssam				printf(" ");
470116742Ssam			printf("%02x", buf[i]);
471116742Ssam		}
472116742Ssam		printf("\n");
473116742Ssam	}
474116742Ssam}
475116742Ssam
476165887Ssamstatic __inline int
477165887Ssamfindrix(const struct ieee80211_rateset *rs, int r)
478165887Ssam{
479165887Ssam	int i;
480165887Ssam
481165887Ssam	for (i = 0; i < rs->rs_nrates; i++)
482165887Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
483165887Ssam			return i;
484165887Ssam	return -1;
485165887Ssam}
486165887Ssam
487116742Ssamint
488167442Ssamieee80211_fix_rate(struct ieee80211_node *ni,
489167442Ssam	struct ieee80211_rateset *nrs, int flags)
490116742Ssam{
491116742Ssam#define	RV(v)	((v) & IEEE80211_RATE_VAL)
492178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
493148299Ssam	struct ieee80211com *ic = ni->ni_ic;
494165887Ssam	int i, j, rix, error;
495178354Ssam	int okrate, badrate, fixedrate, ucastrate;
496165569Ssam	const struct ieee80211_rateset *srs;
497170530Ssam	uint8_t r;
498116742Ssam
499116742Ssam	error = 0;
500170530Ssam	okrate = badrate = 0;
501178354Ssam	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
502178354Ssam	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
503178354Ssam		/*
504178354Ssam		 * Workaround awkwardness with fixed rate.  We are called
505178354Ssam		 * to check both the legacy rate set and the HT rate set
506178354Ssam		 * but we must apply any legacy fixed rate check only to the
507178354Ssam		 * legacy rate set and vice versa.  We cannot tell what type
508178354Ssam		 * of rate set we've been given (legacy or HT) but we can
509178354Ssam		 * distinguish the fixed rate type (MCS have 0x80 set).
510178354Ssam		 * So to deal with this the caller communicates whether to
511178354Ssam		 * check MCS or legacy rate using the flags and we use the
512178354Ssam		 * type of any fixed rate to avoid applying an MCS to a
513178354Ssam		 * legacy rate and vice versa.
514178354Ssam		 */
515178354Ssam		if (ucastrate & 0x80) {
516178354Ssam			if (flags & IEEE80211_F_DOFRATE)
517178354Ssam				flags &= ~IEEE80211_F_DOFRATE;
518178354Ssam		} else if ((ucastrate & 0x80) == 0) {
519178354Ssam			if (flags & IEEE80211_F_DOFMCS)
520178354Ssam				flags &= ~IEEE80211_F_DOFMCS;
521178354Ssam		}
522178354Ssam		/* NB: required to make MCS match below work */
523178354Ssam		ucastrate &= IEEE80211_RATE_VAL;
524178354Ssam	}
525170530Ssam	fixedrate = IEEE80211_FIXED_RATE_NONE;
526178354Ssam	/*
527178354Ssam	 * XXX we are called to process both MCS and legacy rates;
528178354Ssam	 * we must use the appropriate basic rate set or chaos will
529178354Ssam	 * ensue; for now callers that want MCS must supply
530178354Ssam	 * IEEE80211_F_DOBRS; at some point we'll need to split this
531178354Ssam	 * function so there are two variants, one for MCS and one
532178354Ssam	 * for legacy rates.
533178354Ssam	 */
534178354Ssam	if (flags & IEEE80211_F_DOBRS)
535178354Ssam		srs = (const struct ieee80211_rateset *)
536178354Ssam		    ieee80211_get_suphtrates(ic, ni->ni_chan);
537178354Ssam	else
538178354Ssam		srs = ieee80211_get_suprates(ic, ni->ni_chan);
539120482Ssam	for (i = 0; i < nrs->rs_nrates; ) {
540116742Ssam		if (flags & IEEE80211_F_DOSORT) {
541116742Ssam			/*
542116742Ssam			 * Sort rates.
543116742Ssam			 */
544116742Ssam			for (j = i + 1; j < nrs->rs_nrates; j++) {
545116742Ssam				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
546116742Ssam					r = nrs->rs_rates[i];
547116742Ssam					nrs->rs_rates[i] = nrs->rs_rates[j];
548116742Ssam					nrs->rs_rates[j] = r;
549116742Ssam				}
550116742Ssam			}
551116742Ssam		}
552116742Ssam		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
553116742Ssam		badrate = r;
554165887Ssam		/*
555170530Ssam		 * Check for fixed rate.
556170530Ssam		 */
557178354Ssam		if (r == ucastrate)
558170530Ssam			fixedrate = r;
559170530Ssam		/*
560165887Ssam		 * Check against supported rates.
561165887Ssam		 */
562165887Ssam		rix = findrix(srs, r);
563116742Ssam		if (flags & IEEE80211_F_DONEGO) {
564165887Ssam			if (rix < 0) {
565120482Ssam				/*
566120482Ssam				 * A rate in the node's rate set is not
567120482Ssam				 * supported.  If this is a basic rate and we
568165887Ssam				 * are operating as a STA then this is an error.
569120482Ssam				 * Otherwise we just discard/ignore the rate.
570120482Ssam				 */
571165887Ssam				if ((flags & IEEE80211_F_JOIN) &&
572120482Ssam				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
573116742Ssam					error++;
574165887Ssam			} else if ((flags & IEEE80211_F_JOIN) == 0) {
575165887Ssam				/*
576165887Ssam				 * Overwrite with the supported rate
577165887Ssam				 * value so any basic rate bit is set.
578165887Ssam				 */
579165887Ssam				nrs->rs_rates[i] = srs->rs_rates[rix];
580116742Ssam			}
581116742Ssam		}
582165887Ssam		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
583116742Ssam			/*
584116742Ssam			 * Delete unacceptable rates.
585116742Ssam			 */
586165887Ssam			nrs->rs_nrates--;
587165887Ssam			for (j = i; j < nrs->rs_nrates; j++)
588165887Ssam				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
589165887Ssam			nrs->rs_rates[j] = 0;
590165887Ssam			continue;
591116742Ssam		}
592165887Ssam		if (rix >= 0)
593116742Ssam			okrate = nrs->rs_rates[i];
594116742Ssam		i++;
595116742Ssam	}
596138568Ssam	if (okrate == 0 || error != 0 ||
597178354Ssam	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
598178354Ssam	     fixedrate != ucastrate)) {
599178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
600178354Ssam		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
601178354Ssam		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
602116742Ssam		return badrate | IEEE80211_RATE_BASIC;
603178354Ssam	} else
604116742Ssam		return RV(okrate);
605116742Ssam#undef RV
606116742Ssam}
607116742Ssam
608138568Ssam/*
609138568Ssam * Reset 11g-related state.
610138568Ssam */
611138568Ssamvoid
612138568Ssamieee80211_reset_erp(struct ieee80211com *ic)
613138568Ssam{
614138568Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
615138568Ssam	ic->ic_nonerpsta = 0;
616138568Ssam	ic->ic_longslotsta = 0;
617138568Ssam	/*
618138568Ssam	 * Short slot time is enabled only when operating in 11g
619138568Ssam	 * and not in an IBSS.  We must also honor whether or not
620138568Ssam	 * the driver is capable of doing it.
621138568Ssam	 */
622138568Ssam	ieee80211_set_shortslottime(ic,
623170530Ssam		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
624170530Ssam		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
625170530Ssam		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
626138568Ssam		ic->ic_opmode == IEEE80211_M_HOSTAP &&
627138568Ssam		(ic->ic_caps & IEEE80211_C_SHSLOT)));
628138568Ssam	/*
629138568Ssam	 * Set short preamble and ERP barker-preamble flags.
630138568Ssam	 */
631170530Ssam	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
632138568Ssam	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
633138568Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
634138568Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
635138568Ssam	} else {
636138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
637138568Ssam		ic->ic_flags |= IEEE80211_F_USEBARKER;
638138568Ssam	}
639138568Ssam}
640138568Ssam
641138568Ssam/*
642138568Ssam * Set the short slot time state and notify the driver.
643138568Ssam */
644138568Ssamvoid
645138568Ssamieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
646138568Ssam{
647138568Ssam	if (onoff)
648138568Ssam		ic->ic_flags |= IEEE80211_F_SHSLOT;
649138568Ssam	else
650138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
651138568Ssam	/* notify driver */
652138568Ssam	if (ic->ic_updateslot != NULL)
653138568Ssam		ic->ic_updateslot(ic->ic_ifp);
654138568Ssam}
655138568Ssam
656138568Ssam/*
657138568Ssam * Check if the specified rate set supports ERP.
658138568Ssam * NB: the rate set is assumed to be sorted.
659138568Ssam */
660138568Ssamint
661178354Ssamieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
662138568Ssam{
663138568Ssam#define N(a)	(sizeof(a) / sizeof(a[0]))
664138568Ssam	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
665138568Ssam	int i, j;
666138568Ssam
667138568Ssam	if (rs->rs_nrates < N(rates))
668138568Ssam		return 0;
669138568Ssam	for (i = 0; i < N(rates); i++) {
670138568Ssam		for (j = 0; j < rs->rs_nrates; j++) {
671138568Ssam			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
672138568Ssam			if (rates[i] == r)
673138568Ssam				goto next;
674138568Ssam			if (r > rates[i])
675138568Ssam				return 0;
676138568Ssam		}
677138568Ssam		return 0;
678138568Ssam	next:
679138568Ssam		;
680138568Ssam	}
681138568Ssam	return 1;
682138568Ssam#undef N
683138568Ssam}
684138568Ssam
685138568Ssam/*
686178354Ssam * Mark the basic rates for the rate table based on the
687138568Ssam * operating mode.  For real 11g we mark all the 11b rates
688138568Ssam * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
689138568Ssam * 11b rates.  There's also a pseudo 11a-mode used to mark only
690138568Ssam * the basic OFDM rates.
691138568Ssam */
692178354Ssamstatic void
693178354Ssamsetbasicrates(struct ieee80211_rateset *rs,
694178354Ssam    enum ieee80211_phymode mode, int add)
695138568Ssam{
696170530Ssam	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
697188780Ssam	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
698188780Ssam	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
699188780Ssam					    /* NB: mixed b/g */
700188780Ssam	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
701188780Ssam	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
702188780Ssam	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
703188780Ssam	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
704188782Ssam	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
705188782Ssam	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
706188780Ssam	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
707188780Ssam					    /* NB: mixed b/g */
708188780Ssam	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
709138568Ssam	};
710138568Ssam	int i, j;
711138568Ssam
712138568Ssam	for (i = 0; i < rs->rs_nrates; i++) {
713178354Ssam		if (!add)
714178354Ssam			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
715138568Ssam		for (j = 0; j < basic[mode].rs_nrates; j++)
716138568Ssam			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
717138568Ssam				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
718138568Ssam				break;
719138568Ssam			}
720138568Ssam	}
721138568Ssam}
722138568Ssam
723138568Ssam/*
724178354Ssam * Set the basic rates in a rate set.
725138568Ssam */
726178354Ssamvoid
727178354Ssamieee80211_setbasicrates(struct ieee80211_rateset *rs,
728178354Ssam    enum ieee80211_phymode mode)
729178354Ssam{
730178354Ssam	setbasicrates(rs, mode, 0);
731178354Ssam}
732178354Ssam
733178354Ssam/*
734178354Ssam * Add basic rates to a rate set.
735178354Ssam */
736178354Ssamvoid
737178354Ssamieee80211_addbasicrates(struct ieee80211_rateset *rs,
738178354Ssam    enum ieee80211_phymode mode)
739178354Ssam{
740178354Ssam	setbasicrates(rs, mode, 1);
741178354Ssam}
742178354Ssam
743178354Ssam/*
744178354Ssam * WME protocol support.
745178354Ssam *
746178354Ssam * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
747178354Ssam * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
748178354Ssam * Draft 2.0 Test Plan (Appendix D).
749178354Ssam *
750178354Ssam * Static/Dynamic Turbo mode settings come from Atheros.
751178354Ssam */
752138568Ssamtypedef struct phyParamType {
753178354Ssam	uint8_t		aifsn;
754178354Ssam	uint8_t		logcwmin;
755178354Ssam	uint8_t		logcwmax;
756178354Ssam	uint16_t	txopLimit;
757178354Ssam	uint8_t 	acm;
758138568Ssam} paramType;
759138568Ssam
760138568Ssamstatic const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
761188780Ssam	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
762188780Ssam	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
763188780Ssam	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
764188780Ssam	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
765188780Ssam	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
766188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
767188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
768188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
769188782Ssam	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
770188782Ssam	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
771188780Ssam	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
772188780Ssam	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
773138568Ssam};
774138568Ssamstatic const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
775188780Ssam	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
776188780Ssam	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
777188780Ssam	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
778188780Ssam	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
779188780Ssam	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
780188780Ssam	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
781188780Ssam	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
782188780Ssam	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
783188782Ssam	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
784188782Ssam	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
785188780Ssam	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
786188780Ssam	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
787138568Ssam};
788138568Ssamstatic const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
789188780Ssam	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
790188780Ssam	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
791188780Ssam	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
792188780Ssam	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
793188780Ssam	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
794188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
795188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
796188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
797188782Ssam	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
798188782Ssam	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
799188780Ssam	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
800188780Ssam	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
801138568Ssam};
802138568Ssamstatic const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
803188780Ssam	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
804188780Ssam	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
805188780Ssam	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
806188780Ssam	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
807188780Ssam	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
808188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
809188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
810188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
811188782Ssam	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
812188782Ssam	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
813188780Ssam	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
814188780Ssam	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
815138568Ssam};
816138568Ssam
817138568Ssamstatic const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
818188780Ssam	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
819188780Ssam	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
820188780Ssam	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
821188780Ssam	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
822188780Ssam	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
823188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
824188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
825188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
826188782Ssam	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
827188782Ssam	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
828188780Ssam	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
829188780Ssam	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
830138568Ssam};
831138568Ssamstatic const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
832188780Ssam	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
833188780Ssam	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
834188780Ssam	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
835188780Ssam	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
836188780Ssam	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
837188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
838188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
839188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
840188782Ssam	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
841188782Ssam	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
842188780Ssam	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
843188780Ssam	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
844138568Ssam};
845138568Ssamstatic const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
846188780Ssam	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
847188780Ssam	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
848188780Ssam	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
849188780Ssam	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
850188780Ssam	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
851188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
852188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
853188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
854188782Ssam	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
855188782Ssam	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
856188780Ssam	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
857188780Ssam	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
858138568Ssam};
859138568Ssam
860178354Ssamstatic void
861188863Ssam_setifsparams(struct wmeParams *wmep, const paramType *phy)
862188863Ssam{
863188863Ssam	wmep->wmep_aifsn = phy->aifsn;
864188863Ssam	wmep->wmep_logcwmin = phy->logcwmin;
865188863Ssam	wmep->wmep_logcwmax = phy->logcwmax;
866188863Ssam	wmep->wmep_txopLimit = phy->txopLimit;
867188863Ssam}
868188863Ssam
869188863Ssamstatic void
870188863Ssamsetwmeparams(struct ieee80211vap *vap, const char *type, int ac,
871188863Ssam	struct wmeParams *wmep, const paramType *phy)
872188863Ssam{
873188863Ssam	wmep->wmep_acm = phy->acm;
874188863Ssam	_setifsparams(wmep, phy);
875188863Ssam
876188863Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
877188863Ssam	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
878188863Ssam	    ieee80211_wme_acnames[ac], type,
879188863Ssam	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
880188863Ssam	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
881188863Ssam}
882188863Ssam
883188863Ssamstatic void
884178354Ssamieee80211_wme_initparams_locked(struct ieee80211vap *vap)
885138568Ssam{
886178354Ssam	struct ieee80211com *ic = vap->iv_ic;
887138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
888138568Ssam	const paramType *pPhyParam, *pBssPhyParam;
889138568Ssam	struct wmeParams *wmep;
890170530Ssam	enum ieee80211_phymode mode;
891138568Ssam	int i;
892138568Ssam
893178354Ssam	IEEE80211_LOCK_ASSERT(ic);
894178354Ssam
895188864Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
896138568Ssam		return;
897138568Ssam
898170530Ssam	/*
899170530Ssam	 * Select mode; we can be called early in which case we
900170530Ssam	 * always use auto mode.  We know we'll be called when
901170530Ssam	 * entering the RUN state with bsschan setup properly
902170530Ssam	 * so state will eventually get set correctly
903170530Ssam	 */
904170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
905170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
906170530Ssam	else
907170530Ssam		mode = IEEE80211_MODE_AUTO;
908138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
909138568Ssam		switch (i) {
910138568Ssam		case WME_AC_BK:
911170530Ssam			pPhyParam = &phyParamForAC_BK[mode];
912170530Ssam			pBssPhyParam = &phyParamForAC_BK[mode];
913138568Ssam			break;
914138568Ssam		case WME_AC_VI:
915170530Ssam			pPhyParam = &phyParamForAC_VI[mode];
916170530Ssam			pBssPhyParam = &bssPhyParamForAC_VI[mode];
917138568Ssam			break;
918138568Ssam		case WME_AC_VO:
919170530Ssam			pPhyParam = &phyParamForAC_VO[mode];
920170530Ssam			pBssPhyParam = &bssPhyParamForAC_VO[mode];
921138568Ssam			break;
922138568Ssam		case WME_AC_BE:
923138568Ssam		default:
924170530Ssam			pPhyParam = &phyParamForAC_BE[mode];
925170530Ssam			pBssPhyParam = &bssPhyParamForAC_BE[mode];
926138568Ssam			break;
927138568Ssam		}
928138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
929138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
930188863Ssam			setwmeparams(vap, "chan", i, wmep, pPhyParam);
931138568Ssam		} else {
932188863Ssam			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
933138568Ssam		}
934138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
935188863Ssam		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
936138568Ssam	}
937138568Ssam	/* NB: check ic_bss to avoid NULL deref on initial attach */
938178354Ssam	if (vap->iv_bss != NULL) {
939138568Ssam		/*
940138568Ssam		 * Calculate agressive mode switching threshold based
941138568Ssam		 * on beacon interval.  This doesn't need locking since
942138568Ssam		 * we're only called before entering the RUN state at
943138568Ssam		 * which point we start sending beacon frames.
944138568Ssam		 */
945138568Ssam		wme->wme_hipri_switch_thresh =
946178354Ssam			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
947188864Ssam		wme->wme_flags &= ~WME_F_AGGRMODE;
948178354Ssam		ieee80211_wme_updateparams(vap);
949138568Ssam	}
950138568Ssam}
951138568Ssam
952178354Ssamvoid
953178354Ssamieee80211_wme_initparams(struct ieee80211vap *vap)
954178354Ssam{
955178354Ssam	struct ieee80211com *ic = vap->iv_ic;
956178354Ssam
957178354Ssam	IEEE80211_LOCK(ic);
958178354Ssam	ieee80211_wme_initparams_locked(vap);
959178354Ssam	IEEE80211_UNLOCK(ic);
960178354Ssam}
961178354Ssam
962138568Ssam/*
963138568Ssam * Update WME parameters for ourself and the BSS.
964138568Ssam */
965138568Ssamvoid
966178354Ssamieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
967138568Ssam{
968188863Ssam	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
969188780Ssam	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
970188780Ssam	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
971188780Ssam	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
972188780Ssam	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
973188780Ssam	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
974188780Ssam	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
975188780Ssam	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
976188780Ssam	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
977188782Ssam	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
978188782Ssam	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
979188780Ssam	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
980188780Ssam	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
981138568Ssam	};
982178354Ssam	struct ieee80211com *ic = vap->iv_ic;
983138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
984138568Ssam	const struct wmeParams *wmep;
985138568Ssam	struct wmeParams *chanp, *bssp;
986170530Ssam	enum ieee80211_phymode mode;
987138568Ssam	int i;
988138568Ssam
989188863Ssam       	/*
990188863Ssam	 * Set up the channel access parameters for the physical
991188863Ssam	 * device.  First populate the configured settings.
992188863Ssam	 */
993138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
994138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[i];
995138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
996138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
997138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
998138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
999138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1000138568Ssam
1001138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1002138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1003138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
1004138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1005138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1006138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1007138568Ssam	}
1008138568Ssam
1009138568Ssam	/*
1010170530Ssam	 * Select mode; we can be called early in which case we
1011170530Ssam	 * always use auto mode.  We know we'll be called when
1012170530Ssam	 * entering the RUN state with bsschan setup properly
1013170530Ssam	 * so state will eventually get set correctly
1014170530Ssam	 */
1015170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1016170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
1017170530Ssam	else
1018170530Ssam		mode = IEEE80211_MODE_AUTO;
1019170530Ssam
1020170530Ssam	/*
1021138568Ssam	 * This implements agressive mode as found in certain
1022138568Ssam	 * vendors' AP's.  When there is significant high
1023138568Ssam	 * priority (VI/VO) traffic in the BSS throttle back BE
1024138568Ssam	 * traffic by using conservative parameters.  Otherwise
1025138568Ssam	 * BE uses agressive params to optimize performance of
1026138568Ssam	 * legacy/non-QoS traffic.
1027138568Ssam	 */
1028178354Ssam        if ((vap->iv_opmode == IEEE80211_M_HOSTAP &&
1029156524Ssam	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
1030178354Ssam	    (vap->iv_opmode == IEEE80211_M_STA &&
1031178354Ssam	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
1032178354Ssam	    (vap->iv_flags & IEEE80211_F_WME) == 0) {
1033138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1034138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1035138568Ssam
1036188863Ssam		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1037138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1038188863Ssam		    aggrParam[mode].logcwmin;
1039138568Ssam		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1040188863Ssam		    aggrParam[mode].logcwmax;
1041138568Ssam		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1042188863Ssam		    (vap->iv_flags & IEEE80211_F_BURST) ?
1043188863Ssam			aggrParam[mode].txopLimit : 0;
1044178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1045188863Ssam		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1046188863Ssam		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1047188863Ssam		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1048188863Ssam		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1049138568Ssam	}
1050138568Ssam
1051178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1052156524Ssam	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1053188780Ssam		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1054188780Ssam		    [IEEE80211_MODE_AUTO]	= 3,
1055188780Ssam		    [IEEE80211_MODE_11A]	= 3,
1056188780Ssam		    [IEEE80211_MODE_11B]	= 4,
1057188780Ssam		    [IEEE80211_MODE_11G]	= 3,
1058188780Ssam		    [IEEE80211_MODE_FH]		= 4,
1059188780Ssam		    [IEEE80211_MODE_TURBO_A]	= 3,
1060188780Ssam		    [IEEE80211_MODE_TURBO_G]	= 3,
1061188780Ssam		    [IEEE80211_MODE_STURBO_A]	= 3,
1062188782Ssam		    [IEEE80211_MODE_HALF]	= 3,
1063188782Ssam		    [IEEE80211_MODE_QUARTER]	= 3,
1064188780Ssam		    [IEEE80211_MODE_11NA]	= 3,
1065188780Ssam		    [IEEE80211_MODE_11NG]	= 3,
1066138568Ssam		};
1067138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1068138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1069138568Ssam
1070170530Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1071178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1072188863Ssam		    "update %s (chan+bss) logcwmin %u\n",
1073188863Ssam		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1074138568Ssam    	}
1075178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
1076138568Ssam		/*
1077138568Ssam		 * Arrange for a beacon update and bump the parameter
1078138568Ssam		 * set number so associated stations load the new values.
1079138568Ssam		 */
1080138568Ssam		wme->wme_bssChanParams.cap_info =
1081138568Ssam			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1082178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1083138568Ssam	}
1084138568Ssam
1085138568Ssam	wme->wme_update(ic);
1086138568Ssam
1087178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1088188863Ssam	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1089188863Ssam	    vap->iv_opmode == IEEE80211_M_STA ?
1090188863Ssam		wme->wme_wmeChanParams.cap_info :
1091188863Ssam		wme->wme_bssChanParams.cap_info);
1092138568Ssam}
1093138568Ssam
1094138568Ssamvoid
1095178354Ssamieee80211_wme_updateparams(struct ieee80211vap *vap)
1096138568Ssam{
1097178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1098138568Ssam
1099138568Ssam	if (ic->ic_caps & IEEE80211_C_WME) {
1100178354Ssam		IEEE80211_LOCK(ic);
1101178354Ssam		ieee80211_wme_updateparams_locked(vap);
1102178354Ssam		IEEE80211_UNLOCK(ic);
1103138568Ssam	}
1104138568Ssam}
1105138568Ssam
1106178354Ssamstatic void
1107178354Ssamparent_updown(void *arg, int npending)
1108178354Ssam{
1109178354Ssam	struct ifnet *parent = arg;
1110178354Ssam
1111178354Ssam	parent->if_ioctl(parent, SIOCSIFFLAGS, NULL);
1112178354Ssam}
1113178354Ssam
1114191746Sthompsastatic void
1115191746Sthompsaupdate_mcast(void *arg, int npending)
1116191746Sthompsa{
1117191746Sthompsa	struct ieee80211com *ic = arg;
1118191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1119191746Sthompsa
1120191746Sthompsa	ic->ic_update_mcast(parent);
1121191746Sthompsa}
1122191746Sthompsa
1123191746Sthompsastatic void
1124191746Sthompsaupdate_promisc(void *arg, int npending)
1125191746Sthompsa{
1126191746Sthompsa	struct ieee80211com *ic = arg;
1127191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1128191746Sthompsa
1129191746Sthompsa	ic->ic_update_promisc(parent);
1130191746Sthompsa}
1131191746Sthompsa
1132191746Sthompsastatic void
1133191746Sthompsaupdate_channel(void *arg, int npending)
1134191746Sthompsa{
1135191746Sthompsa	struct ieee80211com *ic = arg;
1136191746Sthompsa
1137191746Sthompsa	ic->ic_set_channel(ic);
1138192468Ssam	ieee80211_radiotap_chan_change(ic);
1139191746Sthompsa}
1140191746Sthompsa
1141170530Ssam/*
1142188533Sthompsa * Block until the parent is in a known state.  This is
1143188533Sthompsa * used after any operations that dispatch a task (e.g.
1144188533Sthompsa * to auto-configure the parent device up/down).
1145188533Sthompsa */
1146188533Sthompsavoid
1147188533Sthompsaieee80211_waitfor_parent(struct ieee80211com *ic)
1148188533Sthompsa{
1149191746Sthompsa	taskqueue_block(ic->ic_tq);
1150191746Sthompsa	ieee80211_draintask(ic, &ic->ic_parent_task);
1151191746Sthompsa	ieee80211_draintask(ic, &ic->ic_mcast_task);
1152191746Sthompsa	ieee80211_draintask(ic, &ic->ic_promisc_task);
1153191746Sthompsa	ieee80211_draintask(ic, &ic->ic_chan_task);
1154191746Sthompsa	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1155191746Sthompsa	taskqueue_unblock(ic->ic_tq);
1156188533Sthompsa}
1157188533Sthompsa
1158188533Sthompsa/*
1159178354Ssam * Start a vap running.  If this is the first vap to be
1160178354Ssam * set running on the underlying device then we
1161178354Ssam * automatically bring the device up.
1162170530Ssam */
1163178354Ssamvoid
1164178354Ssamieee80211_start_locked(struct ieee80211vap *vap)
1165170530Ssam{
1166178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1167178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1168178354Ssam	struct ifnet *parent = ic->ic_ifp;
1169170530Ssam
1170178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1171178354Ssam
1172178354Ssam	IEEE80211_DPRINTF(vap,
1173170530Ssam		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1174178354Ssam		"start running, %d vaps running\n", ic->ic_nrunning);
1175170530Ssam
1176178354Ssam	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1177178354Ssam		/*
1178178354Ssam		 * Mark us running.  Note that it's ok to do this first;
1179178354Ssam		 * if we need to bring the parent device up we defer that
1180178354Ssam		 * to avoid dropping the com lock.  We expect the device
1181178354Ssam		 * to respond to being marked up by calling back into us
1182178354Ssam		 * through ieee80211_start_all at which point we'll come
1183178354Ssam		 * back in here and complete the work.
1184178354Ssam		 */
1185178354Ssam		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1186178354Ssam		/*
1187178354Ssam		 * We are not running; if this we are the first vap
1188178354Ssam		 * to be brought up auto-up the parent if necessary.
1189178354Ssam		 */
1190178354Ssam		if (ic->ic_nrunning++ == 0 &&
1191178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1192178354Ssam			IEEE80211_DPRINTF(vap,
1193178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1194178354Ssam			    "%s: up parent %s\n", __func__, parent->if_xname);
1195178354Ssam			parent->if_flags |= IFF_UP;
1196191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1197178354Ssam			return;
1198178354Ssam		}
1199178354Ssam	}
1200170530Ssam	/*
1201178354Ssam	 * If the parent is up and running, then kick the
1202178354Ssam	 * 802.11 state machine as appropriate.
1203170530Ssam	 */
1204178354Ssam	if ((parent->if_drv_flags & IFF_DRV_RUNNING) &&
1205178354Ssam	    vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1206178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1207178354Ssam#if 0
1208178354Ssam			/* XXX bypasses scan too easily; disable for now */
1209178354Ssam			/*
1210178354Ssam			 * Try to be intelligent about clocking the state
1211178354Ssam			 * machine.  If we're currently in RUN state then
1212178354Ssam			 * we should be able to apply any new state/parameters
1213178354Ssam			 * simply by re-associating.  Otherwise we need to
1214178354Ssam			 * re-scan to select an appropriate ap.
1215178354Ssam			 */
1216178354Ssam			if (vap->iv_state >= IEEE80211_S_RUN)
1217178354Ssam				ieee80211_new_state_locked(vap,
1218178354Ssam				    IEEE80211_S_ASSOC, 1);
1219178354Ssam			else
1220178354Ssam#endif
1221178354Ssam				ieee80211_new_state_locked(vap,
1222178354Ssam				    IEEE80211_S_SCAN, 0);
1223170530Ssam		} else {
1224170530Ssam			/*
1225178354Ssam			 * For monitor+wds mode there's nothing to do but
1226178354Ssam			 * start running.  Otherwise if this is the first
1227170530Ssam			 * vap to be brought up, start a scan which may be
1228170530Ssam			 * preempted if the station is locked to a particular
1229170530Ssam			 * channel.
1230170530Ssam			 */
1231191746Sthompsa			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1232178354Ssam			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1233178354Ssam			    vap->iv_opmode == IEEE80211_M_WDS)
1234178354Ssam				ieee80211_new_state_locked(vap,
1235178354Ssam				    IEEE80211_S_RUN, -1);
1236178354Ssam			else
1237178354Ssam				ieee80211_new_state_locked(vap,
1238178354Ssam				    IEEE80211_S_SCAN, 0);
1239170530Ssam		}
1240170530Ssam	}
1241170530Ssam}
1242170530Ssam
1243170530Ssam/*
1244178354Ssam * Start a single vap.
1245178354Ssam */
1246178354Ssamvoid
1247178354Ssamieee80211_init(void *arg)
1248178354Ssam{
1249178354Ssam	struct ieee80211vap *vap = arg;
1250178354Ssam
1251193348Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1252193348Ssam	    "%s\n", __func__);
1253178354Ssam
1254193348Ssam	IEEE80211_LOCK(vap->iv_ic);
1255193348Ssam	ieee80211_start_locked(vap);
1256193348Ssam	IEEE80211_UNLOCK(vap->iv_ic);
1257178354Ssam}
1258178354Ssam
1259178354Ssam/*
1260178354Ssam * Start all runnable vap's on a device.
1261178354Ssam */
1262178354Ssamvoid
1263178354Ssamieee80211_start_all(struct ieee80211com *ic)
1264178354Ssam{
1265178354Ssam	struct ieee80211vap *vap;
1266178354Ssam
1267178354Ssam	IEEE80211_LOCK(ic);
1268178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1269178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1270178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1271178354Ssam			ieee80211_start_locked(vap);
1272178354Ssam	}
1273178354Ssam	IEEE80211_UNLOCK(ic);
1274178354Ssam}
1275178354Ssam
1276178354Ssam/*
1277178354Ssam * Stop a vap.  We force it down using the state machine
1278178354Ssam * then mark it's ifnet not running.  If this is the last
1279178354Ssam * vap running on the underlying device then we close it
1280178354Ssam * too to insure it will be properly initialized when the
1281178354Ssam * next vap is brought up.
1282178354Ssam */
1283178354Ssamvoid
1284178354Ssamieee80211_stop_locked(struct ieee80211vap *vap)
1285178354Ssam{
1286178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1287178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1288178354Ssam	struct ifnet *parent = ic->ic_ifp;
1289178354Ssam
1290178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1291178354Ssam
1292178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1293178354Ssam	    "stop running, %d vaps running\n", ic->ic_nrunning);
1294178354Ssam
1295178354Ssam	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1296178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1297178354Ssam		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1298178354Ssam		if (--ic->ic_nrunning == 0 &&
1299178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING)) {
1300178354Ssam			IEEE80211_DPRINTF(vap,
1301178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1302178354Ssam			    "down parent %s\n", parent->if_xname);
1303178354Ssam			parent->if_flags &= ~IFF_UP;
1304191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1305178354Ssam		}
1306178354Ssam	}
1307178354Ssam}
1308178354Ssam
1309178354Ssamvoid
1310178354Ssamieee80211_stop(struct ieee80211vap *vap)
1311178354Ssam{
1312178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1313178354Ssam
1314178354Ssam	IEEE80211_LOCK(ic);
1315178354Ssam	ieee80211_stop_locked(vap);
1316178354Ssam	IEEE80211_UNLOCK(ic);
1317178354Ssam}
1318178354Ssam
1319178354Ssam/*
1320178354Ssam * Stop all vap's running on a device.
1321178354Ssam */
1322178354Ssamvoid
1323178354Ssamieee80211_stop_all(struct ieee80211com *ic)
1324178354Ssam{
1325178354Ssam	struct ieee80211vap *vap;
1326178354Ssam
1327178354Ssam	IEEE80211_LOCK(ic);
1328178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1329178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1330178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1331178354Ssam			ieee80211_stop_locked(vap);
1332178354Ssam	}
1333178354Ssam	IEEE80211_UNLOCK(ic);
1334188533Sthompsa
1335188533Sthompsa	ieee80211_waitfor_parent(ic);
1336178354Ssam}
1337178354Ssam
1338178354Ssam/*
1339179391Ssam * Stop all vap's running on a device and arrange
1340179391Ssam * for those that were running to be resumed.
1341179391Ssam */
1342179391Ssamvoid
1343179391Ssamieee80211_suspend_all(struct ieee80211com *ic)
1344179391Ssam{
1345179391Ssam	struct ieee80211vap *vap;
1346179391Ssam
1347179391Ssam	IEEE80211_LOCK(ic);
1348179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1349179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1350179391Ssam		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
1351179391Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1352179391Ssam			ieee80211_stop_locked(vap);
1353179391Ssam		}
1354179391Ssam	}
1355179391Ssam	IEEE80211_UNLOCK(ic);
1356188533Sthompsa
1357188533Sthompsa	ieee80211_waitfor_parent(ic);
1358179391Ssam}
1359179391Ssam
1360179391Ssam/*
1361179391Ssam * Start all vap's marked for resume.
1362179391Ssam */
1363179391Ssamvoid
1364179391Ssamieee80211_resume_all(struct ieee80211com *ic)
1365179391Ssam{
1366179391Ssam	struct ieee80211vap *vap;
1367179391Ssam
1368179391Ssam	IEEE80211_LOCK(ic);
1369179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1370179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1371179391Ssam		if (!IFNET_IS_UP_RUNNING(ifp) &&
1372179391Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1373179391Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1374179391Ssam			ieee80211_start_locked(vap);
1375179391Ssam		}
1376179391Ssam	}
1377179391Ssam	IEEE80211_UNLOCK(ic);
1378179391Ssam}
1379179391Ssam
1380153349Ssamvoid
1381153349Ssamieee80211_beacon_miss(struct ieee80211com *ic)
1382153349Ssam{
1383191746Sthompsa	IEEE80211_LOCK(ic);
1384191746Sthompsa	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1385191746Sthompsa		/* Process in a taskq, the handler may reenter the driver */
1386191746Sthompsa		ieee80211_runtask(ic, &ic->ic_bmiss_task);
1387191746Sthompsa	}
1388191746Sthompsa	IEEE80211_UNLOCK(ic);
1389191746Sthompsa}
1390191746Sthompsa
1391191746Sthompsastatic void
1392191746Sthompsabeacon_miss(void *arg, int npending)
1393191746Sthompsa{
1394191746Sthompsa	struct ieee80211com *ic = arg;
1395178354Ssam	struct ieee80211vap *vap;
1396153349Ssam
1397178354Ssam	/* XXX locking */
1398178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1399153349Ssam		/*
1400178354Ssam		 * We only pass events through for sta vap's in RUN state;
1401178354Ssam		 * may be too restrictive but for now this saves all the
1402178354Ssam		 * handlers duplicating these checks.
1403153349Ssam		 */
1404178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1405193439Ssam		    vap->iv_state >= IEEE80211_S_RUN &&
1406178354Ssam		    vap->iv_bmiss != NULL)
1407178354Ssam			vap->iv_bmiss(vap);
1408153349Ssam	}
1409153349Ssam}
1410153349Ssam
1411191746Sthompsastatic void
1412191746Sthompsabeacon_swmiss(void *arg, int npending)
1413191746Sthompsa{
1414191746Sthompsa	struct ieee80211vap *vap = arg;
1415191746Sthompsa
1416191746Sthompsa	if (vap->iv_state != IEEE80211_S_RUN)
1417191746Sthompsa		return;
1418191746Sthompsa
1419191746Sthompsa	/* XXX Call multiple times if npending > zero? */
1420191746Sthompsa	vap->iv_bmiss(vap);
1421191746Sthompsa}
1422191746Sthompsa
1423154736Ssam/*
1424154736Ssam * Software beacon miss handling.  Check if any beacons
1425154736Ssam * were received in the last period.  If not post a
1426154736Ssam * beacon miss; otherwise reset the counter.
1427154736Ssam */
1428178354Ssamvoid
1429154736Ssamieee80211_swbmiss(void *arg)
1430154736Ssam{
1431178354Ssam	struct ieee80211vap *vap = arg;
1432179217Ssam	struct ieee80211com *ic = vap->iv_ic;
1433154736Ssam
1434179217Ssam	/* XXX sleep state? */
1435179217Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1436179217Ssam	    ("wrong state %d", vap->iv_state));
1437179217Ssam
1438179217Ssam	if (ic->ic_flags & IEEE80211_F_SCAN) {
1439179217Ssam		/*
1440179217Ssam		 * If scanning just ignore and reset state.  If we get a
1441179217Ssam		 * bmiss after coming out of scan because we haven't had
1442179217Ssam		 * time to receive a beacon then we should probe the AP
1443179217Ssam		 * before posting a real bmiss (unless iv_bmiss_max has
1444179217Ssam		 * been artifiically lowered).  A cleaner solution might
1445179217Ssam		 * be to disable the timer on scan start/end but to handle
1446179217Ssam		 * case of multiple sta vap's we'd need to disable the
1447179217Ssam		 * timers of all affected vap's.
1448179217Ssam		 */
1449179217Ssam		vap->iv_swbmiss_count = 0;
1450179217Ssam	} else if (vap->iv_swbmiss_count == 0) {
1451178354Ssam		if (vap->iv_bmiss != NULL)
1452191746Sthompsa			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1453154736Ssam	} else
1454178354Ssam		vap->iv_swbmiss_count = 0;
1455178354Ssam	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1456178354Ssam		ieee80211_swbmiss, vap);
1457154736Ssam}
1458154736Ssam
1459178354Ssam/*
1460178354Ssam * Start an 802.11h channel switch.  We record the parameters,
1461178354Ssam * mark the operation pending, notify each vap through the
1462178354Ssam * beacon update mechanism so it can update the beacon frame
1463178354Ssam * contents, and then switch vap's to CSA state to block outbound
1464178354Ssam * traffic.  Devices that handle CSA directly can use the state
1465178354Ssam * switch to do the right thing so long as they call
1466178354Ssam * ieee80211_csa_completeswitch when it's time to complete the
1467178354Ssam * channel change.  Devices that depend on the net80211 layer can
1468178354Ssam * use ieee80211_beacon_update to handle the countdown and the
1469178354Ssam * channel switch.
1470178354Ssam */
1471178354Ssamvoid
1472178354Ssamieee80211_csa_startswitch(struct ieee80211com *ic,
1473178354Ssam	struct ieee80211_channel *c, int mode, int count)
1474178354Ssam{
1475178354Ssam	struct ieee80211vap *vap;
1476178354Ssam
1477178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1478178354Ssam
1479178354Ssam	ic->ic_csa_newchan = c;
1480193439Ssam	ic->ic_csa_mode = mode;
1481178354Ssam	ic->ic_csa_count = count;
1482178354Ssam	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1483178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1484178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1485195618Srpaulo		    vap->iv_opmode == IEEE80211_M_IBSS ||
1486195618Srpaulo		    vap->iv_opmode == IEEE80211_M_MBSS)
1487178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1488178354Ssam		/* switch to CSA state to block outbound traffic */
1489178354Ssam		if (vap->iv_state == IEEE80211_S_RUN)
1490178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1491178354Ssam	}
1492178354Ssam	ieee80211_notify_csa(ic, c, mode, count);
1493178354Ssam}
1494178354Ssam
1495193439Ssamstatic void
1496193439Ssamcsa_completeswitch(struct ieee80211com *ic)
1497193439Ssam{
1498193439Ssam	struct ieee80211vap *vap;
1499193439Ssam
1500193439Ssam	ic->ic_csa_newchan = NULL;
1501193439Ssam	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1502193439Ssam
1503193439Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1504193439Ssam		if (vap->iv_state == IEEE80211_S_CSA)
1505193439Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1506193439Ssam}
1507193439Ssam
1508178354Ssam/*
1509178354Ssam * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1510178354Ssam * We clear state and move all vap's in CSA state to RUN state
1511178354Ssam * so they can again transmit.
1512178354Ssam */
1513178354Ssamvoid
1514178354Ssamieee80211_csa_completeswitch(struct ieee80211com *ic)
1515178354Ssam{
1516178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1517178354Ssam
1518178354Ssam	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1519178354Ssam
1520178354Ssam	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1521193439Ssam	csa_completeswitch(ic);
1522193439Ssam}
1523178354Ssam
1524193439Ssam/*
1525193439Ssam * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1526193439Ssam * We clear state and move all vap's in CSA state to RUN state
1527193439Ssam * so they can again transmit.
1528193439Ssam */
1529193439Ssamvoid
1530193439Ssamieee80211_csa_cancelswitch(struct ieee80211com *ic)
1531193439Ssam{
1532193439Ssam	IEEE80211_LOCK_ASSERT(ic);
1533193439Ssam
1534193439Ssam	csa_completeswitch(ic);
1535178354Ssam}
1536178354Ssam
1537178354Ssam/*
1538178354Ssam * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1539178354Ssam * We clear state and move all vap's in CAC state to RUN state.
1540178354Ssam */
1541178354Ssamvoid
1542178354Ssamieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1543178354Ssam{
1544178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1545178354Ssam	struct ieee80211vap *vap;
1546178354Ssam
1547178354Ssam	IEEE80211_LOCK(ic);
1548178354Ssam	/*
1549178354Ssam	 * Complete CAC state change for lead vap first; then
1550178354Ssam	 * clock all the other vap's waiting.
1551178354Ssam	 */
1552178354Ssam	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1553178354Ssam	    ("wrong state %d", vap0->iv_state));
1554178354Ssam	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1555178354Ssam
1556178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1557178354Ssam		if (vap->iv_state == IEEE80211_S_CAC)
1558178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1559178354Ssam	IEEE80211_UNLOCK(ic);
1560178354Ssam}
1561178354Ssam
1562178354Ssam/*
1563178354Ssam * Force all vap's other than the specified vap to the INIT state
1564178354Ssam * and mark them as waiting for a scan to complete.  These vaps
1565178354Ssam * will be brought up when the scan completes and the scanning vap
1566178354Ssam * reaches RUN state by wakeupwaiting.
1567178354Ssam */
1568154736Ssamstatic void
1569178354Ssammarkwaiting(struct ieee80211vap *vap0)
1570147765Ssam{
1571178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1572178354Ssam	struct ieee80211vap *vap;
1573147765Ssam
1574178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1575178354Ssam
1576191746Sthompsa	/*
1577191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1578191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1579191746Sthompsa	 * change task.
1580191746Sthompsa	 */
1581178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1582178354Ssam		if (vap == vap0)
1583178354Ssam			continue;
1584178354Ssam		if (vap->iv_state != IEEE80211_S_INIT) {
1585191746Sthompsa			/* NB: iv_newstate may drop the lock */
1586178354Ssam			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1587178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1588178354Ssam		}
1589147765Ssam	}
1590147765Ssam}
1591147765Ssam
1592178354Ssam/*
1593178354Ssam * Wakeup all vap's waiting for a scan to complete.  This is the
1594178354Ssam * companion to markwaiting (above) and is used to coordinate
1595178354Ssam * multiple vaps scanning.
1596191746Sthompsa * This is called from the state taskqueue.
1597178354Ssam */
1598147765Ssamstatic void
1599178354Ssamwakeupwaiting(struct ieee80211vap *vap0)
1600147765Ssam{
1601178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1602178354Ssam	struct ieee80211vap *vap;
1603147765Ssam
1604178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1605178354Ssam
1606191746Sthompsa	/*
1607191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1608191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1609191746Sthompsa	 * change task.
1610191746Sthompsa	 */
1611178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1612178354Ssam		if (vap == vap0)
1613178354Ssam			continue;
1614178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1615178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1616178354Ssam			/* NB: sta's cannot go INIT->RUN */
1617191746Sthompsa			/* NB: iv_newstate may drop the lock */
1618178354Ssam			vap->iv_newstate(vap,
1619178354Ssam			    vap->iv_opmode == IEEE80211_M_STA ?
1620178354Ssam			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1621178354Ssam		}
1622178354Ssam	}
1623147765Ssam}
1624147765Ssam
1625170530Ssam/*
1626178354Ssam * Handle post state change work common to all operating modes.
1627170530Ssam */
1628170530Ssamstatic void
1629191746Sthompsaieee80211_newstate_cb(void *xvap, int npending)
1630170530Ssam{
1631191746Sthompsa	struct ieee80211vap *vap = xvap;
1632178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1633191746Sthompsa	enum ieee80211_state nstate, ostate;
1634191746Sthompsa	int arg, rc;
1635178354Ssam
1636191746Sthompsa	IEEE80211_LOCK(ic);
1637191746Sthompsa	nstate = vap->iv_nstate;
1638191746Sthompsa	arg = vap->iv_nstate_arg;
1639178354Ssam
1640191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1641191746Sthompsa		/*
1642191746Sthompsa		 * We have been requested to drop back to the INIT before
1643191746Sthompsa		 * proceeding to the new state.
1644191746Sthompsa		 */
1645191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1646191746Sthompsa		    "%s: %s -> %s arg %d\n", __func__,
1647191746Sthompsa		    ieee80211_state_name[vap->iv_state],
1648191746Sthompsa		    ieee80211_state_name[IEEE80211_S_INIT], arg);
1649191746Sthompsa		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1650191746Sthompsa		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1651191746Sthompsa	}
1652191746Sthompsa
1653191746Sthompsa	ostate = vap->iv_state;
1654191746Sthompsa	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1655191746Sthompsa		/*
1656191746Sthompsa		 * SCAN was forced; e.g. on beacon miss.  Force other running
1657191746Sthompsa		 * vap's to INIT state and mark them as waiting for the scan to
1658191746Sthompsa		 * complete.  This insures they don't interfere with our
1659191746Sthompsa		 * scanning.  Since we are single threaded the vaps can not
1660191746Sthompsa		 * transition again while we are executing.
1661191746Sthompsa		 *
1662191746Sthompsa		 * XXX not always right, assumes ap follows sta
1663191746Sthompsa		 */
1664191746Sthompsa		markwaiting(vap);
1665191746Sthompsa	}
1666178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1667191746Sthompsa	    "%s: %s -> %s arg %d\n", __func__,
1668191746Sthompsa	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1669178354Ssam
1670191746Sthompsa	rc = vap->iv_newstate(vap, nstate, arg);
1671191746Sthompsa	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1672191746Sthompsa	if (rc != 0) {
1673191746Sthompsa		/* State transition failed */
1674191746Sthompsa		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1675191746Sthompsa		KASSERT(nstate != IEEE80211_S_INIT,
1676191746Sthompsa		    ("INIT state change failed"));
1677191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1678191746Sthompsa		    "%s: %s returned error %d\n", __func__,
1679191746Sthompsa		    ieee80211_state_name[nstate], rc);
1680191746Sthompsa		goto done;
1681191746Sthompsa	}
1682191746Sthompsa
1683191746Sthompsa	/* No actual transition, skip post processing */
1684191746Sthompsa	if (ostate == nstate)
1685191746Sthompsa		goto done;
1686191746Sthompsa
1687178354Ssam	if (nstate == IEEE80211_S_RUN) {
1688178354Ssam		/*
1689178354Ssam		 * OACTIVE may be set on the vap if the upper layer
1690178354Ssam		 * tried to transmit (e.g. IPv6 NDP) before we reach
1691178354Ssam		 * RUN state.  Clear it and restart xmit.
1692178354Ssam		 *
1693178354Ssam		 * Note this can also happen as a result of SLEEP->RUN
1694178354Ssam		 * (i.e. coming out of power save mode).
1695178354Ssam		 */
1696178354Ssam		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1697178354Ssam		if_start(vap->iv_ifp);
1698178354Ssam
1699178354Ssam		/* bring up any vaps waiting on us */
1700178354Ssam		wakeupwaiting(vap);
1701178354Ssam	} else if (nstate == IEEE80211_S_INIT) {
1702178354Ssam		/*
1703178354Ssam		 * Flush the scan cache if we did the last scan (XXX?)
1704178354Ssam		 * and flush any frames on send queues from this vap.
1705178354Ssam		 * Note the mgt q is used only for legacy drivers and
1706178354Ssam		 * will go away shortly.
1707178354Ssam		 */
1708178354Ssam		ieee80211_scan_flush(vap);
1709178354Ssam
1710178354Ssam		/* XXX NB: cast for altq */
1711178354Ssam		ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1712170530Ssam	}
1713191746Sthompsadone:
1714191746Sthompsa	IEEE80211_UNLOCK(ic);
1715170530Ssam}
1716170530Ssam
1717178354Ssam/*
1718178354Ssam * Public interface for initiating a state machine change.
1719178354Ssam * This routine single-threads the request and coordinates
1720178354Ssam * the scheduling of multiple vaps for the purpose of selecting
1721178354Ssam * an operating channel.  Specifically the following scenarios
1722178354Ssam * are handled:
1723178354Ssam * o only one vap can be selecting a channel so on transition to
1724178354Ssam *   SCAN state if another vap is already scanning then
1725178354Ssam *   mark the caller for later processing and return without
1726178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1727178354Ssam * o only one vap can be doing CAC of a channel so on transition to
1728178354Ssam *   CAC state if another vap is already scanning for radar then
1729178354Ssam *   mark the caller for later processing and return without
1730178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1731178354Ssam * o if another vap is already running when a request is made
1732178354Ssam *   to SCAN then an operating channel has been chosen; bypass
1733178354Ssam *   the scan and just join the channel
1734178354Ssam *
1735178354Ssam * Note that the state change call is done through the iv_newstate
1736178354Ssam * method pointer so any driver routine gets invoked.  The driver
1737178354Ssam * will normally call back into operating mode-specific
1738178354Ssam * ieee80211_newstate routines (below) unless it needs to completely
1739178354Ssam * bypass the state machine (e.g. because the firmware has it's
1740178354Ssam * own idea how things should work).  Bypassing the net80211 layer
1741178354Ssam * is usually a mistake and indicates lack of proper integration
1742178354Ssam * with the net80211 layer.
1743178354Ssam */
1744117811Ssamstatic int
1745178354Ssamieee80211_new_state_locked(struct ieee80211vap *vap,
1746178354Ssam	enum ieee80211_state nstate, int arg)
1747116742Ssam{
1748178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1749178354Ssam	struct ieee80211vap *vp;
1750117811Ssam	enum ieee80211_state ostate;
1751191746Sthompsa	int nrunning, nscanning;
1752116742Ssam
1753178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1754178354Ssam
1755191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1756191746Sthompsa		if (vap->iv_nstate == IEEE80211_S_INIT) {
1757191746Sthompsa			/*
1758191746Sthompsa			 * XXX The vap is being stopped, do no allow any other
1759191746Sthompsa			 * state changes until this is completed.
1760191746Sthompsa			 */
1761191746Sthompsa			return -1;
1762191768Sthompsa		} else if (vap->iv_state != vap->iv_nstate) {
1763191746Sthompsa#if 0
1764191768Sthompsa			/* Warn if the previous state hasn't completed. */
1765191768Sthompsa			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1766191768Sthompsa			    "%s: pending %s -> %s transition lost\n", __func__,
1767191768Sthompsa			    ieee80211_state_name[vap->iv_state],
1768191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1769191746Sthompsa#else
1770191768Sthompsa			/* XXX temporarily enable to identify issues */
1771191768Sthompsa			if_printf(vap->iv_ifp,
1772191768Sthompsa			    "%s: pending %s -> %s transition lost\n",
1773191768Sthompsa			    __func__, ieee80211_state_name[vap->iv_state],
1774191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1775191746Sthompsa#endif
1776191768Sthompsa		}
1777191746Sthompsa	}
1778191746Sthompsa
1779178354Ssam	nrunning = nscanning = 0;
1780178354Ssam	/* XXX can track this state instead of calculating */
1781178354Ssam	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1782178354Ssam		if (vp != vap) {
1783178354Ssam			if (vp->iv_state >= IEEE80211_S_RUN)
1784178354Ssam				nrunning++;
1785178354Ssam			/* XXX doesn't handle bg scan */
1786178354Ssam			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1787178354Ssam			else if (vp->iv_state > IEEE80211_S_INIT)
1788178354Ssam				nscanning++;
1789178354Ssam		}
1790178354Ssam	}
1791178354Ssam	ostate = vap->iv_state;
1792178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1793178354Ssam	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1794178354Ssam	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1795178354Ssam	    nrunning, nscanning);
1796116742Ssam	switch (nstate) {
1797116742Ssam	case IEEE80211_S_SCAN:
1798178354Ssam		if (ostate == IEEE80211_S_INIT) {
1799178354Ssam			/*
1800178354Ssam			 * INIT -> SCAN happens on initial bringup.
1801178354Ssam			 */
1802178354Ssam			KASSERT(!(nscanning && nrunning),
1803178354Ssam			    ("%d scanning and %d running", nscanning, nrunning));
1804178354Ssam			if (nscanning) {
1805116742Ssam				/*
1806178354Ssam				 * Someone is scanning, defer our state
1807178354Ssam				 * change until the work has completed.
1808116742Ssam				 */
1809178354Ssam				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1810178354Ssam				    "%s: defer %s -> %s\n",
1811178354Ssam				    __func__, ieee80211_state_name[ostate],
1812178354Ssam				    ieee80211_state_name[nstate]);
1813178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1814191746Sthompsa				return 0;
1815116742Ssam			}
1816178354Ssam			if (nrunning) {
1817170530Ssam				/*
1818178354Ssam				 * Someone is operating; just join the channel
1819178354Ssam				 * they have chosen.
1820170530Ssam				 */
1821178354Ssam				/* XXX kill arg? */
1822178354Ssam				/* XXX check each opmode, adhoc? */
1823178354Ssam				if (vap->iv_opmode == IEEE80211_M_STA)
1824178354Ssam					nstate = IEEE80211_S_SCAN;
1825178354Ssam				else
1826178354Ssam					nstate = IEEE80211_S_RUN;
1827138568Ssam#ifdef IEEE80211_DEBUG
1828178354Ssam				if (nstate != IEEE80211_S_SCAN) {
1829178354Ssam					IEEE80211_DPRINTF(vap,
1830178354Ssam					    IEEE80211_MSG_STATE,
1831178354Ssam					    "%s: override, now %s -> %s\n",
1832178354Ssam					    __func__,
1833178354Ssam					    ieee80211_state_name[ostate],
1834178354Ssam					    ieee80211_state_name[nstate]);
1835178354Ssam				}
1836138568Ssam#endif
1837170530Ssam			}
1838116742Ssam		}
1839178354Ssam		break;
1840178354Ssam	case IEEE80211_S_RUN:
1841178354Ssam		if (vap->iv_opmode == IEEE80211_M_WDS &&
1842178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1843178354Ssam		    nscanning) {
1844154736Ssam			/*
1845178354Ssam			 * Legacy WDS with someone else scanning; don't
1846178354Ssam			 * go online until that completes as we should
1847178354Ssam			 * follow the other vap to the channel they choose.
1848154736Ssam			 */
1849178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1850178354Ssam			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
1851178354Ssam			     ieee80211_state_name[ostate],
1852178354Ssam			     ieee80211_state_name[nstate]);
1853178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1854191746Sthompsa			return 0;
1855154736Ssam		}
1856178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1857178354Ssam		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1858178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1859178354Ssam		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1860178354Ssam			/*
1861178354Ssam			 * This is a DFS channel, transition to CAC state
1862178354Ssam			 * instead of RUN.  This allows us to initiate
1863178354Ssam			 * Channel Availability Check (CAC) as specified
1864178354Ssam			 * by 11h/DFS.
1865178354Ssam			 */
1866178354Ssam			nstate = IEEE80211_S_CAC;
1867178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1868178354Ssam			     "%s: override %s -> %s (DFS)\n", __func__,
1869178354Ssam			     ieee80211_state_name[ostate],
1870178354Ssam			     ieee80211_state_name[nstate]);
1871138568Ssam		}
1872116742Ssam		break;
1873178354Ssam	case IEEE80211_S_INIT:
1874191955Sthompsa		/* cancel any scan in progress */
1875191955Sthompsa		ieee80211_cancel_scan(vap);
1876178354Ssam		if (ostate == IEEE80211_S_INIT ) {
1877178354Ssam			/* XXX don't believe this */
1878178354Ssam			/* INIT -> INIT. nothing to do */
1879178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1880178354Ssam		}
1881178354Ssam		/* fall thru... */
1882172058Ssam	default:
1883172058Ssam		break;
1884116742Ssam	}
1885191746Sthompsa	/* defer the state change to a thread */
1886191746Sthompsa	vap->iv_nstate = nstate;
1887191746Sthompsa	vap->iv_nstate_arg = arg;
1888191746Sthompsa	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1889191746Sthompsa	ieee80211_runtask(ic, &vap->iv_nstate_task);
1890191746Sthompsa	return EINPROGRESS;
1891116742Ssam}
1892178354Ssam
1893178354Ssamint
1894178354Ssamieee80211_new_state(struct ieee80211vap *vap,
1895178354Ssam	enum ieee80211_state nstate, int arg)
1896178354Ssam{
1897178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1898178354Ssam	int rc;
1899178354Ssam
1900178354Ssam	IEEE80211_LOCK(ic);
1901178354Ssam	rc = ieee80211_new_state_locked(vap, nstate, arg);
1902178354Ssam	IEEE80211_UNLOCK(ic);
1903178354Ssam	return rc;
1904178354Ssam}
1905