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