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