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