ieee80211_hostap.c revision 301722
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 301722 2016-06-09 08:19:42Z 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		}
415301722Savos		if (mcopy != NULL)
416301722Savos			(void) ieee80211_vap_xmitpkt(vap, mcopy);
417178354Ssam	}
418178354Ssam	if (m != NULL) {
419178354Ssam		/*
420178354Ssam		 * Mark frame as coming from vap's interface.
421178354Ssam		 */
422178354Ssam		m->m_pkthdr.rcvif = ifp;
423178354Ssam		if (m->m_flags & M_MCAST) {
424178354Ssam			/*
425178354Ssam			 * Spam DWDS vap's w/ multicast traffic.
426178354Ssam			 */
427178354Ssam			/* XXX only if dwds in use? */
428178354Ssam			ieee80211_dwds_mcast(vap, m);
429178354Ssam		}
430178354Ssam		if (ni->ni_vlan != 0) {
431178354Ssam			/* attach vlan tag */
432178354Ssam			m->m_pkthdr.ether_vtag = ni->ni_vlan;
433178354Ssam			m->m_flags |= M_VLANTAG;
434178354Ssam		}
435178354Ssam		ifp->if_input(ifp, m);
436178354Ssam	}
437178354Ssam}
438178354Ssam
439178354Ssam/*
440178354Ssam * Decide if a received management frame should be
441178354Ssam * printed when debugging is enabled.  This filters some
442178354Ssam * of the less interesting frames that come frequently
443178354Ssam * (e.g. beacons).
444178354Ssam */
445178354Ssamstatic __inline int
446178354Ssamdoprint(struct ieee80211vap *vap, int subtype)
447178354Ssam{
448178354Ssam	switch (subtype) {
449178354Ssam	case IEEE80211_FC0_SUBTYPE_BEACON:
450178354Ssam		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
451178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
452178354Ssam		return 0;
453178354Ssam	}
454178354Ssam	return 1;
455178354Ssam}
456178354Ssam
457178354Ssam/*
458178354Ssam * Process a received frame.  The node associated with the sender
459178354Ssam * should be supplied.  If nothing was found in the node table then
460178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
461178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
462178354Ssam * during AP scanning to select a AP to associate with; it can have
463178354Ssam * any units so long as values have consistent units and higher values
464178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
465178354Ssam * by the 802.11 layer.
466178354Ssam */
467178354Ssamstatic int
468283535Sadrianhostap_input(struct ieee80211_node *ni, struct mbuf *m,
469283535Sadrian    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
470178354Ssam{
471178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
472178354Ssam	struct ieee80211com *ic = ni->ni_ic;
473178354Ssam	struct ifnet *ifp = vap->iv_ifp;
474178354Ssam	struct ieee80211_frame *wh;
475178354Ssam	struct ieee80211_key *key;
476178354Ssam	struct ether_header *eh;
477203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
478178354Ssam	uint8_t dir, type, subtype, qos;
479178354Ssam	uint8_t *bssid;
480178354Ssam
481183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
482178354Ssam		/*
483178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
484183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
485183247Ssam		 * here but were received out of order and been held on
486183247Ssam		 * the reorder queue.  When resubmitted they are marked
487183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
488183247Ssam		 * the normal processing.
489178354Ssam		 */
490178354Ssam		wh = mtod(m, struct ieee80211_frame *);
491178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
492178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
493178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
494178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
495178354Ssam		goto resubmit_ampdu;
496178354Ssam	}
497178354Ssam
498178354Ssam	KASSERT(ni != NULL, ("null node"));
499178354Ssam	ni->ni_inact = ni->ni_inact_reload;
500178354Ssam
501178354Ssam	type = -1;			/* undefined */
502178354Ssam
503178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
504178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
505178354Ssam		    ni->ni_macaddr, NULL,
506178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
507178354Ssam		vap->iv_stats.is_rx_tooshort++;
508178354Ssam		goto out;
509178354Ssam	}
510178354Ssam	/*
511178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
512178354Ssam	 * frame format but don't reference fields past outside
513178354Ssam	 * ieee80211_frame_min w/o first validating the data is
514178354Ssam	 * present.
515178354Ssam	 */
516178354Ssam	wh = mtod(m, struct ieee80211_frame *);
517178354Ssam
518178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
519178354Ssam	    IEEE80211_FC0_VERSION_0) {
520178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
521191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
522191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
523178354Ssam		vap->iv_stats.is_rx_badversion++;
524178354Ssam		goto err;
525178354Ssam	}
526178354Ssam
527178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
528178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
529178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
530178354Ssam	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
531178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS)
532178354Ssam			bssid = wh->i_addr1;
533178354Ssam		else if (type == IEEE80211_FC0_TYPE_CTL)
534178354Ssam			bssid = wh->i_addr1;
535178354Ssam		else {
536178354Ssam			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
537178354Ssam				IEEE80211_DISCARD_MAC(vap,
538178354Ssam				    IEEE80211_MSG_ANY, ni->ni_macaddr,
539178354Ssam				    NULL, "too short (2): len %u",
540178354Ssam				    m->m_pkthdr.len);
541178354Ssam				vap->iv_stats.is_rx_tooshort++;
542178354Ssam				goto out;
543178354Ssam			}
544178354Ssam			bssid = wh->i_addr3;
545178354Ssam		}
546178354Ssam		/*
547178354Ssam		 * Validate the bssid.
548178354Ssam		 */
549178354Ssam		if (!(type == IEEE80211_FC0_TYPE_MGT &&
550178354Ssam		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
551178354Ssam		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
552178354Ssam		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
553178354Ssam			/* not interested in */
554178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
555178354Ssam			    bssid, NULL, "%s", "not to bss");
556178354Ssam			vap->iv_stats.is_rx_wrongbss++;
557178354Ssam			goto out;
558178354Ssam		}
559178354Ssam
560178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
561192468Ssam		ni->ni_noise = nf;
562282820Sadrian		if (IEEE80211_HAS_SEQ(type, subtype)) {
563178354Ssam			uint8_t tid = ieee80211_gettid(wh);
564178354Ssam			if (IEEE80211_QOS_HAS_SEQ(wh) &&
565178354Ssam			    TID_TO_WME_AC(tid) >= WME_AC_VI)
566178354Ssam				ic->ic_wme.wme_hipri_traffic++;
567296254Savos			if (! ieee80211_check_rxseq(ni, wh, bssid))
568178354Ssam				goto out;
569178354Ssam		}
570178354Ssam	}
571178354Ssam
572178354Ssam	switch (type) {
573178354Ssam	case IEEE80211_FC0_TYPE_DATA:
574178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
575178354Ssam		if (m->m_len < hdrspace &&
576178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
577178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
578178354Ssam			    ni->ni_macaddr, NULL,
579178354Ssam			    "data too short: expecting %u", hdrspace);
580178354Ssam			vap->iv_stats.is_rx_tooshort++;
581178354Ssam			goto out;		/* XXX */
582178354Ssam		}
583178354Ssam		if (!(dir == IEEE80211_FC1_DIR_TODS ||
584178354Ssam		     (dir == IEEE80211_FC1_DIR_DSTODS &&
585178354Ssam		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
586178354Ssam			if (dir != IEEE80211_FC1_DIR_DSTODS) {
587178354Ssam				IEEE80211_DISCARD(vap,
588178354Ssam				    IEEE80211_MSG_INPUT, wh, "data",
589178354Ssam				    "incorrect dir 0x%x", dir);
590178354Ssam			} else {
591178354Ssam				IEEE80211_DISCARD(vap,
592178354Ssam				    IEEE80211_MSG_INPUT |
593178354Ssam				    IEEE80211_MSG_WDS, wh,
594178354Ssam				    "4-address data",
595178354Ssam				    "%s", "DWDS not enabled");
596178354Ssam			}
597178354Ssam			vap->iv_stats.is_rx_wrongdir++;
598178354Ssam			goto out;
599178354Ssam		}
600178354Ssam		/* check if source STA is associated */
601178354Ssam		if (ni == vap->iv_bss) {
602178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
603178354Ssam			    wh, "data", "%s", "unknown src");
604178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
605178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
606178354Ssam			    IEEE80211_REASON_NOT_AUTHED);
607178354Ssam			vap->iv_stats.is_rx_notassoc++;
608178354Ssam			goto err;
609178354Ssam		}
610178354Ssam		if (ni->ni_associd == 0) {
611178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
612178354Ssam			    wh, "data", "%s", "unassoc src");
613178354Ssam			IEEE80211_SEND_MGMT(ni,
614178354Ssam			    IEEE80211_FC0_SUBTYPE_DISASSOC,
615178354Ssam			    IEEE80211_REASON_NOT_ASSOCED);
616178354Ssam			vap->iv_stats.is_rx_notassoc++;
617178354Ssam			goto err;
618178354Ssam		}
619178354Ssam
620178354Ssam		/*
621178354Ssam		 * Check for power save state change.
622178354Ssam		 * XXX out-of-order A-MPDU frames?
623178354Ssam		 */
624178354Ssam		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
625178354Ssam		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
626241138Sadrian			vap->iv_node_ps(ni,
627178354Ssam				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
628178354Ssam		/*
629178354Ssam		 * For 4-address packets handle WDS discovery
630178354Ssam		 * notifications.  Once a WDS link is setup frames
631178354Ssam		 * are just delivered to the WDS vap (see below).
632178354Ssam		 */
633178354Ssam		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
634178354Ssam			if (!ieee80211_node_is_authorized(ni)) {
635178354Ssam				IEEE80211_DISCARD(vap,
636178354Ssam				    IEEE80211_MSG_INPUT |
637178354Ssam				    IEEE80211_MSG_WDS, wh,
638178354Ssam				    "4-address data",
639178354Ssam				    "%s", "unauthorized port");
640178354Ssam				vap->iv_stats.is_rx_unauth++;
641178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
642178354Ssam				goto err;
643178354Ssam			}
644178354Ssam			ieee80211_dwds_discover(ni, m);
645178354Ssam			return type;
646178354Ssam		}
647178354Ssam
648178354Ssam		/*
649183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
650183247Ssam		 * processed directly then ieee80211_ampdu_reorder
651178354Ssam		 * will return 0; otherwise it has consumed the mbuf
652178354Ssam		 * and we should do nothing more with it.
653178354Ssam		 */
654183247Ssam		if ((m->m_flags & M_AMPDU) &&
655178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
656178354Ssam			m = NULL;
657178354Ssam			goto out;
658178354Ssam		}
659178354Ssam	resubmit_ampdu:
660178354Ssam
661178354Ssam		/*
662178354Ssam		 * Handle privacy requirements.  Note that we
663178354Ssam		 * must not be preempted from here until after
664178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
665178354Ssam		 * otherwise we may violate assumptions in the
666178354Ssam		 * crypto cipher modules used to do delayed update
667178354Ssam		 * of replay sequence numbers.
668178354Ssam		 */
669260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
670178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
671178354Ssam				/*
672178354Ssam				 * Discard encrypted frames when privacy is off.
673178354Ssam				 */
674178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
675178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
676178354Ssam				vap->iv_stats.is_rx_noprivacy++;
677178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
678178354Ssam				goto out;
679178354Ssam			}
680178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
681178354Ssam			if (key == NULL) {
682178354Ssam				/* NB: stats+msgs handled in crypto_decap */
683178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
684178354Ssam				goto out;
685178354Ssam			}
686178354Ssam			wh = mtod(m, struct ieee80211_frame *);
687260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
688178354Ssam		} else {
689178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
690178354Ssam			key = NULL;
691178354Ssam		}
692178354Ssam
693178354Ssam		/*
694178354Ssam		 * Save QoS bits for use below--before we strip the header.
695178354Ssam		 */
696178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
697178354Ssam			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
698178354Ssam			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
699178354Ssam			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
700178354Ssam		} else
701178354Ssam			qos = 0;
702178354Ssam
703178354Ssam		/*
704178354Ssam		 * Next up, any fragmentation.
705178354Ssam		 */
706178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
707178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
708178354Ssam			if (m == NULL) {
709178354Ssam				/* Fragment dropped or frame not complete yet */
710178354Ssam				goto out;
711178354Ssam			}
712178354Ssam		}
713178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
714178354Ssam
715178354Ssam		/*
716178354Ssam		 * Next strip any MSDU crypto bits.
717178354Ssam		 */
718178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
719178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
720178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
721178354Ssam			vap->iv_stats.is_rx_demicfail++;
722178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
723178354Ssam			goto out;
724178354Ssam		}
725178354Ssam		/* copy to listener after decrypt */
726192468Ssam		if (ieee80211_radiotap_active_vap(vap))
727192468Ssam			ieee80211_radiotap_rx(vap, m);
728178354Ssam		need_tap = 0;
729178354Ssam		/*
730178354Ssam		 * Finally, strip the 802.11 header.
731178354Ssam		 */
732178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
733178354Ssam		if (m == NULL) {
734178354Ssam			/* XXX mask bit to check for both */
735178354Ssam			/* don't count Null data frames as errors */
736178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
737178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
738178354Ssam				goto out;
739178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
740178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
741178354Ssam			vap->iv_stats.is_rx_decap++;
742178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
743178354Ssam			goto err;
744178354Ssam		}
745178354Ssam		eh = mtod(m, struct ether_header *);
746178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
747178354Ssam			/*
748178354Ssam			 * Deny any non-PAE frames received prior to
749178354Ssam			 * authorization.  For open/shared-key
750178354Ssam			 * authentication the port is mark authorized
751178354Ssam			 * after authentication completes.  For 802.1x
752178354Ssam			 * the port is not marked authorized by the
753178354Ssam			 * authenticator until the handshake has completed.
754178354Ssam			 */
755178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
756178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
757178354Ssam				    eh->ether_shost, "data",
758178354Ssam				    "unauthorized port: ether type 0x%x len %u",
759178354Ssam				    eh->ether_type, m->m_pkthdr.len);
760178354Ssam				vap->iv_stats.is_rx_unauth++;
761178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
762178354Ssam				goto err;
763178354Ssam			}
764178354Ssam		} else {
765178354Ssam			/*
766178354Ssam			 * When denying unencrypted frames, discard
767178354Ssam			 * any non-PAE frames received without encryption.
768178354Ssam			 */
769178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
770178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
771178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
772178354Ssam				/*
773178354Ssam				 * Drop unencrypted frames.
774178354Ssam				 */
775178354Ssam				vap->iv_stats.is_rx_unencrypted++;
776178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
777178354Ssam				goto out;
778178354Ssam			}
779178354Ssam		}
780178354Ssam		/* XXX require HT? */
781178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
782178354Ssam			m = ieee80211_decap_amsdu(ni, m);
783178354Ssam			if (m == NULL)
784178354Ssam				return IEEE80211_FC0_TYPE_DATA;
785190391Ssam		} else {
786190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
787190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
788190391Ssam			if (m == NULL)
789178354Ssam				return IEEE80211_FC0_TYPE_DATA;
790190391Ssam#endif
791178354Ssam		}
792178354Ssam		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
793178354Ssam			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
794178354Ssam		else
795178354Ssam			hostap_deliver_data(vap, ni, m);
796178354Ssam		return IEEE80211_FC0_TYPE_DATA;
797178354Ssam
798178354Ssam	case IEEE80211_FC0_TYPE_MGT:
799178354Ssam		vap->iv_stats.is_rx_mgmt++;
800178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
801178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
802178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
803178354Ssam			    wh, "mgt", "incorrect dir 0x%x", dir);
804178354Ssam			vap->iv_stats.is_rx_wrongdir++;
805178354Ssam			goto err;
806178354Ssam		}
807178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
808178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
809178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
810178354Ssam			    m->m_pkthdr.len);
811178354Ssam			vap->iv_stats.is_rx_tooshort++;
812178354Ssam			goto out;
813178354Ssam		}
814178354Ssam		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
815178354Ssam			/* ensure return frames are unicast */
816178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
817178354Ssam			    wh, NULL, "source is multicast: %s",
818178354Ssam			    ether_sprintf(wh->i_addr2));
819178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
820178354Ssam			goto out;
821178354Ssam		}
822178354Ssam#ifdef IEEE80211_DEBUG
823178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
824178354Ssam		    ieee80211_msg_dumppkts(vap)) {
825178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
826298376Savos			    ieee80211_mgt_subtype_name(subtype),
827178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
828178354Ssam		}
829178354Ssam#endif
830260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
831178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
832178354Ssam				/*
833178354Ssam				 * Only shared key auth frames with a challenge
834178354Ssam				 * should be encrypted, discard all others.
835178354Ssam				 */
836178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
837178354Ssam				    wh, NULL,
838178354Ssam				    "%s", "WEP set but not permitted");
839178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
840178354Ssam				goto out;
841178354Ssam			}
842178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
843178354Ssam				/*
844178354Ssam				 * Discard encrypted frames when privacy is off.
845178354Ssam				 */
846178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
847178354Ssam				    wh, NULL, "%s", "WEP set but PRIVACY off");
848178354Ssam				vap->iv_stats.is_rx_noprivacy++;
849178354Ssam				goto out;
850178354Ssam			}
851178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
852178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
853178354Ssam			if (key == NULL) {
854178354Ssam				/* NB: stats+msgs handled in crypto_decap */
855178354Ssam				goto out;
856178354Ssam			}
857178354Ssam			wh = mtod(m, struct ieee80211_frame *);
858260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
859178354Ssam		}
860205791Srpaulo		/*
861205791Srpaulo		 * Pass the packet to radiotap before calling iv_recv_mgmt().
862205791Srpaulo		 * Otherwise iv_recv_mgmt() might pass another packet to
863205791Srpaulo		 * radiotap, resulting in out of order packet captures.
864205791Srpaulo		 */
865205516Srpaulo		if (ieee80211_radiotap_active_vap(vap))
866205516Srpaulo			ieee80211_radiotap_rx(vap, m);
867205516Srpaulo		need_tap = 0;
868283535Sadrian		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
869191534Ssam		goto out;
870178354Ssam
871178354Ssam	case IEEE80211_FC0_TYPE_CTL:
872178354Ssam		vap->iv_stats.is_rx_ctl++;
873178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
874191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
875178354Ssam		goto out;
876178354Ssam	default:
877178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
878178354Ssam		    wh, "bad", "frame type 0x%x", type);
879178354Ssam		/* should not come here */
880178354Ssam		break;
881178354Ssam	}
882178354Ssamerr:
883271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
884178354Ssamout:
885178354Ssam	if (m != NULL) {
886192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
887192468Ssam			ieee80211_radiotap_rx(vap, m);
888178354Ssam		m_freem(m);
889178354Ssam	}
890178354Ssam	return type;
891178354Ssam}
892178354Ssam
893178354Ssamstatic void
894178354Ssamhostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
895192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
896178354Ssam{
897178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
898178354Ssam
899178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
900178354Ssam
901178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
902178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
903178354Ssam		    ni->ni_macaddr, "open auth",
904178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
905178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
906178354Ssam		/*
907178354Ssam		 * Clear any challenge text that may be there if
908178354Ssam		 * a previous shared key auth failed and then an
909178354Ssam		 * open auth is attempted.
910178354Ssam		 */
911178354Ssam		if (ni->ni_challenge != NULL) {
912283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
913178354Ssam			ni->ni_challenge = NULL;
914178354Ssam		}
915178354Ssam		/* XXX hack to workaround calling convention */
916178354Ssam		ieee80211_send_error(ni, wh->i_addr2,
917178354Ssam		    IEEE80211_FC0_SUBTYPE_AUTH,
918178354Ssam		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
919178354Ssam		return;
920178354Ssam	}
921178354Ssam	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
922178354Ssam		vap->iv_stats.is_rx_bad_auth++;
923178354Ssam		return;
924178354Ssam	}
925178354Ssam	/* always accept open authentication requests */
926178354Ssam	if (ni == vap->iv_bss) {
927178354Ssam		ni = ieee80211_dup_bss(vap, wh->i_addr2);
928178354Ssam		if (ni == NULL)
929178354Ssam			return;
930178354Ssam	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
931178354Ssam		(void) ieee80211_ref_node(ni);
932178354Ssam	/*
933178354Ssam	 * Mark the node as referenced to reflect that it's
934178354Ssam	 * reference count has been bumped to insure it remains
935178354Ssam	 * after the transaction completes.
936178354Ssam	 */
937178354Ssam	ni->ni_flags |= IEEE80211_NODE_AREF;
938186099Ssam	/*
939186151Ssam	 * Mark the node as requiring a valid association id
940186099Ssam	 * before outbound traffic is permitted.
941186099Ssam	 */
942186099Ssam	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
943178354Ssam
944178354Ssam	if (vap->iv_acl != NULL &&
945178354Ssam	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
946178354Ssam		/*
947178354Ssam		 * When the ACL policy is set to RADIUS we defer the
948178354Ssam		 * authorization to a user agent.  Dispatch an event,
949178354Ssam		 * a subsequent MLME call will decide the fate of the
950178354Ssam		 * station.  If the user agent is not present then the
951178354Ssam		 * node will be reclaimed due to inactivity.
952178354Ssam		 */
953178354Ssam		IEEE80211_NOTE_MAC(vap,
954178354Ssam		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
955178354Ssam		    "%s", "station authentication defered (radius acl)");
956178354Ssam		ieee80211_notify_node_auth(ni);
957178354Ssam	} else {
958178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
959178354Ssam		IEEE80211_NOTE_MAC(vap,
960178354Ssam		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
961178354Ssam		    "%s", "station authenticated (open)");
962178354Ssam		/*
963178354Ssam		 * When 802.1x is not in use mark the port
964178354Ssam		 * authorized at this point so traffic can flow.
965178354Ssam		 */
966178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
967178354Ssam			ieee80211_node_authorize(ni);
968178354Ssam	}
969178354Ssam}
970178354Ssam
971178354Ssamstatic void
972178354Ssamhostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
973192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
974178354Ssam    uint16_t seq, uint16_t status)
975178354Ssam{
976178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
977178354Ssam	uint8_t *challenge;
978178354Ssam	int allocbs, estatus;
979178354Ssam
980178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
981178354Ssam
982178354Ssam	/*
983178354Ssam	 * NB: this can happen as we allow pre-shared key
984178354Ssam	 * authentication to be enabled w/o wep being turned
985178354Ssam	 * on so that configuration of these can be done
986178354Ssam	 * in any order.  It may be better to enforce the
987178354Ssam	 * ordering in which case this check would just be
988178354Ssam	 * for sanity/consistency.
989178354Ssam	 */
990178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
991178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
992178354Ssam		    ni->ni_macaddr, "shared key auth",
993178354Ssam		    "%s", " PRIVACY is disabled");
994178354Ssam		estatus = IEEE80211_STATUS_ALG;
995178354Ssam		goto bad;
996178354Ssam	}
997178354Ssam	/*
998178354Ssam	 * Pre-shared key authentication is evil; accept
999178354Ssam	 * it only if explicitly configured (it is supported
1000178354Ssam	 * mainly for compatibility with clients like Mac OS X).
1001178354Ssam	 */
1002178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1003178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1004178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1005178354Ssam		    ni->ni_macaddr, "shared key auth",
1006178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1007178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1008178354Ssam		estatus = IEEE80211_STATUS_ALG;
1009178354Ssam		goto bad;
1010178354Ssam	}
1011178354Ssam
1012178354Ssam	challenge = NULL;
1013178354Ssam	if (frm + 1 < efrm) {
1014178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1015178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1016178354Ssam			    ni->ni_macaddr, "shared key auth",
1017178354Ssam			    "ie %d/%d too long",
1018178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1019178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1020178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1021178354Ssam			goto bad;
1022178354Ssam		}
1023178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1024178354Ssam			challenge = frm;
1025178354Ssam		frm += frm[1] + 2;
1026178354Ssam	}
1027178354Ssam	switch (seq) {
1028178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1029178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1030178354Ssam		if (challenge == NULL) {
1031178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1032178354Ssam			    ni->ni_macaddr, "shared key auth",
1033178354Ssam			    "%s", "no challenge");
1034178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1035178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1036178354Ssam			goto bad;
1037178354Ssam		}
1038178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1039178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1040178354Ssam			    ni->ni_macaddr, "shared key auth",
1041178354Ssam			    "bad challenge len %d", challenge[1]);
1042178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1043178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1044178354Ssam			goto bad;
1045178354Ssam		}
1046178354Ssam	default:
1047178354Ssam		break;
1048178354Ssam	}
1049178354Ssam	switch (seq) {
1050178354Ssam	case IEEE80211_AUTH_SHARED_REQUEST:
1051178354Ssam		if (ni == vap->iv_bss) {
1052178354Ssam			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1053178354Ssam			if (ni == NULL) {
1054178354Ssam				/* NB: no way to return an error */
1055178354Ssam				return;
1056178354Ssam			}
1057178354Ssam			allocbs = 1;
1058178354Ssam		} else {
1059178354Ssam			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1060178354Ssam				(void) ieee80211_ref_node(ni);
1061178354Ssam			allocbs = 0;
1062178354Ssam		}
1063178354Ssam		/*
1064178354Ssam		 * Mark the node as referenced to reflect that it's
1065178354Ssam		 * reference count has been bumped to insure it remains
1066178354Ssam		 * after the transaction completes.
1067178354Ssam		 */
1068178354Ssam		ni->ni_flags |= IEEE80211_NODE_AREF;
1069186099Ssam		/*
1070300232Savos		 * Mark the node as requiring a valid association id
1071186099Ssam		 * before outbound traffic is permitted.
1072186099Ssam		 */
1073186099Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1074178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1075192468Ssam		ni->ni_noise = nf;
1076178354Ssam		if (!ieee80211_alloc_challenge(ni)) {
1077178354Ssam			/* NB: don't return error so they rexmit */
1078178354Ssam			return;
1079178354Ssam		}
1080178354Ssam		get_random_bytes(ni->ni_challenge,
1081178354Ssam			IEEE80211_CHALLENGE_LEN);
1082178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1083178354Ssam		    ni, "shared key %sauth request", allocbs ? "" : "re");
1084178354Ssam		/*
1085178354Ssam		 * When the ACL policy is set to RADIUS we defer the
1086178354Ssam		 * authorization to a user agent.  Dispatch an event,
1087178354Ssam		 * a subsequent MLME call will decide the fate of the
1088178354Ssam		 * station.  If the user agent is not present then the
1089178354Ssam		 * node will be reclaimed due to inactivity.
1090178354Ssam		 */
1091178354Ssam		if (vap->iv_acl != NULL &&
1092178354Ssam		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1093178354Ssam			IEEE80211_NOTE_MAC(vap,
1094178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1095178354Ssam			    ni->ni_macaddr,
1096178354Ssam			    "%s", "station authentication defered (radius acl)");
1097178354Ssam			ieee80211_notify_node_auth(ni);
1098178354Ssam			return;
1099178354Ssam		}
1100178354Ssam		break;
1101178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1102178354Ssam		if (ni == vap->iv_bss) {
1103178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1104178354Ssam			    ni->ni_macaddr, "shared key response",
1105178354Ssam			    "%s", "unknown station");
1106178354Ssam			/* NB: don't send a response */
1107178354Ssam			return;
1108178354Ssam		}
1109178354Ssam		if (ni->ni_challenge == NULL) {
1110178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1111178354Ssam			    ni->ni_macaddr, "shared key response",
1112178354Ssam			    "%s", "no challenge recorded");
1113178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1114178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1115178354Ssam			goto bad;
1116178354Ssam		}
1117178354Ssam		if (memcmp(ni->ni_challenge, &challenge[2],
1118178354Ssam			   challenge[1]) != 0) {
1119178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1120178354Ssam			    ni->ni_macaddr, "shared key response",
1121178354Ssam			    "%s", "challenge mismatch");
1122178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1123178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1124178354Ssam			goto bad;
1125178354Ssam		}
1126178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1127178354Ssam		    ni, "%s", "station authenticated (shared key)");
1128178354Ssam		ieee80211_node_authorize(ni);
1129178354Ssam		break;
1130178354Ssam	default:
1131178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1132178354Ssam		    ni->ni_macaddr, "shared key auth",
1133178354Ssam		    "bad seq %d", seq);
1134178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1135178354Ssam		estatus = IEEE80211_STATUS_SEQUENCE;
1136178354Ssam		goto bad;
1137178354Ssam	}
1138178354Ssam	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1139178354Ssam	return;
1140178354Ssambad:
1141178354Ssam	/*
1142178354Ssam	 * Send an error response; but only when operating as an AP.
1143178354Ssam	 */
1144178354Ssam	/* XXX hack to workaround calling convention */
1145178354Ssam	ieee80211_send_error(ni, wh->i_addr2,
1146178354Ssam	    IEEE80211_FC0_SUBTYPE_AUTH,
1147178354Ssam	    (seq + 1) | (estatus<<16));
1148178354Ssam}
1149178354Ssam
1150178354Ssam/*
1151178354Ssam * Convert a WPA cipher selector OUI to an internal
1152178354Ssam * cipher algorithm.  Where appropriate we also
1153178354Ssam * record any key length.
1154178354Ssam */
1155178354Ssamstatic int
1156178354Ssamwpa_cipher(const uint8_t *sel, uint8_t *keylen)
1157178354Ssam{
1158178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1159298359Savos	uint32_t w = le32dec(sel);
1160178354Ssam
1161178354Ssam	switch (w) {
1162178354Ssam	case WPA_SEL(WPA_CSE_NULL):
1163178354Ssam		return IEEE80211_CIPHER_NONE;
1164178354Ssam	case WPA_SEL(WPA_CSE_WEP40):
1165178354Ssam		if (keylen)
1166178354Ssam			*keylen = 40 / NBBY;
1167178354Ssam		return IEEE80211_CIPHER_WEP;
1168178354Ssam	case WPA_SEL(WPA_CSE_WEP104):
1169178354Ssam		if (keylen)
1170178354Ssam			*keylen = 104 / NBBY;
1171178354Ssam		return IEEE80211_CIPHER_WEP;
1172178354Ssam	case WPA_SEL(WPA_CSE_TKIP):
1173178354Ssam		return IEEE80211_CIPHER_TKIP;
1174178354Ssam	case WPA_SEL(WPA_CSE_CCMP):
1175178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1176178354Ssam	}
1177178354Ssam	return 32;		/* NB: so 1<< is discarded */
1178178354Ssam#undef WPA_SEL
1179178354Ssam}
1180178354Ssam
1181178354Ssam/*
1182178354Ssam * Convert a WPA key management/authentication algorithm
1183178354Ssam * to an internal code.
1184178354Ssam */
1185178354Ssamstatic int
1186178354Ssamwpa_keymgmt(const uint8_t *sel)
1187178354Ssam{
1188178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1189298359Savos	uint32_t w = le32dec(sel);
1190178354Ssam
1191178354Ssam	switch (w) {
1192178354Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1193178354Ssam		return WPA_ASE_8021X_UNSPEC;
1194178354Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
1195178354Ssam		return WPA_ASE_8021X_PSK;
1196178354Ssam	case WPA_SEL(WPA_ASE_NONE):
1197178354Ssam		return WPA_ASE_NONE;
1198178354Ssam	}
1199178354Ssam	return 0;		/* NB: so is discarded */
1200178354Ssam#undef WPA_SEL
1201178354Ssam}
1202178354Ssam
1203178354Ssam/*
1204178354Ssam * Parse a WPA information element to collect parameters.
1205178354Ssam * Note that we do not validate security parameters; that
1206178354Ssam * is handled by the authenticator; the parsing done here
1207178354Ssam * is just for internal use in making operational decisions.
1208178354Ssam */
1209178354Ssamstatic int
1210178354Ssamieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1211178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1212178354Ssam{
1213178354Ssam	uint8_t len = frm[1];
1214178354Ssam	uint32_t w;
1215178354Ssam	int n;
1216178354Ssam
1217178354Ssam	/*
1218178354Ssam	 * Check the length once for fixed parts: OUI, type,
1219178354Ssam	 * version, mcast cipher, and 2 selector counts.
1220178354Ssam	 * Other, variable-length data, must be checked separately.
1221178354Ssam	 */
1222178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1223178354Ssam		IEEE80211_DISCARD_IE(vap,
1224178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1225178354Ssam		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1226178354Ssam		return IEEE80211_REASON_IE_INVALID;
1227178354Ssam	}
1228178354Ssam	if (len < 14) {
1229178354Ssam		IEEE80211_DISCARD_IE(vap,
1230178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1231178354Ssam		    wh, "WPA", "too short, len %u", len);
1232178354Ssam		return IEEE80211_REASON_IE_INVALID;
1233178354Ssam	}
1234178354Ssam	frm += 6, len -= 4;		/* NB: len is payload only */
1235200242Srpaulo	/* NB: iswpaoui already validated the OUI and type */
1236298359Savos	w = le16dec(frm);
1237178354Ssam	if (w != WPA_VERSION) {
1238178354Ssam		IEEE80211_DISCARD_IE(vap,
1239178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1240178354Ssam		    wh, "WPA", "bad version %u", w);
1241178354Ssam		return IEEE80211_REASON_IE_INVALID;
1242178354Ssam	}
1243178354Ssam	frm += 2, len -= 2;
1244178354Ssam
1245178354Ssam	memset(rsn, 0, sizeof(*rsn));
1246178354Ssam
1247178354Ssam	/* multicast/group cipher */
1248178354Ssam	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1249178354Ssam	frm += 4, len -= 4;
1250178354Ssam
1251178354Ssam	/* unicast ciphers */
1252298359Savos	n = le16dec(frm);
1253178354Ssam	frm += 2, len -= 2;
1254178354Ssam	if (len < n*4+2) {
1255178354Ssam		IEEE80211_DISCARD_IE(vap,
1256178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1257178354Ssam		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1258178354Ssam		    len, n);
1259178354Ssam		return IEEE80211_REASON_IE_INVALID;
1260178354Ssam	}
1261178354Ssam	w = 0;
1262178354Ssam	for (; n > 0; n--) {
1263178354Ssam		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1264178354Ssam		frm += 4, len -= 4;
1265178354Ssam	}
1266178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1267178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1268178354Ssam	else
1269178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1270178354Ssam
1271178354Ssam	/* key management algorithms */
1272298359Savos	n = le16dec(frm);
1273178354Ssam	frm += 2, len -= 2;
1274178354Ssam	if (len < n*4) {
1275178354Ssam		IEEE80211_DISCARD_IE(vap,
1276178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1277178354Ssam		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1278178354Ssam		    len, n);
1279178354Ssam		return IEEE80211_REASON_IE_INVALID;
1280178354Ssam	}
1281178354Ssam	w = 0;
1282178354Ssam	for (; n > 0; n--) {
1283178354Ssam		w |= wpa_keymgmt(frm);
1284178354Ssam		frm += 4, len -= 4;
1285178354Ssam	}
1286178354Ssam	if (w & WPA_ASE_8021X_UNSPEC)
1287178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1288178354Ssam	else
1289178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1290178354Ssam
1291178354Ssam	if (len > 2)		/* optional capabilities */
1292298359Savos		rsn->rsn_caps = le16dec(frm);
1293178354Ssam
1294178354Ssam	return 0;
1295178354Ssam}
1296178354Ssam
1297178354Ssam/*
1298178354Ssam * Convert an RSN cipher selector OUI to an internal
1299178354Ssam * cipher algorithm.  Where appropriate we also
1300178354Ssam * record any key length.
1301178354Ssam */
1302178354Ssamstatic int
1303178354Ssamrsn_cipher(const uint8_t *sel, uint8_t *keylen)
1304178354Ssam{
1305178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1306298359Savos	uint32_t w = le32dec(sel);
1307178354Ssam
1308178354Ssam	switch (w) {
1309178354Ssam	case RSN_SEL(RSN_CSE_NULL):
1310178354Ssam		return IEEE80211_CIPHER_NONE;
1311178354Ssam	case RSN_SEL(RSN_CSE_WEP40):
1312178354Ssam		if (keylen)
1313178354Ssam			*keylen = 40 / NBBY;
1314178354Ssam		return IEEE80211_CIPHER_WEP;
1315178354Ssam	case RSN_SEL(RSN_CSE_WEP104):
1316178354Ssam		if (keylen)
1317178354Ssam			*keylen = 104 / NBBY;
1318178354Ssam		return IEEE80211_CIPHER_WEP;
1319178354Ssam	case RSN_SEL(RSN_CSE_TKIP):
1320178354Ssam		return IEEE80211_CIPHER_TKIP;
1321178354Ssam	case RSN_SEL(RSN_CSE_CCMP):
1322178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1323178354Ssam	case RSN_SEL(RSN_CSE_WRAP):
1324178354Ssam		return IEEE80211_CIPHER_AES_OCB;
1325178354Ssam	}
1326178354Ssam	return 32;		/* NB: so 1<< is discarded */
1327178354Ssam#undef WPA_SEL
1328178354Ssam}
1329178354Ssam
1330178354Ssam/*
1331178354Ssam * Convert an RSN key management/authentication algorithm
1332178354Ssam * to an internal code.
1333178354Ssam */
1334178354Ssamstatic int
1335178354Ssamrsn_keymgmt(const uint8_t *sel)
1336178354Ssam{
1337178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1338298359Savos	uint32_t w = le32dec(sel);
1339178354Ssam
1340178354Ssam	switch (w) {
1341178354Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1342178354Ssam		return RSN_ASE_8021X_UNSPEC;
1343178354Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
1344178354Ssam		return RSN_ASE_8021X_PSK;
1345178354Ssam	case RSN_SEL(RSN_ASE_NONE):
1346178354Ssam		return RSN_ASE_NONE;
1347178354Ssam	}
1348178354Ssam	return 0;		/* NB: so is discarded */
1349178354Ssam#undef RSN_SEL
1350178354Ssam}
1351178354Ssam
1352178354Ssam/*
1353178354Ssam * Parse a WPA/RSN information element to collect parameters
1354178354Ssam * and validate the parameters against what has been
1355178354Ssam * configured for the system.
1356178354Ssam */
1357178354Ssamstatic int
1358178354Ssamieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1359178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1360178354Ssam{
1361178354Ssam	uint8_t len = frm[1];
1362178354Ssam	uint32_t w;
1363178354Ssam	int n;
1364178354Ssam
1365178354Ssam	/*
1366178354Ssam	 * Check the length once for fixed parts:
1367178354Ssam	 * version, mcast cipher, and 2 selector counts.
1368178354Ssam	 * Other, variable-length data, must be checked separately.
1369178354Ssam	 */
1370178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1371178354Ssam		IEEE80211_DISCARD_IE(vap,
1372178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1373178354Ssam		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1374178354Ssam		return IEEE80211_REASON_IE_INVALID;
1375178354Ssam	}
1376178354Ssam	if (len < 10) {
1377178354Ssam		IEEE80211_DISCARD_IE(vap,
1378178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1379178354Ssam		    wh, "RSN", "too short, len %u", len);
1380178354Ssam		return IEEE80211_REASON_IE_INVALID;
1381178354Ssam	}
1382178354Ssam	frm += 2;
1383298359Savos	w = le16dec(frm);
1384178354Ssam	if (w != RSN_VERSION) {
1385178354Ssam		IEEE80211_DISCARD_IE(vap,
1386178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1387178354Ssam		    wh, "RSN", "bad version %u", w);
1388178354Ssam		return IEEE80211_REASON_IE_INVALID;
1389178354Ssam	}
1390178354Ssam	frm += 2, len -= 2;
1391178354Ssam
1392178354Ssam	memset(rsn, 0, sizeof(*rsn));
1393178354Ssam
1394178354Ssam	/* multicast/group cipher */
1395178354Ssam	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1396178354Ssam	frm += 4, len -= 4;
1397178354Ssam
1398178354Ssam	/* unicast ciphers */
1399298359Savos	n = le16dec(frm);
1400178354Ssam	frm += 2, len -= 2;
1401178354Ssam	if (len < n*4+2) {
1402178354Ssam		IEEE80211_DISCARD_IE(vap,
1403178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1404178354Ssam		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1405178354Ssam		    len, n);
1406178354Ssam		return IEEE80211_REASON_IE_INVALID;
1407178354Ssam	}
1408178354Ssam	w = 0;
1409178354Ssam	for (; n > 0; n--) {
1410178354Ssam		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1411178354Ssam		frm += 4, len -= 4;
1412178354Ssam	}
1413178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1414178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1415178354Ssam	else
1416178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1417178354Ssam
1418178354Ssam	/* key management algorithms */
1419298359Savos	n = le16dec(frm);
1420178354Ssam	frm += 2, len -= 2;
1421178354Ssam	if (len < n*4) {
1422178354Ssam		IEEE80211_DISCARD_IE(vap,
1423178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1424178354Ssam		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1425178354Ssam		    len, n);
1426178354Ssam		return IEEE80211_REASON_IE_INVALID;
1427178354Ssam	}
1428178354Ssam	w = 0;
1429178354Ssam	for (; n > 0; n--) {
1430178354Ssam		w |= rsn_keymgmt(frm);
1431178354Ssam		frm += 4, len -= 4;
1432178354Ssam	}
1433178354Ssam	if (w & RSN_ASE_8021X_UNSPEC)
1434178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1435178354Ssam	else
1436178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1437178354Ssam
1438178354Ssam	/* optional RSN capabilities */
1439178354Ssam	if (len > 2)
1440298359Savos		rsn->rsn_caps = le16dec(frm);
1441178354Ssam	/* XXXPMKID */
1442178354Ssam
1443178354Ssam	return 0;
1444178354Ssam}
1445178354Ssam
1446178354Ssam/*
1447298995Spfg * WPA/802.11i association request processing.
1448178354Ssam */
1449178354Ssamstatic int
1450178354Ssamwpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1451178354Ssam	const struct ieee80211_frame *wh, const uint8_t *wpa,
1452178354Ssam	const uint8_t *rsn, uint16_t capinfo)
1453178354Ssam{
1454178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1455178354Ssam	uint8_t reason;
1456178354Ssam	int badwparsn;
1457178354Ssam
1458178354Ssam	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1459178354Ssam	if (wpa == NULL && rsn == NULL) {
1460178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1461178354Ssam			/*
1462178354Ssam			 * W-Fi Protected Setup (WPS) permits
1463178354Ssam			 * clients to associate and pass EAPOL frames
1464178354Ssam			 * to establish initial credentials.
1465178354Ssam			 */
1466178354Ssam			ni->ni_flags |= IEEE80211_NODE_WPS;
1467178354Ssam			return 1;
1468178354Ssam		}
1469178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1470178354Ssam		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1471178354Ssam			/*
1472178354Ssam			 * Transitional Security Network.  Permits clients
1473178354Ssam			 * to associate and use WEP while WPA is configured.
1474178354Ssam			 */
1475178354Ssam			ni->ni_flags |= IEEE80211_NODE_TSN;
1476178354Ssam			return 1;
1477178354Ssam		}
1478178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1479178354Ssam		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1480178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1481178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1482178354Ssam		goto bad;
1483178354Ssam	}
1484178354Ssam	/* assert right association security credentials */
1485178354Ssam	badwparsn = 0;			/* NB: to silence compiler */
1486178354Ssam	switch (vap->iv_flags & IEEE80211_F_WPA) {
1487178354Ssam	case IEEE80211_F_WPA1:
1488178354Ssam		badwparsn = (wpa == NULL);
1489178354Ssam		break;
1490178354Ssam	case IEEE80211_F_WPA2:
1491178354Ssam		badwparsn = (rsn == NULL);
1492178354Ssam		break;
1493178354Ssam	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1494178354Ssam		badwparsn = (wpa == NULL && rsn == NULL);
1495178354Ssam		break;
1496178354Ssam	}
1497178354Ssam	if (badwparsn) {
1498178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1499178354Ssam		    wh, NULL,
1500178354Ssam		    "%s", "missing WPA/RSN IE in association request");
1501178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1502178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1503178354Ssam		goto bad;
1504178354Ssam	}
1505178354Ssam	/*
1506178354Ssam	 * Parse WPA/RSN information element.
1507178354Ssam	 */
1508178354Ssam	if (wpa != NULL)
1509178354Ssam		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1510178354Ssam	else
1511178354Ssam		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1512178354Ssam	if (reason != 0) {
1513178354Ssam		/* XXX distinguish WPA/RSN? */
1514178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1515178354Ssam		goto bad;
1516178354Ssam	}
1517178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1518178354Ssam	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1519178354Ssam	    wpa != NULL ? "WPA" : "RSN",
1520178354Ssam	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1521178354Ssam	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1522178354Ssam	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1523178354Ssam
1524178354Ssam	return 1;
1525178354Ssambad:
1526178354Ssam	ieee80211_node_deauth(ni, reason);
1527178354Ssam	return 0;
1528178354Ssam}
1529178354Ssam
1530178354Ssam/* XXX find a better place for definition */
1531178354Ssamstruct l2_update_frame {
1532178354Ssam	struct ether_header eh;
1533178354Ssam	uint8_t dsap;
1534178354Ssam	uint8_t ssap;
1535178354Ssam	uint8_t control;
1536178354Ssam	uint8_t xid[3];
1537178354Ssam}  __packed;
1538178354Ssam
1539178354Ssam/*
1540178354Ssam * Deliver a TGf L2UF frame on behalf of a station.
1541178354Ssam * This primes any bridge when the station is roaming
1542178354Ssam * between ap's on the same wired network.
1543178354Ssam */
1544178354Ssamstatic void
1545178354Ssamieee80211_deliver_l2uf(struct ieee80211_node *ni)
1546178354Ssam{
1547178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1548178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1549178354Ssam	struct mbuf *m;
1550178354Ssam	struct l2_update_frame *l2uf;
1551178354Ssam	struct ether_header *eh;
1552178354Ssam
1553178354Ssam	m = m_gethdr(M_NOWAIT, MT_DATA);
1554178354Ssam	if (m == NULL) {
1555178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1556178354Ssam		    "%s", "no mbuf for l2uf frame");
1557178354Ssam		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1558178354Ssam		return;
1559178354Ssam	}
1560178354Ssam	l2uf = mtod(m, struct l2_update_frame *);
1561178354Ssam	eh = &l2uf->eh;
1562178354Ssam	/* dst: Broadcast address */
1563178354Ssam	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1564178354Ssam	/* src: associated STA */
1565178354Ssam	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1566178354Ssam	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1567178354Ssam
1568178354Ssam	l2uf->dsap = 0;
1569178354Ssam	l2uf->ssap = 0;
1570178354Ssam	l2uf->control = 0xf5;
1571178354Ssam	l2uf->xid[0] = 0x81;
1572178354Ssam	l2uf->xid[1] = 0x80;
1573178354Ssam	l2uf->xid[2] = 0x00;
1574178354Ssam
1575178354Ssam	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1576178354Ssam	hostap_deliver_data(vap, ni, m);
1577178354Ssam}
1578178354Ssam
1579178354Ssamstatic void
1580178354Ssamratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1581178354Ssam	int reassoc, int resp, const char *tag, int rate)
1582178354Ssam{
1583178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1584178354Ssam	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1585178354Ssam	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1586178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1587178354Ssam	ieee80211_node_leave(ni);
1588178354Ssam}
1589178354Ssam
1590178354Ssamstatic void
1591178354Ssamcapinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1592178354Ssam	int reassoc, int resp, const char *tag, int capinfo)
1593178354Ssam{
1594178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1595178354Ssam
1596178354Ssam	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1597178354Ssam	    "deny %s request, %s mismatch 0x%x",
1598178354Ssam	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1599178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1600178354Ssam	ieee80211_node_leave(ni);
1601178354Ssam	vap->iv_stats.is_rx_assoc_capmismatch++;
1602178354Ssam}
1603178354Ssam
1604178354Ssamstatic void
1605178354Ssamhtcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1606178354Ssam	int reassoc, int resp)
1607178354Ssam{
1608178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1609178354Ssam	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1610178354Ssam	/* XXX no better code */
1611193543Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1612178354Ssam	ieee80211_node_leave(ni);
1613178354Ssam}
1614178354Ssam
1615178354Ssamstatic void
1616178354Ssamauthalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1617178354Ssam	int algo, int seq, int status)
1618178354Ssam{
1619178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1620178354Ssam
1621178354Ssam	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1622178354Ssam	    wh, NULL, "unsupported alg %d", algo);
1623178354Ssam	vap->iv_stats.is_rx_auth_unsupported++;
1624178354Ssam	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1625178354Ssam	    seq | (status << 16));
1626178354Ssam}
1627178354Ssam
1628178354Ssamstatic __inline int
1629178354Ssamishtmixed(const uint8_t *ie)
1630178354Ssam{
1631178354Ssam	const struct ieee80211_ie_htinfo *ht =
1632178354Ssam	    (const struct ieee80211_ie_htinfo *) ie;
1633178354Ssam	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1634178354Ssam	    IEEE80211_HTINFO_OPMODE_MIXED;
1635178354Ssam}
1636178354Ssam
1637178354Ssamstatic int
1638178354Ssamis11bclient(const uint8_t *rates, const uint8_t *xrates)
1639178354Ssam{
1640178354Ssam	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1641178354Ssam	int i;
1642178354Ssam
1643178354Ssam	/* NB: the 11b clients we care about will not have xrates */
1644178354Ssam	if (xrates != NULL || rates == NULL)
1645178354Ssam		return 0;
1646178354Ssam	for (i = 0; i < rates[1]; i++) {
1647178354Ssam		int r = rates[2+i] & IEEE80211_RATE_VAL;
1648178354Ssam		if (r > 2*11 || ((1<<r) & brates) == 0)
1649178354Ssam			return 0;
1650178354Ssam	}
1651178354Ssam	return 1;
1652178354Ssam}
1653178354Ssam
1654178354Ssamstatic void
1655178354Ssamhostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1656283535Sadrian	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1657178354Ssam{
1658178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1659178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1660178354Ssam	struct ieee80211_frame *wh;
1661178354Ssam	uint8_t *frm, *efrm, *sfrm;
1662178354Ssam	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1663178354Ssam	int reassoc, resp;
1664178354Ssam	uint8_t rate;
1665178354Ssam
1666178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1667178354Ssam	frm = (uint8_t *)&wh[1];
1668178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1669178354Ssam	switch (subtype) {
1670178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1671178354Ssam		/*
1672178354Ssam		 * We process beacon/probe response frames when scanning;
1673178354Ssam		 * otherwise we check beacon frames for overlapping non-ERP
1674178354Ssam		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1675298360Savos		 */
1676298360Savos		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1677178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1678178354Ssam			return;
1679178354Ssam		}
1680298360Savos		/* FALLTHROUGH */
1681298360Savos	case IEEE80211_FC0_SUBTYPE_BEACON: {
1682298360Savos		struct ieee80211_scanparams scan;
1683298360Savos
1684178354Ssam		/* NB: accept off-channel frames */
1685283535Sadrian		/* XXX TODO: use rxstatus to determine off-channel details */
1686283535Sadrian		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1687178354Ssam			return;
1688178354Ssam		/*
1689178354Ssam		 * Count frame now that we know it's to be processed.
1690178354Ssam		 */
1691178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1692178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1693178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1694178354Ssam		} else
1695178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1696178354Ssam		/*
1697178354Ssam		 * If scanning, just pass information to the scan module.
1698178354Ssam		 */
1699178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1700178354Ssam			if (scan.status == 0 &&		/* NB: on channel */
1701178354Ssam			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1702178354Ssam				/*
1703178354Ssam				 * Actively scanning a channel marked passive;
1704178354Ssam				 * send a probe request now that we know there
1705178354Ssam				 * is 802.11 traffic present.
1706178354Ssam				 *
1707178354Ssam				 * XXX check if the beacon we recv'd gives
1708178354Ssam				 * us what we need and suppress the probe req
1709178354Ssam				 */
1710178354Ssam				ieee80211_probe_curchan(vap, 1);
1711178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1712178354Ssam			}
1713282742Sadrian			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1714282742Sadrian			    subtype, rssi, nf);
1715178354Ssam			return;
1716178354Ssam		}
1717178354Ssam		/*
1718178354Ssam		 * Check beacon for overlapping bss w/ non ERP stations.
1719178354Ssam		 * If we detect one and protection is configured but not
1720178354Ssam		 * enabled, enable it and start a timer that'll bring us
1721178354Ssam		 * out if we stop seeing the bss.
1722178354Ssam		 */
1723178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1724178354Ssam		    scan.status == 0 &&			/* NB: on-channel */
1725178354Ssam		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1726178354Ssam		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1727178354Ssam			ic->ic_lastnonerp = ticks;
1728178354Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1729178354Ssam			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1730178354Ssam			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1731178354Ssam				IEEE80211_NOTE_FRAME(vap,
1732178354Ssam				    IEEE80211_MSG_ASSOC, wh,
1733178354Ssam				    "non-ERP present on channel %d "
1734178354Ssam				    "(saw erp 0x%x from channel %d), "
1735178354Ssam				    "enable use of protection",
1736178354Ssam				    ic->ic_curchan->ic_ieee,
1737178354Ssam				    scan.erp, scan.chan);
1738178354Ssam				ic->ic_flags |= IEEE80211_F_USEPROT;
1739178354Ssam				ieee80211_notify_erp(ic);
1740178354Ssam			}
1741178354Ssam		}
1742178354Ssam		/*
1743178354Ssam		 * Check beacon for non-HT station on HT channel
1744178354Ssam		 * and update HT BSS occupancy as appropriate.
1745178354Ssam		 */
1746178354Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1747178354Ssam			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1748178354Ssam				/*
1749178354Ssam				 * Off control channel; only check frames
1750178354Ssam				 * that come in the extension channel when
1751178354Ssam				 * operating w/ HT40.
1752178354Ssam				 */
1753178354Ssam				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1754178354Ssam					break;
1755178354Ssam				if (scan.chan != ic->ic_curchan->ic_extieee)
1756178354Ssam					break;
1757178354Ssam			}
1758178354Ssam			if (scan.htinfo == NULL) {
1759178354Ssam				ieee80211_htprot_update(ic,
1760178354Ssam				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1761178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1762178354Ssam			} else if (ishtmixed(scan.htinfo)) {
1763178354Ssam				/* XXX? take NONHT_PRESENT from beacon? */
1764178354Ssam				ieee80211_htprot_update(ic,
1765178354Ssam				    IEEE80211_HTINFO_OPMODE_MIXED |
1766178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1767178354Ssam			}
1768178354Ssam		}
1769178354Ssam		break;
1770178354Ssam	}
1771178354Ssam
1772178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1773178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1774178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1775178354Ssam			return;
1776178354Ssam		}
1777178354Ssam		/*
1778228514Sadrian		 * Consult the ACL policy module if setup.
1779228514Sadrian		 */
1780228622Sbschmidt		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1781228514Sadrian			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1782228514Sadrian			    wh, NULL, "%s", "disallowed by ACL");
1783228514Sadrian			vap->iv_stats.is_rx_acl++;
1784228514Sadrian			return;
1785228514Sadrian		}
1786228514Sadrian		/*
1787178354Ssam		 * prreq frame format
1788178354Ssam		 *	[tlv] ssid
1789178354Ssam		 *	[tlv] supported rates
1790178354Ssam		 *	[tlv] extended supported rates
1791178354Ssam		 */
1792178354Ssam		ssid = rates = xrates = NULL;
1793178354Ssam		while (efrm - frm > 1) {
1794178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1795178354Ssam			switch (*frm) {
1796178354Ssam			case IEEE80211_ELEMID_SSID:
1797178354Ssam				ssid = frm;
1798178354Ssam				break;
1799178354Ssam			case IEEE80211_ELEMID_RATES:
1800178354Ssam				rates = frm;
1801178354Ssam				break;
1802178354Ssam			case IEEE80211_ELEMID_XRATES:
1803178354Ssam				xrates = frm;
1804178354Ssam				break;
1805178354Ssam			}
1806178354Ssam			frm += frm[1] + 2;
1807178354Ssam		}
1808178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1809178354Ssam		if (xrates != NULL)
1810178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1811178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1812178354Ssam		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1813178354Ssam		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1814178354Ssam		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1815178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1816178354Ssam			    wh, NULL,
1817178354Ssam			    "%s", "no ssid with ssid suppression enabled");
1818178354Ssam			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1819178354Ssam			return;
1820178354Ssam		}
1821178354Ssam
1822178354Ssam		/* XXX find a better class or define it's own */
1823178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1824178354Ssam		    "%s", "recv probe req");
1825178354Ssam		/*
1826178354Ssam		 * Some legacy 11b clients cannot hack a complete
1827178354Ssam		 * probe response frame.  When the request includes
1828178354Ssam		 * only a bare-bones rate set, communicate this to
1829178354Ssam		 * the transmit side.
1830178354Ssam		 */
1831178354Ssam		ieee80211_send_proberesp(vap, wh->i_addr2,
1832178354Ssam		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1833178354Ssam		break;
1834178354Ssam
1835178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH: {
1836178354Ssam		uint16_t algo, seq, status;
1837178354Ssam
1838178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1839178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1840178354Ssam			return;
1841178354Ssam		}
1842178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1843178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1844178354Ssam			    wh, NULL, "%s", "wrong bssid");
1845178354Ssam			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1846178354Ssam			return;
1847178354Ssam		}
1848178354Ssam		/*
1849178354Ssam		 * auth frame format
1850178354Ssam		 *	[2] algorithm
1851178354Ssam		 *	[2] sequence
1852178354Ssam		 *	[2] status
1853178354Ssam		 *	[tlv*] challenge
1854178354Ssam		 */
1855178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1856178354Ssam		algo   = le16toh(*(uint16_t *)frm);
1857178354Ssam		seq    = le16toh(*(uint16_t *)(frm + 2));
1858178354Ssam		status = le16toh(*(uint16_t *)(frm + 4));
1859178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1860178354Ssam		    "recv auth frame with algorithm %d seq %d", algo, seq);
1861178354Ssam		/*
1862178354Ssam		 * Consult the ACL policy module if setup.
1863178354Ssam		 */
1864228622Sbschmidt		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1865178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1866178354Ssam			    wh, NULL, "%s", "disallowed by ACL");
1867178354Ssam			vap->iv_stats.is_rx_acl++;
1868178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1869178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH,
1870178354Ssam			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1871178354Ssam			return;
1872178354Ssam		}
1873178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1874178354Ssam			IEEE80211_DISCARD(vap,
1875178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1876178354Ssam			    wh, NULL, "%s", "TKIP countermeasures enabled");
1877178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1878178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1879178354Ssam				IEEE80211_FC0_SUBTYPE_AUTH,
1880178354Ssam				IEEE80211_REASON_MIC_FAILURE);
1881178354Ssam			return;
1882178354Ssam		}
1883178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1884192468Ssam			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1885192468Ssam			    seq, status);
1886178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1887192468Ssam			hostap_auth_open(ni, wh, rssi, nf, seq, status);
1888178354Ssam		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1889178354Ssam			authalgreject(ni, wh, algo,
1890178354Ssam			    seq+1, IEEE80211_STATUS_ALG);
1891178354Ssam			return;
1892178354Ssam		} else {
1893178354Ssam			/*
1894178354Ssam			 * We assume that an unknown algorithm is the result
1895178354Ssam			 * of a decryption failure on a shared key auth frame;
1896178354Ssam			 * return a status code appropriate for that instead
1897178354Ssam			 * of IEEE80211_STATUS_ALG.
1898178354Ssam			 *
1899178354Ssam			 * NB: a seq# of 4 is intentional; the decrypted
1900178354Ssam			 *     frame likely has a bogus seq value.
1901178354Ssam			 */
1902178354Ssam			authalgreject(ni, wh, algo,
1903178354Ssam			    4, IEEE80211_STATUS_CHALLENGE);
1904178354Ssam			return;
1905178354Ssam		}
1906178354Ssam		break;
1907178354Ssam	}
1908178354Ssam
1909178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1910178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1911178354Ssam		uint16_t capinfo, lintval;
1912178354Ssam		struct ieee80211_rsnparms rsnparms;
1913178354Ssam
1914178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1915178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1916178354Ssam			return;
1917178354Ssam		}
1918178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1919178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1920178354Ssam			    wh, NULL, "%s", "wrong bssid");
1921178354Ssam			vap->iv_stats.is_rx_assoc_bss++;
1922178354Ssam			return;
1923178354Ssam		}
1924178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1925178354Ssam			reassoc = 1;
1926178354Ssam			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1927178354Ssam		} else {
1928178354Ssam			reassoc = 0;
1929178354Ssam			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1930178354Ssam		}
1931178354Ssam		if (ni == vap->iv_bss) {
1932178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1933178354Ssam			    "deny %s request, sta not authenticated",
1934178354Ssam			    reassoc ? "reassoc" : "assoc");
1935178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1936178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1937178354Ssam			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1938178354Ssam			vap->iv_stats.is_rx_assoc_notauth++;
1939178354Ssam			return;
1940178354Ssam		}
1941178354Ssam
1942178354Ssam		/*
1943178354Ssam		 * asreq frame format
1944178354Ssam		 *	[2] capability information
1945178354Ssam		 *	[2] listen interval
1946178354Ssam		 *	[6*] current AP address (reassoc only)
1947178354Ssam		 *	[tlv] ssid
1948178354Ssam		 *	[tlv] supported rates
1949178354Ssam		 *	[tlv] extended supported rates
1950178354Ssam		 *	[tlv] WPA or RSN
1951178354Ssam		 *	[tlv] HT capabilities
1952178354Ssam		 *	[tlv] Atheros capabilities
1953178354Ssam		 */
1954178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1955178354Ssam		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1956178354Ssam		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1957178354Ssam		if (reassoc)
1958178354Ssam			frm += 6;	/* ignore current AP info */
1959178354Ssam		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1960178354Ssam		sfrm = frm;
1961178354Ssam		while (efrm - frm > 1) {
1962178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1963178354Ssam			switch (*frm) {
1964178354Ssam			case IEEE80211_ELEMID_SSID:
1965178354Ssam				ssid = frm;
1966178354Ssam				break;
1967178354Ssam			case IEEE80211_ELEMID_RATES:
1968178354Ssam				rates = frm;
1969178354Ssam				break;
1970178354Ssam			case IEEE80211_ELEMID_XRATES:
1971178354Ssam				xrates = frm;
1972178354Ssam				break;
1973178354Ssam			case IEEE80211_ELEMID_RSN:
1974178354Ssam				rsn = frm;
1975178354Ssam				break;
1976178354Ssam			case IEEE80211_ELEMID_HTCAP:
1977178354Ssam				htcap = frm;
1978178354Ssam				break;
1979178354Ssam			case IEEE80211_ELEMID_VENDOR:
1980178354Ssam				if (iswpaoui(frm))
1981178354Ssam					wpa = frm;
1982178354Ssam				else if (iswmeinfo(frm))
1983178354Ssam					wme = frm;
1984190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1985178354Ssam				else if (isatherosoui(frm))
1986178354Ssam					ath = frm;
1987190391Ssam#endif
1988193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1989178354Ssam					if (ishtcapoui(frm) && htcap == NULL)
1990178354Ssam						htcap = frm;
1991178354Ssam				}
1992178354Ssam				break;
1993178354Ssam			}
1994178354Ssam			frm += frm[1] + 2;
1995178354Ssam		}
1996178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1997178354Ssam		if (xrates != NULL)
1998178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1999178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
2000178354Ssam		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2001178354Ssam		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2002178354Ssam		if (htcap != NULL) {
2003178354Ssam			IEEE80211_VERIFY_LENGTH(htcap[1],
2004178354Ssam			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2005178354Ssam			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2006178354Ssam			         sizeof(struct ieee80211_ie_htcap)-2,
2007178354Ssam			     return);		/* XXX just NULL out? */
2008178354Ssam		}
2009178354Ssam
2010178354Ssam		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2011178354Ssam		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2012178354Ssam			return;
2013178354Ssam		/* discard challenge after association */
2014178354Ssam		if (ni->ni_challenge != NULL) {
2015283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2016178354Ssam			ni->ni_challenge = NULL;
2017178354Ssam		}
2018178354Ssam		/* NB: 802.11 spec says to ignore station's privacy bit */
2019178354Ssam		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2020178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2021178354Ssam			    "capability", capinfo);
2022178354Ssam			return;
2023178354Ssam		}
2024178354Ssam		/*
2025178354Ssam		 * Disallow re-associate w/ invalid slot time setting.
2026178354Ssam		 */
2027178354Ssam		if (ni->ni_associd != 0 &&
2028178354Ssam		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2029178354Ssam		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2030178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2031178354Ssam			    "slot time", capinfo);
2032178354Ssam			return;
2033178354Ssam		}
2034178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
2035178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2036178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2037178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
2038178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2039178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2040178354Ssam			return;
2041178354Ssam		}
2042178354Ssam		/*
2043178354Ssam		 * If constrained to 11g-only stations reject an
2044178354Ssam		 * 11b-only station.  We cheat a bit here by looking
2045178354Ssam		 * at the max negotiated xmit rate and assuming anyone
2046178354Ssam		 * with a best rate <24Mb/s is an 11b station.
2047178354Ssam		 */
2048178354Ssam		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2049178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2050178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2051178354Ssam			return;
2052178354Ssam		}
2053178354Ssam		/*
2054178354Ssam		 * Do HT rate set handling and setup HT node state.
2055178354Ssam		 */
2056178354Ssam		ni->ni_chan = vap->iv_bss->ni_chan;
2057178354Ssam		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2058178354Ssam			rate = ieee80211_setup_htrates(ni, htcap,
2059178354Ssam				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2060178354Ssam				IEEE80211_F_DOBRS);
2061178354Ssam			if (rate & IEEE80211_RATE_BASIC) {
2062178354Ssam				ratesetmismatch(ni, wh, reassoc, resp,
2063178354Ssam				    "HT", rate);
2064178354Ssam				vap->iv_stats.is_ht_assoc_norate++;
2065178354Ssam				return;
2066178354Ssam			}
2067183254Ssam			ieee80211_ht_node_init(ni);
2068183254Ssam			ieee80211_ht_updatehtcap(ni, htcap);
2069178354Ssam		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2070178354Ssam			ieee80211_ht_node_cleanup(ni);
2071190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2072297604Sadrian		/* Always do ff node cleanup; for A-MSDU */
2073297604Sadrian		ieee80211_ff_node_cleanup(ni);
2074190579Ssam#endif
2075178354Ssam		/*
2076178354Ssam		 * Allow AMPDU operation only with unencrypted traffic
2077178354Ssam		 * or AES-CCM; the 11n spec only specifies these ciphers
2078178354Ssam		 * so permitting any others is undefined and can lead
2079178354Ssam		 * to interoperability problems.
2080178354Ssam		 */
2081178354Ssam		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2082178354Ssam		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2083178354Ssam		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2084178354Ssam		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2085178354Ssam			IEEE80211_NOTE(vap,
2086178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2087178354Ssam			    "disallow HT use because WEP or TKIP requested, "
2088178354Ssam			    "capinfo 0x%x ucastcipher %d", capinfo,
2089178354Ssam			    rsnparms.rsn_ucastcipher);
2090178354Ssam			ieee80211_ht_node_cleanup(ni);
2091297604Sadrian#ifdef IEEE80211_SUPPORT_SUPERG
2092297604Sadrian			/* Always do ff node cleanup; for A-MSDU */
2093297604Sadrian			ieee80211_ff_node_cleanup(ni);
2094297604Sadrian#endif
2095178354Ssam			vap->iv_stats.is_ht_assoc_downgrade++;
2096178354Ssam		}
2097178354Ssam		/*
2098178354Ssam		 * If constrained to 11n-only stations reject legacy stations.
2099178354Ssam		 */
2100193655Ssam		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2101178354Ssam		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2102178354Ssam			htcapmismatch(ni, wh, reassoc, resp);
2103178354Ssam			vap->iv_stats.is_ht_assoc_nohtcap++;
2104178354Ssam			return;
2105178354Ssam		}
2106178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2107192468Ssam		ni->ni_noise = nf;
2108178354Ssam		ni->ni_intval = lintval;
2109178354Ssam		ni->ni_capinfo = capinfo;
2110178354Ssam		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2111178354Ssam		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2112178354Ssam		/*
2113178354Ssam		 * Store the IEs.
2114178354Ssam		 * XXX maybe better to just expand
2115178354Ssam		 */
2116178354Ssam		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2117178354Ssam#define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2118178354Ssam			if (wpa != NULL)
2119178354Ssam				setie(wpa_ie, wpa - sfrm);
2120178354Ssam			if (rsn != NULL)
2121178354Ssam				setie(rsn_ie, rsn - sfrm);
2122178354Ssam			if (htcap != NULL)
2123178354Ssam				setie(htcap_ie, htcap - sfrm);
2124178354Ssam			if (wme != NULL) {
2125178354Ssam				setie(wme_ie, wme - sfrm);
2126178354Ssam				/*
2127178354Ssam				 * Mark node as capable of QoS.
2128178354Ssam				 */
2129178354Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
2130178354Ssam			} else
2131178354Ssam				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2132190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2133178354Ssam			if (ath != NULL) {
2134178354Ssam				setie(ath_ie, ath - sfrm);
2135178354Ssam				/*
2136178354Ssam				 * Parse ATH station parameters.
2137178354Ssam				 */
2138178354Ssam				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2139178354Ssam			} else
2140190391Ssam#endif
2141178354Ssam				ni->ni_ath_flags = 0;
2142178354Ssam#undef setie
2143178354Ssam		} else {
2144178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2145178354Ssam			ni->ni_ath_flags = 0;
2146178354Ssam		}
2147178354Ssam		ieee80211_node_join(ni, resp);
2148178354Ssam		ieee80211_deliver_l2uf(ni);
2149178354Ssam		break;
2150178354Ssam	}
2151178354Ssam
2152178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2153178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2154178354Ssam		uint16_t reason;
2155178354Ssam
2156178354Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
2157178354Ssam		    /* NB: can happen when in promiscuous mode */
2158178354Ssam		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2159178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2160178354Ssam			break;
2161178354Ssam		}
2162178354Ssam		/*
2163178354Ssam		 * deauth/disassoc frame format
2164178354Ssam		 *	[2] reason
2165178354Ssam		 */
2166178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2167178354Ssam		reason = le16toh(*(uint16_t *)frm);
2168178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2169178354Ssam			vap->iv_stats.is_rx_deauth++;
2170178354Ssam			IEEE80211_NODE_STAT(ni, rx_deauth);
2171178354Ssam		} else {
2172178354Ssam			vap->iv_stats.is_rx_disassoc++;
2173178354Ssam			IEEE80211_NODE_STAT(ni, rx_disassoc);
2174178354Ssam		}
2175178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2176298364Savos		    "recv %s (reason: %d (%s))",
2177298376Savos		    ieee80211_mgt_subtype_name(subtype),
2178298364Savos		    reason, ieee80211_reason_to_string(reason));
2179178354Ssam		if (ni != vap->iv_bss)
2180178354Ssam			ieee80211_node_leave(ni);
2181178354Ssam		break;
2182178354Ssam	}
2183178354Ssam
2184178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
2185218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2186218958Sbschmidt		if (ni == vap->iv_bss) {
2187218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2188218958Sbschmidt			    wh, NULL, "%s", "unknown node");
2189218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
2190218958Sbschmidt		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2191218958Sbschmidt		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2192218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2193218958Sbschmidt			    wh, NULL, "%s", "not for us");
2194218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
2195218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
2196218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2197218927Sbschmidt			    wh, NULL, "wrong state %s",
2198218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
2199178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2200218958Sbschmidt		} else {
2201218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
2202218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2203218927Sbschmidt		}
2204178354Ssam		break;
2205178354Ssam
2206178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2207178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2208295795Savos	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2209218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
2210218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2211218927Sbschmidt		    wh, NULL, "%s", "not handled");
2212218927Sbschmidt		vap->iv_stats.is_rx_mgtdiscard++;
2213218927Sbschmidt		break;
2214218927Sbschmidt
2215178354Ssam	default:
2216178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2217218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
2218178354Ssam		vap->iv_stats.is_rx_badsubtype++;
2219178354Ssam		break;
2220178354Ssam	}
2221178354Ssam}
2222178354Ssam
2223191546Ssamstatic void
2224191546Ssamhostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2225191546Ssam{
2226191546Ssam	switch (subtype) {
2227191546Ssam	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2228241138Sadrian		ni->ni_vap->iv_recv_pspoll(ni, m);
2229191546Ssam		break;
2230191546Ssam	case IEEE80211_FC0_SUBTYPE_BAR:
2231191546Ssam		ieee80211_recv_bar(ni, m);
2232191546Ssam		break;
2233191546Ssam	}
2234191546Ssam}
2235191546Ssam
2236178354Ssam/*
2237178354Ssam * Process a received ps-poll frame.
2238178354Ssam */
2239241138Sadrianvoid
2240241138Sadrianieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2241178354Ssam{
2242178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2243248069Sadrian	struct ieee80211com *ic = vap->iv_ic;
2244178354Ssam	struct ieee80211_frame_min *wh;
2245178354Ssam	struct mbuf *m;
2246178354Ssam	uint16_t aid;
2247178354Ssam	int qlen;
2248178354Ssam
2249178354Ssam	wh = mtod(m0, struct ieee80211_frame_min *);
2250178354Ssam	if (ni->ni_associd == 0) {
2251178354Ssam		IEEE80211_DISCARD(vap,
2252178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2253178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2254178354Ssam		    "%s", "unassociated station");
2255178354Ssam		vap->iv_stats.is_ps_unassoc++;
2256178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2257178354Ssam			IEEE80211_REASON_NOT_ASSOCED);
2258178354Ssam		return;
2259178354Ssam	}
2260178354Ssam
2261178354Ssam	aid = le16toh(*(uint16_t *)wh->i_dur);
2262178354Ssam	if (aid != ni->ni_associd) {
2263178354Ssam		IEEE80211_DISCARD(vap,
2264178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2265178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2266178354Ssam		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2267178354Ssam		    ni->ni_associd, aid);
2268178354Ssam		vap->iv_stats.is_ps_badaid++;
2269180837Ssam		/*
2270180837Ssam		 * NB: We used to deauth the station but it turns out
2271180837Ssam		 * the Blackberry Curve 8230 (and perhaps other devices)
2272180837Ssam		 * sometimes send the wrong AID when WME is negotiated.
2273180837Ssam		 * Being more lenient here seems ok as we already check
2274180837Ssam		 * the station is associated and we only return frames
2275180837Ssam		 * queued for the station (i.e. we don't use the AID).
2276180837Ssam		 */
2277178354Ssam		return;
2278178354Ssam	}
2279178354Ssam
2280178354Ssam	/* Okay, take the first queued packet and put it out... */
2281184288Ssam	m = ieee80211_node_psq_dequeue(ni, &qlen);
2282178354Ssam	if (m == NULL) {
2283178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2284178354Ssam		    "%s", "recv ps-poll, but queue empty");
2285178354Ssam		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2286178354Ssam		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2287178354Ssam		if (vap->iv_set_tim != NULL)
2288178354Ssam			vap->iv_set_tim(ni, 0);	/* just in case */
2289178354Ssam		return;
2290178354Ssam	}
2291178354Ssam	/*
2292178354Ssam	 * If there are more packets, set the more packets bit
2293178354Ssam	 * in the packet dispatched to the station; otherwise
2294178354Ssam	 * turn off the TIM bit.
2295178354Ssam	 */
2296178354Ssam	if (qlen != 0) {
2297178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2298178354Ssam		    "recv ps-poll, send packet, %u still queued", qlen);
2299178354Ssam		m->m_flags |= M_MORE_DATA;
2300178354Ssam	} else {
2301178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2302178354Ssam		    "%s", "recv ps-poll, send packet, queue empty");
2303178354Ssam		if (vap->iv_set_tim != NULL)
2304178354Ssam			vap->iv_set_tim(ni, 0);
2305178354Ssam	}
2306178354Ssam	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2307184288Ssam
2308245098Sadrian	/*
2309248069Sadrian	 * Do the right thing; if it's an encap'ed frame then
2310289164Sadrian	 * call ieee80211_parent_xmitpkt() else
2311254082Sadrian	 * call ieee80211_vap_xmitpkt().
2312245098Sadrian	 */
2313248069Sadrian	if (m->m_flags & M_ENCAP) {
2314289164Sadrian		(void) ieee80211_parent_xmitpkt(ic, m);
2315248069Sadrian	} else {
2316254082Sadrian		(void) ieee80211_vap_xmitpkt(vap, m);
2317245098Sadrian	}
2318178354Ssam}
2319