xref: /titanic_41/usr/src/uts/common/io/net80211/net80211_proto.c (revision e2cf88ac9d753a00c17aa235f6afdc76574fe3a6)
10ba2cbe9Sxc151355 /*
219d332feSfei feng - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30ba2cbe9Sxc151355  * Use is subject to license terms.
40ba2cbe9Sxc151355  */
50ba2cbe9Sxc151355 
60ba2cbe9Sxc151355 /*
70ba2cbe9Sxc151355  * Copyright (c) 2001 Atsushi Onoe
819d332feSfei feng - Sun Microsystems - Beijing China  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
90ba2cbe9Sxc151355  * All rights reserved.
100ba2cbe9Sxc151355  *
110ba2cbe9Sxc151355  * Redistribution and use in source and binary forms, with or without
120ba2cbe9Sxc151355  * modification, are permitted provided that the following conditions
130ba2cbe9Sxc151355  * are met:
140ba2cbe9Sxc151355  * 1. Redistributions of source code must retain the above copyright
150ba2cbe9Sxc151355  *    notice, this list of conditions and the following disclaimer.
160ba2cbe9Sxc151355  * 2. Redistributions in binary form must reproduce the above copyright
170ba2cbe9Sxc151355  *    notice, this list of conditions and the following disclaimer in the
180ba2cbe9Sxc151355  *    documentation and/or other materials provided with the distribution.
190ba2cbe9Sxc151355  * 3. The name of the author may not be used to endorse or promote products
200ba2cbe9Sxc151355  *    derived from this software without specific prior written permission.
210ba2cbe9Sxc151355  *
220ba2cbe9Sxc151355  * Alternatively, this software may be distributed under the terms of the
230ba2cbe9Sxc151355  * GNU General Public License ("GPL") version 2 as published by the Free
240ba2cbe9Sxc151355  * Software Foundation.
250ba2cbe9Sxc151355  *
260ba2cbe9Sxc151355  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
270ba2cbe9Sxc151355  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
280ba2cbe9Sxc151355  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
290ba2cbe9Sxc151355  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
300ba2cbe9Sxc151355  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
310ba2cbe9Sxc151355  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
320ba2cbe9Sxc151355  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
330ba2cbe9Sxc151355  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
340ba2cbe9Sxc151355  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
350ba2cbe9Sxc151355  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
360ba2cbe9Sxc151355  */
370ba2cbe9Sxc151355 
380ba2cbe9Sxc151355 /*
390ba2cbe9Sxc151355  * IEEE 802.11 protocol support
400ba2cbe9Sxc151355  */
410ba2cbe9Sxc151355 
420ba2cbe9Sxc151355 #include "net80211_impl.h"
430ba2cbe9Sxc151355 
44*e2cf88acSQuaker Fang /* tunables */
45*e2cf88acSQuaker Fang #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
46*e2cf88acSQuaker Fang #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
47*e2cf88acSQuaker Fang 
48*e2cf88acSQuaker Fang #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
49*e2cf88acSQuaker Fang 
500ba2cbe9Sxc151355 const char *ieee80211_mgt_subtype_name[] = {
510ba2cbe9Sxc151355 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
520ba2cbe9Sxc151355 	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
530ba2cbe9Sxc151355 	"beacon",	"atim",		"disassoc",	"auth",
540ba2cbe9Sxc151355 	"deauth",	"reserved#13",	"reserved#14",	"reserved#15"
550ba2cbe9Sxc151355 };
560ba2cbe9Sxc151355 const char *ieee80211_ctl_subtype_name[] = {
570ba2cbe9Sxc151355 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
580ba2cbe9Sxc151355 	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
590ba2cbe9Sxc151355 	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
600ba2cbe9Sxc151355 	"cts",		"ack",		"cf_end",	"cf_end_ack"
610ba2cbe9Sxc151355 };
620ba2cbe9Sxc151355 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
630ba2cbe9Sxc151355 	"INIT",		/* IEEE80211_S_INIT */
640ba2cbe9Sxc151355 	"SCAN",		/* IEEE80211_S_SCAN */
650ba2cbe9Sxc151355 	"AUTH",		/* IEEE80211_S_AUTH */
660ba2cbe9Sxc151355 	"ASSOC",	/* IEEE80211_S_ASSOC */
670ba2cbe9Sxc151355 	"RUN"		/* IEEE80211_S_RUN */
680ba2cbe9Sxc151355 };
69*e2cf88acSQuaker Fang const char *ieee80211_wme_acnames[] = {
70*e2cf88acSQuaker Fang 	"WME_AC_BE",
71*e2cf88acSQuaker Fang 	"WME_AC_BK",
72*e2cf88acSQuaker Fang 	"WME_AC_VI",
73*e2cf88acSQuaker Fang 	"WME_AC_VO",
74*e2cf88acSQuaker Fang 	"WME_UPSD",
75*e2cf88acSQuaker Fang };
760ba2cbe9Sxc151355 
770ba2cbe9Sxc151355 static int ieee80211_newstate(ieee80211com_t *, enum ieee80211_state, int);
780ba2cbe9Sxc151355 
790ba2cbe9Sxc151355 /*
800ba2cbe9Sxc151355  * Initialize the interface softc, ic, with protocol management
810ba2cbe9Sxc151355  * related data structures and functions.
820ba2cbe9Sxc151355  */
830ba2cbe9Sxc151355 void
ieee80211_proto_attach(ieee80211com_t * ic)840ba2cbe9Sxc151355 ieee80211_proto_attach(ieee80211com_t *ic)
850ba2cbe9Sxc151355 {
860ba2cbe9Sxc151355 	struct ieee80211_impl *im = ic->ic_private;
870ba2cbe9Sxc151355 
880ba2cbe9Sxc151355 	ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
890ba2cbe9Sxc151355 	ic->ic_fragthreshold = IEEE80211_FRAG_DEFAULT;
900ba2cbe9Sxc151355 	ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
910ba2cbe9Sxc151355 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
920ba2cbe9Sxc151355 	im->im_bmiss_max = IEEE80211_BMISS_MAX;
930ba2cbe9Sxc151355 
94*e2cf88acSQuaker Fang 	ic->ic_wme.wme_hipri_switch_hysteresis =
95*e2cf88acSQuaker Fang 	    AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
96*e2cf88acSQuaker Fang 
970ba2cbe9Sxc151355 	/* protocol state change handler */
980ba2cbe9Sxc151355 	ic->ic_newstate = ieee80211_newstate;
990ba2cbe9Sxc151355 
1000ba2cbe9Sxc151355 	/* initialize management frame handlers */
1010ba2cbe9Sxc151355 	ic->ic_recv_mgmt = ieee80211_recv_mgmt;
1020ba2cbe9Sxc151355 	ic->ic_send_mgmt = ieee80211_send_mgmt;
1030ba2cbe9Sxc151355 }
1040ba2cbe9Sxc151355 
1050ba2cbe9Sxc151355 /*
1060ba2cbe9Sxc151355  * Print a 802.11 frame header
1070ba2cbe9Sxc151355  */
1080ba2cbe9Sxc151355 void
ieee80211_dump_pkt(const uint8_t * buf,int32_t len,int32_t rate,int32_t rssi)1090ba2cbe9Sxc151355 ieee80211_dump_pkt(const uint8_t *buf, int32_t len, int32_t rate, int32_t rssi)
1100ba2cbe9Sxc151355 {
1110ba2cbe9Sxc151355 	struct ieee80211_frame *wh;
1120ba2cbe9Sxc151355 	int8_t buf1[100];
1130ba2cbe9Sxc151355 	int8_t buf2[25];
1140ba2cbe9Sxc151355 	int i;
1150ba2cbe9Sxc151355 
1160ba2cbe9Sxc151355 	bzero(buf1, sizeof (buf1));
1170ba2cbe9Sxc151355 	bzero(buf2, sizeof (buf2));
1180ba2cbe9Sxc151355 	wh = (struct ieee80211_frame *)buf;
1190ba2cbe9Sxc151355 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
1200ba2cbe9Sxc151355 	case IEEE80211_FC1_DIR_NODS:
1210ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "NODS %s",
1220ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr2));
1230ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1240ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "->%s",
1250ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr1));
1260ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1270ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "(%s)",
1280ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr3));
1290ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1300ba2cbe9Sxc151355 		break;
1310ba2cbe9Sxc151355 	case IEEE80211_FC1_DIR_TODS:
1320ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "TODS %s",
1330ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr2));
1340ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1350ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "->%s",
1360ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr3));
1370ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1380ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "(%s)",
1390ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr1));
1400ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1410ba2cbe9Sxc151355 		break;
1420ba2cbe9Sxc151355 	case IEEE80211_FC1_DIR_FROMDS:
1430ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "FRDS %s",
1440ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr3));
1450ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1460ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "->%s",
1470ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr1));
1480ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1490ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "(%s)",
1500ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr2));
1510ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1520ba2cbe9Sxc151355 		break;
1530ba2cbe9Sxc151355 	case IEEE80211_FC1_DIR_DSTODS:
1540ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "DSDS %s",
1550ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf((uint8_t *)&wh[1]));
1560ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1570ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "->%s  ",
1580ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr3));
1590ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1600ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "%s",
1610ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr2));
1620ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1630ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "->%s",
1640ba2cbe9Sxc151355 		    ieee80211_macaddr_sprintf(wh->i_addr1));
1650ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1660ba2cbe9Sxc151355 		break;
1670ba2cbe9Sxc151355 	}
1680ba2cbe9Sxc151355 	ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_dump_pkt(): %s", buf1);
1690ba2cbe9Sxc151355 	bzero(buf1, sizeof (buf1));
1700ba2cbe9Sxc151355 
1710ba2cbe9Sxc151355 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1720ba2cbe9Sxc151355 	case IEEE80211_FC0_TYPE_DATA:
1730ba2cbe9Sxc151355 		(void) sprintf(buf2, "data");
1740ba2cbe9Sxc151355 		break;
1750ba2cbe9Sxc151355 	case IEEE80211_FC0_TYPE_MGT:
1760ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "%s",
1770ba2cbe9Sxc151355 		    ieee80211_mgt_subtype_name[
1780ba2cbe9Sxc151355 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1790ba2cbe9Sxc151355 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
1800ba2cbe9Sxc151355 		break;
1810ba2cbe9Sxc151355 	default:
1820ba2cbe9Sxc151355 		(void) snprintf(buf2, sizeof (buf2), "type#%d",
1830ba2cbe9Sxc151355 		    wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
1840ba2cbe9Sxc151355 		break;
1850ba2cbe9Sxc151355 	}
1860ba2cbe9Sxc151355 	(void) strncat(buf1, buf2, sizeof (buf2));
1870ba2cbe9Sxc151355 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1880ba2cbe9Sxc151355 		(void) sprintf(buf2, " WEP");
1890ba2cbe9Sxc151355 		(void) strcat(buf1, buf2);
1900ba2cbe9Sxc151355 	}
1910ba2cbe9Sxc151355 	if (rate >= 0) {
1920ba2cbe9Sxc151355 		(void) snprintf(buf2,  sizeof (buf2), " %dM", rate / 2);
1930ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1940ba2cbe9Sxc151355 	}
1950ba2cbe9Sxc151355 	if (rssi >= 0) {
1960ba2cbe9Sxc151355 		(void) snprintf(buf2,  sizeof (buf2), " +%d", rssi);
1970ba2cbe9Sxc151355 		(void) strncat(buf1, buf2, sizeof (buf2));
1980ba2cbe9Sxc151355 	}
1990ba2cbe9Sxc151355 	ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_dump_pkt(): %s", buf1);
2000ba2cbe9Sxc151355 	bzero(buf1, sizeof (buf1));
2010ba2cbe9Sxc151355 
2020ba2cbe9Sxc151355 	if (len > 0) {
2030ba2cbe9Sxc151355 		for (i = 0; i < (len > 40 ? 40 : len); i++) {
2040ba2cbe9Sxc151355 			if ((i & 0x03) == 0)
2050ba2cbe9Sxc151355 				(void) strcat(buf1, " ");
2060ba2cbe9Sxc151355 			(void) snprintf(buf2, 3, "%02x", buf[i]);
2070ba2cbe9Sxc151355 			(void) strncat(buf1, buf2, 3);
2080ba2cbe9Sxc151355 		}
2090ba2cbe9Sxc151355 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_dump_pkt(): %s",
2100ba2cbe9Sxc151355 		    buf1);
2110ba2cbe9Sxc151355 	}
2120ba2cbe9Sxc151355 }
2130ba2cbe9Sxc151355 
2140ba2cbe9Sxc151355 /*
2150ba2cbe9Sxc151355  * Adjust/Fix the specified node's rate table
2160ba2cbe9Sxc151355  *
2170ba2cbe9Sxc151355  * in   node
2180ba2cbe9Sxc151355  * flag IEEE80211_F_DOSORT : sort the node's rate table
2190ba2cbe9Sxc151355  *      IEEE80211_F_DONEGO : mark a rate as basic rate if it is
2200ba2cbe9Sxc151355  *                           a device's basic rate
2210ba2cbe9Sxc151355  *      IEEE80211_F_DODEL  : delete rates not supported by the device
2220ba2cbe9Sxc151355  *      IEEE80211_F_DOFRATE: check if the fixed rate is supported by
2230ba2cbe9Sxc151355  *                           the device
2240ba2cbe9Sxc151355  *
2250ba2cbe9Sxc151355  * The highest bit of returned rate value is set to 1 on failure.
2260ba2cbe9Sxc151355  */
2270ba2cbe9Sxc151355 int
ieee80211_fix_rate(ieee80211_node_t * in,struct ieee80211_rateset * nrs,int flags)228*e2cf88acSQuaker Fang ieee80211_fix_rate(ieee80211_node_t *in,
229*e2cf88acSQuaker Fang     struct ieee80211_rateset *nrs, int flags)
2300ba2cbe9Sxc151355 {
2310ba2cbe9Sxc151355 	ieee80211com_t *ic = in->in_ic;
2320ba2cbe9Sxc151355 	struct ieee80211_rateset *srs;
2330ba2cbe9Sxc151355 	boolean_t ignore;
2340ba2cbe9Sxc151355 	int i;
2350ba2cbe9Sxc151355 	int okrate;
2360ba2cbe9Sxc151355 	int badrate;
2370ba2cbe9Sxc151355 	int fixedrate;
2380ba2cbe9Sxc151355 	uint8_t r;
2390ba2cbe9Sxc151355 
2400ba2cbe9Sxc151355 	/*
2410ba2cbe9Sxc151355 	 * If the fixed rate check was requested but no
2420ba2cbe9Sxc151355 	 * fixed has been defined then just remove it.
2430ba2cbe9Sxc151355 	 */
2440ba2cbe9Sxc151355 	if ((flags & IEEE80211_F_DOFRATE) &&
2450ba2cbe9Sxc151355 	    (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)) {
2460ba2cbe9Sxc151355 		flags &= ~IEEE80211_F_DOFRATE;
2470ba2cbe9Sxc151355 	}
24840db2e2bSzf162725 	if (in->in_chan == IEEE80211_CHAN_ANYC) {
24940db2e2bSzf162725 		return (IEEE80211_RATE_BASIC);
25040db2e2bSzf162725 	}
2510ba2cbe9Sxc151355 	okrate = badrate = fixedrate = 0;
2520ba2cbe9Sxc151355 	srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, in->in_chan)];
2530ba2cbe9Sxc151355 	for (i = 0; i < nrs->ir_nrates; ) {
2540ba2cbe9Sxc151355 		int j;
2550ba2cbe9Sxc151355 
2560ba2cbe9Sxc151355 		ignore = B_FALSE;
2570ba2cbe9Sxc151355 		if (flags & IEEE80211_F_DOSORT) {
2580ba2cbe9Sxc151355 			/*
2590ba2cbe9Sxc151355 			 * Sort rates.
2600ba2cbe9Sxc151355 			 */
2610ba2cbe9Sxc151355 			for (j = i + 1; j < nrs->ir_nrates; j++) {
2620ba2cbe9Sxc151355 				if (IEEE80211_RV(nrs->ir_rates[i]) >
2630ba2cbe9Sxc151355 				    IEEE80211_RV(nrs->ir_rates[j])) {
2640ba2cbe9Sxc151355 					r = nrs->ir_rates[i];
2650ba2cbe9Sxc151355 					nrs->ir_rates[i] = nrs->ir_rates[j];
2660ba2cbe9Sxc151355 					nrs->ir_rates[j] = r;
2670ba2cbe9Sxc151355 				}
2680ba2cbe9Sxc151355 			}
2690ba2cbe9Sxc151355 		}
2700ba2cbe9Sxc151355 		r = IEEE80211_RV(nrs->ir_rates[i]);
2710ba2cbe9Sxc151355 		badrate = r;
2720ba2cbe9Sxc151355 
2730ba2cbe9Sxc151355 		/*
2740ba2cbe9Sxc151355 		 * Check against supported rates.
2750ba2cbe9Sxc151355 		 */
2760ba2cbe9Sxc151355 		for (j = 0; j < srs->ir_nrates; j++) {
2770ba2cbe9Sxc151355 			if (r == IEEE80211_RV(srs->ir_rates[j])) {
2780ba2cbe9Sxc151355 				/*
2790ba2cbe9Sxc151355 				 * Overwrite with the supported rate
2800ba2cbe9Sxc151355 				 * value so any basic rate bit is set.
2810ba2cbe9Sxc151355 				 * This insures that response we send
2820ba2cbe9Sxc151355 				 * to stations have the necessary basic
2830ba2cbe9Sxc151355 				 * rate bit set.
2840ba2cbe9Sxc151355 				 */
2850ba2cbe9Sxc151355 				if (flags & IEEE80211_F_DONEGO)
2860ba2cbe9Sxc151355 					nrs->ir_rates[i] = srs->ir_rates[j];
2870ba2cbe9Sxc151355 				break;
2880ba2cbe9Sxc151355 			}
2890ba2cbe9Sxc151355 		}
2900ba2cbe9Sxc151355 		if (j == srs->ir_nrates) {
2910ba2cbe9Sxc151355 			/*
2920ba2cbe9Sxc151355 			 * A rate in the node's rate set is not
2930ba2cbe9Sxc151355 			 * supported. We just discard/ignore the rate.
2940ba2cbe9Sxc151355 			 * Note that this is important for 11b stations
2950ba2cbe9Sxc151355 			 * when they want to associate with an 11g AP.
2960ba2cbe9Sxc151355 			 */
2970ba2cbe9Sxc151355 			ignore = B_TRUE;
2980ba2cbe9Sxc151355 		}
2990ba2cbe9Sxc151355 
3000ba2cbe9Sxc151355 		if (flags & IEEE80211_F_DODEL) {
3010ba2cbe9Sxc151355 			/*
3020ba2cbe9Sxc151355 			 * Delete unacceptable rates.
3030ba2cbe9Sxc151355 			 */
3040ba2cbe9Sxc151355 			if (ignore) {
3050ba2cbe9Sxc151355 				nrs->ir_nrates--;
3060ba2cbe9Sxc151355 				for (j = i; j < nrs->ir_nrates; j++)
3070ba2cbe9Sxc151355 					nrs->ir_rates[j] = nrs->ir_rates[j + 1];
3080ba2cbe9Sxc151355 				nrs->ir_rates[j] = 0;
3090ba2cbe9Sxc151355 				continue;
3100ba2cbe9Sxc151355 			}
3110ba2cbe9Sxc151355 		}
3120ba2cbe9Sxc151355 		if (flags & IEEE80211_F_DOFRATE) {
3130ba2cbe9Sxc151355 			/*
3140ba2cbe9Sxc151355 			 * Check any fixed rate is included.
3150ba2cbe9Sxc151355 			 */
3160ba2cbe9Sxc151355 			if (r == ic->ic_fixed_rate)
3170ba2cbe9Sxc151355 				fixedrate = r;
3180ba2cbe9Sxc151355 		}
3190ba2cbe9Sxc151355 		if (!ignore)
3200ba2cbe9Sxc151355 			okrate = nrs->ir_rates[i];
3210ba2cbe9Sxc151355 		i++;
3220ba2cbe9Sxc151355 	}
3230ba2cbe9Sxc151355 	if (okrate == 0 || ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
3240ba2cbe9Sxc151355 		return (badrate | IEEE80211_RATE_BASIC);
3250ba2cbe9Sxc151355 	else
3260ba2cbe9Sxc151355 		return (IEEE80211_RV(okrate));
3270ba2cbe9Sxc151355 }
3280ba2cbe9Sxc151355 
3290ba2cbe9Sxc151355 /*
3300ba2cbe9Sxc151355  * Reset 11g-related state.
3310ba2cbe9Sxc151355  */
3320ba2cbe9Sxc151355 void
ieee80211_reset_erp(ieee80211com_t * ic)3330ba2cbe9Sxc151355 ieee80211_reset_erp(ieee80211com_t *ic)
3340ba2cbe9Sxc151355 {
3350ba2cbe9Sxc151355 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
3360ba2cbe9Sxc151355 	/*
3370ba2cbe9Sxc151355 	 * Short slot time is enabled only when operating in 11g
3380ba2cbe9Sxc151355 	 * and not in an IBSS.  We must also honor whether or not
3390ba2cbe9Sxc151355 	 * the driver is capable of doing it.
3400ba2cbe9Sxc151355 	 */
3410ba2cbe9Sxc151355 	ieee80211_set_shortslottime(ic,
3420ba2cbe9Sxc151355 	    ic->ic_curmode == IEEE80211_MODE_11A);
3430ba2cbe9Sxc151355 	/*
3440ba2cbe9Sxc151355 	 * Set short preamble and ERP barker-preamble flags.
3450ba2cbe9Sxc151355 	 */
3460ba2cbe9Sxc151355 	if (ic->ic_curmode == IEEE80211_MODE_11A ||
3470ba2cbe9Sxc151355 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
3480ba2cbe9Sxc151355 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
3490ba2cbe9Sxc151355 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
3500ba2cbe9Sxc151355 	} else {
3510ba2cbe9Sxc151355 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
3520ba2cbe9Sxc151355 		ic->ic_flags |= IEEE80211_F_USEBARKER;
3530ba2cbe9Sxc151355 	}
3540ba2cbe9Sxc151355 }
3550ba2cbe9Sxc151355 
3560ba2cbe9Sxc151355 /*
3570ba2cbe9Sxc151355  * Change current channel to be the next available channel
3580ba2cbe9Sxc151355  */
3590ba2cbe9Sxc151355 void
ieee80211_reset_chan(ieee80211com_t * ic)3600ba2cbe9Sxc151355 ieee80211_reset_chan(ieee80211com_t *ic)
3610ba2cbe9Sxc151355 {
3620ba2cbe9Sxc151355 	struct ieee80211_channel *ch = ic->ic_curchan;
3630ba2cbe9Sxc151355 
3640ba2cbe9Sxc151355 	IEEE80211_LOCK(ic);
3650ba2cbe9Sxc151355 	do {
3660ba2cbe9Sxc151355 		if (++ch > &ic->ic_sup_channels[IEEE80211_CHAN_MAX])
3670ba2cbe9Sxc151355 			ch = &ic->ic_sup_channels[0];
3680ba2cbe9Sxc151355 		if (ieee80211_isset(ic->ic_chan_active,
3690ba2cbe9Sxc151355 		    ieee80211_chan2ieee(ic, ch))) {
3700ba2cbe9Sxc151355 			break;
3710ba2cbe9Sxc151355 		}
3720ba2cbe9Sxc151355 	} while (ch != ic->ic_curchan);
3730ba2cbe9Sxc151355 	ic->ic_curchan = ch;
3740ba2cbe9Sxc151355 	IEEE80211_UNLOCK(ic);
3750ba2cbe9Sxc151355 }
3760ba2cbe9Sxc151355 
3770ba2cbe9Sxc151355 /*
3780ba2cbe9Sxc151355  * Set the short slot time state and notify the driver.
3790ba2cbe9Sxc151355  */
3800ba2cbe9Sxc151355 void
ieee80211_set_shortslottime(ieee80211com_t * ic,boolean_t on)3810ba2cbe9Sxc151355 ieee80211_set_shortslottime(ieee80211com_t *ic, boolean_t on)
3820ba2cbe9Sxc151355 {
3830ba2cbe9Sxc151355 	if (on)
3840ba2cbe9Sxc151355 		ic->ic_flags |= IEEE80211_F_SHSLOT;
3850ba2cbe9Sxc151355 	else
3860ba2cbe9Sxc151355 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
3870ba2cbe9Sxc151355 	/* notify driver */
3880ba2cbe9Sxc151355 	if (ic->ic_set_shortslot != NULL)
3890ba2cbe9Sxc151355 		ic->ic_set_shortslot(ic, on);
3900ba2cbe9Sxc151355 }
3910ba2cbe9Sxc151355 
3920ba2cbe9Sxc151355 /*
3930ba2cbe9Sxc151355  * Mark the basic rates for the 11g rate table based on the
3940ba2cbe9Sxc151355  * operating mode.  For real 11g we mark all the 11b rates
3950ba2cbe9Sxc151355  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
3960ba2cbe9Sxc151355  * 11b rates.  There's also a pseudo 11a-mode used to mark only
3970ba2cbe9Sxc151355  * the basic OFDM rates.
3980ba2cbe9Sxc151355  */
3990ba2cbe9Sxc151355 void
ieee80211_setbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode)4000ba2cbe9Sxc151355 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
4010ba2cbe9Sxc151355     enum ieee80211_phymode mode)
4020ba2cbe9Sxc151355 {
4030ba2cbe9Sxc151355 	static const struct ieee80211_rateset basic[] = {
4040ba2cbe9Sxc151355 		{ 0 },			/* IEEE80211_MODE_AUTO */
4050ba2cbe9Sxc151355 		{ 3, { 12, 24, 48 } },	/* IEEE80211_MODE_11A */
4060ba2cbe9Sxc151355 		{ 2, { 2, 4} },		/* IEEE80211_MODE_11B */
4070ba2cbe9Sxc151355 		{ 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G mixed b/g */
4080ba2cbe9Sxc151355 		{ 0 },			/* IEEE80211_MODE_FH */
4090ba2cbe9Sxc151355 		{ 3, { 12, 24, 48 } },	/* IEEE80211_MODE_TURBO_A */
410*e2cf88acSQuaker Fang 		{ 4, { 2, 4, 11, 22 } },
411*e2cf88acSQuaker Fang 					/* IEEE80211_MODE_TURBO_G (mixed b/g) */
412*e2cf88acSQuaker Fang 		{ 0 },			/* IEEE80211_MODE_STURBO_A */
413*e2cf88acSQuaker Fang 		{ 3, { 12, 24, 48 } },	/* IEEE80211_MODE_11NA */
414*e2cf88acSQuaker Fang 					/* IEEE80211_MODE_11NG (mixed b/g) */
415*e2cf88acSQuaker Fang 		{ 7, { 2, 4, 11, 22, 12, 24, 48 } }
4160ba2cbe9Sxc151355 	};
4170ba2cbe9Sxc151355 	int i, j;
4180ba2cbe9Sxc151355 
4190ba2cbe9Sxc151355 	ASSERT(mode < IEEE80211_MODE_MAX);
4200ba2cbe9Sxc151355 	for (i = 0; i < rs->ir_nrates; i++) {
4210ba2cbe9Sxc151355 		rs->ir_rates[i] &= IEEE80211_RATE_VAL;
4220ba2cbe9Sxc151355 		for (j = 0; j < basic[mode].ir_nrates; j++) {
4230ba2cbe9Sxc151355 			if (basic[mode].ir_rates[j] == rs->ir_rates[i]) {
4240ba2cbe9Sxc151355 				rs->ir_rates[i] |= IEEE80211_RATE_BASIC;
4250ba2cbe9Sxc151355 				break;
4260ba2cbe9Sxc151355 			}
4270ba2cbe9Sxc151355 		}
4280ba2cbe9Sxc151355 	}
4290ba2cbe9Sxc151355 }
4300ba2cbe9Sxc151355 
4310ba2cbe9Sxc151355 /*
432*e2cf88acSQuaker Fang  * WME protocol support.  The following parameters come from the spec.
433*e2cf88acSQuaker Fang  */
434*e2cf88acSQuaker Fang typedef struct phyParamType {
435*e2cf88acSQuaker Fang 	uint8_t aifsn;
436*e2cf88acSQuaker Fang 	uint8_t logcwmin;
437*e2cf88acSQuaker Fang 	uint8_t logcwmax;
438*e2cf88acSQuaker Fang 	uint16_t txopLimit;
439*e2cf88acSQuaker Fang 	uint8_t acm;
440*e2cf88acSQuaker Fang } paramType;
441*e2cf88acSQuaker Fang 
442*e2cf88acSQuaker Fang static const paramType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
443*e2cf88acSQuaker Fang 	{ 3, 4,  6,  0, 0 },	/* IEEE80211_MODE_AUTO */
444*e2cf88acSQuaker Fang 	{ 3, 4,  6,  0, 0 },	/* IEEE80211_MODE_11A */
445*e2cf88acSQuaker Fang 	{ 3, 4,  6,  0, 0 },	/* IEEE80211_MODE_11B */
446*e2cf88acSQuaker Fang 	{ 3, 4,  6,  0, 0 },	/* IEEE80211_MODE_11G */
447*e2cf88acSQuaker Fang 	{ 3, 4,  6,  0, 0 },	/* IEEE80211_MODE_FH */
448*e2cf88acSQuaker Fang 	{ 2, 3,  5,  0, 0 },	/* IEEE80211_MODE_TURBO_A */
449*e2cf88acSQuaker Fang 	{ 2, 3,  5,  0, 0 },	/* IEEE80211_MODE_TURBO_G */
450*e2cf88acSQuaker Fang 	{ 2, 3,  5,  0, 0 },	/* IEEE80211_MODE_STURBO_A */
451*e2cf88acSQuaker Fang 	{ 3, 4,  6,  0, 0 },	/* IEEE80211_MODE_11NA */
452*e2cf88acSQuaker Fang 	{ 3, 4,  6,  0, 0 }	/* IEEE80211_MODE_11NG */
453*e2cf88acSQuaker Fang };
454*e2cf88acSQuaker Fang static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
455*e2cf88acSQuaker Fang 	{ 7, 4, 10,  0, 0 },	/* IEEE80211_MODE_AUTO */
456*e2cf88acSQuaker Fang 	{ 7, 4, 10,  0, 0 },	/* IEEE80211_MODE_11A */
457*e2cf88acSQuaker Fang 	{ 7, 4, 10,  0, 0 },	/* IEEE80211_MODE_11B */
458*e2cf88acSQuaker Fang 	{ 7, 4, 10,  0, 0 },	/* IEEE80211_MODE_11G */
459*e2cf88acSQuaker Fang 	{ 7, 4, 10,  0, 0 },	/* IEEE80211_MODE_FH */
460*e2cf88acSQuaker Fang 	{ 7, 3, 10,  0, 0 },	/* IEEE80211_MODE_TURBO_A */
461*e2cf88acSQuaker Fang 	{ 7, 3, 10,  0, 0 },	/* IEEE80211_MODE_TURBO_G */
462*e2cf88acSQuaker Fang 	{ 7, 3, 10,  0, 0 },	/* IEEE80211_MODE_STURBO_A */
463*e2cf88acSQuaker Fang 	{ 7, 4, 10,  0, 0 },	/* IEEE80211_MODE_11NA */
464*e2cf88acSQuaker Fang 	{ 7, 4, 10,  0, 0 },	/* IEEE80211_MODE_11NG */
465*e2cf88acSQuaker Fang };
466*e2cf88acSQuaker Fang static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
467*e2cf88acSQuaker Fang 	{ 1, 3, 4,  94, 0 },	/* IEEE80211_MODE_AUTO */
468*e2cf88acSQuaker Fang 	{ 1, 3, 4,  94, 0 },	/* IEEE80211_MODE_11A */
469*e2cf88acSQuaker Fang 	{ 1, 3, 4, 188, 0 },	/* IEEE80211_MODE_11B */
470*e2cf88acSQuaker Fang 	{ 1, 3, 4,  94, 0 },	/* IEEE80211_MODE_11G */
471*e2cf88acSQuaker Fang 	{ 1, 3, 4, 188, 0 },	/* IEEE80211_MODE_FH */
472*e2cf88acSQuaker Fang 	{ 1, 2, 3,  94, 0 },	/* IEEE80211_MODE_TURBO_A */
473*e2cf88acSQuaker Fang 	{ 1, 2, 3,  94, 0 },	/* IEEE80211_MODE_TURBO_G */
474*e2cf88acSQuaker Fang 	{ 1, 2, 3,  94, 0 },	/* IEEE80211_MODE_STURBO_A */
475*e2cf88acSQuaker Fang 	{ 1, 3, 4,  94, 0 },	/* IEEE80211_MODE_11NA */
476*e2cf88acSQuaker Fang 	{ 1, 3, 4,  94, 0 },	/* IEEE80211_MODE_11NG */
477*e2cf88acSQuaker Fang };
478*e2cf88acSQuaker Fang static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
479*e2cf88acSQuaker Fang 	{ 1, 2, 3,  47, 0 },	/* IEEE80211_MODE_AUTO */
480*e2cf88acSQuaker Fang 	{ 1, 2, 3,  47, 0 },	/* IEEE80211_MODE_11A */
481*e2cf88acSQuaker Fang 	{ 1, 2, 3, 102, 0 },	/* IEEE80211_MODE_11B */
482*e2cf88acSQuaker Fang 	{ 1, 2, 3,  47, 0 },	/* IEEE80211_MODE_11G */
483*e2cf88acSQuaker Fang 	{ 1, 2, 3, 102, 0 },	/* IEEE80211_MODE_FH */
484*e2cf88acSQuaker Fang 	{ 1, 2, 2,  47, 0 },	/* IEEE80211_MODE_TURBO_A */
485*e2cf88acSQuaker Fang 	{ 1, 2, 2,  47, 0 },	/* IEEE80211_MODE_TURBO_G */
486*e2cf88acSQuaker Fang 	{ 1, 2, 2,  47, 0 },	/* IEEE80211_MODE_STURBO_A */
487*e2cf88acSQuaker Fang 	{ 1, 2, 3,  47, 0 },	/* IEEE80211_MODE_11NA */
488*e2cf88acSQuaker Fang 	{ 1, 2, 3,  47, 0 },	/* IEEE80211_MODE_11NG */
489*e2cf88acSQuaker Fang };
490*e2cf88acSQuaker Fang 
491*e2cf88acSQuaker Fang static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
492*e2cf88acSQuaker Fang 	{ 3, 4, 10,  0, 0 },	/* IEEE80211_MODE_AUTO */
493*e2cf88acSQuaker Fang 	{ 3, 4, 10,  0, 0 },	/* IEEE80211_MODE_11A */
494*e2cf88acSQuaker Fang 	{ 3, 4, 10,  0, 0 },	/* IEEE80211_MODE_11B */
495*e2cf88acSQuaker Fang 	{ 3, 4, 10,  0, 0 },	/* IEEE80211_MODE_11G */
496*e2cf88acSQuaker Fang 	{ 3, 4, 10,  0, 0 },	/* IEEE80211_MODE_FH */
497*e2cf88acSQuaker Fang 	{ 2, 3, 10,  0, 0 },	/* IEEE80211_MODE_TURBO_A */
498*e2cf88acSQuaker Fang 	{ 2, 3, 10,  0, 0 },	/* IEEE80211_MODE_TURBO_G */
499*e2cf88acSQuaker Fang 	{ 2, 3, 10,  0, 0 },	/* IEEE80211_MODE_STURBO_A */
500*e2cf88acSQuaker Fang 	{ 3, 4, 10,  0, 0 },	/* IEEE80211_MODE_11NA */
501*e2cf88acSQuaker Fang 	{ 3, 4, 10,  0, 0 },	/* IEEE80211_MODE_11NG */
502*e2cf88acSQuaker Fang };
503*e2cf88acSQuaker Fang static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
504*e2cf88acSQuaker Fang 	{ 2, 3, 4,  94, 0 },	/* IEEE80211_MODE_AUTO */
505*e2cf88acSQuaker Fang 	{ 2, 3, 4,  94, 0 },	/* IEEE80211_MODE_11A */
506*e2cf88acSQuaker Fang 	{ 2, 3, 4, 188, 0 },	/* IEEE80211_MODE_11B */
507*e2cf88acSQuaker Fang 	{ 2, 3, 4,  94, 0 },	/* IEEE80211_MODE_11G */
508*e2cf88acSQuaker Fang 	{ 2, 3, 4, 188, 0 },	/* IEEE80211_MODE_FH */
509*e2cf88acSQuaker Fang 	{ 2, 2, 3,  94, 0 },	/* IEEE80211_MODE_TURBO_A */
510*e2cf88acSQuaker Fang 	{ 2, 2, 3,  94, 0 },	/* IEEE80211_MODE_TURBO_G */
511*e2cf88acSQuaker Fang 	{ 2, 2, 3,  94, 0 },	/* IEEE80211_MODE_STURBO_A */
512*e2cf88acSQuaker Fang 	{ 2, 3, 4,  94, 0 },	/* IEEE80211_MODE_11NA */
513*e2cf88acSQuaker Fang 	{ 2, 3, 4,  94, 0 },	/* IEEE80211_MODE_11NG */
514*e2cf88acSQuaker Fang };
515*e2cf88acSQuaker Fang static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
516*e2cf88acSQuaker Fang 	{ 2, 2, 3,  47, 0 },	/* IEEE80211_MODE_AUTO */
517*e2cf88acSQuaker Fang 	{ 2, 2, 3,  47, 0 },	/* IEEE80211_MODE_11A */
518*e2cf88acSQuaker Fang 	{ 2, 2, 3, 102, 0 },	/* IEEE80211_MODE_11B */
519*e2cf88acSQuaker Fang 	{ 2, 2, 3,  47, 0 },	/* IEEE80211_MODE_11G */
520*e2cf88acSQuaker Fang 	{ 2, 2, 3, 102, 0 },	/* IEEE80211_MODE_FH */
521*e2cf88acSQuaker Fang 	{ 1, 2, 2,  47, 0 },	/* IEEE80211_MODE_TURBO_A */
522*e2cf88acSQuaker Fang 	{ 1, 2, 2,  47, 0 },	/* IEEE80211_MODE_TURBO_G */
523*e2cf88acSQuaker Fang 	{ 1, 2, 2,  47, 0 },	/* IEEE80211_MODE_STURBO_A */
524*e2cf88acSQuaker Fang 	{ 2, 2, 3,  47, 0 },	/* IEEE80211_MODE_11NA */
525*e2cf88acSQuaker Fang 	{ 2, 2, 3,  47, 0 },	/* IEEE80211_MODE_11NG */
526*e2cf88acSQuaker Fang };
527*e2cf88acSQuaker Fang 
528*e2cf88acSQuaker Fang void
ieee80211_wme_initparams(struct ieee80211com * ic)529*e2cf88acSQuaker Fang ieee80211_wme_initparams(struct ieee80211com *ic)
530*e2cf88acSQuaker Fang {
531*e2cf88acSQuaker Fang 	struct ieee80211_wme_state *wme = &ic->ic_wme;
532*e2cf88acSQuaker Fang 	const paramType *pPhyParam, *pBssPhyParam;
533*e2cf88acSQuaker Fang 	struct wmeParams *wmep;
534*e2cf88acSQuaker Fang 	enum ieee80211_phymode mode;
535*e2cf88acSQuaker Fang 	int i;
536*e2cf88acSQuaker Fang 
537*e2cf88acSQuaker Fang 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
538*e2cf88acSQuaker Fang 		return;
539*e2cf88acSQuaker Fang 
540*e2cf88acSQuaker Fang 	/*
541*e2cf88acSQuaker Fang 	 * Select mode; we can be called early in which case we
542*e2cf88acSQuaker Fang 	 * always use auto mode.  We know we'll be called when
543*e2cf88acSQuaker Fang 	 * entering the RUN state with bsschan setup properly
544*e2cf88acSQuaker Fang 	 * so state will eventually get set correctly
545*e2cf88acSQuaker Fang 	 */
546*e2cf88acSQuaker Fang 	if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
547*e2cf88acSQuaker Fang 		mode = ieee80211_chan2mode(ic, ic->ic_curchan);
548*e2cf88acSQuaker Fang 	else
549*e2cf88acSQuaker Fang 		mode = IEEE80211_MODE_AUTO;
550*e2cf88acSQuaker Fang 	for (i = 0; i < WME_NUM_AC; i++) {
551*e2cf88acSQuaker Fang 		switch (i) {
552*e2cf88acSQuaker Fang 		case WME_AC_BK:
553*e2cf88acSQuaker Fang 			pPhyParam = &phyParamForAC_BK[mode];
554*e2cf88acSQuaker Fang 			pBssPhyParam = &phyParamForAC_BK[mode];
555*e2cf88acSQuaker Fang 			break;
556*e2cf88acSQuaker Fang 		case WME_AC_VI:
557*e2cf88acSQuaker Fang 			pPhyParam = &phyParamForAC_VI[mode];
558*e2cf88acSQuaker Fang 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
559*e2cf88acSQuaker Fang 			break;
560*e2cf88acSQuaker Fang 		case WME_AC_VO:
561*e2cf88acSQuaker Fang 			pPhyParam = &phyParamForAC_VO[mode];
562*e2cf88acSQuaker Fang 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
563*e2cf88acSQuaker Fang 			break;
564*e2cf88acSQuaker Fang 		case WME_AC_BE:
565*e2cf88acSQuaker Fang 		default:
566*e2cf88acSQuaker Fang 			pPhyParam = &phyParamForAC_BE[mode];
567*e2cf88acSQuaker Fang 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
568*e2cf88acSQuaker Fang 			break;
569*e2cf88acSQuaker Fang 		}
570*e2cf88acSQuaker Fang 
571*e2cf88acSQuaker Fang 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
572*e2cf88acSQuaker Fang 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
573*e2cf88acSQuaker Fang 			wmep->wmep_acm = pPhyParam->acm;
574*e2cf88acSQuaker Fang 			wmep->wmep_aifsn = pPhyParam->aifsn;
575*e2cf88acSQuaker Fang 			wmep->wmep_logcwmin = pPhyParam->logcwmin;
576*e2cf88acSQuaker Fang 			wmep->wmep_logcwmax = pPhyParam->logcwmax;
577*e2cf88acSQuaker Fang 			wmep->wmep_txopLimit = pPhyParam->txopLimit;
578*e2cf88acSQuaker Fang 		} else {
579*e2cf88acSQuaker Fang 			wmep->wmep_acm = pBssPhyParam->acm;
580*e2cf88acSQuaker Fang 			wmep->wmep_aifsn = pBssPhyParam->aifsn;
581*e2cf88acSQuaker Fang 			wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
582*e2cf88acSQuaker Fang 			wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
583*e2cf88acSQuaker Fang 			wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
584*e2cf88acSQuaker Fang 
585*e2cf88acSQuaker Fang 		}
586*e2cf88acSQuaker Fang 		ieee80211_dbg(IEEE80211_MSG_WME, "ieee80211_wme_initparams: "
587*e2cf88acSQuaker Fang 		    "%s chan [acm %u aifsn %u log2(cwmin) %u "
588*e2cf88acSQuaker Fang 		    "log2(cwmax) %u txpoLimit %u]\n",
589*e2cf88acSQuaker Fang 		    ieee80211_wme_acnames[i],
590*e2cf88acSQuaker Fang 		    wmep->wmep_acm,
591*e2cf88acSQuaker Fang 		    wmep->wmep_aifsn,
592*e2cf88acSQuaker Fang 		    wmep->wmep_logcwmin,
593*e2cf88acSQuaker Fang 		    wmep->wmep_logcwmax,
594*e2cf88acSQuaker Fang 		    wmep->wmep_txopLimit);
595*e2cf88acSQuaker Fang 
596*e2cf88acSQuaker Fang 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
597*e2cf88acSQuaker Fang 		wmep->wmep_acm = pBssPhyParam->acm;
598*e2cf88acSQuaker Fang 		wmep->wmep_aifsn = pBssPhyParam->aifsn;
599*e2cf88acSQuaker Fang 		wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
600*e2cf88acSQuaker Fang 		wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
601*e2cf88acSQuaker Fang 		wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
602*e2cf88acSQuaker Fang 		ieee80211_dbg(IEEE80211_MSG_WME, "ieee80211_wme_initparams: "
603*e2cf88acSQuaker Fang 		    "%s  bss [acm %u aifsn %u log2(cwmin) %u "
604*e2cf88acSQuaker Fang 		    "log2(cwmax) %u txpoLimit %u]\n",
605*e2cf88acSQuaker Fang 		    ieee80211_wme_acnames[i],
606*e2cf88acSQuaker Fang 		    wmep->wmep_acm,
607*e2cf88acSQuaker Fang 		    wmep->wmep_aifsn,
608*e2cf88acSQuaker Fang 		    wmep->wmep_logcwmin,
609*e2cf88acSQuaker Fang 		    wmep->wmep_logcwmax,
610*e2cf88acSQuaker Fang 		    wmep->wmep_txopLimit);
611*e2cf88acSQuaker Fang 	}
612*e2cf88acSQuaker Fang 	/* NB: check ic_bss to avoid NULL deref on initial attach */
613*e2cf88acSQuaker Fang 	if (ic->ic_bss != NULL) {
614*e2cf88acSQuaker Fang 		/*
615*e2cf88acSQuaker Fang 		 * Calculate agressive mode switching threshold based
616*e2cf88acSQuaker Fang 		 * on beacon interval.  This doesn't need locking since
617*e2cf88acSQuaker Fang 		 * we're only called before entering the RUN state at
618*e2cf88acSQuaker Fang 		 * which point we start sending beacon frames.
619*e2cf88acSQuaker Fang 		 */
620*e2cf88acSQuaker Fang 		wme->wme_hipri_switch_thresh =
621*e2cf88acSQuaker Fang 		    (HIGH_PRI_SWITCH_THRESH * ic->ic_bss->in_intval) / 100;
622*e2cf88acSQuaker Fang 		ieee80211_wme_updateparams(ic);
623*e2cf88acSQuaker Fang 	}
624*e2cf88acSQuaker Fang }
625*e2cf88acSQuaker Fang 
626*e2cf88acSQuaker Fang /*
627*e2cf88acSQuaker Fang  * Update WME parameters for ourself and the BSS.
628*e2cf88acSQuaker Fang  */
629*e2cf88acSQuaker Fang void
ieee80211_wme_updateparams(struct ieee80211com * ic)630*e2cf88acSQuaker Fang ieee80211_wme_updateparams(struct ieee80211com *ic)
631*e2cf88acSQuaker Fang {
632*e2cf88acSQuaker Fang 	static const paramType phyParam[IEEE80211_MODE_MAX] = {
633*e2cf88acSQuaker Fang 		{ 2, 4, 10, 64, 0 },	/* IEEE80211_MODE_AUTO */
634*e2cf88acSQuaker Fang 		{ 2, 4, 10, 64, 0 },	/* IEEE80211_MODE_11A */
635*e2cf88acSQuaker Fang 		{ 2, 5, 10, 64, 0 },	/* IEEE80211_MODE_11B */
636*e2cf88acSQuaker Fang 		{ 2, 4, 10, 64, 0 },	/* IEEE80211_MODE_11G */
637*e2cf88acSQuaker Fang 		{ 2, 5, 10, 64, 0 },	/* IEEE80211_MODE_FH */
638*e2cf88acSQuaker Fang 		{ 1, 3, 10, 64, 0 },	/* IEEE80211_MODE_TURBO_A */
639*e2cf88acSQuaker Fang 		{ 1, 3, 10, 64, 0 },	/* IEEE80211_MODE_TURBO_G */
640*e2cf88acSQuaker Fang 		{ 1, 3, 10, 64, 0 },	/* IEEE80211_MODE_STURBO_A */
641*e2cf88acSQuaker Fang 		{ 2, 4, 10, 64, 0 },	/* IEEE80211_MODE_11NA */
642*e2cf88acSQuaker Fang 		{ 2, 4, 10, 64, 0 },	/* IEEE80211_MODE_11NG */
643*e2cf88acSQuaker Fang 	};
644*e2cf88acSQuaker Fang 	struct ieee80211_wme_state *wme = &ic->ic_wme;
645*e2cf88acSQuaker Fang 	const struct wmeParams *wmep;
646*e2cf88acSQuaker Fang 	struct wmeParams *chanp, *bssp;
647*e2cf88acSQuaker Fang 	enum ieee80211_phymode mode;
648*e2cf88acSQuaker Fang 	int i;
649*e2cf88acSQuaker Fang 
650*e2cf88acSQuaker Fang 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
651*e2cf88acSQuaker Fang 		return;
652*e2cf88acSQuaker Fang 
653*e2cf88acSQuaker Fang 	/* set up the channel access parameters for the physical device */
654*e2cf88acSQuaker Fang 	for (i = 0; i < WME_NUM_AC; i++) {
655*e2cf88acSQuaker Fang 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
656*e2cf88acSQuaker Fang 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
657*e2cf88acSQuaker Fang 		chanp->wmep_aifsn = wmep->wmep_aifsn;
658*e2cf88acSQuaker Fang 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
659*e2cf88acSQuaker Fang 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
660*e2cf88acSQuaker Fang 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
661*e2cf88acSQuaker Fang 
662*e2cf88acSQuaker Fang 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
663*e2cf88acSQuaker Fang 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
664*e2cf88acSQuaker Fang 		chanp->wmep_aifsn = wmep->wmep_aifsn;
665*e2cf88acSQuaker Fang 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
666*e2cf88acSQuaker Fang 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
667*e2cf88acSQuaker Fang 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
668*e2cf88acSQuaker Fang 	}
669*e2cf88acSQuaker Fang 
670*e2cf88acSQuaker Fang 	/*
671*e2cf88acSQuaker Fang 	 * Select mode; we can be called early in which case we
672*e2cf88acSQuaker Fang 	 * always use auto mode.  We know we'll be called when
673*e2cf88acSQuaker Fang 	 * entering the RUN state with bsschan setup properly
674*e2cf88acSQuaker Fang 	 * so state will eventually get set correctly
675*e2cf88acSQuaker Fang 	 */
676*e2cf88acSQuaker Fang 	if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
677*e2cf88acSQuaker Fang 		mode = ieee80211_chan2mode(ic, ic->ic_curchan);
678*e2cf88acSQuaker Fang 	else
679*e2cf88acSQuaker Fang 		mode = IEEE80211_MODE_AUTO;
680*e2cf88acSQuaker Fang 
681*e2cf88acSQuaker Fang 	/*
682*e2cf88acSQuaker Fang 	 * This implements agressive mode as found in certain
683*e2cf88acSQuaker Fang 	 * vendors' AP's.  When there is significant high
684*e2cf88acSQuaker Fang 	 * priority (VI/VO) traffic in the BSS throttle back BE
685*e2cf88acSQuaker Fang 	 * traffic by using conservative parameters.  Otherwise
686*e2cf88acSQuaker Fang 	 * BE uses agressive params to optimize performance of
687*e2cf88acSQuaker Fang 	 * legacy/non-QoS traffic.
688*e2cf88acSQuaker Fang 	 */
689*e2cf88acSQuaker Fang 	if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
690*e2cf88acSQuaker Fang 	    (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
691*e2cf88acSQuaker Fang 	    (ic->ic_opmode == IEEE80211_M_STA &&
692*e2cf88acSQuaker Fang 	    (ic->ic_bss->in_flags & IEEE80211_NODE_QOS) == 0) ||
693*e2cf88acSQuaker Fang 	    (ic->ic_flags & IEEE80211_F_WME) == 0) {
694*e2cf88acSQuaker Fang 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
695*e2cf88acSQuaker Fang 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
696*e2cf88acSQuaker Fang 
697*e2cf88acSQuaker Fang 		chanp->wmep_aifsn = bssp->wmep_aifsn = phyParam[mode].aifsn;
698*e2cf88acSQuaker Fang 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
699*e2cf88acSQuaker Fang 		    phyParam[mode].logcwmin;
700*e2cf88acSQuaker Fang 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
701*e2cf88acSQuaker Fang 		    phyParam[mode].logcwmax;
702*e2cf88acSQuaker Fang 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
703*e2cf88acSQuaker Fang 		    (ic->ic_flags & IEEE80211_F_BURST) ?
704*e2cf88acSQuaker Fang 		    phyParam[mode].txopLimit : 0;
705*e2cf88acSQuaker Fang 		ieee80211_dbg(IEEE80211_MSG_WME,
706*e2cf88acSQuaker Fang 		    "ieee80211_wme_updateparams_locked: "
707*e2cf88acSQuaker Fang 		    "%s [acm %u aifsn %u log2(cwmin) %u "
708*e2cf88acSQuaker Fang 		    "log2(cwmax) %u txpoLimit %u]\n",
709*e2cf88acSQuaker Fang 		    ieee80211_wme_acnames[WME_AC_BE],
710*e2cf88acSQuaker Fang 		    chanp->wmep_acm,
711*e2cf88acSQuaker Fang 		    chanp->wmep_aifsn,
712*e2cf88acSQuaker Fang 		    chanp->wmep_logcwmin,
713*e2cf88acSQuaker Fang 		    chanp->wmep_logcwmax,
714*e2cf88acSQuaker Fang 		    chanp->wmep_txopLimit);
715*e2cf88acSQuaker Fang 	}
716*e2cf88acSQuaker Fang 
717*e2cf88acSQuaker Fang 	wme->wme_update(ic);
718*e2cf88acSQuaker Fang 
719*e2cf88acSQuaker Fang 	ieee80211_dbg(IEEE80211_MSG_WME, "ieee80211_wme_updateparams(): "
720*e2cf88acSQuaker Fang 	    "%s: WME params updated, cap_info 0x%x\n",
721*e2cf88acSQuaker Fang 	    ic->ic_opmode == IEEE80211_M_STA ?
722*e2cf88acSQuaker Fang 	    wme->wme_wmeChanParams.cap_info :
723*e2cf88acSQuaker Fang 	    wme->wme_bssChanParams.cap_info);
724*e2cf88acSQuaker Fang }
725*e2cf88acSQuaker Fang 
726*e2cf88acSQuaker Fang /*
7270ba2cbe9Sxc151355  * Process STA mode beacon miss events. Send a direct probe request
7280ba2cbe9Sxc151355  * frame to the current ap bmiss_max times (w/o answer) before
7290ba2cbe9Sxc151355  * scanning for a new ap.
7300ba2cbe9Sxc151355  */
7310ba2cbe9Sxc151355 void
ieee80211_beacon_miss(ieee80211com_t * ic)7320ba2cbe9Sxc151355 ieee80211_beacon_miss(ieee80211com_t *ic)
7330ba2cbe9Sxc151355 {
7340ba2cbe9Sxc151355 	ieee80211_impl_t *im = ic->ic_private;
7350ba2cbe9Sxc151355 
7360ba2cbe9Sxc151355 	if (ic->ic_flags & IEEE80211_F_SCAN)
7370ba2cbe9Sxc151355 		return;
7380ba2cbe9Sxc151355 	ieee80211_dbg(IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
7390ba2cbe9Sxc151355 	    "%s\n", "beacon miss");
7400ba2cbe9Sxc151355 
7410ba2cbe9Sxc151355 	/*
7420ba2cbe9Sxc151355 	 * Our handling is only meaningful for stations that are
7430ba2cbe9Sxc151355 	 * associated; any other conditions else will be handled
7440ba2cbe9Sxc151355 	 * through different means (e.g. the tx timeout on mgt frames).
7450ba2cbe9Sxc151355 	 */
7460ba2cbe9Sxc151355 	if (ic->ic_opmode != IEEE80211_M_STA ||
7470ba2cbe9Sxc151355 	    ic->ic_state != IEEE80211_S_RUN) {
7480ba2cbe9Sxc151355 		return;
7490ba2cbe9Sxc151355 	}
7500ba2cbe9Sxc151355 
7510ba2cbe9Sxc151355 	IEEE80211_LOCK(ic);
7520ba2cbe9Sxc151355 	if (++im->im_bmiss_count < im->im_bmiss_max) {
7530ba2cbe9Sxc151355 		/*
7540ba2cbe9Sxc151355 		 * Send a directed probe req before falling back to a scan;
7550ba2cbe9Sxc151355 		 * if we receive a response ic_bmiss_count will be reset.
7560ba2cbe9Sxc151355 		 * Some cards mistakenly report beacon miss so this avoids
7570ba2cbe9Sxc151355 		 * the expensive scan if the ap is still there.
7580ba2cbe9Sxc151355 		 */
7590ba2cbe9Sxc151355 		IEEE80211_UNLOCK(ic);
7600ba2cbe9Sxc151355 		(void) ieee80211_send_probereq(ic->ic_bss, ic->ic_macaddr,
7610ba2cbe9Sxc151355 		    ic->ic_bss->in_bssid, ic->ic_bss->in_bssid,
7620ba2cbe9Sxc151355 		    ic->ic_bss->in_essid, ic->ic_bss->in_esslen,
7630ba2cbe9Sxc151355 		    ic->ic_opt_ie, ic->ic_opt_ie_len);
7640ba2cbe9Sxc151355 		return;
7650ba2cbe9Sxc151355 	}
7660ba2cbe9Sxc151355 	im->im_bmiss_count = 0;
7670ba2cbe9Sxc151355 	IEEE80211_UNLOCK(ic);
7680ba2cbe9Sxc151355 	ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
7690ba2cbe9Sxc151355 }
7700ba2cbe9Sxc151355 
7710ba2cbe9Sxc151355 /*
7720ba2cbe9Sxc151355  * Manage state transition between INIT | AUTH | ASSOC | RUN.
7730ba2cbe9Sxc151355  */
7740ba2cbe9Sxc151355 static int
ieee80211_newstate(ieee80211com_t * ic,enum ieee80211_state nstate,int arg)7750ba2cbe9Sxc151355 ieee80211_newstate(ieee80211com_t *ic, enum ieee80211_state nstate, int arg)
7760ba2cbe9Sxc151355 {
7770ba2cbe9Sxc151355 	struct ieee80211_impl *im = ic->ic_private;
7780ba2cbe9Sxc151355 	ieee80211_node_t *in;
7790ba2cbe9Sxc151355 	enum ieee80211_state ostate;
7800ba2cbe9Sxc151355 	wifi_data_t wd = { 0 };
7810ba2cbe9Sxc151355 
7820ba2cbe9Sxc151355 	IEEE80211_LOCK(ic);
7830ba2cbe9Sxc151355 	ostate = ic->ic_state;
7840ba2cbe9Sxc151355 	ieee80211_dbg(IEEE80211_MSG_STATE, "ieee80211_newstate(): "
7850ba2cbe9Sxc151355 	    "%s -> %s\n",
7860ba2cbe9Sxc151355 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
7870ba2cbe9Sxc151355 	ic->ic_state = nstate;
7880ba2cbe9Sxc151355 	in = ic->ic_bss;
7890ba2cbe9Sxc151355 	im->im_swbmiss_period = 0;	/* Reset software beacon miss period */
7900ba2cbe9Sxc151355 
7910ba2cbe9Sxc151355 	switch (nstate) {
7920ba2cbe9Sxc151355 	case IEEE80211_S_INIT:
7930ba2cbe9Sxc151355 		IEEE80211_UNLOCK(ic);
7940ba2cbe9Sxc151355 		switch (ostate) {
7950ba2cbe9Sxc151355 		case IEEE80211_S_INIT:
7960ba2cbe9Sxc151355 			return (0);
7970ba2cbe9Sxc151355 		case IEEE80211_S_SCAN:
7980ba2cbe9Sxc151355 			ieee80211_cancel_scan(ic);
7990ba2cbe9Sxc151355 			break;
8000ba2cbe9Sxc151355 		case IEEE80211_S_AUTH:
8010ba2cbe9Sxc151355 			break;
8020ba2cbe9Sxc151355 		case IEEE80211_S_ASSOC:
8030ba2cbe9Sxc151355 			if (ic->ic_opmode == IEEE80211_M_STA) {
8040ba2cbe9Sxc151355 				IEEE80211_SEND_MGMT(ic, in,
8050ba2cbe9Sxc151355 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
8060ba2cbe9Sxc151355 				    IEEE80211_REASON_AUTH_LEAVE);
8070ba2cbe9Sxc151355 			}
8080ba2cbe9Sxc151355 			break;
8090ba2cbe9Sxc151355 		case IEEE80211_S_RUN:
81019d332feSfei feng - Sun Microsystems - Beijing China 			switch (ic->ic_opmode) {
81119d332feSfei feng - Sun Microsystems - Beijing China 			case IEEE80211_M_STA:
8120ba2cbe9Sxc151355 				IEEE80211_SEND_MGMT(ic, in,
813*e2cf88acSQuaker Fang 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
814*e2cf88acSQuaker Fang 				    IEEE80211_REASON_AUTH_LEAVE);
8150ba2cbe9Sxc151355 				ieee80211_sta_leave(ic, in);
81619d332feSfei feng - Sun Microsystems - Beijing China 				break;
81719d332feSfei feng - Sun Microsystems - Beijing China 			case IEEE80211_M_IBSS:
81819d332feSfei feng - Sun Microsystems - Beijing China 				ieee80211_notify_node_leave(ic, in);
81919d332feSfei feng - Sun Microsystems - Beijing China 				break;
82019d332feSfei feng - Sun Microsystems - Beijing China 			default:
82119d332feSfei feng - Sun Microsystems - Beijing China 				break;
8220ba2cbe9Sxc151355 			}
8230ba2cbe9Sxc151355 			break;
8240ba2cbe9Sxc151355 		}
8250ba2cbe9Sxc151355 		IEEE80211_LOCK(ic);
8260ba2cbe9Sxc151355 		im->im_mgt_timer = 0;
8270ba2cbe9Sxc151355 		ieee80211_reset_bss(ic);
8280ba2cbe9Sxc151355 		break;
8290ba2cbe9Sxc151355 	case IEEE80211_S_SCAN:
8300ba2cbe9Sxc151355 		switch (ostate) {
8310ba2cbe9Sxc151355 		case IEEE80211_S_INIT:
8320ba2cbe9Sxc151355 			IEEE80211_UNLOCK(ic);
83319d332feSfei feng - Sun Microsystems - Beijing China 			ieee80211_begin_scan(ic, (arg == 0) ? B_FALSE : B_TRUE);
8340ba2cbe9Sxc151355 			return (0);
8350ba2cbe9Sxc151355 		case IEEE80211_S_SCAN:
8360ba2cbe9Sxc151355 			/*
8370ba2cbe9Sxc151355 			 * Scan next. If doing an active scan and the
8380ba2cbe9Sxc151355 			 * channel is not marked passive-only then send
8390ba2cbe9Sxc151355 			 * a probe request.  Otherwise just listen for
8400ba2cbe9Sxc151355 			 * beacons on the channel.
8410ba2cbe9Sxc151355 			 */
8420ba2cbe9Sxc151355 			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
8430ba2cbe9Sxc151355 			    !IEEE80211_IS_CHAN_PASSIVE(ic->ic_curchan)) {
8440ba2cbe9Sxc151355 				IEEE80211_UNLOCK(ic);
8450ba2cbe9Sxc151355 				(void) ieee80211_send_probereq(in,
8460ba2cbe9Sxc151355 				    ic->ic_macaddr, wifi_bcastaddr,
8470ba2cbe9Sxc151355 				    wifi_bcastaddr,
8480ba2cbe9Sxc151355 				    ic->ic_des_essid, ic->ic_des_esslen,
8490ba2cbe9Sxc151355 				    ic->ic_opt_ie, ic->ic_opt_ie_len);
8500ba2cbe9Sxc151355 				return (0);
8510ba2cbe9Sxc151355 			}
8520ba2cbe9Sxc151355 			break;
8530ba2cbe9Sxc151355 		case IEEE80211_S_RUN:
8540ba2cbe9Sxc151355 			/* beacon miss */
8550ba2cbe9Sxc151355 			ieee80211_dbg(IEEE80211_MSG_STATE,
8560ba2cbe9Sxc151355 			    "no recent beacons from %s, rescanning\n",
8570ba2cbe9Sxc151355 			    ieee80211_macaddr_sprintf(in->in_macaddr));
8580ba2cbe9Sxc151355 			IEEE80211_UNLOCK(ic);
8590ba2cbe9Sxc151355 			ieee80211_sta_leave(ic, in);
8600ba2cbe9Sxc151355 			IEEE80211_LOCK(ic);
8610ba2cbe9Sxc151355 			ic->ic_flags &= ~IEEE80211_F_SIBSS;
8620ba2cbe9Sxc151355 			/* FALLTHRU */
8630ba2cbe9Sxc151355 		case IEEE80211_S_AUTH:
8640ba2cbe9Sxc151355 		case IEEE80211_S_ASSOC:
8650ba2cbe9Sxc151355 			/* timeout restart scan */
8660ba2cbe9Sxc151355 			in = ieee80211_find_node(&ic->ic_scan,
8670ba2cbe9Sxc151355 			    ic->ic_bss->in_macaddr);
8680ba2cbe9Sxc151355 			if (in != NULL) {
8690ba2cbe9Sxc151355 				in->in_fails++;
8700ba2cbe9Sxc151355 				ieee80211_unref_node(&in);
8710ba2cbe9Sxc151355 			}
8720ba2cbe9Sxc151355 			break;
8730ba2cbe9Sxc151355 		}
8740ba2cbe9Sxc151355 		break;
8750ba2cbe9Sxc151355 	case IEEE80211_S_AUTH:
87619d332feSfei feng - Sun Microsystems - Beijing China 		ASSERT(ic->ic_opmode == IEEE80211_M_STA);
8770ba2cbe9Sxc151355 		switch (ostate) {
8780ba2cbe9Sxc151355 		case IEEE80211_S_INIT:
8790ba2cbe9Sxc151355 		case IEEE80211_S_SCAN:
8800ba2cbe9Sxc151355 			IEEE80211_UNLOCK(ic);
8810ba2cbe9Sxc151355 			IEEE80211_SEND_MGMT(ic, in, IEEE80211_FC0_SUBTYPE_AUTH,
8820ba2cbe9Sxc151355 			    1);
8830ba2cbe9Sxc151355 			return (0);
8840ba2cbe9Sxc151355 		case IEEE80211_S_AUTH:
8850ba2cbe9Sxc151355 		case IEEE80211_S_ASSOC:
8860ba2cbe9Sxc151355 			switch (arg) {
8870ba2cbe9Sxc151355 			case IEEE80211_FC0_SUBTYPE_AUTH:
8880ba2cbe9Sxc151355 				IEEE80211_UNLOCK(ic);
8890ba2cbe9Sxc151355 				IEEE80211_SEND_MGMT(ic, in,
8900ba2cbe9Sxc151355 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
8910ba2cbe9Sxc151355 				return (0);
8920ba2cbe9Sxc151355 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
8930ba2cbe9Sxc151355 				/* ignore and retry scan on timeout */
8940ba2cbe9Sxc151355 				break;
8950ba2cbe9Sxc151355 			}
8960ba2cbe9Sxc151355 			break;
8970ba2cbe9Sxc151355 		case IEEE80211_S_RUN:
8980ba2cbe9Sxc151355 			switch (arg) {
8990ba2cbe9Sxc151355 			case IEEE80211_FC0_SUBTYPE_AUTH:
9000ba2cbe9Sxc151355 				ic->ic_state = ostate;	/* stay RUN */
9010ba2cbe9Sxc151355 				IEEE80211_UNLOCK(ic);
9020ba2cbe9Sxc151355 				IEEE80211_SEND_MGMT(ic, in,
9030ba2cbe9Sxc151355 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
9040ba2cbe9Sxc151355 				return (0);
9050ba2cbe9Sxc151355 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
9060ba2cbe9Sxc151355 				IEEE80211_UNLOCK(ic);
9070ba2cbe9Sxc151355 				ieee80211_sta_leave(ic, in);
9080ba2cbe9Sxc151355 				/* try to re-auth */
9090ba2cbe9Sxc151355 				IEEE80211_SEND_MGMT(ic, in,
9100ba2cbe9Sxc151355 				    IEEE80211_FC0_SUBTYPE_AUTH, 1);
9110ba2cbe9Sxc151355 				return (0);
9120ba2cbe9Sxc151355 			}
9130ba2cbe9Sxc151355 			break;
9140ba2cbe9Sxc151355 		}
9150ba2cbe9Sxc151355 		break;
9160ba2cbe9Sxc151355 	case IEEE80211_S_ASSOC:
91719d332feSfei feng - Sun Microsystems - Beijing China 		ASSERT(ic->ic_opmode == IEEE80211_M_STA);
9180ba2cbe9Sxc151355 		switch (ostate) {
9190ba2cbe9Sxc151355 		case IEEE80211_S_INIT:
9200ba2cbe9Sxc151355 		case IEEE80211_S_SCAN:
9210ba2cbe9Sxc151355 		case IEEE80211_S_ASSOC:
9220ba2cbe9Sxc151355 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_newstate: "
9230ba2cbe9Sxc151355 			    "invalid transition\n");
9240ba2cbe9Sxc151355 			break;
9250ba2cbe9Sxc151355 		case IEEE80211_S_AUTH:
9260ba2cbe9Sxc151355 			IEEE80211_UNLOCK(ic);
9270ba2cbe9Sxc151355 			IEEE80211_SEND_MGMT(ic, in,
9280ba2cbe9Sxc151355 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
9290ba2cbe9Sxc151355 			return (0);
9300ba2cbe9Sxc151355 		case IEEE80211_S_RUN:
9310ba2cbe9Sxc151355 			IEEE80211_UNLOCK(ic);
9320ba2cbe9Sxc151355 			ieee80211_sta_leave(ic, in);
9330ba2cbe9Sxc151355 			IEEE80211_SEND_MGMT(ic, in,
9340ba2cbe9Sxc151355 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
9350ba2cbe9Sxc151355 			return (0);
9360ba2cbe9Sxc151355 		}
9370ba2cbe9Sxc151355 		break;
9380ba2cbe9Sxc151355 	case IEEE80211_S_RUN:
9390ba2cbe9Sxc151355 		switch (ostate) {
9400ba2cbe9Sxc151355 		case IEEE80211_S_INIT:
9410ba2cbe9Sxc151355 			ieee80211_err("ieee80211_newstate: "
9420ba2cbe9Sxc151355 			    "invalid transition\n");
9430ba2cbe9Sxc151355 			break;
9440ba2cbe9Sxc151355 		case IEEE80211_S_AUTH:
9450ba2cbe9Sxc151355 			ieee80211_err("ieee80211_newstate: "
9460ba2cbe9Sxc151355 			    "invalid transition\n");
9470ba2cbe9Sxc151355 			break;
9480ba2cbe9Sxc151355 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
9490ba2cbe9Sxc151355 		case IEEE80211_S_ASSOC:		/* infra mode */
9500ba2cbe9Sxc151355 			ASSERT(in->in_txrate < in->in_rates.ir_nrates);
9510ba2cbe9Sxc151355 			im->im_mgt_timer = 0;
9520ba2cbe9Sxc151355 			ieee80211_notify_node_join(ic, in);
9530ba2cbe9Sxc151355 
9540ba2cbe9Sxc151355 			/*
9550ba2cbe9Sxc151355 			 * We can send data now; update the fastpath with our
9560ba2cbe9Sxc151355 			 * current associated BSSID and other relevant settings.
9570ba2cbe9Sxc151355 			 */
9580ba2cbe9Sxc151355 			wd.wd_secalloc = ieee80211_crypto_getciphertype(ic);
9590ba2cbe9Sxc151355 			wd.wd_opmode = ic->ic_opmode;
9600ba2cbe9Sxc151355 			IEEE80211_ADDR_COPY(wd.wd_bssid, in->in_bssid);
961*e2cf88acSQuaker Fang 			wd.wd_qospad = 0;
962*e2cf88acSQuaker Fang 			if (in->in_flags &
963*e2cf88acSQuaker Fang 			    (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) {
964*e2cf88acSQuaker Fang 				wd.wd_qospad = 2;
965*e2cf88acSQuaker Fang 				if (ic->ic_flags & IEEE80211_F_DATAPAD) {
966*e2cf88acSQuaker Fang 					wd.wd_qospad = roundup(wd.wd_qospad,
967*e2cf88acSQuaker Fang 					    sizeof (uint32_t));
968*e2cf88acSQuaker Fang 				}
969*e2cf88acSQuaker Fang 			}
9700ba2cbe9Sxc151355 			(void) mac_pdata_update(ic->ic_mach, &wd, sizeof (wd));
9710ba2cbe9Sxc151355 			break;
9720ba2cbe9Sxc151355 		}
9730ba2cbe9Sxc151355 
9740ba2cbe9Sxc151355 		/*
9750ba2cbe9Sxc151355 		 * When 802.1x is not in use mark the port authorized
9760ba2cbe9Sxc151355 		 * at this point so traffic can flow.
9770ba2cbe9Sxc151355 		 */
9780ba2cbe9Sxc151355 		if (in->in_authmode != IEEE80211_AUTH_8021X)
9790ba2cbe9Sxc151355 			ieee80211_node_authorize(in);
9800ba2cbe9Sxc151355 		/*
9810ba2cbe9Sxc151355 		 * Enable inactivity processing.
9820ba2cbe9Sxc151355 		 */
9830ba2cbe9Sxc151355 		ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT;
9840ba2cbe9Sxc151355 		ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT;
9850ba2cbe9Sxc151355 		break;	/* IEEE80211_S_RUN */
9860ba2cbe9Sxc151355 	} /* switch nstate */
9870ba2cbe9Sxc151355 	IEEE80211_UNLOCK(ic);
9880ba2cbe9Sxc151355 
9890ba2cbe9Sxc151355 	return (0);
9900ba2cbe9Sxc151355 }
991