ieee80211_proto.c revision 165569
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3139530Ssam * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116904Ssam * 3. The name of the author may not be used to endorse or promote products
15116904Ssam *    derived from this software without specific prior written permission.
16116742Ssam *
17116742Ssam * Alternatively, this software may be distributed under the terms of the
18116742Ssam * GNU General Public License ("GPL") version 2 as published by the Free
19116742Ssam * Software Foundation.
20116742Ssam *
21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31116742Ssam */
32116742Ssam
33116742Ssam#include <sys/cdefs.h>
34116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_proto.c 165569 2006-12-27 18:46:18Z sam $");
35116742Ssam
36116742Ssam/*
37116742Ssam * IEEE 802.11 protocol support.
38116742Ssam */
39116742Ssam
40116742Ssam#include "opt_inet.h"
41116742Ssam
42116742Ssam#include <sys/param.h>
43138568Ssam#include <sys/kernel.h>
44116742Ssam#include <sys/systm.h>
45138568Ssam
46116742Ssam#include <sys/socket.h>
47116742Ssam
48116742Ssam#include <net/if.h>
49116742Ssam#include <net/if_media.h>
50138568Ssam#include <net/ethernet.h>		/* XXX for ether_sprintf */
51116742Ssam
52116742Ssam#include <net80211/ieee80211_var.h>
53116742Ssam
54138568Ssam/* XXX tunables */
55138568Ssam#define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
56138568Ssam#define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
57116742Ssam
58116742Ssam#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
59116742Ssam
60116742Ssamconst char *ieee80211_mgt_subtype_name[] = {
61116742Ssam	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
62116742Ssam	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
63116742Ssam	"beacon",	"atim",		"disassoc",	"auth",
64116742Ssam	"deauth",	"reserved#13",	"reserved#14",	"reserved#15"
65116742Ssam};
66138568Ssamconst char *ieee80211_ctl_subtype_name[] = {
67138568Ssam	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
68138568Ssam	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
69138568Ssam	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
70138568Ssam	"cts",		"ack",		"cf_end",	"cf_end_ack"
71138568Ssam};
72117811Ssamconst char *ieee80211_state_name[IEEE80211_S_MAX] = {
73117811Ssam	"INIT",		/* IEEE80211_S_INIT */
74117811Ssam	"SCAN",		/* IEEE80211_S_SCAN */
75117811Ssam	"AUTH",		/* IEEE80211_S_AUTH */
76117811Ssam	"ASSOC",	/* IEEE80211_S_ASSOC */
77117811Ssam	"RUN"		/* IEEE80211_S_RUN */
78117811Ssam};
79138568Ssamconst char *ieee80211_wme_acnames[] = {
80138568Ssam	"WME_AC_BE",
81138568Ssam	"WME_AC_BK",
82138568Ssam	"WME_AC_VI",
83138568Ssam	"WME_AC_VO",
84138568Ssam	"WME_UPSD",
85138568Ssam};
86116742Ssam
87117811Ssamstatic int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
88117811Ssam
89116742Ssamvoid
90138568Ssamieee80211_proto_attach(struct ieee80211com *ic)
91116742Ssam{
92138568Ssam	struct ifnet *ifp = ic->ic_ifp;
93116742Ssam
94138568Ssam	/* XXX room for crypto  */
95138568Ssam	ifp->if_hdrlen = sizeof(struct ieee80211_qosframe_addr4);
96116742Ssam
97116742Ssam	ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
98148291Ssam	ic->ic_fragthreshold = IEEE80211_FRAG_DEFAULT;
99148290Ssam	ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
100153349Ssam	ic->ic_bmiss_max = IEEE80211_BMISS_MAX;
101154736Ssam	callout_init(&ic->ic_swbmiss, CALLOUT_MPSAFE);
102153346Ssam	ic->ic_mcast_rate = IEEE80211_MCAST_RATE_DEFAULT;
103127648Ssam	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
104138568Ssam	ic->ic_roaming = IEEE80211_ROAMING_AUTO;
105116742Ssam
106138568Ssam	ic->ic_wme.wme_hipri_switch_hysteresis =
107138568Ssam		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
108138568Ssam
109121816Sbrooks	mtx_init(&ic->ic_mgtq.ifq_mtx, ifp->if_xname, "mgmt send q", MTX_DEF);
110116742Ssam
111117811Ssam	/* protocol state change handler */
112117811Ssam	ic->ic_newstate = ieee80211_newstate;
113117811Ssam
114116742Ssam	/* initialize management frame handlers */
115116742Ssam	ic->ic_recv_mgmt = ieee80211_recv_mgmt;
116116742Ssam	ic->ic_send_mgmt = ieee80211_send_mgmt;
117160690Ssam	ic->ic_raw_xmit = ieee80211_raw_xmit;
118116742Ssam}
119116742Ssam
120116742Ssamvoid
121138568Ssamieee80211_proto_detach(struct ieee80211com *ic)
122116742Ssam{
123116742Ssam
124138568Ssam	/*
125138568Ssam	 * This should not be needed as we detach when reseting
126138568Ssam	 * the state but be conservative here since the
127138568Ssam	 * authenticator may do things like spawn kernel threads.
128138568Ssam	 */
129138568Ssam	if (ic->ic_auth->ia_detach)
130138568Ssam		ic->ic_auth->ia_detach(ic);
131138568Ssam
132116742Ssam	IF_DRAIN(&ic->ic_mgtq);
133116742Ssam	mtx_destroy(&ic->ic_mgtq.ifq_mtx);
134138568Ssam
135138568Ssam	/*
136138568Ssam	 * Detach any ACL'ator.
137138568Ssam	 */
138138568Ssam	if (ic->ic_acl != NULL)
139138568Ssam		ic->ic_acl->iac_detach(ic);
140116742Ssam}
141116742Ssam
142138568Ssam/*
143138568Ssam * Simple-minded authenticator module support.
144138568Ssam */
145138568Ssam
146138568Ssam#define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
147138568Ssam/* XXX well-known names */
148138568Ssamstatic const char *auth_modnames[IEEE80211_AUTH_MAX] = {
149138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_NONE */
150138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
151138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
152138568Ssam	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
153138568Ssam	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
154138568Ssam	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
155138568Ssam};
156138568Ssamstatic const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
157138568Ssam
158138568Ssamstatic const struct ieee80211_authenticator auth_internal = {
159138568Ssam	.ia_name		= "wlan_internal",
160138568Ssam	.ia_attach		= NULL,
161138568Ssam	.ia_detach		= NULL,
162138568Ssam	.ia_node_join		= NULL,
163138568Ssam	.ia_node_leave		= NULL,
164138568Ssam};
165138568Ssam
166138568Ssam/*
167138568Ssam * Setup internal authenticators once; they are never unregistered.
168138568Ssam */
169138568Ssamstatic void
170138568Ssamieee80211_auth_setup(void)
171138568Ssam{
172138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
173138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
174138568Ssam	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
175138568Ssam}
176138568SsamSYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
177138568Ssam
178138568Ssamconst struct ieee80211_authenticator *
179138568Ssamieee80211_authenticator_get(int auth)
180138568Ssam{
181138568Ssam	if (auth >= IEEE80211_AUTH_MAX)
182138568Ssam		return NULL;
183138568Ssam	if (authenticators[auth] == NULL)
184138568Ssam		ieee80211_load_module(auth_modnames[auth]);
185138568Ssam	return authenticators[auth];
186138568Ssam}
187138568Ssam
188116742Ssamvoid
189138568Ssamieee80211_authenticator_register(int type,
190138568Ssam	const struct ieee80211_authenticator *auth)
191116742Ssam{
192138568Ssam	if (type >= IEEE80211_AUTH_MAX)
193138568Ssam		return;
194138568Ssam	authenticators[type] = auth;
195138568Ssam}
196138568Ssam
197138568Ssamvoid
198138568Ssamieee80211_authenticator_unregister(int type)
199138568Ssam{
200138568Ssam
201138568Ssam	if (type >= IEEE80211_AUTH_MAX)
202138568Ssam		return;
203138568Ssam	authenticators[type] = NULL;
204138568Ssam}
205138568Ssam
206138568Ssam/*
207138568Ssam * Very simple-minded ACL module support.
208138568Ssam */
209138568Ssam/* XXX just one for now */
210138568Ssamstatic	const struct ieee80211_aclator *acl = NULL;
211138568Ssam
212138568Ssamvoid
213138568Ssamieee80211_aclator_register(const struct ieee80211_aclator *iac)
214138568Ssam{
215138568Ssam	printf("wlan: %s acl policy registered\n", iac->iac_name);
216138568Ssam	acl = iac;
217138568Ssam}
218138568Ssam
219138568Ssamvoid
220138568Ssamieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
221138568Ssam{
222138568Ssam	if (acl == iac)
223138568Ssam		acl = NULL;
224138568Ssam	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
225138568Ssam}
226138568Ssam
227138568Ssamconst struct ieee80211_aclator *
228138568Ssamieee80211_aclator_get(const char *name)
229138568Ssam{
230138568Ssam	if (acl == NULL)
231138568Ssam		ieee80211_load_module("wlan_acl");
232138568Ssam	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
233138568Ssam}
234138568Ssam
235138568Ssamvoid
236138568Ssamieee80211_print_essid(const u_int8_t *essid, int len)
237138568Ssam{
238138568Ssam	const u_int8_t *p;
239116742Ssam	int i;
240116742Ssam
241116742Ssam	if (len > IEEE80211_NWID_LEN)
242116742Ssam		len = IEEE80211_NWID_LEN;
243116742Ssam	/* determine printable or not */
244116742Ssam	for (i = 0, p = essid; i < len; i++, p++) {
245116742Ssam		if (*p < ' ' || *p > 0x7e)
246116742Ssam			break;
247116742Ssam	}
248116742Ssam	if (i == len) {
249116742Ssam		printf("\"");
250116742Ssam		for (i = 0, p = essid; i < len; i++, p++)
251116742Ssam			printf("%c", *p);
252116742Ssam		printf("\"");
253116742Ssam	} else {
254116742Ssam		printf("0x");
255116742Ssam		for (i = 0, p = essid; i < len; i++, p++)
256116742Ssam			printf("%02x", *p);
257116742Ssam	}
258116742Ssam}
259116742Ssam
260116742Ssamvoid
261138568Ssamieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi)
262116742Ssam{
263138568Ssam	const struct ieee80211_frame *wh;
264116742Ssam	int i;
265116742Ssam
266138568Ssam	wh = (const struct ieee80211_frame *)buf;
267116742Ssam	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
268116742Ssam	case IEEE80211_FC1_DIR_NODS:
269116742Ssam		printf("NODS %s", ether_sprintf(wh->i_addr2));
270116742Ssam		printf("->%s", ether_sprintf(wh->i_addr1));
271116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr3));
272116742Ssam		break;
273116742Ssam	case IEEE80211_FC1_DIR_TODS:
274116742Ssam		printf("TODS %s", ether_sprintf(wh->i_addr2));
275116742Ssam		printf("->%s", ether_sprintf(wh->i_addr3));
276116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr1));
277116742Ssam		break;
278116742Ssam	case IEEE80211_FC1_DIR_FROMDS:
279116742Ssam		printf("FRDS %s", ether_sprintf(wh->i_addr3));
280116742Ssam		printf("->%s", ether_sprintf(wh->i_addr1));
281116742Ssam		printf("(%s)", ether_sprintf(wh->i_addr2));
282116742Ssam		break;
283116742Ssam	case IEEE80211_FC1_DIR_DSTODS:
284138568Ssam		printf("DSDS %s", ether_sprintf((const u_int8_t *)&wh[1]));
285116742Ssam		printf("->%s", ether_sprintf(wh->i_addr3));
286116742Ssam		printf("(%s", ether_sprintf(wh->i_addr2));
287116742Ssam		printf("->%s)", ether_sprintf(wh->i_addr1));
288116742Ssam		break;
289116742Ssam	}
290116742Ssam	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
291116742Ssam	case IEEE80211_FC0_TYPE_DATA:
292116742Ssam		printf(" data");
293116742Ssam		break;
294116742Ssam	case IEEE80211_FC0_TYPE_MGT:
295116742Ssam		printf(" %s", ieee80211_mgt_subtype_name[
296116742Ssam		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
297116742Ssam		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
298116742Ssam		break;
299116742Ssam	default:
300116742Ssam		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
301116742Ssam		break;
302116742Ssam	}
303138568Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
304138568Ssam		int i;
305138568Ssam		printf(" WEP [IV");
306138568Ssam		for (i = 0; i < IEEE80211_WEP_IVLEN; i++)
307138568Ssam			printf(" %.02x", buf[sizeof(*wh)+i]);
308138568Ssam		printf(" KID %u]", buf[sizeof(*wh)+i] >> 6);
309138568Ssam	}
310116742Ssam	if (rate >= 0)
311116742Ssam		printf(" %dM", rate / 2);
312116742Ssam	if (rssi >= 0)
313116742Ssam		printf(" +%d", rssi);
314116742Ssam	printf("\n");
315116742Ssam	if (len > 0) {
316116742Ssam		for (i = 0; i < len; i++) {
317116742Ssam			if ((i & 1) == 0)
318116742Ssam				printf(" ");
319116742Ssam			printf("%02x", buf[i]);
320116742Ssam		}
321116742Ssam		printf("\n");
322116742Ssam	}
323116742Ssam}
324116742Ssam
325116742Ssamint
326148299Ssamieee80211_fix_rate(struct ieee80211_node *ni, int flags)
327116742Ssam{
328116742Ssam#define	RV(v)	((v) & IEEE80211_RATE_VAL)
329148299Ssam	struct ieee80211com *ic = ni->ni_ic;
330116742Ssam	int i, j, ignore, error;
331138568Ssam	int okrate, badrate, fixedrate;
332165569Ssam	const struct ieee80211_rateset *srs;
333165569Ssam	struct ieee80211_rateset *nrs;
334116742Ssam	u_int8_t r;
335116742Ssam
336138568Ssam	/*
337138568Ssam	 * If the fixed rate check was requested but no
338138568Ssam	 * fixed has been defined then just remove it.
339138568Ssam	 */
340148290Ssam	if ((flags & IEEE80211_F_DOFRATE) &&
341148290Ssam	    ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
342138568Ssam		flags &= ~IEEE80211_F_DOFRATE;
343116742Ssam	error = 0;
344138568Ssam	okrate = badrate = fixedrate = 0;
345165569Ssam	srs = ieee80211_get_suprates(ic, ni->ni_chan);
346116742Ssam	nrs = &ni->ni_rates;
347120482Ssam	for (i = 0; i < nrs->rs_nrates; ) {
348116742Ssam		ignore = 0;
349116742Ssam		if (flags & IEEE80211_F_DOSORT) {
350116742Ssam			/*
351116742Ssam			 * Sort rates.
352116742Ssam			 */
353116742Ssam			for (j = i + 1; j < nrs->rs_nrates; j++) {
354116742Ssam				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
355116742Ssam					r = nrs->rs_rates[i];
356116742Ssam					nrs->rs_rates[i] = nrs->rs_rates[j];
357116742Ssam					nrs->rs_rates[j] = r;
358116742Ssam				}
359116742Ssam			}
360116742Ssam		}
361116742Ssam		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
362116742Ssam		badrate = r;
363116742Ssam		if (flags & IEEE80211_F_DOFRATE) {
364116742Ssam			/*
365138568Ssam			 * Check any fixed rate is included.
366116742Ssam			 */
367138568Ssam			if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
368138568Ssam				fixedrate = r;
369116742Ssam		}
370116742Ssam		if (flags & IEEE80211_F_DONEGO) {
371116742Ssam			/*
372116742Ssam			 * Check against supported rates.
373116742Ssam			 */
374116742Ssam			for (j = 0; j < srs->rs_nrates; j++) {
375127761Ssam				if (r == RV(srs->rs_rates[j])) {
376127761Ssam					/*
377127761Ssam					 * Overwrite with the supported rate
378127761Ssam					 * value so any basic rate bit is set.
379127761Ssam					 * This insures that response we send
380127761Ssam					 * to stations have the necessary basic
381127761Ssam					 * rate bit set.
382127761Ssam					 */
383127761Ssam					nrs->rs_rates[i] = srs->rs_rates[j];
384116742Ssam					break;
385127761Ssam				}
386116742Ssam			}
387116742Ssam			if (j == srs->rs_nrates) {
388120482Ssam				/*
389120482Ssam				 * A rate in the node's rate set is not
390120482Ssam				 * supported.  If this is a basic rate and we
391120482Ssam				 * are operating as an AP then this is an error.
392120482Ssam				 * Otherwise we just discard/ignore the rate.
393120482Ssam				 * Note that this is important for 11b stations
394120482Ssam				 * when they want to associate with an 11g AP.
395120482Ssam				 */
396120482Ssam				if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
397120482Ssam				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
398116742Ssam					error++;
399116742Ssam				ignore++;
400116742Ssam			}
401116742Ssam		}
402116742Ssam		if (flags & IEEE80211_F_DODEL) {
403116742Ssam			/*
404116742Ssam			 * Delete unacceptable rates.
405116742Ssam			 */
406116742Ssam			if (ignore) {
407116742Ssam				nrs->rs_nrates--;
408116742Ssam				for (j = i; j < nrs->rs_nrates; j++)
409116742Ssam					nrs->rs_rates[j] = nrs->rs_rates[j + 1];
410116742Ssam				nrs->rs_rates[j] = 0;
411116742Ssam				continue;
412116742Ssam			}
413116742Ssam		}
414116742Ssam		if (!ignore)
415116742Ssam			okrate = nrs->rs_rates[i];
416116742Ssam		i++;
417116742Ssam	}
418138568Ssam	if (okrate == 0 || error != 0 ||
419138568Ssam	    ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
420116742Ssam		return badrate | IEEE80211_RATE_BASIC;
421116742Ssam	else
422116742Ssam		return RV(okrate);
423116742Ssam#undef RV
424116742Ssam}
425116742Ssam
426138568Ssam/*
427138568Ssam * Reset 11g-related state.
428138568Ssam */
429138568Ssamvoid
430138568Ssamieee80211_reset_erp(struct ieee80211com *ic)
431138568Ssam{
432138568Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
433138568Ssam	ic->ic_nonerpsta = 0;
434138568Ssam	ic->ic_longslotsta = 0;
435138568Ssam	/*
436138568Ssam	 * Short slot time is enabled only when operating in 11g
437138568Ssam	 * and not in an IBSS.  We must also honor whether or not
438138568Ssam	 * the driver is capable of doing it.
439138568Ssam	 */
440138568Ssam	ieee80211_set_shortslottime(ic,
441138568Ssam		ic->ic_curmode == IEEE80211_MODE_11A ||
442138568Ssam		(ic->ic_curmode == IEEE80211_MODE_11G &&
443138568Ssam		ic->ic_opmode == IEEE80211_M_HOSTAP &&
444138568Ssam		(ic->ic_caps & IEEE80211_C_SHSLOT)));
445138568Ssam	/*
446138568Ssam	 * Set short preamble and ERP barker-preamble flags.
447138568Ssam	 */
448138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11A ||
449138568Ssam	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
450138568Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
451138568Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
452138568Ssam	} else {
453138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
454138568Ssam		ic->ic_flags |= IEEE80211_F_USEBARKER;
455138568Ssam	}
456138568Ssam}
457138568Ssam
458138568Ssam/*
459138568Ssam * Set the short slot time state and notify the driver.
460138568Ssam */
461138568Ssamvoid
462138568Ssamieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
463138568Ssam{
464138568Ssam	if (onoff)
465138568Ssam		ic->ic_flags |= IEEE80211_F_SHSLOT;
466138568Ssam	else
467138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
468138568Ssam	/* notify driver */
469138568Ssam	if (ic->ic_updateslot != NULL)
470138568Ssam		ic->ic_updateslot(ic->ic_ifp);
471138568Ssam}
472138568Ssam
473138568Ssam/*
474138568Ssam * Check if the specified rate set supports ERP.
475138568Ssam * NB: the rate set is assumed to be sorted.
476138568Ssam */
477138568Ssamint
478138568Ssamieee80211_iserp_rateset(struct ieee80211com *ic, struct ieee80211_rateset *rs)
479138568Ssam{
480138568Ssam#define N(a)	(sizeof(a) / sizeof(a[0]))
481138568Ssam	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
482138568Ssam	int i, j;
483138568Ssam
484138568Ssam	if (rs->rs_nrates < N(rates))
485138568Ssam		return 0;
486138568Ssam	for (i = 0; i < N(rates); i++) {
487138568Ssam		for (j = 0; j < rs->rs_nrates; j++) {
488138568Ssam			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
489138568Ssam			if (rates[i] == r)
490138568Ssam				goto next;
491138568Ssam			if (r > rates[i])
492138568Ssam				return 0;
493138568Ssam		}
494138568Ssam		return 0;
495138568Ssam	next:
496138568Ssam		;
497138568Ssam	}
498138568Ssam	return 1;
499138568Ssam#undef N
500138568Ssam}
501138568Ssam
502138568Ssam/*
503138568Ssam * Mark the basic rates for the 11g rate table based on the
504138568Ssam * operating mode.  For real 11g we mark all the 11b rates
505138568Ssam * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
506138568Ssam * 11b rates.  There's also a pseudo 11a-mode used to mark only
507138568Ssam * the basic OFDM rates.
508138568Ssam */
509138568Ssamvoid
510138568Ssamieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
511138568Ssam{
512138568Ssam	static const struct ieee80211_rateset basic[] = {
513138568Ssam	    { 0 },			/* IEEE80211_MODE_AUTO */
514138568Ssam	    { 3, { 12, 24, 48 } },	/* IEEE80211_MODE_11A */
515138568Ssam	    { 2, { 2, 4 } },		/* IEEE80211_MODE_11B */
516138568Ssam	    { 4, { 2, 4, 11, 22 } },	/* IEEE80211_MODE_11G (mixed b/g) */
517138568Ssam	    { 0 },			/* IEEE80211_MODE_FH */
518138568Ssam					/* IEEE80211_MODE_PUREG (not yet) */
519138568Ssam	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },
520138568Ssam	};
521138568Ssam	int i, j;
522138568Ssam
523138568Ssam	for (i = 0; i < rs->rs_nrates; i++) {
524138568Ssam		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
525138568Ssam		for (j = 0; j < basic[mode].rs_nrates; j++)
526138568Ssam			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
527138568Ssam				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
528138568Ssam				break;
529138568Ssam			}
530138568Ssam	}
531138568Ssam}
532138568Ssam
533138568Ssam/*
534138568Ssam * WME protocol support.  The following parameters come from the spec.
535138568Ssam */
536138568Ssamtypedef struct phyParamType {
537138568Ssam	u_int8_t aifsn;
538138568Ssam	u_int8_t logcwmin;
539138568Ssam	u_int8_t logcwmax;
540138568Ssam	u_int16_t txopLimit;
541138568Ssam	u_int8_t acm;
542138568Ssam} paramType;
543138568Ssam
544138568Ssamstatic const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
545138568Ssam	{ 3, 4, 6 },		/* IEEE80211_MODE_AUTO */
546138568Ssam	{ 3, 4, 6 },		/* IEEE80211_MODE_11A */
547138568Ssam	{ 3, 5, 7 },		/* IEEE80211_MODE_11B */
548138568Ssam	{ 3, 4, 6 },		/* IEEE80211_MODE_11G */
549138568Ssam	{ 3, 5, 7 },		/* IEEE80211_MODE_FH */
550138568Ssam	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_A */
551138568Ssam	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_G */
552138568Ssam};
553138568Ssamstatic const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
554138568Ssam	{ 7, 4, 10 },		/* IEEE80211_MODE_AUTO */
555138568Ssam	{ 7, 4, 10 },		/* IEEE80211_MODE_11A */
556138568Ssam	{ 7, 5, 10 },		/* IEEE80211_MODE_11B */
557138568Ssam	{ 7, 4, 10 },		/* IEEE80211_MODE_11G */
558138568Ssam	{ 7, 5, 10 },		/* IEEE80211_MODE_FH */
559138568Ssam	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
560138568Ssam	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
561138568Ssam};
562138568Ssamstatic const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
563138568Ssam	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
564138568Ssam	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11A */
565138568Ssam	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_11B */
566138568Ssam	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11G */
567138568Ssam	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_FH */
568138568Ssam	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
569138568Ssam	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
570138568Ssam};
571138568Ssamstatic const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
572138568Ssam	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
573138568Ssam	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11A */
574138568Ssam	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_11B */
575138568Ssam	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11G */
576138568Ssam	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_FH */
577138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
578138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
579138568Ssam};
580138568Ssam
581138568Ssamstatic const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
582138568Ssam	{ 3, 4, 10 },		/* IEEE80211_MODE_AUTO */
583138568Ssam	{ 3, 4, 10 },		/* IEEE80211_MODE_11A */
584138568Ssam	{ 3, 5, 10 },		/* IEEE80211_MODE_11B */
585138568Ssam	{ 3, 4, 10 },		/* IEEE80211_MODE_11G */
586138568Ssam	{ 3, 5, 10 },		/* IEEE80211_MODE_FH */
587138568Ssam	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
588138568Ssam	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
589138568Ssam};
590138568Ssamstatic const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
591138568Ssam	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
592138568Ssam	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11A */
593138568Ssam	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_11B */
594138568Ssam	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11G */
595138568Ssam	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_FH */
596138568Ssam	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
597138568Ssam	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
598138568Ssam};
599138568Ssamstatic const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
600138568Ssam	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
601138568Ssam	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11A */
602138568Ssam	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_11B */
603138568Ssam	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11G */
604138568Ssam	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_FH */
605138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
606138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
607138568Ssam};
608138568Ssam
609138568Ssamvoid
610138568Ssamieee80211_wme_initparams(struct ieee80211com *ic)
611138568Ssam{
612138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
613138568Ssam	const paramType *pPhyParam, *pBssPhyParam;
614138568Ssam	struct wmeParams *wmep;
615138568Ssam	int i;
616138568Ssam
617138568Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
618138568Ssam		return;
619138568Ssam
620138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
621138568Ssam		switch (i) {
622138568Ssam		case WME_AC_BK:
623138568Ssam			pPhyParam = &phyParamForAC_BK[ic->ic_curmode];
624138568Ssam			pBssPhyParam = &phyParamForAC_BK[ic->ic_curmode];
625138568Ssam			break;
626138568Ssam		case WME_AC_VI:
627138568Ssam			pPhyParam = &phyParamForAC_VI[ic->ic_curmode];
628138568Ssam			pBssPhyParam = &bssPhyParamForAC_VI[ic->ic_curmode];
629138568Ssam			break;
630138568Ssam		case WME_AC_VO:
631138568Ssam			pPhyParam = &phyParamForAC_VO[ic->ic_curmode];
632138568Ssam			pBssPhyParam = &bssPhyParamForAC_VO[ic->ic_curmode];
633138568Ssam			break;
634138568Ssam		case WME_AC_BE:
635138568Ssam		default:
636138568Ssam			pPhyParam = &phyParamForAC_BE[ic->ic_curmode];
637138568Ssam			pBssPhyParam = &bssPhyParamForAC_BE[ic->ic_curmode];
638138568Ssam			break;
639138568Ssam		}
640138568Ssam
641138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
642138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
643138568Ssam			wmep->wmep_acm = pPhyParam->acm;
644138568Ssam			wmep->wmep_aifsn = pPhyParam->aifsn;
645138568Ssam			wmep->wmep_logcwmin = pPhyParam->logcwmin;
646138568Ssam			wmep->wmep_logcwmax = pPhyParam->logcwmax;
647138568Ssam			wmep->wmep_txopLimit = pPhyParam->txopLimit;
648138568Ssam		} else {
649138568Ssam			wmep->wmep_acm = pBssPhyParam->acm;
650138568Ssam			wmep->wmep_aifsn = pBssPhyParam->aifsn;
651138568Ssam			wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
652138568Ssam			wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
653138568Ssam			wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
654138568Ssam
655138568Ssam		}
656138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
657138568Ssam			"%s: %s chan [acm %u aifsn %u log2(cwmin) %u "
658138568Ssam			"log2(cwmax) %u txpoLimit %u]\n", __func__
659138568Ssam			, ieee80211_wme_acnames[i]
660138568Ssam			, wmep->wmep_acm
661138568Ssam			, wmep->wmep_aifsn
662138568Ssam			, wmep->wmep_logcwmin
663138568Ssam			, wmep->wmep_logcwmax
664138568Ssam			, wmep->wmep_txopLimit
665138568Ssam		);
666138568Ssam
667138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
668138568Ssam		wmep->wmep_acm = pBssPhyParam->acm;
669138568Ssam		wmep->wmep_aifsn = pBssPhyParam->aifsn;
670138568Ssam		wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
671138568Ssam		wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
672138568Ssam		wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
673138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
674138568Ssam			"%s: %s  bss [acm %u aifsn %u log2(cwmin) %u "
675138568Ssam			"log2(cwmax) %u txpoLimit %u]\n", __func__
676138568Ssam			, ieee80211_wme_acnames[i]
677138568Ssam			, wmep->wmep_acm
678138568Ssam			, wmep->wmep_aifsn
679138568Ssam			, wmep->wmep_logcwmin
680138568Ssam			, wmep->wmep_logcwmax
681138568Ssam			, wmep->wmep_txopLimit
682138568Ssam		);
683138568Ssam	}
684138568Ssam	/* NB: check ic_bss to avoid NULL deref on initial attach */
685138568Ssam	if (ic->ic_bss != NULL) {
686138568Ssam		/*
687138568Ssam		 * Calculate agressive mode switching threshold based
688138568Ssam		 * on beacon interval.  This doesn't need locking since
689138568Ssam		 * we're only called before entering the RUN state at
690138568Ssam		 * which point we start sending beacon frames.
691138568Ssam		 */
692138568Ssam		wme->wme_hipri_switch_thresh =
693138568Ssam			(HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100;
694138568Ssam		ieee80211_wme_updateparams(ic);
695138568Ssam	}
696138568Ssam}
697138568Ssam
698138568Ssam/*
699138568Ssam * Update WME parameters for ourself and the BSS.
700138568Ssam */
701138568Ssamvoid
702138568Ssamieee80211_wme_updateparams_locked(struct ieee80211com *ic)
703138568Ssam{
704138568Ssam	static const paramType phyParam[IEEE80211_MODE_MAX] = {
705138568Ssam		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_AUTO */
706138568Ssam		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11A */
707138568Ssam		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_11B */
708138568Ssam		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11G */
709138568Ssam		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_FH */
710138568Ssam		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_A */
711138568Ssam		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_G */
712138568Ssam	};
713138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
714138568Ssam	const struct wmeParams *wmep;
715138568Ssam	struct wmeParams *chanp, *bssp;
716138568Ssam	int i;
717138568Ssam
718138568Ssam       	/* set up the channel access parameters for the physical device */
719138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
720138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[i];
721138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
722138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
723138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
724138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
725138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
726138568Ssam
727138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
728138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
729138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
730138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
731138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
732138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
733138568Ssam	}
734138568Ssam
735138568Ssam	/*
736138568Ssam	 * This implements agressive mode as found in certain
737138568Ssam	 * vendors' AP's.  When there is significant high
738138568Ssam	 * priority (VI/VO) traffic in the BSS throttle back BE
739138568Ssam	 * traffic by using conservative parameters.  Otherwise
740138568Ssam	 * BE uses agressive params to optimize performance of
741138568Ssam	 * legacy/non-QoS traffic.
742138568Ssam	 */
743138568Ssam        if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
744156524Ssam	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
745153974Ssam	    (ic->ic_opmode == IEEE80211_M_STA &&
746138568Ssam	     (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
747138568Ssam	    (ic->ic_flags & IEEE80211_F_WME) == 0) {
748138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
749138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
750138568Ssam
751138568Ssam		chanp->wmep_aifsn = bssp->wmep_aifsn =
752138568Ssam			phyParam[ic->ic_curmode].aifsn;
753138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
754138568Ssam			phyParam[ic->ic_curmode].logcwmin;
755138568Ssam		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
756138568Ssam			phyParam[ic->ic_curmode].logcwmax;
757138568Ssam		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
758153421Ssam			(ic->ic_flags & IEEE80211_F_BURST) ?
759138568Ssam				phyParam[ic->ic_curmode].txopLimit : 0;
760138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
761138568Ssam			"%s: %s [acm %u aifsn %u log2(cwmin) %u "
762138568Ssam			"log2(cwmax) %u txpoLimit %u]\n", __func__
763138568Ssam			, ieee80211_wme_acnames[WME_AC_BE]
764138568Ssam			, chanp->wmep_acm
765138568Ssam			, chanp->wmep_aifsn
766138568Ssam			, chanp->wmep_logcwmin
767138568Ssam			, chanp->wmep_logcwmax
768138568Ssam			, chanp->wmep_txopLimit
769138568Ssam		);
770138568Ssam	}
771138568Ssam
772138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
773156524Ssam	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
774138568Ssam        	static const u_int8_t logCwMin[IEEE80211_MODE_MAX] = {
775138568Ssam              		3,	/* IEEE80211_MODE_AUTO */
776138568Ssam              		3,	/* IEEE80211_MODE_11A */
777138568Ssam              		4,	/* IEEE80211_MODE_11B */
778138568Ssam              		3,	/* IEEE80211_MODE_11G */
779138568Ssam              		4,	/* IEEE80211_MODE_FH */
780138568Ssam              		3,	/* IEEE80211_MODE_TURBO_A */
781138568Ssam              		3,	/* IEEE80211_MODE_TURBO_G */
782138568Ssam		};
783138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
784138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
785138568Ssam
786138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
787138568Ssam			logCwMin[ic->ic_curmode];
788138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
789138568Ssam			"%s: %s log2(cwmin) %u\n", __func__
790138568Ssam			, ieee80211_wme_acnames[WME_AC_BE]
791138568Ssam			, chanp->wmep_logcwmin
792138568Ssam		);
793138568Ssam    	}
794138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
795138568Ssam		/*
796138568Ssam		 * Arrange for a beacon update and bump the parameter
797138568Ssam		 * set number so associated stations load the new values.
798138568Ssam		 */
799138568Ssam		wme->wme_bssChanParams.cap_info =
800138568Ssam			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
801138568Ssam		ic->ic_flags |= IEEE80211_F_WMEUPDATE;
802138568Ssam	}
803138568Ssam
804138568Ssam	wme->wme_update(ic);
805138568Ssam
806138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
807138568Ssam		"%s: WME params updated, cap_info 0x%x\n", __func__,
808138568Ssam		ic->ic_opmode == IEEE80211_M_STA ?
809138568Ssam			wme->wme_wmeChanParams.cap_info :
810138568Ssam			wme->wme_bssChanParams.cap_info);
811138568Ssam}
812138568Ssam
813138568Ssamvoid
814138568Ssamieee80211_wme_updateparams(struct ieee80211com *ic)
815138568Ssam{
816138568Ssam
817138568Ssam	if (ic->ic_caps & IEEE80211_C_WME) {
818138568Ssam		IEEE80211_BEACON_LOCK(ic);
819138568Ssam		ieee80211_wme_updateparams_locked(ic);
820138568Ssam		IEEE80211_BEACON_UNLOCK(ic);
821138568Ssam	}
822138568Ssam}
823138568Ssam
824153349Ssamvoid
825153349Ssamieee80211_beacon_miss(struct ieee80211com *ic)
826153349Ssam{
827153349Ssam
828153349Ssam	if (ic->ic_flags & IEEE80211_F_SCAN) {
829153349Ssam		/* XXX check ic_curchan != ic_bsschan? */
830153349Ssam		return;
831153349Ssam	}
832153349Ssam	IEEE80211_DPRINTF(ic,
833153349Ssam		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
834153349Ssam		"%s\n", "beacon miss");
835153349Ssam
836153349Ssam	/*
837153349Ssam	 * Our handling is only meaningful for stations that are
838153349Ssam	 * associated; any other conditions else will be handled
839153349Ssam	 * through different means (e.g. the tx timeout on mgt frames).
840153349Ssam	 */
841153349Ssam	if (ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
842153349Ssam		return;
843153349Ssam
844153349Ssam	if (++ic->ic_bmiss_count < ic->ic_bmiss_max) {
845153349Ssam		/*
846153349Ssam		 * Send a directed probe req before falling back to a scan;
847153349Ssam		 * if we receive a response ic_bmiss_count will be reset.
848153349Ssam		 * Some cards mistakenly report beacon miss so this avoids
849153349Ssam		 * the expensive scan if the ap is still there.
850153349Ssam		 */
851153349Ssam		ieee80211_send_probereq(ic->ic_bss, ic->ic_myaddr,
852153349Ssam			ic->ic_bss->ni_bssid, ic->ic_bss->ni_bssid,
853153349Ssam			ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen,
854153349Ssam			ic->ic_opt_ie, ic->ic_opt_ie_len);
855153349Ssam		return;
856153349Ssam	}
857153349Ssam	ic->ic_bmiss_count = 0;
858153349Ssam	ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
859153349Ssam}
860153349Ssam
861154736Ssam/*
862154736Ssam * Software beacon miss handling.  Check if any beacons
863154736Ssam * were received in the last period.  If not post a
864154736Ssam * beacon miss; otherwise reset the counter.
865154736Ssam */
866147765Ssamstatic void
867154736Ssamieee80211_swbmiss(void *arg)
868154736Ssam{
869154736Ssam	struct ieee80211com *ic = arg;
870154736Ssam
871154736Ssam	if (ic->ic_swbmiss_count == 0) {
872154736Ssam		ieee80211_beacon_miss(ic);
873154736Ssam		if (ic->ic_bmiss_count == 0)	/* don't re-arm timer */
874154736Ssam			return;
875154736Ssam	} else
876154736Ssam		ic->ic_swbmiss_count = 0;
877154736Ssam	callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
878154736Ssam		ieee80211_swbmiss, ic);
879154736Ssam}
880154736Ssam
881154736Ssamstatic void
882147765Ssamsta_disassoc(void *arg, struct ieee80211_node *ni)
883147765Ssam{
884147765Ssam	struct ieee80211com *ic = arg;
885147765Ssam
886147765Ssam	if (ni->ni_associd != 0) {
887147765Ssam		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
888147765Ssam			IEEE80211_REASON_ASSOC_LEAVE);
889147765Ssam		ieee80211_node_leave(ic, ni);
890147765Ssam	}
891147765Ssam}
892147765Ssam
893147765Ssamstatic void
894147765Ssamsta_deauth(void *arg, struct ieee80211_node *ni)
895147765Ssam{
896147765Ssam	struct ieee80211com *ic = arg;
897147765Ssam
898147765Ssam	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
899147765Ssam		IEEE80211_REASON_ASSOC_LEAVE);
900147765Ssam}
901147765Ssam
902117811Ssamstatic int
903138568Ssamieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
904116742Ssam{
905138568Ssam	struct ifnet *ifp = ic->ic_ifp;
906116742Ssam	struct ieee80211_node *ni;
907117811Ssam	enum ieee80211_state ostate;
908116742Ssam
909116742Ssam	ostate = ic->ic_state;
910138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
911138568Ssam		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
912117811Ssam	ic->ic_state = nstate;			/* state transition */
913116742Ssam	ni = ic->ic_bss;			/* NB: no reference held */
914154736Ssam	if (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)
915154736Ssam		callout_stop(&ic->ic_swbmiss);
916116742Ssam	switch (nstate) {
917116742Ssam	case IEEE80211_S_INIT:
918116742Ssam		switch (ostate) {
919116742Ssam		case IEEE80211_S_INIT:
920116742Ssam			break;
921116742Ssam		case IEEE80211_S_RUN:
922116742Ssam			switch (ic->ic_opmode) {
923116742Ssam			case IEEE80211_M_STA:
924116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
925116742Ssam				    IEEE80211_FC0_SUBTYPE_DISASSOC,
926116742Ssam				    IEEE80211_REASON_ASSOC_LEAVE);
927138568Ssam				ieee80211_sta_leave(ic, ni);
928116742Ssam				break;
929116742Ssam			case IEEE80211_M_HOSTAP:
930147765Ssam				ieee80211_iterate_nodes(&ic->ic_sta,
931147765Ssam					sta_disassoc, ic);
932116742Ssam				break;
933116742Ssam			default:
934116742Ssam				break;
935116742Ssam			}
936138568Ssam			goto reset;
937116742Ssam		case IEEE80211_S_ASSOC:
938116742Ssam			switch (ic->ic_opmode) {
939116742Ssam			case IEEE80211_M_STA:
940116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
941116742Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
942116742Ssam				    IEEE80211_REASON_AUTH_LEAVE);
943116742Ssam				break;
944116742Ssam			case IEEE80211_M_HOSTAP:
945147765Ssam				ieee80211_iterate_nodes(&ic->ic_sta,
946147765Ssam					sta_deauth, ic);
947116742Ssam				break;
948116742Ssam			default:
949116742Ssam				break;
950116742Ssam			}
951138568Ssam			goto reset;
952140441Ssam		case IEEE80211_S_SCAN:
953140441Ssam			ieee80211_cancel_scan(ic);
954140441Ssam			goto reset;
955116742Ssam		case IEEE80211_S_AUTH:
956138568Ssam		reset:
957116742Ssam			ic->ic_mgt_timer = 0;
958116742Ssam			IF_DRAIN(&ic->ic_mgtq);
959138568Ssam			ieee80211_reset_bss(ic);
960116742Ssam			break;
961116742Ssam		}
962138568Ssam		if (ic->ic_auth->ia_detach != NULL)
963138568Ssam			ic->ic_auth->ia_detach(ic);
964116742Ssam		break;
965116742Ssam	case IEEE80211_S_SCAN:
966116742Ssam		switch (ostate) {
967116742Ssam		case IEEE80211_S_INIT:
968138568Ssam			if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
969138568Ssam			     ic->ic_opmode == IEEE80211_M_IBSS ||
970138568Ssam			     ic->ic_opmode == IEEE80211_M_AHDEMO) &&
971116742Ssam			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
972116742Ssam				/*
973116742Ssam				 * AP operation and we already have a channel;
974116742Ssam				 * bypass the scan and startup immediately.
975116742Ssam				 */
976116742Ssam				ieee80211_create_ibss(ic, ic->ic_des_chan);
977116742Ssam			} else {
978138568Ssam				ieee80211_begin_scan(ic, arg);
979116742Ssam			}
980116742Ssam			break;
981116742Ssam		case IEEE80211_S_SCAN:
982138568Ssam			/*
983156358Ssam			 * Scan next. If doing an active scan probe
984156358Ssam			 * for the requested ap (if any).
985138568Ssam			 */
986156358Ssam			if (ic->ic_flags & IEEE80211_F_ASCAN)
987156358Ssam				ieee80211_probe_curchan(ic, 0);
988116742Ssam			break;
989116742Ssam		case IEEE80211_S_RUN:
990116742Ssam			/* beacon miss */
991138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE,
992138568Ssam				"no recent beacons from %s; rescanning\n",
993138568Ssam				ether_sprintf(ic->ic_bss->ni_bssid));
994138568Ssam			ieee80211_sta_leave(ic, ni);
995138568Ssam			ic->ic_flags &= ~IEEE80211_F_SIBSS;	/* XXX */
996116742Ssam			/* FALLTHRU */
997116742Ssam		case IEEE80211_S_AUTH:
998116742Ssam		case IEEE80211_S_ASSOC:
999116742Ssam			/* timeout restart scan */
1000138568Ssam			ni = ieee80211_find_node(&ic->ic_scan,
1001138568Ssam				ic->ic_bss->ni_macaddr);
1002116742Ssam			if (ni != NULL) {
1003116742Ssam				ni->ni_fails++;
1004116742Ssam				ieee80211_unref_node(&ni);
1005116742Ssam			}
1006147116Ssam			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
1007147116Ssam				ieee80211_begin_scan(ic, arg);
1008116742Ssam			break;
1009116742Ssam		}
1010116742Ssam		break;
1011116742Ssam	case IEEE80211_S_AUTH:
1012116742Ssam		switch (ostate) {
1013116742Ssam		case IEEE80211_S_INIT:
1014116742Ssam		case IEEE80211_S_SCAN:
1015116742Ssam			IEEE80211_SEND_MGMT(ic, ni,
1016116742Ssam			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1017116742Ssam			break;
1018116742Ssam		case IEEE80211_S_AUTH:
1019116742Ssam		case IEEE80211_S_ASSOC:
1020138568Ssam			switch (arg) {
1021116742Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
1022116742Ssam				/* ??? */
1023116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
1024116742Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1025116742Ssam				break;
1026116742Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1027116742Ssam				/* ignore and retry scan on timeout */
1028116742Ssam				break;
1029116742Ssam			}
1030116742Ssam			break;
1031116742Ssam		case IEEE80211_S_RUN:
1032138568Ssam			switch (arg) {
1033116742Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
1034116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
1035116742Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1036116742Ssam				ic->ic_state = ostate;	/* stay RUN */
1037116742Ssam				break;
1038116742Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1039138568Ssam				ieee80211_sta_leave(ic, ni);
1040147116Ssam				if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1041147116Ssam					/* try to reauth */
1042147116Ssam					IEEE80211_SEND_MGMT(ic, ni,
1043147116Ssam					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1044147116Ssam				}
1045116742Ssam				break;
1046116742Ssam			}
1047116742Ssam			break;
1048116742Ssam		}
1049116742Ssam		break;
1050116742Ssam	case IEEE80211_S_ASSOC:
1051116742Ssam		switch (ostate) {
1052116742Ssam		case IEEE80211_S_INIT:
1053116742Ssam		case IEEE80211_S_SCAN:
1054116742Ssam		case IEEE80211_S_ASSOC:
1055138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1056138568Ssam				"%s: invalid transition\n", __func__);
1057116742Ssam			break;
1058116742Ssam		case IEEE80211_S_AUTH:
1059116742Ssam			IEEE80211_SEND_MGMT(ic, ni,
1060116742Ssam			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1061116742Ssam			break;
1062116742Ssam		case IEEE80211_S_RUN:
1063138568Ssam			ieee80211_sta_leave(ic, ni);
1064147116Ssam			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1065147116Ssam				IEEE80211_SEND_MGMT(ic, ni,
1066147116Ssam				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
1067147116Ssam			}
1068116742Ssam			break;
1069116742Ssam		}
1070116742Ssam		break;
1071116742Ssam	case IEEE80211_S_RUN:
1072138568Ssam		if (ic->ic_flags & IEEE80211_F_WPA) {
1073138568Ssam			/* XXX validate prerequisites */
1074138568Ssam		}
1075116742Ssam		switch (ostate) {
1076116742Ssam		case IEEE80211_S_INIT:
1077138568Ssam			if (ic->ic_opmode == IEEE80211_M_MONITOR)
1078138568Ssam				break;
1079138568Ssam			/* fall thru... */
1080116742Ssam		case IEEE80211_S_AUTH:
1081138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1082138568Ssam				"%s: invalid transition\n", __func__);
1083140763Ssam			/* fall thru... */
1084140763Ssam		case IEEE80211_S_RUN:
1085116742Ssam			break;
1086116742Ssam		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
1087116742Ssam		case IEEE80211_S_ASSOC:		/* infra mode */
1088116742Ssam			KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
1089116742Ssam				("%s: bogus xmit rate %u setup\n", __func__,
1090116742Ssam					ni->ni_txrate));
1091138568Ssam#ifdef IEEE80211_DEBUG
1092138568Ssam			if (ieee80211_msg_debug(ic)) {
1093116742Ssam				if (ic->ic_opmode == IEEE80211_M_STA)
1094138568Ssam					if_printf(ifp, "associated ");
1095116742Ssam				else
1096138568Ssam					if_printf(ifp, "synchronized ");
1097116742Ssam				printf("with %s ssid ",
1098116742Ssam				    ether_sprintf(ni->ni_bssid));
1099116742Ssam				ieee80211_print_essid(ic->ic_bss->ni_essid,
1100116742Ssam				    ni->ni_esslen);
1101116742Ssam				printf(" channel %d start %uMb\n",
1102148936Ssam					ieee80211_chan2ieee(ic, ic->ic_curchan),
1103116742Ssam					IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
1104116742Ssam			}
1105138568Ssam#endif
1106116742Ssam			ic->ic_mgt_timer = 0;
1107138568Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1108138568Ssam				ieee80211_notify_node_join(ic, ni,
1109138568Ssam					arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1110138568Ssam			if_start(ifp);		/* XXX not authorized yet */
1111116742Ssam			break;
1112116742Ssam		}
1113154736Ssam		if (ostate != IEEE80211_S_RUN &&
1114154736Ssam		    ic->ic_opmode == IEEE80211_M_STA &&
1115154736Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)) {
1116154736Ssam			/*
1117154736Ssam			 * Start s/w beacon miss timer for devices w/o
1118154736Ssam			 * hardware support.  We fudge a bit here since
1119154736Ssam			 * we're doing this in software.
1120154736Ssam			 */
1121154736Ssam			ic->ic_swbmiss_period = IEEE80211_TU_TO_TICKS(
1122154736Ssam				2 * ic->ic_bmissthreshold * ni->ni_intval);
1123154736Ssam			ic->ic_swbmiss_count = 0;
1124154736Ssam			callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
1125154736Ssam				ieee80211_swbmiss, ic);
1126154736Ssam		}
1127138568Ssam		/*
1128138568Ssam		 * Start/stop the authenticator when operating as an
1129138568Ssam		 * AP.  We delay until here to allow configuration to
1130138568Ssam		 * happen out of order.
1131138568Ssam		 */
1132138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */
1133138568Ssam		    ic->ic_auth->ia_attach != NULL) {
1134138568Ssam			/* XXX check failure */
1135138568Ssam			ic->ic_auth->ia_attach(ic);
1136138568Ssam		} else if (ic->ic_auth->ia_detach != NULL) {
1137138568Ssam			ic->ic_auth->ia_detach(ic);
1138138568Ssam		}
1139138568Ssam		/*
1140138568Ssam		 * When 802.1x is not in use mark the port authorized
1141138568Ssam		 * at this point so traffic can flow.
1142138568Ssam		 */
1143138568Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
1144148302Ssam			ieee80211_node_authorize(ni);
1145138568Ssam		/*
1146138568Ssam		 * Enable inactivity processing.
1147138568Ssam		 * XXX
1148138568Ssam		 */
1149138568Ssam		ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT;
1150140753Ssam		ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT;
1151116742Ssam		break;
1152116742Ssam	}
1153116742Ssam	return 0;
1154116742Ssam}
1155