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