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