ieee80211_sta.c revision 297405
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 297405 2016-03-30 00:44:10Z 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>
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);
73283535Sadrianstatic	int sta_input(struct ieee80211_node *, struct mbuf *,
74283535Sadrian	    const struct ieee80211_rx_stats *, int, int);
75178354Ssamstatic void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76283535Sadrian	    int subtype, const struct ieee80211_rx_stats *, int rssi, int nf);
77191546Ssamstatic void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
78178354Ssam
79178354Ssamvoid
80178354Ssamieee80211_sta_attach(struct ieee80211com *ic)
81178354Ssam{
82178354Ssam	ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
83178354Ssam}
84178354Ssam
85178354Ssamvoid
86178354Ssamieee80211_sta_detach(struct ieee80211com *ic)
87178354Ssam{
88178354Ssam}
89178354Ssam
90178354Ssamstatic void
91178354Ssamsta_vdetach(struct ieee80211vap *vap)
92178354Ssam{
93178354Ssam}
94178354Ssam
95178354Ssamstatic void
96178354Ssamsta_vattach(struct ieee80211vap *vap)
97178354Ssam{
98178354Ssam	vap->iv_newstate = sta_newstate;
99178354Ssam	vap->iv_input = sta_input;
100178354Ssam	vap->iv_recv_mgmt = sta_recv_mgmt;
101191546Ssam	vap->iv_recv_ctl = sta_recv_ctl;
102178354Ssam	vap->iv_opdetach = sta_vdetach;
103178354Ssam	vap->iv_bmiss = sta_beacon_miss;
104178354Ssam}
105178354Ssam
106178354Ssam/*
107178354Ssam * Handle a beacon miss event.  The common code filters out
108178354Ssam * spurious events that can happen when scanning and/or before
109178354Ssam * reaching RUN state.
110178354Ssam */
111178354Ssamstatic void
112178354Ssamsta_beacon_miss(struct ieee80211vap *vap)
113178354Ssam{
114193439Ssam	struct ieee80211com *ic = vap->iv_ic;
115178354Ssam
116225913Sadrian	IEEE80211_LOCK_ASSERT(ic);
117225913Sadrian
118193439Ssam	KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
119193439Ssam	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
120193439Ssam	    ("wrong state %s", ieee80211_state_name[vap->iv_state]));
121178354Ssam
122193439Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
123193439Ssam	    "beacon miss, mode %s state %s\n",
124193439Ssam	    ieee80211_opmode_name[vap->iv_opmode],
125193439Ssam	    ieee80211_state_name[vap->iv_state]);
126193439Ssam
127193439Ssam	if (vap->iv_state == IEEE80211_S_CSA) {
128193439Ssam		/*
129193439Ssam		 * A Channel Switch is pending; assume we missed the
130193439Ssam		 * beacon that would've completed the process and just
131193439Ssam		 * force the switch.  If we made a mistake we'll not
132193439Ssam		 * find the AP on the new channel and fall back to a
133193439Ssam		 * normal scan.
134193439Ssam		 */
135193439Ssam		ieee80211_csa_completeswitch(ic);
136193439Ssam		return;
137193439Ssam	}
138178354Ssam	if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
139178354Ssam		/*
140178354Ssam		 * Send a directed probe req before falling back to a
141178354Ssam		 * scan; if we receive a response ic_bmiss_count will
142178354Ssam		 * be reset.  Some cards mistakenly report beacon miss
143178354Ssam		 * so this avoids the expensive scan if the ap is
144178354Ssam		 * still there.
145178354Ssam		 */
146178354Ssam		ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
147178354Ssam			vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
148178354Ssam			vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
149178354Ssam		return;
150178354Ssam	}
151205140Sweongyo
152205140Sweongyo	callout_stop(&vap->iv_swbmiss);
153178354Ssam	vap->iv_bmiss_count = 0;
154178354Ssam	vap->iv_stats.is_beacon_miss++;
155178354Ssam	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
156190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
157190402Ssam		struct ieee80211com *ic = vap->iv_ic;
158190402Ssam
159178354Ssam		/*
160178354Ssam		 * If we receive a beacon miss interrupt when using
161178354Ssam		 * dynamic turbo, attempt to switch modes before
162178354Ssam		 * reassociating.
163178354Ssam		 */
164178354Ssam		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
165178354Ssam			ieee80211_dturbo_switch(vap,
166178354Ssam			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
167190391Ssam#endif
168178354Ssam		/*
169178354Ssam		 * Try to reassociate before scanning for a new ap.
170178354Ssam		 */
171178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
172178354Ssam	} else {
173178354Ssam		/*
174178354Ssam		 * Somebody else is controlling state changes (e.g.
175178354Ssam		 * a user-mode app) don't do anything that would
176178354Ssam		 * confuse them; just drop into scan mode so they'll
177178354Ssam		 * notified of the state change and given control.
178178354Ssam		 */
179178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
180178354Ssam	}
181178354Ssam}
182178354Ssam
183178354Ssam/*
184178354Ssam * Handle deauth with reason.  We retry only for
185178354Ssam * the cases where we might succeed.  Otherwise
186178354Ssam * we downgrade the ap and scan.
187178354Ssam */
188178354Ssamstatic void
189178354Ssamsta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
190178354Ssam{
191178354Ssam	switch (reason) {
192178354Ssam	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
193178354Ssam	case IEEE80211_STATUS_TIMEOUT:
194178354Ssam	case IEEE80211_REASON_ASSOC_EXPIRE:
195178354Ssam	case IEEE80211_REASON_NOT_AUTHED:
196178354Ssam	case IEEE80211_REASON_NOT_ASSOCED:
197178354Ssam	case IEEE80211_REASON_ASSOC_LEAVE:
198178354Ssam	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
199178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
200178354Ssam		break;
201178354Ssam	default:
202178354Ssam		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
203178354Ssam		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
204178354Ssam			ieee80211_check_scan_current(vap);
205178354Ssam		break;
206178354Ssam	}
207178354Ssam}
208178354Ssam
209297164Savosstatic void
210297164Savossta_swbmiss_start(struct ieee80211vap *vap)
211297164Savos{
212297164Savos
213297164Savos	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) {
214297164Savos		/*
215297164Savos		 * Start s/w beacon miss timer for devices w/o
216297164Savos		 * hardware support.  We fudge a bit here since
217297164Savos		 * we're doing this in software.
218297164Savos		 */
219297164Savos		vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
220297164Savos		    2 * vap->iv_bmissthreshold * vap->iv_bss->ni_intval);
221297164Savos		vap->iv_swbmiss_count = 0;
222297164Savos		callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
223297164Savos		    ieee80211_swbmiss, vap);
224297164Savos	}
225297164Savos}
226297164Savos
227178354Ssam/*
228178354Ssam * IEEE80211_M_STA vap state machine handler.
229178354Ssam * This routine handles the main states in the 802.11 protocol.
230178354Ssam */
231178354Ssamstatic int
232178354Ssamsta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
233178354Ssam{
234178354Ssam	struct ieee80211com *ic = vap->iv_ic;
235178354Ssam	struct ieee80211_node *ni;
236178354Ssam	enum ieee80211_state ostate;
237178354Ssam
238178354Ssam	IEEE80211_LOCK_ASSERT(ic);
239178354Ssam
240178354Ssam	ostate = vap->iv_state;
241178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
242178354Ssam	    __func__, ieee80211_state_name[ostate],
243178354Ssam	    ieee80211_state_name[nstate], arg);
244178354Ssam	vap->iv_state = nstate;			/* state transition */
245178354Ssam	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
246178354Ssam	if (ostate != IEEE80211_S_SCAN)
247178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
248178354Ssam	ni = vap->iv_bss;			/* NB: no reference held */
249178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
250178354Ssam		callout_stop(&vap->iv_swbmiss);
251178354Ssam	switch (nstate) {
252178354Ssam	case IEEE80211_S_INIT:
253178354Ssam		switch (ostate) {
254178354Ssam		case IEEE80211_S_SLEEP:
255178354Ssam			/* XXX wakeup */
256264855Sadrian			/* XXX driver hook to wakeup the hardware? */
257178354Ssam		case IEEE80211_S_RUN:
258178354Ssam			IEEE80211_SEND_MGMT(ni,
259178354Ssam			    IEEE80211_FC0_SUBTYPE_DISASSOC,
260178354Ssam			    IEEE80211_REASON_ASSOC_LEAVE);
261178354Ssam			ieee80211_sta_leave(ni);
262178354Ssam			break;
263178354Ssam		case IEEE80211_S_ASSOC:
264178354Ssam			IEEE80211_SEND_MGMT(ni,
265178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
266178354Ssam			    IEEE80211_REASON_AUTH_LEAVE);
267178354Ssam			break;
268178354Ssam		case IEEE80211_S_SCAN:
269178354Ssam			ieee80211_cancel_scan(vap);
270178354Ssam			break;
271178354Ssam		default:
272296355Savos			break;
273178354Ssam		}
274178354Ssam		if (ostate != IEEE80211_S_INIT) {
275178354Ssam			/* NB: optimize INIT -> INIT case */
276178354Ssam			ieee80211_reset_bss(vap);
277178354Ssam		}
278178354Ssam		if (vap->iv_auth->ia_detach != NULL)
279178354Ssam			vap->iv_auth->ia_detach(vap);
280178354Ssam		break;
281178354Ssam	case IEEE80211_S_SCAN:
282178354Ssam		switch (ostate) {
283178354Ssam		case IEEE80211_S_INIT:
284178354Ssam			/*
285178354Ssam			 * Initiate a scan.  We can come here as a result
286178354Ssam			 * of an IEEE80211_IOC_SCAN_REQ too in which case
287178354Ssam			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
288178354Ssam			 * and the scan request parameters will be present
289178354Ssam			 * in iv_scanreq.  Otherwise we do the default.
290178354Ssam			 */
291178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
292178354Ssam				ieee80211_check_scan(vap,
293178354Ssam				    vap->iv_scanreq_flags,
294178354Ssam				    vap->iv_scanreq_duration,
295178354Ssam				    vap->iv_scanreq_mindwell,
296178354Ssam				    vap->iv_scanreq_maxdwell,
297178354Ssam				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
298178354Ssam				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
299178354Ssam			} else
300178354Ssam				ieee80211_check_scan_current(vap);
301178354Ssam			break;
302178354Ssam		case IEEE80211_S_SCAN:
303178354Ssam		case IEEE80211_S_AUTH:
304178354Ssam		case IEEE80211_S_ASSOC:
305178354Ssam			/*
306178354Ssam			 * These can happen either because of a timeout
307178354Ssam			 * on an assoc/auth response or because of a
308178354Ssam			 * change in state that requires a reset.  For
309178354Ssam			 * the former we're called with a non-zero arg
310178354Ssam			 * that is the cause for the failure; pass this
311178354Ssam			 * to the scan code so it can update state.
312178354Ssam			 * Otherwise trigger a new scan unless we're in
313178354Ssam			 * manual roaming mode in which case an application
314178354Ssam			 * must issue an explicit scan request.
315178354Ssam			 */
316178354Ssam			if (arg != 0)
317178354Ssam				ieee80211_scan_assoc_fail(vap,
318178354Ssam					vap->iv_bss->ni_macaddr, arg);
319178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
320178354Ssam				ieee80211_check_scan_current(vap);
321178354Ssam			break;
322264906Sadrian		case IEEE80211_S_SLEEP:		/* beacon miss */
323264906Sadrian			/*
324264906Sadrian			 * XXX if in sleep we need to wakeup the hardware.
325264906Sadrian			 */
326264906Sadrian			/* FALLTHROUGH */
327178354Ssam		case IEEE80211_S_RUN:		/* beacon miss */
328178354Ssam			/*
329178354Ssam			 * Beacon miss.  Notify user space and if not
330178354Ssam			 * under control of a user application (roaming
331178354Ssam			 * manual) kick off a scan to re-connect.
332178354Ssam			 */
333264906Sadrian
334178354Ssam			ieee80211_sta_leave(ni);
335178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
336178354Ssam				ieee80211_check_scan_current(vap);
337178354Ssam			break;
338178354Ssam		default:
339178354Ssam			goto invalid;
340178354Ssam		}
341178354Ssam		break;
342178354Ssam	case IEEE80211_S_AUTH:
343178354Ssam		switch (ostate) {
344178354Ssam		case IEEE80211_S_INIT:
345178354Ssam		case IEEE80211_S_SCAN:
346178354Ssam			IEEE80211_SEND_MGMT(ni,
347178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
348178354Ssam			break;
349178354Ssam		case IEEE80211_S_AUTH:
350178354Ssam		case IEEE80211_S_ASSOC:
351178354Ssam			switch (arg & 0xff) {
352178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
353178354Ssam				/* ??? */
354178354Ssam				IEEE80211_SEND_MGMT(ni,
355178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
356178354Ssam				break;
357178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
358178354Ssam				sta_authretry(vap, ni, arg>>8);
359178354Ssam				break;
360178354Ssam			}
361178354Ssam			break;
362297162Savos		case IEEE80211_S_SLEEP:
363178354Ssam		case IEEE80211_S_RUN:
364178354Ssam			switch (arg & 0xff) {
365178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
366178354Ssam				IEEE80211_SEND_MGMT(ni,
367178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
368297162Savos				vap->iv_state = IEEE80211_S_RUN; /* stay RUN */
369178354Ssam				break;
370178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
371178354Ssam				ieee80211_sta_leave(ni);
372178354Ssam				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
373178354Ssam					/* try to reauth */
374178354Ssam					IEEE80211_SEND_MGMT(ni,
375178354Ssam					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
376178354Ssam				}
377178354Ssam				break;
378178354Ssam			}
379178354Ssam			break;
380178354Ssam		default:
381178354Ssam			goto invalid;
382178354Ssam		}
383178354Ssam		break;
384178354Ssam	case IEEE80211_S_ASSOC:
385178354Ssam		switch (ostate) {
386178354Ssam		case IEEE80211_S_AUTH:
387178354Ssam		case IEEE80211_S_ASSOC:
388178354Ssam			IEEE80211_SEND_MGMT(ni,
389178354Ssam			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
390178354Ssam			break;
391178354Ssam		case IEEE80211_S_SLEEP:		/* cannot happen */
392178354Ssam		case IEEE80211_S_RUN:
393178354Ssam			ieee80211_sta_leave(ni);
394178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
395178354Ssam				IEEE80211_SEND_MGMT(ni, arg ?
396178354Ssam				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
397178354Ssam				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
398178354Ssam			}
399178354Ssam			break;
400178354Ssam		default:
401178354Ssam			goto invalid;
402178354Ssam		}
403178354Ssam		break;
404178354Ssam	case IEEE80211_S_RUN:
405178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA) {
406178354Ssam			/* XXX validate prerequisites */
407178354Ssam		}
408178354Ssam		switch (ostate) {
409178354Ssam		case IEEE80211_S_RUN:
410193439Ssam		case IEEE80211_S_CSA:
411178354Ssam			break;
412178354Ssam		case IEEE80211_S_AUTH:		/* when join is done in fw */
413178354Ssam		case IEEE80211_S_ASSOC:
414178354Ssam#ifdef IEEE80211_DEBUG
415178354Ssam			if (ieee80211_msg_debug(vap)) {
416178354Ssam				ieee80211_note(vap, "%s with %s ssid ",
417178354Ssam				    (vap->iv_opmode == IEEE80211_M_STA ?
418178354Ssam				    "associated" : "synchronized"),
419178354Ssam				    ether_sprintf(ni->ni_bssid));
420178354Ssam				ieee80211_print_essid(vap->iv_bss->ni_essid,
421178354Ssam				    ni->ni_esslen);
422178354Ssam				/* XXX MCS/HT */
423178354Ssam				printf(" channel %d start %uMb\n",
424178354Ssam				    ieee80211_chan2ieee(ic, ic->ic_curchan),
425178354Ssam				    IEEE80211_RATE2MBS(ni->ni_txrate));
426178354Ssam			}
427178354Ssam#endif
428178354Ssam			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
429178354Ssam			ieee80211_notify_node_join(ni,
430178354Ssam			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
431178354Ssam			break;
432178354Ssam		case IEEE80211_S_SLEEP:
433264855Sadrian			/* Wake up from sleep */
434241138Sadrian			vap->iv_sta_ps(vap, 0);
435178354Ssam			break;
436178354Ssam		default:
437178354Ssam			goto invalid;
438178354Ssam		}
439178354Ssam		ieee80211_sync_curchan(ic);
440297164Savos		if (ostate != IEEE80211_S_RUN)
441297164Savos			sta_swbmiss_start(vap);
442178354Ssam		/*
443178354Ssam		 * When 802.1x is not in use mark the port authorized
444178354Ssam		 * at this point so traffic can flow.
445178354Ssam		 */
446178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
447178354Ssam			ieee80211_node_authorize(ni);
448184345Ssam		/*
449184345Ssam		 * Fake association when joining an existing bss.
450264855Sadrian		 *
451264855Sadrian		 * Don't do this if we're doing SLEEP->RUN.
452184345Ssam		 */
453264855Sadrian		if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
454264855Sadrian			ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
455178354Ssam		break;
456193439Ssam	case IEEE80211_S_CSA:
457193439Ssam		if (ostate != IEEE80211_S_RUN)
458193439Ssam			goto invalid;
459193439Ssam		break;
460178354Ssam	case IEEE80211_S_SLEEP:
461297164Savos		sta_swbmiss_start(vap);
462241138Sadrian		vap->iv_sta_ps(vap, 1);
463178354Ssam		break;
464178354Ssam	default:
465178354Ssam	invalid:
466184268Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
467184268Ssam		    "%s: unexpected state transition %s -> %s\n", __func__,
468178354Ssam		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
469178354Ssam		break;
470178354Ssam	}
471178354Ssam	return 0;
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 DSTODS.
477178354Ssam */
478178354Ssamstatic __inline int
479178354Ssamisdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
480178354Ssam{
481178354Ssam#define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
482178354Ssam#define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
483178354Ssam	const uint8_t *sa;
484178354Ssam
485178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
486178354Ssam
487178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
488178354Ssam		return 0;
489178354Ssam	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
490178354Ssam	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
491178354Ssam#undef WH4
492178354Ssam#undef QWH4
493178354Ssam}
494178354Ssam
495178354Ssam/*
496178354Ssam * Return non-zero if the frame is an echo of a multicast
497178354Ssam * frame sent by ourself.  The dir is known to be FROMDS.
498178354Ssam */
499178354Ssamstatic __inline int
500178354Ssamisfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
501178354Ssam{
502178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
503178354Ssam
504178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
505178354Ssam		return 0;
506178354Ssam	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
507178354Ssam}
508178354Ssam
509178354Ssam/*
510178354Ssam * Decide if a received management frame should be
511178354Ssam * printed when debugging is enabled.  This filters some
512178354Ssam * of the less interesting frames that come frequently
513178354Ssam * (e.g. beacons).
514178354Ssam */
515178354Ssamstatic __inline int
516178354Ssamdoprint(struct ieee80211vap *vap, int subtype)
517178354Ssam{
518178354Ssam	switch (subtype) {
519178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
520178354Ssam		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
521178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
522178354Ssam		return 0;
523178354Ssam	}
524178354Ssam	return 1;
525178354Ssam}
526178354Ssam
527178354Ssam/*
528178354Ssam * Process a received frame.  The node associated with the sender
529178354Ssam * should be supplied.  If nothing was found in the node table then
530178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
531178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
532178354Ssam * during AP scanning to select a AP to associate with; it can have
533178354Ssam * any units so long as values have consistent units and higher values
534178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
535178354Ssam * by the 802.11 layer.
536178354Ssam */
537178354Ssamstatic int
538283535Sadriansta_input(struct ieee80211_node *ni, struct mbuf *m,
539283535Sadrian    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
540178354Ssam{
541178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
542178354Ssam	struct ieee80211com *ic = ni->ni_ic;
543178354Ssam	struct ifnet *ifp = vap->iv_ifp;
544178354Ssam	struct ieee80211_frame *wh;
545178354Ssam	struct ieee80211_key *key;
546178354Ssam	struct ether_header *eh;
547203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
548178354Ssam	uint8_t dir, type, subtype, qos;
549178354Ssam	uint8_t *bssid;
550178354Ssam
551183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
552178354Ssam		/*
553178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
554183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
555183247Ssam		 * here but were received out of order and been held on
556183247Ssam		 * the reorder queue.  When resubmitted they are marked
557183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
558183247Ssam		 * the normal processing.
559178354Ssam		 */
560178354Ssam		wh = mtod(m, struct ieee80211_frame *);
561178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
562178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
563178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
564178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
565178354Ssam		goto resubmit_ampdu;
566178354Ssam	}
567178354Ssam
568178354Ssam	KASSERT(ni != NULL, ("null node"));
569178354Ssam	ni->ni_inact = ni->ni_inact_reload;
570178354Ssam
571178354Ssam	type = -1;			/* undefined */
572178354Ssam
573178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
574178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
575178354Ssam		    ni->ni_macaddr, NULL,
576178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
577178354Ssam		vap->iv_stats.is_rx_tooshort++;
578178354Ssam		goto out;
579178354Ssam	}
580178354Ssam	/*
581178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
582178354Ssam	 * frame format but don't reference fields past outside
583178354Ssam	 * ieee80211_frame_min w/o first validating the data is
584178354Ssam	 * present.
585178354Ssam	 */
586178354Ssam	wh = mtod(m, struct ieee80211_frame *);
587178354Ssam
588178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
589178354Ssam	    IEEE80211_FC0_VERSION_0) {
590178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
591191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
592191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
593178354Ssam		vap->iv_stats.is_rx_badversion++;
594178354Ssam		goto err;
595178354Ssam	}
596178354Ssam
597178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
598178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
599178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
600178354Ssam	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
601178354Ssam		bssid = wh->i_addr2;
602178354Ssam		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
603178354Ssam			/* not interested in */
604178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
605178354Ssam			    bssid, NULL, "%s", "not to bss");
606178354Ssam			vap->iv_stats.is_rx_wrongbss++;
607178354Ssam			goto out;
608178354Ssam		}
609227338Sadrian
610227338Sadrian		/*
611227338Sadrian		 * Some devices may be in a promiscuous mode
612227338Sadrian		 * where they receive frames for multiple station
613227338Sadrian		 * addresses.
614227338Sadrian		 *
615227338Sadrian		 * If we receive a data frame that isn't
616227338Sadrian		 * destined to our VAP MAC, drop it.
617227338Sadrian		 *
618227338Sadrian		 * XXX TODO: This is only enforced when not scanning;
619227338Sadrian		 * XXX it assumes a software-driven scan will put the NIC
620227338Sadrian		 * XXX into a "no data frames" mode before setting this
621227338Sadrian		 * XXX flag. Otherwise it may be possible that we'll still
622227338Sadrian		 * XXX process data frames whilst scanning.
623227338Sadrian		 */
624227338Sadrian		if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
625227338Sadrian		    && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
626227338Sadrian			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
627227338Sadrian			    bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
628227338Sadrian			    IF_LLADDR(ifp), ":", wh->i_addr1, ":");
629227338Sadrian			vap->iv_stats.is_rx_wrongbss++;
630227338Sadrian			goto out;
631227338Sadrian		}
632227338Sadrian
633178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
634192468Ssam		ni->ni_noise = nf;
635282820Sadrian		if ( IEEE80211_HAS_SEQ(type, subtype) &&
636282820Sadrian		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
637178354Ssam			uint8_t tid = ieee80211_gettid(wh);
638178354Ssam			if (IEEE80211_QOS_HAS_SEQ(wh) &&
639178354Ssam			    TID_TO_WME_AC(tid) >= WME_AC_VI)
640178354Ssam				ic->ic_wme.wme_hipri_traffic++;
641296254Savos			if (! ieee80211_check_rxseq(ni, wh, bssid))
642178354Ssam				goto out;
643178354Ssam		}
644178354Ssam	}
645178354Ssam
646178354Ssam	switch (type) {
647178354Ssam	case IEEE80211_FC0_TYPE_DATA:
648178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
649178354Ssam		if (m->m_len < hdrspace &&
650178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
651178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
652178354Ssam			    ni->ni_macaddr, NULL,
653178354Ssam			    "data too short: expecting %u", hdrspace);
654178354Ssam			vap->iv_stats.is_rx_tooshort++;
655178354Ssam			goto out;		/* XXX */
656178354Ssam		}
657178354Ssam		/*
658183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
659183247Ssam		 * processed directly then ieee80211_ampdu_reorder
660178354Ssam		 * will return 0; otherwise it has consumed the mbuf
661178354Ssam		 * and we should do nothing more with it.
662178354Ssam		 */
663183247Ssam		if ((m->m_flags & M_AMPDU) &&
664178354Ssam		    (dir == IEEE80211_FC1_DIR_FROMDS ||
665178354Ssam		     dir == IEEE80211_FC1_DIR_DSTODS) &&
666178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
667178354Ssam			m = NULL;
668178354Ssam			goto out;
669178354Ssam		}
670178354Ssam	resubmit_ampdu:
671178354Ssam		if (dir == IEEE80211_FC1_DIR_FROMDS) {
672178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
673178354Ssam			    isfromds_mcastecho(vap, wh)) {
674178354Ssam				/*
675178354Ssam				 * In IEEE802.11 network, multicast
676178354Ssam				 * packets sent from "me" are broadcast
677178354Ssam				 * from the AP; silently discard for
678178354Ssam				 * SIMPLEX interface.
679178354Ssam				 */
680178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
681178354Ssam				    wh, "data", "%s", "multicast echo");
682178354Ssam				vap->iv_stats.is_rx_mcastecho++;
683178354Ssam				goto out;
684178354Ssam			}
685178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
686178354Ssam			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
687178354Ssam				/*
688178354Ssam				 * DWDS sta's must drop 3-address mcast frames
689178354Ssam				 * as they will be sent separately as a 4-addr
690178354Ssam				 * frame.  Accepting the 3-addr frame will
691178354Ssam				 * confuse the bridge into thinking the sending
692178354Ssam				 * sta is located at the end of WDS link.
693178354Ssam				 */
694178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
695178354Ssam				    "3-address data", "%s", "DWDS enabled");
696178354Ssam				vap->iv_stats.is_rx_mcastecho++;
697178354Ssam				goto out;
698178354Ssam			}
699178354Ssam		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
700178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
701178354Ssam				IEEE80211_DISCARD(vap,
702178354Ssam				    IEEE80211_MSG_INPUT, wh, "4-address data",
703178354Ssam				    "%s", "DWDS not enabled");
704178354Ssam				vap->iv_stats.is_rx_wrongdir++;
705178354Ssam				goto out;
706178354Ssam			}
707178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
708178354Ssam			    isdstods_mcastecho(vap, wh)) {
709178354Ssam				/*
710178354Ssam				 * In IEEE802.11 network, multicast
711178354Ssam				 * packets sent from "me" are broadcast
712178354Ssam				 * from the AP; silently discard for
713178354Ssam				 * SIMPLEX interface.
714178354Ssam				 */
715178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
716178354Ssam				    "4-address data", "%s", "multicast echo");
717178354Ssam				vap->iv_stats.is_rx_mcastecho++;
718178354Ssam				goto out;
719178354Ssam			}
720178354Ssam		} else {
721178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
722178354Ssam			    "data", "incorrect dir 0x%x", dir);
723178354Ssam			vap->iv_stats.is_rx_wrongdir++;
724178354Ssam			goto out;
725178354Ssam		}
726178354Ssam
727178354Ssam		/*
728178354Ssam		 * Handle privacy requirements.  Note that we
729178354Ssam		 * must not be preempted from here until after
730178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
731178354Ssam		 * otherwise we may violate assumptions in the
732178354Ssam		 * crypto cipher modules used to do delayed update
733178354Ssam		 * of replay sequence numbers.
734178354Ssam		 */
735260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
736178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
737178354Ssam				/*
738178354Ssam				 * Discard encrypted frames when privacy is off.
739178354Ssam				 */
740178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
741178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
742178354Ssam				vap->iv_stats.is_rx_noprivacy++;
743178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
744178354Ssam				goto out;
745178354Ssam			}
746178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
747178354Ssam			if (key == NULL) {
748178354Ssam				/* NB: stats+msgs handled in crypto_decap */
749178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
750178354Ssam				goto out;
751178354Ssam			}
752178354Ssam			wh = mtod(m, struct ieee80211_frame *);
753260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
754178354Ssam		} else {
755178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
756178354Ssam			key = NULL;
757178354Ssam		}
758178354Ssam
759178354Ssam		/*
760178354Ssam		 * Save QoS bits for use below--before we strip the header.
761178354Ssam		 */
762178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
763178354Ssam			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
764178354Ssam			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
765178354Ssam			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
766178354Ssam		} else
767178354Ssam			qos = 0;
768178354Ssam
769178354Ssam		/*
770178354Ssam		 * Next up, any fragmentation.
771178354Ssam		 */
772178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
773178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
774178354Ssam			if (m == NULL) {
775178354Ssam				/* Fragment dropped or frame not complete yet */
776178354Ssam				goto out;
777178354Ssam			}
778178354Ssam		}
779178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
780178354Ssam
781178354Ssam		/*
782178354Ssam		 * Next strip any MSDU crypto bits.
783178354Ssam		 */
784178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
785178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
786178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
787178354Ssam			vap->iv_stats.is_rx_demicfail++;
788178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
789178354Ssam			goto out;
790178354Ssam		}
791178354Ssam
792178354Ssam		/* copy to listener after decrypt */
793192468Ssam		if (ieee80211_radiotap_active_vap(vap))
794202967Srpaulo			ieee80211_radiotap_rx(vap, m);
795178354Ssam		need_tap = 0;
796178354Ssam
797178354Ssam		/*
798178354Ssam		 * Finally, strip the 802.11 header.
799178354Ssam		 */
800178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
801178354Ssam		if (m == NULL) {
802178354Ssam			/* XXX mask bit to check for both */
803178354Ssam			/* don't count Null data frames as errors */
804178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
805178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
806178354Ssam				goto out;
807178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
808178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
809178354Ssam			vap->iv_stats.is_rx_decap++;
810178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
811178354Ssam			goto err;
812178354Ssam		}
813178354Ssam		eh = mtod(m, struct ether_header *);
814178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
815178354Ssam			/*
816178354Ssam			 * Deny any non-PAE frames received prior to
817178354Ssam			 * authorization.  For open/shared-key
818178354Ssam			 * authentication the port is mark authorized
819178354Ssam			 * after authentication completes.  For 802.1x
820178354Ssam			 * the port is not marked authorized by the
821178354Ssam			 * authenticator until the handshake has completed.
822178354Ssam			 */
823178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
824178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
825178354Ssam				    eh->ether_shost, "data",
826178354Ssam				    "unauthorized port: ether type 0x%x len %u",
827178354Ssam				    eh->ether_type, m->m_pkthdr.len);
828178354Ssam				vap->iv_stats.is_rx_unauth++;
829178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
830178354Ssam				goto err;
831178354Ssam			}
832178354Ssam		} else {
833178354Ssam			/*
834178354Ssam			 * When denying unencrypted frames, discard
835178354Ssam			 * any non-PAE frames received without encryption.
836178354Ssam			 */
837178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
838178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
839178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
840178354Ssam				/*
841178354Ssam				 * Drop unencrypted frames.
842178354Ssam				 */
843178354Ssam				vap->iv_stats.is_rx_unencrypted++;
844178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
845178354Ssam				goto out;
846178354Ssam			}
847178354Ssam		}
848178354Ssam		/* XXX require HT? */
849178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
850178354Ssam			m = ieee80211_decap_amsdu(ni, m);
851178354Ssam			if (m == NULL)
852178354Ssam				return IEEE80211_FC0_TYPE_DATA;
853190391Ssam		} else {
854190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
855190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
856190391Ssam			if (m == NULL)
857178354Ssam				return IEEE80211_FC0_TYPE_DATA;
858190391Ssam#endif
859178354Ssam		}
860178354Ssam		ieee80211_deliver_data(vap, ni, m);
861178354Ssam		return IEEE80211_FC0_TYPE_DATA;
862178354Ssam
863178354Ssam	case IEEE80211_FC0_TYPE_MGT:
864178354Ssam		vap->iv_stats.is_rx_mgmt++;
865178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
866178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
867178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
868178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
869178354Ssam			vap->iv_stats.is_rx_wrongdir++;
870178354Ssam			goto err;
871178354Ssam		}
872178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
873178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
874178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
875178354Ssam			    m->m_pkthdr.len);
876178354Ssam			vap->iv_stats.is_rx_tooshort++;
877178354Ssam			goto out;
878178354Ssam		}
879178354Ssam#ifdef IEEE80211_DEBUG
880178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
881178354Ssam		    ieee80211_msg_dumppkts(vap)) {
882178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
883178354Ssam			    ieee80211_mgt_subtype_name[subtype >>
884178354Ssam				IEEE80211_FC0_SUBTYPE_SHIFT],
885178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
886178354Ssam		}
887178354Ssam#endif
888260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
889178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
890178354Ssam				/*
891178354Ssam				 * Only shared key auth frames with a challenge
892178354Ssam				 * should be encrypted, discard all others.
893178354Ssam				 */
894178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
895178354Ssam				    wh, ieee80211_mgt_subtype_name[subtype >>
896178354Ssam					IEEE80211_FC0_SUBTYPE_SHIFT],
897178354Ssam				    "%s", "WEP set but not permitted");
898178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
899178354Ssam				goto out;
900178354Ssam			}
901178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
902178354Ssam				/*
903178354Ssam				 * Discard encrypted frames when privacy is off.
904178354Ssam				 */
905178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
906178354Ssam				    wh, "mgt", "%s", "WEP set but PRIVACY off");
907178354Ssam				vap->iv_stats.is_rx_noprivacy++;
908178354Ssam				goto out;
909178354Ssam			}
910178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
911178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
912178354Ssam			if (key == NULL) {
913178354Ssam				/* NB: stats+msgs handled in crypto_decap */
914178354Ssam				goto out;
915178354Ssam			}
916178354Ssam			wh = mtod(m, struct ieee80211_frame *);
917260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
918178354Ssam		}
919283535Sadrian		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
920191534Ssam		goto out;
921178354Ssam
922178354Ssam	case IEEE80211_FC0_TYPE_CTL:
923178354Ssam		vap->iv_stats.is_rx_ctl++;
924178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
925191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
926178354Ssam		goto out;
927191549Ssam
928178354Ssam	default:
929178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
930178354Ssam		    wh, NULL, "bad frame type 0x%x", type);
931178354Ssam		/* should not come here */
932178354Ssam		break;
933178354Ssam	}
934178354Ssamerr:
935271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
936178354Ssamout:
937178354Ssam	if (m != NULL) {
938192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
939192468Ssam			ieee80211_radiotap_rx(vap, m);
940178354Ssam		m_freem(m);
941178354Ssam	}
942178354Ssam	return type;
943178354Ssam}
944178354Ssam
945178354Ssamstatic void
946178354Ssamsta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
947192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
948178354Ssam{
949178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
950178354Ssam
951178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
952178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
953178354Ssam		    ni->ni_macaddr, "open auth",
954178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
955178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
956178354Ssam		return;
957178354Ssam	}
958178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH ||
959178354Ssam	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
960178354Ssam		vap->iv_stats.is_rx_bad_auth++;
961178354Ssam		return;
962178354Ssam	}
963178354Ssam	if (status != 0) {
964178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
965178354Ssam		    ni, "open auth failed (reason %d)", status);
966178354Ssam		vap->iv_stats.is_rx_auth_fail++;
967178354Ssam		vap->iv_stats.is_rx_authfail_code = status;
968178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
969178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
970178354Ssam	} else
971178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
972178354Ssam}
973178354Ssam
974178354Ssamstatic void
975178354Ssamsta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
976192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
977178354Ssam    uint16_t seq, uint16_t status)
978178354Ssam{
979178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
980178354Ssam	uint8_t *challenge;
981178354Ssam	int estatus;
982178354Ssam
983178354Ssam	/*
984178354Ssam	 * NB: this can happen as we allow pre-shared key
985178354Ssam	 * authentication to be enabled w/o wep being turned
986178354Ssam	 * on so that configuration of these can be done
987178354Ssam	 * in any order.  It may be better to enforce the
988178354Ssam	 * ordering in which case this check would just be
989178354Ssam	 * for sanity/consistency.
990178354Ssam	 */
991178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
992178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
993178354Ssam		    ni->ni_macaddr, "shared key auth",
994178354Ssam		    "%s", " PRIVACY is disabled");
995178354Ssam		estatus = IEEE80211_STATUS_ALG;
996178354Ssam		goto bad;
997178354Ssam	}
998178354Ssam	/*
999178354Ssam	 * Pre-shared key authentication is evil; accept
1000178354Ssam	 * it only if explicitly configured (it is supported
1001178354Ssam	 * mainly for compatibility with clients like OS X).
1002178354Ssam	 */
1003178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1004178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1005178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1006178354Ssam		    ni->ni_macaddr, "shared key auth",
1007178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1008178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1009178354Ssam		estatus = IEEE80211_STATUS_ALG;
1010178354Ssam		goto bad;
1011178354Ssam	}
1012178354Ssam
1013178354Ssam	challenge = NULL;
1014178354Ssam	if (frm + 1 < efrm) {
1015178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1016178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1017178354Ssam			    ni->ni_macaddr, "shared key auth",
1018178354Ssam			    "ie %d/%d too long",
1019178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1020178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1021178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1022178354Ssam			goto bad;
1023178354Ssam		}
1024178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1025178354Ssam			challenge = frm;
1026178354Ssam		frm += frm[1] + 2;
1027178354Ssam	}
1028178354Ssam	switch (seq) {
1029178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1030178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1031178354Ssam		if (challenge == NULL) {
1032178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1033178354Ssam			    ni->ni_macaddr, "shared key auth",
1034178354Ssam			    "%s", "no challenge");
1035178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1036178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1037178354Ssam			goto bad;
1038178354Ssam		}
1039178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1040178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1041178354Ssam			    ni->ni_macaddr, "shared key auth",
1042178354Ssam			    "bad challenge len %d", challenge[1]);
1043178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1044178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1045178354Ssam			goto bad;
1046178354Ssam		}
1047178354Ssam	default:
1048178354Ssam		break;
1049178354Ssam	}
1050178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH)
1051178354Ssam		return;
1052178354Ssam	switch (seq) {
1053178354Ssam	case IEEE80211_AUTH_SHARED_PASS:
1054178354Ssam		if (ni->ni_challenge != NULL) {
1055283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1056178354Ssam			ni->ni_challenge = NULL;
1057178354Ssam		}
1058178354Ssam		if (status != 0) {
1059178354Ssam			IEEE80211_NOTE_FRAME(vap,
1060178354Ssam			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1061178354Ssam			    "shared key auth failed (reason %d)", status);
1062178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1063178354Ssam			vap->iv_stats.is_rx_authfail_code = status;
1064178354Ssam			return;
1065178354Ssam		}
1066178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1067178354Ssam		break;
1068178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1069178354Ssam		if (!ieee80211_alloc_challenge(ni))
1070178354Ssam			return;
1071178354Ssam		/* XXX could optimize by passing recvd challenge */
1072178354Ssam		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1073178354Ssam		IEEE80211_SEND_MGMT(ni,
1074178354Ssam			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1075178354Ssam		break;
1076178354Ssam	default:
1077178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1078178354Ssam		    wh, "shared key auth", "bad seq %d", seq);
1079178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1080178354Ssam		return;
1081178354Ssam	}
1082178354Ssam	return;
1083178354Ssambad:
1084178354Ssam	/*
1085178354Ssam	 * Kick the state machine.  This short-circuits
1086178354Ssam	 * using the mgt frame timeout to trigger the
1087178354Ssam	 * state transition.
1088178354Ssam	 */
1089178354Ssam	if (vap->iv_state == IEEE80211_S_AUTH)
1090178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1091178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
1092178354Ssam}
1093178354Ssam
1094244060Sadrianint
1095178354Ssamieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1096178354Ssam	const struct ieee80211_frame *wh)
1097178354Ssam{
1098178354Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1099178354Ssam	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1100178354Ssam	u_int len = frm[1], qosinfo;
1101178354Ssam	int i;
1102178354Ssam
1103178354Ssam	if (len < sizeof(struct ieee80211_wme_param)-2) {
1104178354Ssam		IEEE80211_DISCARD_IE(vap,
1105178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1106178354Ssam		    wh, "WME", "too short, len %u", len);
1107178354Ssam		return -1;
1108178354Ssam	}
1109178354Ssam	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1110178354Ssam	qosinfo &= WME_QOSINFO_COUNT;
1111178354Ssam	/* XXX do proper check for wraparound */
1112178354Ssam	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1113178354Ssam		return 0;
1114178354Ssam	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1115178354Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1116178354Ssam		struct wmeParams *wmep =
1117178354Ssam			&wme->wme_wmeChanParams.cap_wmeParams[i];
1118178354Ssam		/* NB: ACI not used */
1119178354Ssam		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1120178354Ssam		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1121178354Ssam		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1122178354Ssam		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1123178354Ssam		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1124178354Ssam		frm += 4;
1125178354Ssam	}
1126178354Ssam	wme->wme_wmeChanParams.cap_info = qosinfo;
1127178354Ssam	return 1;
1128178354Ssam#undef MS
1129178354Ssam}
1130178354Ssam
1131178354Ssam/*
1132193439Ssam * Process 11h Channel Switch Announcement (CSA) ie.  If this
1133193439Ssam * is the first CSA then initiate the switch.  Otherwise we
1134193439Ssam * track state and trigger completion and/or cancel of the switch.
1135193439Ssam * XXX should be public for IBSS use
1136193439Ssam */
1137193439Ssamstatic void
1138193439Ssamieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1139193439Ssam	const struct ieee80211_frame *wh)
1140193439Ssam{
1141193439Ssam	struct ieee80211com *ic = vap->iv_ic;
1142193439Ssam	const struct ieee80211_csa_ie *csa =
1143193439Ssam	    (const struct ieee80211_csa_ie *) frm;
1144193439Ssam
1145193439Ssam	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1146193439Ssam	    ("state %s", ieee80211_state_name[vap->iv_state]));
1147193439Ssam
1148193439Ssam	if (csa->csa_mode > 1) {
1149193439Ssam		IEEE80211_DISCARD_IE(vap,
1150193439Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1151193439Ssam		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1152193439Ssam		return;
1153193439Ssam	}
1154193439Ssam	IEEE80211_LOCK(ic);
1155193439Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1156193439Ssam		/*
1157193439Ssam		 * Convert the channel number to a channel reference.  We
1158193439Ssam		 * try first to preserve turbo attribute of the current
1159193439Ssam		 * channel then fallback.  Note this will not work if the
1160193439Ssam		 * CSA specifies a channel that requires a band switch (e.g.
1161193439Ssam		 * 11a => 11g).  This is intentional as 11h is defined only
1162193439Ssam		 * for 5GHz/11a and because the switch does not involve a
1163193439Ssam		 * reassociation, protocol state (capabilities, negotated
1164193439Ssam		 * rates, etc) may/will be wrong.
1165193439Ssam		 */
1166193439Ssam		struct ieee80211_channel *c =
1167193439Ssam		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1168193439Ssam			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1169193439Ssam		if (c == NULL) {
1170193439Ssam			c = ieee80211_find_channel_byieee(ic,
1171193439Ssam			    csa->csa_newchan,
1172193439Ssam			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1173193439Ssam			if (c == NULL) {
1174193439Ssam				IEEE80211_DISCARD_IE(vap,
1175193439Ssam				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1176193439Ssam				    wh, "CSA", "invalid channel %u",
1177193439Ssam				    csa->csa_newchan);
1178193439Ssam				goto done;
1179193439Ssam			}
1180193439Ssam		}
1181193439Ssam#if IEEE80211_CSA_COUNT_MIN > 0
1182193439Ssam		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1183193439Ssam			/*
1184193439Ssam			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1185193439Ssam			 * reduce the risk of being redirected by a fabricated
1186193439Ssam			 * CSA.  If a valid CSA is dropped we'll still get a
1187193439Ssam			 * beacon miss when the AP leaves the channel so we'll
1188193439Ssam			 * eventually follow to the new channel.
1189193439Ssam			 *
1190193439Ssam			 * NOTE: this violates the 11h spec that states that
1191193439Ssam			 * count may be any value and if 0 then a switch
1192193439Ssam			 * should happen asap.
1193193439Ssam			 */
1194193439Ssam			IEEE80211_DISCARD_IE(vap,
1195193439Ssam			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1196193439Ssam			    wh, "CSA", "count %u too small, must be >= %u",
1197193439Ssam			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1198193439Ssam			goto done;
1199193439Ssam		}
1200193439Ssam#endif
1201193439Ssam		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1202193439Ssam	} else {
1203193439Ssam		/*
1204193439Ssam		 * Validate this ie against the initial CSA.  We require
1205193439Ssam		 * mode and channel not change and the count must be
1206193439Ssam		 * monotonically decreasing.  This may be pointless and
1207193439Ssam		 * canceling the switch as a result may be too paranoid but
1208193439Ssam		 * in the worst case if we drop out of CSA because of this
1209193439Ssam		 * and the AP does move then we'll just end up taking a
1210193439Ssam		 * beacon miss and scan to find the AP.
1211193439Ssam		 *
1212193439Ssam		 * XXX may want <= on count as we also process ProbeResp
1213193439Ssam		 * frames and those may come in w/ the same count as the
1214193439Ssam		 * previous beacon; but doing so leaves us open to a stuck
1215193439Ssam		 * count until we add a dead-man timer
1216193439Ssam		 */
1217193439Ssam		if (!(csa->csa_count < ic->ic_csa_count &&
1218193439Ssam		      csa->csa_mode == ic->ic_csa_mode &&
1219193439Ssam		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1220193439Ssam			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1221193439Ssam			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1222193439Ssam			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1223193439Ssam			    ic->ic_csa_newchan, ic->ic_csa_count,
1224193439Ssam			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1225193439Ssam			ieee80211_csa_cancelswitch(ic);
1226193439Ssam		} else {
1227193439Ssam			if (csa->csa_count <= 1)
1228193439Ssam				ieee80211_csa_completeswitch(ic);
1229193439Ssam			else
1230193439Ssam				ic->ic_csa_count = csa->csa_count;
1231193439Ssam		}
1232193439Ssam	}
1233193439Ssamdone:
1234193439Ssam	IEEE80211_UNLOCK(ic);
1235193439Ssam}
1236193439Ssam
1237193439Ssam/*
1238178354Ssam * Return non-zero if a background scan may be continued:
1239178354Ssam * o bg scan is active
1240178354Ssam * o no channel switch is pending
1241178354Ssam * o there has not been any traffic recently
1242178354Ssam *
1243178354Ssam * Note we do not check if there is an administrative enable;
1244178354Ssam * this is only done to start the scan.  We assume that any
1245178354Ssam * change in state will be accompanied by a request to cancel
1246178354Ssam * active scans which will otherwise cause this test to fail.
1247178354Ssam */
1248178354Ssamstatic __inline int
1249178354Ssamcontbgscan(struct ieee80211vap *vap)
1250178354Ssam{
1251178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1252178354Ssam
1253178354Ssam	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1254178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1255178354Ssam	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1256297405Sadrian	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1257178354Ssam}
1258178354Ssam
1259178354Ssam/*
1260178354Ssam * Return non-zero if a backgrond scan may be started:
1261178354Ssam * o bg scanning is administratively enabled
1262178354Ssam * o no channel switch is pending
1263178354Ssam * o we are not boosted on a dynamic turbo channel
1264178354Ssam * o there has not been a scan recently
1265178354Ssam * o there has not been any traffic recently
1266178354Ssam */
1267178354Ssamstatic __inline int
1268178354Ssamstartbgscan(struct ieee80211vap *vap)
1269178354Ssam{
1270178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1271178354Ssam
1272178354Ssam	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1273178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1274190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1275178354Ssam	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1276190391Ssam#endif
1277297405Sadrian	    ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1278297405Sadrian	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1279178354Ssam}
1280178354Ssam
1281178354Ssamstatic void
1282283535Sadriansta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1283283535Sadrian    const struct ieee80211_rx_stats *rxs,
1284283535Sadrian    int rssi, int nf)
1285178354Ssam{
1286178354Ssam#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1287178354Ssam#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1288178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1289178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1290283535Sadrian	struct ieee80211_channel *rxchan = ic->ic_curchan;
1291178354Ssam	struct ieee80211_frame *wh;
1292178354Ssam	uint8_t *frm, *efrm;
1293178354Ssam	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1294178354Ssam	uint8_t rate;
1295233452Sadrian	int ht_state_change = 0;
1296178354Ssam
1297178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1298178354Ssam	frm = (uint8_t *)&wh[1];
1299178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1300178354Ssam	switch (subtype) {
1301178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1302178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON: {
1303178354Ssam		struct ieee80211_scanparams scan;
1304283535Sadrian		struct ieee80211_channel *c;
1305178354Ssam		/*
1306178354Ssam		 * We process beacon/probe response frames:
1307178354Ssam		 *    o when scanning, or
1308178354Ssam		 *    o station mode when associated (to collect state
1309191444Srpaulo		 *      updates such as 802.11g slot time)
1310178354Ssam		 * Frames otherwise received are discarded.
1311178354Ssam		 */
1312178354Ssam		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1313178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1314178354Ssam			return;
1315178354Ssam		}
1316283535Sadrian
1317283535Sadrian		/* Override RX channel as appropriate */
1318283535Sadrian		if (rxs != NULL) {
1319283535Sadrian			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1320283535Sadrian			if (c != NULL)
1321283535Sadrian				rxchan = c;
1322283535Sadrian		}
1323283535Sadrian
1324178354Ssam		/* XXX probe response in sta mode when !scanning? */
1325283535Sadrian		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1326232270Sadrian			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1327232270Sadrian				vap->iv_stats.is_beacon_bad++;
1328178354Ssam			return;
1329232244Sadrian		}
1330264855Sadrian
1331178354Ssam		/*
1332178354Ssam		 * Count frame now that we know it's to be processed.
1333178354Ssam		 */
1334178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1335178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1336178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1337178354Ssam		} else
1338178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1339178354Ssam		/*
1340178354Ssam		 * When operating in station mode, check for state updates.
1341178354Ssam		 * Be careful to ignore beacons received while doing a
1342178354Ssam		 * background scan.  We consider only 11g/WMM stuff right now.
1343178354Ssam		 */
1344178354Ssam		if (ni->ni_associd != 0 &&
1345178354Ssam		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1346178354Ssam		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1347178354Ssam			/* record tsf of last beacon */
1348178354Ssam			memcpy(ni->ni_tstamp.data, scan.tstamp,
1349178354Ssam				sizeof(ni->ni_tstamp));
1350178354Ssam			/* count beacon frame for s/w bmiss handling */
1351178354Ssam			vap->iv_swbmiss_count++;
1352178354Ssam			vap->iv_bmiss_count = 0;
1353178354Ssam			if (ni->ni_erp != scan.erp) {
1354178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1355178354Ssam				    wh->i_addr2,
1356178354Ssam				    "erp change: was 0x%x, now 0x%x",
1357178354Ssam				    ni->ni_erp, scan.erp);
1358178354Ssam				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1359178354Ssam				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1360178354Ssam					ic->ic_flags |= IEEE80211_F_USEPROT;
1361178354Ssam				else
1362178354Ssam					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1363178354Ssam				ni->ni_erp = scan.erp;
1364178354Ssam				/* XXX statistic */
1365178354Ssam				/* XXX driver notification */
1366178354Ssam			}
1367178354Ssam			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1368178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1369178354Ssam				    wh->i_addr2,
1370178354Ssam				    "capabilities change: was 0x%x, now 0x%x",
1371178354Ssam				    ni->ni_capinfo, scan.capinfo);
1372178354Ssam				/*
1373178354Ssam				 * NB: we assume short preamble doesn't
1374178354Ssam				 *     change dynamically
1375178354Ssam				 */
1376178354Ssam				ieee80211_set_shortslottime(ic,
1377178354Ssam					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1378178354Ssam					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1379178354Ssam				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1380178354Ssam					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1381178354Ssam				/* XXX statistic */
1382178354Ssam			}
1383178354Ssam			if (scan.wme != NULL &&
1384178354Ssam			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1385178354Ssam			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1386178354Ssam				ieee80211_wme_updateparams(vap);
1387190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1388178354Ssam			if (scan.ath != NULL)
1389178354Ssam				ieee80211_parse_athparams(ni, scan.ath, wh);
1390190391Ssam#endif
1391183254Ssam			if (scan.htcap != NULL && scan.htinfo != NULL &&
1392193655Ssam			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1393178354Ssam				/* XXX state changes? */
1394233452Sadrian				if (ieee80211_ht_updateparams(ni,
1395233452Sadrian				    scan.htcap, scan.htinfo))
1396233452Sadrian					ht_state_change = 1;
1397178354Ssam			}
1398227331Sadrian			if (scan.quiet)
1399227331Sadrian				ic->ic_set_quiet(ni, scan.quiet);
1400264855Sadrian
1401178354Ssam			if (scan.tim != NULL) {
1402178354Ssam				struct ieee80211_tim_ie *tim =
1403178354Ssam				    (struct ieee80211_tim_ie *) scan.tim;
1404264855Sadrian				/*
1405264855Sadrian				 * XXX Check/debug this code; see if it's about
1406264855Sadrian				 * the right time to force the VAP awake if we
1407264855Sadrian				 * receive a frame destined for us?
1408264855Sadrian				 */
1409178354Ssam				int aid = IEEE80211_AID(ni->ni_associd);
1410178354Ssam				int ix = aid / NBBY;
1411178354Ssam				int min = tim->tim_bitctl &~ 1;
1412178354Ssam				int max = tim->tim_len + min - 4;
1413275983Sadrian				int tim_ucast = 0, tim_mcast = 0;
1414264855Sadrian
1415264855Sadrian				/*
1416264855Sadrian				 * Only do this for unicast traffic in the TIM
1417264855Sadrian				 * The multicast traffic notification for
1418264855Sadrian				 * the scan notification stuff should occur
1419264855Sadrian				 * differently.
1420264855Sadrian				 */
1421264855Sadrian				if (min <= ix && ix <= max &&
1422264855Sadrian				     isset(tim->tim_bitmap - min, aid)) {
1423275983Sadrian					tim_ucast = 1;
1424264855Sadrian				}
1425264855Sadrian
1426264855Sadrian				/*
1427275983Sadrian				 * Do a separate notification
1428264855Sadrian				 * for the multicast bit being set.
1429264855Sadrian				 */
1430264906Sadrian				if (tim->tim_bitctl & 1) {
1431275983Sadrian					tim_mcast = 1;
1432275983Sadrian				}
1433275983Sadrian
1434275983Sadrian				/*
1435275983Sadrian				 * If the TIM indicates there's traffic for
1436275983Sadrian				 * us then get us out of STA mode powersave.
1437275983Sadrian				 */
1438275983Sadrian				if (tim_ucast == 1) {
1439275983Sadrian
1440275983Sadrian					/*
1441275983Sadrian					 * Wake us out of SLEEP state if we're
1442275983Sadrian					 * in it; and if we're doing bgscan
1443275983Sadrian					 * then wake us out of STA powersave.
1444275983Sadrian					 */
1445264855Sadrian					ieee80211_sta_tim_notify(vap, 1);
1446275983Sadrian
1447275983Sadrian					/*
1448275983Sadrian					 * This is preventing us from
1449275983Sadrian					 * continuing a bgscan; because it
1450275983Sadrian					 * tricks the contbgscan()
1451275983Sadrian					 * routine to think there's always
1452275983Sadrian					 * traffic for us.
1453275983Sadrian					 *
1454275983Sadrian					 * I think we need both an RX and
1455275983Sadrian					 * TX ic_lastdata field.
1456275983Sadrian					 */
1457178354Ssam					ic->ic_lastdata = ticks;
1458178354Ssam				}
1459264906Sadrian
1460178354Ssam				ni->ni_dtim_count = tim->tim_count;
1461178354Ssam				ni->ni_dtim_period = tim->tim_period;
1462178354Ssam			}
1463193439Ssam			if (scan.csa != NULL &&
1464193439Ssam			    (vap->iv_flags & IEEE80211_F_DOTH))
1465193439Ssam				ieee80211_parse_csaparams(vap, scan.csa, wh);
1466193439Ssam			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1467193439Ssam				/*
1468193439Ssam				 * No CSA ie or 11h disabled, but a channel
1469193439Ssam				 * switch is pending; drop out so we aren't
1470193439Ssam				 * stuck in CSA state.  If the AP really is
1471193439Ssam				 * moving we'll get a beacon miss and scan.
1472193439Ssam				 */
1473193439Ssam				IEEE80211_LOCK(ic);
1474193439Ssam				ieee80211_csa_cancelswitch(ic);
1475193439Ssam				IEEE80211_UNLOCK(ic);
1476193439Ssam			}
1477178354Ssam			/*
1478178354Ssam			 * If scanning, pass the info to the scan module.
1479178354Ssam			 * Otherwise, check if it's the right time to do
1480178354Ssam			 * a background scan.  Background scanning must
1481178354Ssam			 * be enabled and we must not be operating in the
1482178354Ssam			 * turbo phase of dynamic turbo mode.  Then,
1483178354Ssam			 * it's been a while since the last background
1484178354Ssam			 * scan and if no data frames have come through
1485178354Ssam			 * recently, kick off a scan.  Note that this
1486178354Ssam			 * is the mechanism by which a background scan
1487178354Ssam			 * is started _and_ continued each time we
1488178354Ssam			 * return on-channel to receive a beacon from
1489178354Ssam			 * our ap.
1490178354Ssam			 */
1491178354Ssam			if (ic->ic_flags & IEEE80211_F_SCAN) {
1492283535Sadrian				ieee80211_add_scan(vap, rxchan,
1493282742Sadrian				    &scan, wh, subtype, rssi, nf);
1494178354Ssam			} else if (contbgscan(vap)) {
1495178354Ssam				ieee80211_bg_scan(vap, 0);
1496178354Ssam			} else if (startbgscan(vap)) {
1497178354Ssam				vap->iv_stats.is_scan_bg++;
1498178354Ssam#if 0
1499178354Ssam				/* wakeup if we are sleeing */
1500178354Ssam				ieee80211_set_pwrsave(vap, 0);
1501178354Ssam#endif
1502178354Ssam				ieee80211_bg_scan(vap, 0);
1503178354Ssam			}
1504233452Sadrian
1505233452Sadrian			/*
1506264855Sadrian			 * Put the station to sleep if we haven't seen
1507264855Sadrian			 * traffic in a while.
1508264855Sadrian			 */
1509264855Sadrian			IEEE80211_LOCK(ic);
1510264855Sadrian			ieee80211_sta_ps_timer_check(vap);
1511264855Sadrian			IEEE80211_UNLOCK(ic);
1512264855Sadrian
1513264855Sadrian			/*
1514233452Sadrian			 * If we've had a channel width change (eg HT20<->HT40)
1515233452Sadrian			 * then schedule a delayed driver notification.
1516233452Sadrian			 */
1517233452Sadrian			if (ht_state_change)
1518233452Sadrian				ieee80211_update_chw(ic);
1519178354Ssam			return;
1520178354Ssam		}
1521178354Ssam		/*
1522178354Ssam		 * If scanning, just pass information to the scan module.
1523178354Ssam		 */
1524178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1525178354Ssam			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1526178354Ssam				/*
1527178354Ssam				 * Actively scanning a channel marked passive;
1528178354Ssam				 * send a probe request now that we know there
1529178354Ssam				 * is 802.11 traffic present.
1530178354Ssam				 *
1531178354Ssam				 * XXX check if the beacon we recv'd gives
1532178354Ssam				 * us what we need and suppress the probe req
1533178354Ssam				 */
1534178354Ssam				ieee80211_probe_curchan(vap, 1);
1535178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1536178354Ssam			}
1537283535Sadrian			ieee80211_add_scan(vap, rxchan, &scan, wh,
1538282742Sadrian			    subtype, rssi, nf);
1539178354Ssam			return;
1540178354Ssam		}
1541178354Ssam		break;
1542178354Ssam	}
1543178354Ssam
1544178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH: {
1545178354Ssam		uint16_t algo, seq, status;
1546178354Ssam		/*
1547178354Ssam		 * auth frame format
1548178354Ssam		 *	[2] algorithm
1549178354Ssam		 *	[2] sequence
1550178354Ssam		 *	[2] status
1551178354Ssam		 *	[tlv*] challenge
1552178354Ssam		 */
1553178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1554178354Ssam		algo   = le16toh(*(uint16_t *)frm);
1555178354Ssam		seq    = le16toh(*(uint16_t *)(frm + 2));
1556178354Ssam		status = le16toh(*(uint16_t *)(frm + 4));
1557178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1558178354Ssam		    "recv auth frame with algorithm %d seq %d", algo, seq);
1559178354Ssam
1560178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1561178354Ssam			IEEE80211_DISCARD(vap,
1562178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1563178354Ssam			    wh, "auth", "%s", "TKIP countermeasures enabled");
1564178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1565178354Ssam			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1566178354Ssam				ieee80211_send_error(ni, wh->i_addr2,
1567178354Ssam					IEEE80211_FC0_SUBTYPE_AUTH,
1568178354Ssam					IEEE80211_REASON_MIC_FAILURE);
1569178354Ssam			}
1570178354Ssam			return;
1571178354Ssam		}
1572178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1573192468Ssam			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1574192468Ssam			    seq, status);
1575178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1576192468Ssam			sta_auth_open(ni, wh, rssi, nf, seq, status);
1577178354Ssam		else {
1578178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1579178354Ssam			    wh, "auth", "unsupported alg %d", algo);
1580178354Ssam			vap->iv_stats.is_rx_auth_unsupported++;
1581178354Ssam			return;
1582178354Ssam		}
1583178354Ssam		break;
1584178354Ssam	}
1585178354Ssam
1586178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1587178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1588178354Ssam		uint16_t capinfo, associd;
1589178354Ssam		uint16_t status;
1590178354Ssam
1591178354Ssam		if (vap->iv_state != IEEE80211_S_ASSOC) {
1592178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1593178354Ssam			return;
1594178354Ssam		}
1595178354Ssam
1596178354Ssam		/*
1597178354Ssam		 * asresp frame format
1598178354Ssam		 *	[2] capability information
1599178354Ssam		 *	[2] status
1600178354Ssam		 *	[2] association ID
1601178354Ssam		 *	[tlv] supported rates
1602178354Ssam		 *	[tlv] extended supported rates
1603178354Ssam		 *	[tlv] WME
1604178354Ssam		 *	[tlv] HT capabilities
1605178354Ssam		 *	[tlv] HT info
1606178354Ssam		 */
1607178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1608178354Ssam		ni = vap->iv_bss;
1609178354Ssam		capinfo = le16toh(*(uint16_t *)frm);
1610178354Ssam		frm += 2;
1611178354Ssam		status = le16toh(*(uint16_t *)frm);
1612178354Ssam		frm += 2;
1613178354Ssam		if (status != 0) {
1614178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1615178354Ssam			    wh->i_addr2, "%sassoc failed (reason %d)",
1616178354Ssam			    ISREASSOC(subtype) ?  "re" : "", status);
1617178354Ssam			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1618178354Ssam			return;
1619178354Ssam		}
1620178354Ssam		associd = le16toh(*(uint16_t *)frm);
1621178354Ssam		frm += 2;
1622178354Ssam
1623178354Ssam		rates = xrates = wme = htcap = htinfo = NULL;
1624178354Ssam		while (efrm - frm > 1) {
1625178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1626178354Ssam			switch (*frm) {
1627178354Ssam			case IEEE80211_ELEMID_RATES:
1628178354Ssam				rates = frm;
1629178354Ssam				break;
1630178354Ssam			case IEEE80211_ELEMID_XRATES:
1631178354Ssam				xrates = frm;
1632178354Ssam				break;
1633178354Ssam			case IEEE80211_ELEMID_HTCAP:
1634178354Ssam				htcap = frm;
1635178354Ssam				break;
1636178354Ssam			case IEEE80211_ELEMID_HTINFO:
1637178354Ssam				htinfo = frm;
1638178354Ssam				break;
1639178354Ssam			case IEEE80211_ELEMID_VENDOR:
1640178354Ssam				if (iswmeoui(frm))
1641178354Ssam					wme = frm;
1642193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1643178354Ssam					/*
1644178354Ssam					 * Accept pre-draft HT ie's if the
1645178354Ssam					 * standard ones have not been seen.
1646178354Ssam					 */
1647178354Ssam					if (ishtcapoui(frm)) {
1648178354Ssam						if (htcap == NULL)
1649178354Ssam							htcap = frm;
1650178354Ssam					} else if (ishtinfooui(frm)) {
1651178354Ssam						if (htinfo == NULL)
1652219603Sbschmidt							htinfo = frm;
1653178354Ssam					}
1654178354Ssam				}
1655178354Ssam				/* XXX Atheros OUI support */
1656178354Ssam				break;
1657178354Ssam			}
1658178354Ssam			frm += frm[1] + 2;
1659178354Ssam		}
1660178354Ssam
1661178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1662178354Ssam		if (xrates != NULL)
1663178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1664178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1665178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
1666178354Ssam				IEEE80211_F_JOIN |
1667178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1668178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1669178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
1670178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1671178354Ssam			    wh->i_addr2,
1672178354Ssam			    "%sassoc failed (rate set mismatch)",
1673178354Ssam			    ISREASSOC(subtype) ?  "re" : "");
1674178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
1675178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1676178354Ssam			    IEEE80211_SCAN_FAIL_STATUS);
1677178354Ssam			return;
1678178354Ssam		}
1679178354Ssam
1680178354Ssam		ni->ni_capinfo = capinfo;
1681178354Ssam		ni->ni_associd = associd;
1682178354Ssam		if (ni->ni_jointime == 0)
1683178354Ssam			ni->ni_jointime = time_uptime;
1684178354Ssam		if (wme != NULL &&
1685178354Ssam		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1686178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1687178354Ssam			ieee80211_wme_updateparams(vap);
1688178354Ssam		} else
1689178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1690178354Ssam		/*
1691178354Ssam		 * Setup HT state according to the negotiation.
1692178354Ssam		 *
1693178354Ssam		 * NB: shouldn't need to check if HT use is enabled but some
1694178354Ssam		 *     ap's send back HT ie's even when we don't indicate we
1695178354Ssam		 *     are HT capable in our AssocReq.
1696178354Ssam		 */
1697178354Ssam		if (htcap != NULL && htinfo != NULL &&
1698193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1699183254Ssam			ieee80211_ht_node_init(ni);
1700183254Ssam			ieee80211_ht_updateparams(ni, htcap, htinfo);
1701178354Ssam			ieee80211_setup_htrates(ni, htcap,
1702178354Ssam			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1703178354Ssam			ieee80211_setup_basic_htrates(ni, htinfo);
1704193966Ssam			ieee80211_node_setuptxparms(ni);
1705214894Sbschmidt			ieee80211_ratectl_node_init(ni);
1706190579Ssam		} else {
1707190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1708190579Ssam			if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1709190579Ssam				ieee80211_ff_node_init(ni);
1710190579Ssam#endif
1711178354Ssam		}
1712178354Ssam		/*
1713178354Ssam		 * Configure state now that we are associated.
1714178354Ssam		 *
1715178354Ssam		 * XXX may need different/additional driver callbacks?
1716178354Ssam		 */
1717178354Ssam		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1718178354Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1719178354Ssam			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1720178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1721178354Ssam		} else {
1722178354Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1723178354Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1724178354Ssam		}
1725178354Ssam		ieee80211_set_shortslottime(ic,
1726178354Ssam			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1727178354Ssam			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1728178354Ssam		/*
1729178354Ssam		 * Honor ERP protection.
1730178354Ssam		 *
1731178354Ssam		 * NB: ni_erp should zero for non-11g operation.
1732178354Ssam		 */
1733178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1734178354Ssam		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1735178354Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1736178354Ssam		else
1737178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1738178354Ssam		IEEE80211_NOTE_MAC(vap,
1739178354Ssam		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1740183256Ssam		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1741178354Ssam		    ISREASSOC(subtype) ? "re" : "",
1742178354Ssam		    IEEE80211_NODE_AID(ni),
1743178354Ssam		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1744178354Ssam		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1745178354Ssam		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1746178354Ssam		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1747178354Ssam		    ni->ni_flags & IEEE80211_NODE_HT ?
1748182834Ssam			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1749178354Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1750183255Ssam		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1751183255Ssam			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1752183256Ssam		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1753178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1754178354Ssam			", fast-frames" : "",
1755178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1756178354Ssam			", turbo" : ""
1757178354Ssam		);
1758178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1759178354Ssam		break;
1760178354Ssam	}
1761178354Ssam
1762178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1763178354Ssam		uint16_t reason;
1764178354Ssam
1765178354Ssam		if (vap->iv_state == IEEE80211_S_SCAN) {
1766178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1767178354Ssam			return;
1768178354Ssam		}
1769178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1770178354Ssam			/* NB: can happen when in promiscuous mode */
1771178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1772178354Ssam			break;
1773178354Ssam		}
1774178354Ssam
1775178354Ssam		/*
1776178354Ssam		 * deauth frame format
1777178354Ssam		 *	[2] reason
1778178354Ssam		 */
1779178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1780178354Ssam		reason = le16toh(*(uint16_t *)frm);
1781178354Ssam
1782178354Ssam		vap->iv_stats.is_rx_deauth++;
1783178354Ssam		vap->iv_stats.is_rx_deauth_code = reason;
1784178354Ssam		IEEE80211_NODE_STAT(ni, rx_deauth);
1785178354Ssam
1786178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1787178354Ssam		    "recv deauthenticate (reason %d)", reason);
1788178354Ssam		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1789178354Ssam		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1790178354Ssam		break;
1791178354Ssam	}
1792178354Ssam
1793178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1794178354Ssam		uint16_t reason;
1795178354Ssam
1796178354Ssam		if (vap->iv_state != IEEE80211_S_RUN &&
1797178354Ssam		    vap->iv_state != IEEE80211_S_ASSOC &&
1798178354Ssam		    vap->iv_state != IEEE80211_S_AUTH) {
1799178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1800178354Ssam			return;
1801178354Ssam		}
1802178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1803178354Ssam			/* NB: can happen when in promiscuous mode */
1804178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1805178354Ssam			break;
1806178354Ssam		}
1807178354Ssam
1808178354Ssam		/*
1809178354Ssam		 * disassoc frame format
1810178354Ssam		 *	[2] reason
1811178354Ssam		 */
1812178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1813178354Ssam		reason = le16toh(*(uint16_t *)frm);
1814178354Ssam
1815178354Ssam		vap->iv_stats.is_rx_disassoc++;
1816178354Ssam		vap->iv_stats.is_rx_disassoc_code = reason;
1817178354Ssam		IEEE80211_NODE_STAT(ni, rx_disassoc);
1818178354Ssam
1819178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1820178354Ssam		    "recv disassociate (reason %d)", reason);
1821178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1822178354Ssam		break;
1823178354Ssam	}
1824178354Ssam
1825178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
1826218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1827218958Sbschmidt		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1828218958Sbschmidt		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1829218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1830218958Sbschmidt			    wh, NULL, "%s", "not for us");
1831218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
1832218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
1833218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1834218927Sbschmidt			    wh, NULL, "wrong state %s",
1835218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
1836178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1837218958Sbschmidt		} else {
1838218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
1839218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1840218927Sbschmidt		}
1841178354Ssam		break;
1842178354Ssam
1843178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1844178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1845218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1846295795Savos	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1847218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
1848218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1849218927Sbschmidt		    wh, NULL, "%s", "not handled");
1850178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
1851218927Sbschmidt		break;
1852218927Sbschmidt
1853178354Ssam	default:
1854178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1855218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
1856178354Ssam		vap->iv_stats.is_rx_badsubtype++;
1857178354Ssam		break;
1858178354Ssam	}
1859178354Ssam#undef ISREASSOC
1860178354Ssam#undef ISPROBE
1861178354Ssam}
1862191546Ssam
1863191546Ssamstatic void
1864205277Srpaulosta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1865191546Ssam{
1866205277Srpaulo	switch (subtype) {
1867205277Srpaulo	case IEEE80211_FC0_SUBTYPE_BAR:
1868205277Srpaulo		ieee80211_recv_bar(ni, m);
1869205277Srpaulo		break;
1870205277Srpaulo	}
1871191546Ssam}
1872