1/*-
2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27#ifdef __FreeBSD__
28__FBSDID("$FreeBSD: stable/11/sys/net80211/ieee80211_sta.c 344969 2019-03-09 12:54:10Z avos $");
29#endif
30
31/*
32 * IEEE 802.11 Station mode support.
33 */
34#include "opt_inet.h"
35#include "opt_wlan.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/mbuf.h>
40#include <sys/malloc.h>
41#include <sys/kernel.h>
42
43#include <sys/socket.h>
44#include <sys/sockio.h>
45#include <sys/endian.h>
46#include <sys/errno.h>
47#include <sys/proc.h>
48#include <sys/sysctl.h>
49
50#include <net/if.h>
51#include <net/if_media.h>
52#include <net/if_llc.h>
53#include <net/if_dl.h>
54#include <net/if_var.h>
55#include <net/ethernet.h>
56
57#include <net/bpf.h>
58
59#include <net80211/ieee80211_var.h>
60#include <net80211/ieee80211_sta.h>
61#include <net80211/ieee80211_input.h>
62#ifdef IEEE80211_SUPPORT_SUPERG
63#include <net80211/ieee80211_superg.h>
64#endif
65#include <net80211/ieee80211_ratectl.h>
66#include <net80211/ieee80211_sta.h>
67
68#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
69
70static	void sta_vattach(struct ieee80211vap *);
71static	void sta_beacon_miss(struct ieee80211vap *);
72static	int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
73static	int sta_input(struct ieee80211_node *, struct mbuf *,
74	    const struct ieee80211_rx_stats *, int, int);
75static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76	    int subtype, const struct ieee80211_rx_stats *, int rssi, int nf);
77static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
78
79void
80ieee80211_sta_attach(struct ieee80211com *ic)
81{
82	ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
83}
84
85void
86ieee80211_sta_detach(struct ieee80211com *ic)
87{
88}
89
90static void
91sta_vdetach(struct ieee80211vap *vap)
92{
93}
94
95static void
96sta_vattach(struct ieee80211vap *vap)
97{
98	vap->iv_newstate = sta_newstate;
99	vap->iv_input = sta_input;
100	vap->iv_recv_mgmt = sta_recv_mgmt;
101	vap->iv_recv_ctl = sta_recv_ctl;
102	vap->iv_opdetach = sta_vdetach;
103	vap->iv_bmiss = sta_beacon_miss;
104}
105
106/*
107 * Handle a beacon miss event.  The common code filters out
108 * spurious events that can happen when scanning and/or before
109 * reaching RUN state.
110 */
111static void
112sta_beacon_miss(struct ieee80211vap *vap)
113{
114	struct ieee80211com *ic = vap->iv_ic;
115
116	IEEE80211_LOCK_ASSERT(ic);
117
118	KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
119	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
120	    ("wrong state %s", ieee80211_state_name[vap->iv_state]));
121
122	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
123	    "beacon miss, mode %s state %s\n",
124	    ieee80211_opmode_name[vap->iv_opmode],
125	    ieee80211_state_name[vap->iv_state]);
126
127	if (vap->iv_state == IEEE80211_S_CSA) {
128		/*
129		 * A Channel Switch is pending; assume we missed the
130		 * beacon that would've completed the process and just
131		 * force the switch.  If we made a mistake we'll not
132		 * find the AP on the new channel and fall back to a
133		 * normal scan.
134		 */
135		ieee80211_csa_completeswitch(ic);
136		return;
137	}
138	if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
139		/*
140		 * Send a directed probe req before falling back to a
141		 * scan; if we receive a response ic_bmiss_count will
142		 * be reset.  Some cards mistakenly report beacon miss
143		 * so this avoids the expensive scan if the ap is
144		 * still there.
145		 */
146		ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
147			vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
148			vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
149		return;
150	}
151
152	callout_stop(&vap->iv_swbmiss);
153	vap->iv_bmiss_count = 0;
154	vap->iv_stats.is_beacon_miss++;
155	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
156#ifdef IEEE80211_SUPPORT_SUPERG
157
158		/*
159		 * If we receive a beacon miss interrupt when using
160		 * dynamic turbo, attempt to switch modes before
161		 * reassociating.
162		 */
163		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
164			ieee80211_dturbo_switch(vap,
165			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
166#endif
167		/*
168		 * Try to reassociate before scanning for a new ap.
169		 */
170		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
171	} else {
172		/*
173		 * Somebody else is controlling state changes (e.g.
174		 * a user-mode app) don't do anything that would
175		 * confuse them; just drop into scan mode so they'll
176		 * notified of the state change and given control.
177		 */
178		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
179	}
180}
181
182/*
183 * Handle deauth with reason.  We retry only for
184 * the cases where we might succeed.  Otherwise
185 * we downgrade the ap and scan.
186 */
187static void
188sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
189{
190	switch (reason) {
191	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
192	case IEEE80211_STATUS_TIMEOUT:
193	case IEEE80211_REASON_ASSOC_EXPIRE:
194	case IEEE80211_REASON_NOT_AUTHED:
195	case IEEE80211_REASON_NOT_ASSOCED:
196	case IEEE80211_REASON_ASSOC_LEAVE:
197	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
198		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
199		break;
200	default:
201		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
202		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
203			ieee80211_check_scan_current(vap);
204		break;
205	}
206}
207
208static void
209sta_swbmiss_start(struct ieee80211vap *vap)
210{
211
212	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) {
213		/*
214		 * Start s/w beacon miss timer for devices w/o
215		 * hardware support.  We fudge a bit here since
216		 * we're doing this in software.
217		 */
218		vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
219		    2 * vap->iv_bmissthreshold * vap->iv_bss->ni_intval);
220		vap->iv_swbmiss_count = 0;
221		callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
222		    ieee80211_swbmiss, vap);
223	}
224}
225
226/*
227 * IEEE80211_M_STA vap state machine handler.
228 * This routine handles the main states in the 802.11 protocol.
229 */
230static int
231sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
232{
233	struct ieee80211com *ic = vap->iv_ic;
234	struct ieee80211_node *ni;
235	enum ieee80211_state ostate;
236
237	IEEE80211_LOCK_ASSERT(ic);
238
239	ostate = vap->iv_state;
240	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
241	    __func__, ieee80211_state_name[ostate],
242	    ieee80211_state_name[nstate], arg);
243	vap->iv_state = nstate;			/* state transition */
244	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
245	if (ostate != IEEE80211_S_SCAN)
246		ieee80211_cancel_scan(vap);	/* background scan */
247	ni = vap->iv_bss;			/* NB: no reference held */
248	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
249		callout_stop(&vap->iv_swbmiss);
250	switch (nstate) {
251	case IEEE80211_S_INIT:
252		switch (ostate) {
253		case IEEE80211_S_SLEEP:
254			/* XXX wakeup */
255			/* XXX driver hook to wakeup the hardware? */
256		case IEEE80211_S_RUN:
257			IEEE80211_SEND_MGMT(ni,
258			    IEEE80211_FC0_SUBTYPE_DISASSOC,
259			    IEEE80211_REASON_ASSOC_LEAVE);
260			ieee80211_sta_leave(ni);
261			break;
262		case IEEE80211_S_ASSOC:
263			IEEE80211_SEND_MGMT(ni,
264			    IEEE80211_FC0_SUBTYPE_DEAUTH,
265			    IEEE80211_REASON_AUTH_LEAVE);
266			break;
267		case IEEE80211_S_SCAN:
268			ieee80211_cancel_scan(vap);
269			break;
270		default:
271			break;
272		}
273		if (ostate != IEEE80211_S_INIT) {
274			/* NB: optimize INIT -> INIT case */
275			ieee80211_reset_bss(vap);
276		}
277		if (vap->iv_auth->ia_detach != NULL)
278			vap->iv_auth->ia_detach(vap);
279		break;
280	case IEEE80211_S_SCAN:
281		switch (ostate) {
282		case IEEE80211_S_INIT:
283			/*
284			 * Initiate a scan.  We can come here as a result
285			 * of an IEEE80211_IOC_SCAN_REQ too in which case
286			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
287			 * and the scan request parameters will be present
288			 * in iv_scanreq.  Otherwise we do the default.
289			 */
290			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
291				ieee80211_check_scan(vap,
292				    vap->iv_scanreq_flags,
293				    vap->iv_scanreq_duration,
294				    vap->iv_scanreq_mindwell,
295				    vap->iv_scanreq_maxdwell,
296				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
297				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
298			} else
299				ieee80211_check_scan_current(vap);
300			break;
301		case IEEE80211_S_SCAN:
302		case IEEE80211_S_AUTH:
303		case IEEE80211_S_ASSOC:
304			/*
305			 * These can happen either because of a timeout
306			 * on an assoc/auth response or because of a
307			 * change in state that requires a reset.  For
308			 * the former we're called with a non-zero arg
309			 * that is the cause for the failure; pass this
310			 * to the scan code so it can update state.
311			 * Otherwise trigger a new scan unless we're in
312			 * manual roaming mode in which case an application
313			 * must issue an explicit scan request.
314			 */
315			if (arg != 0)
316				ieee80211_scan_assoc_fail(vap,
317					vap->iv_bss->ni_macaddr, arg);
318			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
319				ieee80211_check_scan_current(vap);
320			break;
321		case IEEE80211_S_SLEEP:		/* beacon miss */
322			/*
323			 * XXX if in sleep we need to wakeup the hardware.
324			 */
325			/* FALLTHROUGH */
326		case IEEE80211_S_RUN:		/* beacon miss */
327			/*
328			 * Beacon miss.  Notify user space and if not
329			 * under control of a user application (roaming
330			 * manual) kick off a scan to re-connect.
331			 */
332
333			ieee80211_sta_leave(ni);
334			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
335				ieee80211_check_scan_current(vap);
336			break;
337		default:
338			goto invalid;
339		}
340		break;
341	case IEEE80211_S_AUTH:
342		switch (ostate) {
343		case IEEE80211_S_INIT:
344		case IEEE80211_S_SCAN:
345			IEEE80211_SEND_MGMT(ni,
346			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
347			break;
348		case IEEE80211_S_AUTH:
349		case IEEE80211_S_ASSOC:
350			switch (arg & 0xff) {
351			case IEEE80211_FC0_SUBTYPE_AUTH:
352				/* ??? */
353				IEEE80211_SEND_MGMT(ni,
354				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
355				break;
356			case IEEE80211_FC0_SUBTYPE_DEAUTH:
357				sta_authretry(vap, ni, arg>>8);
358				break;
359			}
360			break;
361		case IEEE80211_S_SLEEP:
362		case IEEE80211_S_RUN:
363			switch (arg & 0xff) {
364			case IEEE80211_FC0_SUBTYPE_AUTH:
365				IEEE80211_SEND_MGMT(ni,
366				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
367				vap->iv_state = IEEE80211_S_RUN; /* stay RUN */
368				break;
369			case IEEE80211_FC0_SUBTYPE_DEAUTH:
370				ieee80211_sta_leave(ni);
371				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
372					/* try to reauth */
373					IEEE80211_SEND_MGMT(ni,
374					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
375				}
376				break;
377			}
378			break;
379		default:
380			goto invalid;
381		}
382		break;
383	case IEEE80211_S_ASSOC:
384		switch (ostate) {
385		case IEEE80211_S_AUTH:
386		case IEEE80211_S_ASSOC:
387			IEEE80211_SEND_MGMT(ni,
388			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
389			break;
390		case IEEE80211_S_SLEEP:		/* cannot happen */
391		case IEEE80211_S_RUN:
392			ieee80211_sta_leave(ni);
393			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
394				IEEE80211_SEND_MGMT(ni, arg ?
395				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
396				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
397			}
398			break;
399		default:
400			goto invalid;
401		}
402		break;
403	case IEEE80211_S_RUN:
404		if (vap->iv_flags & IEEE80211_F_WPA) {
405			/* XXX validate prerequisites */
406		}
407		switch (ostate) {
408		case IEEE80211_S_RUN:
409		case IEEE80211_S_CSA:
410			break;
411		case IEEE80211_S_AUTH:		/* when join is done in fw */
412		case IEEE80211_S_ASSOC:
413#ifdef IEEE80211_DEBUG
414			if (ieee80211_msg_debug(vap)) {
415				ieee80211_note(vap, "%s with %s ssid ",
416				    (vap->iv_opmode == IEEE80211_M_STA ?
417				    "associated" : "synchronized"),
418				    ether_sprintf(ni->ni_bssid));
419				ieee80211_print_essid(vap->iv_bss->ni_essid,
420				    ni->ni_esslen);
421				/* XXX MCS/HT */
422				printf(" channel %d start %uMb\n",
423				    ieee80211_chan2ieee(ic, ic->ic_curchan),
424				    IEEE80211_RATE2MBS(ni->ni_txrate));
425			}
426#endif
427			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
428			ieee80211_notify_node_join(ni,
429			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
430			break;
431		case IEEE80211_S_SLEEP:
432			/* Wake up from sleep */
433			vap->iv_sta_ps(vap, 0);
434			break;
435		default:
436			goto invalid;
437		}
438		ieee80211_sync_curchan(ic);
439		if (ostate != IEEE80211_S_RUN)
440			sta_swbmiss_start(vap);
441		/*
442		 * When 802.1x is not in use mark the port authorized
443		 * at this point so traffic can flow.
444		 */
445		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
446			ieee80211_node_authorize(ni);
447		/*
448		 * Fake association when joining an existing bss.
449		 *
450		 * Don't do this if we're doing SLEEP->RUN.
451		 */
452		if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
453			ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
454		break;
455	case IEEE80211_S_CSA:
456		if (ostate != IEEE80211_S_RUN)
457			goto invalid;
458		break;
459	case IEEE80211_S_SLEEP:
460		sta_swbmiss_start(vap);
461		vap->iv_sta_ps(vap, 1);
462		break;
463	default:
464	invalid:
465		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
466		    "%s: unexpected state transition %s -> %s\n", __func__,
467		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
468		break;
469	}
470	return 0;
471}
472
473/*
474 * Return non-zero if the frame is an echo of a multicast
475 * frame sent by ourself.  The dir is known to be DSTODS.
476 */
477static __inline int
478isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
479{
480#define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
481#define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
482	const uint8_t *sa;
483
484	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
485
486	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
487		return 0;
488	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
489	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
490#undef WH4
491#undef QWH4
492}
493
494/*
495 * Return non-zero if the frame is an echo of a multicast
496 * frame sent by ourself.  The dir is known to be FROMDS.
497 */
498static __inline int
499isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
500{
501	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
502
503	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
504		return 0;
505	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
506}
507
508/*
509 * Decide if a received management frame should be
510 * printed when debugging is enabled.  This filters some
511 * of the less interesting frames that come frequently
512 * (e.g. beacons).
513 */
514static __inline int
515doprint(struct ieee80211vap *vap, int subtype)
516{
517	switch (subtype) {
518	case IEEE80211_FC0_SUBTYPE_BEACON:
519		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
520	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
521		return 0;
522	}
523	return 1;
524}
525
526/*
527 * Process a received frame.  The node associated with the sender
528 * should be supplied.  If nothing was found in the node table then
529 * the caller is assumed to supply a reference to iv_bss instead.
530 * The RSSI and a timestamp are also supplied.  The RSSI data is used
531 * during AP scanning to select a AP to associate with; it can have
532 * any units so long as values have consistent units and higher values
533 * mean ``better signal''.  The receive timestamp is currently not used
534 * by the 802.11 layer.
535 */
536static int
537sta_input(struct ieee80211_node *ni, struct mbuf *m,
538    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
539{
540	struct ieee80211vap *vap = ni->ni_vap;
541	struct ieee80211com *ic = ni->ni_ic;
542	struct ifnet *ifp = vap->iv_ifp;
543	struct ieee80211_frame *wh;
544	struct ieee80211_key *key;
545	struct ether_header *eh;
546	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
547	uint8_t dir, type, subtype, qos;
548	uint8_t *bssid;
549
550	if (m->m_flags & M_AMPDU_MPDU) {
551		/*
552		 * Fastpath for A-MPDU reorder q resubmission.  Frames
553		 * w/ M_AMPDU_MPDU marked have already passed through
554		 * here but were received out of order and been held on
555		 * the reorder queue.  When resubmitted they are marked
556		 * with the M_AMPDU_MPDU flag and we can bypass most of
557		 * the normal processing.
558		 */
559		wh = mtod(m, struct ieee80211_frame *);
560		type = IEEE80211_FC0_TYPE_DATA;
561		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
562		subtype = IEEE80211_FC0_SUBTYPE_QOS;
563		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
564		goto resubmit_ampdu;
565	}
566
567	KASSERT(ni != NULL, ("null node"));
568	ni->ni_inact = ni->ni_inact_reload;
569
570	type = -1;			/* undefined */
571
572	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
573		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
574		    ni->ni_macaddr, NULL,
575		    "too short (1): len %u", m->m_pkthdr.len);
576		vap->iv_stats.is_rx_tooshort++;
577		goto out;
578	}
579	/*
580	 * Bit of a cheat here, we use a pointer for a 3-address
581	 * frame format but don't reference fields past outside
582	 * ieee80211_frame_min w/o first validating the data is
583	 * present.
584	 */
585	wh = mtod(m, struct ieee80211_frame *);
586
587	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
588	    IEEE80211_FC0_VERSION_0) {
589		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
590		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
591		    wh->i_fc[0], wh->i_fc[1]);
592		vap->iv_stats.is_rx_badversion++;
593		goto err;
594	}
595
596	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
597	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
598	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
599	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
600		bssid = wh->i_addr2;
601		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
602			/* not interested in */
603			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
604			    bssid, NULL, "%s", "not to bss");
605			vap->iv_stats.is_rx_wrongbss++;
606			goto out;
607		}
608
609		/*
610		 * Some devices may be in a promiscuous mode
611		 * where they receive frames for multiple station
612		 * addresses.
613		 *
614		 * If we receive a data frame that isn't
615		 * destined to our VAP MAC, drop it.
616		 *
617		 * XXX TODO: This is only enforced when not scanning;
618		 * XXX it assumes a software-driven scan will put the NIC
619		 * XXX into a "no data frames" mode before setting this
620		 * XXX flag. Otherwise it may be possible that we'll still
621		 * XXX process data frames whilst scanning.
622		 */
623		if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
624		    && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
625			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
626			    bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
627			    IF_LLADDR(ifp), ":", wh->i_addr1, ":");
628			vap->iv_stats.is_rx_wrongbss++;
629			goto out;
630		}
631
632		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
633		ni->ni_noise = nf;
634		if ( IEEE80211_HAS_SEQ(type, subtype) &&
635		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
636			uint8_t tid = ieee80211_gettid(wh);
637			if (IEEE80211_QOS_HAS_SEQ(wh) &&
638			    TID_TO_WME_AC(tid) >= WME_AC_VI)
639				ic->ic_wme.wme_hipri_traffic++;
640			if (! ieee80211_check_rxseq(ni, wh, bssid))
641				goto out;
642		}
643	}
644
645	switch (type) {
646	case IEEE80211_FC0_TYPE_DATA:
647		hdrspace = ieee80211_hdrspace(ic, wh);
648		if (m->m_len < hdrspace &&
649		    (m = m_pullup(m, hdrspace)) == NULL) {
650			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
651			    ni->ni_macaddr, NULL,
652			    "data too short: expecting %u", hdrspace);
653			vap->iv_stats.is_rx_tooshort++;
654			goto out;		/* XXX */
655		}
656		/*
657		 * Handle A-MPDU re-ordering.  If the frame is to be
658		 * processed directly then ieee80211_ampdu_reorder
659		 * will return 0; otherwise it has consumed the mbuf
660		 * and we should do nothing more with it.
661		 */
662		if ((m->m_flags & M_AMPDU) &&
663		    (dir == IEEE80211_FC1_DIR_FROMDS ||
664		     dir == IEEE80211_FC1_DIR_DSTODS) &&
665		    ieee80211_ampdu_reorder(ni, m) != 0) {
666			m = NULL;
667			goto out;
668		}
669	resubmit_ampdu:
670		if (dir == IEEE80211_FC1_DIR_FROMDS) {
671			if ((ifp->if_flags & IFF_SIMPLEX) &&
672			    isfromds_mcastecho(vap, wh)) {
673				/*
674				 * In IEEE802.11 network, multicast
675				 * packets sent from "me" are broadcast
676				 * from the AP; silently discard for
677				 * SIMPLEX interface.
678				 */
679				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
680				    wh, "data", "%s", "multicast echo");
681				vap->iv_stats.is_rx_mcastecho++;
682				goto out;
683			}
684			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
685			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
686				/*
687				 * DWDS sta's must drop 3-address mcast frames
688				 * as they will be sent separately as a 4-addr
689				 * frame.  Accepting the 3-addr frame will
690				 * confuse the bridge into thinking the sending
691				 * sta is located at the end of WDS link.
692				 */
693				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
694				    "3-address data", "%s", "DWDS enabled");
695				vap->iv_stats.is_rx_mcastecho++;
696				goto out;
697			}
698		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
699			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
700				IEEE80211_DISCARD(vap,
701				    IEEE80211_MSG_INPUT, wh, "4-address data",
702				    "%s", "DWDS not enabled");
703				vap->iv_stats.is_rx_wrongdir++;
704				goto out;
705			}
706			if ((ifp->if_flags & IFF_SIMPLEX) &&
707			    isdstods_mcastecho(vap, wh)) {
708				/*
709				 * In IEEE802.11 network, multicast
710				 * packets sent from "me" are broadcast
711				 * from the AP; silently discard for
712				 * SIMPLEX interface.
713				 */
714				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
715				    "4-address data", "%s", "multicast echo");
716				vap->iv_stats.is_rx_mcastecho++;
717				goto out;
718			}
719		} else {
720			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
721			    "data", "incorrect dir 0x%x", dir);
722			vap->iv_stats.is_rx_wrongdir++;
723			goto out;
724		}
725
726		/*
727		 * Handle privacy requirements.  Note that we
728		 * must not be preempted from here until after
729		 * we (potentially) call ieee80211_crypto_demic;
730		 * otherwise we may violate assumptions in the
731		 * crypto cipher modules used to do delayed update
732		 * of replay sequence numbers.
733		 */
734		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
735			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
736				/*
737				 * Discard encrypted frames when privacy is off.
738				 */
739				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
740				    wh, "WEP", "%s", "PRIVACY off");
741				vap->iv_stats.is_rx_noprivacy++;
742				IEEE80211_NODE_STAT(ni, rx_noprivacy);
743				goto out;
744			}
745			key = ieee80211_crypto_decap(ni, m, hdrspace);
746			if (key == NULL) {
747				/* NB: stats+msgs handled in crypto_decap */
748				IEEE80211_NODE_STAT(ni, rx_wepfail);
749				goto out;
750			}
751			wh = mtod(m, struct ieee80211_frame *);
752			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
753		} else {
754			/* XXX M_WEP and IEEE80211_F_PRIVACY */
755			key = NULL;
756		}
757
758		/*
759		 * Save QoS bits for use below--before we strip the header.
760		 */
761		if (subtype == IEEE80211_FC0_SUBTYPE_QOS)
762			qos = ieee80211_getqos(wh)[0];
763		else
764			qos = 0;
765
766		/*
767		 * Next up, any fragmentation.
768		 */
769		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
770			m = ieee80211_defrag(ni, m, hdrspace);
771			if (m == NULL) {
772				/* Fragment dropped or frame not complete yet */
773				goto out;
774			}
775		}
776		wh = NULL;		/* no longer valid, catch any uses */
777
778		/*
779		 * Next strip any MSDU crypto bits.
780		 */
781		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
782			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
783			    ni->ni_macaddr, "data", "%s", "demic error");
784			vap->iv_stats.is_rx_demicfail++;
785			IEEE80211_NODE_STAT(ni, rx_demicfail);
786			goto out;
787		}
788
789		/* copy to listener after decrypt */
790		if (ieee80211_radiotap_active_vap(vap))
791			ieee80211_radiotap_rx(vap, m);
792		need_tap = 0;
793
794		/*
795		 * Finally, strip the 802.11 header.
796		 */
797		m = ieee80211_decap(vap, m, hdrspace);
798		if (m == NULL) {
799			/* XXX mask bit to check for both */
800			/* don't count Null data frames as errors */
801			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
802			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
803				goto out;
804			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
805			    ni->ni_macaddr, "data", "%s", "decap error");
806			vap->iv_stats.is_rx_decap++;
807			IEEE80211_NODE_STAT(ni, rx_decap);
808			goto err;
809		}
810		eh = mtod(m, struct ether_header *);
811		if (!ieee80211_node_is_authorized(ni)) {
812			/*
813			 * Deny any non-PAE frames received prior to
814			 * authorization.  For open/shared-key
815			 * authentication the port is mark authorized
816			 * after authentication completes.  For 802.1x
817			 * the port is not marked authorized by the
818			 * authenticator until the handshake has completed.
819			 */
820			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
821				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
822				    eh->ether_shost, "data",
823				    "unauthorized port: ether type 0x%x len %u",
824				    eh->ether_type, m->m_pkthdr.len);
825				vap->iv_stats.is_rx_unauth++;
826				IEEE80211_NODE_STAT(ni, rx_unauth);
827				goto err;
828			}
829		} else {
830			/*
831			 * When denying unencrypted frames, discard
832			 * any non-PAE frames received without encryption.
833			 */
834			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
835			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
836			    eh->ether_type != htons(ETHERTYPE_PAE)) {
837				/*
838				 * Drop unencrypted frames.
839				 */
840				vap->iv_stats.is_rx_unencrypted++;
841				IEEE80211_NODE_STAT(ni, rx_unencrypted);
842				goto out;
843			}
844		}
845		/* XXX require HT? */
846		if (qos & IEEE80211_QOS_AMSDU) {
847			m = ieee80211_decap_amsdu(ni, m);
848			if (m == NULL)
849				return IEEE80211_FC0_TYPE_DATA;
850		} else {
851#ifdef IEEE80211_SUPPORT_SUPERG
852			m = ieee80211_decap_fastframe(vap, ni, m);
853			if (m == NULL)
854				return IEEE80211_FC0_TYPE_DATA;
855#endif
856		}
857		ieee80211_deliver_data(vap, ni, m);
858		return IEEE80211_FC0_TYPE_DATA;
859
860	case IEEE80211_FC0_TYPE_MGT:
861		vap->iv_stats.is_rx_mgmt++;
862		IEEE80211_NODE_STAT(ni, rx_mgmt);
863		if (dir != IEEE80211_FC1_DIR_NODS) {
864			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
865			    wh, "data", "incorrect dir 0x%x", dir);
866			vap->iv_stats.is_rx_wrongdir++;
867			goto err;
868		}
869		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
870			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
871			    ni->ni_macaddr, "mgt", "too short: len %u",
872			    m->m_pkthdr.len);
873			vap->iv_stats.is_rx_tooshort++;
874			goto out;
875		}
876#ifdef IEEE80211_DEBUG
877		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
878		    ieee80211_msg_dumppkts(vap)) {
879			if_printf(ifp, "received %s from %s rssi %d\n",
880			    ieee80211_mgt_subtype_name(subtype),
881			    ether_sprintf(wh->i_addr2), rssi);
882		}
883#endif
884		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
885			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
886				/*
887				 * Only shared key auth frames with a challenge
888				 * should be encrypted, discard all others.
889				 */
890				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
891				    wh, ieee80211_mgt_subtype_name(subtype),
892				    "%s", "WEP set but not permitted");
893				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
894				goto out;
895			}
896			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
897				/*
898				 * Discard encrypted frames when privacy is off.
899				 */
900				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
901				    wh, "mgt", "%s", "WEP set but PRIVACY off");
902				vap->iv_stats.is_rx_noprivacy++;
903				goto out;
904			}
905			hdrspace = ieee80211_hdrspace(ic, wh);
906			key = ieee80211_crypto_decap(ni, m, hdrspace);
907			if (key == NULL) {
908				/* NB: stats+msgs handled in crypto_decap */
909				goto out;
910			}
911			wh = mtod(m, struct ieee80211_frame *);
912			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
913		}
914		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
915		goto out;
916
917	case IEEE80211_FC0_TYPE_CTL:
918		vap->iv_stats.is_rx_ctl++;
919		IEEE80211_NODE_STAT(ni, rx_ctrl);
920		vap->iv_recv_ctl(ni, m, subtype);
921		goto out;
922
923	default:
924		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
925		    wh, NULL, "bad frame type 0x%x", type);
926		/* should not come here */
927		break;
928	}
929err:
930	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
931out:
932	if (m != NULL) {
933		if (need_tap && ieee80211_radiotap_active_vap(vap))
934			ieee80211_radiotap_rx(vap, m);
935		m_freem(m);
936	}
937	return type;
938}
939
940static void
941sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
942    int rssi, int nf, uint16_t seq, uint16_t status)
943{
944	struct ieee80211vap *vap = ni->ni_vap;
945
946	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
947		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
948		    ni->ni_macaddr, "open auth",
949		    "bad sta auth mode %u", ni->ni_authmode);
950		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
951		return;
952	}
953	if (vap->iv_state != IEEE80211_S_AUTH ||
954	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
955		vap->iv_stats.is_rx_bad_auth++;
956		return;
957	}
958	if (status != 0) {
959		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
960		    ni, "open auth failed (reason %d)", status);
961		vap->iv_stats.is_rx_auth_fail++;
962		vap->iv_stats.is_rx_authfail_code = status;
963		ieee80211_new_state(vap, IEEE80211_S_SCAN,
964		    IEEE80211_SCAN_FAIL_STATUS);
965	} else
966		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
967}
968
969static void
970sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
971    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
972    uint16_t seq, uint16_t status)
973{
974	struct ieee80211vap *vap = ni->ni_vap;
975	uint8_t *challenge;
976
977	/*
978	 * NB: this can happen as we allow pre-shared key
979	 * authentication to be enabled w/o wep being turned
980	 * on so that configuration of these can be done
981	 * in any order.  It may be better to enforce the
982	 * ordering in which case this check would just be
983	 * for sanity/consistency.
984	 */
985	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
986		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
987		    ni->ni_macaddr, "shared key auth",
988		    "%s", " PRIVACY is disabled");
989		goto bad;
990	}
991	/*
992	 * Pre-shared key authentication is evil; accept
993	 * it only if explicitly configured (it is supported
994	 * mainly for compatibility with clients like OS X).
995	 */
996	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
997	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
998		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
999		    ni->ni_macaddr, "shared key auth",
1000		    "bad sta auth mode %u", ni->ni_authmode);
1001		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1002		goto bad;
1003	}
1004
1005	challenge = NULL;
1006	if (frm + 1 < efrm) {
1007		if ((frm[1] + 2) > (efrm - frm)) {
1008			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1009			    ni->ni_macaddr, "shared key auth",
1010			    "ie %d/%d too long",
1011			    frm[0], (frm[1] + 2) - (efrm - frm));
1012			vap->iv_stats.is_rx_bad_auth++;
1013			goto bad;
1014		}
1015		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1016			challenge = frm;
1017		frm += frm[1] + 2;
1018	}
1019	switch (seq) {
1020	case IEEE80211_AUTH_SHARED_CHALLENGE:
1021	case IEEE80211_AUTH_SHARED_RESPONSE:
1022		if (challenge == NULL) {
1023			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1024			    ni->ni_macaddr, "shared key auth",
1025			    "%s", "no challenge");
1026			vap->iv_stats.is_rx_bad_auth++;
1027			goto bad;
1028		}
1029		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1030			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1031			    ni->ni_macaddr, "shared key auth",
1032			    "bad challenge len %d", challenge[1]);
1033			vap->iv_stats.is_rx_bad_auth++;
1034			goto bad;
1035		}
1036	default:
1037		break;
1038	}
1039	if (vap->iv_state != IEEE80211_S_AUTH)
1040		return;
1041	switch (seq) {
1042	case IEEE80211_AUTH_SHARED_PASS:
1043		if (ni->ni_challenge != NULL) {
1044			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1045			ni->ni_challenge = NULL;
1046		}
1047		if (status != 0) {
1048			IEEE80211_NOTE_FRAME(vap,
1049			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1050			    "shared key auth failed (reason %d)", status);
1051			vap->iv_stats.is_rx_auth_fail++;
1052			vap->iv_stats.is_rx_authfail_code = status;
1053			return;
1054		}
1055		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1056		break;
1057	case IEEE80211_AUTH_SHARED_CHALLENGE:
1058		if (!ieee80211_alloc_challenge(ni))
1059			return;
1060		/* XXX could optimize by passing recvd challenge */
1061		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1062		IEEE80211_SEND_MGMT(ni,
1063			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1064		break;
1065	default:
1066		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1067		    wh, "shared key auth", "bad seq %d", seq);
1068		vap->iv_stats.is_rx_bad_auth++;
1069		return;
1070	}
1071	return;
1072bad:
1073	/*
1074	 * Kick the state machine.  This short-circuits
1075	 * using the mgt frame timeout to trigger the
1076	 * state transition.
1077	 */
1078	if (vap->iv_state == IEEE80211_S_AUTH)
1079		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1080		    IEEE80211_SCAN_FAIL_STATUS);
1081}
1082
1083int
1084ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1085	const struct ieee80211_frame *wh)
1086{
1087#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1088	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1089	u_int len = frm[1], qosinfo;
1090	int i;
1091
1092	if (len < sizeof(struct ieee80211_wme_param)-2) {
1093		IEEE80211_DISCARD_IE(vap,
1094		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1095		    wh, "WME", "too short, len %u", len);
1096		return -1;
1097	}
1098	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1099	qosinfo &= WME_QOSINFO_COUNT;
1100	/* XXX do proper check for wraparound */
1101	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1102		return 0;
1103	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1104	for (i = 0; i < WME_NUM_AC; i++) {
1105		struct wmeParams *wmep =
1106			&wme->wme_wmeChanParams.cap_wmeParams[i];
1107		/* NB: ACI not used */
1108		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1109		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1110		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1111		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1112		wmep->wmep_txopLimit = le16dec(frm+2);
1113		frm += 4;
1114	}
1115	wme->wme_wmeChanParams.cap_info = qosinfo;
1116	return 1;
1117#undef MS
1118}
1119
1120/*
1121 * Process 11h Channel Switch Announcement (CSA) ie.  If this
1122 * is the first CSA then initiate the switch.  Otherwise we
1123 * track state and trigger completion and/or cancel of the switch.
1124 * XXX should be public for IBSS use
1125 */
1126static void
1127ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1128	const struct ieee80211_frame *wh)
1129{
1130	struct ieee80211com *ic = vap->iv_ic;
1131	const struct ieee80211_csa_ie *csa =
1132	    (const struct ieee80211_csa_ie *) frm;
1133
1134	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1135	    ("state %s", ieee80211_state_name[vap->iv_state]));
1136
1137	if (csa->csa_mode > 1) {
1138		IEEE80211_DISCARD_IE(vap,
1139		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1140		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1141		return;
1142	}
1143	IEEE80211_LOCK(ic);
1144	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1145		/*
1146		 * Convert the channel number to a channel reference.  We
1147		 * try first to preserve turbo attribute of the current
1148		 * channel then fallback.  Note this will not work if the
1149		 * CSA specifies a channel that requires a band switch (e.g.
1150		 * 11a => 11g).  This is intentional as 11h is defined only
1151		 * for 5GHz/11a and because the switch does not involve a
1152		 * reassociation, protocol state (capabilities, negotated
1153		 * rates, etc) may/will be wrong.
1154		 */
1155		struct ieee80211_channel *c =
1156		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1157			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1158		if (c == NULL) {
1159			c = ieee80211_find_channel_byieee(ic,
1160			    csa->csa_newchan,
1161			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1162			if (c == NULL) {
1163				IEEE80211_DISCARD_IE(vap,
1164				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1165				    wh, "CSA", "invalid channel %u",
1166				    csa->csa_newchan);
1167				goto done;
1168			}
1169		}
1170#if IEEE80211_CSA_COUNT_MIN > 0
1171		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1172			/*
1173			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1174			 * reduce the risk of being redirected by a fabricated
1175			 * CSA.  If a valid CSA is dropped we'll still get a
1176			 * beacon miss when the AP leaves the channel so we'll
1177			 * eventually follow to the new channel.
1178			 *
1179			 * NOTE: this violates the 11h spec that states that
1180			 * count may be any value and if 0 then a switch
1181			 * should happen asap.
1182			 */
1183			IEEE80211_DISCARD_IE(vap,
1184			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1185			    wh, "CSA", "count %u too small, must be >= %u",
1186			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1187			goto done;
1188		}
1189#endif
1190		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1191	} else {
1192		/*
1193		 * Validate this ie against the initial CSA.  We require
1194		 * mode and channel not change and the count must be
1195		 * monotonically decreasing.  This may be pointless and
1196		 * canceling the switch as a result may be too paranoid but
1197		 * in the worst case if we drop out of CSA because of this
1198		 * and the AP does move then we'll just end up taking a
1199		 * beacon miss and scan to find the AP.
1200		 *
1201		 * XXX may want <= on count as we also process ProbeResp
1202		 * frames and those may come in w/ the same count as the
1203		 * previous beacon; but doing so leaves us open to a stuck
1204		 * count until we add a dead-man timer
1205		 */
1206		if (!(csa->csa_count < ic->ic_csa_count &&
1207		      csa->csa_mode == ic->ic_csa_mode &&
1208		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1209			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1210			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1211			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1212			    ic->ic_csa_newchan, ic->ic_csa_count,
1213			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1214			ieee80211_csa_cancelswitch(ic);
1215		} else {
1216			if (csa->csa_count <= 1)
1217				ieee80211_csa_completeswitch(ic);
1218			else
1219				ic->ic_csa_count = csa->csa_count;
1220		}
1221	}
1222done:
1223	IEEE80211_UNLOCK(ic);
1224}
1225
1226/*
1227 * Return non-zero if a background scan may be continued:
1228 * o bg scan is active
1229 * o no channel switch is pending
1230 * o there has not been any traffic recently
1231 *
1232 * Note we do not check if there is an administrative enable;
1233 * this is only done to start the scan.  We assume that any
1234 * change in state will be accompanied by a request to cancel
1235 * active scans which will otherwise cause this test to fail.
1236 */
1237static __inline int
1238contbgscan(struct ieee80211vap *vap)
1239{
1240	struct ieee80211com *ic = vap->iv_ic;
1241
1242	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1243	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1244	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1245	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1246}
1247
1248/*
1249 * Return non-zero if a backgrond scan may be started:
1250 * o bg scanning is administratively enabled
1251 * o no channel switch is pending
1252 * o we are not boosted on a dynamic turbo channel
1253 * o there has not been a scan recently
1254 * o there has not been any traffic recently
1255 */
1256static __inline int
1257startbgscan(struct ieee80211vap *vap)
1258{
1259	struct ieee80211com *ic = vap->iv_ic;
1260
1261	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1262	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1263#ifdef IEEE80211_SUPPORT_SUPERG
1264	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1265#endif
1266	    ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1267	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1268}
1269
1270static void
1271sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1272    const struct ieee80211_rx_stats *rxs,
1273    int rssi, int nf)
1274{
1275#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1276	struct ieee80211vap *vap = ni->ni_vap;
1277	struct ieee80211com *ic = ni->ni_ic;
1278	struct ieee80211_channel *rxchan = ic->ic_curchan;
1279	struct ieee80211_frame *wh;
1280	uint8_t *frm, *efrm;
1281	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1282	uint8_t rate;
1283	int ht_state_change = 0;
1284
1285	wh = mtod(m0, struct ieee80211_frame *);
1286	frm = (uint8_t *)&wh[1];
1287	efrm = mtod(m0, uint8_t *) + m0->m_len;
1288	switch (subtype) {
1289	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1290	case IEEE80211_FC0_SUBTYPE_BEACON: {
1291		struct ieee80211_scanparams scan;
1292		struct ieee80211_channel *c;
1293		/*
1294		 * We process beacon/probe response frames:
1295		 *    o when scanning, or
1296		 *    o station mode when associated (to collect state
1297		 *      updates such as 802.11g slot time)
1298		 * Frames otherwise received are discarded.
1299		 */
1300		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1301			vap->iv_stats.is_rx_mgtdiscard++;
1302			return;
1303		}
1304
1305		/* Override RX channel as appropriate */
1306		if (rxs != NULL) {
1307			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1308			if (c != NULL)
1309				rxchan = c;
1310		}
1311
1312		/* XXX probe response in sta mode when !scanning? */
1313		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1314			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1315				vap->iv_stats.is_beacon_bad++;
1316			return;
1317		}
1318
1319		/*
1320		 * Count frame now that we know it's to be processed.
1321		 */
1322		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1323			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1324			IEEE80211_NODE_STAT(ni, rx_beacons);
1325		} else
1326			IEEE80211_NODE_STAT(ni, rx_proberesp);
1327		/*
1328		 * When operating in station mode, check for state updates.
1329		 * Be careful to ignore beacons received while doing a
1330		 * background scan.  We consider only 11g/WMM stuff right now.
1331		 */
1332		if (ni->ni_associd != 0 &&
1333		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1334		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1335			/* record tsf of last beacon */
1336			memcpy(ni->ni_tstamp.data, scan.tstamp,
1337				sizeof(ni->ni_tstamp));
1338			/* count beacon frame for s/w bmiss handling */
1339			vap->iv_swbmiss_count++;
1340			vap->iv_bmiss_count = 0;
1341			if (ni->ni_erp != scan.erp) {
1342				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1343				    wh->i_addr2,
1344				    "erp change: was 0x%x, now 0x%x",
1345				    ni->ni_erp, scan.erp);
1346				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1347				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1348					ic->ic_flags |= IEEE80211_F_USEPROT;
1349				else
1350					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1351				ni->ni_erp = scan.erp;
1352				/* XXX statistic */
1353				/* XXX driver notification */
1354			}
1355			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1356				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1357				    wh->i_addr2,
1358				    "capabilities change: was 0x%x, now 0x%x",
1359				    ni->ni_capinfo, scan.capinfo);
1360				/*
1361				 * NB: we assume short preamble doesn't
1362				 *     change dynamically
1363				 */
1364				ieee80211_set_shortslottime(ic,
1365					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1366					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1367				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1368					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1369				/* XXX statistic */
1370			}
1371			if (scan.wme != NULL &&
1372			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1373			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1374				ieee80211_wme_updateparams(vap);
1375#ifdef IEEE80211_SUPPORT_SUPERG
1376			if (scan.ath != NULL)
1377				ieee80211_parse_athparams(ni, scan.ath, wh);
1378#endif
1379			if (scan.htcap != NULL && scan.htinfo != NULL &&
1380			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1381				/* XXX state changes? */
1382				if (ieee80211_ht_updateparams(ni,
1383				    scan.htcap, scan.htinfo))
1384					ht_state_change = 1;
1385			}
1386			if (scan.quiet)
1387				ic->ic_set_quiet(ni, scan.quiet);
1388
1389			if (scan.tim != NULL) {
1390				struct ieee80211_tim_ie *tim =
1391				    (struct ieee80211_tim_ie *) scan.tim;
1392				/*
1393				 * XXX Check/debug this code; see if it's about
1394				 * the right time to force the VAP awake if we
1395				 * receive a frame destined for us?
1396				 */
1397				int aid = IEEE80211_AID(ni->ni_associd);
1398				int ix = aid / NBBY;
1399				int min = tim->tim_bitctl &~ 1;
1400				int max = tim->tim_len + min - 4;
1401				int tim_ucast = 0, tim_mcast = 0;
1402
1403				/*
1404				 * Only do this for unicast traffic in the TIM
1405				 * The multicast traffic notification for
1406				 * the scan notification stuff should occur
1407				 * differently.
1408				 */
1409				if (min <= ix && ix <= max &&
1410				     isset(tim->tim_bitmap - min, aid)) {
1411					tim_ucast = 1;
1412				}
1413
1414				/*
1415				 * Do a separate notification
1416				 * for the multicast bit being set.
1417				 */
1418				if (tim->tim_bitctl & 1) {
1419					tim_mcast = 1;
1420				}
1421
1422				/*
1423				 * If the TIM indicates there's traffic for
1424				 * us then get us out of STA mode powersave.
1425				 */
1426				if (tim_ucast == 1) {
1427
1428					/*
1429					 * Wake us out of SLEEP state if we're
1430					 * in it; and if we're doing bgscan
1431					 * then wake us out of STA powersave.
1432					 */
1433					ieee80211_sta_tim_notify(vap, 1);
1434
1435					/*
1436					 * This is preventing us from
1437					 * continuing a bgscan; because it
1438					 * tricks the contbgscan()
1439					 * routine to think there's always
1440					 * traffic for us.
1441					 *
1442					 * I think we need both an RX and
1443					 * TX ic_lastdata field.
1444					 */
1445					ic->ic_lastdata = ticks;
1446				}
1447
1448				ni->ni_dtim_count = tim->tim_count;
1449				ni->ni_dtim_period = tim->tim_period;
1450			}
1451			if (scan.csa != NULL &&
1452			    (vap->iv_flags & IEEE80211_F_DOTH))
1453				ieee80211_parse_csaparams(vap, scan.csa, wh);
1454			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1455				/*
1456				 * No CSA ie or 11h disabled, but a channel
1457				 * switch is pending; drop out so we aren't
1458				 * stuck in CSA state.  If the AP really is
1459				 * moving we'll get a beacon miss and scan.
1460				 */
1461				IEEE80211_LOCK(ic);
1462				ieee80211_csa_cancelswitch(ic);
1463				IEEE80211_UNLOCK(ic);
1464			}
1465			/*
1466			 * If scanning, pass the info to the scan module.
1467			 * Otherwise, check if it's the right time to do
1468			 * a background scan.  Background scanning must
1469			 * be enabled and we must not be operating in the
1470			 * turbo phase of dynamic turbo mode.  Then,
1471			 * it's been a while since the last background
1472			 * scan and if no data frames have come through
1473			 * recently, kick off a scan.  Note that this
1474			 * is the mechanism by which a background scan
1475			 * is started _and_ continued each time we
1476			 * return on-channel to receive a beacon from
1477			 * our ap.
1478			 */
1479			if (ic->ic_flags & IEEE80211_F_SCAN) {
1480				ieee80211_add_scan(vap, rxchan,
1481				    &scan, wh, subtype, rssi, nf);
1482			} else if (contbgscan(vap)) {
1483				ieee80211_bg_scan(vap, 0);
1484			} else if (startbgscan(vap)) {
1485				vap->iv_stats.is_scan_bg++;
1486#if 0
1487				/* wakeup if we are sleeing */
1488				ieee80211_set_pwrsave(vap, 0);
1489#endif
1490				ieee80211_bg_scan(vap, 0);
1491			}
1492
1493			/*
1494			 * Put the station to sleep if we haven't seen
1495			 * traffic in a while.
1496			 */
1497			IEEE80211_LOCK(ic);
1498			ieee80211_sta_ps_timer_check(vap);
1499			IEEE80211_UNLOCK(ic);
1500
1501			/*
1502			 * If we've had a channel width change (eg HT20<->HT40)
1503			 * then schedule a delayed driver notification.
1504			 */
1505			if (ht_state_change)
1506				ieee80211_update_chw(ic);
1507			return;
1508		}
1509		/*
1510		 * If scanning, just pass information to the scan module.
1511		 */
1512		if (ic->ic_flags & IEEE80211_F_SCAN) {
1513			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1514				/*
1515				 * Actively scanning a channel marked passive;
1516				 * send a probe request now that we know there
1517				 * is 802.11 traffic present.
1518				 *
1519				 * XXX check if the beacon we recv'd gives
1520				 * us what we need and suppress the probe req
1521				 */
1522				ieee80211_probe_curchan(vap, 1);
1523				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1524			}
1525			ieee80211_add_scan(vap, rxchan, &scan, wh,
1526			    subtype, rssi, nf);
1527			return;
1528		}
1529		break;
1530	}
1531
1532	case IEEE80211_FC0_SUBTYPE_AUTH: {
1533		uint16_t algo, seq, status;
1534		/*
1535		 * auth frame format
1536		 *	[2] algorithm
1537		 *	[2] sequence
1538		 *	[2] status
1539		 *	[tlv*] challenge
1540		 */
1541		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1542		algo   = le16toh(*(uint16_t *)frm);
1543		seq    = le16toh(*(uint16_t *)(frm + 2));
1544		status = le16toh(*(uint16_t *)(frm + 4));
1545		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1546		    "recv auth frame with algorithm %d seq %d", algo, seq);
1547
1548		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1549			IEEE80211_DISCARD(vap,
1550			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1551			    wh, "auth", "%s", "TKIP countermeasures enabled");
1552			vap->iv_stats.is_rx_auth_countermeasures++;
1553			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1554				ieee80211_send_error(ni, wh->i_addr2,
1555					IEEE80211_FC0_SUBTYPE_AUTH,
1556					IEEE80211_REASON_MIC_FAILURE);
1557			}
1558			return;
1559		}
1560		if (algo == IEEE80211_AUTH_ALG_SHARED)
1561			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1562			    seq, status);
1563		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1564			sta_auth_open(ni, wh, rssi, nf, seq, status);
1565		else {
1566			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1567			    wh, "auth", "unsupported alg %d", algo);
1568			vap->iv_stats.is_rx_auth_unsupported++;
1569			return;
1570		}
1571		break;
1572	}
1573
1574	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1575	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1576		uint16_t capinfo, associd;
1577		uint16_t status;
1578
1579		if (vap->iv_state != IEEE80211_S_ASSOC) {
1580			vap->iv_stats.is_rx_mgtdiscard++;
1581			return;
1582		}
1583
1584		/*
1585		 * asresp frame format
1586		 *	[2] capability information
1587		 *	[2] status
1588		 *	[2] association ID
1589		 *	[tlv] supported rates
1590		 *	[tlv] extended supported rates
1591		 *	[tlv] WME
1592		 *	[tlv] HT capabilities
1593		 *	[tlv] HT info
1594		 */
1595		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1596		ni = vap->iv_bss;
1597		capinfo = le16toh(*(uint16_t *)frm);
1598		frm += 2;
1599		status = le16toh(*(uint16_t *)frm);
1600		frm += 2;
1601		if (status != 0) {
1602			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1603			    wh->i_addr2, "%sassoc failed (reason %d)",
1604			    ISREASSOC(subtype) ?  "re" : "", status);
1605			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1606			return;
1607		}
1608		associd = le16toh(*(uint16_t *)frm);
1609		frm += 2;
1610
1611		rates = xrates = wme = htcap = htinfo = NULL;
1612		while (efrm - frm > 1) {
1613			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1614			switch (*frm) {
1615			case IEEE80211_ELEMID_RATES:
1616				rates = frm;
1617				break;
1618			case IEEE80211_ELEMID_XRATES:
1619				xrates = frm;
1620				break;
1621			case IEEE80211_ELEMID_HTCAP:
1622				htcap = frm;
1623				break;
1624			case IEEE80211_ELEMID_HTINFO:
1625				htinfo = frm;
1626				break;
1627			case IEEE80211_ELEMID_VENDOR:
1628				if (iswmeoui(frm))
1629					wme = frm;
1630				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1631					/*
1632					 * Accept pre-draft HT ie's if the
1633					 * standard ones have not been seen.
1634					 */
1635					if (ishtcapoui(frm)) {
1636						if (htcap == NULL)
1637							htcap = frm;
1638					} else if (ishtinfooui(frm)) {
1639						if (htinfo == NULL)
1640							htinfo = frm;
1641					}
1642				}
1643				/* XXX Atheros OUI support */
1644				break;
1645			}
1646			frm += frm[1] + 2;
1647		}
1648
1649		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1650		if (xrates != NULL)
1651			IEEE80211_VERIFY_ELEMENT(xrates,
1652				IEEE80211_RATE_MAXSIZE - rates[1], return);
1653		rate = ieee80211_setup_rates(ni, rates, xrates,
1654				IEEE80211_F_JOIN |
1655				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1656				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1657		if (rate & IEEE80211_RATE_BASIC) {
1658			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1659			    wh->i_addr2,
1660			    "%sassoc failed (rate set mismatch)",
1661			    ISREASSOC(subtype) ?  "re" : "");
1662			vap->iv_stats.is_rx_assoc_norate++;
1663			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1664			    IEEE80211_SCAN_FAIL_STATUS);
1665			return;
1666		}
1667
1668		ni->ni_capinfo = capinfo;
1669		ni->ni_associd = associd;
1670		if (ni->ni_jointime == 0)
1671			ni->ni_jointime = time_uptime;
1672		if (wme != NULL &&
1673		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1674			ni->ni_flags |= IEEE80211_NODE_QOS;
1675			ieee80211_wme_updateparams(vap);
1676		} else
1677			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1678		/*
1679		 * Setup HT state according to the negotiation.
1680		 *
1681		 * NB: shouldn't need to check if HT use is enabled but some
1682		 *     ap's send back HT ie's even when we don't indicate we
1683		 *     are HT capable in our AssocReq.
1684		 */
1685		if (htcap != NULL && htinfo != NULL &&
1686		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1687			ieee80211_ht_node_init(ni);
1688			ieee80211_ht_updateparams(ni, htcap, htinfo);
1689			ieee80211_setup_htrates(ni, htcap,
1690			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1691			ieee80211_setup_basic_htrates(ni, htinfo);
1692			ieee80211_node_setuptxparms(ni);
1693			ieee80211_ratectl_node_init(ni);
1694		}
1695
1696		/*
1697		 * Always initialise FF/superg state; we can use this
1698		 * for doing A-MSDU encapsulation as well.
1699		 */
1700#ifdef	IEEE80211_SUPPORT_SUPERG
1701		ieee80211_ff_node_init(ni);
1702#endif
1703
1704		/*
1705		 * Configure state now that we are associated.
1706		 *
1707		 * XXX may need different/additional driver callbacks?
1708		 */
1709		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1710		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1711			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1712			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1713		} else {
1714			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1715			ic->ic_flags |= IEEE80211_F_USEBARKER;
1716		}
1717		ieee80211_set_shortslottime(ic,
1718			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1719			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1720		/*
1721		 * Honor ERP protection.
1722		 *
1723		 * NB: ni_erp should zero for non-11g operation.
1724		 */
1725		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1726		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1727			ic->ic_flags |= IEEE80211_F_USEPROT;
1728		else
1729			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1730		IEEE80211_NOTE_MAC(vap,
1731		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1732		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1733		    ISREASSOC(subtype) ? "re" : "",
1734		    IEEE80211_NODE_AID(ni),
1735		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1736		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1737		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1738		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1739		    ni->ni_flags & IEEE80211_NODE_HT ?
1740			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1741		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1742		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1743			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1744		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1745		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1746			", fast-frames" : "",
1747		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1748			", turbo" : ""
1749		);
1750		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1751		break;
1752	}
1753
1754	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1755		uint16_t reason;
1756
1757		if (vap->iv_state == IEEE80211_S_SCAN) {
1758			vap->iv_stats.is_rx_mgtdiscard++;
1759			return;
1760		}
1761		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1762			/* NB: can happen when in promiscuous mode */
1763			vap->iv_stats.is_rx_mgtdiscard++;
1764			break;
1765		}
1766
1767		/*
1768		 * deauth frame format
1769		 *	[2] reason
1770		 */
1771		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1772		reason = le16toh(*(uint16_t *)frm);
1773
1774		vap->iv_stats.is_rx_deauth++;
1775		vap->iv_stats.is_rx_deauth_code = reason;
1776		IEEE80211_NODE_STAT(ni, rx_deauth);
1777
1778		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1779		    "recv deauthenticate (reason: %d (%s))", reason,
1780		    ieee80211_reason_to_string(reason));
1781		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1782		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1783		break;
1784	}
1785
1786	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1787		uint16_t reason;
1788
1789		if (vap->iv_state != IEEE80211_S_RUN &&
1790		    vap->iv_state != IEEE80211_S_ASSOC &&
1791		    vap->iv_state != IEEE80211_S_AUTH) {
1792			vap->iv_stats.is_rx_mgtdiscard++;
1793			return;
1794		}
1795		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1796			/* NB: can happen when in promiscuous mode */
1797			vap->iv_stats.is_rx_mgtdiscard++;
1798			break;
1799		}
1800
1801		/*
1802		 * disassoc frame format
1803		 *	[2] reason
1804		 */
1805		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1806		reason = le16toh(*(uint16_t *)frm);
1807
1808		vap->iv_stats.is_rx_disassoc++;
1809		vap->iv_stats.is_rx_disassoc_code = reason;
1810		IEEE80211_NODE_STAT(ni, rx_disassoc);
1811
1812		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1813		    "recv disassociate (reason: %d (%s))", reason,
1814		    ieee80211_reason_to_string(reason));
1815		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1816		break;
1817	}
1818
1819	case IEEE80211_FC0_SUBTYPE_ACTION:
1820	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1821		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1822		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1823			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1824			    wh, NULL, "%s", "not for us");
1825			vap->iv_stats.is_rx_mgtdiscard++;
1826		} else if (vap->iv_state != IEEE80211_S_RUN) {
1827			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1828			    wh, NULL, "wrong state %s",
1829			    ieee80211_state_name[vap->iv_state]);
1830			vap->iv_stats.is_rx_mgtdiscard++;
1831		} else {
1832			if (ieee80211_parse_action(ni, m0) == 0)
1833				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1834		}
1835		break;
1836
1837	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1838	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1839	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1840	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1841	case IEEE80211_FC0_SUBTYPE_ATIM:
1842		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1843		    wh, NULL, "%s", "not handled");
1844		vap->iv_stats.is_rx_mgtdiscard++;
1845		break;
1846
1847	default:
1848		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1849		    wh, "mgt", "subtype 0x%x not handled", subtype);
1850		vap->iv_stats.is_rx_badsubtype++;
1851		break;
1852	}
1853#undef ISREASSOC
1854}
1855
1856static void
1857sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1858{
1859	switch (subtype) {
1860	case IEEE80211_FC0_SUBTYPE_BAR:
1861		ieee80211_recv_bar(ni, m);
1862		break;
1863	}
1864}
1865