ieee80211_hostap.c revision 193655
1178354Ssam/*-
2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3178354Ssam * All rights reserved.
4178354Ssam *
5178354Ssam * Redistribution and use in source and binary forms, with or without
6178354Ssam * modification, are permitted provided that the following conditions
7178354Ssam * are met:
8178354Ssam * 1. Redistributions of source code must retain the above copyright
9178354Ssam *    notice, this list of conditions and the following disclaimer.
10178354Ssam * 2. Redistributions in binary form must reproduce the above copyright
11178354Ssam *    notice, this list of conditions and the following disclaimer in the
12178354Ssam *    documentation and/or other materials provided with the distribution.
13178354Ssam *
14178354Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15178354Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16178354Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17178354Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18178354Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19178354Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20178354Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21178354Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22178354Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23178354Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24178354Ssam */
25178354Ssam
26178354Ssam#include <sys/cdefs.h>
27178354Ssam#ifdef __FreeBSD__
28178354Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_hostap.c 193655 2009-06-07 22:00:22Z sam $");
29178354Ssam#endif
30178354Ssam
31178354Ssam/*
32178354Ssam * IEEE 802.11 HOSTAP mode support.
33178354Ssam */
34178354Ssam#include "opt_inet.h"
35178354Ssam#include "opt_wlan.h"
36178354Ssam
37178354Ssam#include <sys/param.h>
38178354Ssam#include <sys/systm.h>
39178354Ssam#include <sys/mbuf.h>
40178354Ssam#include <sys/malloc.h>
41178354Ssam#include <sys/kernel.h>
42178354Ssam
43178354Ssam#include <sys/socket.h>
44178354Ssam#include <sys/sockio.h>
45178354Ssam#include <sys/endian.h>
46178354Ssam#include <sys/errno.h>
47178354Ssam#include <sys/proc.h>
48178354Ssam#include <sys/sysctl.h>
49178354Ssam
50178354Ssam#include <net/if.h>
51178354Ssam#include <net/if_media.h>
52178354Ssam#include <net/if_llc.h>
53178354Ssam#include <net/ethernet.h>
54178354Ssam
55178354Ssam#include <net/bpf.h>
56178354Ssam
57178354Ssam#include <net80211/ieee80211_var.h>
58178354Ssam#include <net80211/ieee80211_hostap.h>
59178354Ssam#include <net80211/ieee80211_input.h>
60190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
61190391Ssam#include <net80211/ieee80211_superg.h>
62190391Ssam#endif
63178354Ssam#include <net80211/ieee80211_wds.h>
64178354Ssam
65178354Ssam#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
66178354Ssam
67178354Ssamstatic	void hostap_vattach(struct ieee80211vap *);
68178354Ssamstatic	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
69178354Ssamstatic	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
70192468Ssam	    int rssi, int nf);
71178354Ssamstatic void hostap_deliver_data(struct ieee80211vap *,
72178354Ssam	    struct ieee80211_node *, struct mbuf *);
73178354Ssamstatic void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
74192468Ssam	    int subtype, int rssi, int nf);
75191546Ssamstatic void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
76178354Ssamstatic void hostap_recv_pspoll(struct ieee80211_node *, struct mbuf *);
77178354Ssam
78178354Ssamvoid
79178354Ssamieee80211_hostap_attach(struct ieee80211com *ic)
80178354Ssam{
81178354Ssam	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
82178354Ssam}
83178354Ssam
84178354Ssamvoid
85178354Ssamieee80211_hostap_detach(struct ieee80211com *ic)
86178354Ssam{
87178354Ssam}
88178354Ssam
89178354Ssamstatic void
90178354Ssamhostap_vdetach(struct ieee80211vap *vap)
91178354Ssam{
92178354Ssam}
93178354Ssam
94178354Ssamstatic void
95178354Ssamhostap_vattach(struct ieee80211vap *vap)
96178354Ssam{
97178354Ssam	vap->iv_newstate = hostap_newstate;
98178354Ssam	vap->iv_input = hostap_input;
99178354Ssam	vap->iv_recv_mgmt = hostap_recv_mgmt;
100191546Ssam	vap->iv_recv_ctl = hostap_recv_ctl;
101178354Ssam	vap->iv_opdetach = hostap_vdetach;
102178354Ssam	vap->iv_deliver_data = hostap_deliver_data;
103178354Ssam}
104178354Ssam
105178354Ssamstatic void
106178354Ssamsta_disassoc(void *arg, struct ieee80211_node *ni)
107178354Ssam{
108178354Ssam	struct ieee80211vap *vap = arg;
109178354Ssam
110178354Ssam	if (ni->ni_vap == vap && ni->ni_associd != 0) {
111178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
112178354Ssam			IEEE80211_REASON_ASSOC_LEAVE);
113178354Ssam		ieee80211_node_leave(ni);
114178354Ssam	}
115178354Ssam}
116178354Ssam
117193413Ssamstatic void
118193413Ssamsta_csa(void *arg, struct ieee80211_node *ni)
119193413Ssam{
120193413Ssam	struct ieee80211vap *vap = arg;
121193413Ssam
122193413Ssam	if (ni->ni_vap == vap && ni->ni_associd != 0)
123193413Ssam		if (ni->ni_inact > vap->iv_inact_init) {
124193413Ssam			ni->ni_inact = vap->iv_inact_init;
125193413Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
126193413Ssam			    "%s: inact %u", __func__, ni->ni_inact);
127193413Ssam		}
128193413Ssam}
129193413Ssam
130193414Ssamstatic void
131193414Ssamsta_drop(void *arg, struct ieee80211_node *ni)
132193414Ssam{
133193414Ssam	struct ieee80211vap *vap = arg;
134193414Ssam
135193414Ssam	if (ni->ni_vap == vap && ni->ni_associd != 0)
136193414Ssam		ieee80211_node_leave(ni);
137193414Ssam}
138193414Ssam
139178354Ssam/*
140193414Ssam * Does a channel change require associated stations to re-associate
141193414Ssam * so protocol state is correct.  This is used when doing CSA across
142193414Ssam * bands or similar (e.g. HT -> legacy).
143193414Ssam */
144193414Ssamstatic int
145193414Ssamisbandchange(struct ieee80211com *ic)
146193414Ssam{
147193414Ssam	return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
148193414Ssam	    (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
149193414Ssam	     IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
150193414Ssam}
151193414Ssam
152193414Ssam/*
153178354Ssam * IEEE80211_M_HOSTAP vap state machine handler.
154178354Ssam */
155178354Ssamstatic int
156178354Ssamhostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
157178354Ssam{
158178354Ssam	struct ieee80211com *ic = vap->iv_ic;
159178354Ssam	enum ieee80211_state ostate;
160178354Ssam
161178354Ssam	IEEE80211_LOCK_ASSERT(ic);
162178354Ssam
163178354Ssam	ostate = vap->iv_state;
164178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
165178354Ssam	    __func__, ieee80211_state_name[ostate],
166178354Ssam	    ieee80211_state_name[nstate], arg);
167178354Ssam	vap->iv_state = nstate;			/* state transition */
168178354Ssam	if (ostate != IEEE80211_S_SCAN)
169178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
170178354Ssam	switch (nstate) {
171178354Ssam	case IEEE80211_S_INIT:
172178354Ssam		switch (ostate) {
173178354Ssam		case IEEE80211_S_SCAN:
174178354Ssam			ieee80211_cancel_scan(vap);
175178354Ssam			break;
176178354Ssam		case IEEE80211_S_CAC:
177178354Ssam			ieee80211_dfs_cac_stop(vap);
178178354Ssam			break;
179178354Ssam		case IEEE80211_S_RUN:
180178354Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
181178354Ssam			break;
182178354Ssam		default:
183178354Ssam			break;
184178354Ssam		}
185178354Ssam		if (ostate != IEEE80211_S_INIT) {
186178354Ssam			/* NB: optimize INIT -> INIT case */
187178354Ssam			ieee80211_reset_bss(vap);
188178354Ssam		}
189178354Ssam		if (vap->iv_auth->ia_detach != NULL)
190178354Ssam			vap->iv_auth->ia_detach(vap);
191178354Ssam		break;
192178354Ssam	case IEEE80211_S_SCAN:
193178354Ssam		switch (ostate) {
194178354Ssam		case IEEE80211_S_CSA:
195178354Ssam		case IEEE80211_S_RUN:
196178354Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
197178354Ssam			/*
198178354Ssam			 * Clear overlapping BSS state; the beacon frame
199178354Ssam			 * will be reconstructed on transition to the RUN
200178354Ssam			 * state and the timeout routines check if the flag
201178354Ssam			 * is set before doing anything so this is sufficient.
202178354Ssam			 */
203178354Ssam			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
204193655Ssam			ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
205178354Ssam			/* fall thru... */
206178354Ssam		case IEEE80211_S_CAC:
207178354Ssam			/*
208178354Ssam			 * NB: We may get here because of a manual channel
209178354Ssam			 *     change in which case we need to stop CAC
210178354Ssam			 * XXX no need to stop if ostate RUN but it's ok
211178354Ssam			 */
212178354Ssam			ieee80211_dfs_cac_stop(vap);
213178354Ssam			/* fall thru... */
214178354Ssam		case IEEE80211_S_INIT:
215178354Ssam			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
216178354Ssam			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
217178354Ssam				/*
218178354Ssam				 * Already have a channel; bypass the
219178354Ssam				 * scan and startup immediately.
220178354Ssam				 * ieee80211_create_ibss will call back to
221178354Ssam				 * move us to RUN state.
222178354Ssam				 */
223178354Ssam				ieee80211_create_ibss(vap, vap->iv_des_chan);
224178354Ssam				break;
225178354Ssam			}
226178354Ssam			/*
227178354Ssam			 * Initiate a scan.  We can come here as a result
228178354Ssam			 * of an IEEE80211_IOC_SCAN_REQ too in which case
229178354Ssam			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
230178354Ssam			 * and the scan request parameters will be present
231178354Ssam			 * in iv_scanreq.  Otherwise we do the default.
232178354Ssam			 */
233178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
234178354Ssam				ieee80211_check_scan(vap,
235178354Ssam				    vap->iv_scanreq_flags,
236178354Ssam				    vap->iv_scanreq_duration,
237178354Ssam				    vap->iv_scanreq_mindwell,
238178354Ssam				    vap->iv_scanreq_maxdwell,
239178354Ssam				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
240178354Ssam				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
241178354Ssam			} else
242178354Ssam				ieee80211_check_scan_current(vap);
243178354Ssam			break;
244178354Ssam		case IEEE80211_S_SCAN:
245178354Ssam			/*
246178354Ssam			 * A state change requires a reset; scan.
247178354Ssam			 */
248178354Ssam			ieee80211_check_scan_current(vap);
249178354Ssam			break;
250178354Ssam		default:
251178354Ssam			break;
252178354Ssam		}
253178354Ssam		break;
254178354Ssam	case IEEE80211_S_CAC:
255178354Ssam		/*
256178354Ssam		 * Start CAC on a DFS channel.  We come here when starting
257178354Ssam		 * a bss on a DFS channel (see ieee80211_create_ibss).
258178354Ssam		 */
259178354Ssam		ieee80211_dfs_cac_start(vap);
260178354Ssam		break;
261178354Ssam	case IEEE80211_S_RUN:
262178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA) {
263178354Ssam			/* XXX validate prerequisites */
264178354Ssam		}
265178354Ssam		switch (ostate) {
266178354Ssam		case IEEE80211_S_INIT:
267178354Ssam			/*
268178354Ssam			 * Already have a channel; bypass the
269178354Ssam			 * scan and startup immediately.
270178354Ssam			 * Note that ieee80211_create_ibss will call
271178354Ssam			 * back to do a RUN->RUN state change.
272178354Ssam			 */
273178354Ssam			ieee80211_create_ibss(vap,
274178354Ssam			    ieee80211_ht_adjust_channel(ic,
275193655Ssam				ic->ic_curchan, vap->iv_flags_ht));
276178354Ssam			/* NB: iv_bss is changed on return */
277178354Ssam			break;
278178354Ssam		case IEEE80211_S_CAC:
279178354Ssam			/*
280178354Ssam			 * NB: This is the normal state change when CAC
281178354Ssam			 * expires and no radar was detected; no need to
282178354Ssam			 * clear the CAC timer as it's already expired.
283178354Ssam			 */
284178354Ssam			/* fall thru... */
285178354Ssam		case IEEE80211_S_CSA:
286178354Ssam			/*
287193413Ssam			 * Shorten inactivity timer of associated stations
288193413Ssam			 * to weed out sta's that don't follow a CSA.
289193413Ssam			 */
290193413Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
291193413Ssam			/*
292178354Ssam			 * Update bss node channel to reflect where
293178354Ssam			 * we landed after CSA.
294178354Ssam			 */
295178354Ssam			ieee80211_node_set_chan(vap->iv_bss,
296178354Ssam			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
297178354Ssam				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
298178354Ssam			/* XXX bypass debug msgs */
299178354Ssam			break;
300178354Ssam		case IEEE80211_S_SCAN:
301178354Ssam		case IEEE80211_S_RUN:
302178354Ssam#ifdef IEEE80211_DEBUG
303178354Ssam			if (ieee80211_msg_debug(vap)) {
304178354Ssam				struct ieee80211_node *ni = vap->iv_bss;
305178354Ssam				ieee80211_note(vap,
306178354Ssam				    "synchronized with %s ssid ",
307178354Ssam				    ether_sprintf(ni->ni_bssid));
308178354Ssam				ieee80211_print_essid(ni->ni_essid,
309178354Ssam				    ni->ni_esslen);
310178354Ssam				/* XXX MCS/HT */
311178354Ssam				printf(" channel %d start %uMb\n",
312178354Ssam				    ieee80211_chan2ieee(ic, ic->ic_curchan),
313178354Ssam				    IEEE80211_RATE2MBS(ni->ni_txrate));
314178354Ssam			}
315178354Ssam#endif
316178354Ssam			break;
317178354Ssam		default:
318178354Ssam			break;
319178354Ssam		}
320178354Ssam		/*
321178354Ssam		 * Start/stop the authenticator.  We delay until here
322178354Ssam		 * to allow configuration to happen out of order.
323178354Ssam		 */
324178354Ssam		if (vap->iv_auth->ia_attach != NULL) {
325178354Ssam			/* XXX check failure */
326178354Ssam			vap->iv_auth->ia_attach(vap);
327178354Ssam		} else if (vap->iv_auth->ia_detach != NULL) {
328178354Ssam			vap->iv_auth->ia_detach(vap);
329178354Ssam		}
330178354Ssam		ieee80211_node_authorize(vap->iv_bss);
331178354Ssam		break;
332193414Ssam	case IEEE80211_S_CSA:
333193414Ssam		if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
334193414Ssam			/*
335193414Ssam			 * On a ``band change'' silently drop associated
336193414Ssam			 * stations as they must re-associate before they
337193414Ssam			 * can pass traffic (as otherwise protocol state
338193414Ssam			 * such as capabilities and the negotiated rate
339193414Ssam			 * set may/will be wrong).
340193414Ssam			 */
341193414Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
342193414Ssam		}
343193414Ssam		break;
344178354Ssam	default:
345178354Ssam		break;
346178354Ssam	}
347178354Ssam	return 0;
348178354Ssam}
349178354Ssam
350178354Ssamstatic void
351178354Ssamhostap_deliver_data(struct ieee80211vap *vap,
352178354Ssam	struct ieee80211_node *ni, struct mbuf *m)
353178354Ssam{
354178354Ssam	struct ether_header *eh = mtod(m, struct ether_header *);
355178354Ssam	struct ifnet *ifp = vap->iv_ifp;
356178354Ssam
357193292Ssam	/* clear driver/net80211 flags before passing up */
358193292Ssam	m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
359193292Ssam
360178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
361178354Ssam	    ("gack, opmode %d", vap->iv_opmode));
362178354Ssam	/*
363178354Ssam	 * Do accounting.
364178354Ssam	 */
365178354Ssam	ifp->if_ipackets++;
366178354Ssam	IEEE80211_NODE_STAT(ni, rx_data);
367178354Ssam	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
368178354Ssam	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
369178354Ssam		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
370178354Ssam		IEEE80211_NODE_STAT(ni, rx_mcast);
371178354Ssam	} else
372178354Ssam		IEEE80211_NODE_STAT(ni, rx_ucast);
373178354Ssam
374178354Ssam	/* perform as a bridge within the AP */
375178354Ssam	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
376178354Ssam		struct mbuf *mcopy = NULL;
377178354Ssam
378178354Ssam		if (m->m_flags & M_MCAST) {
379183364Ssam			mcopy = m_dup(m, M_DONTWAIT);
380178354Ssam			if (mcopy == NULL)
381178354Ssam				ifp->if_oerrors++;
382178354Ssam			else
383178354Ssam				mcopy->m_flags |= M_MCAST;
384178354Ssam		} else {
385178354Ssam			/*
386178354Ssam			 * Check if the destination is associated with the
387178354Ssam			 * same vap and authorized to receive traffic.
388178354Ssam			 * Beware of traffic destined for the vap itself;
389178354Ssam			 * sending it will not work; just let it be delivered
390178354Ssam			 * normally.
391178354Ssam			 */
392178354Ssam			struct ieee80211_node *sta = ieee80211_find_vap_node(
393178354Ssam			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
394178354Ssam			if (sta != NULL) {
395178354Ssam				if (ieee80211_node_is_authorized(sta)) {
396178354Ssam					/*
397178354Ssam					 * Beware of sending to ourself; this
398178354Ssam					 * needs to happen via the normal
399178354Ssam					 * input path.
400178354Ssam					 */
401178354Ssam					if (sta != vap->iv_bss) {
402178354Ssam						mcopy = m;
403178354Ssam						m = NULL;
404178354Ssam					}
405178354Ssam				} else {
406178354Ssam					vap->iv_stats.is_rx_unauth++;
407178354Ssam					IEEE80211_NODE_STAT(sta, rx_unauth);
408178354Ssam				}
409178354Ssam				ieee80211_free_node(sta);
410178354Ssam			}
411178354Ssam		}
412178354Ssam		if (mcopy != NULL) {
413178354Ssam			int len, err;
414178354Ssam			len = mcopy->m_pkthdr.len;
415186658Ssam			err = ifp->if_transmit(ifp, mcopy);
416178354Ssam			if (err) {
417178354Ssam				/* NB: IFQ_HANDOFF reclaims mcopy */
418178354Ssam			} else {
419178354Ssam				ifp->if_opackets++;
420178354Ssam			}
421178354Ssam		}
422178354Ssam	}
423178354Ssam	if (m != NULL) {
424178354Ssam		/*
425178354Ssam		 * Mark frame as coming from vap's interface.
426178354Ssam		 */
427178354Ssam		m->m_pkthdr.rcvif = ifp;
428178354Ssam		if (m->m_flags & M_MCAST) {
429178354Ssam			/*
430178354Ssam			 * Spam DWDS vap's w/ multicast traffic.
431178354Ssam			 */
432178354Ssam			/* XXX only if dwds in use? */
433178354Ssam			ieee80211_dwds_mcast(vap, m);
434178354Ssam		}
435178354Ssam		if (ni->ni_vlan != 0) {
436178354Ssam			/* attach vlan tag */
437178354Ssam			m->m_pkthdr.ether_vtag = ni->ni_vlan;
438178354Ssam			m->m_flags |= M_VLANTAG;
439178354Ssam		}
440178354Ssam		ifp->if_input(ifp, m);
441178354Ssam	}
442178354Ssam}
443178354Ssam
444178354Ssam/*
445178354Ssam * Decide if a received management frame should be
446178354Ssam * printed when debugging is enabled.  This filters some
447178354Ssam * of the less interesting frames that come frequently
448178354Ssam * (e.g. beacons).
449178354Ssam */
450178354Ssamstatic __inline int
451178354Ssamdoprint(struct ieee80211vap *vap, int subtype)
452178354Ssam{
453178354Ssam	switch (subtype) {
454178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
455178354Ssam		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
456178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
457178354Ssam		return 0;
458178354Ssam	}
459178354Ssam	return 1;
460178354Ssam}
461178354Ssam
462178354Ssam/*
463178354Ssam * Process a received frame.  The node associated with the sender
464178354Ssam * should be supplied.  If nothing was found in the node table then
465178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
466178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
467178354Ssam * during AP scanning to select a AP to associate with; it can have
468178354Ssam * any units so long as values have consistent units and higher values
469178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
470178354Ssam * by the 802.11 layer.
471178354Ssam */
472178354Ssamstatic int
473192468Ssamhostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
474178354Ssam{
475178354Ssam#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
476178354Ssam#define	HAS_SEQ(type)	((type & 0x4) == 0)
477178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
478178354Ssam	struct ieee80211com *ic = ni->ni_ic;
479178354Ssam	struct ifnet *ifp = vap->iv_ifp;
480178354Ssam	struct ieee80211_frame *wh;
481178354Ssam	struct ieee80211_key *key;
482178354Ssam	struct ether_header *eh;
483178354Ssam	int hdrspace, need_tap;
484178354Ssam	uint8_t dir, type, subtype, qos;
485178354Ssam	uint8_t *bssid;
486178354Ssam	uint16_t rxseq;
487178354Ssam
488183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
489178354Ssam		/*
490178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
491183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
492183247Ssam		 * here but were received out of order and been held on
493183247Ssam		 * the reorder queue.  When resubmitted they are marked
494183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
495183247Ssam		 * the normal processing.
496178354Ssam		 */
497178354Ssam		wh = mtod(m, struct ieee80211_frame *);
498178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
499178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
500178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
501178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
502178354Ssam		goto resubmit_ampdu;
503178354Ssam	}
504178354Ssam
505178354Ssam	KASSERT(ni != NULL, ("null node"));
506178354Ssam	ni->ni_inact = ni->ni_inact_reload;
507178354Ssam
508178354Ssam	need_tap = 1;			/* mbuf need to be tapped. */
509178354Ssam	type = -1;			/* undefined */
510178354Ssam
511178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
512178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
513178354Ssam		    ni->ni_macaddr, NULL,
514178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
515178354Ssam		vap->iv_stats.is_rx_tooshort++;
516178354Ssam		goto out;
517178354Ssam	}
518178354Ssam	/*
519178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
520178354Ssam	 * frame format but don't reference fields past outside
521178354Ssam	 * ieee80211_frame_min w/o first validating the data is
522178354Ssam	 * present.
523178354Ssam	 */
524178354Ssam	wh = mtod(m, struct ieee80211_frame *);
525178354Ssam
526178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
527178354Ssam	    IEEE80211_FC0_VERSION_0) {
528178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
529191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
530191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
531178354Ssam		vap->iv_stats.is_rx_badversion++;
532178354Ssam		goto err;
533178354Ssam	}
534178354Ssam
535178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
536178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
537178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
538178354Ssam	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
539178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS)
540178354Ssam			bssid = wh->i_addr1;
541178354Ssam		else if (type == IEEE80211_FC0_TYPE_CTL)
542178354Ssam			bssid = wh->i_addr1;
543178354Ssam		else {
544178354Ssam			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
545178354Ssam				IEEE80211_DISCARD_MAC(vap,
546178354Ssam				    IEEE80211_MSG_ANY, ni->ni_macaddr,
547178354Ssam				    NULL, "too short (2): len %u",
548178354Ssam				    m->m_pkthdr.len);
549178354Ssam				vap->iv_stats.is_rx_tooshort++;
550178354Ssam				goto out;
551178354Ssam			}
552178354Ssam			bssid = wh->i_addr3;
553178354Ssam		}
554178354Ssam		/*
555178354Ssam		 * Validate the bssid.
556178354Ssam		 */
557178354Ssam		if (!(type == IEEE80211_FC0_TYPE_MGT &&
558178354Ssam		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
559178354Ssam		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
560178354Ssam		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
561178354Ssam			/* not interested in */
562178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
563178354Ssam			    bssid, NULL, "%s", "not to bss");
564178354Ssam			vap->iv_stats.is_rx_wrongbss++;
565178354Ssam			goto out;
566178354Ssam		}
567178354Ssam
568178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
569192468Ssam		ni->ni_noise = nf;
570178354Ssam		if (HAS_SEQ(type)) {
571178354Ssam			uint8_t tid = ieee80211_gettid(wh);
572178354Ssam			if (IEEE80211_QOS_HAS_SEQ(wh) &&
573178354Ssam			    TID_TO_WME_AC(tid) >= WME_AC_VI)
574178354Ssam				ic->ic_wme.wme_hipri_traffic++;
575178354Ssam			rxseq = le16toh(*(uint16_t *)wh->i_seq);
576178354Ssam			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
577178354Ssam			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
578178354Ssam			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
579178354Ssam				/* duplicate, discard */
580178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
581178354Ssam				    bssid, "duplicate",
582178354Ssam				    "seqno <%u,%u> fragno <%u,%u> tid %u",
583178354Ssam				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
584178354Ssam				    ni->ni_rxseqs[tid] >>
585178354Ssam					IEEE80211_SEQ_SEQ_SHIFT,
586178354Ssam				    rxseq & IEEE80211_SEQ_FRAG_MASK,
587178354Ssam				    ni->ni_rxseqs[tid] &
588178354Ssam					IEEE80211_SEQ_FRAG_MASK,
589178354Ssam				    tid);
590178354Ssam				vap->iv_stats.is_rx_dup++;
591178354Ssam				IEEE80211_NODE_STAT(ni, rx_dup);
592178354Ssam				goto out;
593178354Ssam			}
594178354Ssam			ni->ni_rxseqs[tid] = rxseq;
595178354Ssam		}
596178354Ssam	}
597178354Ssam
598178354Ssam	switch (type) {
599178354Ssam	case IEEE80211_FC0_TYPE_DATA:
600178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
601178354Ssam		if (m->m_len < hdrspace &&
602178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
603178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
604178354Ssam			    ni->ni_macaddr, NULL,
605178354Ssam			    "data too short: expecting %u", hdrspace);
606178354Ssam			vap->iv_stats.is_rx_tooshort++;
607178354Ssam			goto out;		/* XXX */
608178354Ssam		}
609178354Ssam		if (!(dir == IEEE80211_FC1_DIR_TODS ||
610178354Ssam		     (dir == IEEE80211_FC1_DIR_DSTODS &&
611178354Ssam		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
612178354Ssam			if (dir != IEEE80211_FC1_DIR_DSTODS) {
613178354Ssam				IEEE80211_DISCARD(vap,
614178354Ssam				    IEEE80211_MSG_INPUT, wh, "data",
615178354Ssam				    "incorrect dir 0x%x", dir);
616178354Ssam			} else {
617178354Ssam				IEEE80211_DISCARD(vap,
618178354Ssam				    IEEE80211_MSG_INPUT |
619178354Ssam				    IEEE80211_MSG_WDS, wh,
620178354Ssam				    "4-address data",
621178354Ssam				    "%s", "DWDS not enabled");
622178354Ssam			}
623178354Ssam			vap->iv_stats.is_rx_wrongdir++;
624178354Ssam			goto out;
625178354Ssam		}
626178354Ssam		/* check if source STA is associated */
627178354Ssam		if (ni == vap->iv_bss) {
628178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
629178354Ssam			    wh, "data", "%s", "unknown src");
630178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
631178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
632178354Ssam			    IEEE80211_REASON_NOT_AUTHED);
633178354Ssam			vap->iv_stats.is_rx_notassoc++;
634178354Ssam			goto err;
635178354Ssam		}
636178354Ssam		if (ni->ni_associd == 0) {
637178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
638178354Ssam			    wh, "data", "%s", "unassoc src");
639178354Ssam			IEEE80211_SEND_MGMT(ni,
640178354Ssam			    IEEE80211_FC0_SUBTYPE_DISASSOC,
641178354Ssam			    IEEE80211_REASON_NOT_ASSOCED);
642178354Ssam			vap->iv_stats.is_rx_notassoc++;
643178354Ssam			goto err;
644178354Ssam		}
645178354Ssam
646178354Ssam		/*
647178354Ssam		 * Check for power save state change.
648178354Ssam		 * XXX out-of-order A-MPDU frames?
649178354Ssam		 */
650178354Ssam		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
651178354Ssam		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
652178354Ssam			ieee80211_node_pwrsave(ni,
653178354Ssam				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
654178354Ssam		/*
655178354Ssam		 * For 4-address packets handle WDS discovery
656178354Ssam		 * notifications.  Once a WDS link is setup frames
657178354Ssam		 * are just delivered to the WDS vap (see below).
658178354Ssam		 */
659178354Ssam		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
660178354Ssam			if (!ieee80211_node_is_authorized(ni)) {
661178354Ssam				IEEE80211_DISCARD(vap,
662178354Ssam				    IEEE80211_MSG_INPUT |
663178354Ssam				    IEEE80211_MSG_WDS, wh,
664178354Ssam				    "4-address data",
665178354Ssam				    "%s", "unauthorized port");
666178354Ssam				vap->iv_stats.is_rx_unauth++;
667178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
668178354Ssam				goto err;
669178354Ssam			}
670178354Ssam			ieee80211_dwds_discover(ni, m);
671178354Ssam			return type;
672178354Ssam		}
673178354Ssam
674178354Ssam		/*
675183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
676183247Ssam		 * processed directly then ieee80211_ampdu_reorder
677178354Ssam		 * will return 0; otherwise it has consumed the mbuf
678178354Ssam		 * and we should do nothing more with it.
679178354Ssam		 */
680183247Ssam		if ((m->m_flags & M_AMPDU) &&
681178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
682178354Ssam			m = NULL;
683178354Ssam			goto out;
684178354Ssam		}
685178354Ssam	resubmit_ampdu:
686178354Ssam
687178354Ssam		/*
688178354Ssam		 * Handle privacy requirements.  Note that we
689178354Ssam		 * must not be preempted from here until after
690178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
691178354Ssam		 * otherwise we may violate assumptions in the
692178354Ssam		 * crypto cipher modules used to do delayed update
693178354Ssam		 * of replay sequence numbers.
694178354Ssam		 */
695178354Ssam		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
696178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
697178354Ssam				/*
698178354Ssam				 * Discard encrypted frames when privacy is off.
699178354Ssam				 */
700178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
701178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
702178354Ssam				vap->iv_stats.is_rx_noprivacy++;
703178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
704178354Ssam				goto out;
705178354Ssam			}
706178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
707178354Ssam			if (key == NULL) {
708178354Ssam				/* NB: stats+msgs handled in crypto_decap */
709178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
710178354Ssam				goto out;
711178354Ssam			}
712178354Ssam			wh = mtod(m, struct ieee80211_frame *);
713178354Ssam			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
714178354Ssam		} else {
715178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
716178354Ssam			key = NULL;
717178354Ssam		}
718178354Ssam
719178354Ssam		/*
720178354Ssam		 * Save QoS bits for use below--before we strip the header.
721178354Ssam		 */
722178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
723178354Ssam			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
724178354Ssam			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
725178354Ssam			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
726178354Ssam		} else
727178354Ssam			qos = 0;
728178354Ssam
729178354Ssam		/*
730178354Ssam		 * Next up, any fragmentation.
731178354Ssam		 */
732178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
733178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
734178354Ssam			if (m == NULL) {
735178354Ssam				/* Fragment dropped or frame not complete yet */
736178354Ssam				goto out;
737178354Ssam			}
738178354Ssam		}
739178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
740178354Ssam
741178354Ssam		/*
742178354Ssam		 * Next strip any MSDU crypto bits.
743178354Ssam		 */
744178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
745178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
746178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
747178354Ssam			vap->iv_stats.is_rx_demicfail++;
748178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
749178354Ssam			goto out;
750178354Ssam		}
751178354Ssam		/* copy to listener after decrypt */
752192468Ssam		if (ieee80211_radiotap_active_vap(vap))
753192468Ssam			ieee80211_radiotap_rx(vap, m);
754178354Ssam		need_tap = 0;
755178354Ssam		/*
756178354Ssam		 * Finally, strip the 802.11 header.
757178354Ssam		 */
758178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
759178354Ssam		if (m == NULL) {
760178354Ssam			/* XXX mask bit to check for both */
761178354Ssam			/* don't count Null data frames as errors */
762178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
763178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
764178354Ssam				goto out;
765178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
766178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
767178354Ssam			vap->iv_stats.is_rx_decap++;
768178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
769178354Ssam			goto err;
770178354Ssam		}
771178354Ssam		eh = mtod(m, struct ether_header *);
772178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
773178354Ssam			/*
774178354Ssam			 * Deny any non-PAE frames received prior to
775178354Ssam			 * authorization.  For open/shared-key
776178354Ssam			 * authentication the port is mark authorized
777178354Ssam			 * after authentication completes.  For 802.1x
778178354Ssam			 * the port is not marked authorized by the
779178354Ssam			 * authenticator until the handshake has completed.
780178354Ssam			 */
781178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
782178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
783178354Ssam				    eh->ether_shost, "data",
784178354Ssam				    "unauthorized port: ether type 0x%x len %u",
785178354Ssam				    eh->ether_type, m->m_pkthdr.len);
786178354Ssam				vap->iv_stats.is_rx_unauth++;
787178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
788178354Ssam				goto err;
789178354Ssam			}
790178354Ssam		} else {
791178354Ssam			/*
792178354Ssam			 * When denying unencrypted frames, discard
793178354Ssam			 * any non-PAE frames received without encryption.
794178354Ssam			 */
795178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
796178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
797178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
798178354Ssam				/*
799178354Ssam				 * Drop unencrypted frames.
800178354Ssam				 */
801178354Ssam				vap->iv_stats.is_rx_unencrypted++;
802178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
803178354Ssam				goto out;
804178354Ssam			}
805178354Ssam		}
806178354Ssam		/* XXX require HT? */
807178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
808178354Ssam			m = ieee80211_decap_amsdu(ni, m);
809178354Ssam			if (m == NULL)
810178354Ssam				return IEEE80211_FC0_TYPE_DATA;
811190391Ssam		} else {
812190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
813190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
814190391Ssam			if (m == NULL)
815178354Ssam				return IEEE80211_FC0_TYPE_DATA;
816190391Ssam#endif
817178354Ssam		}
818178354Ssam		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
819178354Ssam			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
820178354Ssam		else
821178354Ssam			hostap_deliver_data(vap, ni, m);
822178354Ssam		return IEEE80211_FC0_TYPE_DATA;
823178354Ssam
824178354Ssam	case IEEE80211_FC0_TYPE_MGT:
825178354Ssam		vap->iv_stats.is_rx_mgmt++;
826178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
827178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
828178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
829178354Ssam			    wh, "mgt", "incorrect dir 0x%x", dir);
830178354Ssam			vap->iv_stats.is_rx_wrongdir++;
831178354Ssam			goto err;
832178354Ssam		}
833178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
834178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
835178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
836178354Ssam			    m->m_pkthdr.len);
837178354Ssam			vap->iv_stats.is_rx_tooshort++;
838178354Ssam			goto out;
839178354Ssam		}
840178354Ssam		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
841178354Ssam			/* ensure return frames are unicast */
842178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
843178354Ssam			    wh, NULL, "source is multicast: %s",
844178354Ssam			    ether_sprintf(wh->i_addr2));
845178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
846178354Ssam			goto out;
847178354Ssam		}
848178354Ssam#ifdef IEEE80211_DEBUG
849178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
850178354Ssam		    ieee80211_msg_dumppkts(vap)) {
851178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
852178354Ssam			    ieee80211_mgt_subtype_name[subtype >>
853178354Ssam				IEEE80211_FC0_SUBTYPE_SHIFT],
854178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
855178354Ssam		}
856178354Ssam#endif
857178354Ssam		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
858178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
859178354Ssam				/*
860178354Ssam				 * Only shared key auth frames with a challenge
861178354Ssam				 * should be encrypted, discard all others.
862178354Ssam				 */
863178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
864178354Ssam				    wh, NULL,
865178354Ssam				    "%s", "WEP set but not permitted");
866178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
867178354Ssam				goto out;
868178354Ssam			}
869178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
870178354Ssam				/*
871178354Ssam				 * Discard encrypted frames when privacy is off.
872178354Ssam				 */
873178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
874178354Ssam				    wh, NULL, "%s", "WEP set but PRIVACY off");
875178354Ssam				vap->iv_stats.is_rx_noprivacy++;
876178354Ssam				goto out;
877178354Ssam			}
878178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
879178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
880178354Ssam			if (key == NULL) {
881178354Ssam				/* NB: stats+msgs handled in crypto_decap */
882178354Ssam				goto out;
883178354Ssam			}
884178354Ssam			wh = mtod(m, struct ieee80211_frame *);
885178354Ssam			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
886178354Ssam		}
887192468Ssam		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
888191534Ssam		goto out;
889178354Ssam
890178354Ssam	case IEEE80211_FC0_TYPE_CTL:
891178354Ssam		vap->iv_stats.is_rx_ctl++;
892178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
893191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
894178354Ssam		goto out;
895178354Ssam	default:
896178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
897178354Ssam		    wh, "bad", "frame type 0x%x", type);
898178354Ssam		/* should not come here */
899178354Ssam		break;
900178354Ssam	}
901178354Ssamerr:
902178354Ssam	ifp->if_ierrors++;
903178354Ssamout:
904178354Ssam	if (m != NULL) {
905192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
906192468Ssam			ieee80211_radiotap_rx(vap, m);
907178354Ssam		m_freem(m);
908178354Ssam	}
909178354Ssam	return type;
910178354Ssam#undef SEQ_LEQ
911178354Ssam}
912178354Ssam
913178354Ssamstatic void
914178354Ssamhostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
915192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
916178354Ssam{
917178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
918178354Ssam
919178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
920178354Ssam
921178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
922178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
923178354Ssam		    ni->ni_macaddr, "open auth",
924178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
925178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
926178354Ssam		/*
927178354Ssam		 * Clear any challenge text that may be there if
928178354Ssam		 * a previous shared key auth failed and then an
929178354Ssam		 * open auth is attempted.
930178354Ssam		 */
931178354Ssam		if (ni->ni_challenge != NULL) {
932186302Ssam			free(ni->ni_challenge, M_80211_NODE);
933178354Ssam			ni->ni_challenge = NULL;
934178354Ssam		}
935178354Ssam		/* XXX hack to workaround calling convention */
936178354Ssam		ieee80211_send_error(ni, wh->i_addr2,
937178354Ssam		    IEEE80211_FC0_SUBTYPE_AUTH,
938178354Ssam		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
939178354Ssam		return;
940178354Ssam	}
941178354Ssam	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
942178354Ssam		vap->iv_stats.is_rx_bad_auth++;
943178354Ssam		return;
944178354Ssam	}
945178354Ssam	/* always accept open authentication requests */
946178354Ssam	if (ni == vap->iv_bss) {
947178354Ssam		ni = ieee80211_dup_bss(vap, wh->i_addr2);
948178354Ssam		if (ni == NULL)
949178354Ssam			return;
950178354Ssam	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
951178354Ssam		(void) ieee80211_ref_node(ni);
952178354Ssam	/*
953178354Ssam	 * Mark the node as referenced to reflect that it's
954178354Ssam	 * reference count has been bumped to insure it remains
955178354Ssam	 * after the transaction completes.
956178354Ssam	 */
957178354Ssam	ni->ni_flags |= IEEE80211_NODE_AREF;
958186099Ssam	/*
959186151Ssam	 * Mark the node as requiring a valid association id
960186099Ssam	 * before outbound traffic is permitted.
961186099Ssam	 */
962186099Ssam	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
963178354Ssam
964178354Ssam	if (vap->iv_acl != NULL &&
965178354Ssam	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
966178354Ssam		/*
967178354Ssam		 * When the ACL policy is set to RADIUS we defer the
968178354Ssam		 * authorization to a user agent.  Dispatch an event,
969178354Ssam		 * a subsequent MLME call will decide the fate of the
970178354Ssam		 * station.  If the user agent is not present then the
971178354Ssam		 * node will be reclaimed due to inactivity.
972178354Ssam		 */
973178354Ssam		IEEE80211_NOTE_MAC(vap,
974178354Ssam		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
975178354Ssam		    "%s", "station authentication defered (radius acl)");
976178354Ssam		ieee80211_notify_node_auth(ni);
977178354Ssam	} else {
978178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
979178354Ssam		IEEE80211_NOTE_MAC(vap,
980178354Ssam		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
981178354Ssam		    "%s", "station authenticated (open)");
982178354Ssam		/*
983178354Ssam		 * When 802.1x is not in use mark the port
984178354Ssam		 * authorized at this point so traffic can flow.
985178354Ssam		 */
986178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
987178354Ssam			ieee80211_node_authorize(ni);
988178354Ssam	}
989178354Ssam}
990178354Ssam
991178354Ssamstatic void
992178354Ssamhostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
993192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
994178354Ssam    uint16_t seq, uint16_t status)
995178354Ssam{
996178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
997178354Ssam	uint8_t *challenge;
998178354Ssam	int allocbs, estatus;
999178354Ssam
1000178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1001178354Ssam
1002178354Ssam	/*
1003178354Ssam	 * NB: this can happen as we allow pre-shared key
1004178354Ssam	 * authentication to be enabled w/o wep being turned
1005178354Ssam	 * on so that configuration of these can be done
1006178354Ssam	 * in any order.  It may be better to enforce the
1007178354Ssam	 * ordering in which case this check would just be
1008178354Ssam	 * for sanity/consistency.
1009178354Ssam	 */
1010178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1011178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1012178354Ssam		    ni->ni_macaddr, "shared key auth",
1013178354Ssam		    "%s", " PRIVACY is disabled");
1014178354Ssam		estatus = IEEE80211_STATUS_ALG;
1015178354Ssam		goto bad;
1016178354Ssam	}
1017178354Ssam	/*
1018178354Ssam	 * Pre-shared key authentication is evil; accept
1019178354Ssam	 * it only if explicitly configured (it is supported
1020178354Ssam	 * mainly for compatibility with clients like Mac OS X).
1021178354Ssam	 */
1022178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1023178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1024178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1025178354Ssam		    ni->ni_macaddr, "shared key auth",
1026178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1027178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1028178354Ssam		estatus = IEEE80211_STATUS_ALG;
1029178354Ssam		goto bad;
1030178354Ssam	}
1031178354Ssam
1032178354Ssam	challenge = NULL;
1033178354Ssam	if (frm + 1 < efrm) {
1034178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1035178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1036178354Ssam			    ni->ni_macaddr, "shared key auth",
1037178354Ssam			    "ie %d/%d too long",
1038178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1039178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1040178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1041178354Ssam			goto bad;
1042178354Ssam		}
1043178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1044178354Ssam			challenge = frm;
1045178354Ssam		frm += frm[1] + 2;
1046178354Ssam	}
1047178354Ssam	switch (seq) {
1048178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1049178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1050178354Ssam		if (challenge == NULL) {
1051178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1052178354Ssam			    ni->ni_macaddr, "shared key auth",
1053178354Ssam			    "%s", "no challenge");
1054178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1055178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1056178354Ssam			goto bad;
1057178354Ssam		}
1058178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1059178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1060178354Ssam			    ni->ni_macaddr, "shared key auth",
1061178354Ssam			    "bad challenge len %d", challenge[1]);
1062178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1063178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1064178354Ssam			goto bad;
1065178354Ssam		}
1066178354Ssam	default:
1067178354Ssam		break;
1068178354Ssam	}
1069178354Ssam	switch (seq) {
1070178354Ssam	case IEEE80211_AUTH_SHARED_REQUEST:
1071178354Ssam		if (ni == vap->iv_bss) {
1072178354Ssam			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1073178354Ssam			if (ni == NULL) {
1074178354Ssam				/* NB: no way to return an error */
1075178354Ssam				return;
1076178354Ssam			}
1077178354Ssam			allocbs = 1;
1078178354Ssam		} else {
1079178354Ssam			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1080178354Ssam				(void) ieee80211_ref_node(ni);
1081178354Ssam			allocbs = 0;
1082178354Ssam		}
1083178354Ssam		/*
1084178354Ssam		 * Mark the node as referenced to reflect that it's
1085178354Ssam		 * reference count has been bumped to insure it remains
1086178354Ssam		 * after the transaction completes.
1087178354Ssam		 */
1088178354Ssam		ni->ni_flags |= IEEE80211_NODE_AREF;
1089186099Ssam		/*
1090186099Ssam		 * Mark the node as requiring a valid associatio id
1091186099Ssam		 * before outbound traffic is permitted.
1092186099Ssam		 */
1093186099Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1094178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1095192468Ssam		ni->ni_noise = nf;
1096178354Ssam		if (!ieee80211_alloc_challenge(ni)) {
1097178354Ssam			/* NB: don't return error so they rexmit */
1098178354Ssam			return;
1099178354Ssam		}
1100178354Ssam		get_random_bytes(ni->ni_challenge,
1101178354Ssam			IEEE80211_CHALLENGE_LEN);
1102178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1103178354Ssam		    ni, "shared key %sauth request", allocbs ? "" : "re");
1104178354Ssam		/*
1105178354Ssam		 * When the ACL policy is set to RADIUS we defer the
1106178354Ssam		 * authorization to a user agent.  Dispatch an event,
1107178354Ssam		 * a subsequent MLME call will decide the fate of the
1108178354Ssam		 * station.  If the user agent is not present then the
1109178354Ssam		 * node will be reclaimed due to inactivity.
1110178354Ssam		 */
1111178354Ssam		if (vap->iv_acl != NULL &&
1112178354Ssam		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1113178354Ssam			IEEE80211_NOTE_MAC(vap,
1114178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1115178354Ssam			    ni->ni_macaddr,
1116178354Ssam			    "%s", "station authentication defered (radius acl)");
1117178354Ssam			ieee80211_notify_node_auth(ni);
1118178354Ssam			return;
1119178354Ssam		}
1120178354Ssam		break;
1121178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1122178354Ssam		if (ni == vap->iv_bss) {
1123178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1124178354Ssam			    ni->ni_macaddr, "shared key response",
1125178354Ssam			    "%s", "unknown station");
1126178354Ssam			/* NB: don't send a response */
1127178354Ssam			return;
1128178354Ssam		}
1129178354Ssam		if (ni->ni_challenge == NULL) {
1130178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1131178354Ssam			    ni->ni_macaddr, "shared key response",
1132178354Ssam			    "%s", "no challenge recorded");
1133178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1134178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1135178354Ssam			goto bad;
1136178354Ssam		}
1137178354Ssam		if (memcmp(ni->ni_challenge, &challenge[2],
1138178354Ssam			   challenge[1]) != 0) {
1139178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1140178354Ssam			    ni->ni_macaddr, "shared key response",
1141178354Ssam			    "%s", "challenge mismatch");
1142178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1143178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1144178354Ssam			goto bad;
1145178354Ssam		}
1146178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1147178354Ssam		    ni, "%s", "station authenticated (shared key)");
1148178354Ssam		ieee80211_node_authorize(ni);
1149178354Ssam		break;
1150178354Ssam	default:
1151178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1152178354Ssam		    ni->ni_macaddr, "shared key auth",
1153178354Ssam		    "bad seq %d", seq);
1154178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1155178354Ssam		estatus = IEEE80211_STATUS_SEQUENCE;
1156178354Ssam		goto bad;
1157178354Ssam	}
1158178354Ssam	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1159178354Ssam	return;
1160178354Ssambad:
1161178354Ssam	/*
1162178354Ssam	 * Send an error response; but only when operating as an AP.
1163178354Ssam	 */
1164178354Ssam	/* XXX hack to workaround calling convention */
1165178354Ssam	ieee80211_send_error(ni, wh->i_addr2,
1166178354Ssam	    IEEE80211_FC0_SUBTYPE_AUTH,
1167178354Ssam	    (seq + 1) | (estatus<<16));
1168178354Ssam}
1169178354Ssam
1170178354Ssam/*
1171178354Ssam * Convert a WPA cipher selector OUI to an internal
1172178354Ssam * cipher algorithm.  Where appropriate we also
1173178354Ssam * record any key length.
1174178354Ssam */
1175178354Ssamstatic int
1176178354Ssamwpa_cipher(const uint8_t *sel, uint8_t *keylen)
1177178354Ssam{
1178178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1179178354Ssam	uint32_t w = LE_READ_4(sel);
1180178354Ssam
1181178354Ssam	switch (w) {
1182178354Ssam	case WPA_SEL(WPA_CSE_NULL):
1183178354Ssam		return IEEE80211_CIPHER_NONE;
1184178354Ssam	case WPA_SEL(WPA_CSE_WEP40):
1185178354Ssam		if (keylen)
1186178354Ssam			*keylen = 40 / NBBY;
1187178354Ssam		return IEEE80211_CIPHER_WEP;
1188178354Ssam	case WPA_SEL(WPA_CSE_WEP104):
1189178354Ssam		if (keylen)
1190178354Ssam			*keylen = 104 / NBBY;
1191178354Ssam		return IEEE80211_CIPHER_WEP;
1192178354Ssam	case WPA_SEL(WPA_CSE_TKIP):
1193178354Ssam		return IEEE80211_CIPHER_TKIP;
1194178354Ssam	case WPA_SEL(WPA_CSE_CCMP):
1195178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1196178354Ssam	}
1197178354Ssam	return 32;		/* NB: so 1<< is discarded */
1198178354Ssam#undef WPA_SEL
1199178354Ssam}
1200178354Ssam
1201178354Ssam/*
1202178354Ssam * Convert a WPA key management/authentication algorithm
1203178354Ssam * to an internal code.
1204178354Ssam */
1205178354Ssamstatic int
1206178354Ssamwpa_keymgmt(const uint8_t *sel)
1207178354Ssam{
1208178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1209178354Ssam	uint32_t w = LE_READ_4(sel);
1210178354Ssam
1211178354Ssam	switch (w) {
1212178354Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1213178354Ssam		return WPA_ASE_8021X_UNSPEC;
1214178354Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
1215178354Ssam		return WPA_ASE_8021X_PSK;
1216178354Ssam	case WPA_SEL(WPA_ASE_NONE):
1217178354Ssam		return WPA_ASE_NONE;
1218178354Ssam	}
1219178354Ssam	return 0;		/* NB: so is discarded */
1220178354Ssam#undef WPA_SEL
1221178354Ssam}
1222178354Ssam
1223178354Ssam/*
1224178354Ssam * Parse a WPA information element to collect parameters.
1225178354Ssam * Note that we do not validate security parameters; that
1226178354Ssam * is handled by the authenticator; the parsing done here
1227178354Ssam * is just for internal use in making operational decisions.
1228178354Ssam */
1229178354Ssamstatic int
1230178354Ssamieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1231178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1232178354Ssam{
1233178354Ssam	uint8_t len = frm[1];
1234178354Ssam	uint32_t w;
1235178354Ssam	int n;
1236178354Ssam
1237178354Ssam	/*
1238178354Ssam	 * Check the length once for fixed parts: OUI, type,
1239178354Ssam	 * version, mcast cipher, and 2 selector counts.
1240178354Ssam	 * Other, variable-length data, must be checked separately.
1241178354Ssam	 */
1242178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1243178354Ssam		IEEE80211_DISCARD_IE(vap,
1244178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1245178354Ssam		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1246178354Ssam		return IEEE80211_REASON_IE_INVALID;
1247178354Ssam	}
1248178354Ssam	if (len < 14) {
1249178354Ssam		IEEE80211_DISCARD_IE(vap,
1250178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1251178354Ssam		    wh, "WPA", "too short, len %u", len);
1252178354Ssam		return IEEE80211_REASON_IE_INVALID;
1253178354Ssam	}
1254178354Ssam	frm += 6, len -= 4;		/* NB: len is payload only */
1255178354Ssam	/* NB: iswapoui already validated the OUI and type */
1256178354Ssam	w = LE_READ_2(frm);
1257178354Ssam	if (w != WPA_VERSION) {
1258178354Ssam		IEEE80211_DISCARD_IE(vap,
1259178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1260178354Ssam		    wh, "WPA", "bad version %u", w);
1261178354Ssam		return IEEE80211_REASON_IE_INVALID;
1262178354Ssam	}
1263178354Ssam	frm += 2, len -= 2;
1264178354Ssam
1265178354Ssam	memset(rsn, 0, sizeof(*rsn));
1266178354Ssam
1267178354Ssam	/* multicast/group cipher */
1268178354Ssam	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1269178354Ssam	frm += 4, len -= 4;
1270178354Ssam
1271178354Ssam	/* unicast ciphers */
1272178354Ssam	n = LE_READ_2(frm);
1273178354Ssam	frm += 2, len -= 2;
1274178354Ssam	if (len < n*4+2) {
1275178354Ssam		IEEE80211_DISCARD_IE(vap,
1276178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1277178354Ssam		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1278178354Ssam		    len, n);
1279178354Ssam		return IEEE80211_REASON_IE_INVALID;
1280178354Ssam	}
1281178354Ssam	w = 0;
1282178354Ssam	for (; n > 0; n--) {
1283178354Ssam		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1284178354Ssam		frm += 4, len -= 4;
1285178354Ssam	}
1286178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1287178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1288178354Ssam	else
1289178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1290178354Ssam
1291178354Ssam	/* key management algorithms */
1292178354Ssam	n = LE_READ_2(frm);
1293178354Ssam	frm += 2, len -= 2;
1294178354Ssam	if (len < n*4) {
1295178354Ssam		IEEE80211_DISCARD_IE(vap,
1296178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1297178354Ssam		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1298178354Ssam		    len, n);
1299178354Ssam		return IEEE80211_REASON_IE_INVALID;
1300178354Ssam	}
1301178354Ssam	w = 0;
1302178354Ssam	for (; n > 0; n--) {
1303178354Ssam		w |= wpa_keymgmt(frm);
1304178354Ssam		frm += 4, len -= 4;
1305178354Ssam	}
1306178354Ssam	if (w & WPA_ASE_8021X_UNSPEC)
1307178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1308178354Ssam	else
1309178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1310178354Ssam
1311178354Ssam	if (len > 2)		/* optional capabilities */
1312178354Ssam		rsn->rsn_caps = LE_READ_2(frm);
1313178354Ssam
1314178354Ssam	return 0;
1315178354Ssam}
1316178354Ssam
1317178354Ssam/*
1318178354Ssam * Convert an RSN cipher selector OUI to an internal
1319178354Ssam * cipher algorithm.  Where appropriate we also
1320178354Ssam * record any key length.
1321178354Ssam */
1322178354Ssamstatic int
1323178354Ssamrsn_cipher(const uint8_t *sel, uint8_t *keylen)
1324178354Ssam{
1325178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1326178354Ssam	uint32_t w = LE_READ_4(sel);
1327178354Ssam
1328178354Ssam	switch (w) {
1329178354Ssam	case RSN_SEL(RSN_CSE_NULL):
1330178354Ssam		return IEEE80211_CIPHER_NONE;
1331178354Ssam	case RSN_SEL(RSN_CSE_WEP40):
1332178354Ssam		if (keylen)
1333178354Ssam			*keylen = 40 / NBBY;
1334178354Ssam		return IEEE80211_CIPHER_WEP;
1335178354Ssam	case RSN_SEL(RSN_CSE_WEP104):
1336178354Ssam		if (keylen)
1337178354Ssam			*keylen = 104 / NBBY;
1338178354Ssam		return IEEE80211_CIPHER_WEP;
1339178354Ssam	case RSN_SEL(RSN_CSE_TKIP):
1340178354Ssam		return IEEE80211_CIPHER_TKIP;
1341178354Ssam	case RSN_SEL(RSN_CSE_CCMP):
1342178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1343178354Ssam	case RSN_SEL(RSN_CSE_WRAP):
1344178354Ssam		return IEEE80211_CIPHER_AES_OCB;
1345178354Ssam	}
1346178354Ssam	return 32;		/* NB: so 1<< is discarded */
1347178354Ssam#undef WPA_SEL
1348178354Ssam}
1349178354Ssam
1350178354Ssam/*
1351178354Ssam * Convert an RSN key management/authentication algorithm
1352178354Ssam * to an internal code.
1353178354Ssam */
1354178354Ssamstatic int
1355178354Ssamrsn_keymgmt(const uint8_t *sel)
1356178354Ssam{
1357178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1358178354Ssam	uint32_t w = LE_READ_4(sel);
1359178354Ssam
1360178354Ssam	switch (w) {
1361178354Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1362178354Ssam		return RSN_ASE_8021X_UNSPEC;
1363178354Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
1364178354Ssam		return RSN_ASE_8021X_PSK;
1365178354Ssam	case RSN_SEL(RSN_ASE_NONE):
1366178354Ssam		return RSN_ASE_NONE;
1367178354Ssam	}
1368178354Ssam	return 0;		/* NB: so is discarded */
1369178354Ssam#undef RSN_SEL
1370178354Ssam}
1371178354Ssam
1372178354Ssam/*
1373178354Ssam * Parse a WPA/RSN information element to collect parameters
1374178354Ssam * and validate the parameters against what has been
1375178354Ssam * configured for the system.
1376178354Ssam */
1377178354Ssamstatic int
1378178354Ssamieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1379178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1380178354Ssam{
1381178354Ssam	uint8_t len = frm[1];
1382178354Ssam	uint32_t w;
1383178354Ssam	int n;
1384178354Ssam
1385178354Ssam	/*
1386178354Ssam	 * Check the length once for fixed parts:
1387178354Ssam	 * version, mcast cipher, and 2 selector counts.
1388178354Ssam	 * Other, variable-length data, must be checked separately.
1389178354Ssam	 */
1390178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1391178354Ssam		IEEE80211_DISCARD_IE(vap,
1392178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1393178354Ssam		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1394178354Ssam		return IEEE80211_REASON_IE_INVALID;
1395178354Ssam	}
1396178354Ssam	if (len < 10) {
1397178354Ssam		IEEE80211_DISCARD_IE(vap,
1398178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1399178354Ssam		    wh, "RSN", "too short, len %u", len);
1400178354Ssam		return IEEE80211_REASON_IE_INVALID;
1401178354Ssam	}
1402178354Ssam	frm += 2;
1403178354Ssam	w = LE_READ_2(frm);
1404178354Ssam	if (w != RSN_VERSION) {
1405178354Ssam		IEEE80211_DISCARD_IE(vap,
1406178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1407178354Ssam		    wh, "RSN", "bad version %u", w);
1408178354Ssam		return IEEE80211_REASON_IE_INVALID;
1409178354Ssam	}
1410178354Ssam	frm += 2, len -= 2;
1411178354Ssam
1412178354Ssam	memset(rsn, 0, sizeof(*rsn));
1413178354Ssam
1414178354Ssam	/* multicast/group cipher */
1415178354Ssam	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1416178354Ssam	frm += 4, len -= 4;
1417178354Ssam
1418178354Ssam	/* unicast ciphers */
1419178354Ssam	n = LE_READ_2(frm);
1420178354Ssam	frm += 2, len -= 2;
1421178354Ssam	if (len < n*4+2) {
1422178354Ssam		IEEE80211_DISCARD_IE(vap,
1423178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1424178354Ssam		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1425178354Ssam		    len, n);
1426178354Ssam		return IEEE80211_REASON_IE_INVALID;
1427178354Ssam	}
1428178354Ssam	w = 0;
1429178354Ssam	for (; n > 0; n--) {
1430178354Ssam		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1431178354Ssam		frm += 4, len -= 4;
1432178354Ssam	}
1433178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1434178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1435178354Ssam	else
1436178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1437178354Ssam
1438178354Ssam	/* key management algorithms */
1439178354Ssam	n = LE_READ_2(frm);
1440178354Ssam	frm += 2, len -= 2;
1441178354Ssam	if (len < n*4) {
1442178354Ssam		IEEE80211_DISCARD_IE(vap,
1443178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1444178354Ssam		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1445178354Ssam		    len, n);
1446178354Ssam		return IEEE80211_REASON_IE_INVALID;
1447178354Ssam	}
1448178354Ssam	w = 0;
1449178354Ssam	for (; n > 0; n--) {
1450178354Ssam		w |= rsn_keymgmt(frm);
1451178354Ssam		frm += 4, len -= 4;
1452178354Ssam	}
1453178354Ssam	if (w & RSN_ASE_8021X_UNSPEC)
1454178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1455178354Ssam	else
1456178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1457178354Ssam
1458178354Ssam	/* optional RSN capabilities */
1459178354Ssam	if (len > 2)
1460178354Ssam		rsn->rsn_caps = LE_READ_2(frm);
1461178354Ssam	/* XXXPMKID */
1462178354Ssam
1463178354Ssam	return 0;
1464178354Ssam}
1465178354Ssam
1466178354Ssam/*
1467178354Ssam * WPA/802.11i assocation request processing.
1468178354Ssam */
1469178354Ssamstatic int
1470178354Ssamwpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1471178354Ssam	const struct ieee80211_frame *wh, const uint8_t *wpa,
1472178354Ssam	const uint8_t *rsn, uint16_t capinfo)
1473178354Ssam{
1474178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1475178354Ssam	uint8_t reason;
1476178354Ssam	int badwparsn;
1477178354Ssam
1478178354Ssam	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1479178354Ssam	if (wpa == NULL && rsn == NULL) {
1480178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1481178354Ssam			/*
1482178354Ssam			 * W-Fi Protected Setup (WPS) permits
1483178354Ssam			 * clients to associate and pass EAPOL frames
1484178354Ssam			 * to establish initial credentials.
1485178354Ssam			 */
1486178354Ssam			ni->ni_flags |= IEEE80211_NODE_WPS;
1487178354Ssam			return 1;
1488178354Ssam		}
1489178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1490178354Ssam		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1491178354Ssam			/*
1492178354Ssam			 * Transitional Security Network.  Permits clients
1493178354Ssam			 * to associate and use WEP while WPA is configured.
1494178354Ssam			 */
1495178354Ssam			ni->ni_flags |= IEEE80211_NODE_TSN;
1496178354Ssam			return 1;
1497178354Ssam		}
1498178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1499178354Ssam		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1500178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1501178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1502178354Ssam		goto bad;
1503178354Ssam	}
1504178354Ssam	/* assert right association security credentials */
1505178354Ssam	badwparsn = 0;			/* NB: to silence compiler */
1506178354Ssam	switch (vap->iv_flags & IEEE80211_F_WPA) {
1507178354Ssam	case IEEE80211_F_WPA1:
1508178354Ssam		badwparsn = (wpa == NULL);
1509178354Ssam		break;
1510178354Ssam	case IEEE80211_F_WPA2:
1511178354Ssam		badwparsn = (rsn == NULL);
1512178354Ssam		break;
1513178354Ssam	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1514178354Ssam		badwparsn = (wpa == NULL && rsn == NULL);
1515178354Ssam		break;
1516178354Ssam	}
1517178354Ssam	if (badwparsn) {
1518178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1519178354Ssam		    wh, NULL,
1520178354Ssam		    "%s", "missing WPA/RSN IE in association request");
1521178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1522178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1523178354Ssam		goto bad;
1524178354Ssam	}
1525178354Ssam	/*
1526178354Ssam	 * Parse WPA/RSN information element.
1527178354Ssam	 */
1528178354Ssam	if (wpa != NULL)
1529178354Ssam		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1530178354Ssam	else
1531178354Ssam		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1532178354Ssam	if (reason != 0) {
1533178354Ssam		/* XXX distinguish WPA/RSN? */
1534178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1535178354Ssam		goto bad;
1536178354Ssam	}
1537178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1538178354Ssam	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1539178354Ssam	    wpa != NULL ? "WPA" : "RSN",
1540178354Ssam	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1541178354Ssam	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1542178354Ssam	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1543178354Ssam
1544178354Ssam	return 1;
1545178354Ssambad:
1546178354Ssam	ieee80211_node_deauth(ni, reason);
1547178354Ssam	return 0;
1548178354Ssam}
1549178354Ssam
1550178354Ssam/* XXX find a better place for definition */
1551178354Ssamstruct l2_update_frame {
1552178354Ssam	struct ether_header eh;
1553178354Ssam	uint8_t dsap;
1554178354Ssam	uint8_t ssap;
1555178354Ssam	uint8_t control;
1556178354Ssam	uint8_t xid[3];
1557178354Ssam}  __packed;
1558178354Ssam
1559178354Ssam/*
1560178354Ssam * Deliver a TGf L2UF frame on behalf of a station.
1561178354Ssam * This primes any bridge when the station is roaming
1562178354Ssam * between ap's on the same wired network.
1563178354Ssam */
1564178354Ssamstatic void
1565178354Ssamieee80211_deliver_l2uf(struct ieee80211_node *ni)
1566178354Ssam{
1567178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1568178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1569178354Ssam	struct mbuf *m;
1570178354Ssam	struct l2_update_frame *l2uf;
1571178354Ssam	struct ether_header *eh;
1572178354Ssam
1573178354Ssam	m = m_gethdr(M_NOWAIT, MT_DATA);
1574178354Ssam	if (m == NULL) {
1575178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1576178354Ssam		    "%s", "no mbuf for l2uf frame");
1577178354Ssam		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1578178354Ssam		return;
1579178354Ssam	}
1580178354Ssam	l2uf = mtod(m, struct l2_update_frame *);
1581178354Ssam	eh = &l2uf->eh;
1582178354Ssam	/* dst: Broadcast address */
1583178354Ssam	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1584178354Ssam	/* src: associated STA */
1585178354Ssam	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1586178354Ssam	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1587178354Ssam
1588178354Ssam	l2uf->dsap = 0;
1589178354Ssam	l2uf->ssap = 0;
1590178354Ssam	l2uf->control = 0xf5;
1591178354Ssam	l2uf->xid[0] = 0x81;
1592178354Ssam	l2uf->xid[1] = 0x80;
1593178354Ssam	l2uf->xid[2] = 0x00;
1594178354Ssam
1595178354Ssam	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1596178354Ssam	hostap_deliver_data(vap, ni, m);
1597178354Ssam}
1598178354Ssam
1599178354Ssamstatic void
1600178354Ssamratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1601178354Ssam	int reassoc, int resp, const char *tag, int rate)
1602178354Ssam{
1603178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1604178354Ssam	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1605178354Ssam	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1606178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1607178354Ssam	ieee80211_node_leave(ni);
1608178354Ssam}
1609178354Ssam
1610178354Ssamstatic void
1611178354Ssamcapinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1612178354Ssam	int reassoc, int resp, const char *tag, int capinfo)
1613178354Ssam{
1614178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1615178354Ssam
1616178354Ssam	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1617178354Ssam	    "deny %s request, %s mismatch 0x%x",
1618178354Ssam	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1619178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1620178354Ssam	ieee80211_node_leave(ni);
1621178354Ssam	vap->iv_stats.is_rx_assoc_capmismatch++;
1622178354Ssam}
1623178354Ssam
1624178354Ssamstatic void
1625178354Ssamhtcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1626178354Ssam	int reassoc, int resp)
1627178354Ssam{
1628178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1629178354Ssam	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1630178354Ssam	/* XXX no better code */
1631193543Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1632178354Ssam	ieee80211_node_leave(ni);
1633178354Ssam}
1634178354Ssam
1635178354Ssamstatic void
1636178354Ssamauthalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1637178354Ssam	int algo, int seq, int status)
1638178354Ssam{
1639178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1640178354Ssam
1641178354Ssam	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1642178354Ssam	    wh, NULL, "unsupported alg %d", algo);
1643178354Ssam	vap->iv_stats.is_rx_auth_unsupported++;
1644178354Ssam	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1645178354Ssam	    seq | (status << 16));
1646178354Ssam}
1647178354Ssam
1648178354Ssamstatic __inline int
1649178354Ssamishtmixed(const uint8_t *ie)
1650178354Ssam{
1651178354Ssam	const struct ieee80211_ie_htinfo *ht =
1652178354Ssam	    (const struct ieee80211_ie_htinfo *) ie;
1653178354Ssam	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1654178354Ssam	    IEEE80211_HTINFO_OPMODE_MIXED;
1655178354Ssam}
1656178354Ssam
1657178354Ssamstatic int
1658178354Ssamis11bclient(const uint8_t *rates, const uint8_t *xrates)
1659178354Ssam{
1660178354Ssam	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1661178354Ssam	int i;
1662178354Ssam
1663178354Ssam	/* NB: the 11b clients we care about will not have xrates */
1664178354Ssam	if (xrates != NULL || rates == NULL)
1665178354Ssam		return 0;
1666178354Ssam	for (i = 0; i < rates[1]; i++) {
1667178354Ssam		int r = rates[2+i] & IEEE80211_RATE_VAL;
1668178354Ssam		if (r > 2*11 || ((1<<r) & brates) == 0)
1669178354Ssam			return 0;
1670178354Ssam	}
1671178354Ssam	return 1;
1672178354Ssam}
1673178354Ssam
1674178354Ssamstatic void
1675178354Ssamhostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1676192468Ssam	int subtype, int rssi, int nf)
1677178354Ssam{
1678178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1679178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1680178354Ssam	struct ieee80211_frame *wh;
1681178354Ssam	uint8_t *frm, *efrm, *sfrm;
1682178354Ssam	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1683178354Ssam	int reassoc, resp;
1684178354Ssam	uint8_t rate;
1685178354Ssam
1686178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1687178354Ssam	frm = (uint8_t *)&wh[1];
1688178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1689178354Ssam	switch (subtype) {
1690178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1691178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON: {
1692178354Ssam		struct ieee80211_scanparams scan;
1693178354Ssam		/*
1694178354Ssam		 * We process beacon/probe response frames when scanning;
1695178354Ssam		 * otherwise we check beacon frames for overlapping non-ERP
1696178354Ssam		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1697178354Ssam		 */
1698178354Ssam		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1699178354Ssam		    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1700178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1701178354Ssam			return;
1702178354Ssam		}
1703178354Ssam		/* NB: accept off-channel frames */
1704178354Ssam		if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1705178354Ssam			return;
1706178354Ssam		/*
1707178354Ssam		 * Count frame now that we know it's to be processed.
1708178354Ssam		 */
1709178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1710178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1711178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1712178354Ssam		} else
1713178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1714178354Ssam		/*
1715178354Ssam		 * If scanning, just pass information to the scan module.
1716178354Ssam		 */
1717178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1718178354Ssam			if (scan.status == 0 &&		/* NB: on channel */
1719178354Ssam			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1720178354Ssam				/*
1721178354Ssam				 * Actively scanning a channel marked passive;
1722178354Ssam				 * send a probe request now that we know there
1723178354Ssam				 * is 802.11 traffic present.
1724178354Ssam				 *
1725178354Ssam				 * XXX check if the beacon we recv'd gives
1726178354Ssam				 * us what we need and suppress the probe req
1727178354Ssam				 */
1728178354Ssam				ieee80211_probe_curchan(vap, 1);
1729178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1730178354Ssam			}
1731192468Ssam			ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1732178354Ssam			return;
1733178354Ssam		}
1734178354Ssam		/*
1735178354Ssam		 * Check beacon for overlapping bss w/ non ERP stations.
1736178354Ssam		 * If we detect one and protection is configured but not
1737178354Ssam		 * enabled, enable it and start a timer that'll bring us
1738178354Ssam		 * out if we stop seeing the bss.
1739178354Ssam		 */
1740178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1741178354Ssam		    scan.status == 0 &&			/* NB: on-channel */
1742178354Ssam		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1743178354Ssam		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1744178354Ssam			ic->ic_lastnonerp = ticks;
1745178354Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1746178354Ssam			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1747178354Ssam			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1748178354Ssam				IEEE80211_NOTE_FRAME(vap,
1749178354Ssam				    IEEE80211_MSG_ASSOC, wh,
1750178354Ssam				    "non-ERP present on channel %d "
1751178354Ssam				    "(saw erp 0x%x from channel %d), "
1752178354Ssam				    "enable use of protection",
1753178354Ssam				    ic->ic_curchan->ic_ieee,
1754178354Ssam				    scan.erp, scan.chan);
1755178354Ssam				ic->ic_flags |= IEEE80211_F_USEPROT;
1756178354Ssam				ieee80211_notify_erp(ic);
1757178354Ssam			}
1758178354Ssam		}
1759178354Ssam		/*
1760178354Ssam		 * Check beacon for non-HT station on HT channel
1761178354Ssam		 * and update HT BSS occupancy as appropriate.
1762178354Ssam		 */
1763178354Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1764178354Ssam			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1765178354Ssam				/*
1766178354Ssam				 * Off control channel; only check frames
1767178354Ssam				 * that come in the extension channel when
1768178354Ssam				 * operating w/ HT40.
1769178354Ssam				 */
1770178354Ssam				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1771178354Ssam					break;
1772178354Ssam				if (scan.chan != ic->ic_curchan->ic_extieee)
1773178354Ssam					break;
1774178354Ssam			}
1775178354Ssam			if (scan.htinfo == NULL) {
1776178354Ssam				ieee80211_htprot_update(ic,
1777178354Ssam				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1778178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1779178354Ssam			} else if (ishtmixed(scan.htinfo)) {
1780178354Ssam				/* XXX? take NONHT_PRESENT from beacon? */
1781178354Ssam				ieee80211_htprot_update(ic,
1782178354Ssam				    IEEE80211_HTINFO_OPMODE_MIXED |
1783178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1784178354Ssam			}
1785178354Ssam		}
1786178354Ssam		break;
1787178354Ssam	}
1788178354Ssam
1789178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1790178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1791178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1792178354Ssam			return;
1793178354Ssam		}
1794178354Ssam		/*
1795178354Ssam		 * prreq frame format
1796178354Ssam		 *	[tlv] ssid
1797178354Ssam		 *	[tlv] supported rates
1798178354Ssam		 *	[tlv] extended supported rates
1799178354Ssam		 */
1800178354Ssam		ssid = rates = xrates = NULL;
1801178354Ssam		sfrm = frm;
1802178354Ssam		while (efrm - frm > 1) {
1803178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1804178354Ssam			switch (*frm) {
1805178354Ssam			case IEEE80211_ELEMID_SSID:
1806178354Ssam				ssid = frm;
1807178354Ssam				break;
1808178354Ssam			case IEEE80211_ELEMID_RATES:
1809178354Ssam				rates = frm;
1810178354Ssam				break;
1811178354Ssam			case IEEE80211_ELEMID_XRATES:
1812178354Ssam				xrates = frm;
1813178354Ssam				break;
1814178354Ssam			}
1815178354Ssam			frm += frm[1] + 2;
1816178354Ssam		}
1817178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1818178354Ssam		if (xrates != NULL)
1819178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1820178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1821178354Ssam		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1822178354Ssam		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1823178354Ssam		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1824178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1825178354Ssam			    wh, NULL,
1826178354Ssam			    "%s", "no ssid with ssid suppression enabled");
1827178354Ssam			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1828178354Ssam			return;
1829178354Ssam		}
1830178354Ssam
1831178354Ssam		/* XXX find a better class or define it's own */
1832178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1833178354Ssam		    "%s", "recv probe req");
1834178354Ssam		/*
1835178354Ssam		 * Some legacy 11b clients cannot hack a complete
1836178354Ssam		 * probe response frame.  When the request includes
1837178354Ssam		 * only a bare-bones rate set, communicate this to
1838178354Ssam		 * the transmit side.
1839178354Ssam		 */
1840178354Ssam		ieee80211_send_proberesp(vap, wh->i_addr2,
1841178354Ssam		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1842178354Ssam		break;
1843178354Ssam
1844178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH: {
1845178354Ssam		uint16_t algo, seq, status;
1846178354Ssam
1847178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1848178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1849178354Ssam			return;
1850178354Ssam		}
1851178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1852178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1853178354Ssam			    wh, NULL, "%s", "wrong bssid");
1854178354Ssam			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1855178354Ssam			return;
1856178354Ssam		}
1857178354Ssam		/*
1858178354Ssam		 * auth frame format
1859178354Ssam		 *	[2] algorithm
1860178354Ssam		 *	[2] sequence
1861178354Ssam		 *	[2] status
1862178354Ssam		 *	[tlv*] challenge
1863178354Ssam		 */
1864178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1865178354Ssam		algo   = le16toh(*(uint16_t *)frm);
1866178354Ssam		seq    = le16toh(*(uint16_t *)(frm + 2));
1867178354Ssam		status = le16toh(*(uint16_t *)(frm + 4));
1868178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1869178354Ssam		    "recv auth frame with algorithm %d seq %d", algo, seq);
1870178354Ssam		/*
1871178354Ssam		 * Consult the ACL policy module if setup.
1872178354Ssam		 */
1873178354Ssam		if (vap->iv_acl != NULL &&
1874178354Ssam		    !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1875178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1876178354Ssam			    wh, NULL, "%s", "disallowed by ACL");
1877178354Ssam			vap->iv_stats.is_rx_acl++;
1878178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1879178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH,
1880178354Ssam			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1881178354Ssam			return;
1882178354Ssam		}
1883178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1884178354Ssam			IEEE80211_DISCARD(vap,
1885178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1886178354Ssam			    wh, NULL, "%s", "TKIP countermeasures enabled");
1887178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1888178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1889178354Ssam				IEEE80211_FC0_SUBTYPE_AUTH,
1890178354Ssam				IEEE80211_REASON_MIC_FAILURE);
1891178354Ssam			return;
1892178354Ssam		}
1893178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1894192468Ssam			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1895192468Ssam			    seq, status);
1896178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1897192468Ssam			hostap_auth_open(ni, wh, rssi, nf, seq, status);
1898178354Ssam		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1899178354Ssam			authalgreject(ni, wh, algo,
1900178354Ssam			    seq+1, IEEE80211_STATUS_ALG);
1901178354Ssam			return;
1902178354Ssam		} else {
1903178354Ssam			/*
1904178354Ssam			 * We assume that an unknown algorithm is the result
1905178354Ssam			 * of a decryption failure on a shared key auth frame;
1906178354Ssam			 * return a status code appropriate for that instead
1907178354Ssam			 * of IEEE80211_STATUS_ALG.
1908178354Ssam			 *
1909178354Ssam			 * NB: a seq# of 4 is intentional; the decrypted
1910178354Ssam			 *     frame likely has a bogus seq value.
1911178354Ssam			 */
1912178354Ssam			authalgreject(ni, wh, algo,
1913178354Ssam			    4, IEEE80211_STATUS_CHALLENGE);
1914178354Ssam			return;
1915178354Ssam		}
1916178354Ssam		break;
1917178354Ssam	}
1918178354Ssam
1919178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1920178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1921178354Ssam		uint16_t capinfo, lintval;
1922178354Ssam		struct ieee80211_rsnparms rsnparms;
1923178354Ssam
1924178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1925178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1926178354Ssam			return;
1927178354Ssam		}
1928178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1929178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1930178354Ssam			    wh, NULL, "%s", "wrong bssid");
1931178354Ssam			vap->iv_stats.is_rx_assoc_bss++;
1932178354Ssam			return;
1933178354Ssam		}
1934178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1935178354Ssam			reassoc = 1;
1936178354Ssam			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1937178354Ssam		} else {
1938178354Ssam			reassoc = 0;
1939178354Ssam			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1940178354Ssam		}
1941178354Ssam		if (ni == vap->iv_bss) {
1942178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1943178354Ssam			    "deny %s request, sta not authenticated",
1944178354Ssam			    reassoc ? "reassoc" : "assoc");
1945178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1946178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1947178354Ssam			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1948178354Ssam			vap->iv_stats.is_rx_assoc_notauth++;
1949178354Ssam			return;
1950178354Ssam		}
1951178354Ssam
1952178354Ssam		/*
1953178354Ssam		 * asreq frame format
1954178354Ssam		 *	[2] capability information
1955178354Ssam		 *	[2] listen interval
1956178354Ssam		 *	[6*] current AP address (reassoc only)
1957178354Ssam		 *	[tlv] ssid
1958178354Ssam		 *	[tlv] supported rates
1959178354Ssam		 *	[tlv] extended supported rates
1960178354Ssam		 *	[tlv] WPA or RSN
1961178354Ssam		 *	[tlv] HT capabilities
1962178354Ssam		 *	[tlv] Atheros capabilities
1963178354Ssam		 */
1964178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1965178354Ssam		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1966178354Ssam		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1967178354Ssam		if (reassoc)
1968178354Ssam			frm += 6;	/* ignore current AP info */
1969178354Ssam		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1970178354Ssam		sfrm = frm;
1971178354Ssam		while (efrm - frm > 1) {
1972178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1973178354Ssam			switch (*frm) {
1974178354Ssam			case IEEE80211_ELEMID_SSID:
1975178354Ssam				ssid = frm;
1976178354Ssam				break;
1977178354Ssam			case IEEE80211_ELEMID_RATES:
1978178354Ssam				rates = frm;
1979178354Ssam				break;
1980178354Ssam			case IEEE80211_ELEMID_XRATES:
1981178354Ssam				xrates = frm;
1982178354Ssam				break;
1983178354Ssam			case IEEE80211_ELEMID_RSN:
1984178354Ssam				rsn = frm;
1985178354Ssam				break;
1986178354Ssam			case IEEE80211_ELEMID_HTCAP:
1987178354Ssam				htcap = frm;
1988178354Ssam				break;
1989178354Ssam			case IEEE80211_ELEMID_VENDOR:
1990178354Ssam				if (iswpaoui(frm))
1991178354Ssam					wpa = frm;
1992178354Ssam				else if (iswmeinfo(frm))
1993178354Ssam					wme = frm;
1994190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1995178354Ssam				else if (isatherosoui(frm))
1996178354Ssam					ath = frm;
1997190391Ssam#endif
1998193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1999178354Ssam					if (ishtcapoui(frm) && htcap == NULL)
2000178354Ssam						htcap = frm;
2001178354Ssam				}
2002178354Ssam				break;
2003178354Ssam			}
2004178354Ssam			frm += frm[1] + 2;
2005178354Ssam		}
2006178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2007178354Ssam		if (xrates != NULL)
2008178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
2009178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
2010178354Ssam		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2011178354Ssam		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2012178354Ssam		if (htcap != NULL) {
2013178354Ssam			IEEE80211_VERIFY_LENGTH(htcap[1],
2014178354Ssam			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2015178354Ssam			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2016178354Ssam			         sizeof(struct ieee80211_ie_htcap)-2,
2017178354Ssam			     return);		/* XXX just NULL out? */
2018178354Ssam		}
2019178354Ssam
2020178354Ssam		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2021178354Ssam		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2022178354Ssam			return;
2023178354Ssam		/* discard challenge after association */
2024178354Ssam		if (ni->ni_challenge != NULL) {
2025186302Ssam			free(ni->ni_challenge, M_80211_NODE);
2026178354Ssam			ni->ni_challenge = NULL;
2027178354Ssam		}
2028178354Ssam		/* NB: 802.11 spec says to ignore station's privacy bit */
2029178354Ssam		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2030178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2031178354Ssam			    "capability", capinfo);
2032178354Ssam			return;
2033178354Ssam		}
2034178354Ssam		/*
2035178354Ssam		 * Disallow re-associate w/ invalid slot time setting.
2036178354Ssam		 */
2037178354Ssam		if (ni->ni_associd != 0 &&
2038178354Ssam		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2039178354Ssam		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2040178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2041178354Ssam			    "slot time", capinfo);
2042178354Ssam			return;
2043178354Ssam		}
2044178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
2045178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2046178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2047178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
2048178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2049178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2050178354Ssam			return;
2051178354Ssam		}
2052178354Ssam		/*
2053178354Ssam		 * If constrained to 11g-only stations reject an
2054178354Ssam		 * 11b-only station.  We cheat a bit here by looking
2055178354Ssam		 * at the max negotiated xmit rate and assuming anyone
2056178354Ssam		 * with a best rate <24Mb/s is an 11b station.
2057178354Ssam		 */
2058178354Ssam		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2059178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2060178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2061178354Ssam			return;
2062178354Ssam		}
2063178354Ssam		/*
2064178354Ssam		 * Do HT rate set handling and setup HT node state.
2065178354Ssam		 */
2066178354Ssam		ni->ni_chan = vap->iv_bss->ni_chan;
2067178354Ssam		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2068178354Ssam			rate = ieee80211_setup_htrates(ni, htcap,
2069178354Ssam				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2070178354Ssam				IEEE80211_F_DOBRS);
2071178354Ssam			if (rate & IEEE80211_RATE_BASIC) {
2072178354Ssam				ratesetmismatch(ni, wh, reassoc, resp,
2073178354Ssam				    "HT", rate);
2074178354Ssam				vap->iv_stats.is_ht_assoc_norate++;
2075178354Ssam				return;
2076178354Ssam			}
2077183254Ssam			ieee80211_ht_node_init(ni);
2078183254Ssam			ieee80211_ht_updatehtcap(ni, htcap);
2079178354Ssam		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2080178354Ssam			ieee80211_ht_node_cleanup(ni);
2081190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2082190579Ssam		else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2083190579Ssam			ieee80211_ff_node_cleanup(ni);
2084190579Ssam#endif
2085178354Ssam		/*
2086178354Ssam		 * Allow AMPDU operation only with unencrypted traffic
2087178354Ssam		 * or AES-CCM; the 11n spec only specifies these ciphers
2088178354Ssam		 * so permitting any others is undefined and can lead
2089178354Ssam		 * to interoperability problems.
2090178354Ssam		 */
2091178354Ssam		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2092178354Ssam		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2093178354Ssam		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2094178354Ssam		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2095178354Ssam			IEEE80211_NOTE(vap,
2096178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2097178354Ssam			    "disallow HT use because WEP or TKIP requested, "
2098178354Ssam			    "capinfo 0x%x ucastcipher %d", capinfo,
2099178354Ssam			    rsnparms.rsn_ucastcipher);
2100178354Ssam			ieee80211_ht_node_cleanup(ni);
2101178354Ssam			vap->iv_stats.is_ht_assoc_downgrade++;
2102178354Ssam		}
2103178354Ssam		/*
2104178354Ssam		 * If constrained to 11n-only stations reject legacy stations.
2105178354Ssam		 */
2106193655Ssam		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2107178354Ssam		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2108178354Ssam			htcapmismatch(ni, wh, reassoc, resp);
2109178354Ssam			vap->iv_stats.is_ht_assoc_nohtcap++;
2110178354Ssam			return;
2111178354Ssam		}
2112178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2113192468Ssam		ni->ni_noise = nf;
2114178354Ssam		ni->ni_intval = lintval;
2115178354Ssam		ni->ni_capinfo = capinfo;
2116178354Ssam		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2117178354Ssam		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2118178354Ssam		/*
2119178354Ssam		 * Store the IEs.
2120178354Ssam		 * XXX maybe better to just expand
2121178354Ssam		 */
2122178354Ssam		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2123178354Ssam#define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2124178354Ssam			if (wpa != NULL)
2125178354Ssam				setie(wpa_ie, wpa - sfrm);
2126178354Ssam			if (rsn != NULL)
2127178354Ssam				setie(rsn_ie, rsn - sfrm);
2128178354Ssam			if (htcap != NULL)
2129178354Ssam				setie(htcap_ie, htcap - sfrm);
2130178354Ssam			if (wme != NULL) {
2131178354Ssam				setie(wme_ie, wme - sfrm);
2132178354Ssam				/*
2133178354Ssam				 * Mark node as capable of QoS.
2134178354Ssam				 */
2135178354Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
2136178354Ssam			} else
2137178354Ssam				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2138190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2139178354Ssam			if (ath != NULL) {
2140178354Ssam				setie(ath_ie, ath - sfrm);
2141178354Ssam				/*
2142178354Ssam				 * Parse ATH station parameters.
2143178354Ssam				 */
2144178354Ssam				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2145178354Ssam			} else
2146190391Ssam#endif
2147178354Ssam				ni->ni_ath_flags = 0;
2148178354Ssam#undef setie
2149178354Ssam		} else {
2150178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2151178354Ssam			ni->ni_ath_flags = 0;
2152178354Ssam		}
2153178354Ssam		ieee80211_node_join(ni, resp);
2154178354Ssam		ieee80211_deliver_l2uf(ni);
2155178354Ssam		break;
2156178354Ssam	}
2157178354Ssam
2158178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2159178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2160178354Ssam		uint16_t reason;
2161178354Ssam
2162178354Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
2163178354Ssam		    /* NB: can happen when in promiscuous mode */
2164178354Ssam		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2165178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2166178354Ssam			break;
2167178354Ssam		}
2168178354Ssam		/*
2169178354Ssam		 * deauth/disassoc frame format
2170178354Ssam		 *	[2] reason
2171178354Ssam		 */
2172178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2173178354Ssam		reason = le16toh(*(uint16_t *)frm);
2174178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2175178354Ssam			vap->iv_stats.is_rx_deauth++;
2176178354Ssam			IEEE80211_NODE_STAT(ni, rx_deauth);
2177178354Ssam		} else {
2178178354Ssam			vap->iv_stats.is_rx_disassoc++;
2179178354Ssam			IEEE80211_NODE_STAT(ni, rx_disassoc);
2180178354Ssam		}
2181178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2182178354Ssam		    "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2183178354Ssam			IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2184178354Ssam		if (ni != vap->iv_bss)
2185178354Ssam			ieee80211_node_leave(ni);
2186178354Ssam		break;
2187178354Ssam	}
2188178354Ssam
2189178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
2190178354Ssam		if (vap->iv_state == IEEE80211_S_RUN) {
2191178354Ssam			if (ieee80211_parse_action(ni, m0) == 0)
2192178354Ssam				ic->ic_recv_action(ni, frm, efrm);
2193178354Ssam		} else
2194178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2195178354Ssam		break;
2196178354Ssam
2197178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2198178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2199178354Ssam	default:
2200178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2201178354Ssam		     wh, "mgt", "subtype 0x%x not handled", subtype);
2202178354Ssam		vap->iv_stats.is_rx_badsubtype++;
2203178354Ssam		break;
2204178354Ssam	}
2205178354Ssam}
2206178354Ssam
2207191546Ssamstatic void
2208191546Ssamhostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2209191546Ssam{
2210191546Ssam	switch (subtype) {
2211191546Ssam	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2212191546Ssam		hostap_recv_pspoll(ni, m);
2213191546Ssam		break;
2214191546Ssam	case IEEE80211_FC0_SUBTYPE_BAR:
2215191546Ssam		ieee80211_recv_bar(ni, m);
2216191546Ssam		break;
2217191546Ssam	}
2218191546Ssam}
2219191546Ssam
2220178354Ssam/*
2221178354Ssam * Process a received ps-poll frame.
2222178354Ssam */
2223178354Ssamstatic void
2224178354Ssamhostap_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2225178354Ssam{
2226178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2227178354Ssam	struct ieee80211_frame_min *wh;
2228184288Ssam	struct ifnet *ifp;
2229178354Ssam	struct mbuf *m;
2230178354Ssam	uint16_t aid;
2231178354Ssam	int qlen;
2232178354Ssam
2233178354Ssam	wh = mtod(m0, struct ieee80211_frame_min *);
2234178354Ssam	if (ni->ni_associd == 0) {
2235178354Ssam		IEEE80211_DISCARD(vap,
2236178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2237178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2238178354Ssam		    "%s", "unassociated station");
2239178354Ssam		vap->iv_stats.is_ps_unassoc++;
2240178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2241178354Ssam			IEEE80211_REASON_NOT_ASSOCED);
2242178354Ssam		return;
2243178354Ssam	}
2244178354Ssam
2245178354Ssam	aid = le16toh(*(uint16_t *)wh->i_dur);
2246178354Ssam	if (aid != ni->ni_associd) {
2247178354Ssam		IEEE80211_DISCARD(vap,
2248178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2249178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2250178354Ssam		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2251178354Ssam		    ni->ni_associd, aid);
2252178354Ssam		vap->iv_stats.is_ps_badaid++;
2253180837Ssam		/*
2254180837Ssam		 * NB: We used to deauth the station but it turns out
2255180837Ssam		 * the Blackberry Curve 8230 (and perhaps other devices)
2256180837Ssam		 * sometimes send the wrong AID when WME is negotiated.
2257180837Ssam		 * Being more lenient here seems ok as we already check
2258180837Ssam		 * the station is associated and we only return frames
2259180837Ssam		 * queued for the station (i.e. we don't use the AID).
2260180837Ssam		 */
2261178354Ssam		return;
2262178354Ssam	}
2263178354Ssam
2264178354Ssam	/* Okay, take the first queued packet and put it out... */
2265184288Ssam	m = ieee80211_node_psq_dequeue(ni, &qlen);
2266178354Ssam	if (m == NULL) {
2267178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2268178354Ssam		    "%s", "recv ps-poll, but queue empty");
2269178354Ssam		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2270178354Ssam		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2271178354Ssam		if (vap->iv_set_tim != NULL)
2272178354Ssam			vap->iv_set_tim(ni, 0);	/* just in case */
2273178354Ssam		return;
2274178354Ssam	}
2275178354Ssam	/*
2276178354Ssam	 * If there are more packets, set the more packets bit
2277178354Ssam	 * in the packet dispatched to the station; otherwise
2278178354Ssam	 * turn off the TIM bit.
2279178354Ssam	 */
2280178354Ssam	if (qlen != 0) {
2281178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2282178354Ssam		    "recv ps-poll, send packet, %u still queued", qlen);
2283178354Ssam		m->m_flags |= M_MORE_DATA;
2284178354Ssam	} else {
2285178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2286178354Ssam		    "%s", "recv ps-poll, send packet, queue empty");
2287178354Ssam		if (vap->iv_set_tim != NULL)
2288178354Ssam			vap->iv_set_tim(ni, 0);
2289178354Ssam	}
2290178354Ssam	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2291184288Ssam
2292184288Ssam	if (m->m_flags & M_ENCAP)
2293184288Ssam		ifp = vap->iv_ic->ic_ifp;
2294184288Ssam	else
2295184288Ssam		ifp = vap->iv_ifp;
2296178354Ssam	IF_ENQUEUE(&ifp->if_snd, m);
2297178354Ssam	if_start(ifp);
2298178354Ssam}
2299