ieee80211_proto.c revision 193348
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 193348 2009-06-02 20:51:59Z 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	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1224	    "%s\n", __func__);
1225
1226	IEEE80211_LOCK(vap->iv_ic);
1227	ieee80211_start_locked(vap);
1228	IEEE80211_UNLOCK(vap->iv_ic);
1229}
1230
1231/*
1232 * Start all runnable vap's on a device.
1233 */
1234void
1235ieee80211_start_all(struct ieee80211com *ic)
1236{
1237	struct ieee80211vap *vap;
1238
1239	IEEE80211_LOCK(ic);
1240	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1241		struct ifnet *ifp = vap->iv_ifp;
1242		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1243			ieee80211_start_locked(vap);
1244	}
1245	IEEE80211_UNLOCK(ic);
1246}
1247
1248/*
1249 * Stop a vap.  We force it down using the state machine
1250 * then mark it's ifnet not running.  If this is the last
1251 * vap running on the underlying device then we close it
1252 * too to insure it will be properly initialized when the
1253 * next vap is brought up.
1254 */
1255void
1256ieee80211_stop_locked(struct ieee80211vap *vap)
1257{
1258	struct ieee80211com *ic = vap->iv_ic;
1259	struct ifnet *ifp = vap->iv_ifp;
1260	struct ifnet *parent = ic->ic_ifp;
1261
1262	IEEE80211_LOCK_ASSERT(ic);
1263
1264	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1265	    "stop running, %d vaps running\n", ic->ic_nrunning);
1266
1267	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1268	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1269		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
1270		if (--ic->ic_nrunning == 0 &&
1271		    (parent->if_drv_flags & IFF_DRV_RUNNING)) {
1272			IEEE80211_DPRINTF(vap,
1273			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1274			    "down parent %s\n", parent->if_xname);
1275			parent->if_flags &= ~IFF_UP;
1276			ieee80211_runtask(ic, &ic->ic_parent_task);
1277		}
1278	}
1279}
1280
1281void
1282ieee80211_stop(struct ieee80211vap *vap)
1283{
1284	struct ieee80211com *ic = vap->iv_ic;
1285
1286	IEEE80211_LOCK(ic);
1287	ieee80211_stop_locked(vap);
1288	IEEE80211_UNLOCK(ic);
1289}
1290
1291/*
1292 * Stop all vap's running on a device.
1293 */
1294void
1295ieee80211_stop_all(struct ieee80211com *ic)
1296{
1297	struct ieee80211vap *vap;
1298
1299	IEEE80211_LOCK(ic);
1300	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1301		struct ifnet *ifp = vap->iv_ifp;
1302		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
1303			ieee80211_stop_locked(vap);
1304	}
1305	IEEE80211_UNLOCK(ic);
1306
1307	ieee80211_waitfor_parent(ic);
1308}
1309
1310/*
1311 * Stop all vap's running on a device and arrange
1312 * for those that were running to be resumed.
1313 */
1314void
1315ieee80211_suspend_all(struct ieee80211com *ic)
1316{
1317	struct ieee80211vap *vap;
1318
1319	IEEE80211_LOCK(ic);
1320	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1321		struct ifnet *ifp = vap->iv_ifp;
1322		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
1323			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1324			ieee80211_stop_locked(vap);
1325		}
1326	}
1327	IEEE80211_UNLOCK(ic);
1328
1329	ieee80211_waitfor_parent(ic);
1330}
1331
1332/*
1333 * Start all vap's marked for resume.
1334 */
1335void
1336ieee80211_resume_all(struct ieee80211com *ic)
1337{
1338	struct ieee80211vap *vap;
1339
1340	IEEE80211_LOCK(ic);
1341	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1342		struct ifnet *ifp = vap->iv_ifp;
1343		if (!IFNET_IS_UP_RUNNING(ifp) &&
1344		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1345			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1346			ieee80211_start_locked(vap);
1347		}
1348	}
1349	IEEE80211_UNLOCK(ic);
1350}
1351
1352void
1353ieee80211_beacon_miss(struct ieee80211com *ic)
1354{
1355	IEEE80211_LOCK(ic);
1356	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1357		/* Process in a taskq, the handler may reenter the driver */
1358		ieee80211_runtask(ic, &ic->ic_bmiss_task);
1359	}
1360	IEEE80211_UNLOCK(ic);
1361}
1362
1363static void
1364beacon_miss(void *arg, int npending)
1365{
1366	struct ieee80211com *ic = arg;
1367	struct ieee80211vap *vap;
1368
1369	/* XXX locking */
1370	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1371		/*
1372		 * We only pass events through for sta vap's in RUN state;
1373		 * may be too restrictive but for now this saves all the
1374		 * handlers duplicating these checks.
1375		 */
1376		if (vap->iv_opmode == IEEE80211_M_STA &&
1377		    vap->iv_state == IEEE80211_S_RUN &&
1378		    vap->iv_bmiss != NULL)
1379			vap->iv_bmiss(vap);
1380	}
1381}
1382
1383static void
1384beacon_swmiss(void *arg, int npending)
1385{
1386	struct ieee80211vap *vap = arg;
1387
1388	if (vap->iv_state != IEEE80211_S_RUN)
1389		return;
1390
1391	/* XXX Call multiple times if npending > zero? */
1392	vap->iv_bmiss(vap);
1393}
1394
1395/*
1396 * Software beacon miss handling.  Check if any beacons
1397 * were received in the last period.  If not post a
1398 * beacon miss; otherwise reset the counter.
1399 */
1400void
1401ieee80211_swbmiss(void *arg)
1402{
1403	struct ieee80211vap *vap = arg;
1404	struct ieee80211com *ic = vap->iv_ic;
1405
1406	/* XXX sleep state? */
1407	KASSERT(vap->iv_state == IEEE80211_S_RUN,
1408	    ("wrong state %d", vap->iv_state));
1409
1410	if (ic->ic_flags & IEEE80211_F_SCAN) {
1411		/*
1412		 * If scanning just ignore and reset state.  If we get a
1413		 * bmiss after coming out of scan because we haven't had
1414		 * time to receive a beacon then we should probe the AP
1415		 * before posting a real bmiss (unless iv_bmiss_max has
1416		 * been artifiically lowered).  A cleaner solution might
1417		 * be to disable the timer on scan start/end but to handle
1418		 * case of multiple sta vap's we'd need to disable the
1419		 * timers of all affected vap's.
1420		 */
1421		vap->iv_swbmiss_count = 0;
1422	} else if (vap->iv_swbmiss_count == 0) {
1423		if (vap->iv_bmiss != NULL)
1424			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1425		if (vap->iv_bmiss_count == 0)	/* don't re-arm timer */
1426			return;
1427	} else
1428		vap->iv_swbmiss_count = 0;
1429	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1430		ieee80211_swbmiss, vap);
1431}
1432
1433/*
1434 * Start an 802.11h channel switch.  We record the parameters,
1435 * mark the operation pending, notify each vap through the
1436 * beacon update mechanism so it can update the beacon frame
1437 * contents, and then switch vap's to CSA state to block outbound
1438 * traffic.  Devices that handle CSA directly can use the state
1439 * switch to do the right thing so long as they call
1440 * ieee80211_csa_completeswitch when it's time to complete the
1441 * channel change.  Devices that depend on the net80211 layer can
1442 * use ieee80211_beacon_update to handle the countdown and the
1443 * channel switch.
1444 */
1445void
1446ieee80211_csa_startswitch(struct ieee80211com *ic,
1447	struct ieee80211_channel *c, int mode, int count)
1448{
1449	struct ieee80211vap *vap;
1450
1451	IEEE80211_LOCK_ASSERT(ic);
1452
1453	ic->ic_csa_newchan = c;
1454	ic->ic_csa_count = count;
1455	/* XXX record mode? */
1456	ic->ic_flags |= IEEE80211_F_CSAPENDING;
1457	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1458		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1459		    vap->iv_opmode == IEEE80211_M_IBSS)
1460			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1461		/* switch to CSA state to block outbound traffic */
1462		if (vap->iv_state == IEEE80211_S_RUN)
1463			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1464	}
1465	ieee80211_notify_csa(ic, c, mode, count);
1466}
1467
1468/*
1469 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1470 * We clear state and move all vap's in CSA state to RUN state
1471 * so they can again transmit.
1472 */
1473void
1474ieee80211_csa_completeswitch(struct ieee80211com *ic)
1475{
1476	struct ieee80211vap *vap;
1477
1478	IEEE80211_LOCK_ASSERT(ic);
1479
1480	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1481
1482	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1483	ic->ic_csa_newchan = NULL;
1484	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1485
1486	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1487		if (vap->iv_state == IEEE80211_S_CSA)
1488			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1489}
1490
1491/*
1492 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1493 * We clear state and move all vap's in CAC state to RUN state.
1494 */
1495void
1496ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1497{
1498	struct ieee80211com *ic = vap0->iv_ic;
1499	struct ieee80211vap *vap;
1500
1501	IEEE80211_LOCK(ic);
1502	/*
1503	 * Complete CAC state change for lead vap first; then
1504	 * clock all the other vap's waiting.
1505	 */
1506	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1507	    ("wrong state %d", vap0->iv_state));
1508	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1509
1510	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1511		if (vap->iv_state == IEEE80211_S_CAC)
1512			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1513	IEEE80211_UNLOCK(ic);
1514}
1515
1516/*
1517 * Force all vap's other than the specified vap to the INIT state
1518 * and mark them as waiting for a scan to complete.  These vaps
1519 * will be brought up when the scan completes and the scanning vap
1520 * reaches RUN state by wakeupwaiting.
1521 */
1522static void
1523markwaiting(struct ieee80211vap *vap0)
1524{
1525	struct ieee80211com *ic = vap0->iv_ic;
1526	struct ieee80211vap *vap;
1527
1528	IEEE80211_LOCK_ASSERT(ic);
1529
1530	/*
1531	 * A vap list entry can not disappear since we are running on the
1532	 * taskqueue and a vap destroy will queue and drain another state
1533	 * change task.
1534	 */
1535	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1536		if (vap == vap0)
1537			continue;
1538		if (vap->iv_state != IEEE80211_S_INIT) {
1539			/* NB: iv_newstate may drop the lock */
1540			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1541			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1542		}
1543	}
1544}
1545
1546/*
1547 * Wakeup all vap's waiting for a scan to complete.  This is the
1548 * companion to markwaiting (above) and is used to coordinate
1549 * multiple vaps scanning.
1550 * This is called from the state taskqueue.
1551 */
1552static void
1553wakeupwaiting(struct ieee80211vap *vap0)
1554{
1555	struct ieee80211com *ic = vap0->iv_ic;
1556	struct ieee80211vap *vap;
1557
1558	IEEE80211_LOCK_ASSERT(ic);
1559
1560	/*
1561	 * A vap list entry can not disappear since we are running on the
1562	 * taskqueue and a vap destroy will queue and drain another state
1563	 * change task.
1564	 */
1565	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1566		if (vap == vap0)
1567			continue;
1568		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1569			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1570			/* NB: sta's cannot go INIT->RUN */
1571			/* NB: iv_newstate may drop the lock */
1572			vap->iv_newstate(vap,
1573			    vap->iv_opmode == IEEE80211_M_STA ?
1574			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1575		}
1576	}
1577}
1578
1579/*
1580 * Handle post state change work common to all operating modes.
1581 */
1582static void
1583ieee80211_newstate_cb(void *xvap, int npending)
1584{
1585	struct ieee80211vap *vap = xvap;
1586	struct ieee80211com *ic = vap->iv_ic;
1587	enum ieee80211_state nstate, ostate;
1588	int arg, rc;
1589
1590	IEEE80211_LOCK(ic);
1591	nstate = vap->iv_nstate;
1592	arg = vap->iv_nstate_arg;
1593
1594	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1595		/*
1596		 * We have been requested to drop back to the INIT before
1597		 * proceeding to the new state.
1598		 */
1599		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1600		    "%s: %s -> %s arg %d\n", __func__,
1601		    ieee80211_state_name[vap->iv_state],
1602		    ieee80211_state_name[IEEE80211_S_INIT], arg);
1603		vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1604		vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1605	}
1606
1607	ostate = vap->iv_state;
1608	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1609		/*
1610		 * SCAN was forced; e.g. on beacon miss.  Force other running
1611		 * vap's to INIT state and mark them as waiting for the scan to
1612		 * complete.  This insures they don't interfere with our
1613		 * scanning.  Since we are single threaded the vaps can not
1614		 * transition again while we are executing.
1615		 *
1616		 * XXX not always right, assumes ap follows sta
1617		 */
1618		markwaiting(vap);
1619	}
1620	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1621	    "%s: %s -> %s arg %d\n", __func__,
1622	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1623
1624	rc = vap->iv_newstate(vap, nstate, arg);
1625	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1626	if (rc != 0) {
1627		/* State transition failed */
1628		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1629		KASSERT(nstate != IEEE80211_S_INIT,
1630		    ("INIT state change failed"));
1631		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1632		    "%s: %s returned error %d\n", __func__,
1633		    ieee80211_state_name[nstate], rc);
1634		goto done;
1635	}
1636
1637	/* No actual transition, skip post processing */
1638	if (ostate == nstate)
1639		goto done;
1640
1641	if (nstate == IEEE80211_S_RUN) {
1642		/*
1643		 * OACTIVE may be set on the vap if the upper layer
1644		 * tried to transmit (e.g. IPv6 NDP) before we reach
1645		 * RUN state.  Clear it and restart xmit.
1646		 *
1647		 * Note this can also happen as a result of SLEEP->RUN
1648		 * (i.e. coming out of power save mode).
1649		 */
1650		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1651		if_start(vap->iv_ifp);
1652
1653		/* bring up any vaps waiting on us */
1654		wakeupwaiting(vap);
1655	} else if (nstate == IEEE80211_S_INIT) {
1656		/*
1657		 * Flush the scan cache if we did the last scan (XXX?)
1658		 * and flush any frames on send queues from this vap.
1659		 * Note the mgt q is used only for legacy drivers and
1660		 * will go away shortly.
1661		 */
1662		ieee80211_scan_flush(vap);
1663
1664		/* XXX NB: cast for altq */
1665		ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1666	}
1667done:
1668	IEEE80211_UNLOCK(ic);
1669}
1670
1671/*
1672 * Public interface for initiating a state machine change.
1673 * This routine single-threads the request and coordinates
1674 * the scheduling of multiple vaps for the purpose of selecting
1675 * an operating channel.  Specifically the following scenarios
1676 * are handled:
1677 * o only one vap can be selecting a channel so on transition to
1678 *   SCAN state if another vap is already scanning then
1679 *   mark the caller for later processing and return without
1680 *   doing anything (XXX? expectations by caller of synchronous operation)
1681 * o only one vap can be doing CAC of a channel so on transition to
1682 *   CAC state if another vap is already scanning for radar then
1683 *   mark the caller for later processing and return without
1684 *   doing anything (XXX? expectations by caller of synchronous operation)
1685 * o if another vap is already running when a request is made
1686 *   to SCAN then an operating channel has been chosen; bypass
1687 *   the scan and just join the channel
1688 *
1689 * Note that the state change call is done through the iv_newstate
1690 * method pointer so any driver routine gets invoked.  The driver
1691 * will normally call back into operating mode-specific
1692 * ieee80211_newstate routines (below) unless it needs to completely
1693 * bypass the state machine (e.g. because the firmware has it's
1694 * own idea how things should work).  Bypassing the net80211 layer
1695 * is usually a mistake and indicates lack of proper integration
1696 * with the net80211 layer.
1697 */
1698static int
1699ieee80211_new_state_locked(struct ieee80211vap *vap,
1700	enum ieee80211_state nstate, int arg)
1701{
1702	struct ieee80211com *ic = vap->iv_ic;
1703	struct ieee80211vap *vp;
1704	enum ieee80211_state ostate;
1705	int nrunning, nscanning;
1706
1707	IEEE80211_LOCK_ASSERT(ic);
1708
1709	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1710		if (vap->iv_nstate == IEEE80211_S_INIT) {
1711			/*
1712			 * XXX The vap is being stopped, do no allow any other
1713			 * state changes until this is completed.
1714			 */
1715			return -1;
1716		} else if (vap->iv_state != vap->iv_nstate) {
1717#if 0
1718			/* Warn if the previous state hasn't completed. */
1719			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1720			    "%s: pending %s -> %s transition lost\n", __func__,
1721			    ieee80211_state_name[vap->iv_state],
1722			    ieee80211_state_name[vap->iv_nstate]);
1723#else
1724			/* XXX temporarily enable to identify issues */
1725			if_printf(vap->iv_ifp,
1726			    "%s: pending %s -> %s transition lost\n",
1727			    __func__, ieee80211_state_name[vap->iv_state],
1728			    ieee80211_state_name[vap->iv_nstate]);
1729#endif
1730		}
1731	}
1732
1733	nrunning = nscanning = 0;
1734	/* XXX can track this state instead of calculating */
1735	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1736		if (vp != vap) {
1737			if (vp->iv_state >= IEEE80211_S_RUN)
1738				nrunning++;
1739			/* XXX doesn't handle bg scan */
1740			/* NB: CAC+AUTH+ASSOC treated like SCAN */
1741			else if (vp->iv_state > IEEE80211_S_INIT)
1742				nscanning++;
1743		}
1744	}
1745	ostate = vap->iv_state;
1746	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1747	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1748	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1749	    nrunning, nscanning);
1750	switch (nstate) {
1751	case IEEE80211_S_SCAN:
1752		if (ostate == IEEE80211_S_INIT) {
1753			/*
1754			 * INIT -> SCAN happens on initial bringup.
1755			 */
1756			KASSERT(!(nscanning && nrunning),
1757			    ("%d scanning and %d running", nscanning, nrunning));
1758			if (nscanning) {
1759				/*
1760				 * Someone is scanning, defer our state
1761				 * change until the work has completed.
1762				 */
1763				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1764				    "%s: defer %s -> %s\n",
1765				    __func__, ieee80211_state_name[ostate],
1766				    ieee80211_state_name[nstate]);
1767				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1768				return 0;
1769			}
1770			if (nrunning) {
1771				/*
1772				 * Someone is operating; just join the channel
1773				 * they have chosen.
1774				 */
1775				/* XXX kill arg? */
1776				/* XXX check each opmode, adhoc? */
1777				if (vap->iv_opmode == IEEE80211_M_STA)
1778					nstate = IEEE80211_S_SCAN;
1779				else
1780					nstate = IEEE80211_S_RUN;
1781#ifdef IEEE80211_DEBUG
1782				if (nstate != IEEE80211_S_SCAN) {
1783					IEEE80211_DPRINTF(vap,
1784					    IEEE80211_MSG_STATE,
1785					    "%s: override, now %s -> %s\n",
1786					    __func__,
1787					    ieee80211_state_name[ostate],
1788					    ieee80211_state_name[nstate]);
1789				}
1790#endif
1791			}
1792		}
1793		break;
1794	case IEEE80211_S_RUN:
1795		if (vap->iv_opmode == IEEE80211_M_WDS &&
1796		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1797		    nscanning) {
1798			/*
1799			 * Legacy WDS with someone else scanning; don't
1800			 * go online until that completes as we should
1801			 * follow the other vap to the channel they choose.
1802			 */
1803			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1804			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
1805			     ieee80211_state_name[ostate],
1806			     ieee80211_state_name[nstate]);
1807			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1808			return 0;
1809		}
1810		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1811		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1812		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1813		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1814			/*
1815			 * This is a DFS channel, transition to CAC state
1816			 * instead of RUN.  This allows us to initiate
1817			 * Channel Availability Check (CAC) as specified
1818			 * by 11h/DFS.
1819			 */
1820			nstate = IEEE80211_S_CAC;
1821			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1822			     "%s: override %s -> %s (DFS)\n", __func__,
1823			     ieee80211_state_name[ostate],
1824			     ieee80211_state_name[nstate]);
1825		}
1826		break;
1827	case IEEE80211_S_INIT:
1828		/* cancel any scan in progress */
1829		ieee80211_cancel_scan(vap);
1830		if (ostate == IEEE80211_S_INIT ) {
1831			/* XXX don't believe this */
1832			/* INIT -> INIT. nothing to do */
1833			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1834		}
1835		/* fall thru... */
1836	default:
1837		break;
1838	}
1839	/* defer the state change to a thread */
1840	vap->iv_nstate = nstate;
1841	vap->iv_nstate_arg = arg;
1842	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1843	ieee80211_runtask(ic, &vap->iv_nstate_task);
1844	return EINPROGRESS;
1845}
1846
1847int
1848ieee80211_new_state(struct ieee80211vap *vap,
1849	enum ieee80211_state nstate, int arg)
1850{
1851	struct ieee80211com *ic = vap->iv_ic;
1852	int rc;
1853
1854	IEEE80211_LOCK(ic);
1855	rc = ieee80211_new_state_locked(vap, nstate, arg);
1856	IEEE80211_UNLOCK(ic);
1857	return rc;
1858}
1859