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