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