xref: /openbsd/sys/net80211/ieee80211_proto.c (revision 09467b48)
1 /*	$OpenBSD: ieee80211_proto.c,v 1.98 2020/05/29 07:37:51 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 	int rekeysta = 0;
437 
438 	/* Swap(GM, GN) */
439 	kid = (ic->ic_def_txkey == 1) ? 2 : 1;
440 	k = &ic->ic_nw_keys[kid];
441 	memset(k, 0, sizeof(*k));
442 	k->k_id = kid;
443 	k->k_cipher = ic->ic_bss->ni_rsngroupcipher;
444 	k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX;
445 	k->k_len = ieee80211_cipher_keylen(k->k_cipher);
446 	arc4random_buf(k->k_key, k->k_len);
447 
448 	if (ic->ic_caps & IEEE80211_C_MFP) {
449 		/* Swap(GM_igtk, GN_igtk) */
450 		kid = (ic->ic_igtk_kid == 4) ? 5 : 4;
451 		k = &ic->ic_nw_keys[kid];
452 		memset(k, 0, sizeof(*k));
453 		k->k_id = kid;
454 		k->k_cipher = ic->ic_bss->ni_rsngroupmgmtcipher;
455 		k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX;
456 		k->k_len = 16;
457 		arc4random_buf(k->k_key, k->k_len);
458 	}
459 
460 	ieee80211_iterate_nodes(ic, ieee80211_node_gtk_rekey, ic);
461 	ieee80211_iterate_nodes(ic, ieee80211_count_rekeysta, &rekeysta);
462 	if (rekeysta == 0)
463 		ieee80211_setkeysdone(ic);
464 }
465 
466 /*
467  * The group key handshake has been completed with all associated stations.
468  */
469 void
470 ieee80211_setkeysdone(struct ieee80211com *ic)
471 {
472 	u_int8_t kid;
473 
474 	/*
475 	 * Discard frames buffered for power-saving which were encrypted with
476 	 * the old group key. Clients are no longer able to decrypt them.
477 	 */
478 	mq_purge(&ic->ic_bss->ni_savedq);
479 
480 	/* install GTK */
481 	kid = (ic->ic_def_txkey == 1) ? 2 : 1;
482 	if ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid]) == 0)
483 		ic->ic_def_txkey = kid;
484 
485 	if (ic->ic_caps & IEEE80211_C_MFP) {
486 		/* install IGTK */
487 		kid = (ic->ic_igtk_kid == 4) ? 5 : 4;
488 		if ((*ic->ic_set_key)(ic, ic->ic_bss,
489 		    &ic->ic_nw_keys[kid]) == 0)
490 			ic->ic_igtk_kid = kid;
491 	}
492 }
493 
494 /*
495  * Group key lifetime has expired, update it.
496  */
497 void
498 ieee80211_gtk_rekey_timeout(void *arg)
499 {
500 	struct ieee80211com *ic = arg;
501 	int s;
502 
503 	s = splnet();
504 	ieee80211_setkeys(ic);
505 	splx(s);
506 
507 	/* re-schedule a GTK rekeying after 3600s */
508 	timeout_add_sec(&ic->ic_rsn_timeout, 3600);
509 }
510 
511 void
512 ieee80211_sa_query_timeout(void *arg)
513 {
514 	struct ieee80211_node *ni = arg;
515 	struct ieee80211com *ic = ni->ni_ic;
516 	int s;
517 
518 	s = splnet();
519 	if (++ni->ni_sa_query_count >= 3) {
520 		ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY;
521 		ni->ni_flags |= IEEE80211_NODE_SA_QUERY_FAILED;
522 	} else	/* retry SA Query Request */
523 		ieee80211_sa_query_request(ic, ni);
524 	splx(s);
525 }
526 
527 /*
528  * Request that a SA Query Request frame be sent to a specified peer STA
529  * to which the STA is associated.
530  */
531 void
532 ieee80211_sa_query_request(struct ieee80211com *ic, struct ieee80211_node *ni)
533 {
534 	/* MLME-SAQuery.request */
535 
536 	if (!(ni->ni_flags & IEEE80211_NODE_SA_QUERY)) {
537 		ni->ni_flags |= IEEE80211_NODE_SA_QUERY;
538 		ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY_FAILED;
539 		ni->ni_sa_query_count = 0;
540 	}
541 	/* generate new Transaction Identifier */
542 	ni->ni_sa_query_trid++;
543 
544 	/* send SA Query Request */
545 	IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_SA_QUERY,
546 	    IEEE80211_ACTION_SA_QUERY_REQ, 0);
547 	timeout_add_msec(&ni->ni_sa_query_to, 10);
548 }
549 #endif	/* IEEE80211_STA_ONLY */
550 
551 void
552 ieee80211_ht_negotiate(struct ieee80211com *ic, struct ieee80211_node *ni)
553 {
554 	int i;
555 
556 	ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HT_SGI20 |
557 	    IEEE80211_NODE_HT_SGI40);
558 
559 	/* Check if we support HT. */
560 	if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) == 0)
561 		return;
562 
563 	/* Check if HT support has been explicitly disabled. */
564 	if ((ic->ic_flags & IEEE80211_F_HTON) == 0)
565 		return;
566 
567 	/*
568 	 * Check if the peer supports HT.
569 	 * Require at least one of the mandatory MCS.
570 	 * MCS 0-7 are mandatory but some APs have particular MCS disabled.
571 	 */
572 	if (!ieee80211_node_supports_ht(ni)) {
573 		ic->ic_stats.is_ht_nego_no_mandatory_mcs++;
574 		return;
575 	}
576 
577 	if (ic->ic_opmode == IEEE80211_M_STA) {
578 		/* We must support the AP's basic MCS set. */
579 		for (i = 0; i < IEEE80211_HT_NUM_MCS; i++) {
580 			if (isset(ni->ni_basic_mcs, i) &&
581 			    !isset(ic->ic_sup_mcs, i)) {
582 				ic->ic_stats.is_ht_nego_no_basic_mcs++;
583 				return;
584 			}
585 		}
586 	}
587 
588 	/*
589 	 * Don't allow group cipher (includes WEP) or TKIP
590 	 * for pairwise encryption (see 802.11-2012 11.1.6).
591 	 */
592 	if (ic->ic_flags & IEEE80211_F_WEPON) {
593 		ic->ic_stats.is_ht_nego_bad_crypto++;
594 		return;
595 	}
596 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
597 	    (ni->ni_rsnciphers & IEEE80211_CIPHER_USEGROUP ||
598 	    ni->ni_rsnciphers & IEEE80211_CIPHER_TKIP)) {
599 		ic->ic_stats.is_ht_nego_bad_crypto++;
600 		return;
601 	}
602 
603 	ni->ni_flags |= IEEE80211_NODE_HT;
604 
605 	/* Flags IEEE8021_NODE_HT_SGI20/40 are set by drivers if supported. */
606 }
607 
608 void
609 ieee80211_tx_ba_timeout(void *arg)
610 {
611 	struct ieee80211_tx_ba *ba = arg;
612 	struct ieee80211_node *ni = ba->ba_ni;
613 	struct ieee80211com *ic = ni->ni_ic;
614 	u_int8_t tid;
615 	int s;
616 
617 	s = splnet();
618 	tid = ((caddr_t)ba - (caddr_t)ni->ni_tx_ba) / sizeof(*ba);
619 	if (ba->ba_state == IEEE80211_BA_REQUESTED) {
620 		/* MLME-ADDBA.confirm(TIMEOUT) */
621 		ba->ba_state = IEEE80211_BA_INIT;
622 		if (ni->ni_addba_req_intval[tid] <
623 		    IEEE80211_ADDBA_REQ_INTVAL_MAX)
624 			ni->ni_addba_req_intval[tid]++;
625 		/*
626 		 * In case the peer believes there is an existing
627 		 * block ack agreement with us, try to delete it.
628 		 */
629 		IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
630 		    IEEE80211_ACTION_DELBA,
631 		    IEEE80211_REASON_SETUP_REQUIRED << 16 | 1 << 8 | tid);
632 	} else if (ba->ba_state == IEEE80211_BA_AGREED) {
633 		/* Block Ack inactivity timeout */
634 		ic->ic_stats.is_ht_tx_ba_timeout++;
635 		ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT,
636 		    1, tid);
637 	}
638 	splx(s);
639 }
640 
641 void
642 ieee80211_rx_ba_timeout(void *arg)
643 {
644 	struct ieee80211_rx_ba *ba = arg;
645 	struct ieee80211_node *ni = ba->ba_ni;
646 	struct ieee80211com *ic = ni->ni_ic;
647 	u_int8_t tid;
648 	int s;
649 
650 	ic->ic_stats.is_ht_rx_ba_timeout++;
651 
652 	s = splnet();
653 
654 	/* Block Ack inactivity timeout */
655 	tid = ((caddr_t)ba - (caddr_t)ni->ni_rx_ba) / sizeof(*ba);
656 	ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 0, tid);
657 
658 	splx(s);
659 }
660 
661 /*
662  * Request initiation of Block Ack with the specified peer.
663  */
664 int
665 ieee80211_addba_request(struct ieee80211com *ic, struct ieee80211_node *ni,
666     u_int16_t ssn, u_int8_t tid)
667 {
668 	struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
669 
670 	if (ba->ba_state != IEEE80211_BA_INIT)
671 		return EBUSY;
672 
673 	/* MLME-ADDBA.request */
674 
675 	/* setup Block Ack */
676 	ba->ba_ni = ni;
677 	ba->ba_state = IEEE80211_BA_REQUESTED;
678 	ba->ba_token = ic->ic_dialog_token++;
679 	ba->ba_timeout_val = 0;
680 	timeout_set(&ba->ba_to, ieee80211_tx_ba_timeout, ba);
681 	ba->ba_winsize = IEEE80211_BA_MAX_WINSZ;
682 	ba->ba_winstart = ssn;
683 	ba->ba_winend = (ba->ba_winstart + ba->ba_winsize - 1) & 0xfff;
684 	ba->ba_params =
685 	    (ba->ba_winsize << IEEE80211_ADDBA_BUFSZ_SHIFT) |
686 	    (tid << IEEE80211_ADDBA_TID_SHIFT);
687 #if 0
688 	/*
689 	 * XXX A-MSDUs inside A-MPDUs expose a problem with bad TCP connection
690 	 * sharing behaviour. One connection eats all available bandwidth
691 	 * while others stall. Leave this disabled for now to give packets
692 	 * from disparate connections better chances of interleaving.
693 	 */
694 	ba->ba_params |= IEEE80211_ADDBA_AMSDU;
695 #endif
696 	if ((ic->ic_htcaps & IEEE80211_HTCAP_DELAYEDBA) == 0)
697 		/* immediate BA */
698 		ba->ba_params |= IEEE80211_ADDBA_BA_POLICY;
699 
700 	timeout_add_sec(&ba->ba_to, 1);	/* dot11ADDBAResponseTimeout */
701 	IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
702 	    IEEE80211_ACTION_ADDBA_REQ, tid);
703 	return 0;
704 }
705 
706 /*
707  * Request the deletion of Block Ack with a peer and notify driver.
708  */
709 void
710 ieee80211_delba_request(struct ieee80211com *ic, struct ieee80211_node *ni,
711     u_int16_t reason, u_int8_t dir, u_int8_t tid)
712 {
713 	/* MLME-DELBA.request */
714 
715 	if (reason) {
716 		/* transmit a DELBA frame */
717 		IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
718 		    IEEE80211_ACTION_DELBA, reason << 16 | dir << 8 | tid);
719 	}
720 	if (dir) {
721 		/* MLME-DELBA.confirm(Originator) */
722 		struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
723 
724 		if (ic->ic_ampdu_tx_stop != NULL)
725 			ic->ic_ampdu_tx_stop(ic, ni, tid);
726 
727 		ba->ba_state = IEEE80211_BA_INIT;
728 		/* stop Block Ack inactivity timer */
729 		timeout_del(&ba->ba_to);
730 	} else {
731 		/* MLME-DELBA.confirm(Recipient) */
732 		struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid];
733 		int i;
734 
735 		if (ic->ic_ampdu_rx_stop != NULL)
736 			ic->ic_ampdu_rx_stop(ic, ni, tid);
737 
738 		ba->ba_state = IEEE80211_BA_INIT;
739 		/* stop Block Ack inactivity timer */
740 		timeout_del(&ba->ba_to);
741 		timeout_del(&ba->ba_gap_to);
742 
743 		if (ba->ba_buf != NULL) {
744 			/* free all MSDUs stored in reordering buffer */
745 			for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++)
746 				m_freem(ba->ba_buf[i].m);
747 			/* free reordering buffer */
748 			free(ba->ba_buf, M_DEVBUF,
749 			    IEEE80211_BA_MAX_WINSZ * sizeof(*ba->ba_buf));
750 			ba->ba_buf = NULL;
751 		}
752 	}
753 }
754 
755 #ifndef IEEE80211_STA_ONLY
756 void
757 ieee80211_auth_open_confirm(struct ieee80211com *ic,
758     struct ieee80211_node *ni, uint16_t seq)
759 {
760 	struct ifnet *ifp = &ic->ic_if;
761 
762 	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
763 	if (ifp->if_flags & IFF_DEBUG)
764 		printf("%s: station %s %s authenticated (open)\n",
765 		    ifp->if_xname,
766 		    ether_sprintf((u_int8_t *)ni->ni_macaddr),
767 		    ni->ni_state != IEEE80211_STA_CACHE ?
768 		    "newly" : "already");
769 	ieee80211_node_newstate(ni, IEEE80211_STA_AUTH);
770 }
771 #endif
772 
773 void
774 ieee80211_try_another_bss(struct ieee80211com *ic)
775 {
776 	struct ieee80211_node *curbs, *selbs;
777 	struct ifnet *ifp = &ic->ic_if;
778 
779 	/* Don't select our current AP again. */
780 	curbs = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
781 	if (curbs) {
782 		curbs->ni_fails++;
783 		ieee80211_node_newstate(curbs, IEEE80211_STA_CACHE);
784 	}
785 
786 	/* Try a different AP from the same ESS if available. */
787 	if (ic->ic_caps & IEEE80211_C_SCANALLBAND) {
788 		/*
789 		 * Make sure we will consider APs on all bands during
790 		 * access point selection in ieee80211_node_choose_bss().
791 		 * During multi-band scans, our previous AP may be trying
792 		 * to steer us onto another band by denying authentication.
793 		 */
794 		ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
795 	}
796 	selbs = ieee80211_node_choose_bss(ic, 0, NULL);
797 	if (selbs == NULL)
798 		return;
799 
800 	/* Should not happen but seriously, don't try the same AP again. */
801 	if (memcmp(selbs->ni_macaddr, ic->ic_bss->ni_macaddr,
802 	    IEEE80211_NWID_LEN) == 0)
803 		return;
804 
805 	if (ifp->if_flags & IFF_DEBUG)
806 		printf("%s: trying AP %s on channel %d instead\n",
807 		    ifp->if_xname, ether_sprintf(selbs->ni_macaddr),
808 		    ieee80211_chan2ieee(ic, selbs->ni_chan));
809 
810 	/* Triggers an AUTH->AUTH transition, avoiding another SCAN. */
811 	ieee80211_node_join_bss(ic, selbs);
812 }
813 
814 void
815 ieee80211_auth_open(struct ieee80211com *ic, const struct ieee80211_frame *wh,
816     struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, u_int16_t seq,
817     u_int16_t status)
818 {
819 	struct ifnet *ifp = &ic->ic_if;
820 	switch (ic->ic_opmode) {
821 #ifndef IEEE80211_STA_ONLY
822 	case IEEE80211_M_IBSS:
823 		if (ic->ic_state != IEEE80211_S_RUN ||
824 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
825 			DPRINTF(("discard auth from %s; state %u, seq %u\n",
826 			    ether_sprintf((u_int8_t *)wh->i_addr2),
827 			    ic->ic_state, seq));
828 			ic->ic_stats.is_rx_bad_auth++;
829 			return;
830 		}
831 		ieee80211_new_state(ic, IEEE80211_S_AUTH,
832 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
833 
834 		/* In IBSS mode no (re)association frames are sent. */
835 		if (ic->ic_flags & IEEE80211_F_RSNON)
836 			ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART;
837 		break;
838 
839 	case IEEE80211_M_AHDEMO:
840 		/* should not come here */
841 		break;
842 
843 	case IEEE80211_M_HOSTAP:
844 		if (ic->ic_state != IEEE80211_S_RUN ||
845 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
846 			DPRINTF(("discard auth from %s; state %u, seq %u\n",
847 			    ether_sprintf((u_int8_t *)wh->i_addr2),
848 			    ic->ic_state, seq));
849 			ic->ic_stats.is_rx_bad_auth++;
850 			return;
851 		}
852 		if (ni == ic->ic_bss) {
853 			ni = ieee80211_find_node(ic, wh->i_addr2);
854 			if (ni == NULL)
855 				ni = ieee80211_alloc_node(ic, wh->i_addr2);
856 			if (ni == NULL) {
857 				return;
858 			}
859 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
860 			ni->ni_rssi = rxi->rxi_rssi;
861 			ni->ni_rstamp = rxi->rxi_tstamp;
862 			ni->ni_chan = ic->ic_bss->ni_chan;
863 		}
864 
865 		/*
866 		 * Drivers may want to set up state before confirming.
867 		 * In which case this returns EBUSY and the driver will
868 		 * later call ieee80211_auth_open_confirm() by itself.
869 		 */
870 		if (ic->ic_newauth && ic->ic_newauth(ic, ni,
871 		    ni->ni_state != IEEE80211_STA_CACHE, seq) != 0)
872 			break;
873 		ieee80211_auth_open_confirm(ic, ni, seq);
874 		break;
875 #endif	/* IEEE80211_STA_ONLY */
876 
877 	case IEEE80211_M_STA:
878 		if (ic->ic_state != IEEE80211_S_AUTH ||
879 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
880 			ic->ic_stats.is_rx_bad_auth++;
881 			DPRINTF(("discard auth from %s; state %u, seq %u\n",
882 			    ether_sprintf((u_int8_t *)wh->i_addr2),
883 			    ic->ic_state, seq));
884 			return;
885 		}
886 		if (ic->ic_flags & IEEE80211_F_RSNON) {
887 			/* XXX not here! */
888 			ic->ic_bss->ni_flags &= ~IEEE80211_NODE_TXRXPROT;
889 			ic->ic_bss->ni_port_valid = 0;
890 			ic->ic_bss->ni_replaycnt_ok = 0;
891 			(*ic->ic_delete_key)(ic, ic->ic_bss,
892 			    &ic->ic_bss->ni_pairwise_key);
893 		}
894 		if (status != 0) {
895 			if (ifp->if_flags & IFF_DEBUG)
896 				printf("%s: open authentication failed "
897 				    "(status %d) for %s\n", ifp->if_xname,
898 				    status,
899 				    ether_sprintf((u_int8_t *)wh->i_addr3));
900 			if (ni != ic->ic_bss)
901 				ni->ni_fails++;
902 			else
903 				ieee80211_try_another_bss(ic);
904 			ic->ic_stats.is_rx_auth_fail++;
905 			return;
906 		}
907 		ieee80211_new_state(ic, IEEE80211_S_ASSOC,
908 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
909 		break;
910 	default:
911 		break;
912 	}
913 }
914 
915 void
916 ieee80211_set_beacon_miss_threshold(struct ieee80211com *ic)
917 {
918 	struct ifnet *ifp = &ic->ic_if;
919 
920 	/*
921 	 * Scale the missed beacon counter threshold to the AP's actual
922 	 * beacon interval.
923 	 */
924 	int btimeout = MIN(IEEE80211_BEACON_MISS_THRES * ic->ic_bss->ni_intval,
925 	    IEEE80211_BEACON_MISS_THRES * (IEEE80211_DUR_TU / 10));
926 	/* Ensure that at least one beacon may be missed. */
927 	btimeout = MAX(btimeout, 2 * ic->ic_bss->ni_intval);
928 	if (ic->ic_bss->ni_intval > 0) /* don't crash if interval is bogus */
929 		ic->ic_bmissthres = btimeout / ic->ic_bss->ni_intval;
930 
931 	if (ifp->if_flags & IFF_DEBUG)
932 		printf("%s: missed beacon threshold set to %d beacons, "
933 		    "beacon interval is %u TU\n", ifp->if_xname,
934 		    ic->ic_bmissthres, ic->ic_bss->ni_intval);
935 }
936 
937 /* Tell our peer, and the driver, to stop A-MPDU Tx for all TIDs. */
938 void
939 ieee80211_stop_ampdu_tx(struct ieee80211com *ic, struct ieee80211_node *ni,
940     int mgt)
941 {
942 	int tid;
943 
944 	for (tid = 0; tid < nitems(ni->ni_tx_ba); tid++) {
945 		struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
946 		if (ba->ba_state != IEEE80211_BA_AGREED)
947 			continue;
948 		ieee80211_delba_request(ic, ni,
949 		    mgt == -1 ? 0 : IEEE80211_REASON_AUTH_LEAVE, 1, tid);
950 	}
951 }
952 
953 void
954 ieee80211_check_wpa_supplicant_failure(struct ieee80211com *ic,
955     struct ieee80211_node *ni)
956 {
957 	struct ieee80211_node *ni2;
958 
959 	if (ic->ic_opmode != IEEE80211_M_STA
960 #ifndef IEEE80211_STA_ONLY
961 	    && ic->ic_opmode != IEEE80211_M_IBSS
962 #endif
963 	    )
964 		return;
965 
966 	if (ni->ni_rsn_supp_state != RSNA_SUPP_PTKNEGOTIATING)
967 		return;
968 
969 	ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_WPA_KEY;
970 
971 	if (ni != ic->ic_bss)
972 		return;
973 
974 	/* Also update the copy of our AP's node in the node cache. */
975 	ni2 = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
976 	if (ni2)
977 		ni2->ni_assoc_fail |= ic->ic_bss->ni_assoc_fail;
978 }
979 
980 int
981 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate,
982     int mgt)
983 {
984 	struct ifnet *ifp = &ic->ic_if;
985 	struct ieee80211_node *ni;
986 	enum ieee80211_state ostate;
987 	u_int rate;
988 #ifndef IEEE80211_STA_ONLY
989 	int s;
990 #endif
991 
992 	ostate = ic->ic_state;
993 	if (ifp->if_flags & IFF_DEBUG)
994 		printf("%s: %s -> %s\n", ifp->if_xname,
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 	ieee80211_set_link_state(ic, LINK_STATE_DOWN);
999 	ic->ic_xflags &= ~IEEE80211_F_TX_MGMT_ONLY;
1000 	switch (nstate) {
1001 	case IEEE80211_S_INIT:
1002 		/*
1003 		 * If mgt = -1, driver is already partway down, so do
1004 		 * not send management frames.
1005 		 */
1006 		switch (ostate) {
1007 		case IEEE80211_S_INIT:
1008 			break;
1009 		case IEEE80211_S_RUN:
1010 			if (mgt == -1)
1011 				goto justcleanup;
1012 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1013 			ieee80211_ba_del(ni);
1014 			switch (ic->ic_opmode) {
1015 			case IEEE80211_M_STA:
1016 				IEEE80211_SEND_MGMT(ic, ni,
1017 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
1018 				    IEEE80211_REASON_ASSOC_LEAVE);
1019 				break;
1020 #ifndef IEEE80211_STA_ONLY
1021 			case IEEE80211_M_HOSTAP:
1022 				s = splnet();
1023 				RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) {
1024 					if (ni->ni_state != IEEE80211_STA_ASSOC)
1025 						continue;
1026 					IEEE80211_SEND_MGMT(ic, ni,
1027 					    IEEE80211_FC0_SUBTYPE_DISASSOC,
1028 					    IEEE80211_REASON_ASSOC_LEAVE);
1029 				}
1030 				splx(s);
1031 				break;
1032 #endif
1033 			default:
1034 				break;
1035 			}
1036 			/* FALLTHROUGH */
1037 		case IEEE80211_S_ASSOC:
1038 			if (mgt == -1)
1039 				goto justcleanup;
1040 			switch (ic->ic_opmode) {
1041 			case IEEE80211_M_STA:
1042 				IEEE80211_SEND_MGMT(ic, ni,
1043 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1044 				    IEEE80211_REASON_AUTH_LEAVE);
1045 				break;
1046 #ifndef IEEE80211_STA_ONLY
1047 			case IEEE80211_M_HOSTAP:
1048 				s = splnet();
1049 				RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) {
1050 					IEEE80211_SEND_MGMT(ic, ni,
1051 					    IEEE80211_FC0_SUBTYPE_DEAUTH,
1052 					    IEEE80211_REASON_AUTH_LEAVE);
1053 				}
1054 				splx(s);
1055 				break;
1056 #endif
1057 			default:
1058 				break;
1059 			}
1060 			/* FALLTHROUGH */
1061 		case IEEE80211_S_AUTH:
1062 		case IEEE80211_S_SCAN:
1063 justcleanup:
1064 #ifndef IEEE80211_STA_ONLY
1065 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1066 				timeout_del(&ic->ic_rsn_timeout);
1067 #endif
1068 			ieee80211_ba_del(ni);
1069 			timeout_del(&ic->ic_bgscan_timeout);
1070 			ic->ic_bgscan_fail = 0;
1071 			ic->ic_mgt_timer = 0;
1072 			mq_purge(&ic->ic_mgtq);
1073 			mq_purge(&ic->ic_pwrsaveq);
1074 			ieee80211_free_allnodes(ic, 1);
1075 			break;
1076 		}
1077 		ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE;
1078 		ni->ni_assoc_fail = 0;
1079 		if (ic->ic_flags & IEEE80211_F_RSNON)
1080 			ieee80211_crypto_clear_groupkeys(ic);
1081 		break;
1082 	case IEEE80211_S_SCAN:
1083 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
1084 		/* initialize bss for probe request */
1085 		IEEE80211_ADDR_COPY(ni->ni_macaddr, etherbroadcastaddr);
1086 		IEEE80211_ADDR_COPY(ni->ni_bssid, etherbroadcastaddr);
1087 		ni->ni_rates = ic->ic_sup_rates[
1088 			ieee80211_chan2mode(ic, ni->ni_chan)];
1089 		ni->ni_associd = 0;
1090 		ni->ni_rstamp = 0;
1091 		ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE;
1092 		if (ic->ic_flags & IEEE80211_F_RSNON)
1093 			ieee80211_crypto_clear_groupkeys(ic);
1094 		switch (ostate) {
1095 		case IEEE80211_S_INIT:
1096 #ifndef IEEE80211_STA_ONLY
1097 			if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1098 			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
1099 				/*
1100 				 * AP operation and we already have a channel;
1101 				 * bypass the scan and startup immediately.
1102 				 */
1103 				ieee80211_create_ibss(ic, ic->ic_des_chan);
1104 			} else
1105 #endif
1106 				ieee80211_begin_scan(ifp);
1107 			break;
1108 		case IEEE80211_S_SCAN:
1109 			/* scan next */
1110 			if (ic->ic_flags & IEEE80211_F_ASCAN) {
1111 				IEEE80211_SEND_MGMT(ic, ni,
1112 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
1113 			}
1114 			break;
1115 		case IEEE80211_S_RUN:
1116 			/* beacon miss */
1117 			if (ifp->if_flags & IFF_DEBUG) {
1118 				/* XXX bssid clobbered above */
1119 				printf("%s: no recent beacons from %s;"
1120 				    " rescanning\n", ifp->if_xname,
1121 				    ether_sprintf(ic->ic_bss->ni_bssid));
1122 			}
1123 			timeout_del(&ic->ic_bgscan_timeout);
1124 			ic->ic_bgscan_fail = 0;
1125 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1126 			ieee80211_free_allnodes(ic, 1);
1127 			/* FALLTHROUGH */
1128 		case IEEE80211_S_AUTH:
1129 		case IEEE80211_S_ASSOC:
1130 			/* timeout restart scan */
1131 			ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
1132 			if (ni != NULL)
1133 				ni->ni_fails++;
1134 			ieee80211_begin_scan(ifp);
1135 			break;
1136 		}
1137 		break;
1138 	case IEEE80211_S_AUTH:
1139 		if (ostate == IEEE80211_S_RUN)
1140 			ieee80211_check_wpa_supplicant_failure(ic, ni);
1141 		ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE;
1142 		if (ic->ic_flags & IEEE80211_F_RSNON)
1143 			ieee80211_crypto_clear_groupkeys(ic);
1144 		switch (ostate) {
1145 		case IEEE80211_S_INIT:
1146 			if (ifp->if_flags & IFF_DEBUG)
1147 				printf("%s: invalid transition %s -> %s\n",
1148 				    ifp->if_xname, ieee80211_state_name[ostate],
1149 				    ieee80211_state_name[nstate]);
1150 			break;
1151 		case IEEE80211_S_SCAN:
1152 			IEEE80211_SEND_MGMT(ic, ni,
1153 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1154 			break;
1155 		case IEEE80211_S_AUTH:
1156 		case IEEE80211_S_ASSOC:
1157 			switch (mgt) {
1158 			case IEEE80211_FC0_SUBTYPE_AUTH:
1159 				if (ic->ic_opmode == IEEE80211_M_STA) {
1160 					IEEE80211_SEND_MGMT(ic, ni,
1161 					    IEEE80211_FC0_SUBTYPE_AUTH,
1162 					    IEEE80211_AUTH_OPEN_REQUEST);
1163 				}
1164 				break;
1165 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1166 				/* ignore and retry scan on timeout */
1167 				break;
1168 			}
1169 			break;
1170 		case IEEE80211_S_RUN:
1171 			timeout_del(&ic->ic_bgscan_timeout);
1172 			ic->ic_bgscan_fail = 0;
1173 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1174 			ieee80211_ba_del(ni);
1175 			switch (mgt) {
1176 			case IEEE80211_FC0_SUBTYPE_AUTH:
1177 				IEEE80211_SEND_MGMT(ic, ni,
1178 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
1179 				ic->ic_state = ostate;	/* stay RUN */
1180 				break;
1181 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
1182 				/* try to reauth */
1183 				IEEE80211_SEND_MGMT(ic, ni,
1184 				    IEEE80211_FC0_SUBTYPE_AUTH, 1);
1185 				break;
1186 			}
1187 			break;
1188 		}
1189 		break;
1190 	case IEEE80211_S_ASSOC:
1191 		switch (ostate) {
1192 		case IEEE80211_S_INIT:
1193 		case IEEE80211_S_SCAN:
1194 		case IEEE80211_S_ASSOC:
1195 			if (ifp->if_flags & IFF_DEBUG)
1196 				printf("%s: invalid transition %s -> %s\n",
1197 				    ifp->if_xname, ieee80211_state_name[ostate],
1198 				    ieee80211_state_name[nstate]);
1199 			break;
1200 		case IEEE80211_S_AUTH:
1201 			IEEE80211_SEND_MGMT(ic, ni,
1202 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1203 			break;
1204 		case IEEE80211_S_RUN:
1205 			ieee80211_stop_ampdu_tx(ic, ni, mgt);
1206 			ieee80211_ba_del(ni);
1207 			IEEE80211_SEND_MGMT(ic, ni,
1208 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
1209 			break;
1210 		}
1211 		break;
1212 	case IEEE80211_S_RUN:
1213 		switch (ostate) {
1214 		case IEEE80211_S_INIT:
1215 			if (ic->ic_opmode == IEEE80211_M_MONITOR)
1216 				break;
1217 		case IEEE80211_S_AUTH:
1218 		case IEEE80211_S_RUN:
1219 			if (ifp->if_flags & IFF_DEBUG)
1220 				printf("%s: invalid transition %s -> %s\n",
1221 				    ifp->if_xname, ieee80211_state_name[ostate],
1222 				    ieee80211_state_name[nstate]);
1223 			break;
1224 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
1225 		case IEEE80211_S_ASSOC:		/* infra mode */
1226 			if (ni->ni_txrate >= ni->ni_rates.rs_nrates)
1227 				panic("%s: bogus xmit rate %u setup",
1228 				    __func__, ni->ni_txrate);
1229 			if (ifp->if_flags & IFF_DEBUG) {
1230 				printf("%s: %s with %s ssid ",
1231 				    ifp->if_xname,
1232 				    ic->ic_opmode == IEEE80211_M_STA ?
1233 				    "associated" : "synchronized",
1234 				    ether_sprintf(ni->ni_bssid));
1235 				ieee80211_print_essid(ic->ic_bss->ni_essid,
1236 				    ni->ni_esslen);
1237 				rate = ni->ni_rates.rs_rates[ni->ni_txrate] &
1238 				    IEEE80211_RATE_VAL;
1239 				printf(" channel %d",
1240 				    ieee80211_chan2ieee(ic, ni->ni_chan));
1241 				if (ni->ni_flags & IEEE80211_NODE_HT)
1242 					printf(" start MCS %u", ni->ni_txmcs);
1243 				else
1244 					printf(" start %u%sMb",
1245 					    rate / 2, (rate & 1) ? ".5" : "");
1246 				printf(" %s preamble %s slot time%s%s\n",
1247 				    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ?
1248 					"short" : "long",
1249 				    (ic->ic_flags & IEEE80211_F_SHSLOT) ?
1250 					"short" : "long",
1251 				    (ic->ic_flags & IEEE80211_F_USEPROT) ?
1252 					" protection enabled" : "",
1253 				    (ni->ni_flags & IEEE80211_NODE_HT) ?
1254 					" HT enabled" : "");
1255 			}
1256 			if (!(ic->ic_flags & IEEE80211_F_RSNON)) {
1257 				/*
1258 				 * NB: When RSN is enabled, we defer setting
1259 				 * the link up until the port is valid.
1260 				 */
1261 				ieee80211_set_link_state(ic, LINK_STATE_UP);
1262 				ni->ni_assoc_fail = 0;
1263 			}
1264 			ic->ic_mgt_timer = 0;
1265 			ieee80211_set_beacon_miss_threshold(ic);
1266 			if_start(ifp);
1267 			break;
1268 		}
1269 		break;
1270 	}
1271 	return 0;
1272 }
1273 
1274 void
1275 ieee80211_set_link_state(struct ieee80211com *ic, int nstate)
1276 {
1277 	struct ifnet *ifp = &ic->ic_if;
1278 
1279 	switch (ic->ic_opmode) {
1280 #ifndef IEEE80211_STA_ONLY
1281 	case IEEE80211_M_IBSS:
1282 	case IEEE80211_M_HOSTAP:
1283 		nstate = LINK_STATE_UNKNOWN;
1284 		break;
1285 #endif
1286 	case IEEE80211_M_MONITOR:
1287 		nstate = LINK_STATE_DOWN;
1288 		break;
1289 	default:
1290 		break;
1291 	}
1292 	if (nstate != ifp->if_link_state) {
1293 		ifp->if_link_state = nstate;
1294 		if (LINK_STATE_IS_UP(nstate)) {
1295 			struct if_ieee80211_data ifie;
1296 			memset(&ifie, 0, sizeof(ifie));
1297 			ifie.ifie_nwid_len = ic->ic_bss->ni_esslen;
1298 			memcpy(ifie.ifie_nwid, ic->ic_bss->ni_essid,
1299 			    sizeof(ifie.ifie_nwid));
1300 			memcpy(ifie.ifie_addr, ic->ic_bss->ni_bssid,
1301 			    sizeof(ifie.ifie_addr));
1302 			ifie.ifie_channel = ieee80211_chan2ieee(ic,
1303 			    ic->ic_bss->ni_chan);
1304 			ifie.ifie_flags = ic->ic_flags;
1305 			ifie.ifie_xflags = ic->ic_xflags;
1306 			rtm_80211info(&ic->ic_if, &ifie);
1307 		}
1308 		if_link_state_change(ifp);
1309 	}
1310 }
1311