xref: /freebsd/sys/net80211/ieee80211_sta.c (revision 33f12199250a09b573f7a518b523fdac3f120b8f)
1 /*-
2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
30 
31 /*
32  * IEEE 802.11 Station mode support.
33  */
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49 
50 #include <net/if.h>
51 #include <net/if_media.h>
52 #include <net/if_llc.h>
53 #include <net/ethernet.h>
54 
55 #include <net/bpf.h>
56 
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_sta.h>
59 #include <net80211/ieee80211_input.h>
60 
61 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
62 
63 static	void sta_vattach(struct ieee80211vap *);
64 static	void sta_beacon_miss(struct ieee80211vap *);
65 static	int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
66 static	int sta_input(struct ieee80211_node *, struct mbuf *,
67 	    int rssi, int noise, uint32_t rstamp);
68 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
69 	    int subtype, int rssi, int noise, uint32_t rstamp);
70 
71 void
72 ieee80211_sta_attach(struct ieee80211com *ic)
73 {
74 	ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
75 }
76 
77 void
78 ieee80211_sta_detach(struct ieee80211com *ic)
79 {
80 }
81 
82 static void
83 sta_vdetach(struct ieee80211vap *vap)
84 {
85 }
86 
87 static void
88 sta_vattach(struct ieee80211vap *vap)
89 {
90 	vap->iv_newstate = sta_newstate;
91 	vap->iv_input = sta_input;
92 	vap->iv_recv_mgmt = sta_recv_mgmt;
93 	vap->iv_opdetach = sta_vdetach;
94 	vap->iv_bmiss = sta_beacon_miss;
95 }
96 
97 /*
98  * Handle a beacon miss event.  The common code filters out
99  * spurious events that can happen when scanning and/or before
100  * reaching RUN state.
101  */
102 static void
103 sta_beacon_miss(struct ieee80211vap *vap)
104 {
105 	struct ieee80211com *ic = vap->iv_ic;
106 
107 	KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
108 	KASSERT(vap->iv_state == IEEE80211_S_RUN,
109 	    ("wrong state %d", vap->iv_state));
110 
111 	IEEE80211_DPRINTF(vap,
112 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
113 		"beacon miss, mode %u state %s\n",
114 		vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
115 
116 	if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
117 		/*
118 		 * Send a directed probe req before falling back to a
119 		 * scan; if we receive a response ic_bmiss_count will
120 		 * be reset.  Some cards mistakenly report beacon miss
121 		 * so this avoids the expensive scan if the ap is
122 		 * still there.
123 		 */
124 		ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
125 			vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
126 			vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
127 		return;
128 	}
129 	vap->iv_bmiss_count = 0;
130 	vap->iv_stats.is_beacon_miss++;
131 	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
132 		/*
133 		 * If we receive a beacon miss interrupt when using
134 		 * dynamic turbo, attempt to switch modes before
135 		 * reassociating.
136 		 */
137 		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
138 			ieee80211_dturbo_switch(vap,
139 			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
140 		/*
141 		 * Try to reassociate before scanning for a new ap.
142 		 */
143 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
144 	} else {
145 		/*
146 		 * Somebody else is controlling state changes (e.g.
147 		 * a user-mode app) don't do anything that would
148 		 * confuse them; just drop into scan mode so they'll
149 		 * notified of the state change and given control.
150 		 */
151 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
152 	}
153 }
154 
155 /*
156  * Handle deauth with reason.  We retry only for
157  * the cases where we might succeed.  Otherwise
158  * we downgrade the ap and scan.
159  */
160 static void
161 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
162 {
163 	switch (reason) {
164 	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
165 	case IEEE80211_STATUS_TIMEOUT:
166 	case IEEE80211_REASON_ASSOC_EXPIRE:
167 	case IEEE80211_REASON_NOT_AUTHED:
168 	case IEEE80211_REASON_NOT_ASSOCED:
169 	case IEEE80211_REASON_ASSOC_LEAVE:
170 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
171 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
172 		break;
173 	default:
174 		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
175 		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
176 			ieee80211_check_scan_current(vap);
177 		break;
178 	}
179 }
180 
181 /*
182  * IEEE80211_M_STA vap state machine handler.
183  * This routine handles the main states in the 802.11 protocol.
184  */
185 static int
186 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
187 {
188 	struct ieee80211com *ic = vap->iv_ic;
189 	struct ieee80211_node *ni;
190 	enum ieee80211_state ostate;
191 
192 	IEEE80211_LOCK_ASSERT(ic);
193 
194 	ostate = vap->iv_state;
195 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
196 	    __func__, ieee80211_state_name[ostate],
197 	    ieee80211_state_name[nstate], arg);
198 	vap->iv_state = nstate;			/* state transition */
199 	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
200 	if (ostate != IEEE80211_S_SCAN)
201 		ieee80211_cancel_scan(vap);	/* background scan */
202 	ni = vap->iv_bss;			/* NB: no reference held */
203 	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
204 		callout_stop(&vap->iv_swbmiss);
205 	switch (nstate) {
206 	case IEEE80211_S_INIT:
207 		switch (ostate) {
208 		case IEEE80211_S_SLEEP:
209 			/* XXX wakeup */
210 		case IEEE80211_S_RUN:
211 			IEEE80211_SEND_MGMT(ni,
212 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
213 			    IEEE80211_REASON_ASSOC_LEAVE);
214 			ieee80211_sta_leave(ni);
215 			break;
216 		case IEEE80211_S_ASSOC:
217 			IEEE80211_SEND_MGMT(ni,
218 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
219 			    IEEE80211_REASON_AUTH_LEAVE);
220 			break;
221 		case IEEE80211_S_SCAN:
222 			ieee80211_cancel_scan(vap);
223 			break;
224 		default:
225 			goto invalid;
226 		}
227 		if (ostate != IEEE80211_S_INIT) {
228 			/* NB: optimize INIT -> INIT case */
229 			ieee80211_reset_bss(vap);
230 		}
231 		if (vap->iv_auth->ia_detach != NULL)
232 			vap->iv_auth->ia_detach(vap);
233 		break;
234 	case IEEE80211_S_SCAN:
235 		switch (ostate) {
236 		case IEEE80211_S_INIT:
237 			/*
238 			 * Initiate a scan.  We can come here as a result
239 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
240 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
241 			 * and the scan request parameters will be present
242 			 * in iv_scanreq.  Otherwise we do the default.
243 			 */
244 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
245 				ieee80211_check_scan(vap,
246 				    vap->iv_scanreq_flags,
247 				    vap->iv_scanreq_duration,
248 				    vap->iv_scanreq_mindwell,
249 				    vap->iv_scanreq_maxdwell,
250 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
251 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
252 			} else
253 				ieee80211_check_scan_current(vap);
254 			break;
255 		case IEEE80211_S_SCAN:
256 		case IEEE80211_S_AUTH:
257 		case IEEE80211_S_ASSOC:
258 			/*
259 			 * These can happen either because of a timeout
260 			 * on an assoc/auth response or because of a
261 			 * change in state that requires a reset.  For
262 			 * the former we're called with a non-zero arg
263 			 * that is the cause for the failure; pass this
264 			 * to the scan code so it can update state.
265 			 * Otherwise trigger a new scan unless we're in
266 			 * manual roaming mode in which case an application
267 			 * must issue an explicit scan request.
268 			 */
269 			if (arg != 0)
270 				ieee80211_scan_assoc_fail(vap,
271 					vap->iv_bss->ni_macaddr, arg);
272 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
273 				ieee80211_check_scan_current(vap);
274 			break;
275 		case IEEE80211_S_RUN:		/* beacon miss */
276 			/*
277 			 * Beacon miss.  Notify user space and if not
278 			 * under control of a user application (roaming
279 			 * manual) kick off a scan to re-connect.
280 			 */
281 			ieee80211_sta_leave(ni);
282 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
283 				ieee80211_check_scan_current(vap);
284 			break;
285 		default:
286 			goto invalid;
287 		}
288 		break;
289 	case IEEE80211_S_AUTH:
290 		switch (ostate) {
291 		case IEEE80211_S_INIT:
292 		case IEEE80211_S_SCAN:
293 			IEEE80211_SEND_MGMT(ni,
294 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
295 			break;
296 		case IEEE80211_S_AUTH:
297 		case IEEE80211_S_ASSOC:
298 			switch (arg & 0xff) {
299 			case IEEE80211_FC0_SUBTYPE_AUTH:
300 				/* ??? */
301 				IEEE80211_SEND_MGMT(ni,
302 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
303 				break;
304 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
305 				sta_authretry(vap, ni, arg>>8);
306 				break;
307 			}
308 			break;
309 		case IEEE80211_S_RUN:
310 			switch (arg & 0xff) {
311 			case IEEE80211_FC0_SUBTYPE_AUTH:
312 				IEEE80211_SEND_MGMT(ni,
313 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
314 				vap->iv_state = ostate;	/* stay RUN */
315 				break;
316 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
317 				ieee80211_sta_leave(ni);
318 				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
319 					/* try to reauth */
320 					IEEE80211_SEND_MGMT(ni,
321 					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
322 				}
323 				break;
324 			}
325 			break;
326 		default:
327 			goto invalid;
328 		}
329 		break;
330 	case IEEE80211_S_ASSOC:
331 		switch (ostate) {
332 		case IEEE80211_S_AUTH:
333 		case IEEE80211_S_ASSOC:
334 			IEEE80211_SEND_MGMT(ni,
335 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
336 			break;
337 		case IEEE80211_S_SLEEP:		/* cannot happen */
338 		case IEEE80211_S_RUN:
339 			ieee80211_sta_leave(ni);
340 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
341 				IEEE80211_SEND_MGMT(ni, arg ?
342 				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
343 				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
344 			}
345 			break;
346 		default:
347 			goto invalid;
348 		}
349 		break;
350 	case IEEE80211_S_RUN:
351 		if (vap->iv_flags & IEEE80211_F_WPA) {
352 			/* XXX validate prerequisites */
353 		}
354 		switch (ostate) {
355 		case IEEE80211_S_RUN:
356 			break;
357 		case IEEE80211_S_AUTH:		/* when join is done in fw */
358 		case IEEE80211_S_ASSOC:
359 #ifdef IEEE80211_DEBUG
360 			if (ieee80211_msg_debug(vap)) {
361 				ieee80211_note(vap, "%s with %s ssid ",
362 				    (vap->iv_opmode == IEEE80211_M_STA ?
363 				    "associated" : "synchronized"),
364 				    ether_sprintf(ni->ni_bssid));
365 				ieee80211_print_essid(vap->iv_bss->ni_essid,
366 				    ni->ni_esslen);
367 				/* XXX MCS/HT */
368 				printf(" channel %d start %uMb\n",
369 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
370 				    IEEE80211_RATE2MBS(ni->ni_txrate));
371 			}
372 #endif
373 			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
374 			ieee80211_notify_node_join(ni,
375 			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
376 			break;
377 		case IEEE80211_S_SLEEP:
378 			ieee80211_sta_pwrsave(vap, 0);
379 			break;
380 		default:
381 			goto invalid;
382 		}
383 		ieee80211_sync_curchan(ic);
384 		if (ostate != IEEE80211_S_RUN &&
385 		    (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
386 			/*
387 			 * Start s/w beacon miss timer for devices w/o
388 			 * hardware support.  We fudge a bit here since
389 			 * we're doing this in software.
390 			 */
391 			vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
392 				2 * vap->iv_bmissthreshold * ni->ni_intval);
393 			vap->iv_swbmiss_count = 0;
394 			callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
395 				ieee80211_swbmiss, vap);
396 		}
397 		/*
398 		 * When 802.1x is not in use mark the port authorized
399 		 * at this point so traffic can flow.
400 		 */
401 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
402 			ieee80211_node_authorize(ni);
403 		break;
404 	case IEEE80211_S_SLEEP:
405 		ieee80211_sta_pwrsave(vap, 0);
406 		break;
407 	default:
408 	invalid:
409 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
410 		    "%s: invalid state transition %s -> %s\n", __func__,
411 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
412 		break;
413 	}
414 	return 0;
415 }
416 
417 /*
418  * Return non-zero if the frame is an echo of a multicast
419  * frame sent by ourself.  The dir is known to be DSTODS.
420  */
421 static __inline int
422 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
423 {
424 #define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
425 #define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
426 	const uint8_t *sa;
427 
428 	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
429 
430 	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
431 		return 0;
432 	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
433 	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
434 #undef WH4
435 #undef QWH4
436 }
437 
438 /*
439  * Return non-zero if the frame is an echo of a multicast
440  * frame sent by ourself.  The dir is known to be FROMDS.
441  */
442 static __inline int
443 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
444 {
445 	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
446 
447 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
448 		return 0;
449 	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
450 }
451 
452 /*
453  * Decide if a received management frame should be
454  * printed when debugging is enabled.  This filters some
455  * of the less interesting frames that come frequently
456  * (e.g. beacons).
457  */
458 static __inline int
459 doprint(struct ieee80211vap *vap, int subtype)
460 {
461 	switch (subtype) {
462 	case IEEE80211_FC0_SUBTYPE_BEACON:
463 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
464 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
465 		return 0;
466 	}
467 	return 1;
468 }
469 
470 /*
471  * Process a received frame.  The node associated with the sender
472  * should be supplied.  If nothing was found in the node table then
473  * the caller is assumed to supply a reference to iv_bss instead.
474  * The RSSI and a timestamp are also supplied.  The RSSI data is used
475  * during AP scanning to select a AP to associate with; it can have
476  * any units so long as values have consistent units and higher values
477  * mean ``better signal''.  The receive timestamp is currently not used
478  * by the 802.11 layer.
479  */
480 static int
481 sta_input(struct ieee80211_node *ni, struct mbuf *m,
482 	int rssi, int noise, uint32_t rstamp)
483 {
484 #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
485 #define	HAS_SEQ(type)	((type & 0x4) == 0)
486 	struct ieee80211vap *vap = ni->ni_vap;
487 	struct ieee80211com *ic = ni->ni_ic;
488 	struct ifnet *ifp = vap->iv_ifp;
489 	struct ieee80211_frame *wh;
490 	struct ieee80211_key *key;
491 	struct ether_header *eh;
492 	int hdrspace, need_tap;
493 	uint8_t dir, type, subtype, qos;
494 	uint8_t *bssid;
495 	uint16_t rxseq;
496 
497 	if (m->m_flags & M_AMPDU) {
498 		/*
499 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
500 		 * w/ M_AMPDU marked have already passed through here
501 		 * but were received out of order and been held on the
502 		 * reorder queue.  When resubmitted they are marked
503 		 * with the M_AMPDU flag and we can bypass most of the
504 		 * normal processing.
505 		 */
506 		wh = mtod(m, struct ieee80211_frame *);
507 		type = IEEE80211_FC0_TYPE_DATA;
508 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
509 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
510 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
511 		goto resubmit_ampdu;
512 	}
513 
514 	KASSERT(ni != NULL, ("null node"));
515 	ni->ni_inact = ni->ni_inact_reload;
516 
517 	need_tap = 1;			/* mbuf need to be tapped. */
518 	type = -1;			/* undefined */
519 
520 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
521 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
522 		    ni->ni_macaddr, NULL,
523 		    "too short (1): len %u", m->m_pkthdr.len);
524 		vap->iv_stats.is_rx_tooshort++;
525 		goto out;
526 	}
527 	/*
528 	 * Bit of a cheat here, we use a pointer for a 3-address
529 	 * frame format but don't reference fields past outside
530 	 * ieee80211_frame_min w/o first validating the data is
531 	 * present.
532 	 */
533 	wh = mtod(m, struct ieee80211_frame *);
534 
535 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
536 	    IEEE80211_FC0_VERSION_0) {
537 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
538 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
539 		vap->iv_stats.is_rx_badversion++;
540 		goto err;
541 	}
542 
543 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
544 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
545 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
546 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
547 		bssid = wh->i_addr2;
548 		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
549 			/* not interested in */
550 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
551 			    bssid, NULL, "%s", "not to bss");
552 			vap->iv_stats.is_rx_wrongbss++;
553 			goto out;
554 		}
555 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
556 		ni->ni_noise = noise;
557 		ni->ni_rstamp = rstamp;
558 		if (HAS_SEQ(type)) {
559 			uint8_t tid = ieee80211_gettid(wh);
560 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
561 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
562 				ic->ic_wme.wme_hipri_traffic++;
563 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
564 			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
565 			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
566 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
567 				/* duplicate, discard */
568 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
569 				    bssid, "duplicate",
570 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
571 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
572 				    ni->ni_rxseqs[tid] >>
573 					IEEE80211_SEQ_SEQ_SHIFT,
574 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
575 				    ni->ni_rxseqs[tid] &
576 					IEEE80211_SEQ_FRAG_MASK,
577 				    tid);
578 				vap->iv_stats.is_rx_dup++;
579 				IEEE80211_NODE_STAT(ni, rx_dup);
580 				goto out;
581 			}
582 			ni->ni_rxseqs[tid] = rxseq;
583 		}
584 	}
585 
586 	switch (type) {
587 	case IEEE80211_FC0_TYPE_DATA:
588 		hdrspace = ieee80211_hdrspace(ic, wh);
589 		if (m->m_len < hdrspace &&
590 		    (m = m_pullup(m, hdrspace)) == NULL) {
591 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
592 			    ni->ni_macaddr, NULL,
593 			    "data too short: expecting %u", hdrspace);
594 			vap->iv_stats.is_rx_tooshort++;
595 			goto out;		/* XXX */
596 		}
597 		/*
598 		 * Handle A-MPDU re-ordering.  The station must be
599 		 * associated and negotiated HT.  The frame must be
600 		 * a QoS frame (not QoS null data) and not previously
601 		 * processed for A-MPDU re-ordering.  If the frame is
602 		 * to be processed directly then ieee80211_ampdu_reorder
603 		 * will return 0; otherwise it has consumed the mbuf
604 		 * and we should do nothing more with it.
605 		 */
606 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
607 		    subtype == IEEE80211_FC0_SUBTYPE_QOS &&
608 		    (dir == IEEE80211_FC1_DIR_FROMDS ||
609 		     dir == IEEE80211_FC1_DIR_DSTODS) &&
610 		    ieee80211_ampdu_reorder(ni, m) != 0) {
611 			m = NULL;
612 			goto out;
613 		}
614 	resubmit_ampdu:
615 		if (dir == IEEE80211_FC1_DIR_FROMDS) {
616 			if ((ifp->if_flags & IFF_SIMPLEX) &&
617 			    isfromds_mcastecho(vap, wh)) {
618 				/*
619 				 * In IEEE802.11 network, multicast
620 				 * packets sent from "me" are broadcast
621 				 * from the AP; silently discard for
622 				 * SIMPLEX interface.
623 				 */
624 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
625 				    wh, "data", "%s", "multicast echo");
626 				vap->iv_stats.is_rx_mcastecho++;
627 				goto out;
628 			}
629 			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
630 			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
631 				/*
632 				 * DWDS sta's must drop 3-address mcast frames
633 				 * as they will be sent separately as a 4-addr
634 				 * frame.  Accepting the 3-addr frame will
635 				 * confuse the bridge into thinking the sending
636 				 * sta is located at the end of WDS link.
637 				 */
638 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
639 				    "3-address data", "%s", "DWDS enabled");
640 				vap->iv_stats.is_rx_mcastecho++;
641 				goto out;
642 			}
643 		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
644 			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
645 				IEEE80211_DISCARD(vap,
646 				    IEEE80211_MSG_INPUT, wh, "4-address data",
647 				    "%s", "DWDS not enabled");
648 				vap->iv_stats.is_rx_wrongdir++;
649 				goto out;
650 			}
651 			if ((ifp->if_flags & IFF_SIMPLEX) &&
652 			    isdstods_mcastecho(vap, wh)) {
653 				/*
654 				 * In IEEE802.11 network, multicast
655 				 * packets sent from "me" are broadcast
656 				 * from the AP; silently discard for
657 				 * SIMPLEX interface.
658 				 */
659 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
660 				    "4-address data", "%s", "multicast echo");
661 				vap->iv_stats.is_rx_mcastecho++;
662 				goto out;
663 			}
664 		} else {
665 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
666 			    "data", "incorrect dir 0x%x", dir);
667 			vap->iv_stats.is_rx_wrongdir++;
668 			goto out;
669 		}
670 
671 		/*
672 		 * Handle privacy requirements.  Note that we
673 		 * must not be preempted from here until after
674 		 * we (potentially) call ieee80211_crypto_demic;
675 		 * otherwise we may violate assumptions in the
676 		 * crypto cipher modules used to do delayed update
677 		 * of replay sequence numbers.
678 		 */
679 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
680 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
681 				/*
682 				 * Discard encrypted frames when privacy is off.
683 				 */
684 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
685 				    wh, "WEP", "%s", "PRIVACY off");
686 				vap->iv_stats.is_rx_noprivacy++;
687 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
688 				goto out;
689 			}
690 			key = ieee80211_crypto_decap(ni, m, hdrspace);
691 			if (key == NULL) {
692 				/* NB: stats+msgs handled in crypto_decap */
693 				IEEE80211_NODE_STAT(ni, rx_wepfail);
694 				goto out;
695 			}
696 			wh = mtod(m, struct ieee80211_frame *);
697 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
698 		} else {
699 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
700 			key = NULL;
701 		}
702 
703 		/*
704 		 * Save QoS bits for use below--before we strip the header.
705 		 */
706 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
707 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
708 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
709 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
710 		} else
711 			qos = 0;
712 
713 		/*
714 		 * Next up, any fragmentation.
715 		 */
716 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
717 			m = ieee80211_defrag(ni, m, hdrspace);
718 			if (m == NULL) {
719 				/* Fragment dropped or frame not complete yet */
720 				goto out;
721 			}
722 		}
723 		wh = NULL;		/* no longer valid, catch any uses */
724 
725 		/*
726 		 * Next strip any MSDU crypto bits.
727 		 */
728 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
729 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
730 			    ni->ni_macaddr, "data", "%s", "demic error");
731 			vap->iv_stats.is_rx_demicfail++;
732 			IEEE80211_NODE_STAT(ni, rx_demicfail);
733 			goto out;
734 		}
735 
736 		/* copy to listener after decrypt */
737 		if (bpf_peers_present(vap->iv_rawbpf))
738 			bpf_mtap(vap->iv_rawbpf, m);
739 		need_tap = 0;
740 
741 		/*
742 		 * Finally, strip the 802.11 header.
743 		 */
744 		m = ieee80211_decap(vap, m, hdrspace);
745 		if (m == NULL) {
746 			/* XXX mask bit to check for both */
747 			/* don't count Null data frames as errors */
748 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
749 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
750 				goto out;
751 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
752 			    ni->ni_macaddr, "data", "%s", "decap error");
753 			vap->iv_stats.is_rx_decap++;
754 			IEEE80211_NODE_STAT(ni, rx_decap);
755 			goto err;
756 		}
757 		eh = mtod(m, struct ether_header *);
758 		if (!ieee80211_node_is_authorized(ni)) {
759 			/*
760 			 * Deny any non-PAE frames received prior to
761 			 * authorization.  For open/shared-key
762 			 * authentication the port is mark authorized
763 			 * after authentication completes.  For 802.1x
764 			 * the port is not marked authorized by the
765 			 * authenticator until the handshake has completed.
766 			 */
767 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
768 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
769 				    eh->ether_shost, "data",
770 				    "unauthorized port: ether type 0x%x len %u",
771 				    eh->ether_type, m->m_pkthdr.len);
772 				vap->iv_stats.is_rx_unauth++;
773 				IEEE80211_NODE_STAT(ni, rx_unauth);
774 				goto err;
775 			}
776 		} else {
777 			/*
778 			 * When denying unencrypted frames, discard
779 			 * any non-PAE frames received without encryption.
780 			 */
781 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
782 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
783 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
784 				/*
785 				 * Drop unencrypted frames.
786 				 */
787 				vap->iv_stats.is_rx_unencrypted++;
788 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
789 				goto out;
790 			}
791 		}
792 		/* XXX require HT? */
793 		if (qos & IEEE80211_QOS_AMSDU) {
794 			m = ieee80211_decap_amsdu(ni, m);
795 			if (m == NULL)
796 				return IEEE80211_FC0_TYPE_DATA;
797 		} else if ((ni->ni_ath_flags & IEEE80211_NODE_FF) &&
798 #define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
799 		    m->m_pkthdr.len >= 3*FF_LLC_SIZE) {
800 			struct llc *llc;
801 
802 			/*
803 			 * Check for fast-frame tunnel encapsulation.
804 			 */
805 			if (m->m_len < FF_LLC_SIZE &&
806 			    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
807 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
808 				    ni->ni_macaddr, "fast-frame",
809 				    "%s", "m_pullup(llc) failed");
810 				vap->iv_stats.is_rx_tooshort++;
811 				return IEEE80211_FC0_TYPE_DATA;
812 			}
813 			llc = (struct llc *)(mtod(m, uint8_t *) +
814 				sizeof(struct ether_header));
815 			if (llc->llc_snap.ether_type == htons(ATH_FF_ETH_TYPE)) {
816 				m_adj(m, FF_LLC_SIZE);
817 				m = ieee80211_decap_fastframe(ni, m);
818 				if (m == NULL)
819 					return IEEE80211_FC0_TYPE_DATA;
820 			}
821 		}
822 #undef FF_LLC_SIZE
823 		ieee80211_deliver_data(vap, ni, m);
824 		return IEEE80211_FC0_TYPE_DATA;
825 
826 	case IEEE80211_FC0_TYPE_MGT:
827 		vap->iv_stats.is_rx_mgmt++;
828 		IEEE80211_NODE_STAT(ni, rx_mgmt);
829 		if (dir != IEEE80211_FC1_DIR_NODS) {
830 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
831 			    wh, "data", "incorrect dir 0x%x", dir);
832 			vap->iv_stats.is_rx_wrongdir++;
833 			goto err;
834 		}
835 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
836 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
837 			    ni->ni_macaddr, "mgt", "too short: len %u",
838 			    m->m_pkthdr.len);
839 			vap->iv_stats.is_rx_tooshort++;
840 			goto out;
841 		}
842 #ifdef IEEE80211_DEBUG
843 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
844 		    ieee80211_msg_dumppkts(vap)) {
845 			if_printf(ifp, "received %s from %s rssi %d\n",
846 			    ieee80211_mgt_subtype_name[subtype >>
847 				IEEE80211_FC0_SUBTYPE_SHIFT],
848 			    ether_sprintf(wh->i_addr2), rssi);
849 		}
850 #endif
851 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
852 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
853 				/*
854 				 * Only shared key auth frames with a challenge
855 				 * should be encrypted, discard all others.
856 				 */
857 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
858 				    wh, ieee80211_mgt_subtype_name[subtype >>
859 					IEEE80211_FC0_SUBTYPE_SHIFT],
860 				    "%s", "WEP set but not permitted");
861 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
862 				goto out;
863 			}
864 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
865 				/*
866 				 * Discard encrypted frames when privacy is off.
867 				 */
868 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
869 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
870 				vap->iv_stats.is_rx_noprivacy++;
871 				goto out;
872 			}
873 			hdrspace = ieee80211_hdrspace(ic, wh);
874 			key = ieee80211_crypto_decap(ni, m, hdrspace);
875 			if (key == NULL) {
876 				/* NB: stats+msgs handled in crypto_decap */
877 				goto out;
878 			}
879 			wh = mtod(m, struct ieee80211_frame *);
880 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
881 		}
882 		if (bpf_peers_present(vap->iv_rawbpf))
883 			bpf_mtap(vap->iv_rawbpf, m);
884 		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
885 		m_freem(m);
886 		return IEEE80211_FC0_TYPE_MGT;
887 
888 	case IEEE80211_FC0_TYPE_CTL:
889 		vap->iv_stats.is_rx_ctl++;
890 		IEEE80211_NODE_STAT(ni, rx_ctrl);
891 		goto out;
892 	default:
893 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
894 		    wh, NULL, "bad frame type 0x%x", type);
895 		/* should not come here */
896 		break;
897 	}
898 err:
899 	ifp->if_ierrors++;
900 out:
901 	if (m != NULL) {
902 		if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
903 			bpf_mtap(vap->iv_rawbpf, m);
904 		m_freem(m);
905 	}
906 	return type;
907 #undef SEQ_LEQ
908 }
909 
910 static void
911 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
912     int rssi, int noise, uint32_t rstamp, uint16_t seq, uint16_t status)
913 {
914 	struct ieee80211vap *vap = ni->ni_vap;
915 
916 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
917 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
918 		    ni->ni_macaddr, "open auth",
919 		    "bad sta auth mode %u", ni->ni_authmode);
920 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
921 		return;
922 	}
923 	if (vap->iv_state != IEEE80211_S_AUTH ||
924 	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
925 		vap->iv_stats.is_rx_bad_auth++;
926 		return;
927 	}
928 	if (status != 0) {
929 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
930 		    ni, "open auth failed (reason %d)", status);
931 		vap->iv_stats.is_rx_auth_fail++;
932 		vap->iv_stats.is_rx_authfail_code = status;
933 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
934 		    IEEE80211_SCAN_FAIL_STATUS);
935 	} else
936 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
937 }
938 
939 static void
940 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
941     uint8_t *frm, uint8_t *efrm, int rssi, int noise, uint32_t rstamp,
942     uint16_t seq, uint16_t status)
943 {
944 	struct ieee80211vap *vap = ni->ni_vap;
945 	uint8_t *challenge;
946 	int estatus;
947 
948 	/*
949 	 * NB: this can happen as we allow pre-shared key
950 	 * authentication to be enabled w/o wep being turned
951 	 * on so that configuration of these can be done
952 	 * in any order.  It may be better to enforce the
953 	 * ordering in which case this check would just be
954 	 * for sanity/consistency.
955 	 */
956 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
957 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
958 		    ni->ni_macaddr, "shared key auth",
959 		    "%s", " PRIVACY is disabled");
960 		estatus = IEEE80211_STATUS_ALG;
961 		goto bad;
962 	}
963 	/*
964 	 * Pre-shared key authentication is evil; accept
965 	 * it only if explicitly configured (it is supported
966 	 * mainly for compatibility with clients like OS X).
967 	 */
968 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
969 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
970 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
971 		    ni->ni_macaddr, "shared key auth",
972 		    "bad sta auth mode %u", ni->ni_authmode);
973 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
974 		estatus = IEEE80211_STATUS_ALG;
975 		goto bad;
976 	}
977 
978 	challenge = NULL;
979 	if (frm + 1 < efrm) {
980 		if ((frm[1] + 2) > (efrm - frm)) {
981 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
982 			    ni->ni_macaddr, "shared key auth",
983 			    "ie %d/%d too long",
984 			    frm[0], (frm[1] + 2) - (efrm - frm));
985 			vap->iv_stats.is_rx_bad_auth++;
986 			estatus = IEEE80211_STATUS_CHALLENGE;
987 			goto bad;
988 		}
989 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
990 			challenge = frm;
991 		frm += frm[1] + 2;
992 	}
993 	switch (seq) {
994 	case IEEE80211_AUTH_SHARED_CHALLENGE:
995 	case IEEE80211_AUTH_SHARED_RESPONSE:
996 		if (challenge == NULL) {
997 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
998 			    ni->ni_macaddr, "shared key auth",
999 			    "%s", "no challenge");
1000 			vap->iv_stats.is_rx_bad_auth++;
1001 			estatus = IEEE80211_STATUS_CHALLENGE;
1002 			goto bad;
1003 		}
1004 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1005 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1006 			    ni->ni_macaddr, "shared key auth",
1007 			    "bad challenge len %d", challenge[1]);
1008 			vap->iv_stats.is_rx_bad_auth++;
1009 			estatus = IEEE80211_STATUS_CHALLENGE;
1010 			goto bad;
1011 		}
1012 	default:
1013 		break;
1014 	}
1015 	if (vap->iv_state != IEEE80211_S_AUTH)
1016 		return;
1017 	switch (seq) {
1018 	case IEEE80211_AUTH_SHARED_PASS:
1019 		if (ni->ni_challenge != NULL) {
1020 			FREE(ni->ni_challenge, M_80211_NODE);
1021 			ni->ni_challenge = NULL;
1022 		}
1023 		if (status != 0) {
1024 			IEEE80211_NOTE_FRAME(vap,
1025 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1026 			    "shared key auth failed (reason %d)", status);
1027 			vap->iv_stats.is_rx_auth_fail++;
1028 			vap->iv_stats.is_rx_authfail_code = status;
1029 			return;
1030 		}
1031 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1032 		break;
1033 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1034 		if (!ieee80211_alloc_challenge(ni))
1035 			return;
1036 		/* XXX could optimize by passing recvd challenge */
1037 		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1038 		IEEE80211_SEND_MGMT(ni,
1039 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1040 		break;
1041 	default:
1042 		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1043 		    wh, "shared key auth", "bad seq %d", seq);
1044 		vap->iv_stats.is_rx_bad_auth++;
1045 		return;
1046 	}
1047 	return;
1048 bad:
1049 	/*
1050 	 * Kick the state machine.  This short-circuits
1051 	 * using the mgt frame timeout to trigger the
1052 	 * state transition.
1053 	 */
1054 	if (vap->iv_state == IEEE80211_S_AUTH)
1055 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1056 		    IEEE80211_SCAN_FAIL_STATUS);
1057 }
1058 
1059 static int
1060 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1061 	const struct ieee80211_frame *wh)
1062 {
1063 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1064 	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1065 	u_int len = frm[1], qosinfo;
1066 	int i;
1067 
1068 	if (len < sizeof(struct ieee80211_wme_param)-2) {
1069 		IEEE80211_DISCARD_IE(vap,
1070 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1071 		    wh, "WME", "too short, len %u", len);
1072 		return -1;
1073 	}
1074 	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1075 	qosinfo &= WME_QOSINFO_COUNT;
1076 	/* XXX do proper check for wraparound */
1077 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1078 		return 0;
1079 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1080 	for (i = 0; i < WME_NUM_AC; i++) {
1081 		struct wmeParams *wmep =
1082 			&wme->wme_wmeChanParams.cap_wmeParams[i];
1083 		/* NB: ACI not used */
1084 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1085 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1086 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1087 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1088 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1089 		frm += 4;
1090 	}
1091 	wme->wme_wmeChanParams.cap_info = qosinfo;
1092 	return 1;
1093 #undef MS
1094 }
1095 
1096 static int
1097 ieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm,
1098 	const struct ieee80211_frame *wh)
1099 {
1100 	struct ieee80211vap *vap = ni->ni_vap;
1101 	const struct ieee80211_ath_ie *ath;
1102 	u_int len = frm[1];
1103 	int capschanged;
1104 	uint16_t defkeyix;
1105 
1106 	if (len < sizeof(struct ieee80211_ath_ie)-2) {
1107 		IEEE80211_DISCARD_IE(vap,
1108 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG,
1109 		    wh, "Atheros", "too short, len %u", len);
1110 		return -1;
1111 	}
1112 	ath = (const struct ieee80211_ath_ie *)frm;
1113 	capschanged = (ni->ni_ath_flags != ath->ath_capability);
1114 	defkeyix = LE_READ_2(ath->ath_defkeyix);
1115 	if (capschanged || defkeyix != ni->ni_ath_defkeyix) {
1116 		ni->ni_ath_flags = ath->ath_capability;
1117 		ni->ni_ath_defkeyix = defkeyix;
1118 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
1119 		    "ath ie change: new caps 0x%x defkeyix 0x%x",
1120 		    ni->ni_ath_flags, ni->ni_ath_defkeyix);
1121 	}
1122 	if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) {
1123 		uint16_t curflags, newflags;
1124 
1125 		/*
1126 		 * Check for turbo mode switch.  Calculate flags
1127 		 * for the new mode and effect the switch.
1128 		 */
1129 		newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags;
1130 		/* NB: BOOST is not in ic_flags, so get it from the ie */
1131 		if (ath->ath_capability & ATHEROS_CAP_BOOST)
1132 			newflags |= IEEE80211_CHAN_TURBO;
1133 		else
1134 			newflags &= ~IEEE80211_CHAN_TURBO;
1135 		if (newflags != curflags)
1136 			ieee80211_dturbo_switch(vap, newflags);
1137 	}
1138 	return capschanged;
1139 }
1140 
1141 /*
1142  * Return non-zero if a background scan may be continued:
1143  * o bg scan is active
1144  * o no channel switch is pending
1145  * o there has not been any traffic recently
1146  *
1147  * Note we do not check if there is an administrative enable;
1148  * this is only done to start the scan.  We assume that any
1149  * change in state will be accompanied by a request to cancel
1150  * active scans which will otherwise cause this test to fail.
1151  */
1152 static __inline int
1153 contbgscan(struct ieee80211vap *vap)
1154 {
1155 	struct ieee80211com *ic = vap->iv_ic;
1156 
1157 	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1158 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1159 	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1160 	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1161 }
1162 
1163 /*
1164  * Return non-zero if a backgrond scan may be started:
1165  * o bg scanning is administratively enabled
1166  * o no channel switch is pending
1167  * o we are not boosted on a dynamic turbo channel
1168  * o there has not been a scan recently
1169  * o there has not been any traffic recently
1170  */
1171 static __inline int
1172 startbgscan(struct ieee80211vap *vap)
1173 {
1174 	struct ieee80211com *ic = vap->iv_ic;
1175 
1176 	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1177 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1178 	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1179 	    time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1180 	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1181 }
1182 
1183 static void
1184 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1185 	int subtype, int rssi, int noise, uint32_t rstamp)
1186 {
1187 #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1188 #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1189 	struct ieee80211vap *vap = ni->ni_vap;
1190 	struct ieee80211com *ic = ni->ni_ic;
1191 	struct ieee80211_frame *wh;
1192 	uint8_t *frm, *efrm;
1193 	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1194 	uint8_t rate;
1195 
1196 	wh = mtod(m0, struct ieee80211_frame *);
1197 	frm = (uint8_t *)&wh[1];
1198 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1199 	switch (subtype) {
1200 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1201 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1202 		struct ieee80211_scanparams scan;
1203 		/*
1204 		 * We process beacon/probe response frames:
1205 		 *    o when scanning, or
1206 		 *    o station mode when associated (to collect state
1207 		 *      updates such as 802.11g slot time), or
1208 		 * Frames otherwise received are discarded.
1209 		 */
1210 		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1211 			vap->iv_stats.is_rx_mgtdiscard++;
1212 			return;
1213 		}
1214 		/* XXX probe response in sta mode when !scanning? */
1215 		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1216 			return;
1217 		/*
1218 		 * Count frame now that we know it's to be processed.
1219 		 */
1220 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1221 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1222 			IEEE80211_NODE_STAT(ni, rx_beacons);
1223 		} else
1224 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1225 		/*
1226 		 * When operating in station mode, check for state updates.
1227 		 * Be careful to ignore beacons received while doing a
1228 		 * background scan.  We consider only 11g/WMM stuff right now.
1229 		 */
1230 		if (ni->ni_associd != 0 &&
1231 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1232 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1233 			/* record tsf of last beacon */
1234 			memcpy(ni->ni_tstamp.data, scan.tstamp,
1235 				sizeof(ni->ni_tstamp));
1236 			/* count beacon frame for s/w bmiss handling */
1237 			vap->iv_swbmiss_count++;
1238 			vap->iv_bmiss_count = 0;
1239 			if (ni->ni_erp != scan.erp) {
1240 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1241 				    wh->i_addr2,
1242 				    "erp change: was 0x%x, now 0x%x",
1243 				    ni->ni_erp, scan.erp);
1244 				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1245 				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1246 					ic->ic_flags |= IEEE80211_F_USEPROT;
1247 				else
1248 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1249 				ni->ni_erp = scan.erp;
1250 				/* XXX statistic */
1251 				/* XXX driver notification */
1252 			}
1253 			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1254 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1255 				    wh->i_addr2,
1256 				    "capabilities change: was 0x%x, now 0x%x",
1257 				    ni->ni_capinfo, scan.capinfo);
1258 				/*
1259 				 * NB: we assume short preamble doesn't
1260 				 *     change dynamically
1261 				 */
1262 				ieee80211_set_shortslottime(ic,
1263 					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1264 					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1265 				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1266 					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1267 				/* XXX statistic */
1268 			}
1269 			if (scan.wme != NULL &&
1270 			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1271 			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1272 				ieee80211_wme_updateparams(vap);
1273 			if (scan.ath != NULL)
1274 				ieee80211_parse_athparams(ni, scan.ath, wh);
1275 			if (scan.htcap != NULL && scan.htinfo != NULL) {
1276 				ieee80211_parse_htcap(ni, scan.htcap);
1277 				ieee80211_parse_htinfo(ni, scan.htinfo);
1278 				/* XXX state changes? */
1279 			}
1280 			if (scan.tim != NULL) {
1281 				struct ieee80211_tim_ie *tim =
1282 				    (struct ieee80211_tim_ie *) scan.tim;
1283 #if 0
1284 				int aid = IEEE80211_AID(ni->ni_associd);
1285 				int ix = aid / NBBY;
1286 				int min = tim->tim_bitctl &~ 1;
1287 				int max = tim->tim_len + min - 4;
1288 				if ((tim->tim_bitctl&1) ||
1289 				    (min <= ix && ix <= max &&
1290 				     isset(tim->tim_bitmap - min, aid))) {
1291 					/*
1292 					 * XXX Do not let bg scan kick off
1293 					 * we are expecting data.
1294 					 */
1295 					ic->ic_lastdata = ticks;
1296 					ieee80211_sta_pwrsave(vap, 0);
1297 				}
1298 #endif
1299 				ni->ni_dtim_count = tim->tim_count;
1300 				ni->ni_dtim_period = tim->tim_period;
1301 			}
1302 			/*
1303 			 * If scanning, pass the info to the scan module.
1304 			 * Otherwise, check if it's the right time to do
1305 			 * a background scan.  Background scanning must
1306 			 * be enabled and we must not be operating in the
1307 			 * turbo phase of dynamic turbo mode.  Then,
1308 			 * it's been a while since the last background
1309 			 * scan and if no data frames have come through
1310 			 * recently, kick off a scan.  Note that this
1311 			 * is the mechanism by which a background scan
1312 			 * is started _and_ continued each time we
1313 			 * return on-channel to receive a beacon from
1314 			 * our ap.
1315 			 */
1316 			if (ic->ic_flags & IEEE80211_F_SCAN) {
1317 				ieee80211_add_scan(vap, &scan, wh,
1318 					subtype, rssi, noise, rstamp);
1319 			} else if (contbgscan(vap)) {
1320 				ieee80211_bg_scan(vap, 0);
1321 			} else if (startbgscan(vap)) {
1322 				vap->iv_stats.is_scan_bg++;
1323 #if 0
1324 				/* wakeup if we are sleeing */
1325 				ieee80211_set_pwrsave(vap, 0);
1326 #endif
1327 				ieee80211_bg_scan(vap, 0);
1328 			}
1329 			return;
1330 		}
1331 		/*
1332 		 * If scanning, just pass information to the scan module.
1333 		 */
1334 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1335 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1336 				/*
1337 				 * Actively scanning a channel marked passive;
1338 				 * send a probe request now that we know there
1339 				 * is 802.11 traffic present.
1340 				 *
1341 				 * XXX check if the beacon we recv'd gives
1342 				 * us what we need and suppress the probe req
1343 				 */
1344 				ieee80211_probe_curchan(vap, 1);
1345 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1346 			}
1347 			ieee80211_add_scan(vap, &scan, wh,
1348 				subtype, rssi, noise, rstamp);
1349 			return;
1350 		}
1351 		break;
1352 	}
1353 
1354 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1355 		uint16_t algo, seq, status;
1356 		/*
1357 		 * auth frame format
1358 		 *	[2] algorithm
1359 		 *	[2] sequence
1360 		 *	[2] status
1361 		 *	[tlv*] challenge
1362 		 */
1363 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1364 		algo   = le16toh(*(uint16_t *)frm);
1365 		seq    = le16toh(*(uint16_t *)(frm + 2));
1366 		status = le16toh(*(uint16_t *)(frm + 4));
1367 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1368 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1369 
1370 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1371 			IEEE80211_DISCARD(vap,
1372 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1373 			    wh, "auth", "%s", "TKIP countermeasures enabled");
1374 			vap->iv_stats.is_rx_auth_countermeasures++;
1375 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1376 				ieee80211_send_error(ni, wh->i_addr2,
1377 					IEEE80211_FC0_SUBTYPE_AUTH,
1378 					IEEE80211_REASON_MIC_FAILURE);
1379 			}
1380 			return;
1381 		}
1382 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1383 			sta_auth_shared(ni, wh, frm + 6, efrm, rssi,
1384 			    noise, rstamp, seq, status);
1385 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1386 			sta_auth_open(ni, wh, rssi, noise, rstamp,
1387 			    seq, status);
1388 		else {
1389 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1390 			    wh, "auth", "unsupported alg %d", algo);
1391 			vap->iv_stats.is_rx_auth_unsupported++;
1392 			return;
1393 		}
1394 		break;
1395 	}
1396 
1397 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1398 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1399 		uint16_t capinfo, associd;
1400 		uint16_t status;
1401 
1402 		if (vap->iv_state != IEEE80211_S_ASSOC) {
1403 			vap->iv_stats.is_rx_mgtdiscard++;
1404 			return;
1405 		}
1406 
1407 		/*
1408 		 * asresp frame format
1409 		 *	[2] capability information
1410 		 *	[2] status
1411 		 *	[2] association ID
1412 		 *	[tlv] supported rates
1413 		 *	[tlv] extended supported rates
1414 		 *	[tlv] WME
1415 		 *	[tlv] HT capabilities
1416 		 *	[tlv] HT info
1417 		 */
1418 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1419 		ni = vap->iv_bss;
1420 		capinfo = le16toh(*(uint16_t *)frm);
1421 		frm += 2;
1422 		status = le16toh(*(uint16_t *)frm);
1423 		frm += 2;
1424 		if (status != 0) {
1425 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1426 			    wh->i_addr2, "%sassoc failed (reason %d)",
1427 			    ISREASSOC(subtype) ?  "re" : "", status);
1428 			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1429 			return;
1430 		}
1431 		associd = le16toh(*(uint16_t *)frm);
1432 		frm += 2;
1433 
1434 		rates = xrates = wme = htcap = htinfo = NULL;
1435 		while (efrm - frm > 1) {
1436 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1437 			switch (*frm) {
1438 			case IEEE80211_ELEMID_RATES:
1439 				rates = frm;
1440 				break;
1441 			case IEEE80211_ELEMID_XRATES:
1442 				xrates = frm;
1443 				break;
1444 			case IEEE80211_ELEMID_HTCAP:
1445 				htcap = frm;
1446 				break;
1447 			case IEEE80211_ELEMID_HTINFO:
1448 				htinfo = frm;
1449 				break;
1450 			case IEEE80211_ELEMID_VENDOR:
1451 				if (iswmeoui(frm))
1452 					wme = frm;
1453 				else if (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
1454 					/*
1455 					 * Accept pre-draft HT ie's if the
1456 					 * standard ones have not been seen.
1457 					 */
1458 					if (ishtcapoui(frm)) {
1459 						if (htcap == NULL)
1460 							htcap = frm;
1461 					} else if (ishtinfooui(frm)) {
1462 						if (htinfo == NULL)
1463 							htcap = frm;
1464 					}
1465 				}
1466 				/* XXX Atheros OUI support */
1467 				break;
1468 			}
1469 			frm += frm[1] + 2;
1470 		}
1471 
1472 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1473 		if (xrates != NULL)
1474 			IEEE80211_VERIFY_ELEMENT(xrates,
1475 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1476 		rate = ieee80211_setup_rates(ni, rates, xrates,
1477 				IEEE80211_F_JOIN |
1478 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1479 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1480 		if (rate & IEEE80211_RATE_BASIC) {
1481 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1482 			    wh->i_addr2,
1483 			    "%sassoc failed (rate set mismatch)",
1484 			    ISREASSOC(subtype) ?  "re" : "");
1485 			vap->iv_stats.is_rx_assoc_norate++;
1486 			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1487 			    IEEE80211_SCAN_FAIL_STATUS);
1488 			return;
1489 		}
1490 
1491 		ni->ni_capinfo = capinfo;
1492 		ni->ni_associd = associd;
1493 		if (ni->ni_jointime == 0)
1494 			ni->ni_jointime = time_uptime;
1495 		if (wme != NULL &&
1496 		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1497 			ni->ni_flags |= IEEE80211_NODE_QOS;
1498 			ieee80211_wme_updateparams(vap);
1499 		} else
1500 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1501 		/*
1502 		 * Setup HT state according to the negotiation.
1503 		 *
1504 		 * NB: shouldn't need to check if HT use is enabled but some
1505 		 *     ap's send back HT ie's even when we don't indicate we
1506 		 *     are HT capable in our AssocReq.
1507 		 */
1508 		if (htcap != NULL && htinfo != NULL &&
1509 		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1510 			ieee80211_ht_node_init(ni, htcap);
1511 			ieee80211_parse_htinfo(ni, htinfo);
1512 			ieee80211_setup_htrates(ni, htcap,
1513 			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1514 			ieee80211_setup_basic_htrates(ni, htinfo);
1515 		}
1516 		/*
1517 		 * Configure state now that we are associated.
1518 		 *
1519 		 * XXX may need different/additional driver callbacks?
1520 		 */
1521 		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1522 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1523 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1524 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1525 		} else {
1526 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1527 			ic->ic_flags |= IEEE80211_F_USEBARKER;
1528 		}
1529 		ieee80211_set_shortslottime(ic,
1530 			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1531 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1532 		/*
1533 		 * Honor ERP protection.
1534 		 *
1535 		 * NB: ni_erp should zero for non-11g operation.
1536 		 */
1537 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1538 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1539 			ic->ic_flags |= IEEE80211_F_USEPROT;
1540 		else
1541 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1542 		IEEE80211_NOTE_MAC(vap,
1543 		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1544 		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s",
1545 		    ISREASSOC(subtype) ? "re" : "",
1546 		    IEEE80211_NODE_AID(ni),
1547 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1548 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1549 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1550 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1551 		    ni->ni_flags & IEEE80211_NODE_HT ?
1552 			(ni->ni_chw == 20 ? ", HT20" : ", HT40") : "",
1553 		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1554 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1555 			", fast-frames" : "",
1556 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1557 			", turbo" : ""
1558 		);
1559 		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1560 		break;
1561 	}
1562 
1563 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1564 		uint16_t reason;
1565 
1566 		if (vap->iv_state == IEEE80211_S_SCAN) {
1567 			vap->iv_stats.is_rx_mgtdiscard++;
1568 			return;
1569 		}
1570 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1571 			/* NB: can happen when in promiscuous mode */
1572 			vap->iv_stats.is_rx_mgtdiscard++;
1573 			break;
1574 		}
1575 
1576 		/*
1577 		 * deauth frame format
1578 		 *	[2] reason
1579 		 */
1580 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1581 		reason = le16toh(*(uint16_t *)frm);
1582 
1583 		vap->iv_stats.is_rx_deauth++;
1584 		vap->iv_stats.is_rx_deauth_code = reason;
1585 		IEEE80211_NODE_STAT(ni, rx_deauth);
1586 
1587 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1588 		    "recv deauthenticate (reason %d)", reason);
1589 		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1590 		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1591 		break;
1592 	}
1593 
1594 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1595 		uint16_t reason;
1596 
1597 		if (vap->iv_state != IEEE80211_S_RUN &&
1598 		    vap->iv_state != IEEE80211_S_ASSOC &&
1599 		    vap->iv_state != IEEE80211_S_AUTH) {
1600 			vap->iv_stats.is_rx_mgtdiscard++;
1601 			return;
1602 		}
1603 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1604 			/* NB: can happen when in promiscuous mode */
1605 			vap->iv_stats.is_rx_mgtdiscard++;
1606 			break;
1607 		}
1608 
1609 		/*
1610 		 * disassoc frame format
1611 		 *	[2] reason
1612 		 */
1613 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1614 		reason = le16toh(*(uint16_t *)frm);
1615 
1616 		vap->iv_stats.is_rx_disassoc++;
1617 		vap->iv_stats.is_rx_disassoc_code = reason;
1618 		IEEE80211_NODE_STAT(ni, rx_disassoc);
1619 
1620 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1621 		    "recv disassociate (reason %d)", reason);
1622 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1623 		break;
1624 	}
1625 
1626 	case IEEE80211_FC0_SUBTYPE_ACTION:
1627 		if (vap->iv_state == IEEE80211_S_RUN) {
1628 			if (ieee80211_parse_action(ni, m0) == 0)
1629 				ic->ic_recv_action(ni, frm, efrm);
1630 		} else
1631 			vap->iv_stats.is_rx_mgtdiscard++;
1632 		break;
1633 
1634 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1635 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1636 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1637 		vap->iv_stats.is_rx_mgtdiscard++;
1638 		return;
1639 	default:
1640 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1641 		     wh, "mgt", "subtype 0x%x not handled", subtype);
1642 		vap->iv_stats.is_rx_badsubtype++;
1643 		break;
1644 	}
1645 #undef ISREASSOC
1646 #undef ISPROBE
1647 }
1648