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: stable/11/sys/net80211/ieee80211_hostap.c 344969 2019-03-09 12:54:10Z 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		 */
696344969Savos		if (subtype == IEEE80211_FC0_SUBTYPE_QOS)
697344969Savos			qos = ieee80211_getqos(wh)[0];
698344969Savos		else
699178354Ssam			qos = 0;
700178354Ssam
701178354Ssam		/*
702178354Ssam		 * Next up, any fragmentation.
703178354Ssam		 */
704178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
705178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
706178354Ssam			if (m == NULL) {
707178354Ssam				/* Fragment dropped or frame not complete yet */
708178354Ssam				goto out;
709178354Ssam			}
710178354Ssam		}
711178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
712178354Ssam
713178354Ssam		/*
714178354Ssam		 * Next strip any MSDU crypto bits.
715178354Ssam		 */
716178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
717178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
718178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
719178354Ssam			vap->iv_stats.is_rx_demicfail++;
720178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
721178354Ssam			goto out;
722178354Ssam		}
723178354Ssam		/* copy to listener after decrypt */
724192468Ssam		if (ieee80211_radiotap_active_vap(vap))
725192468Ssam			ieee80211_radiotap_rx(vap, m);
726178354Ssam		need_tap = 0;
727178354Ssam		/*
728178354Ssam		 * Finally, strip the 802.11 header.
729178354Ssam		 */
730178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
731178354Ssam		if (m == NULL) {
732178354Ssam			/* XXX mask bit to check for both */
733178354Ssam			/* don't count Null data frames as errors */
734178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
735178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
736178354Ssam				goto out;
737178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
738178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
739178354Ssam			vap->iv_stats.is_rx_decap++;
740178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
741178354Ssam			goto err;
742178354Ssam		}
743178354Ssam		eh = mtod(m, struct ether_header *);
744178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
745178354Ssam			/*
746178354Ssam			 * Deny any non-PAE frames received prior to
747178354Ssam			 * authorization.  For open/shared-key
748178354Ssam			 * authentication the port is mark authorized
749178354Ssam			 * after authentication completes.  For 802.1x
750178354Ssam			 * the port is not marked authorized by the
751178354Ssam			 * authenticator until the handshake has completed.
752178354Ssam			 */
753178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
754178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
755178354Ssam				    eh->ether_shost, "data",
756178354Ssam				    "unauthorized port: ether type 0x%x len %u",
757178354Ssam				    eh->ether_type, m->m_pkthdr.len);
758178354Ssam				vap->iv_stats.is_rx_unauth++;
759178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
760178354Ssam				goto err;
761178354Ssam			}
762178354Ssam		} else {
763178354Ssam			/*
764178354Ssam			 * When denying unencrypted frames, discard
765178354Ssam			 * any non-PAE frames received without encryption.
766178354Ssam			 */
767178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
768178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
769178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
770178354Ssam				/*
771178354Ssam				 * Drop unencrypted frames.
772178354Ssam				 */
773178354Ssam				vap->iv_stats.is_rx_unencrypted++;
774178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
775178354Ssam				goto out;
776178354Ssam			}
777178354Ssam		}
778178354Ssam		/* XXX require HT? */
779178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
780178354Ssam			m = ieee80211_decap_amsdu(ni, m);
781178354Ssam			if (m == NULL)
782178354Ssam				return IEEE80211_FC0_TYPE_DATA;
783190391Ssam		} else {
784190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
785190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
786190391Ssam			if (m == NULL)
787178354Ssam				return IEEE80211_FC0_TYPE_DATA;
788190391Ssam#endif
789178354Ssam		}
790178354Ssam		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
791178354Ssam			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
792178354Ssam		else
793178354Ssam			hostap_deliver_data(vap, ni, m);
794178354Ssam		return IEEE80211_FC0_TYPE_DATA;
795178354Ssam
796178354Ssam	case IEEE80211_FC0_TYPE_MGT:
797178354Ssam		vap->iv_stats.is_rx_mgmt++;
798178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
799178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
800178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
801178354Ssam			    wh, "mgt", "incorrect dir 0x%x", dir);
802178354Ssam			vap->iv_stats.is_rx_wrongdir++;
803178354Ssam			goto err;
804178354Ssam		}
805178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
806178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
807178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
808178354Ssam			    m->m_pkthdr.len);
809178354Ssam			vap->iv_stats.is_rx_tooshort++;
810178354Ssam			goto out;
811178354Ssam		}
812178354Ssam		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
813178354Ssam			/* ensure return frames are unicast */
814178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
815178354Ssam			    wh, NULL, "source is multicast: %s",
816178354Ssam			    ether_sprintf(wh->i_addr2));
817178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
818178354Ssam			goto out;
819178354Ssam		}
820178354Ssam#ifdef IEEE80211_DEBUG
821178354Ssam		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
822178354Ssam		    ieee80211_msg_dumppkts(vap)) {
823178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
824298376Savos			    ieee80211_mgt_subtype_name(subtype),
825178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
826178354Ssam		}
827178354Ssam#endif
828260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
829178354Ssam			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
830178354Ssam				/*
831178354Ssam				 * Only shared key auth frames with a challenge
832178354Ssam				 * should be encrypted, discard all others.
833178354Ssam				 */
834178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
835178354Ssam				    wh, NULL,
836178354Ssam				    "%s", "WEP set but not permitted");
837178354Ssam				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
838178354Ssam				goto out;
839178354Ssam			}
840178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
841178354Ssam				/*
842178354Ssam				 * Discard encrypted frames when privacy is off.
843178354Ssam				 */
844178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
845178354Ssam				    wh, NULL, "%s", "WEP set but PRIVACY off");
846178354Ssam				vap->iv_stats.is_rx_noprivacy++;
847178354Ssam				goto out;
848178354Ssam			}
849178354Ssam			hdrspace = ieee80211_hdrspace(ic, wh);
850178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
851178354Ssam			if (key == NULL) {
852178354Ssam				/* NB: stats+msgs handled in crypto_decap */
853178354Ssam				goto out;
854178354Ssam			}
855178354Ssam			wh = mtod(m, struct ieee80211_frame *);
856260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
857178354Ssam		}
858205791Srpaulo		/*
859205791Srpaulo		 * Pass the packet to radiotap before calling iv_recv_mgmt().
860205791Srpaulo		 * Otherwise iv_recv_mgmt() might pass another packet to
861205791Srpaulo		 * radiotap, resulting in out of order packet captures.
862205791Srpaulo		 */
863205516Srpaulo		if (ieee80211_radiotap_active_vap(vap))
864205516Srpaulo			ieee80211_radiotap_rx(vap, m);
865205516Srpaulo		need_tap = 0;
866283535Sadrian		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
867191534Ssam		goto out;
868178354Ssam
869178354Ssam	case IEEE80211_FC0_TYPE_CTL:
870178354Ssam		vap->iv_stats.is_rx_ctl++;
871178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
872191546Ssam		vap->iv_recv_ctl(ni, m, subtype);
873178354Ssam		goto out;
874178354Ssam	default:
875178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
876178354Ssam		    wh, "bad", "frame type 0x%x", type);
877178354Ssam		/* should not come here */
878178354Ssam		break;
879178354Ssam	}
880178354Ssamerr:
881271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
882178354Ssamout:
883178354Ssam	if (m != NULL) {
884192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
885192468Ssam			ieee80211_radiotap_rx(vap, m);
886178354Ssam		m_freem(m);
887178354Ssam	}
888178354Ssam	return type;
889178354Ssam}
890178354Ssam
891178354Ssamstatic void
892178354Ssamhostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
893192468Ssam    int rssi, int nf, uint16_t seq, uint16_t status)
894178354Ssam{
895178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
896178354Ssam
897178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
898178354Ssam
899178354Ssam	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
900178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
901178354Ssam		    ni->ni_macaddr, "open auth",
902178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
903178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
904178354Ssam		/*
905178354Ssam		 * Clear any challenge text that may be there if
906178354Ssam		 * a previous shared key auth failed and then an
907178354Ssam		 * open auth is attempted.
908178354Ssam		 */
909178354Ssam		if (ni->ni_challenge != NULL) {
910283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
911178354Ssam			ni->ni_challenge = NULL;
912178354Ssam		}
913178354Ssam		/* XXX hack to workaround calling convention */
914178354Ssam		ieee80211_send_error(ni, wh->i_addr2,
915178354Ssam		    IEEE80211_FC0_SUBTYPE_AUTH,
916178354Ssam		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
917178354Ssam		return;
918178354Ssam	}
919178354Ssam	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
920178354Ssam		vap->iv_stats.is_rx_bad_auth++;
921178354Ssam		return;
922178354Ssam	}
923178354Ssam	/* always accept open authentication requests */
924178354Ssam	if (ni == vap->iv_bss) {
925178354Ssam		ni = ieee80211_dup_bss(vap, wh->i_addr2);
926178354Ssam		if (ni == NULL)
927178354Ssam			return;
928178354Ssam	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
929178354Ssam		(void) ieee80211_ref_node(ni);
930178354Ssam	/*
931178354Ssam	 * Mark the node as referenced to reflect that it's
932178354Ssam	 * reference count has been bumped to insure it remains
933178354Ssam	 * after the transaction completes.
934178354Ssam	 */
935178354Ssam	ni->ni_flags |= IEEE80211_NODE_AREF;
936186099Ssam	/*
937186151Ssam	 * Mark the node as requiring a valid association id
938186099Ssam	 * before outbound traffic is permitted.
939186099Ssam	 */
940186099Ssam	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
941178354Ssam
942178354Ssam	if (vap->iv_acl != NULL &&
943178354Ssam	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
944178354Ssam		/*
945178354Ssam		 * When the ACL policy is set to RADIUS we defer the
946178354Ssam		 * authorization to a user agent.  Dispatch an event,
947178354Ssam		 * a subsequent MLME call will decide the fate of the
948178354Ssam		 * station.  If the user agent is not present then the
949178354Ssam		 * node will be reclaimed due to inactivity.
950178354Ssam		 */
951178354Ssam		IEEE80211_NOTE_MAC(vap,
952178354Ssam		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
953178354Ssam		    "%s", "station authentication defered (radius acl)");
954178354Ssam		ieee80211_notify_node_auth(ni);
955178354Ssam	} else {
956178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
957178354Ssam		IEEE80211_NOTE_MAC(vap,
958178354Ssam		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
959178354Ssam		    "%s", "station authenticated (open)");
960178354Ssam		/*
961178354Ssam		 * When 802.1x is not in use mark the port
962178354Ssam		 * authorized at this point so traffic can flow.
963178354Ssam		 */
964178354Ssam		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
965178354Ssam			ieee80211_node_authorize(ni);
966178354Ssam	}
967178354Ssam}
968178354Ssam
969178354Ssamstatic void
970178354Ssamhostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
971192468Ssam    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
972178354Ssam    uint16_t seq, uint16_t status)
973178354Ssam{
974178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
975178354Ssam	uint8_t *challenge;
976178354Ssam	int allocbs, estatus;
977178354Ssam
978178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
979178354Ssam
980178354Ssam	/*
981178354Ssam	 * NB: this can happen as we allow pre-shared key
982178354Ssam	 * authentication to be enabled w/o wep being turned
983178354Ssam	 * on so that configuration of these can be done
984178354Ssam	 * in any order.  It may be better to enforce the
985178354Ssam	 * ordering in which case this check would just be
986178354Ssam	 * for sanity/consistency.
987178354Ssam	 */
988178354Ssam	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
989178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
990178354Ssam		    ni->ni_macaddr, "shared key auth",
991178354Ssam		    "%s", " PRIVACY is disabled");
992178354Ssam		estatus = IEEE80211_STATUS_ALG;
993178354Ssam		goto bad;
994178354Ssam	}
995178354Ssam	/*
996178354Ssam	 * Pre-shared key authentication is evil; accept
997178354Ssam	 * it only if explicitly configured (it is supported
998178354Ssam	 * mainly for compatibility with clients like Mac OS X).
999178354Ssam	 */
1000178354Ssam	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1001178354Ssam	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1002178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1003178354Ssam		    ni->ni_macaddr, "shared key auth",
1004178354Ssam		    "bad sta auth mode %u", ni->ni_authmode);
1005178354Ssam		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1006178354Ssam		estatus = IEEE80211_STATUS_ALG;
1007178354Ssam		goto bad;
1008178354Ssam	}
1009178354Ssam
1010178354Ssam	challenge = NULL;
1011178354Ssam	if (frm + 1 < efrm) {
1012178354Ssam		if ((frm[1] + 2) > (efrm - frm)) {
1013178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1014178354Ssam			    ni->ni_macaddr, "shared key auth",
1015178354Ssam			    "ie %d/%d too long",
1016178354Ssam			    frm[0], (frm[1] + 2) - (efrm - frm));
1017178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1018178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1019178354Ssam			goto bad;
1020178354Ssam		}
1021178354Ssam		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1022178354Ssam			challenge = frm;
1023178354Ssam		frm += frm[1] + 2;
1024178354Ssam	}
1025178354Ssam	switch (seq) {
1026178354Ssam	case IEEE80211_AUTH_SHARED_CHALLENGE:
1027178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1028178354Ssam		if (challenge == NULL) {
1029178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1030178354Ssam			    ni->ni_macaddr, "shared key auth",
1031178354Ssam			    "%s", "no challenge");
1032178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1033178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1034178354Ssam			goto bad;
1035178354Ssam		}
1036178354Ssam		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1037178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1038178354Ssam			    ni->ni_macaddr, "shared key auth",
1039178354Ssam			    "bad challenge len %d", challenge[1]);
1040178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1041178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1042178354Ssam			goto bad;
1043178354Ssam		}
1044178354Ssam	default:
1045178354Ssam		break;
1046178354Ssam	}
1047178354Ssam	switch (seq) {
1048178354Ssam	case IEEE80211_AUTH_SHARED_REQUEST:
1049178354Ssam		if (ni == vap->iv_bss) {
1050178354Ssam			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1051178354Ssam			if (ni == NULL) {
1052178354Ssam				/* NB: no way to return an error */
1053178354Ssam				return;
1054178354Ssam			}
1055178354Ssam			allocbs = 1;
1056178354Ssam		} else {
1057178354Ssam			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1058178354Ssam				(void) ieee80211_ref_node(ni);
1059178354Ssam			allocbs = 0;
1060178354Ssam		}
1061178354Ssam		/*
1062178354Ssam		 * Mark the node as referenced to reflect that it's
1063178354Ssam		 * reference count has been bumped to insure it remains
1064178354Ssam		 * after the transaction completes.
1065178354Ssam		 */
1066178354Ssam		ni->ni_flags |= IEEE80211_NODE_AREF;
1067186099Ssam		/*
1068300232Savos		 * Mark the node as requiring a valid association id
1069186099Ssam		 * before outbound traffic is permitted.
1070186099Ssam		 */
1071186099Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1072178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1073192468Ssam		ni->ni_noise = nf;
1074178354Ssam		if (!ieee80211_alloc_challenge(ni)) {
1075178354Ssam			/* NB: don't return error so they rexmit */
1076178354Ssam			return;
1077178354Ssam		}
1078178354Ssam		get_random_bytes(ni->ni_challenge,
1079178354Ssam			IEEE80211_CHALLENGE_LEN);
1080178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1081178354Ssam		    ni, "shared key %sauth request", allocbs ? "" : "re");
1082178354Ssam		/*
1083178354Ssam		 * When the ACL policy is set to RADIUS we defer the
1084178354Ssam		 * authorization to a user agent.  Dispatch an event,
1085178354Ssam		 * a subsequent MLME call will decide the fate of the
1086178354Ssam		 * station.  If the user agent is not present then the
1087178354Ssam		 * node will be reclaimed due to inactivity.
1088178354Ssam		 */
1089178354Ssam		if (vap->iv_acl != NULL &&
1090178354Ssam		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1091178354Ssam			IEEE80211_NOTE_MAC(vap,
1092178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1093178354Ssam			    ni->ni_macaddr,
1094178354Ssam			    "%s", "station authentication defered (radius acl)");
1095178354Ssam			ieee80211_notify_node_auth(ni);
1096178354Ssam			return;
1097178354Ssam		}
1098178354Ssam		break;
1099178354Ssam	case IEEE80211_AUTH_SHARED_RESPONSE:
1100178354Ssam		if (ni == vap->iv_bss) {
1101178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1102178354Ssam			    ni->ni_macaddr, "shared key response",
1103178354Ssam			    "%s", "unknown station");
1104178354Ssam			/* NB: don't send a response */
1105178354Ssam			return;
1106178354Ssam		}
1107178354Ssam		if (ni->ni_challenge == NULL) {
1108178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1109178354Ssam			    ni->ni_macaddr, "shared key response",
1110178354Ssam			    "%s", "no challenge recorded");
1111178354Ssam			vap->iv_stats.is_rx_bad_auth++;
1112178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1113178354Ssam			goto bad;
1114178354Ssam		}
1115178354Ssam		if (memcmp(ni->ni_challenge, &challenge[2],
1116178354Ssam			   challenge[1]) != 0) {
1117178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1118178354Ssam			    ni->ni_macaddr, "shared key response",
1119178354Ssam			    "%s", "challenge mismatch");
1120178354Ssam			vap->iv_stats.is_rx_auth_fail++;
1121178354Ssam			estatus = IEEE80211_STATUS_CHALLENGE;
1122178354Ssam			goto bad;
1123178354Ssam		}
1124178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1125178354Ssam		    ni, "%s", "station authenticated (shared key)");
1126178354Ssam		ieee80211_node_authorize(ni);
1127178354Ssam		break;
1128178354Ssam	default:
1129178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1130178354Ssam		    ni->ni_macaddr, "shared key auth",
1131178354Ssam		    "bad seq %d", seq);
1132178354Ssam		vap->iv_stats.is_rx_bad_auth++;
1133178354Ssam		estatus = IEEE80211_STATUS_SEQUENCE;
1134178354Ssam		goto bad;
1135178354Ssam	}
1136178354Ssam	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1137178354Ssam	return;
1138178354Ssambad:
1139178354Ssam	/*
1140178354Ssam	 * Send an error response; but only when operating as an AP.
1141178354Ssam	 */
1142178354Ssam	/* XXX hack to workaround calling convention */
1143178354Ssam	ieee80211_send_error(ni, wh->i_addr2,
1144178354Ssam	    IEEE80211_FC0_SUBTYPE_AUTH,
1145178354Ssam	    (seq + 1) | (estatus<<16));
1146178354Ssam}
1147178354Ssam
1148178354Ssam/*
1149178354Ssam * Convert a WPA cipher selector OUI to an internal
1150178354Ssam * cipher algorithm.  Where appropriate we also
1151178354Ssam * record any key length.
1152178354Ssam */
1153178354Ssamstatic int
1154178354Ssamwpa_cipher(const uint8_t *sel, uint8_t *keylen)
1155178354Ssam{
1156178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1157298359Savos	uint32_t w = le32dec(sel);
1158178354Ssam
1159178354Ssam	switch (w) {
1160178354Ssam	case WPA_SEL(WPA_CSE_NULL):
1161178354Ssam		return IEEE80211_CIPHER_NONE;
1162178354Ssam	case WPA_SEL(WPA_CSE_WEP40):
1163178354Ssam		if (keylen)
1164178354Ssam			*keylen = 40 / NBBY;
1165178354Ssam		return IEEE80211_CIPHER_WEP;
1166178354Ssam	case WPA_SEL(WPA_CSE_WEP104):
1167178354Ssam		if (keylen)
1168178354Ssam			*keylen = 104 / NBBY;
1169178354Ssam		return IEEE80211_CIPHER_WEP;
1170178354Ssam	case WPA_SEL(WPA_CSE_TKIP):
1171178354Ssam		return IEEE80211_CIPHER_TKIP;
1172178354Ssam	case WPA_SEL(WPA_CSE_CCMP):
1173178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1174178354Ssam	}
1175178354Ssam	return 32;		/* NB: so 1<< is discarded */
1176178354Ssam#undef WPA_SEL
1177178354Ssam}
1178178354Ssam
1179178354Ssam/*
1180178354Ssam * Convert a WPA key management/authentication algorithm
1181178354Ssam * to an internal code.
1182178354Ssam */
1183178354Ssamstatic int
1184178354Ssamwpa_keymgmt(const uint8_t *sel)
1185178354Ssam{
1186178354Ssam#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1187298359Savos	uint32_t w = le32dec(sel);
1188178354Ssam
1189178354Ssam	switch (w) {
1190178354Ssam	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1191178354Ssam		return WPA_ASE_8021X_UNSPEC;
1192178354Ssam	case WPA_SEL(WPA_ASE_8021X_PSK):
1193178354Ssam		return WPA_ASE_8021X_PSK;
1194178354Ssam	case WPA_SEL(WPA_ASE_NONE):
1195178354Ssam		return WPA_ASE_NONE;
1196178354Ssam	}
1197178354Ssam	return 0;		/* NB: so is discarded */
1198178354Ssam#undef WPA_SEL
1199178354Ssam}
1200178354Ssam
1201178354Ssam/*
1202178354Ssam * Parse a WPA information element to collect parameters.
1203178354Ssam * Note that we do not validate security parameters; that
1204178354Ssam * is handled by the authenticator; the parsing done here
1205178354Ssam * is just for internal use in making operational decisions.
1206178354Ssam */
1207178354Ssamstatic int
1208178354Ssamieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1209178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1210178354Ssam{
1211178354Ssam	uint8_t len = frm[1];
1212178354Ssam	uint32_t w;
1213178354Ssam	int n;
1214178354Ssam
1215178354Ssam	/*
1216178354Ssam	 * Check the length once for fixed parts: OUI, type,
1217178354Ssam	 * version, mcast cipher, and 2 selector counts.
1218178354Ssam	 * Other, variable-length data, must be checked separately.
1219178354Ssam	 */
1220178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1221178354Ssam		IEEE80211_DISCARD_IE(vap,
1222178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1223178354Ssam		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1224178354Ssam		return IEEE80211_REASON_IE_INVALID;
1225178354Ssam	}
1226178354Ssam	if (len < 14) {
1227178354Ssam		IEEE80211_DISCARD_IE(vap,
1228178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1229178354Ssam		    wh, "WPA", "too short, len %u", len);
1230178354Ssam		return IEEE80211_REASON_IE_INVALID;
1231178354Ssam	}
1232178354Ssam	frm += 6, len -= 4;		/* NB: len is payload only */
1233200242Srpaulo	/* NB: iswpaoui already validated the OUI and type */
1234298359Savos	w = le16dec(frm);
1235178354Ssam	if (w != WPA_VERSION) {
1236178354Ssam		IEEE80211_DISCARD_IE(vap,
1237178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1238178354Ssam		    wh, "WPA", "bad version %u", w);
1239178354Ssam		return IEEE80211_REASON_IE_INVALID;
1240178354Ssam	}
1241178354Ssam	frm += 2, len -= 2;
1242178354Ssam
1243178354Ssam	memset(rsn, 0, sizeof(*rsn));
1244178354Ssam
1245178354Ssam	/* multicast/group cipher */
1246178354Ssam	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1247178354Ssam	frm += 4, len -= 4;
1248178354Ssam
1249178354Ssam	/* unicast ciphers */
1250298359Savos	n = le16dec(frm);
1251178354Ssam	frm += 2, len -= 2;
1252178354Ssam	if (len < n*4+2) {
1253178354Ssam		IEEE80211_DISCARD_IE(vap,
1254178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1255178354Ssam		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1256178354Ssam		    len, n);
1257178354Ssam		return IEEE80211_REASON_IE_INVALID;
1258178354Ssam	}
1259178354Ssam	w = 0;
1260178354Ssam	for (; n > 0; n--) {
1261178354Ssam		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1262178354Ssam		frm += 4, len -= 4;
1263178354Ssam	}
1264178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1265178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1266178354Ssam	else
1267178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1268178354Ssam
1269178354Ssam	/* key management algorithms */
1270298359Savos	n = le16dec(frm);
1271178354Ssam	frm += 2, len -= 2;
1272178354Ssam	if (len < n*4) {
1273178354Ssam		IEEE80211_DISCARD_IE(vap,
1274178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1275178354Ssam		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1276178354Ssam		    len, n);
1277178354Ssam		return IEEE80211_REASON_IE_INVALID;
1278178354Ssam	}
1279178354Ssam	w = 0;
1280178354Ssam	for (; n > 0; n--) {
1281178354Ssam		w |= wpa_keymgmt(frm);
1282178354Ssam		frm += 4, len -= 4;
1283178354Ssam	}
1284178354Ssam	if (w & WPA_ASE_8021X_UNSPEC)
1285178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1286178354Ssam	else
1287178354Ssam		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1288178354Ssam
1289178354Ssam	if (len > 2)		/* optional capabilities */
1290298359Savos		rsn->rsn_caps = le16dec(frm);
1291178354Ssam
1292178354Ssam	return 0;
1293178354Ssam}
1294178354Ssam
1295178354Ssam/*
1296178354Ssam * Convert an RSN cipher selector OUI to an internal
1297178354Ssam * cipher algorithm.  Where appropriate we also
1298178354Ssam * record any key length.
1299178354Ssam */
1300178354Ssamstatic int
1301178354Ssamrsn_cipher(const uint8_t *sel, uint8_t *keylen)
1302178354Ssam{
1303178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1304298359Savos	uint32_t w = le32dec(sel);
1305178354Ssam
1306178354Ssam	switch (w) {
1307178354Ssam	case RSN_SEL(RSN_CSE_NULL):
1308178354Ssam		return IEEE80211_CIPHER_NONE;
1309178354Ssam	case RSN_SEL(RSN_CSE_WEP40):
1310178354Ssam		if (keylen)
1311178354Ssam			*keylen = 40 / NBBY;
1312178354Ssam		return IEEE80211_CIPHER_WEP;
1313178354Ssam	case RSN_SEL(RSN_CSE_WEP104):
1314178354Ssam		if (keylen)
1315178354Ssam			*keylen = 104 / NBBY;
1316178354Ssam		return IEEE80211_CIPHER_WEP;
1317178354Ssam	case RSN_SEL(RSN_CSE_TKIP):
1318178354Ssam		return IEEE80211_CIPHER_TKIP;
1319178354Ssam	case RSN_SEL(RSN_CSE_CCMP):
1320178354Ssam		return IEEE80211_CIPHER_AES_CCM;
1321178354Ssam	case RSN_SEL(RSN_CSE_WRAP):
1322178354Ssam		return IEEE80211_CIPHER_AES_OCB;
1323178354Ssam	}
1324178354Ssam	return 32;		/* NB: so 1<< is discarded */
1325178354Ssam#undef WPA_SEL
1326178354Ssam}
1327178354Ssam
1328178354Ssam/*
1329178354Ssam * Convert an RSN key management/authentication algorithm
1330178354Ssam * to an internal code.
1331178354Ssam */
1332178354Ssamstatic int
1333178354Ssamrsn_keymgmt(const uint8_t *sel)
1334178354Ssam{
1335178354Ssam#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1336298359Savos	uint32_t w = le32dec(sel);
1337178354Ssam
1338178354Ssam	switch (w) {
1339178354Ssam	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1340178354Ssam		return RSN_ASE_8021X_UNSPEC;
1341178354Ssam	case RSN_SEL(RSN_ASE_8021X_PSK):
1342178354Ssam		return RSN_ASE_8021X_PSK;
1343178354Ssam	case RSN_SEL(RSN_ASE_NONE):
1344178354Ssam		return RSN_ASE_NONE;
1345178354Ssam	}
1346178354Ssam	return 0;		/* NB: so is discarded */
1347178354Ssam#undef RSN_SEL
1348178354Ssam}
1349178354Ssam
1350178354Ssam/*
1351178354Ssam * Parse a WPA/RSN information element to collect parameters
1352178354Ssam * and validate the parameters against what has been
1353178354Ssam * configured for the system.
1354178354Ssam */
1355178354Ssamstatic int
1356178354Ssamieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1357178354Ssam	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1358178354Ssam{
1359178354Ssam	uint8_t len = frm[1];
1360178354Ssam	uint32_t w;
1361178354Ssam	int n;
1362178354Ssam
1363178354Ssam	/*
1364178354Ssam	 * Check the length once for fixed parts:
1365178354Ssam	 * version, mcast cipher, and 2 selector counts.
1366178354Ssam	 * Other, variable-length data, must be checked separately.
1367178354Ssam	 */
1368178354Ssam	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1369178354Ssam		IEEE80211_DISCARD_IE(vap,
1370178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1371178354Ssam		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1372178354Ssam		return IEEE80211_REASON_IE_INVALID;
1373178354Ssam	}
1374178354Ssam	if (len < 10) {
1375178354Ssam		IEEE80211_DISCARD_IE(vap,
1376178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1377178354Ssam		    wh, "RSN", "too short, len %u", len);
1378178354Ssam		return IEEE80211_REASON_IE_INVALID;
1379178354Ssam	}
1380178354Ssam	frm += 2;
1381298359Savos	w = le16dec(frm);
1382178354Ssam	if (w != RSN_VERSION) {
1383178354Ssam		IEEE80211_DISCARD_IE(vap,
1384178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1385178354Ssam		    wh, "RSN", "bad version %u", w);
1386178354Ssam		return IEEE80211_REASON_IE_INVALID;
1387178354Ssam	}
1388178354Ssam	frm += 2, len -= 2;
1389178354Ssam
1390178354Ssam	memset(rsn, 0, sizeof(*rsn));
1391178354Ssam
1392178354Ssam	/* multicast/group cipher */
1393178354Ssam	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1394178354Ssam	frm += 4, len -= 4;
1395178354Ssam
1396178354Ssam	/* unicast ciphers */
1397298359Savos	n = le16dec(frm);
1398178354Ssam	frm += 2, len -= 2;
1399178354Ssam	if (len < n*4+2) {
1400178354Ssam		IEEE80211_DISCARD_IE(vap,
1401178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1402178354Ssam		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1403178354Ssam		    len, n);
1404178354Ssam		return IEEE80211_REASON_IE_INVALID;
1405178354Ssam	}
1406178354Ssam	w = 0;
1407178354Ssam	for (; n > 0; n--) {
1408178354Ssam		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1409178354Ssam		frm += 4, len -= 4;
1410178354Ssam	}
1411178354Ssam	if (w & (1<<IEEE80211_CIPHER_TKIP))
1412178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1413178354Ssam	else
1414178354Ssam		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1415178354Ssam
1416178354Ssam	/* key management algorithms */
1417298359Savos	n = le16dec(frm);
1418178354Ssam	frm += 2, len -= 2;
1419178354Ssam	if (len < n*4) {
1420178354Ssam		IEEE80211_DISCARD_IE(vap,
1421178354Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1422178354Ssam		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1423178354Ssam		    len, n);
1424178354Ssam		return IEEE80211_REASON_IE_INVALID;
1425178354Ssam	}
1426178354Ssam	w = 0;
1427178354Ssam	for (; n > 0; n--) {
1428178354Ssam		w |= rsn_keymgmt(frm);
1429178354Ssam		frm += 4, len -= 4;
1430178354Ssam	}
1431178354Ssam	if (w & RSN_ASE_8021X_UNSPEC)
1432178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1433178354Ssam	else
1434178354Ssam		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1435178354Ssam
1436178354Ssam	/* optional RSN capabilities */
1437178354Ssam	if (len > 2)
1438298359Savos		rsn->rsn_caps = le16dec(frm);
1439178354Ssam	/* XXXPMKID */
1440178354Ssam
1441178354Ssam	return 0;
1442178354Ssam}
1443178354Ssam
1444178354Ssam/*
1445298995Spfg * WPA/802.11i association request processing.
1446178354Ssam */
1447178354Ssamstatic int
1448178354Ssamwpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1449178354Ssam	const struct ieee80211_frame *wh, const uint8_t *wpa,
1450178354Ssam	const uint8_t *rsn, uint16_t capinfo)
1451178354Ssam{
1452178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1453178354Ssam	uint8_t reason;
1454178354Ssam	int badwparsn;
1455178354Ssam
1456178354Ssam	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1457178354Ssam	if (wpa == NULL && rsn == NULL) {
1458178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1459178354Ssam			/*
1460178354Ssam			 * W-Fi Protected Setup (WPS) permits
1461178354Ssam			 * clients to associate and pass EAPOL frames
1462178354Ssam			 * to establish initial credentials.
1463178354Ssam			 */
1464178354Ssam			ni->ni_flags |= IEEE80211_NODE_WPS;
1465178354Ssam			return 1;
1466178354Ssam		}
1467178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1468178354Ssam		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1469178354Ssam			/*
1470178354Ssam			 * Transitional Security Network.  Permits clients
1471178354Ssam			 * to associate and use WEP while WPA is configured.
1472178354Ssam			 */
1473178354Ssam			ni->ni_flags |= IEEE80211_NODE_TSN;
1474178354Ssam			return 1;
1475178354Ssam		}
1476178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1477178354Ssam		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1478178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1479178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1480178354Ssam		goto bad;
1481178354Ssam	}
1482178354Ssam	/* assert right association security credentials */
1483178354Ssam	badwparsn = 0;			/* NB: to silence compiler */
1484178354Ssam	switch (vap->iv_flags & IEEE80211_F_WPA) {
1485178354Ssam	case IEEE80211_F_WPA1:
1486178354Ssam		badwparsn = (wpa == NULL);
1487178354Ssam		break;
1488178354Ssam	case IEEE80211_F_WPA2:
1489178354Ssam		badwparsn = (rsn == NULL);
1490178354Ssam		break;
1491178354Ssam	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1492178354Ssam		badwparsn = (wpa == NULL && rsn == NULL);
1493178354Ssam		break;
1494178354Ssam	}
1495178354Ssam	if (badwparsn) {
1496178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1497178354Ssam		    wh, NULL,
1498178354Ssam		    "%s", "missing WPA/RSN IE in association request");
1499178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1500178354Ssam		reason = IEEE80211_REASON_IE_INVALID;
1501178354Ssam		goto bad;
1502178354Ssam	}
1503178354Ssam	/*
1504178354Ssam	 * Parse WPA/RSN information element.
1505178354Ssam	 */
1506178354Ssam	if (wpa != NULL)
1507178354Ssam		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1508178354Ssam	else
1509178354Ssam		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1510178354Ssam	if (reason != 0) {
1511178354Ssam		/* XXX distinguish WPA/RSN? */
1512178354Ssam		vap->iv_stats.is_rx_assoc_badwpaie++;
1513178354Ssam		goto bad;
1514178354Ssam	}
1515178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1516178354Ssam	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1517178354Ssam	    wpa != NULL ? "WPA" : "RSN",
1518178354Ssam	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1519178354Ssam	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1520178354Ssam	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1521178354Ssam
1522178354Ssam	return 1;
1523178354Ssambad:
1524178354Ssam	ieee80211_node_deauth(ni, reason);
1525178354Ssam	return 0;
1526178354Ssam}
1527178354Ssam
1528178354Ssam/* XXX find a better place for definition */
1529178354Ssamstruct l2_update_frame {
1530178354Ssam	struct ether_header eh;
1531178354Ssam	uint8_t dsap;
1532178354Ssam	uint8_t ssap;
1533178354Ssam	uint8_t control;
1534178354Ssam	uint8_t xid[3];
1535178354Ssam}  __packed;
1536178354Ssam
1537178354Ssam/*
1538178354Ssam * Deliver a TGf L2UF frame on behalf of a station.
1539178354Ssam * This primes any bridge when the station is roaming
1540178354Ssam * between ap's on the same wired network.
1541178354Ssam */
1542178354Ssamstatic void
1543178354Ssamieee80211_deliver_l2uf(struct ieee80211_node *ni)
1544178354Ssam{
1545178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1546178354Ssam	struct ifnet *ifp = vap->iv_ifp;
1547178354Ssam	struct mbuf *m;
1548178354Ssam	struct l2_update_frame *l2uf;
1549178354Ssam	struct ether_header *eh;
1550178354Ssam
1551178354Ssam	m = m_gethdr(M_NOWAIT, MT_DATA);
1552178354Ssam	if (m == NULL) {
1553178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1554178354Ssam		    "%s", "no mbuf for l2uf frame");
1555178354Ssam		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1556178354Ssam		return;
1557178354Ssam	}
1558178354Ssam	l2uf = mtod(m, struct l2_update_frame *);
1559178354Ssam	eh = &l2uf->eh;
1560178354Ssam	/* dst: Broadcast address */
1561178354Ssam	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1562178354Ssam	/* src: associated STA */
1563178354Ssam	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1564178354Ssam	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1565178354Ssam
1566178354Ssam	l2uf->dsap = 0;
1567178354Ssam	l2uf->ssap = 0;
1568178354Ssam	l2uf->control = 0xf5;
1569178354Ssam	l2uf->xid[0] = 0x81;
1570178354Ssam	l2uf->xid[1] = 0x80;
1571178354Ssam	l2uf->xid[2] = 0x00;
1572178354Ssam
1573178354Ssam	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1574178354Ssam	hostap_deliver_data(vap, ni, m);
1575178354Ssam}
1576178354Ssam
1577178354Ssamstatic void
1578178354Ssamratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1579178354Ssam	int reassoc, int resp, const char *tag, int rate)
1580178354Ssam{
1581178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1582178354Ssam	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1583178354Ssam	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1584178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1585178354Ssam	ieee80211_node_leave(ni);
1586178354Ssam}
1587178354Ssam
1588178354Ssamstatic void
1589178354Ssamcapinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1590178354Ssam	int reassoc, int resp, const char *tag, int capinfo)
1591178354Ssam{
1592178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1593178354Ssam
1594178354Ssam	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1595178354Ssam	    "deny %s request, %s mismatch 0x%x",
1596178354Ssam	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1597178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1598178354Ssam	ieee80211_node_leave(ni);
1599178354Ssam	vap->iv_stats.is_rx_assoc_capmismatch++;
1600178354Ssam}
1601178354Ssam
1602178354Ssamstatic void
1603178354Ssamhtcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1604178354Ssam	int reassoc, int resp)
1605178354Ssam{
1606178354Ssam	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1607178354Ssam	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1608178354Ssam	/* XXX no better code */
1609193543Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1610178354Ssam	ieee80211_node_leave(ni);
1611178354Ssam}
1612178354Ssam
1613178354Ssamstatic void
1614178354Ssamauthalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1615178354Ssam	int algo, int seq, int status)
1616178354Ssam{
1617178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1618178354Ssam
1619178354Ssam	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1620178354Ssam	    wh, NULL, "unsupported alg %d", algo);
1621178354Ssam	vap->iv_stats.is_rx_auth_unsupported++;
1622178354Ssam	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1623178354Ssam	    seq | (status << 16));
1624178354Ssam}
1625178354Ssam
1626178354Ssamstatic __inline int
1627178354Ssamishtmixed(const uint8_t *ie)
1628178354Ssam{
1629178354Ssam	const struct ieee80211_ie_htinfo *ht =
1630178354Ssam	    (const struct ieee80211_ie_htinfo *) ie;
1631178354Ssam	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1632178354Ssam	    IEEE80211_HTINFO_OPMODE_MIXED;
1633178354Ssam}
1634178354Ssam
1635178354Ssamstatic int
1636178354Ssamis11bclient(const uint8_t *rates, const uint8_t *xrates)
1637178354Ssam{
1638178354Ssam	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1639178354Ssam	int i;
1640178354Ssam
1641178354Ssam	/* NB: the 11b clients we care about will not have xrates */
1642178354Ssam	if (xrates != NULL || rates == NULL)
1643178354Ssam		return 0;
1644178354Ssam	for (i = 0; i < rates[1]; i++) {
1645178354Ssam		int r = rates[2+i] & IEEE80211_RATE_VAL;
1646178354Ssam		if (r > 2*11 || ((1<<r) & brates) == 0)
1647178354Ssam			return 0;
1648178354Ssam	}
1649178354Ssam	return 1;
1650178354Ssam}
1651178354Ssam
1652178354Ssamstatic void
1653178354Ssamhostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1654283535Sadrian	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1655178354Ssam{
1656178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1657178354Ssam	struct ieee80211com *ic = ni->ni_ic;
1658178354Ssam	struct ieee80211_frame *wh;
1659178354Ssam	uint8_t *frm, *efrm, *sfrm;
1660178354Ssam	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1661178354Ssam	int reassoc, resp;
1662178354Ssam	uint8_t rate;
1663178354Ssam
1664178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
1665178354Ssam	frm = (uint8_t *)&wh[1];
1666178354Ssam	efrm = mtod(m0, uint8_t *) + m0->m_len;
1667178354Ssam	switch (subtype) {
1668178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1669178354Ssam		/*
1670178354Ssam		 * We process beacon/probe response frames when scanning;
1671178354Ssam		 * otherwise we check beacon frames for overlapping non-ERP
1672178354Ssam		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1673298360Savos		 */
1674298360Savos		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1675178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1676178354Ssam			return;
1677178354Ssam		}
1678298360Savos		/* FALLTHROUGH */
1679298360Savos	case IEEE80211_FC0_SUBTYPE_BEACON: {
1680298360Savos		struct ieee80211_scanparams scan;
1681298360Savos
1682178354Ssam		/* NB: accept off-channel frames */
1683283535Sadrian		/* XXX TODO: use rxstatus to determine off-channel details */
1684283535Sadrian		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1685178354Ssam			return;
1686178354Ssam		/*
1687178354Ssam		 * Count frame now that we know it's to be processed.
1688178354Ssam		 */
1689178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1690178354Ssam			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1691178354Ssam			IEEE80211_NODE_STAT(ni, rx_beacons);
1692178354Ssam		} else
1693178354Ssam			IEEE80211_NODE_STAT(ni, rx_proberesp);
1694178354Ssam		/*
1695178354Ssam		 * If scanning, just pass information to the scan module.
1696178354Ssam		 */
1697178354Ssam		if (ic->ic_flags & IEEE80211_F_SCAN) {
1698178354Ssam			if (scan.status == 0 &&		/* NB: on channel */
1699178354Ssam			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1700178354Ssam				/*
1701178354Ssam				 * Actively scanning a channel marked passive;
1702178354Ssam				 * send a probe request now that we know there
1703178354Ssam				 * is 802.11 traffic present.
1704178354Ssam				 *
1705178354Ssam				 * XXX check if the beacon we recv'd gives
1706178354Ssam				 * us what we need and suppress the probe req
1707178354Ssam				 */
1708178354Ssam				ieee80211_probe_curchan(vap, 1);
1709178354Ssam				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1710178354Ssam			}
1711282742Sadrian			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1712282742Sadrian			    subtype, rssi, nf);
1713178354Ssam			return;
1714178354Ssam		}
1715178354Ssam		/*
1716178354Ssam		 * Check beacon for overlapping bss w/ non ERP stations.
1717178354Ssam		 * If we detect one and protection is configured but not
1718178354Ssam		 * enabled, enable it and start a timer that'll bring us
1719178354Ssam		 * out if we stop seeing the bss.
1720178354Ssam		 */
1721178354Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1722178354Ssam		    scan.status == 0 &&			/* NB: on-channel */
1723178354Ssam		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1724178354Ssam		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1725178354Ssam			ic->ic_lastnonerp = ticks;
1726178354Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1727178354Ssam			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1728178354Ssam			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1729178354Ssam				IEEE80211_NOTE_FRAME(vap,
1730178354Ssam				    IEEE80211_MSG_ASSOC, wh,
1731178354Ssam				    "non-ERP present on channel %d "
1732178354Ssam				    "(saw erp 0x%x from channel %d), "
1733178354Ssam				    "enable use of protection",
1734178354Ssam				    ic->ic_curchan->ic_ieee,
1735178354Ssam				    scan.erp, scan.chan);
1736178354Ssam				ic->ic_flags |= IEEE80211_F_USEPROT;
1737178354Ssam				ieee80211_notify_erp(ic);
1738178354Ssam			}
1739178354Ssam		}
1740178354Ssam		/*
1741178354Ssam		 * Check beacon for non-HT station on HT channel
1742178354Ssam		 * and update HT BSS occupancy as appropriate.
1743178354Ssam		 */
1744178354Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1745178354Ssam			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1746178354Ssam				/*
1747178354Ssam				 * Off control channel; only check frames
1748178354Ssam				 * that come in the extension channel when
1749178354Ssam				 * operating w/ HT40.
1750178354Ssam				 */
1751178354Ssam				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1752178354Ssam					break;
1753178354Ssam				if (scan.chan != ic->ic_curchan->ic_extieee)
1754178354Ssam					break;
1755178354Ssam			}
1756178354Ssam			if (scan.htinfo == NULL) {
1757178354Ssam				ieee80211_htprot_update(ic,
1758178354Ssam				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1759178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1760178354Ssam			} else if (ishtmixed(scan.htinfo)) {
1761178354Ssam				/* XXX? take NONHT_PRESENT from beacon? */
1762178354Ssam				ieee80211_htprot_update(ic,
1763178354Ssam				    IEEE80211_HTINFO_OPMODE_MIXED |
1764178354Ssam				    IEEE80211_HTINFO_NONHT_PRESENT);
1765178354Ssam			}
1766178354Ssam		}
1767178354Ssam		break;
1768178354Ssam	}
1769178354Ssam
1770178354Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1771178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1772178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1773178354Ssam			return;
1774178354Ssam		}
1775178354Ssam		/*
1776228514Sadrian		 * Consult the ACL policy module if setup.
1777228514Sadrian		 */
1778228622Sbschmidt		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1779228514Sadrian			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1780228514Sadrian			    wh, NULL, "%s", "disallowed by ACL");
1781228514Sadrian			vap->iv_stats.is_rx_acl++;
1782228514Sadrian			return;
1783228514Sadrian		}
1784228514Sadrian		/*
1785178354Ssam		 * prreq frame format
1786178354Ssam		 *	[tlv] ssid
1787178354Ssam		 *	[tlv] supported rates
1788178354Ssam		 *	[tlv] extended supported rates
1789178354Ssam		 */
1790178354Ssam		ssid = rates = xrates = NULL;
1791178354Ssam		while (efrm - frm > 1) {
1792178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1793178354Ssam			switch (*frm) {
1794178354Ssam			case IEEE80211_ELEMID_SSID:
1795178354Ssam				ssid = frm;
1796178354Ssam				break;
1797178354Ssam			case IEEE80211_ELEMID_RATES:
1798178354Ssam				rates = frm;
1799178354Ssam				break;
1800178354Ssam			case IEEE80211_ELEMID_XRATES:
1801178354Ssam				xrates = frm;
1802178354Ssam				break;
1803178354Ssam			}
1804178354Ssam			frm += frm[1] + 2;
1805178354Ssam		}
1806178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1807178354Ssam		if (xrates != NULL)
1808178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1809178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1810178354Ssam		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1811178354Ssam		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1812178354Ssam		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1813178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1814178354Ssam			    wh, NULL,
1815178354Ssam			    "%s", "no ssid with ssid suppression enabled");
1816178354Ssam			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1817178354Ssam			return;
1818178354Ssam		}
1819178354Ssam
1820178354Ssam		/* XXX find a better class or define it's own */
1821178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1822178354Ssam		    "%s", "recv probe req");
1823178354Ssam		/*
1824178354Ssam		 * Some legacy 11b clients cannot hack a complete
1825178354Ssam		 * probe response frame.  When the request includes
1826178354Ssam		 * only a bare-bones rate set, communicate this to
1827178354Ssam		 * the transmit side.
1828178354Ssam		 */
1829178354Ssam		ieee80211_send_proberesp(vap, wh->i_addr2,
1830178354Ssam		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1831178354Ssam		break;
1832178354Ssam
1833178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH: {
1834178354Ssam		uint16_t algo, seq, status;
1835178354Ssam
1836178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1837178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1838178354Ssam			return;
1839178354Ssam		}
1840178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1841178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1842178354Ssam			    wh, NULL, "%s", "wrong bssid");
1843178354Ssam			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1844178354Ssam			return;
1845178354Ssam		}
1846178354Ssam		/*
1847178354Ssam		 * auth frame format
1848178354Ssam		 *	[2] algorithm
1849178354Ssam		 *	[2] sequence
1850178354Ssam		 *	[2] status
1851178354Ssam		 *	[tlv*] challenge
1852178354Ssam		 */
1853178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1854178354Ssam		algo   = le16toh(*(uint16_t *)frm);
1855178354Ssam		seq    = le16toh(*(uint16_t *)(frm + 2));
1856178354Ssam		status = le16toh(*(uint16_t *)(frm + 4));
1857178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1858178354Ssam		    "recv auth frame with algorithm %d seq %d", algo, seq);
1859178354Ssam		/*
1860178354Ssam		 * Consult the ACL policy module if setup.
1861178354Ssam		 */
1862228622Sbschmidt		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1863178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1864178354Ssam			    wh, NULL, "%s", "disallowed by ACL");
1865178354Ssam			vap->iv_stats.is_rx_acl++;
1866178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1867178354Ssam			    IEEE80211_FC0_SUBTYPE_AUTH,
1868178354Ssam			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1869178354Ssam			return;
1870178354Ssam		}
1871178354Ssam		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1872178354Ssam			IEEE80211_DISCARD(vap,
1873178354Ssam			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1874178354Ssam			    wh, NULL, "%s", "TKIP countermeasures enabled");
1875178354Ssam			vap->iv_stats.is_rx_auth_countermeasures++;
1876178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1877178354Ssam				IEEE80211_FC0_SUBTYPE_AUTH,
1878178354Ssam				IEEE80211_REASON_MIC_FAILURE);
1879178354Ssam			return;
1880178354Ssam		}
1881178354Ssam		if (algo == IEEE80211_AUTH_ALG_SHARED)
1882192468Ssam			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1883192468Ssam			    seq, status);
1884178354Ssam		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1885192468Ssam			hostap_auth_open(ni, wh, rssi, nf, seq, status);
1886178354Ssam		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1887178354Ssam			authalgreject(ni, wh, algo,
1888178354Ssam			    seq+1, IEEE80211_STATUS_ALG);
1889178354Ssam			return;
1890178354Ssam		} else {
1891178354Ssam			/*
1892178354Ssam			 * We assume that an unknown algorithm is the result
1893178354Ssam			 * of a decryption failure on a shared key auth frame;
1894178354Ssam			 * return a status code appropriate for that instead
1895178354Ssam			 * of IEEE80211_STATUS_ALG.
1896178354Ssam			 *
1897178354Ssam			 * NB: a seq# of 4 is intentional; the decrypted
1898178354Ssam			 *     frame likely has a bogus seq value.
1899178354Ssam			 */
1900178354Ssam			authalgreject(ni, wh, algo,
1901178354Ssam			    4, IEEE80211_STATUS_CHALLENGE);
1902178354Ssam			return;
1903178354Ssam		}
1904178354Ssam		break;
1905178354Ssam	}
1906178354Ssam
1907178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1908178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1909178354Ssam		uint16_t capinfo, lintval;
1910178354Ssam		struct ieee80211_rsnparms rsnparms;
1911178354Ssam
1912178354Ssam		if (vap->iv_state != IEEE80211_S_RUN) {
1913178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1914178354Ssam			return;
1915178354Ssam		}
1916178354Ssam		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1917178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1918178354Ssam			    wh, NULL, "%s", "wrong bssid");
1919178354Ssam			vap->iv_stats.is_rx_assoc_bss++;
1920178354Ssam			return;
1921178354Ssam		}
1922178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1923178354Ssam			reassoc = 1;
1924178354Ssam			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1925178354Ssam		} else {
1926178354Ssam			reassoc = 0;
1927178354Ssam			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1928178354Ssam		}
1929178354Ssam		if (ni == vap->iv_bss) {
1930178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1931178354Ssam			    "deny %s request, sta not authenticated",
1932178354Ssam			    reassoc ? "reassoc" : "assoc");
1933178354Ssam			ieee80211_send_error(ni, wh->i_addr2,
1934178354Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1935178354Ssam			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1936178354Ssam			vap->iv_stats.is_rx_assoc_notauth++;
1937178354Ssam			return;
1938178354Ssam		}
1939178354Ssam
1940178354Ssam		/*
1941178354Ssam		 * asreq frame format
1942178354Ssam		 *	[2] capability information
1943178354Ssam		 *	[2] listen interval
1944178354Ssam		 *	[6*] current AP address (reassoc only)
1945178354Ssam		 *	[tlv] ssid
1946178354Ssam		 *	[tlv] supported rates
1947178354Ssam		 *	[tlv] extended supported rates
1948178354Ssam		 *	[tlv] WPA or RSN
1949178354Ssam		 *	[tlv] HT capabilities
1950178354Ssam		 *	[tlv] Atheros capabilities
1951178354Ssam		 */
1952178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1953178354Ssam		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1954178354Ssam		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1955178354Ssam		if (reassoc)
1956178354Ssam			frm += 6;	/* ignore current AP info */
1957178354Ssam		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1958178354Ssam		sfrm = frm;
1959178354Ssam		while (efrm - frm > 1) {
1960178354Ssam			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1961178354Ssam			switch (*frm) {
1962178354Ssam			case IEEE80211_ELEMID_SSID:
1963178354Ssam				ssid = frm;
1964178354Ssam				break;
1965178354Ssam			case IEEE80211_ELEMID_RATES:
1966178354Ssam				rates = frm;
1967178354Ssam				break;
1968178354Ssam			case IEEE80211_ELEMID_XRATES:
1969178354Ssam				xrates = frm;
1970178354Ssam				break;
1971178354Ssam			case IEEE80211_ELEMID_RSN:
1972178354Ssam				rsn = frm;
1973178354Ssam				break;
1974178354Ssam			case IEEE80211_ELEMID_HTCAP:
1975178354Ssam				htcap = frm;
1976178354Ssam				break;
1977178354Ssam			case IEEE80211_ELEMID_VENDOR:
1978178354Ssam				if (iswpaoui(frm))
1979178354Ssam					wpa = frm;
1980178354Ssam				else if (iswmeinfo(frm))
1981178354Ssam					wme = frm;
1982190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1983178354Ssam				else if (isatherosoui(frm))
1984178354Ssam					ath = frm;
1985190391Ssam#endif
1986193655Ssam				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1987178354Ssam					if (ishtcapoui(frm) && htcap == NULL)
1988178354Ssam						htcap = frm;
1989178354Ssam				}
1990178354Ssam				break;
1991178354Ssam			}
1992178354Ssam			frm += frm[1] + 2;
1993178354Ssam		}
1994178354Ssam		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1995178354Ssam		if (xrates != NULL)
1996178354Ssam			IEEE80211_VERIFY_ELEMENT(xrates,
1997178354Ssam				IEEE80211_RATE_MAXSIZE - rates[1], return);
1998178354Ssam		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1999178354Ssam		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2000178354Ssam		if (htcap != NULL) {
2001178354Ssam			IEEE80211_VERIFY_LENGTH(htcap[1],
2002178354Ssam			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2003178354Ssam			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2004178354Ssam			         sizeof(struct ieee80211_ie_htcap)-2,
2005178354Ssam			     return);		/* XXX just NULL out? */
2006178354Ssam		}
2007178354Ssam
2008178354Ssam		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2009178354Ssam		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2010178354Ssam			return;
2011178354Ssam		/* discard challenge after association */
2012178354Ssam		if (ni->ni_challenge != NULL) {
2013283538Sadrian			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2014178354Ssam			ni->ni_challenge = NULL;
2015178354Ssam		}
2016178354Ssam		/* NB: 802.11 spec says to ignore station's privacy bit */
2017178354Ssam		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2018178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2019178354Ssam			    "capability", capinfo);
2020178354Ssam			return;
2021178354Ssam		}
2022178354Ssam		/*
2023178354Ssam		 * Disallow re-associate w/ invalid slot time setting.
2024178354Ssam		 */
2025178354Ssam		if (ni->ni_associd != 0 &&
2026178354Ssam		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2027178354Ssam		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2028178354Ssam			capinfomismatch(ni, wh, reassoc, resp,
2029178354Ssam			    "slot time", capinfo);
2030178354Ssam			return;
2031178354Ssam		}
2032178354Ssam		rate = ieee80211_setup_rates(ni, rates, xrates,
2033178354Ssam				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2034178354Ssam				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2035178354Ssam		if (rate & IEEE80211_RATE_BASIC) {
2036178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2037178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2038178354Ssam			return;
2039178354Ssam		}
2040178354Ssam		/*
2041178354Ssam		 * If constrained to 11g-only stations reject an
2042178354Ssam		 * 11b-only station.  We cheat a bit here by looking
2043178354Ssam		 * at the max negotiated xmit rate and assuming anyone
2044178354Ssam		 * with a best rate <24Mb/s is an 11b station.
2045178354Ssam		 */
2046178354Ssam		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2047178354Ssam			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2048178354Ssam			vap->iv_stats.is_rx_assoc_norate++;
2049178354Ssam			return;
2050178354Ssam		}
2051178354Ssam		/*
2052178354Ssam		 * Do HT rate set handling and setup HT node state.
2053178354Ssam		 */
2054178354Ssam		ni->ni_chan = vap->iv_bss->ni_chan;
2055178354Ssam		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2056178354Ssam			rate = ieee80211_setup_htrates(ni, htcap,
2057178354Ssam				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2058178354Ssam				IEEE80211_F_DOBRS);
2059178354Ssam			if (rate & IEEE80211_RATE_BASIC) {
2060178354Ssam				ratesetmismatch(ni, wh, reassoc, resp,
2061178354Ssam				    "HT", rate);
2062178354Ssam				vap->iv_stats.is_ht_assoc_norate++;
2063178354Ssam				return;
2064178354Ssam			}
2065183254Ssam			ieee80211_ht_node_init(ni);
2066183254Ssam			ieee80211_ht_updatehtcap(ni, htcap);
2067178354Ssam		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2068178354Ssam			ieee80211_ht_node_cleanup(ni);
2069190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2070297604Sadrian		/* Always do ff node cleanup; for A-MSDU */
2071297604Sadrian		ieee80211_ff_node_cleanup(ni);
2072190579Ssam#endif
2073178354Ssam		/*
2074178354Ssam		 * Allow AMPDU operation only with unencrypted traffic
2075178354Ssam		 * or AES-CCM; the 11n spec only specifies these ciphers
2076178354Ssam		 * so permitting any others is undefined and can lead
2077178354Ssam		 * to interoperability problems.
2078178354Ssam		 */
2079178354Ssam		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2080178354Ssam		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2081178354Ssam		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2082178354Ssam		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2083178354Ssam			IEEE80211_NOTE(vap,
2084178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2085178354Ssam			    "disallow HT use because WEP or TKIP requested, "
2086178354Ssam			    "capinfo 0x%x ucastcipher %d", capinfo,
2087178354Ssam			    rsnparms.rsn_ucastcipher);
2088178354Ssam			ieee80211_ht_node_cleanup(ni);
2089297604Sadrian#ifdef IEEE80211_SUPPORT_SUPERG
2090297604Sadrian			/* Always do ff node cleanup; for A-MSDU */
2091297604Sadrian			ieee80211_ff_node_cleanup(ni);
2092297604Sadrian#endif
2093178354Ssam			vap->iv_stats.is_ht_assoc_downgrade++;
2094178354Ssam		}
2095178354Ssam		/*
2096178354Ssam		 * If constrained to 11n-only stations reject legacy stations.
2097178354Ssam		 */
2098193655Ssam		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2099178354Ssam		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2100178354Ssam			htcapmismatch(ni, wh, reassoc, resp);
2101178354Ssam			vap->iv_stats.is_ht_assoc_nohtcap++;
2102178354Ssam			return;
2103178354Ssam		}
2104178354Ssam		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2105192468Ssam		ni->ni_noise = nf;
2106178354Ssam		ni->ni_intval = lintval;
2107178354Ssam		ni->ni_capinfo = capinfo;
2108178354Ssam		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2109178354Ssam		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2110178354Ssam		/*
2111178354Ssam		 * Store the IEs.
2112178354Ssam		 * XXX maybe better to just expand
2113178354Ssam		 */
2114178354Ssam		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2115178354Ssam#define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2116178354Ssam			if (wpa != NULL)
2117178354Ssam				setie(wpa_ie, wpa - sfrm);
2118178354Ssam			if (rsn != NULL)
2119178354Ssam				setie(rsn_ie, rsn - sfrm);
2120178354Ssam			if (htcap != NULL)
2121178354Ssam				setie(htcap_ie, htcap - sfrm);
2122178354Ssam			if (wme != NULL) {
2123178354Ssam				setie(wme_ie, wme - sfrm);
2124178354Ssam				/*
2125178354Ssam				 * Mark node as capable of QoS.
2126178354Ssam				 */
2127178354Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
2128178354Ssam			} else
2129178354Ssam				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2130190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
2131178354Ssam			if (ath != NULL) {
2132178354Ssam				setie(ath_ie, ath - sfrm);
2133178354Ssam				/*
2134178354Ssam				 * Parse ATH station parameters.
2135178354Ssam				 */
2136178354Ssam				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2137178354Ssam			} else
2138190391Ssam#endif
2139178354Ssam				ni->ni_ath_flags = 0;
2140178354Ssam#undef setie
2141178354Ssam		} else {
2142178354Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2143178354Ssam			ni->ni_ath_flags = 0;
2144178354Ssam		}
2145178354Ssam		ieee80211_node_join(ni, resp);
2146178354Ssam		ieee80211_deliver_l2uf(ni);
2147178354Ssam		break;
2148178354Ssam	}
2149178354Ssam
2150178354Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2151178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2152178354Ssam		uint16_t reason;
2153178354Ssam
2154178354Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
2155178354Ssam		    /* NB: can happen when in promiscuous mode */
2156178354Ssam		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2157178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2158178354Ssam			break;
2159178354Ssam		}
2160178354Ssam		/*
2161178354Ssam		 * deauth/disassoc frame format
2162178354Ssam		 *	[2] reason
2163178354Ssam		 */
2164178354Ssam		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2165178354Ssam		reason = le16toh(*(uint16_t *)frm);
2166178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2167178354Ssam			vap->iv_stats.is_rx_deauth++;
2168178354Ssam			IEEE80211_NODE_STAT(ni, rx_deauth);
2169178354Ssam		} else {
2170178354Ssam			vap->iv_stats.is_rx_disassoc++;
2171178354Ssam			IEEE80211_NODE_STAT(ni, rx_disassoc);
2172178354Ssam		}
2173178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2174298364Savos		    "recv %s (reason: %d (%s))",
2175298376Savos		    ieee80211_mgt_subtype_name(subtype),
2176298364Savos		    reason, ieee80211_reason_to_string(reason));
2177178354Ssam		if (ni != vap->iv_bss)
2178178354Ssam			ieee80211_node_leave(ni);
2179178354Ssam		break;
2180178354Ssam	}
2181178354Ssam
2182178354Ssam	case IEEE80211_FC0_SUBTYPE_ACTION:
2183218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2184218958Sbschmidt		if (ni == vap->iv_bss) {
2185218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2186218958Sbschmidt			    wh, NULL, "%s", "unknown node");
2187218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
2188218958Sbschmidt		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2189218958Sbschmidt		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2190218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2191218958Sbschmidt			    wh, NULL, "%s", "not for us");
2192218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
2193218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
2194218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2195218927Sbschmidt			    wh, NULL, "wrong state %s",
2196218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
2197178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
2198218958Sbschmidt		} else {
2199218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
2200218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2201218927Sbschmidt		}
2202178354Ssam		break;
2203178354Ssam
2204178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2205178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2206295795Savos	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2207218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
2208218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2209218927Sbschmidt		    wh, NULL, "%s", "not handled");
2210218927Sbschmidt		vap->iv_stats.is_rx_mgtdiscard++;
2211218927Sbschmidt		break;
2212218927Sbschmidt
2213178354Ssam	default:
2214178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2215218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
2216178354Ssam		vap->iv_stats.is_rx_badsubtype++;
2217178354Ssam		break;
2218178354Ssam	}
2219178354Ssam}
2220178354Ssam
2221191546Ssamstatic void
2222191546Ssamhostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2223191546Ssam{
2224191546Ssam	switch (subtype) {
2225191546Ssam	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2226241138Sadrian		ni->ni_vap->iv_recv_pspoll(ni, m);
2227191546Ssam		break;
2228191546Ssam	case IEEE80211_FC0_SUBTYPE_BAR:
2229191546Ssam		ieee80211_recv_bar(ni, m);
2230191546Ssam		break;
2231191546Ssam	}
2232191546Ssam}
2233191546Ssam
2234178354Ssam/*
2235178354Ssam * Process a received ps-poll frame.
2236178354Ssam */
2237241138Sadrianvoid
2238241138Sadrianieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2239178354Ssam{
2240178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2241248069Sadrian	struct ieee80211com *ic = vap->iv_ic;
2242178354Ssam	struct ieee80211_frame_min *wh;
2243178354Ssam	struct mbuf *m;
2244178354Ssam	uint16_t aid;
2245178354Ssam	int qlen;
2246178354Ssam
2247178354Ssam	wh = mtod(m0, struct ieee80211_frame_min *);
2248178354Ssam	if (ni->ni_associd == 0) {
2249178354Ssam		IEEE80211_DISCARD(vap,
2250178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2251178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2252178354Ssam		    "%s", "unassociated station");
2253178354Ssam		vap->iv_stats.is_ps_unassoc++;
2254178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2255178354Ssam			IEEE80211_REASON_NOT_ASSOCED);
2256178354Ssam		return;
2257178354Ssam	}
2258178354Ssam
2259178354Ssam	aid = le16toh(*(uint16_t *)wh->i_dur);
2260178354Ssam	if (aid != ni->ni_associd) {
2261178354Ssam		IEEE80211_DISCARD(vap,
2262178354Ssam		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2263178354Ssam		    (struct ieee80211_frame *) wh, NULL,
2264178354Ssam		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2265178354Ssam		    ni->ni_associd, aid);
2266178354Ssam		vap->iv_stats.is_ps_badaid++;
2267180837Ssam		/*
2268180837Ssam		 * NB: We used to deauth the station but it turns out
2269180837Ssam		 * the Blackberry Curve 8230 (and perhaps other devices)
2270180837Ssam		 * sometimes send the wrong AID when WME is negotiated.
2271180837Ssam		 * Being more lenient here seems ok as we already check
2272180837Ssam		 * the station is associated and we only return frames
2273180837Ssam		 * queued for the station (i.e. we don't use the AID).
2274180837Ssam		 */
2275178354Ssam		return;
2276178354Ssam	}
2277178354Ssam
2278178354Ssam	/* Okay, take the first queued packet and put it out... */
2279184288Ssam	m = ieee80211_node_psq_dequeue(ni, &qlen);
2280178354Ssam	if (m == NULL) {
2281178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2282178354Ssam		    "%s", "recv ps-poll, but queue empty");
2283178354Ssam		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2284178354Ssam		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2285178354Ssam		if (vap->iv_set_tim != NULL)
2286178354Ssam			vap->iv_set_tim(ni, 0);	/* just in case */
2287178354Ssam		return;
2288178354Ssam	}
2289178354Ssam	/*
2290178354Ssam	 * If there are more packets, set the more packets bit
2291178354Ssam	 * in the packet dispatched to the station; otherwise
2292178354Ssam	 * turn off the TIM bit.
2293178354Ssam	 */
2294178354Ssam	if (qlen != 0) {
2295178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2296178354Ssam		    "recv ps-poll, send packet, %u still queued", qlen);
2297178354Ssam		m->m_flags |= M_MORE_DATA;
2298178354Ssam	} else {
2299178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2300178354Ssam		    "%s", "recv ps-poll, send packet, queue empty");
2301178354Ssam		if (vap->iv_set_tim != NULL)
2302178354Ssam			vap->iv_set_tim(ni, 0);
2303178354Ssam	}
2304178354Ssam	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2305184288Ssam
2306245098Sadrian	/*
2307248069Sadrian	 * Do the right thing; if it's an encap'ed frame then
2308289164Sadrian	 * call ieee80211_parent_xmitpkt() else
2309254082Sadrian	 * call ieee80211_vap_xmitpkt().
2310245098Sadrian	 */
2311248069Sadrian	if (m->m_flags & M_ENCAP) {
2312289164Sadrian		(void) ieee80211_parent_xmitpkt(ic, m);
2313248069Sadrian	} else {
2314254082Sadrian		(void) ieee80211_vap_xmitpkt(vap, m);
2315245098Sadrian	}
2316178354Ssam}
2317