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$");
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",
67218927Sbschmidt	"deauth",	"action",	"action_noack",	"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	/*
899219961Sadrian	 * Clear the wme cap_info field so a qoscount from a previous
900219961Sadrian	 * vap doesn't confuse later code which only parses the beacon
901219961Sadrian	 * field and updates hardware when said field changes.
902219961Sadrian	 * Otherwise the hardware is programmed with defaults, not what
903219961Sadrian	 * the beacon actually announces.
904219961Sadrian	 */
905219961Sadrian	wme->wme_wmeChanParams.cap_info = 0;
906219961Sadrian
907219961Sadrian	/*
908170530Ssam	 * Select mode; we can be called early in which case we
909170530Ssam	 * always use auto mode.  We know we'll be called when
910170530Ssam	 * entering the RUN state with bsschan setup properly
911170530Ssam	 * so state will eventually get set correctly
912170530Ssam	 */
913170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
914170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
915170530Ssam	else
916170530Ssam		mode = IEEE80211_MODE_AUTO;
917138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
918138568Ssam		switch (i) {
919138568Ssam		case WME_AC_BK:
920170530Ssam			pPhyParam = &phyParamForAC_BK[mode];
921170530Ssam			pBssPhyParam = &phyParamForAC_BK[mode];
922138568Ssam			break;
923138568Ssam		case WME_AC_VI:
924170530Ssam			pPhyParam = &phyParamForAC_VI[mode];
925170530Ssam			pBssPhyParam = &bssPhyParamForAC_VI[mode];
926138568Ssam			break;
927138568Ssam		case WME_AC_VO:
928170530Ssam			pPhyParam = &phyParamForAC_VO[mode];
929170530Ssam			pBssPhyParam = &bssPhyParamForAC_VO[mode];
930138568Ssam			break;
931138568Ssam		case WME_AC_BE:
932138568Ssam		default:
933170530Ssam			pPhyParam = &phyParamForAC_BE[mode];
934170530Ssam			pBssPhyParam = &bssPhyParamForAC_BE[mode];
935138568Ssam			break;
936138568Ssam		}
937138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
938138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
939188863Ssam			setwmeparams(vap, "chan", i, wmep, pPhyParam);
940138568Ssam		} else {
941188863Ssam			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
942138568Ssam		}
943138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
944188863Ssam		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
945138568Ssam	}
946138568Ssam	/* NB: check ic_bss to avoid NULL deref on initial attach */
947178354Ssam	if (vap->iv_bss != NULL) {
948138568Ssam		/*
949138568Ssam		 * Calculate agressive mode switching threshold based
950138568Ssam		 * on beacon interval.  This doesn't need locking since
951138568Ssam		 * we're only called before entering the RUN state at
952138568Ssam		 * which point we start sending beacon frames.
953138568Ssam		 */
954138568Ssam		wme->wme_hipri_switch_thresh =
955178354Ssam			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
956188864Ssam		wme->wme_flags &= ~WME_F_AGGRMODE;
957178354Ssam		ieee80211_wme_updateparams(vap);
958138568Ssam	}
959138568Ssam}
960138568Ssam
961178354Ssamvoid
962178354Ssamieee80211_wme_initparams(struct ieee80211vap *vap)
963178354Ssam{
964178354Ssam	struct ieee80211com *ic = vap->iv_ic;
965178354Ssam
966178354Ssam	IEEE80211_LOCK(ic);
967178354Ssam	ieee80211_wme_initparams_locked(vap);
968178354Ssam	IEEE80211_UNLOCK(ic);
969178354Ssam}
970178354Ssam
971138568Ssam/*
972138568Ssam * Update WME parameters for ourself and the BSS.
973138568Ssam */
974138568Ssamvoid
975178354Ssamieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
976138568Ssam{
977188863Ssam	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
978188780Ssam	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
979188780Ssam	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
980188780Ssam	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
981188780Ssam	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
982188780Ssam	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
983188780Ssam	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
984188780Ssam	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
985188780Ssam	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
986188782Ssam	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
987188782Ssam	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
988188780Ssam	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
989188780Ssam	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
990138568Ssam	};
991178354Ssam	struct ieee80211com *ic = vap->iv_ic;
992138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
993138568Ssam	const struct wmeParams *wmep;
994138568Ssam	struct wmeParams *chanp, *bssp;
995170530Ssam	enum ieee80211_phymode mode;
996138568Ssam	int i;
997138568Ssam
998188863Ssam       	/*
999188863Ssam	 * Set up the channel access parameters for the physical
1000188863Ssam	 * device.  First populate the configured settings.
1001188863Ssam	 */
1002138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1003138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[i];
1004138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1005138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
1006138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1007138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1008138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1009138568Ssam
1010138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1011138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1012138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
1013138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1014138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1015138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1016138568Ssam	}
1017138568Ssam
1018138568Ssam	/*
1019170530Ssam	 * Select mode; we can be called early in which case we
1020170530Ssam	 * always use auto mode.  We know we'll be called when
1021170530Ssam	 * entering the RUN state with bsschan setup properly
1022170530Ssam	 * so state will eventually get set correctly
1023170530Ssam	 */
1024170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1025170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
1026170530Ssam	else
1027170530Ssam		mode = IEEE80211_MODE_AUTO;
1028170530Ssam
1029170530Ssam	/*
1030138568Ssam	 * This implements agressive mode as found in certain
1031138568Ssam	 * vendors' AP's.  When there is significant high
1032138568Ssam	 * priority (VI/VO) traffic in the BSS throttle back BE
1033138568Ssam	 * traffic by using conservative parameters.  Otherwise
1034138568Ssam	 * BE uses agressive params to optimize performance of
1035138568Ssam	 * legacy/non-QoS traffic.
1036138568Ssam	 */
1037178354Ssam        if ((vap->iv_opmode == IEEE80211_M_HOSTAP &&
1038156524Ssam	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
1039178354Ssam	    (vap->iv_opmode == IEEE80211_M_STA &&
1040178354Ssam	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
1041178354Ssam	    (vap->iv_flags & IEEE80211_F_WME) == 0) {
1042138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1043138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1044138568Ssam
1045188863Ssam		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1046138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1047188863Ssam		    aggrParam[mode].logcwmin;
1048138568Ssam		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1049188863Ssam		    aggrParam[mode].logcwmax;
1050138568Ssam		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1051188863Ssam		    (vap->iv_flags & IEEE80211_F_BURST) ?
1052188863Ssam			aggrParam[mode].txopLimit : 0;
1053178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1054188863Ssam		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1055188863Ssam		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1056188863Ssam		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1057188863Ssam		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1058138568Ssam	}
1059138568Ssam
1060178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1061156524Ssam	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1062188780Ssam		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1063188780Ssam		    [IEEE80211_MODE_AUTO]	= 3,
1064188780Ssam		    [IEEE80211_MODE_11A]	= 3,
1065188780Ssam		    [IEEE80211_MODE_11B]	= 4,
1066188780Ssam		    [IEEE80211_MODE_11G]	= 3,
1067188780Ssam		    [IEEE80211_MODE_FH]		= 4,
1068188780Ssam		    [IEEE80211_MODE_TURBO_A]	= 3,
1069188780Ssam		    [IEEE80211_MODE_TURBO_G]	= 3,
1070188780Ssam		    [IEEE80211_MODE_STURBO_A]	= 3,
1071188782Ssam		    [IEEE80211_MODE_HALF]	= 3,
1072188782Ssam		    [IEEE80211_MODE_QUARTER]	= 3,
1073188780Ssam		    [IEEE80211_MODE_11NA]	= 3,
1074188780Ssam		    [IEEE80211_MODE_11NG]	= 3,
1075138568Ssam		};
1076138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1077138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1078138568Ssam
1079170530Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1080178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1081188863Ssam		    "update %s (chan+bss) logcwmin %u\n",
1082188863Ssam		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1083138568Ssam    	}
1084178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
1085138568Ssam		/*
1086138568Ssam		 * Arrange for a beacon update and bump the parameter
1087138568Ssam		 * set number so associated stations load the new values.
1088138568Ssam		 */
1089138568Ssam		wme->wme_bssChanParams.cap_info =
1090138568Ssam			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1091178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1092138568Ssam	}
1093138568Ssam
1094138568Ssam	wme->wme_update(ic);
1095138568Ssam
1096178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1097188863Ssam	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1098188863Ssam	    vap->iv_opmode == IEEE80211_M_STA ?
1099188863Ssam		wme->wme_wmeChanParams.cap_info :
1100188863Ssam		wme->wme_bssChanParams.cap_info);
1101138568Ssam}
1102138568Ssam
1103138568Ssamvoid
1104178354Ssamieee80211_wme_updateparams(struct ieee80211vap *vap)
1105138568Ssam{
1106178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1107138568Ssam
1108138568Ssam	if (ic->ic_caps & IEEE80211_C_WME) {
1109178354Ssam		IEEE80211_LOCK(ic);
1110178354Ssam		ieee80211_wme_updateparams_locked(vap);
1111178354Ssam		IEEE80211_UNLOCK(ic);
1112138568Ssam	}
1113138568Ssam}
1114138568Ssam
1115178354Ssamstatic void
1116178354Ssamparent_updown(void *arg, int npending)
1117178354Ssam{
1118178354Ssam	struct ifnet *parent = arg;
1119178354Ssam
1120178354Ssam	parent->if_ioctl(parent, SIOCSIFFLAGS, NULL);
1121178354Ssam}
1122178354Ssam
1123191746Sthompsastatic void
1124191746Sthompsaupdate_mcast(void *arg, int npending)
1125191746Sthompsa{
1126191746Sthompsa	struct ieee80211com *ic = arg;
1127191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1128191746Sthompsa
1129191746Sthompsa	ic->ic_update_mcast(parent);
1130191746Sthompsa}
1131191746Sthompsa
1132191746Sthompsastatic void
1133191746Sthompsaupdate_promisc(void *arg, int npending)
1134191746Sthompsa{
1135191746Sthompsa	struct ieee80211com *ic = arg;
1136191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1137191746Sthompsa
1138191746Sthompsa	ic->ic_update_promisc(parent);
1139191746Sthompsa}
1140191746Sthompsa
1141191746Sthompsastatic void
1142191746Sthompsaupdate_channel(void *arg, int npending)
1143191746Sthompsa{
1144191746Sthompsa	struct ieee80211com *ic = arg;
1145191746Sthompsa
1146191746Sthompsa	ic->ic_set_channel(ic);
1147192468Ssam	ieee80211_radiotap_chan_change(ic);
1148191746Sthompsa}
1149191746Sthompsa
1150170530Ssam/*
1151188533Sthompsa * Block until the parent is in a known state.  This is
1152188533Sthompsa * used after any operations that dispatch a task (e.g.
1153188533Sthompsa * to auto-configure the parent device up/down).
1154188533Sthompsa */
1155188533Sthompsavoid
1156188533Sthompsaieee80211_waitfor_parent(struct ieee80211com *ic)
1157188533Sthompsa{
1158191746Sthompsa	taskqueue_block(ic->ic_tq);
1159191746Sthompsa	ieee80211_draintask(ic, &ic->ic_parent_task);
1160191746Sthompsa	ieee80211_draintask(ic, &ic->ic_mcast_task);
1161191746Sthompsa	ieee80211_draintask(ic, &ic->ic_promisc_task);
1162191746Sthompsa	ieee80211_draintask(ic, &ic->ic_chan_task);
1163191746Sthompsa	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1164191746Sthompsa	taskqueue_unblock(ic->ic_tq);
1165188533Sthompsa}
1166188533Sthompsa
1167188533Sthompsa/*
1168178354Ssam * Start a vap running.  If this is the first vap to be
1169178354Ssam * set running on the underlying device then we
1170178354Ssam * automatically bring the device up.
1171170530Ssam */
1172178354Ssamvoid
1173178354Ssamieee80211_start_locked(struct ieee80211vap *vap)
1174170530Ssam{
1175178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1176178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1177178354Ssam	struct ifnet *parent = ic->ic_ifp;
1178170530Ssam
1179178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1180178354Ssam
1181178354Ssam	IEEE80211_DPRINTF(vap,
1182170530Ssam		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1183178354Ssam		"start running, %d vaps running\n", ic->ic_nrunning);
1184170530Ssam
1185178354Ssam	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1186178354Ssam		/*
1187178354Ssam		 * Mark us running.  Note that it's ok to do this first;
1188178354Ssam		 * if we need to bring the parent device up we defer that
1189178354Ssam		 * to avoid dropping the com lock.  We expect the device
1190178354Ssam		 * to respond to being marked up by calling back into us
1191178354Ssam		 * through ieee80211_start_all at which point we'll come
1192178354Ssam		 * back in here and complete the work.
1193178354Ssam		 */
1194178354Ssam		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1195178354Ssam		/*
1196178354Ssam		 * We are not running; if this we are the first vap
1197178354Ssam		 * to be brought up auto-up the parent if necessary.
1198178354Ssam		 */
1199178354Ssam		if (ic->ic_nrunning++ == 0 &&
1200178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1201178354Ssam			IEEE80211_DPRINTF(vap,
1202178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1203178354Ssam			    "%s: up parent %s\n", __func__, parent->if_xname);
1204178354Ssam			parent->if_flags |= IFF_UP;
1205191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1206178354Ssam			return;
1207178354Ssam		}
1208178354Ssam	}
1209170530Ssam	/*
1210178354Ssam	 * If the parent is up and running, then kick the
1211178354Ssam	 * 802.11 state machine as appropriate.
1212170530Ssam	 */
1213178354Ssam	if ((parent->if_drv_flags & IFF_DRV_RUNNING) &&
1214178354Ssam	    vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1215178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1216178354Ssam#if 0
1217178354Ssam			/* XXX bypasses scan too easily; disable for now */
1218178354Ssam			/*
1219178354Ssam			 * Try to be intelligent about clocking the state
1220178354Ssam			 * machine.  If we're currently in RUN state then
1221178354Ssam			 * we should be able to apply any new state/parameters
1222178354Ssam			 * simply by re-associating.  Otherwise we need to
1223178354Ssam			 * re-scan to select an appropriate ap.
1224178354Ssam			 */
1225178354Ssam			if (vap->iv_state >= IEEE80211_S_RUN)
1226178354Ssam				ieee80211_new_state_locked(vap,
1227178354Ssam				    IEEE80211_S_ASSOC, 1);
1228178354Ssam			else
1229178354Ssam#endif
1230178354Ssam				ieee80211_new_state_locked(vap,
1231178354Ssam				    IEEE80211_S_SCAN, 0);
1232170530Ssam		} else {
1233170530Ssam			/*
1234178354Ssam			 * For monitor+wds mode there's nothing to do but
1235178354Ssam			 * start running.  Otherwise if this is the first
1236170530Ssam			 * vap to be brought up, start a scan which may be
1237170530Ssam			 * preempted if the station is locked to a particular
1238170530Ssam			 * channel.
1239170530Ssam			 */
1240191746Sthompsa			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1241178354Ssam			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1242178354Ssam			    vap->iv_opmode == IEEE80211_M_WDS)
1243178354Ssam				ieee80211_new_state_locked(vap,
1244178354Ssam				    IEEE80211_S_RUN, -1);
1245178354Ssam			else
1246178354Ssam				ieee80211_new_state_locked(vap,
1247178354Ssam				    IEEE80211_S_SCAN, 0);
1248170530Ssam		}
1249170530Ssam	}
1250170530Ssam}
1251170530Ssam
1252170530Ssam/*
1253178354Ssam * Start a single vap.
1254178354Ssam */
1255178354Ssamvoid
1256178354Ssamieee80211_init(void *arg)
1257178354Ssam{
1258178354Ssam	struct ieee80211vap *vap = arg;
1259178354Ssam
1260193348Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1261193348Ssam	    "%s\n", __func__);
1262178354Ssam
1263193348Ssam	IEEE80211_LOCK(vap->iv_ic);
1264193348Ssam	ieee80211_start_locked(vap);
1265193348Ssam	IEEE80211_UNLOCK(vap->iv_ic);
1266178354Ssam}
1267178354Ssam
1268178354Ssam/*
1269178354Ssam * Start all runnable vap's on a device.
1270178354Ssam */
1271178354Ssamvoid
1272178354Ssamieee80211_start_all(struct ieee80211com *ic)
1273178354Ssam{
1274178354Ssam	struct ieee80211vap *vap;
1275178354Ssam
1276178354Ssam	IEEE80211_LOCK(ic);
1277178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1278178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1279178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1280178354Ssam			ieee80211_start_locked(vap);
1281178354Ssam	}
1282178354Ssam	IEEE80211_UNLOCK(ic);
1283178354Ssam}
1284178354Ssam
1285178354Ssam/*
1286178354Ssam * Stop a vap.  We force it down using the state machine
1287178354Ssam * then mark it's ifnet not running.  If this is the last
1288178354Ssam * vap running on the underlying device then we close it
1289178354Ssam * too to insure it will be properly initialized when the
1290178354Ssam * next vap is brought up.
1291178354Ssam */
1292178354Ssamvoid
1293178354Ssamieee80211_stop_locked(struct ieee80211vap *vap)
1294178354Ssam{
1295178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1296178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1297178354Ssam	struct ifnet *parent = ic->ic_ifp;
1298178354Ssam
1299178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1300178354Ssam
1301178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1302178354Ssam	    "stop running, %d vaps running\n", ic->ic_nrunning);
1303178354Ssam
1304178354Ssam	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1305178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1306178354Ssam		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1307178354Ssam		if (--ic->ic_nrunning == 0 &&
1308178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING)) {
1309178354Ssam			IEEE80211_DPRINTF(vap,
1310178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1311178354Ssam			    "down parent %s\n", parent->if_xname);
1312178354Ssam			parent->if_flags &= ~IFF_UP;
1313191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1314178354Ssam		}
1315178354Ssam	}
1316178354Ssam}
1317178354Ssam
1318178354Ssamvoid
1319178354Ssamieee80211_stop(struct ieee80211vap *vap)
1320178354Ssam{
1321178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1322178354Ssam
1323178354Ssam	IEEE80211_LOCK(ic);
1324178354Ssam	ieee80211_stop_locked(vap);
1325178354Ssam	IEEE80211_UNLOCK(ic);
1326178354Ssam}
1327178354Ssam
1328178354Ssam/*
1329178354Ssam * Stop all vap's running on a device.
1330178354Ssam */
1331178354Ssamvoid
1332178354Ssamieee80211_stop_all(struct ieee80211com *ic)
1333178354Ssam{
1334178354Ssam	struct ieee80211vap *vap;
1335178354Ssam
1336178354Ssam	IEEE80211_LOCK(ic);
1337178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1338178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1339178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1340178354Ssam			ieee80211_stop_locked(vap);
1341178354Ssam	}
1342178354Ssam	IEEE80211_UNLOCK(ic);
1343188533Sthompsa
1344188533Sthompsa	ieee80211_waitfor_parent(ic);
1345178354Ssam}
1346178354Ssam
1347178354Ssam/*
1348179391Ssam * Stop all vap's running on a device and arrange
1349179391Ssam * for those that were running to be resumed.
1350179391Ssam */
1351179391Ssamvoid
1352179391Ssamieee80211_suspend_all(struct ieee80211com *ic)
1353179391Ssam{
1354179391Ssam	struct ieee80211vap *vap;
1355179391Ssam
1356179391Ssam	IEEE80211_LOCK(ic);
1357179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1358179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1359179391Ssam		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
1360179391Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1361179391Ssam			ieee80211_stop_locked(vap);
1362179391Ssam		}
1363179391Ssam	}
1364179391Ssam	IEEE80211_UNLOCK(ic);
1365188533Sthompsa
1366188533Sthompsa	ieee80211_waitfor_parent(ic);
1367179391Ssam}
1368179391Ssam
1369179391Ssam/*
1370179391Ssam * Start all vap's marked for resume.
1371179391Ssam */
1372179391Ssamvoid
1373179391Ssamieee80211_resume_all(struct ieee80211com *ic)
1374179391Ssam{
1375179391Ssam	struct ieee80211vap *vap;
1376179391Ssam
1377179391Ssam	IEEE80211_LOCK(ic);
1378179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1379179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1380179391Ssam		if (!IFNET_IS_UP_RUNNING(ifp) &&
1381179391Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1382179391Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1383179391Ssam			ieee80211_start_locked(vap);
1384179391Ssam		}
1385179391Ssam	}
1386179391Ssam	IEEE80211_UNLOCK(ic);
1387179391Ssam}
1388179391Ssam
1389153349Ssamvoid
1390153349Ssamieee80211_beacon_miss(struct ieee80211com *ic)
1391153349Ssam{
1392191746Sthompsa	IEEE80211_LOCK(ic);
1393191746Sthompsa	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1394191746Sthompsa		/* Process in a taskq, the handler may reenter the driver */
1395191746Sthompsa		ieee80211_runtask(ic, &ic->ic_bmiss_task);
1396191746Sthompsa	}
1397191746Sthompsa	IEEE80211_UNLOCK(ic);
1398191746Sthompsa}
1399191746Sthompsa
1400191746Sthompsastatic void
1401191746Sthompsabeacon_miss(void *arg, int npending)
1402191746Sthompsa{
1403191746Sthompsa	struct ieee80211com *ic = arg;
1404178354Ssam	struct ieee80211vap *vap;
1405153349Ssam
1406178354Ssam	/* XXX locking */
1407178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1408153349Ssam		/*
1409178354Ssam		 * We only pass events through for sta vap's in RUN state;
1410178354Ssam		 * may be too restrictive but for now this saves all the
1411178354Ssam		 * handlers duplicating these checks.
1412153349Ssam		 */
1413178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1414193439Ssam		    vap->iv_state >= IEEE80211_S_RUN &&
1415178354Ssam		    vap->iv_bmiss != NULL)
1416178354Ssam			vap->iv_bmiss(vap);
1417153349Ssam	}
1418153349Ssam}
1419153349Ssam
1420191746Sthompsastatic void
1421191746Sthompsabeacon_swmiss(void *arg, int npending)
1422191746Sthompsa{
1423191746Sthompsa	struct ieee80211vap *vap = arg;
1424191746Sthompsa
1425191746Sthompsa	if (vap->iv_state != IEEE80211_S_RUN)
1426191746Sthompsa		return;
1427191746Sthompsa
1428191746Sthompsa	/* XXX Call multiple times if npending > zero? */
1429191746Sthompsa	vap->iv_bmiss(vap);
1430191746Sthompsa}
1431191746Sthompsa
1432154736Ssam/*
1433154736Ssam * Software beacon miss handling.  Check if any beacons
1434154736Ssam * were received in the last period.  If not post a
1435154736Ssam * beacon miss; otherwise reset the counter.
1436154736Ssam */
1437178354Ssamvoid
1438154736Ssamieee80211_swbmiss(void *arg)
1439154736Ssam{
1440178354Ssam	struct ieee80211vap *vap = arg;
1441179217Ssam	struct ieee80211com *ic = vap->iv_ic;
1442154736Ssam
1443179217Ssam	/* XXX sleep state? */
1444179217Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1445179217Ssam	    ("wrong state %d", vap->iv_state));
1446179217Ssam
1447179217Ssam	if (ic->ic_flags & IEEE80211_F_SCAN) {
1448179217Ssam		/*
1449179217Ssam		 * If scanning just ignore and reset state.  If we get a
1450179217Ssam		 * bmiss after coming out of scan because we haven't had
1451179217Ssam		 * time to receive a beacon then we should probe the AP
1452179217Ssam		 * before posting a real bmiss (unless iv_bmiss_max has
1453179217Ssam		 * been artifiically lowered).  A cleaner solution might
1454179217Ssam		 * be to disable the timer on scan start/end but to handle
1455179217Ssam		 * case of multiple sta vap's we'd need to disable the
1456179217Ssam		 * timers of all affected vap's.
1457179217Ssam		 */
1458179217Ssam		vap->iv_swbmiss_count = 0;
1459179217Ssam	} else if (vap->iv_swbmiss_count == 0) {
1460178354Ssam		if (vap->iv_bmiss != NULL)
1461191746Sthompsa			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1462154736Ssam	} else
1463178354Ssam		vap->iv_swbmiss_count = 0;
1464178354Ssam	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1465178354Ssam		ieee80211_swbmiss, vap);
1466154736Ssam}
1467154736Ssam
1468178354Ssam/*
1469178354Ssam * Start an 802.11h channel switch.  We record the parameters,
1470178354Ssam * mark the operation pending, notify each vap through the
1471178354Ssam * beacon update mechanism so it can update the beacon frame
1472178354Ssam * contents, and then switch vap's to CSA state to block outbound
1473178354Ssam * traffic.  Devices that handle CSA directly can use the state
1474178354Ssam * switch to do the right thing so long as they call
1475178354Ssam * ieee80211_csa_completeswitch when it's time to complete the
1476178354Ssam * channel change.  Devices that depend on the net80211 layer can
1477178354Ssam * use ieee80211_beacon_update to handle the countdown and the
1478178354Ssam * channel switch.
1479178354Ssam */
1480178354Ssamvoid
1481178354Ssamieee80211_csa_startswitch(struct ieee80211com *ic,
1482178354Ssam	struct ieee80211_channel *c, int mode, int count)
1483178354Ssam{
1484178354Ssam	struct ieee80211vap *vap;
1485178354Ssam
1486178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1487178354Ssam
1488178354Ssam	ic->ic_csa_newchan = c;
1489193439Ssam	ic->ic_csa_mode = mode;
1490178354Ssam	ic->ic_csa_count = count;
1491178354Ssam	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1492178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1493178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1494195618Srpaulo		    vap->iv_opmode == IEEE80211_M_IBSS ||
1495195618Srpaulo		    vap->iv_opmode == IEEE80211_M_MBSS)
1496178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1497178354Ssam		/* switch to CSA state to block outbound traffic */
1498178354Ssam		if (vap->iv_state == IEEE80211_S_RUN)
1499178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1500178354Ssam	}
1501178354Ssam	ieee80211_notify_csa(ic, c, mode, count);
1502178354Ssam}
1503178354Ssam
1504224220Sadrian/*
1505224220Sadrian * Complete the channel switch by transitioning all CSA VAPs to RUN.
1506224220Sadrian * This is called by both the completion and cancellation functions
1507224220Sadrian * so each VAP is placed back in the RUN state and can thus transmit.
1508224220Sadrian */
1509193439Ssamstatic void
1510193439Ssamcsa_completeswitch(struct ieee80211com *ic)
1511193439Ssam{
1512193439Ssam	struct ieee80211vap *vap;
1513193439Ssam
1514193439Ssam	ic->ic_csa_newchan = NULL;
1515193439Ssam	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1516193439Ssam
1517193439Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1518193439Ssam		if (vap->iv_state == IEEE80211_S_CSA)
1519193439Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1520193439Ssam}
1521193439Ssam
1522178354Ssam/*
1523178354Ssam * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1524178354Ssam * We clear state and move all vap's in CSA state to RUN state
1525178354Ssam * so they can again transmit.
1526224220Sadrian *
1527224220Sadrian * Although this may not be completely correct, update the BSS channel
1528224220Sadrian * for each VAP to the newly configured channel. The setcurchan sets
1529224220Sadrian * the current operating channel for the interface (so the radio does
1530224220Sadrian * switch over) but the VAP BSS isn't updated, leading to incorrectly
1531224220Sadrian * reported information via ioctl.
1532178354Ssam */
1533178354Ssamvoid
1534178354Ssamieee80211_csa_completeswitch(struct ieee80211com *ic)
1535178354Ssam{
1536224222Sadrian	struct ieee80211vap *vap;
1537224222Sadrian
1538178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1539178354Ssam
1540178354Ssam	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1541178354Ssam
1542178354Ssam	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1543224220Sadrian	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1544224220Sadrian		if (vap->iv_state == IEEE80211_S_CSA)
1545224220Sadrian			vap->iv_bss->ni_chan = ic->ic_curchan;
1546224220Sadrian
1547193439Ssam	csa_completeswitch(ic);
1548193439Ssam}
1549178354Ssam
1550193439Ssam/*
1551193439Ssam * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1552193439Ssam * We clear state and move all vap's in CSA state to RUN state
1553193439Ssam * so they can again transmit.
1554193439Ssam */
1555193439Ssamvoid
1556193439Ssamieee80211_csa_cancelswitch(struct ieee80211com *ic)
1557193439Ssam{
1558193439Ssam	IEEE80211_LOCK_ASSERT(ic);
1559193439Ssam
1560193439Ssam	csa_completeswitch(ic);
1561178354Ssam}
1562178354Ssam
1563178354Ssam/*
1564178354Ssam * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1565178354Ssam * We clear state and move all vap's in CAC state to RUN state.
1566178354Ssam */
1567178354Ssamvoid
1568178354Ssamieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1569178354Ssam{
1570178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1571178354Ssam	struct ieee80211vap *vap;
1572178354Ssam
1573178354Ssam	IEEE80211_LOCK(ic);
1574178354Ssam	/*
1575178354Ssam	 * Complete CAC state change for lead vap first; then
1576178354Ssam	 * clock all the other vap's waiting.
1577178354Ssam	 */
1578178354Ssam	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1579178354Ssam	    ("wrong state %d", vap0->iv_state));
1580178354Ssam	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1581178354Ssam
1582178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1583178354Ssam		if (vap->iv_state == IEEE80211_S_CAC)
1584178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1585178354Ssam	IEEE80211_UNLOCK(ic);
1586178354Ssam}
1587178354Ssam
1588178354Ssam/*
1589178354Ssam * Force all vap's other than the specified vap to the INIT state
1590178354Ssam * and mark them as waiting for a scan to complete.  These vaps
1591178354Ssam * will be brought up when the scan completes and the scanning vap
1592178354Ssam * reaches RUN state by wakeupwaiting.
1593178354Ssam */
1594154736Ssamstatic void
1595178354Ssammarkwaiting(struct ieee80211vap *vap0)
1596147765Ssam{
1597178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1598178354Ssam	struct ieee80211vap *vap;
1599147765Ssam
1600178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1601178354Ssam
1602191746Sthompsa	/*
1603191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1604191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1605191746Sthompsa	 * change task.
1606191746Sthompsa	 */
1607178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1608178354Ssam		if (vap == vap0)
1609178354Ssam			continue;
1610178354Ssam		if (vap->iv_state != IEEE80211_S_INIT) {
1611191746Sthompsa			/* NB: iv_newstate may drop the lock */
1612178354Ssam			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1613178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1614178354Ssam		}
1615147765Ssam	}
1616147765Ssam}
1617147765Ssam
1618178354Ssam/*
1619178354Ssam * Wakeup all vap's waiting for a scan to complete.  This is the
1620178354Ssam * companion to markwaiting (above) and is used to coordinate
1621178354Ssam * multiple vaps scanning.
1622191746Sthompsa * This is called from the state taskqueue.
1623178354Ssam */
1624147765Ssamstatic void
1625178354Ssamwakeupwaiting(struct ieee80211vap *vap0)
1626147765Ssam{
1627178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1628178354Ssam	struct ieee80211vap *vap;
1629147765Ssam
1630178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1631178354Ssam
1632191746Sthompsa	/*
1633191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1634191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1635191746Sthompsa	 * change task.
1636191746Sthompsa	 */
1637178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1638178354Ssam		if (vap == vap0)
1639178354Ssam			continue;
1640178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1641178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1642178354Ssam			/* NB: sta's cannot go INIT->RUN */
1643191746Sthompsa			/* NB: iv_newstate may drop the lock */
1644178354Ssam			vap->iv_newstate(vap,
1645178354Ssam			    vap->iv_opmode == IEEE80211_M_STA ?
1646178354Ssam			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1647178354Ssam		}
1648178354Ssam	}
1649147765Ssam}
1650147765Ssam
1651170530Ssam/*
1652178354Ssam * Handle post state change work common to all operating modes.
1653170530Ssam */
1654170530Ssamstatic void
1655191746Sthompsaieee80211_newstate_cb(void *xvap, int npending)
1656170530Ssam{
1657191746Sthompsa	struct ieee80211vap *vap = xvap;
1658178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1659191746Sthompsa	enum ieee80211_state nstate, ostate;
1660191746Sthompsa	int arg, rc;
1661178354Ssam
1662191746Sthompsa	IEEE80211_LOCK(ic);
1663191746Sthompsa	nstate = vap->iv_nstate;
1664191746Sthompsa	arg = vap->iv_nstate_arg;
1665178354Ssam
1666191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1667191746Sthompsa		/*
1668191746Sthompsa		 * We have been requested to drop back to the INIT before
1669191746Sthompsa		 * proceeding to the new state.
1670191746Sthompsa		 */
1671191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1672191746Sthompsa		    "%s: %s -> %s arg %d\n", __func__,
1673191746Sthompsa		    ieee80211_state_name[vap->iv_state],
1674191746Sthompsa		    ieee80211_state_name[IEEE80211_S_INIT], arg);
1675191746Sthompsa		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1676191746Sthompsa		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1677191746Sthompsa	}
1678191746Sthompsa
1679191746Sthompsa	ostate = vap->iv_state;
1680191746Sthompsa	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1681191746Sthompsa		/*
1682191746Sthompsa		 * SCAN was forced; e.g. on beacon miss.  Force other running
1683191746Sthompsa		 * vap's to INIT state and mark them as waiting for the scan to
1684191746Sthompsa		 * complete.  This insures they don't interfere with our
1685191746Sthompsa		 * scanning.  Since we are single threaded the vaps can not
1686191746Sthompsa		 * transition again while we are executing.
1687191746Sthompsa		 *
1688191746Sthompsa		 * XXX not always right, assumes ap follows sta
1689191746Sthompsa		 */
1690191746Sthompsa		markwaiting(vap);
1691191746Sthompsa	}
1692178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1693191746Sthompsa	    "%s: %s -> %s arg %d\n", __func__,
1694191746Sthompsa	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1695178354Ssam
1696191746Sthompsa	rc = vap->iv_newstate(vap, nstate, arg);
1697191746Sthompsa	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1698191746Sthompsa	if (rc != 0) {
1699191746Sthompsa		/* State transition failed */
1700191746Sthompsa		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1701191746Sthompsa		KASSERT(nstate != IEEE80211_S_INIT,
1702191746Sthompsa		    ("INIT state change failed"));
1703191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1704191746Sthompsa		    "%s: %s returned error %d\n", __func__,
1705191746Sthompsa		    ieee80211_state_name[nstate], rc);
1706191746Sthompsa		goto done;
1707191746Sthompsa	}
1708191746Sthompsa
1709191746Sthompsa	/* No actual transition, skip post processing */
1710191746Sthompsa	if (ostate == nstate)
1711191746Sthompsa		goto done;
1712191746Sthompsa
1713178354Ssam	if (nstate == IEEE80211_S_RUN) {
1714178354Ssam		/*
1715178354Ssam		 * OACTIVE may be set on the vap if the upper layer
1716178354Ssam		 * tried to transmit (e.g. IPv6 NDP) before we reach
1717178354Ssam		 * RUN state.  Clear it and restart xmit.
1718178354Ssam		 *
1719178354Ssam		 * Note this can also happen as a result of SLEEP->RUN
1720178354Ssam		 * (i.e. coming out of power save mode).
1721178354Ssam		 */
1722178354Ssam		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1723178354Ssam		if_start(vap->iv_ifp);
1724178354Ssam
1725178354Ssam		/* bring up any vaps waiting on us */
1726178354Ssam		wakeupwaiting(vap);
1727178354Ssam	} else if (nstate == IEEE80211_S_INIT) {
1728178354Ssam		/*
1729178354Ssam		 * Flush the scan cache if we did the last scan (XXX?)
1730178354Ssam		 * and flush any frames on send queues from this vap.
1731178354Ssam		 * Note the mgt q is used only for legacy drivers and
1732178354Ssam		 * will go away shortly.
1733178354Ssam		 */
1734178354Ssam		ieee80211_scan_flush(vap);
1735178354Ssam
1736178354Ssam		/* XXX NB: cast for altq */
1737178354Ssam		ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1738170530Ssam	}
1739191746Sthompsadone:
1740191746Sthompsa	IEEE80211_UNLOCK(ic);
1741170530Ssam}
1742170530Ssam
1743178354Ssam/*
1744178354Ssam * Public interface for initiating a state machine change.
1745178354Ssam * This routine single-threads the request and coordinates
1746178354Ssam * the scheduling of multiple vaps for the purpose of selecting
1747178354Ssam * an operating channel.  Specifically the following scenarios
1748178354Ssam * are handled:
1749178354Ssam * o only one vap can be selecting a channel so on transition to
1750178354Ssam *   SCAN state if another vap is already scanning then
1751178354Ssam *   mark the caller for later processing and return without
1752178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1753178354Ssam * o only one vap can be doing CAC of a channel so on transition to
1754178354Ssam *   CAC state if another vap is already scanning for radar then
1755178354Ssam *   mark the caller for later processing and return without
1756178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1757178354Ssam * o if another vap is already running when a request is made
1758178354Ssam *   to SCAN then an operating channel has been chosen; bypass
1759178354Ssam *   the scan and just join the channel
1760178354Ssam *
1761178354Ssam * Note that the state change call is done through the iv_newstate
1762178354Ssam * method pointer so any driver routine gets invoked.  The driver
1763178354Ssam * will normally call back into operating mode-specific
1764178354Ssam * ieee80211_newstate routines (below) unless it needs to completely
1765178354Ssam * bypass the state machine (e.g. because the firmware has it's
1766178354Ssam * own idea how things should work).  Bypassing the net80211 layer
1767178354Ssam * is usually a mistake and indicates lack of proper integration
1768178354Ssam * with the net80211 layer.
1769178354Ssam */
1770117811Ssamstatic int
1771178354Ssamieee80211_new_state_locked(struct ieee80211vap *vap,
1772178354Ssam	enum ieee80211_state nstate, int arg)
1773116742Ssam{
1774178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1775178354Ssam	struct ieee80211vap *vp;
1776117811Ssam	enum ieee80211_state ostate;
1777191746Sthompsa	int nrunning, nscanning;
1778116742Ssam
1779178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1780178354Ssam
1781191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1782191746Sthompsa		if (vap->iv_nstate == IEEE80211_S_INIT) {
1783191746Sthompsa			/*
1784191746Sthompsa			 * XXX The vap is being stopped, do no allow any other
1785191746Sthompsa			 * state changes until this is completed.
1786191746Sthompsa			 */
1787191746Sthompsa			return -1;
1788191768Sthompsa		} else if (vap->iv_state != vap->iv_nstate) {
1789191746Sthompsa#if 0
1790191768Sthompsa			/* Warn if the previous state hasn't completed. */
1791191768Sthompsa			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1792191768Sthompsa			    "%s: pending %s -> %s transition lost\n", __func__,
1793191768Sthompsa			    ieee80211_state_name[vap->iv_state],
1794191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1795191746Sthompsa#else
1796191768Sthompsa			/* XXX temporarily enable to identify issues */
1797191768Sthompsa			if_printf(vap->iv_ifp,
1798191768Sthompsa			    "%s: pending %s -> %s transition lost\n",
1799191768Sthompsa			    __func__, ieee80211_state_name[vap->iv_state],
1800191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1801191746Sthompsa#endif
1802191768Sthompsa		}
1803191746Sthompsa	}
1804191746Sthompsa
1805178354Ssam	nrunning = nscanning = 0;
1806178354Ssam	/* XXX can track this state instead of calculating */
1807178354Ssam	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1808178354Ssam		if (vp != vap) {
1809178354Ssam			if (vp->iv_state >= IEEE80211_S_RUN)
1810178354Ssam				nrunning++;
1811178354Ssam			/* XXX doesn't handle bg scan */
1812178354Ssam			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1813178354Ssam			else if (vp->iv_state > IEEE80211_S_INIT)
1814178354Ssam				nscanning++;
1815178354Ssam		}
1816178354Ssam	}
1817178354Ssam	ostate = vap->iv_state;
1818178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1819178354Ssam	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1820178354Ssam	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1821178354Ssam	    nrunning, nscanning);
1822116742Ssam	switch (nstate) {
1823116742Ssam	case IEEE80211_S_SCAN:
1824178354Ssam		if (ostate == IEEE80211_S_INIT) {
1825178354Ssam			/*
1826178354Ssam			 * INIT -> SCAN happens on initial bringup.
1827178354Ssam			 */
1828178354Ssam			KASSERT(!(nscanning && nrunning),
1829178354Ssam			    ("%d scanning and %d running", nscanning, nrunning));
1830178354Ssam			if (nscanning) {
1831116742Ssam				/*
1832178354Ssam				 * Someone is scanning, defer our state
1833178354Ssam				 * change until the work has completed.
1834116742Ssam				 */
1835178354Ssam				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1836178354Ssam				    "%s: defer %s -> %s\n",
1837178354Ssam				    __func__, ieee80211_state_name[ostate],
1838178354Ssam				    ieee80211_state_name[nstate]);
1839178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1840191746Sthompsa				return 0;
1841116742Ssam			}
1842178354Ssam			if (nrunning) {
1843170530Ssam				/*
1844178354Ssam				 * Someone is operating; just join the channel
1845178354Ssam				 * they have chosen.
1846170530Ssam				 */
1847178354Ssam				/* XXX kill arg? */
1848178354Ssam				/* XXX check each opmode, adhoc? */
1849178354Ssam				if (vap->iv_opmode == IEEE80211_M_STA)
1850178354Ssam					nstate = IEEE80211_S_SCAN;
1851178354Ssam				else
1852178354Ssam					nstate = IEEE80211_S_RUN;
1853138568Ssam#ifdef IEEE80211_DEBUG
1854178354Ssam				if (nstate != IEEE80211_S_SCAN) {
1855178354Ssam					IEEE80211_DPRINTF(vap,
1856178354Ssam					    IEEE80211_MSG_STATE,
1857178354Ssam					    "%s: override, now %s -> %s\n",
1858178354Ssam					    __func__,
1859178354Ssam					    ieee80211_state_name[ostate],
1860178354Ssam					    ieee80211_state_name[nstate]);
1861178354Ssam				}
1862138568Ssam#endif
1863170530Ssam			}
1864116742Ssam		}
1865178354Ssam		break;
1866178354Ssam	case IEEE80211_S_RUN:
1867178354Ssam		if (vap->iv_opmode == IEEE80211_M_WDS &&
1868178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1869178354Ssam		    nscanning) {
1870154736Ssam			/*
1871178354Ssam			 * Legacy WDS with someone else scanning; don't
1872178354Ssam			 * go online until that completes as we should
1873178354Ssam			 * follow the other vap to the channel they choose.
1874154736Ssam			 */
1875178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1876178354Ssam			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
1877178354Ssam			     ieee80211_state_name[ostate],
1878178354Ssam			     ieee80211_state_name[nstate]);
1879178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1880191746Sthompsa			return 0;
1881154736Ssam		}
1882178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1883178354Ssam		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1884178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1885178354Ssam		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1886178354Ssam			/*
1887178354Ssam			 * This is a DFS channel, transition to CAC state
1888178354Ssam			 * instead of RUN.  This allows us to initiate
1889178354Ssam			 * Channel Availability Check (CAC) as specified
1890178354Ssam			 * by 11h/DFS.
1891178354Ssam			 */
1892178354Ssam			nstate = IEEE80211_S_CAC;
1893178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1894178354Ssam			     "%s: override %s -> %s (DFS)\n", __func__,
1895178354Ssam			     ieee80211_state_name[ostate],
1896178354Ssam			     ieee80211_state_name[nstate]);
1897138568Ssam		}
1898116742Ssam		break;
1899178354Ssam	case IEEE80211_S_INIT:
1900191955Sthompsa		/* cancel any scan in progress */
1901191955Sthompsa		ieee80211_cancel_scan(vap);
1902178354Ssam		if (ostate == IEEE80211_S_INIT ) {
1903178354Ssam			/* XXX don't believe this */
1904178354Ssam			/* INIT -> INIT. nothing to do */
1905178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1906178354Ssam		}
1907178354Ssam		/* fall thru... */
1908172058Ssam	default:
1909172058Ssam		break;
1910116742Ssam	}
1911191746Sthompsa	/* defer the state change to a thread */
1912191746Sthompsa	vap->iv_nstate = nstate;
1913191746Sthompsa	vap->iv_nstate_arg = arg;
1914191746Sthompsa	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1915191746Sthompsa	ieee80211_runtask(ic, &vap->iv_nstate_task);
1916191746Sthompsa	return EINPROGRESS;
1917116742Ssam}
1918178354Ssam
1919178354Ssamint
1920178354Ssamieee80211_new_state(struct ieee80211vap *vap,
1921178354Ssam	enum ieee80211_state nstate, int arg)
1922178354Ssam{
1923178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1924178354Ssam	int rc;
1925178354Ssam
1926178354Ssam	IEEE80211_LOCK(ic);
1927178354Ssam	rc = ieee80211_new_state_locked(vap, nstate, arg);
1928178354Ssam	IEEE80211_UNLOCK(ic);
1929178354Ssam	return rc;
1930178354Ssam}
1931