ieee80211_proto.c revision 192468
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_proto.c 192468 2009-05-20 20:00:40Z sam $");
29
30/*
31 * IEEE 802.11 protocol support.
32 */
33
34#include "opt_inet.h"
35#include "opt_wlan.h"
36
37#include <sys/param.h>
38#include <sys/kernel.h>
39#include <sys/systm.h>
40
41#include <sys/socket.h>
42#include <sys/sockio.h>
43
44#include <net/if.h>
45#include <net/if_media.h>
46#include <net/ethernet.h>		/* XXX for ether_sprintf */
47
48#include <net80211/ieee80211_var.h>
49#include <net80211/ieee80211_adhoc.h>
50#include <net80211/ieee80211_sta.h>
51#include <net80211/ieee80211_hostap.h>
52#include <net80211/ieee80211_wds.h>
53#include <net80211/ieee80211_monitor.h>
54#include <net80211/ieee80211_input.h>
55
56/* XXX tunables */
57#define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
58#define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
59
60const char *ieee80211_mgt_subtype_name[] = {
61	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
62	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
63	"beacon",	"atim",		"disassoc",	"auth",
64	"deauth",	"action",	"reserved#14",	"reserved#15"
65};
66const char *ieee80211_ctl_subtype_name[] = {
67	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
68	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
69	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
70	"cts",		"ack",		"cf_end",	"cf_end_ack"
71};
72const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
73	"IBSS",		/* IEEE80211_M_IBSS */
74	"STA",		/* IEEE80211_M_STA */
75	"WDS",		/* IEEE80211_M_WDS */
76	"AHDEMO",	/* IEEE80211_M_AHDEMO */
77	"HOSTAP",	/* IEEE80211_M_HOSTAP */
78	"MONITOR"	/* IEEE80211_M_MONITOR */
79};
80const char *ieee80211_state_name[IEEE80211_S_MAX] = {
81	"INIT",		/* IEEE80211_S_INIT */
82	"SCAN",		/* IEEE80211_S_SCAN */
83	"AUTH",		/* IEEE80211_S_AUTH */
84	"ASSOC",	/* IEEE80211_S_ASSOC */
85	"CAC",		/* IEEE80211_S_CAC */
86	"RUN",		/* IEEE80211_S_RUN */
87	"CSA",		/* IEEE80211_S_CSA */
88	"SLEEP",	/* IEEE80211_S_SLEEP */
89};
90const char *ieee80211_wme_acnames[] = {
91	"WME_AC_BE",
92	"WME_AC_BK",
93	"WME_AC_VI",
94	"WME_AC_VO",
95	"WME_UPSD",
96};
97
98static void beacon_miss(void *, int);
99static void beacon_swmiss(void *, int);
100static void parent_updown(void *, int);
101static void update_mcast(void *, int);
102static void update_promisc(void *, int);
103static void update_channel(void *, int);
104static void ieee80211_newstate_cb(void *, int);
105static int ieee80211_new_state_locked(struct ieee80211vap *,
106	enum ieee80211_state, int);
107
108static int
109null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
110	const struct ieee80211_bpf_params *params)
111{
112	struct ifnet *ifp = ni->ni_ic->ic_ifp;
113
114	if_printf(ifp, "missing ic_raw_xmit callback, drop frame\n");
115	m_freem(m);
116	return ENETDOWN;
117}
118
119void
120ieee80211_proto_attach(struct ieee80211com *ic)
121{
122	struct ifnet *ifp = ic->ic_ifp;
123
124	/* override the 802.3 setting */
125	ifp->if_hdrlen = ic->ic_headroom
126		+ sizeof(struct ieee80211_qosframe_addr4)
127		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
128		+ IEEE80211_WEP_EXTIVLEN;
129	/* XXX no way to recalculate on ifdetach */
130	if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
131		/* XXX sanity check... */
132		max_linkhdr = ALIGN(ifp->if_hdrlen);
133		max_hdr = max_linkhdr + max_protohdr;
134		max_datalen = MHLEN - max_hdr;
135	}
136	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
137
138	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ifp);
139	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
140	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
141	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
142	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
143
144	ic->ic_wme.wme_hipri_switch_hysteresis =
145		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
146
147	/* initialize management frame handlers */
148	ic->ic_send_mgmt = ieee80211_send_mgmt;
149	ic->ic_raw_xmit = null_raw_xmit;
150
151	ieee80211_adhoc_attach(ic);
152	ieee80211_sta_attach(ic);
153	ieee80211_wds_attach(ic);
154	ieee80211_hostap_attach(ic);
155	ieee80211_monitor_attach(ic);
156}
157
158void
159ieee80211_proto_detach(struct ieee80211com *ic)
160{
161	ieee80211_monitor_detach(ic);
162	ieee80211_hostap_detach(ic);
163	ieee80211_wds_detach(ic);
164	ieee80211_adhoc_detach(ic);
165	ieee80211_sta_detach(ic);
166}
167
168static void
169null_update_beacon(struct ieee80211vap *vap, int item)
170{
171}
172
173void
174ieee80211_proto_vattach(struct ieee80211vap *vap)
175{
176	struct ieee80211com *ic = vap->iv_ic;
177	struct ifnet *ifp = vap->iv_ifp;
178	int i;
179
180	/* override the 802.3 setting */
181	ifp->if_hdrlen = ic->ic_ifp->if_hdrlen;
182
183	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
184	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
185	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
186	callout_init(&vap->iv_swbmiss, CALLOUT_MPSAFE);
187	callout_init(&vap->iv_mgtsend, CALLOUT_MPSAFE);
188	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
189	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
190	/*
191	 * Install default tx rate handling: no fixed rate, lowest
192	 * supported rate for mgmt and multicast frames.  Default
193	 * max retry count.  These settings can be changed by the
194	 * driver and/or user applications.
195	 */
196	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
197		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
198
199		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
200		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
201			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
202			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
203		} else {
204			vap->iv_txparms[i].mgmtrate =
205			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
206			vap->iv_txparms[i].mcastrate =
207			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
208		}
209		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
210	}
211	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
212
213	vap->iv_update_beacon = null_update_beacon;
214	vap->iv_deliver_data = ieee80211_deliver_data;
215
216	/* attach support for operating mode */
217	ic->ic_vattach[vap->iv_opmode](vap);
218}
219
220void
221ieee80211_proto_vdetach(struct ieee80211vap *vap)
222{
223#define	FREEAPPIE(ie) do { \
224	if (ie != NULL) \
225		free(ie, M_80211_NODE_IE); \
226} while (0)
227	/*
228	 * Detach operating mode module.
229	 */
230	if (vap->iv_opdetach != NULL)
231		vap->iv_opdetach(vap);
232	/*
233	 * This should not be needed as we detach when reseting
234	 * the state but be conservative here since the
235	 * authenticator may do things like spawn kernel threads.
236	 */
237	if (vap->iv_auth->ia_detach != NULL)
238		vap->iv_auth->ia_detach(vap);
239	/*
240	 * Detach any ACL'ator.
241	 */
242	if (vap->iv_acl != NULL)
243		vap->iv_acl->iac_detach(vap);
244
245	FREEAPPIE(vap->iv_appie_beacon);
246	FREEAPPIE(vap->iv_appie_probereq);
247	FREEAPPIE(vap->iv_appie_proberesp);
248	FREEAPPIE(vap->iv_appie_assocreq);
249	FREEAPPIE(vap->iv_appie_assocresp);
250	FREEAPPIE(vap->iv_appie_wpa);
251#undef FREEAPPIE
252}
253
254/*
255 * Simple-minded authenticator module support.
256 */
257
258#define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
259/* XXX well-known names */
260static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
261	"wlan_internal",	/* IEEE80211_AUTH_NONE */
262	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
263	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
264	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
265	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
266	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
267};
268static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
269
270static const struct ieee80211_authenticator auth_internal = {
271	.ia_name		= "wlan_internal",
272	.ia_attach		= NULL,
273	.ia_detach		= NULL,
274	.ia_node_join		= NULL,
275	.ia_node_leave		= NULL,
276};
277
278/*
279 * Setup internal authenticators once; they are never unregistered.
280 */
281static void
282ieee80211_auth_setup(void)
283{
284	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
285	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
286	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
287}
288SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
289
290const struct ieee80211_authenticator *
291ieee80211_authenticator_get(int auth)
292{
293	if (auth >= IEEE80211_AUTH_MAX)
294		return NULL;
295	if (authenticators[auth] == NULL)
296		ieee80211_load_module(auth_modnames[auth]);
297	return authenticators[auth];
298}
299
300void
301ieee80211_authenticator_register(int type,
302	const struct ieee80211_authenticator *auth)
303{
304	if (type >= IEEE80211_AUTH_MAX)
305		return;
306	authenticators[type] = auth;
307}
308
309void
310ieee80211_authenticator_unregister(int type)
311{
312
313	if (type >= IEEE80211_AUTH_MAX)
314		return;
315	authenticators[type] = NULL;
316}
317
318/*
319 * Very simple-minded ACL module support.
320 */
321/* XXX just one for now */
322static	const struct ieee80211_aclator *acl = NULL;
323
324void
325ieee80211_aclator_register(const struct ieee80211_aclator *iac)
326{
327	printf("wlan: %s acl policy registered\n", iac->iac_name);
328	acl = iac;
329}
330
331void
332ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
333{
334	if (acl == iac)
335		acl = NULL;
336	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
337}
338
339const struct ieee80211_aclator *
340ieee80211_aclator_get(const char *name)
341{
342	if (acl == NULL)
343		ieee80211_load_module("wlan_acl");
344	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
345}
346
347void
348ieee80211_print_essid(const uint8_t *essid, int len)
349{
350	const uint8_t *p;
351	int i;
352
353	if (len > IEEE80211_NWID_LEN)
354		len = IEEE80211_NWID_LEN;
355	/* determine printable or not */
356	for (i = 0, p = essid; i < len; i++, p++) {
357		if (*p < ' ' || *p > 0x7e)
358			break;
359	}
360	if (i == len) {
361		printf("\"");
362		for (i = 0, p = essid; i < len; i++, p++)
363			printf("%c", *p);
364		printf("\"");
365	} else {
366		printf("0x");
367		for (i = 0, p = essid; i < len; i++, p++)
368			printf("%02x", *p);
369	}
370}
371
372void
373ieee80211_dump_pkt(struct ieee80211com *ic,
374	const uint8_t *buf, int len, int rate, int rssi)
375{
376	const struct ieee80211_frame *wh;
377	int i;
378
379	wh = (const struct ieee80211_frame *)buf;
380	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
381	case IEEE80211_FC1_DIR_NODS:
382		printf("NODS %s", ether_sprintf(wh->i_addr2));
383		printf("->%s", ether_sprintf(wh->i_addr1));
384		printf("(%s)", ether_sprintf(wh->i_addr3));
385		break;
386	case IEEE80211_FC1_DIR_TODS:
387		printf("TODS %s", ether_sprintf(wh->i_addr2));
388		printf("->%s", ether_sprintf(wh->i_addr3));
389		printf("(%s)", ether_sprintf(wh->i_addr1));
390		break;
391	case IEEE80211_FC1_DIR_FROMDS:
392		printf("FRDS %s", ether_sprintf(wh->i_addr3));
393		printf("->%s", ether_sprintf(wh->i_addr1));
394		printf("(%s)", ether_sprintf(wh->i_addr2));
395		break;
396	case IEEE80211_FC1_DIR_DSTODS:
397		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
398		printf("->%s", ether_sprintf(wh->i_addr3));
399		printf("(%s", ether_sprintf(wh->i_addr2));
400		printf("->%s)", ether_sprintf(wh->i_addr1));
401		break;
402	}
403	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
404	case IEEE80211_FC0_TYPE_DATA:
405		printf(" data");
406		break;
407	case IEEE80211_FC0_TYPE_MGT:
408		printf(" %s", ieee80211_mgt_subtype_name[
409		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
410		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
411		break;
412	default:
413		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
414		break;
415	}
416	if (IEEE80211_QOS_HAS_SEQ(wh)) {
417		const struct ieee80211_qosframe *qwh =
418			(const struct ieee80211_qosframe *)buf;
419		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
420			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
421	}
422	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
423		int off;
424
425		off = ieee80211_anyhdrspace(ic, wh);
426		printf(" WEP [IV %.02x %.02x %.02x",
427			buf[off+0], buf[off+1], buf[off+2]);
428		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
429			printf(" %.02x %.02x %.02x",
430				buf[off+4], buf[off+5], buf[off+6]);
431		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
432	}
433	if (rate >= 0)
434		printf(" %dM", rate / 2);
435	if (rssi >= 0)
436		printf(" +%d", rssi);
437	printf("\n");
438	if (len > 0) {
439		for (i = 0; i < len; i++) {
440			if ((i & 1) == 0)
441				printf(" ");
442			printf("%02x", buf[i]);
443		}
444		printf("\n");
445	}
446}
447
448static __inline int
449findrix(const struct ieee80211_rateset *rs, int r)
450{
451	int i;
452
453	for (i = 0; i < rs->rs_nrates; i++)
454		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
455			return i;
456	return -1;
457}
458
459int
460ieee80211_fix_rate(struct ieee80211_node *ni,
461	struct ieee80211_rateset *nrs, int flags)
462{
463#define	RV(v)	((v) & IEEE80211_RATE_VAL)
464	struct ieee80211vap *vap = ni->ni_vap;
465	struct ieee80211com *ic = ni->ni_ic;
466	int i, j, rix, error;
467	int okrate, badrate, fixedrate, ucastrate;
468	const struct ieee80211_rateset *srs;
469	uint8_t r;
470
471	error = 0;
472	okrate = badrate = 0;
473	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
474	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
475		/*
476		 * Workaround awkwardness with fixed rate.  We are called
477		 * to check both the legacy rate set and the HT rate set
478		 * but we must apply any legacy fixed rate check only to the
479		 * legacy rate set and vice versa.  We cannot tell what type
480		 * of rate set we've been given (legacy or HT) but we can
481		 * distinguish the fixed rate type (MCS have 0x80 set).
482		 * So to deal with this the caller communicates whether to
483		 * check MCS or legacy rate using the flags and we use the
484		 * type of any fixed rate to avoid applying an MCS to a
485		 * legacy rate and vice versa.
486		 */
487		if (ucastrate & 0x80) {
488			if (flags & IEEE80211_F_DOFRATE)
489				flags &= ~IEEE80211_F_DOFRATE;
490		} else if ((ucastrate & 0x80) == 0) {
491			if (flags & IEEE80211_F_DOFMCS)
492				flags &= ~IEEE80211_F_DOFMCS;
493		}
494		/* NB: required to make MCS match below work */
495		ucastrate &= IEEE80211_RATE_VAL;
496	}
497	fixedrate = IEEE80211_FIXED_RATE_NONE;
498	/*
499	 * XXX we are called to process both MCS and legacy rates;
500	 * we must use the appropriate basic rate set or chaos will
501	 * ensue; for now callers that want MCS must supply
502	 * IEEE80211_F_DOBRS; at some point we'll need to split this
503	 * function so there are two variants, one for MCS and one
504	 * for legacy rates.
505	 */
506	if (flags & IEEE80211_F_DOBRS)
507		srs = (const struct ieee80211_rateset *)
508		    ieee80211_get_suphtrates(ic, ni->ni_chan);
509	else
510		srs = ieee80211_get_suprates(ic, ni->ni_chan);
511	for (i = 0; i < nrs->rs_nrates; ) {
512		if (flags & IEEE80211_F_DOSORT) {
513			/*
514			 * Sort rates.
515			 */
516			for (j = i + 1; j < nrs->rs_nrates; j++) {
517				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
518					r = nrs->rs_rates[i];
519					nrs->rs_rates[i] = nrs->rs_rates[j];
520					nrs->rs_rates[j] = r;
521				}
522			}
523		}
524		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
525		badrate = r;
526		/*
527		 * Check for fixed rate.
528		 */
529		if (r == ucastrate)
530			fixedrate = r;
531		/*
532		 * Check against supported rates.
533		 */
534		rix = findrix(srs, r);
535		if (flags & IEEE80211_F_DONEGO) {
536			if (rix < 0) {
537				/*
538				 * A rate in the node's rate set is not
539				 * supported.  If this is a basic rate and we
540				 * are operating as a STA then this is an error.
541				 * Otherwise we just discard/ignore the rate.
542				 */
543				if ((flags & IEEE80211_F_JOIN) &&
544				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
545					error++;
546			} else if ((flags & IEEE80211_F_JOIN) == 0) {
547				/*
548				 * Overwrite with the supported rate
549				 * value so any basic rate bit is set.
550				 */
551				nrs->rs_rates[i] = srs->rs_rates[rix];
552			}
553		}
554		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
555			/*
556			 * Delete unacceptable rates.
557			 */
558			nrs->rs_nrates--;
559			for (j = i; j < nrs->rs_nrates; j++)
560				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
561			nrs->rs_rates[j] = 0;
562			continue;
563		}
564		if (rix >= 0)
565			okrate = nrs->rs_rates[i];
566		i++;
567	}
568	if (okrate == 0 || error != 0 ||
569	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
570	     fixedrate != ucastrate)) {
571		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
572		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
573		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
574		return badrate | IEEE80211_RATE_BASIC;
575	} else
576		return RV(okrate);
577#undef RV
578}
579
580/*
581 * Reset 11g-related state.
582 */
583void
584ieee80211_reset_erp(struct ieee80211com *ic)
585{
586	ic->ic_flags &= ~IEEE80211_F_USEPROT;
587	ic->ic_nonerpsta = 0;
588	ic->ic_longslotsta = 0;
589	/*
590	 * Short slot time is enabled only when operating in 11g
591	 * and not in an IBSS.  We must also honor whether or not
592	 * the driver is capable of doing it.
593	 */
594	ieee80211_set_shortslottime(ic,
595		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
596		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
597		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
598		ic->ic_opmode == IEEE80211_M_HOSTAP &&
599		(ic->ic_caps & IEEE80211_C_SHSLOT)));
600	/*
601	 * Set short preamble and ERP barker-preamble flags.
602	 */
603	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
604	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
605		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
606		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
607	} else {
608		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
609		ic->ic_flags |= IEEE80211_F_USEBARKER;
610	}
611}
612
613/*
614 * Set the short slot time state and notify the driver.
615 */
616void
617ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
618{
619	if (onoff)
620		ic->ic_flags |= IEEE80211_F_SHSLOT;
621	else
622		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
623	/* notify driver */
624	if (ic->ic_updateslot != NULL)
625		ic->ic_updateslot(ic->ic_ifp);
626}
627
628/*
629 * Check if the specified rate set supports ERP.
630 * NB: the rate set is assumed to be sorted.
631 */
632int
633ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
634{
635#define N(a)	(sizeof(a) / sizeof(a[0]))
636	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
637	int i, j;
638
639	if (rs->rs_nrates < N(rates))
640		return 0;
641	for (i = 0; i < N(rates); i++) {
642		for (j = 0; j < rs->rs_nrates; j++) {
643			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
644			if (rates[i] == r)
645				goto next;
646			if (r > rates[i])
647				return 0;
648		}
649		return 0;
650	next:
651		;
652	}
653	return 1;
654#undef N
655}
656
657/*
658 * Mark the basic rates for the rate table based on the
659 * operating mode.  For real 11g we mark all the 11b rates
660 * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
661 * 11b rates.  There's also a pseudo 11a-mode used to mark only
662 * the basic OFDM rates.
663 */
664static void
665setbasicrates(struct ieee80211_rateset *rs,
666    enum ieee80211_phymode mode, int add)
667{
668	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
669	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
670	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
671					    /* NB: mixed b/g */
672	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
673	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
674	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
675	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
676	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
677	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
678	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
679					    /* NB: mixed b/g */
680	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
681	};
682	int i, j;
683
684	for (i = 0; i < rs->rs_nrates; i++) {
685		if (!add)
686			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
687		for (j = 0; j < basic[mode].rs_nrates; j++)
688			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
689				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
690				break;
691			}
692	}
693}
694
695/*
696 * Set the basic rates in a rate set.
697 */
698void
699ieee80211_setbasicrates(struct ieee80211_rateset *rs,
700    enum ieee80211_phymode mode)
701{
702	setbasicrates(rs, mode, 0);
703}
704
705/*
706 * Add basic rates to a rate set.
707 */
708void
709ieee80211_addbasicrates(struct ieee80211_rateset *rs,
710    enum ieee80211_phymode mode)
711{
712	setbasicrates(rs, mode, 1);
713}
714
715/*
716 * WME protocol support.
717 *
718 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
719 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
720 * Draft 2.0 Test Plan (Appendix D).
721 *
722 * Static/Dynamic Turbo mode settings come from Atheros.
723 */
724typedef struct phyParamType {
725	uint8_t		aifsn;
726	uint8_t		logcwmin;
727	uint8_t		logcwmax;
728	uint16_t	txopLimit;
729	uint8_t 	acm;
730} paramType;
731
732static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
733	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
734	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
735	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
736	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
737	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
738	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
739	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
740	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
741	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
742	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
743	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
744	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
745};
746static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
747	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
748	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
749	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
750	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
751	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
752	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
753	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
754	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
755	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
756	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
757	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
758	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
759};
760static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
761	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
762	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
763	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
764	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
765	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
766	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
767	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
768	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
769	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
770	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
771	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
772	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
773};
774static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
775	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
776	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
777	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
778	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
779	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
780	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
781	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
782	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
783	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
784	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
785	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
786	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
787};
788
789static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
790	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
791	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
792	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
793	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
794	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
795	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
796	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
797	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
798	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
799	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
800	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
801	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
802};
803static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
804	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
805	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
806	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
807	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
808	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
809	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
810	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
811	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
812	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
813	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
814	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
815	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
816};
817static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
818	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
819	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
820	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
821	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
822	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
823	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
824	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
825	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
826	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
827	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
828	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
829	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
830};
831
832static void
833_setifsparams(struct wmeParams *wmep, const paramType *phy)
834{
835	wmep->wmep_aifsn = phy->aifsn;
836	wmep->wmep_logcwmin = phy->logcwmin;
837	wmep->wmep_logcwmax = phy->logcwmax;
838	wmep->wmep_txopLimit = phy->txopLimit;
839}
840
841static void
842setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
843	struct wmeParams *wmep, const paramType *phy)
844{
845	wmep->wmep_acm = phy->acm;
846	_setifsparams(wmep, phy);
847
848	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
849	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
850	    ieee80211_wme_acnames[ac], type,
851	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
852	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
853}
854
855static void
856ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
857{
858	struct ieee80211com *ic = vap->iv_ic;
859	struct ieee80211_wme_state *wme = &ic->ic_wme;
860	const paramType *pPhyParam, *pBssPhyParam;
861	struct wmeParams *wmep;
862	enum ieee80211_phymode mode;
863	int i;
864
865	IEEE80211_LOCK_ASSERT(ic);
866
867	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
868		return;
869
870	/*
871	 * Select mode; we can be called early in which case we
872	 * always use auto mode.  We know we'll be called when
873	 * entering the RUN state with bsschan setup properly
874	 * so state will eventually get set correctly
875	 */
876	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
877		mode = ieee80211_chan2mode(ic->ic_bsschan);
878	else
879		mode = IEEE80211_MODE_AUTO;
880	for (i = 0; i < WME_NUM_AC; i++) {
881		switch (i) {
882		case WME_AC_BK:
883			pPhyParam = &phyParamForAC_BK[mode];
884			pBssPhyParam = &phyParamForAC_BK[mode];
885			break;
886		case WME_AC_VI:
887			pPhyParam = &phyParamForAC_VI[mode];
888			pBssPhyParam = &bssPhyParamForAC_VI[mode];
889			break;
890		case WME_AC_VO:
891			pPhyParam = &phyParamForAC_VO[mode];
892			pBssPhyParam = &bssPhyParamForAC_VO[mode];
893			break;
894		case WME_AC_BE:
895		default:
896			pPhyParam = &phyParamForAC_BE[mode];
897			pBssPhyParam = &bssPhyParamForAC_BE[mode];
898			break;
899		}
900		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
901		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
902			setwmeparams(vap, "chan", i, wmep, pPhyParam);
903		} else {
904			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
905		}
906		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
907		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
908	}
909	/* NB: check ic_bss to avoid NULL deref on initial attach */
910	if (vap->iv_bss != NULL) {
911		/*
912		 * Calculate agressive mode switching threshold based
913		 * on beacon interval.  This doesn't need locking since
914		 * we're only called before entering the RUN state at
915		 * which point we start sending beacon frames.
916		 */
917		wme->wme_hipri_switch_thresh =
918			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
919		wme->wme_flags &= ~WME_F_AGGRMODE;
920		ieee80211_wme_updateparams(vap);
921	}
922}
923
924void
925ieee80211_wme_initparams(struct ieee80211vap *vap)
926{
927	struct ieee80211com *ic = vap->iv_ic;
928
929	IEEE80211_LOCK(ic);
930	ieee80211_wme_initparams_locked(vap);
931	IEEE80211_UNLOCK(ic);
932}
933
934/*
935 * Update WME parameters for ourself and the BSS.
936 */
937void
938ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
939{
940	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
941	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
942	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
943	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
944	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
945	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
946	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
947	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
948	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
949	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
950	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
951	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
952	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
953	};
954	struct ieee80211com *ic = vap->iv_ic;
955	struct ieee80211_wme_state *wme = &ic->ic_wme;
956	const struct wmeParams *wmep;
957	struct wmeParams *chanp, *bssp;
958	enum ieee80211_phymode mode;
959	int i;
960
961       	/*
962	 * Set up the channel access parameters for the physical
963	 * device.  First populate the configured settings.
964	 */
965	for (i = 0; i < WME_NUM_AC; i++) {
966		chanp = &wme->wme_chanParams.cap_wmeParams[i];
967		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
968		chanp->wmep_aifsn = wmep->wmep_aifsn;
969		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
970		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
971		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
972
973		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
974		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
975		chanp->wmep_aifsn = wmep->wmep_aifsn;
976		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
977		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
978		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
979	}
980
981	/*
982	 * Select mode; we can be called early in which case we
983	 * always use auto mode.  We know we'll be called when
984	 * entering the RUN state with bsschan setup properly
985	 * so state will eventually get set correctly
986	 */
987	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
988		mode = ieee80211_chan2mode(ic->ic_bsschan);
989	else
990		mode = IEEE80211_MODE_AUTO;
991
992	/*
993	 * This implements agressive mode as found in certain
994	 * vendors' AP's.  When there is significant high
995	 * priority (VI/VO) traffic in the BSS throttle back BE
996	 * traffic by using conservative parameters.  Otherwise
997	 * BE uses agressive params to optimize performance of
998	 * legacy/non-QoS traffic.
999	 */
1000        if ((vap->iv_opmode == IEEE80211_M_HOSTAP &&
1001	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
1002	    (vap->iv_opmode == IEEE80211_M_STA &&
1003	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
1004	    (vap->iv_flags & IEEE80211_F_WME) == 0) {
1005		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1006		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1007
1008		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1009		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1010		    aggrParam[mode].logcwmin;
1011		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1012		    aggrParam[mode].logcwmax;
1013		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1014		    (vap->iv_flags & IEEE80211_F_BURST) ?
1015			aggrParam[mode].txopLimit : 0;
1016		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1017		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1018		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1019		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1020		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1021	}
1022
1023	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1024	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1025		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1026		    [IEEE80211_MODE_AUTO]	= 3,
1027		    [IEEE80211_MODE_11A]	= 3,
1028		    [IEEE80211_MODE_11B]	= 4,
1029		    [IEEE80211_MODE_11G]	= 3,
1030		    [IEEE80211_MODE_FH]		= 4,
1031		    [IEEE80211_MODE_TURBO_A]	= 3,
1032		    [IEEE80211_MODE_TURBO_G]	= 3,
1033		    [IEEE80211_MODE_STURBO_A]	= 3,
1034		    [IEEE80211_MODE_HALF]	= 3,
1035		    [IEEE80211_MODE_QUARTER]	= 3,
1036		    [IEEE80211_MODE_11NA]	= 3,
1037		    [IEEE80211_MODE_11NG]	= 3,
1038		};
1039		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1040		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1041
1042		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1043		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1044		    "update %s (chan+bss) logcwmin %u\n",
1045		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1046    	}
1047	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
1048		/*
1049		 * Arrange for a beacon update and bump the parameter
1050		 * set number so associated stations load the new values.
1051		 */
1052		wme->wme_bssChanParams.cap_info =
1053			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1054		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1055	}
1056
1057	wme->wme_update(ic);
1058
1059	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1060	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1061	    vap->iv_opmode == IEEE80211_M_STA ?
1062		wme->wme_wmeChanParams.cap_info :
1063		wme->wme_bssChanParams.cap_info);
1064}
1065
1066void
1067ieee80211_wme_updateparams(struct ieee80211vap *vap)
1068{
1069	struct ieee80211com *ic = vap->iv_ic;
1070
1071	if (ic->ic_caps & IEEE80211_C_WME) {
1072		IEEE80211_LOCK(ic);
1073		ieee80211_wme_updateparams_locked(vap);
1074		IEEE80211_UNLOCK(ic);
1075	}
1076}
1077
1078static void
1079parent_updown(void *arg, int npending)
1080{
1081	struct ifnet *parent = arg;
1082
1083	parent->if_ioctl(parent, SIOCSIFFLAGS, NULL);
1084}
1085
1086static void
1087update_mcast(void *arg, int npending)
1088{
1089	struct ieee80211com *ic = arg;
1090	struct ifnet *parent = ic->ic_ifp;
1091
1092	ic->ic_update_mcast(parent);
1093}
1094
1095static void
1096update_promisc(void *arg, int npending)
1097{
1098	struct ieee80211com *ic = arg;
1099	struct ifnet *parent = ic->ic_ifp;
1100
1101	ic->ic_update_promisc(parent);
1102}
1103
1104static void
1105update_channel(void *arg, int npending)
1106{
1107	struct ieee80211com *ic = arg;
1108
1109	ic->ic_set_channel(ic);
1110	ieee80211_radiotap_chan_change(ic);
1111}
1112
1113/*
1114 * Block until the parent is in a known state.  This is
1115 * used after any operations that dispatch a task (e.g.
1116 * to auto-configure the parent device up/down).
1117 */
1118void
1119ieee80211_waitfor_parent(struct ieee80211com *ic)
1120{
1121	taskqueue_block(ic->ic_tq);
1122	ieee80211_draintask(ic, &ic->ic_parent_task);
1123	ieee80211_draintask(ic, &ic->ic_mcast_task);
1124	ieee80211_draintask(ic, &ic->ic_promisc_task);
1125	ieee80211_draintask(ic, &ic->ic_chan_task);
1126	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1127	taskqueue_unblock(ic->ic_tq);
1128}
1129
1130/*
1131 * Start a vap running.  If this is the first vap to be
1132 * set running on the underlying device then we
1133 * automatically bring the device up.
1134 */
1135void
1136ieee80211_start_locked(struct ieee80211vap *vap)
1137{
1138	struct ifnet *ifp = vap->iv_ifp;
1139	struct ieee80211com *ic = vap->iv_ic;
1140	struct ifnet *parent = ic->ic_ifp;
1141
1142	IEEE80211_LOCK_ASSERT(ic);
1143
1144	IEEE80211_DPRINTF(vap,
1145		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1146		"start running, %d vaps running\n", ic->ic_nrunning);
1147
1148	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1149		/*
1150		 * Mark us running.  Note that it's ok to do this first;
1151		 * if we need to bring the parent device up we defer that
1152		 * to avoid dropping the com lock.  We expect the device
1153		 * to respond to being marked up by calling back into us
1154		 * through ieee80211_start_all at which point we'll come
1155		 * back in here and complete the work.
1156		 */
1157		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1158		/*
1159		 * We are not running; if this we are the first vap
1160		 * to be brought up auto-up the parent if necessary.
1161		 */
1162		if (ic->ic_nrunning++ == 0 &&
1163		    (parent->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1164			IEEE80211_DPRINTF(vap,
1165			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1166			    "%s: up parent %s\n", __func__, parent->if_xname);
1167			parent->if_flags |= IFF_UP;
1168			ieee80211_runtask(ic, &ic->ic_parent_task);
1169			return;
1170		}
1171	}
1172	/*
1173	 * If the parent is up and running, then kick the
1174	 * 802.11 state machine as appropriate.
1175	 */
1176	if ((parent->if_drv_flags & IFF_DRV_RUNNING) &&
1177	    vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1178		if (vap->iv_opmode == IEEE80211_M_STA) {
1179#if 0
1180			/* XXX bypasses scan too easily; disable for now */
1181			/*
1182			 * Try to be intelligent about clocking the state
1183			 * machine.  If we're currently in RUN state then
1184			 * we should be able to apply any new state/parameters
1185			 * simply by re-associating.  Otherwise we need to
1186			 * re-scan to select an appropriate ap.
1187			 */
1188			if (vap->iv_state >= IEEE80211_S_RUN)
1189				ieee80211_new_state_locked(vap,
1190				    IEEE80211_S_ASSOC, 1);
1191			else
1192#endif
1193				ieee80211_new_state_locked(vap,
1194				    IEEE80211_S_SCAN, 0);
1195		} else {
1196			/*
1197			 * For monitor+wds mode there's nothing to do but
1198			 * start running.  Otherwise if this is the first
1199			 * vap to be brought up, start a scan which may be
1200			 * preempted if the station is locked to a particular
1201			 * channel.
1202			 */
1203			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1204			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1205			    vap->iv_opmode == IEEE80211_M_WDS)
1206				ieee80211_new_state_locked(vap,
1207				    IEEE80211_S_RUN, -1);
1208			else
1209				ieee80211_new_state_locked(vap,
1210				    IEEE80211_S_SCAN, 0);
1211		}
1212	}
1213}
1214
1215/*
1216 * Start a single vap.
1217 */
1218void
1219ieee80211_init(void *arg)
1220{
1221	struct ieee80211vap *vap = arg;
1222
1223	/*
1224	 * This routine is publicly accessible through the vap's
1225	 * if_init method so guard against calls during detach.
1226	 * ieee80211_vap_detach null's the backpointer before
1227	 * tearing down state to signal any callback should be
1228	 * rejected/ignored.
1229	 */
1230	if (vap != NULL) {
1231		IEEE80211_DPRINTF(vap,
1232		    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1233		    "%s\n", __func__);
1234
1235		IEEE80211_LOCK(vap->iv_ic);
1236		ieee80211_start_locked(vap);
1237		IEEE80211_UNLOCK(vap->iv_ic);
1238	}
1239}
1240
1241/*
1242 * Start all runnable vap's on a device.
1243 */
1244void
1245ieee80211_start_all(struct ieee80211com *ic)
1246{
1247	struct ieee80211vap *vap;
1248
1249	IEEE80211_LOCK(ic);
1250	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1251		struct ifnet *ifp = vap->iv_ifp;
1252		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1253			ieee80211_start_locked(vap);
1254	}
1255	IEEE80211_UNLOCK(ic);
1256}
1257
1258/*
1259 * Stop a vap.  We force it down using the state machine
1260 * then mark it's ifnet not running.  If this is the last
1261 * vap running on the underlying device then we close it
1262 * too to insure it will be properly initialized when the
1263 * next vap is brought up.
1264 */
1265void
1266ieee80211_stop_locked(struct ieee80211vap *vap)
1267{
1268	struct ieee80211com *ic = vap->iv_ic;
1269	struct ifnet *ifp = vap->iv_ifp;
1270	struct ifnet *parent = ic->ic_ifp;
1271
1272	IEEE80211_LOCK_ASSERT(ic);
1273
1274	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1275	    "stop running, %d vaps running\n", ic->ic_nrunning);
1276
1277	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1278	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1279		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1280		if (--ic->ic_nrunning == 0 &&
1281		    (parent->if_drv_flags & IFF_DRV_RUNNING)) {
1282			IEEE80211_DPRINTF(vap,
1283			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1284			    "down parent %s\n", parent->if_xname);
1285			parent->if_flags &= ~IFF_UP;
1286			ieee80211_runtask(ic, &ic->ic_parent_task);
1287		}
1288	}
1289}
1290
1291void
1292ieee80211_stop(struct ieee80211vap *vap)
1293{
1294	struct ieee80211com *ic = vap->iv_ic;
1295
1296	IEEE80211_LOCK(ic);
1297	ieee80211_stop_locked(vap);
1298	IEEE80211_UNLOCK(ic);
1299}
1300
1301/*
1302 * Stop all vap's running on a device.
1303 */
1304void
1305ieee80211_stop_all(struct ieee80211com *ic)
1306{
1307	struct ieee80211vap *vap;
1308
1309	IEEE80211_LOCK(ic);
1310	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1311		struct ifnet *ifp = vap->iv_ifp;
1312		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1313			ieee80211_stop_locked(vap);
1314	}
1315	IEEE80211_UNLOCK(ic);
1316
1317	ieee80211_waitfor_parent(ic);
1318}
1319
1320/*
1321 * Stop all vap's running on a device and arrange
1322 * for those that were running to be resumed.
1323 */
1324void
1325ieee80211_suspend_all(struct ieee80211com *ic)
1326{
1327	struct ieee80211vap *vap;
1328
1329	IEEE80211_LOCK(ic);
1330	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1331		struct ifnet *ifp = vap->iv_ifp;
1332		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
1333			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1334			ieee80211_stop_locked(vap);
1335		}
1336	}
1337	IEEE80211_UNLOCK(ic);
1338
1339	ieee80211_waitfor_parent(ic);
1340}
1341
1342/*
1343 * Start all vap's marked for resume.
1344 */
1345void
1346ieee80211_resume_all(struct ieee80211com *ic)
1347{
1348	struct ieee80211vap *vap;
1349
1350	IEEE80211_LOCK(ic);
1351	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1352		struct ifnet *ifp = vap->iv_ifp;
1353		if (!IFNET_IS_UP_RUNNING(ifp) &&
1354		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1355			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1356			ieee80211_start_locked(vap);
1357		}
1358	}
1359	IEEE80211_UNLOCK(ic);
1360}
1361
1362void
1363ieee80211_beacon_miss(struct ieee80211com *ic)
1364{
1365	IEEE80211_LOCK(ic);
1366	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1367		/* Process in a taskq, the handler may reenter the driver */
1368		ieee80211_runtask(ic, &ic->ic_bmiss_task);
1369	}
1370	IEEE80211_UNLOCK(ic);
1371}
1372
1373static void
1374beacon_miss(void *arg, int npending)
1375{
1376	struct ieee80211com *ic = arg;
1377	struct ieee80211vap *vap;
1378
1379	/* XXX locking */
1380	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1381		/*
1382		 * We only pass events through for sta vap's in RUN state;
1383		 * may be too restrictive but for now this saves all the
1384		 * handlers duplicating these checks.
1385		 */
1386		if (vap->iv_opmode == IEEE80211_M_STA &&
1387		    vap->iv_state == IEEE80211_S_RUN &&
1388		    vap->iv_bmiss != NULL)
1389			vap->iv_bmiss(vap);
1390	}
1391}
1392
1393static void
1394beacon_swmiss(void *arg, int npending)
1395{
1396	struct ieee80211vap *vap = arg;
1397
1398	if (vap->iv_state != IEEE80211_S_RUN)
1399		return;
1400
1401	/* XXX Call multiple times if npending > zero? */
1402	vap->iv_bmiss(vap);
1403}
1404
1405/*
1406 * Software beacon miss handling.  Check if any beacons
1407 * were received in the last period.  If not post a
1408 * beacon miss; otherwise reset the counter.
1409 */
1410void
1411ieee80211_swbmiss(void *arg)
1412{
1413	struct ieee80211vap *vap = arg;
1414	struct ieee80211com *ic = vap->iv_ic;
1415
1416	/* XXX sleep state? */
1417	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1418	    ("wrong state %d", vap->iv_state));
1419
1420	if (ic->ic_flags & IEEE80211_F_SCAN) {
1421		/*
1422		 * If scanning just ignore and reset state.  If we get a
1423		 * bmiss after coming out of scan because we haven't had
1424		 * time to receive a beacon then we should probe the AP
1425		 * before posting a real bmiss (unless iv_bmiss_max has
1426		 * been artifiically lowered).  A cleaner solution might
1427		 * be to disable the timer on scan start/end but to handle
1428		 * case of multiple sta vap's we'd need to disable the
1429		 * timers of all affected vap's.
1430		 */
1431		vap->iv_swbmiss_count = 0;
1432	} else if (vap->iv_swbmiss_count == 0) {
1433		if (vap->iv_bmiss != NULL)
1434			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1435		if (vap->iv_bmiss_count == 0)	/* don't re-arm timer */
1436			return;
1437	} else
1438		vap->iv_swbmiss_count = 0;
1439	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1440		ieee80211_swbmiss, vap);
1441}
1442
1443/*
1444 * Start an 802.11h channel switch.  We record the parameters,
1445 * mark the operation pending, notify each vap through the
1446 * beacon update mechanism so it can update the beacon frame
1447 * contents, and then switch vap's to CSA state to block outbound
1448 * traffic.  Devices that handle CSA directly can use the state
1449 * switch to do the right thing so long as they call
1450 * ieee80211_csa_completeswitch when it's time to complete the
1451 * channel change.  Devices that depend on the net80211 layer can
1452 * use ieee80211_beacon_update to handle the countdown and the
1453 * channel switch.
1454 */
1455void
1456ieee80211_csa_startswitch(struct ieee80211com *ic,
1457	struct ieee80211_channel *c, int mode, int count)
1458{
1459	struct ieee80211vap *vap;
1460
1461	IEEE80211_LOCK_ASSERT(ic);
1462
1463	ic->ic_csa_newchan = c;
1464	ic->ic_csa_count = count;
1465	/* XXX record mode? */
1466	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1467	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1468		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1469		    vap->iv_opmode == IEEE80211_M_IBSS)
1470			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1471		/* switch to CSA state to block outbound traffic */
1472		if (vap->iv_state == IEEE80211_S_RUN)
1473			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1474	}
1475	ieee80211_notify_csa(ic, c, mode, count);
1476}
1477
1478/*
1479 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1480 * We clear state and move all vap's in CSA state to RUN state
1481 * so they can again transmit.
1482 */
1483void
1484ieee80211_csa_completeswitch(struct ieee80211com *ic)
1485{
1486	struct ieee80211vap *vap;
1487
1488	IEEE80211_LOCK_ASSERT(ic);
1489
1490	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1491
1492	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1493	ic->ic_csa_newchan = NULL;
1494	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1495
1496	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1497		if (vap->iv_state == IEEE80211_S_CSA)
1498			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1499}
1500
1501/*
1502 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1503 * We clear state and move all vap's in CAC state to RUN state.
1504 */
1505void
1506ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1507{
1508	struct ieee80211com *ic = vap0->iv_ic;
1509	struct ieee80211vap *vap;
1510
1511	IEEE80211_LOCK(ic);
1512	/*
1513	 * Complete CAC state change for lead vap first; then
1514	 * clock all the other vap's waiting.
1515	 */
1516	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1517	    ("wrong state %d", vap0->iv_state));
1518	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1519
1520	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1521		if (vap->iv_state == IEEE80211_S_CAC)
1522			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1523	IEEE80211_UNLOCK(ic);
1524}
1525
1526/*
1527 * Force all vap's other than the specified vap to the INIT state
1528 * and mark them as waiting for a scan to complete.  These vaps
1529 * will be brought up when the scan completes and the scanning vap
1530 * reaches RUN state by wakeupwaiting.
1531 */
1532static void
1533markwaiting(struct ieee80211vap *vap0)
1534{
1535	struct ieee80211com *ic = vap0->iv_ic;
1536	struct ieee80211vap *vap;
1537
1538	IEEE80211_LOCK_ASSERT(ic);
1539
1540	/*
1541	 * A vap list entry can not disappear since we are running on the
1542	 * taskqueue and a vap destroy will queue and drain another state
1543	 * change task.
1544	 */
1545	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1546		if (vap == vap0)
1547			continue;
1548		if (vap->iv_state != IEEE80211_S_INIT) {
1549			/* NB: iv_newstate may drop the lock */
1550			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1551			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1552		}
1553	}
1554}
1555
1556/*
1557 * Wakeup all vap's waiting for a scan to complete.  This is the
1558 * companion to markwaiting (above) and is used to coordinate
1559 * multiple vaps scanning.
1560 * This is called from the state taskqueue.
1561 */
1562static void
1563wakeupwaiting(struct ieee80211vap *vap0)
1564{
1565	struct ieee80211com *ic = vap0->iv_ic;
1566	struct ieee80211vap *vap;
1567
1568	IEEE80211_LOCK_ASSERT(ic);
1569
1570	/*
1571	 * A vap list entry can not disappear since we are running on the
1572	 * taskqueue and a vap destroy will queue and drain another state
1573	 * change task.
1574	 */
1575	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1576		if (vap == vap0)
1577			continue;
1578		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1579			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1580			/* NB: sta's cannot go INIT->RUN */
1581			/* NB: iv_newstate may drop the lock */
1582			vap->iv_newstate(vap,
1583			    vap->iv_opmode == IEEE80211_M_STA ?
1584			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1585		}
1586	}
1587}
1588
1589/*
1590 * Handle post state change work common to all operating modes.
1591 */
1592static void
1593ieee80211_newstate_cb(void *xvap, int npending)
1594{
1595	struct ieee80211vap *vap = xvap;
1596	struct ieee80211com *ic = vap->iv_ic;
1597	enum ieee80211_state nstate, ostate;
1598	int arg, rc;
1599
1600	IEEE80211_LOCK(ic);
1601	nstate = vap->iv_nstate;
1602	arg = vap->iv_nstate_arg;
1603
1604	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1605		/*
1606		 * We have been requested to drop back to the INIT before
1607		 * proceeding to the new state.
1608		 */
1609		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1610		    "%s: %s -> %s arg %d\n", __func__,
1611		    ieee80211_state_name[vap->iv_state],
1612		    ieee80211_state_name[IEEE80211_S_INIT], arg);
1613		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1614		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1615	}
1616
1617	ostate = vap->iv_state;
1618	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1619		/*
1620		 * SCAN was forced; e.g. on beacon miss.  Force other running
1621		 * vap's to INIT state and mark them as waiting for the scan to
1622		 * complete.  This insures they don't interfere with our
1623		 * scanning.  Since we are single threaded the vaps can not
1624		 * transition again while we are executing.
1625		 *
1626		 * XXX not always right, assumes ap follows sta
1627		 */
1628		markwaiting(vap);
1629	}
1630	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1631	    "%s: %s -> %s arg %d\n", __func__,
1632	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1633
1634	rc = vap->iv_newstate(vap, nstate, arg);
1635	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1636	if (rc != 0) {
1637		/* State transition failed */
1638		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1639		KASSERT(nstate != IEEE80211_S_INIT,
1640		    ("INIT state change failed"));
1641		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1642		    "%s: %s returned error %d\n", __func__,
1643		    ieee80211_state_name[nstate], rc);
1644		goto done;
1645	}
1646
1647	/* No actual transition, skip post processing */
1648	if (ostate == nstate)
1649		goto done;
1650
1651	if (nstate == IEEE80211_S_RUN) {
1652		/*
1653		 * OACTIVE may be set on the vap if the upper layer
1654		 * tried to transmit (e.g. IPv6 NDP) before we reach
1655		 * RUN state.  Clear it and restart xmit.
1656		 *
1657		 * Note this can also happen as a result of SLEEP->RUN
1658		 * (i.e. coming out of power save mode).
1659		 */
1660		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1661		if_start(vap->iv_ifp);
1662
1663		/* bring up any vaps waiting on us */
1664		wakeupwaiting(vap);
1665	} else if (nstate == IEEE80211_S_INIT) {
1666		/*
1667		 * Flush the scan cache if we did the last scan (XXX?)
1668		 * and flush any frames on send queues from this vap.
1669		 * Note the mgt q is used only for legacy drivers and
1670		 * will go away shortly.
1671		 */
1672		ieee80211_scan_flush(vap);
1673
1674		/* XXX NB: cast for altq */
1675		ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1676	}
1677done:
1678	IEEE80211_UNLOCK(ic);
1679}
1680
1681/*
1682 * Public interface for initiating a state machine change.
1683 * This routine single-threads the request and coordinates
1684 * the scheduling of multiple vaps for the purpose of selecting
1685 * an operating channel.  Specifically the following scenarios
1686 * are handled:
1687 * o only one vap can be selecting a channel so on transition to
1688 *   SCAN state if another vap is already scanning then
1689 *   mark the caller for later processing and return without
1690 *   doing anything (XXX? expectations by caller of synchronous operation)
1691 * o only one vap can be doing CAC of a channel so on transition to
1692 *   CAC state if another vap is already scanning for radar then
1693 *   mark the caller for later processing and return without
1694 *   doing anything (XXX? expectations by caller of synchronous operation)
1695 * o if another vap is already running when a request is made
1696 *   to SCAN then an operating channel has been chosen; bypass
1697 *   the scan and just join the channel
1698 *
1699 * Note that the state change call is done through the iv_newstate
1700 * method pointer so any driver routine gets invoked.  The driver
1701 * will normally call back into operating mode-specific
1702 * ieee80211_newstate routines (below) unless it needs to completely
1703 * bypass the state machine (e.g. because the firmware has it's
1704 * own idea how things should work).  Bypassing the net80211 layer
1705 * is usually a mistake and indicates lack of proper integration
1706 * with the net80211 layer.
1707 */
1708static int
1709ieee80211_new_state_locked(struct ieee80211vap *vap,
1710	enum ieee80211_state nstate, int arg)
1711{
1712	struct ieee80211com *ic = vap->iv_ic;
1713	struct ieee80211vap *vp;
1714	enum ieee80211_state ostate;
1715	int nrunning, nscanning;
1716
1717	IEEE80211_LOCK_ASSERT(ic);
1718
1719	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1720		if (vap->iv_nstate == IEEE80211_S_INIT) {
1721			/*
1722			 * XXX The vap is being stopped, do no allow any other
1723			 * state changes until this is completed.
1724			 */
1725			return -1;
1726		} else if (vap->iv_state != vap->iv_nstate) {
1727#if 0
1728			/* Warn if the previous state hasn't completed. */
1729			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1730			    "%s: pending %s -> %s transition lost\n", __func__,
1731			    ieee80211_state_name[vap->iv_state],
1732			    ieee80211_state_name[vap->iv_nstate]);
1733#else
1734			/* XXX temporarily enable to identify issues */
1735			if_printf(vap->iv_ifp,
1736			    "%s: pending %s -> %s transition lost\n",
1737			    __func__, ieee80211_state_name[vap->iv_state],
1738			    ieee80211_state_name[vap->iv_nstate]);
1739#endif
1740		}
1741	}
1742
1743	nrunning = nscanning = 0;
1744	/* XXX can track this state instead of calculating */
1745	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1746		if (vp != vap) {
1747			if (vp->iv_state >= IEEE80211_S_RUN)
1748				nrunning++;
1749			/* XXX doesn't handle bg scan */
1750			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1751			else if (vp->iv_state > IEEE80211_S_INIT)
1752				nscanning++;
1753		}
1754	}
1755	ostate = vap->iv_state;
1756	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1757	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1758	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1759	    nrunning, nscanning);
1760	switch (nstate) {
1761	case IEEE80211_S_SCAN:
1762		if (ostate == IEEE80211_S_INIT) {
1763			/*
1764			 * INIT -> SCAN happens on initial bringup.
1765			 */
1766			KASSERT(!(nscanning && nrunning),
1767			    ("%d scanning and %d running", nscanning, nrunning));
1768			if (nscanning) {
1769				/*
1770				 * Someone is scanning, defer our state
1771				 * change until the work has completed.
1772				 */
1773				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1774				    "%s: defer %s -> %s\n",
1775				    __func__, ieee80211_state_name[ostate],
1776				    ieee80211_state_name[nstate]);
1777				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1778				return 0;
1779			}
1780			if (nrunning) {
1781				/*
1782				 * Someone is operating; just join the channel
1783				 * they have chosen.
1784				 */
1785				/* XXX kill arg? */
1786				/* XXX check each opmode, adhoc? */
1787				if (vap->iv_opmode == IEEE80211_M_STA)
1788					nstate = IEEE80211_S_SCAN;
1789				else
1790					nstate = IEEE80211_S_RUN;
1791#ifdef IEEE80211_DEBUG
1792				if (nstate != IEEE80211_S_SCAN) {
1793					IEEE80211_DPRINTF(vap,
1794					    IEEE80211_MSG_STATE,
1795					    "%s: override, now %s -> %s\n",
1796					    __func__,
1797					    ieee80211_state_name[ostate],
1798					    ieee80211_state_name[nstate]);
1799				}
1800#endif
1801			}
1802		}
1803		break;
1804	case IEEE80211_S_RUN:
1805		if (vap->iv_opmode == IEEE80211_M_WDS &&
1806		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1807		    nscanning) {
1808			/*
1809			 * Legacy WDS with someone else scanning; don't
1810			 * go online until that completes as we should
1811			 * follow the other vap to the channel they choose.
1812			 */
1813			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1814			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
1815			     ieee80211_state_name[ostate],
1816			     ieee80211_state_name[nstate]);
1817			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1818			return 0;
1819		}
1820		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1821		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1822		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1823		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1824			/*
1825			 * This is a DFS channel, transition to CAC state
1826			 * instead of RUN.  This allows us to initiate
1827			 * Channel Availability Check (CAC) as specified
1828			 * by 11h/DFS.
1829			 */
1830			nstate = IEEE80211_S_CAC;
1831			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1832			     "%s: override %s -> %s (DFS)\n", __func__,
1833			     ieee80211_state_name[ostate],
1834			     ieee80211_state_name[nstate]);
1835		}
1836		break;
1837	case IEEE80211_S_INIT:
1838		/* cancel any scan in progress */
1839		ieee80211_cancel_scan(vap);
1840		if (ostate == IEEE80211_S_INIT ) {
1841			/* XXX don't believe this */
1842			/* INIT -> INIT. nothing to do */
1843			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1844		}
1845		/* fall thru... */
1846	default:
1847		break;
1848	}
1849	/* defer the state change to a thread */
1850	vap->iv_nstate = nstate;
1851	vap->iv_nstate_arg = arg;
1852	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1853	ieee80211_runtask(ic, &vap->iv_nstate_task);
1854	return EINPROGRESS;
1855}
1856
1857int
1858ieee80211_new_state(struct ieee80211vap *vap,
1859	enum ieee80211_state nstate, int arg)
1860{
1861	struct ieee80211com *ic = vap->iv_ic;
1862	int rc;
1863
1864	IEEE80211_LOCK(ic);
1865	rc = ieee80211_new_state_locked(vap, nstate, arg);
1866	IEEE80211_UNLOCK(ic);
1867	return rc;
1868}
1869