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