ieee80211_hostap.c revision 300232
1176730Sjeff/*-
2176730Sjeff * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3176730Sjeff * All rights reserved.
4176730Sjeff *
5176730Sjeff * Redistribution and use in source and binary forms, with or without
6176730Sjeff * modification, are permitted provided that the following conditions
7176730Sjeff * are met:
8176730Sjeff * 1. Redistributions of source code must retain the above copyright
9176730Sjeff *    notice, this list of conditions and the following disclaimer.
10176730Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11176730Sjeff *    notice, this list of conditions and the following disclaimer in the
12176730Sjeff *    documentation and/or other materials provided with the distribution.
13176730Sjeff *
14176730Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15176730Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16176730Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17176730Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18176730Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19176730Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20176730Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21176730Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22176730Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23176730Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24176730Sjeff */
25176730Sjeff
26176730Sjeff#include <sys/cdefs.h>
27176730Sjeff#ifdef __FreeBSD__
28176730Sjeff__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_hostap.c 300232 2016-05-19 21:08:33Z avos $");
29176730Sjeff#endif
30176730Sjeff
31176730Sjeff/*
32176730Sjeff * IEEE 802.11 HOSTAP mode support.
33176730Sjeff */
34176730Sjeff#include "opt_inet.h"
35176730Sjeff#include "opt_wlan.h"
36176730Sjeff
37176730Sjeff#include <sys/param.h>
38176730Sjeff#include <sys/systm.h>
39176730Sjeff#include <sys/mbuf.h>
40176730Sjeff#include <sys/malloc.h>
41176730Sjeff#include <sys/kernel.h>
42176730Sjeff
43176730Sjeff#include <sys/socket.h>
44176730Sjeff#include <sys/sockio.h>
45176730Sjeff#include <sys/endian.h>
46176730Sjeff#include <sys/errno.h>
47176730Sjeff#include <sys/proc.h>
48176730Sjeff#include <sys/sysctl.h>
49177738Sjeff
50177738Sjeff#include <net/if.h>
51176730Sjeff#include <net/if_var.h>
52176730Sjeff#include <net/if_media.h>
53176730Sjeff#include <net/if_llc.h>
54176730Sjeff#include <net/ethernet.h>
55176730Sjeff
56176730Sjeff#include <net/bpf.h>
57176730Sjeff
58176730Sjeff#include <net80211/ieee80211_var.h>
59176730Sjeff#include <net80211/ieee80211_hostap.h>
60176730Sjeff#include <net80211/ieee80211_input.h>
61176730Sjeff#ifdef IEEE80211_SUPPORT_SUPERG
62176730Sjeff#include <net80211/ieee80211_superg.h>
63176730Sjeff#endif
64176730Sjeff#include <net80211/ieee80211_wds.h>
65176730Sjeff
66176730Sjeff#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
67176730Sjeff
68176730Sjeffstatic	void hostap_vattach(struct ieee80211vap *);
69176730Sjeffstatic	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70176730Sjeffstatic	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
71176730Sjeff	    const struct ieee80211_rx_stats *,
72176730Sjeff	    int rssi, int nf);
73176730Sjeffstatic void hostap_deliver_data(struct ieee80211vap *,
74176730Sjeff	    struct ieee80211_node *, struct mbuf *);
75176730Sjeffstatic void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76176730Sjeff	    int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
77176730Sjeffstatic void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
78176730Sjeff
79176730Sjeffvoid
80176730Sjeffieee80211_hostap_attach(struct ieee80211com *ic)
81176730Sjeff{
82176730Sjeff	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
83176730Sjeff}
84176730Sjeff
85176730Sjeffvoid
86176730Sjeffieee80211_hostap_detach(struct ieee80211com *ic)
87176730Sjeff{
88176730Sjeff}
89176730Sjeff
90176730Sjeffstatic void
91176730Sjeffhostap_vdetach(struct ieee80211vap *vap)
92176730Sjeff{
93176730Sjeff}
94176730Sjeff
95176730Sjeffstatic void
96176730Sjeffhostap_vattach(struct ieee80211vap *vap)
97176730Sjeff{
98176730Sjeff	vap->iv_newstate = hostap_newstate;
99177738Sjeff	vap->iv_input = hostap_input;
100176730Sjeff	vap->iv_recv_mgmt = hostap_recv_mgmt;
101177738Sjeff	vap->iv_recv_ctl = hostap_recv_ctl;
102177738Sjeff	vap->iv_opdetach = hostap_vdetach;
103176730Sjeff	vap->iv_deliver_data = hostap_deliver_data;
104176730Sjeff	vap->iv_recv_pspoll = ieee80211_recv_pspoll;
105176730Sjeff}
106176730Sjeff
107176730Sjeffstatic void
108176730Sjeffsta_disassoc(void *arg, struct ieee80211_node *ni)
109176730Sjeff{
110176730Sjeff	struct ieee80211vap *vap = arg;
111176730Sjeff
112176730Sjeff	if (ni->ni_vap == vap && ni->ni_associd != 0) {
113176730Sjeff		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
114176730Sjeff			IEEE80211_REASON_ASSOC_LEAVE);
115176730Sjeff		ieee80211_node_leave(ni);
116176730Sjeff	}
117176730Sjeff}
118176730Sjeff
119176730Sjeffstatic void
120176730Sjeffsta_csa(void *arg, struct ieee80211_node *ni)
121176730Sjeff{
122176730Sjeff	struct ieee80211vap *vap = arg;
123176730Sjeff
124176730Sjeff	if (ni->ni_vap == vap && ni->ni_associd != 0)
125176730Sjeff		if (ni->ni_inact > vap->iv_inact_init) {
126176730Sjeff			ni->ni_inact = vap->iv_inact_init;
127176730Sjeff			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
128176730Sjeff			    "%s: inact %u", __func__, ni->ni_inact);
129176730Sjeff		}
130176730Sjeff}
131176730Sjeff
132176730Sjeffstatic void
133176730Sjeffsta_drop(void *arg, struct ieee80211_node *ni)
134176730Sjeff{
135176730Sjeff	struct ieee80211vap *vap = arg;
136176730Sjeff
137176730Sjeff	if (ni->ni_vap == vap && ni->ni_associd != 0)
138176730Sjeff		ieee80211_node_leave(ni);
139176730Sjeff}
140176730Sjeff
141176730Sjeff/*
142176730Sjeff * Does a channel change require associated stations to re-associate
143176730Sjeff * so protocol state is correct.  This is used when doing CSA across
144176730Sjeff * bands or similar (e.g. HT -> legacy).
145176730Sjeff */
146176730Sjeffstatic int
147176730Sjeffisbandchange(struct ieee80211com *ic)
148176730Sjeff{
149176730Sjeff	return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
150176730Sjeff	    (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
151176730Sjeff	     IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
152176730Sjeff}
153176730Sjeff
154176730Sjeff/*
155176730Sjeff * IEEE80211_M_HOSTAP vap state machine handler.
156176730Sjeff */
157176730Sjeffstatic int
158176730Sjeffhostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
159176730Sjeff{
160176730Sjeff	struct ieee80211com *ic = vap->iv_ic;
161176730Sjeff	enum ieee80211_state ostate;
162176730Sjeff
163176730Sjeff	IEEE80211_LOCK_ASSERT(ic);
164176730Sjeff
165176730Sjeff	ostate = vap->iv_state;
166176730Sjeff	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
167176730Sjeff	    __func__, ieee80211_state_name[ostate],
168176730Sjeff	    ieee80211_state_name[nstate], arg);
169176730Sjeff	vap->iv_state = nstate;			/* state transition */
170176730Sjeff	if (ostate != IEEE80211_S_SCAN)
171176730Sjeff		ieee80211_cancel_scan(vap);	/* background scan */
172176730Sjeff	switch (nstate) {
173176730Sjeff	case IEEE80211_S_INIT:
174176730Sjeff		switch (ostate) {
175176730Sjeff		case IEEE80211_S_SCAN:
176176730Sjeff			ieee80211_cancel_scan(vap);
177176730Sjeff			break;
178176730Sjeff		case IEEE80211_S_CAC:
179176730Sjeff			ieee80211_dfs_cac_stop(vap);
180176730Sjeff			break;
181176730Sjeff		case IEEE80211_S_RUN:
182176730Sjeff			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
183176730Sjeff			break;
184176730Sjeff		default:
185176730Sjeff			break;
186176730Sjeff		}
187176730Sjeff		if (ostate != IEEE80211_S_INIT) {
188176730Sjeff			/* NB: optimize INIT -> INIT case */
189176730Sjeff			ieee80211_reset_bss(vap);
190176730Sjeff		}
191176730Sjeff		if (vap->iv_auth->ia_detach != NULL)
192176730Sjeff			vap->iv_auth->ia_detach(vap);
193176730Sjeff		break;
194176730Sjeff	case IEEE80211_S_SCAN:
195176730Sjeff		switch (ostate) {
196176811Sjeff		case IEEE80211_S_CSA:
197176811Sjeff		case IEEE80211_S_RUN:
198176730Sjeff			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
199176730Sjeff			/*
200176730Sjeff			 * Clear overlapping BSS state; the beacon frame
201176730Sjeff			 * will be reconstructed on transition to the RUN
202176730Sjeff			 * state and the timeout routines check if the flag
203176730Sjeff			 * is set before doing anything so this is sufficient.
204176811Sjeff			 */
205176811Sjeff			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
206176811Sjeff			ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
207176811Sjeff			/* fall thru... */
208176811Sjeff		case IEEE80211_S_CAC:
209176730Sjeff			/*
210176730Sjeff			 * NB: We may get here because of a manual channel
211176811Sjeff			 *     change in which case we need to stop CAC
212176730Sjeff			 * XXX no need to stop if ostate RUN but it's ok
213176730Sjeff			 */
214176730Sjeff			ieee80211_dfs_cac_stop(vap);
215176730Sjeff			/* fall thru... */
216176730Sjeff		case IEEE80211_S_INIT:
217176730Sjeff			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
218176730Sjeff			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
219176730Sjeff				/*
220176730Sjeff				 * Already have a channel; bypass the
221176730Sjeff				 * scan and startup immediately.
222176730Sjeff				 * ieee80211_create_ibss will call back to
223176730Sjeff				 * move us to RUN state.
224176730Sjeff				 */
225176730Sjeff				ieee80211_create_ibss(vap, vap->iv_des_chan);
226176730Sjeff				break;
227176730Sjeff			}
228176730Sjeff			/*
229176730Sjeff			 * Initiate a scan.  We can come here as a result
230176730Sjeff			 * of an IEEE80211_IOC_SCAN_REQ too in which case
231176730Sjeff			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
232176730Sjeff			 * and the scan request parameters will be present
233176730Sjeff			 * in iv_scanreq.  Otherwise we do the default.
234176730Sjeff			 */
235176730Sjeff			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
236176730Sjeff				ieee80211_check_scan(vap,
237176730Sjeff				    vap->iv_scanreq_flags,
238176730Sjeff				    vap->iv_scanreq_duration,
239176730Sjeff				    vap->iv_scanreq_mindwell,
240176730Sjeff				    vap->iv_scanreq_maxdwell,
241176730Sjeff				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
242176730Sjeff				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
243176730Sjeff			} else
244176730Sjeff				ieee80211_check_scan_current(vap);
245176730Sjeff			break;
246176730Sjeff		case IEEE80211_S_SCAN:
247176730Sjeff			/*
248176730Sjeff			 * A state change requires a reset; scan.
249176730Sjeff			 */
250176730Sjeff			ieee80211_check_scan_current(vap);
251176730Sjeff			break;
252176730Sjeff		default:
253176730Sjeff			break;
254176811Sjeff		}
255176811Sjeff		break;
256176730Sjeff	case IEEE80211_S_CAC:
257176730Sjeff		/*
258176811Sjeff		 * Start CAC on a DFS channel.  We come here when starting
259176730Sjeff		 * a bss on a DFS channel (see ieee80211_create_ibss).
260176730Sjeff		 */
261176730Sjeff		ieee80211_dfs_cac_start(vap);
262176730Sjeff		break;
263176730Sjeff	case IEEE80211_S_RUN:
264176730Sjeff		if (vap->iv_flags & IEEE80211_F_WPA) {
265176730Sjeff			/* XXX validate prerequisites */
266176730Sjeff		}
267176730Sjeff		switch (ostate) {
268176730Sjeff		case IEEE80211_S_INIT:
269176730Sjeff			/*
270176730Sjeff			 * Already have a channel; bypass the
271176730Sjeff			 * scan and startup immediately.
272176730Sjeff			 * Note that ieee80211_create_ibss will call
273176730Sjeff			 * back to do a RUN->RUN state change.
274176730Sjeff			 */
275176730Sjeff			ieee80211_create_ibss(vap,
276176730Sjeff			    ieee80211_ht_adjust_channel(ic,
277176730Sjeff				ic->ic_curchan, vap->iv_flags_ht));
278176730Sjeff			/* NB: iv_bss is changed on return */
279176730Sjeff			break;
280176730Sjeff		case IEEE80211_S_CAC:
281176730Sjeff			/*
282176730Sjeff			 * NB: This is the normal state change when CAC
283176730Sjeff			 * expires and no radar was detected; no need to
284176730Sjeff			 * clear the CAC timer as it's already expired.
285176730Sjeff			 */
286176730Sjeff			/* fall thru... */
287176730Sjeff		case IEEE80211_S_CSA:
288176730Sjeff			/*
289176811Sjeff			 * Shorten inactivity timer of associated stations
290176730Sjeff			 * to weed out sta's that don't follow a CSA.
291176730Sjeff			 */
292176730Sjeff			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
293176730Sjeff			/*
294176730Sjeff			 * Update bss node channel to reflect where
295176811Sjeff			 * we landed after CSA.
296176811Sjeff			 */
297176811Sjeff			ieee80211_node_set_chan(vap->iv_bss,
298176811Sjeff			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
299176811Sjeff				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
300176811Sjeff			/* XXX bypass debug msgs */
301176811Sjeff			break;
302176730Sjeff		case IEEE80211_S_SCAN:
303176730Sjeff		case IEEE80211_S_RUN:
304176730Sjeff#ifdef IEEE80211_DEBUG
305176730Sjeff			if (ieee80211_msg_debug(vap)) {
306176730Sjeff				struct ieee80211_node *ni = vap->iv_bss;
307176730Sjeff				ieee80211_note(vap,
308176730Sjeff				    "synchronized with %s ssid ",
309176730Sjeff				    ether_sprintf(ni->ni_bssid));
310176730Sjeff				ieee80211_print_essid(ni->ni_essid,
311176730Sjeff				    ni->ni_esslen);
312176730Sjeff				/* XXX MCS/HT */
313176730Sjeff				printf(" channel %d start %uMb\n",
314176730Sjeff				    ieee80211_chan2ieee(ic, ic->ic_curchan),
315176730Sjeff				    IEEE80211_RATE2MBS(ni->ni_txrate));
316176730Sjeff			}
317176730Sjeff#endif
318176730Sjeff			break;
319177738Sjeff		default:
320176730Sjeff			break;
321176730Sjeff		}
322176730Sjeff		/*
323176730Sjeff		 * Start/stop the authenticator.  We delay until here
324176730Sjeff		 * to allow configuration to happen out of order.
325176730Sjeff		 */
326176730Sjeff		if (vap->iv_auth->ia_attach != NULL) {
327176730Sjeff			/* XXX check failure */
328176730Sjeff			vap->iv_auth->ia_attach(vap);
329176730Sjeff		} else if (vap->iv_auth->ia_detach != NULL) {
330176730Sjeff			vap->iv_auth->ia_detach(vap);
331176730Sjeff		}
332176730Sjeff		ieee80211_node_authorize(vap->iv_bss);
333176730Sjeff		break;
334176730Sjeff	case IEEE80211_S_CSA:
335176730Sjeff		if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
336177738Sjeff			/*
337176730Sjeff			 * On a ``band change'' silently drop associated
338176730Sjeff			 * stations as they must re-associate before they
339176730Sjeff			 * can pass traffic (as otherwise protocol state
340176730Sjeff			 * such as capabilities and the negotiated rate
341176730Sjeff			 * set may/will be wrong).
342176730Sjeff			 */
343176730Sjeff			ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
344176730Sjeff		}
345176730Sjeff		break;
346176730Sjeff	default:
347176730Sjeff		break;
348176730Sjeff	}
349176730Sjeff	return 0;
350176730Sjeff}
351176730Sjeff
352176730Sjeffstatic void
353176730Sjeffhostap_deliver_data(struct ieee80211vap *vap,
354176730Sjeff	struct ieee80211_node *ni, struct mbuf *m)
355176730Sjeff{
356176730Sjeff	struct ether_header *eh = mtod(m, struct ether_header *);
357176730Sjeff	struct ifnet *ifp = vap->iv_ifp;
358176730Sjeff
359176730Sjeff	/* clear driver/net80211 flags before passing up */
360176730Sjeff	m->m_flags &= ~(M_MCAST | M_BCAST);
361176730Sjeff	m_clrprotoflags(m);
362176730Sjeff
363176730Sjeff	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
364176730Sjeff	    ("gack, opmode %d", vap->iv_opmode));
365176730Sjeff	/*
366176730Sjeff	 * Do accounting.
367176730Sjeff	 */
368176730Sjeff	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
369176730Sjeff	IEEE80211_NODE_STAT(ni, rx_data);
370176730Sjeff	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
371176730Sjeff	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
372176730Sjeff		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
373176730Sjeff		IEEE80211_NODE_STAT(ni, rx_mcast);
374176730Sjeff	} else
375176730Sjeff		IEEE80211_NODE_STAT(ni, rx_ucast);
376176730Sjeff
377176730Sjeff	/* perform as a bridge within the AP */
378176730Sjeff	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
379176730Sjeff		struct mbuf *mcopy = NULL;
380176730Sjeff
381176730Sjeff		if (m->m_flags & M_MCAST) {
382176730Sjeff			mcopy = m_dup(m, M_NOWAIT);
383176730Sjeff			if (mcopy == NULL)
384176730Sjeff				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
385176730Sjeff			else
386176730Sjeff				mcopy->m_flags |= M_MCAST;
387176730Sjeff		} else {
388176730Sjeff			/*
389176730Sjeff			 * Check if the destination is associated with the
390176730Sjeff			 * same vap and authorized to receive traffic.
391176730Sjeff			 * Beware of traffic destined for the vap itself;
392176730Sjeff			 * sending it will not work; just let it be delivered
393176730Sjeff			 * normally.
394176730Sjeff			 */
395176730Sjeff			struct ieee80211_node *sta = ieee80211_find_vap_node(
396176730Sjeff			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
397176730Sjeff			if (sta != NULL) {
398176730Sjeff				if (ieee80211_node_is_authorized(sta)) {
399176730Sjeff					/*
400176730Sjeff					 * Beware of sending to ourself; this
401176730Sjeff					 * needs to happen via the normal
402176730Sjeff					 * input path.
403177738Sjeff					 */
404176730Sjeff					if (sta != vap->iv_bss) {
405176730Sjeff						mcopy = m;
406176730Sjeff						m = NULL;
407176730Sjeff					}
408176730Sjeff				} else {
409176730Sjeff					vap->iv_stats.is_rx_unauth++;
410176730Sjeff					IEEE80211_NODE_STAT(sta, rx_unauth);
411176730Sjeff				}
412176730Sjeff				ieee80211_free_node(sta);
413176730Sjeff			}
414176730Sjeff		}
415176730Sjeff		if (mcopy != NULL && ieee80211_vap_xmitpkt(vap, mcopy) == 0)
416176730Sjeff			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
417176730Sjeff	}
418176730Sjeff	if (m != NULL) {
419176730Sjeff		/*
420176730Sjeff		 * Mark frame as coming from vap's interface.
421176730Sjeff		 */
422176730Sjeff		m->m_pkthdr.rcvif = ifp;
423176730Sjeff		if (m->m_flags & M_MCAST) {
424176730Sjeff			/*
425176730Sjeff			 * Spam DWDS vap's w/ multicast traffic.
426176730Sjeff			 */
427176730Sjeff			/* XXX only if dwds in use? */
428176730Sjeff			ieee80211_dwds_mcast(vap, m);
429176730Sjeff		}
430176730Sjeff		if (ni->ni_vlan != 0) {
431176730Sjeff			/* attach vlan tag */
432176730Sjeff			m->m_pkthdr.ether_vtag = ni->ni_vlan;
433176730Sjeff			m->m_flags |= M_VLANTAG;
434176730Sjeff		}
435176730Sjeff		ifp->if_input(ifp, m);
436176730Sjeff	}
437176730Sjeff}
438176730Sjeff
439176730Sjeff/*
440176730Sjeff * Decide if a received management frame should be
441176811Sjeff * printed when debugging is enabled.  This filters some
442177738Sjeff * of the less interesting frames that come frequently
443176730Sjeff * (e.g. beacons).
444176730Sjeff */
445176730Sjeffstatic __inline int
446176730Sjeffdoprint(struct ieee80211vap *vap, int subtype)
447176730Sjeff{
448176730Sjeff	switch (subtype) {
449176730Sjeff	case IEEE80211_FC0_SUBTYPE_BEACON:
450176730Sjeff		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
451176730Sjeff	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
452176730Sjeff		return 0;
453176730Sjeff	}
454176730Sjeff	return 1;
455176730Sjeff}
456176730Sjeff
457176730Sjeff/*
458176730Sjeff * Process a received frame.  The node associated with the sender
459176730Sjeff * should be supplied.  If nothing was found in the node table then
460176730Sjeff * the caller is assumed to supply a reference to iv_bss instead.
461176730Sjeff * The RSSI and a timestamp are also supplied.  The RSSI data is used
462176730Sjeff * during AP scanning to select a AP to associate with; it can have
463176730Sjeff * any units so long as values have consistent units and higher values
464176811Sjeff * mean ``better signal''.  The receive timestamp is currently not used
465176730Sjeff * by the 802.11 layer.
466176730Sjeff */
467176730Sjeffstatic int
468176730Sjeffhostap_input(struct ieee80211_node *ni, struct mbuf *m,
469176730Sjeff    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
470176730Sjeff{
471176730Sjeff	struct ieee80211vap *vap = ni->ni_vap;
472176730Sjeff	struct ieee80211com *ic = ni->ni_ic;
473176730Sjeff	struct ifnet *ifp = vap->iv_ifp;
474176730Sjeff	struct ieee80211_frame *wh;
475176730Sjeff	struct ieee80211_key *key;
476176730Sjeff	struct ether_header *eh;
477176730Sjeff	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
478176730Sjeff	uint8_t dir, type, subtype, qos;
479176730Sjeff	uint8_t *bssid;
480176730Sjeff
481176730Sjeff	if (m->m_flags & M_AMPDU_MPDU) {
482176730Sjeff		/*
483176730Sjeff		 * Fastpath for A-MPDU reorder q resubmission.  Frames
484176730Sjeff		 * w/ M_AMPDU_MPDU marked have already passed through
485176730Sjeff		 * here but were received out of order and been held on
486176730Sjeff		 * the reorder queue.  When resubmitted they are marked
487176730Sjeff		 * with the M_AMPDU_MPDU flag and we can bypass most of
488176730Sjeff		 * the normal processing.
489176730Sjeff		 */
490176730Sjeff		wh = mtod(m, struct ieee80211_frame *);
491176730Sjeff		type = IEEE80211_FC0_TYPE_DATA;
492176730Sjeff		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
493176730Sjeff		subtype = IEEE80211_FC0_SUBTYPE_QOS;
494176730Sjeff		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
495176730Sjeff		goto resubmit_ampdu;
496176730Sjeff	}
497176811Sjeff
498176811Sjeff	KASSERT(ni != NULL, ("null node"));
499176811Sjeff	ni->ni_inact = ni->ni_inact_reload;
500176811Sjeff
501176811Sjeff	type = -1;			/* undefined */
502176811Sjeff
503176811Sjeff	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
504176811Sjeff		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
505176811Sjeff		    ni->ni_macaddr, NULL,
506176811Sjeff		    "too short (1): len %u", m->m_pkthdr.len);
507176811Sjeff		vap->iv_stats.is_rx_tooshort++;
508176811Sjeff		goto out;
509176811Sjeff	}
510176811Sjeff	/*
511176811Sjeff	 * Bit of a cheat here, we use a pointer for a 3-address
512177738Sjeff	 * frame format but don't reference fields past outside
513176811Sjeff	 * ieee80211_frame_min w/o first validating the data is
514176811Sjeff	 * present.
515176811Sjeff	 */
516176811Sjeff	wh = mtod(m, struct ieee80211_frame *);
517176811Sjeff
518176811Sjeff	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
519176811Sjeff	    IEEE80211_FC0_VERSION_0) {
520177738Sjeff		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
521176811Sjeff		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
522176811Sjeff		    wh->i_fc[0], wh->i_fc[1]);
523176811Sjeff		vap->iv_stats.is_rx_badversion++;
524176811Sjeff		goto err;
525176811Sjeff	}
526176811Sjeff
527176811Sjeff	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
528177368Sjeff	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
529177368Sjeff	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
530176730Sjeff	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
531176730Sjeff		if (dir != IEEE80211_FC1_DIR_NODS)
532176730Sjeff			bssid = wh->i_addr1;
533176730Sjeff		else if (type == IEEE80211_FC0_TYPE_CTL)
534176730Sjeff			bssid = wh->i_addr1;
535176730Sjeff		else {
536176730Sjeff			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
537176730Sjeff				IEEE80211_DISCARD_MAC(vap,
538176730Sjeff				    IEEE80211_MSG_ANY, ni->ni_macaddr,
539176730Sjeff				    NULL, "too short (2): len %u",
540176730Sjeff				    m->m_pkthdr.len);
541176730Sjeff				vap->iv_stats.is_rx_tooshort++;
542176730Sjeff				goto out;
543176730Sjeff			}
544176730Sjeff			bssid = wh->i_addr3;
545176730Sjeff		}
546176730Sjeff		/*
547176730Sjeff		 * Validate the bssid.
548176730Sjeff		 */
549176730Sjeff		if (!(type == IEEE80211_FC0_TYPE_MGT &&
550176730Sjeff		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
551176730Sjeff		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
552176730Sjeff		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
553176730Sjeff			/* not interested in */
554176730Sjeff			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
555176730Sjeff			    bssid, NULL, "%s", "not to bss");
556176730Sjeff			vap->iv_stats.is_rx_wrongbss++;
557176730Sjeff			goto out;
558176730Sjeff		}
559176730Sjeff
560176730Sjeff		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
561176730Sjeff		ni->ni_noise = nf;
562176811Sjeff		if (IEEE80211_HAS_SEQ(type, subtype)) {
563176730Sjeff			uint8_t tid = ieee80211_gettid(wh);
564176730Sjeff			if (IEEE80211_QOS_HAS_SEQ(wh) &&
565176730Sjeff			    TID_TO_WME_AC(tid) >= WME_AC_VI)
566176730Sjeff				ic->ic_wme.wme_hipri_traffic++;
567176730Sjeff			if (! ieee80211_check_rxseq(ni, wh, bssid))
568176730Sjeff				goto out;
569176730Sjeff		}
570176730Sjeff	}
571176730Sjeff
572176730Sjeff	switch (type) {
573176730Sjeff	case IEEE80211_FC0_TYPE_DATA:
574176730Sjeff		hdrspace = ieee80211_hdrspace(ic, wh);
575176730Sjeff		if (m->m_len < hdrspace &&
576176730Sjeff		    (m = m_pullup(m, hdrspace)) == NULL) {
577177738Sjeff			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
578176730Sjeff			    ni->ni_macaddr, NULL,
579176730Sjeff			    "data too short: expecting %u", hdrspace);
580176730Sjeff			vap->iv_stats.is_rx_tooshort++;
581176730Sjeff			goto out;		/* XXX */
582176730Sjeff		}
583176730Sjeff		if (!(dir == IEEE80211_FC1_DIR_TODS ||
584176730Sjeff		     (dir == IEEE80211_FC1_DIR_DSTODS &&
585176730Sjeff		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
586176730Sjeff			if (dir != IEEE80211_FC1_DIR_DSTODS) {
587176821Sjeff				IEEE80211_DISCARD(vap,
588176730Sjeff				    IEEE80211_MSG_INPUT, wh, "data",
589176730Sjeff				    "incorrect dir 0x%x", dir);
590177738Sjeff			} else {
591176730Sjeff				IEEE80211_DISCARD(vap,
592177738Sjeff				    IEEE80211_MSG_INPUT |
593176730Sjeff				    IEEE80211_MSG_WDS, wh,
594177738Sjeff				    "4-address data",
595176730Sjeff				    "%s", "DWDS not enabled");
596176730Sjeff			}
597176730Sjeff			vap->iv_stats.is_rx_wrongdir++;
598176730Sjeff			goto out;
599176730Sjeff		}
600176730Sjeff		/* check if source STA is associated */
601177738Sjeff		if (ni == vap->iv_bss) {
602177738Sjeff			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
603176730Sjeff			    wh, "data", "%s", "unknown src");
604176730Sjeff			ieee80211_send_error(ni, wh->i_addr2,
605176730Sjeff			    IEEE80211_FC0_SUBTYPE_DEAUTH,
606176730Sjeff			    IEEE80211_REASON_NOT_AUTHED);
607176730Sjeff			vap->iv_stats.is_rx_notassoc++;
608176730Sjeff			goto err;
609176730Sjeff		}
610176730Sjeff		if (ni->ni_associd == 0) {
611176730Sjeff			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
612176730Sjeff			    wh, "data", "%s", "unassoc src");
613176730Sjeff			IEEE80211_SEND_MGMT(ni,
614176730Sjeff			    IEEE80211_FC0_SUBTYPE_DISASSOC,
615176730Sjeff			    IEEE80211_REASON_NOT_ASSOCED);
616176730Sjeff			vap->iv_stats.is_rx_notassoc++;
617176730Sjeff			goto err;
618176730Sjeff		}
619176730Sjeff
620176730Sjeff		/*
621176730Sjeff		 * Check for power save state change.
622176730Sjeff		 * XXX out-of-order A-MPDU frames?
623176730Sjeff		 */
624176730Sjeff		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
625176730Sjeff		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
626176730Sjeff			vap->iv_node_ps(ni,
627176730Sjeff				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
628176730Sjeff		/*
629176730Sjeff		 * For 4-address packets handle WDS discovery
630176730Sjeff		 * notifications.  Once a WDS link is setup frames
631176730Sjeff		 * are just delivered to the WDS vap (see below).
632176730Sjeff		 */
633176730Sjeff		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
634176730Sjeff			if (!ieee80211_node_is_authorized(ni)) {
635176730Sjeff				IEEE80211_DISCARD(vap,
636176730Sjeff				    IEEE80211_MSG_INPUT |
637176730Sjeff				    IEEE80211_MSG_WDS, wh,
638176730Sjeff				    "4-address data",
639176730Sjeff				    "%s", "unauthorized port");
640177738Sjeff				vap->iv_stats.is_rx_unauth++;
641176730Sjeff				IEEE80211_NODE_STAT(ni, rx_unauth);
642176730Sjeff				goto err;
643176730Sjeff			}
644176730Sjeff			ieee80211_dwds_discover(ni, m);
645176730Sjeff			return type;
646176730Sjeff		}
647176730Sjeff
648176730Sjeff		/*
649176730Sjeff		 * Handle A-MPDU re-ordering.  If the frame is to be
650176730Sjeff		 * processed directly then ieee80211_ampdu_reorder
651176730Sjeff		 * will return 0; otherwise it has consumed the mbuf
652176730Sjeff		 * and we should do nothing more with it.
653176730Sjeff		 */
654176730Sjeff		if ((m->m_flags & M_AMPDU) &&
655176730Sjeff		    ieee80211_ampdu_reorder(ni, m) != 0) {
656176730Sjeff			m = NULL;
657176730Sjeff			goto out;
658176730Sjeff		}
659176730Sjeff	resubmit_ampdu:
660176730Sjeff
661176730Sjeff		/*
662176730Sjeff		 * Handle privacy requirements.  Note that we
663176730Sjeff		 * must not be preempted from here until after
664176730Sjeff		 * we (potentially) call ieee80211_crypto_demic;
665176730Sjeff		 * otherwise we may violate assumptions in the
666176730Sjeff		 * crypto cipher modules used to do delayed update
667176730Sjeff		 * of replay sequence numbers.
668176730Sjeff		 */
669176730Sjeff		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
670176730Sjeff			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
671176730Sjeff				/*
672176730Sjeff				 * Discard encrypted frames when privacy is off.
673176730Sjeff				 */
674176730Sjeff				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
675176730Sjeff				    wh, "WEP", "%s", "PRIVACY off");
676176730Sjeff				vap->iv_stats.is_rx_noprivacy++;
677176730Sjeff				IEEE80211_NODE_STAT(ni, rx_noprivacy);
678176730Sjeff				goto out;
679176730Sjeff			}
680176730Sjeff			key = ieee80211_crypto_decap(ni, m, hdrspace);
681176730Sjeff			if (key == NULL) {
682176730Sjeff				/* NB: stats+msgs handled in crypto_decap */
683176730Sjeff				IEEE80211_NODE_STAT(ni, rx_wepfail);
684176730Sjeff				goto out;
685176730Sjeff			}
686176730Sjeff			wh = mtod(m, struct ieee80211_frame *);
687176730Sjeff			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
688176730Sjeff		} else {
689177738Sjeff			/* XXX M_WEP and IEEE80211_F_PRIVACY */
690176730Sjeff			key = NULL;
691176730Sjeff		}
692176730Sjeff
693176730Sjeff		/*
694176730Sjeff		 * Save QoS bits for use below--before we strip the header.
695177738Sjeff		 */
696176730Sjeff		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
697177738Sjeff			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
698176730Sjeff			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
699176730Sjeff			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
700176730Sjeff		} else
701176730Sjeff			qos = 0;
702176730Sjeff
703176730Sjeff		/*
704176730Sjeff		 * Next up, any fragmentation.
705176730Sjeff		 */
706176730Sjeff		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
707176730Sjeff			m = ieee80211_defrag(ni, m, hdrspace);
708176730Sjeff			if (m == NULL) {
709176730Sjeff				/* Fragment dropped or frame not complete yet */
710176730Sjeff				goto out;
711176730Sjeff			}
712176730Sjeff		}
713176730Sjeff		wh = NULL;		/* no longer valid, catch any uses */
714176730Sjeff
715176730Sjeff		/*
716176730Sjeff		 * Next strip any MSDU crypto bits.
717176730Sjeff		 */
718176730Sjeff		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
719176730Sjeff			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
720176730Sjeff			    ni->ni_macaddr, "data", "%s", "demic error");
721176730Sjeff			vap->iv_stats.is_rx_demicfail++;
722176730Sjeff			IEEE80211_NODE_STAT(ni, rx_demicfail);
723176730Sjeff			goto out;
724176730Sjeff		}
725176730Sjeff		/* copy to listener after decrypt */
726176730Sjeff		if (ieee80211_radiotap_active_vap(vap))
727176730Sjeff			ieee80211_radiotap_rx(vap, m);
728176730Sjeff		need_tap = 0;
729176730Sjeff		/*
730176730Sjeff		 * Finally, strip the 802.11 header.
731176730Sjeff		 */
732176730Sjeff		m = ieee80211_decap(vap, m, hdrspace);
733176730Sjeff		if (m == NULL) {
734176730Sjeff			/* XXX mask bit to check for both */
735176730Sjeff			/* don't count Null data frames as errors */
736176730Sjeff			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
737176730Sjeff			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
738176730Sjeff				goto out;
739176730Sjeff			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
740176730Sjeff			    ni->ni_macaddr, "data", "%s", "decap error");
741176730Sjeff			vap->iv_stats.is_rx_decap++;
742176730Sjeff			IEEE80211_NODE_STAT(ni, rx_decap);
743176730Sjeff			goto err;
744176730Sjeff		}
745176730Sjeff		eh = mtod(m, struct ether_header *);
746176730Sjeff		if (!ieee80211_node_is_authorized(ni)) {
747176730Sjeff			/*
748176730Sjeff			 * Deny any non-PAE frames received prior to
749176730Sjeff			 * authorization.  For open/shared-key
750176730Sjeff			 * authentication the port is mark authorized
751176730Sjeff			 * after authentication completes.  For 802.1x
752176730Sjeff			 * the port is not marked authorized by the
753176730Sjeff			 * authenticator until the handshake has completed.
754177738Sjeff			 */
755176730Sjeff			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
756176730Sjeff				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
757176730Sjeff				    eh->ether_shost, "data",
758176730Sjeff				    "unauthorized port: ether type 0x%x len %u",
759176730Sjeff				    eh->ether_type, m->m_pkthdr.len);
760176730Sjeff				vap->iv_stats.is_rx_unauth++;
761176730Sjeff				IEEE80211_NODE_STAT(ni, rx_unauth);
762176730Sjeff				goto err;
763177738Sjeff			}
764176730Sjeff		} else {
765176730Sjeff			/*
766176730Sjeff			 * When denying unencrypted frames, discard
767176730Sjeff			 * any non-PAE frames received without encryption.
768176730Sjeff			 */
769176730Sjeff			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
770176730Sjeff			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
771176730Sjeff			    eh->ether_type != htons(ETHERTYPE_PAE)) {
772176730Sjeff				/*
773176730Sjeff				 * Drop unencrypted frames.
774176730Sjeff				 */
775176730Sjeff				vap->iv_stats.is_rx_unencrypted++;
776176730Sjeff				IEEE80211_NODE_STAT(ni, rx_unencrypted);
777176730Sjeff				goto out;
778176730Sjeff			}
779176730Sjeff		}
780176730Sjeff		/* XXX require HT? */
781176730Sjeff		if (qos & IEEE80211_QOS_AMSDU) {
782177597Sru			m = ieee80211_decap_amsdu(ni, m);
783177597Sru			if (m == NULL)
784177597Sru				return IEEE80211_FC0_TYPE_DATA;
785177597Sru		} else {
786177597Sru#ifdef IEEE80211_SUPPORT_SUPERG
787176730Sjeff			m = ieee80211_decap_fastframe(vap, ni, m);
788176730Sjeff			if (m == NULL)
789176730Sjeff				return IEEE80211_FC0_TYPE_DATA;
790176730Sjeff#endif
791176730Sjeff		}
792176730Sjeff		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
793176730Sjeff			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
794176730Sjeff		else
795176730Sjeff			hostap_deliver_data(vap, ni, m);
796176730Sjeff		return IEEE80211_FC0_TYPE_DATA;
797176730Sjeff
798177597Sru	case IEEE80211_FC0_TYPE_MGT:
799176730Sjeff		vap->iv_stats.is_rx_mgmt++;
800176811Sjeff		IEEE80211_NODE_STAT(ni, rx_mgmt);
801176811Sjeff		if (dir != IEEE80211_FC1_DIR_NODS) {
802176730Sjeff			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
803176811Sjeff			    wh, "mgt", "incorrect dir 0x%x", dir);
804176730Sjeff			vap->iv_stats.is_rx_wrongdir++;
805176730Sjeff			goto err;
806176730Sjeff		}
807176730Sjeff		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
808176730Sjeff			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
809176730Sjeff			    ni->ni_macaddr, "mgt", "too short: len %u",
810176730Sjeff			    m->m_pkthdr.len);
811176730Sjeff			vap->iv_stats.is_rx_tooshort++;
812176730Sjeff			goto out;
813176730Sjeff		}
814176730Sjeff		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
815176730Sjeff			/* ensure return frames are unicast */
816176730Sjeff			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
817176730Sjeff			    wh, NULL, "source is multicast: %s",
818176730Sjeff			    ether_sprintf(wh->i_addr2));
819176730Sjeff			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
820176730Sjeff			goto out;
821176730Sjeff		}
822177738Sjeff#ifdef IEEE80211_DEBUG
823176730Sjeff		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
824177738Sjeff		    ieee80211_msg_dumppkts(vap)) {
825176730Sjeff			if_printf(ifp, "received %s from %s rssi %d\n",
826176730Sjeff			    ieee80211_mgt_subtype_name(subtype),
827176730Sjeff			    ether_sprintf(wh->i_addr2), rssi);
828176730Sjeff		}
829176730Sjeff#endif
830176730Sjeff		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
831176730Sjeff			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
832176730Sjeff				/*
833176730Sjeff				 * Only shared key auth frames with a challenge
834176730Sjeff				 * should be encrypted, discard all others.
835176730Sjeff				 */
836176730Sjeff				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
837176730Sjeff				    wh, NULL,
838176730Sjeff				    "%s", "WEP set but not permitted");
839176730Sjeff				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
840176730Sjeff				goto out;
841176730Sjeff			}
842176730Sjeff			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
843176730Sjeff				/*
844176730Sjeff				 * Discard encrypted frames when privacy is off.
845176730Sjeff				 */
846176730Sjeff				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
847176730Sjeff				    wh, NULL, "%s", "WEP set but PRIVACY off");
848176730Sjeff				vap->iv_stats.is_rx_noprivacy++;
849176730Sjeff				goto out;
850176730Sjeff			}
851176730Sjeff			hdrspace = ieee80211_hdrspace(ic, wh);
852176730Sjeff			key = ieee80211_crypto_decap(ni, m, hdrspace);
853176730Sjeff			if (key == NULL) {
854176730Sjeff				/* NB: stats+msgs handled in crypto_decap */
855176730Sjeff				goto out;
856176730Sjeff			}
857176730Sjeff			wh = mtod(m, struct ieee80211_frame *);
858176730Sjeff			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
859176730Sjeff		}
860176730Sjeff		/*
861176730Sjeff		 * Pass the packet to radiotap before calling iv_recv_mgmt().
862176730Sjeff		 * Otherwise iv_recv_mgmt() might pass another packet to
863176730Sjeff		 * radiotap, resulting in out of order packet captures.
864176730Sjeff		 */
865177597Sru		if (ieee80211_radiotap_active_vap(vap))
866177597Sru			ieee80211_radiotap_rx(vap, m);
867177597Sru		need_tap = 0;
868177597Sru		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
869176730Sjeff		goto out;
870176730Sjeff
871176730Sjeff	case IEEE80211_FC0_TYPE_CTL:
872176730Sjeff		vap->iv_stats.is_rx_ctl++;
873176730Sjeff		IEEE80211_NODE_STAT(ni, rx_ctrl);
874176730Sjeff		vap->iv_recv_ctl(ni, m, subtype);
875176730Sjeff		goto out;
876176730Sjeff	default:
877176730Sjeff		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
878176730Sjeff		    wh, "bad", "frame type 0x%x", type);
879176730Sjeff		/* should not come here */
880176730Sjeff		break;
881176811Sjeff	}
882176811Sjefferr:
883176730Sjeff	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
884176811Sjeffout:
885176811Sjeff	if (m != NULL) {
886176730Sjeff		if (need_tap && ieee80211_radiotap_active_vap(vap))
887176730Sjeff			ieee80211_radiotap_rx(vap, m);
888176811Sjeff		m_freem(m);
889176811Sjeff	}
890176811Sjeff	return type;
891176811Sjeff}
892176811Sjeff
893176811Sjeffstatic void
894176811Sjeffhostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
895176811Sjeff    int rssi, int nf, uint16_t seq, uint16_t status)
896176811Sjeff{
897176811Sjeff	struct ieee80211vap *vap = ni->ni_vap;
898176811Sjeff
899176811Sjeff	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
900176811Sjeff
901176811Sjeff	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
902176811Sjeff		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
903176811Sjeff		    ni->ni_macaddr, "open auth",
904176811Sjeff		    "bad sta auth mode %u", ni->ni_authmode);
905176730Sjeff		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
906176730Sjeff		/*
907176730Sjeff		 * Clear any challenge text that may be there if
908176730Sjeff		 * a previous shared key auth failed and then an
909176730Sjeff		 * open auth is attempted.
910176730Sjeff		 */
911176730Sjeff		if (ni->ni_challenge != NULL) {
912176730Sjeff			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
913176730Sjeff			ni->ni_challenge = NULL;
914176730Sjeff		}
915176730Sjeff		/* XXX hack to workaround calling convention */
916176730Sjeff		ieee80211_send_error(ni, wh->i_addr2,
917176880Sjeff		    IEEE80211_FC0_SUBTYPE_AUTH,
918176730Sjeff		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
919176730Sjeff		return;
920176730Sjeff	}
921176730Sjeff	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
922176730Sjeff		vap->iv_stats.is_rx_bad_auth++;
923177738Sjeff		return;
924176730Sjeff	}
925177738Sjeff	/* always accept open authentication requests */
926176730Sjeff	if (ni == vap->iv_bss) {
927176730Sjeff		ni = ieee80211_dup_bss(vap, wh->i_addr2);
928176730Sjeff		if (ni == NULL)
929176730Sjeff			return;
930176730Sjeff	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
931176730Sjeff		(void) ieee80211_ref_node(ni);
932176730Sjeff	/*
933176730Sjeff	 * Mark the node as referenced to reflect that it's
934176730Sjeff	 * reference count has been bumped to insure it remains
935176730Sjeff	 * after the transaction completes.
936176730Sjeff	 */
937176730Sjeff	ni->ni_flags |= IEEE80211_NODE_AREF;
938176730Sjeff	/*
939176730Sjeff	 * Mark the node as requiring a valid association id
940176730Sjeff	 * before outbound traffic is permitted.
941176730Sjeff	 */
942176730Sjeff	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
943176730Sjeff
944176730Sjeff	if (vap->iv_acl != NULL &&
945176730Sjeff	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
946176730Sjeff		/*
947176730Sjeff		 * When the ACL policy is set to RADIUS we defer the
948176730Sjeff		 * authorization to a user agent.  Dispatch an event,
949176730Sjeff		 * a subsequent MLME call will decide the fate of the
950176730Sjeff		 * station.  If the user agent is not present then the
951176730Sjeff		 * node will be reclaimed due to inactivity.
952176730Sjeff		 */
953176730Sjeff		IEEE80211_NOTE_MAC(vap,
954176730Sjeff		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
955176730Sjeff		    "%s", "station authentication defered (radius acl)");
956176730Sjeff		ieee80211_notify_node_auth(ni);
957176730Sjeff	} else {
958176730Sjeff		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
959		IEEE80211_NOTE_MAC(vap,
960		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
961		    "%s", "station authenticated (open)");
962		/*
963		 * When 802.1x is not in use mark the port
964		 * authorized at this point so traffic can flow.
965		 */
966		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
967			ieee80211_node_authorize(ni);
968	}
969}
970
971static void
972hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
973    uint8_t *frm, uint8_t *efrm, int rssi, int nf,
974    uint16_t seq, uint16_t status)
975{
976	struct ieee80211vap *vap = ni->ni_vap;
977	uint8_t *challenge;
978	int allocbs, estatus;
979
980	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
981
982	/*
983	 * NB: this can happen as we allow pre-shared key
984	 * authentication to be enabled w/o wep being turned
985	 * on so that configuration of these can be done
986	 * in any order.  It may be better to enforce the
987	 * ordering in which case this check would just be
988	 * for sanity/consistency.
989	 */
990	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
991		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
992		    ni->ni_macaddr, "shared key auth",
993		    "%s", " PRIVACY is disabled");
994		estatus = IEEE80211_STATUS_ALG;
995		goto bad;
996	}
997	/*
998	 * Pre-shared key authentication is evil; accept
999	 * it only if explicitly configured (it is supported
1000	 * mainly for compatibility with clients like Mac OS X).
1001	 */
1002	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1003	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1004		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1005		    ni->ni_macaddr, "shared key auth",
1006		    "bad sta auth mode %u", ni->ni_authmode);
1007		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1008		estatus = IEEE80211_STATUS_ALG;
1009		goto bad;
1010	}
1011
1012	challenge = NULL;
1013	if (frm + 1 < efrm) {
1014		if ((frm[1] + 2) > (efrm - frm)) {
1015			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1016			    ni->ni_macaddr, "shared key auth",
1017			    "ie %d/%d too long",
1018			    frm[0], (frm[1] + 2) - (efrm - frm));
1019			vap->iv_stats.is_rx_bad_auth++;
1020			estatus = IEEE80211_STATUS_CHALLENGE;
1021			goto bad;
1022		}
1023		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1024			challenge = frm;
1025		frm += frm[1] + 2;
1026	}
1027	switch (seq) {
1028	case IEEE80211_AUTH_SHARED_CHALLENGE:
1029	case IEEE80211_AUTH_SHARED_RESPONSE:
1030		if (challenge == NULL) {
1031			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1032			    ni->ni_macaddr, "shared key auth",
1033			    "%s", "no challenge");
1034			vap->iv_stats.is_rx_bad_auth++;
1035			estatus = IEEE80211_STATUS_CHALLENGE;
1036			goto bad;
1037		}
1038		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1039			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1040			    ni->ni_macaddr, "shared key auth",
1041			    "bad challenge len %d", challenge[1]);
1042			vap->iv_stats.is_rx_bad_auth++;
1043			estatus = IEEE80211_STATUS_CHALLENGE;
1044			goto bad;
1045		}
1046	default:
1047		break;
1048	}
1049	switch (seq) {
1050	case IEEE80211_AUTH_SHARED_REQUEST:
1051		if (ni == vap->iv_bss) {
1052			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1053			if (ni == NULL) {
1054				/* NB: no way to return an error */
1055				return;
1056			}
1057			allocbs = 1;
1058		} else {
1059			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1060				(void) ieee80211_ref_node(ni);
1061			allocbs = 0;
1062		}
1063		/*
1064		 * Mark the node as referenced to reflect that it's
1065		 * reference count has been bumped to insure it remains
1066		 * after the transaction completes.
1067		 */
1068		ni->ni_flags |= IEEE80211_NODE_AREF;
1069		/*
1070		 * Mark the node as requiring a valid association id
1071		 * before outbound traffic is permitted.
1072		 */
1073		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1074		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1075		ni->ni_noise = nf;
1076		if (!ieee80211_alloc_challenge(ni)) {
1077			/* NB: don't return error so they rexmit */
1078			return;
1079		}
1080		get_random_bytes(ni->ni_challenge,
1081			IEEE80211_CHALLENGE_LEN);
1082		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1083		    ni, "shared key %sauth request", allocbs ? "" : "re");
1084		/*
1085		 * When the ACL policy is set to RADIUS we defer the
1086		 * authorization to a user agent.  Dispatch an event,
1087		 * a subsequent MLME call will decide the fate of the
1088		 * station.  If the user agent is not present then the
1089		 * node will be reclaimed due to inactivity.
1090		 */
1091		if (vap->iv_acl != NULL &&
1092		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1093			IEEE80211_NOTE_MAC(vap,
1094			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1095			    ni->ni_macaddr,
1096			    "%s", "station authentication defered (radius acl)");
1097			ieee80211_notify_node_auth(ni);
1098			return;
1099		}
1100		break;
1101	case IEEE80211_AUTH_SHARED_RESPONSE:
1102		if (ni == vap->iv_bss) {
1103			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1104			    ni->ni_macaddr, "shared key response",
1105			    "%s", "unknown station");
1106			/* NB: don't send a response */
1107			return;
1108		}
1109		if (ni->ni_challenge == NULL) {
1110			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1111			    ni->ni_macaddr, "shared key response",
1112			    "%s", "no challenge recorded");
1113			vap->iv_stats.is_rx_bad_auth++;
1114			estatus = IEEE80211_STATUS_CHALLENGE;
1115			goto bad;
1116		}
1117		if (memcmp(ni->ni_challenge, &challenge[2],
1118			   challenge[1]) != 0) {
1119			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1120			    ni->ni_macaddr, "shared key response",
1121			    "%s", "challenge mismatch");
1122			vap->iv_stats.is_rx_auth_fail++;
1123			estatus = IEEE80211_STATUS_CHALLENGE;
1124			goto bad;
1125		}
1126		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1127		    ni, "%s", "station authenticated (shared key)");
1128		ieee80211_node_authorize(ni);
1129		break;
1130	default:
1131		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1132		    ni->ni_macaddr, "shared key auth",
1133		    "bad seq %d", seq);
1134		vap->iv_stats.is_rx_bad_auth++;
1135		estatus = IEEE80211_STATUS_SEQUENCE;
1136		goto bad;
1137	}
1138	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1139	return;
1140bad:
1141	/*
1142	 * Send an error response; but only when operating as an AP.
1143	 */
1144	/* XXX hack to workaround calling convention */
1145	ieee80211_send_error(ni, wh->i_addr2,
1146	    IEEE80211_FC0_SUBTYPE_AUTH,
1147	    (seq + 1) | (estatus<<16));
1148}
1149
1150/*
1151 * Convert a WPA cipher selector OUI to an internal
1152 * cipher algorithm.  Where appropriate we also
1153 * record any key length.
1154 */
1155static int
1156wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1157{
1158#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1159	uint32_t w = le32dec(sel);
1160
1161	switch (w) {
1162	case WPA_SEL(WPA_CSE_NULL):
1163		return IEEE80211_CIPHER_NONE;
1164	case WPA_SEL(WPA_CSE_WEP40):
1165		if (keylen)
1166			*keylen = 40 / NBBY;
1167		return IEEE80211_CIPHER_WEP;
1168	case WPA_SEL(WPA_CSE_WEP104):
1169		if (keylen)
1170			*keylen = 104 / NBBY;
1171		return IEEE80211_CIPHER_WEP;
1172	case WPA_SEL(WPA_CSE_TKIP):
1173		return IEEE80211_CIPHER_TKIP;
1174	case WPA_SEL(WPA_CSE_CCMP):
1175		return IEEE80211_CIPHER_AES_CCM;
1176	}
1177	return 32;		/* NB: so 1<< is discarded */
1178#undef WPA_SEL
1179}
1180
1181/*
1182 * Convert a WPA key management/authentication algorithm
1183 * to an internal code.
1184 */
1185static int
1186wpa_keymgmt(const uint8_t *sel)
1187{
1188#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1189	uint32_t w = le32dec(sel);
1190
1191	switch (w) {
1192	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1193		return WPA_ASE_8021X_UNSPEC;
1194	case WPA_SEL(WPA_ASE_8021X_PSK):
1195		return WPA_ASE_8021X_PSK;
1196	case WPA_SEL(WPA_ASE_NONE):
1197		return WPA_ASE_NONE;
1198	}
1199	return 0;		/* NB: so is discarded */
1200#undef WPA_SEL
1201}
1202
1203/*
1204 * Parse a WPA information element to collect parameters.
1205 * Note that we do not validate security parameters; that
1206 * is handled by the authenticator; the parsing done here
1207 * is just for internal use in making operational decisions.
1208 */
1209static int
1210ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1211	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1212{
1213	uint8_t len = frm[1];
1214	uint32_t w;
1215	int n;
1216
1217	/*
1218	 * Check the length once for fixed parts: OUI, type,
1219	 * version, mcast cipher, and 2 selector counts.
1220	 * Other, variable-length data, must be checked separately.
1221	 */
1222	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1223		IEEE80211_DISCARD_IE(vap,
1224		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1225		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1226		return IEEE80211_REASON_IE_INVALID;
1227	}
1228	if (len < 14) {
1229		IEEE80211_DISCARD_IE(vap,
1230		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1231		    wh, "WPA", "too short, len %u", len);
1232		return IEEE80211_REASON_IE_INVALID;
1233	}
1234	frm += 6, len -= 4;		/* NB: len is payload only */
1235	/* NB: iswpaoui already validated the OUI and type */
1236	w = le16dec(frm);
1237	if (w != WPA_VERSION) {
1238		IEEE80211_DISCARD_IE(vap,
1239		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1240		    wh, "WPA", "bad version %u", w);
1241		return IEEE80211_REASON_IE_INVALID;
1242	}
1243	frm += 2, len -= 2;
1244
1245	memset(rsn, 0, sizeof(*rsn));
1246
1247	/* multicast/group cipher */
1248	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1249	frm += 4, len -= 4;
1250
1251	/* unicast ciphers */
1252	n = le16dec(frm);
1253	frm += 2, len -= 2;
1254	if (len < n*4+2) {
1255		IEEE80211_DISCARD_IE(vap,
1256		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1257		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1258		    len, n);
1259		return IEEE80211_REASON_IE_INVALID;
1260	}
1261	w = 0;
1262	for (; n > 0; n--) {
1263		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1264		frm += 4, len -= 4;
1265	}
1266	if (w & (1<<IEEE80211_CIPHER_TKIP))
1267		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1268	else
1269		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1270
1271	/* key management algorithms */
1272	n = le16dec(frm);
1273	frm += 2, len -= 2;
1274	if (len < n*4) {
1275		IEEE80211_DISCARD_IE(vap,
1276		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1277		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1278		    len, n);
1279		return IEEE80211_REASON_IE_INVALID;
1280	}
1281	w = 0;
1282	for (; n > 0; n--) {
1283		w |= wpa_keymgmt(frm);
1284		frm += 4, len -= 4;
1285	}
1286	if (w & WPA_ASE_8021X_UNSPEC)
1287		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1288	else
1289		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1290
1291	if (len > 2)		/* optional capabilities */
1292		rsn->rsn_caps = le16dec(frm);
1293
1294	return 0;
1295}
1296
1297/*
1298 * Convert an RSN cipher selector OUI to an internal
1299 * cipher algorithm.  Where appropriate we also
1300 * record any key length.
1301 */
1302static int
1303rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1304{
1305#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1306	uint32_t w = le32dec(sel);
1307
1308	switch (w) {
1309	case RSN_SEL(RSN_CSE_NULL):
1310		return IEEE80211_CIPHER_NONE;
1311	case RSN_SEL(RSN_CSE_WEP40):
1312		if (keylen)
1313			*keylen = 40 / NBBY;
1314		return IEEE80211_CIPHER_WEP;
1315	case RSN_SEL(RSN_CSE_WEP104):
1316		if (keylen)
1317			*keylen = 104 / NBBY;
1318		return IEEE80211_CIPHER_WEP;
1319	case RSN_SEL(RSN_CSE_TKIP):
1320		return IEEE80211_CIPHER_TKIP;
1321	case RSN_SEL(RSN_CSE_CCMP):
1322		return IEEE80211_CIPHER_AES_CCM;
1323	case RSN_SEL(RSN_CSE_WRAP):
1324		return IEEE80211_CIPHER_AES_OCB;
1325	}
1326	return 32;		/* NB: so 1<< is discarded */
1327#undef WPA_SEL
1328}
1329
1330/*
1331 * Convert an RSN key management/authentication algorithm
1332 * to an internal code.
1333 */
1334static int
1335rsn_keymgmt(const uint8_t *sel)
1336{
1337#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1338	uint32_t w = le32dec(sel);
1339
1340	switch (w) {
1341	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1342		return RSN_ASE_8021X_UNSPEC;
1343	case RSN_SEL(RSN_ASE_8021X_PSK):
1344		return RSN_ASE_8021X_PSK;
1345	case RSN_SEL(RSN_ASE_NONE):
1346		return RSN_ASE_NONE;
1347	}
1348	return 0;		/* NB: so is discarded */
1349#undef RSN_SEL
1350}
1351
1352/*
1353 * Parse a WPA/RSN information element to collect parameters
1354 * and validate the parameters against what has been
1355 * configured for the system.
1356 */
1357static int
1358ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1359	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1360{
1361	uint8_t len = frm[1];
1362	uint32_t w;
1363	int n;
1364
1365	/*
1366	 * Check the length once for fixed parts:
1367	 * version, mcast cipher, and 2 selector counts.
1368	 * Other, variable-length data, must be checked separately.
1369	 */
1370	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1371		IEEE80211_DISCARD_IE(vap,
1372		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1373		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1374		return IEEE80211_REASON_IE_INVALID;
1375	}
1376	if (len < 10) {
1377		IEEE80211_DISCARD_IE(vap,
1378		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1379		    wh, "RSN", "too short, len %u", len);
1380		return IEEE80211_REASON_IE_INVALID;
1381	}
1382	frm += 2;
1383	w = le16dec(frm);
1384	if (w != RSN_VERSION) {
1385		IEEE80211_DISCARD_IE(vap,
1386		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1387		    wh, "RSN", "bad version %u", w);
1388		return IEEE80211_REASON_IE_INVALID;
1389	}
1390	frm += 2, len -= 2;
1391
1392	memset(rsn, 0, sizeof(*rsn));
1393
1394	/* multicast/group cipher */
1395	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1396	frm += 4, len -= 4;
1397
1398	/* unicast ciphers */
1399	n = le16dec(frm);
1400	frm += 2, len -= 2;
1401	if (len < n*4+2) {
1402		IEEE80211_DISCARD_IE(vap,
1403		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1404		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1405		    len, n);
1406		return IEEE80211_REASON_IE_INVALID;
1407	}
1408	w = 0;
1409	for (; n > 0; n--) {
1410		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1411		frm += 4, len -= 4;
1412	}
1413	if (w & (1<<IEEE80211_CIPHER_TKIP))
1414		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1415	else
1416		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1417
1418	/* key management algorithms */
1419	n = le16dec(frm);
1420	frm += 2, len -= 2;
1421	if (len < n*4) {
1422		IEEE80211_DISCARD_IE(vap,
1423		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1424		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1425		    len, n);
1426		return IEEE80211_REASON_IE_INVALID;
1427	}
1428	w = 0;
1429	for (; n > 0; n--) {
1430		w |= rsn_keymgmt(frm);
1431		frm += 4, len -= 4;
1432	}
1433	if (w & RSN_ASE_8021X_UNSPEC)
1434		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1435	else
1436		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1437
1438	/* optional RSN capabilities */
1439	if (len > 2)
1440		rsn->rsn_caps = le16dec(frm);
1441	/* XXXPMKID */
1442
1443	return 0;
1444}
1445
1446/*
1447 * WPA/802.11i association request processing.
1448 */
1449static int
1450wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1451	const struct ieee80211_frame *wh, const uint8_t *wpa,
1452	const uint8_t *rsn, uint16_t capinfo)
1453{
1454	struct ieee80211vap *vap = ni->ni_vap;
1455	uint8_t reason;
1456	int badwparsn;
1457
1458	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1459	if (wpa == NULL && rsn == NULL) {
1460		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1461			/*
1462			 * W-Fi Protected Setup (WPS) permits
1463			 * clients to associate and pass EAPOL frames
1464			 * to establish initial credentials.
1465			 */
1466			ni->ni_flags |= IEEE80211_NODE_WPS;
1467			return 1;
1468		}
1469		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1470		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1471			/*
1472			 * Transitional Security Network.  Permits clients
1473			 * to associate and use WEP while WPA is configured.
1474			 */
1475			ni->ni_flags |= IEEE80211_NODE_TSN;
1476			return 1;
1477		}
1478		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1479		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1480		vap->iv_stats.is_rx_assoc_badwpaie++;
1481		reason = IEEE80211_REASON_IE_INVALID;
1482		goto bad;
1483	}
1484	/* assert right association security credentials */
1485	badwparsn = 0;			/* NB: to silence compiler */
1486	switch (vap->iv_flags & IEEE80211_F_WPA) {
1487	case IEEE80211_F_WPA1:
1488		badwparsn = (wpa == NULL);
1489		break;
1490	case IEEE80211_F_WPA2:
1491		badwparsn = (rsn == NULL);
1492		break;
1493	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1494		badwparsn = (wpa == NULL && rsn == NULL);
1495		break;
1496	}
1497	if (badwparsn) {
1498		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1499		    wh, NULL,
1500		    "%s", "missing WPA/RSN IE in association request");
1501		vap->iv_stats.is_rx_assoc_badwpaie++;
1502		reason = IEEE80211_REASON_IE_INVALID;
1503		goto bad;
1504	}
1505	/*
1506	 * Parse WPA/RSN information element.
1507	 */
1508	if (wpa != NULL)
1509		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1510	else
1511		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1512	if (reason != 0) {
1513		/* XXX distinguish WPA/RSN? */
1514		vap->iv_stats.is_rx_assoc_badwpaie++;
1515		goto bad;
1516	}
1517	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1518	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1519	    wpa != NULL ? "WPA" : "RSN",
1520	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1521	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1522	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1523
1524	return 1;
1525bad:
1526	ieee80211_node_deauth(ni, reason);
1527	return 0;
1528}
1529
1530/* XXX find a better place for definition */
1531struct l2_update_frame {
1532	struct ether_header eh;
1533	uint8_t dsap;
1534	uint8_t ssap;
1535	uint8_t control;
1536	uint8_t xid[3];
1537}  __packed;
1538
1539/*
1540 * Deliver a TGf L2UF frame on behalf of a station.
1541 * This primes any bridge when the station is roaming
1542 * between ap's on the same wired network.
1543 */
1544static void
1545ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1546{
1547	struct ieee80211vap *vap = ni->ni_vap;
1548	struct ifnet *ifp = vap->iv_ifp;
1549	struct mbuf *m;
1550	struct l2_update_frame *l2uf;
1551	struct ether_header *eh;
1552
1553	m = m_gethdr(M_NOWAIT, MT_DATA);
1554	if (m == NULL) {
1555		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1556		    "%s", "no mbuf for l2uf frame");
1557		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1558		return;
1559	}
1560	l2uf = mtod(m, struct l2_update_frame *);
1561	eh = &l2uf->eh;
1562	/* dst: Broadcast address */
1563	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1564	/* src: associated STA */
1565	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1566	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1567
1568	l2uf->dsap = 0;
1569	l2uf->ssap = 0;
1570	l2uf->control = 0xf5;
1571	l2uf->xid[0] = 0x81;
1572	l2uf->xid[1] = 0x80;
1573	l2uf->xid[2] = 0x00;
1574
1575	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1576	hostap_deliver_data(vap, ni, m);
1577}
1578
1579static void
1580ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1581	int reassoc, int resp, const char *tag, int rate)
1582{
1583	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1584	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1585	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1586	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1587	ieee80211_node_leave(ni);
1588}
1589
1590static void
1591capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1592	int reassoc, int resp, const char *tag, int capinfo)
1593{
1594	struct ieee80211vap *vap = ni->ni_vap;
1595
1596	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1597	    "deny %s request, %s mismatch 0x%x",
1598	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1599	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1600	ieee80211_node_leave(ni);
1601	vap->iv_stats.is_rx_assoc_capmismatch++;
1602}
1603
1604static void
1605htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1606	int reassoc, int resp)
1607{
1608	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1609	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1610	/* XXX no better code */
1611	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1612	ieee80211_node_leave(ni);
1613}
1614
1615static void
1616authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1617	int algo, int seq, int status)
1618{
1619	struct ieee80211vap *vap = ni->ni_vap;
1620
1621	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1622	    wh, NULL, "unsupported alg %d", algo);
1623	vap->iv_stats.is_rx_auth_unsupported++;
1624	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1625	    seq | (status << 16));
1626}
1627
1628static __inline int
1629ishtmixed(const uint8_t *ie)
1630{
1631	const struct ieee80211_ie_htinfo *ht =
1632	    (const struct ieee80211_ie_htinfo *) ie;
1633	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1634	    IEEE80211_HTINFO_OPMODE_MIXED;
1635}
1636
1637static int
1638is11bclient(const uint8_t *rates, const uint8_t *xrates)
1639{
1640	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1641	int i;
1642
1643	/* NB: the 11b clients we care about will not have xrates */
1644	if (xrates != NULL || rates == NULL)
1645		return 0;
1646	for (i = 0; i < rates[1]; i++) {
1647		int r = rates[2+i] & IEEE80211_RATE_VAL;
1648		if (r > 2*11 || ((1<<r) & brates) == 0)
1649			return 0;
1650	}
1651	return 1;
1652}
1653
1654static void
1655hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1656	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1657{
1658	struct ieee80211vap *vap = ni->ni_vap;
1659	struct ieee80211com *ic = ni->ni_ic;
1660	struct ieee80211_frame *wh;
1661	uint8_t *frm, *efrm, *sfrm;
1662	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1663	int reassoc, resp;
1664	uint8_t rate;
1665
1666	wh = mtod(m0, struct ieee80211_frame *);
1667	frm = (uint8_t *)&wh[1];
1668	efrm = mtod(m0, uint8_t *) + m0->m_len;
1669	switch (subtype) {
1670	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1671		/*
1672		 * We process beacon/probe response frames when scanning;
1673		 * otherwise we check beacon frames for overlapping non-ERP
1674		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1675		 */
1676		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1677			vap->iv_stats.is_rx_mgtdiscard++;
1678			return;
1679		}
1680		/* FALLTHROUGH */
1681	case IEEE80211_FC0_SUBTYPE_BEACON: {
1682		struct ieee80211_scanparams scan;
1683
1684		/* NB: accept off-channel frames */
1685		/* XXX TODO: use rxstatus to determine off-channel details */
1686		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1687			return;
1688		/*
1689		 * Count frame now that we know it's to be processed.
1690		 */
1691		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1692			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1693			IEEE80211_NODE_STAT(ni, rx_beacons);
1694		} else
1695			IEEE80211_NODE_STAT(ni, rx_proberesp);
1696		/*
1697		 * If scanning, just pass information to the scan module.
1698		 */
1699		if (ic->ic_flags & IEEE80211_F_SCAN) {
1700			if (scan.status == 0 &&		/* NB: on channel */
1701			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1702				/*
1703				 * Actively scanning a channel marked passive;
1704				 * send a probe request now that we know there
1705				 * is 802.11 traffic present.
1706				 *
1707				 * XXX check if the beacon we recv'd gives
1708				 * us what we need and suppress the probe req
1709				 */
1710				ieee80211_probe_curchan(vap, 1);
1711				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1712			}
1713			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1714			    subtype, rssi, nf);
1715			return;
1716		}
1717		/*
1718		 * Check beacon for overlapping bss w/ non ERP stations.
1719		 * If we detect one and protection is configured but not
1720		 * enabled, enable it and start a timer that'll bring us
1721		 * out if we stop seeing the bss.
1722		 */
1723		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1724		    scan.status == 0 &&			/* NB: on-channel */
1725		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1726		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1727			ic->ic_lastnonerp = ticks;
1728			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1729			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1730			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1731				IEEE80211_NOTE_FRAME(vap,
1732				    IEEE80211_MSG_ASSOC, wh,
1733				    "non-ERP present on channel %d "
1734				    "(saw erp 0x%x from channel %d), "
1735				    "enable use of protection",
1736				    ic->ic_curchan->ic_ieee,
1737				    scan.erp, scan.chan);
1738				ic->ic_flags |= IEEE80211_F_USEPROT;
1739				ieee80211_notify_erp(ic);
1740			}
1741		}
1742		/*
1743		 * Check beacon for non-HT station on HT channel
1744		 * and update HT BSS occupancy as appropriate.
1745		 */
1746		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1747			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1748				/*
1749				 * Off control channel; only check frames
1750				 * that come in the extension channel when
1751				 * operating w/ HT40.
1752				 */
1753				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1754					break;
1755				if (scan.chan != ic->ic_curchan->ic_extieee)
1756					break;
1757			}
1758			if (scan.htinfo == NULL) {
1759				ieee80211_htprot_update(ic,
1760				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1761				    IEEE80211_HTINFO_NONHT_PRESENT);
1762			} else if (ishtmixed(scan.htinfo)) {
1763				/* XXX? take NONHT_PRESENT from beacon? */
1764				ieee80211_htprot_update(ic,
1765				    IEEE80211_HTINFO_OPMODE_MIXED |
1766				    IEEE80211_HTINFO_NONHT_PRESENT);
1767			}
1768		}
1769		break;
1770	}
1771
1772	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1773		if (vap->iv_state != IEEE80211_S_RUN) {
1774			vap->iv_stats.is_rx_mgtdiscard++;
1775			return;
1776		}
1777		/*
1778		 * Consult the ACL policy module if setup.
1779		 */
1780		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1781			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1782			    wh, NULL, "%s", "disallowed by ACL");
1783			vap->iv_stats.is_rx_acl++;
1784			return;
1785		}
1786		/*
1787		 * prreq frame format
1788		 *	[tlv] ssid
1789		 *	[tlv] supported rates
1790		 *	[tlv] extended supported rates
1791		 */
1792		ssid = rates = xrates = NULL;
1793		while (efrm - frm > 1) {
1794			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1795			switch (*frm) {
1796			case IEEE80211_ELEMID_SSID:
1797				ssid = frm;
1798				break;
1799			case IEEE80211_ELEMID_RATES:
1800				rates = frm;
1801				break;
1802			case IEEE80211_ELEMID_XRATES:
1803				xrates = frm;
1804				break;
1805			}
1806			frm += frm[1] + 2;
1807		}
1808		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1809		if (xrates != NULL)
1810			IEEE80211_VERIFY_ELEMENT(xrates,
1811				IEEE80211_RATE_MAXSIZE - rates[1], return);
1812		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1813		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1814		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1815			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1816			    wh, NULL,
1817			    "%s", "no ssid with ssid suppression enabled");
1818			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1819			return;
1820		}
1821
1822		/* XXX find a better class or define it's own */
1823		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1824		    "%s", "recv probe req");
1825		/*
1826		 * Some legacy 11b clients cannot hack a complete
1827		 * probe response frame.  When the request includes
1828		 * only a bare-bones rate set, communicate this to
1829		 * the transmit side.
1830		 */
1831		ieee80211_send_proberesp(vap, wh->i_addr2,
1832		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1833		break;
1834
1835	case IEEE80211_FC0_SUBTYPE_AUTH: {
1836		uint16_t algo, seq, status;
1837
1838		if (vap->iv_state != IEEE80211_S_RUN) {
1839			vap->iv_stats.is_rx_mgtdiscard++;
1840			return;
1841		}
1842		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1843			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1844			    wh, NULL, "%s", "wrong bssid");
1845			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1846			return;
1847		}
1848		/*
1849		 * auth frame format
1850		 *	[2] algorithm
1851		 *	[2] sequence
1852		 *	[2] status
1853		 *	[tlv*] challenge
1854		 */
1855		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1856		algo   = le16toh(*(uint16_t *)frm);
1857		seq    = le16toh(*(uint16_t *)(frm + 2));
1858		status = le16toh(*(uint16_t *)(frm + 4));
1859		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1860		    "recv auth frame with algorithm %d seq %d", algo, seq);
1861		/*
1862		 * Consult the ACL policy module if setup.
1863		 */
1864		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1865			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1866			    wh, NULL, "%s", "disallowed by ACL");
1867			vap->iv_stats.is_rx_acl++;
1868			ieee80211_send_error(ni, wh->i_addr2,
1869			    IEEE80211_FC0_SUBTYPE_AUTH,
1870			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1871			return;
1872		}
1873		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1874			IEEE80211_DISCARD(vap,
1875			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1876			    wh, NULL, "%s", "TKIP countermeasures enabled");
1877			vap->iv_stats.is_rx_auth_countermeasures++;
1878			ieee80211_send_error(ni, wh->i_addr2,
1879				IEEE80211_FC0_SUBTYPE_AUTH,
1880				IEEE80211_REASON_MIC_FAILURE);
1881			return;
1882		}
1883		if (algo == IEEE80211_AUTH_ALG_SHARED)
1884			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1885			    seq, status);
1886		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1887			hostap_auth_open(ni, wh, rssi, nf, seq, status);
1888		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1889			authalgreject(ni, wh, algo,
1890			    seq+1, IEEE80211_STATUS_ALG);
1891			return;
1892		} else {
1893			/*
1894			 * We assume that an unknown algorithm is the result
1895			 * of a decryption failure on a shared key auth frame;
1896			 * return a status code appropriate for that instead
1897			 * of IEEE80211_STATUS_ALG.
1898			 *
1899			 * NB: a seq# of 4 is intentional; the decrypted
1900			 *     frame likely has a bogus seq value.
1901			 */
1902			authalgreject(ni, wh, algo,
1903			    4, IEEE80211_STATUS_CHALLENGE);
1904			return;
1905		}
1906		break;
1907	}
1908
1909	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1910	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1911		uint16_t capinfo, lintval;
1912		struct ieee80211_rsnparms rsnparms;
1913
1914		if (vap->iv_state != IEEE80211_S_RUN) {
1915			vap->iv_stats.is_rx_mgtdiscard++;
1916			return;
1917		}
1918		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1919			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1920			    wh, NULL, "%s", "wrong bssid");
1921			vap->iv_stats.is_rx_assoc_bss++;
1922			return;
1923		}
1924		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1925			reassoc = 1;
1926			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1927		} else {
1928			reassoc = 0;
1929			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1930		}
1931		if (ni == vap->iv_bss) {
1932			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1933			    "deny %s request, sta not authenticated",
1934			    reassoc ? "reassoc" : "assoc");
1935			ieee80211_send_error(ni, wh->i_addr2,
1936			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1937			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1938			vap->iv_stats.is_rx_assoc_notauth++;
1939			return;
1940		}
1941
1942		/*
1943		 * asreq frame format
1944		 *	[2] capability information
1945		 *	[2] listen interval
1946		 *	[6*] current AP address (reassoc only)
1947		 *	[tlv] ssid
1948		 *	[tlv] supported rates
1949		 *	[tlv] extended supported rates
1950		 *	[tlv] WPA or RSN
1951		 *	[tlv] HT capabilities
1952		 *	[tlv] Atheros capabilities
1953		 */
1954		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1955		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1956		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1957		if (reassoc)
1958			frm += 6;	/* ignore current AP info */
1959		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1960		sfrm = frm;
1961		while (efrm - frm > 1) {
1962			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1963			switch (*frm) {
1964			case IEEE80211_ELEMID_SSID:
1965				ssid = frm;
1966				break;
1967			case IEEE80211_ELEMID_RATES:
1968				rates = frm;
1969				break;
1970			case IEEE80211_ELEMID_XRATES:
1971				xrates = frm;
1972				break;
1973			case IEEE80211_ELEMID_RSN:
1974				rsn = frm;
1975				break;
1976			case IEEE80211_ELEMID_HTCAP:
1977				htcap = frm;
1978				break;
1979			case IEEE80211_ELEMID_VENDOR:
1980				if (iswpaoui(frm))
1981					wpa = frm;
1982				else if (iswmeinfo(frm))
1983					wme = frm;
1984#ifdef IEEE80211_SUPPORT_SUPERG
1985				else if (isatherosoui(frm))
1986					ath = frm;
1987#endif
1988				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1989					if (ishtcapoui(frm) && htcap == NULL)
1990						htcap = frm;
1991				}
1992				break;
1993			}
1994			frm += frm[1] + 2;
1995		}
1996		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1997		if (xrates != NULL)
1998			IEEE80211_VERIFY_ELEMENT(xrates,
1999				IEEE80211_RATE_MAXSIZE - rates[1], return);
2000		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2001		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2002		if (htcap != NULL) {
2003			IEEE80211_VERIFY_LENGTH(htcap[1],
2004			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2005			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2006			         sizeof(struct ieee80211_ie_htcap)-2,
2007			     return);		/* XXX just NULL out? */
2008		}
2009
2010		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2011		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2012			return;
2013		/* discard challenge after association */
2014		if (ni->ni_challenge != NULL) {
2015			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2016			ni->ni_challenge = NULL;
2017		}
2018		/* NB: 802.11 spec says to ignore station's privacy bit */
2019		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2020			capinfomismatch(ni, wh, reassoc, resp,
2021			    "capability", capinfo);
2022			return;
2023		}
2024		/*
2025		 * Disallow re-associate w/ invalid slot time setting.
2026		 */
2027		if (ni->ni_associd != 0 &&
2028		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2029		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2030			capinfomismatch(ni, wh, reassoc, resp,
2031			    "slot time", capinfo);
2032			return;
2033		}
2034		rate = ieee80211_setup_rates(ni, rates, xrates,
2035				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2036				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2037		if (rate & IEEE80211_RATE_BASIC) {
2038			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2039			vap->iv_stats.is_rx_assoc_norate++;
2040			return;
2041		}
2042		/*
2043		 * If constrained to 11g-only stations reject an
2044		 * 11b-only station.  We cheat a bit here by looking
2045		 * at the max negotiated xmit rate and assuming anyone
2046		 * with a best rate <24Mb/s is an 11b station.
2047		 */
2048		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2049			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2050			vap->iv_stats.is_rx_assoc_norate++;
2051			return;
2052		}
2053		/*
2054		 * Do HT rate set handling and setup HT node state.
2055		 */
2056		ni->ni_chan = vap->iv_bss->ni_chan;
2057		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2058			rate = ieee80211_setup_htrates(ni, htcap,
2059				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2060				IEEE80211_F_DOBRS);
2061			if (rate & IEEE80211_RATE_BASIC) {
2062				ratesetmismatch(ni, wh, reassoc, resp,
2063				    "HT", rate);
2064				vap->iv_stats.is_ht_assoc_norate++;
2065				return;
2066			}
2067			ieee80211_ht_node_init(ni);
2068			ieee80211_ht_updatehtcap(ni, htcap);
2069		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2070			ieee80211_ht_node_cleanup(ni);
2071#ifdef IEEE80211_SUPPORT_SUPERG
2072		/* Always do ff node cleanup; for A-MSDU */
2073		ieee80211_ff_node_cleanup(ni);
2074#endif
2075		/*
2076		 * Allow AMPDU operation only with unencrypted traffic
2077		 * or AES-CCM; the 11n spec only specifies these ciphers
2078		 * so permitting any others is undefined and can lead
2079		 * to interoperability problems.
2080		 */
2081		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2082		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2083		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2084		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2085			IEEE80211_NOTE(vap,
2086			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2087			    "disallow HT use because WEP or TKIP requested, "
2088			    "capinfo 0x%x ucastcipher %d", capinfo,
2089			    rsnparms.rsn_ucastcipher);
2090			ieee80211_ht_node_cleanup(ni);
2091#ifdef IEEE80211_SUPPORT_SUPERG
2092			/* Always do ff node cleanup; for A-MSDU */
2093			ieee80211_ff_node_cleanup(ni);
2094#endif
2095			vap->iv_stats.is_ht_assoc_downgrade++;
2096		}
2097		/*
2098		 * If constrained to 11n-only stations reject legacy stations.
2099		 */
2100		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2101		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2102			htcapmismatch(ni, wh, reassoc, resp);
2103			vap->iv_stats.is_ht_assoc_nohtcap++;
2104			return;
2105		}
2106		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2107		ni->ni_noise = nf;
2108		ni->ni_intval = lintval;
2109		ni->ni_capinfo = capinfo;
2110		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2111		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2112		/*
2113		 * Store the IEs.
2114		 * XXX maybe better to just expand
2115		 */
2116		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2117#define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2118			if (wpa != NULL)
2119				setie(wpa_ie, wpa - sfrm);
2120			if (rsn != NULL)
2121				setie(rsn_ie, rsn - sfrm);
2122			if (htcap != NULL)
2123				setie(htcap_ie, htcap - sfrm);
2124			if (wme != NULL) {
2125				setie(wme_ie, wme - sfrm);
2126				/*
2127				 * Mark node as capable of QoS.
2128				 */
2129				ni->ni_flags |= IEEE80211_NODE_QOS;
2130			} else
2131				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2132#ifdef IEEE80211_SUPPORT_SUPERG
2133			if (ath != NULL) {
2134				setie(ath_ie, ath - sfrm);
2135				/*
2136				 * Parse ATH station parameters.
2137				 */
2138				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2139			} else
2140#endif
2141				ni->ni_ath_flags = 0;
2142#undef setie
2143		} else {
2144			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2145			ni->ni_ath_flags = 0;
2146		}
2147		ieee80211_node_join(ni, resp);
2148		ieee80211_deliver_l2uf(ni);
2149		break;
2150	}
2151
2152	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2153	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2154		uint16_t reason;
2155
2156		if (vap->iv_state != IEEE80211_S_RUN ||
2157		    /* NB: can happen when in promiscuous mode */
2158		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2159			vap->iv_stats.is_rx_mgtdiscard++;
2160			break;
2161		}
2162		/*
2163		 * deauth/disassoc frame format
2164		 *	[2] reason
2165		 */
2166		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2167		reason = le16toh(*(uint16_t *)frm);
2168		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2169			vap->iv_stats.is_rx_deauth++;
2170			IEEE80211_NODE_STAT(ni, rx_deauth);
2171		} else {
2172			vap->iv_stats.is_rx_disassoc++;
2173			IEEE80211_NODE_STAT(ni, rx_disassoc);
2174		}
2175		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2176		    "recv %s (reason: %d (%s))",
2177		    ieee80211_mgt_subtype_name(subtype),
2178		    reason, ieee80211_reason_to_string(reason));
2179		if (ni != vap->iv_bss)
2180			ieee80211_node_leave(ni);
2181		break;
2182	}
2183
2184	case IEEE80211_FC0_SUBTYPE_ACTION:
2185	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2186		if (ni == vap->iv_bss) {
2187			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2188			    wh, NULL, "%s", "unknown node");
2189			vap->iv_stats.is_rx_mgtdiscard++;
2190		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2191		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2192			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2193			    wh, NULL, "%s", "not for us");
2194			vap->iv_stats.is_rx_mgtdiscard++;
2195		} else if (vap->iv_state != IEEE80211_S_RUN) {
2196			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2197			    wh, NULL, "wrong state %s",
2198			    ieee80211_state_name[vap->iv_state]);
2199			vap->iv_stats.is_rx_mgtdiscard++;
2200		} else {
2201			if (ieee80211_parse_action(ni, m0) == 0)
2202				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2203		}
2204		break;
2205
2206	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2207	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2208	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2209	case IEEE80211_FC0_SUBTYPE_ATIM:
2210		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2211		    wh, NULL, "%s", "not handled");
2212		vap->iv_stats.is_rx_mgtdiscard++;
2213		break;
2214
2215	default:
2216		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2217		    wh, "mgt", "subtype 0x%x not handled", subtype);
2218		vap->iv_stats.is_rx_badsubtype++;
2219		break;
2220	}
2221}
2222
2223static void
2224hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2225{
2226	switch (subtype) {
2227	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2228		ni->ni_vap->iv_recv_pspoll(ni, m);
2229		break;
2230	case IEEE80211_FC0_SUBTYPE_BAR:
2231		ieee80211_recv_bar(ni, m);
2232		break;
2233	}
2234}
2235
2236/*
2237 * Process a received ps-poll frame.
2238 */
2239void
2240ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2241{
2242	struct ieee80211vap *vap = ni->ni_vap;
2243	struct ieee80211com *ic = vap->iv_ic;
2244	struct ieee80211_frame_min *wh;
2245	struct mbuf *m;
2246	uint16_t aid;
2247	int qlen;
2248
2249	wh = mtod(m0, struct ieee80211_frame_min *);
2250	if (ni->ni_associd == 0) {
2251		IEEE80211_DISCARD(vap,
2252		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2253		    (struct ieee80211_frame *) wh, NULL,
2254		    "%s", "unassociated station");
2255		vap->iv_stats.is_ps_unassoc++;
2256		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2257			IEEE80211_REASON_NOT_ASSOCED);
2258		return;
2259	}
2260
2261	aid = le16toh(*(uint16_t *)wh->i_dur);
2262	if (aid != ni->ni_associd) {
2263		IEEE80211_DISCARD(vap,
2264		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2265		    (struct ieee80211_frame *) wh, NULL,
2266		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2267		    ni->ni_associd, aid);
2268		vap->iv_stats.is_ps_badaid++;
2269		/*
2270		 * NB: We used to deauth the station but it turns out
2271		 * the Blackberry Curve 8230 (and perhaps other devices)
2272		 * sometimes send the wrong AID when WME is negotiated.
2273		 * Being more lenient here seems ok as we already check
2274		 * the station is associated and we only return frames
2275		 * queued for the station (i.e. we don't use the AID).
2276		 */
2277		return;
2278	}
2279
2280	/* Okay, take the first queued packet and put it out... */
2281	m = ieee80211_node_psq_dequeue(ni, &qlen);
2282	if (m == NULL) {
2283		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2284		    "%s", "recv ps-poll, but queue empty");
2285		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2286		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2287		if (vap->iv_set_tim != NULL)
2288			vap->iv_set_tim(ni, 0);	/* just in case */
2289		return;
2290	}
2291	/*
2292	 * If there are more packets, set the more packets bit
2293	 * in the packet dispatched to the station; otherwise
2294	 * turn off the TIM bit.
2295	 */
2296	if (qlen != 0) {
2297		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2298		    "recv ps-poll, send packet, %u still queued", qlen);
2299		m->m_flags |= M_MORE_DATA;
2300	} else {
2301		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2302		    "%s", "recv ps-poll, send packet, queue empty");
2303		if (vap->iv_set_tim != NULL)
2304			vap->iv_set_tim(ni, 0);
2305	}
2306	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2307
2308	/*
2309	 * Do the right thing; if it's an encap'ed frame then
2310	 * call ieee80211_parent_xmitpkt() else
2311	 * call ieee80211_vap_xmitpkt().
2312	 */
2313	if (m->m_flags & M_ENCAP) {
2314		(void) ieee80211_parent_xmitpkt(ic, m);
2315	} else {
2316		(void) ieee80211_vap_xmitpkt(vap, m);
2317	}
2318}
2319