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: stable/11/sys/net80211/ieee80211_sta.c 344969 2019-03-09 12:54:10Z avos $");
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
158178354Ssam		/*
159178354Ssam		 * If we receive a beacon miss interrupt when using
160178354Ssam		 * dynamic turbo, attempt to switch modes before
161178354Ssam		 * reassociating.
162178354Ssam		 */
163178354Ssam		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
164178354Ssam			ieee80211_dturbo_switch(vap,
165178354Ssam			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
166190391Ssam#endif
167178354Ssam		/*
168178354Ssam		 * Try to reassociate before scanning for a new ap.
169178354Ssam		 */
170178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
171178354Ssam	} else {
172178354Ssam		/*
173178354Ssam		 * Somebody else is controlling state changes (e.g.
174178354Ssam		 * a user-mode app) don't do anything that would
175178354Ssam		 * confuse them; just drop into scan mode so they'll
176178354Ssam		 * notified of the state change and given control.
177178354Ssam		 */
178178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
179178354Ssam	}
180178354Ssam}
181178354Ssam
182178354Ssam/*
183178354Ssam * Handle deauth with reason.  We retry only for
184178354Ssam * the cases where we might succeed.  Otherwise
185178354Ssam * we downgrade the ap and scan.
186178354Ssam */
187178354Ssamstatic void
188178354Ssamsta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
189178354Ssam{
190178354Ssam	switch (reason) {
191178354Ssam	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
192178354Ssam	case IEEE80211_STATUS_TIMEOUT:
193178354Ssam	case IEEE80211_REASON_ASSOC_EXPIRE:
194178354Ssam	case IEEE80211_REASON_NOT_AUTHED:
195178354Ssam	case IEEE80211_REASON_NOT_ASSOCED:
196178354Ssam	case IEEE80211_REASON_ASSOC_LEAVE:
197178354Ssam	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
198178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
199178354Ssam		break;
200178354Ssam	default:
201178354Ssam		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
202178354Ssam		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
203178354Ssam			ieee80211_check_scan_current(vap);
204178354Ssam		break;
205178354Ssam	}
206178354Ssam}
207178354Ssam
208297164Savosstatic void
209297164Savossta_swbmiss_start(struct ieee80211vap *vap)
210297164Savos{
211297164Savos
212297164Savos	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) {
213297164Savos		/*
214297164Savos		 * Start s/w beacon miss timer for devices w/o
215297164Savos		 * hardware support.  We fudge a bit here since
216297164Savos		 * we're doing this in software.
217297164Savos		 */
218297164Savos		vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
219297164Savos		    2 * vap->iv_bmissthreshold * vap->iv_bss->ni_intval);
220297164Savos		vap->iv_swbmiss_count = 0;
221297164Savos		callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
222297164Savos		    ieee80211_swbmiss, vap);
223297164Savos	}
224297164Savos}
225297164Savos
226178354Ssam/*
227178354Ssam * IEEE80211_M_STA vap state machine handler.
228178354Ssam * This routine handles the main states in the 802.11 protocol.
229178354Ssam */
230178354Ssamstatic int
231178354Ssamsta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
232178354Ssam{
233178354Ssam	struct ieee80211com *ic = vap->iv_ic;
234178354Ssam	struct ieee80211_node *ni;
235178354Ssam	enum ieee80211_state ostate;
236178354Ssam
237178354Ssam	IEEE80211_LOCK_ASSERT(ic);
238178354Ssam
239178354Ssam	ostate = vap->iv_state;
240178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
241178354Ssam	    __func__, ieee80211_state_name[ostate],
242178354Ssam	    ieee80211_state_name[nstate], arg);
243178354Ssam	vap->iv_state = nstate;			/* state transition */
244178354Ssam	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
245178354Ssam	if (ostate != IEEE80211_S_SCAN)
246178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
247178354Ssam	ni = vap->iv_bss;			/* NB: no reference held */
248178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
249178354Ssam		callout_stop(&vap->iv_swbmiss);
250178354Ssam	switch (nstate) {
251178354Ssam	case IEEE80211_S_INIT:
252178354Ssam		switch (ostate) {
253178354Ssam		case IEEE80211_S_SLEEP:
254178354Ssam			/* XXX wakeup */
255264855Sadrian			/* XXX driver hook to wakeup the hardware? */
256178354Ssam		case IEEE80211_S_RUN:
257178354Ssam			IEEE80211_SEND_MGMT(ni,
258178354Ssam			    IEEE80211_FC0_SUBTYPE_DISASSOC,
259178354Ssam			    IEEE80211_REASON_ASSOC_LEAVE);
260178354Ssam			ieee80211_sta_leave(ni);
261178354Ssam			break;
262178354Ssam		case IEEE80211_S_ASSOC:
263178354Ssam			IEEE80211_SEND_MGMT(ni,
264178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
265178354Ssam			    IEEE80211_REASON_AUTH_LEAVE);
266178354Ssam			break;
267178354Ssam		case IEEE80211_S_SCAN:
268178354Ssam			ieee80211_cancel_scan(vap);
269178354Ssam			break;
270178354Ssam		default:
271296355Savos			break;
272178354Ssam		}
273178354Ssam		if (ostate != IEEE80211_S_INIT) {
274178354Ssam			/* NB: optimize INIT -> INIT case */
275178354Ssam			ieee80211_reset_bss(vap);
276178354Ssam		}
277178354Ssam		if (vap->iv_auth->ia_detach != NULL)
278178354Ssam			vap->iv_auth->ia_detach(vap);
279178354Ssam		break;
280178354Ssam	case IEEE80211_S_SCAN:
281178354Ssam		switch (ostate) {
282178354Ssam		case IEEE80211_S_INIT:
283178354Ssam			/*
284178354Ssam			 * Initiate a scan.  We can come here as a result
285178354Ssam			 * of an IEEE80211_IOC_SCAN_REQ too in which case
286178354Ssam			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
287178354Ssam			 * and the scan request parameters will be present
288178354Ssam			 * in iv_scanreq.  Otherwise we do the default.
289178354Ssam			 */
290178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
291178354Ssam				ieee80211_check_scan(vap,
292178354Ssam				    vap->iv_scanreq_flags,
293178354Ssam				    vap->iv_scanreq_duration,
294178354Ssam				    vap->iv_scanreq_mindwell,
295178354Ssam				    vap->iv_scanreq_maxdwell,
296178354Ssam				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
297178354Ssam				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
298178354Ssam			} else
299178354Ssam				ieee80211_check_scan_current(vap);
300178354Ssam			break;
301178354Ssam		case IEEE80211_S_SCAN:
302178354Ssam		case IEEE80211_S_AUTH:
303178354Ssam		case IEEE80211_S_ASSOC:
304178354Ssam			/*
305178354Ssam			 * These can happen either because of a timeout
306178354Ssam			 * on an assoc/auth response or because of a
307178354Ssam			 * change in state that requires a reset.  For
308178354Ssam			 * the former we're called with a non-zero arg
309178354Ssam			 * that is the cause for the failure; pass this
310178354Ssam			 * to the scan code so it can update state.
311178354Ssam			 * Otherwise trigger a new scan unless we're in
312178354Ssam			 * manual roaming mode in which case an application
313178354Ssam			 * must issue an explicit scan request.
314178354Ssam			 */
315178354Ssam			if (arg != 0)
316178354Ssam				ieee80211_scan_assoc_fail(vap,
317178354Ssam					vap->iv_bss->ni_macaddr, arg);
318178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
319178354Ssam				ieee80211_check_scan_current(vap);
320178354Ssam			break;
321264906Sadrian		case IEEE80211_S_SLEEP:		/* beacon miss */
322264906Sadrian			/*
323264906Sadrian			 * XXX if in sleep we need to wakeup the hardware.
324264906Sadrian			 */
325264906Sadrian			/* FALLTHROUGH */
326178354Ssam		case IEEE80211_S_RUN:		/* beacon miss */
327178354Ssam			/*
328178354Ssam			 * Beacon miss.  Notify user space and if not
329178354Ssam			 * under control of a user application (roaming
330178354Ssam			 * manual) kick off a scan to re-connect.
331178354Ssam			 */
332264906Sadrian
333178354Ssam			ieee80211_sta_leave(ni);
334178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
335178354Ssam				ieee80211_check_scan_current(vap);
336178354Ssam			break;
337178354Ssam		default:
338178354Ssam			goto invalid;
339178354Ssam		}
340178354Ssam		break;
341178354Ssam	case IEEE80211_S_AUTH:
342178354Ssam		switch (ostate) {
343178354Ssam		case IEEE80211_S_INIT:
344178354Ssam		case IEEE80211_S_SCAN:
345178354Ssam			IEEE80211_SEND_MGMT(ni,
346178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
347178354Ssam			break;
348178354Ssam		case IEEE80211_S_AUTH:
349178354Ssam		case IEEE80211_S_ASSOC:
350178354Ssam			switch (arg & 0xff) {
351178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
352178354Ssam				/* ??? */
353178354Ssam				IEEE80211_SEND_MGMT(ni,
354178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
355178354Ssam				break;
356178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
357178354Ssam				sta_authretry(vap, ni, arg>>8);
358178354Ssam				break;
359178354Ssam			}
360178354Ssam			break;
361297162Savos		case IEEE80211_S_SLEEP:
362178354Ssam		case IEEE80211_S_RUN:
363178354Ssam			switch (arg & 0xff) {
364178354Ssam			case IEEE80211_FC0_SUBTYPE_AUTH:
365178354Ssam				IEEE80211_SEND_MGMT(ni,
366178354Ssam				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
367297162Savos				vap->iv_state = IEEE80211_S_RUN; /* stay RUN */
368178354Ssam				break;
369178354Ssam			case IEEE80211_FC0_SUBTYPE_DEAUTH:
370178354Ssam				ieee80211_sta_leave(ni);
371178354Ssam				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
372178354Ssam					/* try to reauth */
373178354Ssam					IEEE80211_SEND_MGMT(ni,
374178354Ssam					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
375178354Ssam				}
376178354Ssam				break;
377178354Ssam			}
378178354Ssam			break;
379178354Ssam		default:
380178354Ssam			goto invalid;
381178354Ssam		}
382178354Ssam		break;
383178354Ssam	case IEEE80211_S_ASSOC:
384178354Ssam		switch (ostate) {
385178354Ssam		case IEEE80211_S_AUTH:
386178354Ssam		case IEEE80211_S_ASSOC:
387178354Ssam			IEEE80211_SEND_MGMT(ni,
388178354Ssam			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
389178354Ssam			break;
390178354Ssam		case IEEE80211_S_SLEEP:		/* cannot happen */
391178354Ssam		case IEEE80211_S_RUN:
392178354Ssam			ieee80211_sta_leave(ni);
393178354Ssam			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
394178354Ssam				IEEE80211_SEND_MGMT(ni, arg ?
395178354Ssam				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
396178354Ssam				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
397178354Ssam			}
398178354Ssam			break;
399178354Ssam		default:
400178354Ssam			goto invalid;
401178354Ssam		}
402178354Ssam		break;
403178354Ssam	case IEEE80211_S_RUN:
404178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA) {
405178354Ssam			/* XXX validate prerequisites */
406178354Ssam		}
407178354Ssam		switch (ostate) {
408178354Ssam		case IEEE80211_S_RUN:
409193439Ssam		case IEEE80211_S_CSA:
410178354Ssam			break;
411178354Ssam		case IEEE80211_S_AUTH:		/* when join is done in fw */
412178354Ssam		case IEEE80211_S_ASSOC:
413178354Ssam#ifdef IEEE80211_DEBUG
414178354Ssam			if (ieee80211_msg_debug(vap)) {
415178354Ssam				ieee80211_note(vap, "%s with %s ssid ",
416178354Ssam				    (vap->iv_opmode == IEEE80211_M_STA ?
417178354Ssam				    "associated" : "synchronized"),
418178354Ssam				    ether_sprintf(ni->ni_bssid));
419178354Ssam				ieee80211_print_essid(vap->iv_bss->ni_essid,
420178354Ssam				    ni->ni_esslen);
421178354Ssam				/* XXX MCS/HT */
422178354Ssam				printf(" channel %d start %uMb\n",
423178354Ssam				    ieee80211_chan2ieee(ic, ic->ic_curchan),
424178354Ssam				    IEEE80211_RATE2MBS(ni->ni_txrate));
425178354Ssam			}
426178354Ssam#endif
427178354Ssam			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
428178354Ssam			ieee80211_notify_node_join(ni,
429178354Ssam			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
430178354Ssam			break;
431178354Ssam		case IEEE80211_S_SLEEP:
432264855Sadrian			/* Wake up from sleep */
433241138Sadrian			vap->iv_sta_ps(vap, 0);
434178354Ssam			break;
435178354Ssam		default:
436178354Ssam			goto invalid;
437178354Ssam		}
438178354Ssam		ieee80211_sync_curchan(ic);
439297164Savos		if (ostate != IEEE80211_S_RUN)
440297164Savos			sta_swbmiss_start(vap);
441178354Ssam		/*
442178354Ssam		 * When 802.1x is not in use mark the port authorized
443178354Ssam		 * at this point so traffic can flow.
444178354Ssam		 */
445178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
446178354Ssam			ieee80211_node_authorize(ni);
447184345Ssam		/*
448184345Ssam		 * Fake association when joining an existing bss.
449264855Sadrian		 *
450264855Sadrian		 * Don't do this if we're doing SLEEP->RUN.
451184345Ssam		 */
452264855Sadrian		if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
453264855Sadrian			ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
454178354Ssam		break;
455193439Ssam	case IEEE80211_S_CSA:
456193439Ssam		if (ostate != IEEE80211_S_RUN)
457193439Ssam			goto invalid;
458193439Ssam		break;
459178354Ssam	case IEEE80211_S_SLEEP:
460297164Savos		sta_swbmiss_start(vap);
461241138Sadrian		vap->iv_sta_ps(vap, 1);
462178354Ssam		break;
463178354Ssam	default:
464178354Ssam	invalid:
465184268Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
466184268Ssam		    "%s: unexpected state transition %s -> %s\n", __func__,
467178354Ssam		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
468178354Ssam		break;
469178354Ssam	}
470178354Ssam	return 0;
471178354Ssam}
472178354Ssam
473178354Ssam/*
474178354Ssam * Return non-zero if the frame is an echo of a multicast
475178354Ssam * frame sent by ourself.  The dir is known to be DSTODS.
476178354Ssam */
477178354Ssamstatic __inline int
478178354Ssamisdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
479178354Ssam{
480178354Ssam#define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
481178354Ssam#define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
482178354Ssam	const uint8_t *sa;
483178354Ssam
484178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
485178354Ssam
486178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
487178354Ssam		return 0;
488178354Ssam	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
489178354Ssam	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
490178354Ssam#undef WH4
491178354Ssam#undef QWH4
492178354Ssam}
493178354Ssam
494178354Ssam/*
495178354Ssam * Return non-zero if the frame is an echo of a multicast
496178354Ssam * frame sent by ourself.  The dir is known to be FROMDS.
497178354Ssam */
498178354Ssamstatic __inline int
499178354Ssamisfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
500178354Ssam{
501178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
502178354Ssam
503178354Ssam	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
504178354Ssam		return 0;
505178354Ssam	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
506178354Ssam}
507178354Ssam
508178354Ssam/*
509178354Ssam * Decide if a received management frame should be
510178354Ssam * printed when debugging is enabled.  This filters some
511178354Ssam * of the less interesting frames that come frequently
512178354Ssam * (e.g. beacons).
513178354Ssam */
514178354Ssamstatic __inline int
515178354Ssamdoprint(struct ieee80211vap *vap, int subtype)
516178354Ssam{
517178354Ssam	switch (subtype) {
518178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
519178354Ssam		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
520178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
521178354Ssam		return 0;
522178354Ssam	}
523178354Ssam	return 1;
524178354Ssam}
525178354Ssam
526178354Ssam/*
527178354Ssam * Process a received frame.  The node associated with the sender
528178354Ssam * should be supplied.  If nothing was found in the node table then
529178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
530178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
531178354Ssam * during AP scanning to select a AP to associate with; it can have
532178354Ssam * any units so long as values have consistent units and higher values
533178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
534178354Ssam * by the 802.11 layer.
535178354Ssam */
536178354Ssamstatic int
537283535Sadriansta_input(struct ieee80211_node *ni, struct mbuf *m,
538283535Sadrian    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
539178354Ssam{
540178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
541178354Ssam	struct ieee80211com *ic = ni->ni_ic;
542178354Ssam	struct ifnet *ifp = vap->iv_ifp;
543178354Ssam	struct ieee80211_frame *wh;
544178354Ssam	struct ieee80211_key *key;
545178354Ssam	struct ether_header *eh;
546203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
547178354Ssam	uint8_t dir, type, subtype, qos;
548178354Ssam	uint8_t *bssid;
549178354Ssam
550183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
551178354Ssam		/*
552178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
553183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
554183247Ssam		 * here but were received out of order and been held on
555183247Ssam		 * the reorder queue.  When resubmitted they are marked
556183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
557183247Ssam		 * the normal processing.
558178354Ssam		 */
559178354Ssam		wh = mtod(m, struct ieee80211_frame *);
560178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
561178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
562178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
563178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
564178354Ssam		goto resubmit_ampdu;
565178354Ssam	}
566178354Ssam
567178354Ssam	KASSERT(ni != NULL, ("null node"));
568178354Ssam	ni->ni_inact = ni->ni_inact_reload;
569178354Ssam
570178354Ssam	type = -1;			/* undefined */
571178354Ssam
572178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
573178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
574178354Ssam		    ni->ni_macaddr, NULL,
575178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
576178354Ssam		vap->iv_stats.is_rx_tooshort++;
577178354Ssam		goto out;
578178354Ssam	}
579178354Ssam	/*
580178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
581178354Ssam	 * frame format but don't reference fields past outside
582178354Ssam	 * ieee80211_frame_min w/o first validating the data is
583178354Ssam	 * present.
584178354Ssam	 */
585178354Ssam	wh = mtod(m, struct ieee80211_frame *);
586178354Ssam
587178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
588178354Ssam	    IEEE80211_FC0_VERSION_0) {
589178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
590191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
591191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
592178354Ssam		vap->iv_stats.is_rx_badversion++;
593178354Ssam		goto err;
594178354Ssam	}
595178354Ssam
596178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
597178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
598178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
599178354Ssam	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
600178354Ssam		bssid = wh->i_addr2;
601178354Ssam		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
602178354Ssam			/* not interested in */
603178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
604178354Ssam			    bssid, NULL, "%s", "not to bss");
605178354Ssam			vap->iv_stats.is_rx_wrongbss++;
606178354Ssam			goto out;
607178354Ssam		}
608227338Sadrian
609227338Sadrian		/*
610227338Sadrian		 * Some devices may be in a promiscuous mode
611227338Sadrian		 * where they receive frames for multiple station
612227338Sadrian		 * addresses.
613227338Sadrian		 *
614227338Sadrian		 * If we receive a data frame that isn't
615227338Sadrian		 * destined to our VAP MAC, drop it.
616227338Sadrian		 *
617227338Sadrian		 * XXX TODO: This is only enforced when not scanning;
618227338Sadrian		 * XXX it assumes a software-driven scan will put the NIC
619227338Sadrian		 * XXX into a "no data frames" mode before setting this
620227338Sadrian		 * XXX flag. Otherwise it may be possible that we'll still
621227338Sadrian		 * XXX process data frames whilst scanning.
622227338Sadrian		 */
623227338Sadrian		if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
624227338Sadrian		    && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
625227338Sadrian			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
626227338Sadrian			    bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
627227338Sadrian			    IF_LLADDR(ifp), ":", wh->i_addr1, ":");
628227338Sadrian			vap->iv_stats.is_rx_wrongbss++;
629227338Sadrian			goto out;
630227338Sadrian		}
631227338Sadrian
632178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
633192468Ssam		ni->ni_noise = nf;
634282820Sadrian		if ( IEEE80211_HAS_SEQ(type, subtype) &&
635282820Sadrian		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
636178354Ssam			uint8_t tid = ieee80211_gettid(wh);
637178354Ssam			if (IEEE80211_QOS_HAS_SEQ(wh) &&
638178354Ssam			    TID_TO_WME_AC(tid) >= WME_AC_VI)
639178354Ssam				ic->ic_wme.wme_hipri_traffic++;
640296254Savos			if (! ieee80211_check_rxseq(ni, wh, bssid))
641178354Ssam				goto out;
642178354Ssam		}
643178354Ssam	}
644178354Ssam
645178354Ssam	switch (type) {
646178354Ssam	case IEEE80211_FC0_TYPE_DATA:
647178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
648178354Ssam		if (m->m_len < hdrspace &&
649178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
650178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
651178354Ssam			    ni->ni_macaddr, NULL,
652178354Ssam			    "data too short: expecting %u", hdrspace);
653178354Ssam			vap->iv_stats.is_rx_tooshort++;
654178354Ssam			goto out;		/* XXX */
655178354Ssam		}
656178354Ssam		/*
657183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
658183247Ssam		 * processed directly then ieee80211_ampdu_reorder
659178354Ssam		 * will return 0; otherwise it has consumed the mbuf
660178354Ssam		 * and we should do nothing more with it.
661178354Ssam		 */
662183247Ssam		if ((m->m_flags & M_AMPDU) &&
663178354Ssam		    (dir == IEEE80211_FC1_DIR_FROMDS ||
664178354Ssam		     dir == IEEE80211_FC1_DIR_DSTODS) &&
665178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
666178354Ssam			m = NULL;
667178354Ssam			goto out;
668178354Ssam		}
669178354Ssam	resubmit_ampdu:
670178354Ssam		if (dir == IEEE80211_FC1_DIR_FROMDS) {
671178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
672178354Ssam			    isfromds_mcastecho(vap, wh)) {
673178354Ssam				/*
674178354Ssam				 * In IEEE802.11 network, multicast
675178354Ssam				 * packets sent from "me" are broadcast
676178354Ssam				 * from the AP; silently discard for
677178354Ssam				 * SIMPLEX interface.
678178354Ssam				 */
679178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
680178354Ssam				    wh, "data", "%s", "multicast echo");
681178354Ssam				vap->iv_stats.is_rx_mcastecho++;
682178354Ssam				goto out;
683178354Ssam			}
684178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
685178354Ssam			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
686178354Ssam				/*
687178354Ssam				 * DWDS sta's must drop 3-address mcast frames
688178354Ssam				 * as they will be sent separately as a 4-addr
689178354Ssam				 * frame.  Accepting the 3-addr frame will
690178354Ssam				 * confuse the bridge into thinking the sending
691178354Ssam				 * sta is located at the end of WDS link.
692178354Ssam				 */
693178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
694178354Ssam				    "3-address data", "%s", "DWDS enabled");
695178354Ssam				vap->iv_stats.is_rx_mcastecho++;
696178354Ssam				goto out;
697178354Ssam			}
698178354Ssam		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
699178354Ssam			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
700178354Ssam				IEEE80211_DISCARD(vap,
701178354Ssam				    IEEE80211_MSG_INPUT, wh, "4-address data",
702178354Ssam				    "%s", "DWDS not enabled");
703178354Ssam				vap->iv_stats.is_rx_wrongdir++;
704178354Ssam				goto out;
705178354Ssam			}
706178354Ssam			if ((ifp->if_flags & IFF_SIMPLEX) &&
707178354Ssam			    isdstods_mcastecho(vap, wh)) {
708178354Ssam				/*
709178354Ssam				 * In IEEE802.11 network, multicast
710178354Ssam				 * packets sent from "me" are broadcast
711178354Ssam				 * from the AP; silently discard for
712178354Ssam				 * SIMPLEX interface.
713178354Ssam				 */
714178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
715178354Ssam				    "4-address data", "%s", "multicast echo");
716178354Ssam				vap->iv_stats.is_rx_mcastecho++;
717178354Ssam				goto out;
718178354Ssam			}
719178354Ssam		} else {
720178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
721178354Ssam			    "data", "incorrect dir 0x%x", dir);
722178354Ssam			vap->iv_stats.is_rx_wrongdir++;
723178354Ssam			goto out;
724178354Ssam		}
725178354Ssam
726178354Ssam		/*
727178354Ssam		 * Handle privacy requirements.  Note that we
728178354Ssam		 * must not be preempted from here until after
729178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
730178354Ssam		 * otherwise we may violate assumptions in the
731178354Ssam		 * crypto cipher modules used to do delayed update
732178354Ssam		 * of replay sequence numbers.
733178354Ssam		 */
734260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
735178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
736178354Ssam				/*
737178354Ssam				 * Discard encrypted frames when privacy is off.
738178354Ssam				 */
739178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
740178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
741178354Ssam				vap->iv_stats.is_rx_noprivacy++;
742178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
743178354Ssam				goto out;
744178354Ssam			}
745178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
746178354Ssam			if (key == NULL) {
747178354Ssam				/* NB: stats+msgs handled in crypto_decap */
748178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
749178354Ssam				goto out;
750178354Ssam			}
751178354Ssam			wh = mtod(m, struct ieee80211_frame *);
752260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
753178354Ssam		} else {
754178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
755178354Ssam			key = NULL;
756178354Ssam		}
757178354Ssam
758178354Ssam		/*
759178354Ssam		 * Save QoS bits for use below--before we strip the header.
760178354Ssam		 */
761344969Savos		if (subtype == IEEE80211_FC0_SUBTYPE_QOS)
762344969Savos			qos = ieee80211_getqos(wh)[0];
763344969Savos		else
764178354Ssam			qos = 0;
765178354Ssam
766178354Ssam		/*
767178354Ssam		 * Next up, any fragmentation.
768178354Ssam		 */
769178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
770178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
771178354Ssam			if (m == NULL) {
772178354Ssam				/* Fragment dropped or frame not complete yet */
773178354Ssam				goto out;
774178354Ssam			}
775178354Ssam		}
776178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
777178354Ssam
778178354Ssam		/*
779178354Ssam		 * Next strip any MSDU crypto bits.
780178354Ssam		 */
781178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
782178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
783178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
784178354Ssam			vap->iv_stats.is_rx_demicfail++;
785178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
786178354Ssam			goto out;
787178354Ssam		}
788178354Ssam
789178354Ssam		/* copy to listener after decrypt */
790192468Ssam		if (ieee80211_radiotap_active_vap(vap))
791202967Srpaulo			ieee80211_radiotap_rx(vap, m);
792178354Ssam		need_tap = 0;
793178354Ssam
794178354Ssam		/*
795178354Ssam		 * Finally, strip the 802.11 header.
796178354Ssam		 */
797178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
798178354Ssam		if (m == NULL) {
799178354Ssam			/* XXX mask bit to check for both */
800178354Ssam			/* don't count Null data frames as errors */
801178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
802178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
803178354Ssam				goto out;
804178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
805178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
806178354Ssam			vap->iv_stats.is_rx_decap++;
807178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
808178354Ssam			goto err;
809178354Ssam		}
810178354Ssam		eh = mtod(m, struct ether_header *);
811178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
812178354Ssam			/*
813178354Ssam			 * Deny any non-PAE frames received prior to
814178354Ssam			 * authorization.  For open/shared-key
815178354Ssam			 * authentication the port is mark authorized
816178354Ssam			 * after authentication completes.  For 802.1x
817178354Ssam			 * the port is not marked authorized by the
818178354Ssam			 * authenticator until the handshake has completed.
819178354Ssam			 */
820178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
821178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
822178354Ssam				    eh->ether_shost, "data",
823178354Ssam				    "unauthorized port: ether type 0x%x len %u",
824178354Ssam				    eh->ether_type, m->m_pkthdr.len);
825178354Ssam				vap->iv_stats.is_rx_unauth++;
826178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
827178354Ssam				goto err;
828178354Ssam			}
829178354Ssam		} else {
830178354Ssam			/*
831178354Ssam			 * When denying unencrypted frames, discard
832178354Ssam			 * any non-PAE frames received without encryption.
833178354Ssam			 */
834178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
835178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
836178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
837178354Ssam				/*
838178354Ssam				 * Drop unencrypted frames.
839178354Ssam				 */
840178354Ssam				vap->iv_stats.is_rx_unencrypted++;
841178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
842178354Ssam				goto out;
843178354Ssam			}
844178354Ssam		}
845178354Ssam		/* XXX require HT? */
846178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
847178354Ssam			m = ieee80211_decap_amsdu(ni, m);
848178354Ssam			if (m == NULL)
849178354Ssam				return IEEE80211_FC0_TYPE_DATA;
850190391Ssam		} else {
851190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
852190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
853190391Ssam			if (m == NULL)
854178354Ssam				return IEEE80211_FC0_TYPE_DATA;
855190391Ssam#endif
856178354Ssam		}
857178354Ssam		ieee80211_deliver_data(vap, ni, m);
858178354Ssam		return IEEE80211_FC0_TYPE_DATA;
859178354Ssam
860178354Ssam	case IEEE80211_FC0_TYPE_MGT:
861178354Ssam		vap->iv_stats.is_rx_mgmt++;
862178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
863178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
864178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
865178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
866178354Ssam			vap->iv_stats.is_rx_wrongdir++;
867178354Ssam			goto err;
868178354Ssam		}
869178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
870178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
871178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
872178354Ssam			    m->m_pkthdr.len);
873178354Ssam			vap->iv_stats.is_rx_tooshort++;
874178354Ssam			goto out;
875178354Ssam		}
876178354Ssam#ifdef IEEE80211_DEBUG
877178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
878178354Ssam		    ieee80211_msg_dumppkts(vap)) {
879178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
880298376Savos			    ieee80211_mgt_subtype_name(subtype),
881178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
882178354Ssam		}
883178354Ssam#endif
884260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
885178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
886178354Ssam				/*
887178354Ssam				 * Only shared key auth frames with a challenge
888178354Ssam				 * should be encrypted, discard all others.
889178354Ssam				 */
890178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
891298376Savos				    wh, ieee80211_mgt_subtype_name(subtype),
892178354Ssam				    "%s", "WEP set but not permitted");
893178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
894178354Ssam				goto out;
895178354Ssam			}
896178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
897178354Ssam				/*
898178354Ssam				 * Discard encrypted frames when privacy is off.
899178354Ssam				 */
900178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
901178354Ssam				    wh, "mgt", "%s", "WEP set but PRIVACY off");
902178354Ssam				vap->iv_stats.is_rx_noprivacy++;
903178354Ssam				goto out;
904178354Ssam			}
905178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
906178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
907178354Ssam			if (key == NULL) {
908178354Ssam				/* NB: stats+msgs handled in crypto_decap */
909178354Ssam				goto out;
910178354Ssam			}
911178354Ssam			wh = mtod(m, struct ieee80211_frame *);
912260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
913178354Ssam		}
914283535Sadrian		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
915191534Ssam		goto out;
916178354Ssam
917178354Ssam	case IEEE80211_FC0_TYPE_CTL:
918178354Ssam		vap->iv_stats.is_rx_ctl++;
919178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
920191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
921178354Ssam		goto out;
922191549Ssam
923178354Ssam	default:
924178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
925178354Ssam		    wh, NULL, "bad frame type 0x%x", type);
926178354Ssam		/* should not come here */
927178354Ssam		break;
928178354Ssam	}
929178354Ssamerr:
930271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
931178354Ssamout:
932178354Ssam	if (m != NULL) {
933192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
934192468Ssam			ieee80211_radiotap_rx(vap, m);
935178354Ssam		m_freem(m);
936178354Ssam	}
937178354Ssam	return type;
938178354Ssam}
939178354Ssam
940178354Ssamstatic void
941178354Ssamsta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
942192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
943178354Ssam{
944178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
945178354Ssam
946178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
947178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
948178354Ssam		    ni->ni_macaddr, "open auth",
949178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
950178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
951178354Ssam		return;
952178354Ssam	}
953178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH ||
954178354Ssam	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
955178354Ssam		vap->iv_stats.is_rx_bad_auth++;
956178354Ssam		return;
957178354Ssam	}
958178354Ssam	if (status != 0) {
959178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
960178354Ssam		    ni, "open auth failed (reason %d)", status);
961178354Ssam		vap->iv_stats.is_rx_auth_fail++;
962178354Ssam		vap->iv_stats.is_rx_authfail_code = status;
963178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
964178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
965178354Ssam	} else
966178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
967178354Ssam}
968178354Ssam
969178354Ssamstatic void
970178354Ssamsta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
971192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
972178354Ssam    uint16_t seq, uint16_t status)
973178354Ssam{
974178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
975178354Ssam	uint8_t *challenge;
976178354Ssam
977178354Ssam	/*
978178354Ssam	 * NB: this can happen as we allow pre-shared key
979178354Ssam	 * authentication to be enabled w/o wep being turned
980178354Ssam	 * on so that configuration of these can be done
981178354Ssam	 * in any order.  It may be better to enforce the
982178354Ssam	 * ordering in which case this check would just be
983178354Ssam	 * for sanity/consistency.
984178354Ssam	 */
985178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
986178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
987178354Ssam		    ni->ni_macaddr, "shared key auth",
988178354Ssam		    "%s", " PRIVACY is disabled");
989178354Ssam		goto bad;
990178354Ssam	}
991178354Ssam	/*
992178354Ssam	 * Pre-shared key authentication is evil; accept
993178354Ssam	 * it only if explicitly configured (it is supported
994178354Ssam	 * mainly for compatibility with clients like OS X).
995178354Ssam	 */
996178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
997178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
998178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
999178354Ssam		    ni->ni_macaddr, "shared key auth",
1000178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1001178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1002178354Ssam		goto bad;
1003178354Ssam	}
1004178354Ssam
1005178354Ssam	challenge = NULL;
1006178354Ssam	if (frm + 1 < efrm) {
1007178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1008178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1009178354Ssam			    ni->ni_macaddr, "shared key auth",
1010178354Ssam			    "ie %d/%d too long",
1011178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1012178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1013178354Ssam			goto bad;
1014178354Ssam		}
1015178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1016178354Ssam			challenge = frm;
1017178354Ssam		frm += frm[1] + 2;
1018178354Ssam	}
1019178354Ssam	switch (seq) {
1020178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1021178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1022178354Ssam		if (challenge == NULL) {
1023178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1024178354Ssam			    ni->ni_macaddr, "shared key auth",
1025178354Ssam			    "%s", "no challenge");
1026178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1027178354Ssam			goto bad;
1028178354Ssam		}
1029178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1030178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1031178354Ssam			    ni->ni_macaddr, "shared key auth",
1032178354Ssam			    "bad challenge len %d", challenge[1]);
1033178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1034178354Ssam			goto bad;
1035178354Ssam		}
1036178354Ssam	default:
1037178354Ssam		break;
1038178354Ssam	}
1039178354Ssam	if (vap->iv_state != IEEE80211_S_AUTH)
1040178354Ssam		return;
1041178354Ssam	switch (seq) {
1042178354Ssam	case IEEE80211_AUTH_SHARED_PASS:
1043178354Ssam		if (ni->ni_challenge != NULL) {
1044283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1045178354Ssam			ni->ni_challenge = NULL;
1046178354Ssam		}
1047178354Ssam		if (status != 0) {
1048178354Ssam			IEEE80211_NOTE_FRAME(vap,
1049178354Ssam			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1050178354Ssam			    "shared key auth failed (reason %d)", status);
1051178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1052178354Ssam			vap->iv_stats.is_rx_authfail_code = status;
1053178354Ssam			return;
1054178354Ssam		}
1055178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1056178354Ssam		break;
1057178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1058178354Ssam		if (!ieee80211_alloc_challenge(ni))
1059178354Ssam			return;
1060178354Ssam		/* XXX could optimize by passing recvd challenge */
1061178354Ssam		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1062178354Ssam		IEEE80211_SEND_MGMT(ni,
1063178354Ssam			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1064178354Ssam		break;
1065178354Ssam	default:
1066178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1067178354Ssam		    wh, "shared key auth", "bad seq %d", seq);
1068178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1069178354Ssam		return;
1070178354Ssam	}
1071178354Ssam	return;
1072178354Ssambad:
1073178354Ssam	/*
1074178354Ssam	 * Kick the state machine.  This short-circuits
1075178354Ssam	 * using the mgt frame timeout to trigger the
1076178354Ssam	 * state transition.
1077178354Ssam	 */
1078178354Ssam	if (vap->iv_state == IEEE80211_S_AUTH)
1079178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1080178354Ssam		    IEEE80211_SCAN_FAIL_STATUS);
1081178354Ssam}
1082178354Ssam
1083244060Sadrianint
1084178354Ssamieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1085178354Ssam	const struct ieee80211_frame *wh)
1086178354Ssam{
1087178354Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1088178354Ssam	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1089178354Ssam	u_int len = frm[1], qosinfo;
1090178354Ssam	int i;
1091178354Ssam
1092178354Ssam	if (len < sizeof(struct ieee80211_wme_param)-2) {
1093178354Ssam		IEEE80211_DISCARD_IE(vap,
1094178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1095178354Ssam		    wh, "WME", "too short, len %u", len);
1096178354Ssam		return -1;
1097178354Ssam	}
1098178354Ssam	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1099178354Ssam	qosinfo &= WME_QOSINFO_COUNT;
1100178354Ssam	/* XXX do proper check for wraparound */
1101178354Ssam	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1102178354Ssam		return 0;
1103178354Ssam	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1104178354Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1105178354Ssam		struct wmeParams *wmep =
1106178354Ssam			&wme->wme_wmeChanParams.cap_wmeParams[i];
1107178354Ssam		/* NB: ACI not used */
1108178354Ssam		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1109178354Ssam		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1110178354Ssam		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1111178354Ssam		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1112298359Savos		wmep->wmep_txopLimit = le16dec(frm+2);
1113178354Ssam		frm += 4;
1114178354Ssam	}
1115178354Ssam	wme->wme_wmeChanParams.cap_info = qosinfo;
1116178354Ssam	return 1;
1117178354Ssam#undef MS
1118178354Ssam}
1119178354Ssam
1120178354Ssam/*
1121193439Ssam * Process 11h Channel Switch Announcement (CSA) ie.  If this
1122193439Ssam * is the first CSA then initiate the switch.  Otherwise we
1123193439Ssam * track state and trigger completion and/or cancel of the switch.
1124193439Ssam * XXX should be public for IBSS use
1125193439Ssam */
1126193439Ssamstatic void
1127193439Ssamieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1128193439Ssam	const struct ieee80211_frame *wh)
1129193439Ssam{
1130193439Ssam	struct ieee80211com *ic = vap->iv_ic;
1131193439Ssam	const struct ieee80211_csa_ie *csa =
1132193439Ssam	    (const struct ieee80211_csa_ie *) frm;
1133193439Ssam
1134193439Ssam	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1135193439Ssam	    ("state %s", ieee80211_state_name[vap->iv_state]));
1136193439Ssam
1137193439Ssam	if (csa->csa_mode > 1) {
1138193439Ssam		IEEE80211_DISCARD_IE(vap,
1139193439Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1140193439Ssam		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1141193439Ssam		return;
1142193439Ssam	}
1143193439Ssam	IEEE80211_LOCK(ic);
1144193439Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1145193439Ssam		/*
1146193439Ssam		 * Convert the channel number to a channel reference.  We
1147193439Ssam		 * try first to preserve turbo attribute of the current
1148193439Ssam		 * channel then fallback.  Note this will not work if the
1149193439Ssam		 * CSA specifies a channel that requires a band switch (e.g.
1150193439Ssam		 * 11a => 11g).  This is intentional as 11h is defined only
1151193439Ssam		 * for 5GHz/11a and because the switch does not involve a
1152193439Ssam		 * reassociation, protocol state (capabilities, negotated
1153193439Ssam		 * rates, etc) may/will be wrong.
1154193439Ssam		 */
1155193439Ssam		struct ieee80211_channel *c =
1156193439Ssam		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1157193439Ssam			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1158193439Ssam		if (c == NULL) {
1159193439Ssam			c = ieee80211_find_channel_byieee(ic,
1160193439Ssam			    csa->csa_newchan,
1161193439Ssam			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1162193439Ssam			if (c == NULL) {
1163193439Ssam				IEEE80211_DISCARD_IE(vap,
1164193439Ssam				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1165193439Ssam				    wh, "CSA", "invalid channel %u",
1166193439Ssam				    csa->csa_newchan);
1167193439Ssam				goto done;
1168193439Ssam			}
1169193439Ssam		}
1170193439Ssam#if IEEE80211_CSA_COUNT_MIN > 0
1171193439Ssam		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1172193439Ssam			/*
1173193439Ssam			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1174193439Ssam			 * reduce the risk of being redirected by a fabricated
1175193439Ssam			 * CSA.  If a valid CSA is dropped we'll still get a
1176193439Ssam			 * beacon miss when the AP leaves the channel so we'll
1177193439Ssam			 * eventually follow to the new channel.
1178193439Ssam			 *
1179193439Ssam			 * NOTE: this violates the 11h spec that states that
1180193439Ssam			 * count may be any value and if 0 then a switch
1181193439Ssam			 * should happen asap.
1182193439Ssam			 */
1183193439Ssam			IEEE80211_DISCARD_IE(vap,
1184193439Ssam			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1185193439Ssam			    wh, "CSA", "count %u too small, must be >= %u",
1186193439Ssam			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1187193439Ssam			goto done;
1188193439Ssam		}
1189193439Ssam#endif
1190193439Ssam		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1191193439Ssam	} else {
1192193439Ssam		/*
1193193439Ssam		 * Validate this ie against the initial CSA.  We require
1194193439Ssam		 * mode and channel not change and the count must be
1195193439Ssam		 * monotonically decreasing.  This may be pointless and
1196193439Ssam		 * canceling the switch as a result may be too paranoid but
1197193439Ssam		 * in the worst case if we drop out of CSA because of this
1198193439Ssam		 * and the AP does move then we'll just end up taking a
1199193439Ssam		 * beacon miss and scan to find the AP.
1200193439Ssam		 *
1201193439Ssam		 * XXX may want <= on count as we also process ProbeResp
1202193439Ssam		 * frames and those may come in w/ the same count as the
1203193439Ssam		 * previous beacon; but doing so leaves us open to a stuck
1204193439Ssam		 * count until we add a dead-man timer
1205193439Ssam		 */
1206193439Ssam		if (!(csa->csa_count < ic->ic_csa_count &&
1207193439Ssam		      csa->csa_mode == ic->ic_csa_mode &&
1208193439Ssam		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1209193439Ssam			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1210193439Ssam			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1211193439Ssam			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1212193439Ssam			    ic->ic_csa_newchan, ic->ic_csa_count,
1213193439Ssam			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1214193439Ssam			ieee80211_csa_cancelswitch(ic);
1215193439Ssam		} else {
1216193439Ssam			if (csa->csa_count <= 1)
1217193439Ssam				ieee80211_csa_completeswitch(ic);
1218193439Ssam			else
1219193439Ssam				ic->ic_csa_count = csa->csa_count;
1220193439Ssam		}
1221193439Ssam	}
1222193439Ssamdone:
1223193439Ssam	IEEE80211_UNLOCK(ic);
1224193439Ssam}
1225193439Ssam
1226193439Ssam/*
1227178354Ssam * Return non-zero if a background scan may be continued:
1228178354Ssam * o bg scan is active
1229178354Ssam * o no channel switch is pending
1230178354Ssam * o there has not been any traffic recently
1231178354Ssam *
1232178354Ssam * Note we do not check if there is an administrative enable;
1233178354Ssam * this is only done to start the scan.  We assume that any
1234178354Ssam * change in state will be accompanied by a request to cancel
1235178354Ssam * active scans which will otherwise cause this test to fail.
1236178354Ssam */
1237178354Ssamstatic __inline int
1238178354Ssamcontbgscan(struct ieee80211vap *vap)
1239178354Ssam{
1240178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1241178354Ssam
1242178354Ssam	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1243178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1244178354Ssam	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1245297405Sadrian	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1246178354Ssam}
1247178354Ssam
1248178354Ssam/*
1249178354Ssam * Return non-zero if a backgrond scan may be started:
1250178354Ssam * o bg scanning is administratively enabled
1251178354Ssam * o no channel switch is pending
1252178354Ssam * o we are not boosted on a dynamic turbo channel
1253178354Ssam * o there has not been a scan recently
1254178354Ssam * o there has not been any traffic recently
1255178354Ssam */
1256178354Ssamstatic __inline int
1257178354Ssamstartbgscan(struct ieee80211vap *vap)
1258178354Ssam{
1259178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1260178354Ssam
1261178354Ssam	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1262178354Ssam	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1263190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1264178354Ssam	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1265190391Ssam#endif
1266297405Sadrian	    ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1267297405Sadrian	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1268178354Ssam}
1269178354Ssam
1270178354Ssamstatic void
1271283535Sadriansta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1272283535Sadrian    const struct ieee80211_rx_stats *rxs,
1273283535Sadrian    int rssi, int nf)
1274178354Ssam{
1275178354Ssam#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1276178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1277178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1278283535Sadrian	struct ieee80211_channel *rxchan = ic->ic_curchan;
1279178354Ssam	struct ieee80211_frame *wh;
1280178354Ssam	uint8_t *frm, *efrm;
1281178354Ssam	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1282178354Ssam	uint8_t rate;
1283233452Sadrian	int ht_state_change = 0;
1284178354Ssam
1285178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1286178354Ssam	frm = (uint8_t *)&wh[1];
1287178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1288178354Ssam	switch (subtype) {
1289178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1290178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON: {
1291178354Ssam		struct ieee80211_scanparams scan;
1292283535Sadrian		struct ieee80211_channel *c;
1293178354Ssam		/*
1294178354Ssam		 * We process beacon/probe response frames:
1295178354Ssam		 *    o when scanning, or
1296178354Ssam		 *    o station mode when associated (to collect state
1297191444Srpaulo		 *      updates such as 802.11g slot time)
1298178354Ssam		 * Frames otherwise received are discarded.
1299178354Ssam		 */
1300178354Ssam		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1301178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1302178354Ssam			return;
1303178354Ssam		}
1304283535Sadrian
1305283535Sadrian		/* Override RX channel as appropriate */
1306283535Sadrian		if (rxs != NULL) {
1307283535Sadrian			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1308283535Sadrian			if (c != NULL)
1309283535Sadrian				rxchan = c;
1310283535Sadrian		}
1311283535Sadrian
1312178354Ssam		/* XXX probe response in sta mode when !scanning? */
1313283535Sadrian		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1314232270Sadrian			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1315232270Sadrian				vap->iv_stats.is_beacon_bad++;
1316178354Ssam			return;
1317232244Sadrian		}
1318264855Sadrian
1319178354Ssam		/*
1320178354Ssam		 * Count frame now that we know it's to be processed.
1321178354Ssam		 */
1322178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1323178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1324178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1325178354Ssam		} else
1326178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1327178354Ssam		/*
1328178354Ssam		 * When operating in station mode, check for state updates.
1329178354Ssam		 * Be careful to ignore beacons received while doing a
1330178354Ssam		 * background scan.  We consider only 11g/WMM stuff right now.
1331178354Ssam		 */
1332178354Ssam		if (ni->ni_associd != 0 &&
1333178354Ssam		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1334178354Ssam		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1335178354Ssam			/* record tsf of last beacon */
1336178354Ssam			memcpy(ni->ni_tstamp.data, scan.tstamp,
1337178354Ssam				sizeof(ni->ni_tstamp));
1338178354Ssam			/* count beacon frame for s/w bmiss handling */
1339178354Ssam			vap->iv_swbmiss_count++;
1340178354Ssam			vap->iv_bmiss_count = 0;
1341178354Ssam			if (ni->ni_erp != scan.erp) {
1342178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1343178354Ssam				    wh->i_addr2,
1344178354Ssam				    "erp change: was 0x%x, now 0x%x",
1345178354Ssam				    ni->ni_erp, scan.erp);
1346178354Ssam				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1347178354Ssam				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1348178354Ssam					ic->ic_flags |= IEEE80211_F_USEPROT;
1349178354Ssam				else
1350178354Ssam					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1351178354Ssam				ni->ni_erp = scan.erp;
1352178354Ssam				/* XXX statistic */
1353178354Ssam				/* XXX driver notification */
1354178354Ssam			}
1355178354Ssam			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1356178354Ssam				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1357178354Ssam				    wh->i_addr2,
1358178354Ssam				    "capabilities change: was 0x%x, now 0x%x",
1359178354Ssam				    ni->ni_capinfo, scan.capinfo);
1360178354Ssam				/*
1361178354Ssam				 * NB: we assume short preamble doesn't
1362178354Ssam				 *     change dynamically
1363178354Ssam				 */
1364178354Ssam				ieee80211_set_shortslottime(ic,
1365178354Ssam					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1366178354Ssam					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1367178354Ssam				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1368178354Ssam					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1369178354Ssam				/* XXX statistic */
1370178354Ssam			}
1371178354Ssam			if (scan.wme != NULL &&
1372178354Ssam			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1373178354Ssam			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1374178354Ssam				ieee80211_wme_updateparams(vap);
1375190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1376178354Ssam			if (scan.ath != NULL)
1377178354Ssam				ieee80211_parse_athparams(ni, scan.ath, wh);
1378190391Ssam#endif
1379183254Ssam			if (scan.htcap != NULL && scan.htinfo != NULL &&
1380193655Ssam			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1381178354Ssam				/* XXX state changes? */
1382233452Sadrian				if (ieee80211_ht_updateparams(ni,
1383233452Sadrian				    scan.htcap, scan.htinfo))
1384233452Sadrian					ht_state_change = 1;
1385178354Ssam			}
1386227331Sadrian			if (scan.quiet)
1387227331Sadrian				ic->ic_set_quiet(ni, scan.quiet);
1388264855Sadrian
1389178354Ssam			if (scan.tim != NULL) {
1390178354Ssam				struct ieee80211_tim_ie *tim =
1391178354Ssam				    (struct ieee80211_tim_ie *) scan.tim;
1392264855Sadrian				/*
1393264855Sadrian				 * XXX Check/debug this code; see if it's about
1394264855Sadrian				 * the right time to force the VAP awake if we
1395264855Sadrian				 * receive a frame destined for us?
1396264855Sadrian				 */
1397178354Ssam				int aid = IEEE80211_AID(ni->ni_associd);
1398178354Ssam				int ix = aid / NBBY;
1399178354Ssam				int min = tim->tim_bitctl &~ 1;
1400178354Ssam				int max = tim->tim_len + min - 4;
1401275983Sadrian				int tim_ucast = 0, tim_mcast = 0;
1402264855Sadrian
1403264855Sadrian				/*
1404264855Sadrian				 * Only do this for unicast traffic in the TIM
1405264855Sadrian				 * The multicast traffic notification for
1406264855Sadrian				 * the scan notification stuff should occur
1407264855Sadrian				 * differently.
1408264855Sadrian				 */
1409264855Sadrian				if (min <= ix && ix <= max &&
1410264855Sadrian				     isset(tim->tim_bitmap - min, aid)) {
1411275983Sadrian					tim_ucast = 1;
1412264855Sadrian				}
1413264855Sadrian
1414264855Sadrian				/*
1415275983Sadrian				 * Do a separate notification
1416264855Sadrian				 * for the multicast bit being set.
1417264855Sadrian				 */
1418264906Sadrian				if (tim->tim_bitctl & 1) {
1419275983Sadrian					tim_mcast = 1;
1420275983Sadrian				}
1421275983Sadrian
1422275983Sadrian				/*
1423275983Sadrian				 * If the TIM indicates there's traffic for
1424275983Sadrian				 * us then get us out of STA mode powersave.
1425275983Sadrian				 */
1426275983Sadrian				if (tim_ucast == 1) {
1427275983Sadrian
1428275983Sadrian					/*
1429275983Sadrian					 * Wake us out of SLEEP state if we're
1430275983Sadrian					 * in it; and if we're doing bgscan
1431275983Sadrian					 * then wake us out of STA powersave.
1432275983Sadrian					 */
1433264855Sadrian					ieee80211_sta_tim_notify(vap, 1);
1434275983Sadrian
1435275983Sadrian					/*
1436275983Sadrian					 * This is preventing us from
1437275983Sadrian					 * continuing a bgscan; because it
1438275983Sadrian					 * tricks the contbgscan()
1439275983Sadrian					 * routine to think there's always
1440275983Sadrian					 * traffic for us.
1441275983Sadrian					 *
1442275983Sadrian					 * I think we need both an RX and
1443275983Sadrian					 * TX ic_lastdata field.
1444275983Sadrian					 */
1445178354Ssam					ic->ic_lastdata = ticks;
1446178354Ssam				}
1447264906Sadrian
1448178354Ssam				ni->ni_dtim_count = tim->tim_count;
1449178354Ssam				ni->ni_dtim_period = tim->tim_period;
1450178354Ssam			}
1451193439Ssam			if (scan.csa != NULL &&
1452193439Ssam			    (vap->iv_flags & IEEE80211_F_DOTH))
1453193439Ssam				ieee80211_parse_csaparams(vap, scan.csa, wh);
1454193439Ssam			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1455193439Ssam				/*
1456193439Ssam				 * No CSA ie or 11h disabled, but a channel
1457193439Ssam				 * switch is pending; drop out so we aren't
1458193439Ssam				 * stuck in CSA state.  If the AP really is
1459193439Ssam				 * moving we'll get a beacon miss and scan.
1460193439Ssam				 */
1461193439Ssam				IEEE80211_LOCK(ic);
1462193439Ssam				ieee80211_csa_cancelswitch(ic);
1463193439Ssam				IEEE80211_UNLOCK(ic);
1464193439Ssam			}
1465178354Ssam			/*
1466178354Ssam			 * If scanning, pass the info to the scan module.
1467178354Ssam			 * Otherwise, check if it's the right time to do
1468178354Ssam			 * a background scan.  Background scanning must
1469178354Ssam			 * be enabled and we must not be operating in the
1470178354Ssam			 * turbo phase of dynamic turbo mode.  Then,
1471178354Ssam			 * it's been a while since the last background
1472178354Ssam			 * scan and if no data frames have come through
1473178354Ssam			 * recently, kick off a scan.  Note that this
1474178354Ssam			 * is the mechanism by which a background scan
1475178354Ssam			 * is started _and_ continued each time we
1476178354Ssam			 * return on-channel to receive a beacon from
1477178354Ssam			 * our ap.
1478178354Ssam			 */
1479178354Ssam			if (ic->ic_flags & IEEE80211_F_SCAN) {
1480283535Sadrian				ieee80211_add_scan(vap, rxchan,
1481282742Sadrian				    &scan, wh, subtype, rssi, nf);
1482178354Ssam			} else if (contbgscan(vap)) {
1483178354Ssam				ieee80211_bg_scan(vap, 0);
1484178354Ssam			} else if (startbgscan(vap)) {
1485178354Ssam				vap->iv_stats.is_scan_bg++;
1486178354Ssam#if 0
1487178354Ssam				/* wakeup if we are sleeing */
1488178354Ssam				ieee80211_set_pwrsave(vap, 0);
1489178354Ssam#endif
1490178354Ssam				ieee80211_bg_scan(vap, 0);
1491178354Ssam			}
1492233452Sadrian
1493233452Sadrian			/*
1494264855Sadrian			 * Put the station to sleep if we haven't seen
1495264855Sadrian			 * traffic in a while.
1496264855Sadrian			 */
1497264855Sadrian			IEEE80211_LOCK(ic);
1498264855Sadrian			ieee80211_sta_ps_timer_check(vap);
1499264855Sadrian			IEEE80211_UNLOCK(ic);
1500264855Sadrian
1501264855Sadrian			/*
1502233452Sadrian			 * If we've had a channel width change (eg HT20<->HT40)
1503233452Sadrian			 * then schedule a delayed driver notification.
1504233452Sadrian			 */
1505233452Sadrian			if (ht_state_change)
1506233452Sadrian				ieee80211_update_chw(ic);
1507178354Ssam			return;
1508178354Ssam		}
1509178354Ssam		/*
1510178354Ssam		 * If scanning, just pass information to the scan module.
1511178354Ssam		 */
1512178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1513178354Ssam			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1514178354Ssam				/*
1515178354Ssam				 * Actively scanning a channel marked passive;
1516178354Ssam				 * send a probe request now that we know there
1517178354Ssam				 * is 802.11 traffic present.
1518178354Ssam				 *
1519178354Ssam				 * XXX check if the beacon we recv'd gives
1520178354Ssam				 * us what we need and suppress the probe req
1521178354Ssam				 */
1522178354Ssam				ieee80211_probe_curchan(vap, 1);
1523178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1524178354Ssam			}
1525283535Sadrian			ieee80211_add_scan(vap, rxchan, &scan, wh,
1526282742Sadrian			    subtype, rssi, nf);
1527178354Ssam			return;
1528178354Ssam		}
1529178354Ssam		break;
1530178354Ssam	}
1531178354Ssam
1532178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH: {
1533178354Ssam		uint16_t algo, seq, status;
1534178354Ssam		/*
1535178354Ssam		 * auth frame format
1536178354Ssam		 *	[2] algorithm
1537178354Ssam		 *	[2] sequence
1538178354Ssam		 *	[2] status
1539178354Ssam		 *	[tlv*] challenge
1540178354Ssam		 */
1541178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1542178354Ssam		algo   = le16toh(*(uint16_t *)frm);
1543178354Ssam		seq    = le16toh(*(uint16_t *)(frm + 2));
1544178354Ssam		status = le16toh(*(uint16_t *)(frm + 4));
1545178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1546178354Ssam		    "recv auth frame with algorithm %d seq %d", algo, seq);
1547178354Ssam
1548178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1549178354Ssam			IEEE80211_DISCARD(vap,
1550178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1551178354Ssam			    wh, "auth", "%s", "TKIP countermeasures enabled");
1552178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1553178354Ssam			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1554178354Ssam				ieee80211_send_error(ni, wh->i_addr2,
1555178354Ssam					IEEE80211_FC0_SUBTYPE_AUTH,
1556178354Ssam					IEEE80211_REASON_MIC_FAILURE);
1557178354Ssam			}
1558178354Ssam			return;
1559178354Ssam		}
1560178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1561192468Ssam			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1562192468Ssam			    seq, status);
1563178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1564192468Ssam			sta_auth_open(ni, wh, rssi, nf, seq, status);
1565178354Ssam		else {
1566178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1567178354Ssam			    wh, "auth", "unsupported alg %d", algo);
1568178354Ssam			vap->iv_stats.is_rx_auth_unsupported++;
1569178354Ssam			return;
1570178354Ssam		}
1571178354Ssam		break;
1572178354Ssam	}
1573178354Ssam
1574178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1575178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1576178354Ssam		uint16_t capinfo, associd;
1577178354Ssam		uint16_t status;
1578178354Ssam
1579178354Ssam		if (vap->iv_state != IEEE80211_S_ASSOC) {
1580178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1581178354Ssam			return;
1582178354Ssam		}
1583178354Ssam
1584178354Ssam		/*
1585178354Ssam		 * asresp frame format
1586178354Ssam		 *	[2] capability information
1587178354Ssam		 *	[2] status
1588178354Ssam		 *	[2] association ID
1589178354Ssam		 *	[tlv] supported rates
1590178354Ssam		 *	[tlv] extended supported rates
1591178354Ssam		 *	[tlv] WME
1592178354Ssam		 *	[tlv] HT capabilities
1593178354Ssam		 *	[tlv] HT info
1594178354Ssam		 */
1595178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1596178354Ssam		ni = vap->iv_bss;
1597178354Ssam		capinfo = le16toh(*(uint16_t *)frm);
1598178354Ssam		frm += 2;
1599178354Ssam		status = le16toh(*(uint16_t *)frm);
1600178354Ssam		frm += 2;
1601178354Ssam		if (status != 0) {
1602178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1603178354Ssam			    wh->i_addr2, "%sassoc failed (reason %d)",
1604178354Ssam			    ISREASSOC(subtype) ?  "re" : "", status);
1605178354Ssam			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1606178354Ssam			return;
1607178354Ssam		}
1608178354Ssam		associd = le16toh(*(uint16_t *)frm);
1609178354Ssam		frm += 2;
1610178354Ssam
1611178354Ssam		rates = xrates = wme = htcap = htinfo = NULL;
1612178354Ssam		while (efrm - frm > 1) {
1613178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1614178354Ssam			switch (*frm) {
1615178354Ssam			case IEEE80211_ELEMID_RATES:
1616178354Ssam				rates = frm;
1617178354Ssam				break;
1618178354Ssam			case IEEE80211_ELEMID_XRATES:
1619178354Ssam				xrates = frm;
1620178354Ssam				break;
1621178354Ssam			case IEEE80211_ELEMID_HTCAP:
1622178354Ssam				htcap = frm;
1623178354Ssam				break;
1624178354Ssam			case IEEE80211_ELEMID_HTINFO:
1625178354Ssam				htinfo = frm;
1626178354Ssam				break;
1627178354Ssam			case IEEE80211_ELEMID_VENDOR:
1628178354Ssam				if (iswmeoui(frm))
1629178354Ssam					wme = frm;
1630193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1631178354Ssam					/*
1632178354Ssam					 * Accept pre-draft HT ie's if the
1633178354Ssam					 * standard ones have not been seen.
1634178354Ssam					 */
1635178354Ssam					if (ishtcapoui(frm)) {
1636178354Ssam						if (htcap == NULL)
1637178354Ssam							htcap = frm;
1638178354Ssam					} else if (ishtinfooui(frm)) {
1639178354Ssam						if (htinfo == NULL)
1640219603Sbschmidt							htinfo = frm;
1641178354Ssam					}
1642178354Ssam				}
1643178354Ssam				/* XXX Atheros OUI support */
1644178354Ssam				break;
1645178354Ssam			}
1646178354Ssam			frm += frm[1] + 2;
1647178354Ssam		}
1648178354Ssam
1649178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1650178354Ssam		if (xrates != NULL)
1651178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1652178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1653178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
1654178354Ssam				IEEE80211_F_JOIN |
1655178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1656178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1657178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
1658178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1659178354Ssam			    wh->i_addr2,
1660178354Ssam			    "%sassoc failed (rate set mismatch)",
1661178354Ssam			    ISREASSOC(subtype) ?  "re" : "");
1662178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
1663178354Ssam			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1664178354Ssam			    IEEE80211_SCAN_FAIL_STATUS);
1665178354Ssam			return;
1666178354Ssam		}
1667178354Ssam
1668178354Ssam		ni->ni_capinfo = capinfo;
1669178354Ssam		ni->ni_associd = associd;
1670178354Ssam		if (ni->ni_jointime == 0)
1671178354Ssam			ni->ni_jointime = time_uptime;
1672178354Ssam		if (wme != NULL &&
1673178354Ssam		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1674178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1675178354Ssam			ieee80211_wme_updateparams(vap);
1676178354Ssam		} else
1677178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1678178354Ssam		/*
1679178354Ssam		 * Setup HT state according to the negotiation.
1680178354Ssam		 *
1681178354Ssam		 * NB: shouldn't need to check if HT use is enabled but some
1682178354Ssam		 *     ap's send back HT ie's even when we don't indicate we
1683178354Ssam		 *     are HT capable in our AssocReq.
1684178354Ssam		 */
1685178354Ssam		if (htcap != NULL && htinfo != NULL &&
1686193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1687183254Ssam			ieee80211_ht_node_init(ni);
1688183254Ssam			ieee80211_ht_updateparams(ni, htcap, htinfo);
1689178354Ssam			ieee80211_setup_htrates(ni, htcap,
1690178354Ssam			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1691178354Ssam			ieee80211_setup_basic_htrates(ni, htinfo);
1692193966Ssam			ieee80211_node_setuptxparms(ni);
1693214894Sbschmidt			ieee80211_ratectl_node_init(ni);
1694178354Ssam		}
1695297603Sadrian
1696178354Ssam		/*
1697297768Sadrian		 * Always initialise FF/superg state; we can use this
1698297768Sadrian		 * for doing A-MSDU encapsulation as well.
1699297768Sadrian		 */
1700297768Sadrian#ifdef	IEEE80211_SUPPORT_SUPERG
1701297768Sadrian		ieee80211_ff_node_init(ni);
1702297768Sadrian#endif
1703297768Sadrian
1704297768Sadrian		/*
1705178354Ssam		 * Configure state now that we are associated.
1706178354Ssam		 *
1707178354Ssam		 * XXX may need different/additional driver callbacks?
1708178354Ssam		 */
1709178354Ssam		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1710178354Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1711178354Ssam			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1712178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1713178354Ssam		} else {
1714178354Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1715178354Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1716178354Ssam		}
1717178354Ssam		ieee80211_set_shortslottime(ic,
1718178354Ssam			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1719178354Ssam			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1720178354Ssam		/*
1721178354Ssam		 * Honor ERP protection.
1722178354Ssam		 *
1723178354Ssam		 * NB: ni_erp should zero for non-11g operation.
1724178354Ssam		 */
1725178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1726178354Ssam		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1727178354Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1728178354Ssam		else
1729178354Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1730178354Ssam		IEEE80211_NOTE_MAC(vap,
1731178354Ssam		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1732183256Ssam		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1733178354Ssam		    ISREASSOC(subtype) ? "re" : "",
1734178354Ssam		    IEEE80211_NODE_AID(ni),
1735178354Ssam		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1736178354Ssam		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1737178354Ssam		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1738178354Ssam		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1739178354Ssam		    ni->ni_flags & IEEE80211_NODE_HT ?
1740182834Ssam			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1741178354Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1742183255Ssam		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1743183255Ssam			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1744183256Ssam		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1745178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1746178354Ssam			", fast-frames" : "",
1747178354Ssam		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1748178354Ssam			", turbo" : ""
1749178354Ssam		);
1750178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1751178354Ssam		break;
1752178354Ssam	}
1753178354Ssam
1754178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1755178354Ssam		uint16_t reason;
1756178354Ssam
1757178354Ssam		if (vap->iv_state == IEEE80211_S_SCAN) {
1758178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1759178354Ssam			return;
1760178354Ssam		}
1761178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1762178354Ssam			/* NB: can happen when in promiscuous mode */
1763178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1764178354Ssam			break;
1765178354Ssam		}
1766178354Ssam
1767178354Ssam		/*
1768178354Ssam		 * deauth frame format
1769178354Ssam		 *	[2] reason
1770178354Ssam		 */
1771178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1772178354Ssam		reason = le16toh(*(uint16_t *)frm);
1773178354Ssam
1774178354Ssam		vap->iv_stats.is_rx_deauth++;
1775178354Ssam		vap->iv_stats.is_rx_deauth_code = reason;
1776178354Ssam		IEEE80211_NODE_STAT(ni, rx_deauth);
1777178354Ssam
1778178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1779298364Savos		    "recv deauthenticate (reason: %d (%s))", reason,
1780298364Savos		    ieee80211_reason_to_string(reason));
1781178354Ssam		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1782178354Ssam		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1783178354Ssam		break;
1784178354Ssam	}
1785178354Ssam
1786178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1787178354Ssam		uint16_t reason;
1788178354Ssam
1789178354Ssam		if (vap->iv_state != IEEE80211_S_RUN &&
1790178354Ssam		    vap->iv_state != IEEE80211_S_ASSOC &&
1791178354Ssam		    vap->iv_state != IEEE80211_S_AUTH) {
1792178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1793178354Ssam			return;
1794178354Ssam		}
1795178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1796178354Ssam			/* NB: can happen when in promiscuous mode */
1797178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1798178354Ssam			break;
1799178354Ssam		}
1800178354Ssam
1801178354Ssam		/*
1802178354Ssam		 * disassoc frame format
1803178354Ssam		 *	[2] reason
1804178354Ssam		 */
1805178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1806178354Ssam		reason = le16toh(*(uint16_t *)frm);
1807178354Ssam
1808178354Ssam		vap->iv_stats.is_rx_disassoc++;
1809178354Ssam		vap->iv_stats.is_rx_disassoc_code = reason;
1810178354Ssam		IEEE80211_NODE_STAT(ni, rx_disassoc);
1811178354Ssam
1812178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1813298364Savos		    "recv disassociate (reason: %d (%s))", reason,
1814298364Savos		    ieee80211_reason_to_string(reason));
1815178354Ssam		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1816178354Ssam		break;
1817178354Ssam	}
1818178354Ssam
1819178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
1820218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1821218958Sbschmidt		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1822218958Sbschmidt		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1823218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1824218958Sbschmidt			    wh, NULL, "%s", "not for us");
1825218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
1826218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
1827218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1828218927Sbschmidt			    wh, NULL, "wrong state %s",
1829218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
1830178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1831218958Sbschmidt		} else {
1832218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
1833218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1834218927Sbschmidt		}
1835178354Ssam		break;
1836178354Ssam
1837178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1838178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1839218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1840295795Savos	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1841218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
1842218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1843218927Sbschmidt		    wh, NULL, "%s", "not handled");
1844178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
1845218927Sbschmidt		break;
1846218927Sbschmidt
1847178354Ssam	default:
1848178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1849218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
1850178354Ssam		vap->iv_stats.is_rx_badsubtype++;
1851178354Ssam		break;
1852178354Ssam	}
1853178354Ssam#undef ISREASSOC
1854178354Ssam}
1855191546Ssam
1856191546Ssamstatic void
1857205277Srpaulosta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1858191546Ssam{
1859205277Srpaulo	switch (subtype) {
1860205277Srpaulo	case IEEE80211_FC0_SUBTYPE_BAR:
1861205277Srpaulo		ieee80211_recv_bar(ni, m);
1862205277Srpaulo		break;
1863205277Srpaulo	}
1864191546Ssam}
1865