xref: /freebsd/sys/net80211/ieee80211_sta.c (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #ifdef __FreeBSD__
30 __FBSDID("$FreeBSD$");
31 #endif
32 
33 /*
34  * IEEE 802.11 Station mode support.
35  */
36 #include "opt_inet.h"
37 #include "opt_wlan.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/endian.h>
48 #include <sys/errno.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51 
52 #include <net/if.h>
53 #include <net/if_media.h>
54 #include <net/if_llc.h>
55 #include <net/if_dl.h>
56 #include <net/if_var.h>
57 #include <net/ethernet.h>
58 
59 #include <net/bpf.h>
60 
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_sta.h>
63 #include <net80211/ieee80211_input.h>
64 #ifdef IEEE80211_SUPPORT_SUPERG
65 #include <net80211/ieee80211_superg.h>
66 #endif
67 #include <net80211/ieee80211_ratectl.h>
68 #include <net80211/ieee80211_sta.h>
69 #include <net80211/ieee80211_vht.h>
70 
71 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
72 
73 static	void sta_vattach(struct ieee80211vap *);
74 static	void sta_beacon_miss(struct ieee80211vap *);
75 static	int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
76 static	int sta_input(struct ieee80211_node *, struct mbuf *,
77 	    const struct ieee80211_rx_stats *, int, int);
78 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
79 	    int subtype, const struct ieee80211_rx_stats *, int rssi, int nf);
80 static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
81 
82 void
83 ieee80211_sta_attach(struct ieee80211com *ic)
84 {
85 	ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
86 }
87 
88 void
89 ieee80211_sta_detach(struct ieee80211com *ic)
90 {
91 }
92 
93 static void
94 sta_vdetach(struct ieee80211vap *vap)
95 {
96 }
97 
98 static void
99 sta_vattach(struct ieee80211vap *vap)
100 {
101 	vap->iv_newstate = sta_newstate;
102 	vap->iv_input = sta_input;
103 	vap->iv_recv_mgmt = sta_recv_mgmt;
104 	vap->iv_recv_ctl = sta_recv_ctl;
105 	vap->iv_opdetach = sta_vdetach;
106 	vap->iv_bmiss = sta_beacon_miss;
107 }
108 
109 /*
110  * Handle a beacon miss event.  The common code filters out
111  * spurious events that can happen when scanning and/or before
112  * reaching RUN state.
113  */
114 static void
115 sta_beacon_miss(struct ieee80211vap *vap)
116 {
117 	struct ieee80211com *ic = vap->iv_ic;
118 
119 	IEEE80211_LOCK_ASSERT(ic);
120 
121 	KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
122 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
123 	    ("wrong state %s", ieee80211_state_name[vap->iv_state]));
124 
125 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
126 	    "beacon miss, mode %s state %s\n",
127 	    ieee80211_opmode_name[vap->iv_opmode],
128 	    ieee80211_state_name[vap->iv_state]);
129 
130 	if (vap->iv_state == IEEE80211_S_CSA) {
131 		/*
132 		 * A Channel Switch is pending; assume we missed the
133 		 * beacon that would've completed the process and just
134 		 * force the switch.  If we made a mistake we'll not
135 		 * find the AP on the new channel and fall back to a
136 		 * normal scan.
137 		 */
138 		ieee80211_csa_completeswitch(ic);
139 		return;
140 	}
141 	if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
142 		/*
143 		 * Send a directed probe req before falling back to a
144 		 * scan; if we receive a response ic_bmiss_count will
145 		 * be reset.  Some cards mistakenly report beacon miss
146 		 * so this avoids the expensive scan if the ap is
147 		 * still there.
148 		 */
149 		ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
150 			vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
151 			vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
152 		return;
153 	}
154 
155 	callout_stop(&vap->iv_swbmiss);
156 	vap->iv_bmiss_count = 0;
157 	vap->iv_stats.is_beacon_miss++;
158 	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
159 #ifdef IEEE80211_SUPPORT_SUPERG
160 
161 		/*
162 		 * If we receive a beacon miss interrupt when using
163 		 * dynamic turbo, attempt to switch modes before
164 		 * reassociating.
165 		 */
166 		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
167 			ieee80211_dturbo_switch(vap,
168 			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
169 #endif
170 		/*
171 		 * Try to reassociate before scanning for a new ap.
172 		 */
173 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
174 	} else {
175 		/*
176 		 * Somebody else is controlling state changes (e.g.
177 		 * a user-mode app) don't do anything that would
178 		 * confuse them; just drop into scan mode so they'll
179 		 * notified of the state change and given control.
180 		 */
181 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
182 	}
183 }
184 
185 /*
186  * Handle deauth with reason.  We retry only for
187  * the cases where we might succeed.  Otherwise
188  * we downgrade the ap and scan.
189  */
190 static void
191 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
192 {
193 	switch (reason) {
194 	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
195 	case IEEE80211_STATUS_TIMEOUT:
196 	case IEEE80211_REASON_ASSOC_EXPIRE:
197 	case IEEE80211_REASON_NOT_AUTHED:
198 	case IEEE80211_REASON_NOT_ASSOCED:
199 	case IEEE80211_REASON_ASSOC_LEAVE:
200 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
201 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
202 		break;
203 	default:
204 		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
205 		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
206 			ieee80211_check_scan_current(vap);
207 		break;
208 	}
209 }
210 
211 static void
212 sta_swbmiss_start(struct ieee80211vap *vap)
213 {
214 
215 	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) {
216 		/*
217 		 * Start s/w beacon miss timer for devices w/o
218 		 * hardware support.  We fudge a bit here since
219 		 * we're doing this in software.
220 		 */
221 		vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
222 		    2 * vap->iv_bmissthreshold * vap->iv_bss->ni_intval);
223 		vap->iv_swbmiss_count = 0;
224 		callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
225 		    ieee80211_swbmiss, vap);
226 	}
227 }
228 
229 /*
230  * IEEE80211_M_STA vap state machine handler.
231  * This routine handles the main states in the 802.11 protocol.
232  */
233 static int
234 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
235 {
236 	struct ieee80211com *ic = vap->iv_ic;
237 	struct ieee80211_node *ni;
238 	enum ieee80211_state ostate;
239 
240 	IEEE80211_LOCK_ASSERT(ic);
241 
242 	ostate = vap->iv_state;
243 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
244 	    __func__, ieee80211_state_name[ostate],
245 	    ieee80211_state_name[nstate], arg);
246 	vap->iv_state = nstate;			/* state transition */
247 	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
248 	if (ostate != IEEE80211_S_SCAN)
249 		ieee80211_cancel_scan(vap);	/* background scan */
250 	ni = vap->iv_bss;			/* NB: no reference held */
251 	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
252 		callout_stop(&vap->iv_swbmiss);
253 	switch (nstate) {
254 	case IEEE80211_S_INIT:
255 		switch (ostate) {
256 		case IEEE80211_S_SLEEP:
257 			/* XXX wakeup */
258 			/* XXX driver hook to wakeup the hardware? */
259 		case IEEE80211_S_RUN:
260 			IEEE80211_SEND_MGMT(ni,
261 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
262 			    IEEE80211_REASON_ASSOC_LEAVE);
263 			ieee80211_sta_leave(ni);
264 			break;
265 		case IEEE80211_S_ASSOC:
266 			IEEE80211_SEND_MGMT(ni,
267 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
268 			    IEEE80211_REASON_AUTH_LEAVE);
269 			break;
270 		case IEEE80211_S_SCAN:
271 			ieee80211_cancel_scan(vap);
272 			break;
273 		default:
274 			break;
275 		}
276 		if (ostate != IEEE80211_S_INIT) {
277 			/* NB: optimize INIT -> INIT case */
278 			ieee80211_reset_bss(vap);
279 		}
280 		if (vap->iv_auth->ia_detach != NULL)
281 			vap->iv_auth->ia_detach(vap);
282 		break;
283 	case IEEE80211_S_SCAN:
284 		switch (ostate) {
285 		case IEEE80211_S_INIT:
286 			/*
287 			 * Initiate a scan.  We can come here as a result
288 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
289 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
290 			 * and the scan request parameters will be present
291 			 * in iv_scanreq.  Otherwise we do the default.
292 			 */
293 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
294 				ieee80211_check_scan(vap,
295 				    vap->iv_scanreq_flags,
296 				    vap->iv_scanreq_duration,
297 				    vap->iv_scanreq_mindwell,
298 				    vap->iv_scanreq_maxdwell,
299 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
300 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
301 			} else
302 				ieee80211_check_scan_current(vap);
303 			break;
304 		case IEEE80211_S_SCAN:
305 		case IEEE80211_S_AUTH:
306 		case IEEE80211_S_ASSOC:
307 			/*
308 			 * These can happen either because of a timeout
309 			 * on an assoc/auth response or because of a
310 			 * change in state that requires a reset.  For
311 			 * the former we're called with a non-zero arg
312 			 * that is the cause for the failure; pass this
313 			 * to the scan code so it can update state.
314 			 * Otherwise trigger a new scan unless we're in
315 			 * manual roaming mode in which case an application
316 			 * must issue an explicit scan request.
317 			 */
318 			if (arg != 0)
319 				ieee80211_scan_assoc_fail(vap,
320 					vap->iv_bss->ni_macaddr, arg);
321 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
322 				ieee80211_check_scan_current(vap);
323 			break;
324 		case IEEE80211_S_SLEEP:		/* beacon miss */
325 			/*
326 			 * XXX if in sleep we need to wakeup the hardware.
327 			 */
328 			/* FALLTHROUGH */
329 		case IEEE80211_S_RUN:		/* beacon miss */
330 			/*
331 			 * Beacon miss.  Notify user space and if not
332 			 * under control of a user application (roaming
333 			 * manual) kick off a scan to re-connect.
334 			 */
335 
336 			ieee80211_sta_leave(ni);
337 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
338 				ieee80211_check_scan_current(vap);
339 			break;
340 		default:
341 			goto invalid;
342 		}
343 		break;
344 	case IEEE80211_S_AUTH:
345 		switch (ostate) {
346 		case IEEE80211_S_INIT:
347 		case IEEE80211_S_SCAN:
348 			IEEE80211_SEND_MGMT(ni,
349 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
350 			break;
351 		case IEEE80211_S_AUTH:
352 		case IEEE80211_S_ASSOC:
353 			switch (arg & 0xff) {
354 			case IEEE80211_FC0_SUBTYPE_AUTH:
355 				/* ??? */
356 				IEEE80211_SEND_MGMT(ni,
357 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
358 				break;
359 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
360 				sta_authretry(vap, ni, arg>>8);
361 				break;
362 			}
363 			break;
364 		case IEEE80211_S_SLEEP:
365 		case IEEE80211_S_RUN:
366 			switch (arg & 0xff) {
367 			case IEEE80211_FC0_SUBTYPE_AUTH:
368 				IEEE80211_SEND_MGMT(ni,
369 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
370 				vap->iv_state = IEEE80211_S_RUN; /* stay RUN */
371 				break;
372 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
373 				ieee80211_sta_leave(ni);
374 				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
375 					/* try to reauth */
376 					IEEE80211_SEND_MGMT(ni,
377 					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
378 				}
379 				break;
380 			}
381 			break;
382 		default:
383 			goto invalid;
384 		}
385 		break;
386 	case IEEE80211_S_ASSOC:
387 		switch (ostate) {
388 		case IEEE80211_S_AUTH:
389 		case IEEE80211_S_ASSOC:
390 			IEEE80211_SEND_MGMT(ni,
391 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
392 			break;
393 		case IEEE80211_S_SLEEP:		/* cannot happen */
394 		case IEEE80211_S_RUN:
395 			ieee80211_sta_leave(ni);
396 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
397 				IEEE80211_SEND_MGMT(ni, arg ?
398 				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
399 				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
400 			}
401 			break;
402 		default:
403 			goto invalid;
404 		}
405 		break;
406 	case IEEE80211_S_RUN:
407 		if (vap->iv_flags & IEEE80211_F_WPA) {
408 			/* XXX validate prerequisites */
409 		}
410 		switch (ostate) {
411 		case IEEE80211_S_RUN:
412 		case IEEE80211_S_CSA:
413 			break;
414 		case IEEE80211_S_AUTH:		/* when join is done in fw */
415 		case IEEE80211_S_ASSOC:
416 #ifdef IEEE80211_DEBUG
417 			if (ieee80211_msg_debug(vap)) {
418 				ieee80211_note(vap, "%s with %s ssid ",
419 				    (vap->iv_opmode == IEEE80211_M_STA ?
420 				    "associated" : "synchronized"),
421 				    ether_sprintf(ni->ni_bssid));
422 				ieee80211_print_essid(vap->iv_bss->ni_essid,
423 				    ni->ni_esslen);
424 				/* XXX MCS/HT */
425 				printf(" channel %d start %uMb\n",
426 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
427 				    IEEE80211_RATE2MBS(ni->ni_txrate));
428 			}
429 #endif
430 			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
431 			ieee80211_notify_node_join(ni,
432 			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
433 			break;
434 		case IEEE80211_S_SLEEP:
435 			/* Wake up from sleep */
436 			vap->iv_sta_ps(vap, 0);
437 			break;
438 		default:
439 			goto invalid;
440 		}
441 		ieee80211_sync_curchan(ic);
442 		if (ostate != IEEE80211_S_RUN)
443 			sta_swbmiss_start(vap);
444 		/*
445 		 * When 802.1x is not in use mark the port authorized
446 		 * at this point so traffic can flow.
447 		 */
448 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
449 			ieee80211_node_authorize(ni);
450 		/*
451 		 * Fake association when joining an existing bss.
452 		 *
453 		 * Don't do this if we're doing SLEEP->RUN.
454 		 */
455 		if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
456 			ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
457 		break;
458 	case IEEE80211_S_CSA:
459 		if (ostate != IEEE80211_S_RUN)
460 			goto invalid;
461 		break;
462 	case IEEE80211_S_SLEEP:
463 		sta_swbmiss_start(vap);
464 		vap->iv_sta_ps(vap, 1);
465 		break;
466 	default:
467 	invalid:
468 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
469 		    "%s: unexpected state transition %s -> %s\n", __func__,
470 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
471 		break;
472 	}
473 	return 0;
474 }
475 
476 /*
477  * Return non-zero if the frame is an echo of a multicast
478  * frame sent by ourself.  The dir is known to be DSTODS.
479  */
480 static __inline int
481 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
482 {
483 #define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
484 #define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
485 	const uint8_t *sa;
486 
487 	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
488 
489 	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
490 		return 0;
491 	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
492 	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
493 #undef WH4
494 #undef QWH4
495 }
496 
497 /*
498  * Return non-zero if the frame is an echo of a multicast
499  * frame sent by ourself.  The dir is known to be FROMDS.
500  */
501 static __inline int
502 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
503 {
504 	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
505 
506 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
507 		return 0;
508 	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
509 }
510 
511 /*
512  * Decide if a received management frame should be
513  * printed when debugging is enabled.  This filters some
514  * of the less interesting frames that come frequently
515  * (e.g. beacons).
516  */
517 static __inline int
518 doprint(struct ieee80211vap *vap, int subtype)
519 {
520 	switch (subtype) {
521 	case IEEE80211_FC0_SUBTYPE_BEACON:
522 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
523 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
524 		return 0;
525 	}
526 	return 1;
527 }
528 
529 /*
530  * Process a received frame.  The node associated with the sender
531  * should be supplied.  If nothing was found in the node table then
532  * the caller is assumed to supply a reference to iv_bss instead.
533  * The RSSI and a timestamp are also supplied.  The RSSI data is used
534  * during AP scanning to select a AP to associate with; it can have
535  * any units so long as values have consistent units and higher values
536  * mean ``better signal''.  The receive timestamp is currently not used
537  * by the 802.11 layer.
538  */
539 static int
540 sta_input(struct ieee80211_node *ni, struct mbuf *m,
541     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
542 {
543 	struct ieee80211vap *vap = ni->ni_vap;
544 	struct ieee80211com *ic = ni->ni_ic;
545 	struct ifnet *ifp = vap->iv_ifp;
546 	struct ieee80211_frame *wh;
547 	struct ieee80211_key *key;
548 	struct ether_header *eh;
549 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
550 	uint8_t dir, type, subtype, qos;
551 	uint8_t *bssid;
552 	int is_hw_decrypted = 0;
553 	int has_decrypted = 0;
554 
555 	/*
556 	 * Some devices do hardware decryption all the way through
557 	 * to pretending the frame wasn't encrypted in the first place.
558 	 * So, tag it appropriately so it isn't discarded inappropriately.
559 	 */
560 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
561 		is_hw_decrypted = 1;
562 
563 	if (m->m_flags & M_AMPDU_MPDU) {
564 		/*
565 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
566 		 * w/ M_AMPDU_MPDU marked have already passed through
567 		 * here but were received out of order and been held on
568 		 * the reorder queue.  When resubmitted they are marked
569 		 * with the M_AMPDU_MPDU flag and we can bypass most of
570 		 * the normal processing.
571 		 */
572 		wh = mtod(m, struct ieee80211_frame *);
573 		type = IEEE80211_FC0_TYPE_DATA;
574 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
575 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
576 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
577 		goto resubmit_ampdu;
578 	}
579 
580 	KASSERT(ni != NULL, ("null node"));
581 	ni->ni_inact = ni->ni_inact_reload;
582 
583 	type = -1;			/* undefined */
584 
585 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
586 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
587 		    ni->ni_macaddr, NULL,
588 		    "too short (1): len %u", m->m_pkthdr.len);
589 		vap->iv_stats.is_rx_tooshort++;
590 		goto out;
591 	}
592 	/*
593 	 * Bit of a cheat here, we use a pointer for a 3-address
594 	 * frame format but don't reference fields past outside
595 	 * ieee80211_frame_min w/o first validating the data is
596 	 * present.
597 	 */
598 	wh = mtod(m, struct ieee80211_frame *);
599 
600 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
601 	    IEEE80211_FC0_VERSION_0) {
602 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
603 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
604 		    wh->i_fc[0], wh->i_fc[1]);
605 		vap->iv_stats.is_rx_badversion++;
606 		goto err;
607 	}
608 
609 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
610 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
611 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
612 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
613 		bssid = wh->i_addr2;
614 		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
615 			/* not interested in */
616 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
617 			    bssid, NULL, "%s", "not to bss");
618 			vap->iv_stats.is_rx_wrongbss++;
619 			goto out;
620 		}
621 
622 		/*
623 		 * Some devices may be in a promiscuous mode
624 		 * where they receive frames for multiple station
625 		 * addresses.
626 		 *
627 		 * If we receive a data frame that isn't
628 		 * destined to our VAP MAC, drop it.
629 		 *
630 		 * XXX TODO: This is only enforced when not scanning;
631 		 * XXX it assumes a software-driven scan will put the NIC
632 		 * XXX into a "no data frames" mode before setting this
633 		 * XXX flag. Otherwise it may be possible that we'll still
634 		 * XXX process data frames whilst scanning.
635 		 */
636 		if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
637 		    && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
638 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
639 			    bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
640 			    IF_LLADDR(ifp), ":", wh->i_addr1, ":");
641 			vap->iv_stats.is_rx_wrongbss++;
642 			goto out;
643 		}
644 
645 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
646 		ni->ni_noise = nf;
647 		if ( IEEE80211_HAS_SEQ(type, subtype) &&
648 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
649 			uint8_t tid = ieee80211_gettid(wh);
650 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
651 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
652 				ic->ic_wme.wme_hipri_traffic++;
653 			if (! ieee80211_check_rxseq(ni, wh, bssid, rxs))
654 				goto out;
655 		}
656 	}
657 
658 	switch (type) {
659 	case IEEE80211_FC0_TYPE_DATA:
660 		hdrspace = ieee80211_hdrspace(ic, wh);
661 		if (m->m_len < hdrspace &&
662 		    (m = m_pullup(m, hdrspace)) == NULL) {
663 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
664 			    ni->ni_macaddr, NULL,
665 			    "data too short: expecting %u", hdrspace);
666 			vap->iv_stats.is_rx_tooshort++;
667 			goto out;		/* XXX */
668 		}
669 		/*
670 		 * Handle A-MPDU re-ordering.  If the frame is to be
671 		 * processed directly then ieee80211_ampdu_reorder
672 		 * will return 0; otherwise it has consumed the mbuf
673 		 * and we should do nothing more with it.
674 		 */
675 		if ((m->m_flags & M_AMPDU) &&
676 		    (dir == IEEE80211_FC1_DIR_FROMDS ||
677 		     dir == IEEE80211_FC1_DIR_DSTODS) &&
678 		    ieee80211_ampdu_reorder(ni, m, rxs) != 0) {
679 			m = NULL;
680 			goto out;
681 		}
682 	resubmit_ampdu:
683 		if (dir == IEEE80211_FC1_DIR_FROMDS) {
684 			if ((ifp->if_flags & IFF_SIMPLEX) &&
685 			    isfromds_mcastecho(vap, wh)) {
686 				/*
687 				 * In IEEE802.11 network, multicast
688 				 * packets sent from "me" are broadcast
689 				 * from the AP; silently discard for
690 				 * SIMPLEX interface.
691 				 */
692 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
693 				    wh, "data", "%s", "multicast echo");
694 				vap->iv_stats.is_rx_mcastecho++;
695 				goto out;
696 			}
697 			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
698 			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
699 				/*
700 				 * DWDS sta's must drop 3-address mcast frames
701 				 * as they will be sent separately as a 4-addr
702 				 * frame.  Accepting the 3-addr frame will
703 				 * confuse the bridge into thinking the sending
704 				 * sta is located at the end of WDS link.
705 				 */
706 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
707 				    "3-address data", "%s", "DWDS enabled");
708 				vap->iv_stats.is_rx_mcastecho++;
709 				goto out;
710 			}
711 		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
712 			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
713 				IEEE80211_DISCARD(vap,
714 				    IEEE80211_MSG_INPUT, wh, "4-address data",
715 				    "%s", "DWDS not enabled");
716 				vap->iv_stats.is_rx_wrongdir++;
717 				goto out;
718 			}
719 			if ((ifp->if_flags & IFF_SIMPLEX) &&
720 			    isdstods_mcastecho(vap, wh)) {
721 				/*
722 				 * In IEEE802.11 network, multicast
723 				 * packets sent from "me" are broadcast
724 				 * from the AP; silently discard for
725 				 * SIMPLEX interface.
726 				 */
727 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
728 				    "4-address data", "%s", "multicast echo");
729 				vap->iv_stats.is_rx_mcastecho++;
730 				goto out;
731 			}
732 		} else {
733 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
734 			    "data", "incorrect dir 0x%x", dir);
735 			vap->iv_stats.is_rx_wrongdir++;
736 			goto out;
737 		}
738 
739 		/*
740 		 * Handle privacy requirements for hardware decryption
741 		 * devices.
742 		 *
743 		 * For those devices, a handful of things happen.
744 		 *
745 		 * + If IV has been stripped, then we can't run
746 		 *   ieee80211_crypto_decap() - none of the key
747 		 * + If MIC has been stripped, we can't validate
748 		 *   MIC here.
749 		 * + If MIC fails, then we need to communicate a
750 		 *   MIC failure up to the stack - but we don't know
751 		 *   which key was used.
752 		 */
753 
754 		/*
755 		 * Handle privacy requirements.  Note that we
756 		 * must not be preempted from here until after
757 		 * we (potentially) call ieee80211_crypto_demic;
758 		 * otherwise we may violate assumptions in the
759 		 * crypto cipher modules used to do delayed update
760 		 * of replay sequence numbers.
761 		 */
762 		if (is_hw_decrypted || wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
763 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
764 				/*
765 				 * Discard encrypted frames when privacy is off.
766 				 */
767 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
768 				    wh, "WEP", "%s", "PRIVACY off");
769 				vap->iv_stats.is_rx_noprivacy++;
770 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
771 				goto out;
772 			}
773 			if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
774 				/* NB: stats+msgs handled in crypto_decap */
775 				IEEE80211_NODE_STAT(ni, rx_wepfail);
776 				goto out;
777 			}
778 			wh = mtod(m, struct ieee80211_frame *);
779 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
780 			has_decrypted = 1;
781 		} else {
782 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
783 			key = NULL;
784 		}
785 
786 		/*
787 		 * Save QoS bits for use below--before we strip the header.
788 		 */
789 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS)
790 			qos = ieee80211_getqos(wh)[0];
791 		else
792 			qos = 0;
793 
794 		/*
795 		 * Next up, any fragmentation.
796 		 */
797 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
798 			m = ieee80211_defrag(ni, m, hdrspace);
799 			if (m == NULL) {
800 				/* Fragment dropped or frame not complete yet */
801 				goto out;
802 			}
803 		}
804 		wh = NULL;		/* no longer valid, catch any uses */
805 
806 		/*
807 		 * Next strip any MSDU crypto bits.
808 		 *
809 		 * Note: we can't do MIC stripping/verification if the
810 		 * upper layer has stripped it.  We have to check MIC
811 		 * ourselves.  So, key may be NULL, but we have to check
812 		 * the RX status.
813 		 */
814 		if (!ieee80211_crypto_demic(vap, key, m, 0)) {
815 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
816 			    ni->ni_macaddr, "data", "%s", "demic error");
817 			vap->iv_stats.is_rx_demicfail++;
818 			IEEE80211_NODE_STAT(ni, rx_demicfail);
819 			goto out;
820 		}
821 
822 		/* copy to listener after decrypt */
823 		if (ieee80211_radiotap_active_vap(vap))
824 			ieee80211_radiotap_rx(vap, m);
825 		need_tap = 0;
826 
827 		/*
828 		 * Finally, strip the 802.11 header.
829 		 */
830 		m = ieee80211_decap(vap, m, hdrspace);
831 		if (m == NULL) {
832 			/* XXX mask bit to check for both */
833 			/* don't count Null data frames as errors */
834 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
835 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
836 				goto out;
837 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
838 			    ni->ni_macaddr, "data", "%s", "decap error");
839 			vap->iv_stats.is_rx_decap++;
840 			IEEE80211_NODE_STAT(ni, rx_decap);
841 			goto err;
842 		}
843 		eh = mtod(m, struct ether_header *);
844 		if (!ieee80211_node_is_authorized(ni)) {
845 			/*
846 			 * Deny any non-PAE frames received prior to
847 			 * authorization.  For open/shared-key
848 			 * authentication the port is mark authorized
849 			 * after authentication completes.  For 802.1x
850 			 * the port is not marked authorized by the
851 			 * authenticator until the handshake has completed.
852 			 */
853 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
854 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
855 				    eh->ether_shost, "data",
856 				    "unauthorized port: ether type 0x%x len %u",
857 				    eh->ether_type, m->m_pkthdr.len);
858 				vap->iv_stats.is_rx_unauth++;
859 				IEEE80211_NODE_STAT(ni, rx_unauth);
860 				goto err;
861 			}
862 		} else {
863 			/*
864 			 * When denying unencrypted frames, discard
865 			 * any non-PAE frames received without encryption.
866 			 */
867 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
868 			    ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
869 			    (is_hw_decrypted == 0) &&
870 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
871 				/*
872 				 * Drop unencrypted frames.
873 				 */
874 				vap->iv_stats.is_rx_unencrypted++;
875 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
876 				goto out;
877 			}
878 		}
879 		/* XXX require HT? */
880 		if (qos & IEEE80211_QOS_AMSDU) {
881 			m = ieee80211_decap_amsdu(ni, m);
882 			if (m == NULL)
883 				return IEEE80211_FC0_TYPE_DATA;
884 		} else {
885 #ifdef IEEE80211_SUPPORT_SUPERG
886 			m = ieee80211_decap_fastframe(vap, ni, m);
887 			if (m == NULL)
888 				return IEEE80211_FC0_TYPE_DATA;
889 #endif
890 		}
891 		ieee80211_deliver_data(vap, ni, m);
892 		return IEEE80211_FC0_TYPE_DATA;
893 
894 	case IEEE80211_FC0_TYPE_MGT:
895 		vap->iv_stats.is_rx_mgmt++;
896 		IEEE80211_NODE_STAT(ni, rx_mgmt);
897 		if (dir != IEEE80211_FC1_DIR_NODS) {
898 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
899 			    wh, "data", "incorrect dir 0x%x", dir);
900 			vap->iv_stats.is_rx_wrongdir++;
901 			goto err;
902 		}
903 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
904 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
905 			    ni->ni_macaddr, "mgt", "too short: len %u",
906 			    m->m_pkthdr.len);
907 			vap->iv_stats.is_rx_tooshort++;
908 			goto out;
909 		}
910 #ifdef IEEE80211_DEBUG
911 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
912 		    ieee80211_msg_dumppkts(vap)) {
913 			if_printf(ifp, "received %s from %s rssi %d\n",
914 			    ieee80211_mgt_subtype_name(subtype),
915 			    ether_sprintf(wh->i_addr2), rssi);
916 		}
917 #endif
918 
919 		/*
920 		 * Note: See above for hardware offload privacy requirements.
921 		 *       It also applies here.
922 		 */
923 
924 		/*
925 		 * Again, having encrypted flag set check would be good, but
926 		 * then we have to also handle crypto_decap() like above.
927 		 */
928 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
929 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
930 				/*
931 				 * Only shared key auth frames with a challenge
932 				 * should be encrypted, discard all others.
933 				 */
934 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
935 				    wh, ieee80211_mgt_subtype_name(subtype),
936 				    "%s", "WEP set but not permitted");
937 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
938 				goto out;
939 			}
940 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
941 				/*
942 				 * Discard encrypted frames when privacy is off.
943 				 */
944 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
945 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
946 				vap->iv_stats.is_rx_noprivacy++;
947 				goto out;
948 			}
949 			hdrspace = ieee80211_hdrspace(ic, wh);
950 
951 			/*
952 			 * Again, if IV/MIC was stripped, then this whole
953 			 * setup will fail.  That's going to need some poking.
954 			 */
955 			if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
956 				/* NB: stats+msgs handled in crypto_decap */
957 				goto out;
958 			}
959 			has_decrypted = 1;
960 			wh = mtod(m, struct ieee80211_frame *);
961 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
962 		}
963 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
964 		goto out;
965 
966 	case IEEE80211_FC0_TYPE_CTL:
967 		vap->iv_stats.is_rx_ctl++;
968 		IEEE80211_NODE_STAT(ni, rx_ctrl);
969 		vap->iv_recv_ctl(ni, m, subtype);
970 		goto out;
971 
972 	default:
973 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
974 		    wh, NULL, "bad frame type 0x%x", type);
975 		/* should not come here */
976 		break;
977 	}
978 err:
979 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
980 out:
981 	if (m != NULL) {
982 		if (need_tap && ieee80211_radiotap_active_vap(vap))
983 			ieee80211_radiotap_rx(vap, m);
984 		m_freem(m);
985 	}
986 	return type;
987 }
988 
989 static void
990 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
991     int rssi, int nf, uint16_t seq, uint16_t status)
992 {
993 	struct ieee80211vap *vap = ni->ni_vap;
994 
995 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
996 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
997 		    ni->ni_macaddr, "open auth",
998 		    "bad sta auth mode %u", ni->ni_authmode);
999 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
1000 		return;
1001 	}
1002 	if (vap->iv_state != IEEE80211_S_AUTH ||
1003 	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
1004 		vap->iv_stats.is_rx_bad_auth++;
1005 		return;
1006 	}
1007 	if (status != 0) {
1008 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1009 		    ni, "open auth failed (reason %d)", status);
1010 		vap->iv_stats.is_rx_auth_fail++;
1011 		vap->iv_stats.is_rx_authfail_code = status;
1012 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1013 		    IEEE80211_SCAN_FAIL_STATUS);
1014 	} else
1015 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1016 }
1017 
1018 static void
1019 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
1020     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
1021     uint16_t seq, uint16_t status)
1022 {
1023 	struct ieee80211vap *vap = ni->ni_vap;
1024 	uint8_t *challenge;
1025 
1026 	/*
1027 	 * NB: this can happen as we allow pre-shared key
1028 	 * authentication to be enabled w/o wep being turned
1029 	 * on so that configuration of these can be done
1030 	 * in any order.  It may be better to enforce the
1031 	 * ordering in which case this check would just be
1032 	 * for sanity/consistency.
1033 	 */
1034 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1035 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1036 		    ni->ni_macaddr, "shared key auth",
1037 		    "%s", " PRIVACY is disabled");
1038 		goto bad;
1039 	}
1040 	/*
1041 	 * Pre-shared key authentication is evil; accept
1042 	 * it only if explicitly configured (it is supported
1043 	 * mainly for compatibility with clients like OS X).
1044 	 */
1045 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1046 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1047 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1048 		    ni->ni_macaddr, "shared key auth",
1049 		    "bad sta auth mode %u", ni->ni_authmode);
1050 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1051 		goto bad;
1052 	}
1053 
1054 	challenge = NULL;
1055 	if (frm + 1 < efrm) {
1056 		if ((frm[1] + 2) > (efrm - frm)) {
1057 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1058 			    ni->ni_macaddr, "shared key auth",
1059 			    "ie %d/%d too long",
1060 			    frm[0], (frm[1] + 2) - (efrm - frm));
1061 			vap->iv_stats.is_rx_bad_auth++;
1062 			goto bad;
1063 		}
1064 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1065 			challenge = frm;
1066 		frm += frm[1] + 2;
1067 	}
1068 	switch (seq) {
1069 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1070 	case IEEE80211_AUTH_SHARED_RESPONSE:
1071 		if (challenge == NULL) {
1072 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1073 			    ni->ni_macaddr, "shared key auth",
1074 			    "%s", "no challenge");
1075 			vap->iv_stats.is_rx_bad_auth++;
1076 			goto bad;
1077 		}
1078 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1079 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1080 			    ni->ni_macaddr, "shared key auth",
1081 			    "bad challenge len %d", challenge[1]);
1082 			vap->iv_stats.is_rx_bad_auth++;
1083 			goto bad;
1084 		}
1085 	default:
1086 		break;
1087 	}
1088 	if (vap->iv_state != IEEE80211_S_AUTH)
1089 		return;
1090 	switch (seq) {
1091 	case IEEE80211_AUTH_SHARED_PASS:
1092 		if (ni->ni_challenge != NULL) {
1093 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1094 			ni->ni_challenge = NULL;
1095 		}
1096 		if (status != 0) {
1097 			IEEE80211_NOTE_FRAME(vap,
1098 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1099 			    "shared key auth failed (reason %d)", status);
1100 			vap->iv_stats.is_rx_auth_fail++;
1101 			vap->iv_stats.is_rx_authfail_code = status;
1102 			return;
1103 		}
1104 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1105 		break;
1106 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1107 		if (!ieee80211_alloc_challenge(ni))
1108 			return;
1109 		/* XXX could optimize by passing recvd challenge */
1110 		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1111 		IEEE80211_SEND_MGMT(ni,
1112 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1113 		break;
1114 	default:
1115 		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1116 		    wh, "shared key auth", "bad seq %d", seq);
1117 		vap->iv_stats.is_rx_bad_auth++;
1118 		return;
1119 	}
1120 	return;
1121 bad:
1122 	/*
1123 	 * Kick the state machine.  This short-circuits
1124 	 * using the mgt frame timeout to trigger the
1125 	 * state transition.
1126 	 */
1127 	if (vap->iv_state == IEEE80211_S_AUTH)
1128 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1129 		    IEEE80211_SCAN_FAIL_STATUS);
1130 }
1131 
1132 /*
1133  * Parse the WME IE for QoS and U-APSD information.
1134  *
1135  * Returns -1 if the IE isn't found, 1 if it's found.
1136  */
1137 int
1138 ieee80211_parse_wmeie(uint8_t *frm, const struct ieee80211_frame *wh,
1139     struct ieee80211_node *ni)
1140 {
1141 	u_int len = frm[1];
1142 
1143 	ni->ni_uapsd = 0;
1144 
1145 	if (len < sizeof(struct ieee80211_wme_param)-2) {
1146 		IEEE80211_DISCARD_IE(ni->ni_vap,
1147 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1148 		    wh, "WME", "too short, len %u", len);
1149 		return -1;
1150 	}
1151 
1152 	ni->ni_uapsd = frm[WME_CAPINFO_IE_OFFSET];
1153 
1154 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_POWER | IEEE80211_MSG_ASSOC,
1155 	    ni, "U-APSD settings from STA: 0x%02x", ni->ni_uapsd);
1156 
1157 	return 1;
1158 }
1159 
1160 int
1161 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1162 	const struct ieee80211_frame *wh, uint8_t *qosinfo)
1163 {
1164 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1165 	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1166 	u_int len = frm[1], qosinfo_count;
1167 	int i;
1168 
1169 	*qosinfo = 0;
1170 
1171 	if (len < sizeof(struct ieee80211_wme_param)-2) {
1172 		IEEE80211_DISCARD_IE(vap,
1173 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1174 		    wh, "WME", "too short, len %u", len);
1175 		return -1;
1176 	}
1177 	*qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1178 	qosinfo_count = *qosinfo & WME_QOSINFO_COUNT;
1179 
1180 	/* XXX do proper check for wraparound */
1181 	if (qosinfo_count == wme->wme_wmeChanParams.cap_info)
1182 		return 0;
1183 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1184 	for (i = 0; i < WME_NUM_AC; i++) {
1185 		struct wmeParams *wmep =
1186 			&wme->wme_wmeChanParams.cap_wmeParams[i];
1187 		/* NB: ACI not used */
1188 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1189 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1190 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1191 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1192 		wmep->wmep_txopLimit = le16dec(frm+2);
1193 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1194 		    "%s: WME: %d: acm=%d aifsn=%d logcwmin=%d logcwmax=%d txopLimit=%d\n",
1195 		    __func__,
1196 		    i,
1197 		    wmep->wmep_acm,
1198 		    wmep->wmep_aifsn,
1199 		    wmep->wmep_logcwmin,
1200 		    wmep->wmep_logcwmax,
1201 		    wmep->wmep_txopLimit);
1202 		frm += 4;
1203 	}
1204 	wme->wme_wmeChanParams.cap_info = qosinfo_count;
1205 	return 1;
1206 #undef MS
1207 }
1208 
1209 /*
1210  * Process 11h Channel Switch Announcement (CSA) ie.  If this
1211  * is the first CSA then initiate the switch.  Otherwise we
1212  * track state and trigger completion and/or cancel of the switch.
1213  * XXX should be public for IBSS use
1214  */
1215 static void
1216 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1217 	const struct ieee80211_frame *wh)
1218 {
1219 	struct ieee80211com *ic = vap->iv_ic;
1220 	const struct ieee80211_csa_ie *csa =
1221 	    (const struct ieee80211_csa_ie *) frm;
1222 
1223 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1224 	    ("state %s", ieee80211_state_name[vap->iv_state]));
1225 
1226 	if (csa->csa_mode > 1) {
1227 		IEEE80211_DISCARD_IE(vap,
1228 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1229 		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1230 		return;
1231 	}
1232 	IEEE80211_LOCK(ic);
1233 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1234 		/*
1235 		 * Convert the channel number to a channel reference.  We
1236 		 * try first to preserve turbo attribute of the current
1237 		 * channel then fallback.  Note this will not work if the
1238 		 * CSA specifies a channel that requires a band switch (e.g.
1239 		 * 11a => 11g).  This is intentional as 11h is defined only
1240 		 * for 5GHz/11a and because the switch does not involve a
1241 		 * reassociation, protocol state (capabilities, negotated
1242 		 * rates, etc) may/will be wrong.
1243 		 */
1244 		struct ieee80211_channel *c =
1245 		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1246 			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1247 		if (c == NULL) {
1248 			c = ieee80211_find_channel_byieee(ic,
1249 			    csa->csa_newchan,
1250 			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1251 			if (c == NULL) {
1252 				IEEE80211_DISCARD_IE(vap,
1253 				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1254 				    wh, "CSA", "invalid channel %u",
1255 				    csa->csa_newchan);
1256 				goto done;
1257 			}
1258 		}
1259 #if IEEE80211_CSA_COUNT_MIN > 0
1260 		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1261 			/*
1262 			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1263 			 * reduce the risk of being redirected by a fabricated
1264 			 * CSA.  If a valid CSA is dropped we'll still get a
1265 			 * beacon miss when the AP leaves the channel so we'll
1266 			 * eventually follow to the new channel.
1267 			 *
1268 			 * NOTE: this violates the 11h spec that states that
1269 			 * count may be any value and if 0 then a switch
1270 			 * should happen asap.
1271 			 */
1272 			IEEE80211_DISCARD_IE(vap,
1273 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1274 			    wh, "CSA", "count %u too small, must be >= %u",
1275 			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1276 			goto done;
1277 		}
1278 #endif
1279 		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1280 	} else {
1281 		/*
1282 		 * Validate this ie against the initial CSA.  We require
1283 		 * mode and channel not change and the count must be
1284 		 * monotonically decreasing.  This may be pointless and
1285 		 * canceling the switch as a result may be too paranoid but
1286 		 * in the worst case if we drop out of CSA because of this
1287 		 * and the AP does move then we'll just end up taking a
1288 		 * beacon miss and scan to find the AP.
1289 		 *
1290 		 * XXX may want <= on count as we also process ProbeResp
1291 		 * frames and those may come in w/ the same count as the
1292 		 * previous beacon; but doing so leaves us open to a stuck
1293 		 * count until we add a dead-man timer
1294 		 */
1295 		if (!(csa->csa_count < ic->ic_csa_count &&
1296 		      csa->csa_mode == ic->ic_csa_mode &&
1297 		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1298 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1299 			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1300 			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1301 			    ic->ic_csa_newchan, ic->ic_csa_count,
1302 			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1303 			ieee80211_csa_cancelswitch(ic);
1304 		} else {
1305 			if (csa->csa_count <= 1)
1306 				ieee80211_csa_completeswitch(ic);
1307 			else
1308 				ic->ic_csa_count = csa->csa_count;
1309 		}
1310 	}
1311 done:
1312 	IEEE80211_UNLOCK(ic);
1313 }
1314 
1315 /*
1316  * Return non-zero if a background scan may be continued:
1317  * o bg scan is active
1318  * o no channel switch is pending
1319  * o there has not been any traffic recently
1320  * o no full-offload scan support (no need for explicitly continuing scan then)
1321  *
1322  * Note we do not check if there is an administrative enable;
1323  * this is only done to start the scan.  We assume that any
1324  * change in state will be accompanied by a request to cancel
1325  * active scans which will otherwise cause this test to fail.
1326  */
1327 static __inline int
1328 contbgscan(struct ieee80211vap *vap)
1329 {
1330 	struct ieee80211com *ic = vap->iv_ic;
1331 
1332 	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1333 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1334 	    !(vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) &&
1335 	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1336 	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1337 }
1338 
1339 /*
1340  * Return non-zero if a backgrond scan may be started:
1341  * o bg scanning is administratively enabled
1342  * o no channel switch is pending
1343  * o we are not boosted on a dynamic turbo channel
1344  * o there has not been a scan recently
1345  * o there has not been any traffic recently (don't check if full-offload scan)
1346  */
1347 static __inline int
1348 startbgscan(struct ieee80211vap *vap)
1349 {
1350 	struct ieee80211com *ic = vap->iv_ic;
1351 
1352 	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1353 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1354 #ifdef IEEE80211_SUPPORT_SUPERG
1355 	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1356 #endif
1357 	    ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1358 	    ((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) ||
1359 	     ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)));
1360 }
1361 
1362 #ifdef	notyet
1363 /*
1364  * Compare two quiet IEs and return if they are equivalent.
1365  *
1366  * The tbttcount isnt checked - that's not part of the configuration.
1367  */
1368 static int
1369 compare_quiet_ie(const struct ieee80211_quiet_ie *q1,
1370     const struct ieee80211_quiet_ie *q2)
1371 {
1372 
1373 	if (q1->period != q2->period)
1374 		return (0);
1375 	if (le16dec(&q1->duration) != le16dec(&q2->duration))
1376 		return (0);
1377 	if (le16dec(&q1->offset) != le16dec(&q2->offset))
1378 		return (0);
1379 	return (1);
1380 }
1381 #endif
1382 
1383 static void
1384 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1385     const struct ieee80211_rx_stats *rxs,
1386     int rssi, int nf)
1387 {
1388 #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1389 	struct ieee80211vap *vap = ni->ni_vap;
1390 	struct ieee80211com *ic = ni->ni_ic;
1391 	struct ieee80211_channel *rxchan = ic->ic_curchan;
1392 	struct ieee80211_frame *wh;
1393 	int ht_state_change = 0, do_ht = 0;
1394 	uint8_t *frm, *efrm;
1395 	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1396 	uint8_t *vhtcap, *vhtopmode;
1397 	uint8_t rate;
1398 	uint8_t qosinfo;
1399 
1400 	wh = mtod(m0, struct ieee80211_frame *);
1401 	frm = (uint8_t *)&wh[1];
1402 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1403 	switch (subtype) {
1404 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1405 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1406 		struct ieee80211_scanparams scan;
1407 		struct ieee80211_channel *c;
1408 		/*
1409 		 * We process beacon/probe response frames:
1410 		 *    o when scanning, or
1411 		 *    o station mode when associated (to collect state
1412 		 *      updates such as 802.11g slot time)
1413 		 * Frames otherwise received are discarded.
1414 		 */
1415 		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1416 			vap->iv_stats.is_rx_mgtdiscard++;
1417 			return;
1418 		}
1419 
1420 		/* Override RX channel as appropriate */
1421 		if (rxs != NULL) {
1422 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1423 			if (c != NULL)
1424 				rxchan = c;
1425 		}
1426 
1427 		/* XXX probe response in sta mode when !scanning? */
1428 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1429 			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1430 				vap->iv_stats.is_beacon_bad++;
1431 			return;
1432 		}
1433 
1434 		/*
1435 		 * Count frame now that we know it's to be processed.
1436 		 */
1437 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1438 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1439 			IEEE80211_NODE_STAT(ni, rx_beacons);
1440 		} else
1441 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1442 		/*
1443 		 * When operating in station mode, check for state updates.
1444 		 * Be careful to ignore beacons received while doing a
1445 		 * background scan.  We consider only 11g/WMM stuff right now.
1446 		 */
1447 		if (ni->ni_associd != 0 &&
1448 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1449 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1450 			/* record tsf of last beacon */
1451 			memcpy(ni->ni_tstamp.data, scan.tstamp,
1452 				sizeof(ni->ni_tstamp));
1453 			/* count beacon frame for s/w bmiss handling */
1454 			vap->iv_swbmiss_count++;
1455 			vap->iv_bmiss_count = 0;
1456 			if (ni->ni_erp != scan.erp) {
1457 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1458 				    wh->i_addr2,
1459 				    "erp change: was 0x%x, now 0x%x",
1460 				    ni->ni_erp, scan.erp);
1461 				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1462 				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1463 					ic->ic_flags |= IEEE80211_F_USEPROT;
1464 				else
1465 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1466 				ni->ni_erp = scan.erp;
1467 				/* XXX statistic */
1468 				/* XXX driver notification */
1469 			}
1470 			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1471 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1472 				    wh->i_addr2,
1473 				    "capabilities change: was 0x%x, now 0x%x",
1474 				    ni->ni_capinfo, scan.capinfo);
1475 				/*
1476 				 * NB: we assume short preamble doesn't
1477 				 *     change dynamically
1478 				 */
1479 				ieee80211_vap_set_shortslottime(vap,
1480 					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1481 					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1482 				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1483 					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1484 				/* XXX statistic */
1485 			}
1486 			if (scan.wme != NULL &&
1487 			    (ni->ni_flags & IEEE80211_NODE_QOS)) {
1488 				int _retval;
1489 				if ((_retval = ieee80211_parse_wmeparams(vap,
1490 				    scan.wme, wh, &qosinfo)) >= 0) {
1491 					if (qosinfo & WME_CAPINFO_UAPSD_EN)
1492 						ni->ni_flags |=
1493 						    IEEE80211_NODE_UAPSD;
1494 					if (_retval > 0)
1495 						ieee80211_wme_updateparams(vap);
1496 				}
1497 			} else
1498 				ni->ni_flags &= ~IEEE80211_NODE_UAPSD;
1499 #ifdef IEEE80211_SUPPORT_SUPERG
1500 			if (scan.ath != NULL)
1501 				ieee80211_parse_athparams(ni, scan.ath, wh);
1502 #endif
1503 			if (scan.htcap != NULL && scan.htinfo != NULL &&
1504 			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1505 				/* XXX state changes? */
1506 				ieee80211_ht_updateparams(ni,
1507 				    scan.htcap, scan.htinfo);
1508 				do_ht = 1;
1509 			}
1510 			if (scan.vhtcap != NULL && scan.vhtopmode != NULL &&
1511 			    (vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1512 				/* XXX state changes? */
1513 				ieee80211_vht_updateparams(ni,
1514 				    scan.vhtcap, scan.vhtopmode);
1515 				do_ht = 1;
1516 			}
1517 			if (do_ht) {
1518 				if (ieee80211_ht_updateparams_final(ni,
1519 				    scan.htcap, scan.htinfo))
1520 					ht_state_change = 1;
1521 			}
1522 
1523 			/*
1524 			 * If we have a quiet time IE then report it up to
1525 			 * the driver.
1526 			 *
1527 			 * Otherwise, inform the driver that the quiet time
1528 			 * IE has disappeared - only do that once rather than
1529 			 * spamming it each time.
1530 			 */
1531 			if (scan.quiet) {
1532 				ic->ic_set_quiet(ni, scan.quiet);
1533 				ni->ni_quiet_ie_set = 1;
1534 				memcpy(&ni->ni_quiet_ie, scan.quiet,
1535 				    sizeof(struct ieee80211_quiet_ie));
1536 			} else {
1537 				if (ni->ni_quiet_ie_set == 1)
1538 					ic->ic_set_quiet(ni, NULL);
1539 				ni->ni_quiet_ie_set = 0;
1540 				bzero(&ni->ni_quiet_ie,
1541 				    sizeof(struct ieee80211_quiet_ie));
1542 			}
1543 
1544 			if (scan.tim != NULL) {
1545 				struct ieee80211_tim_ie *tim =
1546 				    (struct ieee80211_tim_ie *) scan.tim;
1547 				/*
1548 				 * XXX Check/debug this code; see if it's about
1549 				 * the right time to force the VAP awake if we
1550 				 * receive a frame destined for us?
1551 				 */
1552 				int aid = IEEE80211_AID(ni->ni_associd);
1553 				int ix = aid / NBBY;
1554 				int min = tim->tim_bitctl &~ 1;
1555 				int max = tim->tim_len + min - 4;
1556 				int tim_ucast = 0, tim_mcast = 0;
1557 
1558 				/*
1559 				 * Only do this for unicast traffic in the TIM
1560 				 * The multicast traffic notification for
1561 				 * the scan notification stuff should occur
1562 				 * differently.
1563 				 */
1564 				if (min <= ix && ix <= max &&
1565 				     isset(tim->tim_bitmap - min, aid)) {
1566 					tim_ucast = 1;
1567 				}
1568 
1569 				/*
1570 				 * Do a separate notification
1571 				 * for the multicast bit being set.
1572 				 */
1573 				if (tim->tim_bitctl & 1) {
1574 					tim_mcast = 1;
1575 				}
1576 
1577 				/*
1578 				 * If the TIM indicates there's traffic for
1579 				 * us then get us out of STA mode powersave.
1580 				 */
1581 				if (tim_ucast == 1) {
1582 
1583 					/*
1584 					 * Wake us out of SLEEP state if we're
1585 					 * in it; and if we're doing bgscan
1586 					 * then wake us out of STA powersave.
1587 					 */
1588 					ieee80211_sta_tim_notify(vap, 1);
1589 
1590 					/*
1591 					 * This is preventing us from
1592 					 * continuing a bgscan; because it
1593 					 * tricks the contbgscan()
1594 					 * routine to think there's always
1595 					 * traffic for us.
1596 					 *
1597 					 * I think we need both an RX and
1598 					 * TX ic_lastdata field.
1599 					 */
1600 					ic->ic_lastdata = ticks;
1601 				}
1602 
1603 				ni->ni_dtim_count = tim->tim_count;
1604 				ni->ni_dtim_period = tim->tim_period;
1605 			}
1606 			if (scan.csa != NULL &&
1607 			    (vap->iv_flags & IEEE80211_F_DOTH))
1608 				ieee80211_parse_csaparams(vap, scan.csa, wh);
1609 			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1610 				/*
1611 				 * No CSA ie or 11h disabled, but a channel
1612 				 * switch is pending; drop out so we aren't
1613 				 * stuck in CSA state.  If the AP really is
1614 				 * moving we'll get a beacon miss and scan.
1615 				 */
1616 				IEEE80211_LOCK(ic);
1617 				ieee80211_csa_cancelswitch(ic);
1618 				IEEE80211_UNLOCK(ic);
1619 			}
1620 			/*
1621 			 * If scanning, pass the info to the scan module.
1622 			 * Otherwise, check if it's the right time to do
1623 			 * a background scan.  Background scanning must
1624 			 * be enabled and we must not be operating in the
1625 			 * turbo phase of dynamic turbo mode.  Then,
1626 			 * it's been a while since the last background
1627 			 * scan and if no data frames have come through
1628 			 * recently, kick off a scan.  Note that this
1629 			 * is the mechanism by which a background scan
1630 			 * is started _and_ continued each time we
1631 			 * return on-channel to receive a beacon from
1632 			 * our ap.
1633 			 */
1634 			if (ic->ic_flags & IEEE80211_F_SCAN) {
1635 				ieee80211_add_scan(vap, rxchan,
1636 				    &scan, wh, subtype, rssi, nf);
1637 			} else if (contbgscan(vap)) {
1638 				ieee80211_bg_scan(vap, 0);
1639 			} else if (startbgscan(vap)) {
1640 				vap->iv_stats.is_scan_bg++;
1641 #if 0
1642 				/* wakeup if we are sleeing */
1643 				ieee80211_set_pwrsave(vap, 0);
1644 #endif
1645 				ieee80211_bg_scan(vap, 0);
1646 			}
1647 
1648 			/*
1649 			 * Put the station to sleep if we haven't seen
1650 			 * traffic in a while.
1651 			 */
1652 			IEEE80211_LOCK(ic);
1653 			ieee80211_sta_ps_timer_check(vap);
1654 			IEEE80211_UNLOCK(ic);
1655 
1656 			/*
1657 			 * If we've had a channel width change (eg HT20<->HT40)
1658 			 * then schedule a delayed driver notification.
1659 			 */
1660 			if (ht_state_change)
1661 				ieee80211_update_chw(ic);
1662 			return;
1663 		}
1664 		/*
1665 		 * If scanning, just pass information to the scan module.
1666 		 */
1667 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1668 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1669 				/*
1670 				 * Actively scanning a channel marked passive;
1671 				 * send a probe request now that we know there
1672 				 * is 802.11 traffic present.
1673 				 *
1674 				 * XXX check if the beacon we recv'd gives
1675 				 * us what we need and suppress the probe req
1676 				 */
1677 				ieee80211_probe_curchan(vap, 1);
1678 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1679 			}
1680 			ieee80211_add_scan(vap, rxchan, &scan, wh,
1681 			    subtype, rssi, nf);
1682 			return;
1683 		}
1684 		break;
1685 	}
1686 
1687 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1688 		uint16_t algo, seq, status;
1689 		/*
1690 		 * auth frame format
1691 		 *	[2] algorithm
1692 		 *	[2] sequence
1693 		 *	[2] status
1694 		 *	[tlv*] challenge
1695 		 */
1696 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1697 		algo   = le16toh(*(uint16_t *)frm);
1698 		seq    = le16toh(*(uint16_t *)(frm + 2));
1699 		status = le16toh(*(uint16_t *)(frm + 4));
1700 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1701 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1702 
1703 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1704 			IEEE80211_DISCARD(vap,
1705 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1706 			    wh, "auth", "%s", "TKIP countermeasures enabled");
1707 			vap->iv_stats.is_rx_auth_countermeasures++;
1708 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1709 				ieee80211_send_error(ni, wh->i_addr2,
1710 					IEEE80211_FC0_SUBTYPE_AUTH,
1711 					IEEE80211_REASON_MIC_FAILURE);
1712 			}
1713 			return;
1714 		}
1715 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1716 			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1717 			    seq, status);
1718 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1719 			sta_auth_open(ni, wh, rssi, nf, seq, status);
1720 		else {
1721 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1722 			    wh, "auth", "unsupported alg %d", algo);
1723 			vap->iv_stats.is_rx_auth_unsupported++;
1724 			return;
1725 		}
1726 		break;
1727 	}
1728 
1729 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1730 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1731 		uint16_t capinfo, associd;
1732 		uint16_t status;
1733 
1734 		if (vap->iv_state != IEEE80211_S_ASSOC) {
1735 			vap->iv_stats.is_rx_mgtdiscard++;
1736 			return;
1737 		}
1738 
1739 		/*
1740 		 * asresp frame format
1741 		 *	[2] capability information
1742 		 *	[2] status
1743 		 *	[2] association ID
1744 		 *	[tlv] supported rates
1745 		 *	[tlv] extended supported rates
1746 		 *	[tlv] WME
1747 		 *	[tlv] HT capabilities
1748 		 *	[tlv] HT info
1749 		 */
1750 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1751 		ni = vap->iv_bss;
1752 		capinfo = le16toh(*(uint16_t *)frm);
1753 		frm += 2;
1754 		status = le16toh(*(uint16_t *)frm);
1755 		frm += 2;
1756 		if (status != 0) {
1757 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1758 			    wh->i_addr2, "%sassoc failed (reason %d)",
1759 			    ISREASSOC(subtype) ?  "re" : "", status);
1760 			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1761 			return;
1762 		}
1763 		associd = le16toh(*(uint16_t *)frm);
1764 		frm += 2;
1765 
1766 		rates = xrates = wme = htcap = htinfo = NULL;
1767 		vhtcap = vhtopmode = NULL;
1768 		while (efrm - frm > 1) {
1769 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1770 			switch (*frm) {
1771 			case IEEE80211_ELEMID_RATES:
1772 				rates = frm;
1773 				break;
1774 			case IEEE80211_ELEMID_XRATES:
1775 				xrates = frm;
1776 				break;
1777 			case IEEE80211_ELEMID_HTCAP:
1778 				htcap = frm;
1779 				break;
1780 			case IEEE80211_ELEMID_HTINFO:
1781 				htinfo = frm;
1782 				break;
1783 			case IEEE80211_ELEMID_VENDOR:
1784 				if (iswmeoui(frm))
1785 					wme = frm;
1786 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1787 					/*
1788 					 * Accept pre-draft HT ie's if the
1789 					 * standard ones have not been seen.
1790 					 */
1791 					if (ishtcapoui(frm)) {
1792 						if (htcap == NULL)
1793 							htcap = frm;
1794 					} else if (ishtinfooui(frm)) {
1795 						if (htinfo == NULL)
1796 							htinfo = frm;
1797 					}
1798 				}
1799 				/* XXX Atheros OUI support */
1800 				break;
1801 			case IEEE80211_ELEMID_VHT_CAP:
1802 				vhtcap = frm;
1803 				break;
1804 			case IEEE80211_ELEMID_VHT_OPMODE:
1805 				vhtopmode = frm;
1806 				break;
1807 			}
1808 			frm += frm[1] + 2;
1809 		}
1810 
1811 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1812 		if (xrates != NULL)
1813 			IEEE80211_VERIFY_ELEMENT(xrates,
1814 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1815 		rate = ieee80211_setup_rates(ni, rates, xrates,
1816 				IEEE80211_F_JOIN |
1817 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1818 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1819 		if (rate & IEEE80211_RATE_BASIC) {
1820 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1821 			    wh->i_addr2,
1822 			    "%sassoc failed (rate set mismatch)",
1823 			    ISREASSOC(subtype) ?  "re" : "");
1824 			vap->iv_stats.is_rx_assoc_norate++;
1825 			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1826 			    IEEE80211_SCAN_FAIL_STATUS);
1827 			return;
1828 		}
1829 
1830 		ni->ni_capinfo = capinfo;
1831 		ni->ni_associd = associd;
1832 		if (ni->ni_jointime == 0)
1833 			ni->ni_jointime = time_uptime;
1834 		if (wme != NULL &&
1835 		    ieee80211_parse_wmeparams(vap, wme, wh, &qosinfo) >= 0) {
1836 			ni->ni_flags |= IEEE80211_NODE_QOS;
1837 			ieee80211_wme_updateparams(vap);
1838 		} else
1839 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1840 		/*
1841 		 * Setup HT state according to the negotiation.
1842 		 *
1843 		 * NB: shouldn't need to check if HT use is enabled but some
1844 		 *     ap's send back HT ie's even when we don't indicate we
1845 		 *     are HT capable in our AssocReq.
1846 		 */
1847 		if (htcap != NULL && htinfo != NULL &&
1848 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1849 			ieee80211_ht_node_init(ni);
1850 			ieee80211_ht_updateparams(ni, htcap, htinfo);
1851 
1852 			if ((vhtcap != NULL) && (vhtopmode != NULL) &
1853 			    (vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1854 				/*
1855 				 * Log if we get a VHT assoc/reassoc response.
1856 				 * We aren't ready for 2GHz VHT support.
1857 				 */
1858 				if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1859 					printf("%s: peer %6D: VHT on 2GHz, ignoring\n",
1860 					    __func__,
1861 					    ni->ni_macaddr,
1862 					    ":");
1863 				} else {
1864 					ieee80211_vht_node_init(ni);
1865 					ieee80211_vht_updateparams(ni, vhtcap, vhtopmode);
1866 					ieee80211_setup_vht_rates(ni, vhtcap, vhtopmode);
1867 				}
1868 			}
1869 
1870 			ieee80211_ht_updateparams_final(ni, htcap, htinfo);
1871 			ieee80211_setup_htrates(ni, htcap,
1872 			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1873 			ieee80211_setup_basic_htrates(ni, htinfo);
1874 
1875 			ieee80211_node_setuptxparms(ni);
1876 			ieee80211_ratectl_node_init(ni);
1877 		}
1878 
1879 		/*
1880 		 * Always initialise FF/superg state; we can use this
1881 		 * for doing A-MSDU encapsulation as well.
1882 		 */
1883 #ifdef	IEEE80211_SUPPORT_SUPERG
1884 		ieee80211_ff_node_init(ni);
1885 #endif
1886 
1887 		/*
1888 		 * Configure state now that we are associated.
1889 		 *
1890 		 * XXX may need different/additional driver callbacks?
1891 		 */
1892 		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1893 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1894 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1895 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1896 		} else {
1897 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1898 			ic->ic_flags |= IEEE80211_F_USEBARKER;
1899 		}
1900 		ieee80211_vap_set_shortslottime(vap,
1901 			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1902 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1903 		/*
1904 		 * Honor ERP protection.
1905 		 *
1906 		 * NB: ni_erp should zero for non-11g operation.
1907 		 */
1908 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1909 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1910 			ic->ic_flags |= IEEE80211_F_USEPROT;
1911 		else
1912 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1913 		IEEE80211_NOTE_MAC(vap,
1914 		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1915 		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
1916 		    ISREASSOC(subtype) ? "re" : "",
1917 		    IEEE80211_NODE_AID(ni),
1918 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1919 		    vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1920 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1921 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1922 		    ni->ni_flags & IEEE80211_NODE_HT ?
1923 			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1924 		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1925 		    ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
1926 		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1927 			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1928 		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1929 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1930 			", fast-frames" : "",
1931 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1932 			", turbo" : ""
1933 		);
1934 		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1935 		break;
1936 	}
1937 
1938 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1939 		uint16_t reason;
1940 
1941 		if (vap->iv_state == IEEE80211_S_SCAN) {
1942 			vap->iv_stats.is_rx_mgtdiscard++;
1943 			return;
1944 		}
1945 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1946 			/* NB: can happen when in promiscuous mode */
1947 			vap->iv_stats.is_rx_mgtdiscard++;
1948 			break;
1949 		}
1950 
1951 		/*
1952 		 * deauth frame format
1953 		 *	[2] reason
1954 		 */
1955 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1956 		reason = le16toh(*(uint16_t *)frm);
1957 
1958 		vap->iv_stats.is_rx_deauth++;
1959 		vap->iv_stats.is_rx_deauth_code = reason;
1960 		IEEE80211_NODE_STAT(ni, rx_deauth);
1961 
1962 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1963 		    "recv deauthenticate (reason: %d (%s))", reason,
1964 		    ieee80211_reason_to_string(reason));
1965 		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1966 		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1967 		break;
1968 	}
1969 
1970 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1971 		uint16_t reason;
1972 
1973 		if (vap->iv_state != IEEE80211_S_RUN &&
1974 		    vap->iv_state != IEEE80211_S_ASSOC &&
1975 		    vap->iv_state != IEEE80211_S_AUTH) {
1976 			vap->iv_stats.is_rx_mgtdiscard++;
1977 			return;
1978 		}
1979 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1980 			/* NB: can happen when in promiscuous mode */
1981 			vap->iv_stats.is_rx_mgtdiscard++;
1982 			break;
1983 		}
1984 
1985 		/*
1986 		 * disassoc frame format
1987 		 *	[2] reason
1988 		 */
1989 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1990 		reason = le16toh(*(uint16_t *)frm);
1991 
1992 		vap->iv_stats.is_rx_disassoc++;
1993 		vap->iv_stats.is_rx_disassoc_code = reason;
1994 		IEEE80211_NODE_STAT(ni, rx_disassoc);
1995 
1996 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1997 		    "recv disassociate (reason: %d (%s))", reason,
1998 		    ieee80211_reason_to_string(reason));
1999 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
2000 		break;
2001 	}
2002 
2003 	case IEEE80211_FC0_SUBTYPE_ACTION:
2004 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2005 		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2006 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2007 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2008 			    wh, NULL, "%s", "not for us");
2009 			vap->iv_stats.is_rx_mgtdiscard++;
2010 		} else if (vap->iv_state != IEEE80211_S_RUN) {
2011 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2012 			    wh, NULL, "wrong state %s",
2013 			    ieee80211_state_name[vap->iv_state]);
2014 			vap->iv_stats.is_rx_mgtdiscard++;
2015 		} else {
2016 			if (ieee80211_parse_action(ni, m0) == 0)
2017 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2018 		}
2019 		break;
2020 
2021 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2022 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2023 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2024 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2025 	case IEEE80211_FC0_SUBTYPE_ATIM:
2026 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2027 		    wh, NULL, "%s", "not handled");
2028 		vap->iv_stats.is_rx_mgtdiscard++;
2029 		break;
2030 
2031 	default:
2032 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2033 		    wh, "mgt", "subtype 0x%x not handled", subtype);
2034 		vap->iv_stats.is_rx_badsubtype++;
2035 		break;
2036 	}
2037 #undef ISREASSOC
2038 }
2039 
2040 static void
2041 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2042 {
2043 	switch (subtype) {
2044 	case IEEE80211_FC0_SUBTYPE_BAR:
2045 		ieee80211_recv_bar(ni, m);
2046 		break;
2047 	}
2048 }
2049