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