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