ieee80211_proto.c revision 165887
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 165887 2007-01-08 17:24:51Z 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
325165887Ssamstatic __inline int
326165887Ssamfindrix(const struct ieee80211_rateset *rs, int r)
327165887Ssam{
328165887Ssam	int i;
329165887Ssam
330165887Ssam	for (i = 0; i < rs->rs_nrates; i++)
331165887Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
332165887Ssam			return i;
333165887Ssam	return -1;
334165887Ssam}
335165887Ssam
336116742Ssamint
337148299Ssamieee80211_fix_rate(struct ieee80211_node *ni, int flags)
338116742Ssam{
339116742Ssam#define	RV(v)	((v) & IEEE80211_RATE_VAL)
340148299Ssam	struct ieee80211com *ic = ni->ni_ic;
341165887Ssam	int i, j, rix, error;
342138568Ssam	int okrate, badrate, fixedrate;
343165569Ssam	const struct ieee80211_rateset *srs;
344165569Ssam	struct ieee80211_rateset *nrs;
345116742Ssam	u_int8_t r;
346116742Ssam
347138568Ssam	/*
348138568Ssam	 * If the fixed rate check was requested but no
349138568Ssam	 * fixed has been defined then just remove it.
350138568Ssam	 */
351148290Ssam	if ((flags & IEEE80211_F_DOFRATE) &&
352148290Ssam	    ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
353138568Ssam		flags &= ~IEEE80211_F_DOFRATE;
354116742Ssam	error = 0;
355138568Ssam	okrate = badrate = fixedrate = 0;
356165569Ssam	srs = ieee80211_get_suprates(ic, ni->ni_chan);
357116742Ssam	nrs = &ni->ni_rates;
358120482Ssam	for (i = 0; i < nrs->rs_nrates; ) {
359116742Ssam		if (flags & IEEE80211_F_DOSORT) {
360116742Ssam			/*
361116742Ssam			 * Sort rates.
362116742Ssam			 */
363116742Ssam			for (j = i + 1; j < nrs->rs_nrates; j++) {
364116742Ssam				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
365116742Ssam					r = nrs->rs_rates[i];
366116742Ssam					nrs->rs_rates[i] = nrs->rs_rates[j];
367116742Ssam					nrs->rs_rates[j] = r;
368116742Ssam				}
369116742Ssam			}
370116742Ssam		}
371116742Ssam		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
372116742Ssam		badrate = r;
373116742Ssam		if (flags & IEEE80211_F_DOFRATE) {
374116742Ssam			/*
375138568Ssam			 * Check any fixed rate is included.
376116742Ssam			 */
377138568Ssam			if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
378138568Ssam				fixedrate = r;
379116742Ssam		}
380165887Ssam		/*
381165887Ssam		 * Check against supported rates.
382165887Ssam		 */
383165887Ssam		rix = findrix(srs, r);
384116742Ssam		if (flags & IEEE80211_F_DONEGO) {
385165887Ssam			if (rix < 0) {
386120482Ssam				/*
387120482Ssam				 * A rate in the node's rate set is not
388120482Ssam				 * supported.  If this is a basic rate and we
389165887Ssam				 * are operating as a STA then this is an error.
390120482Ssam				 * Otherwise we just discard/ignore the rate.
391120482Ssam				 */
392165887Ssam				if ((flags & IEEE80211_F_JOIN) &&
393120482Ssam				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
394116742Ssam					error++;
395165887Ssam			} else if ((flags & IEEE80211_F_JOIN) == 0) {
396165887Ssam				/*
397165887Ssam				 * Overwrite with the supported rate
398165887Ssam				 * value so any basic rate bit is set.
399165887Ssam				 */
400165887Ssam				nrs->rs_rates[i] = srs->rs_rates[rix];
401116742Ssam			}
402116742Ssam		}
403165887Ssam		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
404116742Ssam			/*
405116742Ssam			 * Delete unacceptable rates.
406116742Ssam			 */
407165887Ssam			nrs->rs_nrates--;
408165887Ssam			for (j = i; j < nrs->rs_nrates; j++)
409165887Ssam				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
410165887Ssam			nrs->rs_rates[j] = 0;
411165887Ssam			continue;
412116742Ssam		}
413165887Ssam		if (rix >= 0)
414116742Ssam			okrate = nrs->rs_rates[i];
415116742Ssam		i++;
416116742Ssam	}
417138568Ssam	if (okrate == 0 || error != 0 ||
418138568Ssam	    ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
419116742Ssam		return badrate | IEEE80211_RATE_BASIC;
420116742Ssam	else
421116742Ssam		return RV(okrate);
422116742Ssam#undef RV
423116742Ssam}
424116742Ssam
425138568Ssam/*
426138568Ssam * Reset 11g-related state.
427138568Ssam */
428138568Ssamvoid
429138568Ssamieee80211_reset_erp(struct ieee80211com *ic)
430138568Ssam{
431138568Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
432138568Ssam	ic->ic_nonerpsta = 0;
433138568Ssam	ic->ic_longslotsta = 0;
434138568Ssam	/*
435138568Ssam	 * Short slot time is enabled only when operating in 11g
436138568Ssam	 * and not in an IBSS.  We must also honor whether or not
437138568Ssam	 * the driver is capable of doing it.
438138568Ssam	 */
439138568Ssam	ieee80211_set_shortslottime(ic,
440138568Ssam		ic->ic_curmode == IEEE80211_MODE_11A ||
441138568Ssam		(ic->ic_curmode == IEEE80211_MODE_11G &&
442138568Ssam		ic->ic_opmode == IEEE80211_M_HOSTAP &&
443138568Ssam		(ic->ic_caps & IEEE80211_C_SHSLOT)));
444138568Ssam	/*
445138568Ssam	 * Set short preamble and ERP barker-preamble flags.
446138568Ssam	 */
447138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11A ||
448138568Ssam	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
449138568Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
450138568Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
451138568Ssam	} else {
452138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
453138568Ssam		ic->ic_flags |= IEEE80211_F_USEBARKER;
454138568Ssam	}
455138568Ssam}
456138568Ssam
457138568Ssam/*
458138568Ssam * Set the short slot time state and notify the driver.
459138568Ssam */
460138568Ssamvoid
461138568Ssamieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
462138568Ssam{
463138568Ssam	if (onoff)
464138568Ssam		ic->ic_flags |= IEEE80211_F_SHSLOT;
465138568Ssam	else
466138568Ssam		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
467138568Ssam	/* notify driver */
468138568Ssam	if (ic->ic_updateslot != NULL)
469138568Ssam		ic->ic_updateslot(ic->ic_ifp);
470138568Ssam}
471138568Ssam
472138568Ssam/*
473138568Ssam * Check if the specified rate set supports ERP.
474138568Ssam * NB: the rate set is assumed to be sorted.
475138568Ssam */
476138568Ssamint
477138568Ssamieee80211_iserp_rateset(struct ieee80211com *ic, struct ieee80211_rateset *rs)
478138568Ssam{
479138568Ssam#define N(a)	(sizeof(a) / sizeof(a[0]))
480138568Ssam	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
481138568Ssam	int i, j;
482138568Ssam
483138568Ssam	if (rs->rs_nrates < N(rates))
484138568Ssam		return 0;
485138568Ssam	for (i = 0; i < N(rates); i++) {
486138568Ssam		for (j = 0; j < rs->rs_nrates; j++) {
487138568Ssam			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
488138568Ssam			if (rates[i] == r)
489138568Ssam				goto next;
490138568Ssam			if (r > rates[i])
491138568Ssam				return 0;
492138568Ssam		}
493138568Ssam		return 0;
494138568Ssam	next:
495138568Ssam		;
496138568Ssam	}
497138568Ssam	return 1;
498138568Ssam#undef N
499138568Ssam}
500138568Ssam
501138568Ssam/*
502138568Ssam * Mark the basic rates for the 11g rate table based on the
503138568Ssam * operating mode.  For real 11g we mark all the 11b rates
504138568Ssam * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
505138568Ssam * 11b rates.  There's also a pseudo 11a-mode used to mark only
506138568Ssam * the basic OFDM rates.
507138568Ssam */
508138568Ssamvoid
509138568Ssamieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
510138568Ssam{
511138568Ssam	static const struct ieee80211_rateset basic[] = {
512138568Ssam	    { 0 },			/* IEEE80211_MODE_AUTO */
513138568Ssam	    { 3, { 12, 24, 48 } },	/* IEEE80211_MODE_11A */
514138568Ssam	    { 2, { 2, 4 } },		/* IEEE80211_MODE_11B */
515138568Ssam	    { 4, { 2, 4, 11, 22 } },	/* IEEE80211_MODE_11G (mixed b/g) */
516138568Ssam	    { 0 },			/* IEEE80211_MODE_FH */
517138568Ssam					/* IEEE80211_MODE_PUREG (not yet) */
518138568Ssam	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },
519138568Ssam	};
520138568Ssam	int i, j;
521138568Ssam
522138568Ssam	for (i = 0; i < rs->rs_nrates; i++) {
523138568Ssam		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
524138568Ssam		for (j = 0; j < basic[mode].rs_nrates; j++)
525138568Ssam			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
526138568Ssam				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
527138568Ssam				break;
528138568Ssam			}
529138568Ssam	}
530138568Ssam}
531138568Ssam
532138568Ssam/*
533138568Ssam * WME protocol support.  The following parameters come from the spec.
534138568Ssam */
535138568Ssamtypedef struct phyParamType {
536138568Ssam	u_int8_t aifsn;
537138568Ssam	u_int8_t logcwmin;
538138568Ssam	u_int8_t logcwmax;
539138568Ssam	u_int16_t txopLimit;
540138568Ssam	u_int8_t acm;
541138568Ssam} paramType;
542138568Ssam
543138568Ssamstatic const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
544138568Ssam	{ 3, 4, 6 },		/* IEEE80211_MODE_AUTO */
545138568Ssam	{ 3, 4, 6 },		/* IEEE80211_MODE_11A */
546138568Ssam	{ 3, 5, 7 },		/* IEEE80211_MODE_11B */
547138568Ssam	{ 3, 4, 6 },		/* IEEE80211_MODE_11G */
548138568Ssam	{ 3, 5, 7 },		/* IEEE80211_MODE_FH */
549138568Ssam	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_A */
550138568Ssam	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_G */
551138568Ssam};
552138568Ssamstatic const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
553138568Ssam	{ 7, 4, 10 },		/* IEEE80211_MODE_AUTO */
554138568Ssam	{ 7, 4, 10 },		/* IEEE80211_MODE_11A */
555138568Ssam	{ 7, 5, 10 },		/* IEEE80211_MODE_11B */
556138568Ssam	{ 7, 4, 10 },		/* IEEE80211_MODE_11G */
557138568Ssam	{ 7, 5, 10 },		/* IEEE80211_MODE_FH */
558138568Ssam	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
559138568Ssam	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
560138568Ssam};
561138568Ssamstatic const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
562138568Ssam	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
563138568Ssam	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11A */
564138568Ssam	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_11B */
565138568Ssam	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11G */
566138568Ssam	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_FH */
567138568Ssam	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
568138568Ssam	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
569138568Ssam};
570138568Ssamstatic const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
571138568Ssam	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
572138568Ssam	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11A */
573138568Ssam	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_11B */
574138568Ssam	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11G */
575138568Ssam	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_FH */
576138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
577138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
578138568Ssam};
579138568Ssam
580138568Ssamstatic const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
581138568Ssam	{ 3, 4, 10 },		/* IEEE80211_MODE_AUTO */
582138568Ssam	{ 3, 4, 10 },		/* IEEE80211_MODE_11A */
583138568Ssam	{ 3, 5, 10 },		/* IEEE80211_MODE_11B */
584138568Ssam	{ 3, 4, 10 },		/* IEEE80211_MODE_11G */
585138568Ssam	{ 3, 5, 10 },		/* IEEE80211_MODE_FH */
586138568Ssam	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
587138568Ssam	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
588138568Ssam};
589138568Ssamstatic const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
590138568Ssam	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
591138568Ssam	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11A */
592138568Ssam	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_11B */
593138568Ssam	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11G */
594138568Ssam	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_FH */
595138568Ssam	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
596138568Ssam	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
597138568Ssam};
598138568Ssamstatic const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
599138568Ssam	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
600138568Ssam	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11A */
601138568Ssam	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_11B */
602138568Ssam	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11G */
603138568Ssam	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_FH */
604138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
605138568Ssam	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
606138568Ssam};
607138568Ssam
608138568Ssamvoid
609138568Ssamieee80211_wme_initparams(struct ieee80211com *ic)
610138568Ssam{
611138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
612138568Ssam	const paramType *pPhyParam, *pBssPhyParam;
613138568Ssam	struct wmeParams *wmep;
614138568Ssam	int i;
615138568Ssam
616138568Ssam	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
617138568Ssam		return;
618138568Ssam
619138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
620138568Ssam		switch (i) {
621138568Ssam		case WME_AC_BK:
622138568Ssam			pPhyParam = &phyParamForAC_BK[ic->ic_curmode];
623138568Ssam			pBssPhyParam = &phyParamForAC_BK[ic->ic_curmode];
624138568Ssam			break;
625138568Ssam		case WME_AC_VI:
626138568Ssam			pPhyParam = &phyParamForAC_VI[ic->ic_curmode];
627138568Ssam			pBssPhyParam = &bssPhyParamForAC_VI[ic->ic_curmode];
628138568Ssam			break;
629138568Ssam		case WME_AC_VO:
630138568Ssam			pPhyParam = &phyParamForAC_VO[ic->ic_curmode];
631138568Ssam			pBssPhyParam = &bssPhyParamForAC_VO[ic->ic_curmode];
632138568Ssam			break;
633138568Ssam		case WME_AC_BE:
634138568Ssam		default:
635138568Ssam			pPhyParam = &phyParamForAC_BE[ic->ic_curmode];
636138568Ssam			pBssPhyParam = &bssPhyParamForAC_BE[ic->ic_curmode];
637138568Ssam			break;
638138568Ssam		}
639138568Ssam
640138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
641138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
642138568Ssam			wmep->wmep_acm = pPhyParam->acm;
643138568Ssam			wmep->wmep_aifsn = pPhyParam->aifsn;
644138568Ssam			wmep->wmep_logcwmin = pPhyParam->logcwmin;
645138568Ssam			wmep->wmep_logcwmax = pPhyParam->logcwmax;
646138568Ssam			wmep->wmep_txopLimit = pPhyParam->txopLimit;
647138568Ssam		} else {
648138568Ssam			wmep->wmep_acm = pBssPhyParam->acm;
649138568Ssam			wmep->wmep_aifsn = pBssPhyParam->aifsn;
650138568Ssam			wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
651138568Ssam			wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
652138568Ssam			wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
653138568Ssam
654138568Ssam		}
655138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
656138568Ssam			"%s: %s chan [acm %u aifsn %u log2(cwmin) %u "
657138568Ssam			"log2(cwmax) %u txpoLimit %u]\n", __func__
658138568Ssam			, ieee80211_wme_acnames[i]
659138568Ssam			, wmep->wmep_acm
660138568Ssam			, wmep->wmep_aifsn
661138568Ssam			, wmep->wmep_logcwmin
662138568Ssam			, wmep->wmep_logcwmax
663138568Ssam			, wmep->wmep_txopLimit
664138568Ssam		);
665138568Ssam
666138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
667138568Ssam		wmep->wmep_acm = pBssPhyParam->acm;
668138568Ssam		wmep->wmep_aifsn = pBssPhyParam->aifsn;
669138568Ssam		wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
670138568Ssam		wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
671138568Ssam		wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
672138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
673138568Ssam			"%s: %s  bss [acm %u aifsn %u log2(cwmin) %u "
674138568Ssam			"log2(cwmax) %u txpoLimit %u]\n", __func__
675138568Ssam			, ieee80211_wme_acnames[i]
676138568Ssam			, wmep->wmep_acm
677138568Ssam			, wmep->wmep_aifsn
678138568Ssam			, wmep->wmep_logcwmin
679138568Ssam			, wmep->wmep_logcwmax
680138568Ssam			, wmep->wmep_txopLimit
681138568Ssam		);
682138568Ssam	}
683138568Ssam	/* NB: check ic_bss to avoid NULL deref on initial attach */
684138568Ssam	if (ic->ic_bss != NULL) {
685138568Ssam		/*
686138568Ssam		 * Calculate agressive mode switching threshold based
687138568Ssam		 * on beacon interval.  This doesn't need locking since
688138568Ssam		 * we're only called before entering the RUN state at
689138568Ssam		 * which point we start sending beacon frames.
690138568Ssam		 */
691138568Ssam		wme->wme_hipri_switch_thresh =
692138568Ssam			(HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100;
693138568Ssam		ieee80211_wme_updateparams(ic);
694138568Ssam	}
695138568Ssam}
696138568Ssam
697138568Ssam/*
698138568Ssam * Update WME parameters for ourself and the BSS.
699138568Ssam */
700138568Ssamvoid
701138568Ssamieee80211_wme_updateparams_locked(struct ieee80211com *ic)
702138568Ssam{
703138568Ssam	static const paramType phyParam[IEEE80211_MODE_MAX] = {
704138568Ssam		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_AUTO */
705138568Ssam		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11A */
706138568Ssam		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_11B */
707138568Ssam		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11G */
708138568Ssam		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_FH */
709138568Ssam		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_A */
710138568Ssam		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_G */
711138568Ssam	};
712138568Ssam	struct ieee80211_wme_state *wme = &ic->ic_wme;
713138568Ssam	const struct wmeParams *wmep;
714138568Ssam	struct wmeParams *chanp, *bssp;
715138568Ssam	int i;
716138568Ssam
717138568Ssam       	/* set up the channel access parameters for the physical device */
718138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
719138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[i];
720138568Ssam		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
721138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
722138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
723138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
724138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
725138568Ssam
726138568Ssam		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
727138568Ssam		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
728138568Ssam		chanp->wmep_aifsn = wmep->wmep_aifsn;
729138568Ssam		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
730138568Ssam		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
731138568Ssam		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
732138568Ssam	}
733138568Ssam
734138568Ssam	/*
735138568Ssam	 * This implements agressive mode as found in certain
736138568Ssam	 * vendors' AP's.  When there is significant high
737138568Ssam	 * priority (VI/VO) traffic in the BSS throttle back BE
738138568Ssam	 * traffic by using conservative parameters.  Otherwise
739138568Ssam	 * BE uses agressive params to optimize performance of
740138568Ssam	 * legacy/non-QoS traffic.
741138568Ssam	 */
742138568Ssam        if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
743156524Ssam	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
744153974Ssam	    (ic->ic_opmode == IEEE80211_M_STA &&
745138568Ssam	     (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
746138568Ssam	    (ic->ic_flags & IEEE80211_F_WME) == 0) {
747138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
748138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
749138568Ssam
750138568Ssam		chanp->wmep_aifsn = bssp->wmep_aifsn =
751138568Ssam			phyParam[ic->ic_curmode].aifsn;
752138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
753138568Ssam			phyParam[ic->ic_curmode].logcwmin;
754138568Ssam		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
755138568Ssam			phyParam[ic->ic_curmode].logcwmax;
756138568Ssam		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
757153421Ssam			(ic->ic_flags & IEEE80211_F_BURST) ?
758138568Ssam				phyParam[ic->ic_curmode].txopLimit : 0;
759138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
760138568Ssam			"%s: %s [acm %u aifsn %u log2(cwmin) %u "
761138568Ssam			"log2(cwmax) %u txpoLimit %u]\n", __func__
762138568Ssam			, ieee80211_wme_acnames[WME_AC_BE]
763138568Ssam			, chanp->wmep_acm
764138568Ssam			, chanp->wmep_aifsn
765138568Ssam			, chanp->wmep_logcwmin
766138568Ssam			, chanp->wmep_logcwmax
767138568Ssam			, chanp->wmep_txopLimit
768138568Ssam		);
769138568Ssam	}
770138568Ssam
771138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
772156524Ssam	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
773138568Ssam        	static const u_int8_t logCwMin[IEEE80211_MODE_MAX] = {
774138568Ssam              		3,	/* IEEE80211_MODE_AUTO */
775138568Ssam              		3,	/* IEEE80211_MODE_11A */
776138568Ssam              		4,	/* IEEE80211_MODE_11B */
777138568Ssam              		3,	/* IEEE80211_MODE_11G */
778138568Ssam              		4,	/* IEEE80211_MODE_FH */
779138568Ssam              		3,	/* IEEE80211_MODE_TURBO_A */
780138568Ssam              		3,	/* IEEE80211_MODE_TURBO_G */
781138568Ssam		};
782138568Ssam		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
783138568Ssam		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
784138568Ssam
785138568Ssam		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
786138568Ssam			logCwMin[ic->ic_curmode];
787138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
788138568Ssam			"%s: %s log2(cwmin) %u\n", __func__
789138568Ssam			, ieee80211_wme_acnames[WME_AC_BE]
790138568Ssam			, chanp->wmep_logcwmin
791138568Ssam		);
792138568Ssam    	}
793138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
794138568Ssam		/*
795138568Ssam		 * Arrange for a beacon update and bump the parameter
796138568Ssam		 * set number so associated stations load the new values.
797138568Ssam		 */
798138568Ssam		wme->wme_bssChanParams.cap_info =
799138568Ssam			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
800138568Ssam		ic->ic_flags |= IEEE80211_F_WMEUPDATE;
801138568Ssam	}
802138568Ssam
803138568Ssam	wme->wme_update(ic);
804138568Ssam
805138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
806138568Ssam		"%s: WME params updated, cap_info 0x%x\n", __func__,
807138568Ssam		ic->ic_opmode == IEEE80211_M_STA ?
808138568Ssam			wme->wme_wmeChanParams.cap_info :
809138568Ssam			wme->wme_bssChanParams.cap_info);
810138568Ssam}
811138568Ssam
812138568Ssamvoid
813138568Ssamieee80211_wme_updateparams(struct ieee80211com *ic)
814138568Ssam{
815138568Ssam
816138568Ssam	if (ic->ic_caps & IEEE80211_C_WME) {
817138568Ssam		IEEE80211_BEACON_LOCK(ic);
818138568Ssam		ieee80211_wme_updateparams_locked(ic);
819138568Ssam		IEEE80211_BEACON_UNLOCK(ic);
820138568Ssam	}
821138568Ssam}
822138568Ssam
823153349Ssamvoid
824153349Ssamieee80211_beacon_miss(struct ieee80211com *ic)
825153349Ssam{
826153349Ssam
827153349Ssam	if (ic->ic_flags & IEEE80211_F_SCAN) {
828153349Ssam		/* XXX check ic_curchan != ic_bsschan? */
829153349Ssam		return;
830153349Ssam	}
831153349Ssam	IEEE80211_DPRINTF(ic,
832153349Ssam		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
833153349Ssam		"%s\n", "beacon miss");
834153349Ssam
835153349Ssam	/*
836153349Ssam	 * Our handling is only meaningful for stations that are
837153349Ssam	 * associated; any other conditions else will be handled
838153349Ssam	 * through different means (e.g. the tx timeout on mgt frames).
839153349Ssam	 */
840153349Ssam	if (ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
841153349Ssam		return;
842153349Ssam
843153349Ssam	if (++ic->ic_bmiss_count < ic->ic_bmiss_max) {
844153349Ssam		/*
845153349Ssam		 * Send a directed probe req before falling back to a scan;
846153349Ssam		 * if we receive a response ic_bmiss_count will be reset.
847153349Ssam		 * Some cards mistakenly report beacon miss so this avoids
848153349Ssam		 * the expensive scan if the ap is still there.
849153349Ssam		 */
850153349Ssam		ieee80211_send_probereq(ic->ic_bss, ic->ic_myaddr,
851153349Ssam			ic->ic_bss->ni_bssid, ic->ic_bss->ni_bssid,
852153349Ssam			ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen,
853153349Ssam			ic->ic_opt_ie, ic->ic_opt_ie_len);
854153349Ssam		return;
855153349Ssam	}
856153349Ssam	ic->ic_bmiss_count = 0;
857153349Ssam	ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
858153349Ssam}
859153349Ssam
860154736Ssam/*
861154736Ssam * Software beacon miss handling.  Check if any beacons
862154736Ssam * were received in the last period.  If not post a
863154736Ssam * beacon miss; otherwise reset the counter.
864154736Ssam */
865147765Ssamstatic void
866154736Ssamieee80211_swbmiss(void *arg)
867154736Ssam{
868154736Ssam	struct ieee80211com *ic = arg;
869154736Ssam
870154736Ssam	if (ic->ic_swbmiss_count == 0) {
871154736Ssam		ieee80211_beacon_miss(ic);
872154736Ssam		if (ic->ic_bmiss_count == 0)	/* don't re-arm timer */
873154736Ssam			return;
874154736Ssam	} else
875154736Ssam		ic->ic_swbmiss_count = 0;
876154736Ssam	callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
877154736Ssam		ieee80211_swbmiss, ic);
878154736Ssam}
879154736Ssam
880154736Ssamstatic void
881147765Ssamsta_disassoc(void *arg, struct ieee80211_node *ni)
882147765Ssam{
883147765Ssam	struct ieee80211com *ic = arg;
884147765Ssam
885147765Ssam	if (ni->ni_associd != 0) {
886147765Ssam		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
887147765Ssam			IEEE80211_REASON_ASSOC_LEAVE);
888147765Ssam		ieee80211_node_leave(ic, ni);
889147765Ssam	}
890147765Ssam}
891147765Ssam
892147765Ssamstatic void
893147765Ssamsta_deauth(void *arg, struct ieee80211_node *ni)
894147765Ssam{
895147765Ssam	struct ieee80211com *ic = arg;
896147765Ssam
897147765Ssam	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
898147765Ssam		IEEE80211_REASON_ASSOC_LEAVE);
899147765Ssam}
900147765Ssam
901117811Ssamstatic int
902138568Ssamieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
903116742Ssam{
904138568Ssam	struct ifnet *ifp = ic->ic_ifp;
905116742Ssam	struct ieee80211_node *ni;
906117811Ssam	enum ieee80211_state ostate;
907116742Ssam
908116742Ssam	ostate = ic->ic_state;
909138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
910138568Ssam		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
911117811Ssam	ic->ic_state = nstate;			/* state transition */
912116742Ssam	ni = ic->ic_bss;			/* NB: no reference held */
913154736Ssam	if (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)
914154736Ssam		callout_stop(&ic->ic_swbmiss);
915116742Ssam	switch (nstate) {
916116742Ssam	case IEEE80211_S_INIT:
917116742Ssam		switch (ostate) {
918116742Ssam		case IEEE80211_S_INIT:
919116742Ssam			break;
920116742Ssam		case IEEE80211_S_RUN:
921116742Ssam			switch (ic->ic_opmode) {
922116742Ssam			case IEEE80211_M_STA:
923116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
924116742Ssam				    IEEE80211_FC0_SUBTYPE_DISASSOC,
925116742Ssam				    IEEE80211_REASON_ASSOC_LEAVE);
926138568Ssam				ieee80211_sta_leave(ic, ni);
927116742Ssam				break;
928116742Ssam			case IEEE80211_M_HOSTAP:
929147765Ssam				ieee80211_iterate_nodes(&ic->ic_sta,
930147765Ssam					sta_disassoc, ic);
931116742Ssam				break;
932116742Ssam			default:
933116742Ssam				break;
934116742Ssam			}
935138568Ssam			goto reset;
936116742Ssam		case IEEE80211_S_ASSOC:
937116742Ssam			switch (ic->ic_opmode) {
938116742Ssam			case IEEE80211_M_STA:
939116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
940116742Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
941116742Ssam				    IEEE80211_REASON_AUTH_LEAVE);
942116742Ssam				break;
943116742Ssam			case IEEE80211_M_HOSTAP:
944147765Ssam				ieee80211_iterate_nodes(&ic->ic_sta,
945147765Ssam					sta_deauth, ic);
946116742Ssam				break;
947116742Ssam			default:
948116742Ssam				break;
949116742Ssam			}
950138568Ssam			goto reset;
951140441Ssam		case IEEE80211_S_SCAN:
952140441Ssam			ieee80211_cancel_scan(ic);
953140441Ssam			goto reset;
954116742Ssam		case IEEE80211_S_AUTH:
955138568Ssam		reset:
956116742Ssam			ic->ic_mgt_timer = 0;
957116742Ssam			IF_DRAIN(&ic->ic_mgtq);
958138568Ssam			ieee80211_reset_bss(ic);
959116742Ssam			break;
960116742Ssam		}
961138568Ssam		if (ic->ic_auth->ia_detach != NULL)
962138568Ssam			ic->ic_auth->ia_detach(ic);
963116742Ssam		break;
964116742Ssam	case IEEE80211_S_SCAN:
965116742Ssam		switch (ostate) {
966116742Ssam		case IEEE80211_S_INIT:
967138568Ssam			if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
968138568Ssam			     ic->ic_opmode == IEEE80211_M_IBSS ||
969138568Ssam			     ic->ic_opmode == IEEE80211_M_AHDEMO) &&
970116742Ssam			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
971116742Ssam				/*
972116742Ssam				 * AP operation and we already have a channel;
973116742Ssam				 * bypass the scan and startup immediately.
974116742Ssam				 */
975116742Ssam				ieee80211_create_ibss(ic, ic->ic_des_chan);
976116742Ssam			} else {
977138568Ssam				ieee80211_begin_scan(ic, arg);
978116742Ssam			}
979116742Ssam			break;
980116742Ssam		case IEEE80211_S_SCAN:
981138568Ssam			/*
982156358Ssam			 * Scan next. If doing an active scan probe
983156358Ssam			 * for the requested ap (if any).
984138568Ssam			 */
985156358Ssam			if (ic->ic_flags & IEEE80211_F_ASCAN)
986156358Ssam				ieee80211_probe_curchan(ic, 0);
987116742Ssam			break;
988116742Ssam		case IEEE80211_S_RUN:
989116742Ssam			/* beacon miss */
990138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE,
991138568Ssam				"no recent beacons from %s; rescanning\n",
992138568Ssam				ether_sprintf(ic->ic_bss->ni_bssid));
993138568Ssam			ieee80211_sta_leave(ic, ni);
994138568Ssam			ic->ic_flags &= ~IEEE80211_F_SIBSS;	/* XXX */
995116742Ssam			/* FALLTHRU */
996116742Ssam		case IEEE80211_S_AUTH:
997116742Ssam		case IEEE80211_S_ASSOC:
998116742Ssam			/* timeout restart scan */
999138568Ssam			ni = ieee80211_find_node(&ic->ic_scan,
1000138568Ssam				ic->ic_bss->ni_macaddr);
1001116742Ssam			if (ni != NULL) {
1002116742Ssam				ni->ni_fails++;
1003116742Ssam				ieee80211_unref_node(&ni);
1004116742Ssam			}
1005147116Ssam			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
1006147116Ssam				ieee80211_begin_scan(ic, arg);
1007116742Ssam			break;
1008116742Ssam		}
1009116742Ssam		break;
1010116742Ssam	case IEEE80211_S_AUTH:
1011116742Ssam		switch (ostate) {
1012116742Ssam		case IEEE80211_S_INIT:
1013116742Ssam		case IEEE80211_S_SCAN:
1014116742Ssam			IEEE80211_SEND_MGMT(ic, ni,
1015116742Ssam			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1016116742Ssam			break;
1017116742Ssam		case IEEE80211_S_AUTH:
1018116742Ssam		case IEEE80211_S_ASSOC:
1019138568Ssam			switch (arg) {
1020116742Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
1021116742Ssam				/* ??? */
1022116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
1023116742Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1024116742Ssam				break;
1025116742Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1026116742Ssam				/* ignore and retry scan on timeout */
1027116742Ssam				break;
1028116742Ssam			}
1029116742Ssam			break;
1030116742Ssam		case IEEE80211_S_RUN:
1031138568Ssam			switch (arg) {
1032116742Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
1033116742Ssam				IEEE80211_SEND_MGMT(ic, ni,
1034116742Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1035116742Ssam				ic->ic_state = ostate;	/* stay RUN */
1036116742Ssam				break;
1037116742Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1038138568Ssam				ieee80211_sta_leave(ic, ni);
1039147116Ssam				if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1040147116Ssam					/* try to reauth */
1041147116Ssam					IEEE80211_SEND_MGMT(ic, ni,
1042147116Ssam					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1043147116Ssam				}
1044116742Ssam				break;
1045116742Ssam			}
1046116742Ssam			break;
1047116742Ssam		}
1048116742Ssam		break;
1049116742Ssam	case IEEE80211_S_ASSOC:
1050116742Ssam		switch (ostate) {
1051116742Ssam		case IEEE80211_S_INIT:
1052116742Ssam		case IEEE80211_S_SCAN:
1053116742Ssam		case IEEE80211_S_ASSOC:
1054138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1055138568Ssam				"%s: invalid transition\n", __func__);
1056116742Ssam			break;
1057116742Ssam		case IEEE80211_S_AUTH:
1058116742Ssam			IEEE80211_SEND_MGMT(ic, ni,
1059116742Ssam			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1060116742Ssam			break;
1061116742Ssam		case IEEE80211_S_RUN:
1062138568Ssam			ieee80211_sta_leave(ic, ni);
1063147116Ssam			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1064147116Ssam				IEEE80211_SEND_MGMT(ic, ni,
1065147116Ssam				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
1066147116Ssam			}
1067116742Ssam			break;
1068116742Ssam		}
1069116742Ssam		break;
1070116742Ssam	case IEEE80211_S_RUN:
1071138568Ssam		if (ic->ic_flags & IEEE80211_F_WPA) {
1072138568Ssam			/* XXX validate prerequisites */
1073138568Ssam		}
1074116742Ssam		switch (ostate) {
1075116742Ssam		case IEEE80211_S_INIT:
1076138568Ssam			if (ic->ic_opmode == IEEE80211_M_MONITOR)
1077138568Ssam				break;
1078138568Ssam			/* fall thru... */
1079116742Ssam		case IEEE80211_S_AUTH:
1080138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1081138568Ssam				"%s: invalid transition\n", __func__);
1082140763Ssam			/* fall thru... */
1083140763Ssam		case IEEE80211_S_RUN:
1084116742Ssam			break;
1085116742Ssam		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
1086116742Ssam		case IEEE80211_S_ASSOC:		/* infra mode */
1087116742Ssam			KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
1088116742Ssam				("%s: bogus xmit rate %u setup\n", __func__,
1089116742Ssam					ni->ni_txrate));
1090138568Ssam#ifdef IEEE80211_DEBUG
1091138568Ssam			if (ieee80211_msg_debug(ic)) {
1092116742Ssam				if (ic->ic_opmode == IEEE80211_M_STA)
1093138568Ssam					if_printf(ifp, "associated ");
1094116742Ssam				else
1095138568Ssam					if_printf(ifp, "synchronized ");
1096116742Ssam				printf("with %s ssid ",
1097116742Ssam				    ether_sprintf(ni->ni_bssid));
1098116742Ssam				ieee80211_print_essid(ic->ic_bss->ni_essid,
1099116742Ssam				    ni->ni_esslen);
1100116742Ssam				printf(" channel %d start %uMb\n",
1101148936Ssam					ieee80211_chan2ieee(ic, ic->ic_curchan),
1102116742Ssam					IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
1103116742Ssam			}
1104138568Ssam#endif
1105116742Ssam			ic->ic_mgt_timer = 0;
1106138568Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1107138568Ssam				ieee80211_notify_node_join(ic, ni,
1108138568Ssam					arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1109138568Ssam			if_start(ifp);		/* XXX not authorized yet */
1110116742Ssam			break;
1111116742Ssam		}
1112154736Ssam		if (ostate != IEEE80211_S_RUN &&
1113154736Ssam		    ic->ic_opmode == IEEE80211_M_STA &&
1114154736Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)) {
1115154736Ssam			/*
1116154736Ssam			 * Start s/w beacon miss timer for devices w/o
1117154736Ssam			 * hardware support.  We fudge a bit here since
1118154736Ssam			 * we're doing this in software.
1119154736Ssam			 */
1120154736Ssam			ic->ic_swbmiss_period = IEEE80211_TU_TO_TICKS(
1121154736Ssam				2 * ic->ic_bmissthreshold * ni->ni_intval);
1122154736Ssam			ic->ic_swbmiss_count = 0;
1123154736Ssam			callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
1124154736Ssam				ieee80211_swbmiss, ic);
1125154736Ssam		}
1126138568Ssam		/*
1127138568Ssam		 * Start/stop the authenticator when operating as an
1128138568Ssam		 * AP.  We delay until here to allow configuration to
1129138568Ssam		 * happen out of order.
1130138568Ssam		 */
1131138568Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */
1132138568Ssam		    ic->ic_auth->ia_attach != NULL) {
1133138568Ssam			/* XXX check failure */
1134138568Ssam			ic->ic_auth->ia_attach(ic);
1135138568Ssam		} else if (ic->ic_auth->ia_detach != NULL) {
1136138568Ssam			ic->ic_auth->ia_detach(ic);
1137138568Ssam		}
1138138568Ssam		/*
1139138568Ssam		 * When 802.1x is not in use mark the port authorized
1140138568Ssam		 * at this point so traffic can flow.
1141138568Ssam		 */
1142138568Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
1143148302Ssam			ieee80211_node_authorize(ni);
1144138568Ssam		/*
1145138568Ssam		 * Enable inactivity processing.
1146138568Ssam		 * XXX
1147138568Ssam		 */
1148138568Ssam		ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT;
1149140753Ssam		ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT;
1150116742Ssam		break;
1151116742Ssam	}
1152116742Ssam	return 0;
1153116742Ssam}
1154