xref: /freebsd/sys/net80211/ieee80211_sta.c (revision 079171874c9bf263b69e3af10784ad2bcd1fe699)
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 
980 	/*
981 	 * NB: this can happen as we allow pre-shared key
982 	 * authentication to be enabled w/o wep being turned
983 	 * on so that configuration of these can be done
984 	 * in any order.  It may be better to enforce the
985 	 * ordering in which case this check would just be
986 	 * for sanity/consistency.
987 	 */
988 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
989 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
990 		    ni->ni_macaddr, "shared key auth",
991 		    "%s", " PRIVACY is disabled");
992 		goto bad;
993 	}
994 	/*
995 	 * Pre-shared key authentication is evil; accept
996 	 * it only if explicitly configured (it is supported
997 	 * mainly for compatibility with clients like OS X).
998 	 */
999 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1000 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1001 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1002 		    ni->ni_macaddr, "shared key auth",
1003 		    "bad sta auth mode %u", ni->ni_authmode);
1004 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1005 		goto bad;
1006 	}
1007 
1008 	challenge = NULL;
1009 	if (frm + 1 < efrm) {
1010 		if ((frm[1] + 2) > (efrm - frm)) {
1011 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1012 			    ni->ni_macaddr, "shared key auth",
1013 			    "ie %d/%d too long",
1014 			    frm[0], (frm[1] + 2) - (efrm - frm));
1015 			vap->iv_stats.is_rx_bad_auth++;
1016 			goto bad;
1017 		}
1018 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1019 			challenge = frm;
1020 		frm += frm[1] + 2;
1021 	}
1022 	switch (seq) {
1023 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1024 	case IEEE80211_AUTH_SHARED_RESPONSE:
1025 		if (challenge == NULL) {
1026 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1027 			    ni->ni_macaddr, "shared key auth",
1028 			    "%s", "no challenge");
1029 			vap->iv_stats.is_rx_bad_auth++;
1030 			goto bad;
1031 		}
1032 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1033 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1034 			    ni->ni_macaddr, "shared key auth",
1035 			    "bad challenge len %d", challenge[1]);
1036 			vap->iv_stats.is_rx_bad_auth++;
1037 			goto bad;
1038 		}
1039 	default:
1040 		break;
1041 	}
1042 	if (vap->iv_state != IEEE80211_S_AUTH)
1043 		return;
1044 	switch (seq) {
1045 	case IEEE80211_AUTH_SHARED_PASS:
1046 		if (ni->ni_challenge != NULL) {
1047 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1048 			ni->ni_challenge = NULL;
1049 		}
1050 		if (status != 0) {
1051 			IEEE80211_NOTE_FRAME(vap,
1052 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1053 			    "shared key auth failed (reason %d)", status);
1054 			vap->iv_stats.is_rx_auth_fail++;
1055 			vap->iv_stats.is_rx_authfail_code = status;
1056 			return;
1057 		}
1058 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1059 		break;
1060 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1061 		if (!ieee80211_alloc_challenge(ni))
1062 			return;
1063 		/* XXX could optimize by passing recvd challenge */
1064 		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1065 		IEEE80211_SEND_MGMT(ni,
1066 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1067 		break;
1068 	default:
1069 		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1070 		    wh, "shared key auth", "bad seq %d", seq);
1071 		vap->iv_stats.is_rx_bad_auth++;
1072 		return;
1073 	}
1074 	return;
1075 bad:
1076 	/*
1077 	 * Kick the state machine.  This short-circuits
1078 	 * using the mgt frame timeout to trigger the
1079 	 * state transition.
1080 	 */
1081 	if (vap->iv_state == IEEE80211_S_AUTH)
1082 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1083 		    IEEE80211_SCAN_FAIL_STATUS);
1084 }
1085 
1086 int
1087 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1088 	const struct ieee80211_frame *wh)
1089 {
1090 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1091 	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1092 	u_int len = frm[1], qosinfo;
1093 	int i;
1094 
1095 	if (len < sizeof(struct ieee80211_wme_param)-2) {
1096 		IEEE80211_DISCARD_IE(vap,
1097 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1098 		    wh, "WME", "too short, len %u", len);
1099 		return -1;
1100 	}
1101 	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1102 	qosinfo &= WME_QOSINFO_COUNT;
1103 	/* XXX do proper check for wraparound */
1104 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1105 		return 0;
1106 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1107 	for (i = 0; i < WME_NUM_AC; i++) {
1108 		struct wmeParams *wmep =
1109 			&wme->wme_wmeChanParams.cap_wmeParams[i];
1110 		/* NB: ACI not used */
1111 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1112 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1113 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1114 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1115 		wmep->wmep_txopLimit = le16dec(frm+2);
1116 		frm += 4;
1117 	}
1118 	wme->wme_wmeChanParams.cap_info = qosinfo;
1119 	return 1;
1120 #undef MS
1121 }
1122 
1123 /*
1124  * Process 11h Channel Switch Announcement (CSA) ie.  If this
1125  * is the first CSA then initiate the switch.  Otherwise we
1126  * track state and trigger completion and/or cancel of the switch.
1127  * XXX should be public for IBSS use
1128  */
1129 static void
1130 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1131 	const struct ieee80211_frame *wh)
1132 {
1133 	struct ieee80211com *ic = vap->iv_ic;
1134 	const struct ieee80211_csa_ie *csa =
1135 	    (const struct ieee80211_csa_ie *) frm;
1136 
1137 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1138 	    ("state %s", ieee80211_state_name[vap->iv_state]));
1139 
1140 	if (csa->csa_mode > 1) {
1141 		IEEE80211_DISCARD_IE(vap,
1142 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1143 		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1144 		return;
1145 	}
1146 	IEEE80211_LOCK(ic);
1147 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1148 		/*
1149 		 * Convert the channel number to a channel reference.  We
1150 		 * try first to preserve turbo attribute of the current
1151 		 * channel then fallback.  Note this will not work if the
1152 		 * CSA specifies a channel that requires a band switch (e.g.
1153 		 * 11a => 11g).  This is intentional as 11h is defined only
1154 		 * for 5GHz/11a and because the switch does not involve a
1155 		 * reassociation, protocol state (capabilities, negotated
1156 		 * rates, etc) may/will be wrong.
1157 		 */
1158 		struct ieee80211_channel *c =
1159 		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1160 			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1161 		if (c == NULL) {
1162 			c = ieee80211_find_channel_byieee(ic,
1163 			    csa->csa_newchan,
1164 			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1165 			if (c == NULL) {
1166 				IEEE80211_DISCARD_IE(vap,
1167 				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1168 				    wh, "CSA", "invalid channel %u",
1169 				    csa->csa_newchan);
1170 				goto done;
1171 			}
1172 		}
1173 #if IEEE80211_CSA_COUNT_MIN > 0
1174 		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1175 			/*
1176 			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1177 			 * reduce the risk of being redirected by a fabricated
1178 			 * CSA.  If a valid CSA is dropped we'll still get a
1179 			 * beacon miss when the AP leaves the channel so we'll
1180 			 * eventually follow to the new channel.
1181 			 *
1182 			 * NOTE: this violates the 11h spec that states that
1183 			 * count may be any value and if 0 then a switch
1184 			 * should happen asap.
1185 			 */
1186 			IEEE80211_DISCARD_IE(vap,
1187 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1188 			    wh, "CSA", "count %u too small, must be >= %u",
1189 			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1190 			goto done;
1191 		}
1192 #endif
1193 		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1194 	} else {
1195 		/*
1196 		 * Validate this ie against the initial CSA.  We require
1197 		 * mode and channel not change and the count must be
1198 		 * monotonically decreasing.  This may be pointless and
1199 		 * canceling the switch as a result may be too paranoid but
1200 		 * in the worst case if we drop out of CSA because of this
1201 		 * and the AP does move then we'll just end up taking a
1202 		 * beacon miss and scan to find the AP.
1203 		 *
1204 		 * XXX may want <= on count as we also process ProbeResp
1205 		 * frames and those may come in w/ the same count as the
1206 		 * previous beacon; but doing so leaves us open to a stuck
1207 		 * count until we add a dead-man timer
1208 		 */
1209 		if (!(csa->csa_count < ic->ic_csa_count &&
1210 		      csa->csa_mode == ic->ic_csa_mode &&
1211 		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1212 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1213 			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1214 			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1215 			    ic->ic_csa_newchan, ic->ic_csa_count,
1216 			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1217 			ieee80211_csa_cancelswitch(ic);
1218 		} else {
1219 			if (csa->csa_count <= 1)
1220 				ieee80211_csa_completeswitch(ic);
1221 			else
1222 				ic->ic_csa_count = csa->csa_count;
1223 		}
1224 	}
1225 done:
1226 	IEEE80211_UNLOCK(ic);
1227 }
1228 
1229 /*
1230  * Return non-zero if a background scan may be continued:
1231  * o bg scan is active
1232  * o no channel switch is pending
1233  * o there has not been any traffic recently
1234  *
1235  * Note we do not check if there is an administrative enable;
1236  * this is only done to start the scan.  We assume that any
1237  * change in state will be accompanied by a request to cancel
1238  * active scans which will otherwise cause this test to fail.
1239  */
1240 static __inline int
1241 contbgscan(struct ieee80211vap *vap)
1242 {
1243 	struct ieee80211com *ic = vap->iv_ic;
1244 
1245 	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1246 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1247 	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1248 	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1249 }
1250 
1251 /*
1252  * Return non-zero if a backgrond scan may be started:
1253  * o bg scanning is administratively enabled
1254  * o no channel switch is pending
1255  * o we are not boosted on a dynamic turbo channel
1256  * o there has not been a scan recently
1257  * o there has not been any traffic recently
1258  */
1259 static __inline int
1260 startbgscan(struct ieee80211vap *vap)
1261 {
1262 	struct ieee80211com *ic = vap->iv_ic;
1263 
1264 	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1265 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1266 #ifdef IEEE80211_SUPPORT_SUPERG
1267 	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1268 #endif
1269 	    ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1270 	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1271 }
1272 
1273 static void
1274 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1275     const struct ieee80211_rx_stats *rxs,
1276     int rssi, int nf)
1277 {
1278 #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1279 	struct ieee80211vap *vap = ni->ni_vap;
1280 	struct ieee80211com *ic = ni->ni_ic;
1281 	struct ieee80211_channel *rxchan = ic->ic_curchan;
1282 	struct ieee80211_frame *wh;
1283 	uint8_t *frm, *efrm;
1284 	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1285 	uint8_t rate;
1286 	int ht_state_change = 0;
1287 
1288 	wh = mtod(m0, struct ieee80211_frame *);
1289 	frm = (uint8_t *)&wh[1];
1290 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1291 	switch (subtype) {
1292 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1293 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1294 		struct ieee80211_scanparams scan;
1295 		struct ieee80211_channel *c;
1296 		/*
1297 		 * We process beacon/probe response frames:
1298 		 *    o when scanning, or
1299 		 *    o station mode when associated (to collect state
1300 		 *      updates such as 802.11g slot time)
1301 		 * Frames otherwise received are discarded.
1302 		 */
1303 		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1304 			vap->iv_stats.is_rx_mgtdiscard++;
1305 			return;
1306 		}
1307 
1308 		/* Override RX channel as appropriate */
1309 		if (rxs != NULL) {
1310 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1311 			if (c != NULL)
1312 				rxchan = c;
1313 		}
1314 
1315 		/* XXX probe response in sta mode when !scanning? */
1316 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1317 			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1318 				vap->iv_stats.is_beacon_bad++;
1319 			return;
1320 		}
1321 
1322 		/*
1323 		 * Count frame now that we know it's to be processed.
1324 		 */
1325 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1326 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1327 			IEEE80211_NODE_STAT(ni, rx_beacons);
1328 		} else
1329 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1330 		/*
1331 		 * When operating in station mode, check for state updates.
1332 		 * Be careful to ignore beacons received while doing a
1333 		 * background scan.  We consider only 11g/WMM stuff right now.
1334 		 */
1335 		if (ni->ni_associd != 0 &&
1336 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1337 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1338 			/* record tsf of last beacon */
1339 			memcpy(ni->ni_tstamp.data, scan.tstamp,
1340 				sizeof(ni->ni_tstamp));
1341 			/* count beacon frame for s/w bmiss handling */
1342 			vap->iv_swbmiss_count++;
1343 			vap->iv_bmiss_count = 0;
1344 			if (ni->ni_erp != scan.erp) {
1345 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1346 				    wh->i_addr2,
1347 				    "erp change: was 0x%x, now 0x%x",
1348 				    ni->ni_erp, scan.erp);
1349 				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1350 				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1351 					ic->ic_flags |= IEEE80211_F_USEPROT;
1352 				else
1353 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1354 				ni->ni_erp = scan.erp;
1355 				/* XXX statistic */
1356 				/* XXX driver notification */
1357 			}
1358 			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1359 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1360 				    wh->i_addr2,
1361 				    "capabilities change: was 0x%x, now 0x%x",
1362 				    ni->ni_capinfo, scan.capinfo);
1363 				/*
1364 				 * NB: we assume short preamble doesn't
1365 				 *     change dynamically
1366 				 */
1367 				ieee80211_set_shortslottime(ic,
1368 					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1369 					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1370 				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1371 					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1372 				/* XXX statistic */
1373 			}
1374 			if (scan.wme != NULL &&
1375 			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1376 			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1377 				ieee80211_wme_updateparams(vap);
1378 #ifdef IEEE80211_SUPPORT_SUPERG
1379 			if (scan.ath != NULL)
1380 				ieee80211_parse_athparams(ni, scan.ath, wh);
1381 #endif
1382 			if (scan.htcap != NULL && scan.htinfo != NULL &&
1383 			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1384 				/* XXX state changes? */
1385 				if (ieee80211_ht_updateparams(ni,
1386 				    scan.htcap, scan.htinfo))
1387 					ht_state_change = 1;
1388 			}
1389 			if (scan.quiet)
1390 				ic->ic_set_quiet(ni, scan.quiet);
1391 
1392 			if (scan.tim != NULL) {
1393 				struct ieee80211_tim_ie *tim =
1394 				    (struct ieee80211_tim_ie *) scan.tim;
1395 				/*
1396 				 * XXX Check/debug this code; see if it's about
1397 				 * the right time to force the VAP awake if we
1398 				 * receive a frame destined for us?
1399 				 */
1400 				int aid = IEEE80211_AID(ni->ni_associd);
1401 				int ix = aid / NBBY;
1402 				int min = tim->tim_bitctl &~ 1;
1403 				int max = tim->tim_len + min - 4;
1404 				int tim_ucast = 0, tim_mcast = 0;
1405 
1406 				/*
1407 				 * Only do this for unicast traffic in the TIM
1408 				 * The multicast traffic notification for
1409 				 * the scan notification stuff should occur
1410 				 * differently.
1411 				 */
1412 				if (min <= ix && ix <= max &&
1413 				     isset(tim->tim_bitmap - min, aid)) {
1414 					tim_ucast = 1;
1415 				}
1416 
1417 				/*
1418 				 * Do a separate notification
1419 				 * for the multicast bit being set.
1420 				 */
1421 				if (tim->tim_bitctl & 1) {
1422 					tim_mcast = 1;
1423 				}
1424 
1425 				/*
1426 				 * If the TIM indicates there's traffic for
1427 				 * us then get us out of STA mode powersave.
1428 				 */
1429 				if (tim_ucast == 1) {
1430 
1431 					/*
1432 					 * Wake us out of SLEEP state if we're
1433 					 * in it; and if we're doing bgscan
1434 					 * then wake us out of STA powersave.
1435 					 */
1436 					ieee80211_sta_tim_notify(vap, 1);
1437 
1438 					/*
1439 					 * This is preventing us from
1440 					 * continuing a bgscan; because it
1441 					 * tricks the contbgscan()
1442 					 * routine to think there's always
1443 					 * traffic for us.
1444 					 *
1445 					 * I think we need both an RX and
1446 					 * TX ic_lastdata field.
1447 					 */
1448 					ic->ic_lastdata = ticks;
1449 				}
1450 
1451 				ni->ni_dtim_count = tim->tim_count;
1452 				ni->ni_dtim_period = tim->tim_period;
1453 			}
1454 			if (scan.csa != NULL &&
1455 			    (vap->iv_flags & IEEE80211_F_DOTH))
1456 				ieee80211_parse_csaparams(vap, scan.csa, wh);
1457 			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1458 				/*
1459 				 * No CSA ie or 11h disabled, but a channel
1460 				 * switch is pending; drop out so we aren't
1461 				 * stuck in CSA state.  If the AP really is
1462 				 * moving we'll get a beacon miss and scan.
1463 				 */
1464 				IEEE80211_LOCK(ic);
1465 				ieee80211_csa_cancelswitch(ic);
1466 				IEEE80211_UNLOCK(ic);
1467 			}
1468 			/*
1469 			 * If scanning, pass the info to the scan module.
1470 			 * Otherwise, check if it's the right time to do
1471 			 * a background scan.  Background scanning must
1472 			 * be enabled and we must not be operating in the
1473 			 * turbo phase of dynamic turbo mode.  Then,
1474 			 * it's been a while since the last background
1475 			 * scan and if no data frames have come through
1476 			 * recently, kick off a scan.  Note that this
1477 			 * is the mechanism by which a background scan
1478 			 * is started _and_ continued each time we
1479 			 * return on-channel to receive a beacon from
1480 			 * our ap.
1481 			 */
1482 			if (ic->ic_flags & IEEE80211_F_SCAN) {
1483 				ieee80211_add_scan(vap, rxchan,
1484 				    &scan, wh, subtype, rssi, nf);
1485 			} else if (contbgscan(vap)) {
1486 				ieee80211_bg_scan(vap, 0);
1487 			} else if (startbgscan(vap)) {
1488 				vap->iv_stats.is_scan_bg++;
1489 #if 0
1490 				/* wakeup if we are sleeing */
1491 				ieee80211_set_pwrsave(vap, 0);
1492 #endif
1493 				ieee80211_bg_scan(vap, 0);
1494 			}
1495 
1496 			/*
1497 			 * Put the station to sleep if we haven't seen
1498 			 * traffic in a while.
1499 			 */
1500 			IEEE80211_LOCK(ic);
1501 			ieee80211_sta_ps_timer_check(vap);
1502 			IEEE80211_UNLOCK(ic);
1503 
1504 			/*
1505 			 * If we've had a channel width change (eg HT20<->HT40)
1506 			 * then schedule a delayed driver notification.
1507 			 */
1508 			if (ht_state_change)
1509 				ieee80211_update_chw(ic);
1510 			return;
1511 		}
1512 		/*
1513 		 * If scanning, just pass information to the scan module.
1514 		 */
1515 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1516 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1517 				/*
1518 				 * Actively scanning a channel marked passive;
1519 				 * send a probe request now that we know there
1520 				 * is 802.11 traffic present.
1521 				 *
1522 				 * XXX check if the beacon we recv'd gives
1523 				 * us what we need and suppress the probe req
1524 				 */
1525 				ieee80211_probe_curchan(vap, 1);
1526 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1527 			}
1528 			ieee80211_add_scan(vap, rxchan, &scan, wh,
1529 			    subtype, rssi, nf);
1530 			return;
1531 		}
1532 		break;
1533 	}
1534 
1535 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1536 		uint16_t algo, seq, status;
1537 		/*
1538 		 * auth frame format
1539 		 *	[2] algorithm
1540 		 *	[2] sequence
1541 		 *	[2] status
1542 		 *	[tlv*] challenge
1543 		 */
1544 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1545 		algo   = le16toh(*(uint16_t *)frm);
1546 		seq    = le16toh(*(uint16_t *)(frm + 2));
1547 		status = le16toh(*(uint16_t *)(frm + 4));
1548 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1549 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1550 
1551 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1552 			IEEE80211_DISCARD(vap,
1553 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1554 			    wh, "auth", "%s", "TKIP countermeasures enabled");
1555 			vap->iv_stats.is_rx_auth_countermeasures++;
1556 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1557 				ieee80211_send_error(ni, wh->i_addr2,
1558 					IEEE80211_FC0_SUBTYPE_AUTH,
1559 					IEEE80211_REASON_MIC_FAILURE);
1560 			}
1561 			return;
1562 		}
1563 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1564 			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1565 			    seq, status);
1566 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1567 			sta_auth_open(ni, wh, rssi, nf, seq, status);
1568 		else {
1569 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1570 			    wh, "auth", "unsupported alg %d", algo);
1571 			vap->iv_stats.is_rx_auth_unsupported++;
1572 			return;
1573 		}
1574 		break;
1575 	}
1576 
1577 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1578 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1579 		uint16_t capinfo, associd;
1580 		uint16_t status;
1581 
1582 		if (vap->iv_state != IEEE80211_S_ASSOC) {
1583 			vap->iv_stats.is_rx_mgtdiscard++;
1584 			return;
1585 		}
1586 
1587 		/*
1588 		 * asresp frame format
1589 		 *	[2] capability information
1590 		 *	[2] status
1591 		 *	[2] association ID
1592 		 *	[tlv] supported rates
1593 		 *	[tlv] extended supported rates
1594 		 *	[tlv] WME
1595 		 *	[tlv] HT capabilities
1596 		 *	[tlv] HT info
1597 		 */
1598 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1599 		ni = vap->iv_bss;
1600 		capinfo = le16toh(*(uint16_t *)frm);
1601 		frm += 2;
1602 		status = le16toh(*(uint16_t *)frm);
1603 		frm += 2;
1604 		if (status != 0) {
1605 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1606 			    wh->i_addr2, "%sassoc failed (reason %d)",
1607 			    ISREASSOC(subtype) ?  "re" : "", status);
1608 			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1609 			return;
1610 		}
1611 		associd = le16toh(*(uint16_t *)frm);
1612 		frm += 2;
1613 
1614 		rates = xrates = wme = htcap = htinfo = NULL;
1615 		while (efrm - frm > 1) {
1616 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1617 			switch (*frm) {
1618 			case IEEE80211_ELEMID_RATES:
1619 				rates = frm;
1620 				break;
1621 			case IEEE80211_ELEMID_XRATES:
1622 				xrates = frm;
1623 				break;
1624 			case IEEE80211_ELEMID_HTCAP:
1625 				htcap = frm;
1626 				break;
1627 			case IEEE80211_ELEMID_HTINFO:
1628 				htinfo = frm;
1629 				break;
1630 			case IEEE80211_ELEMID_VENDOR:
1631 				if (iswmeoui(frm))
1632 					wme = frm;
1633 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1634 					/*
1635 					 * Accept pre-draft HT ie's if the
1636 					 * standard ones have not been seen.
1637 					 */
1638 					if (ishtcapoui(frm)) {
1639 						if (htcap == NULL)
1640 							htcap = frm;
1641 					} else if (ishtinfooui(frm)) {
1642 						if (htinfo == NULL)
1643 							htinfo = frm;
1644 					}
1645 				}
1646 				/* XXX Atheros OUI support */
1647 				break;
1648 			}
1649 			frm += frm[1] + 2;
1650 		}
1651 
1652 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1653 		if (xrates != NULL)
1654 			IEEE80211_VERIFY_ELEMENT(xrates,
1655 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1656 		rate = ieee80211_setup_rates(ni, rates, xrates,
1657 				IEEE80211_F_JOIN |
1658 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1659 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1660 		if (rate & IEEE80211_RATE_BASIC) {
1661 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1662 			    wh->i_addr2,
1663 			    "%sassoc failed (rate set mismatch)",
1664 			    ISREASSOC(subtype) ?  "re" : "");
1665 			vap->iv_stats.is_rx_assoc_norate++;
1666 			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1667 			    IEEE80211_SCAN_FAIL_STATUS);
1668 			return;
1669 		}
1670 
1671 		ni->ni_capinfo = capinfo;
1672 		ni->ni_associd = associd;
1673 		if (ni->ni_jointime == 0)
1674 			ni->ni_jointime = time_uptime;
1675 		if (wme != NULL &&
1676 		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1677 			ni->ni_flags |= IEEE80211_NODE_QOS;
1678 			ieee80211_wme_updateparams(vap);
1679 		} else
1680 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1681 		/*
1682 		 * Setup HT state according to the negotiation.
1683 		 *
1684 		 * NB: shouldn't need to check if HT use is enabled but some
1685 		 *     ap's send back HT ie's even when we don't indicate we
1686 		 *     are HT capable in our AssocReq.
1687 		 */
1688 		if (htcap != NULL && htinfo != NULL &&
1689 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1690 			ieee80211_ht_node_init(ni);
1691 			ieee80211_ht_updateparams(ni, htcap, htinfo);
1692 			ieee80211_setup_htrates(ni, htcap,
1693 			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1694 			ieee80211_setup_basic_htrates(ni, htinfo);
1695 			ieee80211_node_setuptxparms(ni);
1696 			ieee80211_ratectl_node_init(ni);
1697 		}
1698 
1699 		/*
1700 		 * Always initialise FF/superg state; we can use this
1701 		 * for doing A-MSDU encapsulation as well.
1702 		 */
1703 #ifdef	IEEE80211_SUPPORT_SUPERG
1704 		ieee80211_ff_node_init(ni);
1705 #endif
1706 
1707 		/*
1708 		 * Configure state now that we are associated.
1709 		 *
1710 		 * XXX may need different/additional driver callbacks?
1711 		 */
1712 		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1713 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1714 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1715 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1716 		} else {
1717 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1718 			ic->ic_flags |= IEEE80211_F_USEBARKER;
1719 		}
1720 		ieee80211_set_shortslottime(ic,
1721 			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1722 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1723 		/*
1724 		 * Honor ERP protection.
1725 		 *
1726 		 * NB: ni_erp should zero for non-11g operation.
1727 		 */
1728 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1729 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1730 			ic->ic_flags |= IEEE80211_F_USEPROT;
1731 		else
1732 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1733 		IEEE80211_NOTE_MAC(vap,
1734 		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1735 		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1736 		    ISREASSOC(subtype) ? "re" : "",
1737 		    IEEE80211_NODE_AID(ni),
1738 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1739 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1740 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1741 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1742 		    ni->ni_flags & IEEE80211_NODE_HT ?
1743 			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1744 		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1745 		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1746 			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1747 		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1748 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1749 			", fast-frames" : "",
1750 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1751 			", turbo" : ""
1752 		);
1753 		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1754 		break;
1755 	}
1756 
1757 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1758 		uint16_t reason;
1759 
1760 		if (vap->iv_state == IEEE80211_S_SCAN) {
1761 			vap->iv_stats.is_rx_mgtdiscard++;
1762 			return;
1763 		}
1764 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1765 			/* NB: can happen when in promiscuous mode */
1766 			vap->iv_stats.is_rx_mgtdiscard++;
1767 			break;
1768 		}
1769 
1770 		/*
1771 		 * deauth frame format
1772 		 *	[2] reason
1773 		 */
1774 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1775 		reason = le16toh(*(uint16_t *)frm);
1776 
1777 		vap->iv_stats.is_rx_deauth++;
1778 		vap->iv_stats.is_rx_deauth_code = reason;
1779 		IEEE80211_NODE_STAT(ni, rx_deauth);
1780 
1781 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1782 		    "recv deauthenticate (reason: %d (%s))", reason,
1783 		    ieee80211_reason_to_string(reason));
1784 		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1785 		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1786 		break;
1787 	}
1788 
1789 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1790 		uint16_t reason;
1791 
1792 		if (vap->iv_state != IEEE80211_S_RUN &&
1793 		    vap->iv_state != IEEE80211_S_ASSOC &&
1794 		    vap->iv_state != IEEE80211_S_AUTH) {
1795 			vap->iv_stats.is_rx_mgtdiscard++;
1796 			return;
1797 		}
1798 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1799 			/* NB: can happen when in promiscuous mode */
1800 			vap->iv_stats.is_rx_mgtdiscard++;
1801 			break;
1802 		}
1803 
1804 		/*
1805 		 * disassoc frame format
1806 		 *	[2] reason
1807 		 */
1808 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1809 		reason = le16toh(*(uint16_t *)frm);
1810 
1811 		vap->iv_stats.is_rx_disassoc++;
1812 		vap->iv_stats.is_rx_disassoc_code = reason;
1813 		IEEE80211_NODE_STAT(ni, rx_disassoc);
1814 
1815 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1816 		    "recv disassociate (reason: %d (%s))", reason,
1817 		    ieee80211_reason_to_string(reason));
1818 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1819 		break;
1820 	}
1821 
1822 	case IEEE80211_FC0_SUBTYPE_ACTION:
1823 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1824 		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1825 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1826 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1827 			    wh, NULL, "%s", "not for us");
1828 			vap->iv_stats.is_rx_mgtdiscard++;
1829 		} else if (vap->iv_state != IEEE80211_S_RUN) {
1830 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1831 			    wh, NULL, "wrong state %s",
1832 			    ieee80211_state_name[vap->iv_state]);
1833 			vap->iv_stats.is_rx_mgtdiscard++;
1834 		} else {
1835 			if (ieee80211_parse_action(ni, m0) == 0)
1836 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1837 		}
1838 		break;
1839 
1840 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1841 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1842 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1843 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1844 	case IEEE80211_FC0_SUBTYPE_ATIM:
1845 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1846 		    wh, NULL, "%s", "not handled");
1847 		vap->iv_stats.is_rx_mgtdiscard++;
1848 		break;
1849 
1850 	default:
1851 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1852 		    wh, "mgt", "subtype 0x%x not handled", subtype);
1853 		vap->iv_stats.is_rx_badsubtype++;
1854 		break;
1855 	}
1856 #undef ISREASSOC
1857 }
1858 
1859 static void
1860 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1861 {
1862 	switch (subtype) {
1863 	case IEEE80211_FC0_SUBTYPE_BAR:
1864 		ieee80211_recv_bar(ni, m);
1865 		break;
1866 	}
1867 }
1868