ieee80211_sta.c revision 283538
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 283538 2015-05-25 19:18:16Z 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
209178354Ssam/*
210178354Ssam * IEEE80211_M_STA vap state machine handler.
211178354Ssam * This routine handles the main states in the 802.11 protocol.
212178354Ssam */
213178354Ssamstatic int
214178354Ssamsta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
215178354Ssam{
216178354Ssam	struct ieee80211com *ic = vap->iv_ic;
217178354Ssam	struct ieee80211_node *ni;
218178354Ssam	enum ieee80211_state ostate;
219178354Ssam
220178354Ssam	IEEE80211_LOCK_ASSERT(ic);
221178354Ssam
222178354Ssam	ostate = vap->iv_state;
223178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
224178354Ssam	    __func__, ieee80211_state_name[ostate],
225178354Ssam	    ieee80211_state_name[nstate], arg);
226178354Ssam	vap->iv_state = nstate;			/* state transition */
227178354Ssam	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
228178354Ssam	if (ostate != IEEE80211_S_SCAN)
229178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
230178354Ssam	ni = vap->iv_bss;			/* NB: no reference held */
231178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
232178354Ssam		callout_stop(&vap->iv_swbmiss);
233178354Ssam	switch (nstate) {
234178354Ssam	case IEEE80211_S_INIT:
235178354Ssam		switch (ostate) {
236178354Ssam		case IEEE80211_S_SLEEP:
237178354Ssam			/* XXX wakeup */
238264855Sadrian			/* XXX driver hook to wakeup the hardware? */
239178354Ssam		case IEEE80211_S_RUN:
240178354Ssam			IEEE80211_SEND_MGMT(ni,
241178354Ssam			    IEEE80211_FC0_SUBTYPE_DISASSOC,
242178354Ssam			    IEEE80211_REASON_ASSOC_LEAVE);
243178354Ssam			ieee80211_sta_leave(ni);
244178354Ssam			break;
245178354Ssam		case IEEE80211_S_ASSOC:
246178354Ssam			IEEE80211_SEND_MGMT(ni,
247178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
248178354Ssam			    IEEE80211_REASON_AUTH_LEAVE);
249178354Ssam			break;
250178354Ssam		case IEEE80211_S_SCAN:
251178354Ssam			ieee80211_cancel_scan(vap);
252178354Ssam			break;
253178354Ssam		default:
254178354Ssam			goto invalid;
255178354Ssam		}
256178354Ssam		if (ostate != IEEE80211_S_INIT) {
257178354Ssam			/* NB: optimize INIT -> INIT case */
258178354Ssam			ieee80211_reset_bss(vap);
259178354Ssam		}
260178354Ssam		if (vap->iv_auth->ia_detach != NULL)
261178354Ssam			vap->iv_auth->ia_detach(vap);
262178354Ssam		break;
263178354Ssam	case IEEE80211_S_SCAN:
264178354Ssam		switch (ostate) {
265178354Ssam		case IEEE80211_S_INIT:
266178354Ssam			/*
267178354Ssam			 * Initiate a scan.  We can come here as a result
268178354Ssam			 * of an IEEE80211_IOC_SCAN_REQ too in which case
269178354Ssam			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
270178354Ssam			 * and the scan request parameters will be present
271178354Ssam			 * in iv_scanreq.  Otherwise we do the default.
272178354Ssam			 */
273178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
274178354Ssam				ieee80211_check_scan(vap,
275178354Ssam				    vap->iv_scanreq_flags,
276178354Ssam				    vap->iv_scanreq_duration,
277178354Ssam				    vap->iv_scanreq_mindwell,
278178354Ssam				    vap->iv_scanreq_maxdwell,
279178354Ssam				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
280178354Ssam				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
281178354Ssam			} else
282178354Ssam				ieee80211_check_scan_current(vap);
283178354Ssam			break;
284178354Ssam		case IEEE80211_S_SCAN:
285178354Ssam		case IEEE80211_S_AUTH:
286178354Ssam		case IEEE80211_S_ASSOC:
287178354Ssam			/*
288178354Ssam			 * These can happen either because of a timeout
289178354Ssam			 * on an assoc/auth response or because of a
290178354Ssam			 * change in state that requires a reset.  For
291178354Ssam			 * the former we're called with a non-zero arg
292178354Ssam			 * that is the cause for the failure; pass this
293178354Ssam			 * to the scan code so it can update state.
294178354Ssam			 * Otherwise trigger a new scan unless we're in
295178354Ssam			 * manual roaming mode in which case an application
296178354Ssam			 * must issue an explicit scan request.
297178354Ssam			 */
298178354Ssam			if (arg != 0)
299178354Ssam				ieee80211_scan_assoc_fail(vap,
300178354Ssam					vap->iv_bss->ni_macaddr, arg);
301178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
302178354Ssam				ieee80211_check_scan_current(vap);
303178354Ssam			break;
304264906Sadrian		case IEEE80211_S_SLEEP:		/* beacon miss */
305264906Sadrian			/*
306264906Sadrian			 * XXX if in sleep we need to wakeup the hardware.
307264906Sadrian			 */
308264906Sadrian			/* FALLTHROUGH */
309178354Ssam		case IEEE80211_S_RUN:		/* beacon miss */
310178354Ssam			/*
311178354Ssam			 * Beacon miss.  Notify user space and if not
312178354Ssam			 * under control of a user application (roaming
313178354Ssam			 * manual) kick off a scan to re-connect.
314178354Ssam			 */
315264906Sadrian
316178354Ssam			ieee80211_sta_leave(ni);
317178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
318178354Ssam				ieee80211_check_scan_current(vap);
319178354Ssam			break;
320178354Ssam		default:
321178354Ssam			goto invalid;
322178354Ssam		}
323178354Ssam		break;
324178354Ssam	case IEEE80211_S_AUTH:
325178354Ssam		switch (ostate) {
326178354Ssam		case IEEE80211_S_INIT:
327178354Ssam		case IEEE80211_S_SCAN:
328178354Ssam			IEEE80211_SEND_MGMT(ni,
329178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
330178354Ssam			break;
331178354Ssam		case IEEE80211_S_AUTH:
332178354Ssam		case IEEE80211_S_ASSOC:
333178354Ssam			switch (arg & 0xff) {
334178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
335178354Ssam				/* ??? */
336178354Ssam				IEEE80211_SEND_MGMT(ni,
337178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
338178354Ssam				break;
339178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
340178354Ssam				sta_authretry(vap, ni, arg>>8);
341178354Ssam				break;
342178354Ssam			}
343178354Ssam			break;
344178354Ssam		case IEEE80211_S_RUN:
345178354Ssam			switch (arg & 0xff) {
346178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
347178354Ssam				IEEE80211_SEND_MGMT(ni,
348178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
349178354Ssam				vap->iv_state = ostate;	/* stay RUN */
350178354Ssam				break;
351178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
352178354Ssam				ieee80211_sta_leave(ni);
353178354Ssam				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
354178354Ssam					/* try to reauth */
355178354Ssam					IEEE80211_SEND_MGMT(ni,
356178354Ssam					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
357178354Ssam				}
358178354Ssam				break;
359178354Ssam			}
360178354Ssam			break;
361178354Ssam		default:
362178354Ssam			goto invalid;
363178354Ssam		}
364178354Ssam		break;
365178354Ssam	case IEEE80211_S_ASSOC:
366178354Ssam		switch (ostate) {
367178354Ssam		case IEEE80211_S_AUTH:
368178354Ssam		case IEEE80211_S_ASSOC:
369178354Ssam			IEEE80211_SEND_MGMT(ni,
370178354Ssam			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
371178354Ssam			break;
372178354Ssam		case IEEE80211_S_SLEEP:		/* cannot happen */
373178354Ssam		case IEEE80211_S_RUN:
374178354Ssam			ieee80211_sta_leave(ni);
375178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
376178354Ssam				IEEE80211_SEND_MGMT(ni, arg ?
377178354Ssam				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
378178354Ssam				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
379178354Ssam			}
380178354Ssam			break;
381178354Ssam		default:
382178354Ssam			goto invalid;
383178354Ssam		}
384178354Ssam		break;
385178354Ssam	case IEEE80211_S_RUN:
386178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA) {
387178354Ssam			/* XXX validate prerequisites */
388178354Ssam		}
389178354Ssam		switch (ostate) {
390178354Ssam		case IEEE80211_S_RUN:
391193439Ssam		case IEEE80211_S_CSA:
392178354Ssam			break;
393178354Ssam		case IEEE80211_S_AUTH:		/* when join is done in fw */
394178354Ssam		case IEEE80211_S_ASSOC:
395178354Ssam#ifdef IEEE80211_DEBUG
396178354Ssam			if (ieee80211_msg_debug(vap)) {
397178354Ssam				ieee80211_note(vap, "%s with %s ssid ",
398178354Ssam				    (vap->iv_opmode == IEEE80211_M_STA ?
399178354Ssam				    "associated" : "synchronized"),
400178354Ssam				    ether_sprintf(ni->ni_bssid));
401178354Ssam				ieee80211_print_essid(vap->iv_bss->ni_essid,
402178354Ssam				    ni->ni_esslen);
403178354Ssam				/* XXX MCS/HT */
404178354Ssam				printf(" channel %d start %uMb\n",
405178354Ssam				    ieee80211_chan2ieee(ic, ic->ic_curchan),
406178354Ssam				    IEEE80211_RATE2MBS(ni->ni_txrate));
407178354Ssam			}
408178354Ssam#endif
409178354Ssam			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
410178354Ssam			ieee80211_notify_node_join(ni,
411178354Ssam			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
412178354Ssam			break;
413178354Ssam		case IEEE80211_S_SLEEP:
414264855Sadrian			/* Wake up from sleep */
415241138Sadrian			vap->iv_sta_ps(vap, 0);
416178354Ssam			break;
417178354Ssam		default:
418178354Ssam			goto invalid;
419178354Ssam		}
420178354Ssam		ieee80211_sync_curchan(ic);
421178354Ssam		if (ostate != IEEE80211_S_RUN &&
422178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
423178354Ssam			/*
424178354Ssam			 * Start s/w beacon miss timer for devices w/o
425178354Ssam			 * hardware support.  We fudge a bit here since
426178354Ssam			 * we're doing this in software.
427178354Ssam			 */
428178354Ssam			vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
429178354Ssam				2 * vap->iv_bmissthreshold * ni->ni_intval);
430178354Ssam			vap->iv_swbmiss_count = 0;
431178354Ssam			callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
432178354Ssam				ieee80211_swbmiss, vap);
433178354Ssam		}
434178354Ssam		/*
435178354Ssam		 * When 802.1x is not in use mark the port authorized
436178354Ssam		 * at this point so traffic can flow.
437178354Ssam		 */
438178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
439178354Ssam			ieee80211_node_authorize(ni);
440184345Ssam		/*
441184345Ssam		 * Fake association when joining an existing bss.
442264855Sadrian		 *
443264855Sadrian		 * Don't do this if we're doing SLEEP->RUN.
444184345Ssam		 */
445264855Sadrian		if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
446264855Sadrian			ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
447178354Ssam		break;
448193439Ssam	case IEEE80211_S_CSA:
449193439Ssam		if (ostate != IEEE80211_S_RUN)
450193439Ssam			goto invalid;
451193439Ssam		break;
452178354Ssam	case IEEE80211_S_SLEEP:
453241138Sadrian		vap->iv_sta_ps(vap, 1);
454178354Ssam		break;
455178354Ssam	default:
456178354Ssam	invalid:
457184268Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
458184268Ssam		    "%s: unexpected state transition %s -> %s\n", __func__,
459178354Ssam		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
460178354Ssam		break;
461178354Ssam	}
462178354Ssam	return 0;
463178354Ssam}
464178354Ssam
465178354Ssam/*
466178354Ssam * Return non-zero if the frame is an echo of a multicast
467178354Ssam * frame sent by ourself.  The dir is known to be DSTODS.
468178354Ssam */
469178354Ssamstatic __inline int
470178354Ssamisdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
471178354Ssam{
472178354Ssam#define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
473178354Ssam#define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
474178354Ssam	const uint8_t *sa;
475178354Ssam
476178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
477178354Ssam
478178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
479178354Ssam		return 0;
480178354Ssam	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
481178354Ssam	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
482178354Ssam#undef WH4
483178354Ssam#undef QWH4
484178354Ssam}
485178354Ssam
486178354Ssam/*
487178354Ssam * Return non-zero if the frame is an echo of a multicast
488178354Ssam * frame sent by ourself.  The dir is known to be FROMDS.
489178354Ssam */
490178354Ssamstatic __inline int
491178354Ssamisfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
492178354Ssam{
493178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
494178354Ssam
495178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
496178354Ssam		return 0;
497178354Ssam	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
498178354Ssam}
499178354Ssam
500178354Ssam/*
501178354Ssam * Decide if a received management frame should be
502178354Ssam * printed when debugging is enabled.  This filters some
503178354Ssam * of the less interesting frames that come frequently
504178354Ssam * (e.g. beacons).
505178354Ssam */
506178354Ssamstatic __inline int
507178354Ssamdoprint(struct ieee80211vap *vap, int subtype)
508178354Ssam{
509178354Ssam	switch (subtype) {
510178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
511178354Ssam		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
512178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
513178354Ssam		return 0;
514178354Ssam	}
515178354Ssam	return 1;
516178354Ssam}
517178354Ssam
518178354Ssam/*
519178354Ssam * Process a received frame.  The node associated with the sender
520178354Ssam * should be supplied.  If nothing was found in the node table then
521178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
522178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
523178354Ssam * during AP scanning to select a AP to associate with; it can have
524178354Ssam * any units so long as values have consistent units and higher values
525178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
526178354Ssam * by the 802.11 layer.
527178354Ssam */
528178354Ssamstatic int
529283535Sadriansta_input(struct ieee80211_node *ni, struct mbuf *m,
530283535Sadrian    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
531178354Ssam{
532178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
533178354Ssam	struct ieee80211com *ic = ni->ni_ic;
534178354Ssam	struct ifnet *ifp = vap->iv_ifp;
535178354Ssam	struct ieee80211_frame *wh;
536178354Ssam	struct ieee80211_key *key;
537178354Ssam	struct ether_header *eh;
538203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
539178354Ssam	uint8_t dir, type, subtype, qos;
540178354Ssam	uint8_t *bssid;
541178354Ssam	uint16_t rxseq;
542178354Ssam
543183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
544178354Ssam		/*
545178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
546183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
547183247Ssam		 * here but were received out of order and been held on
548183247Ssam		 * the reorder queue.  When resubmitted they are marked
549183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
550183247Ssam		 * the normal processing.
551178354Ssam		 */
552178354Ssam		wh = mtod(m, struct ieee80211_frame *);
553178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
554178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
555178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
556178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
557178354Ssam		goto resubmit_ampdu;
558178354Ssam	}
559178354Ssam
560178354Ssam	KASSERT(ni != NULL, ("null node"));
561178354Ssam	ni->ni_inact = ni->ni_inact_reload;
562178354Ssam
563178354Ssam	type = -1;			/* undefined */
564178354Ssam
565178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
566178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
567178354Ssam		    ni->ni_macaddr, NULL,
568178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
569178354Ssam		vap->iv_stats.is_rx_tooshort++;
570178354Ssam		goto out;
571178354Ssam	}
572178354Ssam	/*
573178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
574178354Ssam	 * frame format but don't reference fields past outside
575178354Ssam	 * ieee80211_frame_min w/o first validating the data is
576178354Ssam	 * present.
577178354Ssam	 */
578178354Ssam	wh = mtod(m, struct ieee80211_frame *);
579178354Ssam
580178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
581178354Ssam	    IEEE80211_FC0_VERSION_0) {
582178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
583191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
584191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
585178354Ssam		vap->iv_stats.is_rx_badversion++;
586178354Ssam		goto err;
587178354Ssam	}
588178354Ssam
589178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
590178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
591178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
592178354Ssam	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
593178354Ssam		bssid = wh->i_addr2;
594178354Ssam		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
595178354Ssam			/* not interested in */
596178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
597178354Ssam			    bssid, NULL, "%s", "not to bss");
598178354Ssam			vap->iv_stats.is_rx_wrongbss++;
599178354Ssam			goto out;
600178354Ssam		}
601227338Sadrian
602227338Sadrian		/*
603227338Sadrian		 * Some devices may be in a promiscuous mode
604227338Sadrian		 * where they receive frames for multiple station
605227338Sadrian		 * addresses.
606227338Sadrian		 *
607227338Sadrian		 * If we receive a data frame that isn't
608227338Sadrian		 * destined to our VAP MAC, drop it.
609227338Sadrian		 *
610227338Sadrian		 * XXX TODO: This is only enforced when not scanning;
611227338Sadrian		 * XXX it assumes a software-driven scan will put the NIC
612227338Sadrian		 * XXX into a "no data frames" mode before setting this
613227338Sadrian		 * XXX flag. Otherwise it may be possible that we'll still
614227338Sadrian		 * XXX process data frames whilst scanning.
615227338Sadrian		 */
616227338Sadrian		if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
617227338Sadrian		    && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
618227338Sadrian			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
619227338Sadrian			    bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
620227338Sadrian			    IF_LLADDR(ifp), ":", wh->i_addr1, ":");
621227338Sadrian			vap->iv_stats.is_rx_wrongbss++;
622227338Sadrian			goto out;
623227338Sadrian		}
624227338Sadrian
625178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
626192468Ssam		ni->ni_noise = nf;
627282820Sadrian		if ( IEEE80211_HAS_SEQ(type, subtype) &&
628282820Sadrian		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
629178354Ssam			uint8_t tid = ieee80211_gettid(wh);
630178354Ssam			if (IEEE80211_QOS_HAS_SEQ(wh) &&
631178354Ssam			    TID_TO_WME_AC(tid) >= WME_AC_VI)
632178354Ssam				ic->ic_wme.wme_hipri_traffic++;
633178354Ssam			rxseq = le16toh(*(uint16_t *)wh->i_seq);
634221418Sadrian			if (! ieee80211_check_rxseq(ni, wh)) {
635178354Ssam				/* duplicate, discard */
636178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
637178354Ssam				    bssid, "duplicate",
638178354Ssam				    "seqno <%u,%u> fragno <%u,%u> tid %u",
639178354Ssam				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
640178354Ssam				    ni->ni_rxseqs[tid] >>
641178354Ssam					IEEE80211_SEQ_SEQ_SHIFT,
642178354Ssam				    rxseq & IEEE80211_SEQ_FRAG_MASK,
643178354Ssam				    ni->ni_rxseqs[tid] &
644178354Ssam					IEEE80211_SEQ_FRAG_MASK,
645178354Ssam				    tid);
646178354Ssam				vap->iv_stats.is_rx_dup++;
647178354Ssam				IEEE80211_NODE_STAT(ni, rx_dup);
648178354Ssam				goto out;
649178354Ssam			}
650178354Ssam			ni->ni_rxseqs[tid] = rxseq;
651178354Ssam		}
652178354Ssam	}
653178354Ssam
654178354Ssam	switch (type) {
655178354Ssam	case IEEE80211_FC0_TYPE_DATA:
656178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
657178354Ssam		if (m->m_len < hdrspace &&
658178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
659178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
660178354Ssam			    ni->ni_macaddr, NULL,
661178354Ssam			    "data too short: expecting %u", hdrspace);
662178354Ssam			vap->iv_stats.is_rx_tooshort++;
663178354Ssam			goto out;		/* XXX */
664178354Ssam		}
665178354Ssam		/*
666183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
667183247Ssam		 * processed directly then ieee80211_ampdu_reorder
668178354Ssam		 * will return 0; otherwise it has consumed the mbuf
669178354Ssam		 * and we should do nothing more with it.
670178354Ssam		 */
671183247Ssam		if ((m->m_flags & M_AMPDU) &&
672178354Ssam		    (dir == IEEE80211_FC1_DIR_FROMDS ||
673178354Ssam		     dir == IEEE80211_FC1_DIR_DSTODS) &&
674178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
675178354Ssam			m = NULL;
676178354Ssam			goto out;
677178354Ssam		}
678178354Ssam	resubmit_ampdu:
679178354Ssam		if (dir == IEEE80211_FC1_DIR_FROMDS) {
680178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
681178354Ssam			    isfromds_mcastecho(vap, wh)) {
682178354Ssam				/*
683178354Ssam				 * In IEEE802.11 network, multicast
684178354Ssam				 * packets sent from "me" are broadcast
685178354Ssam				 * from the AP; silently discard for
686178354Ssam				 * SIMPLEX interface.
687178354Ssam				 */
688178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
689178354Ssam				    wh, "data", "%s", "multicast echo");
690178354Ssam				vap->iv_stats.is_rx_mcastecho++;
691178354Ssam				goto out;
692178354Ssam			}
693178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
694178354Ssam			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
695178354Ssam				/*
696178354Ssam				 * DWDS sta's must drop 3-address mcast frames
697178354Ssam				 * as they will be sent separately as a 4-addr
698178354Ssam				 * frame.  Accepting the 3-addr frame will
699178354Ssam				 * confuse the bridge into thinking the sending
700178354Ssam				 * sta is located at the end of WDS link.
701178354Ssam				 */
702178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
703178354Ssam				    "3-address data", "%s", "DWDS enabled");
704178354Ssam				vap->iv_stats.is_rx_mcastecho++;
705178354Ssam				goto out;
706178354Ssam			}
707178354Ssam		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
708178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
709178354Ssam				IEEE80211_DISCARD(vap,
710178354Ssam				    IEEE80211_MSG_INPUT, wh, "4-address data",
711178354Ssam				    "%s", "DWDS not enabled");
712178354Ssam				vap->iv_stats.is_rx_wrongdir++;
713178354Ssam				goto out;
714178354Ssam			}
715178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
716178354Ssam			    isdstods_mcastecho(vap, wh)) {
717178354Ssam				/*
718178354Ssam				 * In IEEE802.11 network, multicast
719178354Ssam				 * packets sent from "me" are broadcast
720178354Ssam				 * from the AP; silently discard for
721178354Ssam				 * SIMPLEX interface.
722178354Ssam				 */
723178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
724178354Ssam				    "4-address data", "%s", "multicast echo");
725178354Ssam				vap->iv_stats.is_rx_mcastecho++;
726178354Ssam				goto out;
727178354Ssam			}
728178354Ssam		} else {
729178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
730178354Ssam			    "data", "incorrect dir 0x%x", dir);
731178354Ssam			vap->iv_stats.is_rx_wrongdir++;
732178354Ssam			goto out;
733178354Ssam		}
734178354Ssam
735178354Ssam		/*
736178354Ssam		 * Handle privacy requirements.  Note that we
737178354Ssam		 * must not be preempted from here until after
738178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
739178354Ssam		 * otherwise we may violate assumptions in the
740178354Ssam		 * crypto cipher modules used to do delayed update
741178354Ssam		 * of replay sequence numbers.
742178354Ssam		 */
743260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
744178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
745178354Ssam				/*
746178354Ssam				 * Discard encrypted frames when privacy is off.
747178354Ssam				 */
748178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
749178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
750178354Ssam				vap->iv_stats.is_rx_noprivacy++;
751178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
752178354Ssam				goto out;
753178354Ssam			}
754178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
755178354Ssam			if (key == NULL) {
756178354Ssam				/* NB: stats+msgs handled in crypto_decap */
757178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
758178354Ssam				goto out;
759178354Ssam			}
760178354Ssam			wh = mtod(m, struct ieee80211_frame *);
761260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
762178354Ssam		} else {
763178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
764178354Ssam			key = NULL;
765178354Ssam		}
766178354Ssam
767178354Ssam		/*
768178354Ssam		 * Save QoS bits for use below--before we strip the header.
769178354Ssam		 */
770178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
771178354Ssam			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
772178354Ssam			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
773178354Ssam			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
774178354Ssam		} else
775178354Ssam			qos = 0;
776178354Ssam
777178354Ssam		/*
778178354Ssam		 * Next up, any fragmentation.
779178354Ssam		 */
780178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
781178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
782178354Ssam			if (m == NULL) {
783178354Ssam				/* Fragment dropped or frame not complete yet */
784178354Ssam				goto out;
785178354Ssam			}
786178354Ssam		}
787178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
788178354Ssam
789178354Ssam		/*
790178354Ssam		 * Next strip any MSDU crypto bits.
791178354Ssam		 */
792178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
793178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
794178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
795178354Ssam			vap->iv_stats.is_rx_demicfail++;
796178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
797178354Ssam			goto out;
798178354Ssam		}
799178354Ssam
800178354Ssam		/* copy to listener after decrypt */
801192468Ssam		if (ieee80211_radiotap_active_vap(vap))
802202967Srpaulo			ieee80211_radiotap_rx(vap, m);
803178354Ssam		need_tap = 0;
804178354Ssam
805178354Ssam		/*
806178354Ssam		 * Finally, strip the 802.11 header.
807178354Ssam		 */
808178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
809178354Ssam		if (m == NULL) {
810178354Ssam			/* XXX mask bit to check for both */
811178354Ssam			/* don't count Null data frames as errors */
812178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
813178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
814178354Ssam				goto out;
815178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
816178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
817178354Ssam			vap->iv_stats.is_rx_decap++;
818178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
819178354Ssam			goto err;
820178354Ssam		}
821178354Ssam		eh = mtod(m, struct ether_header *);
822178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
823178354Ssam			/*
824178354Ssam			 * Deny any non-PAE frames received prior to
825178354Ssam			 * authorization.  For open/shared-key
826178354Ssam			 * authentication the port is mark authorized
827178354Ssam			 * after authentication completes.  For 802.1x
828178354Ssam			 * the port is not marked authorized by the
829178354Ssam			 * authenticator until the handshake has completed.
830178354Ssam			 */
831178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
832178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
833178354Ssam				    eh->ether_shost, "data",
834178354Ssam				    "unauthorized port: ether type 0x%x len %u",
835178354Ssam				    eh->ether_type, m->m_pkthdr.len);
836178354Ssam				vap->iv_stats.is_rx_unauth++;
837178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
838178354Ssam				goto err;
839178354Ssam			}
840178354Ssam		} else {
841178354Ssam			/*
842178354Ssam			 * When denying unencrypted frames, discard
843178354Ssam			 * any non-PAE frames received without encryption.
844178354Ssam			 */
845178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
846178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
847178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
848178354Ssam				/*
849178354Ssam				 * Drop unencrypted frames.
850178354Ssam				 */
851178354Ssam				vap->iv_stats.is_rx_unencrypted++;
852178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
853178354Ssam				goto out;
854178354Ssam			}
855178354Ssam		}
856178354Ssam		/* XXX require HT? */
857178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
858178354Ssam			m = ieee80211_decap_amsdu(ni, m);
859178354Ssam			if (m == NULL)
860178354Ssam				return IEEE80211_FC0_TYPE_DATA;
861190391Ssam		} else {
862190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
863190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
864190391Ssam			if (m == NULL)
865178354Ssam				return IEEE80211_FC0_TYPE_DATA;
866190391Ssam#endif
867178354Ssam		}
868178354Ssam		ieee80211_deliver_data(vap, ni, m);
869178354Ssam		return IEEE80211_FC0_TYPE_DATA;
870178354Ssam
871178354Ssam	case IEEE80211_FC0_TYPE_MGT:
872178354Ssam		vap->iv_stats.is_rx_mgmt++;
873178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
874178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
875178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
876178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
877178354Ssam			vap->iv_stats.is_rx_wrongdir++;
878178354Ssam			goto err;
879178354Ssam		}
880178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
881178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
882178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
883178354Ssam			    m->m_pkthdr.len);
884178354Ssam			vap->iv_stats.is_rx_tooshort++;
885178354Ssam			goto out;
886178354Ssam		}
887178354Ssam#ifdef IEEE80211_DEBUG
888178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
889178354Ssam		    ieee80211_msg_dumppkts(vap)) {
890178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
891178354Ssam			    ieee80211_mgt_subtype_name[subtype >>
892178354Ssam				IEEE80211_FC0_SUBTYPE_SHIFT],
893178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
894178354Ssam		}
895178354Ssam#endif
896260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
897178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
898178354Ssam				/*
899178354Ssam				 * Only shared key auth frames with a challenge
900178354Ssam				 * should be encrypted, discard all others.
901178354Ssam				 */
902178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
903178354Ssam				    wh, ieee80211_mgt_subtype_name[subtype >>
904178354Ssam					IEEE80211_FC0_SUBTYPE_SHIFT],
905178354Ssam				    "%s", "WEP set but not permitted");
906178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
907178354Ssam				goto out;
908178354Ssam			}
909178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
910178354Ssam				/*
911178354Ssam				 * Discard encrypted frames when privacy is off.
912178354Ssam				 */
913178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
914178354Ssam				    wh, "mgt", "%s", "WEP set but PRIVACY off");
915178354Ssam				vap->iv_stats.is_rx_noprivacy++;
916178354Ssam				goto out;
917178354Ssam			}
918178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
919178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
920178354Ssam			if (key == NULL) {
921178354Ssam				/* NB: stats+msgs handled in crypto_decap */
922178354Ssam				goto out;
923178354Ssam			}
924178354Ssam			wh = mtod(m, struct ieee80211_frame *);
925260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
926178354Ssam		}
927283535Sadrian		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
928191534Ssam		goto out;
929178354Ssam
930178354Ssam	case IEEE80211_FC0_TYPE_CTL:
931178354Ssam		vap->iv_stats.is_rx_ctl++;
932178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
933191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
934178354Ssam		goto out;
935191549Ssam
936178354Ssam	default:
937178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
938178354Ssam		    wh, NULL, "bad frame type 0x%x", type);
939178354Ssam		/* should not come here */
940178354Ssam		break;
941178354Ssam	}
942178354Ssamerr:
943271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
944178354Ssamout:
945178354Ssam	if (m != NULL) {
946192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
947192468Ssam			ieee80211_radiotap_rx(vap, m);
948178354Ssam		m_freem(m);
949178354Ssam	}
950178354Ssam	return type;
951178354Ssam}
952178354Ssam
953178354Ssamstatic void
954178354Ssamsta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
955192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
956178354Ssam{
957178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
958178354Ssam
959178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
960178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
961178354Ssam		    ni->ni_macaddr, "open auth",
962178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
963178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
964178354Ssam		return;
965178354Ssam	}
966178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH ||
967178354Ssam	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
968178354Ssam		vap->iv_stats.is_rx_bad_auth++;
969178354Ssam		return;
970178354Ssam	}
971178354Ssam	if (status != 0) {
972178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
973178354Ssam		    ni, "open auth failed (reason %d)", status);
974178354Ssam		vap->iv_stats.is_rx_auth_fail++;
975178354Ssam		vap->iv_stats.is_rx_authfail_code = status;
976178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
977178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
978178354Ssam	} else
979178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
980178354Ssam}
981178354Ssam
982178354Ssamstatic void
983178354Ssamsta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
984192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
985178354Ssam    uint16_t seq, uint16_t status)
986178354Ssam{
987178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
988178354Ssam	uint8_t *challenge;
989178354Ssam	int estatus;
990178354Ssam
991178354Ssam	/*
992178354Ssam	 * NB: this can happen as we allow pre-shared key
993178354Ssam	 * authentication to be enabled w/o wep being turned
994178354Ssam	 * on so that configuration of these can be done
995178354Ssam	 * in any order.  It may be better to enforce the
996178354Ssam	 * ordering in which case this check would just be
997178354Ssam	 * for sanity/consistency.
998178354Ssam	 */
999178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1000178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1001178354Ssam		    ni->ni_macaddr, "shared key auth",
1002178354Ssam		    "%s", " PRIVACY is disabled");
1003178354Ssam		estatus = IEEE80211_STATUS_ALG;
1004178354Ssam		goto bad;
1005178354Ssam	}
1006178354Ssam	/*
1007178354Ssam	 * Pre-shared key authentication is evil; accept
1008178354Ssam	 * it only if explicitly configured (it is supported
1009178354Ssam	 * mainly for compatibility with clients like OS X).
1010178354Ssam	 */
1011178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1012178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1013178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1014178354Ssam		    ni->ni_macaddr, "shared key auth",
1015178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1016178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1017178354Ssam		estatus = IEEE80211_STATUS_ALG;
1018178354Ssam		goto bad;
1019178354Ssam	}
1020178354Ssam
1021178354Ssam	challenge = NULL;
1022178354Ssam	if (frm + 1 < efrm) {
1023178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1024178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1025178354Ssam			    ni->ni_macaddr, "shared key auth",
1026178354Ssam			    "ie %d/%d too long",
1027178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1028178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1029178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1030178354Ssam			goto bad;
1031178354Ssam		}
1032178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1033178354Ssam			challenge = frm;
1034178354Ssam		frm += frm[1] + 2;
1035178354Ssam	}
1036178354Ssam	switch (seq) {
1037178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1038178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1039178354Ssam		if (challenge == NULL) {
1040178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1041178354Ssam			    ni->ni_macaddr, "shared key auth",
1042178354Ssam			    "%s", "no challenge");
1043178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1044178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1045178354Ssam			goto bad;
1046178354Ssam		}
1047178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1048178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1049178354Ssam			    ni->ni_macaddr, "shared key auth",
1050178354Ssam			    "bad challenge len %d", challenge[1]);
1051178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1052178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1053178354Ssam			goto bad;
1054178354Ssam		}
1055178354Ssam	default:
1056178354Ssam		break;
1057178354Ssam	}
1058178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH)
1059178354Ssam		return;
1060178354Ssam	switch (seq) {
1061178354Ssam	case IEEE80211_AUTH_SHARED_PASS:
1062178354Ssam		if (ni->ni_challenge != NULL) {
1063283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1064178354Ssam			ni->ni_challenge = NULL;
1065178354Ssam		}
1066178354Ssam		if (status != 0) {
1067178354Ssam			IEEE80211_NOTE_FRAME(vap,
1068178354Ssam			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1069178354Ssam			    "shared key auth failed (reason %d)", status);
1070178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1071178354Ssam			vap->iv_stats.is_rx_authfail_code = status;
1072178354Ssam			return;
1073178354Ssam		}
1074178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1075178354Ssam		break;
1076178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1077178354Ssam		if (!ieee80211_alloc_challenge(ni))
1078178354Ssam			return;
1079178354Ssam		/* XXX could optimize by passing recvd challenge */
1080178354Ssam		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1081178354Ssam		IEEE80211_SEND_MGMT(ni,
1082178354Ssam			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1083178354Ssam		break;
1084178354Ssam	default:
1085178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1086178354Ssam		    wh, "shared key auth", "bad seq %d", seq);
1087178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1088178354Ssam		return;
1089178354Ssam	}
1090178354Ssam	return;
1091178354Ssambad:
1092178354Ssam	/*
1093178354Ssam	 * Kick the state machine.  This short-circuits
1094178354Ssam	 * using the mgt frame timeout to trigger the
1095178354Ssam	 * state transition.
1096178354Ssam	 */
1097178354Ssam	if (vap->iv_state == IEEE80211_S_AUTH)
1098178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1099178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
1100178354Ssam}
1101178354Ssam
1102244060Sadrianint
1103178354Ssamieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1104178354Ssam	const struct ieee80211_frame *wh)
1105178354Ssam{
1106178354Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1107178354Ssam	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1108178354Ssam	u_int len = frm[1], qosinfo;
1109178354Ssam	int i;
1110178354Ssam
1111178354Ssam	if (len < sizeof(struct ieee80211_wme_param)-2) {
1112178354Ssam		IEEE80211_DISCARD_IE(vap,
1113178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1114178354Ssam		    wh, "WME", "too short, len %u", len);
1115178354Ssam		return -1;
1116178354Ssam	}
1117178354Ssam	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1118178354Ssam	qosinfo &= WME_QOSINFO_COUNT;
1119178354Ssam	/* XXX do proper check for wraparound */
1120178354Ssam	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1121178354Ssam		return 0;
1122178354Ssam	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1123178354Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1124178354Ssam		struct wmeParams *wmep =
1125178354Ssam			&wme->wme_wmeChanParams.cap_wmeParams[i];
1126178354Ssam		/* NB: ACI not used */
1127178354Ssam		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1128178354Ssam		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1129178354Ssam		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1130178354Ssam		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1131178354Ssam		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1132178354Ssam		frm += 4;
1133178354Ssam	}
1134178354Ssam	wme->wme_wmeChanParams.cap_info = qosinfo;
1135178354Ssam	return 1;
1136178354Ssam#undef MS
1137178354Ssam}
1138178354Ssam
1139178354Ssam/*
1140193439Ssam * Process 11h Channel Switch Announcement (CSA) ie.  If this
1141193439Ssam * is the first CSA then initiate the switch.  Otherwise we
1142193439Ssam * track state and trigger completion and/or cancel of the switch.
1143193439Ssam * XXX should be public for IBSS use
1144193439Ssam */
1145193439Ssamstatic void
1146193439Ssamieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1147193439Ssam	const struct ieee80211_frame *wh)
1148193439Ssam{
1149193439Ssam	struct ieee80211com *ic = vap->iv_ic;
1150193439Ssam	const struct ieee80211_csa_ie *csa =
1151193439Ssam	    (const struct ieee80211_csa_ie *) frm;
1152193439Ssam
1153193439Ssam	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1154193439Ssam	    ("state %s", ieee80211_state_name[vap->iv_state]));
1155193439Ssam
1156193439Ssam	if (csa->csa_mode > 1) {
1157193439Ssam		IEEE80211_DISCARD_IE(vap,
1158193439Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1159193439Ssam		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1160193439Ssam		return;
1161193439Ssam	}
1162193439Ssam	IEEE80211_LOCK(ic);
1163193439Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1164193439Ssam		/*
1165193439Ssam		 * Convert the channel number to a channel reference.  We
1166193439Ssam		 * try first to preserve turbo attribute of the current
1167193439Ssam		 * channel then fallback.  Note this will not work if the
1168193439Ssam		 * CSA specifies a channel that requires a band switch (e.g.
1169193439Ssam		 * 11a => 11g).  This is intentional as 11h is defined only
1170193439Ssam		 * for 5GHz/11a and because the switch does not involve a
1171193439Ssam		 * reassociation, protocol state (capabilities, negotated
1172193439Ssam		 * rates, etc) may/will be wrong.
1173193439Ssam		 */
1174193439Ssam		struct ieee80211_channel *c =
1175193439Ssam		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1176193439Ssam			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1177193439Ssam		if (c == NULL) {
1178193439Ssam			c = ieee80211_find_channel_byieee(ic,
1179193439Ssam			    csa->csa_newchan,
1180193439Ssam			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1181193439Ssam			if (c == NULL) {
1182193439Ssam				IEEE80211_DISCARD_IE(vap,
1183193439Ssam				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1184193439Ssam				    wh, "CSA", "invalid channel %u",
1185193439Ssam				    csa->csa_newchan);
1186193439Ssam				goto done;
1187193439Ssam			}
1188193439Ssam		}
1189193439Ssam#if IEEE80211_CSA_COUNT_MIN > 0
1190193439Ssam		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1191193439Ssam			/*
1192193439Ssam			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1193193439Ssam			 * reduce the risk of being redirected by a fabricated
1194193439Ssam			 * CSA.  If a valid CSA is dropped we'll still get a
1195193439Ssam			 * beacon miss when the AP leaves the channel so we'll
1196193439Ssam			 * eventually follow to the new channel.
1197193439Ssam			 *
1198193439Ssam			 * NOTE: this violates the 11h spec that states that
1199193439Ssam			 * count may be any value and if 0 then a switch
1200193439Ssam			 * should happen asap.
1201193439Ssam			 */
1202193439Ssam			IEEE80211_DISCARD_IE(vap,
1203193439Ssam			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1204193439Ssam			    wh, "CSA", "count %u too small, must be >= %u",
1205193439Ssam			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1206193439Ssam			goto done;
1207193439Ssam		}
1208193439Ssam#endif
1209193439Ssam		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1210193439Ssam	} else {
1211193439Ssam		/*
1212193439Ssam		 * Validate this ie against the initial CSA.  We require
1213193439Ssam		 * mode and channel not change and the count must be
1214193439Ssam		 * monotonically decreasing.  This may be pointless and
1215193439Ssam		 * canceling the switch as a result may be too paranoid but
1216193439Ssam		 * in the worst case if we drop out of CSA because of this
1217193439Ssam		 * and the AP does move then we'll just end up taking a
1218193439Ssam		 * beacon miss and scan to find the AP.
1219193439Ssam		 *
1220193439Ssam		 * XXX may want <= on count as we also process ProbeResp
1221193439Ssam		 * frames and those may come in w/ the same count as the
1222193439Ssam		 * previous beacon; but doing so leaves us open to a stuck
1223193439Ssam		 * count until we add a dead-man timer
1224193439Ssam		 */
1225193439Ssam		if (!(csa->csa_count < ic->ic_csa_count &&
1226193439Ssam		      csa->csa_mode == ic->ic_csa_mode &&
1227193439Ssam		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1228193439Ssam			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1229193439Ssam			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1230193439Ssam			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1231193439Ssam			    ic->ic_csa_newchan, ic->ic_csa_count,
1232193439Ssam			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1233193439Ssam			ieee80211_csa_cancelswitch(ic);
1234193439Ssam		} else {
1235193439Ssam			if (csa->csa_count <= 1)
1236193439Ssam				ieee80211_csa_completeswitch(ic);
1237193439Ssam			else
1238193439Ssam				ic->ic_csa_count = csa->csa_count;
1239193439Ssam		}
1240193439Ssam	}
1241193439Ssamdone:
1242193439Ssam	IEEE80211_UNLOCK(ic);
1243193439Ssam}
1244193439Ssam
1245193439Ssam/*
1246178354Ssam * Return non-zero if a background scan may be continued:
1247178354Ssam * o bg scan is active
1248178354Ssam * o no channel switch is pending
1249178354Ssam * o there has not been any traffic recently
1250178354Ssam *
1251178354Ssam * Note we do not check if there is an administrative enable;
1252178354Ssam * this is only done to start the scan.  We assume that any
1253178354Ssam * change in state will be accompanied by a request to cancel
1254178354Ssam * active scans which will otherwise cause this test to fail.
1255178354Ssam */
1256178354Ssamstatic __inline int
1257178354Ssamcontbgscan(struct ieee80211vap *vap)
1258178354Ssam{
1259178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1260178354Ssam
1261178354Ssam	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1262178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1263178354Ssam	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1264178354Ssam	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1265178354Ssam}
1266178354Ssam
1267178354Ssam/*
1268178354Ssam * Return non-zero if a backgrond scan may be started:
1269178354Ssam * o bg scanning is administratively enabled
1270178354Ssam * o no channel switch is pending
1271178354Ssam * o we are not boosted on a dynamic turbo channel
1272178354Ssam * o there has not been a scan recently
1273178354Ssam * o there has not been any traffic recently
1274178354Ssam */
1275178354Ssamstatic __inline int
1276178354Ssamstartbgscan(struct ieee80211vap *vap)
1277178354Ssam{
1278178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1279178354Ssam
1280178354Ssam	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1281178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1282190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1283178354Ssam	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1284190391Ssam#endif
1285178354Ssam	    time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1286178354Ssam	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1287178354Ssam}
1288178354Ssam
1289178354Ssamstatic void
1290283535Sadriansta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1291283535Sadrian    const struct ieee80211_rx_stats *rxs,
1292283535Sadrian    int rssi, int nf)
1293178354Ssam{
1294178354Ssam#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1295178354Ssam#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1296178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1297178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1298283535Sadrian	struct ieee80211_channel *rxchan = ic->ic_curchan;
1299178354Ssam	struct ieee80211_frame *wh;
1300178354Ssam	uint8_t *frm, *efrm;
1301178354Ssam	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1302178354Ssam	uint8_t rate;
1303233452Sadrian	int ht_state_change = 0;
1304178354Ssam
1305178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1306178354Ssam	frm = (uint8_t *)&wh[1];
1307178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1308178354Ssam	switch (subtype) {
1309178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1310178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON: {
1311178354Ssam		struct ieee80211_scanparams scan;
1312283535Sadrian		struct ieee80211_channel *c;
1313178354Ssam		/*
1314178354Ssam		 * We process beacon/probe response frames:
1315178354Ssam		 *    o when scanning, or
1316178354Ssam		 *    o station mode when associated (to collect state
1317191444Srpaulo		 *      updates such as 802.11g slot time)
1318178354Ssam		 * Frames otherwise received are discarded.
1319178354Ssam		 */
1320178354Ssam		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1321178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1322178354Ssam			return;
1323178354Ssam		}
1324283535Sadrian
1325283535Sadrian		/* Override RX channel as appropriate */
1326283535Sadrian		if (rxs != NULL) {
1327283535Sadrian			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1328283535Sadrian			if (c != NULL)
1329283535Sadrian				rxchan = c;
1330283535Sadrian		}
1331283535Sadrian
1332178354Ssam		/* XXX probe response in sta mode when !scanning? */
1333283535Sadrian		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1334232270Sadrian			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1335232270Sadrian				vap->iv_stats.is_beacon_bad++;
1336178354Ssam			return;
1337232244Sadrian		}
1338264855Sadrian
1339178354Ssam		/*
1340178354Ssam		 * Count frame now that we know it's to be processed.
1341178354Ssam		 */
1342178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1343178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1344178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1345178354Ssam		} else
1346178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1347178354Ssam		/*
1348178354Ssam		 * When operating in station mode, check for state updates.
1349178354Ssam		 * Be careful to ignore beacons received while doing a
1350178354Ssam		 * background scan.  We consider only 11g/WMM stuff right now.
1351178354Ssam		 */
1352178354Ssam		if (ni->ni_associd != 0 &&
1353178354Ssam		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1354178354Ssam		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1355178354Ssam			/* record tsf of last beacon */
1356178354Ssam			memcpy(ni->ni_tstamp.data, scan.tstamp,
1357178354Ssam				sizeof(ni->ni_tstamp));
1358178354Ssam			/* count beacon frame for s/w bmiss handling */
1359178354Ssam			vap->iv_swbmiss_count++;
1360178354Ssam			vap->iv_bmiss_count = 0;
1361178354Ssam			if (ni->ni_erp != scan.erp) {
1362178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1363178354Ssam				    wh->i_addr2,
1364178354Ssam				    "erp change: was 0x%x, now 0x%x",
1365178354Ssam				    ni->ni_erp, scan.erp);
1366178354Ssam				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1367178354Ssam				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1368178354Ssam					ic->ic_flags |= IEEE80211_F_USEPROT;
1369178354Ssam				else
1370178354Ssam					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1371178354Ssam				ni->ni_erp = scan.erp;
1372178354Ssam				/* XXX statistic */
1373178354Ssam				/* XXX driver notification */
1374178354Ssam			}
1375178354Ssam			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1376178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1377178354Ssam				    wh->i_addr2,
1378178354Ssam				    "capabilities change: was 0x%x, now 0x%x",
1379178354Ssam				    ni->ni_capinfo, scan.capinfo);
1380178354Ssam				/*
1381178354Ssam				 * NB: we assume short preamble doesn't
1382178354Ssam				 *     change dynamically
1383178354Ssam				 */
1384178354Ssam				ieee80211_set_shortslottime(ic,
1385178354Ssam					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1386178354Ssam					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1387178354Ssam				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1388178354Ssam					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1389178354Ssam				/* XXX statistic */
1390178354Ssam			}
1391178354Ssam			if (scan.wme != NULL &&
1392178354Ssam			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1393178354Ssam			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1394178354Ssam				ieee80211_wme_updateparams(vap);
1395190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1396178354Ssam			if (scan.ath != NULL)
1397178354Ssam				ieee80211_parse_athparams(ni, scan.ath, wh);
1398190391Ssam#endif
1399183254Ssam			if (scan.htcap != NULL && scan.htinfo != NULL &&
1400193655Ssam			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1401178354Ssam				/* XXX state changes? */
1402233452Sadrian				if (ieee80211_ht_updateparams(ni,
1403233452Sadrian				    scan.htcap, scan.htinfo))
1404233452Sadrian					ht_state_change = 1;
1405178354Ssam			}
1406227331Sadrian			if (scan.quiet)
1407227331Sadrian				ic->ic_set_quiet(ni, scan.quiet);
1408264855Sadrian
1409178354Ssam			if (scan.tim != NULL) {
1410178354Ssam				struct ieee80211_tim_ie *tim =
1411178354Ssam				    (struct ieee80211_tim_ie *) scan.tim;
1412264855Sadrian				/*
1413264855Sadrian				 * XXX Check/debug this code; see if it's about
1414264855Sadrian				 * the right time to force the VAP awake if we
1415264855Sadrian				 * receive a frame destined for us?
1416264855Sadrian				 */
1417178354Ssam				int aid = IEEE80211_AID(ni->ni_associd);
1418178354Ssam				int ix = aid / NBBY;
1419178354Ssam				int min = tim->tim_bitctl &~ 1;
1420178354Ssam				int max = tim->tim_len + min - 4;
1421275983Sadrian				int tim_ucast = 0, tim_mcast = 0;
1422264855Sadrian
1423264855Sadrian				/*
1424264855Sadrian				 * Only do this for unicast traffic in the TIM
1425264855Sadrian				 * The multicast traffic notification for
1426264855Sadrian				 * the scan notification stuff should occur
1427264855Sadrian				 * differently.
1428264855Sadrian				 */
1429264855Sadrian				if (min <= ix && ix <= max &&
1430264855Sadrian				     isset(tim->tim_bitmap - min, aid)) {
1431275983Sadrian					tim_ucast = 1;
1432264855Sadrian				}
1433264855Sadrian
1434264855Sadrian				/*
1435275983Sadrian				 * Do a separate notification
1436264855Sadrian				 * for the multicast bit being set.
1437264855Sadrian				 */
1438264906Sadrian				if (tim->tim_bitctl & 1) {
1439275983Sadrian					tim_mcast = 1;
1440275983Sadrian				}
1441275983Sadrian
1442275983Sadrian				/*
1443275983Sadrian				 * If the TIM indicates there's traffic for
1444275983Sadrian				 * us then get us out of STA mode powersave.
1445275983Sadrian				 */
1446275983Sadrian				if (tim_ucast == 1) {
1447275983Sadrian
1448275983Sadrian					/*
1449275983Sadrian					 * Wake us out of SLEEP state if we're
1450275983Sadrian					 * in it; and if we're doing bgscan
1451275983Sadrian					 * then wake us out of STA powersave.
1452275983Sadrian					 */
1453264855Sadrian					ieee80211_sta_tim_notify(vap, 1);
1454275983Sadrian
1455275983Sadrian					/*
1456275983Sadrian					 * This is preventing us from
1457275983Sadrian					 * continuing a bgscan; because it
1458275983Sadrian					 * tricks the contbgscan()
1459275983Sadrian					 * routine to think there's always
1460275983Sadrian					 * traffic for us.
1461275983Sadrian					 *
1462275983Sadrian					 * I think we need both an RX and
1463275983Sadrian					 * TX ic_lastdata field.
1464275983Sadrian					 */
1465178354Ssam					ic->ic_lastdata = ticks;
1466178354Ssam				}
1467264906Sadrian
1468178354Ssam				ni->ni_dtim_count = tim->tim_count;
1469178354Ssam				ni->ni_dtim_period = tim->tim_period;
1470178354Ssam			}
1471193439Ssam			if (scan.csa != NULL &&
1472193439Ssam			    (vap->iv_flags & IEEE80211_F_DOTH))
1473193439Ssam				ieee80211_parse_csaparams(vap, scan.csa, wh);
1474193439Ssam			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1475193439Ssam				/*
1476193439Ssam				 * No CSA ie or 11h disabled, but a channel
1477193439Ssam				 * switch is pending; drop out so we aren't
1478193439Ssam				 * stuck in CSA state.  If the AP really is
1479193439Ssam				 * moving we'll get a beacon miss and scan.
1480193439Ssam				 */
1481193439Ssam				IEEE80211_LOCK(ic);
1482193439Ssam				ieee80211_csa_cancelswitch(ic);
1483193439Ssam				IEEE80211_UNLOCK(ic);
1484193439Ssam			}
1485178354Ssam			/*
1486178354Ssam			 * If scanning, pass the info to the scan module.
1487178354Ssam			 * Otherwise, check if it's the right time to do
1488178354Ssam			 * a background scan.  Background scanning must
1489178354Ssam			 * be enabled and we must not be operating in the
1490178354Ssam			 * turbo phase of dynamic turbo mode.  Then,
1491178354Ssam			 * it's been a while since the last background
1492178354Ssam			 * scan and if no data frames have come through
1493178354Ssam			 * recently, kick off a scan.  Note that this
1494178354Ssam			 * is the mechanism by which a background scan
1495178354Ssam			 * is started _and_ continued each time we
1496178354Ssam			 * return on-channel to receive a beacon from
1497178354Ssam			 * our ap.
1498178354Ssam			 */
1499178354Ssam			if (ic->ic_flags & IEEE80211_F_SCAN) {
1500283535Sadrian				ieee80211_add_scan(vap, rxchan,
1501282742Sadrian				    &scan, wh, subtype, rssi, nf);
1502178354Ssam			} else if (contbgscan(vap)) {
1503178354Ssam				ieee80211_bg_scan(vap, 0);
1504178354Ssam			} else if (startbgscan(vap)) {
1505178354Ssam				vap->iv_stats.is_scan_bg++;
1506178354Ssam#if 0
1507178354Ssam				/* wakeup if we are sleeing */
1508178354Ssam				ieee80211_set_pwrsave(vap, 0);
1509178354Ssam#endif
1510178354Ssam				ieee80211_bg_scan(vap, 0);
1511178354Ssam			}
1512233452Sadrian
1513233452Sadrian			/*
1514264855Sadrian			 * Put the station to sleep if we haven't seen
1515264855Sadrian			 * traffic in a while.
1516264855Sadrian			 */
1517264855Sadrian			IEEE80211_LOCK(ic);
1518264855Sadrian			ieee80211_sta_ps_timer_check(vap);
1519264855Sadrian			IEEE80211_UNLOCK(ic);
1520264855Sadrian
1521264855Sadrian			/*
1522233452Sadrian			 * If we've had a channel width change (eg HT20<->HT40)
1523233452Sadrian			 * then schedule a delayed driver notification.
1524233452Sadrian			 */
1525233452Sadrian			if (ht_state_change)
1526233452Sadrian				ieee80211_update_chw(ic);
1527178354Ssam			return;
1528178354Ssam		}
1529178354Ssam		/*
1530178354Ssam		 * If scanning, just pass information to the scan module.
1531178354Ssam		 */
1532178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1533178354Ssam			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1534178354Ssam				/*
1535178354Ssam				 * Actively scanning a channel marked passive;
1536178354Ssam				 * send a probe request now that we know there
1537178354Ssam				 * is 802.11 traffic present.
1538178354Ssam				 *
1539178354Ssam				 * XXX check if the beacon we recv'd gives
1540178354Ssam				 * us what we need and suppress the probe req
1541178354Ssam				 */
1542178354Ssam				ieee80211_probe_curchan(vap, 1);
1543178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1544178354Ssam			}
1545283535Sadrian			ieee80211_add_scan(vap, rxchan, &scan, wh,
1546282742Sadrian			    subtype, rssi, nf);
1547178354Ssam			return;
1548178354Ssam		}
1549178354Ssam		break;
1550178354Ssam	}
1551178354Ssam
1552178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH: {
1553178354Ssam		uint16_t algo, seq, status;
1554178354Ssam		/*
1555178354Ssam		 * auth frame format
1556178354Ssam		 *	[2] algorithm
1557178354Ssam		 *	[2] sequence
1558178354Ssam		 *	[2] status
1559178354Ssam		 *	[tlv*] challenge
1560178354Ssam		 */
1561178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1562178354Ssam		algo   = le16toh(*(uint16_t *)frm);
1563178354Ssam		seq    = le16toh(*(uint16_t *)(frm + 2));
1564178354Ssam		status = le16toh(*(uint16_t *)(frm + 4));
1565178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1566178354Ssam		    "recv auth frame with algorithm %d seq %d", algo, seq);
1567178354Ssam
1568178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1569178354Ssam			IEEE80211_DISCARD(vap,
1570178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1571178354Ssam			    wh, "auth", "%s", "TKIP countermeasures enabled");
1572178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1573178354Ssam			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1574178354Ssam				ieee80211_send_error(ni, wh->i_addr2,
1575178354Ssam					IEEE80211_FC0_SUBTYPE_AUTH,
1576178354Ssam					IEEE80211_REASON_MIC_FAILURE);
1577178354Ssam			}
1578178354Ssam			return;
1579178354Ssam		}
1580178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1581192468Ssam			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1582192468Ssam			    seq, status);
1583178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1584192468Ssam			sta_auth_open(ni, wh, rssi, nf, seq, status);
1585178354Ssam		else {
1586178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1587178354Ssam			    wh, "auth", "unsupported alg %d", algo);
1588178354Ssam			vap->iv_stats.is_rx_auth_unsupported++;
1589178354Ssam			return;
1590178354Ssam		}
1591178354Ssam		break;
1592178354Ssam	}
1593178354Ssam
1594178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1595178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1596178354Ssam		uint16_t capinfo, associd;
1597178354Ssam		uint16_t status;
1598178354Ssam
1599178354Ssam		if (vap->iv_state != IEEE80211_S_ASSOC) {
1600178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1601178354Ssam			return;
1602178354Ssam		}
1603178354Ssam
1604178354Ssam		/*
1605178354Ssam		 * asresp frame format
1606178354Ssam		 *	[2] capability information
1607178354Ssam		 *	[2] status
1608178354Ssam		 *	[2] association ID
1609178354Ssam		 *	[tlv] supported rates
1610178354Ssam		 *	[tlv] extended supported rates
1611178354Ssam		 *	[tlv] WME
1612178354Ssam		 *	[tlv] HT capabilities
1613178354Ssam		 *	[tlv] HT info
1614178354Ssam		 */
1615178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1616178354Ssam		ni = vap->iv_bss;
1617178354Ssam		capinfo = le16toh(*(uint16_t *)frm);
1618178354Ssam		frm += 2;
1619178354Ssam		status = le16toh(*(uint16_t *)frm);
1620178354Ssam		frm += 2;
1621178354Ssam		if (status != 0) {
1622178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1623178354Ssam			    wh->i_addr2, "%sassoc failed (reason %d)",
1624178354Ssam			    ISREASSOC(subtype) ?  "re" : "", status);
1625178354Ssam			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1626178354Ssam			return;
1627178354Ssam		}
1628178354Ssam		associd = le16toh(*(uint16_t *)frm);
1629178354Ssam		frm += 2;
1630178354Ssam
1631178354Ssam		rates = xrates = wme = htcap = htinfo = NULL;
1632178354Ssam		while (efrm - frm > 1) {
1633178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1634178354Ssam			switch (*frm) {
1635178354Ssam			case IEEE80211_ELEMID_RATES:
1636178354Ssam				rates = frm;
1637178354Ssam				break;
1638178354Ssam			case IEEE80211_ELEMID_XRATES:
1639178354Ssam				xrates = frm;
1640178354Ssam				break;
1641178354Ssam			case IEEE80211_ELEMID_HTCAP:
1642178354Ssam				htcap = frm;
1643178354Ssam				break;
1644178354Ssam			case IEEE80211_ELEMID_HTINFO:
1645178354Ssam				htinfo = frm;
1646178354Ssam				break;
1647178354Ssam			case IEEE80211_ELEMID_VENDOR:
1648178354Ssam				if (iswmeoui(frm))
1649178354Ssam					wme = frm;
1650193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1651178354Ssam					/*
1652178354Ssam					 * Accept pre-draft HT ie's if the
1653178354Ssam					 * standard ones have not been seen.
1654178354Ssam					 */
1655178354Ssam					if (ishtcapoui(frm)) {
1656178354Ssam						if (htcap == NULL)
1657178354Ssam							htcap = frm;
1658178354Ssam					} else if (ishtinfooui(frm)) {
1659178354Ssam						if (htinfo == NULL)
1660219603Sbschmidt							htinfo = frm;
1661178354Ssam					}
1662178354Ssam				}
1663178354Ssam				/* XXX Atheros OUI support */
1664178354Ssam				break;
1665178354Ssam			}
1666178354Ssam			frm += frm[1] + 2;
1667178354Ssam		}
1668178354Ssam
1669178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1670178354Ssam		if (xrates != NULL)
1671178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1672178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1673178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
1674178354Ssam				IEEE80211_F_JOIN |
1675178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1676178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1677178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
1678178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1679178354Ssam			    wh->i_addr2,
1680178354Ssam			    "%sassoc failed (rate set mismatch)",
1681178354Ssam			    ISREASSOC(subtype) ?  "re" : "");
1682178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
1683178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1684178354Ssam			    IEEE80211_SCAN_FAIL_STATUS);
1685178354Ssam			return;
1686178354Ssam		}
1687178354Ssam
1688178354Ssam		ni->ni_capinfo = capinfo;
1689178354Ssam		ni->ni_associd = associd;
1690178354Ssam		if (ni->ni_jointime == 0)
1691178354Ssam			ni->ni_jointime = time_uptime;
1692178354Ssam		if (wme != NULL &&
1693178354Ssam		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1694178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1695178354Ssam			ieee80211_wme_updateparams(vap);
1696178354Ssam		} else
1697178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1698178354Ssam		/*
1699178354Ssam		 * Setup HT state according to the negotiation.
1700178354Ssam		 *
1701178354Ssam		 * NB: shouldn't need to check if HT use is enabled but some
1702178354Ssam		 *     ap's send back HT ie's even when we don't indicate we
1703178354Ssam		 *     are HT capable in our AssocReq.
1704178354Ssam		 */
1705178354Ssam		if (htcap != NULL && htinfo != NULL &&
1706193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1707183254Ssam			ieee80211_ht_node_init(ni);
1708183254Ssam			ieee80211_ht_updateparams(ni, htcap, htinfo);
1709178354Ssam			ieee80211_setup_htrates(ni, htcap,
1710178354Ssam			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1711178354Ssam			ieee80211_setup_basic_htrates(ni, htinfo);
1712193966Ssam			ieee80211_node_setuptxparms(ni);
1713214894Sbschmidt			ieee80211_ratectl_node_init(ni);
1714190579Ssam		} else {
1715190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1716190579Ssam			if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1717190579Ssam				ieee80211_ff_node_init(ni);
1718190579Ssam#endif
1719178354Ssam		}
1720178354Ssam		/*
1721178354Ssam		 * Configure state now that we are associated.
1722178354Ssam		 *
1723178354Ssam		 * XXX may need different/additional driver callbacks?
1724178354Ssam		 */
1725178354Ssam		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1726178354Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1727178354Ssam			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1728178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1729178354Ssam		} else {
1730178354Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1731178354Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1732178354Ssam		}
1733178354Ssam		ieee80211_set_shortslottime(ic,
1734178354Ssam			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1735178354Ssam			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1736178354Ssam		/*
1737178354Ssam		 * Honor ERP protection.
1738178354Ssam		 *
1739178354Ssam		 * NB: ni_erp should zero for non-11g operation.
1740178354Ssam		 */
1741178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1742178354Ssam		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1743178354Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1744178354Ssam		else
1745178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1746178354Ssam		IEEE80211_NOTE_MAC(vap,
1747178354Ssam		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1748183256Ssam		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1749178354Ssam		    ISREASSOC(subtype) ? "re" : "",
1750178354Ssam		    IEEE80211_NODE_AID(ni),
1751178354Ssam		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1752178354Ssam		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1753178354Ssam		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1754178354Ssam		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1755178354Ssam		    ni->ni_flags & IEEE80211_NODE_HT ?
1756182834Ssam			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1757178354Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1758183255Ssam		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1759183255Ssam			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1760183256Ssam		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1761178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1762178354Ssam			", fast-frames" : "",
1763178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1764178354Ssam			", turbo" : ""
1765178354Ssam		);
1766178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1767178354Ssam		break;
1768178354Ssam	}
1769178354Ssam
1770178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1771178354Ssam		uint16_t reason;
1772178354Ssam
1773178354Ssam		if (vap->iv_state == IEEE80211_S_SCAN) {
1774178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1775178354Ssam			return;
1776178354Ssam		}
1777178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1778178354Ssam			/* NB: can happen when in promiscuous mode */
1779178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1780178354Ssam			break;
1781178354Ssam		}
1782178354Ssam
1783178354Ssam		/*
1784178354Ssam		 * deauth frame format
1785178354Ssam		 *	[2] reason
1786178354Ssam		 */
1787178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1788178354Ssam		reason = le16toh(*(uint16_t *)frm);
1789178354Ssam
1790178354Ssam		vap->iv_stats.is_rx_deauth++;
1791178354Ssam		vap->iv_stats.is_rx_deauth_code = reason;
1792178354Ssam		IEEE80211_NODE_STAT(ni, rx_deauth);
1793178354Ssam
1794178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1795178354Ssam		    "recv deauthenticate (reason %d)", reason);
1796178354Ssam		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1797178354Ssam		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1798178354Ssam		break;
1799178354Ssam	}
1800178354Ssam
1801178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1802178354Ssam		uint16_t reason;
1803178354Ssam
1804178354Ssam		if (vap->iv_state != IEEE80211_S_RUN &&
1805178354Ssam		    vap->iv_state != IEEE80211_S_ASSOC &&
1806178354Ssam		    vap->iv_state != IEEE80211_S_AUTH) {
1807178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1808178354Ssam			return;
1809178354Ssam		}
1810178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1811178354Ssam			/* NB: can happen when in promiscuous mode */
1812178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1813178354Ssam			break;
1814178354Ssam		}
1815178354Ssam
1816178354Ssam		/*
1817178354Ssam		 * disassoc frame format
1818178354Ssam		 *	[2] reason
1819178354Ssam		 */
1820178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1821178354Ssam		reason = le16toh(*(uint16_t *)frm);
1822178354Ssam
1823178354Ssam		vap->iv_stats.is_rx_disassoc++;
1824178354Ssam		vap->iv_stats.is_rx_disassoc_code = reason;
1825178354Ssam		IEEE80211_NODE_STAT(ni, rx_disassoc);
1826178354Ssam
1827178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1828178354Ssam		    "recv disassociate (reason %d)", reason);
1829178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1830178354Ssam		break;
1831178354Ssam	}
1832178354Ssam
1833178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
1834218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1835218958Sbschmidt		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1836218958Sbschmidt		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1837218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1838218958Sbschmidt			    wh, NULL, "%s", "not for us");
1839218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
1840218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
1841218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1842218927Sbschmidt			    wh, NULL, "wrong state %s",
1843218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
1844178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1845218958Sbschmidt		} else {
1846218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
1847218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1848218927Sbschmidt		}
1849178354Ssam		break;
1850178354Ssam
1851178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1852178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1853218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1854218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
1855218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1856218927Sbschmidt		    wh, NULL, "%s", "not handled");
1857178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
1858218927Sbschmidt		break;
1859218927Sbschmidt
1860178354Ssam	default:
1861178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1862218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
1863178354Ssam		vap->iv_stats.is_rx_badsubtype++;
1864178354Ssam		break;
1865178354Ssam	}
1866178354Ssam#undef ISREASSOC
1867178354Ssam#undef ISPROBE
1868178354Ssam}
1869191546Ssam
1870191546Ssamstatic void
1871205277Srpaulosta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1872191546Ssam{
1873205277Srpaulo	switch (subtype) {
1874205277Srpaulo	case IEEE80211_FC0_SUBTYPE_BAR:
1875205277Srpaulo		ieee80211_recv_bar(ni, m);
1876205277Srpaulo		break;
1877205277Srpaulo	}
1878191546Ssam}
1879