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