xref: /freebsd/sys/net80211/ieee80211_hostap.c (revision 45f856e3ac5d0535fd1a285414f23c5038b27207)
1b032f27cSSam Leffler /*-
2b032f27cSSam Leffler  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3b032f27cSSam Leffler  * All rights reserved.
4b032f27cSSam Leffler  *
5b032f27cSSam Leffler  * Redistribution and use in source and binary forms, with or without
6b032f27cSSam Leffler  * modification, are permitted provided that the following conditions
7b032f27cSSam Leffler  * are met:
8b032f27cSSam Leffler  * 1. Redistributions of source code must retain the above copyright
9b032f27cSSam Leffler  *    notice, this list of conditions and the following disclaimer.
10b032f27cSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
11b032f27cSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
12b032f27cSSam Leffler  *    documentation and/or other materials provided with the distribution.
13b032f27cSSam Leffler  *
14b032f27cSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15b032f27cSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16b032f27cSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17b032f27cSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18b032f27cSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19b032f27cSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20b032f27cSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21b032f27cSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22b032f27cSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23b032f27cSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24b032f27cSSam Leffler  */
25b032f27cSSam Leffler 
26b032f27cSSam Leffler #include <sys/cdefs.h>
27b032f27cSSam Leffler #ifdef __FreeBSD__
28b032f27cSSam Leffler __FBSDID("$FreeBSD$");
29b032f27cSSam Leffler #endif
30b032f27cSSam Leffler 
31b032f27cSSam Leffler /*
32b032f27cSSam Leffler  * IEEE 802.11 HOSTAP mode support.
33b032f27cSSam Leffler  */
34b032f27cSSam Leffler #include "opt_inet.h"
35b032f27cSSam Leffler #include "opt_wlan.h"
36b032f27cSSam Leffler 
37b032f27cSSam Leffler #include <sys/param.h>
38b032f27cSSam Leffler #include <sys/systm.h>
39b032f27cSSam Leffler #include <sys/mbuf.h>
40b032f27cSSam Leffler #include <sys/malloc.h>
41b032f27cSSam Leffler #include <sys/kernel.h>
42b032f27cSSam Leffler 
43b032f27cSSam Leffler #include <sys/socket.h>
44b032f27cSSam Leffler #include <sys/sockio.h>
45b032f27cSSam Leffler #include <sys/endian.h>
46b032f27cSSam Leffler #include <sys/errno.h>
47b032f27cSSam Leffler #include <sys/proc.h>
48b032f27cSSam Leffler #include <sys/sysctl.h>
49b032f27cSSam Leffler 
50b032f27cSSam Leffler #include <net/if.h>
51b032f27cSSam Leffler #include <net/if_media.h>
52b032f27cSSam Leffler #include <net/if_llc.h>
53b032f27cSSam Leffler #include <net/ethernet.h>
54b032f27cSSam Leffler 
55b032f27cSSam Leffler #include <net/bpf.h>
56b032f27cSSam Leffler 
57b032f27cSSam Leffler #include <net80211/ieee80211_var.h>
58b032f27cSSam Leffler #include <net80211/ieee80211_hostap.h>
59b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
60b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
61b032f27cSSam Leffler 
62b032f27cSSam Leffler #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
63b032f27cSSam Leffler 
64b032f27cSSam Leffler static	void hostap_vattach(struct ieee80211vap *);
65b032f27cSSam Leffler static	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
66b032f27cSSam Leffler static	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
67b032f27cSSam Leffler 	    int rssi, int noise, uint32_t rstamp);
68b032f27cSSam Leffler static void hostap_deliver_data(struct ieee80211vap *,
69b032f27cSSam Leffler 	    struct ieee80211_node *, struct mbuf *);
70b032f27cSSam Leffler static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
71b032f27cSSam Leffler 	    int subtype, int rssi, int noise, uint32_t rstamp);
72b032f27cSSam Leffler static void hostap_recv_pspoll(struct ieee80211_node *, struct mbuf *);
73b032f27cSSam Leffler 
74b032f27cSSam Leffler void
75b032f27cSSam Leffler ieee80211_hostap_attach(struct ieee80211com *ic)
76b032f27cSSam Leffler {
77b032f27cSSam Leffler 	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
78b032f27cSSam Leffler }
79b032f27cSSam Leffler 
80b032f27cSSam Leffler void
81b032f27cSSam Leffler ieee80211_hostap_detach(struct ieee80211com *ic)
82b032f27cSSam Leffler {
83b032f27cSSam Leffler }
84b032f27cSSam Leffler 
85b032f27cSSam Leffler static void
86b032f27cSSam Leffler hostap_vdetach(struct ieee80211vap *vap)
87b032f27cSSam Leffler {
88b032f27cSSam Leffler }
89b032f27cSSam Leffler 
90b032f27cSSam Leffler static void
91b032f27cSSam Leffler hostap_vattach(struct ieee80211vap *vap)
92b032f27cSSam Leffler {
93b032f27cSSam Leffler 	vap->iv_newstate = hostap_newstate;
94b032f27cSSam Leffler 	vap->iv_input = hostap_input;
95b032f27cSSam Leffler 	vap->iv_recv_mgmt = hostap_recv_mgmt;
96b032f27cSSam Leffler 	vap->iv_opdetach = hostap_vdetach;
97b032f27cSSam Leffler 	vap->iv_deliver_data = hostap_deliver_data;
98b032f27cSSam Leffler }
99b032f27cSSam Leffler 
100b032f27cSSam Leffler static void
101b032f27cSSam Leffler sta_disassoc(void *arg, struct ieee80211_node *ni)
102b032f27cSSam Leffler {
103b032f27cSSam Leffler 	struct ieee80211vap *vap = arg;
104b032f27cSSam Leffler 
105b032f27cSSam Leffler 	if (ni->ni_vap == vap && ni->ni_associd != 0) {
106b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
107b032f27cSSam Leffler 			IEEE80211_REASON_ASSOC_LEAVE);
108b032f27cSSam Leffler 		ieee80211_node_leave(ni);
109b032f27cSSam Leffler 	}
110b032f27cSSam Leffler }
111b032f27cSSam Leffler 
112b032f27cSSam Leffler /*
113b032f27cSSam Leffler  * IEEE80211_M_HOSTAP vap state machine handler.
114b032f27cSSam Leffler  */
115b032f27cSSam Leffler static int
116b032f27cSSam Leffler hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
117b032f27cSSam Leffler {
118b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
119b032f27cSSam Leffler 	enum ieee80211_state ostate;
120b032f27cSSam Leffler 
121b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
122b032f27cSSam Leffler 
123b032f27cSSam Leffler 	ostate = vap->iv_state;
124b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
125b032f27cSSam Leffler 	    __func__, ieee80211_state_name[ostate],
126b032f27cSSam Leffler 	    ieee80211_state_name[nstate], arg);
127b032f27cSSam Leffler 	vap->iv_state = nstate;			/* state transition */
128b032f27cSSam Leffler 	if (ostate != IEEE80211_S_SCAN)
129b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);	/* background scan */
130b032f27cSSam Leffler 	switch (nstate) {
131b032f27cSSam Leffler 	case IEEE80211_S_INIT:
132b032f27cSSam Leffler 		switch (ostate) {
133b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
134b032f27cSSam Leffler 			ieee80211_cancel_scan(vap);
135b032f27cSSam Leffler 			break;
136b032f27cSSam Leffler 		case IEEE80211_S_CAC:
137b032f27cSSam Leffler 			ieee80211_dfs_cac_stop(vap);
138b032f27cSSam Leffler 			break;
139b032f27cSSam Leffler 		case IEEE80211_S_RUN:
140b032f27cSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
141b032f27cSSam Leffler 			break;
142b032f27cSSam Leffler 		default:
143b032f27cSSam Leffler 			break;
144b032f27cSSam Leffler 		}
145b032f27cSSam Leffler 		if (ostate != IEEE80211_S_INIT) {
146b032f27cSSam Leffler 			/* NB: optimize INIT -> INIT case */
147b032f27cSSam Leffler 			ieee80211_reset_bss(vap);
148b032f27cSSam Leffler 		}
149b032f27cSSam Leffler 		if (vap->iv_auth->ia_detach != NULL)
150b032f27cSSam Leffler 			vap->iv_auth->ia_detach(vap);
151b032f27cSSam Leffler 		break;
152b032f27cSSam Leffler 	case IEEE80211_S_SCAN:
153b032f27cSSam Leffler 		switch (ostate) {
154b032f27cSSam Leffler 		case IEEE80211_S_CSA:
155b032f27cSSam Leffler 		case IEEE80211_S_RUN:
156b032f27cSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
157b032f27cSSam Leffler 			/*
158b032f27cSSam Leffler 			 * Clear overlapping BSS state; the beacon frame
159b032f27cSSam Leffler 			 * will be reconstructed on transition to the RUN
160b032f27cSSam Leffler 			 * state and the timeout routines check if the flag
161b032f27cSSam Leffler 			 * is set before doing anything so this is sufficient.
162b032f27cSSam Leffler 			 */
163b032f27cSSam Leffler 			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
164b032f27cSSam Leffler 			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONHT_PR;
165b032f27cSSam Leffler 			/* fall thru... */
166b032f27cSSam Leffler 		case IEEE80211_S_CAC:
167b032f27cSSam Leffler 			/*
168b032f27cSSam Leffler 			 * NB: We may get here because of a manual channel
169b032f27cSSam Leffler 			 *     change in which case we need to stop CAC
170b032f27cSSam Leffler 			 * XXX no need to stop if ostate RUN but it's ok
171b032f27cSSam Leffler 			 */
172b032f27cSSam Leffler 			ieee80211_dfs_cac_stop(vap);
173b032f27cSSam Leffler 			/* fall thru... */
174b032f27cSSam Leffler 		case IEEE80211_S_INIT:
175b032f27cSSam Leffler 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
176b032f27cSSam Leffler 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
177b032f27cSSam Leffler 				/*
178b032f27cSSam Leffler 				 * Already have a channel; bypass the
179b032f27cSSam Leffler 				 * scan and startup immediately.
180b032f27cSSam Leffler 				 * ieee80211_create_ibss will call back to
181b032f27cSSam Leffler 				 * move us to RUN state.
182b032f27cSSam Leffler 				 */
183b032f27cSSam Leffler 				ieee80211_create_ibss(vap, vap->iv_des_chan);
184b032f27cSSam Leffler 				break;
185b032f27cSSam Leffler 			}
186b032f27cSSam Leffler 			/*
187b032f27cSSam Leffler 			 * Initiate a scan.  We can come here as a result
188b032f27cSSam Leffler 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
189b032f27cSSam Leffler 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
190b032f27cSSam Leffler 			 * and the scan request parameters will be present
191b032f27cSSam Leffler 			 * in iv_scanreq.  Otherwise we do the default.
192b032f27cSSam Leffler 			 */
193b032f27cSSam Leffler 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
194b032f27cSSam Leffler 				ieee80211_check_scan(vap,
195b032f27cSSam Leffler 				    vap->iv_scanreq_flags,
196b032f27cSSam Leffler 				    vap->iv_scanreq_duration,
197b032f27cSSam Leffler 				    vap->iv_scanreq_mindwell,
198b032f27cSSam Leffler 				    vap->iv_scanreq_maxdwell,
199b032f27cSSam Leffler 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
200b032f27cSSam Leffler 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
201b032f27cSSam Leffler 			} else
202b032f27cSSam Leffler 				ieee80211_check_scan_current(vap);
203b032f27cSSam Leffler 			break;
204b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
205b032f27cSSam Leffler 			/*
206b032f27cSSam Leffler 			 * A state change requires a reset; scan.
207b032f27cSSam Leffler 			 */
208b032f27cSSam Leffler 			ieee80211_check_scan_current(vap);
209b032f27cSSam Leffler 			break;
210b032f27cSSam Leffler 		default:
211b032f27cSSam Leffler 			break;
212b032f27cSSam Leffler 		}
213b032f27cSSam Leffler 		break;
214b032f27cSSam Leffler 	case IEEE80211_S_CAC:
215b032f27cSSam Leffler 		/*
216b032f27cSSam Leffler 		 * Start CAC on a DFS channel.  We come here when starting
217b032f27cSSam Leffler 		 * a bss on a DFS channel (see ieee80211_create_ibss).
218b032f27cSSam Leffler 		 */
219b032f27cSSam Leffler 		ieee80211_dfs_cac_start(vap);
220b032f27cSSam Leffler 		break;
221b032f27cSSam Leffler 	case IEEE80211_S_RUN:
222b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA) {
223b032f27cSSam Leffler 			/* XXX validate prerequisites */
224b032f27cSSam Leffler 		}
225b032f27cSSam Leffler 		switch (ostate) {
226b032f27cSSam Leffler 		case IEEE80211_S_INIT:
227b032f27cSSam Leffler 			/*
228b032f27cSSam Leffler 			 * Already have a channel; bypass the
229b032f27cSSam Leffler 			 * scan and startup immediately.
230b032f27cSSam Leffler 			 * Note that ieee80211_create_ibss will call
231b032f27cSSam Leffler 			 * back to do a RUN->RUN state change.
232b032f27cSSam Leffler 			 */
233b032f27cSSam Leffler 			ieee80211_create_ibss(vap,
234b032f27cSSam Leffler 			    ieee80211_ht_adjust_channel(ic,
235b032f27cSSam Leffler 				ic->ic_curchan, vap->iv_flags_ext));
236b032f27cSSam Leffler 			/* NB: iv_bss is changed on return */
237b032f27cSSam Leffler 			break;
238b032f27cSSam Leffler 		case IEEE80211_S_CAC:
239b032f27cSSam Leffler 			/*
240b032f27cSSam Leffler 			 * NB: This is the normal state change when CAC
241b032f27cSSam Leffler 			 * expires and no radar was detected; no need to
242b032f27cSSam Leffler 			 * clear the CAC timer as it's already expired.
243b032f27cSSam Leffler 			 */
244b032f27cSSam Leffler 			/* fall thru... */
245b032f27cSSam Leffler 		case IEEE80211_S_CSA:
246b032f27cSSam Leffler 			/*
247b032f27cSSam Leffler 			 * Update bss node channel to reflect where
248b032f27cSSam Leffler 			 * we landed after CSA.
249b032f27cSSam Leffler 			 */
250b032f27cSSam Leffler 			ieee80211_node_set_chan(vap->iv_bss,
251b032f27cSSam Leffler 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
252b032f27cSSam Leffler 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
253b032f27cSSam Leffler 			/* XXX bypass debug msgs */
254b032f27cSSam Leffler 			break;
255b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
256b032f27cSSam Leffler 		case IEEE80211_S_RUN:
257b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
258b032f27cSSam Leffler 			if (ieee80211_msg_debug(vap)) {
259b032f27cSSam Leffler 				struct ieee80211_node *ni = vap->iv_bss;
260b032f27cSSam Leffler 				ieee80211_note(vap,
261b032f27cSSam Leffler 				    "synchronized with %s ssid ",
262b032f27cSSam Leffler 				    ether_sprintf(ni->ni_bssid));
263b032f27cSSam Leffler 				ieee80211_print_essid(ni->ni_essid,
264b032f27cSSam Leffler 				    ni->ni_esslen);
265b032f27cSSam Leffler 				/* XXX MCS/HT */
266b032f27cSSam Leffler 				printf(" channel %d start %uMb\n",
267b032f27cSSam Leffler 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
268b032f27cSSam Leffler 				    IEEE80211_RATE2MBS(ni->ni_txrate));
269b032f27cSSam Leffler 			}
270b032f27cSSam Leffler #endif
271b032f27cSSam Leffler 			break;
272b032f27cSSam Leffler 		default:
273b032f27cSSam Leffler 			break;
274b032f27cSSam Leffler 		}
275b032f27cSSam Leffler 		/*
276b032f27cSSam Leffler 		 * Start/stop the authenticator.  We delay until here
277b032f27cSSam Leffler 		 * to allow configuration to happen out of order.
278b032f27cSSam Leffler 		 */
279b032f27cSSam Leffler 		if (vap->iv_auth->ia_attach != NULL) {
280b032f27cSSam Leffler 			/* XXX check failure */
281b032f27cSSam Leffler 			vap->iv_auth->ia_attach(vap);
282b032f27cSSam Leffler 		} else if (vap->iv_auth->ia_detach != NULL) {
283b032f27cSSam Leffler 			vap->iv_auth->ia_detach(vap);
284b032f27cSSam Leffler 		}
285b032f27cSSam Leffler 		ieee80211_node_authorize(vap->iv_bss);
286b032f27cSSam Leffler 		break;
287b032f27cSSam Leffler 	default:
288b032f27cSSam Leffler 		break;
289b032f27cSSam Leffler 	}
290b032f27cSSam Leffler 	return 0;
291b032f27cSSam Leffler }
292b032f27cSSam Leffler 
293b032f27cSSam Leffler static void
294b032f27cSSam Leffler hostap_deliver_data(struct ieee80211vap *vap,
295b032f27cSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m)
296b032f27cSSam Leffler {
297b032f27cSSam Leffler 	struct ether_header *eh = mtod(m, struct ether_header *);
298b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
299b032f27cSSam Leffler 
300b032f27cSSam Leffler 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
301b032f27cSSam Leffler 	    ("gack, opmode %d", vap->iv_opmode));
302b032f27cSSam Leffler 	/*
303b032f27cSSam Leffler 	 * Do accounting.
304b032f27cSSam Leffler 	 */
305b032f27cSSam Leffler 	ifp->if_ipackets++;
306b032f27cSSam Leffler 	IEEE80211_NODE_STAT(ni, rx_data);
307b032f27cSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
308b032f27cSSam Leffler 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
309b032f27cSSam Leffler 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
310b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mcast);
311b032f27cSSam Leffler 	} else
312b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ucast);
313b032f27cSSam Leffler 
314b032f27cSSam Leffler 	/* clear driver/net80211 flags before passing up */
315b032f27cSSam Leffler 	m->m_flags &= ~M_80211_RX;
316b032f27cSSam Leffler 
317b032f27cSSam Leffler 	/* perform as a bridge within the AP */
318b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
319b032f27cSSam Leffler 		struct mbuf *mcopy = NULL;
320b032f27cSSam Leffler 
321b032f27cSSam Leffler 		if (m->m_flags & M_MCAST) {
322b032f27cSSam Leffler 			mcopy = m_copypacket(m, M_DONTWAIT);
323b032f27cSSam Leffler 			if (mcopy == NULL)
324b032f27cSSam Leffler 				ifp->if_oerrors++;
325b032f27cSSam Leffler 			else
326b032f27cSSam Leffler 				mcopy->m_flags |= M_MCAST;
327b032f27cSSam Leffler 		} else {
328b032f27cSSam Leffler 			/*
329b032f27cSSam Leffler 			 * Check if the destination is associated with the
330b032f27cSSam Leffler 			 * same vap and authorized to receive traffic.
331b032f27cSSam Leffler 			 * Beware of traffic destined for the vap itself;
332b032f27cSSam Leffler 			 * sending it will not work; just let it be delivered
333b032f27cSSam Leffler 			 * normally.
334b032f27cSSam Leffler 			 */
335b032f27cSSam Leffler 			struct ieee80211_node *sta = ieee80211_find_vap_node(
336b032f27cSSam Leffler 			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
337b032f27cSSam Leffler 			if (sta != NULL) {
338b032f27cSSam Leffler 				if (ieee80211_node_is_authorized(sta)) {
339b032f27cSSam Leffler 					/*
340b032f27cSSam Leffler 					 * Beware of sending to ourself; this
341b032f27cSSam Leffler 					 * needs to happen via the normal
342b032f27cSSam Leffler 					 * input path.
343b032f27cSSam Leffler 					 */
344b032f27cSSam Leffler 					if (sta != vap->iv_bss) {
345b032f27cSSam Leffler 						mcopy = m;
346b032f27cSSam Leffler 						m = NULL;
347b032f27cSSam Leffler 					}
348b032f27cSSam Leffler 				} else {
349b032f27cSSam Leffler 					vap->iv_stats.is_rx_unauth++;
350b032f27cSSam Leffler 					IEEE80211_NODE_STAT(sta, rx_unauth);
351b032f27cSSam Leffler 				}
352b032f27cSSam Leffler 				ieee80211_free_node(sta);
353b032f27cSSam Leffler 			}
354b032f27cSSam Leffler 		}
355b032f27cSSam Leffler 		if (mcopy != NULL) {
356b032f27cSSam Leffler 			int len, err;
357b032f27cSSam Leffler 			len = mcopy->m_pkthdr.len;
358b032f27cSSam Leffler 			IFQ_HANDOFF(ifp, mcopy, err);
359b032f27cSSam Leffler 			if (err) {
360b032f27cSSam Leffler 				/* NB: IFQ_HANDOFF reclaims mcopy */
361b032f27cSSam Leffler 			} else {
362b032f27cSSam Leffler 				ifp->if_opackets++;
363b032f27cSSam Leffler 			}
364b032f27cSSam Leffler 		}
365b032f27cSSam Leffler 	}
366b032f27cSSam Leffler 	if (m != NULL) {
367b032f27cSSam Leffler 		/*
368b032f27cSSam Leffler 		 * Mark frame as coming from vap's interface.
369b032f27cSSam Leffler 		 */
370b032f27cSSam Leffler 		m->m_pkthdr.rcvif = ifp;
371b032f27cSSam Leffler 		if (m->m_flags & M_MCAST) {
372b032f27cSSam Leffler 			/*
373b032f27cSSam Leffler 			 * Spam DWDS vap's w/ multicast traffic.
374b032f27cSSam Leffler 			 */
375b032f27cSSam Leffler 			/* XXX only if dwds in use? */
376b032f27cSSam Leffler 			ieee80211_dwds_mcast(vap, m);
377b032f27cSSam Leffler 		}
378b032f27cSSam Leffler 		if (ni->ni_vlan != 0) {
379b032f27cSSam Leffler 			/* attach vlan tag */
380b032f27cSSam Leffler 			m->m_pkthdr.ether_vtag = ni->ni_vlan;
381b032f27cSSam Leffler 			m->m_flags |= M_VLANTAG;
382b032f27cSSam Leffler 		}
383b032f27cSSam Leffler 		ifp->if_input(ifp, m);
384b032f27cSSam Leffler 	}
385b032f27cSSam Leffler }
386b032f27cSSam Leffler 
387b032f27cSSam Leffler /*
388b032f27cSSam Leffler  * Decide if a received management frame should be
389b032f27cSSam Leffler  * printed when debugging is enabled.  This filters some
390b032f27cSSam Leffler  * of the less interesting frames that come frequently
391b032f27cSSam Leffler  * (e.g. beacons).
392b032f27cSSam Leffler  */
393b032f27cSSam Leffler static __inline int
394b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
395b032f27cSSam Leffler {
396b032f27cSSam Leffler 	switch (subtype) {
397b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
398b032f27cSSam Leffler 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
399b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
400b032f27cSSam Leffler 		return 0;
401b032f27cSSam Leffler 	}
402b032f27cSSam Leffler 	return 1;
403b032f27cSSam Leffler }
404b032f27cSSam Leffler 
405b032f27cSSam Leffler /*
406b032f27cSSam Leffler  * Process a received frame.  The node associated with the sender
407b032f27cSSam Leffler  * should be supplied.  If nothing was found in the node table then
408b032f27cSSam Leffler  * the caller is assumed to supply a reference to iv_bss instead.
409b032f27cSSam Leffler  * The RSSI and a timestamp are also supplied.  The RSSI data is used
410b032f27cSSam Leffler  * during AP scanning to select a AP to associate with; it can have
411b032f27cSSam Leffler  * any units so long as values have consistent units and higher values
412b032f27cSSam Leffler  * mean ``better signal''.  The receive timestamp is currently not used
413b032f27cSSam Leffler  * by the 802.11 layer.
414b032f27cSSam Leffler  */
415b032f27cSSam Leffler static int
416b032f27cSSam Leffler hostap_input(struct ieee80211_node *ni, struct mbuf *m,
417b032f27cSSam Leffler 	int rssi, int noise, uint32_t rstamp)
418b032f27cSSam Leffler {
419b032f27cSSam Leffler #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
420b032f27cSSam Leffler #define	HAS_SEQ(type)	((type & 0x4) == 0)
421b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
422b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
423b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
424b032f27cSSam Leffler 	struct ieee80211_frame *wh;
425b032f27cSSam Leffler 	struct ieee80211_key *key;
426b032f27cSSam Leffler 	struct ether_header *eh;
427b032f27cSSam Leffler 	int hdrspace, need_tap;
428b032f27cSSam Leffler 	uint8_t dir, type, subtype, qos;
429b032f27cSSam Leffler 	uint8_t *bssid;
430b032f27cSSam Leffler 	uint16_t rxseq;
431b032f27cSSam Leffler 
43245f856e3SSam Leffler 	if (m->m_flags & M_AMPDU_MPDU) {
433b032f27cSSam Leffler 		/*
434b032f27cSSam Leffler 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
43545f856e3SSam Leffler 		 * w/ M_AMPDU_MPDU marked have already passed through
43645f856e3SSam Leffler 		 * here but were received out of order and been held on
43745f856e3SSam Leffler 		 * the reorder queue.  When resubmitted they are marked
43845f856e3SSam Leffler 		 * with the M_AMPDU_MPDU flag and we can bypass most of
43945f856e3SSam Leffler 		 * the normal processing.
440b032f27cSSam Leffler 		 */
441b032f27cSSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
442b032f27cSSam Leffler 		type = IEEE80211_FC0_TYPE_DATA;
443b032f27cSSam Leffler 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
444b032f27cSSam Leffler 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
445b032f27cSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
446b032f27cSSam Leffler 		goto resubmit_ampdu;
447b032f27cSSam Leffler 	}
448b032f27cSSam Leffler 
449b032f27cSSam Leffler 	KASSERT(ni != NULL, ("null node"));
450b032f27cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
451b032f27cSSam Leffler 
452b032f27cSSam Leffler 	need_tap = 1;			/* mbuf need to be tapped. */
453b032f27cSSam Leffler 	type = -1;			/* undefined */
454b032f27cSSam Leffler 
455b032f27cSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
456b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
457b032f27cSSam Leffler 		    ni->ni_macaddr, NULL,
458b032f27cSSam Leffler 		    "too short (1): len %u", m->m_pkthdr.len);
459b032f27cSSam Leffler 		vap->iv_stats.is_rx_tooshort++;
460b032f27cSSam Leffler 		goto out;
461b032f27cSSam Leffler 	}
462b032f27cSSam Leffler 	/*
463b032f27cSSam Leffler 	 * Bit of a cheat here, we use a pointer for a 3-address
464b032f27cSSam Leffler 	 * frame format but don't reference fields past outside
465b032f27cSSam Leffler 	 * ieee80211_frame_min w/o first validating the data is
466b032f27cSSam Leffler 	 * present.
467b032f27cSSam Leffler 	 */
468b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
469b032f27cSSam Leffler 
470b032f27cSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
471b032f27cSSam Leffler 	    IEEE80211_FC0_VERSION_0) {
472b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
473b032f27cSSam Leffler 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
474b032f27cSSam Leffler 		vap->iv_stats.is_rx_badversion++;
475b032f27cSSam Leffler 		goto err;
476b032f27cSSam Leffler 	}
477b032f27cSSam Leffler 
478b032f27cSSam Leffler 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
479b032f27cSSam Leffler 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
480b032f27cSSam Leffler 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
481b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
482b032f27cSSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS)
483b032f27cSSam Leffler 			bssid = wh->i_addr1;
484b032f27cSSam Leffler 		else if (type == IEEE80211_FC0_TYPE_CTL)
485b032f27cSSam Leffler 			bssid = wh->i_addr1;
486b032f27cSSam Leffler 		else {
487b032f27cSSam Leffler 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
488b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap,
489b032f27cSSam Leffler 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
490b032f27cSSam Leffler 				    NULL, "too short (2): len %u",
491b032f27cSSam Leffler 				    m->m_pkthdr.len);
492b032f27cSSam Leffler 				vap->iv_stats.is_rx_tooshort++;
493b032f27cSSam Leffler 				goto out;
494b032f27cSSam Leffler 			}
495b032f27cSSam Leffler 			bssid = wh->i_addr3;
496b032f27cSSam Leffler 		}
497b032f27cSSam Leffler 		/*
498b032f27cSSam Leffler 		 * Validate the bssid.
499b032f27cSSam Leffler 		 */
500b032f27cSSam Leffler 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
501b032f27cSSam Leffler 		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
502b032f27cSSam Leffler 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
503b032f27cSSam Leffler 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
504b032f27cSSam Leffler 			/* not interested in */
505b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
506b032f27cSSam Leffler 			    bssid, NULL, "%s", "not to bss");
507b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongbss++;
508b032f27cSSam Leffler 			goto out;
509b032f27cSSam Leffler 		}
510b032f27cSSam Leffler 
511b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
512b032f27cSSam Leffler 		ni->ni_noise = noise;
513b032f27cSSam Leffler 		ni->ni_rstamp = rstamp;
514b032f27cSSam Leffler 		if (HAS_SEQ(type)) {
515b032f27cSSam Leffler 			uint8_t tid = ieee80211_gettid(wh);
516b032f27cSSam Leffler 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
517b032f27cSSam Leffler 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
518b032f27cSSam Leffler 				ic->ic_wme.wme_hipri_traffic++;
519b032f27cSSam Leffler 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
520b032f27cSSam Leffler 			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
521b032f27cSSam Leffler 			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
522b032f27cSSam Leffler 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
523b032f27cSSam Leffler 				/* duplicate, discard */
524b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
525b032f27cSSam Leffler 				    bssid, "duplicate",
526b032f27cSSam Leffler 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
527b032f27cSSam Leffler 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
528b032f27cSSam Leffler 				    ni->ni_rxseqs[tid] >>
529b032f27cSSam Leffler 					IEEE80211_SEQ_SEQ_SHIFT,
530b032f27cSSam Leffler 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
531b032f27cSSam Leffler 				    ni->ni_rxseqs[tid] &
532b032f27cSSam Leffler 					IEEE80211_SEQ_FRAG_MASK,
533b032f27cSSam Leffler 				    tid);
534b032f27cSSam Leffler 				vap->iv_stats.is_rx_dup++;
535b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_dup);
536b032f27cSSam Leffler 				goto out;
537b032f27cSSam Leffler 			}
538b032f27cSSam Leffler 			ni->ni_rxseqs[tid] = rxseq;
539b032f27cSSam Leffler 		}
540b032f27cSSam Leffler 	}
541b032f27cSSam Leffler 
542b032f27cSSam Leffler 	switch (type) {
543b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
544b032f27cSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);
545b032f27cSSam Leffler 		if (m->m_len < hdrspace &&
546b032f27cSSam Leffler 		    (m = m_pullup(m, hdrspace)) == NULL) {
547b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
548b032f27cSSam Leffler 			    ni->ni_macaddr, NULL,
549b032f27cSSam Leffler 			    "data too short: expecting %u", hdrspace);
550b032f27cSSam Leffler 			vap->iv_stats.is_rx_tooshort++;
551b032f27cSSam Leffler 			goto out;		/* XXX */
552b032f27cSSam Leffler 		}
553b032f27cSSam Leffler 		if (!(dir == IEEE80211_FC1_DIR_TODS ||
554b032f27cSSam Leffler 		     (dir == IEEE80211_FC1_DIR_DSTODS &&
555b032f27cSSam Leffler 		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
556b032f27cSSam Leffler 			if (dir != IEEE80211_FC1_DIR_DSTODS) {
557b032f27cSSam Leffler 				IEEE80211_DISCARD(vap,
558b032f27cSSam Leffler 				    IEEE80211_MSG_INPUT, wh, "data",
559b032f27cSSam Leffler 				    "incorrect dir 0x%x", dir);
560b032f27cSSam Leffler 			} else {
561b032f27cSSam Leffler 				IEEE80211_DISCARD(vap,
562b032f27cSSam Leffler 				    IEEE80211_MSG_INPUT |
563b032f27cSSam Leffler 				    IEEE80211_MSG_WDS, wh,
564b032f27cSSam Leffler 				    "4-address data",
565b032f27cSSam Leffler 				    "%s", "DWDS not enabled");
566b032f27cSSam Leffler 			}
567b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongdir++;
568b032f27cSSam Leffler 			goto out;
569b032f27cSSam Leffler 		}
570b032f27cSSam Leffler 		/* check if source STA is associated */
571b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
572b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
573b032f27cSSam Leffler 			    wh, "data", "%s", "unknown src");
574b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
575b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
576b032f27cSSam Leffler 			    IEEE80211_REASON_NOT_AUTHED);
577b032f27cSSam Leffler 			vap->iv_stats.is_rx_notassoc++;
578b032f27cSSam Leffler 			goto err;
579b032f27cSSam Leffler 		}
580b032f27cSSam Leffler 		if (ni->ni_associd == 0) {
581b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
582b032f27cSSam Leffler 			    wh, "data", "%s", "unassoc src");
583b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
584b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
585b032f27cSSam Leffler 			    IEEE80211_REASON_NOT_ASSOCED);
586b032f27cSSam Leffler 			vap->iv_stats.is_rx_notassoc++;
587b032f27cSSam Leffler 			goto err;
588b032f27cSSam Leffler 		}
589b032f27cSSam Leffler 
590b032f27cSSam Leffler 		/*
591b032f27cSSam Leffler 		 * Check for power save state change.
592b032f27cSSam Leffler 		 * XXX out-of-order A-MPDU frames?
593b032f27cSSam Leffler 		 */
594b032f27cSSam Leffler 		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
595b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
596b032f27cSSam Leffler 			ieee80211_node_pwrsave(ni,
597b032f27cSSam Leffler 				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
598b032f27cSSam Leffler 		/*
599b032f27cSSam Leffler 		 * For 4-address packets handle WDS discovery
600b032f27cSSam Leffler 		 * notifications.  Once a WDS link is setup frames
601b032f27cSSam Leffler 		 * are just delivered to the WDS vap (see below).
602b032f27cSSam Leffler 		 */
603b032f27cSSam Leffler 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
604b032f27cSSam Leffler 			if (!ieee80211_node_is_authorized(ni)) {
605b032f27cSSam Leffler 				IEEE80211_DISCARD(vap,
606b032f27cSSam Leffler 				    IEEE80211_MSG_INPUT |
607b032f27cSSam Leffler 				    IEEE80211_MSG_WDS, wh,
608b032f27cSSam Leffler 				    "4-address data",
609b032f27cSSam Leffler 				    "%s", "unauthorized port");
610b032f27cSSam Leffler 				vap->iv_stats.is_rx_unauth++;
611b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
612b032f27cSSam Leffler 				goto err;
613b032f27cSSam Leffler 			}
614b032f27cSSam Leffler 			ieee80211_dwds_discover(ni, m);
615b032f27cSSam Leffler 			return type;
616b032f27cSSam Leffler 		}
617b032f27cSSam Leffler 
618b032f27cSSam Leffler 		/*
61945f856e3SSam Leffler 		 * Handle A-MPDU re-ordering.  If the frame is to be
62045f856e3SSam Leffler 		 * processed directly then ieee80211_ampdu_reorder
621b032f27cSSam Leffler 		 * will return 0; otherwise it has consumed the mbuf
622b032f27cSSam Leffler 		 * and we should do nothing more with it.
623b032f27cSSam Leffler 		 */
62445f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU) &&
625b032f27cSSam Leffler 		    ieee80211_ampdu_reorder(ni, m) != 0) {
626b032f27cSSam Leffler 			m = NULL;
627b032f27cSSam Leffler 			goto out;
628b032f27cSSam Leffler 		}
629b032f27cSSam Leffler 	resubmit_ampdu:
630b032f27cSSam Leffler 
631b032f27cSSam Leffler 		/*
632b032f27cSSam Leffler 		 * Handle privacy requirements.  Note that we
633b032f27cSSam Leffler 		 * must not be preempted from here until after
634b032f27cSSam Leffler 		 * we (potentially) call ieee80211_crypto_demic;
635b032f27cSSam Leffler 		 * otherwise we may violate assumptions in the
636b032f27cSSam Leffler 		 * crypto cipher modules used to do delayed update
637b032f27cSSam Leffler 		 * of replay sequence numbers.
638b032f27cSSam Leffler 		 */
639b032f27cSSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
640b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
641b032f27cSSam Leffler 				/*
642b032f27cSSam Leffler 				 * Discard encrypted frames when privacy is off.
643b032f27cSSam Leffler 				 */
644b032f27cSSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
645b032f27cSSam Leffler 				    wh, "WEP", "%s", "PRIVACY off");
646b032f27cSSam Leffler 				vap->iv_stats.is_rx_noprivacy++;
647b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
648b032f27cSSam Leffler 				goto out;
649b032f27cSSam Leffler 			}
650b032f27cSSam Leffler 			key = ieee80211_crypto_decap(ni, m, hdrspace);
651b032f27cSSam Leffler 			if (key == NULL) {
652b032f27cSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
653b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_wepfail);
654b032f27cSSam Leffler 				goto out;
655b032f27cSSam Leffler 			}
656b032f27cSSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
657b032f27cSSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
658b032f27cSSam Leffler 		} else {
659b032f27cSSam Leffler 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
660b032f27cSSam Leffler 			key = NULL;
661b032f27cSSam Leffler 		}
662b032f27cSSam Leffler 
663b032f27cSSam Leffler 		/*
664b032f27cSSam Leffler 		 * Save QoS bits for use below--before we strip the header.
665b032f27cSSam Leffler 		 */
666b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
667b032f27cSSam Leffler 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
668b032f27cSSam Leffler 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
669b032f27cSSam Leffler 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
670b032f27cSSam Leffler 		} else
671b032f27cSSam Leffler 			qos = 0;
672b032f27cSSam Leffler 
673b032f27cSSam Leffler 		/*
674b032f27cSSam Leffler 		 * Next up, any fragmentation.
675b032f27cSSam Leffler 		 */
676b032f27cSSam Leffler 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
677b032f27cSSam Leffler 			m = ieee80211_defrag(ni, m, hdrspace);
678b032f27cSSam Leffler 			if (m == NULL) {
679b032f27cSSam Leffler 				/* Fragment dropped or frame not complete yet */
680b032f27cSSam Leffler 				goto out;
681b032f27cSSam Leffler 			}
682b032f27cSSam Leffler 		}
683b032f27cSSam Leffler 		wh = NULL;		/* no longer valid, catch any uses */
684b032f27cSSam Leffler 
685b032f27cSSam Leffler 		/*
686b032f27cSSam Leffler 		 * Next strip any MSDU crypto bits.
687b032f27cSSam Leffler 		 */
688b032f27cSSam Leffler 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
689b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
690b032f27cSSam Leffler 			    ni->ni_macaddr, "data", "%s", "demic error");
691b032f27cSSam Leffler 			vap->iv_stats.is_rx_demicfail++;
692b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_demicfail);
693b032f27cSSam Leffler 			goto out;
694b032f27cSSam Leffler 		}
695b032f27cSSam Leffler 		/* copy to listener after decrypt */
696b032f27cSSam Leffler 		if (bpf_peers_present(vap->iv_rawbpf))
697b032f27cSSam Leffler 			bpf_mtap(vap->iv_rawbpf, m);
698b032f27cSSam Leffler 		need_tap = 0;
699b032f27cSSam Leffler 		/*
700b032f27cSSam Leffler 		 * Finally, strip the 802.11 header.
701b032f27cSSam Leffler 		 */
702b032f27cSSam Leffler 		m = ieee80211_decap(vap, m, hdrspace);
703b032f27cSSam Leffler 		if (m == NULL) {
704b032f27cSSam Leffler 			/* XXX mask bit to check for both */
705b032f27cSSam Leffler 			/* don't count Null data frames as errors */
706b032f27cSSam Leffler 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
707b032f27cSSam Leffler 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
708b032f27cSSam Leffler 				goto out;
709b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
710b032f27cSSam Leffler 			    ni->ni_macaddr, "data", "%s", "decap error");
711b032f27cSSam Leffler 			vap->iv_stats.is_rx_decap++;
712b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_decap);
713b032f27cSSam Leffler 			goto err;
714b032f27cSSam Leffler 		}
715b032f27cSSam Leffler 		eh = mtod(m, struct ether_header *);
716b032f27cSSam Leffler 		if (!ieee80211_node_is_authorized(ni)) {
717b032f27cSSam Leffler 			/*
718b032f27cSSam Leffler 			 * Deny any non-PAE frames received prior to
719b032f27cSSam Leffler 			 * authorization.  For open/shared-key
720b032f27cSSam Leffler 			 * authentication the port is mark authorized
721b032f27cSSam Leffler 			 * after authentication completes.  For 802.1x
722b032f27cSSam Leffler 			 * the port is not marked authorized by the
723b032f27cSSam Leffler 			 * authenticator until the handshake has completed.
724b032f27cSSam Leffler 			 */
725b032f27cSSam Leffler 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
726b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
727b032f27cSSam Leffler 				    eh->ether_shost, "data",
728b032f27cSSam Leffler 				    "unauthorized port: ether type 0x%x len %u",
729b032f27cSSam Leffler 				    eh->ether_type, m->m_pkthdr.len);
730b032f27cSSam Leffler 				vap->iv_stats.is_rx_unauth++;
731b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
732b032f27cSSam Leffler 				goto err;
733b032f27cSSam Leffler 			}
734b032f27cSSam Leffler 		} else {
735b032f27cSSam Leffler 			/*
736b032f27cSSam Leffler 			 * When denying unencrypted frames, discard
737b032f27cSSam Leffler 			 * any non-PAE frames received without encryption.
738b032f27cSSam Leffler 			 */
739b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
740b032f27cSSam Leffler 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
741b032f27cSSam Leffler 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
742b032f27cSSam Leffler 				/*
743b032f27cSSam Leffler 				 * Drop unencrypted frames.
744b032f27cSSam Leffler 				 */
745b032f27cSSam Leffler 				vap->iv_stats.is_rx_unencrypted++;
746b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
747b032f27cSSam Leffler 				goto out;
748b032f27cSSam Leffler 			}
749b032f27cSSam Leffler 		}
750b032f27cSSam Leffler 		/* XXX require HT? */
751b032f27cSSam Leffler 		if (qos & IEEE80211_QOS_AMSDU) {
752b032f27cSSam Leffler 			m = ieee80211_decap_amsdu(ni, m);
753b032f27cSSam Leffler 			if (m == NULL)
754b032f27cSSam Leffler 				return IEEE80211_FC0_TYPE_DATA;
755b032f27cSSam Leffler 		} else if ((ni->ni_ath_flags & IEEE80211_NODE_FF) &&
756b032f27cSSam Leffler #define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
757b032f27cSSam Leffler 		    m->m_pkthdr.len >= 3*FF_LLC_SIZE) {
758b032f27cSSam Leffler 			struct llc *llc;
759b032f27cSSam Leffler 
760b032f27cSSam Leffler 			/*
761b032f27cSSam Leffler 			 * Check for fast-frame tunnel encapsulation.
762b032f27cSSam Leffler 			 */
763b032f27cSSam Leffler 			if (m->m_len < FF_LLC_SIZE &&
764b032f27cSSam Leffler 			    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
765b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
766b032f27cSSam Leffler 				    ni->ni_macaddr, "fast-frame",
767b032f27cSSam Leffler 				    "%s", "m_pullup(llc) failed");
768b032f27cSSam Leffler 				vap->iv_stats.is_rx_tooshort++;
769b032f27cSSam Leffler 				return IEEE80211_FC0_TYPE_DATA;
770b032f27cSSam Leffler 			}
771b032f27cSSam Leffler 			llc = (struct llc *)(mtod(m, uint8_t *) +
772b032f27cSSam Leffler 				sizeof(struct ether_header));
773b032f27cSSam Leffler 			if (llc->llc_snap.ether_type == htons(ATH_FF_ETH_TYPE)) {
774b032f27cSSam Leffler 				m_adj(m, FF_LLC_SIZE);
775b032f27cSSam Leffler 				m = ieee80211_decap_fastframe(ni, m);
776b032f27cSSam Leffler 				if (m == NULL)
777b032f27cSSam Leffler 					return IEEE80211_FC0_TYPE_DATA;
778b032f27cSSam Leffler 			}
779b032f27cSSam Leffler 		}
780b032f27cSSam Leffler #undef FF_LLC_SIZE
781b032f27cSSam Leffler 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
782b032f27cSSam Leffler 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
783b032f27cSSam Leffler 		else
784b032f27cSSam Leffler 			hostap_deliver_data(vap, ni, m);
785b032f27cSSam Leffler 		return IEEE80211_FC0_TYPE_DATA;
786b032f27cSSam Leffler 
787b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
788b032f27cSSam Leffler 		vap->iv_stats.is_rx_mgmt++;
789b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mgmt);
790b032f27cSSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
791b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
792b032f27cSSam Leffler 			    wh, "mgt", "incorrect dir 0x%x", dir);
793b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongdir++;
794b032f27cSSam Leffler 			goto err;
795b032f27cSSam Leffler 		}
796b032f27cSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
797b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
798b032f27cSSam Leffler 			    ni->ni_macaddr, "mgt", "too short: len %u",
799b032f27cSSam Leffler 			    m->m_pkthdr.len);
800b032f27cSSam Leffler 			vap->iv_stats.is_rx_tooshort++;
801b032f27cSSam Leffler 			goto out;
802b032f27cSSam Leffler 		}
803b032f27cSSam Leffler 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
804b032f27cSSam Leffler 			/* ensure return frames are unicast */
805b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
806b032f27cSSam Leffler 			    wh, NULL, "source is multicast: %s",
807b032f27cSSam Leffler 			    ether_sprintf(wh->i_addr2));
808b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
809b032f27cSSam Leffler 			goto out;
810b032f27cSSam Leffler 		}
811b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
812b032f27cSSam Leffler 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
813b032f27cSSam Leffler 		    ieee80211_msg_dumppkts(vap)) {
814b032f27cSSam Leffler 			if_printf(ifp, "received %s from %s rssi %d\n",
815b032f27cSSam Leffler 			    ieee80211_mgt_subtype_name[subtype >>
816b032f27cSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
817b032f27cSSam Leffler 			    ether_sprintf(wh->i_addr2), rssi);
818b032f27cSSam Leffler 		}
819b032f27cSSam Leffler #endif
820b032f27cSSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
821b032f27cSSam Leffler 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
822b032f27cSSam Leffler 				/*
823b032f27cSSam Leffler 				 * Only shared key auth frames with a challenge
824b032f27cSSam Leffler 				 * should be encrypted, discard all others.
825b032f27cSSam Leffler 				 */
826b032f27cSSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
827b032f27cSSam Leffler 				    wh, NULL,
828b032f27cSSam Leffler 				    "%s", "WEP set but not permitted");
829b032f27cSSam Leffler 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
830b032f27cSSam Leffler 				goto out;
831b032f27cSSam Leffler 			}
832b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
833b032f27cSSam Leffler 				/*
834b032f27cSSam Leffler 				 * Discard encrypted frames when privacy is off.
835b032f27cSSam Leffler 				 */
836b032f27cSSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
837b032f27cSSam Leffler 				    wh, NULL, "%s", "WEP set but PRIVACY off");
838b032f27cSSam Leffler 				vap->iv_stats.is_rx_noprivacy++;
839b032f27cSSam Leffler 				goto out;
840b032f27cSSam Leffler 			}
841b032f27cSSam Leffler 			hdrspace = ieee80211_hdrspace(ic, wh);
842b032f27cSSam Leffler 			key = ieee80211_crypto_decap(ni, m, hdrspace);
843b032f27cSSam Leffler 			if (key == NULL) {
844b032f27cSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
845b032f27cSSam Leffler 				goto out;
846b032f27cSSam Leffler 			}
847b032f27cSSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
848b032f27cSSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
849b032f27cSSam Leffler 		}
850b032f27cSSam Leffler 		if (bpf_peers_present(vap->iv_rawbpf))
851b032f27cSSam Leffler 			bpf_mtap(vap->iv_rawbpf, m);
852b032f27cSSam Leffler 		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
853b032f27cSSam Leffler 		m_freem(m);
854b032f27cSSam Leffler 		return IEEE80211_FC0_TYPE_MGT;
855b032f27cSSam Leffler 
856b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
857b032f27cSSam Leffler 		vap->iv_stats.is_rx_ctl++;
858b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ctrl);
859b032f27cSSam Leffler 		switch (subtype) {
860b032f27cSSam Leffler 		case IEEE80211_FC0_SUBTYPE_PS_POLL:
861b032f27cSSam Leffler 			hostap_recv_pspoll(ni, m);
862b032f27cSSam Leffler 			break;
863b032f27cSSam Leffler 		case IEEE80211_FC0_SUBTYPE_BAR:
864b032f27cSSam Leffler 			ieee80211_recv_bar(ni, m);
865b032f27cSSam Leffler 			break;
866b032f27cSSam Leffler 		}
867b032f27cSSam Leffler 		goto out;
868b032f27cSSam Leffler 	default:
869b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
870b032f27cSSam Leffler 		    wh, "bad", "frame type 0x%x", type);
871b032f27cSSam Leffler 		/* should not come here */
872b032f27cSSam Leffler 		break;
873b032f27cSSam Leffler 	}
874b032f27cSSam Leffler err:
875b032f27cSSam Leffler 	ifp->if_ierrors++;
876b032f27cSSam Leffler out:
877b032f27cSSam Leffler 	if (m != NULL) {
878b032f27cSSam Leffler 		if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
879b032f27cSSam Leffler 			bpf_mtap(vap->iv_rawbpf, m);
880b032f27cSSam Leffler 		m_freem(m);
881b032f27cSSam Leffler 	}
882b032f27cSSam Leffler 	return type;
883b032f27cSSam Leffler #undef SEQ_LEQ
884b032f27cSSam Leffler }
885b032f27cSSam Leffler 
886b032f27cSSam Leffler static void
887b032f27cSSam Leffler hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
888b032f27cSSam Leffler     int rssi, int noise, uint32_t rstamp, uint16_t seq, uint16_t status)
889b032f27cSSam Leffler {
890b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
891b032f27cSSam Leffler 
892b032f27cSSam Leffler 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
893b032f27cSSam Leffler 
894b032f27cSSam Leffler 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
895b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
896b032f27cSSam Leffler 		    ni->ni_macaddr, "open auth",
897b032f27cSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
898b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
899b032f27cSSam Leffler 		/*
900b032f27cSSam Leffler 		 * Clear any challenge text that may be there if
901b032f27cSSam Leffler 		 * a previous shared key auth failed and then an
902b032f27cSSam Leffler 		 * open auth is attempted.
903b032f27cSSam Leffler 		 */
904b032f27cSSam Leffler 		if (ni->ni_challenge != NULL) {
905b032f27cSSam Leffler 			FREE(ni->ni_challenge, M_80211_NODE);
906b032f27cSSam Leffler 			ni->ni_challenge = NULL;
907b032f27cSSam Leffler 		}
908b032f27cSSam Leffler 		/* XXX hack to workaround calling convention */
909b032f27cSSam Leffler 		ieee80211_send_error(ni, wh->i_addr2,
910b032f27cSSam Leffler 		    IEEE80211_FC0_SUBTYPE_AUTH,
911b032f27cSSam Leffler 		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
912b032f27cSSam Leffler 		return;
913b032f27cSSam Leffler 	}
914b032f27cSSam Leffler 	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
915b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;
916b032f27cSSam Leffler 		return;
917b032f27cSSam Leffler 	}
918b032f27cSSam Leffler 	/* always accept open authentication requests */
919b032f27cSSam Leffler 	if (ni == vap->iv_bss) {
920b032f27cSSam Leffler 		ni = ieee80211_dup_bss(vap, wh->i_addr2);
921b032f27cSSam Leffler 		if (ni == NULL)
922b032f27cSSam Leffler 			return;
923b032f27cSSam Leffler 	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
924b032f27cSSam Leffler 		(void) ieee80211_ref_node(ni);
925b032f27cSSam Leffler 	/*
926b032f27cSSam Leffler 	 * Mark the node as referenced to reflect that it's
927b032f27cSSam Leffler 	 * reference count has been bumped to insure it remains
928b032f27cSSam Leffler 	 * after the transaction completes.
929b032f27cSSam Leffler 	 */
930b032f27cSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AREF;
931b032f27cSSam Leffler 
932b032f27cSSam Leffler 	if (vap->iv_acl != NULL &&
933b032f27cSSam Leffler 	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
934b032f27cSSam Leffler 		/*
935b032f27cSSam Leffler 		 * When the ACL policy is set to RADIUS we defer the
936b032f27cSSam Leffler 		 * authorization to a user agent.  Dispatch an event,
937b032f27cSSam Leffler 		 * a subsequent MLME call will decide the fate of the
938b032f27cSSam Leffler 		 * station.  If the user agent is not present then the
939b032f27cSSam Leffler 		 * node will be reclaimed due to inactivity.
940b032f27cSSam Leffler 		 */
941b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap,
942b032f27cSSam Leffler 		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
943b032f27cSSam Leffler 		    "%s", "station authentication defered (radius acl)");
944b032f27cSSam Leffler 		ieee80211_notify_node_auth(ni);
945b032f27cSSam Leffler 	} else {
946b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
947b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap,
948b032f27cSSam Leffler 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
949b032f27cSSam Leffler 		    "%s", "station authenticated (open)");
950b032f27cSSam Leffler 		/*
951b032f27cSSam Leffler 		 * When 802.1x is not in use mark the port
952b032f27cSSam Leffler 		 * authorized at this point so traffic can flow.
953b032f27cSSam Leffler 		 */
954b032f27cSSam Leffler 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
955b032f27cSSam Leffler 			ieee80211_node_authorize(ni);
956b032f27cSSam Leffler 	}
957b032f27cSSam Leffler }
958b032f27cSSam Leffler 
959b032f27cSSam Leffler static void
960b032f27cSSam Leffler hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
961b032f27cSSam Leffler     uint8_t *frm, uint8_t *efrm, int rssi, int noise, uint32_t rstamp,
962b032f27cSSam Leffler     uint16_t seq, uint16_t status)
963b032f27cSSam Leffler {
964b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
965b032f27cSSam Leffler 	uint8_t *challenge;
966b032f27cSSam Leffler 	int allocbs, estatus;
967b032f27cSSam Leffler 
968b032f27cSSam Leffler 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
969b032f27cSSam Leffler 
970b032f27cSSam Leffler 	/*
971b032f27cSSam Leffler 	 * NB: this can happen as we allow pre-shared key
972b032f27cSSam Leffler 	 * authentication to be enabled w/o wep being turned
973b032f27cSSam Leffler 	 * on so that configuration of these can be done
974b032f27cSSam Leffler 	 * in any order.  It may be better to enforce the
975b032f27cSSam Leffler 	 * ordering in which case this check would just be
976b032f27cSSam Leffler 	 * for sanity/consistency.
977b032f27cSSam Leffler 	 */
978b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
979b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
980b032f27cSSam Leffler 		    ni->ni_macaddr, "shared key auth",
981b032f27cSSam Leffler 		    "%s", " PRIVACY is disabled");
982b032f27cSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
983b032f27cSSam Leffler 		goto bad;
984b032f27cSSam Leffler 	}
985b032f27cSSam Leffler 	/*
986b032f27cSSam Leffler 	 * Pre-shared key authentication is evil; accept
987b032f27cSSam Leffler 	 * it only if explicitly configured (it is supported
988b032f27cSSam Leffler 	 * mainly for compatibility with clients like Mac OS X).
989b032f27cSSam Leffler 	 */
990b032f27cSSam Leffler 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
991b032f27cSSam Leffler 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
992b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
993b032f27cSSam Leffler 		    ni->ni_macaddr, "shared key auth",
994b032f27cSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
995b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
996b032f27cSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
997b032f27cSSam Leffler 		goto bad;
998b032f27cSSam Leffler 	}
999b032f27cSSam Leffler 
1000b032f27cSSam Leffler 	challenge = NULL;
1001b032f27cSSam Leffler 	if (frm + 1 < efrm) {
1002b032f27cSSam Leffler 		if ((frm[1] + 2) > (efrm - frm)) {
1003b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1004b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key auth",
1005b032f27cSSam Leffler 			    "ie %d/%d too long",
1006b032f27cSSam Leffler 			    frm[0], (frm[1] + 2) - (efrm - frm));
1007b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1008b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1009b032f27cSSam Leffler 			goto bad;
1010b032f27cSSam Leffler 		}
1011b032f27cSSam Leffler 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1012b032f27cSSam Leffler 			challenge = frm;
1013b032f27cSSam Leffler 		frm += frm[1] + 2;
1014b032f27cSSam Leffler 	}
1015b032f27cSSam Leffler 	switch (seq) {
1016b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1017b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
1018b032f27cSSam Leffler 		if (challenge == NULL) {
1019b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1020b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key auth",
1021b032f27cSSam Leffler 			    "%s", "no challenge");
1022b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1023b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1024b032f27cSSam Leffler 			goto bad;
1025b032f27cSSam Leffler 		}
1026b032f27cSSam Leffler 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1027b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1028b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key auth",
1029b032f27cSSam Leffler 			    "bad challenge len %d", challenge[1]);
1030b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1031b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1032b032f27cSSam Leffler 			goto bad;
1033b032f27cSSam Leffler 		}
1034b032f27cSSam Leffler 	default:
1035b032f27cSSam Leffler 		break;
1036b032f27cSSam Leffler 	}
1037b032f27cSSam Leffler 	switch (seq) {
1038b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_REQUEST:
1039b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
1040b032f27cSSam Leffler 			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1041b032f27cSSam Leffler 			if (ni == NULL) {
1042b032f27cSSam Leffler 				/* NB: no way to return an error */
1043b032f27cSSam Leffler 				return;
1044b032f27cSSam Leffler 			}
1045b032f27cSSam Leffler 			allocbs = 1;
1046b032f27cSSam Leffler 		} else {
1047b032f27cSSam Leffler 			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1048b032f27cSSam Leffler 				(void) ieee80211_ref_node(ni);
1049b032f27cSSam Leffler 			allocbs = 0;
1050b032f27cSSam Leffler 		}
1051b032f27cSSam Leffler 		/*
1052b032f27cSSam Leffler 		 * Mark the node as referenced to reflect that it's
1053b032f27cSSam Leffler 		 * reference count has been bumped to insure it remains
1054b032f27cSSam Leffler 		 * after the transaction completes.
1055b032f27cSSam Leffler 		 */
1056b032f27cSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_AREF;
1057b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1058b032f27cSSam Leffler 		ni->ni_noise = noise;
1059b032f27cSSam Leffler 		ni->ni_rstamp = rstamp;
1060b032f27cSSam Leffler 		if (!ieee80211_alloc_challenge(ni)) {
1061b032f27cSSam Leffler 			/* NB: don't return error so they rexmit */
1062b032f27cSSam Leffler 			return;
1063b032f27cSSam Leffler 		}
1064b032f27cSSam Leffler 		get_random_bytes(ni->ni_challenge,
1065b032f27cSSam Leffler 			IEEE80211_CHALLENGE_LEN);
1066b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1067b032f27cSSam Leffler 		    ni, "shared key %sauth request", allocbs ? "" : "re");
1068b032f27cSSam Leffler 		/*
1069b032f27cSSam Leffler 		 * When the ACL policy is set to RADIUS we defer the
1070b032f27cSSam Leffler 		 * authorization to a user agent.  Dispatch an event,
1071b032f27cSSam Leffler 		 * a subsequent MLME call will decide the fate of the
1072b032f27cSSam Leffler 		 * station.  If the user agent is not present then the
1073b032f27cSSam Leffler 		 * node will be reclaimed due to inactivity.
1074b032f27cSSam Leffler 		 */
1075b032f27cSSam Leffler 		if (vap->iv_acl != NULL &&
1076b032f27cSSam Leffler 		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1077b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap,
1078b032f27cSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1079b032f27cSSam Leffler 			    ni->ni_macaddr,
1080b032f27cSSam Leffler 			    "%s", "station authentication defered (radius acl)");
1081b032f27cSSam Leffler 			ieee80211_notify_node_auth(ni);
1082b032f27cSSam Leffler 			return;
1083b032f27cSSam Leffler 		}
1084b032f27cSSam Leffler 		break;
1085b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
1086b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
1087b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1088b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key response",
1089b032f27cSSam Leffler 			    "%s", "unknown station");
1090b032f27cSSam Leffler 			/* NB: don't send a response */
1091b032f27cSSam Leffler 			return;
1092b032f27cSSam Leffler 		}
1093b032f27cSSam Leffler 		if (ni->ni_challenge == NULL) {
1094b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1095b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key response",
1096b032f27cSSam Leffler 			    "%s", "no challenge recorded");
1097b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1098b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1099b032f27cSSam Leffler 			goto bad;
1100b032f27cSSam Leffler 		}
1101b032f27cSSam Leffler 		if (memcmp(ni->ni_challenge, &challenge[2],
1102b032f27cSSam Leffler 			   challenge[1]) != 0) {
1103b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1104b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key response",
1105b032f27cSSam Leffler 			    "%s", "challenge mismatch");
1106b032f27cSSam Leffler 			vap->iv_stats.is_rx_auth_fail++;
1107b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1108b032f27cSSam Leffler 			goto bad;
1109b032f27cSSam Leffler 		}
1110b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1111b032f27cSSam Leffler 		    ni, "%s", "station authenticated (shared key)");
1112b032f27cSSam Leffler 		ieee80211_node_authorize(ni);
1113b032f27cSSam Leffler 		break;
1114b032f27cSSam Leffler 	default:
1115b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1116b032f27cSSam Leffler 		    ni->ni_macaddr, "shared key auth",
1117b032f27cSSam Leffler 		    "bad seq %d", seq);
1118b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;
1119b032f27cSSam Leffler 		estatus = IEEE80211_STATUS_SEQUENCE;
1120b032f27cSSam Leffler 		goto bad;
1121b032f27cSSam Leffler 	}
1122b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1123b032f27cSSam Leffler 	return;
1124b032f27cSSam Leffler bad:
1125b032f27cSSam Leffler 	/*
1126b032f27cSSam Leffler 	 * Send an error response; but only when operating as an AP.
1127b032f27cSSam Leffler 	 */
1128b032f27cSSam Leffler 	/* XXX hack to workaround calling convention */
1129b032f27cSSam Leffler 	ieee80211_send_error(ni, wh->i_addr2,
1130b032f27cSSam Leffler 	    IEEE80211_FC0_SUBTYPE_AUTH,
1131b032f27cSSam Leffler 	    (seq + 1) | (estatus<<16));
1132b032f27cSSam Leffler }
1133b032f27cSSam Leffler 
1134b032f27cSSam Leffler /*
1135b032f27cSSam Leffler  * Convert a WPA cipher selector OUI to an internal
1136b032f27cSSam Leffler  * cipher algorithm.  Where appropriate we also
1137b032f27cSSam Leffler  * record any key length.
1138b032f27cSSam Leffler  */
1139b032f27cSSam Leffler static int
1140b032f27cSSam Leffler wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1141b032f27cSSam Leffler {
1142b032f27cSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1143b032f27cSSam Leffler 	uint32_t w = LE_READ_4(sel);
1144b032f27cSSam Leffler 
1145b032f27cSSam Leffler 	switch (w) {
1146b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_NULL):
1147b032f27cSSam Leffler 		return IEEE80211_CIPHER_NONE;
1148b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_WEP40):
1149b032f27cSSam Leffler 		if (keylen)
1150b032f27cSSam Leffler 			*keylen = 40 / NBBY;
1151b032f27cSSam Leffler 		return IEEE80211_CIPHER_WEP;
1152b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_WEP104):
1153b032f27cSSam Leffler 		if (keylen)
1154b032f27cSSam Leffler 			*keylen = 104 / NBBY;
1155b032f27cSSam Leffler 		return IEEE80211_CIPHER_WEP;
1156b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_TKIP):
1157b032f27cSSam Leffler 		return IEEE80211_CIPHER_TKIP;
1158b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_CCMP):
1159b032f27cSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
1160b032f27cSSam Leffler 	}
1161b032f27cSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
1162b032f27cSSam Leffler #undef WPA_SEL
1163b032f27cSSam Leffler }
1164b032f27cSSam Leffler 
1165b032f27cSSam Leffler /*
1166b032f27cSSam Leffler  * Convert a WPA key management/authentication algorithm
1167b032f27cSSam Leffler  * to an internal code.
1168b032f27cSSam Leffler  */
1169b032f27cSSam Leffler static int
1170b032f27cSSam Leffler wpa_keymgmt(const uint8_t *sel)
1171b032f27cSSam Leffler {
1172b032f27cSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1173b032f27cSSam Leffler 	uint32_t w = LE_READ_4(sel);
1174b032f27cSSam Leffler 
1175b032f27cSSam Leffler 	switch (w) {
1176b032f27cSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1177b032f27cSSam Leffler 		return WPA_ASE_8021X_UNSPEC;
1178b032f27cSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_PSK):
1179b032f27cSSam Leffler 		return WPA_ASE_8021X_PSK;
1180b032f27cSSam Leffler 	case WPA_SEL(WPA_ASE_NONE):
1181b032f27cSSam Leffler 		return WPA_ASE_NONE;
1182b032f27cSSam Leffler 	}
1183b032f27cSSam Leffler 	return 0;		/* NB: so is discarded */
1184b032f27cSSam Leffler #undef WPA_SEL
1185b032f27cSSam Leffler }
1186b032f27cSSam Leffler 
1187b032f27cSSam Leffler /*
1188b032f27cSSam Leffler  * Parse a WPA information element to collect parameters.
1189b032f27cSSam Leffler  * Note that we do not validate security parameters; that
1190b032f27cSSam Leffler  * is handled by the authenticator; the parsing done here
1191b032f27cSSam Leffler  * is just for internal use in making operational decisions.
1192b032f27cSSam Leffler  */
1193b032f27cSSam Leffler static int
1194b032f27cSSam Leffler ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1195b032f27cSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1196b032f27cSSam Leffler {
1197b032f27cSSam Leffler 	uint8_t len = frm[1];
1198b032f27cSSam Leffler 	uint32_t w;
1199b032f27cSSam Leffler 	int n;
1200b032f27cSSam Leffler 
1201b032f27cSSam Leffler 	/*
1202b032f27cSSam Leffler 	 * Check the length once for fixed parts: OUI, type,
1203b032f27cSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
1204b032f27cSSam Leffler 	 * Other, variable-length data, must be checked separately.
1205b032f27cSSam Leffler 	 */
1206b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1207b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1208b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1209b032f27cSSam Leffler 		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1210b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1211b032f27cSSam Leffler 	}
1212b032f27cSSam Leffler 	if (len < 14) {
1213b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1214b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1215b032f27cSSam Leffler 		    wh, "WPA", "too short, len %u", len);
1216b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1217b032f27cSSam Leffler 	}
1218b032f27cSSam Leffler 	frm += 6, len -= 4;		/* NB: len is payload only */
1219b032f27cSSam Leffler 	/* NB: iswapoui already validated the OUI and type */
1220b032f27cSSam Leffler 	w = LE_READ_2(frm);
1221b032f27cSSam Leffler 	if (w != WPA_VERSION) {
1222b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1223b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1224b032f27cSSam Leffler 		    wh, "WPA", "bad version %u", w);
1225b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1226b032f27cSSam Leffler 	}
1227b032f27cSSam Leffler 	frm += 2, len -= 2;
1228b032f27cSSam Leffler 
1229b032f27cSSam Leffler 	memset(rsn, 0, sizeof(*rsn));
1230b032f27cSSam Leffler 
1231b032f27cSSam Leffler 	/* multicast/group cipher */
1232b032f27cSSam Leffler 	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1233b032f27cSSam Leffler 	frm += 4, len -= 4;
1234b032f27cSSam Leffler 
1235b032f27cSSam Leffler 	/* unicast ciphers */
1236b032f27cSSam Leffler 	n = LE_READ_2(frm);
1237b032f27cSSam Leffler 	frm += 2, len -= 2;
1238b032f27cSSam Leffler 	if (len < n*4+2) {
1239b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1240b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1241b032f27cSSam Leffler 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1242b032f27cSSam Leffler 		    len, n);
1243b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1244b032f27cSSam Leffler 	}
1245b032f27cSSam Leffler 	w = 0;
1246b032f27cSSam Leffler 	for (; n > 0; n--) {
1247b032f27cSSam Leffler 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1248b032f27cSSam Leffler 		frm += 4, len -= 4;
1249b032f27cSSam Leffler 	}
1250b032f27cSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1251b032f27cSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1252b032f27cSSam Leffler 	else
1253b032f27cSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1254b032f27cSSam Leffler 
1255b032f27cSSam Leffler 	/* key management algorithms */
1256b032f27cSSam Leffler 	n = LE_READ_2(frm);
1257b032f27cSSam Leffler 	frm += 2, len -= 2;
1258b032f27cSSam Leffler 	if (len < n*4) {
1259b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1260b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1261b032f27cSSam Leffler 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1262b032f27cSSam Leffler 		    len, n);
1263b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1264b032f27cSSam Leffler 	}
1265b032f27cSSam Leffler 	w = 0;
1266b032f27cSSam Leffler 	for (; n > 0; n--) {
1267b032f27cSSam Leffler 		w |= wpa_keymgmt(frm);
1268b032f27cSSam Leffler 		frm += 4, len -= 4;
1269b032f27cSSam Leffler 	}
1270b032f27cSSam Leffler 	if (w & WPA_ASE_8021X_UNSPEC)
1271b032f27cSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1272b032f27cSSam Leffler 	else
1273b032f27cSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1274b032f27cSSam Leffler 
1275b032f27cSSam Leffler 	if (len > 2)		/* optional capabilities */
1276b032f27cSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
1277b032f27cSSam Leffler 
1278b032f27cSSam Leffler 	return 0;
1279b032f27cSSam Leffler }
1280b032f27cSSam Leffler 
1281b032f27cSSam Leffler /*
1282b032f27cSSam Leffler  * Convert an RSN cipher selector OUI to an internal
1283b032f27cSSam Leffler  * cipher algorithm.  Where appropriate we also
1284b032f27cSSam Leffler  * record any key length.
1285b032f27cSSam Leffler  */
1286b032f27cSSam Leffler static int
1287b032f27cSSam Leffler rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1288b032f27cSSam Leffler {
1289b032f27cSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1290b032f27cSSam Leffler 	uint32_t w = LE_READ_4(sel);
1291b032f27cSSam Leffler 
1292b032f27cSSam Leffler 	switch (w) {
1293b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_NULL):
1294b032f27cSSam Leffler 		return IEEE80211_CIPHER_NONE;
1295b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_WEP40):
1296b032f27cSSam Leffler 		if (keylen)
1297b032f27cSSam Leffler 			*keylen = 40 / NBBY;
1298b032f27cSSam Leffler 		return IEEE80211_CIPHER_WEP;
1299b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_WEP104):
1300b032f27cSSam Leffler 		if (keylen)
1301b032f27cSSam Leffler 			*keylen = 104 / NBBY;
1302b032f27cSSam Leffler 		return IEEE80211_CIPHER_WEP;
1303b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_TKIP):
1304b032f27cSSam Leffler 		return IEEE80211_CIPHER_TKIP;
1305b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_CCMP):
1306b032f27cSSam Leffler 		return IEEE80211_CIPHER_AES_CCM;
1307b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_WRAP):
1308b032f27cSSam Leffler 		return IEEE80211_CIPHER_AES_OCB;
1309b032f27cSSam Leffler 	}
1310b032f27cSSam Leffler 	return 32;		/* NB: so 1<< is discarded */
1311b032f27cSSam Leffler #undef WPA_SEL
1312b032f27cSSam Leffler }
1313b032f27cSSam Leffler 
1314b032f27cSSam Leffler /*
1315b032f27cSSam Leffler  * Convert an RSN key management/authentication algorithm
1316b032f27cSSam Leffler  * to an internal code.
1317b032f27cSSam Leffler  */
1318b032f27cSSam Leffler static int
1319b032f27cSSam Leffler rsn_keymgmt(const uint8_t *sel)
1320b032f27cSSam Leffler {
1321b032f27cSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1322b032f27cSSam Leffler 	uint32_t w = LE_READ_4(sel);
1323b032f27cSSam Leffler 
1324b032f27cSSam Leffler 	switch (w) {
1325b032f27cSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1326b032f27cSSam Leffler 		return RSN_ASE_8021X_UNSPEC;
1327b032f27cSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_PSK):
1328b032f27cSSam Leffler 		return RSN_ASE_8021X_PSK;
1329b032f27cSSam Leffler 	case RSN_SEL(RSN_ASE_NONE):
1330b032f27cSSam Leffler 		return RSN_ASE_NONE;
1331b032f27cSSam Leffler 	}
1332b032f27cSSam Leffler 	return 0;		/* NB: so is discarded */
1333b032f27cSSam Leffler #undef RSN_SEL
1334b032f27cSSam Leffler }
1335b032f27cSSam Leffler 
1336b032f27cSSam Leffler /*
1337b032f27cSSam Leffler  * Parse a WPA/RSN information element to collect parameters
1338b032f27cSSam Leffler  * and validate the parameters against what has been
1339b032f27cSSam Leffler  * configured for the system.
1340b032f27cSSam Leffler  */
1341b032f27cSSam Leffler static int
1342b032f27cSSam Leffler ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1343b032f27cSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1344b032f27cSSam Leffler {
1345b032f27cSSam Leffler 	uint8_t len = frm[1];
1346b032f27cSSam Leffler 	uint32_t w;
1347b032f27cSSam Leffler 	int n;
1348b032f27cSSam Leffler 
1349b032f27cSSam Leffler 	/*
1350b032f27cSSam Leffler 	 * Check the length once for fixed parts:
1351b032f27cSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
1352b032f27cSSam Leffler 	 * Other, variable-length data, must be checked separately.
1353b032f27cSSam Leffler 	 */
1354b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1355b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1356b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1357b032f27cSSam Leffler 		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1358b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1359b032f27cSSam Leffler 	}
1360b032f27cSSam Leffler 	if (len < 10) {
1361b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1362b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1363b032f27cSSam Leffler 		    wh, "RSN", "too short, len %u", len);
1364b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1365b032f27cSSam Leffler 	}
1366b032f27cSSam Leffler 	frm += 2;
1367b032f27cSSam Leffler 	w = LE_READ_2(frm);
1368b032f27cSSam Leffler 	if (w != RSN_VERSION) {
1369b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1370b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1371b032f27cSSam Leffler 		    wh, "RSN", "bad version %u", w);
1372b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1373b032f27cSSam Leffler 	}
1374b032f27cSSam Leffler 	frm += 2, len -= 2;
1375b032f27cSSam Leffler 
1376b032f27cSSam Leffler 	memset(rsn, 0, sizeof(*rsn));
1377b032f27cSSam Leffler 
1378b032f27cSSam Leffler 	/* multicast/group cipher */
1379b032f27cSSam Leffler 	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1380b032f27cSSam Leffler 	frm += 4, len -= 4;
1381b032f27cSSam Leffler 
1382b032f27cSSam Leffler 	/* unicast ciphers */
1383b032f27cSSam Leffler 	n = LE_READ_2(frm);
1384b032f27cSSam Leffler 	frm += 2, len -= 2;
1385b032f27cSSam Leffler 	if (len < n*4+2) {
1386b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1387b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1388b032f27cSSam Leffler 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1389b032f27cSSam Leffler 		    len, n);
1390b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1391b032f27cSSam Leffler 	}
1392b032f27cSSam Leffler 	w = 0;
1393b032f27cSSam Leffler 	for (; n > 0; n--) {
1394b032f27cSSam Leffler 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1395b032f27cSSam Leffler 		frm += 4, len -= 4;
1396b032f27cSSam Leffler 	}
1397b032f27cSSam Leffler 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1398b032f27cSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1399b032f27cSSam Leffler 	else
1400b032f27cSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1401b032f27cSSam Leffler 
1402b032f27cSSam Leffler 	/* key management algorithms */
1403b032f27cSSam Leffler 	n = LE_READ_2(frm);
1404b032f27cSSam Leffler 	frm += 2, len -= 2;
1405b032f27cSSam Leffler 	if (len < n*4) {
1406b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1407b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1408b032f27cSSam Leffler 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1409b032f27cSSam Leffler 		    len, n);
1410b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1411b032f27cSSam Leffler 	}
1412b032f27cSSam Leffler 	w = 0;
1413b032f27cSSam Leffler 	for (; n > 0; n--) {
1414b032f27cSSam Leffler 		w |= rsn_keymgmt(frm);
1415b032f27cSSam Leffler 		frm += 4, len -= 4;
1416b032f27cSSam Leffler 	}
1417b032f27cSSam Leffler 	if (w & RSN_ASE_8021X_UNSPEC)
1418b032f27cSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1419b032f27cSSam Leffler 	else
1420b032f27cSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1421b032f27cSSam Leffler 
1422b032f27cSSam Leffler 	/* optional RSN capabilities */
1423b032f27cSSam Leffler 	if (len > 2)
1424b032f27cSSam Leffler 		rsn->rsn_caps = LE_READ_2(frm);
1425b032f27cSSam Leffler 	/* XXXPMKID */
1426b032f27cSSam Leffler 
1427b032f27cSSam Leffler 	return 0;
1428b032f27cSSam Leffler }
1429b032f27cSSam Leffler 
1430b032f27cSSam Leffler /*
1431b032f27cSSam Leffler  * WPA/802.11i assocation request processing.
1432b032f27cSSam Leffler  */
1433b032f27cSSam Leffler static int
1434b032f27cSSam Leffler wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1435b032f27cSSam Leffler 	const struct ieee80211_frame *wh, const uint8_t *wpa,
1436b032f27cSSam Leffler 	const uint8_t *rsn, uint16_t capinfo)
1437b032f27cSSam Leffler {
1438b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1439b032f27cSSam Leffler 	uint8_t reason;
1440b032f27cSSam Leffler 	int badwparsn;
1441b032f27cSSam Leffler 
1442b032f27cSSam Leffler 	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1443b032f27cSSam Leffler 	if (wpa == NULL && rsn == NULL) {
1444b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1445b032f27cSSam Leffler 			/*
1446b032f27cSSam Leffler 			 * W-Fi Protected Setup (WPS) permits
1447b032f27cSSam Leffler 			 * clients to associate and pass EAPOL frames
1448b032f27cSSam Leffler 			 * to establish initial credentials.
1449b032f27cSSam Leffler 			 */
1450b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_WPS;
1451b032f27cSSam Leffler 			return 1;
1452b032f27cSSam Leffler 		}
1453b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1454b032f27cSSam Leffler 		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1455b032f27cSSam Leffler 			/*
1456b032f27cSSam Leffler 			 * Transitional Security Network.  Permits clients
1457b032f27cSSam Leffler 			 * to associate and use WEP while WPA is configured.
1458b032f27cSSam Leffler 			 */
1459b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_TSN;
1460b032f27cSSam Leffler 			return 1;
1461b032f27cSSam Leffler 		}
1462b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1463b032f27cSSam Leffler 		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1464b032f27cSSam Leffler 		vap->iv_stats.is_rx_assoc_badwpaie++;
1465b032f27cSSam Leffler 		reason = IEEE80211_REASON_IE_INVALID;
1466b032f27cSSam Leffler 		goto bad;
1467b032f27cSSam Leffler 	}
1468b032f27cSSam Leffler 	/* assert right association security credentials */
1469b032f27cSSam Leffler 	badwparsn = 0;			/* NB: to silence compiler */
1470b032f27cSSam Leffler 	switch (vap->iv_flags & IEEE80211_F_WPA) {
1471b032f27cSSam Leffler 	case IEEE80211_F_WPA1:
1472b032f27cSSam Leffler 		badwparsn = (wpa == NULL);
1473b032f27cSSam Leffler 		break;
1474b032f27cSSam Leffler 	case IEEE80211_F_WPA2:
1475b032f27cSSam Leffler 		badwparsn = (rsn == NULL);
1476b032f27cSSam Leffler 		break;
1477b032f27cSSam Leffler 	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1478b032f27cSSam Leffler 		badwparsn = (wpa == NULL && rsn == NULL);
1479b032f27cSSam Leffler 		break;
1480b032f27cSSam Leffler 	}
1481b032f27cSSam Leffler 	if (badwparsn) {
1482b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1483b032f27cSSam Leffler 		    wh, NULL,
1484b032f27cSSam Leffler 		    "%s", "missing WPA/RSN IE in association request");
1485b032f27cSSam Leffler 		vap->iv_stats.is_rx_assoc_badwpaie++;
1486b032f27cSSam Leffler 		reason = IEEE80211_REASON_IE_INVALID;
1487b032f27cSSam Leffler 		goto bad;
1488b032f27cSSam Leffler 	}
1489b032f27cSSam Leffler 	/*
1490b032f27cSSam Leffler 	 * Parse WPA/RSN information element.
1491b032f27cSSam Leffler 	 */
1492b032f27cSSam Leffler 	if (wpa != NULL)
1493b032f27cSSam Leffler 		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1494b032f27cSSam Leffler 	else
1495b032f27cSSam Leffler 		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1496b032f27cSSam Leffler 	if (reason != 0) {
1497b032f27cSSam Leffler 		/* XXX distinguish WPA/RSN? */
1498b032f27cSSam Leffler 		vap->iv_stats.is_rx_assoc_badwpaie++;
1499b032f27cSSam Leffler 		goto bad;
1500b032f27cSSam Leffler 	}
1501b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1502b032f27cSSam Leffler 	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1503b032f27cSSam Leffler 	    wpa != NULL ? "WPA" : "RSN",
1504b032f27cSSam Leffler 	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1505b032f27cSSam Leffler 	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1506b032f27cSSam Leffler 	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1507b032f27cSSam Leffler 
1508b032f27cSSam Leffler 	return 1;
1509b032f27cSSam Leffler bad:
1510b032f27cSSam Leffler 	ieee80211_node_deauth(ni, reason);
1511b032f27cSSam Leffler 	return 0;
1512b032f27cSSam Leffler }
1513b032f27cSSam Leffler 
1514b032f27cSSam Leffler /* XXX find a better place for definition */
1515b032f27cSSam Leffler struct l2_update_frame {
1516b032f27cSSam Leffler 	struct ether_header eh;
1517b032f27cSSam Leffler 	uint8_t dsap;
1518b032f27cSSam Leffler 	uint8_t ssap;
1519b032f27cSSam Leffler 	uint8_t control;
1520b032f27cSSam Leffler 	uint8_t xid[3];
1521b032f27cSSam Leffler }  __packed;
1522b032f27cSSam Leffler 
1523b032f27cSSam Leffler /*
1524b032f27cSSam Leffler  * Deliver a TGf L2UF frame on behalf of a station.
1525b032f27cSSam Leffler  * This primes any bridge when the station is roaming
1526b032f27cSSam Leffler  * between ap's on the same wired network.
1527b032f27cSSam Leffler  */
1528b032f27cSSam Leffler static void
1529b032f27cSSam Leffler ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1530b032f27cSSam Leffler {
1531b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1532b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1533b032f27cSSam Leffler 	struct mbuf *m;
1534b032f27cSSam Leffler 	struct l2_update_frame *l2uf;
1535b032f27cSSam Leffler 	struct ether_header *eh;
1536b032f27cSSam Leffler 
1537b032f27cSSam Leffler 	m = m_gethdr(M_NOWAIT, MT_DATA);
1538b032f27cSSam Leffler 	if (m == NULL) {
1539b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1540b032f27cSSam Leffler 		    "%s", "no mbuf for l2uf frame");
1541b032f27cSSam Leffler 		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1542b032f27cSSam Leffler 		return;
1543b032f27cSSam Leffler 	}
1544b032f27cSSam Leffler 	l2uf = mtod(m, struct l2_update_frame *);
1545b032f27cSSam Leffler 	eh = &l2uf->eh;
1546b032f27cSSam Leffler 	/* dst: Broadcast address */
1547b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1548b032f27cSSam Leffler 	/* src: associated STA */
1549b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1550b032f27cSSam Leffler 	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1551b032f27cSSam Leffler 
1552b032f27cSSam Leffler 	l2uf->dsap = 0;
1553b032f27cSSam Leffler 	l2uf->ssap = 0;
1554b032f27cSSam Leffler 	l2uf->control = 0xf5;
1555b032f27cSSam Leffler 	l2uf->xid[0] = 0x81;
1556b032f27cSSam Leffler 	l2uf->xid[1] = 0x80;
1557b032f27cSSam Leffler 	l2uf->xid[2] = 0x00;
1558b032f27cSSam Leffler 
1559b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1560b032f27cSSam Leffler 	hostap_deliver_data(vap, ni, m);
1561b032f27cSSam Leffler }
1562b032f27cSSam Leffler 
1563b032f27cSSam Leffler static void
1564b032f27cSSam Leffler ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1565b032f27cSSam Leffler 	int reassoc, int resp, const char *tag, int rate)
1566b032f27cSSam Leffler {
1567b032f27cSSam Leffler 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1568b032f27cSSam Leffler 	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1569b032f27cSSam Leffler 	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1570b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1571b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1572b032f27cSSam Leffler }
1573b032f27cSSam Leffler 
1574b032f27cSSam Leffler static void
1575b032f27cSSam Leffler capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1576b032f27cSSam Leffler 	int reassoc, int resp, const char *tag, int capinfo)
1577b032f27cSSam Leffler {
1578b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1579b032f27cSSam Leffler 
1580b032f27cSSam Leffler 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1581b032f27cSSam Leffler 	    "deny %s request, %s mismatch 0x%x",
1582b032f27cSSam Leffler 	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1583b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1584b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1585b032f27cSSam Leffler 	vap->iv_stats.is_rx_assoc_capmismatch++;
1586b032f27cSSam Leffler }
1587b032f27cSSam Leffler 
1588b032f27cSSam Leffler static void
1589b032f27cSSam Leffler htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1590b032f27cSSam Leffler 	int reassoc, int resp)
1591b032f27cSSam Leffler {
1592b032f27cSSam Leffler 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1593b032f27cSSam Leffler 	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1594b032f27cSSam Leffler 	/* XXX no better code */
1595b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_OTHER);
1596b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1597b032f27cSSam Leffler }
1598b032f27cSSam Leffler 
1599b032f27cSSam Leffler static void
1600b032f27cSSam Leffler authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1601b032f27cSSam Leffler 	int algo, int seq, int status)
1602b032f27cSSam Leffler {
1603b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1604b032f27cSSam Leffler 
1605b032f27cSSam Leffler 	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1606b032f27cSSam Leffler 	    wh, NULL, "unsupported alg %d", algo);
1607b032f27cSSam Leffler 	vap->iv_stats.is_rx_auth_unsupported++;
1608b032f27cSSam Leffler 	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1609b032f27cSSam Leffler 	    seq | (status << 16));
1610b032f27cSSam Leffler }
1611b032f27cSSam Leffler 
1612b032f27cSSam Leffler static __inline int
1613b032f27cSSam Leffler ishtmixed(const uint8_t *ie)
1614b032f27cSSam Leffler {
1615b032f27cSSam Leffler 	const struct ieee80211_ie_htinfo *ht =
1616b032f27cSSam Leffler 	    (const struct ieee80211_ie_htinfo *) ie;
1617b032f27cSSam Leffler 	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1618b032f27cSSam Leffler 	    IEEE80211_HTINFO_OPMODE_MIXED;
1619b032f27cSSam Leffler }
1620b032f27cSSam Leffler 
1621b032f27cSSam Leffler static int
1622b032f27cSSam Leffler is11bclient(const uint8_t *rates, const uint8_t *xrates)
1623b032f27cSSam Leffler {
1624b032f27cSSam Leffler 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1625b032f27cSSam Leffler 	int i;
1626b032f27cSSam Leffler 
1627b032f27cSSam Leffler 	/* NB: the 11b clients we care about will not have xrates */
1628b032f27cSSam Leffler 	if (xrates != NULL || rates == NULL)
1629b032f27cSSam Leffler 		return 0;
1630b032f27cSSam Leffler 	for (i = 0; i < rates[1]; i++) {
1631b032f27cSSam Leffler 		int r = rates[2+i] & IEEE80211_RATE_VAL;
1632b032f27cSSam Leffler 		if (r > 2*11 || ((1<<r) & brates) == 0)
1633b032f27cSSam Leffler 			return 0;
1634b032f27cSSam Leffler 	}
1635b032f27cSSam Leffler 	return 1;
1636b032f27cSSam Leffler }
1637b032f27cSSam Leffler 
1638b032f27cSSam Leffler static void
1639b032f27cSSam Leffler hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1640b032f27cSSam Leffler 	int subtype, int rssi, int noise, uint32_t rstamp)
1641b032f27cSSam Leffler {
1642b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1643b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1644b032f27cSSam Leffler 	struct ieee80211_frame *wh;
1645b032f27cSSam Leffler 	uint8_t *frm, *efrm, *sfrm;
1646b032f27cSSam Leffler 	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1647b032f27cSSam Leffler 	int reassoc, resp;
1648b032f27cSSam Leffler 	uint8_t rate;
1649b032f27cSSam Leffler 
1650b032f27cSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
1651b032f27cSSam Leffler 	frm = (uint8_t *)&wh[1];
1652b032f27cSSam Leffler 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1653b032f27cSSam Leffler 	switch (subtype) {
1654b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1655b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1656b032f27cSSam Leffler 		struct ieee80211_scanparams scan;
1657b032f27cSSam Leffler 		/*
1658b032f27cSSam Leffler 		 * We process beacon/probe response frames when scanning;
1659b032f27cSSam Leffler 		 * otherwise we check beacon frames for overlapping non-ERP
1660b032f27cSSam Leffler 		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1661b032f27cSSam Leffler 		 */
1662b032f27cSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1663b032f27cSSam Leffler 		    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1664b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
1665b032f27cSSam Leffler 			return;
1666b032f27cSSam Leffler 		}
1667b032f27cSSam Leffler 		/* NB: accept off-channel frames */
1668b032f27cSSam Leffler 		if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1669b032f27cSSam Leffler 			return;
1670b032f27cSSam Leffler 		/*
1671b032f27cSSam Leffler 		 * Count frame now that we know it's to be processed.
1672b032f27cSSam Leffler 		 */
1673b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1674b032f27cSSam Leffler 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1675b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_beacons);
1676b032f27cSSam Leffler 		} else
1677b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1678b032f27cSSam Leffler 		/*
1679b032f27cSSam Leffler 		 * If scanning, just pass information to the scan module.
1680b032f27cSSam Leffler 		 */
1681b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1682b032f27cSSam Leffler 			if (scan.status == 0 &&		/* NB: on channel */
1683b032f27cSSam Leffler 			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1684b032f27cSSam Leffler 				/*
1685b032f27cSSam Leffler 				 * Actively scanning a channel marked passive;
1686b032f27cSSam Leffler 				 * send a probe request now that we know there
1687b032f27cSSam Leffler 				 * is 802.11 traffic present.
1688b032f27cSSam Leffler 				 *
1689b032f27cSSam Leffler 				 * XXX check if the beacon we recv'd gives
1690b032f27cSSam Leffler 				 * us what we need and suppress the probe req
1691b032f27cSSam Leffler 				 */
1692b032f27cSSam Leffler 				ieee80211_probe_curchan(vap, 1);
1693b032f27cSSam Leffler 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1694b032f27cSSam Leffler 			}
1695b032f27cSSam Leffler 			ieee80211_add_scan(vap, &scan, wh,
1696b032f27cSSam Leffler 				subtype, rssi, noise, rstamp);
1697b032f27cSSam Leffler 			return;
1698b032f27cSSam Leffler 		}
1699b032f27cSSam Leffler 		/*
1700b032f27cSSam Leffler 		 * Check beacon for overlapping bss w/ non ERP stations.
1701b032f27cSSam Leffler 		 * If we detect one and protection is configured but not
1702b032f27cSSam Leffler 		 * enabled, enable it and start a timer that'll bring us
1703b032f27cSSam Leffler 		 * out if we stop seeing the bss.
1704b032f27cSSam Leffler 		 */
1705b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1706b032f27cSSam Leffler 		    scan.status == 0 &&			/* NB: on-channel */
1707b032f27cSSam Leffler 		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1708b032f27cSSam Leffler 		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1709b032f27cSSam Leffler 			ic->ic_lastnonerp = ticks;
1710b032f27cSSam Leffler 			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1711b032f27cSSam Leffler 			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1712b032f27cSSam Leffler 			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1713b032f27cSSam Leffler 				IEEE80211_NOTE_FRAME(vap,
1714b032f27cSSam Leffler 				    IEEE80211_MSG_ASSOC, wh,
1715b032f27cSSam Leffler 				    "non-ERP present on channel %d "
1716b032f27cSSam Leffler 				    "(saw erp 0x%x from channel %d), "
1717b032f27cSSam Leffler 				    "enable use of protection",
1718b032f27cSSam Leffler 				    ic->ic_curchan->ic_ieee,
1719b032f27cSSam Leffler 				    scan.erp, scan.chan);
1720b032f27cSSam Leffler 				ic->ic_flags |= IEEE80211_F_USEPROT;
1721b032f27cSSam Leffler 				ieee80211_notify_erp(ic);
1722b032f27cSSam Leffler 			}
1723b032f27cSSam Leffler 		}
1724b032f27cSSam Leffler 		/*
1725b032f27cSSam Leffler 		 * Check beacon for non-HT station on HT channel
1726b032f27cSSam Leffler 		 * and update HT BSS occupancy as appropriate.
1727b032f27cSSam Leffler 		 */
1728b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1729b032f27cSSam Leffler 			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1730b032f27cSSam Leffler 				/*
1731b032f27cSSam Leffler 				 * Off control channel; only check frames
1732b032f27cSSam Leffler 				 * that come in the extension channel when
1733b032f27cSSam Leffler 				 * operating w/ HT40.
1734b032f27cSSam Leffler 				 */
1735b032f27cSSam Leffler 				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1736b032f27cSSam Leffler 					break;
1737b032f27cSSam Leffler 				if (scan.chan != ic->ic_curchan->ic_extieee)
1738b032f27cSSam Leffler 					break;
1739b032f27cSSam Leffler 			}
1740b032f27cSSam Leffler 			if (scan.htinfo == NULL) {
1741b032f27cSSam Leffler 				ieee80211_htprot_update(ic,
1742b032f27cSSam Leffler 				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1743b032f27cSSam Leffler 				    IEEE80211_HTINFO_NONHT_PRESENT);
1744b032f27cSSam Leffler 			} else if (ishtmixed(scan.htinfo)) {
1745b032f27cSSam Leffler 				/* XXX? take NONHT_PRESENT from beacon? */
1746b032f27cSSam Leffler 				ieee80211_htprot_update(ic,
1747b032f27cSSam Leffler 				    IEEE80211_HTINFO_OPMODE_MIXED |
1748b032f27cSSam Leffler 				    IEEE80211_HTINFO_NONHT_PRESENT);
1749b032f27cSSam Leffler 			}
1750b032f27cSSam Leffler 		}
1751b032f27cSSam Leffler 		break;
1752b032f27cSSam Leffler 	}
1753b032f27cSSam Leffler 
1754b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1755b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
1756b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
1757b032f27cSSam Leffler 			return;
1758b032f27cSSam Leffler 		}
1759b032f27cSSam Leffler 		/*
1760b032f27cSSam Leffler 		 * prreq frame format
1761b032f27cSSam Leffler 		 *	[tlv] ssid
1762b032f27cSSam Leffler 		 *	[tlv] supported rates
1763b032f27cSSam Leffler 		 *	[tlv] extended supported rates
1764b032f27cSSam Leffler 		 */
1765b032f27cSSam Leffler 		ssid = rates = xrates = NULL;
1766b032f27cSSam Leffler 		sfrm = frm;
1767b032f27cSSam Leffler 		while (efrm - frm > 1) {
1768b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1769b032f27cSSam Leffler 			switch (*frm) {
1770b032f27cSSam Leffler 			case IEEE80211_ELEMID_SSID:
1771b032f27cSSam Leffler 				ssid = frm;
1772b032f27cSSam Leffler 				break;
1773b032f27cSSam Leffler 			case IEEE80211_ELEMID_RATES:
1774b032f27cSSam Leffler 				rates = frm;
1775b032f27cSSam Leffler 				break;
1776b032f27cSSam Leffler 			case IEEE80211_ELEMID_XRATES:
1777b032f27cSSam Leffler 				xrates = frm;
1778b032f27cSSam Leffler 				break;
1779b032f27cSSam Leffler 			}
1780b032f27cSSam Leffler 			frm += frm[1] + 2;
1781b032f27cSSam Leffler 		}
1782b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1783b032f27cSSam Leffler 		if (xrates != NULL)
1784b032f27cSSam Leffler 			IEEE80211_VERIFY_ELEMENT(xrates,
1785b032f27cSSam Leffler 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1786b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1787b032f27cSSam Leffler 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1788b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1789b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1790b032f27cSSam Leffler 			    wh, NULL,
1791b032f27cSSam Leffler 			    "%s", "no ssid with ssid suppression enabled");
1792b032f27cSSam Leffler 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1793b032f27cSSam Leffler 			return;
1794b032f27cSSam Leffler 		}
1795b032f27cSSam Leffler 
1796b032f27cSSam Leffler 		/* XXX find a better class or define it's own */
1797b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1798b032f27cSSam Leffler 		    "%s", "recv probe req");
1799b032f27cSSam Leffler 		/*
1800b032f27cSSam Leffler 		 * Some legacy 11b clients cannot hack a complete
1801b032f27cSSam Leffler 		 * probe response frame.  When the request includes
1802b032f27cSSam Leffler 		 * only a bare-bones rate set, communicate this to
1803b032f27cSSam Leffler 		 * the transmit side.
1804b032f27cSSam Leffler 		 */
1805b032f27cSSam Leffler 		ieee80211_send_proberesp(vap, wh->i_addr2,
1806b032f27cSSam Leffler 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1807b032f27cSSam Leffler 		break;
1808b032f27cSSam Leffler 
1809b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1810b032f27cSSam Leffler 		uint16_t algo, seq, status;
1811b032f27cSSam Leffler 
1812b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
1813b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
1814b032f27cSSam Leffler 			return;
1815b032f27cSSam Leffler 		}
1816b032f27cSSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1817b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1818b032f27cSSam Leffler 			    wh, NULL, "%s", "wrong bssid");
1819b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1820b032f27cSSam Leffler 			return;
1821b032f27cSSam Leffler 		}
1822b032f27cSSam Leffler 		/*
1823b032f27cSSam Leffler 		 * auth frame format
1824b032f27cSSam Leffler 		 *	[2] algorithm
1825b032f27cSSam Leffler 		 *	[2] sequence
1826b032f27cSSam Leffler 		 *	[2] status
1827b032f27cSSam Leffler 		 *	[tlv*] challenge
1828b032f27cSSam Leffler 		 */
1829b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1830b032f27cSSam Leffler 		algo   = le16toh(*(uint16_t *)frm);
1831b032f27cSSam Leffler 		seq    = le16toh(*(uint16_t *)(frm + 2));
1832b032f27cSSam Leffler 		status = le16toh(*(uint16_t *)(frm + 4));
1833b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1834b032f27cSSam Leffler 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1835b032f27cSSam Leffler 		/*
1836b032f27cSSam Leffler 		 * Consult the ACL policy module if setup.
1837b032f27cSSam Leffler 		 */
1838b032f27cSSam Leffler 		if (vap->iv_acl != NULL &&
1839b032f27cSSam Leffler 		    !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1840b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1841b032f27cSSam Leffler 			    wh, NULL, "%s", "disallowed by ACL");
1842b032f27cSSam Leffler 			vap->iv_stats.is_rx_acl++;
1843b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
1844b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_AUTH,
1845b032f27cSSam Leffler 			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1846b032f27cSSam Leffler 			return;
1847b032f27cSSam Leffler 		}
1848b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1849b032f27cSSam Leffler 			IEEE80211_DISCARD(vap,
1850b032f27cSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1851b032f27cSSam Leffler 			    wh, NULL, "%s", "TKIP countermeasures enabled");
1852b032f27cSSam Leffler 			vap->iv_stats.is_rx_auth_countermeasures++;
1853b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
1854b032f27cSSam Leffler 				IEEE80211_FC0_SUBTYPE_AUTH,
1855b032f27cSSam Leffler 				IEEE80211_REASON_MIC_FAILURE);
1856b032f27cSSam Leffler 			return;
1857b032f27cSSam Leffler 		}
1858b032f27cSSam Leffler 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1859b032f27cSSam Leffler 			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi,
1860b032f27cSSam Leffler 			    noise, rstamp, seq, status);
1861b032f27cSSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1862b032f27cSSam Leffler 			hostap_auth_open(ni, wh, rssi, noise, rstamp,
1863b032f27cSSam Leffler 			    seq, status);
1864b032f27cSSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1865b032f27cSSam Leffler 			authalgreject(ni, wh, algo,
1866b032f27cSSam Leffler 			    seq+1, IEEE80211_STATUS_ALG);
1867b032f27cSSam Leffler 			return;
1868b032f27cSSam Leffler 		} else {
1869b032f27cSSam Leffler 			/*
1870b032f27cSSam Leffler 			 * We assume that an unknown algorithm is the result
1871b032f27cSSam Leffler 			 * of a decryption failure on a shared key auth frame;
1872b032f27cSSam Leffler 			 * return a status code appropriate for that instead
1873b032f27cSSam Leffler 			 * of IEEE80211_STATUS_ALG.
1874b032f27cSSam Leffler 			 *
1875b032f27cSSam Leffler 			 * NB: a seq# of 4 is intentional; the decrypted
1876b032f27cSSam Leffler 			 *     frame likely has a bogus seq value.
1877b032f27cSSam Leffler 			 */
1878b032f27cSSam Leffler 			authalgreject(ni, wh, algo,
1879b032f27cSSam Leffler 			    4, IEEE80211_STATUS_CHALLENGE);
1880b032f27cSSam Leffler 			return;
1881b032f27cSSam Leffler 		}
1882b032f27cSSam Leffler 		break;
1883b032f27cSSam Leffler 	}
1884b032f27cSSam Leffler 
1885b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1886b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1887b032f27cSSam Leffler 		uint16_t capinfo, lintval;
1888b032f27cSSam Leffler 		struct ieee80211_rsnparms rsnparms;
1889b032f27cSSam Leffler 
1890b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
1891b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
1892b032f27cSSam Leffler 			return;
1893b032f27cSSam Leffler 		}
1894b032f27cSSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1895b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1896b032f27cSSam Leffler 			    wh, NULL, "%s", "wrong bssid");
1897b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_bss++;
1898b032f27cSSam Leffler 			return;
1899b032f27cSSam Leffler 		}
1900b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1901b032f27cSSam Leffler 			reassoc = 1;
1902b032f27cSSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1903b032f27cSSam Leffler 		} else {
1904b032f27cSSam Leffler 			reassoc = 0;
1905b032f27cSSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1906b032f27cSSam Leffler 		}
1907b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
1908b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1909b032f27cSSam Leffler 			    "deny %s request, sta not authenticated",
1910b032f27cSSam Leffler 			    reassoc ? "reassoc" : "assoc");
1911b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
1912b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1913b032f27cSSam Leffler 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1914b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_notauth++;
1915b032f27cSSam Leffler 			return;
1916b032f27cSSam Leffler 		}
1917b032f27cSSam Leffler 
1918b032f27cSSam Leffler 		/*
1919b032f27cSSam Leffler 		 * asreq frame format
1920b032f27cSSam Leffler 		 *	[2] capability information
1921b032f27cSSam Leffler 		 *	[2] listen interval
1922b032f27cSSam Leffler 		 *	[6*] current AP address (reassoc only)
1923b032f27cSSam Leffler 		 *	[tlv] ssid
1924b032f27cSSam Leffler 		 *	[tlv] supported rates
1925b032f27cSSam Leffler 		 *	[tlv] extended supported rates
1926b032f27cSSam Leffler 		 *	[tlv] WPA or RSN
1927b032f27cSSam Leffler 		 *	[tlv] HT capabilities
1928b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities
1929b032f27cSSam Leffler 		 */
1930b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1931b032f27cSSam Leffler 		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1932b032f27cSSam Leffler 		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1933b032f27cSSam Leffler 		if (reassoc)
1934b032f27cSSam Leffler 			frm += 6;	/* ignore current AP info */
1935b032f27cSSam Leffler 		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1936b032f27cSSam Leffler 		sfrm = frm;
1937b032f27cSSam Leffler 		while (efrm - frm > 1) {
1938b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1939b032f27cSSam Leffler 			switch (*frm) {
1940b032f27cSSam Leffler 			case IEEE80211_ELEMID_SSID:
1941b032f27cSSam Leffler 				ssid = frm;
1942b032f27cSSam Leffler 				break;
1943b032f27cSSam Leffler 			case IEEE80211_ELEMID_RATES:
1944b032f27cSSam Leffler 				rates = frm;
1945b032f27cSSam Leffler 				break;
1946b032f27cSSam Leffler 			case IEEE80211_ELEMID_XRATES:
1947b032f27cSSam Leffler 				xrates = frm;
1948b032f27cSSam Leffler 				break;
1949b032f27cSSam Leffler 			case IEEE80211_ELEMID_RSN:
1950b032f27cSSam Leffler 				rsn = frm;
1951b032f27cSSam Leffler 				break;
1952b032f27cSSam Leffler 			case IEEE80211_ELEMID_HTCAP:
1953b032f27cSSam Leffler 				htcap = frm;
1954b032f27cSSam Leffler 				break;
1955b032f27cSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
1956b032f27cSSam Leffler 				if (iswpaoui(frm))
1957b032f27cSSam Leffler 					wpa = frm;
1958b032f27cSSam Leffler 				else if (iswmeinfo(frm))
1959b032f27cSSam Leffler 					wme = frm;
1960b032f27cSSam Leffler 				else if (isatherosoui(frm))
1961b032f27cSSam Leffler 					ath = frm;
1962b032f27cSSam Leffler 				else if (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
1963b032f27cSSam Leffler 					if (ishtcapoui(frm) && htcap == NULL)
1964b032f27cSSam Leffler 						htcap = frm;
1965b032f27cSSam Leffler 				}
1966b032f27cSSam Leffler 				break;
1967b032f27cSSam Leffler 			}
1968b032f27cSSam Leffler 			frm += frm[1] + 2;
1969b032f27cSSam Leffler 		}
1970b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1971b032f27cSSam Leffler 		if (xrates != NULL)
1972b032f27cSSam Leffler 			IEEE80211_VERIFY_ELEMENT(xrates,
1973b032f27cSSam Leffler 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1974b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1975b032f27cSSam Leffler 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1976b032f27cSSam Leffler 		if (htcap != NULL) {
1977b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(htcap[1],
1978b032f27cSSam Leffler 			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
1979b032f27cSSam Leffler 			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
1980b032f27cSSam Leffler 			         sizeof(struct ieee80211_ie_htcap)-2,
1981b032f27cSSam Leffler 			     return);		/* XXX just NULL out? */
1982b032f27cSSam Leffler 		}
1983b032f27cSSam Leffler 
1984b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WPA) &&
1985b032f27cSSam Leffler 		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
1986b032f27cSSam Leffler 			return;
1987b032f27cSSam Leffler 		/* discard challenge after association */
1988b032f27cSSam Leffler 		if (ni->ni_challenge != NULL) {
1989b032f27cSSam Leffler 			FREE(ni->ni_challenge, M_80211_NODE);
1990b032f27cSSam Leffler 			ni->ni_challenge = NULL;
1991b032f27cSSam Leffler 		}
1992b032f27cSSam Leffler 		/* NB: 802.11 spec says to ignore station's privacy bit */
1993b032f27cSSam Leffler 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
1994b032f27cSSam Leffler 			capinfomismatch(ni, wh, reassoc, resp,
1995b032f27cSSam Leffler 			    "capability", capinfo);
1996b032f27cSSam Leffler 			return;
1997b032f27cSSam Leffler 		}
1998b032f27cSSam Leffler 		/*
1999b032f27cSSam Leffler 		 * Disallow re-associate w/ invalid slot time setting.
2000b032f27cSSam Leffler 		 */
2001b032f27cSSam Leffler 		if (ni->ni_associd != 0 &&
2002b032f27cSSam Leffler 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2003b032f27cSSam Leffler 		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2004b032f27cSSam Leffler 			capinfomismatch(ni, wh, reassoc, resp,
2005b032f27cSSam Leffler 			    "slot time", capinfo);
2006b032f27cSSam Leffler 			return;
2007b032f27cSSam Leffler 		}
2008b032f27cSSam Leffler 		rate = ieee80211_setup_rates(ni, rates, xrates,
2009b032f27cSSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2010b032f27cSSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2011b032f27cSSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
2012b032f27cSSam Leffler 			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2013b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_norate++;
2014b032f27cSSam Leffler 			return;
2015b032f27cSSam Leffler 		}
2016b032f27cSSam Leffler 		/*
2017b032f27cSSam Leffler 		 * If constrained to 11g-only stations reject an
2018b032f27cSSam Leffler 		 * 11b-only station.  We cheat a bit here by looking
2019b032f27cSSam Leffler 		 * at the max negotiated xmit rate and assuming anyone
2020b032f27cSSam Leffler 		 * with a best rate <24Mb/s is an 11b station.
2021b032f27cSSam Leffler 		 */
2022b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2023b032f27cSSam Leffler 			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2024b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_norate++;
2025b032f27cSSam Leffler 			return;
2026b032f27cSSam Leffler 		}
2027b032f27cSSam Leffler 		/*
2028b032f27cSSam Leffler 		 * Do HT rate set handling and setup HT node state.
2029b032f27cSSam Leffler 		 */
2030b032f27cSSam Leffler 		ni->ni_chan = vap->iv_bss->ni_chan;
2031b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2032b032f27cSSam Leffler 			rate = ieee80211_setup_htrates(ni, htcap,
2033b032f27cSSam Leffler 				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2034b032f27cSSam Leffler 				IEEE80211_F_DOBRS);
2035b032f27cSSam Leffler 			if (rate & IEEE80211_RATE_BASIC) {
2036b032f27cSSam Leffler 				ratesetmismatch(ni, wh, reassoc, resp,
2037b032f27cSSam Leffler 				    "HT", rate);
2038b032f27cSSam Leffler 				vap->iv_stats.is_ht_assoc_norate++;
2039b032f27cSSam Leffler 				return;
2040b032f27cSSam Leffler 			}
2041b032f27cSSam Leffler 			ieee80211_ht_node_init(ni, htcap);
2042b032f27cSSam Leffler 		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2043b032f27cSSam Leffler 			ieee80211_ht_node_cleanup(ni);
2044b032f27cSSam Leffler 		/*
2045b032f27cSSam Leffler 		 * Allow AMPDU operation only with unencrypted traffic
2046b032f27cSSam Leffler 		 * or AES-CCM; the 11n spec only specifies these ciphers
2047b032f27cSSam Leffler 		 * so permitting any others is undefined and can lead
2048b032f27cSSam Leffler 		 * to interoperability problems.
2049b032f27cSSam Leffler 		 */
2050b032f27cSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2051b032f27cSSam Leffler 		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2052b032f27cSSam Leffler 		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2053b032f27cSSam Leffler 		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2054b032f27cSSam Leffler 			IEEE80211_NOTE(vap,
2055b032f27cSSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2056b032f27cSSam Leffler 			    "disallow HT use because WEP or TKIP requested, "
2057b032f27cSSam Leffler 			    "capinfo 0x%x ucastcipher %d", capinfo,
2058b032f27cSSam Leffler 			    rsnparms.rsn_ucastcipher);
2059b032f27cSSam Leffler 			ieee80211_ht_node_cleanup(ni);
2060b032f27cSSam Leffler 			vap->iv_stats.is_ht_assoc_downgrade++;
2061b032f27cSSam Leffler 		}
2062b032f27cSSam Leffler 		/*
2063b032f27cSSam Leffler 		 * If constrained to 11n-only stations reject legacy stations.
2064b032f27cSSam Leffler 		 */
2065b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_PUREN) &&
2066b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2067b032f27cSSam Leffler 			htcapmismatch(ni, wh, reassoc, resp);
2068b032f27cSSam Leffler 			vap->iv_stats.is_ht_assoc_nohtcap++;
2069b032f27cSSam Leffler 			return;
2070b032f27cSSam Leffler 		}
2071b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2072b032f27cSSam Leffler 		ni->ni_noise = noise;
2073b032f27cSSam Leffler 		ni->ni_rstamp = rstamp;
2074b032f27cSSam Leffler 		ni->ni_intval = lintval;
2075b032f27cSSam Leffler 		ni->ni_capinfo = capinfo;
2076b032f27cSSam Leffler 		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2077b032f27cSSam Leffler 		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2078b032f27cSSam Leffler 		/*
2079b032f27cSSam Leffler 		 * Store the IEs.
2080b032f27cSSam Leffler 		 * XXX maybe better to just expand
2081b032f27cSSam Leffler 		 */
2082b032f27cSSam Leffler 		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2083b032f27cSSam Leffler #define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2084b032f27cSSam Leffler 			if (wpa != NULL)
2085b032f27cSSam Leffler 				setie(wpa_ie, wpa - sfrm);
2086b032f27cSSam Leffler 			if (rsn != NULL)
2087b032f27cSSam Leffler 				setie(rsn_ie, rsn - sfrm);
2088b032f27cSSam Leffler 			if (htcap != NULL)
2089b032f27cSSam Leffler 				setie(htcap_ie, htcap - sfrm);
2090b032f27cSSam Leffler 			if (wme != NULL) {
2091b032f27cSSam Leffler 				setie(wme_ie, wme - sfrm);
2092b032f27cSSam Leffler 				/*
2093b032f27cSSam Leffler 				 * Mark node as capable of QoS.
2094b032f27cSSam Leffler 				 */
2095b032f27cSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
2096b032f27cSSam Leffler 			} else
2097b032f27cSSam Leffler 				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2098b032f27cSSam Leffler 			if (ath != NULL) {
2099b032f27cSSam Leffler 				setie(ath_ie, ath - sfrm);
2100b032f27cSSam Leffler 				/*
2101b032f27cSSam Leffler 				 * Parse ATH station parameters.
2102b032f27cSSam Leffler 				 */
2103b032f27cSSam Leffler 				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2104b032f27cSSam Leffler 			} else
2105b032f27cSSam Leffler 				ni->ni_ath_flags = 0;
2106b032f27cSSam Leffler #undef setie
2107b032f27cSSam Leffler 		} else {
2108b032f27cSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2109b032f27cSSam Leffler 			ni->ni_ath_flags = 0;
2110b032f27cSSam Leffler 		}
2111b032f27cSSam Leffler 		ieee80211_node_join(ni, resp);
2112b032f27cSSam Leffler 		ieee80211_deliver_l2uf(ni);
2113b032f27cSSam Leffler 		break;
2114b032f27cSSam Leffler 	}
2115b032f27cSSam Leffler 
2116b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2117b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2118b032f27cSSam Leffler 		uint16_t reason;
2119b032f27cSSam Leffler 
2120b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN ||
2121b032f27cSSam Leffler 		    /* NB: can happen when in promiscuous mode */
2122b032f27cSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2123b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
2124b032f27cSSam Leffler 			break;
2125b032f27cSSam Leffler 		}
2126b032f27cSSam Leffler 		/*
2127b032f27cSSam Leffler 		 * deauth/disassoc frame format
2128b032f27cSSam Leffler 		 *	[2] reason
2129b032f27cSSam Leffler 		 */
2130b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2131b032f27cSSam Leffler 		reason = le16toh(*(uint16_t *)frm);
2132b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2133b032f27cSSam Leffler 			vap->iv_stats.is_rx_deauth++;
2134b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_deauth);
2135b032f27cSSam Leffler 		} else {
2136b032f27cSSam Leffler 			vap->iv_stats.is_rx_disassoc++;
2137b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_disassoc);
2138b032f27cSSam Leffler 		}
2139b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2140b032f27cSSam Leffler 		    "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2141b032f27cSSam Leffler 			IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2142b032f27cSSam Leffler 		if (ni != vap->iv_bss)
2143b032f27cSSam Leffler 			ieee80211_node_leave(ni);
2144b032f27cSSam Leffler 		break;
2145b032f27cSSam Leffler 	}
2146b032f27cSSam Leffler 
2147b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ACTION:
2148b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN) {
2149b032f27cSSam Leffler 			if (ieee80211_parse_action(ni, m0) == 0)
2150b032f27cSSam Leffler 				ic->ic_recv_action(ni, frm, efrm);
2151b032f27cSSam Leffler 		} else
2152b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
2153b032f27cSSam Leffler 		break;
2154b032f27cSSam Leffler 
2155b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2156b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2157b032f27cSSam Leffler 	default:
2158b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2159b032f27cSSam Leffler 		     wh, "mgt", "subtype 0x%x not handled", subtype);
2160b032f27cSSam Leffler 		vap->iv_stats.is_rx_badsubtype++;
2161b032f27cSSam Leffler 		break;
2162b032f27cSSam Leffler 	}
2163b032f27cSSam Leffler }
2164b032f27cSSam Leffler 
2165b032f27cSSam Leffler /*
2166b032f27cSSam Leffler  * Process a received ps-poll frame.
2167b032f27cSSam Leffler  */
2168b032f27cSSam Leffler static void
2169b032f27cSSam Leffler hostap_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2170b032f27cSSam Leffler {
2171b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2172b032f27cSSam Leffler 	struct ieee80211_frame_min *wh;
2173b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2174b032f27cSSam Leffler 	struct mbuf *m;
2175b032f27cSSam Leffler 	uint16_t aid;
2176b032f27cSSam Leffler 	int qlen;
2177b032f27cSSam Leffler 
2178b032f27cSSam Leffler 	wh = mtod(m0, struct ieee80211_frame_min *);
2179b032f27cSSam Leffler 	if (ni->ni_associd == 0) {
2180b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
2181b032f27cSSam Leffler 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2182b032f27cSSam Leffler 		    (struct ieee80211_frame *) wh, NULL,
2183b032f27cSSam Leffler 		    "%s", "unassociated station");
2184b032f27cSSam Leffler 		vap->iv_stats.is_ps_unassoc++;
2185b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2186b032f27cSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
2187b032f27cSSam Leffler 		return;
2188b032f27cSSam Leffler 	}
2189b032f27cSSam Leffler 
2190b032f27cSSam Leffler 	aid = le16toh(*(uint16_t *)wh->i_dur);
2191b032f27cSSam Leffler 	if (aid != ni->ni_associd) {
2192b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
2193b032f27cSSam Leffler 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2194b032f27cSSam Leffler 		    (struct ieee80211_frame *) wh, NULL,
2195b032f27cSSam Leffler 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2196b032f27cSSam Leffler 		    ni->ni_associd, aid);
2197b032f27cSSam Leffler 		vap->iv_stats.is_ps_badaid++;
2198693e3122SSam Leffler 		/*
2199693e3122SSam Leffler 		 * NB: We used to deauth the station but it turns out
2200693e3122SSam Leffler 		 * the Blackberry Curve 8230 (and perhaps other devices)
2201693e3122SSam Leffler 		 * sometimes send the wrong AID when WME is negotiated.
2202693e3122SSam Leffler 		 * Being more lenient here seems ok as we already check
2203693e3122SSam Leffler 		 * the station is associated and we only return frames
2204693e3122SSam Leffler 		 * queued for the station (i.e. we don't use the AID).
2205693e3122SSam Leffler 		 */
2206b032f27cSSam Leffler 		return;
2207b032f27cSSam Leffler 	}
2208b032f27cSSam Leffler 
2209b032f27cSSam Leffler 	/* Okay, take the first queued packet and put it out... */
2210b032f27cSSam Leffler 	IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
2211b032f27cSSam Leffler 	if (m == NULL) {
2212b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2213b032f27cSSam Leffler 		    "%s", "recv ps-poll, but queue empty");
2214b032f27cSSam Leffler 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2215b032f27cSSam Leffler 		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2216b032f27cSSam Leffler 		if (vap->iv_set_tim != NULL)
2217b032f27cSSam Leffler 			vap->iv_set_tim(ni, 0);	/* just in case */
2218b032f27cSSam Leffler 		return;
2219b032f27cSSam Leffler 	}
2220b032f27cSSam Leffler 	/*
2221b032f27cSSam Leffler 	 * If there are more packets, set the more packets bit
2222b032f27cSSam Leffler 	 * in the packet dispatched to the station; otherwise
2223b032f27cSSam Leffler 	 * turn off the TIM bit.
2224b032f27cSSam Leffler 	 */
2225b032f27cSSam Leffler 	if (qlen != 0) {
2226b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2227b032f27cSSam Leffler 		    "recv ps-poll, send packet, %u still queued", qlen);
2228b032f27cSSam Leffler 		m->m_flags |= M_MORE_DATA;
2229b032f27cSSam Leffler 	} else {
2230b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2231b032f27cSSam Leffler 		    "%s", "recv ps-poll, send packet, queue empty");
2232b032f27cSSam Leffler 		if (vap->iv_set_tim != NULL)
2233b032f27cSSam Leffler 			vap->iv_set_tim(ni, 0);
2234b032f27cSSam Leffler 	}
2235b032f27cSSam Leffler 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2236b032f27cSSam Leffler 	IF_ENQUEUE(&ifp->if_snd, m);
2237b032f27cSSam Leffler 	if_start(ifp);
2238b032f27cSSam Leffler }
2239