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