Home | History | Annotate | Line # | Download | only in net80211
ieee80211_proto.c revision 1.21
      1 /*	$NetBSD: ieee80211_proto.c,v 1.21 2005/07/26 22:52:48 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU General Public License ("GPL") version 2 as published by the Free
     20  * Software Foundation.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_proto.c,v 1.17 2005/07/04 01:29:41 sam Exp $");
     37 #endif
     38 #ifdef __NetBSD__
     39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_proto.c,v 1.21 2005/07/26 22:52:48 dyoung Exp $");
     40 #endif
     41 
     42 /*
     43  * IEEE 802.11 protocol support.
     44  */
     45 
     46 #include "opt_inet.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/kernel.h>
     50 #include <sys/systm.h>
     51 
     52 #include <sys/socket.h>
     53 #include <sys/sockio.h>
     54 #include <sys/endian.h>
     55 #include <sys/errno.h>
     56 #include <sys/proc.h>
     57 #include <sys/sysctl.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_media.h>
     61 #include <net/if_arp.h>
     62 #include <net/if_ether.h>
     63 #include <net/if_llc.h>
     64 
     65 #include <net80211/ieee80211_netbsd.h>
     66 #include <net80211/ieee80211_var.h>
     67 
     68 #include <net/bpf.h>
     69 
     70 #ifdef INET
     71 #include <netinet/in.h>
     72 #include <net/if_ether.h>
     73 #endif
     74 
     75 #include <net/route.h>
     76 /* XXX tunables */
     77 #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
     78 #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
     79 
     80 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
     81 
     82 const char *ieee80211_mgt_subtype_name[] = {
     83 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
     84 	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
     85 	"beacon",	"atim",		"disassoc",	"auth",
     86 	"deauth",	"reserved#13",	"reserved#14",	"reserved#15"
     87 };
     88 const char *ieee80211_ctl_subtype_name[] = {
     89 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
     90 	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
     91 	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
     92 	"cts",		"ack",		"cf_end",	"cf_end_ack"
     93 };
     94 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
     95 	"INIT",		/* IEEE80211_S_INIT */
     96 	"SCAN",		/* IEEE80211_S_SCAN */
     97 	"AUTH",		/* IEEE80211_S_AUTH */
     98 	"ASSOC",	/* IEEE80211_S_ASSOC */
     99 	"RUN"		/* IEEE80211_S_RUN */
    100 };
    101 const char *ieee80211_wme_acnames[] = {
    102 	"WME_AC_BE",
    103 	"WME_AC_BK",
    104 	"WME_AC_VI",
    105 	"WME_AC_VO",
    106 	"WME_UPSD",
    107 };
    108 
    109 static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
    110 
    111 void
    112 ieee80211_proto_attach(struct ieee80211com *ic)
    113 {
    114 	struct ifnet *ifp = ic->ic_ifp;
    115 
    116 	/* XXX room for crypto  */
    117 	ifp->if_hdrlen = sizeof(struct ieee80211_qosframe_addr4);
    118 
    119 #ifdef notdef
    120 	ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
    121 #else
    122 	ic->ic_rtsthreshold = IEEE80211_RTS_MAX;
    123 #endif
    124 	ic->ic_fragthreshold = 2346;		/* XXX not used yet */
    125 	ic->ic_fixed_rate = -1;			/* no fixed rate */
    126 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
    127 	ic->ic_roaming = IEEE80211_ROAMING_AUTO;
    128 
    129 	ic->ic_wme.wme_hipri_switch_hysteresis =
    130 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
    131 
    132 	/* protocol state change handler */
    133 	ic->ic_newstate = ieee80211_newstate;
    134 
    135 	/* initialize management frame handlers */
    136 	ic->ic_recv_mgmt = ieee80211_recv_mgmt;
    137 	ic->ic_send_mgmt = ieee80211_send_mgmt;
    138 }
    139 
    140 void
    141 ieee80211_proto_detach(struct ieee80211com *ic)
    142 {
    143 
    144 	/*
    145 	 * This should not be needed as we detach when reseting
    146 	 * the state but be conservative here since the
    147 	 * authenticator may do things like spawn kernel threads.
    148 	 */
    149 	if (ic->ic_auth->ia_detach)
    150 		ic->ic_auth->ia_detach(ic);
    151 
    152 	IF_PURGE(&ic->ic_mgtq);
    153 
    154 	/*
    155 	 * Detach any ACL'ator.
    156 	 */
    157 	if (ic->ic_acl != NULL)
    158 		ic->ic_acl->iac_detach(ic);
    159 }
    160 
    161 /*
    162  * Simple-minded authenticator module support.
    163  */
    164 
    165 #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
    166 /* XXX well-known names */
    167 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
    168 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
    169 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
    170 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
    171 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
    172 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
    173 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
    174 };
    175 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
    176 
    177 static const struct ieee80211_authenticator auth_internal = {
    178 	.ia_name		= "wlan_internal",
    179 	.ia_attach		= NULL,
    180 	.ia_detach		= NULL,
    181 	.ia_node_join		= NULL,
    182 	.ia_node_leave		= NULL,
    183 };
    184 
    185 /*
    186  * Setup internal authenticators once; they are never unregistered.
    187  */
    188 static void
    189 ieee80211_auth_setup(void)
    190 {
    191 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
    192 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
    193 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
    194 }
    195 
    196 const struct ieee80211_authenticator *
    197 ieee80211_authenticator_get(int auth)
    198 {
    199 	static int initialized = 0;
    200 	if (!initialized) {
    201 		ieee80211_auth_setup();
    202 		initialized = 1;
    203 	}
    204 	if (auth >= IEEE80211_AUTH_MAX)
    205 		return NULL;
    206 	if (authenticators[auth] == NULL)
    207 		ieee80211_load_module(auth_modnames[auth]);
    208 	return authenticators[auth];
    209 }
    210 
    211 void
    212 ieee80211_authenticator_register(int type,
    213 	const struct ieee80211_authenticator *auth)
    214 {
    215 	if (type >= IEEE80211_AUTH_MAX)
    216 		return;
    217 	authenticators[type] = auth;
    218 }
    219 
    220 void
    221 ieee80211_authenticator_unregister(int type)
    222 {
    223 
    224 	if (type >= IEEE80211_AUTH_MAX)
    225 		return;
    226 	authenticators[type] = NULL;
    227 }
    228 
    229 /*
    230  * Very simple-minded ACL module support.
    231  */
    232 /* XXX just one for now */
    233 static	const struct ieee80211_aclator *acl = NULL;
    234 
    235 void
    236 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
    237 {
    238 	printf("wlan: %s acl policy registered\n", iac->iac_name);
    239 	acl = iac;
    240 }
    241 
    242 void
    243 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
    244 {
    245 	if (acl == iac)
    246 		acl = NULL;
    247 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
    248 }
    249 
    250 const struct ieee80211_aclator *
    251 ieee80211_aclator_get(const char *name)
    252 {
    253 	if (acl == NULL)
    254 		ieee80211_load_module("wlan_acl");
    255 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
    256 }
    257 
    258 void
    259 ieee80211_print_essid(const u_int8_t *essid, int len)
    260 {
    261 	const u_int8_t *p;
    262 	int i;
    263 
    264 	if (len > IEEE80211_NWID_LEN)
    265 		len = IEEE80211_NWID_LEN;
    266 	/* determine printable or not */
    267 	for (i = 0, p = essid; i < len; i++, p++) {
    268 		if (*p < ' ' || *p > 0x7e)
    269 			break;
    270 	}
    271 	if (i == len) {
    272 		printf("\"");
    273 		for (i = 0, p = essid; i < len; i++, p++)
    274 			printf("%c", *p);
    275 		printf("\"");
    276 	} else {
    277 		printf("0x");
    278 		for (i = 0, p = essid; i < len; i++, p++)
    279 			printf("%02x", *p);
    280 	}
    281 }
    282 
    283 void
    284 ieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi)
    285 {
    286 	const struct ieee80211_frame *wh;
    287 	int i;
    288 
    289 	wh = (const struct ieee80211_frame *)buf;
    290 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    291 	case IEEE80211_FC1_DIR_NODS:
    292 		printf("NODS %s", ether_sprintf(wh->i_addr2));
    293 		printf("->%s", ether_sprintf(wh->i_addr1));
    294 		printf("(%s)", ether_sprintf(wh->i_addr3));
    295 		break;
    296 	case IEEE80211_FC1_DIR_TODS:
    297 		printf("TODS %s", ether_sprintf(wh->i_addr2));
    298 		printf("->%s", ether_sprintf(wh->i_addr3));
    299 		printf("(%s)", ether_sprintf(wh->i_addr1));
    300 		break;
    301 	case IEEE80211_FC1_DIR_FROMDS:
    302 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
    303 		printf("->%s", ether_sprintf(wh->i_addr1));
    304 		printf("(%s)", ether_sprintf(wh->i_addr2));
    305 		break;
    306 	case IEEE80211_FC1_DIR_DSTODS:
    307 		printf("DSDS %s", ether_sprintf((const u_int8_t *)&wh[1]));
    308 		printf("->%s", ether_sprintf(wh->i_addr3));
    309 		printf("(%s", ether_sprintf(wh->i_addr2));
    310 		printf("->%s)", ether_sprintf(wh->i_addr1));
    311 		break;
    312 	}
    313 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
    314 	case IEEE80211_FC0_TYPE_DATA:
    315 		printf(" data");
    316 		break;
    317 	case IEEE80211_FC0_TYPE_MGT:
    318 		printf(" %s", ieee80211_mgt_subtype_name[
    319 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
    320 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
    321 		break;
    322 	default:
    323 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
    324 		break;
    325 	}
    326 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
    327 		printf(" WEP [IV");
    328 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++)
    329 			printf(" %.02x", buf[sizeof(*wh)+i]);
    330 		printf(" KID %u]", buf[sizeof(*wh)+i] >> 6);
    331 	}
    332 	if (rate >= 0)
    333 		printf(" %dM", rate / 2);
    334 	if (rssi >= 0)
    335 		printf(" +%d", rssi);
    336 	printf("\n");
    337 	if (len > 0) {
    338 		for (i = 0; i < len; i++) {
    339 			if ((i & 1) == 0)
    340 				printf(" ");
    341 			printf("%02x", buf[i]);
    342 		}
    343 		printf("\n");
    344 	}
    345 }
    346 
    347 int
    348 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, int flags)
    349 {
    350 #define	RV(v)	((v) & IEEE80211_RATE_VAL)
    351 	int i, j, ignore, error;
    352 	int okrate, badrate, fixedrate;
    353 	struct ieee80211_rateset *srs, *nrs;
    354 	u_int8_t r;
    355 
    356 	/*
    357 	 * If the fixed rate check was requested but no
    358 	 * fixed has been defined then just remove it.
    359 	 */
    360 	if ((flags & IEEE80211_F_DOFRATE) && ic->ic_fixed_rate < 0)
    361 		flags &= ~IEEE80211_F_DOFRATE;
    362 	error = 0;
    363 	okrate = badrate = fixedrate = 0;
    364 	srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    365 	nrs = &ni->ni_rates;
    366 	for (i = 0; i < nrs->rs_nrates; ) {
    367 		ignore = 0;
    368 		if (flags & IEEE80211_F_DOSORT) {
    369 			/*
    370 			 * Sort rates.
    371 			 */
    372 			for (j = i + 1; j < nrs->rs_nrates; j++) {
    373 				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
    374 					r = nrs->rs_rates[i];
    375 					nrs->rs_rates[i] = nrs->rs_rates[j];
    376 					nrs->rs_rates[j] = r;
    377 				}
    378 			}
    379 		}
    380 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
    381 		badrate = r;
    382 		if (flags & IEEE80211_F_DOFRATE) {
    383 			/*
    384 			 * Check any fixed rate is included.
    385 			 */
    386 			if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
    387 				fixedrate = r;
    388 		}
    389 		if (flags & IEEE80211_F_DONEGO) {
    390 			/*
    391 			 * Check against supported rates.
    392 			 */
    393 			for (j = 0; j < srs->rs_nrates; j++) {
    394 				if (r == RV(srs->rs_rates[j])) {
    395 					/*
    396 					 * Overwrite with the supported rate
    397 					 * value so any basic rate bit is set.
    398 					 * This insures that response we send
    399 					 * to stations have the necessary basic
    400 					 * rate bit set.
    401 					 */
    402 					nrs->rs_rates[i] = srs->rs_rates[j];
    403 					break;
    404 				}
    405 			}
    406 			if (j == srs->rs_nrates) {
    407 				/*
    408 				 * A rate in the node's rate set is not
    409 				 * supported.  If this is a basic rate and we
    410 				 * are operating as an AP then this is an error.
    411 				 * Otherwise we just discard/ignore the rate.
    412 				 * Note that this is important for 11b stations
    413 				 * when they want to associate with an 11g AP.
    414 				 */
    415 #ifndef IEEE80211_NO_HOSTAP
    416 				if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    417 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
    418 					error++;
    419 #endif /* !IEEE80211_NO_HOSTAP */
    420 				ignore++;
    421 			}
    422 		}
    423 		if (flags & IEEE80211_F_DODEL) {
    424 			/*
    425 			 * Delete unacceptable rates.
    426 			 */
    427 			if (ignore) {
    428 				nrs->rs_nrates--;
    429 				for (j = i; j < nrs->rs_nrates; j++)
    430 					nrs->rs_rates[j] = nrs->rs_rates[j + 1];
    431 				nrs->rs_rates[j] = 0;
    432 				continue;
    433 			}
    434 		}
    435 		if (!ignore) {
    436 			okrate = nrs->rs_rates[i];
    437 			ni->ni_txrate = i;
    438 		}
    439 		i++;
    440 	}
    441 	if (okrate == 0 || error != 0 ||
    442 	    ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
    443 		return badrate | IEEE80211_RATE_BASIC;
    444 	else
    445 		return RV(okrate);
    446 #undef RV
    447 }
    448 
    449 /*
    450  * Reset 11g-related state.
    451  */
    452 void
    453 ieee80211_reset_erp(struct ieee80211com *ic)
    454 {
    455 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
    456 	ic->ic_nonerpsta = 0;
    457 	ic->ic_longslotsta = 0;
    458 	/*
    459 	 * Short slot time is enabled only when operating in 11g
    460 	 * and not in an IBSS.  We must also honor whether or not
    461 	 * the driver is capable of doing it.
    462 	 */
    463 	ieee80211_set_shortslottime(ic,
    464 		ic->ic_curmode == IEEE80211_MODE_11A ||
    465 		(ic->ic_curmode == IEEE80211_MODE_11G &&
    466 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
    467 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
    468 	/*
    469 	 * Set short preamble and ERP barker-preamble flags.
    470 	 */
    471 	if (ic->ic_curmode == IEEE80211_MODE_11A ||
    472 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
    473 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    474 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
    475 	} else {
    476 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
    477 		ic->ic_flags |= IEEE80211_F_USEBARKER;
    478 	}
    479 }
    480 
    481 /*
    482  * Set the short slot time state and notify the driver.
    483  */
    484 void
    485 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
    486 {
    487 	if (onoff)
    488 		ic->ic_flags |= IEEE80211_F_SHSLOT;
    489 	else
    490 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
    491 	/* notify driver */
    492 	if (ic->ic_updateslot != NULL)
    493 		ic->ic_updateslot(ic->ic_ifp);
    494 }
    495 
    496 /*
    497  * Check if the specified rate set supports ERP.
    498  * NB: the rate set is assumed to be sorted.
    499  */
    500 int
    501 ieee80211_iserp_rateset(struct ieee80211com *ic, struct ieee80211_rateset *rs)
    502 {
    503 #define N(a)	(sizeof(a) / sizeof(a[0]))
    504 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
    505 	int i, j;
    506 
    507 	if (rs->rs_nrates < N(rates))
    508 		return 0;
    509 	for (i = 0; i < N(rates); i++) {
    510 		for (j = 0; j < rs->rs_nrates; j++) {
    511 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
    512 			if (rates[i] == r)
    513 				goto next;
    514 			if (r > rates[i])
    515 				return 0;
    516 		}
    517 		return 0;
    518 	next:
    519 		;
    520 	}
    521 	return 1;
    522 #undef N
    523 }
    524 
    525 /*
    526  * Mark the basic rates for the 11g rate table based on the
    527  * operating mode.  For real 11g we mark all the 11b rates
    528  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    529  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    530  * the basic OFDM rates.
    531  */
    532 void
    533 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
    534 {
    535 	static const struct ieee80211_rateset basic[] = {
    536 	    { 0 },			/* IEEE80211_MODE_AUTO */
    537 	    { 3, { 12, 24, 48 } },	/* IEEE80211_MODE_11A */
    538 	    { 2, { 2, 4 } },		/* IEEE80211_MODE_11B */
    539 	    { 4, { 2, 4, 11, 22 } },	/* IEEE80211_MODE_11G (mixed b/g) */
    540 	    { 0 },			/* IEEE80211_MODE_FH */
    541 					/* IEEE80211_MODE_PUREG (not yet) */
    542 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },
    543 	};
    544 	int i, j;
    545 
    546 	for (i = 0; i < rs->rs_nrates; i++) {
    547 		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    548 		for (j = 0; j < basic[mode].rs_nrates; j++)
    549 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    550 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    551 				break;
    552 			}
    553 	}
    554 }
    555 
    556 /*
    557  * WME protocol support.  The following parameters come from the spec.
    558  */
    559 typedef struct phyParamType {
    560 	u_int8_t aifsn;
    561 	u_int8_t logcwmin;
    562 	u_int8_t logcwmax;
    563 	u_int16_t txopLimit;
    564 	u_int8_t acm;
    565 } paramType;
    566 
    567 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
    568 	{ 3, 4, 6 },		/* IEEE80211_MODE_AUTO */
    569 	{ 3, 4, 6 },		/* IEEE80211_MODE_11A */
    570 	{ 3, 5, 7 },		/* IEEE80211_MODE_11B */
    571 	{ 3, 4, 6 },		/* IEEE80211_MODE_11G */
    572 	{ 3, 5, 7 },		/* IEEE80211_MODE_FH */
    573 	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_A */
    574 	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_G */
    575 };
    576 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
    577 	{ 7, 4, 10 },		/* IEEE80211_MODE_AUTO */
    578 	{ 7, 4, 10 },		/* IEEE80211_MODE_11A */
    579 	{ 7, 5, 10 },		/* IEEE80211_MODE_11B */
    580 	{ 7, 4, 10 },		/* IEEE80211_MODE_11G */
    581 	{ 7, 5, 10 },		/* IEEE80211_MODE_FH */
    582 	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
    583 	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
    584 };
    585 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
    586 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
    587 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11A */
    588 	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_11B */
    589 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11G */
    590 	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_FH */
    591 	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
    592 	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
    593 };
    594 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
    595 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
    596 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11A */
    597 	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_11B */
    598 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11G */
    599 	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_FH */
    600 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
    601 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
    602 };
    603 
    604 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
    605 	{ 3, 4, 10 },		/* IEEE80211_MODE_AUTO */
    606 	{ 3, 4, 10 },		/* IEEE80211_MODE_11A */
    607 	{ 3, 5, 10 },		/* IEEE80211_MODE_11B */
    608 	{ 3, 4, 10 },		/* IEEE80211_MODE_11G */
    609 	{ 3, 5, 10 },		/* IEEE80211_MODE_FH */
    610 	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
    611 	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
    612 };
    613 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
    614 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
    615 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11A */
    616 	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_11B */
    617 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11G */
    618 	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_FH */
    619 	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
    620 	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
    621 };
    622 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
    623 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
    624 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11A */
    625 	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_11B */
    626 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11G */
    627 	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_FH */
    628 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
    629 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
    630 };
    631 
    632 void
    633 ieee80211_wme_initparams(struct ieee80211com *ic)
    634 {
    635 	struct ieee80211_wme_state *wme = &ic->ic_wme;
    636 	const paramType *pPhyParam, *pBssPhyParam;
    637 	struct wmeParams *wmep;
    638 	int i;
    639 
    640 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
    641 		return;
    642 
    643 	for (i = 0; i < WME_NUM_AC; i++) {
    644 		switch (i) {
    645 		case WME_AC_BK:
    646 			pPhyParam = &phyParamForAC_BK[ic->ic_curmode];
    647 			pBssPhyParam = &phyParamForAC_BK[ic->ic_curmode];
    648 			break;
    649 		case WME_AC_VI:
    650 			pPhyParam = &phyParamForAC_VI[ic->ic_curmode];
    651 			pBssPhyParam = &bssPhyParamForAC_VI[ic->ic_curmode];
    652 			break;
    653 		case WME_AC_VO:
    654 			pPhyParam = &phyParamForAC_VO[ic->ic_curmode];
    655 			pBssPhyParam = &bssPhyParamForAC_VO[ic->ic_curmode];
    656 			break;
    657 		case WME_AC_BE:
    658 		default:
    659 			pPhyParam = &phyParamForAC_BE[ic->ic_curmode];
    660 			pBssPhyParam = &bssPhyParamForAC_BE[ic->ic_curmode];
    661 			break;
    662 		}
    663 
    664 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
    665 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    666 			wmep->wmep_acm = pPhyParam->acm;
    667 			wmep->wmep_aifsn = pPhyParam->aifsn;
    668 			wmep->wmep_logcwmin = pPhyParam->logcwmin;
    669 			wmep->wmep_logcwmax = pPhyParam->logcwmax;
    670 			wmep->wmep_txopLimit = pPhyParam->txopLimit;
    671 		} else {
    672 			wmep->wmep_acm = pBssPhyParam->acm;
    673 			wmep->wmep_aifsn = pBssPhyParam->aifsn;
    674 			wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
    675 			wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
    676 			wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
    677 
    678 		}
    679 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    680 			"%s: %s chan [acm %u aifsn %u log2(cwmin) %u "
    681 			"log2(cwmax) %u txpoLimit %u]\n", __func__
    682 			, ieee80211_wme_acnames[i]
    683 			, wmep->wmep_acm
    684 			, wmep->wmep_aifsn
    685 			, wmep->wmep_logcwmin
    686 			, wmep->wmep_logcwmax
    687 			, wmep->wmep_txopLimit
    688 		);
    689 
    690 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
    691 		wmep->wmep_acm = pBssPhyParam->acm;
    692 		wmep->wmep_aifsn = pBssPhyParam->aifsn;
    693 		wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
    694 		wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
    695 		wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
    696 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    697 			"%s: %s  bss [acm %u aifsn %u log2(cwmin) %u "
    698 			"log2(cwmax) %u txpoLimit %u]\n", __func__
    699 			, ieee80211_wme_acnames[i]
    700 			, wmep->wmep_acm
    701 			, wmep->wmep_aifsn
    702 			, wmep->wmep_logcwmin
    703 			, wmep->wmep_logcwmax
    704 			, wmep->wmep_txopLimit
    705 		);
    706 	}
    707 	/* NB: check ic_bss to avoid NULL deref on initial attach */
    708 	if (ic->ic_bss != NULL) {
    709 		/*
    710 		 * Calculate agressive mode switching threshold based
    711 		 * on beacon interval.  This doesn't need locking since
    712 		 * we're only called before entering the RUN state at
    713 		 * which point we start sending beacon frames.
    714 		 */
    715 		wme->wme_hipri_switch_thresh =
    716 			(HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100;
    717 		ieee80211_wme_updateparams(ic);
    718 	}
    719 }
    720 
    721 /*
    722  * Update WME parameters for ourself and the BSS.
    723  */
    724 void
    725 ieee80211_wme_updateparams_locked(struct ieee80211com *ic)
    726 {
    727 	static const paramType phyParam[IEEE80211_MODE_MAX] = {
    728 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_AUTO */
    729 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11A */
    730 		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_11B */
    731 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11G */
    732 		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_FH */
    733 		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_A */
    734 		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_G */
    735 	};
    736 	struct ieee80211_wme_state *wme = &ic->ic_wme;
    737 	const struct wmeParams *wmep;
    738 	struct wmeParams *chanp, *bssp;
    739 	int i;
    740 
    741        	/* set up the channel access parameters for the physical device */
    742 	for (i = 0; i < WME_NUM_AC; i++) {
    743 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
    744 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
    745 		chanp->wmep_aifsn = wmep->wmep_aifsn;
    746 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
    747 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
    748 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
    749 
    750 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
    751 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
    752 		chanp->wmep_aifsn = wmep->wmep_aifsn;
    753 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
    754 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
    755 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
    756 	}
    757 
    758 	/*
    759 	 * This implements agressive mode as found in certain
    760 	 * vendors' AP's.  When there is significant high
    761 	 * priority (VI/VO) traffic in the BSS throttle back BE
    762 	 * traffic by using conservative parameters.  Otherwise
    763 	 * BE uses agressive params to optimize performance of
    764 	 * legacy/non-QoS traffic.
    765 	 */
    766         if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
    767 	     (wme->wme_flags & WME_F_AGGRMODE) == 0) ||
    768 	    (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    769 	     (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
    770 	    (ic->ic_flags & IEEE80211_F_WME) == 0) {
    771 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
    772 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
    773 
    774 		chanp->wmep_aifsn = bssp->wmep_aifsn =
    775 			phyParam[ic->ic_curmode].aifsn;
    776 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
    777 			phyParam[ic->ic_curmode].logcwmin;
    778 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
    779 			phyParam[ic->ic_curmode].logcwmax;
    780 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
    781 			(ic->ic_caps & IEEE80211_C_BURST) ?
    782 				phyParam[ic->ic_curmode].txopLimit : 0;
    783 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    784 			"%s: %s [acm %u aifsn %u log2(cwmin) %u "
    785 			"log2(cwmax) %u txpoLimit %u]\n", __func__
    786 			, ieee80211_wme_acnames[WME_AC_BE]
    787 			, chanp->wmep_acm
    788 			, chanp->wmep_aifsn
    789 			, chanp->wmep_logcwmin
    790 			, chanp->wmep_logcwmax
    791 			, chanp->wmep_txopLimit
    792 		);
    793 	}
    794 
    795 #ifndef IEEE80211_NO_HOSTAP
    796 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    797 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) == 0) {
    798         	static const u_int8_t logCwMin[IEEE80211_MODE_MAX] = {
    799               		3,	/* IEEE80211_MODE_AUTO */
    800               		3,	/* IEEE80211_MODE_11A */
    801               		4,	/* IEEE80211_MODE_11B */
    802               		3,	/* IEEE80211_MODE_11G */
    803               		4,	/* IEEE80211_MODE_FH */
    804               		3,	/* IEEE80211_MODE_TURBO_A */
    805               		3,	/* IEEE80211_MODE_TURBO_G */
    806 		};
    807 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
    808 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
    809 
    810 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
    811 			logCwMin[ic->ic_curmode];
    812 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    813 			"%s: %s log2(cwmin) %u\n", __func__
    814 			, ieee80211_wme_acnames[WME_AC_BE]
    815 			, chanp->wmep_logcwmin
    816 		);
    817     	}
    818 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
    819 		/*
    820 		 * Arrange for a beacon update and bump the parameter
    821 		 * set number so associated stations load the new values.
    822 		 */
    823 		wme->wme_bssChanParams.cap_info =
    824 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
    825 		ic->ic_flags |= IEEE80211_F_WMEUPDATE;
    826 	}
    827 #endif /* !IEEE80211_NO_HOSTAP */
    828 
    829 	wme->wme_update(ic);
    830 
    831 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    832 		"%s: WME params updated, cap_info 0x%x\n", __func__,
    833 		ic->ic_opmode == IEEE80211_M_STA ?
    834 			wme->wme_wmeChanParams.cap_info :
    835 			wme->wme_bssChanParams.cap_info);
    836 }
    837 
    838 void
    839 ieee80211_wme_updateparams(struct ieee80211com *ic)
    840 {
    841 
    842 	if (ic->ic_caps & IEEE80211_C_WME) {
    843 		IEEE80211_BEACON_LOCK(ic);
    844 		ieee80211_wme_updateparams_locked(ic);
    845 		IEEE80211_BEACON_UNLOCK(ic);
    846 	}
    847 }
    848 
    849 static void
    850 sta_disassoc(void *arg, struct ieee80211_node *ni)
    851 {
    852 	struct ieee80211com *ic = arg;
    853 
    854 	if (ni->ni_associd != 0) {
    855 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
    856 			IEEE80211_REASON_ASSOC_LEAVE);
    857 		ieee80211_node_leave(ic, ni);
    858 	}
    859 }
    860 
    861 static void
    862 sta_deauth(void *arg, struct ieee80211_node *ni)
    863 {
    864 	struct ieee80211com *ic = arg;
    865 
    866 	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
    867 		IEEE80211_REASON_ASSOC_LEAVE);
    868 }
    869 
    870 static int
    871 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    872 {
    873 	struct ifnet *ifp = ic->ic_ifp;
    874 	struct ieee80211_node *ni;
    875 	enum ieee80211_state ostate;
    876 
    877 	ostate = ic->ic_state;
    878 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
    879 		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
    880 	ic->ic_state = nstate;			/* state transition */
    881 	ni = ic->ic_bss;			/* NB: no reference held */
    882 	switch (nstate) {
    883 	case IEEE80211_S_INIT:
    884 		switch (ostate) {
    885 		case IEEE80211_S_INIT:
    886 			break;
    887 		case IEEE80211_S_RUN:
    888 			switch (ic->ic_opmode) {
    889 			case IEEE80211_M_STA:
    890 				IEEE80211_SEND_MGMT(ic, ni,
    891 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
    892 				    IEEE80211_REASON_ASSOC_LEAVE);
    893 				ieee80211_sta_leave(ic, ni);
    894 				break;
    895 			case IEEE80211_M_HOSTAP:
    896 #ifndef IEEE80211_NO_HOSTAP
    897 				ieee80211_iterate_nodes(&ic->ic_sta,
    898 					sta_disassoc, ic);
    899 #endif /* !IEEE80211_NO_HOSTAP */
    900 				break;
    901 			default:
    902 				break;
    903 			}
    904 			goto reset;
    905 		case IEEE80211_S_ASSOC:
    906 			switch (ic->ic_opmode) {
    907 			case IEEE80211_M_STA:
    908 				IEEE80211_SEND_MGMT(ic, ni,
    909 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
    910 				    IEEE80211_REASON_AUTH_LEAVE);
    911 				break;
    912 			case IEEE80211_M_HOSTAP:
    913 #ifndef IEEE80211_NO_HOSTAP
    914 				ieee80211_iterate_nodes(&ic->ic_sta,
    915 					sta_deauth, ic);
    916 #endif /* !IEEE80211_NO_HOSTAP */
    917 				break;
    918 			default:
    919 				break;
    920 			}
    921 			goto reset;
    922 		case IEEE80211_S_SCAN:
    923 			ieee80211_cancel_scan(ic);
    924 			goto reset;
    925 		case IEEE80211_S_AUTH:
    926 		reset:
    927 			ic->ic_mgt_timer = 0;
    928 			IF_PURGE(&ic->ic_mgtq);
    929 			ieee80211_reset_bss(ic);
    930 			break;
    931 		}
    932 		if (ic->ic_auth->ia_detach != NULL)
    933 			ic->ic_auth->ia_detach(ic);
    934 		break;
    935 	case IEEE80211_S_SCAN:
    936 		switch (ostate) {
    937 		case IEEE80211_S_INIT:
    938 			if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
    939 			     ic->ic_opmode == IEEE80211_M_IBSS ||
    940 			     ic->ic_opmode == IEEE80211_M_AHDEMO) &&
    941 			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
    942 				/*
    943 				 * AP operation and we already have a channel;
    944 				 * bypass the scan and startup immediately.
    945 				 */
    946 				ieee80211_create_ibss(ic, ic->ic_des_chan);
    947 			} else {
    948 				ieee80211_begin_scan(ic, arg);
    949 			}
    950 			break;
    951 		case IEEE80211_S_SCAN:
    952 			/*
    953 			 * Scan next. If doing an active scan and the
    954 			 * channel is not marked passive-only then send
    955 			 * a probe request.  Otherwise just listen for
    956 			 * beacons on the channel.
    957 			 */
    958 			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
    959 			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
    960 				IEEE80211_SEND_MGMT(ic, ni,
    961 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
    962 			}
    963 			break;
    964 		case IEEE80211_S_RUN:
    965 			/* beacon miss */
    966 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE,
    967 				"no recent beacons from %s; rescanning\n",
    968 				ether_sprintf(ic->ic_bss->ni_bssid));
    969 			ieee80211_sta_leave(ic, ni);
    970 			ic->ic_flags &= ~IEEE80211_F_SIBSS;	/* XXX */
    971 			/* FALLTHRU */
    972 		case IEEE80211_S_AUTH:
    973 		case IEEE80211_S_ASSOC:
    974 			/* timeout restart scan */
    975 			ni = ieee80211_find_node(&ic->ic_scan,
    976 				ic->ic_bss->ni_macaddr);
    977 			if (ni != NULL) {
    978 				ni->ni_fails++;
    979 				ieee80211_unref_node(&ni);
    980 			}
    981 			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
    982 				ieee80211_begin_scan(ic, arg);
    983 			break;
    984 		}
    985 		break;
    986 	case IEEE80211_S_AUTH:
    987 		switch (ostate) {
    988 		case IEEE80211_S_INIT:
    989 		case IEEE80211_S_SCAN:
    990 			IEEE80211_SEND_MGMT(ic, ni,
    991 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
    992 			break;
    993 		case IEEE80211_S_AUTH:
    994 		case IEEE80211_S_ASSOC:
    995 			switch (arg) {
    996 			case IEEE80211_FC0_SUBTYPE_AUTH:
    997 				/* ??? */
    998 				IEEE80211_SEND_MGMT(ic, ni,
    999 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
   1000 				break;
   1001 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
   1002 				/* ignore and retry scan on timeout */
   1003 				break;
   1004 			}
   1005 			break;
   1006 		case IEEE80211_S_RUN:
   1007 			switch (arg) {
   1008 			case IEEE80211_FC0_SUBTYPE_AUTH:
   1009 				IEEE80211_SEND_MGMT(ic, ni,
   1010 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
   1011 				ic->ic_state = ostate;	/* stay RUN */
   1012 				break;
   1013 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
   1014 				ieee80211_sta_leave(ic, ni);
   1015 				if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
   1016 					/* try to reauth */
   1017 					IEEE80211_SEND_MGMT(ic, ni,
   1018 					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
   1019 				}
   1020 				break;
   1021 			}
   1022 			break;
   1023 		}
   1024 		break;
   1025 	case IEEE80211_S_ASSOC:
   1026 		switch (ostate) {
   1027 		case IEEE80211_S_INIT:
   1028 		case IEEE80211_S_SCAN:
   1029 		case IEEE80211_S_ASSOC:
   1030 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
   1031 				"%s: invalid transition\n", __func__);
   1032 			break;
   1033 		case IEEE80211_S_AUTH:
   1034 			IEEE80211_SEND_MGMT(ic, ni,
   1035 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
   1036 			break;
   1037 		case IEEE80211_S_RUN:
   1038 			ieee80211_sta_leave(ic, ni);
   1039 			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
   1040 				IEEE80211_SEND_MGMT(ic, ni,
   1041 				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
   1042 			}
   1043 			break;
   1044 		}
   1045 		break;
   1046 	case IEEE80211_S_RUN:
   1047 		if (ic->ic_flags & IEEE80211_F_WPA) {
   1048 			/* XXX validate prerequisites */
   1049 		}
   1050 		switch (ostate) {
   1051 		case IEEE80211_S_INIT:
   1052 			if (ic->ic_opmode == IEEE80211_M_MONITOR)
   1053 				break;
   1054 			/* fall thru... */
   1055 		case IEEE80211_S_AUTH:
   1056 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
   1057 				"%s: invalid transition\n", __func__);
   1058 			/* fall thru... */
   1059 		case IEEE80211_S_RUN:
   1060 			break;
   1061 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
   1062 		case IEEE80211_S_ASSOC:		/* infra mode */
   1063 			IASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
   1064 				("%s: bogus xmit rate %u setup\n", __func__,
   1065 					ni->ni_txrate));
   1066 #ifdef IEEE80211_DEBUG
   1067 			if (ieee80211_msg_debug(ic)) {
   1068 				if (ic->ic_opmode == IEEE80211_M_STA)
   1069 					if_printf(ifp, "associated ");
   1070 				else
   1071 					if_printf(ifp, "synchronized ");
   1072 				printf("with %s ssid ",
   1073 				    ether_sprintf(ni->ni_bssid));
   1074 				ieee80211_print_essid(ic->ic_bss->ni_essid,
   1075 				    ni->ni_esslen);
   1076 				printf(" channel %d start %uMb\n",
   1077 					ieee80211_chan2ieee(ic, ni->ni_chan),
   1078 					IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
   1079 			}
   1080 #endif
   1081 			ic->ic_mgt_timer = 0;
   1082 			if (ic->ic_opmode == IEEE80211_M_STA)
   1083 				ieee80211_notify_node_join(ic, ni,
   1084 					arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
   1085 			(*ifp->if_start)(ifp);	/* XXX not authorized yet */
   1086 			break;
   1087 		}
   1088 		/*
   1089 		 * Start/stop the authenticator when operating as an
   1090 		 * AP.  We delay until here to allow configuration to
   1091 		 * happen out of order.
   1092 		 */
   1093 		if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */
   1094 		    ic->ic_auth->ia_attach != NULL) {
   1095 			/* XXX check failure */
   1096 			ic->ic_auth->ia_attach(ic);
   1097 		} else if (ic->ic_auth->ia_detach != NULL) {
   1098 			ic->ic_auth->ia_detach(ic);
   1099 		}
   1100 		/*
   1101 		 * When 802.1x is not in use mark the port authorized
   1102 		 * at this point so traffic can flow.
   1103 		 */
   1104 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
   1105 			ieee80211_node_authorize(ic, ni);
   1106 		/*
   1107 		 * Enable inactivity processing.
   1108 		 * XXX
   1109 		 */
   1110 		ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT;
   1111 		ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT;
   1112 		break;
   1113 	}
   1114 	return 0;
   1115 }
   1116