ieee80211_proto.c revision 195618
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 195618 2009-07-11 15:02:45Z rpaulo $");
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;
210188779Ssam		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
211188779Ssam			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
212188779Ssam			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
213188779Ssam		} else {
214188779Ssam			vap->iv_txparms[i].mgmtrate =
215188779Ssam			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
216188779Ssam			vap->iv_txparms[i].mcastrate =
217188779Ssam			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
218188779Ssam		}
219178354Ssam		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
220178354Ssam	}
221178354Ssam	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
222178354Ssam
223178354Ssam	vap->iv_update_beacon = null_update_beacon;
224178354Ssam	vap->iv_deliver_data = ieee80211_deliver_data;
225178354Ssam
226178354Ssam	/* attach support for operating mode */
227178354Ssam	ic->ic_vattach[vap->iv_opmode](vap);
228178354Ssam}
229178354Ssam
230178354Ssamvoid
231178354Ssamieee80211_proto_vdetach(struct ieee80211vap *vap)
232178354Ssam{
233178354Ssam#define	FREEAPPIE(ie) do { \
234178354Ssam	if (ie != NULL) \
235186302Ssam		free(ie, M_80211_NODE_IE); \
236178354Ssam} while (0)
237178354Ssam	/*
238178354Ssam	 * Detach operating mode module.
239178354Ssam	 */
240178354Ssam	if (vap->iv_opdetach != NULL)
241178354Ssam		vap->iv_opdetach(vap);
242178354Ssam	/*
243138568Ssam	 * This should not be needed as we detach when reseting
244138568Ssam	 * the state but be conservative here since the
245138568Ssam	 * authenticator may do things like spawn kernel threads.
246138568Ssam	 */
247178354Ssam	if (vap->iv_auth->ia_detach != NULL)
248178354Ssam		vap->iv_auth->ia_detach(vap);
249138568Ssam	/*
250138568Ssam	 * Detach any ACL'ator.
251138568Ssam	 */
252178354Ssam	if (vap->iv_acl != NULL)
253178354Ssam		vap->iv_acl->iac_detach(vap);
254178354Ssam
255178354Ssam	FREEAPPIE(vap->iv_appie_beacon);
256178354Ssam	FREEAPPIE(vap->iv_appie_probereq);
257178354Ssam	FREEAPPIE(vap->iv_appie_proberesp);
258178354Ssam	FREEAPPIE(vap->iv_appie_assocreq);
259178354Ssam	FREEAPPIE(vap->iv_appie_assocresp);
260178354Ssam	FREEAPPIE(vap->iv_appie_wpa);
261178354Ssam#undef FREEAPPIE
262116742Ssam}
263116742Ssam
264138568Ssam/*
265138568Ssam * Simple-minded authenticator module support.
266138568Ssam */
267138568Ssam
268138568Ssam#define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
269138568Ssam/* XXX well-known names */
270138568Ssamstatic const char *auth_modnames[IEEE80211_AUTH_MAX] = {
271138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_NONE */
272138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
273138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
274138568Ssam	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
275138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
276138568Ssam	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
277138568Ssam};
278138568Ssamstatic const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
279138568Ssam
280138568Ssamstatic const struct ieee80211_authenticator auth_internal = {
281138568Ssam	.ia_name		= "wlan_internal",
282138568Ssam	.ia_attach		= NULL,
283138568Ssam	.ia_detach		= NULL,
284138568Ssam	.ia_node_join		= NULL,
285138568Ssam	.ia_node_leave		= NULL,
286138568Ssam};
287138568Ssam
288138568Ssam/*
289138568Ssam * Setup internal authenticators once; they are never unregistered.
290138568Ssam */
291138568Ssamstatic void
292138568Ssamieee80211_auth_setup(void)
293138568Ssam{
294138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
295138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
296138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
297138568Ssam}
298138568SsamSYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
299138568Ssam
300138568Ssamconst struct ieee80211_authenticator *
301138568Ssamieee80211_authenticator_get(int auth)
302138568Ssam{
303138568Ssam	if (auth >= IEEE80211_AUTH_MAX)
304138568Ssam		return NULL;
305138568Ssam	if (authenticators[auth] == NULL)
306138568Ssam		ieee80211_load_module(auth_modnames[auth]);
307138568Ssam	return authenticators[auth];
308138568Ssam}
309138568Ssam
310116742Ssamvoid
311138568Ssamieee80211_authenticator_register(int type,
312138568Ssam	const struct ieee80211_authenticator *auth)
313116742Ssam{
314138568Ssam	if (type >= IEEE80211_AUTH_MAX)
315138568Ssam		return;
316138568Ssam	authenticators[type] = auth;
317138568Ssam}
318138568Ssam
319138568Ssamvoid
320138568Ssamieee80211_authenticator_unregister(int type)
321138568Ssam{
322138568Ssam
323138568Ssam	if (type >= IEEE80211_AUTH_MAX)
324138568Ssam		return;
325138568Ssam	authenticators[type] = NULL;
326138568Ssam}
327138568Ssam
328138568Ssam/*
329138568Ssam * Very simple-minded ACL module support.
330138568Ssam */
331138568Ssam/* XXX just one for now */
332138568Ssamstatic	const struct ieee80211_aclator *acl = NULL;
333138568Ssam
334138568Ssamvoid
335138568Ssamieee80211_aclator_register(const struct ieee80211_aclator *iac)
336138568Ssam{
337138568Ssam	printf("wlan: %s acl policy registered\n", iac->iac_name);
338138568Ssam	acl = iac;
339138568Ssam}
340138568Ssam
341138568Ssamvoid
342138568Ssamieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
343138568Ssam{
344138568Ssam	if (acl == iac)
345138568Ssam		acl = NULL;
346138568Ssam	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
347138568Ssam}
348138568Ssam
349138568Ssamconst struct ieee80211_aclator *
350138568Ssamieee80211_aclator_get(const char *name)
351138568Ssam{
352138568Ssam	if (acl == NULL)
353138568Ssam		ieee80211_load_module("wlan_acl");
354138568Ssam	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
355138568Ssam}
356138568Ssam
357138568Ssamvoid
358170530Ssamieee80211_print_essid(const uint8_t *essid, int len)
359138568Ssam{
360170530Ssam	const uint8_t *p;
361116742Ssam	int i;
362116742Ssam
363116742Ssam	if (len > IEEE80211_NWID_LEN)
364116742Ssam		len = IEEE80211_NWID_LEN;
365116742Ssam	/* determine printable or not */
366116742Ssam	for (i = 0, p = essid; i < len; i++, p++) {
367116742Ssam		if (*p < ' ' || *p > 0x7e)
368116742Ssam			break;
369116742Ssam	}
370116742Ssam	if (i == len) {
371116742Ssam		printf("\"");
372116742Ssam		for (i = 0, p = essid; i < len; i++, p++)
373116742Ssam			printf("%c", *p);
374116742Ssam		printf("\"");
375116742Ssam	} else {
376116742Ssam		printf("0x");
377116742Ssam		for (i = 0, p = essid; i < len; i++, p++)
378116742Ssam			printf("%02x", *p);
379116742Ssam	}
380116742Ssam}
381116742Ssam
382116742Ssamvoid
383170530Ssamieee80211_dump_pkt(struct ieee80211com *ic,
384170530Ssam	const uint8_t *buf, int len, int rate, int rssi)
385116742Ssam{
386138568Ssam	const struct ieee80211_frame *wh;
387116742Ssam	int i;
388116742Ssam
389138568Ssam	wh = (const struct ieee80211_frame *)buf;
390116742Ssam	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
391116742Ssam	case IEEE80211_FC1_DIR_NODS:
392116742Ssam		printf("NODS %s", ether_sprintf(wh->i_addr2));
393116742Ssam		printf("->%s", ether_sprintf(wh->i_addr1));
394116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr3));
395116742Ssam		break;
396116742Ssam	case IEEE80211_FC1_DIR_TODS:
397116742Ssam		printf("TODS %s", ether_sprintf(wh->i_addr2));
398116742Ssam		printf("->%s", ether_sprintf(wh->i_addr3));
399116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr1));
400116742Ssam		break;
401116742Ssam	case IEEE80211_FC1_DIR_FROMDS:
402116742Ssam		printf("FRDS %s", ether_sprintf(wh->i_addr3));
403116742Ssam		printf("->%s", ether_sprintf(wh->i_addr1));
404116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr2));
405116742Ssam		break;
406116742Ssam	case IEEE80211_FC1_DIR_DSTODS:
407170530Ssam		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
408116742Ssam		printf("->%s", ether_sprintf(wh->i_addr3));
409116742Ssam		printf("(%s", ether_sprintf(wh->i_addr2));
410116742Ssam		printf("->%s)", ether_sprintf(wh->i_addr1));
411116742Ssam		break;
412116742Ssam	}
413116742Ssam	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
414116742Ssam	case IEEE80211_FC0_TYPE_DATA:
415116742Ssam		printf(" data");
416116742Ssam		break;
417116742Ssam	case IEEE80211_FC0_TYPE_MGT:
418116742Ssam		printf(" %s", ieee80211_mgt_subtype_name[
419116742Ssam		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
420116742Ssam		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
421116742Ssam		break;
422116742Ssam	default:
423116742Ssam		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
424116742Ssam		break;
425116742Ssam	}
426170530Ssam	if (IEEE80211_QOS_HAS_SEQ(wh)) {
427170530Ssam		const struct ieee80211_qosframe *qwh =
428170530Ssam			(const struct ieee80211_qosframe *)buf;
429170530Ssam		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
430170530Ssam			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
431170530Ssam	}
432138568Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
433170530Ssam		int off;
434170530Ssam
435170530Ssam		off = ieee80211_anyhdrspace(ic, wh);
436170530Ssam		printf(" WEP [IV %.02x %.02x %.02x",
437170530Ssam			buf[off+0], buf[off+1], buf[off+2]);
438170530Ssam		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
439170530Ssam			printf(" %.02x %.02x %.02x",
440170530Ssam				buf[off+4], buf[off+5], buf[off+6]);
441170530Ssam		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
442138568Ssam	}
443116742Ssam	if (rate >= 0)
444116742Ssam		printf(" %dM", rate / 2);
445116742Ssam	if (rssi >= 0)
446116742Ssam		printf(" +%d", rssi);
447116742Ssam	printf("\n");
448116742Ssam	if (len > 0) {
449116742Ssam		for (i = 0; i < len; i++) {
450116742Ssam			if ((i & 1) == 0)
451116742Ssam				printf(" ");
452116742Ssam			printf("%02x", buf[i]);
453116742Ssam		}
454116742Ssam		printf("\n");
455116742Ssam	}
456116742Ssam}
457116742Ssam
458165887Ssamstatic __inline int
459165887Ssamfindrix(const struct ieee80211_rateset *rs, int r)
460165887Ssam{
461165887Ssam	int i;
462165887Ssam
463165887Ssam	for (i = 0; i < rs->rs_nrates; i++)
464165887Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
465165887Ssam			return i;
466165887Ssam	return -1;
467165887Ssam}
468165887Ssam
469116742Ssamint
470167442Ssamieee80211_fix_rate(struct ieee80211_node *ni,
471167442Ssam	struct ieee80211_rateset *nrs, int flags)
472116742Ssam{
473116742Ssam#define	RV(v)	((v) & IEEE80211_RATE_VAL)
474178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
475148299Ssam	struct ieee80211com *ic = ni->ni_ic;
476165887Ssam	int i, j, rix, error;
477178354Ssam	int okrate, badrate, fixedrate, ucastrate;
478165569Ssam	const struct ieee80211_rateset *srs;
479170530Ssam	uint8_t r;
480116742Ssam
481116742Ssam	error = 0;
482170530Ssam	okrate = badrate = 0;
483178354Ssam	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
484178354Ssam	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
485178354Ssam		/*
486178354Ssam		 * Workaround awkwardness with fixed rate.  We are called
487178354Ssam		 * to check both the legacy rate set and the HT rate set
488178354Ssam		 * but we must apply any legacy fixed rate check only to the
489178354Ssam		 * legacy rate set and vice versa.  We cannot tell what type
490178354Ssam		 * of rate set we've been given (legacy or HT) but we can
491178354Ssam		 * distinguish the fixed rate type (MCS have 0x80 set).
492178354Ssam		 * So to deal with this the caller communicates whether to
493178354Ssam		 * check MCS or legacy rate using the flags and we use the
494178354Ssam		 * type of any fixed rate to avoid applying an MCS to a
495178354Ssam		 * legacy rate and vice versa.
496178354Ssam		 */
497178354Ssam		if (ucastrate & 0x80) {
498178354Ssam			if (flags & IEEE80211_F_DOFRATE)
499178354Ssam				flags &= ~IEEE80211_F_DOFRATE;
500178354Ssam		} else if ((ucastrate & 0x80) == 0) {
501178354Ssam			if (flags & IEEE80211_F_DOFMCS)
502178354Ssam				flags &= ~IEEE80211_F_DOFMCS;
503178354Ssam		}
504178354Ssam		/* NB: required to make MCS match below work */
505178354Ssam		ucastrate &= IEEE80211_RATE_VAL;
506178354Ssam	}
507170530Ssam	fixedrate = IEEE80211_FIXED_RATE_NONE;
508178354Ssam	/*
509178354Ssam	 * XXX we are called to process both MCS and legacy rates;
510178354Ssam	 * we must use the appropriate basic rate set or chaos will
511178354Ssam	 * ensue; for now callers that want MCS must supply
512178354Ssam	 * IEEE80211_F_DOBRS; at some point we'll need to split this
513178354Ssam	 * function so there are two variants, one for MCS and one
514178354Ssam	 * for legacy rates.
515178354Ssam	 */
516178354Ssam	if (flags & IEEE80211_F_DOBRS)
517178354Ssam		srs = (const struct ieee80211_rateset *)
518178354Ssam		    ieee80211_get_suphtrates(ic, ni->ni_chan);
519178354Ssam	else
520178354Ssam		srs = ieee80211_get_suprates(ic, ni->ni_chan);
521120482Ssam	for (i = 0; i < nrs->rs_nrates; ) {
522116742Ssam		if (flags & IEEE80211_F_DOSORT) {
523116742Ssam			/*
524116742Ssam			 * Sort rates.
525116742Ssam			 */
526116742Ssam			for (j = i + 1; j < nrs->rs_nrates; j++) {
527116742Ssam				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
528116742Ssam					r = nrs->rs_rates[i];
529116742Ssam					nrs->rs_rates[i] = nrs->rs_rates[j];
530116742Ssam					nrs->rs_rates[j] = r;
531116742Ssam				}
532116742Ssam			}
533116742Ssam		}
534116742Ssam		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
535116742Ssam		badrate = r;
536165887Ssam		/*
537170530Ssam		 * Check for fixed rate.
538170530Ssam		 */
539178354Ssam		if (r == ucastrate)
540170530Ssam			fixedrate = r;
541170530Ssam		/*
542165887Ssam		 * Check against supported rates.
543165887Ssam		 */
544165887Ssam		rix = findrix(srs, r);
545116742Ssam		if (flags & IEEE80211_F_DONEGO) {
546165887Ssam			if (rix < 0) {
547120482Ssam				/*
548120482Ssam				 * A rate in the node's rate set is not
549120482Ssam				 * supported.  If this is a basic rate and we
550165887Ssam				 * are operating as a STA then this is an error.
551120482Ssam				 * Otherwise we just discard/ignore the rate.
552120482Ssam				 */
553165887Ssam				if ((flags & IEEE80211_F_JOIN) &&
554120482Ssam				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
555116742Ssam					error++;
556165887Ssam			} else if ((flags & IEEE80211_F_JOIN) == 0) {
557165887Ssam				/*
558165887Ssam				 * Overwrite with the supported rate
559165887Ssam				 * value so any basic rate bit is set.
560165887Ssam				 */
561165887Ssam				nrs->rs_rates[i] = srs->rs_rates[rix];
562116742Ssam			}
563116742Ssam		}
564165887Ssam		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
565116742Ssam			/*
566116742Ssam			 * Delete unacceptable rates.
567116742Ssam			 */
568165887Ssam			nrs->rs_nrates--;
569165887Ssam			for (j = i; j < nrs->rs_nrates; j++)
570165887Ssam				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
571165887Ssam			nrs->rs_rates[j] = 0;
572165887Ssam			continue;
573116742Ssam		}
574165887Ssam		if (rix >= 0)
575116742Ssam			okrate = nrs->rs_rates[i];
576116742Ssam		i++;
577116742Ssam	}
578138568Ssam	if (okrate == 0 || error != 0 ||
579178354Ssam	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
580178354Ssam	     fixedrate != ucastrate)) {
581178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
582178354Ssam		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
583178354Ssam		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
584116742Ssam		return badrate | IEEE80211_RATE_BASIC;
585178354Ssam	} else
586116742Ssam		return RV(okrate);
587116742Ssam#undef RV
588116742Ssam}
589116742Ssam
590138568Ssam/*
591138568Ssam * Reset 11g-related state.
592138568Ssam */
593138568Ssamvoid
594138568Ssamieee80211_reset_erp(struct ieee80211com *ic)
595138568Ssam{
596138568Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
597138568Ssam	ic->ic_nonerpsta = 0;
598138568Ssam	ic->ic_longslotsta = 0;
599138568Ssam	/*
600138568Ssam	 * Short slot time is enabled only when operating in 11g
601138568Ssam	 * and not in an IBSS.  We must also honor whether or not
602138568Ssam	 * the driver is capable of doing it.
603138568Ssam	 */
604138568Ssam	ieee80211_set_shortslottime(ic,
605170530Ssam		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
606170530Ssam		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
607170530Ssam		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
608138568Ssam		ic->ic_opmode == IEEE80211_M_HOSTAP &&
609138568Ssam		(ic->ic_caps & IEEE80211_C_SHSLOT)));
610138568Ssam	/*
611138568Ssam	 * Set short preamble and ERP barker-preamble flags.
612138568Ssam	 */
613170530Ssam	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
614138568Ssam	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
615138568Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
616138568Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
617138568Ssam	} else {
618138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
619138568Ssam		ic->ic_flags |= IEEE80211_F_USEBARKER;
620138568Ssam	}
621138568Ssam}
622138568Ssam
623138568Ssam/*
624138568Ssam * Set the short slot time state and notify the driver.
625138568Ssam */
626138568Ssamvoid
627138568Ssamieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
628138568Ssam{
629138568Ssam	if (onoff)
630138568Ssam		ic->ic_flags |= IEEE80211_F_SHSLOT;
631138568Ssam	else
632138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
633138568Ssam	/* notify driver */
634138568Ssam	if (ic->ic_updateslot != NULL)
635138568Ssam		ic->ic_updateslot(ic->ic_ifp);
636138568Ssam}
637138568Ssam
638138568Ssam/*
639138568Ssam * Check if the specified rate set supports ERP.
640138568Ssam * NB: the rate set is assumed to be sorted.
641138568Ssam */
642138568Ssamint
643178354Ssamieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
644138568Ssam{
645138568Ssam#define N(a)	(sizeof(a) / sizeof(a[0]))
646138568Ssam	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
647138568Ssam	int i, j;
648138568Ssam
649138568Ssam	if (rs->rs_nrates < N(rates))
650138568Ssam		return 0;
651138568Ssam	for (i = 0; i < N(rates); i++) {
652138568Ssam		for (j = 0; j < rs->rs_nrates; j++) {
653138568Ssam			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
654138568Ssam			if (rates[i] == r)
655138568Ssam				goto next;
656138568Ssam			if (r > rates[i])
657138568Ssam				return 0;
658138568Ssam		}
659138568Ssam		return 0;
660138568Ssam	next:
661138568Ssam		;
662138568Ssam	}
663138568Ssam	return 1;
664138568Ssam#undef N
665138568Ssam}
666138568Ssam
667138568Ssam/*
668178354Ssam * Mark the basic rates for the rate table based on the
669138568Ssam * operating mode.  For real 11g we mark all the 11b rates
670138568Ssam * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
671138568Ssam * 11b rates.  There's also a pseudo 11a-mode used to mark only
672138568Ssam * the basic OFDM rates.
673138568Ssam */
674178354Ssamstatic void
675178354Ssamsetbasicrates(struct ieee80211_rateset *rs,
676178354Ssam    enum ieee80211_phymode mode, int add)
677138568Ssam{
678170530Ssam	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
679188780Ssam	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
680188780Ssam	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
681188780Ssam					    /* NB: mixed b/g */
682188780Ssam	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
683188780Ssam	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
684188780Ssam	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
685188780Ssam	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
686188782Ssam	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
687188782Ssam	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
688188780Ssam	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
689188780Ssam					    /* NB: mixed b/g */
690188780Ssam	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
691138568Ssam	};
692138568Ssam	int i, j;
693138568Ssam
694138568Ssam	for (i = 0; i < rs->rs_nrates; i++) {
695178354Ssam		if (!add)
696178354Ssam			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
697138568Ssam		for (j = 0; j < basic[mode].rs_nrates; j++)
698138568Ssam			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
699138568Ssam				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
700138568Ssam				break;
701138568Ssam			}
702138568Ssam	}
703138568Ssam}
704138568Ssam
705138568Ssam/*
706178354Ssam * Set the basic rates in a rate set.
707138568Ssam */
708178354Ssamvoid
709178354Ssamieee80211_setbasicrates(struct ieee80211_rateset *rs,
710178354Ssam    enum ieee80211_phymode mode)
711178354Ssam{
712178354Ssam	setbasicrates(rs, mode, 0);
713178354Ssam}
714178354Ssam
715178354Ssam/*
716178354Ssam * Add basic rates to a rate set.
717178354Ssam */
718178354Ssamvoid
719178354Ssamieee80211_addbasicrates(struct ieee80211_rateset *rs,
720178354Ssam    enum ieee80211_phymode mode)
721178354Ssam{
722178354Ssam	setbasicrates(rs, mode, 1);
723178354Ssam}
724178354Ssam
725178354Ssam/*
726178354Ssam * WME protocol support.
727178354Ssam *
728178354Ssam * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
729178354Ssam * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
730178354Ssam * Draft 2.0 Test Plan (Appendix D).
731178354Ssam *
732178354Ssam * Static/Dynamic Turbo mode settings come from Atheros.
733178354Ssam */
734138568Ssamtypedef struct phyParamType {
735178354Ssam	uint8_t		aifsn;
736178354Ssam	uint8_t		logcwmin;
737178354Ssam	uint8_t		logcwmax;
738178354Ssam	uint16_t	txopLimit;
739178354Ssam	uint8_t 	acm;
740138568Ssam} paramType;
741138568Ssam
742138568Ssamstatic const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
743188780Ssam	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
744188780Ssam	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
745188780Ssam	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
746188780Ssam	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
747188780Ssam	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
748188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
749188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
750188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
751188782Ssam	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
752188782Ssam	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
753188780Ssam	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
754188780Ssam	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
755138568Ssam};
756138568Ssamstatic const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
757188780Ssam	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
758188780Ssam	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
759188780Ssam	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
760188780Ssam	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
761188780Ssam	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
762188780Ssam	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
763188780Ssam	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
764188780Ssam	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
765188782Ssam	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
766188782Ssam	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
767188780Ssam	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
768188780Ssam	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
769138568Ssam};
770138568Ssamstatic const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
771188780Ssam	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
772188780Ssam	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
773188780Ssam	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
774188780Ssam	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
775188780Ssam	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
776188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
777188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
778188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
779188782Ssam	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
780188782Ssam	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
781188780Ssam	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
782188780Ssam	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
783138568Ssam};
784138568Ssamstatic const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
785188780Ssam	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
786188780Ssam	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
787188780Ssam	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
788188780Ssam	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
789188780Ssam	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
790188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
791188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
792188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
793188782Ssam	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
794188782Ssam	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
795188780Ssam	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
796188780Ssam	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
797138568Ssam};
798138568Ssam
799138568Ssamstatic const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
800188780Ssam	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
801188780Ssam	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
802188780Ssam	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
803188780Ssam	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
804188780Ssam	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
805188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
806188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
807188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
808188782Ssam	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
809188782Ssam	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
810188780Ssam	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
811188780Ssam	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
812138568Ssam};
813138568Ssamstatic const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
814188780Ssam	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
815188780Ssam	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
816188780Ssam	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
817188780Ssam	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
818188780Ssam	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
819188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
820188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
821188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
822188782Ssam	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
823188782Ssam	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
824188780Ssam	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
825188780Ssam	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
826138568Ssam};
827138568Ssamstatic const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
828188780Ssam	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
829188780Ssam	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
830188780Ssam	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
831188780Ssam	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
832188780Ssam	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
833188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
834188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
835188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
836188782Ssam	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
837188782Ssam	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
838188780Ssam	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
839188780Ssam	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
840138568Ssam};
841138568Ssam
842178354Ssamstatic void
843188863Ssam_setifsparams(struct wmeParams *wmep, const paramType *phy)
844188863Ssam{
845188863Ssam	wmep->wmep_aifsn = phy->aifsn;
846188863Ssam	wmep->wmep_logcwmin = phy->logcwmin;
847188863Ssam	wmep->wmep_logcwmax = phy->logcwmax;
848188863Ssam	wmep->wmep_txopLimit = phy->txopLimit;
849188863Ssam}
850188863Ssam
851188863Ssamstatic void
852188863Ssamsetwmeparams(struct ieee80211vap *vap, const char *type, int ac,
853188863Ssam	struct wmeParams *wmep, const paramType *phy)
854188863Ssam{
855188863Ssam	wmep->wmep_acm = phy->acm;
856188863Ssam	_setifsparams(wmep, phy);
857188863Ssam
858188863Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
859188863Ssam	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
860188863Ssam	    ieee80211_wme_acnames[ac], type,
861188863Ssam	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
862188863Ssam	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
863188863Ssam}
864188863Ssam
865188863Ssamstatic void
866178354Ssamieee80211_wme_initparams_locked(struct ieee80211vap *vap)
867138568Ssam{
868178354Ssam	struct ieee80211com *ic = vap->iv_ic;
869138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
870138568Ssam	const paramType *pPhyParam, *pBssPhyParam;
871138568Ssam	struct wmeParams *wmep;
872170530Ssam	enum ieee80211_phymode mode;
873138568Ssam	int i;
874138568Ssam
875178354Ssam	IEEE80211_LOCK_ASSERT(ic);
876178354Ssam
877188864Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
878138568Ssam		return;
879138568Ssam
880170530Ssam	/*
881170530Ssam	 * Select mode; we can be called early in which case we
882170530Ssam	 * always use auto mode.  We know we'll be called when
883170530Ssam	 * entering the RUN state with bsschan setup properly
884170530Ssam	 * so state will eventually get set correctly
885170530Ssam	 */
886170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
887170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
888170530Ssam	else
889170530Ssam		mode = IEEE80211_MODE_AUTO;
890138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
891138568Ssam		switch (i) {
892138568Ssam		case WME_AC_BK:
893170530Ssam			pPhyParam = &phyParamForAC_BK[mode];
894170530Ssam			pBssPhyParam = &phyParamForAC_BK[mode];
895138568Ssam			break;
896138568Ssam		case WME_AC_VI:
897170530Ssam			pPhyParam = &phyParamForAC_VI[mode];
898170530Ssam			pBssPhyParam = &bssPhyParamForAC_VI[mode];
899138568Ssam			break;
900138568Ssam		case WME_AC_VO:
901170530Ssam			pPhyParam = &phyParamForAC_VO[mode];
902170530Ssam			pBssPhyParam = &bssPhyParamForAC_VO[mode];
903138568Ssam			break;
904138568Ssam		case WME_AC_BE:
905138568Ssam		default:
906170530Ssam			pPhyParam = &phyParamForAC_BE[mode];
907170530Ssam			pBssPhyParam = &bssPhyParamForAC_BE[mode];
908138568Ssam			break;
909138568Ssam		}
910138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
911138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
912188863Ssam			setwmeparams(vap, "chan", i, wmep, pPhyParam);
913138568Ssam		} else {
914188863Ssam			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
915138568Ssam		}
916138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
917188863Ssam		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
918138568Ssam	}
919138568Ssam	/* NB: check ic_bss to avoid NULL deref on initial attach */
920178354Ssam	if (vap->iv_bss != NULL) {
921138568Ssam		/*
922138568Ssam		 * Calculate agressive mode switching threshold based
923138568Ssam		 * on beacon interval.  This doesn't need locking since
924138568Ssam		 * we're only called before entering the RUN state at
925138568Ssam		 * which point we start sending beacon frames.
926138568Ssam		 */
927138568Ssam		wme->wme_hipri_switch_thresh =
928178354Ssam			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
929188864Ssam		wme->wme_flags &= ~WME_F_AGGRMODE;
930178354Ssam		ieee80211_wme_updateparams(vap);
931138568Ssam	}
932138568Ssam}
933138568Ssam
934178354Ssamvoid
935178354Ssamieee80211_wme_initparams(struct ieee80211vap *vap)
936178354Ssam{
937178354Ssam	struct ieee80211com *ic = vap->iv_ic;
938178354Ssam
939178354Ssam	IEEE80211_LOCK(ic);
940178354Ssam	ieee80211_wme_initparams_locked(vap);
941178354Ssam	IEEE80211_UNLOCK(ic);
942178354Ssam}
943178354Ssam
944138568Ssam/*
945138568Ssam * Update WME parameters for ourself and the BSS.
946138568Ssam */
947138568Ssamvoid
948178354Ssamieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
949138568Ssam{
950188863Ssam	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
951188780Ssam	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
952188780Ssam	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
953188780Ssam	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
954188780Ssam	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
955188780Ssam	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
956188780Ssam	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
957188780Ssam	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
958188780Ssam	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
959188782Ssam	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
960188782Ssam	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
961188780Ssam	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
962188780Ssam	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
963138568Ssam	};
964178354Ssam	struct ieee80211com *ic = vap->iv_ic;
965138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
966138568Ssam	const struct wmeParams *wmep;
967138568Ssam	struct wmeParams *chanp, *bssp;
968170530Ssam	enum ieee80211_phymode mode;
969138568Ssam	int i;
970138568Ssam
971188863Ssam       	/*
972188863Ssam	 * Set up the channel access parameters for the physical
973188863Ssam	 * device.  First populate the configured settings.
974188863Ssam	 */
975138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
976138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[i];
977138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
978138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
979138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
980138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
981138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
982138568Ssam
983138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
984138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
985138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
986138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
987138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
988138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
989138568Ssam	}
990138568Ssam
991138568Ssam	/*
992170530Ssam	 * Select mode; we can be called early in which case we
993170530Ssam	 * always use auto mode.  We know we'll be called when
994170530Ssam	 * entering the RUN state with bsschan setup properly
995170530Ssam	 * so state will eventually get set correctly
996170530Ssam	 */
997170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
998170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
999170530Ssam	else
1000170530Ssam		mode = IEEE80211_MODE_AUTO;
1001170530Ssam
1002170530Ssam	/*
1003138568Ssam	 * This implements agressive mode as found in certain
1004138568Ssam	 * vendors' AP's.  When there is significant high
1005138568Ssam	 * priority (VI/VO) traffic in the BSS throttle back BE
1006138568Ssam	 * traffic by using conservative parameters.  Otherwise
1007138568Ssam	 * BE uses agressive params to optimize performance of
1008138568Ssam	 * legacy/non-QoS traffic.
1009138568Ssam	 */
1010178354Ssam        if ((vap->iv_opmode == IEEE80211_M_HOSTAP &&
1011156524Ssam	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
1012178354Ssam	    (vap->iv_opmode == IEEE80211_M_STA &&
1013178354Ssam	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
1014178354Ssam	    (vap->iv_flags & IEEE80211_F_WME) == 0) {
1015138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1016138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1017138568Ssam
1018188863Ssam		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1019138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1020188863Ssam		    aggrParam[mode].logcwmin;
1021138568Ssam		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1022188863Ssam		    aggrParam[mode].logcwmax;
1023138568Ssam		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1024188863Ssam		    (vap->iv_flags & IEEE80211_F_BURST) ?
1025188863Ssam			aggrParam[mode].txopLimit : 0;
1026178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1027188863Ssam		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1028188863Ssam		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1029188863Ssam		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1030188863Ssam		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1031138568Ssam	}
1032138568Ssam
1033178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1034156524Ssam	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1035188780Ssam		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1036188780Ssam		    [IEEE80211_MODE_AUTO]	= 3,
1037188780Ssam		    [IEEE80211_MODE_11A]	= 3,
1038188780Ssam		    [IEEE80211_MODE_11B]	= 4,
1039188780Ssam		    [IEEE80211_MODE_11G]	= 3,
1040188780Ssam		    [IEEE80211_MODE_FH]		= 4,
1041188780Ssam		    [IEEE80211_MODE_TURBO_A]	= 3,
1042188780Ssam		    [IEEE80211_MODE_TURBO_G]	= 3,
1043188780Ssam		    [IEEE80211_MODE_STURBO_A]	= 3,
1044188782Ssam		    [IEEE80211_MODE_HALF]	= 3,
1045188782Ssam		    [IEEE80211_MODE_QUARTER]	= 3,
1046188780Ssam		    [IEEE80211_MODE_11NA]	= 3,
1047188780Ssam		    [IEEE80211_MODE_11NG]	= 3,
1048138568Ssam		};
1049138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1050138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1051138568Ssam
1052170530Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1053178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1054188863Ssam		    "update %s (chan+bss) logcwmin %u\n",
1055188863Ssam		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1056138568Ssam    	}
1057178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
1058138568Ssam		/*
1059138568Ssam		 * Arrange for a beacon update and bump the parameter
1060138568Ssam		 * set number so associated stations load the new values.
1061138568Ssam		 */
1062138568Ssam		wme->wme_bssChanParams.cap_info =
1063138568Ssam			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1064178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1065138568Ssam	}
1066138568Ssam
1067138568Ssam	wme->wme_update(ic);
1068138568Ssam
1069178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1070188863Ssam	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1071188863Ssam	    vap->iv_opmode == IEEE80211_M_STA ?
1072188863Ssam		wme->wme_wmeChanParams.cap_info :
1073188863Ssam		wme->wme_bssChanParams.cap_info);
1074138568Ssam}
1075138568Ssam
1076138568Ssamvoid
1077178354Ssamieee80211_wme_updateparams(struct ieee80211vap *vap)
1078138568Ssam{
1079178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1080138568Ssam
1081138568Ssam	if (ic->ic_caps & IEEE80211_C_WME) {
1082178354Ssam		IEEE80211_LOCK(ic);
1083178354Ssam		ieee80211_wme_updateparams_locked(vap);
1084178354Ssam		IEEE80211_UNLOCK(ic);
1085138568Ssam	}
1086138568Ssam}
1087138568Ssam
1088178354Ssamstatic void
1089178354Ssamparent_updown(void *arg, int npending)
1090178354Ssam{
1091178354Ssam	struct ifnet *parent = arg;
1092178354Ssam
1093178354Ssam	parent->if_ioctl(parent, SIOCSIFFLAGS, NULL);
1094178354Ssam}
1095178354Ssam
1096191746Sthompsastatic void
1097191746Sthompsaupdate_mcast(void *arg, int npending)
1098191746Sthompsa{
1099191746Sthompsa	struct ieee80211com *ic = arg;
1100191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1101191746Sthompsa
1102191746Sthompsa	ic->ic_update_mcast(parent);
1103191746Sthompsa}
1104191746Sthompsa
1105191746Sthompsastatic void
1106191746Sthompsaupdate_promisc(void *arg, int npending)
1107191746Sthompsa{
1108191746Sthompsa	struct ieee80211com *ic = arg;
1109191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1110191746Sthompsa
1111191746Sthompsa	ic->ic_update_promisc(parent);
1112191746Sthompsa}
1113191746Sthompsa
1114191746Sthompsastatic void
1115191746Sthompsaupdate_channel(void *arg, int npending)
1116191746Sthompsa{
1117191746Sthompsa	struct ieee80211com *ic = arg;
1118191746Sthompsa
1119191746Sthompsa	ic->ic_set_channel(ic);
1120192468Ssam	ieee80211_radiotap_chan_change(ic);
1121191746Sthompsa}
1122191746Sthompsa
1123170530Ssam/*
1124188533Sthompsa * Block until the parent is in a known state.  This is
1125188533Sthompsa * used after any operations that dispatch a task (e.g.
1126188533Sthompsa * to auto-configure the parent device up/down).
1127188533Sthompsa */
1128188533Sthompsavoid
1129188533Sthompsaieee80211_waitfor_parent(struct ieee80211com *ic)
1130188533Sthompsa{
1131191746Sthompsa	taskqueue_block(ic->ic_tq);
1132191746Sthompsa	ieee80211_draintask(ic, &ic->ic_parent_task);
1133191746Sthompsa	ieee80211_draintask(ic, &ic->ic_mcast_task);
1134191746Sthompsa	ieee80211_draintask(ic, &ic->ic_promisc_task);
1135191746Sthompsa	ieee80211_draintask(ic, &ic->ic_chan_task);
1136191746Sthompsa	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1137191746Sthompsa	taskqueue_unblock(ic->ic_tq);
1138188533Sthompsa}
1139188533Sthompsa
1140188533Sthompsa/*
1141178354Ssam * Start a vap running.  If this is the first vap to be
1142178354Ssam * set running on the underlying device then we
1143178354Ssam * automatically bring the device up.
1144170530Ssam */
1145178354Ssamvoid
1146178354Ssamieee80211_start_locked(struct ieee80211vap *vap)
1147170530Ssam{
1148178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1149178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1150178354Ssam	struct ifnet *parent = ic->ic_ifp;
1151170530Ssam
1152178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1153178354Ssam
1154178354Ssam	IEEE80211_DPRINTF(vap,
1155170530Ssam		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1156178354Ssam		"start running, %d vaps running\n", ic->ic_nrunning);
1157170530Ssam
1158178354Ssam	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1159178354Ssam		/*
1160178354Ssam		 * Mark us running.  Note that it's ok to do this first;
1161178354Ssam		 * if we need to bring the parent device up we defer that
1162178354Ssam		 * to avoid dropping the com lock.  We expect the device
1163178354Ssam		 * to respond to being marked up by calling back into us
1164178354Ssam		 * through ieee80211_start_all at which point we'll come
1165178354Ssam		 * back in here and complete the work.
1166178354Ssam		 */
1167178354Ssam		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1168178354Ssam		/*
1169178354Ssam		 * We are not running; if this we are the first vap
1170178354Ssam		 * to be brought up auto-up the parent if necessary.
1171178354Ssam		 */
1172178354Ssam		if (ic->ic_nrunning++ == 0 &&
1173178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1174178354Ssam			IEEE80211_DPRINTF(vap,
1175178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1176178354Ssam			    "%s: up parent %s\n", __func__, parent->if_xname);
1177178354Ssam			parent->if_flags |= IFF_UP;
1178191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1179178354Ssam			return;
1180178354Ssam		}
1181178354Ssam	}
1182170530Ssam	/*
1183178354Ssam	 * If the parent is up and running, then kick the
1184178354Ssam	 * 802.11 state machine as appropriate.
1185170530Ssam	 */
1186178354Ssam	if ((parent->if_drv_flags & IFF_DRV_RUNNING) &&
1187178354Ssam	    vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1188178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1189178354Ssam#if 0
1190178354Ssam			/* XXX bypasses scan too easily; disable for now */
1191178354Ssam			/*
1192178354Ssam			 * Try to be intelligent about clocking the state
1193178354Ssam			 * machine.  If we're currently in RUN state then
1194178354Ssam			 * we should be able to apply any new state/parameters
1195178354Ssam			 * simply by re-associating.  Otherwise we need to
1196178354Ssam			 * re-scan to select an appropriate ap.
1197178354Ssam			 */
1198178354Ssam			if (vap->iv_state >= IEEE80211_S_RUN)
1199178354Ssam				ieee80211_new_state_locked(vap,
1200178354Ssam				    IEEE80211_S_ASSOC, 1);
1201178354Ssam			else
1202178354Ssam#endif
1203178354Ssam				ieee80211_new_state_locked(vap,
1204178354Ssam				    IEEE80211_S_SCAN, 0);
1205170530Ssam		} else {
1206170530Ssam			/*
1207178354Ssam			 * For monitor+wds mode there's nothing to do but
1208178354Ssam			 * start running.  Otherwise if this is the first
1209170530Ssam			 * vap to be brought up, start a scan which may be
1210170530Ssam			 * preempted if the station is locked to a particular
1211170530Ssam			 * channel.
1212170530Ssam			 */
1213191746Sthompsa			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1214178354Ssam			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1215178354Ssam			    vap->iv_opmode == IEEE80211_M_WDS)
1216178354Ssam				ieee80211_new_state_locked(vap,
1217178354Ssam				    IEEE80211_S_RUN, -1);
1218178354Ssam			else
1219178354Ssam				ieee80211_new_state_locked(vap,
1220178354Ssam				    IEEE80211_S_SCAN, 0);
1221170530Ssam		}
1222170530Ssam	}
1223170530Ssam}
1224170530Ssam
1225170530Ssam/*
1226178354Ssam * Start a single vap.
1227178354Ssam */
1228178354Ssamvoid
1229178354Ssamieee80211_init(void *arg)
1230178354Ssam{
1231178354Ssam	struct ieee80211vap *vap = arg;
1232178354Ssam
1233193348Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1234193348Ssam	    "%s\n", __func__);
1235178354Ssam
1236193348Ssam	IEEE80211_LOCK(vap->iv_ic);
1237193348Ssam	ieee80211_start_locked(vap);
1238193348Ssam	IEEE80211_UNLOCK(vap->iv_ic);
1239178354Ssam}
1240178354Ssam
1241178354Ssam/*
1242178354Ssam * Start all runnable vap's on a device.
1243178354Ssam */
1244178354Ssamvoid
1245178354Ssamieee80211_start_all(struct ieee80211com *ic)
1246178354Ssam{
1247178354Ssam	struct ieee80211vap *vap;
1248178354Ssam
1249178354Ssam	IEEE80211_LOCK(ic);
1250178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1251178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1252178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1253178354Ssam			ieee80211_start_locked(vap);
1254178354Ssam	}
1255178354Ssam	IEEE80211_UNLOCK(ic);
1256178354Ssam}
1257178354Ssam
1258178354Ssam/*
1259178354Ssam * Stop a vap.  We force it down using the state machine
1260178354Ssam * then mark it's ifnet not running.  If this is the last
1261178354Ssam * vap running on the underlying device then we close it
1262178354Ssam * too to insure it will be properly initialized when the
1263178354Ssam * next vap is brought up.
1264178354Ssam */
1265178354Ssamvoid
1266178354Ssamieee80211_stop_locked(struct ieee80211vap *vap)
1267178354Ssam{
1268178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1269178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1270178354Ssam	struct ifnet *parent = ic->ic_ifp;
1271178354Ssam
1272178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1273178354Ssam
1274178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1275178354Ssam	    "stop running, %d vaps running\n", ic->ic_nrunning);
1276178354Ssam
1277178354Ssam	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1278178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1279178354Ssam		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1280178354Ssam		if (--ic->ic_nrunning == 0 &&
1281178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING)) {
1282178354Ssam			IEEE80211_DPRINTF(vap,
1283178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1284178354Ssam			    "down parent %s\n", parent->if_xname);
1285178354Ssam			parent->if_flags &= ~IFF_UP;
1286191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1287178354Ssam		}
1288178354Ssam	}
1289178354Ssam}
1290178354Ssam
1291178354Ssamvoid
1292178354Ssamieee80211_stop(struct ieee80211vap *vap)
1293178354Ssam{
1294178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1295178354Ssam
1296178354Ssam	IEEE80211_LOCK(ic);
1297178354Ssam	ieee80211_stop_locked(vap);
1298178354Ssam	IEEE80211_UNLOCK(ic);
1299178354Ssam}
1300178354Ssam
1301178354Ssam/*
1302178354Ssam * Stop all vap's running on a device.
1303178354Ssam */
1304178354Ssamvoid
1305178354Ssamieee80211_stop_all(struct ieee80211com *ic)
1306178354Ssam{
1307178354Ssam	struct ieee80211vap *vap;
1308178354Ssam
1309178354Ssam	IEEE80211_LOCK(ic);
1310178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1311178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1312178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1313178354Ssam			ieee80211_stop_locked(vap);
1314178354Ssam	}
1315178354Ssam	IEEE80211_UNLOCK(ic);
1316188533Sthompsa
1317188533Sthompsa	ieee80211_waitfor_parent(ic);
1318178354Ssam}
1319178354Ssam
1320178354Ssam/*
1321179391Ssam * Stop all vap's running on a device and arrange
1322179391Ssam * for those that were running to be resumed.
1323179391Ssam */
1324179391Ssamvoid
1325179391Ssamieee80211_suspend_all(struct ieee80211com *ic)
1326179391Ssam{
1327179391Ssam	struct ieee80211vap *vap;
1328179391Ssam
1329179391Ssam	IEEE80211_LOCK(ic);
1330179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1331179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1332179391Ssam		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
1333179391Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1334179391Ssam			ieee80211_stop_locked(vap);
1335179391Ssam		}
1336179391Ssam	}
1337179391Ssam	IEEE80211_UNLOCK(ic);
1338188533Sthompsa
1339188533Sthompsa	ieee80211_waitfor_parent(ic);
1340179391Ssam}
1341179391Ssam
1342179391Ssam/*
1343179391Ssam * Start all vap's marked for resume.
1344179391Ssam */
1345179391Ssamvoid
1346179391Ssamieee80211_resume_all(struct ieee80211com *ic)
1347179391Ssam{
1348179391Ssam	struct ieee80211vap *vap;
1349179391Ssam
1350179391Ssam	IEEE80211_LOCK(ic);
1351179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1352179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1353179391Ssam		if (!IFNET_IS_UP_RUNNING(ifp) &&
1354179391Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1355179391Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1356179391Ssam			ieee80211_start_locked(vap);
1357179391Ssam		}
1358179391Ssam	}
1359179391Ssam	IEEE80211_UNLOCK(ic);
1360179391Ssam}
1361179391Ssam
1362153349Ssamvoid
1363153349Ssamieee80211_beacon_miss(struct ieee80211com *ic)
1364153349Ssam{
1365191746Sthompsa	IEEE80211_LOCK(ic);
1366191746Sthompsa	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1367191746Sthompsa		/* Process in a taskq, the handler may reenter the driver */
1368191746Sthompsa		ieee80211_runtask(ic, &ic->ic_bmiss_task);
1369191746Sthompsa	}
1370191746Sthompsa	IEEE80211_UNLOCK(ic);
1371191746Sthompsa}
1372191746Sthompsa
1373191746Sthompsastatic void
1374191746Sthompsabeacon_miss(void *arg, int npending)
1375191746Sthompsa{
1376191746Sthompsa	struct ieee80211com *ic = arg;
1377178354Ssam	struct ieee80211vap *vap;
1378153349Ssam
1379178354Ssam	/* XXX locking */
1380178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1381153349Ssam		/*
1382178354Ssam		 * We only pass events through for sta vap's in RUN state;
1383178354Ssam		 * may be too restrictive but for now this saves all the
1384178354Ssam		 * handlers duplicating these checks.
1385153349Ssam		 */
1386178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1387193439Ssam		    vap->iv_state >= IEEE80211_S_RUN &&
1388178354Ssam		    vap->iv_bmiss != NULL)
1389178354Ssam			vap->iv_bmiss(vap);
1390153349Ssam	}
1391153349Ssam}
1392153349Ssam
1393191746Sthompsastatic void
1394191746Sthompsabeacon_swmiss(void *arg, int npending)
1395191746Sthompsa{
1396191746Sthompsa	struct ieee80211vap *vap = arg;
1397191746Sthompsa
1398191746Sthompsa	if (vap->iv_state != IEEE80211_S_RUN)
1399191746Sthompsa		return;
1400191746Sthompsa
1401191746Sthompsa	/* XXX Call multiple times if npending > zero? */
1402191746Sthompsa	vap->iv_bmiss(vap);
1403191746Sthompsa}
1404191746Sthompsa
1405154736Ssam/*
1406154736Ssam * Software beacon miss handling.  Check if any beacons
1407154736Ssam * were received in the last period.  If not post a
1408154736Ssam * beacon miss; otherwise reset the counter.
1409154736Ssam */
1410178354Ssamvoid
1411154736Ssamieee80211_swbmiss(void *arg)
1412154736Ssam{
1413178354Ssam	struct ieee80211vap *vap = arg;
1414179217Ssam	struct ieee80211com *ic = vap->iv_ic;
1415154736Ssam
1416179217Ssam	/* XXX sleep state? */
1417179217Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1418179217Ssam	    ("wrong state %d", vap->iv_state));
1419179217Ssam
1420179217Ssam	if (ic->ic_flags & IEEE80211_F_SCAN) {
1421179217Ssam		/*
1422179217Ssam		 * If scanning just ignore and reset state.  If we get a
1423179217Ssam		 * bmiss after coming out of scan because we haven't had
1424179217Ssam		 * time to receive a beacon then we should probe the AP
1425179217Ssam		 * before posting a real bmiss (unless iv_bmiss_max has
1426179217Ssam		 * been artifiically lowered).  A cleaner solution might
1427179217Ssam		 * be to disable the timer on scan start/end but to handle
1428179217Ssam		 * case of multiple sta vap's we'd need to disable the
1429179217Ssam		 * timers of all affected vap's.
1430179217Ssam		 */
1431179217Ssam		vap->iv_swbmiss_count = 0;
1432179217Ssam	} else if (vap->iv_swbmiss_count == 0) {
1433178354Ssam		if (vap->iv_bmiss != NULL)
1434191746Sthompsa			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1435178354Ssam		if (vap->iv_bmiss_count == 0)	/* don't re-arm timer */
1436154736Ssam			return;
1437154736Ssam	} else
1438178354Ssam		vap->iv_swbmiss_count = 0;
1439178354Ssam	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1440178354Ssam		ieee80211_swbmiss, vap);
1441154736Ssam}
1442154736Ssam
1443178354Ssam/*
1444178354Ssam * Start an 802.11h channel switch.  We record the parameters,
1445178354Ssam * mark the operation pending, notify each vap through the
1446178354Ssam * beacon update mechanism so it can update the beacon frame
1447178354Ssam * contents, and then switch vap's to CSA state to block outbound
1448178354Ssam * traffic.  Devices that handle CSA directly can use the state
1449178354Ssam * switch to do the right thing so long as they call
1450178354Ssam * ieee80211_csa_completeswitch when it's time to complete the
1451178354Ssam * channel change.  Devices that depend on the net80211 layer can
1452178354Ssam * use ieee80211_beacon_update to handle the countdown and the
1453178354Ssam * channel switch.
1454178354Ssam */
1455178354Ssamvoid
1456178354Ssamieee80211_csa_startswitch(struct ieee80211com *ic,
1457178354Ssam	struct ieee80211_channel *c, int mode, int count)
1458178354Ssam{
1459178354Ssam	struct ieee80211vap *vap;
1460178354Ssam
1461178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1462178354Ssam
1463178354Ssam	ic->ic_csa_newchan = c;
1464193439Ssam	ic->ic_csa_mode = mode;
1465178354Ssam	ic->ic_csa_count = count;
1466178354Ssam	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1467178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1468178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1469195618Srpaulo		    vap->iv_opmode == IEEE80211_M_IBSS ||
1470195618Srpaulo		    vap->iv_opmode == IEEE80211_M_MBSS)
1471178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1472178354Ssam		/* switch to CSA state to block outbound traffic */
1473178354Ssam		if (vap->iv_state == IEEE80211_S_RUN)
1474178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1475178354Ssam	}
1476178354Ssam	ieee80211_notify_csa(ic, c, mode, count);
1477178354Ssam}
1478178354Ssam
1479193439Ssamstatic void
1480193439Ssamcsa_completeswitch(struct ieee80211com *ic)
1481193439Ssam{
1482193439Ssam	struct ieee80211vap *vap;
1483193439Ssam
1484193439Ssam	ic->ic_csa_newchan = NULL;
1485193439Ssam	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1486193439Ssam
1487193439Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1488193439Ssam		if (vap->iv_state == IEEE80211_S_CSA)
1489193439Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1490193439Ssam}
1491193439Ssam
1492178354Ssam/*
1493178354Ssam * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1494178354Ssam * We clear state and move all vap's in CSA state to RUN state
1495178354Ssam * so they can again transmit.
1496178354Ssam */
1497178354Ssamvoid
1498178354Ssamieee80211_csa_completeswitch(struct ieee80211com *ic)
1499178354Ssam{
1500178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1501178354Ssam
1502178354Ssam	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1503178354Ssam
1504178354Ssam	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1505193439Ssam	csa_completeswitch(ic);
1506193439Ssam}
1507178354Ssam
1508193439Ssam/*
1509193439Ssam * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1510193439Ssam * We clear state and move all vap's in CSA state to RUN state
1511193439Ssam * so they can again transmit.
1512193439Ssam */
1513193439Ssamvoid
1514193439Ssamieee80211_csa_cancelswitch(struct ieee80211com *ic)
1515193439Ssam{
1516193439Ssam	IEEE80211_LOCK_ASSERT(ic);
1517193439Ssam
1518193439Ssam	csa_completeswitch(ic);
1519178354Ssam}
1520178354Ssam
1521178354Ssam/*
1522178354Ssam * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1523178354Ssam * We clear state and move all vap's in CAC state to RUN state.
1524178354Ssam */
1525178354Ssamvoid
1526178354Ssamieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1527178354Ssam{
1528178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1529178354Ssam	struct ieee80211vap *vap;
1530178354Ssam
1531178354Ssam	IEEE80211_LOCK(ic);
1532178354Ssam	/*
1533178354Ssam	 * Complete CAC state change for lead vap first; then
1534178354Ssam	 * clock all the other vap's waiting.
1535178354Ssam	 */
1536178354Ssam	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1537178354Ssam	    ("wrong state %d", vap0->iv_state));
1538178354Ssam	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1539178354Ssam
1540178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1541178354Ssam		if (vap->iv_state == IEEE80211_S_CAC)
1542178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1543178354Ssam	IEEE80211_UNLOCK(ic);
1544178354Ssam}
1545178354Ssam
1546178354Ssam/*
1547178354Ssam * Force all vap's other than the specified vap to the INIT state
1548178354Ssam * and mark them as waiting for a scan to complete.  These vaps
1549178354Ssam * will be brought up when the scan completes and the scanning vap
1550178354Ssam * reaches RUN state by wakeupwaiting.
1551178354Ssam */
1552154736Ssamstatic void
1553178354Ssammarkwaiting(struct ieee80211vap *vap0)
1554147765Ssam{
1555178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1556178354Ssam	struct ieee80211vap *vap;
1557147765Ssam
1558178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1559178354Ssam
1560191746Sthompsa	/*
1561191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1562191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1563191746Sthompsa	 * change task.
1564191746Sthompsa	 */
1565178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1566178354Ssam		if (vap == vap0)
1567178354Ssam			continue;
1568178354Ssam		if (vap->iv_state != IEEE80211_S_INIT) {
1569191746Sthompsa			/* NB: iv_newstate may drop the lock */
1570178354Ssam			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1571178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1572178354Ssam		}
1573147765Ssam	}
1574147765Ssam}
1575147765Ssam
1576178354Ssam/*
1577178354Ssam * Wakeup all vap's waiting for a scan to complete.  This is the
1578178354Ssam * companion to markwaiting (above) and is used to coordinate
1579178354Ssam * multiple vaps scanning.
1580191746Sthompsa * This is called from the state taskqueue.
1581178354Ssam */
1582147765Ssamstatic void
1583178354Ssamwakeupwaiting(struct ieee80211vap *vap0)
1584147765Ssam{
1585178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1586178354Ssam	struct ieee80211vap *vap;
1587147765Ssam
1588178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1589178354Ssam
1590191746Sthompsa	/*
1591191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1592191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1593191746Sthompsa	 * change task.
1594191746Sthompsa	 */
1595178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1596178354Ssam		if (vap == vap0)
1597178354Ssam			continue;
1598178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1599178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1600178354Ssam			/* NB: sta's cannot go INIT->RUN */
1601191746Sthompsa			/* NB: iv_newstate may drop the lock */
1602178354Ssam			vap->iv_newstate(vap,
1603178354Ssam			    vap->iv_opmode == IEEE80211_M_STA ?
1604178354Ssam			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1605178354Ssam		}
1606178354Ssam	}
1607147765Ssam}
1608147765Ssam
1609170530Ssam/*
1610178354Ssam * Handle post state change work common to all operating modes.
1611170530Ssam */
1612170530Ssamstatic void
1613191746Sthompsaieee80211_newstate_cb(void *xvap, int npending)
1614170530Ssam{
1615191746Sthompsa	struct ieee80211vap *vap = xvap;
1616178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1617191746Sthompsa	enum ieee80211_state nstate, ostate;
1618191746Sthompsa	int arg, rc;
1619178354Ssam
1620191746Sthompsa	IEEE80211_LOCK(ic);
1621191746Sthompsa	nstate = vap->iv_nstate;
1622191746Sthompsa	arg = vap->iv_nstate_arg;
1623178354Ssam
1624191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1625191746Sthompsa		/*
1626191746Sthompsa		 * We have been requested to drop back to the INIT before
1627191746Sthompsa		 * proceeding to the new state.
1628191746Sthompsa		 */
1629191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1630191746Sthompsa		    "%s: %s -> %s arg %d\n", __func__,
1631191746Sthompsa		    ieee80211_state_name[vap->iv_state],
1632191746Sthompsa		    ieee80211_state_name[IEEE80211_S_INIT], arg);
1633191746Sthompsa		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1634191746Sthompsa		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1635191746Sthompsa	}
1636191746Sthompsa
1637191746Sthompsa	ostate = vap->iv_state;
1638191746Sthompsa	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1639191746Sthompsa		/*
1640191746Sthompsa		 * SCAN was forced; e.g. on beacon miss.  Force other running
1641191746Sthompsa		 * vap's to INIT state and mark them as waiting for the scan to
1642191746Sthompsa		 * complete.  This insures they don't interfere with our
1643191746Sthompsa		 * scanning.  Since we are single threaded the vaps can not
1644191746Sthompsa		 * transition again while we are executing.
1645191746Sthompsa		 *
1646191746Sthompsa		 * XXX not always right, assumes ap follows sta
1647191746Sthompsa		 */
1648191746Sthompsa		markwaiting(vap);
1649191746Sthompsa	}
1650178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1651191746Sthompsa	    "%s: %s -> %s arg %d\n", __func__,
1652191746Sthompsa	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1653178354Ssam
1654191746Sthompsa	rc = vap->iv_newstate(vap, nstate, arg);
1655191746Sthompsa	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1656191746Sthompsa	if (rc != 0) {
1657191746Sthompsa		/* State transition failed */
1658191746Sthompsa		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1659191746Sthompsa		KASSERT(nstate != IEEE80211_S_INIT,
1660191746Sthompsa		    ("INIT state change failed"));
1661191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1662191746Sthompsa		    "%s: %s returned error %d\n", __func__,
1663191746Sthompsa		    ieee80211_state_name[nstate], rc);
1664191746Sthompsa		goto done;
1665191746Sthompsa	}
1666191746Sthompsa
1667191746Sthompsa	/* No actual transition, skip post processing */
1668191746Sthompsa	if (ostate == nstate)
1669191746Sthompsa		goto done;
1670191746Sthompsa
1671178354Ssam	if (nstate == IEEE80211_S_RUN) {
1672178354Ssam		/*
1673178354Ssam		 * OACTIVE may be set on the vap if the upper layer
1674178354Ssam		 * tried to transmit (e.g. IPv6 NDP) before we reach
1675178354Ssam		 * RUN state.  Clear it and restart xmit.
1676178354Ssam		 *
1677178354Ssam		 * Note this can also happen as a result of SLEEP->RUN
1678178354Ssam		 * (i.e. coming out of power save mode).
1679178354Ssam		 */
1680178354Ssam		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1681178354Ssam		if_start(vap->iv_ifp);
1682178354Ssam
1683178354Ssam		/* bring up any vaps waiting on us */
1684178354Ssam		wakeupwaiting(vap);
1685178354Ssam	} else if (nstate == IEEE80211_S_INIT) {
1686178354Ssam		/*
1687178354Ssam		 * Flush the scan cache if we did the last scan (XXX?)
1688178354Ssam		 * and flush any frames on send queues from this vap.
1689178354Ssam		 * Note the mgt q is used only for legacy drivers and
1690178354Ssam		 * will go away shortly.
1691178354Ssam		 */
1692178354Ssam		ieee80211_scan_flush(vap);
1693178354Ssam
1694178354Ssam		/* XXX NB: cast for altq */
1695178354Ssam		ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1696170530Ssam	}
1697191746Sthompsadone:
1698191746Sthompsa	IEEE80211_UNLOCK(ic);
1699170530Ssam}
1700170530Ssam
1701178354Ssam/*
1702178354Ssam * Public interface for initiating a state machine change.
1703178354Ssam * This routine single-threads the request and coordinates
1704178354Ssam * the scheduling of multiple vaps for the purpose of selecting
1705178354Ssam * an operating channel.  Specifically the following scenarios
1706178354Ssam * are handled:
1707178354Ssam * o only one vap can be selecting a channel so on transition to
1708178354Ssam *   SCAN state if another vap is already scanning then
1709178354Ssam *   mark the caller for later processing and return without
1710178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1711178354Ssam * o only one vap can be doing CAC of a channel so on transition to
1712178354Ssam *   CAC state if another vap is already scanning for radar then
1713178354Ssam *   mark the caller for later processing and return without
1714178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1715178354Ssam * o if another vap is already running when a request is made
1716178354Ssam *   to SCAN then an operating channel has been chosen; bypass
1717178354Ssam *   the scan and just join the channel
1718178354Ssam *
1719178354Ssam * Note that the state change call is done through the iv_newstate
1720178354Ssam * method pointer so any driver routine gets invoked.  The driver
1721178354Ssam * will normally call back into operating mode-specific
1722178354Ssam * ieee80211_newstate routines (below) unless it needs to completely
1723178354Ssam * bypass the state machine (e.g. because the firmware has it's
1724178354Ssam * own idea how things should work).  Bypassing the net80211 layer
1725178354Ssam * is usually a mistake and indicates lack of proper integration
1726178354Ssam * with the net80211 layer.
1727178354Ssam */
1728117811Ssamstatic int
1729178354Ssamieee80211_new_state_locked(struct ieee80211vap *vap,
1730178354Ssam	enum ieee80211_state nstate, int arg)
1731116742Ssam{
1732178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1733178354Ssam	struct ieee80211vap *vp;
1734117811Ssam	enum ieee80211_state ostate;
1735191746Sthompsa	int nrunning, nscanning;
1736116742Ssam
1737178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1738178354Ssam
1739191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1740191746Sthompsa		if (vap->iv_nstate == IEEE80211_S_INIT) {
1741191746Sthompsa			/*
1742191746Sthompsa			 * XXX The vap is being stopped, do no allow any other
1743191746Sthompsa			 * state changes until this is completed.
1744191746Sthompsa			 */
1745191746Sthompsa			return -1;
1746191768Sthompsa		} else if (vap->iv_state != vap->iv_nstate) {
1747191746Sthompsa#if 0
1748191768Sthompsa			/* Warn if the previous state hasn't completed. */
1749191768Sthompsa			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1750191768Sthompsa			    "%s: pending %s -> %s transition lost\n", __func__,
1751191768Sthompsa			    ieee80211_state_name[vap->iv_state],
1752191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1753191746Sthompsa#else
1754191768Sthompsa			/* XXX temporarily enable to identify issues */
1755191768Sthompsa			if_printf(vap->iv_ifp,
1756191768Sthompsa			    "%s: pending %s -> %s transition lost\n",
1757191768Sthompsa			    __func__, ieee80211_state_name[vap->iv_state],
1758191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1759191746Sthompsa#endif
1760191768Sthompsa		}
1761191746Sthompsa	}
1762191746Sthompsa
1763178354Ssam	nrunning = nscanning = 0;
1764178354Ssam	/* XXX can track this state instead of calculating */
1765178354Ssam	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1766178354Ssam		if (vp != vap) {
1767178354Ssam			if (vp->iv_state >= IEEE80211_S_RUN)
1768178354Ssam				nrunning++;
1769178354Ssam			/* XXX doesn't handle bg scan */
1770178354Ssam			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1771178354Ssam			else if (vp->iv_state > IEEE80211_S_INIT)
1772178354Ssam				nscanning++;
1773178354Ssam		}
1774178354Ssam	}
1775178354Ssam	ostate = vap->iv_state;
1776178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1777178354Ssam	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1778178354Ssam	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1779178354Ssam	    nrunning, nscanning);
1780116742Ssam	switch (nstate) {
1781116742Ssam	case IEEE80211_S_SCAN:
1782178354Ssam		if (ostate == IEEE80211_S_INIT) {
1783178354Ssam			/*
1784178354Ssam			 * INIT -> SCAN happens on initial bringup.
1785178354Ssam			 */
1786178354Ssam			KASSERT(!(nscanning && nrunning),
1787178354Ssam			    ("%d scanning and %d running", nscanning, nrunning));
1788178354Ssam			if (nscanning) {
1789116742Ssam				/*
1790178354Ssam				 * Someone is scanning, defer our state
1791178354Ssam				 * change until the work has completed.
1792116742Ssam				 */
1793178354Ssam				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1794178354Ssam				    "%s: defer %s -> %s\n",
1795178354Ssam				    __func__, ieee80211_state_name[ostate],
1796178354Ssam				    ieee80211_state_name[nstate]);
1797178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1798191746Sthompsa				return 0;
1799116742Ssam			}
1800178354Ssam			if (nrunning) {
1801170530Ssam				/*
1802178354Ssam				 * Someone is operating; just join the channel
1803178354Ssam				 * they have chosen.
1804170530Ssam				 */
1805178354Ssam				/* XXX kill arg? */
1806178354Ssam				/* XXX check each opmode, adhoc? */
1807178354Ssam				if (vap->iv_opmode == IEEE80211_M_STA)
1808178354Ssam					nstate = IEEE80211_S_SCAN;
1809178354Ssam				else
1810178354Ssam					nstate = IEEE80211_S_RUN;
1811138568Ssam#ifdef IEEE80211_DEBUG
1812178354Ssam				if (nstate != IEEE80211_S_SCAN) {
1813178354Ssam					IEEE80211_DPRINTF(vap,
1814178354Ssam					    IEEE80211_MSG_STATE,
1815178354Ssam					    "%s: override, now %s -> %s\n",
1816178354Ssam					    __func__,
1817178354Ssam					    ieee80211_state_name[ostate],
1818178354Ssam					    ieee80211_state_name[nstate]);
1819178354Ssam				}
1820138568Ssam#endif
1821170530Ssam			}
1822116742Ssam		}
1823178354Ssam		break;
1824178354Ssam	case IEEE80211_S_RUN:
1825178354Ssam		if (vap->iv_opmode == IEEE80211_M_WDS &&
1826178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1827178354Ssam		    nscanning) {
1828154736Ssam			/*
1829178354Ssam			 * Legacy WDS with someone else scanning; don't
1830178354Ssam			 * go online until that completes as we should
1831178354Ssam			 * follow the other vap to the channel they choose.
1832154736Ssam			 */
1833178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1834178354Ssam			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
1835178354Ssam			     ieee80211_state_name[ostate],
1836178354Ssam			     ieee80211_state_name[nstate]);
1837178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1838191746Sthompsa			return 0;
1839154736Ssam		}
1840178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1841178354Ssam		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1842178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1843178354Ssam		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1844178354Ssam			/*
1845178354Ssam			 * This is a DFS channel, transition to CAC state
1846178354Ssam			 * instead of RUN.  This allows us to initiate
1847178354Ssam			 * Channel Availability Check (CAC) as specified
1848178354Ssam			 * by 11h/DFS.
1849178354Ssam			 */
1850178354Ssam			nstate = IEEE80211_S_CAC;
1851178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1852178354Ssam			     "%s: override %s -> %s (DFS)\n", __func__,
1853178354Ssam			     ieee80211_state_name[ostate],
1854178354Ssam			     ieee80211_state_name[nstate]);
1855138568Ssam		}
1856116742Ssam		break;
1857178354Ssam	case IEEE80211_S_INIT:
1858191955Sthompsa		/* cancel any scan in progress */
1859191955Sthompsa		ieee80211_cancel_scan(vap);
1860178354Ssam		if (ostate == IEEE80211_S_INIT ) {
1861178354Ssam			/* XXX don't believe this */
1862178354Ssam			/* INIT -> INIT. nothing to do */
1863178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1864178354Ssam		}
1865178354Ssam		/* fall thru... */
1866172058Ssam	default:
1867172058Ssam		break;
1868116742Ssam	}
1869191746Sthompsa	/* defer the state change to a thread */
1870191746Sthompsa	vap->iv_nstate = nstate;
1871191746Sthompsa	vap->iv_nstate_arg = arg;
1872191746Sthompsa	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1873191746Sthompsa	ieee80211_runtask(ic, &vap->iv_nstate_task);
1874191746Sthompsa	return EINPROGRESS;
1875116742Ssam}
1876178354Ssam
1877178354Ssamint
1878178354Ssamieee80211_new_state(struct ieee80211vap *vap,
1879178354Ssam	enum ieee80211_state nstate, int arg)
1880178354Ssam{
1881178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1882178354Ssam	int rc;
1883178354Ssam
1884178354Ssam	IEEE80211_LOCK(ic);
1885178354Ssam	rc = ieee80211_new_state_locked(vap, nstate, arg);
1886178354Ssam	IEEE80211_UNLOCK(ic);
1887178354Ssam	return rc;
1888178354Ssam}
1889