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