ieee80211_hostap.c revision 298376
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 298376 2016-04-20 21:15:55Z avos $");
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>
51257176Sglebius#include <net/if_var.h>
52178354Ssam#include <net/if_media.h>
53178354Ssam#include <net/if_llc.h>
54178354Ssam#include <net/ethernet.h>
55178354Ssam
56178354Ssam#include <net/bpf.h>
57178354Ssam
58178354Ssam#include <net80211/ieee80211_var.h>
59178354Ssam#include <net80211/ieee80211_hostap.h>
60178354Ssam#include <net80211/ieee80211_input.h>
61190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
62190391Ssam#include <net80211/ieee80211_superg.h>
63190391Ssam#endif
64178354Ssam#include <net80211/ieee80211_wds.h>
65178354Ssam
66178354Ssam#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
67178354Ssam
68178354Ssamstatic	void hostap_vattach(struct ieee80211vap *);
69178354Ssamstatic	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70178354Ssamstatic	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
71283535Sadrian	    const struct ieee80211_rx_stats *,
72192468Ssam	    int rssi, int nf);
73178354Ssamstatic void hostap_deliver_data(struct ieee80211vap *,
74178354Ssam	    struct ieee80211_node *, struct mbuf *);
75178354Ssamstatic void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76283535Sadrian	    int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
77191546Ssamstatic void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
78178354Ssam
79178354Ssamvoid
80178354Ssamieee80211_hostap_attach(struct ieee80211com *ic)
81178354Ssam{
82178354Ssam	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
83178354Ssam}
84178354Ssam
85178354Ssamvoid
86178354Ssamieee80211_hostap_detach(struct ieee80211com *ic)
87178354Ssam{
88178354Ssam}
89178354Ssam
90178354Ssamstatic void
91178354Ssamhostap_vdetach(struct ieee80211vap *vap)
92178354Ssam{
93178354Ssam}
94178354Ssam
95178354Ssamstatic void
96178354Ssamhostap_vattach(struct ieee80211vap *vap)
97178354Ssam{
98178354Ssam	vap->iv_newstate = hostap_newstate;
99178354Ssam	vap->iv_input = hostap_input;
100178354Ssam	vap->iv_recv_mgmt = hostap_recv_mgmt;
101191546Ssam	vap->iv_recv_ctl = hostap_recv_ctl;
102178354Ssam	vap->iv_opdetach = hostap_vdetach;
103178354Ssam	vap->iv_deliver_data = hostap_deliver_data;
104241138Sadrian	vap->iv_recv_pspoll = ieee80211_recv_pspoll;
105178354Ssam}
106178354Ssam
107178354Ssamstatic void
108178354Ssamsta_disassoc(void *arg, struct ieee80211_node *ni)
109178354Ssam{
110178354Ssam	struct ieee80211vap *vap = arg;
111178354Ssam
112178354Ssam	if (ni->ni_vap == vap && ni->ni_associd != 0) {
113178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
114178354Ssam			IEEE80211_REASON_ASSOC_LEAVE);
115178354Ssam		ieee80211_node_leave(ni);
116178354Ssam	}
117178354Ssam}
118178354Ssam
119193413Ssamstatic void
120193413Ssamsta_csa(void *arg, struct ieee80211_node *ni)
121193413Ssam{
122193413Ssam	struct ieee80211vap *vap = arg;
123193413Ssam
124193413Ssam	if (ni->ni_vap == vap && ni->ni_associd != 0)
125193413Ssam		if (ni->ni_inact > vap->iv_inact_init) {
126193413Ssam			ni->ni_inact = vap->iv_inact_init;
127193413Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
128193413Ssam			    "%s: inact %u", __func__, ni->ni_inact);
129193413Ssam		}
130193413Ssam}
131193413Ssam
132193414Ssamstatic void
133193414Ssamsta_drop(void *arg, struct ieee80211_node *ni)
134193414Ssam{
135193414Ssam	struct ieee80211vap *vap = arg;
136193414Ssam
137193414Ssam	if (ni->ni_vap == vap && ni->ni_associd != 0)
138193414Ssam		ieee80211_node_leave(ni);
139193414Ssam}
140193414Ssam
141178354Ssam/*
142193414Ssam * Does a channel change require associated stations to re-associate
143193414Ssam * so protocol state is correct.  This is used when doing CSA across
144193414Ssam * bands or similar (e.g. HT -> legacy).
145193414Ssam */
146193414Ssamstatic int
147193414Ssamisbandchange(struct ieee80211com *ic)
148193414Ssam{
149193414Ssam	return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
150193414Ssam	    (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
151193414Ssam	     IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
152193414Ssam}
153193414Ssam
154193414Ssam/*
155178354Ssam * IEEE80211_M_HOSTAP vap state machine handler.
156178354Ssam */
157178354Ssamstatic int
158178354Ssamhostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
159178354Ssam{
160178354Ssam	struct ieee80211com *ic = vap->iv_ic;
161178354Ssam	enum ieee80211_state ostate;
162178354Ssam
163178354Ssam	IEEE80211_LOCK_ASSERT(ic);
164178354Ssam
165178354Ssam	ostate = vap->iv_state;
166178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
167178354Ssam	    __func__, ieee80211_state_name[ostate],
168178354Ssam	    ieee80211_state_name[nstate], arg);
169178354Ssam	vap->iv_state = nstate;			/* state transition */
170178354Ssam	if (ostate != IEEE80211_S_SCAN)
171178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
172178354Ssam	switch (nstate) {
173178354Ssam	case IEEE80211_S_INIT:
174178354Ssam		switch (ostate) {
175178354Ssam		case IEEE80211_S_SCAN:
176178354Ssam			ieee80211_cancel_scan(vap);
177178354Ssam			break;
178178354Ssam		case IEEE80211_S_CAC:
179178354Ssam			ieee80211_dfs_cac_stop(vap);
180178354Ssam			break;
181178354Ssam		case IEEE80211_S_RUN:
182178354Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
183178354Ssam			break;
184178354Ssam		default:
185178354Ssam			break;
186178354Ssam		}
187178354Ssam		if (ostate != IEEE80211_S_INIT) {
188178354Ssam			/* NB: optimize INIT -> INIT case */
189178354Ssam			ieee80211_reset_bss(vap);
190178354Ssam		}
191178354Ssam		if (vap->iv_auth->ia_detach != NULL)
192178354Ssam			vap->iv_auth->ia_detach(vap);
193178354Ssam		break;
194178354Ssam	case IEEE80211_S_SCAN:
195178354Ssam		switch (ostate) {
196178354Ssam		case IEEE80211_S_CSA:
197178354Ssam		case IEEE80211_S_RUN:
198178354Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
199178354Ssam			/*
200178354Ssam			 * Clear overlapping BSS state; the beacon frame
201178354Ssam			 * will be reconstructed on transition to the RUN
202178354Ssam			 * state and the timeout routines check if the flag
203178354Ssam			 * is set before doing anything so this is sufficient.
204178354Ssam			 */
205178354Ssam			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
206193655Ssam			ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
207178354Ssam			/* fall thru... */
208178354Ssam		case IEEE80211_S_CAC:
209178354Ssam			/*
210178354Ssam			 * NB: We may get here because of a manual channel
211178354Ssam			 *     change in which case we need to stop CAC
212178354Ssam			 * XXX no need to stop if ostate RUN but it's ok
213178354Ssam			 */
214178354Ssam			ieee80211_dfs_cac_stop(vap);
215178354Ssam			/* fall thru... */
216178354Ssam		case IEEE80211_S_INIT:
217178354Ssam			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
218178354Ssam			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
219178354Ssam				/*
220178354Ssam				 * Already have a channel; bypass the
221178354Ssam				 * scan and startup immediately.
222178354Ssam				 * ieee80211_create_ibss will call back to
223178354Ssam				 * move us to RUN state.
224178354Ssam				 */
225178354Ssam				ieee80211_create_ibss(vap, vap->iv_des_chan);
226178354Ssam				break;
227178354Ssam			}
228178354Ssam			/*
229178354Ssam			 * Initiate a scan.  We can come here as a result
230178354Ssam			 * of an IEEE80211_IOC_SCAN_REQ too in which case
231178354Ssam			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
232178354Ssam			 * and the scan request parameters will be present
233178354Ssam			 * in iv_scanreq.  Otherwise we do the default.
234178354Ssam			 */
235178354Ssam			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
236178354Ssam				ieee80211_check_scan(vap,
237178354Ssam				    vap->iv_scanreq_flags,
238178354Ssam				    vap->iv_scanreq_duration,
239178354Ssam				    vap->iv_scanreq_mindwell,
240178354Ssam				    vap->iv_scanreq_maxdwell,
241178354Ssam				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
242178354Ssam				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
243178354Ssam			} else
244178354Ssam				ieee80211_check_scan_current(vap);
245178354Ssam			break;
246178354Ssam		case IEEE80211_S_SCAN:
247178354Ssam			/*
248178354Ssam			 * A state change requires a reset; scan.
249178354Ssam			 */
250178354Ssam			ieee80211_check_scan_current(vap);
251178354Ssam			break;
252178354Ssam		default:
253178354Ssam			break;
254178354Ssam		}
255178354Ssam		break;
256178354Ssam	case IEEE80211_S_CAC:
257178354Ssam		/*
258178354Ssam		 * Start CAC on a DFS channel.  We come here when starting
259178354Ssam		 * a bss on a DFS channel (see ieee80211_create_ibss).
260178354Ssam		 */
261178354Ssam		ieee80211_dfs_cac_start(vap);
262178354Ssam		break;
263178354Ssam	case IEEE80211_S_RUN:
264178354Ssam		if (vap->iv_flags & IEEE80211_F_WPA) {
265178354Ssam			/* XXX validate prerequisites */
266178354Ssam		}
267178354Ssam		switch (ostate) {
268178354Ssam		case IEEE80211_S_INIT:
269178354Ssam			/*
270178354Ssam			 * Already have a channel; bypass the
271178354Ssam			 * scan and startup immediately.
272178354Ssam			 * Note that ieee80211_create_ibss will call
273178354Ssam			 * back to do a RUN->RUN state change.
274178354Ssam			 */
275178354Ssam			ieee80211_create_ibss(vap,
276178354Ssam			    ieee80211_ht_adjust_channel(ic,
277193655Ssam				ic->ic_curchan, vap->iv_flags_ht));
278178354Ssam			/* NB: iv_bss is changed on return */
279178354Ssam			break;
280178354Ssam		case IEEE80211_S_CAC:
281178354Ssam			/*
282178354Ssam			 * NB: This is the normal state change when CAC
283178354Ssam			 * expires and no radar was detected; no need to
284178354Ssam			 * clear the CAC timer as it's already expired.
285178354Ssam			 */
286178354Ssam			/* fall thru... */
287178354Ssam		case IEEE80211_S_CSA:
288178354Ssam			/*
289193413Ssam			 * Shorten inactivity timer of associated stations
290193413Ssam			 * to weed out sta's that don't follow a CSA.
291193413Ssam			 */
292193413Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
293193413Ssam			/*
294178354Ssam			 * Update bss node channel to reflect where
295178354Ssam			 * we landed after CSA.
296178354Ssam			 */
297178354Ssam			ieee80211_node_set_chan(vap->iv_bss,
298178354Ssam			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
299178354Ssam				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
300178354Ssam			/* XXX bypass debug msgs */
301178354Ssam			break;
302178354Ssam		case IEEE80211_S_SCAN:
303178354Ssam		case IEEE80211_S_RUN:
304178354Ssam#ifdef IEEE80211_DEBUG
305178354Ssam			if (ieee80211_msg_debug(vap)) {
306178354Ssam				struct ieee80211_node *ni = vap->iv_bss;
307178354Ssam				ieee80211_note(vap,
308178354Ssam				    "synchronized with %s ssid ",
309178354Ssam				    ether_sprintf(ni->ni_bssid));
310178354Ssam				ieee80211_print_essid(ni->ni_essid,
311178354Ssam				    ni->ni_esslen);
312178354Ssam				/* XXX MCS/HT */
313178354Ssam				printf(" channel %d start %uMb\n",
314178354Ssam				    ieee80211_chan2ieee(ic, ic->ic_curchan),
315178354Ssam				    IEEE80211_RATE2MBS(ni->ni_txrate));
316178354Ssam			}
317178354Ssam#endif
318178354Ssam			break;
319178354Ssam		default:
320178354Ssam			break;
321178354Ssam		}
322178354Ssam		/*
323178354Ssam		 * Start/stop the authenticator.  We delay until here
324178354Ssam		 * to allow configuration to happen out of order.
325178354Ssam		 */
326178354Ssam		if (vap->iv_auth->ia_attach != NULL) {
327178354Ssam			/* XXX check failure */
328178354Ssam			vap->iv_auth->ia_attach(vap);
329178354Ssam		} else if (vap->iv_auth->ia_detach != NULL) {
330178354Ssam			vap->iv_auth->ia_detach(vap);
331178354Ssam		}
332178354Ssam		ieee80211_node_authorize(vap->iv_bss);
333178354Ssam		break;
334193414Ssam	case IEEE80211_S_CSA:
335193414Ssam		if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
336193414Ssam			/*
337193414Ssam			 * On a ``band change'' silently drop associated
338193414Ssam			 * stations as they must re-associate before they
339193414Ssam			 * can pass traffic (as otherwise protocol state
340193414Ssam			 * such as capabilities and the negotiated rate
341193414Ssam			 * set may/will be wrong).
342193414Ssam			 */
343193414Ssam			ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
344193414Ssam		}
345193414Ssam		break;
346178354Ssam	default:
347178354Ssam		break;
348178354Ssam	}
349178354Ssam	return 0;
350178354Ssam}
351178354Ssam
352178354Ssamstatic void
353178354Ssamhostap_deliver_data(struct ieee80211vap *vap,
354178354Ssam	struct ieee80211_node *ni, struct mbuf *m)
355178354Ssam{
356178354Ssam	struct ether_header *eh = mtod(m, struct ether_header *);
357178354Ssam	struct ifnet *ifp = vap->iv_ifp;
358178354Ssam
359193292Ssam	/* clear driver/net80211 flags before passing up */
360254523Sandre	m->m_flags &= ~(M_MCAST | M_BCAST);
361254523Sandre	m_clrprotoflags(m);
362193292Ssam
363178354Ssam	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
364178354Ssam	    ("gack, opmode %d", vap->iv_opmode));
365178354Ssam	/*
366178354Ssam	 * Do accounting.
367178354Ssam	 */
368271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
369178354Ssam	IEEE80211_NODE_STAT(ni, rx_data);
370178354Ssam	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
371178354Ssam	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
372178354Ssam		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
373178354Ssam		IEEE80211_NODE_STAT(ni, rx_mcast);
374178354Ssam	} else
375178354Ssam		IEEE80211_NODE_STAT(ni, rx_ucast);
376178354Ssam
377178354Ssam	/* perform as a bridge within the AP */
378178354Ssam	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
379178354Ssam		struct mbuf *mcopy = NULL;
380178354Ssam
381178354Ssam		if (m->m_flags & M_MCAST) {
382243882Sglebius			mcopy = m_dup(m, M_NOWAIT);
383178354Ssam			if (mcopy == NULL)
384271861Sglebius				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
385178354Ssam			else
386178354Ssam				mcopy->m_flags |= M_MCAST;
387178354Ssam		} else {
388178354Ssam			/*
389178354Ssam			 * Check if the destination is associated with the
390178354Ssam			 * same vap and authorized to receive traffic.
391178354Ssam			 * Beware of traffic destined for the vap itself;
392178354Ssam			 * sending it will not work; just let it be delivered
393178354Ssam			 * normally.
394178354Ssam			 */
395178354Ssam			struct ieee80211_node *sta = ieee80211_find_vap_node(
396178354Ssam			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
397178354Ssam			if (sta != NULL) {
398178354Ssam				if (ieee80211_node_is_authorized(sta)) {
399178354Ssam					/*
400178354Ssam					 * Beware of sending to ourself; this
401178354Ssam					 * needs to happen via the normal
402178354Ssam					 * input path.
403178354Ssam					 */
404178354Ssam					if (sta != vap->iv_bss) {
405178354Ssam						mcopy = m;
406178354Ssam						m = NULL;
407178354Ssam					}
408178354Ssam				} else {
409178354Ssam					vap->iv_stats.is_rx_unauth++;
410178354Ssam					IEEE80211_NODE_STAT(sta, rx_unauth);
411178354Ssam				}
412178354Ssam				ieee80211_free_node(sta);
413178354Ssam			}
414178354Ssam		}
415178354Ssam		if (mcopy != NULL) {
416178354Ssam			int len, err;
417178354Ssam			len = mcopy->m_pkthdr.len;
418254082Sadrian			err = ieee80211_vap_xmitpkt(vap, mcopy);
419178354Ssam			if (err) {
420178354Ssam				/* NB: IFQ_HANDOFF reclaims mcopy */
421178354Ssam			} else {
422271861Sglebius				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
423178354Ssam			}
424178354Ssam		}
425178354Ssam	}
426178354Ssam	if (m != NULL) {
427178354Ssam		/*
428178354Ssam		 * Mark frame as coming from vap's interface.
429178354Ssam		 */
430178354Ssam		m->m_pkthdr.rcvif = ifp;
431178354Ssam		if (m->m_flags & M_MCAST) {
432178354Ssam			/*
433178354Ssam			 * Spam DWDS vap's w/ multicast traffic.
434178354Ssam			 */
435178354Ssam			/* XXX only if dwds in use? */
436178354Ssam			ieee80211_dwds_mcast(vap, m);
437178354Ssam		}
438178354Ssam		if (ni->ni_vlan != 0) {
439178354Ssam			/* attach vlan tag */
440178354Ssam			m->m_pkthdr.ether_vtag = ni->ni_vlan;
441178354Ssam			m->m_flags |= M_VLANTAG;
442178354Ssam		}
443178354Ssam		ifp->if_input(ifp, m);
444178354Ssam	}
445178354Ssam}
446178354Ssam
447178354Ssam/*
448178354Ssam * Decide if a received management frame should be
449178354Ssam * printed when debugging is enabled.  This filters some
450178354Ssam * of the less interesting frames that come frequently
451178354Ssam * (e.g. beacons).
452178354Ssam */
453178354Ssamstatic __inline int
454178354Ssamdoprint(struct ieee80211vap *vap, int subtype)
455178354Ssam{
456178354Ssam	switch (subtype) {
457178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
458178354Ssam		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
459178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
460178354Ssam		return 0;
461178354Ssam	}
462178354Ssam	return 1;
463178354Ssam}
464178354Ssam
465178354Ssam/*
466178354Ssam * Process a received frame.  The node associated with the sender
467178354Ssam * should be supplied.  If nothing was found in the node table then
468178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
469178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
470178354Ssam * during AP scanning to select a AP to associate with; it can have
471178354Ssam * any units so long as values have consistent units and higher values
472178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
473178354Ssam * by the 802.11 layer.
474178354Ssam */
475178354Ssamstatic int
476283535Sadrianhostap_input(struct ieee80211_node *ni, struct mbuf *m,
477283535Sadrian    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
478178354Ssam{
479178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
480178354Ssam	struct ieee80211com *ic = ni->ni_ic;
481178354Ssam	struct ifnet *ifp = vap->iv_ifp;
482178354Ssam	struct ieee80211_frame *wh;
483178354Ssam	struct ieee80211_key *key;
484178354Ssam	struct ether_header *eh;
485203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
486178354Ssam	uint8_t dir, type, subtype, qos;
487178354Ssam	uint8_t *bssid;
488178354Ssam
489183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
490178354Ssam		/*
491178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
492183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
493183247Ssam		 * here but were received out of order and been held on
494183247Ssam		 * the reorder queue.  When resubmitted they are marked
495183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
496183247Ssam		 * the normal processing.
497178354Ssam		 */
498178354Ssam		wh = mtod(m, struct ieee80211_frame *);
499178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
500178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
501178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
502178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
503178354Ssam		goto resubmit_ampdu;
504178354Ssam	}
505178354Ssam
506178354Ssam	KASSERT(ni != NULL, ("null node"));
507178354Ssam	ni->ni_inact = ni->ni_inact_reload;
508178354Ssam
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;
570282820Sadrian		if (IEEE80211_HAS_SEQ(type, subtype)) {
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++;
575296254Savos			if (! ieee80211_check_rxseq(ni, wh, bssid))
576178354Ssam				goto out;
577178354Ssam		}
578178354Ssam	}
579178354Ssam
580178354Ssam	switch (type) {
581178354Ssam	case IEEE80211_FC0_TYPE_DATA:
582178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
583178354Ssam		if (m->m_len < hdrspace &&
584178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
585178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
586178354Ssam			    ni->ni_macaddr, NULL,
587178354Ssam			    "data too short: expecting %u", hdrspace);
588178354Ssam			vap->iv_stats.is_rx_tooshort++;
589178354Ssam			goto out;		/* XXX */
590178354Ssam		}
591178354Ssam		if (!(dir == IEEE80211_FC1_DIR_TODS ||
592178354Ssam		     (dir == IEEE80211_FC1_DIR_DSTODS &&
593178354Ssam		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
594178354Ssam			if (dir != IEEE80211_FC1_DIR_DSTODS) {
595178354Ssam				IEEE80211_DISCARD(vap,
596178354Ssam				    IEEE80211_MSG_INPUT, wh, "data",
597178354Ssam				    "incorrect dir 0x%x", dir);
598178354Ssam			} else {
599178354Ssam				IEEE80211_DISCARD(vap,
600178354Ssam				    IEEE80211_MSG_INPUT |
601178354Ssam				    IEEE80211_MSG_WDS, wh,
602178354Ssam				    "4-address data",
603178354Ssam				    "%s", "DWDS not enabled");
604178354Ssam			}
605178354Ssam			vap->iv_stats.is_rx_wrongdir++;
606178354Ssam			goto out;
607178354Ssam		}
608178354Ssam		/* check if source STA is associated */
609178354Ssam		if (ni == vap->iv_bss) {
610178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
611178354Ssam			    wh, "data", "%s", "unknown src");
612178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
613178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
614178354Ssam			    IEEE80211_REASON_NOT_AUTHED);
615178354Ssam			vap->iv_stats.is_rx_notassoc++;
616178354Ssam			goto err;
617178354Ssam		}
618178354Ssam		if (ni->ni_associd == 0) {
619178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
620178354Ssam			    wh, "data", "%s", "unassoc src");
621178354Ssam			IEEE80211_SEND_MGMT(ni,
622178354Ssam			    IEEE80211_FC0_SUBTYPE_DISASSOC,
623178354Ssam			    IEEE80211_REASON_NOT_ASSOCED);
624178354Ssam			vap->iv_stats.is_rx_notassoc++;
625178354Ssam			goto err;
626178354Ssam		}
627178354Ssam
628178354Ssam		/*
629178354Ssam		 * Check for power save state change.
630178354Ssam		 * XXX out-of-order A-MPDU frames?
631178354Ssam		 */
632178354Ssam		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
633178354Ssam		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
634241138Sadrian			vap->iv_node_ps(ni,
635178354Ssam				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
636178354Ssam		/*
637178354Ssam		 * For 4-address packets handle WDS discovery
638178354Ssam		 * notifications.  Once a WDS link is setup frames
639178354Ssam		 * are just delivered to the WDS vap (see below).
640178354Ssam		 */
641178354Ssam		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
642178354Ssam			if (!ieee80211_node_is_authorized(ni)) {
643178354Ssam				IEEE80211_DISCARD(vap,
644178354Ssam				    IEEE80211_MSG_INPUT |
645178354Ssam				    IEEE80211_MSG_WDS, wh,
646178354Ssam				    "4-address data",
647178354Ssam				    "%s", "unauthorized port");
648178354Ssam				vap->iv_stats.is_rx_unauth++;
649178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
650178354Ssam				goto err;
651178354Ssam			}
652178354Ssam			ieee80211_dwds_discover(ni, m);
653178354Ssam			return type;
654178354Ssam		}
655178354Ssam
656178354Ssam		/*
657183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
658183247Ssam		 * processed directly then ieee80211_ampdu_reorder
659178354Ssam		 * will return 0; otherwise it has consumed the mbuf
660178354Ssam		 * and we should do nothing more with it.
661178354Ssam		 */
662183247Ssam		if ((m->m_flags & M_AMPDU) &&
663178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
664178354Ssam			m = NULL;
665178354Ssam			goto out;
666178354Ssam		}
667178354Ssam	resubmit_ampdu:
668178354Ssam
669178354Ssam		/*
670178354Ssam		 * Handle privacy requirements.  Note that we
671178354Ssam		 * must not be preempted from here until after
672178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
673178354Ssam		 * otherwise we may violate assumptions in the
674178354Ssam		 * crypto cipher modules used to do delayed update
675178354Ssam		 * of replay sequence numbers.
676178354Ssam		 */
677260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
678178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
679178354Ssam				/*
680178354Ssam				 * Discard encrypted frames when privacy is off.
681178354Ssam				 */
682178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
683178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
684178354Ssam				vap->iv_stats.is_rx_noprivacy++;
685178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
686178354Ssam				goto out;
687178354Ssam			}
688178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
689178354Ssam			if (key == NULL) {
690178354Ssam				/* NB: stats+msgs handled in crypto_decap */
691178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
692178354Ssam				goto out;
693178354Ssam			}
694178354Ssam			wh = mtod(m, struct ieee80211_frame *);
695260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
696178354Ssam		} else {
697178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
698178354Ssam			key = NULL;
699178354Ssam		}
700178354Ssam
701178354Ssam		/*
702178354Ssam		 * Save QoS bits for use below--before we strip the header.
703178354Ssam		 */
704178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
705178354Ssam			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
706178354Ssam			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
707178354Ssam			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
708178354Ssam		} else
709178354Ssam			qos = 0;
710178354Ssam
711178354Ssam		/*
712178354Ssam		 * Next up, any fragmentation.
713178354Ssam		 */
714178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
715178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
716178354Ssam			if (m == NULL) {
717178354Ssam				/* Fragment dropped or frame not complete yet */
718178354Ssam				goto out;
719178354Ssam			}
720178354Ssam		}
721178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
722178354Ssam
723178354Ssam		/*
724178354Ssam		 * Next strip any MSDU crypto bits.
725178354Ssam		 */
726178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
727178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
728178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
729178354Ssam			vap->iv_stats.is_rx_demicfail++;
730178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
731178354Ssam			goto out;
732178354Ssam		}
733178354Ssam		/* copy to listener after decrypt */
734192468Ssam		if (ieee80211_radiotap_active_vap(vap))
735192468Ssam			ieee80211_radiotap_rx(vap, m);
736178354Ssam		need_tap = 0;
737178354Ssam		/*
738178354Ssam		 * Finally, strip the 802.11 header.
739178354Ssam		 */
740178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
741178354Ssam		if (m == NULL) {
742178354Ssam			/* XXX mask bit to check for both */
743178354Ssam			/* don't count Null data frames as errors */
744178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
745178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
746178354Ssam				goto out;
747178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
748178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
749178354Ssam			vap->iv_stats.is_rx_decap++;
750178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
751178354Ssam			goto err;
752178354Ssam		}
753178354Ssam		eh = mtod(m, struct ether_header *);
754178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
755178354Ssam			/*
756178354Ssam			 * Deny any non-PAE frames received prior to
757178354Ssam			 * authorization.  For open/shared-key
758178354Ssam			 * authentication the port is mark authorized
759178354Ssam			 * after authentication completes.  For 802.1x
760178354Ssam			 * the port is not marked authorized by the
761178354Ssam			 * authenticator until the handshake has completed.
762178354Ssam			 */
763178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
764178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
765178354Ssam				    eh->ether_shost, "data",
766178354Ssam				    "unauthorized port: ether type 0x%x len %u",
767178354Ssam				    eh->ether_type, m->m_pkthdr.len);
768178354Ssam				vap->iv_stats.is_rx_unauth++;
769178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
770178354Ssam				goto err;
771178354Ssam			}
772178354Ssam		} else {
773178354Ssam			/*
774178354Ssam			 * When denying unencrypted frames, discard
775178354Ssam			 * any non-PAE frames received without encryption.
776178354Ssam			 */
777178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
778178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
779178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
780178354Ssam				/*
781178354Ssam				 * Drop unencrypted frames.
782178354Ssam				 */
783178354Ssam				vap->iv_stats.is_rx_unencrypted++;
784178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
785178354Ssam				goto out;
786178354Ssam			}
787178354Ssam		}
788178354Ssam		/* XXX require HT? */
789178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
790178354Ssam			m = ieee80211_decap_amsdu(ni, m);
791178354Ssam			if (m == NULL)
792178354Ssam				return IEEE80211_FC0_TYPE_DATA;
793190391Ssam		} else {
794190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
795190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
796190391Ssam			if (m == NULL)
797178354Ssam				return IEEE80211_FC0_TYPE_DATA;
798190391Ssam#endif
799178354Ssam		}
800178354Ssam		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
801178354Ssam			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
802178354Ssam		else
803178354Ssam			hostap_deliver_data(vap, ni, m);
804178354Ssam		return IEEE80211_FC0_TYPE_DATA;
805178354Ssam
806178354Ssam	case IEEE80211_FC0_TYPE_MGT:
807178354Ssam		vap->iv_stats.is_rx_mgmt++;
808178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
809178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
810178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
811178354Ssam			    wh, "mgt", "incorrect dir 0x%x", dir);
812178354Ssam			vap->iv_stats.is_rx_wrongdir++;
813178354Ssam			goto err;
814178354Ssam		}
815178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
816178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
817178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
818178354Ssam			    m->m_pkthdr.len);
819178354Ssam			vap->iv_stats.is_rx_tooshort++;
820178354Ssam			goto out;
821178354Ssam		}
822178354Ssam		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
823178354Ssam			/* ensure return frames are unicast */
824178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
825178354Ssam			    wh, NULL, "source is multicast: %s",
826178354Ssam			    ether_sprintf(wh->i_addr2));
827178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
828178354Ssam			goto out;
829178354Ssam		}
830178354Ssam#ifdef IEEE80211_DEBUG
831178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
832178354Ssam		    ieee80211_msg_dumppkts(vap)) {
833178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
834298376Savos			    ieee80211_mgt_subtype_name(subtype),
835178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
836178354Ssam		}
837178354Ssam#endif
838260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
839178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
840178354Ssam				/*
841178354Ssam				 * Only shared key auth frames with a challenge
842178354Ssam				 * should be encrypted, discard all others.
843178354Ssam				 */
844178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
845178354Ssam				    wh, NULL,
846178354Ssam				    "%s", "WEP set but not permitted");
847178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
848178354Ssam				goto out;
849178354Ssam			}
850178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
851178354Ssam				/*
852178354Ssam				 * Discard encrypted frames when privacy is off.
853178354Ssam				 */
854178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
855178354Ssam				    wh, NULL, "%s", "WEP set but PRIVACY off");
856178354Ssam				vap->iv_stats.is_rx_noprivacy++;
857178354Ssam				goto out;
858178354Ssam			}
859178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
860178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
861178354Ssam			if (key == NULL) {
862178354Ssam				/* NB: stats+msgs handled in crypto_decap */
863178354Ssam				goto out;
864178354Ssam			}
865178354Ssam			wh = mtod(m, struct ieee80211_frame *);
866260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
867178354Ssam		}
868205791Srpaulo		/*
869205791Srpaulo		 * Pass the packet to radiotap before calling iv_recv_mgmt().
870205791Srpaulo		 * Otherwise iv_recv_mgmt() might pass another packet to
871205791Srpaulo		 * radiotap, resulting in out of order packet captures.
872205791Srpaulo		 */
873205516Srpaulo		if (ieee80211_radiotap_active_vap(vap))
874205516Srpaulo			ieee80211_radiotap_rx(vap, m);
875205516Srpaulo		need_tap = 0;
876283535Sadrian		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
877191534Ssam		goto out;
878178354Ssam
879178354Ssam	case IEEE80211_FC0_TYPE_CTL:
880178354Ssam		vap->iv_stats.is_rx_ctl++;
881178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
882191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
883178354Ssam		goto out;
884178354Ssam	default:
885178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
886178354Ssam		    wh, "bad", "frame type 0x%x", type);
887178354Ssam		/* should not come here */
888178354Ssam		break;
889178354Ssam	}
890178354Ssamerr:
891271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
892178354Ssamout:
893178354Ssam	if (m != NULL) {
894192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
895192468Ssam			ieee80211_radiotap_rx(vap, m);
896178354Ssam		m_freem(m);
897178354Ssam	}
898178354Ssam	return type;
899178354Ssam}
900178354Ssam
901178354Ssamstatic void
902178354Ssamhostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
903192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
904178354Ssam{
905178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
906178354Ssam
907178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
908178354Ssam
909178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
910178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
911178354Ssam		    ni->ni_macaddr, "open auth",
912178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
913178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
914178354Ssam		/*
915178354Ssam		 * Clear any challenge text that may be there if
916178354Ssam		 * a previous shared key auth failed and then an
917178354Ssam		 * open auth is attempted.
918178354Ssam		 */
919178354Ssam		if (ni->ni_challenge != NULL) {
920283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
921178354Ssam			ni->ni_challenge = NULL;
922178354Ssam		}
923178354Ssam		/* XXX hack to workaround calling convention */
924178354Ssam		ieee80211_send_error(ni, wh->i_addr2,
925178354Ssam		    IEEE80211_FC0_SUBTYPE_AUTH,
926178354Ssam		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
927178354Ssam		return;
928178354Ssam	}
929178354Ssam	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
930178354Ssam		vap->iv_stats.is_rx_bad_auth++;
931178354Ssam		return;
932178354Ssam	}
933178354Ssam	/* always accept open authentication requests */
934178354Ssam	if (ni == vap->iv_bss) {
935178354Ssam		ni = ieee80211_dup_bss(vap, wh->i_addr2);
936178354Ssam		if (ni == NULL)
937178354Ssam			return;
938178354Ssam	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
939178354Ssam		(void) ieee80211_ref_node(ni);
940178354Ssam	/*
941178354Ssam	 * Mark the node as referenced to reflect that it's
942178354Ssam	 * reference count has been bumped to insure it remains
943178354Ssam	 * after the transaction completes.
944178354Ssam	 */
945178354Ssam	ni->ni_flags |= IEEE80211_NODE_AREF;
946186099Ssam	/*
947186151Ssam	 * Mark the node as requiring a valid association id
948186099Ssam	 * before outbound traffic is permitted.
949186099Ssam	 */
950186099Ssam	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
951178354Ssam
952178354Ssam	if (vap->iv_acl != NULL &&
953178354Ssam	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
954178354Ssam		/*
955178354Ssam		 * When the ACL policy is set to RADIUS we defer the
956178354Ssam		 * authorization to a user agent.  Dispatch an event,
957178354Ssam		 * a subsequent MLME call will decide the fate of the
958178354Ssam		 * station.  If the user agent is not present then the
959178354Ssam		 * node will be reclaimed due to inactivity.
960178354Ssam		 */
961178354Ssam		IEEE80211_NOTE_MAC(vap,
962178354Ssam		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
963178354Ssam		    "%s", "station authentication defered (radius acl)");
964178354Ssam		ieee80211_notify_node_auth(ni);
965178354Ssam	} else {
966178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
967178354Ssam		IEEE80211_NOTE_MAC(vap,
968178354Ssam		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
969178354Ssam		    "%s", "station authenticated (open)");
970178354Ssam		/*
971178354Ssam		 * When 802.1x is not in use mark the port
972178354Ssam		 * authorized at this point so traffic can flow.
973178354Ssam		 */
974178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
975178354Ssam			ieee80211_node_authorize(ni);
976178354Ssam	}
977178354Ssam}
978178354Ssam
979178354Ssamstatic void
980178354Ssamhostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
981192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
982178354Ssam    uint16_t seq, uint16_t status)
983178354Ssam{
984178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
985178354Ssam	uint8_t *challenge;
986178354Ssam	int allocbs, estatus;
987178354Ssam
988178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
989178354Ssam
990178354Ssam	/*
991178354Ssam	 * NB: this can happen as we allow pre-shared key
992178354Ssam	 * authentication to be enabled w/o wep being turned
993178354Ssam	 * on so that configuration of these can be done
994178354Ssam	 * in any order.  It may be better to enforce the
995178354Ssam	 * ordering in which case this check would just be
996178354Ssam	 * for sanity/consistency.
997178354Ssam	 */
998178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
999178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1000178354Ssam		    ni->ni_macaddr, "shared key auth",
1001178354Ssam		    "%s", " PRIVACY is disabled");
1002178354Ssam		estatus = IEEE80211_STATUS_ALG;
1003178354Ssam		goto bad;
1004178354Ssam	}
1005178354Ssam	/*
1006178354Ssam	 * Pre-shared key authentication is evil; accept
1007178354Ssam	 * it only if explicitly configured (it is supported
1008178354Ssam	 * mainly for compatibility with clients like Mac OS X).
1009178354Ssam	 */
1010178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1011178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1012178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1013178354Ssam		    ni->ni_macaddr, "shared key auth",
1014178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1015178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1016178354Ssam		estatus = IEEE80211_STATUS_ALG;
1017178354Ssam		goto bad;
1018178354Ssam	}
1019178354Ssam
1020178354Ssam	challenge = NULL;
1021178354Ssam	if (frm + 1 < efrm) {
1022178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1023178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1024178354Ssam			    ni->ni_macaddr, "shared key auth",
1025178354Ssam			    "ie %d/%d too long",
1026178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1027178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1028178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1029178354Ssam			goto bad;
1030178354Ssam		}
1031178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1032178354Ssam			challenge = frm;
1033178354Ssam		frm += frm[1] + 2;
1034178354Ssam	}
1035178354Ssam	switch (seq) {
1036178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1037178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1038178354Ssam		if (challenge == NULL) {
1039178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1040178354Ssam			    ni->ni_macaddr, "shared key auth",
1041178354Ssam			    "%s", "no challenge");
1042178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1043178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1044178354Ssam			goto bad;
1045178354Ssam		}
1046178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1047178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1048178354Ssam			    ni->ni_macaddr, "shared key auth",
1049178354Ssam			    "bad challenge len %d", challenge[1]);
1050178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1051178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1052178354Ssam			goto bad;
1053178354Ssam		}
1054178354Ssam	default:
1055178354Ssam		break;
1056178354Ssam	}
1057178354Ssam	switch (seq) {
1058178354Ssam	case IEEE80211_AUTH_SHARED_REQUEST:
1059178354Ssam		if (ni == vap->iv_bss) {
1060178354Ssam			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1061178354Ssam			if (ni == NULL) {
1062178354Ssam				/* NB: no way to return an error */
1063178354Ssam				return;
1064178354Ssam			}
1065178354Ssam			allocbs = 1;
1066178354Ssam		} else {
1067178354Ssam			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1068178354Ssam				(void) ieee80211_ref_node(ni);
1069178354Ssam			allocbs = 0;
1070178354Ssam		}
1071178354Ssam		/*
1072178354Ssam		 * Mark the node as referenced to reflect that it's
1073178354Ssam		 * reference count has been bumped to insure it remains
1074178354Ssam		 * after the transaction completes.
1075178354Ssam		 */
1076178354Ssam		ni->ni_flags |= IEEE80211_NODE_AREF;
1077186099Ssam		/*
1078186099Ssam		 * Mark the node as requiring a valid associatio id
1079186099Ssam		 * before outbound traffic is permitted.
1080186099Ssam		 */
1081186099Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1082178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1083192468Ssam		ni->ni_noise = nf;
1084178354Ssam		if (!ieee80211_alloc_challenge(ni)) {
1085178354Ssam			/* NB: don't return error so they rexmit */
1086178354Ssam			return;
1087178354Ssam		}
1088178354Ssam		get_random_bytes(ni->ni_challenge,
1089178354Ssam			IEEE80211_CHALLENGE_LEN);
1090178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1091178354Ssam		    ni, "shared key %sauth request", allocbs ? "" : "re");
1092178354Ssam		/*
1093178354Ssam		 * When the ACL policy is set to RADIUS we defer the
1094178354Ssam		 * authorization to a user agent.  Dispatch an event,
1095178354Ssam		 * a subsequent MLME call will decide the fate of the
1096178354Ssam		 * station.  If the user agent is not present then the
1097178354Ssam		 * node will be reclaimed due to inactivity.
1098178354Ssam		 */
1099178354Ssam		if (vap->iv_acl != NULL &&
1100178354Ssam		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1101178354Ssam			IEEE80211_NOTE_MAC(vap,
1102178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1103178354Ssam			    ni->ni_macaddr,
1104178354Ssam			    "%s", "station authentication defered (radius acl)");
1105178354Ssam			ieee80211_notify_node_auth(ni);
1106178354Ssam			return;
1107178354Ssam		}
1108178354Ssam		break;
1109178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1110178354Ssam		if (ni == vap->iv_bss) {
1111178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1112178354Ssam			    ni->ni_macaddr, "shared key response",
1113178354Ssam			    "%s", "unknown station");
1114178354Ssam			/* NB: don't send a response */
1115178354Ssam			return;
1116178354Ssam		}
1117178354Ssam		if (ni->ni_challenge == NULL) {
1118178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1119178354Ssam			    ni->ni_macaddr, "shared key response",
1120178354Ssam			    "%s", "no challenge recorded");
1121178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1122178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1123178354Ssam			goto bad;
1124178354Ssam		}
1125178354Ssam		if (memcmp(ni->ni_challenge, &challenge[2],
1126178354Ssam			   challenge[1]) != 0) {
1127178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1128178354Ssam			    ni->ni_macaddr, "shared key response",
1129178354Ssam			    "%s", "challenge mismatch");
1130178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1131178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1132178354Ssam			goto bad;
1133178354Ssam		}
1134178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1135178354Ssam		    ni, "%s", "station authenticated (shared key)");
1136178354Ssam		ieee80211_node_authorize(ni);
1137178354Ssam		break;
1138178354Ssam	default:
1139178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1140178354Ssam		    ni->ni_macaddr, "shared key auth",
1141178354Ssam		    "bad seq %d", seq);
1142178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1143178354Ssam		estatus = IEEE80211_STATUS_SEQUENCE;
1144178354Ssam		goto bad;
1145178354Ssam	}
1146178354Ssam	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1147178354Ssam	return;
1148178354Ssambad:
1149178354Ssam	/*
1150178354Ssam	 * Send an error response; but only when operating as an AP.
1151178354Ssam	 */
1152178354Ssam	/* XXX hack to workaround calling convention */
1153178354Ssam	ieee80211_send_error(ni, wh->i_addr2,
1154178354Ssam	    IEEE80211_FC0_SUBTYPE_AUTH,
1155178354Ssam	    (seq + 1) | (estatus<<16));
1156178354Ssam}
1157178354Ssam
1158178354Ssam/*
1159178354Ssam * Convert a WPA cipher selector OUI to an internal
1160178354Ssam * cipher algorithm.  Where appropriate we also
1161178354Ssam * record any key length.
1162178354Ssam */
1163178354Ssamstatic int
1164178354Ssamwpa_cipher(const uint8_t *sel, uint8_t *keylen)
1165178354Ssam{
1166178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1167298359Savos	uint32_t w = le32dec(sel);
1168178354Ssam
1169178354Ssam	switch (w) {
1170178354Ssam	case WPA_SEL(WPA_CSE_NULL):
1171178354Ssam		return IEEE80211_CIPHER_NONE;
1172178354Ssam	case WPA_SEL(WPA_CSE_WEP40):
1173178354Ssam		if (keylen)
1174178354Ssam			*keylen = 40 / NBBY;
1175178354Ssam		return IEEE80211_CIPHER_WEP;
1176178354Ssam	case WPA_SEL(WPA_CSE_WEP104):
1177178354Ssam		if (keylen)
1178178354Ssam			*keylen = 104 / NBBY;
1179178354Ssam		return IEEE80211_CIPHER_WEP;
1180178354Ssam	case WPA_SEL(WPA_CSE_TKIP):
1181178354Ssam		return IEEE80211_CIPHER_TKIP;
1182178354Ssam	case WPA_SEL(WPA_CSE_CCMP):
1183178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1184178354Ssam	}
1185178354Ssam	return 32;		/* NB: so 1<< is discarded */
1186178354Ssam#undef WPA_SEL
1187178354Ssam}
1188178354Ssam
1189178354Ssam/*
1190178354Ssam * Convert a WPA key management/authentication algorithm
1191178354Ssam * to an internal code.
1192178354Ssam */
1193178354Ssamstatic int
1194178354Ssamwpa_keymgmt(const uint8_t *sel)
1195178354Ssam{
1196178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1197298359Savos	uint32_t w = le32dec(sel);
1198178354Ssam
1199178354Ssam	switch (w) {
1200178354Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1201178354Ssam		return WPA_ASE_8021X_UNSPEC;
1202178354Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
1203178354Ssam		return WPA_ASE_8021X_PSK;
1204178354Ssam	case WPA_SEL(WPA_ASE_NONE):
1205178354Ssam		return WPA_ASE_NONE;
1206178354Ssam	}
1207178354Ssam	return 0;		/* NB: so is discarded */
1208178354Ssam#undef WPA_SEL
1209178354Ssam}
1210178354Ssam
1211178354Ssam/*
1212178354Ssam * Parse a WPA information element to collect parameters.
1213178354Ssam * Note that we do not validate security parameters; that
1214178354Ssam * is handled by the authenticator; the parsing done here
1215178354Ssam * is just for internal use in making operational decisions.
1216178354Ssam */
1217178354Ssamstatic int
1218178354Ssamieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1219178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1220178354Ssam{
1221178354Ssam	uint8_t len = frm[1];
1222178354Ssam	uint32_t w;
1223178354Ssam	int n;
1224178354Ssam
1225178354Ssam	/*
1226178354Ssam	 * Check the length once for fixed parts: OUI, type,
1227178354Ssam	 * version, mcast cipher, and 2 selector counts.
1228178354Ssam	 * Other, variable-length data, must be checked separately.
1229178354Ssam	 */
1230178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1231178354Ssam		IEEE80211_DISCARD_IE(vap,
1232178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1233178354Ssam		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1234178354Ssam		return IEEE80211_REASON_IE_INVALID;
1235178354Ssam	}
1236178354Ssam	if (len < 14) {
1237178354Ssam		IEEE80211_DISCARD_IE(vap,
1238178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1239178354Ssam		    wh, "WPA", "too short, len %u", len);
1240178354Ssam		return IEEE80211_REASON_IE_INVALID;
1241178354Ssam	}
1242178354Ssam	frm += 6, len -= 4;		/* NB: len is payload only */
1243200242Srpaulo	/* NB: iswpaoui already validated the OUI and type */
1244298359Savos	w = le16dec(frm);
1245178354Ssam	if (w != WPA_VERSION) {
1246178354Ssam		IEEE80211_DISCARD_IE(vap,
1247178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1248178354Ssam		    wh, "WPA", "bad version %u", w);
1249178354Ssam		return IEEE80211_REASON_IE_INVALID;
1250178354Ssam	}
1251178354Ssam	frm += 2, len -= 2;
1252178354Ssam
1253178354Ssam	memset(rsn, 0, sizeof(*rsn));
1254178354Ssam
1255178354Ssam	/* multicast/group cipher */
1256178354Ssam	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1257178354Ssam	frm += 4, len -= 4;
1258178354Ssam
1259178354Ssam	/* unicast ciphers */
1260298359Savos	n = le16dec(frm);
1261178354Ssam	frm += 2, len -= 2;
1262178354Ssam	if (len < n*4+2) {
1263178354Ssam		IEEE80211_DISCARD_IE(vap,
1264178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1265178354Ssam		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1266178354Ssam		    len, n);
1267178354Ssam		return IEEE80211_REASON_IE_INVALID;
1268178354Ssam	}
1269178354Ssam	w = 0;
1270178354Ssam	for (; n > 0; n--) {
1271178354Ssam		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1272178354Ssam		frm += 4, len -= 4;
1273178354Ssam	}
1274178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1275178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1276178354Ssam	else
1277178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1278178354Ssam
1279178354Ssam	/* key management algorithms */
1280298359Savos	n = le16dec(frm);
1281178354Ssam	frm += 2, len -= 2;
1282178354Ssam	if (len < n*4) {
1283178354Ssam		IEEE80211_DISCARD_IE(vap,
1284178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1285178354Ssam		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1286178354Ssam		    len, n);
1287178354Ssam		return IEEE80211_REASON_IE_INVALID;
1288178354Ssam	}
1289178354Ssam	w = 0;
1290178354Ssam	for (; n > 0; n--) {
1291178354Ssam		w |= wpa_keymgmt(frm);
1292178354Ssam		frm += 4, len -= 4;
1293178354Ssam	}
1294178354Ssam	if (w & WPA_ASE_8021X_UNSPEC)
1295178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1296178354Ssam	else
1297178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1298178354Ssam
1299178354Ssam	if (len > 2)		/* optional capabilities */
1300298359Savos		rsn->rsn_caps = le16dec(frm);
1301178354Ssam
1302178354Ssam	return 0;
1303178354Ssam}
1304178354Ssam
1305178354Ssam/*
1306178354Ssam * Convert an RSN cipher selector OUI to an internal
1307178354Ssam * cipher algorithm.  Where appropriate we also
1308178354Ssam * record any key length.
1309178354Ssam */
1310178354Ssamstatic int
1311178354Ssamrsn_cipher(const uint8_t *sel, uint8_t *keylen)
1312178354Ssam{
1313178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1314298359Savos	uint32_t w = le32dec(sel);
1315178354Ssam
1316178354Ssam	switch (w) {
1317178354Ssam	case RSN_SEL(RSN_CSE_NULL):
1318178354Ssam		return IEEE80211_CIPHER_NONE;
1319178354Ssam	case RSN_SEL(RSN_CSE_WEP40):
1320178354Ssam		if (keylen)
1321178354Ssam			*keylen = 40 / NBBY;
1322178354Ssam		return IEEE80211_CIPHER_WEP;
1323178354Ssam	case RSN_SEL(RSN_CSE_WEP104):
1324178354Ssam		if (keylen)
1325178354Ssam			*keylen = 104 / NBBY;
1326178354Ssam		return IEEE80211_CIPHER_WEP;
1327178354Ssam	case RSN_SEL(RSN_CSE_TKIP):
1328178354Ssam		return IEEE80211_CIPHER_TKIP;
1329178354Ssam	case RSN_SEL(RSN_CSE_CCMP):
1330178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1331178354Ssam	case RSN_SEL(RSN_CSE_WRAP):
1332178354Ssam		return IEEE80211_CIPHER_AES_OCB;
1333178354Ssam	}
1334178354Ssam	return 32;		/* NB: so 1<< is discarded */
1335178354Ssam#undef WPA_SEL
1336178354Ssam}
1337178354Ssam
1338178354Ssam/*
1339178354Ssam * Convert an RSN key management/authentication algorithm
1340178354Ssam * to an internal code.
1341178354Ssam */
1342178354Ssamstatic int
1343178354Ssamrsn_keymgmt(const uint8_t *sel)
1344178354Ssam{
1345178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1346298359Savos	uint32_t w = le32dec(sel);
1347178354Ssam
1348178354Ssam	switch (w) {
1349178354Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1350178354Ssam		return RSN_ASE_8021X_UNSPEC;
1351178354Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
1352178354Ssam		return RSN_ASE_8021X_PSK;
1353178354Ssam	case RSN_SEL(RSN_ASE_NONE):
1354178354Ssam		return RSN_ASE_NONE;
1355178354Ssam	}
1356178354Ssam	return 0;		/* NB: so is discarded */
1357178354Ssam#undef RSN_SEL
1358178354Ssam}
1359178354Ssam
1360178354Ssam/*
1361178354Ssam * Parse a WPA/RSN information element to collect parameters
1362178354Ssam * and validate the parameters against what has been
1363178354Ssam * configured for the system.
1364178354Ssam */
1365178354Ssamstatic int
1366178354Ssamieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1367178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1368178354Ssam{
1369178354Ssam	uint8_t len = frm[1];
1370178354Ssam	uint32_t w;
1371178354Ssam	int n;
1372178354Ssam
1373178354Ssam	/*
1374178354Ssam	 * Check the length once for fixed parts:
1375178354Ssam	 * version, mcast cipher, and 2 selector counts.
1376178354Ssam	 * Other, variable-length data, must be checked separately.
1377178354Ssam	 */
1378178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1379178354Ssam		IEEE80211_DISCARD_IE(vap,
1380178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1381178354Ssam		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1382178354Ssam		return IEEE80211_REASON_IE_INVALID;
1383178354Ssam	}
1384178354Ssam	if (len < 10) {
1385178354Ssam		IEEE80211_DISCARD_IE(vap,
1386178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1387178354Ssam		    wh, "RSN", "too short, len %u", len);
1388178354Ssam		return IEEE80211_REASON_IE_INVALID;
1389178354Ssam	}
1390178354Ssam	frm += 2;
1391298359Savos	w = le16dec(frm);
1392178354Ssam	if (w != RSN_VERSION) {
1393178354Ssam		IEEE80211_DISCARD_IE(vap,
1394178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1395178354Ssam		    wh, "RSN", "bad version %u", w);
1396178354Ssam		return IEEE80211_REASON_IE_INVALID;
1397178354Ssam	}
1398178354Ssam	frm += 2, len -= 2;
1399178354Ssam
1400178354Ssam	memset(rsn, 0, sizeof(*rsn));
1401178354Ssam
1402178354Ssam	/* multicast/group cipher */
1403178354Ssam	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1404178354Ssam	frm += 4, len -= 4;
1405178354Ssam
1406178354Ssam	/* unicast ciphers */
1407298359Savos	n = le16dec(frm);
1408178354Ssam	frm += 2, len -= 2;
1409178354Ssam	if (len < n*4+2) {
1410178354Ssam		IEEE80211_DISCARD_IE(vap,
1411178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1412178354Ssam		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1413178354Ssam		    len, n);
1414178354Ssam		return IEEE80211_REASON_IE_INVALID;
1415178354Ssam	}
1416178354Ssam	w = 0;
1417178354Ssam	for (; n > 0; n--) {
1418178354Ssam		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1419178354Ssam		frm += 4, len -= 4;
1420178354Ssam	}
1421178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1422178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1423178354Ssam	else
1424178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1425178354Ssam
1426178354Ssam	/* key management algorithms */
1427298359Savos	n = le16dec(frm);
1428178354Ssam	frm += 2, len -= 2;
1429178354Ssam	if (len < n*4) {
1430178354Ssam		IEEE80211_DISCARD_IE(vap,
1431178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1432178354Ssam		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1433178354Ssam		    len, n);
1434178354Ssam		return IEEE80211_REASON_IE_INVALID;
1435178354Ssam	}
1436178354Ssam	w = 0;
1437178354Ssam	for (; n > 0; n--) {
1438178354Ssam		w |= rsn_keymgmt(frm);
1439178354Ssam		frm += 4, len -= 4;
1440178354Ssam	}
1441178354Ssam	if (w & RSN_ASE_8021X_UNSPEC)
1442178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1443178354Ssam	else
1444178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1445178354Ssam
1446178354Ssam	/* optional RSN capabilities */
1447178354Ssam	if (len > 2)
1448298359Savos		rsn->rsn_caps = le16dec(frm);
1449178354Ssam	/* XXXPMKID */
1450178354Ssam
1451178354Ssam	return 0;
1452178354Ssam}
1453178354Ssam
1454178354Ssam/*
1455178354Ssam * WPA/802.11i assocation request processing.
1456178354Ssam */
1457178354Ssamstatic int
1458178354Ssamwpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1459178354Ssam	const struct ieee80211_frame *wh, const uint8_t *wpa,
1460178354Ssam	const uint8_t *rsn, uint16_t capinfo)
1461178354Ssam{
1462178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1463178354Ssam	uint8_t reason;
1464178354Ssam	int badwparsn;
1465178354Ssam
1466178354Ssam	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1467178354Ssam	if (wpa == NULL && rsn == NULL) {
1468178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1469178354Ssam			/*
1470178354Ssam			 * W-Fi Protected Setup (WPS) permits
1471178354Ssam			 * clients to associate and pass EAPOL frames
1472178354Ssam			 * to establish initial credentials.
1473178354Ssam			 */
1474178354Ssam			ni->ni_flags |= IEEE80211_NODE_WPS;
1475178354Ssam			return 1;
1476178354Ssam		}
1477178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1478178354Ssam		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1479178354Ssam			/*
1480178354Ssam			 * Transitional Security Network.  Permits clients
1481178354Ssam			 * to associate and use WEP while WPA is configured.
1482178354Ssam			 */
1483178354Ssam			ni->ni_flags |= IEEE80211_NODE_TSN;
1484178354Ssam			return 1;
1485178354Ssam		}
1486178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1487178354Ssam		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1488178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1489178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1490178354Ssam		goto bad;
1491178354Ssam	}
1492178354Ssam	/* assert right association security credentials */
1493178354Ssam	badwparsn = 0;			/* NB: to silence compiler */
1494178354Ssam	switch (vap->iv_flags & IEEE80211_F_WPA) {
1495178354Ssam	case IEEE80211_F_WPA1:
1496178354Ssam		badwparsn = (wpa == NULL);
1497178354Ssam		break;
1498178354Ssam	case IEEE80211_F_WPA2:
1499178354Ssam		badwparsn = (rsn == NULL);
1500178354Ssam		break;
1501178354Ssam	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1502178354Ssam		badwparsn = (wpa == NULL && rsn == NULL);
1503178354Ssam		break;
1504178354Ssam	}
1505178354Ssam	if (badwparsn) {
1506178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1507178354Ssam		    wh, NULL,
1508178354Ssam		    "%s", "missing WPA/RSN IE in association request");
1509178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1510178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1511178354Ssam		goto bad;
1512178354Ssam	}
1513178354Ssam	/*
1514178354Ssam	 * Parse WPA/RSN information element.
1515178354Ssam	 */
1516178354Ssam	if (wpa != NULL)
1517178354Ssam		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1518178354Ssam	else
1519178354Ssam		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1520178354Ssam	if (reason != 0) {
1521178354Ssam		/* XXX distinguish WPA/RSN? */
1522178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1523178354Ssam		goto bad;
1524178354Ssam	}
1525178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1526178354Ssam	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1527178354Ssam	    wpa != NULL ? "WPA" : "RSN",
1528178354Ssam	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1529178354Ssam	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1530178354Ssam	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1531178354Ssam
1532178354Ssam	return 1;
1533178354Ssambad:
1534178354Ssam	ieee80211_node_deauth(ni, reason);
1535178354Ssam	return 0;
1536178354Ssam}
1537178354Ssam
1538178354Ssam/* XXX find a better place for definition */
1539178354Ssamstruct l2_update_frame {
1540178354Ssam	struct ether_header eh;
1541178354Ssam	uint8_t dsap;
1542178354Ssam	uint8_t ssap;
1543178354Ssam	uint8_t control;
1544178354Ssam	uint8_t xid[3];
1545178354Ssam}  __packed;
1546178354Ssam
1547178354Ssam/*
1548178354Ssam * Deliver a TGf L2UF frame on behalf of a station.
1549178354Ssam * This primes any bridge when the station is roaming
1550178354Ssam * between ap's on the same wired network.
1551178354Ssam */
1552178354Ssamstatic void
1553178354Ssamieee80211_deliver_l2uf(struct ieee80211_node *ni)
1554178354Ssam{
1555178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1556178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1557178354Ssam	struct mbuf *m;
1558178354Ssam	struct l2_update_frame *l2uf;
1559178354Ssam	struct ether_header *eh;
1560178354Ssam
1561178354Ssam	m = m_gethdr(M_NOWAIT, MT_DATA);
1562178354Ssam	if (m == NULL) {
1563178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1564178354Ssam		    "%s", "no mbuf for l2uf frame");
1565178354Ssam		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1566178354Ssam		return;
1567178354Ssam	}
1568178354Ssam	l2uf = mtod(m, struct l2_update_frame *);
1569178354Ssam	eh = &l2uf->eh;
1570178354Ssam	/* dst: Broadcast address */
1571178354Ssam	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1572178354Ssam	/* src: associated STA */
1573178354Ssam	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1574178354Ssam	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1575178354Ssam
1576178354Ssam	l2uf->dsap = 0;
1577178354Ssam	l2uf->ssap = 0;
1578178354Ssam	l2uf->control = 0xf5;
1579178354Ssam	l2uf->xid[0] = 0x81;
1580178354Ssam	l2uf->xid[1] = 0x80;
1581178354Ssam	l2uf->xid[2] = 0x00;
1582178354Ssam
1583178354Ssam	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1584178354Ssam	hostap_deliver_data(vap, ni, m);
1585178354Ssam}
1586178354Ssam
1587178354Ssamstatic void
1588178354Ssamratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1589178354Ssam	int reassoc, int resp, const char *tag, int rate)
1590178354Ssam{
1591178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1592178354Ssam	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1593178354Ssam	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1594178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1595178354Ssam	ieee80211_node_leave(ni);
1596178354Ssam}
1597178354Ssam
1598178354Ssamstatic void
1599178354Ssamcapinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1600178354Ssam	int reassoc, int resp, const char *tag, int capinfo)
1601178354Ssam{
1602178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1603178354Ssam
1604178354Ssam	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1605178354Ssam	    "deny %s request, %s mismatch 0x%x",
1606178354Ssam	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1607178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1608178354Ssam	ieee80211_node_leave(ni);
1609178354Ssam	vap->iv_stats.is_rx_assoc_capmismatch++;
1610178354Ssam}
1611178354Ssam
1612178354Ssamstatic void
1613178354Ssamhtcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1614178354Ssam	int reassoc, int resp)
1615178354Ssam{
1616178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1617178354Ssam	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1618178354Ssam	/* XXX no better code */
1619193543Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1620178354Ssam	ieee80211_node_leave(ni);
1621178354Ssam}
1622178354Ssam
1623178354Ssamstatic void
1624178354Ssamauthalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1625178354Ssam	int algo, int seq, int status)
1626178354Ssam{
1627178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1628178354Ssam
1629178354Ssam	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1630178354Ssam	    wh, NULL, "unsupported alg %d", algo);
1631178354Ssam	vap->iv_stats.is_rx_auth_unsupported++;
1632178354Ssam	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1633178354Ssam	    seq | (status << 16));
1634178354Ssam}
1635178354Ssam
1636178354Ssamstatic __inline int
1637178354Ssamishtmixed(const uint8_t *ie)
1638178354Ssam{
1639178354Ssam	const struct ieee80211_ie_htinfo *ht =
1640178354Ssam	    (const struct ieee80211_ie_htinfo *) ie;
1641178354Ssam	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1642178354Ssam	    IEEE80211_HTINFO_OPMODE_MIXED;
1643178354Ssam}
1644178354Ssam
1645178354Ssamstatic int
1646178354Ssamis11bclient(const uint8_t *rates, const uint8_t *xrates)
1647178354Ssam{
1648178354Ssam	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1649178354Ssam	int i;
1650178354Ssam
1651178354Ssam	/* NB: the 11b clients we care about will not have xrates */
1652178354Ssam	if (xrates != NULL || rates == NULL)
1653178354Ssam		return 0;
1654178354Ssam	for (i = 0; i < rates[1]; i++) {
1655178354Ssam		int r = rates[2+i] & IEEE80211_RATE_VAL;
1656178354Ssam		if (r > 2*11 || ((1<<r) & brates) == 0)
1657178354Ssam			return 0;
1658178354Ssam	}
1659178354Ssam	return 1;
1660178354Ssam}
1661178354Ssam
1662178354Ssamstatic void
1663178354Ssamhostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1664283535Sadrian	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1665178354Ssam{
1666178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1667178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1668178354Ssam	struct ieee80211_frame *wh;
1669178354Ssam	uint8_t *frm, *efrm, *sfrm;
1670178354Ssam	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1671178354Ssam	int reassoc, resp;
1672178354Ssam	uint8_t rate;
1673178354Ssam
1674178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1675178354Ssam	frm = (uint8_t *)&wh[1];
1676178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1677178354Ssam	switch (subtype) {
1678178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1679178354Ssam		/*
1680178354Ssam		 * We process beacon/probe response frames when scanning;
1681178354Ssam		 * otherwise we check beacon frames for overlapping non-ERP
1682178354Ssam		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1683298360Savos		 */
1684298360Savos		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1685178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1686178354Ssam			return;
1687178354Ssam		}
1688298360Savos		/* FALLTHROUGH */
1689298360Savos	case IEEE80211_FC0_SUBTYPE_BEACON: {
1690298360Savos		struct ieee80211_scanparams scan;
1691298360Savos
1692178354Ssam		/* NB: accept off-channel frames */
1693283535Sadrian		/* XXX TODO: use rxstatus to determine off-channel details */
1694283535Sadrian		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1695178354Ssam			return;
1696178354Ssam		/*
1697178354Ssam		 * Count frame now that we know it's to be processed.
1698178354Ssam		 */
1699178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1700178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1701178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1702178354Ssam		} else
1703178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1704178354Ssam		/*
1705178354Ssam		 * If scanning, just pass information to the scan module.
1706178354Ssam		 */
1707178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1708178354Ssam			if (scan.status == 0 &&		/* NB: on channel */
1709178354Ssam			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1710178354Ssam				/*
1711178354Ssam				 * Actively scanning a channel marked passive;
1712178354Ssam				 * send a probe request now that we know there
1713178354Ssam				 * is 802.11 traffic present.
1714178354Ssam				 *
1715178354Ssam				 * XXX check if the beacon we recv'd gives
1716178354Ssam				 * us what we need and suppress the probe req
1717178354Ssam				 */
1718178354Ssam				ieee80211_probe_curchan(vap, 1);
1719178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1720178354Ssam			}
1721282742Sadrian			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1722282742Sadrian			    subtype, rssi, nf);
1723178354Ssam			return;
1724178354Ssam		}
1725178354Ssam		/*
1726178354Ssam		 * Check beacon for overlapping bss w/ non ERP stations.
1727178354Ssam		 * If we detect one and protection is configured but not
1728178354Ssam		 * enabled, enable it and start a timer that'll bring us
1729178354Ssam		 * out if we stop seeing the bss.
1730178354Ssam		 */
1731178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1732178354Ssam		    scan.status == 0 &&			/* NB: on-channel */
1733178354Ssam		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1734178354Ssam		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1735178354Ssam			ic->ic_lastnonerp = ticks;
1736178354Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1737178354Ssam			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1738178354Ssam			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1739178354Ssam				IEEE80211_NOTE_FRAME(vap,
1740178354Ssam				    IEEE80211_MSG_ASSOC, wh,
1741178354Ssam				    "non-ERP present on channel %d "
1742178354Ssam				    "(saw erp 0x%x from channel %d), "
1743178354Ssam				    "enable use of protection",
1744178354Ssam				    ic->ic_curchan->ic_ieee,
1745178354Ssam				    scan.erp, scan.chan);
1746178354Ssam				ic->ic_flags |= IEEE80211_F_USEPROT;
1747178354Ssam				ieee80211_notify_erp(ic);
1748178354Ssam			}
1749178354Ssam		}
1750178354Ssam		/*
1751178354Ssam		 * Check beacon for non-HT station on HT channel
1752178354Ssam		 * and update HT BSS occupancy as appropriate.
1753178354Ssam		 */
1754178354Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1755178354Ssam			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1756178354Ssam				/*
1757178354Ssam				 * Off control channel; only check frames
1758178354Ssam				 * that come in the extension channel when
1759178354Ssam				 * operating w/ HT40.
1760178354Ssam				 */
1761178354Ssam				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1762178354Ssam					break;
1763178354Ssam				if (scan.chan != ic->ic_curchan->ic_extieee)
1764178354Ssam					break;
1765178354Ssam			}
1766178354Ssam			if (scan.htinfo == NULL) {
1767178354Ssam				ieee80211_htprot_update(ic,
1768178354Ssam				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1769178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1770178354Ssam			} else if (ishtmixed(scan.htinfo)) {
1771178354Ssam				/* XXX? take NONHT_PRESENT from beacon? */
1772178354Ssam				ieee80211_htprot_update(ic,
1773178354Ssam				    IEEE80211_HTINFO_OPMODE_MIXED |
1774178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1775178354Ssam			}
1776178354Ssam		}
1777178354Ssam		break;
1778178354Ssam	}
1779178354Ssam
1780178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1781178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1782178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1783178354Ssam			return;
1784178354Ssam		}
1785178354Ssam		/*
1786228514Sadrian		 * Consult the ACL policy module if setup.
1787228514Sadrian		 */
1788228622Sbschmidt		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1789228514Sadrian			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1790228514Sadrian			    wh, NULL, "%s", "disallowed by ACL");
1791228514Sadrian			vap->iv_stats.is_rx_acl++;
1792228514Sadrian			return;
1793228514Sadrian		}
1794228514Sadrian		/*
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		 */
1873228622Sbschmidt		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1874178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1875178354Ssam			    wh, NULL, "%s", "disallowed by ACL");
1876178354Ssam			vap->iv_stats.is_rx_acl++;
1877178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1878178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH,
1879178354Ssam			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1880178354Ssam			return;
1881178354Ssam		}
1882178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1883178354Ssam			IEEE80211_DISCARD(vap,
1884178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1885178354Ssam			    wh, NULL, "%s", "TKIP countermeasures enabled");
1886178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1887178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1888178354Ssam				IEEE80211_FC0_SUBTYPE_AUTH,
1889178354Ssam				IEEE80211_REASON_MIC_FAILURE);
1890178354Ssam			return;
1891178354Ssam		}
1892178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1893192468Ssam			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1894192468Ssam			    seq, status);
1895178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1896192468Ssam			hostap_auth_open(ni, wh, rssi, nf, seq, status);
1897178354Ssam		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1898178354Ssam			authalgreject(ni, wh, algo,
1899178354Ssam			    seq+1, IEEE80211_STATUS_ALG);
1900178354Ssam			return;
1901178354Ssam		} else {
1902178354Ssam			/*
1903178354Ssam			 * We assume that an unknown algorithm is the result
1904178354Ssam			 * of a decryption failure on a shared key auth frame;
1905178354Ssam			 * return a status code appropriate for that instead
1906178354Ssam			 * of IEEE80211_STATUS_ALG.
1907178354Ssam			 *
1908178354Ssam			 * NB: a seq# of 4 is intentional; the decrypted
1909178354Ssam			 *     frame likely has a bogus seq value.
1910178354Ssam			 */
1911178354Ssam			authalgreject(ni, wh, algo,
1912178354Ssam			    4, IEEE80211_STATUS_CHALLENGE);
1913178354Ssam			return;
1914178354Ssam		}
1915178354Ssam		break;
1916178354Ssam	}
1917178354Ssam
1918178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1919178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1920178354Ssam		uint16_t capinfo, lintval;
1921178354Ssam		struct ieee80211_rsnparms rsnparms;
1922178354Ssam
1923178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1924178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1925178354Ssam			return;
1926178354Ssam		}
1927178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1928178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1929178354Ssam			    wh, NULL, "%s", "wrong bssid");
1930178354Ssam			vap->iv_stats.is_rx_assoc_bss++;
1931178354Ssam			return;
1932178354Ssam		}
1933178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1934178354Ssam			reassoc = 1;
1935178354Ssam			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1936178354Ssam		} else {
1937178354Ssam			reassoc = 0;
1938178354Ssam			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1939178354Ssam		}
1940178354Ssam		if (ni == vap->iv_bss) {
1941178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1942178354Ssam			    "deny %s request, sta not authenticated",
1943178354Ssam			    reassoc ? "reassoc" : "assoc");
1944178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1945178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1946178354Ssam			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1947178354Ssam			vap->iv_stats.is_rx_assoc_notauth++;
1948178354Ssam			return;
1949178354Ssam		}
1950178354Ssam
1951178354Ssam		/*
1952178354Ssam		 * asreq frame format
1953178354Ssam		 *	[2] capability information
1954178354Ssam		 *	[2] listen interval
1955178354Ssam		 *	[6*] current AP address (reassoc only)
1956178354Ssam		 *	[tlv] ssid
1957178354Ssam		 *	[tlv] supported rates
1958178354Ssam		 *	[tlv] extended supported rates
1959178354Ssam		 *	[tlv] WPA or RSN
1960178354Ssam		 *	[tlv] HT capabilities
1961178354Ssam		 *	[tlv] Atheros capabilities
1962178354Ssam		 */
1963178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1964178354Ssam		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1965178354Ssam		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1966178354Ssam		if (reassoc)
1967178354Ssam			frm += 6;	/* ignore current AP info */
1968178354Ssam		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1969178354Ssam		sfrm = frm;
1970178354Ssam		while (efrm - frm > 1) {
1971178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1972178354Ssam			switch (*frm) {
1973178354Ssam			case IEEE80211_ELEMID_SSID:
1974178354Ssam				ssid = frm;
1975178354Ssam				break;
1976178354Ssam			case IEEE80211_ELEMID_RATES:
1977178354Ssam				rates = frm;
1978178354Ssam				break;
1979178354Ssam			case IEEE80211_ELEMID_XRATES:
1980178354Ssam				xrates = frm;
1981178354Ssam				break;
1982178354Ssam			case IEEE80211_ELEMID_RSN:
1983178354Ssam				rsn = frm;
1984178354Ssam				break;
1985178354Ssam			case IEEE80211_ELEMID_HTCAP:
1986178354Ssam				htcap = frm;
1987178354Ssam				break;
1988178354Ssam			case IEEE80211_ELEMID_VENDOR:
1989178354Ssam				if (iswpaoui(frm))
1990178354Ssam					wpa = frm;
1991178354Ssam				else if (iswmeinfo(frm))
1992178354Ssam					wme = frm;
1993190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1994178354Ssam				else if (isatherosoui(frm))
1995178354Ssam					ath = frm;
1996190391Ssam#endif
1997193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1998178354Ssam					if (ishtcapoui(frm) && htcap == NULL)
1999178354Ssam						htcap = frm;
2000178354Ssam				}
2001178354Ssam				break;
2002178354Ssam			}
2003178354Ssam			frm += frm[1] + 2;
2004178354Ssam		}
2005178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2006178354Ssam		if (xrates != NULL)
2007178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
2008178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
2009178354Ssam		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2010178354Ssam		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2011178354Ssam		if (htcap != NULL) {
2012178354Ssam			IEEE80211_VERIFY_LENGTH(htcap[1],
2013178354Ssam			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2014178354Ssam			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2015178354Ssam			         sizeof(struct ieee80211_ie_htcap)-2,
2016178354Ssam			     return);		/* XXX just NULL out? */
2017178354Ssam		}
2018178354Ssam
2019178354Ssam		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2020178354Ssam		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2021178354Ssam			return;
2022178354Ssam		/* discard challenge after association */
2023178354Ssam		if (ni->ni_challenge != NULL) {
2024283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2025178354Ssam			ni->ni_challenge = NULL;
2026178354Ssam		}
2027178354Ssam		/* NB: 802.11 spec says to ignore station's privacy bit */
2028178354Ssam		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2029178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2030178354Ssam			    "capability", capinfo);
2031178354Ssam			return;
2032178354Ssam		}
2033178354Ssam		/*
2034178354Ssam		 * Disallow re-associate w/ invalid slot time setting.
2035178354Ssam		 */
2036178354Ssam		if (ni->ni_associd != 0 &&
2037178354Ssam		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2038178354Ssam		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2039178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2040178354Ssam			    "slot time", capinfo);
2041178354Ssam			return;
2042178354Ssam		}
2043178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
2044178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2045178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2046178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
2047178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2048178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2049178354Ssam			return;
2050178354Ssam		}
2051178354Ssam		/*
2052178354Ssam		 * If constrained to 11g-only stations reject an
2053178354Ssam		 * 11b-only station.  We cheat a bit here by looking
2054178354Ssam		 * at the max negotiated xmit rate and assuming anyone
2055178354Ssam		 * with a best rate <24Mb/s is an 11b station.
2056178354Ssam		 */
2057178354Ssam		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2058178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2059178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2060178354Ssam			return;
2061178354Ssam		}
2062178354Ssam		/*
2063178354Ssam		 * Do HT rate set handling and setup HT node state.
2064178354Ssam		 */
2065178354Ssam		ni->ni_chan = vap->iv_bss->ni_chan;
2066178354Ssam		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2067178354Ssam			rate = ieee80211_setup_htrates(ni, htcap,
2068178354Ssam				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2069178354Ssam				IEEE80211_F_DOBRS);
2070178354Ssam			if (rate & IEEE80211_RATE_BASIC) {
2071178354Ssam				ratesetmismatch(ni, wh, reassoc, resp,
2072178354Ssam				    "HT", rate);
2073178354Ssam				vap->iv_stats.is_ht_assoc_norate++;
2074178354Ssam				return;
2075178354Ssam			}
2076183254Ssam			ieee80211_ht_node_init(ni);
2077183254Ssam			ieee80211_ht_updatehtcap(ni, htcap);
2078178354Ssam		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2079178354Ssam			ieee80211_ht_node_cleanup(ni);
2080190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2081297604Sadrian		/* Always do ff node cleanup; for A-MSDU */
2082297604Sadrian		ieee80211_ff_node_cleanup(ni);
2083190579Ssam#endif
2084178354Ssam		/*
2085178354Ssam		 * Allow AMPDU operation only with unencrypted traffic
2086178354Ssam		 * or AES-CCM; the 11n spec only specifies these ciphers
2087178354Ssam		 * so permitting any others is undefined and can lead
2088178354Ssam		 * to interoperability problems.
2089178354Ssam		 */
2090178354Ssam		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2091178354Ssam		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2092178354Ssam		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2093178354Ssam		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2094178354Ssam			IEEE80211_NOTE(vap,
2095178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2096178354Ssam			    "disallow HT use because WEP or TKIP requested, "
2097178354Ssam			    "capinfo 0x%x ucastcipher %d", capinfo,
2098178354Ssam			    rsnparms.rsn_ucastcipher);
2099178354Ssam			ieee80211_ht_node_cleanup(ni);
2100297604Sadrian#ifdef IEEE80211_SUPPORT_SUPERG
2101297604Sadrian			/* Always do ff node cleanup; for A-MSDU */
2102297604Sadrian			ieee80211_ff_node_cleanup(ni);
2103297604Sadrian#endif
2104178354Ssam			vap->iv_stats.is_ht_assoc_downgrade++;
2105178354Ssam		}
2106178354Ssam		/*
2107178354Ssam		 * If constrained to 11n-only stations reject legacy stations.
2108178354Ssam		 */
2109193655Ssam		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2110178354Ssam		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2111178354Ssam			htcapmismatch(ni, wh, reassoc, resp);
2112178354Ssam			vap->iv_stats.is_ht_assoc_nohtcap++;
2113178354Ssam			return;
2114178354Ssam		}
2115178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2116192468Ssam		ni->ni_noise = nf;
2117178354Ssam		ni->ni_intval = lintval;
2118178354Ssam		ni->ni_capinfo = capinfo;
2119178354Ssam		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2120178354Ssam		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2121178354Ssam		/*
2122178354Ssam		 * Store the IEs.
2123178354Ssam		 * XXX maybe better to just expand
2124178354Ssam		 */
2125178354Ssam		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2126178354Ssam#define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2127178354Ssam			if (wpa != NULL)
2128178354Ssam				setie(wpa_ie, wpa - sfrm);
2129178354Ssam			if (rsn != NULL)
2130178354Ssam				setie(rsn_ie, rsn - sfrm);
2131178354Ssam			if (htcap != NULL)
2132178354Ssam				setie(htcap_ie, htcap - sfrm);
2133178354Ssam			if (wme != NULL) {
2134178354Ssam				setie(wme_ie, wme - sfrm);
2135178354Ssam				/*
2136178354Ssam				 * Mark node as capable of QoS.
2137178354Ssam				 */
2138178354Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
2139178354Ssam			} else
2140178354Ssam				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2141190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2142178354Ssam			if (ath != NULL) {
2143178354Ssam				setie(ath_ie, ath - sfrm);
2144178354Ssam				/*
2145178354Ssam				 * Parse ATH station parameters.
2146178354Ssam				 */
2147178354Ssam				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2148178354Ssam			} else
2149190391Ssam#endif
2150178354Ssam				ni->ni_ath_flags = 0;
2151178354Ssam#undef setie
2152178354Ssam		} else {
2153178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2154178354Ssam			ni->ni_ath_flags = 0;
2155178354Ssam		}
2156178354Ssam		ieee80211_node_join(ni, resp);
2157178354Ssam		ieee80211_deliver_l2uf(ni);
2158178354Ssam		break;
2159178354Ssam	}
2160178354Ssam
2161178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2162178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2163178354Ssam		uint16_t reason;
2164178354Ssam
2165178354Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
2166178354Ssam		    /* NB: can happen when in promiscuous mode */
2167178354Ssam		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2168178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2169178354Ssam			break;
2170178354Ssam		}
2171178354Ssam		/*
2172178354Ssam		 * deauth/disassoc frame format
2173178354Ssam		 *	[2] reason
2174178354Ssam		 */
2175178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2176178354Ssam		reason = le16toh(*(uint16_t *)frm);
2177178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2178178354Ssam			vap->iv_stats.is_rx_deauth++;
2179178354Ssam			IEEE80211_NODE_STAT(ni, rx_deauth);
2180178354Ssam		} else {
2181178354Ssam			vap->iv_stats.is_rx_disassoc++;
2182178354Ssam			IEEE80211_NODE_STAT(ni, rx_disassoc);
2183178354Ssam		}
2184178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2185298364Savos		    "recv %s (reason: %d (%s))",
2186298376Savos		    ieee80211_mgt_subtype_name(subtype),
2187298364Savos		    reason, ieee80211_reason_to_string(reason));
2188178354Ssam		if (ni != vap->iv_bss)
2189178354Ssam			ieee80211_node_leave(ni);
2190178354Ssam		break;
2191178354Ssam	}
2192178354Ssam
2193178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
2194218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2195218958Sbschmidt		if (ni == vap->iv_bss) {
2196218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2197218958Sbschmidt			    wh, NULL, "%s", "unknown node");
2198218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
2199218958Sbschmidt		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2200218958Sbschmidt		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2201218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2202218958Sbschmidt			    wh, NULL, "%s", "not for us");
2203218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
2204218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
2205218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2206218927Sbschmidt			    wh, NULL, "wrong state %s",
2207218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
2208178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2209218958Sbschmidt		} else {
2210218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
2211218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2212218927Sbschmidt		}
2213178354Ssam		break;
2214178354Ssam
2215178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2216178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2217295795Savos	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2218218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
2219218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2220218927Sbschmidt		    wh, NULL, "%s", "not handled");
2221218927Sbschmidt		vap->iv_stats.is_rx_mgtdiscard++;
2222218927Sbschmidt		break;
2223218927Sbschmidt
2224178354Ssam	default:
2225178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2226218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
2227178354Ssam		vap->iv_stats.is_rx_badsubtype++;
2228178354Ssam		break;
2229178354Ssam	}
2230178354Ssam}
2231178354Ssam
2232191546Ssamstatic void
2233191546Ssamhostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2234191546Ssam{
2235191546Ssam	switch (subtype) {
2236191546Ssam	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2237241138Sadrian		ni->ni_vap->iv_recv_pspoll(ni, m);
2238191546Ssam		break;
2239191546Ssam	case IEEE80211_FC0_SUBTYPE_BAR:
2240191546Ssam		ieee80211_recv_bar(ni, m);
2241191546Ssam		break;
2242191546Ssam	}
2243191546Ssam}
2244191546Ssam
2245178354Ssam/*
2246178354Ssam * Process a received ps-poll frame.
2247178354Ssam */
2248241138Sadrianvoid
2249241138Sadrianieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2250178354Ssam{
2251178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2252248069Sadrian	struct ieee80211com *ic = vap->iv_ic;
2253178354Ssam	struct ieee80211_frame_min *wh;
2254178354Ssam	struct mbuf *m;
2255178354Ssam	uint16_t aid;
2256178354Ssam	int qlen;
2257178354Ssam
2258178354Ssam	wh = mtod(m0, struct ieee80211_frame_min *);
2259178354Ssam	if (ni->ni_associd == 0) {
2260178354Ssam		IEEE80211_DISCARD(vap,
2261178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2262178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2263178354Ssam		    "%s", "unassociated station");
2264178354Ssam		vap->iv_stats.is_ps_unassoc++;
2265178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2266178354Ssam			IEEE80211_REASON_NOT_ASSOCED);
2267178354Ssam		return;
2268178354Ssam	}
2269178354Ssam
2270178354Ssam	aid = le16toh(*(uint16_t *)wh->i_dur);
2271178354Ssam	if (aid != ni->ni_associd) {
2272178354Ssam		IEEE80211_DISCARD(vap,
2273178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2274178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2275178354Ssam		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2276178354Ssam		    ni->ni_associd, aid);
2277178354Ssam		vap->iv_stats.is_ps_badaid++;
2278180837Ssam		/*
2279180837Ssam		 * NB: We used to deauth the station but it turns out
2280180837Ssam		 * the Blackberry Curve 8230 (and perhaps other devices)
2281180837Ssam		 * sometimes send the wrong AID when WME is negotiated.
2282180837Ssam		 * Being more lenient here seems ok as we already check
2283180837Ssam		 * the station is associated and we only return frames
2284180837Ssam		 * queued for the station (i.e. we don't use the AID).
2285180837Ssam		 */
2286178354Ssam		return;
2287178354Ssam	}
2288178354Ssam
2289178354Ssam	/* Okay, take the first queued packet and put it out... */
2290184288Ssam	m = ieee80211_node_psq_dequeue(ni, &qlen);
2291178354Ssam	if (m == NULL) {
2292178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2293178354Ssam		    "%s", "recv ps-poll, but queue empty");
2294178354Ssam		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2295178354Ssam		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2296178354Ssam		if (vap->iv_set_tim != NULL)
2297178354Ssam			vap->iv_set_tim(ni, 0);	/* just in case */
2298178354Ssam		return;
2299178354Ssam	}
2300178354Ssam	/*
2301178354Ssam	 * If there are more packets, set the more packets bit
2302178354Ssam	 * in the packet dispatched to the station; otherwise
2303178354Ssam	 * turn off the TIM bit.
2304178354Ssam	 */
2305178354Ssam	if (qlen != 0) {
2306178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2307178354Ssam		    "recv ps-poll, send packet, %u still queued", qlen);
2308178354Ssam		m->m_flags |= M_MORE_DATA;
2309178354Ssam	} else {
2310178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2311178354Ssam		    "%s", "recv ps-poll, send packet, queue empty");
2312178354Ssam		if (vap->iv_set_tim != NULL)
2313178354Ssam			vap->iv_set_tim(ni, 0);
2314178354Ssam	}
2315178354Ssam	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2316184288Ssam
2317245098Sadrian	/*
2318248069Sadrian	 * Do the right thing; if it's an encap'ed frame then
2319289164Sadrian	 * call ieee80211_parent_xmitpkt() else
2320254082Sadrian	 * call ieee80211_vap_xmitpkt().
2321245098Sadrian	 */
2322248069Sadrian	if (m->m_flags & M_ENCAP) {
2323289164Sadrian		(void) ieee80211_parent_xmitpkt(ic, m);
2324248069Sadrian	} else {
2325254082Sadrian		(void) ieee80211_vap_xmitpkt(vap, m);
2326245098Sadrian	}
2327178354Ssam}
2328