ieee80211_sta.c revision 260444
1178354Ssam/*-
2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3178354Ssam * All rights reserved.
4178354Ssam *
5178354Ssam * Redistribution and use in source and binary forms, with or without
6178354Ssam * modification, are permitted provided that the following conditions
7178354Ssam * are met:
8178354Ssam * 1. Redistributions of source code must retain the above copyright
9178354Ssam *    notice, this list of conditions and the following disclaimer.
10178354Ssam * 2. Redistributions in binary form must reproduce the above copyright
11178354Ssam *    notice, this list of conditions and the following disclaimer in the
12178354Ssam *    documentation and/or other materials provided with the distribution.
13178354Ssam *
14178354Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15178354Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16178354Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17178354Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18178354Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19178354Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20178354Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21178354Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22178354Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23178354Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24178354Ssam */
25178354Ssam
26178354Ssam#include <sys/cdefs.h>
27178354Ssam#ifdef __FreeBSD__
28178354Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_sta.c 260444 2014-01-08 08:06:56Z kevlo $");
29178354Ssam#endif
30178354Ssam
31178354Ssam/*
32178354Ssam * IEEE 802.11 Station mode support.
33178354Ssam */
34178354Ssam#include "opt_inet.h"
35178354Ssam#include "opt_wlan.h"
36178354Ssam
37178354Ssam#include <sys/param.h>
38178354Ssam#include <sys/systm.h>
39178354Ssam#include <sys/mbuf.h>
40178354Ssam#include <sys/malloc.h>
41178354Ssam#include <sys/kernel.h>
42178354Ssam
43178354Ssam#include <sys/socket.h>
44178354Ssam#include <sys/sockio.h>
45178354Ssam#include <sys/endian.h>
46178354Ssam#include <sys/errno.h>
47178354Ssam#include <sys/proc.h>
48178354Ssam#include <sys/sysctl.h>
49178354Ssam
50178354Ssam#include <net/if.h>
51178354Ssam#include <net/if_media.h>
52178354Ssam#include <net/if_llc.h>
53227339Sadrian#include <net/if_dl.h>
54227339Sadrian#include <net/if_var.h>
55178354Ssam#include <net/ethernet.h>
56178354Ssam
57178354Ssam#include <net/bpf.h>
58178354Ssam
59178354Ssam#include <net80211/ieee80211_var.h>
60178354Ssam#include <net80211/ieee80211_sta.h>
61178354Ssam#include <net80211/ieee80211_input.h>
62190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
63190391Ssam#include <net80211/ieee80211_superg.h>
64190391Ssam#endif
65211295Sbschmidt#include <net80211/ieee80211_ratectl.h>
66244060Sadrian#include <net80211/ieee80211_sta.h>
67178354Ssam
68178354Ssam#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
69178354Ssam
70178354Ssamstatic	void sta_vattach(struct ieee80211vap *);
71178354Ssamstatic	void sta_beacon_miss(struct ieee80211vap *);
72178354Ssamstatic	int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
73192468Ssamstatic	int sta_input(struct ieee80211_node *, struct mbuf *, int, int);
74178354Ssamstatic void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
75192468Ssam	    int subtype, int rssi, int nf);
76191546Ssamstatic void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
77178354Ssam
78178354Ssamvoid
79178354Ssamieee80211_sta_attach(struct ieee80211com *ic)
80178354Ssam{
81178354Ssam	ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
82178354Ssam}
83178354Ssam
84178354Ssamvoid
85178354Ssamieee80211_sta_detach(struct ieee80211com *ic)
86178354Ssam{
87178354Ssam}
88178354Ssam
89178354Ssamstatic void
90178354Ssamsta_vdetach(struct ieee80211vap *vap)
91178354Ssam{
92178354Ssam}
93178354Ssam
94178354Ssamstatic void
95178354Ssamsta_vattach(struct ieee80211vap *vap)
96178354Ssam{
97178354Ssam	vap->iv_newstate = sta_newstate;
98178354Ssam	vap->iv_input = sta_input;
99178354Ssam	vap->iv_recv_mgmt = sta_recv_mgmt;
100191546Ssam	vap->iv_recv_ctl = sta_recv_ctl;
101178354Ssam	vap->iv_opdetach = sta_vdetach;
102178354Ssam	vap->iv_bmiss = sta_beacon_miss;
103178354Ssam}
104178354Ssam
105178354Ssam/*
106178354Ssam * Handle a beacon miss event.  The common code filters out
107178354Ssam * spurious events that can happen when scanning and/or before
108178354Ssam * reaching RUN state.
109178354Ssam */
110178354Ssamstatic void
111178354Ssamsta_beacon_miss(struct ieee80211vap *vap)
112178354Ssam{
113193439Ssam	struct ieee80211com *ic = vap->iv_ic;
114178354Ssam
115225913Sadrian	IEEE80211_LOCK_ASSERT(ic);
116225913Sadrian
117193439Ssam	KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
118193439Ssam	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
119193439Ssam	    ("wrong state %s", ieee80211_state_name[vap->iv_state]));
120178354Ssam
121193439Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
122193439Ssam	    "beacon miss, mode %s state %s\n",
123193439Ssam	    ieee80211_opmode_name[vap->iv_opmode],
124193439Ssam	    ieee80211_state_name[vap->iv_state]);
125193439Ssam
126193439Ssam	if (vap->iv_state == IEEE80211_S_CSA) {
127193439Ssam		/*
128193439Ssam		 * A Channel Switch is pending; assume we missed the
129193439Ssam		 * beacon that would've completed the process and just
130193439Ssam		 * force the switch.  If we made a mistake we'll not
131193439Ssam		 * find the AP on the new channel and fall back to a
132193439Ssam		 * normal scan.
133193439Ssam		 */
134193439Ssam		ieee80211_csa_completeswitch(ic);
135193439Ssam		return;
136193439Ssam	}
137178354Ssam	if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
138178354Ssam		/*
139178354Ssam		 * Send a directed probe req before falling back to a
140178354Ssam		 * scan; if we receive a response ic_bmiss_count will
141178354Ssam		 * be reset.  Some cards mistakenly report beacon miss
142178354Ssam		 * so this avoids the expensive scan if the ap is
143178354Ssam		 * still there.
144178354Ssam		 */
145178354Ssam		ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
146178354Ssam			vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
147178354Ssam			vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
148178354Ssam		return;
149178354Ssam	}
150205140Sweongyo
151205140Sweongyo	callout_stop(&vap->iv_swbmiss);
152178354Ssam	vap->iv_bmiss_count = 0;
153178354Ssam	vap->iv_stats.is_beacon_miss++;
154178354Ssam	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
155190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
156190402Ssam		struct ieee80211com *ic = vap->iv_ic;
157190402Ssam
158178354Ssam		/*
159178354Ssam		 * If we receive a beacon miss interrupt when using
160178354Ssam		 * dynamic turbo, attempt to switch modes before
161178354Ssam		 * reassociating.
162178354Ssam		 */
163178354Ssam		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
164178354Ssam			ieee80211_dturbo_switch(vap,
165178354Ssam			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
166190391Ssam#endif
167178354Ssam		/*
168178354Ssam		 * Try to reassociate before scanning for a new ap.
169178354Ssam		 */
170178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
171178354Ssam	} else {
172178354Ssam		/*
173178354Ssam		 * Somebody else is controlling state changes (e.g.
174178354Ssam		 * a user-mode app) don't do anything that would
175178354Ssam		 * confuse them; just drop into scan mode so they'll
176178354Ssam		 * notified of the state change and given control.
177178354Ssam		 */
178178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
179178354Ssam	}
180178354Ssam}
181178354Ssam
182178354Ssam/*
183178354Ssam * Handle deauth with reason.  We retry only for
184178354Ssam * the cases where we might succeed.  Otherwise
185178354Ssam * we downgrade the ap and scan.
186178354Ssam */
187178354Ssamstatic void
188178354Ssamsta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
189178354Ssam{
190178354Ssam	switch (reason) {
191178354Ssam	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
192178354Ssam	case IEEE80211_STATUS_TIMEOUT:
193178354Ssam	case IEEE80211_REASON_ASSOC_EXPIRE:
194178354Ssam	case IEEE80211_REASON_NOT_AUTHED:
195178354Ssam	case IEEE80211_REASON_NOT_ASSOCED:
196178354Ssam	case IEEE80211_REASON_ASSOC_LEAVE:
197178354Ssam	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
198178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
199178354Ssam		break;
200178354Ssam	default:
201178354Ssam		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
202178354Ssam		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
203178354Ssam			ieee80211_check_scan_current(vap);
204178354Ssam		break;
205178354Ssam	}
206178354Ssam}
207178354Ssam
208178354Ssam/*
209178354Ssam * IEEE80211_M_STA vap state machine handler.
210178354Ssam * This routine handles the main states in the 802.11 protocol.
211178354Ssam */
212178354Ssamstatic int
213178354Ssamsta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
214178354Ssam{
215178354Ssam	struct ieee80211com *ic = vap->iv_ic;
216178354Ssam	struct ieee80211_node *ni;
217178354Ssam	enum ieee80211_state ostate;
218178354Ssam
219178354Ssam	IEEE80211_LOCK_ASSERT(ic);
220178354Ssam
221178354Ssam	ostate = vap->iv_state;
222178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
223178354Ssam	    __func__, ieee80211_state_name[ostate],
224178354Ssam	    ieee80211_state_name[nstate], arg);
225178354Ssam	vap->iv_state = nstate;			/* state transition */
226178354Ssam	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
227178354Ssam	if (ostate != IEEE80211_S_SCAN)
228178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
229178354Ssam	ni = vap->iv_bss;			/* NB: no reference held */
230178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
231178354Ssam		callout_stop(&vap->iv_swbmiss);
232178354Ssam	switch (nstate) {
233178354Ssam	case IEEE80211_S_INIT:
234178354Ssam		switch (ostate) {
235178354Ssam		case IEEE80211_S_SLEEP:
236178354Ssam			/* XXX wakeup */
237178354Ssam		case IEEE80211_S_RUN:
238178354Ssam			IEEE80211_SEND_MGMT(ni,
239178354Ssam			    IEEE80211_FC0_SUBTYPE_DISASSOC,
240178354Ssam			    IEEE80211_REASON_ASSOC_LEAVE);
241178354Ssam			ieee80211_sta_leave(ni);
242178354Ssam			break;
243178354Ssam		case IEEE80211_S_ASSOC:
244178354Ssam			IEEE80211_SEND_MGMT(ni,
245178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
246178354Ssam			    IEEE80211_REASON_AUTH_LEAVE);
247178354Ssam			break;
248178354Ssam		case IEEE80211_S_SCAN:
249178354Ssam			ieee80211_cancel_scan(vap);
250178354Ssam			break;
251178354Ssam		default:
252178354Ssam			goto invalid;
253178354Ssam		}
254178354Ssam		if (ostate != IEEE80211_S_INIT) {
255178354Ssam			/* NB: optimize INIT -> INIT case */
256178354Ssam			ieee80211_reset_bss(vap);
257178354Ssam		}
258178354Ssam		if (vap->iv_auth->ia_detach != NULL)
259178354Ssam			vap->iv_auth->ia_detach(vap);
260178354Ssam		break;
261178354Ssam	case IEEE80211_S_SCAN:
262178354Ssam		switch (ostate) {
263178354Ssam		case IEEE80211_S_INIT:
264178354Ssam			/*
265178354Ssam			 * Initiate a scan.  We can come here as a result
266178354Ssam			 * of an IEEE80211_IOC_SCAN_REQ too in which case
267178354Ssam			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
268178354Ssam			 * and the scan request parameters will be present
269178354Ssam			 * in iv_scanreq.  Otherwise we do the default.
270178354Ssam			 */
271178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
272178354Ssam				ieee80211_check_scan(vap,
273178354Ssam				    vap->iv_scanreq_flags,
274178354Ssam				    vap->iv_scanreq_duration,
275178354Ssam				    vap->iv_scanreq_mindwell,
276178354Ssam				    vap->iv_scanreq_maxdwell,
277178354Ssam				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
278178354Ssam				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
279178354Ssam			} else
280178354Ssam				ieee80211_check_scan_current(vap);
281178354Ssam			break;
282178354Ssam		case IEEE80211_S_SCAN:
283178354Ssam		case IEEE80211_S_AUTH:
284178354Ssam		case IEEE80211_S_ASSOC:
285178354Ssam			/*
286178354Ssam			 * These can happen either because of a timeout
287178354Ssam			 * on an assoc/auth response or because of a
288178354Ssam			 * change in state that requires a reset.  For
289178354Ssam			 * the former we're called with a non-zero arg
290178354Ssam			 * that is the cause for the failure; pass this
291178354Ssam			 * to the scan code so it can update state.
292178354Ssam			 * Otherwise trigger a new scan unless we're in
293178354Ssam			 * manual roaming mode in which case an application
294178354Ssam			 * must issue an explicit scan request.
295178354Ssam			 */
296178354Ssam			if (arg != 0)
297178354Ssam				ieee80211_scan_assoc_fail(vap,
298178354Ssam					vap->iv_bss->ni_macaddr, arg);
299178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
300178354Ssam				ieee80211_check_scan_current(vap);
301178354Ssam			break;
302178354Ssam		case IEEE80211_S_RUN:		/* beacon miss */
303178354Ssam			/*
304178354Ssam			 * Beacon miss.  Notify user space and if not
305178354Ssam			 * under control of a user application (roaming
306178354Ssam			 * manual) kick off a scan to re-connect.
307178354Ssam			 */
308178354Ssam			ieee80211_sta_leave(ni);
309178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
310178354Ssam				ieee80211_check_scan_current(vap);
311178354Ssam			break;
312178354Ssam		default:
313178354Ssam			goto invalid;
314178354Ssam		}
315178354Ssam		break;
316178354Ssam	case IEEE80211_S_AUTH:
317178354Ssam		switch (ostate) {
318178354Ssam		case IEEE80211_S_INIT:
319178354Ssam		case IEEE80211_S_SCAN:
320178354Ssam			IEEE80211_SEND_MGMT(ni,
321178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
322178354Ssam			break;
323178354Ssam		case IEEE80211_S_AUTH:
324178354Ssam		case IEEE80211_S_ASSOC:
325178354Ssam			switch (arg & 0xff) {
326178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
327178354Ssam				/* ??? */
328178354Ssam				IEEE80211_SEND_MGMT(ni,
329178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
330178354Ssam				break;
331178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
332178354Ssam				sta_authretry(vap, ni, arg>>8);
333178354Ssam				break;
334178354Ssam			}
335178354Ssam			break;
336178354Ssam		case IEEE80211_S_RUN:
337178354Ssam			switch (arg & 0xff) {
338178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
339178354Ssam				IEEE80211_SEND_MGMT(ni,
340178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
341178354Ssam				vap->iv_state = ostate;	/* stay RUN */
342178354Ssam				break;
343178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
344178354Ssam				ieee80211_sta_leave(ni);
345178354Ssam				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
346178354Ssam					/* try to reauth */
347178354Ssam					IEEE80211_SEND_MGMT(ni,
348178354Ssam					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
349178354Ssam				}
350178354Ssam				break;
351178354Ssam			}
352178354Ssam			break;
353178354Ssam		default:
354178354Ssam			goto invalid;
355178354Ssam		}
356178354Ssam		break;
357178354Ssam	case IEEE80211_S_ASSOC:
358178354Ssam		switch (ostate) {
359178354Ssam		case IEEE80211_S_AUTH:
360178354Ssam		case IEEE80211_S_ASSOC:
361178354Ssam			IEEE80211_SEND_MGMT(ni,
362178354Ssam			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
363178354Ssam			break;
364178354Ssam		case IEEE80211_S_SLEEP:		/* cannot happen */
365178354Ssam		case IEEE80211_S_RUN:
366178354Ssam			ieee80211_sta_leave(ni);
367178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
368178354Ssam				IEEE80211_SEND_MGMT(ni, arg ?
369178354Ssam				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
370178354Ssam				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
371178354Ssam			}
372178354Ssam			break;
373178354Ssam		default:
374178354Ssam			goto invalid;
375178354Ssam		}
376178354Ssam		break;
377178354Ssam	case IEEE80211_S_RUN:
378178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA) {
379178354Ssam			/* XXX validate prerequisites */
380178354Ssam		}
381178354Ssam		switch (ostate) {
382178354Ssam		case IEEE80211_S_RUN:
383193439Ssam		case IEEE80211_S_CSA:
384178354Ssam			break;
385178354Ssam		case IEEE80211_S_AUTH:		/* when join is done in fw */
386178354Ssam		case IEEE80211_S_ASSOC:
387178354Ssam#ifdef IEEE80211_DEBUG
388178354Ssam			if (ieee80211_msg_debug(vap)) {
389178354Ssam				ieee80211_note(vap, "%s with %s ssid ",
390178354Ssam				    (vap->iv_opmode == IEEE80211_M_STA ?
391178354Ssam				    "associated" : "synchronized"),
392178354Ssam				    ether_sprintf(ni->ni_bssid));
393178354Ssam				ieee80211_print_essid(vap->iv_bss->ni_essid,
394178354Ssam				    ni->ni_esslen);
395178354Ssam				/* XXX MCS/HT */
396178354Ssam				printf(" channel %d start %uMb\n",
397178354Ssam				    ieee80211_chan2ieee(ic, ic->ic_curchan),
398178354Ssam				    IEEE80211_RATE2MBS(ni->ni_txrate));
399178354Ssam			}
400178354Ssam#endif
401178354Ssam			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
402178354Ssam			ieee80211_notify_node_join(ni,
403178354Ssam			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
404178354Ssam			break;
405178354Ssam		case IEEE80211_S_SLEEP:
406241138Sadrian			vap->iv_sta_ps(vap, 0);
407178354Ssam			break;
408178354Ssam		default:
409178354Ssam			goto invalid;
410178354Ssam		}
411178354Ssam		ieee80211_sync_curchan(ic);
412178354Ssam		if (ostate != IEEE80211_S_RUN &&
413178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
414178354Ssam			/*
415178354Ssam			 * Start s/w beacon miss timer for devices w/o
416178354Ssam			 * hardware support.  We fudge a bit here since
417178354Ssam			 * we're doing this in software.
418178354Ssam			 */
419178354Ssam			vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
420178354Ssam				2 * vap->iv_bmissthreshold * ni->ni_intval);
421178354Ssam			vap->iv_swbmiss_count = 0;
422178354Ssam			callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
423178354Ssam				ieee80211_swbmiss, vap);
424178354Ssam		}
425178354Ssam		/*
426178354Ssam		 * When 802.1x is not in use mark the port authorized
427178354Ssam		 * at this point so traffic can flow.
428178354Ssam		 */
429178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
430178354Ssam			ieee80211_node_authorize(ni);
431184345Ssam		/*
432184345Ssam		 * Fake association when joining an existing bss.
433184345Ssam		 */
434184345Ssam		if (ic->ic_newassoc != NULL)
435184345Ssam			ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN);
436178354Ssam		break;
437193439Ssam	case IEEE80211_S_CSA:
438193439Ssam		if (ostate != IEEE80211_S_RUN)
439193439Ssam			goto invalid;
440193439Ssam		break;
441178354Ssam	case IEEE80211_S_SLEEP:
442241138Sadrian		vap->iv_sta_ps(vap, 1);
443178354Ssam		break;
444178354Ssam	default:
445178354Ssam	invalid:
446184268Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
447184268Ssam		    "%s: unexpected state transition %s -> %s\n", __func__,
448178354Ssam		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
449178354Ssam		break;
450178354Ssam	}
451178354Ssam	return 0;
452178354Ssam}
453178354Ssam
454178354Ssam/*
455178354Ssam * Return non-zero if the frame is an echo of a multicast
456178354Ssam * frame sent by ourself.  The dir is known to be DSTODS.
457178354Ssam */
458178354Ssamstatic __inline int
459178354Ssamisdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
460178354Ssam{
461178354Ssam#define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
462178354Ssam#define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
463178354Ssam	const uint8_t *sa;
464178354Ssam
465178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
466178354Ssam
467178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
468178354Ssam		return 0;
469178354Ssam	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
470178354Ssam	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
471178354Ssam#undef WH4
472178354Ssam#undef QWH4
473178354Ssam}
474178354Ssam
475178354Ssam/*
476178354Ssam * Return non-zero if the frame is an echo of a multicast
477178354Ssam * frame sent by ourself.  The dir is known to be FROMDS.
478178354Ssam */
479178354Ssamstatic __inline int
480178354Ssamisfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
481178354Ssam{
482178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
483178354Ssam
484178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
485178354Ssam		return 0;
486178354Ssam	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
487178354Ssam}
488178354Ssam
489178354Ssam/*
490178354Ssam * Decide if a received management frame should be
491178354Ssam * printed when debugging is enabled.  This filters some
492178354Ssam * of the less interesting frames that come frequently
493178354Ssam * (e.g. beacons).
494178354Ssam */
495178354Ssamstatic __inline int
496178354Ssamdoprint(struct ieee80211vap *vap, int subtype)
497178354Ssam{
498178354Ssam	switch (subtype) {
499178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
500178354Ssam		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
501178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
502178354Ssam		return 0;
503178354Ssam	}
504178354Ssam	return 1;
505178354Ssam}
506178354Ssam
507178354Ssam/*
508178354Ssam * Process a received frame.  The node associated with the sender
509178354Ssam * should be supplied.  If nothing was found in the node table then
510178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
511178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
512178354Ssam * during AP scanning to select a AP to associate with; it can have
513178354Ssam * any units so long as values have consistent units and higher values
514178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
515178354Ssam * by the 802.11 layer.
516178354Ssam */
517178354Ssamstatic int
518192468Ssamsta_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
519178354Ssam{
520178354Ssam#define	HAS_SEQ(type)	((type & 0x4) == 0)
521178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
522178354Ssam	struct ieee80211com *ic = ni->ni_ic;
523178354Ssam	struct ifnet *ifp = vap->iv_ifp;
524178354Ssam	struct ieee80211_frame *wh;
525178354Ssam	struct ieee80211_key *key;
526178354Ssam	struct ether_header *eh;
527203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
528178354Ssam	uint8_t dir, type, subtype, qos;
529178354Ssam	uint8_t *bssid;
530178354Ssam	uint16_t rxseq;
531178354Ssam
532183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
533178354Ssam		/*
534178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
535183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
536183247Ssam		 * here but were received out of order and been held on
537183247Ssam		 * the reorder queue.  When resubmitted they are marked
538183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
539183247Ssam		 * the normal processing.
540178354Ssam		 */
541178354Ssam		wh = mtod(m, struct ieee80211_frame *);
542178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
543178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
544178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
545178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
546178354Ssam		goto resubmit_ampdu;
547178354Ssam	}
548178354Ssam
549178354Ssam	KASSERT(ni != NULL, ("null node"));
550178354Ssam	ni->ni_inact = ni->ni_inact_reload;
551178354Ssam
552178354Ssam	type = -1;			/* undefined */
553178354Ssam
554178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
555178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
556178354Ssam		    ni->ni_macaddr, NULL,
557178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
558178354Ssam		vap->iv_stats.is_rx_tooshort++;
559178354Ssam		goto out;
560178354Ssam	}
561178354Ssam	/*
562178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
563178354Ssam	 * frame format but don't reference fields past outside
564178354Ssam	 * ieee80211_frame_min w/o first validating the data is
565178354Ssam	 * present.
566178354Ssam	 */
567178354Ssam	wh = mtod(m, struct ieee80211_frame *);
568178354Ssam
569178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
570178354Ssam	    IEEE80211_FC0_VERSION_0) {
571178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
572191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
573191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
574178354Ssam		vap->iv_stats.is_rx_badversion++;
575178354Ssam		goto err;
576178354Ssam	}
577178354Ssam
578178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
579178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
580178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
581178354Ssam	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
582178354Ssam		bssid = wh->i_addr2;
583178354Ssam		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
584178354Ssam			/* not interested in */
585178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
586178354Ssam			    bssid, NULL, "%s", "not to bss");
587178354Ssam			vap->iv_stats.is_rx_wrongbss++;
588178354Ssam			goto out;
589178354Ssam		}
590227338Sadrian
591227338Sadrian		/*
592227338Sadrian		 * Some devices may be in a promiscuous mode
593227338Sadrian		 * where they receive frames for multiple station
594227338Sadrian		 * addresses.
595227338Sadrian		 *
596227338Sadrian		 * If we receive a data frame that isn't
597227338Sadrian		 * destined to our VAP MAC, drop it.
598227338Sadrian		 *
599227338Sadrian		 * XXX TODO: This is only enforced when not scanning;
600227338Sadrian		 * XXX it assumes a software-driven scan will put the NIC
601227338Sadrian		 * XXX into a "no data frames" mode before setting this
602227338Sadrian		 * XXX flag. Otherwise it may be possible that we'll still
603227338Sadrian		 * XXX process data frames whilst scanning.
604227338Sadrian		 */
605227338Sadrian		if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
606227338Sadrian		    && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
607227338Sadrian			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
608227338Sadrian			    bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
609227338Sadrian			    IF_LLADDR(ifp), ":", wh->i_addr1, ":");
610227338Sadrian			vap->iv_stats.is_rx_wrongbss++;
611227338Sadrian			goto out;
612227338Sadrian		}
613227338Sadrian
614178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
615192468Ssam		ni->ni_noise = nf;
616209022Savatar		if (HAS_SEQ(type) && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
617178354Ssam			uint8_t tid = ieee80211_gettid(wh);
618178354Ssam			if (IEEE80211_QOS_HAS_SEQ(wh) &&
619178354Ssam			    TID_TO_WME_AC(tid) >= WME_AC_VI)
620178354Ssam				ic->ic_wme.wme_hipri_traffic++;
621178354Ssam			rxseq = le16toh(*(uint16_t *)wh->i_seq);
622221418Sadrian			if (! ieee80211_check_rxseq(ni, wh)) {
623178354Ssam				/* duplicate, discard */
624178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
625178354Ssam				    bssid, "duplicate",
626178354Ssam				    "seqno <%u,%u> fragno <%u,%u> tid %u",
627178354Ssam				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
628178354Ssam				    ni->ni_rxseqs[tid] >>
629178354Ssam					IEEE80211_SEQ_SEQ_SHIFT,
630178354Ssam				    rxseq & IEEE80211_SEQ_FRAG_MASK,
631178354Ssam				    ni->ni_rxseqs[tid] &
632178354Ssam					IEEE80211_SEQ_FRAG_MASK,
633178354Ssam				    tid);
634178354Ssam				vap->iv_stats.is_rx_dup++;
635178354Ssam				IEEE80211_NODE_STAT(ni, rx_dup);
636178354Ssam				goto out;
637178354Ssam			}
638178354Ssam			ni->ni_rxseqs[tid] = rxseq;
639178354Ssam		}
640178354Ssam	}
641178354Ssam
642178354Ssam	switch (type) {
643178354Ssam	case IEEE80211_FC0_TYPE_DATA:
644178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
645178354Ssam		if (m->m_len < hdrspace &&
646178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
647178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
648178354Ssam			    ni->ni_macaddr, NULL,
649178354Ssam			    "data too short: expecting %u", hdrspace);
650178354Ssam			vap->iv_stats.is_rx_tooshort++;
651178354Ssam			goto out;		/* XXX */
652178354Ssam		}
653178354Ssam		/*
654183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
655183247Ssam		 * processed directly then ieee80211_ampdu_reorder
656178354Ssam		 * will return 0; otherwise it has consumed the mbuf
657178354Ssam		 * and we should do nothing more with it.
658178354Ssam		 */
659183247Ssam		if ((m->m_flags & M_AMPDU) &&
660178354Ssam		    (dir == IEEE80211_FC1_DIR_FROMDS ||
661178354Ssam		     dir == IEEE80211_FC1_DIR_DSTODS) &&
662178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
663178354Ssam			m = NULL;
664178354Ssam			goto out;
665178354Ssam		}
666178354Ssam	resubmit_ampdu:
667178354Ssam		if (dir == IEEE80211_FC1_DIR_FROMDS) {
668178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
669178354Ssam			    isfromds_mcastecho(vap, wh)) {
670178354Ssam				/*
671178354Ssam				 * In IEEE802.11 network, multicast
672178354Ssam				 * packets sent from "me" are broadcast
673178354Ssam				 * from the AP; silently discard for
674178354Ssam				 * SIMPLEX interface.
675178354Ssam				 */
676178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
677178354Ssam				    wh, "data", "%s", "multicast echo");
678178354Ssam				vap->iv_stats.is_rx_mcastecho++;
679178354Ssam				goto out;
680178354Ssam			}
681178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
682178354Ssam			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
683178354Ssam				/*
684178354Ssam				 * DWDS sta's must drop 3-address mcast frames
685178354Ssam				 * as they will be sent separately as a 4-addr
686178354Ssam				 * frame.  Accepting the 3-addr frame will
687178354Ssam				 * confuse the bridge into thinking the sending
688178354Ssam				 * sta is located at the end of WDS link.
689178354Ssam				 */
690178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
691178354Ssam				    "3-address data", "%s", "DWDS enabled");
692178354Ssam				vap->iv_stats.is_rx_mcastecho++;
693178354Ssam				goto out;
694178354Ssam			}
695178354Ssam		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
696178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
697178354Ssam				IEEE80211_DISCARD(vap,
698178354Ssam				    IEEE80211_MSG_INPUT, wh, "4-address data",
699178354Ssam				    "%s", "DWDS not enabled");
700178354Ssam				vap->iv_stats.is_rx_wrongdir++;
701178354Ssam				goto out;
702178354Ssam			}
703178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
704178354Ssam			    isdstods_mcastecho(vap, wh)) {
705178354Ssam				/*
706178354Ssam				 * In IEEE802.11 network, multicast
707178354Ssam				 * packets sent from "me" are broadcast
708178354Ssam				 * from the AP; silently discard for
709178354Ssam				 * SIMPLEX interface.
710178354Ssam				 */
711178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
712178354Ssam				    "4-address data", "%s", "multicast echo");
713178354Ssam				vap->iv_stats.is_rx_mcastecho++;
714178354Ssam				goto out;
715178354Ssam			}
716178354Ssam		} else {
717178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
718178354Ssam			    "data", "incorrect dir 0x%x", dir);
719178354Ssam			vap->iv_stats.is_rx_wrongdir++;
720178354Ssam			goto out;
721178354Ssam		}
722178354Ssam
723178354Ssam		/*
724178354Ssam		 * Handle privacy requirements.  Note that we
725178354Ssam		 * must not be preempted from here until after
726178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
727178354Ssam		 * otherwise we may violate assumptions in the
728178354Ssam		 * crypto cipher modules used to do delayed update
729178354Ssam		 * of replay sequence numbers.
730178354Ssam		 */
731260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
732178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
733178354Ssam				/*
734178354Ssam				 * Discard encrypted frames when privacy is off.
735178354Ssam				 */
736178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
737178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
738178354Ssam				vap->iv_stats.is_rx_noprivacy++;
739178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
740178354Ssam				goto out;
741178354Ssam			}
742178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
743178354Ssam			if (key == NULL) {
744178354Ssam				/* NB: stats+msgs handled in crypto_decap */
745178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
746178354Ssam				goto out;
747178354Ssam			}
748178354Ssam			wh = mtod(m, struct ieee80211_frame *);
749260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
750178354Ssam		} else {
751178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
752178354Ssam			key = NULL;
753178354Ssam		}
754178354Ssam
755178354Ssam		/*
756178354Ssam		 * Save QoS bits for use below--before we strip the header.
757178354Ssam		 */
758178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
759178354Ssam			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
760178354Ssam			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
761178354Ssam			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
762178354Ssam		} else
763178354Ssam			qos = 0;
764178354Ssam
765178354Ssam		/*
766178354Ssam		 * Next up, any fragmentation.
767178354Ssam		 */
768178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
769178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
770178354Ssam			if (m == NULL) {
771178354Ssam				/* Fragment dropped or frame not complete yet */
772178354Ssam				goto out;
773178354Ssam			}
774178354Ssam		}
775178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
776178354Ssam
777178354Ssam		/*
778178354Ssam		 * Next strip any MSDU crypto bits.
779178354Ssam		 */
780178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
781178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
782178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
783178354Ssam			vap->iv_stats.is_rx_demicfail++;
784178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
785178354Ssam			goto out;
786178354Ssam		}
787178354Ssam
788178354Ssam		/* copy to listener after decrypt */
789192468Ssam		if (ieee80211_radiotap_active_vap(vap))
790202967Srpaulo			ieee80211_radiotap_rx(vap, m);
791178354Ssam		need_tap = 0;
792178354Ssam
793178354Ssam		/*
794178354Ssam		 * Finally, strip the 802.11 header.
795178354Ssam		 */
796178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
797178354Ssam		if (m == NULL) {
798178354Ssam			/* XXX mask bit to check for both */
799178354Ssam			/* don't count Null data frames as errors */
800178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
801178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
802178354Ssam				goto out;
803178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
804178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
805178354Ssam			vap->iv_stats.is_rx_decap++;
806178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
807178354Ssam			goto err;
808178354Ssam		}
809178354Ssam		eh = mtod(m, struct ether_header *);
810178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
811178354Ssam			/*
812178354Ssam			 * Deny any non-PAE frames received prior to
813178354Ssam			 * authorization.  For open/shared-key
814178354Ssam			 * authentication the port is mark authorized
815178354Ssam			 * after authentication completes.  For 802.1x
816178354Ssam			 * the port is not marked authorized by the
817178354Ssam			 * authenticator until the handshake has completed.
818178354Ssam			 */
819178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
820178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
821178354Ssam				    eh->ether_shost, "data",
822178354Ssam				    "unauthorized port: ether type 0x%x len %u",
823178354Ssam				    eh->ether_type, m->m_pkthdr.len);
824178354Ssam				vap->iv_stats.is_rx_unauth++;
825178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
826178354Ssam				goto err;
827178354Ssam			}
828178354Ssam		} else {
829178354Ssam			/*
830178354Ssam			 * When denying unencrypted frames, discard
831178354Ssam			 * any non-PAE frames received without encryption.
832178354Ssam			 */
833178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
834178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
835178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
836178354Ssam				/*
837178354Ssam				 * Drop unencrypted frames.
838178354Ssam				 */
839178354Ssam				vap->iv_stats.is_rx_unencrypted++;
840178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
841178354Ssam				goto out;
842178354Ssam			}
843178354Ssam		}
844178354Ssam		/* XXX require HT? */
845178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
846178354Ssam			m = ieee80211_decap_amsdu(ni, m);
847178354Ssam			if (m == NULL)
848178354Ssam				return IEEE80211_FC0_TYPE_DATA;
849190391Ssam		} else {
850190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
851190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
852190391Ssam			if (m == NULL)
853178354Ssam				return IEEE80211_FC0_TYPE_DATA;
854190391Ssam#endif
855178354Ssam		}
856178354Ssam		ieee80211_deliver_data(vap, ni, m);
857178354Ssam		return IEEE80211_FC0_TYPE_DATA;
858178354Ssam
859178354Ssam	case IEEE80211_FC0_TYPE_MGT:
860178354Ssam		vap->iv_stats.is_rx_mgmt++;
861178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
862178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
863178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
864178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
865178354Ssam			vap->iv_stats.is_rx_wrongdir++;
866178354Ssam			goto err;
867178354Ssam		}
868178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
869178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
870178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
871178354Ssam			    m->m_pkthdr.len);
872178354Ssam			vap->iv_stats.is_rx_tooshort++;
873178354Ssam			goto out;
874178354Ssam		}
875178354Ssam#ifdef IEEE80211_DEBUG
876178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
877178354Ssam		    ieee80211_msg_dumppkts(vap)) {
878178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
879178354Ssam			    ieee80211_mgt_subtype_name[subtype >>
880178354Ssam				IEEE80211_FC0_SUBTYPE_SHIFT],
881178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
882178354Ssam		}
883178354Ssam#endif
884260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
885178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
886178354Ssam				/*
887178354Ssam				 * Only shared key auth frames with a challenge
888178354Ssam				 * should be encrypted, discard all others.
889178354Ssam				 */
890178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
891178354Ssam				    wh, ieee80211_mgt_subtype_name[subtype >>
892178354Ssam					IEEE80211_FC0_SUBTYPE_SHIFT],
893178354Ssam				    "%s", "WEP set but not permitted");
894178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
895178354Ssam				goto out;
896178354Ssam			}
897178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
898178354Ssam				/*
899178354Ssam				 * Discard encrypted frames when privacy is off.
900178354Ssam				 */
901178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
902178354Ssam				    wh, "mgt", "%s", "WEP set but PRIVACY off");
903178354Ssam				vap->iv_stats.is_rx_noprivacy++;
904178354Ssam				goto out;
905178354Ssam			}
906178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
907178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
908178354Ssam			if (key == NULL) {
909178354Ssam				/* NB: stats+msgs handled in crypto_decap */
910178354Ssam				goto out;
911178354Ssam			}
912178354Ssam			wh = mtod(m, struct ieee80211_frame *);
913260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
914178354Ssam		}
915192468Ssam		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
916191534Ssam		goto out;
917178354Ssam
918178354Ssam	case IEEE80211_FC0_TYPE_CTL:
919178354Ssam		vap->iv_stats.is_rx_ctl++;
920178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
921191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
922178354Ssam		goto out;
923191549Ssam
924178354Ssam	default:
925178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
926178354Ssam		    wh, NULL, "bad frame type 0x%x", type);
927178354Ssam		/* should not come here */
928178354Ssam		break;
929178354Ssam	}
930178354Ssamerr:
931178354Ssam	ifp->if_ierrors++;
932178354Ssamout:
933178354Ssam	if (m != NULL) {
934192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
935192468Ssam			ieee80211_radiotap_rx(vap, m);
936178354Ssam		m_freem(m);
937178354Ssam	}
938178354Ssam	return type;
939178354Ssam}
940178354Ssam
941178354Ssamstatic void
942178354Ssamsta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
943192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
944178354Ssam{
945178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
946178354Ssam
947178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
948178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
949178354Ssam		    ni->ni_macaddr, "open auth",
950178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
951178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
952178354Ssam		return;
953178354Ssam	}
954178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH ||
955178354Ssam	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
956178354Ssam		vap->iv_stats.is_rx_bad_auth++;
957178354Ssam		return;
958178354Ssam	}
959178354Ssam	if (status != 0) {
960178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
961178354Ssam		    ni, "open auth failed (reason %d)", status);
962178354Ssam		vap->iv_stats.is_rx_auth_fail++;
963178354Ssam		vap->iv_stats.is_rx_authfail_code = status;
964178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
965178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
966178354Ssam	} else
967178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
968178354Ssam}
969178354Ssam
970178354Ssamstatic void
971178354Ssamsta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
972192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
973178354Ssam    uint16_t seq, uint16_t status)
974178354Ssam{
975178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
976178354Ssam	uint8_t *challenge;
977178354Ssam	int estatus;
978178354Ssam
979178354Ssam	/*
980178354Ssam	 * NB: this can happen as we allow pre-shared key
981178354Ssam	 * authentication to be enabled w/o wep being turned
982178354Ssam	 * on so that configuration of these can be done
983178354Ssam	 * in any order.  It may be better to enforce the
984178354Ssam	 * ordering in which case this check would just be
985178354Ssam	 * for sanity/consistency.
986178354Ssam	 */
987178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
988178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
989178354Ssam		    ni->ni_macaddr, "shared key auth",
990178354Ssam		    "%s", " PRIVACY is disabled");
991178354Ssam		estatus = IEEE80211_STATUS_ALG;
992178354Ssam		goto bad;
993178354Ssam	}
994178354Ssam	/*
995178354Ssam	 * Pre-shared key authentication is evil; accept
996178354Ssam	 * it only if explicitly configured (it is supported
997178354Ssam	 * mainly for compatibility with clients like OS X).
998178354Ssam	 */
999178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1000178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1001178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1002178354Ssam		    ni->ni_macaddr, "shared key auth",
1003178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1004178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1005178354Ssam		estatus = IEEE80211_STATUS_ALG;
1006178354Ssam		goto bad;
1007178354Ssam	}
1008178354Ssam
1009178354Ssam	challenge = NULL;
1010178354Ssam	if (frm + 1 < efrm) {
1011178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1012178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1013178354Ssam			    ni->ni_macaddr, "shared key auth",
1014178354Ssam			    "ie %d/%d too long",
1015178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1016178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1017178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1018178354Ssam			goto bad;
1019178354Ssam		}
1020178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1021178354Ssam			challenge = frm;
1022178354Ssam		frm += frm[1] + 2;
1023178354Ssam	}
1024178354Ssam	switch (seq) {
1025178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1026178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1027178354Ssam		if (challenge == NULL) {
1028178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1029178354Ssam			    ni->ni_macaddr, "shared key auth",
1030178354Ssam			    "%s", "no challenge");
1031178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1032178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1033178354Ssam			goto bad;
1034178354Ssam		}
1035178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1036178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1037178354Ssam			    ni->ni_macaddr, "shared key auth",
1038178354Ssam			    "bad challenge len %d", challenge[1]);
1039178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1040178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1041178354Ssam			goto bad;
1042178354Ssam		}
1043178354Ssam	default:
1044178354Ssam		break;
1045178354Ssam	}
1046178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH)
1047178354Ssam		return;
1048178354Ssam	switch (seq) {
1049178354Ssam	case IEEE80211_AUTH_SHARED_PASS:
1050178354Ssam		if (ni->ni_challenge != NULL) {
1051186302Ssam			free(ni->ni_challenge, M_80211_NODE);
1052178354Ssam			ni->ni_challenge = NULL;
1053178354Ssam		}
1054178354Ssam		if (status != 0) {
1055178354Ssam			IEEE80211_NOTE_FRAME(vap,
1056178354Ssam			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1057178354Ssam			    "shared key auth failed (reason %d)", status);
1058178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1059178354Ssam			vap->iv_stats.is_rx_authfail_code = status;
1060178354Ssam			return;
1061178354Ssam		}
1062178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1063178354Ssam		break;
1064178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1065178354Ssam		if (!ieee80211_alloc_challenge(ni))
1066178354Ssam			return;
1067178354Ssam		/* XXX could optimize by passing recvd challenge */
1068178354Ssam		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1069178354Ssam		IEEE80211_SEND_MGMT(ni,
1070178354Ssam			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1071178354Ssam		break;
1072178354Ssam	default:
1073178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1074178354Ssam		    wh, "shared key auth", "bad seq %d", seq);
1075178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1076178354Ssam		return;
1077178354Ssam	}
1078178354Ssam	return;
1079178354Ssambad:
1080178354Ssam	/*
1081178354Ssam	 * Kick the state machine.  This short-circuits
1082178354Ssam	 * using the mgt frame timeout to trigger the
1083178354Ssam	 * state transition.
1084178354Ssam	 */
1085178354Ssam	if (vap->iv_state == IEEE80211_S_AUTH)
1086178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1087178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
1088178354Ssam}
1089178354Ssam
1090244060Sadrianint
1091178354Ssamieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1092178354Ssam	const struct ieee80211_frame *wh)
1093178354Ssam{
1094178354Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1095178354Ssam	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1096178354Ssam	u_int len = frm[1], qosinfo;
1097178354Ssam	int i;
1098178354Ssam
1099178354Ssam	if (len < sizeof(struct ieee80211_wme_param)-2) {
1100178354Ssam		IEEE80211_DISCARD_IE(vap,
1101178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1102178354Ssam		    wh, "WME", "too short, len %u", len);
1103178354Ssam		return -1;
1104178354Ssam	}
1105178354Ssam	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1106178354Ssam	qosinfo &= WME_QOSINFO_COUNT;
1107178354Ssam	/* XXX do proper check for wraparound */
1108178354Ssam	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1109178354Ssam		return 0;
1110178354Ssam	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1111178354Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1112178354Ssam		struct wmeParams *wmep =
1113178354Ssam			&wme->wme_wmeChanParams.cap_wmeParams[i];
1114178354Ssam		/* NB: ACI not used */
1115178354Ssam		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1116178354Ssam		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1117178354Ssam		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1118178354Ssam		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1119178354Ssam		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1120178354Ssam		frm += 4;
1121178354Ssam	}
1122178354Ssam	wme->wme_wmeChanParams.cap_info = qosinfo;
1123178354Ssam	return 1;
1124178354Ssam#undef MS
1125178354Ssam}
1126178354Ssam
1127178354Ssam/*
1128193439Ssam * Process 11h Channel Switch Announcement (CSA) ie.  If this
1129193439Ssam * is the first CSA then initiate the switch.  Otherwise we
1130193439Ssam * track state and trigger completion and/or cancel of the switch.
1131193439Ssam * XXX should be public for IBSS use
1132193439Ssam */
1133193439Ssamstatic void
1134193439Ssamieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1135193439Ssam	const struct ieee80211_frame *wh)
1136193439Ssam{
1137193439Ssam	struct ieee80211com *ic = vap->iv_ic;
1138193439Ssam	const struct ieee80211_csa_ie *csa =
1139193439Ssam	    (const struct ieee80211_csa_ie *) frm;
1140193439Ssam
1141193439Ssam	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1142193439Ssam	    ("state %s", ieee80211_state_name[vap->iv_state]));
1143193439Ssam
1144193439Ssam	if (csa->csa_mode > 1) {
1145193439Ssam		IEEE80211_DISCARD_IE(vap,
1146193439Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1147193439Ssam		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1148193439Ssam		return;
1149193439Ssam	}
1150193439Ssam	IEEE80211_LOCK(ic);
1151193439Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1152193439Ssam		/*
1153193439Ssam		 * Convert the channel number to a channel reference.  We
1154193439Ssam		 * try first to preserve turbo attribute of the current
1155193439Ssam		 * channel then fallback.  Note this will not work if the
1156193439Ssam		 * CSA specifies a channel that requires a band switch (e.g.
1157193439Ssam		 * 11a => 11g).  This is intentional as 11h is defined only
1158193439Ssam		 * for 5GHz/11a and because the switch does not involve a
1159193439Ssam		 * reassociation, protocol state (capabilities, negotated
1160193439Ssam		 * rates, etc) may/will be wrong.
1161193439Ssam		 */
1162193439Ssam		struct ieee80211_channel *c =
1163193439Ssam		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1164193439Ssam			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1165193439Ssam		if (c == NULL) {
1166193439Ssam			c = ieee80211_find_channel_byieee(ic,
1167193439Ssam			    csa->csa_newchan,
1168193439Ssam			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1169193439Ssam			if (c == NULL) {
1170193439Ssam				IEEE80211_DISCARD_IE(vap,
1171193439Ssam				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1172193439Ssam				    wh, "CSA", "invalid channel %u",
1173193439Ssam				    csa->csa_newchan);
1174193439Ssam				goto done;
1175193439Ssam			}
1176193439Ssam		}
1177193439Ssam#if IEEE80211_CSA_COUNT_MIN > 0
1178193439Ssam		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1179193439Ssam			/*
1180193439Ssam			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1181193439Ssam			 * reduce the risk of being redirected by a fabricated
1182193439Ssam			 * CSA.  If a valid CSA is dropped we'll still get a
1183193439Ssam			 * beacon miss when the AP leaves the channel so we'll
1184193439Ssam			 * eventually follow to the new channel.
1185193439Ssam			 *
1186193439Ssam			 * NOTE: this violates the 11h spec that states that
1187193439Ssam			 * count may be any value and if 0 then a switch
1188193439Ssam			 * should happen asap.
1189193439Ssam			 */
1190193439Ssam			IEEE80211_DISCARD_IE(vap,
1191193439Ssam			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1192193439Ssam			    wh, "CSA", "count %u too small, must be >= %u",
1193193439Ssam			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1194193439Ssam			goto done;
1195193439Ssam		}
1196193439Ssam#endif
1197193439Ssam		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1198193439Ssam	} else {
1199193439Ssam		/*
1200193439Ssam		 * Validate this ie against the initial CSA.  We require
1201193439Ssam		 * mode and channel not change and the count must be
1202193439Ssam		 * monotonically decreasing.  This may be pointless and
1203193439Ssam		 * canceling the switch as a result may be too paranoid but
1204193439Ssam		 * in the worst case if we drop out of CSA because of this
1205193439Ssam		 * and the AP does move then we'll just end up taking a
1206193439Ssam		 * beacon miss and scan to find the AP.
1207193439Ssam		 *
1208193439Ssam		 * XXX may want <= on count as we also process ProbeResp
1209193439Ssam		 * frames and those may come in w/ the same count as the
1210193439Ssam		 * previous beacon; but doing so leaves us open to a stuck
1211193439Ssam		 * count until we add a dead-man timer
1212193439Ssam		 */
1213193439Ssam		if (!(csa->csa_count < ic->ic_csa_count &&
1214193439Ssam		      csa->csa_mode == ic->ic_csa_mode &&
1215193439Ssam		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1216193439Ssam			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1217193439Ssam			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1218193439Ssam			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1219193439Ssam			    ic->ic_csa_newchan, ic->ic_csa_count,
1220193439Ssam			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1221193439Ssam			ieee80211_csa_cancelswitch(ic);
1222193439Ssam		} else {
1223193439Ssam			if (csa->csa_count <= 1)
1224193439Ssam				ieee80211_csa_completeswitch(ic);
1225193439Ssam			else
1226193439Ssam				ic->ic_csa_count = csa->csa_count;
1227193439Ssam		}
1228193439Ssam	}
1229193439Ssamdone:
1230193439Ssam	IEEE80211_UNLOCK(ic);
1231193439Ssam}
1232193439Ssam
1233193439Ssam/*
1234178354Ssam * Return non-zero if a background scan may be continued:
1235178354Ssam * o bg scan is active
1236178354Ssam * o no channel switch is pending
1237178354Ssam * o there has not been any traffic recently
1238178354Ssam *
1239178354Ssam * Note we do not check if there is an administrative enable;
1240178354Ssam * this is only done to start the scan.  We assume that any
1241178354Ssam * change in state will be accompanied by a request to cancel
1242178354Ssam * active scans which will otherwise cause this test to fail.
1243178354Ssam */
1244178354Ssamstatic __inline int
1245178354Ssamcontbgscan(struct ieee80211vap *vap)
1246178354Ssam{
1247178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1248178354Ssam
1249178354Ssam	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1250178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1251178354Ssam	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1252178354Ssam	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1253178354Ssam}
1254178354Ssam
1255178354Ssam/*
1256178354Ssam * Return non-zero if a backgrond scan may be started:
1257178354Ssam * o bg scanning is administratively enabled
1258178354Ssam * o no channel switch is pending
1259178354Ssam * o we are not boosted on a dynamic turbo channel
1260178354Ssam * o there has not been a scan recently
1261178354Ssam * o there has not been any traffic recently
1262178354Ssam */
1263178354Ssamstatic __inline int
1264178354Ssamstartbgscan(struct ieee80211vap *vap)
1265178354Ssam{
1266178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1267178354Ssam
1268178354Ssam	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1269178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1270190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1271178354Ssam	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1272190391Ssam#endif
1273178354Ssam	    time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1274178354Ssam	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1275178354Ssam}
1276178354Ssam
1277178354Ssamstatic void
1278178354Ssamsta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1279192468Ssam	int subtype, int rssi, int nf)
1280178354Ssam{
1281178354Ssam#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1282178354Ssam#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1283178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1284178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1285178354Ssam	struct ieee80211_frame *wh;
1286178354Ssam	uint8_t *frm, *efrm;
1287178354Ssam	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1288178354Ssam	uint8_t rate;
1289233452Sadrian	int ht_state_change = 0;
1290178354Ssam
1291178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1292178354Ssam	frm = (uint8_t *)&wh[1];
1293178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1294178354Ssam	switch (subtype) {
1295178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1296178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON: {
1297178354Ssam		struct ieee80211_scanparams scan;
1298178354Ssam		/*
1299178354Ssam		 * We process beacon/probe response frames:
1300178354Ssam		 *    o when scanning, or
1301178354Ssam		 *    o station mode when associated (to collect state
1302191444Srpaulo		 *      updates such as 802.11g slot time)
1303178354Ssam		 * Frames otherwise received are discarded.
1304178354Ssam		 */
1305178354Ssam		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1306178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1307178354Ssam			return;
1308178354Ssam		}
1309178354Ssam		/* XXX probe response in sta mode when !scanning? */
1310232244Sadrian		if (ieee80211_parse_beacon(ni, m0, &scan) != 0) {
1311232270Sadrian			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1312232270Sadrian				vap->iv_stats.is_beacon_bad++;
1313178354Ssam			return;
1314232244Sadrian		}
1315178354Ssam		/*
1316178354Ssam		 * Count frame now that we know it's to be processed.
1317178354Ssam		 */
1318178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1319178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1320178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1321178354Ssam		} else
1322178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1323178354Ssam		/*
1324178354Ssam		 * When operating in station mode, check for state updates.
1325178354Ssam		 * Be careful to ignore beacons received while doing a
1326178354Ssam		 * background scan.  We consider only 11g/WMM stuff right now.
1327178354Ssam		 */
1328178354Ssam		if (ni->ni_associd != 0 &&
1329178354Ssam		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1330178354Ssam		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1331178354Ssam			/* record tsf of last beacon */
1332178354Ssam			memcpy(ni->ni_tstamp.data, scan.tstamp,
1333178354Ssam				sizeof(ni->ni_tstamp));
1334178354Ssam			/* count beacon frame for s/w bmiss handling */
1335178354Ssam			vap->iv_swbmiss_count++;
1336178354Ssam			vap->iv_bmiss_count = 0;
1337178354Ssam			if (ni->ni_erp != scan.erp) {
1338178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1339178354Ssam				    wh->i_addr2,
1340178354Ssam				    "erp change: was 0x%x, now 0x%x",
1341178354Ssam				    ni->ni_erp, scan.erp);
1342178354Ssam				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1343178354Ssam				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1344178354Ssam					ic->ic_flags |= IEEE80211_F_USEPROT;
1345178354Ssam				else
1346178354Ssam					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1347178354Ssam				ni->ni_erp = scan.erp;
1348178354Ssam				/* XXX statistic */
1349178354Ssam				/* XXX driver notification */
1350178354Ssam			}
1351178354Ssam			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1352178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1353178354Ssam				    wh->i_addr2,
1354178354Ssam				    "capabilities change: was 0x%x, now 0x%x",
1355178354Ssam				    ni->ni_capinfo, scan.capinfo);
1356178354Ssam				/*
1357178354Ssam				 * NB: we assume short preamble doesn't
1358178354Ssam				 *     change dynamically
1359178354Ssam				 */
1360178354Ssam				ieee80211_set_shortslottime(ic,
1361178354Ssam					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1362178354Ssam					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1363178354Ssam				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1364178354Ssam					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1365178354Ssam				/* XXX statistic */
1366178354Ssam			}
1367178354Ssam			if (scan.wme != NULL &&
1368178354Ssam			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1369178354Ssam			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1370178354Ssam				ieee80211_wme_updateparams(vap);
1371190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1372178354Ssam			if (scan.ath != NULL)
1373178354Ssam				ieee80211_parse_athparams(ni, scan.ath, wh);
1374190391Ssam#endif
1375183254Ssam			if (scan.htcap != NULL && scan.htinfo != NULL &&
1376193655Ssam			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1377178354Ssam				/* XXX state changes? */
1378233452Sadrian				if (ieee80211_ht_updateparams(ni,
1379233452Sadrian				    scan.htcap, scan.htinfo))
1380233452Sadrian					ht_state_change = 1;
1381178354Ssam			}
1382227331Sadrian			if (scan.quiet)
1383227331Sadrian				ic->ic_set_quiet(ni, scan.quiet);
1384178354Ssam			if (scan.tim != NULL) {
1385178354Ssam				struct ieee80211_tim_ie *tim =
1386178354Ssam				    (struct ieee80211_tim_ie *) scan.tim;
1387178354Ssam#if 0
1388178354Ssam				int aid = IEEE80211_AID(ni->ni_associd);
1389178354Ssam				int ix = aid / NBBY;
1390178354Ssam				int min = tim->tim_bitctl &~ 1;
1391178354Ssam				int max = tim->tim_len + min - 4;
1392178354Ssam				if ((tim->tim_bitctl&1) ||
1393178354Ssam				    (min <= ix && ix <= max &&
1394178354Ssam				     isset(tim->tim_bitmap - min, aid))) {
1395178354Ssam					/*
1396178354Ssam					 * XXX Do not let bg scan kick off
1397178354Ssam					 * we are expecting data.
1398178354Ssam					 */
1399178354Ssam					ic->ic_lastdata = ticks;
1400241138Sadrian					vap->iv_sta_ps(vap, 0);
1401178354Ssam				}
1402178354Ssam#endif
1403178354Ssam				ni->ni_dtim_count = tim->tim_count;
1404178354Ssam				ni->ni_dtim_period = tim->tim_period;
1405178354Ssam			}
1406193439Ssam			if (scan.csa != NULL &&
1407193439Ssam			    (vap->iv_flags & IEEE80211_F_DOTH))
1408193439Ssam				ieee80211_parse_csaparams(vap, scan.csa, wh);
1409193439Ssam			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1410193439Ssam				/*
1411193439Ssam				 * No CSA ie or 11h disabled, but a channel
1412193439Ssam				 * switch is pending; drop out so we aren't
1413193439Ssam				 * stuck in CSA state.  If the AP really is
1414193439Ssam				 * moving we'll get a beacon miss and scan.
1415193439Ssam				 */
1416193439Ssam				IEEE80211_LOCK(ic);
1417193439Ssam				ieee80211_csa_cancelswitch(ic);
1418193439Ssam				IEEE80211_UNLOCK(ic);
1419193439Ssam			}
1420178354Ssam			/*
1421178354Ssam			 * If scanning, pass the info to the scan module.
1422178354Ssam			 * Otherwise, check if it's the right time to do
1423178354Ssam			 * a background scan.  Background scanning must
1424178354Ssam			 * be enabled and we must not be operating in the
1425178354Ssam			 * turbo phase of dynamic turbo mode.  Then,
1426178354Ssam			 * it's been a while since the last background
1427178354Ssam			 * scan and if no data frames have come through
1428178354Ssam			 * recently, kick off a scan.  Note that this
1429178354Ssam			 * is the mechanism by which a background scan
1430178354Ssam			 * is started _and_ continued each time we
1431178354Ssam			 * return on-channel to receive a beacon from
1432178354Ssam			 * our ap.
1433178354Ssam			 */
1434178354Ssam			if (ic->ic_flags & IEEE80211_F_SCAN) {
1435178354Ssam				ieee80211_add_scan(vap, &scan, wh,
1436192468Ssam					subtype, rssi, nf);
1437178354Ssam			} else if (contbgscan(vap)) {
1438178354Ssam				ieee80211_bg_scan(vap, 0);
1439178354Ssam			} else if (startbgscan(vap)) {
1440178354Ssam				vap->iv_stats.is_scan_bg++;
1441178354Ssam#if 0
1442178354Ssam				/* wakeup if we are sleeing */
1443178354Ssam				ieee80211_set_pwrsave(vap, 0);
1444178354Ssam#endif
1445178354Ssam				ieee80211_bg_scan(vap, 0);
1446178354Ssam			}
1447233452Sadrian
1448233452Sadrian			/*
1449233452Sadrian			 * If we've had a channel width change (eg HT20<->HT40)
1450233452Sadrian			 * then schedule a delayed driver notification.
1451233452Sadrian			 */
1452233452Sadrian			if (ht_state_change)
1453233452Sadrian				ieee80211_update_chw(ic);
1454178354Ssam			return;
1455178354Ssam		}
1456178354Ssam		/*
1457178354Ssam		 * If scanning, just pass information to the scan module.
1458178354Ssam		 */
1459178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1460178354Ssam			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1461178354Ssam				/*
1462178354Ssam				 * Actively scanning a channel marked passive;
1463178354Ssam				 * send a probe request now that we know there
1464178354Ssam				 * is 802.11 traffic present.
1465178354Ssam				 *
1466178354Ssam				 * XXX check if the beacon we recv'd gives
1467178354Ssam				 * us what we need and suppress the probe req
1468178354Ssam				 */
1469178354Ssam				ieee80211_probe_curchan(vap, 1);
1470178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1471178354Ssam			}
1472192468Ssam			ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1473178354Ssam			return;
1474178354Ssam		}
1475178354Ssam		break;
1476178354Ssam	}
1477178354Ssam
1478178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH: {
1479178354Ssam		uint16_t algo, seq, status;
1480178354Ssam		/*
1481178354Ssam		 * auth frame format
1482178354Ssam		 *	[2] algorithm
1483178354Ssam		 *	[2] sequence
1484178354Ssam		 *	[2] status
1485178354Ssam		 *	[tlv*] challenge
1486178354Ssam		 */
1487178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1488178354Ssam		algo   = le16toh(*(uint16_t *)frm);
1489178354Ssam		seq    = le16toh(*(uint16_t *)(frm + 2));
1490178354Ssam		status = le16toh(*(uint16_t *)(frm + 4));
1491178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1492178354Ssam		    "recv auth frame with algorithm %d seq %d", algo, seq);
1493178354Ssam
1494178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1495178354Ssam			IEEE80211_DISCARD(vap,
1496178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1497178354Ssam			    wh, "auth", "%s", "TKIP countermeasures enabled");
1498178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1499178354Ssam			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1500178354Ssam				ieee80211_send_error(ni, wh->i_addr2,
1501178354Ssam					IEEE80211_FC0_SUBTYPE_AUTH,
1502178354Ssam					IEEE80211_REASON_MIC_FAILURE);
1503178354Ssam			}
1504178354Ssam			return;
1505178354Ssam		}
1506178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1507192468Ssam			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1508192468Ssam			    seq, status);
1509178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1510192468Ssam			sta_auth_open(ni, wh, rssi, nf, seq, status);
1511178354Ssam		else {
1512178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1513178354Ssam			    wh, "auth", "unsupported alg %d", algo);
1514178354Ssam			vap->iv_stats.is_rx_auth_unsupported++;
1515178354Ssam			return;
1516178354Ssam		}
1517178354Ssam		break;
1518178354Ssam	}
1519178354Ssam
1520178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1521178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1522178354Ssam		uint16_t capinfo, associd;
1523178354Ssam		uint16_t status;
1524178354Ssam
1525178354Ssam		if (vap->iv_state != IEEE80211_S_ASSOC) {
1526178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1527178354Ssam			return;
1528178354Ssam		}
1529178354Ssam
1530178354Ssam		/*
1531178354Ssam		 * asresp frame format
1532178354Ssam		 *	[2] capability information
1533178354Ssam		 *	[2] status
1534178354Ssam		 *	[2] association ID
1535178354Ssam		 *	[tlv] supported rates
1536178354Ssam		 *	[tlv] extended supported rates
1537178354Ssam		 *	[tlv] WME
1538178354Ssam		 *	[tlv] HT capabilities
1539178354Ssam		 *	[tlv] HT info
1540178354Ssam		 */
1541178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1542178354Ssam		ni = vap->iv_bss;
1543178354Ssam		capinfo = le16toh(*(uint16_t *)frm);
1544178354Ssam		frm += 2;
1545178354Ssam		status = le16toh(*(uint16_t *)frm);
1546178354Ssam		frm += 2;
1547178354Ssam		if (status != 0) {
1548178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1549178354Ssam			    wh->i_addr2, "%sassoc failed (reason %d)",
1550178354Ssam			    ISREASSOC(subtype) ?  "re" : "", status);
1551178354Ssam			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1552178354Ssam			return;
1553178354Ssam		}
1554178354Ssam		associd = le16toh(*(uint16_t *)frm);
1555178354Ssam		frm += 2;
1556178354Ssam
1557178354Ssam		rates = xrates = wme = htcap = htinfo = NULL;
1558178354Ssam		while (efrm - frm > 1) {
1559178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1560178354Ssam			switch (*frm) {
1561178354Ssam			case IEEE80211_ELEMID_RATES:
1562178354Ssam				rates = frm;
1563178354Ssam				break;
1564178354Ssam			case IEEE80211_ELEMID_XRATES:
1565178354Ssam				xrates = frm;
1566178354Ssam				break;
1567178354Ssam			case IEEE80211_ELEMID_HTCAP:
1568178354Ssam				htcap = frm;
1569178354Ssam				break;
1570178354Ssam			case IEEE80211_ELEMID_HTINFO:
1571178354Ssam				htinfo = frm;
1572178354Ssam				break;
1573178354Ssam			case IEEE80211_ELEMID_VENDOR:
1574178354Ssam				if (iswmeoui(frm))
1575178354Ssam					wme = frm;
1576193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1577178354Ssam					/*
1578178354Ssam					 * Accept pre-draft HT ie's if the
1579178354Ssam					 * standard ones have not been seen.
1580178354Ssam					 */
1581178354Ssam					if (ishtcapoui(frm)) {
1582178354Ssam						if (htcap == NULL)
1583178354Ssam							htcap = frm;
1584178354Ssam					} else if (ishtinfooui(frm)) {
1585178354Ssam						if (htinfo == NULL)
1586219603Sbschmidt							htinfo = frm;
1587178354Ssam					}
1588178354Ssam				}
1589178354Ssam				/* XXX Atheros OUI support */
1590178354Ssam				break;
1591178354Ssam			}
1592178354Ssam			frm += frm[1] + 2;
1593178354Ssam		}
1594178354Ssam
1595178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1596178354Ssam		if (xrates != NULL)
1597178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1598178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1599178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
1600178354Ssam				IEEE80211_F_JOIN |
1601178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1602178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1603178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
1604178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1605178354Ssam			    wh->i_addr2,
1606178354Ssam			    "%sassoc failed (rate set mismatch)",
1607178354Ssam			    ISREASSOC(subtype) ?  "re" : "");
1608178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
1609178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1610178354Ssam			    IEEE80211_SCAN_FAIL_STATUS);
1611178354Ssam			return;
1612178354Ssam		}
1613178354Ssam
1614178354Ssam		ni->ni_capinfo = capinfo;
1615178354Ssam		ni->ni_associd = associd;
1616178354Ssam		if (ni->ni_jointime == 0)
1617178354Ssam			ni->ni_jointime = time_uptime;
1618178354Ssam		if (wme != NULL &&
1619178354Ssam		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1620178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1621178354Ssam			ieee80211_wme_updateparams(vap);
1622178354Ssam		} else
1623178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1624178354Ssam		/*
1625178354Ssam		 * Setup HT state according to the negotiation.
1626178354Ssam		 *
1627178354Ssam		 * NB: shouldn't need to check if HT use is enabled but some
1628178354Ssam		 *     ap's send back HT ie's even when we don't indicate we
1629178354Ssam		 *     are HT capable in our AssocReq.
1630178354Ssam		 */
1631178354Ssam		if (htcap != NULL && htinfo != NULL &&
1632193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1633183254Ssam			ieee80211_ht_node_init(ni);
1634183254Ssam			ieee80211_ht_updateparams(ni, htcap, htinfo);
1635178354Ssam			ieee80211_setup_htrates(ni, htcap,
1636178354Ssam			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1637178354Ssam			ieee80211_setup_basic_htrates(ni, htinfo);
1638193966Ssam			ieee80211_node_setuptxparms(ni);
1639214894Sbschmidt			ieee80211_ratectl_node_init(ni);
1640190579Ssam		} else {
1641190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1642190579Ssam			if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1643190579Ssam				ieee80211_ff_node_init(ni);
1644190579Ssam#endif
1645178354Ssam		}
1646178354Ssam		/*
1647178354Ssam		 * Configure state now that we are associated.
1648178354Ssam		 *
1649178354Ssam		 * XXX may need different/additional driver callbacks?
1650178354Ssam		 */
1651178354Ssam		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1652178354Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1653178354Ssam			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1654178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1655178354Ssam		} else {
1656178354Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1657178354Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1658178354Ssam		}
1659178354Ssam		ieee80211_set_shortslottime(ic,
1660178354Ssam			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1661178354Ssam			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1662178354Ssam		/*
1663178354Ssam		 * Honor ERP protection.
1664178354Ssam		 *
1665178354Ssam		 * NB: ni_erp should zero for non-11g operation.
1666178354Ssam		 */
1667178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1668178354Ssam		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1669178354Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1670178354Ssam		else
1671178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1672178354Ssam		IEEE80211_NOTE_MAC(vap,
1673178354Ssam		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1674183256Ssam		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1675178354Ssam		    ISREASSOC(subtype) ? "re" : "",
1676178354Ssam		    IEEE80211_NODE_AID(ni),
1677178354Ssam		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1678178354Ssam		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1679178354Ssam		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1680178354Ssam		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1681178354Ssam		    ni->ni_flags & IEEE80211_NODE_HT ?
1682182834Ssam			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1683178354Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1684183255Ssam		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1685183255Ssam			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1686183256Ssam		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1687178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1688178354Ssam			", fast-frames" : "",
1689178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1690178354Ssam			", turbo" : ""
1691178354Ssam		);
1692178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1693178354Ssam		break;
1694178354Ssam	}
1695178354Ssam
1696178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1697178354Ssam		uint16_t reason;
1698178354Ssam
1699178354Ssam		if (vap->iv_state == IEEE80211_S_SCAN) {
1700178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1701178354Ssam			return;
1702178354Ssam		}
1703178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1704178354Ssam			/* NB: can happen when in promiscuous mode */
1705178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1706178354Ssam			break;
1707178354Ssam		}
1708178354Ssam
1709178354Ssam		/*
1710178354Ssam		 * deauth frame format
1711178354Ssam		 *	[2] reason
1712178354Ssam		 */
1713178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1714178354Ssam		reason = le16toh(*(uint16_t *)frm);
1715178354Ssam
1716178354Ssam		vap->iv_stats.is_rx_deauth++;
1717178354Ssam		vap->iv_stats.is_rx_deauth_code = reason;
1718178354Ssam		IEEE80211_NODE_STAT(ni, rx_deauth);
1719178354Ssam
1720178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1721178354Ssam		    "recv deauthenticate (reason %d)", reason);
1722178354Ssam		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1723178354Ssam		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1724178354Ssam		break;
1725178354Ssam	}
1726178354Ssam
1727178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1728178354Ssam		uint16_t reason;
1729178354Ssam
1730178354Ssam		if (vap->iv_state != IEEE80211_S_RUN &&
1731178354Ssam		    vap->iv_state != IEEE80211_S_ASSOC &&
1732178354Ssam		    vap->iv_state != IEEE80211_S_AUTH) {
1733178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1734178354Ssam			return;
1735178354Ssam		}
1736178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1737178354Ssam			/* NB: can happen when in promiscuous mode */
1738178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1739178354Ssam			break;
1740178354Ssam		}
1741178354Ssam
1742178354Ssam		/*
1743178354Ssam		 * disassoc frame format
1744178354Ssam		 *	[2] reason
1745178354Ssam		 */
1746178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1747178354Ssam		reason = le16toh(*(uint16_t *)frm);
1748178354Ssam
1749178354Ssam		vap->iv_stats.is_rx_disassoc++;
1750178354Ssam		vap->iv_stats.is_rx_disassoc_code = reason;
1751178354Ssam		IEEE80211_NODE_STAT(ni, rx_disassoc);
1752178354Ssam
1753178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1754178354Ssam		    "recv disassociate (reason %d)", reason);
1755178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1756178354Ssam		break;
1757178354Ssam	}
1758178354Ssam
1759178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
1760218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1761218958Sbschmidt		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1762218958Sbschmidt		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1763218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1764218958Sbschmidt			    wh, NULL, "%s", "not for us");
1765218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
1766218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
1767218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1768218927Sbschmidt			    wh, NULL, "wrong state %s",
1769218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
1770178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1771218958Sbschmidt		} else {
1772218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
1773218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1774218927Sbschmidt		}
1775178354Ssam		break;
1776178354Ssam
1777178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1778178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1779218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1780218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
1781218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1782218927Sbschmidt		    wh, NULL, "%s", "not handled");
1783178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
1784218927Sbschmidt		break;
1785218927Sbschmidt
1786178354Ssam	default:
1787178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1788218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
1789178354Ssam		vap->iv_stats.is_rx_badsubtype++;
1790178354Ssam		break;
1791178354Ssam	}
1792178354Ssam#undef ISREASSOC
1793178354Ssam#undef ISPROBE
1794178354Ssam}
1795191546Ssam
1796191546Ssamstatic void
1797205277Srpaulosta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1798191546Ssam{
1799205277Srpaulo	switch (subtype) {
1800205277Srpaulo	case IEEE80211_FC0_SUBTYPE_BAR:
1801205277Srpaulo		ieee80211_recv_bar(ni, m);
1802205277Srpaulo		break;
1803205277Srpaulo	}
1804191546Ssam}
1805