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