xref: /openbsd-src/sys/net80211/ieee80211_proto.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: ieee80211_proto.c,v 1.96 2019/11/06 13:55:44 stsp Exp $	*/
2 /*	$NetBSD: ieee80211_proto.c,v 1.8 2004/04/30 23:58:20 dyoung Exp $	*/
3 
4 /*-
5  * Copyright (c) 2001 Atsushi Onoe
6  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
7  * Copyright (c) 2008, 2009 Damien Bergamini
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
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 /*
34  * IEEE 802.11 protocol support.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/endian.h>
44 #include <sys/errno.h>
45 #include <sys/sysctl.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_llc.h>
51 #include <net/route.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/if_ether.h>
55 
56 #include <net80211/ieee80211_var.h>
57 #include <net80211/ieee80211_priv.h>
58 
59 const char * const ieee80211_mgt_subtype_name[] = {
60 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
61 	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
62 	"beacon",	"atim",		"disassoc",	"auth",
63 	"deauth",	"action",	"action_noack",	"reserved#15"
64 };
65 const char * const ieee80211_state_name[IEEE80211_S_MAX] = {
66 	"INIT",		/* IEEE80211_S_INIT */
67 	"SCAN",		/* IEEE80211_S_SCAN */
68 	"AUTH",		/* IEEE80211_S_AUTH */
69 	"ASSOC",	/* IEEE80211_S_ASSOC */
70 	"RUN"		/* IEEE80211_S_RUN */
71 };
72 const char * const ieee80211_phymode_name[] = {
73 	"auto",		/* IEEE80211_MODE_AUTO */
74 	"11a",		/* IEEE80211_MODE_11A */
75 	"11b",		/* IEEE80211_MODE_11B */
76 	"11g",		/* IEEE80211_MODE_11G */
77 	"11n",		/* IEEE80211_MODE_11N */
78 };
79 
80 void ieee80211_set_beacon_miss_threshold(struct ieee80211com *);
81 int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
82 
83 void
84 ieee80211_proto_attach(struct ifnet *ifp)
85 {
86 	struct ieee80211com *ic = (void *)ifp;
87 
88 	mq_init(&ic->ic_mgtq, IFQ_MAXLEN, IPL_NET);
89 	mq_init(&ic->ic_pwrsaveq, IFQ_MAXLEN, IPL_NET);
90 
91 	ifp->if_hdrlen = sizeof(struct ieee80211_frame);
92 
93 	ic->ic_rtsthreshold = IEEE80211_RTS_MAX;
94 	ic->ic_fragthreshold = 2346;		/* XXX not used yet */
95 	ic->ic_fixed_rate = -1;			/* no fixed rate */
96 	ic->ic_fixed_mcs = -1;			/* no fixed mcs */
97 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
98 
99 	/* protocol state change handler */
100 	ic->ic_newstate = ieee80211_newstate;
101 
102 	/* initialize management frame handlers */
103 	ic->ic_recv_mgmt = ieee80211_recv_mgmt;
104 	ic->ic_send_mgmt = ieee80211_send_mgmt;
105 }
106 
107 void
108 ieee80211_proto_detach(struct ifnet *ifp)
109 {
110 	struct ieee80211com *ic = (void *)ifp;
111 
112 	mq_purge(&ic->ic_mgtq);
113 	mq_purge(&ic->ic_pwrsaveq);
114 }
115 
116 void
117 ieee80211_print_essid(const u_int8_t *essid, int len)
118 {
119 	int i;
120 	const u_int8_t *p;
121 
122 	if (len > IEEE80211_NWID_LEN)
123 		len = IEEE80211_NWID_LEN;
124 	/* determine printable or not */
125 	for (i = 0, p = essid; i < len; i++, p++) {
126 		if (*p < ' ' || *p > 0x7e)
127 			break;
128 	}
129 	if (i == len) {
130 		printf("\"");
131 		for (i = 0, p = essid; i < len; i++, p++)
132 			printf("%c", *p);
133 		printf("\"");
134 	} else {
135 		printf("0x");
136 		for (i = 0, p = essid; i < len; i++, p++)
137 			printf("%02x", *p);
138 	}
139 }
140 
141 #ifdef IEEE80211_DEBUG
142 void
143 ieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi)
144 {
145 	struct ieee80211_frame *wh;
146 	int i;
147 
148 	wh = (struct ieee80211_frame *)buf;
149 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
150 	case IEEE80211_FC1_DIR_NODS:
151 		printf("NODS %s", ether_sprintf(wh->i_addr2));
152 		printf("->%s", ether_sprintf(wh->i_addr1));
153 		printf("(%s)", ether_sprintf(wh->i_addr3));
154 		break;
155 	case IEEE80211_FC1_DIR_TODS:
156 		printf("TODS %s", ether_sprintf(wh->i_addr2));
157 		printf("->%s", ether_sprintf(wh->i_addr3));
158 		printf("(%s)", ether_sprintf(wh->i_addr1));
159 		break;
160 	case IEEE80211_FC1_DIR_FROMDS:
161 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
162 		printf("->%s", ether_sprintf(wh->i_addr1));
163 		printf("(%s)", ether_sprintf(wh->i_addr2));
164 		break;
165 	case IEEE80211_FC1_DIR_DSTODS:
166 		printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
167 		printf("->%s", ether_sprintf(wh->i_addr3));
168 		printf("(%s", ether_sprintf(wh->i_addr2));
169 		printf("->%s)", ether_sprintf(wh->i_addr1));
170 		break;
171 	}
172 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
173 	case IEEE80211_FC0_TYPE_DATA:
174 		printf(" data");
175 		break;
176 	case IEEE80211_FC0_TYPE_MGT:
177 		printf(" %s", ieee80211_mgt_subtype_name[
178 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
179 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
180 		break;
181 	default:
182 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
183 		break;
184 	}
185 	if (wh->i_fc[1] & IEEE80211_FC1_WEP)
186 		printf(" WEP");
187 	if (rate >= 0)
188 		printf(" %d%sM", rate / 2, (rate & 1) ? ".5" : "");
189 	if (rssi >= 0)
190 		printf(" +%d", rssi);
191 	printf("\n");
192 	if (len > 0) {
193 		for (i = 0; i < len; i++) {
194 			if ((i & 1) == 0)
195 				printf(" ");
196 			printf("%02x", buf[i]);
197 		}
198 		printf("\n");
199 	}
200 }
201 #endif
202 
203 int
204 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
205     int flags)
206 {
207 #define	RV(v)	((v) & IEEE80211_RATE_VAL)
208 	int i, j, ignore, error;
209 	int okrate, badrate, fixedrate;
210 	const struct ieee80211_rateset *srs;
211 	struct ieee80211_rateset *nrs;
212 	u_int8_t r;
213 
214 	/*
215 	 * If the fixed rate check was requested but no fixed rate has been
216 	 * defined then just remove the check.
217 	 */
218 	if ((flags & IEEE80211_F_DOFRATE) && ic->ic_fixed_rate == -1)
219 		flags &= ~IEEE80211_F_DOFRATE;
220 
221 	error = 0;
222 	okrate = badrate = fixedrate = 0;
223 	srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
224 	nrs = &ni->ni_rates;
225 	for (i = 0; i < nrs->rs_nrates; ) {
226 		ignore = 0;
227 		if (flags & IEEE80211_F_DOSORT) {
228 			/*
229 			 * Sort rates.
230 			 */
231 			for (j = i + 1; j < nrs->rs_nrates; j++) {
232 				if (RV(nrs->rs_rates[i]) >
233 				    RV(nrs->rs_rates[j])) {
234 					r = nrs->rs_rates[i];
235 					nrs->rs_rates[i] = nrs->rs_rates[j];
236 					nrs->rs_rates[j] = r;
237 				}
238 			}
239 		}
240 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
241 		badrate = r;
242 		if (flags & IEEE80211_F_DOFRATE) {
243 			/*
244 			 * Check fixed rate is included.
245 			 */
246 			if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
247 				fixedrate = r;
248 		}
249 		if (flags & IEEE80211_F_DONEGO) {
250 			/*
251 			 * Check against supported rates.
252 			 */
253 			for (j = 0; j < srs->rs_nrates; j++) {
254 				if (r == RV(srs->rs_rates[j])) {
255 					/*
256 					 * Overwrite with the supported rate
257 					 * value so any basic rate bit is set.
258 					 * This insures that response we send
259 					 * to stations have the necessary basic
260 					 * rate bit set.
261 					 */
262 					nrs->rs_rates[i] = srs->rs_rates[j];
263 					break;
264 				}
265 			}
266 			if (j == srs->rs_nrates) {
267 				/*
268 				 * A rate in the node's rate set is not
269 				 * supported.  If this is a basic rate and we
270 				 * are operating as an AP then this is an error.
271 				 * Otherwise we just discard/ignore the rate.
272 				 * Note that this is important for 11b stations
273 				 * when they want to associate with an 11g AP.
274 				 */
275 #ifndef IEEE80211_STA_ONLY
276 				if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
277 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
278 					error++;
279 #endif
280 				ignore++;
281 			}
282 		}
283 		if (flags & IEEE80211_F_DODEL) {
284 			/*
285 			 * Delete unacceptable rates.
286 			 */
287 			if (ignore) {
288 				nrs->rs_nrates--;
289 				for (j = i; j < nrs->rs_nrates; j++)
290 					nrs->rs_rates[j] = nrs->rs_rates[j + 1];
291 				nrs->rs_rates[j] = 0;
292 				continue;
293 			}
294 		}
295 		if (!ignore)
296 			okrate = nrs->rs_rates[i];
297 		i++;
298 	}
299 	if (okrate == 0 || error != 0 ||
300 	    ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
301 		return badrate | IEEE80211_RATE_BASIC;
302 	else
303 		return RV(okrate);
304 #undef RV
305 }
306 
307 /*
308  * Reset 11g-related state.
309  */
310 void
311 ieee80211_reset_erp(struct ieee80211com *ic)
312 {
313 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
314 
315 	ieee80211_set_shortslottime(ic,
316 	    ic->ic_curmode == IEEE80211_MODE_11A ||
317 	    (ic->ic_curmode == IEEE80211_MODE_11N &&
318 	    IEEE80211_IS_CHAN_5GHZ(ic->ic_ibss_chan))
319 #ifndef IEEE80211_STA_ONLY
320 	    ||
321 	    ((ic->ic_curmode == IEEE80211_MODE_11G ||
322 	    (ic->ic_curmode == IEEE80211_MODE_11N &&
323 	    IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan))) &&
324 	     ic->ic_opmode == IEEE80211_M_HOSTAP &&
325 	     (ic->ic_caps & IEEE80211_C_SHSLOT))
326 #endif
327 	);
328 
329 	if (ic->ic_curmode == IEEE80211_MODE_11A ||
330 	    (ic->ic_curmode == IEEE80211_MODE_11N &&
331 	    IEEE80211_IS_CHAN_5GHZ(ic->ic_ibss_chan)) ||
332 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE))
333 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
334 	else
335 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
336 }
337 
338 /*
339  * Set the short slot time state and notify the driver.
340  */
341 void
342 ieee80211_set_shortslottime(struct ieee80211com *ic, int on)
343 {
344 	if (on)
345 		ic->ic_flags |= IEEE80211_F_SHSLOT;
346 	else
347 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
348 
349 	/* notify the driver */
350 	if (ic->ic_updateslot != NULL)
351 		ic->ic_updateslot(ic);
352 }
353 
354 /*
355  * This function is called by the 802.1X PACP machine (via an ioctl) when
356  * the transmit key machine (4-Way Handshake for 802.11) should run.
357  */
358 int
359 ieee80211_keyrun(struct ieee80211com *ic, u_int8_t *macaddr)
360 {
361 	struct ieee80211_node *ni = ic->ic_bss;
362 #ifndef IEEE80211_STA_ONLY
363 	struct ieee80211_pmk *pmk;
364 #endif
365 
366 	/* STA must be associated or AP must be ready */
367 	if (ic->ic_state != IEEE80211_S_RUN ||
368 	    !(ic->ic_flags & IEEE80211_F_RSNON))
369 		return ENETDOWN;
370 
371 	ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART;
372 #ifndef IEEE80211_STA_ONLY
373 	if (ic->ic_opmode == IEEE80211_M_STA)
374 #endif
375 		return 0;	/* supplicant only, do nothing */
376 
377 #ifndef IEEE80211_STA_ONLY
378 	/* find the STA with which we must start the key exchange */
379 	if ((ni = ieee80211_find_node(ic, macaddr)) == NULL) {
380 		DPRINTF(("no node found for %s\n", ether_sprintf(macaddr)));
381 		return EINVAL;
382 	}
383 	/* check that the STA is in the correct state */
384 	if (ni->ni_state != IEEE80211_STA_ASSOC ||
385 	    ni->ni_rsn_state != RSNA_AUTHENTICATION_2) {
386 		DPRINTF(("unexpected in state %d\n", ni->ni_rsn_state));
387 		return EINVAL;
388 	}
389 	ni->ni_rsn_state = RSNA_INITPMK;
390 
391 	/* make sure a PMK is available for this STA, otherwise deauth it */
392 	if ((pmk = ieee80211_pmksa_find(ic, ni, NULL)) == NULL) {
393 		DPRINTF(("no PMK available for %s\n", ether_sprintf(macaddr)));
394 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
395 		    IEEE80211_REASON_AUTH_LEAVE);
396 		ieee80211_node_leave(ic, ni);
397 		return EINVAL;
398 	}
399 	memcpy(ni->ni_pmk, pmk->pmk_key, IEEE80211_PMK_LEN);
400 	memcpy(ni->ni_pmkid, pmk->pmk_pmkid, IEEE80211_PMKID_LEN);
401 	ni->ni_flags |= IEEE80211_NODE_PMK;
402 
403 	/* initiate key exchange (4-Way Handshake) with STA */
404 	return ieee80211_send_4way_msg1(ic, ni);
405 #endif	/* IEEE80211_STA_ONLY */
406 }
407 
408 #ifndef IEEE80211_STA_ONLY
409 /*
410  * Initiate a group key handshake with a node.
411  */
412 static void
413 ieee80211_node_gtk_rekey(void *arg, struct ieee80211_node *ni)
414 {
415 	struct ieee80211com *ic = arg;
416 
417 	if (ni->ni_state != IEEE80211_STA_ASSOC ||
418 	    ni->ni_rsn_gstate != RSNA_IDLE)
419 		return;
420 
421 	/* initiate a group key handshake with STA */
422 	ni->ni_flags |= IEEE80211_NODE_REKEY;
423 	if (ieee80211_send_group_msg1(ic, ni) != 0)
424 		ni->ni_flags &= ~IEEE80211_NODE_REKEY;
425 }
426 
427 /*
428  * This function is called in HostAP mode when the group key needs to be
429  * changed.
430  */
431 void
432 ieee80211_setkeys(struct ieee80211com *ic)
433 {
434 	struct ieee80211_key *k;
435 	u_int8_t kid;
436 
437 	/* Swap(GM, GN) */
438 	kid = (ic->ic_def_txkey == 1) ? 2 : 1;
439 	k = &ic->ic_nw_keys[kid];
440 	memset(k, 0, sizeof(*k));
441 	k->k_id = kid;
442 	k->k_cipher = ic->ic_bss->ni_rsngroupcipher;
443 	k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX;
444 	k->k_len = ieee80211_cipher_keylen(k->k_cipher);
445 	arc4random_buf(k->k_key, k->k_len);
446 
447 	if (ic->ic_caps & IEEE80211_C_MFP) {
448 		/* Swap(GM_igtk, GN_igtk) */
449 		kid = (ic->ic_igtk_kid == 4) ? 5 : 4;
450 		k = &ic->ic_nw_keys[kid];
451 		memset(k, 0, sizeof(*k));
452 		k->k_id = kid;
453 		k->k_cipher = ic->ic_bss->ni_rsngroupmgmtcipher;
454 		k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX;
455 		k->k_len = 16;
456 		arc4random_buf(k->k_key, k->k_len);
457 	}
458 
459 	ieee80211_iterate_nodes(ic, ieee80211_node_gtk_rekey, ic);
460 }
461 
462 /*
463  * The group key handshake has been completed with all associated stations.
464  */
465 void
466 ieee80211_setkeysdone(struct ieee80211com *ic)
467 {
468 	u_int8_t kid;
469 
470 	/* install GTK */
471 	kid = (ic->ic_def_txkey == 1) ? 2 : 1;
472 	if ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid]) == 0)
473 		ic->ic_def_txkey = kid;
474 
475 	if (ic->ic_caps & IEEE80211_C_MFP) {
476 		/* install IGTK */
477 		kid = (ic->ic_igtk_kid == 4) ? 5 : 4;
478 		if ((*ic->ic_set_key)(ic, ic->ic_bss,
479 		    &ic->ic_nw_keys[kid]) == 0)
480 			ic->ic_igtk_kid = kid;
481 	}
482 }
483 
484 /*
485  * Group key lifetime has expired, update it.
486  */
487 void
488 ieee80211_gtk_rekey_timeout(void *arg)
489 {
490 	struct ieee80211com *ic = arg;
491 	int s;
492 
493 	s = splnet();
494 	ieee80211_setkeys(ic);
495 	splx(s);
496 
497 	/* re-schedule a GTK rekeying after 3600s */
498 	timeout_add_sec(&ic->ic_rsn_timeout, 3600);
499 }
500 
501 void
502 ieee80211_sa_query_timeout(void *arg)
503 {
504 	struct ieee80211_node *ni = arg;
505 	struct ieee80211com *ic = ni->ni_ic;
506 	int s;
507 
508 	s = splnet();
509 	if (++ni->ni_sa_query_count >= 3) {
510 		ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY;
511 		ni->ni_flags |= IEEE80211_NODE_SA_QUERY_FAILED;
512 	} else	/* retry SA Query Request */
513 		ieee80211_sa_query_request(ic, ni);
514 	splx(s);
515 }
516 
517 /*
518  * Request that a SA Query Request frame be sent to a specified peer STA
519  * to which the STA is associated.
520  */
521 void
522 ieee80211_sa_query_request(struct ieee80211com *ic, struct ieee80211_node *ni)
523 {
524 	/* MLME-SAQuery.request */
525 
526 	if (!(ni->ni_flags & IEEE80211_NODE_SA_QUERY)) {
527 		ni->ni_flags |= IEEE80211_NODE_SA_QUERY;
528 		ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY_FAILED;
529 		ni->ni_sa_query_count = 0;
530 	}
531 	/* generate new Transaction Identifier */
532 	ni->ni_sa_query_trid++;
533 
534 	/* send SA Query Request */
535 	IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_SA_QUERY,
536 	    IEEE80211_ACTION_SA_QUERY_REQ, 0);
537 	timeout_add_msec(&ni->ni_sa_query_to, 10);
538 }
539 #endif	/* IEEE80211_STA_ONLY */
540 
541 void
542 ieee80211_ht_negotiate(struct ieee80211com *ic, struct ieee80211_node *ni)
543 {
544 	int i;
545 
546 	ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HT_SGI20 |
547 	    IEEE80211_NODE_HT_SGI40);
548 
549 	/* Check if we support HT. */
550 	if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) == 0)
551 		return;
552 
553 	/* Check if HT support has been explicitly disabled. */
554 	if ((ic->ic_flags & IEEE80211_F_HTON) == 0)
555 		return;
556 
557 	/*
558 	 * Check if the peer supports HT.
559 	 * Require at least one of the mandatory MCS.
560 	 * MCS 0-7 are mandatory but some APs have particular MCS disabled.
561 	 */
562 	if (!ieee80211_node_supports_ht(ni)) {
563 		ic->ic_stats.is_ht_nego_no_mandatory_mcs++;
564 		return;
565 	}
566 
567 	if (ic->ic_opmode == IEEE80211_M_STA) {
568 		/* We must support the AP's basic MCS set. */
569 		for (i = 0; i < IEEE80211_HT_NUM_MCS; i++) {
570 			if (isset(ni->ni_basic_mcs, i) &&
571 			    !isset(ic->ic_sup_mcs, i)) {
572 				ic->ic_stats.is_ht_nego_no_basic_mcs++;
573 				return;
574 			}
575 		}
576 	}
577 
578 	/*
579 	 * Don't allow group cipher (includes WEP) or TKIP
580 	 * for pairwise encryption (see 802.11-2012 11.1.6).
581 	 */
582 	if (ic->ic_flags & IEEE80211_F_WEPON) {
583 		ic->ic_stats.is_ht_nego_bad_crypto++;
584 		return;
585 	}
586 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
587 	    (ni->ni_rsnciphers & IEEE80211_CIPHER_USEGROUP ||
588 	    ni->ni_rsnciphers & IEEE80211_CIPHER_TKIP)) {
589 		ic->ic_stats.is_ht_nego_bad_crypto++;
590 		return;
591 	}
592 
593 	ni->ni_flags |= IEEE80211_NODE_HT;
594 
595 	/* Flags IEEE8021_NODE_HT_SGI20/40 are set by drivers if supported. */
596 }
597 
598 void
599 ieee80211_tx_ba_timeout(void *arg)
600 {
601 	struct ieee80211_tx_ba *ba = arg;
602 	struct ieee80211_node *ni = ba->ba_ni;
603 	struct ieee80211com *ic = ni->ni_ic;
604 	u_int8_t tid;
605 	int s;
606 
607 	s = splnet();
608 	tid = ((caddr_t)ba - (caddr_t)ni->ni_tx_ba) / sizeof(*ba);
609 	if (ba->ba_state == IEEE80211_BA_REQUESTED) {
610 		/* MLME-ADDBA.confirm(TIMEOUT) */
611 		ba->ba_state = IEEE80211_BA_INIT;
612 		if (ni->ni_addba_req_intval[tid] <
613 		    IEEE80211_ADDBA_REQ_INTVAL_MAX)
614 			ni->ni_addba_req_intval[tid]++;
615 		/*
616 		 * In case the peer believes there is an existing
617 		 * block ack agreement with us, try to delete it.
618 		 */
619 		IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
620 		    IEEE80211_ACTION_DELBA,
621 		    IEEE80211_REASON_SETUP_REQUIRED << 16 | 1 << 8 | tid);
622 	} else if (ba->ba_state == IEEE80211_BA_AGREED) {
623 		/* Block Ack inactivity timeout */
624 		ic->ic_stats.is_ht_tx_ba_timeout++;
625 		ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT,
626 		    1, tid);
627 	}
628 	splx(s);
629 }
630 
631 void
632 ieee80211_rx_ba_timeout(void *arg)
633 {
634 	struct ieee80211_rx_ba *ba = arg;
635 	struct ieee80211_node *ni = ba->ba_ni;
636 	struct ieee80211com *ic = ni->ni_ic;
637 	u_int8_t tid;
638 	int s;
639 
640 	ic->ic_stats.is_ht_rx_ba_timeout++;
641 
642 	s = splnet();
643 
644 	/* Block Ack inactivity timeout */
645 	tid = ((caddr_t)ba - (caddr_t)ni->ni_rx_ba) / sizeof(*ba);
646 	ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 0, tid);
647 
648 	splx(s);
649 }
650 
651 /*
652  * Request initiation of Block Ack with the specified peer.
653  */
654 int
655 ieee80211_addba_request(struct ieee80211com *ic, struct ieee80211_node *ni,
656     u_int16_t ssn, u_int8_t tid)
657 {
658 	struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
659 
660 	if (ba->ba_state != IEEE80211_BA_INIT)
661 		return EBUSY;
662 
663 	/* MLME-ADDBA.request */
664 
665 	/* setup Block Ack */
666 	ba->ba_ni = ni;
667 	ba->ba_state = IEEE80211_BA_REQUESTED;
668 	ba->ba_token = ic->ic_dialog_token++;
669 	ba->ba_timeout_val = 0;
670 	timeout_set(&ba->ba_to, ieee80211_tx_ba_timeout, ba);
671 	ba->ba_winsize = IEEE80211_BA_MAX_WINSZ;
672 	ba->ba_winstart = ssn;
673 	ba->ba_winend = (ba->ba_winstart + ba->ba_winsize - 1) & 0xfff;
674 	ba->ba_params =
675 	    (ba->ba_winsize << IEEE80211_ADDBA_BUFSZ_SHIFT) |
676 	    (tid << IEEE80211_ADDBA_TID_SHIFT);
677 #if 0
678 	/*
679 	 * XXX A-MSDUs inside A-MPDUs expose a problem with bad TCP connection
680 	 * sharing behaviour. One connection eats all available bandwidth
681 	 * while others stall. Leave this disabled for now to give packets
682 	 * from disparate connections better chances of interleaving.
683 	 */
684 	ba->ba_params |= IEEE80211_ADDBA_AMSDU;
685 #endif
686 	if ((ic->ic_htcaps & IEEE80211_HTCAP_DELAYEDBA) == 0)
687 		/* immediate BA */
688 		ba->ba_params |= IEEE80211_ADDBA_BA_POLICY;
689 
690 	timeout_add_sec(&ba->ba_to, 1);	/* dot11ADDBAResponseTimeout */
691 	IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
692 	    IEEE80211_ACTION_ADDBA_REQ, tid);
693 	return 0;
694 }
695 
696 /*
697  * Request the deletion of Block Ack with a peer and notify driver.
698  */
699 void
700 ieee80211_delba_request(struct ieee80211com *ic, struct ieee80211_node *ni,
701     u_int16_t reason, u_int8_t dir, u_int8_t tid)
702 {
703 	/* MLME-DELBA.request */
704 
705 	if (reason) {
706 		/* transmit a DELBA frame */
707 		IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
708 		    IEEE80211_ACTION_DELBA, reason << 16 | dir << 8 | tid);
709 	}
710 	if (dir) {
711 		/* MLME-DELBA.confirm(Originator) */
712 		struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
713 
714 		if (ic->ic_ampdu_tx_stop != NULL)
715 			ic->ic_ampdu_tx_stop(ic, ni, tid);
716 
717 		ba->ba_state = IEEE80211_BA_INIT;
718 		/* stop Block Ack inactivity timer */
719 		timeout_del(&ba->ba_to);
720 	} else {
721 		/* MLME-DELBA.confirm(Recipient) */
722 		struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid];
723 		int i;
724 
725 		if (ic->ic_ampdu_rx_stop != NULL)
726 			ic->ic_ampdu_rx_stop(ic, ni, tid);
727 
728 		ba->ba_state = IEEE80211_BA_INIT;
729 		/* stop Block Ack inactivity timer */
730 		timeout_del(&ba->ba_to);
731 		timeout_del(&ba->ba_gap_to);
732 
733 		if (ba->ba_buf != NULL) {
734 			/* free all MSDUs stored in reordering buffer */
735 			for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++)
736 				m_freem(ba->ba_buf[i].m);
737 			/* free reordering buffer */
738 			free(ba->ba_buf, M_DEVBUF,
739 			    IEEE80211_BA_MAX_WINSZ * sizeof(*ba->ba_buf));
740 			ba->ba_buf = NULL;
741 		}
742 	}
743 }
744 
745 #ifndef IEEE80211_STA_ONLY
746 void
747 ieee80211_auth_open_confirm(struct ieee80211com *ic,
748     struct ieee80211_node *ni, uint16_t seq)
749 {
750 	struct ifnet *ifp = &ic->ic_if;
751 
752 	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
753 	if (ifp->if_flags & IFF_DEBUG)
754 		printf("%s: station %s %s authenticated (open)\n",
755 		    ifp->if_xname,
756 		    ether_sprintf((u_int8_t *)ni->ni_macaddr),
757 		    ni->ni_state != IEEE80211_STA_CACHE ?
758 		    "newly" : "already");
759 	ieee80211_node_newstate(ni, IEEE80211_STA_AUTH);
760 }
761 #endif
762 
763 void
764 ieee80211_try_another_bss(struct ieee80211com *ic)
765 {
766 	struct ieee80211_node *curbs, *selbs;
767 	struct ifnet *ifp = &ic->ic_if;
768 
769 	/* Don't select our current AP again. */
770 	curbs = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
771 	if (curbs) {
772 		curbs->ni_fails++;
773 		ieee80211_node_newstate(curbs, IEEE80211_STA_CACHE);
774 	}
775 
776 	/* Try a different AP from the same ESS if available. */
777 	if (ic->ic_caps & IEEE80211_C_SCANALLBAND) {
778 		/*
779 		 * Make sure we will consider APs on all bands during
780 		 * access point selection in ieee80211_node_choose_bss().
781 		 * During multi-band scans, our previous AP may be trying
782 		 * to steer us onto another band by denying authentication.
783 		 */
784 		ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
785 	}
786 	selbs = ieee80211_node_choose_bss(ic, 0, NULL);
787 	if (selbs == NULL)
788 		return;
789 
790 	/* Should not happen but seriously, don't try the same AP again. */
791 	if (memcmp(selbs->ni_macaddr, ic->ic_bss->ni_macaddr,
792 	    IEEE80211_NWID_LEN) == 0)
793 		return;
794 
795 	if (ifp->if_flags & IFF_DEBUG)
796 		printf("%s: trying AP %s on channel %d instead\n",
797 		    ifp->if_xname, ether_sprintf(selbs->ni_macaddr),
798 		    ieee80211_chan2ieee(ic, selbs->ni_chan));
799 
800 	/* Triggers an AUTH->AUTH transition, avoiding another SCAN. */
801 	ieee80211_node_join_bss(ic, selbs);
802 }
803 
804 void
805 ieee80211_auth_open(struct ieee80211com *ic, const struct ieee80211_frame *wh,
806     struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, u_int16_t seq,
807     u_int16_t status)
808 {
809 	struct ifnet *ifp = &ic->ic_if;
810 	switch (ic->ic_opmode) {
811 #ifndef IEEE80211_STA_ONLY
812 	case IEEE80211_M_IBSS:
813 		if (ic->ic_state != IEEE80211_S_RUN ||
814 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
815 			DPRINTF(("discard auth from %s; state %u, seq %u\n",
816 			    ether_sprintf((u_int8_t *)wh->i_addr2),
817 			    ic->ic_state, seq));
818 			ic->ic_stats.is_rx_bad_auth++;
819 			return;
820 		}
821 		ieee80211_new_state(ic, IEEE80211_S_AUTH,
822 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
823 
824 		/* In IBSS mode no (re)association frames are sent. */
825 		if (ic->ic_flags & IEEE80211_F_RSNON)
826 			ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART;
827 		break;
828 
829 	case IEEE80211_M_AHDEMO:
830 		/* should not come here */
831 		break;
832 
833 	case IEEE80211_M_HOSTAP:
834 		if (ic->ic_state != IEEE80211_S_RUN ||
835 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
836 			DPRINTF(("discard auth from %s; state %u, seq %u\n",
837 			    ether_sprintf((u_int8_t *)wh->i_addr2),
838 			    ic->ic_state, seq));
839 			ic->ic_stats.is_rx_bad_auth++;
840 			return;
841 		}
842 		if (ni == ic->ic_bss) {
843 			ni = ieee80211_find_node(ic, wh->i_addr2);
844 			if (ni == NULL)
845 				ni = ieee80211_alloc_node(ic, wh->i_addr2);
846 			if (ni == NULL) {
847 				return;
848 			}
849 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
850 			ni->ni_rssi = rxi->rxi_rssi;
851 			ni->ni_rstamp = rxi->rxi_tstamp;
852 			ni->ni_chan = ic->ic_bss->ni_chan;
853 		}
854 
855 		/*
856 		 * Drivers may want to set up state before confirming.
857 		 * In which case this returns EBUSY and the driver will
858 		 * later call ieee80211_auth_open_confirm() by itself.
859 		 */
860 		if (ic->ic_newauth && ic->ic_newauth(ic, ni,
861 		    ni->ni_state != IEEE80211_STA_CACHE, seq) != 0)
862 			break;
863 		ieee80211_auth_open_confirm(ic, ni, seq);
864 		break;
865 #endif	/* IEEE80211_STA_ONLY */
866 
867 	case IEEE80211_M_STA:
868 		if (ic->ic_state != IEEE80211_S_AUTH ||
869 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
870 			ic->ic_stats.is_rx_bad_auth++;
871 			DPRINTF(("discard auth from %s; state %u, seq %u\n",
872 			    ether_sprintf((u_int8_t *)wh->i_addr2),
873 			    ic->ic_state, seq));
874 			return;
875 		}
876 		if (ic->ic_flags & IEEE80211_F_RSNON) {
877 			/* XXX not here! */
878 			ic->ic_bss->ni_flags &= ~IEEE80211_NODE_TXRXPROT;
879 			ic->ic_bss->ni_port_valid = 0;
880 			ic->ic_bss->ni_replaycnt_ok = 0;
881 			(*ic->ic_delete_key)(ic, ic->ic_bss,
882 			    &ic->ic_bss->ni_pairwise_key);
883 		}
884 		if (status != 0) {
885 			if (ifp->if_flags & IFF_DEBUG)
886 				printf("%s: open authentication failed "
887 				    "(status %d) for %s\n", ifp->if_xname,
888 				    status,
889 				    ether_sprintf((u_int8_t *)wh->i_addr3));
890 			if (ni != ic->ic_bss)
891 				ni->ni_fails++;
892 			else
893 				ieee80211_try_another_bss(ic);
894 			ic->ic_stats.is_rx_auth_fail++;
895 			return;
896 		}
897 		ieee80211_new_state(ic, IEEE80211_S_ASSOC,
898 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
899 		break;
900 	default:
901 		break;
902 	}
903 }
904 
905 void
906 ieee80211_set_beacon_miss_threshold(struct ieee80211com *ic)
907 {
908 	struct ifnet *ifp = &ic->ic_if;
909 
910 	/*
911 	 * Scale the missed beacon counter threshold to the AP's actual
912 	 * beacon interval.
913 	 */
914 	int btimeout = MIN(IEEE80211_BEACON_MISS_THRES * ic->ic_bss->ni_intval,
915 	    IEEE80211_BEACON_MISS_THRES * (IEEE80211_DUR_TU / 10));
916 	/* Ensure that at least one beacon may be missed. */
917 	btimeout = MAX(btimeout, 2 * ic->ic_bss->ni_intval);
918 	if (ic->ic_bss->ni_intval > 0) /* don't crash if interval is bogus */
919 		ic->ic_bmissthres = btimeout / ic->ic_bss->ni_intval;
920 
921 	if (ifp->if_flags & IFF_DEBUG)
922 		printf("%s: missed beacon threshold set to %d beacons, "
923 		    "beacon interval is %u TU\n", ifp->if_xname,
924 		    ic->ic_bmissthres, ic->ic_bss->ni_intval);
925 }
926 
927 /* Tell our peer, and the driver, to stop A-MPDU Tx for all TIDs. */
928 void
929 ieee80211_stop_ampdu_tx(struct ieee80211com *ic, struct ieee80211_node *ni,
930     int mgt)
931 {
932 	int tid;
933 
934 	for (tid = 0; tid < nitems(ni->ni_tx_ba); tid++) {
935 		struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
936 		if (ba->ba_state != IEEE80211_BA_AGREED)
937 			continue;
938 		ieee80211_delba_request(ic, ni,
939 		    mgt == -1 ? 0 : IEEE80211_REASON_AUTH_LEAVE, 1, tid);
940 	}
941 }
942 
943 void
944 ieee80211_check_wpa_supplicant_failure(struct ieee80211com *ic,
945     struct ieee80211_node *ni)
946 {
947 	struct ieee80211_node *ni2;
948 
949 	if (ic->ic_opmode != IEEE80211_M_STA
950 #ifndef IEEE80211_STA_ONLY
951 	    && ic->ic_opmode != IEEE80211_M_IBSS
952 #endif
953 	    )
954 		return;
955 
956 	if (ni->ni_rsn_supp_state != RSNA_SUPP_PTKNEGOTIATING)
957 		return;
958 
959 	ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_WPA_KEY;
960 
961 	if (ni != ic->ic_bss)
962 		return;
963 
964 	/* Also update the copy of our AP's node in the node cache. */
965 	ni2 = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
966 	if (ni2)
967 		ni2->ni_assoc_fail |= ic->ic_bss->ni_assoc_fail;
968 }
969 
970 int
971 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate,
972     int mgt)
973 {
974 	struct ifnet *ifp = &ic->ic_if;
975 	struct ieee80211_node *ni;
976 	enum ieee80211_state ostate;
977 	u_int rate;
978 #ifndef IEEE80211_STA_ONLY
979 	int s;
980 #endif
981 
982 	ostate = ic->ic_state;
983 	if (ifp->if_flags & IFF_DEBUG)
984 		printf("%s: %s -> %s\n", ifp->if_xname,
985 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
986 	ic->ic_state = nstate;			/* state transition */
987 	ni = ic->ic_bss;			/* NB: no reference held */
988 	ieee80211_set_link_state(ic, LINK_STATE_DOWN);
989 	ic->ic_xflags &= ~IEEE80211_F_TX_MGMT_ONLY;
990 	switch (nstate) {
991 	case IEEE80211_S_INIT:
992 		/*
993 		 * If mgt = -1, driver is already partway down, so do
994 		 * not send management frames.
995 		 */
996 		switch (ostate) {
997 		case IEEE80211_S_INIT:
998 			break;
999 		case IEEE80211_S_RUN:
1000 			if (mgt == -1)
1001 				goto justcleanup;
1002 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1003 			ieee80211_ba_del(ni);
1004 			switch (ic->ic_opmode) {
1005 			case IEEE80211_M_STA:
1006 				IEEE80211_SEND_MGMT(ic, ni,
1007 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
1008 				    IEEE80211_REASON_ASSOC_LEAVE);
1009 				break;
1010 #ifndef IEEE80211_STA_ONLY
1011 			case IEEE80211_M_HOSTAP:
1012 				s = splnet();
1013 				RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) {
1014 					if (ni->ni_state != IEEE80211_STA_ASSOC)
1015 						continue;
1016 					IEEE80211_SEND_MGMT(ic, ni,
1017 					    IEEE80211_FC0_SUBTYPE_DISASSOC,
1018 					    IEEE80211_REASON_ASSOC_LEAVE);
1019 				}
1020 				splx(s);
1021 				break;
1022 #endif
1023 			default:
1024 				break;
1025 			}
1026 			/* FALLTHROUGH */
1027 		case IEEE80211_S_ASSOC:
1028 			if (mgt == -1)
1029 				goto justcleanup;
1030 			switch (ic->ic_opmode) {
1031 			case IEEE80211_M_STA:
1032 				IEEE80211_SEND_MGMT(ic, ni,
1033 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1034 				    IEEE80211_REASON_AUTH_LEAVE);
1035 				break;
1036 #ifndef IEEE80211_STA_ONLY
1037 			case IEEE80211_M_HOSTAP:
1038 				s = splnet();
1039 				RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) {
1040 					IEEE80211_SEND_MGMT(ic, ni,
1041 					    IEEE80211_FC0_SUBTYPE_DEAUTH,
1042 					    IEEE80211_REASON_AUTH_LEAVE);
1043 				}
1044 				splx(s);
1045 				break;
1046 #endif
1047 			default:
1048 				break;
1049 			}
1050 			/* FALLTHROUGH */
1051 		case IEEE80211_S_AUTH:
1052 		case IEEE80211_S_SCAN:
1053 justcleanup:
1054 #ifndef IEEE80211_STA_ONLY
1055 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1056 				timeout_del(&ic->ic_rsn_timeout);
1057 #endif
1058 			ieee80211_ba_del(ni);
1059 			timeout_del(&ic->ic_bgscan_timeout);
1060 			ic->ic_bgscan_fail = 0;
1061 			ic->ic_mgt_timer = 0;
1062 			mq_purge(&ic->ic_mgtq);
1063 			mq_purge(&ic->ic_pwrsaveq);
1064 			ieee80211_free_allnodes(ic, 1);
1065 			break;
1066 		}
1067 		ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE;
1068 		ni->ni_assoc_fail = 0;
1069 		if (ic->ic_flags & IEEE80211_F_RSNON)
1070 			ieee80211_crypto_clear_groupkeys(ic);
1071 		break;
1072 	case IEEE80211_S_SCAN:
1073 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
1074 		/* initialize bss for probe request */
1075 		IEEE80211_ADDR_COPY(ni->ni_macaddr, etherbroadcastaddr);
1076 		IEEE80211_ADDR_COPY(ni->ni_bssid, etherbroadcastaddr);
1077 		ni->ni_rates = ic->ic_sup_rates[
1078 			ieee80211_chan2mode(ic, ni->ni_chan)];
1079 		ni->ni_associd = 0;
1080 		ni->ni_rstamp = 0;
1081 		ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE;
1082 		if (ic->ic_flags & IEEE80211_F_RSNON)
1083 			ieee80211_crypto_clear_groupkeys(ic);
1084 		switch (ostate) {
1085 		case IEEE80211_S_INIT:
1086 #ifndef IEEE80211_STA_ONLY
1087 			if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1088 			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
1089 				/*
1090 				 * AP operation and we already have a channel;
1091 				 * bypass the scan and startup immediately.
1092 				 */
1093 				ieee80211_create_ibss(ic, ic->ic_des_chan);
1094 			} else
1095 #endif
1096 				ieee80211_begin_scan(ifp);
1097 			break;
1098 		case IEEE80211_S_SCAN:
1099 			/* scan next */
1100 			if (ic->ic_flags & IEEE80211_F_ASCAN) {
1101 				IEEE80211_SEND_MGMT(ic, ni,
1102 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
1103 			}
1104 			break;
1105 		case IEEE80211_S_RUN:
1106 			/* beacon miss */
1107 			if (ifp->if_flags & IFF_DEBUG) {
1108 				/* XXX bssid clobbered above */
1109 				printf("%s: no recent beacons from %s;"
1110 				    " rescanning\n", ifp->if_xname,
1111 				    ether_sprintf(ic->ic_bss->ni_bssid));
1112 			}
1113 			timeout_del(&ic->ic_bgscan_timeout);
1114 			ic->ic_bgscan_fail = 0;
1115 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1116 			ieee80211_free_allnodes(ic, 1);
1117 			/* FALLTHROUGH */
1118 		case IEEE80211_S_AUTH:
1119 		case IEEE80211_S_ASSOC:
1120 			/* timeout restart scan */
1121 			ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
1122 			if (ni != NULL)
1123 				ni->ni_fails++;
1124 			ieee80211_begin_scan(ifp);
1125 			break;
1126 		}
1127 		break;
1128 	case IEEE80211_S_AUTH:
1129 		if (ostate == IEEE80211_S_RUN)
1130 			ieee80211_check_wpa_supplicant_failure(ic, ni);
1131 		ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE;
1132 		if (ic->ic_flags & IEEE80211_F_RSNON)
1133 			ieee80211_crypto_clear_groupkeys(ic);
1134 		switch (ostate) {
1135 		case IEEE80211_S_INIT:
1136 			if (ifp->if_flags & IFF_DEBUG)
1137 				printf("%s: invalid transition %s -> %s\n",
1138 				    ifp->if_xname, ieee80211_state_name[ostate],
1139 				    ieee80211_state_name[nstate]);
1140 			break;
1141 		case IEEE80211_S_SCAN:
1142 			IEEE80211_SEND_MGMT(ic, ni,
1143 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1144 			break;
1145 		case IEEE80211_S_AUTH:
1146 		case IEEE80211_S_ASSOC:
1147 			switch (mgt) {
1148 			case IEEE80211_FC0_SUBTYPE_AUTH:
1149 				if (ic->ic_opmode == IEEE80211_M_STA) {
1150 					IEEE80211_SEND_MGMT(ic, ni,
1151 					    IEEE80211_FC0_SUBTYPE_AUTH,
1152 					    IEEE80211_AUTH_OPEN_REQUEST);
1153 				}
1154 				break;
1155 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1156 				/* ignore and retry scan on timeout */
1157 				break;
1158 			}
1159 			break;
1160 		case IEEE80211_S_RUN:
1161 			timeout_del(&ic->ic_bgscan_timeout);
1162 			ic->ic_bgscan_fail = 0;
1163 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1164 			ieee80211_ba_del(ni);
1165 			switch (mgt) {
1166 			case IEEE80211_FC0_SUBTYPE_AUTH:
1167 				IEEE80211_SEND_MGMT(ic, ni,
1168 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1169 				ic->ic_state = ostate;	/* stay RUN */
1170 				break;
1171 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1172 				/* try to reauth */
1173 				IEEE80211_SEND_MGMT(ic, ni,
1174 				    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1175 				break;
1176 			}
1177 			break;
1178 		}
1179 		break;
1180 	case IEEE80211_S_ASSOC:
1181 		switch (ostate) {
1182 		case IEEE80211_S_INIT:
1183 		case IEEE80211_S_SCAN:
1184 		case IEEE80211_S_ASSOC:
1185 			if (ifp->if_flags & IFF_DEBUG)
1186 				printf("%s: invalid transition %s -> %s\n",
1187 				    ifp->if_xname, ieee80211_state_name[ostate],
1188 				    ieee80211_state_name[nstate]);
1189 			break;
1190 		case IEEE80211_S_AUTH:
1191 			IEEE80211_SEND_MGMT(ic, ni,
1192 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1193 			break;
1194 		case IEEE80211_S_RUN:
1195 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1196 			ieee80211_ba_del(ni);
1197 			IEEE80211_SEND_MGMT(ic, ni,
1198 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
1199 			break;
1200 		}
1201 		break;
1202 	case IEEE80211_S_RUN:
1203 		switch (ostate) {
1204 		case IEEE80211_S_INIT:
1205 			if (ic->ic_opmode == IEEE80211_M_MONITOR)
1206 				break;
1207 		case IEEE80211_S_AUTH:
1208 		case IEEE80211_S_RUN:
1209 			if (ifp->if_flags & IFF_DEBUG)
1210 				printf("%s: invalid transition %s -> %s\n",
1211 				    ifp->if_xname, ieee80211_state_name[ostate],
1212 				    ieee80211_state_name[nstate]);
1213 			break;
1214 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
1215 		case IEEE80211_S_ASSOC:		/* infra mode */
1216 			if (ni->ni_txrate >= ni->ni_rates.rs_nrates)
1217 				panic("%s: bogus xmit rate %u setup",
1218 				    __func__, ni->ni_txrate);
1219 			if (ifp->if_flags & IFF_DEBUG) {
1220 				printf("%s: %s with %s ssid ",
1221 				    ifp->if_xname,
1222 				    ic->ic_opmode == IEEE80211_M_STA ?
1223 				    "associated" : "synchronized",
1224 				    ether_sprintf(ni->ni_bssid));
1225 				ieee80211_print_essid(ic->ic_bss->ni_essid,
1226 				    ni->ni_esslen);
1227 				rate = ni->ni_rates.rs_rates[ni->ni_txrate] &
1228 				    IEEE80211_RATE_VAL;
1229 				printf(" channel %d",
1230 				    ieee80211_chan2ieee(ic, ni->ni_chan));
1231 				if (ni->ni_flags & IEEE80211_NODE_HT)
1232 					printf(" start MCS %u", ni->ni_txmcs);
1233 				else
1234 					printf(" start %u%sMb",
1235 					    rate / 2, (rate & 1) ? ".5" : "");
1236 				printf(" %s preamble %s slot time%s%s\n",
1237 				    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ?
1238 					"short" : "long",
1239 				    (ic->ic_flags & IEEE80211_F_SHSLOT) ?
1240 					"short" : "long",
1241 				    (ic->ic_flags & IEEE80211_F_USEPROT) ?
1242 					" protection enabled" : "",
1243 				    (ni->ni_flags & IEEE80211_NODE_HT) ?
1244 					" HT enabled" : "");
1245 			}
1246 			if (!(ic->ic_flags & IEEE80211_F_RSNON)) {
1247 				/*
1248 				 * NB: When RSN is enabled, we defer setting
1249 				 * the link up until the port is valid.
1250 				 */
1251 				ieee80211_set_link_state(ic, LINK_STATE_UP);
1252 				ni->ni_assoc_fail = 0;
1253 			}
1254 			ic->ic_mgt_timer = 0;
1255 			ieee80211_set_beacon_miss_threshold(ic);
1256 			if_start(ifp);
1257 			break;
1258 		}
1259 		break;
1260 	}
1261 	return 0;
1262 }
1263 
1264 void
1265 ieee80211_set_link_state(struct ieee80211com *ic, int nstate)
1266 {
1267 	struct ifnet *ifp = &ic->ic_if;
1268 
1269 	switch (ic->ic_opmode) {
1270 #ifndef IEEE80211_STA_ONLY
1271 	case IEEE80211_M_IBSS:
1272 	case IEEE80211_M_HOSTAP:
1273 		nstate = LINK_STATE_UNKNOWN;
1274 		break;
1275 #endif
1276 	case IEEE80211_M_MONITOR:
1277 		nstate = LINK_STATE_DOWN;
1278 		break;
1279 	default:
1280 		break;
1281 	}
1282 	if (nstate != ifp->if_link_state) {
1283 		ifp->if_link_state = nstate;
1284 		if (LINK_STATE_IS_UP(nstate)) {
1285 			struct if_ieee80211_data ifie;
1286 			memset(&ifie, 0, sizeof(ifie));
1287 			ifie.ifie_nwid_len = ic->ic_bss->ni_esslen;
1288 			memcpy(ifie.ifie_nwid, ic->ic_bss->ni_essid,
1289 			    sizeof(ifie.ifie_nwid));
1290 			memcpy(ifie.ifie_addr, ic->ic_bss->ni_bssid,
1291 			    sizeof(ifie.ifie_addr));
1292 			ifie.ifie_channel = ieee80211_chan2ieee(ic,
1293 			    ic->ic_bss->ni_chan);
1294 			ifie.ifie_flags = ic->ic_flags;
1295 			ifie.ifie_xflags = ic->ic_xflags;
1296 			rtm_80211info(&ic->ic_if, &ifie);
1297 		}
1298 		if_link_state_change(ifp);
1299 	}
1300 }
1301