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