xref: /freebsd/sys/net80211/ieee80211_proto.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
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 	IF_DRAIN(&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 int
326 ieee80211_fix_rate(struct ieee80211_node *ni, int flags)
327 {
328 #define	RV(v)	((v) & IEEE80211_RATE_VAL)
329 	struct ieee80211com *ic = ni->ni_ic;
330 	int i, j, ignore, error;
331 	int okrate, badrate, fixedrate;
332 	const struct ieee80211_rateset *srs;
333 	struct ieee80211_rateset *nrs;
334 	u_int8_t r;
335 
336 	/*
337 	 * If the fixed rate check was requested but no
338 	 * fixed has been defined then just remove it.
339 	 */
340 	if ((flags & IEEE80211_F_DOFRATE) &&
341 	    ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
342 		flags &= ~IEEE80211_F_DOFRATE;
343 	error = 0;
344 	okrate = badrate = fixedrate = 0;
345 	srs = ieee80211_get_suprates(ic, ni->ni_chan);
346 	nrs = &ni->ni_rates;
347 	for (i = 0; i < nrs->rs_nrates; ) {
348 		ignore = 0;
349 		if (flags & IEEE80211_F_DOSORT) {
350 			/*
351 			 * Sort rates.
352 			 */
353 			for (j = i + 1; j < nrs->rs_nrates; j++) {
354 				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
355 					r = nrs->rs_rates[i];
356 					nrs->rs_rates[i] = nrs->rs_rates[j];
357 					nrs->rs_rates[j] = r;
358 				}
359 			}
360 		}
361 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
362 		badrate = r;
363 		if (flags & IEEE80211_F_DOFRATE) {
364 			/*
365 			 * Check any fixed rate is included.
366 			 */
367 			if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
368 				fixedrate = r;
369 		}
370 		if (flags & IEEE80211_F_DONEGO) {
371 			/*
372 			 * Check against supported rates.
373 			 */
374 			for (j = 0; j < srs->rs_nrates; j++) {
375 				if (r == RV(srs->rs_rates[j])) {
376 					/*
377 					 * Overwrite with the supported rate
378 					 * value so any basic rate bit is set.
379 					 * This insures that response we send
380 					 * to stations have the necessary basic
381 					 * rate bit set.
382 					 */
383 					nrs->rs_rates[i] = srs->rs_rates[j];
384 					break;
385 				}
386 			}
387 			if (j == srs->rs_nrates) {
388 				/*
389 				 * A rate in the node's rate set is not
390 				 * supported.  If this is a basic rate and we
391 				 * are operating as an AP then this is an error.
392 				 * Otherwise we just discard/ignore the rate.
393 				 * Note that this is important for 11b stations
394 				 * when they want to associate with an 11g AP.
395 				 */
396 				if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
397 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
398 					error++;
399 				ignore++;
400 			}
401 		}
402 		if (flags & IEEE80211_F_DODEL) {
403 			/*
404 			 * Delete unacceptable rates.
405 			 */
406 			if (ignore) {
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 		}
414 		if (!ignore)
415 			okrate = nrs->rs_rates[i];
416 		i++;
417 	}
418 	if (okrate == 0 || error != 0 ||
419 	    ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
420 		return badrate | IEEE80211_RATE_BASIC;
421 	else
422 		return RV(okrate);
423 #undef RV
424 }
425 
426 /*
427  * Reset 11g-related state.
428  */
429 void
430 ieee80211_reset_erp(struct ieee80211com *ic)
431 {
432 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
433 	ic->ic_nonerpsta = 0;
434 	ic->ic_longslotsta = 0;
435 	/*
436 	 * Short slot time is enabled only when operating in 11g
437 	 * and not in an IBSS.  We must also honor whether or not
438 	 * the driver is capable of doing it.
439 	 */
440 	ieee80211_set_shortslottime(ic,
441 		ic->ic_curmode == IEEE80211_MODE_11A ||
442 		(ic->ic_curmode == IEEE80211_MODE_11G &&
443 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
444 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
445 	/*
446 	 * Set short preamble and ERP barker-preamble flags.
447 	 */
448 	if (ic->ic_curmode == IEEE80211_MODE_11A ||
449 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
450 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
451 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
452 	} else {
453 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
454 		ic->ic_flags |= IEEE80211_F_USEBARKER;
455 	}
456 }
457 
458 /*
459  * Set the short slot time state and notify the driver.
460  */
461 void
462 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
463 {
464 	if (onoff)
465 		ic->ic_flags |= IEEE80211_F_SHSLOT;
466 	else
467 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
468 	/* notify driver */
469 	if (ic->ic_updateslot != NULL)
470 		ic->ic_updateslot(ic->ic_ifp);
471 }
472 
473 /*
474  * Check if the specified rate set supports ERP.
475  * NB: the rate set is assumed to be sorted.
476  */
477 int
478 ieee80211_iserp_rateset(struct ieee80211com *ic, struct ieee80211_rateset *rs)
479 {
480 #define N(a)	(sizeof(a) / sizeof(a[0]))
481 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
482 	int i, j;
483 
484 	if (rs->rs_nrates < N(rates))
485 		return 0;
486 	for (i = 0; i < N(rates); i++) {
487 		for (j = 0; j < rs->rs_nrates; j++) {
488 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
489 			if (rates[i] == r)
490 				goto next;
491 			if (r > rates[i])
492 				return 0;
493 		}
494 		return 0;
495 	next:
496 		;
497 	}
498 	return 1;
499 #undef N
500 }
501 
502 /*
503  * Mark the basic rates for the 11g rate table based on the
504  * operating mode.  For real 11g we mark all the 11b rates
505  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
506  * 11b rates.  There's also a pseudo 11a-mode used to mark only
507  * the basic OFDM rates.
508  */
509 void
510 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
511 {
512 	static const struct ieee80211_rateset basic[] = {
513 	    { 0 },			/* IEEE80211_MODE_AUTO */
514 	    { 3, { 12, 24, 48 } },	/* IEEE80211_MODE_11A */
515 	    { 2, { 2, 4 } },		/* IEEE80211_MODE_11B */
516 	    { 4, { 2, 4, 11, 22 } },	/* IEEE80211_MODE_11G (mixed b/g) */
517 	    { 0 },			/* IEEE80211_MODE_FH */
518 					/* IEEE80211_MODE_PUREG (not yet) */
519 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },
520 	};
521 	int i, j;
522 
523 	for (i = 0; i < rs->rs_nrates; i++) {
524 		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
525 		for (j = 0; j < basic[mode].rs_nrates; j++)
526 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
527 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
528 				break;
529 			}
530 	}
531 }
532 
533 /*
534  * WME protocol support.  The following parameters come from the spec.
535  */
536 typedef struct phyParamType {
537 	u_int8_t aifsn;
538 	u_int8_t logcwmin;
539 	u_int8_t logcwmax;
540 	u_int16_t txopLimit;
541 	u_int8_t acm;
542 } paramType;
543 
544 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
545 	{ 3, 4, 6 },		/* IEEE80211_MODE_AUTO */
546 	{ 3, 4, 6 },		/* IEEE80211_MODE_11A */
547 	{ 3, 5, 7 },		/* IEEE80211_MODE_11B */
548 	{ 3, 4, 6 },		/* IEEE80211_MODE_11G */
549 	{ 3, 5, 7 },		/* IEEE80211_MODE_FH */
550 	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_A */
551 	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_G */
552 };
553 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
554 	{ 7, 4, 10 },		/* IEEE80211_MODE_AUTO */
555 	{ 7, 4, 10 },		/* IEEE80211_MODE_11A */
556 	{ 7, 5, 10 },		/* IEEE80211_MODE_11B */
557 	{ 7, 4, 10 },		/* IEEE80211_MODE_11G */
558 	{ 7, 5, 10 },		/* IEEE80211_MODE_FH */
559 	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
560 	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
561 };
562 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
563 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
564 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11A */
565 	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_11B */
566 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11G */
567 	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_FH */
568 	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
569 	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
570 };
571 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
572 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
573 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11A */
574 	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_11B */
575 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11G */
576 	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_FH */
577 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
578 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
579 };
580 
581 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
582 	{ 3, 4, 10 },		/* IEEE80211_MODE_AUTO */
583 	{ 3, 4, 10 },		/* IEEE80211_MODE_11A */
584 	{ 3, 5, 10 },		/* IEEE80211_MODE_11B */
585 	{ 3, 4, 10 },		/* IEEE80211_MODE_11G */
586 	{ 3, 5, 10 },		/* IEEE80211_MODE_FH */
587 	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
588 	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
589 };
590 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
591 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
592 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11A */
593 	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_11B */
594 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11G */
595 	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_FH */
596 	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
597 	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
598 };
599 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
600 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
601 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11A */
602 	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_11B */
603 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11G */
604 	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_FH */
605 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
606 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
607 };
608 
609 void
610 ieee80211_wme_initparams(struct ieee80211com *ic)
611 {
612 	struct ieee80211_wme_state *wme = &ic->ic_wme;
613 	const paramType *pPhyParam, *pBssPhyParam;
614 	struct wmeParams *wmep;
615 	int i;
616 
617 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
618 		return;
619 
620 	for (i = 0; i < WME_NUM_AC; i++) {
621 		switch (i) {
622 		case WME_AC_BK:
623 			pPhyParam = &phyParamForAC_BK[ic->ic_curmode];
624 			pBssPhyParam = &phyParamForAC_BK[ic->ic_curmode];
625 			break;
626 		case WME_AC_VI:
627 			pPhyParam = &phyParamForAC_VI[ic->ic_curmode];
628 			pBssPhyParam = &bssPhyParamForAC_VI[ic->ic_curmode];
629 			break;
630 		case WME_AC_VO:
631 			pPhyParam = &phyParamForAC_VO[ic->ic_curmode];
632 			pBssPhyParam = &bssPhyParamForAC_VO[ic->ic_curmode];
633 			break;
634 		case WME_AC_BE:
635 		default:
636 			pPhyParam = &phyParamForAC_BE[ic->ic_curmode];
637 			pBssPhyParam = &bssPhyParamForAC_BE[ic->ic_curmode];
638 			break;
639 		}
640 
641 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
642 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
643 			wmep->wmep_acm = pPhyParam->acm;
644 			wmep->wmep_aifsn = pPhyParam->aifsn;
645 			wmep->wmep_logcwmin = pPhyParam->logcwmin;
646 			wmep->wmep_logcwmax = pPhyParam->logcwmax;
647 			wmep->wmep_txopLimit = pPhyParam->txopLimit;
648 		} else {
649 			wmep->wmep_acm = pBssPhyParam->acm;
650 			wmep->wmep_aifsn = pBssPhyParam->aifsn;
651 			wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
652 			wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
653 			wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
654 
655 		}
656 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
657 			"%s: %s chan [acm %u aifsn %u log2(cwmin) %u "
658 			"log2(cwmax) %u txpoLimit %u]\n", __func__
659 			, ieee80211_wme_acnames[i]
660 			, wmep->wmep_acm
661 			, wmep->wmep_aifsn
662 			, wmep->wmep_logcwmin
663 			, wmep->wmep_logcwmax
664 			, wmep->wmep_txopLimit
665 		);
666 
667 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
668 		wmep->wmep_acm = pBssPhyParam->acm;
669 		wmep->wmep_aifsn = pBssPhyParam->aifsn;
670 		wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
671 		wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
672 		wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
673 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
674 			"%s: %s  bss [acm %u aifsn %u log2(cwmin) %u "
675 			"log2(cwmax) %u txpoLimit %u]\n", __func__
676 			, ieee80211_wme_acnames[i]
677 			, wmep->wmep_acm
678 			, wmep->wmep_aifsn
679 			, wmep->wmep_logcwmin
680 			, wmep->wmep_logcwmax
681 			, wmep->wmep_txopLimit
682 		);
683 	}
684 	/* NB: check ic_bss to avoid NULL deref on initial attach */
685 	if (ic->ic_bss != NULL) {
686 		/*
687 		 * Calculate agressive mode switching threshold based
688 		 * on beacon interval.  This doesn't need locking since
689 		 * we're only called before entering the RUN state at
690 		 * which point we start sending beacon frames.
691 		 */
692 		wme->wme_hipri_switch_thresh =
693 			(HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100;
694 		ieee80211_wme_updateparams(ic);
695 	}
696 }
697 
698 /*
699  * Update WME parameters for ourself and the BSS.
700  */
701 void
702 ieee80211_wme_updateparams_locked(struct ieee80211com *ic)
703 {
704 	static const paramType phyParam[IEEE80211_MODE_MAX] = {
705 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_AUTO */
706 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11A */
707 		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_11B */
708 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11G */
709 		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_FH */
710 		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_A */
711 		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_G */
712 	};
713 	struct ieee80211_wme_state *wme = &ic->ic_wme;
714 	const struct wmeParams *wmep;
715 	struct wmeParams *chanp, *bssp;
716 	int i;
717 
718        	/* set up the channel access parameters for the physical device */
719 	for (i = 0; i < WME_NUM_AC; i++) {
720 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
721 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
722 		chanp->wmep_aifsn = wmep->wmep_aifsn;
723 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
724 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
725 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
726 
727 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
728 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
729 		chanp->wmep_aifsn = wmep->wmep_aifsn;
730 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
731 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
732 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
733 	}
734 
735 	/*
736 	 * This implements agressive mode as found in certain
737 	 * vendors' AP's.  When there is significant high
738 	 * priority (VI/VO) traffic in the BSS throttle back BE
739 	 * traffic by using conservative parameters.  Otherwise
740 	 * BE uses agressive params to optimize performance of
741 	 * legacy/non-QoS traffic.
742 	 */
743         if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
744 	     (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
745 	    (ic->ic_opmode == IEEE80211_M_STA &&
746 	     (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
747 	    (ic->ic_flags & IEEE80211_F_WME) == 0) {
748 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
749 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
750 
751 		chanp->wmep_aifsn = bssp->wmep_aifsn =
752 			phyParam[ic->ic_curmode].aifsn;
753 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
754 			phyParam[ic->ic_curmode].logcwmin;
755 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
756 			phyParam[ic->ic_curmode].logcwmax;
757 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
758 			(ic->ic_flags & IEEE80211_F_BURST) ?
759 				phyParam[ic->ic_curmode].txopLimit : 0;
760 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
761 			"%s: %s [acm %u aifsn %u log2(cwmin) %u "
762 			"log2(cwmax) %u txpoLimit %u]\n", __func__
763 			, ieee80211_wme_acnames[WME_AC_BE]
764 			, chanp->wmep_acm
765 			, chanp->wmep_aifsn
766 			, chanp->wmep_logcwmin
767 			, chanp->wmep_logcwmax
768 			, chanp->wmep_txopLimit
769 		);
770 	}
771 
772 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
773 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
774         	static const u_int8_t logCwMin[IEEE80211_MODE_MAX] = {
775               		3,	/* IEEE80211_MODE_AUTO */
776               		3,	/* IEEE80211_MODE_11A */
777               		4,	/* IEEE80211_MODE_11B */
778               		3,	/* IEEE80211_MODE_11G */
779               		4,	/* IEEE80211_MODE_FH */
780               		3,	/* IEEE80211_MODE_TURBO_A */
781               		3,	/* IEEE80211_MODE_TURBO_G */
782 		};
783 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
784 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
785 
786 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
787 			logCwMin[ic->ic_curmode];
788 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
789 			"%s: %s log2(cwmin) %u\n", __func__
790 			, ieee80211_wme_acnames[WME_AC_BE]
791 			, chanp->wmep_logcwmin
792 		);
793     	}
794 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
795 		/*
796 		 * Arrange for a beacon update and bump the parameter
797 		 * set number so associated stations load the new values.
798 		 */
799 		wme->wme_bssChanParams.cap_info =
800 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
801 		ic->ic_flags |= IEEE80211_F_WMEUPDATE;
802 	}
803 
804 	wme->wme_update(ic);
805 
806 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
807 		"%s: WME params updated, cap_info 0x%x\n", __func__,
808 		ic->ic_opmode == IEEE80211_M_STA ?
809 			wme->wme_wmeChanParams.cap_info :
810 			wme->wme_bssChanParams.cap_info);
811 }
812 
813 void
814 ieee80211_wme_updateparams(struct ieee80211com *ic)
815 {
816 
817 	if (ic->ic_caps & IEEE80211_C_WME) {
818 		IEEE80211_BEACON_LOCK(ic);
819 		ieee80211_wme_updateparams_locked(ic);
820 		IEEE80211_BEACON_UNLOCK(ic);
821 	}
822 }
823 
824 void
825 ieee80211_beacon_miss(struct ieee80211com *ic)
826 {
827 
828 	if (ic->ic_flags & IEEE80211_F_SCAN) {
829 		/* XXX check ic_curchan != ic_bsschan? */
830 		return;
831 	}
832 	IEEE80211_DPRINTF(ic,
833 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
834 		"%s\n", "beacon miss");
835 
836 	/*
837 	 * Our handling is only meaningful for stations that are
838 	 * associated; any other conditions else will be handled
839 	 * through different means (e.g. the tx timeout on mgt frames).
840 	 */
841 	if (ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
842 		return;
843 
844 	if (++ic->ic_bmiss_count < ic->ic_bmiss_max) {
845 		/*
846 		 * Send a directed probe req before falling back to a scan;
847 		 * if we receive a response ic_bmiss_count will be reset.
848 		 * Some cards mistakenly report beacon miss so this avoids
849 		 * the expensive scan if the ap is still there.
850 		 */
851 		ieee80211_send_probereq(ic->ic_bss, ic->ic_myaddr,
852 			ic->ic_bss->ni_bssid, ic->ic_bss->ni_bssid,
853 			ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen,
854 			ic->ic_opt_ie, ic->ic_opt_ie_len);
855 		return;
856 	}
857 	ic->ic_bmiss_count = 0;
858 	ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
859 }
860 
861 /*
862  * Software beacon miss handling.  Check if any beacons
863  * were received in the last period.  If not post a
864  * beacon miss; otherwise reset the counter.
865  */
866 static void
867 ieee80211_swbmiss(void *arg)
868 {
869 	struct ieee80211com *ic = arg;
870 
871 	if (ic->ic_swbmiss_count == 0) {
872 		ieee80211_beacon_miss(ic);
873 		if (ic->ic_bmiss_count == 0)	/* don't re-arm timer */
874 			return;
875 	} else
876 		ic->ic_swbmiss_count = 0;
877 	callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
878 		ieee80211_swbmiss, ic);
879 }
880 
881 static void
882 sta_disassoc(void *arg, struct ieee80211_node *ni)
883 {
884 	struct ieee80211com *ic = arg;
885 
886 	if (ni->ni_associd != 0) {
887 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
888 			IEEE80211_REASON_ASSOC_LEAVE);
889 		ieee80211_node_leave(ic, ni);
890 	}
891 }
892 
893 static void
894 sta_deauth(void *arg, struct ieee80211_node *ni)
895 {
896 	struct ieee80211com *ic = arg;
897 
898 	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
899 		IEEE80211_REASON_ASSOC_LEAVE);
900 }
901 
902 static int
903 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
904 {
905 	struct ifnet *ifp = ic->ic_ifp;
906 	struct ieee80211_node *ni;
907 	enum ieee80211_state ostate;
908 
909 	ostate = ic->ic_state;
910 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
911 		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
912 	ic->ic_state = nstate;			/* state transition */
913 	ni = ic->ic_bss;			/* NB: no reference held */
914 	if (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)
915 		callout_stop(&ic->ic_swbmiss);
916 	switch (nstate) {
917 	case IEEE80211_S_INIT:
918 		switch (ostate) {
919 		case IEEE80211_S_INIT:
920 			break;
921 		case IEEE80211_S_RUN:
922 			switch (ic->ic_opmode) {
923 			case IEEE80211_M_STA:
924 				IEEE80211_SEND_MGMT(ic, ni,
925 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
926 				    IEEE80211_REASON_ASSOC_LEAVE);
927 				ieee80211_sta_leave(ic, ni);
928 				break;
929 			case IEEE80211_M_HOSTAP:
930 				ieee80211_iterate_nodes(&ic->ic_sta,
931 					sta_disassoc, ic);
932 				break;
933 			default:
934 				break;
935 			}
936 			goto reset;
937 		case IEEE80211_S_ASSOC:
938 			switch (ic->ic_opmode) {
939 			case IEEE80211_M_STA:
940 				IEEE80211_SEND_MGMT(ic, ni,
941 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
942 				    IEEE80211_REASON_AUTH_LEAVE);
943 				break;
944 			case IEEE80211_M_HOSTAP:
945 				ieee80211_iterate_nodes(&ic->ic_sta,
946 					sta_deauth, ic);
947 				break;
948 			default:
949 				break;
950 			}
951 			goto reset;
952 		case IEEE80211_S_SCAN:
953 			ieee80211_cancel_scan(ic);
954 			goto reset;
955 		case IEEE80211_S_AUTH:
956 		reset:
957 			ic->ic_mgt_timer = 0;
958 			IF_DRAIN(&ic->ic_mgtq);
959 			ieee80211_reset_bss(ic);
960 			break;
961 		}
962 		if (ic->ic_auth->ia_detach != NULL)
963 			ic->ic_auth->ia_detach(ic);
964 		break;
965 	case IEEE80211_S_SCAN:
966 		switch (ostate) {
967 		case IEEE80211_S_INIT:
968 			if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
969 			     ic->ic_opmode == IEEE80211_M_IBSS ||
970 			     ic->ic_opmode == IEEE80211_M_AHDEMO) &&
971 			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
972 				/*
973 				 * AP operation and we already have a channel;
974 				 * bypass the scan and startup immediately.
975 				 */
976 				ieee80211_create_ibss(ic, ic->ic_des_chan);
977 			} else {
978 				ieee80211_begin_scan(ic, arg);
979 			}
980 			break;
981 		case IEEE80211_S_SCAN:
982 			/*
983 			 * Scan next. If doing an active scan probe
984 			 * for the requested ap (if any).
985 			 */
986 			if (ic->ic_flags & IEEE80211_F_ASCAN)
987 				ieee80211_probe_curchan(ic, 0);
988 			break;
989 		case IEEE80211_S_RUN:
990 			/* beacon miss */
991 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE,
992 				"no recent beacons from %s; rescanning\n",
993 				ether_sprintf(ic->ic_bss->ni_bssid));
994 			ieee80211_sta_leave(ic, ni);
995 			ic->ic_flags &= ~IEEE80211_F_SIBSS;	/* XXX */
996 			/* FALLTHRU */
997 		case IEEE80211_S_AUTH:
998 		case IEEE80211_S_ASSOC:
999 			/* timeout restart scan */
1000 			ni = ieee80211_find_node(&ic->ic_scan,
1001 				ic->ic_bss->ni_macaddr);
1002 			if (ni != NULL) {
1003 				ni->ni_fails++;
1004 				ieee80211_unref_node(&ni);
1005 			}
1006 			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
1007 				ieee80211_begin_scan(ic, arg);
1008 			break;
1009 		}
1010 		break;
1011 	case IEEE80211_S_AUTH:
1012 		switch (ostate) {
1013 		case IEEE80211_S_INIT:
1014 		case IEEE80211_S_SCAN:
1015 			IEEE80211_SEND_MGMT(ic, ni,
1016 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1017 			break;
1018 		case IEEE80211_S_AUTH:
1019 		case IEEE80211_S_ASSOC:
1020 			switch (arg) {
1021 			case IEEE80211_FC0_SUBTYPE_AUTH:
1022 				/* ??? */
1023 				IEEE80211_SEND_MGMT(ic, ni,
1024 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1025 				break;
1026 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1027 				/* ignore and retry scan on timeout */
1028 				break;
1029 			}
1030 			break;
1031 		case IEEE80211_S_RUN:
1032 			switch (arg) {
1033 			case IEEE80211_FC0_SUBTYPE_AUTH:
1034 				IEEE80211_SEND_MGMT(ic, ni,
1035 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1036 				ic->ic_state = ostate;	/* stay RUN */
1037 				break;
1038 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1039 				ieee80211_sta_leave(ic, ni);
1040 				if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1041 					/* try to reauth */
1042 					IEEE80211_SEND_MGMT(ic, ni,
1043 					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1044 				}
1045 				break;
1046 			}
1047 			break;
1048 		}
1049 		break;
1050 	case IEEE80211_S_ASSOC:
1051 		switch (ostate) {
1052 		case IEEE80211_S_INIT:
1053 		case IEEE80211_S_SCAN:
1054 		case IEEE80211_S_ASSOC:
1055 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1056 				"%s: invalid transition\n", __func__);
1057 			break;
1058 		case IEEE80211_S_AUTH:
1059 			IEEE80211_SEND_MGMT(ic, ni,
1060 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1061 			break;
1062 		case IEEE80211_S_RUN:
1063 			ieee80211_sta_leave(ic, ni);
1064 			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1065 				IEEE80211_SEND_MGMT(ic, ni,
1066 				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
1067 			}
1068 			break;
1069 		}
1070 		break;
1071 	case IEEE80211_S_RUN:
1072 		if (ic->ic_flags & IEEE80211_F_WPA) {
1073 			/* XXX validate prerequisites */
1074 		}
1075 		switch (ostate) {
1076 		case IEEE80211_S_INIT:
1077 			if (ic->ic_opmode == IEEE80211_M_MONITOR)
1078 				break;
1079 			/* fall thru... */
1080 		case IEEE80211_S_AUTH:
1081 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1082 				"%s: invalid transition\n", __func__);
1083 			/* fall thru... */
1084 		case IEEE80211_S_RUN:
1085 			break;
1086 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
1087 		case IEEE80211_S_ASSOC:		/* infra mode */
1088 			KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
1089 				("%s: bogus xmit rate %u setup\n", __func__,
1090 					ni->ni_txrate));
1091 #ifdef IEEE80211_DEBUG
1092 			if (ieee80211_msg_debug(ic)) {
1093 				if (ic->ic_opmode == IEEE80211_M_STA)
1094 					if_printf(ifp, "associated ");
1095 				else
1096 					if_printf(ifp, "synchronized ");
1097 				printf("with %s ssid ",
1098 				    ether_sprintf(ni->ni_bssid));
1099 				ieee80211_print_essid(ic->ic_bss->ni_essid,
1100 				    ni->ni_esslen);
1101 				printf(" channel %d start %uMb\n",
1102 					ieee80211_chan2ieee(ic, ic->ic_curchan),
1103 					IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
1104 			}
1105 #endif
1106 			ic->ic_mgt_timer = 0;
1107 			if (ic->ic_opmode == IEEE80211_M_STA)
1108 				ieee80211_notify_node_join(ic, ni,
1109 					arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1110 			if_start(ifp);		/* XXX not authorized yet */
1111 			break;
1112 		}
1113 		if (ostate != IEEE80211_S_RUN &&
1114 		    ic->ic_opmode == IEEE80211_M_STA &&
1115 		    (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)) {
1116 			/*
1117 			 * Start s/w beacon miss timer for devices w/o
1118 			 * hardware support.  We fudge a bit here since
1119 			 * we're doing this in software.
1120 			 */
1121 			ic->ic_swbmiss_period = IEEE80211_TU_TO_TICKS(
1122 				2 * ic->ic_bmissthreshold * ni->ni_intval);
1123 			ic->ic_swbmiss_count = 0;
1124 			callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
1125 				ieee80211_swbmiss, ic);
1126 		}
1127 		/*
1128 		 * Start/stop the authenticator when operating as an
1129 		 * AP.  We delay until here to allow configuration to
1130 		 * happen out of order.
1131 		 */
1132 		if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */
1133 		    ic->ic_auth->ia_attach != NULL) {
1134 			/* XXX check failure */
1135 			ic->ic_auth->ia_attach(ic);
1136 		} else if (ic->ic_auth->ia_detach != NULL) {
1137 			ic->ic_auth->ia_detach(ic);
1138 		}
1139 		/*
1140 		 * When 802.1x is not in use mark the port authorized
1141 		 * at this point so traffic can flow.
1142 		 */
1143 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
1144 			ieee80211_node_authorize(ni);
1145 		/*
1146 		 * Enable inactivity processing.
1147 		 * XXX
1148 		 */
1149 		ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT;
1150 		ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT;
1151 		break;
1152 	}
1153 	return 0;
1154 }
1155