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);
108233452Sadrianstatic void update_chw(void *, int);
109191746Sthompsastatic void ieee80211_newstate_cb(void *, int);
110117811Ssam
111178354Ssamstatic int
112178354Ssamnull_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
113178354Ssam	const struct ieee80211_bpf_params *params)
114172211Ssam{
115178354Ssam	struct ifnet *ifp = ni->ni_ic->ic_ifp;
116178354Ssam
117178354Ssam	if_printf(ifp, "missing ic_raw_xmit callback, drop frame\n");
118178354Ssam	m_freem(m);
119178354Ssam	return ENETDOWN;
120172211Ssam}
121172211Ssam
122116742Ssamvoid
123138568Ssamieee80211_proto_attach(struct ieee80211com *ic)
124116742Ssam{
125138568Ssam	struct ifnet *ifp = ic->ic_ifp;
126116742Ssam
127178354Ssam	/* override the 802.3 setting */
128178354Ssam	ifp->if_hdrlen = ic->ic_headroom
129178354Ssam		+ sizeof(struct ieee80211_qosframe_addr4)
130178354Ssam		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
131178354Ssam		+ IEEE80211_WEP_EXTIVLEN;
132178354Ssam	/* XXX no way to recalculate on ifdetach */
133178354Ssam	if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
134178354Ssam		/* XXX sanity check... */
135178354Ssam		max_linkhdr = ALIGN(ifp->if_hdrlen);
136178354Ssam		max_hdr = max_linkhdr + max_protohdr;
137178354Ssam		max_datalen = MHLEN - max_hdr;
138178354Ssam	}
139127648Ssam	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
140116742Ssam
141178354Ssam	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ifp);
142191746Sthompsa	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
143191746Sthompsa	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
144191746Sthompsa	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
145191746Sthompsa	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
146233452Sadrian	TASK_INIT(&ic->ic_chw_task, 0, update_chw, 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;
196225913Sadrian	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
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	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
664138568Ssam	int i, j;
665138568Ssam
666254315Srpaulo	if (rs->rs_nrates < nitems(rates))
667138568Ssam		return 0;
668254315Srpaulo	for (i = 0; i < nitems(rates); i++) {
669138568Ssam		for (j = 0; j < rs->rs_nrates; j++) {
670138568Ssam			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
671138568Ssam			if (rates[i] == r)
672138568Ssam				goto next;
673138568Ssam			if (r > rates[i])
674138568Ssam				return 0;
675138568Ssam		}
676138568Ssam		return 0;
677138568Ssam	next:
678138568Ssam		;
679138568Ssam	}
680138568Ssam	return 1;
681138568Ssam}
682138568Ssam
683138568Ssam/*
684178354Ssam * Mark the basic rates for the rate table based on the
685138568Ssam * operating mode.  For real 11g we mark all the 11b rates
686138568Ssam * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
687138568Ssam * 11b rates.  There's also a pseudo 11a-mode used to mark only
688138568Ssam * the basic OFDM rates.
689138568Ssam */
690178354Ssamstatic void
691178354Ssamsetbasicrates(struct ieee80211_rateset *rs,
692178354Ssam    enum ieee80211_phymode mode, int add)
693138568Ssam{
694170530Ssam	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
695188780Ssam	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
696188780Ssam	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
697188780Ssam					    /* NB: mixed b/g */
698188780Ssam	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
699188780Ssam	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
700188780Ssam	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
701188780Ssam	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
702188782Ssam	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
703188782Ssam	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
704188780Ssam	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
705188780Ssam					    /* NB: mixed b/g */
706188780Ssam	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
707138568Ssam	};
708138568Ssam	int i, j;
709138568Ssam
710138568Ssam	for (i = 0; i < rs->rs_nrates; i++) {
711178354Ssam		if (!add)
712178354Ssam			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
713138568Ssam		for (j = 0; j < basic[mode].rs_nrates; j++)
714138568Ssam			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
715138568Ssam				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
716138568Ssam				break;
717138568Ssam			}
718138568Ssam	}
719138568Ssam}
720138568Ssam
721138568Ssam/*
722178354Ssam * Set the basic rates in a rate set.
723138568Ssam */
724178354Ssamvoid
725178354Ssamieee80211_setbasicrates(struct ieee80211_rateset *rs,
726178354Ssam    enum ieee80211_phymode mode)
727178354Ssam{
728178354Ssam	setbasicrates(rs, mode, 0);
729178354Ssam}
730178354Ssam
731178354Ssam/*
732178354Ssam * Add basic rates to a rate set.
733178354Ssam */
734178354Ssamvoid
735178354Ssamieee80211_addbasicrates(struct ieee80211_rateset *rs,
736178354Ssam    enum ieee80211_phymode mode)
737178354Ssam{
738178354Ssam	setbasicrates(rs, mode, 1);
739178354Ssam}
740178354Ssam
741178354Ssam/*
742178354Ssam * WME protocol support.
743178354Ssam *
744178354Ssam * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
745178354Ssam * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
746178354Ssam * Draft 2.0 Test Plan (Appendix D).
747178354Ssam *
748178354Ssam * Static/Dynamic Turbo mode settings come from Atheros.
749178354Ssam */
750138568Ssamtypedef struct phyParamType {
751178354Ssam	uint8_t		aifsn;
752178354Ssam	uint8_t		logcwmin;
753178354Ssam	uint8_t		logcwmax;
754178354Ssam	uint16_t	txopLimit;
755178354Ssam	uint8_t 	acm;
756138568Ssam} paramType;
757138568Ssam
758138568Ssamstatic const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
759188780Ssam	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
760188780Ssam	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
761188780Ssam	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
762188780Ssam	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
763188780Ssam	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
764188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
765188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
766188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
767188782Ssam	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
768188782Ssam	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
769188780Ssam	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
770188780Ssam	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
771138568Ssam};
772138568Ssamstatic const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
773188780Ssam	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
774188780Ssam	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
775188780Ssam	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
776188780Ssam	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
777188780Ssam	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
778188780Ssam	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
779188780Ssam	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
780188780Ssam	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
781188782Ssam	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
782188782Ssam	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
783188780Ssam	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
784188780Ssam	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
785138568Ssam};
786138568Ssamstatic const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
787188780Ssam	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
788188780Ssam	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
789188780Ssam	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
790188780Ssam	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
791188780Ssam	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
792188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
793188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
794188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
795188782Ssam	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
796188782Ssam	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
797188780Ssam	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
798188780Ssam	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
799138568Ssam};
800138568Ssamstatic const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
801188780Ssam	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
802188780Ssam	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
803188780Ssam	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
804188780Ssam	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
805188780Ssam	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
806188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
807188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
808188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
809188782Ssam	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
810188782Ssam	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
811188780Ssam	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
812188780Ssam	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
813138568Ssam};
814138568Ssam
815138568Ssamstatic const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
816188780Ssam	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
817188780Ssam	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
818188780Ssam	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
819188780Ssam	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
820188780Ssam	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
821188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
822188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
823188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
824188782Ssam	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
825188782Ssam	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
826188780Ssam	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
827188780Ssam	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
828138568Ssam};
829138568Ssamstatic const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
830188780Ssam	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
831188780Ssam	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
832188780Ssam	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
833188780Ssam	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
834188780Ssam	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
835188780Ssam	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
836188780Ssam	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
837188780Ssam	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
838188782Ssam	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
839188782Ssam	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
840188780Ssam	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
841188780Ssam	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
842138568Ssam};
843138568Ssamstatic const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
844188780Ssam	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
845188780Ssam	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
846188780Ssam	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
847188780Ssam	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
848188780Ssam	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
849188780Ssam	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
850188780Ssam	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
851188780Ssam	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
852188782Ssam	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
853188782Ssam	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
854188780Ssam	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
855188780Ssam	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
856138568Ssam};
857138568Ssam
858178354Ssamstatic void
859188863Ssam_setifsparams(struct wmeParams *wmep, const paramType *phy)
860188863Ssam{
861188863Ssam	wmep->wmep_aifsn = phy->aifsn;
862188863Ssam	wmep->wmep_logcwmin = phy->logcwmin;
863188863Ssam	wmep->wmep_logcwmax = phy->logcwmax;
864188863Ssam	wmep->wmep_txopLimit = phy->txopLimit;
865188863Ssam}
866188863Ssam
867188863Ssamstatic void
868188863Ssamsetwmeparams(struct ieee80211vap *vap, const char *type, int ac,
869188863Ssam	struct wmeParams *wmep, const paramType *phy)
870188863Ssam{
871188863Ssam	wmep->wmep_acm = phy->acm;
872188863Ssam	_setifsparams(wmep, phy);
873188863Ssam
874188863Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
875188863Ssam	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
876188863Ssam	    ieee80211_wme_acnames[ac], type,
877188863Ssam	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
878188863Ssam	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
879188863Ssam}
880188863Ssam
881188863Ssamstatic void
882178354Ssamieee80211_wme_initparams_locked(struct ieee80211vap *vap)
883138568Ssam{
884178354Ssam	struct ieee80211com *ic = vap->iv_ic;
885138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
886138568Ssam	const paramType *pPhyParam, *pBssPhyParam;
887138568Ssam	struct wmeParams *wmep;
888170530Ssam	enum ieee80211_phymode mode;
889138568Ssam	int i;
890138568Ssam
891178354Ssam	IEEE80211_LOCK_ASSERT(ic);
892178354Ssam
893188864Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
894138568Ssam		return;
895138568Ssam
896170530Ssam	/*
897219961Sadrian	 * Clear the wme cap_info field so a qoscount from a previous
898219961Sadrian	 * vap doesn't confuse later code which only parses the beacon
899219961Sadrian	 * field and updates hardware when said field changes.
900219961Sadrian	 * Otherwise the hardware is programmed with defaults, not what
901219961Sadrian	 * the beacon actually announces.
902219961Sadrian	 */
903219961Sadrian	wme->wme_wmeChanParams.cap_info = 0;
904219961Sadrian
905219961Sadrian	/*
906170530Ssam	 * Select mode; we can be called early in which case we
907170530Ssam	 * always use auto mode.  We know we'll be called when
908170530Ssam	 * entering the RUN state with bsschan setup properly
909170530Ssam	 * so state will eventually get set correctly
910170530Ssam	 */
911170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
912170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
913170530Ssam	else
914170530Ssam		mode = IEEE80211_MODE_AUTO;
915138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
916138568Ssam		switch (i) {
917138568Ssam		case WME_AC_BK:
918170530Ssam			pPhyParam = &phyParamForAC_BK[mode];
919170530Ssam			pBssPhyParam = &phyParamForAC_BK[mode];
920138568Ssam			break;
921138568Ssam		case WME_AC_VI:
922170530Ssam			pPhyParam = &phyParamForAC_VI[mode];
923170530Ssam			pBssPhyParam = &bssPhyParamForAC_VI[mode];
924138568Ssam			break;
925138568Ssam		case WME_AC_VO:
926170530Ssam			pPhyParam = &phyParamForAC_VO[mode];
927170530Ssam			pBssPhyParam = &bssPhyParamForAC_VO[mode];
928138568Ssam			break;
929138568Ssam		case WME_AC_BE:
930138568Ssam		default:
931170530Ssam			pPhyParam = &phyParamForAC_BE[mode];
932170530Ssam			pBssPhyParam = &bssPhyParamForAC_BE[mode];
933138568Ssam			break;
934138568Ssam		}
935138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
936138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
937188863Ssam			setwmeparams(vap, "chan", i, wmep, pPhyParam);
938138568Ssam		} else {
939188863Ssam			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
940138568Ssam		}
941138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
942188863Ssam		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
943138568Ssam	}
944138568Ssam	/* NB: check ic_bss to avoid NULL deref on initial attach */
945178354Ssam	if (vap->iv_bss != NULL) {
946138568Ssam		/*
947138568Ssam		 * Calculate agressive mode switching threshold based
948138568Ssam		 * on beacon interval.  This doesn't need locking since
949138568Ssam		 * we're only called before entering the RUN state at
950138568Ssam		 * which point we start sending beacon frames.
951138568Ssam		 */
952138568Ssam		wme->wme_hipri_switch_thresh =
953178354Ssam			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
954188864Ssam		wme->wme_flags &= ~WME_F_AGGRMODE;
955178354Ssam		ieee80211_wme_updateparams(vap);
956138568Ssam	}
957138568Ssam}
958138568Ssam
959178354Ssamvoid
960178354Ssamieee80211_wme_initparams(struct ieee80211vap *vap)
961178354Ssam{
962178354Ssam	struct ieee80211com *ic = vap->iv_ic;
963178354Ssam
964178354Ssam	IEEE80211_LOCK(ic);
965178354Ssam	ieee80211_wme_initparams_locked(vap);
966178354Ssam	IEEE80211_UNLOCK(ic);
967178354Ssam}
968178354Ssam
969138568Ssam/*
970138568Ssam * Update WME parameters for ourself and the BSS.
971138568Ssam */
972138568Ssamvoid
973178354Ssamieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
974138568Ssam{
975188863Ssam	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
976188780Ssam	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
977188780Ssam	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
978188780Ssam	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
979188780Ssam	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
980188780Ssam	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
981188780Ssam	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
982188780Ssam	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
983188780Ssam	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
984188782Ssam	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
985188782Ssam	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
986188780Ssam	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
987188780Ssam	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
988138568Ssam	};
989178354Ssam	struct ieee80211com *ic = vap->iv_ic;
990138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
991138568Ssam	const struct wmeParams *wmep;
992138568Ssam	struct wmeParams *chanp, *bssp;
993170530Ssam	enum ieee80211_phymode mode;
994138568Ssam	int i;
995244062Sadrian	int do_aggrmode = 0;
996138568Ssam
997188863Ssam       	/*
998188863Ssam	 * Set up the channel access parameters for the physical
999188863Ssam	 * device.  First populate the configured settings.
1000188863Ssam	 */
1001138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1002138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[i];
1003138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1004138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
1005138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1006138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1007138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1008138568Ssam
1009138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1010138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1011138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
1012138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1013138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1014138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1015138568Ssam	}
1016138568Ssam
1017138568Ssam	/*
1018170530Ssam	 * Select mode; we can be called early in which case we
1019170530Ssam	 * always use auto mode.  We know we'll be called when
1020170530Ssam	 * entering the RUN state with bsschan setup properly
1021170530Ssam	 * so state will eventually get set correctly
1022170530Ssam	 */
1023170530Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1024170530Ssam		mode = ieee80211_chan2mode(ic->ic_bsschan);
1025170530Ssam	else
1026170530Ssam		mode = IEEE80211_MODE_AUTO;
1027170530Ssam
1028170530Ssam	/*
1029138568Ssam	 * This implements agressive mode as found in certain
1030138568Ssam	 * vendors' AP's.  When there is significant high
1031138568Ssam	 * priority (VI/VO) traffic in the BSS throttle back BE
1032138568Ssam	 * traffic by using conservative parameters.  Otherwise
1033138568Ssam	 * BE uses agressive params to optimize performance of
1034138568Ssam	 * legacy/non-QoS traffic.
1035138568Ssam	 */
1036244062Sadrian
1037244062Sadrian	/* Hostap? Only if aggressive mode is enabled */
1038244062Sadrian        if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1039244062Sadrian	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
1040244062Sadrian		do_aggrmode = 1;
1041244062Sadrian
1042244062Sadrian	/*
1043244062Sadrian	 * Station? Only if we're in a non-QoS BSS.
1044244062Sadrian	 */
1045244062Sadrian	else if ((vap->iv_opmode == IEEE80211_M_STA &&
1046244062Sadrian	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1047244062Sadrian		do_aggrmode = 1;
1048244062Sadrian
1049244062Sadrian	/*
1050244062Sadrian	 * IBSS? Only if we we have WME enabled.
1051244062Sadrian	 */
1052244062Sadrian	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1053244062Sadrian	    (vap->iv_flags & IEEE80211_F_WME))
1054244062Sadrian		do_aggrmode = 1;
1055244062Sadrian
1056244062Sadrian	/*
1057244062Sadrian	 * If WME is disabled on this VAP, default to aggressive mode
1058244062Sadrian	 * regardless of the configuration.
1059244062Sadrian	 */
1060244062Sadrian	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1061244062Sadrian		do_aggrmode = 1;
1062244062Sadrian
1063244062Sadrian	/* XXX WDS? */
1064244062Sadrian
1065244062Sadrian	/* XXX MBSS? */
1066244062Sadrian
1067244062Sadrian	if (do_aggrmode) {
1068138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1069138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1070138568Ssam
1071188863Ssam		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1072138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1073188863Ssam		    aggrParam[mode].logcwmin;
1074138568Ssam		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1075188863Ssam		    aggrParam[mode].logcwmax;
1076138568Ssam		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1077188863Ssam		    (vap->iv_flags & IEEE80211_F_BURST) ?
1078188863Ssam			aggrParam[mode].txopLimit : 0;
1079178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1080188863Ssam		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1081188863Ssam		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1082188863Ssam		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1083188863Ssam		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1084138568Ssam	}
1085244062Sadrian
1086244062Sadrian
1087244062Sadrian	/*
1088244062Sadrian	 * Change the contention window based on the number of associated
1089244062Sadrian	 * stations.  If the number of associated stations is 1 and
1090244062Sadrian	 * aggressive mode is enabled, lower the contention window even
1091244062Sadrian	 * further.
1092244062Sadrian	 */
1093178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1094156524Ssam	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1095188780Ssam		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1096188780Ssam		    [IEEE80211_MODE_AUTO]	= 3,
1097188780Ssam		    [IEEE80211_MODE_11A]	= 3,
1098188780Ssam		    [IEEE80211_MODE_11B]	= 4,
1099188780Ssam		    [IEEE80211_MODE_11G]	= 3,
1100188780Ssam		    [IEEE80211_MODE_FH]		= 4,
1101188780Ssam		    [IEEE80211_MODE_TURBO_A]	= 3,
1102188780Ssam		    [IEEE80211_MODE_TURBO_G]	= 3,
1103188780Ssam		    [IEEE80211_MODE_STURBO_A]	= 3,
1104188782Ssam		    [IEEE80211_MODE_HALF]	= 3,
1105188782Ssam		    [IEEE80211_MODE_QUARTER]	= 3,
1106188780Ssam		    [IEEE80211_MODE_11NA]	= 3,
1107188780Ssam		    [IEEE80211_MODE_11NG]	= 3,
1108138568Ssam		};
1109138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1110138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1111138568Ssam
1112170530Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1113178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1114188863Ssam		    "update %s (chan+bss) logcwmin %u\n",
1115188863Ssam		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1116244062Sadrian	}
1117244062Sadrian
1118244062Sadrian	/*
1119244062Sadrian	 * Arrange for the beacon update.
1120244062Sadrian	 *
1121244062Sadrian	 * XXX what about MBSS, WDS?
1122244062Sadrian	 */
1123244062Sadrian	if (vap->iv_opmode == IEEE80211_M_HOSTAP
1124244062Sadrian	    || vap->iv_opmode == IEEE80211_M_IBSS) {
1125138568Ssam		/*
1126138568Ssam		 * Arrange for a beacon update and bump the parameter
1127138568Ssam		 * set number so associated stations load the new values.
1128138568Ssam		 */
1129138568Ssam		wme->wme_bssChanParams.cap_info =
1130138568Ssam			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1131178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1132138568Ssam	}
1133138568Ssam
1134138568Ssam	wme->wme_update(ic);
1135138568Ssam
1136178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1137188863Ssam	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1138188863Ssam	    vap->iv_opmode == IEEE80211_M_STA ?
1139188863Ssam		wme->wme_wmeChanParams.cap_info :
1140188863Ssam		wme->wme_bssChanParams.cap_info);
1141138568Ssam}
1142138568Ssam
1143138568Ssamvoid
1144178354Ssamieee80211_wme_updateparams(struct ieee80211vap *vap)
1145138568Ssam{
1146178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1147138568Ssam
1148138568Ssam	if (ic->ic_caps & IEEE80211_C_WME) {
1149178354Ssam		IEEE80211_LOCK(ic);
1150178354Ssam		ieee80211_wme_updateparams_locked(vap);
1151178354Ssam		IEEE80211_UNLOCK(ic);
1152138568Ssam	}
1153138568Ssam}
1154138568Ssam
1155178354Ssamstatic void
1156178354Ssamparent_updown(void *arg, int npending)
1157178354Ssam{
1158178354Ssam	struct ifnet *parent = arg;
1159178354Ssam
1160178354Ssam	parent->if_ioctl(parent, SIOCSIFFLAGS, NULL);
1161178354Ssam}
1162178354Ssam
1163191746Sthompsastatic void
1164191746Sthompsaupdate_mcast(void *arg, int npending)
1165191746Sthompsa{
1166191746Sthompsa	struct ieee80211com *ic = arg;
1167191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1168191746Sthompsa
1169191746Sthompsa	ic->ic_update_mcast(parent);
1170191746Sthompsa}
1171191746Sthompsa
1172191746Sthompsastatic void
1173191746Sthompsaupdate_promisc(void *arg, int npending)
1174191746Sthompsa{
1175191746Sthompsa	struct ieee80211com *ic = arg;
1176191746Sthompsa	struct ifnet *parent = ic->ic_ifp;
1177191746Sthompsa
1178191746Sthompsa	ic->ic_update_promisc(parent);
1179191746Sthompsa}
1180191746Sthompsa
1181191746Sthompsastatic void
1182191746Sthompsaupdate_channel(void *arg, int npending)
1183191746Sthompsa{
1184191746Sthompsa	struct ieee80211com *ic = arg;
1185191746Sthompsa
1186191746Sthompsa	ic->ic_set_channel(ic);
1187192468Ssam	ieee80211_radiotap_chan_change(ic);
1188191746Sthompsa}
1189191746Sthompsa
1190233452Sadrianstatic void
1191233452Sadrianupdate_chw(void *arg, int npending)
1192233452Sadrian{
1193233452Sadrian	struct ieee80211com *ic = arg;
1194233452Sadrian
1195233452Sadrian	/*
1196233452Sadrian	 * XXX should we defer the channel width _config_ update until now?
1197233452Sadrian	 */
1198233452Sadrian	ic->ic_update_chw(ic);
1199233452Sadrian}
1200233452Sadrian
1201170530Ssam/*
1202188533Sthompsa * Block until the parent is in a known state.  This is
1203188533Sthompsa * used after any operations that dispatch a task (e.g.
1204188533Sthompsa * to auto-configure the parent device up/down).
1205188533Sthompsa */
1206188533Sthompsavoid
1207188533Sthompsaieee80211_waitfor_parent(struct ieee80211com *ic)
1208188533Sthompsa{
1209191746Sthompsa	taskqueue_block(ic->ic_tq);
1210191746Sthompsa	ieee80211_draintask(ic, &ic->ic_parent_task);
1211191746Sthompsa	ieee80211_draintask(ic, &ic->ic_mcast_task);
1212191746Sthompsa	ieee80211_draintask(ic, &ic->ic_promisc_task);
1213191746Sthompsa	ieee80211_draintask(ic, &ic->ic_chan_task);
1214191746Sthompsa	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1215233452Sadrian	ieee80211_draintask(ic, &ic->ic_chw_task);
1216191746Sthompsa	taskqueue_unblock(ic->ic_tq);
1217188533Sthompsa}
1218188533Sthompsa
1219188533Sthompsa/*
1220178354Ssam * Start a vap running.  If this is the first vap to be
1221178354Ssam * set running on the underlying device then we
1222178354Ssam * automatically bring the device up.
1223170530Ssam */
1224178354Ssamvoid
1225178354Ssamieee80211_start_locked(struct ieee80211vap *vap)
1226170530Ssam{
1227178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1228178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1229178354Ssam	struct ifnet *parent = ic->ic_ifp;
1230170530Ssam
1231178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1232178354Ssam
1233178354Ssam	IEEE80211_DPRINTF(vap,
1234170530Ssam		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1235178354Ssam		"start running, %d vaps running\n", ic->ic_nrunning);
1236170530Ssam
1237178354Ssam	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1238178354Ssam		/*
1239178354Ssam		 * Mark us running.  Note that it's ok to do this first;
1240178354Ssam		 * if we need to bring the parent device up we defer that
1241178354Ssam		 * to avoid dropping the com lock.  We expect the device
1242178354Ssam		 * to respond to being marked up by calling back into us
1243178354Ssam		 * through ieee80211_start_all at which point we'll come
1244178354Ssam		 * back in here and complete the work.
1245178354Ssam		 */
1246178354Ssam		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1247178354Ssam		/*
1248178354Ssam		 * We are not running; if this we are the first vap
1249178354Ssam		 * to be brought up auto-up the parent if necessary.
1250178354Ssam		 */
1251178354Ssam		if (ic->ic_nrunning++ == 0 &&
1252178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1253178354Ssam			IEEE80211_DPRINTF(vap,
1254178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1255178354Ssam			    "%s: up parent %s\n", __func__, parent->if_xname);
1256178354Ssam			parent->if_flags |= IFF_UP;
1257191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1258178354Ssam			return;
1259178354Ssam		}
1260178354Ssam	}
1261170530Ssam	/*
1262178354Ssam	 * If the parent is up and running, then kick the
1263178354Ssam	 * 802.11 state machine as appropriate.
1264170530Ssam	 */
1265178354Ssam	if ((parent->if_drv_flags & IFF_DRV_RUNNING) &&
1266178354Ssam	    vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1267178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA) {
1268178354Ssam#if 0
1269178354Ssam			/* XXX bypasses scan too easily; disable for now */
1270178354Ssam			/*
1271178354Ssam			 * Try to be intelligent about clocking the state
1272178354Ssam			 * machine.  If we're currently in RUN state then
1273178354Ssam			 * we should be able to apply any new state/parameters
1274178354Ssam			 * simply by re-associating.  Otherwise we need to
1275178354Ssam			 * re-scan to select an appropriate ap.
1276178354Ssam			 */
1277178354Ssam			if (vap->iv_state >= IEEE80211_S_RUN)
1278178354Ssam				ieee80211_new_state_locked(vap,
1279178354Ssam				    IEEE80211_S_ASSOC, 1);
1280178354Ssam			else
1281178354Ssam#endif
1282178354Ssam				ieee80211_new_state_locked(vap,
1283178354Ssam				    IEEE80211_S_SCAN, 0);
1284170530Ssam		} else {
1285170530Ssam			/*
1286178354Ssam			 * For monitor+wds mode there's nothing to do but
1287178354Ssam			 * start running.  Otherwise if this is the first
1288170530Ssam			 * vap to be brought up, start a scan which may be
1289170530Ssam			 * preempted if the station is locked to a particular
1290170530Ssam			 * channel.
1291170530Ssam			 */
1292191746Sthompsa			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1293178354Ssam			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1294178354Ssam			    vap->iv_opmode == IEEE80211_M_WDS)
1295178354Ssam				ieee80211_new_state_locked(vap,
1296178354Ssam				    IEEE80211_S_RUN, -1);
1297178354Ssam			else
1298178354Ssam				ieee80211_new_state_locked(vap,
1299178354Ssam				    IEEE80211_S_SCAN, 0);
1300170530Ssam		}
1301170530Ssam	}
1302170530Ssam}
1303170530Ssam
1304170530Ssam/*
1305178354Ssam * Start a single vap.
1306178354Ssam */
1307178354Ssamvoid
1308178354Ssamieee80211_init(void *arg)
1309178354Ssam{
1310178354Ssam	struct ieee80211vap *vap = arg;
1311178354Ssam
1312193348Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1313193348Ssam	    "%s\n", __func__);
1314178354Ssam
1315193348Ssam	IEEE80211_LOCK(vap->iv_ic);
1316193348Ssam	ieee80211_start_locked(vap);
1317193348Ssam	IEEE80211_UNLOCK(vap->iv_ic);
1318178354Ssam}
1319178354Ssam
1320178354Ssam/*
1321178354Ssam * Start all runnable vap's on a device.
1322178354Ssam */
1323178354Ssamvoid
1324178354Ssamieee80211_start_all(struct ieee80211com *ic)
1325178354Ssam{
1326178354Ssam	struct ieee80211vap *vap;
1327178354Ssam
1328178354Ssam	IEEE80211_LOCK(ic);
1329178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1330178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1331178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1332178354Ssam			ieee80211_start_locked(vap);
1333178354Ssam	}
1334178354Ssam	IEEE80211_UNLOCK(ic);
1335178354Ssam}
1336178354Ssam
1337178354Ssam/*
1338178354Ssam * Stop a vap.  We force it down using the state machine
1339178354Ssam * then mark it's ifnet not running.  If this is the last
1340178354Ssam * vap running on the underlying device then we close it
1341178354Ssam * too to insure it will be properly initialized when the
1342178354Ssam * next vap is brought up.
1343178354Ssam */
1344178354Ssamvoid
1345178354Ssamieee80211_stop_locked(struct ieee80211vap *vap)
1346178354Ssam{
1347178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1348178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1349178354Ssam	struct ifnet *parent = ic->ic_ifp;
1350178354Ssam
1351178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1352178354Ssam
1353178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1354178354Ssam	    "stop running, %d vaps running\n", ic->ic_nrunning);
1355178354Ssam
1356178354Ssam	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1357178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1358178354Ssam		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1359178354Ssam		if (--ic->ic_nrunning == 0 &&
1360178354Ssam		    (parent->if_drv_flags & IFF_DRV_RUNNING)) {
1361178354Ssam			IEEE80211_DPRINTF(vap,
1362178354Ssam			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1363178354Ssam			    "down parent %s\n", parent->if_xname);
1364178354Ssam			parent->if_flags &= ~IFF_UP;
1365191746Sthompsa			ieee80211_runtask(ic, &ic->ic_parent_task);
1366178354Ssam		}
1367178354Ssam	}
1368178354Ssam}
1369178354Ssam
1370178354Ssamvoid
1371178354Ssamieee80211_stop(struct ieee80211vap *vap)
1372178354Ssam{
1373178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1374178354Ssam
1375178354Ssam	IEEE80211_LOCK(ic);
1376178354Ssam	ieee80211_stop_locked(vap);
1377178354Ssam	IEEE80211_UNLOCK(ic);
1378178354Ssam}
1379178354Ssam
1380178354Ssam/*
1381178354Ssam * Stop all vap's running on a device.
1382178354Ssam */
1383178354Ssamvoid
1384178354Ssamieee80211_stop_all(struct ieee80211com *ic)
1385178354Ssam{
1386178354Ssam	struct ieee80211vap *vap;
1387178354Ssam
1388178354Ssam	IEEE80211_LOCK(ic);
1389178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1390178354Ssam		struct ifnet *ifp = vap->iv_ifp;
1391178354Ssam		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1392178354Ssam			ieee80211_stop_locked(vap);
1393178354Ssam	}
1394178354Ssam	IEEE80211_UNLOCK(ic);
1395188533Sthompsa
1396188533Sthompsa	ieee80211_waitfor_parent(ic);
1397178354Ssam}
1398178354Ssam
1399178354Ssam/*
1400179391Ssam * Stop all vap's running on a device and arrange
1401179391Ssam * for those that were running to be resumed.
1402179391Ssam */
1403179391Ssamvoid
1404179391Ssamieee80211_suspend_all(struct ieee80211com *ic)
1405179391Ssam{
1406179391Ssam	struct ieee80211vap *vap;
1407179391Ssam
1408179391Ssam	IEEE80211_LOCK(ic);
1409179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1410179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1411179391Ssam		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
1412179391Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1413179391Ssam			ieee80211_stop_locked(vap);
1414179391Ssam		}
1415179391Ssam	}
1416179391Ssam	IEEE80211_UNLOCK(ic);
1417188533Sthompsa
1418188533Sthompsa	ieee80211_waitfor_parent(ic);
1419179391Ssam}
1420179391Ssam
1421179391Ssam/*
1422179391Ssam * Start all vap's marked for resume.
1423179391Ssam */
1424179391Ssamvoid
1425179391Ssamieee80211_resume_all(struct ieee80211com *ic)
1426179391Ssam{
1427179391Ssam	struct ieee80211vap *vap;
1428179391Ssam
1429179391Ssam	IEEE80211_LOCK(ic);
1430179391Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1431179391Ssam		struct ifnet *ifp = vap->iv_ifp;
1432179391Ssam		if (!IFNET_IS_UP_RUNNING(ifp) &&
1433179391Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1434179391Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1435179391Ssam			ieee80211_start_locked(vap);
1436179391Ssam		}
1437179391Ssam	}
1438179391Ssam	IEEE80211_UNLOCK(ic);
1439179391Ssam}
1440179391Ssam
1441153349Ssamvoid
1442153349Ssamieee80211_beacon_miss(struct ieee80211com *ic)
1443153349Ssam{
1444191746Sthompsa	IEEE80211_LOCK(ic);
1445191746Sthompsa	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1446191746Sthompsa		/* Process in a taskq, the handler may reenter the driver */
1447191746Sthompsa		ieee80211_runtask(ic, &ic->ic_bmiss_task);
1448191746Sthompsa	}
1449191746Sthompsa	IEEE80211_UNLOCK(ic);
1450191746Sthompsa}
1451191746Sthompsa
1452191746Sthompsastatic void
1453191746Sthompsabeacon_miss(void *arg, int npending)
1454191746Sthompsa{
1455191746Sthompsa	struct ieee80211com *ic = arg;
1456178354Ssam	struct ieee80211vap *vap;
1457153349Ssam
1458225913Sadrian	IEEE80211_LOCK(ic);
1459178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1460153349Ssam		/*
1461178354Ssam		 * We only pass events through for sta vap's in RUN state;
1462178354Ssam		 * may be too restrictive but for now this saves all the
1463178354Ssam		 * handlers duplicating these checks.
1464153349Ssam		 */
1465178354Ssam		if (vap->iv_opmode == IEEE80211_M_STA &&
1466193439Ssam		    vap->iv_state >= IEEE80211_S_RUN &&
1467178354Ssam		    vap->iv_bmiss != NULL)
1468178354Ssam			vap->iv_bmiss(vap);
1469153349Ssam	}
1470225913Sadrian	IEEE80211_UNLOCK(ic);
1471153349Ssam}
1472153349Ssam
1473191746Sthompsastatic void
1474191746Sthompsabeacon_swmiss(void *arg, int npending)
1475191746Sthompsa{
1476191746Sthompsa	struct ieee80211vap *vap = arg;
1477225913Sadrian	struct ieee80211com *ic = vap->iv_ic;
1478191746Sthompsa
1479225913Sadrian	IEEE80211_LOCK(ic);
1480225913Sadrian	if (vap->iv_state == IEEE80211_S_RUN) {
1481225913Sadrian		/* XXX Call multiple times if npending > zero? */
1482225913Sadrian		vap->iv_bmiss(vap);
1483225913Sadrian	}
1484225913Sadrian	IEEE80211_UNLOCK(ic);
1485191746Sthompsa}
1486191746Sthompsa
1487154736Ssam/*
1488154736Ssam * Software beacon miss handling.  Check if any beacons
1489154736Ssam * were received in the last period.  If not post a
1490154736Ssam * beacon miss; otherwise reset the counter.
1491154736Ssam */
1492178354Ssamvoid
1493154736Ssamieee80211_swbmiss(void *arg)
1494154736Ssam{
1495178354Ssam	struct ieee80211vap *vap = arg;
1496179217Ssam	struct ieee80211com *ic = vap->iv_ic;
1497154736Ssam
1498225913Sadrian	IEEE80211_LOCK_ASSERT(ic);
1499225913Sadrian
1500179217Ssam	/* XXX sleep state? */
1501179217Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1502179217Ssam	    ("wrong state %d", vap->iv_state));
1503179217Ssam
1504179217Ssam	if (ic->ic_flags & IEEE80211_F_SCAN) {
1505179217Ssam		/*
1506179217Ssam		 * If scanning just ignore and reset state.  If we get a
1507179217Ssam		 * bmiss after coming out of scan because we haven't had
1508179217Ssam		 * time to receive a beacon then we should probe the AP
1509179217Ssam		 * before posting a real bmiss (unless iv_bmiss_max has
1510179217Ssam		 * been artifiically lowered).  A cleaner solution might
1511179217Ssam		 * be to disable the timer on scan start/end but to handle
1512179217Ssam		 * case of multiple sta vap's we'd need to disable the
1513179217Ssam		 * timers of all affected vap's.
1514179217Ssam		 */
1515179217Ssam		vap->iv_swbmiss_count = 0;
1516179217Ssam	} else if (vap->iv_swbmiss_count == 0) {
1517178354Ssam		if (vap->iv_bmiss != NULL)
1518191746Sthompsa			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1519154736Ssam	} else
1520178354Ssam		vap->iv_swbmiss_count = 0;
1521178354Ssam	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1522178354Ssam		ieee80211_swbmiss, vap);
1523154736Ssam}
1524154736Ssam
1525178354Ssam/*
1526178354Ssam * Start an 802.11h channel switch.  We record the parameters,
1527178354Ssam * mark the operation pending, notify each vap through the
1528178354Ssam * beacon update mechanism so it can update the beacon frame
1529178354Ssam * contents, and then switch vap's to CSA state to block outbound
1530178354Ssam * traffic.  Devices that handle CSA directly can use the state
1531178354Ssam * switch to do the right thing so long as they call
1532178354Ssam * ieee80211_csa_completeswitch when it's time to complete the
1533178354Ssam * channel change.  Devices that depend on the net80211 layer can
1534178354Ssam * use ieee80211_beacon_update to handle the countdown and the
1535178354Ssam * channel switch.
1536178354Ssam */
1537178354Ssamvoid
1538178354Ssamieee80211_csa_startswitch(struct ieee80211com *ic,
1539178354Ssam	struct ieee80211_channel *c, int mode, int count)
1540178354Ssam{
1541178354Ssam	struct ieee80211vap *vap;
1542178354Ssam
1543178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1544178354Ssam
1545178354Ssam	ic->ic_csa_newchan = c;
1546193439Ssam	ic->ic_csa_mode = mode;
1547178354Ssam	ic->ic_csa_count = count;
1548178354Ssam	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1549178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1550178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1551195618Srpaulo		    vap->iv_opmode == IEEE80211_M_IBSS ||
1552195618Srpaulo		    vap->iv_opmode == IEEE80211_M_MBSS)
1553178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1554178354Ssam		/* switch to CSA state to block outbound traffic */
1555178354Ssam		if (vap->iv_state == IEEE80211_S_RUN)
1556178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1557178354Ssam	}
1558178354Ssam	ieee80211_notify_csa(ic, c, mode, count);
1559178354Ssam}
1560178354Ssam
1561224220Sadrian/*
1562224220Sadrian * Complete the channel switch by transitioning all CSA VAPs to RUN.
1563224220Sadrian * This is called by both the completion and cancellation functions
1564224220Sadrian * so each VAP is placed back in the RUN state and can thus transmit.
1565224220Sadrian */
1566193439Ssamstatic void
1567193439Ssamcsa_completeswitch(struct ieee80211com *ic)
1568193439Ssam{
1569193439Ssam	struct ieee80211vap *vap;
1570193439Ssam
1571193439Ssam	ic->ic_csa_newchan = NULL;
1572193439Ssam	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1573193439Ssam
1574193439Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1575193439Ssam		if (vap->iv_state == IEEE80211_S_CSA)
1576193439Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1577193439Ssam}
1578193439Ssam
1579178354Ssam/*
1580178354Ssam * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1581178354Ssam * We clear state and move all vap's in CSA state to RUN state
1582178354Ssam * so they can again transmit.
1583224220Sadrian *
1584224220Sadrian * Although this may not be completely correct, update the BSS channel
1585224220Sadrian * for each VAP to the newly configured channel. The setcurchan sets
1586224220Sadrian * the current operating channel for the interface (so the radio does
1587224220Sadrian * switch over) but the VAP BSS isn't updated, leading to incorrectly
1588224220Sadrian * reported information via ioctl.
1589178354Ssam */
1590178354Ssamvoid
1591178354Ssamieee80211_csa_completeswitch(struct ieee80211com *ic)
1592178354Ssam{
1593224222Sadrian	struct ieee80211vap *vap;
1594224222Sadrian
1595178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1596178354Ssam
1597178354Ssam	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1598178354Ssam
1599178354Ssam	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1600224220Sadrian	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1601224220Sadrian		if (vap->iv_state == IEEE80211_S_CSA)
1602224220Sadrian			vap->iv_bss->ni_chan = ic->ic_curchan;
1603224220Sadrian
1604193439Ssam	csa_completeswitch(ic);
1605193439Ssam}
1606178354Ssam
1607193439Ssam/*
1608193439Ssam * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1609193439Ssam * We clear state and move all vap's in CSA state to RUN state
1610193439Ssam * so they can again transmit.
1611193439Ssam */
1612193439Ssamvoid
1613193439Ssamieee80211_csa_cancelswitch(struct ieee80211com *ic)
1614193439Ssam{
1615193439Ssam	IEEE80211_LOCK_ASSERT(ic);
1616193439Ssam
1617193439Ssam	csa_completeswitch(ic);
1618178354Ssam}
1619178354Ssam
1620178354Ssam/*
1621178354Ssam * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1622178354Ssam * We clear state and move all vap's in CAC state to RUN state.
1623178354Ssam */
1624178354Ssamvoid
1625178354Ssamieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1626178354Ssam{
1627178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1628178354Ssam	struct ieee80211vap *vap;
1629178354Ssam
1630178354Ssam	IEEE80211_LOCK(ic);
1631178354Ssam	/*
1632178354Ssam	 * Complete CAC state change for lead vap first; then
1633178354Ssam	 * clock all the other vap's waiting.
1634178354Ssam	 */
1635178354Ssam	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1636178354Ssam	    ("wrong state %d", vap0->iv_state));
1637178354Ssam	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1638178354Ssam
1639178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1640178354Ssam		if (vap->iv_state == IEEE80211_S_CAC)
1641178354Ssam			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1642178354Ssam	IEEE80211_UNLOCK(ic);
1643178354Ssam}
1644178354Ssam
1645178354Ssam/*
1646178354Ssam * Force all vap's other than the specified vap to the INIT state
1647178354Ssam * and mark them as waiting for a scan to complete.  These vaps
1648178354Ssam * will be brought up when the scan completes and the scanning vap
1649178354Ssam * reaches RUN state by wakeupwaiting.
1650178354Ssam */
1651154736Ssamstatic void
1652178354Ssammarkwaiting(struct ieee80211vap *vap0)
1653147765Ssam{
1654178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1655178354Ssam	struct ieee80211vap *vap;
1656147765Ssam
1657178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1658178354Ssam
1659191746Sthompsa	/*
1660191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1661191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1662191746Sthompsa	 * change task.
1663191746Sthompsa	 */
1664178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1665178354Ssam		if (vap == vap0)
1666178354Ssam			continue;
1667178354Ssam		if (vap->iv_state != IEEE80211_S_INIT) {
1668191746Sthompsa			/* NB: iv_newstate may drop the lock */
1669178354Ssam			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1670232096Sadrian			IEEE80211_LOCK_ASSERT(ic);
1671178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1672178354Ssam		}
1673147765Ssam	}
1674147765Ssam}
1675147765Ssam
1676178354Ssam/*
1677178354Ssam * Wakeup all vap's waiting for a scan to complete.  This is the
1678178354Ssam * companion to markwaiting (above) and is used to coordinate
1679178354Ssam * multiple vaps scanning.
1680191746Sthompsa * This is called from the state taskqueue.
1681178354Ssam */
1682147765Ssamstatic void
1683178354Ssamwakeupwaiting(struct ieee80211vap *vap0)
1684147765Ssam{
1685178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
1686178354Ssam	struct ieee80211vap *vap;
1687147765Ssam
1688178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1689178354Ssam
1690191746Sthompsa	/*
1691191746Sthompsa	 * A vap list entry can not disappear since we are running on the
1692191746Sthompsa	 * taskqueue and a vap destroy will queue and drain another state
1693191746Sthompsa	 * change task.
1694191746Sthompsa	 */
1695178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1696178354Ssam		if (vap == vap0)
1697178354Ssam			continue;
1698178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1699178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1700178354Ssam			/* NB: sta's cannot go INIT->RUN */
1701191746Sthompsa			/* NB: iv_newstate may drop the lock */
1702178354Ssam			vap->iv_newstate(vap,
1703178354Ssam			    vap->iv_opmode == IEEE80211_M_STA ?
1704178354Ssam			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1705232096Sadrian			IEEE80211_LOCK_ASSERT(ic);
1706178354Ssam		}
1707178354Ssam	}
1708147765Ssam}
1709147765Ssam
1710170530Ssam/*
1711178354Ssam * Handle post state change work common to all operating modes.
1712170530Ssam */
1713170530Ssamstatic void
1714191746Sthompsaieee80211_newstate_cb(void *xvap, int npending)
1715170530Ssam{
1716191746Sthompsa	struct ieee80211vap *vap = xvap;
1717178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1718191746Sthompsa	enum ieee80211_state nstate, ostate;
1719191746Sthompsa	int arg, rc;
1720178354Ssam
1721191746Sthompsa	IEEE80211_LOCK(ic);
1722191746Sthompsa	nstate = vap->iv_nstate;
1723191746Sthompsa	arg = vap->iv_nstate_arg;
1724178354Ssam
1725191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1726191746Sthompsa		/*
1727191746Sthompsa		 * We have been requested to drop back to the INIT before
1728191746Sthompsa		 * proceeding to the new state.
1729191746Sthompsa		 */
1730191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1731191746Sthompsa		    "%s: %s -> %s arg %d\n", __func__,
1732191746Sthompsa		    ieee80211_state_name[vap->iv_state],
1733191746Sthompsa		    ieee80211_state_name[IEEE80211_S_INIT], arg);
1734191746Sthompsa		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1735232096Sadrian		IEEE80211_LOCK_ASSERT(ic);
1736191746Sthompsa		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1737191746Sthompsa	}
1738191746Sthompsa
1739191746Sthompsa	ostate = vap->iv_state;
1740191746Sthompsa	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1741191746Sthompsa		/*
1742191746Sthompsa		 * SCAN was forced; e.g. on beacon miss.  Force other running
1743191746Sthompsa		 * vap's to INIT state and mark them as waiting for the scan to
1744191746Sthompsa		 * complete.  This insures they don't interfere with our
1745191746Sthompsa		 * scanning.  Since we are single threaded the vaps can not
1746191746Sthompsa		 * transition again while we are executing.
1747191746Sthompsa		 *
1748191746Sthompsa		 * XXX not always right, assumes ap follows sta
1749191746Sthompsa		 */
1750191746Sthompsa		markwaiting(vap);
1751191746Sthompsa	}
1752178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1753191746Sthompsa	    "%s: %s -> %s arg %d\n", __func__,
1754191746Sthompsa	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1755178354Ssam
1756191746Sthompsa	rc = vap->iv_newstate(vap, nstate, arg);
1757232096Sadrian	IEEE80211_LOCK_ASSERT(ic);
1758191746Sthompsa	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1759191746Sthompsa	if (rc != 0) {
1760191746Sthompsa		/* State transition failed */
1761191746Sthompsa		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1762191746Sthompsa		KASSERT(nstate != IEEE80211_S_INIT,
1763191746Sthompsa		    ("INIT state change failed"));
1764191746Sthompsa		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1765191746Sthompsa		    "%s: %s returned error %d\n", __func__,
1766191746Sthompsa		    ieee80211_state_name[nstate], rc);
1767191746Sthompsa		goto done;
1768191746Sthompsa	}
1769191746Sthompsa
1770191746Sthompsa	/* No actual transition, skip post processing */
1771191746Sthompsa	if (ostate == nstate)
1772191746Sthompsa		goto done;
1773191746Sthompsa
1774178354Ssam	if (nstate == IEEE80211_S_RUN) {
1775178354Ssam		/*
1776178354Ssam		 * OACTIVE may be set on the vap if the upper layer
1777178354Ssam		 * tried to transmit (e.g. IPv6 NDP) before we reach
1778178354Ssam		 * RUN state.  Clear it and restart xmit.
1779178354Ssam		 *
1780178354Ssam		 * Note this can also happen as a result of SLEEP->RUN
1781178354Ssam		 * (i.e. coming out of power save mode).
1782178354Ssam		 */
1783178354Ssam		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1784244577Sadrian
1785244577Sadrian		/*
1786254082Sadrian		 * XXX TODO Kick-start a VAP queue - this should be a method!
1787244577Sadrian		 */
1788178354Ssam
1789178354Ssam		/* bring up any vaps waiting on us */
1790178354Ssam		wakeupwaiting(vap);
1791178354Ssam	} else if (nstate == IEEE80211_S_INIT) {
1792178354Ssam		/*
1793178354Ssam		 * Flush the scan cache if we did the last scan (XXX?)
1794178354Ssam		 * and flush any frames on send queues from this vap.
1795178354Ssam		 * Note the mgt q is used only for legacy drivers and
1796178354Ssam		 * will go away shortly.
1797178354Ssam		 */
1798178354Ssam		ieee80211_scan_flush(vap);
1799178354Ssam
1800254082Sadrian		/*
1801254082Sadrian		 * XXX TODO: ic/vap queue flush
1802254082Sadrian		 */
1803170530Ssam	}
1804191746Sthompsadone:
1805191746Sthompsa	IEEE80211_UNLOCK(ic);
1806170530Ssam}
1807170530Ssam
1808178354Ssam/*
1809178354Ssam * Public interface for initiating a state machine change.
1810178354Ssam * This routine single-threads the request and coordinates
1811178354Ssam * the scheduling of multiple vaps for the purpose of selecting
1812178354Ssam * an operating channel.  Specifically the following scenarios
1813178354Ssam * are handled:
1814178354Ssam * o only one vap can be selecting a channel so on transition to
1815178354Ssam *   SCAN state if another vap is already scanning then
1816178354Ssam *   mark the caller for later processing and return without
1817178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1818178354Ssam * o only one vap can be doing CAC of a channel so on transition to
1819178354Ssam *   CAC state if another vap is already scanning for radar then
1820178354Ssam *   mark the caller for later processing and return without
1821178354Ssam *   doing anything (XXX? expectations by caller of synchronous operation)
1822178354Ssam * o if another vap is already running when a request is made
1823178354Ssam *   to SCAN then an operating channel has been chosen; bypass
1824178354Ssam *   the scan and just join the channel
1825178354Ssam *
1826178354Ssam * Note that the state change call is done through the iv_newstate
1827178354Ssam * method pointer so any driver routine gets invoked.  The driver
1828178354Ssam * will normally call back into operating mode-specific
1829178354Ssam * ieee80211_newstate routines (below) unless it needs to completely
1830178354Ssam * bypass the state machine (e.g. because the firmware has it's
1831178354Ssam * own idea how things should work).  Bypassing the net80211 layer
1832178354Ssam * is usually a mistake and indicates lack of proper integration
1833178354Ssam * with the net80211 layer.
1834178354Ssam */
1835259188Sgavinint
1836178354Ssamieee80211_new_state_locked(struct ieee80211vap *vap,
1837178354Ssam	enum ieee80211_state nstate, int arg)
1838116742Ssam{
1839178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1840178354Ssam	struct ieee80211vap *vp;
1841117811Ssam	enum ieee80211_state ostate;
1842191746Sthompsa	int nrunning, nscanning;
1843116742Ssam
1844178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1845178354Ssam
1846191746Sthompsa	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1847191746Sthompsa		if (vap->iv_nstate == IEEE80211_S_INIT) {
1848191746Sthompsa			/*
1849191746Sthompsa			 * XXX The vap is being stopped, do no allow any other
1850191746Sthompsa			 * state changes until this is completed.
1851191746Sthompsa			 */
1852191746Sthompsa			return -1;
1853191768Sthompsa		} else if (vap->iv_state != vap->iv_nstate) {
1854191746Sthompsa#if 0
1855191768Sthompsa			/* Warn if the previous state hasn't completed. */
1856191768Sthompsa			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1857191768Sthompsa			    "%s: pending %s -> %s transition lost\n", __func__,
1858191768Sthompsa			    ieee80211_state_name[vap->iv_state],
1859191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1860191746Sthompsa#else
1861191768Sthompsa			/* XXX temporarily enable to identify issues */
1862191768Sthompsa			if_printf(vap->iv_ifp,
1863191768Sthompsa			    "%s: pending %s -> %s transition lost\n",
1864191768Sthompsa			    __func__, ieee80211_state_name[vap->iv_state],
1865191768Sthompsa			    ieee80211_state_name[vap->iv_nstate]);
1866191746Sthompsa#endif
1867191768Sthompsa		}
1868191746Sthompsa	}
1869191746Sthompsa
1870178354Ssam	nrunning = nscanning = 0;
1871178354Ssam	/* XXX can track this state instead of calculating */
1872178354Ssam	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1873178354Ssam		if (vp != vap) {
1874178354Ssam			if (vp->iv_state >= IEEE80211_S_RUN)
1875178354Ssam				nrunning++;
1876178354Ssam			/* XXX doesn't handle bg scan */
1877178354Ssam			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1878178354Ssam			else if (vp->iv_state > IEEE80211_S_INIT)
1879178354Ssam				nscanning++;
1880178354Ssam		}
1881178354Ssam	}
1882178354Ssam	ostate = vap->iv_state;
1883178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1884178354Ssam	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1885178354Ssam	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1886178354Ssam	    nrunning, nscanning);
1887116742Ssam	switch (nstate) {
1888116742Ssam	case IEEE80211_S_SCAN:
1889178354Ssam		if (ostate == IEEE80211_S_INIT) {
1890178354Ssam			/*
1891178354Ssam			 * INIT -> SCAN happens on initial bringup.
1892178354Ssam			 */
1893178354Ssam			KASSERT(!(nscanning && nrunning),
1894178354Ssam			    ("%d scanning and %d running", nscanning, nrunning));
1895178354Ssam			if (nscanning) {
1896116742Ssam				/*
1897178354Ssam				 * Someone is scanning, defer our state
1898178354Ssam				 * change until the work has completed.
1899116742Ssam				 */
1900178354Ssam				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1901178354Ssam				    "%s: defer %s -> %s\n",
1902178354Ssam				    __func__, ieee80211_state_name[ostate],
1903178354Ssam				    ieee80211_state_name[nstate]);
1904178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1905191746Sthompsa				return 0;
1906116742Ssam			}
1907178354Ssam			if (nrunning) {
1908170530Ssam				/*
1909178354Ssam				 * Someone is operating; just join the channel
1910178354Ssam				 * they have chosen.
1911170530Ssam				 */
1912178354Ssam				/* XXX kill arg? */
1913178354Ssam				/* XXX check each opmode, adhoc? */
1914178354Ssam				if (vap->iv_opmode == IEEE80211_M_STA)
1915178354Ssam					nstate = IEEE80211_S_SCAN;
1916178354Ssam				else
1917178354Ssam					nstate = IEEE80211_S_RUN;
1918138568Ssam#ifdef IEEE80211_DEBUG
1919178354Ssam				if (nstate != IEEE80211_S_SCAN) {
1920178354Ssam					IEEE80211_DPRINTF(vap,
1921178354Ssam					    IEEE80211_MSG_STATE,
1922178354Ssam					    "%s: override, now %s -> %s\n",
1923178354Ssam					    __func__,
1924178354Ssam					    ieee80211_state_name[ostate],
1925178354Ssam					    ieee80211_state_name[nstate]);
1926178354Ssam				}
1927138568Ssam#endif
1928170530Ssam			}
1929116742Ssam		}
1930178354Ssam		break;
1931178354Ssam	case IEEE80211_S_RUN:
1932178354Ssam		if (vap->iv_opmode == IEEE80211_M_WDS &&
1933178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1934178354Ssam		    nscanning) {
1935154736Ssam			/*
1936178354Ssam			 * Legacy WDS with someone else scanning; don't
1937178354Ssam			 * go online until that completes as we should
1938178354Ssam			 * follow the other vap to the channel they choose.
1939154736Ssam			 */
1940178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1941178354Ssam			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
1942178354Ssam			     ieee80211_state_name[ostate],
1943178354Ssam			     ieee80211_state_name[nstate]);
1944178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1945191746Sthompsa			return 0;
1946154736Ssam		}
1947178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1948178354Ssam		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1949178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1950178354Ssam		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1951178354Ssam			/*
1952178354Ssam			 * This is a DFS channel, transition to CAC state
1953178354Ssam			 * instead of RUN.  This allows us to initiate
1954178354Ssam			 * Channel Availability Check (CAC) as specified
1955178354Ssam			 * by 11h/DFS.
1956178354Ssam			 */
1957178354Ssam			nstate = IEEE80211_S_CAC;
1958178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1959178354Ssam			     "%s: override %s -> %s (DFS)\n", __func__,
1960178354Ssam			     ieee80211_state_name[ostate],
1961178354Ssam			     ieee80211_state_name[nstate]);
1962138568Ssam		}
1963116742Ssam		break;
1964178354Ssam	case IEEE80211_S_INIT:
1965191955Sthompsa		/* cancel any scan in progress */
1966191955Sthompsa		ieee80211_cancel_scan(vap);
1967178354Ssam		if (ostate == IEEE80211_S_INIT ) {
1968178354Ssam			/* XXX don't believe this */
1969178354Ssam			/* INIT -> INIT. nothing to do */
1970178354Ssam			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1971178354Ssam		}
1972178354Ssam		/* fall thru... */
1973172058Ssam	default:
1974172058Ssam		break;
1975116742Ssam	}
1976191746Sthompsa	/* defer the state change to a thread */
1977191746Sthompsa	vap->iv_nstate = nstate;
1978191746Sthompsa	vap->iv_nstate_arg = arg;
1979191746Sthompsa	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1980191746Sthompsa	ieee80211_runtask(ic, &vap->iv_nstate_task);
1981191746Sthompsa	return EINPROGRESS;
1982116742Ssam}
1983178354Ssam
1984178354Ssamint
1985178354Ssamieee80211_new_state(struct ieee80211vap *vap,
1986178354Ssam	enum ieee80211_state nstate, int arg)
1987178354Ssam{
1988178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1989178354Ssam	int rc;
1990178354Ssam
1991178354Ssam	IEEE80211_LOCK(ic);
1992178354Ssam	rc = ieee80211_new_state_locked(vap, nstate, arg);
1993178354Ssam	IEEE80211_UNLOCK(ic);
1994178354Ssam	return rc;
1995178354Ssam}
1996