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