ieee80211_sta.c revision 191547
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: head/sys/net80211/ieee80211_sta.c 191547 2009-04-26 21:50:21Z sam $");
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/ethernet.h>
54
55#include <net/bpf.h>
56
57#include <net80211/ieee80211_var.h>
58#include <net80211/ieee80211_sta.h>
59#include <net80211/ieee80211_input.h>
60#ifdef IEEE80211_SUPPORT_SUPERG
61#include <net80211/ieee80211_superg.h>
62#endif
63
64#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
65
66static	void sta_vattach(struct ieee80211vap *);
67static	void sta_beacon_miss(struct ieee80211vap *);
68static	int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
69static	int sta_input(struct ieee80211_node *, struct mbuf *,
70	    int rssi, int noise, uint32_t rstamp);
71static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
72	    int subtype, int rssi, int noise, uint32_t rstamp);
73static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
74
75void
76ieee80211_sta_attach(struct ieee80211com *ic)
77{
78	ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
79}
80
81void
82ieee80211_sta_detach(struct ieee80211com *ic)
83{
84}
85
86static void
87sta_vdetach(struct ieee80211vap *vap)
88{
89}
90
91static void
92sta_vattach(struct ieee80211vap *vap)
93{
94	vap->iv_newstate = sta_newstate;
95	vap->iv_input = sta_input;
96	vap->iv_recv_mgmt = sta_recv_mgmt;
97	vap->iv_recv_ctl = sta_recv_ctl;
98	vap->iv_opdetach = sta_vdetach;
99	vap->iv_bmiss = sta_beacon_miss;
100}
101
102/*
103 * Handle a beacon miss event.  The common code filters out
104 * spurious events that can happen when scanning and/or before
105 * reaching RUN state.
106 */
107static void
108sta_beacon_miss(struct ieee80211vap *vap)
109{
110	KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
111	KASSERT(vap->iv_state == IEEE80211_S_RUN,
112	    ("wrong state %d", vap->iv_state));
113
114	IEEE80211_DPRINTF(vap,
115		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
116		"beacon miss, mode %u state %s\n",
117		vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
118
119	if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
120		/*
121		 * Send a directed probe req before falling back to a
122		 * scan; if we receive a response ic_bmiss_count will
123		 * be reset.  Some cards mistakenly report beacon miss
124		 * so this avoids the expensive scan if the ap is
125		 * still there.
126		 */
127		ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
128			vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
129			vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
130		return;
131	}
132	vap->iv_bmiss_count = 0;
133	vap->iv_stats.is_beacon_miss++;
134	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
135#ifdef IEEE80211_SUPPORT_SUPERG
136		struct ieee80211com *ic = vap->iv_ic;
137
138		/*
139		 * If we receive a beacon miss interrupt when using
140		 * dynamic turbo, attempt to switch modes before
141		 * reassociating.
142		 */
143		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
144			ieee80211_dturbo_switch(vap,
145			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
146#endif
147		/*
148		 * Try to reassociate before scanning for a new ap.
149		 */
150		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
151	} else {
152		/*
153		 * Somebody else is controlling state changes (e.g.
154		 * a user-mode app) don't do anything that would
155		 * confuse them; just drop into scan mode so they'll
156		 * notified of the state change and given control.
157		 */
158		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
159	}
160}
161
162/*
163 * Handle deauth with reason.  We retry only for
164 * the cases where we might succeed.  Otherwise
165 * we downgrade the ap and scan.
166 */
167static void
168sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
169{
170	switch (reason) {
171	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
172	case IEEE80211_STATUS_TIMEOUT:
173	case IEEE80211_REASON_ASSOC_EXPIRE:
174	case IEEE80211_REASON_NOT_AUTHED:
175	case IEEE80211_REASON_NOT_ASSOCED:
176	case IEEE80211_REASON_ASSOC_LEAVE:
177	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
178		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
179		break;
180	default:
181		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
182		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
183			ieee80211_check_scan_current(vap);
184		break;
185	}
186}
187
188/*
189 * IEEE80211_M_STA vap state machine handler.
190 * This routine handles the main states in the 802.11 protocol.
191 */
192static int
193sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
194{
195	struct ieee80211com *ic = vap->iv_ic;
196	struct ieee80211_node *ni;
197	enum ieee80211_state ostate;
198
199	IEEE80211_LOCK_ASSERT(ic);
200
201	ostate = vap->iv_state;
202	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
203	    __func__, ieee80211_state_name[ostate],
204	    ieee80211_state_name[nstate], arg);
205	vap->iv_state = nstate;			/* state transition */
206	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
207	if (ostate != IEEE80211_S_SCAN)
208		ieee80211_cancel_scan(vap);	/* background scan */
209	ni = vap->iv_bss;			/* NB: no reference held */
210	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
211		callout_stop(&vap->iv_swbmiss);
212	switch (nstate) {
213	case IEEE80211_S_INIT:
214		switch (ostate) {
215		case IEEE80211_S_SLEEP:
216			/* XXX wakeup */
217		case IEEE80211_S_RUN:
218			IEEE80211_SEND_MGMT(ni,
219			    IEEE80211_FC0_SUBTYPE_DISASSOC,
220			    IEEE80211_REASON_ASSOC_LEAVE);
221			ieee80211_sta_leave(ni);
222			break;
223		case IEEE80211_S_ASSOC:
224			IEEE80211_SEND_MGMT(ni,
225			    IEEE80211_FC0_SUBTYPE_DEAUTH,
226			    IEEE80211_REASON_AUTH_LEAVE);
227			break;
228		case IEEE80211_S_SCAN:
229			ieee80211_cancel_scan(vap);
230			break;
231		default:
232			goto invalid;
233		}
234		if (ostate != IEEE80211_S_INIT) {
235			/* NB: optimize INIT -> INIT case */
236			ieee80211_reset_bss(vap);
237		}
238		if (vap->iv_auth->ia_detach != NULL)
239			vap->iv_auth->ia_detach(vap);
240		break;
241	case IEEE80211_S_SCAN:
242		switch (ostate) {
243		case IEEE80211_S_INIT:
244			/*
245			 * Initiate a scan.  We can come here as a result
246			 * of an IEEE80211_IOC_SCAN_REQ too in which case
247			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
248			 * and the scan request parameters will be present
249			 * in iv_scanreq.  Otherwise we do the default.
250			 */
251			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
252				ieee80211_check_scan(vap,
253				    vap->iv_scanreq_flags,
254				    vap->iv_scanreq_duration,
255				    vap->iv_scanreq_mindwell,
256				    vap->iv_scanreq_maxdwell,
257				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
258				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
259			} else
260				ieee80211_check_scan_current(vap);
261			break;
262		case IEEE80211_S_SCAN:
263		case IEEE80211_S_AUTH:
264		case IEEE80211_S_ASSOC:
265			/*
266			 * These can happen either because of a timeout
267			 * on an assoc/auth response or because of a
268			 * change in state that requires a reset.  For
269			 * the former we're called with a non-zero arg
270			 * that is the cause for the failure; pass this
271			 * to the scan code so it can update state.
272			 * Otherwise trigger a new scan unless we're in
273			 * manual roaming mode in which case an application
274			 * must issue an explicit scan request.
275			 */
276			if (arg != 0)
277				ieee80211_scan_assoc_fail(vap,
278					vap->iv_bss->ni_macaddr, arg);
279			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
280				ieee80211_check_scan_current(vap);
281			break;
282		case IEEE80211_S_RUN:		/* beacon miss */
283			/*
284			 * Beacon miss.  Notify user space and if not
285			 * under control of a user application (roaming
286			 * manual) kick off a scan to re-connect.
287			 */
288			ieee80211_sta_leave(ni);
289			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
290				ieee80211_check_scan_current(vap);
291			break;
292		default:
293			goto invalid;
294		}
295		break;
296	case IEEE80211_S_AUTH:
297		switch (ostate) {
298		case IEEE80211_S_INIT:
299		case IEEE80211_S_SCAN:
300			IEEE80211_SEND_MGMT(ni,
301			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
302			break;
303		case IEEE80211_S_AUTH:
304		case IEEE80211_S_ASSOC:
305			switch (arg & 0xff) {
306			case IEEE80211_FC0_SUBTYPE_AUTH:
307				/* ??? */
308				IEEE80211_SEND_MGMT(ni,
309				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
310				break;
311			case IEEE80211_FC0_SUBTYPE_DEAUTH:
312				sta_authretry(vap, ni, arg>>8);
313				break;
314			}
315			break;
316		case IEEE80211_S_RUN:
317			switch (arg & 0xff) {
318			case IEEE80211_FC0_SUBTYPE_AUTH:
319				IEEE80211_SEND_MGMT(ni,
320				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
321				vap->iv_state = ostate;	/* stay RUN */
322				break;
323			case IEEE80211_FC0_SUBTYPE_DEAUTH:
324				ieee80211_sta_leave(ni);
325				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
326					/* try to reauth */
327					IEEE80211_SEND_MGMT(ni,
328					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
329				}
330				break;
331			}
332			break;
333		default:
334			goto invalid;
335		}
336		break;
337	case IEEE80211_S_ASSOC:
338		switch (ostate) {
339		case IEEE80211_S_AUTH:
340		case IEEE80211_S_ASSOC:
341			IEEE80211_SEND_MGMT(ni,
342			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
343			break;
344		case IEEE80211_S_SLEEP:		/* cannot happen */
345		case IEEE80211_S_RUN:
346			ieee80211_sta_leave(ni);
347			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
348				IEEE80211_SEND_MGMT(ni, arg ?
349				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
350				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
351			}
352			break;
353		default:
354			goto invalid;
355		}
356		break;
357	case IEEE80211_S_RUN:
358		if (vap->iv_flags & IEEE80211_F_WPA) {
359			/* XXX validate prerequisites */
360		}
361		switch (ostate) {
362		case IEEE80211_S_RUN:
363			break;
364		case IEEE80211_S_AUTH:		/* when join is done in fw */
365		case IEEE80211_S_ASSOC:
366#ifdef IEEE80211_DEBUG
367			if (ieee80211_msg_debug(vap)) {
368				ieee80211_note(vap, "%s with %s ssid ",
369				    (vap->iv_opmode == IEEE80211_M_STA ?
370				    "associated" : "synchronized"),
371				    ether_sprintf(ni->ni_bssid));
372				ieee80211_print_essid(vap->iv_bss->ni_essid,
373				    ni->ni_esslen);
374				/* XXX MCS/HT */
375				printf(" channel %d start %uMb\n",
376				    ieee80211_chan2ieee(ic, ic->ic_curchan),
377				    IEEE80211_RATE2MBS(ni->ni_txrate));
378			}
379#endif
380			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
381			ieee80211_notify_node_join(ni,
382			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
383			break;
384		case IEEE80211_S_SLEEP:
385			ieee80211_sta_pwrsave(vap, 0);
386			break;
387		default:
388			goto invalid;
389		}
390		ieee80211_sync_curchan(ic);
391		if (ostate != IEEE80211_S_RUN &&
392		    (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
393			/*
394			 * Start s/w beacon miss timer for devices w/o
395			 * hardware support.  We fudge a bit here since
396			 * we're doing this in software.
397			 */
398			vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
399				2 * vap->iv_bmissthreshold * ni->ni_intval);
400			vap->iv_swbmiss_count = 0;
401			callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
402				ieee80211_swbmiss, vap);
403		}
404		/*
405		 * When 802.1x is not in use mark the port authorized
406		 * at this point so traffic can flow.
407		 */
408		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
409			ieee80211_node_authorize(ni);
410		/*
411		 * Fake association when joining an existing bss.
412		 */
413		if (ic->ic_newassoc != NULL)
414			ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN);
415		break;
416	case IEEE80211_S_SLEEP:
417		ieee80211_sta_pwrsave(vap, 0);
418		break;
419	default:
420	invalid:
421		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
422		    "%s: unexpected state transition %s -> %s\n", __func__,
423		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
424		break;
425	}
426	return 0;
427}
428
429/*
430 * Return non-zero if the frame is an echo of a multicast
431 * frame sent by ourself.  The dir is known to be DSTODS.
432 */
433static __inline int
434isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
435{
436#define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
437#define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
438	const uint8_t *sa;
439
440	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
441
442	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
443		return 0;
444	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
445	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
446#undef WH4
447#undef QWH4
448}
449
450/*
451 * Return non-zero if the frame is an echo of a multicast
452 * frame sent by ourself.  The dir is known to be FROMDS.
453 */
454static __inline int
455isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
456{
457	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
458
459	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
460		return 0;
461	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
462}
463
464/*
465 * Decide if a received management frame should be
466 * printed when debugging is enabled.  This filters some
467 * of the less interesting frames that come frequently
468 * (e.g. beacons).
469 */
470static __inline int
471doprint(struct ieee80211vap *vap, int subtype)
472{
473	switch (subtype) {
474	case IEEE80211_FC0_SUBTYPE_BEACON:
475		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
476	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
477		return 0;
478	}
479	return 1;
480}
481
482/*
483 * Process a received frame.  The node associated with the sender
484 * should be supplied.  If nothing was found in the node table then
485 * the caller is assumed to supply a reference to iv_bss instead.
486 * The RSSI and a timestamp are also supplied.  The RSSI data is used
487 * during AP scanning to select a AP to associate with; it can have
488 * any units so long as values have consistent units and higher values
489 * mean ``better signal''.  The receive timestamp is currently not used
490 * by the 802.11 layer.
491 */
492static int
493sta_input(struct ieee80211_node *ni, struct mbuf *m,
494	int rssi, int noise, uint32_t rstamp)
495{
496#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
497#define	HAS_SEQ(type)	((type & 0x4) == 0)
498	struct ieee80211vap *vap = ni->ni_vap;
499	struct ieee80211com *ic = ni->ni_ic;
500	struct ifnet *ifp = vap->iv_ifp;
501	struct ieee80211_frame *wh;
502	struct ieee80211_key *key;
503	struct ether_header *eh;
504	int hdrspace, need_tap;
505	uint8_t dir, type, subtype, qos;
506	uint8_t *bssid;
507	uint16_t rxseq;
508
509	if (m->m_flags & M_AMPDU_MPDU) {
510		/*
511		 * Fastpath for A-MPDU reorder q resubmission.  Frames
512		 * w/ M_AMPDU_MPDU marked have already passed through
513		 * here but were received out of order and been held on
514		 * the reorder queue.  When resubmitted they are marked
515		 * with the M_AMPDU_MPDU flag and we can bypass most of
516		 * the normal processing.
517		 */
518		wh = mtod(m, struct ieee80211_frame *);
519		type = IEEE80211_FC0_TYPE_DATA;
520		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
521		subtype = IEEE80211_FC0_SUBTYPE_QOS;
522		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
523		goto resubmit_ampdu;
524	}
525
526	KASSERT(ni != NULL, ("null node"));
527	ni->ni_inact = ni->ni_inact_reload;
528
529	need_tap = 1;			/* mbuf need to be tapped. */
530	type = -1;			/* undefined */
531
532	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
533		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
534		    ni->ni_macaddr, NULL,
535		    "too short (1): len %u", m->m_pkthdr.len);
536		vap->iv_stats.is_rx_tooshort++;
537		goto out;
538	}
539	/*
540	 * Bit of a cheat here, we use a pointer for a 3-address
541	 * frame format but don't reference fields past outside
542	 * ieee80211_frame_min w/o first validating the data is
543	 * present.
544	 */
545	wh = mtod(m, struct ieee80211_frame *);
546
547	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
548	    IEEE80211_FC0_VERSION_0) {
549		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
550		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
551		    wh->i_fc[0], wh->i_fc[1]);
552		vap->iv_stats.is_rx_badversion++;
553		goto err;
554	}
555
556	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
557	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
558	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
559	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
560		bssid = wh->i_addr2;
561		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
562			/* not interested in */
563			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
564			    bssid, NULL, "%s", "not to bss");
565			vap->iv_stats.is_rx_wrongbss++;
566			goto out;
567		}
568		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
569		ni->ni_noise = noise;
570		ni->ni_rstamp = rstamp;
571		if (HAS_SEQ(type)) {
572			uint8_t tid = ieee80211_gettid(wh);
573			if (IEEE80211_QOS_HAS_SEQ(wh) &&
574			    TID_TO_WME_AC(tid) >= WME_AC_VI)
575				ic->ic_wme.wme_hipri_traffic++;
576			rxseq = le16toh(*(uint16_t *)wh->i_seq);
577			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
578			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
579			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
580				/* duplicate, discard */
581				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
582				    bssid, "duplicate",
583				    "seqno <%u,%u> fragno <%u,%u> tid %u",
584				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
585				    ni->ni_rxseqs[tid] >>
586					IEEE80211_SEQ_SEQ_SHIFT,
587				    rxseq & IEEE80211_SEQ_FRAG_MASK,
588				    ni->ni_rxseqs[tid] &
589					IEEE80211_SEQ_FRAG_MASK,
590				    tid);
591				vap->iv_stats.is_rx_dup++;
592				IEEE80211_NODE_STAT(ni, rx_dup);
593				goto out;
594			}
595			ni->ni_rxseqs[tid] = rxseq;
596		}
597	}
598
599	switch (type) {
600	case IEEE80211_FC0_TYPE_DATA:
601		hdrspace = ieee80211_hdrspace(ic, wh);
602		if (m->m_len < hdrspace &&
603		    (m = m_pullup(m, hdrspace)) == NULL) {
604			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
605			    ni->ni_macaddr, NULL,
606			    "data too short: expecting %u", hdrspace);
607			vap->iv_stats.is_rx_tooshort++;
608			goto out;		/* XXX */
609		}
610		/*
611		 * Handle A-MPDU re-ordering.  If the frame is to be
612		 * processed directly then ieee80211_ampdu_reorder
613		 * will return 0; otherwise it has consumed the mbuf
614		 * and we should do nothing more with it.
615		 */
616		if ((m->m_flags & M_AMPDU) &&
617		    (dir == IEEE80211_FC1_DIR_FROMDS ||
618		     dir == IEEE80211_FC1_DIR_DSTODS) &&
619		    ieee80211_ampdu_reorder(ni, m) != 0) {
620			m = NULL;
621			goto out;
622		}
623	resubmit_ampdu:
624		if (dir == IEEE80211_FC1_DIR_FROMDS) {
625			if ((ifp->if_flags & IFF_SIMPLEX) &&
626			    isfromds_mcastecho(vap, wh)) {
627				/*
628				 * In IEEE802.11 network, multicast
629				 * packets sent from "me" are broadcast
630				 * from the AP; silently discard for
631				 * SIMPLEX interface.
632				 */
633				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
634				    wh, "data", "%s", "multicast echo");
635				vap->iv_stats.is_rx_mcastecho++;
636				goto out;
637			}
638			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
639			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
640				/*
641				 * DWDS sta's must drop 3-address mcast frames
642				 * as they will be sent separately as a 4-addr
643				 * frame.  Accepting the 3-addr frame will
644				 * confuse the bridge into thinking the sending
645				 * sta is located at the end of WDS link.
646				 */
647				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
648				    "3-address data", "%s", "DWDS enabled");
649				vap->iv_stats.is_rx_mcastecho++;
650				goto out;
651			}
652		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
653			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
654				IEEE80211_DISCARD(vap,
655				    IEEE80211_MSG_INPUT, wh, "4-address data",
656				    "%s", "DWDS not enabled");
657				vap->iv_stats.is_rx_wrongdir++;
658				goto out;
659			}
660			if ((ifp->if_flags & IFF_SIMPLEX) &&
661			    isdstods_mcastecho(vap, wh)) {
662				/*
663				 * In IEEE802.11 network, multicast
664				 * packets sent from "me" are broadcast
665				 * from the AP; silently discard for
666				 * SIMPLEX interface.
667				 */
668				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
669				    "4-address data", "%s", "multicast echo");
670				vap->iv_stats.is_rx_mcastecho++;
671				goto out;
672			}
673		} else {
674			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
675			    "data", "incorrect dir 0x%x", dir);
676			vap->iv_stats.is_rx_wrongdir++;
677			goto out;
678		}
679
680		/*
681		 * Handle privacy requirements.  Note that we
682		 * must not be preempted from here until after
683		 * we (potentially) call ieee80211_crypto_demic;
684		 * otherwise we may violate assumptions in the
685		 * crypto cipher modules used to do delayed update
686		 * of replay sequence numbers.
687		 */
688		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
689			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
690				/*
691				 * Discard encrypted frames when privacy is off.
692				 */
693				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
694				    wh, "WEP", "%s", "PRIVACY off");
695				vap->iv_stats.is_rx_noprivacy++;
696				IEEE80211_NODE_STAT(ni, rx_noprivacy);
697				goto out;
698			}
699			key = ieee80211_crypto_decap(ni, m, hdrspace);
700			if (key == NULL) {
701				/* NB: stats+msgs handled in crypto_decap */
702				IEEE80211_NODE_STAT(ni, rx_wepfail);
703				goto out;
704			}
705			wh = mtod(m, struct ieee80211_frame *);
706			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
707		} else {
708			/* XXX M_WEP and IEEE80211_F_PRIVACY */
709			key = NULL;
710		}
711
712		/*
713		 * Save QoS bits for use below--before we strip the header.
714		 */
715		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
716			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
717			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
718			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
719		} else
720			qos = 0;
721
722		/*
723		 * Next up, any fragmentation.
724		 */
725		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
726			m = ieee80211_defrag(ni, m, hdrspace);
727			if (m == NULL) {
728				/* Fragment dropped or frame not complete yet */
729				goto out;
730			}
731		}
732		wh = NULL;		/* no longer valid, catch any uses */
733
734		/*
735		 * Next strip any MSDU crypto bits.
736		 */
737		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
738			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
739			    ni->ni_macaddr, "data", "%s", "demic error");
740			vap->iv_stats.is_rx_demicfail++;
741			IEEE80211_NODE_STAT(ni, rx_demicfail);
742			goto out;
743		}
744
745		/* copy to listener after decrypt */
746		if (bpf_peers_present(vap->iv_rawbpf))
747			bpf_mtap(vap->iv_rawbpf, m);
748		need_tap = 0;
749
750		/*
751		 * Finally, strip the 802.11 header.
752		 */
753		m = ieee80211_decap(vap, m, hdrspace);
754		if (m == NULL) {
755			/* XXX mask bit to check for both */
756			/* don't count Null data frames as errors */
757			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
758			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
759				goto out;
760			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
761			    ni->ni_macaddr, "data", "%s", "decap error");
762			vap->iv_stats.is_rx_decap++;
763			IEEE80211_NODE_STAT(ni, rx_decap);
764			goto err;
765		}
766		eh = mtod(m, struct ether_header *);
767		if (!ieee80211_node_is_authorized(ni)) {
768			/*
769			 * Deny any non-PAE frames received prior to
770			 * authorization.  For open/shared-key
771			 * authentication the port is mark authorized
772			 * after authentication completes.  For 802.1x
773			 * the port is not marked authorized by the
774			 * authenticator until the handshake has completed.
775			 */
776			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
777				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
778				    eh->ether_shost, "data",
779				    "unauthorized port: ether type 0x%x len %u",
780				    eh->ether_type, m->m_pkthdr.len);
781				vap->iv_stats.is_rx_unauth++;
782				IEEE80211_NODE_STAT(ni, rx_unauth);
783				goto err;
784			}
785		} else {
786			/*
787			 * When denying unencrypted frames, discard
788			 * any non-PAE frames received without encryption.
789			 */
790			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
791			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
792			    eh->ether_type != htons(ETHERTYPE_PAE)) {
793				/*
794				 * Drop unencrypted frames.
795				 */
796				vap->iv_stats.is_rx_unencrypted++;
797				IEEE80211_NODE_STAT(ni, rx_unencrypted);
798				goto out;
799			}
800		}
801		/* XXX require HT? */
802		if (qos & IEEE80211_QOS_AMSDU) {
803			m = ieee80211_decap_amsdu(ni, m);
804			if (m == NULL)
805				return IEEE80211_FC0_TYPE_DATA;
806		} else {
807#ifdef IEEE80211_SUPPORT_SUPERG
808			m = ieee80211_decap_fastframe(vap, ni, m);
809			if (m == NULL)
810				return IEEE80211_FC0_TYPE_DATA;
811#endif
812		}
813		ieee80211_deliver_data(vap, ni, m);
814		return IEEE80211_FC0_TYPE_DATA;
815
816	case IEEE80211_FC0_TYPE_MGT:
817		vap->iv_stats.is_rx_mgmt++;
818		IEEE80211_NODE_STAT(ni, rx_mgmt);
819		if (dir != IEEE80211_FC1_DIR_NODS) {
820			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
821			    wh, "data", "incorrect dir 0x%x", dir);
822			vap->iv_stats.is_rx_wrongdir++;
823			goto err;
824		}
825		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
826			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
827			    ni->ni_macaddr, "mgt", "too short: len %u",
828			    m->m_pkthdr.len);
829			vap->iv_stats.is_rx_tooshort++;
830			goto out;
831		}
832#ifdef IEEE80211_DEBUG
833		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
834		    ieee80211_msg_dumppkts(vap)) {
835			if_printf(ifp, "received %s from %s rssi %d\n",
836			    ieee80211_mgt_subtype_name[subtype >>
837				IEEE80211_FC0_SUBTYPE_SHIFT],
838			    ether_sprintf(wh->i_addr2), rssi);
839		}
840#endif
841		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
842			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
843				/*
844				 * Only shared key auth frames with a challenge
845				 * should be encrypted, discard all others.
846				 */
847				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
848				    wh, ieee80211_mgt_subtype_name[subtype >>
849					IEEE80211_FC0_SUBTYPE_SHIFT],
850				    "%s", "WEP set but not permitted");
851				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
852				goto out;
853			}
854			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
855				/*
856				 * Discard encrypted frames when privacy is off.
857				 */
858				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
859				    wh, "mgt", "%s", "WEP set but PRIVACY off");
860				vap->iv_stats.is_rx_noprivacy++;
861				goto out;
862			}
863			hdrspace = ieee80211_hdrspace(ic, wh);
864			key = ieee80211_crypto_decap(ni, m, hdrspace);
865			if (key == NULL) {
866				/* NB: stats+msgs handled in crypto_decap */
867				goto out;
868			}
869			wh = mtod(m, struct ieee80211_frame *);
870			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
871		}
872		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
873		goto out;
874
875	case IEEE80211_FC0_TYPE_CTL:
876		vap->iv_stats.is_rx_ctl++;
877		IEEE80211_NODE_STAT(ni, rx_ctrl);
878		vap->iv_recv_ctl(ni, m, subtype);
879		goto out;
880	default:
881		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
882		    wh, NULL, "bad frame type 0x%x", type);
883		/* should not come here */
884		break;
885	}
886err:
887	ifp->if_ierrors++;
888out:
889	if (m != NULL) {
890		if (need_tap && bpf_peers_present(vap->iv_rawbpf))
891			bpf_mtap(vap->iv_rawbpf, m);
892		m_freem(m);
893	}
894	return type;
895#undef SEQ_LEQ
896}
897
898static void
899sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
900    int rssi, int noise, uint32_t rstamp, uint16_t seq, uint16_t status)
901{
902	struct ieee80211vap *vap = ni->ni_vap;
903
904	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
905		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
906		    ni->ni_macaddr, "open auth",
907		    "bad sta auth mode %u", ni->ni_authmode);
908		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
909		return;
910	}
911	if (vap->iv_state != IEEE80211_S_AUTH ||
912	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
913		vap->iv_stats.is_rx_bad_auth++;
914		return;
915	}
916	if (status != 0) {
917		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
918		    ni, "open auth failed (reason %d)", status);
919		vap->iv_stats.is_rx_auth_fail++;
920		vap->iv_stats.is_rx_authfail_code = status;
921		ieee80211_new_state(vap, IEEE80211_S_SCAN,
922		    IEEE80211_SCAN_FAIL_STATUS);
923	} else
924		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
925}
926
927static void
928sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
929    uint8_t *frm, uint8_t *efrm, int rssi, int noise, uint32_t rstamp,
930    uint16_t seq, uint16_t status)
931{
932	struct ieee80211vap *vap = ni->ni_vap;
933	uint8_t *challenge;
934	int estatus;
935
936	/*
937	 * NB: this can happen as we allow pre-shared key
938	 * authentication to be enabled w/o wep being turned
939	 * on so that configuration of these can be done
940	 * in any order.  It may be better to enforce the
941	 * ordering in which case this check would just be
942	 * for sanity/consistency.
943	 */
944	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
945		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
946		    ni->ni_macaddr, "shared key auth",
947		    "%s", " PRIVACY is disabled");
948		estatus = IEEE80211_STATUS_ALG;
949		goto bad;
950	}
951	/*
952	 * Pre-shared key authentication is evil; accept
953	 * it only if explicitly configured (it is supported
954	 * mainly for compatibility with clients like OS X).
955	 */
956	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
957	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
958		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
959		    ni->ni_macaddr, "shared key auth",
960		    "bad sta auth mode %u", ni->ni_authmode);
961		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
962		estatus = IEEE80211_STATUS_ALG;
963		goto bad;
964	}
965
966	challenge = NULL;
967	if (frm + 1 < efrm) {
968		if ((frm[1] + 2) > (efrm - frm)) {
969			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
970			    ni->ni_macaddr, "shared key auth",
971			    "ie %d/%d too long",
972			    frm[0], (frm[1] + 2) - (efrm - frm));
973			vap->iv_stats.is_rx_bad_auth++;
974			estatus = IEEE80211_STATUS_CHALLENGE;
975			goto bad;
976		}
977		if (*frm == IEEE80211_ELEMID_CHALLENGE)
978			challenge = frm;
979		frm += frm[1] + 2;
980	}
981	switch (seq) {
982	case IEEE80211_AUTH_SHARED_CHALLENGE:
983	case IEEE80211_AUTH_SHARED_RESPONSE:
984		if (challenge == NULL) {
985			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
986			    ni->ni_macaddr, "shared key auth",
987			    "%s", "no challenge");
988			vap->iv_stats.is_rx_bad_auth++;
989			estatus = IEEE80211_STATUS_CHALLENGE;
990			goto bad;
991		}
992		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
993			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
994			    ni->ni_macaddr, "shared key auth",
995			    "bad challenge len %d", challenge[1]);
996			vap->iv_stats.is_rx_bad_auth++;
997			estatus = IEEE80211_STATUS_CHALLENGE;
998			goto bad;
999		}
1000	default:
1001		break;
1002	}
1003	if (vap->iv_state != IEEE80211_S_AUTH)
1004		return;
1005	switch (seq) {
1006	case IEEE80211_AUTH_SHARED_PASS:
1007		if (ni->ni_challenge != NULL) {
1008			free(ni->ni_challenge, M_80211_NODE);
1009			ni->ni_challenge = NULL;
1010		}
1011		if (status != 0) {
1012			IEEE80211_NOTE_FRAME(vap,
1013			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1014			    "shared key auth failed (reason %d)", status);
1015			vap->iv_stats.is_rx_auth_fail++;
1016			vap->iv_stats.is_rx_authfail_code = status;
1017			return;
1018		}
1019		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1020		break;
1021	case IEEE80211_AUTH_SHARED_CHALLENGE:
1022		if (!ieee80211_alloc_challenge(ni))
1023			return;
1024		/* XXX could optimize by passing recvd challenge */
1025		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1026		IEEE80211_SEND_MGMT(ni,
1027			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1028		break;
1029	default:
1030		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1031		    wh, "shared key auth", "bad seq %d", seq);
1032		vap->iv_stats.is_rx_bad_auth++;
1033		return;
1034	}
1035	return;
1036bad:
1037	/*
1038	 * Kick the state machine.  This short-circuits
1039	 * using the mgt frame timeout to trigger the
1040	 * state transition.
1041	 */
1042	if (vap->iv_state == IEEE80211_S_AUTH)
1043		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1044		    IEEE80211_SCAN_FAIL_STATUS);
1045}
1046
1047static int
1048ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1049	const struct ieee80211_frame *wh)
1050{
1051#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1052	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1053	u_int len = frm[1], qosinfo;
1054	int i;
1055
1056	if (len < sizeof(struct ieee80211_wme_param)-2) {
1057		IEEE80211_DISCARD_IE(vap,
1058		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1059		    wh, "WME", "too short, len %u", len);
1060		return -1;
1061	}
1062	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1063	qosinfo &= WME_QOSINFO_COUNT;
1064	/* XXX do proper check for wraparound */
1065	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1066		return 0;
1067	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1068	for (i = 0; i < WME_NUM_AC; i++) {
1069		struct wmeParams *wmep =
1070			&wme->wme_wmeChanParams.cap_wmeParams[i];
1071		/* NB: ACI not used */
1072		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1073		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1074		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1075		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1076		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1077		frm += 4;
1078	}
1079	wme->wme_wmeChanParams.cap_info = qosinfo;
1080	return 1;
1081#undef MS
1082}
1083
1084/*
1085 * Return non-zero if a background scan may be continued:
1086 * o bg scan is active
1087 * o no channel switch is pending
1088 * o there has not been any traffic recently
1089 *
1090 * Note we do not check if there is an administrative enable;
1091 * this is only done to start the scan.  We assume that any
1092 * change in state will be accompanied by a request to cancel
1093 * active scans which will otherwise cause this test to fail.
1094 */
1095static __inline int
1096contbgscan(struct ieee80211vap *vap)
1097{
1098	struct ieee80211com *ic = vap->iv_ic;
1099
1100	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1101	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1102	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1103	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1104}
1105
1106/*
1107 * Return non-zero if a backgrond scan may be started:
1108 * o bg scanning is administratively enabled
1109 * o no channel switch is pending
1110 * o we are not boosted on a dynamic turbo channel
1111 * o there has not been a scan recently
1112 * o there has not been any traffic recently
1113 */
1114static __inline int
1115startbgscan(struct ieee80211vap *vap)
1116{
1117	struct ieee80211com *ic = vap->iv_ic;
1118
1119	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1120	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1121#ifdef IEEE80211_SUPPORT_SUPERG
1122	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1123#endif
1124	    time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1125	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1126}
1127
1128static void
1129sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1130	int subtype, int rssi, int noise, uint32_t rstamp)
1131{
1132#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1133#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1134	struct ieee80211vap *vap = ni->ni_vap;
1135	struct ieee80211com *ic = ni->ni_ic;
1136	struct ieee80211_frame *wh;
1137	uint8_t *frm, *efrm;
1138	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1139	uint8_t rate;
1140
1141	wh = mtod(m0, struct ieee80211_frame *);
1142	frm = (uint8_t *)&wh[1];
1143	efrm = mtod(m0, uint8_t *) + m0->m_len;
1144	switch (subtype) {
1145	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1146	case IEEE80211_FC0_SUBTYPE_BEACON: {
1147		struct ieee80211_scanparams scan;
1148		/*
1149		 * We process beacon/probe response frames:
1150		 *    o when scanning, or
1151		 *    o station mode when associated (to collect state
1152		 *      updates such as 802.11g slot time)
1153		 * Frames otherwise received are discarded.
1154		 */
1155		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1156			vap->iv_stats.is_rx_mgtdiscard++;
1157			return;
1158		}
1159		/* XXX probe response in sta mode when !scanning? */
1160		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1161			return;
1162		/*
1163		 * Count frame now that we know it's to be processed.
1164		 */
1165		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1166			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1167			IEEE80211_NODE_STAT(ni, rx_beacons);
1168		} else
1169			IEEE80211_NODE_STAT(ni, rx_proberesp);
1170		/*
1171		 * When operating in station mode, check for state updates.
1172		 * Be careful to ignore beacons received while doing a
1173		 * background scan.  We consider only 11g/WMM stuff right now.
1174		 */
1175		if (ni->ni_associd != 0 &&
1176		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1177		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1178			/* record tsf of last beacon */
1179			memcpy(ni->ni_tstamp.data, scan.tstamp,
1180				sizeof(ni->ni_tstamp));
1181			/* count beacon frame for s/w bmiss handling */
1182			vap->iv_swbmiss_count++;
1183			vap->iv_bmiss_count = 0;
1184			if (ni->ni_erp != scan.erp) {
1185				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1186				    wh->i_addr2,
1187				    "erp change: was 0x%x, now 0x%x",
1188				    ni->ni_erp, scan.erp);
1189				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1190				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1191					ic->ic_flags |= IEEE80211_F_USEPROT;
1192				else
1193					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1194				ni->ni_erp = scan.erp;
1195				/* XXX statistic */
1196				/* XXX driver notification */
1197			}
1198			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1199				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1200				    wh->i_addr2,
1201				    "capabilities change: was 0x%x, now 0x%x",
1202				    ni->ni_capinfo, scan.capinfo);
1203				/*
1204				 * NB: we assume short preamble doesn't
1205				 *     change dynamically
1206				 */
1207				ieee80211_set_shortslottime(ic,
1208					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1209					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1210				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1211					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1212				/* XXX statistic */
1213			}
1214			if (scan.wme != NULL &&
1215			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1216			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1217				ieee80211_wme_updateparams(vap);
1218#ifdef IEEE80211_SUPPORT_SUPERG
1219			if (scan.ath != NULL)
1220				ieee80211_parse_athparams(ni, scan.ath, wh);
1221#endif
1222			if (scan.htcap != NULL && scan.htinfo != NULL &&
1223			    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1224				ieee80211_ht_updateparams(ni,
1225				    scan.htcap, scan.htinfo);
1226				/* XXX state changes? */
1227			}
1228			if (scan.tim != NULL) {
1229				struct ieee80211_tim_ie *tim =
1230				    (struct ieee80211_tim_ie *) scan.tim;
1231#if 0
1232				int aid = IEEE80211_AID(ni->ni_associd);
1233				int ix = aid / NBBY;
1234				int min = tim->tim_bitctl &~ 1;
1235				int max = tim->tim_len + min - 4;
1236				if ((tim->tim_bitctl&1) ||
1237				    (min <= ix && ix <= max &&
1238				     isset(tim->tim_bitmap - min, aid))) {
1239					/*
1240					 * XXX Do not let bg scan kick off
1241					 * we are expecting data.
1242					 */
1243					ic->ic_lastdata = ticks;
1244					ieee80211_sta_pwrsave(vap, 0);
1245				}
1246#endif
1247				ni->ni_dtim_count = tim->tim_count;
1248				ni->ni_dtim_period = tim->tim_period;
1249			}
1250			/*
1251			 * If scanning, pass the info to the scan module.
1252			 * Otherwise, check if it's the right time to do
1253			 * a background scan.  Background scanning must
1254			 * be enabled and we must not be operating in the
1255			 * turbo phase of dynamic turbo mode.  Then,
1256			 * it's been a while since the last background
1257			 * scan and if no data frames have come through
1258			 * recently, kick off a scan.  Note that this
1259			 * is the mechanism by which a background scan
1260			 * is started _and_ continued each time we
1261			 * return on-channel to receive a beacon from
1262			 * our ap.
1263			 */
1264			if (ic->ic_flags & IEEE80211_F_SCAN) {
1265				ieee80211_add_scan(vap, &scan, wh,
1266					subtype, rssi, noise, rstamp);
1267			} else if (contbgscan(vap)) {
1268				ieee80211_bg_scan(vap, 0);
1269			} else if (startbgscan(vap)) {
1270				vap->iv_stats.is_scan_bg++;
1271#if 0
1272				/* wakeup if we are sleeing */
1273				ieee80211_set_pwrsave(vap, 0);
1274#endif
1275				ieee80211_bg_scan(vap, 0);
1276			}
1277			return;
1278		}
1279		/*
1280		 * If scanning, just pass information to the scan module.
1281		 */
1282		if (ic->ic_flags & IEEE80211_F_SCAN) {
1283			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1284				/*
1285				 * Actively scanning a channel marked passive;
1286				 * send a probe request now that we know there
1287				 * is 802.11 traffic present.
1288				 *
1289				 * XXX check if the beacon we recv'd gives
1290				 * us what we need and suppress the probe req
1291				 */
1292				ieee80211_probe_curchan(vap, 1);
1293				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1294			}
1295			ieee80211_add_scan(vap, &scan, wh,
1296				subtype, rssi, noise, rstamp);
1297			return;
1298		}
1299		break;
1300	}
1301
1302	case IEEE80211_FC0_SUBTYPE_AUTH: {
1303		uint16_t algo, seq, status;
1304		/*
1305		 * auth frame format
1306		 *	[2] algorithm
1307		 *	[2] sequence
1308		 *	[2] status
1309		 *	[tlv*] challenge
1310		 */
1311		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1312		algo   = le16toh(*(uint16_t *)frm);
1313		seq    = le16toh(*(uint16_t *)(frm + 2));
1314		status = le16toh(*(uint16_t *)(frm + 4));
1315		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1316		    "recv auth frame with algorithm %d seq %d", algo, seq);
1317
1318		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1319			IEEE80211_DISCARD(vap,
1320			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1321			    wh, "auth", "%s", "TKIP countermeasures enabled");
1322			vap->iv_stats.is_rx_auth_countermeasures++;
1323			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1324				ieee80211_send_error(ni, wh->i_addr2,
1325					IEEE80211_FC0_SUBTYPE_AUTH,
1326					IEEE80211_REASON_MIC_FAILURE);
1327			}
1328			return;
1329		}
1330		if (algo == IEEE80211_AUTH_ALG_SHARED)
1331			sta_auth_shared(ni, wh, frm + 6, efrm, rssi,
1332			    noise, rstamp, seq, status);
1333		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1334			sta_auth_open(ni, wh, rssi, noise, rstamp,
1335			    seq, status);
1336		else {
1337			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1338			    wh, "auth", "unsupported alg %d", algo);
1339			vap->iv_stats.is_rx_auth_unsupported++;
1340			return;
1341		}
1342		break;
1343	}
1344
1345	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1346	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1347		uint16_t capinfo, associd;
1348		uint16_t status;
1349
1350		if (vap->iv_state != IEEE80211_S_ASSOC) {
1351			vap->iv_stats.is_rx_mgtdiscard++;
1352			return;
1353		}
1354
1355		/*
1356		 * asresp frame format
1357		 *	[2] capability information
1358		 *	[2] status
1359		 *	[2] association ID
1360		 *	[tlv] supported rates
1361		 *	[tlv] extended supported rates
1362		 *	[tlv] WME
1363		 *	[tlv] HT capabilities
1364		 *	[tlv] HT info
1365		 */
1366		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1367		ni = vap->iv_bss;
1368		capinfo = le16toh(*(uint16_t *)frm);
1369		frm += 2;
1370		status = le16toh(*(uint16_t *)frm);
1371		frm += 2;
1372		if (status != 0) {
1373			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1374			    wh->i_addr2, "%sassoc failed (reason %d)",
1375			    ISREASSOC(subtype) ?  "re" : "", status);
1376			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1377			return;
1378		}
1379		associd = le16toh(*(uint16_t *)frm);
1380		frm += 2;
1381
1382		rates = xrates = wme = htcap = htinfo = NULL;
1383		while (efrm - frm > 1) {
1384			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1385			switch (*frm) {
1386			case IEEE80211_ELEMID_RATES:
1387				rates = frm;
1388				break;
1389			case IEEE80211_ELEMID_XRATES:
1390				xrates = frm;
1391				break;
1392			case IEEE80211_ELEMID_HTCAP:
1393				htcap = frm;
1394				break;
1395			case IEEE80211_ELEMID_HTINFO:
1396				htinfo = frm;
1397				break;
1398			case IEEE80211_ELEMID_VENDOR:
1399				if (iswmeoui(frm))
1400					wme = frm;
1401				else if (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
1402					/*
1403					 * Accept pre-draft HT ie's if the
1404					 * standard ones have not been seen.
1405					 */
1406					if (ishtcapoui(frm)) {
1407						if (htcap == NULL)
1408							htcap = frm;
1409					} else if (ishtinfooui(frm)) {
1410						if (htinfo == NULL)
1411							htcap = frm;
1412					}
1413				}
1414				/* XXX Atheros OUI support */
1415				break;
1416			}
1417			frm += frm[1] + 2;
1418		}
1419
1420		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1421		if (xrates != NULL)
1422			IEEE80211_VERIFY_ELEMENT(xrates,
1423				IEEE80211_RATE_MAXSIZE - rates[1], return);
1424		rate = ieee80211_setup_rates(ni, rates, xrates,
1425				IEEE80211_F_JOIN |
1426				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1427				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1428		if (rate & IEEE80211_RATE_BASIC) {
1429			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1430			    wh->i_addr2,
1431			    "%sassoc failed (rate set mismatch)",
1432			    ISREASSOC(subtype) ?  "re" : "");
1433			vap->iv_stats.is_rx_assoc_norate++;
1434			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1435			    IEEE80211_SCAN_FAIL_STATUS);
1436			return;
1437		}
1438
1439		ni->ni_capinfo = capinfo;
1440		ni->ni_associd = associd;
1441		if (ni->ni_jointime == 0)
1442			ni->ni_jointime = time_uptime;
1443		if (wme != NULL &&
1444		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1445			ni->ni_flags |= IEEE80211_NODE_QOS;
1446			ieee80211_wme_updateparams(vap);
1447		} else
1448			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1449		/*
1450		 * Setup HT state according to the negotiation.
1451		 *
1452		 * NB: shouldn't need to check if HT use is enabled but some
1453		 *     ap's send back HT ie's even when we don't indicate we
1454		 *     are HT capable in our AssocReq.
1455		 */
1456		if (htcap != NULL && htinfo != NULL &&
1457		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1458			ieee80211_ht_node_init(ni);
1459			ieee80211_ht_updateparams(ni, htcap, htinfo);
1460			ieee80211_setup_htrates(ni, htcap,
1461			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1462			ieee80211_setup_basic_htrates(ni, htinfo);
1463		} else {
1464#ifdef IEEE80211_SUPPORT_SUPERG
1465			if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1466				ieee80211_ff_node_init(ni);
1467#endif
1468		}
1469		/*
1470		 * Configure state now that we are associated.
1471		 *
1472		 * XXX may need different/additional driver callbacks?
1473		 */
1474		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1475		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1476			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1477			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1478		} else {
1479			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1480			ic->ic_flags |= IEEE80211_F_USEBARKER;
1481		}
1482		ieee80211_set_shortslottime(ic,
1483			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1484			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1485		/*
1486		 * Honor ERP protection.
1487		 *
1488		 * NB: ni_erp should zero for non-11g operation.
1489		 */
1490		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1491		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1492			ic->ic_flags |= IEEE80211_F_USEPROT;
1493		else
1494			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1495		IEEE80211_NOTE_MAC(vap,
1496		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1497		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1498		    ISREASSOC(subtype) ? "re" : "",
1499		    IEEE80211_NODE_AID(ni),
1500		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1501		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1502		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1503		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1504		    ni->ni_flags & IEEE80211_NODE_HT ?
1505			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1506		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1507		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1508			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1509		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1510		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1511			", fast-frames" : "",
1512		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1513			", turbo" : ""
1514		);
1515		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1516		break;
1517	}
1518
1519	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1520		uint16_t reason;
1521
1522		if (vap->iv_state == IEEE80211_S_SCAN) {
1523			vap->iv_stats.is_rx_mgtdiscard++;
1524			return;
1525		}
1526		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1527			/* NB: can happen when in promiscuous mode */
1528			vap->iv_stats.is_rx_mgtdiscard++;
1529			break;
1530		}
1531
1532		/*
1533		 * deauth frame format
1534		 *	[2] reason
1535		 */
1536		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1537		reason = le16toh(*(uint16_t *)frm);
1538
1539		vap->iv_stats.is_rx_deauth++;
1540		vap->iv_stats.is_rx_deauth_code = reason;
1541		IEEE80211_NODE_STAT(ni, rx_deauth);
1542
1543		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1544		    "recv deauthenticate (reason %d)", reason);
1545		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1546		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1547		break;
1548	}
1549
1550	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1551		uint16_t reason;
1552
1553		if (vap->iv_state != IEEE80211_S_RUN &&
1554		    vap->iv_state != IEEE80211_S_ASSOC &&
1555		    vap->iv_state != IEEE80211_S_AUTH) {
1556			vap->iv_stats.is_rx_mgtdiscard++;
1557			return;
1558		}
1559		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1560			/* NB: can happen when in promiscuous mode */
1561			vap->iv_stats.is_rx_mgtdiscard++;
1562			break;
1563		}
1564
1565		/*
1566		 * disassoc frame format
1567		 *	[2] reason
1568		 */
1569		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1570		reason = le16toh(*(uint16_t *)frm);
1571
1572		vap->iv_stats.is_rx_disassoc++;
1573		vap->iv_stats.is_rx_disassoc_code = reason;
1574		IEEE80211_NODE_STAT(ni, rx_disassoc);
1575
1576		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1577		    "recv disassociate (reason %d)", reason);
1578		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1579		break;
1580	}
1581
1582	case IEEE80211_FC0_SUBTYPE_ACTION:
1583		if (vap->iv_state == IEEE80211_S_RUN) {
1584			if (ieee80211_parse_action(ni, m0) == 0)
1585				ic->ic_recv_action(ni, frm, efrm);
1586		} else
1587			vap->iv_stats.is_rx_mgtdiscard++;
1588		break;
1589
1590	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1591	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1592	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1593		vap->iv_stats.is_rx_mgtdiscard++;
1594		return;
1595	default:
1596		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1597		     wh, "mgt", "subtype 0x%x not handled", subtype);
1598		vap->iv_stats.is_rx_badsubtype++;
1599		break;
1600	}
1601#undef ISREASSOC
1602#undef ISPROBE
1603}
1604
1605static void
1606sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)
1607{
1608}
1609