ieee80211_sta.c revision 191546
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 191546 2009-04-26 21:46:04Z 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 %x", wh->i_fc[0]);
551		vap->iv_stats.is_rx_badversion++;
552		goto err;
553	}
554
555	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
556	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
557	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
558	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
559		bssid = wh->i_addr2;
560		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
561			/* not interested in */
562			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
563			    bssid, NULL, "%s", "not to bss");
564			vap->iv_stats.is_rx_wrongbss++;
565			goto out;
566		}
567		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
568		ni->ni_noise = noise;
569		ni->ni_rstamp = rstamp;
570		if (HAS_SEQ(type)) {
571			uint8_t tid = ieee80211_gettid(wh);
572			if (IEEE80211_QOS_HAS_SEQ(wh) &&
573			    TID_TO_WME_AC(tid) >= WME_AC_VI)
574				ic->ic_wme.wme_hipri_traffic++;
575			rxseq = le16toh(*(uint16_t *)wh->i_seq);
576			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
577			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
578			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
579				/* duplicate, discard */
580				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
581				    bssid, "duplicate",
582				    "seqno <%u,%u> fragno <%u,%u> tid %u",
583				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
584				    ni->ni_rxseqs[tid] >>
585					IEEE80211_SEQ_SEQ_SHIFT,
586				    rxseq & IEEE80211_SEQ_FRAG_MASK,
587				    ni->ni_rxseqs[tid] &
588					IEEE80211_SEQ_FRAG_MASK,
589				    tid);
590				vap->iv_stats.is_rx_dup++;
591				IEEE80211_NODE_STAT(ni, rx_dup);
592				goto out;
593			}
594			ni->ni_rxseqs[tid] = rxseq;
595		}
596	}
597
598	switch (type) {
599	case IEEE80211_FC0_TYPE_DATA:
600		hdrspace = ieee80211_hdrspace(ic, wh);
601		if (m->m_len < hdrspace &&
602		    (m = m_pullup(m, hdrspace)) == NULL) {
603			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
604			    ni->ni_macaddr, NULL,
605			    "data too short: expecting %u", hdrspace);
606			vap->iv_stats.is_rx_tooshort++;
607			goto out;		/* XXX */
608		}
609		/*
610		 * Handle A-MPDU re-ordering.  If the frame is to be
611		 * processed directly then ieee80211_ampdu_reorder
612		 * will return 0; otherwise it has consumed the mbuf
613		 * and we should do nothing more with it.
614		 */
615		if ((m->m_flags & M_AMPDU) &&
616		    (dir == IEEE80211_FC1_DIR_FROMDS ||
617		     dir == IEEE80211_FC1_DIR_DSTODS) &&
618		    ieee80211_ampdu_reorder(ni, m) != 0) {
619			m = NULL;
620			goto out;
621		}
622	resubmit_ampdu:
623		if (dir == IEEE80211_FC1_DIR_FROMDS) {
624			if ((ifp->if_flags & IFF_SIMPLEX) &&
625			    isfromds_mcastecho(vap, wh)) {
626				/*
627				 * In IEEE802.11 network, multicast
628				 * packets sent from "me" are broadcast
629				 * from the AP; silently discard for
630				 * SIMPLEX interface.
631				 */
632				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
633				    wh, "data", "%s", "multicast echo");
634				vap->iv_stats.is_rx_mcastecho++;
635				goto out;
636			}
637			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
638			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
639				/*
640				 * DWDS sta's must drop 3-address mcast frames
641				 * as they will be sent separately as a 4-addr
642				 * frame.  Accepting the 3-addr frame will
643				 * confuse the bridge into thinking the sending
644				 * sta is located at the end of WDS link.
645				 */
646				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
647				    "3-address data", "%s", "DWDS enabled");
648				vap->iv_stats.is_rx_mcastecho++;
649				goto out;
650			}
651		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
652			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
653				IEEE80211_DISCARD(vap,
654				    IEEE80211_MSG_INPUT, wh, "4-address data",
655				    "%s", "DWDS not enabled");
656				vap->iv_stats.is_rx_wrongdir++;
657				goto out;
658			}
659			if ((ifp->if_flags & IFF_SIMPLEX) &&
660			    isdstods_mcastecho(vap, wh)) {
661				/*
662				 * In IEEE802.11 network, multicast
663				 * packets sent from "me" are broadcast
664				 * from the AP; silently discard for
665				 * SIMPLEX interface.
666				 */
667				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
668				    "4-address data", "%s", "multicast echo");
669				vap->iv_stats.is_rx_mcastecho++;
670				goto out;
671			}
672		} else {
673			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
674			    "data", "incorrect dir 0x%x", dir);
675			vap->iv_stats.is_rx_wrongdir++;
676			goto out;
677		}
678
679		/*
680		 * Handle privacy requirements.  Note that we
681		 * must not be preempted from here until after
682		 * we (potentially) call ieee80211_crypto_demic;
683		 * otherwise we may violate assumptions in the
684		 * crypto cipher modules used to do delayed update
685		 * of replay sequence numbers.
686		 */
687		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
688			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
689				/*
690				 * Discard encrypted frames when privacy is off.
691				 */
692				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
693				    wh, "WEP", "%s", "PRIVACY off");
694				vap->iv_stats.is_rx_noprivacy++;
695				IEEE80211_NODE_STAT(ni, rx_noprivacy);
696				goto out;
697			}
698			key = ieee80211_crypto_decap(ni, m, hdrspace);
699			if (key == NULL) {
700				/* NB: stats+msgs handled in crypto_decap */
701				IEEE80211_NODE_STAT(ni, rx_wepfail);
702				goto out;
703			}
704			wh = mtod(m, struct ieee80211_frame *);
705			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
706		} else {
707			/* XXX M_WEP and IEEE80211_F_PRIVACY */
708			key = NULL;
709		}
710
711		/*
712		 * Save QoS bits for use below--before we strip the header.
713		 */
714		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
715			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
716			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
717			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
718		} else
719			qos = 0;
720
721		/*
722		 * Next up, any fragmentation.
723		 */
724		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
725			m = ieee80211_defrag(ni, m, hdrspace);
726			if (m == NULL) {
727				/* Fragment dropped or frame not complete yet */
728				goto out;
729			}
730		}
731		wh = NULL;		/* no longer valid, catch any uses */
732
733		/*
734		 * Next strip any MSDU crypto bits.
735		 */
736		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
737			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
738			    ni->ni_macaddr, "data", "%s", "demic error");
739			vap->iv_stats.is_rx_demicfail++;
740			IEEE80211_NODE_STAT(ni, rx_demicfail);
741			goto out;
742		}
743
744		/* copy to listener after decrypt */
745		if (bpf_peers_present(vap->iv_rawbpf))
746			bpf_mtap(vap->iv_rawbpf, m);
747		need_tap = 0;
748
749		/*
750		 * Finally, strip the 802.11 header.
751		 */
752		m = ieee80211_decap(vap, m, hdrspace);
753		if (m == NULL) {
754			/* XXX mask bit to check for both */
755			/* don't count Null data frames as errors */
756			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
757			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
758				goto out;
759			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
760			    ni->ni_macaddr, "data", "%s", "decap error");
761			vap->iv_stats.is_rx_decap++;
762			IEEE80211_NODE_STAT(ni, rx_decap);
763			goto err;
764		}
765		eh = mtod(m, struct ether_header *);
766		if (!ieee80211_node_is_authorized(ni)) {
767			/*
768			 * Deny any non-PAE frames received prior to
769			 * authorization.  For open/shared-key
770			 * authentication the port is mark authorized
771			 * after authentication completes.  For 802.1x
772			 * the port is not marked authorized by the
773			 * authenticator until the handshake has completed.
774			 */
775			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
776				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
777				    eh->ether_shost, "data",
778				    "unauthorized port: ether type 0x%x len %u",
779				    eh->ether_type, m->m_pkthdr.len);
780				vap->iv_stats.is_rx_unauth++;
781				IEEE80211_NODE_STAT(ni, rx_unauth);
782				goto err;
783			}
784		} else {
785			/*
786			 * When denying unencrypted frames, discard
787			 * any non-PAE frames received without encryption.
788			 */
789			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
790			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
791			    eh->ether_type != htons(ETHERTYPE_PAE)) {
792				/*
793				 * Drop unencrypted frames.
794				 */
795				vap->iv_stats.is_rx_unencrypted++;
796				IEEE80211_NODE_STAT(ni, rx_unencrypted);
797				goto out;
798			}
799		}
800		/* XXX require HT? */
801		if (qos & IEEE80211_QOS_AMSDU) {
802			m = ieee80211_decap_amsdu(ni, m);
803			if (m == NULL)
804				return IEEE80211_FC0_TYPE_DATA;
805		} else {
806#ifdef IEEE80211_SUPPORT_SUPERG
807			m = ieee80211_decap_fastframe(vap, ni, m);
808			if (m == NULL)
809				return IEEE80211_FC0_TYPE_DATA;
810#endif
811		}
812		ieee80211_deliver_data(vap, ni, m);
813		return IEEE80211_FC0_TYPE_DATA;
814
815	case IEEE80211_FC0_TYPE_MGT:
816		vap->iv_stats.is_rx_mgmt++;
817		IEEE80211_NODE_STAT(ni, rx_mgmt);
818		if (dir != IEEE80211_FC1_DIR_NODS) {
819			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
820			    wh, "data", "incorrect dir 0x%x", dir);
821			vap->iv_stats.is_rx_wrongdir++;
822			goto err;
823		}
824		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
825			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
826			    ni->ni_macaddr, "mgt", "too short: len %u",
827			    m->m_pkthdr.len);
828			vap->iv_stats.is_rx_tooshort++;
829			goto out;
830		}
831#ifdef IEEE80211_DEBUG
832		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
833		    ieee80211_msg_dumppkts(vap)) {
834			if_printf(ifp, "received %s from %s rssi %d\n",
835			    ieee80211_mgt_subtype_name[subtype >>
836				IEEE80211_FC0_SUBTYPE_SHIFT],
837			    ether_sprintf(wh->i_addr2), rssi);
838		}
839#endif
840		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
841			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
842				/*
843				 * Only shared key auth frames with a challenge
844				 * should be encrypted, discard all others.
845				 */
846				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
847				    wh, ieee80211_mgt_subtype_name[subtype >>
848					IEEE80211_FC0_SUBTYPE_SHIFT],
849				    "%s", "WEP set but not permitted");
850				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
851				goto out;
852			}
853			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
854				/*
855				 * Discard encrypted frames when privacy is off.
856				 */
857				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
858				    wh, "mgt", "%s", "WEP set but PRIVACY off");
859				vap->iv_stats.is_rx_noprivacy++;
860				goto out;
861			}
862			hdrspace = ieee80211_hdrspace(ic, wh);
863			key = ieee80211_crypto_decap(ni, m, hdrspace);
864			if (key == NULL) {
865				/* NB: stats+msgs handled in crypto_decap */
866				goto out;
867			}
868			wh = mtod(m, struct ieee80211_frame *);
869			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
870		}
871		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
872		goto out;
873
874	case IEEE80211_FC0_TYPE_CTL:
875		vap->iv_stats.is_rx_ctl++;
876		IEEE80211_NODE_STAT(ni, rx_ctrl);
877		vap->iv_recv_ctl(ni, m, subtype);
878		goto out;
879	default:
880		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
881		    wh, NULL, "bad frame type 0x%x", type);
882		/* should not come here */
883		break;
884	}
885err:
886	ifp->if_ierrors++;
887out:
888	if (m != NULL) {
889		if (need_tap && bpf_peers_present(vap->iv_rawbpf))
890			bpf_mtap(vap->iv_rawbpf, m);
891		m_freem(m);
892	}
893	return type;
894#undef SEQ_LEQ
895}
896
897static void
898sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
899    int rssi, int noise, uint32_t rstamp, uint16_t seq, uint16_t status)
900{
901	struct ieee80211vap *vap = ni->ni_vap;
902
903	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
904		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
905		    ni->ni_macaddr, "open auth",
906		    "bad sta auth mode %u", ni->ni_authmode);
907		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
908		return;
909	}
910	if (vap->iv_state != IEEE80211_S_AUTH ||
911	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
912		vap->iv_stats.is_rx_bad_auth++;
913		return;
914	}
915	if (status != 0) {
916		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
917		    ni, "open auth failed (reason %d)", status);
918		vap->iv_stats.is_rx_auth_fail++;
919		vap->iv_stats.is_rx_authfail_code = status;
920		ieee80211_new_state(vap, IEEE80211_S_SCAN,
921		    IEEE80211_SCAN_FAIL_STATUS);
922	} else
923		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
924}
925
926static void
927sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
928    uint8_t *frm, uint8_t *efrm, int rssi, int noise, uint32_t rstamp,
929    uint16_t seq, uint16_t status)
930{
931	struct ieee80211vap *vap = ni->ni_vap;
932	uint8_t *challenge;
933	int estatus;
934
935	/*
936	 * NB: this can happen as we allow pre-shared key
937	 * authentication to be enabled w/o wep being turned
938	 * on so that configuration of these can be done
939	 * in any order.  It may be better to enforce the
940	 * ordering in which case this check would just be
941	 * for sanity/consistency.
942	 */
943	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
944		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
945		    ni->ni_macaddr, "shared key auth",
946		    "%s", " PRIVACY is disabled");
947		estatus = IEEE80211_STATUS_ALG;
948		goto bad;
949	}
950	/*
951	 * Pre-shared key authentication is evil; accept
952	 * it only if explicitly configured (it is supported
953	 * mainly for compatibility with clients like OS X).
954	 */
955	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
956	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
957		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
958		    ni->ni_macaddr, "shared key auth",
959		    "bad sta auth mode %u", ni->ni_authmode);
960		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
961		estatus = IEEE80211_STATUS_ALG;
962		goto bad;
963	}
964
965	challenge = NULL;
966	if (frm + 1 < efrm) {
967		if ((frm[1] + 2) > (efrm - frm)) {
968			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
969			    ni->ni_macaddr, "shared key auth",
970			    "ie %d/%d too long",
971			    frm[0], (frm[1] + 2) - (efrm - frm));
972			vap->iv_stats.is_rx_bad_auth++;
973			estatus = IEEE80211_STATUS_CHALLENGE;
974			goto bad;
975		}
976		if (*frm == IEEE80211_ELEMID_CHALLENGE)
977			challenge = frm;
978		frm += frm[1] + 2;
979	}
980	switch (seq) {
981	case IEEE80211_AUTH_SHARED_CHALLENGE:
982	case IEEE80211_AUTH_SHARED_RESPONSE:
983		if (challenge == NULL) {
984			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
985			    ni->ni_macaddr, "shared key auth",
986			    "%s", "no challenge");
987			vap->iv_stats.is_rx_bad_auth++;
988			estatus = IEEE80211_STATUS_CHALLENGE;
989			goto bad;
990		}
991		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
992			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
993			    ni->ni_macaddr, "shared key auth",
994			    "bad challenge len %d", challenge[1]);
995			vap->iv_stats.is_rx_bad_auth++;
996			estatus = IEEE80211_STATUS_CHALLENGE;
997			goto bad;
998		}
999	default:
1000		break;
1001	}
1002	if (vap->iv_state != IEEE80211_S_AUTH)
1003		return;
1004	switch (seq) {
1005	case IEEE80211_AUTH_SHARED_PASS:
1006		if (ni->ni_challenge != NULL) {
1007			free(ni->ni_challenge, M_80211_NODE);
1008			ni->ni_challenge = NULL;
1009		}
1010		if (status != 0) {
1011			IEEE80211_NOTE_FRAME(vap,
1012			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1013			    "shared key auth failed (reason %d)", status);
1014			vap->iv_stats.is_rx_auth_fail++;
1015			vap->iv_stats.is_rx_authfail_code = status;
1016			return;
1017		}
1018		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1019		break;
1020	case IEEE80211_AUTH_SHARED_CHALLENGE:
1021		if (!ieee80211_alloc_challenge(ni))
1022			return;
1023		/* XXX could optimize by passing recvd challenge */
1024		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1025		IEEE80211_SEND_MGMT(ni,
1026			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1027		break;
1028	default:
1029		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1030		    wh, "shared key auth", "bad seq %d", seq);
1031		vap->iv_stats.is_rx_bad_auth++;
1032		return;
1033	}
1034	return;
1035bad:
1036	/*
1037	 * Kick the state machine.  This short-circuits
1038	 * using the mgt frame timeout to trigger the
1039	 * state transition.
1040	 */
1041	if (vap->iv_state == IEEE80211_S_AUTH)
1042		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1043		    IEEE80211_SCAN_FAIL_STATUS);
1044}
1045
1046static int
1047ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1048	const struct ieee80211_frame *wh)
1049{
1050#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1051	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1052	u_int len = frm[1], qosinfo;
1053	int i;
1054
1055	if (len < sizeof(struct ieee80211_wme_param)-2) {
1056		IEEE80211_DISCARD_IE(vap,
1057		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1058		    wh, "WME", "too short, len %u", len);
1059		return -1;
1060	}
1061	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1062	qosinfo &= WME_QOSINFO_COUNT;
1063	/* XXX do proper check for wraparound */
1064	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1065		return 0;
1066	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1067	for (i = 0; i < WME_NUM_AC; i++) {
1068		struct wmeParams *wmep =
1069			&wme->wme_wmeChanParams.cap_wmeParams[i];
1070		/* NB: ACI not used */
1071		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1072		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1073		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1074		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1075		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1076		frm += 4;
1077	}
1078	wme->wme_wmeChanParams.cap_info = qosinfo;
1079	return 1;
1080#undef MS
1081}
1082
1083/*
1084 * Return non-zero if a background scan may be continued:
1085 * o bg scan is active
1086 * o no channel switch is pending
1087 * o there has not been any traffic recently
1088 *
1089 * Note we do not check if there is an administrative enable;
1090 * this is only done to start the scan.  We assume that any
1091 * change in state will be accompanied by a request to cancel
1092 * active scans which will otherwise cause this test to fail.
1093 */
1094static __inline int
1095contbgscan(struct ieee80211vap *vap)
1096{
1097	struct ieee80211com *ic = vap->iv_ic;
1098
1099	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1100	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1101	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1102	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1103}
1104
1105/*
1106 * Return non-zero if a backgrond scan may be started:
1107 * o bg scanning is administratively enabled
1108 * o no channel switch is pending
1109 * o we are not boosted on a dynamic turbo channel
1110 * o there has not been a scan recently
1111 * o there has not been any traffic recently
1112 */
1113static __inline int
1114startbgscan(struct ieee80211vap *vap)
1115{
1116	struct ieee80211com *ic = vap->iv_ic;
1117
1118	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1119	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1120#ifdef IEEE80211_SUPPORT_SUPERG
1121	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1122#endif
1123	    time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1124	    time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1125}
1126
1127static void
1128sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1129	int subtype, int rssi, int noise, uint32_t rstamp)
1130{
1131#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1132#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1133	struct ieee80211vap *vap = ni->ni_vap;
1134	struct ieee80211com *ic = ni->ni_ic;
1135	struct ieee80211_frame *wh;
1136	uint8_t *frm, *efrm;
1137	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1138	uint8_t rate;
1139
1140	wh = mtod(m0, struct ieee80211_frame *);
1141	frm = (uint8_t *)&wh[1];
1142	efrm = mtod(m0, uint8_t *) + m0->m_len;
1143	switch (subtype) {
1144	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1145	case IEEE80211_FC0_SUBTYPE_BEACON: {
1146		struct ieee80211_scanparams scan;
1147		/*
1148		 * We process beacon/probe response frames:
1149		 *    o when scanning, or
1150		 *    o station mode when associated (to collect state
1151		 *      updates such as 802.11g slot time)
1152		 * Frames otherwise received are discarded.
1153		 */
1154		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1155			vap->iv_stats.is_rx_mgtdiscard++;
1156			return;
1157		}
1158		/* XXX probe response in sta mode when !scanning? */
1159		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1160			return;
1161		/*
1162		 * Count frame now that we know it's to be processed.
1163		 */
1164		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1165			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1166			IEEE80211_NODE_STAT(ni, rx_beacons);
1167		} else
1168			IEEE80211_NODE_STAT(ni, rx_proberesp);
1169		/*
1170		 * When operating in station mode, check for state updates.
1171		 * Be careful to ignore beacons received while doing a
1172		 * background scan.  We consider only 11g/WMM stuff right now.
1173		 */
1174		if (ni->ni_associd != 0 &&
1175		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1176		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1177			/* record tsf of last beacon */
1178			memcpy(ni->ni_tstamp.data, scan.tstamp,
1179				sizeof(ni->ni_tstamp));
1180			/* count beacon frame for s/w bmiss handling */
1181			vap->iv_swbmiss_count++;
1182			vap->iv_bmiss_count = 0;
1183			if (ni->ni_erp != scan.erp) {
1184				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1185				    wh->i_addr2,
1186				    "erp change: was 0x%x, now 0x%x",
1187				    ni->ni_erp, scan.erp);
1188				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1189				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1190					ic->ic_flags |= IEEE80211_F_USEPROT;
1191				else
1192					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1193				ni->ni_erp = scan.erp;
1194				/* XXX statistic */
1195				/* XXX driver notification */
1196			}
1197			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1198				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1199				    wh->i_addr2,
1200				    "capabilities change: was 0x%x, now 0x%x",
1201				    ni->ni_capinfo, scan.capinfo);
1202				/*
1203				 * NB: we assume short preamble doesn't
1204				 *     change dynamically
1205				 */
1206				ieee80211_set_shortslottime(ic,
1207					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1208					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1209				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1210					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1211				/* XXX statistic */
1212			}
1213			if (scan.wme != NULL &&
1214			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1215			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1216				ieee80211_wme_updateparams(vap);
1217#ifdef IEEE80211_SUPPORT_SUPERG
1218			if (scan.ath != NULL)
1219				ieee80211_parse_athparams(ni, scan.ath, wh);
1220#endif
1221			if (scan.htcap != NULL && scan.htinfo != NULL &&
1222			    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1223				ieee80211_ht_updateparams(ni,
1224				    scan.htcap, scan.htinfo);
1225				/* XXX state changes? */
1226			}
1227			if (scan.tim != NULL) {
1228				struct ieee80211_tim_ie *tim =
1229				    (struct ieee80211_tim_ie *) scan.tim;
1230#if 0
1231				int aid = IEEE80211_AID(ni->ni_associd);
1232				int ix = aid / NBBY;
1233				int min = tim->tim_bitctl &~ 1;
1234				int max = tim->tim_len + min - 4;
1235				if ((tim->tim_bitctl&1) ||
1236				    (min <= ix && ix <= max &&
1237				     isset(tim->tim_bitmap - min, aid))) {
1238					/*
1239					 * XXX Do not let bg scan kick off
1240					 * we are expecting data.
1241					 */
1242					ic->ic_lastdata = ticks;
1243					ieee80211_sta_pwrsave(vap, 0);
1244				}
1245#endif
1246				ni->ni_dtim_count = tim->tim_count;
1247				ni->ni_dtim_period = tim->tim_period;
1248			}
1249			/*
1250			 * If scanning, pass the info to the scan module.
1251			 * Otherwise, check if it's the right time to do
1252			 * a background scan.  Background scanning must
1253			 * be enabled and we must not be operating in the
1254			 * turbo phase of dynamic turbo mode.  Then,
1255			 * it's been a while since the last background
1256			 * scan and if no data frames have come through
1257			 * recently, kick off a scan.  Note that this
1258			 * is the mechanism by which a background scan
1259			 * is started _and_ continued each time we
1260			 * return on-channel to receive a beacon from
1261			 * our ap.
1262			 */
1263			if (ic->ic_flags & IEEE80211_F_SCAN) {
1264				ieee80211_add_scan(vap, &scan, wh,
1265					subtype, rssi, noise, rstamp);
1266			} else if (contbgscan(vap)) {
1267				ieee80211_bg_scan(vap, 0);
1268			} else if (startbgscan(vap)) {
1269				vap->iv_stats.is_scan_bg++;
1270#if 0
1271				/* wakeup if we are sleeing */
1272				ieee80211_set_pwrsave(vap, 0);
1273#endif
1274				ieee80211_bg_scan(vap, 0);
1275			}
1276			return;
1277		}
1278		/*
1279		 * If scanning, just pass information to the scan module.
1280		 */
1281		if (ic->ic_flags & IEEE80211_F_SCAN) {
1282			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1283				/*
1284				 * Actively scanning a channel marked passive;
1285				 * send a probe request now that we know there
1286				 * is 802.11 traffic present.
1287				 *
1288				 * XXX check if the beacon we recv'd gives
1289				 * us what we need and suppress the probe req
1290				 */
1291				ieee80211_probe_curchan(vap, 1);
1292				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1293			}
1294			ieee80211_add_scan(vap, &scan, wh,
1295				subtype, rssi, noise, rstamp);
1296			return;
1297		}
1298		break;
1299	}
1300
1301	case IEEE80211_FC0_SUBTYPE_AUTH: {
1302		uint16_t algo, seq, status;
1303		/*
1304		 * auth frame format
1305		 *	[2] algorithm
1306		 *	[2] sequence
1307		 *	[2] status
1308		 *	[tlv*] challenge
1309		 */
1310		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1311		algo   = le16toh(*(uint16_t *)frm);
1312		seq    = le16toh(*(uint16_t *)(frm + 2));
1313		status = le16toh(*(uint16_t *)(frm + 4));
1314		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1315		    "recv auth frame with algorithm %d seq %d", algo, seq);
1316
1317		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1318			IEEE80211_DISCARD(vap,
1319			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1320			    wh, "auth", "%s", "TKIP countermeasures enabled");
1321			vap->iv_stats.is_rx_auth_countermeasures++;
1322			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1323				ieee80211_send_error(ni, wh->i_addr2,
1324					IEEE80211_FC0_SUBTYPE_AUTH,
1325					IEEE80211_REASON_MIC_FAILURE);
1326			}
1327			return;
1328		}
1329		if (algo == IEEE80211_AUTH_ALG_SHARED)
1330			sta_auth_shared(ni, wh, frm + 6, efrm, rssi,
1331			    noise, rstamp, seq, status);
1332		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1333			sta_auth_open(ni, wh, rssi, noise, rstamp,
1334			    seq, status);
1335		else {
1336			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1337			    wh, "auth", "unsupported alg %d", algo);
1338			vap->iv_stats.is_rx_auth_unsupported++;
1339			return;
1340		}
1341		break;
1342	}
1343
1344	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1345	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1346		uint16_t capinfo, associd;
1347		uint16_t status;
1348
1349		if (vap->iv_state != IEEE80211_S_ASSOC) {
1350			vap->iv_stats.is_rx_mgtdiscard++;
1351			return;
1352		}
1353
1354		/*
1355		 * asresp frame format
1356		 *	[2] capability information
1357		 *	[2] status
1358		 *	[2] association ID
1359		 *	[tlv] supported rates
1360		 *	[tlv] extended supported rates
1361		 *	[tlv] WME
1362		 *	[tlv] HT capabilities
1363		 *	[tlv] HT info
1364		 */
1365		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1366		ni = vap->iv_bss;
1367		capinfo = le16toh(*(uint16_t *)frm);
1368		frm += 2;
1369		status = le16toh(*(uint16_t *)frm);
1370		frm += 2;
1371		if (status != 0) {
1372			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1373			    wh->i_addr2, "%sassoc failed (reason %d)",
1374			    ISREASSOC(subtype) ?  "re" : "", status);
1375			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1376			return;
1377		}
1378		associd = le16toh(*(uint16_t *)frm);
1379		frm += 2;
1380
1381		rates = xrates = wme = htcap = htinfo = NULL;
1382		while (efrm - frm > 1) {
1383			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1384			switch (*frm) {
1385			case IEEE80211_ELEMID_RATES:
1386				rates = frm;
1387				break;
1388			case IEEE80211_ELEMID_XRATES:
1389				xrates = frm;
1390				break;
1391			case IEEE80211_ELEMID_HTCAP:
1392				htcap = frm;
1393				break;
1394			case IEEE80211_ELEMID_HTINFO:
1395				htinfo = frm;
1396				break;
1397			case IEEE80211_ELEMID_VENDOR:
1398				if (iswmeoui(frm))
1399					wme = frm;
1400				else if (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
1401					/*
1402					 * Accept pre-draft HT ie's if the
1403					 * standard ones have not been seen.
1404					 */
1405					if (ishtcapoui(frm)) {
1406						if (htcap == NULL)
1407							htcap = frm;
1408					} else if (ishtinfooui(frm)) {
1409						if (htinfo == NULL)
1410							htcap = frm;
1411					}
1412				}
1413				/* XXX Atheros OUI support */
1414				break;
1415			}
1416			frm += frm[1] + 2;
1417		}
1418
1419		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1420		if (xrates != NULL)
1421			IEEE80211_VERIFY_ELEMENT(xrates,
1422				IEEE80211_RATE_MAXSIZE - rates[1], return);
1423		rate = ieee80211_setup_rates(ni, rates, xrates,
1424				IEEE80211_F_JOIN |
1425				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1426				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1427		if (rate & IEEE80211_RATE_BASIC) {
1428			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1429			    wh->i_addr2,
1430			    "%sassoc failed (rate set mismatch)",
1431			    ISREASSOC(subtype) ?  "re" : "");
1432			vap->iv_stats.is_rx_assoc_norate++;
1433			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1434			    IEEE80211_SCAN_FAIL_STATUS);
1435			return;
1436		}
1437
1438		ni->ni_capinfo = capinfo;
1439		ni->ni_associd = associd;
1440		if (ni->ni_jointime == 0)
1441			ni->ni_jointime = time_uptime;
1442		if (wme != NULL &&
1443		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1444			ni->ni_flags |= IEEE80211_NODE_QOS;
1445			ieee80211_wme_updateparams(vap);
1446		} else
1447			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1448		/*
1449		 * Setup HT state according to the negotiation.
1450		 *
1451		 * NB: shouldn't need to check if HT use is enabled but some
1452		 *     ap's send back HT ie's even when we don't indicate we
1453		 *     are HT capable in our AssocReq.
1454		 */
1455		if (htcap != NULL && htinfo != NULL &&
1456		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1457			ieee80211_ht_node_init(ni);
1458			ieee80211_ht_updateparams(ni, htcap, htinfo);
1459			ieee80211_setup_htrates(ni, htcap,
1460			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1461			ieee80211_setup_basic_htrates(ni, htinfo);
1462		} else {
1463#ifdef IEEE80211_SUPPORT_SUPERG
1464			if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1465				ieee80211_ff_node_init(ni);
1466#endif
1467		}
1468		/*
1469		 * Configure state now that we are associated.
1470		 *
1471		 * XXX may need different/additional driver callbacks?
1472		 */
1473		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1474		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1475			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1476			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1477		} else {
1478			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1479			ic->ic_flags |= IEEE80211_F_USEBARKER;
1480		}
1481		ieee80211_set_shortslottime(ic,
1482			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1483			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1484		/*
1485		 * Honor ERP protection.
1486		 *
1487		 * NB: ni_erp should zero for non-11g operation.
1488		 */
1489		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1490		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1491			ic->ic_flags |= IEEE80211_F_USEPROT;
1492		else
1493			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1494		IEEE80211_NOTE_MAC(vap,
1495		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1496		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1497		    ISREASSOC(subtype) ? "re" : "",
1498		    IEEE80211_NODE_AID(ni),
1499		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1500		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1501		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1502		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1503		    ni->ni_flags & IEEE80211_NODE_HT ?
1504			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1505		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1506		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1507			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1508		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1509		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1510			", fast-frames" : "",
1511		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1512			", turbo" : ""
1513		);
1514		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1515		break;
1516	}
1517
1518	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1519		uint16_t reason;
1520
1521		if (vap->iv_state == IEEE80211_S_SCAN) {
1522			vap->iv_stats.is_rx_mgtdiscard++;
1523			return;
1524		}
1525		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1526			/* NB: can happen when in promiscuous mode */
1527			vap->iv_stats.is_rx_mgtdiscard++;
1528			break;
1529		}
1530
1531		/*
1532		 * deauth frame format
1533		 *	[2] reason
1534		 */
1535		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1536		reason = le16toh(*(uint16_t *)frm);
1537
1538		vap->iv_stats.is_rx_deauth++;
1539		vap->iv_stats.is_rx_deauth_code = reason;
1540		IEEE80211_NODE_STAT(ni, rx_deauth);
1541
1542		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1543		    "recv deauthenticate (reason %d)", reason);
1544		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1545		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1546		break;
1547	}
1548
1549	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1550		uint16_t reason;
1551
1552		if (vap->iv_state != IEEE80211_S_RUN &&
1553		    vap->iv_state != IEEE80211_S_ASSOC &&
1554		    vap->iv_state != IEEE80211_S_AUTH) {
1555			vap->iv_stats.is_rx_mgtdiscard++;
1556			return;
1557		}
1558		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1559			/* NB: can happen when in promiscuous mode */
1560			vap->iv_stats.is_rx_mgtdiscard++;
1561			break;
1562		}
1563
1564		/*
1565		 * disassoc frame format
1566		 *	[2] reason
1567		 */
1568		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1569		reason = le16toh(*(uint16_t *)frm);
1570
1571		vap->iv_stats.is_rx_disassoc++;
1572		vap->iv_stats.is_rx_disassoc_code = reason;
1573		IEEE80211_NODE_STAT(ni, rx_disassoc);
1574
1575		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1576		    "recv disassociate (reason %d)", reason);
1577		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1578		break;
1579	}
1580
1581	case IEEE80211_FC0_SUBTYPE_ACTION:
1582		if (vap->iv_state == IEEE80211_S_RUN) {
1583			if (ieee80211_parse_action(ni, m0) == 0)
1584				ic->ic_recv_action(ni, frm, efrm);
1585		} else
1586			vap->iv_stats.is_rx_mgtdiscard++;
1587		break;
1588
1589	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1590	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1591	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1592		vap->iv_stats.is_rx_mgtdiscard++;
1593		return;
1594	default:
1595		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1596		     wh, "mgt", "subtype 0x%x not handled", subtype);
1597		vap->iv_stats.is_rx_badsubtype++;
1598		break;
1599	}
1600#undef ISREASSOC
1601#undef ISPROBE
1602}
1603
1604static void
1605sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)
1606{
1607}
1608